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