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 _stype
: MMType
350 # Is the expression the implicit receiver
351 meth is_implicit_self
: Bool do return false
353 # Is the expression the current receiver (implicit or explicit)
354 meth is_self
: Bool do return false
356 # The variable accessed is any
357 meth its_variable
: Variable do return null
359 # The variable type information if current boolean expression is true
360 readable private attr _if_true_variable_ctx
: VariableContext
363 redef class AVardeclExpr
364 redef meth after_typing
(v
)
366 var va
= new VarVariable(n_id
.to_symbol
, self)
368 v
.variable_ctx
.add
(va
)
370 if n_type
!= null then
371 va
.stype
= n_type
.stype
372 if n_expr
!= null then
373 v
.check_conform_expr
(n_expr
, va
.stype
)
377 va
.stype
= n_expr
.stype
382 redef class ABlockExpr
383 redef meth accept_typing
(v
)
385 var old_var_ctx
= v
.variable_ctx
386 v
.variable_ctx
= v
.variable_ctx
.sub
390 v
.variable_ctx
= old_var_ctx
394 redef class AReturnExpr
395 redef meth after_typing
(v
)
397 var t
= v
.local_property
.signature
.return_type
398 if n_expr
== null and t
!= null then
399 v
.error
(self, "Error: Return without value in a function.")
400 else if n_expr
!= null and t
== null then
401 v
.error
(self, "Error: Return with value in a procedure.")
402 else if n_expr
!= null and t
!= null then
403 v
.check_conform_expr
(n_expr
, t
)
408 redef class AContinueExpr
409 redef meth after_typing
(v
)
411 var esc
= compute_escapable_block
(v
.escapable_ctx
)
412 if esc
== null then return
414 if esc
.is_break_block
then
415 v
.error
(self, "Error: 'continue' forbiden in break blocks.")
419 var t
= esc
.continue_stype
420 if n_expr
== null and t
!= null then
421 v
.error
(self, "Error: continue with a value required in this block.")
422 else if n_expr
!= null and t
== null then
423 v
.error
(self, "Error: continue without value required in this block.")
424 else if n_expr
!= null and t
!= null then
425 v
.check_conform_expr
(n_expr
, t
)
430 redef class ABreakExpr
431 redef meth after_typing
(v
)
433 var esc
= compute_escapable_block
(v
.escapable_ctx
)
434 if esc
== null then return
436 var bl
= esc
.break_list
437 if n_expr
== null and bl
!= null then
438 v
.error
(self, "Error: break with a value required in this block.")
439 else if n_expr
!= null and bl
== null then
440 v
.error
(self, "Error: break without value required in this block.")
441 else if n_expr
!= null and bl
!= null then
442 # Typing check can only be done later
449 redef meth accept_typing
(v
)
451 var old_var_ctx
= v
.variable_ctx
453 v
.check_conform_expr
(n_expr
, v
.type_bool
)
455 v
.use_if_true_variable_ctx
(n_expr
)
458 # Restore variable ctx
459 v
.variable_ctx
= old_var_ctx
461 if n_else
!= null then
463 v
.variable_ctx
= old_var_ctx
468 redef class AWhileExpr
469 # The corresponding escapable block
470 readable attr _escapable
: EscapableBlock
472 redef meth accept_typing
(v
)
474 _escapable
= new EscapableBlock(self)
475 v
.escapable_ctx
.push
(_escapable
)
479 v
.check_conform_expr
(n_expr
, v
.type_bool
)
485 # The corresponding escapable block
486 readable attr _escapable
: EscapableBlock
488 readable attr _meth_iterator
: MMMethod
489 readable attr _meth_is_ok
: MMMethod
490 readable attr _meth_item
: MMMethod
491 readable attr _meth_next
: MMMethod
492 redef meth accept_typing
(v
)
494 _escapable
= new EscapableBlock(self)
495 v
.escapable_ctx
.push
(_escapable
)
497 v
.variable_ctx
= v
.variable_ctx
.sub
498 var va
= new AutoVariable(n_id
.to_symbol
, self)
500 v
.variable_ctx
.add
(va
)
504 var expr_type
= n_expr
.stype
505 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then
508 _meth_iterator
= expr_type
.local_class
.select_method
(once
("iterator".to_symbol
))
509 if _meth_iterator
== null then
510 v
.error
(self, "Error: Collection MUST have an iterate method")
513 var iter_type
= _meth_iterator
.signature_for
(expr_type
).return_type
514 _meth_is_ok
= iter_type
.local_class
.select_method
(once
("is_ok".to_symbol
))
515 if _meth_is_ok
== null then
516 v
.error
(self, "Error: {iter_type} MUST have an is_ok method")
519 _meth_item
= iter_type
.local_class
.select_method
(once
("item".to_symbol
))
520 if _meth_item
== null then
521 v
.error
(self, "Error: {iter_type} MUST have an item method")
524 _meth_next
= iter_type
.local_class
.select_method
(once
("next".to_symbol
))
525 if _meth_next
== null then
526 v
.error
(self, "Error: {iter_type} MUST have a next method")
529 var t
= _meth_item
.signature_for
(iter_type
).return_type
530 if not n_expr
.is_self
then t
= t
.not_for_self
533 if n_block
!= null then v
.visit
(n_block
)
536 var varctx
= v
.variable_ctx
537 assert varctx
isa SubVariableContext
538 v
.variable_ctx
= varctx
.prev
544 redef class AAssertExpr
545 redef meth after_typing
(v
)
547 v
.check_conform_expr
(n_expr
, v
.type_bool
)
548 v
.use_if_true_variable_ctx
(n_expr
)
553 redef meth its_variable
do return variable
555 redef meth after_typing
(v
)
557 _stype
= v
.variable_ctx
.stype
(variable
)
561 redef class AVarAssignExpr
562 redef meth after_typing
(v
)
564 var t
= v
.variable_ctx
.stype
(variable
)
565 v
.check_conform_expr
(n_value
, t
)
569 redef class AReassignFormExpr
570 # Compute and check method used through the reassigment operator
571 private meth do_lvalue_typing
(v
: TypingVisitor, type_lvalue
: MMType)
573 if type_lvalue
== null then
576 var name
= n_assign_op
.method_name
577 var lc
= type_lvalue
.local_class
578 if not lc
.has_global_property_by_name
(name
) then
579 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
582 var prop
= lc
.select_method
(name
)
583 prop
.global
.check_visibility
(v
, self, v
.module, false)
584 var psig
= prop
.signature_for
(type_lvalue
)
585 _assign_method
= prop
586 v
.check_conform_expr
(n_value
, psig
[0].not_for_self
)
587 v
.check_conform
(self, psig
.return_type
.not_for_self
, n_value
.stype
)
590 # Method used through the reassigment operator (once computed)
591 readable attr _assign_method
: MMMethod
594 redef class AVarReassignExpr
595 redef meth after_typing
(v
)
597 var t
= v
.variable_ctx
.stype
(variable
)
598 do_lvalue_typing
(v
, t
)
602 redef class PAssignOp
603 meth method_name
: Symbol is abstract
605 redef class APlusAssignOp
606 redef meth method_name
do return once
"+".to_symbol
608 redef class AMinusAssignOp
609 redef meth method_name
do return once
"-".to_symbol
612 redef class ASelfExpr
613 redef meth its_variable
do return variable
615 redef meth after_typing
(v
)
617 variable
= v
.self_var
618 _stype
= v
.variable_ctx
.stype
(variable
)
621 redef meth is_self
do return true
624 redef class AImplicitSelfExpr
625 redef meth is_implicit_self
do return true
628 redef class AIfexprExpr
629 redef meth accept_typing
(v
)
631 var old_var_ctx
= v
.variable_ctx
634 v
.use_if_true_variable_ctx
(n_expr
)
636 v
.variable_ctx
= old_var_ctx
639 v
.check_conform_expr
(n_expr
, v
.type_bool
)
641 _stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
645 redef class ABoolExpr
646 redef meth after_typing
(v
)
653 redef meth after_typing
(v
)
655 v
.check_conform_expr
(n_expr
, v
.type_bool
)
656 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
662 redef meth accept_typing
(v
)
664 var old_var_ctx
= v
.variable_ctx
667 v
.use_if_true_variable_ctx
(n_expr
)
670 if n_expr2
.if_true_variable_ctx
!= null then
671 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
673 _if_true_variable_ctx
= v
.variable_ctx
676 v
.variable_ctx
= old_var_ctx
678 v
.check_conform_expr
(n_expr
, v
.type_bool
)
679 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
685 redef meth after_typing
(v
)
687 v
.check_conform_expr
(n_expr
, v
.type_bool
)
693 redef meth after_typing
(v
)
700 redef class AFloatExpr
701 redef meth after_typing
(v
)
703 _stype
= v
.type_float
707 redef class ACharExpr
708 redef meth after_typing
(v
)
714 redef class AStringFormExpr
715 readable attr _meth_with_native
: MMMethod
716 redef meth after_typing
(v
)
718 _stype
= v
.type_string
719 _meth_with_native
= _stype
.local_class
.select_method
(once
"with_native".to_symbol
)
720 if _meth_with_native
== null then v
.error
(self, "{_stype} MUST have a with_native method.")
724 redef class ASuperstringExpr
725 readable attr _meth_with_capacity
: MMMethod
726 readable attr _meth_add
: MMMethod
727 readable attr _meth_to_s
: MMMethod
728 readable attr _atype
: MMType
729 redef meth after_typing
(v
)
731 _stype
= v
.type_string
732 _atype
= v
.type_array
(_stype
)
733 _meth_with_capacity
= _atype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
734 if _meth_with_capacity
== null then v
.error
(self, "{_atype} MUST have a with_capacity method.")
735 _meth_add
= _atype
.local_class
.select_method
(once
"add".to_symbol
)
736 if _meth_add
== null then v
.error
(self, "{_atype} MUST have an add method.")
737 _meth_to_s
= v
.type_object
.local_class
.select_method
(once
"to_s".to_symbol
)
738 if _meth_to_s
== null then v
.error
(self, "Object MUST have a to_s method.")
742 redef class ANullExpr
743 redef meth after_typing
(v
)
749 redef class AArrayExpr
750 readable attr _meth_with_capacity
: MMMethod
751 readable attr _meth_add
: MMMethod
753 redef meth after_typing
(v
)
755 var stype
= v
.check_conform_multiexpr
(null, n_exprs
)
756 if stype
== null then return
760 private meth do_typing
(v
: TypingVisitor, element_type
: MMType)
762 _stype
= v
.type_array
(element_type
)
764 _meth_with_capacity
= _stype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
765 if _meth_with_capacity
== null then v
.error
(self, "{_stype} MUST have a with_capacity method.")
766 _meth_add
= _stype
.local_class
.select_method
(once
"add".to_symbol
)
767 if _meth_add
== null then v
.error
(self, "{_stype} MUST have an add method.")
771 redef class ARangeExpr
772 readable attr _meth_init
: MMMethod
773 redef meth after_typing
(v
)
775 var ntype
= n_expr
.stype
776 var ntype2
= n_expr2
.stype
777 if ntype
== null or ntype
== null then
780 if ntype
< ntype2
then
782 else if not ntype2
< ntype
then
783 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
786 var dtype
= v
.type_discrete
787 v
.check_conform_expr
(n_expr
, dtype
)
788 v
.check_conform_expr
(n_expr2
, dtype
)
789 _stype
= v
.type_range
(ntype
)
793 redef class ACrangeExpr
794 redef meth after_typing
(v
)
797 _meth_init
= stype
.local_class
.select_method
(once
"init".to_symbol
)
800 redef class AOrangeExpr
801 redef meth after_typing
(v
)
804 _meth_init
= stype
.local_class
.select_method
(once
"without_last".to_symbol
)
809 redef class ASuperExpr
810 special ASuperInitCall
811 # readable attr _prop: MMSrcMethod
812 readable attr _init_in_superclass
: MMMethod
813 redef meth after_typing
(v
)
815 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
816 if not precs
.is_empty
then
817 v
.local_property
.need_super
= true
818 else if v
.local_property
.global
.is_init
then
819 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
820 for p
in base_precs
do
821 if not p
.global
.is_init
then
822 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
824 precs
.add
(v
.local_class
[p
.global
])
827 if precs
.is_empty
then
828 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
830 else if precs
.length
> 1 then
831 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
834 var p
= base_precs
.first
835 assert p
isa MMMethod
836 _init_in_superclass
= p
837 register_super_init_call
(v
, p
)
838 if n_args
.length
> 0 then
839 var signature
= get_signature
(v
, v
.self_var
.stype
, p
, true)
840 _arguments
= process_signature
(v
, signature
, p
.name
, n_args
.to_a
)
843 v
.error
(self, "Error: No super method to call for {v.local_property}.")
847 if precs
.first
.signature_for
(v
.self_var
.stype
).return_type
!= null then
848 var stypes
= new Array[MMType]
849 var stype
: MMType = null
851 assert prop
isa MMMethod
852 var t
= prop
.signature_for
(v
.self_var
.stype
).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
854 if stype
== null or stype
< t
then
859 v
.check_conform
(self, t
, stype
)
863 var p
= v
.local_property
864 assert p
isa MMSrcMethod
869 redef class AAttrFormExpr
871 readable attr _prop
: MMAttribute
873 # Attribute type of the acceded attribute
874 readable attr _attr_type
: MMType
876 # Compute the attribute accessed
877 private meth do_typing
(v
: TypingVisitor)
879 if not v
.check_expr
(n_expr
) then return
880 var type_recv
= n_expr
.stype
881 var name
= n_id
.to_symbol
882 var lc
= type_recv
.local_class
883 if not lc
.has_global_property_by_name
(name
) then
884 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
887 var prop
= lc
.select_attribute
(name
)
888 if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
889 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
892 var at
= prop
.signature_for
(type_recv
).return_type
893 if not n_expr
.is_self
then at
= at
.not_for_self
898 redef class AAttrExpr
899 redef meth after_typing
(v
)
909 redef class AAttrAssignExpr
910 redef meth after_typing
(v
)
916 v
.check_conform_expr
(n_value
, attr_type
)
920 redef class AAttrReassignExpr
921 redef meth after_typing
(v
)
927 do_lvalue_typing
(v
, attr_type
)
933 # The signature of the called property
934 readable attr _prop_signature
: MMSignature
936 # Compute the called global property
937 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])
939 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
940 if prop
== null then return
941 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
942 if sig
== null then return
943 var args
= process_signature
(v
, sig
, prop
.name
, raw_args
)
944 if args
== null then return
945 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
947 _prop_signature
= sig
952 private meth get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): MMMethod
954 if type_recv
== null then return null
955 var lc
= type_recv
.local_class
956 var prop
: MMMethod = null
957 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
958 if prop
== null and v
.local_property
.global
.is_init
then
959 var props
= type_recv
.local_class
.super_methods_named
(name
)
960 if props
.length
> 1 then
961 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
963 else if props
.length
== 1 then
964 var p
= type_recv
.local_class
[props
.first
.global
]
965 assert p
isa MMMethod
971 if is_implicit_self
then
972 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
974 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
981 # Get the signature for a local property and a receiver
982 private meth get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
984 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
985 var psig
= prop
.signature_for
(type_recv
)
986 if not recv_is_self
then psig
= psig
.not_for_self
990 # Check the conformity of a set of arguments `raw_args' to a signature.
991 private meth process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: Array[PExpr]): Array[PExpr]
993 var par_vararg
= psig
.vararg_rank
994 var par_arity
= psig
.arity
996 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
997 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
998 v
.error
(self, "Error: '{name}' arity missmatch.")
1002 var args
= new Array[PExpr]
1003 for par_idx
in [0..par_arity
[ do
1005 var par_type
= psig
[par_idx
]
1006 if par_idx
== par_vararg
then
1007 var star
= new Array[PExpr]
1008 for i
in [0..(raw_arity-par_arity
)] do
1009 a
= raw_args
[arg_idx
]
1010 v
.check_conform_expr
(a
, par_type
)
1012 arg_idx
= arg_idx
+ 1
1014 var aa
= new AArrayExpr.init_aarrayexpr
(star
)
1015 aa
.do_typing
(v
, par_type
)
1018 a
= raw_args
[arg_idx
]
1019 v
.check_conform_expr
(a
, par_type
)
1020 arg_idx
= arg_idx
+ 1
1027 # Check the conformity of a set of defined closures
1028 private meth process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: Array[PClosureDef]): MMType
1030 var t
= psig
.return_type
1031 var cs
= psig
.closures
# Declared closures
1034 if not c
.is_optional
then min_arity
+= 1
1037 if cs
.length
== 0 then
1038 v
.error
(self, "Error: {name} does not require blocks.")
1039 else if cd
.length
> cs
.length
or cd
.length
< min_arity
then
1040 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
1042 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1043 var break_list
: Array[ABreakExpr] = null
1044 if t
!= null then break_list
= new Array[ABreakExpr]
1046 # Process each closure definition
1047 for i
in [0..cd
.length
[ do
1050 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1051 v
.escapable_ctx
.push
(esc
)
1052 cdi
.accept_typing2
(v
, esc
)
1056 # Check break type conformity
1057 if break_list
!= null then
1058 t
= v
.check_conform_multiexpr
(t
, break_list
)
1061 else if min_arity
!= 0 then
1062 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1067 # The invoked method (once computed)
1068 readable attr _prop
: MMMethod
1070 # The real arguments used (after star transformation) (once computed)
1071 readable attr _arguments
: Array[PExpr]
1073 # The return type (if any) (once computed)
1074 readable attr _return_type
: MMType
1077 # A possible call of constructor in a super class
1078 # Could be an explicit call or with the 'super' keyword
1079 class ASuperInitCall
1080 special AAbsSendExpr
1081 private meth register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1083 if parent
!= v
.top_block
and self != v
.top_block
then
1084 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1086 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
1087 var prev_class
: MMLocalClass = null
1088 if not v
.explicit_super_init_calls
.is_empty
then
1089 prev_class
= v
.explicit_super_init_calls
.last
.global
.intro
.local_class
1091 var order
= v
.local_class
.cshe
.reverse_linear_extension
1092 if cla
== v
.local_class
then
1093 v
.explicit_other_init_call
= true
1094 else if not order
.has
(cla
) then
1095 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1096 else if cla
== prev_class
then
1097 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1099 var last_is_found
= prev_class
== null
1101 if c
== prev_class
then
1102 last_is_found
= true
1103 else if c
== cla
then
1104 if not last_is_found
then
1105 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1107 v
.explicit_super_init_calls
.add
(property
)
1116 redef class ANewExpr
1117 special AAbsSendExpr
1118 redef meth after_typing
(v
)
1120 var t
= n_type
.stype
1121 if t
== null then return
1122 if t
.local_class
.global
.is_abstract
then
1123 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1127 if n_id
== null then
1128 name
= once
"init".to_symbol
1130 name
= n_id
.to_symbol
1133 do_typing
(v
, t
, false, false, name
, n_args
.to_a
, null)
1134 if prop
== null then return
1136 if not prop
.global
.is_init
then
1137 v
.error
(self, "Error: {prop} is not a constructor.")
1144 redef class ASendExpr
1145 special ASuperInitCall
1146 # Name of the invoked property
1147 meth name
: Symbol is abstract
1149 # Raw arguments used (withour star transformation)
1150 meth raw_arguments
: Array[PExpr] is abstract
1152 # Closure definitions
1153 meth closure_defs
: Array[PClosureDef] do return null
1155 redef meth after_typing
(v
)
1160 private meth do_all_typing
(v
: TypingVisitor)
1162 if not v
.check_expr
(n_expr
) then return
1163 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1164 if prop
== null then return
1166 if prop
.global
.is_init
then
1167 if not v
.local_property
.global
.is_init
then
1168 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1169 else if not n_expr
.is_self
then
1170 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1172 register_super_init_call
(v
, prop
)
1176 _stype
= return_type
1180 class ASendReassignExpr
1182 special AReassignFormExpr
1183 readable attr _read_prop
: MMMethod
1184 redef meth do_all_typing
(v
)
1186 if not v
.check_expr
(n_expr
) then return
1187 var raw_args
= raw_arguments
1188 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1189 if prop
== null then return
1190 if prop
.global
.is_init
then
1191 if not v
.local_property
.global
.is_init
then
1192 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1193 else if not n_expr
.is_self
then
1194 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1197 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1198 if not n_expr
.is_self
then t
= t
.not_for_self
1200 do_lvalue_typing
(v
, t
)
1203 var old_args
= arguments
1204 raw_args
.add
(n_value
)
1206 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1207 if prop
== null then return
1208 if prop
.global
.is_init
then
1209 if not v
.local_property
.global
.is_init
then
1210 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1211 else if not n_expr
.is_self
then
1212 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1216 _arguments
= old_args
# FIXME: What if star parameters do not match betwen the two methods?
1220 redef class ABinopExpr
1221 redef meth raw_arguments
do return [n_expr2
]
1224 redef meth name
do return once
"==".to_symbol
1227 redef meth name
do return once
"!=".to_symbol
1230 redef meth name
do return once
"<".to_symbol
1233 redef meth name
do return once
"<=".to_symbol
1236 redef meth name
do return once
">".to_symbol
1239 redef meth name
do return once
">=".to_symbol
1241 redef class APlusExpr
1242 redef meth name
do return once
"+".to_symbol
1244 redef class AMinusExpr
1245 redef meth name
do return once
"-".to_symbol
1247 redef class AStarshipExpr
1248 redef meth name
do return once
"<=>".to_symbol
1250 redef class AStarExpr
1251 redef meth name
do return once
"*".to_symbol
1253 redef class ASlashExpr
1254 redef meth name
do return once
"/".to_symbol
1256 redef class APercentExpr
1257 redef meth name
do return once
"%".to_symbol
1260 redef class AUminusExpr
1261 redef meth name
do return once
"unary -".to_symbol
1262 redef meth raw_arguments
do return null
1265 redef class ACallFormExpr
1266 redef meth after_typing
(v
)
1268 if n_expr
!= null and n_expr
.is_implicit_self
then
1269 var name
= n_id
.to_symbol
1270 var variable
= v
.variable_ctx
[name
]
1271 if variable
!= null then
1272 if variable
isa ClosureVariable then
1273 var n
= new AClosureCallExpr(n_id
, n_args
, n_closure_defs
)
1275 n
.variable
= variable
1279 if not n_args
.is_empty
then
1280 v
.error
(self, "Error: {name} is variable, not a function.")
1282 var vform
= variable_create
(variable
)
1283 vform
.variable
= variable
1285 vform
.after_typing
(v
)
1294 redef meth closure_defs
1296 if n_closure_defs
== null or n_closure_defs
.is_empty
then
1299 return n_closure_defs
.to_a
1303 # Create a variable acces corresponding to the call form
1304 meth variable_create
(variable
: Variable): AVarFormExpr is abstract
1307 redef class ACallExpr
1308 redef meth variable_create
(variable
)
1310 return new AVarExpr.init_avarexpr
(n_id
)
1313 redef meth name
do return n_id
.to_symbol
1314 redef meth raw_arguments
do return n_args
.to_a
1317 redef class ACallAssignExpr
1318 redef meth variable_create
(variable
)
1320 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1323 redef meth name
do return (n_id
.text
+ "=").to_symbol
1324 redef meth raw_arguments
do
1325 var res
= n_args
.to_a
1331 redef class ACallReassignExpr
1332 special ASendReassignExpr
1333 redef meth variable_create
(variable
)
1335 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1338 redef meth name
do return n_id
.to_symbol
1339 redef meth raw_arguments
do return n_args
.to_a
1342 redef class ABraExpr
1343 redef meth name
do return once
"[]".to_symbol
1344 redef meth raw_arguments
do return n_args
.to_a
1347 redef class ABraAssignExpr
1348 redef meth name
do return once
"[]=".to_symbol
1349 redef meth raw_arguments
do
1350 var res
= n_args
.to_a
1356 redef class ABraReassignExpr
1357 special ASendReassignExpr
1358 redef meth name
do return once
"[]".to_symbol
1359 redef meth raw_arguments
do return n_args
.to_a
1362 redef class AInitExpr
1363 redef meth name
do return once
"init".to_symbol
1364 redef meth raw_arguments
do return n_args
.to_a
1367 redef class AClosureCallExpr
1368 redef meth after_typing
(v
)
1371 var sig
= va
.closure
.signature
1372 var args
= process_signature
(v
, sig
, n_id
.to_symbol
, n_args
.to_a
)
1373 if closure_defs
!= null then
1374 process_closures
(v
, sig
, n_id
.to_symbol
, closure_defs
)
1376 if args
== null then return
1378 _prop_signature
= sig
1380 _stype
= sig
.return_type
1384 redef class PClosureDef
1385 # The corresponding escapable object
1386 readable attr _escapable
: EscapableBlock
1388 attr _accept_typing2
: Bool
1389 redef meth accept_typing
(v
)
1391 # Typing is deferred, wait accept_typing2(v)
1392 if _accept_typing2
then super
1395 private meth accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure) is abstract
1398 redef class AClosureDef
1399 redef meth accept_typing2
(v
, esc
)
1403 var sig
= esc
.closure
.signature
1404 if sig
.arity
!= n_id
.length
then
1405 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1409 closure
= esc
.closure
1411 v
.variable_ctx
= v
.variable_ctx
.sub
1412 variables
= new Array[AutoVariable]
1413 for i
in [0..n_id
.length
[ do
1414 var va
= new AutoVariable(n_id
[i
].to_symbol
, self)
1417 v
.variable_ctx
.add
(va
)
1420 _accept_typing2
= true
1425 redef class AIsaExpr
1426 redef meth after_typing
(v
)
1428 var variable
= n_expr
.its_variable
1429 if variable
!= null then
1430 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(variable
, n_type
.stype
)
1432 _stype
= v
.type_bool
1436 redef class AAsCastExpr
1437 redef meth after_typing
(v
)
1439 v
.check_expr
(n_expr
)
1440 _stype
= n_type
.stype
1444 redef class AProxyExpr
1445 redef meth after_typing
(v
)
1447 _stype
= n_expr
.stype