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