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
24 redef class MMSrcModule
25 # Walk trough the module and type statments and expressions
26 # Require than supermodules are processed
27 fun do_typing
(tc
: ToolContext)
29 var tv
= new TypingVisitor(tc
, self)
35 # * Associate local variables to nodes
36 # * Distinguish method call and local variable access
37 # * Resolve call and attribute access
38 # * Check type conformance
39 private class TypingVisitor
40 special AbsSyntaxVisitor
43 if n
!= null then n
.accept_typing
(self)
46 # Current knowledge about variables names and types
47 fun variable_ctx
: VariableContext do return _variable_ctx
.as(not null)
48 writable var _variable_ctx
: nullable VariableContext
50 # Non-bypassable knowledge about variables names and types
51 fun base_variable_ctx
: VariableContext do return _base_variable_ctx
.as(not null)
52 writable var _base_variable_ctx
: nullable VariableContext
54 # Current knowledge about escapable blocks
55 readable writable var _escapable_ctx
: EscapableContext = new EscapableContext(self)
57 # The current reciever
58 fun self_var
: ParamVariable do return _self_var
.as(not null)
59 writable var _self_var
: nullable ParamVariable
61 # Block of the current method
62 readable writable var _top_block
: nullable AExpr
64 # List of explicit invocation of constructors of super-classes
65 readable writable var _explicit_super_init_calls
: nullable Array[MMMethod]
67 # Is a other constructor of the same class invoked
68 readable writable var _explicit_other_init_call
: Bool = false
70 # Make the if_true_variable_ctx of the expression effective
71 private fun use_if_true_variable_ctx
(e
: AExpr)
73 var ctx
= e
.if_true_variable_ctx
74 if ctx
!= null then variable_ctx
= ctx
77 # Make the if_false_variable_ctx of the expression effective
78 private fun use_if_false_variable_ctx
(e
: AExpr)
80 var ctx
= e
.if_false_variable_ctx
81 if ctx
!= null then variable_ctx
= ctx
84 # Number of nested once
85 readable writable var _once_count
: Int = 0
87 init(tc
, module) do super
89 private fun get_default_constructor_for
(n
: ANode, c
: MMLocalClass, prop
: MMSrcMethod): nullable MMMethod
92 #var prop = v.local_property
93 #assert prop isa MMMethod
94 var candidates
= new Array[MMMethod]
95 var false_candidates
= new Array[MMMethod]
96 var parity
= prop
.signature
.arity
97 for g
in c
.global_properties
do
98 if not g
.is_init_for
(c
) then continue
100 var gps
= gp
.signature_for
(c
.get_type
)
101 assert gp
isa MMSrcMethod
102 var garity
= gps
.arity
103 if gp
.name
== prop
.name
then
104 if garity
== 0 or (parity
== garity
and prop
.signature
< gps
) then
107 false_candidates
.add
(gp
)
109 else if garity
== 0 and gp
.name
== once
("init".to_symbol
) then
111 false_candidates
.add
(gp
)
113 false_candidates
.add
(gp
)
116 if candidates
.length
== 1 then
117 return candidates
.first
118 else if candidates
.length
> 0 then
119 var a
= new Array[String]
120 for p
in candidates
do
121 a
.add
("{p.full_name}{p.signature}")
123 v
.error
(n
, "Error: Conflicting default constructor to call for {c}: {a.join(", ")}.")
125 else if false_candidates
.length
> 0 then
126 var a
= new Array[String]
127 for p
in false_candidates
do
128 a
.add
("{p.full_name}{p.signature}")
130 v
.error
(n
, "Error: there is no available compatible constrctor in {c}. Discarded candidates are {a.join(", ")}.")
133 v
.error
(n
, "Error: there is no available compatible constrctor in {c}.")
140 ###############################################################################
143 private fun accept_typing
(v
: TypingVisitor)
145 accept_abs_syntax_visitor
(v
)
148 private fun after_typing
(v
: TypingVisitor) do end
151 redef class AClassdef
152 redef fun accept_typing
(v
)
154 v
.variable_ctx
= new RootVariableContext(v
, self)
155 v
.base_variable_ctx
= v
.variable_ctx
156 v
.self_var
= new ParamVariable("self".to_symbol
, self)
157 v
.self_var
.stype
= local_class
.get_type
163 redef fun self_var
do return _self_var
.as(not null)
164 var _self_var
: nullable ParamVariable
167 redef class AAttrPropdef
168 redef fun accept_typing
(v
)
170 var old_var_ctx
= v
.variable_ctx
171 v
.variable_ctx
= old_var_ctx
.sub
(self)
172 _self_var
= v
.self_var
174 if n_expr
!= null then
175 v
.check_conform_expr
(n_expr
.as(not null), prop
.signature
.return_type
.as(not null))
177 v
.variable_ctx
= old_var_ctx
181 redef class AMethPropdef
182 redef fun accept_typing
(v
)
184 var old_var_ctx
= v
.variable_ctx
185 v
.variable_ctx
= old_var_ctx
.sub
(self)
186 _self_var
= v
.self_var
188 v
.variable_ctx
= old_var_ctx
192 redef class AConcreteMethPropdef
193 redef fun after_typing
(v
)
196 if v
.variable_ctx
.unreash
== false and method
.signature
.return_type
!= null then
197 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
202 redef class AConcreteInitPropdef
203 redef fun accept_typing
(v
)
205 v
.top_block
= n_block
206 v
.explicit_super_init_calls
= explicit_super_init_calls
207 v
.explicit_other_init_call
= false
211 redef fun after_typing
(v
)
214 if v
.explicit_other_init_call
or method
.global
.intro
!= method
then
218 var l
= explicit_super_init_calls
.length
219 var cur_m
: nullable MMMethod = null
220 var cur_c
: nullable MMLocalClass = null
222 cur_m
= explicit_super_init_calls
[i
]
223 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
226 while j
< v
.local_class
.cshe
.direct_greaters
.length
do
227 var c
= v
.local_class
.cshe
.direct_greaters
[j
]
228 if c
.global
.is_interface
or c
.global
.is_universal
or c
.global
.is_mixin
then
230 else if cur_c
!= null and (c
.cshe
<= cur_c
or cur_c
.global
.is_mixin
) then
231 if c
== cur_c
then j
+= 1
232 super_init_calls
.add
(cur_m
.as(not null))
235 cur_m
= explicit_super_init_calls
[i
]
236 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
242 var p
= v
.get_default_constructor_for
(self, c
, method
)
244 super_init_calls
.add
(p
)
254 redef fun after_typing
(v
)
256 v
.variable_ctx
.add
(variable
)
260 redef class AClosureDecl
261 # The corresponding escapable object
262 readable var _escapable
: nullable EscapableBlock
264 redef fun accept_typing
(v
)
266 # Register the closure for ClosureCallExpr
267 v
.variable_ctx
.add
(variable
)
269 var old_var_ctx
= v
.variable_ctx
270 var old_base_var_ctx
= v
.base_variable_ctx
271 v
.base_variable_ctx
= v
.variable_ctx
272 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
274 var blist
: nullable Array[AExpr] = null
275 var t
= v
.local_property
.signature
.return_type
276 if t
!= null then blist
= new Array[AExpr]
277 var escapable
= new EscapableClosure(self, variable
.closure
, blist
)
278 _escapable
= escapable
279 v
.escapable_ctx
.push
(escapable
, null)
283 if n_expr
!= null then
284 if v
.variable_ctx
.unreash
== false then
285 if variable
.closure
.signature
.return_type
!= null then
286 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
287 else if variable
.closure
.is_break
and escapable
.break_list
!= null then
288 v
.error
(self, "Control error: Reached end of break block (a 'break' with a value was expected).")
292 if blist
!= null then for x
in blist
do
293 v
.check_conform_expr
(x
, t
)
296 old_var_ctx
.merge
(v
.variable_ctx
)
297 v
.variable_ctx
= old_var_ctx
298 v
.base_variable_ctx
= old_base_var_ctx
304 redef fun stype
: MMType do return _stype
.as(not null)
305 redef fun is_typed
: Bool do return _stype
!= null
306 var _stype
: nullable MMType
308 redef fun after_typing
(v
)
310 _stype
= get_stype
(v
)
315 redef readable var _is_typed
: Bool = false
316 redef fun is_statement
: Bool do return _stype
== null
320 print
"{location}: not is_typed"
324 print
"{location}: is_statement"
327 return _stype
.as(not null)
329 var _stype
: nullable MMType
331 redef fun after_typing
(v
)
333 # Default behavior is to be happy
337 # Is the expression the implicit receiver
338 fun is_implicit_self
: Bool do return false
340 # Is the expression the current receiver (implicit or explicit)
341 fun is_self
: Bool do return false
343 # The variable accessed is any
344 fun its_variable
: nullable Variable do return null
346 # The variable type information if current boolean expression is true
347 readable private var _if_true_variable_ctx
: nullable VariableContext
349 # The variable type information if current boolean expression is false
350 readable private var _if_false_variable_ctx
: nullable VariableContext
353 redef class AVardeclExpr
354 var _variable
: nullable VarVariable
355 redef fun variable
do return _variable
.as(not null)
357 redef fun after_typing
(v
)
359 var va
= new VarVariable(n_id
.to_symbol
, n_id
)
361 v
.variable_ctx
.add
(va
)
363 if ne
!= null then v
.variable_ctx
.mark_is_set
(va
)
365 if n_type
!= null then
366 if not n_type
.is_typed
then return
367 va
.stype
= n_type
.stype
369 v
.check_conform_expr
(ne
, va
.stype
)
371 else if ne
!= null then
372 if not v
.check_expr
(ne
) then return
375 va
.stype
= v
.type_object
.as_nullable
381 redef class ABlockExpr
382 redef fun accept_typing
(v
)
384 var old_var_ctx
= v
.variable_ctx
385 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
388 if not v
.variable_ctx
.unreash
then
390 else if not v
.variable_ctx
.already_unreash
then
391 v
.variable_ctx
.already_unreash
= true
392 v
.error
(e
, "Error: unreachable statement.")
396 old_var_ctx
.merge
(v
.variable_ctx
)
397 v
.variable_ctx
= old_var_ctx
402 redef class AReturnExpr
403 redef fun after_typing
(v
)
405 v
.variable_ctx
.unreash
= true
406 var t
= v
.local_property
.signature
.return_type
408 if e
== null and t
!= null then
409 v
.error
(self, "Error: Return without value in a function.")
410 else if e
!= null and t
== null then
411 v
.error
(self, "Error: Return with value in a procedure.")
412 else if e
!= null and t
!= null then
413 v
.check_conform_expr
(e
, t
)
419 redef class AContinueExpr
420 redef fun after_typing
(v
)
422 v
.variable_ctx
.unreash
= true
423 var esc
= compute_escapable_block
(v
.escapable_ctx
)
424 if esc
== null then return
426 if esc
.is_break_block
then
427 v
.error
(self, "Error: cannot 'continue', only 'break'.")
431 var t
= esc
.continue_stype
432 if n_expr
== null and t
!= null then
433 v
.error
(self, "Error: continue with a value required in this block.")
434 else if n_expr
!= null and t
== null then
435 v
.error
(self, "Error: continue without value required in this block.")
436 else if n_expr
!= null and t
!= null then
437 v
.check_conform_expr
(n_expr
.as(not null), t
)
443 redef class ABreakExpr
444 redef fun after_typing
(v
)
446 var unreash
= v
.variable_ctx
.unreash
447 v
.variable_ctx
.unreash
= true
448 var esc
= compute_escapable_block
(v
.escapable_ctx
)
449 if esc
== null then return
451 if not unreash
then esc
.break_variable_contexts
.add
(v
.variable_ctx
)
453 var bl
= esc
.break_list
454 if n_expr
== null and bl
!= null then
455 v
.error
(self, "Error: break with a value required in this block.")
456 else if n_expr
!= null and bl
== null then
457 v
.error
(self, "Error: break without value required in this block.")
458 else if n_expr
!= null and bl
!= null then
459 # Typing check can only be done later
460 bl
.add
(n_expr
.as(not null))
466 redef class AAbortExpr
467 redef fun after_typing
(v
)
469 v
.variable_ctx
.unreash
= true
475 # The corresponding escapable block
476 readable var _escapable
: nullable EscapableBlock
478 redef fun accept_typing
(v
)
480 var escapable
= new BreakOnlyEscapableBlock(self)
481 _escapable
= escapable
482 v
.escapable_ctx
.push
(escapable
, n_label
)
492 redef fun accept_typing
(v
)
494 var old_var_ctx
= v
.variable_ctx
495 v
.enter_visit
(n_expr
)
496 v
.check_conform_expr
(n_expr
, v
.type_bool
)
498 # Prepare 'then' context
499 v
.use_if_true_variable_ctx
(n_expr
)
502 if n_then
!= null then
503 v
.variable_ctx
= v
.variable_ctx
.sub
(n_then
.as(not null))
504 v
.enter_visit
(n_then
)
507 # Remember what appened in the 'then'
508 var then_var_ctx
= v
.variable_ctx
510 # Prepare 'else' context
511 v
.variable_ctx
= old_var_ctx
512 v
.use_if_false_variable_ctx
(n_expr
)
515 if n_else
!= null then
516 v
.variable_ctx
= v
.variable_ctx
.sub
(n_else
.as(not null))
517 v
.enter_visit
(n_else
)
520 # Merge 'then' and 'else' contexts
521 old_var_ctx
.merge2
(then_var_ctx
, v
.variable_ctx
, v
.base_variable_ctx
)
522 v
.variable_ctx
= old_var_ctx
527 redef class AWhileExpr
528 # The corresponding escapable block
529 readable var _escapable
: nullable EscapableBlock
531 redef fun accept_typing
(v
)
533 var escapable
= new EscapableBlock(self)
534 _escapable
= escapable
535 v
.escapable_ctx
.push
(escapable
, n_label
)
536 var old_var_ctx
= v
.variable_ctx
537 var old_base_var_ctx
= v
.base_variable_ctx
538 v
.base_variable_ctx
= v
.variable_ctx
539 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
542 v
.enter_visit
(n_expr
)
543 v
.check_conform_expr
(n_expr
, v
.type_bool
)
545 if n_expr
isa ATrueExpr then
546 v
.warning
(self, "Warning: use 'loop' instead of 'while true do'.")
549 # Prepare inside context (assert cond)
550 v
.use_if_true_variable_ctx
(n_expr
)
553 if n_block
!= null then
554 v
.variable_ctx
= v
.variable_ctx
.sub
(n_block
.as(not null))
555 v
.enter_visit
(n_block
)
558 v
.variable_ctx
= old_var_ctx
560 # Compute outside context (assert !cond + all breaks)
561 v
.use_if_false_variable_ctx
(n_expr
)
562 escapable
.break_variable_contexts
.add
(v
.variable_ctx
)
563 old_var_ctx
.combine_merge
(escapable
.break_variable_contexts
, v
.base_variable_ctx
)
565 v
.base_variable_ctx
= old_base_var_ctx
571 redef class ALoopExpr
572 # The corresponding escapable block
573 readable var _escapable
: nullable EscapableBlock
575 redef fun accept_typing
(v
)
577 var escapable
= new EscapableBlock(self)
578 _escapable
= escapable
579 v
.escapable_ctx
.push
(escapable
, n_label
)
580 var old_var_ctx
= v
.variable_ctx
581 var old_base_var_ctx
= v
.base_variable_ctx
582 v
.base_variable_ctx
= v
.variable_ctx
583 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
586 if n_block
!= null then
587 v
.variable_ctx
= v
.variable_ctx
.sub
(n_block
.as(not null))
588 v
.enter_visit
(n_block
)
591 # Compute outside context (assert all breaks)
592 if escapable
.break_variable_contexts
.is_empty
then
593 old_var_ctx
.unreash
= true
595 old_var_ctx
.combine_merge
(escapable
.break_variable_contexts
, v
.base_variable_ctx
)
598 v
.variable_ctx
= old_var_ctx
599 v
.base_variable_ctx
= old_base_var_ctx
606 var _variable
: nullable AutoVariable
607 redef fun variable
do return _variable
.as(not null)
609 # The corresponding escapable block
610 readable var _escapable
: nullable EscapableBlock
612 redef fun accept_typing
(v
)
614 var escapable
= new EscapableBlock(self)
615 _escapable
= escapable
616 v
.escapable_ctx
.push
(escapable
, n_label
)
618 var old_var_ctx
= v
.variable_ctx
619 var old_base_var_ctx
= v
.base_variable_ctx
620 v
.base_variable_ctx
= v
.variable_ctx
621 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
622 var va
= new AutoVariable(n_id
.to_symbol
, n_id
)
624 v
.variable_ctx
.add
(va
)
627 v
.enter_visit
(n_expr
)
629 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then return
630 var expr_type
= n_expr
.stype
633 var meth_iterator
= v
.get_method
(expr_type
, once
"iterator".to_symbol
)
634 var iter_type
= meth_iterator
.signature_for
(expr_type
).return_type
.as(not null)
635 var meth_item
= v
.get_method
(iter_type
, once
("item".to_symbol
))
636 var va_stype
= meth_item
.signature_for
(iter_type
).return_type
.as(not null)
637 if not n_expr
.is_self
then va_stype
= va_stype
.not_for_self
641 if n_block
!= null then v
.enter_visit
(n_block
)
644 v
.variable_ctx
= old_var_ctx
645 v
.base_variable_ctx
= old_base_var_ctx
651 redef class AAssertExpr
652 redef fun accept_typing
(v
)
655 v
.enter_visit
(n_expr
)
656 v
.check_conform_expr
(n_expr
, v
.type_bool
)
658 # Process optional 'else' part
659 if n_else
!= null then
660 var old_var_ctx
= v
.variable_ctx
661 v
.use_if_false_variable_ctx
(n_expr
)
662 v
.enter_visit
(n_else
)
663 v
.variable_ctx
= old_var_ctx
667 v
.use_if_true_variable_ctx
(n_expr
)
672 redef class AVarFormExpr
673 var _variable
: nullable Variable
674 redef fun variable
do return _variable
.as(not null)
678 redef fun its_variable
do return variable
680 redef fun after_typing
(v
)
682 v
.variable_ctx
.check_is_set
(self, variable
)
683 _stype
= v
.variable_ctx
.stype
(variable
)
684 _is_typed
= _stype
!= null
688 redef class AVarAssignExpr
689 redef fun after_typing
(v
)
691 v
.variable_ctx
.mark_is_set
(variable
)
693 # Check the base type
694 var btype
= v
.base_variable_ctx
.stype
(variable
)
695 if not v
.check_expr
(n_value
) then return
696 if btype
!= null and not v
.check_conform_expr
(n_value
, btype
) then return
699 v
.variable_ctx
.stype
(variable
) = n_value
.stype
705 redef class AReassignFormExpr
706 # Compute and check method used through the reassigment operator
707 # On success return the static type of the result of the reassigment operator
708 # Else display an error and return null
709 private fun do_rvalue_typing
(v
: TypingVisitor, type_lvalue
: nullable MMType): nullable MMType
711 if type_lvalue
== null then
714 var name
= n_assign_op
.method_name
715 if type_lvalue
isa MMTypeNone then
716 v
.error
(self, "Error: Method '{name}' call on 'null'.")
719 var lc
= type_lvalue
.local_class
720 if not lc
.has_global_property_by_name
(name
) then
721 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
724 var prop
= lc
.select_method
(name
)
725 prop
.global
.check_visibility
(v
, self, v
.module, false)
726 var psig
= prop
.signature_for
(type_lvalue
)
727 _assign_method
= prop
728 if not v
.check_conform_expr
(n_value
, psig
[0].not_for_self
) then return null
729 return psig
.return_type
.not_for_self
732 redef fun assign_method
do return _assign_method
.as(not null)
733 var _assign_method
: nullable MMMethod
736 redef class AVarReassignExpr
737 redef fun after_typing
(v
)
739 v
.variable_ctx
.check_is_set
(self, variable
)
740 v
.variable_ctx
.mark_is_set
(variable
)
741 var t
= v
.variable_ctx
.stype
(variable
)
742 var t2
= do_rvalue_typing
(v
, t
)
743 if t2
== null then return
745 # Check the base type
746 var btype
= v
.base_variable_ctx
.stype
(variable
)
747 if not v
.check_expr
(n_value
) then return
748 if btype
!= null and not v
.check_conform
(n_value
, t2
, btype
) then return
751 v
.variable_ctx
.stype
(variable
) = t2
757 redef class AAssignOp
758 fun method_name
: Symbol is abstract
760 redef class APlusAssignOp
761 redef fun method_name
do return once
"+".to_symbol
763 redef class AMinusAssignOp
764 redef fun method_name
do return once
"-".to_symbol
767 redef class ASelfExpr
768 var _variable
: nullable ParamVariable
769 redef fun variable
do return _variable
.as(not null)
771 redef fun its_variable
do return variable
773 redef fun after_typing
(v
)
775 _variable
= v
.self_var
776 _stype
= v
.variable_ctx
.stype
(variable
)
780 redef fun is_self
do return true
783 redef class AImplicitSelfExpr
784 redef fun is_implicit_self
do return true
787 redef class AIfexprExpr
788 redef fun accept_typing
(v
)
790 var old_var_ctx
= v
.variable_ctx
793 v
.enter_visit
(n_expr
)
794 v
.check_conform_expr
(n_expr
, v
.type_bool
)
796 # Prepare 'then' context
797 v
.use_if_true_variable_ctx
(n_expr
)
800 v
.variable_ctx
= v
.variable_ctx
.sub
(n_then
)
801 v
.enter_visit
(n_then
)
803 # Remember what appened in the 'then'
804 var then_var_ctx
= v
.variable_ctx
806 # Prepare 'else' context
807 v
.variable_ctx
= old_var_ctx
808 v
.use_if_false_variable_ctx
(n_expr
)
811 v
.variable_ctx
= v
.variable_ctx
.sub
(n_else
)
812 v
.enter_visit
(n_else
)
814 # Merge 'then' and 'else' contexts
815 old_var_ctx
.merge2
(then_var_ctx
, v
.variable_ctx
, v
.base_variable_ctx
)
816 v
.variable_ctx
= old_var_ctx
818 var stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
819 if stype
== null then return
826 redef class ABoolExpr
827 redef fun after_typing
(v
)
835 redef fun accept_typing
(v
)
837 var old_var_ctx
= v
.variable_ctx
838 var stype
= v
.type_bool
841 # Process left operand
842 v
.enter_visit
(n_expr
)
844 # Prepare right operand context
845 v
.use_if_false_variable_ctx
(n_expr
)
847 # Process right operand
848 v
.enter_visit
(n_expr2
)
849 if n_expr2
.if_false_variable_ctx
!= null then
850 _if_false_variable_ctx
= n_expr2
.if_false_variable_ctx
852 _if_false_variable_ctx
= v
.variable_ctx
855 v
.variable_ctx
= old_var_ctx
857 v
.check_conform_expr
(n_expr
, stype
)
858 v
.check_conform_expr
(n_expr2
, stype
)
865 redef fun accept_typing
(v
)
867 var old_var_ctx
= v
.variable_ctx
868 var stype
= v
.type_bool
870 # Process left operand
871 v
.enter_visit
(n_expr
)
873 # Prepare right operand context
874 v
.use_if_true_variable_ctx
(n_expr
)
876 # Process right operand
877 v
.enter_visit
(n_expr2
)
878 if n_expr2
.if_true_variable_ctx
!= null then
879 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
881 _if_true_variable_ctx
= v
.variable_ctx
884 v
.variable_ctx
= old_var_ctx
886 v
.check_conform_expr
(n_expr
, stype
)
887 v
.check_conform_expr
(n_expr2
, stype
)
894 redef fun after_typing
(v
)
896 v
.check_conform_expr
(n_expr
, v
.type_bool
)
898 # Invert if_true/if_false information
899 _if_false_variable_ctx
= n_expr
._if_true_variable_ctx
900 _if_true_variable_ctx
= n_expr
._if_false_variable_ctx
908 redef fun after_typing
(v
)
915 redef class AFloatExpr
916 redef fun after_typing
(v
)
918 _stype
= v
.type_float
923 redef class ACharExpr
924 redef fun after_typing
(v
)
931 redef class AStringFormExpr
932 redef fun after_typing
(v
)
934 _stype
= v
.type_string
939 redef class ASuperstringExpr
940 redef fun atype
do return _atype
.as(not null)
941 var _atype
: nullable MMType
942 redef fun after_typing
(v
)
944 var stype
= v
.type_string
946 var atype
= v
.type_array
(stype
)
952 redef class ANullExpr
953 redef fun after_typing
(v
)
960 redef class AArrayExpr
961 redef fun after_typing
(v
)
963 var stype
= v
.check_conform_multiexpr
(null, n_exprs
)
964 if stype
!= null then do_typing
(v
, stype
)
967 private fun do_typing
(v
: TypingVisitor, element_type
: MMType)
969 _stype
= v
.type_array
(element_type
)
974 redef class ARangeExpr
975 redef fun after_typing
(v
)
977 if not v
.check_expr
(n_expr
) or not v
.check_expr
(n_expr2
) then return
978 var ntype
= n_expr
.stype
979 var ntype2
= n_expr2
.stype
980 if ntype
< ntype2
then
982 else if not ntype2
< ntype
then
983 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
986 var dtype
= v
.type_discrete
987 if not v
.check_conform_expr
(n_expr
, dtype
) or not v
.check_conform_expr
(n_expr2
, dtype
) then return
988 _stype
= v
.type_range
(ntype
)
993 redef class ASuperExpr
994 redef readable var _init_in_superclass
: nullable MMMethod
995 redef fun compute_raw_arguments
do return n_args
.to_a
996 redef fun after_typing
(v
)
998 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
999 if not precs
.is_empty
then
1000 v
.local_property
.need_super
= true
1001 else if v
.local_property
.global
.is_init
then
1002 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
1003 for p
in base_precs
do
1004 if not p
.global
.is_init
then
1005 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
1007 precs
.add
(v
.local_class
[p
.global
])
1010 if precs
.is_empty
then
1011 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
1013 else if precs
.length
> 1 then
1014 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
1017 var p
= base_precs
.first
1018 assert p
isa MMMethod
1019 _init_in_superclass
= p
1020 register_super_init_call
(v
, p
)
1021 if n_args
.length
> 0 then
1022 var signature
= get_signature
(v
, v
.self_var
.stype
.as(not null), p
, true)
1023 process_signature
(v
, signature
, p
.name
, compute_raw_arguments
)
1026 v
.error
(self, "Error: No super method to call for {v.local_property}.")
1030 if precs
.first
.signature_for
(v
.self_var
.stype
.as(not null)).return_type
!= null then
1031 var stypes
= new Array[MMType]
1032 var stype
: nullable MMType = null
1033 for prop
in precs
do
1034 assert prop
isa MMMethod
1035 var t
= prop
.signature_for
(v
.self_var
.stype
.as(not null)).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
1037 if stype
== null or stype
< t
then
1042 v
.check_conform
(self, t
, stype
.as(not null))
1046 var p
= v
.local_property
1047 assert p
isa MMSrcMethod
1053 redef class AAttrFormExpr
1054 redef fun prop
do return _prop
.as(not null)
1055 var _prop
: nullable MMAttribute
1057 redef fun attr_type
do return _attr_type
.as(not null)
1058 var _attr_type
: nullable MMType
1060 # Compute the attribute accessed
1061 private fun do_typing
(v
: TypingVisitor)
1063 if not v
.check_expr
(n_expr
) then return
1064 var type_recv
= n_expr
.stype
1065 var name
= n_id
.to_symbol
1066 if type_recv
isa MMTypeNone then
1067 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1070 var lc
= type_recv
.local_class
1071 if not lc
.has_global_property_by_name
(name
) then
1072 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
1075 var prop
= lc
.select_attribute
(name
)
1076 if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
1077 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
1080 var at
= prop
.signature_for
(type_recv
).return_type
1081 if not n_expr
.is_self
then at
= at
.not_for_self
1086 redef class AAttrExpr
1087 redef fun after_typing
(v
)
1090 if _prop
== null then return
1096 redef class AAttrAssignExpr
1097 redef fun after_typing
(v
)
1100 if _prop
== null then return
1101 if not v
.check_conform_expr
(n_value
, attr_type
) then return
1106 redef class AAttrReassignExpr
1107 redef fun after_typing
(v
)
1110 if _prop
== null then return
1111 var t
= do_rvalue_typing
(v
, attr_type
)
1112 if t
== null then return
1113 v
.check_conform
(self, t
, n_value
.stype
)
1118 redef class AIssetAttrExpr
1119 redef fun after_typing
(v
)
1122 if _prop
== null then return
1123 if attr_type
.is_nullable
then
1124 v
.error
(self, "Error: isset on a nullable attribute.")
1126 _stype
= v
.type_bool
1131 redef class AAbsAbsSendExpr
1132 # The signature of the called property
1133 redef fun prop_signature
do return _prop_signature
.as(not null)
1134 var _prop_signature
: nullable MMSignature
1136 # Raw arguments used (without vararg transformation)
1137 redef fun raw_arguments
: Array[AExpr]
1139 var res
= _raw_arguments_cache
1143 res
= compute_raw_arguments
1144 if res
== null then res
= new Array[AExpr]
1145 _raw_arguments_cache
= res
1150 var _raw_arguments_cache
: nullable Array[AExpr] = null
1152 fun compute_raw_arguments
: nullable Array[AExpr]
1154 print
"{location} no compute_raw_arguments"
1158 # Check the conformity of a set of arguments `raw_args' to a signature.
1159 private fun process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: nullable Array[AExpr]): Bool
1161 var par_vararg
= psig
.vararg_rank
1162 var par_arity
= psig
.arity
1164 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
1165 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
1166 v
.error
(self, "Error: arity missmatch; prototype is '{name}{psig}'.")
1170 for par_idx
in [0..par_arity
[ do
1172 var par_type
= psig
[par_idx
]
1173 if par_idx
== par_vararg
then
1174 for i
in [0..(raw_arity-par_arity
)] do
1175 a
= raw_args
[arg_idx
]
1176 v
.check_conform_expr
(a
, par_type
)
1177 arg_idx
= arg_idx
+ 1
1180 a
= raw_args
[arg_idx
]
1181 v
.check_conform_expr
(a
, par_type
)
1182 arg_idx
= arg_idx
+ 1
1188 # Check the conformity of a set of defined closures
1189 private fun process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: nullable Array[AClosureDef]): nullable MMType
1191 var t
= psig
.return_type
1192 var cs
= psig
.closures
# Declared closures
1195 if not c
.is_optional
then min_arity
+= 1
1198 if cd
!= null then arity
= cd
.length
1199 if cs
.length
> 0 then
1200 if arity
== 0 and min_arity
> 0 then
1201 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1202 else if arity
> cs
.length
or arity
< min_arity
then
1203 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
1205 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1206 var break_list
: nullable Array[ABreakExpr] = null
1207 if t
!= null then break_list
= new Array[ABreakExpr]
1209 # The n_label, is any in only set on the last decl
1210 var n_label
= if arity
> 0 then cd
[arity-1
].n_label
else null
1212 # Process each closure definition
1213 for i
in [0..arity
[ do
1215 var cni
= cdi
.n_id
.to_symbol
1216 var csi
= psig
.closure_named
(cni
)
1218 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1219 v
.escapable_ctx
.push
(esc
, n_label
)
1220 cdi
.accept_typing2
(v
, esc
)
1222 else if cs
.length
== 1 then
1223 v
.error
(cdi
.n_id
, "Error: no closure named '!{cni}' in {name}; only closure is !{cs.first.name}.")
1225 var a
= new Array[String]
1229 v
.error
(cdi
.n_id
, "Error: no closure named '!{cni}' in {name}; only closures are {a.join(",")}.")
1233 # Check break type conformity
1234 if break_list
!= null then
1235 t
= v
.check_conform_multiexpr
(t
, break_list
)
1238 else if arity
!= 0 then
1239 v
.error
(self, "Error: {name} does not require blocks.")
1245 redef class AAbsSendExpr
1246 # Compute the called global property
1247 private fun do_typing
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, recv_is_self
: Bool, name
: Symbol, raw_args
: nullable Array[AExpr], closure_defs
: nullable Array[AClosureDef])
1249 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
1250 if prop
== null then return
1251 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
1252 if not process_signature
(v
, sig
, prop
.name
, raw_args
) then return
1253 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
1254 if rtype
== null and sig
.return_type
!= null then return
1256 _prop_signature
= sig
1257 _return_type
= rtype
1260 private fun get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): nullable MMMethod
1262 if type_recv
isa MMTypeNone then
1263 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1266 var lc
= type_recv
.local_class
1267 var prop
: nullable MMMethod = null
1268 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
1269 if prop
== null and v
.local_property
.global
.is_init
then
1270 var props
= lc
.super_methods_named
(name
)
1271 if props
.length
> 1 then
1272 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1274 else if props
.length
== 1 then
1275 var p
= lc
[props
.first
.global
]
1276 assert p
isa MMMethod
1281 if prop
== null then
1282 if is_implicit_self
then
1283 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1285 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1292 # Get the signature for a local property and a receiver
1293 private fun get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
1295 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
1296 var psig
= prop
.signature_for
(type_recv
)
1297 if not recv_is_self
then psig
= psig
.not_for_self
1301 # The invoked method (once computed)
1302 redef fun prop
do return _prop
.as(not null)
1303 var _prop
: nullable MMMethod
1305 # The return type (if any) (once computed)
1306 redef readable var _return_type
: nullable MMType
1309 # A possible call of constructor in a super class
1310 # Could be an explicit call or with the 'super' keyword
1311 redef class ASuperInitCall
1312 private fun register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1314 if parent
!= v
.top_block
and self != v
.top_block
then
1315 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1317 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
1318 var prev_class
: nullable MMLocalClass = null
1319 var esic
= v
.explicit_super_init_calls
.as(not null)
1320 if not esic
.is_empty
then
1321 prev_class
= esic
.last
.global
.intro
.local_class
1323 var order
= v
.local_class
.cshe
.reverse_linear_extension
1324 if cla
== v
.local_class
then
1325 v
.explicit_other_init_call
= true
1326 else if not order
.has
(cla
) then
1327 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1328 else if cla
== prev_class
then
1329 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1331 var last_is_found
= prev_class
== null
1333 if c
== prev_class
then
1334 last_is_found
= true
1335 else if c
== cla
then
1336 if not last_is_found
then
1337 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1348 redef class ANewExpr
1349 redef fun compute_raw_arguments
do return n_args
.to_a
1350 redef fun after_typing
(v
)
1352 if not n_type
.is_typed
then return
1353 var t
= n_type
.stype
1354 if t
.local_class
.global
.is_abstract
then
1355 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1359 if n_id
== null then
1360 name
= once
"init".to_symbol
1362 name
= n_id
.to_symbol
1365 do_typing
(v
, t
, false, false, name
, raw_arguments
, null)
1366 if _prop
== null then return
1368 if not prop
.global
.is_init
then
1369 v
.error
(self, "Error: {prop} is not a constructor.")
1378 redef class ASendExpr
1379 # Name of the invoked property
1380 fun name
: Symbol is abstract
1382 # Closure definitions
1383 redef fun closure_defs
: nullable Array[AClosureDef] do return null
1385 redef fun after_typing
(v
)
1390 private fun do_all_typing
(v
: TypingVisitor)
1392 if not v
.check_expr
(n_expr
) then return
1393 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1394 if _prop
== null then return
1395 var prop
= _prop
.as(not null)
1397 if prop
.global
.is_init
then
1398 if not v
.local_property
.global
.is_init
then
1399 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1400 else if not n_expr
.is_self
then
1401 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1403 register_super_init_call
(v
, prop
)
1407 _stype
= return_type
1412 redef class ASendReassignExpr
1413 redef fun read_prop
do return _read_prop
.as(not null)
1414 var _read_prop
: nullable MMMethod
1415 redef fun do_all_typing
(v
)
1417 if not v
.check_expr
(n_expr
) then return
1418 var raw_args
= raw_arguments
1419 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1421 if prop
== null then return
1422 if prop
.global
.is_init
then
1423 if not v
.local_property
.global
.is_init
then
1424 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1425 else if not n_expr
.is_self
then
1426 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1429 var t
= prop
.signature_for
(n_expr
.stype
).return_type
.as(not null)
1430 if not n_expr
.is_self
then t
= t
.not_for_self
1432 var t2
= do_rvalue_typing
(v
, t
)
1433 if t2
== null then return
1434 v
.check_conform
(self, t2
, n_value
.stype
)
1437 raw_args
= raw_args
.to_a
1438 raw_args
.add
(n_value
)
1440 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1441 if prop
.global
.is_init
then
1442 if not v
.local_property
.global
.is_init
then
1443 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1444 else if not n_expr
.is_self
then
1445 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1453 redef class ABinopExpr
1454 redef fun compute_raw_arguments
do return [n_expr2
]
1457 redef fun name
do return once
"==".to_symbol
1458 redef fun after_typing
(v
)
1461 if not n_expr
.is_typed
or not n_expr2
.is_typed
then return
1462 if n_expr
.stype
isa MMTypeNone and not n_expr2
.stype
.is_nullable
or
1463 n_expr2
.stype
isa MMTypeNone and not n_expr
.stype
.is_nullable
then
1464 v
.warning
(self, "Warning: comparaison between null and a non nullable value.")
1467 if n_expr
.stype
isa MMTypeNone then
1468 if n_expr2
.stype
isa MMTypeNone then
1469 v
.warning
(self, "Warning: comparaison between 2 null values.")
1471 try_to_isa
(v
, n_expr2
)
1473 else if n_expr2
.stype
isa MMTypeNone then
1474 try_to_isa
(v
, n_expr
)
1478 private fun try_to_isa
(v
: TypingVisitor, n
: AExpr)
1480 var variable
= n
.its_variable
1481 if variable
!= null and n
.stype
isa MMNullableType then
1482 _if_false_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, n
.stype
.as_notnull
)
1483 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, v
.type_none
)
1488 redef fun name
do return once
"!=".to_symbol
1489 redef fun after_typing
(v
)
1492 if not n_expr
.is_typed
or not n_expr2
.is_typed
then return
1493 if n_expr
.stype
isa MMTypeNone and not n_expr2
.stype
.is_nullable
or
1494 n_expr2
.stype
isa MMTypeNone and not n_expr
.stype
.is_nullable
then
1495 v
.warning
(self, "Warning: comparaison between null and a non nullable value.")
1498 if n_expr
.stype
isa MMTypeNone then
1499 if n_expr2
.stype
isa MMTypeNone then
1500 v
.warning
(self, "Warning: comparaison between 2 null values.")
1502 try_to_isa
(v
, n_expr2
)
1504 else if n_expr2
.stype
isa MMTypeNone then
1505 try_to_isa
(v
, n_expr
)
1509 private fun try_to_isa
(v
: TypingVisitor, n
: AExpr)
1511 var variable
= n
.its_variable
1512 if variable
!= null and n
.stype
isa MMNullableType then
1513 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, n
.stype
.as_notnull
)
1514 _if_false_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, v
.type_none
)
1519 redef fun name
do return once
"<".to_symbol
1522 redef fun name
do return once
"<=".to_symbol
1525 redef fun name
do return once
">".to_symbol
1528 redef fun name
do return once
">=".to_symbol
1530 redef class APlusExpr
1531 redef fun name
do return once
"+".to_symbol
1533 redef class AMinusExpr
1534 redef fun name
do return once
"-".to_symbol
1536 redef class AStarshipExpr
1537 redef fun name
do return once
"<=>".to_symbol
1539 redef class AStarExpr
1540 redef fun name
do return once
"*".to_symbol
1542 redef class ASlashExpr
1543 redef fun name
do return once
"/".to_symbol
1545 redef class APercentExpr
1546 redef fun name
do return once
"%".to_symbol
1549 redef class AUminusExpr
1550 redef fun name
do return once
"unary -".to_symbol
1551 redef fun compute_raw_arguments
do return null
1554 redef class ACallFormExpr
1555 redef fun after_typing
(v
)
1557 if n_expr
.is_implicit_self
then
1558 var name
= n_id
.to_symbol
1559 var variable
= v
.variable_ctx
[name
]
1560 if variable
!= null then
1562 if variable
isa ClosureVariable then
1563 n
= new AClosureCallExpr.init_aclosurecallexpr
(n_id
, n_args
, n_closure_defs
)
1564 n
._variable
= variable
1566 if not n_args
.is_empty
then
1567 v
.error
(self, "Error: {name} is variable, not a function.")
1570 n
= variable_create
(variable
)
1571 n
._variable
= variable
1582 redef fun closure_defs
1584 if n_closure_defs
.is_empty
then
1587 return n_closure_defs
.to_a
1591 # Create a variable acces corresponding to the call form
1592 fun variable_create
(variable
: Variable): AVarFormExpr is abstract
1595 redef class ACallExpr
1596 redef fun variable_create
(variable
)
1598 return new AVarExpr.init_avarexpr
(n_id
)
1601 redef fun name
do return n_id
.to_symbol
1602 redef fun compute_raw_arguments
do return n_args
.to_a
1605 redef class ACallAssignExpr
1606 redef fun variable_create
(variable
)
1608 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1611 redef fun name
do return (n_id
.text
+ "=").to_symbol
1612 redef fun compute_raw_arguments
do
1613 var res
= n_args
.to_a
1619 redef class ACallReassignExpr
1620 redef fun variable_create
(variable
)
1622 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1625 redef fun name
do return n_id
.to_symbol
1626 redef fun compute_raw_arguments
do return n_args
.to_a
1629 redef class ABraExpr
1630 redef fun name
do return once
"[]".to_symbol
1631 redef fun compute_raw_arguments
do return n_args
.to_a
1632 redef fun closure_defs
1634 if n_closure_defs
.is_empty
then
1637 return n_closure_defs
.to_a
1642 redef class ABraAssignExpr
1643 redef fun name
do return once
"[]=".to_symbol
1644 redef fun compute_raw_arguments
do
1645 var res
= n_args
.to_a
1651 redef class ABraReassignExpr
1652 redef fun name
do return once
"[]".to_symbol
1653 redef fun compute_raw_arguments
do return n_args
.to_a
1656 redef class AInitExpr
1657 redef fun name
do return once
"init".to_symbol
1658 redef fun compute_raw_arguments
do return n_args
.to_a
1661 redef class AClosureCallExpr
1662 var _variable
: nullable ClosureVariable
1663 redef fun variable
do return _variable
.as(not null)
1664 redef fun compute_raw_arguments
do return n_args
.to_a
1666 redef fun after_typing
(v
)
1669 if va
.closure
.is_break
then v
.variable_ctx
.unreash
= true
1670 var sig
= va
.closure
.signature
1671 var s
= process_signature
(v
, sig
, n_id
.to_symbol
, compute_raw_arguments
)
1672 if not n_closure_defs
.is_empty
then
1673 process_closures
(v
, sig
, n_id
.to_symbol
, n_closure_defs
.to_a
)
1675 if not s
then return
1676 _prop_signature
= sig
1677 _stype
= sig
.return_type
1682 redef class AClosureId
1683 fun to_symbol
: Symbol is abstract
1685 redef class ASimpleClosureId
1686 redef fun to_symbol
: Symbol do return n_id
.to_symbol
1688 redef class ABreakClosureId
1689 redef fun to_symbol
: Symbol do return n_kwbreak
.to_symbol
1692 redef class AClosureDef
1693 var _closure
: nullable MMClosure
1694 redef fun closure
do return _closure
.as(not null)
1696 # The corresponding escapable object
1697 readable var _escapable
: nullable EscapableBlock
1699 var _accept_typing2
: Bool = false
1700 redef fun accept_typing
(v
)
1702 # Typing is deferred, wait accept_typing2(v)
1703 if _accept_typing2
then super
1706 private fun accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure)
1710 var sig
= esc
.closure
.signature
1711 if sig
.arity
!= n_ids
.length
then
1712 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_ids.length} found.")
1716 _closure
= esc
.closure
1718 var old_var_ctx
= v
.variable_ctx
1719 var old_base_var_ctx
= v
.base_variable_ctx
1720 v
.base_variable_ctx
= v
.variable_ctx
1721 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
1722 variables
= new Array[AutoVariable]
1723 for i
in [0..n_ids
.length
[ do
1724 var va
= new AutoVariable(n_ids
[i
].to_symbol
, n_ids
[i
])
1727 v
.variable_ctx
.add
(va
)
1730 _accept_typing2
= true
1733 if v
.variable_ctx
.unreash
== false then
1734 if closure
.signature
.return_type
!= null then
1735 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
1736 else if closure
.is_break
and esc
.break_list
!= null then
1737 v
.error
(self, "Control error: Reached end of break block (a 'break' with a value was expected).")
1740 v
.variable_ctx
= old_var_ctx
1741 v
.base_variable_ctx
= old_base_var_ctx
1745 class ATypeCheckExpr
1747 private fun check_expr_cast
(v
: TypingVisitor, n_expr
: AExpr, n_type
: AType)
1749 if not v
.check_expr
(n_expr
) then return
1750 if not n_type
.is_typed
then return
1751 var etype
= n_expr
.stype
1752 var ttype
= n_type
.stype
1753 if etype
== ttype
then
1754 v
.warning
(self, "Warning: Expression is already a {ttype}.")
1755 else if etype
< ttype
then
1756 v
.warning
(self, "Warning: Expression is already a {ttype} since it is a {etype}.")
1757 else if etype
isa MMTypeNone then
1758 # ttype is not nullable because of prevous test
1759 v
.warning
(self, "Warning: Expression is null therefore cannot be a {ttype}.")
1760 else if etype
.is_nullable
and etype
.as_notnull
== ttype
then
1761 if ttype
isa MMTypeFormal and ttype
.bound
.is_nullable
then
1762 # No warning in this case since with
1763 # type T: nullable A
1765 # 'x.as(not null)' != 'x.as(T)'
1766 # 'x != null' != 'x isa T'
1767 else if self isa AIsaExpr then
1768 v
.warning
(self, "Warning: Prefer '!= null'.")
1770 v
.warning
(self, "Warning: Prefer '.as(not null)'.")
1776 redef class AIsaExpr
1777 special ATypeCheckExpr
1778 redef fun after_typing
(v
)
1780 check_expr_cast
(v
, n_expr
, n_type
)
1781 if not n_type
.is_typed
then return
1782 var variable
= n_expr
.its_variable
1783 if variable
!= null then
1784 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, n_type
.stype
)
1786 _stype
= v
.type_bool
1791 redef class AAsCastExpr
1792 special ATypeCheckExpr
1793 redef fun after_typing
(v
)
1795 check_expr_cast
(v
, n_expr
, n_type
)
1796 if not n_type
.is_typed
then return
1797 _stype
= n_type
.stype
1798 _is_typed
= _stype
!= null
1802 redef class AAsNotnullExpr
1803 redef fun after_typing
(v
)
1805 if not v
.check_expr
(n_expr
) then return
1806 var t
= n_expr
.stype
1807 if t
isa MMTypeNone then
1808 v
.error
(n_expr
, "Type error: 'as(not null)' on 'null' value.")
1810 else if not t
.is_nullable
then
1811 v
.warning
(n_expr
, "Warning: 'as(not null)' on non nullable type.")
1813 _stype
= n_expr
.stype
.as_notnull
1818 redef class AProxyExpr
1819 redef fun after_typing
(v
)
1821 if not n_expr
.is_typed
then return
1823 if n_expr
.is_statement
then return
1824 _stype
= n_expr
.stype
1828 redef class AOnceExpr
1829 redef fun accept_typing
(v
)
1831 if v
.once_count
> 0 then
1832 v
.warning
(self, "Useless once in a once expression.")
1834 v
.once_count
= v
.once_count
+ 1
1838 v
.once_count
= v
.once_count
- 1