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