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