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