1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2008 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Analysis property bodies, statements and expressions
24 redef class MMSrcModule
25 # Walk trough the module and type statments and expressions
26 # Require than supermodules are processed
27 fun do_typing
(tc
: ToolContext)
29 var tv
= new TypingVisitor(tc
, self)
35 # * Associate local variables to nodes
36 # * Distinguish method call and local variable access
37 # * Resolve call and attribute access
38 # * Check type conformance
39 private class TypingVisitor
40 special AbsSyntaxVisitor
43 if n
!= null then n
.accept_typing
(self)
46 # Current knowledge about variables names and types
47 fun variable_ctx
: VariableContext do return _variable_ctx
.as(not null)
48 writable var _variable_ctx
: nullable VariableContext
50 # Non-bypassable knowledge about variables names and types
51 fun base_variable_ctx
: VariableContext do return _base_variable_ctx
.as(not null)
52 writable var _base_variable_ctx
: nullable VariableContext
54 # Current knowledge about escapable blocks
55 readable writable var _escapable_ctx
: EscapableContext = new EscapableContext(self)
57 # The current reciever
58 fun self_var
: ParamVariable do return _self_var
.as(not null)
59 writable var _self_var
: nullable ParamVariable
61 # Block of the current method
62 readable writable var _top_block
: nullable AExpr
64 # List of explicit invocation of constructors of super-classes
65 readable writable var _explicit_super_init_calls
: nullable Array[MMMethod]
67 # Is a other constructor of the same class invoked
68 readable writable var _explicit_other_init_call
: Bool = false
70 # Make the if_true_variable_ctx of the expression effective
71 private fun use_if_true_variable_ctx
(e
: AExpr)
73 var ctx
= e
.if_true_variable_ctx
74 if ctx
!= null then variable_ctx
= ctx
77 # Make the if_false_variable_ctx of the expression effective
78 private fun use_if_false_variable_ctx
(e
: AExpr)
80 var ctx
= e
.if_false_variable_ctx
81 if ctx
!= null then variable_ctx
= ctx
84 # Are we inside a default closure definition ?
85 readable writable var _is_default_closure_definition
: Bool = false
87 # Number of nested once
88 readable writable var _once_count
: Int = 0
90 init(tc
, module) do super
92 private fun get_default_constructor_for
(n
: ANode, c
: MMLocalClass, prop
: MMSrcMethod): nullable MMMethod
95 #var prop = v.local_property
96 #assert prop isa MMMethod
97 var candidates
= new Array[MMMethod]
98 var false_candidates
= new Array[MMMethod]
99 var parity
= prop
.signature
.arity
100 for g
in c
.global_properties
do
101 if not g
.is_init_for
(c
) then continue
103 var gps
= gp
.signature_for
(c
.get_type
)
104 assert gp
isa MMSrcMethod
105 var garity
= gps
.arity
106 if gp
.name
== prop
.name
then
107 if garity
== 0 or (parity
== garity
and prop
.signature
< gps
) then
110 false_candidates
.add
(gp
)
112 else if garity
== 0 and gp
.name
== once
("init".to_symbol
) then
114 false_candidates
.add
(gp
)
116 false_candidates
.add
(gp
)
119 if candidates
.length
== 1 then
120 return candidates
.first
121 else if candidates
.length
> 0 then
122 var a
= new Array[String]
123 for p
in candidates
do
124 a
.add
("{p.full_name}{p.signature.as(not null)}")
126 v
.error
(n
, "Error: Conflicting default constructor to call for {c}: {a.join(", ")}.")
128 else if false_candidates
.length
> 0 then
129 var a
= new Array[String]
130 for p
in false_candidates
do
131 a
.add
("{p.full_name}{p.signature.as(not null)}")
133 v
.error
(n
, "Error: there is no available compatible constrctor in {c}. Discarded candidates are {a.join(", ")}.")
136 v
.error
(n
, "Error: there is no available compatible constrctor in {c}.")
143 ###############################################################################
146 private fun accept_typing
(v
: TypingVisitor)
148 accept_abs_syntax_visitor
(v
)
151 private fun after_typing
(v
: TypingVisitor) do end
154 redef class AClassdef
155 redef fun accept_typing
(v
)
157 v
.variable_ctx
= new RootVariableContext(v
, self)
158 v
.base_variable_ctx
= v
.variable_ctx
159 v
.self_var
= new ParamVariable("self".to_symbol
, self)
160 v
.self_var
.stype
= local_class
.get_type
166 redef fun self_var
do return _self_var
.as(not null)
167 var _self_var
: nullable ParamVariable
170 redef class AAttrPropdef
171 redef fun accept_typing
(v
)
173 var old_var_ctx
= v
.variable_ctx
174 v
.variable_ctx
= old_var_ctx
.sub
(self)
175 _self_var
= v
.self_var
177 if n_expr
!= null then
178 v
.check_conform_expr
(n_expr
.as(not null), prop
.signature
.return_type
.as(not null))
180 v
.variable_ctx
= old_var_ctx
184 redef class AMethPropdef
185 redef fun accept_typing
(v
)
187 var old_var_ctx
= v
.variable_ctx
188 v
.variable_ctx
= old_var_ctx
.sub
(self)
189 _self_var
= v
.self_var
191 v
.variable_ctx
= old_var_ctx
195 redef class AConcreteMethPropdef
196 redef fun after_typing
(v
)
199 if v
.variable_ctx
.unreash
== false and method
.signature
.return_type
!= null then
200 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
205 redef class AConcreteInitPropdef
206 redef fun accept_typing
(v
)
208 v
.top_block
= n_block
209 v
.explicit_super_init_calls
= explicit_super_init_calls
210 v
.explicit_other_init_call
= false
214 redef fun after_typing
(v
)
217 if v
.explicit_other_init_call
or method
.global
.intro
!= method
then
221 var l
= explicit_super_init_calls
.length
222 var cur_m
: nullable MMMethod = null
223 var cur_c
: nullable MMLocalClass = null
225 cur_m
= explicit_super_init_calls
[i
]
226 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
229 while j
< v
.local_class
.cshe
.direct_greaters
.length
do
230 var c
= v
.local_class
.cshe
.direct_greaters
[j
]
231 if c
.global
.is_interface
or c
.global
.is_universal
or c
.global
.is_mixin
then
233 else if cur_c
!= null and (c
.cshe
<= cur_c
or cur_c
.global
.is_mixin
) then
234 if c
== cur_c
then j
+= 1
235 super_init_calls
.add
(cur_m
.as(not null))
238 cur_m
= explicit_super_init_calls
[i
]
239 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
245 var p
= v
.get_default_constructor_for
(self, c
, method
)
247 super_init_calls
.add
(p
)
257 redef fun after_typing
(v
)
259 v
.variable_ctx
.add
(variable
)
263 redef class AClosureDecl
264 # The corresponding escapable object
265 readable var _escapable
: nullable EscapableBlock
267 redef fun accept_typing
(v
)
269 # Register the closure for ClosureCallExpr
270 v
.variable_ctx
.add
(variable
)
272 var old_var_ctx
= v
.variable_ctx
273 var old_base_var_ctx
= v
.base_variable_ctx
274 v
.base_variable_ctx
= v
.variable_ctx
275 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
277 var blist
: nullable Array[AExpr] = null
278 var t
= v
.local_property
.signature
.return_type
279 if t
!= null then blist
= new Array[AExpr]
280 var escapable
= new EscapableClosure(self, variable
.closure
, blist
)
281 _escapable
= escapable
282 v
.escapable_ctx
.push
(escapable
, null)
284 v
.is_default_closure_definition
= true
288 v
.is_default_closure_definition
= false
290 if n_expr
!= null then
291 if v
.variable_ctx
.unreash
== false then
292 if variable
.closure
.signature
.return_type
!= null then
293 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
294 else if variable
.closure
.is_break
and escapable
.break_list
!= null then
295 v
.error
(self, "Control error: Reached end of break block (a 'break' with a value was expected).")
299 if blist
!= null then for x
in blist
do
300 v
.check_conform_expr
(x
, t
)
303 v
.variable_ctx
= old_var_ctx
304 v
.base_variable_ctx
= old_base_var_ctx
310 redef fun stype
: MMType do return _stype
.as(not null)
311 redef fun is_typed
: Bool do return _stype
!= null
312 var _stype
: nullable MMType
314 redef fun after_typing
(v
)
316 _stype
= get_stype
(v
)
321 redef readable var _is_typed
: Bool = false
322 redef fun is_statement
: Bool do return _stype
== null
326 print
"{location}: not is_typed"
330 print
"{location}: is_statement"
333 return _stype
.as(not null)
335 var _stype
: nullable MMType
337 redef fun after_typing
(v
)
339 # Default behavior is to be happy
343 # Is the expression the implicit receiver
344 fun is_implicit_self
: Bool do return false
346 # Is the expression the current receiver (implicit or explicit)
347 fun is_self
: Bool do return false
349 # The variable accessed is any
350 fun its_variable
: nullable Variable do return null
352 # The variable type information if current boolean expression is true
353 readable private var _if_true_variable_ctx
: nullable VariableContext
355 # The variable type information if current boolean expression is false
356 readable private var _if_false_variable_ctx
: nullable VariableContext
359 redef class AVardeclExpr
360 var _variable
: nullable VarVariable
361 redef fun variable
do return _variable
.as(not null)
363 redef fun after_typing
(v
)
365 var va
= new VarVariable(n_id
.to_symbol
, n_id
)
367 v
.variable_ctx
.add
(va
)
369 if ne
!= null then v
.variable_ctx
.mark_is_set
(va
)
371 if n_type
!= null then
372 if not n_type
.is_typed
then return
373 va
.stype
= n_type
.stype
375 v
.check_conform_expr
(ne
, va
.stype
)
377 else if ne
!= null then
378 if not v
.check_expr
(ne
) then return
381 va
.stype
= v
.type_object
.as_nullable
387 redef class ABlockExpr
388 redef fun accept_typing
(v
)
391 if not v
.variable_ctx
.unreash
then
393 else if not v
.variable_ctx
.already_unreash
then
394 v
.variable_ctx
.already_unreash
= true
395 v
.error
(e
, "Error: unreachable statement.")
403 redef class AReturnExpr
404 redef fun after_typing
(v
)
406 v
.variable_ctx
.unreash
= true
407 var t
= v
.local_property
.signature
.return_type
409 if v
.is_default_closure_definition
then
410 v
.error
(self, "Error: 'return' invalid in default closure definitions. Use 'continue' or 'break'.")
415 if e
== null and t
!= null then
416 v
.error
(self, "Error: Return without value in a function.")
417 else if e
!= null and t
== null then
418 v
.error
(self, "Error: Return with value in a procedure.")
419 else if e
!= null and t
!= null then
420 v
.check_conform_expr
(e
, t
)
426 redef class AContinueExpr
427 redef fun after_typing
(v
)
429 v
.variable_ctx
.unreash
= true
430 var esc
= compute_escapable_block
(v
.escapable_ctx
)
431 if esc
== null then return
433 if esc
.is_break_block
then
434 v
.error
(self, "Error: cannot 'continue', only 'break'.")
438 var t
= esc
.continue_stype
439 if n_expr
== null and t
!= null then
440 v
.error
(self, "Error: continue with a value required in this block.")
441 else if n_expr
!= null and t
== null then
442 v
.error
(self, "Error: continue without value required in this block.")
443 else if n_expr
!= null and t
!= null then
444 v
.check_conform_expr
(n_expr
.as(not null), t
)
450 redef class ABreakExpr
451 redef fun after_typing
(v
)
453 v
.variable_ctx
.unreash
= true
454 var esc
= compute_escapable_block
(v
.escapable_ctx
)
455 if esc
== null then return
457 esc
.break_variable_contexts
.add
(v
.variable_ctx
)
459 var bl
= esc
.break_list
460 if n_expr
== null and bl
!= null then
461 v
.error
(self, "Error: break with a value required in this block.")
462 else if n_expr
!= null and bl
== null then
463 v
.error
(self, "Error: break without value required in this block.")
464 else if n_expr
!= null and bl
!= null then
465 # Typing check can only be done later
466 bl
.add
(n_expr
.as(not null))
472 redef class AAbortExpr
473 redef fun after_typing
(v
)
475 v
.variable_ctx
.unreash
= true
481 # The corresponding escapable block
482 readable var _escapable
: nullable EscapableBlock
484 redef fun accept_typing
(v
)
486 var escapable
= new BreakOnlyEscapableBlock(self)
487 _escapable
= escapable
488 v
.escapable_ctx
.push
(escapable
, n_label
)
489 var old_var_ctx
= v
.variable_ctx
491 v
.variable_ctx
= old_var_ctx
.sub
(self)
494 # Add the end of the block as an exit context
495 if not v
.variable_ctx
.unreash
then
496 escapable
.break_variable_contexts
.add
(v
.variable_ctx
)
499 # Merge all exit contexts
500 if not escapable
.break_variable_contexts
.is_empty
then
501 v
.variable_ctx
= old_var_ctx
.merge
(self, escapable
.break_variable_contexts
, v
.base_variable_ctx
)
510 redef fun accept_typing
(v
)
512 var old_var_ctx
= v
.variable_ctx
513 v
.enter_visit
(n_expr
)
514 v
.check_conform_expr
(n_expr
, v
.type_bool
)
516 # Prepare 'then' context
517 v
.use_if_true_variable_ctx
(n_expr
)
520 if n_then
!= null then
521 v
.variable_ctx
= v
.variable_ctx
.sub
(n_then
.as(not null))
522 v
.enter_visit
(n_then
)
525 # Remember what appened in the 'then'
526 var then_var_ctx
= v
.variable_ctx
528 # Prepare 'else' context
529 v
.variable_ctx
= old_var_ctx
530 v
.use_if_false_variable_ctx
(n_expr
)
533 if n_else
!= null then
534 v
.variable_ctx
= v
.variable_ctx
.sub
(n_else
.as(not null))
535 v
.enter_visit
(n_else
)
538 # Merge 'then' and 'else' contexts
539 v
.variable_ctx
= old_var_ctx
.merge_reash
(self, then_var_ctx
, v
.variable_ctx
, v
.base_variable_ctx
)
544 redef class AWhileExpr
545 # The corresponding escapable block
546 readable var _escapable
: nullable EscapableBlock
548 redef fun accept_typing
(v
)
550 var escapable
= new EscapableBlock(self)
551 _escapable
= escapable
552 v
.escapable_ctx
.push
(escapable
, n_label
)
553 var old_var_ctx
= v
.variable_ctx
554 var old_base_var_ctx
= v
.base_variable_ctx
555 v
.base_variable_ctx
= v
.variable_ctx
558 v
.enter_visit
(n_expr
)
559 v
.check_conform_expr
(n_expr
, v
.type_bool
)
561 if n_expr
isa ATrueExpr then
562 v
.warning
(self, "Warning: use 'loop' instead of 'while true do'.")
565 # Prepare inside context (assert cond)
566 v
.use_if_true_variable_ctx
(n_expr
)
569 if n_block
!= null then
570 v
.variable_ctx
= v
.variable_ctx
.sub
(n_block
.as(not null))
571 v
.enter_visit
(n_block
)
574 # Compute outside context (assert !cond + all breaks)
575 v
.variable_ctx
= old_var_ctx
576 v
.use_if_false_variable_ctx
(n_expr
)
577 escapable
.break_variable_contexts
.add
(v
.variable_ctx
)
578 v
.variable_ctx
= old_var_ctx
.merge
(self, escapable
.break_variable_contexts
, v
.base_variable_ctx
)
580 v
.base_variable_ctx
= old_base_var_ctx
586 redef class ALoopExpr
587 # The corresponding escapable block
588 readable var _escapable
: nullable EscapableBlock
590 redef fun accept_typing
(v
)
592 var escapable
= new EscapableBlock(self)
593 _escapable
= escapable
594 v
.escapable_ctx
.push
(escapable
, n_label
)
595 var old_var_ctx
= v
.variable_ctx
596 var old_base_var_ctx
= v
.base_variable_ctx
597 v
.base_variable_ctx
= v
.variable_ctx
600 if n_block
!= null then
601 v
.variable_ctx
= v
.variable_ctx
.sub
(n_block
.as(not null))
602 v
.enter_visit
(n_block
)
605 # Compute outside context (assert all breaks)
606 if escapable
.break_variable_contexts
.is_empty
then
607 old_var_ctx
.unreash
= true
608 v
.variable_ctx
= old_var_ctx
610 v
.variable_ctx
= old_var_ctx
.merge
(self, escapable
.break_variable_contexts
, v
.base_variable_ctx
)
613 v
.base_variable_ctx
= old_base_var_ctx
620 var _variable
: nullable AutoVariable
621 redef fun variable
do return _variable
.as(not null)
623 # The corresponding escapable block
624 readable var _escapable
: nullable EscapableBlock
626 redef fun accept_typing
(v
)
628 var escapable
= new EscapableBlock(self)
629 _escapable
= escapable
630 v
.escapable_ctx
.push
(escapable
, n_label
)
632 var old_var_ctx
= v
.variable_ctx
633 var old_base_var_ctx
= v
.base_variable_ctx
634 v
.base_variable_ctx
= v
.variable_ctx
635 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
636 var va
= new AutoVariable(n_id
.to_symbol
, n_id
)
638 v
.variable_ctx
.add
(va
)
641 v
.enter_visit
(n_expr
)
643 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then
644 v
.variable_ctx
= old_var_ctx
645 v
.base_variable_ctx
= old_base_var_ctx
649 var expr_type
= n_expr
.stype
652 var meth_iterator
= v
.get_method
(expr_type
, once
"iterator".to_symbol
)
653 var iter_type
= meth_iterator
.signature_for
(expr_type
).return_type
.as(not null)
654 var meth_item
= v
.get_method
(iter_type
, once
("item".to_symbol
))
655 var va_stype
= meth_item
.signature_for
(iter_type
).return_type
.as(not null)
656 if not n_expr
.is_self
then va_stype
= va_stype
.not_for_self
660 if n_block
!= null then v
.enter_visit
(n_block
)
663 v
.variable_ctx
= old_var_ctx
664 v
.base_variable_ctx
= old_base_var_ctx
670 redef class AAssertExpr
671 redef fun accept_typing
(v
)
674 v
.enter_visit
(n_expr
)
675 v
.check_conform_expr
(n_expr
, v
.type_bool
)
677 # Process optional 'else' part
678 if n_else
!= null then
679 var old_var_ctx
= v
.variable_ctx
680 v
.use_if_false_variable_ctx
(n_expr
)
681 v
.enter_visit
(n_else
)
682 v
.variable_ctx
= old_var_ctx
686 v
.use_if_true_variable_ctx
(n_expr
)
691 redef class AVarFormExpr
692 var _variable
: nullable Variable
693 redef fun variable
do return _variable
.as(not null)
697 redef fun its_variable
do return variable
699 redef fun after_typing
(v
)
701 v
.variable_ctx
.check_is_set
(self, variable
)
702 _stype
= v
.variable_ctx
.stype
(variable
)
703 _is_typed
= _stype
!= null
707 redef class AVarAssignExpr
708 redef fun after_typing
(v
)
710 v
.variable_ctx
.mark_is_set
(variable
)
712 # Check the base type
713 var btype
= v
.base_variable_ctx
.stype
(variable
)
714 if not v
.check_expr
(n_value
) then return
715 if btype
!= null and not v
.check_conform_expr
(n_value
, btype
) then return
718 v
.variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, n_value
.stype
)
724 redef class AReassignFormExpr
725 # Compute and check method used through the reassigment operator
726 # On success return the static type of the result of the reassigment operator
727 # Else display an error and return null
728 private fun do_rvalue_typing
(v
: TypingVisitor, type_lvalue
: nullable MMType): nullable MMType
730 if type_lvalue
== null then
733 var name
= n_assign_op
.method_name
734 if type_lvalue
isa MMTypeNone then
735 v
.error
(self, "Error: Method '{name}' call on 'null'.")
738 var lc
= type_lvalue
.local_class
739 if not lc
.has_global_property_by_name
(name
) then
740 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
743 var prop
= lc
.select_method
(name
)
744 prop
.global
.check_visibility
(v
, self, v
.module, false)
745 var psig
= prop
.signature_for
(type_lvalue
)
746 _assign_method
= prop
747 if not v
.check_conform_expr
(n_value
, psig
[0].not_for_self
) then return null
748 return psig
.return_type
.not_for_self
751 redef fun assign_method
do return _assign_method
.as(not null)
752 var _assign_method
: nullable MMMethod
755 redef class AVarReassignExpr
756 redef fun after_typing
(v
)
758 v
.variable_ctx
.check_is_set
(self, variable
)
759 v
.variable_ctx
.mark_is_set
(variable
)
760 var t
= v
.variable_ctx
.stype
(variable
)
761 var t2
= do_rvalue_typing
(v
, t
)
762 if t2
== null then return
764 # Check the base type
765 var btype
= v
.base_variable_ctx
.stype
(variable
)
766 if not v
.check_expr
(n_value
) then return
767 if btype
!= null and not v
.check_conform
(n_value
, t2
, btype
) then return
770 v
.variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, t2
)
776 redef class AAssignOp
777 fun method_name
: Symbol is abstract
779 redef class APlusAssignOp
780 redef fun method_name
do return once
"+".to_symbol
782 redef class AMinusAssignOp
783 redef fun method_name
do return once
"-".to_symbol
786 redef class ASelfExpr
787 var _variable
: nullable ParamVariable
788 redef fun variable
do return _variable
.as(not null)
790 redef fun its_variable
do return variable
792 redef fun after_typing
(v
)
794 _variable
= v
.self_var
795 _stype
= v
.variable_ctx
.stype
(variable
)
799 redef fun is_self
do return true
802 redef class AImplicitSelfExpr
803 redef fun is_implicit_self
do return true
806 redef class AIfexprExpr
807 redef fun accept_typing
(v
)
809 var old_var_ctx
= v
.variable_ctx
812 v
.enter_visit
(n_expr
)
813 v
.check_conform_expr
(n_expr
, v
.type_bool
)
815 # Prepare 'then' context
816 v
.use_if_true_variable_ctx
(n_expr
)
819 v
.variable_ctx
= v
.variable_ctx
.sub
(n_then
)
820 v
.enter_visit
(n_then
)
822 # Remember what appened in the 'then'
823 var then_var_ctx
= v
.variable_ctx
825 # Prepare 'else' context
826 v
.variable_ctx
= old_var_ctx
827 v
.use_if_false_variable_ctx
(n_expr
)
830 v
.variable_ctx
= v
.variable_ctx
.sub
(n_else
)
831 v
.enter_visit
(n_else
)
833 # Merge 'then' and 'else' contexts
834 v
.variable_ctx
= old_var_ctx
.merge_reash
(self, then_var_ctx
, v
.variable_ctx
, v
.base_variable_ctx
)
836 var stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
837 if stype
== null then return
844 redef class ABoolExpr
845 redef fun after_typing
(v
)
853 redef fun accept_typing
(v
)
855 var old_var_ctx
= v
.variable_ctx
856 var stype
= v
.type_bool
859 # Process left operand
860 v
.enter_visit
(n_expr
)
862 # Prepare right operand context
863 v
.use_if_false_variable_ctx
(n_expr
)
865 # Process right operand
866 v
.enter_visit
(n_expr2
)
867 if n_expr2
.if_false_variable_ctx
!= null then
868 _if_false_variable_ctx
= n_expr2
.if_false_variable_ctx
870 _if_false_variable_ctx
= v
.variable_ctx
873 v
.variable_ctx
= old_var_ctx
875 v
.check_conform_expr
(n_expr
, stype
)
876 v
.check_conform_expr
(n_expr2
, stype
)
883 redef fun accept_typing
(v
)
885 var old_var_ctx
= v
.variable_ctx
886 var stype
= v
.type_bool
888 # Process left operand
889 v
.enter_visit
(n_expr
)
891 # Prepare right operand context
892 v
.use_if_true_variable_ctx
(n_expr
)
894 # Process right operand
895 v
.enter_visit
(n_expr2
)
896 if n_expr2
.if_true_variable_ctx
!= null then
897 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
899 _if_true_variable_ctx
= v
.variable_ctx
902 v
.variable_ctx
= old_var_ctx
904 v
.check_conform_expr
(n_expr
, stype
)
905 v
.check_conform_expr
(n_expr2
, stype
)
912 redef fun after_typing
(v
)
914 v
.check_conform_expr
(n_expr
, v
.type_bool
)
916 # Invert if_true/if_false information
917 _if_false_variable_ctx
= n_expr
._if_true_variable_ctx
918 _if_true_variable_ctx
= n_expr
._if_false_variable_ctx
926 redef fun after_typing
(v
)
933 redef class AFloatExpr
934 redef fun after_typing
(v
)
936 _stype
= v
.type_float
941 redef class ACharExpr
942 redef fun after_typing
(v
)
949 redef class AStringFormExpr
950 redef fun after_typing
(v
)
952 _stype
= v
.type_string
957 redef class ASuperstringExpr
958 redef fun atype
do return _atype
.as(not null)
959 var _atype
: nullable MMType
960 redef fun after_typing
(v
)
962 var otype
= v
.type_object
963 var stype
= v
.type_string
965 for e
in n_exprs
do v
.check_conform_expr
(e
, otype
)
966 var atype
= v
.type_array
(stype
)
972 redef class ANullExpr
973 redef fun after_typing
(v
)
980 redef class AArrayExpr
981 redef fun after_typing
(v
)
983 var stype
= v
.check_conform_multiexpr
(null, n_exprs
)
984 if stype
!= null then do_typing
(v
, stype
)
987 private fun do_typing
(v
: TypingVisitor, element_type
: MMType)
989 _stype
= v
.type_array
(element_type
)
994 redef class ARangeExpr
995 redef fun after_typing
(v
)
997 if not v
.check_expr
(n_expr
) or not v
.check_expr
(n_expr2
) then return
998 var ntype
= n_expr
.stype
999 var ntype2
= n_expr2
.stype
1000 if ntype
< ntype2
then
1002 else if not ntype2
< ntype
then
1003 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
1006 var dtype
= v
.type_discrete
1007 if not v
.check_conform_expr
(n_expr
, dtype
) or not v
.check_conform_expr
(n_expr2
, dtype
) then return
1008 _stype
= v
.type_range
(ntype
)
1013 redef class ASuperExpr
1014 redef readable var _init_in_superclass
: nullable MMMethod
1015 redef fun compute_raw_arguments
do return n_args
.to_a
1016 redef fun after_typing
(v
)
1018 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
1019 if not precs
.is_empty
then
1020 v
.local_property
.need_super
= true
1021 else if v
.local_property
.global
.is_init
then
1022 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
1023 for p
in base_precs
do
1024 if not p
.global
.is_init
then
1025 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
1027 precs
.add
(v
.local_class
[p
.global
])
1030 if precs
.is_empty
then
1031 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
1033 else if precs
.length
> 1 then
1034 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
1037 var p
= base_precs
.first
1038 assert p
isa MMMethod
1039 _init_in_superclass
= p
1040 register_super_init_call
(v
, p
)
1041 if n_args
.length
> 0 then
1042 var signature
= get_signature
(v
, v
.self_var
.stype
.as(not null), p
, true)
1043 process_signature
(v
, signature
, p
.name
, compute_raw_arguments
)
1046 v
.error
(self, "Error: No super method to call for {v.local_property}.")
1050 if precs
.first
.signature_for
(v
.self_var
.stype
.as(not null)).return_type
!= null then
1051 var stypes
= new Array[MMType]
1052 var stype
: nullable MMType = null
1053 for prop
in precs
do
1054 assert prop
isa MMMethod
1055 var t
= prop
.signature_for
(v
.self_var
.stype
.as(not null)).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
1057 if stype
== null or stype
< t
then
1062 v
.check_conform
(self, t
, stype
.as(not null))
1066 var p
= v
.local_property
1067 assert p
isa MMSrcMethod
1073 redef class AAttrFormExpr
1074 redef fun prop
do return _prop
.as(not null)
1075 var _prop
: nullable MMAttribute
1077 redef fun attr_type
do return _attr_type
.as(not null)
1078 var _attr_type
: nullable MMType
1080 # Compute the attribute accessed
1081 private fun do_typing
(v
: TypingVisitor)
1083 if not v
.check_expr
(n_expr
) then return
1084 var type_recv
= n_expr
.stype
1085 var name
= n_id
.to_symbol
1086 if type_recv
isa MMTypeNone then
1087 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1090 var lc
= type_recv
.local_class
1091 if not lc
.has_global_property_by_name
(name
) then
1092 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
1095 var prop
= lc
.select_attribute
(name
)
1096 if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
1097 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
1100 var at
= prop
.signature_for
(type_recv
).return_type
1101 if not n_expr
.is_self
then at
= at
.not_for_self
1106 redef class AAttrExpr
1107 redef fun after_typing
(v
)
1110 if _prop
== null then return
1116 redef class AAttrAssignExpr
1117 redef fun after_typing
(v
)
1120 if _prop
== null then return
1121 if not v
.check_conform_expr
(n_value
, attr_type
) then return
1126 redef class AAttrReassignExpr
1127 redef fun after_typing
(v
)
1130 if _prop
== null then return
1131 var t
= do_rvalue_typing
(v
, attr_type
)
1132 if t
== null then return
1133 v
.check_conform
(self, t
, n_value
.stype
)
1138 redef class AIssetAttrExpr
1139 redef fun after_typing
(v
)
1142 if _prop
== null then return
1143 if attr_type
.is_nullable
then
1144 v
.error
(self, "Error: isset on a nullable attribute.")
1146 _stype
= v
.type_bool
1151 redef class AAbsAbsSendExpr
1152 # The signature of the called property
1153 redef fun prop_signature
do return _prop_signature
.as(not null)
1154 var _prop_signature
: nullable MMSignature
1156 # Raw arguments used (without vararg transformation)
1157 redef fun raw_arguments
: Array[AExpr]
1159 var res
= _raw_arguments_cache
1163 res
= compute_raw_arguments
1164 if res
== null then res
= new Array[AExpr]
1165 _raw_arguments_cache
= res
1170 var _raw_arguments_cache
: nullable Array[AExpr] = null
1172 fun compute_raw_arguments
: nullable Array[AExpr]
1174 print
"{location} no compute_raw_arguments"
1178 # Check the conformity of a set of arguments `raw_args' to a signature.
1179 private fun process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: nullable Array[AExpr]): Bool
1181 var par_vararg
= psig
.vararg_rank
1182 var par_arity
= psig
.arity
1184 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
1185 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
1186 v
.error
(self, "Error: arity missmatch; prototype is '{name}{psig}'.")
1190 for par_idx
in [0..par_arity
[ do
1192 var par_type
= psig
[par_idx
]
1193 if par_idx
== par_vararg
then
1194 for i
in [0..(raw_arity-par_arity
)] do
1195 a
= raw_args
[arg_idx
]
1196 v
.check_conform_expr
(a
, par_type
)
1197 arg_idx
= arg_idx
+ 1
1200 a
= raw_args
[arg_idx
]
1201 v
.check_conform_expr
(a
, par_type
)
1202 arg_idx
= arg_idx
+ 1
1208 # Check the conformity of a set of defined closures
1209 private fun process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: nullable Array[AClosureDef]): nullable MMType
1211 var t
= psig
.return_type
1212 var cs
= psig
.closures
# Declared closures
1215 if not c
.is_optional
then min_arity
+= 1
1218 if cd
!= null then arity
= cd
.length
1219 if cs
.length
> 0 then
1220 if arity
== 0 and min_arity
> 0 then
1221 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1222 else if arity
> cs
.length
or arity
< min_arity
then
1223 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
1225 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1226 var break_list
: nullable Array[ABreakExpr] = null
1227 if t
!= null then break_list
= new Array[ABreakExpr]
1229 # The n_label, is any in only set on the last decl
1230 var n_label
= if arity
> 0 then cd
[arity-1
].n_label
else null
1232 # Process each closure definition
1233 for i
in [0..arity
[ do
1235 var cni
= cdi
.n_id
.to_symbol
1236 var csi
= psig
.closure_named
(cni
)
1238 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1239 v
.escapable_ctx
.push
(esc
, n_label
)
1240 cdi
.accept_typing2
(v
, esc
)
1242 else if cs
.length
== 1 then
1243 v
.error
(cdi
.n_id
, "Error: no closure named '!{cni}' in {name}; only closure is !{cs.first.name}.")
1245 var a
= new Array[String]
1249 v
.error
(cdi
.n_id
, "Error: no closure named '!{cni}' in {name}; only closures are {a.join(",")}.")
1253 # Check break type conformity
1254 if break_list
!= null then
1255 t
= v
.check_conform_multiexpr
(t
, break_list
)
1258 else if arity
!= 0 then
1259 v
.error
(self, "Error: {name} does not require blocks.")
1265 redef class AAbsSendExpr
1266 # Compute the called global property
1267 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])
1269 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
1270 if prop
== null then return
1271 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
1272 if not process_signature
(v
, sig
, prop
.name
, raw_args
) then return
1273 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
1274 if rtype
== null and sig
.return_type
!= null then return
1276 _prop_signature
= sig
1277 _return_type
= rtype
1280 private fun get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): nullable MMMethod
1282 if type_recv
isa MMTypeNone then
1283 if name
== (once
"==".to_symbol
) or name
== (once
"!=".to_symbol
) then
1284 # Special case on != and == that are allowed for 'null'
1285 type_recv
= v
.type_object
.as_nullable
1287 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1291 var lc
= type_recv
.local_class
1292 var prop
: nullable MMMethod = null
1293 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
1294 if prop
== null and v
.local_property
.global
.is_init
then
1295 var props
= lc
.super_methods_named
(name
)
1296 if props
.length
> 1 then
1297 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1299 else if props
.length
== 1 then
1300 var p
= lc
[props
.first
.global
]
1301 assert p
isa MMMethod
1306 if prop
== null then
1307 if is_implicit_self
then
1308 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1310 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1317 # Get the signature for a local property and a receiver
1318 private fun get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
1320 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
1321 var psig
= prop
.signature_for
(type_recv
)
1322 if not recv_is_self
then psig
= psig
.not_for_self
1326 # The invoked method (once computed)
1327 redef fun prop
do return _prop
.as(not null)
1328 var _prop
: nullable MMMethod
1330 # The return type (if any) (once computed)
1331 redef readable var _return_type
: nullable MMType
1334 # A possible call of constructor in a super class
1335 # Could be an explicit call or with the 'super' keyword
1336 redef class ASuperInitCall
1337 private fun register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1339 if parent
!= v
.top_block
and self != v
.top_block
then
1340 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1342 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
1343 var prev_class
: nullable MMLocalClass = null
1344 var esic
= v
.explicit_super_init_calls
.as(not null)
1345 if not esic
.is_empty
then
1346 prev_class
= esic
.last
.global
.intro
.local_class
1348 var order
= v
.local_class
.cshe
.reverse_linear_extension
1349 if cla
== v
.local_class
then
1350 v
.explicit_other_init_call
= true
1351 else if not order
.has
(cla
) then
1352 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1353 else if cla
== prev_class
then
1354 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1357 if c
== prev_class
then
1359 else if c
== cla
then
1360 if prev_class
!= null then
1361 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1372 redef class ANewExpr
1373 redef fun compute_raw_arguments
do return n_args
.to_a
1374 redef fun after_typing
(v
)
1376 if not n_type
.is_typed
then return
1377 var t
= n_type
.stype
1378 if t
.local_class
.global
.is_abstract
then
1379 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1383 if n_id
== null then
1384 name
= once
"init".to_symbol
1386 name
= n_id
.to_symbol
1389 do_typing
(v
, t
, false, false, name
, raw_arguments
, null)
1390 if _prop
== null then return
1392 if not prop
.global
.is_init
then
1393 v
.error
(self, "Error: {prop} is not a constructor.")
1402 redef class ASendExpr
1403 # Name of the invoked property
1404 fun name
: Symbol is abstract
1406 # Closure definitions
1407 redef fun closure_defs
: nullable Array[AClosureDef] do return null
1409 redef fun after_typing
(v
)
1414 private fun do_all_typing
(v
: TypingVisitor)
1416 if not v
.check_expr
(n_expr
) then return
1417 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1418 if _prop
== null then return
1419 var prop
= _prop
.as(not null)
1421 if prop
.global
.is_init
then
1422 if not v
.local_property
.global
.is_init
then
1423 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1424 else if not n_expr
.is_self
then
1425 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1427 register_super_init_call
(v
, prop
)
1431 _stype
= return_type
1436 redef class ASendReassignExpr
1437 redef fun read_prop
do return _read_prop
.as(not null)
1438 var _read_prop
: nullable MMMethod
1439 redef fun do_all_typing
(v
)
1441 if not v
.check_expr
(n_expr
) then return
1442 var raw_args
= raw_arguments
1443 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1445 if prop
== null then return
1446 if prop
.global
.is_init
then
1447 if not v
.local_property
.global
.is_init
then
1448 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1449 else if not n_expr
.is_self
then
1450 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1453 var t
= prop
.signature_for
(n_expr
.stype
).return_type
.as(not null)
1454 if not n_expr
.is_self
then t
= t
.not_for_self
1456 var t2
= do_rvalue_typing
(v
, t
)
1457 if t2
== null then return
1458 v
.check_conform
(self, t2
, n_value
.stype
)
1461 raw_args
= raw_args
.to_a
1462 raw_args
.add
(n_value
)
1464 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1465 if prop
.global
.is_init
then
1466 if not v
.local_property
.global
.is_init
then
1467 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1468 else if not n_expr
.is_self
then
1469 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1477 redef class ABinopExpr
1478 redef fun compute_raw_arguments
do return [n_expr2
]
1481 redef fun name
do return once
"==".to_symbol
1482 redef fun after_typing
(v
)
1485 if not n_expr
.is_typed
or not n_expr2
.is_typed
then return
1486 if n_expr
.stype
isa MMTypeNone and not n_expr2
.stype
.is_nullable
or
1487 n_expr2
.stype
isa MMTypeNone and not n_expr
.stype
.is_nullable
then
1488 v
.warning
(self, "Warning: comparaison between null and a non nullable value.")
1491 if n_expr
.stype
isa MMTypeNone then
1492 if n_expr2
.stype
isa MMTypeNone then
1493 v
.warning
(self, "Warning: comparaison between 2 null values.")
1495 try_to_isa
(v
, n_expr2
)
1497 else if n_expr2
.stype
isa MMTypeNone then
1498 try_to_isa
(v
, n_expr
)
1502 private fun try_to_isa
(v
: TypingVisitor, n
: AExpr)
1504 var variable
= n
.its_variable
1505 if variable
!= null and n
.stype
isa MMNullableType then
1506 _if_false_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, n
.stype
.as_notnull
)
1507 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, v
.type_none
)
1512 redef fun name
do return once
"!=".to_symbol
1513 redef fun after_typing
(v
)
1516 if not n_expr
.is_typed
or not n_expr2
.is_typed
then return
1517 if n_expr
.stype
isa MMTypeNone and not n_expr2
.stype
.is_nullable
or
1518 n_expr2
.stype
isa MMTypeNone and not n_expr
.stype
.is_nullable
then
1519 v
.warning
(self, "Warning: comparaison between null and a non nullable value.")
1522 if n_expr
.stype
isa MMTypeNone then
1523 if n_expr2
.stype
isa MMTypeNone then
1524 v
.warning
(self, "Warning: comparaison between 2 null values.")
1526 try_to_isa
(v
, n_expr2
)
1528 else if n_expr2
.stype
isa MMTypeNone then
1529 try_to_isa
(v
, n_expr
)
1533 private fun try_to_isa
(v
: TypingVisitor, n
: AExpr)
1535 var variable
= n
.its_variable
1536 if variable
!= null and n
.stype
isa MMNullableType then
1537 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, n
.stype
.as_notnull
)
1538 _if_false_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, v
.type_none
)
1543 redef fun name
do return once
"<".to_symbol
1546 redef fun name
do return once
"<=".to_symbol
1549 redef fun name
do return once
">".to_symbol
1552 redef fun name
do return once
">=".to_symbol
1554 redef class APlusExpr
1555 redef fun name
do return once
"+".to_symbol
1557 redef class AMinusExpr
1558 redef fun name
do return once
"-".to_symbol
1560 redef class AStarshipExpr
1561 redef fun name
do return once
"<=>".to_symbol
1563 redef class AStarExpr
1564 redef fun name
do return once
"*".to_symbol
1566 redef class ASlashExpr
1567 redef fun name
do return once
"/".to_symbol
1569 redef class APercentExpr
1570 redef fun name
do return once
"%".to_symbol
1573 redef class AUminusExpr
1574 redef fun name
do return once
"unary -".to_symbol
1575 redef fun compute_raw_arguments
do return null
1578 redef class ACallFormExpr
1579 redef fun after_typing
(v
)
1581 if n_expr
.is_implicit_self
then
1582 var name
= n_id
.to_symbol
1583 var variable
= v
.variable_ctx
[name
]
1584 if variable
!= null then
1586 if variable
isa ClosureVariable then
1587 n
= new AClosureCallExpr.init_aclosurecallexpr
(n_id
, n_args
, n_closure_defs
)
1588 n
._variable
= variable
1590 if not n_args
.is_empty
then
1591 v
.error
(self, "Error: {name} is variable, not a function.")
1594 n
= variable_create
(variable
)
1595 n
._variable
= variable
1606 redef fun closure_defs
1608 if n_closure_defs
.is_empty
then
1611 return n_closure_defs
.to_a
1615 # Create a variable acces corresponding to the call form
1616 fun variable_create
(variable
: Variable): AVarFormExpr is abstract
1619 redef class ACallExpr
1620 redef fun variable_create
(variable
)
1622 return new AVarExpr.init_avarexpr
(n_id
)
1625 redef fun name
do return n_id
.to_symbol
1626 redef fun compute_raw_arguments
do return n_args
.to_a
1629 redef class ACallAssignExpr
1630 redef fun variable_create
(variable
)
1632 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1635 redef fun name
do return (n_id
.text
+ "=").to_symbol
1636 redef fun compute_raw_arguments
do
1637 var res
= n_args
.to_a
1643 redef class ACallReassignExpr
1644 redef fun variable_create
(variable
)
1646 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1649 redef fun name
do return n_id
.to_symbol
1650 redef fun compute_raw_arguments
do return n_args
.to_a
1653 redef class ABraExpr
1654 redef fun name
do return once
"[]".to_symbol
1655 redef fun compute_raw_arguments
do return n_args
.to_a
1656 redef fun closure_defs
1658 if n_closure_defs
.is_empty
then
1661 return n_closure_defs
.to_a
1666 redef class ABraAssignExpr
1667 redef fun name
do return once
"[]=".to_symbol
1668 redef fun compute_raw_arguments
do
1669 var res
= n_args
.to_a
1675 redef class ABraReassignExpr
1676 redef fun name
do return once
"[]".to_symbol
1677 redef fun compute_raw_arguments
do return n_args
.to_a
1680 redef class AInitExpr
1681 redef fun name
do return once
"init".to_symbol
1682 redef fun compute_raw_arguments
do return n_args
.to_a
1685 redef class AClosureCallExpr
1686 var _variable
: nullable ClosureVariable
1687 redef fun variable
do return _variable
.as(not null)
1688 redef fun compute_raw_arguments
do return n_args
.to_a
1690 redef fun after_typing
(v
)
1693 if va
.closure
.is_break
then v
.variable_ctx
.unreash
= true
1694 var sig
= va
.closure
.signature
1695 var s
= process_signature
(v
, sig
, n_id
.to_symbol
, compute_raw_arguments
)
1696 if not n_closure_defs
.is_empty
then
1697 process_closures
(v
, sig
, n_id
.to_symbol
, n_closure_defs
.to_a
)
1699 if not s
then return
1700 _prop_signature
= sig
1701 _stype
= sig
.return_type
1706 redef class AClosureId
1707 fun to_symbol
: Symbol is abstract
1709 redef class ASimpleClosureId
1710 redef fun to_symbol
: Symbol do return n_id
.to_symbol
1712 redef class ABreakClosureId
1713 redef fun to_symbol
: Symbol do return n_kwbreak
.to_symbol
1716 redef class AClosureDef
1717 var _closure
: nullable MMClosure
1718 redef fun closure
do return _closure
.as(not null)
1720 # The corresponding escapable object
1721 readable var _escapable
: nullable EscapableBlock
1723 var _accept_typing2
: Bool = false
1724 redef fun accept_typing
(v
)
1726 # Typing is deferred, wait accept_typing2(v)
1727 if _accept_typing2
then super
1730 private fun accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure)
1734 var sig
= esc
.closure
.signature
1735 if sig
.arity
!= n_ids
.length
then
1736 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_ids.length} found.")
1740 _closure
= esc
.closure
1742 var old_var_ctx
= v
.variable_ctx
1743 var old_base_var_ctx
= v
.base_variable_ctx
1744 v
.base_variable_ctx
= v
.variable_ctx
1745 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
1746 variables
= new Array[AutoVariable]
1747 for i
in [0..n_ids
.length
[ do
1748 var va
= new AutoVariable(n_ids
[i
].to_symbol
, n_ids
[i
])
1751 v
.variable_ctx
.add
(va
)
1754 _accept_typing2
= true
1757 if v
.variable_ctx
.unreash
== false then
1758 if closure
.signature
.return_type
!= null then
1759 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
1760 else if closure
.is_break
and esc
.break_list
!= null then
1761 v
.error
(self, "Control error: Reached end of break block (a 'break' with a value was expected).")
1764 v
.variable_ctx
= old_var_ctx
1765 v
.base_variable_ctx
= old_base_var_ctx
1769 class ATypeCheckExpr
1771 private fun check_expr_cast
(v
: TypingVisitor, n_expr
: AExpr, n_type
: AType)
1773 if not v
.check_expr
(n_expr
) then return
1774 if not n_type
.is_typed
then return
1775 var etype
= n_expr
.stype
1776 var ttype
= n_type
.stype
1777 if etype
== ttype
then
1778 v
.warning
(self, "Warning: Expression is already a {ttype}.")
1779 else if etype
< ttype
then
1780 v
.warning
(self, "Warning: Expression is already a {ttype} since it is a {etype}.")
1781 else if etype
isa MMTypeNone then
1782 # ttype is not nullable because of prevous test
1783 v
.warning
(self, "Warning: Expression is null therefore cannot be a {ttype}.")
1784 else if etype
.is_nullable
and etype
.as_notnull
== ttype
then
1785 if ttype
isa MMTypeFormal and ttype
.bound
.is_nullable
then
1786 # No warning in this case since with
1787 # type T: nullable A
1789 # 'x.as(not null)' != 'x.as(T)'
1790 # 'x != null' != 'x isa T'
1791 else if self isa AIsaExpr then
1792 v
.warning
(self, "Warning: Prefer '!= null'.")
1794 v
.warning
(self, "Warning: Prefer '.as(not null)'.")
1800 redef class AIsaExpr
1801 special ATypeCheckExpr
1802 redef fun after_typing
(v
)
1804 check_expr_cast
(v
, n_expr
, n_type
)
1805 if not n_type
.is_typed
then return
1806 var variable
= n_expr
.its_variable
1807 if variable
!= null then
1808 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, n_type
.stype
)
1810 _stype
= v
.type_bool
1815 redef class AAsCastExpr
1816 special ATypeCheckExpr
1817 redef fun after_typing
(v
)
1819 check_expr_cast
(v
, n_expr
, n_type
)
1820 if not n_type
.is_typed
then return
1821 _stype
= n_type
.stype
1822 _is_typed
= _stype
!= null
1826 redef class AAsNotnullExpr
1827 redef fun after_typing
(v
)
1829 if not v
.check_expr
(n_expr
) then return
1830 var t
= n_expr
.stype
1831 if t
isa MMTypeNone then
1832 v
.error
(n_expr
, "Type error: 'as(not null)' on 'null' value.")
1834 else if not t
.is_nullable
then
1835 v
.warning
(n_expr
, "Warning: 'as(not null)' on non nullable type.")
1837 _stype
= n_expr
.stype
.as_notnull
1842 redef class AProxyExpr
1843 redef fun after_typing
(v
)
1845 if not n_expr
.is_typed
then return
1847 if n_expr
.is_statement
then return
1848 _stype
= n_expr
.stype
1852 redef class AOnceExpr
1853 redef fun accept_typing
(v
)
1855 if v
.once_count
> 0 then
1856 v
.warning
(self, "Useless once in a once expression.")
1858 v
.once_count
= v
.once_count
+ 1
1862 v
.once_count
= v
.once_count
- 1