rename 'package' to 'module'
[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 module 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 v.error(self, "Redef error: Expected {isrt}, as in {ip.local_class}::{ip}.")
1037 end
1038 end
1039
1040 if visibility_level != 1 and glob.visibility_level != visibility_level then
1041 v.error(self, "Redef error: {prop.local_class}::{prop} redefinition cannot change visibility.")
1042 end
1043 glob.check_visibility(v, self, v.mmmodule, true)
1044 end
1045 end
1046
1047 redef class AAttrPropdef
1048 redef readable var _readmethod: nullable MMSrcMethod
1049 redef readable var _writemethod: nullable MMSrcMethod
1050 var _prop: nullable MMSrcAttribute
1051 redef fun prop do return _prop.as(not null)
1052
1053 redef fun accept_property_builder(v)
1054 do
1055 super
1056 var name: Symbol
1057 if n_id != null then
1058 name = n_id.to_symbol
1059 else
1060 name = ("@" + n_id2.text).to_symbol
1061 end
1062 var lc = v.local_class
1063 var prop = new MMSrcAttribute(name, lc, self)
1064 _prop = prop
1065 v.local_class.add_src_local_property(v, prop)
1066
1067 if n_readable != null or n_id == null then
1068 if n_id != null then
1069 name = n_id.text.substring_from(1).to_symbol
1070 else
1071 name = n_id2.to_symbol
1072 end
1073 var readmethod = new MMReadImplementationMethod(name, lc, self)
1074 _readmethod = readmethod
1075 v.local_class.add_src_local_property(v, readmethod)
1076 end
1077 if n_writable != null or n_id == null then
1078 if n_id != null then
1079 name = (n_id.text.substring_from(1) + "=").to_symbol
1080 else
1081 name = (n_id2.text + "=").to_symbol
1082 end
1083 var writemethod = new MMWriteImplementationMethod(name, lc, self)
1084 _writemethod = writemethod
1085 v.local_class.add_src_local_property(v, writemethod)
1086 end
1087 end
1088
1089 redef fun accept_property_verifier(v)
1090 do
1091 super
1092 var t: nullable MMType = null
1093 if n_type != null then
1094 var t0 = n_type.get_stype(v)
1095 if t0 != null then t = t0 else return
1096 else if n_expr != null then
1097 t = n_expr.get_easy_stype(v)
1098 end
1099
1100 if t == null then
1101 v.error(self, "Not yet implemented: Attribute definition {prop.local_class}::{prop} requires an explicit type.")
1102 return
1103 end
1104
1105 var prop = prop
1106 var signature = new MMSignature(new Array[MMParam], t, v.local_class.get_type)
1107 prop.signature = signature
1108 var visibility_level = n_visibility.level
1109 process_and_check(v, prop, n_id != null and n_kwredef != null, visibility_level)
1110 if n_readable != null or n_id == null then
1111 var m = _readmethod.as(not null)
1112 m.signature = signature
1113 process_and_check(v, m, (n_readable != null and n_readable.n_kwredef != null) or (n_id == null and n_kwredef != null), visibility_level)
1114 if n_type != null then n_type.check_visibility(v, m)
1115 end
1116 if n_writable != null or n_id == null then
1117 var m = _writemethod.as(not null)
1118 m.signature = new MMSignature(new Array[MMParam].with_items(new MMParam(t, once "value".to_symbol)), null, v.local_class.get_type)
1119 var vl = visibility_level
1120 if n_id == null then
1121 if n_writable == null then vl = 3 else vl = n_writable.n_visibility.level # write accessor has a specific visibility
1122 end
1123 process_and_check(v, m, n_writable != null and n_writable.n_kwredef != null, vl)
1124 if n_type != null then n_type.check_visibility(v, m)
1125 end
1126 end
1127
1128 redef fun accept_abs_syntax_visitor(v)
1129 do
1130 v.local_property = _prop
1131 super
1132 v.local_property = null
1133 end
1134 end
1135
1136 redef class AMethPropdef
1137 # Name of the method
1138 readable var _name: nullable Symbol
1139
1140 var _method: nullable MMMethSrcMethod
1141 redef fun method do return _method.as(not null)
1142
1143 redef fun accept_property_builder(v)
1144 do
1145 super
1146 var name: Symbol
1147 if n_methid == null then
1148 if self isa AInitPropdef then
1149 name = once "init".to_symbol
1150 else
1151 name = once "main".to_symbol
1152 end
1153 else
1154 name = n_methid.name.as(not null)
1155 # FIXME: Add the 'unary' keyword
1156 if n_methid.name == (once "-".to_symbol) then
1157 var ns = n_signature
1158 if ns != null and ns.n_params.length == 0 then
1159 name = once "unary -".to_symbol
1160 end
1161 end
1162 end
1163 _name = name
1164 var prop = new MMMethSrcMethod(name, v.local_class, self)
1165 _method = prop
1166 v.local_class.add_src_local_property(v, prop)
1167 end
1168
1169 redef fun accept_property_verifier(v)
1170 do
1171 v.signature_builder = new SignatureBuilder
1172 super
1173
1174 if v.signature_builder.has_error_occured then return
1175
1176 if v.signature_builder.signature == null then
1177 #_method.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
1178 else
1179 method.signature = v.signature_builder.signature.as(not null)
1180 end
1181 var visibility_level = 1
1182 if n_visibility != null and n_visibility.level > 1 then
1183 visibility_level = n_visibility.level
1184 end
1185 process_and_check(v, method, n_kwredef != null, visibility_level)
1186 if n_signature != null then n_signature.check_visibility(v, method)
1187 end
1188
1189 redef fun accept_abs_syntax_visitor(v)
1190 do
1191 v.local_property = _method
1192 super
1193 v.local_property = null
1194 end
1195 end
1196
1197 redef class AMainMethPropdef
1198 redef fun process_and_check(v, prop, has_redef, visibility_level)
1199 do
1200 prop.global.visibility_level = visibility_level
1201 prop.signature = new MMSignature(new Array[MMParam], null, v.local_class.get_type)
1202 # Disable all checks for main
1203 end
1204 end
1205
1206 redef class AExternPropdef
1207 redef fun accept_property_verifier(v)
1208 do
1209 super # Compute signature
1210 var ename: String
1211 if n_extern != null then
1212 ename = n_extern.text
1213 ename = ename.substring(1, ename.length-2)
1214 else
1215 ename = method.default_extern_name
1216 end
1217 method.extern_name = ename
1218 end
1219 end
1220
1221 redef class ATypePropdef
1222 redef fun prop do return _prop.as(not null)
1223 var _prop: nullable MMSrcTypeProperty
1224
1225 redef fun accept_property_builder(v)
1226 do
1227 super
1228 var name = n_id.to_symbol
1229 var prop = new MMSrcTypeProperty(name, v.local_class, self)
1230 _prop = prop
1231 v.local_class.add_src_local_property(v, prop)
1232 end
1233
1234 redef fun accept_property_verifier(v)
1235 do
1236 super
1237 var signature = new MMSignature(new Array[MMParam], n_type.get_stype(v), v.local_class.get_type)
1238 prop.signature = signature
1239 var visibility_level = n_visibility.level
1240 process_and_check(v, prop, n_kwredef != null, visibility_level)
1241 end
1242
1243 redef fun accept_abs_syntax_visitor(v)
1244 do
1245 v.local_property = _prop
1246 super
1247 v.local_property = null
1248 end
1249 end
1250
1251 # Visitor used to build a full method name from multiple tokens
1252 private class MethidAccumulator
1253 super Visitor
1254 readable var _name: Buffer = new Buffer
1255 redef fun visit(n)
1256 do
1257 if n isa Token then
1258 _name.append(n.text)
1259 else
1260 n.visit_all(self)
1261 end
1262 end
1263 end
1264
1265 redef class AMethid
1266 redef readable var _name: nullable Symbol
1267
1268 redef fun accept_property_builder(v)
1269 do
1270 var accumulator = new MethidAccumulator
1271 accumulator.enter_visit(self)
1272 _name = accumulator.name.to_s.to_symbol
1273 super
1274 end
1275 end
1276
1277 redef class ASignature
1278 redef fun accept_property_verifier(v)
1279 do
1280 super
1281 if v.signature_builder.has_error_occured then
1282 return
1283 else if not v.signature_builder.untyped_params.is_empty then
1284 if v.signature_builder.untyped_params.first != v.signature_builder.params.first or n_type != null then
1285 v.error(v.signature_builder.untyped_params.first, "Syntax error: untyped parameter.")
1286 return
1287 end
1288 else if not v.signature_builder.params.is_empty or n_type != null then
1289 var pars = new Array[MMParam]
1290 for p in v.signature_builder.params do
1291 pars.add( new MMParam( p.stype.as(not null), p.n_id.to_symbol ) )
1292 end
1293 var ret: nullable MMType = null
1294 if n_type != null then
1295 ret = n_type.get_stype(v)
1296 if ret == null then
1297 v.signature_builder.has_error_occured = true
1298 return
1299 end
1300 end
1301 v.signature_builder.signature = new MMSignature(pars, ret, v.local_class.get_type)
1302 if v.signature_builder.vararg_rank >= 0 then
1303 v.signature_builder.signature.vararg_rank = v.signature_builder.vararg_rank
1304 end
1305 for clos in v.signature_builder.closure_decls do
1306 v.signature_builder.signature.closures.add(clos.variable.closure)
1307 end
1308 end
1309 end
1310
1311 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1312 fun check_visibility(v: AbsSyntaxVisitor, p: MMLocalProperty)
1313 do
1314 if p.global.visibility_level >= 3 then return
1315 for n in n_params do
1316 if n.n_type != null then n.n_type.check_visibility(v, p)
1317 end
1318 if n_type != null then n_type.check_visibility(v, p)
1319 end
1320 end
1321
1322 redef class AParam
1323 redef readable var _position: Int = 0
1324
1325 redef fun variable: ParamVariable do return _variable.as(not null)
1326 var _variable: nullable ParamVariable
1327
1328 # The type of the parameter in signature
1329 readable writable var _stype: nullable MMType
1330
1331 redef fun accept_property_verifier(v)
1332 do
1333 super
1334 _position = v.signature_builder.params.length
1335 _variable = new ParamVariable(n_id.to_symbol, n_id)
1336 v.signature_builder.params.add(self)
1337 v.signature_builder.untyped_params.add(self)
1338 if n_type != null then
1339 var stype = n_type.get_stype(v)
1340 if stype == null then
1341 v.signature_builder.has_error_occured = true
1342 return
1343 end
1344 for p in v.signature_builder.untyped_params do
1345 p.stype = stype
1346 if is_vararg then
1347 if v.signature_builder.vararg_rank == -1 then
1348 v.signature_builder.vararg_rank = p.position
1349 else
1350 v.error(self, "Error: A vararg parameter is already defined.")
1351 end
1352 stype = v.type_array(stype)
1353 end
1354 p.variable.stype = stype
1355 end
1356 v.signature_builder.untyped_params.clear
1357 end
1358 end
1359
1360 fun is_vararg: Bool do return n_dotdotdot != null
1361 end
1362
1363 redef class AClosureDecl
1364 redef readable var _position: Int = 0
1365
1366 redef fun variable: ClosureVariable do return _variable.as(not null)
1367 var _variable: nullable ClosureVariable
1368
1369 redef fun accept_property_verifier(v)
1370 do
1371 var old_signature_builder = v.signature_builder
1372 v.signature_builder = new SignatureBuilder
1373 super
1374 if v.signature_builder.has_error_occured then
1375 return
1376 end
1377 var sig = v.signature_builder.signature
1378 if sig == null then
1379 sig = new MMSignature(new Array[MMParam], null, v.local_class.get_type)
1380 end
1381 if sig.return_type != null and n_kwbreak != null then
1382 v.error(self, "Syntax Error: A break block cannot have a return value.")
1383 end
1384
1385 # Add the finalizer to the closure signature
1386 var finalize_sig = new MMSignature(new Array[MMParam], null, v.mmmodule.type_any) # FIXME should be no receiver
1387 var finalizer_clos = new MMClosure(once ("break".to_symbol), finalize_sig, false, true)
1388 sig.closures.add(finalizer_clos)
1389
1390 var name = n_id.to_symbol
1391 var clos = new MMClosure(name, sig, n_kwbreak != null, n_expr != null)
1392 for c in old_signature_builder.closure_decls do
1393 if c.n_id.to_symbol == name then
1394 v.error(n_id, "Error: A closure '!{name}' already defined at {c.n_id.location.relative_to(n_id.location)}.")
1395 return
1396 end
1397 end
1398 v.signature_builder = old_signature_builder
1399 _position = old_signature_builder.closure_decls.length
1400 old_signature_builder.closure_decls.add(self)
1401 _variable = new ClosureVariable(n_id.to_symbol, n_id, clos)
1402 end
1403 end
1404
1405 redef class AType
1406 # Check that visibilities of types in the signature are compatible with the visibility of the property.
1407 private fun check_visibility(v: AbsSyntaxVisitor, p: MMLocalProperty)
1408 do
1409 if p.global.visibility_level >= 3 then return
1410 var t = get_stype(v)
1411 if t == null then return
1412 var bc = t.local_class
1413 if bc.global.visibility_level >= 3 then
1414 v.error(self, "Access error: Class {bc} is private and cannot be used in the signature of the non-private property {p}.")
1415 end
1416 for n in n_types do
1417 n.check_visibility(v, p)
1418 end
1419 end
1420 end
1421
1422 redef class AExpr
1423 redef fun accept_class_builder(v) do end
1424 redef fun accept_property_builder(v) do end
1425 redef fun accept_property_verifier(v) do end
1426
1427 private fun get_easy_stype(v:PropertyVerifierVisitor) : nullable MMType do return null
1428 end
1429
1430 redef class ABoolExpr
1431 redef fun get_easy_stype(v) do return v.type_bool
1432 end
1433
1434 redef class AStringExpr
1435 redef fun get_easy_stype(v) do return v.type_string
1436 end
1437
1438 redef class ACharExpr
1439 redef fun get_easy_stype(v) do return v.type_char
1440 end
1441
1442 redef class AIntExpr
1443 redef fun get_easy_stype(v) do return v.type_int
1444 end
1445
1446 redef class AFloatExpr
1447 redef fun get_easy_stype(v) do return v.type_float
1448 end
1449
1450 redef class ANewExpr
1451 redef fun get_easy_stype(v) do return n_type.get_stype(v)
1452 end