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 # Number of nested once
73 readable writable attr _once_count
: Int = 0
75 init(tc
, module) do super
77 private meth get_default_constructor_for
(n
: PNode, c
: MMLocalClass, prop
: MMSrcMethod): MMMethod
80 #var prop = v.local_property
81 #assert prop isa MMMethod
82 var candidates
= new Array[MMMethod]
83 var false_candidates
= new Array[MMMethod]
84 var parity
= prop
.signature
.arity
85 for g
in c
.global_properties
do
86 if not g
.is_init_for
(c
) then continue
88 var gps
= gp
.signature_for
(c
.get_type
)
89 assert gp
isa MMSrcMethod
90 var garity
= gps
.arity
91 if prop
!= null and gp
.name
== prop
.name
then
92 if garity
== 0 or (parity
== garity
and prop
.signature
< gps
) then
95 false_candidates
.add
(gp
)
97 else if garity
== 0 and gp
.name
== once
("init".to_symbol
) then
99 false_candidates
.add
(gp
)
101 false_candidates
.add
(gp
)
104 if candidates
.length
== 1 then
105 return candidates
.first
106 else if candidates
.length
> 0 then
107 var a
= new Array[String]
108 for p
in candidates
do
109 a
.add
("{p.full_name}{p.signature}")
111 v
.error
(n
, "Error: Conflicting default constructor to call for {c}: {a.join(", ")}.")
113 else if false_candidates
.length
> 0 then
114 var a
= new Array[String]
115 for p
in false_candidates
do
116 a
.add
("{p.full_name}{p.signature}")
118 v
.error
(n
, "Error: there is no available compatible constrctor in {c}. Discarded candidates are {a.join(", ")}.")
121 v
.error
(n
, "Error: there is no available compatible constrctor in {c}.")
127 # Associate symbols to variable and variables to type
129 abstract class VariableContext
130 # Look for the variable from its name
131 # Return null if nothing found
132 meth
[](s
: Symbol): Variable
134 if _dico
.has_key
(s
) then
141 # Register a new variable with its name
142 meth add
(v
: Variable)
145 _all_variables
.add
(v
)
148 meth mark_is_set
(v
: Variable)
150 _set_variables
.add
(v
)
153 meth check_is_set
(n
: PNode, v
: Variable)
155 if v
.must_be_set
and not is_set
(v
) then
156 _visitor
.error
(n
, "Error: variable '{v}' is possibly unset.")
159 print
" {x.node.locate}: {x._set_variables.join(", ")} ; {x._dico.join(", ")}"
161 if x0
isa SubVariableContext then
170 # The effective static type of a given variable
171 # May be different from the declaration static type
172 meth stype
(v
: Variable): MMType
177 # Variables by name (in the current context only)
178 attr _dico
: Map[Symbol, Variable]
180 # All variables in all contextes
181 attr _all_variables
: Set[Variable]
183 # Build a new VariableContext
184 meth sub
(node
: PNode): SubVariableContext
186 return new SubVariableContext.with_prev
(self, node
)
189 # Build a nested VariableContext with new variable information
190 meth sub_with
(node
: PNode, v
: Variable, t
: MMType): SubVariableContext
192 return new CastVariableContext.with_prev
(self, node
, v
, t
)
195 # The visitor of the context (used to display error)
196 attr _visitor
: AbsSyntaxVisitor
198 # The syntax node that introduced the context
199 readable attr _node
: PNode
201 init(visitor
: AbsSyntaxVisitor, node
: PNode)
205 _dico
= new HashMap[Symbol, Variable]
208 # Is a control flow break met? (return, break, continue)
209 readable writable attr _unreash
: Bool = false
211 # Is a control flow already broken?
212 # Used to avoid repeating the same error message
213 readable writable attr _already_unreash
: Bool = false
215 # Set of variable that are set (assigned)
216 readable attr _set_variables
: HashSet[Variable] = new HashSet[Variable]
219 meth is_set
(v
: Variable): Bool
221 return _set_variables
.has
(v
)
224 # Merge back one flow context information
225 meth merge
(ctx
: VariableContext)
229 if ctx
.already_unreash
then already_unreash
= true
232 for v
in _all_variables
do
233 if not is_set
(v
) and ctx
.is_set
(v
) then
239 # Merge back two alternative flow context informations
240 meth merge2
(ctx1
, ctx2
: VariableContext)
244 else if ctx2
.unreash
then
247 for v
in _all_variables
do
248 if not is_set
(v
) and ctx1
.is_set
(v
) and ctx2
.is_set
(v
) then
255 class RootVariableContext
256 special VariableContext
257 init(visitor
: AbsSyntaxVisitor, node
: PNode)
260 _all_variables
= new HashSet[Variable]
264 class SubVariableContext
265 special VariableContext
266 readable attr _prev
: VariableContext
270 if _dico
.has_key
(s
) then
282 init with_prev
(p
: VariableContext, node
: PNode)
284 init(p
._visitor
, node
)
286 _all_variables
= p
._all_variables
291 return _set_variables
.has
(v
) or _prev
.is_set
(v
)
295 class CastVariableContext
296 special SubVariableContext
297 attr _variable
: Variable
298 attr _var_type
: MMType
302 if _variable
== v
then
308 init with_prev
(p
: VariableContext, node
: PNode, v
: Variable, t
: MMType)
317 # Is the variable must be set before being used ?
318 meth must_be_set
: Bool do return false
321 redef class VarVariable
322 redef meth must_be_set
do return true
326 ###############################################################################
329 private meth accept_typing
(v
: TypingVisitor)
331 accept_abs_syntax_visitor
(v
)
334 private meth after_typing
(v
: TypingVisitor) do end
337 redef class PClassdef
338 redef meth accept_typing
(v
)
340 v
.self_var
= new ParamVariable("self".to_symbol
, self)
341 v
.self_var
.stype
= local_class
.get_type
346 redef class AAttrPropdef
347 redef meth accept_typing
(v
)
350 if n_expr
!= null then
351 v
.check_conform_expr
(n_expr
, prop
.signature
.return_type
)
356 redef class AMethPropdef
357 redef readable attr _self_var
: ParamVariable
358 redef meth accept_typing
(v
)
360 v
.variable_ctx
= new RootVariableContext(v
, self)
361 _self_var
= v
.self_var
366 redef class AConcreteMethPropdef
367 redef meth accept_typing
(v
)
370 if v
.variable_ctx
.unreash
== false and method
.signature
.return_type
!= null then
371 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
376 redef class AConcreteInitPropdef
377 readable attr _super_init_calls
: Array[MMMethod] = new Array[MMMethod]
378 readable attr _explicit_super_init_calls
: Array[MMMethod] = new Array[MMMethod]
379 redef meth accept_typing
(v
)
381 v
.top_block
= n_block
382 v
.explicit_super_init_calls
= explicit_super_init_calls
383 v
.explicit_other_init_call
= false
385 if v
.explicit_other_init_call
or method
.global
.intro
!= method
then
389 var l
= explicit_super_init_calls
.length
390 var cur_m
: MMMethod = null
391 var cur_c
: MMLocalClass = null
393 cur_m
= explicit_super_init_calls
[i
]
394 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
397 while j
< v
.local_class
.cshe
.direct_greaters
.length
do
398 var c
= v
.local_class
.cshe
.direct_greaters
[j
]
399 if c
.global
.is_interface
or c
.global
.is_universal
or c
.global
.is_mixin
then
401 else if cur_c
!= null and (c
.cshe
<= cur_c
or cur_c
.global
.is_mixin
) then
402 if c
== cur_c
then j
+= 1
403 super_init_calls
.add
(cur_m
)
406 cur_m
= explicit_super_init_calls
[i
]
407 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
413 var p
= v
.get_default_constructor_for
(self, c
, method
)
415 super_init_calls
.add
(p
)
425 redef meth after_typing
(v
)
427 # TODO: why the test?
428 if v
.variable_ctx
!= null then
429 v
.variable_ctx
.add
(variable
)
434 redef class AClosureDecl
435 # The corresponding escapable object
436 readable attr _escapable
: EscapableBlock
438 redef meth accept_typing
(v
)
440 # Register the closure for ClosureCallExpr
441 v
.variable_ctx
.add
(variable
)
443 var old_var_ctx
= v
.variable_ctx
444 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
446 _escapable
= new EscapableClosure(self, variable
.closure
, null)
447 v
.escapable_ctx
.push
(_escapable
)
451 if n_expr
!= null then
452 if v
.variable_ctx
.unreash
== false then
453 if variable
.closure
.signature
.return_type
!= null then
454 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
455 else if variable
.closure
.is_break
then
456 v
.error
(self, "Control error: Reached end of break block (an 'abort' was expected).")
461 old_var_ctx
.merge
(v
.variable_ctx
)
462 v
.variable_ctx
= old_var_ctx
468 readable attr _stype
: MMType
469 redef meth after_typing
(v
)
471 _stype
= get_stype
(v
)
476 redef readable attr _is_typed
: Bool = false
477 redef meth is_statement
: Bool do return _stype
== null
481 print
"{locate}: not is_typed"
485 print
"{locate}: is_statement"
492 # Is the expression the implicit receiver
493 meth is_implicit_self
: Bool do return false
495 # Is the expression the current receiver (implicit or explicit)
496 meth is_self
: Bool do return false
498 # The variable accessed is any
499 meth its_variable
: Variable do return null
501 # The variable type information if current boolean expression is true
502 readable private attr _if_true_variable_ctx
: VariableContext
505 redef class AVardeclExpr
506 redef meth after_typing
(v
)
508 var va
= new VarVariable(n_id
.to_symbol
, self)
510 v
.variable_ctx
.add
(va
)
511 if n_expr
!= null then v
.variable_ctx
.mark_is_set
(va
)
513 if n_type
!= null then
514 va
.stype
= n_type
.stype
515 if n_expr
!= null then
516 v
.check_conform_expr
(n_expr
, va
.stype
)
519 if not v
.check_expr
(n_expr
) then return
520 va
.stype
= n_expr
.stype
526 redef class ABlockExpr
527 redef meth accept_typing
(v
)
529 var old_var_ctx
= v
.variable_ctx
530 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
533 if v
.variable_ctx
.unreash
and not v
.variable_ctx
.already_unreash
then
534 v
.variable_ctx
.already_unreash
= true
535 v
.warning
(e
, "Warning: unreachable statement.")
540 old_var_ctx
.merge
(v
.variable_ctx
)
541 v
.variable_ctx
= old_var_ctx
546 redef class AReturnExpr
547 redef meth after_typing
(v
)
549 v
.variable_ctx
.unreash
= true
550 var t
= v
.local_property
.signature
.return_type
551 if n_expr
== null and t
!= null then
552 v
.error
(self, "Error: Return without value in a function.")
553 else if n_expr
!= null and t
== null then
554 v
.error
(self, "Error: Return with value in a procedure.")
555 else if n_expr
!= null and t
!= null then
556 v
.check_conform_expr
(n_expr
, t
)
562 redef class AContinueExpr
563 redef meth after_typing
(v
)
565 v
.variable_ctx
.unreash
= true
566 var esc
= compute_escapable_block
(v
.escapable_ctx
)
567 if esc
== null then return
569 if esc
.is_break_block
then
570 v
.error
(self, "Error: 'continue' forbiden in break blocks.")
574 var t
= esc
.continue_stype
575 if n_expr
== null and t
!= null then
576 v
.error
(self, "Error: continue with a value required in this block.")
577 else if n_expr
!= null and t
== null then
578 v
.error
(self, "Error: continue without value required in this block.")
579 else if n_expr
!= null and t
!= null then
580 v
.check_conform_expr
(n_expr
, t
)
586 redef class ABreakExpr
587 redef meth after_typing
(v
)
589 v
.variable_ctx
.unreash
= true
590 var esc
= compute_escapable_block
(v
.escapable_ctx
)
591 if esc
== null then return
593 var bl
= esc
.break_list
594 if n_expr
== null and bl
!= null then
595 v
.error
(self, "Error: break with a value required in this block.")
596 else if n_expr
!= null and bl
== null then
597 v
.error
(self, "Error: break without value required in this block.")
598 else if n_expr
!= null and bl
!= null then
599 # Typing check can only be done later
606 redef class AAbortExpr
607 redef meth after_typing
(v
)
609 v
.variable_ctx
.unreash
= true
614 redef meth accept_typing
(v
)
616 var old_var_ctx
= v
.variable_ctx
618 v
.check_conform_expr
(n_expr
, v
.type_bool
)
620 v
.use_if_true_variable_ctx
(n_expr
)
621 v
.variable_ctx
= v
.variable_ctx
.sub
(n_then
)
625 if n_else
== null then
626 # Restore variable ctx since the 'then' block is optional
627 v
.variable_ctx
= old_var_ctx
629 # Remember what appened in the 'then'
630 var then_var_ctx
= v
.variable_ctx
631 # Reset to process the 'else'
632 v
.variable_ctx
= old_var_ctx
.sub
(n_else
)
634 # Merge then and else in the old control_flow
635 old_var_ctx
.merge2
(then_var_ctx
, v
.variable_ctx
)
636 v
.variable_ctx
= old_var_ctx
642 redef class AWhileExpr
643 # The corresponding escapable block
644 readable attr _escapable
: EscapableBlock
646 redef meth accept_typing
(v
)
648 _escapable
= new EscapableBlock(self)
649 v
.escapable_ctx
.push
(_escapable
)
650 var old_var_ctx
= v
.variable_ctx
651 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
655 v
.check_conform_expr
(n_expr
, v
.type_bool
)
656 v
.variable_ctx
= old_var_ctx
663 # The corresponding escapable block
664 readable attr _escapable
: EscapableBlock
666 readable attr _meth_iterator
: MMMethod
667 readable attr _meth_is_ok
: MMMethod
668 readable attr _meth_item
: MMMethod
669 readable attr _meth_next
: MMMethod
670 redef meth accept_typing
(v
)
672 _escapable
= new EscapableBlock(self)
673 v
.escapable_ctx
.push
(_escapable
)
675 var old_var_ctx
= v
.variable_ctx
676 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
677 var va
= new AutoVariable(n_id
.to_symbol
, self)
679 v
.variable_ctx
.add
(va
)
683 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then return
684 var expr_type
= n_expr
.stype
685 _meth_iterator
= expr_type
.local_class
.select_method
(once
("iterator".to_symbol
))
686 if _meth_iterator
== null then
687 v
.error
(self, "Error: Collection MUST have an iterate method")
690 var iter_type
= _meth_iterator
.signature_for
(expr_type
).return_type
691 _meth_is_ok
= iter_type
.local_class
.select_method
(once
("is_ok".to_symbol
))
692 if _meth_is_ok
== null then
693 v
.error
(self, "Error: {iter_type} MUST have an is_ok method")
696 _meth_item
= iter_type
.local_class
.select_method
(once
("item".to_symbol
))
697 if _meth_item
== null then
698 v
.error
(self, "Error: {iter_type} MUST have an item method")
701 _meth_next
= iter_type
.local_class
.select_method
(once
("next".to_symbol
))
702 if _meth_next
== null then
703 v
.error
(self, "Error: {iter_type} MUST have a next method")
706 var t
= _meth_item
.signature_for
(iter_type
).return_type
707 if not n_expr
.is_self
then t
= t
.not_for_self
710 if n_block
!= null then v
.visit
(n_block
)
713 v
.variable_ctx
= old_var_ctx
719 redef class AAssertExpr
720 redef meth after_typing
(v
)
722 v
.check_conform_expr
(n_expr
, v
.type_bool
)
723 v
.use_if_true_variable_ctx
(n_expr
)
729 redef meth its_variable
do return variable
731 redef meth after_typing
(v
)
733 v
.variable_ctx
.check_is_set
(self, variable
)
734 _stype
= v
.variable_ctx
.stype
(variable
)
735 _is_typed
= _stype
!= null
739 redef class AVarAssignExpr
740 redef meth after_typing
(v
)
742 v
.variable_ctx
.mark_is_set
(variable
)
743 var t
= v
.variable_ctx
.stype
(variable
)
744 v
.check_conform_expr
(n_value
, t
)
749 redef class AReassignFormExpr
750 # Compute and check method used through the reassigment operator
751 private meth do_lvalue_typing
(v
: TypingVisitor, type_lvalue
: MMType)
753 if type_lvalue
== null then
756 var name
= n_assign_op
.method_name
757 var lc
= type_lvalue
.local_class
758 if not lc
.has_global_property_by_name
(name
) then
759 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
762 var prop
= lc
.select_method
(name
)
763 prop
.global
.check_visibility
(v
, self, v
.module, false)
764 var psig
= prop
.signature_for
(type_lvalue
)
765 _assign_method
= prop
766 if not v
.check_conform_expr
(n_value
, psig
[0].not_for_self
) then return
767 if not v
.check_conform
(self, psig
.return_type
.not_for_self
, n_value
.stype
) then return
770 # Method used through the reassigment operator (once computed)
771 readable attr _assign_method
: MMMethod
774 redef class AVarReassignExpr
775 redef meth after_typing
(v
)
777 v
.variable_ctx
.check_is_set
(self, variable
)
778 v
.variable_ctx
.mark_is_set
(variable
)
779 var t
= v
.variable_ctx
.stype
(variable
)
780 do_lvalue_typing
(v
, t
)
785 redef class PAssignOp
786 meth method_name
: Symbol is abstract
788 redef class APlusAssignOp
789 redef meth method_name
do return once
"+".to_symbol
791 redef class AMinusAssignOp
792 redef meth method_name
do return once
"-".to_symbol
795 redef class ASelfExpr
796 redef meth its_variable
do return variable
798 redef meth after_typing
(v
)
800 variable
= v
.self_var
801 _stype
= v
.variable_ctx
.stype
(variable
)
805 redef meth is_self
do return true
808 redef class AImplicitSelfExpr
809 redef meth is_implicit_self
do return true
812 redef class AIfexprExpr
813 redef meth accept_typing
(v
)
815 var old_var_ctx
= v
.variable_ctx
818 v
.use_if_true_variable_ctx
(n_expr
)
820 v
.variable_ctx
= old_var_ctx
823 v
.check_conform_expr
(n_expr
, v
.type_bool
)
825 _stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
826 _is_typed
= _stype
!= null
830 redef class ABoolExpr
831 redef meth after_typing
(v
)
839 redef meth after_typing
(v
)
841 v
.check_conform_expr
(n_expr
, v
.type_bool
)
842 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
849 redef meth accept_typing
(v
)
851 var old_var_ctx
= v
.variable_ctx
854 v
.use_if_true_variable_ctx
(n_expr
)
857 if n_expr2
.if_true_variable_ctx
!= null then
858 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
860 _if_true_variable_ctx
= v
.variable_ctx
863 v
.variable_ctx
= old_var_ctx
865 v
.check_conform_expr
(n_expr
, v
.type_bool
)
866 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
873 redef meth after_typing
(v
)
875 v
.check_conform_expr
(n_expr
, v
.type_bool
)
882 redef meth after_typing
(v
)
889 redef class AFloatExpr
890 redef meth after_typing
(v
)
892 _stype
= v
.type_float
897 redef class ACharExpr
898 redef meth after_typing
(v
)
905 redef class AStringFormExpr
906 readable attr _meth_with_native
: MMMethod
907 redef meth after_typing
(v
)
909 _stype
= v
.type_string
911 _meth_with_native
= _stype
.local_class
.select_method
(once
"with_native".to_symbol
)
912 if _meth_with_native
== null then v
.error
(self, "{_stype} MUST have a with_native method.")
916 redef class ASuperstringExpr
917 readable attr _meth_with_capacity
: MMMethod
918 readable attr _meth_add
: MMMethod
919 readable attr _meth_to_s
: MMMethod
920 readable attr _atype
: MMType
921 redef meth after_typing
(v
)
923 _stype
= v
.type_string
924 _atype
= v
.type_array
(_stype
)
925 _meth_with_capacity
= _atype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
926 if _meth_with_capacity
== null then v
.error
(self, "{_atype} MUST have a with_capacity method.")
927 _meth_add
= _atype
.local_class
.select_method
(once
"add".to_symbol
)
928 if _meth_add
== null then v
.error
(self, "{_atype} MUST have an add method.")
929 _meth_to_s
= v
.type_object
.local_class
.select_method
(once
"to_s".to_symbol
)
930 if _meth_to_s
== null then v
.error
(self, "Object MUST have a to_s method.")
935 redef class ANullExpr
936 redef meth after_typing
(v
)
943 redef class AArrayExpr
944 readable attr _meth_with_capacity
: MMMethod
945 readable attr _meth_add
: MMMethod
947 redef meth after_typing
(v
)
949 var stype
= v
.check_conform_multiexpr
(null, n_exprs
)
950 if stype
== null then return
954 private meth do_typing
(v
: TypingVisitor, element_type
: MMType)
956 _stype
= v
.type_array
(element_type
)
958 _meth_with_capacity
= _stype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
959 if _meth_with_capacity
== null then v
.error
(self, "{_stype} MUST have a with_capacity method.")
960 _meth_add
= _stype
.local_class
.select_method
(once
"add".to_symbol
)
961 if _meth_add
== null then v
.error
(self, "{_stype} MUST have an add method.")
967 redef class ARangeExpr
968 readable attr _meth_init
: MMMethod
969 redef meth after_typing
(v
)
971 if not v
.check_expr
(n_expr
) or not v
.check_expr
(n_expr2
) then return
972 var ntype
= n_expr
.stype
973 var ntype2
= n_expr2
.stype
974 if ntype
< ntype2
then
976 else if not ntype2
< ntype
then
977 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
980 var dtype
= v
.type_discrete
981 if not v
.check_conform_expr
(n_expr
, dtype
) or not v
.check_conform_expr
(n_expr2
, dtype
) then return
982 _stype
= v
.type_range
(ntype
)
987 redef class ACrangeExpr
988 redef meth after_typing
(v
)
991 _meth_init
= stype
.local_class
.select_method
(once
"init".to_symbol
)
994 redef class AOrangeExpr
995 redef meth after_typing
(v
)
998 _meth_init
= stype
.local_class
.select_method
(once
"without_last".to_symbol
)
1003 redef class ASuperExpr
1004 special ASuperInitCall
1005 # readable attr _prop: MMSrcMethod
1006 readable attr _init_in_superclass
: MMMethod
1007 redef meth after_typing
(v
)
1009 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
1010 if not precs
.is_empty
then
1011 v
.local_property
.need_super
= true
1012 else if v
.local_property
.global
.is_init
then
1013 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
1014 for p
in base_precs
do
1015 if not p
.global
.is_init
then
1016 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
1018 precs
.add
(v
.local_class
[p
.global
])
1021 if precs
.is_empty
then
1022 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
1024 else if precs
.length
> 1 then
1025 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
1028 var p
= base_precs
.first
1029 assert p
isa MMMethod
1030 _init_in_superclass
= p
1031 register_super_init_call
(v
, p
)
1032 if n_args
.length
> 0 then
1033 var signature
= get_signature
(v
, v
.self_var
.stype
, p
, true)
1034 _arguments
= process_signature
(v
, signature
, p
.name
, n_args
.to_a
)
1037 v
.error
(self, "Error: No super method to call for {v.local_property}.")
1041 if precs
.first
.signature_for
(v
.self_var
.stype
).return_type
!= null then
1042 var stypes
= new Array[MMType]
1043 var stype
: MMType = null
1044 for prop
in precs
do
1045 assert prop
isa MMMethod
1046 var t
= prop
.signature_for
(v
.self_var
.stype
).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
1048 if stype
== null or stype
< t
then
1053 v
.check_conform
(self, t
, stype
)
1057 var p
= v
.local_property
1058 assert p
isa MMSrcMethod
1064 redef class AAttrFormExpr
1065 # Attribute accessed
1066 readable attr _prop
: MMAttribute
1068 # Attribute type of the acceded attribute
1069 readable attr _attr_type
: MMType
1071 # Compute the attribute accessed
1072 private meth do_typing
(v
: TypingVisitor)
1074 if not v
.check_expr
(n_expr
) then return
1075 var type_recv
= n_expr
.stype
1076 var name
= n_id
.to_symbol
1077 var lc
= type_recv
.local_class
1078 if not lc
.has_global_property_by_name
(name
) then
1079 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
1082 var prop
= lc
.select_attribute
(name
)
1083 if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
1084 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
1087 var at
= prop
.signature_for
(type_recv
).return_type
1088 if not n_expr
.is_self
then at
= at
.not_for_self
1093 redef class AAttrExpr
1094 redef meth after_typing
(v
)
1097 if prop
== null then return
1103 redef class AAttrAssignExpr
1104 redef meth after_typing
(v
)
1107 if prop
== null then return
1108 if not v
.check_conform_expr
(n_value
, attr_type
) then return
1113 redef class AAttrReassignExpr
1114 redef meth after_typing
(v
)
1117 if prop
== null then return
1118 do_lvalue_typing
(v
, attr_type
)
1123 class AAbsAbsSendExpr
1125 # The signature of the called property
1126 readable attr _prop_signature
: MMSignature
1128 # The real arguments used (after star transformation) (once computed)
1129 readable attr _arguments
: Array[PExpr]
1131 # Check the conformity of a set of arguments `raw_args' to a signature.
1132 private meth process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: Array[PExpr]): Array[PExpr]
1134 var par_vararg
= psig
.vararg_rank
1135 var par_arity
= psig
.arity
1137 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
1138 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
1139 v
.error
(self, "Error: '{name}' arity missmatch.")
1143 var args
= new Array[PExpr]
1144 for par_idx
in [0..par_arity
[ do
1146 var par_type
= psig
[par_idx
]
1147 if par_idx
== par_vararg
then
1148 var star
= new Array[PExpr]
1149 for i
in [0..(raw_arity-par_arity
)] do
1150 a
= raw_args
[arg_idx
]
1151 v
.check_conform_expr
(a
, par_type
)
1153 arg_idx
= arg_idx
+ 1
1155 var aa
= new AArrayExpr.init_aarrayexpr
(star
)
1156 aa
.do_typing
(v
, par_type
)
1159 a
= raw_args
[arg_idx
]
1160 v
.check_conform_expr
(a
, par_type
)
1161 arg_idx
= arg_idx
+ 1
1168 # Check the conformity of a set of defined closures
1169 private meth process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: Array[PClosureDef]): MMType
1171 var t
= psig
.return_type
1172 var cs
= psig
.closures
# Declared closures
1175 if not c
.is_optional
then min_arity
+= 1
1178 if cs
.length
== 0 then
1179 v
.error
(self, "Error: {name} does not require blocks.")
1180 else if cd
.length
> cs
.length
or cd
.length
< min_arity
then
1181 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
1183 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1184 var break_list
: Array[ABreakExpr] = null
1185 if t
!= null then break_list
= new Array[ABreakExpr]
1187 # Process each closure definition
1188 for i
in [0..cd
.length
[ do
1191 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1192 v
.escapable_ctx
.push
(esc
)
1193 cdi
.accept_typing2
(v
, esc
)
1197 # Check break type conformity
1198 if break_list
!= null then
1199 t
= v
.check_conform_multiexpr
(t
, break_list
)
1202 else if min_arity
!= 0 then
1203 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1210 special AAbsAbsSendExpr
1211 # Compute the called global property
1212 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])
1214 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
1215 if prop
== null then return
1216 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
1217 if sig
== null then return
1218 var args
= process_signature
(v
, sig
, prop
.name
, raw_args
)
1219 if args
== null then return
1220 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
1221 if rtype
== null and sig
.return_type
!= null then return
1223 _prop_signature
= sig
1225 _return_type
= rtype
1228 private meth get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): MMMethod
1230 if type_recv
== null then return null
1231 var lc
= type_recv
.local_class
1232 var prop
: MMMethod = null
1233 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
1234 if prop
== null and v
.local_property
.global
.is_init
then
1235 var props
= type_recv
.local_class
.super_methods_named
(name
)
1236 if props
.length
> 1 then
1237 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1239 else if props
.length
== 1 then
1240 var p
= type_recv
.local_class
[props
.first
.global
]
1241 assert p
isa MMMethod
1246 if prop
== null then
1247 if is_implicit_self
then
1248 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1250 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1257 # Get the signature for a local property and a receiver
1258 private meth get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
1260 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
1261 var psig
= prop
.signature_for
(type_recv
)
1262 if not recv_is_self
then psig
= psig
.not_for_self
1266 # The invoked method (once computed)
1267 readable attr _prop
: MMMethod
1269 # The return type (if any) (once computed)
1270 readable attr _return_type
: MMType
1273 # A possible call of constructor in a super class
1274 # Could be an explicit call or with the 'super' keyword
1275 class ASuperInitCall
1276 special AAbsSendExpr
1277 private meth register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1279 if parent
!= v
.top_block
and self != v
.top_block
then
1280 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1282 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
1283 var prev_class
: MMLocalClass = null
1284 if not v
.explicit_super_init_calls
.is_empty
then
1285 prev_class
= v
.explicit_super_init_calls
.last
.global
.intro
.local_class
1287 var order
= v
.local_class
.cshe
.reverse_linear_extension
1288 if cla
== v
.local_class
then
1289 v
.explicit_other_init_call
= true
1290 else if not order
.has
(cla
) then
1291 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1292 else if cla
== prev_class
then
1293 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1295 var last_is_found
= prev_class
== null
1297 if c
== prev_class
then
1298 last_is_found
= true
1299 else if c
== cla
then
1300 if not last_is_found
then
1301 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1303 v
.explicit_super_init_calls
.add
(property
)
1312 redef class ANewExpr
1313 special AAbsSendExpr
1314 redef meth after_typing
(v
)
1316 var t
= n_type
.stype
1317 if t
== null then return
1318 if t
.local_class
.global
.is_abstract
then
1319 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1323 if n_id
== null then
1324 name
= once
"init".to_symbol
1326 name
= n_id
.to_symbol
1329 do_typing
(v
, t
, false, false, name
, n_args
.to_a
, null)
1330 if prop
== null then return
1332 if not prop
.global
.is_init
then
1333 v
.error
(self, "Error: {prop} is not a constructor.")
1342 redef class ASendExpr
1343 special ASuperInitCall
1344 # Name of the invoked property
1345 meth name
: Symbol is abstract
1347 # Raw arguments used (withour star transformation)
1348 meth raw_arguments
: Array[PExpr] is abstract
1350 # Closure definitions
1351 meth closure_defs
: Array[PClosureDef] do return null
1353 redef meth after_typing
(v
)
1358 private meth do_all_typing
(v
: TypingVisitor)
1360 if not v
.check_expr
(n_expr
) then return
1361 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1362 if prop
== null then return
1364 if prop
.global
.is_init
then
1365 if not v
.local_property
.global
.is_init
then
1366 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1367 else if not n_expr
.is_self
then
1368 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1370 register_super_init_call
(v
, prop
)
1374 _stype
= return_type
1379 class ASendReassignExpr
1381 special AReassignFormExpr
1382 readable attr _read_prop
: MMMethod
1383 redef meth do_all_typing
(v
)
1385 if not v
.check_expr
(n_expr
) then return
1386 var raw_args
= raw_arguments
1387 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1388 if prop
== null then return
1389 if prop
.global
.is_init
then
1390 if not v
.local_property
.global
.is_init
then
1391 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1392 else if not n_expr
.is_self
then
1393 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1396 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1397 if not n_expr
.is_self
then t
= t
.not_for_self
1399 do_lvalue_typing
(v
, t
)
1402 var old_args
= arguments
1403 raw_args
.add
(n_value
)
1405 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1406 if prop
== null then return
1407 if prop
.global
.is_init
then
1408 if not v
.local_property
.global
.is_init
then
1409 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1410 else if not n_expr
.is_self
then
1411 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1415 _arguments
= old_args
# FIXME: What if star parameters do not match betwen the two methods?
1420 redef class ABinopExpr
1421 redef meth raw_arguments
do return [n_expr2
]
1424 redef meth name
do return once
"==".to_symbol
1427 redef meth name
do return once
"!=".to_symbol
1430 redef meth name
do return once
"<".to_symbol
1433 redef meth name
do return once
"<=".to_symbol
1436 redef meth name
do return once
">".to_symbol
1439 redef meth name
do return once
">=".to_symbol
1441 redef class APlusExpr
1442 redef meth name
do return once
"+".to_symbol
1444 redef class AMinusExpr
1445 redef meth name
do return once
"-".to_symbol
1447 redef class AStarshipExpr
1448 redef meth name
do return once
"<=>".to_symbol
1450 redef class AStarExpr
1451 redef meth name
do return once
"*".to_symbol
1453 redef class ASlashExpr
1454 redef meth name
do return once
"/".to_symbol
1456 redef class APercentExpr
1457 redef meth name
do return once
"%".to_symbol
1460 redef class AUminusExpr
1461 redef meth name
do return once
"unary -".to_symbol
1462 redef meth raw_arguments
do return null
1465 redef class ACallFormExpr
1466 redef meth after_typing
(v
)
1468 if n_expr
!= null and n_expr
.is_implicit_self
then
1469 var name
= n_id
.to_symbol
1470 var variable
= v
.variable_ctx
[name
]
1471 if variable
!= null then
1472 if variable
isa ClosureVariable then
1473 var n
= new AClosureCallExpr.init_aclosurecallexpr
(n_id
, n_args
, n_closure_defs
)
1475 n
.variable
= variable
1479 if not n_args
.is_empty
then
1480 v
.error
(self, "Error: {name} is variable, not a function.")
1483 var vform
= variable_create
(variable
)
1484 vform
.variable
= variable
1486 vform
.after_typing
(v
)
1495 redef meth closure_defs
1497 if n_closure_defs
== null or n_closure_defs
.is_empty
then
1500 return n_closure_defs
.to_a
1504 # Create a variable acces corresponding to the call form
1505 meth variable_create
(variable
: Variable): AVarFormExpr is abstract
1508 redef class ACallExpr
1509 redef meth variable_create
(variable
)
1511 return new AVarExpr.init_avarexpr
(n_id
)
1514 redef meth name
do return n_id
.to_symbol
1515 redef meth raw_arguments
do return n_args
.to_a
1518 redef class ACallAssignExpr
1519 redef meth variable_create
(variable
)
1521 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1524 redef meth name
do return (n_id
.text
+ "=").to_symbol
1525 redef meth raw_arguments
do
1526 var res
= n_args
.to_a
1532 redef class ACallReassignExpr
1533 special ASendReassignExpr
1534 redef meth variable_create
(variable
)
1536 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1539 redef meth name
do return n_id
.to_symbol
1540 redef meth raw_arguments
do return n_args
.to_a
1543 redef class ABraExpr
1544 redef meth name
do return once
"[]".to_symbol
1545 redef meth raw_arguments
do return n_args
.to_a
1548 redef class ABraAssignExpr
1549 redef meth name
do return once
"[]=".to_symbol
1550 redef meth raw_arguments
do
1551 var res
= n_args
.to_a
1557 redef class ABraReassignExpr
1558 special ASendReassignExpr
1559 redef meth name
do return once
"[]".to_symbol
1560 redef meth raw_arguments
do return n_args
.to_a
1563 redef class AInitExpr
1564 redef meth name
do return once
"init".to_symbol
1565 redef meth raw_arguments
do return n_args
.to_a
1568 redef class AClosureCallExpr
1569 special AAbsAbsSendExpr
1570 redef meth after_typing
(v
)
1573 if va
.closure
.is_break
then v
.variable_ctx
.unreash
= true
1574 var sig
= va
.closure
.signature
1575 var args
= process_signature
(v
, sig
, n_id
.to_symbol
, n_args
.to_a
)
1576 if not n_closure_defs
.is_empty
then
1577 process_closures
(v
, sig
, n_id
.to_symbol
, n_closure_defs
.to_a
)
1579 if args
== null then return
1580 _prop_signature
= sig
1582 _stype
= sig
.return_type
1587 redef class PClosureDef
1588 # The corresponding escapable object
1589 readable attr _escapable
: EscapableBlock
1591 attr _accept_typing2
: Bool
1592 redef meth accept_typing
(v
)
1594 # Typing is deferred, wait accept_typing2(v)
1595 if _accept_typing2
then super
1598 private meth accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure) is abstract
1601 redef class AClosureDef
1602 redef meth accept_typing2
(v
, esc
)
1606 var sig
= esc
.closure
.signature
1607 if sig
.arity
!= n_id
.length
then
1608 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1612 closure
= esc
.closure
1614 var old_var_ctx
= v
.variable_ctx
1615 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
1616 variables
= new Array[AutoVariable]
1617 for i
in [0..n_id
.length
[ do
1618 var va
= new AutoVariable(n_id
[i
].to_symbol
, self)
1621 v
.variable_ctx
.add
(va
)
1624 _accept_typing2
= true
1627 if v
.variable_ctx
.unreash
== false then
1628 if closure
.signature
.return_type
!= null then
1629 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
1630 else if closure
.is_break
then
1631 v
.error
(self, "Control error: Reached end of break block (a 'break' was expected).")
1634 v
.variable_ctx
= old_var_ctx
1638 class ATypeCheckExpr
1640 private meth check_expr_cast
(v
: TypingVisitor, n_expr
: PExpr, n_type
: PType)
1642 if not v
.check_expr
(n_expr
) then return
1643 var etype
= n_expr
.stype
1644 var ttype
= n_type
.stype
1645 if etype
== ttype
then
1646 v
.warning
(self, "Warning: Expression is already a {ttype}.")
1647 else if etype
< ttype
then
1648 v
.warning
(self, "Warning: Expression is already a {ttype} since it is a {etype}.")
1653 redef class AIsaExpr
1654 special ATypeCheckExpr
1655 redef meth after_typing
(v
)
1657 check_expr_cast
(v
, n_expr
, n_type
)
1658 var variable
= n_expr
.its_variable
1659 if variable
!= null then
1660 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, n_type
.stype
)
1662 _stype
= v
.type_bool
1667 redef class AAsCastExpr
1668 special ATypeCheckExpr
1669 redef meth after_typing
(v
)
1671 check_expr_cast
(v
, n_expr
, n_type
)
1672 _stype
= n_type
.stype
1673 _is_typed
= _stype
!= null
1677 redef class AProxyExpr
1678 redef meth after_typing
(v
)
1680 if not n_expr
.is_typed
then return
1682 if n_expr
.is_statement
then return
1683 _stype
= n_expr
.stype
1687 redef class AOnceExpr
1688 redef meth accept_typing
(v
)
1690 if v
.once_count
> 0 then
1691 v
.warning
(self, "Useless once in a once expression.")
1693 v
.once_count
= v
.once_count
+ 1
1697 v
.once_count
= v
.once_count
- 1