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 meth 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 meth 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)
46 if tc
.error_count
> 0 then exit
(1)
48 # Import unrefined local classes and attach them to global classes
51 # Resolve classes in super clauses
52 var mmbv1
= new ClassSpecializationBuilderVisitor(tc
, self)
54 if tc
.error_count
> 0 then exit
(1)
56 # Compute specialization relation
57 for c
in local_classes
do
58 if visibility_for
(c
.global
.intro
.module) < c
.global
.visibility_level
then
61 c
.compute_super_classes
64 # Class that we will process now are those in the hierarchy
65 # Its mean all the visible classes and their super-classes
66 # Note that leaves invisible classes are not in the 'classes' set
67 var classes
= class_specialization_hierarchy
.to_a
69 # Prepare class list to process the following in a right order
70 var sorter
= once
new CSHSorter
73 # Compute class ancestors types
74 var mmbv1b
= new ClassAncestorBuilder(tc
, self)
76 c
.accept_class_visitor
(mmbv1b
)
79 if tc
.error_count
> 0 then exit
(1)
81 # Check class conformity
82 var mmbv1b
= new ClassVerifierVisitor(tc
, self)
84 c
.accept_class_visitor
(mmbv1b
)
86 if tc
.error_count
> 0 then exit
(1)
88 # Property inhritance and introduction
89 var mmbv2
= new PropertyBuilderVisitor(tc
, self)
91 # Inherit global properties
92 c
.inherit_global_properties
94 # Global property introduction and redefinition
95 c
.accept_class_visitor
(mmbv2
)
97 # Default and inherited constructor if needed
98 if c
isa MMSrcLocalClass and c
.global
.intro
== c
and not c
.global
.is_universal
and not c
.global
.is_interface
then
99 c
.process_default_constructors
(mmbv2
)
102 # Note that inherited unredefined property are processed on demand latter
104 if tc
.error_count
> 0 then exit
(1)
106 # Property signature analysis and inheritance conformance
107 var mmbv3
= new PropertyVerifierVisitor(tc
, self)
109 c
.accept_properties_visitor
(mmbv3
)
112 # Check inherited local properties
114 for g
in c
.global_properties
do
115 if visibility_for
(g
.intro
.module) < g
.visibility_level
then continue
120 if tc
.error_count
> 0 then exit
(1)
124 redef class MMLocalClass
125 # Accept a class visitor (on class nodes)
126 private meth accept_class_visitor
(v
: AbsSyntaxVisitor)
130 # Accept a class visitor (on class properties)
131 private meth accept_properties_visitor
(v
: AbsSyntaxVisitor)
136 redef class MMSrcLocalClass
137 redef meth accept_class_visitor
(v
)
144 # Accept a class visitor (on class properties)
145 redef meth accept_properties_visitor
(v
)
151 for p
in src_local_properties
do
152 p
.accept_property_visitor
(v
)
156 # Introduce or inherit default constructors
157 private meth process_default_constructors
(v
: PropertyBuilderVisitor)
159 # Is there already a constructor ?
160 for gp
in global_properties
do
162 # Return if explicit constructor in the class
163 if gp
.intro
.local_class
== self then return
167 # Collect visible constructors in super stateful classes
168 var super_inits
= new ArraySet[MMLocalProperty]
169 var super_constructors
= new ArraySet[MMGlobalProperty]
170 for sc
in che
.direct_greaters
do
171 if sc
.global
.is_universal
or sc
.global
.is_interface
then continue
172 for gp
in sc
.global_properties
do
173 if not gp
.is_init
then continue
174 super_constructors
.add
(gp
)
176 var gp
= sc
.get_property_by_name
(once
("init".to_symbol
))
178 super_inits
.add
(self[gp
])
182 # Collect unassigned attributes
183 var unassigned_attributes
= new Array[MMSrcAttribute]
184 for a
in src_local_properties
do
185 if a
isa MMSrcAttribute then
187 assert n
isa AAttrPropdef
188 if n
.n_expr
== null then unassigned_attributes
.add
(a
)
192 if not super_constructors
.is_empty
then
193 # Select most specific classes introducing inheritable constructors
194 # Mixin classes are skipped
195 var supers
= new Array[MMLocalClass]
196 for gp
in super_constructors
do
197 var sc
= gp
.local_class
198 if supers
.has
(sc
) then continue
199 if not sc
.global
.is_mixin
then
203 supers
= che
.order
.select_smallests
(supers
)
205 # A mixin class can only have 0 or 1 most specific non-mixin superclass
206 var superclass
: MMLocalClass = null # This most specific non-mixin superclass (if any)
208 if supers
.length
> 1 then
209 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(", ")}.")
211 else if supers
.length
== 1 then
212 superclass
= supers
.first
215 for gp
in super_constructors
do
216 # Inherit constructors : the one of the non-mixin super class or all from the all mixin super-classes
217 if superclass
== null or gp
.local_class
== superclass
then
218 make_visible_an_inherited_global_property
(gp
)
221 global
.mixin_of
= superclass
.global
223 # v.error(nodes.first, "Error, constructor required in {self} since no anonimous init found in {sc}.")
225 # unassigned attributes, then implicit consructors are generated
226 var p
= new MMImplicitInit(self, unassigned_attributes
, super_inits
.to_a
)
227 add_src_local_property
(v
, p
)
228 #print("Create implicit init {p} in {self} from {super_inits.join(", ")} + {unassigned_attributes.length} args")
232 # Add a source property
233 # Register it to the class and attach it to global property
234 private meth add_src_local_property
(v
: PropertyBuilderVisitor, prop
: MMLocalProperty)
236 var pname
= prop
.name
237 # Check double definition in the same class
238 if src_local_properties
.has_key
(pname
) then
239 v
.error
(prop
.node
, "Error: A property {pname} is already defined in class {name}.")
242 src_local_properties
[pname
] = prop
244 # Intro or redefinition ?
245 if has_global_property_by_name
(pname
) then
246 var globs
= properties_by_name
[pname
]
247 if globs
.length
> 1 then
248 v
.error
(prop
.node
, "Name error: {self} inherits {globs.length} global properties named {pname}.")
251 prop
.inherit_global
(g
)
254 if prop
.global
== null then
256 prop
.global
.is_init
= prop
.is_init
261 redef class MMLocalProperty
262 private meth accept_property_visitor
(v
: AbsSyntaxVisitor)
267 redef class MMImplicitInit
268 readable attr _super_init
: MMLocalProperty = null
269 redef meth accept_property_visitor
(v
)
271 var base
: MMLocalProperty = null
272 for p
in super_inits
do
273 if p
.signature
.arity
> 0 then
277 v
.error
(null, "Error: explicit constructor needed in {local_class} since both super-constructor {base.full_name} and {p.full_name} have paramters")
284 var params
= new Array[MMType]
286 var sig
= base
.signature
287 for i
in [0..sig
.arity
[ do
291 for a
in unassigned_attributes
do
292 params
.add
(a
.signature
.return_type
)
294 signature
= new MMSignature(params
, null, local_class
.get_type
)
299 # Concrete NIT class specialization relation
302 # The related AST node
303 readable attr _node
: ASuperclass
304 redef readable attr _local_class
: MMLocalClass
306 init(n
: ASuperclass, c
: MMLocalClass)
313 ###############################################################################
315 # A pass visitor for syntax analysis.
316 # * Build the classes and attach them to global classes
317 # * Collect generic formal parameters.
318 private class ClassBuilderVisitor
319 special AbsSyntaxVisitor
320 # Current class arity
321 readable writable attr _local_class_arity
: Int
323 # Current class formal parameters
324 readable writable attr _formals
: Map[Symbol, MMTypeFormalParameter]
326 redef meth visit
(n
) do n
.accept_class_builder
(self)
330 # Another pass visitor for syntax analysis.
331 # * Build ancertors (with only class informations not the type one)
332 private class ClassSpecializationBuilderVisitor
333 special AbsSyntaxVisitor
334 redef meth visit
(n
) do n
.accept_class_specialization_builder
(self)
338 # Another pass visitor for syntax analysis.
339 # * Compute types in ancestors
340 private class ClassAncestorBuilder
341 special AbsSyntaxVisitor
342 redef meth visit
(n
) do n
.accept_class_ancestor_builder
(self)
346 # Another pass visitor for syntax analysis.
347 # * Checks classes in regard to superclasses
348 private class ClassVerifierVisitor
349 special AbsSyntaxVisitor
350 redef meth visit
(n
) do n
.accept_class_verifier
(self)
355 # Another pass visitor for syntax analysis.
356 # * Build propertie names
357 # * Build local properties and attache them to global properties
358 # * Attach bound to formal types
359 private class PropertyBuilderVisitor
360 special AbsSyntaxVisitor
361 redef meth visit
(n
) do n
.accept_property_builder
(self)
365 # Another pass pass visitor for syntax analysis.
366 # * Check property conformance
367 private class PropertyVerifierVisitor
368 special AbsSyntaxVisitor
370 # The signature currently build
371 readable writable attr _signature_builder
: SignatureBuilder
373 redef meth visit
(n
) do n
.accept_property_verifier
(self)
378 _signature_builder
= new SignatureBuilder
382 # Information about a signature currently build
383 private class SignatureBuilder
384 # Current visited parameter types
385 readable writable attr _params
: Array[PParam] = new Array[PParam]
387 # Visited parameters without type information added
388 readable writable attr _untyped_params
: Array[PParam] = new Array[PParam]
390 # Position of the current star parameter
391 readable writable attr _vararg_rank
: Int = -1
394 readable writable attr _signature
: MMSignature = null
397 ###############################################################################
400 private meth accept_class_builder
(v
: ClassBuilderVisitor) do accept_abs_syntax_visitor
(v
)
401 private meth accept_class_specialization_builder
(v
: ClassSpecializationBuilderVisitor) do accept_abs_syntax_visitor
(v
)
402 private meth accept_class_ancestor_builder
(v
: ClassAncestorBuilder) do accept_abs_syntax_visitor
(v
)
403 private meth accept_class_verifier
(v
: ClassVerifierVisitor) do accept_abs_syntax_visitor
(v
)
404 private meth accept_property_builder
(v
: PropertyBuilderVisitor) do accept_abs_syntax_visitor
(v
)
405 private meth accept_property_verifier
(v
: PropertyVerifierVisitor) do accept_abs_syntax_visitor
(v
)
409 # Import supermodules and compute visibility
410 meth import_super_modules
(tc
: ToolContext, mod
: MMSrcModule)
412 # Import super-modules
413 var module_names_to_import
= new Array[Symbol]
414 var module_visibility
= new HashMap[Symbol, Int]
415 var no_import
: PImport = null
416 for i
in n_imports
do
417 var n
= i
.module_name
419 module_names_to_import
.add
(n
)
420 module_visibility
[n
] = i
.visibility_level
425 if no_import
!= null then
426 if not module_names_to_import
.is_empty
then
427 tc
.error
("{no_import.locate}: Error: Top modules cannot import other modules.")
429 else if module_names_to_import
.is_empty
then
430 var stdname
= once
"standard".to_symbol
431 module_names_to_import
.add
(stdname
)
432 module_visibility
[stdname
] = 1
435 mod
.import_supers_modules
(module_names_to_import
)
437 for mname
in module_names_to_import
do
438 var level
= module_visibility
[mname
]
439 var m
= tc
.get_module
(mname
, mod
)
440 mod
.add_super_module
(m
, level
)
445 redef class APackagedecl
446 redef meth accept_class_builder
(v
)
448 if n_id
.to_symbol
!= v
.module.name
then
449 v
.error
(n_id
, "Error: Package name missmatch between {v.module.name} and {n_id.to_symbol}")
455 # Imported module name (or null)
456 meth module_name
: Symbol is abstract
458 # Visibility level (intrude/public/private)
459 meth visibility_level
: Int is abstract
462 redef meth module_name
464 return n_id
.to_symbol
466 redef meth visibility_level
468 return n_visibility
.level
471 redef class ANoImport
472 redef meth module_name
478 redef class PVisibility
480 meth level
: Int is abstract
482 redef class APublicVisibility
483 redef meth level
do return 1
485 redef class AProtectedVisibility
486 redef meth level
do return 2
488 redef class APrivateVisibility
489 redef meth level
do return 3
491 redef class AIntrudeVisibility
492 redef meth level
do return 0
496 redef class PClassdef
497 redef readable attr _local_class
: MMSrcLocalClass
500 meth name
: Symbol is abstract
502 # Number of formal parameters
503 meth arity
: Int do return 0
505 # Visibility of the class
506 meth visibility_level
: Int do return 1
508 redef meth accept_class_builder
(v
)
510 var local_class
: MMSrcLocalClass
512 var local_classes
= mod
.src_local_classes
513 if (local_classes
.has_key
(name
)) then
514 local_class
= local_classes
[name
]
515 if self isa AClassdef then
516 # If we are not a special implicit class then rant
517 v
.error
(self, "Error: A class {name} is already defined at line {local_class.nodes.first.first_token.line}.")
520 local_class
.nodes
.add
(self)
522 local_class
= new MMSrcLocalClass(name
, self, arity
)
523 mod
.add_local_class
(local_class
)
524 local_classes
[name
] = local_class
525 var g
= mod
.global_class_named
(name
)
528 local_class
.new_global
529 g
= local_class
.global
531 local_class
.set_global
(g
)
535 _local_class
= local_class
536 v
.local_class_arity
= 0
537 v
.formals
= new HashMap[Symbol, MMTypeFormalParameter]
543 _local_class
.formal_dict
= v
.formals
547 redef meth accept_abs_syntax_visitor
(v
)
549 v
.local_class
= _local_class
555 redef class PClasskind
556 meth is_interface
: Bool do return false
557 meth is_universal
: Bool do return false
558 meth is_abstract
: Bool do return false
561 redef class AInterfaceClasskind
562 redef meth is_interface
do return true
564 redef class AUniversalClasskind
565 redef meth is_universal
do return true
567 redef class AAbstractClasskind
568 redef meth is_abstract
do return true
571 redef class AClassdef
574 return n_id
.to_symbol
578 return n_formaldefs
.length
580 redef meth accept_class_verifier
(v
)
583 var glob
= _local_class
.global
584 if glob
.intro
== _local_class
then
586 glob
.visibility_level
= visibility_level
587 glob
.is_interface
= n_classkind
.is_interface
588 glob
.is_abstract
= n_classkind
.is_abstract
589 glob
.is_universal
= n_classkind
.is_universal
590 if n_kwredef
!= null then
591 v
.error
(self, "Redef error: No class {name} is imported. Remove the redef keyword to define a new class.")
594 for c
in _local_class
.cshe
.direct_greaters
do
596 if glob
.is_interface
then
597 if cg
.is_universal
then
598 v
.error
(self, "Special error: Interface {name} try to specialise universal class {c.name}.")
599 else if not cg
.is_interface
then
600 v
.error
(self, "Special error: Interface {name} try to specialise class {c.name}.")
602 else if glob
.is_universal
then
603 if not cg
.is_interface
and not cg
.is_universal
then
604 v
.error
(self, "Special error: Universal class {name} try to specialise class {c.name}.")
607 if cg
.is_universal
then
608 v
.error
(self, "Special error: Class {name} try to specialise universal class {c.name}.")
618 glob
.check_visibility
(v
, self, v
.module)
619 if n_kwredef
== null then
620 v
.error
(self, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
624 if glob
.intro
.arity
!= _local_class
.arity
then
625 v
.error
(self, "Redef error: Formal parameter arity missmatch; got {_local_class.arity}, expected {glob.intro.arity}.")
629 not glob
.is_interface
and n_classkind
.is_interface
or
630 not glob
.is_abstract
and n_classkind
.is_abstract
or
631 not glob
.is_universal
and n_classkind
.is_universal
633 v
.error
(self, "Redef error: cannot change kind of class {name}.")
637 redef meth visibility_level
639 return n_visibility
.level
643 redef class AMainClassdef
646 return once
"Sys".to_symbol
650 redef class ATopClassdef
653 return once
"Object".to_symbol
657 class MMSrcTypeFormalParameter
658 special MMTypeFormalParameter
659 # The associated node
660 readable attr _node
: AFormaldef
662 init(name
: Symbol, pos
: Int, local_class
: MMLocalClass, n
: AFormaldef)
664 super(name
, pos
, local_class
)
669 redef class AFormaldef
670 # The associated formal generic parameter (MM entity)
671 attr _formal
: MMSrcTypeFormalParameter
673 redef meth accept_class_builder
(v
)
675 var name
= n_id
.to_symbol
676 var formal_type
= new MMSrcTypeFormalParameter(name
, v
.local_class_arity
, v
.local_class
, self)
677 _formal
= formal_type
678 v
.local_class_arity
= v
.local_class_arity
+ 1
679 v
.local_class
.register_formal
(formal_type
)
680 v
.formals
[name
] = formal_type
684 redef meth accept_class_verifier
(v
)
687 var c
= v
.local_class
688 var o
= c
.global
.intro
690 if n_type
== null then
691 _formal
.bound
= v
.module.type_any
693 _formal
.bound
= n_type
.get_stype
(v
)
696 var ob
= o
.get_formal
(_formal
.position
).bound
.for_module
(v
.module)
697 if n_type
== null then
700 _formal
.bound
= n_type
.get_stype
(v
)
701 if _formal
.bound
!= ob
then
702 v
.error
(self, "Redef error: Cannot change formal parameter type of class {c}; got {_formal.bound}, expected {ob}.")
709 redef class ASuperclass
710 readable attr _ancestor
: MMSrcAncestor
712 redef meth accept_class_specialization_builder
(v
)
715 var c
= n_type
.get_local_class
(v
)
716 var ancestor
= new MMSrcAncestor(self, c
)
718 v
.local_class
.add_direct_parent
(ancestor
)
721 redef meth accept_class_ancestor_builder
(v
)
724 _ancestor
.stype
= n_type
.get_unchecked_stype
(v
)
725 _ancestor
.inheriter
= v
.local_class
.get_type
728 redef meth accept_class_verifier
(v
)
731 n_type
.check_conform
(v
)
736 # Process and check properties of the property.
737 # * Distinguish inits and methods
738 # * Inherit or check visibility.
739 # * Check redef errors.
740 # * Check forbiden attribute definitions.
741 # * Check signature conformance.
742 private meth process_and_check
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
744 if prop
.global
.intro
== prop
then
745 do_and_check_intro
(v
, prop
, has_redef
, visibility_level
)
747 do_and_check_redef
(v
, prop
, has_redef
, visibility_level
)
751 # The part of process_and_check when prop is an introduction
752 private meth do_and_check_intro
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
754 var glob
= prop
.global
755 var gbc
= prop
.local_class
.global
756 if v
.local_class
.global
.visibility_level
>= 3 then
757 # Method of private classes are private
760 glob
.visibility_level
= visibility_level
762 v
.error
(self, "Error: No property {prop.local_class}::{prop} is inherited. Remove the redef keyword to define a new property.")
764 if glob
.is_attribute
then
765 if gbc
.is_interface
then
766 v
.error
(self, "Error: Attempt to define attribute {prop} in the interface {prop.local_class}.")
767 else if gbc
.is_universal
then
768 v
.error
(self, "Error: Attempt to define attribute {prop} in the universal class {prop.local_class}.")
770 else if glob
.is_init
then
771 if gbc
.is_interface
then
772 v
.error
(self, "Error: Attempt to define a constructor {prop} in the class {prop.local_class}.")
773 else if gbc
.is_universal
then
774 v
.error
(self, "Error: Attempt to define a constructor {prop} in the universal {prop.local_class}.")
777 if prop
.signature
== null then
779 var supers
= prop
.local_class
.super_methods_named
(prop
.name
)
780 inherit_signature
(v
, prop
, supers
)
782 if prop
.signature
!= null then
784 else if not v
.signature_builder
.untyped_params
.is_empty
then
785 v
.error
(v
.signature_builder
.untyped_params
.first
, "Error: Untyped parameter.")
787 prop
.signature
= new MMSignature(new Array[MMType], null, v
.local_class
.get_type
)
792 private meth inherit_signature
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, supers
: Array[MMLocalProperty])
794 var s
= prop
.signature
796 var isig
= ip
.signature
.adaptation_to
(v
.local_class
.get_type
)
799 if v
.signature_builder
.params
.length
!= isig
.arity
then
800 #prop.node.printl("v.params.length {v.params.length} != isig.arity {isig.arity} ; {prop.full_name} vs {ip.full_name}")
803 for p
in v
.signature_builder
.params
do
804 var t
= isig
[p
.position
]
806 if p
.position
== isig
.vararg_rank
then
818 # The part of process_and_check when prop is a redefinition
819 private meth do_and_check_redef
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
821 var is_init
= self isa AConcreteInitPropdef
822 var glob
= prop
.global
824 if not has_redef
then
825 v
.error
(self, "Redef error: {prop.local_class}::{prop} is an inherited property. To redefine it, add the redef keyword.")
828 if glob
.is_init
and not is_init
then
829 v
.error
(self, "Redef error: A method {prop.local_class}::{prop} cannot redefine a constructor.")
830 else if not glob
.is_init
and is_init
then
831 v
.error
(self, "Redef error: A constructor {prop.local_class}::{prop} cannot redefine a method.")
834 var s
= prop
.signature
835 #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}"
836 for i
in prop
.prhe
.direct_greaters
do
837 var ip
= i
.local_class
[prop
.global
]
838 var isig
= i
.signature
.adaptation_to
(v
.local_class
.get_type
)
841 #print "{prop.full_name} inherits signature from {ip.full_name}"
842 if v
.signature_builder
.params
.length
!= isig
.arity
then
843 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
846 for p
in v
.signature_builder
.params
do
847 var t
= isig
[p
.position
]
849 if p
.position
== isig
.vararg_rank
then
858 #print "orig signature: {i.signature.recv} . {i.signature}"
859 #print "inh signature: {isig.recv} . {isig}"
860 #print "redef signature: {s.recv} . {s}"
862 if glob
.is_init
and i
.local_class
.global
!= prop
.local_class
.global
then
863 # Do not check signature
864 else if s
.arity
!= isig
.arity
then
865 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
867 for i
in [0..s
.arity
[ do
868 if s
[i
] != isig
[i
] then
869 v
.error
(self, "Redef error: Expected {isig[i]} (as in {ip.local_class}::{ip}), got {s[i]} in {prop.local_class}::{prop}.")
874 var srt
= s
.return_type
875 var isrt
= isig
.return_type
876 if srt
== null and isrt
!= null then
877 v
.error
(self, "Redef error: The procedure {prop.local_class}::{prop} redefines the function {ip.local_class}::{ip}.")
878 else if srt
!= null and isrt
== null then
879 v
.error
(self, "Redef error: The function {prop.local_class}::{prop} redefines the procedure {ip.local_class}::{ip}.")
880 else if srt
!= null and isrt
!= null and not srt
< isrt
then
881 v
.error
(self, "Redef error: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
882 else if srt
!= null and isrt
!= null and srt
!= isrt
and prop
isa MMAttribute then
884 v
.warning
(self, "Redef warning: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
888 if visibility_level
!= 1 and glob
.visibility_level
!= visibility_level
then
889 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefinition cannot change visibility.")
891 glob
.check_visibility
(v
, self, v
.module, true)
895 redef class AAttrPropdef
896 redef readable attr _readmethod
: MMSrcMethod
897 redef readable attr _writemethod
: MMSrcMethod
898 redef readable attr _prop
: MMSrcAttribute
900 redef meth accept_property_builder
(v
)
903 var name
= n_id
.to_symbol
904 var prop
= new MMSrcAttribute(name
, v
.local_class
, self)
906 v
.local_class
.add_src_local_property
(v
, prop
)
908 if n_readable
!= null then
909 name
= n_id
.text
.substring_from
(1).to_symbol
910 _readmethod
= new MMReadImplementationMethod(name
, v
.local_class
, self)
911 v
.local_class
.add_src_local_property
(v
, _readmethod
)
913 if n_writable
!= null then
914 name
= (n_id
.text
.substring_from
(1) + "=").to_symbol
915 _writemethod
= new MMWriteImplementationMethod(name
, v
.local_class
, self)
916 v
.local_class
.add_src_local_property
(v
, _writemethod
)
920 redef meth accept_property_verifier
(v
)
924 if n_type
!= null then
925 t
= n_type
.get_stype
(v
)
927 v
.error
(self, "Not yet implemented: Attribute definition {_prop.local_class}::{_prop} requires an explicit type.")
931 var signature
= new MMSignature(new Array[MMType], t
, v
.local_class
.get_type
)
932 _prop
.signature
= signature
933 var visibility_level
= n_visibility
.level
934 process_and_check
(v
, _prop
, n_kwredef
!= null, visibility_level
)
935 if n_readable
!= null then
936 _readmethod
.signature
= signature
937 process_and_check
(v
, _readmethod
, n_readable
.n_kwredef
!= null, visibility_level
)
938 n_type
.check_visibility
(v
, _readmethod
)
940 if n_writable
!= null then
941 _writemethod
.signature
= new MMSignature(new Array[MMType].with_items
(t
), null, v
.local_class
.get_type
)
942 process_and_check
(v
, _writemethod
, n_writable
.n_kwredef
!= null, visibility_level
)
943 n_type
.check_visibility
(v
, _writemethod
)
947 redef meth accept_abs_syntax_visitor
(v
)
949 v
.local_property
= prop
951 v
.local_property
= null
955 redef class AMethPropdef
957 readable attr _name
: Symbol
959 redef readable attr _method
: MMMethSrcMethod
961 redef meth accept_property_builder
(v
)
964 if n_methid
== null then
965 if self isa AConcreteInitPropdef then
966 _name
= once
"init".to_symbol
968 _name
= once
"main".to_symbol
971 _name
= n_methid
.name
972 # FIXME: Add the 'unary' keyword
973 if n_methid
.name
== (once
"-".to_symbol
) then
975 if ns
isa ASignature and ns
.n_params
.length
== 0 then
976 _name
= once
"unary -".to_symbol
980 var prop
= new MMMethSrcMethod(_name
, v
.local_class
, self)
982 v
.local_class
.add_src_local_property
(v
, prop
)
985 redef meth accept_property_verifier
(v
)
987 v
.signature_builder
= new SignatureBuilder
990 if v
.signature_builder
.signature
== null then
991 #_method.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
993 _method
.signature
= v
.signature_builder
.signature
995 var visibility_level
= 1
996 if n_visibility
!= null and n_visibility
.level
> 1 then
997 visibility_level
= n_visibility
.level
999 process_and_check
(v
, _method
, n_kwredef
!= null, visibility_level
)
1000 if n_signature
!= null then n_signature
.check_visibility
(v
, _method
)
1003 redef meth accept_abs_syntax_visitor
(v
)
1005 v
.local_property
= method
1007 v
.local_property
= null
1011 redef class AMainMethPropdef
1012 redef meth process_and_check
(v
, prop
, has_redef
, visibility_level
)
1014 prop
.global
.visibility_level
= visibility_level
1015 prop
.signature
= new MMSignature(new Array[MMType], null, v
.local_class
.get_type
)
1016 # Disable all checks for main
1020 redef class ATypePropdef
1021 redef readable attr _prop
: MMSrcTypeProperty
1023 redef meth accept_property_builder
(v
)
1026 var name
= n_id
.to_symbol
1027 var prop
= new MMSrcTypeProperty(name
, v
.local_class
, self)
1029 v
.local_class
.add_src_local_property
(v
, prop
)
1032 redef meth accept_property_verifier
(v
)
1035 var signature
= new MMSignature(new Array[MMType], n_type
.get_stype
(v
), v
.local_class
.get_type
)
1036 _prop
.signature
= signature
1037 var visibility_level
= n_visibility
.level
1038 process_and_check
(v
, _prop
, n_kwredef
!= null, visibility_level
)
1041 redef meth accept_abs_syntax_visitor
(v
)
1043 v
.local_property
= prop
1045 v
.local_property
= null
1049 # Visitor used to build a full method name from multiple tokens
1050 private class MethidAccumulator
1052 readable attr _name
: String
1056 _name
.append
(n
.text
)
1070 readable attr _name
: Symbol
1072 redef meth accept_property_builder
(v
)
1074 var accumulator
= new MethidAccumulator
1075 accumulator
.visit
(self)
1076 _name
= accumulator
.name
.to_symbol
1081 redef class PSignature
1082 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1083 meth check_visibility
(v
: AbsSyntaxVisitor, p
: MMLocalProperty) is abstract
1086 redef class ASignature
1087 redef meth accept_property_verifier
(v
)
1090 if not v
.signature_builder
.untyped_params
.is_empty
then
1091 if v
.signature_builder
.untyped_params
.first
!= v
.signature_builder
.params
.first
or n_type
!= null then
1092 v
.error
(v
.signature_builder
.untyped_params
.first
, "Syntax error: untyped parameter.")
1095 else if not v
.signature_builder
.params
.is_empty
or n_type
!= null then
1096 var pars
= new Array[MMType]
1097 for p
in v
.signature_builder
.params
do
1100 var ret
: MMType = null
1101 if n_type
!= null then
1102 ret
= n_type
.get_stype
(v
)
1104 v
.signature_builder
.signature
= new MMSignature(pars
, ret
, v
.local_class
.get_type
)
1105 if v
.signature_builder
.vararg_rank
>= 0 then
1106 v
.signature_builder
.signature
.vararg_rank
= v
.signature_builder
.vararg_rank
1111 redef meth check_visibility
(v
, p
)
1113 if p
.global
.visibility_level
>= 3 then return
1114 for n
in n_params
do
1115 if n
.n_type
!= null then n
.n_type
.check_visibility
(v
, p
)
1117 if n_type
!= null then n_type
.check_visibility
(v
, p
)
1122 redef readable attr _position
: Int
1124 redef readable attr _variable
: ParamVariable
1126 # The type of the parameter in signature
1127 readable writable attr _stype
: MMType
1129 redef meth accept_property_verifier
(v
)
1132 _position
= v
.signature_builder
.params
.length
1133 _variable
= new ParamVariable(n_id
.to_symbol
, self)
1134 v
.signature_builder
.params
.add
(self)
1135 v
.signature_builder
.untyped_params
.add
(self)
1136 if n_type
!= null then
1137 var stype
= n_type
.get_stype
(v
)
1138 for p
in v
.signature_builder
.untyped_params
do
1141 if v
.signature_builder
.vararg_rank
== -1 then
1142 v
.signature_builder
.vararg_rank
= p
.position
1144 v
.error
(self, "Error: A vararg parameter is already defined.")
1146 stype
= v
.type_array
(stype
)
1148 p
.variable
.stype
= stype
1150 v
.signature_builder
.untyped_params
.clear
1154 meth is_vararg
: Bool is abstract
1158 redef meth is_vararg
do return n_dotdotdot
!= null
1162 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1163 private meth check_visibility
(v
: AbsSyntaxVisitor, p
: MMLocalProperty) is abstract
1167 redef meth check_visibility
(v
, p
)
1169 if p
.global
.visibility_level
>= 3 then return
1170 var t
= get_stype
(v
)
1171 if t
== null then return
1172 var bc
= t
.local_class
1173 if bc
== null then return
1174 if bc
.global
.visibility_level
>= 3 then
1175 v
.error
(self, "Access error: Class {bc} is private and cannot be used in the signature of the non-private property {p}.")
1178 n
.check_visibility
(v
, p
)
1184 redef meth accept_class_builder
(v
) do end
1185 redef meth accept_property_builder
(v
) do end
1186 redef meth accept_property_verifier
(v
) do end