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 = null
44 # The analyzed mclassdef
45 var mclassdef
: nullable MClassDef = null
47 # The analyzed property
48 var mpropdef
: nullable MPropDef
50 var selfvariable
= new Variable("self")
52 # Is `self` use restricted?
53 # * no explicit `self`
54 # * method called on the implicit self must be top-level
55 # Currently only used for `new` factory since there is no valid receiver inside
56 var is_toplevel_context
= false
60 var mpropdef
= self.mpropdef
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_new
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 when `autocast` is true:
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, autocast
: Bool): nullable MType
113 if self.is_subtype
(sub
, sup
) then return sub
114 if autocast
and 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 if sub
.need_anchor
then
120 var u
= anchor_to
(sub
)
121 self.modelbuilder
.error
(node
, "Type Error: expected `{sup}`, got `{sub}: {u}`.")
123 self.modelbuilder
.error
(node
, "Type Error: expected `{sup}`, got `{sub}`.")
128 # Visit an expression and do not care about the return value
129 fun visit_stmt
(nexpr
: nullable AExpr)
131 if nexpr
== null then return
132 nexpr
.accept_typing
(self)
135 # Visit an expression and expects that it is not a statement
136 # Return the type of the expression
137 # Display an error and return null if:
138 # * the type cannot be determined or
139 # * `nexpr` is a statement
140 fun visit_expr
(nexpr
: AExpr): nullable MType
142 nexpr
.accept_typing
(self)
143 var mtype
= nexpr
.mtype
144 if mtype
!= null then return mtype
145 if not nexpr
.is_typed
then
146 if not self.modelbuilder
.toolcontext
.error_count
> 0 then # check that there is really an error
147 if self.modelbuilder
.toolcontext
.verbose_level
> 1 then
148 nexpr
.debug
("No return type but no error.")
151 return null # forward error
153 self.error
(nexpr
, "Error: expected an expression.")
157 # Visit an expression and expect its static type is a least a `sup`
158 # Return the type of the expression or null if
159 # * the type cannot be determined or
160 # * `nexpr` is a statement or
161 # * `nexpr` is not a `sup`
162 fun visit_expr_subtype
(nexpr
: AExpr, sup
: nullable MType): nullable MType
164 var sub
= visit_expr
(nexpr
)
165 if sub
== null then return null # Forward error
167 if sup
== null then return null # Forward error
169 var res
= check_subtype
(nexpr
, sub
, sup
, true)
171 nexpr
.implicit_cast_to
= res
176 # Visit an expression and expect its static type is a `Bool`
177 # Return the type of the expression or null if
178 # * the type cannot be determined or
179 # * `nexpr` is a statement or
180 # * `nexpr` is not a `Bool`
181 fun visit_expr_bool
(nexpr
: AExpr): nullable MType
183 return self.visit_expr_subtype
(nexpr
, self.type_bool
(nexpr
))
187 fun check_expr_cast
(node
: ANode, nexpr
: AExpr, ntype
: AType): nullable MType
189 var sub
= nexpr
.mtype
190 if sub
== null then return null # Forward error
192 var sup
= ntype
.mtype
193 if sup
== null then return null # Forward error
196 self.modelbuilder
.warning
(node
, "useless-type-test", "Warning: expression is already a `{sup}`.")
197 else if self.is_subtype
(sub
, sup
) then
198 self.modelbuilder
.warning
(node
, "useless-type-test", "Warning: expression is already a `{sup}` since it is a `{sub}`.")
203 # Can `mtype` be null (up to the current knowledge)?
204 fun can_be_null
(mtype
: MType): Bool
206 if mtype
isa MNullableType or mtype
isa MNullType then return true
207 if mtype
isa MFormalType then
208 var x
= anchor_to
(mtype
)
209 if x
isa MNullableType or x
isa MNullType then return true
214 # Check that `mtype` can be null (up to the current knowledge).
216 # If not then display a `useless-null-test` warning on node and return false.
218 fun check_can_be_null
(anode
: ANode, mtype
: MType): Bool
220 if mtype
isa MNullType then
221 modelbuilder
.warning
(anode
, "useless-null-test", "Warning: expression is always `null`.")
224 if can_be_null
(mtype
) then return true
226 if mtype
isa MFormalType then
227 var res
= anchor_to
(mtype
)
228 modelbuilder
.warning
(anode
, "useless-null-test", "Warning: expression is not null, since it is a `{mtype}: {res}`.")
230 modelbuilder
.warning
(anode
, "useless-null-test", "Warning: expression is not null, since it is a `{mtype}`.")
235 # Special verification on != and == for null
237 fun null_test
(anode
: ABinopExpr)
239 var mtype
= anode
.n_expr
.mtype
240 var mtype2
= anode
.n_expr2
.mtype
242 if mtype
== null or mtype2
== null then return
244 if not mtype2
isa MNullType then return
246 # Check of useless null
247 if not can_be_null
(mtype
) then return
249 if mtype
isa MNullType then
250 # Because of type adaptation, we cannot just stop here
251 # so return use `null` as a bottom type that will be merged easily (cf) `merge_types`
254 mtype
= mtype
.as_notnull
257 # Check for type adaptation
258 var variable
= anode
.n_expr
.its_variable
259 if variable
== null then return
261 # One is null (mtype2 see above) the other is not null
262 if anode
isa AEqExpr then
263 anode
.after_flow_context
.when_true
.set_var
(self, variable
, mtype2
)
264 anode
.after_flow_context
.when_false
.set_var
(self, variable
, mtype
)
265 else if anode
isa ANeExpr then
266 anode
.after_flow_context
.when_false
.set_var
(self, variable
, mtype2
)
267 anode
.after_flow_context
.when_true
.set_var
(self, variable
, mtype
)
273 fun try_get_mproperty_by_name2
(anode
: ANode, mtype
: MType, name
: String): nullable MProperty
275 return self.modelbuilder
.try_get_mproperty_by_name2
(anode
, mmodule
, mtype
, name
)
278 fun resolve_mtype
(node
: AType): nullable MType
280 return self.modelbuilder
.resolve_mtype
(mmodule
, mclassdef
, node
)
283 fun try_get_mclass
(node
: ANode, name
: String): nullable MClass
285 var mclass
= modelbuilder
.try_get_mclass_by_name
(node
, mmodule
, name
)
289 fun get_mclass
(node
: ANode, name
: String): nullable MClass
291 var mclass
= modelbuilder
.get_mclass_by_name
(node
, mmodule
, name
)
295 fun type_bool
(node
: ANode): nullable MType
297 var mclass
= self.get_mclass
(node
, "Bool")
298 if mclass
== null then return null
299 return mclass
.mclass_type
302 fun get_method
(node
: ANode, recvtype
: MType, name
: String, recv_is_self
: Bool): nullable CallSite
304 var unsafe_type
= self.anchor_to
(recvtype
)
306 #debug("recv: {recvtype} (aka {unsafe_type})")
307 if recvtype
isa MNullType then
308 var objclass
= get_mclass
(node
, "Object")
309 if objclass
== null then return null # Forward error
310 unsafe_type
= objclass
.mclass_type
313 var mproperty
= self.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
314 if name
== "new" and mproperty
== null then
316 mproperty
= self.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
319 if mproperty
== null then
321 self.modelbuilder
.error
(node
, "Error: method or variable `{name}` unknown in `{recvtype}`.")
322 else if recvtype
.need_anchor
then
323 self.modelbuilder
.error
(node
, "Error: method `{name}` does not exists in `{recvtype}: {unsafe_type}`.")
325 self.modelbuilder
.error
(node
, "Error: method `{name}` does not exists in `{recvtype}`.")
330 assert mproperty
isa MMethod
332 # `null` only accepts some methods of object.
333 if recvtype
isa MNullType and not mproperty
.is_null_safe
then
334 self.error
(node
, "Error: method `{name}` called on `null`.")
336 else if unsafe_type
isa MNullableType and not mproperty
.is_null_safe
then
337 modelbuilder
.advice
(node
, "call-on-nullable", "Warning: method call on a nullable receiver `{recvtype}`.")
340 if is_toplevel_context
and recv_is_self
and not mproperty
.is_toplevel
then
341 error
(node
, "Error: `{name}` is not a top-level method, thus need a receiver.")
343 if not recv_is_self
and mproperty
.is_toplevel
then
344 error
(node
, "Error: cannot call `{name}`, a top-level method, with a receiver.")
347 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
348 self.modelbuilder
.error
(node
, "Error: method `{name}` is protected and can only accessed by `self`.")
352 var info
= mproperty
.deprecation
353 if info
!= null and self.mpropdef
.mproperty
.deprecation
== null then
356 self.modelbuilder
.warning
(node
, "deprecated-method", "Deprecation Warning: method `{name}` is deprecated: {mdoc.content.first}")
358 self.modelbuilder
.warning
(node
, "deprecated-method", "Deprecation Warning: method `{name}` is deprecated.")
362 var propdefs
= mproperty
.lookup_definitions
(self.mmodule
, unsafe_type
)
364 if propdefs
.length
== 0 then
365 self.modelbuilder
.error
(node
, "Type Error: no definition found for property `{name}` in `{unsafe_type}`.")
367 else if propdefs
.length
== 1 then
368 mpropdef
= propdefs
.first
370 self.modelbuilder
.warning
(node
, "property-conflict", "Warning: conflicting property definitions for property `{name}` in `{unsafe_type}`: {propdefs.join(" ")}")
371 mpropdef
= mproperty
.intro
375 var msignature
= mpropdef
.new_msignature
or else mpropdef
.msignature
376 if msignature
== null then return null # skip error
377 msignature
= resolve_for
(msignature
, recvtype
, recv_is_self
).as(MSignature)
379 var erasure_cast
= false
380 var rettype
= mpropdef
.msignature
.return_mtype
381 if not recv_is_self
and rettype
!= null then
382 rettype
= rettype
.undecorate
383 if rettype
isa MParameterType then
384 var erased_rettype
= msignature
.return_mtype
385 assert erased_rettype
!= null
386 #node.debug("Erasure cast: Really a {rettype} but unsafely a {erased_rettype}")
391 var callsite
= new CallSite(node
, recvtype
, mmodule
, anchor
, recv_is_self
, mproperty
, mpropdef
, msignature
, erasure_cast
)
395 fun try_get_method
(node
: ANode, recvtype
: MType, name
: String, recv_is_self
: Bool): nullable CallSite
397 var unsafe_type
= self.anchor_to
(recvtype
)
398 var mproperty
= self.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
399 if mproperty
== null then return null
400 return get_method
(node
, recvtype
, name
, recv_is_self
)
404 # Visit the expressions of args and check their conformity with the corresponding type in signature
405 # The point of this method is to handle varargs correctly
406 # Note: The signature must be correctly adapted
407 fun check_signature
(node
: ANode, args
: Array[AExpr], mproperty
: MProperty, msignature
: MSignature): nullable SignatureMap
409 var vararg_rank
= msignature
.vararg_rank
410 if vararg_rank
>= 0 then
411 if args
.length
< msignature
.arity
then
412 modelbuilder
.error
(node
, "Error: expected at least {msignature.arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
415 else if args
.length
!= msignature
.arity
then
416 if msignature
.arity
== msignature
.min_arity
then
417 modelbuilder
.error
(node
, "Error: expected {msignature.arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
420 if args
.length
> msignature
.arity
then
421 modelbuilder
.error
(node
, "Error: expected at most {msignature.arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
424 if args
.length
< msignature
.min_arity
then
425 modelbuilder
.error
(node
, "Error: expected at least {msignature.min_arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
430 #debug("CALL {unsafe_type}.{msignature}")
432 # Associate each parameter to a position in the arguments
433 var map
= new SignatureMap
435 var setted
= args
.length
- msignature
.min_arity
437 # First, handle named arguments
438 for i
in [0..args
.length
[ do
440 if not e
isa ANamedargExpr then continue
441 var name
= e
.n_id
.text
442 var param
= msignature
.mparameter_by_name
(name
)
443 if param
== null then
444 modelbuilder
.error
(e
.n_id
, "Error: no parameter `{name}` for `{mproperty}{msignature}`.")
447 if not param
.is_default
then
448 modelbuilder
.error
(e
, "Error: parameter `{name}` is not optional for `{mproperty}{msignature}`.")
451 var idx
= msignature
.mparameters
.index_of
(param
)
452 var prev
= map
.map
.get_or_null
(idx
)
454 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 # Second, associate remaining parameters
463 var vararg_decl
= args
.length
- msignature
.arity
465 for i
in [0..msignature
.arity
[ do
466 # Skip parameters associated by name
467 if map
.map
.has_key
(i
) then continue
469 var param
= msignature
.mparameters
[i
]
470 if param
.is_default
then
478 # Search the next free argument: skip named arguments since they are already associated
479 while args
[j
] isa ANamedargExpr do j
+= 1
484 if i
== vararg_rank
then
486 continue # skip the vararg
489 var paramtype
= param
.mtype
490 self.visit_expr_subtype
(arg
, paramtype
)
493 # Third, check varargs
494 if vararg_rank
>= 0 then
495 var paramtype
= msignature
.mparameters
[vararg_rank
].mtype
496 var first
= args
[vararg_rank
]
497 if vararg_decl
== 0 then
498 var mclass
= get_mclass
(node
, "Array")
499 if mclass
== null then return null # Forward error
500 var array_mtype
= mclass
.get_mtype
([paramtype
])
501 if first
isa AVarargExpr then
502 self.visit_expr_subtype
(first
.n_expr
, array_mtype
)
503 first
.mtype
= first
.n_expr
.mtype
505 # only one vararg, maybe `...` was forgot, so be gentle!
506 var t
= visit_expr
(first
)
507 if t
== null then return null # Forward error
508 if not is_subtype
(t
, paramtype
) and is_subtype
(t
, array_mtype
) then
509 # Not acceptable but could be a `...`
510 error
(first
, "Type Error: expected `{paramtype}`, got `{t}`. Is an ellipsis `...` missing on the argument?")
513 # Standard valid vararg, finish the job
515 self.visit_expr_subtype
(first
, paramtype
)
518 map
.vararg_decl
= vararg_decl
+ 1
519 for i
in [vararg_rank
..vararg_rank
+vararg_decl
] do
520 self.visit_expr_subtype
(args
[i
], paramtype
)
528 fun error
(node
: ANode, message
: String)
530 self.modelbuilder
.toolcontext
.error
(node
.hot_location
, message
)
533 fun get_variable
(node
: AExpr, variable
: Variable): nullable MType
535 if not variable
.is_adapted
then return variable
.declared_type
537 var flow
= node
.after_flow_context
538 if flow
== null then return null # skip error
540 if flow
.vars
.has_key
(variable
) then
541 return flow
.vars
[variable
]
543 #node.debug("*** START Collected for {variable}")
544 var mtypes
= flow
.collect_types
(variable
)
545 #node.debug("**** END Collected for {variable}")
546 if mtypes
.length
== 0 then
547 return variable
.declared_type
548 else if mtypes
.length
== 1 then
551 var res
= merge_types
(node
,mtypes
)
552 if res
== null then res
= variable
.declared_type
558 # Some variables where type-adapted during the visit
561 # Some loops had been visited during the visit
564 fun set_variable
(node
: AExpr, variable
: Variable, mtype
: nullable MType)
566 var flow
= node
.after_flow_context
569 flow
.set_var
(self, variable
, mtype
)
572 fun merge_types
(node
: ANode, col
: Array[nullable MType]): nullable MType
574 if col
.length
== 1 then return col
.first
576 if t1
== null then continue # return null
579 if t2
== null then continue # return null
580 if can_be_null
(t2
) and not can_be_null
(t1
) then
583 if not is_subtype
(t2
, t1
) then found
= false
586 #print "merge {col.join(" ")} -> {t1}"
590 #self.modelbuilder.warning(node, "Type Error: {col.length} conflicting types: <{col.join(", ")}>")
595 # Mapping between parameters and arguments in a call.
597 # Parameters and arguments are not stored in the class but referenced by their position (starting from 0)
599 # The point of this class is to help engine and other things to map arguments in the AST to parameters of the model.
601 # Associate a parameter to an argument
602 var map
= new ArrayMap[Int, Int]
604 # The length of the vararg sequence
605 # 0 if no vararg or if reverse vararg (cf `AVarargExpr`)
606 var vararg_decl
: Int = 0
609 # A specific method call site with its associated informations.
611 # The associated node for location
614 # The static type of the receiver (possibly unresolved)
617 # The module where the callsite is present
620 # The anchor to use with `recv` or `msignature`
621 var anchor
: nullable MClassType
623 # Is the receiver self?
624 # If "for_self", virtual types of the signature are kept
625 # If "not_for_self", virtual type are erased
626 var recv_is_self
: Bool
628 # The designated method
629 var mproperty
: MMethod
631 # The statically designated method definition
632 # The most specif one, it is.
633 var mpropdef
: MMethodDef
635 # The resolved signature for the receiver
636 var msignature
: MSignature
638 # Is a implicit cast required on erasure typing policy?
639 var erasure_cast
: Bool
641 # The mapping used on the call to associate arguments to parameters
642 # If null then no specific association is required.
643 var signaturemap
: nullable SignatureMap = null
645 private fun check_signature
(v
: TypeVisitor, args
: Array[AExpr]): Bool
647 var map
= v
.check_signature
(self.node
, args
, self.mproperty
, self.msignature
)
654 # The declared type of the variable
655 var declared_type
: nullable MType is writable
657 # Was the variable type-adapted?
658 # This is used to speedup type retrieval while it remains `false`
659 private var is_adapted
= false
662 redef class FlowContext
663 # Store changes of types because of type evolution
664 private var vars
= new HashMap[Variable, nullable MType]
666 # Adapt the variable to a static type
667 # Warning1: do not modify vars directly.
668 # Warning2: sub-flow may have cached a unadapted variable
669 private fun set_var
(v
: TypeVisitor, variable
: Variable, mtype
: nullable MType)
671 if variable
.declared_type
== mtype
and not variable
.is_adapted
then return
672 if vars
.has_key
(variable
) and vars
[variable
] == mtype
then return
673 self.vars
[variable
] = mtype
675 variable
.is_adapted
= true
676 #node.debug "set {variable} to {mtype or else "X"}"
679 # Look in the flow and previous flow and collect all first reachable type adaptation of a local variable
680 private fun collect_types
(variable
: Variable): Array[nullable MType]
682 #node.debug "flow for {variable}"
683 var res
= new Array[nullable MType]
686 var seen
= new HashSet[FlowContext]
687 while not todo
.is_empty
do
689 if f
.is_unreachable
then continue
690 if seen
.has
(f
) then continue
693 if f
.vars
.has_key
(variable
) then
694 # Found something. Collect it and do not process further on this path
695 res
.add f
.vars
[variable
]
696 #f.node.debug "process {variable}: got {f.vars[variable] or else "X"}"
698 todo
.add_all f
.previous
700 if f
.previous
.is_empty
then
701 # Root flowcontext mean a parameter or something related
702 res
.add variable
.declared_type
703 #f.node.debug "root process {variable}: got {variable.declared_type or else "X"}"
707 #self.node.debug "##### end flow for {variable}: {res.join(" ")}"
713 # The entry point of the whole typing analysis
714 fun do_typing
(modelbuilder
: ModelBuilder)
718 # The variable associated to the receiver (if any)
719 var selfvariable
: nullable Variable
722 redef class AMethPropdef
723 redef fun do_typing
(modelbuilder
: ModelBuilder)
725 var mpropdef
= self.mpropdef
726 if mpropdef
== null then return # skip error
728 var v
= new TypeVisitor(modelbuilder
, mpropdef
.mclassdef
.mmodule
, mpropdef
)
729 self.selfvariable
= v
.selfvariable
731 var mmethoddef
= self.mpropdef
.as(not null)
732 var msignature
= mmethoddef
.msignature
733 if msignature
== null then return # skip error
734 for i
in [0..msignature
.arity
[ do
735 var mtype
= msignature
.mparameters
[i
].mtype
736 if msignature
.vararg_rank
== i
then
737 var arrayclass
= v
.get_mclass
(self.n_signature
.n_params
[i
], "Array")
738 if arrayclass
== null then return # Skip error
739 mtype
= arrayclass
.get_mtype
([mtype
])
741 var variable
= self.n_signature
.n_params
[i
].variable
742 assert variable
!= null
743 variable
.declared_type
= mtype
746 var nblock
= self.n_block
747 if nblock
== null then return
752 if not v
.has_loop
or not v
.dirty
then break
755 var post_visitor
= new PostTypingVisitor(v
)
756 post_visitor
.enter_visit
(self)
758 if not nblock
.after_flow_context
.is_unreachable
and msignature
.return_mtype
!= null then
759 # We reach the end of the function without having a return, it is bad
760 v
.error
(self, "Error: reached end of function; expected `return` with a value.")
765 private class PostTypingVisitor
767 var type_visitor
: TypeVisitor
768 redef fun visit
(n
) do
770 n
.accept_post_typing
(type_visitor
)
775 private fun accept_post_typing
(v
: TypeVisitor) do end
778 redef class AAttrPropdef
779 redef fun do_typing
(modelbuilder
: ModelBuilder)
781 if not has_value
then return
783 var mpropdef
= self.mreadpropdef
784 if mpropdef
== null or mpropdef
.msignature
== null then return # skip error
786 var v
= new TypeVisitor(modelbuilder
, mpropdef
.mclassdef
.mmodule
, mpropdef
)
787 self.selfvariable
= v
.selfvariable
789 var nexpr
= self.n_expr
790 if nexpr
!= null then
791 var mtype
= self.mtype
792 v
.visit_expr_subtype
(nexpr
, mtype
)
794 var nblock
= self.n_block
795 if nblock
!= null then
797 if not nblock
.after_flow_context
.is_unreachable
then
798 # We reach the end of the init without having a return, it is bad
799 v
.error
(self, "Error: reached end of block; expected `return`.")
808 # The static type of the expression.
809 # null if self is a statement or in case of error
810 var mtype
: nullable MType = null
812 # Is the statement correctly typed?
813 # Used to distinguish errors and statements when `mtype == null`
814 var is_typed
: Bool = false
816 # If required, the following implicit cast `.as(XXX)`
817 # Such a cast may by required after evaluating the expression when
818 # a unsafe operation is detected (silently accepted by the Nit language).
819 # The attribute is computed by `check_subtype`
820 var implicit_cast_to
: nullable MType = null
822 # Return the variable read (if any)
823 # Used to perform adaptive typing
824 fun its_variable
: nullable Variable do return null
826 private fun accept_typing
(v
: TypeVisitor)
828 v
.error
(self, "no implemented accept_typing for {self.class_name}")
831 # Is non-null if `self` is a leaf of a comprehension array construction.
832 # In this case, the enclosing literal array node is designated.
833 # The result of the evaluation of `self` must be
834 # stored inside the designated array (there is an implicit `push`)
835 var comprehension
: nullable AArrayExpr = null
838 redef class ABlockExpr
839 redef fun accept_typing
(v
)
841 for e
in self.n_expr
do v
.visit_stmt
(e
)
845 # The type of a blockexpr is the one of the last expression (or null if empty)
848 if self.n_expr
.is_empty
then return null
849 return self.n_expr
.last
.mtype
853 redef class AVardeclExpr
854 redef fun accept_typing
(v
)
856 var variable
= self.variable
857 if variable
== null then return # Skip error
859 var ntype
= self.n_type
860 var mtype
: nullable MType
861 if ntype
== null then
864 mtype
= v
.resolve_mtype
(ntype
)
865 if mtype
== null then return # Skip error
868 var nexpr
= self.n_expr
869 if nexpr
!= null then
870 if mtype
!= null then
871 var etype
= v
.visit_expr_subtype
(nexpr
, mtype
)
872 if etype
== mtype
then
874 v
.modelbuilder
.advice
(ntype
, "useless-type", "Warning: useless type definition for variable `{variable.name}`")
877 mtype
= v
.visit_expr
(nexpr
)
878 if mtype
== null then return # Skip error
883 if mtype
== null or mtype
isa MNullType then
884 var objclass
= v
.get_mclass
(self, "Object")
885 if objclass
== null then return # skip error
886 decltype
= objclass
.mclass_type
.as_nullable
887 if mtype
== null then mtype
= decltype
890 variable
.declared_type
= decltype
891 v
.set_variable
(self, variable
, mtype
)
893 #debug("var {variable}: {mtype}")
901 redef fun its_variable
do return self.variable
902 redef fun accept_typing
(v
)
904 var variable
= self.variable
905 if variable
== null then return # Skip error
907 var mtype
= v
.get_variable
(self, variable
)
908 if mtype
!= null then
909 #debug("{variable} is {mtype}")
911 #debug("{variable} is untyped")
918 redef class AVarAssignExpr
919 redef fun accept_typing
(v
)
921 var variable
= self.variable
922 assert variable
!= null
924 var mtype
= v
.visit_expr_subtype
(n_value
, variable
.declared_type
)
926 v
.set_variable
(self, variable
, mtype
)
932 redef class AReassignFormExpr
933 # The method designed by the reassign operator.
934 var reassign_callsite
: nullable CallSite
936 var read_type
: nullable MType = null
938 # Determine the `reassign_property`
939 # `readtype` is the type of the reading of the left value.
940 # `writetype` is the type of the writing of the left value.
941 # (Because of `ACallReassignExpr`, both can be different.
942 # Return the static type of the value to store.
943 private fun resolve_reassignment
(v
: TypeVisitor, readtype
, writetype
: MType): nullable MType
945 var reassign_name
= self.n_assign_op
.operator
947 self.read_type
= readtype
949 var callsite
= v
.get_method
(self.n_assign_op
, readtype
, reassign_name
, false)
950 if callsite
== null then return null # Skip error
951 self.reassign_callsite
= callsite
953 var msignature
= callsite
.msignature
954 var rettype
= msignature
.return_mtype
955 assert msignature
.arity
== 1 and rettype
!= null
957 var value_type
= v
.visit_expr_subtype
(self.n_value
, msignature
.mparameters
.first
.mtype
)
958 if value_type
== null then return null # Skip error
960 v
.check_subtype
(self, rettype
, writetype
, false)
965 redef class AVarReassignExpr
966 redef fun accept_typing
(v
)
968 var variable
= self.variable
969 assert variable
!= null
971 var readtype
= v
.get_variable
(self, variable
)
972 if readtype
== null then return
976 var writetype
= variable
.declared_type
977 if writetype
== null then return
979 var rettype
= self.resolve_reassignment
(v
, readtype
, writetype
)
981 v
.set_variable
(self, variable
, rettype
)
983 self.is_typed
= rettype
!= null
988 redef class AContinueExpr
989 redef fun accept_typing
(v
)
991 var nexpr
= self.n_expr
992 if nexpr
!= null then
999 redef class ABreakExpr
1000 redef fun accept_typing
(v
)
1002 var nexpr
= self.n_expr
1003 if nexpr
!= null then
1006 self.is_typed
= true
1010 redef class AReturnExpr
1011 redef fun accept_typing
(v
)
1013 var nexpr
= self.n_expr
1015 var mpropdef
= v
.mpropdef
1016 if mpropdef
isa MMethodDef then
1017 ret_type
= mpropdef
.msignature
.return_mtype
1018 else if mpropdef
isa MAttributeDef then
1019 ret_type
= mpropdef
.static_mtype
1023 if nexpr
!= null then
1024 if ret_type
!= null then
1025 v
.visit_expr_subtype
(nexpr
, ret_type
)
1028 v
.error
(nexpr
, "Error: `return` with value in a procedure.")
1031 else if ret_type
!= null then
1032 v
.error
(self, "Error: `return` without value in a function.")
1035 self.is_typed
= true
1039 redef class AAbortExpr
1040 redef fun accept_typing
(v
)
1042 self.is_typed
= true
1047 redef fun accept_typing
(v
)
1049 v
.visit_expr_bool
(n_expr
)
1051 v
.visit_stmt
(n_then
)
1052 v
.visit_stmt
(n_else
)
1054 self.is_typed
= true
1056 if n_then
!= null and n_else
== null then
1057 self.mtype
= n_then
.mtype
1062 redef class AIfexprExpr
1063 redef fun accept_typing
(v
)
1065 v
.visit_expr_bool
(n_expr
)
1067 var t1
= v
.visit_expr
(n_then
)
1068 var t2
= v
.visit_expr
(n_else
)
1070 if t1
== null or t2
== null then
1074 var t
= v
.merge_types
(self, [t1
, t2
])
1076 v
.error
(self, "Type Error: ambiguous type `{t1}` vs `{t2}`.")
1083 redef fun accept_typing
(v
)
1085 v
.visit_stmt
(n_block
)
1086 self.is_typed
= true
1090 redef class AWhileExpr
1091 redef fun accept_typing
(v
)
1094 v
.visit_expr_bool
(n_expr
)
1095 v
.visit_stmt
(n_block
)
1096 self.is_typed
= true
1100 redef class ALoopExpr
1101 redef fun accept_typing
(v
)
1104 v
.visit_stmt
(n_block
)
1105 self.is_typed
= true
1109 redef class AForExpr
1110 var coltype
: nullable MClassType
1112 var method_iterator
: nullable CallSite
1113 var method_is_ok
: nullable CallSite
1114 var method_item
: nullable CallSite
1115 var method_next
: nullable CallSite
1116 var method_key
: nullable CallSite
1117 var method_finish
: nullable CallSite
1119 var method_lt
: nullable CallSite
1120 var method_successor
: nullable CallSite
1122 private fun do_type_iterator
(v
: TypeVisitor, mtype
: MType)
1124 if mtype
isa MNullType then
1125 v
.error
(self, "Type Error: `for` cannot iterate over `null`.")
1130 var objcla
= v
.get_mclass
(self, "Object")
1131 if objcla
== null then return
1133 # check iterator method
1134 var itdef
= v
.get_method
(self, mtype
, "iterator", n_expr
isa ASelfExpr)
1135 if itdef
== null then
1136 v
.error
(self, "Type Error: `for` expects a type providing an `iterator` method, got `{mtype}`.")
1139 self.method_iterator
= itdef
1141 # check that iterator return something
1142 var ittype
= itdef
.msignature
.return_mtype
1143 if ittype
== null then
1144 v
.error
(self, "Type Error: `for` expects the method `iterator` to return an `Iterator` or `MapIterator` type.")
1149 var colit_cla
= v
.try_get_mclass
(self, "Iterator")
1150 var mapit_cla
= v
.try_get_mclass
(self, "MapIterator")
1154 if colit_cla
!= null and v
.is_subtype
(ittype
, colit_cla
.get_mtype
([objcla
.mclass_type
.as_nullable
])) then
1156 var coltype
= ittype
.supertype_to
(v
.mmodule
, v
.anchor
, colit_cla
)
1157 var variables
= self.variables
1158 if variables
.length
!= 1 then
1159 v
.error
(self, "Type Error: `for` expects only one variable when using `Iterator`.")
1161 variables
.first
.declared_type
= coltype
.arguments
.first
1166 if mapit_cla
!= null and v
.is_subtype
(ittype
, mapit_cla
.get_mtype
([objcla
.mclass_type
.as_nullable
, objcla
.mclass_type
.as_nullable
])) then
1168 var coltype
= ittype
.supertype_to
(v
.mmodule
, v
.anchor
, mapit_cla
)
1169 var variables
= self.variables
1170 if variables
.length
!= 2 then
1171 v
.error
(self, "Type Error: `for` expects two variables when using `MapIterator`.")
1173 variables
[0].declared_type
= coltype
.arguments
[0]
1174 variables
[1].declared_type
= coltype
.arguments
[1]
1179 if not is_col
and not is_map
then
1180 v
.error
(self, "Type Error: `for` expects the method `iterator` to return an `Iterator` or `MapIterator` type.")
1184 # anchor formal and virtual types
1185 if mtype
.need_anchor
then mtype
= v
.anchor_to
(mtype
)
1187 mtype
= mtype
.undecorate
1188 self.coltype
= mtype
.as(MClassType)
1190 # get methods is_ok, next, item
1191 var ikdef
= v
.get_method
(self, ittype
, "is_ok", false)
1192 if ikdef
== null then
1193 v
.error
(self, "Type Error: `for` expects a method `is_ok` in type `{ittype}`.")
1196 self.method_is_ok
= ikdef
1198 var itemdef
= v
.get_method
(self, ittype
, "item", false)
1199 if itemdef
== null then
1200 v
.error
(self, "Type Error: `for` expects a method `item` in type `{ittype}`.")
1203 self.method_item
= itemdef
1205 var nextdef
= v
.get_method
(self, ittype
, "next", false)
1206 if nextdef
== null then
1207 v
.error
(self, "Type Error: `for` expects a method `next` in type {ittype}.")
1210 self.method_next
= nextdef
1212 self.method_finish
= v
.try_get_method
(self, ittype
, "finish", false)
1215 var keydef
= v
.get_method
(self, ittype
, "key", false)
1216 if keydef
== null then
1217 v
.error
(self, "Type Error: `for` expects a method `key` in type `{ittype}`.")
1220 self.method_key
= keydef
1223 if self.variables
.length
== 1 and n_expr
isa ARangeExpr then
1224 var variable
= variables
.first
1225 var vtype
= variable
.declared_type
.as(not null)
1227 if n_expr
isa AOrangeExpr then
1228 self.method_lt
= v
.get_method
(self, vtype
, "<", false)
1230 self.method_lt
= v
.get_method
(self, vtype
, "<=", false)
1233 self.method_successor
= v
.get_method
(self, vtype
, "successor", false)
1237 redef fun accept_typing
(v
)
1240 var mtype
= v
.visit_expr
(n_expr
)
1241 if mtype
== null then return
1243 self.do_type_iterator
(v
, mtype
)
1245 v
.visit_stmt
(n_block
)
1247 self.mtype
= n_block
.mtype
1248 self.is_typed
= true
1252 redef class AWithExpr
1253 var method_start
: nullable CallSite
1254 var method_finish
: nullable CallSite
1256 redef fun accept_typing
(v
: TypeVisitor)
1258 var mtype
= v
.visit_expr
(n_expr
)
1259 if mtype
== null then return
1261 method_start
= v
.get_method
(self, mtype
, "start", n_expr
isa ASelfExpr)
1262 method_finish
= v
.get_method
(self, mtype
, "finish", n_expr
isa ASelfExpr)
1264 v
.visit_stmt
(n_block
)
1265 self.mtype
= n_block
.mtype
1266 self.is_typed
= true
1270 redef class AAssertExpr
1271 redef fun accept_typing
(v
)
1273 v
.visit_expr_bool
(n_expr
)
1275 v
.visit_stmt
(n_else
)
1276 self.is_typed
= true
1281 redef fun accept_typing
(v
)
1283 v
.visit_expr_bool
(n_expr
)
1284 v
.visit_expr_bool
(n_expr2
)
1285 self.mtype
= v
.type_bool
(self)
1289 redef class AImpliesExpr
1290 redef fun accept_typing
(v
)
1292 v
.visit_expr_bool
(n_expr
)
1293 v
.visit_expr_bool
(n_expr2
)
1294 self.mtype
= v
.type_bool
(self)
1298 redef class AAndExpr
1299 redef fun accept_typing
(v
)
1301 v
.visit_expr_bool
(n_expr
)
1302 v
.visit_expr_bool
(n_expr2
)
1303 self.mtype
= v
.type_bool
(self)
1308 redef class ANotExpr
1309 redef fun accept_typing
(v
)
1311 v
.visit_expr_bool
(n_expr
)
1312 self.mtype
= v
.type_bool
(self)
1316 redef class AOrElseExpr
1317 redef fun accept_typing
(v
)
1319 var t1
= v
.visit_expr
(n_expr
)
1320 var t2
= v
.visit_expr
(n_expr2
)
1322 if t1
== null or t2
== null then
1326 if t1
isa MNullType then
1329 else if v
.can_be_null
(t1
) then
1333 var t
= v
.merge_types
(self, [t1
, t2
])
1335 var c
= v
.get_mclass
(self, "Object")
1336 if c
== null then return # forward error
1338 if v
.can_be_null
(t2
) then
1341 #v.error(self, "Type Error: ambiguous type {t1} vs {t2}")
1346 redef fun accept_post_typing
(v
)
1348 var t1
= n_expr
.mtype
1352 v
.check_can_be_null
(n_expr
, t1
)
1357 redef class ATrueExpr
1358 redef fun accept_typing
(v
)
1360 self.mtype
= v
.type_bool
(self)
1364 redef class AFalseExpr
1365 redef fun accept_typing
(v
)
1367 self.mtype
= v
.type_bool
(self)
1371 redef class AIntExpr
1372 redef fun accept_typing
(v
)
1374 var mclass
= v
.get_mclass
(self, "Int")
1375 if mclass
== null then return # Forward error
1376 self.mtype
= mclass
.mclass_type
1380 redef class AByteExpr
1381 redef fun accept_typing
(v
)
1383 var mclass
= v
.get_mclass
(self, "Byte")
1384 if mclass
== null then return # Forward error
1385 self.mtype
= mclass
.mclass_type
1389 redef class AFloatExpr
1390 redef fun accept_typing
(v
)
1392 var mclass
= v
.get_mclass
(self, "Float")
1393 if mclass
== null then return # Forward error
1394 self.mtype
= mclass
.mclass_type
1398 redef class ACharExpr
1399 redef fun accept_typing
(v
)
1401 var mclass
= v
.get_mclass
(self, "Char")
1402 if mclass
== null then return # Forward error
1403 self.mtype
= mclass
.mclass_type
1407 redef class AStringFormExpr
1408 redef fun accept_typing
(v
)
1410 var mclass
= v
.get_mclass
(self, "String")
1411 if mclass
== null then return # Forward error
1412 self.mtype
= mclass
.mclass_type
1416 redef class ASuperstringExpr
1417 redef fun accept_typing
(v
)
1419 var mclass
= v
.get_mclass
(self, "String")
1420 if mclass
== null then return # Forward error
1421 self.mtype
= mclass
.mclass_type
1422 var objclass
= v
.get_mclass
(self, "Object")
1423 if objclass
== null then return # Forward error
1424 var objtype
= objclass
.mclass_type
1425 for nexpr
in self.n_exprs
do
1426 v
.visit_expr_subtype
(nexpr
, objtype
)
1431 redef class AArrayExpr
1432 # The `with_capacity` method on Array
1433 var with_capacity_callsite
: nullable CallSite
1435 # The `push` method on arrays
1436 var push_callsite
: nullable CallSite
1438 # The element of each type
1439 var element_mtype
: nullable MType
1441 # Set that `self` is a part of comprehension array `na`
1442 # If `self` is a `for`, or a `if`, then `set_comprehension` is recursively applied.
1443 private fun set_comprehension
(n
: nullable AExpr)
1447 else if n
isa AForExpr then
1448 set_comprehension
(n
.n_block
)
1449 else if n
isa AIfExpr then
1450 set_comprehension
(n
.n_then
)
1451 set_comprehension
(n
.n_else
)
1454 n
.comprehension
= self
1457 redef fun accept_typing
(v
)
1459 var mtype
: nullable MType = null
1460 var ntype
= self.n_type
1461 if ntype
!= null then
1462 mtype
= v
.resolve_mtype
(ntype
)
1463 if mtype
== null then return # Skip error
1465 var mtypes
= new Array[nullable MType]
1467 for e
in self.n_exprs
do
1468 var t
= v
.visit_expr
(e
)
1472 set_comprehension
(e
)
1473 if mtype
!= null then
1474 if v
.check_subtype
(e
, t
, mtype
, false) == null then return # Forward error
1475 if t
== mtype
then useless
= true
1480 if mtype
== null then
1481 # Ensure monotony for type adaptation on loops
1482 if self.element_mtype
!= null then mtypes
.add
self.element_mtype
1483 mtype
= v
.merge_types
(self, mtypes
)
1485 if mtype
== null or mtype
isa MNullType then
1486 v
.error
(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
1490 assert ntype
!= null
1491 v
.modelbuilder
.warning
(ntype
, "useless-type", "Warning: useless type declaration `{mtype}` in literal Array since it can be inferred from the elements type.")
1494 self.element_mtype
= mtype
1496 var mclass
= v
.get_mclass
(self, "Array")
1497 if mclass
== null then return # Forward error
1498 var array_mtype
= mclass
.get_mtype
([mtype
])
1500 with_capacity_callsite
= v
.get_method
(self, array_mtype
, "with_capacity", false)
1501 push_callsite
= v
.get_method
(self, array_mtype
, "push", false)
1503 self.mtype
= array_mtype
1507 redef class ARangeExpr
1508 var init_callsite
: nullable CallSite
1510 redef fun accept_typing
(v
)
1512 var discrete_class
= v
.get_mclass
(self, "Discrete")
1513 if discrete_class
== null then return # Forward error
1514 var discrete_type
= discrete_class
.intro
.bound_mtype
1515 var t1
= v
.visit_expr_subtype
(self.n_expr
, discrete_type
)
1516 var t2
= v
.visit_expr_subtype
(self.n_expr2
, discrete_type
)
1517 if t1
== null or t2
== null then return
1518 var mclass
= v
.get_mclass
(self, "Range")
1519 if mclass
== null then return # Forward error
1521 if v
.is_subtype
(t1
, t2
) then
1522 mtype
= mclass
.get_mtype
([t2
])
1523 else if v
.is_subtype
(t2
, t1
) then
1524 mtype
= mclass
.get_mtype
([t1
])
1526 v
.error
(self, "Type Error: cannot create range: `{t1}` vs `{t2}`.")
1532 # get the constructor
1534 if self isa ACrangeExpr then
1535 callsite
= v
.get_method
(self, mtype
, "init", false)
1536 else if self isa AOrangeExpr then
1537 callsite
= v
.get_method
(self, mtype
, "without_last", false)
1541 init_callsite
= callsite
1545 redef class ANullExpr
1546 redef fun accept_typing
(v
)
1548 self.mtype
= v
.mmodule
.model
.null_type
1552 redef class AIsaExpr
1553 # The static type to cast to.
1554 # (different from the static type of the expression that is `Bool`).
1555 var cast_type
: nullable MType
1556 redef fun accept_typing
(v
)
1558 v
.visit_expr
(n_expr
)
1560 var mtype
= v
.resolve_mtype
(n_type
)
1562 self.cast_type
= mtype
1564 var variable
= self.n_expr
.its_variable
1565 if variable
!= null then
1566 #var orig = self.n_expr.mtype
1567 #var from = if orig != null then orig.to_s else "invalid"
1568 #var to = if mtype != null then mtype.to_s else "invalid"
1569 #debug("adapt {variable}: {from} -> {to}")
1570 self.after_flow_context
.when_true
.set_var
(v
, variable
, mtype
)
1573 self.mtype
= v
.type_bool
(self)
1576 redef fun accept_post_typing
(v
)
1578 v
.check_expr_cast
(self, self.n_expr
, self.n_type
)
1582 redef class AAsCastExpr
1583 redef fun accept_typing
(v
)
1585 v
.visit_expr
(n_expr
)
1587 self.mtype
= v
.resolve_mtype
(n_type
)
1590 redef fun accept_post_typing
(v
)
1592 v
.check_expr_cast
(self, self.n_expr
, self.n_type
)
1596 redef class AAsNotnullExpr
1597 redef fun accept_typing
(v
)
1599 var mtype
= v
.visit_expr
(self.n_expr
)
1600 if mtype
== null then return # Forward error
1602 if mtype
isa MNullType then
1603 v
.error
(self, "Type Error: `as(not null)` on `null`.")
1607 if v
.can_be_null
(mtype
) then
1608 mtype
= mtype
.as_notnull
1614 redef fun accept_post_typing
(v
)
1616 var mtype
= n_expr
.mtype
1617 if mtype
== null then return
1618 v
.check_can_be_null
(n_expr
, mtype
)
1622 redef class AParExpr
1623 redef fun accept_typing
(v
)
1625 self.mtype
= v
.visit_expr
(self.n_expr
)
1629 redef class AOnceExpr
1630 redef fun accept_typing
(v
)
1632 self.mtype
= v
.visit_expr
(self.n_expr
)
1636 redef class ASelfExpr
1637 redef var its_variable
: nullable Variable
1638 redef fun accept_typing
(v
)
1640 if v
.is_toplevel_context
and not self isa AImplicitSelfExpr then
1641 v
.error
(self, "Error: `self` cannot be used in top-level method.")
1643 var variable
= v
.selfvariable
1644 self.its_variable
= variable
1645 self.mtype
= v
.get_variable
(self, variable
)
1649 redef class AImplicitSelfExpr
1650 # Is the implicit receiver `sys`?
1652 # By default, the implicit receiver is `self`.
1653 # But when there is not method for `self`, `sys` is used as a fall-back.
1654 # Is this case this flag is set to `true`.
1658 ## MESSAGE SENDING AND PROPERTY
1660 redef class ASendExpr
1661 # The property invoked by the send.
1662 var callsite
: nullable CallSite
1664 redef fun accept_typing
(v
)
1666 var nrecv
= self.n_expr
1667 var recvtype
= v
.visit_expr
(nrecv
)
1668 var name
= self.property_name
1669 var node
= self.property_node
1671 if recvtype
== null then return # Forward error
1674 var unsafe_type
= v
.anchor_to
(recvtype
)
1675 var mproperty
= v
.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
1676 if mproperty
== null and nrecv
isa AImplicitSelfExpr then
1677 # Special fall-back search in `sys` when noting found in the implicit receiver.
1678 var sysclass
= v
.try_get_mclass
(node
, "Sys")
1679 if sysclass
!= null then
1680 var systype
= sysclass
.mclass_type
1681 mproperty
= v
.try_get_mproperty_by_name2
(node
, systype
, name
)
1682 if mproperty
!= null then
1683 callsite
= v
.get_method
(node
, systype
, name
, false)
1684 if callsite
== null then return # Forward error
1685 # Update information, we are looking at `sys` now, not `self`
1687 nrecv
.its_variable
= null
1688 nrecv
.mtype
= systype
1693 if callsite
== null then
1694 # If still nothing, just exit
1695 callsite
= v
.get_method
(node
, recvtype
, name
, nrecv
isa ASelfExpr)
1696 if callsite
== null then return
1699 self.callsite
= callsite
1700 var msignature
= callsite
.msignature
1702 var args
= compute_raw_arguments
1704 callsite
.check_signature
(v
, args
)
1706 if callsite
.mproperty
.is_init
then
1707 var vmpropdef
= v
.mpropdef
1708 if not (vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_init
) then
1709 v
.error
(node
, "Error: an `init` can only be called from another `init`.")
1711 if vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_root_init
and not callsite
.mproperty
.is_root_init
then
1712 v
.error
(node
, "Error: `{vmpropdef}` cannot call a factory `{callsite.mproperty}`.")
1716 var ret
= msignature
.return_mtype
1720 self.is_typed
= true
1724 # The name of the property
1725 # Each subclass simply provide the correct name.
1726 private fun property_name
: String is abstract
1728 # The node identifying the name (id, operator, etc) for messages.
1730 # Is `self` by default
1731 private fun property_node
: ANode do return self
1733 # An array of all arguments (excluding self)
1734 fun raw_arguments
: Array[AExpr] do return compute_raw_arguments
1736 private fun compute_raw_arguments
: Array[AExpr] is abstract
1739 redef class ABinopExpr
1740 redef fun compute_raw_arguments
do return [n_expr2
]
1741 redef fun property_name
do return operator
1742 redef fun property_node
do return n_op
1745 redef class AEqFormExpr
1746 redef fun accept_typing
(v
)
1752 redef fun accept_post_typing
(v
)
1754 var mtype
= n_expr
.mtype
1755 var mtype2
= n_expr2
.mtype
1757 if mtype
== null or mtype2
== null then return
1759 if not mtype2
isa MNullType then return
1761 v
.check_can_be_null
(n_expr
, mtype
)
1765 redef class AUnaryopExpr
1766 redef fun property_name
do return "unary {operator}"
1767 redef fun compute_raw_arguments
do return new Array[AExpr]
1771 redef class ACallExpr
1772 redef fun property_name
do return n_id
.text
1773 redef fun property_node
do return n_id
1774 redef fun compute_raw_arguments
do return n_args
.to_a
1777 redef class ACallAssignExpr
1778 redef fun property_name
do return n_id
.text
+ "="
1779 redef fun property_node
do return n_id
1780 redef fun compute_raw_arguments
1782 var res
= n_args
.to_a
1788 redef class ABraExpr
1789 redef fun property_name
do return "[]"
1790 redef fun compute_raw_arguments
do return n_args
.to_a
1793 redef class ABraAssignExpr
1794 redef fun property_name
do return "[]="
1795 redef fun compute_raw_arguments
1797 var res
= n_args
.to_a
1803 redef class ASendReassignFormExpr
1804 # The property invoked for the writing
1805 var write_callsite
: nullable CallSite
1807 redef fun accept_typing
(v
)
1809 var recvtype
= v
.visit_expr
(self.n_expr
)
1810 var name
= self.property_name
1811 var node
= self.property_node
1813 if recvtype
== null then return # Forward error
1815 var for_self
= self.n_expr
isa ASelfExpr
1816 var callsite
= v
.get_method
(node
, recvtype
, name
, for_self
)
1818 if callsite
== null then return
1819 self.callsite
= callsite
1821 var args
= compute_raw_arguments
1823 callsite
.check_signature
(v
, args
)
1825 var readtype
= callsite
.msignature
.return_mtype
1826 if readtype
== null then
1827 v
.error
(node
, "Error: `{name}` is not a function.")
1831 var wcallsite
= v
.get_method
(node
, recvtype
, name
+ "=", self.n_expr
isa ASelfExpr)
1832 if wcallsite
== null then return
1833 self.write_callsite
= wcallsite
1835 var wtype
= self.resolve_reassignment
(v
, readtype
, wcallsite
.msignature
.mparameters
.last
.mtype
)
1836 if wtype
== null then return
1838 args
= args
.to_a
# duplicate so raw_arguments keeps only the getter args
1839 args
.add
(self.n_value
)
1840 wcallsite
.check_signature
(v
, args
)
1842 self.is_typed
= true
1846 redef class ACallReassignExpr
1847 redef fun property_name
do return n_id
.text
1848 redef fun property_node
do return n_id
1849 redef fun compute_raw_arguments
do return n_args
.to_a
1852 redef class ABraReassignExpr
1853 redef fun property_name
do return "[]"
1854 redef fun compute_raw_arguments
do return n_args
.to_a
1857 redef class AInitExpr
1858 redef fun property_name
do return "init"
1859 redef fun property_node
do return n_kwinit
1860 redef fun compute_raw_arguments
do return n_args
.to_a
1864 fun to_a
: Array[AExpr] do return self.n_exprs
.to_a
1869 redef class ASuperExpr
1870 # The method to call if the super is in fact a 'super init call'
1871 # Note: if the super is a normal call-next-method, then this attribute is null
1872 var callsite
: nullable CallSite
1874 # The method to call is the super is a standard `call-next-method` super-call
1875 # Note: if the super is a special super-init-call, then this attribute is null
1876 var mpropdef
: nullable MMethodDef
1878 redef fun accept_typing
(v
)
1880 var anchor
= v
.anchor
1881 assert anchor
!= null
1882 var recvtype
= v
.get_variable
(self, v
.selfvariable
)
1883 assert recvtype
!= null
1884 var mproperty
= v
.mpropdef
.mproperty
1885 if not mproperty
isa MMethod then
1886 v
.error
(self, "Error: `super` only usable in a `method`.")
1889 var superprops
= mproperty
.lookup_super_definitions
(v
.mmodule
, anchor
)
1890 if superprops
.length
== 0 then
1891 if mproperty
.is_init
and v
.mpropdef
.is_intro
then
1892 process_superinit
(v
)
1895 v
.error
(self, "Error: no super method to call for `{mproperty}`.")
1898 # FIXME: covariance of return type in linear extension?
1899 var superprop
= superprops
.first
1901 var msignature
= superprop
.msignature
.as(not null)
1902 msignature
= v
.resolve_for
(msignature
, recvtype
, true).as(MSignature)
1903 var args
= self.n_args
.to_a
1904 if args
.length
> 0 then
1905 signaturemap
= v
.check_signature
(self, args
, mproperty
, msignature
)
1907 self.mtype
= msignature
.return_mtype
1908 self.is_typed
= true
1909 v
.mpropdef
.has_supercall
= true
1910 mpropdef
= v
.mpropdef
.as(MMethodDef)
1913 # The mapping used on the call to associate arguments to parameters.
1914 # If null then no specific association is required.
1915 var signaturemap
: nullable SignatureMap
1917 private fun process_superinit
(v
: TypeVisitor)
1919 var anchor
= v
.anchor
1920 assert anchor
!= null
1921 var recvtype
= v
.get_variable
(self, v
.selfvariable
)
1922 assert recvtype
!= null
1923 var mpropdef
= v
.mpropdef
1924 assert mpropdef
isa MMethodDef
1925 var mproperty
= mpropdef
.mproperty
1926 var superprop
: nullable MMethodDef = null
1927 for msupertype
in mpropdef
.mclassdef
.supertypes
do
1928 msupertype
= msupertype
.anchor_to
(v
.mmodule
, anchor
)
1929 var errcount
= v
.modelbuilder
.toolcontext
.error_count
1930 var candidate
= v
.try_get_mproperty_by_name2
(self, msupertype
, mproperty
.name
).as(nullable MMethod)
1931 if candidate
== null then
1932 if v
.modelbuilder
.toolcontext
.error_count
> errcount
then return # Forward error
1933 continue # Try next super-class
1935 if superprop
!= null and candidate
.is_root_init
then
1938 if superprop
!= null and superprop
.mproperty
!= candidate
and not superprop
.mproperty
.is_root_init
then
1939 v
.error
(self, "Error: conflicting super constructor to call for `{mproperty}`: `{candidate.full_name}`, `{superprop.mproperty.full_name}`")
1942 var candidatedefs
= candidate
.lookup_definitions
(v
.mmodule
, anchor
)
1943 if superprop
!= null and superprop
.mproperty
== candidate
then
1944 if superprop
== candidatedefs
.first
then continue
1945 candidatedefs
.add
(superprop
)
1947 if candidatedefs
.length
> 1 then
1948 v
.error
(self, "Error: conflicting property definitions for property `{mproperty}` in `{recvtype}`: {candidatedefs.join(", ")}")
1951 superprop
= candidatedefs
.first
1953 if superprop
== null then
1954 v
.error
(self, "Error: no super method to call for `{mproperty}`.")
1958 var msignature
= superprop
.new_msignature
or else superprop
.msignature
.as(not null)
1959 msignature
= v
.resolve_for
(msignature
, recvtype
, true).as(MSignature)
1961 var callsite
= new CallSite(self, recvtype
, v
.mmodule
, v
.anchor
, true, superprop
.mproperty
, superprop
, msignature
, false)
1962 self.callsite
= callsite
1964 var args
= self.n_args
.to_a
1965 if args
.length
> 0 then
1966 callsite
.check_signature
(v
, args
)
1968 # Check there is at least enough parameters
1969 if mpropdef
.msignature
.arity
< msignature
.arity
then
1970 v
.error
(self, "Error: not enough implicit arguments to pass. Got `{mpropdef.msignature.arity}`, expected at least `{msignature.arity}`. Signature is `{msignature}`.")
1973 # Check that each needed parameter is conform
1975 for sp
in msignature
.mparameters
do
1976 var p
= mpropdef
.msignature
.mparameters
[i
]
1977 if not v
.is_subtype
(p
.mtype
, sp
.mtype
) then
1978 v
.error
(self, "Type Error: expected argument #{i} of type `{sp.mtype}`, got implicit argument `{p.name}` of type `{p.mtype}`. Signature is {msignature}")
1985 self.is_typed
= true
1991 redef class ANewExpr
1992 # The constructor invoked by the new.
1993 var callsite
: nullable CallSite
1995 # The designated type
1996 var recvtype
: nullable MClassType
1998 redef fun accept_typing
(v
)
2000 var recvtype
= v
.resolve_mtype
(self.n_type
)
2001 if recvtype
== null then return
2003 if not recvtype
isa MClassType then
2004 if recvtype
isa MNullableType then
2005 v
.error
(self, "Type Error: cannot instantiate the nullable type `{recvtype}`.")
2007 else if recvtype
isa MFormalType then
2008 v
.error
(self, "Type Error: cannot instantiate the formal type `{recvtype}`.")
2011 v
.error
(self, "Type Error: cannot instantiate the type `{recvtype}`.")
2016 self.recvtype
= recvtype
2017 var kind
= recvtype
.mclass
.kind
2029 if name
== "intern" then
2030 if kind
!= concrete_kind
then
2031 v
.error
(self, "Type Error: cannot instantiate {kind} {recvtype}.")
2034 if n_args
.n_exprs
.not_empty
then
2035 v
.error
(n_args
, "Type Error: the intern constructor expects no arguments.")
2039 self.mtype
= recvtype
2043 var callsite
= v
.get_method
(node
, recvtype
, name
, false)
2044 if callsite
== null then return
2046 if not callsite
.mproperty
.is_new
then
2047 if kind
!= concrete_kind
then
2048 v
.error
(self, "Type Error: cannot instantiate {kind} `{recvtype}`.")
2051 self.mtype
= recvtype
2053 self.mtype
= callsite
.msignature
.return_mtype
2054 assert self.mtype
!= null
2057 self.callsite
= callsite
2059 if not callsite
.mproperty
.is_init_for
(recvtype
.mclass
) then
2060 v
.error
(self, "Error: `{name}` is not a constructor.")
2064 var args
= n_args
.to_a
2065 callsite
.check_signature
(v
, args
)
2071 redef class AAttrFormExpr
2072 # The attribute accessed.
2073 var mproperty
: nullable MAttribute
2075 # The static type of the attribute.
2076 var attr_type
: nullable MType
2078 # Resolve the attribute accessed.
2079 private fun resolve_property
(v
: TypeVisitor)
2081 var recvtype
= v
.visit_expr
(self.n_expr
)
2082 if recvtype
== null then return # Skip error
2083 var node
= self.n_id
2084 var name
= node
.text
2085 if recvtype
isa MNullType then
2086 v
.error
(node
, "Error: attribute `{name}` access on `null`.")
2090 var unsafe_type
= v
.anchor_to
(recvtype
)
2091 var mproperty
= v
.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
2092 if mproperty
== null then
2093 v
.modelbuilder
.error
(node
, "Error: attribute `{name}` does not exist in `{recvtype}`.")
2096 assert mproperty
isa MAttribute
2097 self.mproperty
= mproperty
2099 var mpropdefs
= mproperty
.lookup_definitions
(v
.mmodule
, unsafe_type
)
2100 assert mpropdefs
.length
== 1
2101 var mpropdef
= mpropdefs
.first
2102 var attr_type
= mpropdef
.static_mtype
2103 if attr_type
== null then return # skip error
2104 attr_type
= v
.resolve_for
(attr_type
, recvtype
, self.n_expr
isa ASelfExpr)
2105 self.attr_type
= attr_type
2109 redef class AAttrExpr
2110 redef fun accept_typing
(v
)
2112 self.resolve_property
(v
)
2113 self.mtype
= self.attr_type
2118 redef class AAttrAssignExpr
2119 redef fun accept_typing
(v
)
2121 self.resolve_property
(v
)
2122 var mtype
= self.attr_type
2124 v
.visit_expr_subtype
(self.n_value
, mtype
)
2125 self.is_typed
= mtype
!= null
2129 redef class AAttrReassignExpr
2130 redef fun accept_typing
(v
)
2132 self.resolve_property
(v
)
2133 var mtype
= self.attr_type
2134 if mtype
== null then return # Skip error
2136 var rettype
= self.resolve_reassignment
(v
, mtype
, mtype
)
2138 self.is_typed
= rettype
!= null
2142 redef class AIssetAttrExpr
2143 redef fun accept_typing
(v
)
2145 self.resolve_property
(v
)
2146 var mtype
= self.attr_type
2147 if mtype
== null then return # Skip error
2149 var recvtype
= self.n_expr
.mtype
.as(not null)
2150 var bound
= v
.resolve_for
(mtype
, recvtype
, false)
2151 if bound
isa MNullableType then
2152 v
.error
(n_id
, "Type Error: `isset` on a nullable attribute.")
2154 self.mtype
= v
.type_bool
(self)
2158 redef class AVarargExpr
2159 redef fun accept_typing
(v
)
2161 # This kind of pseudo-expression can be only processed trough a signature
2162 # See `check_signature`
2163 # Other cases are a syntax error.
2164 v
.error
(self, "Syntax Error: unexpected `...`.")
2170 redef class ADebugTypeExpr
2171 redef fun accept_typing
(v
)
2173 var expr
= v
.visit_expr
(self.n_expr
)
2174 if expr
== null then return
2175 var unsafe
= v
.anchor_to
(expr
)
2176 var ntype
= self.n_type
2177 var mtype
= v
.resolve_mtype
(ntype
)
2178 if mtype
!= null and mtype
!= expr
then
2179 var umtype
= v
.anchor_to
(mtype
)
2180 v
.modelbuilder
.warning
(self, "debug", "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")
2182 self.is_typed
= true