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