f9fb9d2c07b916eef1d055d9aeb95abfc4cdaffc
[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 mmbv1b = new ClassVerifierVisitor(tc, self)
88 for c in classes do
89 c.accept_class_visitor(mmbv1b)
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 #prop.node.printl("v.params.length {v.params.length} != isig.arity {isig.arity} ; {prop.full_name} vs {ip.full_name}")
823 return
824 end
825 for p in v.signature_builder.params do
826 var t = isig[p.position]
827 p.stype = t
828 if p.position == isig.vararg_rank then
829 t = v.type_array(t)
830 end
831 p.variable.stype = t
832 end
833
834 s = isig
835 prop.signature = s
836 end
837 end
838 end
839
840 # The part of process_and_check when prop is a redefinition
841 private fun do_and_check_redef(v: PropertyVerifierVisitor, prop: MMLocalProperty, has_redef: Bool, visibility_level: Int)
842 do
843 var is_init = self isa AConcreteInitPropdef
844 var glob = prop.global
845
846 if not has_redef then
847 v.error(self, "Redef error: {prop.local_class}::{prop} is an inherited property. To redefine it, add the redef keyword.")
848 return
849 end
850 if glob.is_init and not is_init then
851 v.error(self, "Redef error: A method {prop.local_class}::{prop} cannot redefine a constructor.")
852 else if not glob.is_init and is_init then
853 v.error(self, "Redef error: A constructor {prop.local_class}::{prop} cannot redefine a method.")
854 end
855
856 var s = prop.signature
857 #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}"
858 for i in prop.prhe.direct_greaters do
859 var ip = i.local_class[prop.global]
860 var isig = i.signature.adaptation_to(v.local_class.get_type)
861
862 if s == null then
863 #print "{prop.full_name} inherits signature from {ip.full_name}"
864 if v.signature_builder.params.length != isig.arity then
865 v.error(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
866 return
867 end
868 if v.signature_builder.closure_decls.length != isig.closures.length then
869 v.error(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} closure(s).")
870 return
871 end
872 for p in v.signature_builder.params do
873 var t = isig[p.position]
874 p.stype = t
875 if p.position == isig.vararg_rank then
876 t = v.type_array(t)
877 end
878 p.variable.stype = t
879 end
880 s = isig
881 prop.signature = s
882 #print "s is null"
883 end
884
885 var nberr = v.tc.error_count
886 #print "Check {prop.local_class}::{prop}{s} vs {ip.local_class}::{ip}{isig}"
887 #print "s={s.object_id} isig={isig.object_id} isigorig={i.signature.object_id}"
888
889 #print "orig signature: {i.signature.recv} . {i.signature}"
890 #print "inh signature: {isig.recv} . {isig}"
891 #print "redef signature: {s.recv} . {s}"
892
893 if s.arity != isig.arity then
894 v.error(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
895 else
896 for i in [0..s.arity[ do
897 if s[i] != isig[i] then
898 v.error(self, "Redef error: Expected {isig[i]} (as in {ip.local_class}::{ip}), got {s[i]} in {prop.local_class}::{prop}.")
899 end
900 end
901 end
902
903 var srt = s.return_type
904 var isrt = isig.return_type
905 if srt == null and isrt != null then
906 v.error(self, "Redef error: The procedure {prop.local_class}::{prop} redefines the function {ip.local_class}::{ip}.")
907 else if srt != null and isrt == null then
908 v.error(self, "Redef error: The function {prop.local_class}::{prop} redefines the procedure {ip.local_class}::{ip}.")
909 else if srt != null and isrt != null and not srt < isrt then
910 v.error(self, "Redef error: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
911 else if not s < isig and nberr == v.tc.error_count then
912 # Systematic fallback for conformance check
913 v.error(self, "Redef error: Incompatible redefinition of {ip.local_class}::{ip} with {prop.local_class}::{prop}")
914 else if srt != null and isrt != null and srt != isrt and prop isa MMAttribute then
915 # FIXME: To remove
916 v.warning(self, "Redef warning: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
917 end
918 end
919
920 if visibility_level != 1 and glob.visibility_level != visibility_level then
921 v.error(self, "Redef error: {prop.local_class}::{prop} redefinition cannot change visibility.")
922 end
923 glob.check_visibility(v, self, v.module, true)
924 end
925 end
926
927 redef class AAttrPropdef
928 redef readable var _readmethod: nullable MMSrcMethod
929 redef readable var _writemethod: nullable MMSrcMethod
930 var _prop: nullable MMSrcAttribute
931 redef fun prop do return _prop.as(not null)
932
933 redef fun accept_property_builder(v)
934 do
935 super
936 var name = n_id.to_symbol
937 var lc = v.local_class
938 var prop = new MMSrcAttribute(name, lc, self)
939 _prop = prop
940 v.local_class.add_src_local_property(v, prop)
941
942 if n_readable != null then
943 name = n_id.text.substring_from(1).to_symbol
944 var readmethod = new MMReadImplementationMethod(name, lc, self)
945 _readmethod = readmethod
946 v.local_class.add_src_local_property(v, readmethod)
947 end
948 if n_writable != null then
949 name = (n_id.text.substring_from(1) + "=").to_symbol
950 var writemethod = new MMWriteImplementationMethod(name, lc, self)
951 _writemethod = writemethod
952 v.local_class.add_src_local_property(v, writemethod)
953 end
954 end
955
956 redef fun accept_property_verifier(v)
957 do
958 super
959 var t: MMType
960 if n_type != null then
961 var t0 = n_type.get_stype(v)
962 if t0 != null then t = t0 else return
963 else
964 v.error(self, "Not yet implemented: Attribute definition {_prop.local_class}::{_prop} requires an explicit type.")
965 return
966 end
967
968 var prop = prop
969 var signature = new MMSignature(new Array[MMType], t, v.local_class.get_type)
970 prop.signature = signature
971 var visibility_level = n_visibility.level
972 process_and_check(v, prop, n_kwredef != null, visibility_level)
973 if n_readable != null then
974 var m = _readmethod.as(not null)
975 m.signature = signature
976 process_and_check(v, m, n_readable.n_kwredef != null, visibility_level)
977 n_type.check_visibility(v, m)
978 end
979 if n_writable != null then
980 var m = _writemethod.as(not null)
981 m.signature = new MMSignature(new Array[MMType].with_items(t), null, v.local_class.get_type)
982 process_and_check(v, m, n_writable.n_kwredef != null, visibility_level)
983 n_type.check_visibility(v, m)
984 end
985 end
986
987 redef fun accept_abs_syntax_visitor(v)
988 do
989 v.local_property = _prop
990 super
991 v.local_property = null
992 end
993 end
994
995 redef class AMethPropdef
996 # Name of the method
997 readable var _name: nullable Symbol
998
999 var _method: nullable MMMethSrcMethod
1000 redef fun method do return _method.as(not null)
1001
1002 redef fun accept_property_builder(v)
1003 do
1004 super
1005 var name: Symbol
1006 if n_methid == null then
1007 if self isa AConcreteInitPropdef then
1008 name = once "init".to_symbol
1009 else
1010 name = once "main".to_symbol
1011 end
1012 else
1013 name = n_methid.name.as(not null)
1014 # FIXME: Add the 'unary' keyword
1015 if n_methid.name == (once "-".to_symbol) then
1016 var ns = n_signature
1017 if ns != null and ns.n_params.length == 0 then
1018 name = once "unary -".to_symbol
1019 end
1020 end
1021 end
1022 _name = name
1023 var prop = new MMMethSrcMethod(name, v.local_class, self)
1024 _method = prop
1025 v.local_class.add_src_local_property(v, prop)
1026 end
1027
1028 redef fun accept_property_verifier(v)
1029 do
1030 v.signature_builder = new SignatureBuilder
1031 super
1032
1033 if v.signature_builder.has_error_occured then return
1034
1035 if v.signature_builder.signature == null then
1036 #_method.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
1037 else
1038 method.signature = v.signature_builder.signature.as(not null)
1039 end
1040 var visibility_level = 1
1041 if n_visibility != null and n_visibility.level > 1 then
1042 visibility_level = n_visibility.level
1043 end
1044 process_and_check(v, method, n_kwredef != null, visibility_level)
1045 if n_signature != null then n_signature.check_visibility(v, method)
1046 end
1047
1048 redef fun accept_abs_syntax_visitor(v)
1049 do
1050 v.local_property = _method
1051 super
1052 v.local_property = null
1053 end
1054 end
1055
1056 redef class AMainMethPropdef
1057 redef fun process_and_check(v, prop, has_redef, visibility_level)
1058 do
1059 prop.global.visibility_level = visibility_level
1060 prop.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
1061 # Disable all checks for main
1062 end
1063 end
1064
1065 redef class ATypePropdef
1066 redef fun prop do return _prop.as(not null)
1067 var _prop: nullable MMSrcTypeProperty
1068
1069 redef fun accept_property_builder(v)
1070 do
1071 super
1072 var name = n_id.to_symbol
1073 var prop = new MMSrcTypeProperty(name, v.local_class, self)
1074 _prop = prop
1075 v.local_class.add_src_local_property(v, prop)
1076 end
1077
1078 redef fun accept_property_verifier(v)
1079 do
1080 super
1081 var signature = new MMSignature(new Array[MMType], n_type.get_stype(v), v.local_class.get_type)
1082 prop.signature = signature
1083 var visibility_level = n_visibility.level
1084 process_and_check(v, prop, n_kwredef != null, visibility_level)
1085 end
1086
1087 redef fun accept_abs_syntax_visitor(v)
1088 do
1089 v.local_property = _prop
1090 super
1091 v.local_property = null
1092 end
1093 end
1094
1095 # Visitor used to build a full method name from multiple tokens
1096 private class MethidAccumulator
1097 special Visitor
1098 readable var _name: Buffer = new Buffer
1099 redef fun visit(n)
1100 do
1101 if n isa Token then
1102 _name.append(n.text)
1103 else
1104 n.visit_all(self)
1105 end
1106 end
1107 end
1108
1109 redef class AMethid
1110 # Method name
1111 readable var _name: nullable Symbol
1112
1113 redef fun accept_property_builder(v)
1114 do
1115 var accumulator = new MethidAccumulator
1116 accumulator.enter_visit(self)
1117 _name = accumulator.name.to_s.to_symbol
1118 super
1119 end
1120 end
1121
1122 redef class ASignature
1123 redef fun accept_property_verifier(v)
1124 do
1125 super
1126 if v.signature_builder.has_error_occured then
1127 return
1128 else if not v.signature_builder.untyped_params.is_empty then
1129 if v.signature_builder.untyped_params.first != v.signature_builder.params.first or n_type != null then
1130 v.error(v.signature_builder.untyped_params.first, "Syntax error: untyped parameter.")
1131 return
1132 end
1133 else if not v.signature_builder.params.is_empty or n_type != null then
1134 var pars = new Array[MMType]
1135 for p in v.signature_builder.params do
1136 pars.add(p.stype.as(not null))
1137 end
1138 var ret: nullable MMType = null
1139 if n_type != null then
1140 ret = n_type.get_stype(v)
1141 if ret == null then
1142 v.signature_builder.has_error_occured = true
1143 return
1144 end
1145 end
1146 v.signature_builder.signature = new MMSignature(pars, ret, v.local_class.get_type)
1147 if v.signature_builder.vararg_rank >= 0 then
1148 v.signature_builder.signature.vararg_rank = v.signature_builder.vararg_rank
1149 end
1150 for clos in v.signature_builder.closure_decls do
1151 v.signature_builder.signature.closures.add(clos.variable.closure)
1152 end
1153 end
1154 end
1155
1156 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1157 fun check_visibility(v: AbsSyntaxVisitor, p: MMLocalProperty)
1158 do
1159 if p.global.visibility_level >= 3 then return
1160 for n in n_params do
1161 if n.n_type != null then n.n_type.check_visibility(v, p)
1162 end
1163 if n_type != null then n_type.check_visibility(v, p)
1164 end
1165 end
1166
1167 redef class AParam
1168 redef readable var _position: Int = 0
1169
1170 redef fun variable: ParamVariable do return _variable.as(not null)
1171 var _variable: nullable ParamVariable
1172
1173 # The type of the parameter in signature
1174 readable writable var _stype: nullable MMType
1175
1176 redef fun accept_property_verifier(v)
1177 do
1178 super
1179 _position = v.signature_builder.params.length
1180 _variable = new ParamVariable(n_id.to_symbol, self)
1181 v.signature_builder.params.add(self)
1182 v.signature_builder.untyped_params.add(self)
1183 if n_type != null then
1184 var stype = n_type.get_stype(v)
1185 if stype == null then
1186 v.signature_builder.has_error_occured = true
1187 return
1188 end
1189 for p in v.signature_builder.untyped_params do
1190 p.stype = stype
1191 if is_vararg then
1192 if v.signature_builder.vararg_rank == -1 then
1193 v.signature_builder.vararg_rank = p.position
1194 else
1195 v.error(self, "Error: A vararg parameter is already defined.")
1196 end
1197 stype = v.type_array(stype)
1198 end
1199 p.variable.stype = stype
1200 end
1201 v.signature_builder.untyped_params.clear
1202 end
1203 end
1204
1205 fun is_vararg: Bool do return n_dotdotdot != null
1206 end
1207
1208 redef class AClosureDecl
1209 redef readable var _position: Int = 0
1210
1211 redef fun variable: ClosureVariable do return _variable.as(not null)
1212 var _variable: nullable ClosureVariable
1213
1214 redef fun accept_property_verifier(v)
1215 do
1216 var old_signature_builder = v.signature_builder
1217 v.signature_builder = new SignatureBuilder
1218 super
1219 if v.signature_builder.has_error_occured then
1220 return
1221 end
1222 var sig = v.signature_builder.signature
1223 if sig == null then
1224 sig = new MMSignature(new Array[MMType], null, v.local_class.get_type)
1225 end
1226 if sig.return_type != null and n_kwbreak != null then
1227 v.error(self, "Syntax Error: A break block cannot have a return value.")
1228 end
1229
1230 # Add the finalizer to the closure signature
1231 var finalize_sig = new MMSignature(new Array[MMType], null, v.module.type_any) # FIXME should be no receiver
1232 var finalizer_clos = new MMClosure(finalize_sig, false, true)
1233 sig.closures.add(finalizer_clos)
1234
1235 var clos = new MMClosure(sig, n_kwbreak != null, n_expr != null)
1236 v.signature_builder = old_signature_builder
1237 _position = old_signature_builder.closure_decls.length
1238 old_signature_builder.closure_decls.add(self)
1239 _variable = new ClosureVariable(n_id.to_symbol, self, clos)
1240 end
1241 end
1242
1243 redef class AType
1244 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1245 private fun check_visibility(v: AbsSyntaxVisitor, p: MMLocalProperty)
1246 do
1247 if p.global.visibility_level >= 3 then return
1248 var t = get_stype(v)
1249 if t == null then return
1250 var bc = t.local_class
1251 if bc.global.visibility_level >= 3 then
1252 v.error(self, "Access error: Class {bc} is private and cannot be used in the signature of the non-private property {p}.")
1253 end
1254 for n in n_types do
1255 n.check_visibility(v, p)
1256 end
1257 end
1258 end
1259
1260 redef class AExpr
1261 redef fun accept_class_builder(v) do end
1262 redef fun accept_property_builder(v) do end
1263 redef fun accept_property_verifier(v) do end
1264 end