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