Methods without PNodes.
[nit.git] / src / syntax / mmbuilder.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2008 Jean Privat <jean@pryen.org>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
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
19 #
20 package mmbuilder
21
22 import syntax_base
23
24 # Class specialization hierarchy sorter
25 private class CSHSorter
26 special AbstractSorter[MMLocalClass]
27 redef meth compare(a, b)
28 do
29 return a.cshe.rank <=> b.cshe.rank
30 end
31
32 init do end
33 end
34
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)
39 do
40 # Import global classes
41 import_global_classes
42
43 # Create local classes and attach them to global classes
44 var mmbv = new ClassBuilderVisitor(tc, self)
45 mmbv.visit(node)
46 if tc.error_count > 0 then exit(1)
47
48 # Import unrefined local classes and attach them to global classes
49 import_local_classes
50
51 # Resolve classes in super clauses
52 var mmbv1 = new ClassSpecializationBuilderVisitor(tc, self)
53 mmbv1.visit(node)
54 if tc.error_count > 0 then exit(1)
55
56 # Compute specialization relation
57 for c in local_classes do
58 if visibility_for(c.global.intro.module) < c.global.visibility_level then
59 continue
60 end
61 c.compute_super_classes
62 end
63
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
68
69 # Prepare class list to process the following in a right order
70 var sorter = once new CSHSorter
71 sorter.sort(classes)
72
73 # Compute class ancestors types
74 var mmbv1b = new ClassAncestorBuilder(tc, self)
75 for c in classes do
76 c.accept_class_visitor(mmbv1b)
77 c.compute_ancestors
78 end
79 if tc.error_count > 0 then exit(1)
80
81 # Check class conformity
82 var mmbv1b = new ClassVerifierVisitor(tc, self)
83 for c in classes do
84 c.accept_class_visitor(mmbv1b)
85 end
86 if tc.error_count > 0 then exit(1)
87
88 # Property inhritance and introduction
89 var mmbv2 = new PropertyBuilderVisitor(tc, self)
90 for c in classes do
91 # Inherit global properties
92 c.inherit_global_properties
93
94 # Global property introduction and redefinition
95 c.accept_class_visitor(mmbv2)
96
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)
100 end
101
102 # Note that inherited unredefined property are processed on demand latter
103 end
104 if tc.error_count > 0 then exit(1)
105
106 # Property signature analysis and inheritance conformance
107 var mmbv3 = new PropertyVerifierVisitor(tc, self)
108 for c in classes do
109 c.accept_properties_visitor(mmbv3)
110 end
111
112 # Check inherited local properties
113 for c in classes do
114 for g in c.global_properties do
115 if visibility_for(g.intro.module) < g.visibility_level then continue
116 var p = c[g]
117 end
118 end
119
120 if tc.error_count > 0 then exit(1)
121 end
122 end
123
124 redef class MMLocalClass
125 # Accept a class visitor (on class nodes)
126 private meth accept_class_visitor(v: AbsSyntaxVisitor)
127 do
128 end
129
130 # Accept a class visitor (on class properties)
131 private meth accept_properties_visitor(v: AbsSyntaxVisitor)
132 do
133 end
134 end
135
136 redef class MMSrcLocalClass
137 redef meth accept_class_visitor(v)
138 do
139 for n in nodes do
140 v.visit(n)
141 end
142 end
143
144 # Accept a class visitor (on class properties)
145 redef meth accept_properties_visitor(v)
146 do
147 for n in nodes do
148 v.visit(n)
149 end
150
151 for p in src_local_properties do
152 p.accept_property_visitor(v)
153 end
154 end
155
156 # A mixin class can be build the same way that one of its superclasses
157 readable attr _is_mixin: Bool = false
158
159 # Introduce or inherit default constructors
160 private meth process_default_constructors(v: PropertyBuilderVisitor)
161 do
162 # Is there already a constructor ?
163 for gp in global_properties do
164 if gp.is_init then
165 # Return if explicit constructor in the class
166 if gp.intro.local_class == self then return
167 end
168 end
169
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)
177 end
178 end
179
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
189 supers.add(sc)
190 end
191 end
192 supers = che.order.select_smallests(supers)
193
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)
196
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(", ")}.")
199 return
200 else if supers.length == 1 then
201 superclass = supers.first
202 end
203
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)
208 end
209 end
210 _is_mixin = true
211 end
212 end
213
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)
217 do
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}.")
222 return
223 end
224 src_local_properties[pname] = prop
225
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}.")
231 end
232 var g = globs.first
233 prop.inherit_global(g)
234 end
235
236 if prop.global == null then
237 prop.new_global
238 prop.global.is_init = prop.is_init
239 end
240 end
241 end
242
243 redef class MMLocalProperty
244 private meth accept_property_visitor(v: AbsSyntaxVisitor)
245 do
246 end
247 end
248 # Concrete NIT class specialization relation
249 class MMSrcAncestor
250 special MMAncestor
251 # The related AST node
252 readable attr _node: ASuperclass
253 redef readable attr _local_class: MMLocalClass
254
255 init(n: ASuperclass, c: MMLocalClass)
256 do
257 _node = n
258 _local_class = c
259 end
260 end
261
262 ###############################################################################
263
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
271
272 # Current class formal parameters
273 readable writable attr _formals: Map[Symbol, MMTypeFormalParameter]
274
275 redef meth visit(n) do n.accept_class_builder(self)
276 init(tc, m) do super
277 end
278
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)
284 init(tc, m) do super
285 end
286
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)
292 init(tc, m) do super
293 end
294
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)
300 init(tc, m) do super
301 end
302
303
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)
311 init(tc, m) do super
312 end
313
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]
320
321 # Visited parameters without type information added
322 readable writable attr _untyped_params: Array[PParam]
323
324 # Position of the current star parameter
325 readable writable attr _vararg_rank: Int
326
327 # Current signature
328 readable writable attr _signature: MMSignature
329
330 redef meth visit(n) do n.accept_property_verifier(self)
331
332 init(tc, m) do super
333 end
334
335 ###############################################################################
336
337 redef class PNode
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)
344 end
345
346 redef class AModule
347 # Import supermodules and compute visibility
348 meth import_super_modules(tc: ToolContext, mod: MMSrcModule)
349 do
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
356 if n != null then
357 module_names_to_import.add(n)
358 module_visibility[n] = i.visibility_level
359 else
360 no_import = i
361 end
362 end
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.")
366 end
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
371 end
372
373 mod.import_supers_modules(module_names_to_import)
374
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)
379 end
380 end
381 end
382
383 redef class APackagedecl
384 redef meth accept_class_builder(v)
385 do
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}")
388 end
389 end
390 end
391
392 redef class PImport
393 # Imported module name (or null)
394 meth module_name: Symbol is abstract
395
396 # Visibility level (intrude/public/private)
397 meth visibility_level: Int is abstract
398 end
399 redef class AImport
400 redef meth module_name
401 do
402 return n_id.to_symbol
403 end
404 redef meth visibility_level
405 do
406 return n_visibility.level
407 end
408 end
409 redef class ANoImport
410 redef meth module_name
411 do
412 return null
413 end
414 end
415
416 redef class PVisibility
417 # Visibility level
418 meth level: Int is abstract
419 end
420 redef class APublicVisibility
421 redef meth level do return 1
422 end
423 redef class AProtectedVisibility
424 redef meth level do return 2
425 end
426 redef class APrivateVisibility
427 redef meth level do return 3
428 end
429 redef class AIntrudeVisibility
430 redef meth level do return 0
431 end
432
433
434 redef class PClassdef
435 redef readable attr _local_class: MMSrcLocalClass
436
437 # Name of the class
438 meth name: Symbol is abstract
439
440 # Number of formal parameters
441 meth arity: Int do return 0
442
443 # Visibility of the class
444 meth visibility_level: Int do return 1
445
446 redef meth accept_class_builder(v)
447 do
448 var local_class: MMSrcLocalClass
449 var mod = v.module
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}.")
456 return
457 end
458 local_class.nodes.add(self)
459 else
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)
464 if g == null then
465 # Intro
466 local_class.new_global
467 g = local_class.global
468 else
469 local_class.set_global(g)
470 end
471
472 end
473 _local_class = local_class
474 v.local_class_arity = 0
475 v.formals = new HashMap[Symbol, MMTypeFormalParameter]
476
477 #####
478 super
479 #####
480
481 _local_class.formal_dict = v.formals
482 v.formals = null
483 end
484
485 redef meth accept_abs_syntax_visitor(v)
486 do
487 v.local_class = _local_class
488 super
489 v.local_class = null
490 end
491 end
492
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
497 end
498
499 redef class AInterfaceClasskind
500 redef meth is_interface do return true
501 end
502 redef class AUniversalClasskind
503 redef meth is_universal do return true
504 end
505 redef class AAbstractClasskind
506 redef meth is_abstract do return true
507 end
508
509 redef class AClassdef
510 redef meth name
511 do
512 return n_id.to_symbol
513 end
514 redef meth arity
515 do
516 return n_formaldefs.length
517 end
518 redef meth accept_class_verifier(v)
519 do
520 super
521 var glob = _local_class.global
522 if glob.intro == _local_class then
523 # Intro
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.")
530 end
531
532 for c in _local_class.cshe.direct_greaters do
533 var cg = c.global
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}.")
539 end
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}.")
543 end
544 else
545 if cg.is_universal then
546 v.error(self, "Special error: Class {name} try to specialise universal class {c.name}.")
547 end
548 end
549
550 end
551 return
552 end
553
554 # Redef
555
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.")
559 return
560 end
561
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}.")
564 end
565
566 if
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
570 then
571 v.error(self, "Redef error: cannot change kind of class {name}.")
572 end
573 end
574
575 redef meth visibility_level
576 do
577 return n_visibility.level
578 end
579 end
580
581 redef class AMainClassdef
582 redef meth name
583 do
584 return once "Sys".to_symbol
585 end
586 end
587
588 redef class ATopClassdef
589 redef meth name
590 do
591 return once "Object".to_symbol
592 end
593 end
594
595 class MMSrcTypeFormalParameter
596 special MMTypeFormalParameter
597 # The associated node
598 readable attr _node: AFormaldef
599
600 init(name: Symbol, pos: Int, local_class: MMLocalClass, n: AFormaldef)
601 do
602 super(name, pos, local_class)
603 _node = n
604 end
605 end
606
607 redef class AFormaldef
608 # The associated formal generic parameter (MM entity)
609 attr _formal: MMSrcTypeFormalParameter
610
611 redef meth accept_class_builder(v)
612 do
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
619 super
620 end
621
622 redef meth accept_class_verifier(v)
623 do
624 super
625 var c = v.local_class
626 var o = c.global.intro
627 if c == o then
628 if n_type == null then
629 _formal.bound = v.module.type_any
630 else
631 _formal.bound = n_type.get_stype(v)
632 end
633 else
634 var ob = o.get_formal(_formal.position).bound.for_module(v.module)
635 if n_type == null then
636 _formal.bound = ob
637 else
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}.")
641 end
642 end
643 end
644 end
645 end
646
647 redef class ASuperclass
648 readable attr _ancestor: MMSrcAncestor
649
650 redef meth accept_class_specialization_builder(v)
651 do
652 super
653 var c = n_type.get_local_class(v)
654 var ancestor = new MMSrcAncestor(self, c)
655 _ancestor = ancestor
656 v.local_class.add_direct_parent(ancestor)
657 end
658
659 redef meth accept_class_ancestor_builder(v)
660 do
661 super
662 _ancestor.stype = n_type.get_unchecked_stype(v)
663 _ancestor.inheriter = v.local_class.get_type
664 end
665
666 redef meth accept_class_verifier(v)
667 do
668 super
669 n_type.check_conform(v)
670 end
671 end
672
673 redef class PPropdef
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)
681 do
682 if prop.global.intro == prop then
683 do_and_check_intro(v, prop, has_redef, visibility_level)
684 else
685 do_and_check_redef(v, prop, has_redef, visibility_level)
686 end
687 end
688
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)
691 do
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
696 visibility_level = 3
697 end
698 glob.visibility_level = visibility_level
699 if has_redef then
700 v.error(self, "Error: No property {prop.local_class}::{prop} is inherited. Remove the redef keyword to define a new property.")
701 end
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}.")
707 end
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}.")
713 end
714 end
715 if prop.signature == null then
716 if glob.is_init then
717 var supers = prop.local_class.super_methods_named(prop.name)
718 inherit_signature(v, prop, supers)
719 end
720 if prop.signature != null then
721 # ok
722 else if not v.untyped_params.is_empty then
723 v.error(v.untyped_params.first, "Error: Untyped parameter.")
724 else
725 prop.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
726 end
727 end
728 end
729
730 private meth inherit_signature(v: PropertyVerifierVisitor, prop: MMLocalProperty, supers: Array[MMLocalProperty])
731 do
732 var s = prop.signature
733 for ip in supers do
734 var isig = ip.signature.adaptation_to(v.local_class.get_type)
735
736 if s == null then
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}")
739 return
740 end
741 for p in v.params do
742 var t = isig[p.position]
743 p.stype = t
744 if p.position == isig.vararg_rank then
745 t = v.type_array(t)
746 end
747 p.variable.stype = t
748 end
749
750 s = isig
751 prop.signature = s
752 end
753 end
754 end
755
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)
758 do
759 var is_init = self isa AConcreteInitPropdef
760 var glob = prop.global
761
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.")
764 return
765 end
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.")
770 end
771
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)
777
778 if s == null then
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).")
782 return
783 end
784 for p in v.params do
785 var t = isig[p.position]
786 p.stype = t
787 if p.position == isig.vararg_rank then
788 t = v.type_array(t)
789 end
790 p.variable.stype = t
791 end
792 s = isig
793 prop.signature = s
794 end
795
796 #print "orig signature: {i.signature.recv} . {i.signature}"
797 #print "inh signature: {isig.recv} . {isig}"
798 #print "redef signature: {s.recv} . {s}"
799
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).")
804 else
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}.")
808 end
809 end
810 end
811
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
821 # FIXME: To remove
822 v.warning(self, "Redef warning: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
823 end
824 end
825
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.")
828 end
829 glob.check_visibility(v, self, v.module, true)
830 end
831 end
832
833 redef class AAttrPropdef
834 redef readable attr _readmethod: MMSrcMethod
835 redef readable attr _writemethod: MMSrcMethod
836 redef readable attr _prop: MMSrcAttribute
837
838 redef meth accept_property_builder(v)
839 do
840 super
841 var name = n_id.to_symbol
842 var prop = new MMSrcAttribute(name, v.local_class, self)
843 _prop = prop
844 v.local_class.add_src_local_property(v, prop)
845
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)
850 end
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)
855 end
856 end
857
858 redef meth accept_property_verifier(v)
859 do
860 super
861 var t: MMType
862 if n_type != null then
863 t = n_type.get_stype(v)
864 else
865 v.error(self, "Not yet implemented: Attribute definition {_prop.local_class}::{_prop} requires an explicit type.")
866 return
867 end
868
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)
877 end
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)
882 end
883 end
884
885 redef meth accept_abs_syntax_visitor(v)
886 do
887 v.local_property = prop
888 super
889 v.local_property = null
890 end
891 end
892
893 redef class AMethPropdef
894 # Name of the method
895 readable attr _name: Symbol
896
897 redef readable attr _method: MMMethSrcMethod
898
899 redef meth accept_property_builder(v)
900 do
901 super
902 if n_methid == null then
903 if self isa AConcreteInitPropdef then
904 _name = once "init".to_symbol
905 else
906 _name = once "main".to_symbol
907 end
908 else
909 _name = n_methid.name
910 # FIXME: Add the 'unary' keyword
911 if n_methid.name == (once "-".to_symbol) then
912 var ns = n_signature
913 if ns isa ASignature and ns.n_params.length == 0 then
914 _name = once "unary -".to_symbol
915 end
916 end
917 end
918 var prop = new MMMethSrcMethod(_name, v.local_class, self)
919 _method = prop
920 v.local_class.add_src_local_property(v, prop)
921 end
922
923 redef meth accept_property_verifier(v)
924 do
925 v.params = new Array[PParam]
926 v.untyped_params = new Array[PParam]
927 v.signature = null
928 v.vararg_rank = -1
929
930 super
931
932 if v.signature == null then
933 #_method.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
934 else
935 _method.signature = v.signature
936 end
937 var visibility_level = 1
938 if n_visibility != null and n_visibility.level > 1 then
939 visibility_level = n_visibility.level
940 end
941 process_and_check(v, _method, n_kwredef != null, visibility_level)
942 if n_signature != null then n_signature.check_visibility(v, _method)
943 end
944
945 redef meth accept_abs_syntax_visitor(v)
946 do
947 v.local_property = method
948 super
949 v.local_property = null
950 end
951 end
952
953 redef class AMainMethPropdef
954 redef meth process_and_check(v, prop, has_redef, visibility_level)
955 do
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
959 end
960 end
961
962 redef class ATypePropdef
963 redef readable attr _prop: MMSrcTypeProperty
964
965 redef meth accept_property_builder(v)
966 do
967 super
968 var name = n_id.to_symbol
969 var prop = new MMSrcTypeProperty(name, v.local_class, self)
970 _prop = prop
971 v.local_class.add_src_local_property(v, prop)
972 end
973
974 redef meth accept_property_verifier(v)
975 do
976 super
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)
981 end
982
983 redef meth accept_abs_syntax_visitor(v)
984 do
985 v.local_property = prop
986 super
987 v.local_property = null
988 end
989 end
990
991 # Visitor used to build a full method name from multiple tokens
992 private class MethidAccumulator
993 special Visitor
994 readable attr _name: String
995 redef meth visit(n)
996 do
997 if n isa Token then
998 _name.append(n.text)
999 else
1000 n.visit_all(self)
1001 end
1002 end
1003
1004 init
1005 do
1006 _name = new String
1007 end
1008 end
1009
1010 redef class PMethid
1011 # Method name
1012 readable attr _name: Symbol
1013
1014 redef meth accept_property_builder(v)
1015 do
1016 var accumulator = new MethidAccumulator
1017 accumulator.visit(self)
1018 _name = accumulator.name.to_symbol
1019 super
1020 end
1021 end
1022
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
1026 end
1027
1028 redef class ASignature
1029 redef meth accept_property_verifier(v)
1030 do
1031 super
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.")
1035 return
1036 end
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
1040 pars.add(p.stype)
1041 end
1042 var ret: MMType = null
1043 if n_type != null then
1044 ret = n_type.get_stype(v)
1045 end
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
1049 end
1050 end
1051 end
1052
1053 redef meth check_visibility(v, p)
1054 do
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)
1058 end
1059 if n_type != null then n_type.check_visibility(v, p)
1060 end
1061 end
1062
1063 redef class PParam
1064 redef readable attr _position: Int
1065
1066 redef readable attr _variable: Variable
1067
1068 # The type of the parameter in signature
1069 readable writable attr _stype: MMType
1070
1071 redef meth accept_property_verifier(v)
1072 do
1073 super
1074 _position = v.params.length
1075 _variable = new Variable(n_id.to_symbol, self)
1076 v.params.add(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
1081 p.stype = stype
1082 if is_vararg then
1083 if v.vararg_rank == -1 then
1084 v.vararg_rank = p.position
1085 else
1086 v.error(self, "Error: A vararg parameter is already defined.")
1087 end
1088 stype = v.type_array(stype)
1089 end
1090 p.variable.stype = stype
1091 end
1092 v.untyped_params.clear
1093 end
1094 end
1095
1096 meth is_vararg: Bool is abstract
1097 end
1098
1099 redef class AParam
1100 redef meth is_vararg do return n_dotdotdot != null
1101 end
1102
1103 redef class PType
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
1106 end
1107
1108 redef class AType
1109 redef meth check_visibility(v, p)
1110 do
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}.")
1118 end
1119 for n in n_types do
1120 n.check_visibility(v, p)
1121 end
1122 end
1123 end
1124
1125 redef class PExpr
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
1129 end