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