1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Analysis property bodies, statements and expressions
23 redef class MMSrcModule
24 # Walk trough the module and type statments and expressions
25 # Require than supermodules are processed
26 meth do_typing
(tc
: ToolContext)
28 var tv
= new TypingVisitor(tc
, self)
34 # * Associate local variables to nodes
35 # * Distinguish method call and local variable access
36 # * Resolve call and attribute access
37 # * Check type conformance
38 private class TypingVisitor
39 special AbsSyntaxVisitor
42 if n
!= null then n
.accept_typing
(self)
45 # Current knowledge about variables names and types
46 readable writable attr _variable_ctx
: VariableContext
48 # Current knowledge about escapable blocks
49 readable writable attr _escapable_ctx
: EscapableContext = new EscapableContext(self)
51 # The current reciever
52 readable writable attr _self_var
: ParamVariable
54 # Block of the current method
55 readable writable attr _top_block
: PExpr
57 # List of explicit invocation of constructors of super-classes
58 readable writable attr _explicit_super_init_calls
: Array[MMMethod]
60 # Is a other constructor of the same class invoked
61 readable writable attr _explicit_other_init_call
: Bool
63 # Make the if_true_variable_ctx of the expression effective
64 private meth use_if_true_variable_ctx
(e
: PExpr)
66 var ctx
= e
.if_true_variable_ctx
72 init(tc
, module) do super
74 private meth get_default_constructor_for
(n
: PNode, c
: MMLocalClass, prop
: MMSrcMethod): MMMethod
77 #var prop = v.local_property
78 #assert prop isa MMMethod
79 var candidates
= new Array[MMMethod]
80 var false_candidates
= new Array[MMMethod]
81 var parity
= prop
.signature
.arity
82 for g
in c
.global_properties
do
83 if not g
.is_init_for
(c
) then continue
85 var gps
= gp
.signature_for
(c
.get_type
)
86 assert gp
isa MMSrcMethod
87 var garity
= gps
.arity
88 if prop
!= null and gp
.name
== prop
.name
then
89 if garity
== 0 or (parity
== garity
and prop
.signature
< gps
) then
92 false_candidates
.add
(gp
)
94 else if garity
== 0 and gp
.name
== once
("init".to_symbol
) then
96 false_candidates
.add
(gp
)
98 false_candidates
.add
(gp
)
101 if candidates
.length
== 1 then
102 return candidates
.first
103 else if candidates
.length
> 0 then
104 var a
= new Array[String]
105 for p
in candidates
do
106 a
.add
("{p.full_name}{p.signature}")
108 v
.error
(n
, "Error: Conflicting default constructor to call for {c}: {a.join(", ")}.")
110 else if false_candidates
.length
> 0 then
111 var a
= new Array[String]
112 for p
in false_candidates
do
113 a
.add
("{p.full_name}{p.signature}")
115 v
.error
(n
, "Error: there is no available compatible constrctor in {c}. Discarded candidates are {a.join(", ")}.")
118 v
.error
(n
, "Error: there is no available compatible constrctor in {c}.")
124 # Associate symbols to variable and variables to type
126 private class VariableContext
127 # Look for the variable from its name
128 # Return null if nothing found
129 meth
[](s
: Symbol): Variable
131 if _dico
.has_key
(s
) then
138 # Register a new variable with its name
139 meth add
(v
: Variable)
145 # The effective static type of a given variable
146 # May be different from the declaration static type
147 meth stype
(v
: Variable): MMType
152 # Variables by name (in the current context only)
153 attr _dico
: Map[Symbol, Variable]
155 # Build a new VariableContext
156 meth sub
: SubVariableContext
158 return new SubVariableContext.with_prev
(self)
161 # Build a nested VariableContext with new variable information
162 meth sub_with
(v
: Variable, t
: MMType): SubVariableContext
164 return new CastVariableContext.with_prev
(self, v
, t
)
169 _dico
= new HashMap[Symbol, Variable]
173 private class SubVariableContext
174 special VariableContext
175 readable attr _prev
: VariableContext
179 if _dico
.has_key
(s
) then
191 init with_prev
(p
: VariableContext)
198 private class CastVariableContext
199 special SubVariableContext
200 attr _variable
: Variable
201 attr _var_type
: MMType
205 if _variable
== v
then
211 init with_prev
(p
: VariableContext, v
: Variable, t
: MMType)
219 ###############################################################################
222 private meth accept_typing
(v
: TypingVisitor)
224 accept_abs_syntax_visitor
(v
)
227 private meth after_typing
(v
: TypingVisitor) do end
230 redef class PClassdef
231 redef meth accept_typing
(v
)
233 v
.self_var
= new ParamVariable("self".to_symbol
, self)
234 v
.self_var
.stype
= local_class
.get_type
239 redef class AAttrPropdef
240 redef meth accept_typing
(v
)
243 if n_expr
!= null then
244 v
.check_conform_expr
(n_expr
, prop
.signature
.return_type
)
249 redef class AMethPropdef
250 redef readable attr _self_var
: ParamVariable
251 redef meth accept_typing
(v
)
253 v
.variable_ctx
= new VariableContext
254 _self_var
= v
.self_var
259 redef class AConcreteInitPropdef
260 readable attr _super_init_calls
: Array[MMMethod] = new Array[MMMethod]
261 readable attr _explicit_super_init_calls
: Array[MMMethod] = new Array[MMMethod]
262 redef meth accept_typing
(v
)
264 v
.top_block
= n_block
265 v
.explicit_super_init_calls
= explicit_super_init_calls
266 v
.explicit_other_init_call
= false
268 if v
.explicit_other_init_call
or method
.global
.intro
!= method
then
272 var l
= explicit_super_init_calls
.length
273 var cur_m
: MMMethod = null
274 var cur_c
: MMLocalClass = null
276 cur_m
= explicit_super_init_calls
[i
]
277 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
280 while j
< v
.local_class
.cshe
.direct_greaters
.length
do
281 var c
= v
.local_class
.cshe
.direct_greaters
[j
]
282 if c
.global
.is_interface
or c
.global
.is_universal
or c
.global
.is_mixin
then
284 else if cur_c
!= null and (c
.cshe
<= cur_c
or cur_c
.global
.is_mixin
) then
285 if c
== cur_c
then j
+= 1
286 super_init_calls
.add
(cur_m
)
289 cur_m
= explicit_super_init_calls
[i
]
290 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
296 var p
= v
.get_default_constructor_for
(self, c
, method
)
298 super_init_calls
.add
(p
)
308 redef meth after_typing
(v
)
310 # TODO: why the test?
311 if v
.variable_ctx
!= null then
312 v
.variable_ctx
.add
(variable
)
317 redef class AClosureDecl
318 # The corresponding escapable object
319 readable attr _escapable
: EscapableBlock
321 redef meth accept_typing
(v
)
323 # Register the closure for ClosureCallExpr
324 v
.variable_ctx
.add
(variable
)
326 var old_var_ctx
= v
.variable_ctx
327 v
.variable_ctx
= v
.variable_ctx
.sub
329 _escapable
= new EscapableClosure(self, variable
.closure
, null)
330 v
.escapable_ctx
.push
(_escapable
)
334 v
.variable_ctx
= old_var_ctx
340 readable attr _stype
: MMType
341 redef meth after_typing
(v
)
343 _stype
= get_stype
(v
)
348 redef readable attr _is_typed
: Bool = false
349 redef meth is_statement
: Bool do return _stype
== null
353 print
"{locate}: not is_typed"
357 print
"{locate}: is_statement"
364 # Is the expression the implicit receiver
365 meth is_implicit_self
: Bool do return false
367 # Is the expression the current receiver (implicit or explicit)
368 meth is_self
: Bool do return false
370 # The variable accessed is any
371 meth its_variable
: Variable do return null
373 # The variable type information if current boolean expression is true
374 readable private attr _if_true_variable_ctx
: VariableContext
377 redef class AVardeclExpr
378 redef meth after_typing
(v
)
380 var va
= new VarVariable(n_id
.to_symbol
, self)
382 v
.variable_ctx
.add
(va
)
384 if n_type
!= null then
385 va
.stype
= n_type
.stype
386 if n_expr
!= null then
387 v
.check_conform_expr
(n_expr
, va
.stype
)
390 if not v
.check_expr
(n_expr
) then return
391 va
.stype
= n_expr
.stype
397 redef class ABlockExpr
398 redef meth accept_typing
(v
)
400 var old_var_ctx
= v
.variable_ctx
401 v
.variable_ctx
= v
.variable_ctx
.sub
405 v
.variable_ctx
= old_var_ctx
410 redef class AReturnExpr
411 redef meth after_typing
(v
)
413 var t
= v
.local_property
.signature
.return_type
414 if n_expr
== null and t
!= null then
415 v
.error
(self, "Error: Return without value in a function.")
416 else if n_expr
!= null and t
== null then
417 v
.error
(self, "Error: Return with value in a procedure.")
418 else if n_expr
!= null and t
!= null then
419 v
.check_conform_expr
(n_expr
, t
)
425 redef class AContinueExpr
426 redef meth after_typing
(v
)
428 var esc
= compute_escapable_block
(v
.escapable_ctx
)
429 if esc
== null then return
431 if esc
.is_break_block
then
432 v
.error
(self, "Error: 'continue' forbiden in break blocks.")
436 var t
= esc
.continue_stype
437 if n_expr
== null and t
!= null then
438 v
.error
(self, "Error: continue with a value required in this block.")
439 else if n_expr
!= null and t
== null then
440 v
.error
(self, "Error: continue without value required in this block.")
441 else if n_expr
!= null and t
!= null then
442 v
.check_conform_expr
(n_expr
, t
)
448 redef class ABreakExpr
449 redef meth after_typing
(v
)
451 var esc
= compute_escapable_block
(v
.escapable_ctx
)
452 if esc
== null then return
454 var bl
= esc
.break_list
455 if n_expr
== null and bl
!= null then
456 v
.error
(self, "Error: break with a value required in this block.")
457 else if n_expr
!= null and bl
== null then
458 v
.error
(self, "Error: break without value required in this block.")
459 else if n_expr
!= null and bl
!= null then
460 # Typing check can only be done later
468 redef meth accept_typing
(v
)
470 var old_var_ctx
= v
.variable_ctx
472 v
.check_conform_expr
(n_expr
, v
.type_bool
)
474 v
.use_if_true_variable_ctx
(n_expr
)
477 # Restore variable ctx
478 v
.variable_ctx
= old_var_ctx
480 if n_else
!= null then
482 v
.variable_ctx
= old_var_ctx
488 redef class AWhileExpr
489 # The corresponding escapable block
490 readable attr _escapable
: EscapableBlock
492 redef meth accept_typing
(v
)
494 _escapable
= new EscapableBlock(self)
495 v
.escapable_ctx
.push
(_escapable
)
499 v
.check_conform_expr
(n_expr
, v
.type_bool
)
506 # The corresponding escapable block
507 readable attr _escapable
: EscapableBlock
509 readable attr _meth_iterator
: MMMethod
510 readable attr _meth_is_ok
: MMMethod
511 readable attr _meth_item
: MMMethod
512 readable attr _meth_next
: MMMethod
513 redef meth accept_typing
(v
)
515 _escapable
= new EscapableBlock(self)
516 v
.escapable_ctx
.push
(_escapable
)
518 v
.variable_ctx
= v
.variable_ctx
.sub
519 var va
= new AutoVariable(n_id
.to_symbol
, self)
521 v
.variable_ctx
.add
(va
)
525 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then return
526 var expr_type
= n_expr
.stype
527 _meth_iterator
= expr_type
.local_class
.select_method
(once
("iterator".to_symbol
))
528 if _meth_iterator
== null then
529 v
.error
(self, "Error: Collection MUST have an iterate method")
532 var iter_type
= _meth_iterator
.signature_for
(expr_type
).return_type
533 _meth_is_ok
= iter_type
.local_class
.select_method
(once
("is_ok".to_symbol
))
534 if _meth_is_ok
== null then
535 v
.error
(self, "Error: {iter_type} MUST have an is_ok method")
538 _meth_item
= iter_type
.local_class
.select_method
(once
("item".to_symbol
))
539 if _meth_item
== null then
540 v
.error
(self, "Error: {iter_type} MUST have an item method")
543 _meth_next
= iter_type
.local_class
.select_method
(once
("next".to_symbol
))
544 if _meth_next
== null then
545 v
.error
(self, "Error: {iter_type} MUST have a next method")
548 var t
= _meth_item
.signature_for
(iter_type
).return_type
549 if not n_expr
.is_self
then t
= t
.not_for_self
552 if n_block
!= null then v
.visit
(n_block
)
555 var varctx
= v
.variable_ctx
556 assert varctx
isa SubVariableContext
557 v
.variable_ctx
= varctx
.prev
564 redef class AAssertExpr
565 redef meth after_typing
(v
)
567 v
.check_conform_expr
(n_expr
, v
.type_bool
)
568 v
.use_if_true_variable_ctx
(n_expr
)
574 redef meth its_variable
do return variable
576 redef meth after_typing
(v
)
578 _stype
= v
.variable_ctx
.stype
(variable
)
579 _is_typed
= _stype
!= null
583 redef class AVarAssignExpr
584 redef meth after_typing
(v
)
586 var t
= v
.variable_ctx
.stype
(variable
)
587 v
.check_conform_expr
(n_value
, t
)
592 redef class AReassignFormExpr
593 # Compute and check method used through the reassigment operator
594 private meth do_lvalue_typing
(v
: TypingVisitor, type_lvalue
: MMType)
596 if type_lvalue
== null then
599 var name
= n_assign_op
.method_name
600 var lc
= type_lvalue
.local_class
601 if not lc
.has_global_property_by_name
(name
) then
602 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
605 var prop
= lc
.select_method
(name
)
606 prop
.global
.check_visibility
(v
, self, v
.module, false)
607 var psig
= prop
.signature_for
(type_lvalue
)
608 _assign_method
= prop
609 if not v
.check_conform_expr
(n_value
, psig
[0].not_for_self
) then return
610 if not v
.check_conform
(self, psig
.return_type
.not_for_self
, n_value
.stype
) then return
613 # Method used through the reassigment operator (once computed)
614 readable attr _assign_method
: MMMethod
617 redef class AVarReassignExpr
618 redef meth after_typing
(v
)
620 var t
= v
.variable_ctx
.stype
(variable
)
621 do_lvalue_typing
(v
, t
)
626 redef class PAssignOp
627 meth method_name
: Symbol is abstract
629 redef class APlusAssignOp
630 redef meth method_name
do return once
"+".to_symbol
632 redef class AMinusAssignOp
633 redef meth method_name
do return once
"-".to_symbol
636 redef class ASelfExpr
637 redef meth its_variable
do return variable
639 redef meth after_typing
(v
)
641 variable
= v
.self_var
642 _stype
= v
.variable_ctx
.stype
(variable
)
646 redef meth is_self
do return true
649 redef class AImplicitSelfExpr
650 redef meth is_implicit_self
do return true
653 redef class AIfexprExpr
654 redef meth accept_typing
(v
)
656 var old_var_ctx
= v
.variable_ctx
659 v
.use_if_true_variable_ctx
(n_expr
)
661 v
.variable_ctx
= old_var_ctx
664 v
.check_conform_expr
(n_expr
, v
.type_bool
)
666 _stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
667 _is_typed
= _stype
!= null
671 redef class ABoolExpr
672 redef meth after_typing
(v
)
680 redef meth after_typing
(v
)
682 v
.check_conform_expr
(n_expr
, v
.type_bool
)
683 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
690 redef meth accept_typing
(v
)
692 var old_var_ctx
= v
.variable_ctx
695 v
.use_if_true_variable_ctx
(n_expr
)
698 if n_expr2
.if_true_variable_ctx
!= null then
699 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
701 _if_true_variable_ctx
= v
.variable_ctx
704 v
.variable_ctx
= old_var_ctx
706 v
.check_conform_expr
(n_expr
, v
.type_bool
)
707 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
714 redef meth after_typing
(v
)
716 v
.check_conform_expr
(n_expr
, v
.type_bool
)
723 redef meth after_typing
(v
)
730 redef class AFloatExpr
731 redef meth after_typing
(v
)
733 _stype
= v
.type_float
738 redef class ACharExpr
739 redef meth after_typing
(v
)
746 redef class AStringFormExpr
747 readable attr _meth_with_native
: MMMethod
748 redef meth after_typing
(v
)
750 _stype
= v
.type_string
752 _meth_with_native
= _stype
.local_class
.select_method
(once
"with_native".to_symbol
)
753 if _meth_with_native
== null then v
.error
(self, "{_stype} MUST have a with_native method.")
757 redef class ASuperstringExpr
758 readable attr _meth_with_capacity
: MMMethod
759 readable attr _meth_add
: MMMethod
760 readable attr _meth_to_s
: MMMethod
761 readable attr _atype
: MMType
762 redef meth after_typing
(v
)
764 _stype
= v
.type_string
765 _atype
= v
.type_array
(_stype
)
766 _meth_with_capacity
= _atype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
767 if _meth_with_capacity
== null then v
.error
(self, "{_atype} MUST have a with_capacity method.")
768 _meth_add
= _atype
.local_class
.select_method
(once
"add".to_symbol
)
769 if _meth_add
== null then v
.error
(self, "{_atype} MUST have an add method.")
770 _meth_to_s
= v
.type_object
.local_class
.select_method
(once
"to_s".to_symbol
)
771 if _meth_to_s
== null then v
.error
(self, "Object MUST have a to_s method.")
776 redef class ANullExpr
777 redef meth after_typing
(v
)
784 redef class AArrayExpr
785 readable attr _meth_with_capacity
: MMMethod
786 readable attr _meth_add
: MMMethod
788 redef meth after_typing
(v
)
790 var stype
= v
.check_conform_multiexpr
(null, n_exprs
)
791 if stype
== null then return
795 private meth do_typing
(v
: TypingVisitor, element_type
: MMType)
797 _stype
= v
.type_array
(element_type
)
799 _meth_with_capacity
= _stype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
800 if _meth_with_capacity
== null then v
.error
(self, "{_stype} MUST have a with_capacity method.")
801 _meth_add
= _stype
.local_class
.select_method
(once
"add".to_symbol
)
802 if _meth_add
== null then v
.error
(self, "{_stype} MUST have an add method.")
808 redef class ARangeExpr
809 readable attr _meth_init
: MMMethod
810 redef meth after_typing
(v
)
812 if not v
.check_expr
(n_expr
) or not v
.check_expr
(n_expr2
) then return
813 var ntype
= n_expr
.stype
814 var ntype2
= n_expr2
.stype
815 if ntype
< ntype2
then
817 else if not ntype2
< ntype
then
818 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
821 var dtype
= v
.type_discrete
822 if not v
.check_conform_expr
(n_expr
, dtype
) or not v
.check_conform_expr
(n_expr2
, dtype
) then return
823 _stype
= v
.type_range
(ntype
)
828 redef class ACrangeExpr
829 redef meth after_typing
(v
)
832 _meth_init
= stype
.local_class
.select_method
(once
"init".to_symbol
)
835 redef class AOrangeExpr
836 redef meth after_typing
(v
)
839 _meth_init
= stype
.local_class
.select_method
(once
"without_last".to_symbol
)
844 redef class ASuperExpr
845 special ASuperInitCall
846 # readable attr _prop: MMSrcMethod
847 readable attr _init_in_superclass
: MMMethod
848 redef meth after_typing
(v
)
850 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
851 if not precs
.is_empty
then
852 v
.local_property
.need_super
= true
853 else if v
.local_property
.global
.is_init
then
854 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
855 for p
in base_precs
do
856 if not p
.global
.is_init
then
857 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
859 precs
.add
(v
.local_class
[p
.global
])
862 if precs
.is_empty
then
863 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
865 else if precs
.length
> 1 then
866 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
869 var p
= base_precs
.first
870 assert p
isa MMMethod
871 _init_in_superclass
= p
872 register_super_init_call
(v
, p
)
873 if n_args
.length
> 0 then
874 var signature
= get_signature
(v
, v
.self_var
.stype
, p
, true)
875 _arguments
= process_signature
(v
, signature
, p
.name
, n_args
.to_a
)
878 v
.error
(self, "Error: No super method to call for {v.local_property}.")
882 if precs
.first
.signature_for
(v
.self_var
.stype
).return_type
!= null then
883 var stypes
= new Array[MMType]
884 var stype
: MMType = null
886 assert prop
isa MMMethod
887 var t
= prop
.signature_for
(v
.self_var
.stype
).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
889 if stype
== null or stype
< t
then
894 v
.check_conform
(self, t
, stype
)
898 var p
= v
.local_property
899 assert p
isa MMSrcMethod
905 redef class AAttrFormExpr
907 readable attr _prop
: MMAttribute
909 # Attribute type of the acceded attribute
910 readable attr _attr_type
: MMType
912 # Compute the attribute accessed
913 private meth do_typing
(v
: TypingVisitor)
915 if not v
.check_expr
(n_expr
) then return
916 var type_recv
= n_expr
.stype
917 var name
= n_id
.to_symbol
918 var lc
= type_recv
.local_class
919 if not lc
.has_global_property_by_name
(name
) then
920 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
923 var prop
= lc
.select_attribute
(name
)
924 if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
925 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
928 var at
= prop
.signature_for
(type_recv
).return_type
929 if not n_expr
.is_self
then at
= at
.not_for_self
934 redef class AAttrExpr
935 redef meth after_typing
(v
)
938 if prop
== null then return
944 redef class AAttrAssignExpr
945 redef meth after_typing
(v
)
948 if prop
== null then return
949 if not v
.check_conform_expr
(n_value
, attr_type
) then return
954 redef class AAttrReassignExpr
955 redef meth after_typing
(v
)
958 if prop
== null then return
959 do_lvalue_typing
(v
, attr_type
)
964 class AAbsAbsSendExpr
966 # The signature of the called property
967 readable attr _prop_signature
: MMSignature
969 # The real arguments used (after star transformation) (once computed)
970 readable attr _arguments
: Array[PExpr]
972 # Check the conformity of a set of arguments `raw_args' to a signature.
973 private meth process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: Array[PExpr]): Array[PExpr]
975 var par_vararg
= psig
.vararg_rank
976 var par_arity
= psig
.arity
978 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
979 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
980 v
.error
(self, "Error: '{name}' arity missmatch.")
984 var args
= new Array[PExpr]
985 for par_idx
in [0..par_arity
[ do
987 var par_type
= psig
[par_idx
]
988 if par_idx
== par_vararg
then
989 var star
= new Array[PExpr]
990 for i
in [0..(raw_arity-par_arity
)] do
991 a
= raw_args
[arg_idx
]
992 v
.check_conform_expr
(a
, par_type
)
994 arg_idx
= arg_idx
+ 1
996 var aa
= new AArrayExpr.init_aarrayexpr
(star
)
997 aa
.do_typing
(v
, par_type
)
1000 a
= raw_args
[arg_idx
]
1001 v
.check_conform_expr
(a
, par_type
)
1002 arg_idx
= arg_idx
+ 1
1009 # Check the conformity of a set of defined closures
1010 private meth process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: Array[PClosureDef]): MMType
1012 var t
= psig
.return_type
1013 var cs
= psig
.closures
# Declared closures
1016 if not c
.is_optional
then min_arity
+= 1
1019 if cs
.length
== 0 then
1020 v
.error
(self, "Error: {name} does not require blocks.")
1021 else if cd
.length
> cs
.length
or cd
.length
< min_arity
then
1022 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
1024 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1025 var break_list
: Array[ABreakExpr] = null
1026 if t
!= null then break_list
= new Array[ABreakExpr]
1028 # Process each closure definition
1029 for i
in [0..cd
.length
[ do
1032 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1033 v
.escapable_ctx
.push
(esc
)
1034 cdi
.accept_typing2
(v
, esc
)
1038 # Check break type conformity
1039 if break_list
!= null then
1040 t
= v
.check_conform_multiexpr
(t
, break_list
)
1043 else if min_arity
!= 0 then
1044 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1051 special AAbsAbsSendExpr
1052 # Compute the called global property
1053 private meth do_typing
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, recv_is_self
: Bool, name
: Symbol, raw_args
: Array[PExpr], closure_defs
: Array[PClosureDef])
1055 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
1056 if prop
== null then return
1057 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
1058 if sig
== null then return
1059 var args
= process_signature
(v
, sig
, prop
.name
, raw_args
)
1060 if args
== null then return
1061 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
1062 if rtype
== null and sig
.return_type
!= null then return
1064 _prop_signature
= sig
1066 _return_type
= rtype
1069 private meth get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): MMMethod
1071 if type_recv
== null then return null
1072 var lc
= type_recv
.local_class
1073 var prop
: MMMethod = null
1074 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
1075 if prop
== null and v
.local_property
.global
.is_init
then
1076 var props
= type_recv
.local_class
.super_methods_named
(name
)
1077 if props
.length
> 1 then
1078 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1080 else if props
.length
== 1 then
1081 var p
= type_recv
.local_class
[props
.first
.global
]
1082 assert p
isa MMMethod
1087 if prop
== null then
1088 if is_implicit_self
then
1089 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1091 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1098 # Get the signature for a local property and a receiver
1099 private meth get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
1101 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
1102 var psig
= prop
.signature_for
(type_recv
)
1103 if not recv_is_self
then psig
= psig
.not_for_self
1107 # The invoked method (once computed)
1108 readable attr _prop
: MMMethod
1110 # The return type (if any) (once computed)
1111 readable attr _return_type
: MMType
1114 # A possible call of constructor in a super class
1115 # Could be an explicit call or with the 'super' keyword
1116 class ASuperInitCall
1117 special AAbsSendExpr
1118 private meth register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1120 if parent
!= v
.top_block
and self != v
.top_block
then
1121 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1123 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
1124 var prev_class
: MMLocalClass = null
1125 if not v
.explicit_super_init_calls
.is_empty
then
1126 prev_class
= v
.explicit_super_init_calls
.last
.global
.intro
.local_class
1128 var order
= v
.local_class
.cshe
.reverse_linear_extension
1129 if cla
== v
.local_class
then
1130 v
.explicit_other_init_call
= true
1131 else if not order
.has
(cla
) then
1132 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1133 else if cla
== prev_class
then
1134 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1136 var last_is_found
= prev_class
== null
1138 if c
== prev_class
then
1139 last_is_found
= true
1140 else if c
== cla
then
1141 if not last_is_found
then
1142 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1144 v
.explicit_super_init_calls
.add
(property
)
1153 redef class ANewExpr
1154 special AAbsSendExpr
1155 redef meth after_typing
(v
)
1157 var t
= n_type
.stype
1158 if t
== null then return
1159 if t
.local_class
.global
.is_abstract
then
1160 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1164 if n_id
== null then
1165 name
= once
"init".to_symbol
1167 name
= n_id
.to_symbol
1170 do_typing
(v
, t
, false, false, name
, n_args
.to_a
, null)
1171 if prop
== null then return
1173 if not prop
.global
.is_init
then
1174 v
.error
(self, "Error: {prop} is not a constructor.")
1183 redef class ASendExpr
1184 special ASuperInitCall
1185 # Name of the invoked property
1186 meth name
: Symbol is abstract
1188 # Raw arguments used (withour star transformation)
1189 meth raw_arguments
: Array[PExpr] is abstract
1191 # Closure definitions
1192 meth closure_defs
: Array[PClosureDef] do return null
1194 redef meth after_typing
(v
)
1199 private meth do_all_typing
(v
: TypingVisitor)
1201 if not v
.check_expr
(n_expr
) then return
1202 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1203 if prop
== null then return
1205 if prop
.global
.is_init
then
1206 if not v
.local_property
.global
.is_init
then
1207 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1208 else if not n_expr
.is_self
then
1209 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1211 register_super_init_call
(v
, prop
)
1215 _stype
= return_type
1220 class ASendReassignExpr
1222 special AReassignFormExpr
1223 readable attr _read_prop
: MMMethod
1224 redef meth do_all_typing
(v
)
1226 if not v
.check_expr
(n_expr
) then return
1227 var raw_args
= raw_arguments
1228 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1229 if prop
== null then return
1230 if prop
.global
.is_init
then
1231 if not v
.local_property
.global
.is_init
then
1232 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1233 else if not n_expr
.is_self
then
1234 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1237 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1238 if not n_expr
.is_self
then t
= t
.not_for_self
1240 do_lvalue_typing
(v
, t
)
1243 var old_args
= arguments
1244 raw_args
.add
(n_value
)
1246 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1247 if prop
== null then return
1248 if prop
.global
.is_init
then
1249 if not v
.local_property
.global
.is_init
then
1250 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1251 else if not n_expr
.is_self
then
1252 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1256 _arguments
= old_args
# FIXME: What if star parameters do not match betwen the two methods?
1261 redef class ABinopExpr
1262 redef meth raw_arguments
do return [n_expr2
]
1265 redef meth name
do return once
"==".to_symbol
1268 redef meth name
do return once
"!=".to_symbol
1271 redef meth name
do return once
"<".to_symbol
1274 redef meth name
do return once
"<=".to_symbol
1277 redef meth name
do return once
">".to_symbol
1280 redef meth name
do return once
">=".to_symbol
1282 redef class APlusExpr
1283 redef meth name
do return once
"+".to_symbol
1285 redef class AMinusExpr
1286 redef meth name
do return once
"-".to_symbol
1288 redef class AStarshipExpr
1289 redef meth name
do return once
"<=>".to_symbol
1291 redef class AStarExpr
1292 redef meth name
do return once
"*".to_symbol
1294 redef class ASlashExpr
1295 redef meth name
do return once
"/".to_symbol
1297 redef class APercentExpr
1298 redef meth name
do return once
"%".to_symbol
1301 redef class AUminusExpr
1302 redef meth name
do return once
"unary -".to_symbol
1303 redef meth raw_arguments
do return null
1306 redef class ACallFormExpr
1307 redef meth after_typing
(v
)
1309 if n_expr
!= null and n_expr
.is_implicit_self
then
1310 var name
= n_id
.to_symbol
1311 var variable
= v
.variable_ctx
[name
]
1312 if variable
!= null then
1313 if variable
isa ClosureVariable then
1314 var n
= new AClosureCallExpr.init_aclosurecallexpr
(n_id
, n_args
, n_closure_defs
)
1316 n
.variable
= variable
1320 if not n_args
.is_empty
then
1321 v
.error
(self, "Error: {name} is variable, not a function.")
1323 var vform
= variable_create
(variable
)
1324 vform
.variable
= variable
1326 vform
.after_typing
(v
)
1335 redef meth closure_defs
1337 if n_closure_defs
== null or n_closure_defs
.is_empty
then
1340 return n_closure_defs
.to_a
1344 # Create a variable acces corresponding to the call form
1345 meth variable_create
(variable
: Variable): AVarFormExpr is abstract
1348 redef class ACallExpr
1349 redef meth variable_create
(variable
)
1351 return new AVarExpr.init_avarexpr
(n_id
)
1354 redef meth name
do return n_id
.to_symbol
1355 redef meth raw_arguments
do return n_args
.to_a
1358 redef class ACallAssignExpr
1359 redef meth variable_create
(variable
)
1361 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1364 redef meth name
do return (n_id
.text
+ "=").to_symbol
1365 redef meth raw_arguments
do
1366 var res
= n_args
.to_a
1372 redef class ACallReassignExpr
1373 special ASendReassignExpr
1374 redef meth variable_create
(variable
)
1376 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1379 redef meth name
do return n_id
.to_symbol
1380 redef meth raw_arguments
do return n_args
.to_a
1383 redef class ABraExpr
1384 redef meth name
do return once
"[]".to_symbol
1385 redef meth raw_arguments
do return n_args
.to_a
1388 redef class ABraAssignExpr
1389 redef meth name
do return once
"[]=".to_symbol
1390 redef meth raw_arguments
do
1391 var res
= n_args
.to_a
1397 redef class ABraReassignExpr
1398 special ASendReassignExpr
1399 redef meth name
do return once
"[]".to_symbol
1400 redef meth raw_arguments
do return n_args
.to_a
1403 redef class AInitExpr
1404 redef meth name
do return once
"init".to_symbol
1405 redef meth raw_arguments
do return n_args
.to_a
1408 redef class AClosureCallExpr
1409 special AAbsAbsSendExpr
1410 redef meth after_typing
(v
)
1413 var sig
= va
.closure
.signature
1414 var args
= process_signature
(v
, sig
, n_id
.to_symbol
, n_args
.to_a
)
1415 if not n_closure_defs
.is_empty
then
1416 process_closures
(v
, sig
, n_id
.to_symbol
, n_closure_defs
.to_a
)
1418 if args
== null then return
1419 _prop_signature
= sig
1421 _stype
= sig
.return_type
1426 redef class PClosureDef
1427 # The corresponding escapable object
1428 readable attr _escapable
: EscapableBlock
1430 attr _accept_typing2
: Bool
1431 redef meth accept_typing
(v
)
1433 # Typing is deferred, wait accept_typing2(v)
1434 if _accept_typing2
then super
1437 private meth accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure) is abstract
1440 redef class AClosureDef
1441 redef meth accept_typing2
(v
, esc
)
1445 var sig
= esc
.closure
.signature
1446 if sig
.arity
!= n_id
.length
then
1447 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1451 closure
= esc
.closure
1453 v
.variable_ctx
= v
.variable_ctx
.sub
1454 variables
= new Array[AutoVariable]
1455 for i
in [0..n_id
.length
[ do
1456 var va
= new AutoVariable(n_id
[i
].to_symbol
, self)
1459 v
.variable_ctx
.add
(va
)
1462 _accept_typing2
= true
1467 redef class AIsaExpr
1468 redef meth after_typing
(v
)
1470 var variable
= n_expr
.its_variable
1471 if variable
!= null then
1472 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(variable
, n_type
.stype
)
1474 _stype
= v
.type_bool
1479 redef class AAsCastExpr
1480 redef meth after_typing
(v
)
1482 v
.check_expr
(n_expr
)
1483 _stype
= n_type
.stype
1484 _is_typed
= _stype
!= null
1488 redef class AProxyExpr
1489 redef meth after_typing
(v
)
1491 if not n_expr
.is_typed
then return
1493 if n_expr
.is_statement
then return
1494 _stype
= n_expr
.stype