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