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