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