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
24 redef class ToolContext
25 redef fun handle_property_conflict
(lc
, impls
)
27 var location
: nullable Location = null
28 if lc
isa MMSrcLocalClass then
30 if node
!= null then node
.location
32 #if location == null then location = lc.mmmodule.location
33 var clas
= new Array[MMLocalClass]
35 clas
.add
(i
.local_class
)
37 self.fatal_error
(location
, "Property inheritance conflict in class {lc} for `{impls.first.name}': conflicting properties are defined in {clas.join(", ")}")
41 # Class specialization hierarchy sorter
42 private class CSHSorter
43 super AbstractSorter[MMLocalClass]
44 redef fun compare
(a
, b
)
46 return a
.cshe
.rank
<=> b
.cshe
.rank
52 redef class MMSrcModule
53 # Syntax analysis and MM construction for the module
54 # Require that supermodules are processed
55 fun do_mmbuilder
(tc
: ToolContext)
57 # Import global classes
60 # Create local classes and attach them to global classes
61 var mmbv
= new ClassBuilderVisitor(tc
, self)
62 mmbv
.enter_visit
(node
)
65 if mhe
.direct_greaters
.is_empty
then
66 process_default_classes
(tc
)
69 # Import unrefined local classes and attach them to global classes
72 # Resolve classes in super clauses
73 var mmbv1
= new ClassSpecializationBuilderVisitor(tc
, self)
74 mmbv1
.enter_visit
(node
)
77 # Compute specialization relation
78 for c
in local_classes
do
79 if visibility_for
(c
.global
.intro
.mmmodule
) < c
.global
.visibility_level
then
82 c
.compute_super_classes
86 # Class that we will process now are those in the hierarchy
87 # Its mean all the visible classes and their super-classes
88 # Note that leaves invisible classes are not in the 'classes' set
89 var classes
= class_specialization_hierarchy
.to_a
91 # Prepare class list to process the following in a right order
92 var sorter
= once
new CSHSorter
95 # Compute class ancestors types
96 var mmbv1b
= new ClassAncestorBuilder(tc
, self)
98 c
.accept_class_visitor
(mmbv1b
)
103 # Check class conformity
104 var mmbv1c
= new ClassVerifierVisitor(tc
, self)
106 c
.accept_class_visitor
(mmbv1c
)
110 # Property inhritance and introduction
111 var mmbv2
= new PropertyBuilderVisitor(tc
, self)
113 # Global property introduction and redefinition
114 c
.accept_class_visitor
(mmbv2
)
116 # Default and inherited constructor if needed
117 if c
isa MMSrcLocalClass and c
.global
.intro
== c
and not c
.global
.is_enum
and not c
.global
.is_interface
then
118 c
.process_default_constructors
(mmbv2
)
121 # Note that inherited unredefined property are processed on demand latter
125 # Property signature analysis and inheritance conformance
126 var mmbv3
= new PropertyVerifierVisitor(tc
, self)
128 c
.accept_properties_visitor
(mmbv3
)
134 # Create some primitive default classes if they do not exists
135 fun process_default_classes
(tc
: ToolContext)
137 var name
= once
("Object".to_symbol
)
138 if not has_global_class_named
(name
) then
139 var c
= new MMSrcLocalClass(self, name
, null, 0)
141 src_local_classes
[name
] = c
143 name
= once
("Bool".to_symbol
)
144 if not has_global_class_named
(name
) then
145 var c
= new MMSrcLocalClass(self, name
, null, 0)
147 src_local_classes
[name
] = c
152 redef class MMLocalClass
153 # Accept a class visitor (on class nodes)
154 private fun accept_class_visitor
(v
: AbsSyntaxVisitor)
158 # Accept a class visitor (on class properties)
159 private fun accept_properties_visitor
(v
: AbsSyntaxVisitor)
164 redef class MMSrcLocalClass
165 redef fun accept_class_visitor
(v
)
174 # Accept a class visitor (on class properties)
175 redef fun accept_properties_visitor
(v
)
183 for p
in src_local_properties
do
184 p
.accept_property_visitor
(v
)
188 # Introduce or inherit default constructors
189 private fun process_default_constructors
(v
: PropertyBuilderVisitor)
191 # Is there already a constructor ?
192 for gp
in global_properties
do
194 # Return if explicit constructor in the class
195 if gp
.intro
.local_class
== self then return
199 # Collect visible constructors in super stateful classes
200 var super_inits
= new ArraySet[MMLocalProperty]
201 var super_constructors
= new ArraySet[MMGlobalProperty]
202 for sc
in che
.direct_greaters
do
203 if sc
.global
.is_enum
or sc
.global
.is_interface
then continue
204 for gp
in sc
.global_properties
do
205 if not gp
.is_init
then continue
206 super_constructors
.add
(gp
)
208 var initname
= once
("init".to_symbol
)
209 if sc
.has_global_property_by_name
(initname
) then
210 var gp
= sc
.get_property_by_name
(initname
)
211 super_inits
.add
(self[gp
])
215 # Collect unassigned attributes
216 var unassigned_attributes
= new Array[MMSrcAttribute]
217 for a
in src_local_properties
do
218 if a
isa MMSrcAttribute then
220 if n
.n_expr
== null then unassigned_attributes
.add
(a
)
224 if not super_constructors
.is_empty
then
225 # Select most specific classes introducing inheritable constructors
226 # Mixin classes are skipped
227 var supers
= new Array[MMLocalClass]
228 for gp
in super_constructors
do
229 var sc
= gp
.local_class
230 if supers
.has
(sc
) then continue
231 if not sc
.global
.is_mixin
then
235 supers
= che
.order
.select_smallests
(supers
)
237 # A mixin class can only have 0 or 1 most specific non-mixin superclass
238 var superclass
: nullable MMLocalClass = null # This most specific non-mixin superclass (if any)
240 if supers
.length
> 1 then
241 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(", ")}.")
243 else if supers
.length
== 1 then
244 superclass
= supers
.first
247 for gp
in super_constructors
do
248 # Inherit constructors : the one of the non-mixin super class or all from the all mixin super-classes
249 if superclass
== null or gp
.local_class
== superclass
then
250 make_visible_an_inherited_global_property
(gp
)
253 global
.mixin_of
= superclass
.as(not null).global
# FIXME Dear! this should break!
255 # v.error(nodes.first, "Error, constructor required in {self} since no anonimous init found in {sc}.")
257 # unassigned attributes, then implicit consructors are generated
258 var p
= new MMImplicitInit(self, unassigned_attributes
, super_inits
.to_a
)
259 add_src_local_property
(v
, p
)
260 #print("Create implicit init {p} in {self} from {super_inits.join(", ")} + {unassigned_attributes.length} args")
264 # Add a source property
265 # Register it to the class and attach it to global property
266 private fun add_src_local_property
(v
: PropertyBuilderVisitor, prop
: MMLocalProperty)
268 var pname
= prop
.name
269 # Check double definition in the same class
270 if src_local_properties
.has_key
(pname
) then
271 v
.error
(prop
.node
, "Error: A property {pname} is already defined in class {name}.")
274 src_local_properties
[pname
] = prop
276 # Intro or redefinition ?
277 if has_global_property_by_name
(pname
) then
278 var g
= get_property_by_name
(pname
)
279 prop
.inherit_global
(g
)
282 if not prop
.is_global_set
then
284 prop
.global
.is_init
= prop
.is_init
289 redef class MMLocalProperty
290 private fun accept_property_visitor
(v
: AbsSyntaxVisitor)
295 redef class MMImplicitInit
296 redef readable var _super_init
: nullable MMLocalProperty = null
297 redef fun accept_property_visitor
(v
)
299 var base
: nullable MMLocalProperty = null
300 for p
in super_inits
do
301 if p
.signature
.arity
> 0 then
305 v
.error
(null, "Error: explicit constructor needed in {local_class} since both super-constructor {base.full_name} and {p.full_name} have paramters")
312 var params
= new Array[MMParam]
314 var sig
= base
.signature
315 for i
in [0..sig
.arity
[ do
316 params
.add
(sig
.params
[i
])
319 for a
in unassigned_attributes
do
320 var sig
= a
.signature
321 if sig
== null then return # Broken attribute definition
323 if name
.to_s
.first
== '_' or name
.to_s
.first
== '@' then
324 name
= a
.to_s
.substring_from
(1).to_symbol
326 params
.add
(new MMParam(sig
.return_type
.as(not null), name
))
328 signature
= new MMSignature(params
, null, local_class
.get_type
)
333 # Concrete NIT class specialization relation
336 redef readable var _local_class
: MMLocalClass
338 init(c
: MMLocalClass)
344 ###############################################################################
346 # A pass visitor for syntax analysis.
347 # * Build the classes and attach them to global classes
348 # * Collect generic formal parameters.
349 private class ClassBuilderVisitor
350 super AbsSyntaxVisitor
351 # Current class arity
352 readable writable var _local_class_arity
: Int = 0
354 # Current class formal parameters
355 readable writable var _formals
: nullable Map[Symbol, MMTypeFormalParameter]
357 redef fun visit
(n
) do n
.accept_class_builder
(self)
361 # Another pass visitor for syntax analysis.
362 # * Build ancertors (with only class informations not the type one)
363 private class ClassSpecializationBuilderVisitor
364 super AbsSyntaxVisitor
365 redef fun visit
(n
) do n
.accept_class_specialization_builder
(self)
369 # Another pass visitor for syntax analysis.
370 # * Compute types in ancestors
371 private class ClassAncestorBuilder
372 super AbsSyntaxVisitor
373 redef fun visit
(n
) do n
.accept_class_ancestor_builder
(self)
377 # Another pass visitor for syntax analysis.
378 # * Checks classes in regard to superclasses
379 private class ClassVerifierVisitor
380 super AbsSyntaxVisitor
381 redef fun visit
(n
) do n
.accept_class_verifier
(self)
386 # Another pass visitor for syntax analysis.
387 # * Build propertie names
388 # * Build local properties and attache them to global properties
389 # * Attach bound to formal types
390 private class PropertyBuilderVisitor
391 super AbsSyntaxVisitor
392 redef fun visit
(n
) do n
.accept_property_builder
(self)
396 # Another pass pass visitor for syntax analysis.
397 # * Check property conformance
398 private class PropertyVerifierVisitor
399 super AbsSyntaxVisitor
401 # The signature currently build
402 readable writable var _signature_builder
: SignatureBuilder
404 redef fun visit
(n
) do n
.accept_property_verifier
(self)
409 _signature_builder
= new SignatureBuilder
413 # Information about a signature currently build
414 private class SignatureBuilder
415 # Current visited parameter types
416 readable writable var _params
: Array[AParam] = new Array[AParam]
418 # Visited parameters without type information added
419 readable writable var _untyped_params
: Array[AParam] = new Array[AParam]
421 # Position of the current star parameter
422 readable writable var _vararg_rank
: Int = -1
424 # Current closure declarations
425 readable writable var _closure_decls
: Array[AClosureDecl] = new Array[AClosureDecl]
427 # True is a problen occured durring building
428 readable writable var _has_error_occured
: Bool = false
431 readable writable var _signature
: nullable MMSignature = null
434 ###############################################################################
437 private fun accept_class_builder
(v
: ClassBuilderVisitor) do accept_abs_syntax_visitor
(v
)
438 private fun accept_class_specialization_builder
(v
: ClassSpecializationBuilderVisitor) do accept_abs_syntax_visitor
(v
)
439 private fun accept_class_ancestor_builder
(v
: ClassAncestorBuilder) do accept_abs_syntax_visitor
(v
)
440 private fun accept_class_verifier
(v
: ClassVerifierVisitor) do accept_abs_syntax_visitor
(v
)
441 private fun accept_property_builder
(v
: PropertyBuilderVisitor) do accept_abs_syntax_visitor
(v
)
442 private fun accept_property_verifier
(v
: PropertyVerifierVisitor) do accept_abs_syntax_visitor
(v
)
446 # Import supermodules and compute visibility
447 fun import_super_modules
(tc
: ToolContext, mod
: MMSrcModule)
449 # Import super-modules
450 var supers
= new Array[MMModule]
451 var no_import
: nullable AImport = null
452 for i
in n_imports
do
453 var n
= i
.module_name
455 var m
= tc
.get_module
(n
, mod
)
457 mod
.add_super_module
(m
, i
.visibility_level
)
462 if no_import
!= null then
463 if not supers
.is_empty
then
464 tc
.error
(no_import
.location
, "Error: Top modules cannot import other modules.")
466 else if supers
.is_empty
then
467 var stdname
= once
"standard".to_symbol
468 var m
= tc
.get_module
(stdname
, mod
)
470 mod
.add_super_module
(m
, 1)
473 tc
.add_module
(mod
, supers
)
477 redef class AModuledecl
478 redef fun accept_class_builder
(v
)
480 if n_name
.n_id
.to_symbol
!= v
.mmmodule
.name
then
481 v
.error
(n_name
.n_id
, "Error: Module name mismatch between {v.mmmodule.name} and {n_name.n_id.to_symbol}")
487 # Imported module name (or null)
488 fun module_name
: nullable Symbol is abstract
490 # Visibility level (intrude/public/private)
491 fun visibility_level
: Int is abstract
493 redef class AStdImport
494 redef fun module_name
496 return n_name
.n_id
.to_symbol
498 redef fun visibility_level
500 return n_visibility
.level
503 redef class ANoImport
504 redef fun module_name
510 redef class AVisibility
512 fun level
: Int is abstract
514 redef class APublicVisibility
515 redef fun level
do return 1
517 redef class AProtectedVisibility
518 redef fun level
do return 2
520 redef class APrivateVisibility
521 redef fun level
do return 3
523 redef class AIntrudeVisibility
524 redef fun level
do return 0
528 redef class AClassdef
529 redef fun local_class
: MMSrcLocalClass do return _local_class
.as(not null)
530 var _local_class
: nullable MMSrcLocalClass
533 fun name
: Symbol is abstract
535 # Number of formal parameters
536 fun arity
: Int do return 0
538 # Visibility of the class
539 fun visibility_level
: Int do return 1
541 redef fun accept_class_builder
(v
)
543 var local_class
: MMSrcLocalClass
545 var local_classes
= mod
.src_local_classes
546 if (local_classes
.has_key
(name
)) then
547 local_class
= local_classes
[name
]
548 _local_class
= local_class
549 if self isa AStdClassdef then
550 # If we are not a special implicit class then rant
551 v
.error
(self, "Error: A class {name} is already defined at line {local_class.node.location.line_start}.")
554 # Add the new node after the last node
555 var n
= local_class
.node
556 while n
.next_node
!= null do n
= n
.next_node
559 local_class
= new MMSrcLocalClass(mod
, name
, self, arity
)
560 _local_class
= local_class
561 local_classes
[name
] = local_class
562 if not mod
.has_global_class_named
(name
) then
563 build_class_introduction
(v
)
565 var glob
= mod
.global_class_named
(name
)
566 build_class_refinement
(v
, glob
)
570 v
.local_class_arity
= 0
571 v
.formals
= local_class
.formal_dict
580 fun build_class_introduction
(v
: AbsSyntaxVisitor)
582 local_class
.new_global
583 var glob
= local_class
.global
585 glob
.visibility_level
= visibility_level
586 if self isa AStdClassdef then
587 if n_kwredef
!= null then
588 v
.error
(self, "Redef error: No class {name} is imported. Remove the redef keyword to define a new class.")
591 glob
.is_interface
= n_classkind
.is_interface
592 glob
.is_abstract
= n_classkind
.is_abstract
593 glob
.is_enum
= n_classkind
.is_enum
597 fun build_class_refinement
(v
: AbsSyntaxVisitor, glob
: MMGlobalClass)
599 local_class
.set_global
(glob
)
601 glob
.check_visibility
(v
, self, v
.mmmodule
)
602 if self isa AStdClassdef and n_kwredef
== null then
603 v
.error
(self, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
607 if glob
.intro
.arity
!= _local_class
.arity
then
608 v
.error
(self, "Redef error: Formal parameter arity missmatch; got {_local_class.arity}, expected {glob.intro.arity}.")
611 if self isa AStdClassdef and (not glob
.is_interface
and n_classkind
.is_interface
or
612 not glob
.is_abstract
and n_classkind
.is_abstract
or
613 not glob
.is_enum
and n_classkind
.is_enum
)
615 v
.error
(self, "Redef error: cannot change kind of class {name}.")
619 redef fun accept_class_verifier
(v
)
622 var glob
= _local_class
.global
623 for c
in _local_class
.cshe
.direct_greaters
do
625 if glob
.is_interface
then
627 v
.error
(self, "Special error: Interface {name} try to specialise enum class {c.name}.")
628 else if not cg
.is_interface
then
629 v
.error
(self, "Special error: Interface {name} try to specialise class {c.name}.")
631 else if glob
.is_enum
then
632 if not cg
.is_interface
and not cg
.is_enum
then
633 v
.error
(self, "Special error: Enum class {name} try to specialise class {c.name}.")
637 v
.error
(self, "Special error: Class {name} try to specialise enum class {c.name}.")
644 redef fun accept_abs_syntax_visitor
(v
)
646 v
.local_class
= _local_class
652 redef class AClasskind
653 fun is_interface
: Bool do return false
654 fun is_enum
: Bool do return false
655 fun is_abstract
: Bool do return false
658 redef class AInterfaceClasskind
659 redef fun is_interface
do return true
661 redef class AEnumClasskind
662 redef fun is_enum
do return true
664 redef class AAbstractClasskind
665 redef fun is_abstract
do return true
668 redef class AStdClassdef
671 return n_id
.to_symbol
675 return n_formaldefs
.length
677 redef fun accept_class_verifier
(v
)
680 var glob
= _local_class
.global
681 if glob
.intro
== _local_class
then
683 glob
.visibility_level
= visibility_level
684 glob
.is_interface
= n_classkind
.is_interface
685 glob
.is_abstract
= n_classkind
.is_abstract
686 glob
.is_enum
= n_classkind
.is_enum
687 if n_kwredef
!= null then
688 v
.error
(self, "Redef error: No class {name} is imported. Remove the redef keyword to define a new class.")
691 for c
in _local_class
.cshe
.direct_greaters
do
693 if glob
.is_interface
then
695 v
.error
(self, "Special error: Interface {name} try to specialise enum class {c.name}.")
696 else if not cg
.is_interface
then
697 v
.error
(self, "Special error: Interface {name} try to specialise class {c.name}.")
699 else if glob
.is_enum
then
700 if not cg
.is_interface
and not cg
.is_enum
then
701 v
.error
(self, "Special error: Enum class {name} try to specialise class {c.name}.")
705 v
.error
(self, "Special error: Class {name} try to specialise enum class {c.name}.")
715 glob
.check_visibility
(v
, self, v
.mmmodule
)
716 if n_kwredef
== null then
717 v
.error
(self, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
721 if glob
.intro
.arity
!= _local_class
.arity
then
722 v
.error
(self, "Redef error: Formal parameter arity mismatch; got {_local_class.arity}, expected {glob.intro.arity}.")
726 not glob
.is_interface
and n_classkind
.is_interface
or
727 not glob
.is_abstract
and n_classkind
.is_abstract
or
728 not glob
.is_enum
and n_classkind
.is_enum
730 v
.error
(self, "Redef error: cannot change kind of class {name}.")
734 redef fun visibility_level
736 return n_visibility
.level
740 redef class AMainClassdef
743 return once
"Sys".to_symbol
747 redef class ATopClassdef
750 return once
"Object".to_symbol
754 redef class AFormaldef
755 # The associated formal generic parameter (MM entity)
756 var _formal
: nullable MMTypeFormalParameter
758 redef fun accept_class_builder
(v
)
760 var name
= n_id
.to_symbol
761 var formal_type
= new MMTypeFormalParameter(name
, v
.local_class_arity
, v
.local_class
)
762 _formal
= formal_type
763 v
.local_class_arity
= v
.local_class_arity
+ 1
764 v
.local_class
.register_formal
(formal_type
)
765 v
.formals
[name
] = formal_type
769 redef fun accept_class_verifier
(v
)
772 var c
= v
.local_class
773 var o
= c
.global
.intro
775 if n_type
== null then
776 _formal
.bound
= v
.mmmodule
.type_any
.as_nullable
778 var stype
= n_type
.get_stype
(v
)
779 if stype
== null then return
780 _formal
.bound
= stype
783 var ob
= o
.get_formal
(_formal
.position
).bound
.for_module
(v
.mmmodule
)
784 if n_type
== null then
787 var stype
= n_type
.get_stype
(v
)
788 if stype
== null then return
789 _formal
.bound
= stype
790 if _formal
.bound
!= ob
then
791 v
.error
(self, "Redef error: Cannot change formal parameter type of class {c}; got {_formal.bound}, expected {ob}.")
798 redef class ASuperclass
799 readable var _ancestor
: nullable MMSrcAncestor
801 redef fun accept_class_specialization_builder
(v
)
804 var c
= n_type
.get_local_class
(v
)
805 if c
== null then return
806 var ancestor
= new MMSrcAncestor(c
)
808 v
.local_class
.add_direct_parent
(ancestor
)
811 redef fun accept_class_ancestor_builder
(v
)
814 _ancestor
.stype
= n_type
.get_unchecked_stype
(v
)
815 _ancestor
.inheriter
= v
.local_class
.get_type
818 redef fun accept_class_verifier
(v
)
821 n_type
.check_conform
(v
)
826 # Process and check properties of the property.
827 # * Distinguish inits and methods
828 # * Inherit or check visibility.
829 # * Check redef errors.
830 # * Check forbiden attribute definitions.
831 # * Check signature conformance.
832 private fun process_and_check
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
834 if prop
.global
.intro
== prop
then
835 do_and_check_intro
(v
, prop
, has_redef
, visibility_level
)
837 do_and_check_redef
(v
, prop
, has_redef
, visibility_level
)
841 # The part of process_and_check when prop is an introduction
842 private fun do_and_check_intro
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
844 var glob
= prop
.global
845 var gbc
= prop
.local_class
.global
846 if v
.local_class
.global
.visibility_level
>= 3 then
847 # Method of private classes are private
850 glob
.visibility_level
= visibility_level
852 v
.error
(self, "Error: No property {prop.local_class}::{prop} is inherited. Remove the redef keyword to define a new property.")
854 if glob
.is_attribute
then
855 if gbc
.is_interface
then
856 v
.error
(self, "Error: Attempt to define attribute {prop} in the interface {prop.local_class}.")
857 else if gbc
.is_enum
then
858 v
.error
(self, "Error: Attempt to define attribute {prop} in the enum class {prop.local_class}.")
860 else if glob
.is_init
then
861 if gbc
.is_interface
then
862 v
.error
(self, "Error: Attempt to define a constructor {prop} in the class {prop.local_class}.")
863 else if gbc
.is_enum
then
864 v
.error
(self, "Error: Attempt to define a constructor {prop} in the enum {prop.local_class}.")
867 if prop
.signature
== null then
869 var supers
= prop
.local_class
.super_methods_named
(prop
.name
)
870 inherit_signature
(v
, prop
, supers
)
872 if prop
.signature
!= null or v
.signature_builder
.has_error_occured
then
874 else if not v
.signature_builder
.untyped_params
.is_empty
then
875 v
.error
(v
.signature_builder
.untyped_params
.first
, "Error: Untyped parameter.")
877 prop
.signature
= new MMSignature(new Array[MMParam], null, v
.local_class
.get_type
)
878 for clos
in v
.signature_builder
.closure_decls
do
879 prop
.signature
.closures
.add
(clos
.variable
.closure
)
885 private fun inherit_signature
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, supers
: Array[MMLocalProperty])
887 var s
= prop
.signature
889 var isig
= ip
.signature
.adaptation_to
(v
.local_class
.get_type
)
892 if v
.signature_builder
.params
.length
!= isig
.arity
then
895 for p
in v
.signature_builder
.params
do
896 var t
= isig
[p
.position
]
898 if p
.position
== isig
.vararg_rank
then
910 # The part of process_and_check when prop is a redefinition
911 private fun do_and_check_redef
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
913 var is_init
= self isa AConcreteInitPropdef
914 var glob
= prop
.global
916 if not has_redef
then
917 v
.error
(self, "Redef error: {prop.local_class}::{prop} is an inherited property. To redefine it, add the redef keyword.")
920 if glob
.is_init
and not is_init
then
921 v
.error
(self, "Redef error: A method {prop.local_class}::{prop} cannot redefine a constructor.")
922 else if not glob
.is_init
and is_init
then
923 v
.error
(self, "Redef error: A constructor {prop.local_class}::{prop} cannot redefine a method.")
926 var s
= prop
.signature
927 #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}"
928 for i
in prop
.prhe
.direct_greaters
do
929 var ip
= i
.local_class
[prop
.global
]
930 var isig
= i
.signature
931 if isig
== null then break # previous signature is invalid
932 isig
= isig
.adaptation_to
(v
.local_class
.get_type
)
935 #print "{prop.full_name} inherits signature from {ip.full_name}"
936 if v
.signature_builder
.params
.length
!= isig
.arity
then
937 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
940 if v
.signature_builder
.closure_decls
.length
!= isig
.closures
.length
then
941 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} closure(s).")
944 for p
in v
.signature_builder
.params
do
945 var t
= isig
[p
.position
]
947 if p
.position
== isig
.vararg_rank
then
957 var nberr
= v
.tc
.error_count
958 #print "Check {prop.local_class}::{prop}{s} vs {ip.local_class}::{ip}{isig}"
959 #print "s={s.object_id} isig={isig.object_id} isigorig={i.signature.object_id}"
961 #print "orig signature: {i.signature.recv} . {i.signature}"
962 #print "inh signature: {isig.recv} . {isig}"
963 #print "redef signature: {s.recv} . {s}"
965 if s
.arity
!= isig
.arity
then
966 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
967 else if s
.arity
> 0 then
968 if self isa AMethPropdef then
970 for j
in [0..s
.arity
[ do
971 if s
[j
] != isig
[j
] then
972 v
.error
(n_signature
.n_params
[j
], "Redef error: Expected {isig[j]}, as in {ip.local_class}::{ip}.")
975 else if self isa AAttrPropdef then
977 if s
[0] != isig
[0] then
978 v
.error
(n_type
, "Redef error: Expected {isig[0]}, as in the parameter of {ip.local_class}::{ip}.")
986 var srt
= s
.return_type
987 var isrt
= isig
.return_type
988 if srt
== null and isrt
!= null then
989 v
.error
(self, "Redef error: The procedure {prop.local_class}::{prop} redefines the function {ip.local_class}::{ip}.")
990 else if srt
!= null and isrt
== null then
991 v
.error
(self, "Redef error: The function {prop.local_class}::{prop} redefines the procedure {ip.local_class}::{ip}.")
992 else if srt
!= null and isrt
!= null and not srt
< isrt
then
993 var n
: nullable ANode = null
994 if self isa AMethPropdef then
995 n
= self.n_signature
.n_type
996 else if self isa AAttrPropdef then
998 else if self isa ATypePropdef then
1001 v
.error
(n
, "Redef error: Expected {isrt}, as in {ip.local_class}::{ip}.")
1002 else if not s
< isig
and nberr
== v
.tc
.error_count
then
1003 # Systematic fallback for conformance check
1004 v
.error
(self, "Redef error: Incompatible redefinition of {ip.local_class}::{ip} with {prop.local_class}::{prop}")
1005 else if srt
!= null and isrt
!= null and srt
!= isrt
and prop
isa MMAttribute then
1007 v
.warning
(self, "Redef warning: Expected {isrt}, as in {ip.local_class}::{ip}.")
1011 if visibility_level
!= 1 and glob
.visibility_level
!= visibility_level
then
1012 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefinition cannot change visibility.")
1014 glob
.check_visibility
(v
, self, v
.mmmodule
, true)
1018 redef class AAttrPropdef
1019 redef readable var _readmethod
: nullable MMSrcMethod
1020 redef readable var _writemethod
: nullable MMSrcMethod
1021 var _prop
: nullable MMSrcAttribute
1022 redef fun prop
do return _prop
.as(not null)
1024 redef fun accept_property_builder
(v
)
1028 if n_id
!= null then
1029 name
= n_id
.to_symbol
1031 name
= ("@" + n_id2
.text
).to_symbol
1033 var lc
= v
.local_class
1034 var prop
= new MMSrcAttribute(name
, lc
, self)
1036 v
.local_class
.add_src_local_property
(v
, prop
)
1038 if n_readable
!= null or n_id
== null then
1039 if n_id
!= null then
1040 name
= n_id
.text
.substring_from
(1).to_symbol
1042 name
= n_id2
.to_symbol
1044 var readmethod
= new MMReadImplementationMethod(name
, lc
, self)
1045 _readmethod
= readmethod
1046 v
.local_class
.add_src_local_property
(v
, readmethod
)
1048 if n_writable
!= null or n_id
== null then
1049 if n_id
!= null then
1050 name
= (n_id
.text
.substring_from
(1) + "=").to_symbol
1052 name
= (n_id2
.text
+ "=").to_symbol
1054 var writemethod
= new MMWriteImplementationMethod(name
, lc
, self)
1055 _writemethod
= writemethod
1056 v
.local_class
.add_src_local_property
(v
, writemethod
)
1060 redef fun accept_property_verifier
(v
)
1064 if n_type
!= null then
1065 var t0
= n_type
.get_stype
(v
)
1066 if t0
!= null then t
= t0
else return
1068 v
.error
(self, "Not yet implemented: Attribute definition {prop.local_class}::{prop} requires an explicit type.")
1073 var signature
= new MMSignature(new Array[MMParam], t
, v
.local_class
.get_type
)
1074 prop
.signature
= signature
1075 var visibility_level
= n_visibility
.level
1076 process_and_check
(v
, prop
, n_id
!= null and n_kwredef
!= null, visibility_level
)
1077 if n_readable
!= null or n_id
== null then
1078 var m
= _readmethod
.as(not null)
1079 m
.signature
= signature
1080 process_and_check
(v
, m
, (n_readable
!= null and n_readable
.n_kwredef
!= null) or (n_id
== null and n_kwredef
!= null), visibility_level
)
1081 n_type
.check_visibility
(v
, m
)
1083 if n_writable
!= null or n_id
== null then
1084 var m
= _writemethod
.as(not null)
1085 m
.signature
= new MMSignature(new Array[MMParam].with_items
(new MMParam(t
, once
"value".to_symbol
)), null, v
.local_class
.get_type
)
1086 var vl
= visibility_level
1087 if n_id
== null then
1088 if n_writable
== null then vl
= 3 else vl
= n_writable
.n_visibility
.level
# write accessor has a specific visibility
1090 process_and_check
(v
, m
, n_writable
!= null and n_writable
.n_kwredef
!= null, vl
)
1091 n_type
.check_visibility
(v
, m
)
1095 redef fun accept_abs_syntax_visitor
(v
)
1097 v
.local_property
= _prop
1099 v
.local_property
= null
1103 redef class AMethPropdef
1104 # Name of the method
1105 readable var _name
: nullable Symbol
1107 var _method
: nullable MMMethSrcMethod
1108 redef fun method
do return _method
.as(not null)
1110 redef fun accept_property_builder
(v
)
1114 if n_methid
== null then
1115 if self isa AConcreteInitPropdef then
1116 name
= once
"init".to_symbol
1118 name
= once
"main".to_symbol
1121 name
= n_methid
.name
.as(not null)
1122 # FIXME: Add the 'unary' keyword
1123 if n_methid
.name
== (once
"-".to_symbol
) then
1124 var ns
= n_signature
1125 if ns
!= null and ns
.n_params
.length
== 0 then
1126 name
= once
"unary -".to_symbol
1131 var prop
= new MMMethSrcMethod(name
, v
.local_class
, self)
1133 v
.local_class
.add_src_local_property
(v
, prop
)
1136 redef fun accept_property_verifier
(v
)
1138 v
.signature_builder
= new SignatureBuilder
1141 if v
.signature_builder
.has_error_occured
then return
1143 if v
.signature_builder
.signature
== null then
1144 #_method.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
1146 method
.signature
= v
.signature_builder
.signature
.as(not null)
1148 var visibility_level
= 1
1149 if n_visibility
!= null and n_visibility
.level
> 1 then
1150 visibility_level
= n_visibility
.level
1152 process_and_check
(v
, method
, n_kwredef
!= null, visibility_level
)
1153 if n_signature
!= null then n_signature
.check_visibility
(v
, method
)
1156 redef fun accept_abs_syntax_visitor
(v
)
1158 v
.local_property
= _method
1160 v
.local_property
= null
1164 redef class AMainMethPropdef
1165 redef fun process_and_check
(v
, prop
, has_redef
, visibility_level
)
1167 prop
.global
.visibility_level
= visibility_level
1168 prop
.signature
= new MMSignature(new Array[MMParam], null, v
.local_class
.get_type
)
1169 # Disable all checks for main
1173 redef class AExternMethPropdef
1174 redef fun accept_property_verifier
(v
)
1176 super # Compute signature
1178 if n_extern
!= null then
1179 ename
= n_extern
.text
1180 ename
= ename
.substring
(1, ename
.length-2
)
1182 ename
= "{method.mmmodule.name}_{method.local_class.name}_{method.local_class.name}_{method.name}_{method.signature.arity}"
1184 method
.extern_name
= ename
1188 redef class ATypePropdef
1189 redef fun prop
do return _prop
.as(not null)
1190 var _prop
: nullable MMSrcTypeProperty
1192 redef fun accept_property_builder
(v
)
1195 var name
= n_id
.to_symbol
1196 var prop
= new MMSrcTypeProperty(name
, v
.local_class
, self)
1198 v
.local_class
.add_src_local_property
(v
, prop
)
1201 redef fun accept_property_verifier
(v
)
1204 var signature
= new MMSignature(new Array[MMParam], n_type
.get_stype
(v
), v
.local_class
.get_type
)
1205 prop
.signature
= signature
1206 var visibility_level
= n_visibility
.level
1207 process_and_check
(v
, prop
, n_kwredef
!= null, visibility_level
)
1210 redef fun accept_abs_syntax_visitor
(v
)
1212 v
.local_property
= _prop
1214 v
.local_property
= null
1218 # Visitor used to build a full method name from multiple tokens
1219 private class MethidAccumulator
1221 readable var _name
: Buffer = new Buffer
1225 _name
.append
(n
.text
)
1233 redef readable var _name
: nullable Symbol
1235 redef fun accept_property_builder
(v
)
1237 var accumulator
= new MethidAccumulator
1238 accumulator
.enter_visit
(self)
1239 _name
= accumulator
.name
.to_s
.to_symbol
1244 redef class ASignature
1245 redef fun accept_property_verifier
(v
)
1248 if v
.signature_builder
.has_error_occured
then
1250 else if not v
.signature_builder
.untyped_params
.is_empty
then
1251 if v
.signature_builder
.untyped_params
.first
!= v
.signature_builder
.params
.first
or n_type
!= null then
1252 v
.error
(v
.signature_builder
.untyped_params
.first
, "Syntax error: untyped parameter.")
1255 else if not v
.signature_builder
.params
.is_empty
or n_type
!= null then
1256 var pars
= new Array[MMParam]
1257 for p
in v
.signature_builder
.params
do
1258 pars
.add
( new MMParam( p
.stype
.as(not null), p
.n_id
.to_symbol
) )
1260 var ret
: nullable MMType = null
1261 if n_type
!= null then
1262 ret
= n_type
.get_stype
(v
)
1264 v
.signature_builder
.has_error_occured
= true
1268 v
.signature_builder
.signature
= new MMSignature(pars
, ret
, v
.local_class
.get_type
)
1269 if v
.signature_builder
.vararg_rank
>= 0 then
1270 v
.signature_builder
.signature
.vararg_rank
= v
.signature_builder
.vararg_rank
1272 for clos
in v
.signature_builder
.closure_decls
do
1273 v
.signature_builder
.signature
.closures
.add
(clos
.variable
.closure
)
1278 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1279 fun check_visibility
(v
: AbsSyntaxVisitor, p
: MMLocalProperty)
1281 if p
.global
.visibility_level
>= 3 then return
1282 for n
in n_params
do
1283 if n
.n_type
!= null then n
.n_type
.check_visibility
(v
, p
)
1285 if n_type
!= null then n_type
.check_visibility
(v
, p
)
1290 redef readable var _position
: Int = 0
1292 redef fun variable
: ParamVariable do return _variable
.as(not null)
1293 var _variable
: nullable ParamVariable
1295 # The type of the parameter in signature
1296 readable writable var _stype
: nullable MMType
1298 redef fun accept_property_verifier
(v
)
1301 _position
= v
.signature_builder
.params
.length
1302 _variable
= new ParamVariable(n_id
.to_symbol
, n_id
)
1303 v
.signature_builder
.params
.add
(self)
1304 v
.signature_builder
.untyped_params
.add
(self)
1305 if n_type
!= null then
1306 var stype
= n_type
.get_stype
(v
)
1307 if stype
== null then
1308 v
.signature_builder
.has_error_occured
= true
1311 for p
in v
.signature_builder
.untyped_params
do
1314 if v
.signature_builder
.vararg_rank
== -1 then
1315 v
.signature_builder
.vararg_rank
= p
.position
1317 v
.error
(self, "Error: A vararg parameter is already defined.")
1319 stype
= v
.type_array
(stype
)
1321 p
.variable
.stype
= stype
1323 v
.signature_builder
.untyped_params
.clear
1327 fun is_vararg
: Bool do return n_dotdotdot
!= null
1330 redef class AClosureDecl
1331 redef readable var _position
: Int = 0
1333 redef fun variable
: ClosureVariable do return _variable
.as(not null)
1334 var _variable
: nullable ClosureVariable
1336 redef fun accept_property_verifier
(v
)
1338 var old_signature_builder
= v
.signature_builder
1339 v
.signature_builder
= new SignatureBuilder
1341 if v
.signature_builder
.has_error_occured
then
1344 var sig
= v
.signature_builder
.signature
1346 sig
= new MMSignature(new Array[MMParam], null, v
.local_class
.get_type
)
1348 if sig
.return_type
!= null and n_kwbreak
!= null then
1349 v
.error
(self, "Syntax Error: A break block cannot have a return value.")
1352 # Add the finalizer to the closure signature
1353 var finalize_sig
= new MMSignature(new Array[MMParam], null, v
.mmmodule
.type_any
) # FIXME should be no receiver
1354 var finalizer_clos
= new MMClosure(once
("break".to_symbol
), finalize_sig
, false, true)
1355 sig
.closures
.add
(finalizer_clos
)
1357 var name
= n_id
.to_symbol
1358 var clos
= new MMClosure(name
, sig
, n_kwbreak
!= null, n_expr
!= null)
1359 for c
in old_signature_builder
.closure_decls
do
1360 if c
.n_id
.to_symbol
== name
then
1361 v
.error
(n_id
, "A closure '!{name}' already defined at {c.n_id.location.relative_to(n_id.location)}.")
1365 v
.signature_builder
= old_signature_builder
1366 _position
= old_signature_builder
.closure_decls
.length
1367 old_signature_builder
.closure_decls
.add
(self)
1368 _variable
= new ClosureVariable(n_id
.to_symbol
, n_id
, clos
)
1373 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1374 private fun check_visibility
(v
: AbsSyntaxVisitor, p
: MMLocalProperty)
1376 if p
.global
.visibility_level
>= 3 then return
1377 var t
= get_stype
(v
)
1378 if t
== null then return
1379 var bc
= t
.local_class
1380 if bc
.global
.visibility_level
>= 3 then
1381 v
.error
(self, "Access error: Class {bc} is private and cannot be used in the signature of the non-private property {p}.")
1384 n
.check_visibility
(v
, p
)
1390 redef fun accept_class_builder
(v
) do end
1391 redef fun accept_property_builder
(v
) do end
1392 redef fun accept_property_verifier
(v
) do end