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
68 if ctx
!= null then variable_ctx
= ctx
71 # Make the if_false_variable_ctx of the expression effective
72 private meth use_if_false_variable_ctx
(e
: PExpr)
74 var ctx
= e
.if_false_variable_ctx
75 if ctx
!= null then variable_ctx
= ctx
78 # Number of nested once
79 readable writable attr _once_count
: Int = 0
81 init(tc
, module) do super
83 private meth get_default_constructor_for
(n
: PNode, c
: MMLocalClass, prop
: MMSrcMethod): MMMethod
86 #var prop = v.local_property
87 #assert prop isa MMMethod
88 var candidates
= new Array[MMMethod]
89 var false_candidates
= new Array[MMMethod]
90 var parity
= prop
.signature
.arity
91 for g
in c
.global_properties
do
92 if not g
.is_init_for
(c
) then continue
94 var gps
= gp
.signature_for
(c
.get_type
)
95 assert gp
isa MMSrcMethod
96 var garity
= gps
.arity
97 if prop
!= null and gp
.name
== prop
.name
then
98 if garity
== 0 or (parity
== garity
and prop
.signature
< gps
) then
101 false_candidates
.add
(gp
)
103 else if garity
== 0 and gp
.name
== once
("init".to_symbol
) then
105 false_candidates
.add
(gp
)
107 false_candidates
.add
(gp
)
110 if candidates
.length
== 1 then
111 return candidates
.first
112 else if candidates
.length
> 0 then
113 var a
= new Array[String]
114 for p
in candidates
do
115 a
.add
("{p.full_name}{p.signature}")
117 v
.error
(n
, "Error: Conflicting default constructor to call for {c}: {a.join(", ")}.")
119 else if false_candidates
.length
> 0 then
120 var a
= new Array[String]
121 for p
in false_candidates
do
122 a
.add
("{p.full_name}{p.signature}")
124 v
.error
(n
, "Error: there is no available compatible constrctor in {c}. Discarded candidates are {a.join(", ")}.")
127 v
.error
(n
, "Error: there is no available compatible constrctor in {c}.")
134 ###############################################################################
137 private meth accept_typing
(v
: TypingVisitor)
139 accept_abs_syntax_visitor
(v
)
142 private meth after_typing
(v
: TypingVisitor) do end
145 redef class PClassdef
146 redef meth accept_typing
(v
)
148 v
.self_var
= new ParamVariable("self".to_symbol
, self)
149 v
.self_var
.stype
= local_class
.get_type
154 redef class AAttrPropdef
155 redef meth accept_typing
(v
)
158 if n_expr
!= null then
159 v
.check_conform_expr
(n_expr
, prop
.signature
.return_type
)
164 redef class AMethPropdef
165 redef readable attr _self_var
: ParamVariable
166 redef meth accept_typing
(v
)
168 v
.variable_ctx
= new RootVariableContext(v
, self)
169 _self_var
= v
.self_var
174 redef class AConcreteMethPropdef
175 redef meth accept_typing
(v
)
178 if v
.variable_ctx
.unreash
== false and method
.signature
.return_type
!= null then
179 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
184 redef class AConcreteInitPropdef
185 readable attr _super_init_calls
: Array[MMMethod] = new Array[MMMethod]
186 readable attr _explicit_super_init_calls
: Array[MMMethod] = new Array[MMMethod]
187 redef meth accept_typing
(v
)
189 v
.top_block
= n_block
190 v
.explicit_super_init_calls
= explicit_super_init_calls
191 v
.explicit_other_init_call
= false
193 if v
.explicit_other_init_call
or method
.global
.intro
!= method
then
197 var l
= explicit_super_init_calls
.length
198 var cur_m
: MMMethod = null
199 var cur_c
: MMLocalClass = null
201 cur_m
= explicit_super_init_calls
[i
]
202 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
205 while j
< v
.local_class
.cshe
.direct_greaters
.length
do
206 var c
= v
.local_class
.cshe
.direct_greaters
[j
]
207 if c
.global
.is_interface
or c
.global
.is_universal
or c
.global
.is_mixin
then
209 else if cur_c
!= null and (c
.cshe
<= cur_c
or cur_c
.global
.is_mixin
) then
210 if c
== cur_c
then j
+= 1
211 super_init_calls
.add
(cur_m
)
214 cur_m
= explicit_super_init_calls
[i
]
215 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
221 var p
= v
.get_default_constructor_for
(self, c
, method
)
223 super_init_calls
.add
(p
)
233 redef meth after_typing
(v
)
235 # TODO: why the test?
236 if v
.variable_ctx
!= null then
237 v
.variable_ctx
.add
(variable
)
242 redef class AClosureDecl
243 # The corresponding escapable object
244 readable attr _escapable
: EscapableBlock
246 redef meth accept_typing
(v
)
248 # Register the closure for ClosureCallExpr
249 v
.variable_ctx
.add
(variable
)
251 var old_var_ctx
= v
.variable_ctx
252 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
254 _escapable
= new EscapableClosure(self, variable
.closure
, null)
255 v
.escapable_ctx
.push
(_escapable
)
259 if n_expr
!= null then
260 if v
.variable_ctx
.unreash
== false then
261 if variable
.closure
.signature
.return_type
!= null then
262 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
263 else if variable
.closure
.is_break
then
264 v
.error
(self, "Control error: Reached end of break block (an 'abort' was expected).")
269 old_var_ctx
.merge
(v
.variable_ctx
)
270 v
.variable_ctx
= old_var_ctx
276 readable attr _stype
: MMType
277 redef meth after_typing
(v
)
279 _stype
= get_stype
(v
)
284 redef readable attr _is_typed
: Bool = false
285 redef meth is_statement
: Bool do return _stype
== null
289 print
"{locate}: not is_typed"
293 print
"{locate}: is_statement"
300 # Is the expression the implicit receiver
301 meth is_implicit_self
: Bool do return false
303 # Is the expression the current receiver (implicit or explicit)
304 meth is_self
: Bool do return false
306 # The variable accessed is any
307 meth its_variable
: Variable do return null
309 # The variable type information if current boolean expression is true
310 readable private attr _if_true_variable_ctx
: VariableContext
312 # The variable type information if current boolean expression is false
313 readable private attr _if_false_variable_ctx
: VariableContext
316 redef class AVardeclExpr
317 redef meth after_typing
(v
)
319 var va
= new VarVariable(n_id
.to_symbol
, self)
321 v
.variable_ctx
.add
(va
)
322 if n_expr
!= null then v
.variable_ctx
.mark_is_set
(va
)
324 if n_type
!= null then
325 va
.stype
= n_type
.stype
326 if n_expr
!= null then
327 v
.check_conform_expr
(n_expr
, va
.stype
)
330 if not v
.check_expr
(n_expr
) then return
331 va
.stype
= n_expr
.stype
337 redef class ABlockExpr
338 redef meth accept_typing
(v
)
340 var old_var_ctx
= v
.variable_ctx
341 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
344 if v
.variable_ctx
.unreash
and not v
.variable_ctx
.already_unreash
then
345 v
.variable_ctx
.already_unreash
= true
346 v
.warning
(e
, "Warning: unreachable statement.")
351 old_var_ctx
.merge
(v
.variable_ctx
)
352 v
.variable_ctx
= old_var_ctx
357 redef class AReturnExpr
358 redef meth after_typing
(v
)
360 v
.variable_ctx
.unreash
= true
361 var t
= v
.local_property
.signature
.return_type
362 if n_expr
== null and t
!= null then
363 v
.error
(self, "Error: Return without value in a function.")
364 else if n_expr
!= null and t
== null then
365 v
.error
(self, "Error: Return with value in a procedure.")
366 else if n_expr
!= null and t
!= null then
367 v
.check_conform_expr
(n_expr
, t
)
373 redef class AContinueExpr
374 redef meth after_typing
(v
)
376 v
.variable_ctx
.unreash
= true
377 var esc
= compute_escapable_block
(v
.escapable_ctx
)
378 if esc
== null then return
380 if esc
.is_break_block
then
381 v
.error
(self, "Error: 'continue' forbiden in break blocks.")
385 var t
= esc
.continue_stype
386 if n_expr
== null and t
!= null then
387 v
.error
(self, "Error: continue with a value required in this block.")
388 else if n_expr
!= null and t
== null then
389 v
.error
(self, "Error: continue without value required in this block.")
390 else if n_expr
!= null and t
!= null then
391 v
.check_conform_expr
(n_expr
, t
)
397 redef class ABreakExpr
398 redef meth after_typing
(v
)
400 v
.variable_ctx
.unreash
= true
401 var esc
= compute_escapable_block
(v
.escapable_ctx
)
402 if esc
== null then return
404 var bl
= esc
.break_list
405 if n_expr
== null and bl
!= null then
406 v
.error
(self, "Error: break with a value required in this block.")
407 else if n_expr
!= null and bl
== null then
408 v
.error
(self, "Error: break without value required in this block.")
409 else if n_expr
!= null and bl
!= null then
410 # Typing check can only be done later
417 redef class AAbortExpr
418 redef meth after_typing
(v
)
420 v
.variable_ctx
.unreash
= true
425 redef meth accept_typing
(v
)
427 var old_var_ctx
= v
.variable_ctx
429 v
.check_conform_expr
(n_expr
, v
.type_bool
)
431 # Prepare 'then' context
432 v
.use_if_true_variable_ctx
(n_expr
)
435 if n_then
!= null then
436 v
.variable_ctx
= v
.variable_ctx
.sub
(n_then
)
440 # Remember what appened in the 'then'
441 var then_var_ctx
= v
.variable_ctx
443 # Prepare 'else' context
444 v
.variable_ctx
= old_var_ctx
445 v
.use_if_false_variable_ctx
(n_expr
)
448 if n_else
!= null then
449 v
.variable_ctx
= v
.variable_ctx
.sub
(n_else
)
453 # Merge 'then' and 'else' contexts
454 old_var_ctx
.merge2
(then_var_ctx
, v
.variable_ctx
)
455 v
.variable_ctx
= old_var_ctx
460 redef class AWhileExpr
461 # The corresponding escapable block
462 readable attr _escapable
: EscapableBlock
464 redef meth accept_typing
(v
)
466 _escapable
= new EscapableBlock(self)
467 v
.escapable_ctx
.push
(_escapable
)
468 var old_var_ctx
= v
.variable_ctx
469 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
473 v
.check_conform_expr
(n_expr
, v
.type_bool
)
474 v
.variable_ctx
= old_var_ctx
481 # The corresponding escapable block
482 readable attr _escapable
: EscapableBlock
484 readable attr _meth_iterator
: MMMethod
485 readable attr _meth_is_ok
: MMMethod
486 readable attr _meth_item
: MMMethod
487 readable attr _meth_next
: MMMethod
488 redef meth accept_typing
(v
)
490 _escapable
= new EscapableBlock(self)
491 v
.escapable_ctx
.push
(_escapable
)
493 var old_var_ctx
= v
.variable_ctx
494 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
495 var va
= new AutoVariable(n_id
.to_symbol
, self)
497 v
.variable_ctx
.add
(va
)
501 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then return
502 var expr_type
= n_expr
.stype
503 _meth_iterator
= expr_type
.local_class
.select_method
(once
("iterator".to_symbol
))
504 if _meth_iterator
== null then
505 v
.error
(self, "Error: Collection MUST have an iterate method")
508 var iter_type
= _meth_iterator
.signature_for
(expr_type
).return_type
509 _meth_is_ok
= iter_type
.local_class
.select_method
(once
("is_ok".to_symbol
))
510 if _meth_is_ok
== null then
511 v
.error
(self, "Error: {iter_type} MUST have an is_ok method")
514 _meth_item
= iter_type
.local_class
.select_method
(once
("item".to_symbol
))
515 if _meth_item
== null then
516 v
.error
(self, "Error: {iter_type} MUST have an item method")
519 _meth_next
= iter_type
.local_class
.select_method
(once
("next".to_symbol
))
520 if _meth_next
== null then
521 v
.error
(self, "Error: {iter_type} MUST have a next method")
524 var t
= _meth_item
.signature_for
(iter_type
).return_type
525 if not n_expr
.is_self
then t
= t
.not_for_self
528 if n_block
!= null then v
.visit
(n_block
)
531 v
.variable_ctx
= old_var_ctx
537 redef class AAssertExpr
538 redef meth after_typing
(v
)
540 v
.check_conform_expr
(n_expr
, v
.type_bool
)
541 v
.use_if_true_variable_ctx
(n_expr
)
547 redef meth its_variable
do return variable
549 redef meth after_typing
(v
)
551 v
.variable_ctx
.check_is_set
(self, variable
)
552 _stype
= v
.variable_ctx
.stype
(variable
)
553 _is_typed
= _stype
!= null
557 redef class AVarAssignExpr
558 redef meth after_typing
(v
)
560 v
.variable_ctx
.mark_is_set
(variable
)
561 var t
= v
.variable_ctx
.stype
(variable
)
562 if v
.check_conform_expr
(n_value
, variable
.stype
) then
563 # Fall back to base type if current type does not match
564 if not n_value
.stype
< t
then
565 v
.variable_ctx
.stype
(variable
) = variable
.stype
572 redef class AReassignFormExpr
573 # Compute and check method used through the reassigment operator
574 # On success return the static type of the result of the reassigment operator
575 # Else display an error and return null
576 private meth do_rvalue_typing
(v
: TypingVisitor, type_lvalue
: MMType): MMType
578 if type_lvalue
== null then
581 var name
= n_assign_op
.method_name
582 var lc
= type_lvalue
.local_class
583 if not lc
.has_global_property_by_name
(name
) then
584 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
587 var prop
= lc
.select_method
(name
)
588 prop
.global
.check_visibility
(v
, self, v
.module, false)
589 var psig
= prop
.signature_for
(type_lvalue
)
590 _assign_method
= prop
591 if not v
.check_conform_expr
(n_value
, psig
[0].not_for_self
) then return null
592 return psig
.return_type
.not_for_self
595 # Method used through the reassigment operator (once computed)
596 readable attr _assign_method
: MMMethod
599 redef class AVarReassignExpr
600 redef meth after_typing
(v
)
602 v
.variable_ctx
.check_is_set
(self, variable
)
603 v
.variable_ctx
.mark_is_set
(variable
)
604 var t
= v
.variable_ctx
.stype
(variable
)
605 var t2
= do_rvalue_typing
(v
, t
)
606 if t2
== null then return
607 if v
.check_conform
(self, t2
, variable
.stype
) then
608 # Fall back to base type if current type does not match
610 v
.variable_ctx
.stype
(variable
) = variable
.stype
617 redef class PAssignOp
618 meth method_name
: Symbol is abstract
620 redef class APlusAssignOp
621 redef meth method_name
do return once
"+".to_symbol
623 redef class AMinusAssignOp
624 redef meth method_name
do return once
"-".to_symbol
627 redef class ASelfExpr
628 redef meth its_variable
do return variable
630 redef meth after_typing
(v
)
632 variable
= v
.self_var
633 _stype
= v
.variable_ctx
.stype
(variable
)
637 redef meth is_self
do return true
640 redef class AImplicitSelfExpr
641 redef meth is_implicit_self
do return true
644 redef class AIfexprExpr
645 redef meth accept_typing
(v
)
647 var old_var_ctx
= v
.variable_ctx
650 v
.use_if_true_variable_ctx
(n_expr
)
652 v
.variable_ctx
= old_var_ctx
653 v
.use_if_false_variable_ctx
(n_expr
)
656 v
.check_conform_expr
(n_expr
, v
.type_bool
)
658 _stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
659 _is_typed
= _stype
!= null
663 redef class ABoolExpr
664 redef meth after_typing
(v
)
672 redef meth after_typing
(v
)
674 var old_var_ctx
= v
.variable_ctx
677 v
.use_if_false_variable_ctx
(n_expr
)
680 if n_expr2
.if_false_variable_ctx
!= null then
681 _if_false_variable_ctx
= n_expr2
.if_false_variable_ctx
683 _if_false_variable_ctx
= v
.variable_ctx
686 v
.variable_ctx
= old_var_ctx
688 v
.check_conform_expr
(n_expr
, v
.type_bool
)
689 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
696 redef meth accept_typing
(v
)
698 var old_var_ctx
= v
.variable_ctx
701 v
.use_if_true_variable_ctx
(n_expr
)
704 if n_expr2
.if_true_variable_ctx
!= null then
705 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
707 _if_true_variable_ctx
= v
.variable_ctx
710 v
.variable_ctx
= old_var_ctx
712 v
.check_conform_expr
(n_expr
, v
.type_bool
)
713 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
720 redef meth after_typing
(v
)
722 v
.check_conform_expr
(n_expr
, v
.type_bool
)
724 # Invert if_true/if_false information
725 _if_false_variable_ctx
= n_expr
._if_true_variable_ctx
726 _if_true_variable_ctx
= n_expr
._if_false_variable_ctx
734 redef meth after_typing
(v
)
741 redef class AFloatExpr
742 redef meth after_typing
(v
)
744 _stype
= v
.type_float
749 redef class ACharExpr
750 redef meth after_typing
(v
)
757 redef class AStringFormExpr
758 readable attr _meth_with_native
: MMMethod
759 redef meth after_typing
(v
)
761 _stype
= v
.type_string
763 _meth_with_native
= _stype
.local_class
.select_method
(once
"with_native".to_symbol
)
764 if _meth_with_native
== null then v
.error
(self, "{_stype} MUST have a with_native method.")
768 redef class ASuperstringExpr
769 readable attr _meth_with_capacity
: MMMethod
770 readable attr _meth_add
: MMMethod
771 readable attr _meth_to_s
: MMMethod
772 readable attr _atype
: MMType
773 redef meth after_typing
(v
)
775 _stype
= v
.type_string
776 _atype
= v
.type_array
(_stype
)
777 _meth_with_capacity
= _atype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
778 if _meth_with_capacity
== null then v
.error
(self, "{_atype} MUST have a with_capacity method.")
779 _meth_add
= _atype
.local_class
.select_method
(once
"add".to_symbol
)
780 if _meth_add
== null then v
.error
(self, "{_atype} MUST have an add method.")
781 _meth_to_s
= v
.type_object
.local_class
.select_method
(once
"to_s".to_symbol
)
782 if _meth_to_s
== null then v
.error
(self, "Object MUST have a to_s method.")
787 redef class ANullExpr
788 redef meth after_typing
(v
)
795 redef class AArrayExpr
796 readable attr _meth_with_capacity
: MMMethod
797 readable attr _meth_add
: MMMethod
799 redef meth after_typing
(v
)
801 var stype
= v
.check_conform_multiexpr
(null, n_exprs
)
802 if stype
== null then return
806 private meth do_typing
(v
: TypingVisitor, element_type
: MMType)
808 _stype
= v
.type_array
(element_type
)
810 _meth_with_capacity
= _stype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
811 if _meth_with_capacity
== null then v
.error
(self, "{_stype} MUST have a with_capacity method.")
812 _meth_add
= _stype
.local_class
.select_method
(once
"add".to_symbol
)
813 if _meth_add
== null then v
.error
(self, "{_stype} MUST have an add method.")
819 redef class ARangeExpr
820 readable attr _meth_init
: MMMethod
821 redef meth after_typing
(v
)
823 if not v
.check_expr
(n_expr
) or not v
.check_expr
(n_expr2
) then return
824 var ntype
= n_expr
.stype
825 var ntype2
= n_expr2
.stype
826 if ntype
< ntype2
then
828 else if not ntype2
< ntype
then
829 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
832 var dtype
= v
.type_discrete
833 if not v
.check_conform_expr
(n_expr
, dtype
) or not v
.check_conform_expr
(n_expr2
, dtype
) then return
834 _stype
= v
.type_range
(ntype
)
839 redef class ACrangeExpr
840 redef meth after_typing
(v
)
843 _meth_init
= stype
.local_class
.select_method
(once
"init".to_symbol
)
846 redef class AOrangeExpr
847 redef meth after_typing
(v
)
850 _meth_init
= stype
.local_class
.select_method
(once
"without_last".to_symbol
)
855 redef class ASuperExpr
856 special ASuperInitCall
857 # readable attr _prop: MMSrcMethod
858 readable attr _init_in_superclass
: MMMethod
859 redef meth after_typing
(v
)
861 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
862 if not precs
.is_empty
then
863 v
.local_property
.need_super
= true
864 else if v
.local_property
.global
.is_init
then
865 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
866 for p
in base_precs
do
867 if not p
.global
.is_init
then
868 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
870 precs
.add
(v
.local_class
[p
.global
])
873 if precs
.is_empty
then
874 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
876 else if precs
.length
> 1 then
877 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
880 var p
= base_precs
.first
881 assert p
isa MMMethod
882 _init_in_superclass
= p
883 register_super_init_call
(v
, p
)
884 if n_args
.length
> 0 then
885 var signature
= get_signature
(v
, v
.self_var
.stype
, p
, true)
886 _arguments
= process_signature
(v
, signature
, p
.name
, n_args
.to_a
)
889 v
.error
(self, "Error: No super method to call for {v.local_property}.")
893 if precs
.first
.signature_for
(v
.self_var
.stype
).return_type
!= null then
894 var stypes
= new Array[MMType]
895 var stype
: MMType = null
897 assert prop
isa MMMethod
898 var t
= prop
.signature_for
(v
.self_var
.stype
).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
900 if stype
== null or stype
< t
then
905 v
.check_conform
(self, t
, stype
)
909 var p
= v
.local_property
910 assert p
isa MMSrcMethod
916 redef class AAttrFormExpr
918 readable attr _prop
: MMAttribute
920 # Attribute type of the acceded attribute
921 readable attr _attr_type
: MMType
923 # Compute the attribute accessed
924 private meth do_typing
(v
: TypingVisitor)
926 if not v
.check_expr
(n_expr
) then return
927 var type_recv
= n_expr
.stype
928 var name
= n_id
.to_symbol
929 var lc
= type_recv
.local_class
930 if not lc
.has_global_property_by_name
(name
) then
931 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
934 var prop
= lc
.select_attribute
(name
)
935 if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
936 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
939 var at
= prop
.signature_for
(type_recv
).return_type
940 if not n_expr
.is_self
then at
= at
.not_for_self
945 redef class AAttrExpr
946 redef meth after_typing
(v
)
949 if prop
== null then return
955 redef class AAttrAssignExpr
956 redef meth after_typing
(v
)
959 if prop
== null then return
960 if not v
.check_conform_expr
(n_value
, attr_type
) then return
965 redef class AAttrReassignExpr
966 redef meth after_typing
(v
)
969 if prop
== null then return
970 var t
= do_rvalue_typing
(v
, attr_type
)
971 if t
== null then return
972 v
.check_conform
(self, t
, n_value
.stype
)
977 class AAbsAbsSendExpr
979 # The signature of the called property
980 readable attr _prop_signature
: MMSignature
982 # The real arguments used (after star transformation) (once computed)
983 readable attr _arguments
: Array[PExpr]
985 # Check the conformity of a set of arguments `raw_args' to a signature.
986 private meth process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: Array[PExpr]): Array[PExpr]
988 var par_vararg
= psig
.vararg_rank
989 var par_arity
= psig
.arity
991 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
992 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
993 v
.error
(self, "Error: '{name}' arity missmatch.")
997 var args
= new Array[PExpr]
998 for par_idx
in [0..par_arity
[ do
1000 var par_type
= psig
[par_idx
]
1001 if par_idx
== par_vararg
then
1002 var star
= new Array[PExpr]
1003 for i
in [0..(raw_arity-par_arity
)] do
1004 a
= raw_args
[arg_idx
]
1005 v
.check_conform_expr
(a
, par_type
)
1007 arg_idx
= arg_idx
+ 1
1009 var aa
= new AArrayExpr.init_aarrayexpr
(star
)
1010 aa
.do_typing
(v
, par_type
)
1013 a
= raw_args
[arg_idx
]
1014 v
.check_conform_expr
(a
, par_type
)
1015 arg_idx
= arg_idx
+ 1
1022 # Check the conformity of a set of defined closures
1023 private meth process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: Array[PClosureDef]): MMType
1025 var t
= psig
.return_type
1026 var cs
= psig
.closures
# Declared closures
1029 if not c
.is_optional
then min_arity
+= 1
1032 if cs
.length
== 0 then
1033 v
.error
(self, "Error: {name} does not require blocks.")
1034 else if cd
.length
> cs
.length
or cd
.length
< min_arity
then
1035 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
1037 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1038 var break_list
: Array[ABreakExpr] = null
1039 if t
!= null then break_list
= new Array[ABreakExpr]
1041 # Process each closure definition
1042 for i
in [0..cd
.length
[ do
1045 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1046 v
.escapable_ctx
.push
(esc
)
1047 cdi
.accept_typing2
(v
, esc
)
1051 # Check break type conformity
1052 if break_list
!= null then
1053 t
= v
.check_conform_multiexpr
(t
, break_list
)
1056 else if min_arity
!= 0 then
1057 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1064 special AAbsAbsSendExpr
1065 # Compute the called global property
1066 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])
1068 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
1069 if prop
== null then return
1070 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
1071 if sig
== null then return
1072 var args
= process_signature
(v
, sig
, prop
.name
, raw_args
)
1073 if args
== null then return
1074 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
1075 if rtype
== null and sig
.return_type
!= null then return
1077 _prop_signature
= sig
1079 _return_type
= rtype
1082 private meth get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): MMMethod
1084 if type_recv
== null then return null
1085 var lc
= type_recv
.local_class
1086 var prop
: MMMethod = null
1087 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
1088 if prop
== null and v
.local_property
.global
.is_init
then
1089 var props
= type_recv
.local_class
.super_methods_named
(name
)
1090 if props
.length
> 1 then
1091 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1093 else if props
.length
== 1 then
1094 var p
= type_recv
.local_class
[props
.first
.global
]
1095 assert p
isa MMMethod
1100 if prop
== null then
1101 if is_implicit_self
then
1102 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1104 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1111 # Get the signature for a local property and a receiver
1112 private meth get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
1114 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
1115 var psig
= prop
.signature_for
(type_recv
)
1116 if not recv_is_self
then psig
= psig
.not_for_self
1120 # The invoked method (once computed)
1121 readable attr _prop
: MMMethod
1123 # The return type (if any) (once computed)
1124 readable attr _return_type
: MMType
1127 # A possible call of constructor in a super class
1128 # Could be an explicit call or with the 'super' keyword
1129 class ASuperInitCall
1130 special AAbsSendExpr
1131 private meth register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1133 if parent
!= v
.top_block
and self != v
.top_block
then
1134 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1136 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
1137 var prev_class
: MMLocalClass = null
1138 if not v
.explicit_super_init_calls
.is_empty
then
1139 prev_class
= v
.explicit_super_init_calls
.last
.global
.intro
.local_class
1141 var order
= v
.local_class
.cshe
.reverse_linear_extension
1142 if cla
== v
.local_class
then
1143 v
.explicit_other_init_call
= true
1144 else if not order
.has
(cla
) then
1145 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1146 else if cla
== prev_class
then
1147 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1149 var last_is_found
= prev_class
== null
1151 if c
== prev_class
then
1152 last_is_found
= true
1153 else if c
== cla
then
1154 if not last_is_found
then
1155 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1157 v
.explicit_super_init_calls
.add
(property
)
1166 redef class ANewExpr
1167 special AAbsSendExpr
1168 redef meth after_typing
(v
)
1170 var t
= n_type
.stype
1171 if t
== null then return
1172 if t
.local_class
.global
.is_abstract
then
1173 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1177 if n_id
== null then
1178 name
= once
"init".to_symbol
1180 name
= n_id
.to_symbol
1183 do_typing
(v
, t
, false, false, name
, n_args
.to_a
, null)
1184 if prop
== null then return
1186 if not prop
.global
.is_init
then
1187 v
.error
(self, "Error: {prop} is not a constructor.")
1196 redef class ASendExpr
1197 special ASuperInitCall
1198 # Name of the invoked property
1199 meth name
: Symbol is abstract
1201 # Raw arguments used (withour star transformation)
1202 meth raw_arguments
: Array[PExpr] is abstract
1204 # Closure definitions
1205 meth closure_defs
: Array[PClosureDef] do return null
1207 redef meth after_typing
(v
)
1212 private meth do_all_typing
(v
: TypingVisitor)
1214 if not v
.check_expr
(n_expr
) then return
1215 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1216 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'.")
1224 register_super_init_call
(v
, prop
)
1228 _stype
= return_type
1233 class ASendReassignExpr
1235 special AReassignFormExpr
1236 readable attr _read_prop
: MMMethod
1237 redef meth do_all_typing
(v
)
1239 if not v
.check_expr
(n_expr
) then return
1240 var raw_args
= raw_arguments
1241 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1242 if prop
== null then return
1243 if prop
.global
.is_init
then
1244 if not v
.local_property
.global
.is_init
then
1245 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1246 else if not n_expr
.is_self
then
1247 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1250 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1251 if not n_expr
.is_self
then t
= t
.not_for_self
1253 var t2
= do_rvalue_typing
(v
, t
)
1254 if t2
== null then return
1255 v
.check_conform
(self, t2
, n_value
.stype
)
1258 var old_args
= arguments
1259 raw_args
.add
(n_value
)
1261 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1262 if prop
== null then return
1263 if prop
.global
.is_init
then
1264 if not v
.local_property
.global
.is_init
then
1265 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1266 else if not n_expr
.is_self
then
1267 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1271 _arguments
= old_args
# FIXME: What if star parameters do not match betwen the two methods?
1276 redef class ABinopExpr
1277 redef meth raw_arguments
do return [n_expr2
]
1280 redef meth name
do return once
"==".to_symbol
1283 redef meth name
do return once
"!=".to_symbol
1286 redef meth name
do return once
"<".to_symbol
1289 redef meth name
do return once
"<=".to_symbol
1292 redef meth name
do return once
">".to_symbol
1295 redef meth name
do return once
">=".to_symbol
1297 redef class APlusExpr
1298 redef meth name
do return once
"+".to_symbol
1300 redef class AMinusExpr
1301 redef meth name
do return once
"-".to_symbol
1303 redef class AStarshipExpr
1304 redef meth name
do return once
"<=>".to_symbol
1306 redef class AStarExpr
1307 redef meth name
do return once
"*".to_symbol
1309 redef class ASlashExpr
1310 redef meth name
do return once
"/".to_symbol
1312 redef class APercentExpr
1313 redef meth name
do return once
"%".to_symbol
1316 redef class AUminusExpr
1317 redef meth name
do return once
"unary -".to_symbol
1318 redef meth raw_arguments
do return null
1321 redef class ACallFormExpr
1322 redef meth after_typing
(v
)
1324 if n_expr
!= null and n_expr
.is_implicit_self
then
1325 var name
= n_id
.to_symbol
1326 var variable
= v
.variable_ctx
[name
]
1327 if variable
!= null then
1328 if variable
isa ClosureVariable then
1329 var n
= new AClosureCallExpr.init_aclosurecallexpr
(n_id
, n_args
, n_closure_defs
)
1331 n
.variable
= variable
1335 if not n_args
.is_empty
then
1336 v
.error
(self, "Error: {name} is variable, not a function.")
1339 var vform
= variable_create
(variable
)
1340 vform
.variable
= variable
1342 vform
.after_typing
(v
)
1351 redef meth closure_defs
1353 if n_closure_defs
== null or n_closure_defs
.is_empty
then
1356 return n_closure_defs
.to_a
1360 # Create a variable acces corresponding to the call form
1361 meth variable_create
(variable
: Variable): AVarFormExpr is abstract
1364 redef class ACallExpr
1365 redef meth variable_create
(variable
)
1367 return new AVarExpr.init_avarexpr
(n_id
)
1370 redef meth name
do return n_id
.to_symbol
1371 redef meth raw_arguments
do return n_args
.to_a
1374 redef class ACallAssignExpr
1375 redef meth variable_create
(variable
)
1377 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1380 redef meth name
do return (n_id
.text
+ "=").to_symbol
1381 redef meth raw_arguments
do
1382 var res
= n_args
.to_a
1388 redef class ACallReassignExpr
1389 special ASendReassignExpr
1390 redef meth variable_create
(variable
)
1392 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1395 redef meth name
do return n_id
.to_symbol
1396 redef meth raw_arguments
do return n_args
.to_a
1399 redef class ABraExpr
1400 redef meth name
do return once
"[]".to_symbol
1401 redef meth raw_arguments
do return n_args
.to_a
1404 redef class ABraAssignExpr
1405 redef meth name
do return once
"[]=".to_symbol
1406 redef meth raw_arguments
do
1407 var res
= n_args
.to_a
1413 redef class ABraReassignExpr
1414 special ASendReassignExpr
1415 redef meth name
do return once
"[]".to_symbol
1416 redef meth raw_arguments
do return n_args
.to_a
1419 redef class AInitExpr
1420 redef meth name
do return once
"init".to_symbol
1421 redef meth raw_arguments
do return n_args
.to_a
1424 redef class AClosureCallExpr
1425 special AAbsAbsSendExpr
1426 redef meth after_typing
(v
)
1429 if va
.closure
.is_break
then v
.variable_ctx
.unreash
= true
1430 var sig
= va
.closure
.signature
1431 var args
= process_signature
(v
, sig
, n_id
.to_symbol
, n_args
.to_a
)
1432 if not n_closure_defs
.is_empty
then
1433 process_closures
(v
, sig
, n_id
.to_symbol
, n_closure_defs
.to_a
)
1435 if args
== null then return
1436 _prop_signature
= sig
1438 _stype
= sig
.return_type
1443 redef class PClosureDef
1444 # The corresponding escapable object
1445 readable attr _escapable
: EscapableBlock
1447 attr _accept_typing2
: Bool
1448 redef meth accept_typing
(v
)
1450 # Typing is deferred, wait accept_typing2(v)
1451 if _accept_typing2
then super
1454 private meth accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure) is abstract
1457 redef class AClosureDef
1458 redef meth accept_typing2
(v
, esc
)
1462 var sig
= esc
.closure
.signature
1463 if sig
.arity
!= n_id
.length
then
1464 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1468 closure
= esc
.closure
1470 var old_var_ctx
= v
.variable_ctx
1471 v
.variable_ctx
= v
.variable_ctx
.sub
(self)
1472 variables
= new Array[AutoVariable]
1473 for i
in [0..n_id
.length
[ do
1474 var va
= new AutoVariable(n_id
[i
].to_symbol
, self)
1477 v
.variable_ctx
.add
(va
)
1480 _accept_typing2
= true
1483 if v
.variable_ctx
.unreash
== false then
1484 if closure
.signature
.return_type
!= null then
1485 v
.error
(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
1486 else if closure
.is_break
then
1487 v
.error
(self, "Control error: Reached end of break block (a 'break' was expected).")
1490 v
.variable_ctx
= old_var_ctx
1494 class ATypeCheckExpr
1496 private meth check_expr_cast
(v
: TypingVisitor, n_expr
: PExpr, n_type
: PType)
1498 if not v
.check_expr
(n_expr
) then return
1499 var etype
= n_expr
.stype
1500 var ttype
= n_type
.stype
1501 if etype
== ttype
then
1502 v
.warning
(self, "Warning: Expression is already a {ttype}.")
1503 else if etype
< ttype
then
1504 v
.warning
(self, "Warning: Expression is already a {ttype} since it is a {etype}.")
1509 redef class AIsaExpr
1510 special ATypeCheckExpr
1511 redef meth after_typing
(v
)
1513 check_expr_cast
(v
, n_expr
, n_type
)
1514 var variable
= n_expr
.its_variable
1515 if variable
!= null then
1516 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(self, variable
, n_type
.stype
)
1518 _stype
= v
.type_bool
1523 redef class AAsCastExpr
1524 special ATypeCheckExpr
1525 redef meth after_typing
(v
)
1527 check_expr_cast
(v
, n_expr
, n_type
)
1528 _stype
= n_type
.stype
1529 _is_typed
= _stype
!= null
1533 redef class AProxyExpr
1534 redef meth after_typing
(v
)
1536 if not n_expr
.is_typed
then return
1538 if n_expr
.is_statement
then return
1539 _stype
= n_expr
.stype
1543 redef class AOnceExpr
1544 redef meth accept_typing
(v
)
1546 if v
.once_count
> 0 then
1547 v
.warning
(self, "Useless once in a once expression.")
1549 v
.once_count
= v
.once_count
+ 1
1553 v
.once_count
= v
.once_count
- 1