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