syntax: Do not inherit signatures with closures
[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 # Introduce or inherit default constructors
157 private meth process_default_constructors(v: PropertyBuilderVisitor)
158 do
159 # Is there already a constructor ?
160 for gp in global_properties do
161 if gp.is_init then
162 # Return if explicit constructor in the class
163 if gp.intro.local_class == self then return
164 end
165 end
166
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)
175 end
176 var gp = sc.get_property_by_name(once ("init".to_symbol))
177 if gp != null then
178 super_inits.add(self[gp])
179 end
180 end
181
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
186 var n = a.node
187 assert n isa AAttrPropdef
188 if n.n_expr == null then unassigned_attributes.add(a)
189 end
190 end
191
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
200 supers.add(sc)
201 end
202 end
203 supers = che.order.select_smallests(supers)
204
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)
207
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(", ")}.")
210 return
211 else if supers.length == 1 then
212 superclass = supers.first
213 end
214
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)
219 end
220 end
221 global.mixin_of = superclass.global
222 else
223 # v.error(nodes.first, "Error, constructor required in {self} since no anonimous init found in {sc}.")
224
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")
229 end
230 end
231
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)
235 do
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}.")
240 return
241 end
242 src_local_properties[pname] = prop
243
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}.")
249 end
250 var g = globs.first
251 prop.inherit_global(g)
252 end
253
254 if prop.global == null then
255 prop.new_global
256 prop.global.is_init = prop.is_init
257 end
258 end
259 end
260
261 redef class MMLocalProperty
262 private meth accept_property_visitor(v: AbsSyntaxVisitor)
263 do
264 end
265 end
266
267 redef class MMImplicitInit
268 readable attr _super_init: MMLocalProperty = null
269 redef meth accept_property_visitor(v)
270 do
271 var base: MMLocalProperty = null
272 for p in super_inits do
273 if p.signature.arity > 0 then
274 if base == null then
275 base = p
276 else
277 v.error(null, "Error: explicit constructor needed in {local_class} since both super-constructor {base.full_name} and {p.full_name} have paramters")
278 return
279 end
280 end
281 end
282 _super_init = base
283
284 var params = new Array[MMType]
285 if base != null then
286 var sig = base.signature
287 for i in [0..sig.arity[ do
288 params.add(sig[i])
289 end
290 end
291 for a in unassigned_attributes do
292 params.add(a.signature.return_type)
293 end
294 signature = new MMSignature(params, null, local_class.get_type)
295 end
296 end
297
298
299 # Concrete NIT class specialization relation
300 class MMSrcAncestor
301 special MMAncestor
302 # The related AST node
303 readable attr _node: ASuperclass
304 redef readable attr _local_class: MMLocalClass
305
306 init(n: ASuperclass, c: MMLocalClass)
307 do
308 _node = n
309 _local_class = c
310 end
311 end
312
313 ###############################################################################
314
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
322
323 # Current class formal parameters
324 readable writable attr _formals: Map[Symbol, MMTypeFormalParameter]
325
326 redef meth visit(n) do n.accept_class_builder(self)
327 init(tc, m) do super
328 end
329
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)
335 init(tc, m) do super
336 end
337
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)
343 init(tc, m) do super
344 end
345
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)
351 init(tc, m) do super
352 end
353
354
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)
362 init(tc, m) do super
363 end
364
365 # Another pass pass visitor for syntax analysis.
366 # * Check property conformance
367 private class PropertyVerifierVisitor
368 special AbsSyntaxVisitor
369
370 # The signature currently build
371 readable writable attr _signature_builder: SignatureBuilder
372
373 redef meth visit(n) do n.accept_property_verifier(self)
374
375 init(tc, m)
376 do
377 super
378 _signature_builder = new SignatureBuilder
379 end
380 end
381
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]
386
387 # Visited parameters without type information added
388 readable writable attr _untyped_params: Array[PParam] = new Array[PParam]
389
390 # Position of the current star parameter
391 readable writable attr _vararg_rank: Int = -1
392
393 # Current closure declarations
394 readable writable attr _closure_decls: Array[AClosureDecl] = new Array[AClosureDecl]
395
396 # Current signature
397 readable writable attr _signature: MMSignature = null
398 end
399
400 ###############################################################################
401
402 redef class PNode
403 private meth accept_class_builder(v: ClassBuilderVisitor) do accept_abs_syntax_visitor(v)
404 private meth accept_class_specialization_builder(v: ClassSpecializationBuilderVisitor) do accept_abs_syntax_visitor(v)
405 private meth accept_class_ancestor_builder(v: ClassAncestorBuilder) do accept_abs_syntax_visitor(v)
406 private meth accept_class_verifier(v: ClassVerifierVisitor) do accept_abs_syntax_visitor(v)
407 private meth accept_property_builder(v: PropertyBuilderVisitor) do accept_abs_syntax_visitor(v)
408 private meth accept_property_verifier(v: PropertyVerifierVisitor) do accept_abs_syntax_visitor(v)
409 end
410
411 redef class AModule
412 # Import supermodules and compute visibility
413 meth import_super_modules(tc: ToolContext, mod: MMSrcModule)
414 do
415 # Import super-modules
416 var module_names_to_import = new Array[Symbol]
417 var module_visibility = new HashMap[Symbol, Int]
418 var no_import: PImport = null
419 for i in n_imports do
420 var n = i.module_name
421 if n != null then
422 module_names_to_import.add(n)
423 module_visibility[n] = i.visibility_level
424 else
425 no_import = i
426 end
427 end
428 if no_import != null then
429 if not module_names_to_import.is_empty then
430 tc.error("{no_import.locate}: Error: Top modules cannot import other modules.")
431 end
432 else if module_names_to_import.is_empty then
433 var stdname = once "standard".to_symbol
434 module_names_to_import.add(stdname)
435 module_visibility[stdname] = 1
436 end
437
438 mod.import_supers_modules(module_names_to_import)
439
440 for mname in module_names_to_import do
441 var level = module_visibility[mname]
442 var m = tc.get_module(mname, mod)
443 mod.add_super_module(m, level)
444 end
445 end
446 end
447
448 redef class APackagedecl
449 redef meth accept_class_builder(v)
450 do
451 if n_id.to_symbol != v.module.name then
452 v.error(n_id, "Error: Package name missmatch between {v.module.name} and {n_id.to_symbol}")
453 end
454 end
455 end
456
457 redef class PImport
458 # Imported module name (or null)
459 meth module_name: Symbol is abstract
460
461 # Visibility level (intrude/public/private)
462 meth visibility_level: Int is abstract
463 end
464 redef class AImport
465 redef meth module_name
466 do
467 return n_id.to_symbol
468 end
469 redef meth visibility_level
470 do
471 return n_visibility.level
472 end
473 end
474 redef class ANoImport
475 redef meth module_name
476 do
477 return null
478 end
479 end
480
481 redef class PVisibility
482 # Visibility level
483 meth level: Int is abstract
484 end
485 redef class APublicVisibility
486 redef meth level do return 1
487 end
488 redef class AProtectedVisibility
489 redef meth level do return 2
490 end
491 redef class APrivateVisibility
492 redef meth level do return 3
493 end
494 redef class AIntrudeVisibility
495 redef meth level do return 0
496 end
497
498
499 redef class PClassdef
500 redef readable attr _local_class: MMSrcLocalClass
501
502 # Name of the class
503 meth name: Symbol is abstract
504
505 # Number of formal parameters
506 meth arity: Int do return 0
507
508 # Visibility of the class
509 meth visibility_level: Int do return 1
510
511 redef meth accept_class_builder(v)
512 do
513 var local_class: MMSrcLocalClass
514 var mod = v.module
515 var local_classes = mod.src_local_classes
516 if (local_classes.has_key(name)) then
517 local_class = local_classes[name]
518 if self isa AClassdef then
519 # If we are not a special implicit class then rant
520 v.error(self, "Error: A class {name} is already defined at line {local_class.nodes.first.first_token.line}.")
521 return
522 end
523 local_class.nodes.add(self)
524 else
525 local_class = new MMSrcLocalClass(name, self, arity)
526 mod.add_local_class(local_class)
527 local_classes[name] = local_class
528 var g = mod.global_class_named(name)
529 if g == null then
530 # Intro
531 local_class.new_global
532 g = local_class.global
533 else
534 local_class.set_global(g)
535 end
536
537 end
538 _local_class = local_class
539 v.local_class_arity = 0
540 v.formals = new HashMap[Symbol, MMTypeFormalParameter]
541
542 #####
543 super
544 #####
545
546 _local_class.formal_dict = v.formals
547 v.formals = null
548 end
549
550 redef meth accept_abs_syntax_visitor(v)
551 do
552 v.local_class = _local_class
553 super
554 v.local_class = null
555 end
556 end
557
558 redef class PClasskind
559 meth is_interface: Bool do return false
560 meth is_universal: Bool do return false
561 meth is_abstract: Bool do return false
562 end
563
564 redef class AInterfaceClasskind
565 redef meth is_interface do return true
566 end
567 redef class AUniversalClasskind
568 redef meth is_universal do return true
569 end
570 redef class AAbstractClasskind
571 redef meth is_abstract do return true
572 end
573
574 redef class AClassdef
575 redef meth name
576 do
577 return n_id.to_symbol
578 end
579 redef meth arity
580 do
581 return n_formaldefs.length
582 end
583 redef meth accept_class_verifier(v)
584 do
585 super
586 var glob = _local_class.global
587 if glob.intro == _local_class then
588 # Intro
589 glob.visibility_level = visibility_level
590 glob.is_interface = n_classkind.is_interface
591 glob.is_abstract = n_classkind.is_abstract
592 glob.is_universal = n_classkind.is_universal
593 if n_kwredef != null then
594 v.error(self, "Redef error: No class {name} is imported. Remove the redef keyword to define a new class.")
595 end
596
597 for c in _local_class.cshe.direct_greaters do
598 var cg = c.global
599 if glob.is_interface then
600 if cg.is_universal then
601 v.error(self, "Special error: Interface {name} try to specialise universal class {c.name}.")
602 else if not cg.is_interface then
603 v.error(self, "Special error: Interface {name} try to specialise class {c.name}.")
604 end
605 else if glob.is_universal then
606 if not cg.is_interface and not cg.is_universal then
607 v.error(self, "Special error: Universal class {name} try to specialise class {c.name}.")
608 end
609 else
610 if cg.is_universal then
611 v.error(self, "Special error: Class {name} try to specialise universal class {c.name}.")
612 end
613 end
614
615 end
616 return
617 end
618
619 # Redef
620
621 glob.check_visibility(v, self, v.module)
622 if n_kwredef == null then
623 v.error(self, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
624 return
625 end
626
627 if glob.intro.arity != _local_class.arity then
628 v.error(self, "Redef error: Formal parameter arity missmatch; got {_local_class.arity}, expected {glob.intro.arity}.")
629 end
630
631 if
632 not glob.is_interface and n_classkind.is_interface or
633 not glob.is_abstract and n_classkind.is_abstract or
634 not glob.is_universal and n_classkind.is_universal
635 then
636 v.error(self, "Redef error: cannot change kind of class {name}.")
637 end
638 end
639
640 redef meth visibility_level
641 do
642 return n_visibility.level
643 end
644 end
645
646 redef class AMainClassdef
647 redef meth name
648 do
649 return once "Sys".to_symbol
650 end
651 end
652
653 redef class ATopClassdef
654 redef meth name
655 do
656 return once "Object".to_symbol
657 end
658 end
659
660 class MMSrcTypeFormalParameter
661 special MMTypeFormalParameter
662 # The associated node
663 readable attr _node: AFormaldef
664
665 init(name: Symbol, pos: Int, local_class: MMLocalClass, n: AFormaldef)
666 do
667 super(name, pos, local_class)
668 _node = n
669 end
670 end
671
672 redef class AFormaldef
673 # The associated formal generic parameter (MM entity)
674 attr _formal: MMSrcTypeFormalParameter
675
676 redef meth accept_class_builder(v)
677 do
678 var name = n_id.to_symbol
679 var formal_type = new MMSrcTypeFormalParameter(name, v.local_class_arity, v.local_class, self)
680 _formal = formal_type
681 v.local_class_arity = v.local_class_arity + 1
682 v.local_class.register_formal(formal_type)
683 v.formals[name] = formal_type
684 super
685 end
686
687 redef meth accept_class_verifier(v)
688 do
689 super
690 var c = v.local_class
691 var o = c.global.intro
692 if c == o then
693 if n_type == null then
694 _formal.bound = v.module.type_any
695 else
696 _formal.bound = n_type.get_stype(v)
697 end
698 else
699 var ob = o.get_formal(_formal.position).bound.for_module(v.module)
700 if n_type == null then
701 _formal.bound = ob
702 else
703 _formal.bound = n_type.get_stype(v)
704 if _formal.bound != ob then
705 v.error(self, "Redef error: Cannot change formal parameter type of class {c}; got {_formal.bound}, expected {ob}.")
706 end
707 end
708 end
709 end
710 end
711
712 redef class ASuperclass
713 readable attr _ancestor: MMSrcAncestor
714
715 redef meth accept_class_specialization_builder(v)
716 do
717 super
718 var c = n_type.get_local_class(v)
719 var ancestor = new MMSrcAncestor(self, c)
720 _ancestor = ancestor
721 v.local_class.add_direct_parent(ancestor)
722 end
723
724 redef meth accept_class_ancestor_builder(v)
725 do
726 super
727 _ancestor.stype = n_type.get_unchecked_stype(v)
728 _ancestor.inheriter = v.local_class.get_type
729 end
730
731 redef meth accept_class_verifier(v)
732 do
733 super
734 n_type.check_conform(v)
735 end
736 end
737
738 redef class PPropdef
739 # Process and check properties of the property.
740 # * Distinguish inits and methods
741 # * Inherit or check visibility.
742 # * Check redef errors.
743 # * Check forbiden attribute definitions.
744 # * Check signature conformance.
745 private meth process_and_check(v: PropertyVerifierVisitor, prop: MMLocalProperty, has_redef: Bool, visibility_level: Int)
746 do
747 if prop.global.intro == prop then
748 do_and_check_intro(v, prop, has_redef, visibility_level)
749 else
750 do_and_check_redef(v, prop, has_redef, visibility_level)
751 end
752 end
753
754 # The part of process_and_check when prop is an introduction
755 private meth do_and_check_intro(v: PropertyVerifierVisitor, prop: MMLocalProperty, has_redef: Bool, visibility_level: Int)
756 do
757 var glob = prop.global
758 var gbc = prop.local_class.global
759 if v.local_class.global.visibility_level >= 3 then
760 # Method of private classes are private
761 visibility_level = 3
762 end
763 glob.visibility_level = visibility_level
764 if has_redef then
765 v.error(self, "Error: No property {prop.local_class}::{prop} is inherited. Remove the redef keyword to define a new property.")
766 end
767 if glob.is_attribute then
768 if gbc.is_interface then
769 v.error(self, "Error: Attempt to define attribute {prop} in the interface {prop.local_class}.")
770 else if gbc.is_universal then
771 v.error(self, "Error: Attempt to define attribute {prop} in the universal class {prop.local_class}.")
772 end
773 else if glob.is_init then
774 if gbc.is_interface then
775 v.error(self, "Error: Attempt to define a constructor {prop} in the class {prop.local_class}.")
776 else if gbc.is_universal then
777 v.error(self, "Error: Attempt to define a constructor {prop} in the universal {prop.local_class}.")
778 end
779 end
780 if prop.signature == null then
781 if glob.is_init then
782 var supers = prop.local_class.super_methods_named(prop.name)
783 inherit_signature(v, prop, supers)
784 end
785 if prop.signature != null then
786 # ok
787 else if not v.signature_builder.untyped_params.is_empty then
788 v.error(v.signature_builder.untyped_params.first, "Error: Untyped parameter.")
789 else
790 prop.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
791 if v.signature_builder.closure_decls != null then
792 for clos in v.signature_builder.closure_decls do
793 prop.signature.closures.add(clos.variable.closure)
794 end
795 end
796 end
797 end
798 end
799
800 private meth inherit_signature(v: PropertyVerifierVisitor, prop: MMLocalProperty, supers: Array[MMLocalProperty])
801 do
802 var s = prop.signature
803 for ip in supers do
804 var isig = ip.signature.adaptation_to(v.local_class.get_type)
805
806 if s == null then
807 if v.signature_builder.params.length != isig.arity then
808 #prop.node.printl("v.params.length {v.params.length} != isig.arity {isig.arity} ; {prop.full_name} vs {ip.full_name}")
809 return
810 end
811 for p in v.signature_builder.params do
812 var t = isig[p.position]
813 p.stype = t
814 if p.position == isig.vararg_rank then
815 t = v.type_array(t)
816 end
817 p.variable.stype = t
818 end
819
820 s = isig
821 prop.signature = s
822 end
823 end
824 end
825
826 # The part of process_and_check when prop is a redefinition
827 private meth do_and_check_redef(v: PropertyVerifierVisitor, prop: MMLocalProperty, has_redef: Bool, visibility_level: Int)
828 do
829 var is_init = self isa AConcreteInitPropdef
830 var glob = prop.global
831
832 if not has_redef then
833 v.error(self, "Redef error: {prop.local_class}::{prop} is an inherited property. To redefine it, add the redef keyword.")
834 return
835 end
836 if glob.is_init and not is_init then
837 v.error(self, "Redef error: A method {prop.local_class}::{prop} cannot redefine a constructor.")
838 else if not glob.is_init and is_init then
839 v.error(self, "Redef error: A constructor {prop.local_class}::{prop} cannot redefine a method.")
840 end
841
842 var s = prop.signature
843 #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}"
844 for i in prop.prhe.direct_greaters do
845 var ip = i.local_class[prop.global]
846 var isig = i.signature.adaptation_to(v.local_class.get_type)
847
848 if s == null then
849 #print "{prop.full_name} inherits signature from {ip.full_name}"
850 if v.signature_builder.params.length != isig.arity then
851 v.error(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
852 return
853 end
854 if v.signature_builder.closure_decls.length != isig.closures.length then
855 v.error(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} closure(s).")
856 return
857 end
858 for p in v.signature_builder.params do
859 var t = isig[p.position]
860 p.stype = t
861 if p.position == isig.vararg_rank then
862 t = v.type_array(t)
863 end
864 p.variable.stype = t
865 end
866 s = isig
867 prop.signature = s
868 #print "s is null"
869 end
870
871 var nberr = v.tc.error_count
872 #print "Check {prop.local_class}::{prop}{s} vs {ip.local_class}::{ip}{isig}"
873 #print "s={s.object_id} isig={isig.object_id} isigorig={i.signature.object_id}"
874
875 #print "orig signature: {i.signature.recv} . {i.signature}"
876 #print "inh signature: {isig.recv} . {isig}"
877 #print "redef signature: {s.recv} . {s}"
878
879 if s.arity != isig.arity then
880 v.error(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
881 else
882 for i in [0..s.arity[ do
883 if s[i] != isig[i] then
884 v.error(self, "Redef error: Expected {isig[i]} (as in {ip.local_class}::{ip}), got {s[i]} in {prop.local_class}::{prop}.")
885 end
886 end
887 end
888
889 var srt = s.return_type
890 var isrt = isig.return_type
891 if srt == null and isrt != null then
892 v.error(self, "Redef error: The procedure {prop.local_class}::{prop} redefines the function {ip.local_class}::{ip}.")
893 else if srt != null and isrt == null then
894 v.error(self, "Redef error: The function {prop.local_class}::{prop} redefines the procedure {ip.local_class}::{ip}.")
895 else if srt != null and isrt != null and not srt < isrt then
896 v.error(self, "Redef error: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
897 else if not s < isig and nberr == v.tc.error_count then
898 # Systematic fallback for conformance check
899 v.error(self, "Redef error: Incompatible redefinition of {ip.local_class}::{ip} with {prop.local_class}::{prop}")
900 else if srt != null and isrt != null and srt != isrt and prop isa MMAttribute then
901 # FIXME: To remove
902 v.warning(self, "Redef warning: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
903 end
904 end
905
906 if visibility_level != 1 and glob.visibility_level != visibility_level then
907 v.error(self, "Redef error: {prop.local_class}::{prop} redefinition cannot change visibility.")
908 end
909 glob.check_visibility(v, self, v.module, true)
910 end
911 end
912
913 redef class AAttrPropdef
914 redef readable attr _readmethod: MMSrcMethod
915 redef readable attr _writemethod: MMSrcMethod
916 redef readable attr _prop: MMSrcAttribute
917
918 redef meth accept_property_builder(v)
919 do
920 super
921 var name = n_id.to_symbol
922 var prop = new MMSrcAttribute(name, v.local_class, self)
923 _prop = prop
924 v.local_class.add_src_local_property(v, prop)
925
926 if n_readable != null then
927 name = n_id.text.substring_from(1).to_symbol
928 _readmethod = new MMReadImplementationMethod(name, v.local_class, self)
929 v.local_class.add_src_local_property(v, _readmethod)
930 end
931 if n_writable != null then
932 name = (n_id.text.substring_from(1) + "=").to_symbol
933 _writemethod = new MMWriteImplementationMethod(name, v.local_class, self)
934 v.local_class.add_src_local_property(v, _writemethod)
935 end
936 end
937
938 redef meth accept_property_verifier(v)
939 do
940 super
941 var t: MMType
942 if n_type != null then
943 t = n_type.get_stype(v)
944 else
945 v.error(self, "Not yet implemented: Attribute definition {_prop.local_class}::{_prop} requires an explicit type.")
946 return
947 end
948
949 var signature = new MMSignature(new Array[MMType], t, v.local_class.get_type)
950 _prop.signature = signature
951 var visibility_level = n_visibility.level
952 process_and_check(v, _prop, n_kwredef != null, visibility_level)
953 if n_readable != null then
954 _readmethod.signature = signature
955 process_and_check(v, _readmethod, n_readable.n_kwredef != null, visibility_level)
956 n_type.check_visibility(v, _readmethod)
957 end
958 if n_writable != null then
959 _writemethod.signature = new MMSignature(new Array[MMType].with_items(t), null, v.local_class.get_type)
960 process_and_check(v, _writemethod, n_writable.n_kwredef != null, visibility_level)
961 n_type.check_visibility(v, _writemethod)
962 end
963 end
964
965 redef meth accept_abs_syntax_visitor(v)
966 do
967 v.local_property = prop
968 super
969 v.local_property = null
970 end
971 end
972
973 redef class AMethPropdef
974 # Name of the method
975 readable attr _name: Symbol
976
977 redef readable attr _method: MMMethSrcMethod
978
979 redef meth accept_property_builder(v)
980 do
981 super
982 if n_methid == null then
983 if self isa AConcreteInitPropdef then
984 _name = once "init".to_symbol
985 else
986 _name = once "main".to_symbol
987 end
988 else
989 _name = n_methid.name
990 # FIXME: Add the 'unary' keyword
991 if n_methid.name == (once "-".to_symbol) then
992 var ns = n_signature
993 if ns isa ASignature and ns.n_params.length == 0 then
994 _name = once "unary -".to_symbol
995 end
996 end
997 end
998 var prop = new MMMethSrcMethod(_name, v.local_class, self)
999 _method = prop
1000 v.local_class.add_src_local_property(v, prop)
1001 end
1002
1003 redef meth accept_property_verifier(v)
1004 do
1005 v.signature_builder = new SignatureBuilder
1006 super
1007
1008 if v.signature_builder.signature == null then
1009 #_method.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
1010 else
1011 _method.signature = v.signature_builder.signature
1012 end
1013 var visibility_level = 1
1014 if n_visibility != null and n_visibility.level > 1 then
1015 visibility_level = n_visibility.level
1016 end
1017 process_and_check(v, _method, n_kwredef != null, visibility_level)
1018 if n_signature != null then n_signature.check_visibility(v, _method)
1019 end
1020
1021 redef meth accept_abs_syntax_visitor(v)
1022 do
1023 v.local_property = method
1024 super
1025 v.local_property = null
1026 end
1027 end
1028
1029 redef class AMainMethPropdef
1030 redef meth process_and_check(v, prop, has_redef, visibility_level)
1031 do
1032 prop.global.visibility_level = visibility_level
1033 prop.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
1034 # Disable all checks for main
1035 end
1036 end
1037
1038 redef class ATypePropdef
1039 redef readable attr _prop: MMSrcTypeProperty
1040
1041 redef meth accept_property_builder(v)
1042 do
1043 super
1044 var name = n_id.to_symbol
1045 var prop = new MMSrcTypeProperty(name, v.local_class, self)
1046 _prop = prop
1047 v.local_class.add_src_local_property(v, prop)
1048 end
1049
1050 redef meth accept_property_verifier(v)
1051 do
1052 super
1053 var signature = new MMSignature(new Array[MMType], n_type.get_stype(v), v.local_class.get_type)
1054 _prop.signature = signature
1055 var visibility_level = n_visibility.level
1056 process_and_check(v, _prop, n_kwredef != null, visibility_level)
1057 end
1058
1059 redef meth accept_abs_syntax_visitor(v)
1060 do
1061 v.local_property = prop
1062 super
1063 v.local_property = null
1064 end
1065 end
1066
1067 # Visitor used to build a full method name from multiple tokens
1068 private class MethidAccumulator
1069 special Visitor
1070 readable attr _name: Buffer = new Buffer
1071 redef meth visit(n)
1072 do
1073 if n isa Token then
1074 _name.append(n.text)
1075 else
1076 n.visit_all(self)
1077 end
1078 end
1079 end
1080
1081 redef class PMethid
1082 # Method name
1083 readable attr _name: Symbol
1084
1085 redef meth accept_property_builder(v)
1086 do
1087 var accumulator = new MethidAccumulator
1088 accumulator.visit(self)
1089 _name = accumulator.name.to_s.to_symbol
1090 super
1091 end
1092 end
1093
1094 redef class PSignature
1095 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1096 meth check_visibility(v: AbsSyntaxVisitor, p: MMLocalProperty) is abstract
1097 end
1098
1099 redef class ASignature
1100 redef meth accept_property_verifier(v)
1101 do
1102 super
1103 if not v.signature_builder.untyped_params.is_empty then
1104 if v.signature_builder.untyped_params.first != v.signature_builder.params.first or n_type != null then
1105 v.error(v.signature_builder.untyped_params.first, "Syntax error: untyped parameter.")
1106 return
1107 end
1108 else if not v.signature_builder.params.is_empty or n_type != null then
1109 var pars = new Array[MMType]
1110 for p in v.signature_builder.params do
1111 pars.add(p.stype)
1112 end
1113 var ret: MMType = null
1114 if n_type != null then
1115 ret = n_type.get_stype(v)
1116 end
1117 v.signature_builder.signature = new MMSignature(pars, ret, v.local_class.get_type)
1118 if v.signature_builder.vararg_rank >= 0 then
1119 v.signature_builder.signature.vararg_rank = v.signature_builder.vararg_rank
1120 end
1121 for clos in v.signature_builder.closure_decls do
1122 v.signature_builder.signature.closures.add(clos.variable.closure)
1123 end
1124 end
1125 end
1126
1127 redef meth check_visibility(v, p)
1128 do
1129 if p.global.visibility_level >= 3 then return
1130 for n in n_params do
1131 if n.n_type != null then n.n_type.check_visibility(v, p)
1132 end
1133 if n_type != null then n_type.check_visibility(v, p)
1134 end
1135 end
1136
1137 redef class PParam
1138 redef readable attr _position: Int
1139
1140 redef readable attr _variable: ParamVariable
1141
1142 # The type of the parameter in signature
1143 readable writable attr _stype: MMType
1144
1145 redef meth accept_property_verifier(v)
1146 do
1147 super
1148 _position = v.signature_builder.params.length
1149 _variable = new ParamVariable(n_id.to_symbol, self)
1150 v.signature_builder.params.add(self)
1151 v.signature_builder.untyped_params.add(self)
1152 if n_type != null then
1153 var stype = n_type.get_stype(v)
1154 for p in v.signature_builder.untyped_params do
1155 p.stype = stype
1156 if is_vararg then
1157 if v.signature_builder.vararg_rank == -1 then
1158 v.signature_builder.vararg_rank = p.position
1159 else
1160 v.error(self, "Error: A vararg parameter is already defined.")
1161 end
1162 stype = v.type_array(stype)
1163 end
1164 p.variable.stype = stype
1165 end
1166 v.signature_builder.untyped_params.clear
1167 end
1168 end
1169
1170 meth is_vararg: Bool is abstract
1171 end
1172
1173 redef class AParam
1174 redef meth is_vararg do return n_dotdotdot != null
1175 end
1176
1177 redef class AClosureDecl
1178 redef readable attr _variable: ClosureVariable
1179
1180 redef meth accept_property_verifier(v)
1181 do
1182 var old_signature_builder = v.signature_builder
1183 v.signature_builder = new SignatureBuilder
1184 super
1185 var sig = v.signature_builder.signature
1186 if sig == null then
1187 sig = new MMSignature(new Array[MMType], null, v.local_class.get_type)
1188 end
1189 if sig.return_type != null and n_kwbreak != null then
1190 v.error(self, "Syntax Error: A break bloc cannot have a return value.")
1191 end
1192
1193 # Add the finalizer to the closure signature
1194 var finalize_sig = new MMSignature(new Array[MMType], null, null)
1195 var finalizer_clos = new MMClosure(finalize_sig, false, true)
1196 sig.closures.add(finalizer_clos)
1197
1198 var clos = new MMClosure(sig, n_kwbreak != null, n_expr != null)
1199 v.signature_builder = old_signature_builder
1200 old_signature_builder.closure_decls.add(self)
1201 _variable = new ClosureVariable(n_id.to_symbol, self, clos)
1202 end
1203 end
1204
1205 redef class PType
1206 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1207 private meth check_visibility(v: AbsSyntaxVisitor, p: MMLocalProperty) is abstract
1208 end
1209
1210 redef class AType
1211 redef meth check_visibility(v, p)
1212 do
1213 if p.global.visibility_level >= 3 then return
1214 var t = get_stype(v)
1215 if t == null then return
1216 var bc = t.local_class
1217 if bc == null then return
1218 if bc.global.visibility_level >= 3 then
1219 v.error(self, "Access error: Class {bc} is private and cannot be used in the signature of the non-private property {p}.")
1220 end
1221 for n in n_types do
1222 n.check_visibility(v, p)
1223 end
1224 end
1225 end
1226
1227 redef class PExpr
1228 redef meth accept_class_builder(v) do end
1229 redef meth accept_property_builder(v) do end
1230 redef meth accept_property_verifier(v) do end
1231 end