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
)
484 # Prepare inside context (assert cond)
485 v
.use_if_true_variable_ctx
(n_expr
)
488 if n_block
!= null then
489 v
.variable_ctx
= v
.variable_ctx
.sub
(n_block
)
493 v
.variable_ctx
= old_var_ctx
494 v
.base_variable_ctx
= old_base_var_ctx
501 # The corresponding escapable block
502 readable attr _escapable
: EscapableBlock
504 readable attr _meth_iterator
: MMMethod
505 readable attr _meth_is_ok
: MMMethod
506 readable attr _meth_item
: MMMethod
507 readable attr _meth_next
: MMMethod
508 redef meth accept_typing
(v
)
510 _escapable
= new EscapableBlock(self)
511 v
.escapable_ctx
.push
(_escapable
)
513 var old_var_ctx
= v
.variable_ctx
514 var old_base_var_ctx
= v
.base_variable_ctx
515 v
.base_variable_ctx
= v
.variable_ctx
516 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
517 var va
= new AutoVariable(n_id
.to_symbol
, self)
519 v
.variable_ctx
.add
(va
)
523 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then return
524 var expr_type
= n_expr
.stype
525 _meth_iterator
= expr_type
.local_class
.select_method
(once
("iterator".to_symbol
))
526 if _meth_iterator
== null then
527 v
.error
(self, "Error: Collection MUST have an iterate method")
530 var iter_type
= _meth_iterator
.signature_for
(expr_type
).return_type
531 _meth_is_ok
= iter_type
.local_class
.select_method
(once
("is_ok".to_symbol
))
532 if _meth_is_ok
== null then
533 v
.error
(self, "Error: {iter_type} MUST have an is_ok method")
536 _meth_item
= iter_type
.local_class
.select_method
(once
("item".to_symbol
))
537 if _meth_item
== null then
538 v
.error
(self, "Error: {iter_type} MUST have an item method")
541 _meth_next
= iter_type
.local_class
.select_method
(once
("next".to_symbol
))
542 if _meth_next
== null then
543 v
.error
(self, "Error: {iter_type} MUST have a next method")
546 var t
= _meth_item
.signature_for
(iter_type
).return_type
547 if not n_expr
.is_self
then t
= t
.not_for_self
550 if n_block
!= null then v
.visit
(n_block
)
553 v
.variable_ctx
= old_var_ctx
554 v
.base_variable_ctx
= old_base_var_ctx
560 redef class AAssertExpr
561 redef meth after_typing
(v
)
563 v
.check_conform_expr
(n_expr
, v
.type_bool
)
564 v
.use_if_true_variable_ctx
(n_expr
)
570 redef meth its_variable
do return variable
572 redef meth after_typing
(v
)
574 v
.variable_ctx
.check_is_set
(self, variable
)
575 _stype
= v
.variable_ctx
.stype
(variable
)
576 _is_typed
= _stype
!= null
580 redef class AVarAssignExpr
581 redef meth after_typing
(v
)
583 v
.variable_ctx
.mark_is_set
(variable
)
584 var t
= v
.variable_ctx
.stype
(variable
)
586 # Check the base type
587 var btype
= v
.base_variable_ctx
.stype
(variable
)
588 if not v
.check_conform_expr
(n_value
, btype
) then return
591 v
.variable_ctx
.stype
(variable
) = n_value
.stype
597 redef class AReassignFormExpr
598 # Compute and check method used through the reassigment operator
599 # On success return the static type of the result of the reassigment operator
600 # Else display an error and return null
601 private meth do_rvalue_typing
(v
: TypingVisitor, type_lvalue
: MMType): MMType
603 if type_lvalue
== null then
606 var name
= n_assign_op
.method_name
607 var lc
= type_lvalue
.local_class
608 if not lc
.has_global_property_by_name
(name
) then
609 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
612 var prop
= lc
.select_method
(name
)
613 prop
.global
.check_visibility
(v
, self, v
.module, false)
614 var psig
= prop
.signature_for
(type_lvalue
)
615 _assign_method
= prop
616 if not v
.check_conform_expr
(n_value
, psig
[0].not_for_self
) then return null
617 return psig
.return_type
.not_for_self
620 # Method used through the reassigment operator (once computed)
621 readable attr _assign_method
: MMMethod
624 redef class AVarReassignExpr
625 redef meth after_typing
(v
)
627 v
.variable_ctx
.check_is_set
(self, variable
)
628 v
.variable_ctx
.mark_is_set
(variable
)
629 var t
= v
.variable_ctx
.stype
(variable
)
630 var t2
= do_rvalue_typing
(v
, t
)
631 if t2
== null then return
633 # Check the base type
634 var btype
= v
.base_variable_ctx
.stype
(variable
)
635 if not v
.check_conform
(n_value
, t2
, btype
) then return
638 v
.variable_ctx
.stype
(variable
) = t2
644 redef class PAssignOp
645 meth method_name
: Symbol is abstract
647 redef class APlusAssignOp
648 redef meth method_name
do return once
"+".to_symbol
650 redef class AMinusAssignOp
651 redef meth method_name
do return once
"-".to_symbol
654 redef class ASelfExpr
655 redef meth its_variable
do return variable
657 redef meth after_typing
(v
)
659 variable
= v
.self_var
660 _stype
= v
.variable_ctx
.stype
(variable
)
664 redef meth is_self
do return true
667 redef class AImplicitSelfExpr
668 redef meth is_implicit_self
do return true
671 redef class AIfexprExpr
672 redef meth accept_typing
(v
)
674 var old_var_ctx
= v
.variable_ctx
677 v
.use_if_true_variable_ctx
(n_expr
)
679 v
.variable_ctx
= old_var_ctx
680 v
.use_if_false_variable_ctx
(n_expr
)
683 v
.check_conform_expr
(n_expr
, v
.type_bool
)
685 _stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
686 _is_typed
= _stype
!= null
690 redef class ABoolExpr
691 redef meth after_typing
(v
)
699 redef meth accept_typing
(v
)
701 var old_var_ctx
= v
.variable_ctx
704 v
.use_if_false_variable_ctx
(n_expr
)
707 if n_expr2
.if_false_variable_ctx
!= null then
708 _if_false_variable_ctx
= n_expr2
.if_false_variable_ctx
710 _if_false_variable_ctx
= v
.variable_ctx
713 v
.variable_ctx
= old_var_ctx
715 v
.check_conform_expr
(n_expr
, v
.type_bool
)
716 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
723 redef meth accept_typing
(v
)
725 var old_var_ctx
= v
.variable_ctx
728 v
.use_if_true_variable_ctx
(n_expr
)
731 if n_expr2
.if_true_variable_ctx
!= null then
732 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
734 _if_true_variable_ctx
= v
.variable_ctx
737 v
.variable_ctx
= old_var_ctx
739 v
.check_conform_expr
(n_expr
, v
.type_bool
)
740 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
747 redef meth after_typing
(v
)
749 v
.check_conform_expr
(n_expr
, v
.type_bool
)
751 # Invert if_true/if_false information
752 _if_false_variable_ctx
= n_expr
._if_true_variable_ctx
753 _if_true_variable_ctx
= n_expr
._if_false_variable_ctx
761 redef meth after_typing
(v
)
768 redef class AFloatExpr
769 redef meth after_typing
(v
)
771 _stype
= v
.type_float
776 redef class ACharExpr
777 redef meth after_typing
(v
)
784 redef class AStringFormExpr
785 readable attr _meth_with_native
: MMMethod
786 redef meth after_typing
(v
)
788 _stype
= v
.type_string
790 _meth_with_native
= _stype
.local_class
.select_method
(once
"with_native".to_symbol
)
791 if _meth_with_native
== null then v
.error
(self, "{_stype} MUST have a with_native method.")
795 redef class ASuperstringExpr
796 readable attr _meth_with_capacity
: MMMethod
797 readable attr _meth_add
: MMMethod
798 readable attr _meth_to_s
: MMMethod
799 readable attr _atype
: MMType
800 redef meth after_typing
(v
)
802 _stype
= v
.type_string
803 _atype
= v
.type_array
(_stype
)
804 _meth_with_capacity
= _atype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
805 if _meth_with_capacity
== null then v
.error
(self, "{_atype} MUST have a with_capacity method.")
806 _meth_add
= _atype
.local_class
.select_method
(once
"add".to_symbol
)
807 if _meth_add
== null then v
.error
(self, "{_atype} MUST have an add method.")
808 _meth_to_s
= v
.type_object
.local_class
.select_method
(once
"to_s".to_symbol
)
809 if _meth_to_s
== null then v
.error
(self, "Object MUST have a to_s method.")
814 redef class ANullExpr
815 redef meth after_typing
(v
)
822 redef class AArrayExpr
823 readable attr _meth_with_capacity
: MMMethod
824 readable attr _meth_add
: MMMethod
826 redef meth after_typing
(v
)
828 var stype
= v
.check_conform_multiexpr
(null, n_exprs
)
829 if stype
== null then return
833 private meth do_typing
(v
: TypingVisitor, element_type
: MMType)
835 _stype
= v
.type_array
(element_type
)
837 _meth_with_capacity
= _stype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
838 if _meth_with_capacity
== null then v
.error
(self, "{_stype} MUST have a with_capacity method.")
839 _meth_add
= _stype
.local_class
.select_method
(once
"add".to_symbol
)
840 if _meth_add
== null then v
.error
(self, "{_stype} MUST have an add method.")
846 redef class ARangeExpr
847 readable attr _meth_init
: MMMethod
848 redef meth after_typing
(v
)
850 if not v
.check_expr
(n_expr
) or not v
.check_expr
(n_expr2
) then return
851 var ntype
= n_expr
.stype
852 var ntype2
= n_expr2
.stype
853 if ntype
< ntype2
then
855 else if not ntype2
< ntype
then
856 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
859 var dtype
= v
.type_discrete
860 if not v
.check_conform_expr
(n_expr
, dtype
) or not v
.check_conform_expr
(n_expr2
, dtype
) then return
861 _stype
= v
.type_range
(ntype
)
866 redef class ACrangeExpr
867 redef meth after_typing
(v
)
870 _meth_init
= stype
.local_class
.select_method
(once
"init".to_symbol
)
873 redef class AOrangeExpr
874 redef meth after_typing
(v
)
877 _meth_init
= stype
.local_class
.select_method
(once
"without_last".to_symbol
)
882 redef class ASuperExpr
883 special ASuperInitCall
884 # readable attr _prop: MMSrcMethod
885 readable attr _init_in_superclass
: MMMethod
886 redef meth after_typing
(v
)
888 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
889 if not precs
.is_empty
then
890 v
.local_property
.need_super
= true
891 else if v
.local_property
.global
.is_init
then
892 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
893 for p
in base_precs
do
894 if not p
.global
.is_init
then
895 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
897 precs
.add
(v
.local_class
[p
.global
])
900 if precs
.is_empty
then
901 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
903 else if precs
.length
> 1 then
904 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
907 var p
= base_precs
.first
908 assert p
isa MMMethod
909 _init_in_superclass
= p
910 register_super_init_call
(v
, p
)
911 if n_args
.length
> 0 then
912 var signature
= get_signature
(v
, v
.self_var
.stype
, p
, true)
913 _arguments
= process_signature
(v
, signature
, p
.name
, n_args
.to_a
)
916 v
.error
(self, "Error: No super method to call for {v.local_property}.")
920 if precs
.first
.signature_for
(v
.self_var
.stype
).return_type
!= null then
921 var stypes
= new Array[MMType]
922 var stype
: MMType = null
924 assert prop
isa MMMethod
925 var t
= prop
.signature_for
(v
.self_var
.stype
).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
927 if stype
== null or stype
< t
then
932 v
.check_conform
(self, t
, stype
)
936 var p
= v
.local_property
937 assert p
isa MMSrcMethod
943 redef class AAttrFormExpr
945 readable attr _prop
: MMAttribute
947 # Attribute type of the acceded attribute
948 readable attr _attr_type
: MMType
950 # Compute the attribute accessed
951 private meth do_typing
(v
: TypingVisitor)
953 if not v
.check_expr
(n_expr
) then return
954 var type_recv
= n_expr
.stype
955 var name
= n_id
.to_symbol
956 var lc
= type_recv
.local_class
957 if not lc
.has_global_property_by_name
(name
) then
958 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
961 var prop
= lc
.select_attribute
(name
)
962 if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
963 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
966 var at
= prop
.signature_for
(type_recv
).return_type
967 if not n_expr
.is_self
then at
= at
.not_for_self
972 redef class AAttrExpr
973 redef meth after_typing
(v
)
976 if prop
== null then return
982 redef class AAttrAssignExpr
983 redef meth after_typing
(v
)
986 if prop
== null then return
987 if not v
.check_conform_expr
(n_value
, attr_type
) then return
992 redef class AAttrReassignExpr
993 redef meth after_typing
(v
)
996 if prop
== null then return
997 var t
= do_rvalue_typing
(v
, attr_type
)
998 if t
== null then return
999 v
.check_conform
(self, t
, n_value
.stype
)
1004 class AAbsAbsSendExpr
1006 # The signature of the called property
1007 readable attr _prop_signature
: MMSignature
1009 # The real arguments used (after star transformation) (once computed)
1010 readable attr _arguments
: Array[PExpr]
1012 # Check the conformity of a set of arguments `raw_args' to a signature.
1013 private meth process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: Array[PExpr]): Array[PExpr]
1015 var par_vararg
= psig
.vararg_rank
1016 var par_arity
= psig
.arity
1018 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
1019 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
1020 v
.error
(self, "Error: '{name}' arity missmatch.")
1024 var args
= new Array[PExpr]
1025 for par_idx
in [0..par_arity
[ do
1027 var par_type
= psig
[par_idx
]
1028 if par_idx
== par_vararg
then
1029 var star
= new Array[PExpr]
1030 for i
in [0..(raw_arity-par_arity
)] do
1031 a
= raw_args
[arg_idx
]
1032 v
.check_conform_expr
(a
, par_type
)
1034 arg_idx
= arg_idx
+ 1
1036 var aa
= new AArrayExpr.init_aarrayexpr
(star
)
1037 aa
.do_typing
(v
, par_type
)
1040 a
= raw_args
[arg_idx
]
1041 v
.check_conform_expr
(a
, par_type
)
1042 arg_idx
= arg_idx
+ 1
1049 # Check the conformity of a set of defined closures
1050 private meth process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: Array[PClosureDef]): MMType
1052 var t
= psig
.return_type
1053 var cs
= psig
.closures
# Declared closures
1056 if not c
.is_optional
then min_arity
+= 1
1059 if cs
.length
== 0 then
1060 v
.error
(self, "Error: {name} does not require blocks.")
1061 else if cd
.length
> cs
.length
or cd
.length
< min_arity
then
1062 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
1064 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1065 var break_list
: Array[ABreakExpr] = null
1066 if t
!= null then break_list
= new Array[ABreakExpr]
1068 # Process each closure definition
1069 for i
in [0..cd
.length
[ do
1072 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1073 v
.escapable_ctx
.push
(esc
)
1074 cdi
.accept_typing2
(v
, esc
)
1078 # Check break type conformity
1079 if break_list
!= null then
1080 t
= v
.check_conform_multiexpr
(t
, break_list
)
1083 else if min_arity
!= 0 then
1084 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1091 special AAbsAbsSendExpr
1092 # Compute the called global property
1093 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])
1095 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
1096 if prop
== null then return
1097 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
1098 if sig
== null then return
1099 var args
= process_signature
(v
, sig
, prop
.name
, raw_args
)
1100 if args
== null then return
1101 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
1102 if rtype
== null and sig
.return_type
!= null then return
1104 _prop_signature
= sig
1106 _return_type
= rtype
1109 private meth get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): MMMethod
1111 if type_recv
== null then return null
1112 var lc
= type_recv
.local_class
1113 var prop
: MMMethod = null
1114 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
1115 if prop
== null and v
.local_property
.global
.is_init
then
1116 var props
= type_recv
.local_class
.super_methods_named
(name
)
1117 if props
.length
> 1 then
1118 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1120 else if props
.length
== 1 then
1121 var p
= type_recv
.local_class
[props
.first
.global
]
1122 assert p
isa MMMethod
1127 if prop
== null then
1128 if is_implicit_self
then
1129 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1131 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1138 # Get the signature for a local property and a receiver
1139 private meth get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
1141 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
1142 var psig
= prop
.signature_for
(type_recv
)
1143 if not recv_is_self
then psig
= psig
.not_for_self
1147 # The invoked method (once computed)
1148 readable attr _prop
: MMMethod
1150 # The return type (if any) (once computed)
1151 readable attr _return_type
: MMType
1154 # A possible call of constructor in a super class
1155 # Could be an explicit call or with the 'super' keyword
1156 class ASuperInitCall
1157 special AAbsSendExpr
1158 private meth register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1160 if parent
!= v
.top_block
and self != v
.top_block
then
1161 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1163 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
1164 var prev_class
: MMLocalClass = null
1165 if not v
.explicit_super_init_calls
.is_empty
then
1166 prev_class
= v
.explicit_super_init_calls
.last
.global
.intro
.local_class
1168 var order
= v
.local_class
.cshe
.reverse_linear_extension
1169 if cla
== v
.local_class
then
1170 v
.explicit_other_init_call
= true
1171 else if not order
.has
(cla
) then
1172 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1173 else if cla
== prev_class
then
1174 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1176 var last_is_found
= prev_class
== null
1178 if c
== prev_class
then
1179 last_is_found
= true
1180 else if c
== cla
then
1181 if not last_is_found
then
1182 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1184 v
.explicit_super_init_calls
.add
(property
)
1193 redef class ANewExpr
1194 special AAbsSendExpr
1195 redef meth after_typing
(v
)
1197 var t
= n_type
.stype
1198 if t
== null then return
1199 if t
.local_class
.global
.is_abstract
then
1200 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1204 if n_id
== null then
1205 name
= once
"init".to_symbol
1207 name
= n_id
.to_symbol
1210 do_typing
(v
, t
, false, false, name
, n_args
.to_a
, null)
1211 if prop
== null then return
1213 if not prop
.global
.is_init
then
1214 v
.error
(self, "Error: {prop} is not a constructor.")
1223 redef class ASendExpr
1224 special ASuperInitCall
1225 # Name of the invoked property
1226 meth name
: Symbol is abstract
1228 # Raw arguments used (withour star transformation)
1229 meth raw_arguments
: Array[PExpr] is abstract
1231 # Closure definitions
1232 meth closure_defs
: Array[PClosureDef] do return null
1234 redef meth after_typing
(v
)
1239 private meth do_all_typing
(v
: TypingVisitor)
1241 if not v
.check_expr
(n_expr
) then return
1242 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1243 if prop
== null then return
1245 if prop
.global
.is_init
then
1246 if not v
.local_property
.global
.is_init
then
1247 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1248 else if not n_expr
.is_self
then
1249 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1251 register_super_init_call
(v
, prop
)
1255 _stype
= return_type
1260 class ASendReassignExpr
1262 special AReassignFormExpr
1263 readable attr _read_prop
: MMMethod
1264 redef meth do_all_typing
(v
)
1266 if not v
.check_expr
(n_expr
) then return
1267 var raw_args
= raw_arguments
1268 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1269 if prop
== null then return
1270 if prop
.global
.is_init
then
1271 if not v
.local_property
.global
.is_init
then
1272 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1273 else if not n_expr
.is_self
then
1274 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1277 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1278 if not n_expr
.is_self
then t
= t
.not_for_self
1280 var t2
= do_rvalue_typing
(v
, t
)
1281 if t2
== null then return
1282 v
.check_conform
(self, t2
, n_value
.stype
)
1285 var old_args
= arguments
1286 raw_args
.add
(n_value
)
1288 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1289 if prop
== null then return
1290 if prop
.global
.is_init
then
1291 if not v
.local_property
.global
.is_init
then
1292 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1293 else if not n_expr
.is_self
then
1294 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1298 _arguments
= old_args
# FIXME: What if star parameters do not match betwen the two methods?
1303 redef class ABinopExpr
1304 redef meth raw_arguments
do return [n_expr2
]
1307 redef meth name
do return once
"==".to_symbol
1310 redef meth name
do return once
"!=".to_symbol
1313 redef meth name
do return once
"<".to_symbol
1316 redef meth name
do return once
"<=".to_symbol
1319 redef meth name
do return once
">".to_symbol
1322 redef meth name
do return once
">=".to_symbol
1324 redef class APlusExpr
1325 redef meth name
do return once
"+".to_symbol
1327 redef class AMinusExpr
1328 redef meth name
do return once
"-".to_symbol
1330 redef class AStarshipExpr
1331 redef meth name
do return once
"<=>".to_symbol
1333 redef class AStarExpr
1334 redef meth name
do return once
"*".to_symbol
1336 redef class ASlashExpr
1337 redef meth name
do return once
"/".to_symbol
1339 redef class APercentExpr
1340 redef meth name
do return once
"%".to_symbol
1343 redef class AUminusExpr
1344 redef meth name
do return once
"unary -".to_symbol
1345 redef meth raw_arguments
do return null
1348 redef class ACallFormExpr
1349 redef meth after_typing
(v
)
1351 if n_expr
!= null and n_expr
.is_implicit_self
then
1352 var name
= n_id
.to_symbol
1353 var variable
= v
.variable_ctx
[name
]
1354 if variable
!= null then
1355 if variable
isa ClosureVariable then
1356 var n
= new AClosureCallExpr.init_aclosurecallexpr
(n_id
, n_args
, n_closure_defs
)
1358 n
.variable
= variable
1362 if not n_args
.is_empty
then
1363 v
.error
(self, "Error: {name} is variable, not a function.")
1366 var vform
= variable_create
(variable
)
1367 vform
.variable
= variable
1369 vform
.after_typing
(v
)
1378 redef meth closure_defs
1380 if n_closure_defs
== null or n_closure_defs
.is_empty
then
1383 return n_closure_defs
.to_a
1387 # Create a variable acces corresponding to the call form
1388 meth variable_create
(variable
: Variable): AVarFormExpr is abstract
1391 redef class ACallExpr
1392 redef meth variable_create
(variable
)
1394 return new AVarExpr.init_avarexpr
(n_id
)
1397 redef meth name
do return n_id
.to_symbol
1398 redef meth raw_arguments
do return n_args
.to_a
1401 redef class ACallAssignExpr
1402 redef meth variable_create
(variable
)
1404 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1407 redef meth name
do return (n_id
.text
+ "=").to_symbol
1408 redef meth raw_arguments
do
1409 var res
= n_args
.to_a
1415 redef class ACallReassignExpr
1416 special ASendReassignExpr
1417 redef meth variable_create
(variable
)
1419 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1422 redef meth name
do return n_id
.to_symbol
1423 redef meth raw_arguments
do return n_args
.to_a
1426 redef class ABraExpr
1427 redef meth name
do return once
"[]".to_symbol
1428 redef meth raw_arguments
do return n_args
.to_a
1431 redef class ABraAssignExpr
1432 redef meth name
do return once
"[]=".to_symbol
1433 redef meth raw_arguments
do
1434 var res
= n_args
.to_a
1440 redef class ABraReassignExpr
1441 special ASendReassignExpr
1442 redef meth name
do return once
"[]".to_symbol
1443 redef meth raw_arguments
do return n_args
.to_a
1446 redef class AInitExpr
1447 redef meth name
do return once
"init".to_symbol
1448 redef meth raw_arguments
do return n_args
.to_a
1451 redef class AClosureCallExpr
1452 special AAbsAbsSendExpr
1453 redef meth after_typing
(v
)
1456 if va
.closure
.is_break
then v
.variable_ctx
.unreash
= true
1457 var sig
= va
.closure
.signature
1458 var args
= process_signature
(v
, sig
, n_id
.to_symbol
, n_args
.to_a
)
1459 if not n_closure_defs
.is_empty
then
1460 process_closures
(v
, sig
, n_id
.to_symbol
, n_closure_defs
.to_a
)
1462 if args
== null then return
1463 _prop_signature
= sig
1465 _stype
= sig
.return_type
1470 redef class PClosureDef
1471 # The corresponding escapable object
1472 readable attr _escapable
: EscapableBlock
1474 attr _accept_typing2
: Bool
1475 redef meth accept_typing
(v
)
1477 # Typing is deferred, wait accept_typing2(v)
1478 if _accept_typing2
then super
1481 private meth accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure) is abstract
1484 redef class AClosureDef
1485 redef meth accept_typing2
(v
, esc
)
1489 var sig
= esc
.closure
.signature
1490 if sig
.arity
!= n_id
.length
then
1491 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1495 closure
= esc
.closure
1497 var old_var_ctx
= v
.variable_ctx
1498 var old_base_var_ctx
= v
.base_variable_ctx
1499 v
.base_variable_ctx
= v
.variable_ctx
1500 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
1501 variables
= new Array[AutoVariable]
1502 for i
in [0..n_id
.length
[ do
1503 var va
= new AutoVariable(n_id
[i
].to_symbol
, self)
1506 v
.variable_ctx
.add
(va
)
1509 _accept_typing2
= true
1512 if v
.variable_ctx
.unreash
== false then
1513 if closure
.signature
.return_type
!= null then
1514 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
1515 else if closure
.is_break
then
1516 v
.error
(self, "Control error: Reached end of break block (a 'break' was expected).")
1519 v
.variable_ctx
= old_var_ctx
1520 v
.base_variable_ctx
= old_base_var_ctx
1524 class ATypeCheckExpr
1526 private meth check_expr_cast
(v
: TypingVisitor, n_expr
: PExpr, n_type
: PType)
1528 if not v
.check_expr
(n_expr
) then return
1529 var etype
= n_expr
.stype
1530 var ttype
= n_type
.stype
1531 if etype
== ttype
then
1532 v
.warning
(self, "Warning: Expression is already a {ttype}.")
1533 else if etype
< ttype
then
1534 v
.warning
(self, "Warning: Expression is already a {ttype} since it is a {etype}.")
1539 redef class AIsaExpr
1540 special ATypeCheckExpr
1541 redef meth after_typing
(v
)
1543 check_expr_cast
(v
, n_expr
, n_type
)
1544 var variable
= n_expr
.its_variable
1545 if variable
!= null then
1546 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, n_type
.stype
)
1548 _stype
= v
.type_bool
1553 redef class AAsCastExpr
1554 special ATypeCheckExpr
1555 redef meth after_typing
(v
)
1557 check_expr_cast
(v
, n_expr
, n_type
)
1558 _stype
= n_type
.stype
1559 _is_typed
= _stype
!= null
1563 redef class AProxyExpr
1564 redef meth after_typing
(v
)
1566 if not n_expr
.is_typed
then return
1568 if n_expr
.is_statement
then return
1569 _stype
= n_expr
.stype
1573 redef class AOnceExpr
1574 redef meth accept_typing
(v
)
1576 if v
.once_count
> 0 then
1577 v
.warning
(self, "Useless once in a once expression.")
1579 v
.once_count
= v
.once_count
+ 1
1583 v
.once_count
= v
.once_count
- 1