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
.mparameters
[i
].mtype
249 self.visit_expr_subtype
(args
[j
], paramtype
)
251 if vararg_rank
>= 0 then
252 var varargs
= new Array[AExpr]
253 var paramtype
= msignature
.mparameters
[vararg_rank
].mtype
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
.mparameters
[i
].mtype
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
408 for i
in [0..mmethoddef
.msignature
.mclosures
.length
[ do
409 var mclosure
= mmethoddef
.msignature
.mclosures
[i
]
410 var variable
= self.n_signature
.n_closure_decls
[i
].variable
411 assert variable
!= null
412 variable
.declared_type
= mclosure
.mtype
416 if not nblock
.after_flow_context
.is_unreachable
and mmethoddef
.msignature
.return_mtype
!= null then
417 # We reach the end of the function without having a return, it is bad
418 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
423 redef class AAttrPropdef
424 redef fun do_typing
(modelbuilder
: ModelBuilder)
426 var nclassdef
= self.parent
.as(AClassdef)
427 var v
= new TypeVisitor(modelbuilder
, nclassdef
, self.mpropdef
.as(not null))
428 self.selfvariable
= v
.selfvariable
430 var nexpr
= self.n_expr
431 if nexpr
!= null then
432 var mtype
= self.mpropdef
.static_mtype
433 v
.visit_expr_subtype
(nexpr
, mtype
)
441 # The static type of the expression.
442 # null if self is a statement of in case of error
443 var mtype
: nullable MType = null
445 # Is the statement correctly typed?
446 # Used to distinguish errors and statements when `mtype' == null
447 var is_typed
: Bool = false
449 # Return the variable read (if any)
450 # Used to perform adaptive typing
451 fun its_variable
: nullable Variable do return null
453 private fun accept_typing
(v
: TypeVisitor)
455 v
.error
(self, "no implemented accept_typing for {self.class_name}")
459 redef class ABlockExpr
460 redef fun accept_typing
(v
)
462 for e
in self.n_expr
do v
.visit_stmt
(e
)
467 redef class AVardeclExpr
468 redef fun accept_typing
(v
)
470 var variable
= self.variable
471 if variable
== null then return # Skip error
473 var ntype
= self.n_type
474 var mtype
: nullable MType
475 if ntype
== null then
478 mtype
= v
.resolve_mtype
(ntype
)
479 if mtype
== null then return # Skip error
482 var nexpr
= self.n_expr
483 if nexpr
!= null then
484 if mtype
!= null then
485 v
.visit_expr_subtype
(nexpr
, mtype
)
487 mtype
= v
.visit_expr
(nexpr
)
488 if mtype
== null then return # Skip error
492 if mtype
== null then
493 mtype
= v
.get_mclass
(self, "Object").mclass_type
.as_nullable
496 variable
.declared_type
= mtype
497 v
.set_variable
(self, variable
, mtype
)
499 #debug("var {variable}: {mtype}")
506 redef fun its_variable
do return self.variable
507 redef fun accept_typing
(v
)
509 var variable
= self.variable
510 if variable
== null then return # Skip error
512 var mtype
= v
.get_variable
(self, variable
)
513 if mtype
!= null then
514 #debug("{variable} is {mtype}")
516 #debug("{variable} is untyped")
523 redef class AVarAssignExpr
524 redef fun accept_typing
(v
)
526 var variable
= self.variable
527 assert variable
!= null
529 var mtype
= v
.visit_expr_subtype
(n_value
, variable
.declared_type
)
531 v
.set_variable
(self, variable
, mtype
)
537 redef class AReassignFormExpr
538 # The method designed by the reassign operator.
539 var reassign_property
: nullable MMethodDef = null
541 var read_type
: nullable MType = null
543 # Determine the `reassign_property'
544 # `readtype' is the type of the reading of the left value.
545 # `writetype' is the type of the writing of the left value.
546 # (Because of ACallReassignExpr, both can be different.
547 # Return the static type of the value to store.
548 private fun resolve_reassignment
(v
: TypeVisitor, readtype
, writetype
: MType): nullable MType
550 var reassign_name
: String
551 if self.n_assign_op
isa APlusAssignOp then
553 else if self.n_assign_op
isa AMinusAssignOp then
559 self.read_type
= readtype
561 if readtype
isa MNullType then
562 v
.error
(self, "Error: Method '{reassign_name}' call on 'null'.")
566 var mpropdef
= v
.get_method
(self, readtype
, reassign_name
, false)
567 if mpropdef
== null then return null # Skip error
569 self.reassign_property
= mpropdef
571 var msignature
= mpropdef
.msignature
572 assert msignature
!= null
573 msignature
= v
.resolve_signature_for
(msignature
, readtype
, false)
575 var rettype
= msignature
.return_mtype
576 assert msignature
.arity
== 1 and rettype
!= null
578 var value_type
= v
.visit_expr_subtype
(self.n_value
, msignature
.mparameters
.first
.mtype
)
579 if value_type
== null then return null # Skip error
581 v
.check_subtype
(self, rettype
, writetype
)
586 redef class AVarReassignExpr
587 redef fun accept_typing
(v
)
589 var variable
= self.variable
590 assert variable
!= null
592 var readtype
= v
.get_variable
(self, variable
)
593 if readtype
== null then return
595 var writetype
= variable
.declared_type
596 if writetype
== null then return
598 var rettype
= self.resolve_reassignment
(v
, readtype
, writetype
)
600 v
.set_variable
(self, variable
, rettype
)
607 redef class AContinueExpr
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 ABreakExpr
619 redef fun accept_typing
(v
)
621 var nexpr
= self.n_expr
622 if nexpr
!= null then
623 var mtype
= v
.visit_expr
(nexpr
)
629 redef class AReturnExpr
630 redef fun accept_typing
(v
)
632 var nexpr
= self.n_expr
633 var ret_type
= v
.mpropdef
.as(MMethodDef).msignature
.return_mtype
634 if nexpr
!= null then
635 if ret_type
!= null then
636 var mtype
= v
.visit_expr_subtype
(nexpr
, ret_type
)
638 var mtype
= v
.visit_expr
(nexpr
)
639 v
.error
(self, "Error: Return with value in a procedure.")
641 else if ret_type
!= null then
642 v
.error
(self, "Error: Return without value in a function.")
648 redef class AAbortExpr
649 redef fun accept_typing
(v
)
656 redef fun accept_typing
(v
)
658 v
.visit_expr_bool
(n_expr
)
666 redef class AIfexprExpr
667 redef fun accept_typing
(v
)
669 v
.visit_expr_bool
(n_expr
)
671 var t1
= v
.visit_expr
(n_then
)
672 var t2
= v
.visit_expr
(n_else
)
674 if t1
== null or t2
== null then
678 var t
= v
.merge_types
(self, [t1
, t2
])
680 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
687 redef fun accept_typing
(v
)
689 v
.visit_stmt
(n_block
)
694 redef class AWhileExpr
695 redef fun accept_typing
(v
)
697 v
.visit_expr_bool
(n_expr
)
699 v
.visit_stmt
(n_block
)
704 redef class ALoopExpr
705 redef fun accept_typing
(v
)
707 v
.visit_stmt
(n_block
)
713 var coltype
: nullable MGenericType
714 redef fun accept_typing
(v
)
716 var mtype
= v
.visit_expr
(n_expr
)
717 if mtype
== null then return
719 var colcla
= v
.get_mclass
(self, "Collection")
720 if colcla
== null then return
721 var objcla
= v
.get_mclass
(self, "Object")
722 if objcla
== null then return
723 var mapcla
= v
.get_mclass
(self, "Map")
724 if mapcla
== null then return
725 if v
.is_subtype
(mtype
, colcla
.get_mtype
([objcla
.mclass_type
.as_nullable
])) then
726 var coltype
= mtype
.supertype_to
(v
.mmodule
, v
.anchor
, colcla
)
727 assert coltype
isa MGenericType
728 self.coltype
= coltype
729 var variables
= self.variables
730 if variables
.length
!= 1 then
731 v
.error
(self, "Type Error: Expected one variable")
733 variables
.first
.declared_type
= coltype
.arguments
.first
735 else if v
.is_subtype
(mtype
, mapcla
.get_mtype
([objcla
.mclass_type
.as_nullable
, objcla
.mclass_type
.as_nullable
])) then
736 var coltype
= mtype
.supertype_to
(v
.mmodule
, v
.anchor
, mapcla
)
737 assert coltype
isa MGenericType
738 self.coltype
= coltype
739 var variables
= self.variables
740 if variables
.length
!= 2 then
741 v
.error
(self, "Type Error: Expected two variables")
743 variables
[0].declared_type
= coltype
.arguments
[0]
744 variables
[1].declared_type
= coltype
.arguments
[1]
747 v
.modelbuilder
.error
(self, "TODO: Do 'for' on {mtype}")
750 v
.visit_stmt
(n_block
)
755 redef class AAssertExpr
756 redef fun accept_typing
(v
)
758 v
.visit_expr_bool
(n_expr
)
766 redef fun accept_typing
(v
)
768 v
.visit_expr_bool
(n_expr
)
769 v
.visit_expr_bool
(n_expr2
)
770 self.mtype
= v
.type_bool
(self)
775 redef fun accept_typing
(v
)
777 v
.visit_expr_bool
(n_expr
)
778 v
.visit_expr_bool
(n_expr2
)
779 self.mtype
= v
.type_bool
(self)
785 redef fun accept_typing
(v
)
787 v
.visit_expr_bool
(n_expr
)
788 self.mtype
= v
.type_bool
(self)
792 redef class AOrElseExpr
793 redef fun accept_typing
(v
)
795 var t1
= v
.visit_expr
(n_expr
)
796 var t2
= v
.visit_expr
(n_expr2
)
798 if t1
== null or t2
== null then
802 if t1
isa MNullableType then
806 var t
= v
.merge_types
(self, [t1
, t2
])
808 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
815 redef fun accept_typing
(v
)
818 v
.visit_expr
(n_expr2
)
819 self.mtype
= v
.type_bool
(self)
823 redef class ATrueExpr
824 redef fun accept_typing
(v
)
826 self.mtype
= v
.type_bool
(self)
830 redef class AFalseExpr
831 redef fun accept_typing
(v
)
833 self.mtype
= v
.type_bool
(self)
838 redef fun accept_typing
(v
)
840 var mclass
= v
.get_mclass
(self, "Int")
841 if mclass
== null then return # Forward error
842 self.mtype
= mclass
.mclass_type
846 redef class AFloatExpr
847 redef fun accept_typing
(v
)
849 var mclass
= v
.get_mclass
(self, "Float")
850 if mclass
== null then return # Forward error
851 self.mtype
= mclass
.mclass_type
855 redef class ACharExpr
856 redef fun accept_typing
(v
)
858 var mclass
= v
.get_mclass
(self, "Char")
859 if mclass
== null then return # Forward error
860 self.mtype
= mclass
.mclass_type
864 redef class AStringFormExpr
865 redef fun accept_typing
(v
)
867 var mclass
= v
.get_mclass
(self, "String")
868 if mclass
== null then return # Forward error
869 self.mtype
= mclass
.mclass_type
873 redef class ASuperstringExpr
874 redef fun accept_typing
(v
)
876 var mclass
= v
.get_mclass
(self, "String")
877 if mclass
== null then return # Forward error
878 self.mtype
= mclass
.mclass_type
879 for nexpr
in self.n_exprs
do
880 var t
= v
.visit_expr
(nexpr
)
885 redef class AArrayExpr
886 redef fun accept_typing
(v
)
888 var mtypes
= new Array[nullable MType]
889 for e
in self.n_exprs
.n_exprs
do
890 var t
= v
.visit_expr
(e
)
896 var mtype
= v
.merge_types
(self, mtypes
)
897 if mtype
== null then
898 v
.error
(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
901 var mclass
= v
.get_mclass
(self, "Array")
902 if mclass
== null then return # Forward error
903 self.mtype
= mclass
.get_mtype
([mtype
])
907 redef class ARangeExpr
908 redef fun accept_typing
(v
)
910 var t1
= v
.visit_expr
(self.n_expr
)
911 var t2
= v
.visit_expr
(self.n_expr2
)
912 if t1
== null or t2
== null then return
913 var mclass
= v
.get_mclass
(self, "Range")
914 if mclass
== null then return # Forward error
915 if v
.is_subtype
(t1
, t2
) then
916 self.mtype
= mclass
.get_mtype
([t2
])
917 else if v
.is_subtype
(t2
, t1
) then
918 self.mtype
= mclass
.get_mtype
([t1
])
920 v
.error
(self, "Type Error: Cannot create range: {t1} vs {t2}")
925 redef class ANullExpr
926 redef fun accept_typing
(v
)
928 self.mtype
= v
.mmodule
.model
.null_type
933 # The static type to cast to.
934 # (different from the static type of the expression that is Bool).
935 var cast_type
: nullable MType
936 redef fun accept_typing
(v
)
938 var mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
939 self.cast_type
= mtype
941 var variable
= self.n_expr
.its_variable
942 if variable
!= null then
943 var orig
= self.n_expr
.mtype
944 var from
= if orig
!= null then orig
.to_s
else "invalid"
945 var to
= if mtype
!= null then mtype
.to_s
else "invalid"
946 #debug("adapt {variable}: {from} -> {to}")
947 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
950 self.mtype
= v
.type_bool
(self)
954 redef class AAsCastExpr
955 redef fun accept_typing
(v
)
957 self.mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
961 redef class AAsNotnullExpr
962 redef fun accept_typing
(v
)
964 var mtype
= v
.visit_expr
(self.n_expr
)
965 if mtype
isa MNullType then
966 v
.error
(self, "Type error: as(not null) on null")
969 if mtype
isa MNullableType then
970 self.mtype
= mtype
.mtype
973 # TODO: warn on useless as not null
978 redef class AProxyExpr
979 redef fun accept_typing
(v
)
981 self.mtype
= v
.visit_expr
(self.n_expr
)
985 redef class ASelfExpr
986 redef var its_variable
: nullable Variable
987 redef fun accept_typing
(v
)
989 var variable
= v
.selfvariable
990 self.its_variable
= variable
991 self.mtype
= v
.get_variable
(self, variable
)
995 ## MESSAGE SENDING AND PROPERTY
997 redef class ASendExpr
998 # The property invoked by the send.
999 var mproperty
: nullable MMethod
1001 redef fun accept_typing
(v
)
1003 var recvtype
= v
.visit_expr
(self.n_expr
)
1004 var name
= self.property_name
1006 if recvtype
== null then return # Forward error
1007 if recvtype
isa MNullType then
1008 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1012 var propdef
= v
.get_method
(self, recvtype
, name
, self.n_expr
isa ASelfExpr)
1013 if propdef
== null then return
1014 var mproperty
= propdef
.mproperty
1015 self.mproperty
= mproperty
1016 var msignature
= propdef
.msignature
1017 if msignature
== null then abort # Forward error
1019 var for_self
= self.n_expr
isa ASelfExpr
1020 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, for_self
)
1022 var args
= compute_raw_arguments
1024 v
.check_signature
(self, args
, name
, msignature
)
1026 if mproperty
.is_init
then
1027 var vmpropdef
= v
.mpropdef
1028 if not (vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_init
) then
1029 v
.error
(self, "Can call a init only in another init")
1033 var ret
= msignature
.return_mtype
1037 self.is_typed
= true
1040 if self.n_closure_defs
.length
== msignature
.mclosures
.length
then
1041 for i
in [0..self.n_closure_defs
.length
[ do
1042 self.n_closure_defs
[i
].accept_typing
(v
, msignature
.mclosures
[i
])
1045 debug
("closure: got {self.n_closure_defs.length}, want {msignature.mclosures.length}")
1049 # The name of the property
1050 # Each subclass simply provide the correct name.
1051 private fun property_name
: String is abstract
1053 # An array of all arguments (excluding self)
1054 fun compute_raw_arguments
: Array[AExpr] is abstract
1057 redef class ABinopExpr
1058 redef fun compute_raw_arguments
do return [n_expr2
]
1061 redef fun property_name
do return "=="
1062 redef fun accept_typing
(v
)
1066 var variable
= self.n_expr
.its_variable
1067 if variable
== null then return
1068 var mtype
= self.n_expr2
.mtype
1069 if not mtype
isa MNullType then return
1070 var vartype
= v
.get_variable
(self, variable
)
1071 if not vartype
isa MNullableType then return
1072 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1073 self.after_flow_context
.when_false
.set_var
(variable
, vartype
.mtype
)
1074 #debug("adapt {variable}:{vartype} ; true->{mtype} false->{vartype.mtype}")
1078 redef fun property_name
do return "!="
1079 redef fun accept_typing
(v
)
1083 var variable
= self.n_expr
.its_variable
1084 if variable
== null then return
1085 var mtype
= self.n_expr2
.mtype
1086 if not mtype
isa MNullType then return
1087 var vartype
= v
.get_variable
(self, variable
)
1088 if not vartype
isa MNullableType then return
1089 self.after_flow_context
.when_false
.set_var
(variable
, mtype
)
1090 self.after_flow_context
.when_true
.set_var
(variable
, vartype
.mtype
)
1091 #debug("adapt {variable}:{vartype} ; true->{vartype.mtype} false->{mtype}")
1095 redef fun property_name
do return "<"
1098 redef fun property_name
do return "<="
1101 redef fun property_name
do return "<<"
1104 redef fun property_name
do return ">"
1107 redef fun property_name
do return ">="
1110 redef fun property_name
do return ">>"
1112 redef class APlusExpr
1113 redef fun property_name
do return "+"
1115 redef class AMinusExpr
1116 redef fun property_name
do return "-"
1118 redef class AStarshipExpr
1119 redef fun property_name
do return "<=>"
1121 redef class AStarExpr
1122 redef fun property_name
do return "*"
1124 redef class ASlashExpr
1125 redef fun property_name
do return "/"
1127 redef class APercentExpr
1128 redef fun property_name
do return "%"
1131 redef class AUminusExpr
1132 redef fun property_name
do return "unary -"
1133 redef fun compute_raw_arguments
do return new Array[AExpr]
1137 redef class ACallExpr
1138 redef fun property_name
do return n_id
.text
1139 redef fun compute_raw_arguments
do return n_args
.to_a
1142 redef class ACallAssignExpr
1143 redef fun property_name
do return n_id
.text
+ "="
1144 redef fun compute_raw_arguments
1146 var res
= n_args
.to_a
1152 redef class ABraExpr
1153 redef fun property_name
do return "[]"
1154 redef fun compute_raw_arguments
do return n_args
.to_a
1157 redef class ABraAssignExpr
1158 redef fun property_name
do return "[]="
1159 redef fun compute_raw_arguments
1161 var res
= n_args
.to_a
1167 redef class ASendReassignFormExpr
1168 # The property invoked for the writing
1169 var write_mproperty
: nullable MMethod = null
1171 redef fun accept_typing
(v
)
1173 var recvtype
= v
.visit_expr
(self.n_expr
)
1174 var name
= self.property_name
1176 if recvtype
== null then return # Forward error
1177 if recvtype
isa MNullType then
1178 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1182 var propdef
= v
.get_method
(self, recvtype
, name
, self.n_expr
isa ASelfExpr)
1183 if propdef
== null then return
1184 var mproperty
= propdef
.mproperty
1185 self.mproperty
= mproperty
1186 var msignature
= propdef
.msignature
1187 if msignature
== null then abort # Forward error
1188 var for_self
= self.n_expr
isa ASelfExpr
1189 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, for_self
)
1191 var args
= compute_raw_arguments
1193 v
.check_signature
(self, args
, name
, msignature
)
1195 var readtype
= msignature
.return_mtype
1196 if readtype
== null then
1197 v
.error
(self, "Error: {name} is not a function")
1201 var wpropdef
= v
.get_method
(self, recvtype
, name
+ "=", self.n_expr
isa ASelfExpr)
1202 if wpropdef
== null then return
1203 var wmproperty
= wpropdef
.mproperty
1204 self.write_mproperty
= wmproperty
1205 var wmsignature
= wpropdef
.msignature
1206 if wmsignature
== null then abort # Forward error
1207 wmsignature
= v
.resolve_signature_for
(wmsignature
, recvtype
, for_self
)
1209 var wtype
= self.resolve_reassignment
(v
, readtype
, wmsignature
.mparameters
.last
.mtype
)
1210 if wtype
== null then return
1212 args
.add
(self.n_value
)
1213 v
.check_signature
(self, args
, name
+ "=", wmsignature
)
1215 self.is_typed
= true
1219 redef class ACallReassignExpr
1220 redef fun property_name
do return n_id
.text
1221 redef fun compute_raw_arguments
do return n_args
.to_a
1224 redef class ABraReassignExpr
1225 redef fun property_name
do return "[]"
1226 redef fun compute_raw_arguments
do return n_args
.to_a
1229 redef class AInitExpr
1230 redef fun property_name
do return "init"
1231 redef fun compute_raw_arguments
do return n_args
.to_a
1235 fun to_a
: Array[AExpr] do return self.n_exprs
.to_a
1240 redef class ASuperExpr
1241 # The method to call if the super is in fact a 'super init call'
1242 # Note: if the super is a normal call-next-method, then this attribute is null
1243 var mproperty
: nullable MMethod
1245 redef fun accept_typing
(v
)
1247 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1248 var mproperty
= v
.mpropdef
.mproperty
1249 if not mproperty
isa MMethod then
1250 v
.error
(self, "Error: super only usable in a method")
1253 var superprops
= mproperty
.lookup_super_definitions
(v
.mmodule
, recvtype
)
1254 if superprops
.length
== 0 then
1255 if mproperty
.is_init
and v
.mpropdef
.is_intro
then
1256 process_superinit
(v
)
1259 v
.error
(self, "Error: No super method to call for {mproperty}.")
1261 else if superprops
.length
> 1 then
1262 v
.modelbuilder
.warning
(self, "Error: Conflicting super method to call for {mproperty}: {superprops.join(", ")}.")
1265 var superprop
= superprops
.first
1266 assert superprop
isa MMethodDef
1268 var msignature
= superprop
.msignature
.as(not null)
1269 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, true)
1270 var args
= self.n_args
.to_a
1271 if args
.length
> 0 then
1272 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1274 self.mtype
= msignature
.return_mtype
1277 private fun process_superinit
(v
: TypeVisitor)
1279 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1280 var mproperty
= v
.mpropdef
.mproperty
1281 var superprop
: nullable MMethodDef = null
1282 for msupertype
in v
.nclassdef
.mclassdef
.supertypes
do
1283 msupertype
= msupertype
.anchor_to
(v
.mmodule
, recvtype
)
1284 var errcount
= v
.modelbuilder
.toolcontext
.error_count
1285 var candidate
= v
.try_get_mproperty_by_name2
(self, msupertype
, mproperty
.name
).as(nullable MMethod)
1286 if candidate
== null then
1287 if v
.modelbuilder
.toolcontext
.error_count
> errcount
then return # Forard error
1288 continue # Try next super-class
1290 if superprop
!= null and superprop
.mproperty
!= candidate
then
1291 v
.error
(self, "Error: conflicting super constructor to call for {mproperty}: {candidate.full_name}, {superprop.mproperty.full_name}")
1294 var candidatedefs
= candidate
.lookup_definitions
(v
.mmodule
, recvtype
)
1295 if superprop
!= null then
1296 if superprop
== candidatedefs
.first
then continue
1297 candidatedefs
.add
(superprop
)
1299 if candidatedefs
.length
> 1 then
1300 v
.error
(self, "Error: confliting property definitions for property {mproperty} in {recvtype}: {candidatedefs.join(", ")}")
1303 superprop
= candidatedefs
.first
1305 if superprop
== null then
1306 v
.error
(self, "Error: No super method to call for {mproperty}.")
1309 self.mproperty
= superprop
.mproperty
1311 var args
= self.n_args
.to_a
1312 var msignature
= superprop
.msignature
.as(not null)
1313 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, true)
1314 if args
.length
> 0 then
1315 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1317 # TODO: Check signature
1320 self.is_typed
= true
1326 redef class ANewExpr
1327 # The constructor invoked by the new.
1328 var mproperty
: nullable MMethod
1330 redef fun accept_typing
(v
)
1332 var recvtype
= v
.resolve_mtype
(self.n_type
)
1333 if recvtype
== null then return
1334 self.mtype
= recvtype
1336 if not recvtype
isa MClassType then
1337 if recvtype
isa MNullableType then
1338 v
.error
(self, "Type error: cannot instantiate the nullable type {recvtype}.")
1341 v
.error
(self, "Type error: cannot instantiate the formal type {recvtype}.")
1353 var propdef
= v
.get_method
(self, recvtype
, name
, false)
1354 if propdef
== null then return
1356 self.mproperty
= propdef
.mproperty
1358 if not propdef
.mproperty
.is_init_for
(recvtype
.mclass
) then
1359 v
.error
(self, "Error: {name} is not a constructor.")
1363 var msignature
= propdef
.msignature
.as(not null)
1364 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, false)
1366 var args
= n_args
.to_a
1367 v
.check_signature
(self, args
, name
, msignature
)
1373 redef class AAttrFormExpr
1374 # The attribute acceded.
1375 var mproperty
: nullable MAttribute
1377 # The static type of the attribute.
1378 var attr_type
: nullable MType
1380 # Resolve the attribute acceded.
1381 private fun resolve_property
(v
: TypeVisitor)
1383 var recvtype
= v
.visit_expr
(self.n_expr
)
1384 if recvtype
== null then return # Skip error
1385 var name
= self.n_id
.text
1386 if recvtype
isa MNullType then
1387 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1391 var unsafe_type
= v
.anchor_to
(recvtype
)
1392 var mproperty
= v
.try_get_mproperty_by_name2
(self, unsafe_type
, name
)
1393 if mproperty
== null then
1394 v
.modelbuilder
.error
(self, "Error: Attribute {name} doesn't exists in {recvtype}.")
1397 assert mproperty
isa MAttribute
1398 self.mproperty
= mproperty
1400 var mpropdefs
= mproperty
.lookup_definitions
(v
.mmodule
, unsafe_type
)
1401 assert mpropdefs
.length
== 1
1402 var mpropdef
= mpropdefs
.first
1403 var attr_type
= mpropdef
.static_mtype
.as(not null)
1404 attr_type
= v
.resolve_for
(attr_type
, recvtype
, self.n_expr
isa ASelfExpr)
1405 self.attr_type
= attr_type
1409 redef class AAttrExpr
1410 redef fun accept_typing
(v
)
1412 self.resolve_property
(v
)
1413 self.mtype
= self.attr_type
1418 redef class AAttrAssignExpr
1419 redef fun accept_typing
(v
)
1421 self.resolve_property
(v
)
1422 var mtype
= self.attr_type
1424 v
.visit_expr_subtype
(self.n_value
, mtype
)
1425 self.is_typed
= true
1429 redef class AAttrReassignExpr
1430 redef fun accept_typing
(v
)
1432 self.resolve_property
(v
)
1433 var mtype
= self.attr_type
1434 if mtype
== null then return # Skip error
1436 self.resolve_reassignment
(v
, mtype
, mtype
)
1438 self.is_typed
= true
1442 redef class AIssetAttrExpr
1443 redef fun accept_typing
(v
)
1445 self.resolve_property
(v
)
1446 var mtype
= self.attr_type
1447 if mtype
== null then return # Skip error
1449 var recvtype
= self.n_expr
.mtype
.as(not null)
1450 var bound
= v
.resolve_for
(mtype
, recvtype
, false)
1451 if bound
isa MNullableType then
1452 v
.error
(self, "Error: isset on a nullable attribute.")
1454 self.mtype
= v
.type_bool
(self)
1460 redef class AClosureCallExpr
1461 redef fun accept_typing
(v
)
1463 var variable
= self.variable
1464 if variable
== null then return # Skip error
1466 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1467 var msignature
= variable
.declared_type
.as(MSignature)
1468 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, false)
1470 var args
= n_args
.to_a
1471 v
.check_signature
(self, args
, variable
.name
, msignature
)
1473 self.is_typed
= true
1474 self.mtype
= msignature
.return_mtype
1478 redef class AClosureDef
1479 var mclosure
: nullable MParameter
1481 private fun accept_typing
(v
: TypeVisitor, mparameter
: MParameter)
1483 var variables
= self.variables
1484 if variables
== null then return
1486 self.mclosure
= mparameter
1487 var msignature
= mparameter
.mtype
.as(MSignature)
1489 if msignature
.arity
!= variables
.length
then
1490 v
.error
(self, "Type error: closure {mparameter.name} expects {msignature.arity} parameters, {variables.length} given")
1494 for i
in [0..variables
.length
[ do
1495 variables
[i
].declared_type
= msignature
.mparameters
[i
].mtype
1498 v
.visit_stmt
(self.n_expr
)
1504 redef class ADebugTypeExpr
1505 redef fun accept_typing
(v
)
1507 var expr
= v
.visit_expr
(self.n_expr
)
1508 if expr
== null then return
1509 var unsafe
= v
.anchor_to
(expr
)
1510 var ntype
= self.n_type
1511 var mtype
= v
.resolve_mtype
(ntype
)
1512 if mtype
!= null and mtype
!= expr
then
1513 var umtype
= v
.anchor_to
(mtype
)
1514 v
.modelbuilder
.warning
(self, "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")