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
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 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
446 va
.stype
= v
.type_object
.as_nullable
452 redef class ABlockExpr
453 redef fun accept_typing
(v
)
456 if not v
.flow_ctx
.unreash
then
458 else if not v
.flow_ctx
.already_unreash
then
459 v
.flow_ctx
.already_unreash
= true
460 v
.error
(e
, "Error: unreachable statement.")
468 redef class AReturnExpr
469 redef fun after_typing
(v
)
472 var t
= v
.local_property
.signature
.return_type
474 if v
.is_default_closure_definition
then
475 v
.error
(self, "Error: 'return' invalid in default closure definitions. Use 'continue' or 'break'.")
480 if e
== null and t
!= null then
481 v
.error
(self, "Error: Return without value in a function.")
482 else if e
!= null and t
== null then
483 v
.error
(self, "Error: Return with value in a procedure.")
484 else if e
!= null and t
!= null then
485 v
.check_conform_expr
(e
, t
)
488 e
.warn_parentheses
(v
)
494 redef class AContinueExpr
495 redef fun after_typing
(v
)
498 var esc
= compute_escapable_block
(v
.scope_ctx
)
499 if esc
== null then return
501 if esc
.is_break_block
then
502 v
.error
(self, "Error: cannot 'continue', only 'break'.")
506 var t
= esc
.continue_stype
508 if e
== null and t
!= null then
509 v
.error
(self, "Error: continue with a value required in this block.")
510 else if e
!= null and t
== null then
511 v
.error
(self, "Error: continue without value required in this block.")
512 else if e
!= null and t
!= null then
513 v
.check_conform_expr
(e
, t
)
516 e
.warn_parentheses
(v
)
522 redef class ABreakExpr
523 redef fun after_typing
(v
)
525 var old_flow_ctx
= v
.flow_ctx
527 var esc
= compute_escapable_block
(v
.scope_ctx
)
528 if esc
== null then return
530 esc
.break_flow_contexts
.add
(old_flow_ctx
)
532 var bl
= esc
.break_list
534 if e
== null and bl
!= null then
535 v
.error
(self, "Error: break with a value required in this block.")
536 else if e
!= null and bl
== null then
537 v
.error
(self, "Error: break without value required in this block.")
538 else if e
!= null and bl
!= null then
539 # Typing check can only be done later
543 e
.warn_parentheses
(v
)
549 redef class AAbortExpr
550 redef fun after_typing
(v
)
557 # An abstract control structure with feature escapable block
558 abstract class AAbsControl
560 # The corresponding escapable block
561 readable var _escapable
: nullable EscapableBlock
563 # Enter and process a control structure
564 private fun process_control
(v
: TypingVisitor, escapable
: EscapableBlock, n_label
: nullable ALabel, is_loop
: Bool)
566 # Register the escapable block
567 _escapable
= escapable
568 v
.scope_ctx
.push_escapable
(escapable
, n_label
)
570 # Save an prepare the contextes
571 var old_flow_ctx
= v
.flow_ctx
572 var old_base_flow_ctx
= v
.base_flow_ctx
573 if is_loop
then v
.base_flow_ctx
= v
.flow_ctx
575 # Do the main processing
576 process_control_inside
(v
)
578 # Add the end of the block as an exit context
579 if not v
.flow_ctx
.unreash
then
580 escapable
.break_flow_contexts
.add
(v
.flow_ctx
)
583 # Merge all exit contexts
584 if escapable
.break_flow_contexts
.is_empty
then
585 v
.flow_ctx
= old_flow_ctx
588 v
.flow_ctx
= old_base_flow_ctx
.merge
(self, escapable
.break_flow_contexts
)
591 if is_loop
then v
.base_flow_ctx
= old_base_flow_ctx
596 # What to do inside the control block?
597 private fun process_control_inside
(v
: TypingVisitor) is abstract
602 redef fun accept_typing
(v
)
604 process_control
(v
, new BreakOnlyEscapableBlock(self), n_label
, false)
607 redef fun process_control_inside
(v
)
609 v
.enter_visit_block
(n_block
)
614 redef fun accept_typing
(v
)
616 v
.enter_visit
(n_expr
)
617 v
.check_conform_expr
(n_expr
, v
.type_bool
)
619 n_expr
.warn_parentheses
(v
)
621 # Prepare 'then' context
622 var old_flow_ctx
= v
.flow_ctx
623 v
.use_if_true_flow_ctx
(n_expr
)
626 v
.enter_visit_block
(n_then
)
628 # Remember what appened in the 'then'
629 var then_flow_ctx
= v
.flow_ctx
631 # Prepare 'else' context
632 v
.flow_ctx
= old_flow_ctx
633 v
.use_if_false_flow_ctx
(n_expr
)
636 v
.enter_visit_block
(n_else
)
638 # Merge 'then' and 'else' contexts
639 v
.flow_ctx
= v
.base_flow_ctx
.merge_reash
(self, then_flow_ctx
, v
.flow_ctx
)
644 redef class AWhileExpr
646 redef fun accept_typing
(v
)
648 process_control
(v
, new EscapableBlock(self), n_label
, true)
651 redef fun process_control_inside
(v
)
653 var old_flow_ctx
= v
.flow_ctx
656 v
.enter_visit
(n_expr
)
657 v
.check_conform_expr
(n_expr
, v
.type_bool
)
659 if n_expr
isa ATrueExpr then
660 v
.warning
(self, "Warning: use 'loop' instead of 'while true do'.")
662 n_expr
.warn_parentheses
(v
)
665 # Prepare inside context (assert cond)
666 v
.use_if_true_flow_ctx
(n_expr
)
669 v
.enter_visit_block
(n_block
)
671 # Compute outside context (assert !cond + all breaks)
672 v
.flow_ctx
= old_flow_ctx
673 v
.use_if_false_flow_ctx
(n_expr
)
674 escapable
.break_flow_contexts
.add
(v
.flow_ctx
)
678 redef class ALoopExpr
680 redef fun accept_typing
(v
)
682 process_control
(v
, new EscapableBlock(self), n_label
, true)
685 redef fun process_control_inside
(v
)
688 v
.enter_visit_block
(n_block
)
690 # Never automatically reach after the loop
697 var _variables
: nullable Array[AutoVariable]
698 redef fun variables
do return _variables
.as(not null)
700 redef fun accept_typing
(v
)
702 process_control
(v
, new EscapableBlock(self), n_label
, true)
705 redef fun process_control_inside
(v
)
707 v
.scope_ctx
.push
(self)
708 var old_flow_ctx
= v
.flow_ctx
713 v
.enter_visit_block
(n_block
)
715 # end == begin of the loop
716 v
.flow_ctx
= old_flow_ctx
720 private fun do_typing
(v
: TypingVisitor)
722 # Create the automatic variables
723 var vas
= new Array[AutoVariable]
725 var va
= new AutoVariable(n_id
.to_symbol
, n_id
)
726 v
.scope_ctx
.add_variable
(va
)
732 v
.enter_visit
(n_expr
)
733 if not v
.check_expr
(n_expr
) then return
734 var expr_type
= n_expr
.stype
736 if expr_type
.is_nullable
then
737 v
.error
(n_expr
, "Type error: 'for' on a nullable expression.")
740 n_expr
.warn_parentheses
(v
)
743 var iterate_name
= once
"iterate".to_symbol
744 if not expr_type
.local_class
.has_global_property_by_name
(iterate_name
) then
745 v
.error
(n_expr
, "Type error: Expected a type with an 'iterate' method. Found {expr_type}.")
748 var prop
= expr_type
.local_class
.select_method
(iterate_name
)
749 prop
.global
.check_visibility
(v
, self, v
.mmmodule
, n_expr
.is_self
)
750 var psig
= prop
.signature_for
(expr_type
)
751 if not n_expr
.is_self
then psig
= psig
.not_for_self
752 if psig
.arity
!= 0 then
753 v
.error
(self, "Error: 'iterate' incompatible with 'for': require no arguments.")
755 else if psig
.closures
.length
!= 1 then
756 v
.error
(self, "Error: 'iterate' incompatible with 'for': require one closure.")
759 psig
= psig
.closures
.first
.signature
760 if psig
.return_type
!= null then
761 v
.error
(self, "Error: 'iterate' incompatible with 'for': require one procedural closure.")
764 if vas
.length
!= psig
.arity
then
765 if psig
.arity
== 1 then
766 v
.error
(self, "Error: Expected {psig.arity} variable {psig}, found {vas.length}.")
768 v
.error
(self, "Error: Expected {psig.arity} variables {psig}, found {vas.length}.")
773 # Type the automatic variables
774 for i
in [0..vas
.length
[ do
775 vas
[i
].stype
= psig
[i
]
780 redef class AAssertExpr
781 redef fun accept_typing
(v
)
784 v
.enter_visit
(n_expr
)
785 v
.check_conform_expr
(n_expr
, v
.type_bool
)
786 n_expr
.warn_parentheses
(v
)
788 # Process optional 'else' part
789 if n_else
!= null then
790 var old_flow_ctx
= v
.flow_ctx
791 v
.use_if_false_flow_ctx
(n_expr
)
792 v
.enter_visit
(n_else
)
793 v
.flow_ctx
= old_flow_ctx
797 v
.use_if_true_flow_ctx
(n_expr
)
802 redef class AVarFormExpr
803 var _variable
: nullable Variable
804 redef fun variable
do return _variable
.as(not null)
808 redef fun its_variable
do return variable
810 redef fun after_typing
(v
)
812 v
.flow_ctx
.check_is_set
(self, variable
)
813 _stype
= v
.flow_ctx
.stype
(variable
)
814 _is_typed
= _stype
!= null
818 redef class AVarAssignExpr
819 redef fun after_typing
(v
)
821 v
.mark_is_set
(variable
)
823 # Check the base type
824 var btype
= v
.base_flow_ctx
.stype
(variable
)
825 if not v
.check_expr
(n_value
) then return
826 if btype
!= null and not v
.check_conform_expr
(n_value
, btype
) then return
829 v
.flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, n_value
.stype
)
835 redef class AReassignFormExpr
836 # Compute and check method used through the reassigment operator
837 # On success return the static type of the result of the reassigment operator
838 # Else display an error and return null
839 private fun do_rvalue_typing
(v
: TypingVisitor, type_lvalue
: nullable MMType): nullable MMType
841 if type_lvalue
== null then
844 var name
= n_assign_op
.method_name
845 if type_lvalue
isa MMTypeNone then
846 v
.error
(self, "Error: Method '{name}' call on 'null'.")
849 var lc
= type_lvalue
.local_class
850 if not lc
.has_global_property_by_name
(name
) then
851 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
854 var prop
= lc
.select_method
(name
)
855 prop
.global
.check_visibility
(v
, self, v
.mmmodule
, false)
856 var psig
= prop
.signature_for
(type_lvalue
)
857 _assign_method
= prop
858 if not v
.check_conform_expr
(n_value
, psig
[0].not_for_self
) then return null
859 return psig
.return_type
.not_for_self
862 redef fun assign_method
do return _assign_method
.as(not null)
863 var _assign_method
: nullable MMMethod
866 redef class AVarReassignExpr
867 redef fun after_typing
(v
)
869 v
.flow_ctx
.check_is_set
(self, variable
)
870 v
.mark_is_set
(variable
)
871 var t
= v
.flow_ctx
.stype
(variable
)
872 var t2
= do_rvalue_typing
(v
, t
)
873 if t2
== null then return
875 # Check the base type
876 var btype
= v
.base_flow_ctx
.stype
(variable
)
877 if not v
.check_expr
(n_value
) then return
878 if btype
!= null and not v
.check_conform
(n_value
, t2
, btype
) then return
881 v
.flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, t2
)
887 redef class AAssignOp
888 fun method_name
: Symbol is abstract
890 redef class APlusAssignOp
891 redef fun method_name
do return once
"+".to_symbol
893 redef class AMinusAssignOp
894 redef fun method_name
do return once
"-".to_symbol
897 redef class ASelfExpr
898 var _variable
: nullable ParamVariable
899 redef fun variable
do return _variable
.as(not null)
901 redef fun its_variable
do return variable
903 redef fun after_typing
(v
)
905 _variable
= v
.self_var
906 _stype
= v
.flow_ctx
.stype
(variable
)
910 redef fun is_self
do return true
913 redef class AImplicitSelfExpr
914 redef fun is_implicit_self
do return true
917 redef class AIfexprExpr
918 redef fun accept_typing
(v
)
920 var old_flow_ctx
= v
.flow_ctx
923 v
.enter_visit
(n_expr
)
924 v
.check_conform_expr
(n_expr
, v
.type_bool
)
926 # Prepare 'then' context
927 v
.use_if_true_flow_ctx
(n_expr
)
930 v
.enter_visit_block
(n_then
)
932 # Remember what appened in the 'then'
933 var then_flow_ctx
= v
.flow_ctx
935 # Prepare 'else' context
936 v
.flow_ctx
= old_flow_ctx
937 v
.use_if_false_flow_ctx
(n_expr
)
940 v
.enter_visit_block
(n_else
)
942 # Merge 'then' and 'else' contexts
943 v
.flow_ctx
= v
.base_flow_ctx
.merge_reash
(self, then_flow_ctx
, v
.flow_ctx
)
945 var stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
946 if stype
== null then return
953 redef class ABoolExpr
954 redef fun after_typing
(v
)
962 redef fun accept_typing
(v
)
964 var old_flow_ctx
= v
.flow_ctx
965 var stype
= v
.type_bool
968 # Process left operand
969 v
.enter_visit
(n_expr
)
971 # Prepare right operand context
972 v
.use_if_false_flow_ctx
(n_expr
)
974 # Process right operand
975 v
.enter_visit
(n_expr2
)
976 if n_expr2
.if_false_flow_ctx
!= null then
977 _if_false_flow_ctx
= n_expr2
.if_false_flow_ctx
979 _if_false_flow_ctx
= v
.flow_ctx
982 v
.flow_ctx
= old_flow_ctx
984 v
.check_conform_expr
(n_expr
, stype
)
985 v
.check_conform_expr
(n_expr2
, stype
)
992 redef fun accept_typing
(v
)
994 var old_flow_ctx
= v
.flow_ctx
995 var stype
= v
.type_bool
997 # Process left operand
998 v
.enter_visit
(n_expr
)
1000 # Prepare right operand context
1001 v
.use_if_true_flow_ctx
(n_expr
)
1003 # Process right operand
1004 v
.enter_visit
(n_expr2
)
1005 if n_expr2
.if_true_flow_ctx
!= null then
1006 _if_true_flow_ctx
= n_expr2
.if_true_flow_ctx
1008 _if_true_flow_ctx
= v
.flow_ctx
1011 v
.flow_ctx
= old_flow_ctx
1013 v
.check_conform_expr
(n_expr
, stype
)
1014 v
.check_conform_expr
(n_expr2
, stype
)
1020 redef class ANotExpr
1021 redef fun after_typing
(v
)
1023 v
.check_conform_expr
(n_expr
, v
.type_bool
)
1025 # Invert if_true/if_false information
1026 _if_false_flow_ctx
= n_expr
._if_true_flow_ctx
1027 _if_true_flow_ctx
= n_expr
._if_false_flow_ctx
1029 _stype
= v
.type_bool
1034 redef class AOrElseExpr
1035 redef fun after_typing
(v
)
1037 var old_flow_ctx
= v
.flow_ctx
1039 # Process left operand
1040 v
.enter_visit
(n_expr
)
1041 v
.check_expr
(n_expr
)
1043 # Consider the type of the left operand
1044 var t
= n_expr
.stype
1045 if not t
.is_nullable
then
1046 v
.warning
(n_expr
, "Warning: left operand of a 'or else' is not a nullable type.")
1051 # Prepare the else context : ie the first expression is null
1052 var variable
= n_expr
.its_variable
1053 if variable
!= null then
1054 v
.flow_ctx
.sub_with
(self, variable
, v
.type_none
)
1057 # Process right operand
1058 v
.enter_visit
(n_expr2
)
1059 v
.check_expr
(n_expr
)
1061 # Restore the context
1062 v
.flow_ctx
= old_flow_ctx
1065 var stype
= v
.check_conform_multiexpr
(t
, [n_expr2
])
1066 if stype
== null then return
1073 redef class AIntExpr
1074 redef fun after_typing
(v
)
1081 redef class AFloatExpr
1082 redef fun after_typing
(v
)
1084 _stype
= v
.type_float
1089 redef class ACharExpr
1090 redef fun after_typing
(v
)
1092 _stype
= v
.type_char
1097 redef class AStringFormExpr
1098 redef fun after_typing
(v
)
1100 _stype
= v
.type_string
1105 redef class ASuperstringExpr
1106 redef fun atype
do return _atype
.as(not null)
1107 var _atype
: nullable MMType
1108 redef fun after_typing
(v
)
1110 var otype
= v
.type_object
1111 var stype
= v
.type_string
1113 for e
in n_exprs
do v
.check_conform_expr
(e
, otype
)
1114 var atype
= v
.type_array
(stype
)
1120 redef class ANullExpr
1121 redef fun after_typing
(v
)
1123 _stype
= v
.type_none
1128 redef class AArrayExpr
1129 redef fun after_typing
(v
)
1131 var stype
= v
.check_conform_multiexpr
(null, n_exprs
.n_exprs
)
1132 if stype
!= null then do_typing
(v
, stype
)
1135 private fun do_typing
(v
: TypingVisitor, element_type
: MMType)
1137 _stype
= v
.type_array
(element_type
)
1142 redef class ARangeExpr
1143 redef fun after_typing
(v
)
1145 if not v
.check_expr
(n_expr
) or not v
.check_expr
(n_expr2
) then return
1146 var ntype
= n_expr
.stype
1147 var ntype2
= n_expr2
.stype
1148 if ntype
< ntype2
then
1150 else if not ntype2
< ntype
then
1151 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
1154 var dtype
= v
.type_discrete
1155 if not v
.check_conform_expr
(n_expr
, dtype
) or not v
.check_conform_expr
(n_expr2
, dtype
) then return
1156 _stype
= v
.type_range
(ntype
)
1161 redef class ASuperExpr
1162 redef readable var _init_in_superclass
: nullable MMMethod
1163 redef fun compute_raw_arguments
do return n_args
.to_a
1164 redef fun after_typing
(v
)
1166 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
1167 if not precs
.is_empty
then
1168 v
.local_property
.need_super
= true
1169 else if v
.local_property
.global
.is_init
then
1170 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
1171 for p
in base_precs
do
1172 if not p
.global
.is_init
then
1173 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
1175 precs
.add
(v
.local_class
[p
.global
])
1178 if precs
.is_empty
then
1179 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
1181 else if precs
.length
> 1 then
1182 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
1185 var p
= base_precs
.first
1186 assert p
isa MMMethod
1187 _init_in_superclass
= p
1188 register_super_init_call
(v
, p
)
1189 if n_args
.n_exprs
.length
> 0 then
1190 var signature
= get_signature
(v
, v
.self_var
.stype
.as(not null), p
, true)
1191 process_signature
(v
, signature
, p
.name
, compute_raw_arguments
)
1194 v
.error
(self, "Error: No super method to call for {v.local_property}.")
1198 if precs
.first
.signature_for
(v
.self_var
.stype
.as(not null)).return_type
!= null then
1199 var stypes
= new Array[MMType]
1200 var stype
: nullable MMType = null
1201 for prop
in precs
do
1202 assert prop
isa MMMethod
1203 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
)
1205 if stype
== null or stype
< t
then
1210 v
.check_conform
(self, t
, stype
.as(not null))
1214 var p
= v
.local_property
1215 assert p
isa MMSrcMethod
1221 redef class AExternCall
1222 fun target_class_name
: nullable Symbol do return null
1223 fun target_method_name
: Symbol is abstract
1225 redef fun after_typing
(v
)
1227 var target_class_name
= self.target_class_name
1228 var target_method_name
= self.target_method_name
1230 var target_class
: MMLocalClass
1231 var target_method
: MMMethod
1234 # self.target_class_name can be redef'd by sub-classes
1235 if target_class_name
== null then
1236 target_class
= v
.local_property
.local_class
1238 if v
.local_property
.mmmodule
.has_global_class_named
( target_class_name
) then
1239 var global_class
= v
.local_property
.mmmodule
.global_class_named
( target_class_name
)
1240 target_class
= v
.local_property
.mmmodule
[ global_class
]
1242 v
.error
( self, "Error: class {target_class_name.to_s}, not found." )
1247 if target_class
.has_global_property_by_name
( target_method_name
) then
1248 var global_property
= target_class
.get_property_by_name
( target_method_name
)
1250 var target_property
= target_class
[global_property
]
1252 if target_property
isa MMMethod then
1253 target_method
= target_property
1255 v
.error
( self, "Error: property {target_method_name.to_s} is not a method." )
1259 v
.error
( self, "Error: property {target_method_name.to_s} not found in target class." )
1263 var explicit_import
= new MMExplicitImport( target_class
, target_method
)
1264 v
.local_property
.as(MMSrcMethod).explicit_imports
.add
( explicit_import
)
1268 redef class ALocalPropExternCall
1269 redef fun target_class_name
do return null
1270 redef fun target_method_name
do return n_methid
.name
.as(not null)
1273 redef class ASuperExternCall
1274 redef fun after_typing
(v
)
1276 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
1277 if not precs
.is_empty
then
1278 v
.local_property
.need_super
= true
1280 v
.error
(self, "Error: No super method to call for {v.local_property}.")
1286 redef class AFullPropExternCall
1287 redef fun target_class_name
do return n_classid
.to_symbol
1288 redef fun target_method_name
do return n_methid
.name
.as(not null)
1291 redef class AInitPropExternCall
1292 redef fun target_class_name
do return n_classid
.to_symbol
1293 redef fun target_method_name
do return "init".to_symbol
1296 redef class ACastExternCall
1297 fun from_type
: MMType is abstract
1298 fun to_type
: MMType is abstract
1300 redef fun after_typing
(v
)
1302 if from_type
== to_type
1304 v
.error
( self, "Attepting to cast from and to the same type." )
1307 var cast
= new MMImportedCast( from_type
, to_type
)
1308 var m
= v
.local_property
1309 assert m
isa MMMethod
1310 m
.explicit_casts
.add
( cast
)
1314 redef class ACastAsExternCall
1315 redef fun from_type
do return n_from_type
.stype
1316 redef fun to_type
do return n_to_type
.stype
1319 redef class AAsNullableExternCall
1320 redef fun from_type
do return n_type
.stype
1321 redef fun to_type
do return n_type
.stype
.as_nullable
1324 redef class AAsNotNullableExternCall
1327 var t
= n_type
.stype
1332 return t
.as_nullable
1335 redef fun to_type
do return n_type
.stype
.as_notnull
1338 redef class AAttrFormExpr
1339 redef fun prop
do return _prop
.as(not null)
1340 var _prop
: nullable MMAttribute
1342 redef fun attr_type
do return _attr_type
.as(not null)
1343 var _attr_type
: nullable MMType
1345 # Compute the attribute accessed
1346 private fun do_typing
(v
: TypingVisitor)
1348 if not v
.check_expr
(n_expr
) then return
1349 var type_recv
= n_expr
.stype
1350 var name
= n_id
.to_symbol
1351 if type_recv
isa MMTypeNone then
1352 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1355 var lc
= type_recv
.local_class
1356 if not lc
.has_global_property_by_name
(name
) then
1357 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
1360 var prop
= lc
.select_attribute
(name
)
1361 if v
.mmmodule
.visibility_for
(prop
.global
.local_class
.mmmodule
) < 3 then
1362 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.mmmodule} is invisible in {v.mmmodule}")
1365 var at
= prop
.signature_for
(type_recv
).return_type
1366 if not n_expr
.is_self
then at
= at
.not_for_self
1371 redef class AAttrExpr
1372 redef fun after_typing
(v
)
1375 if _prop
== null then return
1381 redef class AAttrAssignExpr
1382 redef fun after_typing
(v
)
1385 if _prop
== null then return
1386 if not v
.check_conform_expr
(n_value
, attr_type
) then return
1391 redef class AAttrReassignExpr
1392 redef fun after_typing
(v
)
1395 if _prop
== null then return
1396 var t
= do_rvalue_typing
(v
, attr_type
)
1397 if t
== null then return
1398 v
.check_conform
(self, t
, n_value
.stype
)
1403 redef class AIssetAttrExpr
1404 redef fun after_typing
(v
)
1407 if _prop
== null then return
1408 if attr_type
.is_nullable
then
1409 v
.error
(self, "Error: isset on a nullable attribute.")
1411 _stype
= v
.type_bool
1416 redef class AAbsAbsSendExpr
1417 # The signature of the called property
1418 redef fun prop_signature
do return _prop_signature
.as(not null)
1419 var _prop_signature
: nullable MMSignature
1421 # Raw arguments used (without vararg transformation)
1422 redef fun raw_arguments
: Array[AExpr]
1424 var res
= _raw_arguments_cache
1428 res
= compute_raw_arguments
1429 if res
== null then res
= new Array[AExpr]
1430 _raw_arguments_cache
= res
1435 var _raw_arguments_cache
: nullable Array[AExpr] = null
1437 fun compute_raw_arguments
: nullable Array[AExpr]
1439 print
"{location} no compute_raw_arguments"
1443 # Check the conformity of a set of arguments `raw_args' to a signature.
1444 private fun process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: nullable Array[AExpr]): Bool
1446 var par_vararg
= psig
.vararg_rank
1447 var par_arity
= psig
.arity
1449 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
1450 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
1451 v
.error
(self, "Error: arity mismatch; prototype is '{name}{psig}'.")
1455 for par_idx
in [0..par_arity
[ do
1457 var par_type
= psig
[par_idx
]
1458 if par_idx
== par_vararg
then
1459 for i
in [0..(raw_arity-par_arity
)] do
1460 a
= raw_args
[arg_idx
]
1461 v
.check_conform_expr
(a
, par_type
)
1462 arg_idx
= arg_idx
+ 1
1465 a
= raw_args
[arg_idx
]
1466 v
.check_conform_expr
(a
, par_type
)
1467 arg_idx
= arg_idx
+ 1
1473 # Check the conformity of a set of defined closures
1474 private fun process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: nullable Array[AClosureDef]): nullable MMType
1476 var t
= psig
.return_type
1477 var cs
= psig
.closures
# Declared closures
1480 if not c
.is_optional
then min_arity
+= 1
1483 if cd
!= null then arity
= cd
.length
1484 if cs
.length
> 0 then
1485 if arity
== 0 and min_arity
> 0 then
1486 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1487 else if arity
> cs
.length
or arity
< min_arity
then
1488 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
1490 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1491 var break_list
: nullable Array[ABreakExpr] = null
1492 if t
!= null then break_list
= new Array[ABreakExpr]
1494 # The n_label, is any in only set on the last decl
1495 var n_label
= if arity
> 0 then cd
[arity-1
].n_label
else null
1497 # Process each closure definition
1498 for i
in [0..arity
[ do
1500 var cni
= cdi
.n_id
.to_symbol
1501 var csi
= psig
.closure_named
(cni
)
1503 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1504 v
.scope_ctx
.push_escapable
(esc
, n_label
)
1505 cdi
.accept_typing2
(v
, esc
)
1507 else if cs
.length
== 1 then
1508 v
.error
(cdi
.n_id
, "Error: no closure named '!{cni}' in {name}; only closure is !{cs.first.name}.")
1510 var a
= new Array[String]
1514 v
.error
(cdi
.n_id
, "Error: no closure named '!{cni}' in {name}; only closures are {a.join(",")}.")
1518 # Check break type conformity
1519 if break_list
!= null then
1520 t
= v
.check_conform_multiexpr
(t
, break_list
)
1523 else if arity
!= 0 then
1524 v
.error
(self, "Error: {name} does not require blocks.")
1530 redef class AAbsSendExpr
1531 # Compute the called global property
1532 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])
1534 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
1535 if prop
== null then return
1536 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
1537 if not process_signature
(v
, sig
, prop
.name
, raw_args
) then return
1538 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
1539 if rtype
== null and sig
.return_type
!= null then return
1541 _prop_signature
= sig
1542 _return_type
= rtype
1545 private fun get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): nullable MMMethod
1547 if type_recv
isa MMTypeNone then
1548 if name
== (once
"==".to_symbol
) or name
== (once
"!=".to_symbol
) then
1549 # Special case on != and == that are allowed for 'null'
1550 type_recv
= v
.type_object
.as_nullable
1552 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1556 var lc
= type_recv
.local_class
1557 var prop
: nullable MMMethod = null
1558 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
1559 if prop
== null and v
.local_property
.global
.is_init
then
1560 var props
= lc
.super_methods_named
(name
)
1561 if props
.length
> 1 then
1562 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1564 else if props
.length
== 1 then
1565 var p
= lc
[props
.first
.global
]
1566 assert p
isa MMMethod
1571 if prop
== null then
1572 if is_implicit_self
then
1573 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1575 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1582 # Get the signature for a local property and a receiver
1583 private fun get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
1585 prop
.global
.check_visibility
(v
, self, v
.mmmodule
, recv_is_self
)
1586 var psig
= prop
.signature_for
(type_recv
)
1587 if not recv_is_self
then psig
= psig
.not_for_self
1591 # The invoked method (once computed)
1592 redef fun prop
do return _prop
.as(not null)
1593 var _prop
: nullable MMMethod
1595 # The return type (if any) (once computed)
1596 redef readable var _return_type
: nullable MMType
1599 # A possible call of constructor in a super class
1600 # Could be an explicit call or with the 'super' keyword
1601 redef class ASuperInitCall
1602 private fun register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1604 if parent
!= v
.top_block
and self != v
.top_block
then
1605 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1607 var cla
= v
.mmmodule
[property
.global
.intro
.local_class
.global
]
1608 var prev_class
: nullable MMLocalClass = null
1609 var esic
= v
.explicit_super_init_calls
.as(not null)
1610 if not esic
.is_empty
then
1611 prev_class
= esic
.last
.global
.intro
.local_class
1613 var order
= v
.local_class
.cshe
.reverse_linear_extension
1614 if cla
== v
.local_class
then
1615 v
.explicit_other_init_call
= true
1616 else if not order
.has
(cla
) then
1617 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1618 else if cla
== prev_class
then
1619 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1622 if c
== prev_class
then
1624 else if c
== cla
then
1634 redef class ANewExpr
1635 redef fun compute_raw_arguments
do return n_args
.to_a
1636 redef fun after_typing
(v
)
1638 if not n_type
.is_typed
then return
1639 var t
= n_type
.stype
1640 if t
.local_class
.global
.is_abstract
then
1641 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1644 if t
.is_nullable
then
1645 v
.error
(self, "Type error: cannot instantiate the nullable type {t}.")
1648 if n_id
== null then
1649 name
= once
"init".to_symbol
1651 name
= n_id
.to_symbol
1654 do_typing
(v
, t
, false, false, name
, raw_arguments
, null)
1655 if _prop
== null then return
1657 if not prop
.global
.is_init
then
1658 v
.error
(self, "Error: {prop} is not a constructor.")
1667 redef class ASendExpr
1668 # Name of the invoked property
1669 fun name
: Symbol is abstract
1671 # Closure definitions
1672 redef fun closure_defs
: nullable Array[AClosureDef] do return null
1674 redef fun after_typing
(v
)
1679 private fun do_all_typing
(v
: TypingVisitor)
1681 if not v
.check_expr
(n_expr
) then return
1682 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1683 if _prop
== null then return
1684 var prop
= _prop
.as(not null)
1686 if prop
.global
.is_init
then
1687 if not v
.local_property
.global
.is_init
then
1688 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1689 else if not n_expr
.is_self
then
1690 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1692 register_super_init_call
(v
, prop
)
1696 _stype
= return_type
1701 redef class ASendReassignExpr
1702 redef fun read_prop
do return _read_prop
.as(not null)
1703 var _read_prop
: nullable MMMethod
1704 redef fun do_all_typing
(v
)
1706 if not v
.check_expr
(n_expr
) then return
1707 var raw_args
= raw_arguments
1708 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1710 if prop
== null then return
1711 if prop
.global
.is_init
then
1712 if not v
.local_property
.global
.is_init
then
1713 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1714 else if not n_expr
.is_self
then
1715 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1718 var t
= prop
.signature_for
(n_expr
.stype
).return_type
.as(not null)
1719 if not n_expr
.is_self
then t
= t
.not_for_self
1721 var t2
= do_rvalue_typing
(v
, t
)
1722 if t2
== null then return
1723 v
.check_conform
(self, t2
, n_value
.stype
)
1726 raw_args
= raw_args
.to_a
1727 raw_args
.add
(n_value
)
1729 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1730 if prop
.global
.is_init
then
1731 if not v
.local_property
.global
.is_init
then
1732 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1733 else if not n_expr
.is_self
then
1734 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1742 redef class ABinopExpr
1743 redef fun compute_raw_arguments
do return [n_expr2
]
1746 redef fun name
do return once
"==".to_symbol
1747 redef fun after_typing
(v
)
1750 if not n_expr
.is_typed
or not n_expr2
.is_typed
then return
1751 if n_expr
.stype
isa MMTypeNone and not n_expr2
.stype
.is_nullable
or
1752 n_expr2
.stype
isa MMTypeNone and not n_expr
.stype
.is_nullable
then
1753 v
.warning
(self, "Warning: comparaison between null and a non nullable value.")
1756 if n_expr
.stype
isa MMTypeNone then
1757 if n_expr2
.stype
isa MMTypeNone then
1758 v
.warning
(self, "Warning: comparaison between two null values.")
1760 try_to_isa
(v
, n_expr2
)
1762 else if n_expr2
.stype
isa MMTypeNone then
1763 try_to_isa
(v
, n_expr
)
1767 private fun try_to_isa
(v
: TypingVisitor, n
: AExpr)
1769 var variable
= n
.its_variable
1770 if variable
!= null and n
.stype
isa MMNullableType then
1771 _if_false_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, n
.stype
.as_notnull
)
1772 _if_true_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, v
.type_none
)
1777 redef fun name
do return once
"!=".to_symbol
1778 redef fun after_typing
(v
)
1781 if not n_expr
.is_typed
or not n_expr2
.is_typed
then return
1782 if n_expr
.stype
isa MMTypeNone and not n_expr2
.stype
.is_nullable
or
1783 n_expr2
.stype
isa MMTypeNone and not n_expr
.stype
.is_nullable
then
1784 v
.warning
(self, "Warning: comparaison between null and a non nullable value.")
1787 if n_expr
.stype
isa MMTypeNone then
1788 if n_expr2
.stype
isa MMTypeNone then
1789 v
.warning
(self, "Warning: comparaison between two null values.")
1791 try_to_isa
(v
, n_expr2
)
1793 else if n_expr2
.stype
isa MMTypeNone then
1794 try_to_isa
(v
, n_expr
)
1798 private fun try_to_isa
(v
: TypingVisitor, n
: AExpr)
1800 var variable
= n
.its_variable
1801 if variable
!= null and n
.stype
isa MMNullableType then
1802 _if_true_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, n
.stype
.as_notnull
)
1803 _if_false_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, v
.type_none
)
1808 redef fun name
do return once
"<".to_symbol
1811 redef fun name
do return once
"<=".to_symbol
1814 redef fun name
do return once
"<<".to_symbol
1817 redef fun name
do return once
">".to_symbol
1820 redef fun name
do return once
">=".to_symbol
1823 redef fun name
do return once
">>".to_symbol
1825 redef class APlusExpr
1826 redef fun name
do return once
"+".to_symbol
1828 redef class AMinusExpr
1829 redef fun name
do return once
"-".to_symbol
1831 redef class AStarshipExpr
1832 redef fun name
do return once
"<=>".to_symbol
1834 redef class AStarExpr
1835 redef fun name
do return once
"*".to_symbol
1837 redef class ASlashExpr
1838 redef fun name
do return once
"/".to_symbol
1840 redef class APercentExpr
1841 redef fun name
do return once
"%".to_symbol
1844 redef class AUminusExpr
1845 redef fun name
do return once
"unary -".to_symbol
1846 redef fun compute_raw_arguments
do return null
1849 redef class ACallFormExpr
1850 redef fun after_typing
(v
)
1852 if n_expr
.is_implicit_self
then
1853 var name
= n_id
.to_symbol
1854 var variable
= v
.scope_ctx
[name
]
1855 if variable
!= null then
1857 if variable
isa ClosureVariable then
1858 n
= new AClosureCallExpr.init_aclosurecallexpr
(n_id
, n_args
, n_closure_defs
)
1859 n
._variable
= variable
1861 if not n_args
.n_exprs
.is_empty
or n_args
isa AParExprs then
1862 v
.error
(self, "Error: {name} is variable, not a function.")
1865 n
= variable_create
(variable
)
1866 n
._variable
= variable
1877 redef fun closure_defs
1879 if n_closure_defs
.is_empty
then
1882 return n_closure_defs
.to_a
1886 # Create a variable acces corresponding to the call form
1887 fun variable_create
(variable
: Variable): AVarFormExpr is abstract
1890 redef class ACallExpr
1891 redef fun variable_create
(variable
)
1893 return new AVarExpr.init_avarexpr
(n_id
)
1896 redef fun name
do return n_id
.to_symbol
1897 redef fun compute_raw_arguments
do return n_args
.to_a
1900 redef class ACallAssignExpr
1901 redef fun variable_create
(variable
)
1903 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1906 redef fun name
do return (n_id
.text
+ "=").to_symbol
1907 redef fun compute_raw_arguments
do
1908 var res
= n_args
.to_a
1914 redef class ACallReassignExpr
1915 redef fun variable_create
(variable
)
1917 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1920 redef fun name
do return n_id
.to_symbol
1921 redef fun compute_raw_arguments
do return n_args
.to_a
1924 redef class ABraExpr
1925 redef fun name
do return once
"[]".to_symbol
1926 redef fun compute_raw_arguments
do return n_args
.to_a
1927 redef fun closure_defs
1929 if n_closure_defs
.is_empty
then
1932 return n_closure_defs
.to_a
1937 redef class ABraAssignExpr
1938 redef fun name
do return once
"[]=".to_symbol
1939 redef fun compute_raw_arguments
do
1940 var res
= n_args
.to_a
1946 redef class ABraReassignExpr
1947 redef fun name
do return once
"[]".to_symbol
1948 redef fun compute_raw_arguments
do return n_args
.to_a
1951 redef class AInitExpr
1952 redef fun name
do return once
"init".to_symbol
1953 redef fun compute_raw_arguments
do return n_args
.to_a
1956 redef class AClosureCallExpr
1957 var _variable
: nullable ClosureVariable
1958 redef fun variable
do return _variable
.as(not null)
1959 redef fun compute_raw_arguments
do return n_args
.to_a
1961 redef fun after_typing
(v
)
1964 if va
.closure
.is_break
then v
.mark_unreash
(self)
1965 var sig
= va
.closure
.signature
1966 var s
= process_signature
(v
, sig
, n_id
.to_symbol
, compute_raw_arguments
)
1967 if not n_closure_defs
.is_empty
then
1968 process_closures
(v
, sig
, n_id
.to_symbol
, n_closure_defs
.to_a
)
1970 if not s
then return
1971 _prop_signature
= sig
1972 _stype
= sig
.return_type
1977 redef class AClosureId
1978 fun to_symbol
: Symbol is abstract
1980 redef class ASimpleClosureId
1981 redef fun to_symbol
: Symbol do return n_id
.to_symbol
1983 redef class ABreakClosureId
1984 redef fun to_symbol
: Symbol do return n_kwbreak
.to_symbol
1987 redef class AClosureDef
1988 var _closure
: nullable MMClosure
1989 redef fun closure
do return _closure
.as(not null)
1991 # The corresponding escapable object
1992 readable var _escapable
: nullable EscapableBlock
1994 var _accept_typing2
: Bool = false
1995 redef fun accept_typing
(v
)
1997 # Typing is deferred, wait accept_typing2(v)
1998 if _accept_typing2
then super
2001 private fun accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure)
2005 var sig
= esc
.closure
.signature
2006 if sig
.arity
!= n_ids
.length
then
2007 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_ids.length} found.")
2011 _closure
= esc
.closure
2013 v
.scope_ctx
.push
(self)
2014 var old_flow_ctx
= v
.flow_ctx
2015 var old_base_flow_ctx
= v
.base_flow_ctx
2016 v
.base_flow_ctx
= v
.flow_ctx
2017 variables
= new Array[AutoVariable]
2018 for i
in [0..n_ids
.length
[ do
2019 var va
= new AutoVariable(n_ids
[i
].to_symbol
, n_ids
[i
])
2022 v
.scope_ctx
.add_variable
(va
)
2025 _accept_typing2
= true
2028 if v
.flow_ctx
.unreash
== false then
2029 if closure
.signature
.return_type
!= null then
2030 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
2031 else if closure
.is_break
and esc
.break_list
!= null then
2032 v
.error
(self, "Control error: Reached end of break block (a 'break' with a value was expected).")
2035 v
.flow_ctx
= old_flow_ctx
2036 v
.base_flow_ctx
= old_base_flow_ctx
2041 abstract class ATypeCheckExpr
2043 private fun check_expr_cast
(v
: TypingVisitor, n_expr
: AExpr, n_type
: AType)
2045 if not v
.check_expr
(n_expr
) then return
2046 if not n_type
.is_typed
then return
2047 var etype
= n_expr
.stype
2048 var ttype
= n_type
.stype
2049 if etype
== ttype
then
2050 v
.warning
(self, "Warning: Expression is already a {ttype}.")
2051 else if etype
< ttype
then
2052 if not ttype
isa MMTypeFormal then
2053 # the old metamodel is not that great with formal types
2054 v
.warning
(self, "Warning: Expression is already a {ttype} since it is a {etype}.")
2056 else if etype
isa MMTypeNone then
2057 # ttype is not nullable because of prevous test
2058 v
.warning
(self, "Warning: Expression is null therefore cannot be a {ttype}.")
2059 else if etype
.is_nullable
and etype
.as_notnull
== ttype
then
2060 if ttype
isa MMTypeFormal and ttype
.bound
.is_nullable
then
2061 # No warning in this case since with
2062 # type T: nullable A
2064 # 'x.as(not null)' != 'x.as(T)'
2065 # 'x != null' != 'x isa T'
2066 else if self isa AIsaExpr then
2067 v
.warning
(self, "Warning: Prefer '!= null'.")
2069 v
.warning
(self, "Warning: Prefer '.as(not null)'.")
2075 redef class AIsaExpr
2076 super ATypeCheckExpr
2077 redef fun after_typing
(v
)
2079 check_expr_cast
(v
, n_expr
, n_type
)
2080 if not n_type
.is_typed
then return
2081 var variable
= n_expr
.its_variable
2082 if variable
!= null then
2083 _if_true_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, n_type
.stype
)
2085 _stype
= v
.type_bool
2090 redef class AAsCastExpr
2091 super ATypeCheckExpr
2092 redef fun after_typing
(v
)
2094 check_expr_cast
(v
, n_expr
, n_type
)
2095 if not n_type
.is_typed
then return
2096 _stype
= n_type
.stype
2097 _is_typed
= _stype
!= null
2101 redef class AAsNotnullExpr
2102 redef fun after_typing
(v
)
2104 if not v
.check_expr
(n_expr
) then return
2105 var t
= n_expr
.stype
2106 if t
isa MMTypeNone then
2107 v
.error
(n_expr
, "Type error: 'as(not null)' on 'null' value.")
2109 else if not t
.is_nullable
then
2110 v
.warning
(n_expr
, "Warning: 'as(not null)' on non nullable type.")
2112 _stype
= n_expr
.stype
.as_notnull
2117 redef class AProxyExpr
2118 redef fun after_typing
(v
)
2120 if not n_expr
.is_typed
then return
2122 if n_expr
.is_statement
then return
2123 _stype
= n_expr
.stype
2124 _if_true_flow_ctx
= n_expr
._if_true_flow_ctx
2125 _if_false_flow_ctx
= n_expr
._if_false_flow_ctx
2128 redef fun is_self
do return n_expr
.is_self
2130 redef fun its_variable
do return n_expr
.its_variable
2133 redef class AOnceExpr
2134 redef fun accept_typing
(v
)
2136 if v
.once_count
> 0 then
2137 v
.warning
(self, "Useless once in a once expression.")
2139 v
.once_count
= v
.once_count
+ 1
2143 v
.once_count
= v
.once_count
- 1
2147 redef class ADebugTypeExpr
2148 redef fun after_typing
(v
)
2150 if not v
.check_expr
(n_expr
) then return
2151 if not n_type
.is_typed
then return
2152 var etype
= n_expr
.stype
2153 var ttype
= n_type
.stype
2154 if etype
!= ttype
then
2155 v
.warning
(self, "Warning: Expression is a {etype}, expected {ttype}.")