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 # Current closure 'return' static type (if any)
54 readable writable attr _closure_stype
: MMType
56 # List of explicit invocation of constructors of super-classes
57 readable writable attr _explicit_super_init_calls
: Array[MMMethod]
59 # Is a other constructor of the same class invoked
60 readable writable attr _explicit_other_init_call
: Bool
62 init(tc
, module) do super
64 private meth get_default_constructor_for
(n
: PNode, c
: MMLocalClass, prop
: MMSrcMethod): MMMethod
67 #var prop = v.local_property
68 #assert prop isa MMMethod
69 var candidates
= new Array[MMMethod]
70 var false_candidates
= new Array[MMMethod]
71 var parity
= prop
.signature
.arity
72 for g
in c
.global_properties
do
73 if not g
.is_init_for
(c
) then continue
75 var gps
= gp
.signature_for
(c
.get_type
)
76 assert gp
isa MMSrcMethod
77 var garity
= gps
.arity
78 if prop
!= null and gp
.name
== prop
.name
then
79 if garity
== 0 or (parity
== garity
and prop
.signature
< gps
) then
82 false_candidates
.add
(gp
)
84 else if garity
== 0 and gp
.name
== once
("init".to_symbol
) then
86 false_candidates
.add
(gp
)
88 false_candidates
.add
(gp
)
91 if candidates
.length
== 1 then
92 return candidates
.first
93 else if candidates
.length
> 0 then
94 var a
= new Array[String]
95 for p
in candidates
do
96 a
.add
("{p.full_name}{p.signature}")
98 v
.error
(n
, "Error: Conflicting default constructor to call for {c}: {a.join(", ")}.")
100 else if false_candidates
.length
> 0 then
101 var a
= new Array[String]
102 for p
in false_candidates
do
103 a
.add
("{p.full_name}{p.signature}")
105 v
.error
(n
, "Error: there is no available compatible constrctor in {c}. Discarded candidates are {a.join(", ")}.")
108 v
.error
(n
, "Error: there is no available compatible constrctor in {c}.")
114 # Associate symbols to variable and variables to type
116 private class VariableContext
117 # Look for the variable from its name
118 # Return null if nothing found
119 meth
[](s
: Symbol): Variable
121 if _dico
.has_key
(s
) then
128 # Register a new variable with its name
129 meth add
(v
: Variable)
135 # The effective static type of a given variable
136 # May be different from the declaration static type
137 meth stype
(v
: Variable): MMType
142 # Variables by name (in the current context only)
143 attr _dico
: Map[Symbol, Variable]
145 # Build a new VariableContext
146 meth sub
: SubVariableContext
148 return new SubVariableContext.with_prev
(self, null, null)
151 # Build a nested VariableContext with new variable information
152 meth sub_with
(v
: Variable, t
: MMType): SubVariableContext
154 return new SubVariableContext.with_prev
(self, v
, t
)
159 _dico
= new HashMap[Symbol, Variable]
163 private class SubVariableContext
164 special VariableContext
165 readable attr _prev
: VariableContext
166 attr _variable
: Variable
167 attr _var_type
: MMType
171 if _dico
.has_key
(s
) then
180 if _variable
== v
then
186 init with_prev
(p
: VariableContext, v
: Variable, t
: MMType)
196 ###############################################################################
199 private meth accept_typing
(v
: TypingVisitor)
201 accept_abs_syntax_visitor
(v
)
204 private meth after_typing
(v
: TypingVisitor) do end
207 redef class PClassdef
208 redef meth accept_typing
(v
)
210 v
.self_var
= new ParamVariable("self".to_symbol
, self)
211 v
.self_var
.stype
= local_class
.get_type
216 redef class AAttrPropdef
217 redef meth accept_typing
(v
)
220 if n_expr
!= null then
221 v
.check_conform_expr
(n_expr
, prop
.signature
.return_type
)
226 redef class AMethPropdef
227 redef readable attr _self_var
: ParamVariable
228 redef meth accept_typing
(v
)
230 v
.variable_ctx
= new VariableContext
231 _self_var
= v
.self_var
236 redef class AConcreteInitPropdef
237 readable attr _super_init_calls
: Array[MMMethod] = new Array[MMMethod]
238 readable attr _explicit_super_init_calls
: Array[MMMethod] = new Array[MMMethod]
239 redef meth accept_typing
(v
)
241 v
.top_block
= n_block
242 v
.explicit_super_init_calls
= explicit_super_init_calls
243 v
.explicit_other_init_call
= false
245 if v
.explicit_other_init_call
or method
.global
.intro
!= method
then
249 var l
= explicit_super_init_calls
.length
250 var cur_m
: MMMethod = null
251 var cur_c
: MMLocalClass = null
253 cur_m
= explicit_super_init_calls
[i
]
254 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
257 while j
< v
.local_class
.cshe
.direct_greaters
.length
do
258 var c
= v
.local_class
.cshe
.direct_greaters
[j
]
259 if c
.global
.is_interface
or c
.global
.is_universal
or c
.global
.is_mixin
then
261 else if cur_c
!= null and (c
.cshe
<= cur_c
or cur_c
.global
.is_mixin
) then
262 if c
== cur_c
then j
+= 1
263 super_init_calls
.add
(cur_m
)
266 cur_m
= explicit_super_init_calls
[i
]
267 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
273 var p
= v
.get_default_constructor_for
(self, c
, method
)
275 super_init_calls
.add
(p
)
285 redef meth after_typing
(v
)
287 # TODO: why the test?
288 if v
.variable_ctx
!= null then
289 v
.variable_ctx
.add
(variable
)
294 redef class AClosureDecl
295 redef meth after_typing
(v
)
297 v
.variable_ctx
.add
(variable
)
302 readable attr _stype
: MMType
303 redef meth after_typing
(v
)
305 _stype
= get_stype
(v
)
310 redef readable attr _stype
: MMType
312 # Is the expression the implicit receiver
313 meth is_implicit_self
: Bool do return false
315 # Is the expression the current receiver (implicit or explicit)
316 meth is_self
: Bool do return false
318 # The variable accessed is any
319 meth its_variable
: Variable do return null
321 # The variable type information if current boolean expression is true
322 readable private attr _if_true_variable_ctx
: VariableContext
325 redef class AVardeclExpr
326 redef meth after_typing
(v
)
328 var va
= new VarVariable(n_id
.to_symbol
, self)
330 v
.variable_ctx
.add
(va
)
332 if n_type
!= null then
333 va
.stype
= n_type
.stype
334 if n_expr
!= null then
335 v
.check_conform_expr
(n_expr
, va
.stype
)
339 va
.stype
= n_expr
.stype
344 redef class ABlockExpr
345 redef meth accept_typing
(v
)
347 var old_var_ctx
= v
.variable_ctx
348 v
.variable_ctx
= v
.variable_ctx
.sub
352 v
.variable_ctx
= old_var_ctx
356 redef class AReturnExpr
357 redef meth after_typing
(v
)
359 var t
= v
.local_property
.signature
.return_type
360 if n_expr
== null and t
!= null then
361 v
.error
(self, "Error: Return without value in a function.")
362 else if n_expr
!= null and t
== null then
363 v
.error
(self, "Error: Return with value in a procedure.")
364 else if n_expr
!= null and t
!= null then
365 v
.check_conform_expr
(n_expr
, t
)
370 redef class AContinueExpr
371 redef meth after_typing
(v
)
373 var t
= v
.closure_stype
374 if n_expr
== null and t
!= null then
375 v
.error
(self, "Error: continue with a value required in this bloc.")
376 else if n_expr
!= null and t
== null then
377 v
.error
(self, "Error: continue without value required in this bloc.")
378 else if n_expr
!= null and t
!= null then
379 v
.check_conform_expr
(n_expr
, t
)
385 redef meth accept_typing
(v
)
387 var old_var_ctx
= v
.variable_ctx
389 v
.check_conform_expr
(n_expr
, v
.type_bool
)
391 if n_expr
.if_true_variable_ctx
!= null then
392 v
.variable_ctx
= n_expr
.if_true_variable_ctx
396 # Restore variable ctx
397 v
.variable_ctx
= old_var_ctx
399 if n_else
!= null then
401 v
.variable_ctx
= old_var_ctx
406 redef class AWhileExpr
407 redef meth after_typing
(v
)
409 v
.check_conform_expr
(n_expr
, v
.type_bool
)
414 redef meth after_typing
(v
)
416 # pop context created in AForVardeclExpr
417 var varctx
= v
.variable_ctx
418 assert varctx
isa SubVariableContext
419 v
.variable_ctx
= varctx
.prev
423 redef class AForVardeclExpr
424 redef meth after_typing
(v
)
426 v
.variable_ctx
= v
.variable_ctx
.sub
427 var va
= new AutoVariable(n_id
.to_symbol
, self)
429 v
.variable_ctx
.add
(va
)
431 var expr_type
= n_expr
.stype
432 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then
435 var prop
= expr_type
.local_class
.select_method
(once
("iterator".to_symbol
))
437 v
.error
(self, "Error: Collection MUST have an iterate method")
440 var iter_type
= prop
.signature_for
(expr_type
).return_type
441 var prop2
= iter_type
.local_class
.select_method
(once
("item".to_symbol
))
442 if prop2
== null then
443 v
.error
(self, "Error: {iter_type} MUST have an item method")
446 var t
= prop2
.signature_for
(iter_type
).return_type
447 if not n_expr
.is_self
then t
= t
.not_for_self
452 redef class AAssertExpr
453 redef meth after_typing
(v
)
455 v
.check_conform_expr
(n_expr
, v
.type_bool
)
456 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
461 redef meth its_variable
do return variable
463 redef meth after_typing
(v
)
465 _stype
= v
.variable_ctx
.stype
(variable
)
469 redef class AVarAssignExpr
470 redef meth after_typing
(v
)
472 var t
= v
.variable_ctx
.stype
(variable
)
473 v
.check_conform_expr
(n_value
, t
)
477 redef class AReassignFormExpr
478 # Compute and check method used through the reassigment operator
479 private meth do_lvalue_typing
(v
: TypingVisitor, type_lvalue
: MMType)
481 if type_lvalue
== null then
484 var name
= n_assign_op
.method_name
485 var prop
= type_lvalue
.local_class
.select_method
(name
)
487 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
490 prop
.global
.check_visibility
(v
, self, v
.module, false)
491 var psig
= prop
.signature_for
(type_lvalue
)
492 _assign_method
= prop
493 v
.check_conform_expr
(n_value
, psig
[0].not_for_self
)
494 v
.check_conform
(self, psig
.return_type
.not_for_self
, n_value
.stype
)
497 # Method used through the reassigment operator (once computed)
498 readable attr _assign_method
: MMMethod
501 redef class AVarReassignExpr
502 redef meth after_typing
(v
)
504 var t
= v
.variable_ctx
.stype
(variable
)
505 do_lvalue_typing
(v
, t
)
509 redef class PAssignOp
510 meth method_name
: Symbol is abstract
512 redef class APlusAssignOp
513 redef meth method_name
do return once
"+".to_symbol
515 redef class AMinusAssignOp
516 redef meth method_name
do return once
"-".to_symbol
519 redef class ASelfExpr
520 redef meth its_variable
do return variable
522 redef meth after_typing
(v
)
524 variable
= v
.self_var
525 _stype
= v
.variable_ctx
.stype
(variable
)
528 redef meth is_self
do return true
531 redef class AImplicitSelfExpr
532 redef meth is_implicit_self
do return true
535 redef class AIfexprExpr
536 redef meth accept_typing
(v
)
538 var old_var_ctx
= v
.variable_ctx
541 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
543 v
.variable_ctx
= old_var_ctx
546 v
.check_conform_expr
(n_expr
, v
.type_bool
)
548 if not v
.check_expr
(n_then
) or not v
.check_expr
(n_else
) then return
551 var te
= n_else
.stype
554 else if not te
< t
then
555 v
.error
(self, "Type error: {te} is not a subtype of {t}.")
563 redef class ABoolExpr
564 redef meth after_typing
(v
)
571 redef meth after_typing
(v
)
573 v
.check_conform_expr
(n_expr
, v
.type_bool
)
574 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
580 redef meth accept_typing
(v
)
582 var old_var_ctx
= v
.variable_ctx
585 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
588 if n_expr2
.if_true_variable_ctx
!= null then
589 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
591 _if_true_variable_ctx
= v
.variable_ctx
594 v
.variable_ctx
= old_var_ctx
596 v
.check_conform_expr
(n_expr
, v
.type_bool
)
597 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
603 redef meth after_typing
(v
)
605 v
.check_conform_expr
(n_expr
, v
.type_bool
)
611 redef meth after_typing
(v
)
618 redef class AFloatExpr
619 redef meth after_typing
(v
)
621 _stype
= v
.type_float
625 redef class ACharExpr
626 redef meth after_typing
(v
)
632 redef class AStringFormExpr
633 redef meth after_typing
(v
)
635 _stype
= v
.type_string
639 redef class ASuperstringExpr
640 redef meth after_typing
(v
)
642 _stype
= v
.type_string
646 redef class ANullExpr
647 redef meth after_typing
(v
)
653 redef class AArrayExpr
654 private meth stype
=(t
: MMType) do _stype
= t
656 redef meth after_typing
(v
)
658 var stype
: MMType = null
661 if stype
== null or (ntype
!= null and stype
< ntype
) then
666 v
.check_conform_expr
(n
, stype
)
668 _stype
= v
.type_array
(stype
)
672 redef class ARangeExpr
673 redef meth after_typing
(v
)
675 var ntype
= n_expr
.stype
676 var ntype2
= n_expr2
.stype
677 if ntype
== null or ntype
== null then
680 if ntype
< ntype2
then
682 else if not ntype2
< ntype
then
683 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
686 var dtype
= v
.type_discrete
687 v
.check_conform_expr
(n_expr
, dtype
)
688 v
.check_conform_expr
(n_expr2
, dtype
)
689 _stype
= v
.type_range
(ntype
)
693 redef class ASuperExpr
694 special ASuperInitCall
695 # readable attr _prop: MMSrcMethod
696 readable attr _init_in_superclass
: MMMethod
697 redef meth after_typing
(v
)
699 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
700 if not precs
.is_empty
then
701 v
.local_property
.need_super
= true
702 else if v
.local_property
.global
.is_init
then
703 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
704 for p
in base_precs
do
705 if not p
.global
.is_init
then
706 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
708 precs
.add
(v
.local_class
[p
.global
])
711 if precs
.is_empty
then
712 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
714 else if precs
.length
> 1 then
715 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
718 var p
= base_precs
.first
719 assert p
isa MMMethod
720 _init_in_superclass
= p
721 register_super_init_call
(v
, p
)
722 if n_args
.length
> 0 then
723 var signature
= get_signature
(v
, v
.self_var
.stype
, p
, true)
724 _arguments
= process_signature
(v
, signature
, p
.name
, n_args
.to_a
)
727 v
.error
(self, "Error: No super method to call for {v.local_property}.")
731 if precs
.first
.signature_for
(v
.self_var
.stype
).return_type
!= null then
732 var stypes
= new Array[MMType]
733 var stype
: MMType = null
735 assert prop
isa MMMethod
736 var t
= prop
.signature_for
(v
.self_var
.stype
).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
738 if stype
== null or stype
< t
then
743 v
.check_conform
(self, t
, stype
)
747 var p
= v
.local_property
748 assert p
isa MMSrcMethod
753 redef class AAttrFormExpr
755 readable attr _prop
: MMAttribute
757 # Attribute type of the acceded attribute
758 readable attr _attr_type
: MMType
760 # Compute the attribute accessed
761 private meth do_typing
(v
: TypingVisitor)
763 if not v
.check_expr
(n_expr
) then return
764 var type_recv
= n_expr
.stype
765 var name
= n_id
.to_symbol
766 var prop
= type_recv
.local_class
.select_attribute
(name
)
768 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
770 else if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
771 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
774 var at
= prop
.signature_for
(type_recv
).return_type
775 if not n_expr
.is_self
then at
= at
.not_for_self
780 redef class AAttrExpr
781 redef meth after_typing
(v
)
791 redef class AAttrAssignExpr
792 redef meth after_typing
(v
)
798 v
.check_conform_expr
(n_value
, attr_type
)
802 redef class AAttrReassignExpr
803 redef meth after_typing
(v
)
809 do_lvalue_typing
(v
, attr_type
)
815 # The signature of the called property
816 readable attr _prop_signature
: MMSignature
818 # Compute the called global property
819 private meth do_typing
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, recv_is_self
: Bool, name
: Symbol, raw_args
: Array[PExpr])
821 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
822 if prop
== null then return
823 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
824 if sig
== null then return
825 var args
= process_signature
(v
, sig
, prop
.name
, raw_args
)
826 if args
== null then return
828 _prop_signature
= sig
832 private meth get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): MMMethod
834 if type_recv
== null then return null
835 var prop
= type_recv
.local_class
.select_method
(name
)
836 if prop
== null and v
.local_property
.global
.is_init
then
837 var props
= type_recv
.local_class
.super_methods_named
(name
)
838 if props
.length
> 1 then
839 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
841 else if props
.length
== 1 then
842 var p
= type_recv
.local_class
[props
.first
.global
]
843 assert p
isa MMMethod
849 if is_implicit_self
then
850 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
852 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
859 private meth get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
861 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
862 var psig
= prop
.signature_for
(type_recv
)
863 if not recv_is_self
then psig
= psig
.not_for_self
867 # Check the conformity of a set of arguments `raw_args' to a signature.
868 private meth process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: Array[PExpr]): Array[PExpr]
870 var par_vararg
= psig
.vararg_rank
871 var par_arity
= psig
.arity
873 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
874 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
875 v
.error
(self, "Error: '{name}' arity missmatch.")
879 var args
= new Array[PExpr]
880 for par_idx
in [0..par_arity
[ do
882 var par_type
= psig
[par_idx
]
883 if par_idx
== par_vararg
then
884 var star
= new Array[PExpr]
885 for i
in [0..(raw_arity-par_arity
)] do
886 a
= raw_args
[arg_idx
]
887 v
.check_conform_expr
(a
, par_type
)
889 arg_idx
= arg_idx
+ 1
891 var aa
= new AArrayExpr.init_aarrayexpr
(star
)
892 aa
.stype
= v
.type_array
(par_type
)
895 a
= raw_args
[arg_idx
]
896 v
.check_conform_expr
(a
, par_type
)
897 arg_idx
= arg_idx
+ 1
904 # The invoked method (once computed)
905 readable attr _prop
: MMMethod
907 # The real arguments used (after star transformation) (once computed)
908 readable attr _arguments
: Array[PExpr]
911 # A possible call of constructor in a super class
912 # Could be an explicit call or with the 'super' keyword
915 private meth register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
917 if parent
!= v
.top_block
and self != v
.top_block
then
918 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
920 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
921 var prev_class
: MMLocalClass = null
922 if not v
.explicit_super_init_calls
.is_empty
then
923 prev_class
= v
.explicit_super_init_calls
.last
.global
.intro
.local_class
925 var order
= v
.local_class
.cshe
.reverse_linear_extension
926 if cla
== v
.local_class
then
927 v
.explicit_other_init_call
= true
928 else if not order
.has
(cla
) then
929 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
930 else if cla
== prev_class
then
931 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
933 var last_is_found
= prev_class
== null
935 if c
== prev_class
then
937 else if c
== cla
then
938 if not last_is_found
then
939 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
941 v
.explicit_super_init_calls
.add
(property
)
952 redef meth after_typing
(v
)
955 if t
== null then return
956 if t
.local_class
.global
.is_abstract
then
957 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
962 name
= once
"init".to_symbol
964 name
= n_id
.to_symbol
967 do_typing
(v
, t
, false, false, name
, n_args
.to_a
)
968 if prop
== null then return
970 if not prop
.global
.is_init
then
971 v
.error
(self, "Error: {prop} is not a constructor.")
978 redef class ASendExpr
979 special ASuperInitCall
980 # Name of the invoked property
981 meth name
: Symbol is abstract
983 # Raw arguments used (withour star transformation)
984 meth raw_arguments
: Array[PExpr] is abstract
986 # Closure definitions
987 meth closure_defs
: Array[PClosureDef] do return null
989 redef meth after_typing
(v
)
994 private meth do_all_typing
(v
: TypingVisitor)
996 if not v
.check_expr
(n_expr
) then return
997 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
)
998 if prop
== null then return
1000 var cd
= closure_defs
1001 var cs
= _prop_signature
.closures
# Closure signatures
1003 if cs
.length
== 0 then
1004 v
.error
(self, "Error: property {prop} does not require blocs.")
1005 else if cs
.length
!= cd
.length
then
1006 v
.error
(self, "Error: property {prop} requires {cs.length} blocs, {cd.length} found.")
1008 for i
in [0..cs
.length
[ do
1009 cd
[i
].accept_typing2
(v
, cs
[i
])
1012 else if cs
.length
!= 0 then
1013 v
.error
(self, "Error: property {prop} requires {cs.length} blocs.")
1016 if prop
.global
.is_init
then
1017 if not v
.local_property
.global
.is_init
then
1018 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1019 else if not n_expr
.is_self
then
1020 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1022 register_super_init_call
(v
, prop
)
1025 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1026 if t
!= null and not n_expr
.is_self
then t
= t
.not_for_self
1031 class ASendReassignExpr
1033 special AReassignFormExpr
1034 readable attr _read_prop
: MMMethod
1035 redef meth do_all_typing
(v
)
1037 if not v
.check_expr
(n_expr
) then return
1038 var raw_args
= raw_arguments
1039 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
)
1040 if prop
== null then return
1041 if prop
.global
.is_init
then
1042 if not v
.local_property
.global
.is_init
then
1043 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1044 else if not n_expr
.is_self
then
1045 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1048 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1049 if not n_expr
.is_self
then t
= t
.not_for_self
1051 do_lvalue_typing
(v
, t
)
1054 var old_args
= arguments
1055 raw_args
.add
(n_value
)
1057 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
)
1058 if prop
== null then return
1059 if prop
.global
.is_init
then
1060 if not v
.local_property
.global
.is_init
then
1061 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1062 else if not n_expr
.is_self
then
1063 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1067 _arguments
= old_args
# FIXME: What if star parameters do not match betwen the two methods?
1071 redef class ABinopExpr
1072 redef meth raw_arguments
do return [n_expr2
]
1075 redef meth name
do return once
"==".to_symbol
1078 redef meth name
do return once
"!=".to_symbol
1081 redef meth name
do return once
"<".to_symbol
1084 redef meth name
do return once
"<=".to_symbol
1087 redef meth name
do return once
">".to_symbol
1090 redef meth name
do return once
">=".to_symbol
1092 redef class APlusExpr
1093 redef meth name
do return once
"+".to_symbol
1095 redef class AMinusExpr
1096 redef meth name
do return once
"-".to_symbol
1098 redef class AStarshipExpr
1099 redef meth name
do return once
"<=>".to_symbol
1101 redef class AStarExpr
1102 redef meth name
do return once
"*".to_symbol
1104 redef class ASlashExpr
1105 redef meth name
do return once
"/".to_symbol
1107 redef class APercentExpr
1108 redef meth name
do return once
"%".to_symbol
1111 redef class AUminusExpr
1112 redef meth name
do return once
"unary -".to_symbol
1113 redef meth raw_arguments
do return null
1116 redef class ACallFormExpr
1117 redef meth after_typing
(v
)
1119 if n_expr
!= null and n_expr
.is_implicit_self
then
1120 var name
= n_id
.to_symbol
1121 var variable
= v
.variable_ctx
[name
]
1122 if variable
!= null then
1123 if variable
isa ClosureVariable then
1124 var n
= new AClosureCallExpr(n_id
, n_args
, n_closure_defs
)
1126 n
.variable
= variable
1130 if not n_args
.is_empty
then
1131 v
.error
(self, "Error: {name} is variable, not a function.")
1133 var vform
= variable_create
(variable
)
1134 vform
.variable
= variable
1136 vform
.after_typing
(v
)
1144 redef meth closure_defs
1146 if n_closure_defs
== null or n_closure_defs
.is_empty
then
1149 return n_closure_defs
.to_a
1153 # Create a variable acces corresponding to the call form
1154 meth variable_create
(variable
: Variable): AVarFormExpr is abstract
1157 redef class ACallExpr
1158 redef meth variable_create
(variable
)
1160 return new AVarExpr.init_avarexpr
(n_id
)
1163 redef meth name
do return n_id
.to_symbol
1164 redef meth raw_arguments
do return n_args
.to_a
1167 redef class ACallAssignExpr
1168 redef meth variable_create
(variable
)
1170 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1173 redef meth name
do return (n_id
.text
+ "=").to_symbol
1174 redef meth raw_arguments
do
1175 var res
= n_args
.to_a
1181 redef class ACallReassignExpr
1182 special ASendReassignExpr
1183 redef meth variable_create
(variable
)
1185 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1188 redef meth name
do return n_id
.to_symbol
1189 redef meth raw_arguments
do return n_args
.to_a
1192 redef class ABraExpr
1193 redef meth name
do return once
"[]".to_symbol
1194 redef meth raw_arguments
do return n_args
.to_a
1197 redef class ABraAssignExpr
1198 redef meth name
do return once
"[]=".to_symbol
1199 redef meth raw_arguments
do
1200 var res
= n_args
.to_a
1206 redef class ABraReassignExpr
1207 special ASendReassignExpr
1208 redef meth name
do return once
"[]".to_symbol
1209 redef meth raw_arguments
do return n_args
.to_a
1212 redef class AInitExpr
1213 redef meth name
do return once
"init".to_symbol
1214 redef meth raw_arguments
do return n_args
.to_a
1217 redef class AClosureCallExpr
1218 redef meth after_typing
(v
)
1221 var sig
= va
.signature
1222 var args
= process_signature
(v
, sig
, n_id
.to_symbol
, n_args
.to_a
)
1223 if args
== null then return
1225 _prop_signature
= sig
1227 _stype
= sig
.return_type
1231 redef class PClosureDef
1232 attr _accept_typing2
: Bool
1233 redef meth accept_typing
(v
)
1235 # Typing is deferred, wait accept_typing2(v)
1236 if _accept_typing2
then super
1239 private meth accept_typing2
(v
: TypingVisitor, sig
: MMSignature) is abstract
1242 redef class AClosureDef
1243 redef meth accept_typing2
(v
, sig
)
1245 if sig
.arity
!= n_id
.length
then
1246 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1252 var old_stype
= v
.closure_stype
1253 v
.closure_stype
= sig
.return_type
1255 v
.variable_ctx
= v
.variable_ctx
.sub
1256 variables
= new Array[AutoVariable]
1257 for i
in [0..n_id
.length
[ do
1258 var va
= new AutoVariable(n_id
[i
].to_symbol
, self)
1261 v
.variable_ctx
.add
(va
)
1264 _accept_typing2
= true
1267 v
.closure_stype
= old_stype
1271 redef class AIsaExpr
1272 redef meth after_typing
(v
)
1274 var variable
= n_expr
.its_variable
1275 if variable
!= null then
1276 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(variable
, n_type
.stype
)
1278 _stype
= v
.type_bool
1282 redef class AAsCastExpr
1283 redef meth after_typing
(v
)
1285 v
.check_expr
(n_expr
)
1286 _stype
= n_type
.stype
1290 redef class AProxyExpr
1291 redef meth after_typing
(v
)
1293 _stype
= n_expr
.stype