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 # Special verification on != and == for null
200 fun null_test
(anode
: ABinopExpr)
202 var mtype
= anode
.n_expr
.mtype
203 var mtype2
= anode
.n_expr2
.mtype
205 if mtype
== null or mtype2
== null then return
207 if not mtype2
isa MNullType then return
209 if not mtype
isa MNullableType then
213 # Check for type adaptation
214 var variable
= anode
.n_expr
.its_variable
215 if variable
== null then return
217 if anode
isa AEqExpr then
218 anode
.after_flow_context
.when_true
.set_var
(variable
, mtype2
)
219 anode
.after_flow_context
.when_false
.set_var
(variable
, mtype
.mtype
)
220 else if anode
isa ANeExpr then
221 anode
.after_flow_context
.when_false
.set_var
(variable
, mtype2
)
222 anode
.after_flow_context
.when_true
.set_var
(variable
, mtype
.mtype
)
228 fun try_get_mproperty_by_name2
(anode
: ANode, mtype
: MType, name
: String): nullable MProperty
230 return self.modelbuilder
.try_get_mproperty_by_name2
(anode
, mmodule
, mtype
, name
)
233 fun resolve_mtype
(node
: AType): nullable MType
235 return self.modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, node
)
238 fun try_get_mclass
(node
: ANode, name
: String): nullable MClass
240 var mclass
= modelbuilder
.try_get_mclass_by_name
(node
, mmodule
, name
)
244 fun get_mclass
(node
: ANode, name
: String): nullable MClass
246 var mclass
= modelbuilder
.try_get_mclass_by_name
(node
, mmodule
, name
)
247 if mclass
== null then
248 self.modelbuilder
.error
(node
, "Type Error: missing primitive class `{name}'.")
253 fun type_bool
(node
: ANode): nullable MType
255 var mclass
= self.get_mclass
(node
, "Bool")
256 if mclass
== null then return null
257 return mclass
.mclass_type
260 fun get_method
(node
: ANode, recvtype
: MType, name
: String, recv_is_self
: Bool): nullable CallSite
262 var unsafe_type
= self.anchor_to
(recvtype
)
264 #debug("recv: {recvtype} (aka {unsafe_type})")
265 if recvtype
isa MNullType then
266 self.error
(node
, "Error: Method '{name}' call on 'null'.")
270 var mproperty
= self.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
271 if mproperty
== null then
272 #self.modelbuilder.error(node, "Type error: property {name} not found in {unsafe_type} (ie {recvtype})")
274 self.modelbuilder
.error
(node
, "Error: Method or variable '{name}' unknown in {recvtype}.")
276 self.modelbuilder
.error
(node
, "Error: Method '{name}' doesn't exists in {recvtype}.")
281 assert mproperty
isa MMethod
283 if is_toplevel_context
and recv_is_self
and not mproperty
.is_toplevel
then
284 error
(node
, "Error: '{name}' is not a top-level method, thus need a receiver.")
286 if not recv_is_self
and mproperty
.is_toplevel
then
287 error
(node
, "Error: cannot call '{name}', a top-level method, with a receiver.")
290 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
291 self.modelbuilder
.error
(node
, "Error: Method '{name}' is protected and can only acceded by self.")
295 var info
= mproperty
.deprecation
296 if info
!= null and self.mpropdef
.mproperty
.deprecation
== null then
299 self.modelbuilder
.warning
(node
, "deprecated-method", "Deprecation Warning: Method '{name}' is deprecated: {mdoc.content.first}")
301 self.modelbuilder
.warning
(node
, "deprecated-method", "Deprecation Warning: Method '{name}' is deprecated.")
305 var propdefs
= mproperty
.lookup_definitions
(self.mmodule
, unsafe_type
)
307 if propdefs
.length
== 0 then
308 self.modelbuilder
.error
(node
, "Type error: no definition found for property {name} in {unsafe_type}")
310 else if propdefs
.length
== 1 then
311 mpropdef
= propdefs
.first
313 self.modelbuilder
.warning
(node
, "property-conflict", "Warning: conflicting property definitions for property {name} in {unsafe_type}: {propdefs.join(" ")}")
314 mpropdef
= mproperty
.intro
318 var msignature
= mpropdef
.new_msignature
or else mpropdef
.msignature
.as(not null)
319 msignature
= resolve_for
(msignature
, recvtype
, recv_is_self
).as(MSignature)
321 var erasure_cast
= false
322 var rettype
= mpropdef
.msignature
.return_mtype
323 if not recv_is_self
and rettype
!= null then
324 rettype
= rettype
.as_notnullable
325 if rettype
isa MParameterType then
326 var erased_rettype
= msignature
.return_mtype
327 assert erased_rettype
!= null
328 #node.debug("Erasure cast: Really a {rettype} but unsafely a {erased_rettype}")
333 var callsite
= new CallSite(node
, recvtype
, mmodule
, anchor
, recv_is_self
, mproperty
, mpropdef
, msignature
, erasure_cast
)
337 fun try_get_method
(node
: ANode, recvtype
: MType, name
: String, recv_is_self
: Bool): nullable CallSite
339 var unsafe_type
= self.anchor_to
(recvtype
)
340 var mproperty
= self.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
341 if mproperty
== null then return null
342 return get_method
(node
, recvtype
, name
, recv_is_self
)
346 # Visit the expressions of args and check their conformity with the corresponding type in signature
347 # The point of this method is to handle varargs correctly
348 # Note: The signature must be correctly adapted
349 fun check_signature
(node
: ANode, args
: Array[AExpr], name
: String, msignature
: MSignature): Bool
351 var vararg_rank
= msignature
.vararg_rank
352 if vararg_rank
>= 0 then
353 if args
.length
< msignature
.arity
then
354 #self.modelbuilder.error(node, "Error: Incorrect number of parameters. Got {args.length}, expected at least {msignature.arity}. Signature is {msignature}")
355 self.modelbuilder
.error
(node
, "Error: arity mismatch; prototype is '{name}{msignature}'")
358 else if args
.length
!= msignature
.arity
then
359 self.modelbuilder
.error
(node
, "Error: Incorrect number of parameters. Got {args.length}, expected {msignature.arity}. Signature is {msignature}")
363 #debug("CALL {unsafe_type}.{msignature}")
365 var vararg_decl
= args
.length
- msignature
.arity
366 for i
in [0..msignature
.arity
[ do
368 if i
== vararg_rank
then continue # skip the vararg
369 if i
> vararg_rank
then
372 var paramtype
= msignature
.mparameters
[i
].mtype
373 self.visit_expr_subtype
(args
[j
], paramtype
)
375 if vararg_rank
>= 0 then
376 var varargs
= new Array[AExpr]
377 var paramtype
= msignature
.mparameters
[vararg_rank
].mtype
378 for j
in [vararg_rank
..vararg_rank
+vararg_decl
] do
380 self.visit_expr_subtype
(args
[j
], paramtype
)
386 fun error
(node
: ANode, message
: String)
388 self.modelbuilder
.toolcontext
.error
(node
.hot_location
, message
)
391 fun get_variable
(node
: AExpr, variable
: Variable): nullable MType
393 var flow
= node
.after_flow_context
395 self.error
(node
, "No context!")
399 if flow
.vars
.has_key
(variable
) then
400 return flow
.vars
[variable
]
402 #node.debug("*** START Collected for {variable}")
403 var mtypes
= flow
.collect_types
(variable
)
404 #node.debug("**** END Collected for {variable}")
405 if mtypes
== null or mtypes
.length
== 0 then
406 return variable
.declared_type
407 else if mtypes
.length
== 1 then
410 var res
= merge_types
(node
,mtypes
)
411 if res
== null then res
= variable
.declared_type
417 fun set_variable
(node
: AExpr, variable
: Variable, mtype
: nullable MType)
419 var flow
= node
.after_flow_context
422 flow
.set_var
(variable
, mtype
)
425 fun merge_types
(node
: ANode, col
: Array[nullable MType]): nullable MType
427 if col
.length
== 1 then return col
.first
428 var res
= new Array[nullable MType]
430 if t1
== null then continue # return null
433 if t2
== null then continue # return null
434 if t2
isa MNullableType or t2
isa MNullType then
437 if not is_subtype
(t2
, t1
) then found
= false
440 #print "merge {col.join(" ")} -> {t1}"
444 #self.modelbuilder.warning(node, "Type Error: {col.length} conflicting types: <{col.join(", ")}>")
449 # A specific method call site with its associated informations.
451 # The associated node for location
454 # The static type of the receiver (possibly unresolved)
457 # The module where the callsite is present
460 # The anchor to use with `recv` or `msignature`
461 var anchor
: nullable MClassType
463 # Is the receiver self?
464 # If "for_self", virtual types of the signature are kept
465 # If "not_for_self", virtual type are erased
466 var recv_is_self
: Bool
468 # The designated method
469 var mproperty
: MMethod
471 # The statically designated method definition
472 # The most specif one, it is.
473 var mpropdef
: MMethodDef
475 # The resolved signature for the receiver
476 var msignature
: MSignature
478 # Is a implicit cast required on erasure typing policy?
479 var erasure_cast
: Bool
481 private fun check_signature
(v
: TypeVisitor, args
: Array[AExpr]): Bool
483 return v
.check_signature
(self.node
, args
, self.mproperty
.name
, self.msignature
)
488 # The declared type of the variable
489 var declared_type
: nullable MType
492 redef class FlowContext
493 # Store changes of types because of type evolution
494 private var vars
: HashMap[Variable, nullable MType] = new HashMap[Variable, nullable MType]
495 private var cache
: HashMap[Variable, nullable Array[nullable MType]] = new HashMap[Variable, nullable Array[nullable MType]]
497 # Adapt the variable to a static type
498 # Warning1: do not modify vars directly.
499 # Warning2: sub-flow may have cached a unadapted variable
500 private fun set_var
(variable
: Variable, mtype
: nullable MType)
502 self.vars
[variable
] = mtype
503 self.cache
.keys
.remove
(variable
)
506 private fun collect_types
(variable
: Variable): nullable Array[nullable MType]
508 if cache
.has_key
(variable
) then
509 return cache
[variable
]
511 var res
: nullable Array[nullable MType] = null
512 if vars
.has_key
(variable
) then
513 var mtype
= vars
[variable
]
515 else if self.previous
.is_empty
then
517 res
= [variable
.declared_type
]
519 for flow
in self.previous
do
520 if flow
.is_unreachable
then continue
521 var r2
= flow
.collect_types
(variable
)
522 if r2
== null then continue
527 if not res
.has
(t
) then res
.add
(t
)
532 cache
[variable
] = res
538 # The entry point of the whole typing analysis
539 fun do_typing
(modelbuilder
: ModelBuilder)
543 # The variable associated to the receiver (if any)
544 var selfvariable
: nullable Variable
547 redef class AMethPropdef
548 redef fun do_typing
(modelbuilder
: ModelBuilder)
550 var nblock
= self.n_block
551 if nblock
== null then return
553 var mpropdef
= self.mpropdef
.as(not null)
554 var v
= new TypeVisitor(modelbuilder
, mpropdef
.mclassdef
.mmodule
, mpropdef
)
555 self.selfvariable
= v
.selfvariable
557 var mmethoddef
= self.mpropdef
.as(not null)
558 for i
in [0..mmethoddef
.msignature
.arity
[ do
559 var mtype
= mmethoddef
.msignature
.mparameters
[i
].mtype
560 if mmethoddef
.msignature
.vararg_rank
== i
then
561 var arrayclass
= v
.get_mclass
(self.n_signature
.n_params
[i
], "Array")
562 if arrayclass
== null then return # Skip error
563 mtype
= arrayclass
.get_mtype
([mtype
])
565 var variable
= self.n_signature
.n_params
[i
].variable
566 assert variable
!= null
567 variable
.declared_type
= mtype
571 if not nblock
.after_flow_context
.is_unreachable
and mmethoddef
.msignature
.return_mtype
!= null then
572 # We reach the end of the function without having a return, it is bad
573 v
.error
(self, "Control error: Reached end of function (a 'return' with a value was expected).")
578 redef class AAttrPropdef
579 redef fun do_typing
(modelbuilder
: ModelBuilder)
581 var mpropdef
= self.mpropdef
.as(not null)
582 var v
= new TypeVisitor(modelbuilder
, mpropdef
.mclassdef
.mmodule
, mpropdef
)
583 self.selfvariable
= v
.selfvariable
585 var nexpr
= self.n_expr
586 if nexpr
!= null then
587 var mtype
= self.mpropdef
.static_mtype
588 v
.visit_expr_subtype
(nexpr
, mtype
)
596 # The static type of the expression.
597 # null if self is a statement or in case of error
598 var mtype
: nullable MType = null
600 # Is the statement correctly typed?
601 # Used to distinguish errors and statements when `mtype == null`
602 var is_typed
: Bool = false
604 # If required, the following implicit cast `.as(XXX)`
605 # Such a cast may by required after evaluating the expression when
606 # a unsafe operation is detected (silently accepted by the Nit language).
607 # The attribute is computed by `check_subtype`
608 var implicit_cast_to
: nullable MType = null
610 # Return the variable read (if any)
611 # Used to perform adaptive typing
612 fun its_variable
: nullable Variable do return null
614 private fun accept_typing
(v
: TypeVisitor)
616 v
.error
(self, "no implemented accept_typing for {self.class_name}")
620 redef class ABlockExpr
621 redef fun accept_typing
(v
)
623 for e
in self.n_expr
do v
.visit_stmt
(e
)
627 # The type of a blockexpr is the one of the last expression (or null if empty)
630 if self.n_expr
.is_empty
then return null
631 return self.n_expr
.last
.mtype
635 redef class AVardeclExpr
636 redef fun accept_typing
(v
)
638 var variable
= self.variable
639 if variable
== null then return # Skip error
641 var ntype
= self.n_type
642 var mtype
: nullable MType
643 if ntype
== null then
646 mtype
= v
.resolve_mtype
(ntype
)
647 if mtype
== null then return # Skip error
650 var nexpr
= self.n_expr
651 if nexpr
!= null then
652 if mtype
!= null then
653 v
.visit_expr_subtype
(nexpr
, mtype
)
655 mtype
= v
.visit_expr
(nexpr
)
656 if mtype
== null then return # Skip error
661 if mtype
== null or mtype
isa MNullType then
662 decltype
= v
.get_mclass
(self, "Object").mclass_type
.as_nullable
663 if mtype
== null then mtype
= decltype
666 variable
.declared_type
= decltype
667 v
.set_variable
(self, variable
, mtype
)
669 #debug("var {variable}: {mtype}")
676 redef fun its_variable
do return self.variable
677 redef fun accept_typing
(v
)
679 var variable
= self.variable
680 if variable
== null then return # Skip error
682 var mtype
= v
.get_variable
(self, variable
)
683 if mtype
!= null then
684 #debug("{variable} is {mtype}")
686 #debug("{variable} is untyped")
693 redef class AVarAssignExpr
694 redef fun accept_typing
(v
)
696 var variable
= self.variable
697 assert variable
!= null
699 var mtype
= v
.visit_expr_subtype
(n_value
, variable
.declared_type
)
701 v
.set_variable
(self, variable
, mtype
)
707 redef class AReassignFormExpr
708 # The method designed by the reassign operator.
709 var reassign_callsite
: nullable CallSite
711 var read_type
: nullable MType = null
713 # Determine the `reassign_property`
714 # `readtype` is the type of the reading of the left value.
715 # `writetype` is the type of the writing of the left value.
716 # (Because of `ACallReassignExpr`, both can be different.
717 # Return the static type of the value to store.
718 private fun resolve_reassignment
(v
: TypeVisitor, readtype
, writetype
: MType): nullable MType
720 var reassign_name
: String
721 if self.n_assign_op
isa APlusAssignOp then
723 else if self.n_assign_op
isa AMinusAssignOp then
729 self.read_type
= readtype
731 if readtype
isa MNullType then
732 v
.error
(self, "Error: Method '{reassign_name}' call on 'null'.")
736 var callsite
= v
.get_method
(self, readtype
, reassign_name
, false)
737 if callsite
== null then return null # Skip error
738 self.reassign_callsite
= callsite
740 var msignature
= callsite
.msignature
741 var rettype
= msignature
.return_mtype
742 assert msignature
.arity
== 1 and rettype
!= null
744 var value_type
= v
.visit_expr_subtype
(self.n_value
, msignature
.mparameters
.first
.mtype
)
745 if value_type
== null then return null # Skip error
747 v
.check_subtype
(self, rettype
, writetype
)
752 redef class AVarReassignExpr
753 redef fun accept_typing
(v
)
755 var variable
= self.variable
756 assert variable
!= null
758 var readtype
= v
.get_variable
(self, variable
)
759 if readtype
== null then return
763 var writetype
= variable
.declared_type
764 if writetype
== null then return
766 var rettype
= self.resolve_reassignment
(v
, readtype
, writetype
)
768 v
.set_variable
(self, variable
, rettype
)
775 redef class AContinueExpr
776 redef fun accept_typing
(v
)
778 var nexpr
= self.n_expr
779 if nexpr
!= null then
780 var mtype
= v
.visit_expr
(nexpr
)
786 redef class ABreakExpr
787 redef fun accept_typing
(v
)
789 var nexpr
= self.n_expr
790 if nexpr
!= null then
791 var mtype
= v
.visit_expr
(nexpr
)
797 redef class AReturnExpr
798 redef fun accept_typing
(v
)
800 var nexpr
= self.n_expr
801 var ret_type
= v
.mpropdef
.as(MMethodDef).msignature
.return_mtype
802 if nexpr
!= null then
803 if ret_type
!= null then
804 var mtype
= v
.visit_expr_subtype
(nexpr
, ret_type
)
806 var mtype
= v
.visit_expr
(nexpr
)
807 v
.error
(self, "Error: Return with value in a procedure.")
809 else if ret_type
!= null then
810 v
.error
(self, "Error: Return without value in a function.")
816 redef class AAbortExpr
817 redef fun accept_typing
(v
)
824 redef fun accept_typing
(v
)
826 v
.visit_expr_bool
(n_expr
)
834 redef class AIfexprExpr
835 redef fun accept_typing
(v
)
837 v
.visit_expr_bool
(n_expr
)
839 var t1
= v
.visit_expr
(n_then
)
840 var t2
= v
.visit_expr
(n_else
)
842 if t1
== null or t2
== null then
846 var t
= v
.merge_types
(self, [t1
, t2
])
848 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
855 redef fun accept_typing
(v
)
857 v
.visit_stmt
(n_block
)
862 redef class AWhileExpr
863 redef fun accept_typing
(v
)
865 v
.visit_expr_bool
(n_expr
)
867 v
.visit_stmt
(n_block
)
872 redef class ALoopExpr
873 redef fun accept_typing
(v
)
875 v
.visit_stmt
(n_block
)
881 var coltype
: nullable MClassType
883 var method_iterator
: nullable CallSite
884 var method_is_ok
: nullable CallSite
885 var method_item
: nullable CallSite
886 var method_next
: nullable CallSite
887 var method_key
: nullable CallSite
888 var method_finish
: nullable CallSite
890 private fun do_type_iterator
(v
: TypeVisitor, mtype
: MType)
892 if mtype
isa MNullType then
893 v
.error
(self, "Type error: 'for' cannot iterate over 'null'")
898 var objcla
= v
.get_mclass
(self, "Object")
899 if objcla
== null then return
901 # check iterator method
902 var itdef
= v
.get_method
(self, mtype
, "iterator", n_expr
isa ASelfExpr)
903 if itdef
== null then
904 v
.error
(self, "Type Error: 'for' expects a type providing 'iterator' method, got '{mtype}'.")
907 self.method_iterator
= itdef
909 # check that iterator return something
910 var ittype
= itdef
.msignature
.return_mtype
911 if ittype
== null then
912 v
.error
(self, "Type Error: 'for' expects method 'iterator' to return an 'Iterator' or 'MapIterator' type'.")
917 var colit_cla
= v
.try_get_mclass
(self, "Iterator")
918 var mapit_cla
= v
.try_get_mclass
(self, "MapIterator")
922 if colit_cla
!= null and v
.is_subtype
(ittype
, colit_cla
.get_mtype
([objcla
.mclass_type
.as_nullable
])) then
924 var coltype
= ittype
.supertype_to
(v
.mmodule
, v
.anchor
, colit_cla
)
925 var variables
= self.variables
926 if variables
.length
!= 1 then
927 v
.error
(self, "Type Error: 'for' expects only one variable when using 'Iterator'.")
929 variables
.first
.declared_type
= coltype
.arguments
.first
934 if mapit_cla
!= null and v
.is_subtype
(ittype
, mapit_cla
.get_mtype
([objcla
.mclass_type
, objcla
.mclass_type
.as_nullable
])) then
936 var coltype
= ittype
.supertype_to
(v
.mmodule
, v
.anchor
, mapit_cla
)
937 var variables
= self.variables
938 if variables
.length
!= 2 then
939 v
.error
(self, "Type Error: 'for' expects two variables when using 'MapIterator'.")
941 variables
[0].declared_type
= coltype
.arguments
[0]
942 variables
[1].declared_type
= coltype
.arguments
[1]
947 if not is_col
and not is_map
then
948 v
.error
(self, "Type Error: 'for' expects method 'iterator' to return an 'Iterator' or 'MapIterator' type'.")
952 # anchor formal and virtual types
953 if mtype
.need_anchor
then mtype
= v
.anchor_to
(mtype
)
955 mtype
= mtype
.as_notnullable
956 self.coltype
= mtype
.as(MClassType)
958 # get methods is_ok, next, item
959 var ikdef
= v
.get_method
(self, ittype
, "is_ok", false)
960 if ikdef
== null then
961 v
.error
(self, "Type Error: 'for' expects a method 'is_ok' in 'Iterator' type {ittype}.")
964 self.method_is_ok
= ikdef
966 var itemdef
= v
.get_method
(self, ittype
, "item", false)
967 if itemdef
== null then
968 v
.error
(self, "Type Error: 'for' expects a method 'item' in 'Iterator' type {ittype}.")
971 self.method_item
= itemdef
973 var nextdef
= v
.get_method
(self, ittype
, "next", false)
974 if nextdef
== null then
975 v
.error
(self, "Type Error: 'for' expects a method 'next' in 'Iterator' type {ittype}.")
978 self.method_next
= nextdef
980 self.method_finish
= v
.try_get_method
(self, ittype
, "finish", false)
983 var keydef
= v
.get_method
(self, ittype
, "key", false)
984 if keydef
== null then
985 v
.error
(self, "Type Error: 'for' expects a method 'key' in 'Iterator' type {ittype}.")
988 self.method_key
= keydef
992 redef fun accept_typing
(v
)
994 var mtype
= v
.visit_expr
(n_expr
)
995 if mtype
== null then return
997 self.do_type_iterator
(v
, mtype
)
999 v
.visit_stmt
(n_block
)
1000 self.is_typed
= true
1004 redef class AAssertExpr
1005 redef fun accept_typing
(v
)
1007 v
.visit_expr_bool
(n_expr
)
1009 v
.visit_stmt
(n_else
)
1010 self.is_typed
= true
1015 redef fun accept_typing
(v
)
1017 v
.visit_expr_bool
(n_expr
)
1018 v
.visit_expr_bool
(n_expr2
)
1019 self.mtype
= v
.type_bool
(self)
1023 redef class AImpliesExpr
1024 redef fun accept_typing
(v
)
1026 v
.visit_expr_bool
(n_expr
)
1027 v
.visit_expr_bool
(n_expr2
)
1028 self.mtype
= v
.type_bool
(self)
1032 redef class AAndExpr
1033 redef fun accept_typing
(v
)
1035 v
.visit_expr_bool
(n_expr
)
1036 v
.visit_expr_bool
(n_expr2
)
1037 self.mtype
= v
.type_bool
(self)
1042 redef class ANotExpr
1043 redef fun accept_typing
(v
)
1045 v
.visit_expr_bool
(n_expr
)
1046 self.mtype
= v
.type_bool
(self)
1050 redef class AOrElseExpr
1051 redef fun accept_typing
(v
)
1053 var t1
= v
.visit_expr
(n_expr
)
1054 var t2
= v
.visit_expr
(n_expr2
)
1056 if t1
== null or t2
== null then
1060 t1
= t1
.as_notnullable
1062 var t
= v
.merge_types
(self, [t1
, t2
])
1064 t
= v
.mmodule
.object_type
1065 if t2
isa MNullableType then
1068 #v.error(self, "Type Error: ambiguous type {t1} vs {t2}")
1074 redef class ATrueExpr
1075 redef fun accept_typing
(v
)
1077 self.mtype
= v
.type_bool
(self)
1081 redef class AFalseExpr
1082 redef fun accept_typing
(v
)
1084 self.mtype
= v
.type_bool
(self)
1088 redef class AIntExpr
1089 redef fun accept_typing
(v
)
1091 var mclass
= v
.get_mclass
(self, "Int")
1092 if mclass
== null then return # Forward error
1093 self.mtype
= mclass
.mclass_type
1097 redef class AFloatExpr
1098 redef fun accept_typing
(v
)
1100 var mclass
= v
.get_mclass
(self, "Float")
1101 if mclass
== null then return # Forward error
1102 self.mtype
= mclass
.mclass_type
1106 redef class ACharExpr
1107 redef fun accept_typing
(v
)
1109 var mclass
= v
.get_mclass
(self, "Char")
1110 if mclass
== null then return # Forward error
1111 self.mtype
= mclass
.mclass_type
1115 redef class AStringFormExpr
1116 redef fun accept_typing
(v
)
1118 var mclass
= v
.get_mclass
(self, "String")
1119 if mclass
== null then return # Forward error
1120 self.mtype
= mclass
.mclass_type
1124 redef class ASuperstringExpr
1125 redef fun accept_typing
(v
)
1127 var mclass
= v
.get_mclass
(self, "String")
1128 if mclass
== null then return # Forward error
1129 self.mtype
= mclass
.mclass_type
1130 for nexpr
in self.n_exprs
do
1131 v
.visit_expr_subtype
(nexpr
, v
.mmodule
.object_type
)
1136 redef class AArrayExpr
1137 var with_capacity_callsite
: nullable CallSite
1138 var push_callsite
: nullable CallSite
1140 redef fun accept_typing
(v
)
1142 var mtype
: nullable MType = null
1143 var ntype
= self.n_type
1144 if ntype
!= null then
1145 mtype
= v
.resolve_mtype
(ntype
)
1146 if mtype
== null then return # Skip error
1148 var mtypes
= new Array[nullable MType]
1150 for e
in self.n_exprs
.n_exprs
do
1151 var t
= v
.visit_expr
(e
)
1155 if mtype
!= null then
1156 if v
.check_subtype
(e
, t
, mtype
) == null then return # Skip error
1157 if t
== mtype
then useless
= true
1162 if mtype
== null then
1163 mtype
= v
.merge_types
(self, mtypes
)
1165 if mtype
== null then
1166 v
.error
(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
1170 assert ntype
!= null
1171 v
.modelbuilder
.warning
(ntype
, "useless-type", "Warning: useless type declaration `{mtype}` in literal Array since it can be inferred from the elements type.")
1173 var mclass
= v
.get_mclass
(self, "Array")
1174 if mclass
== null then return # Forward error
1175 var array_mtype
= mclass
.get_mtype
([mtype
])
1177 with_capacity_callsite
= v
.get_method
(self, array_mtype
, "with_capacity", false)
1178 push_callsite
= v
.get_method
(self, array_mtype
, "push", false)
1180 self.mtype
= array_mtype
1184 redef class ARangeExpr
1185 var init_callsite
: nullable CallSite
1187 redef fun accept_typing
(v
)
1189 var discrete_class
= v
.get_mclass
(self, "Discrete")
1190 if discrete_class
== null then return # Forward error
1191 var discrete_type
= discrete_class
.intro
.bound_mtype
1192 var t1
= v
.visit_expr_subtype
(self.n_expr
, discrete_type
)
1193 var t2
= v
.visit_expr_subtype
(self.n_expr2
, discrete_type
)
1194 if t1
== null or t2
== null then return
1195 var mclass
= v
.get_mclass
(self, "Range")
1196 if mclass
== null then return # Forward error
1198 if v
.is_subtype
(t1
, t2
) then
1199 mtype
= mclass
.get_mtype
([t2
])
1200 else if v
.is_subtype
(t2
, t1
) then
1201 mtype
= mclass
.get_mtype
([t1
])
1203 v
.error
(self, "Type Error: Cannot create range: {t1} vs {t2}")
1209 # get the constructor
1211 if self isa ACrangeExpr then
1212 callsite
= v
.get_method
(self, mtype
, "init", false)
1213 else if self isa AOrangeExpr then
1214 callsite
= v
.get_method
(self, mtype
, "without_last", false)
1218 init_callsite
= callsite
1222 redef class ANullExpr
1223 redef fun accept_typing
(v
)
1225 self.mtype
= v
.mmodule
.model
.null_type
1229 redef class AIsaExpr
1230 # The static type to cast to.
1231 # (different from the static type of the expression that is `Bool`).
1232 var cast_type
: nullable MType
1233 redef fun accept_typing
(v
)
1235 var mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
1236 self.cast_type
= mtype
1238 var variable
= self.n_expr
.its_variable
1239 if variable
!= null then
1240 var orig
= self.n_expr
.mtype
1241 var from
= if orig
!= null then orig
.to_s
else "invalid"
1242 var to
= if mtype
!= null then mtype
.to_s
else "invalid"
1243 #debug("adapt {variable}: {from} -> {to}")
1244 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1247 self.mtype
= v
.type_bool
(self)
1251 redef class AAsCastExpr
1252 redef fun accept_typing
(v
)
1254 self.mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
1258 redef class AAsNotnullExpr
1259 redef fun accept_typing
(v
)
1261 var mtype
= v
.visit_expr
(self.n_expr
)
1262 if mtype
== null then return # Forward error
1264 if mtype
isa MNullType then
1265 v
.error
(self, "Type error: as(not null) on null")
1268 if mtype
isa MNullableType then
1269 self.mtype
= mtype
.mtype
1274 if mtype
isa MClassType then
1275 v
.modelbuilder
.warning
(self, "useless-type-test", "Warning: expression is already not null, since it is a `{mtype}`.")
1278 assert mtype
.need_anchor
1279 var u
= v
.anchor_to
(mtype
)
1280 if not u
isa MNullableType then
1281 v
.modelbuilder
.warning
(self, "useless-type-test", "Warning: expression is already not null, since it is a `{mtype}: {u}`.")
1287 redef class AProxyExpr
1288 redef fun accept_typing
(v
)
1290 self.mtype
= v
.visit_expr
(self.n_expr
)
1294 redef class ASelfExpr
1295 redef var its_variable
: nullable Variable
1296 redef fun accept_typing
(v
)
1298 if v
.is_toplevel_context
and not self isa AImplicitSelfExpr then
1299 v
.error
(self, "Error: self cannot be used in top-level method.")
1301 var variable
= v
.selfvariable
1302 self.its_variable
= variable
1303 self.mtype
= v
.get_variable
(self, variable
)
1307 ## MESSAGE SENDING AND PROPERTY
1309 redef class ASendExpr
1310 # The property invoked by the send.
1311 var callsite
: nullable CallSite
1313 redef fun accept_typing
(v
)
1315 var recvtype
= v
.visit_expr
(self.n_expr
)
1316 var name
= self.property_name
1318 if recvtype
== null then return # Forward error
1319 if recvtype
isa MNullType then
1320 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1324 var callsite
= v
.get_method
(self, recvtype
, name
, self.n_expr
isa ASelfExpr)
1325 if callsite
== null then return
1326 self.callsite
= callsite
1327 var msignature
= callsite
.msignature
1329 var args
= compute_raw_arguments
1331 callsite
.check_signature
(v
, args
)
1333 if callsite
.mproperty
.is_init
then
1334 var vmpropdef
= v
.mpropdef
1335 if not (vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_init
) then
1336 v
.error
(self, "Can call a init only in another init")
1338 if vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_root_init
and not callsite
.mproperty
.is_root_init
then
1339 v
.error
(self, "Error: {vmpropdef} cannot call a factory {callsite.mproperty}")
1343 var ret
= msignature
.return_mtype
1347 self.is_typed
= true
1351 # The name of the property
1352 # Each subclass simply provide the correct name.
1353 private fun property_name
: String is abstract
1355 # An array of all arguments (excluding self)
1356 fun raw_arguments
: Array[AExpr] do return compute_raw_arguments
1358 private fun compute_raw_arguments
: Array[AExpr] is abstract
1361 redef class ABinopExpr
1362 redef fun compute_raw_arguments
do return [n_expr2
]
1365 redef fun property_name
do return "=="
1366 redef fun accept_typing
(v
)
1373 redef fun property_name
do return "!="
1374 redef fun accept_typing
(v
)
1381 redef fun property_name
do return "<"
1384 redef fun property_name
do return "<="
1387 redef fun property_name
do return "<<"
1390 redef fun property_name
do return ">"
1393 redef fun property_name
do return ">="
1396 redef fun property_name
do return ">>"
1398 redef class APlusExpr
1399 redef fun property_name
do return "+"
1401 redef class AMinusExpr
1402 redef fun property_name
do return "-"
1404 redef class AStarshipExpr
1405 redef fun property_name
do return "<=>"
1407 redef class AStarExpr
1408 redef fun property_name
do return "*"
1410 redef class AStarstarExpr
1411 redef fun property_name
do return "**"
1413 redef class ASlashExpr
1414 redef fun property_name
do return "/"
1416 redef class APercentExpr
1417 redef fun property_name
do return "%"
1420 redef class AUminusExpr
1421 redef fun property_name
do return "unary -"
1422 redef fun compute_raw_arguments
do return new Array[AExpr]
1426 redef class ACallExpr
1427 redef fun property_name
do return n_id
.text
1428 redef fun compute_raw_arguments
do return n_args
.to_a
1431 redef class ACallAssignExpr
1432 redef fun property_name
do return n_id
.text
+ "="
1433 redef fun compute_raw_arguments
1435 var res
= n_args
.to_a
1441 redef class ABraExpr
1442 redef fun property_name
do return "[]"
1443 redef fun compute_raw_arguments
do return n_args
.to_a
1446 redef class ABraAssignExpr
1447 redef fun property_name
do return "[]="
1448 redef fun compute_raw_arguments
1450 var res
= n_args
.to_a
1456 redef class ASendReassignFormExpr
1457 # The property invoked for the writing
1458 var write_callsite
: nullable CallSite
1460 redef fun accept_typing
(v
)
1462 var recvtype
= v
.visit_expr
(self.n_expr
)
1463 var name
= self.property_name
1465 if recvtype
== null then return # Forward error
1466 if recvtype
isa MNullType then
1467 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1471 var for_self
= self.n_expr
isa ASelfExpr
1472 var callsite
= v
.get_method
(self, recvtype
, name
, for_self
)
1474 if callsite
== null then return
1475 self.callsite
= callsite
1477 var args
= compute_raw_arguments
1479 callsite
.check_signature
(v
, args
)
1481 var readtype
= callsite
.msignature
.return_mtype
1482 if readtype
== null then
1483 v
.error
(self, "Error: {name} is not a function")
1487 var wcallsite
= v
.get_method
(self, recvtype
, name
+ "=", self.n_expr
isa ASelfExpr)
1488 if wcallsite
== null then return
1489 self.write_callsite
= wcallsite
1491 var wtype
= self.resolve_reassignment
(v
, readtype
, wcallsite
.msignature
.mparameters
.last
.mtype
)
1492 if wtype
== null then return
1494 args
= args
.to_a
# duplicate so raw_arguments keeps only the getter args
1495 args
.add
(self.n_value
)
1496 wcallsite
.check_signature
(v
, args
)
1498 self.is_typed
= true
1502 redef class ACallReassignExpr
1503 redef fun property_name
do return n_id
.text
1504 redef fun compute_raw_arguments
do return n_args
.to_a
1507 redef class ABraReassignExpr
1508 redef fun property_name
do return "[]"
1509 redef fun compute_raw_arguments
do return n_args
.to_a
1512 redef class AInitExpr
1513 redef fun property_name
do return "init"
1514 redef fun compute_raw_arguments
do return n_args
.to_a
1518 fun to_a
: Array[AExpr] do return self.n_exprs
.to_a
1523 redef class ASuperExpr
1524 # The method to call if the super is in fact a 'super init call'
1525 # Note: if the super is a normal call-next-method, then this attribute is null
1526 var callsite
: nullable CallSite
1528 # The method to call is the super is a standard `call-next-method` super-call
1529 # Note: if the super is a special super-init-call, then this attribute is null
1530 var mpropdef
: nullable MMethodDef
1532 redef fun accept_typing
(v
)
1534 var anchor
= v
.anchor
1535 assert anchor
!= null
1536 var recvtype
= v
.get_variable
(self, v
.selfvariable
)
1537 assert recvtype
!= null
1538 var mproperty
= v
.mpropdef
.mproperty
1539 if not mproperty
isa MMethod then
1540 v
.error
(self, "Error: super only usable in a method")
1543 var superprops
= mproperty
.lookup_super_definitions
(v
.mmodule
, anchor
)
1544 if superprops
.length
== 0 then
1545 if mproperty
.is_init
and v
.mpropdef
.is_intro
then
1546 process_superinit
(v
)
1549 v
.error
(self, "Error: No super method to call for {mproperty}.")
1552 # FIXME: covariance of return type in linear extension?
1553 var superprop
= superprops
.first
1555 var msignature
= superprop
.msignature
.as(not null)
1556 msignature
= v
.resolve_for
(msignature
, recvtype
, true).as(MSignature)
1557 var args
= self.n_args
.to_a
1558 if args
.length
> 0 then
1559 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1561 self.mtype
= msignature
.return_mtype
1562 self.is_typed
= true
1563 v
.mpropdef
.has_supercall
= true
1564 mpropdef
= v
.mpropdef
.as(MMethodDef)
1567 private fun process_superinit
(v
: TypeVisitor)
1569 var anchor
= v
.anchor
1570 assert anchor
!= null
1571 var recvtype
= v
.get_variable
(self, v
.selfvariable
)
1572 assert recvtype
!= null
1573 var mpropdef
= v
.mpropdef
1574 assert mpropdef
isa MMethodDef
1575 var mproperty
= mpropdef
.mproperty
1576 var superprop
: nullable MMethodDef = null
1577 for msupertype
in mpropdef
.mclassdef
.supertypes
do
1578 msupertype
= msupertype
.anchor_to
(v
.mmodule
, anchor
)
1579 var errcount
= v
.modelbuilder
.toolcontext
.error_count
1580 var candidate
= v
.try_get_mproperty_by_name2
(self, msupertype
, mproperty
.name
).as(nullable MMethod)
1581 if candidate
== null then
1582 if v
.modelbuilder
.toolcontext
.error_count
> errcount
then return # Forward error
1583 continue # Try next super-class
1585 if superprop
!= null and candidate
.is_root_init
then
1588 if superprop
!= null and superprop
.mproperty
!= candidate
and not superprop
.mproperty
.is_root_init
then
1589 v
.error
(self, "Error: conflicting super constructor to call for {mproperty}: {candidate.full_name}, {superprop.mproperty.full_name}")
1592 var candidatedefs
= candidate
.lookup_definitions
(v
.mmodule
, anchor
)
1593 if superprop
!= null and superprop
.mproperty
== candidate
then
1594 if superprop
== candidatedefs
.first
then continue
1595 candidatedefs
.add
(superprop
)
1597 if candidatedefs
.length
> 1 then
1598 v
.error
(self, "Error: conflicting property definitions for property {mproperty} in {recvtype}: {candidatedefs.join(", ")}")
1601 superprop
= candidatedefs
.first
1603 if superprop
== null then
1604 v
.error
(self, "Error: No super method to call for {mproperty}.")
1608 var msignature
= superprop
.new_msignature
or else superprop
.msignature
.as(not null)
1609 msignature
= v
.resolve_for
(msignature
, recvtype
, true).as(MSignature)
1611 var callsite
= new CallSite(self, recvtype
, v
.mmodule
, v
.anchor
, true, superprop
.mproperty
, superprop
, msignature
, false)
1612 self.callsite
= callsite
1614 var args
= self.n_args
.to_a
1615 if args
.length
> 0 then
1616 callsite
.check_signature
(v
, args
)
1618 # Check there is at least enough parameters
1619 if mpropdef
.msignature
.arity
< msignature
.arity
then
1620 v
.error
(self, "Error: Not enough implicit arguments to pass. Got {mpropdef.msignature.arity}, expected at least {msignature.arity}. Signature is {msignature}")
1623 # Check that each needed parameter is conform
1625 for sp
in msignature
.mparameters
do
1626 var p
= mpropdef
.msignature
.mparameters
[i
]
1627 if not v
.is_subtype
(p
.mtype
, sp
.mtype
) then
1628 v
.error
(self, "Type error: expected argument #{i} of type {sp.mtype}, got implicit argument {p.name} of type {p.mtype}. Signature is {msignature}")
1635 self.is_typed
= true
1641 redef class ANewExpr
1642 # The constructor invoked by the new.
1643 var callsite
: nullable CallSite
1645 redef fun accept_typing
(v
)
1647 var recvtype
= v
.resolve_mtype
(self.n_type
)
1648 if recvtype
== null then return
1649 self.mtype
= recvtype
1651 if not recvtype
isa MClassType then
1652 if recvtype
isa MNullableType then
1653 v
.error
(self, "Type error: cannot instantiate the nullable type {recvtype}.")
1656 v
.error
(self, "Type error: cannot instantiate the formal type {recvtype}.")
1660 if recvtype
.mclass
.kind
== abstract_kind
then
1661 v
.error
(self, "Cannot instantiate abstract class {recvtype}.")
1663 else if recvtype
.mclass
.kind
== interface_kind
then
1664 v
.error
(self, "Cannot instantiate interface {recvtype}.")
1676 var callsite
= v
.get_method
(self, recvtype
, name
, false)
1677 if callsite
== null then return
1679 self.callsite
= callsite
1681 if not callsite
.mproperty
.is_init_for
(recvtype
.mclass
) then
1682 v
.error
(self, "Error: {name} is not a constructor.")
1686 var args
= n_args
.to_a
1687 callsite
.check_signature
(v
, args
)
1693 redef class AAttrFormExpr
1694 # The attribute acceded.
1695 var mproperty
: nullable MAttribute
1697 # The static type of the attribute.
1698 var attr_type
: nullable MType
1700 # Resolve the attribute acceded.
1701 private fun resolve_property
(v
: TypeVisitor)
1703 var recvtype
= v
.visit_expr
(self.n_expr
)
1704 if recvtype
== null then return # Skip error
1705 var name
= self.n_id
.text
1706 if recvtype
isa MNullType then
1707 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1711 var unsafe_type
= v
.anchor_to
(recvtype
)
1712 var mproperty
= v
.try_get_mproperty_by_name2
(self, unsafe_type
, name
)
1713 if mproperty
== null then
1714 v
.modelbuilder
.error
(self, "Error: Attribute {name} doesn't exists in {recvtype}.")
1717 assert mproperty
isa MAttribute
1718 self.mproperty
= mproperty
1720 var mpropdefs
= mproperty
.lookup_definitions
(v
.mmodule
, unsafe_type
)
1721 assert mpropdefs
.length
== 1
1722 var mpropdef
= mpropdefs
.first
1723 var attr_type
= mpropdef
.static_mtype
.as(not null)
1724 attr_type
= v
.resolve_for
(attr_type
, recvtype
, self.n_expr
isa ASelfExpr)
1725 self.attr_type
= attr_type
1729 redef class AAttrExpr
1730 redef fun accept_typing
(v
)
1732 self.resolve_property
(v
)
1733 self.mtype
= self.attr_type
1738 redef class AAttrAssignExpr
1739 redef fun accept_typing
(v
)
1741 self.resolve_property
(v
)
1742 var mtype
= self.attr_type
1744 v
.visit_expr_subtype
(self.n_value
, mtype
)
1745 self.is_typed
= true
1749 redef class AAttrReassignExpr
1750 redef fun accept_typing
(v
)
1752 self.resolve_property
(v
)
1753 var mtype
= self.attr_type
1754 if mtype
== null then return # Skip error
1756 self.resolve_reassignment
(v
, mtype
, mtype
)
1758 self.is_typed
= true
1762 redef class AIssetAttrExpr
1763 redef fun accept_typing
(v
)
1765 self.resolve_property
(v
)
1766 var mtype
= self.attr_type
1767 if mtype
== null then return # Skip error
1769 var recvtype
= self.n_expr
.mtype
.as(not null)
1770 var bound
= v
.resolve_for
(mtype
, recvtype
, false)
1771 if bound
isa MNullableType then
1772 v
.error
(self, "Error: isset on a nullable attribute.")
1774 self.mtype
= v
.type_bool
(self)
1780 redef class ADebugTypeExpr
1781 redef fun accept_typing
(v
)
1783 var expr
= v
.visit_expr
(self.n_expr
)
1784 if expr
== null then return
1785 var unsafe
= v
.anchor_to
(expr
)
1786 var ntype
= self.n_type
1787 var mtype
= v
.resolve_mtype
(ntype
)
1788 if mtype
!= null and mtype
!= expr
then
1789 var umtype
= v
.anchor_to
(mtype
)
1790 v
.modelbuilder
.warning
(self, "debug", "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")
1792 self.is_typed
= true