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