1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Build MM entity from NIT AST and check conformance of these entities.
18 # This module introduce specific MM class (MMSrcXXX) that specialize the abstract one from metamodel
23 private import primitive_info
25 redef class ToolContext
26 redef fun handle_property_conflict
(lc
, impls
)
28 var location
: nullable Location = null
29 if lc
isa MMSrcLocalClass then
31 if node
!= null then node
.location
33 #if location == null then location = lc.mmmodule.location
34 var clas
= new Array[MMLocalClass]
36 clas
.add
(i
.local_class
)
38 self.fatal_error
(location
, "Property inheritance conflict in class {lc} for `{impls.first.name}': conflicting properties are defined in {clas.join(", ")}")
42 # Class specialization hierarchy sorter
43 private class CSHSorter
44 super AbstractSorter[MMLocalClass]
45 redef fun compare
(a
, b
)
47 return a
.cshe
.rank
<=> b
.cshe
.rank
53 redef class MMSrcModule
54 # Syntax analysis and MM construction for the module
55 # Require that supermodules are processed
56 fun do_mmbuilder
(tc
: ToolContext)
58 # Import global classes
61 # Create local classes and attach them to global classes
62 var mmbv
= new ClassBuilderVisitor(tc
, self)
63 mmbv
.enter_visit
(node
)
66 if mhe
.direct_greaters
.is_empty
then
67 process_default_classes
(tc
)
70 # Import unrefined local classes and attach them to global classes
73 # Resolve classes in super clauses
74 var mmbv1
= new ClassSpecializationBuilderVisitor(tc
, self)
75 mmbv1
.enter_visit
(node
)
78 # Compute specialization relation
79 for c
in local_classes
do
80 if visibility_for
(c
.global
.intro
.mmmodule
) < c
.global
.visibility_level
then
83 c
.compute_super_classes
87 # Class that we will process now are those in the hierarchy
88 # Its mean all the visible classes and their super-classes
89 # Note that leaves invisible classes are not in the 'classes' set
90 var classes
= class_specialization_hierarchy
.to_a
92 # Prepare class list to process the following in a right order
93 var sorter
= once
new CSHSorter
96 # Compute class ancestors types
97 var mmbv1b
= new ClassAncestorBuilder(tc
, self)
99 c
.accept_class_visitor
(mmbv1b
)
104 # Check class conformity
105 var mmbv1c
= new ClassVerifierVisitor(tc
, self)
107 c
.accept_class_visitor
(mmbv1c
)
111 # Property inhritance and introduction
112 var mmbv2
= new PropertyBuilderVisitor(tc
, self)
114 # Global property introduction and redefinition
115 c
.accept_class_visitor
(mmbv2
)
117 # Default and inherited constructor if needed
118 if c
isa MMSrcLocalClass and c
.global
.intro
== c
and not c
.global
.is_enum
and not c
.global
.is_extern
and not c
.global
.is_interface
then
119 c
.process_default_constructors
(mmbv2
)
122 # Note that inherited unredefined property are processed on demand latter
126 # Property signature analysis and inheritance conformance
127 var mmbv3
= new PropertyVerifierVisitor(tc
, self)
129 c
.accept_properties_visitor
(mmbv3
)
135 # Create some primitive default classes if they do not exists
136 fun process_default_classes
(tc
: ToolContext)
138 var name
= once
("Object".to_symbol
)
139 if not has_global_class_named
(name
) then
140 var c
= new MMSrcLocalClass(self, name
, null, 0)
142 src_local_classes
[name
] = c
144 name
= once
("Bool".to_symbol
)
145 if not has_global_class_named
(name
) then
146 var c
= new MMSrcLocalClass(self, name
, null, 0)
148 src_local_classes
[name
] = c
153 redef class MMLocalClass
154 # Accept a class visitor (on class nodes)
155 private fun accept_class_visitor
(v
: AbsSyntaxVisitor)
159 # Accept a class visitor (on class properties)
160 private fun accept_properties_visitor
(v
: AbsSyntaxVisitor)
165 redef class MMSrcLocalClass
166 redef fun accept_class_visitor
(v
)
175 # Accept a class visitor (on class properties)
176 redef fun accept_properties_visitor
(v
)
184 for p
in src_local_properties
.values
do
185 p
.accept_property_visitor
(v
)
189 # Introduce or inherit default constructors
190 private fun process_default_constructors
(v
: PropertyBuilderVisitor)
192 # Is there already a constructor ?
193 for gp
in global_properties
do
195 # Return if explicit constructor in the class
196 if gp
.intro
.local_class
== self then return
200 # Collect visible constructors in super stateful classes
201 var super_inits
= new ArraySet[MMLocalProperty]
202 var super_constructors
= new ArraySet[MMGlobalProperty]
203 for sc
in che
.direct_greaters
do
204 if sc
.global
.is_enum
and not sc
.global
.is_extern
or sc
.global
.is_interface
then continue
205 for gp
in sc
.global_properties
do
206 if not gp
.is_init
then continue
207 super_constructors
.add
(gp
)
209 var initname
= once
("init".to_symbol
)
210 if sc
.has_global_property_by_name
(initname
) then
211 var gp
= sc
.get_property_by_name
(initname
)
212 super_inits
.add
(self[gp
])
216 # Collect unassigned attributes
217 var unassigned_attributes
= new Array[MMSrcAttribute]
218 for a
in src_local_properties
.values
do
219 if a
isa MMSrcAttribute then
221 if n
.n_expr
== null then unassigned_attributes
.add
(a
)
225 if not super_constructors
.is_empty
then
226 # Select most specific classes introducing inheritable constructors
227 # Mixin classes are skipped
228 var supers
= new Array[MMLocalClass]
229 for gp
in super_constructors
do
230 var sc
= gp
.local_class
231 if supers
.has
(sc
) then continue
232 if not sc
.global
.is_mixin
then
236 supers
= che
.order
.select_smallests
(supers
)
238 # A mixin class can only have 0 or 1 most specific non-mixin superclass
239 var superclass
: nullable MMLocalClass = null # This most specific non-mixin superclass (if any)
241 if supers
.length
> 1 then
242 v
.error
(node
, "Error: Explicit constructor required in {self} since multiple inheritance of constructor is forbiden. Conflicting classes are {supers.join(", ")}. Costructors are {super_constructors.join(", ")}.")
244 else if supers
.length
== 1 then
245 superclass
= supers
.first
248 for gp
in super_constructors
do
249 # Inherit constructors : the one of the non-mixin super class or all from the all mixin super-classes
250 if superclass
== null or gp
.local_class
== superclass
then
251 make_visible_an_inherited_global_property
(gp
)
254 global
.mixin_of
= superclass
.as(not null).global
# FIXME Dear! this should break!
256 # v.error(nodes.first, "Error, constructor required in {self} since no anonimous init found in {sc}.")
258 # unassigned attributes, then implicit consructors are generated
259 var p
= new MMImplicitInit(self, unassigned_attributes
, super_inits
.to_a
)
260 add_src_local_property
(v
, p
)
261 #print("Create implicit init {p} in {self} from {super_inits.join(", ")} + {unassigned_attributes.length} args")
265 # Add a source property
266 # Register it to the class and attach it to global property
267 private fun add_src_local_property
(v
: PropertyBuilderVisitor, prop
: MMLocalProperty)
269 var pname
= prop
.name
270 # Check double definition in the same class
271 if src_local_properties
.has_key
(pname
) then
272 v
.error
(prop
.node
, "Error: A property {pname} is already defined in class {name}.")
275 src_local_properties
[pname
] = prop
277 # Intro or redefinition ?
278 if has_global_property_by_name
(pname
) then
279 var g
= get_property_by_name
(pname
)
280 prop
.inherit_global
(g
)
283 if not prop
.is_global_set
then
285 prop
.global
.is_init
= prop
.is_init
290 redef class MMLocalProperty
291 private fun accept_property_visitor
(v
: AbsSyntaxVisitor)
296 redef class MMImplicitInit
297 redef readable var _super_init
: nullable MMLocalProperty = null
298 redef fun accept_property_visitor
(v
)
300 var base
: nullable MMLocalProperty = null
301 for p
in super_inits
do
302 if p
.signature
.arity
> 0 then
306 v
.error
(null, "Error: explicit constructor needed in {local_class} since both super-constructor {base.full_name} and {p.full_name} have paramters")
313 var params
= new Array[MMParam]
315 var sig
= base
.signature
316 for i
in [0..sig
.arity
[ do
317 params
.add
(sig
.params
[i
])
320 for a
in unassigned_attributes
do
321 var sig
= a
.signature
322 if sig
== null then return # Broken attribute definition
324 if name
.to_s
.first
== '_' or name
.to_s
.first
== '@' then
325 name
= a
.to_s
.substring_from
(1).to_symbol
327 params
.add
(new MMParam(sig
.return_type
.as(not null), name
))
329 signature
= new MMSignature(params
, null, local_class
.get_type
)
334 # Concrete NIT class specialization relation
337 redef readable var _local_class
: MMLocalClass
339 init(c
: MMLocalClass)
345 ###############################################################################
347 # A pass visitor for syntax analysis.
348 # * Build the classes and attach them to global classes
349 # * Collect generic formal parameters.
350 private class ClassBuilderVisitor
351 super AbsSyntaxVisitor
352 # Current class arity
353 readable writable var _local_class_arity
: Int = 0
355 # Current class formal parameters
356 readable writable var _formals
: nullable Map[Symbol, MMTypeFormalParameter]
358 redef fun visit
(n
) do n
.accept_class_builder
(self)
362 # Another pass visitor for syntax analysis.
363 # * Build ancertors (with only class informations not the type one)
364 private class ClassSpecializationBuilderVisitor
365 super AbsSyntaxVisitor
366 redef fun visit
(n
) do n
.accept_class_specialization_builder
(self)
370 # Another pass visitor for syntax analysis.
371 # * Compute types in ancestors
372 private class ClassAncestorBuilder
373 super AbsSyntaxVisitor
374 redef fun visit
(n
) do n
.accept_class_ancestor_builder
(self)
378 # Another pass visitor for syntax analysis.
379 # * Checks classes in regard to superclasses
380 private class ClassVerifierVisitor
381 super AbsSyntaxVisitor
382 redef fun visit
(n
) do n
.accept_class_verifier
(self)
387 # Another pass visitor for syntax analysis.
388 # * Build propertie names
389 # * Build local properties and attache them to global properties
390 # * Attach bound to formal types
391 private class PropertyBuilderVisitor
392 super AbsSyntaxVisitor
393 redef fun visit
(n
) do n
.accept_property_builder
(self)
397 # Another pass pass visitor for syntax analysis.
398 # * Check property conformance
399 private class PropertyVerifierVisitor
400 super AbsSyntaxVisitor
402 # The signature currently build
403 readable writable var _signature_builder
: SignatureBuilder
405 redef fun visit
(n
) do n
.accept_property_verifier
(self)
410 _signature_builder
= new SignatureBuilder
414 # Information about a signature currently build
415 private class SignatureBuilder
416 # Current visited parameter types
417 readable writable var _params
: Array[AParam] = new Array[AParam]
419 # Visited parameters without type information added
420 readable writable var _untyped_params
: Array[AParam] = new Array[AParam]
422 # Position of the current star parameter
423 readable writable var _vararg_rank
: Int = -1
425 # Current closure declarations
426 readable writable var _closure_decls
: Array[AClosureDecl] = new Array[AClosureDecl]
428 # True is a problen occured durring building
429 readable writable var _has_error_occured
: Bool = false
432 readable writable var _signature
: nullable MMSignature = null
435 ###############################################################################
438 private fun accept_class_builder
(v
: ClassBuilderVisitor) do accept_abs_syntax_visitor
(v
)
439 private fun accept_class_specialization_builder
(v
: ClassSpecializationBuilderVisitor) do accept_abs_syntax_visitor
(v
)
440 private fun accept_class_ancestor_builder
(v
: ClassAncestorBuilder) do accept_abs_syntax_visitor
(v
)
441 private fun accept_class_verifier
(v
: ClassVerifierVisitor) do accept_abs_syntax_visitor
(v
)
442 private fun accept_property_builder
(v
: PropertyBuilderVisitor) do accept_abs_syntax_visitor
(v
)
443 private fun accept_property_verifier
(v
: PropertyVerifierVisitor) do accept_abs_syntax_visitor
(v
)
447 # Import supermodules and compute visibility
448 fun import_super_modules
(tc
: ToolContext, mod
: MMSrcModule)
450 # Import super-modules
451 var supers
= new Array[MMModule]
452 var no_import
: nullable AImport = null
453 for i
in n_imports
do
454 var n
= i
.module_name
456 var m
= tc
.get_module
(n
, mod
)
458 mod
.add_super_module
(m
, i
.visibility_level
)
463 if no_import
!= null then
464 if not supers
.is_empty
then
465 tc
.error
(no_import
.location
, "Error: Top modules cannot import other modules.")
467 else if supers
.is_empty
then
468 var stdname
= once
"standard".to_symbol
469 var m
= tc
.get_module
(stdname
, mod
)
471 mod
.add_super_module
(m
, 1)
474 tc
.add_module
(mod
, supers
)
478 redef class AModuledecl
479 redef fun accept_class_builder
(v
)
481 if n_name
.n_id
.to_symbol
!= v
.mmmodule
.name
then
482 v
.error
(n_name
.n_id
, "Error: Module name mismatch between {v.mmmodule.name} and {n_name.n_id.to_symbol}")
488 # Imported module name (or null)
489 fun module_name
: nullable Symbol is abstract
491 # Visibility level (intrude/public/private)
492 fun visibility_level
: Int is abstract
494 redef class AStdImport
495 redef fun module_name
497 return n_name
.n_id
.to_symbol
499 redef fun visibility_level
501 return n_visibility
.level
504 redef class ANoImport
505 redef fun module_name
511 redef class AVisibility
513 fun level
: Int is abstract
515 redef class APublicVisibility
516 redef fun level
do return 1
518 redef class AProtectedVisibility
519 redef fun level
do return 2
521 redef class APrivateVisibility
522 redef fun level
do return 3
524 redef class AIntrudeVisibility
525 redef fun level
do return 0
529 redef class AClassdef
530 redef fun local_class
: MMSrcLocalClass do return _local_class
.as(not null)
531 var _local_class
: nullable MMSrcLocalClass
534 fun name
: Symbol is abstract
536 # Number of formal parameters
537 fun arity
: Int do return 0
539 # Visibility of the class
540 fun visibility_level
: Int do return 1
542 redef fun accept_class_builder
(v
)
544 var local_class
: MMSrcLocalClass
546 var local_classes
= mod
.src_local_classes
547 if local_classes
.has_key
(name
) then
548 local_class
= local_classes
[name
]
549 _local_class
= local_class
550 if self isa AStdClassdef then
551 # If we are not a special implicit class then rant
552 v
.error
(self, "Error: A class {name} is already defined at line {local_class.node.location.line_start}.")
555 # Add the new node after the last node
556 var n
= local_class
.node
557 while n
.next_node
!= null do n
= n
.next_node
560 local_class
= new MMSrcLocalClass(mod
, name
, self, arity
)
561 _local_class
= local_class
562 local_classes
[name
] = local_class
563 if not mod
.has_global_class_named
(name
) then
564 build_class_introduction
(v
)
566 var glob
= mod
.global_class_named
(name
)
567 build_class_refinement
(v
, glob
)
571 v
.local_class_arity
= 0
572 v
.formals
= local_class
.formal_dict
581 fun build_class_introduction
(v
: AbsSyntaxVisitor)
583 local_class
.new_global
584 var glob
= local_class
.global
586 glob
.visibility_level
= visibility_level
587 if self isa AStdClassdef then
588 if n_kwredef
!= null then
589 v
.error
(self, "Redef error: No class {name} is imported. Remove the redef keyword to define a new class.")
592 glob
.is_interface
= n_classkind
.is_interface
593 glob
.is_abstract
= n_classkind
.is_abstract
594 glob
.is_enum
= n_classkind
.is_enum
598 fun build_class_refinement
(v
: AbsSyntaxVisitor, glob
: MMGlobalClass)
600 local_class
.set_global
(glob
)
602 glob
.check_visibility
(v
, self, v
.mmmodule
)
603 if self isa AStdClassdef and n_kwredef
== null then
604 v
.error
(self, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
608 if glob
.intro
.arity
!= _local_class
.arity
then
609 v
.error
(self, "Redef error: Formal parameter arity missmatch; got {_local_class.arity}, expected {glob.intro.arity}.")
612 if self isa AStdClassdef and (not glob
.is_interface
and n_classkind
.is_interface
or
613 not glob
.is_abstract
and n_classkind
.is_abstract
or
614 not glob
.is_enum
and n_classkind
.is_enum
)
616 v
.error
(self, "Redef error: cannot change kind of class {name}.")
620 redef fun accept_class_verifier
(v
)
623 var glob
= _local_class
.global
624 for c
in _local_class
.cshe
.direct_greaters
do
626 if glob
.is_interface
then
628 v
.error
(self, "Special error: Interface {name} try to specialise enum class {c.name}.")
629 else if not cg
.is_interface
then
630 v
.error
(self, "Special error: Interface {name} try to specialise class {c.name}.")
632 else if glob
.is_enum
then
633 if not cg
.is_interface
and not cg
.is_enum
then
634 v
.error
(self, "Special error: Enum class {name} try to specialise class {c.name}.")
638 v
.error
(self, "Special error: Class {name} try to specialise enum class {c.name}.")
645 redef fun accept_abs_syntax_visitor
(v
)
647 v
.local_class
= _local_class
653 redef class AClasskind
654 fun is_interface
: Bool do return false
655 fun is_enum
: Bool do return false
656 fun is_abstract
: Bool do return false
657 fun is_extern
: Bool do return false
660 redef class AInterfaceClasskind
661 redef fun is_interface
do return true
663 redef class AEnumClasskind
664 redef fun is_enum
do return true
666 redef class AExternClasskind
667 redef fun is_extern
do return true
669 redef class AAbstractClasskind
670 redef fun is_abstract
do return true
673 redef class AStdClassdef
676 return n_id
.to_symbol
680 return n_formaldefs
.length
682 redef fun accept_class_specialization_builder
(v
)
686 var glob
= local_class
.global
687 if glob
.intro
== local_class
then
688 glob
.is_interface
= n_classkind
.is_interface
689 glob
.is_abstract
= n_classkind
.is_abstract
690 glob
.is_enum
= n_classkind
.is_enum
691 glob
.is_extern
= n_classkind
.is_extern
692 glob
.visibility_level
= visibility_level
695 redef fun accept_class_verifier
(v
)
698 var glob
= _local_class
.global
699 if glob
.intro
== _local_class
then
701 if n_kwredef
!= null then
702 v
.error
(self, "Redef error: No class {name} is imported. Remove the redef keyword to define a new class.")
704 if glob
.is_extern
then
705 glob
.mmmodule
.is_extern_hybrid
= true
708 for c
in _local_class
.cshe
.direct_greaters
do
710 if glob
.is_interface
then
712 v
.error
(self, "Special error: Interface {name} try to specialise enum class {c.name}.")
713 else if not cg
.is_interface
then
714 v
.error
(self, "Special error: Interface {name} try to specialise class {c.name}.")
716 else if glob
.is_enum
then
717 if not cg
.is_interface
and not cg
.is_enum
then
718 v
.error
(self, "Special error: Enum class {name} try to specialise class {c.name}.")
720 else if glob
.is_extern
then
721 if not cg
.is_interface
and not cg
.is_extern
then
722 v
.error
(self, "Special error: Extern class {name} try to specialise class {c.name}.")
726 v
.error
(self, "Special error: Class {name} try to specialise enum class {c.name}.")
727 else if cg
.is_extern
then
728 v
.error
(self, "Special error: Class {name} try to specialise extern class {c.name}.")
738 glob
.check_visibility
(v
, self, v
.mmmodule
)
739 if n_kwredef
== null then
740 v
.error
(self, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
744 if glob
.intro
.arity
!= _local_class
.arity
then
745 v
.error
(self, "Redef error: Formal parameter arity mismatch; got {_local_class.arity}, expected {glob.intro.arity}.")
749 not glob
.is_interface
and n_classkind
.is_interface
or
750 not glob
.is_abstract
and n_classkind
.is_abstract
or
751 not glob
.is_enum
and n_classkind
.is_enum
or
752 not glob
.is_extern
and n_classkind
.is_extern
754 v
.error
(self, "Redef error: cannot change kind of class {name}.")
758 redef fun visibility_level
760 return n_visibility
.level
764 redef class AMainClassdef
767 return once
"Sys".to_symbol
771 redef class ATopClassdef
774 return once
"Object".to_symbol
778 redef class AFormaldef
779 # The associated formal generic parameter (MM entity)
780 var _formal
: nullable MMTypeFormalParameter
782 redef fun accept_class_builder
(v
)
784 var name
= n_id
.to_symbol
785 var formal_type
= new MMTypeFormalParameter(name
, v
.local_class_arity
, v
.local_class
)
786 _formal
= formal_type
787 v
.local_class_arity
= v
.local_class_arity
+ 1
788 v
.local_class
.register_formal
(formal_type
)
789 v
.formals
[name
] = formal_type
793 redef fun accept_class_verifier
(v
)
796 var c
= v
.local_class
797 var o
= c
.global
.intro
799 if n_type
== null then
800 _formal
.bound
= v
.mmmodule
.type_any
.as_nullable
802 var stype
= n_type
.get_stype
(v
)
803 if stype
== null then return
804 _formal
.bound
= stype
807 var ob
= o
.get_formal
(_formal
.position
).bound
.for_module
(v
.mmmodule
)
808 if n_type
== null then
811 var stype
= n_type
.get_stype
(v
)
812 if stype
== null then return
813 _formal
.bound
= stype
814 if _formal
.bound
!= ob
then
815 v
.error
(self, "Redef error: Cannot change formal parameter type of class {c}; got {_formal.bound}, expected {ob}.")
822 redef class ASuperclass
823 readable var _ancestor
: nullable MMSrcAncestor
825 redef fun accept_class_specialization_builder
(v
)
828 var c
= n_type
.get_local_class
(v
)
829 if c
== null then return
830 var ancestor
= new MMSrcAncestor(c
)
832 v
.local_class
.add_direct_parent
(ancestor
)
835 redef fun accept_class_ancestor_builder
(v
)
838 _ancestor
.stype
= n_type
.get_unchecked_stype
(v
)
839 _ancestor
.inheriter
= v
.local_class
.get_type
842 redef fun accept_class_verifier
(v
)
845 n_type
.check_conform
(v
)
850 # Process and check properties of the property.
851 # * Distinguish inits and methods
852 # * Inherit or check visibility.
853 # * Check redef errors.
854 # * Check forbiden attribute definitions.
855 # * Check signature conformance.
856 private fun process_and_check
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
858 if prop
.global
.intro
== prop
then
859 do_and_check_intro
(v
, prop
, has_redef
, visibility_level
)
861 do_and_check_redef
(v
, prop
, has_redef
, visibility_level
)
865 # The part of process_and_check when prop is an introduction
866 private fun do_and_check_intro
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
868 var glob
= prop
.global
869 var gbc
= prop
.local_class
.global
870 if v
.local_class
.global
.visibility_level
>= 3 then
871 # Method of private classes are private
874 glob
.visibility_level
= visibility_level
876 v
.error
(self, "Error: No property {prop.local_class}::{prop} is inherited. Remove the redef keyword to define a new property.")
878 if glob
.is_attribute
then
879 if gbc
.is_interface
then
880 v
.error
(self, "Error: Attempt to define attribute {prop} in the interface {prop.local_class}.")
881 else if gbc
.is_enum
then
882 v
.error
(self, "Error: Attempt to define attribute {prop} in the enum class {prop.local_class}.")
883 else if gbc
.is_extern
then
884 v
.error
(self, "Error: Attempt to define attribute {prop} in the extern class {prop.local_class}.")
886 else if glob
.is_init
then
887 if gbc
.is_interface
then
888 v
.error
(self, "Error: Attempt to define a constructor {prop} in the class {prop.local_class}.")
889 else if gbc
.is_enum
then
890 v
.error
(self, "Error: Attempt to define a constructor {prop} in the enum {prop.local_class}.")
895 if prop
.signature
== null then
897 var supers
= prop
.local_class
.super_methods_named
(prop
.name
)
898 inherit_signature
(v
, prop
, supers
)
900 if prop
.signature
!= null or v
.signature_builder
.has_error_occured
then
902 else if not v
.signature_builder
.untyped_params
.is_empty
then
903 v
.error
(v
.signature_builder
.untyped_params
.first
, "Error: Untyped parameter.")
905 prop
.signature
= new MMSignature(new Array[MMParam], null, v
.local_class
.get_type
)
906 for clos
in v
.signature_builder
.closure_decls
do
907 prop
.signature
.closures
.add
(clos
.variable
.closure
)
913 private fun inherit_signature
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, supers
: Array[MMLocalProperty])
915 var s
= prop
.signature
917 var isig
= ip
.signature
.adaptation_to
(v
.local_class
.get_type
)
920 if v
.signature_builder
.params
.length
!= isig
.arity
then
923 for p
in v
.signature_builder
.params
do
924 var t
= isig
[p
.position
]
926 if p
.position
== isig
.vararg_rank
then
938 # The part of process_and_check when prop is a redefinition
939 private fun do_and_check_redef
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
941 var is_init
= self isa AInitPropdef
942 var glob
= prop
.global
944 if not has_redef
then
945 v
.error
(self, "Redef error: {prop.local_class}::{prop} is an inherited property. To redefine it, add the redef keyword.")
948 if glob
.is_init
and not is_init
then
949 v
.error
(self, "Redef error: A method {prop.local_class}::{prop} cannot redefine a constructor.")
950 else if not glob
.is_init
and is_init
then
951 v
.error
(self, "Redef error: A constructor {prop.local_class}::{prop} cannot redefine a method.")
954 var s
= prop
.signature
955 #print "process {prop.local_class.mmmodule}::{prop.local_class}::{prop} from global {prop.global.local_property.local_class.mmmodule}::{prop.global.local_property.local_class}::{prop.global.local_property}"
956 for i
in prop
.prhe
.direct_greaters
do
957 var ip
= i
.local_class
[prop
.global
]
958 var isig
= i
.signature
959 if isig
== null then break # previous signature is invalid
960 isig
= isig
.adaptation_to
(v
.local_class
.get_type
)
963 #print "{prop.full_name} inherits signature from {ip.full_name}"
964 if v
.signature_builder
.params
.length
!= isig
.arity
then
965 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
968 if v
.signature_builder
.closure_decls
.length
!= isig
.closures
.length
then
969 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} closure(s).")
972 for p
in v
.signature_builder
.params
do
973 var t
= isig
[p
.position
]
975 if p
.position
== isig
.vararg_rank
then
980 isig
.params
[p
.position
].name
= p
.variable
.name
987 var nberr
= v
.tc
.error_count
988 #print "Check {prop.local_class}::{prop}{s} vs {ip.local_class}::{ip}{isig}"
989 #print "s={s.object_id} isig={isig.object_id} isigorig={i.signature.object_id}"
991 #print "orig signature: {i.signature.recv} . {i.signature}"
992 #print "inh signature: {isig.recv} . {isig}"
993 #print "redef signature: {s.recv} . {s}"
995 if s
.arity
!= isig
.arity
then
996 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
997 else if s
.arity
> 0 then
998 if self isa AMethPropdef then
1000 for j
in [0..s
.arity
[ do
1001 if s
[j
] != isig
[j
] then
1002 v
.error
(n_signature
.n_params
[j
], "Redef error: Expected {isig[j]}, as in {ip.local_class}::{ip}.")
1005 else if self isa AAttrPropdef then
1007 if s
[0] != isig
[0] then
1008 v
.error
(n_type
, "Redef error: Expected {isig[0]}, as in the parameter of {ip.local_class}::{ip}.")
1016 var srt
= s
.return_type
1017 var isrt
= isig
.return_type
1018 if srt
== null and isrt
!= null then
1019 v
.error
(self, "Redef error: The procedure {prop.local_class}::{prop} redefines the function {ip.local_class}::{ip}.")
1020 else if srt
!= null and isrt
== null then
1021 v
.error
(self, "Redef error: The function {prop.local_class}::{prop} redefines the procedure {ip.local_class}::{ip}.")
1022 else if srt
!= null and isrt
!= null and not srt
< isrt
then
1023 var n
: nullable ANode = null
1024 if self isa AMethPropdef then
1025 n
= self.n_signature
.n_type
1026 else if self isa AAttrPropdef then
1028 else if self isa ATypePropdef then
1031 v
.error
(n
, "Redef error: Expected {isrt}, as in {ip.local_class}::{ip}.")
1032 else if not s
< isig
and nberr
== v
.tc
.error_count
then
1033 # Systematic fallback for conformance check
1034 v
.error
(self, "Redef error: Incompatible redefinition of {ip.local_class}::{ip} with {prop.local_class}::{prop}")
1035 else if srt
!= null and isrt
!= null and srt
!= isrt
and prop
isa MMAttribute then
1037 v
.warning
(self, "Redef warning: Expected {isrt}, as in {ip.local_class}::{ip}.")
1041 if visibility_level
!= 1 and glob
.visibility_level
!= visibility_level
then
1042 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefinition cannot change visibility.")
1044 glob
.check_visibility
(v
, self, v
.mmmodule
, true)
1048 redef class AAttrPropdef
1049 redef readable var _readmethod
: nullable MMSrcMethod
1050 redef readable var _writemethod
: nullable MMSrcMethod
1051 var _prop
: nullable MMSrcAttribute
1052 redef fun prop
do return _prop
.as(not null)
1054 redef fun accept_property_builder
(v
)
1058 if n_id
!= null then
1059 name
= n_id
.to_symbol
1061 name
= ("@" + n_id2
.text
).to_symbol
1063 var lc
= v
.local_class
1064 var prop
= new MMSrcAttribute(name
, lc
, self)
1066 v
.local_class
.add_src_local_property
(v
, prop
)
1068 if n_readable
!= null or n_id
== null then
1069 if n_id
!= null then
1070 name
= n_id
.text
.substring_from
(1).to_symbol
1072 name
= n_id2
.to_symbol
1074 var readmethod
= new MMReadImplementationMethod(name
, lc
, self)
1075 _readmethod
= readmethod
1076 v
.local_class
.add_src_local_property
(v
, readmethod
)
1078 if n_writable
!= null or n_id
== null then
1079 if n_id
!= null then
1080 name
= (n_id
.text
.substring_from
(1) + "=").to_symbol
1082 name
= (n_id2
.text
+ "=").to_symbol
1084 var writemethod
= new MMWriteImplementationMethod(name
, lc
, self)
1085 _writemethod
= writemethod
1086 v
.local_class
.add_src_local_property
(v
, writemethod
)
1090 redef fun accept_property_verifier
(v
)
1094 if n_type
!= null then
1095 var t0
= n_type
.get_stype
(v
)
1096 if t0
!= null then t
= t0
else return
1098 v
.error
(self, "Not yet implemented: Attribute definition {prop.local_class}::{prop} requires an explicit type.")
1103 var signature
= new MMSignature(new Array[MMParam], t
, v
.local_class
.get_type
)
1104 prop
.signature
= signature
1105 var visibility_level
= n_visibility
.level
1106 process_and_check
(v
, prop
, n_id
!= null and n_kwredef
!= null, visibility_level
)
1107 if n_readable
!= null or n_id
== null then
1108 var m
= _readmethod
.as(not null)
1109 m
.signature
= signature
1110 process_and_check
(v
, m
, (n_readable
!= null and n_readable
.n_kwredef
!= null) or (n_id
== null and n_kwredef
!= null), visibility_level
)
1111 n_type
.check_visibility
(v
, m
)
1113 if n_writable
!= null or n_id
== null then
1114 var m
= _writemethod
.as(not null)
1115 m
.signature
= new MMSignature(new Array[MMParam].with_items
(new MMParam(t
, once
"value".to_symbol
)), null, v
.local_class
.get_type
)
1116 var vl
= visibility_level
1117 if n_id
== null then
1118 if n_writable
== null then vl
= 3 else vl
= n_writable
.n_visibility
.level
# write accessor has a specific visibility
1120 process_and_check
(v
, m
, n_writable
!= null and n_writable
.n_kwredef
!= null, vl
)
1121 n_type
.check_visibility
(v
, m
)
1125 redef fun accept_abs_syntax_visitor
(v
)
1127 v
.local_property
= _prop
1129 v
.local_property
= null
1133 redef class AMethPropdef
1134 # Name of the method
1135 readable var _name
: nullable Symbol
1137 var _method
: nullable MMMethSrcMethod
1138 redef fun method
do return _method
.as(not null)
1140 redef fun accept_property_builder
(v
)
1144 if n_methid
== null then
1145 if self isa AInitPropdef then
1146 name
= once
"init".to_symbol
1148 name
= once
"main".to_symbol
1151 name
= n_methid
.name
.as(not null)
1152 # FIXME: Add the 'unary' keyword
1153 if n_methid
.name
== (once
"-".to_symbol
) then
1154 var ns
= n_signature
1155 if ns
!= null and ns
.n_params
.length
== 0 then
1156 name
= once
"unary -".to_symbol
1161 var prop
= new MMMethSrcMethod(name
, v
.local_class
, self)
1163 v
.local_class
.add_src_local_property
(v
, prop
)
1166 redef fun accept_property_verifier
(v
)
1168 v
.signature_builder
= new SignatureBuilder
1171 if v
.signature_builder
.has_error_occured
then return
1173 if v
.signature_builder
.signature
== null then
1174 #_method.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
1176 method
.signature
= v
.signature_builder
.signature
.as(not null)
1178 var visibility_level
= 1
1179 if n_visibility
!= null and n_visibility
.level
> 1 then
1180 visibility_level
= n_visibility
.level
1182 process_and_check
(v
, method
, n_kwredef
!= null, visibility_level
)
1183 if n_signature
!= null then n_signature
.check_visibility
(v
, method
)
1186 redef fun accept_abs_syntax_visitor
(v
)
1188 v
.local_property
= _method
1190 v
.local_property
= null
1194 redef class AMainMethPropdef
1195 redef fun process_and_check
(v
, prop
, has_redef
, visibility_level
)
1197 prop
.global
.visibility_level
= visibility_level
1198 prop
.signature
= new MMSignature(new Array[MMParam], null, v
.local_class
.get_type
)
1199 # Disable all checks for main
1203 redef class AExternPropdef
1204 redef fun accept_property_verifier
(v
)
1206 super # Compute signature
1208 if n_extern
!= null then
1209 ename
= n_extern
.text
1210 ename
= ename
.substring
(1, ename
.length-2
)
1212 ename
= method
.default_extern_name
1214 method
.extern_name
= ename
1218 redef class ATypePropdef
1219 redef fun prop
do return _prop
.as(not null)
1220 var _prop
: nullable MMSrcTypeProperty
1222 redef fun accept_property_builder
(v
)
1225 var name
= n_id
.to_symbol
1226 var prop
= new MMSrcTypeProperty(name
, v
.local_class
, self)
1228 v
.local_class
.add_src_local_property
(v
, prop
)
1231 redef fun accept_property_verifier
(v
)
1234 var signature
= new MMSignature(new Array[MMParam], n_type
.get_stype
(v
), v
.local_class
.get_type
)
1235 prop
.signature
= signature
1236 var visibility_level
= n_visibility
.level
1237 process_and_check
(v
, prop
, n_kwredef
!= null, visibility_level
)
1240 redef fun accept_abs_syntax_visitor
(v
)
1242 v
.local_property
= _prop
1244 v
.local_property
= null
1248 # Visitor used to build a full method name from multiple tokens
1249 private class MethidAccumulator
1251 readable var _name
: Buffer = new Buffer
1255 _name
.append
(n
.text
)
1263 redef readable var _name
: nullable Symbol
1265 redef fun accept_property_builder
(v
)
1267 var accumulator
= new MethidAccumulator
1268 accumulator
.enter_visit
(self)
1269 _name
= accumulator
.name
.to_s
.to_symbol
1274 redef class ASignature
1275 redef fun accept_property_verifier
(v
)
1278 if v
.signature_builder
.has_error_occured
then
1280 else if not v
.signature_builder
.untyped_params
.is_empty
then
1281 if v
.signature_builder
.untyped_params
.first
!= v
.signature_builder
.params
.first
or n_type
!= null then
1282 v
.error
(v
.signature_builder
.untyped_params
.first
, "Syntax error: untyped parameter.")
1285 else if not v
.signature_builder
.params
.is_empty
or n_type
!= null then
1286 var pars
= new Array[MMParam]
1287 for p
in v
.signature_builder
.params
do
1288 pars
.add
( new MMParam( p
.stype
.as(not null), p
.n_id
.to_symbol
) )
1290 var ret
: nullable MMType = null
1291 if n_type
!= null then
1292 ret
= n_type
.get_stype
(v
)
1294 v
.signature_builder
.has_error_occured
= true
1298 v
.signature_builder
.signature
= new MMSignature(pars
, ret
, v
.local_class
.get_type
)
1299 if v
.signature_builder
.vararg_rank
>= 0 then
1300 v
.signature_builder
.signature
.vararg_rank
= v
.signature_builder
.vararg_rank
1302 for clos
in v
.signature_builder
.closure_decls
do
1303 v
.signature_builder
.signature
.closures
.add
(clos
.variable
.closure
)
1308 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1309 fun check_visibility
(v
: AbsSyntaxVisitor, p
: MMLocalProperty)
1311 if p
.global
.visibility_level
>= 3 then return
1312 for n
in n_params
do
1313 if n
.n_type
!= null then n
.n_type
.check_visibility
(v
, p
)
1315 if n_type
!= null then n_type
.check_visibility
(v
, p
)
1320 redef readable var _position
: Int = 0
1322 redef fun variable
: ParamVariable do return _variable
.as(not null)
1323 var _variable
: nullable ParamVariable
1325 # The type of the parameter in signature
1326 readable writable var _stype
: nullable MMType
1328 redef fun accept_property_verifier
(v
)
1331 _position
= v
.signature_builder
.params
.length
1332 _variable
= new ParamVariable(n_id
.to_symbol
, n_id
)
1333 v
.signature_builder
.params
.add
(self)
1334 v
.signature_builder
.untyped_params
.add
(self)
1335 if n_type
!= null then
1336 var stype
= n_type
.get_stype
(v
)
1337 if stype
== null then
1338 v
.signature_builder
.has_error_occured
= true
1341 for p
in v
.signature_builder
.untyped_params
do
1344 if v
.signature_builder
.vararg_rank
== -1 then
1345 v
.signature_builder
.vararg_rank
= p
.position
1347 v
.error
(self, "Error: A vararg parameter is already defined.")
1349 stype
= v
.type_array
(stype
)
1351 p
.variable
.stype
= stype
1353 v
.signature_builder
.untyped_params
.clear
1357 fun is_vararg
: Bool do return n_dotdotdot
!= null
1360 redef class AClosureDecl
1361 redef readable var _position
: Int = 0
1363 redef fun variable
: ClosureVariable do return _variable
.as(not null)
1364 var _variable
: nullable ClosureVariable
1366 redef fun accept_property_verifier
(v
)
1368 var old_signature_builder
= v
.signature_builder
1369 v
.signature_builder
= new SignatureBuilder
1371 if v
.signature_builder
.has_error_occured
then
1374 var sig
= v
.signature_builder
.signature
1376 sig
= new MMSignature(new Array[MMParam], null, v
.local_class
.get_type
)
1378 if sig
.return_type
!= null and n_kwbreak
!= null then
1379 v
.error
(self, "Syntax Error: A break block cannot have a return value.")
1382 # Add the finalizer to the closure signature
1383 var finalize_sig
= new MMSignature(new Array[MMParam], null, v
.mmmodule
.type_any
) # FIXME should be no receiver
1384 var finalizer_clos
= new MMClosure(once
("break".to_symbol
), finalize_sig
, false, true)
1385 sig
.closures
.add
(finalizer_clos
)
1387 var name
= n_id
.to_symbol
1388 var clos
= new MMClosure(name
, sig
, n_kwbreak
!= null, n_expr
!= null)
1389 for c
in old_signature_builder
.closure_decls
do
1390 if c
.n_id
.to_symbol
== name
then
1391 v
.error
(n_id
, "A closure '!{name}' already defined at {c.n_id.location.relative_to(n_id.location)}.")
1395 v
.signature_builder
= old_signature_builder
1396 _position
= old_signature_builder
.closure_decls
.length
1397 old_signature_builder
.closure_decls
.add
(self)
1398 _variable
= new ClosureVariable(n_id
.to_symbol
, n_id
, clos
)
1403 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1404 private fun check_visibility
(v
: AbsSyntaxVisitor, p
: MMLocalProperty)
1406 if p
.global
.visibility_level
>= 3 then return
1407 var t
= get_stype
(v
)
1408 if t
== null then return
1409 var bc
= t
.local_class
1410 if bc
.global
.visibility_level
>= 3 then
1411 v
.error
(self, "Access error: Class {bc} is private and cannot be used in the signature of the non-private property {p}.")
1414 n
.check_visibility
(v
, p
)
1420 redef fun accept_class_builder
(v
) do end
1421 redef fun accept_property_builder
(v
) do end
1422 redef fun accept_property_verifier
(v
) do end