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 # A mixin class can be build the same way that one of its superclasses
157 readable attr _is_mixin
: Bool = false
159 # Introduce or inherit default constructors
160 private meth process_default_constructors
(v
: PropertyBuilderVisitor)
162 # Is there already a constructor ?
163 for gp
in global_properties
do
165 # Return if explicit constructor in the class
166 if gp
.intro
.local_class
== self then return
170 # Collect visible constructors in super stateful classes
171 var super_constructors
= new ArraySet[MMGlobalProperty]
172 for sc
in che
.direct_greaters
do
173 if sc
.global
.is_universal
or sc
.global
.is_interface
then continue
174 for gp
in sc
.global_properties
do
175 if not gp
.is_init
then continue
176 super_constructors
.add
(gp
)
180 if not super_constructors
.is_empty
then
181 # Select most specific classes introducing inheritable constructors
182 # Mixin classes are skipped
183 var supers
= new Array[MMLocalClass]
184 for gp
in super_constructors
do
185 var sc
= gp
.local_class
186 if supers
.has
(sc
) then continue
187 assert sc
isa MMSrcLocalClass
188 if not sc
.is_mixin
then
192 supers
= che
.order
.select_smallests
(supers
)
194 # A mixin class can only have 0 or 1 most specific non-mixin superclass
195 var superclass
: MMLocalClass = null # This most specific non-mixin superclass (if any)
197 if supers
.length
> 1 then
198 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(", ")}.")
200 else if supers
.length
== 1 then
201 superclass
= supers
.first
204 for gp
in super_constructors
do
205 # Inherit constructors : the one of the non-mixin super class or all from the all mixin super-classes
206 if superclass
== null or gp
.local_class
== superclass
then
207 make_visible_an_inherited_global_property
(gp
)
214 # Add a source property
215 # Register it to the class and attach it to global property
216 private meth add_src_local_property
(v
: PropertyBuilderVisitor, prop
: MMLocalProperty)
218 var pname
= prop
.name
219 # Check double definition in the same class
220 if src_local_properties
.has_key
(pname
) then
221 v
.error
(prop
.node
, "Error: A property {pname} is already defined in class {name}.")
224 src_local_properties
[pname
] = prop
226 # Intro or redefinition ?
227 if has_global_property_by_name
(pname
) then
228 var globs
= properties_by_name
[pname
]
229 if globs
.length
> 1 then
230 v
.error
(prop
.node
, "Name error: {self} inherits {globs.length} global properties named {pname}.")
233 prop
.inherit_global
(g
)
236 if prop
.global
== null then
238 prop
.global
.is_init
= prop
.is_init
243 redef class MMLocalProperty
244 private meth accept_property_visitor
(v
: AbsSyntaxVisitor)
248 # Concrete NIT class specialization relation
251 # The related AST node
252 readable attr _node
: ASuperclass
253 redef readable attr _local_class
: MMLocalClass
255 init(n
: ASuperclass, c
: MMLocalClass)
262 ###############################################################################
264 # A pass visitor for syntax analysis.
265 # * Build the classes and attach them to global classes
266 # * Collect generic formal parameters.
267 private class ClassBuilderVisitor
268 special AbsSyntaxVisitor
269 # Current class arity
270 readable writable attr _local_class_arity
: Int
272 # Current class formal parameters
273 readable writable attr _formals
: Map[Symbol, MMTypeFormalParameter]
275 redef meth visit
(n
) do n
.accept_class_builder
(self)
279 # Another pass visitor for syntax analysis.
280 # * Build ancertors (with only class informations not the type one)
281 private class ClassSpecializationBuilderVisitor
282 special AbsSyntaxVisitor
283 redef meth visit
(n
) do n
.accept_class_specialization_builder
(self)
287 # Another pass visitor for syntax analysis.
288 # * Compute types in ancestors
289 private class ClassAncestorBuilder
290 special AbsSyntaxVisitor
291 redef meth visit
(n
) do n
.accept_class_ancestor_builder
(self)
295 # Another pass visitor for syntax analysis.
296 # * Checks classes in regard to superclasses
297 private class ClassVerifierVisitor
298 special AbsSyntaxVisitor
299 redef meth visit
(n
) do n
.accept_class_verifier
(self)
304 # Another pass visitor for syntax analysis.
305 # * Build propertie names
306 # * Build local properties and attache them to global properties
307 # * Attach bound to formal types
308 private class PropertyBuilderVisitor
309 special AbsSyntaxVisitor
310 redef meth visit
(n
) do n
.accept_property_builder
(self)
314 # Another pass pass visitor for syntax analysis.
315 # * Check property conformance
316 private class PropertyVerifierVisitor
317 special AbsSyntaxVisitor
318 # Current visited parameter types
319 readable writable attr _params
: Array[PParam]
321 # Visited parameters without type information added
322 readable writable attr _untyped_params
: Array[PParam]
324 # Position of the current star parameter
325 readable writable attr _vararg_rank
: Int
328 readable writable attr _signature
: MMSignature
330 redef meth visit
(n
) do n
.accept_property_verifier
(self)
335 ###############################################################################
338 private meth accept_class_builder
(v
: ClassBuilderVisitor) do accept_abs_syntax_visitor
(v
)
339 private meth accept_class_specialization_builder
(v
: ClassSpecializationBuilderVisitor) do accept_abs_syntax_visitor
(v
)
340 private meth accept_class_ancestor_builder
(v
: ClassAncestorBuilder) do accept_abs_syntax_visitor
(v
)
341 private meth accept_class_verifier
(v
: ClassVerifierVisitor) do accept_abs_syntax_visitor
(v
)
342 private meth accept_property_builder
(v
: PropertyBuilderVisitor) do accept_abs_syntax_visitor
(v
)
343 private meth accept_property_verifier
(v
: PropertyVerifierVisitor) do accept_abs_syntax_visitor
(v
)
347 # Import supermodules and compute visibility
348 meth import_super_modules
(tc
: ToolContext, mod
: MMSrcModule)
350 # Import super-modules
351 var module_names_to_import
= new Array[Symbol]
352 var module_visibility
= new HashMap[Symbol, Int]
353 var no_import
: PImport = null
354 for i
in n_imports
do
355 var n
= i
.module_name
357 module_names_to_import
.add
(n
)
358 module_visibility
[n
] = i
.visibility_level
363 if no_import
!= null then
364 if not module_names_to_import
.is_empty
then
365 tc
.error
("{no_import.locate}: Error: Top modules cannot import other modules.")
367 else if module_names_to_import
.is_empty
then
368 var stdname
= once
"standard".to_symbol
369 module_names_to_import
.add
(stdname
)
370 module_visibility
[stdname
] = 1
373 mod
.import_supers_modules
(module_names_to_import
)
375 for mname
in module_names_to_import
do
376 var level
= module_visibility
[mname
]
377 var m
= tc
.get_module
(mname
, mod
)
378 mod
.add_super_module
(m
, level
)
383 redef class APackagedecl
384 redef meth accept_class_builder
(v
)
386 if n_id
.to_symbol
!= v
.module.name
then
387 v
.error
(n_id
, "Error: Package name missmatch between {v.module.name} and {n_id.to_symbol}")
393 # Imported module name (or null)
394 meth module_name
: Symbol is abstract
396 # Visibility level (intrude/public/private)
397 meth visibility_level
: Int is abstract
400 redef meth module_name
402 return n_id
.to_symbol
404 redef meth visibility_level
406 return n_visibility
.level
409 redef class ANoImport
410 redef meth module_name
416 redef class PVisibility
418 meth level
: Int is abstract
420 redef class APublicVisibility
421 redef meth level
do return 1
423 redef class AProtectedVisibility
424 redef meth level
do return 2
426 redef class APrivateVisibility
427 redef meth level
do return 3
429 redef class AIntrudeVisibility
430 redef meth level
do return 0
434 redef class PClassdef
435 redef readable attr _local_class
: MMSrcLocalClass
438 meth name
: Symbol is abstract
440 # Number of formal parameters
441 meth arity
: Int do return 0
443 # Visibility of the class
444 meth visibility_level
: Int do return 1
446 redef meth accept_class_builder
(v
)
448 var local_class
: MMSrcLocalClass
450 var local_classes
= mod
.src_local_classes
451 if (local_classes
.has_key
(name
)) then
452 local_class
= local_classes
[name
]
453 if self isa AClassdef then
454 # If we are not a special implicit class then rant
455 v
.error
(self, "Error: A class {name} is already defined at line {local_class.nodes.first.first_token.line}.")
458 local_class
.nodes
.add
(self)
460 local_class
= new MMSrcLocalClass(name
, self, arity
)
461 mod
.add_local_class
(local_class
)
462 local_classes
[name
] = local_class
463 var g
= mod
.global_class_named
(name
)
466 local_class
.new_global
467 g
= local_class
.global
469 local_class
.set_global
(g
)
473 _local_class
= local_class
474 v
.local_class_arity
= 0
475 v
.formals
= new HashMap[Symbol, MMTypeFormalParameter]
481 _local_class
.formal_dict
= v
.formals
485 redef meth accept_abs_syntax_visitor
(v
)
487 v
.local_class
= _local_class
493 redef class PClasskind
494 meth is_interface
: Bool do return false
495 meth is_universal
: Bool do return false
496 meth is_abstract
: Bool do return false
499 redef class AInterfaceClasskind
500 redef meth is_interface
do return true
502 redef class AUniversalClasskind
503 redef meth is_universal
do return true
505 redef class AAbstractClasskind
506 redef meth is_abstract
do return true
509 redef class AClassdef
512 return n_id
.to_symbol
516 return n_formaldefs
.length
518 redef meth accept_class_verifier
(v
)
521 var glob
= _local_class
.global
522 if glob
.intro
== _local_class
then
524 glob
.visibility_level
= visibility_level
525 glob
.is_interface
= n_classkind
.is_interface
526 glob
.is_abstract
= n_classkind
.is_abstract
527 glob
.is_universal
= n_classkind
.is_universal
528 if n_kwredef
!= null then
529 v
.error
(self, "Redef error: No class {name} is imported. Remove the redef keyword to define a new class.")
532 for c
in _local_class
.cshe
.direct_greaters
do
534 if glob
.is_interface
then
535 if cg
.is_universal
then
536 v
.error
(self, "Special error: Interface {name} try to specialise universal class {c.name}.")
537 else if not cg
.is_interface
then
538 v
.error
(self, "Special error: Interface {name} try to specialise class {c.name}.")
540 else if glob
.is_universal
then
541 if not cg
.is_interface
and not cg
.is_universal
then
542 v
.error
(self, "Special error: Universal class {name} try to specialise class {c.name}.")
545 if cg
.is_universal
then
546 v
.error
(self, "Special error: Class {name} try to specialise universal class {c.name}.")
556 glob
.check_visibility
(v
, self, v
.module)
557 if n_kwredef
== null then
558 v
.error
(self, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
562 if glob
.intro
.arity
!= _local_class
.arity
then
563 v
.error
(self, "Redef error: Formal parameter arity missmatch; got {_local_class.arity}, expected {glob.intro.arity}.")
567 not glob
.is_interface
and n_classkind
.is_interface
or
568 not glob
.is_abstract
and n_classkind
.is_abstract
or
569 not glob
.is_universal
and n_classkind
.is_universal
571 v
.error
(self, "Redef error: cannot change kind of class {name}.")
575 redef meth visibility_level
577 return n_visibility
.level
581 redef class AMainClassdef
584 return once
"Sys".to_symbol
588 redef class ATopClassdef
591 return once
"Object".to_symbol
595 class MMSrcTypeFormalParameter
596 special MMTypeFormalParameter
597 # The associated node
598 readable attr _node
: AFormaldef
600 init(name
: Symbol, pos
: Int, local_class
: MMLocalClass, n
: AFormaldef)
602 super(name
, pos
, local_class
)
607 redef class AFormaldef
608 # The associated formal generic parameter (MM entity)
609 attr _formal
: MMSrcTypeFormalParameter
611 redef meth accept_class_builder
(v
)
613 var name
= n_id
.to_symbol
614 var formal_type
= new MMSrcTypeFormalParameter(name
, v
.local_class_arity
, v
.local_class
, self)
615 _formal
= formal_type
616 v
.local_class_arity
= v
.local_class_arity
+ 1
617 v
.local_class
.register_formal
(formal_type
)
618 v
.formals
[name
] = formal_type
622 redef meth accept_class_verifier
(v
)
625 var c
= v
.local_class
626 var o
= c
.global
.intro
628 if n_type
== null then
629 _formal
.bound
= v
.module.type_any
631 _formal
.bound
= n_type
.get_stype
(v
)
634 var ob
= o
.get_formal
(_formal
.position
).bound
.for_module
(v
.module)
635 if n_type
== null then
638 _formal
.bound
= n_type
.get_stype
(v
)
639 if _formal
.bound
!= ob
then
640 v
.error
(self, "Redef error: Cannot change formal parameter type of class {c}; got {_formal.bound}, expected {ob}.")
647 redef class ASuperclass
648 readable attr _ancestor
: MMSrcAncestor
650 redef meth accept_class_specialization_builder
(v
)
653 var c
= n_type
.get_local_class
(v
)
654 var ancestor
= new MMSrcAncestor(self, c
)
656 v
.local_class
.add_direct_parent
(ancestor
)
659 redef meth accept_class_ancestor_builder
(v
)
662 _ancestor
.stype
= n_type
.get_unchecked_stype
(v
)
663 _ancestor
.inheriter
= v
.local_class
.get_type
666 redef meth accept_class_verifier
(v
)
669 n_type
.check_conform
(v
)
674 # Process and check properties of the property.
675 # * Distinguish inits and methods
676 # * Inherit or check visibility.
677 # * Check redef errors.
678 # * Check forbiden attribute definitions.
679 # * Check signature conformance.
680 private meth process_and_check
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
682 if prop
.global
.intro
== prop
then
683 do_and_check_intro
(v
, prop
, has_redef
, visibility_level
)
685 do_and_check_redef
(v
, prop
, has_redef
, visibility_level
)
689 # The part of process_and_check when prop is an introduction
690 private meth do_and_check_intro
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
692 var glob
= prop
.global
693 var gbc
= prop
.local_class
.global
694 if v
.local_class
.global
.visibility_level
>= 3 then
695 # Method of private classes are private
698 glob
.visibility_level
= visibility_level
700 v
.error
(self, "Error: No property {prop.local_class}::{prop} is inherited. Remove the redef keyword to define a new property.")
702 if glob
.is_attribute
then
703 if gbc
.is_interface
then
704 v
.error
(self, "Error: Attempt to define attribute {prop} in the interface {prop.local_class}.")
705 else if gbc
.is_universal
then
706 v
.error
(self, "Error: Attempt to define attribute {prop} in the universal class {prop.local_class}.")
708 else if glob
.is_init
then
709 if gbc
.is_interface
then
710 v
.error
(self, "Error: Attempt to define a constructor {prop} in the class {prop.local_class}.")
711 else if gbc
.is_universal
then
712 v
.error
(self, "Error: Attempt to define a constructor {prop} in the universal {prop.local_class}.")
715 if prop
.signature
== null then
717 var supers
= prop
.local_class
.super_methods_named
(prop
.name
)
718 inherit_signature
(v
, prop
, supers
)
720 if prop
.signature
!= null then
722 else if not v
.untyped_params
.is_empty
then
723 v
.error
(v
.untyped_params
.first
, "Error: Untyped parameter.")
725 prop
.signature
= new MMSignature(new Array[MMType], null, v
.local_class
.get_type
)
730 private meth inherit_signature
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, supers
: Array[MMLocalProperty])
732 var s
= prop
.signature
734 var isig
= ip
.signature
.adaptation_to
(v
.local_class
.get_type
)
737 if v
.params
.length
!= isig
.arity
then
738 #prop.node.printl("v.params.length {v.params.length} != isig.arity {isig.arity} ; {prop.full_name} vs {ip.full_name}")
742 var t
= isig
[p
.position
]
744 if p
.position
== isig
.vararg_rank
then
756 # The part of process_and_check when prop is a redefinition
757 private meth do_and_check_redef
(v
: PropertyVerifierVisitor, prop
: MMLocalProperty, has_redef
: Bool, visibility_level
: Int)
759 var is_init
= self isa AConcreteInitPropdef
760 var glob
= prop
.global
762 if not has_redef
then
763 v
.error
(self, "Redef error: {prop.local_class}::{prop} is an inherited property. To redefine it, add the redef keyword.")
766 if glob
.is_init
and not is_init
then
767 v
.error
(self, "Redef error: A method {prop.local_class}::{prop} cannot redefine a constructor.")
768 else if not glob
.is_init
and is_init
then
769 v
.error
(self, "Redef error: A constructor {prop.local_class}::{prop} cannot redefine a method.")
772 var s
= prop
.signature
773 #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}"
774 for i
in prop
.prhe
.direct_greaters
do
775 var ip
= i
.local_class
[prop
.global
]
776 var isig
= i
.signature
.adaptation_to
(v
.local_class
.get_type
)
779 #print "{prop.full_name} inherits signature from {ip.full_name}"
780 if v
.params
.length
!= isig
.arity
then
781 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
785 var t
= isig
[p
.position
]
787 if p
.position
== isig
.vararg_rank
then
796 #print "orig signature: {i.signature.recv} . {i.signature}"
797 #print "inh signature: {isig.recv} . {isig}"
798 #print "redef signature: {s.recv} . {s}"
800 if glob
.is_init
and i
.local_class
.global
!= prop
.local_class
.global
then
801 # Do not check signature
802 else if s
.arity
!= isig
.arity
then
803 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
805 for i
in [0..s
.arity
[ do
806 if s
[i
] != isig
[i
] then
807 v
.error
(self, "Redef error: Expected {isig[i]} (as in {ip.local_class}::{ip}), got {s[i]} in {prop.local_class}::{prop}.")
812 var srt
= s
.return_type
813 var isrt
= isig
.return_type
814 if srt
== null and isrt
!= null then
815 v
.error
(self, "Redef error: The procedure {prop.local_class}::{prop} redefines the function {ip.local_class}::{ip}.")
816 else if srt
!= null and isrt
== null then
817 v
.error
(self, "Redef error: The function {prop.local_class}::{prop} redefines the procedure {ip.local_class}::{ip}.")
818 else if srt
!= null and isrt
!= null and not srt
< isrt
then
819 v
.error
(self, "Redef error: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
820 else if srt
!= null and isrt
!= null and srt
!= isrt
and prop
isa MMAttribute then
822 v
.warning
(self, "Redef warning: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
826 if visibility_level
!= 1 and glob
.visibility_level
!= visibility_level
then
827 v
.error
(self, "Redef error: {prop.local_class}::{prop} redefinition cannot change visibility.")
829 glob
.check_visibility
(v
, self, v
.module, true)
833 redef class AAttrPropdef
834 redef readable attr _readmethod
: MMSrcMethod
835 redef readable attr _writemethod
: MMSrcMethod
836 redef readable attr _prop
: MMSrcAttribute
838 redef meth accept_property_builder
(v
)
841 var name
= n_id
.to_symbol
842 var prop
= new MMSrcAttribute(name
, v
.local_class
, self)
844 v
.local_class
.add_src_local_property
(v
, prop
)
846 if n_readable
!= null then
847 name
= n_id
.text
.substring_from
(1).to_symbol
848 _readmethod
= new MMReadImplementationMethod(name
, v
.local_class
, self)
849 v
.local_class
.add_src_local_property
(v
, _readmethod
)
851 if n_writable
!= null then
852 name
= (n_id
.text
.substring_from
(1) + "=").to_symbol
853 _writemethod
= new MMWriteImplementationMethod(name
, v
.local_class
, self)
854 v
.local_class
.add_src_local_property
(v
, _writemethod
)
858 redef meth accept_property_verifier
(v
)
862 if n_type
!= null then
863 t
= n_type
.get_stype
(v
)
865 v
.error
(self, "Not yet implemented: Attribute definition {_prop.local_class}::{_prop} requires an explicit type.")
869 var signature
= new MMSignature(new Array[MMType], t
, v
.local_class
.get_type
)
870 _prop
.signature
= signature
871 var visibility_level
= n_visibility
.level
872 process_and_check
(v
, _prop
, n_kwredef
!= null, visibility_level
)
873 if n_readable
!= null then
874 _readmethod
.signature
= signature
875 process_and_check
(v
, _readmethod
, n_readable
.n_kwredef
!= null, visibility_level
)
876 n_type
.check_visibility
(v
, _readmethod
)
878 if n_writable
!= null then
879 _writemethod
.signature
= new MMSignature(new Array[MMType].with
(t
), null, v
.local_class
.get_type
)
880 process_and_check
(v
, _writemethod
, n_writable
.n_kwredef
!= null, visibility_level
)
881 n_type
.check_visibility
(v
, _writemethod
)
885 redef meth accept_abs_syntax_visitor
(v
)
887 v
.local_property
= prop
889 v
.local_property
= null
893 redef class AMethPropdef
895 readable attr _name
: Symbol
897 redef readable attr _method
: MMMethSrcMethod
899 redef meth accept_property_builder
(v
)
902 if n_methid
== null then
903 if self isa AConcreteInitPropdef then
904 _name
= once
"init".to_symbol
906 _name
= once
"main".to_symbol
909 _name
= n_methid
.name
910 # FIXME: Add the 'unary' keyword
911 if n_methid
.name
== (once
"-".to_symbol
) then
913 if ns
isa ASignature and ns
.n_params
.length
== 0 then
914 _name
= once
"unary -".to_symbol
918 var prop
= new MMMethSrcMethod(_name
, v
.local_class
, self)
920 v
.local_class
.add_src_local_property
(v
, prop
)
923 redef meth accept_property_verifier
(v
)
925 v
.params
= new Array[PParam]
926 v
.untyped_params
= new Array[PParam]
932 if v
.signature
== null then
933 #_method.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
935 _method
.signature
= v
.signature
937 var visibility_level
= 1
938 if n_visibility
!= null and n_visibility
.level
> 1 then
939 visibility_level
= n_visibility
.level
941 process_and_check
(v
, _method
, n_kwredef
!= null, visibility_level
)
942 if n_signature
!= null then n_signature
.check_visibility
(v
, _method
)
945 redef meth accept_abs_syntax_visitor
(v
)
947 v
.local_property
= method
949 v
.local_property
= null
953 redef class AMainMethPropdef
954 redef meth process_and_check
(v
, prop
, has_redef
, visibility_level
)
956 prop
.global
.visibility_level
= visibility_level
957 prop
.signature
= new MMSignature(new Array[MMType], null, v
.local_class
.get_type
)
958 # Disable all checks for main
962 redef class ATypePropdef
963 redef readable attr _prop
: MMSrcTypeProperty
965 redef meth accept_property_builder
(v
)
968 var name
= n_id
.to_symbol
969 var prop
= new MMSrcTypeProperty(name
, v
.local_class
, self)
971 v
.local_class
.add_src_local_property
(v
, prop
)
974 redef meth accept_property_verifier
(v
)
977 var signature
= new MMSignature(new Array[MMType], n_type
.get_stype
(v
), 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
)
983 redef meth accept_abs_syntax_visitor
(v
)
985 v
.local_property
= prop
987 v
.local_property
= null
991 # Visitor used to build a full method name from multiple tokens
992 private class MethidAccumulator
994 readable attr _name
: String
1012 readable attr _name
: Symbol
1014 redef meth accept_property_builder
(v
)
1016 var accumulator
= new MethidAccumulator
1017 accumulator
.visit
(self)
1018 _name
= accumulator
.name
.to_symbol
1023 redef class PSignature
1024 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1025 meth check_visibility
(v
: AbsSyntaxVisitor, p
: MMLocalProperty) is abstract
1028 redef class ASignature
1029 redef meth accept_property_verifier
(v
)
1032 if not v
.untyped_params
.is_empty
then
1033 if v
.untyped_params
.first
!= v
.params
.first
or n_type
!= null then
1034 v
.error
(v
.untyped_params
.first
, "Syntax error: untyped parameter.")
1037 else if not v
.params
.is_empty
or n_type
!= null then
1038 var pars
= new Array[MMType]
1039 for p
in v
.params
do
1042 var ret
: MMType = null
1043 if n_type
!= null then
1044 ret
= n_type
.get_stype
(v
)
1046 v
.signature
= new MMSignature(pars
, ret
, v
.local_class
.get_type
)
1047 if v
.vararg_rank
>= 0 then
1048 v
.signature
.vararg_rank
= v
.vararg_rank
1053 redef meth check_visibility
(v
, p
)
1055 if p
.global
.visibility_level
>= 3 then return
1056 for n
in n_params
do
1057 if n
.n_type
!= null then n
.n_type
.check_visibility
(v
, p
)
1059 if n_type
!= null then n_type
.check_visibility
(v
, p
)
1064 redef readable attr _position
: Int
1066 redef readable attr _variable
: Variable
1068 # The type of the parameter in signature
1069 readable writable attr _stype
: MMType
1071 redef meth accept_property_verifier
(v
)
1074 _position
= v
.params
.length
1075 _variable
= new Variable(n_id
.to_symbol
, self)
1077 v
.untyped_params
.add
(self)
1078 if n_type
!= null then
1079 var stype
= n_type
.get_stype
(v
)
1080 for p
in v
.untyped_params
do
1083 if v
.vararg_rank
== -1 then
1084 v
.vararg_rank
= p
.position
1086 v
.error
(self, "Error: A vararg parameter is already defined.")
1088 stype
= v
.type_array
(stype
)
1090 p
.variable
.stype
= stype
1092 v
.untyped_params
.clear
1096 meth is_vararg
: Bool is abstract
1100 redef meth is_vararg
do return n_dotdotdot
!= null
1104 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1105 private meth check_visibility
(v
: AbsSyntaxVisitor, p
: MMLocalProperty) is abstract
1109 redef meth check_visibility
(v
, p
)
1111 if p
.global
.visibility_level
>= 3 then return
1112 var t
= get_stype
(v
)
1113 if t
== null then return
1114 var bc
= t
.local_class
1115 if bc
== null then return
1116 if bc
.global
.visibility_level
>= 3 then
1117 v
.error
(self, "Access error: Class {bc} is private and cannot be used in the signature of the non-private property {p}.")
1120 n
.check_visibility
(v
, p
)
1126 redef meth accept_class_builder
(v
) do end
1127 redef meth accept_property_builder
(v
) do end
1128 redef meth accept_property_verifier
(v
) do end