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