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 meth 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 readable writable attr _variable_ctx
: VariableContext
49 # Current knowledge about escapable blocks
50 readable writable attr _escapable_ctx
: EscapableContext = new EscapableContext(self)
52 # The current reciever
53 readable writable attr _self_var
: ParamVariable
55 # Block of the current method
56 readable writable attr _top_block
: PExpr
58 # List of explicit invocation of constructors of super-classes
59 readable writable attr _explicit_super_init_calls
: Array[MMMethod]
61 # Is a other constructor of the same class invoked
62 readable writable attr _explicit_other_init_call
: Bool
64 # Make the if_true_variable_ctx of the expression effective
65 private meth use_if_true_variable_ctx
(e
: PExpr)
67 var ctx
= e
.if_true_variable_ctx
73 # Number of nested once
74 readable writable attr _once_count
: Int = 0
76 init(tc
, module) do super
78 private meth get_default_constructor_for
(n
: PNode, c
: MMLocalClass, prop
: MMSrcMethod): MMMethod
81 #var prop = v.local_property
82 #assert prop isa MMMethod
83 var candidates
= new Array[MMMethod]
84 var false_candidates
= new Array[MMMethod]
85 var parity
= prop
.signature
.arity
86 for g
in c
.global_properties
do
87 if not g
.is_init_for
(c
) then continue
89 var gps
= gp
.signature_for
(c
.get_type
)
90 assert gp
isa MMSrcMethod
91 var garity
= gps
.arity
92 if prop
!= null and gp
.name
== prop
.name
then
93 if garity
== 0 or (parity
== garity
and prop
.signature
< gps
) then
96 false_candidates
.add
(gp
)
98 else if garity
== 0 and gp
.name
== once
("init".to_symbol
) then
100 false_candidates
.add
(gp
)
102 false_candidates
.add
(gp
)
105 if candidates
.length
== 1 then
106 return candidates
.first
107 else if candidates
.length
> 0 then
108 var a
= new Array[String]
109 for p
in candidates
do
110 a
.add
("{p.full_name}{p.signature}")
112 v
.error
(n
, "Error: Conflicting default constructor to call for {c}: {a.join(", ")}.")
114 else if false_candidates
.length
> 0 then
115 var a
= new Array[String]
116 for p
in false_candidates
do
117 a
.add
("{p.full_name}{p.signature}")
119 v
.error
(n
, "Error: there is no available compatible constrctor in {c}. Discarded candidates are {a.join(", ")}.")
122 v
.error
(n
, "Error: there is no available compatible constrctor in {c}.")
129 ###############################################################################
132 private meth accept_typing
(v
: TypingVisitor)
134 accept_abs_syntax_visitor
(v
)
137 private meth after_typing
(v
: TypingVisitor) do end
140 redef class PClassdef
141 redef meth accept_typing
(v
)
143 v
.self_var
= new ParamVariable("self".to_symbol
, self)
144 v
.self_var
.stype
= local_class
.get_type
149 redef class AAttrPropdef
150 redef meth accept_typing
(v
)
153 if n_expr
!= null then
154 v
.check_conform_expr
(n_expr
, prop
.signature
.return_type
)
159 redef class AMethPropdef
160 redef readable attr _self_var
: ParamVariable
161 redef meth accept_typing
(v
)
163 v
.variable_ctx
= new RootVariableContext(v
, self)
164 _self_var
= v
.self_var
169 redef class AConcreteMethPropdef
170 redef meth accept_typing
(v
)
173 if v
.variable_ctx
.unreash
== false and method
.signature
.return_type
!= null then
174 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
179 redef class AConcreteInitPropdef
180 readable attr _super_init_calls
: Array[MMMethod] = new Array[MMMethod]
181 readable attr _explicit_super_init_calls
: Array[MMMethod] = new Array[MMMethod]
182 redef meth accept_typing
(v
)
184 v
.top_block
= n_block
185 v
.explicit_super_init_calls
= explicit_super_init_calls
186 v
.explicit_other_init_call
= false
188 if v
.explicit_other_init_call
or method
.global
.intro
!= method
then
192 var l
= explicit_super_init_calls
.length
193 var cur_m
: MMMethod = null
194 var cur_c
: MMLocalClass = null
196 cur_m
= explicit_super_init_calls
[i
]
197 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
200 while j
< v
.local_class
.cshe
.direct_greaters
.length
do
201 var c
= v
.local_class
.cshe
.direct_greaters
[j
]
202 if c
.global
.is_interface
or c
.global
.is_universal
or c
.global
.is_mixin
then
204 else if cur_c
!= null and (c
.cshe
<= cur_c
or cur_c
.global
.is_mixin
) then
205 if c
== cur_c
then j
+= 1
206 super_init_calls
.add
(cur_m
)
209 cur_m
= explicit_super_init_calls
[i
]
210 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
216 var p
= v
.get_default_constructor_for
(self, c
, method
)
218 super_init_calls
.add
(p
)
228 redef meth after_typing
(v
)
230 # TODO: why the test?
231 if v
.variable_ctx
!= null then
232 v
.variable_ctx
.add
(variable
)
237 redef class AClosureDecl
238 # The corresponding escapable object
239 readable attr _escapable
: EscapableBlock
241 redef meth accept_typing
(v
)
243 # Register the closure for ClosureCallExpr
244 v
.variable_ctx
.add
(variable
)
246 var old_var_ctx
= v
.variable_ctx
247 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
249 _escapable
= new EscapableClosure(self, variable
.closure
, null)
250 v
.escapable_ctx
.push
(_escapable
)
254 if n_expr
!= null then
255 if v
.variable_ctx
.unreash
== false then
256 if variable
.closure
.signature
.return_type
!= null then
257 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
258 else if variable
.closure
.is_break
then
259 v
.error
(self, "Control error: Reached end of break block (an 'abort' was expected).")
264 old_var_ctx
.merge
(v
.variable_ctx
)
265 v
.variable_ctx
= old_var_ctx
271 readable attr _stype
: MMType
272 redef meth after_typing
(v
)
274 _stype
= get_stype
(v
)
279 redef readable attr _is_typed
: Bool = false
280 redef meth is_statement
: Bool do return _stype
== null
284 print
"{locate}: not is_typed"
288 print
"{locate}: is_statement"
295 # Is the expression the implicit receiver
296 meth is_implicit_self
: Bool do return false
298 # Is the expression the current receiver (implicit or explicit)
299 meth is_self
: Bool do return false
301 # The variable accessed is any
302 meth its_variable
: Variable do return null
304 # The variable type information if current boolean expression is true
305 readable private attr _if_true_variable_ctx
: VariableContext
308 redef class AVardeclExpr
309 redef meth after_typing
(v
)
311 var va
= new VarVariable(n_id
.to_symbol
, self)
313 v
.variable_ctx
.add
(va
)
314 if n_expr
!= null then v
.variable_ctx
.mark_is_set
(va
)
316 if n_type
!= null then
317 va
.stype
= n_type
.stype
318 if n_expr
!= null then
319 v
.check_conform_expr
(n_expr
, va
.stype
)
322 if not v
.check_expr
(n_expr
) then return
323 va
.stype
= n_expr
.stype
329 redef class ABlockExpr
330 redef meth accept_typing
(v
)
332 var old_var_ctx
= v
.variable_ctx
333 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
336 if v
.variable_ctx
.unreash
and not v
.variable_ctx
.already_unreash
then
337 v
.variable_ctx
.already_unreash
= true
338 v
.warning
(e
, "Warning: unreachable statement.")
343 old_var_ctx
.merge
(v
.variable_ctx
)
344 v
.variable_ctx
= old_var_ctx
349 redef class AReturnExpr
350 redef meth after_typing
(v
)
352 v
.variable_ctx
.unreash
= true
353 var t
= v
.local_property
.signature
.return_type
354 if n_expr
== null and t
!= null then
355 v
.error
(self, "Error: Return without value in a function.")
356 else if n_expr
!= null and t
== null then
357 v
.error
(self, "Error: Return with value in a procedure.")
358 else if n_expr
!= null and t
!= null then
359 v
.check_conform_expr
(n_expr
, t
)
365 redef class AContinueExpr
366 redef meth after_typing
(v
)
368 v
.variable_ctx
.unreash
= true
369 var esc
= compute_escapable_block
(v
.escapable_ctx
)
370 if esc
== null then return
372 if esc
.is_break_block
then
373 v
.error
(self, "Error: 'continue' forbiden in break blocks.")
377 var t
= esc
.continue_stype
378 if n_expr
== null and t
!= null then
379 v
.error
(self, "Error: continue with a value required in this block.")
380 else if n_expr
!= null and t
== null then
381 v
.error
(self, "Error: continue without value required in this block.")
382 else if n_expr
!= null and t
!= null then
383 v
.check_conform_expr
(n_expr
, t
)
389 redef class ABreakExpr
390 redef meth after_typing
(v
)
392 v
.variable_ctx
.unreash
= true
393 var esc
= compute_escapable_block
(v
.escapable_ctx
)
394 if esc
== null then return
396 var bl
= esc
.break_list
397 if n_expr
== null and bl
!= null then
398 v
.error
(self, "Error: break with a value required in this block.")
399 else if n_expr
!= null and bl
== null then
400 v
.error
(self, "Error: break without value required in this block.")
401 else if n_expr
!= null and bl
!= null then
402 # Typing check can only be done later
409 redef class AAbortExpr
410 redef meth after_typing
(v
)
412 v
.variable_ctx
.unreash
= true
417 redef meth accept_typing
(v
)
419 var old_var_ctx
= v
.variable_ctx
421 v
.check_conform_expr
(n_expr
, v
.type_bool
)
423 v
.use_if_true_variable_ctx
(n_expr
)
424 v
.variable_ctx
= v
.variable_ctx
.sub
(n_then
)
428 if n_else
== null then
429 # Restore variable ctx since the 'then' block is optional
430 v
.variable_ctx
= old_var_ctx
432 # Remember what appened in the 'then'
433 var then_var_ctx
= v
.variable_ctx
434 # Reset to process the 'else'
435 v
.variable_ctx
= old_var_ctx
.sub
(n_else
)
437 # Merge then and else in the old control_flow
438 old_var_ctx
.merge2
(then_var_ctx
, v
.variable_ctx
)
439 v
.variable_ctx
= old_var_ctx
445 redef class AWhileExpr
446 # The corresponding escapable block
447 readable attr _escapable
: EscapableBlock
449 redef meth accept_typing
(v
)
451 _escapable
= new EscapableBlock(self)
452 v
.escapable_ctx
.push
(_escapable
)
453 var old_var_ctx
= v
.variable_ctx
454 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
458 v
.check_conform_expr
(n_expr
, v
.type_bool
)
459 v
.variable_ctx
= old_var_ctx
466 # The corresponding escapable block
467 readable attr _escapable
: EscapableBlock
469 readable attr _meth_iterator
: MMMethod
470 readable attr _meth_is_ok
: MMMethod
471 readable attr _meth_item
: MMMethod
472 readable attr _meth_next
: MMMethod
473 redef meth accept_typing
(v
)
475 _escapable
= new EscapableBlock(self)
476 v
.escapable_ctx
.push
(_escapable
)
478 var old_var_ctx
= v
.variable_ctx
479 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
480 var va
= new AutoVariable(n_id
.to_symbol
, self)
482 v
.variable_ctx
.add
(va
)
486 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then return
487 var expr_type
= n_expr
.stype
488 _meth_iterator
= expr_type
.local_class
.select_method
(once
("iterator".to_symbol
))
489 if _meth_iterator
== null then
490 v
.error
(self, "Error: Collection MUST have an iterate method")
493 var iter_type
= _meth_iterator
.signature_for
(expr_type
).return_type
494 _meth_is_ok
= iter_type
.local_class
.select_method
(once
("is_ok".to_symbol
))
495 if _meth_is_ok
== null then
496 v
.error
(self, "Error: {iter_type} MUST have an is_ok method")
499 _meth_item
= iter_type
.local_class
.select_method
(once
("item".to_symbol
))
500 if _meth_item
== null then
501 v
.error
(self, "Error: {iter_type} MUST have an item method")
504 _meth_next
= iter_type
.local_class
.select_method
(once
("next".to_symbol
))
505 if _meth_next
== null then
506 v
.error
(self, "Error: {iter_type} MUST have a next method")
509 var t
= _meth_item
.signature_for
(iter_type
).return_type
510 if not n_expr
.is_self
then t
= t
.not_for_self
513 if n_block
!= null then v
.visit
(n_block
)
516 v
.variable_ctx
= old_var_ctx
522 redef class AAssertExpr
523 redef meth after_typing
(v
)
525 v
.check_conform_expr
(n_expr
, v
.type_bool
)
526 v
.use_if_true_variable_ctx
(n_expr
)
532 redef meth its_variable
do return variable
534 redef meth after_typing
(v
)
536 v
.variable_ctx
.check_is_set
(self, variable
)
537 _stype
= v
.variable_ctx
.stype
(variable
)
538 _is_typed
= _stype
!= null
542 redef class AVarAssignExpr
543 redef meth after_typing
(v
)
545 v
.variable_ctx
.mark_is_set
(variable
)
546 var t
= v
.variable_ctx
.stype
(variable
)
547 v
.check_conform_expr
(n_value
, t
)
552 redef class AReassignFormExpr
553 # Compute and check method used through the reassigment operator
554 # On success return the static type of the result of the reassigment operator
555 # Else display an error and return null
556 private meth do_rvalue_typing
(v
: TypingVisitor, type_lvalue
: MMType): MMType
558 if type_lvalue
== null then
561 var name
= n_assign_op
.method_name
562 var lc
= type_lvalue
.local_class
563 if not lc
.has_global_property_by_name
(name
) then
564 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
567 var prop
= lc
.select_method
(name
)
568 prop
.global
.check_visibility
(v
, self, v
.module, false)
569 var psig
= prop
.signature_for
(type_lvalue
)
570 _assign_method
= prop
571 if not v
.check_conform_expr
(n_value
, psig
[0].not_for_self
) then return null
572 return psig
.return_type
.not_for_self
575 # Method used through the reassigment operator (once computed)
576 readable attr _assign_method
: MMMethod
579 redef class AVarReassignExpr
580 redef meth after_typing
(v
)
582 v
.variable_ctx
.check_is_set
(self, variable
)
583 v
.variable_ctx
.mark_is_set
(variable
)
584 var t
= v
.variable_ctx
.stype
(variable
)
585 var t2
= do_rvalue_typing
(v
, t
)
586 if t2
== null then return
587 v
.check_conform
(self, t2
, n_value
.stype
)
592 redef class PAssignOp
593 meth method_name
: Symbol is abstract
595 redef class APlusAssignOp
596 redef meth method_name
do return once
"+".to_symbol
598 redef class AMinusAssignOp
599 redef meth method_name
do return once
"-".to_symbol
602 redef class ASelfExpr
603 redef meth its_variable
do return variable
605 redef meth after_typing
(v
)
607 variable
= v
.self_var
608 _stype
= v
.variable_ctx
.stype
(variable
)
612 redef meth is_self
do return true
615 redef class AImplicitSelfExpr
616 redef meth is_implicit_self
do return true
619 redef class AIfexprExpr
620 redef meth accept_typing
(v
)
622 var old_var_ctx
= v
.variable_ctx
625 v
.use_if_true_variable_ctx
(n_expr
)
627 v
.variable_ctx
= old_var_ctx
630 v
.check_conform_expr
(n_expr
, v
.type_bool
)
632 _stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
633 _is_typed
= _stype
!= null
637 redef class ABoolExpr
638 redef meth after_typing
(v
)
646 redef meth after_typing
(v
)
648 v
.check_conform_expr
(n_expr
, v
.type_bool
)
649 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
656 redef meth accept_typing
(v
)
658 var old_var_ctx
= v
.variable_ctx
661 v
.use_if_true_variable_ctx
(n_expr
)
664 if n_expr2
.if_true_variable_ctx
!= null then
665 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
667 _if_true_variable_ctx
= v
.variable_ctx
670 v
.variable_ctx
= old_var_ctx
672 v
.check_conform_expr
(n_expr
, v
.type_bool
)
673 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
680 redef meth after_typing
(v
)
682 v
.check_conform_expr
(n_expr
, v
.type_bool
)
689 redef meth after_typing
(v
)
696 redef class AFloatExpr
697 redef meth after_typing
(v
)
699 _stype
= v
.type_float
704 redef class ACharExpr
705 redef meth after_typing
(v
)
712 redef class AStringFormExpr
713 readable attr _meth_with_native
: MMMethod
714 redef meth after_typing
(v
)
716 _stype
= v
.type_string
718 _meth_with_native
= _stype
.local_class
.select_method
(once
"with_native".to_symbol
)
719 if _meth_with_native
== null then v
.error
(self, "{_stype} MUST have a with_native method.")
723 redef class ASuperstringExpr
724 readable attr _meth_with_capacity
: MMMethod
725 readable attr _meth_add
: MMMethod
726 readable attr _meth_to_s
: MMMethod
727 readable attr _atype
: MMType
728 redef meth after_typing
(v
)
730 _stype
= v
.type_string
731 _atype
= v
.type_array
(_stype
)
732 _meth_with_capacity
= _atype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
733 if _meth_with_capacity
== null then v
.error
(self, "{_atype} MUST have a with_capacity method.")
734 _meth_add
= _atype
.local_class
.select_method
(once
"add".to_symbol
)
735 if _meth_add
== null then v
.error
(self, "{_atype} MUST have an add method.")
736 _meth_to_s
= v
.type_object
.local_class
.select_method
(once
"to_s".to_symbol
)
737 if _meth_to_s
== null then v
.error
(self, "Object MUST have a to_s method.")
742 redef class ANullExpr
743 redef meth after_typing
(v
)
750 redef class AArrayExpr
751 readable attr _meth_with_capacity
: MMMethod
752 readable attr _meth_add
: MMMethod
754 redef meth after_typing
(v
)
756 var stype
= v
.check_conform_multiexpr
(null, n_exprs
)
757 if stype
== null then return
761 private meth do_typing
(v
: TypingVisitor, element_type
: MMType)
763 _stype
= v
.type_array
(element_type
)
765 _meth_with_capacity
= _stype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
766 if _meth_with_capacity
== null then v
.error
(self, "{_stype} MUST have a with_capacity method.")
767 _meth_add
= _stype
.local_class
.select_method
(once
"add".to_symbol
)
768 if _meth_add
== null then v
.error
(self, "{_stype} MUST have an add method.")
774 redef class ARangeExpr
775 readable attr _meth_init
: MMMethod
776 redef meth after_typing
(v
)
778 if not v
.check_expr
(n_expr
) or not v
.check_expr
(n_expr2
) then return
779 var ntype
= n_expr
.stype
780 var ntype2
= n_expr2
.stype
781 if ntype
< ntype2
then
783 else if not ntype2
< ntype
then
784 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
787 var dtype
= v
.type_discrete
788 if not v
.check_conform_expr
(n_expr
, dtype
) or not v
.check_conform_expr
(n_expr2
, dtype
) then return
789 _stype
= v
.type_range
(ntype
)
794 redef class ACrangeExpr
795 redef meth after_typing
(v
)
798 _meth_init
= stype
.local_class
.select_method
(once
"init".to_symbol
)
801 redef class AOrangeExpr
802 redef meth after_typing
(v
)
805 _meth_init
= stype
.local_class
.select_method
(once
"without_last".to_symbol
)
810 redef class ASuperExpr
811 special ASuperInitCall
812 # readable attr _prop: MMSrcMethod
813 readable attr _init_in_superclass
: MMMethod
814 redef meth after_typing
(v
)
816 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
817 if not precs
.is_empty
then
818 v
.local_property
.need_super
= true
819 else if v
.local_property
.global
.is_init
then
820 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
821 for p
in base_precs
do
822 if not p
.global
.is_init
then
823 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
825 precs
.add
(v
.local_class
[p
.global
])
828 if precs
.is_empty
then
829 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
831 else if precs
.length
> 1 then
832 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
835 var p
= base_precs
.first
836 assert p
isa MMMethod
837 _init_in_superclass
= p
838 register_super_init_call
(v
, p
)
839 if n_args
.length
> 0 then
840 var signature
= get_signature
(v
, v
.self_var
.stype
, p
, true)
841 _arguments
= process_signature
(v
, signature
, p
.name
, n_args
.to_a
)
844 v
.error
(self, "Error: No super method to call for {v.local_property}.")
848 if precs
.first
.signature_for
(v
.self_var
.stype
).return_type
!= null then
849 var stypes
= new Array[MMType]
850 var stype
: MMType = null
852 assert prop
isa MMMethod
853 var t
= prop
.signature_for
(v
.self_var
.stype
).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
855 if stype
== null or stype
< t
then
860 v
.check_conform
(self, t
, stype
)
864 var p
= v
.local_property
865 assert p
isa MMSrcMethod
871 redef class AAttrFormExpr
873 readable attr _prop
: MMAttribute
875 # Attribute type of the acceded attribute
876 readable attr _attr_type
: MMType
878 # Compute the attribute accessed
879 private meth do_typing
(v
: TypingVisitor)
881 if not v
.check_expr
(n_expr
) then return
882 var type_recv
= n_expr
.stype
883 var name
= n_id
.to_symbol
884 var lc
= type_recv
.local_class
885 if not lc
.has_global_property_by_name
(name
) then
886 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
889 var prop
= lc
.select_attribute
(name
)
890 if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
891 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
894 var at
= prop
.signature_for
(type_recv
).return_type
895 if not n_expr
.is_self
then at
= at
.not_for_self
900 redef class AAttrExpr
901 redef meth after_typing
(v
)
904 if prop
== null then return
910 redef class AAttrAssignExpr
911 redef meth after_typing
(v
)
914 if prop
== null then return
915 if not v
.check_conform_expr
(n_value
, attr_type
) then return
920 redef class AAttrReassignExpr
921 redef meth after_typing
(v
)
924 if prop
== null then return
925 var t
= do_rvalue_typing
(v
, attr_type
)
926 if t
== null then return
927 v
.check_conform
(self, t
, n_value
.stype
)
932 class AAbsAbsSendExpr
934 # The signature of the called property
935 readable attr _prop_signature
: MMSignature
937 # The real arguments used (after star transformation) (once computed)
938 readable attr _arguments
: Array[PExpr]
940 # Check the conformity of a set of arguments `raw_args' to a signature.
941 private meth process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: Array[PExpr]): Array[PExpr]
943 var par_vararg
= psig
.vararg_rank
944 var par_arity
= psig
.arity
946 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
947 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
948 v
.error
(self, "Error: '{name}' arity missmatch.")
952 var args
= new Array[PExpr]
953 for par_idx
in [0..par_arity
[ do
955 var par_type
= psig
[par_idx
]
956 if par_idx
== par_vararg
then
957 var star
= new Array[PExpr]
958 for i
in [0..(raw_arity-par_arity
)] do
959 a
= raw_args
[arg_idx
]
960 v
.check_conform_expr
(a
, par_type
)
962 arg_idx
= arg_idx
+ 1
964 var aa
= new AArrayExpr.init_aarrayexpr
(star
)
965 aa
.do_typing
(v
, par_type
)
968 a
= raw_args
[arg_idx
]
969 v
.check_conform_expr
(a
, par_type
)
970 arg_idx
= arg_idx
+ 1
977 # Check the conformity of a set of defined closures
978 private meth process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: Array[PClosureDef]): MMType
980 var t
= psig
.return_type
981 var cs
= psig
.closures
# Declared closures
984 if not c
.is_optional
then min_arity
+= 1
987 if cs
.length
== 0 then
988 v
.error
(self, "Error: {name} does not require blocks.")
989 else if cd
.length
> cs
.length
or cd
.length
< min_arity
then
990 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
992 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
993 var break_list
: Array[ABreakExpr] = null
994 if t
!= null then break_list
= new Array[ABreakExpr]
996 # Process each closure definition
997 for i
in [0..cd
.length
[ do
1000 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1001 v
.escapable_ctx
.push
(esc
)
1002 cdi
.accept_typing2
(v
, esc
)
1006 # Check break type conformity
1007 if break_list
!= null then
1008 t
= v
.check_conform_multiexpr
(t
, break_list
)
1011 else if min_arity
!= 0 then
1012 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1019 special AAbsAbsSendExpr
1020 # Compute the called global property
1021 private meth do_typing
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, recv_is_self
: Bool, name
: Symbol, raw_args
: Array[PExpr], closure_defs
: Array[PClosureDef])
1023 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
1024 if prop
== null then return
1025 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
1026 if sig
== null then return
1027 var args
= process_signature
(v
, sig
, prop
.name
, raw_args
)
1028 if args
== null then return
1029 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
1030 if rtype
== null and sig
.return_type
!= null then return
1032 _prop_signature
= sig
1034 _return_type
= rtype
1037 private meth get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): MMMethod
1039 if type_recv
== null then return null
1040 var lc
= type_recv
.local_class
1041 var prop
: MMMethod = null
1042 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
1043 if prop
== null and v
.local_property
.global
.is_init
then
1044 var props
= type_recv
.local_class
.super_methods_named
(name
)
1045 if props
.length
> 1 then
1046 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1048 else if props
.length
== 1 then
1049 var p
= type_recv
.local_class
[props
.first
.global
]
1050 assert p
isa MMMethod
1055 if prop
== null then
1056 if is_implicit_self
then
1057 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1059 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1066 # Get the signature for a local property and a receiver
1067 private meth get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
1069 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
1070 var psig
= prop
.signature_for
(type_recv
)
1071 if not recv_is_self
then psig
= psig
.not_for_self
1075 # The invoked method (once computed)
1076 readable attr _prop
: MMMethod
1078 # The return type (if any) (once computed)
1079 readable attr _return_type
: MMType
1082 # A possible call of constructor in a super class
1083 # Could be an explicit call or with the 'super' keyword
1084 class ASuperInitCall
1085 special AAbsSendExpr
1086 private meth register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1088 if parent
!= v
.top_block
and self != v
.top_block
then
1089 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1091 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
1092 var prev_class
: MMLocalClass = null
1093 if not v
.explicit_super_init_calls
.is_empty
then
1094 prev_class
= v
.explicit_super_init_calls
.last
.global
.intro
.local_class
1096 var order
= v
.local_class
.cshe
.reverse_linear_extension
1097 if cla
== v
.local_class
then
1098 v
.explicit_other_init_call
= true
1099 else if not order
.has
(cla
) then
1100 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1101 else if cla
== prev_class
then
1102 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1104 var last_is_found
= prev_class
== null
1106 if c
== prev_class
then
1107 last_is_found
= true
1108 else if c
== cla
then
1109 if not last_is_found
then
1110 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1112 v
.explicit_super_init_calls
.add
(property
)
1121 redef class ANewExpr
1122 special AAbsSendExpr
1123 redef meth after_typing
(v
)
1125 var t
= n_type
.stype
1126 if t
== null then return
1127 if t
.local_class
.global
.is_abstract
then
1128 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1132 if n_id
== null then
1133 name
= once
"init".to_symbol
1135 name
= n_id
.to_symbol
1138 do_typing
(v
, t
, false, false, name
, n_args
.to_a
, null)
1139 if prop
== null then return
1141 if not prop
.global
.is_init
then
1142 v
.error
(self, "Error: {prop} is not a constructor.")
1151 redef class ASendExpr
1152 special ASuperInitCall
1153 # Name of the invoked property
1154 meth name
: Symbol is abstract
1156 # Raw arguments used (withour star transformation)
1157 meth raw_arguments
: Array[PExpr] is abstract
1159 # Closure definitions
1160 meth closure_defs
: Array[PClosureDef] do return null
1162 redef meth after_typing
(v
)
1167 private meth do_all_typing
(v
: TypingVisitor)
1169 if not v
.check_expr
(n_expr
) then return
1170 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1171 if prop
== null then return
1173 if prop
.global
.is_init
then
1174 if not v
.local_property
.global
.is_init
then
1175 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1176 else if not n_expr
.is_self
then
1177 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1179 register_super_init_call
(v
, prop
)
1183 _stype
= return_type
1188 class ASendReassignExpr
1190 special AReassignFormExpr
1191 readable attr _read_prop
: MMMethod
1192 redef meth do_all_typing
(v
)
1194 if not v
.check_expr
(n_expr
) then return
1195 var raw_args
= raw_arguments
1196 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1197 if prop
== null then return
1198 if prop
.global
.is_init
then
1199 if not v
.local_property
.global
.is_init
then
1200 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1201 else if not n_expr
.is_self
then
1202 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1205 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1206 if not n_expr
.is_self
then t
= t
.not_for_self
1208 var t2
= do_rvalue_typing
(v
, t
)
1209 if t2
== null then return
1210 v
.check_conform
(self, t2
, n_value
.stype
)
1213 var old_args
= arguments
1214 raw_args
.add
(n_value
)
1216 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1217 if prop
== null then return
1218 if prop
.global
.is_init
then
1219 if not v
.local_property
.global
.is_init
then
1220 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1221 else if not n_expr
.is_self
then
1222 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1226 _arguments
= old_args
# FIXME: What if star parameters do not match betwen the two methods?
1231 redef class ABinopExpr
1232 redef meth raw_arguments
do return [n_expr2
]
1235 redef meth name
do return once
"==".to_symbol
1238 redef meth name
do return once
"!=".to_symbol
1241 redef meth name
do return once
"<".to_symbol
1244 redef meth name
do return once
"<=".to_symbol
1247 redef meth name
do return once
">".to_symbol
1250 redef meth name
do return once
">=".to_symbol
1252 redef class APlusExpr
1253 redef meth name
do return once
"+".to_symbol
1255 redef class AMinusExpr
1256 redef meth name
do return once
"-".to_symbol
1258 redef class AStarshipExpr
1259 redef meth name
do return once
"<=>".to_symbol
1261 redef class AStarExpr
1262 redef meth name
do return once
"*".to_symbol
1264 redef class ASlashExpr
1265 redef meth name
do return once
"/".to_symbol
1267 redef class APercentExpr
1268 redef meth name
do return once
"%".to_symbol
1271 redef class AUminusExpr
1272 redef meth name
do return once
"unary -".to_symbol
1273 redef meth raw_arguments
do return null
1276 redef class ACallFormExpr
1277 redef meth after_typing
(v
)
1279 if n_expr
!= null and n_expr
.is_implicit_self
then
1280 var name
= n_id
.to_symbol
1281 var variable
= v
.variable_ctx
[name
]
1282 if variable
!= null then
1283 if variable
isa ClosureVariable then
1284 var n
= new AClosureCallExpr.init_aclosurecallexpr
(n_id
, n_args
, n_closure_defs
)
1286 n
.variable
= variable
1290 if not n_args
.is_empty
then
1291 v
.error
(self, "Error: {name} is variable, not a function.")
1294 var vform
= variable_create
(variable
)
1295 vform
.variable
= variable
1297 vform
.after_typing
(v
)
1306 redef meth closure_defs
1308 if n_closure_defs
== null or n_closure_defs
.is_empty
then
1311 return n_closure_defs
.to_a
1315 # Create a variable acces corresponding to the call form
1316 meth variable_create
(variable
: Variable): AVarFormExpr is abstract
1319 redef class ACallExpr
1320 redef meth variable_create
(variable
)
1322 return new AVarExpr.init_avarexpr
(n_id
)
1325 redef meth name
do return n_id
.to_symbol
1326 redef meth raw_arguments
do return n_args
.to_a
1329 redef class ACallAssignExpr
1330 redef meth variable_create
(variable
)
1332 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1335 redef meth name
do return (n_id
.text
+ "=").to_symbol
1336 redef meth raw_arguments
do
1337 var res
= n_args
.to_a
1343 redef class ACallReassignExpr
1344 special ASendReassignExpr
1345 redef meth variable_create
(variable
)
1347 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1350 redef meth name
do return n_id
.to_symbol
1351 redef meth raw_arguments
do return n_args
.to_a
1354 redef class ABraExpr
1355 redef meth name
do return once
"[]".to_symbol
1356 redef meth raw_arguments
do return n_args
.to_a
1359 redef class ABraAssignExpr
1360 redef meth name
do return once
"[]=".to_symbol
1361 redef meth raw_arguments
do
1362 var res
= n_args
.to_a
1368 redef class ABraReassignExpr
1369 special ASendReassignExpr
1370 redef meth name
do return once
"[]".to_symbol
1371 redef meth raw_arguments
do return n_args
.to_a
1374 redef class AInitExpr
1375 redef meth name
do return once
"init".to_symbol
1376 redef meth raw_arguments
do return n_args
.to_a
1379 redef class AClosureCallExpr
1380 special AAbsAbsSendExpr
1381 redef meth after_typing
(v
)
1384 if va
.closure
.is_break
then v
.variable_ctx
.unreash
= true
1385 var sig
= va
.closure
.signature
1386 var args
= process_signature
(v
, sig
, n_id
.to_symbol
, n_args
.to_a
)
1387 if not n_closure_defs
.is_empty
then
1388 process_closures
(v
, sig
, n_id
.to_symbol
, n_closure_defs
.to_a
)
1390 if args
== null then return
1391 _prop_signature
= sig
1393 _stype
= sig
.return_type
1398 redef class PClosureDef
1399 # The corresponding escapable object
1400 readable attr _escapable
: EscapableBlock
1402 attr _accept_typing2
: Bool
1403 redef meth accept_typing
(v
)
1405 # Typing is deferred, wait accept_typing2(v)
1406 if _accept_typing2
then super
1409 private meth accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure) is abstract
1412 redef class AClosureDef
1413 redef meth accept_typing2
(v
, esc
)
1417 var sig
= esc
.closure
.signature
1418 if sig
.arity
!= n_id
.length
then
1419 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1423 closure
= esc
.closure
1425 var old_var_ctx
= v
.variable_ctx
1426 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
1427 variables
= new Array[AutoVariable]
1428 for i
in [0..n_id
.length
[ do
1429 var va
= new AutoVariable(n_id
[i
].to_symbol
, self)
1432 v
.variable_ctx
.add
(va
)
1435 _accept_typing2
= true
1438 if v
.variable_ctx
.unreash
== false then
1439 if closure
.signature
.return_type
!= null then
1440 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
1441 else if closure
.is_break
then
1442 v
.error
(self, "Control error: Reached end of break block (a 'break' was expected).")
1445 v
.variable_ctx
= old_var_ctx
1449 class ATypeCheckExpr
1451 private meth check_expr_cast
(v
: TypingVisitor, n_expr
: PExpr, n_type
: PType)
1453 if not v
.check_expr
(n_expr
) then return
1454 var etype
= n_expr
.stype
1455 var ttype
= n_type
.stype
1456 if etype
== ttype
then
1457 v
.warning
(self, "Warning: Expression is already a {ttype}.")
1458 else if etype
< ttype
then
1459 v
.warning
(self, "Warning: Expression is already a {ttype} since it is a {etype}.")
1464 redef class AIsaExpr
1465 special ATypeCheckExpr
1466 redef meth after_typing
(v
)
1468 check_expr_cast
(v
, n_expr
, n_type
)
1469 var variable
= n_expr
.its_variable
1470 if variable
!= null then
1471 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, n_type
.stype
)
1473 _stype
= v
.type_bool
1478 redef class AAsCastExpr
1479 special ATypeCheckExpr
1480 redef meth after_typing
(v
)
1482 check_expr_cast
(v
, n_expr
, n_type
)
1483 _stype
= n_type
.stype
1484 _is_typed
= _stype
!= null
1488 redef class AProxyExpr
1489 redef meth after_typing
(v
)
1491 if not n_expr
.is_typed
then return
1493 if n_expr
.is_statement
then return
1494 _stype
= n_expr
.stype
1498 redef class AOnceExpr
1499 redef meth accept_typing
(v
)
1501 if v
.once_count
> 0 then
1502 v
.warning
(self, "Useless once in a once expression.")
1504 v
.once_count
= v
.once_count
+ 1
1508 v
.once_count
= v
.once_count
- 1