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.
25 redef class ToolContext
26 var typing_phase
: Phase = new TypingPhase(self, [flow_phase
, modelize_property_phase
, local_var_init_phase
])
29 private class TypingPhase
31 redef fun process_npropdef
(npropdef
) do npropdef
.do_typing
(toolcontext
.modelbuilder
)
34 private class TypeVisitor
35 var modelbuilder
: ModelBuilder
37 # The module of the analysis
38 # Used to correctly query the model
41 # The static type of the receiver
42 # Mainly used for type tests and type resolutions
43 var anchor
: nullable MClassType = null
45 # The analyzed mclassdef
46 var mclassdef
: nullable MClassDef = null
48 # The analyzed property
49 var mpropdef
: nullable MPropDef
51 var selfvariable
= new Variable("self")
53 # Is `self` use restricted?
54 # * no explicit `self`
55 # * method called on the implicit self must be top-level
56 # Currently only used for `new` factory since there is no valid receiver inside
57 var is_toplevel_context
= false
61 var mpropdef
= self.mpropdef
63 if mpropdef
!= null then
64 self.mpropdef
= mpropdef
65 var mclassdef
= mpropdef
.mclassdef
66 self.mclassdef
= mclassdef
67 self.anchor
= mclassdef
.bound_mtype
69 var mclass
= mclassdef
.mclass
71 var selfvariable
= new Variable("self")
72 self.selfvariable
= selfvariable
73 selfvariable
.declared_type
= mclass
.mclass_type
75 var mprop
= mpropdef
.mproperty
76 if mprop
isa MMethod and mprop
.is_new
then
77 is_toplevel_context
= true
82 fun anchor_to
(mtype
: MType): MType
84 return mtype
.anchor_to
(mmodule
, anchor
)
87 fun is_subtype
(sub
, sup
: MType): Bool
89 return sub
.is_subtype
(mmodule
, anchor
, sup
)
92 fun resolve_for
(mtype
, subtype
: MType, for_self
: Bool): MType
94 #print "resolve_for {mtype} sub={subtype} forself={for_self} mmodule={mmodule} anchor={anchor}"
95 var res
= mtype
.resolve_for
(subtype
, anchor
, mmodule
, not for_self
)
99 # Check that `sub` is a subtype of `sup`.
100 # If `sub` is not a valid suptype, then display an error on `node` and return `null`.
101 # If `sub` is a safe subtype of `sup`, then return `sub`.
102 # If `sub` is an unsafe subtype (i.e., an implicit cast is required), then return `sup`.
104 # The point of the return type is to determinate the usable type on an expression when `autocast` is true:
105 # If the suptype is safe, then the return type is the one on the expression typed by `sub`.
106 # Is the subtype is unsafe, then the return type is the one of an implicit cast on `sup`.
107 fun check_subtype
(node
: ANode, sub
, sup
: MType, autocast
: Bool): nullable MType
109 if self.is_subtype
(sub
, sup
) then return sub
110 if autocast
and self.is_subtype
(sub
, self.anchor_to
(sup
)) then
111 # FIXME workaround to the current unsafe typing policy. To remove once fixed virtual types exists.
112 #node.debug("Unsafe typing: expected {sup}, got {sub}")
115 if sup
isa MErrorType then return null # Skip error
116 if sub
.need_anchor
then
117 var u
= anchor_to
(sub
)
118 self.modelbuilder
.error
(node
, "Type Error: expected `{sup}`, got `{sub}: {u}`.")
120 self.modelbuilder
.error
(node
, "Type Error: expected `{sup}`, got `{sub}`.")
125 # Visit an expression and do not care about the return value
126 fun visit_stmt
(nexpr
: nullable AExpr)
128 if nexpr
== null then return
129 nexpr
.accept_typing
(self)
132 # Visit an expression and expects that it is not a statement
133 # Return the type of the expression
134 # Display an error and return null if:
135 # * the type cannot be determined or
136 # * `nexpr` is a statement
137 fun visit_expr
(nexpr
: AExpr): nullable MType
139 nexpr
.accept_typing
(self)
140 var mtype
= nexpr
.mtype
141 if mtype
!= null then return mtype
142 if not nexpr
.is_typed
then
143 if not self.modelbuilder
.toolcontext
.error_count
> 0 then # check that there is really an error
144 if self.modelbuilder
.toolcontext
.verbose_level
> 1 then
145 nexpr
.debug
("No return type but no error.")
148 return null # forward error
150 var more_message
= null
152 if p
!= null then more_message
= p
.bad_expr_message
(nexpr
)
153 if more_message
== null then more_message
= "" else more_message
= " " + more_message
154 self.error
(nexpr
, "Error: expected an expression{more_message}.")
158 # Visit an expression and expect its static type is a least a `sup`
159 # Return the type of the expression or null if
160 # * the type cannot be determined or
161 # * `nexpr` is a statement or
162 # * `nexpr` is not a `sup`
163 fun visit_expr_subtype
(nexpr
: AExpr, sup
: nullable MType): nullable MType
165 var sub
= visit_expr
(nexpr
)
166 if sub
== null then return null # Forward error
168 if sup
== null then return null # Forward error
170 var res
= check_subtype
(nexpr
, sub
, sup
, true)
172 nexpr
.implicit_cast_to
= res
177 # Visit an expression and expect its static type is a `Bool`
178 # Return the type of the expression or null if
179 # * the type cannot be determined or
180 # * `nexpr` is a statement or
181 # * `nexpr` is not a `Bool`
182 fun visit_expr_bool
(nexpr
: AExpr): nullable MType
184 return self.visit_expr_subtype
(nexpr
, self.type_bool
(nexpr
))
188 fun check_expr_cast
(node
: ANode, nexpr
: AExpr, ntype
: AType): nullable MType
190 var sub
= nexpr
.mtype
191 if sub
== null then return null # Forward error
193 var sup
= ntype
.mtype
194 if sup
== null then return null # Forward error
197 self.modelbuilder
.warning
(node
, "useless-type-test", "Warning: expression is already a `{sup}`.")
198 else if self.is_subtype
(sub
, sup
) then
199 self.modelbuilder
.warning
(node
, "useless-type-test", "Warning: expression is already a `{sup}` since it is a `{sub}`.")
204 # Can `mtype` be null (up to the current knowledge)?
205 fun can_be_null
(mtype
: MType): Bool
207 if mtype
isa MNullableType or mtype
isa MNullType then return true
208 if mtype
isa MFormalType then
209 var x
= anchor_to
(mtype
)
210 if x
isa MNullableType or x
isa MNullType then return true
215 # Check that `mtype` can be null (up to the current knowledge).
217 # If not then display a `useless-null-test` warning on node and return false.
219 fun check_can_be_null
(anode
: ANode, mtype
: MType): Bool
221 if mtype
isa MNullType then
222 modelbuilder
.warning
(anode
, "useless-null-test", "Warning: expression is always `null`.")
225 if can_be_null
(mtype
) then return true
227 if mtype
isa MFormalType then
228 var res
= anchor_to
(mtype
)
229 modelbuilder
.warning
(anode
, "useless-null-test", "Warning: expression is not null, since it is a `{mtype}: {res}`.")
231 modelbuilder
.warning
(anode
, "useless-null-test", "Warning: expression is not null, since it is a `{mtype}`.")
236 # Special verification on != and == for null
238 fun null_test
(anode
: ABinopExpr)
240 var mtype
= anode
.n_expr
.mtype
241 var mtype2
= anode
.n_expr2
.mtype
243 if mtype
== null or mtype2
== null then return
245 if not mtype2
isa MNullType then return
247 # Check of useless null
248 if not can_be_null
(mtype
) then return
250 if mtype
isa MNullType then
251 # Because of type adaptation, we cannot just stop here
252 # so return use `null` as a bottom type that will be merged easily (cf) `merge_types`
255 mtype
= mtype
.as_notnull
258 # Check for type adaptation
259 var variable
= anode
.n_expr
.its_variable
260 if variable
== null then return
262 # One is null (mtype2 see above) the other is not null
263 if anode
isa AEqExpr then
264 anode
.after_flow_context
.when_true
.set_var
(self, variable
, mtype2
)
265 anode
.after_flow_context
.when_false
.set_var
(self, variable
, mtype
)
266 else if anode
isa ANeExpr then
267 anode
.after_flow_context
.when_false
.set_var
(self, variable
, mtype2
)
268 anode
.after_flow_context
.when_true
.set_var
(self, variable
, mtype
)
274 fun try_get_mproperty_by_name2
(anode
: ANode, mtype
: MType, name
: String): nullable MProperty
276 return self.modelbuilder
.try_get_mproperty_by_name2
(anode
, mmodule
, mtype
, name
)
279 fun resolve_mtype
(node
: AType): nullable MType
281 return self.modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, node
)
284 fun try_get_mclass
(node
: ANode, name
: String): nullable MClass
286 var mclass
= modelbuilder
.try_get_mclass_by_name
(node
, mmodule
, name
)
290 fun get_mclass
(node
: ANode, name
: String): nullable MClass
292 var mclass
= modelbuilder
.get_mclass_by_name
(node
, mmodule
, name
)
296 fun type_bool
(node
: ANode): nullable MType
298 var mclass
= self.get_mclass
(node
, "Bool")
299 if mclass
== null then return null
300 return mclass
.mclass_type
303 fun get_method
(node
: ANode, recvtype
: MType, name
: String, recv_is_self
: Bool): nullable CallSite
305 var unsafe_type
= self.anchor_to
(recvtype
)
307 #debug("recv: {recvtype} (aka {unsafe_type})")
308 if recvtype
isa MNullType then
309 var objclass
= get_mclass
(node
, "Object")
310 if objclass
== null then return null # Forward error
311 unsafe_type
= objclass
.mclass_type
314 var mproperty
= self.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
315 if name
== "new" and mproperty
== null then
317 mproperty
= self.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
320 if mproperty
== null then
322 self.modelbuilder
.error
(node
, "Error: method or variable `{name}` unknown in `{recvtype}`.")
323 else if recvtype
.need_anchor
then
324 self.modelbuilder
.error
(node
, "Error: method `{name}` does not exists in `{recvtype}: {unsafe_type}`.")
326 self.modelbuilder
.error
(node
, "Error: method `{name}` does not exists in `{recvtype}`.")
331 assert mproperty
isa MMethod
333 # `null` only accepts some methods of object.
334 if recvtype
isa MNullType and not mproperty
.is_null_safe
then
335 self.error
(node
, "Error: method `{name}` called on `null`.")
337 else if unsafe_type
isa MNullableType and not mproperty
.is_null_safe
then
338 modelbuilder
.advice
(node
, "call-on-nullable", "Warning: method call on a nullable receiver `{recvtype}`.")
341 if is_toplevel_context
and recv_is_self
and not mproperty
.is_toplevel
then
342 error
(node
, "Error: `{name}` is not a top-level method, thus need a receiver.")
344 if not recv_is_self
and mproperty
.is_toplevel
then
345 error
(node
, "Error: cannot call `{name}`, a top-level method, with a receiver.")
348 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
349 self.modelbuilder
.error
(node
, "Error: method `{name}` is protected and can only accessed by `self`.")
353 var info
= mproperty
.deprecation
354 if info
!= null and self.mpropdef
.mproperty
.deprecation
== null then
357 self.modelbuilder
.warning
(node
, "deprecated-method", "Deprecation Warning: method `{name}` is deprecated: {mdoc.content.first}")
359 self.modelbuilder
.warning
(node
, "deprecated-method", "Deprecation Warning: method `{name}` is deprecated.")
363 var propdefs
= mproperty
.lookup_definitions
(self.mmodule
, unsafe_type
)
365 if propdefs
.length
== 0 then
366 self.modelbuilder
.error
(node
, "Type Error: no definition found for property `{name}` in `{unsafe_type}`.")
368 else if propdefs
.length
== 1 then
369 mpropdef
= propdefs
.first
371 self.modelbuilder
.warning
(node
, "property-conflict", "Warning: conflicting property definitions for property `{name}` in `{unsafe_type}`: {propdefs.join(" ")}")
372 mpropdef
= mproperty
.intro
376 var msignature
= mpropdef
.new_msignature
or else mpropdef
.msignature
377 if msignature
== null then return null # skip error
378 msignature
= resolve_for
(msignature
, recvtype
, recv_is_self
).as(MSignature)
380 var erasure_cast
= false
381 var rettype
= mpropdef
.msignature
.return_mtype
382 if not recv_is_self
and rettype
!= null then
383 rettype
= rettype
.undecorate
384 if rettype
isa MParameterType then
385 var erased_rettype
= msignature
.return_mtype
386 assert erased_rettype
!= null
387 #node.debug("Erasure cast: Really a {rettype} but unsafely a {erased_rettype}")
392 var callsite
= new CallSite(node
.hot_location
, recvtype
, mmodule
, anchor
, recv_is_self
, mproperty
, mpropdef
, msignature
, erasure_cast
)
396 fun try_get_method
(node
: ANode, recvtype
: MType, name
: String, recv_is_self
: Bool): nullable CallSite
398 var unsafe_type
= self.anchor_to
(recvtype
)
399 var mproperty
= self.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
400 if mproperty
== null then return null
401 return get_method
(node
, recvtype
, name
, recv_is_self
)
405 # Visit the expressions of args and check their conformity with the corresponding type in signature
406 # The point of this method is to handle varargs correctly
407 # Note: The signature must be correctly adapted
408 fun check_signature
(node
: ANode, args
: Array[AExpr], mproperty
: MProperty, msignature
: MSignature): nullable SignatureMap
410 var vararg_rank
= msignature
.vararg_rank
411 if vararg_rank
>= 0 then
412 if args
.length
< msignature
.arity
then
413 modelbuilder
.error
(node
, "Error: expected at least {msignature.arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
416 else if args
.length
!= msignature
.arity
then
418 if args
.length
> msignature
.arity
then
419 modelbuilder
.error
(node
, "Error: expected {msignature.arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
422 # Other cases are managed later
426 #debug("CALL {unsafe_type}.{msignature}")
428 # Associate each parameter to a position in the arguments
429 var map
= new SignatureMap
431 # Special case for the isolated last argument
432 # TODO: reify this method characteristics (where? the param, the signature, the method?)
433 var last_is_padded
= mproperty
.name
.chars
.last
== '='
434 var nbargs
= args
.length
435 if last_is_padded
then
437 assert not args
.last
isa ANamedargExpr
438 map
.map
[msignature
.arity
- 1] = args
.length
- 1
439 self.visit_expr_subtype
(args
.last
, msignature
.mparameters
.last
.mtype
)
442 # First, handle named arguments
443 for i
in [0..args
.length
[ do
445 if not e
isa ANamedargExpr then continue
446 var name
= e
.n_id
.text
447 var param
= msignature
.mparameter_by_name
(name
)
448 if param
== null then
449 modelbuilder
.error
(e
.n_id
, "Error: no parameter `{name}` for `{mproperty}{msignature}`.")
452 var idx
= msignature
.mparameters
.index_of
(param
)
453 var prev
= map
.map
.get_or_null
(idx
)
455 modelbuilder
.error
(e
, "Error: parameter `{name}` already associated with argument #{prev} for `{mproperty}{msignature}`.")
459 e
.mtype
= self.visit_expr_subtype
(e
.n_expr
, param
.mtype
)
462 # Number of minimum mandatory remaining parameters
465 # Second, associate remaining parameters
466 var vararg_decl
= args
.length
- msignature
.arity
468 for i
in [0..msignature
.arity
[ do
469 # Skip parameters associated by name
470 if map
.map
.has_key
(i
) then continue
472 var param
= msignature
.mparameters
[i
]
474 # Search the next free argument: skip named arguments since they are already associated
475 while j
< nbargs
and args
[j
] isa ANamedargExpr do j
+= 1
477 if not param
.mtype
isa MNullableType then
487 if i
== vararg_rank
then
489 continue # skip the vararg
492 if not param
.is_vararg
then
493 var paramtype
= param
.mtype
494 self.visit_expr_subtype
(arg
, paramtype
)
496 check_one_vararg
(arg
, param
)
500 if min_arity
> 0 then
501 if last_is_padded
then min_arity
+= 1
502 if min_arity
< msignature
.arity
then
503 modelbuilder
.error
(node
, "Error: expected at least {min_arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
505 modelbuilder
.error
(node
, "Error: expected {min_arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
510 # Third, check varargs
511 if vararg_rank
>= 0 then
512 var paramtype
= msignature
.mparameters
[vararg_rank
].mtype
513 var first
= args
[vararg_rank
]
514 if vararg_decl
== 0 then
515 if not check_one_vararg
(first
, msignature
.mparameters
[vararg_rank
]) then return null
517 first
.vararg_decl
= vararg_decl
+ 1
518 for i
in [vararg_rank
..vararg_rank
+vararg_decl
] do
519 self.visit_expr_subtype
(args
[i
], paramtype
)
527 # Check an expression as a single vararg.
528 # The main point of the method if to handle the case of reversed vararg (see `AVarargExpr`)
529 fun check_one_vararg
(arg
: AExpr, param
: MParameter): Bool
531 var paramtype
= param
.mtype
532 var mclass
= get_mclass
(arg
, "Array")
533 if mclass
== null then return false # Forward error
534 var array_mtype
= mclass
.get_mtype
([paramtype
])
535 if arg
isa AVarargExpr then
536 self.visit_expr_subtype
(arg
.n_expr
, array_mtype
)
537 arg
.mtype
= arg
.n_expr
.mtype
539 # only one vararg, maybe `...` was forgot, so be gentle!
540 var t
= visit_expr
(arg
)
541 if t
== null then return false # Forward error
542 if not is_subtype
(t
, paramtype
) and is_subtype
(t
, array_mtype
) then
543 # Not acceptable but could be a `...`
544 error
(arg
, "Type Error: expected `{paramtype}`, got `{t}`. Is an ellipsis `...` missing on the argument?")
547 # Standard valid vararg, finish the job
549 self.visit_expr_subtype
(arg
, paramtype
)
554 fun error
(node
: ANode, message
: String)
556 self.modelbuilder
.error
(node
, message
)
559 fun get_variable
(node
: AExpr, variable
: Variable): nullable MType
561 if not variable
.is_adapted
then return variable
.declared_type
563 var flow
= node
.after_flow_context
564 if flow
== null then return null # skip error
566 if flow
.vars
.has_key
(variable
) then
567 return flow
.vars
[variable
]
569 #node.debug("*** START Collected for {variable}")
570 var mtypes
= flow
.collect_types
(variable
)
571 #node.debug("**** END Collected for {variable}")
572 if mtypes
.length
== 0 then
573 return variable
.declared_type
574 else if mtypes
.length
== 1 then
577 var res
= merge_types
(node
,mtypes
)
579 res
= variable
.declared_type
580 # Try to fallback to a non-null version
581 if res
!= null and can_be_null
(res
) then do
583 if t
!= null and can_be_null
(t
) then break label
593 # Some variables where type-adapted during the visit
596 # Some loops had been visited during the visit
599 fun set_variable
(node
: AExpr, variable
: Variable, mtype
: nullable MType)
601 var flow
= node
.after_flow_context
604 flow
.set_var
(self, variable
, mtype
)
607 # Find the exact representable most specific common super-type in `col`.
609 # Try to find the most specific common type that is a super-type of each types
611 # In most cases, the result is simply the most general type in `col`.
612 # If nullables types are involved, then the nullable information is correctly preserved.
613 # If incomparable super-types exists in `col`, them no solution is given and the `null`
614 # value is returned (since union types are non representable in Nit)
616 # The `null` values in `col` are ignored, nulltypes (MNullType) are considered.
618 # Returns the `null` value if:
621 # * `col` only have null values
622 # * there is a conflict
624 # Example (with a diamond A,B,C,D):
626 # * merge(A,B,C) -> A, because A is the most general type in {A,B,C}
627 # * merge(C,B) -> null, there is conflict, because `B or C` cannot be represented
628 # * merge(A,nullable B) -> nullable A, because A is the most general type and
629 # the nullable information is preserved
630 fun merge_types
(node
: ANode, col
: Array[nullable MType]): nullable MType
632 if col
.length
== 1 then return col
.first
634 if t1
== null then continue # return null
637 if t2
== null then continue # return null
638 if can_be_null
(t2
) and not can_be_null
(t1
) then
641 if not is_subtype
(t2
, t1
) then found
= false
644 #print "merge {col.join(" ")} -> {t1}"
648 #self.modelbuilder.warning(node, "Type Error: {col.length} conflicting types: <{col.join(", ")}>")
652 # Find a most general common subtype between `type1` and `type2`.
654 # Find the most general type that is a subtype of `type2` and, if possible, a subtype of `type1`.
655 # Basically, this return the most specific type between `type1` and `type2`.
656 # If nullable types are involved, the information is correctly preserved.
657 # If `type1` and `type2` are incomparable then `type2` is preferred (since intersection types
658 # are not representable in Nit).
660 # The `null` value is returned if both `type1` and `type2` are null.
662 # Examples (with diamond A,B,C,D):
664 # * intersect_types(A,B) -> B, because B is a subtype of A
665 # * intersect_types(B,A) -> B, because B is a subtype of A
666 # * intersect_types(B,C) -> C, B and C are incomparable,
667 # `type2` is then preferred (`B and C` cannot be represented)
668 # * intersect_types(nullable B,A) -> B, because B<:A and the non-null information is preserved
669 # * intersect_types(B,nullable C) -> C, `type2` is preferred and the non-null information is preserved
670 fun intersect_types
(node
: ANode, type1
, type2
: nullable MType): nullable MType
672 if type1
== null then return type2
673 if type2
== null then return type1
675 if not can_be_null
(type2
) or not can_be_null
(type1
) then
676 type1
= type1
.as_notnull
677 type2
= type2
.as_notnull
681 if is_subtype
(type1
, type2
) then
689 # Find a most general type that is a subtype of `type1` but not one of `type2`.
691 # Basically, this returns `type1`-`type2` but since there is no substraction type
692 # in Nit this just returns `type1` most of the case.
694 # The few other cases are if `type2` is a super-type and if some nullable information
697 # The `null` value is returned if `type1` is null.
699 # Examples (with diamond A,B,C,D):
701 # * diff_types(A,B) -> A, because the notB cannot be represented
702 # * diff_types(B,A) -> None (absurd type), because B<:A
703 # * diff_types(nullable A, nullable B) -> A, because null is removed
704 # * diff_types(nullable B, A) -> Null, because anything but null is removed
705 fun diff_types
(node
: ANode, type1
, type2
: nullable MType): nullable MType
707 if type1
== null then return null
708 if type2
== null then return type1
710 # if t1 <: t2 then t1-t2 = bottom
711 if is_subtype
(type1
, type2
) then
712 return modelbuilder
.model
.null_type
.as_notnull
715 # else if t1 <: nullable t2 then t1-t2 = nulltype
716 if is_subtype
(type1
, type2
.as_nullable
) then
717 return modelbuilder
.model
.null_type
720 # else t2 can be null and type2 must accept null then null is excluded in t1
721 if can_be_null
(type1
) and (type2
isa MNullableType or type2
isa MNullType) then
722 return type1
.as_notnull
729 # Mapping between parameters and arguments in a call.
731 # Parameters and arguments are not stored in the class but referenced by their position (starting from 0)
733 # The point of this class is to help engine and other things to map arguments in the AST to parameters of the model.
735 # Associate a parameter to an argument
736 var map
= new ArrayMap[Int, Int]
739 # A specific method call site with its associated informations.
745 # The static type of the receiver (possibly unresolved)
748 # The module where the callsite is present
751 # The anchor to use with `recv` or `msignature`
752 var anchor
: nullable MClassType
754 # Is the receiver self?
755 # If "for_self", virtual types of the signature are kept
756 # If "not_for_self", virtual type are erased
757 var recv_is_self
: Bool
759 # The designated method
760 var mproperty
: MMethod
762 # The statically designated method definition
763 # The most specif one, it is.
764 var mpropdef
: MMethodDef
766 # The resolved signature for the receiver
767 var msignature
: MSignature
769 # Is a implicit cast required on erasure typing policy?
770 var erasure_cast
: Bool
772 # The mapping used on the call to associate arguments to parameters
773 # If null then no specific association is required.
774 var signaturemap
: nullable SignatureMap = null
776 private fun check_signature
(v
: TypeVisitor, node
: ANode, args
: Array[AExpr]): Bool
778 var map
= v
.check_signature
(node
, args
, self.mproperty
, self.msignature
)
780 if map
== null then is_broken
= true
784 # Information about the callsite to display on a node
785 fun dump_info
(v
: ASTDump): String do
786 return "{recv}.{mpropdef}{msignature}"
789 redef fun mdoc_or_fallback
do return mproperty
.intro
.mdoc
793 # The declared type of the variable
794 var declared_type
: nullable MType = null is writable
796 # Was the variable type-adapted?
797 # This is used to speedup type retrieval while it remains `false`
798 private var is_adapted
= false
801 redef class FlowContext
802 # Store changes of types because of type evolution
803 private var vars
= new HashMap[Variable, nullable MType]
805 # Adapt the variable to a static type
806 # Warning1: do not modify vars directly.
807 # Warning2: sub-flow may have cached a unadapted variable
808 private fun set_var
(v
: TypeVisitor, variable
: Variable, mtype
: nullable MType)
810 if variable
.declared_type
== mtype
and not variable
.is_adapted
then return
811 if vars
.has_key
(variable
) and vars
[variable
] == mtype
then return
812 self.vars
[variable
] = mtype
814 variable
.is_adapted
= true
815 #node.debug "set {variable} to {mtype or else "X"}"
818 # Look in the flow and previous flow and collect all first reachable type adaptation of a local variable
819 private fun collect_types
(variable
: Variable): Array[nullable MType]
821 #node.debug "flow for {variable}"
822 var res
= new Array[nullable MType]
825 var seen
= new HashSet[FlowContext]
826 while not todo
.is_empty
do
828 if f
.is_unreachable
then continue
829 if seen
.has
(f
) then continue
832 if f
.vars
.has_key
(variable
) then
833 # Found something. Collect it and do not process further on this path
834 res
.add f
.vars
[variable
]
835 #f.node.debug "process {variable}: got {f.vars[variable] or else "X"}"
837 todo
.add_all f
.previous
839 if f
.previous
.is_empty
then
840 # Root flowcontext mean a parameter or something related
841 res
.add variable
.declared_type
842 #f.node.debug "root process {variable}: got {variable.declared_type or else "X"}"
846 #self.node.debug "##### end flow for {variable}: {res.join(" ")}"
852 # The entry point of the whole typing analysis
853 fun do_typing
(modelbuilder
: ModelBuilder)
857 # The variable associated to the receiver (if any)
858 var selfvariable
: nullable Variable
861 redef class AMethPropdef
862 redef fun do_typing
(modelbuilder
: ModelBuilder)
864 var mpropdef
= self.mpropdef
865 if mpropdef
== null then return # skip error
867 var v
= new TypeVisitor(modelbuilder
, mpropdef
.mclassdef
.mmodule
, mpropdef
)
868 self.selfvariable
= v
.selfvariable
870 var mmethoddef
= self.mpropdef
.as(not null)
871 var msignature
= mmethoddef
.msignature
872 if msignature
== null then return # skip error
873 for i
in [0..msignature
.arity
[ do
874 var mtype
= msignature
.mparameters
[i
].mtype
875 if msignature
.vararg_rank
== i
then
876 var arrayclass
= v
.get_mclass
(self.n_signature
.n_params
[i
], "Array")
877 if arrayclass
== null then return # Skip error
878 mtype
= arrayclass
.get_mtype
([mtype
])
880 var variable
= self.n_signature
.n_params
[i
].variable
881 assert variable
!= null
882 variable
.declared_type
= mtype
885 var nblock
= self.n_block
886 if nblock
== null then return
891 if not v
.has_loop
or not v
.dirty
then break
894 var post_visitor
= new PostTypingVisitor(v
)
895 post_visitor
.enter_visit
(self)
897 if not nblock
.after_flow_context
.is_unreachable
and msignature
.return_mtype
!= null then
898 # We reach the end of the function without having a return, it is bad
899 v
.error
(self, "Error: reached end of function; expected `return` with a value.")
904 private class PostTypingVisitor
906 var type_visitor
: TypeVisitor
907 redef fun visit
(n
) do
909 n
.accept_post_typing
(type_visitor
)
910 if n
isa AExpr and n
.mtype
== null and not n
.is_typed
then
917 private fun accept_post_typing
(v
: TypeVisitor) do end
919 # An additional information message to explain the role of a child expression.
921 # The point of the method is to allow some kind of double dispatch so the parent
922 # choose how to describe its children.
923 private fun bad_expr_message
(child
: AExpr): nullable String do return null
926 redef class AAttrPropdef
927 redef fun do_typing
(modelbuilder
: ModelBuilder)
929 if not has_value
then return
931 var mpropdef
= self.mreadpropdef
932 if mpropdef
== null or mpropdef
.msignature
== null then return # skip error
934 var v
= new TypeVisitor(modelbuilder
, mpropdef
.mclassdef
.mmodule
, mpropdef
)
935 self.selfvariable
= v
.selfvariable
937 var nexpr
= self.n_expr
938 if nexpr
!= null then
939 var mtype
= self.mtype
940 v
.visit_expr_subtype
(nexpr
, mtype
)
942 var nblock
= self.n_block
943 if nblock
!= null then
945 if not nblock
.after_flow_context
.is_unreachable
then
946 # We reach the end of the init without having a return, it is bad
947 v
.error
(self, "Error: reached end of block; expected `return`.")
956 # The static type of the expression.
957 # null if self is a statement or in case of error
958 var mtype
: nullable MType = null
960 # Is the statement correctly typed?
961 # Used to distinguish errors and statements when `mtype == null`
962 var is_typed
: Bool = false
964 # If required, the following implicit cast `.as(XXX)`
965 # Such a cast may by required after evaluating the expression when
966 # a unsafe operation is detected (silently accepted by the Nit language).
967 # The attribute is computed by `check_subtype`
968 var implicit_cast_to
: nullable MType = null
970 # Return the variable read (if any)
971 # Used to perform adaptive typing
972 fun its_variable
: nullable Variable do return null
974 private fun accept_typing
(v
: TypeVisitor)
976 v
.error
(self, "no implemented accept_typing for {self.class_name}")
979 # Is non-null if `self` is a leaf of a comprehension array construction.
980 # In this case, the enclosing literal array node is designated.
981 # The result of the evaluation of `self` must be
982 # stored inside the designated array (there is an implicit `push`)
983 var comprehension
: nullable AArrayExpr = null
985 # It indicates the number of arguments collected as a vararg.
987 # When 0, the argument is used as is, without transformation.
988 # When 1, the argument is transformed into an singleton array.
989 # Above 1, the arguments and the next ones are transformed into a common array.
991 # This attribute is meaning less on expressions not used as attributes.
992 var vararg_decl
: Int = 0
994 redef fun dump_info
(v
) do
996 var mtype
= self.mtype
997 if mtype
!= null then
998 res
+= v
.yellow
(":{mtype}")
1000 var ict
= self.implicit_cast_to
1002 res
+= v
.yellow
("(.as({ict}))")
1008 redef class ABlockExpr
1009 redef fun accept_typing
(v
)
1011 for e
in self.n_expr
do v
.visit_stmt
(e
)
1012 self.is_typed
= true
1015 # The type of a blockexpr is the one of the last expression (or null if empty)
1018 if self.n_expr
.is_empty
then return null
1019 return self.n_expr
.last
.mtype
1023 redef class AVardeclExpr
1024 redef fun accept_typing
(v
)
1026 var variable
= self.variable
1027 if variable
== null then return # Skip error
1029 var ntype
= self.n_type
1030 var mtype
: nullable MType
1031 if ntype
== null then
1034 mtype
= v
.resolve_mtype
(ntype
)
1035 if mtype
== null then return # Skip error
1038 var nexpr
= self.n_expr
1039 if nexpr
!= null then
1040 if mtype
!= null then
1041 var etype
= v
.visit_expr_subtype
(nexpr
, mtype
)
1042 if etype
== mtype
then
1043 assert ntype
!= null
1044 v
.modelbuilder
.advice
(ntype
, "useless-type", "Warning: useless type definition for variable `{variable.name}`")
1047 mtype
= v
.visit_expr
(nexpr
)
1048 if mtype
== null then return # Skip error
1052 var decltype
= mtype
1053 if mtype
== null or mtype
isa MNullType then
1054 var objclass
= v
.get_mclass
(self, "Object")
1055 if objclass
== null then return # skip error
1056 decltype
= objclass
.mclass_type
.as_nullable
1057 if mtype
== null then mtype
= decltype
1060 variable
.declared_type
= decltype
1061 v
.set_variable
(self, variable
, mtype
)
1063 #debug("var {variable}: {mtype}")
1066 self.is_typed
= true
1070 redef class AVarExpr
1071 redef fun its_variable
do return self.variable
1072 redef fun accept_typing
(v
)
1074 var variable
= self.variable
1075 if variable
== null then return # Skip error
1077 var mtype
= v
.get_variable
(self, variable
)
1078 if mtype
!= null then
1079 #debug("{variable} is {mtype}")
1081 #debug("{variable} is untyped")
1088 redef class AVarAssignExpr
1089 redef fun accept_typing
(v
)
1091 var variable
= self.variable
1092 assert variable
!= null
1094 var mtype
= v
.visit_expr_subtype
(n_value
, variable
.declared_type
)
1096 v
.set_variable
(self, variable
, mtype
)
1098 self.is_typed
= true
1102 redef class AReassignFormExpr
1103 # The method designed by the reassign operator.
1104 var reassign_callsite
: nullable CallSite
1106 var read_type
: nullable MType = null
1108 # Determine the `reassign_property`
1109 # `readtype` is the type of the reading of the left value.
1110 # `writetype` is the type of the writing of the left value.
1111 # (Because of `ACallReassignExpr`, both can be different.
1112 # Return the static type of the value to store.
1113 private fun resolve_reassignment
(v
: TypeVisitor, readtype
, writetype
: MType): nullable MType
1115 var reassign_name
= self.n_assign_op
.operator
1117 self.read_type
= readtype
1119 var callsite
= v
.get_method
(self.n_assign_op
, readtype
, reassign_name
, false)
1120 if callsite
== null then return null # Skip error
1121 self.reassign_callsite
= callsite
1123 var msignature
= callsite
.msignature
1124 var rettype
= msignature
.return_mtype
1125 assert msignature
.arity
== 1 and rettype
!= null
1127 var value_type
= v
.visit_expr_subtype
(self.n_value
, msignature
.mparameters
.first
.mtype
)
1128 if value_type
== null then return null # Skip error
1130 v
.check_subtype
(self, rettype
, writetype
, false)
1135 redef class AVarReassignExpr
1136 redef fun accept_typing
(v
)
1138 var variable
= self.variable
1139 assert variable
!= null
1141 var readtype
= v
.get_variable
(self, variable
)
1142 if readtype
== null then return
1144 read_type
= readtype
1146 var writetype
= variable
.declared_type
1147 if writetype
== null then return
1149 var rettype
= self.resolve_reassignment
(v
, readtype
, writetype
)
1151 v
.set_variable
(self, variable
, rettype
)
1153 self.is_typed
= rettype
!= null
1158 redef class AContinueExpr
1159 redef fun accept_typing
(v
)
1161 var nexpr
= self.n_expr
1162 if nexpr
!= null then
1165 self.is_typed
= true
1169 redef class ABreakExpr
1170 redef fun accept_typing
(v
)
1172 var nexpr
= self.n_expr
1173 if nexpr
!= null then
1176 self.is_typed
= true
1180 redef class AReturnExpr
1181 redef fun accept_typing
(v
)
1183 var nexpr
= self.n_expr
1185 var mpropdef
= v
.mpropdef
1186 if mpropdef
isa MMethodDef then
1187 ret_type
= mpropdef
.msignature
.return_mtype
1188 else if mpropdef
isa MAttributeDef then
1189 ret_type
= mpropdef
.static_mtype
1193 if nexpr
!= null then
1194 if ret_type
!= null then
1195 v
.visit_expr_subtype
(nexpr
, ret_type
)
1198 v
.error
(nexpr
, "Error: `return` with value in a procedure.")
1201 else if ret_type
!= null then
1202 v
.error
(self, "Error: `return` without value in a function.")
1205 self.is_typed
= true
1209 redef class AAbortExpr
1210 redef fun accept_typing
(v
)
1212 self.is_typed
= true
1217 redef fun accept_typing
(v
)
1219 v
.visit_expr_bool
(n_expr
)
1221 v
.visit_stmt
(n_then
)
1222 v
.visit_stmt
(n_else
)
1224 self.is_typed
= true
1226 if n_then
!= null and n_else
== null then
1227 self.mtype
= n_then
.mtype
1232 redef class AIfexprExpr
1233 redef fun accept_typing
(v
)
1235 v
.visit_expr_bool
(n_expr
)
1237 var t1
= v
.visit_expr
(n_then
)
1238 var t2
= v
.visit_expr
(n_else
)
1240 if t1
== null or t2
== null then
1244 var t
= v
.merge_types
(self, [t1
, t2
])
1246 v
.error
(self, "Type Error: ambiguous type `{t1}` vs `{t2}`.")
1253 redef fun accept_typing
(v
)
1255 v
.visit_stmt
(n_block
)
1256 v
.visit_stmt
(n_catch
)
1257 self.is_typed
= true
1261 redef class AWhileExpr
1262 redef fun accept_typing
(v
)
1265 v
.visit_expr_bool
(n_expr
)
1266 v
.visit_stmt
(n_block
)
1267 self.is_typed
= true
1271 redef class ALoopExpr
1272 redef fun accept_typing
(v
)
1275 v
.visit_stmt
(n_block
)
1276 self.is_typed
= true
1280 redef class AForExpr
1281 redef fun accept_typing
(v
)
1285 for g
in n_groups
do
1286 var mtype
= v
.visit_expr
(g
.n_expr
)
1287 if mtype
== null then return
1288 g
.do_type_iterator
(v
, mtype
)
1289 if g
.is_broken
then is_broken
= true
1292 v
.visit_stmt
(n_block
)
1294 self.mtype
= n_block
.mtype
1295 self.is_typed
= true
1299 redef class AForGroup
1300 var coltype
: nullable MClassType
1302 var method_iterator
: nullable CallSite
1303 var method_is_ok
: nullable CallSite
1304 var method_item
: nullable CallSite
1305 var method_next
: nullable CallSite
1306 var method_key
: nullable CallSite
1307 var method_finish
: nullable CallSite
1309 var method_lt
: nullable CallSite
1310 var method_successor
: nullable CallSite
1312 private fun do_type_iterator
(v
: TypeVisitor, mtype
: MType)
1314 if mtype
isa MNullType then
1315 v
.error
(self, "Type Error: `for` cannot iterate over `null`.")
1320 var objcla
= v
.get_mclass
(self, "Object")
1321 if objcla
== null then return
1323 # check iterator method
1324 var itdef
= v
.get_method
(self, mtype
, "iterator", n_expr
isa ASelfExpr)
1325 if itdef
== null then
1326 v
.error
(self, "Type Error: `for` expects a type providing an `iterator` method, got `{mtype}`.")
1329 self.method_iterator
= itdef
1331 # check that iterator return something
1332 var ittype
= itdef
.msignature
.return_mtype
1333 if ittype
== null then
1334 v
.error
(self, "Type Error: `for` expects the method `iterator` to return an `Iterator` or `MapIterator` type.")
1339 var colit_cla
= v
.try_get_mclass
(self, "Iterator")
1340 var mapit_cla
= v
.try_get_mclass
(self, "MapIterator")
1344 if colit_cla
!= null and v
.is_subtype
(ittype
, colit_cla
.get_mtype
([objcla
.mclass_type
.as_nullable
])) then
1346 var coltype
= ittype
.supertype_to
(v
.mmodule
, v
.anchor
, colit_cla
)
1347 var variables
= self.variables
1348 if variables
.length
!= 1 then
1349 v
.error
(self, "Type Error: `for` expects only one variable when using `Iterator`.")
1351 variables
.first
.declared_type
= coltype
.arguments
.first
1356 if mapit_cla
!= null and v
.is_subtype
(ittype
, mapit_cla
.get_mtype
([objcla
.mclass_type
.as_nullable
, objcla
.mclass_type
.as_nullable
])) then
1358 var coltype
= ittype
.supertype_to
(v
.mmodule
, v
.anchor
, mapit_cla
)
1359 var variables
= self.variables
1360 if variables
.length
!= 2 then
1361 v
.error
(self, "Type Error: `for` expects two variables when using `MapIterator`.")
1363 variables
[0].declared_type
= coltype
.arguments
[0]
1364 variables
[1].declared_type
= coltype
.arguments
[1]
1369 if not is_col
and not is_map
then
1370 v
.error
(self, "Type Error: `for` expects the method `iterator` to return an `Iterator` or `MapIterator` type.")
1374 # anchor formal and virtual types
1375 if mtype
.need_anchor
then mtype
= v
.anchor_to
(mtype
)
1377 mtype
= mtype
.undecorate
1378 self.coltype
= mtype
.as(MClassType)
1380 # get methods is_ok, next, item
1381 var ikdef
= v
.get_method
(self, ittype
, "is_ok", false)
1382 if ikdef
== null then
1383 v
.error
(self, "Type Error: `for` expects a method `is_ok` in type `{ittype}`.")
1386 self.method_is_ok
= ikdef
1388 var itemdef
= v
.get_method
(self, ittype
, "item", false)
1389 if itemdef
== null then
1390 v
.error
(self, "Type Error: `for` expects a method `item` in type `{ittype}`.")
1393 self.method_item
= itemdef
1395 var nextdef
= v
.get_method
(self, ittype
, "next", false)
1396 if nextdef
== null then
1397 v
.error
(self, "Type Error: `for` expects a method `next` in type {ittype}.")
1400 self.method_next
= nextdef
1402 self.method_finish
= v
.try_get_method
(self, ittype
, "finish", false)
1405 var keydef
= v
.get_method
(self, ittype
, "key", false)
1406 if keydef
== null then
1407 v
.error
(self, "Type Error: `for` expects a method `key` in type `{ittype}`.")
1410 self.method_key
= keydef
1413 if self.variables
.length
== 1 and n_expr
isa ARangeExpr then
1414 var variable
= variables
.first
1415 var vtype
= variable
.declared_type
.as(not null)
1417 if n_expr
isa AOrangeExpr then
1418 self.method_lt
= v
.get_method
(self, vtype
, "<", false)
1420 self.method_lt
= v
.get_method
(self, vtype
, "<=", false)
1423 self.method_successor
= v
.get_method
(self, vtype
, "successor", false)
1428 redef class AWithExpr
1429 var method_start
: nullable CallSite
1430 var method_finish
: nullable CallSite
1432 redef fun accept_typing
(v
: TypeVisitor)
1434 var mtype
= v
.visit_expr
(n_expr
)
1435 if mtype
== null then return
1437 method_start
= v
.get_method
(self, mtype
, "start", n_expr
isa ASelfExpr)
1438 method_finish
= v
.get_method
(self, mtype
, "finish", n_expr
isa ASelfExpr)
1440 v
.visit_stmt
(n_block
)
1441 self.mtype
= n_block
.mtype
1442 self.is_typed
= true
1446 redef class AAssertExpr
1447 redef fun accept_typing
(v
)
1449 v
.visit_expr_bool
(n_expr
)
1451 v
.visit_stmt
(n_else
)
1452 self.is_typed
= true
1457 redef fun accept_typing
(v
)
1459 v
.visit_expr_bool
(n_expr
)
1460 v
.visit_expr_bool
(n_expr2
)
1461 self.mtype
= v
.type_bool
(self)
1465 redef class AImpliesExpr
1466 redef fun accept_typing
(v
)
1468 v
.visit_expr_bool
(n_expr
)
1469 v
.visit_expr_bool
(n_expr2
)
1470 self.mtype
= v
.type_bool
(self)
1474 redef class AAndExpr
1475 redef fun accept_typing
(v
)
1477 v
.visit_expr_bool
(n_expr
)
1478 v
.visit_expr_bool
(n_expr2
)
1479 self.mtype
= v
.type_bool
(self)
1484 redef class ANotExpr
1485 redef fun accept_typing
(v
)
1487 v
.visit_expr_bool
(n_expr
)
1488 self.mtype
= v
.type_bool
(self)
1492 redef class AOrElseExpr
1493 redef fun accept_typing
(v
)
1495 var t1
= v
.visit_expr
(n_expr
)
1496 var t2
= v
.visit_expr
(n_expr2
)
1498 if t1
== null or t2
== null then
1502 if t1
isa MNullType then
1505 else if v
.can_be_null
(t1
) then
1509 var t
= v
.merge_types
(self, [t1
, t2
])
1511 var c
= v
.get_mclass
(self, "Object")
1512 if c
== null then return # forward error
1514 if v
.can_be_null
(t2
) then
1517 #v.error(self, "Type Error: ambiguous type {t1} vs {t2}")
1522 redef fun accept_post_typing
(v
)
1524 var t1
= n_expr
.mtype
1528 v
.check_can_be_null
(n_expr
, t1
)
1533 redef class ATrueExpr
1534 redef fun accept_typing
(v
)
1536 self.mtype
= v
.type_bool
(self)
1540 redef class AFalseExpr
1541 redef fun accept_typing
(v
)
1543 self.mtype
= v
.type_bool
(self)
1547 redef class AIntegerExpr
1548 redef fun accept_typing
(v
)
1550 var mclass
: nullable MClass = null
1551 if value
isa Byte then
1552 mclass
= v
.get_mclass
(self, "Byte")
1553 else if value
isa Int then
1554 mclass
= v
.get_mclass
(self, "Int")
1555 else if value
isa Int8 then
1556 mclass
= v
.get_mclass
(self, "Int8")
1557 else if value
isa Int16 then
1558 mclass
= v
.get_mclass
(self, "Int16")
1559 else if value
isa UInt16 then
1560 mclass
= v
.get_mclass
(self, "UInt16")
1561 else if value
isa Int32 then
1562 mclass
= v
.get_mclass
(self, "Int32")
1563 else if value
isa UInt32 then
1564 mclass
= v
.get_mclass
(self, "UInt32")
1566 if mclass
== null then return # Forward error
1567 self.mtype
= mclass
.mclass_type
1571 redef class AFloatExpr
1572 redef fun accept_typing
(v
)
1574 var mclass
= v
.get_mclass
(self, "Float")
1575 if mclass
== null then return # Forward error
1576 self.mtype
= mclass
.mclass_type
1580 redef class ACharExpr
1581 redef fun accept_typing
(v
) do
1582 var mclass
: nullable MClass = null
1584 mclass
= v
.get_mclass
(self, "Byte")
1585 else if is_code_point
then
1586 mclass
= v
.get_mclass
(self, "Int")
1588 mclass
= v
.get_mclass
(self, "Char")
1590 if mclass
== null then return # Forward error
1591 self.mtype
= mclass
.mclass_type
1595 redef class AugmentedStringFormExpr
1598 # Text::to_re, used for prefix `re`
1599 var to_re
: nullable CallSite = null
1600 # Regex::ignore_case, used for suffix `i` on `re`
1601 var ignore_case
: nullable CallSite = null
1602 # Regex::newline, used for suffix `m` on `re`
1603 var newline
: nullable CallSite = null
1604 # Regex::extended, used for suffix `b` on `re`
1605 var extended
: nullable CallSite = null
1606 # CString::to_bytes_with_copy, used for prefix `b`
1607 var to_bytes_with_copy
: nullable CallSite = null
1609 redef fun accept_typing
(v
) do
1610 var mclass
= v
.get_mclass
(self, "String")
1611 if mclass
== null then return # Forward error
1612 if is_bytestring
then
1613 to_bytes_with_copy
= v
.get_method
(self, v
.mmodule
.c_string_type
, "to_bytes_with_copy", false)
1614 mclass
= v
.get_mclass
(self, "Bytes")
1616 to_re
= v
.get_method
(self, mclass
.mclass_type
, "to_re", false)
1617 for i
in suffix
.chars
do
1618 mclass
= v
.get_mclass
(self, "Regex")
1619 if mclass
== null then
1620 v
.error
(self, "Error: `Regex` class unknown")
1625 service
= "ignore_case="
1626 ignore_case
= v
.get_method
(self, mclass
.mclass_type
, service
, false)
1627 else if i
== 'm' then
1628 service
= "newline="
1629 newline
= v
.get_method
(self, mclass
.mclass_type
, service
, false)
1630 else if i
== 'b' then
1631 service
= "extended="
1632 extended
= v
.get_method
(self, mclass
.mclass_type
, service
, false)
1634 v
.error
(self, "Type Error: Unrecognized suffix {i} in prefixed Regex")
1639 if mclass
== null then return # Forward error
1640 mtype
= mclass
.mclass_type
1644 redef class ASuperstringExpr
1645 redef fun accept_typing
(v
)
1648 var objclass
= v
.get_mclass
(self, "Object")
1649 if objclass
== null then return # Forward error
1650 var objtype
= objclass
.mclass_type
1651 for nexpr
in self.n_exprs
do
1652 v
.visit_expr_subtype
(nexpr
, objtype
)
1657 redef class AArrayExpr
1658 # The `with_capacity` method on Array
1659 var with_capacity_callsite
: nullable CallSite
1661 # The `push` method on arrays
1662 var push_callsite
: nullable CallSite
1664 # The element of each type
1665 var element_mtype
: nullable MType
1667 # Set that `self` is a part of comprehension array `na`
1668 # If `self` is a `for`, or a `if`, then `set_comprehension` is recursively applied.
1669 private fun set_comprehension
(n
: nullable AExpr)
1673 else if n
isa AForExpr then
1674 set_comprehension
(n
.n_block
)
1675 else if n
isa AIfExpr then
1676 set_comprehension
(n
.n_then
)
1677 set_comprehension
(n
.n_else
)
1680 n
.comprehension
= self
1683 redef fun accept_typing
(v
)
1685 var mtype
: nullable MType = null
1686 var ntype
= self.n_type
1687 if ntype
!= null then
1688 mtype
= v
.resolve_mtype
(ntype
)
1689 if mtype
== null then return # Skip error
1691 var mtypes
= new Array[nullable MType]
1693 for e
in self.n_exprs
do
1694 var t
= v
.visit_expr
(e
)
1698 set_comprehension
(e
)
1699 if mtype
!= null then
1700 if v
.check_subtype
(e
, t
, mtype
, false) == null then return # Forward error
1701 if t
== mtype
then useless
= true
1706 if mtype
== null then
1707 # Ensure monotony for type adaptation on loops
1708 if self.element_mtype
!= null then mtypes
.add
self.element_mtype
1709 mtype
= v
.merge_types
(self, mtypes
)
1711 if mtype
== null or mtype
isa MNullType then
1712 v
.error
(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
1716 assert ntype
!= null
1717 v
.modelbuilder
.warning
(ntype
, "useless-type", "Warning: useless type declaration `{mtype}` in literal Array since it can be inferred from the elements type.")
1720 self.element_mtype
= mtype
1722 var mclass
= v
.get_mclass
(self, "Array")
1723 if mclass
== null then return # Forward error
1724 var array_mtype
= mclass
.get_mtype
([mtype
])
1726 with_capacity_callsite
= v
.get_method
(self, array_mtype
, "with_capacity", false)
1727 push_callsite
= v
.get_method
(self, array_mtype
, "push", false)
1729 self.mtype
= array_mtype
1733 redef class ARangeExpr
1734 var init_callsite
: nullable CallSite
1736 redef fun accept_typing
(v
)
1738 var discrete_class
= v
.get_mclass
(self, "Discrete")
1739 if discrete_class
== null then return # Forward error
1740 var discrete_type
= discrete_class
.intro
.bound_mtype
1741 var t1
= v
.visit_expr_subtype
(self.n_expr
, discrete_type
)
1742 var t2
= v
.visit_expr_subtype
(self.n_expr2
, discrete_type
)
1743 if t1
== null or t2
== null then return
1744 var mclass
= v
.get_mclass
(self, "Range")
1745 if mclass
== null then return # Forward error
1747 if v
.is_subtype
(t1
, t2
) then
1748 mtype
= mclass
.get_mtype
([t2
])
1749 else if v
.is_subtype
(t2
, t1
) then
1750 mtype
= mclass
.get_mtype
([t1
])
1752 v
.error
(self, "Type Error: cannot create range: `{t1}` vs `{t2}`.")
1758 # get the constructor
1760 if self isa ACrangeExpr then
1761 callsite
= v
.get_method
(self, mtype
, "init", false)
1762 else if self isa AOrangeExpr then
1763 callsite
= v
.get_method
(self, mtype
, "without_last", false)
1767 init_callsite
= callsite
1771 redef class ANullExpr
1772 redef fun accept_typing
(v
)
1774 self.mtype
= v
.mmodule
.model
.null_type
1778 redef class AIsaExpr
1779 # The static type to cast to.
1780 # (different from the static type of the expression that is `Bool`).
1781 var cast_type
: nullable MType
1782 redef fun accept_typing
(v
)
1784 v
.visit_expr
(n_expr
)
1786 var mtype
= v
.resolve_mtype
(n_type
)
1788 self.cast_type
= mtype
1790 var variable
= self.n_expr
.its_variable
1791 if variable
!= null then
1792 var orig
= self.n_expr
.mtype
1793 #var from = if orig != null then orig.to_s else "invalid"
1794 #var to = if mtype != null then mtype.to_s else "invalid"
1795 #debug("adapt {variable}: {from} -> {to}")
1797 var thentype
= v
.intersect_types
(self, orig
, mtype
)
1798 if thentype
!= orig
then
1799 self.after_flow_context
.when_true
.set_var
(v
, variable
, thentype
)
1800 #debug "{variable}:{orig or else "?"} isa {mtype or else "?"} -> then {thentype or else "?"}"
1803 var elsetype
= v
.diff_types
(self, orig
, mtype
)
1804 if elsetype
!= orig
then
1805 self.after_flow_context
.when_false
.set_var
(v
, variable
, elsetype
)
1806 #debug "{variable}:{orig or else "?"} isa {mtype or else "?"} -> else {elsetype or else "?"}"
1810 self.mtype
= v
.type_bool
(self)
1813 redef fun accept_post_typing
(v
)
1815 v
.check_expr_cast
(self, self.n_expr
, self.n_type
)
1818 redef fun dump_info
(v
) do
1820 var mtype
= self.cast_type
1821 if mtype
!= null then
1822 res
+= v
.yellow
(".as({mtype})")
1829 redef class AAsCastExpr
1830 redef fun accept_typing
(v
)
1832 v
.visit_expr
(n_expr
)
1834 self.mtype
= v
.resolve_mtype
(n_type
)
1837 redef fun accept_post_typing
(v
)
1839 v
.check_expr_cast
(self, self.n_expr
, self.n_type
)
1843 redef class AAsNotnullExpr
1844 redef fun accept_typing
(v
)
1846 var mtype
= v
.visit_expr
(self.n_expr
)
1847 if mtype
== null then return # Forward error
1849 if mtype
isa MNullType then
1850 v
.error
(self, "Type Error: `as(not null)` on `null`.")
1854 if v
.can_be_null
(mtype
) then
1855 mtype
= mtype
.as_notnull
1861 redef fun accept_post_typing
(v
)
1863 var mtype
= n_expr
.mtype
1864 if mtype
== null then return
1865 v
.check_can_be_null
(n_expr
, mtype
)
1869 redef class AParExpr
1870 redef fun accept_typing
(v
)
1872 self.mtype
= v
.visit_expr
(self.n_expr
)
1876 redef class AOnceExpr
1877 redef fun accept_typing
(v
)
1879 self.mtype
= v
.visit_expr
(self.n_expr
)
1883 redef class ASelfExpr
1884 redef var its_variable
: nullable Variable
1885 redef fun accept_typing
(v
)
1887 if v
.is_toplevel_context
and not self isa AImplicitSelfExpr then
1888 v
.error
(self, "Error: `self` cannot be used in top-level method.")
1890 var variable
= v
.selfvariable
1891 self.its_variable
= variable
1892 self.mtype
= v
.get_variable
(self, variable
)
1896 redef class AImplicitSelfExpr
1897 # Is the implicit receiver `sys`?
1899 # By default, the implicit receiver is `self`.
1900 # But when there is not method for `self`, `sys` is used as a fall-back.
1901 # Is this case this flag is set to `true`.
1905 ## MESSAGE SENDING AND PROPERTY
1907 redef class ASendExpr
1908 # The property invoked by the send.
1909 var callsite
: nullable CallSite
1911 redef fun bad_expr_message
(child
)
1913 if child
== self.n_expr
then
1914 return "to be the receiver of `{self.property_name}`"
1919 redef fun accept_typing
(v
)
1921 var nrecv
= self.n_expr
1922 var recvtype
= v
.visit_expr
(nrecv
)
1923 var name
= self.property_name
1924 var node
= self.property_node
1926 if recvtype
== null then return # Forward error
1929 var unsafe_type
= v
.anchor_to
(recvtype
)
1930 var mproperty
= v
.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
1931 if mproperty
== null and nrecv
isa AImplicitSelfExpr then
1932 # Special fall-back search in `sys` when noting found in the implicit receiver.
1933 var sysclass
= v
.try_get_mclass
(node
, "Sys")
1934 if sysclass
!= null then
1935 var systype
= sysclass
.mclass_type
1936 mproperty
= v
.try_get_mproperty_by_name2
(node
, systype
, name
)
1937 if mproperty
!= null then
1938 callsite
= v
.get_method
(node
, systype
, name
, false)
1939 if callsite
== null then return # Forward error
1940 # Update information, we are looking at `sys` now, not `self`
1942 nrecv
.its_variable
= null
1943 nrecv
.mtype
= systype
1948 if callsite
== null then
1949 # If still nothing, just exit
1950 callsite
= v
.get_method
(node
, recvtype
, name
, nrecv
isa ASelfExpr)
1951 if callsite
== null then return
1954 self.callsite
= callsite
1955 var msignature
= callsite
.msignature
1957 var args
= compute_raw_arguments
1959 callsite
.check_signature
(v
, node
, args
)
1961 if callsite
.mproperty
.is_init
then
1962 var vmpropdef
= v
.mpropdef
1963 if not (vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_init
) then
1964 v
.error
(node
, "Error: an `init` can only be called from another `init`.")
1966 if vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_root_init
and not callsite
.mproperty
.is_root_init
then
1967 v
.error
(node
, "Error: `{vmpropdef}` cannot call a factory `{callsite.mproperty}`.")
1971 var ret
= msignature
.return_mtype
1975 self.is_typed
= true
1979 # The name of the property
1980 # Each subclass simply provide the correct name.
1981 private fun property_name
: String is abstract
1983 # The node identifying the name (id, operator, etc) for messages.
1985 # Is `self` by default
1986 private fun property_node
: ANode do return self
1988 # An array of all arguments (excluding self)
1989 fun raw_arguments
: Array[AExpr] do return compute_raw_arguments
1991 private fun compute_raw_arguments
: Array[AExpr] is abstract
1993 redef fun dump_info
(v
) do
1995 var callsite
= self.callsite
1996 if callsite
!= null then
1997 res
+= v
.yellow
(" call="+callsite
.dump_info
(v
))
2003 redef class ABinopExpr
2004 redef fun compute_raw_arguments
do return [n_expr2
]
2005 redef fun property_name
do return operator
2006 redef fun property_node
do return n_op
2009 redef class AEqFormExpr
2010 redef fun accept_typing
(v
)
2016 redef fun accept_post_typing
(v
)
2018 var mtype
= n_expr
.mtype
2019 var mtype2
= n_expr2
.mtype
2021 if mtype
== null or mtype2
== null then return
2023 if not mtype2
isa MNullType then return
2025 v
.check_can_be_null
(n_expr
, mtype
)
2029 redef class AUnaryopExpr
2030 redef fun property_name
do return "unary {operator}"
2031 redef fun compute_raw_arguments
do return new Array[AExpr]
2035 redef class ACallExpr
2036 redef fun property_name
do return n_qid
.n_id
.text
2037 redef fun property_node
do return n_qid
2038 redef fun compute_raw_arguments
do return n_args
.to_a
2041 redef class ACallAssignExpr
2042 redef fun property_name
do return n_qid
.n_id
.text
+ "="
2043 redef fun property_node
do return n_qid
2044 redef fun compute_raw_arguments
2046 var res
= n_args
.to_a
2052 redef class ABraExpr
2053 redef fun property_name
do return "[]"
2054 redef fun compute_raw_arguments
do return n_args
.to_a
2057 redef class ABraAssignExpr
2058 redef fun property_name
do return "[]="
2059 redef fun compute_raw_arguments
2061 var res
= n_args
.to_a
2067 redef class ASendReassignFormExpr
2068 # The property invoked for the writing
2069 var write_callsite
: nullable CallSite
2071 redef fun accept_typing
(v
)
2073 var recvtype
= v
.visit_expr
(self.n_expr
)
2074 var name
= self.property_name
2075 var node
= self.property_node
2077 if recvtype
== null then return # Forward error
2079 var for_self
= self.n_expr
isa ASelfExpr
2080 var callsite
= v
.get_method
(node
, recvtype
, name
, for_self
)
2082 if callsite
== null then return
2083 self.callsite
= callsite
2085 var args
= compute_raw_arguments
2087 callsite
.check_signature
(v
, node
, args
)
2089 var readtype
= callsite
.msignature
.return_mtype
2090 if readtype
== null then
2091 v
.error
(node
, "Error: `{name}` is not a function.")
2095 var wcallsite
= v
.get_method
(node
, recvtype
, name
+ "=", self.n_expr
isa ASelfExpr)
2096 if wcallsite
== null then return
2097 self.write_callsite
= wcallsite
2099 var wtype
= self.resolve_reassignment
(v
, readtype
, wcallsite
.msignature
.mparameters
.last
.mtype
)
2100 if wtype
== null then return
2102 args
= args
.to_a
# duplicate so raw_arguments keeps only the getter args
2103 args
.add
(self.n_value
)
2104 wcallsite
.check_signature
(v
, node
, args
)
2106 self.is_typed
= true
2110 redef class ACallReassignExpr
2111 redef fun property_name
do return n_qid
.n_id
.text
2112 redef fun property_node
do return n_qid
.n_id
2113 redef fun compute_raw_arguments
do return n_args
.to_a
2116 redef class ABraReassignExpr
2117 redef fun property_name
do return "[]"
2118 redef fun compute_raw_arguments
do return n_args
.to_a
2121 redef class AInitExpr
2122 redef fun property_name
do return "init"
2123 redef fun property_node
do return n_kwinit
2124 redef fun compute_raw_arguments
do return n_args
.to_a
2128 fun to_a
: Array[AExpr] do return self.n_exprs
.to_a
2133 redef class ASuperExpr
2134 # The method to call if the super is in fact a 'super init call'
2135 # Note: if the super is a normal call-next-method, then this attribute is null
2136 var callsite
: nullable CallSite
2138 # The method to call is the super is a standard `call-next-method` super-call
2139 # Note: if the super is a special super-init-call, then this attribute is null
2140 var mpropdef
: nullable MMethodDef
2142 redef fun accept_typing
(v
)
2144 var anchor
= v
.anchor
2145 assert anchor
!= null
2146 var recvtype
= v
.get_variable
(self, v
.selfvariable
)
2147 assert recvtype
!= null
2148 var mproperty
= v
.mpropdef
.mproperty
2149 if not mproperty
isa MMethod then
2150 v
.error
(self, "Error: `super` only usable in a `method`.")
2153 var superprops
= mproperty
.lookup_super_definitions
(v
.mmodule
, anchor
)
2154 if superprops
.length
== 0 then
2155 if mproperty
.is_init
and v
.mpropdef
.is_intro
then
2156 process_superinit
(v
)
2159 v
.error
(self, "Error: no super method to call for `{mproperty}`.")
2162 # FIXME: covariance of return type in linear extension?
2163 var superprop
= superprops
.first
2165 var msignature
= superprop
.msignature
.as(not null)
2166 msignature
= v
.resolve_for
(msignature
, recvtype
, true).as(MSignature)
2167 var args
= self.n_args
.to_a
2168 if args
.length
> 0 then
2169 signaturemap
= v
.check_signature
(self, args
, mproperty
, msignature
)
2171 self.mtype
= msignature
.return_mtype
2172 self.is_typed
= true
2173 v
.mpropdef
.has_supercall
= true
2174 mpropdef
= v
.mpropdef
.as(MMethodDef)
2177 # The mapping used on the call to associate arguments to parameters.
2178 # If null then no specific association is required.
2179 var signaturemap
: nullable SignatureMap
2181 private fun process_superinit
(v
: TypeVisitor)
2183 var anchor
= v
.anchor
2184 assert anchor
!= null
2185 var recvtype
= v
.get_variable
(self, v
.selfvariable
)
2186 assert recvtype
!= null
2187 var mpropdef
= v
.mpropdef
2188 assert mpropdef
isa MMethodDef
2189 var mproperty
= mpropdef
.mproperty
2190 var superprop
: nullable MMethodDef = null
2191 for msupertype
in mpropdef
.mclassdef
.supertypes
do
2192 msupertype
= msupertype
.anchor_to
(v
.mmodule
, anchor
)
2193 var errcount
= v
.modelbuilder
.toolcontext
.error_count
2194 var candidate
= v
.try_get_mproperty_by_name2
(self, msupertype
, mproperty
.name
).as(nullable MMethod)
2195 if candidate
== null then
2196 if v
.modelbuilder
.toolcontext
.error_count
> errcount
then return # Forward error
2197 continue # Try next super-class
2199 if superprop
!= null and candidate
.is_root_init
then
2202 if superprop
!= null and superprop
.mproperty
!= candidate
and not superprop
.mproperty
.is_root_init
then
2203 v
.error
(self, "Error: conflicting super constructor to call for `{mproperty}`: `{candidate.full_name}`, `{superprop.mproperty.full_name}`")
2206 var candidatedefs
= candidate
.lookup_definitions
(v
.mmodule
, anchor
)
2207 if superprop
!= null and superprop
.mproperty
== candidate
then
2208 if superprop
== candidatedefs
.first
then continue
2209 candidatedefs
.add
(superprop
)
2211 if candidatedefs
.length
> 1 then
2212 v
.error
(self, "Error: conflicting property definitions for property `{mproperty}` in `{recvtype}`: {candidatedefs.join(", ")}")
2215 superprop
= candidatedefs
.first
2217 if superprop
== null then
2218 v
.error
(self, "Error: no super method to call for `{mproperty}`.")
2222 var msignature
= superprop
.new_msignature
or else superprop
.msignature
.as(not null)
2223 msignature
= v
.resolve_for
(msignature
, recvtype
, true).as(MSignature)
2225 var callsite
= new CallSite(hot_location
, recvtype
, v
.mmodule
, v
.anchor
, true, superprop
.mproperty
, superprop
, msignature
, false)
2226 self.callsite
= callsite
2228 var args
= self.n_args
.to_a
2229 if args
.length
> 0 then
2230 callsite
.check_signature
(v
, self, args
)
2232 # Check there is at least enough parameters
2233 if mpropdef
.msignature
.arity
< msignature
.arity
then
2234 v
.error
(self, "Error: not enough implicit arguments to pass. Got `{mpropdef.msignature.arity}`, expected at least `{msignature.arity}`. Signature is `{msignature}`.")
2237 # Check that each needed parameter is conform
2239 for sp
in msignature
.mparameters
do
2240 var p
= mpropdef
.msignature
.mparameters
[i
]
2241 if not v
.is_subtype
(p
.mtype
, sp
.mtype
) then
2242 v
.error
(self, "Type Error: expected argument #{i} of type `{sp.mtype}`, got implicit argument `{p.name}` of type `{p.mtype}`. Signature is {msignature}")
2249 self.is_typed
= true
2252 redef fun dump_info
(v
) do
2254 var callsite
= self.callsite
2255 if callsite
!= null then
2256 res
+= v
.yellow
(" super-init="+callsite
.dump_info
(v
))
2258 var mpropdef
= self.mpropdef
2259 if mpropdef
!= null then
2260 res
+= v
.yellow
(" call-next-method="+mpropdef
.to_s
)
2268 redef class ANewExpr
2269 # The constructor invoked by the new.
2270 var callsite
: nullable CallSite
2272 # The designated type
2273 var recvtype
: nullable MClassType
2275 redef fun accept_typing
(v
)
2277 var recvtype
= v
.resolve_mtype
(self.n_type
)
2278 if recvtype
== null then return
2280 if not recvtype
isa MClassType then
2281 if recvtype
isa MNullableType then
2282 v
.error
(self, "Type Error: cannot instantiate the nullable type `{recvtype}`.")
2284 else if recvtype
isa MFormalType then
2285 v
.error
(self, "Type Error: cannot instantiate the formal type `{recvtype}`.")
2288 v
.error
(self, "Type Error: cannot instantiate the type `{recvtype}`.")
2293 self.recvtype
= recvtype
2294 var kind
= recvtype
.mclass
.kind
2297 var nqid
= self.n_qid
2299 if nqid
!= null then
2300 name
= nqid
.n_id
.text
2306 if name
== "intern" then
2307 if kind
!= concrete_kind
then
2308 v
.error
(self, "Type Error: cannot instantiate {kind} {recvtype}.")
2311 if n_args
.n_exprs
.not_empty
then
2312 v
.error
(n_args
, "Type Error: the intern constructor expects no arguments.")
2316 self.mtype
= recvtype
2320 var callsite
= v
.get_method
(node
, recvtype
, name
, false)
2321 if callsite
== null then return
2323 if not callsite
.mproperty
.is_new
then
2324 if kind
!= concrete_kind
then
2325 v
.error
(self, "Type Error: cannot instantiate {kind} `{recvtype}`.")
2328 self.mtype
= recvtype
2330 self.mtype
= callsite
.msignature
.return_mtype
2331 assert self.mtype
!= null
2334 self.callsite
= callsite
2336 if not callsite
.mproperty
.is_init_for
(recvtype
.mclass
) then
2337 v
.error
(self, "Error: `{name}` is not a constructor.")
2341 var args
= n_args
.to_a
2342 callsite
.check_signature
(v
, node
, args
)
2345 redef fun dump_info
(v
) do
2347 var callsite
= self.callsite
2348 if callsite
!= null then
2349 res
+= v
.yellow
(" call="+callsite
.dump_info
(v
))
2357 redef class AAttrFormExpr
2358 # The attribute accessed.
2359 var mproperty
: nullable MAttribute
2361 # The static type of the attribute.
2362 var attr_type
: nullable MType
2364 # Resolve the attribute accessed.
2365 private fun resolve_property
(v
: TypeVisitor)
2367 var recvtype
= v
.visit_expr
(self.n_expr
)
2368 if recvtype
== null then return # Skip error
2369 var node
= self.n_id
2370 var name
= node
.text
2371 if recvtype
isa MNullType then
2372 v
.error
(node
, "Error: attribute `{name}` access on `null`.")
2376 var unsafe_type
= v
.anchor_to
(recvtype
)
2377 var mproperty
= v
.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
2378 if mproperty
== null then
2379 v
.modelbuilder
.error
(node
, "Error: attribute `{name}` does not exist in `{recvtype}`.")
2382 assert mproperty
isa MAttribute
2383 self.mproperty
= mproperty
2385 var mpropdefs
= mproperty
.lookup_definitions
(v
.mmodule
, unsafe_type
)
2386 assert mpropdefs
.length
== 1
2387 var mpropdef
= mpropdefs
.first
2388 var attr_type
= mpropdef
.static_mtype
2389 if attr_type
== null then return # skip error
2390 attr_type
= v
.resolve_for
(attr_type
, recvtype
, self.n_expr
isa ASelfExpr)
2391 self.attr_type
= attr_type
2394 redef fun dump_info
(v
) do
2396 var mproperty
= self.mproperty
2397 var attr_type
= self.attr_type
2398 if mproperty
!= null then
2399 res
+= v
.yellow
(" attr={mproperty}:{attr_type or else "BROKEN"}")
2405 redef class AAttrExpr
2406 redef fun accept_typing
(v
)
2408 self.resolve_property
(v
)
2409 self.mtype
= self.attr_type
2414 redef class AAttrAssignExpr
2415 redef fun accept_typing
(v
)
2417 self.resolve_property
(v
)
2418 var mtype
= self.attr_type
2420 v
.visit_expr_subtype
(self.n_value
, mtype
)
2421 self.is_typed
= mtype
!= null
2425 redef class AAttrReassignExpr
2426 redef fun accept_typing
(v
)
2428 self.resolve_property
(v
)
2429 var mtype
= self.attr_type
2430 if mtype
== null then return # Skip error
2432 var rettype
= self.resolve_reassignment
(v
, mtype
, mtype
)
2434 self.is_typed
= rettype
!= null
2438 redef class AIssetAttrExpr
2439 redef fun accept_typing
(v
)
2441 self.resolve_property
(v
)
2442 var mtype
= self.attr_type
2443 if mtype
== null then return # Skip error
2445 var recvtype
= self.n_expr
.mtype
.as(not null)
2446 var bound
= v
.resolve_for
(mtype
, recvtype
, false)
2447 if bound
isa MNullableType then
2448 v
.error
(n_id
, "Type Error: `isset` on a nullable attribute.")
2450 self.mtype
= v
.type_bool
(self)
2454 redef class AVarargExpr
2455 redef fun accept_typing
(v
)
2457 # This kind of pseudo-expression can be only processed trough a signature
2458 # See `check_signature`
2459 # Other cases are a syntax error.
2460 v
.error
(self, "Syntax Error: unexpected `...`.")
2466 redef class ADebugTypeExpr
2467 redef fun accept_typing
(v
)
2469 var expr
= v
.visit_expr
(self.n_expr
)
2470 if expr
== null then return
2471 var unsafe
= v
.anchor_to
(expr
)
2472 var ntype
= self.n_type
2473 var mtype
= v
.resolve_mtype
(ntype
)
2474 if mtype
!= null and mtype
!= expr
then
2475 var umtype
= v
.anchor_to
(mtype
)
2476 v
.modelbuilder
.warning
(self, "debug", "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")
2478 self.is_typed
= true