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.
24 private class TypeVisitor
25 var modelbuilder
: ModelBuilder
26 var nclassdef
: AClassdef
27 var mpropdef
: MPropDef
29 var selfvariable
: Variable = new Variable("self")
31 init(modelbuilder
: ModelBuilder, nclassdef
: AClassdef, mpropdef
: MPropDef)
33 self.modelbuilder
= modelbuilder
34 self.nclassdef
= nclassdef
35 self.mpropdef
= mpropdef
37 var mclass
= nclassdef
.mclassdef
.mclass
39 var selfvariable
= new Variable("self")
40 self.selfvariable
= selfvariable
41 selfvariable
.declared_type
= mclass
.mclass_type
44 fun mmodule
: MModule do return self.nclassdef
.mclassdef
.mmodule
46 fun anchor
: MClassType do return self.nclassdef
.mclassdef
.bound_mtype
48 fun anchor_to
(mtype
: MType): MType
50 var mmodule
= self.nclassdef
.mclassdef
.mmodule
51 var anchor
= self.nclassdef
.mclassdef
.bound_mtype
52 return mtype
.anchor_to
(mmodule
, anchor
)
55 fun is_subtype
(sub
, sup
: MType): Bool
57 var mmodule
= self.nclassdef
.mclassdef
.mmodule
58 var anchor
= self.nclassdef
.mclassdef
.bound_mtype
59 return sub
.is_subtype
(mmodule
, anchor
, sup
)
62 fun resolve_for
(mtype
, subtype
: MType, for_self
: Bool): MType
64 var mmodule
= self.nclassdef
.mclassdef
.mmodule
65 var anchor
= self.nclassdef
.mclassdef
.bound_mtype
66 #print "resolve_for {mtype} sub={subtype} forself={for_self} mmodule={mmodule} anchor={anchor}"
67 var res
= mtype
.resolve_for
(subtype
, anchor
, mmodule
, not for_self
)
71 # Retrieve the signature of a MMethodDef resolved for a specific call.
72 # This method is an helper to symplify the query on the model.
74 # Note: `for_self` indicates if the reciever is self or not.
75 # If yes, virtual types are not resolved.
76 fun resolve_signature_for
(mmethoddef
: MMethodDef, recv
: MType, for_self
: Bool): MSignature
78 return self.resolve_for
(mmethoddef
.msignature
.as(not null), recv
, for_self
).as(MSignature)
81 # Check that `sub` is a subtype of `sup`.
82 # If `sub` is not a valud suptype, then display an error on `node` an return null.
83 # If `sub` is a safe subtype of `sup` then return `sub`.
84 # If `sun` is an insafe subtype (ie an imlicit cast is required), then return `sup`.
86 # The point of the return type is to determinate the usable type on an expression:
87 # If the suptype is safe, then the return type is the one on the expression typed by `sub`.
88 # Is the subtype is unsafe, then the return type is the one of an implicit cast on `sup`.
89 fun check_subtype
(node
: ANode, sub
, sup
: MType): nullable MType
91 if self.is_subtype
(sub
, sup
) then return sub
92 if self.is_subtype
(sub
, self.anchor_to
(sup
)) then
93 # FIXME workarround to the current unsafe typing policy. To remove once fixed virtual types exists.
94 #node.debug("Unsafe typing: expected {sup}, got {sub}")
97 self.modelbuilder
.error
(node
, "Type error: expected {sup}, got {sub}")
101 # Visit an expression and do not care about the return value
102 fun visit_stmt
(nexpr
: nullable AExpr)
104 if nexpr
== null then return
105 nexpr
.accept_typing
(self)
108 # Visit an expression and expects that it is not a statement
109 # Return the type of the expression
110 # Display an error and return null if:
111 # * the type cannot be determined or
112 # * `nexpr' is a statement
113 fun visit_expr
(nexpr
: AExpr): nullable MType
115 nexpr
.accept_typing
(self)
116 var mtype
= nexpr
.mtype
117 if mtype
!= null then return mtype
118 if not nexpr
.is_typed
then
119 if not self.modelbuilder
.toolcontext
.error_count
> 0 then # check that there is really an error
120 if self.modelbuilder
.toolcontext
.verbose_level
> 1 then
121 nexpr
.debug
("No return type but no error.")
124 return null # forward error
126 self.error
(nexpr
, "Type error: expected expression.")
130 # Visit an expression and expect its static type is a least a `sup'
131 # Return the type of the expression
132 # * the type cannot be determined or
133 # * `nexpr' is a statement
134 # * `nexpt' is not a `sup'
135 fun visit_expr_subtype
(nexpr
: AExpr, sup
: nullable MType): nullable MType
137 var sub
= visit_expr
(nexpr
)
138 if sub
== null then return null # Forward error
140 if sup
== null then return null # Forward error
142 var res
= check_subtype
(nexpr
, sub
, sup
)
144 nexpr
.implicit_cast_to
= res
149 # Visit an expression and expect its static type is a bool
150 # Return the type of the expression
151 # * the type cannot be determined or
152 # * `nexpr' is a statement
153 # * `nexpt' is not a `sup'
154 fun visit_expr_bool
(nexpr
: AExpr): nullable MType
156 return self.visit_expr_subtype
(nexpr
, self.type_bool
(nexpr
))
160 private fun visit_expr_cast
(node
: ANode, nexpr
: AExpr, ntype
: AType): nullable MType
162 var sub
= visit_expr
(nexpr
)
163 if sub
== null then return null # Forward error
165 var sup
= self.resolve_mtype
(ntype
)
166 if sup
== null then return null # Forward error
168 var mmodule
= self.nclassdef
.mclassdef
.mmodule
169 var anchor
= self.nclassdef
.mclassdef
.bound_mtype
171 self.modelbuilder
.warning
(node
, "Warning: Expression is already a {sup}.")
172 else if self.is_subtype
(sub
, sup
) and not sup
.need_anchor
then
173 self.modelbuilder
.warning
(node
, "Warning: Expression is already a {sup} since it is a {sub}.")
178 fun try_get_mproperty_by_name2
(anode
: ANode, mtype
: MType, name
: String): nullable MProperty
180 return self.modelbuilder
.try_get_mproperty_by_name2
(anode
, self.nclassdef
.mclassdef
.mmodule
, mtype
, name
)
183 fun resolve_mtype
(node
: AType): nullable MType
185 return self.modelbuilder
.resolve_mtype
(self.nclassdef
, node
)
188 fun try_get_mclass
(node
: ANode, name
: String): nullable MClass
190 var mmodule
= self.nclassdef
.mclassdef
.mmodule
191 var mclass
= modelbuilder
.try_get_mclass_by_name
(node
, mmodule
, name
)
195 fun get_mclass
(node
: ANode, name
: String): nullable MClass
197 var mmodule
= self.nclassdef
.mclassdef
.mmodule
198 var mclass
= modelbuilder
.try_get_mclass_by_name
(node
, mmodule
, name
)
199 if mclass
== null then
200 self.modelbuilder
.error
(node
, "Type Error: missing primitive class `{name}'.")
205 fun type_bool
(node
: ANode): nullable MType
207 var mclass
= self.get_mclass
(node
, "Bool")
208 if mclass
== null then return null
209 return mclass
.mclass_type
212 fun get_method
(node
: ANode, recvtype
: MType, name
: String, recv_is_self
: Bool): nullable CallSite
214 var unsafe_type
= self.anchor_to
(recvtype
)
216 #debug("recv: {recvtype} (aka {unsafe_type})")
217 if recvtype
isa MNullType then
218 self.error
(node
, "Error: Method '{name}' call on 'null'.")
222 var mproperty
= self.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
223 if mproperty
== null then
224 #self.modelbuilder.error(node, "Type error: property {name} not found in {unsafe_type} (ie {recvtype})")
226 self.modelbuilder
.error
(node
, "Error: Method or variable '{name}' unknown in {recvtype}.")
228 self.modelbuilder
.error
(node
, "Error: Method '{name}' doesn't exists in {recvtype}.")
233 assert mproperty
isa MMethod
234 if mproperty
.visibility
== protected_visibility
and not recv_is_self
and self.mmodule
.visibility_for
(mproperty
.intro_mclassdef
.mmodule
) < intrude_visibility
then
235 self.modelbuilder
.error
(node
, "Error: Method '{name}' is protected and can only acceded by self. {mproperty.intro_mclassdef.mmodule.visibility_for(self.mmodule)}")
239 var propdefs
= mproperty
.lookup_definitions
(self.mmodule
, unsafe_type
)
240 if propdefs
.length
== 0 then
241 self.modelbuilder
.error
(node
, "Type error: no definition found for property {name} in {unsafe_type}")
243 else if propdefs
.length
> 1 then
244 self.modelbuilder
.error
(node
, "Error: confliting property definitions for property {name} in {unsafe_type}: {propdefs.join(" ")}")
248 var mpropdef
= propdefs
.first
250 var msignature
= self.resolve_signature_for
(mpropdef
, recvtype
, recv_is_self
)
252 var callsite
= new CallSite(node
, recvtype
, recv_is_self
, mproperty
, mpropdef
, msignature
)
256 # Visit the expressions of args and cheik their conformity with the corresponding typi in signature
257 # The point of this method is to handle varargs correctly
258 # Note: The signature must be correctly adapted
259 fun check_signature
(node
: ANode, args
: Array[AExpr], name
: String, msignature
: MSignature): Bool
261 var vararg_rank
= msignature
.vararg_rank
262 if vararg_rank
>= 0 then
263 if args
.length
< msignature
.arity
then
264 #self.modelbuilder.error(node, "Error: Incorrect number of parameters. Got {args.length}, expected at least {msignature.arity}. Signature is {msignature}")
265 self.modelbuilder
.error
(node
, "Error: arity mismatch; prototype is '{name}{msignature}'")
268 else if args
.length
!= msignature
.arity
then
269 self.modelbuilder
.error
(node
, "Error: Incorrect number of parameters. Got {args.length}, expected {msignature.arity}. Signature is {msignature}")
273 #debug("CALL {unsafe_type}.{msignature}")
275 var vararg_decl
= args
.length
- msignature
.arity
276 for i
in [0..msignature
.arity
[ do
278 if i
== vararg_rank
then continue # skip the vararg
279 if i
> vararg_rank
then
282 var paramtype
= msignature
.mparameters
[i
].mtype
283 self.visit_expr_subtype
(args
[j
], paramtype
)
285 if vararg_rank
>= 0 then
286 var varargs
= new Array[AExpr]
287 var paramtype
= msignature
.mparameters
[vararg_rank
].mtype
288 for j
in [vararg_rank
..vararg_rank
+vararg_decl
] do
290 self.visit_expr_subtype
(args
[j
], paramtype
)
296 fun error
(node
: ANode, message
: String)
298 self.modelbuilder
.toolcontext
.error
(node
.hot_location
, message
)
301 fun get_variable
(node
: AExpr, variable
: Variable): nullable MType
303 var flow
= node
.after_flow_context
305 self.error
(node
, "No context!")
309 if flow
.vars
.has_key
(variable
) then
310 return flow
.vars
[variable
]
312 #node.debug("*** START Collected for {variable}")
313 var mtypes
= flow
.collect_types
(variable
)
314 #node.debug("**** END Collected for {variable}")
315 if mtypes
== null or mtypes
.length
== 0 then
316 return variable
.declared_type
317 else if mtypes
.length
== 1 then
320 var res
= merge_types
(node
,mtypes
)
321 if res
== null then res
= variable
.declared_type
327 fun set_variable
(node
: AExpr, variable
: Variable, mtype
: nullable MType)
329 var flow
= node
.after_flow_context
332 flow
.set_var
(variable
, mtype
)
335 fun merge_types
(node
: ANode, col
: Array[nullable MType]): nullable MType
337 if col
.length
== 1 then return col
.first
338 var res
= new Array[nullable MType]
340 if t1
== null then continue # return null
343 if t2
== null then continue # return null
344 if t2
isa MNullableType or t2
isa MNullType then
347 if not is_subtype
(t2
, t1
) then found
= false
350 #print "merge {col.join(" ")} -> {t1}"
354 #self.modelbuilder.warning(node, "Type Error: {col.length} conflicting types: <{col.join(", ")}>")
359 # A specific method call site with its associated informations.
361 # The assiciated node for location
364 # The statis type of the receiver
367 # Is the receiver self?
368 # If "for_self", virtual types of the signature are keeped
369 # If "not_for_self", virtual type are erased
370 var recv_is_self
: Bool
372 # The designated method
373 var mproperty
: MMethod
375 # The statically designated method definition
376 # The most specif one, it is.
377 var mpropdef
: MMethodDef
379 # The resolved signature for the receiver
380 var msignature
: MSignature
382 private fun check_signature
(v
: TypeVisitor, args
: Array[AExpr]): Bool
384 return v
.check_signature
(self.node
, args
, self.mproperty
.name
, self.msignature
)
389 # The declared type of the variable
390 var declared_type
: nullable MType
393 redef class FlowContext
394 # Store changes of types because of type evolution
395 private var vars
: HashMap[Variable, nullable MType] = new HashMap[Variable, nullable MType]
396 private var cache
: HashMap[Variable, nullable Array[nullable MType]] = new HashMap[Variable, nullable Array[nullable MType]]
398 # Adapt the variable to a static type
399 # Warning1: do not modify vars directly.
400 # Warning2: sub-flow may have cached a unadapted variabial
401 private fun set_var
(variable
: Variable, mtype
: nullable MType)
403 self.vars
[variable
] = mtype
404 self.cache
.keys
.remove
(variable
)
407 private fun collect_types
(variable
: Variable): nullable Array[nullable MType]
409 if cache
.has_key
(variable
) then
410 return cache
[variable
]
412 var res
: nullable Array[nullable MType] = null
413 if vars
.has_key
(variable
) then
414 var mtype
= vars
[variable
]
416 else if self.previous
.is_empty
then
418 res
= [variable
.declared_type
]
420 for flow
in self.previous
do
421 if flow
.is_unreachable
then continue
422 var r2
= flow
.collect_types
(variable
)
423 if r2
== null then continue
428 if not res
.has
(t
) then res
.add
(t
)
433 cache
[variable
] = res
439 # The entry point of the whole typing analysis
440 fun do_typing
(modelbuilder
: ModelBuilder)
444 # The variable associated to the reciever (if any)
445 var selfvariable
: nullable Variable
448 redef class AConcreteMethPropdef
449 redef fun do_typing
(modelbuilder
: ModelBuilder)
451 var nclassdef
= self.parent
.as(AClassdef)
452 var mpropdef
= self.mpropdef
.as(not null)
453 var v
= new TypeVisitor(modelbuilder
, nclassdef
, mpropdef
)
454 self.selfvariable
= v
.selfvariable
456 var nblock
= self.n_block
457 if nblock
== null then return
459 var mmethoddef
= self.mpropdef
.as(not null)
460 for i
in [0..mmethoddef
.msignature
.arity
[ do
461 var mtype
= mmethoddef
.msignature
.mparameters
[i
].mtype
462 if mmethoddef
.msignature
.vararg_rank
== i
then
463 var arrayclass
= v
.get_mclass
(self.n_signature
.n_params
[i
], "Array")
464 if arrayclass
== null then return # Skip error
465 mtype
= arrayclass
.get_mtype
([mtype
])
467 var variable
= self.n_signature
.n_params
[i
].variable
468 assert variable
!= null
469 variable
.declared_type
= mtype
471 for i
in [0..mmethoddef
.msignature
.mclosures
.length
[ do
472 var mclosure
= mmethoddef
.msignature
.mclosures
[i
]
473 var variable
= self.n_signature
.n_closure_decls
[i
].variable
474 assert variable
!= null
475 variable
.declared_type
= mclosure
.mtype
479 if not nblock
.after_flow_context
.is_unreachable
and mmethoddef
.msignature
.return_mtype
!= null then
480 # We reach the end of the function without having a return, it is bad
481 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
486 redef class AAttrPropdef
487 redef fun do_typing
(modelbuilder
: ModelBuilder)
489 var nclassdef
= self.parent
.as(AClassdef)
490 var v
= new TypeVisitor(modelbuilder
, nclassdef
, self.mpropdef
.as(not null))
491 self.selfvariable
= v
.selfvariable
493 var nexpr
= self.n_expr
494 if nexpr
!= null then
495 var mtype
= self.mpropdef
.static_mtype
496 v
.visit_expr_subtype
(nexpr
, mtype
)
504 # The static type of the expression.
505 # null if self is a statement of in case of error
506 var mtype
: nullable MType = null
508 # Is the statement correctly typed?
509 # Used to distinguish errors and statements when `mtype' == null
510 var is_typed
: Bool = false
512 # If required, the following implicit cast ".as(XXX)"
513 # Such a cast may by required after evaluating the expression when
514 # a unsafe operation is detected (silently accepted by the Nit language).
515 # The attribute is computed by `check_subtype`
516 var implicit_cast_to
: nullable MType = null
518 # Return the variable read (if any)
519 # Used to perform adaptive typing
520 fun its_variable
: nullable Variable do return null
522 private fun accept_typing
(v
: TypeVisitor)
524 v
.error
(self, "no implemented accept_typing for {self.class_name}")
528 redef class ABlockExpr
529 redef fun accept_typing
(v
)
531 for e
in self.n_expr
do v
.visit_stmt
(e
)
536 redef class AVardeclExpr
537 redef fun accept_typing
(v
)
539 var variable
= self.variable
540 if variable
== null then return # Skip error
542 var ntype
= self.n_type
543 var mtype
: nullable MType
544 if ntype
== null then
547 mtype
= v
.resolve_mtype
(ntype
)
548 if mtype
== null then return # Skip error
551 var nexpr
= self.n_expr
552 if nexpr
!= null then
553 if mtype
!= null then
554 v
.visit_expr_subtype
(nexpr
, mtype
)
556 mtype
= v
.visit_expr
(nexpr
)
557 if mtype
== null then return # Skip error
561 if mtype
== null then
562 mtype
= v
.get_mclass
(self, "Object").mclass_type
.as_nullable
565 variable
.declared_type
= mtype
566 v
.set_variable
(self, variable
, mtype
)
568 #debug("var {variable}: {mtype}")
575 redef fun its_variable
do return self.variable
576 redef fun accept_typing
(v
)
578 var variable
= self.variable
579 if variable
== null then return # Skip error
581 var mtype
= v
.get_variable
(self, variable
)
582 if mtype
!= null then
583 #debug("{variable} is {mtype}")
585 #debug("{variable} is untyped")
592 redef class AVarAssignExpr
593 redef fun accept_typing
(v
)
595 var variable
= self.variable
596 assert variable
!= null
598 var mtype
= v
.visit_expr_subtype
(n_value
, variable
.declared_type
)
600 v
.set_variable
(self, variable
, mtype
)
606 redef class AReassignFormExpr
607 # @depreciated use `reassign_callsite`
608 fun reassign_property
: nullable MMethodDef do return self.reassign_callsite
.mpropdef
610 # The method designed by the reassign operator.
611 var reassign_callsite
: nullable CallSite
613 var read_type
: nullable MType = null
615 # Determine the `reassign_property'
616 # `readtype' is the type of the reading of the left value.
617 # `writetype' is the type of the writing of the left value.
618 # (Because of ACallReassignExpr, both can be different.
619 # Return the static type of the value to store.
620 private fun resolve_reassignment
(v
: TypeVisitor, readtype
, writetype
: MType): nullable MType
622 var reassign_name
: String
623 if self.n_assign_op
isa APlusAssignOp then
625 else if self.n_assign_op
isa AMinusAssignOp then
631 self.read_type
= readtype
633 if readtype
isa MNullType then
634 v
.error
(self, "Error: Method '{reassign_name}' call on 'null'.")
638 var callsite
= v
.get_method
(self, readtype
, reassign_name
, false)
639 if callsite
== null then return null # Skip error
640 self.reassign_callsite
= callsite
642 var msignature
= callsite
.msignature
643 var rettype
= msignature
.return_mtype
644 assert msignature
.arity
== 1 and rettype
!= null
646 var value_type
= v
.visit_expr_subtype
(self.n_value
, msignature
.mparameters
.first
.mtype
)
647 if value_type
== null then return null # Skip error
649 v
.check_subtype
(self, rettype
, writetype
)
654 redef class AVarReassignExpr
655 redef fun accept_typing
(v
)
657 var variable
= self.variable
658 assert variable
!= null
660 var readtype
= v
.get_variable
(self, variable
)
661 if readtype
== null then return
663 var writetype
= variable
.declared_type
664 if writetype
== null then return
666 var rettype
= self.resolve_reassignment
(v
, readtype
, writetype
)
668 v
.set_variable
(self, variable
, rettype
)
675 redef class AContinueExpr
676 redef fun accept_typing
(v
)
678 var nexpr
= self.n_expr
679 if nexpr
!= null then
680 var mtype
= v
.visit_expr
(nexpr
)
686 redef class ABreakExpr
687 redef fun accept_typing
(v
)
689 var nexpr
= self.n_expr
690 if nexpr
!= null then
691 var mtype
= v
.visit_expr
(nexpr
)
697 redef class AReturnExpr
698 redef fun accept_typing
(v
)
700 var nexpr
= self.n_expr
701 var ret_type
= v
.mpropdef
.as(MMethodDef).msignature
.return_mtype
702 if nexpr
!= null then
703 if ret_type
!= null then
704 var mtype
= v
.visit_expr_subtype
(nexpr
, ret_type
)
706 var mtype
= v
.visit_expr
(nexpr
)
707 v
.error
(self, "Error: Return with value in a procedure.")
709 else if ret_type
!= null then
710 v
.error
(self, "Error: Return without value in a function.")
716 redef class AAbortExpr
717 redef fun accept_typing
(v
)
724 redef fun accept_typing
(v
)
726 v
.visit_expr_bool
(n_expr
)
734 redef class AIfexprExpr
735 redef fun accept_typing
(v
)
737 v
.visit_expr_bool
(n_expr
)
739 var t1
= v
.visit_expr
(n_then
)
740 var t2
= v
.visit_expr
(n_else
)
742 if t1
== null or t2
== null then
746 var t
= v
.merge_types
(self, [t1
, t2
])
748 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
755 redef fun accept_typing
(v
)
757 v
.visit_stmt
(n_block
)
762 redef class AWhileExpr
763 redef fun accept_typing
(v
)
765 v
.visit_expr_bool
(n_expr
)
767 v
.visit_stmt
(n_block
)
772 redef class ALoopExpr
773 redef fun accept_typing
(v
)
775 v
.visit_stmt
(n_block
)
781 var coltype
: nullable MClassType
783 var method_iterator
: nullable MMethod
784 var method_is_ok
: nullable MMethod
785 var method_item
: nullable MMethod
786 var method_next
: nullable MMethod
787 var method_key
: nullable MMethod
789 private fun do_type_iterator
(v
: TypeVisitor, mtype
: MType)
791 var objcla
= v
.get_mclass
(self, "Object")
792 if objcla
== null then return
797 var colcla
= v
.try_get_mclass
(self, "Collection")
798 if colcla
!= null and v
.is_subtype
(mtype
, colcla
.get_mtype
([objcla
.mclass_type
.as_nullable
])) then
799 var coltype
= mtype
.supertype_to
(v
.mmodule
, v
.anchor
, colcla
)
800 self.coltype
= coltype
801 var variables
= self.variables
802 if variables
.length
!= 1 then
803 v
.error
(self, "Type Error: Expected one variable")
805 variables
.first
.declared_type
= coltype
.arguments
.first
810 var mapcla
= v
.try_get_mclass
(self, "Map")
811 if mapcla
!= null and v
.is_subtype
(mtype
, mapcla
.get_mtype
([objcla
.mclass_type
.as_nullable
, objcla
.mclass_type
.as_nullable
])) then
812 var coltype
= mtype
.supertype_to
(v
.mmodule
, v
.anchor
, mapcla
)
813 self.coltype
= coltype
814 var variables
= self.variables
815 if variables
.length
!= 2 then
816 v
.error
(self, "Type Error: Expected two variables")
818 variables
[0].declared_type
= coltype
.arguments
[0]
819 variables
[1].declared_type
= coltype
.arguments
[1]
824 if is_col
or is_map
then
825 # get iterator method
826 var coltype
= self.coltype
.as(not null)
827 var itdef
= v
.get_method
(self, coltype
, "iterator", true)
828 if itdef
== null then
829 v
.error
(self, "Type Error: Expected method 'iterator' in type {coltype}")
832 self.method_iterator
= itdef
.mproperty
835 var ittype
= itdef
.msignature
.return_mtype
836 if ittype
== null then
837 v
.error
(self, "Type Error: Expected method 'iterator' to return an Iterator type")
841 # get methods is_ok, next, item
842 var ikdef
= v
.get_method
(self, ittype
, "is_ok", false)
843 if ikdef
== null then
844 v
.error
(self, "Type Error: Expected method 'is_ok' in Iterator type {ittype}")
847 self.method_is_ok
= ikdef
.mproperty
849 var itemdef
= v
.get_method
(self, ittype
, "item", false)
850 if itemdef
== null then
851 v
.error
(self, "Type Error: Expected method 'item' in Iterator type {ittype}")
854 self.method_item
= itemdef
.mproperty
856 var nextdef
= v
.get_method
(self, ittype
, "next", false)
857 if nextdef
== null then
858 v
.error
(self, "Type Error: Expected method 'next' in Iterator type {ittype}")
861 self.method_next
= nextdef
.mproperty
864 var keydef
= v
.get_method
(self, ittype
, "key", false)
865 if keydef
== null then
866 v
.error
(self, "Type Error: Expected method 'key' in Iterator type {ittype}")
869 self.method_key
= keydef
.mproperty
874 v
.modelbuilder
.error
(self, "NOT YET IMPLEMENTED: Do 'for' on {mtype}")
877 redef fun accept_typing
(v
)
879 var mtype
= v
.visit_expr
(n_expr
)
880 if mtype
== null then return
882 self.do_type_iterator
(v
, mtype
)
884 v
.visit_stmt
(n_block
)
889 redef class AAssertExpr
890 redef fun accept_typing
(v
)
892 v
.visit_expr_bool
(n_expr
)
900 redef fun accept_typing
(v
)
902 v
.visit_expr_bool
(n_expr
)
903 v
.visit_expr_bool
(n_expr2
)
904 self.mtype
= v
.type_bool
(self)
909 redef fun accept_typing
(v
)
911 v
.visit_expr_bool
(n_expr
)
912 v
.visit_expr_bool
(n_expr2
)
913 self.mtype
= v
.type_bool
(self)
919 redef fun accept_typing
(v
)
921 v
.visit_expr_bool
(n_expr
)
922 self.mtype
= v
.type_bool
(self)
926 redef class AOrElseExpr
927 redef fun accept_typing
(v
)
929 var t1
= v
.visit_expr
(n_expr
)
930 var t2
= v
.visit_expr
(n_expr2
)
932 if t1
== null or t2
== null then
936 if t1
isa MNullableType then
940 var t
= v
.merge_types
(self, [t1
, t2
])
942 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
949 redef fun accept_typing
(v
)
952 v
.visit_expr
(n_expr2
)
953 self.mtype
= v
.type_bool
(self)
957 redef class ATrueExpr
958 redef fun accept_typing
(v
)
960 self.mtype
= v
.type_bool
(self)
964 redef class AFalseExpr
965 redef fun accept_typing
(v
)
967 self.mtype
= v
.type_bool
(self)
972 redef fun accept_typing
(v
)
974 var mclass
= v
.get_mclass
(self, "Int")
975 if mclass
== null then return # Forward error
976 self.mtype
= mclass
.mclass_type
980 redef class AFloatExpr
981 redef fun accept_typing
(v
)
983 var mclass
= v
.get_mclass
(self, "Float")
984 if mclass
== null then return # Forward error
985 self.mtype
= mclass
.mclass_type
989 redef class ACharExpr
990 redef fun accept_typing
(v
)
992 var mclass
= v
.get_mclass
(self, "Char")
993 if mclass
== null then return # Forward error
994 self.mtype
= mclass
.mclass_type
998 redef class AStringFormExpr
999 redef fun accept_typing
(v
)
1001 var mclass
= v
.get_mclass
(self, "String")
1002 if mclass
== null then return # Forward error
1003 self.mtype
= mclass
.mclass_type
1007 redef class ASuperstringExpr
1008 redef fun accept_typing
(v
)
1010 var mclass
= v
.get_mclass
(self, "String")
1011 if mclass
== null then return # Forward error
1012 self.mtype
= mclass
.mclass_type
1013 for nexpr
in self.n_exprs
do
1014 var t
= v
.visit_expr
(nexpr
)
1019 redef class AArrayExpr
1020 redef fun accept_typing
(v
)
1022 var mtypes
= new Array[nullable MType]
1023 for e
in self.n_exprs
.n_exprs
do
1024 var t
= v
.visit_expr
(e
)
1030 var mtype
= v
.merge_types
(self, mtypes
)
1031 if mtype
== null then
1032 v
.error
(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
1035 var mclass
= v
.get_mclass
(self, "Array")
1036 if mclass
== null then return # Forward error
1037 self.mtype
= mclass
.get_mtype
([mtype
])
1041 redef class ARangeExpr
1042 redef fun accept_typing
(v
)
1044 var discrete_class
= v
.get_mclass
(self, "Discrete")
1045 if discrete_class
== null then return # Forward error
1046 var discrete_type
= discrete_class
.intro
.bound_mtype
1047 var t1
= v
.visit_expr_subtype
(self.n_expr
, discrete_type
)
1048 var t2
= v
.visit_expr_subtype
(self.n_expr2
, discrete_type
)
1049 if t1
== null or t2
== null then return
1050 var mclass
= v
.get_mclass
(self, "Range")
1051 if mclass
== null then return # Forward error
1052 if v
.is_subtype
(t1
, t2
) then
1053 self.mtype
= mclass
.get_mtype
([t2
])
1054 else if v
.is_subtype
(t2
, t1
) then
1055 self.mtype
= mclass
.get_mtype
([t1
])
1057 v
.error
(self, "Type Error: Cannot create range: {t1} vs {t2}")
1062 redef class ANullExpr
1063 redef fun accept_typing
(v
)
1065 self.mtype
= v
.mmodule
.model
.null_type
1069 redef class AIsaExpr
1070 # The static type to cast to.
1071 # (different from the static type of the expression that is Bool).
1072 var cast_type
: nullable MType
1073 redef fun accept_typing
(v
)
1075 var mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
1076 self.cast_type
= mtype
1078 var variable
= self.n_expr
.its_variable
1079 if variable
!= null then
1080 var orig
= self.n_expr
.mtype
1081 var from
= if orig
!= null then orig
.to_s
else "invalid"
1082 var to
= if mtype
!= null then mtype
.to_s
else "invalid"
1083 #debug("adapt {variable}: {from} -> {to}")
1084 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1087 self.mtype
= v
.type_bool
(self)
1091 redef class AAsCastExpr
1092 redef fun accept_typing
(v
)
1094 self.mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
1098 redef class AAsNotnullExpr
1099 redef fun accept_typing
(v
)
1101 var mtype
= v
.visit_expr
(self.n_expr
)
1102 if mtype
isa MNullType then
1103 v
.error
(self, "Type error: as(not null) on null")
1106 if mtype
isa MNullableType then
1107 self.mtype
= mtype
.mtype
1110 # TODO: warn on useless as not null
1115 redef class AProxyExpr
1116 redef fun accept_typing
(v
)
1118 self.mtype
= v
.visit_expr
(self.n_expr
)
1122 redef class ASelfExpr
1123 redef var its_variable
: nullable Variable
1124 redef fun accept_typing
(v
)
1126 var variable
= v
.selfvariable
1127 self.its_variable
= variable
1128 self.mtype
= v
.get_variable
(self, variable
)
1132 ## MESSAGE SENDING AND PROPERTY
1134 redef class ASendExpr
1135 # @depreciated: use `callsite`
1136 fun mproperty
: nullable MMethod do return callsite
.mproperty
1138 # The property invoked by the send.
1139 var callsite
: nullable CallSite
1141 redef fun accept_typing
(v
)
1143 var recvtype
= v
.visit_expr
(self.n_expr
)
1144 var name
= self.property_name
1146 if recvtype
== null then return # Forward error
1147 if recvtype
isa MNullType then
1148 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1152 var callsite
= v
.get_method
(self, recvtype
, name
, self.n_expr
isa ASelfExpr)
1153 if callsite
== null then return
1154 self.callsite
= callsite
1155 var msignature
= callsite
.msignature
1157 var args
= compute_raw_arguments
1158 self.raw_arguments
= args
1160 callsite
.check_signature
(v
, args
)
1162 if callsite
.mproperty
.is_init
then
1163 var vmpropdef
= v
.mpropdef
1164 if not (vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_init
) then
1165 v
.error
(self, "Can call a init only in another init")
1169 var ret
= msignature
.return_mtype
1173 self.is_typed
= true
1176 if self.n_closure_defs
.length
== msignature
.mclosures
.length
then
1177 for i
in [0..self.n_closure_defs
.length
[ do
1178 self.n_closure_defs
[i
].accept_typing
(v
, msignature
.mclosures
[i
])
1181 debug
("closure: got {self.n_closure_defs.length}, want {msignature.mclosures.length}")
1185 # The name of the property
1186 # Each subclass simply provide the correct name.
1187 private fun property_name
: String is abstract
1189 # An array of all arguments (excluding self)
1190 var raw_arguments
: nullable Array[AExpr]
1192 private fun compute_raw_arguments
: Array[AExpr] is abstract
1195 redef class ABinopExpr
1196 redef fun compute_raw_arguments
do return [n_expr2
]
1199 redef fun property_name
do return "=="
1200 redef fun accept_typing
(v
)
1204 var variable
= self.n_expr
.its_variable
1205 if variable
== null then return
1206 var mtype
= self.n_expr2
.mtype
1207 if not mtype
isa MNullType then return
1208 var vartype
= v
.get_variable
(self, variable
)
1209 if not vartype
isa MNullableType then return
1210 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1211 self.after_flow_context
.when_false
.set_var
(variable
, vartype
.mtype
)
1212 #debug("adapt {variable}:{vartype} ; true->{mtype} false->{vartype.mtype}")
1216 redef fun property_name
do return "!="
1217 redef fun accept_typing
(v
)
1221 var variable
= self.n_expr
.its_variable
1222 if variable
== null then return
1223 var mtype
= self.n_expr2
.mtype
1224 if not mtype
isa MNullType then return
1225 var vartype
= v
.get_variable
(self, variable
)
1226 if not vartype
isa MNullableType then return
1227 self.after_flow_context
.when_false
.set_var
(variable
, mtype
)
1228 self.after_flow_context
.when_true
.set_var
(variable
, vartype
.mtype
)
1229 #debug("adapt {variable}:{vartype} ; true->{vartype.mtype} false->{mtype}")
1233 redef fun property_name
do return "<"
1236 redef fun property_name
do return "<="
1239 redef fun property_name
do return "<<"
1242 redef fun property_name
do return ">"
1245 redef fun property_name
do return ">="
1248 redef fun property_name
do return ">>"
1250 redef class APlusExpr
1251 redef fun property_name
do return "+"
1253 redef class AMinusExpr
1254 redef fun property_name
do return "-"
1256 redef class AStarshipExpr
1257 redef fun property_name
do return "<=>"
1259 redef class AStarExpr
1260 redef fun property_name
do return "*"
1262 redef class ASlashExpr
1263 redef fun property_name
do return "/"
1265 redef class APercentExpr
1266 redef fun property_name
do return "%"
1269 redef class AUminusExpr
1270 redef fun property_name
do return "unary -"
1271 redef fun compute_raw_arguments
do return new Array[AExpr]
1275 redef class ACallExpr
1276 redef fun property_name
do return n_id
.text
1277 redef fun compute_raw_arguments
do return n_args
.to_a
1280 redef class ACallAssignExpr
1281 redef fun property_name
do return n_id
.text
+ "="
1282 redef fun compute_raw_arguments
1284 var res
= n_args
.to_a
1290 redef class ABraExpr
1291 redef fun property_name
do return "[]"
1292 redef fun compute_raw_arguments
do return n_args
.to_a
1295 redef class ABraAssignExpr
1296 redef fun property_name
do return "[]="
1297 redef fun compute_raw_arguments
1299 var res
= n_args
.to_a
1305 redef class ASendReassignFormExpr
1306 # @depreciated use `write_callsite`
1307 fun write_mproperty
: nullable MMethod do return write_callsite
.mproperty
1309 # The property invoked for the writing
1310 var write_callsite
: nullable CallSite
1312 redef fun accept_typing
(v
)
1314 var recvtype
= v
.visit_expr
(self.n_expr
)
1315 var name
= self.property_name
1317 if recvtype
== null then return # Forward error
1318 if recvtype
isa MNullType then
1319 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1323 var for_self
= self.n_expr
isa ASelfExpr
1324 var callsite
= v
.get_method
(self, recvtype
, name
, for_self
)
1326 if callsite
== null then return
1327 self.callsite
= callsite
1329 var args
= compute_raw_arguments
1330 self.raw_arguments
= args
1332 callsite
.check_signature
(v
, args
)
1334 var readtype
= callsite
.msignature
.return_mtype
1335 if readtype
== null then
1336 v
.error
(self, "Error: {name} is not a function")
1340 var wcallsite
= v
.get_method
(self, recvtype
, name
+ "=", self.n_expr
isa ASelfExpr)
1341 if wcallsite
== null then return
1342 self.write_callsite
= wcallsite
1344 var wtype
= self.resolve_reassignment
(v
, readtype
, wcallsite
.msignature
.mparameters
.last
.mtype
)
1345 if wtype
== null then return
1347 args
= args
.to_a
# duplicate so raw_arguments keeps only the getter args
1348 args
.add
(self.n_value
)
1349 wcallsite
.check_signature
(v
, args
)
1351 self.is_typed
= true
1355 redef class ACallReassignExpr
1356 redef fun property_name
do return n_id
.text
1357 redef fun compute_raw_arguments
do return n_args
.to_a
1360 redef class ABraReassignExpr
1361 redef fun property_name
do return "[]"
1362 redef fun compute_raw_arguments
do return n_args
.to_a
1365 redef class AInitExpr
1366 redef fun property_name
do return "init"
1367 redef fun compute_raw_arguments
do return n_args
.to_a
1371 fun to_a
: Array[AExpr] do return self.n_exprs
.to_a
1376 redef class ASuperExpr
1377 # The method to call if the super is in fact a 'super init call'
1378 # Note: if the super is a normal call-next-method, then this attribute is null
1379 var mproperty
: nullable MMethod
1381 redef fun accept_typing
(v
)
1383 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1384 var mproperty
= v
.mpropdef
.mproperty
1385 if not mproperty
isa MMethod then
1386 v
.error
(self, "Error: super only usable in a method")
1389 var superprops
= mproperty
.lookup_super_definitions
(v
.mmodule
, recvtype
)
1390 if superprops
.length
== 0 then
1391 if mproperty
.is_init
and v
.mpropdef
.is_intro
then
1392 process_superinit
(v
)
1395 v
.error
(self, "Error: No super method to call for {mproperty}.")
1397 else if superprops
.length
> 1 then
1398 v
.modelbuilder
.warning
(self, "Warning: NOT YET IMPLEMENTED: Conflicting super method to call for {mproperty}: {superprops.join(", ")}.")
1401 var superprop
= superprops
.first
1402 assert superprop
isa MMethodDef
1404 var msignature
= v
.resolve_signature_for
(superprop
, recvtype
, true)
1405 var args
= self.n_args
.to_a
1406 if args
.length
> 0 then
1407 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1409 self.mtype
= msignature
.return_mtype
1412 private fun process_superinit
(v
: TypeVisitor)
1414 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1415 var mproperty
= v
.mpropdef
.mproperty
1416 var superprop
: nullable MMethodDef = null
1417 for msupertype
in v
.nclassdef
.mclassdef
.supertypes
do
1418 msupertype
= msupertype
.anchor_to
(v
.mmodule
, recvtype
)
1419 var errcount
= v
.modelbuilder
.toolcontext
.error_count
1420 var candidate
= v
.try_get_mproperty_by_name2
(self, msupertype
, mproperty
.name
).as(nullable MMethod)
1421 if candidate
== null then
1422 if v
.modelbuilder
.toolcontext
.error_count
> errcount
then return # Forard error
1423 continue # Try next super-class
1425 if superprop
!= null and superprop
.mproperty
!= candidate
then
1426 v
.error
(self, "Error: conflicting super constructor to call for {mproperty}: {candidate.full_name}, {superprop.mproperty.full_name}")
1429 var candidatedefs
= candidate
.lookup_definitions
(v
.mmodule
, recvtype
)
1430 if superprop
!= null then
1431 if superprop
== candidatedefs
.first
then continue
1432 candidatedefs
.add
(superprop
)
1434 if candidatedefs
.length
> 1 then
1435 v
.error
(self, "Error: confliting property definitions for property {mproperty} in {recvtype}: {candidatedefs.join(", ")}")
1438 superprop
= candidatedefs
.first
1440 if superprop
== null then
1441 v
.error
(self, "Error: No super method to call for {mproperty}.")
1444 self.mproperty
= superprop
.mproperty
1446 var args
= self.n_args
.to_a
1447 var msignature
= v
.resolve_signature_for
(superprop
, recvtype
, true)
1448 if args
.length
> 0 then
1449 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1451 # TODO: Check signature
1454 self.is_typed
= true
1460 redef class ANewExpr
1461 # @depreciated use `callsite`
1462 fun mproperty
: nullable MMethod do return self.callsite
.mproperty
1464 # The constructor invoked by the new.
1465 var callsite
: nullable CallSite
1467 redef fun accept_typing
(v
)
1469 var recvtype
= v
.resolve_mtype
(self.n_type
)
1470 if recvtype
== null then return
1471 self.mtype
= recvtype
1473 if not recvtype
isa MClassType then
1474 if recvtype
isa MNullableType then
1475 v
.error
(self, "Type error: cannot instantiate the nullable type {recvtype}.")
1478 v
.error
(self, "Type error: cannot instantiate the formal type {recvtype}.")
1490 var callsite
= v
.get_method
(self, recvtype
, name
, false)
1491 if callsite
== null then return
1493 self.callsite
= callsite
1495 if not callsite
.mproperty
.is_init_for
(recvtype
.mclass
) then
1496 v
.error
(self, "Error: {name} is not a constructor.")
1500 var args
= n_args
.to_a
1501 callsite
.check_signature
(v
, args
)
1507 redef class AAttrFormExpr
1508 # The attribute acceded.
1509 var mproperty
: nullable MAttribute
1511 # The static type of the attribute.
1512 var attr_type
: nullable MType
1514 # Resolve the attribute acceded.
1515 private fun resolve_property
(v
: TypeVisitor)
1517 var recvtype
= v
.visit_expr
(self.n_expr
)
1518 if recvtype
== null then return # Skip error
1519 var name
= self.n_id
.text
1520 if recvtype
isa MNullType then
1521 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1525 var unsafe_type
= v
.anchor_to
(recvtype
)
1526 var mproperty
= v
.try_get_mproperty_by_name2
(self, unsafe_type
, name
)
1527 if mproperty
== null then
1528 v
.modelbuilder
.error
(self, "Error: Attribute {name} doesn't exists in {recvtype}.")
1531 assert mproperty
isa MAttribute
1532 self.mproperty
= mproperty
1534 var mpropdefs
= mproperty
.lookup_definitions
(v
.mmodule
, unsafe_type
)
1535 assert mpropdefs
.length
== 1
1536 var mpropdef
= mpropdefs
.first
1537 var attr_type
= mpropdef
.static_mtype
.as(not null)
1538 attr_type
= v
.resolve_for
(attr_type
, recvtype
, self.n_expr
isa ASelfExpr)
1539 self.attr_type
= attr_type
1543 redef class AAttrExpr
1544 redef fun accept_typing
(v
)
1546 self.resolve_property
(v
)
1547 self.mtype
= self.attr_type
1552 redef class AAttrAssignExpr
1553 redef fun accept_typing
(v
)
1555 self.resolve_property
(v
)
1556 var mtype
= self.attr_type
1558 v
.visit_expr_subtype
(self.n_value
, mtype
)
1559 self.is_typed
= true
1563 redef class AAttrReassignExpr
1564 redef fun accept_typing
(v
)
1566 self.resolve_property
(v
)
1567 var mtype
= self.attr_type
1568 if mtype
== null then return # Skip error
1570 self.resolve_reassignment
(v
, mtype
, mtype
)
1572 self.is_typed
= true
1576 redef class AIssetAttrExpr
1577 redef fun accept_typing
(v
)
1579 self.resolve_property
(v
)
1580 var mtype
= self.attr_type
1581 if mtype
== null then return # Skip error
1583 var recvtype
= self.n_expr
.mtype
.as(not null)
1584 var bound
= v
.resolve_for
(mtype
, recvtype
, false)
1585 if bound
isa MNullableType then
1586 v
.error
(self, "Error: isset on a nullable attribute.")
1588 self.mtype
= v
.type_bool
(self)
1594 redef class AClosureCallExpr
1595 redef fun accept_typing
(v
)
1597 var variable
= self.variable
1598 if variable
== null then return # Skip error
1600 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1601 var msignature
= variable
.declared_type
.as(not null)
1602 msignature
= v
.resolve_for
(msignature
, recvtype
, false).as(MSignature)
1604 var args
= n_args
.to_a
1605 v
.check_signature
(self, args
, variable
.name
, msignature
)
1607 self.is_typed
= true
1608 self.mtype
= msignature
.return_mtype
1612 redef class AClosureDef
1613 var mclosure
: nullable MParameter
1615 private fun accept_typing
(v
: TypeVisitor, mparameter
: MParameter)
1617 var variables
= self.variables
1618 if variables
== null then return
1620 self.mclosure
= mparameter
1621 var msignature
= mparameter
.mtype
.as(MSignature)
1623 if msignature
.arity
!= variables
.length
then
1624 v
.error
(self, "Type error: closure {mparameter.name} expects {msignature.arity} parameters, {variables.length} given")
1628 for i
in [0..variables
.length
[ do
1629 variables
[i
].declared_type
= msignature
.mparameters
[i
].mtype
1632 v
.visit_stmt
(self.n_expr
)
1638 redef class ADebugTypeExpr
1639 redef fun accept_typing
(v
)
1641 var expr
= v
.visit_expr
(self.n_expr
)
1642 if expr
== null then return
1643 var unsafe
= v
.anchor_to
(expr
)
1644 var ntype
= self.n_type
1645 var mtype
= v
.resolve_mtype
(ntype
)
1646 if mtype
!= null and mtype
!= expr
then
1647 var umtype
= v
.anchor_to
(mtype
)
1648 v
.modelbuilder
.warning
(self, "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")