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 = true # FIXME: Switch to false once subclasses are adapted
349 redef meth is_statement
: Bool do return _stype
== null
356 # Is the expression the implicit receiver
357 meth is_implicit_self
: Bool do return false
359 # Is the expression the current receiver (implicit or explicit)
360 meth is_self
: Bool do return false
362 # The variable accessed is any
363 meth its_variable
: Variable do return null
365 # The variable type information if current boolean expression is true
366 readable private attr _if_true_variable_ctx
: VariableContext
369 redef class AVardeclExpr
370 redef meth after_typing
(v
)
372 var va
= new VarVariable(n_id
.to_symbol
, self)
374 v
.variable_ctx
.add
(va
)
376 if n_type
!= null then
377 va
.stype
= n_type
.stype
378 if n_expr
!= null then
379 v
.check_conform_expr
(n_expr
, va
.stype
)
383 va
.stype
= n_expr
.stype
388 redef class ABlockExpr
389 redef meth accept_typing
(v
)
391 var old_var_ctx
= v
.variable_ctx
392 v
.variable_ctx
= v
.variable_ctx
.sub
396 v
.variable_ctx
= old_var_ctx
400 redef class AReturnExpr
401 redef meth after_typing
(v
)
403 var t
= v
.local_property
.signature
.return_type
404 if n_expr
== null and t
!= null then
405 v
.error
(self, "Error: Return without value in a function.")
406 else if n_expr
!= null and t
== null then
407 v
.error
(self, "Error: Return with value in a procedure.")
408 else if n_expr
!= null and t
!= null then
409 v
.check_conform_expr
(n_expr
, t
)
414 redef class AContinueExpr
415 redef meth after_typing
(v
)
417 var esc
= compute_escapable_block
(v
.escapable_ctx
)
418 if esc
== null then return
420 if esc
.is_break_block
then
421 v
.error
(self, "Error: 'continue' forbiden in break blocks.")
425 var t
= esc
.continue_stype
426 if n_expr
== null and t
!= null then
427 v
.error
(self, "Error: continue with a value required in this block.")
428 else if n_expr
!= null and t
== null then
429 v
.error
(self, "Error: continue without value required in this block.")
430 else if n_expr
!= null and t
!= null then
431 v
.check_conform_expr
(n_expr
, t
)
436 redef class ABreakExpr
437 redef meth after_typing
(v
)
439 var esc
= compute_escapable_block
(v
.escapable_ctx
)
440 if esc
== null then return
442 var bl
= esc
.break_list
443 if n_expr
== null and bl
!= null then
444 v
.error
(self, "Error: break with a value required in this block.")
445 else if n_expr
!= null and bl
== null then
446 v
.error
(self, "Error: break without value required in this block.")
447 else if n_expr
!= null and bl
!= null then
448 # Typing check can only be done later
455 redef meth accept_typing
(v
)
457 var old_var_ctx
= v
.variable_ctx
459 v
.check_conform_expr
(n_expr
, v
.type_bool
)
461 v
.use_if_true_variable_ctx
(n_expr
)
464 # Restore variable ctx
465 v
.variable_ctx
= old_var_ctx
467 if n_else
!= null then
469 v
.variable_ctx
= old_var_ctx
474 redef class AWhileExpr
475 # The corresponding escapable block
476 readable attr _escapable
: EscapableBlock
478 redef meth accept_typing
(v
)
480 _escapable
= new EscapableBlock(self)
481 v
.escapable_ctx
.push
(_escapable
)
485 v
.check_conform_expr
(n_expr
, v
.type_bool
)
491 # The corresponding escapable block
492 readable attr _escapable
: EscapableBlock
494 readable attr _meth_iterator
: MMMethod
495 readable attr _meth_is_ok
: MMMethod
496 readable attr _meth_item
: MMMethod
497 readable attr _meth_next
: MMMethod
498 redef meth accept_typing
(v
)
500 _escapable
= new EscapableBlock(self)
501 v
.escapable_ctx
.push
(_escapable
)
503 v
.variable_ctx
= v
.variable_ctx
.sub
504 var va
= new AutoVariable(n_id
.to_symbol
, self)
506 v
.variable_ctx
.add
(va
)
510 var expr_type
= n_expr
.stype
511 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then
514 _meth_iterator
= expr_type
.local_class
.select_method
(once
("iterator".to_symbol
))
515 if _meth_iterator
== null then
516 v
.error
(self, "Error: Collection MUST have an iterate method")
519 var iter_type
= _meth_iterator
.signature_for
(expr_type
).return_type
520 _meth_is_ok
= iter_type
.local_class
.select_method
(once
("is_ok".to_symbol
))
521 if _meth_is_ok
== null then
522 v
.error
(self, "Error: {iter_type} MUST have an is_ok method")
525 _meth_item
= iter_type
.local_class
.select_method
(once
("item".to_symbol
))
526 if _meth_item
== null then
527 v
.error
(self, "Error: {iter_type} MUST have an item method")
530 _meth_next
= iter_type
.local_class
.select_method
(once
("next".to_symbol
))
531 if _meth_next
== null then
532 v
.error
(self, "Error: {iter_type} MUST have a next method")
535 var t
= _meth_item
.signature_for
(iter_type
).return_type
536 if not n_expr
.is_self
then t
= t
.not_for_self
539 if n_block
!= null then v
.visit
(n_block
)
542 var varctx
= v
.variable_ctx
543 assert varctx
isa SubVariableContext
544 v
.variable_ctx
= varctx
.prev
550 redef class AAssertExpr
551 redef meth after_typing
(v
)
553 v
.check_conform_expr
(n_expr
, v
.type_bool
)
554 v
.use_if_true_variable_ctx
(n_expr
)
559 redef meth its_variable
do return variable
561 redef meth after_typing
(v
)
563 _stype
= v
.variable_ctx
.stype
(variable
)
567 redef class AVarAssignExpr
568 redef meth after_typing
(v
)
570 var t
= v
.variable_ctx
.stype
(variable
)
571 v
.check_conform_expr
(n_value
, t
)
575 redef class AReassignFormExpr
576 # Compute and check method used through the reassigment operator
577 private meth do_lvalue_typing
(v
: TypingVisitor, type_lvalue
: MMType)
579 if type_lvalue
== null then
582 var name
= n_assign_op
.method_name
583 var lc
= type_lvalue
.local_class
584 if not lc
.has_global_property_by_name
(name
) then
585 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
588 var prop
= lc
.select_method
(name
)
589 prop
.global
.check_visibility
(v
, self, v
.module, false)
590 var psig
= prop
.signature_for
(type_lvalue
)
591 _assign_method
= prop
592 v
.check_conform_expr
(n_value
, psig
[0].not_for_self
)
593 v
.check_conform
(self, psig
.return_type
.not_for_self
, n_value
.stype
)
596 # Method used through the reassigment operator (once computed)
597 readable attr _assign_method
: MMMethod
600 redef class AVarReassignExpr
601 redef meth after_typing
(v
)
603 var t
= v
.variable_ctx
.stype
(variable
)
604 do_lvalue_typing
(v
, t
)
608 redef class PAssignOp
609 meth method_name
: Symbol is abstract
611 redef class APlusAssignOp
612 redef meth method_name
do return once
"+".to_symbol
614 redef class AMinusAssignOp
615 redef meth method_name
do return once
"-".to_symbol
618 redef class ASelfExpr
619 redef meth its_variable
do return variable
621 redef meth after_typing
(v
)
623 variable
= v
.self_var
624 _stype
= v
.variable_ctx
.stype
(variable
)
627 redef meth is_self
do return true
630 redef class AImplicitSelfExpr
631 redef meth is_implicit_self
do return true
634 redef class AIfexprExpr
635 redef meth accept_typing
(v
)
637 var old_var_ctx
= v
.variable_ctx
640 v
.use_if_true_variable_ctx
(n_expr
)
642 v
.variable_ctx
= old_var_ctx
645 v
.check_conform_expr
(n_expr
, v
.type_bool
)
647 _stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
651 redef class ABoolExpr
652 redef meth after_typing
(v
)
659 redef meth after_typing
(v
)
661 v
.check_conform_expr
(n_expr
, v
.type_bool
)
662 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
668 redef meth accept_typing
(v
)
670 var old_var_ctx
= v
.variable_ctx
673 v
.use_if_true_variable_ctx
(n_expr
)
676 if n_expr2
.if_true_variable_ctx
!= null then
677 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
679 _if_true_variable_ctx
= v
.variable_ctx
682 v
.variable_ctx
= old_var_ctx
684 v
.check_conform_expr
(n_expr
, v
.type_bool
)
685 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
691 redef meth after_typing
(v
)
693 v
.check_conform_expr
(n_expr
, v
.type_bool
)
699 redef meth after_typing
(v
)
706 redef class AFloatExpr
707 redef meth after_typing
(v
)
709 _stype
= v
.type_float
713 redef class ACharExpr
714 redef meth after_typing
(v
)
720 redef class AStringFormExpr
721 readable attr _meth_with_native
: MMMethod
722 redef meth after_typing
(v
)
724 _stype
= v
.type_string
725 _meth_with_native
= _stype
.local_class
.select_method
(once
"with_native".to_symbol
)
726 if _meth_with_native
== null then v
.error
(self, "{_stype} MUST have a with_native method.")
730 redef class ASuperstringExpr
731 readable attr _meth_with_capacity
: MMMethod
732 readable attr _meth_add
: MMMethod
733 readable attr _meth_to_s
: MMMethod
734 readable attr _atype
: MMType
735 redef meth after_typing
(v
)
737 _stype
= v
.type_string
738 _atype
= v
.type_array
(_stype
)
739 _meth_with_capacity
= _atype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
740 if _meth_with_capacity
== null then v
.error
(self, "{_atype} MUST have a with_capacity method.")
741 _meth_add
= _atype
.local_class
.select_method
(once
"add".to_symbol
)
742 if _meth_add
== null then v
.error
(self, "{_atype} MUST have an add method.")
743 _meth_to_s
= v
.type_object
.local_class
.select_method
(once
"to_s".to_symbol
)
744 if _meth_to_s
== null then v
.error
(self, "Object MUST have a to_s method.")
748 redef class ANullExpr
749 redef meth after_typing
(v
)
755 redef class AArrayExpr
756 readable attr _meth_with_capacity
: MMMethod
757 readable attr _meth_add
: MMMethod
759 redef meth after_typing
(v
)
761 var stype
= v
.check_conform_multiexpr
(null, n_exprs
)
762 if stype
== null then return
766 private meth do_typing
(v
: TypingVisitor, element_type
: MMType)
768 _stype
= v
.type_array
(element_type
)
770 _meth_with_capacity
= _stype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
771 if _meth_with_capacity
== null then v
.error
(self, "{_stype} MUST have a with_capacity method.")
772 _meth_add
= _stype
.local_class
.select_method
(once
"add".to_symbol
)
773 if _meth_add
== null then v
.error
(self, "{_stype} MUST have an add method.")
777 redef class ARangeExpr
778 readable attr _meth_init
: MMMethod
779 redef meth after_typing
(v
)
781 var ntype
= n_expr
.stype
782 var ntype2
= n_expr2
.stype
783 if ntype
== null or ntype
== null then
786 if ntype
< ntype2
then
788 else if not ntype2
< ntype
then
789 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
792 var dtype
= v
.type_discrete
793 v
.check_conform_expr
(n_expr
, dtype
)
794 v
.check_conform_expr
(n_expr2
, dtype
)
795 _stype
= v
.type_range
(ntype
)
799 redef class ACrangeExpr
800 redef meth after_typing
(v
)
803 _meth_init
= stype
.local_class
.select_method
(once
"init".to_symbol
)
806 redef class AOrangeExpr
807 redef meth after_typing
(v
)
810 _meth_init
= stype
.local_class
.select_method
(once
"without_last".to_symbol
)
815 redef class ASuperExpr
816 special ASuperInitCall
817 # readable attr _prop: MMSrcMethod
818 readable attr _init_in_superclass
: MMMethod
819 redef meth after_typing
(v
)
821 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
822 if not precs
.is_empty
then
823 v
.local_property
.need_super
= true
824 else if v
.local_property
.global
.is_init
then
825 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
826 for p
in base_precs
do
827 if not p
.global
.is_init
then
828 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
830 precs
.add
(v
.local_class
[p
.global
])
833 if precs
.is_empty
then
834 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
836 else if precs
.length
> 1 then
837 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
840 var p
= base_precs
.first
841 assert p
isa MMMethod
842 _init_in_superclass
= p
843 register_super_init_call
(v
, p
)
844 if n_args
.length
> 0 then
845 var signature
= get_signature
(v
, v
.self_var
.stype
, p
, true)
846 _arguments
= process_signature
(v
, signature
, p
.name
, n_args
.to_a
)
849 v
.error
(self, "Error: No super method to call for {v.local_property}.")
853 if precs
.first
.signature_for
(v
.self_var
.stype
).return_type
!= null then
854 var stypes
= new Array[MMType]
855 var stype
: MMType = null
857 assert prop
isa MMMethod
858 var t
= prop
.signature_for
(v
.self_var
.stype
).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
860 if stype
== null or stype
< t
then
865 v
.check_conform
(self, t
, stype
)
869 var p
= v
.local_property
870 assert p
isa MMSrcMethod
875 redef class AAttrFormExpr
877 readable attr _prop
: MMAttribute
879 # Attribute type of the acceded attribute
880 readable attr _attr_type
: MMType
882 # Compute the attribute accessed
883 private meth do_typing
(v
: TypingVisitor)
885 if not v
.check_expr
(n_expr
) then return
886 var type_recv
= n_expr
.stype
887 var name
= n_id
.to_symbol
888 var lc
= type_recv
.local_class
889 if not lc
.has_global_property_by_name
(name
) then
890 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
893 var prop
= lc
.select_attribute
(name
)
894 if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
895 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
898 var at
= prop
.signature_for
(type_recv
).return_type
899 if not n_expr
.is_self
then at
= at
.not_for_self
904 redef class AAttrExpr
905 redef meth after_typing
(v
)
915 redef class AAttrAssignExpr
916 redef meth after_typing
(v
)
922 v
.check_conform_expr
(n_value
, attr_type
)
926 redef class AAttrReassignExpr
927 redef meth after_typing
(v
)
933 do_lvalue_typing
(v
, attr_type
)
937 class AAbsAbsSendExpr
939 # The signature of the called property
940 readable attr _prop_signature
: MMSignature
942 # The real arguments used (after star transformation) (once computed)
943 readable attr _arguments
: Array[PExpr]
945 # Check the conformity of a set of arguments `raw_args' to a signature.
946 private meth process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: Array[PExpr]): Array[PExpr]
948 var par_vararg
= psig
.vararg_rank
949 var par_arity
= psig
.arity
951 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
952 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
953 v
.error
(self, "Error: '{name}' arity missmatch.")
957 var args
= new Array[PExpr]
958 for par_idx
in [0..par_arity
[ do
960 var par_type
= psig
[par_idx
]
961 if par_idx
== par_vararg
then
962 var star
= new Array[PExpr]
963 for i
in [0..(raw_arity-par_arity
)] do
964 a
= raw_args
[arg_idx
]
965 v
.check_conform_expr
(a
, par_type
)
967 arg_idx
= arg_idx
+ 1
969 var aa
= new AArrayExpr.init_aarrayexpr
(star
)
970 aa
.do_typing
(v
, par_type
)
973 a
= raw_args
[arg_idx
]
974 v
.check_conform_expr
(a
, par_type
)
975 arg_idx
= arg_idx
+ 1
982 # Check the conformity of a set of defined closures
983 private meth process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: Array[PClosureDef]): MMType
985 var t
= psig
.return_type
986 var cs
= psig
.closures
# Declared closures
989 if not c
.is_optional
then min_arity
+= 1
992 if cs
.length
== 0 then
993 v
.error
(self, "Error: {name} does not require blocks.")
994 else if cd
.length
> cs
.length
or cd
.length
< min_arity
then
995 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
997 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
998 var break_list
: Array[ABreakExpr] = null
999 if t
!= null then break_list
= new Array[ABreakExpr]
1001 # Process each closure definition
1002 for i
in [0..cd
.length
[ do
1005 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1006 v
.escapable_ctx
.push
(esc
)
1007 cdi
.accept_typing2
(v
, esc
)
1011 # Check break type conformity
1012 if break_list
!= null then
1013 t
= v
.check_conform_multiexpr
(t
, break_list
)
1016 else if min_arity
!= 0 then
1017 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1024 special AAbsAbsSendExpr
1025 # Compute the called global property
1026 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])
1028 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
1029 if prop
== null then return
1030 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
1031 if sig
== null then return
1032 var args
= process_signature
(v
, sig
, prop
.name
, raw_args
)
1033 if args
== null then return
1034 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
1036 _prop_signature
= sig
1038 _return_type
= rtype
1041 private meth get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): MMMethod
1043 if type_recv
== null then return null
1044 var lc
= type_recv
.local_class
1045 var prop
: MMMethod = null
1046 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
1047 if prop
== null and v
.local_property
.global
.is_init
then
1048 var props
= type_recv
.local_class
.super_methods_named
(name
)
1049 if props
.length
> 1 then
1050 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1052 else if props
.length
== 1 then
1053 var p
= type_recv
.local_class
[props
.first
.global
]
1054 assert p
isa MMMethod
1059 if prop
== null then
1060 if is_implicit_self
then
1061 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1063 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1070 # Get the signature for a local property and a receiver
1071 private meth get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
1073 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
1074 var psig
= prop
.signature_for
(type_recv
)
1075 if not recv_is_self
then psig
= psig
.not_for_self
1079 # The invoked method (once computed)
1080 readable attr _prop
: MMMethod
1082 # The return type (if any) (once computed)
1083 readable attr _return_type
: MMType
1086 # A possible call of constructor in a super class
1087 # Could be an explicit call or with the 'super' keyword
1088 class ASuperInitCall
1089 special AAbsSendExpr
1090 private meth register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1092 if parent
!= v
.top_block
and self != v
.top_block
then
1093 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1095 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
1096 var prev_class
: MMLocalClass = null
1097 if not v
.explicit_super_init_calls
.is_empty
then
1098 prev_class
= v
.explicit_super_init_calls
.last
.global
.intro
.local_class
1100 var order
= v
.local_class
.cshe
.reverse_linear_extension
1101 if cla
== v
.local_class
then
1102 v
.explicit_other_init_call
= true
1103 else if not order
.has
(cla
) then
1104 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1105 else if cla
== prev_class
then
1106 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1108 var last_is_found
= prev_class
== null
1110 if c
== prev_class
then
1111 last_is_found
= true
1112 else if c
== cla
then
1113 if not last_is_found
then
1114 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1116 v
.explicit_super_init_calls
.add
(property
)
1125 redef class ANewExpr
1126 special AAbsSendExpr
1127 redef meth after_typing
(v
)
1129 var t
= n_type
.stype
1130 if t
== null then return
1131 if t
.local_class
.global
.is_abstract
then
1132 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1136 if n_id
== null then
1137 name
= once
"init".to_symbol
1139 name
= n_id
.to_symbol
1142 do_typing
(v
, t
, false, false, name
, n_args
.to_a
, null)
1143 if prop
== null then return
1145 if not prop
.global
.is_init
then
1146 v
.error
(self, "Error: {prop} is not a constructor.")
1153 redef class ASendExpr
1154 special ASuperInitCall
1155 # Name of the invoked property
1156 meth name
: Symbol is abstract
1158 # Raw arguments used (withour star transformation)
1159 meth raw_arguments
: Array[PExpr] is abstract
1161 # Closure definitions
1162 meth closure_defs
: Array[PClosureDef] do return null
1164 redef meth after_typing
(v
)
1169 private meth do_all_typing
(v
: TypingVisitor)
1171 if not v
.check_expr
(n_expr
) then return
1172 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1173 if prop
== null then return
1175 if prop
.global
.is_init
then
1176 if not v
.local_property
.global
.is_init
then
1177 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1178 else if not n_expr
.is_self
then
1179 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1181 register_super_init_call
(v
, prop
)
1185 _stype
= return_type
1189 class ASendReassignExpr
1191 special AReassignFormExpr
1192 readable attr _read_prop
: MMMethod
1193 redef meth do_all_typing
(v
)
1195 if not v
.check_expr
(n_expr
) then return
1196 var raw_args
= raw_arguments
1197 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1198 if prop
== null then return
1199 if prop
.global
.is_init
then
1200 if not v
.local_property
.global
.is_init
then
1201 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1202 else if not n_expr
.is_self
then
1203 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1206 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1207 if not n_expr
.is_self
then t
= t
.not_for_self
1209 do_lvalue_typing
(v
, t
)
1212 var old_args
= arguments
1213 raw_args
.add
(n_value
)
1215 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1216 if prop
== null then return
1217 if prop
.global
.is_init
then
1218 if not v
.local_property
.global
.is_init
then
1219 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1220 else if not n_expr
.is_self
then
1221 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1225 _arguments
= old_args
# FIXME: What if star parameters do not match betwen the two methods?
1229 redef class ABinopExpr
1230 redef meth raw_arguments
do return [n_expr2
]
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
1242 redef meth name
do return once
"<=".to_symbol
1245 redef meth name
do return once
">".to_symbol
1248 redef meth name
do return once
">=".to_symbol
1250 redef class APlusExpr
1251 redef meth name
do return once
"+".to_symbol
1253 redef class AMinusExpr
1254 redef meth name
do return once
"-".to_symbol
1256 redef class AStarshipExpr
1257 redef meth name
do return once
"<=>".to_symbol
1259 redef class AStarExpr
1260 redef meth name
do return once
"*".to_symbol
1262 redef class ASlashExpr
1263 redef meth name
do return once
"/".to_symbol
1265 redef class APercentExpr
1266 redef meth name
do return once
"%".to_symbol
1269 redef class AUminusExpr
1270 redef meth name
do return once
"unary -".to_symbol
1271 redef meth raw_arguments
do return null
1274 redef class ACallFormExpr
1275 redef meth after_typing
(v
)
1277 if n_expr
!= null and n_expr
.is_implicit_self
then
1278 var name
= n_id
.to_symbol
1279 var variable
= v
.variable_ctx
[name
]
1280 if variable
!= null then
1281 if variable
isa ClosureVariable then
1282 var n
= new AClosureCallExpr.init_aclosurecallexpr
(n_id
, n_args
, n_closure_defs
)
1284 n
.variable
= variable
1288 if not n_args
.is_empty
then
1289 v
.error
(self, "Error: {name} is variable, not a function.")
1291 var vform
= variable_create
(variable
)
1292 vform
.variable
= variable
1294 vform
.after_typing
(v
)
1303 redef meth closure_defs
1305 if n_closure_defs
== null or n_closure_defs
.is_empty
then
1308 return n_closure_defs
.to_a
1312 # Create a variable acces corresponding to the call form
1313 meth variable_create
(variable
: Variable): AVarFormExpr is abstract
1316 redef class ACallExpr
1317 redef meth variable_create
(variable
)
1319 return new AVarExpr.init_avarexpr
(n_id
)
1322 redef meth name
do return n_id
.to_symbol
1323 redef meth raw_arguments
do return n_args
.to_a
1326 redef class ACallAssignExpr
1327 redef meth variable_create
(variable
)
1329 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1332 redef meth name
do return (n_id
.text
+ "=").to_symbol
1333 redef meth raw_arguments
do
1334 var res
= n_args
.to_a
1340 redef class ACallReassignExpr
1341 special ASendReassignExpr
1342 redef meth variable_create
(variable
)
1344 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1347 redef meth name
do return n_id
.to_symbol
1348 redef meth raw_arguments
do return n_args
.to_a
1351 redef class ABraExpr
1352 redef meth name
do return once
"[]".to_symbol
1353 redef meth raw_arguments
do return n_args
.to_a
1356 redef class ABraAssignExpr
1357 redef meth name
do return once
"[]=".to_symbol
1358 redef meth raw_arguments
do
1359 var res
= n_args
.to_a
1365 redef class ABraReassignExpr
1366 special ASendReassignExpr
1367 redef meth name
do return once
"[]".to_symbol
1368 redef meth raw_arguments
do return n_args
.to_a
1371 redef class AInitExpr
1372 redef meth name
do return once
"init".to_symbol
1373 redef meth raw_arguments
do return n_args
.to_a
1376 redef class AClosureCallExpr
1377 special AAbsAbsSendExpr
1378 redef meth after_typing
(v
)
1381 var sig
= va
.closure
.signature
1382 var args
= process_signature
(v
, sig
, n_id
.to_symbol
, n_args
.to_a
)
1383 if not n_closure_defs
.is_empty
then
1384 process_closures
(v
, sig
, n_id
.to_symbol
, n_closure_defs
.to_a
)
1386 if args
== null then return
1387 _prop_signature
= sig
1389 _stype
= sig
.return_type
1393 redef class PClosureDef
1394 # The corresponding escapable object
1395 readable attr _escapable
: EscapableBlock
1397 attr _accept_typing2
: Bool
1398 redef meth accept_typing
(v
)
1400 # Typing is deferred, wait accept_typing2(v)
1401 if _accept_typing2
then super
1404 private meth accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure) is abstract
1407 redef class AClosureDef
1408 redef meth accept_typing2
(v
, esc
)
1412 var sig
= esc
.closure
.signature
1413 if sig
.arity
!= n_id
.length
then
1414 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1418 closure
= esc
.closure
1420 v
.variable_ctx
= v
.variable_ctx
.sub
1421 variables
= new Array[AutoVariable]
1422 for i
in [0..n_id
.length
[ do
1423 var va
= new AutoVariable(n_id
[i
].to_symbol
, self)
1426 v
.variable_ctx
.add
(va
)
1429 _accept_typing2
= true
1434 redef class AIsaExpr
1435 redef meth after_typing
(v
)
1437 var variable
= n_expr
.its_variable
1438 if variable
!= null then
1439 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(variable
, n_type
.stype
)
1441 _stype
= v
.type_bool
1445 redef class AAsCastExpr
1446 redef meth after_typing
(v
)
1448 v
.check_expr
(n_expr
)
1449 _stype
= n_type
.stype
1453 redef class AProxyExpr
1454 redef meth after_typing
(v
)
1456 _stype
= n_expr
.stype