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 scoped things (variable, labels, etc.)
47 readable var _scope_ctx
: ScopeContext = new ScopeContext(self)
49 # Current knowledge about control flow
50 fun flow_ctx
: FlowContext do return _flow_ctx
.as(not null)
51 writable var _flow_ctx
: nullable FlowContext
53 # Mark a local variable as set
54 fun mark_is_set
(va
: Variable)
56 if flow_ctx
.is_set
(va
) then return
57 flow_ctx
= flow_ctx
.sub_setvariable
(va
)
60 # Mark the flow context as unreashable
61 fun mark_unreash
(n
: ANode)
63 flow_ctx
= flow_ctx
.sub_unreash
(n
)
66 # Enter in an expression as inside a new local variable scope
67 fun enter_visit_block
(node
: nullable AExpr)
69 if node
== null then return
75 # Non-bypassable knowledge about variables names and types
76 fun base_flow_ctx
: FlowContext do return _base_flow_ctx
.as(not null)
77 writable var _base_flow_ctx
: nullable FlowContext
79 # The current reciever
80 fun self_var
: ParamVariable do return _self_var
.as(not null)
81 writable var _self_var
: nullable ParamVariable
83 # Block of the current method
84 readable writable var _top_block
: nullable AExpr
86 # List of explicit invocation of constructors of super-classes
87 readable writable var _explicit_super_init_calls
: nullable Array[MMMethod]
89 # Is a other constructor of the same class invoked
90 readable writable var _explicit_other_init_call
: Bool = false
92 # Make the if_true_flow_ctx of the expression effective
93 private fun use_if_true_flow_ctx
(e
: AExpr)
95 var ctx
= e
.if_true_flow_ctx
96 if ctx
!= null then flow_ctx
= ctx
99 # Make the if_false_flow_ctx of the expression effective
100 private fun use_if_false_flow_ctx
(e
: AExpr)
102 var ctx
= e
.if_false_flow_ctx
103 if ctx
!= null then flow_ctx
= ctx
106 # Are we inside a default closure definition ?
107 readable writable var _is_default_closure_definition
: Bool = false
109 # Number of nested once
110 readable writable var _once_count
: Int = 0
112 init(tc
, mod
) do super
114 private fun get_default_constructor_for
(n
: ANode, c
: MMLocalClass, prop
: MMSrcMethod): nullable MMMethod
117 #var prop = v.local_property
118 #assert prop isa MMMethod
119 var candidates
= new Array[MMMethod]
120 var false_candidates
= new Array[MMMethod]
121 var parity
= prop
.signature
.arity
122 for g
in c
.global_properties
do
123 if not g
.is_init_for
(c
) then continue
125 var gps
= gp
.signature_for
(c
.get_type
)
126 assert gp
isa MMSrcMethod
127 var garity
= gps
.arity
128 if gp
.name
== prop
.name
then
129 if garity
== 0 or (parity
== garity
and prop
.signature
< gps
) then
132 false_candidates
.add
(gp
)
134 else if garity
== 0 and gp
.name
== once
("init".to_symbol
) then
136 false_candidates
.add
(gp
)
138 false_candidates
.add
(gp
)
141 if candidates
.length
== 1 then
142 return candidates
.first
143 else if candidates
.length
> 0 then
144 var a
= new Array[String]
145 for p
in candidates
do
146 a
.add
("{p.full_name}{p.signature.as(not null)}")
148 v
.error
(n
, "Error: Conflicting default constructor to call for {c}: {a.join(", ")}.")
150 else if false_candidates
.length
> 0 then
151 var a
= new Array[String]
152 for p
in false_candidates
do
153 a
.add
("{p.full_name}{p.signature.as(not null)}")
155 v
.error
(n
, "Error: there is no available compatible constrctor in {c}. Discarded candidates are {a.join(", ")}.")
158 v
.error
(n
, "Error: there is no available compatible constrctor in {c}.")
165 ###############################################################################
168 private fun accept_typing
(v
: TypingVisitor)
170 accept_abs_syntax_visitor
(v
)
173 private fun after_typing
(v
: TypingVisitor) do end
176 redef class AClassdef
177 redef fun accept_typing
(v
)
179 v
.self_var
= new ParamVariable("self".to_symbol
, self)
180 v
.self_var
.stype
= local_class
.get_type
186 redef fun self_var
do return _self_var
.as(not null)
187 var _self_var
: nullable ParamVariable
190 redef class AAttrPropdef
191 redef fun accept_typing
(v
)
193 v
.flow_ctx
= new RootFlowContext(v
, self)
194 v
.base_flow_ctx
= v
.flow_ctx
196 v
.scope_ctx
.push
(self)
197 _self_var
= v
.self_var
199 if n_expr
!= null then
200 v
.check_conform_expr
(n_expr
.as(not null), prop
.signature
.return_type
.as(not null))
206 redef class AMethPropdef
207 redef fun accept_typing
(v
)
209 v
.flow_ctx
= new RootFlowContext(v
, self)
210 v
.base_flow_ctx
= v
.flow_ctx
212 v
.scope_ctx
.push
(self)
213 _self_var
= v
.self_var
219 redef class AConcreteMethPropdef
220 redef fun after_typing
(v
)
223 if not v
.flow_ctx
.unreash
and method
.signature
.return_type
!= null then
224 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
229 redef class AConcreteInitPropdef
230 redef fun accept_typing
(v
)
232 v
.top_block
= n_block
233 v
.explicit_super_init_calls
= explicit_super_init_calls
234 v
.explicit_other_init_call
= false
238 redef fun after_typing
(v
)
241 if v
.explicit_other_init_call
or method
.global
.intro
!= method
then
245 var l
= explicit_super_init_calls
.length
246 var cur_m
: nullable MMMethod = null
247 var cur_c
: nullable MMLocalClass = null
249 cur_m
= explicit_super_init_calls
[i
]
250 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.mmmodule
)
253 while j
< v
.local_class
.cshe
.direct_greaters
.length
do
254 var c
= v
.local_class
.cshe
.direct_greaters
[j
]
255 if c
.global
.is_interface
or c
.global
.is_enum
or c
.global
.is_mixin
then
257 else if cur_c
!= null and (c
.cshe
<= cur_c
or cur_c
.global
.is_mixin
) then
258 if c
== cur_c
then j
+= 1
259 super_init_calls
.add
(cur_m
.as(not null))
262 cur_m
= explicit_super_init_calls
[i
]
263 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.mmmodule
)
269 var p
= v
.get_default_constructor_for
(self, c
, method
)
271 super_init_calls
.add
(p
)
281 redef fun after_typing
(v
)
283 v
.scope_ctx
.add_variable
(variable
)
287 redef class AClosureDecl
288 # The corresponding escapable object
289 readable var _escapable
: nullable EscapableBlock
291 redef fun accept_typing
(v
)
293 # Register the closure for ClosureCallExpr
294 v
.scope_ctx
.add_variable
(variable
)
296 var old_flow_ctx
= v
.flow_ctx
297 var old_base_flow_ctx
= v
.base_flow_ctx
298 v
.base_flow_ctx
= v
.flow_ctx
300 var blist
: nullable Array[AExpr] = null
301 var t
= v
.local_property
.signature
.return_type
302 if t
!= null then blist
= new Array[AExpr]
303 var escapable
= new EscapableClosure(self, variable
.closure
, blist
)
304 _escapable
= escapable
305 v
.scope_ctx
.push_escapable
(escapable
, null)
307 v
.is_default_closure_definition
= true
311 v
.is_default_closure_definition
= false
313 if n_expr
!= null then
314 if v
.flow_ctx
.unreash
== false then
315 if variable
.closure
.signature
.return_type
!= null then
316 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
317 else if variable
.closure
.is_break
and escapable
.break_list
!= null then
318 v
.error
(self, "Control error: Reached end of break block (a 'break' with a value was expected).")
322 if blist
!= null then for x
in blist
do
323 v
.check_conform_expr
(x
, t
)
326 v
.flow_ctx
= old_flow_ctx
327 v
.base_flow_ctx
= old_base_flow_ctx
333 redef fun stype
: MMType do return _stype
.as(not null)
334 redef fun is_typed
: Bool do return _stype
!= null
335 var _stype
: nullable MMType
337 redef fun after_typing
(v
)
339 _stype
= get_stype
(v
)
344 redef readable var _is_typed
: Bool = false
345 redef fun is_statement
: Bool do return _stype
== null
349 print
"{location}: not is_typed"
353 print
"{location}: is_statement"
356 return _stype
.as(not null)
358 var _stype
: nullable MMType
360 redef fun after_typing
(v
)
362 # Default behavior is to be happy
366 # Is the expression the implicit receiver
367 fun is_implicit_self
: Bool do return false
369 # Is the expression the current receiver (implicit or explicit)
370 fun is_self
: Bool do return false
372 # The variable accessed is any
373 fun its_variable
: nullable Variable do return null
375 # The control flow information if current boolean expression is true
376 readable private var _if_true_flow_ctx
: nullable FlowContext
378 # The control flow information if current boolean expression is false
379 readable private var _if_false_flow_ctx
: nullable FlowContext
382 redef class AVardeclExpr
383 var _variable
: nullable VarVariable
384 redef fun variable
do return _variable
.as(not null)
386 redef fun after_typing
(v
)
388 var va
= new VarVariable(n_id
.to_symbol
, n_id
)
390 v
.scope_ctx
.add_variable
(va
)
392 if ne
!= null then v
.mark_is_set
(va
)
394 if n_type
!= null then
395 if not n_type
.is_typed
then return
396 va
.stype
= n_type
.stype
398 v
.check_conform_expr
(ne
, va
.stype
)
400 else if ne
!= null then
401 if not v
.check_expr
(ne
) then return
404 va
.stype
= v
.type_object
.as_nullable
410 redef class ABlockExpr
411 redef fun accept_typing
(v
)
414 if not v
.flow_ctx
.unreash
then
416 else if not v
.flow_ctx
.already_unreash
then
417 v
.flow_ctx
.already_unreash
= true
418 v
.error
(e
, "Error: unreachable statement.")
426 redef class AReturnExpr
427 redef fun after_typing
(v
)
430 var t
= v
.local_property
.signature
.return_type
432 if v
.is_default_closure_definition
then
433 v
.error
(self, "Error: 'return' invalid in default closure definitions. Use 'continue' or 'break'.")
438 if e
== null and t
!= null then
439 v
.error
(self, "Error: Return without value in a function.")
440 else if e
!= null and t
== null then
441 v
.error
(self, "Error: Return with value in a procedure.")
442 else if e
!= null and t
!= null then
443 v
.check_conform_expr
(e
, t
)
449 redef class AContinueExpr
450 redef fun after_typing
(v
)
453 var esc
= compute_escapable_block
(v
.scope_ctx
)
454 if esc
== null then return
456 if esc
.is_break_block
then
457 v
.error
(self, "Error: cannot 'continue', only 'break'.")
461 var t
= esc
.continue_stype
462 if n_expr
== null and t
!= null then
463 v
.error
(self, "Error: continue with a value required in this block.")
464 else if n_expr
!= null and t
== null then
465 v
.error
(self, "Error: continue without value required in this block.")
466 else if n_expr
!= null and t
!= null then
467 v
.check_conform_expr
(n_expr
.as(not null), t
)
473 redef class ABreakExpr
474 redef fun after_typing
(v
)
476 var old_flow_ctx
= v
.flow_ctx
478 var esc
= compute_escapable_block
(v
.scope_ctx
)
479 if esc
== null then return
481 esc
.break_flow_contexts
.add
(old_flow_ctx
)
483 var bl
= esc
.break_list
484 if n_expr
== null and bl
!= null then
485 v
.error
(self, "Error: break with a value required in this block.")
486 else if n_expr
!= null and bl
== null then
487 v
.error
(self, "Error: break without value required in this block.")
488 else if n_expr
!= null and bl
!= null then
489 # Typing check can only be done later
490 bl
.add
(n_expr
.as(not null))
496 redef class AAbortExpr
497 redef fun after_typing
(v
)
504 # An abstract control structure with feature escapable block
507 # The corresponding escapable block
508 readable var _escapable
: nullable EscapableBlock
510 # Enter and process a control structure
511 private fun process_control
(v
: TypingVisitor, escapable
: EscapableBlock, n_label
: nullable ALabel, is_loop
: Bool)
513 # Register the escapable block
514 _escapable
= escapable
515 v
.scope_ctx
.push_escapable
(escapable
, n_label
)
517 # Save an prepare the contextes
518 var old_flow_ctx
= v
.flow_ctx
519 var old_base_flow_ctx
= v
.base_flow_ctx
520 if is_loop
then v
.base_flow_ctx
= v
.flow_ctx
522 # Do the main processing
523 process_control_inside
(v
)
525 # Add the end of the block as an exit context
526 if not v
.flow_ctx
.unreash
then
527 escapable
.break_flow_contexts
.add
(v
.flow_ctx
)
530 # Merge all exit contexts
531 if escapable
.break_flow_contexts
.is_empty
then
532 v
.flow_ctx
= old_flow_ctx
535 v
.flow_ctx
= old_base_flow_ctx
.merge
(self, escapable
.break_flow_contexts
)
538 if is_loop
then v
.base_flow_ctx
= old_base_flow_ctx
543 # What to do inside the control block?
544 private fun process_control_inside
(v
: TypingVisitor) is abstract
549 redef fun accept_typing
(v
)
551 process_control
(v
, new BreakOnlyEscapableBlock(self), n_label
, false)
554 redef fun process_control_inside
(v
)
556 v
.enter_visit_block
(n_block
)
561 redef fun accept_typing
(v
)
563 v
.enter_visit
(n_expr
)
564 v
.check_conform_expr
(n_expr
, v
.type_bool
)
566 # Prepare 'then' context
567 var old_flow_ctx
= v
.flow_ctx
568 v
.use_if_true_flow_ctx
(n_expr
)
571 v
.enter_visit_block
(n_then
)
573 # Remember what appened in the 'then'
574 var then_flow_ctx
= v
.flow_ctx
576 # Prepare 'else' context
577 v
.flow_ctx
= old_flow_ctx
578 v
.use_if_false_flow_ctx
(n_expr
)
581 v
.enter_visit_block
(n_else
)
583 # Merge 'then' and 'else' contexts
584 v
.flow_ctx
= v
.base_flow_ctx
.merge_reash
(self, then_flow_ctx
, v
.flow_ctx
)
589 redef class AWhileExpr
591 redef fun accept_typing
(v
)
593 process_control
(v
, new EscapableBlock(self), n_label
, true)
596 redef fun process_control_inside
(v
)
598 var old_flow_ctx
= v
.flow_ctx
601 v
.enter_visit
(n_expr
)
602 v
.check_conform_expr
(n_expr
, v
.type_bool
)
604 if n_expr
isa ATrueExpr then
605 v
.warning
(self, "Warning: use 'loop' instead of 'while true do'.")
608 # Prepare inside context (assert cond)
609 v
.use_if_true_flow_ctx
(n_expr
)
612 v
.enter_visit_block
(n_block
)
614 # Compute outside context (assert !cond + all breaks)
615 v
.flow_ctx
= old_flow_ctx
616 v
.use_if_false_flow_ctx
(n_expr
)
617 escapable
.break_flow_contexts
.add
(v
.flow_ctx
)
621 redef class ALoopExpr
623 redef fun accept_typing
(v
)
625 process_control
(v
, new EscapableBlock(self), n_label
, true)
628 redef fun process_control_inside
(v
)
631 v
.enter_visit_block
(n_block
)
633 # Never automatically reach after the loop
640 var _variable
: nullable AutoVariable
641 redef fun variable
do return _variable
.as(not null)
643 redef fun accept_typing
(v
)
645 process_control
(v
, new EscapableBlock(self), n_label
, true)
648 redef fun process_control_inside
(v
)
650 v
.scope_ctx
.push
(self)
651 var old_flow_ctx
= v
.flow_ctx
653 # Create the automatic variable
654 var va
= new AutoVariable(n_id
.to_symbol
, n_id
)
656 v
.scope_ctx
.add_variable
(va
)
659 v
.enter_visit
(n_expr
)
661 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then return
662 var expr_type
= n_expr
.stype
665 var meth_iterator
= v
.get_method
(expr_type
, once
"iterator".to_symbol
)
666 var iter_type
= meth_iterator
.signature_for
(expr_type
).return_type
.as(not null)
667 var meth_item
= v
.get_method
(iter_type
, once
("item".to_symbol
))
668 var va_stype
= meth_item
.signature_for
(iter_type
).return_type
.as(not null)
669 if not n_expr
.is_self
then va_stype
= va_stype
.not_for_self
673 v
.enter_visit_block
(n_block
)
675 # end == begin of the loop
676 v
.flow_ctx
= old_flow_ctx
681 redef class AAssertExpr
682 redef fun accept_typing
(v
)
685 v
.enter_visit
(n_expr
)
686 v
.check_conform_expr
(n_expr
, v
.type_bool
)
688 # Process optional 'else' part
689 if n_else
!= null then
690 var old_flow_ctx
= v
.flow_ctx
691 v
.use_if_false_flow_ctx
(n_expr
)
692 v
.enter_visit
(n_else
)
693 v
.flow_ctx
= old_flow_ctx
697 v
.use_if_true_flow_ctx
(n_expr
)
702 redef class AVarFormExpr
703 var _variable
: nullable Variable
704 redef fun variable
do return _variable
.as(not null)
708 redef fun its_variable
do return variable
710 redef fun after_typing
(v
)
712 v
.flow_ctx
.check_is_set
(self, variable
)
713 _stype
= v
.flow_ctx
.stype
(variable
)
714 _is_typed
= _stype
!= null
718 redef class AVarAssignExpr
719 redef fun after_typing
(v
)
721 v
.mark_is_set
(variable
)
723 # Check the base type
724 var btype
= v
.base_flow_ctx
.stype
(variable
)
725 if not v
.check_expr
(n_value
) then return
726 if btype
!= null and not v
.check_conform_expr
(n_value
, btype
) then return
729 v
.flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, n_value
.stype
)
735 redef class AReassignFormExpr
736 # Compute and check method used through the reassigment operator
737 # On success return the static type of the result of the reassigment operator
738 # Else display an error and return null
739 private fun do_rvalue_typing
(v
: TypingVisitor, type_lvalue
: nullable MMType): nullable MMType
741 if type_lvalue
== null then
744 var name
= n_assign_op
.method_name
745 if type_lvalue
isa MMTypeNone then
746 v
.error
(self, "Error: Method '{name}' call on 'null'.")
749 var lc
= type_lvalue
.local_class
750 if not lc
.has_global_property_by_name
(name
) then
751 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
754 var prop
= lc
.select_method
(name
)
755 prop
.global
.check_visibility
(v
, self, v
.mmmodule
, false)
756 var psig
= prop
.signature_for
(type_lvalue
)
757 _assign_method
= prop
758 if not v
.check_conform_expr
(n_value
, psig
[0].not_for_self
) then return null
759 return psig
.return_type
.not_for_self
762 redef fun assign_method
do return _assign_method
.as(not null)
763 var _assign_method
: nullable MMMethod
766 redef class AVarReassignExpr
767 redef fun after_typing
(v
)
769 v
.flow_ctx
.check_is_set
(self, variable
)
770 v
.mark_is_set
(variable
)
771 var t
= v
.flow_ctx
.stype
(variable
)
772 var t2
= do_rvalue_typing
(v
, t
)
773 if t2
== null then return
775 # Check the base type
776 var btype
= v
.base_flow_ctx
.stype
(variable
)
777 if not v
.check_expr
(n_value
) then return
778 if btype
!= null and not v
.check_conform
(n_value
, t2
, btype
) then return
781 v
.flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, t2
)
787 redef class AAssignOp
788 fun method_name
: Symbol is abstract
790 redef class APlusAssignOp
791 redef fun method_name
do return once
"+".to_symbol
793 redef class AMinusAssignOp
794 redef fun method_name
do return once
"-".to_symbol
797 redef class ASelfExpr
798 var _variable
: nullable ParamVariable
799 redef fun variable
do return _variable
.as(not null)
801 redef fun its_variable
do return variable
803 redef fun after_typing
(v
)
805 _variable
= v
.self_var
806 _stype
= v
.flow_ctx
.stype
(variable
)
810 redef fun is_self
do return true
813 redef class AImplicitSelfExpr
814 redef fun is_implicit_self
do return true
817 redef class AIfexprExpr
818 redef fun accept_typing
(v
)
820 var old_flow_ctx
= v
.flow_ctx
823 v
.enter_visit
(n_expr
)
824 v
.check_conform_expr
(n_expr
, v
.type_bool
)
826 # Prepare 'then' context
827 v
.use_if_true_flow_ctx
(n_expr
)
830 v
.enter_visit_block
(n_then
)
832 # Remember what appened in the 'then'
833 var then_flow_ctx
= v
.flow_ctx
835 # Prepare 'else' context
836 v
.flow_ctx
= old_flow_ctx
837 v
.use_if_false_flow_ctx
(n_expr
)
840 v
.enter_visit_block
(n_else
)
842 # Merge 'then' and 'else' contexts
843 v
.flow_ctx
= v
.base_flow_ctx
.merge_reash
(self, then_flow_ctx
, v
.flow_ctx
)
845 var stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
846 if stype
== null then return
853 redef class ABoolExpr
854 redef fun after_typing
(v
)
862 redef fun accept_typing
(v
)
864 var old_flow_ctx
= v
.flow_ctx
865 var stype
= v
.type_bool
868 # Process left operand
869 v
.enter_visit
(n_expr
)
871 # Prepare right operand context
872 v
.use_if_false_flow_ctx
(n_expr
)
874 # Process right operand
875 v
.enter_visit
(n_expr2
)
876 if n_expr2
.if_false_flow_ctx
!= null then
877 _if_false_flow_ctx
= n_expr2
.if_false_flow_ctx
879 _if_false_flow_ctx
= v
.flow_ctx
882 v
.flow_ctx
= old_flow_ctx
884 v
.check_conform_expr
(n_expr
, stype
)
885 v
.check_conform_expr
(n_expr2
, stype
)
892 redef fun accept_typing
(v
)
894 var old_flow_ctx
= v
.flow_ctx
895 var stype
= v
.type_bool
897 # Process left operand
898 v
.enter_visit
(n_expr
)
900 # Prepare right operand context
901 v
.use_if_true_flow_ctx
(n_expr
)
903 # Process right operand
904 v
.enter_visit
(n_expr2
)
905 if n_expr2
.if_true_flow_ctx
!= null then
906 _if_true_flow_ctx
= n_expr2
.if_true_flow_ctx
908 _if_true_flow_ctx
= v
.flow_ctx
911 v
.flow_ctx
= old_flow_ctx
913 v
.check_conform_expr
(n_expr
, stype
)
914 v
.check_conform_expr
(n_expr2
, stype
)
921 redef fun after_typing
(v
)
923 v
.check_conform_expr
(n_expr
, v
.type_bool
)
925 # Invert if_true/if_false information
926 _if_false_flow_ctx
= n_expr
._if_true_flow_ctx
927 _if_true_flow_ctx
= n_expr
._if_false_flow_ctx
934 redef class AOrElseExpr
935 redef fun after_typing
(v
)
937 var old_flow_ctx
= v
.flow_ctx
939 # Process left operand
940 v
.enter_visit
(n_expr
)
943 # Consider the type of the left operand
945 if not t
.is_nullable
then
946 v
.warning
(n_expr
, "Warning: left operant of a 'or else' is not a nullable type.")
951 # Prepare the else context : ie the first expression is null
952 var variable
= n_expr
.its_variable
953 if variable
!= null then
954 v
.flow_ctx
.sub_with
(self, variable
, v
.type_none
)
957 # Process right operand
958 v
.enter_visit
(n_expr2
)
961 # Restore the context
962 v
.flow_ctx
= old_flow_ctx
965 var stype
= v
.check_conform_multiexpr
(t
, [n_expr2
])
966 if stype
== null then return
974 redef fun after_typing
(v
)
981 redef class AFloatExpr
982 redef fun after_typing
(v
)
984 _stype
= v
.type_float
989 redef class ACharExpr
990 redef fun after_typing
(v
)
997 redef class AStringFormExpr
998 redef fun after_typing
(v
)
1000 _stype
= v
.type_string
1005 redef class ASuperstringExpr
1006 redef fun atype
do return _atype
.as(not null)
1007 var _atype
: nullable MMType
1008 redef fun after_typing
(v
)
1010 var otype
= v
.type_object
1011 var stype
= v
.type_string
1013 for e
in n_exprs
do v
.check_conform_expr
(e
, otype
)
1014 var atype
= v
.type_array
(stype
)
1020 redef class ANullExpr
1021 redef fun after_typing
(v
)
1023 _stype
= v
.type_none
1028 redef class AArrayExpr
1029 redef fun after_typing
(v
)
1031 var stype
= v
.check_conform_multiexpr
(null, n_exprs
)
1032 if stype
!= null then do_typing
(v
, stype
)
1035 private fun do_typing
(v
: TypingVisitor, element_type
: MMType)
1037 _stype
= v
.type_array
(element_type
)
1042 redef class ARangeExpr
1043 redef fun after_typing
(v
)
1045 if not v
.check_expr
(n_expr
) or not v
.check_expr
(n_expr2
) then return
1046 var ntype
= n_expr
.stype
1047 var ntype2
= n_expr2
.stype
1048 if ntype
< ntype2
then
1050 else if not ntype2
< ntype
then
1051 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
1054 var dtype
= v
.type_discrete
1055 if not v
.check_conform_expr
(n_expr
, dtype
) or not v
.check_conform_expr
(n_expr2
, dtype
) then return
1056 _stype
= v
.type_range
(ntype
)
1061 redef class ASuperExpr
1062 redef readable var _init_in_superclass
: nullable MMMethod
1063 redef fun compute_raw_arguments
do return n_args
.to_a
1064 redef fun after_typing
(v
)
1066 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
1067 if not precs
.is_empty
then
1068 v
.local_property
.need_super
= true
1069 else if v
.local_property
.global
.is_init
then
1070 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
1071 for p
in base_precs
do
1072 if not p
.global
.is_init
then
1073 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
1075 precs
.add
(v
.local_class
[p
.global
])
1078 if precs
.is_empty
then
1079 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
1081 else if precs
.length
> 1 then
1082 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
1085 var p
= base_precs
.first
1086 assert p
isa MMMethod
1087 _init_in_superclass
= p
1088 register_super_init_call
(v
, p
)
1089 if n_args
.length
> 0 then
1090 var signature
= get_signature
(v
, v
.self_var
.stype
.as(not null), p
, true)
1091 process_signature
(v
, signature
, p
.name
, compute_raw_arguments
)
1094 v
.error
(self, "Error: No super method to call for {v.local_property}.")
1098 if precs
.first
.signature_for
(v
.self_var
.stype
.as(not null)).return_type
!= null then
1099 var stypes
= new Array[MMType]
1100 var stype
: nullable MMType = null
1101 for prop
in precs
do
1102 assert prop
isa MMMethod
1103 var t
= prop
.signature_for
(v
.self_var
.stype
.as(not null)).return_type
.for_module
(v
.mmmodule
).adapt_to
(v
.local_property
.signature
.recv
)
1105 if stype
== null or stype
< t
then
1110 v
.check_conform
(self, t
, stype
.as(not null))
1114 var p
= v
.local_property
1115 assert p
isa MMSrcMethod
1121 redef class AAttrFormExpr
1122 redef fun prop
do return _prop
.as(not null)
1123 var _prop
: nullable MMAttribute
1125 redef fun attr_type
do return _attr_type
.as(not null)
1126 var _attr_type
: nullable MMType
1128 # Compute the attribute accessed
1129 private fun do_typing
(v
: TypingVisitor)
1131 if not v
.check_expr
(n_expr
) then return
1132 var type_recv
= n_expr
.stype
1133 var name
= n_id
.to_symbol
1134 if type_recv
isa MMTypeNone then
1135 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1138 var lc
= type_recv
.local_class
1139 if not lc
.has_global_property_by_name
(name
) then
1140 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
1143 var prop
= lc
.select_attribute
(name
)
1144 if v
.mmmodule
.visibility_for
(prop
.global
.local_class
.mmmodule
) < 3 then
1145 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.mmmodule} is invisible in {v.mmmodule}")
1148 var at
= prop
.signature_for
(type_recv
).return_type
1149 if not n_expr
.is_self
then at
= at
.not_for_self
1154 redef class AAttrExpr
1155 redef fun after_typing
(v
)
1158 if _prop
== null then return
1164 redef class AAttrAssignExpr
1165 redef fun after_typing
(v
)
1168 if _prop
== null then return
1169 if not v
.check_conform_expr
(n_value
, attr_type
) then return
1174 redef class AAttrReassignExpr
1175 redef fun after_typing
(v
)
1178 if _prop
== null then return
1179 var t
= do_rvalue_typing
(v
, attr_type
)
1180 if t
== null then return
1181 v
.check_conform
(self, t
, n_value
.stype
)
1186 redef class AIssetAttrExpr
1187 redef fun after_typing
(v
)
1190 if _prop
== null then return
1191 if attr_type
.is_nullable
then
1192 v
.error
(self, "Error: isset on a nullable attribute.")
1194 _stype
= v
.type_bool
1199 redef class AAbsAbsSendExpr
1200 # The signature of the called property
1201 redef fun prop_signature
do return _prop_signature
.as(not null)
1202 var _prop_signature
: nullable MMSignature
1204 # Raw arguments used (without vararg transformation)
1205 redef fun raw_arguments
: Array[AExpr]
1207 var res
= _raw_arguments_cache
1211 res
= compute_raw_arguments
1212 if res
== null then res
= new Array[AExpr]
1213 _raw_arguments_cache
= res
1218 var _raw_arguments_cache
: nullable Array[AExpr] = null
1220 fun compute_raw_arguments
: nullable Array[AExpr]
1222 print
"{location} no compute_raw_arguments"
1226 # Check the conformity of a set of arguments `raw_args' to a signature.
1227 private fun process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: nullable Array[AExpr]): Bool
1229 var par_vararg
= psig
.vararg_rank
1230 var par_arity
= psig
.arity
1232 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
1233 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
1234 v
.error
(self, "Error: arity missmatch; prototype is '{name}{psig}'.")
1238 for par_idx
in [0..par_arity
[ do
1240 var par_type
= psig
[par_idx
]
1241 if par_idx
== par_vararg
then
1242 for i
in [0..(raw_arity-par_arity
)] do
1243 a
= raw_args
[arg_idx
]
1244 v
.check_conform_expr
(a
, par_type
)
1245 arg_idx
= arg_idx
+ 1
1248 a
= raw_args
[arg_idx
]
1249 v
.check_conform_expr
(a
, par_type
)
1250 arg_idx
= arg_idx
+ 1
1256 # Check the conformity of a set of defined closures
1257 private fun process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: nullable Array[AClosureDef]): nullable MMType
1259 var t
= psig
.return_type
1260 var cs
= psig
.closures
# Declared closures
1263 if not c
.is_optional
then min_arity
+= 1
1266 if cd
!= null then arity
= cd
.length
1267 if cs
.length
> 0 then
1268 if arity
== 0 and min_arity
> 0 then
1269 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1270 else if arity
> cs
.length
or arity
< min_arity
then
1271 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
1273 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1274 var break_list
: nullable Array[ABreakExpr] = null
1275 if t
!= null then break_list
= new Array[ABreakExpr]
1277 # The n_label, is any in only set on the last decl
1278 var n_label
= if arity
> 0 then cd
[arity-1
].n_label
else null
1280 # Process each closure definition
1281 for i
in [0..arity
[ do
1283 var cni
= cdi
.n_id
.to_symbol
1284 var csi
= psig
.closure_named
(cni
)
1286 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1287 v
.scope_ctx
.push_escapable
(esc
, n_label
)
1288 cdi
.accept_typing2
(v
, esc
)
1290 else if cs
.length
== 1 then
1291 v
.error
(cdi
.n_id
, "Error: no closure named '!{cni}' in {name}; only closure is !{cs.first.name}.")
1293 var a
= new Array[String]
1297 v
.error
(cdi
.n_id
, "Error: no closure named '!{cni}' in {name}; only closures are {a.join(",")}.")
1301 # Check break type conformity
1302 if break_list
!= null then
1303 t
= v
.check_conform_multiexpr
(t
, break_list
)
1306 else if arity
!= 0 then
1307 v
.error
(self, "Error: {name} does not require blocks.")
1313 redef class AAbsSendExpr
1314 # Compute the called global property
1315 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])
1317 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
1318 if prop
== null then return
1319 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
1320 if not process_signature
(v
, sig
, prop
.name
, raw_args
) then return
1321 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
1322 if rtype
== null and sig
.return_type
!= null then return
1324 _prop_signature
= sig
1325 _return_type
= rtype
1328 private fun get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): nullable MMMethod
1330 if type_recv
isa MMTypeNone then
1331 if name
== (once
"==".to_symbol
) or name
== (once
"!=".to_symbol
) then
1332 # Special case on != and == that are allowed for 'null'
1333 type_recv
= v
.type_object
.as_nullable
1335 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1339 var lc
= type_recv
.local_class
1340 var prop
: nullable MMMethod = null
1341 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
1342 if prop
== null and v
.local_property
.global
.is_init
then
1343 var props
= lc
.super_methods_named
(name
)
1344 if props
.length
> 1 then
1345 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1347 else if props
.length
== 1 then
1348 var p
= lc
[props
.first
.global
]
1349 assert p
isa MMMethod
1354 if prop
== null then
1355 if is_implicit_self
then
1356 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1358 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1365 # Get the signature for a local property and a receiver
1366 private fun get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
1368 prop
.global
.check_visibility
(v
, self, v
.mmmodule
, recv_is_self
)
1369 var psig
= prop
.signature_for
(type_recv
)
1370 if not recv_is_self
then psig
= psig
.not_for_self
1374 # The invoked method (once computed)
1375 redef fun prop
do return _prop
.as(not null)
1376 var _prop
: nullable MMMethod
1378 # The return type (if any) (once computed)
1379 redef readable var _return_type
: nullable MMType
1382 # A possible call of constructor in a super class
1383 # Could be an explicit call or with the 'super' keyword
1384 redef class ASuperInitCall
1385 private fun register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1387 if parent
!= v
.top_block
and self != v
.top_block
then
1388 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1390 var cla
= v
.mmmodule
[property
.global
.intro
.local_class
.global
]
1391 var prev_class
: nullable MMLocalClass = null
1392 var esic
= v
.explicit_super_init_calls
.as(not null)
1393 if not esic
.is_empty
then
1394 prev_class
= esic
.last
.global
.intro
.local_class
1396 var order
= v
.local_class
.cshe
.reverse_linear_extension
1397 if cla
== v
.local_class
then
1398 v
.explicit_other_init_call
= true
1399 else if not order
.has
(cla
) then
1400 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1401 else if cla
== prev_class
then
1402 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1405 if c
== prev_class
then
1407 else if c
== cla
then
1408 if prev_class
!= null then
1409 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1420 redef class ANewExpr
1421 redef fun compute_raw_arguments
do return n_args
.to_a
1422 redef fun after_typing
(v
)
1424 if not n_type
.is_typed
then return
1425 var t
= n_type
.stype
1426 if t
.local_class
.global
.is_abstract
then
1427 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1431 if n_id
== null then
1432 name
= once
"init".to_symbol
1434 name
= n_id
.to_symbol
1437 do_typing
(v
, t
, false, false, name
, raw_arguments
, null)
1438 if _prop
== null then return
1440 if not prop
.global
.is_init
then
1441 v
.error
(self, "Error: {prop} is not a constructor.")
1450 redef class ASendExpr
1451 # Name of the invoked property
1452 fun name
: Symbol is abstract
1454 # Closure definitions
1455 redef fun closure_defs
: nullable Array[AClosureDef] do return null
1457 redef fun after_typing
(v
)
1462 private fun do_all_typing
(v
: TypingVisitor)
1464 if not v
.check_expr
(n_expr
) then return
1465 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1466 if _prop
== null then return
1467 var prop
= _prop
.as(not null)
1469 if prop
.global
.is_init
then
1470 if not v
.local_property
.global
.is_init
then
1471 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1472 else if not n_expr
.is_self
then
1473 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1475 register_super_init_call
(v
, prop
)
1479 _stype
= return_type
1484 redef class ASendReassignExpr
1485 redef fun read_prop
do return _read_prop
.as(not null)
1486 var _read_prop
: nullable MMMethod
1487 redef fun do_all_typing
(v
)
1489 if not v
.check_expr
(n_expr
) then return
1490 var raw_args
= raw_arguments
1491 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1493 if prop
== null then return
1494 if prop
.global
.is_init
then
1495 if not v
.local_property
.global
.is_init
then
1496 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1497 else if not n_expr
.is_self
then
1498 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1501 var t
= prop
.signature_for
(n_expr
.stype
).return_type
.as(not null)
1502 if not n_expr
.is_self
then t
= t
.not_for_self
1504 var t2
= do_rvalue_typing
(v
, t
)
1505 if t2
== null then return
1506 v
.check_conform
(self, t2
, n_value
.stype
)
1509 raw_args
= raw_args
.to_a
1510 raw_args
.add
(n_value
)
1512 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1513 if prop
.global
.is_init
then
1514 if not v
.local_property
.global
.is_init
then
1515 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1516 else if not n_expr
.is_self
then
1517 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1525 redef class ABinopExpr
1526 redef fun compute_raw_arguments
do return [n_expr2
]
1529 redef fun name
do return once
"==".to_symbol
1530 redef fun after_typing
(v
)
1533 if not n_expr
.is_typed
or not n_expr2
.is_typed
then return
1534 if n_expr
.stype
isa MMTypeNone and not n_expr2
.stype
.is_nullable
or
1535 n_expr2
.stype
isa MMTypeNone and not n_expr
.stype
.is_nullable
then
1536 v
.warning
(self, "Warning: comparaison between null and a non nullable value.")
1539 if n_expr
.stype
isa MMTypeNone then
1540 if n_expr2
.stype
isa MMTypeNone then
1541 v
.warning
(self, "Warning: comparaison between 2 null values.")
1543 try_to_isa
(v
, n_expr2
)
1545 else if n_expr2
.stype
isa MMTypeNone then
1546 try_to_isa
(v
, n_expr
)
1550 private fun try_to_isa
(v
: TypingVisitor, n
: AExpr)
1552 var variable
= n
.its_variable
1553 if variable
!= null and n
.stype
isa MMNullableType then
1554 _if_false_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, n
.stype
.as_notnull
)
1555 _if_true_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, v
.type_none
)
1560 redef fun name
do return once
"!=".to_symbol
1561 redef fun after_typing
(v
)
1564 if not n_expr
.is_typed
or not n_expr2
.is_typed
then return
1565 if n_expr
.stype
isa MMTypeNone and not n_expr2
.stype
.is_nullable
or
1566 n_expr2
.stype
isa MMTypeNone and not n_expr
.stype
.is_nullable
then
1567 v
.warning
(self, "Warning: comparaison between null and a non nullable value.")
1570 if n_expr
.stype
isa MMTypeNone then
1571 if n_expr2
.stype
isa MMTypeNone then
1572 v
.warning
(self, "Warning: comparaison between 2 null values.")
1574 try_to_isa
(v
, n_expr2
)
1576 else if n_expr2
.stype
isa MMTypeNone then
1577 try_to_isa
(v
, n_expr
)
1581 private fun try_to_isa
(v
: TypingVisitor, n
: AExpr)
1583 var variable
= n
.its_variable
1584 if variable
!= null and n
.stype
isa MMNullableType then
1585 _if_true_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, n
.stype
.as_notnull
)
1586 _if_false_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, v
.type_none
)
1591 redef fun name
do return once
"<".to_symbol
1594 redef fun name
do return once
"<=".to_symbol
1597 redef fun name
do return once
"<<".to_symbol
1600 redef fun name
do return once
">".to_symbol
1603 redef fun name
do return once
">=".to_symbol
1606 redef fun name
do return once
">>".to_symbol
1608 redef class APlusExpr
1609 redef fun name
do return once
"+".to_symbol
1611 redef class AMinusExpr
1612 redef fun name
do return once
"-".to_symbol
1614 redef class AStarshipExpr
1615 redef fun name
do return once
"<=>".to_symbol
1617 redef class AStarExpr
1618 redef fun name
do return once
"*".to_symbol
1620 redef class ASlashExpr
1621 redef fun name
do return once
"/".to_symbol
1623 redef class APercentExpr
1624 redef fun name
do return once
"%".to_symbol
1627 redef class AUminusExpr
1628 redef fun name
do return once
"unary -".to_symbol
1629 redef fun compute_raw_arguments
do return null
1632 redef class ACallFormExpr
1633 redef fun after_typing
(v
)
1635 if n_expr
.is_implicit_self
then
1636 var name
= n_id
.to_symbol
1637 var variable
= v
.scope_ctx
[name
]
1638 if variable
!= null then
1640 if variable
isa ClosureVariable then
1641 n
= new AClosureCallExpr.init_aclosurecallexpr
(n_id
, n_args
, n_closure_defs
)
1642 n
._variable
= variable
1644 if not n_args
.is_empty
then
1645 v
.error
(self, "Error: {name} is variable, not a function.")
1648 n
= variable_create
(variable
)
1649 n
._variable
= variable
1660 redef fun closure_defs
1662 if n_closure_defs
.is_empty
then
1665 return n_closure_defs
.to_a
1669 # Create a variable acces corresponding to the call form
1670 fun variable_create
(variable
: Variable): AVarFormExpr is abstract
1673 redef class ACallExpr
1674 redef fun variable_create
(variable
)
1676 return new AVarExpr.init_avarexpr
(n_id
)
1679 redef fun name
do return n_id
.to_symbol
1680 redef fun compute_raw_arguments
do return n_args
.to_a
1683 redef class ACallAssignExpr
1684 redef fun variable_create
(variable
)
1686 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1689 redef fun name
do return (n_id
.text
+ "=").to_symbol
1690 redef fun compute_raw_arguments
do
1691 var res
= n_args
.to_a
1697 redef class ACallReassignExpr
1698 redef fun variable_create
(variable
)
1700 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1703 redef fun name
do return n_id
.to_symbol
1704 redef fun compute_raw_arguments
do return n_args
.to_a
1707 redef class ABraExpr
1708 redef fun name
do return once
"[]".to_symbol
1709 redef fun compute_raw_arguments
do return n_args
.to_a
1710 redef fun closure_defs
1712 if n_closure_defs
.is_empty
then
1715 return n_closure_defs
.to_a
1720 redef class ABraAssignExpr
1721 redef fun name
do return once
"[]=".to_symbol
1722 redef fun compute_raw_arguments
do
1723 var res
= n_args
.to_a
1729 redef class ABraReassignExpr
1730 redef fun name
do return once
"[]".to_symbol
1731 redef fun compute_raw_arguments
do return n_args
.to_a
1734 redef class AInitExpr
1735 redef fun name
do return once
"init".to_symbol
1736 redef fun compute_raw_arguments
do return n_args
.to_a
1739 redef class AClosureCallExpr
1740 var _variable
: nullable ClosureVariable
1741 redef fun variable
do return _variable
.as(not null)
1742 redef fun compute_raw_arguments
do return n_args
.to_a
1744 redef fun after_typing
(v
)
1747 if va
.closure
.is_break
then v
.mark_unreash
(self)
1748 var sig
= va
.closure
.signature
1749 var s
= process_signature
(v
, sig
, n_id
.to_symbol
, compute_raw_arguments
)
1750 if not n_closure_defs
.is_empty
then
1751 process_closures
(v
, sig
, n_id
.to_symbol
, n_closure_defs
.to_a
)
1753 if not s
then return
1754 _prop_signature
= sig
1755 _stype
= sig
.return_type
1760 redef class AClosureId
1761 fun to_symbol
: Symbol is abstract
1763 redef class ASimpleClosureId
1764 redef fun to_symbol
: Symbol do return n_id
.to_symbol
1766 redef class ABreakClosureId
1767 redef fun to_symbol
: Symbol do return n_kwbreak
.to_symbol
1770 redef class AClosureDef
1771 var _closure
: nullable MMClosure
1772 redef fun closure
do return _closure
.as(not null)
1774 # The corresponding escapable object
1775 readable var _escapable
: nullable EscapableBlock
1777 var _accept_typing2
: Bool = false
1778 redef fun accept_typing
(v
)
1780 # Typing is deferred, wait accept_typing2(v)
1781 if _accept_typing2
then super
1784 private fun accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure)
1788 var sig
= esc
.closure
.signature
1789 if sig
.arity
!= n_ids
.length
then
1790 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_ids.length} found.")
1794 _closure
= esc
.closure
1796 v
.scope_ctx
.push
(self)
1797 var old_flow_ctx
= v
.flow_ctx
1798 var old_base_flow_ctx
= v
.base_flow_ctx
1799 v
.base_flow_ctx
= v
.flow_ctx
1800 variables
= new Array[AutoVariable]
1801 for i
in [0..n_ids
.length
[ do
1802 var va
= new AutoVariable(n_ids
[i
].to_symbol
, n_ids
[i
])
1805 v
.scope_ctx
.add_variable
(va
)
1808 _accept_typing2
= true
1811 if v
.flow_ctx
.unreash
== false then
1812 if closure
.signature
.return_type
!= null then
1813 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
1814 else if closure
.is_break
and esc
.break_list
!= null then
1815 v
.error
(self, "Control error: Reached end of break block (a 'break' with a value was expected).")
1818 v
.flow_ctx
= old_flow_ctx
1819 v
.base_flow_ctx
= old_base_flow_ctx
1824 class ATypeCheckExpr
1826 private fun check_expr_cast
(v
: TypingVisitor, n_expr
: AExpr, n_type
: AType)
1828 if not v
.check_expr
(n_expr
) then return
1829 if not n_type
.is_typed
then return
1830 var etype
= n_expr
.stype
1831 var ttype
= n_type
.stype
1832 if etype
== ttype
then
1833 v
.warning
(self, "Warning: Expression is already a {ttype}.")
1834 else if etype
< ttype
then
1835 v
.warning
(self, "Warning: Expression is already a {ttype} since it is a {etype}.")
1836 else if etype
isa MMTypeNone then
1837 # ttype is not nullable because of prevous test
1838 v
.warning
(self, "Warning: Expression is null therefore cannot be a {ttype}.")
1839 else if etype
.is_nullable
and etype
.as_notnull
== ttype
then
1840 if ttype
isa MMTypeFormal and ttype
.bound
.is_nullable
then
1841 # No warning in this case since with
1842 # type T: nullable A
1844 # 'x.as(not null)' != 'x.as(T)'
1845 # 'x != null' != 'x isa T'
1846 else if self isa AIsaExpr then
1847 v
.warning
(self, "Warning: Prefer '!= null'.")
1849 v
.warning
(self, "Warning: Prefer '.as(not null)'.")
1855 redef class AIsaExpr
1856 special ATypeCheckExpr
1857 redef fun after_typing
(v
)
1859 check_expr_cast
(v
, n_expr
, n_type
)
1860 if not n_type
.is_typed
then return
1861 var variable
= n_expr
.its_variable
1862 if variable
!= null then
1863 _if_true_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, n_type
.stype
)
1865 _stype
= v
.type_bool
1870 redef class AAsCastExpr
1871 special ATypeCheckExpr
1872 redef fun after_typing
(v
)
1874 check_expr_cast
(v
, n_expr
, n_type
)
1875 if not n_type
.is_typed
then return
1876 _stype
= n_type
.stype
1877 _is_typed
= _stype
!= null
1881 redef class AAsNotnullExpr
1882 redef fun after_typing
(v
)
1884 if not v
.check_expr
(n_expr
) then return
1885 var t
= n_expr
.stype
1886 if t
isa MMTypeNone then
1887 v
.error
(n_expr
, "Type error: 'as(not null)' on 'null' value.")
1889 else if not t
.is_nullable
then
1890 v
.warning
(n_expr
, "Warning: 'as(not null)' on non nullable type.")
1892 _stype
= n_expr
.stype
.as_notnull
1897 redef class AProxyExpr
1898 redef fun after_typing
(v
)
1900 if not n_expr
.is_typed
then return
1902 if n_expr
.is_statement
then return
1903 _stype
= n_expr
.stype
1907 redef class AOnceExpr
1908 redef fun accept_typing
(v
)
1910 if v
.once_count
> 0 then
1911 v
.warning
(self, "Useless once in a once expression.")
1913 v
.once_count
= v
.once_count
+ 1
1917 v
.once_count
= v
.once_count
- 1