Extract get_signature from process_signature.
[nit.git] / src / syntax / typing.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 # Analysis property bodies, statements and expressions
18 package typing
19
20 import syntax_base
21
22 redef class MMSrcModule
23 # Walk trough the module and type statments and expressions
24 # Require than supermodules are processed
25 meth do_typing(tc: ToolContext)
26 do
27 var tv = new TypingVisitor(tc, self)
28 tv.visit(node)
29 end
30 end
31
32 # Typing visitor
33 # * Associate local variables to nodes
34 # * Distinguish method call and local variable access
35 # * Resolve call and attribute access
36 # * Check type conformance
37 private class TypingVisitor
38 special AbsSyntaxVisitor
39 redef meth visit(n)
40 do
41 if n != null then n.accept_typing(self)
42 end
43
44 # Current knowledge about variables names and types
45 readable writable attr _variable_ctx: VariableContext
46
47 # Type of the receiver
48 readable writable attr _self_type: MMType
49
50 # Block of the current method
51 readable writable attr _top_block: PExpr
52
53 # List of explicit invocation of constructors of super-classes
54 readable writable attr _explicit_super_init_calls: Array[MMMethod]
55
56 # Is a other constructor of the same class invoked
57 readable writable attr _explicit_other_init_call: Bool
58
59 init(tc, module) do super
60
61 private meth get_default_constructor_for(n: PNode, c: MMLocalClass, prop: MMSrcMethod): MMMethod
62 do
63 var v = self
64 #var prop = v.local_property
65 #assert prop isa MMMethod
66 var candidates = new Array[MMMethod]
67 var false_candidates = new Array[MMMethod]
68 var parity = prop.signature.arity
69 for g in c.global_properties do
70 if not g.is_init then continue
71 if g.intro.local_class != c then continue
72 var gp = c[g]
73 assert gp isa MMSrcMethod
74 var garity = gp.signature.arity
75 if prop != null and gp.name == prop.name then
76 if garity == 0 or (parity == garity and prop.signature < gp.signature) then
77 return gp
78 else
79 false_candidates.add(gp)
80 end
81 else if garity == 0 and gp.name == once ("init".to_symbol) then
82 candidates.add(gp)
83 false_candidates.add(gp)
84 else
85 false_candidates.add(gp)
86 end
87 end
88 if candidates.length == 1 then
89 return candidates.first
90 else if candidates.length > 0 then
91 v.error(n, "Error: Conflicting default constructor to call for {c}: {candidates.join(", ")}.")
92 return null
93 else if false_candidates.length > 0 then
94 v.error(n, "Error: there is no available compatible constrctor in {c}. discarded candidates are {false_candidates.join(", ")}.")
95 return null
96 else
97 v.warning(n, "Error: there is no available compatible constrctor in {c}.")
98 return null
99 end
100 end
101 end
102
103 # Associate symbols to variable and variables to type
104 # Can be nested
105 private class VariableContext
106 # Look for the variable from its name
107 # Return null if nothing found
108 meth [](s: Symbol): Variable
109 do
110 if _dico.has_key(s) then
111 return _dico[s]
112 else
113 return null
114 end
115 end
116
117 # Register a new variable with its name
118 meth add(v: Variable)
119 do
120 _dico[v.name] = v
121 end
122
123
124 # The effective static type of a given variable
125 # May be different from the declaration static type
126 meth stype(v: Variable): MMType
127 do
128 return v.stype
129 end
130
131 # Variables by name (in the current context only)
132 attr _dico: Map[Symbol, Variable]
133
134 # Build a new VariableContext
135 meth sub: SubVariableContext
136 do
137 return new SubVariableContext.with_prev(self, null, null)
138 end
139
140 # Build a nested VariableContext with new variable information
141 meth sub_with(v: Variable, t: MMType): SubVariableContext
142 do
143 return new SubVariableContext.with_prev(self, v, t)
144 end
145
146 init
147 do
148 _dico = new HashMap[Symbol, Variable]
149 end
150 end
151
152 private class SubVariableContext
153 special VariableContext
154 readable attr _prev: VariableContext
155 attr _variable: Variable
156 attr _var_type: MMType
157
158 redef meth [](s)
159 do
160 if _dico.has_key(s) then
161 return _dico[s]
162 else
163 return prev[s]
164 end
165 end
166
167 redef meth stype(v)
168 do
169 if _variable == v then
170 return _var_type
171 end
172 return prev.stype(v)
173 end
174
175 init with_prev(p: VariableContext, v: Variable, t: MMType)
176 do
177 init
178 _prev = p
179 _variable = v
180 _var_type =t
181 end
182 end
183
184
185 ###############################################################################
186
187 redef class PNode
188 private meth accept_typing(v: TypingVisitor)
189 do
190 accept_abs_syntax_visitor(v)
191 after_typing(v)
192 end
193 private meth after_typing(v: TypingVisitor) do end
194 end
195
196 redef class PClassdef
197 redef meth accept_typing(v)
198 do
199 v.self_type = local_class.get_type
200 super
201 end
202 end
203
204 redef class AAttrPropdef
205 redef meth accept_typing(v)
206 do
207 super
208 if n_expr != null then
209 v.check_conform(n_expr, n_expr.stype, prop.signature.return_type)
210 end
211 end
212 end
213
214 redef class AMethPropdef
215 redef meth accept_typing(v)
216 do
217 v.variable_ctx = new VariableContext
218 super
219 end
220 end
221
222 redef class AConcreteInitPropdef
223 readable attr _super_init_calls: Array[MMMethod] = new Array[MMMethod]
224 readable attr _explicit_super_init_calls: Array[MMMethod] = new Array[MMMethod]
225 redef meth accept_typing(v)
226 do
227 v.top_block = n_block
228 v.explicit_super_init_calls = explicit_super_init_calls
229 v.explicit_other_init_call = false
230 super
231 if v.explicit_other_init_call then
232 # TODO: something?
233 else
234 var i = 0
235 var l = explicit_super_init_calls.length
236 var cur_m: MMMethod = null
237 var cur_c: MMLocalClass = null
238 if i < l then
239 cur_m = explicit_super_init_calls[i]
240 cur_c = cur_m.global.intro.local_class
241 end
242 var j = 0
243 while j < v.local_class.cshe.direct_greaters.length do
244 var c = v.local_class.cshe.direct_greaters[j]
245 if c.global.is_interface or c.global.is_universal then
246 j += 1
247 else if cur_c != null and c.cshe <= cur_c then
248 if c == cur_c then j += 1
249 super_init_calls.add(cur_m)
250 i += 1
251 if i < l then
252 cur_m = explicit_super_init_calls[i]
253 cur_c = cur_m.global.intro.local_class
254 else
255 cur_m = null
256 cur_c = null
257 end
258 else
259 var p = v.get_default_constructor_for(self, c, method)
260 if p != null then
261 super_init_calls.add(p)
262 end
263 j += 1
264 end
265 end
266 end
267 end
268 end
269
270 redef class PParam
271 redef meth after_typing(v)
272 do
273 if v.variable_ctx != null then
274 v.variable_ctx.add(variable)
275 end
276 end
277 end
278
279 redef class PType
280 readable attr _stype: MMType
281 redef meth after_typing(v)
282 do
283 _stype = get_stype(v)
284 end
285 end
286
287 redef class PExpr
288 redef readable attr _stype: MMType
289
290 # Is the expression the implicit receiver
291 meth is_implicit_self: Bool do return false
292
293 # Is the expression the current receiver (implicit or explicit)
294 meth is_self: Bool do return false
295
296 # Is the expression a variable access
297 meth is_variable: Bool do return false
298
299 # The variable type information if current boolean expression is true
300 readable private attr _if_true_variable_ctx: VariableContext
301 end
302
303 redef class AVardeclExpr
304 redef meth after_typing(v)
305 do
306 var va = new VarVariable(n_id.to_symbol, self)
307 variable = va
308 v.variable_ctx.add(va)
309
310 if n_type != null then
311 va.stype = n_type.stype
312 if n_expr != null then
313 v.check_conform(self, n_expr.stype, va.stype)
314 end
315 else
316 va.stype = n_expr.stype
317 end
318 end
319 end
320
321 redef class ABlockExpr
322 redef meth accept_typing(v)
323 do
324 var old_var_ctx = v.variable_ctx
325 v.variable_ctx = v.variable_ctx.sub
326
327 super
328
329 v.variable_ctx = old_var_ctx
330 end
331 end
332
333 redef class AReturnExpr
334 redef meth after_typing(v)
335 do
336 var t = v.local_property.signature.return_type
337 if n_expr == null and t != null then
338 v.error(self, "Error: Return without value in a function.")
339 else if n_expr != null and t == null then
340 v.error(self, "Error: Return with value in a procedure.")
341 else if n_expr != null and t != null then
342 v.check_conform(self, n_expr.stype, t)
343 end
344 end
345 end
346
347 redef class AIfExpr
348 redef meth accept_typing(v)
349 do
350 var old_var_ctx = v.variable_ctx
351 v.visit(n_expr)
352 v.check_conform(self, n_expr.stype, v.type_bool)
353
354 if n_expr.if_true_variable_ctx != null then
355 v.variable_ctx = n_expr.if_true_variable_ctx
356 end
357
358 v.visit(n_then)
359 # Restore variable ctx
360 v.variable_ctx = old_var_ctx
361
362 if n_else != null then
363 v.visit(n_else)
364 v.variable_ctx = old_var_ctx
365 end
366 end
367 end
368
369 redef class AWhileExpr
370 redef meth after_typing(v)
371 do
372 v.check_conform(self, n_expr.stype, v.type_bool)
373 end
374 end
375
376 redef class AForExpr
377 redef meth after_typing(v)
378 do
379 # pop context created in AForVardeclExpr
380 var varctx = v.variable_ctx
381 assert varctx isa SubVariableContext
382 v.variable_ctx = varctx.prev
383 end
384 end
385
386 redef class AForVardeclExpr
387 redef meth after_typing(v)
388 do
389 v.variable_ctx = v.variable_ctx.sub
390 var va = new AutoVariable(n_id.to_symbol, self)
391 variable = va
392 v.variable_ctx.add(va)
393
394 var expr_type = n_expr.stype
395 if not v.check_conform(self, expr_type, v.type_collection) then
396 return
397 end
398 var prop = expr_type.local_class.select_method(once ("iterator".to_symbol))
399 if prop == null then
400 v.error(self, "Error: Collection MUST have an iterate method")
401 return
402 end
403 var iter_type = prop.signature_for(expr_type).return_type
404 var prop2 = iter_type.local_class.select_method(once ("item".to_symbol))
405 if prop2 == null then
406 v.error(self, "Error: {iter_type} MUST have an item method")
407 return
408 end
409 var t = prop2.signature_for(iter_type).return_type
410 if not n_expr.is_self then t = t.not_for_self
411 va.stype = t
412 end
413 end
414
415 redef class AAssertExpr
416 redef meth after_typing(v)
417 do
418 v.check_conform(self, n_expr.stype, v.type_bool)
419 if n_expr.if_true_variable_ctx != null then v.variable_ctx = n_expr.if_true_variable_ctx
420 end
421 end
422
423 redef class AVarExpr
424 redef meth is_variable do return true
425
426 redef meth after_typing(v)
427 do
428 _stype = v.variable_ctx.stype(variable)
429 end
430 end
431
432 redef class AVarAssignExpr
433 redef meth after_typing(v)
434 do
435 var t = v.variable_ctx.stype(variable)
436 v.check_conform(self, n_value.stype, t)
437 end
438 end
439
440 redef class AReassignFormExpr
441 # Compute and check method used through the reassigment operator
442 private meth do_lvalue_typing(v: TypingVisitor, type_lvalue: MMType)
443 do
444 if type_lvalue == null then
445 return
446 end
447 var name = n_assign_op.method_name
448 var prop = type_lvalue.local_class.select_method(name)
449 if prop == null then
450 v.error(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
451 return
452 end
453 prop.global.check_visibility(v, self, v.module, false)
454 var psig = prop.signature_for(type_lvalue)
455 _assign_method = prop
456 v.check_conform(n_value, n_value.stype, psig[0].not_for_self)
457 v.check_conform(self, psig.return_type.not_for_self, n_value.stype)
458 end
459
460 # Method used through the reassigment operator (once computed)
461 readable attr _assign_method: MMMethod
462 end
463
464 redef class AVarReassignExpr
465 redef meth after_typing(v)
466 do
467 var t = v.variable_ctx.stype(variable)
468 do_lvalue_typing(v, t)
469 end
470 end
471
472 redef class PAssignOp
473 meth method_name: Symbol is abstract
474 end
475 redef class APlusAssignOp
476 redef meth method_name do return once "+".to_symbol
477 end
478 redef class AMinusAssignOp
479 redef meth method_name do return once "-".to_symbol
480 end
481
482 redef class ASelfExpr
483 redef meth after_typing(v)
484 do
485 assert v.self_type != null
486 _stype = v.self_type
487 end
488
489 redef meth is_self do return true
490 end
491
492 redef class AImplicitSelfExpr
493 redef meth is_implicit_self do return true
494 end
495
496 redef class AIfexprExpr
497 redef meth accept_typing(v)
498 do
499 var old_var_ctx = v.variable_ctx
500
501 v.visit(n_expr)
502 if n_expr.if_true_variable_ctx != null then v.variable_ctx = n_expr.if_true_variable_ctx
503 v.visit(n_then)
504 v.variable_ctx = old_var_ctx
505 v.visit(n_else)
506
507 v.check_conform(self, n_expr.stype, v.type_bool)
508
509 var t = n_then.stype
510 var te = n_else.stype
511 if t < te then
512 t = te
513 else if not te < t then
514 v.error(self, "Type error: {te} is not a subtype of {t}.")
515 return
516 end
517
518 _stype = t
519 end
520 end
521
522 redef class ABoolExpr
523 redef meth after_typing(v)
524 do
525 _stype = v.type_bool
526 end
527 end
528
529 redef class AOrExpr
530 redef meth after_typing(v)
531 do
532 v.check_conform(self, n_expr.stype, v.type_bool)
533 v.check_conform(self, n_expr2.stype, v.type_bool)
534 _stype = v.type_bool
535 end
536 end
537
538 redef class AAndExpr
539 redef meth accept_typing(v)
540 do
541 var old_var_ctx = v.variable_ctx
542
543 v.visit(n_expr)
544 if n_expr.if_true_variable_ctx != null then v.variable_ctx = n_expr.if_true_variable_ctx
545
546 v.visit(n_expr2)
547 if n_expr2.if_true_variable_ctx != null then
548 _if_true_variable_ctx = n_expr2.if_true_variable_ctx
549 else
550 _if_true_variable_ctx = v.variable_ctx
551 end
552
553 v.variable_ctx = old_var_ctx
554
555 v.check_conform(self, n_expr.stype, v.type_bool)
556 v.check_conform(self, n_expr2.stype, v.type_bool)
557 _stype = v.type_bool
558 end
559 end
560
561 redef class ANotExpr
562 redef meth after_typing(v)
563 do
564 v.check_conform(self, n_expr.stype, v.type_bool)
565 _stype = v.type_bool
566 end
567 end
568
569 redef class AIntExpr
570 redef meth after_typing(v)
571 do
572 _stype = v.type_int
573
574 end
575 end
576
577 redef class AFloatExpr
578 redef meth after_typing(v)
579 do
580 _stype = v.type_float
581 end
582 end
583
584 redef class ACharExpr
585 redef meth after_typing(v)
586 do
587 _stype = v.type_char
588 end
589 end
590
591 redef class AStringFormExpr
592 redef meth after_typing(v)
593 do
594 _stype = v.type_string
595 end
596 end
597
598 redef class ASuperstringExpr
599 redef meth after_typing(v)
600 do
601 _stype = v.type_string
602 end
603 end
604
605 redef class ANullExpr
606 redef meth after_typing(v)
607 do
608 _stype = v.type_none
609 end
610 end
611
612 redef class AArrayExpr
613 private meth stype=(t: MMType) do _stype = t
614
615 redef meth after_typing(v)
616 do
617 var stype: MMType = null
618 for n in n_exprs do
619 var ntype = n.stype
620 if stype == null or (ntype != null and stype < ntype) then
621 stype = ntype
622 end
623 end
624 for n in n_exprs do
625 v.check_conform(self, n.stype, stype)
626 end
627 _stype = v.type_array(stype)
628 end
629 end
630
631 redef class ARangeExpr
632 redef meth after_typing(v)
633 do
634 var ntype = n_expr.stype
635 var ntype2 = n_expr2.stype
636 if ntype == null or ntype == null then
637 return
638 end
639 if ntype < ntype2 then
640 ntype = ntype2
641 else if not ntype2 < ntype then
642 v.error(self, "Type error: {ntype} incompatible with {ntype2}.")
643 return
644 end
645 var dtype = v.type_discrete
646 v.check_conform(self, ntype, dtype)
647 _stype = v.type_range(ntype)
648 end
649 end
650
651 redef class ASuperExpr
652 special ASuperInitCall
653 # readable attr _prop: MMSrcMethod
654 readable attr _init_in_superclass: MMMethod
655 redef meth after_typing(v)
656 do
657 var precs: Array[MMLocalProperty] = v.local_property.prhe.direct_greaters
658 if not precs.is_empty then
659 v.local_property.need_super = true
660 else if v.local_property.global.is_init then
661 var base_precs = v.local_class.super_methods_named(v.local_property.name)
662 for p in base_precs do
663 if not p.global.is_init then
664 v.error(self, "Error: {p.local_class}::{p} is not a constructor.")
665 else
666 precs.add(v.local_class[p.global])
667 end
668 end
669 if precs.is_empty then
670 v.error(self, "Error: No contructor named {v.local_property.name} in superclasses.")
671 return
672 else if precs.length > 1 then
673 v.error(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
674 return
675 end
676 var p = base_precs.first
677 assert p isa MMMethod
678 _init_in_superclass = p
679 register_super_init_call(v, p)
680 if n_args.length > 0 then
681 var signature = get_signature(v, v.self_type, p, true)
682 _arguments = process_signature(v, signature, p, n_args.to_a)
683 end
684 else
685 v.error(self, "Error: No super method to call for {v.local_property}.")
686 return
687 end
688
689 if precs.first.signature_for(v.self_type).return_type != null then
690 var stypes = new Array[MMType]
691 var stype: MMType = null
692 for prop in precs do
693 assert prop isa MMMethod
694 var t = prop.signature_for(v.self_type).return_type.for_module(v.module).adapt_to(v.local_property.signature.recv)
695 stypes.add(t)
696 if stype == null or stype < t then
697 stype = t
698 end
699 end
700 for t in stypes do
701 v.check_conform(self, t, stype)
702 end
703 _stype = stype
704 end
705 var p = v.local_property
706 assert p isa MMSrcMethod
707 _prop = p
708 end
709 end
710
711 redef class AAttrFormExpr
712 # Attribute accessed
713 readable attr _prop: MMAttribute
714
715 # Attribute type of the acceded attribute
716 readable attr _attr_type: MMType
717
718 # Compute the attribute accessed
719 private meth do_typing(v: TypingVisitor)
720 do
721 var type_recv = n_expr.stype
722 if type_recv == null then
723 return
724 end
725 var name = n_id.to_symbol
726 var prop = type_recv.local_class.select_attribute(name)
727 if prop == null then
728 v.error(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
729 return
730 else if v.module.visibility_for(prop.global.local_class.module) < 3 then
731 v.error(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
732 end
733 _prop = prop
734 var at = prop.signature_for(type_recv).return_type
735 if not n_expr.is_self then at = at.not_for_self
736 _attr_type = at
737 end
738 end
739
740 redef class AAttrExpr
741 redef meth after_typing(v)
742 do
743 do_typing(v)
744 if prop == null then
745 return
746 end
747 _stype = attr_type
748 end
749 end
750
751 redef class AAttrAssignExpr
752 redef meth after_typing(v)
753 do
754 do_typing(v)
755 if prop == null then
756 return
757 end
758 v.check_conform(self, n_value.stype, attr_type)
759 end
760 end
761
762 redef class AAttrReassignExpr
763 redef meth after_typing(v)
764 do
765 do_typing(v)
766 if prop == null then
767 return
768 end
769 do_lvalue_typing(v, attr_type)
770 end
771 end
772
773 class AAbsSendExpr
774 special PExpr
775 # Compute the called global property
776 private meth do_typing(v: TypingVisitor, type_recv: MMType, is_implicit_self: Bool, recv_is_self: Bool, name: Symbol, raw_args: Array[PExpr])
777 do
778 var prop = get_property(v, type_recv, is_implicit_self, name)
779 if prop == null then return
780 var sig = get_signature(v, type_recv, prop, recv_is_self)
781 if sig == null then return
782 var args = process_signature(v, sig, prop, raw_args)
783 if args == null then return
784 _prop = prop
785 _arguments = args
786 end
787
788 private meth get_property(v: TypingVisitor, type_recv: MMType, is_implicit_self: Bool, name: Symbol): MMMethod
789 do
790 if type_recv == null then return null
791 var prop = type_recv.local_class.select_method(name)
792 if prop == null and v.local_property.global.is_init then
793 var props = type_recv.local_class.super_methods_named(name)
794 if props.length > 1 then
795 v.error(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
796 return null
797 else if props.length == 1 then
798 var p = type_recv.local_class[props.first.global]
799 assert p isa MMMethod
800 prop = p
801 end
802
803 end
804 if prop == null then
805 if is_implicit_self then
806 v.error(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
807 else
808 v.error(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
809 end
810 return null
811 end
812 return prop
813 end
814
815 private meth get_signature(v: TypingVisitor, type_recv: MMType, prop: MMMethod, recv_is_self: Bool): MMSignature
816 do
817 prop.global.check_visibility(v, self, v.module, recv_is_self)
818 var psig = prop.signature_for(type_recv)
819 if not recv_is_self then psig = psig.not_for_self
820 return psig
821 end
822
823 # Check the conformity of a set of arguments `raw_args' to a signature.
824 private meth process_signature(v: TypingVisitor, psig: MMSignature, prop: MMMethod, raw_args: Array[PExpr]): Array[PExpr]
825 do
826 var par_vararg = psig.vararg_rank
827 var par_arity = psig.arity
828 var raw_arity: Int
829 if raw_args == null then raw_arity = 0 else raw_arity = raw_args.length
830 if par_arity > raw_arity or (par_arity != raw_arity and par_vararg == -1) then
831 v.error(self, "Error: Method '{prop}' arity missmatch.")
832 return null
833 end
834 var arg_idx = 0
835 var args = new Array[PExpr]
836 for par_idx in [0..par_arity[ do
837 var a: PExpr
838 var par_type = psig[par_idx]
839 if par_idx == par_vararg then
840 var star = new Array[PExpr]
841 for i in [0..(raw_arity-par_arity)] do
842 a = raw_args[arg_idx]
843 v.check_conform(self, a.stype, par_type)
844 star.add(a)
845 arg_idx = arg_idx + 1
846 end
847 var aa = new AArrayExpr.init_aarrayexpr(star)
848 aa.stype = v.type_array(par_type)
849 a = aa
850 else
851 a = raw_args[arg_idx]
852 v.check_conform(self, a.stype, par_type)
853 arg_idx = arg_idx + 1
854 end
855 args.add(a)
856 end
857 return args
858 end
859
860 # The invoked method (once computed)
861 readable attr _prop: MMMethod
862
863 # The real arguments used (after star transformation) (once computed)
864 readable attr _arguments: Array[PExpr]
865 end
866
867 # A possible call of constructor in a super class
868 # Could be an explicit call or with the 'super' keyword
869 class ASuperInitCall
870 special AAbsSendExpr
871 private meth register_super_init_call(v: TypingVisitor, property: MMMethod)
872 do
873 if parent != v.top_block and self != v.top_block then
874 v.error(self, "Error: Constructor invocation {property} must not be in nested block.")
875 end
876 var cla = v.module[property.global.intro.local_class.global]
877 var prev_class: MMLocalClass = null
878 if not v.explicit_super_init_calls.is_empty then
879 prev_class = v.explicit_super_init_calls.last.global.intro.local_class
880 end
881 var order = v.local_class.cshe.reverse_linear_extension
882 if cla == v.local_class then
883 v.explicit_other_init_call = true
884 else if not order.has(cla) then
885 v.error(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
886 else if cla == prev_class then
887 v.error(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
888 else
889 var last_is_found = prev_class == null
890 for c in order do
891 if c == prev_class then
892 last_is_found = true
893 else if c == cla then
894 if not last_is_found then
895 v.error(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
896 end
897 v.explicit_super_init_calls.add(property)
898 break
899 end
900 end
901 end
902 end
903
904 end
905
906 redef class ANewExpr
907 special AAbsSendExpr
908 redef meth after_typing(v)
909 do
910 var t = n_type.stype
911 if t == null then return
912 if t.local_class.global.is_abstract then
913 v.error(self, "Error: try to instantiate abstract class {t.local_class}.")
914 return
915 end
916 var name: Symbol
917 if n_id == null then
918 name = once "init".to_symbol
919 else
920 name = n_id.to_symbol
921 end
922
923 do_typing(v, t, false, false, name, n_args.to_a)
924 if prop == null then return
925
926 if not prop.global.is_init then
927 v.error(self, "Error: {prop} is not a constructor.")
928 end
929 _stype = t
930 end
931 end
932
933
934 redef class ASendExpr
935 special ASuperInitCall
936 # Name of the invoked property
937 meth name: Symbol is abstract
938
939 # Raw arguments used (withour star transformation)
940 meth raw_arguments: Array[PExpr] is abstract
941
942 redef meth after_typing(v)
943 do
944 do_all_typing(v)
945 end
946
947 private meth do_all_typing(v: TypingVisitor)
948 do
949 do_typing(v, n_expr.stype, n_expr.is_implicit_self, n_expr.is_self, name, raw_arguments)
950 if prop == null then return
951 if prop.global.is_init then
952 if not v.local_property.global.is_init then
953 v.error(self, "Error: try to invoke constructor {prop} in a method.")
954 else if not n_expr.is_self then
955 v.error(self, "Error: constructor {prop} is not invoken on 'self'.")
956 else
957 register_super_init_call(v, prop)
958 end
959 end
960 var t = prop.signature_for(n_expr.stype).return_type
961 if t != null and not n_expr.is_self then t = t.not_for_self
962 _stype = t
963 end
964 end
965
966 class ASendReassignExpr
967 special ASendExpr
968 special AReassignFormExpr
969 readable attr _read_prop: MMMethod
970 redef meth do_all_typing(v)
971 do
972 var raw_args = raw_arguments
973 do_typing(v, n_expr.stype, n_expr.is_implicit_self, n_expr.is_self, name, raw_args)
974 if prop == null then return
975 if prop.global.is_init then
976 if not v.local_property.global.is_init then
977 v.error(self, "Error: try to invoke constructor {prop} in a method.")
978 else if not n_expr.is_self then
979 v.error(self, "Error: constructor {prop} is not invoken on 'self'.")
980 end
981 end
982 var t = prop.signature_for(n_expr.stype).return_type
983 if not n_expr.is_self then t = t.not_for_self
984
985 do_lvalue_typing(v, t)
986
987 _read_prop = prop
988 var old_args = arguments
989 raw_args.add(n_value)
990
991 do_typing(v, n_expr.stype, n_expr.is_implicit_self, n_expr.is_self, "{name}=".to_symbol, raw_args)
992 if prop == null then return
993 if prop.global.is_init then
994 if not v.local_property.global.is_init then
995 v.error(self, "Error: try to invoke constructor {prop} in a method.")
996 else if not n_expr.is_self then
997 v.error(self, "Error: constructor {prop} is not invoken on 'self'.")
998 end
999 end
1000
1001 _arguments = old_args # FIXME: What if star parameters do not match betwen the two methods?
1002 end
1003 end
1004
1005 redef class ABinopExpr
1006 redef meth raw_arguments do return [n_expr2]
1007 end
1008 redef class AEqExpr
1009 redef meth name do return once "==".to_symbol
1010 end
1011 redef class ANeExpr
1012 redef meth name do return once "!=".to_symbol
1013 end
1014 redef class ALtExpr
1015 redef meth name do return once "<".to_symbol
1016 end
1017 redef class ALeExpr
1018 redef meth name do return once "<=".to_symbol
1019 end
1020 redef class AGtExpr
1021 redef meth name do return once ">".to_symbol
1022 end
1023 redef class AGeExpr
1024 redef meth name do return once ">=".to_symbol
1025 end
1026 redef class APlusExpr
1027 redef meth name do return once "+".to_symbol
1028 end
1029 redef class AMinusExpr
1030 redef meth name do return once "-".to_symbol
1031 end
1032 redef class AStarshipExpr
1033 redef meth name do return once "<=>".to_symbol
1034 end
1035 redef class AStarExpr
1036 redef meth name do return once "*".to_symbol
1037 end
1038 redef class ASlashExpr
1039 redef meth name do return once "/".to_symbol
1040 end
1041 redef class APercentExpr
1042 redef meth name do return once "%".to_symbol
1043 end
1044
1045 redef class AUminusExpr
1046 redef meth name do return once "unary -".to_symbol
1047 redef meth raw_arguments do return null
1048 end
1049
1050 redef class ACallFormExpr
1051 redef meth after_typing(v)
1052 do
1053 if n_expr.is_implicit_self then
1054 var name = n_id.to_symbol
1055 var variable = v.variable_ctx[name]
1056 if variable != null then
1057 if not n_args.is_empty then
1058 v.error(self, "Error: {name} is variable, not a function.")
1059 end
1060 var vform = variable_create(variable)
1061 vform.variable = variable
1062 replace_with(vform)
1063 vform.after_typing(v)
1064 return
1065 end
1066 end
1067 super
1068 end
1069
1070 # Create a variable acces corresponding to the call form
1071 meth variable_create(variable: Variable): AVarFormExpr is abstract
1072 end
1073
1074 redef class ACallExpr
1075 redef meth variable_create(variable)
1076 do
1077 return new AVarExpr.init_avarexpr(n_id)
1078 end
1079
1080 redef meth name do return n_id.to_symbol
1081 redef meth raw_arguments do return n_args.to_a
1082 end
1083
1084 redef class ACallAssignExpr
1085 redef meth variable_create(variable)
1086 do
1087 return new AVarAssignExpr.init_avarassignexpr(n_id, n_assign, n_value)
1088 end
1089
1090 redef meth name do return (n_id.text + "=").to_symbol
1091 redef meth raw_arguments do
1092 var res = n_args.to_a
1093 res.add(n_value)
1094 return res
1095 end
1096 end
1097
1098 redef class ACallReassignExpr
1099 special ASendReassignExpr
1100 redef meth variable_create(variable)
1101 do
1102 return new AVarReassignExpr.init_avarreassignexpr(n_id, n_assign_op, n_value)
1103 end
1104
1105 redef meth name do return n_id.to_symbol
1106 redef meth raw_arguments do return n_args.to_a
1107 end
1108
1109 redef class ABraExpr
1110 redef meth name do return once "[]".to_symbol
1111 redef meth raw_arguments do return n_args.to_a
1112 end
1113
1114 redef class ABraAssignExpr
1115 redef meth name do return once "[]=".to_symbol
1116 redef meth raw_arguments do
1117 var res = n_args.to_a
1118 res.add(n_value)
1119 return res
1120 end
1121 end
1122
1123 redef class ABraReassignExpr
1124 special ASendReassignExpr
1125 redef meth name do return once "[]".to_symbol
1126 redef meth raw_arguments do return n_args.to_a
1127 end
1128
1129 redef class AInitExpr
1130 redef meth name do return once "init".to_symbol
1131 redef meth raw_arguments do return n_args.to_a
1132 end
1133
1134 redef class AIsaExpr
1135 redef meth after_typing(v)
1136 do
1137 if n_expr.is_variable then
1138 var n = n_expr
1139 assert n isa AVarExpr
1140 _if_true_variable_ctx = v.variable_ctx.sub_with(n.variable, n_type.stype)
1141 end
1142 _stype = v.type_bool
1143 end
1144 end
1145
1146 redef class AAsCastExpr
1147 redef meth after_typing(v)
1148 do
1149 _stype = n_type.stype
1150 var et = n_expr.stype
1151 end
1152 end
1153
1154 redef class AProxyExpr
1155 redef meth after_typing(v)
1156 do
1157 _stype = n_expr.stype
1158 end
1159 end