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 escapable
= new EscapableClosure(self, variable
.closure
, null)
275 _escapable
= escapable
276 v
.escapable_ctx
.push
(escapable
, null)
280 if n_expr
!= null then
281 if v
.variable_ctx
.unreash
== false then
282 if variable
.closure
.signature
.return_type
!= null then
283 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
284 else if variable
.closure
.is_break
then
285 v
.error
(self, "Control error: Reached end of break block (an 'abort' was expected).")
290 old_var_ctx
.merge
(v
.variable_ctx
)
291 v
.variable_ctx
= old_var_ctx
292 v
.base_variable_ctx
= old_base_var_ctx
298 redef fun stype
: MMType do return _stype
.as(not null)
299 redef fun is_typed
: Bool do return _stype
!= null
300 var _stype
: nullable MMType
302 redef fun after_typing
(v
)
304 _stype
= get_stype
(v
)
309 redef readable var _is_typed
: Bool = false
310 redef fun is_statement
: Bool do return _stype
== null
314 print
"{location}: not is_typed"
318 print
"{location}: is_statement"
321 return _stype
.as(not null)
323 var _stype
: nullable MMType
325 redef fun after_typing
(v
)
327 # Default behavior is to be happy
331 # Is the expression the implicit receiver
332 fun is_implicit_self
: Bool do return false
334 # Is the expression the current receiver (implicit or explicit)
335 fun is_self
: Bool do return false
337 # The variable accessed is any
338 fun its_variable
: nullable Variable do return null
340 # The variable type information if current boolean expression is true
341 readable private var _if_true_variable_ctx
: nullable VariableContext
343 # The variable type information if current boolean expression is false
344 readable private var _if_false_variable_ctx
: nullable VariableContext
347 redef class AVardeclExpr
348 var _variable
: nullable VarVariable
349 redef fun variable
do return _variable
.as(not null)
351 redef fun after_typing
(v
)
353 var va
= new VarVariable(n_id
.to_symbol
, n_id
)
355 v
.variable_ctx
.add
(va
)
356 if n_expr
!= null then v
.variable_ctx
.mark_is_set
(va
)
358 if n_type
!= null then
359 if not n_type
.is_typed
then return
360 va
.stype
= n_type
.stype
361 if n_expr
!= null then
362 v
.check_conform_expr
(n_expr
.as(not null), va
.stype
)
365 if not v
.check_expr
(n_expr
.as(not null)) then return
366 va
.stype
= n_expr
.stype
372 redef class ABlockExpr
373 redef fun accept_typing
(v
)
375 var old_var_ctx
= v
.variable_ctx
376 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
379 if v
.variable_ctx
.unreash
and not v
.variable_ctx
.already_unreash
then
380 v
.variable_ctx
.already_unreash
= true
381 v
.warning
(e
, "Warning: unreachable statement.")
386 old_var_ctx
.merge
(v
.variable_ctx
)
387 v
.variable_ctx
= old_var_ctx
392 redef class AReturnExpr
393 redef fun after_typing
(v
)
395 v
.variable_ctx
.unreash
= true
396 var t
= v
.local_property
.signature
.return_type
398 if e
== null and t
!= null then
399 v
.error
(self, "Error: Return without value in a function.")
400 else if e
!= null and t
== null then
401 v
.error
(self, "Error: Return with value in a procedure.")
402 else if e
!= null and t
!= null then
403 v
.check_conform_expr
(e
, t
)
409 redef class AContinueExpr
410 redef fun after_typing
(v
)
412 v
.variable_ctx
.unreash
= true
413 var esc
= compute_escapable_block
(v
.escapable_ctx
)
414 if esc
== null then return
416 if esc
.is_break_block
then
417 v
.error
(self, "Error: cannot 'continue', only 'break'.")
421 var t
= esc
.continue_stype
422 if n_expr
== null and t
!= null then
423 v
.error
(self, "Error: continue with a value required in this block.")
424 else if n_expr
!= null and t
== null then
425 v
.error
(self, "Error: continue without value required in this block.")
426 else if n_expr
!= null and t
!= null then
427 v
.check_conform_expr
(n_expr
.as(not null), t
)
433 redef class ABreakExpr
434 redef fun after_typing
(v
)
436 v
.variable_ctx
.unreash
= true
437 var esc
= compute_escapable_block
(v
.escapable_ctx
)
438 if esc
== null then return
440 var bl
= esc
.break_list
441 if n_expr
== null and bl
!= null then
442 v
.error
(self, "Error: break with a value required in this block.")
443 else if n_expr
!= null and bl
== null then
444 v
.error
(self, "Error: break without value required in this block.")
445 else if n_expr
!= null and bl
!= null then
446 # Typing check can only be done later
447 bl
.add
(n_expr
.as(not null))
453 redef class AAbortExpr
454 redef fun after_typing
(v
)
456 v
.variable_ctx
.unreash
= true
462 # The corresponding escapable block
463 readable var _escapable
: nullable EscapableBlock
465 redef fun accept_typing
(v
)
467 var escapable
= new BreakOnlyEscapableBlock(self)
468 _escapable
= escapable
469 v
.escapable_ctx
.push
(escapable
, n_label
)
479 redef fun accept_typing
(v
)
481 var old_var_ctx
= v
.variable_ctx
482 v
.enter_visit
(n_expr
)
483 v
.check_conform_expr
(n_expr
, v
.type_bool
)
485 # Prepare 'then' context
486 v
.use_if_true_variable_ctx
(n_expr
)
489 if n_then
!= null then
490 v
.variable_ctx
= v
.variable_ctx
.sub
(n_then
.as(not null))
491 v
.enter_visit
(n_then
)
494 # Remember what appened in the 'then'
495 var then_var_ctx
= v
.variable_ctx
497 # Prepare 'else' context
498 v
.variable_ctx
= old_var_ctx
499 v
.use_if_false_variable_ctx
(n_expr
)
502 if n_else
!= null then
503 v
.variable_ctx
= v
.variable_ctx
.sub
(n_else
.as(not null))
504 v
.enter_visit
(n_else
)
507 # Merge 'then' and 'else' contexts
508 old_var_ctx
.merge2
(then_var_ctx
, v
.variable_ctx
, v
.base_variable_ctx
)
509 v
.variable_ctx
= old_var_ctx
514 redef class AWhileExpr
515 # The corresponding escapable block
516 readable var _escapable
: nullable EscapableBlock
518 redef fun accept_typing
(v
)
520 var escapable
= new EscapableBlock(self)
521 _escapable
= escapable
522 v
.escapable_ctx
.push
(escapable
, n_label
)
523 var old_var_ctx
= v
.variable_ctx
524 var old_base_var_ctx
= v
.base_variable_ctx
525 v
.base_variable_ctx
= v
.variable_ctx
526 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
529 v
.enter_visit
(n_expr
)
530 v
.check_conform_expr
(n_expr
, v
.type_bool
)
532 # Prepare inside context (assert cond)
533 v
.use_if_true_variable_ctx
(n_expr
)
536 if n_block
!= null then
537 v
.variable_ctx
= v
.variable_ctx
.sub
(n_block
.as(not null))
538 v
.enter_visit
(n_block
)
541 v
.variable_ctx
= old_var_ctx
542 v
.base_variable_ctx
= old_base_var_ctx
548 redef class ALoopExpr
549 # The corresponding escapable block
550 readable var _escapable
: nullable EscapableBlock
552 redef fun accept_typing
(v
)
554 var escapable
= new EscapableBlock(self)
555 _escapable
= escapable
556 v
.escapable_ctx
.push
(escapable
, n_label
)
557 var old_var_ctx
= v
.variable_ctx
558 var old_base_var_ctx
= v
.base_variable_ctx
559 v
.base_variable_ctx
= v
.variable_ctx
560 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
563 if n_block
!= null then
564 v
.variable_ctx
= v
.variable_ctx
.sub
(n_block
.as(not null))
565 v
.enter_visit
(n_block
)
568 v
.variable_ctx
= old_var_ctx
569 v
.base_variable_ctx
= old_base_var_ctx
576 var _variable
: nullable AutoVariable
577 redef fun variable
do return _variable
.as(not null)
579 # The corresponding escapable block
580 readable var _escapable
: nullable EscapableBlock
582 redef fun accept_typing
(v
)
584 var escapable
= new EscapableBlock(self)
585 _escapable
= escapable
586 v
.escapable_ctx
.push
(escapable
, n_label
)
588 var old_var_ctx
= v
.variable_ctx
589 var old_base_var_ctx
= v
.base_variable_ctx
590 v
.base_variable_ctx
= v
.variable_ctx
591 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
592 var va
= new AutoVariable(n_id
.to_symbol
, n_id
)
594 v
.variable_ctx
.add
(va
)
597 v
.enter_visit
(n_expr
)
599 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then return
600 var expr_type
= n_expr
.stype
603 var meth_iterator
= v
.get_method
(expr_type
, once
"iterator".to_symbol
)
604 var iter_type
= meth_iterator
.signature_for
(expr_type
).return_type
.as(not null)
605 var meth_item
= v
.get_method
(iter_type
, once
("item".to_symbol
))
606 var va_stype
= meth_item
.signature_for
(iter_type
).return_type
.as(not null)
607 if not n_expr
.is_self
then va_stype
= va_stype
.not_for_self
611 if n_block
!= null then v
.enter_visit
(n_block
)
614 v
.variable_ctx
= old_var_ctx
615 v
.base_variable_ctx
= old_base_var_ctx
621 redef class AAssertExpr
622 redef fun accept_typing
(v
)
625 v
.enter_visit
(n_expr
)
626 v
.check_conform_expr
(n_expr
, v
.type_bool
)
628 # Process optional 'else' part
629 if n_else
!= null then
630 var old_var_ctx
= v
.variable_ctx
631 v
.use_if_false_variable_ctx
(n_expr
)
632 v
.enter_visit
(n_else
)
633 v
.variable_ctx
= old_var_ctx
637 v
.use_if_true_variable_ctx
(n_expr
)
642 redef class AVarFormExpr
643 var _variable
: nullable Variable
644 redef fun variable
do return _variable
.as(not null)
648 redef fun its_variable
do return variable
650 redef fun after_typing
(v
)
652 v
.variable_ctx
.check_is_set
(self, variable
)
653 _stype
= v
.variable_ctx
.stype
(variable
)
654 _is_typed
= _stype
!= null
658 redef class AVarAssignExpr
659 redef fun after_typing
(v
)
661 v
.variable_ctx
.mark_is_set
(variable
)
662 var t
= v
.variable_ctx
.stype
(variable
)
664 # Check the base type
665 var btype
= v
.base_variable_ctx
.stype
(variable
)
666 if not v
.check_conform_expr
(n_value
, btype
) then return
669 v
.variable_ctx
.stype
(variable
) = n_value
.stype
675 redef class AReassignFormExpr
676 # Compute and check method used through the reassigment operator
677 # On success return the static type of the result of the reassigment operator
678 # Else display an error and return null
679 private fun do_rvalue_typing
(v
: TypingVisitor, type_lvalue
: nullable MMType): nullable MMType
681 if type_lvalue
== null then
684 var name
= n_assign_op
.method_name
685 var lc
= type_lvalue
.local_class
686 if not lc
.has_global_property_by_name
(name
) then
687 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
690 var prop
= lc
.select_method
(name
)
691 prop
.global
.check_visibility
(v
, self, v
.module, false)
692 var psig
= prop
.signature_for
(type_lvalue
)
693 _assign_method
= prop
694 if not v
.check_conform_expr
(n_value
, psig
[0].not_for_self
) then return null
695 return psig
.return_type
.not_for_self
698 redef fun assign_method
do return _assign_method
.as(not null)
699 var _assign_method
: nullable MMMethod
702 redef class AVarReassignExpr
703 redef fun after_typing
(v
)
705 v
.variable_ctx
.check_is_set
(self, variable
)
706 v
.variable_ctx
.mark_is_set
(variable
)
707 var t
= v
.variable_ctx
.stype
(variable
)
708 var t2
= do_rvalue_typing
(v
, t
)
709 if t2
== null then return
711 # Check the base type
712 var btype
= v
.base_variable_ctx
.stype
(variable
)
713 if not v
.check_conform
(n_value
, t2
, btype
) then return
716 v
.variable_ctx
.stype
(variable
) = t2
722 redef class AAssignOp
723 fun method_name
: Symbol is abstract
725 redef class APlusAssignOp
726 redef fun method_name
do return once
"+".to_symbol
728 redef class AMinusAssignOp
729 redef fun method_name
do return once
"-".to_symbol
732 redef class ASelfExpr
733 var _variable
: nullable ParamVariable
734 redef fun variable
do return _variable
.as(not null)
736 redef fun its_variable
do return variable
738 redef fun after_typing
(v
)
740 _variable
= v
.self_var
741 _stype
= v
.variable_ctx
.stype
(variable
)
745 redef fun is_self
do return true
748 redef class AImplicitSelfExpr
749 redef fun is_implicit_self
do return true
752 redef class AIfexprExpr
753 redef fun accept_typing
(v
)
755 var old_var_ctx
= v
.variable_ctx
758 v
.enter_visit
(n_expr
)
759 v
.check_conform_expr
(n_expr
, v
.type_bool
)
761 # Prepare 'then' context
762 v
.use_if_true_variable_ctx
(n_expr
)
765 v
.enter_visit
(n_then
)
767 # Prepare 'else' context
768 v
.variable_ctx
= old_var_ctx
769 v
.use_if_false_variable_ctx
(n_expr
)
772 v
.enter_visit
(n_else
)
774 var stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
775 if stype
== null then return
782 redef class ABoolExpr
783 redef fun after_typing
(v
)
791 redef fun accept_typing
(v
)
793 var old_var_ctx
= v
.variable_ctx
794 var stype
= v
.type_bool
797 # Process left operand
798 v
.enter_visit
(n_expr
)
800 # Prepare right operand context
801 v
.use_if_false_variable_ctx
(n_expr
)
803 # Process right operand
804 v
.enter_visit
(n_expr2
)
805 if n_expr2
.if_false_variable_ctx
!= null then
806 _if_false_variable_ctx
= n_expr2
.if_false_variable_ctx
808 _if_false_variable_ctx
= v
.variable_ctx
811 v
.variable_ctx
= old_var_ctx
813 v
.check_conform_expr
(n_expr
, stype
)
814 v
.check_conform_expr
(n_expr2
, stype
)
821 redef fun accept_typing
(v
)
823 var old_var_ctx
= v
.variable_ctx
824 var stype
= v
.type_bool
826 # Process left operand
827 v
.enter_visit
(n_expr
)
829 # Prepare right operand context
830 v
.use_if_true_variable_ctx
(n_expr
)
832 # Process right operand
833 v
.enter_visit
(n_expr2
)
834 if n_expr2
.if_true_variable_ctx
!= null then
835 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
837 _if_true_variable_ctx
= v
.variable_ctx
840 v
.variable_ctx
= old_var_ctx
842 v
.check_conform_expr
(n_expr
, stype
)
843 v
.check_conform_expr
(n_expr2
, stype
)
850 redef fun after_typing
(v
)
852 v
.check_conform_expr
(n_expr
, v
.type_bool
)
854 # Invert if_true/if_false information
855 _if_false_variable_ctx
= n_expr
._if_true_variable_ctx
856 _if_true_variable_ctx
= n_expr
._if_false_variable_ctx
864 redef fun after_typing
(v
)
871 redef class AFloatExpr
872 redef fun after_typing
(v
)
874 _stype
= v
.type_float
879 redef class ACharExpr
880 redef fun after_typing
(v
)
887 redef class AStringFormExpr
888 redef fun after_typing
(v
)
890 _stype
= v
.type_string
895 redef class ASuperstringExpr
896 redef fun atype
do return _atype
.as(not null)
897 var _atype
: nullable MMType
898 redef fun after_typing
(v
)
900 var stype
= v
.type_string
902 var atype
= v
.type_array
(stype
)
908 redef class ANullExpr
909 redef fun after_typing
(v
)
916 redef class AArrayExpr
917 redef fun after_typing
(v
)
919 var stype
= v
.check_conform_multiexpr
(null, n_exprs
)
920 if stype
!= null then do_typing
(v
, stype
)
923 private fun do_typing
(v
: TypingVisitor, element_type
: MMType)
925 _stype
= v
.type_array
(element_type
)
930 redef class ARangeExpr
931 redef fun after_typing
(v
)
933 if not v
.check_expr
(n_expr
) or not v
.check_expr
(n_expr2
) then return
934 var ntype
= n_expr
.stype
935 var ntype2
= n_expr2
.stype
936 if ntype
< ntype2
then
938 else if not ntype2
< ntype
then
939 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
942 var dtype
= v
.type_discrete
943 if not v
.check_conform_expr
(n_expr
, dtype
) or not v
.check_conform_expr
(n_expr2
, dtype
) then return
944 _stype
= v
.type_range
(ntype
)
949 redef class ASuperExpr
950 redef readable var _init_in_superclass
: nullable MMMethod
951 redef fun compute_raw_arguments
do return n_args
.to_a
952 redef fun after_typing
(v
)
954 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
955 if not precs
.is_empty
then
956 v
.local_property
.need_super
= true
957 else if v
.local_property
.global
.is_init
then
958 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
959 for p
in base_precs
do
960 if not p
.global
.is_init
then
961 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
963 precs
.add
(v
.local_class
[p
.global
])
966 if precs
.is_empty
then
967 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
969 else if precs
.length
> 1 then
970 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
973 var p
= base_precs
.first
974 assert p
isa MMMethod
975 _init_in_superclass
= p
976 register_super_init_call
(v
, p
)
977 if n_args
.length
> 0 then
978 var signature
= get_signature
(v
, v
.self_var
.stype
.as(not null), p
, true)
979 process_signature
(v
, signature
, p
.name
, compute_raw_arguments
)
982 v
.error
(self, "Error: No super method to call for {v.local_property}.")
986 if precs
.first
.signature_for
(v
.self_var
.stype
.as(not null)).return_type
!= null then
987 var stypes
= new Array[MMType]
988 var stype
: nullable MMType = null
990 assert prop
isa MMMethod
991 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
)
993 if stype
== null or stype
< t
then
998 v
.check_conform
(self, t
, stype
.as(not null))
1002 var p
= v
.local_property
1003 assert p
isa MMSrcMethod
1009 redef class AAttrFormExpr
1010 redef fun prop
do return _prop
.as(not null)
1011 var _prop
: nullable MMAttribute
1013 redef fun attr_type
do return _attr_type
.as(not null)
1014 var _attr_type
: nullable MMType
1016 # Compute the attribute accessed
1017 private fun do_typing
(v
: TypingVisitor)
1019 if not v
.check_expr
(n_expr
) then return
1020 var type_recv
= n_expr
.stype
1021 var name
= n_id
.to_symbol
1022 var lc
= type_recv
.local_class
1023 if not lc
.has_global_property_by_name
(name
) then
1024 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
1027 var prop
= lc
.select_attribute
(name
)
1028 if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
1029 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
1032 var at
= prop
.signature_for
(type_recv
).return_type
1033 if not n_expr
.is_self
then at
= at
.not_for_self
1038 redef class AAttrExpr
1039 redef fun after_typing
(v
)
1042 if _prop
== null then return
1048 redef class AAttrAssignExpr
1049 redef fun after_typing
(v
)
1052 if _prop
== null then return
1053 if not v
.check_conform_expr
(n_value
, attr_type
) then return
1058 redef class AAttrReassignExpr
1059 redef fun after_typing
(v
)
1062 if _prop
== null then return
1063 var t
= do_rvalue_typing
(v
, attr_type
)
1064 if t
== null then return
1065 v
.check_conform
(self, t
, n_value
.stype
)
1070 redef class AIssetAttrExpr
1071 redef fun after_typing
(v
)
1074 if _prop
== null then return
1075 if attr_type
.is_nullable
then
1076 v
.error
(self, "Error: isset on a nullable attribute.")
1078 _stype
= v
.type_bool
1083 redef class AAbsAbsSendExpr
1084 # The signature of the called property
1085 redef fun prop_signature
do return _prop_signature
.as(not null)
1086 var _prop_signature
: nullable MMSignature
1088 # Raw arguments used (without vararg transformation)
1089 redef fun raw_arguments
: Array[AExpr]
1091 var res
= _raw_arguments_cache
1095 res
= compute_raw_arguments
1096 if res
== null then res
= new Array[AExpr]
1097 _raw_arguments_cache
= res
1102 var _raw_arguments_cache
: nullable Array[AExpr] = null
1104 fun compute_raw_arguments
: nullable Array[AExpr]
1106 print
"{location} no compute_raw_arguments"
1110 # Check the conformity of a set of arguments `raw_args' to a signature.
1111 private fun process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: nullable Array[AExpr]): Bool
1113 var par_vararg
= psig
.vararg_rank
1114 var par_arity
= psig
.arity
1116 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
1117 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
1118 v
.error
(self, "Error: arity missmatch; prototype is '{name}{psig}'.")
1122 for par_idx
in [0..par_arity
[ do
1124 var par_type
= psig
[par_idx
]
1125 if par_idx
== par_vararg
then
1126 for i
in [0..(raw_arity-par_arity
)] do
1127 a
= raw_args
[arg_idx
]
1128 v
.check_conform_expr
(a
, par_type
)
1129 arg_idx
= arg_idx
+ 1
1132 a
= raw_args
[arg_idx
]
1133 v
.check_conform_expr
(a
, par_type
)
1134 arg_idx
= arg_idx
+ 1
1140 # Check the conformity of a set of defined closures
1141 private fun process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: nullable Array[AClosureDef]): nullable MMType
1143 var t
= psig
.return_type
1144 var cs
= psig
.closures
# Declared closures
1147 if not c
.is_optional
then min_arity
+= 1
1150 if cd
!= null then arity
= cd
.length
1151 if cs
.length
> 0 then
1152 if arity
== 0 and min_arity
> 0 then
1153 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1154 else if arity
> cs
.length
or arity
< min_arity
then
1155 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
1157 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1158 var break_list
: nullable Array[ABreakExpr] = null
1159 if t
!= null then break_list
= new Array[ABreakExpr]
1161 # The n_label, is any in only set on the last decl
1162 var n_label
= if arity
> 0 then cd
[arity-1
].n_label
else null
1164 # Process each closure definition
1165 for i
in [0..arity
[ do
1167 var cni
= cdi
.n_id
.to_symbol
1168 var csi
= psig
.closure_named
(cni
)
1170 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1171 v
.escapable_ctx
.push
(esc
, n_label
)
1172 cdi
.accept_typing2
(v
, esc
)
1174 else if cs
.length
== 1 then
1175 v
.error
(cdi
.n_id
, "Error: no closure named '!{cni}' in {name}; only closure is !{cs.first.name}.")
1177 var a
= new Array[String]
1181 v
.error
(cdi
.n_id
, "Error: no closure named '!{cni}' in {name}; only closures are {a.join(",")}.")
1185 # Check break type conformity
1186 if break_list
!= null then
1187 t
= v
.check_conform_multiexpr
(t
, break_list
)
1190 else if arity
!= 0 then
1191 v
.error
(self, "Error: {name} does not require blocks.")
1197 redef class AAbsSendExpr
1198 # Compute the called global property
1199 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])
1201 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
1202 if prop
== null then return
1203 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
1204 if not process_signature
(v
, sig
, prop
.name
, raw_args
) then return
1205 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
1206 if rtype
== null and sig
.return_type
!= null then return
1208 _prop_signature
= sig
1209 _return_type
= rtype
1212 private fun get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): nullable MMMethod
1214 var lc
= type_recv
.local_class
1215 var prop
: nullable MMMethod = null
1216 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
1217 if prop
== null and v
.local_property
.global
.is_init
then
1218 var props
= type_recv
.local_class
.super_methods_named
(name
)
1219 if props
.length
> 1 then
1220 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1222 else if props
.length
== 1 then
1223 var p
= type_recv
.local_class
[props
.first
.global
]
1224 assert p
isa MMMethod
1229 if prop
== null then
1230 if is_implicit_self
then
1231 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1233 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1240 # Get the signature for a local property and a receiver
1241 private fun get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
1243 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
1244 var psig
= prop
.signature_for
(type_recv
)
1245 if not recv_is_self
then psig
= psig
.not_for_self
1249 # The invoked method (once computed)
1250 redef fun prop
do return _prop
.as(not null)
1251 var _prop
: nullable MMMethod
1253 # The return type (if any) (once computed)
1254 redef readable var _return_type
: nullable MMType
1257 # A possible call of constructor in a super class
1258 # Could be an explicit call or with the 'super' keyword
1259 redef class ASuperInitCall
1260 private fun register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1262 if parent
!= v
.top_block
and self != v
.top_block
then
1263 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1265 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
1266 var prev_class
: nullable MMLocalClass = null
1267 var esic
= v
.explicit_super_init_calls
.as(not null)
1268 if not esic
.is_empty
then
1269 prev_class
= esic
.last
.global
.intro
.local_class
1271 var order
= v
.local_class
.cshe
.reverse_linear_extension
1272 if cla
== v
.local_class
then
1273 v
.explicit_other_init_call
= true
1274 else if not order
.has
(cla
) then
1275 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1276 else if cla
== prev_class
then
1277 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1279 var last_is_found
= prev_class
== null
1281 if c
== prev_class
then
1282 last_is_found
= true
1283 else if c
== cla
then
1284 if not last_is_found
then
1285 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1296 redef class ANewExpr
1297 redef fun compute_raw_arguments
do return n_args
.to_a
1298 redef fun after_typing
(v
)
1300 if not n_type
.is_typed
then return
1301 var t
= n_type
.stype
1302 if t
.local_class
.global
.is_abstract
then
1303 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1307 if n_id
== null then
1308 name
= once
"init".to_symbol
1310 name
= n_id
.to_symbol
1313 do_typing
(v
, t
, false, false, name
, raw_arguments
, null)
1314 if _prop
== null then return
1316 if not prop
.global
.is_init
then
1317 v
.error
(self, "Error: {prop} is not a constructor.")
1326 redef class ASendExpr
1327 # Name of the invoked property
1328 fun name
: Symbol is abstract
1330 # Closure definitions
1331 redef fun closure_defs
: nullable Array[AClosureDef] do return null
1333 redef fun after_typing
(v
)
1338 private fun do_all_typing
(v
: TypingVisitor)
1340 if not v
.check_expr
(n_expr
) then return
1341 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1342 if _prop
== null then return
1343 var prop
= _prop
.as(not null)
1345 if prop
.global
.is_init
then
1346 if not v
.local_property
.global
.is_init
then
1347 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1348 else if not n_expr
.is_self
then
1349 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1351 register_super_init_call
(v
, prop
)
1355 _stype
= return_type
1360 redef class ASendReassignExpr
1361 redef fun read_prop
do return _read_prop
.as(not null)
1362 var _read_prop
: nullable MMMethod
1363 redef fun do_all_typing
(v
)
1365 if not v
.check_expr
(n_expr
) then return
1366 var raw_args
= raw_arguments
1367 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1369 if prop
== null then return
1370 if prop
.global
.is_init
then
1371 if not v
.local_property
.global
.is_init
then
1372 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1373 else if not n_expr
.is_self
then
1374 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1377 var t
= prop
.signature_for
(n_expr
.stype
).return_type
.as(not null)
1378 if not n_expr
.is_self
then t
= t
.not_for_self
1380 var t2
= do_rvalue_typing
(v
, t
)
1381 if t2
== null then return
1382 v
.check_conform
(self, t2
, n_value
.stype
)
1385 raw_args
= raw_args
.to_a
1386 raw_args
.add
(n_value
)
1388 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
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'.")
1401 redef class ABinopExpr
1402 redef fun compute_raw_arguments
do return [n_expr2
]
1405 redef fun name
do return once
"==".to_symbol
1406 redef fun after_typing
(v
)
1409 if not n_expr
.is_typed
or not n_expr2
.is_typed
then return
1410 if n_expr
.stype
isa MMTypeNone and not n_expr2
.stype
.is_nullable
or
1411 n_expr2
.stype
isa MMTypeNone and not n_expr
.stype
.is_nullable
then
1412 v
.warning
(self, "Warning: comparaison between null and a non nullable value.")
1415 if n_expr
.stype
isa MMTypeNone then
1416 try_to_isa
(v
, n_expr2
)
1417 else if n_expr2
.stype
isa MMTypeNone then
1418 try_to_isa
(v
, n_expr
)
1422 private fun try_to_isa
(v
: TypingVisitor, n
: AExpr)
1424 var variable
= n
.its_variable
1425 if variable
!= null then
1426 _if_false_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, n
.stype
.as_notnull
)
1431 redef fun name
do return once
"!=".to_symbol
1432 redef fun after_typing
(v
)
1435 if not n_expr
.is_typed
or not n_expr2
.is_typed
then return
1436 if n_expr
.stype
isa MMTypeNone and not n_expr2
.stype
.is_nullable
or
1437 n_expr2
.stype
isa MMTypeNone and not n_expr
.stype
.is_nullable
then
1438 v
.warning
(self, "Warning: comparaison between null and a non nullable value.")
1441 if n_expr
.stype
isa MMTypeNone then
1442 try_to_isa
(v
, n_expr2
)
1443 else if n_expr2
.stype
isa MMTypeNone then
1444 try_to_isa
(v
, n_expr
)
1448 private fun try_to_isa
(v
: TypingVisitor, n
: AExpr)
1450 var variable
= n
.its_variable
1451 if variable
!= null then
1452 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, n
.stype
.as_notnull
)
1457 redef fun name
do return once
"<".to_symbol
1460 redef fun name
do return once
"<=".to_symbol
1463 redef fun name
do return once
">".to_symbol
1466 redef fun name
do return once
">=".to_symbol
1468 redef class APlusExpr
1469 redef fun name
do return once
"+".to_symbol
1471 redef class AMinusExpr
1472 redef fun name
do return once
"-".to_symbol
1474 redef class AStarshipExpr
1475 redef fun name
do return once
"<=>".to_symbol
1477 redef class AStarExpr
1478 redef fun name
do return once
"*".to_symbol
1480 redef class ASlashExpr
1481 redef fun name
do return once
"/".to_symbol
1483 redef class APercentExpr
1484 redef fun name
do return once
"%".to_symbol
1487 redef class AUminusExpr
1488 redef fun name
do return once
"unary -".to_symbol
1489 redef fun compute_raw_arguments
do return null
1492 redef class ACallFormExpr
1493 redef fun after_typing
(v
)
1495 if n_expr
.is_implicit_self
then
1496 var name
= n_id
.to_symbol
1497 var variable
= v
.variable_ctx
[name
]
1498 if variable
!= null then
1500 if variable
isa ClosureVariable then
1501 n
= new AClosureCallExpr.init_aclosurecallexpr
(n_id
, n_args
, n_closure_defs
)
1502 n
._variable
= variable
1504 if not n_args
.is_empty
then
1505 v
.error
(self, "Error: {name} is variable, not a function.")
1508 n
= variable_create
(variable
)
1509 n
._variable
= variable
1520 redef fun closure_defs
1522 if n_closure_defs
.is_empty
then
1525 return n_closure_defs
.to_a
1529 # Create a variable acces corresponding to the call form
1530 fun variable_create
(variable
: Variable): AVarFormExpr is abstract
1533 redef class ACallExpr
1534 redef fun variable_create
(variable
)
1536 return new AVarExpr.init_avarexpr
(n_id
)
1539 redef fun name
do return n_id
.to_symbol
1540 redef fun compute_raw_arguments
do return n_args
.to_a
1543 redef class ACallAssignExpr
1544 redef fun variable_create
(variable
)
1546 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1549 redef fun name
do return (n_id
.text
+ "=").to_symbol
1550 redef fun compute_raw_arguments
do
1551 var res
= n_args
.to_a
1557 redef class ACallReassignExpr
1558 redef fun variable_create
(variable
)
1560 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1563 redef fun name
do return n_id
.to_symbol
1564 redef fun compute_raw_arguments
do return n_args
.to_a
1567 redef class ABraExpr
1568 redef fun name
do return once
"[]".to_symbol
1569 redef fun compute_raw_arguments
do return n_args
.to_a
1570 redef fun closure_defs
1572 if n_closure_defs
.is_empty
then
1575 return n_closure_defs
.to_a
1580 redef class ABraAssignExpr
1581 redef fun name
do return once
"[]=".to_symbol
1582 redef fun compute_raw_arguments
do
1583 var res
= n_args
.to_a
1589 redef class ABraReassignExpr
1590 redef fun name
do return once
"[]".to_symbol
1591 redef fun compute_raw_arguments
do return n_args
.to_a
1594 redef class AInitExpr
1595 redef fun name
do return once
"init".to_symbol
1596 redef fun compute_raw_arguments
do return n_args
.to_a
1599 redef class AClosureCallExpr
1600 var _variable
: nullable ClosureVariable
1601 redef fun variable
do return _variable
.as(not null)
1602 redef fun compute_raw_arguments
do return n_args
.to_a
1604 redef fun after_typing
(v
)
1607 if va
.closure
.is_break
then v
.variable_ctx
.unreash
= true
1608 var sig
= va
.closure
.signature
1609 var s
= process_signature
(v
, sig
, n_id
.to_symbol
, compute_raw_arguments
)
1610 if not n_closure_defs
.is_empty
then
1611 process_closures
(v
, sig
, n_id
.to_symbol
, n_closure_defs
.to_a
)
1613 if not s
then return
1614 _prop_signature
= sig
1615 _stype
= sig
.return_type
1620 redef class AClosureId
1621 fun to_symbol
: Symbol is abstract
1623 redef class ASimpleClosureId
1624 redef fun to_symbol
: Symbol do return n_id
.to_symbol
1626 redef class ABreakClosureId
1627 redef fun to_symbol
: Symbol do return n_kwbreak
.to_symbol
1630 redef class AClosureDef
1631 var _closure
: nullable MMClosure
1632 redef fun closure
do return _closure
.as(not null)
1634 # The corresponding escapable object
1635 readable var _escapable
: nullable EscapableBlock
1637 var _accept_typing2
: Bool = false
1638 redef fun accept_typing
(v
)
1640 # Typing is deferred, wait accept_typing2(v)
1641 if _accept_typing2
then super
1644 private fun accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure)
1648 var sig
= esc
.closure
.signature
1649 if sig
.arity
!= n_ids
.length
then
1650 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_ids.length} found.")
1654 _closure
= esc
.closure
1656 var old_var_ctx
= v
.variable_ctx
1657 var old_base_var_ctx
= v
.base_variable_ctx
1658 v
.base_variable_ctx
= v
.variable_ctx
1659 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
1660 variables
= new Array[AutoVariable]
1661 for i
in [0..n_ids
.length
[ do
1662 var va
= new AutoVariable(n_ids
[i
].to_symbol
, n_ids
[i
])
1665 v
.variable_ctx
.add
(va
)
1668 _accept_typing2
= true
1671 if v
.variable_ctx
.unreash
== false then
1672 if closure
.signature
.return_type
!= null then
1673 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
1674 else if closure
.is_break
then
1675 v
.error
(self, "Control error: Reached end of break block (a 'break' was expected).")
1678 v
.variable_ctx
= old_var_ctx
1679 v
.base_variable_ctx
= old_base_var_ctx
1683 class ATypeCheckExpr
1685 private fun check_expr_cast
(v
: TypingVisitor, n_expr
: AExpr, n_type
: AType)
1687 if not v
.check_expr
(n_expr
) then return
1688 if not n_type
.is_typed
then return
1689 var etype
= n_expr
.stype
1690 var ttype
= n_type
.stype
1691 if etype
== ttype
then
1692 v
.warning
(self, "Warning: Expression is already a {ttype}.")
1693 else if etype
< ttype
then
1694 v
.warning
(self, "Warning: Expression is already a {ttype} since it is a {etype}.")
1695 else if etype
.is_nullable
and etype
.as_notnull
== ttype
then
1696 if ttype
isa MMTypeFormal and ttype
.bound
.is_nullable
then
1697 # No warning in this case since with
1698 # type T: nullable A
1700 # 'x.as(not null)' != 'x.as(T)'
1701 # 'x != null' != 'x isa T'
1702 else if self isa AIsaExpr then
1703 v
.warning
(self, "Warning: Prefer '!= null'.")
1705 v
.warning
(self, "Warning: Prefer '.as(not null)'.")
1711 redef class AIsaExpr
1712 special ATypeCheckExpr
1713 redef fun after_typing
(v
)
1715 check_expr_cast
(v
, n_expr
, n_type
)
1716 if not n_type
.is_typed
then return
1717 var variable
= n_expr
.its_variable
1718 if variable
!= null then
1719 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, n_type
.stype
)
1721 _stype
= v
.type_bool
1726 redef class AAsCastExpr
1727 special ATypeCheckExpr
1728 redef fun after_typing
(v
)
1730 check_expr_cast
(v
, n_expr
, n_type
)
1731 if not n_type
.is_typed
then return
1732 _stype
= n_type
.stype
1733 _is_typed
= _stype
!= null
1737 redef class AAsNotnullExpr
1738 redef fun after_typing
(v
)
1740 if not v
.check_expr
(n_expr
) then return
1741 var t
= n_expr
.stype
1742 if t
isa MMTypeNone then
1743 v
.error
(n_expr
, "Type error: 'as(not null)' on 'null' value.")
1745 else if not t
.is_nullable
then
1746 v
.warning
(n_expr
, "Warning: 'as(not null)' on non nullable type.")
1748 _stype
= n_expr
.stype
.as_notnull
1753 redef class AProxyExpr
1754 redef fun after_typing
(v
)
1756 if not n_expr
.is_typed
then return
1758 if n_expr
.is_statement
then return
1759 _stype
= n_expr
.stype
1763 redef class AOnceExpr
1764 redef fun accept_typing
(v
)
1766 if v
.once_count
> 0 then
1767 v
.warning
(self, "Useless once in a once expression.")
1769 v
.once_count
= v
.once_count
+ 1
1773 v
.once_count
= v
.once_count
- 1