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