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