syntax: warning for useless 'isa' and 'as' type checks
[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 import escape
22
23 redef class MMSrcModule
24 # Walk trough the module and type statments and expressions
25 # Require than supermodules are processed
26 meth do_typing(tc: ToolContext)
27 do
28 var tv = new TypingVisitor(tc, self)
29 tv.visit(node)
30 end
31 end
32
33 # Typing visitor
34 # * Associate local variables to nodes
35 # * Distinguish method call and local variable access
36 # * Resolve call and attribute access
37 # * Check type conformance
38 private class TypingVisitor
39 special AbsSyntaxVisitor
40 redef meth visit(n)
41 do
42 if n != null then n.accept_typing(self)
43 end
44
45 # Current knowledge about variables names and types
46 readable writable attr _variable_ctx: VariableContext
47
48 # Current knowledge about escapable blocks
49 readable writable attr _escapable_ctx: EscapableContext = new EscapableContext(self)
50
51 # The current reciever
52 readable writable attr _self_var: ParamVariable
53
54 # Block of the current method
55 readable writable attr _top_block: PExpr
56
57 # List of explicit invocation of constructors of super-classes
58 readable writable attr _explicit_super_init_calls: Array[MMMethod]
59
60 # Is a other constructor of the same class invoked
61 readable writable attr _explicit_other_init_call: Bool
62
63 # Make the if_true_variable_ctx of the expression effective
64 private meth use_if_true_variable_ctx(e: PExpr)
65 do
66 var ctx = e.if_true_variable_ctx
67 if ctx != null then
68 variable_ctx = ctx
69 end
70 end
71
72 init(tc, module) do super
73
74 private meth get_default_constructor_for(n: PNode, c: MMLocalClass, prop: MMSrcMethod): MMMethod
75 do
76 var v = self
77 #var prop = v.local_property
78 #assert prop isa MMMethod
79 var candidates = new Array[MMMethod]
80 var false_candidates = new Array[MMMethod]
81 var parity = prop.signature.arity
82 for g in c.global_properties do
83 if not g.is_init_for(c) then continue
84 var gp = c[g]
85 var gps = gp.signature_for(c.get_type)
86 assert gp isa MMSrcMethod
87 var garity = gps.arity
88 if prop != null and gp.name == prop.name then
89 if garity == 0 or (parity == garity and prop.signature < gps) then
90 return gp
91 else
92 false_candidates.add(gp)
93 end
94 else if garity == 0 and gp.name == once ("init".to_symbol) then
95 candidates.add(gp)
96 false_candidates.add(gp)
97 else
98 false_candidates.add(gp)
99 end
100 end
101 if candidates.length == 1 then
102 return candidates.first
103 else if candidates.length > 0 then
104 var a = new Array[String]
105 for p in candidates do
106 a.add("{p.full_name}{p.signature}")
107 end
108 v.error(n, "Error: Conflicting default constructor to call for {c}: {a.join(", ")}.")
109 return null
110 else if false_candidates.length > 0 then
111 var a = new Array[String]
112 for p in false_candidates do
113 a.add("{p.full_name}{p.signature}")
114 end
115 v.error(n, "Error: there is no available compatible constrctor in {c}. Discarded candidates are {a.join(", ")}.")
116 return null
117 else
118 v.error(n, "Error: there is no available compatible constrctor in {c}.")
119 return null
120 end
121 end
122 end
123
124 # Associate symbols to variable and variables to type
125 # Can be nested
126 private class VariableContext
127 # Look for the variable from its name
128 # Return null if nothing found
129 meth [](s: Symbol): Variable
130 do
131 if _dico.has_key(s) then
132 return _dico[s]
133 else
134 return null
135 end
136 end
137
138 # Register a new variable with its name
139 meth add(v: Variable)
140 do
141 _dico[v.name] = v
142 end
143
144
145 # The effective static type of a given variable
146 # May be different from the declaration static type
147 meth stype(v: Variable): MMType
148 do
149 return v.stype
150 end
151
152 # Variables by name (in the current context only)
153 attr _dico: Map[Symbol, Variable]
154
155 # Build a new VariableContext
156 meth sub: SubVariableContext
157 do
158 return new SubVariableContext.with_prev(self)
159 end
160
161 # Build a nested VariableContext with new variable information
162 meth sub_with(v: Variable, t: MMType): SubVariableContext
163 do
164 return new CastVariableContext.with_prev(self, v, t)
165 end
166
167 init
168 do
169 _dico = new HashMap[Symbol, Variable]
170 end
171 end
172
173 private class SubVariableContext
174 special VariableContext
175 readable attr _prev: VariableContext
176
177 redef meth [](s)
178 do
179 if _dico.has_key(s) then
180 return _dico[s]
181 else
182 return prev[s]
183 end
184 end
185
186 redef meth stype(v)
187 do
188 return prev.stype(v)
189 end
190
191 init with_prev(p: VariableContext)
192 do
193 init
194 _prev = p
195 end
196 end
197
198 private class CastVariableContext
199 special SubVariableContext
200 attr _variable: Variable
201 attr _var_type: MMType
202
203 redef meth stype(v)
204 do
205 if _variable == v then
206 return _var_type
207 end
208 return prev.stype(v)
209 end
210
211 init with_prev(p: VariableContext, v: Variable, t: MMType)
212 do
213 super(p)
214 _variable = v
215 _var_type =t
216 end
217 end
218
219 ###############################################################################
220
221 redef class PNode
222 private meth accept_typing(v: TypingVisitor)
223 do
224 accept_abs_syntax_visitor(v)
225 after_typing(v)
226 end
227 private meth after_typing(v: TypingVisitor) do end
228 end
229
230 redef class PClassdef
231 redef meth accept_typing(v)
232 do
233 v.self_var = new ParamVariable("self".to_symbol, self)
234 v.self_var.stype = local_class.get_type
235 super
236 end
237 end
238
239 redef class AAttrPropdef
240 redef meth accept_typing(v)
241 do
242 super
243 if n_expr != null then
244 v.check_conform_expr(n_expr, prop.signature.return_type)
245 end
246 end
247 end
248
249 redef class AMethPropdef
250 redef readable attr _self_var: ParamVariable
251 redef meth accept_typing(v)
252 do
253 v.variable_ctx = new VariableContext
254 _self_var = v.self_var
255 super
256 end
257 end
258
259 redef class AConcreteInitPropdef
260 readable attr _super_init_calls: Array[MMMethod] = new Array[MMMethod]
261 readable attr _explicit_super_init_calls: Array[MMMethod] = new Array[MMMethod]
262 redef meth accept_typing(v)
263 do
264 v.top_block = n_block
265 v.explicit_super_init_calls = explicit_super_init_calls
266 v.explicit_other_init_call = false
267 super
268 if v.explicit_other_init_call or method.global.intro != method then
269 # TODO: something?
270 else
271 var i = 0
272 var l = explicit_super_init_calls.length
273 var cur_m: MMMethod = null
274 var cur_c: MMLocalClass = null
275 if i < l then
276 cur_m = explicit_super_init_calls[i]
277 cur_c = cur_m.global.intro.local_class.for_module(v.module)
278 end
279 var j = 0
280 while j < v.local_class.cshe.direct_greaters.length do
281 var c = v.local_class.cshe.direct_greaters[j]
282 if c.global.is_interface or c.global.is_universal or c.global.is_mixin then
283 j += 1
284 else if cur_c != null and (c.cshe <= cur_c or cur_c.global.is_mixin) then
285 if c == cur_c then j += 1
286 super_init_calls.add(cur_m)
287 i += 1
288 if i < l then
289 cur_m = explicit_super_init_calls[i]
290 cur_c = cur_m.global.intro.local_class.for_module(v.module)
291 else
292 cur_m = null
293 cur_c = null
294 end
295 else
296 var p = v.get_default_constructor_for(self, c, method)
297 if p != null then
298 super_init_calls.add(p)
299 end
300 j += 1
301 end
302 end
303 end
304 end
305 end
306
307 redef class PParam
308 redef meth after_typing(v)
309 do
310 # TODO: why the test?
311 if v.variable_ctx != null then
312 v.variable_ctx.add(variable)
313 end
314 end
315 end
316
317 redef class AClosureDecl
318 # The corresponding escapable object
319 readable attr _escapable: EscapableBlock
320
321 redef meth accept_typing(v)
322 do
323 # Register the closure for ClosureCallExpr
324 v.variable_ctx.add(variable)
325
326 var old_var_ctx = v.variable_ctx
327 v.variable_ctx = v.variable_ctx.sub
328
329 _escapable = new EscapableClosure(self, variable.closure, null)
330 v.escapable_ctx.push(_escapable)
331
332 super
333
334 v.variable_ctx = old_var_ctx
335 v.escapable_ctx.pop
336 end
337 end
338
339 redef class PType
340 readable attr _stype: MMType
341 redef meth after_typing(v)
342 do
343 _stype = get_stype(v)
344 end
345 end
346
347 redef class PExpr
348 redef readable attr _is_typed: Bool = false
349 redef meth is_statement: Bool do return _stype == null
350 redef meth stype
351 do
352 if not is_typed then
353 print "{locate}: not is_typed"
354 abort
355 end
356 if is_statement then
357 print "{locate}: is_statement"
358 abort
359 end
360 return _stype
361 end
362 attr _stype: MMType
363
364 # Is the expression the implicit receiver
365 meth is_implicit_self: Bool do return false
366
367 # Is the expression the current receiver (implicit or explicit)
368 meth is_self: Bool do return false
369
370 # The variable accessed is any
371 meth its_variable: Variable do return null
372
373 # The variable type information if current boolean expression is true
374 readable private attr _if_true_variable_ctx: VariableContext
375 end
376
377 redef class AVardeclExpr
378 redef meth after_typing(v)
379 do
380 var va = new VarVariable(n_id.to_symbol, self)
381 variable = va
382 v.variable_ctx.add(va)
383
384 if n_type != null then
385 va.stype = n_type.stype
386 if n_expr != null then
387 v.check_conform_expr(n_expr, va.stype)
388 end
389 else
390 if not v.check_expr(n_expr) then return
391 va.stype = n_expr.stype
392 end
393 _is_typed = true
394 end
395 end
396
397 redef class ABlockExpr
398 redef meth accept_typing(v)
399 do
400 var old_var_ctx = v.variable_ctx
401 v.variable_ctx = v.variable_ctx.sub
402
403 super
404
405 v.variable_ctx = old_var_ctx
406 _is_typed = true
407 end
408 end
409
410 redef class AReturnExpr
411 redef meth after_typing(v)
412 do
413 var t = v.local_property.signature.return_type
414 if n_expr == null and t != null then
415 v.error(self, "Error: Return without value in a function.")
416 else if n_expr != null and t == null then
417 v.error(self, "Error: Return with value in a procedure.")
418 else if n_expr != null and t != null then
419 v.check_conform_expr(n_expr, t)
420 end
421 _is_typed = true
422 end
423 end
424
425 redef class AContinueExpr
426 redef meth after_typing(v)
427 do
428 var esc = compute_escapable_block(v.escapable_ctx)
429 if esc == null then return
430
431 if esc.is_break_block then
432 v.error(self, "Error: 'continue' forbiden in break blocks.")
433 return
434 end
435
436 var t = esc.continue_stype
437 if n_expr == null and t != null then
438 v.error(self, "Error: continue with a value required in this block.")
439 else if n_expr != null and t == null then
440 v.error(self, "Error: continue without value required in this block.")
441 else if n_expr != null and t != null then
442 v.check_conform_expr(n_expr, t)
443 end
444 _is_typed = true
445 end
446 end
447
448 redef class ABreakExpr
449 redef meth after_typing(v)
450 do
451 var esc = compute_escapable_block(v.escapable_ctx)
452 if esc == null then return
453
454 var bl = esc.break_list
455 if n_expr == null and bl != null then
456 v.error(self, "Error: break with a value required in this block.")
457 else if n_expr != null and bl == null then
458 v.error(self, "Error: break without value required in this block.")
459 else if n_expr != null and bl != null then
460 # Typing check can only be done later
461 bl.add(n_expr)
462 end
463 _is_typed = true
464 end
465 end
466
467 redef class AIfExpr
468 redef meth accept_typing(v)
469 do
470 var old_var_ctx = v.variable_ctx
471 v.visit(n_expr)
472 v.check_conform_expr(n_expr, v.type_bool)
473
474 v.use_if_true_variable_ctx(n_expr)
475
476 v.visit(n_then)
477 # Restore variable ctx
478 v.variable_ctx = old_var_ctx
479
480 if n_else != null then
481 v.visit(n_else)
482 v.variable_ctx = old_var_ctx
483 end
484 _is_typed = true
485 end
486 end
487
488 redef class AWhileExpr
489 # The corresponding escapable block
490 readable attr _escapable: EscapableBlock
491
492 redef meth accept_typing(v)
493 do
494 _escapable = new EscapableBlock(self)
495 v.escapable_ctx.push(_escapable)
496
497 super
498
499 v.check_conform_expr(n_expr, v.type_bool)
500 v.escapable_ctx.pop
501 _is_typed = true
502 end
503 end
504
505 redef class AForExpr
506 # The corresponding escapable block
507 readable attr _escapable: EscapableBlock
508
509 readable attr _meth_iterator: MMMethod
510 readable attr _meth_is_ok: MMMethod
511 readable attr _meth_item: MMMethod
512 readable attr _meth_next: MMMethod
513 redef meth accept_typing(v)
514 do
515 _escapable = new EscapableBlock(self)
516 v.escapable_ctx.push(_escapable)
517
518 v.variable_ctx = v.variable_ctx.sub
519 var va = new AutoVariable(n_id.to_symbol, self)
520 variable = va
521 v.variable_ctx.add(va)
522
523 v.visit(n_expr)
524
525 if not v.check_conform_expr(n_expr, v.type_collection) then return
526 var expr_type = n_expr.stype
527 _meth_iterator = expr_type.local_class.select_method(once ("iterator".to_symbol))
528 if _meth_iterator == null then
529 v.error(self, "Error: Collection MUST have an iterate method")
530 return
531 end
532 var iter_type = _meth_iterator.signature_for(expr_type).return_type
533 _meth_is_ok = iter_type.local_class.select_method(once ("is_ok".to_symbol))
534 if _meth_is_ok == null then
535 v.error(self, "Error: {iter_type} MUST have an is_ok method")
536 return
537 end
538 _meth_item = iter_type.local_class.select_method(once ("item".to_symbol))
539 if _meth_item == null then
540 v.error(self, "Error: {iter_type} MUST have an item method")
541 return
542 end
543 _meth_next = iter_type.local_class.select_method(once ("next".to_symbol))
544 if _meth_next == null then
545 v.error(self, "Error: {iter_type} MUST have a next method")
546 return
547 end
548 var t = _meth_item.signature_for(iter_type).return_type
549 if not n_expr.is_self then t = t.not_for_self
550 va.stype = t
551
552 if n_block != null then v.visit(n_block)
553
554 # pop context
555 var varctx = v.variable_ctx
556 assert varctx isa SubVariableContext
557 v.variable_ctx = varctx.prev
558
559 v.escapable_ctx.pop
560 _is_typed = true
561 end
562 end
563
564 redef class AAssertExpr
565 redef meth after_typing(v)
566 do
567 v.check_conform_expr(n_expr, v.type_bool)
568 v.use_if_true_variable_ctx(n_expr)
569 _is_typed = true
570 end
571 end
572
573 redef class AVarExpr
574 redef meth its_variable do return variable
575
576 redef meth after_typing(v)
577 do
578 _stype = v.variable_ctx.stype(variable)
579 _is_typed = _stype != null
580 end
581 end
582
583 redef class AVarAssignExpr
584 redef meth after_typing(v)
585 do
586 var t = v.variable_ctx.stype(variable)
587 v.check_conform_expr(n_value, t)
588 _is_typed = true
589 end
590 end
591
592 redef class AReassignFormExpr
593 # Compute and check method used through the reassigment operator
594 private meth do_lvalue_typing(v: TypingVisitor, type_lvalue: MMType)
595 do
596 if type_lvalue == null then
597 return
598 end
599 var name = n_assign_op.method_name
600 var lc = type_lvalue.local_class
601 if not lc.has_global_property_by_name(name) then
602 v.error(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
603 return
604 end
605 var prop = lc.select_method(name)
606 prop.global.check_visibility(v, self, v.module, false)
607 var psig = prop.signature_for(type_lvalue)
608 _assign_method = prop
609 if not v.check_conform_expr(n_value, psig[0].not_for_self) then return
610 if not v.check_conform(self, psig.return_type.not_for_self, n_value.stype) then return
611 end
612
613 # Method used through the reassigment operator (once computed)
614 readable attr _assign_method: MMMethod
615 end
616
617 redef class AVarReassignExpr
618 redef meth after_typing(v)
619 do
620 var t = v.variable_ctx.stype(variable)
621 do_lvalue_typing(v, t)
622 _is_typed = true
623 end
624 end
625
626 redef class PAssignOp
627 meth method_name: Symbol is abstract
628 end
629 redef class APlusAssignOp
630 redef meth method_name do return once "+".to_symbol
631 end
632 redef class AMinusAssignOp
633 redef meth method_name do return once "-".to_symbol
634 end
635
636 redef class ASelfExpr
637 redef meth its_variable do return variable
638
639 redef meth after_typing(v)
640 do
641 variable = v.self_var
642 _stype = v.variable_ctx.stype(variable)
643 _is_typed = true
644 end
645
646 redef meth is_self do return true
647 end
648
649 redef class AImplicitSelfExpr
650 redef meth is_implicit_self do return true
651 end
652
653 redef class AIfexprExpr
654 redef meth accept_typing(v)
655 do
656 var old_var_ctx = v.variable_ctx
657
658 v.visit(n_expr)
659 v.use_if_true_variable_ctx(n_expr)
660 v.visit(n_then)
661 v.variable_ctx = old_var_ctx
662 v.visit(n_else)
663
664 v.check_conform_expr(n_expr, v.type_bool)
665
666 _stype = v.check_conform_multiexpr(null, [n_then, n_else])
667 _is_typed = _stype != null
668 end
669 end
670
671 redef class ABoolExpr
672 redef meth after_typing(v)
673 do
674 _stype = v.type_bool
675 _is_typed = true
676 end
677 end
678
679 redef class AOrExpr
680 redef meth after_typing(v)
681 do
682 v.check_conform_expr(n_expr, v.type_bool)
683 v.check_conform_expr(n_expr2, v.type_bool)
684 _stype = v.type_bool
685 _is_typed = true
686 end
687 end
688
689 redef class AAndExpr
690 redef meth accept_typing(v)
691 do
692 var old_var_ctx = v.variable_ctx
693
694 v.visit(n_expr)
695 v.use_if_true_variable_ctx(n_expr)
696
697 v.visit(n_expr2)
698 if n_expr2.if_true_variable_ctx != null then
699 _if_true_variable_ctx = n_expr2.if_true_variable_ctx
700 else
701 _if_true_variable_ctx = v.variable_ctx
702 end
703
704 v.variable_ctx = old_var_ctx
705
706 v.check_conform_expr(n_expr, v.type_bool)
707 v.check_conform_expr(n_expr2, v.type_bool)
708 _stype = v.type_bool
709 _is_typed = true
710 end
711 end
712
713 redef class ANotExpr
714 redef meth after_typing(v)
715 do
716 v.check_conform_expr(n_expr, v.type_bool)
717 _stype = v.type_bool
718 _is_typed = true
719 end
720 end
721
722 redef class AIntExpr
723 redef meth after_typing(v)
724 do
725 _stype = v.type_int
726 _is_typed = true
727 end
728 end
729
730 redef class AFloatExpr
731 redef meth after_typing(v)
732 do
733 _stype = v.type_float
734 _is_typed = true
735 end
736 end
737
738 redef class ACharExpr
739 redef meth after_typing(v)
740 do
741 _stype = v.type_char
742 _is_typed = true
743 end
744 end
745
746 redef class AStringFormExpr
747 readable attr _meth_with_native: MMMethod
748 redef meth after_typing(v)
749 do
750 _stype = v.type_string
751 _is_typed = true
752 _meth_with_native = _stype.local_class.select_method(once "with_native".to_symbol)
753 if _meth_with_native == null then v.error(self, "{_stype} MUST have a with_native method.")
754 end
755 end
756
757 redef class ASuperstringExpr
758 readable attr _meth_with_capacity: MMMethod
759 readable attr _meth_add: MMMethod
760 readable attr _meth_to_s: MMMethod
761 readable attr _atype: MMType
762 redef meth after_typing(v)
763 do
764 _stype = v.type_string
765 _atype = v.type_array(_stype)
766 _meth_with_capacity = _atype.local_class.select_method(once "with_capacity".to_symbol)
767 if _meth_with_capacity == null then v.error(self, "{_atype} MUST have a with_capacity method.")
768 _meth_add = _atype.local_class.select_method(once "add".to_symbol)
769 if _meth_add == null then v.error(self, "{_atype} MUST have an add method.")
770 _meth_to_s = v.type_object.local_class.select_method(once "to_s".to_symbol)
771 if _meth_to_s == null then v.error(self, "Object MUST have a to_s method.")
772 _is_typed = true
773 end
774 end
775
776 redef class ANullExpr
777 redef meth after_typing(v)
778 do
779 _stype = v.type_none
780 _is_typed = true
781 end
782 end
783
784 redef class AArrayExpr
785 readable attr _meth_with_capacity: MMMethod
786 readable attr _meth_add: MMMethod
787
788 redef meth after_typing(v)
789 do
790 var stype = v.check_conform_multiexpr(null, n_exprs)
791 if stype == null then return
792 do_typing(v, stype)
793 end
794
795 private meth do_typing(v: TypingVisitor, element_type: MMType)
796 do
797 _stype = v.type_array(element_type)
798
799 _meth_with_capacity = _stype.local_class.select_method(once "with_capacity".to_symbol)
800 if _meth_with_capacity == null then v.error(self, "{_stype} MUST have a with_capacity method.")
801 _meth_add = _stype.local_class.select_method(once "add".to_symbol)
802 if _meth_add == null then v.error(self, "{_stype} MUST have an add method.")
803
804 _is_typed = true
805 end
806 end
807
808 redef class ARangeExpr
809 readable attr _meth_init: MMMethod
810 redef meth after_typing(v)
811 do
812 if not v.check_expr(n_expr) or not v.check_expr(n_expr2) then return
813 var ntype = n_expr.stype
814 var ntype2 = n_expr2.stype
815 if ntype < ntype2 then
816 ntype = ntype2
817 else if not ntype2 < ntype then
818 v.error(self, "Type error: {ntype} incompatible with {ntype2}.")
819 return
820 end
821 var dtype = v.type_discrete
822 if not v.check_conform_expr(n_expr, dtype) or not v.check_conform_expr(n_expr2, dtype) then return
823 _stype = v.type_range(ntype)
824 _is_typed = true
825 end
826 end
827
828 redef class ACrangeExpr
829 redef meth after_typing(v)
830 do
831 super
832 _meth_init = stype.local_class.select_method(once "init".to_symbol)
833 end
834 end
835 redef class AOrangeExpr
836 redef meth after_typing(v)
837 do
838 super
839 _meth_init = stype.local_class.select_method(once "without_last".to_symbol)
840 end
841 end
842
843
844 redef class ASuperExpr
845 special ASuperInitCall
846 # readable attr _prop: MMSrcMethod
847 readable attr _init_in_superclass: MMMethod
848 redef meth after_typing(v)
849 do
850 var precs: Array[MMLocalProperty] = v.local_property.prhe.direct_greaters
851 if not precs.is_empty then
852 v.local_property.need_super = true
853 else if v.local_property.global.is_init then
854 var base_precs = v.local_class.super_methods_named(v.local_property.name)
855 for p in base_precs do
856 if not p.global.is_init then
857 v.error(self, "Error: {p.local_class}::{p} is not a constructor.")
858 else
859 precs.add(v.local_class[p.global])
860 end
861 end
862 if precs.is_empty then
863 v.error(self, "Error: No contructor named {v.local_property.name} in superclasses.")
864 return
865 else if precs.length > 1 then
866 v.error(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
867 return
868 end
869 var p = base_precs.first
870 assert p isa MMMethod
871 _init_in_superclass = p
872 register_super_init_call(v, p)
873 if n_args.length > 0 then
874 var signature = get_signature(v, v.self_var.stype, p, true)
875 _arguments = process_signature(v, signature, p.name, n_args.to_a)
876 end
877 else
878 v.error(self, "Error: No super method to call for {v.local_property}.")
879 return
880 end
881
882 if precs.first.signature_for(v.self_var.stype).return_type != null then
883 var stypes = new Array[MMType]
884 var stype: MMType = null
885 for prop in precs do
886 assert prop isa MMMethod
887 var t = prop.signature_for(v.self_var.stype).return_type.for_module(v.module).adapt_to(v.local_property.signature.recv)
888 stypes.add(t)
889 if stype == null or stype < t then
890 stype = t
891 end
892 end
893 for t in stypes do
894 v.check_conform(self, t, stype)
895 end
896 _stype = stype
897 end
898 var p = v.local_property
899 assert p isa MMSrcMethod
900 _prop = p
901 _is_typed = true
902 end
903 end
904
905 redef class AAttrFormExpr
906 # Attribute accessed
907 readable attr _prop: MMAttribute
908
909 # Attribute type of the acceded attribute
910 readable attr _attr_type: MMType
911
912 # Compute the attribute accessed
913 private meth do_typing(v: TypingVisitor)
914 do
915 if not v.check_expr(n_expr) then return
916 var type_recv = n_expr.stype
917 var name = n_id.to_symbol
918 var lc = type_recv.local_class
919 if not lc.has_global_property_by_name(name) then
920 v.error(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
921 return
922 end
923 var prop = lc.select_attribute(name)
924 if v.module.visibility_for(prop.global.local_class.module) < 3 then
925 v.error(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
926 end
927 _prop = prop
928 var at = prop.signature_for(type_recv).return_type
929 if not n_expr.is_self then at = at.not_for_self
930 _attr_type = at
931 end
932 end
933
934 redef class AAttrExpr
935 redef meth after_typing(v)
936 do
937 do_typing(v)
938 if prop == null then return
939 _stype = attr_type
940 _is_typed = true
941 end
942 end
943
944 redef class AAttrAssignExpr
945 redef meth after_typing(v)
946 do
947 do_typing(v)
948 if prop == null then return
949 if not v.check_conform_expr(n_value, attr_type) then return
950 _is_typed = true
951 end
952 end
953
954 redef class AAttrReassignExpr
955 redef meth after_typing(v)
956 do
957 do_typing(v)
958 if prop == null then return
959 do_lvalue_typing(v, attr_type)
960 _is_typed = true
961 end
962 end
963
964 class AAbsAbsSendExpr
965 special PExpr
966 # The signature of the called property
967 readable attr _prop_signature: MMSignature
968
969 # The real arguments used (after star transformation) (once computed)
970 readable attr _arguments: Array[PExpr]
971
972 # Check the conformity of a set of arguments `raw_args' to a signature.
973 private meth process_signature(v: TypingVisitor, psig: MMSignature, name: Symbol, raw_args: Array[PExpr]): Array[PExpr]
974 do
975 var par_vararg = psig.vararg_rank
976 var par_arity = psig.arity
977 var raw_arity: Int
978 if raw_args == null then raw_arity = 0 else raw_arity = raw_args.length
979 if par_arity > raw_arity or (par_arity != raw_arity and par_vararg == -1) then
980 v.error(self, "Error: '{name}' arity missmatch.")
981 return null
982 end
983 var arg_idx = 0
984 var args = new Array[PExpr]
985 for par_idx in [0..par_arity[ do
986 var a: PExpr
987 var par_type = psig[par_idx]
988 if par_idx == par_vararg then
989 var star = new Array[PExpr]
990 for i in [0..(raw_arity-par_arity)] do
991 a = raw_args[arg_idx]
992 v.check_conform_expr(a, par_type)
993 star.add(a)
994 arg_idx = arg_idx + 1
995 end
996 var aa = new AArrayExpr.init_aarrayexpr(star)
997 aa.do_typing(v, par_type)
998 a = aa
999 else
1000 a = raw_args[arg_idx]
1001 v.check_conform_expr(a, par_type)
1002 arg_idx = arg_idx + 1
1003 end
1004 args.add(a)
1005 end
1006 return args
1007 end
1008
1009 # Check the conformity of a set of defined closures
1010 private meth process_closures(v: TypingVisitor, psig: MMSignature, name: Symbol, cd: Array[PClosureDef]): MMType
1011 do
1012 var t = psig.return_type
1013 var cs = psig.closures # Declared closures
1014 var min_arity = 0
1015 for c in cs do
1016 if not c.is_optional then min_arity += 1
1017 end
1018 if cd != null then
1019 if cs.length == 0 then
1020 v.error(self, "Error: {name} does not require blocks.")
1021 else if cd.length > cs.length or cd.length < min_arity then
1022 v.error(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
1023 else
1024 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1025 var break_list: Array[ABreakExpr] = null
1026 if t != null then break_list = new Array[ABreakExpr]
1027
1028 # Process each closure definition
1029 for i in [0..cd.length[ do
1030 var csi = cs[i]
1031 var cdi = cd[i]
1032 var esc = new EscapableClosure(cdi, csi, break_list)
1033 v.escapable_ctx.push(esc)
1034 cdi.accept_typing2(v, esc)
1035 v.escapable_ctx.pop
1036 end
1037
1038 # Check break type conformity
1039 if break_list != null then
1040 t = v.check_conform_multiexpr(t, break_list)
1041 end
1042 end
1043 else if min_arity != 0 then
1044 v.error(self, "Error: {name} requires {cs.length} blocks.")
1045 end
1046 return t
1047 end
1048 end
1049
1050 class AAbsSendExpr
1051 special AAbsAbsSendExpr
1052 # Compute the called global property
1053 private meth do_typing(v: TypingVisitor, type_recv: MMType, is_implicit_self: Bool, recv_is_self: Bool, name: Symbol, raw_args: Array[PExpr], closure_defs: Array[PClosureDef])
1054 do
1055 var prop = get_property(v, type_recv, is_implicit_self, name)
1056 if prop == null then return
1057 var sig = get_signature(v, type_recv, prop, recv_is_self)
1058 if sig == null then return
1059 var args = process_signature(v, sig, prop.name, raw_args)
1060 if args == null then return
1061 var rtype = process_closures(v, sig, prop.name, closure_defs)
1062 if rtype == null and sig.return_type != null then return
1063 _prop = prop
1064 _prop_signature = sig
1065 _arguments = args
1066 _return_type = rtype
1067 end
1068
1069 private meth get_property(v: TypingVisitor, type_recv: MMType, is_implicit_self: Bool, name: Symbol): MMMethod
1070 do
1071 if type_recv == null then return null
1072 var lc = type_recv.local_class
1073 var prop: MMMethod = null
1074 if lc.has_global_property_by_name(name) then prop = lc.select_method(name)
1075 if prop == null and v.local_property.global.is_init then
1076 var props = type_recv.local_class.super_methods_named(name)
1077 if props.length > 1 then
1078 v.error(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1079 return null
1080 else if props.length == 1 then
1081 var p = type_recv.local_class[props.first.global]
1082 assert p isa MMMethod
1083 prop = p
1084 end
1085
1086 end
1087 if prop == null then
1088 if is_implicit_self then
1089 v.error(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1090 else
1091 v.error(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1092 end
1093 return null
1094 end
1095 return prop
1096 end
1097
1098 # Get the signature for a local property and a receiver
1099 private meth get_signature(v: TypingVisitor, type_recv: MMType, prop: MMMethod, recv_is_self: Bool): MMSignature
1100 do
1101 prop.global.check_visibility(v, self, v.module, recv_is_self)
1102 var psig = prop.signature_for(type_recv)
1103 if not recv_is_self then psig = psig.not_for_self
1104 return psig
1105 end
1106
1107 # The invoked method (once computed)
1108 readable attr _prop: MMMethod
1109
1110 # The return type (if any) (once computed)
1111 readable attr _return_type: MMType
1112 end
1113
1114 # A possible call of constructor in a super class
1115 # Could be an explicit call or with the 'super' keyword
1116 class ASuperInitCall
1117 special AAbsSendExpr
1118 private meth register_super_init_call(v: TypingVisitor, property: MMMethod)
1119 do
1120 if parent != v.top_block and self != v.top_block then
1121 v.error(self, "Error: Constructor invocation {property} must not be in nested block.")
1122 end
1123 var cla = v.module[property.global.intro.local_class.global]
1124 var prev_class: MMLocalClass = null
1125 if not v.explicit_super_init_calls.is_empty then
1126 prev_class = v.explicit_super_init_calls.last.global.intro.local_class
1127 end
1128 var order = v.local_class.cshe.reverse_linear_extension
1129 if cla == v.local_class then
1130 v.explicit_other_init_call = true
1131 else if not order.has(cla) then
1132 v.error(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1133 else if cla == prev_class then
1134 v.error(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1135 else
1136 var last_is_found = prev_class == null
1137 for c in order do
1138 if c == prev_class then
1139 last_is_found = true
1140 else if c == cla then
1141 if not last_is_found then
1142 v.error(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1143 end
1144 v.explicit_super_init_calls.add(property)
1145 break
1146 end
1147 end
1148 end
1149 end
1150
1151 end
1152
1153 redef class ANewExpr
1154 special AAbsSendExpr
1155 redef meth after_typing(v)
1156 do
1157 var t = n_type.stype
1158 if t == null then return
1159 if t.local_class.global.is_abstract then
1160 v.error(self, "Error: try to instantiate abstract class {t.local_class}.")
1161 return
1162 end
1163 var name: Symbol
1164 if n_id == null then
1165 name = once "init".to_symbol
1166 else
1167 name = n_id.to_symbol
1168 end
1169
1170 do_typing(v, t, false, false, name, n_args.to_a, null)
1171 if prop == null then return
1172
1173 if not prop.global.is_init then
1174 v.error(self, "Error: {prop} is not a constructor.")
1175 return
1176 end
1177 _stype = t
1178 _is_typed = true
1179 end
1180 end
1181
1182
1183 redef class ASendExpr
1184 special ASuperInitCall
1185 # Name of the invoked property
1186 meth name: Symbol is abstract
1187
1188 # Raw arguments used (withour star transformation)
1189 meth raw_arguments: Array[PExpr] is abstract
1190
1191 # Closure definitions
1192 meth closure_defs: Array[PClosureDef] do return null
1193
1194 redef meth after_typing(v)
1195 do
1196 do_all_typing(v)
1197 end
1198
1199 private meth do_all_typing(v: TypingVisitor)
1200 do
1201 if not v.check_expr(n_expr) then return
1202 do_typing(v, n_expr.stype, n_expr.is_implicit_self, n_expr.is_self, name, raw_arguments, closure_defs)
1203 if prop == null then return
1204
1205 if prop.global.is_init then
1206 if not v.local_property.global.is_init then
1207 v.error(self, "Error: try to invoke constructor {prop} in a method.")
1208 else if not n_expr.is_self then
1209 v.error(self, "Error: constructor {prop} is not invoken on 'self'.")
1210 else
1211 register_super_init_call(v, prop)
1212 end
1213 end
1214
1215 _stype = return_type
1216 _is_typed = true
1217 end
1218 end
1219
1220 class ASendReassignExpr
1221 special ASendExpr
1222 special AReassignFormExpr
1223 readable attr _read_prop: MMMethod
1224 redef meth do_all_typing(v)
1225 do
1226 if not v.check_expr(n_expr) then return
1227 var raw_args = raw_arguments
1228 do_typing(v, n_expr.stype, n_expr.is_implicit_self, n_expr.is_self, name, raw_args, null)
1229 if prop == null then return
1230 if prop.global.is_init then
1231 if not v.local_property.global.is_init then
1232 v.error(self, "Error: try to invoke constructor {prop} in a method.")
1233 else if not n_expr.is_self then
1234 v.error(self, "Error: constructor {prop} is not invoken on 'self'.")
1235 end
1236 end
1237 var t = prop.signature_for(n_expr.stype).return_type
1238 if not n_expr.is_self then t = t.not_for_self
1239
1240 do_lvalue_typing(v, t)
1241
1242 _read_prop = prop
1243 var old_args = arguments
1244 raw_args.add(n_value)
1245
1246 do_typing(v, n_expr.stype, n_expr.is_implicit_self, n_expr.is_self, "{name}=".to_symbol, raw_args, null)
1247 if prop == null then return
1248 if prop.global.is_init then
1249 if not v.local_property.global.is_init then
1250 v.error(self, "Error: try to invoke constructor {prop} in a method.")
1251 else if not n_expr.is_self then
1252 v.error(self, "Error: constructor {prop} is not invoken on 'self'.")
1253 end
1254 end
1255
1256 _arguments = old_args # FIXME: What if star parameters do not match betwen the two methods?
1257 _is_typed = true
1258 end
1259 end
1260
1261 redef class ABinopExpr
1262 redef meth raw_arguments do return [n_expr2]
1263 end
1264 redef class AEqExpr
1265 redef meth name do return once "==".to_symbol
1266 end
1267 redef class ANeExpr
1268 redef meth name do return once "!=".to_symbol
1269 end
1270 redef class ALtExpr
1271 redef meth name do return once "<".to_symbol
1272 end
1273 redef class ALeExpr
1274 redef meth name do return once "<=".to_symbol
1275 end
1276 redef class AGtExpr
1277 redef meth name do return once ">".to_symbol
1278 end
1279 redef class AGeExpr
1280 redef meth name do return once ">=".to_symbol
1281 end
1282 redef class APlusExpr
1283 redef meth name do return once "+".to_symbol
1284 end
1285 redef class AMinusExpr
1286 redef meth name do return once "-".to_symbol
1287 end
1288 redef class AStarshipExpr
1289 redef meth name do return once "<=>".to_symbol
1290 end
1291 redef class AStarExpr
1292 redef meth name do return once "*".to_symbol
1293 end
1294 redef class ASlashExpr
1295 redef meth name do return once "/".to_symbol
1296 end
1297 redef class APercentExpr
1298 redef meth name do return once "%".to_symbol
1299 end
1300
1301 redef class AUminusExpr
1302 redef meth name do return once "unary -".to_symbol
1303 redef meth raw_arguments do return null
1304 end
1305
1306 redef class ACallFormExpr
1307 redef meth after_typing(v)
1308 do
1309 if n_expr != null and n_expr.is_implicit_self then
1310 var name = n_id.to_symbol
1311 var variable = v.variable_ctx[name]
1312 if variable != null then
1313 if variable isa ClosureVariable then
1314 var n = new AClosureCallExpr.init_aclosurecallexpr(n_id, n_args, n_closure_defs)
1315 replace_with(n)
1316 n.variable = variable
1317 n.after_typing(v)
1318 return
1319 else
1320 if not n_args.is_empty then
1321 v.error(self, "Error: {name} is variable, not a function.")
1322 end
1323 var vform = variable_create(variable)
1324 vform.variable = variable
1325 replace_with(vform)
1326 vform.after_typing(v)
1327 return
1328 end
1329 end
1330 end
1331
1332 super
1333 end
1334
1335 redef meth closure_defs
1336 do
1337 if n_closure_defs == null or n_closure_defs.is_empty then
1338 return null
1339 else
1340 return n_closure_defs.to_a
1341 end
1342 end
1343
1344 # Create a variable acces corresponding to the call form
1345 meth variable_create(variable: Variable): AVarFormExpr is abstract
1346 end
1347
1348 redef class ACallExpr
1349 redef meth variable_create(variable)
1350 do
1351 return new AVarExpr.init_avarexpr(n_id)
1352 end
1353
1354 redef meth name do return n_id.to_symbol
1355 redef meth raw_arguments do return n_args.to_a
1356 end
1357
1358 redef class ACallAssignExpr
1359 redef meth variable_create(variable)
1360 do
1361 return new AVarAssignExpr.init_avarassignexpr(n_id, n_assign, n_value)
1362 end
1363
1364 redef meth name do return (n_id.text + "=").to_symbol
1365 redef meth raw_arguments do
1366 var res = n_args.to_a
1367 res.add(n_value)
1368 return res
1369 end
1370 end
1371
1372 redef class ACallReassignExpr
1373 special ASendReassignExpr
1374 redef meth variable_create(variable)
1375 do
1376 return new AVarReassignExpr.init_avarreassignexpr(n_id, n_assign_op, n_value)
1377 end
1378
1379 redef meth name do return n_id.to_symbol
1380 redef meth raw_arguments do return n_args.to_a
1381 end
1382
1383 redef class ABraExpr
1384 redef meth name do return once "[]".to_symbol
1385 redef meth raw_arguments do return n_args.to_a
1386 end
1387
1388 redef class ABraAssignExpr
1389 redef meth name do return once "[]=".to_symbol
1390 redef meth raw_arguments do
1391 var res = n_args.to_a
1392 res.add(n_value)
1393 return res
1394 end
1395 end
1396
1397 redef class ABraReassignExpr
1398 special ASendReassignExpr
1399 redef meth name do return once "[]".to_symbol
1400 redef meth raw_arguments do return n_args.to_a
1401 end
1402
1403 redef class AInitExpr
1404 redef meth name do return once "init".to_symbol
1405 redef meth raw_arguments do return n_args.to_a
1406 end
1407
1408 redef class AClosureCallExpr
1409 special AAbsAbsSendExpr
1410 redef meth after_typing(v)
1411 do
1412 var va = variable
1413 var sig = va.closure.signature
1414 var args = process_signature(v, sig, n_id.to_symbol, n_args.to_a)
1415 if not n_closure_defs.is_empty then
1416 process_closures(v, sig, n_id.to_symbol, n_closure_defs.to_a)
1417 end
1418 if args == null then return
1419 _prop_signature = sig
1420 _arguments = args
1421 _stype = sig.return_type
1422 _is_typed = true
1423 end
1424 end
1425
1426 redef class PClosureDef
1427 # The corresponding escapable object
1428 readable attr _escapable: EscapableBlock
1429
1430 attr _accept_typing2: Bool
1431 redef meth accept_typing(v)
1432 do
1433 # Typing is deferred, wait accept_typing2(v)
1434 if _accept_typing2 then super
1435 end
1436
1437 private meth accept_typing2(v: TypingVisitor, esc: EscapableClosure) is abstract
1438 end
1439
1440 redef class AClosureDef
1441 redef meth accept_typing2(v, esc)
1442 do
1443 _escapable = esc
1444
1445 var sig = esc.closure.signature
1446 if sig.arity != n_id.length then
1447 v.error(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1448 return
1449 end
1450
1451 closure = esc.closure
1452
1453 v.variable_ctx = v.variable_ctx.sub
1454 variables = new Array[AutoVariable]
1455 for i in [0..n_id.length[ do
1456 var va = new AutoVariable(n_id[i].to_symbol, self)
1457 variables.add(va)
1458 va.stype = sig[i]
1459 v.variable_ctx.add(va)
1460 end
1461
1462 _accept_typing2 = true
1463 accept_typing(v)
1464 end
1465 end
1466
1467 class ATypeCheckExpr
1468 special PExpr
1469 private meth check_expr_cast(v: TypingVisitor, n_expr: PExpr, n_type: PType)
1470 do
1471 if not v.check_expr(n_expr) then return
1472 var etype = n_expr.stype
1473 var ttype = n_type.stype
1474 if etype == ttype then
1475 v.warning(self, "Warning: Expression is already a {ttype}.")
1476 else if etype < ttype then
1477 v.warning(self, "Warning: Expression is already a {ttype} since it is a {etype}.")
1478 end
1479 end
1480 end
1481
1482 redef class AIsaExpr
1483 special ATypeCheckExpr
1484 redef meth after_typing(v)
1485 do
1486 check_expr_cast(v, n_expr, n_type)
1487 var variable = n_expr.its_variable
1488 if variable != null then
1489 _if_true_variable_ctx = v.variable_ctx.sub_with(variable, n_type.stype)
1490 end
1491 _stype = v.type_bool
1492 _is_typed = true
1493 end
1494 end
1495
1496 redef class AAsCastExpr
1497 special ATypeCheckExpr
1498 redef meth after_typing(v)
1499 do
1500 check_expr_cast(v, n_expr, n_type)
1501 _stype = n_type.stype
1502 _is_typed = _stype != null
1503 end
1504 end
1505
1506 redef class AProxyExpr
1507 redef meth after_typing(v)
1508 do
1509 if not n_expr.is_typed then return
1510 _is_typed = true
1511 if n_expr.is_statement then return
1512 _stype = n_expr.stype
1513 end
1514 end