1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 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 # Intraprocedural resolution of static types and OO-services
18 # By OO-services we mean message sending, attribute access, instantiation, etc.
22 import modelize_property
26 redef class ToolContext
27 var typing_phase
: Phase = new TypingPhase(self, [flow_phase
, modelize_property_phase
, local_var_init_phase
])
30 private class TypingPhase
32 redef fun process_npropdef
(npropdef
) do npropdef
.do_typing
(toolcontext
.modelbuilder
)
35 private class TypeVisitor
36 var modelbuilder
: ModelBuilder
38 # The module of the analysis
39 # Used to correctly query the model
42 # The static type of the receiver
43 # Mainly used for type tests and type resolutions
44 var anchor
: nullable MClassType
46 # The analyzed mclassdef
47 var mclassdef
: nullable MClassDef
49 # The analyzed property
50 var mpropdef
: nullable MPropDef
52 var selfvariable
: Variable = new Variable("self")
54 init(modelbuilder
: ModelBuilder, mmodule
: MModule, mpropdef
: nullable MPropDef)
56 self.modelbuilder
= modelbuilder
57 self.mmodule
= mmodule
59 if mpropdef
!= null then
60 self.mpropdef
= mpropdef
61 var mclassdef
= mpropdef
.mclassdef
62 self.mclassdef
= mclassdef
63 self.anchor
= mclassdef
.bound_mtype
65 var mclass
= mclassdef
.mclass
67 var selfvariable
= new Variable("self")
68 self.selfvariable
= selfvariable
69 selfvariable
.declared_type
= mclass
.mclass_type
73 fun anchor_to
(mtype
: MType): MType
76 if anchor
== null then
77 assert not mtype
.need_anchor
80 return mtype
.anchor_to
(mmodule
, anchor
)
83 fun is_subtype
(sub
, sup
: MType): Bool
85 return sub
.is_subtype
(mmodule
, anchor
, sup
)
88 fun resolve_for
(mtype
, subtype
: MType, for_self
: Bool): MType
90 #print "resolve_for {mtype} sub={subtype} forself={for_self} mmodule={mmodule} anchor={anchor}"
91 var res
= mtype
.resolve_for
(subtype
, anchor
, mmodule
, not for_self
)
95 # Check that `sub` is a subtype of `sup`.
96 # If `sub` is not a valud suptype, then display an error on `node` an return null.
97 # If `sub` is a safe subtype of `sup` then return `sub`.
98 # If `sub` is an insafe subtype (ie an imlicit cast is required), then return `sup`.
100 # The point of the return type is to determinate the usable type on an expression:
101 # If the suptype is safe, then the return type is the one on the expression typed by `sub`.
102 # Is the subtype is unsafe, then the return type is the one of an implicit cast on `sup`.
103 fun check_subtype
(node
: ANode, sub
, sup
: MType): nullable MType
105 if self.is_subtype
(sub
, sup
) then return sub
106 if self.is_subtype
(sub
, self.anchor_to
(sup
)) then
107 # FIXME workarround to the current unsafe typing policy. To remove once fixed virtual types exists.
108 #node.debug("Unsafe typing: expected {sup}, got {sub}")
111 self.modelbuilder
.error
(node
, "Type error: expected {sup}, got {sub}")
115 # Visit an expression and do not care about the return value
116 fun visit_stmt
(nexpr
: nullable AExpr)
118 if nexpr
== null then return
119 nexpr
.accept_typing
(self)
122 # Visit an expression and expects that it is not a statement
123 # Return the type of the expression
124 # Display an error and return null if:
125 # * the type cannot be determined or
126 # * `nexpr` is a statement
127 fun visit_expr
(nexpr
: AExpr): nullable MType
129 nexpr
.accept_typing
(self)
130 var mtype
= nexpr
.mtype
131 if mtype
!= null then return mtype
132 if not nexpr
.is_typed
then
133 if not self.modelbuilder
.toolcontext
.error_count
> 0 then # check that there is really an error
134 if self.modelbuilder
.toolcontext
.verbose_level
> 1 then
135 nexpr
.debug
("No return type but no error.")
138 return null # forward error
140 self.error
(nexpr
, "Type error: expected expression.")
144 # Visit an expression and expect its static type is a least a `sup`
145 # Return the type of the expression or null if
146 # * the type cannot be determined or
147 # * `nexpr` is a statement or
148 # * `nexpr` is not a `sup`
149 fun visit_expr_subtype
(nexpr
: AExpr, sup
: nullable MType): nullable MType
151 var sub
= visit_expr
(nexpr
)
152 if sub
== null then return null # Forward error
154 if sup
== null then return null # Forward error
156 var res
= check_subtype
(nexpr
, sub
, sup
)
158 nexpr
.implicit_cast_to
= res
163 # Visit an expression and expect its static type is a `Bool`
164 # Return the type of the expression or null if
165 # * the type cannot be determined or
166 # * `nexpr` is a statement or
167 # * `nexpr` is not a `Bool`
168 fun visit_expr_bool
(nexpr
: AExpr): nullable MType
170 return self.visit_expr_subtype
(nexpr
, self.type_bool
(nexpr
))
174 private fun visit_expr_cast
(node
: ANode, nexpr
: AExpr, ntype
: AType): nullable MType
176 var sub
= visit_expr
(nexpr
)
177 if sub
== null then return null # Forward error
179 var sup
= self.resolve_mtype
(ntype
)
180 if sup
== null then return null # Forward error
183 self.modelbuilder
.warning
(node
, "Warning: Expression is already a {sup}.")
184 else if self.is_subtype
(sub
, sup
) and not sup
.need_anchor
then
185 self.modelbuilder
.warning
(node
, "Warning: Expression is already a {sup} since it is a {sub}.")
190 fun try_get_mproperty_by_name2
(anode
: ANode, mtype
: MType, name
: String): nullable MProperty
192 return self.modelbuilder
.try_get_mproperty_by_name2
(anode
, mmodule
, mtype
, name
)
195 fun resolve_mtype
(node
: AType): nullable MType
197 return self.modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, node
)
200 fun try_get_mclass
(node
: ANode, name
: String): nullable MClass
202 var mclass
= modelbuilder
.try_get_mclass_by_name
(node
, mmodule
, name
)
206 fun get_mclass
(node
: ANode, name
: String): nullable MClass
208 var mclass
= modelbuilder
.try_get_mclass_by_name
(node
, mmodule
, name
)
209 if mclass
== null then
210 self.modelbuilder
.error
(node
, "Type Error: missing primitive class `{name}'.")
215 fun type_bool
(node
: ANode): nullable MType
217 var mclass
= self.get_mclass
(node
, "Bool")
218 if mclass
== null then return null
219 return mclass
.mclass_type
222 fun get_method
(node
: ANode, recvtype
: MType, name
: String, recv_is_self
: Bool): nullable CallSite
224 var unsafe_type
= self.anchor_to
(recvtype
)
226 #debug("recv: {recvtype} (aka {unsafe_type})")
227 if recvtype
isa MNullType then
228 self.error
(node
, "Error: Method '{name}' call on 'null'.")
232 var mproperty
= self.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
233 if mproperty
== null then
234 #self.modelbuilder.error(node, "Type error: property {name} not found in {unsafe_type} (ie {recvtype})")
236 self.modelbuilder
.error
(node
, "Error: Method or variable '{name}' unknown in {recvtype}.")
238 self.modelbuilder
.error
(node
, "Error: Method '{name}' doesn't exists in {recvtype}.")
243 assert mproperty
isa MMethod
244 if mproperty
.visibility
== protected_visibility
and not recv_is_self
and self.mmodule
.visibility_for
(mproperty
.intro_mclassdef
.mmodule
) < intrude_visibility
and not modelbuilder
.toolcontext
.opt_ignore_visibility
.value
then
245 self.modelbuilder
.error
(node
, "Error: Method '{name}' is protected and can only acceded by self.")
249 var propdefs
= mproperty
.lookup_definitions
(self.mmodule
, unsafe_type
)
251 if propdefs
.length
== 0 then
252 self.modelbuilder
.error
(node
, "Type error: no definition found for property {name} in {unsafe_type}")
254 else if propdefs
.length
== 1 then
255 mpropdef
= propdefs
.first
257 self.modelbuilder
.warning
(node
, "Warning: confliting property definitions for property {name} in {unsafe_type}: {propdefs.join(" ")}")
258 mpropdef
= mproperty
.intro
262 var msignature
= mpropdef
.msignature
.as(not null)
263 msignature
= resolve_for
(msignature
, recvtype
, recv_is_self
).as(MSignature)
265 var erasure_cast
= false
266 var rettype
= mpropdef
.msignature
.return_mtype
267 if not recv_is_self
and rettype
!= null then
268 if rettype
isa MNullableType then rettype
= rettype
.mtype
269 if rettype
isa MParameterType then
270 var erased_rettype
= msignature
.return_mtype
271 assert erased_rettype
!= null
272 #node.debug("Erasure cast: Really a {rettype} but unsafely a {erased_rettype}")
277 var callsite
= new CallSite(node
, recvtype
, mmodule
, anchor
, recv_is_self
, mproperty
, mpropdef
, msignature
, erasure_cast
)
281 # Visit the expressions of args and cheik their conformity with the corresponding typi in signature
282 # The point of this method is to handle varargs correctly
283 # Note: The signature must be correctly adapted
284 fun check_signature
(node
: ANode, args
: Array[AExpr], name
: String, msignature
: MSignature): Bool
286 var vararg_rank
= msignature
.vararg_rank
287 if vararg_rank
>= 0 then
288 if args
.length
< msignature
.arity
then
289 #self.modelbuilder.error(node, "Error: Incorrect number of parameters. Got {args.length}, expected at least {msignature.arity}. Signature is {msignature}")
290 self.modelbuilder
.error
(node
, "Error: arity mismatch; prototype is '{name}{msignature}'")
293 else if args
.length
!= msignature
.arity
then
294 self.modelbuilder
.error
(node
, "Error: Incorrect number of parameters. Got {args.length}, expected {msignature.arity}. Signature is {msignature}")
298 #debug("CALL {unsafe_type}.{msignature}")
300 var vararg_decl
= args
.length
- msignature
.arity
301 for i
in [0..msignature
.arity
[ do
303 if i
== vararg_rank
then continue # skip the vararg
304 if i
> vararg_rank
then
307 var paramtype
= msignature
.mparameters
[i
].mtype
308 self.visit_expr_subtype
(args
[j
], paramtype
)
310 if vararg_rank
>= 0 then
311 var varargs
= new Array[AExpr]
312 var paramtype
= msignature
.mparameters
[vararg_rank
].mtype
313 for j
in [vararg_rank
..vararg_rank
+vararg_decl
] do
315 self.visit_expr_subtype
(args
[j
], paramtype
)
321 fun error
(node
: ANode, message
: String)
323 self.modelbuilder
.toolcontext
.error
(node
.hot_location
, message
)
326 fun get_variable
(node
: AExpr, variable
: Variable): nullable MType
328 var flow
= node
.after_flow_context
330 self.error
(node
, "No context!")
334 if flow
.vars
.has_key
(variable
) then
335 return flow
.vars
[variable
]
337 #node.debug("*** START Collected for {variable}")
338 var mtypes
= flow
.collect_types
(variable
)
339 #node.debug("**** END Collected for {variable}")
340 if mtypes
== null or mtypes
.length
== 0 then
341 return variable
.declared_type
342 else if mtypes
.length
== 1 then
345 var res
= merge_types
(node
,mtypes
)
346 if res
== null then res
= variable
.declared_type
352 fun set_variable
(node
: AExpr, variable
: Variable, mtype
: nullable MType)
354 var flow
= node
.after_flow_context
357 flow
.set_var
(variable
, mtype
)
360 fun merge_types
(node
: ANode, col
: Array[nullable MType]): nullable MType
362 if col
.length
== 1 then return col
.first
363 var res
= new Array[nullable MType]
365 if t1
== null then continue # return null
368 if t2
== null then continue # return null
369 if t2
isa MNullableType or t2
isa MNullType then
372 if not is_subtype
(t2
, t1
) then found
= false
375 #print "merge {col.join(" ")} -> {t1}"
379 #self.modelbuilder.warning(node, "Type Error: {col.length} conflicting types: <{col.join(", ")}>")
384 # A specific method call site with its associated informations.
386 # The assiciated node for location
389 # The static type of the receiver (possibly unresolved)
392 # The module where the callsite is present
395 # The anchor to use with `recv` or `msignature`
396 var anchor
: nullable MClassType
398 # Is the receiver self?
399 # If "for_self", virtual types of the signature are keeped
400 # If "not_for_self", virtual type are erased
401 var recv_is_self
: Bool
403 # The designated method
404 var mproperty
: MMethod
406 # The statically designated method definition
407 # The most specif one, it is.
408 var mpropdef
: MMethodDef
410 # The resolved signature for the receiver
411 var msignature
: MSignature
413 # Is a implicit cast required on erasure typing policy?
414 var erasure_cast
: Bool
416 private fun check_signature
(v
: TypeVisitor, args
: Array[AExpr]): Bool
418 return v
.check_signature
(self.node
, args
, self.mproperty
.name
, self.msignature
)
423 # The declared type of the variable
424 var declared_type
: nullable MType
427 redef class FlowContext
428 # Store changes of types because of type evolution
429 private var vars
: HashMap[Variable, nullable MType] = new HashMap[Variable, nullable MType]
430 private var cache
: HashMap[Variable, nullable Array[nullable MType]] = new HashMap[Variable, nullable Array[nullable MType]]
432 # Adapt the variable to a static type
433 # Warning1: do not modify vars directly.
434 # Warning2: sub-flow may have cached a unadapted variabial
435 private fun set_var
(variable
: Variable, mtype
: nullable MType)
437 self.vars
[variable
] = mtype
438 self.cache
.keys
.remove
(variable
)
441 private fun collect_types
(variable
: Variable): nullable Array[nullable MType]
443 if cache
.has_key
(variable
) then
444 return cache
[variable
]
446 var res
: nullable Array[nullable MType] = null
447 if vars
.has_key
(variable
) then
448 var mtype
= vars
[variable
]
450 else if self.previous
.is_empty
then
452 res
= [variable
.declared_type
]
454 for flow
in self.previous
do
455 if flow
.is_unreachable
then continue
456 var r2
= flow
.collect_types
(variable
)
457 if r2
== null then continue
462 if not res
.has
(t
) then res
.add
(t
)
467 cache
[variable
] = res
473 # The entry point of the whole typing analysis
474 fun do_typing
(modelbuilder
: ModelBuilder)
478 # The variable associated to the reciever (if any)
479 var selfvariable
: nullable Variable
482 redef class AMethPropdef
483 redef fun do_typing
(modelbuilder
: ModelBuilder)
485 var nblock
= self.n_block
486 if nblock
== null then return
488 var mpropdef
= self.mpropdef
.as(not null)
489 var v
= new TypeVisitor(modelbuilder
, mpropdef
.mclassdef
.mmodule
, mpropdef
)
490 self.selfvariable
= v
.selfvariable
492 var mmethoddef
= self.mpropdef
.as(not null)
493 for i
in [0..mmethoddef
.msignature
.arity
[ do
494 var mtype
= mmethoddef
.msignature
.mparameters
[i
].mtype
495 if mmethoddef
.msignature
.vararg_rank
== i
then
496 var arrayclass
= v
.get_mclass
(self.n_signature
.n_params
[i
], "Array")
497 if arrayclass
== null then return # Skip error
498 mtype
= arrayclass
.get_mtype
([mtype
])
500 var variable
= self.n_signature
.n_params
[i
].variable
501 assert variable
!= null
502 variable
.declared_type
= mtype
506 if not nblock
.after_flow_context
.is_unreachable
and mmethoddef
.msignature
.return_mtype
!= null then
507 # We reach the end of the function without having a return, it is bad
508 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
513 redef class AAttrPropdef
514 redef fun do_typing
(modelbuilder
: ModelBuilder)
516 var mpropdef
= self.mpropdef
.as(not null)
517 var v
= new TypeVisitor(modelbuilder
, mpropdef
.mclassdef
.mmodule
, mpropdef
)
518 self.selfvariable
= v
.selfvariable
520 var nexpr
= self.n_expr
521 if nexpr
!= null then
522 var mtype
= self.mpropdef
.static_mtype
523 v
.visit_expr_subtype
(nexpr
, mtype
)
531 # The static type of the expression.
532 # null if self is a statement or in case of error
533 var mtype
: nullable MType = null
535 # Is the statement correctly typed?
536 # Used to distinguish errors and statements when `mtype == null`
537 var is_typed
: Bool = false
539 # If required, the following implicit cast `.as(XXX)`
540 # Such a cast may by required after evaluating the expression when
541 # a unsafe operation is detected (silently accepted by the Nit language).
542 # The attribute is computed by `check_subtype`
543 var implicit_cast_to
: nullable MType = null
545 # Return the variable read (if any)
546 # Used to perform adaptive typing
547 fun its_variable
: nullable Variable do return null
549 private fun accept_typing
(v
: TypeVisitor)
551 v
.error
(self, "no implemented accept_typing for {self.class_name}")
555 redef class ABlockExpr
556 redef fun accept_typing
(v
)
558 for e
in self.n_expr
do v
.visit_stmt
(e
)
562 # The type of a blockexpr is the one of the last expression (or null if empty)
565 if self.n_expr
.is_empty
then return null
566 return self.n_expr
.last
.mtype
570 redef class AVardeclExpr
571 redef fun accept_typing
(v
)
573 var variable
= self.variable
574 if variable
== null then return # Skip error
576 var ntype
= self.n_type
577 var mtype
: nullable MType
578 if ntype
== null then
581 mtype
= v
.resolve_mtype
(ntype
)
582 if mtype
== null then return # Skip error
585 var nexpr
= self.n_expr
586 if nexpr
!= null then
587 if mtype
!= null then
588 v
.visit_expr_subtype
(nexpr
, mtype
)
590 mtype
= v
.visit_expr
(nexpr
)
591 if mtype
== null then return # Skip error
596 if mtype
== null or mtype
isa MNullType then
597 decltype
= v
.get_mclass
(self, "Object").mclass_type
.as_nullable
598 if mtype
== null then mtype
= decltype
601 variable
.declared_type
= decltype
602 v
.set_variable
(self, variable
, mtype
)
604 #debug("var {variable}: {mtype}")
611 redef fun its_variable
do return self.variable
612 redef fun accept_typing
(v
)
614 var variable
= self.variable
615 if variable
== null then return # Skip error
617 var mtype
= v
.get_variable
(self, variable
)
618 if mtype
!= null then
619 #debug("{variable} is {mtype}")
621 #debug("{variable} is untyped")
628 redef class AVarAssignExpr
629 redef fun accept_typing
(v
)
631 var variable
= self.variable
632 assert variable
!= null
634 var mtype
= v
.visit_expr_subtype
(n_value
, variable
.declared_type
)
636 v
.set_variable
(self, variable
, mtype
)
642 redef class AReassignFormExpr
643 # The method designed by the reassign operator.
644 var reassign_callsite
: nullable CallSite
646 var read_type
: nullable MType = null
648 # Determine the `reassign_property`
649 # `readtype` is the type of the reading of the left value.
650 # `writetype` is the type of the writing of the left value.
651 # (Because of `ACallReassignExpr`, both can be different.
652 # Return the static type of the value to store.
653 private fun resolve_reassignment
(v
: TypeVisitor, readtype
, writetype
: MType): nullable MType
655 var reassign_name
: String
656 if self.n_assign_op
isa APlusAssignOp then
658 else if self.n_assign_op
isa AMinusAssignOp then
664 self.read_type
= readtype
666 if readtype
isa MNullType then
667 v
.error
(self, "Error: Method '{reassign_name}' call on 'null'.")
671 var callsite
= v
.get_method
(self, readtype
, reassign_name
, false)
672 if callsite
== null then return null # Skip error
673 self.reassign_callsite
= callsite
675 var msignature
= callsite
.msignature
676 var rettype
= msignature
.return_mtype
677 assert msignature
.arity
== 1 and rettype
!= null
679 var value_type
= v
.visit_expr_subtype
(self.n_value
, msignature
.mparameters
.first
.mtype
)
680 if value_type
== null then return null # Skip error
682 v
.check_subtype
(self, rettype
, writetype
)
687 redef class AVarReassignExpr
688 redef fun accept_typing
(v
)
690 var variable
= self.variable
691 assert variable
!= null
693 var readtype
= v
.get_variable
(self, variable
)
694 if readtype
== null then return
698 var writetype
= variable
.declared_type
699 if writetype
== null then return
701 var rettype
= self.resolve_reassignment
(v
, readtype
, writetype
)
703 v
.set_variable
(self, variable
, rettype
)
710 redef class AContinueExpr
711 redef fun accept_typing
(v
)
713 var nexpr
= self.n_expr
714 if nexpr
!= null then
715 var mtype
= v
.visit_expr
(nexpr
)
721 redef class ABreakExpr
722 redef fun accept_typing
(v
)
724 var nexpr
= self.n_expr
725 if nexpr
!= null then
726 var mtype
= v
.visit_expr
(nexpr
)
732 redef class AReturnExpr
733 redef fun accept_typing
(v
)
735 var nexpr
= self.n_expr
736 var ret_type
= v
.mpropdef
.as(MMethodDef).msignature
.return_mtype
737 if nexpr
!= null then
738 if ret_type
!= null then
739 var mtype
= v
.visit_expr_subtype
(nexpr
, ret_type
)
741 var mtype
= v
.visit_expr
(nexpr
)
742 v
.error
(self, "Error: Return with value in a procedure.")
744 else if ret_type
!= null then
745 v
.error
(self, "Error: Return without value in a function.")
751 redef class AAbortExpr
752 redef fun accept_typing
(v
)
759 redef fun accept_typing
(v
)
761 v
.visit_expr_bool
(n_expr
)
769 redef class AIfexprExpr
770 redef fun accept_typing
(v
)
772 v
.visit_expr_bool
(n_expr
)
774 var t1
= v
.visit_expr
(n_then
)
775 var t2
= v
.visit_expr
(n_else
)
777 if t1
== null or t2
== null then
781 var t
= v
.merge_types
(self, [t1
, t2
])
783 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
790 redef fun accept_typing
(v
)
792 v
.visit_stmt
(n_block
)
797 redef class AWhileExpr
798 redef fun accept_typing
(v
)
800 v
.visit_expr_bool
(n_expr
)
802 v
.visit_stmt
(n_block
)
807 redef class ALoopExpr
808 redef fun accept_typing
(v
)
810 v
.visit_stmt
(n_block
)
816 var coltype
: nullable MClassType
818 var method_iterator
: nullable CallSite
819 var method_is_ok
: nullable CallSite
820 var method_item
: nullable CallSite
821 var method_next
: nullable CallSite
822 var method_key
: nullable CallSite
824 private fun do_type_iterator
(v
: TypeVisitor, mtype
: MType)
826 if mtype
isa MNullType then
827 v
.error
(self, "Type error: 'for' cannot iterate over 'null'")
832 var objcla
= v
.get_mclass
(self, "Object")
833 if objcla
== null then return
835 # check iterator method
836 var itdef
= v
.get_method
(self, mtype
, "iterator", true)
837 if itdef
== null then
838 v
.error
(self, "Type Error: 'for' expects a type providing 'iterator' method, got '{mtype}'.")
841 self.method_iterator
= itdef
843 # check that iterator return something
844 var ittype
= itdef
.msignature
.return_mtype
845 if ittype
== null then
846 v
.error
(self, "Type Error: 'for' expects method 'iterator' to return an 'Iterator' or 'MapIterator' type'.")
851 var colit_cla
= v
.try_get_mclass
(self, "Iterator")
852 var mapit_cla
= v
.try_get_mclass
(self, "MapIterator")
856 if colit_cla
!= null and v
.is_subtype
(ittype
, colit_cla
.get_mtype
([objcla
.mclass_type
.as_nullable
])) then
858 var coltype
= ittype
.supertype_to
(v
.mmodule
, v
.anchor
, colit_cla
)
859 var variables
= self.variables
860 if variables
.length
!= 1 then
861 v
.error
(self, "Type Error: 'for' expects only one variable when using 'Iterator'.")
863 variables
.first
.declared_type
= coltype
.arguments
.first
868 if mapit_cla
!= null and v
.is_subtype
(ittype
, mapit_cla
.get_mtype
([objcla
.mclass_type
, objcla
.mclass_type
.as_nullable
])) then
870 var coltype
= ittype
.supertype_to
(v
.mmodule
, v
.anchor
, mapit_cla
)
871 var variables
= self.variables
872 if variables
.length
!= 2 then
873 v
.error
(self, "Type Error: 'for' expects two variables when using 'MapIterator'.")
875 variables
[0].declared_type
= coltype
.arguments
[0]
876 variables
[1].declared_type
= coltype
.arguments
[1]
881 if not is_col
and not is_map
then
882 v
.error
(self, "Type Error: 'for' expects method 'iterator' to return an 'Iterator' or 'MapIterator' type'.")
886 # anchor formal and virtual types
887 if mtype
.need_anchor
then mtype
= v
.anchor_to
(mtype
)
889 if mtype
isa MNullableType then mtype
= mtype
.mtype
890 self.coltype
= mtype
.as(MClassType)
892 # get methods is_ok, next, item
893 var ikdef
= v
.get_method
(self, ittype
, "is_ok", false)
894 if ikdef
== null then
895 v
.error
(self, "Type Error: 'for' expects a method 'is_ok' in 'Iterator' type {ittype}.")
898 self.method_is_ok
= ikdef
900 var itemdef
= v
.get_method
(self, ittype
, "item", false)
901 if itemdef
== null then
902 v
.error
(self, "Type Error: 'for' expects a method 'item' in 'Iterator' type {ittype}.")
905 self.method_item
= itemdef
907 var nextdef
= v
.get_method
(self, ittype
, "next", false)
908 if nextdef
== null then
909 v
.error
(self, "Type Error: 'for' expects a method 'next' in 'Iterator' type {ittype}.")
912 self.method_next
= nextdef
915 var keydef
= v
.get_method
(self, ittype
, "key", false)
916 if keydef
== null then
917 v
.error
(self, "Type Error: 'for' expects a method 'key' in 'Iterator' type {ittype}.")
920 self.method_key
= keydef
924 redef fun accept_typing
(v
)
926 var mtype
= v
.visit_expr
(n_expr
)
927 if mtype
== null then return
929 self.do_type_iterator
(v
, mtype
)
931 v
.visit_stmt
(n_block
)
936 redef class AAssertExpr
937 redef fun accept_typing
(v
)
939 v
.visit_expr_bool
(n_expr
)
947 redef fun accept_typing
(v
)
949 v
.visit_expr_bool
(n_expr
)
950 v
.visit_expr_bool
(n_expr2
)
951 self.mtype
= v
.type_bool
(self)
955 redef class AImpliesExpr
956 redef fun accept_typing
(v
)
958 v
.visit_expr_bool
(n_expr
)
959 v
.visit_expr_bool
(n_expr2
)
960 self.mtype
= v
.type_bool
(self)
965 redef fun accept_typing
(v
)
967 v
.visit_expr_bool
(n_expr
)
968 v
.visit_expr_bool
(n_expr2
)
969 self.mtype
= v
.type_bool
(self)
975 redef fun accept_typing
(v
)
977 v
.visit_expr_bool
(n_expr
)
978 self.mtype
= v
.type_bool
(self)
982 redef class AOrElseExpr
983 redef fun accept_typing
(v
)
985 var t1
= v
.visit_expr
(n_expr
)
986 var t2
= v
.visit_expr
(n_expr2
)
988 if t1
== null or t2
== null then
992 if t1
isa MNullableType then
996 var t
= v
.merge_types
(self, [t1
, t2
])
998 t
= v
.mmodule
.object_type
999 if t2
isa MNullableType then
1002 #v.error(self, "Type Error: ambiguous type {t1} vs {t2}")
1008 redef class ATrueExpr
1009 redef fun accept_typing
(v
)
1011 self.mtype
= v
.type_bool
(self)
1015 redef class AFalseExpr
1016 redef fun accept_typing
(v
)
1018 self.mtype
= v
.type_bool
(self)
1022 redef class AIntExpr
1023 redef fun accept_typing
(v
)
1025 var mclass
= v
.get_mclass
(self, "Int")
1026 if mclass
== null then return # Forward error
1027 self.mtype
= mclass
.mclass_type
1031 redef class AFloatExpr
1032 redef fun accept_typing
(v
)
1034 var mclass
= v
.get_mclass
(self, "Float")
1035 if mclass
== null then return # Forward error
1036 self.mtype
= mclass
.mclass_type
1040 redef class ACharExpr
1041 redef fun accept_typing
(v
)
1043 var mclass
= v
.get_mclass
(self, "Char")
1044 if mclass
== null then return # Forward error
1045 self.mtype
= mclass
.mclass_type
1049 redef class AStringFormExpr
1050 redef fun accept_typing
(v
)
1052 var mclass
= v
.get_mclass
(self, "String")
1053 if mclass
== null then return # Forward error
1054 self.mtype
= mclass
.mclass_type
1058 redef class ASuperstringExpr
1059 redef fun accept_typing
(v
)
1061 var mclass
= v
.get_mclass
(self, "String")
1062 if mclass
== null then return # Forward error
1063 self.mtype
= mclass
.mclass_type
1064 for nexpr
in self.n_exprs
do
1065 var t
= v
.visit_expr
(nexpr
)
1070 redef class AArrayExpr
1071 var with_capacity_callsite
: nullable CallSite
1072 var push_callsite
: nullable CallSite
1074 redef fun accept_typing
(v
)
1076 var mtypes
= new Array[nullable MType]
1077 for e
in self.n_exprs
.n_exprs
do
1078 var t
= v
.visit_expr
(e
)
1084 var mtype
= v
.merge_types
(self, mtypes
)
1085 if mtype
== null then
1086 v
.error
(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
1089 var mclass
= v
.get_mclass
(self, "Array")
1090 if mclass
== null then return # Forward error
1091 var array_mtype
= mclass
.get_mtype
([mtype
])
1093 with_capacity_callsite
= v
.get_method
(self, array_mtype
, "with_capacity", false)
1094 push_callsite
= v
.get_method
(self, array_mtype
, "push", false)
1096 self.mtype
= array_mtype
1100 redef class ARangeExpr
1101 var init_callsite
: nullable CallSite
1103 redef fun accept_typing
(v
)
1105 var discrete_class
= v
.get_mclass
(self, "Discrete")
1106 if discrete_class
== null then return # Forward error
1107 var discrete_type
= discrete_class
.intro
.bound_mtype
1108 var t1
= v
.visit_expr_subtype
(self.n_expr
, discrete_type
)
1109 var t2
= v
.visit_expr_subtype
(self.n_expr2
, discrete_type
)
1110 if t1
== null or t2
== null then return
1111 var mclass
= v
.get_mclass
(self, "Range")
1112 if mclass
== null then return # Forward error
1114 if v
.is_subtype
(t1
, t2
) then
1115 mtype
= mclass
.get_mtype
([t2
])
1116 else if v
.is_subtype
(t2
, t1
) then
1117 mtype
= mclass
.get_mtype
([t1
])
1119 v
.error
(self, "Type Error: Cannot create range: {t1} vs {t2}")
1125 # get the constructor
1127 if self isa ACrangeExpr then
1128 callsite
= v
.get_method
(self, mtype
, "init", false)
1129 else if self isa AOrangeExpr then
1130 callsite
= v
.get_method
(self, mtype
, "without_last", false)
1134 init_callsite
= callsite
1138 redef class ANullExpr
1139 redef fun accept_typing
(v
)
1141 self.mtype
= v
.mmodule
.model
.null_type
1145 redef class AIsaExpr
1146 # The static type to cast to.
1147 # (different from the static type of the expression that is `Bool`).
1148 var cast_type
: nullable MType
1149 redef fun accept_typing
(v
)
1151 var mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
1152 self.cast_type
= mtype
1154 var variable
= self.n_expr
.its_variable
1155 if variable
!= null then
1156 var orig
= self.n_expr
.mtype
1157 var from
= if orig
!= null then orig
.to_s
else "invalid"
1158 var to
= if mtype
!= null then mtype
.to_s
else "invalid"
1159 #debug("adapt {variable}: {from} -> {to}")
1160 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1163 self.mtype
= v
.type_bool
(self)
1167 redef class AAsCastExpr
1168 redef fun accept_typing
(v
)
1170 self.mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
1174 redef class AAsNotnullExpr
1175 redef fun accept_typing
(v
)
1177 var mtype
= v
.visit_expr
(self.n_expr
)
1178 if mtype
isa MNullType then
1179 v
.error
(self, "Type error: as(not null) on null")
1182 if mtype
isa MNullableType then
1183 self.mtype
= mtype
.mtype
1186 # TODO: warn on useless as not null
1191 redef class AProxyExpr
1192 redef fun accept_typing
(v
)
1194 self.mtype
= v
.visit_expr
(self.n_expr
)
1198 redef class ASelfExpr
1199 redef var its_variable
: nullable Variable
1200 redef fun accept_typing
(v
)
1202 var variable
= v
.selfvariable
1203 self.its_variable
= variable
1204 self.mtype
= v
.get_variable
(self, variable
)
1208 ## MESSAGE SENDING AND PROPERTY
1210 redef class ASendExpr
1211 # The property invoked by the send.
1212 var callsite
: nullable CallSite
1214 redef fun accept_typing
(v
)
1216 var recvtype
= v
.visit_expr
(self.n_expr
)
1217 var name
= self.property_name
1219 if recvtype
== null then return # Forward error
1220 if recvtype
isa MNullType then
1221 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1225 var callsite
= v
.get_method
(self, recvtype
, name
, self.n_expr
isa ASelfExpr)
1226 if callsite
== null then return
1227 self.callsite
= callsite
1228 var msignature
= callsite
.msignature
1230 var args
= compute_raw_arguments
1232 callsite
.check_signature
(v
, args
)
1234 if callsite
.mproperty
.is_init
then
1235 var vmpropdef
= v
.mpropdef
1236 if not (vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_init
) then
1237 v
.error
(self, "Can call a init only in another init")
1241 var ret
= msignature
.return_mtype
1245 self.is_typed
= true
1249 # The name of the property
1250 # Each subclass simply provide the correct name.
1251 private fun property_name
: String is abstract
1253 # An array of all arguments (excluding self)
1254 fun raw_arguments
: Array[AExpr] do return compute_raw_arguments
1256 private fun compute_raw_arguments
: Array[AExpr] is abstract
1259 redef class ABinopExpr
1260 redef fun compute_raw_arguments
do return [n_expr2
]
1263 redef fun property_name
do return "=="
1264 redef fun accept_typing
(v
)
1268 var variable
= self.n_expr
.its_variable
1269 if variable
== null then return
1270 var mtype
= self.n_expr2
.mtype
1271 if not mtype
isa MNullType then return
1272 var vartype
= v
.get_variable
(self, variable
)
1273 if not vartype
isa MNullableType then return
1274 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1275 self.after_flow_context
.when_false
.set_var
(variable
, vartype
.mtype
)
1276 #debug("adapt {variable}:{vartype} ; true->{mtype} false->{vartype.mtype}")
1280 redef fun property_name
do return "!="
1281 redef fun accept_typing
(v
)
1285 var variable
= self.n_expr
.its_variable
1286 if variable
== null then return
1287 var mtype
= self.n_expr2
.mtype
1288 if not mtype
isa MNullType then return
1289 var vartype
= v
.get_variable
(self, variable
)
1290 if not vartype
isa MNullableType then return
1291 self.after_flow_context
.when_false
.set_var
(variable
, mtype
)
1292 self.after_flow_context
.when_true
.set_var
(variable
, vartype
.mtype
)
1293 #debug("adapt {variable}:{vartype} ; true->{vartype.mtype} false->{mtype}")
1297 redef fun property_name
do return "<"
1300 redef fun property_name
do return "<="
1303 redef fun property_name
do return "<<"
1306 redef fun property_name
do return ">"
1309 redef fun property_name
do return ">="
1312 redef fun property_name
do return ">>"
1314 redef class APlusExpr
1315 redef fun property_name
do return "+"
1317 redef class AMinusExpr
1318 redef fun property_name
do return "-"
1320 redef class AStarshipExpr
1321 redef fun property_name
do return "<=>"
1323 redef class AStarExpr
1324 redef fun property_name
do return "*"
1326 redef class ASlashExpr
1327 redef fun property_name
do return "/"
1329 redef class APercentExpr
1330 redef fun property_name
do return "%"
1333 redef class AUminusExpr
1334 redef fun property_name
do return "unary -"
1335 redef fun compute_raw_arguments
do return new Array[AExpr]
1339 redef class ACallExpr
1340 redef fun property_name
do return n_id
.text
1341 redef fun compute_raw_arguments
do return n_args
.to_a
1344 redef class ACallAssignExpr
1345 redef fun property_name
do return n_id
.text
+ "="
1346 redef fun compute_raw_arguments
1348 var res
= n_args
.to_a
1354 redef class ABraExpr
1355 redef fun property_name
do return "[]"
1356 redef fun compute_raw_arguments
do return n_args
.to_a
1359 redef class ABraAssignExpr
1360 redef fun property_name
do return "[]="
1361 redef fun compute_raw_arguments
1363 var res
= n_args
.to_a
1369 redef class ASendReassignFormExpr
1370 # The property invoked for the writing
1371 var write_callsite
: nullable CallSite
1373 redef fun accept_typing
(v
)
1375 var recvtype
= v
.visit_expr
(self.n_expr
)
1376 var name
= self.property_name
1378 if recvtype
== null then return # Forward error
1379 if recvtype
isa MNullType then
1380 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1384 var for_self
= self.n_expr
isa ASelfExpr
1385 var callsite
= v
.get_method
(self, recvtype
, name
, for_self
)
1387 if callsite
== null then return
1388 self.callsite
= callsite
1390 var args
= compute_raw_arguments
1392 callsite
.check_signature
(v
, args
)
1394 var readtype
= callsite
.msignature
.return_mtype
1395 if readtype
== null then
1396 v
.error
(self, "Error: {name} is not a function")
1400 var wcallsite
= v
.get_method
(self, recvtype
, name
+ "=", self.n_expr
isa ASelfExpr)
1401 if wcallsite
== null then return
1402 self.write_callsite
= wcallsite
1404 var wtype
= self.resolve_reassignment
(v
, readtype
, wcallsite
.msignature
.mparameters
.last
.mtype
)
1405 if wtype
== null then return
1407 args
= args
.to_a
# duplicate so raw_arguments keeps only the getter args
1408 args
.add
(self.n_value
)
1409 wcallsite
.check_signature
(v
, args
)
1411 self.is_typed
= true
1415 redef class ACallReassignExpr
1416 redef fun property_name
do return n_id
.text
1417 redef fun compute_raw_arguments
do return n_args
.to_a
1420 redef class ABraReassignExpr
1421 redef fun property_name
do return "[]"
1422 redef fun compute_raw_arguments
do return n_args
.to_a
1425 redef class AInitExpr
1426 redef fun property_name
do return "init"
1427 redef fun compute_raw_arguments
do return n_args
.to_a
1431 fun to_a
: Array[AExpr] do return self.n_exprs
.to_a
1436 redef class ASuperExpr
1437 # The method to call if the super is in fact a 'super init call'
1438 # Note: if the super is a normal call-next-method, then this attribute is null
1439 var callsite
: nullable CallSite
1441 # The method to call is the super is a standard `call-next-method` super-call
1442 # Note: if the super is a special super-init-call, then this attribute is null
1443 var mpropdef
: nullable MMethodDef
1445 redef fun accept_typing
(v
)
1447 var recvtype
= v
.anchor
1448 assert recvtype
!= null
1449 var mproperty
= v
.mpropdef
.mproperty
1450 if not mproperty
isa MMethod then
1451 v
.error
(self, "Error: super only usable in a method")
1454 var superprops
= mproperty
.lookup_super_definitions
(v
.mmodule
, recvtype
)
1455 if superprops
.length
== 0 then
1456 if mproperty
.is_init
and v
.mpropdef
.is_intro
then
1457 process_superinit
(v
)
1460 v
.error
(self, "Error: No super method to call for {mproperty}.")
1463 # FIXME: covariance of return type in linear extension?
1464 var superprop
= superprops
.first
1466 var msignature
= superprop
.msignature
.as(not null)
1467 msignature
= v
.resolve_for
(msignature
, recvtype
, true).as(MSignature)
1468 var args
= self.n_args
.to_a
1469 if args
.length
> 0 then
1470 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1472 self.mtype
= msignature
.return_mtype
1473 self.is_typed
= true
1474 v
.mpropdef
.has_supercall
= true
1475 mpropdef
= v
.mpropdef
.as(MMethodDef)
1478 private fun process_superinit
(v
: TypeVisitor)
1480 var recvtype
= v
.anchor
1481 assert recvtype
!= null
1482 var mpropdef
= v
.mpropdef
1483 assert mpropdef
isa MMethodDef
1484 var mproperty
= mpropdef
.mproperty
1485 var superprop
: nullable MMethodDef = null
1486 for msupertype
in mpropdef
.mclassdef
.supertypes
do
1487 msupertype
= msupertype
.anchor_to
(v
.mmodule
, recvtype
)
1488 var errcount
= v
.modelbuilder
.toolcontext
.error_count
1489 var candidate
= v
.try_get_mproperty_by_name2
(self, msupertype
, mproperty
.name
).as(nullable MMethod)
1490 if candidate
== null then
1491 if v
.modelbuilder
.toolcontext
.error_count
> errcount
then return # Forard error
1492 continue # Try next super-class
1494 if superprop
!= null and superprop
.mproperty
!= candidate
then
1495 v
.error
(self, "Error: conflicting super constructor to call for {mproperty}: {candidate.full_name}, {superprop.mproperty.full_name}")
1498 var candidatedefs
= candidate
.lookup_definitions
(v
.mmodule
, recvtype
)
1499 if superprop
!= null then
1500 if superprop
== candidatedefs
.first
then continue
1501 candidatedefs
.add
(superprop
)
1503 if candidatedefs
.length
> 1 then
1504 v
.error
(self, "Error: confliting property definitions for property {mproperty} in {recvtype}: {candidatedefs.join(", ")}")
1507 superprop
= candidatedefs
.first
1509 if superprop
== null then
1510 v
.error
(self, "Error: No super method to call for {mproperty}.")
1514 var msignature
= superprop
.msignature
.as(not null)
1515 msignature
= v
.resolve_for
(msignature
, recvtype
, true).as(MSignature)
1517 var callsite
= new CallSite(self, recvtype
, v
.mmodule
, v
.anchor
, true, superprop
.mproperty
, superprop
, msignature
, false)
1518 self.callsite
= callsite
1520 var args
= self.n_args
.to_a
1521 if args
.length
> 0 then
1522 callsite
.check_signature
(v
, args
)
1524 # Check there is at least enough parameters
1525 if mpropdef
.msignature
.arity
< msignature
.arity
then
1526 v
.error
(self, "Error: Not enough implicit arguments to pass. Got {mpropdef.msignature.arity}, expected at least {msignature.arity}. Signature is {msignature}")
1529 # Check that each needed parameter is conform
1531 for sp
in msignature
.mparameters
do
1532 var p
= mpropdef
.msignature
.mparameters
[i
]
1533 if not v
.is_subtype
(p
.mtype
, sp
.mtype
) then
1534 v
.error
(self, "Type error: expected argument #{i} of type {sp.mtype}, got implicit argument {p.name} of type {p.mtype}. Signature is {msignature}")
1541 self.is_typed
= true
1547 redef class ANewExpr
1548 # The constructor invoked by the new.
1549 var callsite
: nullable CallSite
1551 redef fun accept_typing
(v
)
1553 var recvtype
= v
.resolve_mtype
(self.n_type
)
1554 if recvtype
== null then return
1555 self.mtype
= recvtype
1557 if not recvtype
isa MClassType then
1558 if recvtype
isa MNullableType then
1559 v
.error
(self, "Type error: cannot instantiate the nullable type {recvtype}.")
1562 v
.error
(self, "Type error: cannot instantiate the formal type {recvtype}.")
1566 if recvtype
.mclass
.kind
== abstract_kind
then
1567 v
.error
(self, "Cannot instantiate abstract class {recvtype}.")
1569 else if recvtype
.mclass
.kind
== interface_kind
then
1570 v
.error
(self, "Cannot instantiate interface {recvtype}.")
1582 var callsite
= v
.get_method
(self, recvtype
, name
, false)
1583 if callsite
== null then return
1585 self.callsite
= callsite
1587 if not callsite
.mproperty
.is_init_for
(recvtype
.mclass
) then
1588 v
.error
(self, "Error: {name} is not a constructor.")
1592 var args
= n_args
.to_a
1593 callsite
.check_signature
(v
, args
)
1599 redef class AAttrFormExpr
1600 # The attribute acceded.
1601 var mproperty
: nullable MAttribute
1603 # The static type of the attribute.
1604 var attr_type
: nullable MType
1606 # Resolve the attribute acceded.
1607 private fun resolve_property
(v
: TypeVisitor)
1609 var recvtype
= v
.visit_expr
(self.n_expr
)
1610 if recvtype
== null then return # Skip error
1611 var name
= self.n_id
.text
1612 if recvtype
isa MNullType then
1613 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1617 var unsafe_type
= v
.anchor_to
(recvtype
)
1618 var mproperty
= v
.try_get_mproperty_by_name2
(self, unsafe_type
, name
)
1619 if mproperty
== null then
1620 v
.modelbuilder
.error
(self, "Error: Attribute {name} doesn't exists in {recvtype}.")
1623 assert mproperty
isa MAttribute
1624 self.mproperty
= mproperty
1626 var mpropdefs
= mproperty
.lookup_definitions
(v
.mmodule
, unsafe_type
)
1627 assert mpropdefs
.length
== 1
1628 var mpropdef
= mpropdefs
.first
1629 var attr_type
= mpropdef
.static_mtype
.as(not null)
1630 attr_type
= v
.resolve_for
(attr_type
, recvtype
, self.n_expr
isa ASelfExpr)
1631 self.attr_type
= attr_type
1635 redef class AAttrExpr
1636 redef fun accept_typing
(v
)
1638 self.resolve_property
(v
)
1639 self.mtype
= self.attr_type
1644 redef class AAttrAssignExpr
1645 redef fun accept_typing
(v
)
1647 self.resolve_property
(v
)
1648 var mtype
= self.attr_type
1650 v
.visit_expr_subtype
(self.n_value
, mtype
)
1651 self.is_typed
= true
1655 redef class AAttrReassignExpr
1656 redef fun accept_typing
(v
)
1658 self.resolve_property
(v
)
1659 var mtype
= self.attr_type
1660 if mtype
== null then return # Skip error
1662 self.resolve_reassignment
(v
, mtype
, mtype
)
1664 self.is_typed
= true
1668 redef class AIssetAttrExpr
1669 redef fun accept_typing
(v
)
1671 self.resolve_property
(v
)
1672 var mtype
= self.attr_type
1673 if mtype
== null then return # Skip error
1675 var recvtype
= self.n_expr
.mtype
.as(not null)
1676 var bound
= v
.resolve_for
(mtype
, recvtype
, false)
1677 if bound
isa MNullableType then
1678 v
.error
(self, "Error: isset on a nullable attribute.")
1680 self.mtype
= v
.type_bool
(self)
1686 redef class ADebugTypeExpr
1687 redef fun accept_typing
(v
)
1689 var expr
= v
.visit_expr
(self.n_expr
)
1690 if expr
== null then return
1691 var unsafe
= v
.anchor_to
(expr
)
1692 var ntype
= self.n_type
1693 var mtype
= v
.resolve_mtype
(ntype
)
1694 if mtype
!= null and mtype
!= expr
then
1695 var umtype
= v
.anchor_to
(mtype
)
1696 v
.modelbuilder
.warning
(self, "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")
1698 self.is_typed
= true