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 # Non-bypassable knowledge about variables names and types
50 readable writable attr _base_variable_ctx
: VariableContext
52 # Current knowledge about escapable blocks
53 readable writable attr _escapable_ctx
: EscapableContext = new EscapableContext(self)
55 # The current reciever
56 readable writable attr _self_var
: ParamVariable
58 # Block of the current method
59 readable writable attr _top_block
: PExpr
61 # List of explicit invocation of constructors of super-classes
62 readable writable attr _explicit_super_init_calls
: Array[MMMethod]
64 # Is a other constructor of the same class invoked
65 readable writable attr _explicit_other_init_call
: Bool
67 # Make the if_true_variable_ctx of the expression effective
68 private meth use_if_true_variable_ctx
(e
: PExpr)
70 var ctx
= e
.if_true_variable_ctx
71 if ctx
!= null then variable_ctx
= ctx
74 # Make the if_false_variable_ctx of the expression effective
75 private meth use_if_false_variable_ctx
(e
: PExpr)
77 var ctx
= e
.if_false_variable_ctx
78 if ctx
!= null then variable_ctx
= ctx
81 # Number of nested once
82 readable writable attr _once_count
: Int = 0
84 init(tc
, module) do super
86 private meth get_default_constructor_for
(n
: PNode, c
: MMLocalClass, prop
: MMSrcMethod): MMMethod
89 #var prop = v.local_property
90 #assert prop isa MMMethod
91 var candidates
= new Array[MMMethod]
92 var false_candidates
= new Array[MMMethod]
93 var parity
= prop
.signature
.arity
94 for g
in c
.global_properties
do
95 if not g
.is_init_for
(c
) then continue
97 var gps
= gp
.signature_for
(c
.get_type
)
98 assert gp
isa MMSrcMethod
99 var garity
= gps
.arity
100 if prop
!= null and gp
.name
== prop
.name
then
101 if garity
== 0 or (parity
== garity
and prop
.signature
< gps
) then
104 false_candidates
.add
(gp
)
106 else if garity
== 0 and gp
.name
== once
("init".to_symbol
) then
108 false_candidates
.add
(gp
)
110 false_candidates
.add
(gp
)
113 if candidates
.length
== 1 then
114 return candidates
.first
115 else if candidates
.length
> 0 then
116 var a
= new Array[String]
117 for p
in candidates
do
118 a
.add
("{p.full_name}{p.signature}")
120 v
.error
(n
, "Error: Conflicting default constructor to call for {c}: {a.join(", ")}.")
122 else if false_candidates
.length
> 0 then
123 var a
= new Array[String]
124 for p
in false_candidates
do
125 a
.add
("{p.full_name}{p.signature}")
127 v
.error
(n
, "Error: there is no available compatible constrctor in {c}. Discarded candidates are {a.join(", ")}.")
130 v
.error
(n
, "Error: there is no available compatible constrctor in {c}.")
137 ###############################################################################
140 private meth accept_typing
(v
: TypingVisitor)
142 accept_abs_syntax_visitor
(v
)
145 private meth after_typing
(v
: TypingVisitor) do end
148 redef class PClassdef
149 redef meth accept_typing
(v
)
151 v
.self_var
= new ParamVariable("self".to_symbol
, self)
152 v
.self_var
.stype
= local_class
.get_type
157 redef class AAttrPropdef
158 redef meth accept_typing
(v
)
161 if n_expr
!= null then
162 v
.check_conform_expr
(n_expr
, prop
.signature
.return_type
)
167 redef class AMethPropdef
168 redef readable attr _self_var
: ParamVariable
169 redef meth accept_typing
(v
)
171 v
.variable_ctx
= new RootVariableContext(v
, self)
172 v
.base_variable_ctx
= v
.variable_ctx
173 _self_var
= v
.self_var
178 redef class AConcreteMethPropdef
179 redef meth accept_typing
(v
)
182 if v
.variable_ctx
.unreash
== false and method
.signature
.return_type
!= null then
183 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
188 redef class AConcreteInitPropdef
189 readable attr _super_init_calls
: Array[MMMethod] = new Array[MMMethod]
190 readable attr _explicit_super_init_calls
: Array[MMMethod] = new Array[MMMethod]
191 redef meth accept_typing
(v
)
193 v
.top_block
= n_block
194 v
.explicit_super_init_calls
= explicit_super_init_calls
195 v
.explicit_other_init_call
= false
197 if v
.explicit_other_init_call
or method
.global
.intro
!= method
then
201 var l
= explicit_super_init_calls
.length
202 var cur_m
: MMMethod = null
203 var cur_c
: MMLocalClass = null
205 cur_m
= explicit_super_init_calls
[i
]
206 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
209 while j
< v
.local_class
.cshe
.direct_greaters
.length
do
210 var c
= v
.local_class
.cshe
.direct_greaters
[j
]
211 if c
.global
.is_interface
or c
.global
.is_universal
or c
.global
.is_mixin
then
213 else if cur_c
!= null and (c
.cshe
<= cur_c
or cur_c
.global
.is_mixin
) then
214 if c
== cur_c
then j
+= 1
215 super_init_calls
.add
(cur_m
)
218 cur_m
= explicit_super_init_calls
[i
]
219 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
225 var p
= v
.get_default_constructor_for
(self, c
, method
)
227 super_init_calls
.add
(p
)
237 redef meth after_typing
(v
)
239 # TODO: why the test?
240 if v
.variable_ctx
!= null then
241 v
.variable_ctx
.add
(variable
)
246 redef class AClosureDecl
247 # The corresponding escapable object
248 readable attr _escapable
: EscapableBlock
250 redef meth accept_typing
(v
)
252 # Register the closure for ClosureCallExpr
253 v
.variable_ctx
.add
(variable
)
255 var old_var_ctx
= v
.variable_ctx
256 var old_base_var_ctx
= v
.base_variable_ctx
257 v
.base_variable_ctx
= v
.variable_ctx
258 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
260 _escapable
= new EscapableClosure(self, variable
.closure
, null)
261 v
.escapable_ctx
.push
(_escapable
)
265 if n_expr
!= null then
266 if v
.variable_ctx
.unreash
== false then
267 if variable
.closure
.signature
.return_type
!= null then
268 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
269 else if variable
.closure
.is_break
then
270 v
.error
(self, "Control error: Reached end of break block (an 'abort' was expected).")
275 old_var_ctx
.merge
(v
.variable_ctx
)
276 v
.variable_ctx
= old_var_ctx
277 v
.base_variable_ctx
= old_base_var_ctx
283 readable attr _stype
: MMType
284 redef meth after_typing
(v
)
286 _stype
= get_stype
(v
)
291 redef readable attr _is_typed
: Bool = false
292 redef meth is_statement
: Bool do return _stype
== null
296 print
"{locate}: not is_typed"
300 print
"{locate}: is_statement"
307 # Is the expression the implicit receiver
308 meth is_implicit_self
: Bool do return false
310 # Is the expression the current receiver (implicit or explicit)
311 meth is_self
: Bool do return false
313 # The variable accessed is any
314 meth its_variable
: Variable do return null
316 # The variable type information if current boolean expression is true
317 readable private attr _if_true_variable_ctx
: VariableContext
319 # The variable type information if current boolean expression is false
320 readable private attr _if_false_variable_ctx
: VariableContext
323 redef class AVardeclExpr
324 redef meth after_typing
(v
)
326 var va
= new VarVariable(n_id
.to_symbol
, self)
328 v
.variable_ctx
.add
(va
)
329 if n_expr
!= null then v
.variable_ctx
.mark_is_set
(va
)
331 if n_type
!= null then
332 va
.stype
= n_type
.stype
333 if n_expr
!= null then
334 v
.check_conform_expr
(n_expr
, va
.stype
)
337 if not v
.check_expr
(n_expr
) then return
338 va
.stype
= n_expr
.stype
344 redef class ABlockExpr
345 redef meth accept_typing
(v
)
347 var old_var_ctx
= v
.variable_ctx
348 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
351 if v
.variable_ctx
.unreash
and not v
.variable_ctx
.already_unreash
then
352 v
.variable_ctx
.already_unreash
= true
353 v
.warning
(e
, "Warning: unreachable statement.")
358 old_var_ctx
.merge
(v
.variable_ctx
)
359 v
.variable_ctx
= old_var_ctx
364 redef class AReturnExpr
365 redef meth after_typing
(v
)
367 v
.variable_ctx
.unreash
= true
368 var t
= v
.local_property
.signature
.return_type
369 if n_expr
== null and t
!= null then
370 v
.error
(self, "Error: Return without value in a function.")
371 else if n_expr
!= null and t
== null then
372 v
.error
(self, "Error: Return with value in a procedure.")
373 else if n_expr
!= null and t
!= null then
374 v
.check_conform_expr
(n_expr
, t
)
380 redef class AContinueExpr
381 redef meth after_typing
(v
)
383 v
.variable_ctx
.unreash
= true
384 var esc
= compute_escapable_block
(v
.escapable_ctx
)
385 if esc
== null then return
387 if esc
.is_break_block
then
388 v
.error
(self, "Error: 'continue' forbiden in break blocks.")
392 var t
= esc
.continue_stype
393 if n_expr
== null and t
!= null then
394 v
.error
(self, "Error: continue with a value required in this block.")
395 else if n_expr
!= null and t
== null then
396 v
.error
(self, "Error: continue without value required in this block.")
397 else if n_expr
!= null and t
!= null then
398 v
.check_conform_expr
(n_expr
, t
)
404 redef class ABreakExpr
405 redef meth after_typing
(v
)
407 v
.variable_ctx
.unreash
= true
408 var esc
= compute_escapable_block
(v
.escapable_ctx
)
409 if esc
== null then return
411 var bl
= esc
.break_list
412 if n_expr
== null and bl
!= null then
413 v
.error
(self, "Error: break with a value required in this block.")
414 else if n_expr
!= null and bl
== null then
415 v
.error
(self, "Error: break without value required in this block.")
416 else if n_expr
!= null and bl
!= null then
417 # Typing check can only be done later
424 redef class AAbortExpr
425 redef meth after_typing
(v
)
427 v
.variable_ctx
.unreash
= true
432 redef meth accept_typing
(v
)
434 var old_var_ctx
= v
.variable_ctx
436 v
.check_conform_expr
(n_expr
, v
.type_bool
)
438 # Prepare 'then' context
439 v
.use_if_true_variable_ctx
(n_expr
)
442 if n_then
!= null then
443 v
.variable_ctx
= v
.variable_ctx
.sub
(n_then
)
447 # Remember what appened in the 'then'
448 var then_var_ctx
= v
.variable_ctx
450 # Prepare 'else' context
451 v
.variable_ctx
= old_var_ctx
452 v
.use_if_false_variable_ctx
(n_expr
)
455 if n_else
!= null then
456 v
.variable_ctx
= v
.variable_ctx
.sub
(n_else
)
460 # Merge 'then' and 'else' contexts
461 old_var_ctx
.merge2
(then_var_ctx
, v
.variable_ctx
, v
.base_variable_ctx
)
462 v
.variable_ctx
= old_var_ctx
467 redef class AWhileExpr
468 # The corresponding escapable block
469 readable attr _escapable
: EscapableBlock
471 redef meth accept_typing
(v
)
473 _escapable
= new EscapableBlock(self)
474 v
.escapable_ctx
.push
(_escapable
)
475 var old_var_ctx
= v
.variable_ctx
476 var old_base_var_ctx
= v
.base_variable_ctx
477 v
.base_variable_ctx
= v
.variable_ctx
478 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
482 v
.check_conform_expr
(n_expr
, v
.type_bool
)
483 v
.variable_ctx
= old_var_ctx
484 v
.base_variable_ctx
= old_base_var_ctx
491 # The corresponding escapable block
492 readable attr _escapable
: EscapableBlock
494 readable attr _meth_iterator
: MMMethod
495 readable attr _meth_is_ok
: MMMethod
496 readable attr _meth_item
: MMMethod
497 readable attr _meth_next
: MMMethod
498 redef meth accept_typing
(v
)
500 _escapable
= new EscapableBlock(self)
501 v
.escapable_ctx
.push
(_escapable
)
503 var old_var_ctx
= v
.variable_ctx
504 var old_base_var_ctx
= v
.base_variable_ctx
505 v
.base_variable_ctx
= v
.variable_ctx
506 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
507 var va
= new AutoVariable(n_id
.to_symbol
, self)
509 v
.variable_ctx
.add
(va
)
513 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then return
514 var expr_type
= n_expr
.stype
515 _meth_iterator
= expr_type
.local_class
.select_method
(once
("iterator".to_symbol
))
516 if _meth_iterator
== null then
517 v
.error
(self, "Error: Collection MUST have an iterate method")
520 var iter_type
= _meth_iterator
.signature_for
(expr_type
).return_type
521 _meth_is_ok
= iter_type
.local_class
.select_method
(once
("is_ok".to_symbol
))
522 if _meth_is_ok
== null then
523 v
.error
(self, "Error: {iter_type} MUST have an is_ok method")
526 _meth_item
= iter_type
.local_class
.select_method
(once
("item".to_symbol
))
527 if _meth_item
== null then
528 v
.error
(self, "Error: {iter_type} MUST have an item method")
531 _meth_next
= iter_type
.local_class
.select_method
(once
("next".to_symbol
))
532 if _meth_next
== null then
533 v
.error
(self, "Error: {iter_type} MUST have a next method")
536 var t
= _meth_item
.signature_for
(iter_type
).return_type
537 if not n_expr
.is_self
then t
= t
.not_for_self
540 if n_block
!= null then v
.visit
(n_block
)
543 v
.variable_ctx
= old_var_ctx
544 v
.base_variable_ctx
= old_base_var_ctx
550 redef class AAssertExpr
551 redef meth after_typing
(v
)
553 v
.check_conform_expr
(n_expr
, v
.type_bool
)
554 v
.use_if_true_variable_ctx
(n_expr
)
560 redef meth its_variable
do return variable
562 redef meth after_typing
(v
)
564 v
.variable_ctx
.check_is_set
(self, variable
)
565 _stype
= v
.variable_ctx
.stype
(variable
)
566 _is_typed
= _stype
!= null
570 redef class AVarAssignExpr
571 redef meth after_typing
(v
)
573 v
.variable_ctx
.mark_is_set
(variable
)
574 var t
= v
.variable_ctx
.stype
(variable
)
576 # Check the base type
577 var btype
= v
.base_variable_ctx
.stype
(variable
)
578 if not v
.check_conform_expr
(n_value
, btype
) then return
580 # Bypasse cast if then current type does not match
581 if not n_value
.stype
< t
then v
.variable_ctx
.stype
(variable
) = btype
587 redef class AReassignFormExpr
588 # Compute and check method used through the reassigment operator
589 # On success return the static type of the result of the reassigment operator
590 # Else display an error and return null
591 private meth do_rvalue_typing
(v
: TypingVisitor, type_lvalue
: MMType): MMType
593 if type_lvalue
== null then
596 var name
= n_assign_op
.method_name
597 var lc
= type_lvalue
.local_class
598 if not lc
.has_global_property_by_name
(name
) then
599 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
602 var prop
= lc
.select_method
(name
)
603 prop
.global
.check_visibility
(v
, self, v
.module, false)
604 var psig
= prop
.signature_for
(type_lvalue
)
605 _assign_method
= prop
606 if not v
.check_conform_expr
(n_value
, psig
[0].not_for_self
) then return null
607 return psig
.return_type
.not_for_self
610 # Method used through the reassigment operator (once computed)
611 readable attr _assign_method
: MMMethod
614 redef class AVarReassignExpr
615 redef meth after_typing
(v
)
617 v
.variable_ctx
.check_is_set
(self, variable
)
618 v
.variable_ctx
.mark_is_set
(variable
)
619 var t
= v
.variable_ctx
.stype
(variable
)
620 var t2
= do_rvalue_typing
(v
, t
)
621 if t2
== null then return
623 # Check the base type
624 var btype
= v
.base_variable_ctx
.stype
(variable
)
625 if not v
.check_conform
(n_value
, t2
, btype
) then return
627 # Bypasse cast if then current type does not match
628 if not t2
< t
then v
.variable_ctx
.stype
(variable
) = btype
634 redef class PAssignOp
635 meth method_name
: Symbol is abstract
637 redef class APlusAssignOp
638 redef meth method_name
do return once
"+".to_symbol
640 redef class AMinusAssignOp
641 redef meth method_name
do return once
"-".to_symbol
644 redef class ASelfExpr
645 redef meth its_variable
do return variable
647 redef meth after_typing
(v
)
649 variable
= v
.self_var
650 _stype
= v
.variable_ctx
.stype
(variable
)
654 redef meth is_self
do return true
657 redef class AImplicitSelfExpr
658 redef meth is_implicit_self
do return true
661 redef class AIfexprExpr
662 redef meth accept_typing
(v
)
664 var old_var_ctx
= v
.variable_ctx
667 v
.use_if_true_variable_ctx
(n_expr
)
669 v
.variable_ctx
= old_var_ctx
670 v
.use_if_false_variable_ctx
(n_expr
)
673 v
.check_conform_expr
(n_expr
, v
.type_bool
)
675 _stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
676 _is_typed
= _stype
!= null
680 redef class ABoolExpr
681 redef meth after_typing
(v
)
689 redef meth after_typing
(v
)
691 var old_var_ctx
= v
.variable_ctx
694 v
.use_if_false_variable_ctx
(n_expr
)
697 if n_expr2
.if_false_variable_ctx
!= null then
698 _if_false_variable_ctx
= n_expr2
.if_false_variable_ctx
700 _if_false_variable_ctx
= v
.variable_ctx
703 v
.variable_ctx
= old_var_ctx
705 v
.check_conform_expr
(n_expr
, v
.type_bool
)
706 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
713 redef meth accept_typing
(v
)
715 var old_var_ctx
= v
.variable_ctx
718 v
.use_if_true_variable_ctx
(n_expr
)
721 if n_expr2
.if_true_variable_ctx
!= null then
722 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
724 _if_true_variable_ctx
= v
.variable_ctx
727 v
.variable_ctx
= old_var_ctx
729 v
.check_conform_expr
(n_expr
, v
.type_bool
)
730 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
737 redef meth after_typing
(v
)
739 v
.check_conform_expr
(n_expr
, v
.type_bool
)
741 # Invert if_true/if_false information
742 _if_false_variable_ctx
= n_expr
._if_true_variable_ctx
743 _if_true_variable_ctx
= n_expr
._if_false_variable_ctx
751 redef meth after_typing
(v
)
758 redef class AFloatExpr
759 redef meth after_typing
(v
)
761 _stype
= v
.type_float
766 redef class ACharExpr
767 redef meth after_typing
(v
)
774 redef class AStringFormExpr
775 readable attr _meth_with_native
: MMMethod
776 redef meth after_typing
(v
)
778 _stype
= v
.type_string
780 _meth_with_native
= _stype
.local_class
.select_method
(once
"with_native".to_symbol
)
781 if _meth_with_native
== null then v
.error
(self, "{_stype} MUST have a with_native method.")
785 redef class ASuperstringExpr
786 readable attr _meth_with_capacity
: MMMethod
787 readable attr _meth_add
: MMMethod
788 readable attr _meth_to_s
: MMMethod
789 readable attr _atype
: MMType
790 redef meth after_typing
(v
)
792 _stype
= v
.type_string
793 _atype
= v
.type_array
(_stype
)
794 _meth_with_capacity
= _atype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
795 if _meth_with_capacity
== null then v
.error
(self, "{_atype} MUST have a with_capacity method.")
796 _meth_add
= _atype
.local_class
.select_method
(once
"add".to_symbol
)
797 if _meth_add
== null then v
.error
(self, "{_atype} MUST have an add method.")
798 _meth_to_s
= v
.type_object
.local_class
.select_method
(once
"to_s".to_symbol
)
799 if _meth_to_s
== null then v
.error
(self, "Object MUST have a to_s method.")
804 redef class ANullExpr
805 redef meth after_typing
(v
)
812 redef class AArrayExpr
813 readable attr _meth_with_capacity
: MMMethod
814 readable attr _meth_add
: MMMethod
816 redef meth after_typing
(v
)
818 var stype
= v
.check_conform_multiexpr
(null, n_exprs
)
819 if stype
== null then return
823 private meth do_typing
(v
: TypingVisitor, element_type
: MMType)
825 _stype
= v
.type_array
(element_type
)
827 _meth_with_capacity
= _stype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
828 if _meth_with_capacity
== null then v
.error
(self, "{_stype} MUST have a with_capacity method.")
829 _meth_add
= _stype
.local_class
.select_method
(once
"add".to_symbol
)
830 if _meth_add
== null then v
.error
(self, "{_stype} MUST have an add method.")
836 redef class ARangeExpr
837 readable attr _meth_init
: MMMethod
838 redef meth after_typing
(v
)
840 if not v
.check_expr
(n_expr
) or not v
.check_expr
(n_expr2
) then return
841 var ntype
= n_expr
.stype
842 var ntype2
= n_expr2
.stype
843 if ntype
< ntype2
then
845 else if not ntype2
< ntype
then
846 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
849 var dtype
= v
.type_discrete
850 if not v
.check_conform_expr
(n_expr
, dtype
) or not v
.check_conform_expr
(n_expr2
, dtype
) then return
851 _stype
= v
.type_range
(ntype
)
856 redef class ACrangeExpr
857 redef meth after_typing
(v
)
860 _meth_init
= stype
.local_class
.select_method
(once
"init".to_symbol
)
863 redef class AOrangeExpr
864 redef meth after_typing
(v
)
867 _meth_init
= stype
.local_class
.select_method
(once
"without_last".to_symbol
)
872 redef class ASuperExpr
873 special ASuperInitCall
874 # readable attr _prop: MMSrcMethod
875 readable attr _init_in_superclass
: MMMethod
876 redef meth after_typing
(v
)
878 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
879 if not precs
.is_empty
then
880 v
.local_property
.need_super
= true
881 else if v
.local_property
.global
.is_init
then
882 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
883 for p
in base_precs
do
884 if not p
.global
.is_init
then
885 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
887 precs
.add
(v
.local_class
[p
.global
])
890 if precs
.is_empty
then
891 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
893 else if precs
.length
> 1 then
894 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
897 var p
= base_precs
.first
898 assert p
isa MMMethod
899 _init_in_superclass
= p
900 register_super_init_call
(v
, p
)
901 if n_args
.length
> 0 then
902 var signature
= get_signature
(v
, v
.self_var
.stype
, p
, true)
903 _arguments
= process_signature
(v
, signature
, p
.name
, n_args
.to_a
)
906 v
.error
(self, "Error: No super method to call for {v.local_property}.")
910 if precs
.first
.signature_for
(v
.self_var
.stype
).return_type
!= null then
911 var stypes
= new Array[MMType]
912 var stype
: MMType = null
914 assert prop
isa MMMethod
915 var t
= prop
.signature_for
(v
.self_var
.stype
).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
917 if stype
== null or stype
< t
then
922 v
.check_conform
(self, t
, stype
)
926 var p
= v
.local_property
927 assert p
isa MMSrcMethod
933 redef class AAttrFormExpr
935 readable attr _prop
: MMAttribute
937 # Attribute type of the acceded attribute
938 readable attr _attr_type
: MMType
940 # Compute the attribute accessed
941 private meth do_typing
(v
: TypingVisitor)
943 if not v
.check_expr
(n_expr
) then return
944 var type_recv
= n_expr
.stype
945 var name
= n_id
.to_symbol
946 var lc
= type_recv
.local_class
947 if not lc
.has_global_property_by_name
(name
) then
948 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
951 var prop
= lc
.select_attribute
(name
)
952 if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
953 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
956 var at
= prop
.signature_for
(type_recv
).return_type
957 if not n_expr
.is_self
then at
= at
.not_for_self
962 redef class AAttrExpr
963 redef meth after_typing
(v
)
966 if prop
== null then return
972 redef class AAttrAssignExpr
973 redef meth after_typing
(v
)
976 if prop
== null then return
977 if not v
.check_conform_expr
(n_value
, attr_type
) then return
982 redef class AAttrReassignExpr
983 redef meth after_typing
(v
)
986 if prop
== null then return
987 var t
= do_rvalue_typing
(v
, attr_type
)
988 if t
== null then return
989 v
.check_conform
(self, t
, n_value
.stype
)
994 class AAbsAbsSendExpr
996 # The signature of the called property
997 readable attr _prop_signature
: MMSignature
999 # The real arguments used (after star transformation) (once computed)
1000 readable attr _arguments
: Array[PExpr]
1002 # Check the conformity of a set of arguments `raw_args' to a signature.
1003 private meth process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: Array[PExpr]): Array[PExpr]
1005 var par_vararg
= psig
.vararg_rank
1006 var par_arity
= psig
.arity
1008 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
1009 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
1010 v
.error
(self, "Error: '{name}' arity missmatch.")
1014 var args
= new Array[PExpr]
1015 for par_idx
in [0..par_arity
[ do
1017 var par_type
= psig
[par_idx
]
1018 if par_idx
== par_vararg
then
1019 var star
= new Array[PExpr]
1020 for i
in [0..(raw_arity-par_arity
)] do
1021 a
= raw_args
[arg_idx
]
1022 v
.check_conform_expr
(a
, par_type
)
1024 arg_idx
= arg_idx
+ 1
1026 var aa
= new AArrayExpr.init_aarrayexpr
(star
)
1027 aa
.do_typing
(v
, par_type
)
1030 a
= raw_args
[arg_idx
]
1031 v
.check_conform_expr
(a
, par_type
)
1032 arg_idx
= arg_idx
+ 1
1039 # Check the conformity of a set of defined closures
1040 private meth process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: Array[PClosureDef]): MMType
1042 var t
= psig
.return_type
1043 var cs
= psig
.closures
# Declared closures
1046 if not c
.is_optional
then min_arity
+= 1
1049 if cs
.length
== 0 then
1050 v
.error
(self, "Error: {name} does not require blocks.")
1051 else if cd
.length
> cs
.length
or cd
.length
< min_arity
then
1052 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
1054 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1055 var break_list
: Array[ABreakExpr] = null
1056 if t
!= null then break_list
= new Array[ABreakExpr]
1058 # Process each closure definition
1059 for i
in [0..cd
.length
[ do
1062 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1063 v
.escapable_ctx
.push
(esc
)
1064 cdi
.accept_typing2
(v
, esc
)
1068 # Check break type conformity
1069 if break_list
!= null then
1070 t
= v
.check_conform_multiexpr
(t
, break_list
)
1073 else if min_arity
!= 0 then
1074 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1081 special AAbsAbsSendExpr
1082 # Compute the called global property
1083 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])
1085 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
1086 if prop
== null then return
1087 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
1088 if sig
== null then return
1089 var args
= process_signature
(v
, sig
, prop
.name
, raw_args
)
1090 if args
== null then return
1091 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
1092 if rtype
== null and sig
.return_type
!= null then return
1094 _prop_signature
= sig
1096 _return_type
= rtype
1099 private meth get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): MMMethod
1101 if type_recv
== null then return null
1102 var lc
= type_recv
.local_class
1103 var prop
: MMMethod = null
1104 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
1105 if prop
== null and v
.local_property
.global
.is_init
then
1106 var props
= type_recv
.local_class
.super_methods_named
(name
)
1107 if props
.length
> 1 then
1108 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1110 else if props
.length
== 1 then
1111 var p
= type_recv
.local_class
[props
.first
.global
]
1112 assert p
isa MMMethod
1117 if prop
== null then
1118 if is_implicit_self
then
1119 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1121 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1128 # Get the signature for a local property and a receiver
1129 private meth get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
1131 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
1132 var psig
= prop
.signature_for
(type_recv
)
1133 if not recv_is_self
then psig
= psig
.not_for_self
1137 # The invoked method (once computed)
1138 readable attr _prop
: MMMethod
1140 # The return type (if any) (once computed)
1141 readable attr _return_type
: MMType
1144 # A possible call of constructor in a super class
1145 # Could be an explicit call or with the 'super' keyword
1146 class ASuperInitCall
1147 special AAbsSendExpr
1148 private meth register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1150 if parent
!= v
.top_block
and self != v
.top_block
then
1151 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1153 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
1154 var prev_class
: MMLocalClass = null
1155 if not v
.explicit_super_init_calls
.is_empty
then
1156 prev_class
= v
.explicit_super_init_calls
.last
.global
.intro
.local_class
1158 var order
= v
.local_class
.cshe
.reverse_linear_extension
1159 if cla
== v
.local_class
then
1160 v
.explicit_other_init_call
= true
1161 else if not order
.has
(cla
) then
1162 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1163 else if cla
== prev_class
then
1164 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1166 var last_is_found
= prev_class
== null
1168 if c
== prev_class
then
1169 last_is_found
= true
1170 else if c
== cla
then
1171 if not last_is_found
then
1172 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1174 v
.explicit_super_init_calls
.add
(property
)
1183 redef class ANewExpr
1184 special AAbsSendExpr
1185 redef meth after_typing
(v
)
1187 var t
= n_type
.stype
1188 if t
== null then return
1189 if t
.local_class
.global
.is_abstract
then
1190 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1194 if n_id
== null then
1195 name
= once
"init".to_symbol
1197 name
= n_id
.to_symbol
1200 do_typing
(v
, t
, false, false, name
, n_args
.to_a
, null)
1201 if prop
== null then return
1203 if not prop
.global
.is_init
then
1204 v
.error
(self, "Error: {prop} is not a constructor.")
1213 redef class ASendExpr
1214 special ASuperInitCall
1215 # Name of the invoked property
1216 meth name
: Symbol is abstract
1218 # Raw arguments used (withour star transformation)
1219 meth raw_arguments
: Array[PExpr] is abstract
1221 # Closure definitions
1222 meth closure_defs
: Array[PClosureDef] do return null
1224 redef meth after_typing
(v
)
1229 private meth do_all_typing
(v
: TypingVisitor)
1231 if not v
.check_expr
(n_expr
) then return
1232 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1233 if prop
== null then return
1235 if prop
.global
.is_init
then
1236 if not v
.local_property
.global
.is_init
then
1237 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1238 else if not n_expr
.is_self
then
1239 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1241 register_super_init_call
(v
, prop
)
1245 _stype
= return_type
1250 class ASendReassignExpr
1252 special AReassignFormExpr
1253 readable attr _read_prop
: MMMethod
1254 redef meth do_all_typing
(v
)
1256 if not v
.check_expr
(n_expr
) then return
1257 var raw_args
= raw_arguments
1258 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1259 if prop
== null then return
1260 if prop
.global
.is_init
then
1261 if not v
.local_property
.global
.is_init
then
1262 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1263 else if not n_expr
.is_self
then
1264 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1267 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1268 if not n_expr
.is_self
then t
= t
.not_for_self
1270 var t2
= do_rvalue_typing
(v
, t
)
1271 if t2
== null then return
1272 v
.check_conform
(self, t2
, n_value
.stype
)
1275 var old_args
= arguments
1276 raw_args
.add
(n_value
)
1278 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1279 if prop
== null then return
1280 if prop
.global
.is_init
then
1281 if not v
.local_property
.global
.is_init
then
1282 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1283 else if not n_expr
.is_self
then
1284 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1288 _arguments
= old_args
# FIXME: What if star parameters do not match betwen the two methods?
1293 redef class ABinopExpr
1294 redef meth raw_arguments
do return [n_expr2
]
1297 redef meth name
do return once
"==".to_symbol
1300 redef meth name
do return once
"!=".to_symbol
1303 redef meth name
do return once
"<".to_symbol
1306 redef meth name
do return once
"<=".to_symbol
1309 redef meth name
do return once
">".to_symbol
1312 redef meth name
do return once
">=".to_symbol
1314 redef class APlusExpr
1315 redef meth name
do return once
"+".to_symbol
1317 redef class AMinusExpr
1318 redef meth name
do return once
"-".to_symbol
1320 redef class AStarshipExpr
1321 redef meth name
do return once
"<=>".to_symbol
1323 redef class AStarExpr
1324 redef meth name
do return once
"*".to_symbol
1326 redef class ASlashExpr
1327 redef meth name
do return once
"/".to_symbol
1329 redef class APercentExpr
1330 redef meth name
do return once
"%".to_symbol
1333 redef class AUminusExpr
1334 redef meth name
do return once
"unary -".to_symbol
1335 redef meth raw_arguments
do return null
1338 redef class ACallFormExpr
1339 redef meth after_typing
(v
)
1341 if n_expr
!= null and n_expr
.is_implicit_self
then
1342 var name
= n_id
.to_symbol
1343 var variable
= v
.variable_ctx
[name
]
1344 if variable
!= null then
1345 if variable
isa ClosureVariable then
1346 var n
= new AClosureCallExpr.init_aclosurecallexpr
(n_id
, n_args
, n_closure_defs
)
1348 n
.variable
= variable
1352 if not n_args
.is_empty
then
1353 v
.error
(self, "Error: {name} is variable, not a function.")
1356 var vform
= variable_create
(variable
)
1357 vform
.variable
= variable
1359 vform
.after_typing
(v
)
1368 redef meth closure_defs
1370 if n_closure_defs
== null or n_closure_defs
.is_empty
then
1373 return n_closure_defs
.to_a
1377 # Create a variable acces corresponding to the call form
1378 meth variable_create
(variable
: Variable): AVarFormExpr is abstract
1381 redef class ACallExpr
1382 redef meth variable_create
(variable
)
1384 return new AVarExpr.init_avarexpr
(n_id
)
1387 redef meth name
do return n_id
.to_symbol
1388 redef meth raw_arguments
do return n_args
.to_a
1391 redef class ACallAssignExpr
1392 redef meth variable_create
(variable
)
1394 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1397 redef meth name
do return (n_id
.text
+ "=").to_symbol
1398 redef meth raw_arguments
do
1399 var res
= n_args
.to_a
1405 redef class ACallReassignExpr
1406 special ASendReassignExpr
1407 redef meth variable_create
(variable
)
1409 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1412 redef meth name
do return n_id
.to_symbol
1413 redef meth raw_arguments
do return n_args
.to_a
1416 redef class ABraExpr
1417 redef meth name
do return once
"[]".to_symbol
1418 redef meth raw_arguments
do return n_args
.to_a
1421 redef class ABraAssignExpr
1422 redef meth name
do return once
"[]=".to_symbol
1423 redef meth raw_arguments
do
1424 var res
= n_args
.to_a
1430 redef class ABraReassignExpr
1431 special ASendReassignExpr
1432 redef meth name
do return once
"[]".to_symbol
1433 redef meth raw_arguments
do return n_args
.to_a
1436 redef class AInitExpr
1437 redef meth name
do return once
"init".to_symbol
1438 redef meth raw_arguments
do return n_args
.to_a
1441 redef class AClosureCallExpr
1442 special AAbsAbsSendExpr
1443 redef meth after_typing
(v
)
1446 if va
.closure
.is_break
then v
.variable_ctx
.unreash
= true
1447 var sig
= va
.closure
.signature
1448 var args
= process_signature
(v
, sig
, n_id
.to_symbol
, n_args
.to_a
)
1449 if not n_closure_defs
.is_empty
then
1450 process_closures
(v
, sig
, n_id
.to_symbol
, n_closure_defs
.to_a
)
1452 if args
== null then return
1453 _prop_signature
= sig
1455 _stype
= sig
.return_type
1460 redef class PClosureDef
1461 # The corresponding escapable object
1462 readable attr _escapable
: EscapableBlock
1464 attr _accept_typing2
: Bool
1465 redef meth accept_typing
(v
)
1467 # Typing is deferred, wait accept_typing2(v)
1468 if _accept_typing2
then super
1471 private meth accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure) is abstract
1474 redef class AClosureDef
1475 redef meth accept_typing2
(v
, esc
)
1479 var sig
= esc
.closure
.signature
1480 if sig
.arity
!= n_id
.length
then
1481 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1485 closure
= esc
.closure
1487 var old_var_ctx
= v
.variable_ctx
1488 var old_base_var_ctx
= v
.base_variable_ctx
1489 v
.base_variable_ctx
= v
.variable_ctx
1490 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
1491 variables
= new Array[AutoVariable]
1492 for i
in [0..n_id
.length
[ do
1493 var va
= new AutoVariable(n_id
[i
].to_symbol
, self)
1496 v
.variable_ctx
.add
(va
)
1499 _accept_typing2
= true
1502 if v
.variable_ctx
.unreash
== false then
1503 if closure
.signature
.return_type
!= null then
1504 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
1505 else if closure
.is_break
then
1506 v
.error
(self, "Control error: Reached end of break block (a 'break' was expected).")
1509 v
.variable_ctx
= old_var_ctx
1510 v
.base_variable_ctx
= old_base_var_ctx
1514 class ATypeCheckExpr
1516 private meth check_expr_cast
(v
: TypingVisitor, n_expr
: PExpr, n_type
: PType)
1518 if not v
.check_expr
(n_expr
) then return
1519 var etype
= n_expr
.stype
1520 var ttype
= n_type
.stype
1521 if etype
== ttype
then
1522 v
.warning
(self, "Warning: Expression is already a {ttype}.")
1523 else if etype
< ttype
then
1524 v
.warning
(self, "Warning: Expression is already a {ttype} since it is a {etype}.")
1529 redef class AIsaExpr
1530 special ATypeCheckExpr
1531 redef meth after_typing
(v
)
1533 check_expr_cast
(v
, n_expr
, n_type
)
1534 var variable
= n_expr
.its_variable
1535 if variable
!= null then
1536 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, n_type
.stype
)
1538 _stype
= v
.type_bool
1543 redef class AAsCastExpr
1544 special ATypeCheckExpr
1545 redef meth after_typing
(v
)
1547 check_expr_cast
(v
, n_expr
, n_type
)
1548 _stype
= n_type
.stype
1549 _is_typed
= _stype
!= null
1553 redef class AProxyExpr
1554 redef meth after_typing
(v
)
1556 if not n_expr
.is_typed
then return
1558 if n_expr
.is_statement
then return
1559 _stype
= n_expr
.stype
1563 redef class AOnceExpr
1564 redef meth accept_typing
(v
)
1566 if v
.once_count
> 0 then
1567 v
.warning
(self, "Useless once in a once expression.")
1569 v
.once_count
= v
.once_count
+ 1
1573 v
.once_count
= v
.once_count
- 1