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 fun resolve_signature_for
(msignature
: MSignature, recv
: MType, for_self
: Bool): MSignature
73 return self.resolve_for
(msignature
, recv
, for_self
).as(MSignature)
76 fun check_subtype
(node
: ANode, sub
, sup
: MType): Bool
78 if self.is_subtype
(sub
, sup
) then return true
79 if self.is_subtype
(sub
, self.anchor_to
(sup
)) then
80 # FIXME workarround to the current unsafe typing policy. To remove once fixed virtual types exists.
81 #node.debug("Unsafe typing: expected {sup}, got {sub}")
84 self.modelbuilder
.error
(node
, "Type error: expected {sup}, got {sub}")
88 # Visit an expression and do not care about the return value
89 fun visit_stmt
(nexpr
: nullable AExpr)
91 if nexpr
== null then return
92 nexpr
.accept_typing
(self)
95 # Visit an expression and expects that it is not a statement
96 # Return the type of the expression
97 # Display an error and return null if:
98 # * the type cannot be determined or
99 # * `nexpr' is a statement
100 fun visit_expr
(nexpr
: AExpr): nullable MType
102 nexpr
.accept_typing
(self)
103 var mtype
= nexpr
.mtype
104 if mtype
!= null then return mtype
105 if not nexpr
.is_typed
then
106 if not self.modelbuilder
.toolcontext
.error_count
> 0 then # check that there is really an error
107 if self.modelbuilder
.toolcontext
.verbose_level
> 1 then
108 nexpr
.debug
("No return type but no error.")
111 return null # forward error
113 self.error
(nexpr
, "Type error: expected expression.")
117 # Visit an expression and expect its static type is a least a `sup'
118 # Return the type of the expression
119 # * the type cannot be determined or
120 # * `nexpr' is a statement
121 # * `nexpt' is not a `sup'
122 fun visit_expr_subtype
(nexpr
: AExpr, sup
: nullable MType): nullable MType
124 var sub
= visit_expr
(nexpr
)
125 if sub
== null then return null # Forward error
127 if sup
== null then return null # Forward error
129 if not check_subtype
(nexpr
, sub
, sup
) then
135 # Visit an expression and expect its static type is a bool
136 # Return the type of the expression
137 # * the type cannot be determined or
138 # * `nexpr' is a statement
139 # * `nexpt' is not a `sup'
140 fun visit_expr_bool
(nexpr
: AExpr): nullable MType
142 return self.visit_expr_subtype
(nexpr
, self.type_bool
(nexpr
))
146 private fun visit_expr_cast
(node
: ANode, nexpr
: AExpr, ntype
: AType): nullable MType
148 var sub
= visit_expr
(nexpr
)
149 if sub
== null then return null # Forward error
151 var sup
= self.resolve_mtype
(ntype
)
152 if sup
== null then return null # Forward error
154 var mmodule
= self.nclassdef
.mclassdef
.mmodule
155 var anchor
= self.nclassdef
.mclassdef
.bound_mtype
157 self.modelbuilder
.warning
(node
, "Warning: Expression is already a {sup}.")
158 else if self.is_subtype
(sub
, sup
) and not sup
.need_anchor
then
159 self.modelbuilder
.warning
(node
, "Warning: Expression is already a {sup} since it is a {sub}.")
164 fun try_get_mproperty_by_name2
(anode
: ANode, mtype
: MType, name
: String): nullable MProperty
166 return self.modelbuilder
.try_get_mproperty_by_name2
(anode
, self.nclassdef
.mclassdef
.mmodule
, mtype
, name
)
169 fun resolve_mtype
(node
: AType): nullable MType
171 return self.modelbuilder
.resolve_mtype
(self.nclassdef
, node
)
174 fun get_mclass
(node
: ANode, name
: String): nullable MClass
176 var mmodule
= self.nclassdef
.mclassdef
.mmodule
177 var mclass
= modelbuilder
.try_get_mclass_by_name
(node
, mmodule
, name
)
178 if mclass
== null then
179 self.modelbuilder
.error
(node
, "Type Error: missing primitive class `{name}'.")
184 fun type_bool
(node
: ANode): nullable MType
186 var mclass
= self.get_mclass
(node
, "Bool")
187 if mclass
== null then return null
188 return mclass
.mclass_type
191 fun get_method
(node
: ANode, recvtype
: MType, name
: String, recv_is_self
: Bool): nullable MMethodDef
193 var unsafe_type
= self.anchor_to
(recvtype
)
195 #debug("recv: {recvtype} (aka {unsafe_type})")
197 var mproperty
= self.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
198 if mproperty
== null then
199 #self.modelbuilder.error(node, "Type error: property {name} not found in {unsafe_type} (ie {recvtype})")
201 self.modelbuilder
.error
(node
, "Error: Method or variable '{name}' unknown in {recvtype}.")
203 self.modelbuilder
.error
(node
, "Error: Method '{name}' doesn't exists in {recvtype}.")
208 var propdefs
= mproperty
.lookup_definitions
(self.mmodule
, unsafe_type
)
209 if propdefs
.length
== 0 then
210 self.modelbuilder
.error
(node
, "Type error: no definition found for property {name} in {unsafe_type}")
212 else if propdefs
.length
> 1 then
213 self.modelbuilder
.error
(node
, "Error: confliting property definitions for property {name} in {unsafe_type}: {propdefs.join(" ")}")
217 var propdef
= propdefs
.first
218 assert propdef
isa MMethodDef
222 # Visit the expressions of args and cheik their conformity with the corresponding typi in signature
223 # The point of this method is to handle varargs correctly
224 # Note: The signature must be correctly adapted
225 fun check_signature
(node
: ANode, args
: Array[AExpr], name
: String, msignature
: MSignature): Bool
227 var vararg_rank
= msignature
.vararg_rank
228 if vararg_rank
>= 0 then
229 if args
.length
< msignature
.arity
then
230 #self.modelbuilder.error(node, "Error: Incorrect number of parameters. Got {args.length}, expected at least {msignature.arity}. Signature is {msignature}")
231 self.modelbuilder
.error
(node
, "Error: arity mismatch; prototype is '{name}{msignature}'")
234 else if args
.length
!= msignature
.arity
then
235 self.modelbuilder
.error
(node
, "Error: Incorrect number of parameters. Got {args.length}, expected {msignature.arity}. Signature is {msignature}")
239 #debug("CALL {unsafe_type}.{msignature}")
241 var vararg_decl
= args
.length
- msignature
.arity
242 for i
in [0..msignature
.arity
[ do
244 if i
== vararg_rank
then continue # skip the vararg
245 if i
> vararg_rank
then
248 var paramtype
= msignature
.parameter_mtypes
[i
]
249 self.visit_expr_subtype
(args
[j
], paramtype
)
251 if vararg_rank
>= 0 then
252 var varargs
= new Array[AExpr]
253 var paramtype
= msignature
.parameter_mtypes
[vararg_rank
]
254 for j
in [vararg_rank
..vararg_rank
+vararg_decl
] do
256 self.visit_expr_subtype
(args
[j
], paramtype
)
262 fun error
(node
: ANode, message
: String)
264 self.modelbuilder
.toolcontext
.error
(node
.hot_location
, message
)
267 fun get_variable
(node
: AExpr, variable
: Variable): nullable MType
269 var flow
= node
.after_flow_context
271 self.error
(node
, "No context!")
275 if flow
.vars
.has_key
(variable
) then
276 return flow
.vars
[variable
]
278 #node.debug("*** START Collected for {variable}")
279 var mtypes
= flow
.collect_types
(variable
)
280 #node.debug("**** END Collected for {variable}")
281 if mtypes
== null or mtypes
.length
== 0 then
282 return variable
.declared_type
283 else if mtypes
.length
== 1 then
286 var res
= merge_types
(node
,mtypes
)
287 if res
== null then res
= variable
.declared_type
293 fun set_variable
(node
: AExpr, variable
: Variable, mtype
: nullable MType)
295 var flow
= node
.after_flow_context
298 flow
.set_var
(variable
, mtype
)
301 fun merge_types
(node
: ANode, col
: Array[nullable MType]): nullable MType
303 if col
.length
== 1 then return col
.first
304 var res
= new Array[nullable MType]
306 if t1
== null then continue # return null
309 if t2
== null then continue # return null
310 if t2
isa MNullableType or t2
isa MNullType then
313 if not is_subtype
(t2
, t1
) then found
= false
316 #print "merge {col.join(" ")} -> {t1}"
320 #self.modelbuilder.warning(node, "Type Error: {col.length} conflicting types: <{col.join(", ")}>")
326 # The declared type of the variable
327 var declared_type
: nullable MType
330 redef class FlowContext
331 # Store changes of types because of type evolution
332 private var vars
: HashMap[Variable, nullable MType] = new HashMap[Variable, nullable MType]
333 private var cache
: HashMap[Variable, nullable Array[nullable MType]] = new HashMap[Variable, nullable Array[nullable MType]]
335 # Adapt the variable to a static type
336 # Warning1: do not modify vars directly.
337 # Warning2: sub-flow may have cached a unadapted variabial
338 private fun set_var
(variable
: Variable, mtype
: nullable MType)
340 self.vars
[variable
] = mtype
341 self.cache
.keys
.remove
(variable
)
344 private fun collect_types
(variable
: Variable): nullable Array[nullable MType]
346 if cache
.has_key
(variable
) then
347 return cache
[variable
]
349 var res
: nullable Array[nullable MType] = null
350 if vars
.has_key
(variable
) then
351 var mtype
= vars
[variable
]
353 else if self.previous
.is_empty
then
355 res
= [variable
.declared_type
]
357 for flow
in self.previous
do
358 if flow
.is_unreachable
then continue
359 var r2
= flow
.collect_types
(variable
)
360 if r2
== null then continue
365 if not res
.has
(t
) then res
.add
(t
)
370 cache
[variable
] = res
376 # The entry point of the whole typing analysis
377 fun do_typing
(modelbuilder
: ModelBuilder)
382 redef class AConcreteMethPropdef
383 redef fun do_typing
(modelbuilder
: ModelBuilder)
385 var nclassdef
= self.parent
.as(AClassdef)
386 var mpropdef
= self.mpropdef
.as(not null)
387 var v
= new TypeVisitor(modelbuilder
, nclassdef
, mpropdef
)
389 var nblock
= self.n_block
390 if nblock
== null then return
392 var mmethoddef
= self.mpropdef
.as(not null)
393 for i
in [0..mmethoddef
.msignature
.arity
[ do
394 var mtype
= mmethoddef
.msignature
.parameter_mtypes
[i
]
395 if mmethoddef
.msignature
.vararg_rank
== i
then
396 var arrayclass
= v
.get_mclass
(self.n_signature
.n_params
[i
], "Array")
397 if arrayclass
== null then return # Skip error
398 mtype
= arrayclass
.get_mtype
([mtype
])
400 var variable
= self.n_signature
.n_params
[i
].variable
401 assert variable
!= null
402 variable
.declared_type
= mtype
406 if not nblock
.after_flow_context
.is_unreachable
and mmethoddef
.msignature
.return_mtype
!= null then
407 # We reach the end of the function without having a return, it is bad
408 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
413 redef class AAttrPropdef
414 redef fun do_typing
(modelbuilder
: ModelBuilder)
416 var nclassdef
= self.parent
.as(AClassdef)
417 var v
= new TypeVisitor(modelbuilder
, nclassdef
, self.mpropdef
.as(not null))
419 var nexpr
= self.n_expr
420 if nexpr
!= null then
421 var mtype
= self.mpropdef
.static_mtype
422 v
.visit_expr_subtype
(nexpr
, mtype
)
430 # The static type of the expression.
431 # null if self is a statement of in case of error
432 var mtype
: nullable MType = null
434 # Is the statement correctly typed?
435 # Used to distinguish errors and statements when `mtype' == null
436 var is_typed
: Bool = false
438 # Return the variable read (if any)
439 # Used to perform adaptive typing
440 fun its_variable
: nullable Variable do return null
442 private fun accept_typing
(v
: TypeVisitor)
444 v
.error
(self, "no implemented accept_typing for {self.class_name}")
448 redef class ABlockExpr
449 redef fun accept_typing
(v
)
451 for e
in self.n_expr
do v
.visit_stmt
(e
)
456 redef class AVardeclExpr
457 redef fun accept_typing
(v
)
459 var variable
= self.variable
460 if variable
== null then return # Skip error
462 var ntype
= self.n_type
463 var mtype
: nullable MType
464 if ntype
== null then
467 mtype
= v
.resolve_mtype
(ntype
)
468 if mtype
== null then return # Skip error
471 var nexpr
= self.n_expr
472 if nexpr
!= null then
473 if mtype
!= null then
474 v
.visit_expr_subtype
(nexpr
, mtype
)
476 mtype
= v
.visit_expr
(nexpr
)
477 if mtype
== null then return # Skip error
481 if mtype
== null then
482 mtype
= v
.get_mclass
(self, "Object").mclass_type
485 variable
.declared_type
= mtype
486 v
.set_variable
(self, variable
, mtype
)
488 #debug("var {variable}: {mtype}")
495 redef fun its_variable
do return self.variable
496 redef fun accept_typing
(v
)
498 var variable
= self.variable
499 if variable
== null then return # Skip error
501 var mtype
= v
.get_variable
(self, variable
)
502 if mtype
!= null then
503 #debug("{variable} is {mtype}")
505 #debug("{variable} is untyped")
512 redef class AVarAssignExpr
513 redef fun accept_typing
(v
)
515 var variable
= self.variable
516 assert variable
!= null
518 var mtype
= v
.visit_expr_subtype
(n_value
, variable
.declared_type
)
520 v
.set_variable
(self, variable
, mtype
)
526 redef class AReassignFormExpr
527 # The method designed by the reassign operator.
528 var reassign_property
: nullable MMethodDef = null
530 var read_type
: nullable MType = null
532 # Determine the `reassign_property'
533 # `readtype' is the type of the reading of the left value.
534 # `writetype' is the type of the writing of the left value.
535 # (Because of ACallReassignExpr, both can be different.
536 # Return the static type of the value to store.
537 private fun resolve_reassignment
(v
: TypeVisitor, readtype
, writetype
: MType): nullable MType
539 var reassign_name
: String
540 if self.n_assign_op
isa APlusAssignOp then
542 else if self.n_assign_op
isa AMinusAssignOp then
548 self.read_type
= readtype
550 if readtype
isa MNullType then
551 v
.error
(self, "Error: Method '{reassign_name}' call on 'null'.")
555 var mpropdef
= v
.get_method
(self, readtype
, reassign_name
, false)
556 if mpropdef
== null then return null # Skip error
558 self.reassign_property
= mpropdef
560 var msignature
= mpropdef
.msignature
561 assert msignature
!= null
562 msignature
= v
.resolve_signature_for
(msignature
, readtype
, false)
564 var rettype
= msignature
.return_mtype
565 assert msignature
.arity
== 1 and rettype
!= null
567 var value_type
= v
.visit_expr_subtype
(self.n_value
, msignature
.parameter_mtypes
.first
)
568 if value_type
== null then return null # Skip error
570 v
.check_subtype
(self, rettype
, writetype
)
575 redef class AVarReassignExpr
576 redef fun accept_typing
(v
)
578 var variable
= self.variable
579 assert variable
!= null
581 var readtype
= v
.get_variable
(self, variable
)
582 if readtype
== null then return
584 var writetype
= variable
.declared_type
585 if writetype
== null then return
587 var rettype
= self.resolve_reassignment
(v
, readtype
, writetype
)
589 v
.set_variable
(self, variable
, rettype
)
596 redef class AContinueExpr
597 redef fun accept_typing
(v
)
599 var nexpr
= self.n_expr
600 if nexpr
!= null then
601 var mtype
= v
.visit_expr
(nexpr
)
607 redef class ABreakExpr
608 redef fun accept_typing
(v
)
610 var nexpr
= self.n_expr
611 if nexpr
!= null then
612 var mtype
= v
.visit_expr
(nexpr
)
618 redef class AReturnExpr
619 redef fun accept_typing
(v
)
621 var nexpr
= self.n_expr
622 var ret_type
= v
.mpropdef
.as(MMethodDef).msignature
.return_mtype
623 if nexpr
!= null then
624 if ret_type
!= null then
625 var mtype
= v
.visit_expr_subtype
(nexpr
, ret_type
)
627 var mtype
= v
.visit_expr
(nexpr
)
628 v
.error
(self, "Error: Return with value in a procedure.")
630 else if ret_type
!= null then
631 v
.error
(self, "Error: Return without value in a function.")
637 redef class AAbortExpr
638 redef fun accept_typing
(v
)
645 redef fun accept_typing
(v
)
647 v
.visit_expr_bool
(n_expr
)
655 redef class AIfexprExpr
656 redef fun accept_typing
(v
)
658 v
.visit_expr_bool
(n_expr
)
660 var t1
= v
.visit_expr
(n_then
)
661 var t2
= v
.visit_expr
(n_else
)
663 if t1
== null or t2
== null then
667 var t
= v
.merge_types
(self, [t1
, t2
])
669 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
676 redef fun accept_typing
(v
)
678 v
.visit_stmt
(n_block
)
683 redef class AWhileExpr
684 redef fun accept_typing
(v
)
686 v
.visit_expr_bool
(n_expr
)
688 v
.visit_stmt
(n_block
)
693 redef class ALoopExpr
694 redef fun accept_typing
(v
)
696 v
.visit_stmt
(n_block
)
702 redef fun accept_typing
(v
)
704 var mtype
= v
.visit_expr
(n_expr
)
705 if mtype
== null then return
707 var colcla
= v
.get_mclass
(self, "Collection")
708 if colcla
== null then return
709 var objcla
= v
.get_mclass
(self, "Object")
710 if objcla
== null then return
711 if v
.is_subtype
(mtype
, colcla
.get_mtype
([objcla
.mclass_type
.as_nullable
])) then
712 var coltype
= mtype
.supertype_to
(v
.mmodule
, v
.anchor
, colcla
)
713 assert coltype
isa MGenericType
714 var variables
= self.variables
715 if variables
.length
!= 1 then
716 v
.error
(self, "Type Error: Expected one variable")
718 variables
.first
.declared_type
= coltype
.arguments
.first
721 v
.modelbuilder
.error
(self, "TODO: Do 'for' on {mtype}")
724 v
.visit_stmt
(n_block
)
729 redef class AAssertExpr
730 redef fun accept_typing
(v
)
732 v
.visit_expr_bool
(n_expr
)
740 redef fun accept_typing
(v
)
742 v
.visit_expr_bool
(n_expr
)
743 v
.visit_expr_bool
(n_expr2
)
744 self.mtype
= v
.type_bool
(self)
749 redef fun accept_typing
(v
)
751 v
.visit_expr_bool
(n_expr
)
752 v
.visit_expr_bool
(n_expr2
)
753 self.mtype
= v
.type_bool
(self)
759 redef fun accept_typing
(v
)
761 v
.visit_expr_bool
(n_expr
)
762 self.mtype
= v
.type_bool
(self)
766 redef class AOrElseExpr
767 redef fun accept_typing
(v
)
769 var t1
= v
.visit_expr
(n_expr
)
770 var t2
= v
.visit_expr
(n_expr2
)
772 if t1
== null or t2
== null then
776 if t1
isa MNullableType then
780 var t
= v
.merge_types
(self, [t1
, t2
])
782 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
789 redef fun accept_typing
(v
)
792 v
.visit_expr
(n_expr2
)
793 self.mtype
= v
.type_bool
(self)
797 redef class ATrueExpr
798 redef fun accept_typing
(v
)
800 self.mtype
= v
.type_bool
(self)
804 redef class AFalseExpr
805 redef fun accept_typing
(v
)
807 self.mtype
= v
.type_bool
(self)
812 redef fun accept_typing
(v
)
814 var mclass
= v
.get_mclass
(self, "Int")
815 if mclass
== null then return # Forward error
816 self.mtype
= mclass
.mclass_type
820 redef class AFloatExpr
821 redef fun accept_typing
(v
)
823 var mclass
= v
.get_mclass
(self, "Float")
824 if mclass
== null then return # Forward error
825 self.mtype
= mclass
.mclass_type
829 redef class ACharExpr
830 redef fun accept_typing
(v
)
832 var mclass
= v
.get_mclass
(self, "Char")
833 if mclass
== null then return # Forward error
834 self.mtype
= mclass
.mclass_type
838 redef class AStringFormExpr
839 redef fun accept_typing
(v
)
841 var mclass
= v
.get_mclass
(self, "String")
842 if mclass
== null then return # Forward error
843 self.mtype
= mclass
.mclass_type
847 redef class ASuperstringExpr
848 redef fun accept_typing
(v
)
850 var mclass
= v
.get_mclass
(self, "String")
851 if mclass
== null then return # Forward error
852 self.mtype
= mclass
.mclass_type
853 for nexpr
in self.n_exprs
do
854 var t
= v
.visit_expr
(nexpr
)
859 redef class AArrayExpr
860 redef fun accept_typing
(v
)
862 var mtypes
= new Array[nullable MType]
863 for e
in self.n_exprs
.n_exprs
do
864 var t
= v
.visit_expr
(e
)
870 var mtype
= v
.merge_types
(self, mtypes
)
871 if mtype
== null then
872 v
.error
(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
875 var mclass
= v
.get_mclass
(self, "Array")
876 if mclass
== null then return # Forward error
877 self.mtype
= mclass
.get_mtype
([mtype
])
881 redef class ARangeExpr
882 redef fun accept_typing
(v
)
884 var t1
= v
.visit_expr
(self.n_expr
)
885 var t2
= v
.visit_expr
(self.n_expr2
)
886 if t1
== null or t2
== null then return
887 var mclass
= v
.get_mclass
(self, "Range")
888 if mclass
== null then return # Forward error
889 if v
.is_subtype
(t1
, t2
) then
890 self.mtype
= mclass
.get_mtype
([t2
])
891 else if v
.is_subtype
(t2
, t1
) then
892 self.mtype
= mclass
.get_mtype
([t1
])
894 v
.error
(self, "Type Error: Cannot create range: {t1} vs {t2}")
899 redef class ANullExpr
900 redef fun accept_typing
(v
)
902 self.mtype
= v
.mmodule
.model
.null_type
907 # The static type to cast to.
908 # (different from the static type of the expression that is Bool).
909 var cast_type
: nullable MType
910 redef fun accept_typing
(v
)
912 var mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
913 self.cast_type
= mtype
915 var variable
= self.n_expr
.its_variable
916 if variable
!= null then
917 var orig
= self.n_expr
.mtype
918 var from
= if orig
!= null then orig
.to_s
else "invalid"
919 var to
= if mtype
!= null then mtype
.to_s
else "invalid"
920 #debug("adapt {variable}: {from} -> {to}")
921 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
924 self.mtype
= v
.type_bool
(self)
928 redef class AAsCastExpr
929 redef fun accept_typing
(v
)
931 self.mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
935 redef class AAsNotnullExpr
936 redef fun accept_typing
(v
)
938 var mtype
= v
.visit_expr
(self.n_expr
)
939 if mtype
isa MNullType then
940 v
.error
(self, "Type error: as(not null) on null")
943 if mtype
isa MNullableType then
944 self.mtype
= mtype
.mtype
947 # TODO: warn on useless as not null
952 redef class AProxyExpr
953 redef fun accept_typing
(v
)
955 self.mtype
= v
.visit_expr
(self.n_expr
)
959 redef class ASelfExpr
960 redef var its_variable
: nullable Variable
961 redef fun accept_typing
(v
)
963 var variable
= v
.selfvariable
964 self.its_variable
= variable
965 self.mtype
= v
.get_variable
(self, variable
)
969 ## MESSAGE SENDING AND PROPERTY
971 redef class ASendExpr
972 # The property invoked by the send.
973 var mproperty
: nullable MMethod
975 redef fun accept_typing
(v
)
977 var recvtype
= v
.visit_expr
(self.n_expr
)
978 var name
= self.property_name
980 if recvtype
== null then return # Forward error
981 if recvtype
isa MNullType then
982 v
.error
(self, "Error: Method '{name}' call on 'null'.")
986 var propdef
= v
.get_method
(self, recvtype
, name
, self.n_expr
isa ASelfExpr)
987 if propdef
== null then return
988 var mproperty
= propdef
.mproperty
989 self.mproperty
= mproperty
990 var msignature
= propdef
.msignature
991 if msignature
== null then abort # Forward error
993 var for_self
= self.n_expr
isa ASelfExpr
994 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, for_self
)
996 var args
= compute_raw_arguments
998 v
.check_signature
(self, args
, name
, msignature
)
1000 if mproperty
.is_init
then
1001 var vmpropdef
= v
.mpropdef
1002 if not (vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_init
) then
1003 v
.error
(self, "Can call a init only in another init")
1007 var ret
= msignature
.return_mtype
1011 self.is_typed
= true
1015 # The name of the property
1016 # Each subclass simply provide the correct name.
1017 private fun property_name
: String is abstract
1019 # An array of all arguments (excluding self)
1020 fun compute_raw_arguments
: Array[AExpr] is abstract
1023 redef class ABinopExpr
1024 redef fun compute_raw_arguments
do return [n_expr2
]
1027 redef fun property_name
do return "=="
1028 redef fun accept_typing
(v
)
1032 var variable
= self.n_expr
.its_variable
1033 if variable
== null then return
1034 var mtype
= self.n_expr2
.mtype
1035 if not mtype
isa MNullType then return
1036 var vartype
= v
.get_variable
(self, variable
)
1037 if not vartype
isa MNullableType then return
1038 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1039 self.after_flow_context
.when_false
.set_var
(variable
, vartype
.mtype
)
1040 #debug("adapt {variable}:{vartype} ; true->{mtype} false->{vartype.mtype}")
1044 redef fun property_name
do return "!="
1045 redef fun accept_typing
(v
)
1049 var variable
= self.n_expr
.its_variable
1050 if variable
== null then return
1051 var mtype
= self.n_expr2
.mtype
1052 if not mtype
isa MNullType then return
1053 var vartype
= v
.get_variable
(self, variable
)
1054 if not vartype
isa MNullableType then return
1055 self.after_flow_context
.when_false
.set_var
(variable
, mtype
)
1056 self.after_flow_context
.when_true
.set_var
(variable
, vartype
.mtype
)
1057 #debug("adapt {variable}:{vartype} ; true->{vartype.mtype} false->{mtype}")
1061 redef fun property_name
do return "<"
1064 redef fun property_name
do return "<="
1067 redef fun property_name
do return "<<"
1070 redef fun property_name
do return ">"
1073 redef fun property_name
do return ">="
1076 redef fun property_name
do return ">>"
1078 redef class APlusExpr
1079 redef fun property_name
do return "+"
1081 redef class AMinusExpr
1082 redef fun property_name
do return "-"
1084 redef class AStarshipExpr
1085 redef fun property_name
do return "<=>"
1087 redef class AStarExpr
1088 redef fun property_name
do return "*"
1090 redef class ASlashExpr
1091 redef fun property_name
do return "/"
1093 redef class APercentExpr
1094 redef fun property_name
do return "%"
1097 redef class AUminusExpr
1098 redef fun property_name
do return "unary -"
1099 redef fun compute_raw_arguments
do return new Array[AExpr]
1103 redef class ACallExpr
1104 redef fun property_name
do return n_id
.text
1105 redef fun compute_raw_arguments
do return n_args
.to_a
1108 redef class ACallAssignExpr
1109 redef fun property_name
do return n_id
.text
+ "="
1110 redef fun compute_raw_arguments
1112 var res
= n_args
.to_a
1118 redef class ABraExpr
1119 redef fun property_name
do return "[]"
1120 redef fun compute_raw_arguments
do return n_args
.to_a
1123 redef class ABraAssignExpr
1124 redef fun property_name
do return "[]="
1125 redef fun compute_raw_arguments
1127 var res
= n_args
.to_a
1133 redef class ASendReassignFormExpr
1134 # The property invoked for the writing
1135 var write_mproperty
: nullable MMethod = null
1137 redef fun accept_typing
(v
)
1139 var recvtype
= v
.visit_expr
(self.n_expr
)
1140 var name
= self.property_name
1142 if recvtype
== null then return # Forward error
1143 if recvtype
isa MNullType then
1144 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1148 var propdef
= v
.get_method
(self, recvtype
, name
, self.n_expr
isa ASelfExpr)
1149 if propdef
== null then return
1150 var mproperty
= propdef
.mproperty
1151 self.mproperty
= mproperty
1152 var msignature
= propdef
.msignature
1153 if msignature
== null then abort # Forward error
1154 var for_self
= self.n_expr
isa ASelfExpr
1155 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, for_self
)
1157 var args
= compute_raw_arguments
1159 v
.check_signature
(self, args
, name
, msignature
)
1161 var readtype
= msignature
.return_mtype
1162 if readtype
== null then
1163 v
.error
(self, "Error: {name} is not a function")
1167 var wpropdef
= v
.get_method
(self, recvtype
, name
+ "=", self.n_expr
isa ASelfExpr)
1168 if wpropdef
== null then return
1169 var wmproperty
= wpropdef
.mproperty
1170 self.write_mproperty
= wmproperty
1171 var wmsignature
= wpropdef
.msignature
1172 if wmsignature
== null then abort # Forward error
1173 wmsignature
= v
.resolve_signature_for
(wmsignature
, recvtype
, for_self
)
1175 var wtype
= self.resolve_reassignment
(v
, readtype
, wmsignature
.parameter_mtypes
.last
)
1176 if wtype
== null then return
1178 args
.add
(self.n_value
)
1179 v
.check_signature
(self, args
, name
+ "=", wmsignature
)
1181 self.is_typed
= true
1185 redef class ACallReassignExpr
1186 redef fun property_name
do return n_id
.text
1187 redef fun compute_raw_arguments
do return n_args
.to_a
1190 redef class ABraReassignExpr
1191 redef fun property_name
do return "[]"
1192 redef fun compute_raw_arguments
do return n_args
.to_a
1195 redef class AInitExpr
1196 redef fun property_name
do return "init"
1197 redef fun compute_raw_arguments
do return n_args
.to_a
1201 fun to_a
: Array[AExpr] do return self.n_exprs
.to_a
1206 redef class ASuperExpr
1207 # The method to call if the super is in fact a 'super init call'
1208 # Note: if the super is a normal call-next-method, then this attribute is null
1209 var mproperty
: nullable MMethod
1211 redef fun accept_typing
(v
)
1213 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1214 var mproperty
= v
.mpropdef
.mproperty
1215 if not mproperty
isa MMethod then
1216 v
.error
(self, "Error: super only usable in a method")
1219 var superprops
= mproperty
.lookup_super_definitions
(v
.mmodule
, recvtype
)
1220 if superprops
.length
== 0 then
1221 if mproperty
.is_init
and v
.mpropdef
.is_intro
then
1222 process_superinit
(v
)
1225 v
.error
(self, "Error: No super method to call for {mproperty}.")
1227 else if superprops
.length
> 1 then
1228 v
.modelbuilder
.warning
(self, "Error: Conflicting super method to call for {mproperty}: {superprops.join(", ")}.")
1231 var superprop
= superprops
.first
1232 assert superprop
isa MMethodDef
1234 var msignature
= superprop
.msignature
.as(not null)
1235 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, true)
1236 var args
= self.n_args
.to_a
1237 if args
.length
> 0 then
1238 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1240 self.mtype
= msignature
.return_mtype
1243 private fun process_superinit
(v
: TypeVisitor)
1245 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1246 var mproperty
= v
.mpropdef
.mproperty
1247 var superprop
: nullable MMethodDef = null
1248 for msupertype
in v
.nclassdef
.mclassdef
.supertypes
do
1249 msupertype
= msupertype
.anchor_to
(v
.mmodule
, recvtype
)
1250 var errcount
= v
.modelbuilder
.toolcontext
.error_count
1251 var candidate
= v
.try_get_mproperty_by_name2
(self, msupertype
, mproperty
.name
).as(nullable MMethod)
1252 if candidate
== null then
1253 if v
.modelbuilder
.toolcontext
.error_count
> errcount
then return # Forard error
1254 continue # Try next super-class
1256 if superprop
!= null and superprop
.mproperty
!= candidate
then
1257 v
.error
(self, "Error: conflicting super constructor to call for {mproperty}: {candidate.full_name}, {superprop.mproperty.full_name}")
1260 var candidatedefs
= candidate
.lookup_definitions
(v
.mmodule
, recvtype
)
1261 if superprop
!= null then
1262 if superprop
== candidatedefs
.first
then continue
1263 candidatedefs
.add
(superprop
)
1265 if candidatedefs
.length
> 1 then
1266 v
.error
(self, "Error: confliting property definitions for property {mproperty} in {recvtype}: {candidatedefs.join(", ")}")
1269 superprop
= candidatedefs
.first
1271 if superprop
== null then
1272 v
.error
(self, "Error: No super method to call for {mproperty}.")
1275 self.mproperty
= superprop
.mproperty
1277 var args
= self.n_args
.to_a
1278 var msignature
= superprop
.msignature
.as(not null)
1279 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, true)
1280 if args
.length
> 0 then
1281 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1283 # TODO: Check signature
1286 self.is_typed
= true
1292 redef class ANewExpr
1293 # The constructor invoked by the new.
1294 var mproperty
: nullable MMethod
1296 redef fun accept_typing
(v
)
1298 var recvtype
= v
.resolve_mtype
(self.n_type
)
1299 if recvtype
== null then return
1300 self.mtype
= recvtype
1302 if not recvtype
isa MClassType then
1303 if recvtype
isa MNullableType then
1304 v
.error
(self, "Type error: cannot instantiate the nullable type {recvtype}.")
1307 v
.error
(self, "Type error: cannot instantiate the formal type {recvtype}.")
1319 var propdef
= v
.get_method
(self, recvtype
, name
, false)
1320 if propdef
== null then return
1322 self.mproperty
= propdef
.mproperty
1324 if not propdef
.mproperty
.is_init_for
(recvtype
.mclass
) then
1325 v
.error
(self, "Error: {name} is not a constructor.")
1329 var msignature
= propdef
.msignature
.as(not null)
1330 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, false)
1332 var args
= n_args
.to_a
1333 v
.check_signature
(self, args
, name
, msignature
)
1339 redef class AAttrFormExpr
1340 # The attribute acceded.
1341 var mproperty
: nullable MAttribute
1343 # The static type of the attribute.
1344 var attr_type
: nullable MType
1346 # Resolve the attribute acceded.
1347 private fun resolve_property
(v
: TypeVisitor)
1349 var recvtype
= v
.visit_expr
(self.n_expr
)
1350 if recvtype
== null then return # Skip error
1351 var name
= self.n_id
.text
1352 if recvtype
isa MNullType then
1353 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1357 var unsafe_type
= v
.anchor_to
(recvtype
)
1358 var mproperty
= v
.try_get_mproperty_by_name2
(self, unsafe_type
, name
)
1359 if mproperty
== null then
1360 v
.modelbuilder
.error
(self, "Error: Attribute {name} doesn't exists in {recvtype}.")
1363 assert mproperty
isa MAttribute
1364 self.mproperty
= mproperty
1366 var mpropdefs
= mproperty
.lookup_definitions
(v
.mmodule
, unsafe_type
)
1367 assert mpropdefs
.length
== 1
1368 var mpropdef
= mpropdefs
.first
1369 var attr_type
= mpropdef
.static_mtype
.as(not null)
1370 attr_type
= v
.resolve_for
(attr_type
, recvtype
, self.n_expr
isa ASelfExpr)
1371 self.attr_type
= attr_type
1375 redef class AAttrExpr
1376 redef fun accept_typing
(v
)
1378 self.resolve_property
(v
)
1379 self.mtype
= self.attr_type
1384 redef class AAttrAssignExpr
1385 redef fun accept_typing
(v
)
1387 self.resolve_property
(v
)
1388 var mtype
= self.attr_type
1390 v
.visit_expr_subtype
(self.n_value
, mtype
)
1391 self.is_typed
= true
1395 redef class AAttrReassignExpr
1396 redef fun accept_typing
(v
)
1398 self.resolve_property
(v
)
1399 var mtype
= self.attr_type
1400 if mtype
== null then return # Skip error
1402 self.resolve_reassignment
(v
, mtype
, mtype
)
1404 self.is_typed
= true
1408 redef class AIssetAttrExpr
1409 redef fun accept_typing
(v
)
1411 self.resolve_property
(v
)
1412 var mtype
= self.attr_type
1413 if mtype
== null then return # Skip error
1415 var recvtype
= self.n_expr
.mtype
.as(not null)
1416 var bound
= v
.resolve_for
(mtype
, recvtype
, false)
1417 if bound
isa MNullableType then
1418 v
.error
(self, "Error: isset on a nullable attribute.")
1420 self.mtype
= v
.type_bool
(self)
1426 redef class AClosureCallExpr
1427 redef fun accept_typing
(v
)
1435 redef class ADebugTypeExpr
1436 redef fun accept_typing
(v
)
1438 var expr
= v
.visit_expr
(self.n_expr
)
1439 if expr
== null then return
1440 var unsafe
= v
.anchor_to
(expr
)
1441 var ntype
= self.n_type
1442 var mtype
= v
.resolve_mtype
(ntype
)
1443 if mtype
!= null and mtype
!= expr
then
1444 var umtype
= v
.anchor_to
(mtype
)
1445 v
.modelbuilder
.warning
(self, "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")