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
22 redef class MMSrcModule
23 # Walk trough the module and type statments and expressions
24 # Require than supermodules are processed
25 meth do_typing
(tc
: ToolContext)
27 var tv
= new TypingVisitor(tc
, self)
33 # * Associate local variables to nodes
34 # * Distinguish method call and local variable access
35 # * Resolve call and attribute access
36 # * Check type conformance
37 private class TypingVisitor
38 special AbsSyntaxVisitor
41 if n
!= null then n
.accept_typing
(self)
44 # Current knowledge about variables names and types
45 readable writable attr _variable_ctx
: VariableContext
47 # The current reciever
48 readable writable attr _self_var
: ParamVariable
50 # Block of the current method
51 readable writable attr _top_block
: PExpr
53 # Current closure (if any)
54 readable writable attr _closure
: MMClosure
56 # Current closure method return type (for break) (if any)
57 readable writable attr _closure_break_stype
: MMType = null
59 # Current closure break expressions (if any)
60 readable writable attr _break_list
: Array[PExpr]
62 # List of explicit invocation of constructors of super-classes
63 readable writable attr _explicit_super_init_calls
: Array[MMMethod]
65 # Is a other constructor of the same class invoked
66 readable writable attr _explicit_other_init_call
: Bool
68 init(tc
, module) do super
70 private meth get_default_constructor_for
(n
: PNode, c
: MMLocalClass, prop
: MMSrcMethod): MMMethod
73 #var prop = v.local_property
74 #assert prop isa MMMethod
75 var candidates
= new Array[MMMethod]
76 var false_candidates
= new Array[MMMethod]
77 var parity
= prop
.signature
.arity
78 for g
in c
.global_properties
do
79 if not g
.is_init_for
(c
) then continue
81 var gps
= gp
.signature_for
(c
.get_type
)
82 assert gp
isa MMSrcMethod
83 var garity
= gps
.arity
84 if prop
!= null and gp
.name
== prop
.name
then
85 if garity
== 0 or (parity
== garity
and prop
.signature
< gps
) then
88 false_candidates
.add
(gp
)
90 else if garity
== 0 and gp
.name
== once
("init".to_symbol
) then
92 false_candidates
.add
(gp
)
94 false_candidates
.add
(gp
)
97 if candidates
.length
== 1 then
98 return candidates
.first
99 else if candidates
.length
> 0 then
100 var a
= new Array[String]
101 for p
in candidates
do
102 a
.add
("{p.full_name}{p.signature}")
104 v
.error
(n
, "Error: Conflicting default constructor to call for {c}: {a.join(", ")}.")
106 else if false_candidates
.length
> 0 then
107 var a
= new Array[String]
108 for p
in false_candidates
do
109 a
.add
("{p.full_name}{p.signature}")
111 v
.error
(n
, "Error: there is no available compatible constrctor in {c}. Discarded candidates are {a.join(", ")}.")
114 v
.error
(n
, "Error: there is no available compatible constrctor in {c}.")
120 # Associate symbols to variable and variables to type
122 private class VariableContext
123 # Look for the variable from its name
124 # Return null if nothing found
125 meth
[](s
: Symbol): Variable
127 if _dico
.has_key
(s
) then
134 # Register a new variable with its name
135 meth add
(v
: Variable)
141 # The effective static type of a given variable
142 # May be different from the declaration static type
143 meth stype
(v
: Variable): MMType
148 # Variables by name (in the current context only)
149 attr _dico
: Map[Symbol, Variable]
151 # Build a new VariableContext
152 meth sub
: SubVariableContext
154 return new SubVariableContext.with_prev
(self, null, null)
157 # Build a nested VariableContext with new variable information
158 meth sub_with
(v
: Variable, t
: MMType): SubVariableContext
160 return new SubVariableContext.with_prev
(self, v
, t
)
165 _dico
= new HashMap[Symbol, Variable]
169 private class SubVariableContext
170 special VariableContext
171 readable attr _prev
: VariableContext
172 attr _variable
: Variable
173 attr _var_type
: MMType
177 if _dico
.has_key
(s
) then
186 if _variable
== v
then
192 init with_prev
(p
: VariableContext, v
: Variable, t
: MMType)
202 ###############################################################################
205 private meth accept_typing
(v
: TypingVisitor)
207 accept_abs_syntax_visitor
(v
)
210 private meth after_typing
(v
: TypingVisitor) do end
213 redef class PClassdef
214 redef meth accept_typing
(v
)
216 v
.self_var
= new ParamVariable("self".to_symbol
, self)
217 v
.self_var
.stype
= local_class
.get_type
222 redef class AAttrPropdef
223 redef meth accept_typing
(v
)
226 if n_expr
!= null then
227 v
.check_conform_expr
(n_expr
, prop
.signature
.return_type
)
232 redef class AMethPropdef
233 redef readable attr _self_var
: ParamVariable
234 redef meth accept_typing
(v
)
236 v
.variable_ctx
= new VariableContext
237 _self_var
= v
.self_var
242 redef class AConcreteInitPropdef
243 readable attr _super_init_calls
: Array[MMMethod] = new Array[MMMethod]
244 readable attr _explicit_super_init_calls
: Array[MMMethod] = new Array[MMMethod]
245 redef meth accept_typing
(v
)
247 v
.top_block
= n_block
248 v
.explicit_super_init_calls
= explicit_super_init_calls
249 v
.explicit_other_init_call
= false
251 if v
.explicit_other_init_call
or method
.global
.intro
!= method
then
255 var l
= explicit_super_init_calls
.length
256 var cur_m
: MMMethod = null
257 var cur_c
: MMLocalClass = null
259 cur_m
= explicit_super_init_calls
[i
]
260 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
263 while j
< v
.local_class
.cshe
.direct_greaters
.length
do
264 var c
= v
.local_class
.cshe
.direct_greaters
[j
]
265 if c
.global
.is_interface
or c
.global
.is_universal
or c
.global
.is_mixin
then
267 else if cur_c
!= null and (c
.cshe
<= cur_c
or cur_c
.global
.is_mixin
) then
268 if c
== cur_c
then j
+= 1
269 super_init_calls
.add
(cur_m
)
272 cur_m
= explicit_super_init_calls
[i
]
273 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
279 var p
= v
.get_default_constructor_for
(self, c
, method
)
281 super_init_calls
.add
(p
)
291 redef meth after_typing
(v
)
293 # TODO: why the test?
294 if v
.variable_ctx
!= null then
295 v
.variable_ctx
.add
(variable
)
300 redef class AClosureDecl
301 redef meth accept_typing
(v
)
303 # Register the closure for ClosureCallExpr
304 v
.variable_ctx
.add
(variable
)
306 var old_var_ctx
= v
.variable_ctx
307 v
.variable_ctx
= v
.variable_ctx
.sub
308 v
.closure
= variable
.closure
312 v
.variable_ctx
= old_var_ctx
318 readable attr _stype
: MMType
319 redef meth after_typing
(v
)
321 _stype
= get_stype
(v
)
326 redef readable attr _stype
: MMType
328 # Is the expression the implicit receiver
329 meth is_implicit_self
: Bool do return false
331 # Is the expression the current receiver (implicit or explicit)
332 meth is_self
: Bool do return false
334 # The variable accessed is any
335 meth its_variable
: Variable do return null
337 # The variable type information if current boolean expression is true
338 readable private attr _if_true_variable_ctx
: VariableContext
341 redef class AVardeclExpr
342 redef meth after_typing
(v
)
344 var va
= new VarVariable(n_id
.to_symbol
, self)
346 v
.variable_ctx
.add
(va
)
348 if n_type
!= null then
349 va
.stype
= n_type
.stype
350 if n_expr
!= null then
351 v
.check_conform_expr
(n_expr
, va
.stype
)
355 va
.stype
= n_expr
.stype
360 redef class ABlockExpr
361 redef meth accept_typing
(v
)
363 var old_var_ctx
= v
.variable_ctx
364 v
.variable_ctx
= v
.variable_ctx
.sub
368 v
.variable_ctx
= old_var_ctx
372 redef class AReturnExpr
373 redef meth after_typing
(v
)
375 var t
= v
.local_property
.signature
.return_type
376 if n_expr
== null and t
!= null then
377 v
.error
(self, "Error: Return without value in a function.")
378 else if n_expr
!= null and t
== null then
379 v
.error
(self, "Error: Return with value in a procedure.")
380 else if n_expr
!= null and t
!= null then
381 v
.check_conform_expr
(n_expr
, t
)
386 redef class AContinueExpr
387 redef meth after_typing
(v
)
393 v
.error
(self, "Error: 'continue' forbiden in break blocks.")
396 t
= c
.signature
.return_type
399 if n_expr
== null and t
!= null then
400 v
.error
(self, "Error: continue with a value required in this bloc.")
401 else if n_expr
!= null and t
== null then
402 v
.error
(self, "Error: continue without value required in this bloc.")
403 else if n_expr
!= null and t
!= null then
404 v
.check_conform_expr
(n_expr
, t
)
409 redef class ABreakExpr
410 redef meth after_typing
(v
)
412 var t
= v
.closure_break_stype
413 if n_expr
== null and t
!= null then
414 v
.error
(self, "Error: break with a value required in this bloc.")
415 else if n_expr
!= null and t
== null then
416 v
.error
(self, "Error: break without value required in this bloc.")
417 else if n_expr
!= null and t
!= null then
418 # Typing check can only be done later
419 v
.break_list
.add
(n_expr
)
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 if n_expr
.if_true_variable_ctx
!= null then
432 v
.variable_ctx
= n_expr
.if_true_variable_ctx
436 # Restore variable ctx
437 v
.variable_ctx
= old_var_ctx
439 if n_else
!= null then
441 v
.variable_ctx
= old_var_ctx
446 redef class AWhileExpr
447 redef meth after_typing
(v
)
449 v
.check_conform_expr
(n_expr
, v
.type_bool
)
454 readable attr _meth_iterator
: MMMethod
455 readable attr _meth_is_ok
: MMMethod
456 readable attr _meth_item
: MMMethod
457 readable attr _meth_next
: MMMethod
458 redef meth accept_typing
(v
)
460 v
.variable_ctx
= v
.variable_ctx
.sub
461 var va
= new AutoVariable(n_id
.to_symbol
, self)
463 v
.variable_ctx
.add
(va
)
467 var expr_type
= n_expr
.stype
468 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then
471 _meth_iterator
= expr_type
.local_class
.select_method
(once
("iterator".to_symbol
))
472 if _meth_iterator
== null then
473 v
.error
(self, "Error: Collection MUST have an iterate method")
476 var iter_type
= _meth_iterator
.signature_for
(expr_type
).return_type
477 _meth_is_ok
= iter_type
.local_class
.select_method
(once
("is_ok".to_symbol
))
478 if _meth_is_ok
== null then
479 v
.error
(self, "Error: {iter_type} MUST have an is_ok method")
482 _meth_item
= iter_type
.local_class
.select_method
(once
("item".to_symbol
))
483 if _meth_item
== null then
484 v
.error
(self, "Error: {iter_type} MUST have an item method")
487 _meth_next
= iter_type
.local_class
.select_method
(once
("next".to_symbol
))
488 if _meth_next
== null then
489 v
.error
(self, "Error: {iter_type} MUST have a next method")
492 var t
= _meth_item
.signature_for
(iter_type
).return_type
493 if not n_expr
.is_self
then t
= t
.not_for_self
496 if n_block
!= null then v
.visit
(n_block
)
499 var varctx
= v
.variable_ctx
500 assert varctx
isa SubVariableContext
501 v
.variable_ctx
= varctx
.prev
505 redef class AAssertExpr
506 redef meth after_typing
(v
)
508 v
.check_conform_expr
(n_expr
, v
.type_bool
)
509 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
514 redef meth its_variable
do return variable
516 redef meth after_typing
(v
)
518 _stype
= v
.variable_ctx
.stype
(variable
)
522 redef class AVarAssignExpr
523 redef meth after_typing
(v
)
525 var t
= v
.variable_ctx
.stype
(variable
)
526 v
.check_conform_expr
(n_value
, t
)
530 redef class AReassignFormExpr
531 # Compute and check method used through the reassigment operator
532 private meth do_lvalue_typing
(v
: TypingVisitor, type_lvalue
: MMType)
534 if type_lvalue
== null then
537 var name
= n_assign_op
.method_name
538 var prop
= type_lvalue
.local_class
.select_method
(name
)
540 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
543 prop
.global
.check_visibility
(v
, self, v
.module, false)
544 var psig
= prop
.signature_for
(type_lvalue
)
545 _assign_method
= prop
546 v
.check_conform_expr
(n_value
, psig
[0].not_for_self
)
547 v
.check_conform
(self, psig
.return_type
.not_for_self
, n_value
.stype
)
550 # Method used through the reassigment operator (once computed)
551 readable attr _assign_method
: MMMethod
554 redef class AVarReassignExpr
555 redef meth after_typing
(v
)
557 var t
= v
.variable_ctx
.stype
(variable
)
558 do_lvalue_typing
(v
, t
)
562 redef class PAssignOp
563 meth method_name
: Symbol is abstract
565 redef class APlusAssignOp
566 redef meth method_name
do return once
"+".to_symbol
568 redef class AMinusAssignOp
569 redef meth method_name
do return once
"-".to_symbol
572 redef class ASelfExpr
573 redef meth its_variable
do return variable
575 redef meth after_typing
(v
)
577 variable
= v
.self_var
578 _stype
= v
.variable_ctx
.stype
(variable
)
581 redef meth is_self
do return true
584 redef class AImplicitSelfExpr
585 redef meth is_implicit_self
do return true
588 redef class AIfexprExpr
589 redef meth accept_typing
(v
)
591 var old_var_ctx
= v
.variable_ctx
594 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
596 v
.variable_ctx
= old_var_ctx
599 v
.check_conform_expr
(n_expr
, v
.type_bool
)
601 if not v
.check_expr
(n_then
) or not v
.check_expr
(n_else
) then return
604 var te
= n_else
.stype
607 else if not te
< t
then
608 v
.error
(self, "Type error: {te} is not a subtype of {t}.")
616 redef class ABoolExpr
617 redef meth after_typing
(v
)
624 redef meth after_typing
(v
)
626 v
.check_conform_expr
(n_expr
, v
.type_bool
)
627 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
633 redef meth accept_typing
(v
)
635 var old_var_ctx
= v
.variable_ctx
638 if n_expr
.if_true_variable_ctx
!= null then v
.variable_ctx
= n_expr
.if_true_variable_ctx
641 if n_expr2
.if_true_variable_ctx
!= null then
642 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
644 _if_true_variable_ctx
= v
.variable_ctx
647 v
.variable_ctx
= old_var_ctx
649 v
.check_conform_expr
(n_expr
, v
.type_bool
)
650 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
656 redef meth after_typing
(v
)
658 v
.check_conform_expr
(n_expr
, v
.type_bool
)
664 redef meth after_typing
(v
)
671 redef class AFloatExpr
672 redef meth after_typing
(v
)
674 _stype
= v
.type_float
678 redef class ACharExpr
679 redef meth after_typing
(v
)
685 redef class AStringFormExpr
686 readable attr _meth_with_native
: MMMethod
687 redef meth after_typing
(v
)
689 _stype
= v
.type_string
690 _meth_with_native
= _stype
.local_class
.select_method
(once
"with_native".to_symbol
)
691 if _meth_with_native
== null then v
.error
(self, "{_stype} MUST have a with_native method.")
695 redef class ASuperstringExpr
696 readable attr _meth_with_capacity
: MMMethod
697 readable attr _meth_add
: MMMethod
698 readable attr _meth_to_s
: MMMethod
699 readable attr _atype
: MMType
700 redef meth after_typing
(v
)
702 _stype
= v
.type_string
703 _atype
= v
.type_array
(_stype
)
704 _meth_with_capacity
= _atype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
705 if _meth_with_capacity
== null then v
.error
(self, "{_atype} MUST have a with_capacity method.")
706 _meth_add
= _atype
.local_class
.select_method
(once
"add".to_symbol
)
707 if _meth_add
== null then v
.error
(self, "{_atype} MUST have an add method.")
708 _meth_to_s
= v
.type_object
.local_class
.select_method
(once
"to_s".to_symbol
)
709 if _meth_to_s
== null then v
.error
(self, "Object MUST have a to_s method.")
713 redef class ANullExpr
714 redef meth after_typing
(v
)
720 redef class AArrayExpr
721 readable attr _meth_with_capacity
: MMMethod
722 readable attr _meth_add
: MMMethod
724 redef meth after_typing
(v
)
726 var stype
: MMType = null
729 if stype
== null or (ntype
!= null and stype
< ntype
) then
734 v
.check_conform_expr
(n
, stype
)
739 private meth do_typing
(v
: TypingVisitor, element_type
: MMType)
741 _stype
= v
.type_array
(element_type
)
743 _meth_with_capacity
= _stype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
744 if _meth_with_capacity
== null then v
.error
(self, "{_stype} MUST have a with_capacity method.")
745 _meth_add
= _stype
.local_class
.select_method
(once
"add".to_symbol
)
746 if _meth_add
== null then v
.error
(self, "{_stype} MUST have an add method.")
750 redef class ARangeExpr
751 readable attr _meth_init
: MMMethod
752 redef meth after_typing
(v
)
754 var ntype
= n_expr
.stype
755 var ntype2
= n_expr2
.stype
756 if ntype
== null or ntype
== null then
759 if ntype
< ntype2
then
761 else if not ntype2
< ntype
then
762 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
765 var dtype
= v
.type_discrete
766 v
.check_conform_expr
(n_expr
, dtype
)
767 v
.check_conform_expr
(n_expr2
, dtype
)
768 _stype
= v
.type_range
(ntype
)
772 redef class ACrangeExpr
773 redef meth after_typing
(v
)
776 _meth_init
= stype
.local_class
.select_method
(once
"init".to_symbol
)
779 redef class AOrangeExpr
780 redef meth after_typing
(v
)
783 _meth_init
= stype
.local_class
.select_method
(once
"without_last".to_symbol
)
788 redef class ASuperExpr
789 special ASuperInitCall
790 # readable attr _prop: MMSrcMethod
791 readable attr _init_in_superclass
: MMMethod
792 redef meth after_typing
(v
)
794 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
795 if not precs
.is_empty
then
796 v
.local_property
.need_super
= true
797 else if v
.local_property
.global
.is_init
then
798 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
799 for p
in base_precs
do
800 if not p
.global
.is_init
then
801 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
803 precs
.add
(v
.local_class
[p
.global
])
806 if precs
.is_empty
then
807 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
809 else if precs
.length
> 1 then
810 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
813 var p
= base_precs
.first
814 assert p
isa MMMethod
815 _init_in_superclass
= p
816 register_super_init_call
(v
, p
)
817 if n_args
.length
> 0 then
818 var signature
= get_signature
(v
, v
.self_var
.stype
, p
, true)
819 _arguments
= process_signature
(v
, signature
, p
.name
, n_args
.to_a
)
822 v
.error
(self, "Error: No super method to call for {v.local_property}.")
826 if precs
.first
.signature_for
(v
.self_var
.stype
).return_type
!= null then
827 var stypes
= new Array[MMType]
828 var stype
: MMType = null
830 assert prop
isa MMMethod
831 var t
= prop
.signature_for
(v
.self_var
.stype
).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
833 if stype
== null or stype
< t
then
838 v
.check_conform
(self, t
, stype
)
842 var p
= v
.local_property
843 assert p
isa MMSrcMethod
848 redef class AAttrFormExpr
850 readable attr _prop
: MMAttribute
852 # Attribute type of the acceded attribute
853 readable attr _attr_type
: MMType
855 # Compute the attribute accessed
856 private meth do_typing
(v
: TypingVisitor)
858 if not v
.check_expr
(n_expr
) then return
859 var type_recv
= n_expr
.stype
860 var name
= n_id
.to_symbol
861 var prop
= type_recv
.local_class
.select_attribute
(name
)
863 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
865 else if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
866 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
869 var at
= prop
.signature_for
(type_recv
).return_type
870 if not n_expr
.is_self
then at
= at
.not_for_self
875 redef class AAttrExpr
876 redef meth after_typing
(v
)
886 redef class AAttrAssignExpr
887 redef meth after_typing
(v
)
893 v
.check_conform_expr
(n_value
, attr_type
)
897 redef class AAttrReassignExpr
898 redef meth after_typing
(v
)
904 do_lvalue_typing
(v
, attr_type
)
910 # The signature of the called property
911 readable attr _prop_signature
: MMSignature
913 # Compute the called global property
914 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])
916 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
917 if prop
== null then return
918 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
919 if sig
== null then return
920 var args
= process_signature
(v
, sig
, prop
.name
, raw_args
)
921 if args
== null then return
922 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
924 _prop_signature
= sig
929 private meth get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): MMMethod
931 if type_recv
== null then return null
932 var prop
= type_recv
.local_class
.select_method
(name
)
933 if prop
== null and v
.local_property
.global
.is_init
then
934 var props
= type_recv
.local_class
.super_methods_named
(name
)
935 if props
.length
> 1 then
936 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
938 else if props
.length
== 1 then
939 var p
= type_recv
.local_class
[props
.first
.global
]
940 assert p
isa MMMethod
946 if is_implicit_self
then
947 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
949 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
956 # Get the signature for a local property and a receiver
957 private meth get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
959 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
960 var psig
= prop
.signature_for
(type_recv
)
961 if not recv_is_self
then psig
= psig
.not_for_self
965 # Check the conformity of a set of arguments `raw_args' to a signature.
966 private meth process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: Array[PExpr]): Array[PExpr]
968 var par_vararg
= psig
.vararg_rank
969 var par_arity
= psig
.arity
971 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
972 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
973 v
.error
(self, "Error: '{name}' arity missmatch.")
977 var args
= new Array[PExpr]
978 for par_idx
in [0..par_arity
[ do
980 var par_type
= psig
[par_idx
]
981 if par_idx
== par_vararg
then
982 var star
= new Array[PExpr]
983 for i
in [0..(raw_arity-par_arity
)] do
984 a
= raw_args
[arg_idx
]
985 v
.check_conform_expr
(a
, par_type
)
987 arg_idx
= arg_idx
+ 1
989 var aa
= new AArrayExpr.init_aarrayexpr
(star
)
990 aa
.do_typing
(v
, par_type
)
993 a
= raw_args
[arg_idx
]
994 v
.check_conform_expr
(a
, par_type
)
995 arg_idx
= arg_idx
+ 1
1002 # Check the conformity of a set of defined closures
1003 private meth process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: Array[PClosureDef]): MMType
1005 var t
= psig
.return_type
1006 var cs
= psig
.closures
# Declared closures
1009 if not c
.is_optional
then min_arity
+= 1
1012 if cs
.length
== 0 then
1013 v
.error
(self, "Error: {name} does not require blocs.")
1014 else if cd
.length
> cs
.length
or cd
.length
< min_arity
then
1015 v
.error
(self, "Error: {name} requires {cs.length} blocs, {cd.length} found.")
1017 var old_bbst
= v
.closure_break_stype
1018 var old_bl
= v
.break_list
1019 v
.closure_break_stype
= t
1020 v
.break_list
= new Array[ABreakExpr]
1021 for i
in [0..cd
.length
[ do
1022 cd
[i
].accept_typing2
(v
, cs
[i
])
1024 for n
in v
.break_list
do
1026 if t
== null or (t
!= null and t
< ntype
) then
1030 for n
in v
.break_list
do
1031 v
.check_conform_expr
(n
, t
)
1034 v
.closure_break_stype
= old_bbst
1035 v
.break_list
= old_bl
1037 else if min_arity
!= 0 then
1038 v
.error
(self, "Error: {name} requires {cs.length} blocs.")
1043 # The invoked method (once computed)
1044 readable attr _prop
: MMMethod
1046 # The real arguments used (after star transformation) (once computed)
1047 readable attr _arguments
: Array[PExpr]
1049 # The return type (if any) (once computed)
1050 readable attr _return_type
: MMType
1053 # A possible call of constructor in a super class
1054 # Could be an explicit call or with the 'super' keyword
1055 class ASuperInitCall
1056 special AAbsSendExpr
1057 private meth register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1059 if parent
!= v
.top_block
and self != v
.top_block
then
1060 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1062 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
1063 var prev_class
: MMLocalClass = null
1064 if not v
.explicit_super_init_calls
.is_empty
then
1065 prev_class
= v
.explicit_super_init_calls
.last
.global
.intro
.local_class
1067 var order
= v
.local_class
.cshe
.reverse_linear_extension
1068 if cla
== v
.local_class
then
1069 v
.explicit_other_init_call
= true
1070 else if not order
.has
(cla
) then
1071 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1072 else if cla
== prev_class
then
1073 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1075 var last_is_found
= prev_class
== null
1077 if c
== prev_class
then
1078 last_is_found
= true
1079 else if c
== cla
then
1080 if not last_is_found
then
1081 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1083 v
.explicit_super_init_calls
.add
(property
)
1092 redef class ANewExpr
1093 special AAbsSendExpr
1094 redef meth after_typing
(v
)
1096 var t
= n_type
.stype
1097 if t
== null then return
1098 if t
.local_class
.global
.is_abstract
then
1099 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1103 if n_id
== null then
1104 name
= once
"init".to_symbol
1106 name
= n_id
.to_symbol
1109 do_typing
(v
, t
, false, false, name
, n_args
.to_a
, null)
1110 if prop
== null then return
1112 if not prop
.global
.is_init
then
1113 v
.error
(self, "Error: {prop} is not a constructor.")
1120 redef class ASendExpr
1121 special ASuperInitCall
1122 # Name of the invoked property
1123 meth name
: Symbol is abstract
1125 # Raw arguments used (withour star transformation)
1126 meth raw_arguments
: Array[PExpr] is abstract
1128 # Closure definitions
1129 meth closure_defs
: Array[PClosureDef] do return null
1131 redef meth after_typing
(v
)
1136 private meth do_all_typing
(v
: TypingVisitor)
1138 if not v
.check_expr
(n_expr
) then return
1139 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1140 if prop
== null then return
1142 if prop
.global
.is_init
then
1143 if not v
.local_property
.global
.is_init
then
1144 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1145 else if not n_expr
.is_self
then
1146 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1148 register_super_init_call
(v
, prop
)
1152 _stype
= return_type
1156 class ASendReassignExpr
1158 special AReassignFormExpr
1159 readable attr _read_prop
: MMMethod
1160 redef meth do_all_typing
(v
)
1162 if not v
.check_expr
(n_expr
) then return
1163 var raw_args
= raw_arguments
1164 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1165 if prop
== null then return
1166 if prop
.global
.is_init
then
1167 if not v
.local_property
.global
.is_init
then
1168 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1169 else if not n_expr
.is_self
then
1170 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1173 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1174 if not n_expr
.is_self
then t
= t
.not_for_self
1176 do_lvalue_typing
(v
, t
)
1179 var old_args
= arguments
1180 raw_args
.add
(n_value
)
1182 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1183 if prop
== null then return
1184 if prop
.global
.is_init
then
1185 if not v
.local_property
.global
.is_init
then
1186 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1187 else if not n_expr
.is_self
then
1188 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1192 _arguments
= old_args
# FIXME: What if star parameters do not match betwen the two methods?
1196 redef class ABinopExpr
1197 redef meth raw_arguments
do return [n_expr2
]
1200 redef meth name
do return once
"==".to_symbol
1203 redef meth name
do return once
"!=".to_symbol
1206 redef meth name
do return once
"<".to_symbol
1209 redef meth name
do return once
"<=".to_symbol
1212 redef meth name
do return once
">".to_symbol
1215 redef meth name
do return once
">=".to_symbol
1217 redef class APlusExpr
1218 redef meth name
do return once
"+".to_symbol
1220 redef class AMinusExpr
1221 redef meth name
do return once
"-".to_symbol
1223 redef class AStarshipExpr
1224 redef meth name
do return once
"<=>".to_symbol
1226 redef class AStarExpr
1227 redef meth name
do return once
"*".to_symbol
1229 redef class ASlashExpr
1230 redef meth name
do return once
"/".to_symbol
1232 redef class APercentExpr
1233 redef meth name
do return once
"%".to_symbol
1236 redef class AUminusExpr
1237 redef meth name
do return once
"unary -".to_symbol
1238 redef meth raw_arguments
do return null
1241 redef class ACallFormExpr
1242 redef meth after_typing
(v
)
1244 if n_expr
!= null and n_expr
.is_implicit_self
then
1245 var name
= n_id
.to_symbol
1246 var variable
= v
.variable_ctx
[name
]
1247 if variable
!= null then
1248 if variable
isa ClosureVariable then
1249 var n
= new AClosureCallExpr(n_id
, n_args
, n_closure_defs
)
1251 n
.variable
= variable
1255 if not n_args
.is_empty
then
1256 v
.error
(self, "Error: {name} is variable, not a function.")
1258 var vform
= variable_create
(variable
)
1259 vform
.variable
= variable
1261 vform
.after_typing
(v
)
1269 redef meth closure_defs
1271 if n_closure_defs
== null or n_closure_defs
.is_empty
then
1274 return n_closure_defs
.to_a
1278 # Create a variable acces corresponding to the call form
1279 meth variable_create
(variable
: Variable): AVarFormExpr is abstract
1282 redef class ACallExpr
1283 redef meth variable_create
(variable
)
1285 return new AVarExpr.init_avarexpr
(n_id
)
1288 redef meth name
do return n_id
.to_symbol
1289 redef meth raw_arguments
do return n_args
.to_a
1292 redef class ACallAssignExpr
1293 redef meth variable_create
(variable
)
1295 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1298 redef meth name
do return (n_id
.text
+ "=").to_symbol
1299 redef meth raw_arguments
do
1300 var res
= n_args
.to_a
1306 redef class ACallReassignExpr
1307 special ASendReassignExpr
1308 redef meth variable_create
(variable
)
1310 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1313 redef meth name
do return n_id
.to_symbol
1314 redef meth raw_arguments
do return n_args
.to_a
1317 redef class ABraExpr
1318 redef meth name
do return once
"[]".to_symbol
1319 redef meth raw_arguments
do return n_args
.to_a
1322 redef class ABraAssignExpr
1323 redef meth name
do return once
"[]=".to_symbol
1324 redef meth raw_arguments
do
1325 var res
= n_args
.to_a
1331 redef class ABraReassignExpr
1332 special ASendReassignExpr
1333 redef meth name
do return once
"[]".to_symbol
1334 redef meth raw_arguments
do return n_args
.to_a
1337 redef class AInitExpr
1338 redef meth name
do return once
"init".to_symbol
1339 redef meth raw_arguments
do return n_args
.to_a
1342 redef class AClosureCallExpr
1343 redef meth after_typing
(v
)
1346 var sig
= va
.closure
.signature
1347 var args
= process_signature
(v
, sig
, n_id
.to_symbol
, n_args
.to_a
)
1348 if closure_defs
!= null then
1349 process_closures
(v
, sig
, n_id
.to_symbol
, closure_defs
)
1351 if args
== null then return
1353 _prop_signature
= sig
1355 _stype
= sig
.return_type
1359 redef class PClosureDef
1360 attr _accept_typing2
: Bool
1361 redef meth accept_typing
(v
)
1363 # Typing is deferred, wait accept_typing2(v)
1364 if _accept_typing2
then super
1367 private meth accept_typing2
(v
: TypingVisitor, clos
: MMClosure) is abstract
1370 redef class AClosureDef
1371 redef meth accept_typing2
(v
, clos
)
1373 var sig
= clos
.signature
1374 if sig
.arity
!= n_id
.length
then
1375 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1381 var old_clos
= v
.closure
1384 v
.variable_ctx
= v
.variable_ctx
.sub
1385 variables
= new Array[AutoVariable]
1386 for i
in [0..n_id
.length
[ do
1387 var va
= new AutoVariable(n_id
[i
].to_symbol
, self)
1390 v
.variable_ctx
.add
(va
)
1393 _accept_typing2
= true
1396 v
.closure
= old_clos
1400 redef class AIsaExpr
1401 redef meth after_typing
(v
)
1403 var variable
= n_expr
.its_variable
1404 if variable
!= null then
1405 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(variable
, n_type
.stype
)
1407 _stype
= v
.type_bool
1411 redef class AAsCastExpr
1412 redef meth after_typing
(v
)
1414 v
.check_expr
(n_expr
)
1415 _stype
= n_type
.stype
1419 redef class AProxyExpr
1420 redef meth after_typing
(v
)
1422 _stype
= n_expr
.stype