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 mmbv1b
= new ClassVerifierVisitor(tc
, self)
89 c
.accept_class_visitor
(mmbv1b
)
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
)
166 # Accept a class visitor (on class properties)
167 redef fun accept_properties_visitor
(v
)
173 for p
in src_local_properties
do
174 p
.accept_property_visitor
(v
)
178 # Introduce or inherit default constructors
179 private fun process_default_constructors
(v
: PropertyBuilderVisitor)
181 # Is there already a constructor ?
182 for gp
in global_properties
do
184 # Return if explicit constructor in the class
185 if gp
.intro
.local_class
== self then return
189 # Collect visible constructors in super stateful classes
190 var super_inits
= new ArraySet[MMLocalProperty]
191 var super_constructors
= new ArraySet[MMGlobalProperty]
192 for sc
in che
.direct_greaters
do
193 if sc
.global
.is_universal
or sc
.global
.is_interface
then continue
194 for gp
in sc
.global_properties
do
195 if not gp
.is_init
then continue
196 super_constructors
.add
(gp
)
198 var initname
= once
("init".to_symbol
)
199 if sc
.has_global_property_by_name
(initname
) then
200 var gp
= sc
.get_property_by_name
(initname
)
201 super_inits
.add
(self[gp
])
205 # Collect unassigned attributes
206 var unassigned_attributes
= new Array[MMSrcAttribute]
207 for a
in src_local_properties
do
208 if a
isa MMSrcAttribute then
210 if n
.n_expr
== null then unassigned_attributes
.add
(a
)
214 if not super_constructors
.is_empty
then
215 # Select most specific classes introducing inheritable constructors
216 # Mixin classes are skipped
217 var supers
= new Array[MMLocalClass]
218 for gp
in super_constructors
do
219 var sc
= gp
.local_class
220 if supers
.has
(sc
) then continue
221 if not sc
.global
.is_mixin
then
225 supers
= che
.order
.select_smallests
(supers
)
227 # A mixin class can only have 0 or 1 most specific non-mixin superclass
228 var superclass
: nullable MMLocalClass = null # This most specific non-mixin superclass (if any)
230 if supers
.length
> 1 then
231 v
.error
(nodes
.first
, "Error: Explicit constructor required in {self} since multiple inheritance of constructor is forbiden. Conflicting classes are {supers.join(", ")}. Costructors are {super_constructors.join(", ")}.")
233 else if supers
.length
== 1 then
234 superclass
= supers
.first
237 for gp
in super_constructors
do
238 # Inherit constructors : the one of the non-mixin super class or all from the all mixin super-classes
239 if superclass
== null or gp
.local_class
== superclass
then
240 make_visible_an_inherited_global_property
(gp
)
243 global
.mixin_of
= superclass
.as(not null).global
# FIXME Dear! this should break!
245 # v.error(nodes.first, "Error, constructor required in {self} since no anonimous init found in {sc}.")
247 # unassigned attributes, then implicit consructors are generated
248 var p
= new MMImplicitInit(self, unassigned_attributes
, super_inits
.to_a
)
249 add_src_local_property
(v
, p
)
250 #print("Create implicit init {p} in {self} from {super_inits.join(", ")} + {unassigned_attributes.length} args")
254 # Add a source property
255 # Register it to the class and attach it to global property
256 private fun add_src_local_property
(v
: PropertyBuilderVisitor, prop
: MMLocalProperty)
258 var pname
= prop
.name
259 # Check double definition in the same class
260 if src_local_properties
.has_key
(pname
) then
261 v
.error
(prop
.node
, "Error: A property {pname} is already defined in class {name}.")
264 src_local_properties
[pname
] = prop
266 # Intro or redefinition ?
267 if has_global_property_by_name
(pname
) then
268 var g
= get_property_by_name
(pname
)
269 prop
.inherit_global
(g
)
272 if not prop
.is_global_set
then
274 prop
.global
.is_init
= prop
.is_init
279 redef class MMLocalProperty
280 private fun accept_property_visitor
(v
: AbsSyntaxVisitor)
285 redef class MMImplicitInit
286 redef readable var _super_init
: nullable MMLocalProperty = null
287 redef fun accept_property_visitor
(v
)
289 var base
: nullable MMLocalProperty = null
290 for p
in super_inits
do
291 if p
.signature
.arity
> 0 then
295 v
.error
(null, "Error: explicit constructor needed in {local_class} since both super-constructor {base.full_name} and {p.full_name} have paramters")
302 var params
= new Array[MMType]
304 var sig
= base
.signature
305 for i
in [0..sig
.arity
[ do
309 for a
in unassigned_attributes
do
310 var sig
= a
.signature
311 if sig
== null then return # Broken attribute definition
312 params
.add
(sig
.return_type
.as(not null))
314 signature
= new MMSignature(params
, null, local_class
.get_type
)
319 # Concrete NIT class specialization relation
322 # The related AST node
323 readable var _node
: ASuperclass
324 redef readable var _local_class
: MMLocalClass
326 init(n
: ASuperclass, c
: MMLocalClass)
333 ###############################################################################
335 # A pass visitor for syntax analysis.
336 # * Build the classes and attach them to global classes
337 # * Collect generic formal parameters.
338 private class ClassBuilderVisitor
339 special AbsSyntaxVisitor
340 # Current class arity
341 readable writable var _local_class_arity
: Int = 0
343 # Current class formal parameters
344 readable writable var _formals
: nullable Map[Symbol, MMTypeFormalParameter]
346 redef fun visit
(n
) do n
.accept_class_builder
(self)
350 # Another pass visitor for syntax analysis.
351 # * Build ancertors (with only class informations not the type one)
352 private class ClassSpecializationBuilderVisitor
353 special AbsSyntaxVisitor
354 redef fun visit
(n
) do n
.accept_class_specialization_builder
(self)
358 # Another pass visitor for syntax analysis.
359 # * Compute types in ancestors
360 private class ClassAncestorBuilder
361 special AbsSyntaxVisitor
362 redef fun visit
(n
) do n
.accept_class_ancestor_builder
(self)
366 # Another pass visitor for syntax analysis.
367 # * Checks classes in regard to superclasses
368 private class ClassVerifierVisitor
369 special AbsSyntaxVisitor
370 redef fun visit
(n
) do n
.accept_class_verifier
(self)
375 # Another pass visitor for syntax analysis.
376 # * Build propertie names
377 # * Build local properties and attache them to global properties
378 # * Attach bound to formal types
379 private class PropertyBuilderVisitor
380 special AbsSyntaxVisitor
381 redef fun visit
(n
) do n
.accept_property_builder
(self)
385 # Another pass pass visitor for syntax analysis.
386 # * Check property conformance
387 private class PropertyVerifierVisitor
388 special AbsSyntaxVisitor
390 # The signature currently build
391 readable writable var _signature_builder
: SignatureBuilder
393 redef fun visit
(n
) do n
.accept_property_verifier
(self)
398 _signature_builder
= new SignatureBuilder
402 # Information about a signature currently build
403 private class SignatureBuilder
404 # Current visited parameter types
405 readable writable var _params
: Array[PParam] = new Array[PParam]
407 # Visited parameters without type information added
408 readable writable var _untyped_params
: Array[PParam] = new Array[PParam]
410 # Position of the current star parameter
411 readable writable var _vararg_rank
: Int = -1
413 # Current closure declarations
414 readable writable var _closure_decls
: Array[AClosureDecl] = new Array[AClosureDecl]
416 # True is a problen occured durring building
417 readable writable var _has_error_occured
: Bool = false
420 readable writable var _signature
: nullable MMSignature = null
423 ###############################################################################
426 private fun accept_class_builder
(v
: ClassBuilderVisitor) do accept_abs_syntax_visitor
(v
)
427 private fun accept_class_specialization_builder
(v
: ClassSpecializationBuilderVisitor) do accept_abs_syntax_visitor
(v
)
428 private fun accept_class_ancestor_builder
(v
: ClassAncestorBuilder) do accept_abs_syntax_visitor
(v
)
429 private fun accept_class_verifier
(v
: ClassVerifierVisitor) do accept_abs_syntax_visitor
(v
)
430 private fun accept_property_builder
(v
: PropertyBuilderVisitor) do accept_abs_syntax_visitor
(v
)
431 private fun accept_property_verifier
(v
: PropertyVerifierVisitor) do accept_abs_syntax_visitor
(v
)
435 # Import supermodules and compute visibility
436 fun import_super_modules
(tc
: ToolContext, mod
: MMSrcModule)
438 # Import super-modules
439 var module_names_to_import
= new Array[Symbol]
440 var module_visibility
= new HashMap[Symbol, Int]
441 var no_import
: nullable PImport = null
442 for i
in n_imports
do
443 var n
= i
.module_name
445 module_names_to_import
.add
(n
)
446 module_visibility
[n
] = i
.visibility_level
451 if no_import
!= null then
452 if not module_names_to_import
.is_empty
then
453 tc
.error
(no_import
.location
, "Error: Top modules cannot import other modules.")
455 else if module_names_to_import
.is_empty
then
456 var stdname
= once
"standard".to_symbol
457 module_names_to_import
.add
(stdname
)
458 module_visibility
[stdname
] = 1
461 mod
.import_supers_modules
(module_names_to_import
)
463 for mname
in module_names_to_import
do
464 var level
= module_visibility
[mname
]
465 var m
= tc
.get_module
(mname
, mod
)
466 mod
.add_super_module
(m
, level
)
471 redef class APackagedecl
472 redef fun accept_class_builder
(v
)
474 if n_id
.to_symbol
!= v
.module.name
then
475 v
.error
(n_id
, "Error: Package name missmatch between {v.module.name} and {n_id.to_symbol}")
481 # Imported module name (or null)
482 fun module_name
: nullable Symbol is abstract
484 # Visibility level (intrude/public/private)
485 fun visibility_level
: Int is abstract
488 redef fun module_name
490 return n_id
.to_symbol
492 redef fun visibility_level
494 return n_visibility
.level
497 redef class ANoImport
498 redef fun module_name
504 redef class PVisibility
506 fun level
: Int is abstract
508 redef class APublicVisibility
509 redef fun level
do return 1
511 redef class AProtectedVisibility
512 redef fun level
do return 2
514 redef class APrivateVisibility
515 redef fun level
do return 3
517 redef class AIntrudeVisibility
518 redef fun level
do return 0
522 redef class PClassdef
523 redef fun local_class
: MMSrcLocalClass do return _local_class
.as(not null)
524 var _local_class
: nullable MMSrcLocalClass
527 fun name
: Symbol is abstract
529 # Number of formal parameters
530 fun arity
: Int do return 0
532 # Visibility of the class
533 fun visibility_level
: Int do return 1
535 redef fun accept_class_builder
(v
)
537 var local_class
: MMSrcLocalClass
539 var local_classes
= mod
.src_local_classes
540 if (local_classes
.has_key
(name
)) then
541 local_class
= local_classes
[name
]
542 if self isa AClassdef then
543 # If we are not a special implicit class then rant
544 v
.error
(self, "Error: A class {name} is already defined at line {local_class.nodes.first.first_token.location.line_start}.")
547 local_class
.nodes
.add
(self)
549 local_class
= new MMSrcLocalClass(mod
, name
, self, arity
)
550 local_classes
[name
] = local_class
551 if not mod
.has_global_class_named
(name
) then
552 local_class
.new_global
554 local_class
.set_global
(mod
.global_class_named
(name
))
558 _local_class
= local_class
559 v
.local_class_arity
= 0
560 v
.formals
= local_class
.formal_dict
569 redef fun accept_abs_syntax_visitor
(v
)
571 v
.local_class
= _local_class
577 redef class PClasskind
578 fun is_interface
: Bool do return false
579 fun is_universal
: Bool do return false
580 fun is_abstract
: Bool do return false
583 redef class AInterfaceClasskind
584 redef fun is_interface
do return true
586 redef class AUniversalClasskind
587 redef fun is_universal
do return true
589 redef class AAbstractClasskind
590 redef fun is_abstract
do return true
593 redef class AClassdef
596 return n_id
.to_symbol
600 return n_formaldefs
.length
602 redef fun accept_class_verifier
(v
)
605 var glob
= _local_class
.global
606 if glob
.intro
== _local_class
then
608 glob
.visibility_level
= visibility_level
609 glob
.is_interface
= n_classkind
.is_interface
610 glob
.is_abstract
= n_classkind
.is_abstract
611 glob
.is_universal
= n_classkind
.is_universal
612 if n_kwredef
!= null then
613 v
.error
(self, "Redef error: No class {name} is imported. Remove the redef keyword to define a new class.")
616 for c
in _local_class
.cshe
.direct_greaters
do
618 if glob
.is_interface
then
619 if cg
.is_universal
then
620 v
.error
(self, "Special error: Interface {name} try to specialise universal class {c.name}.")
621 else if not cg
.is_interface
then
622 v
.error
(self, "Special error: Interface {name} try to specialise class {c.name}.")
624 else if glob
.is_universal
then
625 if not cg
.is_interface
and not cg
.is_universal
then
626 v
.error
(self, "Special error: Universal class {name} try to specialise class {c.name}.")
629 if cg
.is_universal
then
630 v
.error
(self, "Special error: Class {name} try to specialise universal class {c.name}.")
640 glob
.check_visibility
(v
, self, v
.module)
641 if n_kwredef
== null then
642 v
.error
(self, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
646 if glob
.intro
.arity
!= _local_class
.arity
then
647 v
.error
(self, "Redef error: Formal parameter arity missmatch; got {_local_class.arity}, expected {glob.intro.arity}.")
651 not glob
.is_interface
and n_classkind
.is_interface
or
652 not glob
.is_abstract
and n_classkind
.is_abstract
or
653 not glob
.is_universal
and n_classkind
.is_universal
655 v
.error
(self, "Redef error: cannot change kind of class {name}.")
659 redef fun visibility_level
661 return n_visibility
.level
665 redef class AMainClassdef
668 return once
"Sys".to_symbol
672 redef class ATopClassdef
675 return once
"Object".to_symbol
679 class MMSrcTypeFormalParameter
680 special MMTypeFormalParameter
681 # The associated node
682 readable var _node
: AFormaldef
684 init(name
: Symbol, pos
: Int, local_class
: MMLocalClass, n
: AFormaldef)
686 super(name
, pos
, local_class
)
691 redef class AFormaldef
692 # The associated formal generic parameter (MM entity)
693 var _formal
: nullable MMSrcTypeFormalParameter
695 redef fun accept_class_builder
(v
)
697 var name
= n_id
.to_symbol
698 var formal_type
= new MMSrcTypeFormalParameter(name
, v
.local_class_arity
, v
.local_class
, self)
699 _formal
= formal_type
700 v
.local_class_arity
= v
.local_class_arity
+ 1
701 v
.local_class
.register_formal
(formal_type
)
702 v
.formals
[name
] = formal_type
706 redef fun accept_class_verifier
(v
)
709 var c
= v
.local_class
710 var o
= c
.global
.intro
712 if n_type
== null then
713 _formal
.bound
= v
.module.type_any
.as_nullable
715 var stype
= n_type
.get_stype
(v
)
716 if stype
== null then return
717 _formal
.bound
= stype
720 var ob
= o
.get_formal
(_formal
.position
).bound
.for_module
(v
.module)
721 if n_type
== null then
724 var stype
= n_type
.get_stype
(v
)
725 if stype
== null then return
726 _formal
.bound
= stype
727 if _formal
.bound
!= ob
then
728 v
.error
(self, "Redef error: Cannot change formal parameter type of class {c}; got {_formal.bound}, expected {ob}.")
735 redef class ASuperclass
736 readable var _ancestor
: nullable MMSrcAncestor
738 redef fun accept_class_specialization_builder
(v
)
741 var c
= n_type
.get_local_class
(v
)
742 if c
== null then return
743 var ancestor
= new MMSrcAncestor(self, c
)
745 v
.local_class
.add_direct_parent
(ancestor
)
748 redef fun accept_class_ancestor_builder
(v
)
751 _ancestor
.stype
= n_type
.get_unchecked_stype
(v
)
752 _ancestor
.inheriter
= v
.local_class
.get_type
755 redef fun accept_class_verifier
(v
)
758 n_type
.check_conform
(v
)
763 # Process and check properties of the property.
764 # * Distinguish inits and methods
765 # * Inherit or check visibility.
766 # * Check redef errors.
767 # * Check forbiden attribute definitions.
768 # * Check signature conformance.
769 private fun process_and_check
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
771 if prop
.global
.intro
== prop
then
772 do_and_check_intro
(v
, prop
, has_redef
, visibility_level
)
774 do_and_check_redef
(v
, prop
, has_redef
, visibility_level
)
778 # The part of process_and_check when prop is an introduction
779 private fun do_and_check_intro
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
781 var glob
= prop
.global
782 var gbc
= prop
.local_class
.global
783 if v
.local_class
.global
.visibility_level
>= 3 then
784 # Method of private classes are private
787 glob
.visibility_level
= visibility_level
789 v
.error
(self, "Error: No property {prop.local_class}::{prop} is inherited. Remove the redef keyword to define a new property.")
791 if glob
.is_attribute
then
792 if gbc
.is_interface
then
793 v
.error
(self, "Error: Attempt to define attribute {prop} in the interface {prop.local_class}.")
794 else if gbc
.is_universal
then
795 v
.error
(self, "Error: Attempt to define attribute {prop} in the universal class {prop.local_class}.")
797 else if glob
.is_init
then
798 if gbc
.is_interface
then
799 v
.error
(self, "Error: Attempt to define a constructor {prop} in the class {prop.local_class}.")
800 else if gbc
.is_universal
then
801 v
.error
(self, "Error: Attempt to define a constructor {prop} in the universal {prop.local_class}.")
804 if prop
.signature
== null then
806 var supers
= prop
.local_class
.super_methods_named
(prop
.name
)
807 inherit_signature
(v
, prop
, supers
)
809 if prop
.signature
!= null or v
.signature_builder
.has_error_occured
then
811 else if not v
.signature_builder
.untyped_params
.is_empty
then
812 v
.error
(v
.signature_builder
.untyped_params
.first
, "Error: Untyped parameter.")
814 prop
.signature
= new MMSignature(new Array[MMType], null, v
.local_class
.get_type
)
815 for clos
in v
.signature_builder
.closure_decls
do
816 prop
.signature
.closures
.add
(clos
.variable
.closure
)
822 private fun inherit_signature
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, supers
: Array[MMLocalProperty])
824 var s
= prop
.signature
826 var isig
= ip
.signature
.adaptation_to
(v
.local_class
.get_type
)
829 if v
.signature_builder
.params
.length
!= isig
.arity
then
830 #prop.node.printl("v.params.length {v.params.length} != isig.arity {isig.arity} ; {prop.full_name} vs {ip.full_name}")
833 for p
in v
.signature_builder
.params
do
834 var t
= isig
[p
.position
]
836 if p
.position
== isig
.vararg_rank
then
848 # The part of process_and_check when prop is a redefinition
849 private fun do_and_check_redef
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
851 var is_init
= self isa AConcreteInitPropdef
852 var glob
= prop
.global
854 if not has_redef
then
855 v
.error
(self, "Redef error: {prop.local_class}::{prop} is an inherited property. To redefine it, add the redef keyword.")
858 if glob
.is_init
and not is_init
then
859 v
.error
(self, "Redef error: A method {prop.local_class}::{prop} cannot redefine a constructor.")
860 else if not glob
.is_init
and is_init
then
861 v
.error
(self, "Redef error: A constructor {prop.local_class}::{prop} cannot redefine a method.")
864 var s
= prop
.signature
865 #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}"
866 for i
in prop
.prhe
.direct_greaters
do
867 var ip
= i
.local_class
[prop
.global
]
868 var isig
= i
.signature
.adaptation_to
(v
.local_class
.get_type
)
871 #print "{prop.full_name} inherits signature from {ip.full_name}"
872 if v
.signature_builder
.params
.length
!= isig
.arity
then
873 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
876 if v
.signature_builder
.closure_decls
.length
!= isig
.closures
.length
then
877 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} closure(s).")
880 for p
in v
.signature_builder
.params
do
881 var t
= isig
[p
.position
]
883 if p
.position
== isig
.vararg_rank
then
893 var nberr
= v
.tc
.error_count
894 #print "Check {prop.local_class}::{prop}{s} vs {ip.local_class}::{ip}{isig}"
895 #print "s={s.object_id} isig={isig.object_id} isigorig={i.signature.object_id}"
897 #print "orig signature: {i.signature.recv} . {i.signature}"
898 #print "inh signature: {isig.recv} . {isig}"
899 #print "redef signature: {s.recv} . {s}"
901 if s
.arity
!= isig
.arity
then
902 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
904 for i
in [0..s
.arity
[ do
905 if s
[i
] != isig
[i
] then
906 v
.error
(self, "Redef error: Expected {isig[i]} (as in {ip.local_class}::{ip}), got {s[i]} in {prop.local_class}::{prop}.")
911 var srt
= s
.return_type
912 var isrt
= isig
.return_type
913 if srt
== null and isrt
!= null then
914 v
.error
(self, "Redef error: The procedure {prop.local_class}::{prop} redefines the function {ip.local_class}::{ip}.")
915 else if srt
!= null and isrt
== null then
916 v
.error
(self, "Redef error: The function {prop.local_class}::{prop} redefines the procedure {ip.local_class}::{ip}.")
917 else if srt
!= null and isrt
!= null and not srt
< isrt
then
918 v
.error
(self, "Redef error: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
919 else if not s
< isig
and nberr
== v
.tc
.error_count
then
920 # Systematic fallback for conformance check
921 v
.error
(self, "Redef error: Incompatible redefinition of {ip.local_class}::{ip} with {prop.local_class}::{prop}")
922 else if srt
!= null and isrt
!= null and srt
!= isrt
and prop
isa MMAttribute then
924 v
.warning
(self, "Redef warning: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
928 if visibility_level
!= 1 and glob
.visibility_level
!= visibility_level
then
929 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefinition cannot change visibility.")
931 glob
.check_visibility
(v
, self, v
.module, true)
935 redef class AAttrPropdef
936 redef readable var _readmethod
: nullable MMSrcMethod
937 redef readable var _writemethod
: nullable MMSrcMethod
938 var _prop
: nullable MMSrcAttribute
939 redef fun prop
do return _prop
.as(not null)
941 redef fun accept_property_builder
(v
)
944 var name
= n_id
.to_symbol
945 var lc
= v
.local_class
946 var prop
= new MMSrcAttribute(name
, lc
, self)
948 v
.local_class
.add_src_local_property
(v
, prop
)
950 if n_readable
!= null then
951 name
= n_id
.text
.substring_from
(1).to_symbol
952 var readmethod
= new MMReadImplementationMethod(name
, lc
, self)
953 _readmethod
= readmethod
954 v
.local_class
.add_src_local_property
(v
, readmethod
)
956 if n_writable
!= null then
957 name
= (n_id
.text
.substring_from
(1) + "=").to_symbol
958 var writemethod
= new MMWriteImplementationMethod(name
, lc
, self)
959 _writemethod
= writemethod
960 v
.local_class
.add_src_local_property
(v
, writemethod
)
964 redef fun accept_property_verifier
(v
)
968 if n_type
!= null then
969 var t0
= n_type
.get_stype
(v
)
970 if t0
!= null then t
= t0
else return
972 v
.error
(self, "Not yet implemented: Attribute definition {_prop.local_class}::{_prop} requires an explicit type.")
977 var signature
= new MMSignature(new Array[MMType], t
, v
.local_class
.get_type
)
978 prop
.signature
= signature
979 var visibility_level
= n_visibility
.level
980 process_and_check
(v
, prop
, n_kwredef
!= null, visibility_level
)
981 if n_readable
!= null then
982 var m
= _readmethod
.as(not null)
983 m
.signature
= signature
984 process_and_check
(v
, m
, n_readable
.n_kwredef
!= null, visibility_level
)
985 n_type
.check_visibility
(v
, m
)
987 if n_writable
!= null then
988 var m
= _writemethod
.as(not null)
989 m
.signature
= new MMSignature(new Array[MMType].with_items
(t
), null, v
.local_class
.get_type
)
990 process_and_check
(v
, m
, n_writable
.n_kwredef
!= null, visibility_level
)
991 n_type
.check_visibility
(v
, m
)
995 redef fun accept_abs_syntax_visitor
(v
)
997 v
.local_property
= _prop
999 v
.local_property
= null
1003 redef class AMethPropdef
1004 # Name of the method
1005 readable var _name
: nullable Symbol
1007 var _method
: nullable MMMethSrcMethod
1008 redef fun method
do return _method
.as(not null)
1010 redef fun accept_property_builder
(v
)
1014 if n_methid
== null then
1015 if self isa AConcreteInitPropdef then
1016 name
= once
"init".to_symbol
1018 name
= once
"main".to_symbol
1021 name
= n_methid
.name
.as(not null)
1022 # FIXME: Add the 'unary' keyword
1023 if n_methid
.name
== (once
"-".to_symbol
) then
1024 var ns
= n_signature
1025 if ns
isa ASignature and ns
.n_params
.length
== 0 then
1026 name
= once
"unary -".to_symbol
1031 var prop
= new MMMethSrcMethod(name
, v
.local_class
, self)
1033 v
.local_class
.add_src_local_property
(v
, prop
)
1036 redef fun accept_property_verifier
(v
)
1038 v
.signature_builder
= new SignatureBuilder
1041 if v
.signature_builder
.has_error_occured
then return
1043 if v
.signature_builder
.signature
== null then
1044 #_method.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
1046 method
.signature
= v
.signature_builder
.signature
.as(not null)
1048 var visibility_level
= 1
1049 if n_visibility
!= null and n_visibility
.level
> 1 then
1050 visibility_level
= n_visibility
.level
1052 process_and_check
(v
, method
, n_kwredef
!= null, visibility_level
)
1053 if n_signature
!= null then n_signature
.check_visibility
(v
, method
)
1056 redef fun accept_abs_syntax_visitor
(v
)
1058 v
.local_property
= _method
1060 v
.local_property
= null
1064 redef class AMainMethPropdef
1065 redef fun process_and_check
(v
, prop
, has_redef
, visibility_level
)
1067 prop
.global
.visibility_level
= visibility_level
1068 prop
.signature
= new MMSignature(new Array[MMType], null, v
.local_class
.get_type
)
1069 # Disable all checks for main
1073 redef class ATypePropdef
1074 redef fun prop
do return _prop
.as(not null)
1075 var _prop
: nullable MMSrcTypeProperty
1077 redef fun accept_property_builder
(v
)
1080 var name
= n_id
.to_symbol
1081 var prop
= new MMSrcTypeProperty(name
, v
.local_class
, self)
1083 v
.local_class
.add_src_local_property
(v
, prop
)
1086 redef fun accept_property_verifier
(v
)
1089 var signature
= new MMSignature(new Array[MMType], n_type
.get_stype
(v
), v
.local_class
.get_type
)
1090 prop
.signature
= signature
1091 var visibility_level
= n_visibility
.level
1092 process_and_check
(v
, prop
, n_kwredef
!= null, visibility_level
)
1095 redef fun accept_abs_syntax_visitor
(v
)
1097 v
.local_property
= _prop
1099 v
.local_property
= null
1103 # Visitor used to build a full method name from multiple tokens
1104 private class MethidAccumulator
1106 readable var _name
: Buffer = new Buffer
1110 _name
.append
(n
.text
)
1119 readable var _name
: nullable Symbol
1121 redef fun accept_property_builder
(v
)
1123 var accumulator
= new MethidAccumulator
1124 accumulator
.enter_visit
(self)
1125 _name
= accumulator
.name
.to_s
.to_symbol
1130 redef class PSignature
1131 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1132 fun check_visibility
(v
: AbsSyntaxVisitor, p
: MMLocalProperty) is abstract
1135 redef class ASignature
1136 redef fun accept_property_verifier
(v
)
1139 if v
.signature_builder
.has_error_occured
then
1141 else if not v
.signature_builder
.untyped_params
.is_empty
then
1142 if v
.signature_builder
.untyped_params
.first
!= v
.signature_builder
.params
.first
or n_type
!= null then
1143 v
.error
(v
.signature_builder
.untyped_params
.first
, "Syntax error: untyped parameter.")
1146 else if not v
.signature_builder
.params
.is_empty
or n_type
!= null then
1147 var pars
= new Array[MMType]
1148 for p
in v
.signature_builder
.params
do
1149 pars
.add
(p
.stype
.as(not null))
1151 var ret
: nullable MMType = null
1152 if n_type
!= null then
1153 ret
= n_type
.get_stype
(v
)
1155 v
.signature_builder
.has_error_occured
= true
1159 v
.signature_builder
.signature
= new MMSignature(pars
, ret
, v
.local_class
.get_type
)
1160 if v
.signature_builder
.vararg_rank
>= 0 then
1161 v
.signature_builder
.signature
.vararg_rank
= v
.signature_builder
.vararg_rank
1163 for clos
in v
.signature_builder
.closure_decls
do
1164 v
.signature_builder
.signature
.closures
.add
(clos
.variable
.closure
)
1169 redef fun check_visibility
(v
, p
)
1171 if p
.global
.visibility_level
>= 3 then return
1172 for n
in n_params
do
1173 if n
.n_type
!= null then n
.n_type
.check_visibility
(v
, p
)
1175 if n_type
!= null then n_type
.check_visibility
(v
, p
)
1180 redef readable var _position
: Int = 0
1182 redef fun variable
: ParamVariable do return _variable
.as(not null)
1183 var _variable
: nullable ParamVariable
1185 # The type of the parameter in signature
1186 readable writable var _stype
: nullable MMType
1188 redef fun accept_property_verifier
(v
)
1191 _position
= v
.signature_builder
.params
.length
1192 _variable
= new ParamVariable(n_id
.to_symbol
, self)
1193 v
.signature_builder
.params
.add
(self)
1194 v
.signature_builder
.untyped_params
.add
(self)
1195 if n_type
!= null then
1196 var stype
= n_type
.get_stype
(v
)
1197 if stype
== null then
1198 v
.signature_builder
.has_error_occured
= true
1201 for p
in v
.signature_builder
.untyped_params
do
1204 if v
.signature_builder
.vararg_rank
== -1 then
1205 v
.signature_builder
.vararg_rank
= p
.position
1207 v
.error
(self, "Error: A vararg parameter is already defined.")
1209 stype
= v
.type_array
(stype
)
1211 p
.variable
.stype
= stype
1213 v
.signature_builder
.untyped_params
.clear
1217 fun is_vararg
: Bool is abstract
1221 redef fun is_vararg
do return n_dotdotdot
!= null
1224 redef class AClosureDecl
1225 redef readable var _position
: Int = 0
1227 redef fun variable
: ClosureVariable do return _variable
.as(not null)
1228 var _variable
: nullable ClosureVariable
1230 redef fun accept_property_verifier
(v
)
1232 var old_signature_builder
= v
.signature_builder
1233 v
.signature_builder
= new SignatureBuilder
1235 if v
.signature_builder
.has_error_occured
then
1238 var sig
= v
.signature_builder
.signature
1240 sig
= new MMSignature(new Array[MMType], null, v
.local_class
.get_type
)
1242 if sig
.return_type
!= null and n_kwbreak
!= null then
1243 v
.error
(self, "Syntax Error: A break block cannot have a return value.")
1246 # Add the finalizer to the closure signature
1247 var finalize_sig
= new MMSignature(new Array[MMType], null, v
.module.type_any
) # FIXME should be no receiver
1248 var finalizer_clos
= new MMClosure(finalize_sig
, false, true)
1249 sig
.closures
.add
(finalizer_clos
)
1251 var clos
= new MMClosure(sig
, n_kwbreak
!= null, n_expr
!= null)
1252 v
.signature_builder
= old_signature_builder
1253 _position
= old_signature_builder
.closure_decls
.length
1254 old_signature_builder
.closure_decls
.add
(self)
1255 _variable
= new ClosureVariable(n_id
.to_symbol
, self, clos
)
1260 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1261 private fun check_visibility
(v
: AbsSyntaxVisitor, p
: MMLocalProperty) is abstract
1265 redef fun check_visibility
(v
, p
)
1267 if p
.global
.visibility_level
>= 3 then return
1268 var t
= get_stype
(v
)
1269 if t
== null then return
1270 var bc
= t
.local_class
1271 if bc
.global
.visibility_level
>= 3 then
1272 v
.error
(self, "Access error: Class {bc} is private and cannot be used in the signature of the non-private property {p}.")
1275 n
.check_visibility
(v
, p
)
1281 redef fun accept_class_builder
(v
) do end
1282 redef fun accept_property_builder
(v
) do end
1283 redef fun accept_property_verifier
(v
) do end