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