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