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
23 redef class MMSrcModule
24 # Walk trough the module and type statments and expressions
25 # Require than supermodules are processed
26 meth do_typing
(tc
: ToolContext)
28 var tv
= new TypingVisitor(tc
, self)
34 # * Associate local variables to nodes
35 # * Distinguish method call and local variable access
36 # * Resolve call and attribute access
37 # * Check type conformance
38 private class TypingVisitor
39 special AbsSyntaxVisitor
42 if n
!= null then n
.accept_typing
(self)
45 # Current knowledge about variables names and types
46 readable writable attr _variable_ctx
: VariableContext
48 # Current knowledge about escapable blocs
49 readable writable attr _escapable_ctx
: EscapableContext = new EscapableContext(self)
51 # The current reciever
52 readable writable attr _self_var
: ParamVariable
54 # Block of the current method
55 readable writable attr _top_block
: PExpr
57 # List of explicit invocation of constructors of super-classes
58 readable writable attr _explicit_super_init_calls
: Array[MMMethod]
60 # Is a other constructor of the same class invoked
61 readable writable attr _explicit_other_init_call
: Bool
63 init(tc
, module) do super
65 private meth get_default_constructor_for
(n
: PNode, c
: MMLocalClass, prop
: MMSrcMethod): MMMethod
68 #var prop = v.local_property
69 #assert prop isa MMMethod
70 var candidates
= new Array[MMMethod]
71 var false_candidates
= new Array[MMMethod]
72 var parity
= prop
.signature
.arity
73 for g
in c
.global_properties
do
74 if not g
.is_init_for
(c
) then continue
76 var gps
= gp
.signature_for
(c
.get_type
)
77 assert gp
isa MMSrcMethod
78 var garity
= gps
.arity
79 if prop
!= null and gp
.name
== prop
.name
then
80 if garity
== 0 or (parity
== garity
and prop
.signature
< gps
) then
83 false_candidates
.add
(gp
)
85 else if garity
== 0 and gp
.name
== once
("init".to_symbol
) then
87 false_candidates
.add
(gp
)
89 false_candidates
.add
(gp
)
92 if candidates
.length
== 1 then
93 return candidates
.first
94 else if candidates
.length
> 0 then
95 var a
= new Array[String]
96 for p
in candidates
do
97 a
.add
("{p.full_name}{p.signature}")
99 v
.error
(n
, "Error: Conflicting default constructor to call for {c}: {a.join(", ")}.")
101 else if false_candidates
.length
> 0 then
102 var a
= new Array[String]
103 for p
in false_candidates
do
104 a
.add
("{p.full_name}{p.signature}")
106 v
.error
(n
, "Error: there is no available compatible constrctor in {c}. Discarded candidates are {a.join(", ")}.")
109 v
.error
(n
, "Error: there is no available compatible constrctor in {c}.")
115 # Associate symbols to variable and variables to type
117 private class VariableContext
118 # Look for the variable from its name
119 # Return null if nothing found
120 meth
[](s
: Symbol): Variable
122 if _dico
.has_key
(s
) then
129 # Register a new variable with its name
130 meth add
(v
: Variable)
136 # The effective static type of a given variable
137 # May be different from the declaration static type
138 meth stype
(v
: Variable): MMType
143 # Variables by name (in the current context only)
144 attr _dico
: Map[Symbol, Variable]
146 # Build a new VariableContext
147 meth sub
: SubVariableContext
149 return new SubVariableContext.with_prev
(self, null, null)
152 # Build a nested VariableContext with new variable information
153 meth sub_with
(v
: Variable, t
: MMType): SubVariableContext
155 return new SubVariableContext.with_prev
(self, v
, t
)
160 _dico
= new HashMap[Symbol, Variable]
164 private class SubVariableContext
165 special VariableContext
166 readable attr _prev
: VariableContext
167 attr _variable
: Variable
168 attr _var_type
: MMType
172 if _dico
.has_key
(s
) then
181 if _variable
== v
then
187 init with_prev
(p
: VariableContext, v
: Variable, t
: MMType)
196 ###############################################################################
199 private meth accept_typing
(v
: TypingVisitor)
201 accept_abs_syntax_visitor
(v
)
204 private meth after_typing
(v
: TypingVisitor) do end
207 redef class PClassdef
208 redef meth accept_typing
(v
)
210 v
.self_var
= new ParamVariable("self".to_symbol
, self)
211 v
.self_var
.stype
= local_class
.get_type
216 redef class AAttrPropdef
217 redef meth accept_typing
(v
)
220 if n_expr
!= null then
221 v
.check_conform_expr
(n_expr
, prop
.signature
.return_type
)
226 redef class AMethPropdef
227 redef readable attr _self_var
: ParamVariable
228 redef meth accept_typing
(v
)
230 v
.variable_ctx
= new VariableContext
231 _self_var
= v
.self_var
236 redef class AConcreteInitPropdef
237 readable attr _super_init_calls
: Array[MMMethod] = new Array[MMMethod]
238 readable attr _explicit_super_init_calls
: Array[MMMethod] = new Array[MMMethod]
239 redef meth accept_typing
(v
)
241 v
.top_block
= n_block
242 v
.explicit_super_init_calls
= explicit_super_init_calls
243 v
.explicit_other_init_call
= false
245 if v
.explicit_other_init_call
or method
.global
.intro
!= method
then
249 var l
= explicit_super_init_calls
.length
250 var cur_m
: MMMethod = null
251 var cur_c
: MMLocalClass = null
253 cur_m
= explicit_super_init_calls
[i
]
254 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
257 while j
< v
.local_class
.cshe
.direct_greaters
.length
do
258 var c
= v
.local_class
.cshe
.direct_greaters
[j
]
259 if c
.global
.is_interface
or c
.global
.is_universal
or c
.global
.is_mixin
then
261 else if cur_c
!= null and (c
.cshe
<= cur_c
or cur_c
.global
.is_mixin
) then
262 if c
== cur_c
then j
+= 1
263 super_init_calls
.add
(cur_m
)
266 cur_m
= explicit_super_init_calls
[i
]
267 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
273 var p
= v
.get_default_constructor_for
(self, c
, method
)
275 super_init_calls
.add
(p
)
285 redef meth after_typing
(v
)
287 # TODO: why the test?
288 if v
.variable_ctx
!= null then
289 v
.variable_ctx
.add
(variable
)
294 redef class AClosureDecl
295 # The corresponding escapable object
296 readable attr _escapable
: EscapableBlock
298 redef meth accept_typing
(v
)
300 # Register the closure for ClosureCallExpr
301 v
.variable_ctx
.add
(variable
)
303 var old_var_ctx
= v
.variable_ctx
304 v
.variable_ctx
= v
.variable_ctx
.sub
306 _escapable
= new EscapableClosure(self, variable
.closure
, null)
307 v
.escapable_ctx
.push
(_escapable
)
311 v
.variable_ctx
= old_var_ctx
317 readable attr _stype
: MMType
318 redef meth after_typing
(v
)
320 _stype
= get_stype
(v
)
325 redef readable attr _stype
: MMType
327 # Is the expression the implicit receiver
328 meth is_implicit_self
: Bool do return false
330 # Is the expression the current receiver (implicit or explicit)
331 meth is_self
: Bool do return false
333 # The variable accessed is any
334 meth its_variable
: Variable do return null
336 # The variable type information if current boolean expression is true
337 readable private attr _if_true_variable_ctx
: VariableContext
340 redef class AVardeclExpr
341 redef meth after_typing
(v
)
343 var va
= new VarVariable(n_id
.to_symbol
, self)
345 v
.variable_ctx
.add
(va
)
347 if n_type
!= null then
348 va
.stype
= n_type
.stype
349 if n_expr
!= null then
350 v
.check_conform_expr
(n_expr
, va
.stype
)
354 va
.stype
= n_expr
.stype
359 redef class ABlockExpr
360 redef meth accept_typing
(v
)
362 var old_var_ctx
= v
.variable_ctx
363 v
.variable_ctx
= v
.variable_ctx
.sub
367 v
.variable_ctx
= old_var_ctx
371 redef class AReturnExpr
372 redef meth after_typing
(v
)
374 var t
= v
.local_property
.signature
.return_type
375 if n_expr
== null and t
!= null then
376 v
.error
(self, "Error: Return without value in a function.")
377 else if n_expr
!= null and t
== null then
378 v
.error
(self, "Error: Return with value in a procedure.")
379 else if n_expr
!= null and t
!= null then
380 v
.check_conform_expr
(n_expr
, t
)
385 redef class AContinueExpr
386 redef meth after_typing
(v
)
388 var esc
= compute_escapable_block
(v
.escapable_ctx
)
389 if esc
== null then return
391 if esc
.is_break_block
then
392 v
.error
(self, "Error: 'continue' forbiden in break blocks.")
396 var t
= esc
.continue_stype
397 if n_expr
== null and t
!= null then
398 v
.error
(self, "Error: continue with a value required in this bloc.")
399 else if n_expr
!= null and t
== null then
400 v
.error
(self, "Error: continue without value required in this bloc.")
401 else if n_expr
!= null and t
!= null then
402 v
.check_conform_expr
(n_expr
, t
)
407 redef class ABreakExpr
408 redef meth after_typing
(v
)
410 var esc
= compute_escapable_block
(v
.escapable_ctx
)
411 if esc
== null then return
413 var bl
= esc
.break_list
414 if n_expr
== null and bl
!= null then
415 v
.error
(self, "Error: break with a value required in this bloc.")
416 else if n_expr
!= null and bl
== null then
417 v
.error
(self, "Error: break without value required in this bloc.")
418 else if n_expr
!= null and bl
!= null then
419 # Typing check can only be done later
426 redef meth accept_typing
(v
)
428 var old_var_ctx
= v
.variable_ctx
430 v
.check_conform_expr
(n_expr
, v
.type_bool
)
432 if n_expr
.if_true_variable_ctx
!= null then
433 v
.variable_ctx
= n_expr
.if_true_variable_ctx
437 # Restore variable ctx
438 v
.variable_ctx
= old_var_ctx
440 if n_else
!= null then
442 v
.variable_ctx
= old_var_ctx
447 redef class AWhileExpr
448 # The corresponding escapable block
449 readable attr _escapable
: EscapableBlock
451 redef meth accept_typing
(v
)
453 _escapable
= new EscapableBlock(self)
454 v
.escapable_ctx
.push
(_escapable
)
458 v
.check_conform_expr
(n_expr
, v
.type_bool
)
464 # The corresponding escapable block
465 readable attr _escapable
: EscapableBlock
467 readable attr _meth_iterator
: MMMethod
468 readable attr _meth_is_ok
: MMMethod
469 readable attr _meth_item
: MMMethod
470 readable attr _meth_next
: MMMethod
471 redef meth accept_typing
(v
)
473 _escapable
= new EscapableBlock(self)
474 v
.escapable_ctx
.push
(_escapable
)
476 v
.variable_ctx
= v
.variable_ctx
.sub
477 var va
= new AutoVariable(n_id
.to_symbol
, self)
479 v
.variable_ctx
.add
(va
)
483 var expr_type
= n_expr
.stype
484 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then
487 _meth_iterator
= expr_type
.local_class
.select_method
(once
("iterator".to_symbol
))
488 if _meth_iterator
== null then
489 v
.error
(self, "Error: Collection MUST have an iterate method")
492 var iter_type
= _meth_iterator
.signature_for
(expr_type
).return_type
493 _meth_is_ok
= iter_type
.local_class
.select_method
(once
("is_ok".to_symbol
))
494 if _meth_is_ok
== null then
495 v
.error
(self, "Error: {iter_type} MUST have an is_ok method")
498 _meth_item
= iter_type
.local_class
.select_method
(once
("item".to_symbol
))
499 if _meth_item
== null then
500 v
.error
(self, "Error: {iter_type} MUST have an item method")
503 _meth_next
= iter_type
.local_class
.select_method
(once
("next".to_symbol
))
504 if _meth_next
== null then
505 v
.error
(self, "Error: {iter_type} MUST have a next method")
508 var t
= _meth_item
.signature_for
(iter_type
).return_type
509 if not n_expr
.is_self
then t
= t
.not_for_self
512 if n_block
!= null then v
.visit
(n_block
)
515 var varctx
= v
.variable_ctx
516 assert varctx
isa SubVariableContext
517 v
.variable_ctx
= varctx
.prev
523 redef class AAssertExpr
524 redef meth after_typing
(v
)
526 v
.check_conform_expr
(n_expr
, v
.type_bool
)
527 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
532 redef meth its_variable
do return variable
534 redef meth after_typing
(v
)
536 _stype
= v
.variable_ctx
.stype
(variable
)
540 redef class AVarAssignExpr
541 redef meth after_typing
(v
)
543 var t
= v
.variable_ctx
.stype
(variable
)
544 v
.check_conform_expr
(n_value
, t
)
548 redef class AReassignFormExpr
549 # Compute and check method used through the reassigment operator
550 private meth do_lvalue_typing
(v
: TypingVisitor, type_lvalue
: MMType)
552 if type_lvalue
== null then
555 var name
= n_assign_op
.method_name
556 var prop
= type_lvalue
.local_class
.select_method
(name
)
558 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
561 prop
.global
.check_visibility
(v
, self, v
.module, false)
562 var psig
= prop
.signature_for
(type_lvalue
)
563 _assign_method
= prop
564 v
.check_conform_expr
(n_value
, psig
[0].not_for_self
)
565 v
.check_conform
(self, psig
.return_type
.not_for_self
, n_value
.stype
)
568 # Method used through the reassigment operator (once computed)
569 readable attr _assign_method
: MMMethod
572 redef class AVarReassignExpr
573 redef meth after_typing
(v
)
575 var t
= v
.variable_ctx
.stype
(variable
)
576 do_lvalue_typing
(v
, t
)
580 redef class PAssignOp
581 meth method_name
: Symbol is abstract
583 redef class APlusAssignOp
584 redef meth method_name
do return once
"+".to_symbol
586 redef class AMinusAssignOp
587 redef meth method_name
do return once
"-".to_symbol
590 redef class ASelfExpr
591 redef meth its_variable
do return variable
593 redef meth after_typing
(v
)
595 variable
= v
.self_var
596 _stype
= v
.variable_ctx
.stype
(variable
)
599 redef meth is_self
do return true
602 redef class AImplicitSelfExpr
603 redef meth is_implicit_self
do return true
606 redef class AIfexprExpr
607 redef meth accept_typing
(v
)
609 var old_var_ctx
= v
.variable_ctx
612 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
614 v
.variable_ctx
= old_var_ctx
617 v
.check_conform_expr
(n_expr
, v
.type_bool
)
619 if not v
.check_expr
(n_then
) or not v
.check_expr
(n_else
) then return
622 var te
= n_else
.stype
625 else if not te
< t
then
626 v
.error
(self, "Type error: {te} is not a subtype of {t}.")
634 redef class ABoolExpr
635 redef meth after_typing
(v
)
642 redef meth after_typing
(v
)
644 v
.check_conform_expr
(n_expr
, v
.type_bool
)
645 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
651 redef meth accept_typing
(v
)
653 var old_var_ctx
= v
.variable_ctx
656 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
659 if n_expr2
.if_true_variable_ctx
!= null then
660 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
662 _if_true_variable_ctx
= v
.variable_ctx
665 v
.variable_ctx
= old_var_ctx
667 v
.check_conform_expr
(n_expr
, v
.type_bool
)
668 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
674 redef meth after_typing
(v
)
676 v
.check_conform_expr
(n_expr
, v
.type_bool
)
682 redef meth after_typing
(v
)
689 redef class AFloatExpr
690 redef meth after_typing
(v
)
692 _stype
= v
.type_float
696 redef class ACharExpr
697 redef meth after_typing
(v
)
703 redef class AStringFormExpr
704 readable attr _meth_with_native
: MMMethod
705 redef meth after_typing
(v
)
707 _stype
= v
.type_string
708 _meth_with_native
= _stype
.local_class
.select_method
(once
"with_native".to_symbol
)
709 if _meth_with_native
== null then v
.error
(self, "{_stype} MUST have a with_native method.")
713 redef class ASuperstringExpr
714 readable attr _meth_with_capacity
: MMMethod
715 readable attr _meth_add
: MMMethod
716 readable attr _meth_to_s
: MMMethod
717 readable attr _atype
: MMType
718 redef meth after_typing
(v
)
720 _stype
= v
.type_string
721 _atype
= v
.type_array
(_stype
)
722 _meth_with_capacity
= _atype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
723 if _meth_with_capacity
== null then v
.error
(self, "{_atype} MUST have a with_capacity method.")
724 _meth_add
= _atype
.local_class
.select_method
(once
"add".to_symbol
)
725 if _meth_add
== null then v
.error
(self, "{_atype} MUST have an add method.")
726 _meth_to_s
= v
.type_object
.local_class
.select_method
(once
"to_s".to_symbol
)
727 if _meth_to_s
== null then v
.error
(self, "Object MUST have a to_s method.")
731 redef class ANullExpr
732 redef meth after_typing
(v
)
738 redef class AArrayExpr
739 readable attr _meth_with_capacity
: MMMethod
740 readable attr _meth_add
: MMMethod
742 redef meth after_typing
(v
)
744 var stype
: MMType = null
747 if stype
== null or (ntype
!= null and stype
< ntype
) then
752 v
.check_conform_expr
(n
, stype
)
757 private meth do_typing
(v
: TypingVisitor, element_type
: MMType)
759 _stype
= v
.type_array
(element_type
)
761 _meth_with_capacity
= _stype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
762 if _meth_with_capacity
== null then v
.error
(self, "{_stype} MUST have a with_capacity method.")
763 _meth_add
= _stype
.local_class
.select_method
(once
"add".to_symbol
)
764 if _meth_add
== null then v
.error
(self, "{_stype} MUST have an add method.")
768 redef class ARangeExpr
769 readable attr _meth_init
: MMMethod
770 redef meth after_typing
(v
)
772 var ntype
= n_expr
.stype
773 var ntype2
= n_expr2
.stype
774 if ntype
== null or ntype
== null then
777 if ntype
< ntype2
then
779 else if not ntype2
< ntype
then
780 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
783 var dtype
= v
.type_discrete
784 v
.check_conform_expr
(n_expr
, dtype
)
785 v
.check_conform_expr
(n_expr2
, dtype
)
786 _stype
= v
.type_range
(ntype
)
790 redef class ACrangeExpr
791 redef meth after_typing
(v
)
794 _meth_init
= stype
.local_class
.select_method
(once
"init".to_symbol
)
797 redef class AOrangeExpr
798 redef meth after_typing
(v
)
801 _meth_init
= stype
.local_class
.select_method
(once
"without_last".to_symbol
)
806 redef class ASuperExpr
807 special ASuperInitCall
808 # readable attr _prop: MMSrcMethod
809 readable attr _init_in_superclass
: MMMethod
810 redef meth after_typing
(v
)
812 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
813 if not precs
.is_empty
then
814 v
.local_property
.need_super
= true
815 else if v
.local_property
.global
.is_init
then
816 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
817 for p
in base_precs
do
818 if not p
.global
.is_init
then
819 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
821 precs
.add
(v
.local_class
[p
.global
])
824 if precs
.is_empty
then
825 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
827 else if precs
.length
> 1 then
828 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
831 var p
= base_precs
.first
832 assert p
isa MMMethod
833 _init_in_superclass
= p
834 register_super_init_call
(v
, p
)
835 if n_args
.length
> 0 then
836 var signature
= get_signature
(v
, v
.self_var
.stype
, p
, true)
837 _arguments
= process_signature
(v
, signature
, p
.name
, n_args
.to_a
)
840 v
.error
(self, "Error: No super method to call for {v.local_property}.")
844 if precs
.first
.signature_for
(v
.self_var
.stype
).return_type
!= null then
845 var stypes
= new Array[MMType]
846 var stype
: MMType = null
848 assert prop
isa MMMethod
849 var t
= prop
.signature_for
(v
.self_var
.stype
).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
851 if stype
== null or stype
< t
then
856 v
.check_conform
(self, t
, stype
)
860 var p
= v
.local_property
861 assert p
isa MMSrcMethod
866 redef class AAttrFormExpr
868 readable attr _prop
: MMAttribute
870 # Attribute type of the acceded attribute
871 readable attr _attr_type
: MMType
873 # Compute the attribute accessed
874 private meth do_typing
(v
: TypingVisitor)
876 if not v
.check_expr
(n_expr
) then return
877 var type_recv
= n_expr
.stype
878 var name
= n_id
.to_symbol
879 var prop
= type_recv
.local_class
.select_attribute
(name
)
881 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
883 else if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
884 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
887 var at
= prop
.signature_for
(type_recv
).return_type
888 if not n_expr
.is_self
then at
= at
.not_for_self
893 redef class AAttrExpr
894 redef meth after_typing
(v
)
904 redef class AAttrAssignExpr
905 redef meth after_typing
(v
)
911 v
.check_conform_expr
(n_value
, attr_type
)
915 redef class AAttrReassignExpr
916 redef meth after_typing
(v
)
922 do_lvalue_typing
(v
, attr_type
)
928 # The signature of the called property
929 readable attr _prop_signature
: MMSignature
931 # Compute the called global property
932 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])
934 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
935 if prop
== null then return
936 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
937 if sig
== null then return
938 var args
= process_signature
(v
, sig
, prop
.name
, raw_args
)
939 if args
== null then return
940 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
942 _prop_signature
= sig
947 private meth get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): MMMethod
949 if type_recv
== null then return null
950 var prop
= type_recv
.local_class
.select_method
(name
)
951 if prop
== null and v
.local_property
.global
.is_init
then
952 var props
= type_recv
.local_class
.super_methods_named
(name
)
953 if props
.length
> 1 then
954 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
956 else if props
.length
== 1 then
957 var p
= type_recv
.local_class
[props
.first
.global
]
958 assert p
isa MMMethod
964 if is_implicit_self
then
965 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
967 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
974 # Get the signature for a local property and a receiver
975 private meth get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
977 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
978 var psig
= prop
.signature_for
(type_recv
)
979 if not recv_is_self
then psig
= psig
.not_for_self
983 # Check the conformity of a set of arguments `raw_args' to a signature.
984 private meth process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: Array[PExpr]): Array[PExpr]
986 var par_vararg
= psig
.vararg_rank
987 var par_arity
= psig
.arity
989 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
990 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
991 v
.error
(self, "Error: '{name}' arity missmatch.")
995 var args
= new Array[PExpr]
996 for par_idx
in [0..par_arity
[ do
998 var par_type
= psig
[par_idx
]
999 if par_idx
== par_vararg
then
1000 var star
= new Array[PExpr]
1001 for i
in [0..(raw_arity-par_arity
)] do
1002 a
= raw_args
[arg_idx
]
1003 v
.check_conform_expr
(a
, par_type
)
1005 arg_idx
= arg_idx
+ 1
1007 var aa
= new AArrayExpr.init_aarrayexpr
(star
)
1008 aa
.do_typing
(v
, par_type
)
1011 a
= raw_args
[arg_idx
]
1012 v
.check_conform_expr
(a
, par_type
)
1013 arg_idx
= arg_idx
+ 1
1020 # Check the conformity of a set of defined closures
1021 private meth process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: Array[PClosureDef]): MMType
1023 var t
= psig
.return_type
1024 var cs
= psig
.closures
# Declared closures
1027 if not c
.is_optional
then min_arity
+= 1
1030 if cs
.length
== 0 then
1031 v
.error
(self, "Error: {name} does not require blocs.")
1032 else if cd
.length
> cs
.length
or cd
.length
< min_arity
then
1033 v
.error
(self, "Error: {name} requires {cs.length} blocs, {cd.length} found.")
1035 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
1036 var break_list
: Array[ABreakExpr] = null
1037 if t
!= null then break_list
= new Array[ABreakExpr]
1039 # Process each closure definition
1040 for i
in [0..cd
.length
[ do
1043 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1044 v
.escapable_ctx
.push
(esc
)
1045 cdi
.accept_typing2
(v
, esc
)
1049 # Check break type conformity
1050 if break_list
!= null then
1051 for n
in break_list
do
1053 if t
== null or (t
!= null and t
< ntype
) then
1057 for n
in break_list
do
1058 v
.check_conform_expr
(n
, t
)
1062 else if min_arity
!= 0 then
1063 v
.error
(self, "Error: {name} requires {cs.length} blocs.")
1068 # The invoked method (once computed)
1069 readable attr _prop
: MMMethod
1071 # The real arguments used (after star transformation) (once computed)
1072 readable attr _arguments
: Array[PExpr]
1074 # The return type (if any) (once computed)
1075 readable attr _return_type
: MMType
1078 # A possible call of constructor in a super class
1079 # Could be an explicit call or with the 'super' keyword
1080 class ASuperInitCall
1081 special AAbsSendExpr
1082 private meth register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1084 if parent
!= v
.top_block
and self != v
.top_block
then
1085 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1087 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
1088 var prev_class
: MMLocalClass = null
1089 if not v
.explicit_super_init_calls
.is_empty
then
1090 prev_class
= v
.explicit_super_init_calls
.last
.global
.intro
.local_class
1092 var order
= v
.local_class
.cshe
.reverse_linear_extension
1093 if cla
== v
.local_class
then
1094 v
.explicit_other_init_call
= true
1095 else if not order
.has
(cla
) then
1096 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1097 else if cla
== prev_class
then
1098 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1100 var last_is_found
= prev_class
== null
1102 if c
== prev_class
then
1103 last_is_found
= true
1104 else if c
== cla
then
1105 if not last_is_found
then
1106 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1108 v
.explicit_super_init_calls
.add
(property
)
1117 redef class ANewExpr
1118 special AAbsSendExpr
1119 redef meth after_typing
(v
)
1121 var t
= n_type
.stype
1122 if t
== null then return
1123 if t
.local_class
.global
.is_abstract
then
1124 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1128 if n_id
== null then
1129 name
= once
"init".to_symbol
1131 name
= n_id
.to_symbol
1134 do_typing
(v
, t
, false, false, name
, n_args
.to_a
, null)
1135 if prop
== null then return
1137 if not prop
.global
.is_init
then
1138 v
.error
(self, "Error: {prop} is not a constructor.")
1145 redef class ASendExpr
1146 special ASuperInitCall
1147 # Name of the invoked property
1148 meth name
: Symbol is abstract
1150 # Raw arguments used (withour star transformation)
1151 meth raw_arguments
: Array[PExpr] is abstract
1153 # Closure definitions
1154 meth closure_defs
: Array[PClosureDef] do return null
1156 redef meth after_typing
(v
)
1161 private meth do_all_typing
(v
: TypingVisitor)
1163 if not v
.check_expr
(n_expr
) then return
1164 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1165 if prop
== null then return
1167 if prop
.global
.is_init
then
1168 if not v
.local_property
.global
.is_init
then
1169 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1170 else if not n_expr
.is_self
then
1171 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1173 register_super_init_call
(v
, prop
)
1177 _stype
= return_type
1181 class ASendReassignExpr
1183 special AReassignFormExpr
1184 readable attr _read_prop
: MMMethod
1185 redef meth do_all_typing
(v
)
1187 if not v
.check_expr
(n_expr
) then return
1188 var raw_args
= raw_arguments
1189 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1190 if prop
== null then return
1191 if prop
.global
.is_init
then
1192 if not v
.local_property
.global
.is_init
then
1193 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1194 else if not n_expr
.is_self
then
1195 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1198 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1199 if not n_expr
.is_self
then t
= t
.not_for_self
1201 do_lvalue_typing
(v
, t
)
1204 var old_args
= arguments
1205 raw_args
.add
(n_value
)
1207 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1208 if prop
== null then return
1209 if prop
.global
.is_init
then
1210 if not v
.local_property
.global
.is_init
then
1211 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1212 else if not n_expr
.is_self
then
1213 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1217 _arguments
= old_args
# FIXME: What if star parameters do not match betwen the two methods?
1221 redef class ABinopExpr
1222 redef meth raw_arguments
do return [n_expr2
]
1225 redef meth name
do return once
"==".to_symbol
1228 redef meth name
do return once
"!=".to_symbol
1231 redef meth name
do return once
"<".to_symbol
1234 redef meth name
do return once
"<=".to_symbol
1237 redef meth name
do return once
">".to_symbol
1240 redef meth name
do return once
">=".to_symbol
1242 redef class APlusExpr
1243 redef meth name
do return once
"+".to_symbol
1245 redef class AMinusExpr
1246 redef meth name
do return once
"-".to_symbol
1248 redef class AStarshipExpr
1249 redef meth name
do return once
"<=>".to_symbol
1251 redef class AStarExpr
1252 redef meth name
do return once
"*".to_symbol
1254 redef class ASlashExpr
1255 redef meth name
do return once
"/".to_symbol
1257 redef class APercentExpr
1258 redef meth name
do return once
"%".to_symbol
1261 redef class AUminusExpr
1262 redef meth name
do return once
"unary -".to_symbol
1263 redef meth raw_arguments
do return null
1266 redef class ACallFormExpr
1267 redef meth after_typing
(v
)
1269 if n_expr
!= null and n_expr
.is_implicit_self
then
1270 var name
= n_id
.to_symbol
1271 var variable
= v
.variable_ctx
[name
]
1272 if variable
!= null then
1273 if variable
isa ClosureVariable then
1274 var n
= new AClosureCallExpr(n_id
, n_args
, n_closure_defs
)
1276 n
.variable
= variable
1280 if not n_args
.is_empty
then
1281 v
.error
(self, "Error: {name} is variable, not a function.")
1283 var vform
= variable_create
(variable
)
1284 vform
.variable
= variable
1286 vform
.after_typing
(v
)
1295 redef meth closure_defs
1297 if n_closure_defs
== null or n_closure_defs
.is_empty
then
1300 return n_closure_defs
.to_a
1304 # Create a variable acces corresponding to the call form
1305 meth variable_create
(variable
: Variable): AVarFormExpr is abstract
1308 redef class ACallExpr
1309 redef meth variable_create
(variable
)
1311 return new AVarExpr.init_avarexpr
(n_id
)
1314 redef meth name
do return n_id
.to_symbol
1315 redef meth raw_arguments
do return n_args
.to_a
1318 redef class ACallAssignExpr
1319 redef meth variable_create
(variable
)
1321 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1324 redef meth name
do return (n_id
.text
+ "=").to_symbol
1325 redef meth raw_arguments
do
1326 var res
= n_args
.to_a
1332 redef class ACallReassignExpr
1333 special ASendReassignExpr
1334 redef meth variable_create
(variable
)
1336 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1339 redef meth name
do return n_id
.to_symbol
1340 redef meth raw_arguments
do return n_args
.to_a
1343 redef class ABraExpr
1344 redef meth name
do return once
"[]".to_symbol
1345 redef meth raw_arguments
do return n_args
.to_a
1348 redef class ABraAssignExpr
1349 redef meth name
do return once
"[]=".to_symbol
1350 redef meth raw_arguments
do
1351 var res
= n_args
.to_a
1357 redef class ABraReassignExpr
1358 special ASendReassignExpr
1359 redef meth name
do return once
"[]".to_symbol
1360 redef meth raw_arguments
do return n_args
.to_a
1363 redef class AInitExpr
1364 redef meth name
do return once
"init".to_symbol
1365 redef meth raw_arguments
do return n_args
.to_a
1368 redef class AClosureCallExpr
1369 redef meth after_typing
(v
)
1372 var sig
= va
.closure
.signature
1373 var args
= process_signature
(v
, sig
, n_id
.to_symbol
, n_args
.to_a
)
1374 if closure_defs
!= null then
1375 process_closures
(v
, sig
, n_id
.to_symbol
, closure_defs
)
1377 if args
== null then return
1379 _prop_signature
= sig
1381 _stype
= sig
.return_type
1385 redef class PClosureDef
1386 # The corresponding escapable object
1387 readable attr _escapable
: EscapableBlock
1389 attr _accept_typing2
: Bool
1390 redef meth accept_typing
(v
)
1392 # Typing is deferred, wait accept_typing2(v)
1393 if _accept_typing2
then super
1396 private meth accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure) is abstract
1399 redef class AClosureDef
1400 redef meth accept_typing2
(v
, esc
)
1404 var sig
= esc
.closure
.signature
1405 if sig
.arity
!= n_id
.length
then
1406 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1410 closure
= esc
.closure
1412 v
.variable_ctx
= v
.variable_ctx
.sub
1413 variables
= new Array[AutoVariable]
1414 for i
in [0..n_id
.length
[ do
1415 var va
= new AutoVariable(n_id
[i
].to_symbol
, self)
1418 v
.variable_ctx
.add
(va
)
1421 _accept_typing2
= true
1426 redef class AIsaExpr
1427 redef meth after_typing
(v
)
1429 var variable
= n_expr
.its_variable
1430 if variable
!= null then
1431 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(variable
, n_type
.stype
)
1433 _stype
= v
.type_bool
1437 redef class AAsCastExpr
1438 redef meth after_typing
(v
)
1440 v
.check_expr
(n_expr
)
1441 _stype
= n_type
.stype
1445 redef class AProxyExpr
1446 redef meth after_typing
(v
)
1448 _stype
= n_expr
.stype