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