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