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 erasure_cast
= false
253 var rettype
= mpropdef
.msignature
.return_mtype
254 if not recv_is_self
and rettype
!= null then
255 if rettype
isa MNullableType then rettype
= rettype
.mtype
256 if rettype
isa MParameterType then
257 var erased_rettype
= msignature
.return_mtype
258 assert erased_rettype
!= null
259 #node.debug("Erasure cast: Really a {rettype} but unsafely a {erased_rettype}")
264 var callsite
= new CallSite(node
, recvtype
, recv_is_self
, mproperty
, mpropdef
, msignature
, erasure_cast
)
268 # Visit the expressions of args and cheik their conformity with the corresponding typi in signature
269 # The point of this method is to handle varargs correctly
270 # Note: The signature must be correctly adapted
271 fun check_signature
(node
: ANode, args
: Array[AExpr], name
: String, msignature
: MSignature): Bool
273 var vararg_rank
= msignature
.vararg_rank
274 if vararg_rank
>= 0 then
275 if args
.length
< msignature
.arity
then
276 #self.modelbuilder.error(node, "Error: Incorrect number of parameters. Got {args.length}, expected at least {msignature.arity}. Signature is {msignature}")
277 self.modelbuilder
.error
(node
, "Error: arity mismatch; prototype is '{name}{msignature}'")
280 else if args
.length
!= msignature
.arity
then
281 self.modelbuilder
.error
(node
, "Error: Incorrect number of parameters. Got {args.length}, expected {msignature.arity}. Signature is {msignature}")
285 #debug("CALL {unsafe_type}.{msignature}")
287 var vararg_decl
= args
.length
- msignature
.arity
288 for i
in [0..msignature
.arity
[ do
290 if i
== vararg_rank
then continue # skip the vararg
291 if i
> vararg_rank
then
294 var paramtype
= msignature
.mparameters
[i
].mtype
295 self.visit_expr_subtype
(args
[j
], paramtype
)
297 if vararg_rank
>= 0 then
298 var varargs
= new Array[AExpr]
299 var paramtype
= msignature
.mparameters
[vararg_rank
].mtype
300 for j
in [vararg_rank
..vararg_rank
+vararg_decl
] do
302 self.visit_expr_subtype
(args
[j
], paramtype
)
308 fun error
(node
: ANode, message
: String)
310 self.modelbuilder
.toolcontext
.error
(node
.hot_location
, message
)
313 fun get_variable
(node
: AExpr, variable
: Variable): nullable MType
315 var flow
= node
.after_flow_context
317 self.error
(node
, "No context!")
321 if flow
.vars
.has_key
(variable
) then
322 return flow
.vars
[variable
]
324 #node.debug("*** START Collected for {variable}")
325 var mtypes
= flow
.collect_types
(variable
)
326 #node.debug("**** END Collected for {variable}")
327 if mtypes
== null or mtypes
.length
== 0 then
328 return variable
.declared_type
329 else if mtypes
.length
== 1 then
332 var res
= merge_types
(node
,mtypes
)
333 if res
== null then res
= variable
.declared_type
339 fun set_variable
(node
: AExpr, variable
: Variable, mtype
: nullable MType)
341 var flow
= node
.after_flow_context
344 flow
.set_var
(variable
, mtype
)
347 fun merge_types
(node
: ANode, col
: Array[nullable MType]): nullable MType
349 if col
.length
== 1 then return col
.first
350 var res
= new Array[nullable MType]
352 if t1
== null then continue # return null
355 if t2
== null then continue # return null
356 if t2
isa MNullableType or t2
isa MNullType then
359 if not is_subtype
(t2
, t1
) then found
= false
362 #print "merge {col.join(" ")} -> {t1}"
366 #self.modelbuilder.warning(node, "Type Error: {col.length} conflicting types: <{col.join(", ")}>")
371 # A specific method call site with its associated informations.
373 # The assiciated node for location
376 # The statis type of the receiver
379 # Is the receiver self?
380 # If "for_self", virtual types of the signature are keeped
381 # If "not_for_self", virtual type are erased
382 var recv_is_self
: Bool
384 # The designated method
385 var mproperty
: MMethod
387 # The statically designated method definition
388 # The most specif one, it is.
389 var mpropdef
: MMethodDef
391 # The resolved signature for the receiver
392 var msignature
: MSignature
394 # Is a implicit cast required on erasure typing policy?
395 var erasure_cast
: Bool
397 private fun check_signature
(v
: TypeVisitor, args
: Array[AExpr]): Bool
399 return v
.check_signature
(self.node
, args
, self.mproperty
.name
, self.msignature
)
404 # The declared type of the variable
405 var declared_type
: nullable MType
408 redef class FlowContext
409 # Store changes of types because of type evolution
410 private var vars
: HashMap[Variable, nullable MType] = new HashMap[Variable, nullable MType]
411 private var cache
: HashMap[Variable, nullable Array[nullable MType]] = new HashMap[Variable, nullable Array[nullable MType]]
413 # Adapt the variable to a static type
414 # Warning1: do not modify vars directly.
415 # Warning2: sub-flow may have cached a unadapted variabial
416 private fun set_var
(variable
: Variable, mtype
: nullable MType)
418 self.vars
[variable
] = mtype
419 self.cache
.keys
.remove
(variable
)
422 private fun collect_types
(variable
: Variable): nullable Array[nullable MType]
424 if cache
.has_key
(variable
) then
425 return cache
[variable
]
427 var res
: nullable Array[nullable MType] = null
428 if vars
.has_key
(variable
) then
429 var mtype
= vars
[variable
]
431 else if self.previous
.is_empty
then
433 res
= [variable
.declared_type
]
435 for flow
in self.previous
do
436 if flow
.is_unreachable
then continue
437 var r2
= flow
.collect_types
(variable
)
438 if r2
== null then continue
443 if not res
.has
(t
) then res
.add
(t
)
448 cache
[variable
] = res
454 # The entry point of the whole typing analysis
455 fun do_typing
(modelbuilder
: ModelBuilder)
459 # The variable associated to the reciever (if any)
460 var selfvariable
: nullable Variable
463 redef class AConcreteMethPropdef
464 redef fun do_typing
(modelbuilder
: ModelBuilder)
466 var nclassdef
= self.parent
.as(AClassdef)
467 var mpropdef
= self.mpropdef
.as(not null)
468 var v
= new TypeVisitor(modelbuilder
, nclassdef
, mpropdef
)
469 self.selfvariable
= v
.selfvariable
471 var nblock
= self.n_block
472 if nblock
== null then return
474 var mmethoddef
= self.mpropdef
.as(not null)
475 for i
in [0..mmethoddef
.msignature
.arity
[ do
476 var mtype
= mmethoddef
.msignature
.mparameters
[i
].mtype
477 if mmethoddef
.msignature
.vararg_rank
== i
then
478 var arrayclass
= v
.get_mclass
(self.n_signature
.n_params
[i
], "Array")
479 if arrayclass
== null then return # Skip error
480 mtype
= arrayclass
.get_mtype
([mtype
])
482 var variable
= self.n_signature
.n_params
[i
].variable
483 assert variable
!= null
484 variable
.declared_type
= mtype
486 for i
in [0..mmethoddef
.msignature
.mclosures
.length
[ do
487 var mclosure
= mmethoddef
.msignature
.mclosures
[i
]
488 var variable
= self.n_signature
.n_closure_decls
[i
].variable
489 assert variable
!= null
490 variable
.declared_type
= mclosure
.mtype
494 if not nblock
.after_flow_context
.is_unreachable
and mmethoddef
.msignature
.return_mtype
!= null then
495 # We reach the end of the function without having a return, it is bad
496 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
501 redef class AAttrPropdef
502 redef fun do_typing
(modelbuilder
: ModelBuilder)
504 var nclassdef
= self.parent
.as(AClassdef)
505 var v
= new TypeVisitor(modelbuilder
, nclassdef
, self.mpropdef
.as(not null))
506 self.selfvariable
= v
.selfvariable
508 var nexpr
= self.n_expr
509 if nexpr
!= null then
510 var mtype
= self.mpropdef
.static_mtype
511 v
.visit_expr_subtype
(nexpr
, mtype
)
519 # The static type of the expression.
520 # null if self is a statement of in case of error
521 var mtype
: nullable MType = null
523 # Is the statement correctly typed?
524 # Used to distinguish errors and statements when `mtype' == null
525 var is_typed
: Bool = false
527 # If required, the following implicit cast ".as(XXX)"
528 # Such a cast may by required after evaluating the expression when
529 # a unsafe operation is detected (silently accepted by the Nit language).
530 # The attribute is computed by `check_subtype`
531 var implicit_cast_to
: nullable MType = null
533 # Return the variable read (if any)
534 # Used to perform adaptive typing
535 fun its_variable
: nullable Variable do return null
537 private fun accept_typing
(v
: TypeVisitor)
539 v
.error
(self, "no implemented accept_typing for {self.class_name}")
543 redef class ABlockExpr
544 redef fun accept_typing
(v
)
546 for e
in self.n_expr
do v
.visit_stmt
(e
)
551 redef class AVardeclExpr
552 redef fun accept_typing
(v
)
554 var variable
= self.variable
555 if variable
== null then return # Skip error
557 var ntype
= self.n_type
558 var mtype
: nullable MType
559 if ntype
== null then
562 mtype
= v
.resolve_mtype
(ntype
)
563 if mtype
== null then return # Skip error
566 var nexpr
= self.n_expr
567 if nexpr
!= null then
568 if mtype
!= null then
569 v
.visit_expr_subtype
(nexpr
, mtype
)
571 mtype
= v
.visit_expr
(nexpr
)
572 if mtype
== null then return # Skip error
576 if mtype
== null then
577 mtype
= v
.get_mclass
(self, "Object").mclass_type
.as_nullable
580 variable
.declared_type
= mtype
581 v
.set_variable
(self, variable
, mtype
)
583 #debug("var {variable}: {mtype}")
590 redef fun its_variable
do return self.variable
591 redef fun accept_typing
(v
)
593 var variable
= self.variable
594 if variable
== null then return # Skip error
596 var mtype
= v
.get_variable
(self, variable
)
597 if mtype
!= null then
598 #debug("{variable} is {mtype}")
600 #debug("{variable} is untyped")
607 redef class AVarAssignExpr
608 redef fun accept_typing
(v
)
610 var variable
= self.variable
611 assert variable
!= null
613 var mtype
= v
.visit_expr_subtype
(n_value
, variable
.declared_type
)
615 v
.set_variable
(self, variable
, mtype
)
621 redef class AReassignFormExpr
622 # @depreciated use `reassign_callsite`
623 fun reassign_property
: nullable MMethodDef do return self.reassign_callsite
.mpropdef
625 # The method designed by the reassign operator.
626 var reassign_callsite
: nullable CallSite
628 var read_type
: nullable MType = null
630 # Determine the `reassign_property'
631 # `readtype' is the type of the reading of the left value.
632 # `writetype' is the type of the writing of the left value.
633 # (Because of ACallReassignExpr, both can be different.
634 # Return the static type of the value to store.
635 private fun resolve_reassignment
(v
: TypeVisitor, readtype
, writetype
: MType): nullable MType
637 var reassign_name
: String
638 if self.n_assign_op
isa APlusAssignOp then
640 else if self.n_assign_op
isa AMinusAssignOp then
646 self.read_type
= readtype
648 if readtype
isa MNullType then
649 v
.error
(self, "Error: Method '{reassign_name}' call on 'null'.")
653 var callsite
= v
.get_method
(self, readtype
, reassign_name
, false)
654 if callsite
== null then return null # Skip error
655 self.reassign_callsite
= callsite
657 var msignature
= callsite
.msignature
658 var rettype
= msignature
.return_mtype
659 assert msignature
.arity
== 1 and rettype
!= null
661 var value_type
= v
.visit_expr_subtype
(self.n_value
, msignature
.mparameters
.first
.mtype
)
662 if value_type
== null then return null # Skip error
664 v
.check_subtype
(self, rettype
, writetype
)
669 redef class AVarReassignExpr
670 redef fun accept_typing
(v
)
672 var variable
= self.variable
673 assert variable
!= null
675 var readtype
= v
.get_variable
(self, variable
)
676 if readtype
== null then return
678 var writetype
= variable
.declared_type
679 if writetype
== null then return
681 var rettype
= self.resolve_reassignment
(v
, readtype
, writetype
)
683 v
.set_variable
(self, variable
, rettype
)
690 redef class AContinueExpr
691 redef fun accept_typing
(v
)
693 var nexpr
= self.n_expr
694 if nexpr
!= null then
695 var mtype
= v
.visit_expr
(nexpr
)
701 redef class ABreakExpr
702 redef fun accept_typing
(v
)
704 var nexpr
= self.n_expr
705 if nexpr
!= null then
706 var mtype
= v
.visit_expr
(nexpr
)
712 redef class AReturnExpr
713 redef fun accept_typing
(v
)
715 var nexpr
= self.n_expr
716 var ret_type
= v
.mpropdef
.as(MMethodDef).msignature
.return_mtype
717 if nexpr
!= null then
718 if ret_type
!= null then
719 var mtype
= v
.visit_expr_subtype
(nexpr
, ret_type
)
721 var mtype
= v
.visit_expr
(nexpr
)
722 v
.error
(self, "Error: Return with value in a procedure.")
724 else if ret_type
!= null then
725 v
.error
(self, "Error: Return without value in a function.")
731 redef class AAbortExpr
732 redef fun accept_typing
(v
)
739 redef fun accept_typing
(v
)
741 v
.visit_expr_bool
(n_expr
)
749 redef class AIfexprExpr
750 redef fun accept_typing
(v
)
752 v
.visit_expr_bool
(n_expr
)
754 var t1
= v
.visit_expr
(n_then
)
755 var t2
= v
.visit_expr
(n_else
)
757 if t1
== null or t2
== null then
761 var t
= v
.merge_types
(self, [t1
, t2
])
763 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
770 redef fun accept_typing
(v
)
772 v
.visit_stmt
(n_block
)
777 redef class AWhileExpr
778 redef fun accept_typing
(v
)
780 v
.visit_expr_bool
(n_expr
)
782 v
.visit_stmt
(n_block
)
787 redef class ALoopExpr
788 redef fun accept_typing
(v
)
790 v
.visit_stmt
(n_block
)
796 var coltype
: nullable MClassType
798 var method_iterator
: nullable MMethod
799 var method_is_ok
: nullable MMethod
800 var method_item
: nullable MMethod
801 var method_next
: nullable MMethod
802 var method_key
: nullable MMethod
804 private fun do_type_iterator
(v
: TypeVisitor, mtype
: MType)
806 var objcla
= v
.get_mclass
(self, "Object")
807 if objcla
== null then return
812 var colcla
= v
.try_get_mclass
(self, "Collection")
813 if colcla
!= null and v
.is_subtype
(mtype
, colcla
.get_mtype
([objcla
.mclass_type
.as_nullable
])) then
814 var coltype
= mtype
.supertype_to
(v
.mmodule
, v
.anchor
, colcla
)
815 self.coltype
= coltype
816 var variables
= self.variables
817 if variables
.length
!= 1 then
818 v
.error
(self, "Type Error: Expected one variable")
820 variables
.first
.declared_type
= coltype
.arguments
.first
825 var mapcla
= v
.try_get_mclass
(self, "Map")
826 if mapcla
!= null and v
.is_subtype
(mtype
, mapcla
.get_mtype
([objcla
.mclass_type
.as_nullable
, objcla
.mclass_type
.as_nullable
])) then
827 var coltype
= mtype
.supertype_to
(v
.mmodule
, v
.anchor
, mapcla
)
828 self.coltype
= coltype
829 var variables
= self.variables
830 if variables
.length
!= 2 then
831 v
.error
(self, "Type Error: Expected two variables")
833 variables
[0].declared_type
= coltype
.arguments
[0]
834 variables
[1].declared_type
= coltype
.arguments
[1]
839 if is_col
or is_map
then
840 # get iterator method
841 var coltype
= self.coltype
.as(not null)
842 var itdef
= v
.get_method
(self, coltype
, "iterator", true)
843 if itdef
== null then
844 v
.error
(self, "Type Error: Expected method 'iterator' in type {coltype}")
847 self.method_iterator
= itdef
.mproperty
850 var ittype
= itdef
.msignature
.return_mtype
851 if ittype
== null then
852 v
.error
(self, "Type Error: Expected method 'iterator' to return an Iterator type")
856 # get methods is_ok, next, item
857 var ikdef
= v
.get_method
(self, ittype
, "is_ok", false)
858 if ikdef
== null then
859 v
.error
(self, "Type Error: Expected method 'is_ok' in Iterator type {ittype}")
862 self.method_is_ok
= ikdef
.mproperty
864 var itemdef
= v
.get_method
(self, ittype
, "item", false)
865 if itemdef
== null then
866 v
.error
(self, "Type Error: Expected method 'item' in Iterator type {ittype}")
869 self.method_item
= itemdef
.mproperty
871 var nextdef
= v
.get_method
(self, ittype
, "next", false)
872 if nextdef
== null then
873 v
.error
(self, "Type Error: Expected method 'next' in Iterator type {ittype}")
876 self.method_next
= nextdef
.mproperty
879 var keydef
= v
.get_method
(self, ittype
, "key", false)
880 if keydef
== null then
881 v
.error
(self, "Type Error: Expected method 'key' in Iterator type {ittype}")
884 self.method_key
= keydef
.mproperty
889 v
.modelbuilder
.error
(self, "NOT YET IMPLEMENTED: Do 'for' on {mtype}")
892 redef fun accept_typing
(v
)
894 var mtype
= v
.visit_expr
(n_expr
)
895 if mtype
== null then return
897 self.do_type_iterator
(v
, mtype
)
899 v
.visit_stmt
(n_block
)
904 redef class AAssertExpr
905 redef fun accept_typing
(v
)
907 v
.visit_expr_bool
(n_expr
)
915 redef fun accept_typing
(v
)
917 v
.visit_expr_bool
(n_expr
)
918 v
.visit_expr_bool
(n_expr2
)
919 self.mtype
= v
.type_bool
(self)
924 redef fun accept_typing
(v
)
926 v
.visit_expr_bool
(n_expr
)
927 v
.visit_expr_bool
(n_expr2
)
928 self.mtype
= v
.type_bool
(self)
934 redef fun accept_typing
(v
)
936 v
.visit_expr_bool
(n_expr
)
937 self.mtype
= v
.type_bool
(self)
941 redef class AOrElseExpr
942 redef fun accept_typing
(v
)
944 var t1
= v
.visit_expr
(n_expr
)
945 var t2
= v
.visit_expr
(n_expr2
)
947 if t1
== null or t2
== null then
951 if t1
isa MNullableType then
955 var t
= v
.merge_types
(self, [t1
, t2
])
957 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
964 redef fun accept_typing
(v
)
967 v
.visit_expr
(n_expr2
)
968 self.mtype
= v
.type_bool
(self)
972 redef class ATrueExpr
973 redef fun accept_typing
(v
)
975 self.mtype
= v
.type_bool
(self)
979 redef class AFalseExpr
980 redef fun accept_typing
(v
)
982 self.mtype
= v
.type_bool
(self)
987 redef fun accept_typing
(v
)
989 var mclass
= v
.get_mclass
(self, "Int")
990 if mclass
== null then return # Forward error
991 self.mtype
= mclass
.mclass_type
995 redef class AFloatExpr
996 redef fun accept_typing
(v
)
998 var mclass
= v
.get_mclass
(self, "Float")
999 if mclass
== null then return # Forward error
1000 self.mtype
= mclass
.mclass_type
1004 redef class ACharExpr
1005 redef fun accept_typing
(v
)
1007 var mclass
= v
.get_mclass
(self, "Char")
1008 if mclass
== null then return # Forward error
1009 self.mtype
= mclass
.mclass_type
1013 redef class AStringFormExpr
1014 redef fun accept_typing
(v
)
1016 var mclass
= v
.get_mclass
(self, "String")
1017 if mclass
== null then return # Forward error
1018 self.mtype
= mclass
.mclass_type
1022 redef class ASuperstringExpr
1023 redef fun accept_typing
(v
)
1025 var mclass
= v
.get_mclass
(self, "String")
1026 if mclass
== null then return # Forward error
1027 self.mtype
= mclass
.mclass_type
1028 for nexpr
in self.n_exprs
do
1029 var t
= v
.visit_expr
(nexpr
)
1034 redef class AArrayExpr
1035 redef fun accept_typing
(v
)
1037 var mtypes
= new Array[nullable MType]
1038 for e
in self.n_exprs
.n_exprs
do
1039 var t
= v
.visit_expr
(e
)
1045 var mtype
= v
.merge_types
(self, mtypes
)
1046 if mtype
== null then
1047 v
.error
(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
1050 var mclass
= v
.get_mclass
(self, "Array")
1051 if mclass
== null then return # Forward error
1052 self.mtype
= mclass
.get_mtype
([mtype
])
1056 redef class ARangeExpr
1057 redef fun accept_typing
(v
)
1059 var discrete_class
= v
.get_mclass
(self, "Discrete")
1060 if discrete_class
== null then return # Forward error
1061 var discrete_type
= discrete_class
.intro
.bound_mtype
1062 var t1
= v
.visit_expr_subtype
(self.n_expr
, discrete_type
)
1063 var t2
= v
.visit_expr_subtype
(self.n_expr2
, discrete_type
)
1064 if t1
== null or t2
== null then return
1065 var mclass
= v
.get_mclass
(self, "Range")
1066 if mclass
== null then return # Forward error
1067 if v
.is_subtype
(t1
, t2
) then
1068 self.mtype
= mclass
.get_mtype
([t2
])
1069 else if v
.is_subtype
(t2
, t1
) then
1070 self.mtype
= mclass
.get_mtype
([t1
])
1072 v
.error
(self, "Type Error: Cannot create range: {t1} vs {t2}")
1077 redef class ANullExpr
1078 redef fun accept_typing
(v
)
1080 self.mtype
= v
.mmodule
.model
.null_type
1084 redef class AIsaExpr
1085 # The static type to cast to.
1086 # (different from the static type of the expression that is Bool).
1087 var cast_type
: nullable MType
1088 redef fun accept_typing
(v
)
1090 var mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
1091 self.cast_type
= mtype
1093 var variable
= self.n_expr
.its_variable
1094 if variable
!= null then
1095 var orig
= self.n_expr
.mtype
1096 var from
= if orig
!= null then orig
.to_s
else "invalid"
1097 var to
= if mtype
!= null then mtype
.to_s
else "invalid"
1098 #debug("adapt {variable}: {from} -> {to}")
1099 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1102 self.mtype
= v
.type_bool
(self)
1106 redef class AAsCastExpr
1107 redef fun accept_typing
(v
)
1109 self.mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
1113 redef class AAsNotnullExpr
1114 redef fun accept_typing
(v
)
1116 var mtype
= v
.visit_expr
(self.n_expr
)
1117 if mtype
isa MNullType then
1118 v
.error
(self, "Type error: as(not null) on null")
1121 if mtype
isa MNullableType then
1122 self.mtype
= mtype
.mtype
1125 # TODO: warn on useless as not null
1130 redef class AProxyExpr
1131 redef fun accept_typing
(v
)
1133 self.mtype
= v
.visit_expr
(self.n_expr
)
1137 redef class ASelfExpr
1138 redef var its_variable
: nullable Variable
1139 redef fun accept_typing
(v
)
1141 var variable
= v
.selfvariable
1142 self.its_variable
= variable
1143 self.mtype
= v
.get_variable
(self, variable
)
1147 ## MESSAGE SENDING AND PROPERTY
1149 redef class ASendExpr
1150 # @depreciated: use `callsite`
1151 fun mproperty
: nullable MMethod do return callsite
.mproperty
1153 # The property invoked by the send.
1154 var callsite
: nullable CallSite
1156 redef fun accept_typing
(v
)
1158 var recvtype
= v
.visit_expr
(self.n_expr
)
1159 var name
= self.property_name
1161 if recvtype
== null then return # Forward error
1162 if recvtype
isa MNullType then
1163 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1167 var callsite
= v
.get_method
(self, recvtype
, name
, self.n_expr
isa ASelfExpr)
1168 if callsite
== null then return
1169 self.callsite
= callsite
1170 var msignature
= callsite
.msignature
1172 var args
= compute_raw_arguments
1173 self.raw_arguments
= args
1175 callsite
.check_signature
(v
, args
)
1177 if callsite
.mproperty
.is_init
then
1178 var vmpropdef
= v
.mpropdef
1179 if not (vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_init
) then
1180 v
.error
(self, "Can call a init only in another init")
1184 var ret
= msignature
.return_mtype
1188 self.is_typed
= true
1191 if self.n_closure_defs
.length
== msignature
.mclosures
.length
then
1192 for i
in [0..self.n_closure_defs
.length
[ do
1193 self.n_closure_defs
[i
].accept_typing
(v
, msignature
.mclosures
[i
])
1196 debug
("closure: got {self.n_closure_defs.length}, want {msignature.mclosures.length}")
1200 # The name of the property
1201 # Each subclass simply provide the correct name.
1202 private fun property_name
: String is abstract
1204 # An array of all arguments (excluding self)
1205 var raw_arguments
: nullable Array[AExpr]
1207 private fun compute_raw_arguments
: Array[AExpr] is abstract
1210 redef class ABinopExpr
1211 redef fun compute_raw_arguments
do return [n_expr2
]
1214 redef fun property_name
do return "=="
1215 redef fun accept_typing
(v
)
1219 var variable
= self.n_expr
.its_variable
1220 if variable
== null then return
1221 var mtype
= self.n_expr2
.mtype
1222 if not mtype
isa MNullType then return
1223 var vartype
= v
.get_variable
(self, variable
)
1224 if not vartype
isa MNullableType then return
1225 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1226 self.after_flow_context
.when_false
.set_var
(variable
, vartype
.mtype
)
1227 #debug("adapt {variable}:{vartype} ; true->{mtype} false->{vartype.mtype}")
1231 redef fun property_name
do return "!="
1232 redef fun accept_typing
(v
)
1236 var variable
= self.n_expr
.its_variable
1237 if variable
== null then return
1238 var mtype
= self.n_expr2
.mtype
1239 if not mtype
isa MNullType then return
1240 var vartype
= v
.get_variable
(self, variable
)
1241 if not vartype
isa MNullableType then return
1242 self.after_flow_context
.when_false
.set_var
(variable
, mtype
)
1243 self.after_flow_context
.when_true
.set_var
(variable
, vartype
.mtype
)
1244 #debug("adapt {variable}:{vartype} ; true->{vartype.mtype} false->{mtype}")
1248 redef fun property_name
do return "<"
1251 redef fun property_name
do return "<="
1254 redef fun property_name
do return "<<"
1257 redef fun property_name
do return ">"
1260 redef fun property_name
do return ">="
1263 redef fun property_name
do return ">>"
1265 redef class APlusExpr
1266 redef fun property_name
do return "+"
1268 redef class AMinusExpr
1269 redef fun property_name
do return "-"
1271 redef class AStarshipExpr
1272 redef fun property_name
do return "<=>"
1274 redef class AStarExpr
1275 redef fun property_name
do return "*"
1277 redef class ASlashExpr
1278 redef fun property_name
do return "/"
1280 redef class APercentExpr
1281 redef fun property_name
do return "%"
1284 redef class AUminusExpr
1285 redef fun property_name
do return "unary -"
1286 redef fun compute_raw_arguments
do return new Array[AExpr]
1290 redef class ACallExpr
1291 redef fun property_name
do return n_id
.text
1292 redef fun compute_raw_arguments
do return n_args
.to_a
1295 redef class ACallAssignExpr
1296 redef fun property_name
do return n_id
.text
+ "="
1297 redef fun compute_raw_arguments
1299 var res
= n_args
.to_a
1305 redef class ABraExpr
1306 redef fun property_name
do return "[]"
1307 redef fun compute_raw_arguments
do return n_args
.to_a
1310 redef class ABraAssignExpr
1311 redef fun property_name
do return "[]="
1312 redef fun compute_raw_arguments
1314 var res
= n_args
.to_a
1320 redef class ASendReassignFormExpr
1321 # @depreciated use `write_callsite`
1322 fun write_mproperty
: nullable MMethod do return write_callsite
.mproperty
1324 # The property invoked for the writing
1325 var write_callsite
: nullable CallSite
1327 redef fun accept_typing
(v
)
1329 var recvtype
= v
.visit_expr
(self.n_expr
)
1330 var name
= self.property_name
1332 if recvtype
== null then return # Forward error
1333 if recvtype
isa MNullType then
1334 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1338 var for_self
= self.n_expr
isa ASelfExpr
1339 var callsite
= v
.get_method
(self, recvtype
, name
, for_self
)
1341 if callsite
== null then return
1342 self.callsite
= callsite
1344 var args
= compute_raw_arguments
1345 self.raw_arguments
= args
1347 callsite
.check_signature
(v
, args
)
1349 var readtype
= callsite
.msignature
.return_mtype
1350 if readtype
== null then
1351 v
.error
(self, "Error: {name} is not a function")
1355 var wcallsite
= v
.get_method
(self, recvtype
, name
+ "=", self.n_expr
isa ASelfExpr)
1356 if wcallsite
== null then return
1357 self.write_callsite
= wcallsite
1359 var wtype
= self.resolve_reassignment
(v
, readtype
, wcallsite
.msignature
.mparameters
.last
.mtype
)
1360 if wtype
== null then return
1362 args
= args
.to_a
# duplicate so raw_arguments keeps only the getter args
1363 args
.add
(self.n_value
)
1364 wcallsite
.check_signature
(v
, args
)
1366 self.is_typed
= true
1370 redef class ACallReassignExpr
1371 redef fun property_name
do return n_id
.text
1372 redef fun compute_raw_arguments
do return n_args
.to_a
1375 redef class ABraReassignExpr
1376 redef fun property_name
do return "[]"
1377 redef fun compute_raw_arguments
do return n_args
.to_a
1380 redef class AInitExpr
1381 redef fun property_name
do return "init"
1382 redef fun compute_raw_arguments
do return n_args
.to_a
1386 fun to_a
: Array[AExpr] do return self.n_exprs
.to_a
1391 redef class ASuperExpr
1392 # The method to call if the super is in fact a 'super init call'
1393 # Note: if the super is a normal call-next-method, then this attribute is null
1394 var mproperty
: nullable MMethod
1396 redef fun accept_typing
(v
)
1398 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1399 var mproperty
= v
.mpropdef
.mproperty
1400 if not mproperty
isa MMethod then
1401 v
.error
(self, "Error: super only usable in a method")
1404 var superprops
= mproperty
.lookup_super_definitions
(v
.mmodule
, recvtype
)
1405 if superprops
.length
== 0 then
1406 if mproperty
.is_init
and v
.mpropdef
.is_intro
then
1407 process_superinit
(v
)
1410 v
.error
(self, "Error: No super method to call for {mproperty}.")
1412 else if superprops
.length
> 1 then
1413 v
.modelbuilder
.warning
(self, "Warning: NOT YET IMPLEMENTED: Conflicting super method to call for {mproperty}: {superprops.join(", ")}.")
1416 var superprop
= superprops
.first
1417 assert superprop
isa MMethodDef
1419 var msignature
= v
.resolve_signature_for
(superprop
, recvtype
, true)
1420 var args
= self.n_args
.to_a
1421 if args
.length
> 0 then
1422 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1424 self.mtype
= msignature
.return_mtype
1427 private fun process_superinit
(v
: TypeVisitor)
1429 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1430 var mproperty
= v
.mpropdef
.mproperty
1431 var superprop
: nullable MMethodDef = null
1432 for msupertype
in v
.nclassdef
.mclassdef
.supertypes
do
1433 msupertype
= msupertype
.anchor_to
(v
.mmodule
, recvtype
)
1434 var errcount
= v
.modelbuilder
.toolcontext
.error_count
1435 var candidate
= v
.try_get_mproperty_by_name2
(self, msupertype
, mproperty
.name
).as(nullable MMethod)
1436 if candidate
== null then
1437 if v
.modelbuilder
.toolcontext
.error_count
> errcount
then return # Forard error
1438 continue # Try next super-class
1440 if superprop
!= null and superprop
.mproperty
!= candidate
then
1441 v
.error
(self, "Error: conflicting super constructor to call for {mproperty}: {candidate.full_name}, {superprop.mproperty.full_name}")
1444 var candidatedefs
= candidate
.lookup_definitions
(v
.mmodule
, recvtype
)
1445 if superprop
!= null then
1446 if superprop
== candidatedefs
.first
then continue
1447 candidatedefs
.add
(superprop
)
1449 if candidatedefs
.length
> 1 then
1450 v
.error
(self, "Error: confliting property definitions for property {mproperty} in {recvtype}: {candidatedefs.join(", ")}")
1453 superprop
= candidatedefs
.first
1455 if superprop
== null then
1456 v
.error
(self, "Error: No super method to call for {mproperty}.")
1459 self.mproperty
= superprop
.mproperty
1461 var args
= self.n_args
.to_a
1462 var msignature
= v
.resolve_signature_for
(superprop
, recvtype
, true)
1463 if args
.length
> 0 then
1464 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1466 # TODO: Check signature
1469 self.is_typed
= true
1475 redef class ANewExpr
1476 # @depreciated use `callsite`
1477 fun mproperty
: nullable MMethod do return self.callsite
.mproperty
1479 # The constructor invoked by the new.
1480 var callsite
: nullable CallSite
1482 redef fun accept_typing
(v
)
1484 var recvtype
= v
.resolve_mtype
(self.n_type
)
1485 if recvtype
== null then return
1486 self.mtype
= recvtype
1488 if not recvtype
isa MClassType then
1489 if recvtype
isa MNullableType then
1490 v
.error
(self, "Type error: cannot instantiate the nullable type {recvtype}.")
1493 v
.error
(self, "Type error: cannot instantiate the formal type {recvtype}.")
1505 var callsite
= v
.get_method
(self, recvtype
, name
, false)
1506 if callsite
== null then return
1508 self.callsite
= callsite
1510 if not callsite
.mproperty
.is_init_for
(recvtype
.mclass
) then
1511 v
.error
(self, "Error: {name} is not a constructor.")
1515 var args
= n_args
.to_a
1516 callsite
.check_signature
(v
, args
)
1522 redef class AAttrFormExpr
1523 # The attribute acceded.
1524 var mproperty
: nullable MAttribute
1526 # The static type of the attribute.
1527 var attr_type
: nullable MType
1529 # Resolve the attribute acceded.
1530 private fun resolve_property
(v
: TypeVisitor)
1532 var recvtype
= v
.visit_expr
(self.n_expr
)
1533 if recvtype
== null then return # Skip error
1534 var name
= self.n_id
.text
1535 if recvtype
isa MNullType then
1536 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1540 var unsafe_type
= v
.anchor_to
(recvtype
)
1541 var mproperty
= v
.try_get_mproperty_by_name2
(self, unsafe_type
, name
)
1542 if mproperty
== null then
1543 v
.modelbuilder
.error
(self, "Error: Attribute {name} doesn't exists in {recvtype}.")
1546 assert mproperty
isa MAttribute
1547 self.mproperty
= mproperty
1549 var mpropdefs
= mproperty
.lookup_definitions
(v
.mmodule
, unsafe_type
)
1550 assert mpropdefs
.length
== 1
1551 var mpropdef
= mpropdefs
.first
1552 var attr_type
= mpropdef
.static_mtype
.as(not null)
1553 attr_type
= v
.resolve_for
(attr_type
, recvtype
, self.n_expr
isa ASelfExpr)
1554 self.attr_type
= attr_type
1558 redef class AAttrExpr
1559 redef fun accept_typing
(v
)
1561 self.resolve_property
(v
)
1562 self.mtype
= self.attr_type
1567 redef class AAttrAssignExpr
1568 redef fun accept_typing
(v
)
1570 self.resolve_property
(v
)
1571 var mtype
= self.attr_type
1573 v
.visit_expr_subtype
(self.n_value
, mtype
)
1574 self.is_typed
= true
1578 redef class AAttrReassignExpr
1579 redef fun accept_typing
(v
)
1581 self.resolve_property
(v
)
1582 var mtype
= self.attr_type
1583 if mtype
== null then return # Skip error
1585 self.resolve_reassignment
(v
, mtype
, mtype
)
1587 self.is_typed
= true
1591 redef class AIssetAttrExpr
1592 redef fun accept_typing
(v
)
1594 self.resolve_property
(v
)
1595 var mtype
= self.attr_type
1596 if mtype
== null then return # Skip error
1598 var recvtype
= self.n_expr
.mtype
.as(not null)
1599 var bound
= v
.resolve_for
(mtype
, recvtype
, false)
1600 if bound
isa MNullableType then
1601 v
.error
(self, "Error: isset on a nullable attribute.")
1603 self.mtype
= v
.type_bool
(self)
1609 redef class AClosureCallExpr
1610 redef fun accept_typing
(v
)
1612 var variable
= self.variable
1613 if variable
== null then return # Skip error
1615 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1616 var msignature
= variable
.declared_type
.as(not null)
1617 msignature
= v
.resolve_for
(msignature
, recvtype
, false).as(MSignature)
1619 var args
= n_args
.to_a
1620 v
.check_signature
(self, args
, variable
.name
, msignature
)
1622 self.is_typed
= true
1623 self.mtype
= msignature
.return_mtype
1627 redef class AClosureDef
1628 var mclosure
: nullable MParameter
1630 private fun accept_typing
(v
: TypeVisitor, mparameter
: MParameter)
1632 var variables
= self.variables
1633 if variables
== null then return
1635 self.mclosure
= mparameter
1636 var msignature
= mparameter
.mtype
.as(MSignature)
1638 if msignature
.arity
!= variables
.length
then
1639 v
.error
(self, "Type error: closure {mparameter.name} expects {msignature.arity} parameters, {variables.length} given")
1643 for i
in [0..variables
.length
[ do
1644 variables
[i
].declared_type
= msignature
.mparameters
[i
].mtype
1647 v
.visit_stmt
(self.n_expr
)
1653 redef class ADebugTypeExpr
1654 redef fun accept_typing
(v
)
1656 var expr
= v
.visit_expr
(self.n_expr
)
1657 if expr
== null then return
1658 var unsafe
= v
.anchor_to
(expr
)
1659 var ntype
= self.n_type
1660 var mtype
= v
.resolve_mtype
(ntype
)
1661 if mtype
!= null and mtype
!= expr
then
1662 var umtype
= v
.anchor_to
(mtype
)
1663 v
.modelbuilder
.warning
(self, "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")