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