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 super 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
.mmmodule
) < 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 # Global property introduction and redefinition
97 c
.accept_class_visitor
(mmbv2
)
99 # Default and inherited constructor if needed
100 if c
isa MMSrcLocalClass and c
.global
.intro
== c
and not c
.global
.is_enum
and not c
.global
.is_interface
then
101 c
.process_default_constructors
(mmbv2
)
104 # Note that inherited unredefined property are processed on demand latter
108 # Property signature analysis and inheritance conformance
109 var mmbv3
= new PropertyVerifierVisitor(tc
, self)
111 c
.accept_properties_visitor
(mmbv3
)
117 # Create some primitive default classes if they do not exists
118 fun process_default_classes
(tc
: ToolContext)
120 var name
= once
("Object".to_symbol
)
121 if not has_global_class_named
(name
) then
122 var c
= new MMSrcLocalClass(self, name
, null, 0)
124 src_local_classes
[name
] = c
126 name
= once
("Bool".to_symbol
)
127 if not has_global_class_named
(name
) then
128 var c
= new MMSrcLocalClass(self, name
, null, 0)
130 src_local_classes
[name
] = c
135 redef class MMLocalClass
136 # Accept a class visitor (on class nodes)
137 private fun accept_class_visitor
(v
: AbsSyntaxVisitor)
141 # Accept a class visitor (on class properties)
142 private fun accept_properties_visitor
(v
: AbsSyntaxVisitor)
147 redef class MMSrcLocalClass
148 redef fun accept_class_visitor
(v
)
157 # Accept a class visitor (on class properties)
158 redef fun accept_properties_visitor
(v
)
166 for p
in src_local_properties
do
167 p
.accept_property_visitor
(v
)
171 # Introduce or inherit default constructors
172 private fun process_default_constructors
(v
: PropertyBuilderVisitor)
174 # Is there already a constructor ?
175 for gp
in global_properties
do
177 # Return if explicit constructor in the class
178 if gp
.intro
.local_class
== self then return
182 # Collect visible constructors in super stateful classes
183 var super_inits
= new ArraySet[MMLocalProperty]
184 var super_constructors
= new ArraySet[MMGlobalProperty]
185 for sc
in che
.direct_greaters
do
186 if sc
.global
.is_enum
or sc
.global
.is_interface
then continue
187 for gp
in sc
.global_properties
do
188 if not gp
.is_init
then continue
189 super_constructors
.add
(gp
)
191 var initname
= once
("init".to_symbol
)
192 if sc
.has_global_property_by_name
(initname
) then
193 var gp
= sc
.get_property_by_name
(initname
)
194 super_inits
.add
(self[gp
])
198 # Collect unassigned attributes
199 var unassigned_attributes
= new Array[MMSrcAttribute]
200 for a
in src_local_properties
do
201 if a
isa MMSrcAttribute then
203 if n
.n_expr
== null then unassigned_attributes
.add
(a
)
207 if not super_constructors
.is_empty
then
208 # Select most specific classes introducing inheritable constructors
209 # Mixin classes are skipped
210 var supers
= new Array[MMLocalClass]
211 for gp
in super_constructors
do
212 var sc
= gp
.local_class
213 if supers
.has
(sc
) then continue
214 if not sc
.global
.is_mixin
then
218 supers
= che
.order
.select_smallests
(supers
)
220 # A mixin class can only have 0 or 1 most specific non-mixin superclass
221 var superclass
: nullable MMLocalClass = null # This most specific non-mixin superclass (if any)
223 if supers
.length
> 1 then
224 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(", ")}.")
226 else if supers
.length
== 1 then
227 superclass
= supers
.first
230 for gp
in super_constructors
do
231 # Inherit constructors : the one of the non-mixin super class or all from the all mixin super-classes
232 if superclass
== null or gp
.local_class
== superclass
then
233 make_visible_an_inherited_global_property
(gp
)
236 global
.mixin_of
= superclass
.as(not null).global
# FIXME Dear! this should break!
238 # v.error(nodes.first, "Error, constructor required in {self} since no anonimous init found in {sc}.")
240 # unassigned attributes, then implicit consructors are generated
241 var p
= new MMImplicitInit(self, unassigned_attributes
, super_inits
.to_a
)
242 add_src_local_property
(v
, p
)
243 #print("Create implicit init {p} in {self} from {super_inits.join(", ")} + {unassigned_attributes.length} args")
247 # Add a source property
248 # Register it to the class and attach it to global property
249 private fun add_src_local_property
(v
: PropertyBuilderVisitor, prop
: MMLocalProperty)
251 var pname
= prop
.name
252 # Check double definition in the same class
253 if src_local_properties
.has_key
(pname
) then
254 v
.error
(prop
.node
, "Error: A property {pname} is already defined in class {name}.")
257 src_local_properties
[pname
] = prop
259 # Intro or redefinition ?
260 if has_global_property_by_name
(pname
) then
261 var g
= get_property_by_name
(pname
)
262 prop
.inherit_global
(g
)
265 if not prop
.is_global_set
then
267 prop
.global
.is_init
= prop
.is_init
272 redef class MMLocalProperty
273 private fun accept_property_visitor
(v
: AbsSyntaxVisitor)
278 redef class MMImplicitInit
279 redef readable var _super_init
: nullable MMLocalProperty = null
280 redef fun accept_property_visitor
(v
)
282 var base
: nullable MMLocalProperty = null
283 for p
in super_inits
do
284 if p
.signature
.arity
> 0 then
288 v
.error
(null, "Error: explicit constructor needed in {local_class} since both super-constructor {base.full_name} and {p.full_name} have paramters")
295 var params
= new Array[MMType]
297 var sig
= base
.signature
298 for i
in [0..sig
.arity
[ do
302 for a
in unassigned_attributes
do
303 var sig
= a
.signature
304 if sig
== null then return # Broken attribute definition
305 params
.add
(sig
.return_type
.as(not null))
307 signature
= new MMSignature(params
, null, local_class
.get_type
)
312 # Concrete NIT class specialization relation
315 redef readable var _local_class
: MMLocalClass
317 init(c
: MMLocalClass)
323 ###############################################################################
325 # A pass visitor for syntax analysis.
326 # * Build the classes and attach them to global classes
327 # * Collect generic formal parameters.
328 private class ClassBuilderVisitor
329 super AbsSyntaxVisitor
330 # Current class arity
331 readable writable var _local_class_arity
: Int = 0
333 # Current class formal parameters
334 readable writable var _formals
: nullable Map[Symbol, MMTypeFormalParameter]
336 redef fun visit
(n
) do n
.accept_class_builder
(self)
340 # Another pass visitor for syntax analysis.
341 # * Build ancertors (with only class informations not the type one)
342 private class ClassSpecializationBuilderVisitor
343 super AbsSyntaxVisitor
344 redef fun visit
(n
) do n
.accept_class_specialization_builder
(self)
348 # Another pass visitor for syntax analysis.
349 # * Compute types in ancestors
350 private class ClassAncestorBuilder
351 super AbsSyntaxVisitor
352 redef fun visit
(n
) do n
.accept_class_ancestor_builder
(self)
356 # Another pass visitor for syntax analysis.
357 # * Checks classes in regard to superclasses
358 private class ClassVerifierVisitor
359 super AbsSyntaxVisitor
360 redef fun visit
(n
) do n
.accept_class_verifier
(self)
365 # Another pass visitor for syntax analysis.
366 # * Build propertie names
367 # * Build local properties and attache them to global properties
368 # * Attach bound to formal types
369 private class PropertyBuilderVisitor
370 super AbsSyntaxVisitor
371 redef fun visit
(n
) do n
.accept_property_builder
(self)
375 # Another pass pass visitor for syntax analysis.
376 # * Check property conformance
377 private class PropertyVerifierVisitor
378 super AbsSyntaxVisitor
380 # The signature currently build
381 readable writable var _signature_builder
: SignatureBuilder
383 redef fun visit
(n
) do n
.accept_property_verifier
(self)
388 _signature_builder
= new SignatureBuilder
392 # Information about a signature currently build
393 private class SignatureBuilder
394 # Current visited parameter types
395 readable writable var _params
: Array[AParam] = new Array[AParam]
397 # Visited parameters without type information added
398 readable writable var _untyped_params
: Array[AParam] = new Array[AParam]
400 # Position of the current star parameter
401 readable writable var _vararg_rank
: Int = -1
403 # Current closure declarations
404 readable writable var _closure_decls
: Array[AClosureDecl] = new Array[AClosureDecl]
406 # True is a problen occured durring building
407 readable writable var _has_error_occured
: Bool = false
410 readable writable var _signature
: nullable MMSignature = null
413 ###############################################################################
416 private fun accept_class_builder
(v
: ClassBuilderVisitor) do accept_abs_syntax_visitor
(v
)
417 private fun accept_class_specialization_builder
(v
: ClassSpecializationBuilderVisitor) do accept_abs_syntax_visitor
(v
)
418 private fun accept_class_ancestor_builder
(v
: ClassAncestorBuilder) do accept_abs_syntax_visitor
(v
)
419 private fun accept_class_verifier
(v
: ClassVerifierVisitor) do accept_abs_syntax_visitor
(v
)
420 private fun accept_property_builder
(v
: PropertyBuilderVisitor) do accept_abs_syntax_visitor
(v
)
421 private fun accept_property_verifier
(v
: PropertyVerifierVisitor) do accept_abs_syntax_visitor
(v
)
425 # Import supermodules and compute visibility
426 fun import_super_modules
(tc
: ToolContext, mod
: MMSrcModule)
428 # Import super-modules
429 var module_names_to_import
= new Array[Symbol]
430 var module_visibility
= new HashMap[Symbol, Int]
431 var no_import
: nullable AImport = null
432 for i
in n_imports
do
433 var n
= i
.module_name
435 module_names_to_import
.add
(n
)
436 module_visibility
[n
] = i
.visibility_level
441 if no_import
!= null then
442 if not module_names_to_import
.is_empty
then
443 tc
.error
(no_import
.location
, "Error: Top modules cannot import other modules.")
445 else if module_names_to_import
.is_empty
then
446 var stdname
= once
"standard".to_symbol
447 module_names_to_import
.add
(stdname
)
448 module_visibility
[stdname
] = 1
451 mod
.import_supers_modules
(module_names_to_import
)
453 for mname
in module_names_to_import
do
454 var level
= module_visibility
[mname
]
455 var m
= tc
.get_module
(mname
, mod
)
456 mod
.add_super_module
(m
, level
)
461 redef class AModuledecl
462 redef fun accept_class_builder
(v
)
464 if n_id
.to_symbol
!= v
.mmmodule
.name
then
465 v
.error
(n_id
, "Error: Module name missmatch between {v.mmmodule.name} and {n_id.to_symbol}")
471 # Imported module name (or null)
472 fun module_name
: nullable Symbol is abstract
474 # Visibility level (intrude/public/private)
475 fun visibility_level
: Int is abstract
477 redef class AStdImport
478 redef fun module_name
480 return n_id
.to_symbol
482 redef fun visibility_level
484 return n_visibility
.level
487 redef class ANoImport
488 redef fun module_name
494 redef class AVisibility
496 fun level
: Int is abstract
498 redef class APublicVisibility
499 redef fun level
do return 1
501 redef class AProtectedVisibility
502 redef fun level
do return 2
504 redef class APrivateVisibility
505 redef fun level
do return 3
507 redef class AIntrudeVisibility
508 redef fun level
do return 0
512 redef class AClassdef
513 redef fun local_class
: MMSrcLocalClass do return _local_class
.as(not null)
514 var _local_class
: nullable MMSrcLocalClass
517 fun name
: Symbol is abstract
519 # Number of formal parameters
520 fun arity
: Int do return 0
522 # Visibility of the class
523 fun visibility_level
: Int do return 1
525 redef fun accept_class_builder
(v
)
527 var local_class
: MMSrcLocalClass
529 var local_classes
= mod
.src_local_classes
530 if (local_classes
.has_key
(name
)) then
531 local_class
= local_classes
[name
]
532 _local_class
= local_class
533 if self isa AStdClassdef then
534 # If we are not a special implicit class then rant
535 v
.error
(self, "Error: A class {name} is already defined at line {local_class.node.location.line_start}.")
538 # Add the new node after the last node
539 var n
= local_class
.node
540 while n
.next_node
!= null do n
= n
.next_node
543 local_class
= new MMSrcLocalClass(mod
, name
, self, arity
)
544 _local_class
= local_class
545 local_classes
[name
] = local_class
546 if not mod
.has_global_class_named
(name
) then
547 build_class_introduction
(v
)
549 var glob
= mod
.global_class_named
(name
)
550 build_class_refinement
(v
, glob
)
554 v
.local_class_arity
= 0
555 v
.formals
= local_class
.formal_dict
564 fun build_class_introduction
(v
: AbsSyntaxVisitor)
566 local_class
.new_global
567 var glob
= local_class
.global
569 glob
.visibility_level
= visibility_level
570 if self isa AStdClassdef then
571 if n_kwredef
!= null then
572 v
.error
(self, "Redef error: No class {name} is imported. Remove the redef keyword to define a new class.")
575 glob
.is_interface
= n_classkind
.is_interface
576 glob
.is_abstract
= n_classkind
.is_abstract
577 glob
.is_enum
= n_classkind
.is_enum
581 fun build_class_refinement
(v
: AbsSyntaxVisitor, glob
: MMGlobalClass)
583 local_class
.set_global
(glob
)
585 glob
.check_visibility
(v
, self, v
.mmmodule
)
586 if self isa AStdClassdef and n_kwredef
== null then
587 v
.error
(self, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
591 if glob
.intro
.arity
!= _local_class
.arity
then
592 v
.error
(self, "Redef error: Formal parameter arity missmatch; got {_local_class.arity}, expected {glob.intro.arity}.")
595 if self isa AStdClassdef and (not glob
.is_interface
and n_classkind
.is_interface
or
596 not glob
.is_abstract
and n_classkind
.is_abstract
or
597 not glob
.is_enum
and n_classkind
.is_enum
)
599 v
.error
(self, "Redef error: cannot change kind of class {name}.")
603 redef fun accept_class_verifier
(v
)
606 var glob
= _local_class
.global
607 for c
in _local_class
.cshe
.direct_greaters
do
609 if glob
.is_interface
then
611 v
.error
(self, "Special error: Interface {name} try to specialise enum class {c.name}.")
612 else if not cg
.is_interface
then
613 v
.error
(self, "Special error: Interface {name} try to specialise class {c.name}.")
615 else if glob
.is_enum
then
616 if not cg
.is_interface
and not cg
.is_enum
then
617 v
.error
(self, "Special error: Enum class {name} try to specialise class {c.name}.")
621 v
.error
(self, "Special error: Class {name} try to specialise enum class {c.name}.")
628 redef fun accept_abs_syntax_visitor
(v
)
630 v
.local_class
= _local_class
636 redef class AClasskind
637 fun is_interface
: Bool do return false
638 fun is_enum
: Bool do return false
639 fun is_abstract
: Bool do return false
642 redef class AInterfaceClasskind
643 redef fun is_interface
do return true
645 redef class AEnumClasskind
646 redef fun is_enum
do return true
648 redef class AAbstractClasskind
649 redef fun is_abstract
do return true
652 redef class AStdClassdef
655 return n_id
.to_symbol
659 return n_formaldefs
.length
662 redef fun visibility_level
664 return n_visibility
.level
668 redef class AMainClassdef
671 return once
"Sys".to_symbol
675 redef class ATopClassdef
678 return once
"Object".to_symbol
682 redef class AFormaldef
683 # The associated formal generic parameter (MM entity)
684 var _formal
: nullable MMTypeFormalParameter
686 redef fun accept_class_builder
(v
)
688 var name
= n_id
.to_symbol
689 var formal_type
= new MMTypeFormalParameter(name
, v
.local_class_arity
, v
.local_class
)
690 _formal
= formal_type
691 v
.local_class_arity
= v
.local_class_arity
+ 1
692 v
.local_class
.register_formal
(formal_type
)
693 v
.formals
[name
] = formal_type
697 redef fun accept_class_verifier
(v
)
700 var c
= v
.local_class
701 var o
= c
.global
.intro
703 if n_type
== null then
704 _formal
.bound
= v
.mmmodule
.type_any
.as_nullable
706 var stype
= n_type
.get_stype
(v
)
707 if stype
== null then return
708 _formal
.bound
= stype
711 var ob
= o
.get_formal
(_formal
.position
).bound
.for_module
(v
.mmmodule
)
712 if n_type
== null then
715 var stype
= n_type
.get_stype
(v
)
716 if stype
== null then return
717 _formal
.bound
= stype
718 if _formal
.bound
!= ob
then
719 v
.error
(self, "Redef error: Cannot change formal parameter type of class {c}; got {_formal.bound}, expected {ob}.")
726 redef class ASuperclass
727 readable var _ancestor
: nullable MMSrcAncestor
729 redef fun accept_class_specialization_builder
(v
)
732 var c
= n_type
.get_local_class
(v
)
733 if c
== null then return
734 var ancestor
= new MMSrcAncestor(c
)
736 v
.local_class
.add_direct_parent
(ancestor
)
739 redef fun accept_class_ancestor_builder
(v
)
742 _ancestor
.stype
= n_type
.get_unchecked_stype
(v
)
743 _ancestor
.inheriter
= v
.local_class
.get_type
746 redef fun accept_class_verifier
(v
)
749 n_type
.check_conform
(v
)
754 # Process and check properties of the property.
755 # * Distinguish inits and methods
756 # * Inherit or check visibility.
757 # * Check redef errors.
758 # * Check forbiden attribute definitions.
759 # * Check signature conformance.
760 private fun process_and_check
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
762 if prop
.global
.intro
== prop
then
763 do_and_check_intro
(v
, prop
, has_redef
, visibility_level
)
765 do_and_check_redef
(v
, prop
, has_redef
, visibility_level
)
769 # The part of process_and_check when prop is an introduction
770 private fun do_and_check_intro
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
772 var glob
= prop
.global
773 var gbc
= prop
.local_class
.global
774 if v
.local_class
.global
.visibility_level
>= 3 then
775 # Method of private classes are private
778 glob
.visibility_level
= visibility_level
780 v
.error
(self, "Error: No property {prop.local_class}::{prop} is inherited. Remove the redef keyword to define a new property.")
782 if glob
.is_attribute
then
783 if gbc
.is_interface
then
784 v
.error
(self, "Error: Attempt to define attribute {prop} in the interface {prop.local_class}.")
785 else if gbc
.is_enum
then
786 v
.error
(self, "Error: Attempt to define attribute {prop} in the enum class {prop.local_class}.")
788 else if glob
.is_init
then
789 if gbc
.is_interface
then
790 v
.error
(self, "Error: Attempt to define a constructor {prop} in the class {prop.local_class}.")
791 else if gbc
.is_enum
then
792 v
.error
(self, "Error: Attempt to define a constructor {prop} in the enum {prop.local_class}.")
795 if prop
.signature
== null then
797 var supers
= prop
.local_class
.super_methods_named
(prop
.name
)
798 inherit_signature
(v
, prop
, supers
)
800 if prop
.signature
!= null or v
.signature_builder
.has_error_occured
then
802 else if not v
.signature_builder
.untyped_params
.is_empty
then
803 v
.error
(v
.signature_builder
.untyped_params
.first
, "Error: Untyped parameter.")
805 prop
.signature
= new MMSignature(new Array[MMType], null, v
.local_class
.get_type
)
806 for clos
in v
.signature_builder
.closure_decls
do
807 prop
.signature
.closures
.add
(clos
.variable
.closure
)
813 private fun inherit_signature
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, supers
: Array[MMLocalProperty])
815 var s
= prop
.signature
817 var isig
= ip
.signature
.adaptation_to
(v
.local_class
.get_type
)
820 if v
.signature_builder
.params
.length
!= isig
.arity
then
823 for p
in v
.signature_builder
.params
do
824 var t
= isig
[p
.position
]
826 if p
.position
== isig
.vararg_rank
then
838 # The part of process_and_check when prop is a redefinition
839 private fun do_and_check_redef
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
841 var is_init
= self isa AConcreteInitPropdef
842 var glob
= prop
.global
844 if not has_redef
then
845 v
.error
(self, "Redef error: {prop.local_class}::{prop} is an inherited property. To redefine it, add the redef keyword.")
848 if glob
.is_init
and not is_init
then
849 v
.error
(self, "Redef error: A method {prop.local_class}::{prop} cannot redefine a constructor.")
850 else if not glob
.is_init
and is_init
then
851 v
.error
(self, "Redef error: A constructor {prop.local_class}::{prop} cannot redefine a method.")
854 var s
= prop
.signature
855 #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}"
856 for i
in prop
.prhe
.direct_greaters
do
857 var ip
= i
.local_class
[prop
.global
]
858 var isig
= i
.signature
859 if isig
== null then break # previous signature is invalid
860 isig
= isig
.adaptation_to
(v
.local_class
.get_type
)
863 #print "{prop.full_name} inherits signature from {ip.full_name}"
864 if v
.signature_builder
.params
.length
!= isig
.arity
then
865 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
868 if v
.signature_builder
.closure_decls
.length
!= isig
.closures
.length
then
869 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} closure(s).")
872 for p
in v
.signature_builder
.params
do
873 var t
= isig
[p
.position
]
875 if p
.position
== isig
.vararg_rank
then
885 var nberr
= v
.tc
.error_count
886 #print "Check {prop.local_class}::{prop}{s} vs {ip.local_class}::{ip}{isig}"
887 #print "s={s.object_id} isig={isig.object_id} isigorig={i.signature.object_id}"
889 #print "orig signature: {i.signature.recv} . {i.signature}"
890 #print "inh signature: {isig.recv} . {isig}"
891 #print "redef signature: {s.recv} . {s}"
893 if s
.arity
!= isig
.arity
then
894 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
896 for j
in [0..s
.arity
[ do
897 if s
[j
] != isig
[j
] then
898 v
.error
(self, "Redef error: Expected {isig[j]} (as in {ip.local_class}::{ip}), got {s[j]} in {prop.local_class}::{prop}.")
903 var srt
= s
.return_type
904 var isrt
= isig
.return_type
905 if srt
== null and isrt
!= null then
906 v
.error
(self, "Redef error: The procedure {prop.local_class}::{prop} redefines the function {ip.local_class}::{ip}.")
907 else if srt
!= null and isrt
== null then
908 v
.error
(self, "Redef error: The function {prop.local_class}::{prop} redefines the procedure {ip.local_class}::{ip}.")
909 else if srt
!= null and isrt
!= null and not srt
< isrt
then
910 v
.error
(self, "Redef error: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
911 else if not s
< isig
and nberr
== v
.tc
.error_count
then
912 # Systematic fallback for conformance check
913 v
.error
(self, "Redef error: Incompatible redefinition of {ip.local_class}::{ip} with {prop.local_class}::{prop}")
914 else if srt
!= null and isrt
!= null and srt
!= isrt
and prop
isa MMAttribute then
916 v
.warning
(self, "Redef warning: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
920 if visibility_level
!= 1 and glob
.visibility_level
!= visibility_level
then
921 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefinition cannot change visibility.")
923 glob
.check_visibility
(v
, self, v
.mmmodule
, true)
927 redef class AAttrPropdef
928 redef readable var _readmethod
: nullable MMSrcMethod
929 redef readable var _writemethod
: nullable MMSrcMethod
930 var _prop
: nullable MMSrcAttribute
931 redef fun prop
do return _prop
.as(not null)
933 redef fun accept_property_builder
(v
)
938 name
= n_id
.to_symbol
940 name
= ("@" + n_id2
.text
).to_symbol
942 var lc
= v
.local_class
943 var prop
= new MMSrcAttribute(name
, lc
, self)
945 v
.local_class
.add_src_local_property
(v
, prop
)
947 if n_readable
!= null or n_id
== null then
949 name
= n_id
.text
.substring_from
(1).to_symbol
951 name
= n_id2
.to_symbol
953 var readmethod
= new MMReadImplementationMethod(name
, lc
, self)
954 _readmethod
= readmethod
955 v
.local_class
.add_src_local_property
(v
, readmethod
)
957 if n_writable
!= null or n_id
== null then
959 name
= (n_id
.text
.substring_from
(1) + "=").to_symbol
961 name
= (n_id2
.text
+ "=").to_symbol
963 var writemethod
= new MMWriteImplementationMethod(name
, lc
, self)
964 _writemethod
= writemethod
965 v
.local_class
.add_src_local_property
(v
, writemethod
)
969 redef fun accept_property_verifier
(v
)
973 if n_type
!= null then
974 var t0
= n_type
.get_stype
(v
)
975 if t0
!= null then t
= t0
else return
977 v
.error
(self, "Not yet implemented: Attribute definition {prop.local_class}::{prop} requires an explicit type.")
982 var signature
= new MMSignature(new Array[MMType], t
, v
.local_class
.get_type
)
983 prop
.signature
= signature
984 var visibility_level
= n_visibility
.level
985 process_and_check
(v
, prop
, n_id
!= null and n_kwredef
!= null, visibility_level
)
986 if n_readable
!= null or n_id
== null then
987 var m
= _readmethod
.as(not null)
988 m
.signature
= signature
989 process_and_check
(v
, m
, (n_readable
!= null and n_readable
.n_kwredef
!= null) or (n_id
== null and n_kwredef
!= null), visibility_level
)
990 n_type
.check_visibility
(v
, m
)
992 if n_writable
!= null or n_id
== null then
993 var m
= _writemethod
.as(not null)
994 m
.signature
= new MMSignature(new Array[MMType].with_items
(t
), null, v
.local_class
.get_type
)
995 var vl
= visibility_level
997 if n_writable
== null then vl
= 3 else vl
= n_writable
.n_visibility
.level
# write accessor has a specific visibility
999 process_and_check
(v
, m
, n_writable
!= null and n_writable
.n_kwredef
!= null, vl
)
1000 n_type
.check_visibility
(v
, m
)
1004 redef fun accept_abs_syntax_visitor
(v
)
1006 v
.local_property
= _prop
1008 v
.local_property
= null
1012 redef class AMethPropdef
1013 # Name of the method
1014 readable var _name
: nullable Symbol
1016 var _method
: nullable MMMethSrcMethod
1017 redef fun method
do return _method
.as(not null)
1019 redef fun accept_property_builder
(v
)
1023 if n_methid
== null then
1024 if self isa AConcreteInitPropdef then
1025 name
= once
"init".to_symbol
1027 name
= once
"main".to_symbol
1030 name
= n_methid
.name
.as(not null)
1031 # FIXME: Add the 'unary' keyword
1032 if n_methid
.name
== (once
"-".to_symbol
) then
1033 var ns
= n_signature
1034 if ns
!= null and ns
.n_params
.length
== 0 then
1035 name
= once
"unary -".to_symbol
1040 var prop
= new MMMethSrcMethod(name
, v
.local_class
, self)
1042 v
.local_class
.add_src_local_property
(v
, prop
)
1045 redef fun accept_property_verifier
(v
)
1047 v
.signature_builder
= new SignatureBuilder
1050 if v
.signature_builder
.has_error_occured
then return
1052 if v
.signature_builder
.signature
== null then
1053 #_method.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
1055 method
.signature
= v
.signature_builder
.signature
.as(not null)
1057 var visibility_level
= 1
1058 if n_visibility
!= null and n_visibility
.level
> 1 then
1059 visibility_level
= n_visibility
.level
1061 process_and_check
(v
, method
, n_kwredef
!= null, visibility_level
)
1062 if n_signature
!= null then n_signature
.check_visibility
(v
, method
)
1065 redef fun accept_abs_syntax_visitor
(v
)
1067 v
.local_property
= _method
1069 v
.local_property
= null
1073 redef class AMainMethPropdef
1074 redef fun process_and_check
(v
, prop
, has_redef
, visibility_level
)
1076 prop
.global
.visibility_level
= visibility_level
1077 prop
.signature
= new MMSignature(new Array[MMType], null, v
.local_class
.get_type
)
1078 # Disable all checks for main
1082 redef class AExternMethPropdef
1083 redef fun accept_property_verifier
(v
)
1085 super # Compute signature
1087 if n_extern
!= null then
1088 ename
= n_extern
.text
1089 ename
= ename
.substring
(1, ename
.length-2
)
1091 ename
= "{method.mmmodule.name}_{method.local_class.name}_{method.local_class.name}_{method.name}_{method.signature.arity}"
1093 method
.extern_name
= ename
1097 redef class ATypePropdef
1098 redef fun prop
do return _prop
.as(not null)
1099 var _prop
: nullable MMSrcTypeProperty
1101 redef fun accept_property_builder
(v
)
1104 var name
= n_id
.to_symbol
1105 var prop
= new MMSrcTypeProperty(name
, v
.local_class
, self)
1107 v
.local_class
.add_src_local_property
(v
, prop
)
1110 redef fun accept_property_verifier
(v
)
1113 var signature
= new MMSignature(new Array[MMType], n_type
.get_stype
(v
), v
.local_class
.get_type
)
1114 prop
.signature
= signature
1115 var visibility_level
= n_visibility
.level
1116 process_and_check
(v
, prop
, n_kwredef
!= null, visibility_level
)
1119 redef fun accept_abs_syntax_visitor
(v
)
1121 v
.local_property
= _prop
1123 v
.local_property
= null
1127 # Visitor used to build a full method name from multiple tokens
1128 private class MethidAccumulator
1130 readable var _name
: Buffer = new Buffer
1134 _name
.append
(n
.text
)
1143 readable var _name
: nullable Symbol
1145 redef fun accept_property_builder
(v
)
1147 var accumulator
= new MethidAccumulator
1148 accumulator
.enter_visit
(self)
1149 _name
= accumulator
.name
.to_s
.to_symbol
1154 redef class ASignature
1155 redef fun accept_property_verifier
(v
)
1158 if v
.signature_builder
.has_error_occured
then
1160 else if not v
.signature_builder
.untyped_params
.is_empty
then
1161 if v
.signature_builder
.untyped_params
.first
!= v
.signature_builder
.params
.first
or n_type
!= null then
1162 v
.error
(v
.signature_builder
.untyped_params
.first
, "Syntax error: untyped parameter.")
1165 else if not v
.signature_builder
.params
.is_empty
or n_type
!= null then
1166 var pars
= new Array[MMType]
1167 for p
in v
.signature_builder
.params
do
1168 pars
.add
(p
.stype
.as(not null))
1170 var ret
: nullable MMType = null
1171 if n_type
!= null then
1172 ret
= n_type
.get_stype
(v
)
1174 v
.signature_builder
.has_error_occured
= true
1178 v
.signature_builder
.signature
= new MMSignature(pars
, ret
, v
.local_class
.get_type
)
1179 if v
.signature_builder
.vararg_rank
>= 0 then
1180 v
.signature_builder
.signature
.vararg_rank
= v
.signature_builder
.vararg_rank
1182 for clos
in v
.signature_builder
.closure_decls
do
1183 v
.signature_builder
.signature
.closures
.add
(clos
.variable
.closure
)
1188 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1189 fun check_visibility
(v
: AbsSyntaxVisitor, p
: MMLocalProperty)
1191 if p
.global
.visibility_level
>= 3 then return
1192 for n
in n_params
do
1193 if n
.n_type
!= null then n
.n_type
.check_visibility
(v
, p
)
1195 if n_type
!= null then n_type
.check_visibility
(v
, p
)
1200 redef readable var _position
: Int = 0
1202 redef fun variable
: ParamVariable do return _variable
.as(not null)
1203 var _variable
: nullable ParamVariable
1205 # The type of the parameter in signature
1206 readable writable var _stype
: nullable MMType
1208 redef fun accept_property_verifier
(v
)
1211 _position
= v
.signature_builder
.params
.length
1212 _variable
= new ParamVariable(n_id
.to_symbol
, n_id
)
1213 v
.signature_builder
.params
.add
(self)
1214 v
.signature_builder
.untyped_params
.add
(self)
1215 if n_type
!= null then
1216 var stype
= n_type
.get_stype
(v
)
1217 if stype
== null then
1218 v
.signature_builder
.has_error_occured
= true
1221 for p
in v
.signature_builder
.untyped_params
do
1224 if v
.signature_builder
.vararg_rank
== -1 then
1225 v
.signature_builder
.vararg_rank
= p
.position
1227 v
.error
(self, "Error: A vararg parameter is already defined.")
1229 stype
= v
.type_array
(stype
)
1231 p
.variable
.stype
= stype
1233 v
.signature_builder
.untyped_params
.clear
1237 fun is_vararg
: Bool do return n_dotdotdot
!= null
1240 redef class AClosureDecl
1241 redef readable var _position
: Int = 0
1243 redef fun variable
: ClosureVariable do return _variable
.as(not null)
1244 var _variable
: nullable ClosureVariable
1246 redef fun accept_property_verifier
(v
)
1248 var old_signature_builder
= v
.signature_builder
1249 v
.signature_builder
= new SignatureBuilder
1251 if v
.signature_builder
.has_error_occured
then
1254 var sig
= v
.signature_builder
.signature
1256 sig
= new MMSignature(new Array[MMType], null, v
.local_class
.get_type
)
1258 if sig
.return_type
!= null and n_kwbreak
!= null then
1259 v
.error
(self, "Syntax Error: A break block cannot have a return value.")
1262 # Add the finalizer to the closure signature
1263 var finalize_sig
= new MMSignature(new Array[MMType], null, v
.mmmodule
.type_any
) # FIXME should be no receiver
1264 var finalizer_clos
= new MMClosure(once
("break".to_symbol
), finalize_sig
, false, true)
1265 sig
.closures
.add
(finalizer_clos
)
1267 var name
= n_id
.to_symbol
1268 var clos
= new MMClosure(name
, sig
, n_kwbreak
!= null, n_expr
!= null)
1269 for c
in old_signature_builder
.closure_decls
do
1270 if c
.n_id
.to_symbol
== name
then
1271 v
.error
(n_id
, "A closure '!{name}' already defined at {c.n_id.location.relative_to(n_id.location)}.")
1275 v
.signature_builder
= old_signature_builder
1276 _position
= old_signature_builder
.closure_decls
.length
1277 old_signature_builder
.closure_decls
.add
(self)
1278 _variable
= new ClosureVariable(n_id
.to_symbol
, n_id
, clos
)
1283 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1284 private fun check_visibility
(v
: AbsSyntaxVisitor, p
: MMLocalProperty)
1286 if p
.global
.visibility_level
>= 3 then return
1287 var t
= get_stype
(v
)
1288 if t
== null then return
1289 var bc
= t
.local_class
1290 if bc
.global
.visibility_level
>= 3 then
1291 v
.error
(self, "Access error: Class {bc} is private and cannot be used in the signature of the non-private property {p}.")
1294 n
.check_visibility
(v
, p
)
1300 redef fun accept_class_builder
(v
) do end
1301 redef fun accept_property_builder
(v
) do end
1302 redef fun accept_property_verifier
(v
) do end