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