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