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 # TODO: why the test?
285 if v
.variable_ctx
!= null then
286 v
.variable_ctx
.add
(variable
)
291 redef class AClosureDecl
292 redef meth after_typing
(v
)
294 v
.variable_ctx
.add
(variable
)
299 readable attr _stype
: MMType
300 redef meth after_typing
(v
)
302 _stype
= get_stype
(v
)
307 redef readable attr _stype
: MMType
309 # Is the expression the implicit receiver
310 meth is_implicit_self
: Bool do return false
312 # Is the expression the current receiver (implicit or explicit)
313 meth is_self
: Bool do return false
315 # The variable accessed is any
316 meth its_variable
: Variable do return null
318 # The variable type information if current boolean expression is true
319 readable private attr _if_true_variable_ctx
: VariableContext
322 redef class AVardeclExpr
323 redef meth after_typing
(v
)
325 var va
= new VarVariable(n_id
.to_symbol
, self)
327 v
.variable_ctx
.add
(va
)
329 if n_type
!= null then
330 va
.stype
= n_type
.stype
331 if n_expr
!= null then
332 v
.check_conform_expr
(n_expr
, va
.stype
)
336 va
.stype
= n_expr
.stype
341 redef class ABlockExpr
342 redef meth accept_typing
(v
)
344 var old_var_ctx
= v
.variable_ctx
345 v
.variable_ctx
= v
.variable_ctx
.sub
349 v
.variable_ctx
= old_var_ctx
353 redef class AReturnExpr
354 redef meth after_typing
(v
)
356 var t
= v
.local_property
.signature
.return_type
357 if n_expr
== null and t
!= null then
358 v
.error
(self, "Error: Return without value in a function.")
359 else if n_expr
!= null and t
== null then
360 v
.error
(self, "Error: Return with value in a procedure.")
361 else if n_expr
!= null and t
!= null then
362 v
.check_conform_expr
(n_expr
, t
)
368 redef meth accept_typing
(v
)
370 var old_var_ctx
= v
.variable_ctx
372 v
.check_conform_expr
(n_expr
, v
.type_bool
)
374 if n_expr
.if_true_variable_ctx
!= null then
375 v
.variable_ctx
= n_expr
.if_true_variable_ctx
379 # Restore variable ctx
380 v
.variable_ctx
= old_var_ctx
382 if n_else
!= null then
384 v
.variable_ctx
= old_var_ctx
389 redef class AWhileExpr
390 redef meth after_typing
(v
)
392 v
.check_conform_expr
(n_expr
, v
.type_bool
)
397 redef meth after_typing
(v
)
399 # pop context created in AForVardeclExpr
400 var varctx
= v
.variable_ctx
401 assert varctx
isa SubVariableContext
402 v
.variable_ctx
= varctx
.prev
406 redef class AForVardeclExpr
407 redef meth after_typing
(v
)
409 v
.variable_ctx
= v
.variable_ctx
.sub
410 var va
= new AutoVariable(n_id
.to_symbol
, self)
412 v
.variable_ctx
.add
(va
)
414 var expr_type
= n_expr
.stype
415 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then
418 var prop
= expr_type
.local_class
.select_method
(once
("iterator".to_symbol
))
420 v
.error
(self, "Error: Collection MUST have an iterate method")
423 var iter_type
= prop
.signature_for
(expr_type
).return_type
424 var prop2
= iter_type
.local_class
.select_method
(once
("item".to_symbol
))
425 if prop2
== null then
426 v
.error
(self, "Error: {iter_type} MUST have an item method")
429 var t
= prop2
.signature_for
(iter_type
).return_type
430 if not n_expr
.is_self
then t
= t
.not_for_self
435 redef class AAssertExpr
436 redef meth after_typing
(v
)
438 v
.check_conform_expr
(n_expr
, v
.type_bool
)
439 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
444 redef meth its_variable
do return variable
446 redef meth after_typing
(v
)
448 _stype
= v
.variable_ctx
.stype
(variable
)
452 redef class AVarAssignExpr
453 redef meth after_typing
(v
)
455 var t
= v
.variable_ctx
.stype
(variable
)
456 v
.check_conform_expr
(n_value
, t
)
460 redef class AReassignFormExpr
461 # Compute and check method used through the reassigment operator
462 private meth do_lvalue_typing
(v
: TypingVisitor, type_lvalue
: MMType)
464 if type_lvalue
== null then
467 var name
= n_assign_op
.method_name
468 var prop
= type_lvalue
.local_class
.select_method
(name
)
470 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
473 prop
.global
.check_visibility
(v
, self, v
.module, false)
474 var psig
= prop
.signature_for
(type_lvalue
)
475 _assign_method
= prop
476 v
.check_conform_expr
(n_value
, psig
[0].not_for_self
)
477 v
.check_conform
(self, psig
.return_type
.not_for_self
, n_value
.stype
)
480 # Method used through the reassigment operator (once computed)
481 readable attr _assign_method
: MMMethod
484 redef class AVarReassignExpr
485 redef meth after_typing
(v
)
487 var t
= v
.variable_ctx
.stype
(variable
)
488 do_lvalue_typing
(v
, t
)
492 redef class PAssignOp
493 meth method_name
: Symbol is abstract
495 redef class APlusAssignOp
496 redef meth method_name
do return once
"+".to_symbol
498 redef class AMinusAssignOp
499 redef meth method_name
do return once
"-".to_symbol
502 redef class ASelfExpr
503 redef meth its_variable
do return variable
505 redef meth after_typing
(v
)
507 variable
= v
.self_var
508 _stype
= v
.variable_ctx
.stype
(variable
)
511 redef meth is_self
do return true
514 redef class AImplicitSelfExpr
515 redef meth is_implicit_self
do return true
518 redef class AIfexprExpr
519 redef meth accept_typing
(v
)
521 var old_var_ctx
= v
.variable_ctx
524 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
526 v
.variable_ctx
= old_var_ctx
529 v
.check_conform_expr
(n_expr
, v
.type_bool
)
531 if not v
.check_expr
(n_then
) or not v
.check_expr
(n_else
) then return
534 var te
= n_else
.stype
537 else if not te
< t
then
538 v
.error
(self, "Type error: {te} is not a subtype of {t}.")
546 redef class ABoolExpr
547 redef meth after_typing
(v
)
554 redef meth after_typing
(v
)
556 v
.check_conform_expr
(n_expr
, v
.type_bool
)
557 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
563 redef meth accept_typing
(v
)
565 var old_var_ctx
= v
.variable_ctx
568 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
571 if n_expr2
.if_true_variable_ctx
!= null then
572 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
574 _if_true_variable_ctx
= v
.variable_ctx
577 v
.variable_ctx
= old_var_ctx
579 v
.check_conform_expr
(n_expr
, v
.type_bool
)
580 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
586 redef meth after_typing
(v
)
588 v
.check_conform_expr
(n_expr
, v
.type_bool
)
594 redef meth after_typing
(v
)
601 redef class AFloatExpr
602 redef meth after_typing
(v
)
604 _stype
= v
.type_float
608 redef class ACharExpr
609 redef meth after_typing
(v
)
615 redef class AStringFormExpr
616 redef meth after_typing
(v
)
618 _stype
= v
.type_string
622 redef class ASuperstringExpr
623 redef meth after_typing
(v
)
625 _stype
= v
.type_string
629 redef class ANullExpr
630 redef meth after_typing
(v
)
636 redef class AArrayExpr
637 private meth stype
=(t
: MMType) do _stype
= t
639 redef meth after_typing
(v
)
641 var stype
: MMType = null
644 if stype
== null or (ntype
!= null and stype
< ntype
) then
649 v
.check_conform_expr
(n
, stype
)
651 _stype
= v
.type_array
(stype
)
655 redef class ARangeExpr
656 redef meth after_typing
(v
)
658 var ntype
= n_expr
.stype
659 var ntype2
= n_expr2
.stype
660 if ntype
== null or ntype
== null then
663 if ntype
< ntype2
then
665 else if not ntype2
< ntype
then
666 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
669 var dtype
= v
.type_discrete
670 v
.check_conform_expr
(n_expr
, dtype
)
671 v
.check_conform_expr
(n_expr2
, dtype
)
672 _stype
= v
.type_range
(ntype
)
676 redef class ASuperExpr
677 special ASuperInitCall
678 # readable attr _prop: MMSrcMethod
679 readable attr _init_in_superclass
: MMMethod
680 redef meth after_typing
(v
)
682 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
683 if not precs
.is_empty
then
684 v
.local_property
.need_super
= true
685 else if v
.local_property
.global
.is_init
then
686 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
687 for p
in base_precs
do
688 if not p
.global
.is_init
then
689 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
691 precs
.add
(v
.local_class
[p
.global
])
694 if precs
.is_empty
then
695 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
697 else if precs
.length
> 1 then
698 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
701 var p
= base_precs
.first
702 assert p
isa MMMethod
703 _init_in_superclass
= p
704 register_super_init_call
(v
, p
)
705 if n_args
.length
> 0 then
706 var signature
= get_signature
(v
, v
.self_var
.stype
, p
, true)
707 _arguments
= process_signature
(v
, signature
, p
.name
, n_args
.to_a
)
710 v
.error
(self, "Error: No super method to call for {v.local_property}.")
714 if precs
.first
.signature_for
(v
.self_var
.stype
).return_type
!= null then
715 var stypes
= new Array[MMType]
716 var stype
: MMType = null
718 assert prop
isa MMMethod
719 var t
= prop
.signature_for
(v
.self_var
.stype
).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
721 if stype
== null or stype
< t
then
726 v
.check_conform
(self, t
, stype
)
730 var p
= v
.local_property
731 assert p
isa MMSrcMethod
736 redef class AAttrFormExpr
738 readable attr _prop
: MMAttribute
740 # Attribute type of the acceded attribute
741 readable attr _attr_type
: MMType
743 # Compute the attribute accessed
744 private meth do_typing
(v
: TypingVisitor)
746 if not v
.check_expr
(n_expr
) then return
747 var type_recv
= n_expr
.stype
748 var name
= n_id
.to_symbol
749 var prop
= type_recv
.local_class
.select_attribute
(name
)
751 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
753 else if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
754 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
757 var at
= prop
.signature_for
(type_recv
).return_type
758 if not n_expr
.is_self
then at
= at
.not_for_self
763 redef class AAttrExpr
764 redef meth after_typing
(v
)
774 redef class AAttrAssignExpr
775 redef meth after_typing
(v
)
781 v
.check_conform_expr
(n_value
, attr_type
)
785 redef class AAttrReassignExpr
786 redef meth after_typing
(v
)
792 do_lvalue_typing
(v
, attr_type
)
798 # The signature of the called property
799 readable attr _prop_signature
: MMSignature
801 # Compute the called global property
802 private meth do_typing
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, recv_is_self
: Bool, name
: Symbol, raw_args
: Array[PExpr])
804 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
805 if prop
== null then return
806 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
807 if sig
== null then return
808 var args
= process_signature
(v
, sig
, prop
.name
, raw_args
)
809 if args
== null then return
811 _prop_signature
= sig
815 private meth get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): MMMethod
817 if type_recv
== null then return null
818 var prop
= type_recv
.local_class
.select_method
(name
)
819 if prop
== null and v
.local_property
.global
.is_init
then
820 var props
= type_recv
.local_class
.super_methods_named
(name
)
821 if props
.length
> 1 then
822 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
824 else if props
.length
== 1 then
825 var p
= type_recv
.local_class
[props
.first
.global
]
826 assert p
isa MMMethod
832 if is_implicit_self
then
833 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
835 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
842 private meth get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
844 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
845 var psig
= prop
.signature_for
(type_recv
)
846 if not recv_is_self
then psig
= psig
.not_for_self
850 # Check the conformity of a set of arguments `raw_args' to a signature.
851 private meth process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: Array[PExpr]): Array[PExpr]
853 var par_vararg
= psig
.vararg_rank
854 var par_arity
= psig
.arity
856 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
857 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
858 v
.error
(self, "Error: '{name}' arity missmatch.")
862 var args
= new Array[PExpr]
863 for par_idx
in [0..par_arity
[ do
865 var par_type
= psig
[par_idx
]
866 if par_idx
== par_vararg
then
867 var star
= new Array[PExpr]
868 for i
in [0..(raw_arity-par_arity
)] do
869 a
= raw_args
[arg_idx
]
870 v
.check_conform_expr
(a
, par_type
)
872 arg_idx
= arg_idx
+ 1
874 var aa
= new AArrayExpr.init_aarrayexpr
(star
)
875 aa
.stype
= v
.type_array
(par_type
)
878 a
= raw_args
[arg_idx
]
879 v
.check_conform_expr
(a
, par_type
)
880 arg_idx
= arg_idx
+ 1
887 # The invoked method (once computed)
888 readable attr _prop
: MMMethod
890 # The real arguments used (after star transformation) (once computed)
891 readable attr _arguments
: Array[PExpr]
894 # A possible call of constructor in a super class
895 # Could be an explicit call or with the 'super' keyword
898 private meth register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
900 if parent
!= v
.top_block
and self != v
.top_block
then
901 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
903 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
904 var prev_class
: MMLocalClass = null
905 if not v
.explicit_super_init_calls
.is_empty
then
906 prev_class
= v
.explicit_super_init_calls
.last
.global
.intro
.local_class
908 var order
= v
.local_class
.cshe
.reverse_linear_extension
909 if cla
== v
.local_class
then
910 v
.explicit_other_init_call
= true
911 else if not order
.has
(cla
) then
912 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
913 else if cla
== prev_class
then
914 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
916 var last_is_found
= prev_class
== null
918 if c
== prev_class
then
920 else if c
== cla
then
921 if not last_is_found
then
922 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
924 v
.explicit_super_init_calls
.add
(property
)
935 redef meth after_typing
(v
)
938 if t
== null then return
939 if t
.local_class
.global
.is_abstract
then
940 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
945 name
= once
"init".to_symbol
947 name
= n_id
.to_symbol
950 do_typing
(v
, t
, false, false, name
, n_args
.to_a
)
951 if prop
== null then return
953 if not prop
.global
.is_init
then
954 v
.error
(self, "Error: {prop} is not a constructor.")
961 redef class ASendExpr
962 special ASuperInitCall
963 # Name of the invoked property
964 meth name
: Symbol is abstract
966 # Raw arguments used (withour star transformation)
967 meth raw_arguments
: Array[PExpr] is abstract
969 # Closure definitions
970 meth closure_defs
: Array[PClosureDef] do return null
972 redef meth after_typing
(v
)
977 private meth do_all_typing
(v
: TypingVisitor)
979 if not v
.check_expr
(n_expr
) then return
980 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
)
981 if prop
== null then return
983 var cd
= closure_defs
984 var cs
= _prop_signature
.closures
# Closure signatures
986 if cs
.length
== 0 then
987 v
.error
(self, "Error: property {prop} does not require blocs.")
988 else if cs
.length
!= cd
.length
then
989 v
.error
(self, "Error: property {prop} requires {cs.length} blocs, {cd.length} found.")
991 for i
in [0..cs
.length
[ do
992 cd
[i
].accept_typing2
(v
, cs
[i
])
995 else if cs
.length
!= 0 then
996 v
.error
(self, "Error: property {prop} requires {cs.length} blocs.")
999 if prop
.global
.is_init
then
1000 if not v
.local_property
.global
.is_init
then
1001 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1002 else if not n_expr
.is_self
then
1003 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1005 register_super_init_call
(v
, prop
)
1008 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1009 if t
!= null and not n_expr
.is_self
then t
= t
.not_for_self
1014 class ASendReassignExpr
1016 special AReassignFormExpr
1017 readable attr _read_prop
: MMMethod
1018 redef meth do_all_typing
(v
)
1020 if not v
.check_expr
(n_expr
) then return
1021 var raw_args
= raw_arguments
1022 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
)
1023 if prop
== null then return
1024 if prop
.global
.is_init
then
1025 if not v
.local_property
.global
.is_init
then
1026 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1027 else if not n_expr
.is_self
then
1028 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1031 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1032 if not n_expr
.is_self
then t
= t
.not_for_self
1034 do_lvalue_typing
(v
, t
)
1037 var old_args
= arguments
1038 raw_args
.add
(n_value
)
1040 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
)
1041 if prop
== null then return
1042 if prop
.global
.is_init
then
1043 if not v
.local_property
.global
.is_init
then
1044 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1045 else if not n_expr
.is_self
then
1046 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1050 _arguments
= old_args
# FIXME: What if star parameters do not match betwen the two methods?
1054 redef class ABinopExpr
1055 redef meth raw_arguments
do return [n_expr2
]
1058 redef meth name
do return once
"==".to_symbol
1061 redef meth name
do return once
"!=".to_symbol
1064 redef meth name
do return once
"<".to_symbol
1067 redef meth name
do return once
"<=".to_symbol
1070 redef meth name
do return once
">".to_symbol
1073 redef meth name
do return once
">=".to_symbol
1075 redef class APlusExpr
1076 redef meth name
do return once
"+".to_symbol
1078 redef class AMinusExpr
1079 redef meth name
do return once
"-".to_symbol
1081 redef class AStarshipExpr
1082 redef meth name
do return once
"<=>".to_symbol
1084 redef class AStarExpr
1085 redef meth name
do return once
"*".to_symbol
1087 redef class ASlashExpr
1088 redef meth name
do return once
"/".to_symbol
1090 redef class APercentExpr
1091 redef meth name
do return once
"%".to_symbol
1094 redef class AUminusExpr
1095 redef meth name
do return once
"unary -".to_symbol
1096 redef meth raw_arguments
do return null
1099 redef class ACallFormExpr
1100 redef meth after_typing
(v
)
1102 if n_expr
!= null and n_expr
.is_implicit_self
then
1103 var name
= n_id
.to_symbol
1104 var variable
= v
.variable_ctx
[name
]
1105 if variable
!= null then
1106 if variable
isa ClosureVariable then
1107 var n
= new AClosureCallExpr(n_id
, n_args
, n_closure_defs
)
1109 n
.variable
= variable
1113 if not n_args
.is_empty
then
1114 v
.error
(self, "Error: {name} is variable, not a function.")
1116 var vform
= variable_create
(variable
)
1117 vform
.variable
= variable
1119 vform
.after_typing
(v
)
1127 redef meth closure_defs
1129 if n_closure_defs
== null or n_closure_defs
.is_empty
then
1132 return n_closure_defs
.to_a
1136 # Create a variable acces corresponding to the call form
1137 meth variable_create
(variable
: Variable): AVarFormExpr is abstract
1140 redef class ACallExpr
1141 redef meth variable_create
(variable
)
1143 return new AVarExpr.init_avarexpr
(n_id
)
1146 redef meth name
do return n_id
.to_symbol
1147 redef meth raw_arguments
do return n_args
.to_a
1150 redef class ACallAssignExpr
1151 redef meth variable_create
(variable
)
1153 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1156 redef meth name
do return (n_id
.text
+ "=").to_symbol
1157 redef meth raw_arguments
do
1158 var res
= n_args
.to_a
1164 redef class ACallReassignExpr
1165 special ASendReassignExpr
1166 redef meth variable_create
(variable
)
1168 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1171 redef meth name
do return n_id
.to_symbol
1172 redef meth raw_arguments
do return n_args
.to_a
1175 redef class ABraExpr
1176 redef meth name
do return once
"[]".to_symbol
1177 redef meth raw_arguments
do return n_args
.to_a
1180 redef class ABraAssignExpr
1181 redef meth name
do return once
"[]=".to_symbol
1182 redef meth raw_arguments
do
1183 var res
= n_args
.to_a
1189 redef class ABraReassignExpr
1190 special ASendReassignExpr
1191 redef meth name
do return once
"[]".to_symbol
1192 redef meth raw_arguments
do return n_args
.to_a
1195 redef class AInitExpr
1196 redef meth name
do return once
"init".to_symbol
1197 redef meth raw_arguments
do return n_args
.to_a
1200 redef class AClosureCallExpr
1201 redef meth after_typing
(v
)
1204 var sig
= va
.signature
1205 var args
= process_signature
(v
, sig
, n_id
.to_symbol
, n_args
.to_a
)
1206 if args
== null then return
1208 _prop_signature
= sig
1210 _stype
= sig
.return_type
1214 redef class PClosureDef
1215 attr _accept_typing2
: Bool
1216 redef meth accept_typing
(v
)
1218 # Typing is deferred, wait accept_typing2(v)
1219 if _accept_typing2
then super
1222 private meth accept_typing2
(v
: TypingVisitor, sig
: MMSignature) is abstract
1225 redef class AClosureDef
1226 redef meth accept_typing2
(v
, sig
)
1228 if sig
.arity
!= n_id
.length
then
1229 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1235 v
.variable_ctx
= v
.variable_ctx
.sub
1236 variables
= new Array[AutoVariable]
1237 for i
in [0..n_id
.length
[ do
1238 var va
= new AutoVariable(n_id
[i
].to_symbol
, self)
1241 v
.variable_ctx
.add
(va
)
1244 _accept_typing2
= true
1249 redef class AIsaExpr
1250 redef meth after_typing
(v
)
1252 var variable
= n_expr
.its_variable
1253 if variable
!= null then
1254 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(variable
, n_type
.stype
)
1256 _stype
= v
.type_bool
1260 redef class AAsCastExpr
1261 redef meth after_typing
(v
)
1263 v
.check_expr
(n_expr
)
1264 _stype
= n_type
.stype
1268 redef class AProxyExpr
1269 redef meth after_typing
(v
)
1271 _stype
= n_expr
.stype