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 redef class ToolContext
25 var typing_phase
: Phase = new TypingPhase(self, [flow_phase
, modelize_property_phase
, local_var_init_phase
])
28 private class TypingPhase
30 redef fun process_npropdef
(npropdef
) do npropdef
.do_typing
(toolcontext
.modelbuilder
)
33 private class TypeVisitor
34 var modelbuilder
: ModelBuilder
36 # The module of the analysis
37 # Used to correctly query the model
40 # The static type of the receiver
41 # Mainly used for type tests and type resolutions
42 var anchor
: nullable MClassType
44 # The analyzed mclassdef
45 var mclassdef
: nullable MClassDef
47 # The analyzed property
48 var mpropdef
: nullable MPropDef
50 var selfvariable
: Variable = new Variable("self")
52 # Is `self` use restricted?
53 # * no explicit `self`
54 # * method called on the implicit self must be top-level
55 var is_toplevel_context
= false
57 init(modelbuilder
: ModelBuilder, mmodule
: MModule, mpropdef
: nullable MPropDef)
59 self.modelbuilder
= modelbuilder
60 self.mmodule
= mmodule
62 if mpropdef
!= null then
63 self.mpropdef
= mpropdef
64 var mclassdef
= mpropdef
.mclassdef
65 self.mclassdef
= mclassdef
66 self.anchor
= mclassdef
.bound_mtype
68 var mclass
= mclassdef
.mclass
70 var selfvariable
= new Variable("self")
71 self.selfvariable
= selfvariable
72 selfvariable
.declared_type
= mclass
.mclass_type
74 var mprop
= mpropdef
.mproperty
75 if mprop
isa MMethod and mprop
.is_toplevel
then
76 is_toplevel_context
= true
81 fun anchor_to
(mtype
: MType): MType
84 if anchor
== null then
85 assert not mtype
.need_anchor
88 return mtype
.anchor_to
(mmodule
, anchor
)
91 fun is_subtype
(sub
, sup
: MType): Bool
93 return sub
.is_subtype
(mmodule
, anchor
, sup
)
96 fun resolve_for
(mtype
, subtype
: MType, for_self
: Bool): MType
98 #print "resolve_for {mtype} sub={subtype} forself={for_self} mmodule={mmodule} anchor={anchor}"
99 var res
= mtype
.resolve_for
(subtype
, anchor
, mmodule
, not for_self
)
103 # Check that `sub` is a subtype of `sup`.
104 # If `sub` is not a valid suptype, then display an error on `node` an return null.
105 # If `sub` is a safe subtype of `sup` then return `sub`.
106 # If `sub` is an unsafe subtype (ie an implicit cast is required), then return `sup`.
108 # The point of the return type is to determinate the usable type on an expression:
109 # If the suptype is safe, then the return type is the one on the expression typed by `sub`.
110 # Is the subtype is unsafe, then the return type is the one of an implicit cast on `sup`.
111 fun check_subtype
(node
: ANode, sub
, sup
: MType): nullable MType
113 if self.is_subtype
(sub
, sup
) then return sub
114 if self.is_subtype
(sub
, self.anchor_to
(sup
)) then
115 # FIXME workaround to the current unsafe typing policy. To remove once fixed virtual types exists.
116 #node.debug("Unsafe typing: expected {sup}, got {sub}")
119 self.modelbuilder
.error
(node
, "Type error: expected {sup}, got {sub}")
123 # Visit an expression and do not care about the return value
124 fun visit_stmt
(nexpr
: nullable AExpr)
126 if nexpr
== null then return
127 nexpr
.accept_typing
(self)
130 # Visit an expression and expects that it is not a statement
131 # Return the type of the expression
132 # Display an error and return null if:
133 # * the type cannot be determined or
134 # * `nexpr` is a statement
135 fun visit_expr
(nexpr
: AExpr): nullable MType
137 nexpr
.accept_typing
(self)
138 var mtype
= nexpr
.mtype
139 if mtype
!= null then return mtype
140 if not nexpr
.is_typed
then
141 if not self.modelbuilder
.toolcontext
.error_count
> 0 then # check that there is really an error
142 if self.modelbuilder
.toolcontext
.verbose_level
> 1 then
143 nexpr
.debug
("No return type but no error.")
146 return null # forward error
148 self.error
(nexpr
, "Type error: expected expression.")
152 # Visit an expression and expect its static type is a least a `sup`
153 # Return the type of the expression or null if
154 # * the type cannot be determined or
155 # * `nexpr` is a statement or
156 # * `nexpr` is not a `sup`
157 fun visit_expr_subtype
(nexpr
: AExpr, sup
: nullable MType): nullable MType
159 var sub
= visit_expr
(nexpr
)
160 if sub
== null then return null # Forward error
162 if sup
== null then return null # Forward error
164 var res
= check_subtype
(nexpr
, sub
, sup
)
166 nexpr
.implicit_cast_to
= res
171 # Visit an expression and expect its static type is a `Bool`
172 # Return the type of the expression or null if
173 # * the type cannot be determined or
174 # * `nexpr` is a statement or
175 # * `nexpr` is not a `Bool`
176 fun visit_expr_bool
(nexpr
: AExpr): nullable MType
178 return self.visit_expr_subtype
(nexpr
, self.type_bool
(nexpr
))
182 private fun visit_expr_cast
(node
: ANode, nexpr
: AExpr, ntype
: AType): nullable MType
184 var sub
= visit_expr
(nexpr
)
185 if sub
== null then return null # Forward error
187 var sup
= self.resolve_mtype
(ntype
)
188 if sup
== null then return null # Forward error
191 self.modelbuilder
.warning
(node
, "useless-type-test", "Warning: Expression is already a {sup}.")
192 else if self.is_subtype
(sub
, sup
) then
193 self.modelbuilder
.warning
(node
, "useless-type-test", "Warning: Expression is already a {sup} since it is a {sub}.")
198 fun try_get_mproperty_by_name2
(anode
: ANode, mtype
: MType, name
: String): nullable MProperty
200 return self.modelbuilder
.try_get_mproperty_by_name2
(anode
, mmodule
, mtype
, name
)
203 fun resolve_mtype
(node
: AType): nullable MType
205 return self.modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, node
)
208 fun try_get_mclass
(node
: ANode, name
: String): nullable MClass
210 var mclass
= modelbuilder
.try_get_mclass_by_name
(node
, mmodule
, name
)
214 fun get_mclass
(node
: ANode, name
: String): nullable MClass
216 var mclass
= modelbuilder
.try_get_mclass_by_name
(node
, mmodule
, name
)
217 if mclass
== null then
218 self.modelbuilder
.error
(node
, "Type Error: missing primitive class `{name}'.")
223 fun type_bool
(node
: ANode): nullable MType
225 var mclass
= self.get_mclass
(node
, "Bool")
226 if mclass
== null then return null
227 return mclass
.mclass_type
230 fun get_method
(node
: ANode, recvtype
: MType, name
: String, recv_is_self
: Bool): nullable CallSite
232 var unsafe_type
= self.anchor_to
(recvtype
)
234 #debug("recv: {recvtype} (aka {unsafe_type})")
235 if recvtype
isa MNullType then
236 self.error
(node
, "Error: Method '{name}' call on 'null'.")
240 var mproperty
= self.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
241 if mproperty
== null then
242 #self.modelbuilder.error(node, "Type error: property {name} not found in {unsafe_type} (ie {recvtype})")
244 self.modelbuilder
.error
(node
, "Error: Method or variable '{name}' unknown in {recvtype}.")
246 self.modelbuilder
.error
(node
, "Error: Method '{name}' doesn't exists in {recvtype}.")
251 assert mproperty
isa MMethod
253 if is_toplevel_context
and recv_is_self
and not mproperty
.is_toplevel
then
254 error
(node
, "Error: '{name}' is not a top-level method, thus need a receiver.")
256 if not recv_is_self
and mproperty
.is_toplevel
then
257 error
(node
, "Error: cannot call '{name}', a top-level method, with a receiver.")
260 if mproperty
.visibility
== protected_visibility
and not recv_is_self
and self.mmodule
.visibility_for
(mproperty
.intro_mclassdef
.mmodule
) < intrude_visibility
and not modelbuilder
.toolcontext
.opt_ignore_visibility
.value
then
261 self.modelbuilder
.error
(node
, "Error: Method '{name}' is protected and can only acceded by self.")
265 var info
= mproperty
.deprecation
266 if info
!= null and self.mpropdef
.mproperty
.deprecation
== null then
269 self.modelbuilder
.warning
(node
, "deprecated-method", "Deprecation Warning: Method '{name}' is deprecated: {mdoc.content.first}")
271 self.modelbuilder
.warning
(node
, "deprecated-method", "Deprecation Warning: Method '{name}' is deprecated.")
275 var propdefs
= mproperty
.lookup_definitions
(self.mmodule
, unsafe_type
)
277 if propdefs
.length
== 0 then
278 self.modelbuilder
.error
(node
, "Type error: no definition found for property {name} in {unsafe_type}")
280 else if propdefs
.length
== 1 then
281 mpropdef
= propdefs
.first
283 self.modelbuilder
.warning
(node
, "property-conflict", "Warning: conflicting property definitions for property {name} in {unsafe_type}: {propdefs.join(" ")}")
284 mpropdef
= mproperty
.intro
288 var msignature
= mpropdef
.new_msignature
or else mpropdef
.msignature
.as(not null)
289 msignature
= resolve_for
(msignature
, recvtype
, recv_is_self
).as(MSignature)
291 var erasure_cast
= false
292 var rettype
= mpropdef
.msignature
.return_mtype
293 if not recv_is_self
and rettype
!= null then
294 rettype
= rettype
.as_notnullable
295 if rettype
isa MParameterType then
296 var erased_rettype
= msignature
.return_mtype
297 assert erased_rettype
!= null
298 #node.debug("Erasure cast: Really a {rettype} but unsafely a {erased_rettype}")
303 var callsite
= new CallSite(node
, recvtype
, mmodule
, anchor
, recv_is_self
, mproperty
, mpropdef
, msignature
, erasure_cast
)
307 # Visit the expressions of args and check their conformity with the corresponding type in signature
308 # The point of this method is to handle varargs correctly
309 # Note: The signature must be correctly adapted
310 fun check_signature
(node
: ANode, args
: Array[AExpr], name
: String, msignature
: MSignature): Bool
312 var vararg_rank
= msignature
.vararg_rank
313 if vararg_rank
>= 0 then
314 if args
.length
< msignature
.arity
then
315 #self.modelbuilder.error(node, "Error: Incorrect number of parameters. Got {args.length}, expected at least {msignature.arity}. Signature is {msignature}")
316 self.modelbuilder
.error
(node
, "Error: arity mismatch; prototype is '{name}{msignature}'")
319 else if args
.length
!= msignature
.arity
then
320 self.modelbuilder
.error
(node
, "Error: Incorrect number of parameters. Got {args.length}, expected {msignature.arity}. Signature is {msignature}")
324 #debug("CALL {unsafe_type}.{msignature}")
326 var vararg_decl
= args
.length
- msignature
.arity
327 for i
in [0..msignature
.arity
[ do
329 if i
== vararg_rank
then continue # skip the vararg
330 if i
> vararg_rank
then
333 var paramtype
= msignature
.mparameters
[i
].mtype
334 self.visit_expr_subtype
(args
[j
], paramtype
)
336 if vararg_rank
>= 0 then
337 var varargs
= new Array[AExpr]
338 var paramtype
= msignature
.mparameters
[vararg_rank
].mtype
339 for j
in [vararg_rank
..vararg_rank
+vararg_decl
] do
341 self.visit_expr_subtype
(args
[j
], paramtype
)
347 fun error
(node
: ANode, message
: String)
349 self.modelbuilder
.toolcontext
.error
(node
.hot_location
, message
)
352 fun get_variable
(node
: AExpr, variable
: Variable): nullable MType
354 var flow
= node
.after_flow_context
356 self.error
(node
, "No context!")
360 if flow
.vars
.has_key
(variable
) then
361 return flow
.vars
[variable
]
363 #node.debug("*** START Collected for {variable}")
364 var mtypes
= flow
.collect_types
(variable
)
365 #node.debug("**** END Collected for {variable}")
366 if mtypes
== null or mtypes
.length
== 0 then
367 return variable
.declared_type
368 else if mtypes
.length
== 1 then
371 var res
= merge_types
(node
,mtypes
)
372 if res
== null then res
= variable
.declared_type
378 fun set_variable
(node
: AExpr, variable
: Variable, mtype
: nullable MType)
380 var flow
= node
.after_flow_context
383 flow
.set_var
(variable
, mtype
)
386 fun merge_types
(node
: ANode, col
: Array[nullable MType]): nullable MType
388 if col
.length
== 1 then return col
.first
389 var res
= new Array[nullable MType]
391 if t1
== null then continue # return null
394 if t2
== null then continue # return null
395 if t2
isa MNullableType or t2
isa MNullType then
398 if not is_subtype
(t2
, t1
) then found
= false
401 #print "merge {col.join(" ")} -> {t1}"
405 #self.modelbuilder.warning(node, "Type Error: {col.length} conflicting types: <{col.join(", ")}>")
410 # A specific method call site with its associated informations.
412 # The associated node for location
415 # The static type of the receiver (possibly unresolved)
418 # The module where the callsite is present
421 # The anchor to use with `recv` or `msignature`
422 var anchor
: nullable MClassType
424 # Is the receiver self?
425 # If "for_self", virtual types of the signature are kept
426 # If "not_for_self", virtual type are erased
427 var recv_is_self
: Bool
429 # The designated method
430 var mproperty
: MMethod
432 # The statically designated method definition
433 # The most specif one, it is.
434 var mpropdef
: MMethodDef
436 # The resolved signature for the receiver
437 var msignature
: MSignature
439 # Is a implicit cast required on erasure typing policy?
440 var erasure_cast
: Bool
442 private fun check_signature
(v
: TypeVisitor, args
: Array[AExpr]): Bool
444 return v
.check_signature
(self.node
, args
, self.mproperty
.name
, self.msignature
)
449 # The declared type of the variable
450 var declared_type
: nullable MType
453 redef class FlowContext
454 # Store changes of types because of type evolution
455 private var vars
: HashMap[Variable, nullable MType] = new HashMap[Variable, nullable MType]
456 private var cache
: HashMap[Variable, nullable Array[nullable MType]] = new HashMap[Variable, nullable Array[nullable MType]]
458 # Adapt the variable to a static type
459 # Warning1: do not modify vars directly.
460 # Warning2: sub-flow may have cached a unadapted variable
461 private fun set_var
(variable
: Variable, mtype
: nullable MType)
463 self.vars
[variable
] = mtype
464 self.cache
.keys
.remove
(variable
)
467 private fun collect_types
(variable
: Variable): nullable Array[nullable MType]
469 if cache
.has_key
(variable
) then
470 return cache
[variable
]
472 var res
: nullable Array[nullable MType] = null
473 if vars
.has_key
(variable
) then
474 var mtype
= vars
[variable
]
476 else if self.previous
.is_empty
then
478 res
= [variable
.declared_type
]
480 for flow
in self.previous
do
481 if flow
.is_unreachable
then continue
482 var r2
= flow
.collect_types
(variable
)
483 if r2
== null then continue
488 if not res
.has
(t
) then res
.add
(t
)
493 cache
[variable
] = res
499 # The entry point of the whole typing analysis
500 fun do_typing
(modelbuilder
: ModelBuilder)
504 # The variable associated to the receiver (if any)
505 var selfvariable
: nullable Variable
508 redef class AMethPropdef
509 redef fun do_typing
(modelbuilder
: ModelBuilder)
511 var nblock
= self.n_block
512 if nblock
== null then return
514 var mpropdef
= self.mpropdef
.as(not null)
515 var v
= new TypeVisitor(modelbuilder
, mpropdef
.mclassdef
.mmodule
, mpropdef
)
516 self.selfvariable
= v
.selfvariable
518 var mmethoddef
= self.mpropdef
.as(not null)
519 for i
in [0..mmethoddef
.msignature
.arity
[ do
520 var mtype
= mmethoddef
.msignature
.mparameters
[i
].mtype
521 if mmethoddef
.msignature
.vararg_rank
== i
then
522 var arrayclass
= v
.get_mclass
(self.n_signature
.n_params
[i
], "Array")
523 if arrayclass
== null then return # Skip error
524 mtype
= arrayclass
.get_mtype
([mtype
])
526 var variable
= self.n_signature
.n_params
[i
].variable
527 assert variable
!= null
528 variable
.declared_type
= mtype
532 if not nblock
.after_flow_context
.is_unreachable
and mmethoddef
.msignature
.return_mtype
!= null then
533 # We reach the end of the function without having a return, it is bad
534 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
539 redef class AAttrPropdef
540 redef fun do_typing
(modelbuilder
: ModelBuilder)
542 var mpropdef
= self.mpropdef
.as(not null)
543 var v
= new TypeVisitor(modelbuilder
, mpropdef
.mclassdef
.mmodule
, mpropdef
)
544 self.selfvariable
= v
.selfvariable
546 var nexpr
= self.n_expr
547 if nexpr
!= null then
548 var mtype
= self.mpropdef
.static_mtype
549 v
.visit_expr_subtype
(nexpr
, mtype
)
557 # The static type of the expression.
558 # null if self is a statement or in case of error
559 var mtype
: nullable MType = null
561 # Is the statement correctly typed?
562 # Used to distinguish errors and statements when `mtype == null`
563 var is_typed
: Bool = false
565 # If required, the following implicit cast `.as(XXX)`
566 # Such a cast may by required after evaluating the expression when
567 # a unsafe operation is detected (silently accepted by the Nit language).
568 # The attribute is computed by `check_subtype`
569 var implicit_cast_to
: nullable MType = null
571 # Return the variable read (if any)
572 # Used to perform adaptive typing
573 fun its_variable
: nullable Variable do return null
575 private fun accept_typing
(v
: TypeVisitor)
577 v
.error
(self, "no implemented accept_typing for {self.class_name}")
581 redef class ABlockExpr
582 redef fun accept_typing
(v
)
584 for e
in self.n_expr
do v
.visit_stmt
(e
)
588 # The type of a blockexpr is the one of the last expression (or null if empty)
591 if self.n_expr
.is_empty
then return null
592 return self.n_expr
.last
.mtype
596 redef class AVardeclExpr
597 redef fun accept_typing
(v
)
599 var variable
= self.variable
600 if variable
== null then return # Skip error
602 var ntype
= self.n_type
603 var mtype
: nullable MType
604 if ntype
== null then
607 mtype
= v
.resolve_mtype
(ntype
)
608 if mtype
== null then return # Skip error
611 var nexpr
= self.n_expr
612 if nexpr
!= null then
613 if mtype
!= null then
614 v
.visit_expr_subtype
(nexpr
, mtype
)
616 mtype
= v
.visit_expr
(nexpr
)
617 if mtype
== null then return # Skip error
622 if mtype
== null or mtype
isa MNullType then
623 decltype
= v
.get_mclass
(self, "Object").mclass_type
.as_nullable
624 if mtype
== null then mtype
= decltype
627 variable
.declared_type
= decltype
628 v
.set_variable
(self, variable
, mtype
)
630 #debug("var {variable}: {mtype}")
637 redef fun its_variable
do return self.variable
638 redef fun accept_typing
(v
)
640 var variable
= self.variable
641 if variable
== null then return # Skip error
643 var mtype
= v
.get_variable
(self, variable
)
644 if mtype
!= null then
645 #debug("{variable} is {mtype}")
647 #debug("{variable} is untyped")
654 redef class AVarAssignExpr
655 redef fun accept_typing
(v
)
657 var variable
= self.variable
658 assert variable
!= null
660 var mtype
= v
.visit_expr_subtype
(n_value
, variable
.declared_type
)
662 v
.set_variable
(self, variable
, mtype
)
668 redef class AReassignFormExpr
669 # The method designed by the reassign operator.
670 var reassign_callsite
: nullable CallSite
672 var read_type
: nullable MType = null
674 # Determine the `reassign_property`
675 # `readtype` is the type of the reading of the left value.
676 # `writetype` is the type of the writing of the left value.
677 # (Because of `ACallReassignExpr`, both can be different.
678 # Return the static type of the value to store.
679 private fun resolve_reassignment
(v
: TypeVisitor, readtype
, writetype
: MType): nullable MType
681 var reassign_name
: String
682 if self.n_assign_op
isa APlusAssignOp then
684 else if self.n_assign_op
isa AMinusAssignOp then
690 self.read_type
= readtype
692 if readtype
isa MNullType then
693 v
.error
(self, "Error: Method '{reassign_name}' call on 'null'.")
697 var callsite
= v
.get_method
(self, readtype
, reassign_name
, false)
698 if callsite
== null then return null # Skip error
699 self.reassign_callsite
= callsite
701 var msignature
= callsite
.msignature
702 var rettype
= msignature
.return_mtype
703 assert msignature
.arity
== 1 and rettype
!= null
705 var value_type
= v
.visit_expr_subtype
(self.n_value
, msignature
.mparameters
.first
.mtype
)
706 if value_type
== null then return null # Skip error
708 v
.check_subtype
(self, rettype
, writetype
)
713 redef class AVarReassignExpr
714 redef fun accept_typing
(v
)
716 var variable
= self.variable
717 assert variable
!= null
719 var readtype
= v
.get_variable
(self, variable
)
720 if readtype
== null then return
724 var writetype
= variable
.declared_type
725 if writetype
== null then return
727 var rettype
= self.resolve_reassignment
(v
, readtype
, writetype
)
729 v
.set_variable
(self, variable
, rettype
)
736 redef class AContinueExpr
737 redef fun accept_typing
(v
)
739 var nexpr
= self.n_expr
740 if nexpr
!= null then
741 var mtype
= v
.visit_expr
(nexpr
)
747 redef class ABreakExpr
748 redef fun accept_typing
(v
)
750 var nexpr
= self.n_expr
751 if nexpr
!= null then
752 var mtype
= v
.visit_expr
(nexpr
)
758 redef class AReturnExpr
759 redef fun accept_typing
(v
)
761 var nexpr
= self.n_expr
762 var ret_type
= v
.mpropdef
.as(MMethodDef).msignature
.return_mtype
763 if nexpr
!= null then
764 if ret_type
!= null then
765 var mtype
= v
.visit_expr_subtype
(nexpr
, ret_type
)
767 var mtype
= v
.visit_expr
(nexpr
)
768 v
.error
(self, "Error: Return with value in a procedure.")
770 else if ret_type
!= null then
771 v
.error
(self, "Error: Return without value in a function.")
777 redef class AAbortExpr
778 redef fun accept_typing
(v
)
785 redef fun accept_typing
(v
)
787 v
.visit_expr_bool
(n_expr
)
795 redef class AIfexprExpr
796 redef fun accept_typing
(v
)
798 v
.visit_expr_bool
(n_expr
)
800 var t1
= v
.visit_expr
(n_then
)
801 var t2
= v
.visit_expr
(n_else
)
803 if t1
== null or t2
== null then
807 var t
= v
.merge_types
(self, [t1
, t2
])
809 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
816 redef fun accept_typing
(v
)
818 v
.visit_stmt
(n_block
)
823 redef class AWhileExpr
824 redef fun accept_typing
(v
)
826 v
.visit_expr_bool
(n_expr
)
828 v
.visit_stmt
(n_block
)
833 redef class ALoopExpr
834 redef fun accept_typing
(v
)
836 v
.visit_stmt
(n_block
)
842 var coltype
: nullable MClassType
844 var method_iterator
: nullable CallSite
845 var method_is_ok
: nullable CallSite
846 var method_item
: nullable CallSite
847 var method_next
: nullable CallSite
848 var method_key
: nullable CallSite
850 private fun do_type_iterator
(v
: TypeVisitor, mtype
: MType)
852 if mtype
isa MNullType then
853 v
.error
(self, "Type error: 'for' cannot iterate over 'null'")
858 var objcla
= v
.get_mclass
(self, "Object")
859 if objcla
== null then return
861 # check iterator method
862 var itdef
= v
.get_method
(self, mtype
, "iterator", n_expr
isa ASelfExpr)
863 if itdef
== null then
864 v
.error
(self, "Type Error: 'for' expects a type providing 'iterator' method, got '{mtype}'.")
867 self.method_iterator
= itdef
869 # check that iterator return something
870 var ittype
= itdef
.msignature
.return_mtype
871 if ittype
== null then
872 v
.error
(self, "Type Error: 'for' expects method 'iterator' to return an 'Iterator' or 'MapIterator' type'.")
877 var colit_cla
= v
.try_get_mclass
(self, "Iterator")
878 var mapit_cla
= v
.try_get_mclass
(self, "MapIterator")
882 if colit_cla
!= null and v
.is_subtype
(ittype
, colit_cla
.get_mtype
([objcla
.mclass_type
.as_nullable
])) then
884 var coltype
= ittype
.supertype_to
(v
.mmodule
, v
.anchor
, colit_cla
)
885 var variables
= self.variables
886 if variables
.length
!= 1 then
887 v
.error
(self, "Type Error: 'for' expects only one variable when using 'Iterator'.")
889 variables
.first
.declared_type
= coltype
.arguments
.first
894 if mapit_cla
!= null and v
.is_subtype
(ittype
, mapit_cla
.get_mtype
([objcla
.mclass_type
, objcla
.mclass_type
.as_nullable
])) then
896 var coltype
= ittype
.supertype_to
(v
.mmodule
, v
.anchor
, mapit_cla
)
897 var variables
= self.variables
898 if variables
.length
!= 2 then
899 v
.error
(self, "Type Error: 'for' expects two variables when using 'MapIterator'.")
901 variables
[0].declared_type
= coltype
.arguments
[0]
902 variables
[1].declared_type
= coltype
.arguments
[1]
907 if not is_col
and not is_map
then
908 v
.error
(self, "Type Error: 'for' expects method 'iterator' to return an 'Iterator' or 'MapIterator' type'.")
912 # anchor formal and virtual types
913 if mtype
.need_anchor
then mtype
= v
.anchor_to
(mtype
)
915 mtype
= mtype
.as_notnullable
916 self.coltype
= mtype
.as(MClassType)
918 # get methods is_ok, next, item
919 var ikdef
= v
.get_method
(self, ittype
, "is_ok", false)
920 if ikdef
== null then
921 v
.error
(self, "Type Error: 'for' expects a method 'is_ok' in 'Iterator' type {ittype}.")
924 self.method_is_ok
= ikdef
926 var itemdef
= v
.get_method
(self, ittype
, "item", false)
927 if itemdef
== null then
928 v
.error
(self, "Type Error: 'for' expects a method 'item' in 'Iterator' type {ittype}.")
931 self.method_item
= itemdef
933 var nextdef
= v
.get_method
(self, ittype
, "next", false)
934 if nextdef
== null then
935 v
.error
(self, "Type Error: 'for' expects a method 'next' in 'Iterator' type {ittype}.")
938 self.method_next
= nextdef
941 var keydef
= v
.get_method
(self, ittype
, "key", false)
942 if keydef
== null then
943 v
.error
(self, "Type Error: 'for' expects a method 'key' in 'Iterator' type {ittype}.")
946 self.method_key
= keydef
950 redef fun accept_typing
(v
)
952 var mtype
= v
.visit_expr
(n_expr
)
953 if mtype
== null then return
955 self.do_type_iterator
(v
, mtype
)
957 v
.visit_stmt
(n_block
)
962 redef class AAssertExpr
963 redef fun accept_typing
(v
)
965 v
.visit_expr_bool
(n_expr
)
973 redef fun accept_typing
(v
)
975 v
.visit_expr_bool
(n_expr
)
976 v
.visit_expr_bool
(n_expr2
)
977 self.mtype
= v
.type_bool
(self)
981 redef class AImpliesExpr
982 redef fun accept_typing
(v
)
984 v
.visit_expr_bool
(n_expr
)
985 v
.visit_expr_bool
(n_expr2
)
986 self.mtype
= v
.type_bool
(self)
991 redef fun accept_typing
(v
)
993 v
.visit_expr_bool
(n_expr
)
994 v
.visit_expr_bool
(n_expr2
)
995 self.mtype
= v
.type_bool
(self)
1000 redef class ANotExpr
1001 redef fun accept_typing
(v
)
1003 v
.visit_expr_bool
(n_expr
)
1004 self.mtype
= v
.type_bool
(self)
1008 redef class AOrElseExpr
1009 redef fun accept_typing
(v
)
1011 var t1
= v
.visit_expr
(n_expr
)
1012 var t2
= v
.visit_expr
(n_expr2
)
1014 if t1
== null or t2
== null then
1018 t1
= t1
.as_notnullable
1020 var t
= v
.merge_types
(self, [t1
, t2
])
1022 t
= v
.mmodule
.object_type
1023 if t2
isa MNullableType then
1026 #v.error(self, "Type Error: ambiguous type {t1} vs {t2}")
1032 redef class ATrueExpr
1033 redef fun accept_typing
(v
)
1035 self.mtype
= v
.type_bool
(self)
1039 redef class AFalseExpr
1040 redef fun accept_typing
(v
)
1042 self.mtype
= v
.type_bool
(self)
1046 redef class AIntExpr
1047 redef fun accept_typing
(v
)
1049 var mclass
= v
.get_mclass
(self, "Int")
1050 if mclass
== null then return # Forward error
1051 self.mtype
= mclass
.mclass_type
1055 redef class AFloatExpr
1056 redef fun accept_typing
(v
)
1058 var mclass
= v
.get_mclass
(self, "Float")
1059 if mclass
== null then return # Forward error
1060 self.mtype
= mclass
.mclass_type
1064 redef class ACharExpr
1065 redef fun accept_typing
(v
)
1067 var mclass
= v
.get_mclass
(self, "Char")
1068 if mclass
== null then return # Forward error
1069 self.mtype
= mclass
.mclass_type
1073 redef class AStringFormExpr
1074 redef fun accept_typing
(v
)
1076 var mclass
= v
.get_mclass
(self, "String")
1077 if mclass
== null then return # Forward error
1078 self.mtype
= mclass
.mclass_type
1082 redef class ASuperstringExpr
1083 redef fun accept_typing
(v
)
1085 var mclass
= v
.get_mclass
(self, "String")
1086 if mclass
== null then return # Forward error
1087 self.mtype
= mclass
.mclass_type
1088 for nexpr
in self.n_exprs
do
1089 v
.visit_expr_subtype
(nexpr
, v
.mmodule
.object_type
)
1094 redef class AArrayExpr
1095 var with_capacity_callsite
: nullable CallSite
1096 var push_callsite
: nullable CallSite
1098 redef fun accept_typing
(v
)
1100 var mtype
: nullable MType = null
1101 var ntype
= self.n_type
1102 if ntype
!= null then
1103 mtype
= v
.resolve_mtype
(ntype
)
1104 if mtype
== null then return # Skip error
1106 var mtypes
= new Array[nullable MType]
1108 for e
in self.n_exprs
.n_exprs
do
1109 var t
= v
.visit_expr
(e
)
1113 if mtype
!= null then
1114 if v
.check_subtype
(e
, t
, mtype
) == null then return # Skip error
1115 if t
== mtype
then useless
= true
1120 if mtype
== null then
1121 mtype
= v
.merge_types
(self, mtypes
)
1123 if mtype
== null then
1124 v
.error
(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
1128 assert ntype
!= null
1129 v
.modelbuilder
.warning
(ntype
, "useless-type", "Warning: useless type declaration `{mtype}` in literal Array since it can be inferred from the elements type.")
1131 var mclass
= v
.get_mclass
(self, "Array")
1132 if mclass
== null then return # Forward error
1133 var array_mtype
= mclass
.get_mtype
([mtype
])
1135 with_capacity_callsite
= v
.get_method
(self, array_mtype
, "with_capacity", false)
1136 push_callsite
= v
.get_method
(self, array_mtype
, "push", false)
1138 self.mtype
= array_mtype
1142 redef class ARangeExpr
1143 var init_callsite
: nullable CallSite
1145 redef fun accept_typing
(v
)
1147 var discrete_class
= v
.get_mclass
(self, "Discrete")
1148 if discrete_class
== null then return # Forward error
1149 var discrete_type
= discrete_class
.intro
.bound_mtype
1150 var t1
= v
.visit_expr_subtype
(self.n_expr
, discrete_type
)
1151 var t2
= v
.visit_expr_subtype
(self.n_expr2
, discrete_type
)
1152 if t1
== null or t2
== null then return
1153 var mclass
= v
.get_mclass
(self, "Range")
1154 if mclass
== null then return # Forward error
1156 if v
.is_subtype
(t1
, t2
) then
1157 mtype
= mclass
.get_mtype
([t2
])
1158 else if v
.is_subtype
(t2
, t1
) then
1159 mtype
= mclass
.get_mtype
([t1
])
1161 v
.error
(self, "Type Error: Cannot create range: {t1} vs {t2}")
1167 # get the constructor
1169 if self isa ACrangeExpr then
1170 callsite
= v
.get_method
(self, mtype
, "init", false)
1171 else if self isa AOrangeExpr then
1172 callsite
= v
.get_method
(self, mtype
, "without_last", false)
1176 init_callsite
= callsite
1180 redef class ANullExpr
1181 redef fun accept_typing
(v
)
1183 self.mtype
= v
.mmodule
.model
.null_type
1187 redef class AIsaExpr
1188 # The static type to cast to.
1189 # (different from the static type of the expression that is `Bool`).
1190 var cast_type
: nullable MType
1191 redef fun accept_typing
(v
)
1193 var mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
1194 self.cast_type
= mtype
1196 var variable
= self.n_expr
.its_variable
1197 if variable
!= null then
1198 var orig
= self.n_expr
.mtype
1199 var from
= if orig
!= null then orig
.to_s
else "invalid"
1200 var to
= if mtype
!= null then mtype
.to_s
else "invalid"
1201 #debug("adapt {variable}: {from} -> {to}")
1202 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1205 self.mtype
= v
.type_bool
(self)
1209 redef class AAsCastExpr
1210 redef fun accept_typing
(v
)
1212 self.mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
1216 redef class AAsNotnullExpr
1217 redef fun accept_typing
(v
)
1219 var mtype
= v
.visit_expr
(self.n_expr
)
1220 if mtype
== null then return # Forward error
1222 if mtype
isa MNullType then
1223 v
.error
(self, "Type error: as(not null) on null")
1226 if mtype
isa MNullableType then
1227 self.mtype
= mtype
.mtype
1232 if mtype
isa MClassType then
1233 v
.modelbuilder
.warning
(self, "useless-type-test", "Warning: expression is already not null, since it is a `{mtype}`.")
1236 assert mtype
.need_anchor
1237 var u
= v
.anchor_to
(mtype
)
1238 if not u
isa MNullableType then
1239 v
.modelbuilder
.warning
(self, "useless-type-test", "Warning: expression is already not null, since it is a `{mtype}: {u}`.")
1245 redef class AProxyExpr
1246 redef fun accept_typing
(v
)
1248 self.mtype
= v
.visit_expr
(self.n_expr
)
1252 redef class ASelfExpr
1253 redef var its_variable
: nullable Variable
1254 redef fun accept_typing
(v
)
1256 if v
.is_toplevel_context
and not self isa AImplicitSelfExpr then
1257 v
.error
(self, "Error: self cannot be used in top-level method.")
1259 var variable
= v
.selfvariable
1260 self.its_variable
= variable
1261 self.mtype
= v
.get_variable
(self, variable
)
1265 ## MESSAGE SENDING AND PROPERTY
1267 redef class ASendExpr
1268 # The property invoked by the send.
1269 var callsite
: nullable CallSite
1271 redef fun accept_typing
(v
)
1273 var recvtype
= v
.visit_expr
(self.n_expr
)
1274 var name
= self.property_name
1276 if recvtype
== null then return # Forward error
1277 if recvtype
isa MNullType then
1278 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1282 var callsite
= v
.get_method
(self, recvtype
, name
, self.n_expr
isa ASelfExpr)
1283 if callsite
== null then return
1284 self.callsite
= callsite
1285 var msignature
= callsite
.msignature
1287 var args
= compute_raw_arguments
1289 callsite
.check_signature
(v
, args
)
1291 if callsite
.mproperty
.is_init
then
1292 var vmpropdef
= v
.mpropdef
1293 if not (vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_init
) then
1294 v
.error
(self, "Can call a init only in another init")
1296 if vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_root_init
and not callsite
.mproperty
.is_root_init
then
1297 v
.error
(self, "Error: {vmpropdef} cannot call a factory {callsite.mproperty}")
1301 var ret
= msignature
.return_mtype
1305 self.is_typed
= true
1309 # The name of the property
1310 # Each subclass simply provide the correct name.
1311 private fun property_name
: String is abstract
1313 # An array of all arguments (excluding self)
1314 fun raw_arguments
: Array[AExpr] do return compute_raw_arguments
1316 private fun compute_raw_arguments
: Array[AExpr] is abstract
1319 redef class ABinopExpr
1320 redef fun compute_raw_arguments
do return [n_expr2
]
1323 redef fun property_name
do return "=="
1324 redef fun accept_typing
(v
)
1328 var variable
= self.n_expr
.its_variable
1329 if variable
== null then return
1330 var mtype
= self.n_expr2
.mtype
1331 if not mtype
isa MNullType then return
1332 var vartype
= v
.get_variable
(self, variable
)
1333 if not vartype
isa MNullableType then return
1334 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1335 self.after_flow_context
.when_false
.set_var
(variable
, vartype
.mtype
)
1336 #debug("adapt {variable}:{vartype} ; true->{mtype} false->{vartype.mtype}")
1340 redef fun property_name
do return "!="
1341 redef fun accept_typing
(v
)
1345 var variable
= self.n_expr
.its_variable
1346 if variable
== null then return
1347 var mtype
= self.n_expr2
.mtype
1348 if not mtype
isa MNullType then return
1349 var vartype
= v
.get_variable
(self, variable
)
1350 if not vartype
isa MNullableType then return
1351 self.after_flow_context
.when_false
.set_var
(variable
, mtype
)
1352 self.after_flow_context
.when_true
.set_var
(variable
, vartype
.mtype
)
1353 #debug("adapt {variable}:{vartype} ; true->{vartype.mtype} false->{mtype}")
1357 redef fun property_name
do return "<"
1360 redef fun property_name
do return "<="
1363 redef fun property_name
do return "<<"
1366 redef fun property_name
do return ">"
1369 redef fun property_name
do return ">="
1372 redef fun property_name
do return ">>"
1374 redef class APlusExpr
1375 redef fun property_name
do return "+"
1377 redef class AMinusExpr
1378 redef fun property_name
do return "-"
1380 redef class AStarshipExpr
1381 redef fun property_name
do return "<=>"
1383 redef class AStarExpr
1384 redef fun property_name
do return "*"
1386 redef class AStarstarExpr
1387 redef fun property_name
do return "**"
1389 redef class ASlashExpr
1390 redef fun property_name
do return "/"
1392 redef class APercentExpr
1393 redef fun property_name
do return "%"
1396 redef class AUminusExpr
1397 redef fun property_name
do return "unary -"
1398 redef fun compute_raw_arguments
do return new Array[AExpr]
1402 redef class ACallExpr
1403 redef fun property_name
do return n_id
.text
1404 redef fun compute_raw_arguments
do return n_args
.to_a
1407 redef class ACallAssignExpr
1408 redef fun property_name
do return n_id
.text
+ "="
1409 redef fun compute_raw_arguments
1411 var res
= n_args
.to_a
1417 redef class ABraExpr
1418 redef fun property_name
do return "[]"
1419 redef fun compute_raw_arguments
do return n_args
.to_a
1422 redef class ABraAssignExpr
1423 redef fun property_name
do return "[]="
1424 redef fun compute_raw_arguments
1426 var res
= n_args
.to_a
1432 redef class ASendReassignFormExpr
1433 # The property invoked for the writing
1434 var write_callsite
: nullable CallSite
1436 redef fun accept_typing
(v
)
1438 var recvtype
= v
.visit_expr
(self.n_expr
)
1439 var name
= self.property_name
1441 if recvtype
== null then return # Forward error
1442 if recvtype
isa MNullType then
1443 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1447 var for_self
= self.n_expr
isa ASelfExpr
1448 var callsite
= v
.get_method
(self, recvtype
, name
, for_self
)
1450 if callsite
== null then return
1451 self.callsite
= callsite
1453 var args
= compute_raw_arguments
1455 callsite
.check_signature
(v
, args
)
1457 var readtype
= callsite
.msignature
.return_mtype
1458 if readtype
== null then
1459 v
.error
(self, "Error: {name} is not a function")
1463 var wcallsite
= v
.get_method
(self, recvtype
, name
+ "=", self.n_expr
isa ASelfExpr)
1464 if wcallsite
== null then return
1465 self.write_callsite
= wcallsite
1467 var wtype
= self.resolve_reassignment
(v
, readtype
, wcallsite
.msignature
.mparameters
.last
.mtype
)
1468 if wtype
== null then return
1470 args
= args
.to_a
# duplicate so raw_arguments keeps only the getter args
1471 args
.add
(self.n_value
)
1472 wcallsite
.check_signature
(v
, args
)
1474 self.is_typed
= true
1478 redef class ACallReassignExpr
1479 redef fun property_name
do return n_id
.text
1480 redef fun compute_raw_arguments
do return n_args
.to_a
1483 redef class ABraReassignExpr
1484 redef fun property_name
do return "[]"
1485 redef fun compute_raw_arguments
do return n_args
.to_a
1488 redef class AInitExpr
1489 redef fun property_name
do return "init"
1490 redef fun compute_raw_arguments
do return n_args
.to_a
1494 fun to_a
: Array[AExpr] do return self.n_exprs
.to_a
1499 redef class ASuperExpr
1500 # The method to call if the super is in fact a 'super init call'
1501 # Note: if the super is a normal call-next-method, then this attribute is null
1502 var callsite
: nullable CallSite
1504 # The method to call is the super is a standard `call-next-method` super-call
1505 # Note: if the super is a special super-init-call, then this attribute is null
1506 var mpropdef
: nullable MMethodDef
1508 redef fun accept_typing
(v
)
1510 var anchor
= v
.anchor
1511 assert anchor
!= null
1512 var recvtype
= v
.get_variable
(self, v
.selfvariable
)
1513 assert recvtype
!= null
1514 var mproperty
= v
.mpropdef
.mproperty
1515 if not mproperty
isa MMethod then
1516 v
.error
(self, "Error: super only usable in a method")
1519 var superprops
= mproperty
.lookup_super_definitions
(v
.mmodule
, anchor
)
1520 if superprops
.length
== 0 then
1521 if mproperty
.is_init
and v
.mpropdef
.is_intro
then
1522 process_superinit
(v
)
1525 v
.error
(self, "Error: No super method to call for {mproperty}.")
1528 # FIXME: covariance of return type in linear extension?
1529 var superprop
= superprops
.first
1531 var msignature
= superprop
.msignature
.as(not null)
1532 msignature
= v
.resolve_for
(msignature
, recvtype
, true).as(MSignature)
1533 var args
= self.n_args
.to_a
1534 if args
.length
> 0 then
1535 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1537 self.mtype
= msignature
.return_mtype
1538 self.is_typed
= true
1539 v
.mpropdef
.has_supercall
= true
1540 mpropdef
= v
.mpropdef
.as(MMethodDef)
1543 private fun process_superinit
(v
: TypeVisitor)
1545 var anchor
= v
.anchor
1546 assert anchor
!= null
1547 var recvtype
= v
.get_variable
(self, v
.selfvariable
)
1548 assert recvtype
!= null
1549 var mpropdef
= v
.mpropdef
1550 assert mpropdef
isa MMethodDef
1551 var mproperty
= mpropdef
.mproperty
1552 var superprop
: nullable MMethodDef = null
1553 for msupertype
in mpropdef
.mclassdef
.supertypes
do
1554 msupertype
= msupertype
.anchor_to
(v
.mmodule
, anchor
)
1555 var errcount
= v
.modelbuilder
.toolcontext
.error_count
1556 var candidate
= v
.try_get_mproperty_by_name2
(self, msupertype
, mproperty
.name
).as(nullable MMethod)
1557 if candidate
== null then
1558 if v
.modelbuilder
.toolcontext
.error_count
> errcount
then return # Forward error
1559 continue # Try next super-class
1561 if superprop
!= null and candidate
.is_root_init
then
1564 if superprop
!= null and superprop
.mproperty
!= candidate
and not superprop
.mproperty
.is_root_init
then
1565 v
.error
(self, "Error: conflicting super constructor to call for {mproperty}: {candidate.full_name}, {superprop.mproperty.full_name}")
1568 var candidatedefs
= candidate
.lookup_definitions
(v
.mmodule
, anchor
)
1569 if superprop
!= null and superprop
.mproperty
== candidate
then
1570 if superprop
== candidatedefs
.first
then continue
1571 candidatedefs
.add
(superprop
)
1573 if candidatedefs
.length
> 1 then
1574 v
.error
(self, "Error: conflicting property definitions for property {mproperty} in {recvtype}: {candidatedefs.join(", ")}")
1577 superprop
= candidatedefs
.first
1579 if superprop
== null then
1580 v
.error
(self, "Error: No super method to call for {mproperty}.")
1584 var msignature
= superprop
.new_msignature
or else superprop
.msignature
.as(not null)
1585 msignature
= v
.resolve_for
(msignature
, recvtype
, true).as(MSignature)
1587 var callsite
= new CallSite(self, recvtype
, v
.mmodule
, v
.anchor
, true, superprop
.mproperty
, superprop
, msignature
, false)
1588 self.callsite
= callsite
1590 var args
= self.n_args
.to_a
1591 if args
.length
> 0 then
1592 callsite
.check_signature
(v
, args
)
1594 # Check there is at least enough parameters
1595 if mpropdef
.msignature
.arity
< msignature
.arity
then
1596 v
.error
(self, "Error: Not enough implicit arguments to pass. Got {mpropdef.msignature.arity}, expected at least {msignature.arity}. Signature is {msignature}")
1599 # Check that each needed parameter is conform
1601 for sp
in msignature
.mparameters
do
1602 var p
= mpropdef
.msignature
.mparameters
[i
]
1603 if not v
.is_subtype
(p
.mtype
, sp
.mtype
) then
1604 v
.error
(self, "Type error: expected argument #{i} of type {sp.mtype}, got implicit argument {p.name} of type {p.mtype}. Signature is {msignature}")
1611 self.is_typed
= true
1617 redef class ANewExpr
1618 # The constructor invoked by the new.
1619 var callsite
: nullable CallSite
1621 redef fun accept_typing
(v
)
1623 var recvtype
= v
.resolve_mtype
(self.n_type
)
1624 if recvtype
== null then return
1625 self.mtype
= recvtype
1627 if not recvtype
isa MClassType then
1628 if recvtype
isa MNullableType then
1629 v
.error
(self, "Type error: cannot instantiate the nullable type {recvtype}.")
1632 v
.error
(self, "Type error: cannot instantiate the formal type {recvtype}.")
1636 if recvtype
.mclass
.kind
== abstract_kind
then
1637 v
.error
(self, "Cannot instantiate abstract class {recvtype}.")
1639 else if recvtype
.mclass
.kind
== interface_kind
then
1640 v
.error
(self, "Cannot instantiate interface {recvtype}.")
1652 var callsite
= v
.get_method
(self, recvtype
, name
, false)
1653 if callsite
== null then return
1655 self.callsite
= callsite
1657 if not callsite
.mproperty
.is_init_for
(recvtype
.mclass
) then
1658 v
.error
(self, "Error: {name} is not a constructor.")
1662 var args
= n_args
.to_a
1663 callsite
.check_signature
(v
, args
)
1669 redef class AAttrFormExpr
1670 # The attribute acceded.
1671 var mproperty
: nullable MAttribute
1673 # The static type of the attribute.
1674 var attr_type
: nullable MType
1676 # Resolve the attribute acceded.
1677 private fun resolve_property
(v
: TypeVisitor)
1679 var recvtype
= v
.visit_expr
(self.n_expr
)
1680 if recvtype
== null then return # Skip error
1681 var name
= self.n_id
.text
1682 if recvtype
isa MNullType then
1683 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1687 var unsafe_type
= v
.anchor_to
(recvtype
)
1688 var mproperty
= v
.try_get_mproperty_by_name2
(self, unsafe_type
, name
)
1689 if mproperty
== null then
1690 v
.modelbuilder
.error
(self, "Error: Attribute {name} doesn't exists in {recvtype}.")
1693 assert mproperty
isa MAttribute
1694 self.mproperty
= mproperty
1696 var mpropdefs
= mproperty
.lookup_definitions
(v
.mmodule
, unsafe_type
)
1697 assert mpropdefs
.length
== 1
1698 var mpropdef
= mpropdefs
.first
1699 var attr_type
= mpropdef
.static_mtype
.as(not null)
1700 attr_type
= v
.resolve_for
(attr_type
, recvtype
, self.n_expr
isa ASelfExpr)
1701 self.attr_type
= attr_type
1705 redef class AAttrExpr
1706 redef fun accept_typing
(v
)
1708 self.resolve_property
(v
)
1709 self.mtype
= self.attr_type
1714 redef class AAttrAssignExpr
1715 redef fun accept_typing
(v
)
1717 self.resolve_property
(v
)
1718 var mtype
= self.attr_type
1720 v
.visit_expr_subtype
(self.n_value
, mtype
)
1721 self.is_typed
= true
1725 redef class AAttrReassignExpr
1726 redef fun accept_typing
(v
)
1728 self.resolve_property
(v
)
1729 var mtype
= self.attr_type
1730 if mtype
== null then return # Skip error
1732 self.resolve_reassignment
(v
, mtype
, mtype
)
1734 self.is_typed
= true
1738 redef class AIssetAttrExpr
1739 redef fun accept_typing
(v
)
1741 self.resolve_property
(v
)
1742 var mtype
= self.attr_type
1743 if mtype
== null then return # Skip error
1745 var recvtype
= self.n_expr
.mtype
.as(not null)
1746 var bound
= v
.resolve_for
(mtype
, recvtype
, false)
1747 if bound
isa MNullableType then
1748 v
.error
(self, "Error: isset on a nullable attribute.")
1750 self.mtype
= v
.type_bool
(self)
1756 redef class ADebugTypeExpr
1757 redef fun accept_typing
(v
)
1759 var expr
= v
.visit_expr
(self.n_expr
)
1760 if expr
== null then return
1761 var unsafe
= v
.anchor_to
(expr
)
1762 var ntype
= self.n_type
1763 var mtype
= v
.resolve_mtype
(ntype
)
1764 if mtype
!= null and mtype
!= expr
then
1765 var umtype
= v
.anchor_to
(mtype
)
1766 v
.modelbuilder
.warning
(self, "debug", "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")
1768 self.is_typed
= true