4608ccd8c2e055a1e018e17b7309901c6a7bec6a
[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, null)
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: cannot 'continue', only 'break'.")
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 ADoExpr
462 # The corresponding escapable block
463 readable var _escapable: nullable EscapableBlock
464
465 redef fun accept_typing(v)
466 do
467 var escapable = new BreakOnlyEscapableBlock(self)
468 _escapable = escapable
469 v.escapable_ctx.push(escapable, n_label)
470
471 super
472
473 v.escapable_ctx.pop
474 _is_typed = true
475 end
476 end
477
478 redef class AIfExpr
479 redef fun accept_typing(v)
480 do
481 var old_var_ctx = v.variable_ctx
482 v.enter_visit(n_expr)
483 v.check_conform_expr(n_expr, v.type_bool)
484
485 # Prepare 'then' context
486 v.use_if_true_variable_ctx(n_expr)
487
488 # Process the 'then'
489 if n_then != null then
490 v.variable_ctx = v.variable_ctx.sub(n_then.as(not null))
491 v.enter_visit(n_then)
492 end
493
494 # Remember what appened in the 'then'
495 var then_var_ctx = v.variable_ctx
496
497 # Prepare 'else' context
498 v.variable_ctx = old_var_ctx
499 v.use_if_false_variable_ctx(n_expr)
500
501 # Process the 'else'
502 if n_else != null then
503 v.variable_ctx = v.variable_ctx.sub(n_else.as(not null))
504 v.enter_visit(n_else)
505 end
506
507 # Merge 'then' and 'else' contexts
508 old_var_ctx.merge2(then_var_ctx, v.variable_ctx, v.base_variable_ctx)
509 v.variable_ctx = old_var_ctx
510 _is_typed = true
511 end
512 end
513
514 redef class AWhileExpr
515 # The corresponding escapable block
516 readable var _escapable: nullable EscapableBlock
517
518 redef fun accept_typing(v)
519 do
520 var escapable = new EscapableBlock(self)
521 _escapable = escapable
522 v.escapable_ctx.push(escapable, n_label)
523 var old_var_ctx = v.variable_ctx
524 var old_base_var_ctx = v.base_variable_ctx
525 v.base_variable_ctx = v.variable_ctx
526 v.variable_ctx = v.variable_ctx.sub(self)
527
528 # Process condition
529 v.enter_visit(n_expr)
530 v.check_conform_expr(n_expr, v.type_bool)
531
532 # Prepare inside context (assert cond)
533 v.use_if_true_variable_ctx(n_expr)
534
535 # Process inside
536 if n_block != null then
537 v.variable_ctx = v.variable_ctx.sub(n_block.as(not null))
538 v.enter_visit(n_block)
539 end
540
541 v.variable_ctx = old_var_ctx
542 v.base_variable_ctx = old_base_var_ctx
543 v.escapable_ctx.pop
544 _is_typed = true
545 end
546 end
547
548 redef class AForExpr
549 var _variable: nullable AutoVariable
550 redef fun variable do return _variable.as(not null)
551
552 # The corresponding escapable block
553 readable var _escapable: nullable EscapableBlock
554
555 redef fun accept_typing(v)
556 do
557 var escapable = new EscapableBlock(self)
558 _escapable = escapable
559 v.escapable_ctx.push(escapable, n_label)
560
561 var old_var_ctx = v.variable_ctx
562 var old_base_var_ctx = v.base_variable_ctx
563 v.base_variable_ctx = v.variable_ctx
564 v.variable_ctx = v.variable_ctx.sub(self)
565 var va = new AutoVariable(n_id.to_symbol, self)
566 _variable = va
567 v.variable_ctx.add(va)
568
569 # Process collection
570 v.enter_visit(n_expr)
571
572 if not v.check_conform_expr(n_expr, v.type_collection) then return
573 var expr_type = n_expr.stype
574
575 # Get iterator
576 var meth_iterator = v.get_method(expr_type, once "iterator".to_symbol)
577 var iter_type = meth_iterator.signature_for(expr_type).return_type.as(not null)
578 var meth_item = v.get_method(iter_type, once ("item".to_symbol))
579 var va_stype = meth_item.signature_for(iter_type).return_type.as(not null)
580 if not n_expr.is_self then va_stype = va_stype.not_for_self
581 va.stype = va_stype
582
583 # Body evaluation
584 if n_block != null then v.enter_visit(n_block)
585
586 # pop context
587 v.variable_ctx = old_var_ctx
588 v.base_variable_ctx = old_base_var_ctx
589 v.escapable_ctx.pop
590 _is_typed = true
591 end
592 end
593
594 redef class AAssertExpr
595 redef fun after_typing(v)
596 do
597 v.check_conform_expr(n_expr, v.type_bool)
598 v.use_if_true_variable_ctx(n_expr)
599 _is_typed = true
600 end
601 end
602
603 redef class AVarFormExpr
604 var _variable: nullable Variable
605 redef fun variable do return _variable.as(not null)
606 end
607
608 redef class AVarExpr
609 redef fun its_variable do return variable
610
611 redef fun after_typing(v)
612 do
613 v.variable_ctx.check_is_set(self, variable)
614 _stype = v.variable_ctx.stype(variable)
615 _is_typed = _stype != null
616 end
617 end
618
619 redef class AVarAssignExpr
620 redef fun after_typing(v)
621 do
622 v.variable_ctx.mark_is_set(variable)
623 var t = v.variable_ctx.stype(variable)
624
625 # Check the base type
626 var btype = v.base_variable_ctx.stype(variable)
627 if not v.check_conform_expr(n_value, btype) then return
628
629 # Always cast
630 v.variable_ctx.stype(variable) = n_value.stype
631
632 _is_typed = true
633 end
634 end
635
636 redef class AReassignFormExpr
637 # Compute and check method used through the reassigment operator
638 # On success return the static type of the result of the reassigment operator
639 # Else display an error and return null
640 private fun do_rvalue_typing(v: TypingVisitor, type_lvalue: nullable MMType): nullable MMType
641 do
642 if type_lvalue == null then
643 return null
644 end
645 var name = n_assign_op.method_name
646 var lc = type_lvalue.local_class
647 if not lc.has_global_property_by_name(name) then
648 v.error(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
649 return null
650 end
651 var prop = lc.select_method(name)
652 prop.global.check_visibility(v, self, v.module, false)
653 var psig = prop.signature_for(type_lvalue)
654 _assign_method = prop
655 if not v.check_conform_expr(n_value, psig[0].not_for_self) then return null
656 return psig.return_type.not_for_self
657 end
658
659 redef fun assign_method do return _assign_method.as(not null)
660 var _assign_method: nullable MMMethod
661 end
662
663 redef class AVarReassignExpr
664 redef fun after_typing(v)
665 do
666 v.variable_ctx.check_is_set(self, variable)
667 v.variable_ctx.mark_is_set(variable)
668 var t = v.variable_ctx.stype(variable)
669 var t2 = do_rvalue_typing(v, t)
670 if t2 == null then return
671
672 # Check the base type
673 var btype = v.base_variable_ctx.stype(variable)
674 if not v.check_conform(n_value, t2, btype) then return
675
676 # Always cast
677 v.variable_ctx.stype(variable) = t2
678
679 _is_typed = true
680 end
681 end
682
683 redef class AAssignOp
684 fun method_name: Symbol is abstract
685 end
686 redef class APlusAssignOp
687 redef fun method_name do return once "+".to_symbol
688 end
689 redef class AMinusAssignOp
690 redef fun method_name do return once "-".to_symbol
691 end
692
693 redef class ASelfExpr
694 var _variable: nullable ParamVariable
695 redef fun variable do return _variable.as(not null)
696
697 redef fun its_variable do return variable
698
699 redef fun after_typing(v)
700 do
701 _variable = v.self_var
702 _stype = v.variable_ctx.stype(variable)
703 _is_typed = true
704 end
705
706 redef fun is_self do return true
707 end
708
709 redef class AImplicitSelfExpr
710 redef fun is_implicit_self do return true
711 end
712
713 redef class AIfexprExpr
714 redef fun accept_typing(v)
715 do
716 var old_var_ctx = v.variable_ctx
717
718 # Process condition
719 v.enter_visit(n_expr)
720 v.check_conform_expr(n_expr, v.type_bool)
721
722 # Prepare 'then' context
723 v.use_if_true_variable_ctx(n_expr)
724
725 # Process 'then'
726 v.enter_visit(n_then)
727
728 # Prepare 'else' context
729 v.variable_ctx = old_var_ctx
730 v.use_if_false_variable_ctx(n_expr)
731
732 # Process 'else'
733 v.enter_visit(n_else)
734
735 var stype = v.check_conform_multiexpr(null, [n_then, n_else])
736 if stype == null then return
737
738 _stype = stype
739 _is_typed = true
740 end
741 end
742
743 redef class ABoolExpr
744 redef fun after_typing(v)
745 do
746 _stype = v.type_bool
747 _is_typed = true
748 end
749 end
750
751 redef class AOrExpr
752 redef fun accept_typing(v)
753 do
754 var old_var_ctx = v.variable_ctx
755 var stype = v.type_bool
756 _stype = stype
757
758 # Process left operand
759 v.enter_visit(n_expr)
760
761 # Prepare right operand context
762 v.use_if_false_variable_ctx(n_expr)
763
764 # Process right operand
765 v.enter_visit(n_expr2)
766 if n_expr2.if_false_variable_ctx != null then
767 _if_false_variable_ctx = n_expr2.if_false_variable_ctx
768 else
769 _if_false_variable_ctx = v.variable_ctx
770 end
771
772 v.variable_ctx = old_var_ctx
773
774 v.check_conform_expr(n_expr, stype)
775 v.check_conform_expr(n_expr2, stype)
776 _stype = stype
777 _is_typed = true
778 end
779 end
780
781 redef class AAndExpr
782 redef fun accept_typing(v)
783 do
784 var old_var_ctx = v.variable_ctx
785 var stype = v.type_bool
786
787 # Process left operand
788 v.enter_visit(n_expr)
789
790 # Prepare right operand context
791 v.use_if_true_variable_ctx(n_expr)
792
793 # Process right operand
794 v.enter_visit(n_expr2)
795 if n_expr2.if_true_variable_ctx != null then
796 _if_true_variable_ctx = n_expr2.if_true_variable_ctx
797 else
798 _if_true_variable_ctx = v.variable_ctx
799 end
800
801 v.variable_ctx = old_var_ctx
802
803 v.check_conform_expr(n_expr, stype)
804 v.check_conform_expr(n_expr2, stype)
805 _stype = stype
806 _is_typed = true
807 end
808 end
809
810 redef class ANotExpr
811 redef fun after_typing(v)
812 do
813 v.check_conform_expr(n_expr, v.type_bool)
814
815 # Invert if_true/if_false information
816 _if_false_variable_ctx = n_expr._if_true_variable_ctx
817 _if_true_variable_ctx = n_expr._if_false_variable_ctx
818
819 _stype = v.type_bool
820 _is_typed = true
821 end
822 end
823
824 redef class AIntExpr
825 redef fun after_typing(v)
826 do
827 _stype = v.type_int
828 _is_typed = true
829 end
830 end
831
832 redef class AFloatExpr
833 redef fun after_typing(v)
834 do
835 _stype = v.type_float
836 _is_typed = true
837 end
838 end
839
840 redef class ACharExpr
841 redef fun after_typing(v)
842 do
843 _stype = v.type_char
844 _is_typed = true
845 end
846 end
847
848 redef class AStringFormExpr
849 redef fun after_typing(v)
850 do
851 _stype = v.type_string
852 _is_typed = true
853 end
854 end
855
856 redef class ASuperstringExpr
857 redef fun atype do return _atype.as(not null)
858 var _atype: nullable MMType
859 redef fun after_typing(v)
860 do
861 var stype = v.type_string
862 _stype = stype
863 var atype = v.type_array(stype)
864 _atype = atype
865 _is_typed = true
866 end
867 end
868
869 redef class ANullExpr
870 redef fun after_typing(v)
871 do
872 _stype = v.type_none
873 _is_typed = true
874 end
875 end
876
877 redef class AArrayExpr
878 redef fun after_typing(v)
879 do
880 var stype = v.check_conform_multiexpr(null, n_exprs)
881 if stype != null then do_typing(v, stype)
882 end
883
884 private fun do_typing(v: TypingVisitor, element_type: MMType)
885 do
886 _stype = v.type_array(element_type)
887 _is_typed = true
888 end
889 end
890
891 redef class ARangeExpr
892 redef fun after_typing(v)
893 do
894 if not v.check_expr(n_expr) or not v.check_expr(n_expr2) then return
895 var ntype = n_expr.stype
896 var ntype2 = n_expr2.stype
897 if ntype < ntype2 then
898 ntype = ntype2
899 else if not ntype2 < ntype then
900 v.error(self, "Type error: {ntype} incompatible with {ntype2}.")
901 return
902 end
903 var dtype = v.type_discrete
904 if not v.check_conform_expr(n_expr, dtype) or not v.check_conform_expr(n_expr2, dtype) then return
905 _stype = v.type_range(ntype)
906 _is_typed = true
907 end
908 end
909
910 redef class ASuperExpr
911 redef readable var _init_in_superclass: nullable MMMethod
912 redef fun after_typing(v)
913 do
914 var precs: Array[MMLocalProperty] = v.local_property.prhe.direct_greaters
915 if not precs.is_empty then
916 v.local_property.need_super = true
917 else if v.local_property.global.is_init then
918 var base_precs = v.local_class.super_methods_named(v.local_property.name)
919 for p in base_precs do
920 if not p.global.is_init then
921 v.error(self, "Error: {p.local_class}::{p} is not a constructor.")
922 else
923 precs.add(v.local_class[p.global])
924 end
925 end
926 if precs.is_empty then
927 v.error(self, "Error: No contructor named {v.local_property.name} in superclasses.")
928 return
929 else if precs.length > 1 then
930 v.error(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
931 return
932 end
933 var p = base_precs.first
934 assert p isa MMMethod
935 _init_in_superclass = p
936 register_super_init_call(v, p)
937 if n_args.length > 0 then
938 var signature = get_signature(v, v.self_var.stype.as(not null), p, true)
939 _arguments = process_signature(v, signature, p.name, n_args.to_a)
940 end
941 else
942 v.error(self, "Error: No super method to call for {v.local_property}.")
943 return
944 end
945
946 if precs.first.signature_for(v.self_var.stype.as(not null)).return_type != null then
947 var stypes = new Array[MMType]
948 var stype: nullable MMType = null
949 for prop in precs do
950 assert prop isa MMMethod
951 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)
952 stypes.add(t)
953 if stype == null or stype < t then
954 stype = t
955 end
956 end
957 for t in stypes do
958 v.check_conform(self, t, stype.as(not null))
959 end
960 _stype = stype
961 end
962 var p = v.local_property
963 assert p isa MMSrcMethod
964 _prop = p
965 _is_typed = true
966 end
967 end
968
969 redef class AAttrFormExpr
970 redef fun prop do return _prop.as(not null)
971 var _prop: nullable MMAttribute
972
973 redef fun attr_type do return _attr_type.as(not null)
974 var _attr_type: nullable MMType
975
976 # Compute the attribute accessed
977 private fun do_typing(v: TypingVisitor)
978 do
979 if not v.check_expr(n_expr) then return
980 var type_recv = n_expr.stype
981 var name = n_id.to_symbol
982 var lc = type_recv.local_class
983 if not lc.has_global_property_by_name(name) then
984 v.error(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
985 return
986 end
987 var prop = lc.select_attribute(name)
988 if v.module.visibility_for(prop.global.local_class.module) < 3 then
989 v.error(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
990 end
991 _prop = prop
992 var at = prop.signature_for(type_recv).return_type
993 if not n_expr.is_self then at = at.not_for_self
994 _attr_type = at
995 end
996 end
997
998 redef class AAttrExpr
999 redef fun after_typing(v)
1000 do
1001 do_typing(v)
1002 if _prop == null then return
1003 _stype = attr_type
1004 _is_typed = true
1005 end
1006 end
1007
1008 redef class AAttrAssignExpr
1009 redef fun after_typing(v)
1010 do
1011 do_typing(v)
1012 if _prop == null then return
1013 if not v.check_conform_expr(n_value, attr_type) then return
1014 _is_typed = true
1015 end
1016 end
1017
1018 redef class AAttrReassignExpr
1019 redef fun after_typing(v)
1020 do
1021 do_typing(v)
1022 if _prop == null then return
1023 var t = do_rvalue_typing(v, attr_type)
1024 if t == null then return
1025 v.check_conform(self, t, n_value.stype)
1026 _is_typed = true
1027 end
1028 end
1029
1030 redef class AIssetAttrExpr
1031 redef fun after_typing(v)
1032 do
1033 do_typing(v)
1034 if _prop == null then return
1035 if attr_type.is_nullable then
1036 v.error(self, "Error: isset on a nullable attribute.")
1037 end
1038 _stype = v.type_bool
1039 _is_typed = true
1040 end
1041 end
1042
1043 redef class AAbsAbsSendExpr
1044 # The signature of the called property
1045 redef fun prop_signature do return _prop_signature.as(not null)
1046 var _prop_signature: nullable MMSignature
1047
1048 # The real arguments used (after star transformation) (once computed)
1049 redef fun arguments do return _arguments.as(not null)
1050 var _arguments: nullable Array[AExpr]
1051
1052 # Check the conformity of a set of arguments `raw_args' to a signature.
1053 private fun process_signature(v: TypingVisitor, psig: MMSignature, name: Symbol, raw_args: nullable Array[AExpr]): nullable Array[AExpr]
1054 do
1055 var par_vararg = psig.vararg_rank
1056 var par_arity = psig.arity
1057 var raw_arity: Int
1058 if raw_args == null then raw_arity = 0 else raw_arity = raw_args.length
1059 if par_arity > raw_arity or (par_arity != raw_arity and par_vararg == -1) then
1060 v.error(self, "Error: '{name}' arity missmatch.")
1061 return null
1062 end
1063 var arg_idx = 0
1064 var args = new Array[AExpr]
1065 for par_idx in [0..par_arity[ do
1066 var a: AExpr
1067 var par_type = psig[par_idx]
1068 if par_idx == par_vararg then
1069 var star = new Array[AExpr]
1070 for i in [0..(raw_arity-par_arity)] do
1071 a = raw_args[arg_idx]
1072 v.check_conform_expr(a, par_type)
1073 star.add(a)
1074 arg_idx = arg_idx + 1
1075 end
1076 var aa = new AArrayExpr.init_aarrayexpr(star)
1077 aa.do_typing(v, par_type)
1078 a = aa
1079 else
1080 a = raw_args[arg_idx]
1081 v.check_conform_expr(a, par_type)
1082 arg_idx = arg_idx + 1
1083 end
1084 args.add(a)
1085 end
1086 return args
1087 end
1088
1089 # Check the conformity of a set of defined closures
1090 private fun process_closures(v: TypingVisitor, psig: MMSignature, name: Symbol, cd: nullable Array[AClosureDef]): nullable MMType
1091 do
1092 var t = psig.return_type
1093 var cs = psig.closures # Declared closures
1094 var min_arity = 0
1095 for c in cs do
1096 if not c.is_optional then min_arity += 1
1097 end
1098 var arity = 0
1099 if cd != null then arity = cd.length
1100 if cs.length > 0 then
1101 if arity == 0 and min_arity > 0 then
1102 v.error(self, "Error: {name} requires {cs.length} blocks.")
1103 else if arity > cs.length or arity < min_arity then
1104 v.error(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
1105 else
1106 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1107 var break_list: nullable Array[ABreakExpr] = null
1108 if t != null then break_list = new Array[ABreakExpr]
1109
1110 # The n_label, is any in only set on the last decl
1111 var n_label = if arity > 0 then cd[arity-1].n_label else null
1112
1113 # Process each closure definition
1114 for i in [0..arity[ do
1115 var csi = cs[i]
1116 var cdi = cd[i]
1117 var esc = new EscapableClosure(cdi, csi, break_list)
1118 v.escapable_ctx.push(esc, n_label)
1119 cdi.accept_typing2(v, esc)
1120 v.escapable_ctx.pop
1121 end
1122
1123 # Check break type conformity
1124 if break_list != null then
1125 t = v.check_conform_multiexpr(t, break_list)
1126 end
1127 end
1128 else if arity != 0 then
1129 v.error(self, "Error: {name} does not require blocks.")
1130 end
1131 return t
1132 end
1133 end
1134
1135 redef class AAbsSendExpr
1136 # Compute the called global property
1137 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])
1138 do
1139 var prop = get_property(v, type_recv, is_implicit_self, name)
1140 if prop == null then return
1141 var sig = get_signature(v, type_recv, prop, recv_is_self)
1142 var args = process_signature(v, sig, prop.name, raw_args)
1143 if args == null then return
1144 var rtype = process_closures(v, sig, prop.name, closure_defs)
1145 if rtype == null and sig.return_type != null then return
1146 _prop = prop
1147 _prop_signature = sig
1148 _arguments = args
1149 _return_type = rtype
1150 end
1151
1152 private fun get_property(v: TypingVisitor, type_recv: MMType, is_implicit_self: Bool, name: Symbol): nullable MMMethod
1153 do
1154 var lc = type_recv.local_class
1155 var prop: nullable MMMethod = null
1156 if lc.has_global_property_by_name(name) then prop = lc.select_method(name)
1157 if prop == null and v.local_property.global.is_init then
1158 var props = type_recv.local_class.super_methods_named(name)
1159 if props.length > 1 then
1160 v.error(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1161 return null
1162 else if props.length == 1 then
1163 var p = type_recv.local_class[props.first.global]
1164 assert p isa MMMethod
1165 prop = p
1166 end
1167
1168 end
1169 if prop == null then
1170 if is_implicit_self then
1171 v.error(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1172 else
1173 v.error(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1174 end
1175 return null
1176 end
1177 return prop
1178 end
1179
1180 # Get the signature for a local property and a receiver
1181 private fun get_signature(v: TypingVisitor, type_recv: MMType, prop: MMMethod, recv_is_self: Bool): MMSignature
1182 do
1183 prop.global.check_visibility(v, self, v.module, recv_is_self)
1184 var psig = prop.signature_for(type_recv)
1185 if not recv_is_self then psig = psig.not_for_self
1186 return psig
1187 end
1188
1189 # The invoked method (once computed)
1190 redef fun prop do return _prop.as(not null)
1191 var _prop: nullable MMMethod
1192
1193 # The return type (if any) (once computed)
1194 redef readable var _return_type: nullable MMType
1195 end
1196
1197 # A possible call of constructor in a super class
1198 # Could be an explicit call or with the 'super' keyword
1199 redef class ASuperInitCall
1200 private fun register_super_init_call(v: TypingVisitor, property: MMMethod)
1201 do
1202 if parent != v.top_block and self != v.top_block then
1203 v.error(self, "Error: Constructor invocation {property} must not be in nested block.")
1204 end
1205 var cla = v.module[property.global.intro.local_class.global]
1206 var prev_class: nullable MMLocalClass = null
1207 var esic = v.explicit_super_init_calls.as(not null)
1208 if not esic.is_empty then
1209 prev_class = esic.last.global.intro.local_class
1210 end
1211 var order = v.local_class.cshe.reverse_linear_extension
1212 if cla == v.local_class then
1213 v.explicit_other_init_call = true
1214 else if not order.has(cla) then
1215 v.error(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1216 else if cla == prev_class then
1217 v.error(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1218 else
1219 var last_is_found = prev_class == null
1220 for c in order do
1221 if c == prev_class then
1222 last_is_found = true
1223 else if c == cla then
1224 if not last_is_found then
1225 v.error(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1226 end
1227 esic.add(property)
1228 break
1229 end
1230 end
1231 end
1232 end
1233
1234 end
1235
1236 redef class ANewExpr
1237 redef fun after_typing(v)
1238 do
1239 if not n_type.is_typed then return
1240 var t = n_type.stype
1241 if t.local_class.global.is_abstract then
1242 v.error(self, "Error: try to instantiate abstract class {t.local_class}.")
1243 return
1244 end
1245 var name: Symbol
1246 if n_id == null then
1247 name = once "init".to_symbol
1248 else
1249 name = n_id.to_symbol
1250 end
1251
1252 do_typing(v, t, false, false, name, n_args.to_a, null)
1253 if _prop == null then return
1254
1255 if not prop.global.is_init then
1256 v.error(self, "Error: {prop} is not a constructor.")
1257 return
1258 end
1259 _stype = t
1260 _is_typed = true
1261 end
1262 end
1263
1264
1265 redef class ASendExpr
1266 # Name of the invoked property
1267 fun name: Symbol is abstract
1268
1269 # Raw arguments used (withour star transformation)
1270 fun raw_arguments: nullable Array[AExpr] is abstract
1271
1272 # Closure definitions
1273 redef fun closure_defs: nullable Array[AClosureDef] do return null
1274
1275 redef fun after_typing(v)
1276 do
1277 do_all_typing(v)
1278 end
1279
1280 private fun do_all_typing(v: TypingVisitor)
1281 do
1282 if not v.check_expr(n_expr) then return
1283 do_typing(v, n_expr.stype, n_expr.is_implicit_self, n_expr.is_self, name, raw_arguments, closure_defs)
1284 if _prop == null then return
1285 var prop = _prop.as(not null)
1286
1287 if prop.global.is_init then
1288 if not v.local_property.global.is_init then
1289 v.error(self, "Error: try to invoke constructor {prop} in a method.")
1290 else if not n_expr.is_self then
1291 v.error(self, "Error: constructor {prop} is not invoken on 'self'.")
1292 else
1293 register_super_init_call(v, prop)
1294 end
1295 end
1296
1297 _stype = return_type
1298 _is_typed = true
1299 end
1300 end
1301
1302 redef class ASendReassignExpr
1303 redef fun read_prop do return _read_prop.as(not null)
1304 var _read_prop: nullable MMMethod
1305 redef fun do_all_typing(v)
1306 do
1307 if not v.check_expr(n_expr) then return
1308 var raw_args = raw_arguments
1309 do_typing(v, n_expr.stype, n_expr.is_implicit_self, n_expr.is_self, name, raw_args, null)
1310 var prop = _prop
1311 if prop == null then return
1312 if prop.global.is_init then
1313 if not v.local_property.global.is_init then
1314 v.error(self, "Error: try to invoke constructor {prop} in a method.")
1315 else if not n_expr.is_self then
1316 v.error(self, "Error: constructor {prop} is not invoken on 'self'.")
1317 end
1318 end
1319 var t = prop.signature_for(n_expr.stype).return_type.as(not null)
1320 if not n_expr.is_self then t = t.not_for_self
1321
1322 var t2 = do_rvalue_typing(v, t)
1323 if t2 == null then return
1324 v.check_conform(self, t2, n_value.stype)
1325
1326 _read_prop = prop
1327 var old_args = arguments
1328 raw_args.add(n_value)
1329
1330 do_typing(v, n_expr.stype, n_expr.is_implicit_self, n_expr.is_self, "{name}=".to_symbol, raw_args, null)
1331 if prop.global.is_init then
1332 if not v.local_property.global.is_init then
1333 v.error(self, "Error: try to invoke constructor {prop} in a method.")
1334 else if not n_expr.is_self then
1335 v.error(self, "Error: constructor {prop} is not invoken on 'self'.")
1336 end
1337 end
1338
1339 _arguments = old_args # FIXME: What if star parameters do not match betwen the two methods?
1340 _is_typed = true
1341 end
1342 end
1343
1344 redef class ABinopExpr
1345 redef fun raw_arguments do return [n_expr2]
1346 end
1347 redef class AEqExpr
1348 redef fun name do return once "==".to_symbol
1349 redef fun after_typing(v)
1350 do
1351 super
1352 if not n_expr.is_typed or not n_expr2.is_typed then return
1353 if n_expr.stype isa MMTypeNone and not n_expr2.stype.is_nullable or
1354 n_expr2.stype isa MMTypeNone and not n_expr.stype.is_nullable then
1355 v.warning(self, "Warning: comparaison between null and a non nullable value.")
1356 end
1357
1358 if n_expr.stype isa MMTypeNone then
1359 try_to_isa(v, n_expr2)
1360 else if n_expr2.stype isa MMTypeNone then
1361 try_to_isa(v, n_expr)
1362 end
1363 end
1364
1365 private fun try_to_isa(v: TypingVisitor, n: AExpr)
1366 do
1367 var variable = n.its_variable
1368 if variable != null then
1369 _if_false_variable_ctx = v.variable_ctx.sub_with(self, variable, n.stype.as_notnull)
1370 end
1371 end
1372 end
1373 redef class ANeExpr
1374 redef fun name do return once "!=".to_symbol
1375 redef fun after_typing(v)
1376 do
1377 super
1378 if not n_expr.is_typed or not n_expr2.is_typed then return
1379 if n_expr.stype isa MMTypeNone and not n_expr2.stype.is_nullable or
1380 n_expr2.stype isa MMTypeNone and not n_expr.stype.is_nullable then
1381 v.warning(self, "Warning: comparaison between null and a non nullable value.")
1382 end
1383
1384 if n_expr.stype isa MMTypeNone then
1385 try_to_isa(v, n_expr2)
1386 else if n_expr2.stype isa MMTypeNone then
1387 try_to_isa(v, n_expr)
1388 end
1389 end
1390
1391 private fun try_to_isa(v: TypingVisitor, n: AExpr)
1392 do
1393 var variable = n.its_variable
1394 if variable != null then
1395 _if_true_variable_ctx = v.variable_ctx.sub_with(self, variable, n.stype.as_notnull)
1396 end
1397 end
1398 end
1399 redef class ALtExpr
1400 redef fun name do return once "<".to_symbol
1401 end
1402 redef class ALeExpr
1403 redef fun name do return once "<=".to_symbol
1404 end
1405 redef class AGtExpr
1406 redef fun name do return once ">".to_symbol
1407 end
1408 redef class AGeExpr
1409 redef fun name do return once ">=".to_symbol
1410 end
1411 redef class APlusExpr
1412 redef fun name do return once "+".to_symbol
1413 end
1414 redef class AMinusExpr
1415 redef fun name do return once "-".to_symbol
1416 end
1417 redef class AStarshipExpr
1418 redef fun name do return once "<=>".to_symbol
1419 end
1420 redef class AStarExpr
1421 redef fun name do return once "*".to_symbol
1422 end
1423 redef class ASlashExpr
1424 redef fun name do return once "/".to_symbol
1425 end
1426 redef class APercentExpr
1427 redef fun name do return once "%".to_symbol
1428 end
1429
1430 redef class AUminusExpr
1431 redef fun name do return once "unary -".to_symbol
1432 redef fun raw_arguments do return null
1433 end
1434
1435 redef class ACallFormExpr
1436 redef fun after_typing(v)
1437 do
1438 if n_expr.is_implicit_self then
1439 var name = n_id.to_symbol
1440 var variable = v.variable_ctx[name]
1441 if variable != null then
1442 var n: AExpr
1443 if variable isa ClosureVariable then
1444 n = new AClosureCallExpr.init_aclosurecallexpr(n_id, n_args, n_closure_defs)
1445 n._variable = variable
1446 else
1447 if not n_args.is_empty then
1448 v.error(self, "Error: {name} is variable, not a function.")
1449 return
1450 end
1451 n = variable_create(variable)
1452 n._variable = variable
1453 end
1454 replace_with(n)
1455 n.after_typing(v)
1456 return
1457 end
1458 end
1459
1460 super
1461 end
1462
1463 redef fun closure_defs
1464 do
1465 if n_closure_defs.is_empty then
1466 return null
1467 else
1468 return n_closure_defs.to_a
1469 end
1470 end
1471
1472 # Create a variable acces corresponding to the call form
1473 fun variable_create(variable: Variable): AVarFormExpr is abstract
1474 end
1475
1476 redef class ACallExpr
1477 redef fun variable_create(variable)
1478 do
1479 return new AVarExpr.init_avarexpr(n_id)
1480 end
1481
1482 redef fun name do return n_id.to_symbol
1483 redef fun raw_arguments do return n_args.to_a
1484 end
1485
1486 redef class ACallAssignExpr
1487 redef fun variable_create(variable)
1488 do
1489 return new AVarAssignExpr.init_avarassignexpr(n_id, n_assign, n_value)
1490 end
1491
1492 redef fun name do return (n_id.text + "=").to_symbol
1493 redef fun raw_arguments do
1494 var res = n_args.to_a
1495 res.add(n_value)
1496 return res
1497 end
1498 end
1499
1500 redef class ACallReassignExpr
1501 redef fun variable_create(variable)
1502 do
1503 return new AVarReassignExpr.init_avarreassignexpr(n_id, n_assign_op, n_value)
1504 end
1505
1506 redef fun name do return n_id.to_symbol
1507 redef fun raw_arguments do return n_args.to_a
1508 end
1509
1510 redef class ABraExpr
1511 redef fun name do return once "[]".to_symbol
1512 redef fun raw_arguments do return n_args.to_a
1513 end
1514
1515 redef class ABraAssignExpr
1516 redef fun name do return once "[]=".to_symbol
1517 redef fun raw_arguments do
1518 var res = n_args.to_a
1519 res.add(n_value)
1520 return res
1521 end
1522 end
1523
1524 redef class ABraReassignExpr
1525 redef fun name do return once "[]".to_symbol
1526 redef fun raw_arguments do return n_args.to_a
1527 end
1528
1529 redef class AInitExpr
1530 redef fun name do return once "init".to_symbol
1531 redef fun raw_arguments do return n_args.to_a
1532 end
1533
1534 redef class AClosureCallExpr
1535 var _variable: nullable ClosureVariable
1536 redef fun variable do return _variable.as(not null)
1537
1538 redef fun after_typing(v)
1539 do
1540 var va = variable
1541 if va.closure.is_break then v.variable_ctx.unreash = true
1542 var sig = va.closure.signature
1543 var args = process_signature(v, sig, n_id.to_symbol, n_args.to_a)
1544 if not n_closure_defs.is_empty then
1545 process_closures(v, sig, n_id.to_symbol, n_closure_defs.to_a)
1546 end
1547 if args == null then return
1548 _prop_signature = sig
1549 _arguments = args
1550 _stype = sig.return_type
1551 _is_typed = true
1552 end
1553 end
1554
1555 redef class AClosureDef
1556 var _closure: nullable MMClosure
1557 redef fun closure do return _closure.as(not null)
1558
1559 # The corresponding escapable object
1560 readable var _escapable: nullable EscapableBlock
1561
1562 var _accept_typing2: Bool = false
1563 redef fun accept_typing(v)
1564 do
1565 # Typing is deferred, wait accept_typing2(v)
1566 if _accept_typing2 then super
1567 end
1568
1569 private fun accept_typing2(v: TypingVisitor, esc: EscapableClosure)
1570 do
1571 _escapable = esc
1572
1573 var sig = esc.closure.signature
1574 if sig.arity != n_id.length then
1575 v.error(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1576 return
1577 end
1578
1579 _closure = esc.closure
1580
1581 var old_var_ctx = v.variable_ctx
1582 var old_base_var_ctx = v.base_variable_ctx
1583 v.base_variable_ctx = v.variable_ctx
1584 v.variable_ctx = v.variable_ctx.sub(self)
1585 variables = new Array[AutoVariable]
1586 for i in [0..n_id.length[ do
1587 var va = new AutoVariable(n_id[i].to_symbol, self)
1588 variables.add(va)
1589 va.stype = sig[i]
1590 v.variable_ctx.add(va)
1591 end
1592
1593 _accept_typing2 = true
1594 accept_typing(v)
1595
1596 if v.variable_ctx.unreash == false then
1597 if closure.signature.return_type != null then
1598 v.error(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
1599 else if closure.is_break then
1600 v.error(self, "Control error: Reached end of break block (a 'break' was expected).")
1601 end
1602 end
1603 v.variable_ctx = old_var_ctx
1604 v.base_variable_ctx = old_base_var_ctx
1605 end
1606 end
1607
1608 class ATypeCheckExpr
1609 special AExpr
1610 private fun check_expr_cast(v: TypingVisitor, n_expr: AExpr, n_type: AType)
1611 do
1612 if not v.check_expr(n_expr) then return
1613 if not n_type.is_typed then return
1614 var etype = n_expr.stype
1615 var ttype = n_type.stype
1616 if etype == ttype then
1617 v.warning(self, "Warning: Expression is already a {ttype}.")
1618 else if etype < ttype then
1619 v.warning(self, "Warning: Expression is already a {ttype} since it is a {etype}.")
1620 else if etype.is_nullable and etype.as_notnull == ttype then
1621 if ttype isa MMTypeFormal and ttype.bound.is_nullable then
1622 # No warning in this case since with
1623 # type T: nullable A
1624 # var x: nullable T
1625 # 'x.as(not null)' != 'x.as(T)'
1626 # 'x != null' != 'x isa T'
1627 else if self isa AIsaExpr then
1628 v.warning(self, "Warning: Prefer '!= null'.")
1629 else
1630 v.warning(self, "Warning: Prefer '.as(not null)'.")
1631 end
1632 end
1633 end
1634 end
1635
1636 redef class AIsaExpr
1637 special ATypeCheckExpr
1638 redef fun after_typing(v)
1639 do
1640 check_expr_cast(v, n_expr, n_type)
1641 if not n_type.is_typed then return
1642 var variable = n_expr.its_variable
1643 if variable != null then
1644 _if_true_variable_ctx = v.variable_ctx.sub_with(self, variable, n_type.stype)
1645 end
1646 _stype = v.type_bool
1647 _is_typed = true
1648 end
1649 end
1650
1651 redef class AAsCastExpr
1652 special ATypeCheckExpr
1653 redef fun after_typing(v)
1654 do
1655 check_expr_cast(v, n_expr, n_type)
1656 if not n_type.is_typed then return
1657 _stype = n_type.stype
1658 _is_typed = _stype != null
1659 end
1660 end
1661
1662 redef class AAsNotnullExpr
1663 redef fun after_typing(v)
1664 do
1665 if not v.check_expr(n_expr) then return
1666 var t = n_expr.stype
1667 if t isa MMTypeNone then
1668 v.error(n_expr, "Type error: 'as(not null)' on 'null' value.")
1669 return
1670 else if not t.is_nullable then
1671 v.warning(n_expr, "Warning: 'as(not null)' on non nullable type.")
1672 end
1673 _stype = n_expr.stype.as_notnull
1674 _is_typed = true
1675 end
1676 end
1677
1678 redef class AProxyExpr
1679 redef fun after_typing(v)
1680 do
1681 if not n_expr.is_typed then return
1682 _is_typed = true
1683 if n_expr.is_statement then return
1684 _stype = n_expr.stype
1685 end
1686 end
1687
1688 redef class AOnceExpr
1689 redef fun accept_typing(v)
1690 do
1691 if v.once_count > 0 then
1692 v.warning(self, "Useless once in a once expression.")
1693 end
1694 v.once_count = v.once_count + 1
1695
1696 super
1697
1698 v.once_count = v.once_count - 1
1699 end
1700 end
1701