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 # Class specialization hierarchy sorter
25 private class CSHSorter
26 special AbstractSorter[MMLocalClass]
27 redef fun compare
(a
, b
)
29 return a
.cshe
.rank
<=> b
.cshe
.rank
35 redef class MMSrcModule
36 # Syntax analysis and MM construction for the module
37 # Require that supermodules are processed
38 fun do_mmbuilder
(tc
: ToolContext)
40 # Import global classes
43 # Create local classes and attach them to global classes
44 var mmbv
= new ClassBuilderVisitor(tc
, self)
45 mmbv
.enter_visit
(node
)
48 if mhe
.direct_greaters
.is_empty
then
49 process_default_classes
(tc
)
52 # Import unrefined local classes and attach them to global classes
55 # Resolve classes in super clauses
56 var mmbv1
= new ClassSpecializationBuilderVisitor(tc
, self)
57 mmbv1
.enter_visit
(node
)
60 # Compute specialization relation
61 for c
in local_classes
do
62 if visibility_for
(c
.global
.intro
.module) < c
.global
.visibility_level
then
65 c
.compute_super_classes
69 # Class that we will process now are those in the hierarchy
70 # Its mean all the visible classes and their super-classes
71 # Note that leaves invisible classes are not in the 'classes' set
72 var classes
= class_specialization_hierarchy
.to_a
74 # Prepare class list to process the following in a right order
75 var sorter
= once
new CSHSorter
78 # Compute class ancestors types
79 var mmbv1b
= new ClassAncestorBuilder(tc
, self)
81 c
.accept_class_visitor
(mmbv1b
)
86 # Check class conformity
87 var mmbv1c
= new ClassVerifierVisitor(tc
, self)
89 c
.accept_class_visitor
(mmbv1c
)
93 # Property inhritance and introduction
94 var mmbv2
= new PropertyBuilderVisitor(tc
, self)
96 # Inherit global properties
97 c
.inherit_global_properties
99 # Global property introduction and redefinition
100 c
.accept_class_visitor
(mmbv2
)
102 # Default and inherited constructor if needed
103 if c
isa MMSrcLocalClass and c
.global
.intro
== c
and not c
.global
.is_universal
and not c
.global
.is_interface
then
104 c
.process_default_constructors
(mmbv2
)
107 # Note that inherited unredefined property are processed on demand latter
111 # Property signature analysis and inheritance conformance
112 var mmbv3
= new PropertyVerifierVisitor(tc
, self)
114 c
.accept_properties_visitor
(mmbv3
)
117 # Check inherited local properties
119 for g
in c
.global_properties
do
120 if visibility_for
(g
.intro
.module) < g
.visibility_level
then continue
128 # Create some primitive default classes if they do not exists
129 fun process_default_classes
(tc
: ToolContext)
131 var name
= once
("Object".to_symbol
)
132 if not has_global_class_named
(name
) then
133 var c
= new MMSrcLocalClass(self, name
, null, 0)
135 src_local_classes
[name
] = c
137 name
= once
("Bool".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
146 redef class MMLocalClass
147 # Accept a class visitor (on class nodes)
148 private fun accept_class_visitor
(v
: AbsSyntaxVisitor)
152 # Accept a class visitor (on class properties)
153 private fun accept_properties_visitor
(v
: AbsSyntaxVisitor)
158 redef class MMSrcLocalClass
159 redef fun accept_class_visitor
(v
)
168 # Accept a class visitor (on class properties)
169 redef fun accept_properties_visitor
(v
)
177 for p
in src_local_properties
do
178 p
.accept_property_visitor
(v
)
182 # Introduce or inherit default constructors
183 private fun process_default_constructors
(v
: PropertyBuilderVisitor)
185 # Is there already a constructor ?
186 for gp
in global_properties
do
188 # Return if explicit constructor in the class
189 if gp
.intro
.local_class
== self then return
193 # Collect visible constructors in super stateful classes
194 var super_inits
= new ArraySet[MMLocalProperty]
195 var super_constructors
= new ArraySet[MMGlobalProperty]
196 for sc
in che
.direct_greaters
do
197 if sc
.global
.is_universal
or sc
.global
.is_interface
then continue
198 for gp
in sc
.global_properties
do
199 if not gp
.is_init
then continue
200 super_constructors
.add
(gp
)
202 var initname
= once
("init".to_symbol
)
203 if sc
.has_global_property_by_name
(initname
) then
204 var gp
= sc
.get_property_by_name
(initname
)
205 super_inits
.add
(self[gp
])
209 # Collect unassigned attributes
210 var unassigned_attributes
= new Array[MMSrcAttribute]
211 for a
in src_local_properties
do
212 if a
isa MMSrcAttribute then
214 if n
.n_expr
== null then unassigned_attributes
.add
(a
)
218 if not super_constructors
.is_empty
then
219 # Select most specific classes introducing inheritable constructors
220 # Mixin classes are skipped
221 var supers
= new Array[MMLocalClass]
222 for gp
in super_constructors
do
223 var sc
= gp
.local_class
224 if supers
.has
(sc
) then continue
225 if not sc
.global
.is_mixin
then
229 supers
= che
.order
.select_smallests
(supers
)
231 # A mixin class can only have 0 or 1 most specific non-mixin superclass
232 var superclass
: nullable MMLocalClass = null # This most specific non-mixin superclass (if any)
234 if supers
.length
> 1 then
235 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(", ")}.")
237 else if supers
.length
== 1 then
238 superclass
= supers
.first
241 for gp
in super_constructors
do
242 # Inherit constructors : the one of the non-mixin super class or all from the all mixin super-classes
243 if superclass
== null or gp
.local_class
== superclass
then
244 make_visible_an_inherited_global_property
(gp
)
247 global
.mixin_of
= superclass
.as(not null).global
# FIXME Dear! this should break!
249 # v.error(nodes.first, "Error, constructor required in {self} since no anonimous init found in {sc}.")
251 # unassigned attributes, then implicit consructors are generated
252 var p
= new MMImplicitInit(self, unassigned_attributes
, super_inits
.to_a
)
253 add_src_local_property
(v
, p
)
254 #print("Create implicit init {p} in {self} from {super_inits.join(", ")} + {unassigned_attributes.length} args")
258 # Add a source property
259 # Register it to the class and attach it to global property
260 private fun add_src_local_property
(v
: PropertyBuilderVisitor, prop
: MMLocalProperty)
262 var pname
= prop
.name
263 # Check double definition in the same class
264 if src_local_properties
.has_key
(pname
) then
265 v
.error
(prop
.node
, "Error: A property {pname} is already defined in class {name}.")
268 src_local_properties
[pname
] = prop
270 # Intro or redefinition ?
271 if has_global_property_by_name
(pname
) then
272 var g
= get_property_by_name
(pname
)
273 prop
.inherit_global
(g
)
276 if not prop
.is_global_set
then
278 prop
.global
.is_init
= prop
.is_init
283 redef class MMLocalProperty
284 private fun accept_property_visitor
(v
: AbsSyntaxVisitor)
289 redef class MMImplicitInit
290 redef readable var _super_init
: nullable MMLocalProperty = null
291 redef fun accept_property_visitor
(v
)
293 var base
: nullable MMLocalProperty = null
294 for p
in super_inits
do
295 if p
.signature
.arity
> 0 then
299 v
.error
(null, "Error: explicit constructor needed in {local_class} since both super-constructor {base.full_name} and {p.full_name} have paramters")
306 var params
= new Array[MMType]
308 var sig
= base
.signature
309 for i
in [0..sig
.arity
[ do
313 for a
in unassigned_attributes
do
314 var sig
= a
.signature
315 if sig
== null then return # Broken attribute definition
316 params
.add
(sig
.return_type
.as(not null))
318 signature
= new MMSignature(params
, null, local_class
.get_type
)
323 # Concrete NIT class specialization relation
326 redef readable var _local_class
: MMLocalClass
328 init(c
: MMLocalClass)
334 ###############################################################################
336 # A pass visitor for syntax analysis.
337 # * Build the classes and attach them to global classes
338 # * Collect generic formal parameters.
339 private class ClassBuilderVisitor
340 special AbsSyntaxVisitor
341 # Current class arity
342 readable writable var _local_class_arity
: Int = 0
344 # Current class formal parameters
345 readable writable var _formals
: nullable Map[Symbol, MMTypeFormalParameter]
347 redef fun visit
(n
) do n
.accept_class_builder
(self)
351 # Another pass visitor for syntax analysis.
352 # * Build ancertors (with only class informations not the type one)
353 private class ClassSpecializationBuilderVisitor
354 special AbsSyntaxVisitor
355 redef fun visit
(n
) do n
.accept_class_specialization_builder
(self)
359 # Another pass visitor for syntax analysis.
360 # * Compute types in ancestors
361 private class ClassAncestorBuilder
362 special AbsSyntaxVisitor
363 redef fun visit
(n
) do n
.accept_class_ancestor_builder
(self)
367 # Another pass visitor for syntax analysis.
368 # * Checks classes in regard to superclasses
369 private class ClassVerifierVisitor
370 special AbsSyntaxVisitor
371 redef fun visit
(n
) do n
.accept_class_verifier
(self)
376 # Another pass visitor for syntax analysis.
377 # * Build propertie names
378 # * Build local properties and attache them to global properties
379 # * Attach bound to formal types
380 private class PropertyBuilderVisitor
381 special AbsSyntaxVisitor
382 redef fun visit
(n
) do n
.accept_property_builder
(self)
386 # Another pass pass visitor for syntax analysis.
387 # * Check property conformance
388 private class PropertyVerifierVisitor
389 special AbsSyntaxVisitor
391 # The signature currently build
392 readable writable var _signature_builder
: SignatureBuilder
394 redef fun visit
(n
) do n
.accept_property_verifier
(self)
399 _signature_builder
= new SignatureBuilder
403 # Information about a signature currently build
404 private class SignatureBuilder
405 # Current visited parameter types
406 readable writable var _params
: Array[AParam] = new Array[AParam]
408 # Visited parameters without type information added
409 readable writable var _untyped_params
: Array[AParam] = new Array[AParam]
411 # Position of the current star parameter
412 readable writable var _vararg_rank
: Int = -1
414 # Current closure declarations
415 readable writable var _closure_decls
: Array[AClosureDecl] = new Array[AClosureDecl]
417 # True is a problen occured durring building
418 readable writable var _has_error_occured
: Bool = false
421 readable writable var _signature
: nullable MMSignature = null
424 ###############################################################################
427 private fun accept_class_builder
(v
: ClassBuilderVisitor) do accept_abs_syntax_visitor
(v
)
428 private fun accept_class_specialization_builder
(v
: ClassSpecializationBuilderVisitor) do accept_abs_syntax_visitor
(v
)
429 private fun accept_class_ancestor_builder
(v
: ClassAncestorBuilder) do accept_abs_syntax_visitor
(v
)
430 private fun accept_class_verifier
(v
: ClassVerifierVisitor) do accept_abs_syntax_visitor
(v
)
431 private fun accept_property_builder
(v
: PropertyBuilderVisitor) do accept_abs_syntax_visitor
(v
)
432 private fun accept_property_verifier
(v
: PropertyVerifierVisitor) do accept_abs_syntax_visitor
(v
)
436 # Import supermodules and compute visibility
437 fun import_super_modules
(tc
: ToolContext, mod
: MMSrcModule)
439 # Import super-modules
440 var module_names_to_import
= new Array[Symbol]
441 var module_visibility
= new HashMap[Symbol, Int]
442 var no_import
: nullable AImport = null
443 for i
in n_imports
do
444 var n
= i
.module_name
446 module_names_to_import
.add
(n
)
447 module_visibility
[n
] = i
.visibility_level
452 if no_import
!= null then
453 if not module_names_to_import
.is_empty
then
454 tc
.error
(no_import
.location
, "Error: Top modules cannot import other modules.")
456 else if module_names_to_import
.is_empty
then
457 var stdname
= once
"standard".to_symbol
458 module_names_to_import
.add
(stdname
)
459 module_visibility
[stdname
] = 1
462 mod
.import_supers_modules
(module_names_to_import
)
464 for mname
in module_names_to_import
do
465 var level
= module_visibility
[mname
]
466 var m
= tc
.get_module
(mname
, mod
)
467 mod
.add_super_module
(m
, level
)
472 redef class APackagedecl
473 redef fun accept_class_builder
(v
)
475 if n_id
.to_symbol
!= v
.module.name
then
476 v
.error
(n_id
, "Error: Package name missmatch between {v.module.name} and {n_id.to_symbol}")
482 # Imported module name (or null)
483 fun module_name
: nullable Symbol is abstract
485 # Visibility level (intrude/public/private)
486 fun visibility_level
: Int is abstract
488 redef class AStdImport
489 redef fun module_name
491 return n_id
.to_symbol
493 redef fun visibility_level
495 return n_visibility
.level
498 redef class ANoImport
499 redef fun module_name
505 redef class AVisibility
507 fun level
: Int is abstract
509 redef class APublicVisibility
510 redef fun level
do return 1
512 redef class AProtectedVisibility
513 redef fun level
do return 2
515 redef class APrivateVisibility
516 redef fun level
do return 3
518 redef class AIntrudeVisibility
519 redef fun level
do return 0
523 redef class AClassdef
524 redef fun local_class
: MMSrcLocalClass do return _local_class
.as(not null)
525 var _local_class
: nullable MMSrcLocalClass
528 fun name
: Symbol is abstract
530 # Number of formal parameters
531 fun arity
: Int do return 0
533 # Visibility of the class
534 fun visibility_level
: Int do return 1
536 redef fun accept_class_builder
(v
)
538 var local_class
: MMSrcLocalClass
540 var local_classes
= mod
.src_local_classes
541 if (local_classes
.has_key
(name
)) then
542 local_class
= local_classes
[name
]
543 if self isa AStdClassdef then
544 # If we are not a special implicit class then rant
545 v
.error
(self, "Error: A class {name} is already defined at line {local_class.node.location.line_start}.")
548 # Add the new node after the last node
549 var n
= local_class
.node
550 while n
.next_node
!= null do n
= n
.next_node
553 local_class
= new MMSrcLocalClass(mod
, name
, self, arity
)
554 local_classes
[name
] = local_class
555 if not mod
.has_global_class_named
(name
) then
556 local_class
.new_global
558 local_class
.set_global
(mod
.global_class_named
(name
))
562 _local_class
= local_class
563 v
.local_class_arity
= 0
564 v
.formals
= local_class
.formal_dict
573 redef fun accept_abs_syntax_visitor
(v
)
575 v
.local_class
= _local_class
581 redef class AClasskind
582 fun is_interface
: Bool do return false
583 fun is_universal
: Bool do return false
584 fun is_abstract
: Bool do return false
587 redef class AInterfaceClasskind
588 redef fun is_interface
do return true
590 redef class AUniversalClasskind
591 redef fun is_universal
do return true
593 redef class AAbstractClasskind
594 redef fun is_abstract
do return true
597 redef class AStdClassdef
600 return n_id
.to_symbol
604 return n_formaldefs
.length
606 redef fun accept_class_verifier
(v
)
609 var glob
= _local_class
.global
610 if glob
.intro
== _local_class
then
612 glob
.visibility_level
= visibility_level
613 glob
.is_interface
= n_classkind
.is_interface
614 glob
.is_abstract
= n_classkind
.is_abstract
615 glob
.is_universal
= n_classkind
.is_universal
616 if n_kwredef
!= null then
617 v
.error
(self, "Redef error: No class {name} is imported. Remove the redef keyword to define a new class.")
620 for c
in _local_class
.cshe
.direct_greaters
do
622 if glob
.is_interface
then
623 if cg
.is_universal
then
624 v
.error
(self, "Special error: Interface {name} try to specialise universal class {c.name}.")
625 else if not cg
.is_interface
then
626 v
.error
(self, "Special error: Interface {name} try to specialise class {c.name}.")
628 else if glob
.is_universal
then
629 if not cg
.is_interface
and not cg
.is_universal
then
630 v
.error
(self, "Special error: Universal class {name} try to specialise class {c.name}.")
633 if cg
.is_universal
then
634 v
.error
(self, "Special error: Class {name} try to specialise universal class {c.name}.")
644 glob
.check_visibility
(v
, self, v
.module)
645 if n_kwredef
== null then
646 v
.error
(self, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
650 if glob
.intro
.arity
!= _local_class
.arity
then
651 v
.error
(self, "Redef error: Formal parameter arity missmatch; got {_local_class.arity}, expected {glob.intro.arity}.")
655 not glob
.is_interface
and n_classkind
.is_interface
or
656 not glob
.is_abstract
and n_classkind
.is_abstract
or
657 not glob
.is_universal
and n_classkind
.is_universal
659 v
.error
(self, "Redef error: cannot change kind of class {name}.")
663 redef fun visibility_level
665 return n_visibility
.level
669 redef class AMainClassdef
672 return once
"Sys".to_symbol
676 redef class ATopClassdef
679 return once
"Object".to_symbol
683 redef class AFormaldef
684 # The associated formal generic parameter (MM entity)
685 var _formal
: nullable MMTypeFormalParameter
687 redef fun accept_class_builder
(v
)
689 var name
= n_id
.to_symbol
690 var formal_type
= new MMTypeFormalParameter(name
, v
.local_class_arity
, v
.local_class
)
691 _formal
= formal_type
692 v
.local_class_arity
= v
.local_class_arity
+ 1
693 v
.local_class
.register_formal
(formal_type
)
694 v
.formals
[name
] = formal_type
698 redef fun accept_class_verifier
(v
)
701 var c
= v
.local_class
702 var o
= c
.global
.intro
704 if n_type
== null then
705 _formal
.bound
= v
.module.type_any
.as_nullable
707 var stype
= n_type
.get_stype
(v
)
708 if stype
== null then return
709 _formal
.bound
= stype
712 var ob
= o
.get_formal
(_formal
.position
).bound
.for_module
(v
.module)
713 if n_type
== null then
716 var stype
= n_type
.get_stype
(v
)
717 if stype
== null then return
718 _formal
.bound
= stype
719 if _formal
.bound
!= ob
then
720 v
.error
(self, "Redef error: Cannot change formal parameter type of class {c}; got {_formal.bound}, expected {ob}.")
727 redef class ASuperclass
728 readable var _ancestor
: nullable MMSrcAncestor
730 redef fun accept_class_specialization_builder
(v
)
733 var c
= n_type
.get_local_class
(v
)
734 if c
== null then return
735 var ancestor
= new MMSrcAncestor(c
)
737 v
.local_class
.add_direct_parent
(ancestor
)
740 redef fun accept_class_ancestor_builder
(v
)
743 _ancestor
.stype
= n_type
.get_unchecked_stype
(v
)
744 _ancestor
.inheriter
= v
.local_class
.get_type
747 redef fun accept_class_verifier
(v
)
750 n_type
.check_conform
(v
)
755 # Process and check properties of the property.
756 # * Distinguish inits and methods
757 # * Inherit or check visibility.
758 # * Check redef errors.
759 # * Check forbiden attribute definitions.
760 # * Check signature conformance.
761 private fun process_and_check
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
763 if prop
.global
.intro
== prop
then
764 do_and_check_intro
(v
, prop
, has_redef
, visibility_level
)
766 do_and_check_redef
(v
, prop
, has_redef
, visibility_level
)
770 # The part of process_and_check when prop is an introduction
771 private fun do_and_check_intro
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
773 var glob
= prop
.global
774 var gbc
= prop
.local_class
.global
775 if v
.local_class
.global
.visibility_level
>= 3 then
776 # Method of private classes are private
779 glob
.visibility_level
= visibility_level
781 v
.error
(self, "Error: No property {prop.local_class}::{prop} is inherited. Remove the redef keyword to define a new property.")
783 if glob
.is_attribute
then
784 if gbc
.is_interface
then
785 v
.error
(self, "Error: Attempt to define attribute {prop} in the interface {prop.local_class}.")
786 else if gbc
.is_universal
then
787 v
.error
(self, "Error: Attempt to define attribute {prop} in the universal class {prop.local_class}.")
789 else if glob
.is_init
then
790 if gbc
.is_interface
then
791 v
.error
(self, "Error: Attempt to define a constructor {prop} in the class {prop.local_class}.")
792 else if gbc
.is_universal
then
793 v
.error
(self, "Error: Attempt to define a constructor {prop} in the universal {prop.local_class}.")
796 if prop
.signature
== null then
798 var supers
= prop
.local_class
.super_methods_named
(prop
.name
)
799 inherit_signature
(v
, prop
, supers
)
801 if prop
.signature
!= null or v
.signature_builder
.has_error_occured
then
803 else if not v
.signature_builder
.untyped_params
.is_empty
then
804 v
.error
(v
.signature_builder
.untyped_params
.first
, "Error: Untyped parameter.")
806 prop
.signature
= new MMSignature(new Array[MMType], null, v
.local_class
.get_type
)
807 for clos
in v
.signature_builder
.closure_decls
do
808 prop
.signature
.closures
.add
(clos
.variable
.closure
)
814 private fun inherit_signature
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, supers
: Array[MMLocalProperty])
816 var s
= prop
.signature
818 var isig
= ip
.signature
.adaptation_to
(v
.local_class
.get_type
)
821 if v
.signature_builder
.params
.length
!= isig
.arity
then
824 for p
in v
.signature_builder
.params
do
825 var t
= isig
[p
.position
]
827 if p
.position
== isig
.vararg_rank
then
839 # The part of process_and_check when prop is a redefinition
840 private fun do_and_check_redef
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
842 var is_init
= self isa AConcreteInitPropdef
843 var glob
= prop
.global
845 if not has_redef
then
846 v
.error
(self, "Redef error: {prop.local_class}::{prop} is an inherited property. To redefine it, add the redef keyword.")
849 if glob
.is_init
and not is_init
then
850 v
.error
(self, "Redef error: A method {prop.local_class}::{prop} cannot redefine a constructor.")
851 else if not glob
.is_init
and is_init
then
852 v
.error
(self, "Redef error: A constructor {prop.local_class}::{prop} cannot redefine a method.")
855 var s
= prop
.signature
856 #print "process {prop.local_class.module}::{prop.local_class}::{prop} from global {prop.global.local_property.local_class.module}::{prop.global.local_property.local_class}::{prop.global.local_property}"
857 for i
in prop
.prhe
.direct_greaters
do
858 var ip
= i
.local_class
[prop
.global
]
859 var isig
= i
.signature
.adaptation_to
(v
.local_class
.get_type
)
862 #print "{prop.full_name} inherits signature from {ip.full_name}"
863 if v
.signature_builder
.params
.length
!= isig
.arity
then
864 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
867 if v
.signature_builder
.closure_decls
.length
!= isig
.closures
.length
then
868 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} closure(s).")
871 for p
in v
.signature_builder
.params
do
872 var t
= isig
[p
.position
]
874 if p
.position
== isig
.vararg_rank
then
884 var nberr
= v
.tc
.error_count
885 #print "Check {prop.local_class}::{prop}{s} vs {ip.local_class}::{ip}{isig}"
886 #print "s={s.object_id} isig={isig.object_id} isigorig={i.signature.object_id}"
888 #print "orig signature: {i.signature.recv} . {i.signature}"
889 #print "inh signature: {isig.recv} . {isig}"
890 #print "redef signature: {s.recv} . {s}"
892 if s
.arity
!= isig
.arity
then
893 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
895 for j
in [0..s
.arity
[ do
896 if s
[j
] != isig
[j
] then
897 v
.error
(self, "Redef error: Expected {isig[j]} (as in {ip.local_class}::{ip}), got {s[j]} in {prop.local_class}::{prop}.")
902 var srt
= s
.return_type
903 var isrt
= isig
.return_type
904 if srt
== null and isrt
!= null then
905 v
.error
(self, "Redef error: The procedure {prop.local_class}::{prop} redefines the function {ip.local_class}::{ip}.")
906 else if srt
!= null and isrt
== null then
907 v
.error
(self, "Redef error: The function {prop.local_class}::{prop} redefines the procedure {ip.local_class}::{ip}.")
908 else if srt
!= null and isrt
!= null and not srt
< isrt
then
909 v
.error
(self, "Redef error: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
910 else if not s
< isig
and nberr
== v
.tc
.error_count
then
911 # Systematic fallback for conformance check
912 v
.error
(self, "Redef error: Incompatible redefinition of {ip.local_class}::{ip} with {prop.local_class}::{prop}")
913 else if srt
!= null and isrt
!= null and srt
!= isrt
and prop
isa MMAttribute then
915 v
.warning
(self, "Redef warning: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
919 if visibility_level
!= 1 and glob
.visibility_level
!= visibility_level
then
920 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefinition cannot change visibility.")
922 glob
.check_visibility
(v
, self, v
.module, true)
926 redef class AAttrPropdef
927 redef readable var _readmethod
: nullable MMSrcMethod
928 redef readable var _writemethod
: nullable MMSrcMethod
929 var _prop
: nullable MMSrcAttribute
930 redef fun prop
do return _prop
.as(not null)
932 redef fun accept_property_builder
(v
)
935 var name
= n_id
.to_symbol
936 var lc
= v
.local_class
937 var prop
= new MMSrcAttribute(name
, lc
, self)
939 v
.local_class
.add_src_local_property
(v
, prop
)
941 if n_readable
!= null then
942 name
= n_id
.text
.substring_from
(1).to_symbol
943 var readmethod
= new MMReadImplementationMethod(name
, lc
, self)
944 _readmethod
= readmethod
945 v
.local_class
.add_src_local_property
(v
, readmethod
)
947 if n_writable
!= null then
948 name
= (n_id
.text
.substring_from
(1) + "=").to_symbol
949 var writemethod
= new MMWriteImplementationMethod(name
, lc
, self)
950 _writemethod
= writemethod
951 v
.local_class
.add_src_local_property
(v
, writemethod
)
955 redef fun accept_property_verifier
(v
)
959 if n_type
!= null then
960 var t0
= n_type
.get_stype
(v
)
961 if t0
!= null then t
= t0
else return
963 v
.error
(self, "Not yet implemented: Attribute definition {_prop.local_class}::{_prop} requires an explicit type.")
968 var signature
= new MMSignature(new Array[MMType], t
, v
.local_class
.get_type
)
969 prop
.signature
= signature
970 var visibility_level
= n_visibility
.level
971 process_and_check
(v
, prop
, n_kwredef
!= null, visibility_level
)
972 if n_readable
!= null then
973 var m
= _readmethod
.as(not null)
974 m
.signature
= signature
975 process_and_check
(v
, m
, n_readable
.n_kwredef
!= null, visibility_level
)
976 n_type
.check_visibility
(v
, m
)
978 if n_writable
!= null then
979 var m
= _writemethod
.as(not null)
980 m
.signature
= new MMSignature(new Array[MMType].with_items
(t
), null, v
.local_class
.get_type
)
981 process_and_check
(v
, m
, n_writable
.n_kwredef
!= null, visibility_level
)
982 n_type
.check_visibility
(v
, m
)
986 redef fun accept_abs_syntax_visitor
(v
)
988 v
.local_property
= _prop
990 v
.local_property
= null
994 redef class AMethPropdef
996 readable var _name
: nullable Symbol
998 var _method
: nullable MMMethSrcMethod
999 redef fun method
do return _method
.as(not null)
1001 redef fun accept_property_builder
(v
)
1005 if n_methid
== null then
1006 if self isa AConcreteInitPropdef then
1007 name
= once
"init".to_symbol
1009 name
= once
"main".to_symbol
1012 name
= n_methid
.name
.as(not null)
1013 # FIXME: Add the 'unary' keyword
1014 if n_methid
.name
== (once
"-".to_symbol
) then
1015 var ns
= n_signature
1016 if ns
!= null and ns
.n_params
.length
== 0 then
1017 name
= once
"unary -".to_symbol
1022 var prop
= new MMMethSrcMethod(name
, v
.local_class
, self)
1024 v
.local_class
.add_src_local_property
(v
, prop
)
1027 redef fun accept_property_verifier
(v
)
1029 v
.signature_builder
= new SignatureBuilder
1032 if v
.signature_builder
.has_error_occured
then return
1034 if v
.signature_builder
.signature
== null then
1035 #_method.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
1037 method
.signature
= v
.signature_builder
.signature
.as(not null)
1039 var visibility_level
= 1
1040 if n_visibility
!= null and n_visibility
.level
> 1 then
1041 visibility_level
= n_visibility
.level
1043 process_and_check
(v
, method
, n_kwredef
!= null, visibility_level
)
1044 if n_signature
!= null then n_signature
.check_visibility
(v
, method
)
1047 redef fun accept_abs_syntax_visitor
(v
)
1049 v
.local_property
= _method
1051 v
.local_property
= null
1055 redef class AMainMethPropdef
1056 redef fun process_and_check
(v
, prop
, has_redef
, visibility_level
)
1058 prop
.global
.visibility_level
= visibility_level
1059 prop
.signature
= new MMSignature(new Array[MMType], null, v
.local_class
.get_type
)
1060 # Disable all checks for main
1064 redef class ATypePropdef
1065 redef fun prop
do return _prop
.as(not null)
1066 var _prop
: nullable MMSrcTypeProperty
1068 redef fun accept_property_builder
(v
)
1071 var name
= n_id
.to_symbol
1072 var prop
= new MMSrcTypeProperty(name
, v
.local_class
, self)
1074 v
.local_class
.add_src_local_property
(v
, prop
)
1077 redef fun accept_property_verifier
(v
)
1080 var signature
= new MMSignature(new Array[MMType], n_type
.get_stype
(v
), v
.local_class
.get_type
)
1081 prop
.signature
= signature
1082 var visibility_level
= n_visibility
.level
1083 process_and_check
(v
, prop
, n_kwredef
!= null, visibility_level
)
1086 redef fun accept_abs_syntax_visitor
(v
)
1088 v
.local_property
= _prop
1090 v
.local_property
= null
1094 # Visitor used to build a full method name from multiple tokens
1095 private class MethidAccumulator
1097 readable var _name
: Buffer = new Buffer
1101 _name
.append
(n
.text
)
1110 readable var _name
: nullable Symbol
1112 redef fun accept_property_builder
(v
)
1114 var accumulator
= new MethidAccumulator
1115 accumulator
.enter_visit
(self)
1116 _name
= accumulator
.name
.to_s
.to_symbol
1121 redef class ASignature
1122 redef fun accept_property_verifier
(v
)
1125 if v
.signature_builder
.has_error_occured
then
1127 else if not v
.signature_builder
.untyped_params
.is_empty
then
1128 if v
.signature_builder
.untyped_params
.first
!= v
.signature_builder
.params
.first
or n_type
!= null then
1129 v
.error
(v
.signature_builder
.untyped_params
.first
, "Syntax error: untyped parameter.")
1132 else if not v
.signature_builder
.params
.is_empty
or n_type
!= null then
1133 var pars
= new Array[MMType]
1134 for p
in v
.signature_builder
.params
do
1135 pars
.add
(p
.stype
.as(not null))
1137 var ret
: nullable MMType = null
1138 if n_type
!= null then
1139 ret
= n_type
.get_stype
(v
)
1141 v
.signature_builder
.has_error_occured
= true
1145 v
.signature_builder
.signature
= new MMSignature(pars
, ret
, v
.local_class
.get_type
)
1146 if v
.signature_builder
.vararg_rank
>= 0 then
1147 v
.signature_builder
.signature
.vararg_rank
= v
.signature_builder
.vararg_rank
1149 for clos
in v
.signature_builder
.closure_decls
do
1150 v
.signature_builder
.signature
.closures
.add
(clos
.variable
.closure
)
1155 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1156 fun check_visibility
(v
: AbsSyntaxVisitor, p
: MMLocalProperty)
1158 if p
.global
.visibility_level
>= 3 then return
1159 for n
in n_params
do
1160 if n
.n_type
!= null then n
.n_type
.check_visibility
(v
, p
)
1162 if n_type
!= null then n_type
.check_visibility
(v
, p
)
1167 redef readable var _position
: Int = 0
1169 redef fun variable
: ParamVariable do return _variable
.as(not null)
1170 var _variable
: nullable ParamVariable
1172 # The type of the parameter in signature
1173 readable writable var _stype
: nullable MMType
1175 redef fun accept_property_verifier
(v
)
1178 _position
= v
.signature_builder
.params
.length
1179 _variable
= new ParamVariable(n_id
.to_symbol
, self)
1180 v
.signature_builder
.params
.add
(self)
1181 v
.signature_builder
.untyped_params
.add
(self)
1182 if n_type
!= null then
1183 var stype
= n_type
.get_stype
(v
)
1184 if stype
== null then
1185 v
.signature_builder
.has_error_occured
= true
1188 for p
in v
.signature_builder
.untyped_params
do
1191 if v
.signature_builder
.vararg_rank
== -1 then
1192 v
.signature_builder
.vararg_rank
= p
.position
1194 v
.error
(self, "Error: A vararg parameter is already defined.")
1196 stype
= v
.type_array
(stype
)
1198 p
.variable
.stype
= stype
1200 v
.signature_builder
.untyped_params
.clear
1204 fun is_vararg
: Bool do return n_dotdotdot
!= null
1207 redef class AClosureDecl
1208 redef readable var _position
: Int = 0
1210 redef fun variable
: ClosureVariable do return _variable
.as(not null)
1211 var _variable
: nullable ClosureVariable
1213 redef fun accept_property_verifier
(v
)
1215 var old_signature_builder
= v
.signature_builder
1216 v
.signature_builder
= new SignatureBuilder
1218 if v
.signature_builder
.has_error_occured
then
1221 var sig
= v
.signature_builder
.signature
1223 sig
= new MMSignature(new Array[MMType], null, v
.local_class
.get_type
)
1225 if sig
.return_type
!= null and n_kwbreak
!= null then
1226 v
.error
(self, "Syntax Error: A break block cannot have a return value.")
1229 # Add the finalizer to the closure signature
1230 var finalize_sig
= new MMSignature(new Array[MMType], null, v
.module.type_any
) # FIXME should be no receiver
1231 var finalizer_clos
= new MMClosure(once
("break".to_symbol
), finalize_sig
, false, true)
1232 sig
.closures
.add
(finalizer_clos
)
1234 var name
= n_id
.to_symbol
1235 var clos
= new MMClosure(name
, sig
, n_kwbreak
!= null, n_expr
!= null)
1236 for c
in old_signature_builder
.closure_decls
do
1237 if c
.n_id
.to_symbol
== name
then
1238 v
.error
(n_id
, "A closure '!{name}' already defined at {c.n_id.location.relative_to(n_id.location)}.")
1242 v
.signature_builder
= old_signature_builder
1243 _position
= old_signature_builder
.closure_decls
.length
1244 old_signature_builder
.closure_decls
.add
(self)
1245 _variable
= new ClosureVariable(n_id
.to_symbol
, self, clos
)
1250 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1251 private fun check_visibility
(v
: AbsSyntaxVisitor, p
: MMLocalProperty)
1253 if p
.global
.visibility_level
>= 3 then return
1254 var t
= get_stype
(v
)
1255 if t
== null then return
1256 var bc
= t
.local_class
1257 if bc
.global
.visibility_level
>= 3 then
1258 v
.error
(self, "Access error: Class {bc} is private and cannot be used in the signature of the non-private property {p}.")
1261 n
.check_visibility
(v
, p
)
1267 redef fun accept_class_builder
(v
) do end
1268 redef fun accept_property_builder
(v
) do end
1269 redef fun accept_property_verifier
(v
) do end