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