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 blocks
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 # Make the if_true_variable_ctx of the expression effective
64 private meth use_if_true_variable_ctx
(e
: PExpr)
66 var ctx
= e
.if_true_variable_ctx
72 init(tc
, module) do super
74 private meth get_default_constructor_for
(n
: PNode, c
: MMLocalClass, prop
: MMSrcMethod): MMMethod
77 #var prop = v.local_property
78 #assert prop isa MMMethod
79 var candidates
= new Array[MMMethod]
80 var false_candidates
= new Array[MMMethod]
81 var parity
= prop
.signature
.arity
82 for g
in c
.global_properties
do
83 if not g
.is_init_for
(c
) then continue
85 var gps
= gp
.signature_for
(c
.get_type
)
86 assert gp
isa MMSrcMethod
87 var garity
= gps
.arity
88 if prop
!= null and gp
.name
== prop
.name
then
89 if garity
== 0 or (parity
== garity
and prop
.signature
< gps
) then
92 false_candidates
.add
(gp
)
94 else if garity
== 0 and gp
.name
== once
("init".to_symbol
) then
96 false_candidates
.add
(gp
)
98 false_candidates
.add
(gp
)
101 if candidates
.length
== 1 then
102 return candidates
.first
103 else if candidates
.length
> 0 then
104 var a
= new Array[String]
105 for p
in candidates
do
106 a
.add
("{p.full_name}{p.signature}")
108 v
.error
(n
, "Error: Conflicting default constructor to call for {c}: {a.join(", ")}.")
110 else if false_candidates
.length
> 0 then
111 var a
= new Array[String]
112 for p
in false_candidates
do
113 a
.add
("{p.full_name}{p.signature}")
115 v
.error
(n
, "Error: there is no available compatible constrctor in {c}. Discarded candidates are {a.join(", ")}.")
118 v
.error
(n
, "Error: there is no available compatible constrctor in {c}.")
124 # Associate symbols to variable and variables to type
126 private class VariableContext
127 # Look for the variable from its name
128 # Return null if nothing found
129 meth
[](s
: Symbol): Variable
131 if _dico
.has_key
(s
) then
138 # Register a new variable with its name
139 meth add
(v
: Variable)
145 # The effective static type of a given variable
146 # May be different from the declaration static type
147 meth stype
(v
: Variable): MMType
152 # Variables by name (in the current context only)
153 attr _dico
: Map[Symbol, Variable]
155 # Build a new VariableContext
156 meth sub
: SubVariableContext
158 return new SubVariableContext.with_prev
(self)
161 # Build a nested VariableContext with new variable information
162 meth sub_with
(v
: Variable, t
: MMType): SubVariableContext
164 return new CastVariableContext.with_prev
(self, v
, t
)
169 _dico
= new HashMap[Symbol, Variable]
173 private class SubVariableContext
174 special VariableContext
175 readable attr _prev
: VariableContext
179 if _dico
.has_key
(s
) then
191 init with_prev
(p
: VariableContext)
198 private class CastVariableContext
199 special SubVariableContext
200 attr _variable
: Variable
201 attr _var_type
: MMType
205 if _variable
== v
then
211 init with_prev
(p
: VariableContext, v
: Variable, t
: MMType)
219 ###############################################################################
222 private meth accept_typing
(v
: TypingVisitor)
224 accept_abs_syntax_visitor
(v
)
227 private meth after_typing
(v
: TypingVisitor) do end
230 redef class PClassdef
231 redef meth accept_typing
(v
)
233 v
.self_var
= new ParamVariable("self".to_symbol
, self)
234 v
.self_var
.stype
= local_class
.get_type
239 redef class AAttrPropdef
240 redef meth accept_typing
(v
)
243 if n_expr
!= null then
244 v
.check_conform_expr
(n_expr
, prop
.signature
.return_type
)
249 redef class AMethPropdef
250 redef readable attr _self_var
: ParamVariable
251 redef meth accept_typing
(v
)
253 v
.variable_ctx
= new VariableContext
254 _self_var
= v
.self_var
259 redef class AConcreteInitPropdef
260 readable attr _super_init_calls
: Array[MMMethod] = new Array[MMMethod]
261 readable attr _explicit_super_init_calls
: Array[MMMethod] = new Array[MMMethod]
262 redef meth accept_typing
(v
)
264 v
.top_block
= n_block
265 v
.explicit_super_init_calls
= explicit_super_init_calls
266 v
.explicit_other_init_call
= false
268 if v
.explicit_other_init_call
or method
.global
.intro
!= method
then
272 var l
= explicit_super_init_calls
.length
273 var cur_m
: MMMethod = null
274 var cur_c
: MMLocalClass = null
276 cur_m
= explicit_super_init_calls
[i
]
277 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
280 while j
< v
.local_class
.cshe
.direct_greaters
.length
do
281 var c
= v
.local_class
.cshe
.direct_greaters
[j
]
282 if c
.global
.is_interface
or c
.global
.is_universal
or c
.global
.is_mixin
then
284 else if cur_c
!= null and (c
.cshe
<= cur_c
or cur_c
.global
.is_mixin
) then
285 if c
== cur_c
then j
+= 1
286 super_init_calls
.add
(cur_m
)
289 cur_m
= explicit_super_init_calls
[i
]
290 cur_c
= cur_m
.global
.intro
.local_class
.for_module
(v
.module)
296 var p
= v
.get_default_constructor_for
(self, c
, method
)
298 super_init_calls
.add
(p
)
308 redef meth after_typing
(v
)
310 # TODO: why the test?
311 if v
.variable_ctx
!= null then
312 v
.variable_ctx
.add
(variable
)
317 redef class AClosureDecl
318 # The corresponding escapable object
319 readable attr _escapable
: EscapableBlock
321 redef meth accept_typing
(v
)
323 # Register the closure for ClosureCallExpr
324 v
.variable_ctx
.add
(variable
)
326 var old_var_ctx
= v
.variable_ctx
327 v
.variable_ctx
= v
.variable_ctx
.sub
329 _escapable
= new EscapableClosure(self, variable
.closure
, null)
330 v
.escapable_ctx
.push
(_escapable
)
334 v
.variable_ctx
= old_var_ctx
340 readable attr _stype
: MMType
341 redef meth after_typing
(v
)
343 _stype
= get_stype
(v
)
348 redef readable attr _stype
: MMType
350 # Is the expression the implicit receiver
351 meth is_implicit_self
: Bool do return false
353 # Is the expression the current receiver (implicit or explicit)
354 meth is_self
: Bool do return false
356 # The variable accessed is any
357 meth its_variable
: Variable do return null
359 # The variable type information if current boolean expression is true
360 readable private attr _if_true_variable_ctx
: VariableContext
363 redef class AVardeclExpr
364 redef meth after_typing
(v
)
366 var va
= new VarVariable(n_id
.to_symbol
, self)
368 v
.variable_ctx
.add
(va
)
370 if n_type
!= null then
371 va
.stype
= n_type
.stype
372 if n_expr
!= null then
373 v
.check_conform_expr
(n_expr
, va
.stype
)
377 va
.stype
= n_expr
.stype
382 redef class ABlockExpr
383 redef meth accept_typing
(v
)
385 var old_var_ctx
= v
.variable_ctx
386 v
.variable_ctx
= v
.variable_ctx
.sub
390 v
.variable_ctx
= old_var_ctx
394 redef class AReturnExpr
395 redef meth after_typing
(v
)
397 var t
= v
.local_property
.signature
.return_type
398 if n_expr
== null and t
!= null then
399 v
.error
(self, "Error: Return without value in a function.")
400 else if n_expr
!= null and t
== null then
401 v
.error
(self, "Error: Return with value in a procedure.")
402 else if n_expr
!= null and t
!= null then
403 v
.check_conform_expr
(n_expr
, t
)
408 redef class AContinueExpr
409 redef meth after_typing
(v
)
411 var esc
= compute_escapable_block
(v
.escapable_ctx
)
412 if esc
== null then return
414 if esc
.is_break_block
then
415 v
.error
(self, "Error: 'continue' forbiden in break blocks.")
419 var t
= esc
.continue_stype
420 if n_expr
== null and t
!= null then
421 v
.error
(self, "Error: continue with a value required in this block.")
422 else if n_expr
!= null and t
== null then
423 v
.error
(self, "Error: continue without value required in this block.")
424 else if n_expr
!= null and t
!= null then
425 v
.check_conform_expr
(n_expr
, t
)
430 redef class ABreakExpr
431 redef meth after_typing
(v
)
433 var esc
= compute_escapable_block
(v
.escapable_ctx
)
434 if esc
== null then return
436 var bl
= esc
.break_list
437 if n_expr
== null and bl
!= null then
438 v
.error
(self, "Error: break with a value required in this block.")
439 else if n_expr
!= null and bl
== null then
440 v
.error
(self, "Error: break without value required in this block.")
441 else if n_expr
!= null and bl
!= null then
442 # Typing check can only be done later
449 redef meth accept_typing
(v
)
451 var old_var_ctx
= v
.variable_ctx
453 v
.check_conform_expr
(n_expr
, v
.type_bool
)
455 v
.use_if_true_variable_ctx
(n_expr
)
458 # Restore variable ctx
459 v
.variable_ctx
= old_var_ctx
461 if n_else
!= null then
463 v
.variable_ctx
= old_var_ctx
468 redef class AWhileExpr
469 # The corresponding escapable block
470 readable attr _escapable
: EscapableBlock
472 redef meth accept_typing
(v
)
474 _escapable
= new EscapableBlock(self)
475 v
.escapable_ctx
.push
(_escapable
)
479 v
.check_conform_expr
(n_expr
, v
.type_bool
)
485 # The corresponding escapable block
486 readable attr _escapable
: EscapableBlock
488 readable attr _meth_iterator
: MMMethod
489 readable attr _meth_is_ok
: MMMethod
490 readable attr _meth_item
: MMMethod
491 readable attr _meth_next
: MMMethod
492 redef meth accept_typing
(v
)
494 _escapable
= new EscapableBlock(self)
495 v
.escapable_ctx
.push
(_escapable
)
497 v
.variable_ctx
= v
.variable_ctx
.sub
498 var va
= new AutoVariable(n_id
.to_symbol
, self)
500 v
.variable_ctx
.add
(va
)
504 var expr_type
= n_expr
.stype
505 if not v
.check_conform_expr
(n_expr
, v
.type_collection
) then
508 _meth_iterator
= expr_type
.local_class
.select_method
(once
("iterator".to_symbol
))
509 if _meth_iterator
== null then
510 v
.error
(self, "Error: Collection MUST have an iterate method")
513 var iter_type
= _meth_iterator
.signature_for
(expr_type
).return_type
514 _meth_is_ok
= iter_type
.local_class
.select_method
(once
("is_ok".to_symbol
))
515 if _meth_is_ok
== null then
516 v
.error
(self, "Error: {iter_type} MUST have an is_ok method")
519 _meth_item
= iter_type
.local_class
.select_method
(once
("item".to_symbol
))
520 if _meth_item
== null then
521 v
.error
(self, "Error: {iter_type} MUST have an item method")
524 _meth_next
= iter_type
.local_class
.select_method
(once
("next".to_symbol
))
525 if _meth_next
== null then
526 v
.error
(self, "Error: {iter_type} MUST have a next method")
529 var t
= _meth_item
.signature_for
(iter_type
).return_type
530 if not n_expr
.is_self
then t
= t
.not_for_self
533 if n_block
!= null then v
.visit
(n_block
)
536 var varctx
= v
.variable_ctx
537 assert varctx
isa SubVariableContext
538 v
.variable_ctx
= varctx
.prev
544 redef class AAssertExpr
545 redef meth after_typing
(v
)
547 v
.check_conform_expr
(n_expr
, v
.type_bool
)
548 v
.use_if_true_variable_ctx
(n_expr
)
553 redef meth its_variable
do return variable
555 redef meth after_typing
(v
)
557 _stype
= v
.variable_ctx
.stype
(variable
)
561 redef class AVarAssignExpr
562 redef meth after_typing
(v
)
564 var t
= v
.variable_ctx
.stype
(variable
)
565 v
.check_conform_expr
(n_value
, t
)
569 redef class AReassignFormExpr
570 # Compute and check method used through the reassigment operator
571 private meth do_lvalue_typing
(v
: TypingVisitor, type_lvalue
: MMType)
573 if type_lvalue
== null then
576 var name
= n_assign_op
.method_name
577 var lc
= type_lvalue
.local_class
578 if not lc
.has_global_property_by_name
(name
) then
579 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
582 var prop
= lc
.select_method
(name
)
583 prop
.global
.check_visibility
(v
, self, v
.module, false)
584 var psig
= prop
.signature_for
(type_lvalue
)
585 _assign_method
= prop
586 v
.check_conform_expr
(n_value
, psig
[0].not_for_self
)
587 v
.check_conform
(self, psig
.return_type
.not_for_self
, n_value
.stype
)
590 # Method used through the reassigment operator (once computed)
591 readable attr _assign_method
: MMMethod
594 redef class AVarReassignExpr
595 redef meth after_typing
(v
)
597 var t
= v
.variable_ctx
.stype
(variable
)
598 do_lvalue_typing
(v
, t
)
602 redef class PAssignOp
603 meth method_name
: Symbol is abstract
605 redef class APlusAssignOp
606 redef meth method_name
do return once
"+".to_symbol
608 redef class AMinusAssignOp
609 redef meth method_name
do return once
"-".to_symbol
612 redef class ASelfExpr
613 redef meth its_variable
do return variable
615 redef meth after_typing
(v
)
617 variable
= v
.self_var
618 _stype
= v
.variable_ctx
.stype
(variable
)
621 redef meth is_self
do return true
624 redef class AImplicitSelfExpr
625 redef meth is_implicit_self
do return true
628 redef class AIfexprExpr
629 redef meth accept_typing
(v
)
631 var old_var_ctx
= v
.variable_ctx
634 v
.use_if_true_variable_ctx
(n_expr
)
636 v
.variable_ctx
= old_var_ctx
639 v
.check_conform_expr
(n_expr
, v
.type_bool
)
641 _stype
= v
.check_conform_multiexpr
(null, [n_then
, n_else
])
645 redef class ABoolExpr
646 redef meth after_typing
(v
)
653 redef meth after_typing
(v
)
655 v
.check_conform_expr
(n_expr
, v
.type_bool
)
656 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
662 redef meth accept_typing
(v
)
664 var old_var_ctx
= v
.variable_ctx
667 v
.use_if_true_variable_ctx
(n_expr
)
670 if n_expr2
.if_true_variable_ctx
!= null then
671 _if_true_variable_ctx
= n_expr2
.if_true_variable_ctx
673 _if_true_variable_ctx
= v
.variable_ctx
676 v
.variable_ctx
= old_var_ctx
678 v
.check_conform_expr
(n_expr
, v
.type_bool
)
679 v
.check_conform_expr
(n_expr2
, v
.type_bool
)
685 redef meth after_typing
(v
)
687 v
.check_conform_expr
(n_expr
, v
.type_bool
)
693 redef meth after_typing
(v
)
700 redef class AFloatExpr
701 redef meth after_typing
(v
)
703 _stype
= v
.type_float
707 redef class ACharExpr
708 redef meth after_typing
(v
)
714 redef class AStringFormExpr
715 readable attr _meth_with_native
: MMMethod
716 redef meth after_typing
(v
)
718 _stype
= v
.type_string
719 _meth_with_native
= _stype
.local_class
.select_method
(once
"with_native".to_symbol
)
720 if _meth_with_native
== null then v
.error
(self, "{_stype} MUST have a with_native method.")
724 redef class ASuperstringExpr
725 readable attr _meth_with_capacity
: MMMethod
726 readable attr _meth_add
: MMMethod
727 readable attr _meth_to_s
: MMMethod
728 readable attr _atype
: MMType
729 redef meth after_typing
(v
)
731 _stype
= v
.type_string
732 _atype
= v
.type_array
(_stype
)
733 _meth_with_capacity
= _atype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
734 if _meth_with_capacity
== null then v
.error
(self, "{_atype} MUST have a with_capacity method.")
735 _meth_add
= _atype
.local_class
.select_method
(once
"add".to_symbol
)
736 if _meth_add
== null then v
.error
(self, "{_atype} MUST have an add method.")
737 _meth_to_s
= v
.type_object
.local_class
.select_method
(once
"to_s".to_symbol
)
738 if _meth_to_s
== null then v
.error
(self, "Object MUST have a to_s method.")
742 redef class ANullExpr
743 redef meth after_typing
(v
)
749 redef class AArrayExpr
750 readable attr _meth_with_capacity
: MMMethod
751 readable attr _meth_add
: MMMethod
753 redef meth after_typing
(v
)
755 var stype
= v
.check_conform_multiexpr
(null, n_exprs
)
756 if stype
== null then return
760 private meth do_typing
(v
: TypingVisitor, element_type
: MMType)
762 _stype
= v
.type_array
(element_type
)
764 _meth_with_capacity
= _stype
.local_class
.select_method
(once
"with_capacity".to_symbol
)
765 if _meth_with_capacity
== null then v
.error
(self, "{_stype} MUST have a with_capacity method.")
766 _meth_add
= _stype
.local_class
.select_method
(once
"add".to_symbol
)
767 if _meth_add
== null then v
.error
(self, "{_stype} MUST have an add method.")
771 redef class ARangeExpr
772 readable attr _meth_init
: MMMethod
773 redef meth after_typing
(v
)
775 var ntype
= n_expr
.stype
776 var ntype2
= n_expr2
.stype
777 if ntype
== null or ntype
== null then
780 if ntype
< ntype2
then
782 else if not ntype2
< ntype
then
783 v
.error
(self, "Type error: {ntype} incompatible with {ntype2}.")
786 var dtype
= v
.type_discrete
787 v
.check_conform_expr
(n_expr
, dtype
)
788 v
.check_conform_expr
(n_expr2
, dtype
)
789 _stype
= v
.type_range
(ntype
)
793 redef class ACrangeExpr
794 redef meth after_typing
(v
)
797 _meth_init
= stype
.local_class
.select_method
(once
"init".to_symbol
)
800 redef class AOrangeExpr
801 redef meth after_typing
(v
)
804 _meth_init
= stype
.local_class
.select_method
(once
"without_last".to_symbol
)
809 redef class ASuperExpr
810 special ASuperInitCall
811 # readable attr _prop: MMSrcMethod
812 readable attr _init_in_superclass
: MMMethod
813 redef meth after_typing
(v
)
815 var precs
: Array[MMLocalProperty] = v
.local_property
.prhe
.direct_greaters
816 if not precs
.is_empty
then
817 v
.local_property
.need_super
= true
818 else if v
.local_property
.global
.is_init
then
819 var base_precs
= v
.local_class
.super_methods_named
(v
.local_property
.name
)
820 for p
in base_precs
do
821 if not p
.global
.is_init
then
822 v
.error
(self, "Error: {p.local_class}::{p} is not a constructor.")
824 precs
.add
(v
.local_class
[p
.global
])
827 if precs
.is_empty
then
828 v
.error
(self, "Error: No contructor named {v.local_property.name} in superclasses.")
830 else if precs
.length
> 1 then
831 v
.error
(self, "Error: Conflicting contructors named {v.local_property.name} in superclasses: {precs.join(", ")}.")
834 var p
= base_precs
.first
835 assert p
isa MMMethod
836 _init_in_superclass
= p
837 register_super_init_call
(v
, p
)
838 if n_args
.length
> 0 then
839 var signature
= get_signature
(v
, v
.self_var
.stype
, p
, true)
840 _arguments
= process_signature
(v
, signature
, p
.name
, n_args
.to_a
)
843 v
.error
(self, "Error: No super method to call for {v.local_property}.")
847 if precs
.first
.signature_for
(v
.self_var
.stype
).return_type
!= null then
848 var stypes
= new Array[MMType]
849 var stype
: MMType = null
851 assert prop
isa MMMethod
852 var t
= prop
.signature_for
(v
.self_var
.stype
).return_type
.for_module
(v
.module).adapt_to
(v
.local_property
.signature
.recv
)
854 if stype
== null or stype
< t
then
859 v
.check_conform
(self, t
, stype
)
863 var p
= v
.local_property
864 assert p
isa MMSrcMethod
869 redef class AAttrFormExpr
871 readable attr _prop
: MMAttribute
873 # Attribute type of the acceded attribute
874 readable attr _attr_type
: MMType
876 # Compute the attribute accessed
877 private meth do_typing
(v
: TypingVisitor)
879 if not v
.check_expr
(n_expr
) then return
880 var type_recv
= n_expr
.stype
881 var name
= n_id
.to_symbol
882 var lc
= type_recv
.local_class
883 if not lc
.has_global_property_by_name
(name
) then
884 v
.error
(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
887 var prop
= lc
.select_attribute
(name
)
888 if v
.module.visibility_for
(prop
.global
.local_class
.module) < 3 then
889 v
.error
(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
892 var at
= prop
.signature_for
(type_recv
).return_type
893 if not n_expr
.is_self
then at
= at
.not_for_self
898 redef class AAttrExpr
899 redef meth after_typing
(v
)
909 redef class AAttrAssignExpr
910 redef meth after_typing
(v
)
916 v
.check_conform_expr
(n_value
, attr_type
)
920 redef class AAttrReassignExpr
921 redef meth after_typing
(v
)
927 do_lvalue_typing
(v
, attr_type
)
931 class AAbsAbsSendExpr
933 # The signature of the called property
934 readable attr _prop_signature
: MMSignature
936 # The real arguments used (after star transformation) (once computed)
937 readable attr _arguments
: Array[PExpr]
939 # Check the conformity of a set of arguments `raw_args' to a signature.
940 private meth process_signature
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, raw_args
: Array[PExpr]): Array[PExpr]
942 var par_vararg
= psig
.vararg_rank
943 var par_arity
= psig
.arity
945 if raw_args
== null then raw_arity
= 0 else raw_arity
= raw_args
.length
946 if par_arity
> raw_arity
or (par_arity
!= raw_arity
and par_vararg
== -1) then
947 v
.error
(self, "Error: '{name}' arity missmatch.")
951 var args
= new Array[PExpr]
952 for par_idx
in [0..par_arity
[ do
954 var par_type
= psig
[par_idx
]
955 if par_idx
== par_vararg
then
956 var star
= new Array[PExpr]
957 for i
in [0..(raw_arity-par_arity
)] do
958 a
= raw_args
[arg_idx
]
959 v
.check_conform_expr
(a
, par_type
)
961 arg_idx
= arg_idx
+ 1
963 var aa
= new AArrayExpr.init_aarrayexpr
(star
)
964 aa
.do_typing
(v
, par_type
)
967 a
= raw_args
[arg_idx
]
968 v
.check_conform_expr
(a
, par_type
)
969 arg_idx
= arg_idx
+ 1
976 # Check the conformity of a set of defined closures
977 private meth process_closures
(v
: TypingVisitor, psig
: MMSignature, name
: Symbol, cd
: Array[PClosureDef]): MMType
979 var t
= psig
.return_type
980 var cs
= psig
.closures
# Declared closures
983 if not c
.is_optional
then min_arity
+= 1
986 if cs
.length
== 0 then
987 v
.error
(self, "Error: {name} does not require blocks.")
988 else if cd
.length
> cs
.length
or cd
.length
< min_arity
then
989 v
.error
(self, "Error: {name} requires {cs.length} blocks, {cd.length} found.")
991 # Initialize the break list if a value is required for breaks (ie. if the method is a function)
992 var break_list
: Array[ABreakExpr] = null
993 if t
!= null then break_list
= new Array[ABreakExpr]
995 # Process each closure definition
996 for i
in [0..cd
.length
[ do
999 var esc
= new EscapableClosure(cdi
, csi
, break_list
)
1000 v
.escapable_ctx
.push
(esc
)
1001 cdi
.accept_typing2
(v
, esc
)
1005 # Check break type conformity
1006 if break_list
!= null then
1007 t
= v
.check_conform_multiexpr
(t
, break_list
)
1010 else if min_arity
!= 0 then
1011 v
.error
(self, "Error: {name} requires {cs.length} blocks.")
1018 special AAbsAbsSendExpr
1019 # Compute the called global property
1020 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])
1022 var prop
= get_property
(v
, type_recv
, is_implicit_self
, name
)
1023 if prop
== null then return
1024 var sig
= get_signature
(v
, type_recv
, prop
, recv_is_self
)
1025 if sig
== null then return
1026 var args
= process_signature
(v
, sig
, prop
.name
, raw_args
)
1027 if args
== null then return
1028 var rtype
= process_closures
(v
, sig
, prop
.name
, closure_defs
)
1030 _prop_signature
= sig
1032 _return_type
= rtype
1035 private meth get_property
(v
: TypingVisitor, type_recv
: MMType, is_implicit_self
: Bool, name
: Symbol): MMMethod
1037 if type_recv
== null then return null
1038 var lc
= type_recv
.local_class
1039 var prop
: MMMethod = null
1040 if lc
.has_global_property_by_name
(name
) then prop
= lc
.select_method
(name
)
1041 if prop
== null and v
.local_property
.global
.is_init
then
1042 var props
= type_recv
.local_class
.super_methods_named
(name
)
1043 if props
.length
> 1 then
1044 v
.error
(self, "Error: Ambigous method name '{name}' for {props.join(", ")}. Use explicit designation.")
1046 else if props
.length
== 1 then
1047 var p
= type_recv
.local_class
[props
.first
.global
]
1048 assert p
isa MMMethod
1053 if prop
== null then
1054 if is_implicit_self
then
1055 v
.error
(self, "Error: Method or variable '{name}' unknown in {type_recv}.")
1057 v
.error
(self, "Error: Method '{name}' doesn't exists in {type_recv}.")
1064 # Get the signature for a local property and a receiver
1065 private meth get_signature
(v
: TypingVisitor, type_recv
: MMType, prop
: MMMethod, recv_is_self
: Bool): MMSignature
1067 prop
.global
.check_visibility
(v
, self, v
.module, recv_is_self
)
1068 var psig
= prop
.signature_for
(type_recv
)
1069 if not recv_is_self
then psig
= psig
.not_for_self
1073 # The invoked method (once computed)
1074 readable attr _prop
: MMMethod
1076 # The return type (if any) (once computed)
1077 readable attr _return_type
: MMType
1080 # A possible call of constructor in a super class
1081 # Could be an explicit call or with the 'super' keyword
1082 class ASuperInitCall
1083 special AAbsSendExpr
1084 private meth register_super_init_call
(v
: TypingVisitor, property
: MMMethod)
1086 if parent
!= v
.top_block
and self != v
.top_block
then
1087 v
.error
(self, "Error: Constructor invocation {property} must not be in nested block.")
1089 var cla
= v
.module[property
.global
.intro
.local_class
.global
]
1090 var prev_class
: MMLocalClass = null
1091 if not v
.explicit_super_init_calls
.is_empty
then
1092 prev_class
= v
.explicit_super_init_calls
.last
.global
.intro
.local_class
1094 var order
= v
.local_class
.cshe
.reverse_linear_extension
1095 if cla
== v
.local_class
then
1096 v
.explicit_other_init_call
= true
1097 else if not order
.has
(cla
) then
1098 v
.error
(self, "Error: Constructor of class {cla} must be one in {order.join(", ")}.")
1099 else if cla
== prev_class
then
1100 v
.error
(self, "Error: Only one super constructor invocation of class {cla} is allowed.")
1102 var last_is_found
= prev_class
== null
1104 if c
== prev_class
then
1105 last_is_found
= true
1106 else if c
== cla
then
1107 if not last_is_found
then
1108 v
.error
(self, "Error: Constructor of {c} must be invoked before constructor of {prev_class}")
1110 v
.explicit_super_init_calls
.add
(property
)
1119 redef class ANewExpr
1120 special AAbsSendExpr
1121 redef meth after_typing
(v
)
1123 var t
= n_type
.stype
1124 if t
== null then return
1125 if t
.local_class
.global
.is_abstract
then
1126 v
.error
(self, "Error: try to instantiate abstract class {t.local_class}.")
1130 if n_id
== null then
1131 name
= once
"init".to_symbol
1133 name
= n_id
.to_symbol
1136 do_typing
(v
, t
, false, false, name
, n_args
.to_a
, null)
1137 if prop
== null then return
1139 if not prop
.global
.is_init
then
1140 v
.error
(self, "Error: {prop} is not a constructor.")
1147 redef class ASendExpr
1148 special ASuperInitCall
1149 # Name of the invoked property
1150 meth name
: Symbol is abstract
1152 # Raw arguments used (withour star transformation)
1153 meth raw_arguments
: Array[PExpr] is abstract
1155 # Closure definitions
1156 meth closure_defs
: Array[PClosureDef] do return null
1158 redef meth after_typing
(v
)
1163 private meth do_all_typing
(v
: TypingVisitor)
1165 if not v
.check_expr
(n_expr
) then return
1166 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_arguments
, closure_defs
)
1167 if prop
== null then return
1169 if prop
.global
.is_init
then
1170 if not v
.local_property
.global
.is_init
then
1171 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1172 else if not n_expr
.is_self
then
1173 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1175 register_super_init_call
(v
, prop
)
1179 _stype
= return_type
1183 class ASendReassignExpr
1185 special AReassignFormExpr
1186 readable attr _read_prop
: MMMethod
1187 redef meth do_all_typing
(v
)
1189 if not v
.check_expr
(n_expr
) then return
1190 var raw_args
= raw_arguments
1191 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, name
, raw_args
, null)
1192 if prop
== null then return
1193 if prop
.global
.is_init
then
1194 if not v
.local_property
.global
.is_init
then
1195 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1196 else if not n_expr
.is_self
then
1197 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1200 var t
= prop
.signature_for
(n_expr
.stype
).return_type
1201 if not n_expr
.is_self
then t
= t
.not_for_self
1203 do_lvalue_typing
(v
, t
)
1206 var old_args
= arguments
1207 raw_args
.add
(n_value
)
1209 do_typing
(v
, n_expr
.stype
, n_expr
.is_implicit_self
, n_expr
.is_self
, "{name}=".to_symbol
, raw_args
, null)
1210 if prop
== null then return
1211 if prop
.global
.is_init
then
1212 if not v
.local_property
.global
.is_init
then
1213 v
.error
(self, "Error: try to invoke constructor {prop} in a method.")
1214 else if not n_expr
.is_self
then
1215 v
.error
(self, "Error: constructor {prop} is not invoken on 'self'.")
1219 _arguments
= old_args
# FIXME: What if star parameters do not match betwen the two methods?
1223 redef class ABinopExpr
1224 redef meth raw_arguments
do return [n_expr2
]
1227 redef meth name
do return once
"==".to_symbol
1230 redef meth name
do return once
"!=".to_symbol
1233 redef meth name
do return once
"<".to_symbol
1236 redef meth name
do return once
"<=".to_symbol
1239 redef meth name
do return once
">".to_symbol
1242 redef meth name
do return once
">=".to_symbol
1244 redef class APlusExpr
1245 redef meth name
do return once
"+".to_symbol
1247 redef class AMinusExpr
1248 redef meth name
do return once
"-".to_symbol
1250 redef class AStarshipExpr
1251 redef meth name
do return once
"<=>".to_symbol
1253 redef class AStarExpr
1254 redef meth name
do return once
"*".to_symbol
1256 redef class ASlashExpr
1257 redef meth name
do return once
"/".to_symbol
1259 redef class APercentExpr
1260 redef meth name
do return once
"%".to_symbol
1263 redef class AUminusExpr
1264 redef meth name
do return once
"unary -".to_symbol
1265 redef meth raw_arguments
do return null
1268 redef class ACallFormExpr
1269 redef meth after_typing
(v
)
1271 if n_expr
!= null and n_expr
.is_implicit_self
then
1272 var name
= n_id
.to_symbol
1273 var variable
= v
.variable_ctx
[name
]
1274 if variable
!= null then
1275 if variable
isa ClosureVariable then
1276 var n
= new AClosureCallExpr.init_aclosurecallexpr
(n_id
, n_args
, n_closure_defs
)
1278 n
.variable
= variable
1282 if not n_args
.is_empty
then
1283 v
.error
(self, "Error: {name} is variable, not a function.")
1285 var vform
= variable_create
(variable
)
1286 vform
.variable
= variable
1288 vform
.after_typing
(v
)
1297 redef meth closure_defs
1299 if n_closure_defs
== null or n_closure_defs
.is_empty
then
1302 return n_closure_defs
.to_a
1306 # Create a variable acces corresponding to the call form
1307 meth variable_create
(variable
: Variable): AVarFormExpr is abstract
1310 redef class ACallExpr
1311 redef meth variable_create
(variable
)
1313 return new AVarExpr.init_avarexpr
(n_id
)
1316 redef meth name
do return n_id
.to_symbol
1317 redef meth raw_arguments
do return n_args
.to_a
1320 redef class ACallAssignExpr
1321 redef meth variable_create
(variable
)
1323 return new AVarAssignExpr.init_avarassignexpr
(n_id
, n_assign
, n_value
)
1326 redef meth name
do return (n_id
.text
+ "=").to_symbol
1327 redef meth raw_arguments
do
1328 var res
= n_args
.to_a
1334 redef class ACallReassignExpr
1335 special ASendReassignExpr
1336 redef meth variable_create
(variable
)
1338 return new AVarReassignExpr.init_avarreassignexpr
(n_id
, n_assign_op
, n_value
)
1341 redef meth name
do return n_id
.to_symbol
1342 redef meth raw_arguments
do return n_args
.to_a
1345 redef class ABraExpr
1346 redef meth name
do return once
"[]".to_symbol
1347 redef meth raw_arguments
do return n_args
.to_a
1350 redef class ABraAssignExpr
1351 redef meth name
do return once
"[]=".to_symbol
1352 redef meth raw_arguments
do
1353 var res
= n_args
.to_a
1359 redef class ABraReassignExpr
1360 special ASendReassignExpr
1361 redef meth name
do return once
"[]".to_symbol
1362 redef meth raw_arguments
do return n_args
.to_a
1365 redef class AInitExpr
1366 redef meth name
do return once
"init".to_symbol
1367 redef meth raw_arguments
do return n_args
.to_a
1370 redef class AClosureCallExpr
1371 special AAbsAbsSendExpr
1372 redef meth after_typing
(v
)
1375 var sig
= va
.closure
.signature
1376 var args
= process_signature
(v
, sig
, n_id
.to_symbol
, n_args
.to_a
)
1377 if not n_closure_defs
.is_empty
then
1378 process_closures
(v
, sig
, n_id
.to_symbol
, n_closure_defs
.to_a
)
1380 if args
== null then return
1381 _prop_signature
= sig
1383 _stype
= sig
.return_type
1387 redef class PClosureDef
1388 # The corresponding escapable object
1389 readable attr _escapable
: EscapableBlock
1391 attr _accept_typing2
: Bool
1392 redef meth accept_typing
(v
)
1394 # Typing is deferred, wait accept_typing2(v)
1395 if _accept_typing2
then super
1398 private meth accept_typing2
(v
: TypingVisitor, esc
: EscapableClosure) is abstract
1401 redef class AClosureDef
1402 redef meth accept_typing2
(v
, esc
)
1406 var sig
= esc
.closure
.signature
1407 if sig
.arity
!= n_id
.length
then
1408 v
.error
(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
1412 closure
= esc
.closure
1414 v
.variable_ctx
= v
.variable_ctx
.sub
1415 variables
= new Array[AutoVariable]
1416 for i
in [0..n_id
.length
[ do
1417 var va
= new AutoVariable(n_id
[i
].to_symbol
, self)
1420 v
.variable_ctx
.add
(va
)
1423 _accept_typing2
= true
1428 redef class AIsaExpr
1429 redef meth after_typing
(v
)
1431 var variable
= n_expr
.its_variable
1432 if variable
!= null then
1433 _if_true_variable_ctx
= v
.variable_ctx
.sub_with
(variable
, n_type
.stype
)
1435 _stype
= v
.type_bool
1439 redef class AAsCastExpr
1440 redef meth after_typing
(v
)
1442 v
.check_expr
(n_expr
)
1443 _stype
= n_type
.stype
1447 redef class AProxyExpr
1448 redef meth after_typing
(v
)
1450 _stype
= n_expr
.stype