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