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 try_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
)
181 fun get_mclass
(node
: ANode, name
: String): nullable MClass
183 var mmodule
= self.nclassdef
.mclassdef
.mmodule
184 var mclass
= modelbuilder
.try_get_mclass_by_name
(node
, mmodule
, name
)
185 if mclass
== null then
186 self.modelbuilder
.error
(node
, "Type Error: missing primitive class `{name}'.")
191 fun type_bool
(node
: ANode): nullable MType
193 var mclass
= self.get_mclass
(node
, "Bool")
194 if mclass
== null then return null
195 return mclass
.mclass_type
198 fun get_method
(node
: ANode, recvtype
: MType, name
: String, recv_is_self
: Bool): nullable MMethodDef
200 var unsafe_type
= self.anchor_to
(recvtype
)
202 #debug("recv: {recvtype} (aka {unsafe_type})")
204 var mproperty
= self.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
205 if mproperty
== null then
206 #self.modelbuilder.error(node, "Type error: property {name} not found in {unsafe_type} (ie {recvtype})")
208 self.modelbuilder
.error
(node
, "Error: Method or variable '{name}' unknown in {recvtype}.")
210 self.modelbuilder
.error
(node
, "Error: Method '{name}' doesn't exists in {recvtype}.")
215 if mproperty
.visibility
== protected_visibility
and not recv_is_self
and self.mmodule
.visibility_for
(mproperty
.intro_mclassdef
.mmodule
) < intrude_visibility
then
216 self.modelbuilder
.error
(node
, "Error: Method '{name}' is protected and can only acceded by self. {mproperty.intro_mclassdef.mmodule.visibility_for(self.mmodule)}")
220 var propdefs
= mproperty
.lookup_definitions
(self.mmodule
, unsafe_type
)
221 if propdefs
.length
== 0 then
222 self.modelbuilder
.error
(node
, "Type error: no definition found for property {name} in {unsafe_type}")
224 else if propdefs
.length
> 1 then
225 self.modelbuilder
.error
(node
, "Error: confliting property definitions for property {name} in {unsafe_type}: {propdefs.join(" ")}")
229 var propdef
= propdefs
.first
230 assert propdef
isa MMethodDef
234 # Visit the expressions of args and cheik their conformity with the corresponding typi in signature
235 # The point of this method is to handle varargs correctly
236 # Note: The signature must be correctly adapted
237 fun check_signature
(node
: ANode, args
: Array[AExpr], name
: String, msignature
: MSignature): Bool
239 var vararg_rank
= msignature
.vararg_rank
240 if vararg_rank
>= 0 then
241 if args
.length
< msignature
.arity
then
242 #self.modelbuilder.error(node, "Error: Incorrect number of parameters. Got {args.length}, expected at least {msignature.arity}. Signature is {msignature}")
243 self.modelbuilder
.error
(node
, "Error: arity mismatch; prototype is '{name}{msignature}'")
246 else if args
.length
!= msignature
.arity
then
247 self.modelbuilder
.error
(node
, "Error: Incorrect number of parameters. Got {args.length}, expected {msignature.arity}. Signature is {msignature}")
251 #debug("CALL {unsafe_type}.{msignature}")
253 var vararg_decl
= args
.length
- msignature
.arity
254 for i
in [0..msignature
.arity
[ do
256 if i
== vararg_rank
then continue # skip the vararg
257 if i
> vararg_rank
then
260 var paramtype
= msignature
.mparameters
[i
].mtype
261 self.visit_expr_subtype
(args
[j
], paramtype
)
263 if vararg_rank
>= 0 then
264 var varargs
= new Array[AExpr]
265 var paramtype
= msignature
.mparameters
[vararg_rank
].mtype
266 for j
in [vararg_rank
..vararg_rank
+vararg_decl
] do
268 self.visit_expr_subtype
(args
[j
], paramtype
)
274 fun error
(node
: ANode, message
: String)
276 self.modelbuilder
.toolcontext
.error
(node
.hot_location
, message
)
279 fun get_variable
(node
: AExpr, variable
: Variable): nullable MType
281 var flow
= node
.after_flow_context
283 self.error
(node
, "No context!")
287 if flow
.vars
.has_key
(variable
) then
288 return flow
.vars
[variable
]
290 #node.debug("*** START Collected for {variable}")
291 var mtypes
= flow
.collect_types
(variable
)
292 #node.debug("**** END Collected for {variable}")
293 if mtypes
== null or mtypes
.length
== 0 then
294 return variable
.declared_type
295 else if mtypes
.length
== 1 then
298 var res
= merge_types
(node
,mtypes
)
299 if res
== null then res
= variable
.declared_type
305 fun set_variable
(node
: AExpr, variable
: Variable, mtype
: nullable MType)
307 var flow
= node
.after_flow_context
310 flow
.set_var
(variable
, mtype
)
313 fun merge_types
(node
: ANode, col
: Array[nullable MType]): nullable MType
315 if col
.length
== 1 then return col
.first
316 var res
= new Array[nullable MType]
318 if t1
== null then continue # return null
321 if t2
== null then continue # return null
322 if t2
isa MNullableType or t2
isa MNullType then
325 if not is_subtype
(t2
, t1
) then found
= false
328 #print "merge {col.join(" ")} -> {t1}"
332 #self.modelbuilder.warning(node, "Type Error: {col.length} conflicting types: <{col.join(", ")}>")
338 # The declared type of the variable
339 var declared_type
: nullable MType
342 redef class FlowContext
343 # Store changes of types because of type evolution
344 private var vars
: HashMap[Variable, nullable MType] = new HashMap[Variable, nullable MType]
345 private var cache
: HashMap[Variable, nullable Array[nullable MType]] = new HashMap[Variable, nullable Array[nullable MType]]
347 # Adapt the variable to a static type
348 # Warning1: do not modify vars directly.
349 # Warning2: sub-flow may have cached a unadapted variabial
350 private fun set_var
(variable
: Variable, mtype
: nullable MType)
352 self.vars
[variable
] = mtype
353 self.cache
.keys
.remove
(variable
)
356 private fun collect_types
(variable
: Variable): nullable Array[nullable MType]
358 if cache
.has_key
(variable
) then
359 return cache
[variable
]
361 var res
: nullable Array[nullable MType] = null
362 if vars
.has_key
(variable
) then
363 var mtype
= vars
[variable
]
365 else if self.previous
.is_empty
then
367 res
= [variable
.declared_type
]
369 for flow
in self.previous
do
370 if flow
.is_unreachable
then continue
371 var r2
= flow
.collect_types
(variable
)
372 if r2
== null then continue
377 if not res
.has
(t
) then res
.add
(t
)
382 cache
[variable
] = res
388 # The entry point of the whole typing analysis
389 fun do_typing
(modelbuilder
: ModelBuilder)
393 # The variable associated to the reciever (if any)
394 var selfvariable
: nullable Variable
397 redef class AConcreteMethPropdef
398 redef fun do_typing
(modelbuilder
: ModelBuilder)
400 var nclassdef
= self.parent
.as(AClassdef)
401 var mpropdef
= self.mpropdef
.as(not null)
402 var v
= new TypeVisitor(modelbuilder
, nclassdef
, mpropdef
)
403 self.selfvariable
= v
.selfvariable
405 var nblock
= self.n_block
406 if nblock
== null then return
408 var mmethoddef
= self.mpropdef
.as(not null)
409 for i
in [0..mmethoddef
.msignature
.arity
[ do
410 var mtype
= mmethoddef
.msignature
.mparameters
[i
].mtype
411 if mmethoddef
.msignature
.vararg_rank
== i
then
412 var arrayclass
= v
.get_mclass
(self.n_signature
.n_params
[i
], "Array")
413 if arrayclass
== null then return # Skip error
414 mtype
= arrayclass
.get_mtype
([mtype
])
416 var variable
= self.n_signature
.n_params
[i
].variable
417 assert variable
!= null
418 variable
.declared_type
= mtype
420 for i
in [0..mmethoddef
.msignature
.mclosures
.length
[ do
421 var mclosure
= mmethoddef
.msignature
.mclosures
[i
]
422 var variable
= self.n_signature
.n_closure_decls
[i
].variable
423 assert variable
!= null
424 variable
.declared_type
= mclosure
.mtype
428 if not nblock
.after_flow_context
.is_unreachable
and mmethoddef
.msignature
.return_mtype
!= null then
429 # We reach the end of the function without having a return, it is bad
430 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
435 redef class AAttrPropdef
436 redef fun do_typing
(modelbuilder
: ModelBuilder)
438 var nclassdef
= self.parent
.as(AClassdef)
439 var v
= new TypeVisitor(modelbuilder
, nclassdef
, self.mpropdef
.as(not null))
440 self.selfvariable
= v
.selfvariable
442 var nexpr
= self.n_expr
443 if nexpr
!= null then
444 var mtype
= self.mpropdef
.static_mtype
445 v
.visit_expr_subtype
(nexpr
, mtype
)
453 # The static type of the expression.
454 # null if self is a statement of in case of error
455 var mtype
: nullable MType = null
457 # Is the statement correctly typed?
458 # Used to distinguish errors and statements when `mtype' == null
459 var is_typed
: Bool = false
461 # Return the variable read (if any)
462 # Used to perform adaptive typing
463 fun its_variable
: nullable Variable do return null
465 private fun accept_typing
(v
: TypeVisitor)
467 v
.error
(self, "no implemented accept_typing for {self.class_name}")
471 redef class ABlockExpr
472 redef fun accept_typing
(v
)
474 for e
in self.n_expr
do v
.visit_stmt
(e
)
479 redef class AVardeclExpr
480 redef fun accept_typing
(v
)
482 var variable
= self.variable
483 if variable
== null then return # Skip error
485 var ntype
= self.n_type
486 var mtype
: nullable MType
487 if ntype
== null then
490 mtype
= v
.resolve_mtype
(ntype
)
491 if mtype
== null then return # Skip error
494 var nexpr
= self.n_expr
495 if nexpr
!= null then
496 if mtype
!= null then
497 v
.visit_expr_subtype
(nexpr
, mtype
)
499 mtype
= v
.visit_expr
(nexpr
)
500 if mtype
== null then return # Skip error
504 if mtype
== null then
505 mtype
= v
.get_mclass
(self, "Object").mclass_type
.as_nullable
508 variable
.declared_type
= mtype
509 v
.set_variable
(self, variable
, mtype
)
511 #debug("var {variable}: {mtype}")
518 redef fun its_variable
do return self.variable
519 redef fun accept_typing
(v
)
521 var variable
= self.variable
522 if variable
== null then return # Skip error
524 var mtype
= v
.get_variable
(self, variable
)
525 if mtype
!= null then
526 #debug("{variable} is {mtype}")
528 #debug("{variable} is untyped")
535 redef class AVarAssignExpr
536 redef fun accept_typing
(v
)
538 var variable
= self.variable
539 assert variable
!= null
541 var mtype
= v
.visit_expr_subtype
(n_value
, variable
.declared_type
)
543 v
.set_variable
(self, variable
, mtype
)
549 redef class AReassignFormExpr
550 # The method designed by the reassign operator.
551 var reassign_property
: nullable MMethodDef = null
553 var read_type
: nullable MType = null
555 # Determine the `reassign_property'
556 # `readtype' is the type of the reading of the left value.
557 # `writetype' is the type of the writing of the left value.
558 # (Because of ACallReassignExpr, both can be different.
559 # Return the static type of the value to store.
560 private fun resolve_reassignment
(v
: TypeVisitor, readtype
, writetype
: MType): nullable MType
562 var reassign_name
: String
563 if self.n_assign_op
isa APlusAssignOp then
565 else if self.n_assign_op
isa AMinusAssignOp then
571 self.read_type
= readtype
573 if readtype
isa MNullType then
574 v
.error
(self, "Error: Method '{reassign_name}' call on 'null'.")
578 var mpropdef
= v
.get_method
(self, readtype
, reassign_name
, false)
579 if mpropdef
== null then return null # Skip error
581 self.reassign_property
= mpropdef
583 var msignature
= mpropdef
.msignature
584 assert msignature
!= null
585 msignature
= v
.resolve_signature_for
(msignature
, readtype
, false)
587 var rettype
= msignature
.return_mtype
588 assert msignature
.arity
== 1 and rettype
!= null
590 var value_type
= v
.visit_expr_subtype
(self.n_value
, msignature
.mparameters
.first
.mtype
)
591 if value_type
== null then return null # Skip error
593 v
.check_subtype
(self, rettype
, writetype
)
598 redef class AVarReassignExpr
599 redef fun accept_typing
(v
)
601 var variable
= self.variable
602 assert variable
!= null
604 var readtype
= v
.get_variable
(self, variable
)
605 if readtype
== null then return
607 var writetype
= variable
.declared_type
608 if writetype
== null then return
610 var rettype
= self.resolve_reassignment
(v
, readtype
, writetype
)
612 v
.set_variable
(self, variable
, rettype
)
619 redef class AContinueExpr
620 redef fun accept_typing
(v
)
622 var nexpr
= self.n_expr
623 if nexpr
!= null then
624 var mtype
= v
.visit_expr
(nexpr
)
630 redef class ABreakExpr
631 redef fun accept_typing
(v
)
633 var nexpr
= self.n_expr
634 if nexpr
!= null then
635 var mtype
= v
.visit_expr
(nexpr
)
641 redef class AReturnExpr
642 redef fun accept_typing
(v
)
644 var nexpr
= self.n_expr
645 var ret_type
= v
.mpropdef
.as(MMethodDef).msignature
.return_mtype
646 if nexpr
!= null then
647 if ret_type
!= null then
648 var mtype
= v
.visit_expr_subtype
(nexpr
, ret_type
)
650 var mtype
= v
.visit_expr
(nexpr
)
651 v
.error
(self, "Error: Return with value in a procedure.")
653 else if ret_type
!= null then
654 v
.error
(self, "Error: Return without value in a function.")
660 redef class AAbortExpr
661 redef fun accept_typing
(v
)
668 redef fun accept_typing
(v
)
670 v
.visit_expr_bool
(n_expr
)
678 redef class AIfexprExpr
679 redef fun accept_typing
(v
)
681 v
.visit_expr_bool
(n_expr
)
683 var t1
= v
.visit_expr
(n_then
)
684 var t2
= v
.visit_expr
(n_else
)
686 if t1
== null or t2
== null then
690 var t
= v
.merge_types
(self, [t1
, t2
])
692 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
699 redef fun accept_typing
(v
)
701 v
.visit_stmt
(n_block
)
706 redef class AWhileExpr
707 redef fun accept_typing
(v
)
709 v
.visit_expr_bool
(n_expr
)
711 v
.visit_stmt
(n_block
)
716 redef class ALoopExpr
717 redef fun accept_typing
(v
)
719 v
.visit_stmt
(n_block
)
725 var coltype
: nullable MGenericType
727 private fun do_type_iterator
(v
: TypeVisitor, mtype
: MType)
729 var objcla
= v
.get_mclass
(self, "Object")
730 if objcla
== null then return
732 var colcla
= v
.try_get_mclass
(self, "Collection")
733 if colcla
!= null and v
.is_subtype
(mtype
, colcla
.get_mtype
([objcla
.mclass_type
.as_nullable
])) then
734 var coltype
= mtype
.supertype_to
(v
.mmodule
, v
.anchor
, colcla
)
735 assert coltype
isa MGenericType
736 self.coltype
= coltype
737 var variables
= self.variables
738 if variables
.length
!= 1 then
739 v
.error
(self, "Type Error: Expected one variable")
741 variables
.first
.declared_type
= coltype
.arguments
.first
746 var mapcla
= v
.try_get_mclass
(self, "Map")
747 if mapcla
!= null and v
.is_subtype
(mtype
, mapcla
.get_mtype
([objcla
.mclass_type
.as_nullable
, objcla
.mclass_type
.as_nullable
])) then
748 var coltype
= mtype
.supertype_to
(v
.mmodule
, v
.anchor
, mapcla
)
749 assert coltype
isa MGenericType
750 self.coltype
= coltype
751 var variables
= self.variables
752 if variables
.length
!= 2 then
753 v
.error
(self, "Type Error: Expected two variables")
755 variables
[0].declared_type
= coltype
.arguments
[0]
756 variables
[1].declared_type
= coltype
.arguments
[1]
761 v
.modelbuilder
.error
(self, "NOT YET IMPLEMENTED: Do 'for' on {mtype}")
764 redef fun accept_typing
(v
)
766 var mtype
= v
.visit_expr
(n_expr
)
767 if mtype
== null then return
769 self.do_type_iterator
(v
, mtype
)
771 v
.visit_stmt
(n_block
)
776 redef class AAssertExpr
777 redef fun accept_typing
(v
)
779 v
.visit_expr_bool
(n_expr
)
787 redef fun accept_typing
(v
)
789 v
.visit_expr_bool
(n_expr
)
790 v
.visit_expr_bool
(n_expr2
)
791 self.mtype
= v
.type_bool
(self)
796 redef fun accept_typing
(v
)
798 v
.visit_expr_bool
(n_expr
)
799 v
.visit_expr_bool
(n_expr2
)
800 self.mtype
= v
.type_bool
(self)
806 redef fun accept_typing
(v
)
808 v
.visit_expr_bool
(n_expr
)
809 self.mtype
= v
.type_bool
(self)
813 redef class AOrElseExpr
814 redef fun accept_typing
(v
)
816 var t1
= v
.visit_expr
(n_expr
)
817 var t2
= v
.visit_expr
(n_expr2
)
819 if t1
== null or t2
== null then
823 if t1
isa MNullableType then
827 var t
= v
.merge_types
(self, [t1
, t2
])
829 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
836 redef fun accept_typing
(v
)
839 v
.visit_expr
(n_expr2
)
840 self.mtype
= v
.type_bool
(self)
844 redef class ATrueExpr
845 redef fun accept_typing
(v
)
847 self.mtype
= v
.type_bool
(self)
851 redef class AFalseExpr
852 redef fun accept_typing
(v
)
854 self.mtype
= v
.type_bool
(self)
859 redef fun accept_typing
(v
)
861 var mclass
= v
.get_mclass
(self, "Int")
862 if mclass
== null then return # Forward error
863 self.mtype
= mclass
.mclass_type
867 redef class AFloatExpr
868 redef fun accept_typing
(v
)
870 var mclass
= v
.get_mclass
(self, "Float")
871 if mclass
== null then return # Forward error
872 self.mtype
= mclass
.mclass_type
876 redef class ACharExpr
877 redef fun accept_typing
(v
)
879 var mclass
= v
.get_mclass
(self, "Char")
880 if mclass
== null then return # Forward error
881 self.mtype
= mclass
.mclass_type
885 redef class AStringFormExpr
886 redef fun accept_typing
(v
)
888 var mclass
= v
.get_mclass
(self, "String")
889 if mclass
== null then return # Forward error
890 self.mtype
= mclass
.mclass_type
894 redef class ASuperstringExpr
895 redef fun accept_typing
(v
)
897 var mclass
= v
.get_mclass
(self, "String")
898 if mclass
== null then return # Forward error
899 self.mtype
= mclass
.mclass_type
900 for nexpr
in self.n_exprs
do
901 var t
= v
.visit_expr
(nexpr
)
906 redef class AArrayExpr
907 redef fun accept_typing
(v
)
909 var mtypes
= new Array[nullable MType]
910 for e
in self.n_exprs
.n_exprs
do
911 var t
= v
.visit_expr
(e
)
917 var mtype
= v
.merge_types
(self, mtypes
)
918 if mtype
== null then
919 v
.error
(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
922 var mclass
= v
.get_mclass
(self, "Array")
923 if mclass
== null then return # Forward error
924 self.mtype
= mclass
.get_mtype
([mtype
])
928 redef class ARangeExpr
929 redef fun accept_typing
(v
)
931 var discrete_class
= v
.get_mclass
(self, "Discrete")
932 if discrete_class
== null then return # Forward error
933 var discrete_type
= discrete_class
.mclassdefs
.first
.bound_mtype
934 var t1
= v
.visit_expr_subtype
(self.n_expr
, discrete_type
)
935 var t2
= v
.visit_expr_subtype
(self.n_expr2
, discrete_type
)
936 if t1
== null or t2
== null then return
937 var mclass
= v
.get_mclass
(self, "Range")
938 if mclass
== null then return # Forward error
939 if v
.is_subtype
(t1
, t2
) then
940 self.mtype
= mclass
.get_mtype
([t2
])
941 else if v
.is_subtype
(t2
, t1
) then
942 self.mtype
= mclass
.get_mtype
([t1
])
944 v
.error
(self, "Type Error: Cannot create range: {t1} vs {t2}")
949 redef class ANullExpr
950 redef fun accept_typing
(v
)
952 self.mtype
= v
.mmodule
.model
.null_type
957 # The static type to cast to.
958 # (different from the static type of the expression that is Bool).
959 var cast_type
: nullable MType
960 redef fun accept_typing
(v
)
962 var mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
963 self.cast_type
= mtype
965 var variable
= self.n_expr
.its_variable
966 if variable
!= null then
967 var orig
= self.n_expr
.mtype
968 var from
= if orig
!= null then orig
.to_s
else "invalid"
969 var to
= if mtype
!= null then mtype
.to_s
else "invalid"
970 #debug("adapt {variable}: {from} -> {to}")
971 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
974 self.mtype
= v
.type_bool
(self)
978 redef class AAsCastExpr
979 redef fun accept_typing
(v
)
981 self.mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
985 redef class AAsNotnullExpr
986 redef fun accept_typing
(v
)
988 var mtype
= v
.visit_expr
(self.n_expr
)
989 if mtype
isa MNullType then
990 v
.error
(self, "Type error: as(not null) on null")
993 if mtype
isa MNullableType then
994 self.mtype
= mtype
.mtype
997 # TODO: warn on useless as not null
1002 redef class AProxyExpr
1003 redef fun accept_typing
(v
)
1005 self.mtype
= v
.visit_expr
(self.n_expr
)
1009 redef class ASelfExpr
1010 redef var its_variable
: nullable Variable
1011 redef fun accept_typing
(v
)
1013 var variable
= v
.selfvariable
1014 self.its_variable
= variable
1015 self.mtype
= v
.get_variable
(self, variable
)
1019 ## MESSAGE SENDING AND PROPERTY
1021 redef class ASendExpr
1022 # The property invoked by the send.
1023 var mproperty
: nullable MMethod
1025 redef fun accept_typing
(v
)
1027 var recvtype
= v
.visit_expr
(self.n_expr
)
1028 var name
= self.property_name
1030 if recvtype
== null then return # Forward error
1031 if recvtype
isa MNullType then
1032 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1036 var propdef
= v
.get_method
(self, recvtype
, name
, self.n_expr
isa ASelfExpr)
1037 if propdef
== null then return
1038 var mproperty
= propdef
.mproperty
1039 self.mproperty
= mproperty
1040 var msignature
= propdef
.msignature
1041 if msignature
== null then abort # Forward error
1043 var for_self
= self.n_expr
isa ASelfExpr
1044 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, for_self
)
1046 var args
= compute_raw_arguments
1047 self.raw_arguments
= args
1049 v
.check_signature
(self, args
, name
, msignature
)
1051 if mproperty
.is_init
then
1052 var vmpropdef
= v
.mpropdef
1053 if not (vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_init
) then
1054 v
.error
(self, "Can call a init only in another init")
1058 var ret
= msignature
.return_mtype
1062 self.is_typed
= true
1065 if self.n_closure_defs
.length
== msignature
.mclosures
.length
then
1066 for i
in [0..self.n_closure_defs
.length
[ do
1067 self.n_closure_defs
[i
].accept_typing
(v
, msignature
.mclosures
[i
])
1070 debug
("closure: got {self.n_closure_defs.length}, want {msignature.mclosures.length}")
1074 # The name of the property
1075 # Each subclass simply provide the correct name.
1076 private fun property_name
: String is abstract
1078 # An array of all arguments (excluding self)
1079 var raw_arguments
: nullable Array[AExpr]
1081 private fun compute_raw_arguments
: Array[AExpr] is abstract
1084 redef class ABinopExpr
1085 redef fun compute_raw_arguments
do return [n_expr2
]
1088 redef fun property_name
do return "=="
1089 redef fun accept_typing
(v
)
1093 var variable
= self.n_expr
.its_variable
1094 if variable
== null then return
1095 var mtype
= self.n_expr2
.mtype
1096 if not mtype
isa MNullType then return
1097 var vartype
= v
.get_variable
(self, variable
)
1098 if not vartype
isa MNullableType then return
1099 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1100 self.after_flow_context
.when_false
.set_var
(variable
, vartype
.mtype
)
1101 #debug("adapt {variable}:{vartype} ; true->{mtype} false->{vartype.mtype}")
1105 redef fun property_name
do return "!="
1106 redef fun accept_typing
(v
)
1110 var variable
= self.n_expr
.its_variable
1111 if variable
== null then return
1112 var mtype
= self.n_expr2
.mtype
1113 if not mtype
isa MNullType then return
1114 var vartype
= v
.get_variable
(self, variable
)
1115 if not vartype
isa MNullableType then return
1116 self.after_flow_context
.when_false
.set_var
(variable
, mtype
)
1117 self.after_flow_context
.when_true
.set_var
(variable
, vartype
.mtype
)
1118 #debug("adapt {variable}:{vartype} ; true->{vartype.mtype} false->{mtype}")
1122 redef fun property_name
do return "<"
1125 redef fun property_name
do return "<="
1128 redef fun property_name
do return "<<"
1131 redef fun property_name
do return ">"
1134 redef fun property_name
do return ">="
1137 redef fun property_name
do return ">>"
1139 redef class APlusExpr
1140 redef fun property_name
do return "+"
1142 redef class AMinusExpr
1143 redef fun property_name
do return "-"
1145 redef class AStarshipExpr
1146 redef fun property_name
do return "<=>"
1148 redef class AStarExpr
1149 redef fun property_name
do return "*"
1151 redef class ASlashExpr
1152 redef fun property_name
do return "/"
1154 redef class APercentExpr
1155 redef fun property_name
do return "%"
1158 redef class AUminusExpr
1159 redef fun property_name
do return "unary -"
1160 redef fun compute_raw_arguments
do return new Array[AExpr]
1164 redef class ACallExpr
1165 redef fun property_name
do return n_id
.text
1166 redef fun compute_raw_arguments
do return n_args
.to_a
1169 redef class ACallAssignExpr
1170 redef fun property_name
do return n_id
.text
+ "="
1171 redef fun compute_raw_arguments
1173 var res
= n_args
.to_a
1179 redef class ABraExpr
1180 redef fun property_name
do return "[]"
1181 redef fun compute_raw_arguments
do return n_args
.to_a
1184 redef class ABraAssignExpr
1185 redef fun property_name
do return "[]="
1186 redef fun compute_raw_arguments
1188 var res
= n_args
.to_a
1194 redef class ASendReassignFormExpr
1195 # The property invoked for the writing
1196 var write_mproperty
: nullable MMethod = null
1198 redef fun accept_typing
(v
)
1200 var recvtype
= v
.visit_expr
(self.n_expr
)
1201 var name
= self.property_name
1203 if recvtype
== null then return # Forward error
1204 if recvtype
isa MNullType then
1205 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1209 var propdef
= v
.get_method
(self, recvtype
, name
, self.n_expr
isa ASelfExpr)
1210 if propdef
== null then return
1211 var mproperty
= propdef
.mproperty
1212 self.mproperty
= mproperty
1213 var msignature
= propdef
.msignature
1214 if msignature
== null then abort # Forward error
1215 var for_self
= self.n_expr
isa ASelfExpr
1216 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, for_self
)
1218 var args
= compute_raw_arguments
1219 self.raw_arguments
= args
1221 v
.check_signature
(self, args
, name
, msignature
)
1223 var readtype
= msignature
.return_mtype
1224 if readtype
== null then
1225 v
.error
(self, "Error: {name} is not a function")
1229 var wpropdef
= v
.get_method
(self, recvtype
, name
+ "=", self.n_expr
isa ASelfExpr)
1230 if wpropdef
== null then return
1231 var wmproperty
= wpropdef
.mproperty
1232 self.write_mproperty
= wmproperty
1233 var wmsignature
= wpropdef
.msignature
1234 if wmsignature
== null then abort # Forward error
1235 wmsignature
= v
.resolve_signature_for
(wmsignature
, recvtype
, for_self
)
1237 var wtype
= self.resolve_reassignment
(v
, readtype
, wmsignature
.mparameters
.last
.mtype
)
1238 if wtype
== null then return
1240 args
= args
.to_a
# duplicate so raw_arguments keeps only the getter args
1241 args
.add
(self.n_value
)
1242 v
.check_signature
(self, args
, name
+ "=", wmsignature
)
1244 self.is_typed
= true
1248 redef class ACallReassignExpr
1249 redef fun property_name
do return n_id
.text
1250 redef fun compute_raw_arguments
do return n_args
.to_a
1253 redef class ABraReassignExpr
1254 redef fun property_name
do return "[]"
1255 redef fun compute_raw_arguments
do return n_args
.to_a
1258 redef class AInitExpr
1259 redef fun property_name
do return "init"
1260 redef fun compute_raw_arguments
do return n_args
.to_a
1264 fun to_a
: Array[AExpr] do return self.n_exprs
.to_a
1269 redef class ASuperExpr
1270 # The method to call if the super is in fact a 'super init call'
1271 # Note: if the super is a normal call-next-method, then this attribute is null
1272 var mproperty
: nullable MMethod
1274 redef fun accept_typing
(v
)
1276 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1277 var mproperty
= v
.mpropdef
.mproperty
1278 if not mproperty
isa MMethod then
1279 v
.error
(self, "Error: super only usable in a method")
1282 var superprops
= mproperty
.lookup_super_definitions
(v
.mmodule
, recvtype
)
1283 if superprops
.length
== 0 then
1284 if mproperty
.is_init
and v
.mpropdef
.is_intro
then
1285 process_superinit
(v
)
1288 v
.error
(self, "Error: No super method to call for {mproperty}.")
1290 else if superprops
.length
> 1 then
1291 v
.modelbuilder
.warning
(self, "Warning: NOT YET IMPLEMENTED: Conflicting super method to call for {mproperty}: {superprops.join(", ")}.")
1294 var superprop
= superprops
.first
1295 assert superprop
isa MMethodDef
1297 var msignature
= superprop
.msignature
.as(not null)
1298 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, true)
1299 var args
= self.n_args
.to_a
1300 if args
.length
> 0 then
1301 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1303 self.mtype
= msignature
.return_mtype
1306 private fun process_superinit
(v
: TypeVisitor)
1308 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1309 var mproperty
= v
.mpropdef
.mproperty
1310 var superprop
: nullable MMethodDef = null
1311 for msupertype
in v
.nclassdef
.mclassdef
.supertypes
do
1312 msupertype
= msupertype
.anchor_to
(v
.mmodule
, recvtype
)
1313 var errcount
= v
.modelbuilder
.toolcontext
.error_count
1314 var candidate
= v
.try_get_mproperty_by_name2
(self, msupertype
, mproperty
.name
).as(nullable MMethod)
1315 if candidate
== null then
1316 if v
.modelbuilder
.toolcontext
.error_count
> errcount
then return # Forard error
1317 continue # Try next super-class
1319 if superprop
!= null and superprop
.mproperty
!= candidate
then
1320 v
.error
(self, "Error: conflicting super constructor to call for {mproperty}: {candidate.full_name}, {superprop.mproperty.full_name}")
1323 var candidatedefs
= candidate
.lookup_definitions
(v
.mmodule
, recvtype
)
1324 if superprop
!= null then
1325 if superprop
== candidatedefs
.first
then continue
1326 candidatedefs
.add
(superprop
)
1328 if candidatedefs
.length
> 1 then
1329 v
.error
(self, "Error: confliting property definitions for property {mproperty} in {recvtype}: {candidatedefs.join(", ")}")
1332 superprop
= candidatedefs
.first
1334 if superprop
== null then
1335 v
.error
(self, "Error: No super method to call for {mproperty}.")
1338 self.mproperty
= superprop
.mproperty
1340 var args
= self.n_args
.to_a
1341 var msignature
= superprop
.msignature
.as(not null)
1342 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, true)
1343 if args
.length
> 0 then
1344 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1346 # TODO: Check signature
1349 self.is_typed
= true
1355 redef class ANewExpr
1356 # The constructor invoked by the new.
1357 var mproperty
: nullable MMethod
1359 redef fun accept_typing
(v
)
1361 var recvtype
= v
.resolve_mtype
(self.n_type
)
1362 if recvtype
== null then return
1363 self.mtype
= recvtype
1365 if not recvtype
isa MClassType then
1366 if recvtype
isa MNullableType then
1367 v
.error
(self, "Type error: cannot instantiate the nullable type {recvtype}.")
1370 v
.error
(self, "Type error: cannot instantiate the formal type {recvtype}.")
1382 var propdef
= v
.get_method
(self, recvtype
, name
, false)
1383 if propdef
== null then return
1385 self.mproperty
= propdef
.mproperty
1387 if not propdef
.mproperty
.is_init_for
(recvtype
.mclass
) then
1388 v
.error
(self, "Error: {name} is not a constructor.")
1392 var msignature
= propdef
.msignature
.as(not null)
1393 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, false)
1395 var args
= n_args
.to_a
1396 v
.check_signature
(self, args
, name
, msignature
)
1402 redef class AAttrFormExpr
1403 # The attribute acceded.
1404 var mproperty
: nullable MAttribute
1406 # The static type of the attribute.
1407 var attr_type
: nullable MType
1409 # Resolve the attribute acceded.
1410 private fun resolve_property
(v
: TypeVisitor)
1412 var recvtype
= v
.visit_expr
(self.n_expr
)
1413 if recvtype
== null then return # Skip error
1414 var name
= self.n_id
.text
1415 if recvtype
isa MNullType then
1416 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1420 var unsafe_type
= v
.anchor_to
(recvtype
)
1421 var mproperty
= v
.try_get_mproperty_by_name2
(self, unsafe_type
, name
)
1422 if mproperty
== null then
1423 v
.modelbuilder
.error
(self, "Error: Attribute {name} doesn't exists in {recvtype}.")
1426 assert mproperty
isa MAttribute
1427 self.mproperty
= mproperty
1429 var mpropdefs
= mproperty
.lookup_definitions
(v
.mmodule
, unsafe_type
)
1430 assert mpropdefs
.length
== 1
1431 var mpropdef
= mpropdefs
.first
1432 var attr_type
= mpropdef
.static_mtype
.as(not null)
1433 attr_type
= v
.resolve_for
(attr_type
, recvtype
, self.n_expr
isa ASelfExpr)
1434 self.attr_type
= attr_type
1438 redef class AAttrExpr
1439 redef fun accept_typing
(v
)
1441 self.resolve_property
(v
)
1442 self.mtype
= self.attr_type
1447 redef class AAttrAssignExpr
1448 redef fun accept_typing
(v
)
1450 self.resolve_property
(v
)
1451 var mtype
= self.attr_type
1453 v
.visit_expr_subtype
(self.n_value
, mtype
)
1454 self.is_typed
= true
1458 redef class AAttrReassignExpr
1459 redef fun accept_typing
(v
)
1461 self.resolve_property
(v
)
1462 var mtype
= self.attr_type
1463 if mtype
== null then return # Skip error
1465 self.resolve_reassignment
(v
, mtype
, mtype
)
1467 self.is_typed
= true
1471 redef class AIssetAttrExpr
1472 redef fun accept_typing
(v
)
1474 self.resolve_property
(v
)
1475 var mtype
= self.attr_type
1476 if mtype
== null then return # Skip error
1478 var recvtype
= self.n_expr
.mtype
.as(not null)
1479 var bound
= v
.resolve_for
(mtype
, recvtype
, false)
1480 if bound
isa MNullableType then
1481 v
.error
(self, "Error: isset on a nullable attribute.")
1483 self.mtype
= v
.type_bool
(self)
1489 redef class AClosureCallExpr
1490 redef fun accept_typing
(v
)
1492 var variable
= self.variable
1493 if variable
== null then return # Skip error
1495 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1496 var msignature
= variable
.declared_type
.as(MSignature)
1497 msignature
= v
.resolve_signature_for
(msignature
, recvtype
, false)
1499 var args
= n_args
.to_a
1500 v
.check_signature
(self, args
, variable
.name
, msignature
)
1502 self.is_typed
= true
1503 self.mtype
= msignature
.return_mtype
1507 redef class AClosureDef
1508 var mclosure
: nullable MParameter
1510 private fun accept_typing
(v
: TypeVisitor, mparameter
: MParameter)
1512 var variables
= self.variables
1513 if variables
== null then return
1515 self.mclosure
= mparameter
1516 var msignature
= mparameter
.mtype
.as(MSignature)
1518 if msignature
.arity
!= variables
.length
then
1519 v
.error
(self, "Type error: closure {mparameter.name} expects {msignature.arity} parameters, {variables.length} given")
1523 for i
in [0..variables
.length
[ do
1524 variables
[i
].declared_type
= msignature
.mparameters
[i
].mtype
1527 v
.visit_stmt
(self.n_expr
)
1533 redef class ADebugTypeExpr
1534 redef fun accept_typing
(v
)
1536 var expr
= v
.visit_expr
(self.n_expr
)
1537 if expr
== null then return
1538 var unsafe
= v
.anchor_to
(expr
)
1539 var ntype
= self.n_type
1540 var mtype
= v
.resolve_mtype
(ntype
)
1541 if mtype
!= null and mtype
!= expr
then
1542 var umtype
= v
.anchor_to
(mtype
)
1543 v
.modelbuilder
.warning
(self, "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")