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