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