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