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
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)
27 var tv
= new TypingVisitor(tc
, self)
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
41 if n
!= null then n
.accept_typing
(self)
44 # Current knowledge about variables names and types
45 readable writable attr _variable_ctx
: VariableContext
47 # The current reciever
48 readable writable attr _self_var
: ParamVariable
50 # Block of the current method
51 readable writable attr _top_block
: PExpr
53 # List of explicit invocation of constructors of super-classes
54 readable writable attr _explicit_super_init_calls
: Array[MMMethod]
56 # Is a other constructor of the same class invoked
57 readable writable attr _explicit_other_init_call
: Bool
59 init(tc
, module) do super
61 private meth get_default_constructor_for
(n
: PNode, c
: MMLocalClass, prop
: MMSrcMethod): MMMethod
64 #var prop = v.local_property
65 #assert prop isa MMMethod
66 var candidates
= new Array[MMMethod]
67 var false_candidates
= new Array[MMMethod]
68 var parity
= prop
.signature
.arity
69 for g
in c
.global_properties
do
70 if not g
.is_init_for
(c
) then continue
72 var gps
= gp
.signature_for
(c
.get_type
)
73 assert gp
isa MMSrcMethod
74 var garity
= gps
.arity
75 if prop
!= null and gp
.name
== prop
.name
then
76 if garity
== 0 or (parity
== garity
and prop
.signature
< gps
) then
79 false_candidates
.add
(gp
)
81 else if garity
== 0 and gp
.name
== once
("init".to_symbol
) then
83 false_candidates
.add
(gp
)
85 false_candidates
.add
(gp
)
88 if candidates
.length
== 1 then
89 return candidates
.first
90 else if candidates
.length
> 0 then
91 var a
= new Array[String]
92 for p
in candidates
do
93 a
.add
("{p.full_name}{p.signature}")
95 v
.error
(n
, "Error: Conflicting default constructor to call for {c}: {a.join(", ")}.")
97 else if false_candidates
.length
> 0 then
98 var a
= new Array[String]
99 for p
in false_candidates
do
100 a
.add
("{p.full_name}{p.signature}")
102 v
.error
(n
, "Error: there is no available compatible constrctor in {c}. Discarded candidates are {a.join(", ")}.")
105 v
.error
(n
, "Error: there is no available compatible constrctor in {c}.")
111 # Associate symbols to variable and variables to type
113 private class VariableContext
114 # Look for the variable from its name
115 # Return null if nothing found
116 meth
[](s
: Symbol): Variable
118 if _dico
.has_key
(s
) then
125 # Register a new variable with its name
126 meth add
(v
: Variable)
132 # The effective static type of a given variable
133 # May be different from the declaration static type
134 meth stype
(v
: Variable): MMType
139 # Variables by name (in the current context only)
140 attr _dico
: Map[Symbol, Variable]
142 # Build a new VariableContext
143 meth sub
: SubVariableContext
145 return new SubVariableContext.with_prev
(self, null, null)
148 # Build a nested VariableContext with new variable information
149 meth sub_with
(v
: Variable, t
: MMType): SubVariableContext
151 return new SubVariableContext.with_prev
(self, v
, t
)
156 _dico
= new HashMap[Symbol, Variable]
160 private class SubVariableContext
161 special VariableContext
162 readable attr _prev
: VariableContext
163 attr _variable
: Variable
164 attr _var_type
: MMType
168 if _dico
.has_key
(s
) then
177 if _variable
== v
then
183 init with_prev
(p
: VariableContext, v
: Variable, t
: MMType)
193 ###############################################################################
196 private meth accept_typing
(v
: TypingVisitor)
198 accept_abs_syntax_visitor
(v
)
201 private meth after_typing
(v
: TypingVisitor) do end
204 redef class PClassdef
205 redef meth accept_typing
(v
)
207 v
.self_var
= new ParamVariable("self".to_symbol
, self)
208 v
.self_var
.stype
= local_class
.get_type
213 redef class AAttrPropdef
214 redef meth accept_typing
(v
)
217 if n_expr
!= null then
218 v
.check_conform_expr
(n_expr
, prop
.signature
.return_type
)
223 redef class AMethPropdef
224 redef readable attr _self_var
: ParamVariable
225 redef meth accept_typing
(v
)
227 v
.variable_ctx
= new VariableContext
228 _self_var
= v
.self_var
233 redef class AConcreteInitPropdef
234 readable attr _super_init_calls
: Array[MMMethod] = new Array[MMMethod]
235 readable attr _explicit_super_init_calls
: Array[MMMethod] = new Array[MMMethod]
236 redef meth accept_typing
(v
)
238 v
.top_block
= n_block
239 v
.explicit_super_init_calls
= explicit_super_init_calls
240 v
.explicit_other_init_call
= false
242 if v
.explicit_other_init_call
or method
.global
.intro
!= method
then
246 var l
= explicit_super_init_calls
.length
247 var cur_m
: MMMethod = null
248 var cur_c
: MMLocalClass = null
250 cur_m
= explicit_super_init_calls
[i
]
251 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
254 while j
< v
.local_class
.cshe
.direct_greaters
.length
do
255 var c
= v
.local_class
.cshe
.direct_greaters
[j
]
256 if c
.global
.is_interface
or c
.global
.is_universal
or c
.global
.is_mixin
then
258 else if cur_c
!= null and (c
.cshe
<= cur_c
or cur_c
.global
.is_mixin
) then
259 if c
== cur_c
then j
+= 1
260 super_init_calls
.add
(cur_m
)
263 cur_m
= explicit_super_init_calls
[i
]
264 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
270 var p
= v
.get_default_constructor_for
(self, c
, method
)
272 super_init_calls
.add
(p
)
282 redef meth after_typing
(v
)
284 if v
.variable_ctx
!= null then
285 v
.variable_ctx
.add
(variable
)
291 readable attr _stype
: MMType
292 redef meth after_typing
(v
)
294 _stype
= get_stype
(v
)
299 redef readable attr _stype
: MMType
301 # Is the expression the implicit receiver
302 meth is_implicit_self
: Bool do return false
304 # Is the expression the current receiver (implicit or explicit)
305 meth is_self
: Bool do return false
307 # The variable accessed is any
308 meth its_variable
: Variable do return null
310 # The variable type information if current boolean expression is true
311 readable private attr _if_true_variable_ctx
: VariableContext
314 redef class AVardeclExpr
315 redef meth after_typing
(v
)
317 var va
= new VarVariable(n_id
.to_symbol
, self)
319 v
.variable_ctx
.add
(va
)
321 if n_type
!= null then
322 va
.stype
= n_type
.stype
323 if n_expr
!= null then
324 v
.check_conform_expr
(n_expr
, va
.stype
)
328 va
.stype
= n_expr
.stype
333 redef class ABlockExpr
334 redef meth accept_typing
(v
)
336 var old_var_ctx
= v
.variable_ctx
337 v
.variable_ctx
= v
.variable_ctx
.sub
341 v
.variable_ctx
= old_var_ctx
345 redef class AReturnExpr
346 redef meth after_typing
(v
)
348 var t
= v
.local_property
.signature
.return_type
349 if n_expr
== null and t
!= null then
350 v
.error
(self, "Error: Return without value in a function.")
351 else if n_expr
!= null and t
== null then
352 v
.error
(self, "Error: Return with value in a procedure.")
353 else if n_expr
!= null and t
!= null then
354 v
.check_conform_expr
(n_expr
, t
)
360 redef meth accept_typing
(v
)
362 var old_var_ctx
= v
.variable_ctx
364 v
.check_conform_expr
(n_expr
, v
.type_bool
)
366 if n_expr
.if_true_variable_ctx
!= null then
367 v
.variable_ctx
= n_expr
.if_true_variable_ctx
371 # Restore variable ctx
372 v
.variable_ctx
= old_var_ctx
374 if n_else
!= null then
376 v
.variable_ctx
= old_var_ctx
381 redef class AWhileExpr
382 redef meth after_typing
(v
)
384 v
.check_conform_expr
(n_expr
, v
.type_bool
)
389 redef meth after_typing
(v
)
391 # pop context created in AForVardeclExpr
392 var varctx
= v
.variable_ctx
393 assert varctx
isa SubVariableContext
394 v
.variable_ctx
= varctx
.prev
398 redef class AForVardeclExpr
399 redef meth after_typing
(v
)
401 v
.variable_ctx
= v
.variable_ctx
.sub
402 var va
= new AutoVariable(n_id
.to_symbol
, self)
404 v
.variable_ctx
.add
(va
)
406 var expr_type
= n_expr
.stype
407 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then
410 var prop
= expr_type
.local_class
.select_method
(once
("iterator".to_symbol
))
412 v
.error
(self, "Error: Collection MUST have an iterate method")
415 var iter_type
= prop
.signature_for
(expr_type
).return_type
416 var prop2
= iter_type
.local_class
.select_method
(once
("item".to_symbol
))
417 if prop2
== null then
418 v
.error
(self, "Error: {iter_type} MUST have an item method")
421 var t
= prop2
.signature_for
(iter_type
).return_type
422 if not n_expr
.is_self
then t
= t
.not_for_self
427 redef class AAssertExpr
428 redef meth after_typing
(v
)
430 v
.check_conform_expr
(n_expr
, v
.type_bool
)
431 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
436 redef meth its_variable
do return variable
438 redef meth after_typing
(v
)
440 _stype
= v
.variable_ctx
.stype
(variable
)
444 redef class AVarAssignExpr
445 redef meth after_typing
(v
)
447 var t
= v
.variable_ctx
.stype
(variable
)
448 v
.check_conform_expr
(n_value
, t
)
452 redef class AReassignFormExpr
453 # Compute and check method used through the reassigment operator
454 private meth do_lvalue_typing
(v
: TypingVisitor, type_lvalue
: MMType)
456 if type_lvalue
== null then
459 var name
= n_assign_op
.method_name
460 var prop
= type_lvalue
.local_class
.select_method
(name
)
462 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
465 prop
.global
.check_visibility
(v
, self, v
.module, false)
466 var psig
= prop
.signature_for
(type_lvalue
)
467 _assign_method
= prop
468 v
.check_conform_expr
(n_value
, psig
[0].not_for_self
)
469 v
.check_conform
(self, psig
.return_type
.not_for_self
, n_value
.stype
)
472 # Method used through the reassigment operator (once computed)
473 readable attr _assign_method
: MMMethod
476 redef class AVarReassignExpr
477 redef meth after_typing
(v
)
479 var t
= v
.variable_ctx
.stype
(variable
)
480 do_lvalue_typing
(v
, t
)
484 redef class PAssignOp
485 meth method_name
: Symbol is abstract
487 redef class APlusAssignOp
488 redef meth method_name
do return once
"+".to_symbol
490 redef class AMinusAssignOp
491 redef meth method_name
do return once
"-".to_symbol
494 redef class ASelfExpr
495 redef meth its_variable
do return variable
497 redef meth after_typing
(v
)
499 variable
= v
.self_var
500 _stype
= v
.variable_ctx
.stype
(variable
)
503 redef meth is_self
do return true
506 redef class AImplicitSelfExpr
507 redef meth is_implicit_self
do return true
510 redef class AIfexprExpr
511 redef meth accept_typing
(v
)
513 var old_var_ctx
= v
.variable_ctx
516 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
518 v
.variable_ctx
= old_var_ctx
521 v
.check_conform_expr
(n_expr
, v
.type_bool
)
523 if not v
.check_expr
(n_then
) or not v
.check_expr
(n_else
) then return
526 var te
= n_else
.stype
529 else if not te
< t
then
530 v
.error
(self, "Type error: {te} is not a subtype of {t}.")
538 redef class ABoolExpr
539 redef meth after_typing
(v
)
546 redef meth after_typing
(v
)
548 v
.check_conform_expr
(n_expr
, v
.type_bool
)
549 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
555 redef meth accept_typing
(v
)
557 var old_var_ctx
= v
.variable_ctx
560 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
563 if n_expr2
.if_true_variable_ctx
!= null then
564 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
566 _if_true_variable_ctx
= v
.variable_ctx
569 v
.variable_ctx
= old_var_ctx
571 v
.check_conform_expr
(n_expr
, v
.type_bool
)
572 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
578 redef meth after_typing
(v
)
580 v
.check_conform_expr
(n_expr
, v
.type_bool
)
586 redef meth after_typing
(v
)
593 redef class AFloatExpr
594 redef meth after_typing
(v
)
596 _stype
= v
.type_float
600 redef class ACharExpr
601 redef meth after_typing
(v
)
607 redef class AStringFormExpr
608 redef meth after_typing
(v
)
610 _stype
= v
.type_string
614 redef class ASuperstringExpr
615 redef meth after_typing
(v
)
617 _stype
= v
.type_string
621 redef class ANullExpr
622 redef meth after_typing
(v
)
628 redef class AArrayExpr
629 private meth stype
=(t
: MMType) do _stype
= t
631 redef meth after_typing
(v
)
633 var stype
: MMType = null
636 if stype
== null or (ntype
!= null and stype
< ntype
) then
641 v
.check_conform_expr
(n
, stype
)
643 _stype
= v
.type_array
(stype
)
647 redef class ARangeExpr
648 redef meth after_typing
(v
)
650 var ntype
= n_expr
.stype
651 var ntype2
= n_expr2
.stype
652 if ntype
== null or ntype
== null then
655 if ntype
< ntype2
then
657 else if not ntype2
< ntype
then
658 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
661 var dtype
= v
.type_discrete
662 v
.check_conform_expr
(n_expr
, dtype
)
663 v
.check_conform_expr
(n_expr2
, dtype
)
664 _stype
= v
.type_range
(ntype
)
668 redef class ASuperExpr
669 special ASuperInitCall
670 # readable attr _prop: MMSrcMethod
671 readable attr _init_in_superclass
: MMMethod
672 redef meth after_typing
(v
)
674 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
675 if not precs
.is_empty
then
676 v
.local_property
.need_super
= true
677 else if v
.local_property
.global
.is_init
then
678 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
679 for p
in base_precs
do
680 if not p
.global
.is_init
then
681 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
683 precs
.add
(v
.local_class
[p
.global
])
686 if precs
.is_empty
then
687 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
689 else if precs
.length
> 1 then
690 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
693 var p
= base_precs
.first
694 assert p
isa MMMethod
695 _init_in_superclass
= p
696 register_super_init_call
(v
, p
)
697 if n_args
.length
> 0 then
698 var signature
= get_signature
(v
, v
.self_var
.stype
, p
, true)
699 _arguments
= process_signature
(v
, signature
, p
, n_args
.to_a
)
702 v
.error
(self, "Error: No super method to call for {v.local_property}.")
706 if precs
.first
.signature_for
(v
.self_var
.stype
).return_type
!= null then
707 var stypes
= new Array[MMType]
708 var stype
: MMType = null
710 assert prop
isa MMMethod
711 var t
= prop
.signature_for
(v
.self_var
.stype
).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
713 if stype
== null or stype
< t
then
718 v
.check_conform
(self, t
, stype
)
722 var p
= v
.local_property
723 assert p
isa MMSrcMethod
728 redef class AAttrFormExpr
730 readable attr _prop
: MMAttribute
732 # Attribute type of the acceded attribute
733 readable attr _attr_type
: MMType
735 # Compute the attribute accessed
736 private meth do_typing
(v
: TypingVisitor)
738 if not v
.check_expr
(n_expr
) then return
739 var type_recv
= n_expr
.stype
740 var name
= n_id
.to_symbol
741 var prop
= type_recv
.local_class
.select_attribute
(name
)
743 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
745 else if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
746 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
749 var at
= prop
.signature_for
(type_recv
).return_type
750 if not n_expr
.is_self
then at
= at
.not_for_self
755 redef class AAttrExpr
756 redef meth after_typing
(v
)
766 redef class AAttrAssignExpr
767 redef meth after_typing
(v
)
773 v
.check_conform_expr
(n_value
, attr_type
)
777 redef class AAttrReassignExpr
778 redef meth after_typing
(v
)
784 do_lvalue_typing
(v
, attr_type
)
790 # Compute the called global property
791 private meth do_typing
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, recv_is_self
: Bool, name
: Symbol, raw_args
: Array[PExpr])
793 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
794 if prop
== null then return
795 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
796 if sig
== null then return
797 var args
= process_signature
(v
, sig
, prop
, raw_args
)
798 if args
== null then return
803 private meth get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): MMMethod
805 if type_recv
== null then return null
806 var prop
= type_recv
.local_class
.select_method
(name
)
807 if prop
== null and v
.local_property
.global
.is_init
then
808 var props
= type_recv
.local_class
.super_methods_named
(name
)
809 if props
.length
> 1 then
810 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
812 else if props
.length
== 1 then
813 var p
= type_recv
.local_class
[props
.first
.global
]
814 assert p
isa MMMethod
820 if is_implicit_self
then
821 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
823 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
830 private meth get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
832 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
833 var psig
= prop
.signature_for
(type_recv
)
834 if not recv_is_self
then psig
= psig
.not_for_self
838 # Check the conformity of a set of arguments `raw_args' to a signature.
839 private meth process_signature
(v
: TypingVisitor, psig
: MMSignature, prop
: MMMethod, raw_args
: Array[PExpr]): Array[PExpr]
841 var par_vararg
= psig
.vararg_rank
842 var par_arity
= psig
.arity
844 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
845 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
846 v
.error
(self, "Error: Method '{prop}' arity missmatch.")
850 var args
= new Array[PExpr]
851 for par_idx
in [0..par_arity
[ do
853 var par_type
= psig
[par_idx
]
854 if par_idx
== par_vararg
then
855 var star
= new Array[PExpr]
856 for i
in [0..(raw_arity-par_arity
)] do
857 a
= raw_args
[arg_idx
]
858 v
.check_conform_expr
(a
, par_type
)
860 arg_idx
= arg_idx
+ 1
862 var aa
= new AArrayExpr.init_aarrayexpr
(star
)
863 aa
.stype
= v
.type_array
(par_type
)
866 a
= raw_args
[arg_idx
]
867 v
.check_conform_expr
(a
, par_type
)
868 arg_idx
= arg_idx
+ 1
875 # The invoked method (once computed)
876 readable attr _prop
: MMMethod
878 # The real arguments used (after star transformation) (once computed)
879 readable attr _arguments
: Array[PExpr]
882 # A possible call of constructor in a super class
883 # Could be an explicit call or with the 'super' keyword
886 private meth register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
888 if parent
!= v
.top_block
and self != v
.top_block
then
889 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
891 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
892 var prev_class
: MMLocalClass = null
893 if not v
.explicit_super_init_calls
.is_empty
then
894 prev_class
= v
.explicit_super_init_calls
.last
.global
.intro
.local_class
896 var order
= v
.local_class
.cshe
.reverse_linear_extension
897 if cla
== v
.local_class
then
898 v
.explicit_other_init_call
= true
899 else if not order
.has
(cla
) then
900 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
901 else if cla
== prev_class
then
902 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
904 var last_is_found
= prev_class
== null
906 if c
== prev_class
then
908 else if c
== cla
then
909 if not last_is_found
then
910 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
912 v
.explicit_super_init_calls
.add
(property
)
923 redef meth after_typing
(v
)
926 if t
== null then return
927 if t
.local_class
.global
.is_abstract
then
928 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
933 name
= once
"init".to_symbol
935 name
= n_id
.to_symbol
938 do_typing
(v
, t
, false, false, name
, n_args
.to_a
)
939 if prop
== null then return
941 if not prop
.global
.is_init
then
942 v
.error
(self, "Error: {prop} is not a constructor.")
949 redef class ASendExpr
950 special ASuperInitCall
951 # Name of the invoked property
952 meth name
: Symbol is abstract
954 # Raw arguments used (withour star transformation)
955 meth raw_arguments
: Array[PExpr] is abstract
957 redef meth after_typing
(v
)
962 private meth do_all_typing
(v
: TypingVisitor)
964 if not v
.check_expr
(n_expr
) then return
965 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
)
966 if prop
== null then return
967 if prop
.global
.is_init
then
968 if not v
.local_property
.global
.is_init
then
969 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
970 else if not n_expr
.is_self
then
971 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
973 register_super_init_call
(v
, prop
)
976 var t
= prop
.signature_for
(n_expr
.stype
).return_type
977 if t
!= null and not n_expr
.is_self
then t
= t
.not_for_self
982 class ASendReassignExpr
984 special AReassignFormExpr
985 readable attr _read_prop
: MMMethod
986 redef meth do_all_typing
(v
)
988 if not v
.check_expr
(n_expr
) then return
989 var raw_args
= raw_arguments
990 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
)
991 if prop
== null then return
992 if prop
.global
.is_init
then
993 if not v
.local_property
.global
.is_init
then
994 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
995 else if not n_expr
.is_self
then
996 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
999 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1000 if not n_expr
.is_self
then t
= t
.not_for_self
1002 do_lvalue_typing
(v
, t
)
1005 var old_args
= arguments
1006 raw_args
.add
(n_value
)
1008 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
)
1009 if prop
== null then return
1010 if prop
.global
.is_init
then
1011 if not v
.local_property
.global
.is_init
then
1012 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1013 else if not n_expr
.is_self
then
1014 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1018 _arguments
= old_args
# FIXME: What if star parameters do not match betwen the two methods?
1022 redef class ABinopExpr
1023 redef meth raw_arguments
do return [n_expr2
]
1026 redef meth name
do return once
"==".to_symbol
1029 redef meth name
do return once
"!=".to_symbol
1032 redef meth name
do return once
"<".to_symbol
1035 redef meth name
do return once
"<=".to_symbol
1038 redef meth name
do return once
">".to_symbol
1041 redef meth name
do return once
">=".to_symbol
1043 redef class APlusExpr
1044 redef meth name
do return once
"+".to_symbol
1046 redef class AMinusExpr
1047 redef meth name
do return once
"-".to_symbol
1049 redef class AStarshipExpr
1050 redef meth name
do return once
"<=>".to_symbol
1052 redef class AStarExpr
1053 redef meth name
do return once
"*".to_symbol
1055 redef class ASlashExpr
1056 redef meth name
do return once
"/".to_symbol
1058 redef class APercentExpr
1059 redef meth name
do return once
"%".to_symbol
1062 redef class AUminusExpr
1063 redef meth name
do return once
"unary -".to_symbol
1064 redef meth raw_arguments
do return null
1067 redef class ACallFormExpr
1068 redef meth after_typing
(v
)
1070 if n_expr
.is_implicit_self
then
1071 var name
= n_id
.to_symbol
1072 var variable
= v
.variable_ctx
[name
]
1073 if variable
!= null then
1074 if not n_args
.is_empty
then
1075 v
.error
(self, "Error: {name} is variable, not a function.")
1077 var vform
= variable_create
(variable
)
1078 vform
.variable
= variable
1080 vform
.after_typing
(v
)
1087 # Create a variable acces corresponding to the call form
1088 meth variable_create
(variable
: Variable): AVarFormExpr is abstract
1091 redef class ACallExpr
1092 redef meth variable_create
(variable
)
1094 return new AVarExpr.init_avarexpr
(n_id
)
1097 redef meth name
do return n_id
.to_symbol
1098 redef meth raw_arguments
do return n_args
.to_a
1101 redef class ACallAssignExpr
1102 redef meth variable_create
(variable
)
1104 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1107 redef meth name
do return (n_id
.text
+ "=").to_symbol
1108 redef meth raw_arguments
do
1109 var res
= n_args
.to_a
1115 redef class ACallReassignExpr
1116 special ASendReassignExpr
1117 redef meth variable_create
(variable
)
1119 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1122 redef meth name
do return n_id
.to_symbol
1123 redef meth raw_arguments
do return n_args
.to_a
1126 redef class ABraExpr
1127 redef meth name
do return once
"[]".to_symbol
1128 redef meth raw_arguments
do return n_args
.to_a
1131 redef class ABraAssignExpr
1132 redef meth name
do return once
"[]=".to_symbol
1133 redef meth raw_arguments
do
1134 var res
= n_args
.to_a
1140 redef class ABraReassignExpr
1141 special ASendReassignExpr
1142 redef meth name
do return once
"[]".to_symbol
1143 redef meth raw_arguments
do return n_args
.to_a
1146 redef class AInitExpr
1147 redef meth name
do return once
"init".to_symbol
1148 redef meth raw_arguments
do return n_args
.to_a
1151 redef class AIsaExpr
1152 redef meth after_typing
(v
)
1154 var variable
= n_expr
.its_variable
1155 if variable
!= null then
1156 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(variable
, n_type
.stype
)
1158 _stype
= v
.type_bool
1162 redef class AAsCastExpr
1163 redef meth after_typing
(v
)
1165 v
.check_expr
(n_expr
)
1166 _stype
= n_type
.stype
1170 redef class AProxyExpr
1171 redef meth after_typing
(v
)
1173 _stype
= n_expr
.stype