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 # Current closure method return type (for break) (if any)
57 readable writable attr _closure_break_stype
: MMType = null
59 # Current closure break expressions (if any)
60 readable writable attr _break_list
: Array[PExpr]
62 # List of explicit invocation of constructors of super-classes
63 readable writable attr _explicit_super_init_calls
: Array[MMMethod]
65 # Is a other constructor of the same class invoked
66 readable writable attr _explicit_other_init_call
: Bool
68 init(tc
, module) do super
70 private meth get_default_constructor_for
(n
: PNode, c
: MMLocalClass, prop
: MMSrcMethod): MMMethod
73 #var prop = v.local_property
74 #assert prop isa MMMethod
75 var candidates
= new Array[MMMethod]
76 var false_candidates
= new Array[MMMethod]
77 var parity
= prop
.signature
.arity
78 for g
in c
.global_properties
do
79 if not g
.is_init_for
(c
) then continue
81 var gps
= gp
.signature_for
(c
.get_type
)
82 assert gp
isa MMSrcMethod
83 var garity
= gps
.arity
84 if prop
!= null and gp
.name
== prop
.name
then
85 if garity
== 0 or (parity
== garity
and prop
.signature
< gps
) then
88 false_candidates
.add
(gp
)
90 else if garity
== 0 and gp
.name
== once
("init".to_symbol
) then
92 false_candidates
.add
(gp
)
94 false_candidates
.add
(gp
)
97 if candidates
.length
== 1 then
98 return candidates
.first
99 else if candidates
.length
> 0 then
100 var a
= new Array[String]
101 for p
in candidates
do
102 a
.add
("{p.full_name}{p.signature}")
104 v
.error
(n
, "Error: Conflicting default constructor to call for {c}: {a.join(", ")}.")
106 else if false_candidates
.length
> 0 then
107 var a
= new Array[String]
108 for p
in false_candidates
do
109 a
.add
("{p.full_name}{p.signature}")
111 v
.error
(n
, "Error: there is no available compatible constrctor in {c}. Discarded candidates are {a.join(", ")}.")
114 v
.error
(n
, "Error: there is no available compatible constrctor in {c}.")
120 # Associate symbols to variable and variables to type
122 private class VariableContext
123 # Look for the variable from its name
124 # Return null if nothing found
125 meth
[](s
: Symbol): Variable
127 if _dico
.has_key
(s
) then
134 # Register a new variable with its name
135 meth add
(v
: Variable)
141 # The effective static type of a given variable
142 # May be different from the declaration static type
143 meth stype
(v
: Variable): MMType
148 # Variables by name (in the current context only)
149 attr _dico
: Map[Symbol, Variable]
151 # Build a new VariableContext
152 meth sub
: SubVariableContext
154 return new SubVariableContext.with_prev
(self, null, null)
157 # Build a nested VariableContext with new variable information
158 meth sub_with
(v
: Variable, t
: MMType): SubVariableContext
160 return new SubVariableContext.with_prev
(self, v
, t
)
165 _dico
= new HashMap[Symbol, Variable]
169 private class SubVariableContext
170 special VariableContext
171 readable attr _prev
: VariableContext
172 attr _variable
: Variable
173 attr _var_type
: MMType
177 if _dico
.has_key
(s
) then
186 if _variable
== v
then
192 init with_prev
(p
: VariableContext, v
: Variable, t
: MMType)
202 ###############################################################################
205 private meth accept_typing
(v
: TypingVisitor)
207 accept_abs_syntax_visitor
(v
)
210 private meth after_typing
(v
: TypingVisitor) do end
213 redef class PClassdef
214 redef meth accept_typing
(v
)
216 v
.self_var
= new ParamVariable("self".to_symbol
, self)
217 v
.self_var
.stype
= local_class
.get_type
222 redef class AAttrPropdef
223 redef meth accept_typing
(v
)
226 if n_expr
!= null then
227 v
.check_conform_expr
(n_expr
, prop
.signature
.return_type
)
232 redef class AMethPropdef
233 redef readable attr _self_var
: ParamVariable
234 redef meth accept_typing
(v
)
236 v
.variable_ctx
= new VariableContext
237 _self_var
= v
.self_var
242 redef class AConcreteInitPropdef
243 readable attr _super_init_calls
: Array[MMMethod] = new Array[MMMethod]
244 readable attr _explicit_super_init_calls
: Array[MMMethod] = new Array[MMMethod]
245 redef meth accept_typing
(v
)
247 v
.top_block
= n_block
248 v
.explicit_super_init_calls
= explicit_super_init_calls
249 v
.explicit_other_init_call
= false
251 if v
.explicit_other_init_call
or method
.global
.intro
!= method
then
255 var l
= explicit_super_init_calls
.length
256 var cur_m
: MMMethod = null
257 var cur_c
: MMLocalClass = null
259 cur_m
= explicit_super_init_calls
[i
]
260 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
263 while j
< v
.local_class
.cshe
.direct_greaters
.length
do
264 var c
= v
.local_class
.cshe
.direct_greaters
[j
]
265 if c
.global
.is_interface
or c
.global
.is_universal
or c
.global
.is_mixin
then
267 else if cur_c
!= null and (c
.cshe
<= cur_c
or cur_c
.global
.is_mixin
) then
268 if c
== cur_c
then j
+= 1
269 super_init_calls
.add
(cur_m
)
272 cur_m
= explicit_super_init_calls
[i
]
273 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
279 var p
= v
.get_default_constructor_for
(self, c
, method
)
281 super_init_calls
.add
(p
)
291 redef meth after_typing
(v
)
293 # TODO: why the test?
294 if v
.variable_ctx
!= null then
295 v
.variable_ctx
.add
(variable
)
300 redef class AClosureDecl
301 redef meth after_typing
(v
)
303 v
.variable_ctx
.add
(variable
)
308 readable attr _stype
: MMType
309 redef meth after_typing
(v
)
311 _stype
= get_stype
(v
)
316 redef readable attr _stype
: MMType
318 # Is the expression the implicit receiver
319 meth is_implicit_self
: Bool do return false
321 # Is the expression the current receiver (implicit or explicit)
322 meth is_self
: Bool do return false
324 # The variable accessed is any
325 meth its_variable
: Variable do return null
327 # The variable type information if current boolean expression is true
328 readable private attr _if_true_variable_ctx
: VariableContext
331 redef class AVardeclExpr
332 redef meth after_typing
(v
)
334 var va
= new VarVariable(n_id
.to_symbol
, self)
336 v
.variable_ctx
.add
(va
)
338 if n_type
!= null then
339 va
.stype
= n_type
.stype
340 if n_expr
!= null then
341 v
.check_conform_expr
(n_expr
, va
.stype
)
345 va
.stype
= n_expr
.stype
350 redef class ABlockExpr
351 redef meth accept_typing
(v
)
353 var old_var_ctx
= v
.variable_ctx
354 v
.variable_ctx
= v
.variable_ctx
.sub
358 v
.variable_ctx
= old_var_ctx
362 redef class AReturnExpr
363 redef meth after_typing
(v
)
365 var t
= v
.local_property
.signature
.return_type
366 if n_expr
== null and t
!= null then
367 v
.error
(self, "Error: Return without value in a function.")
368 else if n_expr
!= null and t
== null then
369 v
.error
(self, "Error: Return with value in a procedure.")
370 else if n_expr
!= null and t
!= null then
371 v
.check_conform_expr
(n_expr
, t
)
376 redef class AContinueExpr
377 redef meth after_typing
(v
)
379 var t
= v
.closure_stype
380 if n_expr
== null and t
!= null then
381 v
.error
(self, "Error: continue with a value required in this bloc.")
382 else if n_expr
!= null and t
== null then
383 v
.error
(self, "Error: continue without value required in this bloc.")
384 else if n_expr
!= null and t
!= null then
385 v
.check_conform_expr
(n_expr
, t
)
390 redef class ABreakExpr
391 redef meth after_typing
(v
)
393 var t
= v
.closure_break_stype
394 if n_expr
== null and t
!= null then
395 v
.error
(self, "Error: break with a value required in this bloc.")
396 else if n_expr
!= null and t
== null then
397 v
.error
(self, "Error: break without value required in this bloc.")
398 else if n_expr
!= null and t
!= null then
399 # Typing check can only be done later
400 v
.break_list
.add
(n_expr
)
406 redef meth accept_typing
(v
)
408 var old_var_ctx
= v
.variable_ctx
410 v
.check_conform_expr
(n_expr
, v
.type_bool
)
412 if n_expr
.if_true_variable_ctx
!= null then
413 v
.variable_ctx
= n_expr
.if_true_variable_ctx
417 # Restore variable ctx
418 v
.variable_ctx
= old_var_ctx
420 if n_else
!= null then
422 v
.variable_ctx
= old_var_ctx
427 redef class AWhileExpr
428 redef meth after_typing
(v
)
430 v
.check_conform_expr
(n_expr
, v
.type_bool
)
435 redef meth after_typing
(v
)
437 # pop context created in AForVardeclExpr
438 var varctx
= v
.variable_ctx
439 assert varctx
isa SubVariableContext
440 v
.variable_ctx
= varctx
.prev
444 redef class AForVardeclExpr
445 redef meth after_typing
(v
)
447 v
.variable_ctx
= v
.variable_ctx
.sub
448 var va
= new AutoVariable(n_id
.to_symbol
, self)
450 v
.variable_ctx
.add
(va
)
452 var expr_type
= n_expr
.stype
453 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then
456 var prop
= expr_type
.local_class
.select_method
(once
("iterator".to_symbol
))
458 v
.error
(self, "Error: Collection MUST have an iterate method")
461 var iter_type
= prop
.signature_for
(expr_type
).return_type
462 var prop2
= iter_type
.local_class
.select_method
(once
("item".to_symbol
))
463 if prop2
== null then
464 v
.error
(self, "Error: {iter_type} MUST have an item method")
467 var t
= prop2
.signature_for
(iter_type
).return_type
468 if not n_expr
.is_self
then t
= t
.not_for_self
473 redef class AAssertExpr
474 redef meth after_typing
(v
)
476 v
.check_conform_expr
(n_expr
, v
.type_bool
)
477 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
482 redef meth its_variable
do return variable
484 redef meth after_typing
(v
)
486 _stype
= v
.variable_ctx
.stype
(variable
)
490 redef class AVarAssignExpr
491 redef meth after_typing
(v
)
493 var t
= v
.variable_ctx
.stype
(variable
)
494 v
.check_conform_expr
(n_value
, t
)
498 redef class AReassignFormExpr
499 # Compute and check method used through the reassigment operator
500 private meth do_lvalue_typing
(v
: TypingVisitor, type_lvalue
: MMType)
502 if type_lvalue
== null then
505 var name
= n_assign_op
.method_name
506 var prop
= type_lvalue
.local_class
.select_method
(name
)
508 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
511 prop
.global
.check_visibility
(v
, self, v
.module, false)
512 var psig
= prop
.signature_for
(type_lvalue
)
513 _assign_method
= prop
514 v
.check_conform_expr
(n_value
, psig
[0].not_for_self
)
515 v
.check_conform
(self, psig
.return_type
.not_for_self
, n_value
.stype
)
518 # Method used through the reassigment operator (once computed)
519 readable attr _assign_method
: MMMethod
522 redef class AVarReassignExpr
523 redef meth after_typing
(v
)
525 var t
= v
.variable_ctx
.stype
(variable
)
526 do_lvalue_typing
(v
, t
)
530 redef class PAssignOp
531 meth method_name
: Symbol is abstract
533 redef class APlusAssignOp
534 redef meth method_name
do return once
"+".to_symbol
536 redef class AMinusAssignOp
537 redef meth method_name
do return once
"-".to_symbol
540 redef class ASelfExpr
541 redef meth its_variable
do return variable
543 redef meth after_typing
(v
)
545 variable
= v
.self_var
546 _stype
= v
.variable_ctx
.stype
(variable
)
549 redef meth is_self
do return true
552 redef class AImplicitSelfExpr
553 redef meth is_implicit_self
do return true
556 redef class AIfexprExpr
557 redef meth accept_typing
(v
)
559 var old_var_ctx
= v
.variable_ctx
562 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
564 v
.variable_ctx
= old_var_ctx
567 v
.check_conform_expr
(n_expr
, v
.type_bool
)
569 if not v
.check_expr
(n_then
) or not v
.check_expr
(n_else
) then return
572 var te
= n_else
.stype
575 else if not te
< t
then
576 v
.error
(self, "Type error: {te} is not a subtype of {t}.")
584 redef class ABoolExpr
585 redef meth after_typing
(v
)
592 redef meth after_typing
(v
)
594 v
.check_conform_expr
(n_expr
, v
.type_bool
)
595 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
601 redef meth accept_typing
(v
)
603 var old_var_ctx
= v
.variable_ctx
606 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
609 if n_expr2
.if_true_variable_ctx
!= null then
610 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
612 _if_true_variable_ctx
= v
.variable_ctx
615 v
.variable_ctx
= old_var_ctx
617 v
.check_conform_expr
(n_expr
, v
.type_bool
)
618 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
624 redef meth after_typing
(v
)
626 v
.check_conform_expr
(n_expr
, v
.type_bool
)
632 redef meth after_typing
(v
)
639 redef class AFloatExpr
640 redef meth after_typing
(v
)
642 _stype
= v
.type_float
646 redef class ACharExpr
647 redef meth after_typing
(v
)
653 redef class AStringFormExpr
654 redef meth after_typing
(v
)
656 _stype
= v
.type_string
660 redef class ASuperstringExpr
661 redef meth after_typing
(v
)
663 _stype
= v
.type_string
667 redef class ANullExpr
668 redef meth after_typing
(v
)
674 redef class AArrayExpr
675 private meth stype
=(t
: MMType) do _stype
= t
677 redef meth after_typing
(v
)
679 var stype
: MMType = null
682 if stype
== null or (ntype
!= null and stype
< ntype
) then
687 v
.check_conform_expr
(n
, stype
)
689 _stype
= v
.type_array
(stype
)
693 redef class ARangeExpr
694 redef meth after_typing
(v
)
696 var ntype
= n_expr
.stype
697 var ntype2
= n_expr2
.stype
698 if ntype
== null or ntype
== null then
701 if ntype
< ntype2
then
703 else if not ntype2
< ntype
then
704 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
707 var dtype
= v
.type_discrete
708 v
.check_conform_expr
(n_expr
, dtype
)
709 v
.check_conform_expr
(n_expr2
, dtype
)
710 _stype
= v
.type_range
(ntype
)
714 redef class ASuperExpr
715 special ASuperInitCall
716 # readable attr _prop: MMSrcMethod
717 readable attr _init_in_superclass
: MMMethod
718 redef meth after_typing
(v
)
720 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
721 if not precs
.is_empty
then
722 v
.local_property
.need_super
= true
723 else if v
.local_property
.global
.is_init
then
724 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
725 for p
in base_precs
do
726 if not p
.global
.is_init
then
727 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
729 precs
.add
(v
.local_class
[p
.global
])
732 if precs
.is_empty
then
733 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
735 else if precs
.length
> 1 then
736 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
739 var p
= base_precs
.first
740 assert p
isa MMMethod
741 _init_in_superclass
= p
742 register_super_init_call
(v
, p
)
743 if n_args
.length
> 0 then
744 var signature
= get_signature
(v
, v
.self_var
.stype
, p
, true)
745 _arguments
= process_signature
(v
, signature
, p
.name
, n_args
.to_a
)
748 v
.error
(self, "Error: No super method to call for {v.local_property}.")
752 if precs
.first
.signature_for
(v
.self_var
.stype
).return_type
!= null then
753 var stypes
= new Array[MMType]
754 var stype
: MMType = null
756 assert prop
isa MMMethod
757 var t
= prop
.signature_for
(v
.self_var
.stype
).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
759 if stype
== null or stype
< t
then
764 v
.check_conform
(self, t
, stype
)
768 var p
= v
.local_property
769 assert p
isa MMSrcMethod
774 redef class AAttrFormExpr
776 readable attr _prop
: MMAttribute
778 # Attribute type of the acceded attribute
779 readable attr _attr_type
: MMType
781 # Compute the attribute accessed
782 private meth do_typing
(v
: TypingVisitor)
784 if not v
.check_expr
(n_expr
) then return
785 var type_recv
= n_expr
.stype
786 var name
= n_id
.to_symbol
787 var prop
= type_recv
.local_class
.select_attribute
(name
)
789 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
791 else if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
792 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
795 var at
= prop
.signature_for
(type_recv
).return_type
796 if not n_expr
.is_self
then at
= at
.not_for_self
801 redef class AAttrExpr
802 redef meth after_typing
(v
)
812 redef class AAttrAssignExpr
813 redef meth after_typing
(v
)
819 v
.check_conform_expr
(n_value
, attr_type
)
823 redef class AAttrReassignExpr
824 redef meth after_typing
(v
)
830 do_lvalue_typing
(v
, attr_type
)
836 # The signature of the called property
837 readable attr _prop_signature
: MMSignature
839 # Compute the called global property
840 private meth do_typing
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, recv_is_self
: Bool, name
: Symbol, raw_args
: Array[PExpr])
842 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
843 if prop
== null then return
844 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
845 if sig
== null then return
846 var args
= process_signature
(v
, sig
, prop
.name
, raw_args
)
847 if args
== null then return
849 _prop_signature
= sig
853 private meth get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): MMMethod
855 if type_recv
== null then return null
856 var prop
= type_recv
.local_class
.select_method
(name
)
857 if prop
== null and v
.local_property
.global
.is_init
then
858 var props
= type_recv
.local_class
.super_methods_named
(name
)
859 if props
.length
> 1 then
860 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
862 else if props
.length
== 1 then
863 var p
= type_recv
.local_class
[props
.first
.global
]
864 assert p
isa MMMethod
870 if is_implicit_self
then
871 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
873 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
880 private meth get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
882 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
883 var psig
= prop
.signature_for
(type_recv
)
884 if not recv_is_self
then psig
= psig
.not_for_self
888 # Check the conformity of a set of arguments `raw_args' to a signature.
889 private meth process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: Array[PExpr]): Array[PExpr]
891 var par_vararg
= psig
.vararg_rank
892 var par_arity
= psig
.arity
894 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
895 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
896 v
.error
(self, "Error: '{name}' arity missmatch.")
900 var args
= new Array[PExpr]
901 for par_idx
in [0..par_arity
[ do
903 var par_type
= psig
[par_idx
]
904 if par_idx
== par_vararg
then
905 var star
= new Array[PExpr]
906 for i
in [0..(raw_arity-par_arity
)] do
907 a
= raw_args
[arg_idx
]
908 v
.check_conform_expr
(a
, par_type
)
910 arg_idx
= arg_idx
+ 1
912 var aa
= new AArrayExpr.init_aarrayexpr
(star
)
913 aa
.stype
= v
.type_array
(par_type
)
916 a
= raw_args
[arg_idx
]
917 v
.check_conform_expr
(a
, par_type
)
918 arg_idx
= arg_idx
+ 1
925 # The invoked method (once computed)
926 readable attr _prop
: MMMethod
928 # The real arguments used (after star transformation) (once computed)
929 readable attr _arguments
: Array[PExpr]
932 # A possible call of constructor in a super class
933 # Could be an explicit call or with the 'super' keyword
936 private meth register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
938 if parent
!= v
.top_block
and self != v
.top_block
then
939 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
941 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
942 var prev_class
: MMLocalClass = null
943 if not v
.explicit_super_init_calls
.is_empty
then
944 prev_class
= v
.explicit_super_init_calls
.last
.global
.intro
.local_class
946 var order
= v
.local_class
.cshe
.reverse_linear_extension
947 if cla
== v
.local_class
then
948 v
.explicit_other_init_call
= true
949 else if not order
.has
(cla
) then
950 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
951 else if cla
== prev_class
then
952 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
954 var last_is_found
= prev_class
== null
956 if c
== prev_class
then
958 else if c
== cla
then
959 if not last_is_found
then
960 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
962 v
.explicit_super_init_calls
.add
(property
)
973 redef meth after_typing
(v
)
976 if t
== null then return
977 if t
.local_class
.global
.is_abstract
then
978 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
983 name
= once
"init".to_symbol
985 name
= n_id
.to_symbol
988 do_typing
(v
, t
, false, false, name
, n_args
.to_a
)
989 if prop
== null then return
991 if not prop
.global
.is_init
then
992 v
.error
(self, "Error: {prop} is not a constructor.")
999 redef class ASendExpr
1000 special ASuperInitCall
1001 # Name of the invoked property
1002 meth name
: Symbol is abstract
1004 # Raw arguments used (withour star transformation)
1005 meth raw_arguments
: Array[PExpr] is abstract
1007 # Closure definitions
1008 meth closure_defs
: Array[PClosureDef] do return null
1010 redef meth after_typing
(v
)
1015 private meth do_all_typing
(v
: TypingVisitor)
1017 if not v
.check_expr
(n_expr
) then return
1018 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
)
1019 if prop
== null then return
1021 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1022 if t
!= null and not n_expr
.is_self
then t
= t
.not_for_self
1024 var cd
= closure_defs
1025 var cs
= _prop_signature
.closures
# Closure signatures
1027 if cs
.length
== 0 then
1028 v
.error
(self, "Error: property {prop} does not require blocs.")
1029 else if cs
.length
!= cd
.length
then
1030 v
.error
(self, "Error: property {prop} requires {cs.length} blocs, {cd.length} found.")
1032 var old_bbst
= v
.closure_break_stype
1033 var old_bl
= v
.break_list
1034 v
.closure_break_stype
= t
1035 v
.break_list
= new Array[ABreakExpr]
1036 for i
in [0..cs
.length
[ do
1037 cd
[i
].accept_typing2
(v
, cs
[i
])
1039 for n
in v
.break_list
do
1041 if t
== null or (t
!= null and t
< ntype
) then
1045 for n
in v
.break_list
do
1046 v
.check_conform_expr
(n
, t
)
1049 v
.closure_break_stype
= old_bbst
1050 v
.break_list
= old_bl
1052 else if cs
.length
!= 0 then
1053 v
.error
(self, "Error: property {prop} requires {cs.length} blocs.")
1056 if prop
.global
.is_init
then
1057 if not v
.local_property
.global
.is_init
then
1058 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1059 else if not n_expr
.is_self
then
1060 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1062 register_super_init_call
(v
, prop
)
1069 class ASendReassignExpr
1071 special AReassignFormExpr
1072 readable attr _read_prop
: MMMethod
1073 redef meth do_all_typing
(v
)
1075 if not v
.check_expr
(n_expr
) then return
1076 var raw_args
= raw_arguments
1077 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
)
1078 if prop
== null then return
1079 if prop
.global
.is_init
then
1080 if not v
.local_property
.global
.is_init
then
1081 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1082 else if not n_expr
.is_self
then
1083 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1086 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1087 if not n_expr
.is_self
then t
= t
.not_for_self
1089 do_lvalue_typing
(v
, t
)
1092 var old_args
= arguments
1093 raw_args
.add
(n_value
)
1095 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
)
1096 if prop
== null then return
1097 if prop
.global
.is_init
then
1098 if not v
.local_property
.global
.is_init
then
1099 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1100 else if not n_expr
.is_self
then
1101 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1105 _arguments
= old_args
# FIXME: What if star parameters do not match betwen the two methods?
1109 redef class ABinopExpr
1110 redef meth raw_arguments
do return [n_expr2
]
1113 redef meth name
do return once
"==".to_symbol
1116 redef meth name
do return once
"!=".to_symbol
1119 redef meth name
do return once
"<".to_symbol
1122 redef meth name
do return once
"<=".to_symbol
1125 redef meth name
do return once
">".to_symbol
1128 redef meth name
do return once
">=".to_symbol
1130 redef class APlusExpr
1131 redef meth name
do return once
"+".to_symbol
1133 redef class AMinusExpr
1134 redef meth name
do return once
"-".to_symbol
1136 redef class AStarshipExpr
1137 redef meth name
do return once
"<=>".to_symbol
1139 redef class AStarExpr
1140 redef meth name
do return once
"*".to_symbol
1142 redef class ASlashExpr
1143 redef meth name
do return once
"/".to_symbol
1145 redef class APercentExpr
1146 redef meth name
do return once
"%".to_symbol
1149 redef class AUminusExpr
1150 redef meth name
do return once
"unary -".to_symbol
1151 redef meth raw_arguments
do return null
1154 redef class ACallFormExpr
1155 redef meth after_typing
(v
)
1157 if n_expr
!= null and n_expr
.is_implicit_self
then
1158 var name
= n_id
.to_symbol
1159 var variable
= v
.variable_ctx
[name
]
1160 if variable
!= null then
1161 if variable
isa ClosureVariable then
1162 var n
= new AClosureCallExpr(n_id
, n_args
, n_closure_defs
)
1164 n
.variable
= variable
1168 if not n_args
.is_empty
then
1169 v
.error
(self, "Error: {name} is variable, not a function.")
1171 var vform
= variable_create
(variable
)
1172 vform
.variable
= variable
1174 vform
.after_typing
(v
)
1182 redef meth closure_defs
1184 if n_closure_defs
== null or n_closure_defs
.is_empty
then
1187 return n_closure_defs
.to_a
1191 # Create a variable acces corresponding to the call form
1192 meth variable_create
(variable
: Variable): AVarFormExpr is abstract
1195 redef class ACallExpr
1196 redef meth variable_create
(variable
)
1198 return new AVarExpr.init_avarexpr
(n_id
)
1201 redef meth name
do return n_id
.to_symbol
1202 redef meth raw_arguments
do return n_args
.to_a
1205 redef class ACallAssignExpr
1206 redef meth variable_create
(variable
)
1208 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1211 redef meth name
do return (n_id
.text
+ "=").to_symbol
1212 redef meth raw_arguments
do
1213 var res
= n_args
.to_a
1219 redef class ACallReassignExpr
1220 special ASendReassignExpr
1221 redef meth variable_create
(variable
)
1223 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1226 redef meth name
do return n_id
.to_symbol
1227 redef meth raw_arguments
do return n_args
.to_a
1230 redef class ABraExpr
1231 redef meth name
do return once
"[]".to_symbol
1232 redef meth raw_arguments
do return n_args
.to_a
1235 redef class ABraAssignExpr
1236 redef meth name
do return once
"[]=".to_symbol
1237 redef meth raw_arguments
do
1238 var res
= n_args
.to_a
1244 redef class ABraReassignExpr
1245 special ASendReassignExpr
1246 redef meth name
do return once
"[]".to_symbol
1247 redef meth raw_arguments
do return n_args
.to_a
1250 redef class AInitExpr
1251 redef meth name
do return once
"init".to_symbol
1252 redef meth raw_arguments
do return n_args
.to_a
1255 redef class AClosureCallExpr
1256 redef meth after_typing
(v
)
1259 var sig
= va
.signature
1260 var args
= process_signature
(v
, sig
, n_id
.to_symbol
, n_args
.to_a
)
1261 if args
== null then return
1263 _prop_signature
= sig
1265 _stype
= sig
.return_type
1269 redef class PClosureDef
1270 attr _accept_typing2
: Bool
1271 redef meth accept_typing
(v
)
1273 # Typing is deferred, wait accept_typing2(v)
1274 if _accept_typing2
then super
1277 private meth accept_typing2
(v
: TypingVisitor, sig
: MMSignature) is abstract
1280 redef class AClosureDef
1281 redef meth accept_typing2
(v
, sig
)
1283 if sig
.arity
!= n_id
.length
then
1284 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1290 var old_stype
= v
.closure_stype
1291 v
.closure_stype
= sig
.return_type
1293 v
.variable_ctx
= v
.variable_ctx
.sub
1294 variables
= new Array[AutoVariable]
1295 for i
in [0..n_id
.length
[ do
1296 var va
= new AutoVariable(n_id
[i
].to_symbol
, self)
1299 v
.variable_ctx
.add
(va
)
1302 _accept_typing2
= true
1305 v
.closure_stype
= old_stype
1309 redef class AIsaExpr
1310 redef meth after_typing
(v
)
1312 var variable
= n_expr
.its_variable
1313 if variable
!= null then
1314 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(variable
, n_type
.stype
)
1316 _stype
= v
.type_bool
1320 redef class AAsCastExpr
1321 redef meth after_typing
(v
)
1323 v
.check_expr
(n_expr
)
1324 _stype
= n_type
.stype
1328 redef class AProxyExpr
1329 redef meth after_typing
(v
)
1331 _stype
= n_expr
.stype