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