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