metamodel: rename 'universal' to 'enum'
[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.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[MMType]
296 if base != null then
297 var sig = base.signature
298 for i in [0..sig.arity[ do
299 params.add(sig[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(sig.return_type.as(not null))
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 special 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 special 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 special 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 special 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 special 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 special 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 special 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 module_names_to_import = new Array[Symbol]
430 var module_visibility = new HashMap[Symbol, Int]
431 var no_import: nullable AImport = null
432 for i in n_imports do
433 var n = i.module_name
434 if n != null then
435 module_names_to_import.add(n)
436 module_visibility[n] = i.visibility_level
437 else
438 no_import = i
439 end
440 end
441 if no_import != null then
442 if not module_names_to_import.is_empty then
443 tc.error(no_import.location, "Error: Top modules cannot import other modules.")
444 end
445 else if module_names_to_import.is_empty then
446 var stdname = once "standard".to_symbol
447 module_names_to_import.add(stdname)
448 module_visibility[stdname] = 1
449 end
450
451 mod.import_supers_modules(module_names_to_import)
452
453 for mname in module_names_to_import do
454 var level = module_visibility[mname]
455 var m = tc.get_module(mname, mod)
456 mod.add_super_module(m, level)
457 end
458 end
459 end
460
461 redef class AModuledecl
462 redef fun accept_class_builder(v)
463 do
464 if n_id.to_symbol != v.mmmodule.name then
465 v.error(n_id, "Error: Module name missmatch between {v.mmmodule.name} and {n_id.to_symbol}")
466 end
467 end
468 end
469
470 redef class AImport
471 # Imported module name (or null)
472 fun module_name: nullable Symbol is abstract
473
474 # Visibility level (intrude/public/private)
475 fun visibility_level: Int is abstract
476 end
477 redef class AStdImport
478 redef fun module_name
479 do
480 return n_id.to_symbol
481 end
482 redef fun visibility_level
483 do
484 return n_visibility.level
485 end
486 end
487 redef class ANoImport
488 redef fun module_name
489 do
490 return null
491 end
492 end
493
494 redef class AVisibility
495 # Visibility level
496 fun level: Int is abstract
497 end
498 redef class APublicVisibility
499 redef fun level do return 1
500 end
501 redef class AProtectedVisibility
502 redef fun level do return 2
503 end
504 redef class APrivateVisibility
505 redef fun level do return 3
506 end
507 redef class AIntrudeVisibility
508 redef fun level do return 0
509 end
510
511
512 redef class AClassdef
513 redef fun local_class: MMSrcLocalClass do return _local_class.as(not null)
514 var _local_class: nullable MMSrcLocalClass
515
516 # Name of the class
517 fun name: Symbol is abstract
518
519 # Number of formal parameters
520 fun arity: Int do return 0
521
522 # Visibility of the class
523 fun visibility_level: Int do return 1
524
525 redef fun accept_class_builder(v)
526 do
527 var local_class: MMSrcLocalClass
528 var mod = v.mmmodule
529 var local_classes = mod.src_local_classes
530 if (local_classes.has_key(name)) then
531 local_class = local_classes[name]
532 if self isa AStdClassdef then
533 # If we are not a special implicit class then rant
534 v.error(self, "Error: A class {name} is already defined at line {local_class.node.location.line_start}.")
535 return
536 end
537 # Add the new node after the last node
538 var n = local_class.node
539 while n.next_node != null do n = n.next_node
540 n.next_node = self
541 else
542 local_class = new MMSrcLocalClass(mod, name, self, arity)
543 local_classes[name] = local_class
544 if not mod.has_global_class_named(name) then
545 local_class.new_global
546 else
547 local_class.set_global(mod.global_class_named(name))
548 end
549
550 end
551 _local_class = local_class
552 v.local_class_arity = 0
553 v.formals = local_class.formal_dict
554
555 #####
556 super
557 #####
558
559 v.formals = null
560 end
561
562 redef fun accept_abs_syntax_visitor(v)
563 do
564 v.local_class = _local_class
565 super
566 v.local_class = null
567 end
568 end
569
570 redef class AClasskind
571 fun is_interface: Bool do return false
572 fun is_enum: Bool do return false
573 fun is_abstract: Bool do return false
574 end
575
576 redef class AInterfaceClasskind
577 redef fun is_interface do return true
578 end
579 redef class AEnumClasskind
580 redef fun is_enum do return true
581 end
582 redef class AAbstractClasskind
583 redef fun is_abstract do return true
584 end
585
586 redef class AStdClassdef
587 redef fun name
588 do
589 return n_id.to_symbol
590 end
591 redef fun arity
592 do
593 return n_formaldefs.length
594 end
595 redef fun accept_class_verifier(v)
596 do
597 super
598 var glob = _local_class.global
599 if glob.intro == _local_class then
600 # Intro
601 glob.visibility_level = visibility_level
602 glob.is_interface = n_classkind.is_interface
603 glob.is_abstract = n_classkind.is_abstract
604 glob.is_enum = n_classkind.is_enum
605 if n_kwredef != null then
606 v.error(self, "Redef error: No class {name} is imported. Remove the redef keyword to define a new class.")
607 end
608
609 for c in _local_class.cshe.direct_greaters do
610 var cg = c.global
611 if glob.is_interface then
612 if cg.is_enum then
613 v.error(self, "Special error: Interface {name} try to specialise enum class {c.name}.")
614 else if not cg.is_interface then
615 v.error(self, "Special error: Interface {name} try to specialise class {c.name}.")
616 end
617 else if glob.is_enum then
618 if not cg.is_interface and not cg.is_enum then
619 v.error(self, "Special error: Enum class {name} try to specialise class {c.name}.")
620 end
621 else
622 if cg.is_enum then
623 v.error(self, "Special error: Class {name} try to specialise enum class {c.name}.")
624 end
625 end
626
627 end
628 return
629 end
630
631 # Redef
632
633 glob.check_visibility(v, self, v.mmmodule)
634 if n_kwredef == null then
635 v.error(self, "Redef error: {name} is an imported class. Add the redef keyword to refine it.")
636 return
637 end
638
639 if glob.intro.arity != _local_class.arity then
640 v.error(self, "Redef error: Formal parameter arity missmatch; got {_local_class.arity}, expected {glob.intro.arity}.")
641 end
642
643 if
644 not glob.is_interface and n_classkind.is_interface or
645 not glob.is_abstract and n_classkind.is_abstract or
646 not glob.is_enum and n_classkind.is_enum
647 then
648 v.error(self, "Redef error: cannot change kind of class {name}.")
649 end
650 end
651
652 redef fun visibility_level
653 do
654 return n_visibility.level
655 end
656 end
657
658 redef class AMainClassdef
659 redef fun name
660 do
661 return once "Sys".to_symbol
662 end
663 end
664
665 redef class ATopClassdef
666 redef fun name
667 do
668 return once "Object".to_symbol
669 end
670 end
671
672 redef class AFormaldef
673 # The associated formal generic parameter (MM entity)
674 var _formal: nullable MMTypeFormalParameter
675
676 redef fun accept_class_builder(v)
677 do
678 var name = n_id.to_symbol
679 var formal_type = new MMTypeFormalParameter(name, v.local_class_arity, v.local_class)
680 _formal = formal_type
681 v.local_class_arity = v.local_class_arity + 1
682 v.local_class.register_formal(formal_type)
683 v.formals[name] = formal_type
684 super
685 end
686
687 redef fun accept_class_verifier(v)
688 do
689 super
690 var c = v.local_class
691 var o = c.global.intro
692 if c == o then
693 if n_type == null then
694 _formal.bound = v.mmmodule.type_any.as_nullable
695 else
696 var stype = n_type.get_stype(v)
697 if stype == null then return
698 _formal.bound = stype
699 end
700 else
701 var ob = o.get_formal(_formal.position).bound.for_module(v.mmmodule)
702 if n_type == null then
703 _formal.bound = ob
704 else
705 var stype = n_type.get_stype(v)
706 if stype == null then return
707 _formal.bound = stype
708 if _formal.bound != ob then
709 v.error(self, "Redef error: Cannot change formal parameter type of class {c}; got {_formal.bound}, expected {ob}.")
710 end
711 end
712 end
713 end
714 end
715
716 redef class ASuperclass
717 readable var _ancestor: nullable MMSrcAncestor
718
719 redef fun accept_class_specialization_builder(v)
720 do
721 super
722 var c = n_type.get_local_class(v)
723 if c == null then return
724 var ancestor = new MMSrcAncestor(c)
725 _ancestor = ancestor
726 v.local_class.add_direct_parent(ancestor)
727 end
728
729 redef fun accept_class_ancestor_builder(v)
730 do
731 super
732 _ancestor.stype = n_type.get_unchecked_stype(v)
733 _ancestor.inheriter = v.local_class.get_type
734 end
735
736 redef fun accept_class_verifier(v)
737 do
738 super
739 n_type.check_conform(v)
740 end
741 end
742
743 redef class APropdef
744 # Process and check properties of the property.
745 # * Distinguish inits and methods
746 # * Inherit or check visibility.
747 # * Check redef errors.
748 # * Check forbiden attribute definitions.
749 # * Check signature conformance.
750 private fun process_and_check(v: PropertyVerifierVisitor, prop: MMLocalProperty, has_redef: Bool, visibility_level: Int)
751 do
752 if prop.global.intro == prop then
753 do_and_check_intro(v, prop, has_redef, visibility_level)
754 else
755 do_and_check_redef(v, prop, has_redef, visibility_level)
756 end
757 end
758
759 # The part of process_and_check when prop is an introduction
760 private fun do_and_check_intro(v: PropertyVerifierVisitor, prop: MMLocalProperty, has_redef: Bool, visibility_level: Int)
761 do
762 var glob = prop.global
763 var gbc = prop.local_class.global
764 if v.local_class.global.visibility_level >= 3 then
765 # Method of private classes are private
766 visibility_level = 3
767 end
768 glob.visibility_level = visibility_level
769 if has_redef then
770 v.error(self, "Error: No property {prop.local_class}::{prop} is inherited. Remove the redef keyword to define a new property.")
771 end
772 if glob.is_attribute then
773 if gbc.is_interface then
774 v.error(self, "Error: Attempt to define attribute {prop} in the interface {prop.local_class}.")
775 else if gbc.is_enum then
776 v.error(self, "Error: Attempt to define attribute {prop} in the enum class {prop.local_class}.")
777 end
778 else if glob.is_init then
779 if gbc.is_interface then
780 v.error(self, "Error: Attempt to define a constructor {prop} in the class {prop.local_class}.")
781 else if gbc.is_enum then
782 v.error(self, "Error: Attempt to define a constructor {prop} in the enum {prop.local_class}.")
783 end
784 end
785 if prop.signature == null then
786 if glob.is_init then
787 var supers = prop.local_class.super_methods_named(prop.name)
788 inherit_signature(v, prop, supers)
789 end
790 if prop.signature != null or v.signature_builder.has_error_occured then
791 # ok
792 else if not v.signature_builder.untyped_params.is_empty then
793 v.error(v.signature_builder.untyped_params.first, "Error: Untyped parameter.")
794 else
795 prop.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
796 for clos in v.signature_builder.closure_decls do
797 prop.signature.closures.add(clos.variable.closure)
798 end
799 end
800 end
801 end
802
803 private fun inherit_signature(v: PropertyVerifierVisitor, prop: MMLocalProperty, supers: Array[MMLocalProperty])
804 do
805 var s = prop.signature
806 for ip in supers do
807 var isig = ip.signature.adaptation_to(v.local_class.get_type)
808
809 if s == null then
810 if v.signature_builder.params.length != isig.arity then
811 return
812 end
813 for p in v.signature_builder.params do
814 var t = isig[p.position]
815 p.stype = t
816 if p.position == isig.vararg_rank then
817 t = v.type_array(t)
818 end
819 p.variable.stype = t
820 end
821
822 s = isig
823 prop.signature = s
824 end
825 end
826 end
827
828 # The part of process_and_check when prop is a redefinition
829 private fun do_and_check_redef(v: PropertyVerifierVisitor, prop: MMLocalProperty, has_redef: Bool, visibility_level: Int)
830 do
831 var is_init = self isa AConcreteInitPropdef
832 var glob = prop.global
833
834 if not has_redef then
835 v.error(self, "Redef error: {prop.local_class}::{prop} is an inherited property. To redefine it, add the redef keyword.")
836 return
837 end
838 if glob.is_init and not is_init then
839 v.error(self, "Redef error: A method {prop.local_class}::{prop} cannot redefine a constructor.")
840 else if not glob.is_init and is_init then
841 v.error(self, "Redef error: A constructor {prop.local_class}::{prop} cannot redefine a method.")
842 end
843
844 var s = prop.signature
845 #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}"
846 for i in prop.prhe.direct_greaters do
847 var ip = i.local_class[prop.global]
848 var isig = i.signature.adaptation_to(v.local_class.get_type)
849
850 if s == null then
851 #print "{prop.full_name} inherits signature from {ip.full_name}"
852 if v.signature_builder.params.length != isig.arity then
853 v.error(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
854 return
855 end
856 if v.signature_builder.closure_decls.length != isig.closures.length then
857 v.error(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} closure(s).")
858 return
859 end
860 for p in v.signature_builder.params do
861 var t = isig[p.position]
862 p.stype = t
863 if p.position == isig.vararg_rank then
864 t = v.type_array(t)
865 end
866 p.variable.stype = t
867 end
868 s = isig
869 prop.signature = s
870 #print "s is null"
871 end
872
873 var nberr = v.tc.error_count
874 #print "Check {prop.local_class}::{prop}{s} vs {ip.local_class}::{ip}{isig}"
875 #print "s={s.object_id} isig={isig.object_id} isigorig={i.signature.object_id}"
876
877 #print "orig signature: {i.signature.recv} . {i.signature}"
878 #print "inh signature: {isig.recv} . {isig}"
879 #print "redef signature: {s.recv} . {s}"
880
881 if s.arity != isig.arity then
882 v.error(self, "Redef error: {prop.local_class}::{prop} redefines {ip.local_class}::{ip} with {isig.arity} parameter(s).")
883 else
884 for j in [0..s.arity[ do
885 if s[j] != isig[j] then
886 v.error(self, "Redef error: Expected {isig[j]} (as in {ip.local_class}::{ip}), got {s[j]} in {prop.local_class}::{prop}.")
887 end
888 end
889 end
890
891 var srt = s.return_type
892 var isrt = isig.return_type
893 if srt == null and isrt != null then
894 v.error(self, "Redef error: The procedure {prop.local_class}::{prop} redefines the function {ip.local_class}::{ip}.")
895 else if srt != null and isrt == null then
896 v.error(self, "Redef error: The function {prop.local_class}::{prop} redefines the procedure {ip.local_class}::{ip}.")
897 else if srt != null and isrt != null and not srt < isrt then
898 v.error(self, "Redef error: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
899 else if not s < isig and nberr == v.tc.error_count then
900 # Systematic fallback for conformance check
901 v.error(self, "Redef error: Incompatible redefinition of {ip.local_class}::{ip} with {prop.local_class}::{prop}")
902 else if srt != null and isrt != null and srt != isrt and prop isa MMAttribute then
903 # FIXME: To remove
904 v.warning(self, "Redef warning: Expected {isrt} (as in {ip.local_class}::{ip}), got {srt} in {prop.local_class}::{prop}.")
905 end
906 end
907
908 if visibility_level != 1 and glob.visibility_level != visibility_level then
909 v.error(self, "Redef error: {prop.local_class}::{prop} redefinition cannot change visibility.")
910 end
911 glob.check_visibility(v, self, v.mmmodule, true)
912 end
913 end
914
915 redef class AAttrPropdef
916 redef readable var _readmethod: nullable MMSrcMethod
917 redef readable var _writemethod: nullable MMSrcMethod
918 var _prop: nullable MMSrcAttribute
919 redef fun prop do return _prop.as(not null)
920
921 redef fun accept_property_builder(v)
922 do
923 super
924 var name: Symbol
925 if n_id != null then
926 name = n_id.to_symbol
927 else
928 name = ("@" + n_id2.text).to_symbol
929 end
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 or n_id == null then
936 if n_id != null then
937 name = n_id.text.substring_from(1).to_symbol
938 else
939 name = n_id2.to_symbol
940 end
941 var readmethod = new MMReadImplementationMethod(name, lc, self)
942 _readmethod = readmethod
943 v.local_class.add_src_local_property(v, readmethod)
944 end
945 if n_writable != null or n_id == null then
946 if n_id != null then
947 name = (n_id.text.substring_from(1) + "=").to_symbol
948 else
949 name = (n_id2.text + "=").to_symbol
950 end
951 var writemethod = new MMWriteImplementationMethod(name, lc, self)
952 _writemethod = writemethod
953 v.local_class.add_src_local_property(v, writemethod)
954 end
955 end
956
957 redef fun accept_property_verifier(v)
958 do
959 super
960 var t: MMType
961 if n_type != null then
962 var t0 = n_type.get_stype(v)
963 if t0 != null then t = t0 else return
964 else
965 v.error(self, "Not yet implemented: Attribute definition {prop.local_class}::{prop} requires an explicit type.")
966 return
967 end
968
969 var prop = prop
970 var signature = new MMSignature(new Array[MMType], t, v.local_class.get_type)
971 prop.signature = signature
972 var visibility_level = n_visibility.level
973 process_and_check(v, prop, n_id != null and n_kwredef != null, visibility_level)
974 if n_readable != null or n_id == null then
975 var m = _readmethod.as(not null)
976 m.signature = signature
977 process_and_check(v, m, (n_readable != null and n_readable.n_kwredef != null) or (n_id == null and n_kwredef != null), visibility_level)
978 n_type.check_visibility(v, m)
979 end
980 if n_writable != null or n_id == null then
981 var m = _writemethod.as(not null)
982 m.signature = new MMSignature(new Array[MMType].with_items(t), null, v.local_class.get_type)
983 var vl = visibility_level
984 if n_id == null then
985 if n_writable == null then vl = 3 else vl = n_writable.n_visibility.level # write accessor has a specific visibility
986 end
987 process_and_check(v, m, n_writable != null and n_writable.n_kwredef != null, vl)
988 n_type.check_visibility(v, m)
989 end
990 end
991
992 redef fun accept_abs_syntax_visitor(v)
993 do
994 v.local_property = _prop
995 super
996 v.local_property = null
997 end
998 end
999
1000 redef class AMethPropdef
1001 # Name of the method
1002 readable var _name: nullable Symbol
1003
1004 var _method: nullable MMMethSrcMethod
1005 redef fun method do return _method.as(not null)
1006
1007 redef fun accept_property_builder(v)
1008 do
1009 super
1010 var name: Symbol
1011 if n_methid == null then
1012 if self isa AConcreteInitPropdef then
1013 name = once "init".to_symbol
1014 else
1015 name = once "main".to_symbol
1016 end
1017 else
1018 name = n_methid.name.as(not null)
1019 # FIXME: Add the 'unary' keyword
1020 if n_methid.name == (once "-".to_symbol) then
1021 var ns = n_signature
1022 if ns != null and ns.n_params.length == 0 then
1023 name = once "unary -".to_symbol
1024 end
1025 end
1026 end
1027 _name = name
1028 var prop = new MMMethSrcMethod(name, v.local_class, self)
1029 _method = prop
1030 v.local_class.add_src_local_property(v, prop)
1031 end
1032
1033 redef fun accept_property_verifier(v)
1034 do
1035 v.signature_builder = new SignatureBuilder
1036 super
1037
1038 if v.signature_builder.has_error_occured then return
1039
1040 if v.signature_builder.signature == null then
1041 #_method.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
1042 else
1043 method.signature = v.signature_builder.signature.as(not null)
1044 end
1045 var visibility_level = 1
1046 if n_visibility != null and n_visibility.level > 1 then
1047 visibility_level = n_visibility.level
1048 end
1049 process_and_check(v, method, n_kwredef != null, visibility_level)
1050 if n_signature != null then n_signature.check_visibility(v, method)
1051 end
1052
1053 redef fun accept_abs_syntax_visitor(v)
1054 do
1055 v.local_property = _method
1056 super
1057 v.local_property = null
1058 end
1059 end
1060
1061 redef class AMainMethPropdef
1062 redef fun process_and_check(v, prop, has_redef, visibility_level)
1063 do
1064 prop.global.visibility_level = visibility_level
1065 prop.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
1066 # Disable all checks for main
1067 end
1068 end
1069
1070 redef class AExternMethPropdef
1071 redef fun accept_property_verifier(v)
1072 do
1073 super # Compute signature
1074 var ename: String
1075 if n_extern != null then
1076 ename = n_extern.text
1077 ename = ename.substring(1, ename.length-2)
1078 else
1079 ename = "{method.mmmodule.name}_{method.local_class.name}_{method.local_class.name}_{method.name}_{method.signature.arity}"
1080 end
1081 method.extern_name = ename
1082 end
1083 end
1084
1085 redef class ATypePropdef
1086 redef fun prop do return _prop.as(not null)
1087 var _prop: nullable MMSrcTypeProperty
1088
1089 redef fun accept_property_builder(v)
1090 do
1091 super
1092 var name = n_id.to_symbol
1093 var prop = new MMSrcTypeProperty(name, v.local_class, self)
1094 _prop = prop
1095 v.local_class.add_src_local_property(v, prop)
1096 end
1097
1098 redef fun accept_property_verifier(v)
1099 do
1100 super
1101 var signature = new MMSignature(new Array[MMType], n_type.get_stype(v), v.local_class.get_type)
1102 prop.signature = signature
1103 var visibility_level = n_visibility.level
1104 process_and_check(v, prop, n_kwredef != null, visibility_level)
1105 end
1106
1107 redef fun accept_abs_syntax_visitor(v)
1108 do
1109 v.local_property = _prop
1110 super
1111 v.local_property = null
1112 end
1113 end
1114
1115 # Visitor used to build a full method name from multiple tokens
1116 private class MethidAccumulator
1117 special Visitor
1118 readable var _name: Buffer = new Buffer
1119 redef fun visit(n)
1120 do
1121 if n isa Token then
1122 _name.append(n.text)
1123 else
1124 n.visit_all(self)
1125 end
1126 end
1127 end
1128
1129 redef class AMethid
1130 # Method name
1131 readable var _name: nullable Symbol
1132
1133 redef fun accept_property_builder(v)
1134 do
1135 var accumulator = new MethidAccumulator
1136 accumulator.enter_visit(self)
1137 _name = accumulator.name.to_s.to_symbol
1138 super
1139 end
1140 end
1141
1142 redef class ASignature
1143 redef fun accept_property_verifier(v)
1144 do
1145 super
1146 if v.signature_builder.has_error_occured then
1147 return
1148 else if not v.signature_builder.untyped_params.is_empty then
1149 if v.signature_builder.untyped_params.first != v.signature_builder.params.first or n_type != null then
1150 v.error(v.signature_builder.untyped_params.first, "Syntax error: untyped parameter.")
1151 return
1152 end
1153 else if not v.signature_builder.params.is_empty or n_type != null then
1154 var pars = new Array[MMType]
1155 for p in v.signature_builder.params do
1156 pars.add(p.stype.as(not null))
1157 end
1158 var ret: nullable MMType = null
1159 if n_type != null then
1160 ret = n_type.get_stype(v)
1161 if ret == null then
1162 v.signature_builder.has_error_occured = true
1163 return
1164 end
1165 end
1166 v.signature_builder.signature = new MMSignature(pars, ret, v.local_class.get_type)
1167 if v.signature_builder.vararg_rank >= 0 then
1168 v.signature_builder.signature.vararg_rank = v.signature_builder.vararg_rank
1169 end
1170 for clos in v.signature_builder.closure_decls do
1171 v.signature_builder.signature.closures.add(clos.variable.closure)
1172 end
1173 end
1174 end
1175
1176 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1177 fun check_visibility(v: AbsSyntaxVisitor, p: MMLocalProperty)
1178 do
1179 if p.global.visibility_level >= 3 then return
1180 for n in n_params do
1181 if n.n_type != null then n.n_type.check_visibility(v, p)
1182 end
1183 if n_type != null then n_type.check_visibility(v, p)
1184 end
1185 end
1186
1187 redef class AParam
1188 redef readable var _position: Int = 0
1189
1190 redef fun variable: ParamVariable do return _variable.as(not null)
1191 var _variable: nullable ParamVariable
1192
1193 # The type of the parameter in signature
1194 readable writable var _stype: nullable MMType
1195
1196 redef fun accept_property_verifier(v)
1197 do
1198 super
1199 _position = v.signature_builder.params.length
1200 _variable = new ParamVariable(n_id.to_symbol, n_id)
1201 v.signature_builder.params.add(self)
1202 v.signature_builder.untyped_params.add(self)
1203 if n_type != null then
1204 var stype = n_type.get_stype(v)
1205 if stype == null then
1206 v.signature_builder.has_error_occured = true
1207 return
1208 end
1209 for p in v.signature_builder.untyped_params do
1210 p.stype = stype
1211 if is_vararg then
1212 if v.signature_builder.vararg_rank == -1 then
1213 v.signature_builder.vararg_rank = p.position
1214 else
1215 v.error(self, "Error: A vararg parameter is already defined.")
1216 end
1217 stype = v.type_array(stype)
1218 end
1219 p.variable.stype = stype
1220 end
1221 v.signature_builder.untyped_params.clear
1222 end
1223 end
1224
1225 fun is_vararg: Bool do return n_dotdotdot != null
1226 end
1227
1228 redef class AClosureDecl
1229 redef readable var _position: Int = 0
1230
1231 redef fun variable: ClosureVariable do return _variable.as(not null)
1232 var _variable: nullable ClosureVariable
1233
1234 redef fun accept_property_verifier(v)
1235 do
1236 var old_signature_builder = v.signature_builder
1237 v.signature_builder = new SignatureBuilder
1238 super
1239 if v.signature_builder.has_error_occured then
1240 return
1241 end
1242 var sig = v.signature_builder.signature
1243 if sig == null then
1244 sig = new MMSignature(new Array[MMType], null, v.local_class.get_type)
1245 end
1246 if sig.return_type != null and n_kwbreak != null then
1247 v.error(self, "Syntax Error: A break block cannot have a return value.")
1248 end
1249
1250 # Add the finalizer to the closure signature
1251 var finalize_sig = new MMSignature(new Array[MMType], null, v.mmmodule.type_any) # FIXME should be no receiver
1252 var finalizer_clos = new MMClosure(once ("break".to_symbol), finalize_sig, false, true)
1253 sig.closures.add(finalizer_clos)
1254
1255 var name = n_id.to_symbol
1256 var clos = new MMClosure(name, sig, n_kwbreak != null, n_expr != null)
1257 for c in old_signature_builder.closure_decls do
1258 if c.n_id.to_symbol == name then
1259 v.error(n_id, "A closure '!{name}' already defined at {c.n_id.location.relative_to(n_id.location)}.")
1260 return
1261 end
1262 end
1263 v.signature_builder = old_signature_builder
1264 _position = old_signature_builder.closure_decls.length
1265 old_signature_builder.closure_decls.add(self)
1266 _variable = new ClosureVariable(n_id.to_symbol, n_id, clos)
1267 end
1268 end
1269
1270 redef class AType
1271 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1272 private fun check_visibility(v: AbsSyntaxVisitor, p: MMLocalProperty)
1273 do
1274 if p.global.visibility_level >= 3 then return
1275 var t = get_stype(v)
1276 if t == null then return
1277 var bc = t.local_class
1278 if bc.global.visibility_level >= 3 then
1279 v.error(self, "Access error: Class {bc} is private and cannot be used in the signature of the non-private property {p}.")
1280 end
1281 for n in n_types do
1282 n.check_visibility(v, p)
1283 end
1284 end
1285 end
1286
1287 redef class AExpr
1288 redef fun accept_class_builder(v) do end
1289 redef fun accept_property_builder(v) do end
1290 redef fun accept_property_verifier(v) do end
1291 end