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