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