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)
381 # The variable associated to the reciever (if any)
382 var selfvariable
: nullable Variable
385 redef class AConcreteMethPropdef
386 redef fun do_typing
(modelbuilder
: ModelBuilder)
388 var nclassdef
= self.parent
.as(AClassdef)
389 var mpropdef
= self.mpropdef
.as(not null)
390 var v
= new TypeVisitor(modelbuilder
, nclassdef
, mpropdef
)
391 self.selfvariable
= v
.selfvariable
393 var nblock
= self.n_block
394 if nblock
== null then return
396 var mmethoddef
= self.mpropdef
.as(not null)
397 for i
in [0..mmethoddef
.msignature
.arity
[ do
398 var mtype
= mmethoddef
.msignature
.parameter_mtypes
[i
]
399 if mmethoddef
.msignature
.vararg_rank
== i
then
400 var arrayclass
= v
.get_mclass
(self.n_signature
.n_params
[i
], "Array")
401 if arrayclass
== null then return # Skip error
402 mtype
= arrayclass
.get_mtype
([mtype
])
404 var variable
= self.n_signature
.n_params
[i
].variable
405 assert variable
!= null
406 variable
.declared_type
= mtype
410 if not nblock
.after_flow_context
.is_unreachable
and mmethoddef
.msignature
.return_mtype
!= null then
411 # We reach the end of the function without having a return, it is bad
412 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
417 redef class AAttrPropdef
418 redef fun do_typing
(modelbuilder
: ModelBuilder)
420 var nclassdef
= self.parent
.as(AClassdef)
421 var v
= new TypeVisitor(modelbuilder
, nclassdef
, self.mpropdef
.as(not null))
422 self.selfvariable
= v
.selfvariable
424 var nexpr
= self.n_expr
425 if nexpr
!= null then
426 var mtype
= self.mpropdef
.static_mtype
427 v
.visit_expr_subtype
(nexpr
, mtype
)
435 # The static type of the expression.
436 # null if self is a statement of in case of error
437 var mtype
: nullable MType = null
439 # Is the statement correctly typed?
440 # Used to distinguish errors and statements when `mtype' == null
441 var is_typed
: Bool = false
443 # Return the variable read (if any)
444 # Used to perform adaptive typing
445 fun its_variable
: nullable Variable do return null
447 private fun accept_typing
(v
: TypeVisitor)
449 v
.error
(self, "no implemented accept_typing for {self.class_name}")
453 redef class ABlockExpr
454 redef fun accept_typing
(v
)
456 for e
in self.n_expr
do v
.visit_stmt
(e
)
461 redef class AVardeclExpr
462 redef fun accept_typing
(v
)
464 var variable
= self.variable
465 if variable
== null then return # Skip error
467 var ntype
= self.n_type
468 var mtype
: nullable MType
469 if ntype
== null then
472 mtype
= v
.resolve_mtype
(ntype
)
473 if mtype
== null then return # Skip error
476 var nexpr
= self.n_expr
477 if nexpr
!= null then
478 if mtype
!= null then
479 v
.visit_expr_subtype
(nexpr
, mtype
)
481 mtype
= v
.visit_expr
(nexpr
)
482 if mtype
== null then return # Skip error
486 if mtype
== null then
487 mtype
= v
.get_mclass
(self, "Object").mclass_type
.as_nullable
490 variable
.declared_type
= mtype
491 v
.set_variable
(self, variable
, mtype
)
493 #debug("var {variable}: {mtype}")
500 redef fun its_variable
do return self.variable
501 redef fun accept_typing
(v
)
503 var variable
= self.variable
504 if variable
== null then return # Skip error
506 var mtype
= v
.get_variable
(self, variable
)
507 if mtype
!= null then
508 #debug("{variable} is {mtype}")
510 #debug("{variable} is untyped")
517 redef class AVarAssignExpr
518 redef fun accept_typing
(v
)
520 var variable
= self.variable
521 assert variable
!= null
523 var mtype
= v
.visit_expr_subtype
(n_value
, variable
.declared_type
)
525 v
.set_variable
(self, variable
, mtype
)
531 redef class AReassignFormExpr
532 # The method designed by the reassign operator.
533 var reassign_property
: nullable MMethodDef = null
535 var read_type
: nullable MType = null
537 # Determine the `reassign_property'
538 # `readtype' is the type of the reading of the left value.
539 # `writetype' is the type of the writing of the left value.
540 # (Because of ACallReassignExpr, both can be different.
541 # Return the static type of the value to store.
542 private fun resolve_reassignment
(v
: TypeVisitor, readtype
, writetype
: MType): nullable MType
544 var reassign_name
: String
545 if self.n_assign_op
isa APlusAssignOp then
547 else if self.n_assign_op
isa AMinusAssignOp then
553 self.read_type
= readtype
555 if readtype
isa MNullType then
556 v
.error
(self, "Error: Method '{reassign_name}' call on 'null'.")
560 var mpropdef
= v
.get_method
(self, readtype
, reassign_name
, false)
561 if mpropdef
== null then return null # Skip error
563 self.reassign_property
= mpropdef
565 var msignature
= mpropdef
.msignature
566 assert msignature
!= null
567 msignature
= v
.resolve_signature_for
(msignature
, readtype
, false)
569 var rettype
= msignature
.return_mtype
570 assert msignature
.arity
== 1 and rettype
!= null
572 var value_type
= v
.visit_expr_subtype
(self.n_value
, msignature
.parameter_mtypes
.first
)
573 if value_type
== null then return null # Skip error
575 v
.check_subtype
(self, rettype
, writetype
)
580 redef class AVarReassignExpr
581 redef fun accept_typing
(v
)
583 var variable
= self.variable
584 assert variable
!= null
586 var readtype
= v
.get_variable
(self, variable
)
587 if readtype
== null then return
589 var writetype
= variable
.declared_type
590 if writetype
== null then return
592 var rettype
= self.resolve_reassignment
(v
, readtype
, writetype
)
594 v
.set_variable
(self, variable
, rettype
)
601 redef class AContinueExpr
602 redef fun accept_typing
(v
)
604 var nexpr
= self.n_expr
605 if nexpr
!= null then
606 var mtype
= v
.visit_expr
(nexpr
)
612 redef class ABreakExpr
613 redef fun accept_typing
(v
)
615 var nexpr
= self.n_expr
616 if nexpr
!= null then
617 var mtype
= v
.visit_expr
(nexpr
)
623 redef class AReturnExpr
624 redef fun accept_typing
(v
)
626 var nexpr
= self.n_expr
627 var ret_type
= v
.mpropdef
.as(MMethodDef).msignature
.return_mtype
628 if nexpr
!= null then
629 if ret_type
!= null then
630 var mtype
= v
.visit_expr_subtype
(nexpr
, ret_type
)
632 var mtype
= v
.visit_expr
(nexpr
)
633 v
.error
(self, "Error: Return with value in a procedure.")
635 else if ret_type
!= null then
636 v
.error
(self, "Error: Return without value in a function.")
642 redef class AAbortExpr
643 redef fun accept_typing
(v
)
650 redef fun accept_typing
(v
)
652 v
.visit_expr_bool
(n_expr
)
660 redef class AIfexprExpr
661 redef fun accept_typing
(v
)
663 v
.visit_expr_bool
(n_expr
)
665 var t1
= v
.visit_expr
(n_then
)
666 var t2
= v
.visit_expr
(n_else
)
668 if t1
== null or t2
== null then
672 var t
= v
.merge_types
(self, [t1
, t2
])
674 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
681 redef fun accept_typing
(v
)
683 v
.visit_stmt
(n_block
)
688 redef class AWhileExpr
689 redef fun accept_typing
(v
)
691 v
.visit_expr_bool
(n_expr
)
693 v
.visit_stmt
(n_block
)
698 redef class ALoopExpr
699 redef fun accept_typing
(v
)
701 v
.visit_stmt
(n_block
)
707 redef fun accept_typing
(v
)
709 var mtype
= v
.visit_expr
(n_expr
)
710 if mtype
== null then return
712 var colcla
= v
.get_mclass
(self, "Collection")
713 if colcla
== null then return
714 var objcla
= v
.get_mclass
(self, "Object")
715 if objcla
== null then return
716 if v
.is_subtype
(mtype
, colcla
.get_mtype
([objcla
.mclass_type
.as_nullable
])) then
717 var coltype
= mtype
.supertype_to
(v
.mmodule
, v
.anchor
, colcla
)
718 assert coltype
isa MGenericType
719 var variables
= self.variables
720 if variables
.length
!= 1 then
721 v
.error
(self, "Type Error: Expected one variable")
723 variables
.first
.declared_type
= coltype
.arguments
.first
726 v
.modelbuilder
.error
(self, "TODO: Do 'for' on {mtype}")
729 v
.visit_stmt
(n_block
)
734 redef class AAssertExpr
735 redef fun accept_typing
(v
)
737 v
.visit_expr_bool
(n_expr
)
745 redef fun accept_typing
(v
)
747 v
.visit_expr_bool
(n_expr
)
748 v
.visit_expr_bool
(n_expr2
)
749 self.mtype
= v
.type_bool
(self)
754 redef fun accept_typing
(v
)
756 v
.visit_expr_bool
(n_expr
)
757 v
.visit_expr_bool
(n_expr2
)
758 self.mtype
= v
.type_bool
(self)
764 redef fun accept_typing
(v
)
766 v
.visit_expr_bool
(n_expr
)
767 self.mtype
= v
.type_bool
(self)
771 redef class AOrElseExpr
772 redef fun accept_typing
(v
)
774 var t1
= v
.visit_expr
(n_expr
)
775 var t2
= v
.visit_expr
(n_expr2
)
777 if t1
== null or t2
== null then
781 if t1
isa MNullableType then
785 var t
= v
.merge_types
(self, [t1
, t2
])
787 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
794 redef fun accept_typing
(v
)
797 v
.visit_expr
(n_expr2
)
798 self.mtype
= v
.type_bool
(self)
802 redef class ATrueExpr
803 redef fun accept_typing
(v
)
805 self.mtype
= v
.type_bool
(self)
809 redef class AFalseExpr
810 redef fun accept_typing
(v
)
812 self.mtype
= v
.type_bool
(self)
817 redef fun accept_typing
(v
)
819 var mclass
= v
.get_mclass
(self, "Int")
820 if mclass
== null then return # Forward error
821 self.mtype
= mclass
.mclass_type
825 redef class AFloatExpr
826 redef fun accept_typing
(v
)
828 var mclass
= v
.get_mclass
(self, "Float")
829 if mclass
== null then return # Forward error
830 self.mtype
= mclass
.mclass_type
834 redef class ACharExpr
835 redef fun accept_typing
(v
)
837 var mclass
= v
.get_mclass
(self, "Char")
838 if mclass
== null then return # Forward error
839 self.mtype
= mclass
.mclass_type
843 redef class AStringFormExpr
844 redef fun accept_typing
(v
)
846 var mclass
= v
.get_mclass
(self, "String")
847 if mclass
== null then return # Forward error
848 self.mtype
= mclass
.mclass_type
852 redef class ASuperstringExpr
853 redef fun accept_typing
(v
)
855 var mclass
= v
.get_mclass
(self, "String")
856 if mclass
== null then return # Forward error
857 self.mtype
= mclass
.mclass_type
858 for nexpr
in self.n_exprs
do
859 var t
= v
.visit_expr
(nexpr
)
864 redef class AArrayExpr
865 redef fun accept_typing
(v
)
867 var mtypes
= new Array[nullable MType]
868 for e
in self.n_exprs
.n_exprs
do
869 var t
= v
.visit_expr
(e
)
875 var mtype
= v
.merge_types
(self, mtypes
)
876 if mtype
== null then
877 v
.error
(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
880 var mclass
= v
.get_mclass
(self, "Array")
881 if mclass
== null then return # Forward error
882 self.mtype
= mclass
.get_mtype
([mtype
])
886 redef class ARangeExpr
887 redef fun accept_typing
(v
)
889 var t1
= v
.visit_expr
(self.n_expr
)
890 var t2
= v
.visit_expr
(self.n_expr2
)
891 if t1
== null or t2
== null then return
892 var mclass
= v
.get_mclass
(self, "Range")
893 if mclass
== null then return # Forward error
894 if v
.is_subtype
(t1
, t2
) then
895 self.mtype
= mclass
.get_mtype
([t2
])
896 else if v
.is_subtype
(t2
, t1
) then
897 self.mtype
= mclass
.get_mtype
([t1
])
899 v
.error
(self, "Type Error: Cannot create range: {t1} vs {t2}")
904 redef class ANullExpr
905 redef fun accept_typing
(v
)
907 self.mtype
= v
.mmodule
.model
.null_type
912 # The static type to cast to.
913 # (different from the static type of the expression that is Bool).
914 var cast_type
: nullable MType
915 redef fun accept_typing
(v
)
917 var mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
918 self.cast_type
= mtype
920 var variable
= self.n_expr
.its_variable
921 if variable
!= null then
922 var orig
= self.n_expr
.mtype
923 var from
= if orig
!= null then orig
.to_s
else "invalid"
924 var to
= if mtype
!= null then mtype
.to_s
else "invalid"
925 #debug("adapt {variable}: {from} -> {to}")
926 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
929 self.mtype
= v
.type_bool
(self)
933 redef class AAsCastExpr
934 redef fun accept_typing
(v
)
936 self.mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
940 redef class AAsNotnullExpr
941 redef fun accept_typing
(v
)
943 var mtype
= v
.visit_expr
(self.n_expr
)
944 if mtype
isa MNullType then
945 v
.error
(self, "Type error: as(not null) on null")
948 if mtype
isa MNullableType then
949 self.mtype
= mtype
.mtype
952 # TODO: warn on useless as not null
957 redef class AProxyExpr
958 redef fun accept_typing
(v
)
960 self.mtype
= v
.visit_expr
(self.n_expr
)
964 redef class ASelfExpr
965 redef var its_variable
: nullable Variable
966 redef fun accept_typing
(v
)
968 var variable
= v
.selfvariable
969 self.its_variable
= variable
970 self.mtype
= v
.get_variable
(self, variable
)
974 ## MESSAGE SENDING AND PROPERTY
976 redef class ASendExpr
977 # The property invoked by the send.
978 var mproperty
: nullable MMethod
980 redef fun accept_typing
(v
)
982 var recvtype
= v
.visit_expr
(self.n_expr
)
983 var name
= self.property_name
985 if recvtype
== null then return # Forward error
986 if recvtype
isa MNullType then
987 v
.error
(self, "Error: Method '{name}' call on 'null'.")
991 var propdef
= v
.get_method
(self, recvtype
, name
, self.n_expr
isa ASelfExpr)
992 if propdef
== null then return
993 var mproperty
= propdef
.mproperty
994 self.mproperty
= mproperty
995 var msignature
= propdef
.msignature
996 if msignature
== null then abort # Forward error
998 var for_self
= self.n_expr
isa ASelfExpr
999 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, for_self
)
1001 var args
= compute_raw_arguments
1003 v
.check_signature
(self, args
, name
, msignature
)
1005 if mproperty
.is_init
then
1006 var vmpropdef
= v
.mpropdef
1007 if not (vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_init
) then
1008 v
.error
(self, "Can call a init only in another init")
1012 var ret
= msignature
.return_mtype
1016 self.is_typed
= true
1020 # The name of the property
1021 # Each subclass simply provide the correct name.
1022 private fun property_name
: String is abstract
1024 # An array of all arguments (excluding self)
1025 fun compute_raw_arguments
: Array[AExpr] is abstract
1028 redef class ABinopExpr
1029 redef fun compute_raw_arguments
do return [n_expr2
]
1032 redef fun property_name
do return "=="
1033 redef fun accept_typing
(v
)
1037 var variable
= self.n_expr
.its_variable
1038 if variable
== null then return
1039 var mtype
= self.n_expr2
.mtype
1040 if not mtype
isa MNullType then return
1041 var vartype
= v
.get_variable
(self, variable
)
1042 if not vartype
isa MNullableType then return
1043 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1044 self.after_flow_context
.when_false
.set_var
(variable
, vartype
.mtype
)
1045 #debug("adapt {variable}:{vartype} ; true->{mtype} false->{vartype.mtype}")
1049 redef fun property_name
do return "!="
1050 redef fun accept_typing
(v
)
1054 var variable
= self.n_expr
.its_variable
1055 if variable
== null then return
1056 var mtype
= self.n_expr2
.mtype
1057 if not mtype
isa MNullType then return
1058 var vartype
= v
.get_variable
(self, variable
)
1059 if not vartype
isa MNullableType then return
1060 self.after_flow_context
.when_false
.set_var
(variable
, mtype
)
1061 self.after_flow_context
.when_true
.set_var
(variable
, vartype
.mtype
)
1062 #debug("adapt {variable}:{vartype} ; true->{vartype.mtype} false->{mtype}")
1066 redef fun property_name
do return "<"
1069 redef fun property_name
do return "<="
1072 redef fun property_name
do return "<<"
1075 redef fun property_name
do return ">"
1078 redef fun property_name
do return ">="
1081 redef fun property_name
do return ">>"
1083 redef class APlusExpr
1084 redef fun property_name
do return "+"
1086 redef class AMinusExpr
1087 redef fun property_name
do return "-"
1089 redef class AStarshipExpr
1090 redef fun property_name
do return "<=>"
1092 redef class AStarExpr
1093 redef fun property_name
do return "*"
1095 redef class ASlashExpr
1096 redef fun property_name
do return "/"
1098 redef class APercentExpr
1099 redef fun property_name
do return "%"
1102 redef class AUminusExpr
1103 redef fun property_name
do return "unary -"
1104 redef fun compute_raw_arguments
do return new Array[AExpr]
1108 redef class ACallExpr
1109 redef fun property_name
do return n_id
.text
1110 redef fun compute_raw_arguments
do return n_args
.to_a
1113 redef class ACallAssignExpr
1114 redef fun property_name
do return n_id
.text
+ "="
1115 redef fun compute_raw_arguments
1117 var res
= n_args
.to_a
1123 redef class ABraExpr
1124 redef fun property_name
do return "[]"
1125 redef fun compute_raw_arguments
do return n_args
.to_a
1128 redef class ABraAssignExpr
1129 redef fun property_name
do return "[]="
1130 redef fun compute_raw_arguments
1132 var res
= n_args
.to_a
1138 redef class ASendReassignFormExpr
1139 # The property invoked for the writing
1140 var write_mproperty
: nullable MMethod = null
1142 redef fun accept_typing
(v
)
1144 var recvtype
= v
.visit_expr
(self.n_expr
)
1145 var name
= self.property_name
1147 if recvtype
== null then return # Forward error
1148 if recvtype
isa MNullType then
1149 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1153 var propdef
= v
.get_method
(self, recvtype
, name
, self.n_expr
isa ASelfExpr)
1154 if propdef
== null then return
1155 var mproperty
= propdef
.mproperty
1156 self.mproperty
= mproperty
1157 var msignature
= propdef
.msignature
1158 if msignature
== null then abort # Forward error
1159 var for_self
= self.n_expr
isa ASelfExpr
1160 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, for_self
)
1162 var args
= compute_raw_arguments
1164 v
.check_signature
(self, args
, name
, msignature
)
1166 var readtype
= msignature
.return_mtype
1167 if readtype
== null then
1168 v
.error
(self, "Error: {name} is not a function")
1172 var wpropdef
= v
.get_method
(self, recvtype
, name
+ "=", self.n_expr
isa ASelfExpr)
1173 if wpropdef
== null then return
1174 var wmproperty
= wpropdef
.mproperty
1175 self.write_mproperty
= wmproperty
1176 var wmsignature
= wpropdef
.msignature
1177 if wmsignature
== null then abort # Forward error
1178 wmsignature
= v
.resolve_signature_for
(wmsignature
, recvtype
, for_self
)
1180 var wtype
= self.resolve_reassignment
(v
, readtype
, wmsignature
.parameter_mtypes
.last
)
1181 if wtype
== null then return
1183 args
.add
(self.n_value
)
1184 v
.check_signature
(self, args
, name
+ "=", wmsignature
)
1186 self.is_typed
= true
1190 redef class ACallReassignExpr
1191 redef fun property_name
do return n_id
.text
1192 redef fun compute_raw_arguments
do return n_args
.to_a
1195 redef class ABraReassignExpr
1196 redef fun property_name
do return "[]"
1197 redef fun compute_raw_arguments
do return n_args
.to_a
1200 redef class AInitExpr
1201 redef fun property_name
do return "init"
1202 redef fun compute_raw_arguments
do return n_args
.to_a
1206 fun to_a
: Array[AExpr] do return self.n_exprs
.to_a
1211 redef class ASuperExpr
1212 # The method to call if the super is in fact a 'super init call'
1213 # Note: if the super is a normal call-next-method, then this attribute is null
1214 var mproperty
: nullable MMethod
1216 redef fun accept_typing
(v
)
1218 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1219 var mproperty
= v
.mpropdef
.mproperty
1220 if not mproperty
isa MMethod then
1221 v
.error
(self, "Error: super only usable in a method")
1224 var superprops
= mproperty
.lookup_super_definitions
(v
.mmodule
, recvtype
)
1225 if superprops
.length
== 0 then
1226 if mproperty
.is_init
and v
.mpropdef
.is_intro
then
1227 process_superinit
(v
)
1230 v
.error
(self, "Error: No super method to call for {mproperty}.")
1232 else if superprops
.length
> 1 then
1233 v
.modelbuilder
.warning
(self, "Error: Conflicting super method to call for {mproperty}: {superprops.join(", ")}.")
1236 var superprop
= superprops
.first
1237 assert superprop
isa MMethodDef
1239 var msignature
= superprop
.msignature
.as(not null)
1240 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, true)
1241 var args
= self.n_args
.to_a
1242 if args
.length
> 0 then
1243 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1245 self.mtype
= msignature
.return_mtype
1248 private fun process_superinit
(v
: TypeVisitor)
1250 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1251 var mproperty
= v
.mpropdef
.mproperty
1252 var superprop
: nullable MMethodDef = null
1253 for msupertype
in v
.nclassdef
.mclassdef
.supertypes
do
1254 msupertype
= msupertype
.anchor_to
(v
.mmodule
, recvtype
)
1255 var errcount
= v
.modelbuilder
.toolcontext
.error_count
1256 var candidate
= v
.try_get_mproperty_by_name2
(self, msupertype
, mproperty
.name
).as(nullable MMethod)
1257 if candidate
== null then
1258 if v
.modelbuilder
.toolcontext
.error_count
> errcount
then return # Forard error
1259 continue # Try next super-class
1261 if superprop
!= null and superprop
.mproperty
!= candidate
then
1262 v
.error
(self, "Error: conflicting super constructor to call for {mproperty}: {candidate.full_name}, {superprop.mproperty.full_name}")
1265 var candidatedefs
= candidate
.lookup_definitions
(v
.mmodule
, recvtype
)
1266 if superprop
!= null then
1267 if superprop
== candidatedefs
.first
then continue
1268 candidatedefs
.add
(superprop
)
1270 if candidatedefs
.length
> 1 then
1271 v
.error
(self, "Error: confliting property definitions for property {mproperty} in {recvtype}: {candidatedefs.join(", ")}")
1274 superprop
= candidatedefs
.first
1276 if superprop
== null then
1277 v
.error
(self, "Error: No super method to call for {mproperty}.")
1280 self.mproperty
= superprop
.mproperty
1282 var args
= self.n_args
.to_a
1283 var msignature
= superprop
.msignature
.as(not null)
1284 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, true)
1285 if args
.length
> 0 then
1286 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1288 # TODO: Check signature
1291 self.is_typed
= true
1297 redef class ANewExpr
1298 # The constructor invoked by the new.
1299 var mproperty
: nullable MMethod
1301 redef fun accept_typing
(v
)
1303 var recvtype
= v
.resolve_mtype
(self.n_type
)
1304 if recvtype
== null then return
1305 self.mtype
= recvtype
1307 if not recvtype
isa MClassType then
1308 if recvtype
isa MNullableType then
1309 v
.error
(self, "Type error: cannot instantiate the nullable type {recvtype}.")
1312 v
.error
(self, "Type error: cannot instantiate the formal type {recvtype}.")
1324 var propdef
= v
.get_method
(self, recvtype
, name
, false)
1325 if propdef
== null then return
1327 self.mproperty
= propdef
.mproperty
1329 if not propdef
.mproperty
.is_init_for
(recvtype
.mclass
) then
1330 v
.error
(self, "Error: {name} is not a constructor.")
1334 var msignature
= propdef
.msignature
.as(not null)
1335 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, false)
1337 var args
= n_args
.to_a
1338 v
.check_signature
(self, args
, name
, msignature
)
1344 redef class AAttrFormExpr
1345 # The attribute acceded.
1346 var mproperty
: nullable MAttribute
1348 # The static type of the attribute.
1349 var attr_type
: nullable MType
1351 # Resolve the attribute acceded.
1352 private fun resolve_property
(v
: TypeVisitor)
1354 var recvtype
= v
.visit_expr
(self.n_expr
)
1355 if recvtype
== null then return # Skip error
1356 var name
= self.n_id
.text
1357 if recvtype
isa MNullType then
1358 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1362 var unsafe_type
= v
.anchor_to
(recvtype
)
1363 var mproperty
= v
.try_get_mproperty_by_name2
(self, unsafe_type
, name
)
1364 if mproperty
== null then
1365 v
.modelbuilder
.error
(self, "Error: Attribute {name} doesn't exists in {recvtype}.")
1368 assert mproperty
isa MAttribute
1369 self.mproperty
= mproperty
1371 var mpropdefs
= mproperty
.lookup_definitions
(v
.mmodule
, unsafe_type
)
1372 assert mpropdefs
.length
== 1
1373 var mpropdef
= mpropdefs
.first
1374 var attr_type
= mpropdef
.static_mtype
.as(not null)
1375 attr_type
= v
.resolve_for
(attr_type
, recvtype
, self.n_expr
isa ASelfExpr)
1376 self.attr_type
= attr_type
1380 redef class AAttrExpr
1381 redef fun accept_typing
(v
)
1383 self.resolve_property
(v
)
1384 self.mtype
= self.attr_type
1389 redef class AAttrAssignExpr
1390 redef fun accept_typing
(v
)
1392 self.resolve_property
(v
)
1393 var mtype
= self.attr_type
1395 v
.visit_expr_subtype
(self.n_value
, mtype
)
1396 self.is_typed
= true
1400 redef class AAttrReassignExpr
1401 redef fun accept_typing
(v
)
1403 self.resolve_property
(v
)
1404 var mtype
= self.attr_type
1405 if mtype
== null then return # Skip error
1407 self.resolve_reassignment
(v
, mtype
, mtype
)
1409 self.is_typed
= true
1413 redef class AIssetAttrExpr
1414 redef fun accept_typing
(v
)
1416 self.resolve_property
(v
)
1417 var mtype
= self.attr_type
1418 if mtype
== null then return # Skip error
1420 var recvtype
= self.n_expr
.mtype
.as(not null)
1421 var bound
= v
.resolve_for
(mtype
, recvtype
, false)
1422 if bound
isa MNullableType then
1423 v
.error
(self, "Error: isset on a nullable attribute.")
1425 self.mtype
= v
.type_bool
(self)
1431 redef class AClosureCallExpr
1432 redef fun accept_typing
(v
)
1440 redef class ADebugTypeExpr
1441 redef fun accept_typing
(v
)
1443 var expr
= v
.visit_expr
(self.n_expr
)
1444 if expr
== null then return
1445 var unsafe
= v
.anchor_to
(expr
)
1446 var ntype
= self.n_type
1447 var mtype
= v
.resolve_mtype
(ntype
)
1448 if mtype
!= null and mtype
!= expr
then
1449 var umtype
= v
.anchor_to
(mtype
)
1450 v
.modelbuilder
.warning
(self, "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")