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 super AbsSyntaxVisitor
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 constructor in {c}. Discarded candidates are {a.join(", ")}.")
158 v
.error
(n
, "Error: there is no available compatible constructor 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_extern
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
)
280 redef class AExternInitPropdef
281 redef fun accept_typing
(v
)
283 v
.explicit_other_init_call
= false
286 redef fun after_typing
(v
)
292 redef class ASignature
293 redef fun after_typing
(v
)
295 if self.n_opar
!= null and self.n_params
.is_empty
then
296 v
.warning
(self, "Warning: superfluous parentheses.")
302 redef fun after_typing
(v
)
304 v
.scope_ctx
.add_variable
(variable
)
308 redef class AClosureDecl
309 # The corresponding escapable object
310 readable var _escapable
: nullable EscapableBlock
312 redef fun accept_typing
(v
)
314 # Register the closure for ClosureCallExpr
315 v
.scope_ctx
.add_variable
(variable
)
317 var old_flow_ctx
= v
.flow_ctx
318 var old_base_flow_ctx
= v
.base_flow_ctx
319 v
.base_flow_ctx
= v
.flow_ctx
321 var blist
: nullable Array[AExpr] = null
322 var t
= v
.local_property
.signature
.return_type
323 if t
!= null then blist
= new Array[AExpr]
324 var escapable
= new EscapableClosure(self, variable
.closure
, blist
)
325 _escapable
= escapable
326 v
.scope_ctx
.push_escapable
(escapable
, null)
328 v
.is_default_closure_definition
= true
332 v
.is_default_closure_definition
= false
334 if n_expr
!= null then
335 if v
.flow_ctx
.unreash
== false then
336 if variable
.closure
.signature
.return_type
!= null then
337 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
338 else if variable
.closure
.is_break
and escapable
.break_list
!= null then
339 v
.error
(self, "Control error: Reached end of break block (a 'break' with a value was expected).")
343 if blist
!= null then for x
in blist
do
344 v
.check_conform_expr
(x
, t
)
347 v
.flow_ctx
= old_flow_ctx
348 v
.base_flow_ctx
= old_base_flow_ctx
354 redef fun stype
: MMType do return _stype
.as(not null)
355 redef fun is_typed
: Bool do return _stype
!= null
356 var _stype
: nullable MMType
358 redef fun after_typing
(v
)
360 _stype
= get_stype
(v
)
365 redef readable var _is_typed
: Bool = false
366 redef fun is_statement
: Bool do return _stype
== null
370 print
"{location}: not is_typed"
374 print
"{location}: is_statement"
377 return _stype
.as(not null)
379 var _stype
: nullable MMType
381 redef fun after_typing
(v
)
383 # Default behavior is to be happy
387 # Is the expression the implicit receiver
388 fun is_implicit_self
: Bool do return false
390 # Is the expression the current receiver (implicit or explicit)
391 fun is_self
: Bool do return false
393 # The variable accessed is any
394 fun its_variable
: nullable Variable do return null
396 # The control flow information if current boolean expression is true
397 readable private var _if_true_flow_ctx
: nullable FlowContext
399 # The control flow information if current boolean expression is false
400 readable private var _if_false_flow_ctx
: nullable FlowContext
402 # Wharn in case of superfluous parentheses
403 private fun warn_parentheses
(v
: AbsSyntaxVisitor)
409 redef fun warn_parentheses
(v
)
411 v
.warning
(self, "Warning: superfluous parentheses.")
415 redef class AParExprs
416 redef fun after_typing
(v
)
418 if n_exprs
.is_empty
then
419 v
.warning
(self, "Warning: superfluous parentheses.")
424 redef class AVardeclExpr
425 var _variable
: nullable VarVariable
426 redef fun variable
do return _variable
.as(not null)
428 redef fun after_typing
(v
)
430 var va
= new VarVariable(n_id
.to_symbol
, n_id
)
432 v
.scope_ctx
.add_variable
(va
)
434 if ne
!= null then v
.mark_is_set
(va
)
436 if n_type
!= null then
437 if not n_type
.is_typed
then return
438 va
.stype
= n_type
.stype
440 v
.check_conform_expr
(ne
, va
.stype
)
442 else if ne
!= null then
443 if not v
.check_expr
(ne
) then return
445 if st
isa MMTypeNone then
446 va
.stype
= v
.type_object
.as_nullable
447 v
.flow_ctx
= v
.flow_ctx
.sub_with
(self, va
, st
)
452 va
.stype
= v
.type_object
.as_nullable
458 redef class ABlockExpr
459 redef fun accept_typing
(v
)
462 if not v
.flow_ctx
.unreash
then
464 else if not v
.flow_ctx
.already_unreash
then
465 v
.flow_ctx
.already_unreash
= true
466 v
.error
(e
, "Error: unreachable statement.")
474 redef class AReturnExpr
475 redef fun after_typing
(v
)
478 var t
= v
.local_property
.signature
.return_type
480 if v
.is_default_closure_definition
then
481 v
.error
(self, "Error: 'return' invalid in default closure definitions. Use 'continue' or 'break'.")
486 if e
== null and t
!= null then
487 v
.error
(self, "Error: Return without value in a function.")
488 else if e
!= null and t
== null then
489 v
.error
(self, "Error: Return with value in a procedure.")
490 else if e
!= null and t
!= null then
491 v
.check_conform_expr
(e
, t
)
494 e
.warn_parentheses
(v
)
500 redef class AContinueExpr
501 redef fun after_typing
(v
)
504 var esc
= compute_escapable_block
(v
.scope_ctx
)
505 if esc
== null then return
507 if esc
.is_break_block
then
508 v
.error
(self, "Error: cannot 'continue', only 'break'.")
512 var t
= esc
.continue_stype
514 if e
== null and t
!= null then
515 v
.error
(self, "Error: continue with a value required in this block.")
516 else if e
!= null and t
== null then
517 v
.error
(self, "Error: continue without value required in this block.")
518 else if e
!= null and t
!= null then
519 v
.check_conform_expr
(e
, t
)
522 e
.warn_parentheses
(v
)
528 redef class ABreakExpr
529 redef fun after_typing
(v
)
531 var old_flow_ctx
= v
.flow_ctx
533 var esc
= compute_escapable_block
(v
.scope_ctx
)
534 if esc
== null then return
536 esc
.break_flow_contexts
.add
(old_flow_ctx
)
538 var bl
= esc
.break_list
540 if e
== null and bl
!= null then
541 v
.error
(self, "Error: break with a value required in this block.")
542 else if e
!= null and bl
== null then
543 v
.error
(self, "Error: break without value required in this block.")
544 else if e
!= null and bl
!= null then
545 # Typing check can only be done later
549 e
.warn_parentheses
(v
)
555 redef class AAbortExpr
556 redef fun after_typing
(v
)
563 # An abstract control structure with feature escapable block
564 abstract class AAbsControl
566 # The corresponding escapable block
567 readable var _escapable
: nullable EscapableBlock
569 # Enter and process a control structure
570 private fun process_control
(v
: TypingVisitor, escapable
: EscapableBlock, n_label
: nullable ALabel, is_loop
: Bool)
572 # Register the escapable block
573 _escapable
= escapable
574 v
.scope_ctx
.push_escapable
(escapable
, n_label
)
576 # Save an prepare the contextes
577 var old_flow_ctx
= v
.flow_ctx
578 var old_base_flow_ctx
= v
.base_flow_ctx
579 if is_loop
then v
.base_flow_ctx
= v
.flow_ctx
581 # Do the main processing
582 process_control_inside
(v
)
584 # Add the end of the block as an exit context
585 if not v
.flow_ctx
.unreash
then
586 escapable
.break_flow_contexts
.add
(v
.flow_ctx
)
589 # Merge all exit contexts
590 if escapable
.break_flow_contexts
.is_empty
then
591 v
.flow_ctx
= old_flow_ctx
594 v
.flow_ctx
= old_base_flow_ctx
.merge
(self, escapable
.break_flow_contexts
)
597 if is_loop
then v
.base_flow_ctx
= old_base_flow_ctx
602 # What to do inside the control block?
603 private fun process_control_inside
(v
: TypingVisitor) is abstract
608 redef fun accept_typing
(v
)
610 process_control
(v
, new BreakOnlyEscapableBlock(self), n_label
, false)
613 redef fun process_control_inside
(v
)
615 v
.enter_visit_block
(n_block
)
620 redef fun accept_typing
(v
)
622 v
.enter_visit
(n_expr
)
623 v
.check_conform_expr
(n_expr
, v
.type_bool
)
625 n_expr
.warn_parentheses
(v
)
627 # Prepare 'then' context
628 var old_flow_ctx
= v
.flow_ctx
629 v
.use_if_true_flow_ctx
(n_expr
)
632 v
.enter_visit_block
(n_then
)
634 # Remember what appened in the 'then'
635 var then_flow_ctx
= v
.flow_ctx
637 # Prepare 'else' context
638 v
.flow_ctx
= old_flow_ctx
639 v
.use_if_false_flow_ctx
(n_expr
)
642 v
.enter_visit_block
(n_else
)
644 # Merge 'then' and 'else' contexts
645 v
.flow_ctx
= v
.base_flow_ctx
.merge_reash
(self, then_flow_ctx
, v
.flow_ctx
)
650 redef class AWhileExpr
652 redef fun accept_typing
(v
)
654 process_control
(v
, new EscapableBlock(self), n_label
, true)
657 redef fun process_control_inside
(v
)
659 var old_flow_ctx
= v
.flow_ctx
662 v
.enter_visit
(n_expr
)
663 v
.check_conform_expr
(n_expr
, v
.type_bool
)
665 if n_expr
isa ATrueExpr then
666 v
.warning
(self, "Warning: use 'loop' instead of 'while true do'.")
668 n_expr
.warn_parentheses
(v
)
671 # Prepare inside context (assert cond)
672 v
.use_if_true_flow_ctx
(n_expr
)
675 v
.enter_visit_block
(n_block
)
677 # Compute outside context (assert !cond + all breaks)
678 v
.flow_ctx
= old_flow_ctx
679 v
.use_if_false_flow_ctx
(n_expr
)
680 escapable
.break_flow_contexts
.add
(v
.flow_ctx
)
684 redef class ALoopExpr
686 redef fun accept_typing
(v
)
688 process_control
(v
, new EscapableBlock(self), n_label
, true)
691 redef fun process_control_inside
(v
)
694 v
.enter_visit_block
(n_block
)
696 # Never automatically reach after the loop
703 var _variables
: nullable Array[AutoVariable]
704 redef fun variables
do return _variables
.as(not null)
706 redef fun accept_typing
(v
)
708 process_control
(v
, new EscapableBlock(self), n_label
, true)
711 redef fun process_control_inside
(v
)
713 v
.scope_ctx
.push
(self)
714 var old_flow_ctx
= v
.flow_ctx
719 v
.enter_visit_block
(n_block
)
721 # end == begin of the loop
722 v
.flow_ctx
= old_flow_ctx
726 private fun do_typing
(v
: TypingVisitor)
728 # Create the automatic variables
729 var vas
= new Array[AutoVariable]
731 var va
= new AutoVariable(n_id
.to_symbol
, n_id
)
732 v
.scope_ctx
.add_variable
(va
)
738 v
.enter_visit
(n_expr
)
739 if not v
.check_expr
(n_expr
) then return
740 var expr_type
= n_expr
.stype
742 if expr_type
.is_nullable
then
743 v
.error
(n_expr
, "Type error: 'for' on a nullable expression.")
746 n_expr
.warn_parentheses
(v
)
749 var iterate_name
= once
"iterate".to_symbol
750 if not expr_type
.local_class
.has_global_property_by_name
(iterate_name
) then
751 v
.error
(n_expr
, "Type error: Expected a type with an 'iterate' method. Found {expr_type}.")
754 var prop
= expr_type
.local_class
.select_method
(iterate_name
)
755 prop
.global
.check_visibility
(v
, self, v
.mmmodule
, n_expr
.is_self
)
756 var psig
= prop
.signature_for
(expr_type
)
757 if not n_expr
.is_self
then psig
= psig
.not_for_self
758 if psig
.arity
!= 0 then
759 v
.error
(self, "Error: 'iterate' incompatible with 'for': require no arguments.")
761 else if psig
.closures
.length
!= 1 then
762 v
.error
(self, "Error: 'iterate' incompatible with 'for': require one closure.")
765 psig
= psig
.closures
.first
.signature
766 if psig
.return_type
!= null then
767 v
.error
(self, "Error: 'iterate' incompatible with 'for': require one procedural closure.")
770 if vas
.length
!= psig
.arity
then
771 if psig
.arity
== 1 then
772 v
.error
(self, "Error: Expected {psig.arity} variable {psig}, found {vas.length}.")
774 v
.error
(self, "Error: Expected {psig.arity} variables {psig}, found {vas.length}.")
779 # Type the automatic variables
780 for i
in [0..vas
.length
[ do
781 vas
[i
].stype
= psig
[i
]
786 redef class AAssertExpr
787 redef fun accept_typing
(v
)
790 v
.enter_visit
(n_expr
)
791 v
.check_conform_expr
(n_expr
, v
.type_bool
)
792 n_expr
.warn_parentheses
(v
)
794 # Process optional 'else' part
795 if n_else
!= null then
796 var old_flow_ctx
= v
.flow_ctx
797 v
.use_if_false_flow_ctx
(n_expr
)
798 v
.enter_visit
(n_else
)
799 v
.flow_ctx
= old_flow_ctx
803 v
.use_if_true_flow_ctx
(n_expr
)
808 redef class AVarFormExpr
809 var _variable
: nullable Variable
810 redef fun variable
do return _variable
.as(not null)
814 redef fun its_variable
do return variable
816 redef fun after_typing
(v
)
818 v
.flow_ctx
.check_is_set
(self, variable
)
819 _stype
= v
.flow_ctx
.stype
(variable
)
820 _is_typed
= _stype
!= null
824 redef class AVarAssignExpr
825 redef fun after_typing
(v
)
827 v
.mark_is_set
(variable
)
829 # Check the base type
830 var btype
= v
.base_flow_ctx
.stype
(variable
)
831 if not v
.check_expr
(n_value
) then return
832 if btype
!= null and not v
.check_conform_expr
(n_value
, btype
) then return
835 v
.flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, n_value
.stype
)
841 redef class AReassignFormExpr
842 # Compute and check method used through the reassigment operator
843 # On success return the static type of the result of the reassigment operator
844 # Else display an error and return null
845 private fun do_rvalue_typing
(v
: TypingVisitor, type_lvalue
: nullable MMType): nullable MMType
847 if type_lvalue
== null then
850 var name
= n_assign_op
.method_name
851 if type_lvalue
isa MMTypeNone then
852 v
.error
(self, "Error: Method '{name}' call on 'null'.")
855 var lc
= type_lvalue
.local_class
856 if not lc
.has_global_property_by_name
(name
) then
857 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
860 var prop
= lc
.select_method
(name
)
861 prop
.global
.check_visibility
(v
, self, v
.mmmodule
, false)
862 var psig
= prop
.signature_for
(type_lvalue
)
863 _assign_method
= prop
864 if not v
.check_conform_expr
(n_value
, psig
[0].not_for_self
) then return null
865 return psig
.return_type
.not_for_self
868 redef fun assign_method
do return _assign_method
.as(not null)
869 var _assign_method
: nullable MMMethod
872 redef class AVarReassignExpr
873 redef fun after_typing
(v
)
875 v
.flow_ctx
.check_is_set
(self, variable
)
876 v
.mark_is_set
(variable
)
877 var t
= v
.flow_ctx
.stype
(variable
)
878 var t2
= do_rvalue_typing
(v
, t
)
879 if t2
== null then return
881 # Check the base type
882 var btype
= v
.base_flow_ctx
.stype
(variable
)
883 if not v
.check_expr
(n_value
) then return
884 if btype
!= null and not v
.check_conform
(n_value
, t2
, btype
) then return
887 v
.flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, t2
)
893 redef class AAssignOp
894 fun method_name
: Symbol is abstract
896 redef class APlusAssignOp
897 redef fun method_name
do return once
"+".to_symbol
899 redef class AMinusAssignOp
900 redef fun method_name
do return once
"-".to_symbol
903 redef class ASelfExpr
904 var _variable
: nullable ParamVariable
905 redef fun variable
do return _variable
.as(not null)
907 redef fun its_variable
do return variable
909 redef fun after_typing
(v
)
911 _variable
= v
.self_var
912 _stype
= v
.flow_ctx
.stype
(variable
)
916 redef fun is_self
do return true
919 redef class AImplicitSelfExpr
920 redef fun is_implicit_self
do return true
923 redef class AIfexprExpr
924 redef fun accept_typing
(v
)
926 var old_flow_ctx
= v
.flow_ctx
929 v
.enter_visit
(n_expr
)
930 v
.check_conform_expr
(n_expr
, v
.type_bool
)
932 # Prepare 'then' context
933 v
.use_if_true_flow_ctx
(n_expr
)
936 v
.enter_visit_block
(n_then
)
938 # Remember what appened in the 'then'
939 var then_flow_ctx
= v
.flow_ctx
941 # Prepare 'else' context
942 v
.flow_ctx
= old_flow_ctx
943 v
.use_if_false_flow_ctx
(n_expr
)
946 v
.enter_visit_block
(n_else
)
948 # Merge 'then' and 'else' contexts
949 v
.flow_ctx
= v
.base_flow_ctx
.merge_reash
(self, then_flow_ctx
, v
.flow_ctx
)
951 var stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
952 if stype
== null then return
959 redef class ABoolExpr
960 redef fun after_typing
(v
)
968 redef fun accept_typing
(v
)
970 var old_flow_ctx
= v
.flow_ctx
971 var stype
= v
.type_bool
974 # Process left operand
975 v
.enter_visit
(n_expr
)
977 # Prepare right operand context
978 v
.use_if_false_flow_ctx
(n_expr
)
980 # Process right operand
981 v
.enter_visit
(n_expr2
)
982 if n_expr2
.if_false_flow_ctx
!= null then
983 _if_false_flow_ctx
= n_expr2
.if_false_flow_ctx
985 _if_false_flow_ctx
= v
.flow_ctx
988 v
.flow_ctx
= old_flow_ctx
990 v
.check_conform_expr
(n_expr
, stype
)
991 v
.check_conform_expr
(n_expr2
, stype
)
998 redef fun accept_typing
(v
)
1000 var old_flow_ctx
= v
.flow_ctx
1001 var stype
= v
.type_bool
1003 # Process left operand
1004 v
.enter_visit
(n_expr
)
1006 # Prepare right operand context
1007 v
.use_if_true_flow_ctx
(n_expr
)
1009 # Process right operand
1010 v
.enter_visit
(n_expr2
)
1011 if n_expr2
.if_true_flow_ctx
!= null then
1012 _if_true_flow_ctx
= n_expr2
.if_true_flow_ctx
1014 _if_true_flow_ctx
= v
.flow_ctx
1017 v
.flow_ctx
= old_flow_ctx
1019 v
.check_conform_expr
(n_expr
, stype
)
1020 v
.check_conform_expr
(n_expr2
, stype
)
1026 redef class ANotExpr
1027 redef fun after_typing
(v
)
1029 v
.check_conform_expr
(n_expr
, v
.type_bool
)
1031 # Invert if_true/if_false information
1032 _if_false_flow_ctx
= n_expr
._if_true_flow_ctx
1033 _if_true_flow_ctx
= n_expr
._if_false_flow_ctx
1035 _stype
= v
.type_bool
1040 redef class AOrElseExpr
1041 redef fun after_typing
(v
)
1043 var old_flow_ctx
= v
.flow_ctx
1045 # Process left operand
1046 v
.enter_visit
(n_expr
)
1047 v
.check_expr
(n_expr
)
1049 # Consider the type of the left operand
1050 var t
= n_expr
.stype
1051 if not t
.is_nullable
then
1052 v
.warning
(n_expr
, "Warning: left operand of a 'or else' is not a nullable type.")
1057 # Prepare the else context : ie the first expression is null
1058 var variable
= n_expr
.its_variable
1059 if variable
!= null then
1060 v
.flow_ctx
.sub_with
(self, variable
, v
.type_none
)
1063 # Process right operand
1064 v
.enter_visit
(n_expr2
)
1065 v
.check_expr
(n_expr
)
1067 # Restore the context
1068 v
.flow_ctx
= old_flow_ctx
1071 var stype
= v
.check_conform_multiexpr
(t
, [n_expr2
])
1072 if stype
== null then return
1079 redef class AIntExpr
1080 redef fun after_typing
(v
)
1087 redef class AFloatExpr
1088 redef fun after_typing
(v
)
1090 _stype
= v
.type_float
1095 redef class ACharExpr
1096 redef fun after_typing
(v
)
1098 _stype
= v
.type_char
1103 redef class AStringFormExpr
1104 redef fun after_typing
(v
)
1106 _stype
= v
.type_string
1111 redef class ASuperstringExpr
1112 redef fun atype
do return _atype
.as(not null)
1113 var _atype
: nullable MMType
1114 redef fun after_typing
(v
)
1116 var otype
= v
.type_object
1117 var stype
= v
.type_string
1119 for e
in n_exprs
do v
.check_conform_expr
(e
, otype
)
1120 var atype
= v
.type_array
(stype
)
1126 redef class ANullExpr
1127 redef fun after_typing
(v
)
1129 _stype
= v
.type_none
1134 redef class AArrayExpr
1135 redef fun after_typing
(v
)
1137 var stype
= v
.check_conform_multiexpr
(null, n_exprs
.n_exprs
)
1138 if stype
!= null then do_typing
(v
, stype
)
1141 private fun do_typing
(v
: TypingVisitor, element_type
: MMType)
1143 _stype
= v
.type_array
(element_type
)
1148 redef class ARangeExpr
1149 redef fun after_typing
(v
)
1151 if not v
.check_expr
(n_expr
) or not v
.check_expr
(n_expr2
) then return
1152 var ntype
= n_expr
.stype
1153 var ntype2
= n_expr2
.stype
1154 if ntype
< ntype2
then
1156 else if not ntype2
< ntype
then
1157 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
1160 var dtype
= v
.type_discrete
1161 if not v
.check_conform_expr
(n_expr
, dtype
) or not v
.check_conform_expr
(n_expr2
, dtype
) then return
1162 _stype
= v
.type_range
(ntype
)
1167 redef class ASuperExpr
1168 redef readable var _init_in_superclass
: nullable MMMethod
1169 redef fun compute_raw_arguments
do return n_args
.to_a
1170 redef fun after_typing
(v
)
1172 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
1173 if not precs
.is_empty
then
1174 v
.local_property
.need_super
= true
1175 else if v
.local_property
.global
.is_init
then
1176 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
1177 for p
in base_precs
do
1178 if not p
.global
.is_init
then
1179 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
1181 precs
.add
(v
.local_class
[p
.global
])
1184 if precs
.is_empty
then
1185 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
1187 else if precs
.length
> 1 then
1188 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
1191 var p
= base_precs
.first
1192 assert p
isa MMMethod
1193 _init_in_superclass
= p
1194 register_super_init_call
(v
, p
)
1195 if n_args
.n_exprs
.length
> 0 then
1196 var signature
= get_signature
(v
, v
.self_var
.stype
.as(not null), p
, true)
1197 process_signature
(v
, signature
, p
.name
, compute_raw_arguments
)
1200 v
.error
(self, "Error: No super method to call for {v.local_property}.")
1204 if precs
.first
.signature_for
(v
.self_var
.stype
.as(not null)).return_type
!= null then
1205 var stypes
= new Array[MMType]
1206 var stype
: nullable MMType = null
1207 for prop
in precs
do
1208 assert prop
isa MMMethod
1209 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
)
1211 if stype
== null or stype
< t
then
1216 v
.check_conform
(self, t
, stype
.as(not null))
1220 var p
= v
.local_property
1221 assert p
isa MMSrcMethod
1227 redef class AExternCall
1228 fun target_class_name
: nullable Symbol do return null
1229 fun target_method_name
: Symbol is abstract
1231 redef fun after_typing
(v
)
1233 var target_class_name
= self.target_class_name
1234 var target_method_name
= self.target_method_name
1236 var target_class
: MMLocalClass
1237 var target_method
: MMMethod
1240 # self.target_class_name can be redef'd by sub-classes
1241 if target_class_name
== null then
1242 target_class
= v
.local_property
.local_class
1244 if v
.local_property
.mmmodule
.has_global_class_named
( target_class_name
) then
1245 var global_class
= v
.local_property
.mmmodule
.global_class_named
( target_class_name
)
1246 target_class
= v
.local_property
.mmmodule
[ global_class
]
1248 v
.error
( self, "Error: class {target_class_name.to_s}, not found." )
1253 if target_class
.has_global_property_by_name
( target_method_name
) then
1254 var global_property
= target_class
.get_property_by_name
( target_method_name
)
1256 var target_property
= target_class
[global_property
]
1258 if target_property
isa MMMethod then
1259 target_method
= target_property
1261 v
.error
( self, "Error: property {target_method_name.to_s} is not a method." )
1265 v
.error
( self, "Error: property {target_method_name.to_s} not found in target class." )
1269 var explicit_import
= new MMExplicitImport( target_class
, target_method
)
1270 v
.local_property
.as(MMSrcMethod).explicit_imports
.add
( explicit_import
)
1274 redef class ALocalPropExternCall
1275 redef fun target_class_name
do return null
1276 redef fun target_method_name
do return n_methid
.name
.as(not null)
1279 redef class ASuperExternCall
1280 redef fun after_typing
(v
)
1282 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
1283 if not precs
.is_empty
then
1284 v
.local_property
.need_super
= true
1286 v
.error
(self, "Error: No super method to call for {v.local_property}.")
1292 redef class AFullPropExternCall
1293 redef fun target_class_name
do return n_classid
.to_symbol
1294 redef fun target_method_name
do return n_methid
.name
.as(not null)
1297 redef class AInitPropExternCall
1298 redef fun target_class_name
do return n_classid
.to_symbol
1299 redef fun target_method_name
do return "init".to_symbol
1302 redef class ACastExternCall
1303 fun from_type
: MMType is abstract
1304 fun to_type
: MMType is abstract
1306 redef fun after_typing
(v
)
1308 if from_type
== to_type
1310 v
.error
( self, "Attepting to cast from and to the same type." )
1313 var cast
= new MMImportedCast( from_type
, to_type
)
1314 var m
= v
.local_property
1315 assert m
isa MMMethod
1316 m
.explicit_casts
.add
( cast
)
1320 redef class ACastAsExternCall
1321 redef fun from_type
do return n_from_type
.stype
1322 redef fun to_type
do return n_to_type
.stype
1325 redef class AAsNullableExternCall
1326 redef fun from_type
do return n_type
.stype
1327 redef fun to_type
do return n_type
.stype
.as_nullable
1330 redef class AAsNotNullableExternCall
1333 var t
= n_type
.stype
1338 return t
.as_nullable
1341 redef fun to_type
do return n_type
.stype
.as_notnull
1344 redef class AAttrFormExpr
1345 redef fun prop
do return _prop
.as(not null)
1346 var _prop
: nullable MMAttribute
1348 redef fun attr_type
do return _attr_type
.as(not null)
1349 var _attr_type
: nullable MMType
1351 # Compute the attribute accessed
1352 private fun do_typing
(v
: TypingVisitor)
1354 if not v
.check_expr
(n_expr
) then return
1355 var type_recv
= n_expr
.stype
1356 var name
= n_id
.to_symbol
1357 if type_recv
isa MMTypeNone then
1358 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1361 var lc
= type_recv
.local_class
1362 if not lc
.has_global_property_by_name
(name
) then
1363 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
1366 var prop
= lc
.select_attribute
(name
)
1367 if v
.mmmodule
.visibility_for
(prop
.global
.local_class
.mmmodule
) < 3 then
1368 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.mmmodule} is invisible in {v.mmmodule}")
1371 var at
= prop
.signature_for
(type_recv
).return_type
1372 if not n_expr
.is_self
then at
= at
.not_for_self
1377 redef class AAttrExpr
1378 redef fun after_typing
(v
)
1381 if _prop
== null then return
1387 redef class AAttrAssignExpr
1388 redef fun after_typing
(v
)
1391 if _prop
== null then return
1392 if not v
.check_conform_expr
(n_value
, attr_type
) then return
1397 redef class AAttrReassignExpr
1398 redef fun after_typing
(v
)
1401 if _prop
== null then return
1402 var t
= do_rvalue_typing
(v
, attr_type
)
1403 if t
== null then return
1404 v
.check_conform
(self, t
, n_value
.stype
)
1409 redef class AIssetAttrExpr
1410 redef fun after_typing
(v
)
1413 if _prop
== null then return
1414 if attr_type
.is_nullable
then
1415 v
.error
(self, "Error: isset on a nullable attribute.")
1417 _stype
= v
.type_bool
1422 redef class AAbsAbsSendExpr
1423 # The signature of the called property
1424 redef fun prop_signature
do return _prop_signature
.as(not null)
1425 var _prop_signature
: nullable MMSignature
1427 # Raw arguments used (without vararg transformation)
1428 redef fun raw_arguments
: Array[AExpr]
1430 var res
= _raw_arguments_cache
1434 res
= compute_raw_arguments
1435 if res
== null then res
= new Array[AExpr]
1436 _raw_arguments_cache
= res
1441 var _raw_arguments_cache
: nullable Array[AExpr] = null
1443 fun compute_raw_arguments
: nullable Array[AExpr]
1445 print
"{location} no compute_raw_arguments"
1449 # Check the conformity of a set of arguments `raw_args' to a signature.
1450 private fun process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: nullable Array[AExpr]): Bool
1452 var par_vararg
= psig
.vararg_rank
1453 var par_arity
= psig
.arity
1455 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
1456 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
1457 v
.error
(self, "Error: arity mismatch; prototype is '{name}{psig}'.")
1461 for par_idx
in [0..par_arity
[ do
1463 var par_type
= psig
[par_idx
]
1464 if par_idx
== par_vararg
then
1465 for i
in [0..(raw_arity-par_arity
)] do
1466 a
= raw_args
[arg_idx
]
1467 v
.check_conform_expr
(a
, par_type
)
1468 arg_idx
= arg_idx
+ 1
1471 a
= raw_args
[arg_idx
]
1472 v
.check_conform_expr
(a
, par_type
)
1473 arg_idx
= arg_idx
+ 1
1479 # Check the conformity of a set of defined closures
1480 private fun process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: nullable Array[AClosureDef]): nullable MMType
1482 var t
= psig
.return_type
1483 var cs
= psig
.closures
# Declared closures
1486 if not c
.is_optional
then min_arity
+= 1
1489 if cd
!= null then arity
= cd
.length
1490 if cs
.length
> 0 then
1491 if arity
== 0 and min_arity
> 0 then
1492 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1493 else if arity
> cs
.length
or arity
< min_arity
then
1494 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
1496 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1497 var break_list
: nullable Array[ABreakExpr] = null
1498 if t
!= null then break_list
= new Array[ABreakExpr]
1500 # The n_label, is any in only set on the last decl
1501 var n_label
= if arity
> 0 then cd
[arity-1
].n_label
else null
1503 # Process each closure definition
1504 for i
in [0..arity
[ do
1506 var cni
= cdi
.n_id
.to_symbol
1507 var csi
= psig
.closure_named
(cni
)
1509 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1510 v
.scope_ctx
.push_escapable
(esc
, n_label
)
1511 cdi
.accept_typing2
(v
, esc
)
1513 else if cs
.length
== 1 then
1514 v
.error
(cdi
.n_id
, "Error: no closure named '!{cni}' in {name}; only closure is !{cs.first.name}.")
1516 var a
= new Array[String]
1520 v
.error
(cdi
.n_id
, "Error: no closure named '!{cni}' in {name}; only closures are {a.join(",")}.")
1524 # Check break type conformity
1525 if break_list
!= null then
1526 t
= v
.check_conform_multiexpr
(t
, break_list
)
1529 else if arity
!= 0 then
1530 v
.error
(self, "Error: {name} does not require blocks.")
1536 redef class AAbsSendExpr
1537 # Compute the called global property
1538 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])
1540 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
1541 if prop
== null then return
1542 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
1543 if not process_signature
(v
, sig
, prop
.name
, raw_args
) then return
1544 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
1545 if rtype
== null and sig
.return_type
!= null then return
1547 _prop_signature
= sig
1548 _return_type
= rtype
1551 private fun get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): nullable MMMethod
1553 if type_recv
isa MMTypeNone then
1554 if name
== (once
"==".to_symbol
) or name
== (once
"!=".to_symbol
) then
1555 # Special case on != and == that are allowed for 'null'
1556 type_recv
= v
.type_object
.as_nullable
1558 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1562 var lc
= type_recv
.local_class
1563 var prop
: nullable MMMethod = null
1564 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
1565 if prop
== null then
1566 var props
= lc
.super_methods_named
(name
)
1567 if props
.length
> 1 then
1568 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1570 else if props
.length
== 1 then
1571 var p
= lc
[props
.first
.global
]
1572 assert p
isa MMMethod
1577 if prop
== null then
1578 if is_implicit_self
then
1579 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1581 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1588 # Get the signature for a local property and a receiver
1589 private fun get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
1591 prop
.global
.check_visibility
(v
, self, v
.mmmodule
, recv_is_self
)
1592 var psig
= prop
.signature_for
(type_recv
)
1593 if not recv_is_self
then psig
= psig
.not_for_self
1597 # The invoked method (once computed)
1598 redef fun prop
do return _prop
.as(not null)
1599 var _prop
: nullable MMMethod
1601 # The return type (if any) (once computed)
1602 redef readable var _return_type
: nullable MMType
1605 # A possible call of constructor in a super class
1606 # Could be an explicit call or with the 'super' keyword
1607 redef class ASuperInitCall
1608 private fun register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1610 if parent
!= v
.top_block
and self != v
.top_block
then
1611 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1613 var cla
= v
.mmmodule
[property
.global
.intro
.local_class
.global
]
1614 var prev_class
: nullable MMLocalClass = null
1615 var esic
= v
.explicit_super_init_calls
.as(not null)
1616 if not esic
.is_empty
then
1617 prev_class
= esic
.last
.global
.intro
.local_class
1619 var order
= v
.local_class
.cshe
.reverse_linear_extension
1620 if cla
== v
.local_class
then
1621 v
.explicit_other_init_call
= true
1622 else if not order
.has
(cla
) then
1623 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1624 else if cla
== prev_class
then
1625 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1628 if c
== prev_class
then
1630 else if c
== cla
then
1640 redef class ANewExpr
1641 redef fun compute_raw_arguments
do return n_args
.to_a
1642 redef fun after_typing
(v
)
1644 if not n_type
.is_typed
then return
1645 var t
= n_type
.stype
1646 if t
.local_class
.global
.is_abstract
then
1647 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1650 if t
.is_nullable
then
1651 v
.error
(self, "Type error: cannot instantiate the nullable type {t}.")
1654 if n_id
== null then
1655 name
= once
"init".to_symbol
1657 name
= n_id
.to_symbol
1660 do_typing
(v
, t
, false, false, name
, raw_arguments
, null)
1661 if _prop
== null then return
1663 if not prop
.global
.is_init
then
1664 v
.error
(self, "Error: {prop} is not a constructor.")
1667 if not prop
.global
.is_init_for
(t
.local_class
) then
1668 v
.error
(self, "Error: {prop} is not a constructor in {t.local_class}.")
1677 redef class ASendExpr
1678 # Name of the invoked property
1679 fun name
: Symbol is abstract
1681 # Closure definitions
1682 redef fun closure_defs
: nullable Array[AClosureDef] do return null
1684 redef fun after_typing
(v
)
1689 private fun do_all_typing
(v
: TypingVisitor)
1691 if not v
.check_expr
(n_expr
) then return
1692 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1693 if _prop
== null then return
1694 var prop
= _prop
.as(not null)
1696 if prop
.global
.is_init
then
1697 if not v
.local_property
.global
.is_init
then
1698 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1699 else if not n_expr
.is_self
then
1700 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1702 register_super_init_call
(v
, prop
)
1706 _stype
= return_type
1711 redef class ASendReassignExpr
1712 redef fun read_prop
do return _read_prop
.as(not null)
1713 var _read_prop
: nullable MMMethod
1714 redef fun do_all_typing
(v
)
1716 if not v
.check_expr
(n_expr
) then return
1717 var raw_args
= raw_arguments
1718 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1720 if prop
== null then return
1721 if prop
.global
.is_init
then
1722 if not v
.local_property
.global
.is_init
then
1723 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1724 else if not n_expr
.is_self
then
1725 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1728 var t
= prop
.signature_for
(n_expr
.stype
).return_type
.as(not null)
1729 if not n_expr
.is_self
then t
= t
.not_for_self
1731 var t2
= do_rvalue_typing
(v
, t
)
1732 if t2
== null then return
1733 v
.check_conform
(self, t2
, n_value
.stype
)
1736 raw_args
= raw_args
.to_a
1737 raw_args
.add
(n_value
)
1739 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1740 if prop
.global
.is_init
then
1741 if not v
.local_property
.global
.is_init
then
1742 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1743 else if not n_expr
.is_self
then
1744 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1752 redef class ABinopExpr
1753 redef fun compute_raw_arguments
do return [n_expr2
]
1756 redef fun name
do return once
"==".to_symbol
1757 redef fun after_typing
(v
)
1760 if not n_expr
.is_typed
or not n_expr2
.is_typed
then return
1761 if n_expr
.stype
isa MMTypeNone and not n_expr2
.stype
.is_nullable
or
1762 n_expr2
.stype
isa MMTypeNone and not n_expr
.stype
.is_nullable
then
1763 v
.warning
(self, "Warning: comparaison between null and a non nullable value.")
1766 if n_expr
.stype
isa MMTypeNone then
1767 if n_expr2
.stype
isa MMTypeNone then
1768 v
.warning
(self, "Warning: comparaison between two null values.")
1770 try_to_isa
(v
, n_expr2
)
1772 else if n_expr2
.stype
isa MMTypeNone then
1773 try_to_isa
(v
, n_expr
)
1777 private fun try_to_isa
(v
: TypingVisitor, n
: AExpr)
1779 var variable
= n
.its_variable
1780 if variable
!= null and n
.stype
isa MMNullableType then
1781 _if_false_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, n
.stype
.as_notnull
)
1782 _if_true_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, v
.type_none
)
1787 redef fun name
do return once
"!=".to_symbol
1788 redef fun after_typing
(v
)
1791 if not n_expr
.is_typed
or not n_expr2
.is_typed
then return
1792 if n_expr
.stype
isa MMTypeNone and not n_expr2
.stype
.is_nullable
or
1793 n_expr2
.stype
isa MMTypeNone and not n_expr
.stype
.is_nullable
then
1794 v
.warning
(self, "Warning: comparaison between null and a non nullable value.")
1797 if n_expr
.stype
isa MMTypeNone then
1798 if n_expr2
.stype
isa MMTypeNone then
1799 v
.warning
(self, "Warning: comparaison between two null values.")
1801 try_to_isa
(v
, n_expr2
)
1803 else if n_expr2
.stype
isa MMTypeNone then
1804 try_to_isa
(v
, n_expr
)
1808 private fun try_to_isa
(v
: TypingVisitor, n
: AExpr)
1810 var variable
= n
.its_variable
1811 if variable
!= null and n
.stype
isa MMNullableType then
1812 _if_true_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, n
.stype
.as_notnull
)
1813 _if_false_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, v
.type_none
)
1818 redef fun name
do return once
"<".to_symbol
1821 redef fun name
do return once
"<=".to_symbol
1824 redef fun name
do return once
"<<".to_symbol
1827 redef fun name
do return once
">".to_symbol
1830 redef fun name
do return once
">=".to_symbol
1833 redef fun name
do return once
">>".to_symbol
1835 redef class APlusExpr
1836 redef fun name
do return once
"+".to_symbol
1838 redef class AMinusExpr
1839 redef fun name
do return once
"-".to_symbol
1841 redef class AStarshipExpr
1842 redef fun name
do return once
"<=>".to_symbol
1844 redef class AStarExpr
1845 redef fun name
do return once
"*".to_symbol
1847 redef class ASlashExpr
1848 redef fun name
do return once
"/".to_symbol
1850 redef class APercentExpr
1851 redef fun name
do return once
"%".to_symbol
1854 redef class AUminusExpr
1855 redef fun name
do return once
"unary -".to_symbol
1856 redef fun compute_raw_arguments
do return null
1859 redef class ACallFormExpr
1860 redef fun after_typing
(v
)
1862 if n_expr
.is_implicit_self
then
1863 var name
= n_id
.to_symbol
1864 var variable
= v
.scope_ctx
[name
]
1865 if variable
!= null then
1867 if variable
isa ClosureVariable then
1868 n
= new AClosureCallExpr.init_aclosurecallexpr
(n_id
, n_args
, n_closure_defs
)
1869 n
._variable
= variable
1871 if not n_args
.n_exprs
.is_empty
or n_args
isa AParExprs then
1872 v
.error
(self, "Error: {name} is variable, not a function.")
1875 n
= variable_create
(variable
)
1876 n
._variable
= variable
1887 redef fun closure_defs
1889 if n_closure_defs
.is_empty
then
1892 return n_closure_defs
.to_a
1896 # Create a variable acces corresponding to the call form
1897 fun variable_create
(variable
: Variable): AVarFormExpr is abstract
1900 redef class ACallExpr
1901 redef fun variable_create
(variable
)
1903 return new AVarExpr.init_avarexpr
(n_id
)
1906 redef fun name
do return n_id
.to_symbol
1907 redef fun compute_raw_arguments
do return n_args
.to_a
1910 redef class ACallAssignExpr
1911 redef fun variable_create
(variable
)
1913 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1916 redef fun name
do return (n_id
.text
+ "=").to_symbol
1917 redef fun compute_raw_arguments
do
1918 var res
= n_args
.to_a
1924 redef class ACallReassignExpr
1925 redef fun variable_create
(variable
)
1927 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1930 redef fun name
do return n_id
.to_symbol
1931 redef fun compute_raw_arguments
do return n_args
.to_a
1934 redef class ABraExpr
1935 redef fun name
do return once
"[]".to_symbol
1936 redef fun compute_raw_arguments
do return n_args
.to_a
1937 redef fun closure_defs
1939 if n_closure_defs
.is_empty
then
1942 return n_closure_defs
.to_a
1947 redef class ABraAssignExpr
1948 redef fun name
do return once
"[]=".to_symbol
1949 redef fun compute_raw_arguments
do
1950 var res
= n_args
.to_a
1956 redef class ABraReassignExpr
1957 redef fun name
do return once
"[]".to_symbol
1958 redef fun compute_raw_arguments
do return n_args
.to_a
1961 redef class AInitExpr
1962 redef fun name
do return once
"init".to_symbol
1963 redef fun compute_raw_arguments
do return n_args
.to_a
1966 redef class AClosureCallExpr
1967 var _variable
: nullable ClosureVariable
1968 redef fun variable
do return _variable
.as(not null)
1969 redef fun compute_raw_arguments
do return n_args
.to_a
1971 redef fun after_typing
(v
)
1974 if va
.closure
.is_break
then v
.mark_unreash
(self)
1975 var sig
= va
.closure
.signature
1976 var s
= process_signature
(v
, sig
, n_id
.to_symbol
, compute_raw_arguments
)
1977 if not n_closure_defs
.is_empty
then
1978 process_closures
(v
, sig
, n_id
.to_symbol
, n_closure_defs
.to_a
)
1980 if not s
then return
1981 _prop_signature
= sig
1982 _stype
= sig
.return_type
1987 redef class AClosureId
1988 fun to_symbol
: Symbol is abstract
1990 redef class ASimpleClosureId
1991 redef fun to_symbol
: Symbol do return n_id
.to_symbol
1993 redef class ABreakClosureId
1994 redef fun to_symbol
: Symbol do return n_kwbreak
.to_symbol
1997 redef class AClosureDef
1998 var _closure
: nullable MMClosure
1999 redef fun closure
do return _closure
.as(not null)
2001 # The corresponding escapable object
2002 readable var _escapable
: nullable EscapableBlock
2004 var _accept_typing2
: Bool = false
2005 redef fun accept_typing
(v
)
2007 # Typing is deferred, wait accept_typing2(v)
2008 if _accept_typing2
then super
2011 private fun accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure)
2015 var sig
= esc
.closure
.signature
2016 if sig
.arity
!= n_ids
.length
then
2017 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_ids.length} found.")
2021 _closure
= esc
.closure
2023 v
.scope_ctx
.push
(self)
2024 var old_flow_ctx
= v
.flow_ctx
2025 var old_base_flow_ctx
= v
.base_flow_ctx
2026 v
.base_flow_ctx
= v
.flow_ctx
2027 variables
= new Array[AutoVariable]
2028 for i
in [0..n_ids
.length
[ do
2029 var va
= new AutoVariable(n_ids
[i
].to_symbol
, n_ids
[i
])
2032 v
.scope_ctx
.add_variable
(va
)
2035 _accept_typing2
= true
2038 if v
.flow_ctx
.unreash
== false then
2039 if closure
.signature
.return_type
!= null then
2040 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
2041 else if closure
.is_break
and esc
.break_list
!= null then
2042 v
.error
(self, "Control error: Reached end of break block (a 'break' with a value was expected).")
2045 v
.flow_ctx
= old_flow_ctx
2046 v
.base_flow_ctx
= old_base_flow_ctx
2051 abstract class ATypeCheckExpr
2053 private fun check_expr_cast
(v
: TypingVisitor, n_expr
: AExpr, n_type
: AType)
2055 if not v
.check_expr
(n_expr
) then return
2056 if not n_type
.is_typed
then return
2057 var etype
= n_expr
.stype
2058 var ttype
= n_type
.stype
2059 if etype
== ttype
then
2060 v
.warning
(self, "Warning: Expression is already a {ttype}.")
2061 else if etype
< ttype
then
2062 if not ttype
.has_formal
and not etype
.has_formal
then
2063 # the old metamodel is not that great with formal types
2064 v
.warning
(self, "Warning: Expression is already a {ttype} since it is a {etype}.")
2066 else if etype
isa MMTypeNone then
2067 # ttype is not nullable because of prevous test
2068 v
.warning
(self, "Warning: Expression is null therefore cannot be a {ttype}.")
2069 else if etype
.is_nullable
and etype
.as_notnull
== ttype
then
2070 if ttype
isa MMTypeFormal and ttype
.bound
.is_nullable
then
2071 # No warning in this case since with
2072 # type T: nullable A
2074 # 'x.as(not null)' != 'x.as(T)'
2075 # 'x != null' != 'x isa T'
2076 else if self isa AIsaExpr then
2077 v
.warning
(self, "Warning: Prefer '!= null'.")
2079 v
.warning
(self, "Warning: Prefer '.as(not null)'.")
2085 redef class AIsaExpr
2086 super ATypeCheckExpr
2087 redef fun after_typing
(v
)
2089 check_expr_cast
(v
, n_expr
, n_type
)
2090 if not n_type
.is_typed
then return
2091 var variable
= n_expr
.its_variable
2092 if variable
!= null then
2093 _if_true_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, n_type
.stype
)
2095 _stype
= v
.type_bool
2100 redef class AAsCastExpr
2101 super ATypeCheckExpr
2102 redef fun after_typing
(v
)
2104 check_expr_cast
(v
, n_expr
, n_type
)
2105 if not n_type
.is_typed
then return
2106 _stype
= n_type
.stype
2107 _is_typed
= _stype
!= null
2111 redef class AAsNotnullExpr
2112 redef fun after_typing
(v
)
2114 if not v
.check_expr
(n_expr
) then return
2115 var t
= n_expr
.stype
2116 if t
isa MMTypeNone then
2117 v
.error
(n_expr
, "Type error: 'as(not null)' on 'null' value.")
2119 else if not t
.is_nullable
then
2120 v
.warning
(n_expr
, "Warning: 'as(not null)' on non nullable type.")
2122 _stype
= n_expr
.stype
.as_notnull
2127 redef class AProxyExpr
2128 redef fun after_typing
(v
)
2130 if not n_expr
.is_typed
then return
2132 if n_expr
.is_statement
then return
2133 _stype
= n_expr
.stype
2134 _if_true_flow_ctx
= n_expr
._if_true_flow_ctx
2135 _if_false_flow_ctx
= n_expr
._if_false_flow_ctx
2138 redef fun is_self
do return n_expr
.is_self
2140 redef fun its_variable
do return n_expr
.its_variable
2143 redef class AOnceExpr
2144 redef fun accept_typing
(v
)
2146 if v
.once_count
> 0 then
2147 v
.warning
(self, "Useless once in a once expression.")
2149 v
.once_count
= v
.once_count
+ 1
2153 v
.once_count
= v
.once_count
- 1
2157 redef class ADebugTypeExpr
2158 redef fun after_typing
(v
)
2160 if not v
.check_expr
(n_expr
) then return
2161 if not n_type
.is_typed
then return
2162 var etype
= n_expr
.stype
2163 var ttype
= n_type
.stype
2164 if etype
!= ttype
then
2165 v
.warning
(self, "Warning: Expression is a {etype}, expected {ttype}.")