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