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