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