1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Intraprocedural resolution of static types and OO-services
18 # By OO-services we mean message sending, attribute access, instantiation, etc.
24 private class TypeVisitor
25 var modelbuilder
: ModelBuilder
26 var nclassdef
: AClassdef
27 var mpropdef
: MPropDef
29 var selfvariable
: Variable = new Variable("self")
31 init(modelbuilder
: ModelBuilder, nclassdef
: AClassdef, mpropdef
: MPropDef)
33 self.modelbuilder
= modelbuilder
34 self.nclassdef
= nclassdef
35 self.mpropdef
= mpropdef
37 var mclass
= nclassdef
.mclassdef
.mclass
39 var selfvariable
= new Variable("self")
40 self.selfvariable
= selfvariable
41 selfvariable
.declared_type
= mclass
.mclass_type
44 fun mmodule
: MModule do return self.nclassdef
.mclassdef
.mmodule
46 fun anchor
: MClassType do return self.nclassdef
.mclassdef
.bound_mtype
48 fun anchor_to
(mtype
: MType): MType
50 var mmodule
= self.nclassdef
.mclassdef
.mmodule
51 var anchor
= self.nclassdef
.mclassdef
.bound_mtype
52 return mtype
.anchor_to
(mmodule
, anchor
)
55 fun is_subtype
(sub
, sup
: MType): Bool
57 var mmodule
= self.nclassdef
.mclassdef
.mmodule
58 var anchor
= self.nclassdef
.mclassdef
.bound_mtype
59 return sub
.is_subtype
(mmodule
, anchor
, sup
)
62 fun resolve_for
(mtype
, subtype
: MType, for_self
: Bool): MType
64 var mmodule
= self.nclassdef
.mclassdef
.mmodule
65 var anchor
= self.nclassdef
.mclassdef
.bound_mtype
66 #print "resolve_for {mtype} sub={subtype} forself={for_self} mmodule={mmodule} anchor={anchor}"
67 var res
= mtype
.resolve_for
(subtype
, anchor
, mmodule
, not for_self
)
71 # Retrieve the signature of a MMethodDef resolved for a specific call.
72 # This method is an helper to symplify the query on the model.
74 # Note: `for_self` indicates if the reciever is self or not.
75 # If yes, virtual types are not resolved.
76 fun resolve_signature_for
(mmethoddef
: MMethodDef, recv
: MType, for_self
: Bool): MSignature
78 return self.resolve_for
(mmethoddef
.msignature
.as(not null), recv
, for_self
).as(MSignature)
81 fun check_subtype
(node
: ANode, sub
, sup
: MType): Bool
83 if self.is_subtype
(sub
, sup
) then return true
84 if self.is_subtype
(sub
, self.anchor_to
(sup
)) then
85 # FIXME workarround to the current unsafe typing policy. To remove once fixed virtual types exists.
86 #node.debug("Unsafe typing: expected {sup}, got {sub}")
89 self.modelbuilder
.error
(node
, "Type error: expected {sup}, got {sub}")
93 # Visit an expression and do not care about the return value
94 fun visit_stmt
(nexpr
: nullable AExpr)
96 if nexpr
== null then return
97 nexpr
.accept_typing
(self)
100 # Visit an expression and expects that it is not a statement
101 # Return the type of the expression
102 # Display an error and return null if:
103 # * the type cannot be determined or
104 # * `nexpr' is a statement
105 fun visit_expr
(nexpr
: AExpr): nullable MType
107 nexpr
.accept_typing
(self)
108 var mtype
= nexpr
.mtype
109 if mtype
!= null then return mtype
110 if not nexpr
.is_typed
then
111 if not self.modelbuilder
.toolcontext
.error_count
> 0 then # check that there is really an error
112 if self.modelbuilder
.toolcontext
.verbose_level
> 1 then
113 nexpr
.debug
("No return type but no error.")
116 return null # forward error
118 self.error
(nexpr
, "Type error: expected expression.")
122 # Visit an expression and expect its static type is a least a `sup'
123 # Return the type of the expression
124 # * the type cannot be determined or
125 # * `nexpr' is a statement
126 # * `nexpt' is not a `sup'
127 fun visit_expr_subtype
(nexpr
: AExpr, sup
: nullable MType): nullable MType
129 var sub
= visit_expr
(nexpr
)
130 if sub
== null then return null # Forward error
132 if sup
== null then return null # Forward error
134 if not check_subtype
(nexpr
, sub
, sup
) then
140 # Visit an expression and expect its static type is a bool
141 # Return the type of the expression
142 # * the type cannot be determined or
143 # * `nexpr' is a statement
144 # * `nexpt' is not a `sup'
145 fun visit_expr_bool
(nexpr
: AExpr): nullable MType
147 return self.visit_expr_subtype
(nexpr
, self.type_bool
(nexpr
))
151 private fun visit_expr_cast
(node
: ANode, nexpr
: AExpr, ntype
: AType): nullable MType
153 var sub
= visit_expr
(nexpr
)
154 if sub
== null then return null # Forward error
156 var sup
= self.resolve_mtype
(ntype
)
157 if sup
== null then return null # Forward error
159 var mmodule
= self.nclassdef
.mclassdef
.mmodule
160 var anchor
= self.nclassdef
.mclassdef
.bound_mtype
162 self.modelbuilder
.warning
(node
, "Warning: Expression is already a {sup}.")
163 else if self.is_subtype
(sub
, sup
) and not sup
.need_anchor
then
164 self.modelbuilder
.warning
(node
, "Warning: Expression is already a {sup} since it is a {sub}.")
169 fun try_get_mproperty_by_name2
(anode
: ANode, mtype
: MType, name
: String): nullable MProperty
171 return self.modelbuilder
.try_get_mproperty_by_name2
(anode
, self.nclassdef
.mclassdef
.mmodule
, mtype
, name
)
174 fun resolve_mtype
(node
: AType): nullable MType
176 return self.modelbuilder
.resolve_mtype
(self.nclassdef
, node
)
179 fun try_get_mclass
(node
: ANode, name
: String): nullable MClass
181 var mmodule
= self.nclassdef
.mclassdef
.mmodule
182 var mclass
= modelbuilder
.try_get_mclass_by_name
(node
, mmodule
, name
)
186 fun get_mclass
(node
: ANode, name
: String): nullable MClass
188 var mmodule
= self.nclassdef
.mclassdef
.mmodule
189 var mclass
= modelbuilder
.try_get_mclass_by_name
(node
, mmodule
, name
)
190 if mclass
== null then
191 self.modelbuilder
.error
(node
, "Type Error: missing primitive class `{name}'.")
196 fun type_bool
(node
: ANode): nullable MType
198 var mclass
= self.get_mclass
(node
, "Bool")
199 if mclass
== null then return null
200 return mclass
.mclass_type
203 fun get_method
(node
: ANode, recvtype
: MType, name
: String, recv_is_self
: Bool): nullable MMethodDef
205 var unsafe_type
= self.anchor_to
(recvtype
)
207 #debug("recv: {recvtype} (aka {unsafe_type})")
209 var mproperty
= self.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
210 if mproperty
== null then
211 #self.modelbuilder.error(node, "Type error: property {name} not found in {unsafe_type} (ie {recvtype})")
213 self.modelbuilder
.error
(node
, "Error: Method or variable '{name}' unknown in {recvtype}.")
215 self.modelbuilder
.error
(node
, "Error: Method '{name}' doesn't exists in {recvtype}.")
220 if mproperty
.visibility
== protected_visibility
and not recv_is_self
and self.mmodule
.visibility_for
(mproperty
.intro_mclassdef
.mmodule
) < intrude_visibility
then
221 self.modelbuilder
.error
(node
, "Error: Method '{name}' is protected and can only acceded by self. {mproperty.intro_mclassdef.mmodule.visibility_for(self.mmodule)}")
225 var propdefs
= mproperty
.lookup_definitions
(self.mmodule
, unsafe_type
)
226 if propdefs
.length
== 0 then
227 self.modelbuilder
.error
(node
, "Type error: no definition found for property {name} in {unsafe_type}")
229 else if propdefs
.length
> 1 then
230 self.modelbuilder
.error
(node
, "Error: confliting property definitions for property {name} in {unsafe_type}: {propdefs.join(" ")}")
234 var propdef
= propdefs
.first
235 assert propdef
isa MMethodDef
239 # Visit the expressions of args and cheik their conformity with the corresponding typi in signature
240 # The point of this method is to handle varargs correctly
241 # Note: The signature must be correctly adapted
242 fun check_signature
(node
: ANode, args
: Array[AExpr], name
: String, msignature
: MSignature): Bool
244 var vararg_rank
= msignature
.vararg_rank
245 if vararg_rank
>= 0 then
246 if args
.length
< msignature
.arity
then
247 #self.modelbuilder.error(node, "Error: Incorrect number of parameters. Got {args.length}, expected at least {msignature.arity}. Signature is {msignature}")
248 self.modelbuilder
.error
(node
, "Error: arity mismatch; prototype is '{name}{msignature}'")
251 else if args
.length
!= msignature
.arity
then
252 self.modelbuilder
.error
(node
, "Error: Incorrect number of parameters. Got {args.length}, expected {msignature.arity}. Signature is {msignature}")
256 #debug("CALL {unsafe_type}.{msignature}")
258 var vararg_decl
= args
.length
- msignature
.arity
259 for i
in [0..msignature
.arity
[ do
261 if i
== vararg_rank
then continue # skip the vararg
262 if i
> vararg_rank
then
265 var paramtype
= msignature
.mparameters
[i
].mtype
266 self.visit_expr_subtype
(args
[j
], paramtype
)
268 if vararg_rank
>= 0 then
269 var varargs
= new Array[AExpr]
270 var paramtype
= msignature
.mparameters
[vararg_rank
].mtype
271 for j
in [vararg_rank
..vararg_rank
+vararg_decl
] do
273 self.visit_expr_subtype
(args
[j
], paramtype
)
279 fun error
(node
: ANode, message
: String)
281 self.modelbuilder
.toolcontext
.error
(node
.hot_location
, message
)
284 fun get_variable
(node
: AExpr, variable
: Variable): nullable MType
286 var flow
= node
.after_flow_context
288 self.error
(node
, "No context!")
292 if flow
.vars
.has_key
(variable
) then
293 return flow
.vars
[variable
]
295 #node.debug("*** START Collected for {variable}")
296 var mtypes
= flow
.collect_types
(variable
)
297 #node.debug("**** END Collected for {variable}")
298 if mtypes
== null or mtypes
.length
== 0 then
299 return variable
.declared_type
300 else if mtypes
.length
== 1 then
303 var res
= merge_types
(node
,mtypes
)
304 if res
== null then res
= variable
.declared_type
310 fun set_variable
(node
: AExpr, variable
: Variable, mtype
: nullable MType)
312 var flow
= node
.after_flow_context
315 flow
.set_var
(variable
, mtype
)
318 fun merge_types
(node
: ANode, col
: Array[nullable MType]): nullable MType
320 if col
.length
== 1 then return col
.first
321 var res
= new Array[nullable MType]
323 if t1
== null then continue # return null
326 if t2
== null then continue # return null
327 if t2
isa MNullableType or t2
isa MNullType then
330 if not is_subtype
(t2
, t1
) then found
= false
333 #print "merge {col.join(" ")} -> {t1}"
337 #self.modelbuilder.warning(node, "Type Error: {col.length} conflicting types: <{col.join(", ")}>")
343 # The declared type of the variable
344 var declared_type
: nullable MType
347 redef class FlowContext
348 # Store changes of types because of type evolution
349 private var vars
: HashMap[Variable, nullable MType] = new HashMap[Variable, nullable MType]
350 private var cache
: HashMap[Variable, nullable Array[nullable MType]] = new HashMap[Variable, nullable Array[nullable MType]]
352 # Adapt the variable to a static type
353 # Warning1: do not modify vars directly.
354 # Warning2: sub-flow may have cached a unadapted variabial
355 private fun set_var
(variable
: Variable, mtype
: nullable MType)
357 self.vars
[variable
] = mtype
358 self.cache
.keys
.remove
(variable
)
361 private fun collect_types
(variable
: Variable): nullable Array[nullable MType]
363 if cache
.has_key
(variable
) then
364 return cache
[variable
]
366 var res
: nullable Array[nullable MType] = null
367 if vars
.has_key
(variable
) then
368 var mtype
= vars
[variable
]
370 else if self.previous
.is_empty
then
372 res
= [variable
.declared_type
]
374 for flow
in self.previous
do
375 if flow
.is_unreachable
then continue
376 var r2
= flow
.collect_types
(variable
)
377 if r2
== null then continue
382 if not res
.has
(t
) then res
.add
(t
)
387 cache
[variable
] = res
393 # The entry point of the whole typing analysis
394 fun do_typing
(modelbuilder
: ModelBuilder)
398 # The variable associated to the reciever (if any)
399 var selfvariable
: nullable Variable
402 redef class AConcreteMethPropdef
403 redef fun do_typing
(modelbuilder
: ModelBuilder)
405 var nclassdef
= self.parent
.as(AClassdef)
406 var mpropdef
= self.mpropdef
.as(not null)
407 var v
= new TypeVisitor(modelbuilder
, nclassdef
, mpropdef
)
408 self.selfvariable
= v
.selfvariable
410 var nblock
= self.n_block
411 if nblock
== null then return
413 var mmethoddef
= self.mpropdef
.as(not null)
414 for i
in [0..mmethoddef
.msignature
.arity
[ do
415 var mtype
= mmethoddef
.msignature
.mparameters
[i
].mtype
416 if mmethoddef
.msignature
.vararg_rank
== i
then
417 var arrayclass
= v
.get_mclass
(self.n_signature
.n_params
[i
], "Array")
418 if arrayclass
== null then return # Skip error
419 mtype
= arrayclass
.get_mtype
([mtype
])
421 var variable
= self.n_signature
.n_params
[i
].variable
422 assert variable
!= null
423 variable
.declared_type
= mtype
425 for i
in [0..mmethoddef
.msignature
.mclosures
.length
[ do
426 var mclosure
= mmethoddef
.msignature
.mclosures
[i
]
427 var variable
= self.n_signature
.n_closure_decls
[i
].variable
428 assert variable
!= null
429 variable
.declared_type
= mclosure
.mtype
433 if not nblock
.after_flow_context
.is_unreachable
and mmethoddef
.msignature
.return_mtype
!= null then
434 # We reach the end of the function without having a return, it is bad
435 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
440 redef class AAttrPropdef
441 redef fun do_typing
(modelbuilder
: ModelBuilder)
443 var nclassdef
= self.parent
.as(AClassdef)
444 var v
= new TypeVisitor(modelbuilder
, nclassdef
, self.mpropdef
.as(not null))
445 self.selfvariable
= v
.selfvariable
447 var nexpr
= self.n_expr
448 if nexpr
!= null then
449 var mtype
= self.mpropdef
.static_mtype
450 v
.visit_expr_subtype
(nexpr
, mtype
)
458 # The static type of the expression.
459 # null if self is a statement of in case of error
460 var mtype
: nullable MType = null
462 # Is the statement correctly typed?
463 # Used to distinguish errors and statements when `mtype' == null
464 var is_typed
: Bool = false
466 # Return the variable read (if any)
467 # Used to perform adaptive typing
468 fun its_variable
: nullable Variable do return null
470 private fun accept_typing
(v
: TypeVisitor)
472 v
.error
(self, "no implemented accept_typing for {self.class_name}")
476 redef class ABlockExpr
477 redef fun accept_typing
(v
)
479 for e
in self.n_expr
do v
.visit_stmt
(e
)
484 redef class AVardeclExpr
485 redef fun accept_typing
(v
)
487 var variable
= self.variable
488 if variable
== null then return # Skip error
490 var ntype
= self.n_type
491 var mtype
: nullable MType
492 if ntype
== null then
495 mtype
= v
.resolve_mtype
(ntype
)
496 if mtype
== null then return # Skip error
499 var nexpr
= self.n_expr
500 if nexpr
!= null then
501 if mtype
!= null then
502 v
.visit_expr_subtype
(nexpr
, mtype
)
504 mtype
= v
.visit_expr
(nexpr
)
505 if mtype
== null then return # Skip error
509 if mtype
== null then
510 mtype
= v
.get_mclass
(self, "Object").mclass_type
.as_nullable
513 variable
.declared_type
= mtype
514 v
.set_variable
(self, variable
, mtype
)
516 #debug("var {variable}: {mtype}")
523 redef fun its_variable
do return self.variable
524 redef fun accept_typing
(v
)
526 var variable
= self.variable
527 if variable
== null then return # Skip error
529 var mtype
= v
.get_variable
(self, variable
)
530 if mtype
!= null then
531 #debug("{variable} is {mtype}")
533 #debug("{variable} is untyped")
540 redef class AVarAssignExpr
541 redef fun accept_typing
(v
)
543 var variable
= self.variable
544 assert variable
!= null
546 var mtype
= v
.visit_expr_subtype
(n_value
, variable
.declared_type
)
548 v
.set_variable
(self, variable
, mtype
)
554 redef class AReassignFormExpr
555 # The method designed by the reassign operator.
556 var reassign_property
: nullable MMethodDef = null
558 var read_type
: nullable MType = null
560 # Determine the `reassign_property'
561 # `readtype' is the type of the reading of the left value.
562 # `writetype' is the type of the writing of the left value.
563 # (Because of ACallReassignExpr, both can be different.
564 # Return the static type of the value to store.
565 private fun resolve_reassignment
(v
: TypeVisitor, readtype
, writetype
: MType): nullable MType
567 var reassign_name
: String
568 if self.n_assign_op
isa APlusAssignOp then
570 else if self.n_assign_op
isa AMinusAssignOp then
576 self.read_type
= readtype
578 if readtype
isa MNullType then
579 v
.error
(self, "Error: Method '{reassign_name}' call on 'null'.")
583 var mpropdef
= v
.get_method
(self, readtype
, reassign_name
, false)
584 if mpropdef
== null then return null # Skip error
586 self.reassign_property
= mpropdef
588 var msignature
= v
.resolve_signature_for
(mpropdef
, readtype
, false)
590 var rettype
= msignature
.return_mtype
591 assert msignature
.arity
== 1 and rettype
!= null
593 var value_type
= v
.visit_expr_subtype
(self.n_value
, msignature
.mparameters
.first
.mtype
)
594 if value_type
== null then return null # Skip error
596 v
.check_subtype
(self, rettype
, writetype
)
601 redef class AVarReassignExpr
602 redef fun accept_typing
(v
)
604 var variable
= self.variable
605 assert variable
!= null
607 var readtype
= v
.get_variable
(self, variable
)
608 if readtype
== null then return
610 var writetype
= variable
.declared_type
611 if writetype
== null then return
613 var rettype
= self.resolve_reassignment
(v
, readtype
, writetype
)
615 v
.set_variable
(self, variable
, rettype
)
622 redef class AContinueExpr
623 redef fun accept_typing
(v
)
625 var nexpr
= self.n_expr
626 if nexpr
!= null then
627 var mtype
= v
.visit_expr
(nexpr
)
633 redef class ABreakExpr
634 redef fun accept_typing
(v
)
636 var nexpr
= self.n_expr
637 if nexpr
!= null then
638 var mtype
= v
.visit_expr
(nexpr
)
644 redef class AReturnExpr
645 redef fun accept_typing
(v
)
647 var nexpr
= self.n_expr
648 var ret_type
= v
.mpropdef
.as(MMethodDef).msignature
.return_mtype
649 if nexpr
!= null then
650 if ret_type
!= null then
651 var mtype
= v
.visit_expr_subtype
(nexpr
, ret_type
)
653 var mtype
= v
.visit_expr
(nexpr
)
654 v
.error
(self, "Error: Return with value in a procedure.")
656 else if ret_type
!= null then
657 v
.error
(self, "Error: Return without value in a function.")
663 redef class AAbortExpr
664 redef fun accept_typing
(v
)
671 redef fun accept_typing
(v
)
673 v
.visit_expr_bool
(n_expr
)
681 redef class AIfexprExpr
682 redef fun accept_typing
(v
)
684 v
.visit_expr_bool
(n_expr
)
686 var t1
= v
.visit_expr
(n_then
)
687 var t2
= v
.visit_expr
(n_else
)
689 if t1
== null or t2
== null then
693 var t
= v
.merge_types
(self, [t1
, t2
])
695 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
702 redef fun accept_typing
(v
)
704 v
.visit_stmt
(n_block
)
709 redef class AWhileExpr
710 redef fun accept_typing
(v
)
712 v
.visit_expr_bool
(n_expr
)
714 v
.visit_stmt
(n_block
)
719 redef class ALoopExpr
720 redef fun accept_typing
(v
)
722 v
.visit_stmt
(n_block
)
728 var coltype
: nullable MClassType
730 var method_iterator
: nullable MMethod
731 var method_is_ok
: nullable MMethod
732 var method_item
: nullable MMethod
733 var method_next
: nullable MMethod
734 var method_key
: nullable MMethod
736 private fun do_type_iterator
(v
: TypeVisitor, mtype
: MType)
738 var objcla
= v
.get_mclass
(self, "Object")
739 if objcla
== null then return
744 var colcla
= v
.try_get_mclass
(self, "Collection")
745 if colcla
!= null and v
.is_subtype
(mtype
, colcla
.get_mtype
([objcla
.mclass_type
.as_nullable
])) then
746 var coltype
= mtype
.supertype_to
(v
.mmodule
, v
.anchor
, colcla
)
747 self.coltype
= coltype
748 var variables
= self.variables
749 if variables
.length
!= 1 then
750 v
.error
(self, "Type Error: Expected one variable")
752 variables
.first
.declared_type
= coltype
.arguments
.first
757 var mapcla
= v
.try_get_mclass
(self, "Map")
758 if mapcla
!= null and v
.is_subtype
(mtype
, mapcla
.get_mtype
([objcla
.mclass_type
.as_nullable
, objcla
.mclass_type
.as_nullable
])) then
759 var coltype
= mtype
.supertype_to
(v
.mmodule
, v
.anchor
, mapcla
)
760 self.coltype
= coltype
761 var variables
= self.variables
762 if variables
.length
!= 2 then
763 v
.error
(self, "Type Error: Expected two variables")
765 variables
[0].declared_type
= coltype
.arguments
[0]
766 variables
[1].declared_type
= coltype
.arguments
[1]
771 if is_col
or is_map
then
772 # get iterator method
773 var coltype
= self.coltype
.as(not null)
774 var itdef
= v
.get_method
(self, coltype
, "iterator", true)
775 if itdef
== null then
776 v
.error
(self, "Type Error: Expected method 'iterator' in type {coltype}")
779 self.method_iterator
= itdef
.mproperty
782 var ittype
= v
.resolve_signature_for
(itdef
, mtype
, false).return_mtype
783 if ittype
== null then
784 v
.error
(self, "Type Error: Expected method 'iterator' to return an Iterator type")
788 # get methods is_ok, next, item
789 var ikdef
= v
.get_method
(self, ittype
, "is_ok", false)
790 if ikdef
== null then
791 v
.error
(self, "Type Error: Expected method 'is_ok' in Iterator type {ittype}")
794 self.method_is_ok
= ikdef
.mproperty
796 var itemdef
= v
.get_method
(self, ittype
, "item", false)
797 if itemdef
== null then
798 v
.error
(self, "Type Error: Expected method 'item' in Iterator type {ittype}")
801 self.method_item
= itemdef
.mproperty
803 var nextdef
= v
.get_method
(self, ittype
, "next", false)
804 if nextdef
== null then
805 v
.error
(self, "Type Error: Expected method 'next' in Iterator type {ittype}")
808 self.method_next
= nextdef
.mproperty
811 var keydef
= v
.get_method
(self, ittype
, "key", false)
812 if keydef
== null then
813 v
.error
(self, "Type Error: Expected method 'key' in Iterator type {ittype}")
816 self.method_key
= keydef
.mproperty
821 v
.modelbuilder
.error
(self, "NOT YET IMPLEMENTED: Do 'for' on {mtype}")
824 redef fun accept_typing
(v
)
826 var mtype
= v
.visit_expr
(n_expr
)
827 if mtype
== null then return
829 self.do_type_iterator
(v
, mtype
)
831 v
.visit_stmt
(n_block
)
836 redef class AAssertExpr
837 redef fun accept_typing
(v
)
839 v
.visit_expr_bool
(n_expr
)
847 redef fun accept_typing
(v
)
849 v
.visit_expr_bool
(n_expr
)
850 v
.visit_expr_bool
(n_expr2
)
851 self.mtype
= v
.type_bool
(self)
856 redef fun accept_typing
(v
)
858 v
.visit_expr_bool
(n_expr
)
859 v
.visit_expr_bool
(n_expr2
)
860 self.mtype
= v
.type_bool
(self)
866 redef fun accept_typing
(v
)
868 v
.visit_expr_bool
(n_expr
)
869 self.mtype
= v
.type_bool
(self)
873 redef class AOrElseExpr
874 redef fun accept_typing
(v
)
876 var t1
= v
.visit_expr
(n_expr
)
877 var t2
= v
.visit_expr
(n_expr2
)
879 if t1
== null or t2
== null then
883 if t1
isa MNullableType then
887 var t
= v
.merge_types
(self, [t1
, t2
])
889 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
896 redef fun accept_typing
(v
)
899 v
.visit_expr
(n_expr2
)
900 self.mtype
= v
.type_bool
(self)
904 redef class ATrueExpr
905 redef fun accept_typing
(v
)
907 self.mtype
= v
.type_bool
(self)
911 redef class AFalseExpr
912 redef fun accept_typing
(v
)
914 self.mtype
= v
.type_bool
(self)
919 redef fun accept_typing
(v
)
921 var mclass
= v
.get_mclass
(self, "Int")
922 if mclass
== null then return # Forward error
923 self.mtype
= mclass
.mclass_type
927 redef class AFloatExpr
928 redef fun accept_typing
(v
)
930 var mclass
= v
.get_mclass
(self, "Float")
931 if mclass
== null then return # Forward error
932 self.mtype
= mclass
.mclass_type
936 redef class ACharExpr
937 redef fun accept_typing
(v
)
939 var mclass
= v
.get_mclass
(self, "Char")
940 if mclass
== null then return # Forward error
941 self.mtype
= mclass
.mclass_type
945 redef class AStringFormExpr
946 redef fun accept_typing
(v
)
948 var mclass
= v
.get_mclass
(self, "String")
949 if mclass
== null then return # Forward error
950 self.mtype
= mclass
.mclass_type
954 redef class ASuperstringExpr
955 redef fun accept_typing
(v
)
957 var mclass
= v
.get_mclass
(self, "String")
958 if mclass
== null then return # Forward error
959 self.mtype
= mclass
.mclass_type
960 for nexpr
in self.n_exprs
do
961 var t
= v
.visit_expr
(nexpr
)
966 redef class AArrayExpr
967 redef fun accept_typing
(v
)
969 var mtypes
= new Array[nullable MType]
970 for e
in self.n_exprs
.n_exprs
do
971 var t
= v
.visit_expr
(e
)
977 var mtype
= v
.merge_types
(self, mtypes
)
978 if mtype
== null then
979 v
.error
(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
982 var mclass
= v
.get_mclass
(self, "Array")
983 if mclass
== null then return # Forward error
984 self.mtype
= mclass
.get_mtype
([mtype
])
988 redef class ARangeExpr
989 redef fun accept_typing
(v
)
991 var discrete_class
= v
.get_mclass
(self, "Discrete")
992 if discrete_class
== null then return # Forward error
993 var discrete_type
= discrete_class
.intro
.bound_mtype
994 var t1
= v
.visit_expr_subtype
(self.n_expr
, discrete_type
)
995 var t2
= v
.visit_expr_subtype
(self.n_expr2
, discrete_type
)
996 if t1
== null or t2
== null then return
997 var mclass
= v
.get_mclass
(self, "Range")
998 if mclass
== null then return # Forward error
999 if v
.is_subtype
(t1
, t2
) then
1000 self.mtype
= mclass
.get_mtype
([t2
])
1001 else if v
.is_subtype
(t2
, t1
) then
1002 self.mtype
= mclass
.get_mtype
([t1
])
1004 v
.error
(self, "Type Error: Cannot create range: {t1} vs {t2}")
1009 redef class ANullExpr
1010 redef fun accept_typing
(v
)
1012 self.mtype
= v
.mmodule
.model
.null_type
1016 redef class AIsaExpr
1017 # The static type to cast to.
1018 # (different from the static type of the expression that is Bool).
1019 var cast_type
: nullable MType
1020 redef fun accept_typing
(v
)
1022 var mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
1023 self.cast_type
= mtype
1025 var variable
= self.n_expr
.its_variable
1026 if variable
!= null then
1027 var orig
= self.n_expr
.mtype
1028 var from
= if orig
!= null then orig
.to_s
else "invalid"
1029 var to
= if mtype
!= null then mtype
.to_s
else "invalid"
1030 #debug("adapt {variable}: {from} -> {to}")
1031 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1034 self.mtype
= v
.type_bool
(self)
1038 redef class AAsCastExpr
1039 redef fun accept_typing
(v
)
1041 self.mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
1045 redef class AAsNotnullExpr
1046 redef fun accept_typing
(v
)
1048 var mtype
= v
.visit_expr
(self.n_expr
)
1049 if mtype
isa MNullType then
1050 v
.error
(self, "Type error: as(not null) on null")
1053 if mtype
isa MNullableType then
1054 self.mtype
= mtype
.mtype
1057 # TODO: warn on useless as not null
1062 redef class AProxyExpr
1063 redef fun accept_typing
(v
)
1065 self.mtype
= v
.visit_expr
(self.n_expr
)
1069 redef class ASelfExpr
1070 redef var its_variable
: nullable Variable
1071 redef fun accept_typing
(v
)
1073 var variable
= v
.selfvariable
1074 self.its_variable
= variable
1075 self.mtype
= v
.get_variable
(self, variable
)
1079 ## MESSAGE SENDING AND PROPERTY
1081 redef class ASendExpr
1082 # The property invoked by the send.
1083 var mproperty
: nullable MMethod
1085 redef fun accept_typing
(v
)
1087 var recvtype
= v
.visit_expr
(self.n_expr
)
1088 var name
= self.property_name
1090 if recvtype
== null then return # Forward error
1091 if recvtype
isa MNullType then
1092 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1096 var mpropdef
= v
.get_method
(self, recvtype
, name
, self.n_expr
isa ASelfExpr)
1097 if mpropdef
== null then return
1098 var mproperty
= mpropdef
.mproperty
1099 self.mproperty
= mproperty
1100 var msignature
= v
.resolve_signature_for
(mpropdef
, recvtype
, self.n_expr
isa ASelfExpr)
1102 var args
= compute_raw_arguments
1103 self.raw_arguments
= args
1105 v
.check_signature
(self, args
, name
, msignature
)
1107 if mproperty
.is_init
then
1108 var vmpropdef
= v
.mpropdef
1109 if not (vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_init
) then
1110 v
.error
(self, "Can call a init only in another init")
1114 var ret
= msignature
.return_mtype
1118 self.is_typed
= true
1121 if self.n_closure_defs
.length
== msignature
.mclosures
.length
then
1122 for i
in [0..self.n_closure_defs
.length
[ do
1123 self.n_closure_defs
[i
].accept_typing
(v
, msignature
.mclosures
[i
])
1126 debug
("closure: got {self.n_closure_defs.length}, want {msignature.mclosures.length}")
1130 # The name of the property
1131 # Each subclass simply provide the correct name.
1132 private fun property_name
: String is abstract
1134 # An array of all arguments (excluding self)
1135 var raw_arguments
: nullable Array[AExpr]
1137 private fun compute_raw_arguments
: Array[AExpr] is abstract
1140 redef class ABinopExpr
1141 redef fun compute_raw_arguments
do return [n_expr2
]
1144 redef fun property_name
do return "=="
1145 redef fun accept_typing
(v
)
1149 var variable
= self.n_expr
.its_variable
1150 if variable
== null then return
1151 var mtype
= self.n_expr2
.mtype
1152 if not mtype
isa MNullType then return
1153 var vartype
= v
.get_variable
(self, variable
)
1154 if not vartype
isa MNullableType then return
1155 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1156 self.after_flow_context
.when_false
.set_var
(variable
, vartype
.mtype
)
1157 #debug("adapt {variable}:{vartype} ; true->{mtype} false->{vartype.mtype}")
1161 redef fun property_name
do return "!="
1162 redef fun accept_typing
(v
)
1166 var variable
= self.n_expr
.its_variable
1167 if variable
== null then return
1168 var mtype
= self.n_expr2
.mtype
1169 if not mtype
isa MNullType then return
1170 var vartype
= v
.get_variable
(self, variable
)
1171 if not vartype
isa MNullableType then return
1172 self.after_flow_context
.when_false
.set_var
(variable
, mtype
)
1173 self.after_flow_context
.when_true
.set_var
(variable
, vartype
.mtype
)
1174 #debug("adapt {variable}:{vartype} ; true->{vartype.mtype} false->{mtype}")
1178 redef fun property_name
do return "<"
1181 redef fun property_name
do return "<="
1184 redef fun property_name
do return "<<"
1187 redef fun property_name
do return ">"
1190 redef fun property_name
do return ">="
1193 redef fun property_name
do return ">>"
1195 redef class APlusExpr
1196 redef fun property_name
do return "+"
1198 redef class AMinusExpr
1199 redef fun property_name
do return "-"
1201 redef class AStarshipExpr
1202 redef fun property_name
do return "<=>"
1204 redef class AStarExpr
1205 redef fun property_name
do return "*"
1207 redef class ASlashExpr
1208 redef fun property_name
do return "/"
1210 redef class APercentExpr
1211 redef fun property_name
do return "%"
1214 redef class AUminusExpr
1215 redef fun property_name
do return "unary -"
1216 redef fun compute_raw_arguments
do return new Array[AExpr]
1220 redef class ACallExpr
1221 redef fun property_name
do return n_id
.text
1222 redef fun compute_raw_arguments
do return n_args
.to_a
1225 redef class ACallAssignExpr
1226 redef fun property_name
do return n_id
.text
+ "="
1227 redef fun compute_raw_arguments
1229 var res
= n_args
.to_a
1235 redef class ABraExpr
1236 redef fun property_name
do return "[]"
1237 redef fun compute_raw_arguments
do return n_args
.to_a
1240 redef class ABraAssignExpr
1241 redef fun property_name
do return "[]="
1242 redef fun compute_raw_arguments
1244 var res
= n_args
.to_a
1250 redef class ASendReassignFormExpr
1251 # The property invoked for the writing
1252 var write_mproperty
: nullable MMethod = null
1254 redef fun accept_typing
(v
)
1256 var recvtype
= v
.visit_expr
(self.n_expr
)
1257 var name
= self.property_name
1259 if recvtype
== null then return # Forward error
1260 if recvtype
isa MNullType then
1261 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1265 var for_self
= self.n_expr
isa ASelfExpr
1266 var mpropdef
= v
.get_method
(self, recvtype
, name
, for_self
)
1268 if mpropdef
== null then return
1269 var mproperty
= mpropdef
.mproperty
1270 self.mproperty
= mproperty
1271 var msignature
= v
.resolve_signature_for
(mpropdef
, recvtype
, for_self
)
1273 var args
= compute_raw_arguments
1274 self.raw_arguments
= args
1276 v
.check_signature
(self, args
, name
, msignature
)
1278 var readtype
= msignature
.return_mtype
1279 if readtype
== null then
1280 v
.error
(self, "Error: {name} is not a function")
1284 var wpropdef
= v
.get_method
(self, recvtype
, name
+ "=", self.n_expr
isa ASelfExpr)
1285 if wpropdef
== null then return
1286 var wmproperty
= wpropdef
.mproperty
1287 self.write_mproperty
= wmproperty
1288 var wmsignature
= v
.resolve_signature_for
(wpropdef
, recvtype
, for_self
)
1290 var wtype
= self.resolve_reassignment
(v
, readtype
, wmsignature
.mparameters
.last
.mtype
)
1291 if wtype
== null then return
1293 args
= args
.to_a
# duplicate so raw_arguments keeps only the getter args
1294 args
.add
(self.n_value
)
1295 v
.check_signature
(self, args
, name
+ "=", wmsignature
)
1297 self.is_typed
= true
1301 redef class ACallReassignExpr
1302 redef fun property_name
do return n_id
.text
1303 redef fun compute_raw_arguments
do return n_args
.to_a
1306 redef class ABraReassignExpr
1307 redef fun property_name
do return "[]"
1308 redef fun compute_raw_arguments
do return n_args
.to_a
1311 redef class AInitExpr
1312 redef fun property_name
do return "init"
1313 redef fun compute_raw_arguments
do return n_args
.to_a
1317 fun to_a
: Array[AExpr] do return self.n_exprs
.to_a
1322 redef class ASuperExpr
1323 # The method to call if the super is in fact a 'super init call'
1324 # Note: if the super is a normal call-next-method, then this attribute is null
1325 var mproperty
: nullable MMethod
1327 redef fun accept_typing
(v
)
1329 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1330 var mproperty
= v
.mpropdef
.mproperty
1331 if not mproperty
isa MMethod then
1332 v
.error
(self, "Error: super only usable in a method")
1335 var superprops
= mproperty
.lookup_super_definitions
(v
.mmodule
, recvtype
)
1336 if superprops
.length
== 0 then
1337 if mproperty
.is_init
and v
.mpropdef
.is_intro
then
1338 process_superinit
(v
)
1341 v
.error
(self, "Error: No super method to call for {mproperty}.")
1343 else if superprops
.length
> 1 then
1344 v
.modelbuilder
.warning
(self, "Warning: NOT YET IMPLEMENTED: Conflicting super method to call for {mproperty}: {superprops.join(", ")}.")
1347 var superprop
= superprops
.first
1348 assert superprop
isa MMethodDef
1350 var msignature
= v
.resolve_signature_for
(superprop
, recvtype
, true)
1351 var args
= self.n_args
.to_a
1352 if args
.length
> 0 then
1353 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1355 self.mtype
= msignature
.return_mtype
1358 private fun process_superinit
(v
: TypeVisitor)
1360 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1361 var mproperty
= v
.mpropdef
.mproperty
1362 var superprop
: nullable MMethodDef = null
1363 for msupertype
in v
.nclassdef
.mclassdef
.supertypes
do
1364 msupertype
= msupertype
.anchor_to
(v
.mmodule
, recvtype
)
1365 var errcount
= v
.modelbuilder
.toolcontext
.error_count
1366 var candidate
= v
.try_get_mproperty_by_name2
(self, msupertype
, mproperty
.name
).as(nullable MMethod)
1367 if candidate
== null then
1368 if v
.modelbuilder
.toolcontext
.error_count
> errcount
then return # Forard error
1369 continue # Try next super-class
1371 if superprop
!= null and superprop
.mproperty
!= candidate
then
1372 v
.error
(self, "Error: conflicting super constructor to call for {mproperty}: {candidate.full_name}, {superprop.mproperty.full_name}")
1375 var candidatedefs
= candidate
.lookup_definitions
(v
.mmodule
, recvtype
)
1376 if superprop
!= null then
1377 if superprop
== candidatedefs
.first
then continue
1378 candidatedefs
.add
(superprop
)
1380 if candidatedefs
.length
> 1 then
1381 v
.error
(self, "Error: confliting property definitions for property {mproperty} in {recvtype}: {candidatedefs.join(", ")}")
1384 superprop
= candidatedefs
.first
1386 if superprop
== null then
1387 v
.error
(self, "Error: No super method to call for {mproperty}.")
1390 self.mproperty
= superprop
.mproperty
1392 var args
= self.n_args
.to_a
1393 var msignature
= v
.resolve_signature_for
(superprop
, recvtype
, true)
1394 if args
.length
> 0 then
1395 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1397 # TODO: Check signature
1400 self.is_typed
= true
1406 redef class ANewExpr
1407 # The constructor invoked by the new.
1408 var mproperty
: nullable MMethod
1410 redef fun accept_typing
(v
)
1412 var recvtype
= v
.resolve_mtype
(self.n_type
)
1413 if recvtype
== null then return
1414 self.mtype
= recvtype
1416 if not recvtype
isa MClassType then
1417 if recvtype
isa MNullableType then
1418 v
.error
(self, "Type error: cannot instantiate the nullable type {recvtype}.")
1421 v
.error
(self, "Type error: cannot instantiate the formal type {recvtype}.")
1433 var propdef
= v
.get_method
(self, recvtype
, name
, false)
1434 if propdef
== null then return
1436 self.mproperty
= propdef
.mproperty
1438 if not propdef
.mproperty
.is_init_for
(recvtype
.mclass
) then
1439 v
.error
(self, "Error: {name} is not a constructor.")
1443 var msignature
= v
.resolve_signature_for
(propdef
, recvtype
, false)
1445 var args
= n_args
.to_a
1446 v
.check_signature
(self, args
, name
, msignature
)
1452 redef class AAttrFormExpr
1453 # The attribute acceded.
1454 var mproperty
: nullable MAttribute
1456 # The static type of the attribute.
1457 var attr_type
: nullable MType
1459 # Resolve the attribute acceded.
1460 private fun resolve_property
(v
: TypeVisitor)
1462 var recvtype
= v
.visit_expr
(self.n_expr
)
1463 if recvtype
== null then return # Skip error
1464 var name
= self.n_id
.text
1465 if recvtype
isa MNullType then
1466 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1470 var unsafe_type
= v
.anchor_to
(recvtype
)
1471 var mproperty
= v
.try_get_mproperty_by_name2
(self, unsafe_type
, name
)
1472 if mproperty
== null then
1473 v
.modelbuilder
.error
(self, "Error: Attribute {name} doesn't exists in {recvtype}.")
1476 assert mproperty
isa MAttribute
1477 self.mproperty
= mproperty
1479 var mpropdefs
= mproperty
.lookup_definitions
(v
.mmodule
, unsafe_type
)
1480 assert mpropdefs
.length
== 1
1481 var mpropdef
= mpropdefs
.first
1482 var attr_type
= mpropdef
.static_mtype
.as(not null)
1483 attr_type
= v
.resolve_for
(attr_type
, recvtype
, self.n_expr
isa ASelfExpr)
1484 self.attr_type
= attr_type
1488 redef class AAttrExpr
1489 redef fun accept_typing
(v
)
1491 self.resolve_property
(v
)
1492 self.mtype
= self.attr_type
1497 redef class AAttrAssignExpr
1498 redef fun accept_typing
(v
)
1500 self.resolve_property
(v
)
1501 var mtype
= self.attr_type
1503 v
.visit_expr_subtype
(self.n_value
, mtype
)
1504 self.is_typed
= true
1508 redef class AAttrReassignExpr
1509 redef fun accept_typing
(v
)
1511 self.resolve_property
(v
)
1512 var mtype
= self.attr_type
1513 if mtype
== null then return # Skip error
1515 self.resolve_reassignment
(v
, mtype
, mtype
)
1517 self.is_typed
= true
1521 redef class AIssetAttrExpr
1522 redef fun accept_typing
(v
)
1524 self.resolve_property
(v
)
1525 var mtype
= self.attr_type
1526 if mtype
== null then return # Skip error
1528 var recvtype
= self.n_expr
.mtype
.as(not null)
1529 var bound
= v
.resolve_for
(mtype
, recvtype
, false)
1530 if bound
isa MNullableType then
1531 v
.error
(self, "Error: isset on a nullable attribute.")
1533 self.mtype
= v
.type_bool
(self)
1539 redef class AClosureCallExpr
1540 redef fun accept_typing
(v
)
1542 var variable
= self.variable
1543 if variable
== null then return # Skip error
1545 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1546 var msignature
= variable
.declared_type
.as(not null)
1547 msignature
= v
.resolve_for
(msignature
, recvtype
, false).as(MSignature)
1549 var args
= n_args
.to_a
1550 v
.check_signature
(self, args
, variable
.name
, msignature
)
1552 self.is_typed
= true
1553 self.mtype
= msignature
.return_mtype
1557 redef class AClosureDef
1558 var mclosure
: nullable MParameter
1560 private fun accept_typing
(v
: TypeVisitor, mparameter
: MParameter)
1562 var variables
= self.variables
1563 if variables
== null then return
1565 self.mclosure
= mparameter
1566 var msignature
= mparameter
.mtype
.as(MSignature)
1568 if msignature
.arity
!= variables
.length
then
1569 v
.error
(self, "Type error: closure {mparameter.name} expects {msignature.arity} parameters, {variables.length} given")
1573 for i
in [0..variables
.length
[ do
1574 variables
[i
].declared_type
= msignature
.mparameters
[i
].mtype
1577 v
.visit_stmt
(self.n_expr
)
1583 redef class ADebugTypeExpr
1584 redef fun accept_typing
(v
)
1586 var expr
= v
.visit_expr
(self.n_expr
)
1587 if expr
== null then return
1588 var unsafe
= v
.anchor_to
(expr
)
1589 var ntype
= self.n_type
1590 var mtype
= v
.resolve_mtype
(ntype
)
1591 if mtype
!= null and mtype
!= expr
then
1592 var umtype
= v
.anchor_to
(mtype
)
1593 v
.modelbuilder
.warning
(self, "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")