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