1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Analysis property bodies, statements and expressions
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)
28 var tv
= new TypingVisitor(tc
, self)
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
42 if n
!= null then n
.accept_typing
(self)
45 # Current knowledge about variables names and types
46 readable writable attr _variable_ctx
: VariableContext
48 # Current knowledge about escapable blocks
49 readable writable attr _escapable_ctx
: EscapableContext = new EscapableContext(self)
51 # The current reciever
52 readable writable attr _self_var
: ParamVariable
54 # Block of the current method
55 readable writable attr _top_block
: PExpr
57 # List of explicit invocation of constructors of super-classes
58 readable writable attr _explicit_super_init_calls
: Array[MMMethod]
60 # Is a other constructor of the same class invoked
61 readable writable attr _explicit_other_init_call
: Bool
63 init(tc
, module) do super
65 private meth get_default_constructor_for
(n
: PNode, c
: MMLocalClass, prop
: MMSrcMethod): MMMethod
68 #var prop = v.local_property
69 #assert prop isa MMMethod
70 var candidates
= new Array[MMMethod]
71 var false_candidates
= new Array[MMMethod]
72 var parity
= prop
.signature
.arity
73 for g
in c
.global_properties
do
74 if not g
.is_init_for
(c
) then continue
76 var gps
= gp
.signature_for
(c
.get_type
)
77 assert gp
isa MMSrcMethod
78 var garity
= gps
.arity
79 if prop
!= null and gp
.name
== prop
.name
then
80 if garity
== 0 or (parity
== garity
and prop
.signature
< gps
) then
83 false_candidates
.add
(gp
)
85 else if garity
== 0 and gp
.name
== once
("init".to_symbol
) then
87 false_candidates
.add
(gp
)
89 false_candidates
.add
(gp
)
92 if candidates
.length
== 1 then
93 return candidates
.first
94 else if candidates
.length
> 0 then
95 var a
= new Array[String]
96 for p
in candidates
do
97 a
.add
("{p.full_name}{p.signature}")
99 v
.error
(n
, "Error: Conflicting default constructor to call for {c}: {a.join(", ")}.")
101 else if false_candidates
.length
> 0 then
102 var a
= new Array[String]
103 for p
in false_candidates
do
104 a
.add
("{p.full_name}{p.signature}")
106 v
.error
(n
, "Error: there is no available compatible constrctor in {c}. Discarded candidates are {a.join(", ")}.")
109 v
.error
(n
, "Error: there is no available compatible constrctor in {c}.")
115 # Associate symbols to variable and variables to type
117 private class VariableContext
118 # Look for the variable from its name
119 # Return null if nothing found
120 meth
[](s
: Symbol): Variable
122 if _dico
.has_key
(s
) then
129 # Register a new variable with its name
130 meth add
(v
: Variable)
136 # The effective static type of a given variable
137 # May be different from the declaration static type
138 meth stype
(v
: Variable): MMType
143 # Variables by name (in the current context only)
144 attr _dico
: Map[Symbol, Variable]
146 # Build a new VariableContext
147 meth sub
: SubVariableContext
149 return new SubVariableContext.with_prev
(self)
152 # Build a nested VariableContext with new variable information
153 meth sub_with
(v
: Variable, t
: MMType): SubVariableContext
155 return new CastVariableContext.with_prev
(self, v
, t
)
160 _dico
= new HashMap[Symbol, Variable]
164 private class SubVariableContext
165 special VariableContext
166 readable attr _prev
: VariableContext
170 if _dico
.has_key
(s
) then
182 init with_prev
(p
: VariableContext)
189 private class CastVariableContext
190 special SubVariableContext
191 attr _variable
: Variable
192 attr _var_type
: MMType
196 if _variable
== v
then
202 init with_prev
(p
: VariableContext, v
: Variable, t
: MMType)
210 ###############################################################################
213 private meth accept_typing
(v
: TypingVisitor)
215 accept_abs_syntax_visitor
(v
)
218 private meth after_typing
(v
: TypingVisitor) do end
221 redef class PClassdef
222 redef meth accept_typing
(v
)
224 v
.self_var
= new ParamVariable("self".to_symbol
, self)
225 v
.self_var
.stype
= local_class
.get_type
230 redef class AAttrPropdef
231 redef meth accept_typing
(v
)
234 if n_expr
!= null then
235 v
.check_conform_expr
(n_expr
, prop
.signature
.return_type
)
240 redef class AMethPropdef
241 redef readable attr _self_var
: ParamVariable
242 redef meth accept_typing
(v
)
244 v
.variable_ctx
= new VariableContext
245 _self_var
= v
.self_var
250 redef class AConcreteInitPropdef
251 readable attr _super_init_calls
: Array[MMMethod] = new Array[MMMethod]
252 readable attr _explicit_super_init_calls
: Array[MMMethod] = new Array[MMMethod]
253 redef meth accept_typing
(v
)
255 v
.top_block
= n_block
256 v
.explicit_super_init_calls
= explicit_super_init_calls
257 v
.explicit_other_init_call
= false
259 if v
.explicit_other_init_call
or method
.global
.intro
!= method
then
263 var l
= explicit_super_init_calls
.length
264 var cur_m
: MMMethod = null
265 var cur_c
: MMLocalClass = null
267 cur_m
= explicit_super_init_calls
[i
]
268 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
271 while j
< v
.local_class
.cshe
.direct_greaters
.length
do
272 var c
= v
.local_class
.cshe
.direct_greaters
[j
]
273 if c
.global
.is_interface
or c
.global
.is_universal
or c
.global
.is_mixin
then
275 else if cur_c
!= null and (c
.cshe
<= cur_c
or cur_c
.global
.is_mixin
) then
276 if c
== cur_c
then j
+= 1
277 super_init_calls
.add
(cur_m
)
280 cur_m
= explicit_super_init_calls
[i
]
281 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
287 var p
= v
.get_default_constructor_for
(self, c
, method
)
289 super_init_calls
.add
(p
)
299 redef meth after_typing
(v
)
301 # TODO: why the test?
302 if v
.variable_ctx
!= null then
303 v
.variable_ctx
.add
(variable
)
308 redef class AClosureDecl
309 # The corresponding escapable object
310 readable attr _escapable
: EscapableBlock
312 redef meth accept_typing
(v
)
314 # Register the closure for ClosureCallExpr
315 v
.variable_ctx
.add
(variable
)
317 var old_var_ctx
= v
.variable_ctx
318 v
.variable_ctx
= v
.variable_ctx
.sub
320 _escapable
= new EscapableClosure(self, variable
.closure
, null)
321 v
.escapable_ctx
.push
(_escapable
)
325 v
.variable_ctx
= old_var_ctx
331 readable attr _stype
: MMType
332 redef meth after_typing
(v
)
334 _stype
= get_stype
(v
)
339 redef readable attr _stype
: MMType
341 # Is the expression the implicit receiver
342 meth is_implicit_self
: Bool do return false
344 # Is the expression the current receiver (implicit or explicit)
345 meth is_self
: Bool do return false
347 # The variable accessed is any
348 meth its_variable
: Variable do return null
350 # The variable type information if current boolean expression is true
351 readable private attr _if_true_variable_ctx
: VariableContext
354 redef class AVardeclExpr
355 redef meth after_typing
(v
)
357 var va
= new VarVariable(n_id
.to_symbol
, self)
359 v
.variable_ctx
.add
(va
)
361 if n_type
!= null then
362 va
.stype
= n_type
.stype
363 if n_expr
!= null then
364 v
.check_conform_expr
(n_expr
, va
.stype
)
368 va
.stype
= n_expr
.stype
373 redef class ABlockExpr
374 redef meth accept_typing
(v
)
376 var old_var_ctx
= v
.variable_ctx
377 v
.variable_ctx
= v
.variable_ctx
.sub
381 v
.variable_ctx
= old_var_ctx
385 redef class AReturnExpr
386 redef meth after_typing
(v
)
388 var t
= v
.local_property
.signature
.return_type
389 if n_expr
== null and t
!= null then
390 v
.error
(self, "Error: Return without value in a function.")
391 else if n_expr
!= null and t
== null then
392 v
.error
(self, "Error: Return with value in a procedure.")
393 else if n_expr
!= null and t
!= null then
394 v
.check_conform_expr
(n_expr
, t
)
399 redef class AContinueExpr
400 redef meth after_typing
(v
)
402 var esc
= compute_escapable_block
(v
.escapable_ctx
)
403 if esc
== null then return
405 if esc
.is_break_block
then
406 v
.error
(self, "Error: 'continue' forbiden in break blocks.")
410 var t
= esc
.continue_stype
411 if n_expr
== null and t
!= null then
412 v
.error
(self, "Error: continue with a value required in this block.")
413 else if n_expr
!= null and t
== null then
414 v
.error
(self, "Error: continue without value required in this block.")
415 else if n_expr
!= null and t
!= null then
416 v
.check_conform_expr
(n_expr
, t
)
421 redef class ABreakExpr
422 redef meth after_typing
(v
)
424 var esc
= compute_escapable_block
(v
.escapable_ctx
)
425 if esc
== null then return
427 var bl
= esc
.break_list
428 if n_expr
== null and bl
!= null then
429 v
.error
(self, "Error: break with a value required in this block.")
430 else if n_expr
!= null and bl
== null then
431 v
.error
(self, "Error: break without value required in this block.")
432 else if n_expr
!= null and bl
!= null then
433 # Typing check can only be done later
440 redef meth accept_typing
(v
)
442 var old_var_ctx
= v
.variable_ctx
444 v
.check_conform_expr
(n_expr
, v
.type_bool
)
446 if n_expr
.if_true_variable_ctx
!= null then
447 v
.variable_ctx
= n_expr
.if_true_variable_ctx
451 # Restore variable ctx
452 v
.variable_ctx
= old_var_ctx
454 if n_else
!= null then
456 v
.variable_ctx
= old_var_ctx
461 redef class AWhileExpr
462 # The corresponding escapable block
463 readable attr _escapable
: EscapableBlock
465 redef meth accept_typing
(v
)
467 _escapable
= new EscapableBlock(self)
468 v
.escapable_ctx
.push
(_escapable
)
472 v
.check_conform_expr
(n_expr
, v
.type_bool
)
478 # The corresponding escapable block
479 readable attr _escapable
: EscapableBlock
481 readable attr _meth_iterator
: MMMethod
482 readable attr _meth_is_ok
: MMMethod
483 readable attr _meth_item
: MMMethod
484 readable attr _meth_next
: MMMethod
485 redef meth accept_typing
(v
)
487 _escapable
= new EscapableBlock(self)
488 v
.escapable_ctx
.push
(_escapable
)
490 v
.variable_ctx
= v
.variable_ctx
.sub
491 var va
= new AutoVariable(n_id
.to_symbol
, self)
493 v
.variable_ctx
.add
(va
)
497 var expr_type
= n_expr
.stype
498 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then
501 _meth_iterator
= expr_type
.local_class
.select_method
(once
("iterator".to_symbol
))
502 if _meth_iterator
== null then
503 v
.error
(self, "Error: Collection MUST have an iterate method")
506 var iter_type
= _meth_iterator
.signature_for
(expr_type
).return_type
507 _meth_is_ok
= iter_type
.local_class
.select_method
(once
("is_ok".to_symbol
))
508 if _meth_is_ok
== null then
509 v
.error
(self, "Error: {iter_type} MUST have an is_ok method")
512 _meth_item
= iter_type
.local_class
.select_method
(once
("item".to_symbol
))
513 if _meth_item
== null then
514 v
.error
(self, "Error: {iter_type} MUST have an item method")
517 _meth_next
= iter_type
.local_class
.select_method
(once
("next".to_symbol
))
518 if _meth_next
== null then
519 v
.error
(self, "Error: {iter_type} MUST have a next method")
522 var t
= _meth_item
.signature_for
(iter_type
).return_type
523 if not n_expr
.is_self
then t
= t
.not_for_self
526 if n_block
!= null then v
.visit
(n_block
)
529 var varctx
= v
.variable_ctx
530 assert varctx
isa SubVariableContext
531 v
.variable_ctx
= varctx
.prev
537 redef class AAssertExpr
538 redef meth after_typing
(v
)
540 v
.check_conform_expr
(n_expr
, v
.type_bool
)
541 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
546 redef meth its_variable
do return variable
548 redef meth after_typing
(v
)
550 _stype
= v
.variable_ctx
.stype
(variable
)
554 redef class AVarAssignExpr
555 redef meth after_typing
(v
)
557 var t
= v
.variable_ctx
.stype
(variable
)
558 v
.check_conform_expr
(n_value
, t
)
562 redef class AReassignFormExpr
563 # Compute and check method used through the reassigment operator
564 private meth do_lvalue_typing
(v
: TypingVisitor, type_lvalue
: MMType)
566 if type_lvalue
== null then
569 var name
= n_assign_op
.method_name
570 var lc
= type_lvalue
.local_class
571 if not lc
.has_global_property_by_name
(name
) then
572 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
575 var prop
= lc
.select_method
(name
)
576 prop
.global
.check_visibility
(v
, self, v
.module, false)
577 var psig
= prop
.signature_for
(type_lvalue
)
578 _assign_method
= prop
579 v
.check_conform_expr
(n_value
, psig
[0].not_for_self
)
580 v
.check_conform
(self, psig
.return_type
.not_for_self
, n_value
.stype
)
583 # Method used through the reassigment operator (once computed)
584 readable attr _assign_method
: MMMethod
587 redef class AVarReassignExpr
588 redef meth after_typing
(v
)
590 var t
= v
.variable_ctx
.stype
(variable
)
591 do_lvalue_typing
(v
, t
)
595 redef class PAssignOp
596 meth method_name
: Symbol is abstract
598 redef class APlusAssignOp
599 redef meth method_name
do return once
"+".to_symbol
601 redef class AMinusAssignOp
602 redef meth method_name
do return once
"-".to_symbol
605 redef class ASelfExpr
606 redef meth its_variable
do return variable
608 redef meth after_typing
(v
)
610 variable
= v
.self_var
611 _stype
= v
.variable_ctx
.stype
(variable
)
614 redef meth is_self
do return true
617 redef class AImplicitSelfExpr
618 redef meth is_implicit_self
do return true
621 redef class AIfexprExpr
622 redef meth accept_typing
(v
)
624 var old_var_ctx
= v
.variable_ctx
627 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
629 v
.variable_ctx
= old_var_ctx
632 v
.check_conform_expr
(n_expr
, v
.type_bool
)
634 _stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
638 redef class ABoolExpr
639 redef meth after_typing
(v
)
646 redef meth after_typing
(v
)
648 v
.check_conform_expr
(n_expr
, v
.type_bool
)
649 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
655 redef meth accept_typing
(v
)
657 var old_var_ctx
= v
.variable_ctx
660 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
663 if n_expr2
.if_true_variable_ctx
!= null then
664 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
666 _if_true_variable_ctx
= v
.variable_ctx
669 v
.variable_ctx
= old_var_ctx
671 v
.check_conform_expr
(n_expr
, v
.type_bool
)
672 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
678 redef meth after_typing
(v
)
680 v
.check_conform_expr
(n_expr
, v
.type_bool
)
686 redef meth after_typing
(v
)
693 redef class AFloatExpr
694 redef meth after_typing
(v
)
696 _stype
= v
.type_float
700 redef class ACharExpr
701 redef meth after_typing
(v
)
707 redef class AStringFormExpr
708 readable attr _meth_with_native
: MMMethod
709 redef meth after_typing
(v
)
711 _stype
= v
.type_string
712 _meth_with_native
= _stype
.local_class
.select_method
(once
"with_native".to_symbol
)
713 if _meth_with_native
== null then v
.error
(self, "{_stype} MUST have a with_native method.")
717 redef class ASuperstringExpr
718 readable attr _meth_with_capacity
: MMMethod
719 readable attr _meth_add
: MMMethod
720 readable attr _meth_to_s
: MMMethod
721 readable attr _atype
: MMType
722 redef meth after_typing
(v
)
724 _stype
= v
.type_string
725 _atype
= v
.type_array
(_stype
)
726 _meth_with_capacity
= _atype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
727 if _meth_with_capacity
== null then v
.error
(self, "{_atype} MUST have a with_capacity method.")
728 _meth_add
= _atype
.local_class
.select_method
(once
"add".to_symbol
)
729 if _meth_add
== null then v
.error
(self, "{_atype} MUST have an add method.")
730 _meth_to_s
= v
.type_object
.local_class
.select_method
(once
"to_s".to_symbol
)
731 if _meth_to_s
== null then v
.error
(self, "Object MUST have a to_s method.")
735 redef class ANullExpr
736 redef meth after_typing
(v
)
742 redef class AArrayExpr
743 readable attr _meth_with_capacity
: MMMethod
744 readable attr _meth_add
: MMMethod
746 redef meth after_typing
(v
)
748 var stype
= v
.check_conform_multiexpr
(null, n_exprs
)
749 if stype
== null then return
753 private meth do_typing
(v
: TypingVisitor, element_type
: MMType)
755 _stype
= v
.type_array
(element_type
)
757 _meth_with_capacity
= _stype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
758 if _meth_with_capacity
== null then v
.error
(self, "{_stype} MUST have a with_capacity method.")
759 _meth_add
= _stype
.local_class
.select_method
(once
"add".to_symbol
)
760 if _meth_add
== null then v
.error
(self, "{_stype} MUST have an add method.")
764 redef class ARangeExpr
765 readable attr _meth_init
: MMMethod
766 redef meth after_typing
(v
)
768 var ntype
= n_expr
.stype
769 var ntype2
= n_expr2
.stype
770 if ntype
== null or ntype
== null then
773 if ntype
< ntype2
then
775 else if not ntype2
< ntype
then
776 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
779 var dtype
= v
.type_discrete
780 v
.check_conform_expr
(n_expr
, dtype
)
781 v
.check_conform_expr
(n_expr2
, dtype
)
782 _stype
= v
.type_range
(ntype
)
786 redef class ACrangeExpr
787 redef meth after_typing
(v
)
790 _meth_init
= stype
.local_class
.select_method
(once
"init".to_symbol
)
793 redef class AOrangeExpr
794 redef meth after_typing
(v
)
797 _meth_init
= stype
.local_class
.select_method
(once
"without_last".to_symbol
)
802 redef class ASuperExpr
803 special ASuperInitCall
804 # readable attr _prop: MMSrcMethod
805 readable attr _init_in_superclass
: MMMethod
806 redef meth after_typing
(v
)
808 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
809 if not precs
.is_empty
then
810 v
.local_property
.need_super
= true
811 else if v
.local_property
.global
.is_init
then
812 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
813 for p
in base_precs
do
814 if not p
.global
.is_init
then
815 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
817 precs
.add
(v
.local_class
[p
.global
])
820 if precs
.is_empty
then
821 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
823 else if precs
.length
> 1 then
824 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
827 var p
= base_precs
.first
828 assert p
isa MMMethod
829 _init_in_superclass
= p
830 register_super_init_call
(v
, p
)
831 if n_args
.length
> 0 then
832 var signature
= get_signature
(v
, v
.self_var
.stype
, p
, true)
833 _arguments
= process_signature
(v
, signature
, p
.name
, n_args
.to_a
)
836 v
.error
(self, "Error: No super method to call for {v.local_property}.")
840 if precs
.first
.signature_for
(v
.self_var
.stype
).return_type
!= null then
841 var stypes
= new Array[MMType]
842 var stype
: MMType = null
844 assert prop
isa MMMethod
845 var t
= prop
.signature_for
(v
.self_var
.stype
).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
847 if stype
== null or stype
< t
then
852 v
.check_conform
(self, t
, stype
)
856 var p
= v
.local_property
857 assert p
isa MMSrcMethod
862 redef class AAttrFormExpr
864 readable attr _prop
: MMAttribute
866 # Attribute type of the acceded attribute
867 readable attr _attr_type
: MMType
869 # Compute the attribute accessed
870 private meth do_typing
(v
: TypingVisitor)
872 if not v
.check_expr
(n_expr
) then return
873 var type_recv
= n_expr
.stype
874 var name
= n_id
.to_symbol
875 var lc
= type_recv
.local_class
876 if not lc
.has_global_property_by_name
(name
) then
877 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
880 var prop
= lc
.select_attribute
(name
)
881 if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
882 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
885 var at
= prop
.signature_for
(type_recv
).return_type
886 if not n_expr
.is_self
then at
= at
.not_for_self
891 redef class AAttrExpr
892 redef meth after_typing
(v
)
902 redef class AAttrAssignExpr
903 redef meth after_typing
(v
)
909 v
.check_conform_expr
(n_value
, attr_type
)
913 redef class AAttrReassignExpr
914 redef meth after_typing
(v
)
920 do_lvalue_typing
(v
, attr_type
)
926 # The signature of the called property
927 readable attr _prop_signature
: MMSignature
929 # Compute the called global property
930 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])
932 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
933 if prop
== null then return
934 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
935 if sig
== null then return
936 var args
= process_signature
(v
, sig
, prop
.name
, raw_args
)
937 if args
== null then return
938 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
940 _prop_signature
= sig
945 private meth get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): MMMethod
947 if type_recv
== null then return null
948 var lc
= type_recv
.local_class
949 var prop
: MMMethod = null
950 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
951 if prop
== null and v
.local_property
.global
.is_init
then
952 var props
= type_recv
.local_class
.super_methods_named
(name
)
953 if props
.length
> 1 then
954 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
956 else if props
.length
== 1 then
957 var p
= type_recv
.local_class
[props
.first
.global
]
958 assert p
isa MMMethod
964 if is_implicit_self
then
965 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
967 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
974 # Get the signature for a local property and a receiver
975 private meth get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
977 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
978 var psig
= prop
.signature_for
(type_recv
)
979 if not recv_is_self
then psig
= psig
.not_for_self
983 # Check the conformity of a set of arguments `raw_args' to a signature.
984 private meth process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: Array[PExpr]): Array[PExpr]
986 var par_vararg
= psig
.vararg_rank
987 var par_arity
= psig
.arity
989 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
990 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
991 v
.error
(self, "Error: '{name}' arity missmatch.")
995 var args
= new Array[PExpr]
996 for par_idx
in [0..par_arity
[ do
998 var par_type
= psig
[par_idx
]
999 if par_idx
== par_vararg
then
1000 var star
= new Array[PExpr]
1001 for i
in [0..(raw_arity-par_arity
)] do
1002 a
= raw_args
[arg_idx
]
1003 v
.check_conform_expr
(a
, par_type
)
1005 arg_idx
= arg_idx
+ 1
1007 var aa
= new AArrayExpr.init_aarrayexpr
(star
)
1008 aa
.do_typing
(v
, par_type
)
1011 a
= raw_args
[arg_idx
]
1012 v
.check_conform_expr
(a
, par_type
)
1013 arg_idx
= arg_idx
+ 1
1020 # Check the conformity of a set of defined closures
1021 private meth process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: Array[PClosureDef]): MMType
1023 var t
= psig
.return_type
1024 var cs
= psig
.closures
# Declared closures
1027 if not c
.is_optional
then min_arity
+= 1
1030 if cs
.length
== 0 then
1031 v
.error
(self, "Error: {name} does not require blocks.")
1032 else if cd
.length
> cs
.length
or cd
.length
< min_arity
then
1033 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
1035 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1036 var break_list
: Array[ABreakExpr] = null
1037 if t
!= null then break_list
= new Array[ABreakExpr]
1039 # Process each closure definition
1040 for i
in [0..cd
.length
[ do
1043 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1044 v
.escapable_ctx
.push
(esc
)
1045 cdi
.accept_typing2
(v
, esc
)
1049 # Check break type conformity
1050 if break_list
!= null then
1051 t
= v
.check_conform_multiexpr
(t
, break_list
)
1054 else if min_arity
!= 0 then
1055 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1060 # The invoked method (once computed)
1061 readable attr _prop
: MMMethod
1063 # The real arguments used (after star transformation) (once computed)
1064 readable attr _arguments
: Array[PExpr]
1066 # The return type (if any) (once computed)
1067 readable attr _return_type
: MMType
1070 # A possible call of constructor in a super class
1071 # Could be an explicit call or with the 'super' keyword
1072 class ASuperInitCall
1073 special AAbsSendExpr
1074 private meth register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1076 if parent
!= v
.top_block
and self != v
.top_block
then
1077 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1079 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
1080 var prev_class
: MMLocalClass = null
1081 if not v
.explicit_super_init_calls
.is_empty
then
1082 prev_class
= v
.explicit_super_init_calls
.last
.global
.intro
.local_class
1084 var order
= v
.local_class
.cshe
.reverse_linear_extension
1085 if cla
== v
.local_class
then
1086 v
.explicit_other_init_call
= true
1087 else if not order
.has
(cla
) then
1088 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1089 else if cla
== prev_class
then
1090 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1092 var last_is_found
= prev_class
== null
1094 if c
== prev_class
then
1095 last_is_found
= true
1096 else if c
== cla
then
1097 if not last_is_found
then
1098 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1100 v
.explicit_super_init_calls
.add
(property
)
1109 redef class ANewExpr
1110 special AAbsSendExpr
1111 redef meth after_typing
(v
)
1113 var t
= n_type
.stype
1114 if t
== null then return
1115 if t
.local_class
.global
.is_abstract
then
1116 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1120 if n_id
== null then
1121 name
= once
"init".to_symbol
1123 name
= n_id
.to_symbol
1126 do_typing
(v
, t
, false, false, name
, n_args
.to_a
, null)
1127 if prop
== null then return
1129 if not prop
.global
.is_init
then
1130 v
.error
(self, "Error: {prop} is not a constructor.")
1137 redef class ASendExpr
1138 special ASuperInitCall
1139 # Name of the invoked property
1140 meth name
: Symbol is abstract
1142 # Raw arguments used (withour star transformation)
1143 meth raw_arguments
: Array[PExpr] is abstract
1145 # Closure definitions
1146 meth closure_defs
: Array[PClosureDef] do return null
1148 redef meth after_typing
(v
)
1153 private meth do_all_typing
(v
: TypingVisitor)
1155 if not v
.check_expr
(n_expr
) then return
1156 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1157 if prop
== null then return
1159 if prop
.global
.is_init
then
1160 if not v
.local_property
.global
.is_init
then
1161 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1162 else if not n_expr
.is_self
then
1163 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1165 register_super_init_call
(v
, prop
)
1169 _stype
= return_type
1173 class ASendReassignExpr
1175 special AReassignFormExpr
1176 readable attr _read_prop
: MMMethod
1177 redef meth do_all_typing
(v
)
1179 if not v
.check_expr
(n_expr
) then return
1180 var raw_args
= raw_arguments
1181 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1182 if prop
== null then return
1183 if prop
.global
.is_init
then
1184 if not v
.local_property
.global
.is_init
then
1185 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1186 else if not n_expr
.is_self
then
1187 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1190 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1191 if not n_expr
.is_self
then t
= t
.not_for_self
1193 do_lvalue_typing
(v
, t
)
1196 var old_args
= arguments
1197 raw_args
.add
(n_value
)
1199 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1200 if prop
== null then return
1201 if prop
.global
.is_init
then
1202 if not v
.local_property
.global
.is_init
then
1203 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1204 else if not n_expr
.is_self
then
1205 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1209 _arguments
= old_args
# FIXME: What if star parameters do not match betwen the two methods?
1213 redef class ABinopExpr
1214 redef meth raw_arguments
do return [n_expr2
]
1217 redef meth name
do return once
"==".to_symbol
1220 redef meth name
do return once
"!=".to_symbol
1223 redef meth name
do return once
"<".to_symbol
1226 redef meth name
do return once
"<=".to_symbol
1229 redef meth name
do return once
">".to_symbol
1232 redef meth name
do return once
">=".to_symbol
1234 redef class APlusExpr
1235 redef meth name
do return once
"+".to_symbol
1237 redef class AMinusExpr
1238 redef meth name
do return once
"-".to_symbol
1240 redef class AStarshipExpr
1241 redef meth name
do return once
"<=>".to_symbol
1243 redef class AStarExpr
1244 redef meth name
do return once
"*".to_symbol
1246 redef class ASlashExpr
1247 redef meth name
do return once
"/".to_symbol
1249 redef class APercentExpr
1250 redef meth name
do return once
"%".to_symbol
1253 redef class AUminusExpr
1254 redef meth name
do return once
"unary -".to_symbol
1255 redef meth raw_arguments
do return null
1258 redef class ACallFormExpr
1259 redef meth after_typing
(v
)
1261 if n_expr
!= null and n_expr
.is_implicit_self
then
1262 var name
= n_id
.to_symbol
1263 var variable
= v
.variable_ctx
[name
]
1264 if variable
!= null then
1265 if variable
isa ClosureVariable then
1266 var n
= new AClosureCallExpr(n_id
, n_args
, n_closure_defs
)
1268 n
.variable
= variable
1272 if not n_args
.is_empty
then
1273 v
.error
(self, "Error: {name} is variable, not a function.")
1275 var vform
= variable_create
(variable
)
1276 vform
.variable
= variable
1278 vform
.after_typing
(v
)
1287 redef meth closure_defs
1289 if n_closure_defs
== null or n_closure_defs
.is_empty
then
1292 return n_closure_defs
.to_a
1296 # Create a variable acces corresponding to the call form
1297 meth variable_create
(variable
: Variable): AVarFormExpr is abstract
1300 redef class ACallExpr
1301 redef meth variable_create
(variable
)
1303 return new AVarExpr.init_avarexpr
(n_id
)
1306 redef meth name
do return n_id
.to_symbol
1307 redef meth raw_arguments
do return n_args
.to_a
1310 redef class ACallAssignExpr
1311 redef meth variable_create
(variable
)
1313 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1316 redef meth name
do return (n_id
.text
+ "=").to_symbol
1317 redef meth raw_arguments
do
1318 var res
= n_args
.to_a
1324 redef class ACallReassignExpr
1325 special ASendReassignExpr
1326 redef meth variable_create
(variable
)
1328 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1331 redef meth name
do return n_id
.to_symbol
1332 redef meth raw_arguments
do return n_args
.to_a
1335 redef class ABraExpr
1336 redef meth name
do return once
"[]".to_symbol
1337 redef meth raw_arguments
do return n_args
.to_a
1340 redef class ABraAssignExpr
1341 redef meth name
do return once
"[]=".to_symbol
1342 redef meth raw_arguments
do
1343 var res
= n_args
.to_a
1349 redef class ABraReassignExpr
1350 special ASendReassignExpr
1351 redef meth name
do return once
"[]".to_symbol
1352 redef meth raw_arguments
do return n_args
.to_a
1355 redef class AInitExpr
1356 redef meth name
do return once
"init".to_symbol
1357 redef meth raw_arguments
do return n_args
.to_a
1360 redef class AClosureCallExpr
1361 redef meth after_typing
(v
)
1364 var sig
= va
.closure
.signature
1365 var args
= process_signature
(v
, sig
, n_id
.to_symbol
, n_args
.to_a
)
1366 if closure_defs
!= null then
1367 process_closures
(v
, sig
, n_id
.to_symbol
, closure_defs
)
1369 if args
== null then return
1371 _prop_signature
= sig
1373 _stype
= sig
.return_type
1377 redef class PClosureDef
1378 # The corresponding escapable object
1379 readable attr _escapable
: EscapableBlock
1381 attr _accept_typing2
: Bool
1382 redef meth accept_typing
(v
)
1384 # Typing is deferred, wait accept_typing2(v)
1385 if _accept_typing2
then super
1388 private meth accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure) is abstract
1391 redef class AClosureDef
1392 redef meth accept_typing2
(v
, esc
)
1396 var sig
= esc
.closure
.signature
1397 if sig
.arity
!= n_id
.length
then
1398 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1402 closure
= esc
.closure
1404 v
.variable_ctx
= v
.variable_ctx
.sub
1405 variables
= new Array[AutoVariable]
1406 for i
in [0..n_id
.length
[ do
1407 var va
= new AutoVariable(n_id
[i
].to_symbol
, self)
1410 v
.variable_ctx
.add
(va
)
1413 _accept_typing2
= true
1418 redef class AIsaExpr
1419 redef meth after_typing
(v
)
1421 var variable
= n_expr
.its_variable
1422 if variable
!= null then
1423 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(variable
, n_type
.stype
)
1425 _stype
= v
.type_bool
1429 redef class AAsCastExpr
1430 redef meth after_typing
(v
)
1432 v
.check_expr
(n_expr
)
1433 _stype
= n_type
.stype
1437 redef class AProxyExpr
1438 redef meth after_typing
(v
)
1440 _stype
= n_expr
.stype