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_mixin
then
257 else if cur_c
!= null and (c
.cshe
<= cur_c
or cur_c
.global
.is_mixin
) then
258 if c
== cur_c
then j
+= 1
259 super_init_calls
.add
(cur_m
.as(not null))
262 cur_m
= explicit_super_init_calls
[i
]
263 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.mmmodule
)
269 var p
= v
.get_default_constructor_for
(self, c
, method
)
271 super_init_calls
.add
(p
)
281 redef fun after_typing
(v
)
283 v
.scope_ctx
.add_variable
(variable
)
287 redef class AClosureDecl
288 # The corresponding escapable object
289 readable var _escapable
: nullable EscapableBlock
291 redef fun accept_typing
(v
)
293 # Register the closure for ClosureCallExpr
294 v
.scope_ctx
.add_variable
(variable
)
296 var old_flow_ctx
= v
.flow_ctx
297 var old_base_flow_ctx
= v
.base_flow_ctx
298 v
.base_flow_ctx
= v
.flow_ctx
300 var blist
: nullable Array[AExpr] = null
301 var t
= v
.local_property
.signature
.return_type
302 if t
!= null then blist
= new Array[AExpr]
303 var escapable
= new EscapableClosure(self, variable
.closure
, blist
)
304 _escapable
= escapable
305 v
.scope_ctx
.push_escapable
(escapable
, null)
307 v
.is_default_closure_definition
= true
311 v
.is_default_closure_definition
= false
313 if n_expr
!= null then
314 if v
.flow_ctx
.unreash
== false then
315 if variable
.closure
.signature
.return_type
!= null then
316 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
317 else if variable
.closure
.is_break
and escapable
.break_list
!= null then
318 v
.error
(self, "Control error: Reached end of break block (a 'break' with a value was expected).")
322 if blist
!= null then for x
in blist
do
323 v
.check_conform_expr
(x
, t
)
326 v
.flow_ctx
= old_flow_ctx
327 v
.base_flow_ctx
= old_base_flow_ctx
333 redef fun stype
: MMType do return _stype
.as(not null)
334 redef fun is_typed
: Bool do return _stype
!= null
335 var _stype
: nullable MMType
337 redef fun after_typing
(v
)
339 _stype
= get_stype
(v
)
344 redef readable var _is_typed
: Bool = false
345 redef fun is_statement
: Bool do return _stype
== null
349 print
"{location}: not is_typed"
353 print
"{location}: is_statement"
356 return _stype
.as(not null)
358 var _stype
: nullable MMType
360 redef fun after_typing
(v
)
362 # Default behavior is to be happy
366 # Is the expression the implicit receiver
367 fun is_implicit_self
: Bool do return false
369 # Is the expression the current receiver (implicit or explicit)
370 fun is_self
: Bool do return false
372 # The variable accessed is any
373 fun its_variable
: nullable Variable do return null
375 # The control flow information if current boolean expression is true
376 readable private var _if_true_flow_ctx
: nullable FlowContext
378 # The control flow information if current boolean expression is false
379 readable private var _if_false_flow_ctx
: nullable FlowContext
382 redef class AVardeclExpr
383 var _variable
: nullable VarVariable
384 redef fun variable
do return _variable
.as(not null)
386 redef fun after_typing
(v
)
388 var va
= new VarVariable(n_id
.to_symbol
, n_id
)
390 v
.scope_ctx
.add_variable
(va
)
392 if ne
!= null then v
.mark_is_set
(va
)
394 if n_type
!= null then
395 if not n_type
.is_typed
then return
396 va
.stype
= n_type
.stype
398 v
.check_conform_expr
(ne
, va
.stype
)
400 else if ne
!= null then
401 if not v
.check_expr
(ne
) then return
404 va
.stype
= v
.type_object
.as_nullable
410 redef class ABlockExpr
411 redef fun accept_typing
(v
)
414 if not v
.flow_ctx
.unreash
then
416 else if not v
.flow_ctx
.already_unreash
then
417 v
.flow_ctx
.already_unreash
= true
418 v
.error
(e
, "Error: unreachable statement.")
426 redef class AReturnExpr
427 redef fun after_typing
(v
)
430 var t
= v
.local_property
.signature
.return_type
432 if v
.is_default_closure_definition
then
433 v
.error
(self, "Error: 'return' invalid in default closure definitions. Use 'continue' or 'break'.")
438 if e
== null and t
!= null then
439 v
.error
(self, "Error: Return without value in a function.")
440 else if e
!= null and t
== null then
441 v
.error
(self, "Error: Return with value in a procedure.")
442 else if e
!= null and t
!= null then
443 v
.check_conform_expr
(e
, t
)
449 redef class AContinueExpr
450 redef fun after_typing
(v
)
453 var esc
= compute_escapable_block
(v
.scope_ctx
)
454 if esc
== null then return
456 if esc
.is_break_block
then
457 v
.error
(self, "Error: cannot 'continue', only 'break'.")
461 var t
= esc
.continue_stype
462 if n_expr
== null and t
!= null then
463 v
.error
(self, "Error: continue with a value required in this block.")
464 else if n_expr
!= null and t
== null then
465 v
.error
(self, "Error: continue without value required in this block.")
466 else if n_expr
!= null and t
!= null then
467 v
.check_conform_expr
(n_expr
.as(not null), t
)
473 redef class ABreakExpr
474 redef fun after_typing
(v
)
476 var old_flow_ctx
= v
.flow_ctx
478 var esc
= compute_escapable_block
(v
.scope_ctx
)
479 if esc
== null then return
481 esc
.break_flow_contexts
.add
(old_flow_ctx
)
483 var bl
= esc
.break_list
484 if n_expr
== null and bl
!= null then
485 v
.error
(self, "Error: break with a value required in this block.")
486 else if n_expr
!= null and bl
== null then
487 v
.error
(self, "Error: break without value required in this block.")
488 else if n_expr
!= null and bl
!= null then
489 # Typing check can only be done later
490 bl
.add
(n_expr
.as(not null))
496 redef class AAbortExpr
497 redef fun after_typing
(v
)
504 # An abstract control structure with feature escapable block
507 # The corresponding escapable block
508 readable var _escapable
: nullable EscapableBlock
510 # Enter and process a control structure
511 private fun process_control
(v
: TypingVisitor, escapable
: EscapableBlock, n_label
: nullable ALabel, is_loop
: Bool)
513 # Register the escapable block
514 _escapable
= escapable
515 v
.scope_ctx
.push_escapable
(escapable
, n_label
)
517 # Save an prepare the contextes
518 var old_flow_ctx
= v
.flow_ctx
519 var old_base_flow_ctx
= v
.base_flow_ctx
520 if is_loop
then v
.base_flow_ctx
= v
.flow_ctx
522 # Do the main processing
523 process_control_inside
(v
)
525 # Add the end of the block as an exit context
526 if not v
.flow_ctx
.unreash
then
527 escapable
.break_flow_contexts
.add
(v
.flow_ctx
)
530 # Merge all exit contexts
531 if escapable
.break_flow_contexts
.is_empty
then
532 v
.flow_ctx
= old_flow_ctx
535 v
.flow_ctx
= old_base_flow_ctx
.merge
(self, escapable
.break_flow_contexts
)
538 if is_loop
then v
.base_flow_ctx
= old_base_flow_ctx
543 # What to do inside the control block?
544 private fun process_control_inside
(v
: TypingVisitor) is abstract
549 redef fun accept_typing
(v
)
551 process_control
(v
, new BreakOnlyEscapableBlock(self), n_label
, false)
554 redef fun process_control_inside
(v
)
556 v
.enter_visit_block
(n_block
)
561 redef fun accept_typing
(v
)
563 v
.enter_visit
(n_expr
)
564 v
.check_conform_expr
(n_expr
, v
.type_bool
)
566 # Prepare 'then' context
567 var old_flow_ctx
= v
.flow_ctx
568 v
.use_if_true_flow_ctx
(n_expr
)
571 v
.enter_visit_block
(n_then
)
573 # Remember what appened in the 'then'
574 var then_flow_ctx
= v
.flow_ctx
576 # Prepare 'else' context
577 v
.flow_ctx
= old_flow_ctx
578 v
.use_if_false_flow_ctx
(n_expr
)
581 v
.enter_visit_block
(n_else
)
583 # Merge 'then' and 'else' contexts
584 v
.flow_ctx
= v
.base_flow_ctx
.merge_reash
(self, then_flow_ctx
, v
.flow_ctx
)
589 redef class AWhileExpr
591 redef fun accept_typing
(v
)
593 process_control
(v
, new EscapableBlock(self), n_label
, true)
596 redef fun process_control_inside
(v
)
598 var old_flow_ctx
= v
.flow_ctx
601 v
.enter_visit
(n_expr
)
602 v
.check_conform_expr
(n_expr
, v
.type_bool
)
604 if n_expr
isa ATrueExpr then
605 v
.warning
(self, "Warning: use 'loop' instead of 'while true do'.")
608 # Prepare inside context (assert cond)
609 v
.use_if_true_flow_ctx
(n_expr
)
612 v
.enter_visit_block
(n_block
)
614 # Compute outside context (assert !cond + all breaks)
615 v
.flow_ctx
= old_flow_ctx
616 v
.use_if_false_flow_ctx
(n_expr
)
617 escapable
.break_flow_contexts
.add
(v
.flow_ctx
)
621 redef class ALoopExpr
623 redef fun accept_typing
(v
)
625 process_control
(v
, new EscapableBlock(self), n_label
, true)
628 redef fun process_control_inside
(v
)
631 v
.enter_visit_block
(n_block
)
633 # Never automatically reach after the loop
640 var _variables
: nullable Array[AutoVariable]
641 redef fun variables
do return _variables
.as(not null)
643 redef fun accept_typing
(v
)
645 process_control
(v
, new EscapableBlock(self), n_label
, true)
648 redef fun process_control_inside
(v
)
650 v
.scope_ctx
.push
(self)
651 var old_flow_ctx
= v
.flow_ctx
656 v
.enter_visit_block
(n_block
)
658 # end == begin of the loop
659 v
.flow_ctx
= old_flow_ctx
663 private fun do_typing
(v
: TypingVisitor)
665 # Create the automatic variables
666 var vas
= new Array[AutoVariable]
668 var va
= new AutoVariable(n_id
.to_symbol
, n_id
)
669 v
.scope_ctx
.add_variable
(va
)
675 v
.enter_visit
(n_expr
)
676 if not v
.check_expr
(n_expr
) then return
677 var expr_type
= n_expr
.stype
679 if expr_type
.is_nullable
then
680 v
.error
(n_expr
, "Type error: 'for' on a nullable expression.")
685 var iterate_name
= once
"iterate".to_symbol
686 if not expr_type
.local_class
.has_global_property_by_name
(iterate_name
) then
687 v
.error
(n_expr
, "Type error: Expected a type with an 'iterate' method. Found {expr_type}.")
690 var prop
= expr_type
.local_class
.select_method
(iterate_name
)
691 prop
.global
.check_visibility
(v
, self, v
.mmmodule
, n_expr
.is_self
)
692 var psig
= prop
.signature_for
(expr_type
)
693 if not n_expr
.is_self
then psig
= psig
.not_for_self
694 if psig
.arity
!= 0 then
695 v
.error
(self, "Error: 'iterate' incompatible with 'for': require no arguments.")
697 else if psig
.closures
.length
!= 1 then
698 v
.error
(self, "Error: 'iterate' incompatible with 'for': require one closure.")
701 psig
= psig
.closures
.first
.signature
702 if psig
.return_type
!= null then
703 v
.error
(self, "Error: 'iterate' incompatible with 'for': require one procedural closure.")
706 if vas
.length
!= psig
.arity
then
707 if psig
.arity
== 1 then
708 v
.error
(self, "Error: Expected {psig.arity} variable {psig}, found {vas.length}.")
710 v
.error
(self, "Error: Expected {psig.arity} variables {psig}, found {vas.length}.")
715 # Type the automatic variables
716 for i
in [0..vas
.length
[ do
717 vas
[i
].stype
= psig
[i
]
722 redef class AAssertExpr
723 redef fun accept_typing
(v
)
726 v
.enter_visit
(n_expr
)
727 v
.check_conform_expr
(n_expr
, v
.type_bool
)
729 # Process optional 'else' part
730 if n_else
!= null then
731 var old_flow_ctx
= v
.flow_ctx
732 v
.use_if_false_flow_ctx
(n_expr
)
733 v
.enter_visit
(n_else
)
734 v
.flow_ctx
= old_flow_ctx
738 v
.use_if_true_flow_ctx
(n_expr
)
743 redef class AVarFormExpr
744 var _variable
: nullable Variable
745 redef fun variable
do return _variable
.as(not null)
749 redef fun its_variable
do return variable
751 redef fun after_typing
(v
)
753 v
.flow_ctx
.check_is_set
(self, variable
)
754 _stype
= v
.flow_ctx
.stype
(variable
)
755 _is_typed
= _stype
!= null
759 redef class AVarAssignExpr
760 redef fun after_typing
(v
)
762 v
.mark_is_set
(variable
)
764 # Check the base type
765 var btype
= v
.base_flow_ctx
.stype
(variable
)
766 if not v
.check_expr
(n_value
) then return
767 if btype
!= null and not v
.check_conform_expr
(n_value
, btype
) then return
770 v
.flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, n_value
.stype
)
776 redef class AReassignFormExpr
777 # Compute and check method used through the reassigment operator
778 # On success return the static type of the result of the reassigment operator
779 # Else display an error and return null
780 private fun do_rvalue_typing
(v
: TypingVisitor, type_lvalue
: nullable MMType): nullable MMType
782 if type_lvalue
== null then
785 var name
= n_assign_op
.method_name
786 if type_lvalue
isa MMTypeNone then
787 v
.error
(self, "Error: Method '{name}' call on 'null'.")
790 var lc
= type_lvalue
.local_class
791 if not lc
.has_global_property_by_name
(name
) then
792 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
795 var prop
= lc
.select_method
(name
)
796 prop
.global
.check_visibility
(v
, self, v
.mmmodule
, false)
797 var psig
= prop
.signature_for
(type_lvalue
)
798 _assign_method
= prop
799 if not v
.check_conform_expr
(n_value
, psig
[0].not_for_self
) then return null
800 return psig
.return_type
.not_for_self
803 redef fun assign_method
do return _assign_method
.as(not null)
804 var _assign_method
: nullable MMMethod
807 redef class AVarReassignExpr
808 redef fun after_typing
(v
)
810 v
.flow_ctx
.check_is_set
(self, variable
)
811 v
.mark_is_set
(variable
)
812 var t
= v
.flow_ctx
.stype
(variable
)
813 var t2
= do_rvalue_typing
(v
, t
)
814 if t2
== null then return
816 # Check the base type
817 var btype
= v
.base_flow_ctx
.stype
(variable
)
818 if not v
.check_expr
(n_value
) then return
819 if btype
!= null and not v
.check_conform
(n_value
, t2
, btype
) then return
822 v
.flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, t2
)
828 redef class AAssignOp
829 fun method_name
: Symbol is abstract
831 redef class APlusAssignOp
832 redef fun method_name
do return once
"+".to_symbol
834 redef class AMinusAssignOp
835 redef fun method_name
do return once
"-".to_symbol
838 redef class ASelfExpr
839 var _variable
: nullable ParamVariable
840 redef fun variable
do return _variable
.as(not null)
842 redef fun its_variable
do return variable
844 redef fun after_typing
(v
)
846 _variable
= v
.self_var
847 _stype
= v
.flow_ctx
.stype
(variable
)
851 redef fun is_self
do return true
854 redef class AImplicitSelfExpr
855 redef fun is_implicit_self
do return true
858 redef class AIfexprExpr
859 redef fun accept_typing
(v
)
861 var old_flow_ctx
= v
.flow_ctx
864 v
.enter_visit
(n_expr
)
865 v
.check_conform_expr
(n_expr
, v
.type_bool
)
867 # Prepare 'then' context
868 v
.use_if_true_flow_ctx
(n_expr
)
871 v
.enter_visit_block
(n_then
)
873 # Remember what appened in the 'then'
874 var then_flow_ctx
= v
.flow_ctx
876 # Prepare 'else' context
877 v
.flow_ctx
= old_flow_ctx
878 v
.use_if_false_flow_ctx
(n_expr
)
881 v
.enter_visit_block
(n_else
)
883 # Merge 'then' and 'else' contexts
884 v
.flow_ctx
= v
.base_flow_ctx
.merge_reash
(self, then_flow_ctx
, v
.flow_ctx
)
886 var stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
887 if stype
== null then return
894 redef class ABoolExpr
895 redef fun after_typing
(v
)
903 redef fun accept_typing
(v
)
905 var old_flow_ctx
= v
.flow_ctx
906 var stype
= v
.type_bool
909 # Process left operand
910 v
.enter_visit
(n_expr
)
912 # Prepare right operand context
913 v
.use_if_false_flow_ctx
(n_expr
)
915 # Process right operand
916 v
.enter_visit
(n_expr2
)
917 if n_expr2
.if_false_flow_ctx
!= null then
918 _if_false_flow_ctx
= n_expr2
.if_false_flow_ctx
920 _if_false_flow_ctx
= v
.flow_ctx
923 v
.flow_ctx
= old_flow_ctx
925 v
.check_conform_expr
(n_expr
, stype
)
926 v
.check_conform_expr
(n_expr2
, stype
)
933 redef fun accept_typing
(v
)
935 var old_flow_ctx
= v
.flow_ctx
936 var stype
= v
.type_bool
938 # Process left operand
939 v
.enter_visit
(n_expr
)
941 # Prepare right operand context
942 v
.use_if_true_flow_ctx
(n_expr
)
944 # Process right operand
945 v
.enter_visit
(n_expr2
)
946 if n_expr2
.if_true_flow_ctx
!= null then
947 _if_true_flow_ctx
= n_expr2
.if_true_flow_ctx
949 _if_true_flow_ctx
= v
.flow_ctx
952 v
.flow_ctx
= old_flow_ctx
954 v
.check_conform_expr
(n_expr
, stype
)
955 v
.check_conform_expr
(n_expr2
, stype
)
962 redef fun after_typing
(v
)
964 v
.check_conform_expr
(n_expr
, v
.type_bool
)
966 # Invert if_true/if_false information
967 _if_false_flow_ctx
= n_expr
._if_true_flow_ctx
968 _if_true_flow_ctx
= n_expr
._if_false_flow_ctx
975 redef class AOrElseExpr
976 redef fun after_typing
(v
)
978 var old_flow_ctx
= v
.flow_ctx
980 # Process left operand
981 v
.enter_visit
(n_expr
)
984 # Consider the type of the left operand
986 if not t
.is_nullable
then
987 v
.warning
(n_expr
, "Warning: left operant of a 'or else' is not a nullable type.")
992 # Prepare the else context : ie the first expression is null
993 var variable
= n_expr
.its_variable
994 if variable
!= null then
995 v
.flow_ctx
.sub_with
(self, variable
, v
.type_none
)
998 # Process right operand
999 v
.enter_visit
(n_expr2
)
1000 v
.check_expr
(n_expr
)
1002 # Restore the context
1003 v
.flow_ctx
= old_flow_ctx
1006 var stype
= v
.check_conform_multiexpr
(t
, [n_expr2
])
1007 if stype
== null then return
1014 redef class AIntExpr
1015 redef fun after_typing
(v
)
1022 redef class AFloatExpr
1023 redef fun after_typing
(v
)
1025 _stype
= v
.type_float
1030 redef class ACharExpr
1031 redef fun after_typing
(v
)
1033 _stype
= v
.type_char
1038 redef class AStringFormExpr
1039 redef fun after_typing
(v
)
1041 _stype
= v
.type_string
1046 redef class ASuperstringExpr
1047 redef fun atype
do return _atype
.as(not null)
1048 var _atype
: nullable MMType
1049 redef fun after_typing
(v
)
1051 var otype
= v
.type_object
1052 var stype
= v
.type_string
1054 for e
in n_exprs
do v
.check_conform_expr
(e
, otype
)
1055 var atype
= v
.type_array
(stype
)
1061 redef class ANullExpr
1062 redef fun after_typing
(v
)
1064 _stype
= v
.type_none
1069 redef class AArrayExpr
1070 redef fun after_typing
(v
)
1072 var stype
= v
.check_conform_multiexpr
(null, n_exprs
)
1073 if stype
!= null then do_typing
(v
, stype
)
1076 private fun do_typing
(v
: TypingVisitor, element_type
: MMType)
1078 _stype
= v
.type_array
(element_type
)
1083 redef class ARangeExpr
1084 redef fun after_typing
(v
)
1086 if not v
.check_expr
(n_expr
) or not v
.check_expr
(n_expr2
) then return
1087 var ntype
= n_expr
.stype
1088 var ntype2
= n_expr2
.stype
1089 if ntype
< ntype2
then
1091 else if not ntype2
< ntype
then
1092 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
1095 var dtype
= v
.type_discrete
1096 if not v
.check_conform_expr
(n_expr
, dtype
) or not v
.check_conform_expr
(n_expr2
, dtype
) then return
1097 _stype
= v
.type_range
(ntype
)
1102 redef class ASuperExpr
1103 redef readable var _init_in_superclass
: nullable MMMethod
1104 redef fun compute_raw_arguments
do return n_args
.to_a
1105 redef fun after_typing
(v
)
1107 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
1108 if not precs
.is_empty
then
1109 v
.local_property
.need_super
= true
1110 else if v
.local_property
.global
.is_init
then
1111 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
1112 for p
in base_precs
do
1113 if not p
.global
.is_init
then
1114 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
1116 precs
.add
(v
.local_class
[p
.global
])
1119 if precs
.is_empty
then
1120 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
1122 else if precs
.length
> 1 then
1123 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
1126 var p
= base_precs
.first
1127 assert p
isa MMMethod
1128 _init_in_superclass
= p
1129 register_super_init_call
(v
, p
)
1130 if n_args
.length
> 0 then
1131 var signature
= get_signature
(v
, v
.self_var
.stype
.as(not null), p
, true)
1132 process_signature
(v
, signature
, p
.name
, compute_raw_arguments
)
1135 v
.error
(self, "Error: No super method to call for {v.local_property}.")
1139 if precs
.first
.signature_for
(v
.self_var
.stype
.as(not null)).return_type
!= null then
1140 var stypes
= new Array[MMType]
1141 var stype
: nullable MMType = null
1142 for prop
in precs
do
1143 assert prop
isa MMMethod
1144 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
)
1146 if stype
== null or stype
< t
then
1151 v
.check_conform
(self, t
, stype
.as(not null))
1155 var p
= v
.local_property
1156 assert p
isa MMSrcMethod
1162 redef class AAttrFormExpr
1163 redef fun prop
do return _prop
.as(not null)
1164 var _prop
: nullable MMAttribute
1166 redef fun attr_type
do return _attr_type
.as(not null)
1167 var _attr_type
: nullable MMType
1169 # Compute the attribute accessed
1170 private fun do_typing
(v
: TypingVisitor)
1172 if not v
.check_expr
(n_expr
) then return
1173 var type_recv
= n_expr
.stype
1174 var name
= n_id
.to_symbol
1175 if type_recv
isa MMTypeNone then
1176 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1179 var lc
= type_recv
.local_class
1180 if not lc
.has_global_property_by_name
(name
) then
1181 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
1184 var prop
= lc
.select_attribute
(name
)
1185 if v
.mmmodule
.visibility_for
(prop
.global
.local_class
.mmmodule
) < 3 then
1186 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.mmmodule} is invisible in {v.mmmodule}")
1189 var at
= prop
.signature_for
(type_recv
).return_type
1190 if not n_expr
.is_self
then at
= at
.not_for_self
1195 redef class AAttrExpr
1196 redef fun after_typing
(v
)
1199 if _prop
== null then return
1205 redef class AAttrAssignExpr
1206 redef fun after_typing
(v
)
1209 if _prop
== null then return
1210 if not v
.check_conform_expr
(n_value
, attr_type
) then return
1215 redef class AAttrReassignExpr
1216 redef fun after_typing
(v
)
1219 if _prop
== null then return
1220 var t
= do_rvalue_typing
(v
, attr_type
)
1221 if t
== null then return
1222 v
.check_conform
(self, t
, n_value
.stype
)
1227 redef class AIssetAttrExpr
1228 redef fun after_typing
(v
)
1231 if _prop
== null then return
1232 if attr_type
.is_nullable
then
1233 v
.error
(self, "Error: isset on a nullable attribute.")
1235 _stype
= v
.type_bool
1240 redef class AAbsAbsSendExpr
1241 # The signature of the called property
1242 redef fun prop_signature
do return _prop_signature
.as(not null)
1243 var _prop_signature
: nullable MMSignature
1245 # Raw arguments used (without vararg transformation)
1246 redef fun raw_arguments
: Array[AExpr]
1248 var res
= _raw_arguments_cache
1252 res
= compute_raw_arguments
1253 if res
== null then res
= new Array[AExpr]
1254 _raw_arguments_cache
= res
1259 var _raw_arguments_cache
: nullable Array[AExpr] = null
1261 fun compute_raw_arguments
: nullable Array[AExpr]
1263 print
"{location} no compute_raw_arguments"
1267 # Check the conformity of a set of arguments `raw_args' to a signature.
1268 private fun process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: nullable Array[AExpr]): Bool
1270 var par_vararg
= psig
.vararg_rank
1271 var par_arity
= psig
.arity
1273 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
1274 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
1275 v
.error
(self, "Error: arity mismatch; prototype is '{name}{psig}'.")
1279 for par_idx
in [0..par_arity
[ do
1281 var par_type
= psig
[par_idx
]
1282 if par_idx
== par_vararg
then
1283 for i
in [0..(raw_arity-par_arity
)] do
1284 a
= raw_args
[arg_idx
]
1285 v
.check_conform_expr
(a
, par_type
)
1286 arg_idx
= arg_idx
+ 1
1289 a
= raw_args
[arg_idx
]
1290 v
.check_conform_expr
(a
, par_type
)
1291 arg_idx
= arg_idx
+ 1
1297 # Check the conformity of a set of defined closures
1298 private fun process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: nullable Array[AClosureDef]): nullable MMType
1300 var t
= psig
.return_type
1301 var cs
= psig
.closures
# Declared closures
1304 if not c
.is_optional
then min_arity
+= 1
1307 if cd
!= null then arity
= cd
.length
1308 if cs
.length
> 0 then
1309 if arity
== 0 and min_arity
> 0 then
1310 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1311 else if arity
> cs
.length
or arity
< min_arity
then
1312 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
1314 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1315 var break_list
: nullable Array[ABreakExpr] = null
1316 if t
!= null then break_list
= new Array[ABreakExpr]
1318 # The n_label, is any in only set on the last decl
1319 var n_label
= if arity
> 0 then cd
[arity-1
].n_label
else null
1321 # Process each closure definition
1322 for i
in [0..arity
[ do
1324 var cni
= cdi
.n_id
.to_symbol
1325 var csi
= psig
.closure_named
(cni
)
1327 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1328 v
.scope_ctx
.push_escapable
(esc
, n_label
)
1329 cdi
.accept_typing2
(v
, esc
)
1331 else if cs
.length
== 1 then
1332 v
.error
(cdi
.n_id
, "Error: no closure named '!{cni}' in {name}; only closure is !{cs.first.name}.")
1334 var a
= new Array[String]
1338 v
.error
(cdi
.n_id
, "Error: no closure named '!{cni}' in {name}; only closures are {a.join(",")}.")
1342 # Check break type conformity
1343 if break_list
!= null then
1344 t
= v
.check_conform_multiexpr
(t
, break_list
)
1347 else if arity
!= 0 then
1348 v
.error
(self, "Error: {name} does not require blocks.")
1354 redef class AAbsSendExpr
1355 # Compute the called global property
1356 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])
1358 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
1359 if prop
== null then return
1360 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
1361 if not process_signature
(v
, sig
, prop
.name
, raw_args
) then return
1362 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
1363 if rtype
== null and sig
.return_type
!= null then return
1365 _prop_signature
= sig
1366 _return_type
= rtype
1369 private fun get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): nullable MMMethod
1371 if type_recv
isa MMTypeNone then
1372 if name
== (once
"==".to_symbol
) or name
== (once
"!=".to_symbol
) then
1373 # Special case on != and == that are allowed for 'null'
1374 type_recv
= v
.type_object
.as_nullable
1376 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1380 var lc
= type_recv
.local_class
1381 var prop
: nullable MMMethod = null
1382 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
1383 if prop
== null and v
.local_property
.global
.is_init
then
1384 var props
= lc
.super_methods_named
(name
)
1385 if props
.length
> 1 then
1386 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1388 else if props
.length
== 1 then
1389 var p
= lc
[props
.first
.global
]
1390 assert p
isa MMMethod
1395 if prop
== null then
1396 if is_implicit_self
then
1397 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1399 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1406 # Get the signature for a local property and a receiver
1407 private fun get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
1409 prop
.global
.check_visibility
(v
, self, v
.mmmodule
, recv_is_self
)
1410 var psig
= prop
.signature_for
(type_recv
)
1411 if not recv_is_self
then psig
= psig
.not_for_self
1415 # The invoked method (once computed)
1416 redef fun prop
do return _prop
.as(not null)
1417 var _prop
: nullable MMMethod
1419 # The return type (if any) (once computed)
1420 redef readable var _return_type
: nullable MMType
1423 # A possible call of constructor in a super class
1424 # Could be an explicit call or with the 'super' keyword
1425 redef class ASuperInitCall
1426 private fun register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1428 if parent
!= v
.top_block
and self != v
.top_block
then
1429 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1431 var cla
= v
.mmmodule
[property
.global
.intro
.local_class
.global
]
1432 var prev_class
: nullable MMLocalClass = null
1433 var esic
= v
.explicit_super_init_calls
.as(not null)
1434 if not esic
.is_empty
then
1435 prev_class
= esic
.last
.global
.intro
.local_class
1437 var order
= v
.local_class
.cshe
.reverse_linear_extension
1438 if cla
== v
.local_class
then
1439 v
.explicit_other_init_call
= true
1440 else if not order
.has
(cla
) then
1441 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1442 else if cla
== prev_class
then
1443 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1446 if c
== prev_class
then
1448 else if c
== cla
then
1449 if prev_class
!= null then
1450 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1461 redef class ANewExpr
1462 redef fun compute_raw_arguments
do return n_args
.to_a
1463 redef fun after_typing
(v
)
1465 if not n_type
.is_typed
then return
1466 var t
= n_type
.stype
1467 if t
.local_class
.global
.is_abstract
then
1468 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1472 if n_id
== null then
1473 name
= once
"init".to_symbol
1475 name
= n_id
.to_symbol
1478 do_typing
(v
, t
, false, false, name
, raw_arguments
, null)
1479 if _prop
== null then return
1481 if not prop
.global
.is_init
then
1482 v
.error
(self, "Error: {prop} is not a constructor.")
1491 redef class ASendExpr
1492 # Name of the invoked property
1493 fun name
: Symbol is abstract
1495 # Closure definitions
1496 redef fun closure_defs
: nullable Array[AClosureDef] do return null
1498 redef fun after_typing
(v
)
1503 private fun do_all_typing
(v
: TypingVisitor)
1505 if not v
.check_expr
(n_expr
) then return
1506 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1507 if _prop
== null then return
1508 var prop
= _prop
.as(not null)
1510 if prop
.global
.is_init
then
1511 if not v
.local_property
.global
.is_init
then
1512 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1513 else if not n_expr
.is_self
then
1514 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1516 register_super_init_call
(v
, prop
)
1520 _stype
= return_type
1525 redef class ASendReassignExpr
1526 redef fun read_prop
do return _read_prop
.as(not null)
1527 var _read_prop
: nullable MMMethod
1528 redef fun do_all_typing
(v
)
1530 if not v
.check_expr
(n_expr
) then return
1531 var raw_args
= raw_arguments
1532 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1534 if prop
== null then return
1535 if prop
.global
.is_init
then
1536 if not v
.local_property
.global
.is_init
then
1537 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1538 else if not n_expr
.is_self
then
1539 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1542 var t
= prop
.signature_for
(n_expr
.stype
).return_type
.as(not null)
1543 if not n_expr
.is_self
then t
= t
.not_for_self
1545 var t2
= do_rvalue_typing
(v
, t
)
1546 if t2
== null then return
1547 v
.check_conform
(self, t2
, n_value
.stype
)
1550 raw_args
= raw_args
.to_a
1551 raw_args
.add
(n_value
)
1553 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1554 if prop
.global
.is_init
then
1555 if not v
.local_property
.global
.is_init
then
1556 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1557 else if not n_expr
.is_self
then
1558 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1566 redef class ABinopExpr
1567 redef fun compute_raw_arguments
do return [n_expr2
]
1570 redef fun name
do return once
"==".to_symbol
1571 redef fun after_typing
(v
)
1574 if not n_expr
.is_typed
or not n_expr2
.is_typed
then return
1575 if n_expr
.stype
isa MMTypeNone and not n_expr2
.stype
.is_nullable
or
1576 n_expr2
.stype
isa MMTypeNone and not n_expr
.stype
.is_nullable
then
1577 v
.warning
(self, "Warning: comparaison between null and a non nullable value.")
1580 if n_expr
.stype
isa MMTypeNone then
1581 if n_expr2
.stype
isa MMTypeNone then
1582 v
.warning
(self, "Warning: comparaison between 2 null values.")
1584 try_to_isa
(v
, n_expr2
)
1586 else if n_expr2
.stype
isa MMTypeNone then
1587 try_to_isa
(v
, n_expr
)
1591 private fun try_to_isa
(v
: TypingVisitor, n
: AExpr)
1593 var variable
= n
.its_variable
1594 if variable
!= null and n
.stype
isa MMNullableType then
1595 _if_false_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, n
.stype
.as_notnull
)
1596 _if_true_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, v
.type_none
)
1601 redef fun name
do return once
"!=".to_symbol
1602 redef fun after_typing
(v
)
1605 if not n_expr
.is_typed
or not n_expr2
.is_typed
then return
1606 if n_expr
.stype
isa MMTypeNone and not n_expr2
.stype
.is_nullable
or
1607 n_expr2
.stype
isa MMTypeNone and not n_expr
.stype
.is_nullable
then
1608 v
.warning
(self, "Warning: comparaison between null and a non nullable value.")
1611 if n_expr
.stype
isa MMTypeNone then
1612 if n_expr2
.stype
isa MMTypeNone then
1613 v
.warning
(self, "Warning: comparaison between 2 null values.")
1615 try_to_isa
(v
, n_expr2
)
1617 else if n_expr2
.stype
isa MMTypeNone then
1618 try_to_isa
(v
, n_expr
)
1622 private fun try_to_isa
(v
: TypingVisitor, n
: AExpr)
1624 var variable
= n
.its_variable
1625 if variable
!= null and n
.stype
isa MMNullableType then
1626 _if_true_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, n
.stype
.as_notnull
)
1627 _if_false_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, v
.type_none
)
1632 redef fun name
do return once
"<".to_symbol
1635 redef fun name
do return once
"<=".to_symbol
1638 redef fun name
do return once
"<<".to_symbol
1641 redef fun name
do return once
">".to_symbol
1644 redef fun name
do return once
">=".to_symbol
1647 redef fun name
do return once
">>".to_symbol
1649 redef class APlusExpr
1650 redef fun name
do return once
"+".to_symbol
1652 redef class AMinusExpr
1653 redef fun name
do return once
"-".to_symbol
1655 redef class AStarshipExpr
1656 redef fun name
do return once
"<=>".to_symbol
1658 redef class AStarExpr
1659 redef fun name
do return once
"*".to_symbol
1661 redef class ASlashExpr
1662 redef fun name
do return once
"/".to_symbol
1664 redef class APercentExpr
1665 redef fun name
do return once
"%".to_symbol
1668 redef class AUminusExpr
1669 redef fun name
do return once
"unary -".to_symbol
1670 redef fun compute_raw_arguments
do return null
1673 redef class ACallFormExpr
1674 redef fun after_typing
(v
)
1676 if n_expr
.is_implicit_self
then
1677 var name
= n_id
.to_symbol
1678 var variable
= v
.scope_ctx
[name
]
1679 if variable
!= null then
1681 if variable
isa ClosureVariable then
1682 n
= new AClosureCallExpr.init_aclosurecallexpr
(n_id
, n_args
, n_closure_defs
)
1683 n
._variable
= variable
1685 if not n_args
.is_empty
then
1686 v
.error
(self, "Error: {name} is variable, not a function.")
1689 n
= variable_create
(variable
)
1690 n
._variable
= variable
1701 redef fun closure_defs
1703 if n_closure_defs
.is_empty
then
1706 return n_closure_defs
.to_a
1710 # Create a variable acces corresponding to the call form
1711 fun variable_create
(variable
: Variable): AVarFormExpr is abstract
1714 redef class ACallExpr
1715 redef fun variable_create
(variable
)
1717 return new AVarExpr.init_avarexpr
(n_id
)
1720 redef fun name
do return n_id
.to_symbol
1721 redef fun compute_raw_arguments
do return n_args
.to_a
1724 redef class ACallAssignExpr
1725 redef fun variable_create
(variable
)
1727 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1730 redef fun name
do return (n_id
.text
+ "=").to_symbol
1731 redef fun compute_raw_arguments
do
1732 var res
= n_args
.to_a
1738 redef class ACallReassignExpr
1739 redef fun variable_create
(variable
)
1741 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1744 redef fun name
do return n_id
.to_symbol
1745 redef fun compute_raw_arguments
do return n_args
.to_a
1748 redef class ABraExpr
1749 redef fun name
do return once
"[]".to_symbol
1750 redef fun compute_raw_arguments
do return n_args
.to_a
1751 redef fun closure_defs
1753 if n_closure_defs
.is_empty
then
1756 return n_closure_defs
.to_a
1761 redef class ABraAssignExpr
1762 redef fun name
do return once
"[]=".to_symbol
1763 redef fun compute_raw_arguments
do
1764 var res
= n_args
.to_a
1770 redef class ABraReassignExpr
1771 redef fun name
do return once
"[]".to_symbol
1772 redef fun compute_raw_arguments
do return n_args
.to_a
1775 redef class AInitExpr
1776 redef fun name
do return once
"init".to_symbol
1777 redef fun compute_raw_arguments
do return n_args
.to_a
1780 redef class AClosureCallExpr
1781 var _variable
: nullable ClosureVariable
1782 redef fun variable
do return _variable
.as(not null)
1783 redef fun compute_raw_arguments
do return n_args
.to_a
1785 redef fun after_typing
(v
)
1788 if va
.closure
.is_break
then v
.mark_unreash
(self)
1789 var sig
= va
.closure
.signature
1790 var s
= process_signature
(v
, sig
, n_id
.to_symbol
, compute_raw_arguments
)
1791 if not n_closure_defs
.is_empty
then
1792 process_closures
(v
, sig
, n_id
.to_symbol
, n_closure_defs
.to_a
)
1794 if not s
then return
1795 _prop_signature
= sig
1796 _stype
= sig
.return_type
1801 redef class AClosureId
1802 fun to_symbol
: Symbol is abstract
1804 redef class ASimpleClosureId
1805 redef fun to_symbol
: Symbol do return n_id
.to_symbol
1807 redef class ABreakClosureId
1808 redef fun to_symbol
: Symbol do return n_kwbreak
.to_symbol
1811 redef class AClosureDef
1812 var _closure
: nullable MMClosure
1813 redef fun closure
do return _closure
.as(not null)
1815 # The corresponding escapable object
1816 readable var _escapable
: nullable EscapableBlock
1818 var _accept_typing2
: Bool = false
1819 redef fun accept_typing
(v
)
1821 # Typing is deferred, wait accept_typing2(v)
1822 if _accept_typing2
then super
1825 private fun accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure)
1829 var sig
= esc
.closure
.signature
1830 if sig
.arity
!= n_ids
.length
then
1831 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_ids.length} found.")
1835 _closure
= esc
.closure
1837 v
.scope_ctx
.push
(self)
1838 var old_flow_ctx
= v
.flow_ctx
1839 var old_base_flow_ctx
= v
.base_flow_ctx
1840 v
.base_flow_ctx
= v
.flow_ctx
1841 variables
= new Array[AutoVariable]
1842 for i
in [0..n_ids
.length
[ do
1843 var va
= new AutoVariable(n_ids
[i
].to_symbol
, n_ids
[i
])
1846 v
.scope_ctx
.add_variable
(va
)
1849 _accept_typing2
= true
1852 if v
.flow_ctx
.unreash
== false then
1853 if closure
.signature
.return_type
!= null then
1854 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
1855 else if closure
.is_break
and esc
.break_list
!= null then
1856 v
.error
(self, "Control error: Reached end of break block (a 'break' with a value was expected).")
1859 v
.flow_ctx
= old_flow_ctx
1860 v
.base_flow_ctx
= old_base_flow_ctx
1865 class ATypeCheckExpr
1867 private fun check_expr_cast
(v
: TypingVisitor, n_expr
: AExpr, n_type
: AType)
1869 if not v
.check_expr
(n_expr
) then return
1870 if not n_type
.is_typed
then return
1871 var etype
= n_expr
.stype
1872 var ttype
= n_type
.stype
1873 if etype
== ttype
then
1874 v
.warning
(self, "Warning: Expression is already a {ttype}.")
1875 else if etype
< ttype
then
1876 v
.warning
(self, "Warning: Expression is already a {ttype} since it is a {etype}.")
1877 else if etype
isa MMTypeNone then
1878 # ttype is not nullable because of prevous test
1879 v
.warning
(self, "Warning: Expression is null therefore cannot be a {ttype}.")
1880 else if etype
.is_nullable
and etype
.as_notnull
== ttype
then
1881 if ttype
isa MMTypeFormal and ttype
.bound
.is_nullable
then
1882 # No warning in this case since with
1883 # type T: nullable A
1885 # 'x.as(not null)' != 'x.as(T)'
1886 # 'x != null' != 'x isa T'
1887 else if self isa AIsaExpr then
1888 v
.warning
(self, "Warning: Prefer '!= null'.")
1890 v
.warning
(self, "Warning: Prefer '.as(not null)'.")
1896 redef class AIsaExpr
1897 super ATypeCheckExpr
1898 redef fun after_typing
(v
)
1900 check_expr_cast
(v
, n_expr
, n_type
)
1901 if not n_type
.is_typed
then return
1902 var variable
= n_expr
.its_variable
1903 if variable
!= null then
1904 _if_true_flow_ctx
= v
.flow_ctx
.sub_with
(self, variable
, n_type
.stype
)
1906 _stype
= v
.type_bool
1911 redef class AAsCastExpr
1912 super ATypeCheckExpr
1913 redef fun after_typing
(v
)
1915 check_expr_cast
(v
, n_expr
, n_type
)
1916 if not n_type
.is_typed
then return
1917 _stype
= n_type
.stype
1918 _is_typed
= _stype
!= null
1922 redef class AAsNotnullExpr
1923 redef fun after_typing
(v
)
1925 if not v
.check_expr
(n_expr
) then return
1926 var t
= n_expr
.stype
1927 if t
isa MMTypeNone then
1928 v
.error
(n_expr
, "Type error: 'as(not null)' on 'null' value.")
1930 else if not t
.is_nullable
then
1931 v
.warning
(n_expr
, "Warning: 'as(not null)' on non nullable type.")
1933 _stype
= n_expr
.stype
.as_notnull
1938 redef class AProxyExpr
1939 redef fun after_typing
(v
)
1941 if not n_expr
.is_typed
then return
1943 if n_expr
.is_statement
then return
1944 _stype
= n_expr
.stype
1948 redef class AOnceExpr
1949 redef fun accept_typing
(v
)
1951 if v
.once_count
> 0 then
1952 v
.warning
(self, "Useless once in a once expression.")
1954 v
.once_count
= v
.once_count
+ 1
1958 v
.once_count
= v
.once_count
- 1