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
85 if anchor
== null then
86 assert not mtype
.need_anchor
89 return mtype
.anchor_to
(mmodule
, anchor
)
92 fun is_subtype
(sub
, sup
: MType): Bool
94 return sub
.is_subtype
(mmodule
, anchor
, sup
)
97 fun resolve_for
(mtype
, subtype
: MType, for_self
: Bool): MType
99 #print "resolve_for {mtype} sub={subtype} forself={for_self} mmodule={mmodule} anchor={anchor}"
100 var res
= mtype
.resolve_for
(subtype
, anchor
, mmodule
, not for_self
)
104 # Check that `sub` is a subtype of `sup`.
105 # If `sub` is not a valid suptype, then display an error on `node` an return null.
106 # If `sub` is a safe subtype of `sup` then return `sub`.
107 # If `sub` is an unsafe subtype (ie an implicit cast is required), then return `sup`.
109 # The point of the return type is to determinate the usable type on an expression when `autocast` is true:
110 # If the suptype is safe, then the return type is the one on the expression typed by `sub`.
111 # Is the subtype is unsafe, then the return type is the one of an implicit cast on `sup`.
112 fun check_subtype
(node
: ANode, sub
, sup
: MType, autocast
: Bool): nullable MType
114 if self.is_subtype
(sub
, sup
) then return sub
115 if autocast
and self.is_subtype
(sub
, self.anchor_to
(sup
)) then
116 # FIXME workaround to the current unsafe typing policy. To remove once fixed virtual types exists.
117 #node.debug("Unsafe typing: expected {sup}, got {sub}")
120 if sub
.need_anchor
then
121 var u
= anchor_to
(sub
)
122 self.modelbuilder
.error
(node
, "Type Error: expected `{sup}`, got `{sub}: {u}`.")
124 self.modelbuilder
.error
(node
, "Type Error: expected `{sup}`, got `{sub}`.")
129 # Visit an expression and do not care about the return value
130 fun visit_stmt
(nexpr
: nullable AExpr)
132 if nexpr
== null then return
133 nexpr
.accept_typing
(self)
136 # Visit an expression and expects that it is not a statement
137 # Return the type of the expression
138 # Display an error and return null if:
139 # * the type cannot be determined or
140 # * `nexpr` is a statement
141 fun visit_expr
(nexpr
: AExpr): nullable MType
143 nexpr
.accept_typing
(self)
144 var mtype
= nexpr
.mtype
145 if mtype
!= null then return mtype
146 if not nexpr
.is_typed
then
147 if not self.modelbuilder
.toolcontext
.error_count
> 0 then # check that there is really an error
148 if self.modelbuilder
.toolcontext
.verbose_level
> 1 then
149 nexpr
.debug
("No return type but no error.")
152 return null # forward error
154 self.error
(nexpr
, "Error: expected an expression.")
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
, 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
417 if msignature
.arity
== msignature
.min_arity
then
418 modelbuilder
.error
(node
, "Error: expected {msignature.arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
421 if args
.length
> msignature
.arity
then
422 modelbuilder
.error
(node
, "Error: expected at most {msignature.arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
425 if args
.length
< msignature
.min_arity
then
426 modelbuilder
.error
(node
, "Error: expected at least {msignature.min_arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
431 #debug("CALL {unsafe_type}.{msignature}")
433 # Associate each parameter to a position in the arguments
434 var map
= new SignatureMap
436 var setted
= args
.length
- msignature
.min_arity
438 # First, handle named arguments
439 for i
in [0..args
.length
[ do
441 if not e
isa ANamedargExpr then continue
442 var name
= e
.n_id
.text
443 var param
= msignature
.mparameter_by_name
(name
)
444 if param
== null then
445 modelbuilder
.error
(e
.n_id
, "Error: no parameter `{name}` for `{mproperty}{msignature}`.")
448 if not param
.is_default
then
449 modelbuilder
.error
(e
, "Error: parameter `{name}` is not optional 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}`.")
460 e
.mtype
= self.visit_expr_subtype
(e
.n_expr
, param
.mtype
)
463 # Second, associate remaining parameters
464 var vararg_decl
= args
.length
- msignature
.arity
466 for i
in [0..msignature
.arity
[ do
467 # Skip parameters associated by name
468 if map
.map
.has_key
(i
) then continue
470 var param
= msignature
.mparameters
[i
]
471 if param
.is_default
then
479 # Search the next free argument: skip named arguments since they are already associated
480 while args
[j
] isa ANamedargExpr do j
+= 1
485 if i
== vararg_rank
then
487 continue # skip the vararg
490 var paramtype
= param
.mtype
491 self.visit_expr_subtype
(arg
, paramtype
)
494 # Third, check varargs
495 if vararg_rank
>= 0 then
496 var paramtype
= msignature
.mparameters
[vararg_rank
].mtype
497 var first
= args
[vararg_rank
]
498 if vararg_decl
== 0 then
499 var mclass
= get_mclass
(node
, "Array")
500 if mclass
== null then return null # Forward error
501 var array_mtype
= mclass
.get_mtype
([paramtype
])
502 if first
isa AVarargExpr then
503 self.visit_expr_subtype
(first
.n_expr
, array_mtype
)
504 first
.mtype
= first
.n_expr
.mtype
506 # only one vararg, maybe `...` was forgot, so be gentle!
507 var t
= visit_expr
(first
)
508 if t
== null then return null # Forward error
509 if not is_subtype
(t
, paramtype
) and is_subtype
(t
, array_mtype
) then
510 # Not acceptable but could be a `...`
511 error
(first
, "Type Error: expected `{paramtype}`, got `{t}`. Is an ellipsis `...` missing on the argument?")
514 # Standard valid vararg, finish the job
516 self.visit_expr_subtype
(first
, paramtype
)
519 map
.vararg_decl
= vararg_decl
+ 1
520 for i
in [vararg_rank
..vararg_rank
+vararg_decl
] do
521 self.visit_expr_subtype
(args
[i
], paramtype
)
529 fun error
(node
: ANode, message
: String)
531 self.modelbuilder
.toolcontext
.error
(node
.hot_location
, message
)
534 fun get_variable
(node
: AExpr, variable
: Variable): nullable MType
536 if not variable
.is_adapted
then return variable
.declared_type
538 var flow
= node
.after_flow_context
539 if flow
== null then return null # skip error
541 if flow
.vars
.has_key
(variable
) then
542 return flow
.vars
[variable
]
544 #node.debug("*** START Collected for {variable}")
545 var mtypes
= flow
.collect_types
(variable
)
546 #node.debug("**** END Collected for {variable}")
547 if mtypes
.length
== 0 then
548 return variable
.declared_type
549 else if mtypes
.length
== 1 then
552 var res
= merge_types
(node
,mtypes
)
553 if res
== null then res
= variable
.declared_type
559 # Some variables where type-adapted during the visit
562 # Some loops had been visited during the visit
565 fun set_variable
(node
: AExpr, variable
: Variable, mtype
: nullable MType)
567 var flow
= node
.after_flow_context
570 flow
.set_var
(self, variable
, mtype
)
573 fun merge_types
(node
: ANode, col
: Array[nullable MType]): nullable MType
575 if col
.length
== 1 then return col
.first
577 if t1
== null then continue # return null
580 if t2
== null then continue # return null
581 if can_be_null
(t2
) and not can_be_null
(t1
) then
584 if not is_subtype
(t2
, t1
) then found
= false
587 #print "merge {col.join(" ")} -> {t1}"
591 #self.modelbuilder.warning(node, "Type Error: {col.length} conflicting types: <{col.join(", ")}>")
596 # Mapping between parameters and arguments in a call.
598 # Parameters and arguments are not stored in the class but referenced by their position (starting from 0)
600 # The point of this class is to help engine and other things to map arguments in the AST to parameters of the model.
602 # Associate a parameter to an argument
603 var map
= new ArrayMap[Int, Int]
605 # The length of the vararg sequence
606 # 0 if no vararg or if reverse vararg (cf `AVarargExpr`)
607 var vararg_decl
: Int = 0
610 # A specific method call site with its associated informations.
612 # The associated node for location
615 # The static type of the receiver (possibly unresolved)
618 # The module where the callsite is present
621 # The anchor to use with `recv` or `msignature`
622 var anchor
: nullable MClassType
624 # Is the receiver self?
625 # If "for_self", virtual types of the signature are kept
626 # If "not_for_self", virtual type are erased
627 var recv_is_self
: Bool
629 # The designated method
630 var mproperty
: MMethod
632 # The statically designated method definition
633 # The most specif one, it is.
634 var mpropdef
: MMethodDef
636 # The resolved signature for the receiver
637 var msignature
: MSignature
639 # Is a implicit cast required on erasure typing policy?
640 var erasure_cast
: Bool
642 # The mapping used on the call to associate arguments to parameters
643 # If null then no specific association is required.
644 var signaturemap
: nullable SignatureMap = null
646 private fun check_signature
(v
: TypeVisitor, args
: Array[AExpr]): Bool
648 var map
= v
.check_signature
(self.node
, args
, self.mproperty
, self.msignature
)
655 # The declared type of the variable
656 var declared_type
: nullable MType is writable
658 # Was the variable type-adapted?
659 # This is used to speedup type retrieval while it remains `false`
660 private var is_adapted
= false
663 redef class FlowContext
664 # Store changes of types because of type evolution
665 private var vars
= new HashMap[Variable, nullable MType]
667 # Adapt the variable to a static type
668 # Warning1: do not modify vars directly.
669 # Warning2: sub-flow may have cached a unadapted variable
670 private fun set_var
(v
: TypeVisitor, variable
: Variable, mtype
: nullable MType)
672 if variable
.declared_type
== mtype
and not variable
.is_adapted
then return
673 if vars
.has_key
(variable
) and vars
[variable
] == mtype
then return
674 self.vars
[variable
] = mtype
676 variable
.is_adapted
= true
677 #node.debug "set {variable} to {mtype or else "X"}"
680 # Look in the flow and previous flow and collect all first reachable type adaptation of a local variable
681 private fun collect_types
(variable
: Variable): Array[nullable MType]
683 #node.debug "flow for {variable}"
684 var res
= new Array[nullable MType]
687 var seen
= new HashSet[FlowContext]
688 while not todo
.is_empty
do
690 if f
.is_unreachable
then continue
691 if seen
.has
(f
) then continue
694 if f
.vars
.has_key
(variable
) then
695 # Found something. Collect it and do not process further on this path
696 res
.add f
.vars
[variable
]
697 #f.node.debug "process {variable}: got {f.vars[variable] or else "X"}"
699 todo
.add_all f
.previous
701 if f
.previous
.is_empty
then
702 # Root flowcontext mean a parameter or something related
703 res
.add variable
.declared_type
704 #f.node.debug "root process {variable}: got {variable.declared_type or else "X"}"
708 #self.node.debug "##### end flow for {variable}: {res.join(" ")}"
714 # The entry point of the whole typing analysis
715 fun do_typing
(modelbuilder
: ModelBuilder)
719 # The variable associated to the receiver (if any)
720 var selfvariable
: nullable Variable
723 redef class AMethPropdef
724 redef fun do_typing
(modelbuilder
: ModelBuilder)
726 var mpropdef
= self.mpropdef
727 if mpropdef
== null then return # skip error
729 var v
= new TypeVisitor(modelbuilder
, mpropdef
.mclassdef
.mmodule
, mpropdef
)
730 self.selfvariable
= v
.selfvariable
732 var mmethoddef
= self.mpropdef
.as(not null)
733 var msignature
= mmethoddef
.msignature
734 if msignature
== null then return # skip error
735 for i
in [0..msignature
.arity
[ do
736 var mtype
= msignature
.mparameters
[i
].mtype
737 if msignature
.vararg_rank
== i
then
738 var arrayclass
= v
.get_mclass
(self.n_signature
.n_params
[i
], "Array")
739 if arrayclass
== null then return # Skip error
740 mtype
= arrayclass
.get_mtype
([mtype
])
742 var variable
= self.n_signature
.n_params
[i
].variable
743 assert variable
!= null
744 variable
.declared_type
= mtype
747 var nblock
= self.n_block
748 if nblock
== null then return
753 if not v
.has_loop
or not v
.dirty
then break
756 var post_visitor
= new PostTypingVisitor(v
)
757 post_visitor
.enter_visit
(self)
759 if not nblock
.after_flow_context
.is_unreachable
and msignature
.return_mtype
!= null then
760 # We reach the end of the function without having a return, it is bad
761 v
.error
(self, "Error: reached end of function; expected `return` with a value.")
766 private class PostTypingVisitor
768 var type_visitor
: TypeVisitor
769 redef fun visit
(n
) do
771 n
.accept_post_typing
(type_visitor
)
776 private fun accept_post_typing
(v
: TypeVisitor) do end
779 redef class AAttrPropdef
780 redef fun do_typing
(modelbuilder
: ModelBuilder)
782 if not has_value
then return
784 var mpropdef
= self.mreadpropdef
785 if mpropdef
== null or mpropdef
.msignature
== null then return # skip error
787 var v
= new TypeVisitor(modelbuilder
, mpropdef
.mclassdef
.mmodule
, mpropdef
)
788 self.selfvariable
= v
.selfvariable
790 var nexpr
= self.n_expr
791 if nexpr
!= null then
792 var mtype
= self.mtype
793 v
.visit_expr_subtype
(nexpr
, mtype
)
795 var nblock
= self.n_block
796 if nblock
!= null then
798 if not nblock
.after_flow_context
.is_unreachable
then
799 # We reach the end of the init without having a return, it is bad
800 v
.error
(self, "Error: reached end of block; expected `return`.")
809 # The static type of the expression.
810 # null if self is a statement or in case of error
811 var mtype
: nullable MType = null
813 # Is the statement correctly typed?
814 # Used to distinguish errors and statements when `mtype == null`
815 var is_typed
: Bool = false
817 # If required, the following implicit cast `.as(XXX)`
818 # Such a cast may by required after evaluating the expression when
819 # a unsafe operation is detected (silently accepted by the Nit language).
820 # The attribute is computed by `check_subtype`
821 var implicit_cast_to
: nullable MType = null
823 # Return the variable read (if any)
824 # Used to perform adaptive typing
825 fun its_variable
: nullable Variable do return null
827 private fun accept_typing
(v
: TypeVisitor)
829 v
.error
(self, "no implemented accept_typing for {self.class_name}")
832 # Is non-null if `self` is a leaf of a comprehension array construction.
833 # In this case, the enclosing literal array node is designated.
834 # The result of the evaluation of `self` must be
835 # stored inside the designated array (there is an implicit `push`)
836 var comprehension
: nullable AArrayExpr = null
839 redef class ABlockExpr
840 redef fun accept_typing
(v
)
842 for e
in self.n_expr
do v
.visit_stmt
(e
)
846 # The type of a blockexpr is the one of the last expression (or null if empty)
849 if self.n_expr
.is_empty
then return null
850 return self.n_expr
.last
.mtype
854 redef class AVardeclExpr
855 redef fun accept_typing
(v
)
857 var variable
= self.variable
858 if variable
== null then return # Skip error
860 var ntype
= self.n_type
861 var mtype
: nullable MType
862 if ntype
== null then
865 mtype
= v
.resolve_mtype
(ntype
)
866 if mtype
== null then return # Skip error
869 var nexpr
= self.n_expr
870 if nexpr
!= null then
871 if mtype
!= null then
872 var etype
= v
.visit_expr_subtype
(nexpr
, mtype
)
873 if etype
== mtype
then
875 v
.modelbuilder
.advice
(ntype
, "useless-type", "Warning: useless type definition for variable `{variable.name}`")
878 mtype
= v
.visit_expr
(nexpr
)
879 if mtype
== null then return # Skip error
884 if mtype
== null or mtype
isa MNullType then
885 var objclass
= v
.get_mclass
(self, "Object")
886 if objclass
== null then return # skip error
887 decltype
= objclass
.mclass_type
.as_nullable
888 if mtype
== null then mtype
= decltype
891 variable
.declared_type
= decltype
892 v
.set_variable
(self, variable
, mtype
)
894 #debug("var {variable}: {mtype}")
902 redef fun its_variable
do return self.variable
903 redef fun accept_typing
(v
)
905 var variable
= self.variable
906 if variable
== null then return # Skip error
908 var mtype
= v
.get_variable
(self, variable
)
909 if mtype
!= null then
910 #debug("{variable} is {mtype}")
912 #debug("{variable} is untyped")
919 redef class AVarAssignExpr
920 redef fun accept_typing
(v
)
922 var variable
= self.variable
923 assert variable
!= null
925 var mtype
= v
.visit_expr_subtype
(n_value
, variable
.declared_type
)
927 v
.set_variable
(self, variable
, mtype
)
933 redef class AReassignFormExpr
934 # The method designed by the reassign operator.
935 var reassign_callsite
: nullable CallSite
937 var read_type
: nullable MType = null
939 # Determine the `reassign_property`
940 # `readtype` is the type of the reading of the left value.
941 # `writetype` is the type of the writing of the left value.
942 # (Because of `ACallReassignExpr`, both can be different.
943 # Return the static type of the value to store.
944 private fun resolve_reassignment
(v
: TypeVisitor, readtype
, writetype
: MType): nullable MType
946 var reassign_name
= self.n_assign_op
.operator
948 self.read_type
= readtype
950 var callsite
= v
.get_method
(self.n_assign_op
, readtype
, reassign_name
, false)
951 if callsite
== null then return null # Skip error
952 self.reassign_callsite
= callsite
954 var msignature
= callsite
.msignature
955 var rettype
= msignature
.return_mtype
956 assert msignature
.arity
== 1 and rettype
!= null
958 var value_type
= v
.visit_expr_subtype
(self.n_value
, msignature
.mparameters
.first
.mtype
)
959 if value_type
== null then return null # Skip error
961 v
.check_subtype
(self, rettype
, writetype
, false)
966 redef class AVarReassignExpr
967 redef fun accept_typing
(v
)
969 var variable
= self.variable
970 assert variable
!= null
972 var readtype
= v
.get_variable
(self, variable
)
973 if readtype
== null then return
977 var writetype
= variable
.declared_type
978 if writetype
== null then return
980 var rettype
= self.resolve_reassignment
(v
, readtype
, writetype
)
982 v
.set_variable
(self, variable
, rettype
)
984 self.is_typed
= rettype
!= null
989 redef class AContinueExpr
990 redef fun accept_typing
(v
)
992 var nexpr
= self.n_expr
993 if nexpr
!= null then
1000 redef class ABreakExpr
1001 redef fun accept_typing
(v
)
1003 var nexpr
= self.n_expr
1004 if nexpr
!= null then
1007 self.is_typed
= true
1011 redef class AReturnExpr
1012 redef fun accept_typing
(v
)
1014 var nexpr
= self.n_expr
1016 var mpropdef
= v
.mpropdef
1017 if mpropdef
isa MMethodDef then
1018 ret_type
= mpropdef
.msignature
.return_mtype
1019 else if mpropdef
isa MAttributeDef then
1020 ret_type
= mpropdef
.static_mtype
1024 if nexpr
!= null then
1025 if ret_type
!= null then
1026 v
.visit_expr_subtype
(nexpr
, ret_type
)
1029 v
.error
(nexpr
, "Error: `return` with value in a procedure.")
1032 else if ret_type
!= null then
1033 v
.error
(self, "Error: `return` without value in a function.")
1036 self.is_typed
= true
1040 redef class AAbortExpr
1041 redef fun accept_typing
(v
)
1043 self.is_typed
= true
1048 redef fun accept_typing
(v
)
1050 v
.visit_expr_bool
(n_expr
)
1052 v
.visit_stmt
(n_then
)
1053 v
.visit_stmt
(n_else
)
1055 self.is_typed
= true
1057 if n_then
!= null and n_else
== null then
1058 self.mtype
= n_then
.mtype
1063 redef class AIfexprExpr
1064 redef fun accept_typing
(v
)
1066 v
.visit_expr_bool
(n_expr
)
1068 var t1
= v
.visit_expr
(n_then
)
1069 var t2
= v
.visit_expr
(n_else
)
1071 if t1
== null or t2
== null then
1075 var t
= v
.merge_types
(self, [t1
, t2
])
1077 v
.error
(self, "Type Error: ambiguous type `{t1}` vs `{t2}`.")
1084 redef fun accept_typing
(v
)
1086 v
.visit_stmt
(n_block
)
1087 self.is_typed
= true
1091 redef class AWhileExpr
1092 redef fun accept_typing
(v
)
1095 v
.visit_expr_bool
(n_expr
)
1096 v
.visit_stmt
(n_block
)
1097 self.is_typed
= true
1101 redef class ALoopExpr
1102 redef fun accept_typing
(v
)
1105 v
.visit_stmt
(n_block
)
1106 self.is_typed
= true
1110 redef class AForExpr
1111 var coltype
: nullable MClassType
1113 var method_iterator
: nullable CallSite
1114 var method_is_ok
: nullable CallSite
1115 var method_item
: nullable CallSite
1116 var method_next
: nullable CallSite
1117 var method_key
: nullable CallSite
1118 var method_finish
: nullable CallSite
1120 var method_lt
: nullable CallSite
1121 var method_successor
: nullable CallSite
1123 private fun do_type_iterator
(v
: TypeVisitor, mtype
: MType)
1125 if mtype
isa MNullType then
1126 v
.error
(self, "Type Error: `for` cannot iterate over `null`.")
1131 var objcla
= v
.get_mclass
(self, "Object")
1132 if objcla
== null then return
1134 # check iterator method
1135 var itdef
= v
.get_method
(self, mtype
, "iterator", n_expr
isa ASelfExpr)
1136 if itdef
== null then
1137 v
.error
(self, "Type Error: `for` expects a type providing an `iterator` method, got `{mtype}`.")
1140 self.method_iterator
= itdef
1142 # check that iterator return something
1143 var ittype
= itdef
.msignature
.return_mtype
1144 if ittype
== null then
1145 v
.error
(self, "Type Error: `for` expects the method `iterator` to return an `Iterator` or `MapIterator` type.")
1150 var colit_cla
= v
.try_get_mclass
(self, "Iterator")
1151 var mapit_cla
= v
.try_get_mclass
(self, "MapIterator")
1155 if colit_cla
!= null and v
.is_subtype
(ittype
, colit_cla
.get_mtype
([objcla
.mclass_type
.as_nullable
])) then
1157 var coltype
= ittype
.supertype_to
(v
.mmodule
, v
.anchor
, colit_cla
)
1158 var variables
= self.variables
1159 if variables
.length
!= 1 then
1160 v
.error
(self, "Type Error: `for` expects only one variable when using `Iterator`.")
1162 variables
.first
.declared_type
= coltype
.arguments
.first
1167 if mapit_cla
!= null and v
.is_subtype
(ittype
, mapit_cla
.get_mtype
([objcla
.mclass_type
.as_nullable
, objcla
.mclass_type
.as_nullable
])) then
1169 var coltype
= ittype
.supertype_to
(v
.mmodule
, v
.anchor
, mapit_cla
)
1170 var variables
= self.variables
1171 if variables
.length
!= 2 then
1172 v
.error
(self, "Type Error: `for` expects two variables when using `MapIterator`.")
1174 variables
[0].declared_type
= coltype
.arguments
[0]
1175 variables
[1].declared_type
= coltype
.arguments
[1]
1180 if not is_col
and not is_map
then
1181 v
.error
(self, "Type Error: `for` expects the method `iterator` to return an `Iterator` or `MapIterator` type.")
1185 # anchor formal and virtual types
1186 if mtype
.need_anchor
then mtype
= v
.anchor_to
(mtype
)
1188 mtype
= mtype
.undecorate
1189 self.coltype
= mtype
.as(MClassType)
1191 # get methods is_ok, next, item
1192 var ikdef
= v
.get_method
(self, ittype
, "is_ok", false)
1193 if ikdef
== null then
1194 v
.error
(self, "Type Error: `for` expects a method `is_ok` in type `{ittype}`.")
1197 self.method_is_ok
= ikdef
1199 var itemdef
= v
.get_method
(self, ittype
, "item", false)
1200 if itemdef
== null then
1201 v
.error
(self, "Type Error: `for` expects a method `item` in type `{ittype}`.")
1204 self.method_item
= itemdef
1206 var nextdef
= v
.get_method
(self, ittype
, "next", false)
1207 if nextdef
== null then
1208 v
.error
(self, "Type Error: `for` expects a method `next` in type {ittype}.")
1211 self.method_next
= nextdef
1213 self.method_finish
= v
.try_get_method
(self, ittype
, "finish", false)
1216 var keydef
= v
.get_method
(self, ittype
, "key", false)
1217 if keydef
== null then
1218 v
.error
(self, "Type Error: `for` expects a method `key` in type `{ittype}`.")
1221 self.method_key
= keydef
1224 if self.variables
.length
== 1 and n_expr
isa ARangeExpr then
1225 var variable
= variables
.first
1226 var vtype
= variable
.declared_type
.as(not null)
1228 if n_expr
isa AOrangeExpr then
1229 self.method_lt
= v
.get_method
(self, vtype
, "<", false)
1231 self.method_lt
= v
.get_method
(self, vtype
, "<=", false)
1234 self.method_successor
= v
.get_method
(self, vtype
, "successor", false)
1238 redef fun accept_typing
(v
)
1241 var mtype
= v
.visit_expr
(n_expr
)
1242 if mtype
== null then return
1244 self.do_type_iterator
(v
, mtype
)
1246 v
.visit_stmt
(n_block
)
1248 self.mtype
= n_block
.mtype
1249 self.is_typed
= true
1253 redef class AWithExpr
1254 var method_start
: nullable CallSite
1255 var method_finish
: nullable CallSite
1257 redef fun accept_typing
(v
: TypeVisitor)
1259 var mtype
= v
.visit_expr
(n_expr
)
1260 if mtype
== null then return
1262 method_start
= v
.get_method
(self, mtype
, "start", n_expr
isa ASelfExpr)
1263 method_finish
= v
.get_method
(self, mtype
, "finish", n_expr
isa ASelfExpr)
1265 v
.visit_stmt
(n_block
)
1266 self.mtype
= n_block
.mtype
1267 self.is_typed
= true
1271 redef class AAssertExpr
1272 redef fun accept_typing
(v
)
1274 v
.visit_expr_bool
(n_expr
)
1276 v
.visit_stmt
(n_else
)
1277 self.is_typed
= true
1282 redef fun accept_typing
(v
)
1284 v
.visit_expr_bool
(n_expr
)
1285 v
.visit_expr_bool
(n_expr2
)
1286 self.mtype
= v
.type_bool
(self)
1290 redef class AImpliesExpr
1291 redef fun accept_typing
(v
)
1293 v
.visit_expr_bool
(n_expr
)
1294 v
.visit_expr_bool
(n_expr2
)
1295 self.mtype
= v
.type_bool
(self)
1299 redef class AAndExpr
1300 redef fun accept_typing
(v
)
1302 v
.visit_expr_bool
(n_expr
)
1303 v
.visit_expr_bool
(n_expr2
)
1304 self.mtype
= v
.type_bool
(self)
1309 redef class ANotExpr
1310 redef fun accept_typing
(v
)
1312 v
.visit_expr_bool
(n_expr
)
1313 self.mtype
= v
.type_bool
(self)
1317 redef class AOrElseExpr
1318 redef fun accept_typing
(v
)
1320 var t1
= v
.visit_expr
(n_expr
)
1321 var t2
= v
.visit_expr
(n_expr2
)
1323 if t1
== null or t2
== null then
1327 if t1
isa MNullType then
1330 else if v
.can_be_null
(t1
) then
1334 var t
= v
.merge_types
(self, [t1
, t2
])
1336 var c
= v
.get_mclass
(self, "Object")
1337 if c
== null then return # forward error
1339 if v
.can_be_null
(t2
) then
1342 #v.error(self, "Type Error: ambiguous type {t1} vs {t2}")
1347 redef fun accept_post_typing
(v
)
1349 var t1
= n_expr
.mtype
1353 v
.check_can_be_null
(n_expr
, t1
)
1358 redef class ATrueExpr
1359 redef fun accept_typing
(v
)
1361 self.mtype
= v
.type_bool
(self)
1365 redef class AFalseExpr
1366 redef fun accept_typing
(v
)
1368 self.mtype
= v
.type_bool
(self)
1372 redef class AIntegerExpr
1373 redef fun accept_typing
(v
)
1375 var mclass
: nullable MClass = null
1376 if value
isa Byte then
1377 mclass
= v
.get_mclass
(self, "Byte")
1378 else if value
isa Int then
1379 mclass
= v
.get_mclass
(self, "Int")
1381 if mclass
== null then return # Forward error
1382 self.mtype
= mclass
.mclass_type
1386 redef class AFloatExpr
1387 redef fun accept_typing
(v
)
1389 var mclass
= v
.get_mclass
(self, "Float")
1390 if mclass
== null then return # Forward error
1391 self.mtype
= mclass
.mclass_type
1395 redef class ACharExpr
1396 redef fun accept_typing
(v
)
1398 var mclass
= v
.get_mclass
(self, "Char")
1399 if mclass
== null then return # Forward error
1400 self.mtype
= mclass
.mclass_type
1404 redef class AStringFormExpr
1405 redef fun accept_typing
(v
)
1407 var mclass
= v
.get_mclass
(self, "String")
1408 if mclass
== null then return # Forward error
1409 self.mtype
= mclass
.mclass_type
1413 redef class ASuperstringExpr
1414 redef fun accept_typing
(v
)
1416 var mclass
= v
.get_mclass
(self, "String")
1417 if mclass
== null then return # Forward error
1418 self.mtype
= mclass
.mclass_type
1419 var objclass
= v
.get_mclass
(self, "Object")
1420 if objclass
== null then return # Forward error
1421 var objtype
= objclass
.mclass_type
1422 for nexpr
in self.n_exprs
do
1423 v
.visit_expr_subtype
(nexpr
, objtype
)
1428 redef class AArrayExpr
1429 # The `with_capacity` method on Array
1430 var with_capacity_callsite
: nullable CallSite
1432 # The `push` method on arrays
1433 var push_callsite
: nullable CallSite
1435 # The element of each type
1436 var element_mtype
: nullable MType
1438 # Set that `self` is a part of comprehension array `na`
1439 # If `self` is a `for`, or a `if`, then `set_comprehension` is recursively applied.
1440 private fun set_comprehension
(n
: nullable AExpr)
1444 else if n
isa AForExpr then
1445 set_comprehension
(n
.n_block
)
1446 else if n
isa AIfExpr then
1447 set_comprehension
(n
.n_then
)
1448 set_comprehension
(n
.n_else
)
1451 n
.comprehension
= self
1454 redef fun accept_typing
(v
)
1456 var mtype
: nullable MType = null
1457 var ntype
= self.n_type
1458 if ntype
!= null then
1459 mtype
= v
.resolve_mtype
(ntype
)
1460 if mtype
== null then return # Skip error
1462 var mtypes
= new Array[nullable MType]
1464 for e
in self.n_exprs
do
1465 var t
= v
.visit_expr
(e
)
1469 set_comprehension
(e
)
1470 if mtype
!= null then
1471 if v
.check_subtype
(e
, t
, mtype
, false) == null then return # Forward error
1472 if t
== mtype
then useless
= true
1477 if mtype
== null then
1478 # Ensure monotony for type adaptation on loops
1479 if self.element_mtype
!= null then mtypes
.add
self.element_mtype
1480 mtype
= v
.merge_types
(self, mtypes
)
1482 if mtype
== null or mtype
isa MNullType then
1483 v
.error
(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
1487 assert ntype
!= null
1488 v
.modelbuilder
.warning
(ntype
, "useless-type", "Warning: useless type declaration `{mtype}` in literal Array since it can be inferred from the elements type.")
1491 self.element_mtype
= mtype
1493 var mclass
= v
.get_mclass
(self, "Array")
1494 if mclass
== null then return # Forward error
1495 var array_mtype
= mclass
.get_mtype
([mtype
])
1497 with_capacity_callsite
= v
.get_method
(self, array_mtype
, "with_capacity", false)
1498 push_callsite
= v
.get_method
(self, array_mtype
, "push", false)
1500 self.mtype
= array_mtype
1504 redef class ARangeExpr
1505 var init_callsite
: nullable CallSite
1507 redef fun accept_typing
(v
)
1509 var discrete_class
= v
.get_mclass
(self, "Discrete")
1510 if discrete_class
== null then return # Forward error
1511 var discrete_type
= discrete_class
.intro
.bound_mtype
1512 var t1
= v
.visit_expr_subtype
(self.n_expr
, discrete_type
)
1513 var t2
= v
.visit_expr_subtype
(self.n_expr2
, discrete_type
)
1514 if t1
== null or t2
== null then return
1515 var mclass
= v
.get_mclass
(self, "Range")
1516 if mclass
== null then return # Forward error
1518 if v
.is_subtype
(t1
, t2
) then
1519 mtype
= mclass
.get_mtype
([t2
])
1520 else if v
.is_subtype
(t2
, t1
) then
1521 mtype
= mclass
.get_mtype
([t1
])
1523 v
.error
(self, "Type Error: cannot create range: `{t1}` vs `{t2}`.")
1529 # get the constructor
1531 if self isa ACrangeExpr then
1532 callsite
= v
.get_method
(self, mtype
, "init", false)
1533 else if self isa AOrangeExpr then
1534 callsite
= v
.get_method
(self, mtype
, "without_last", false)
1538 init_callsite
= callsite
1542 redef class ANullExpr
1543 redef fun accept_typing
(v
)
1545 self.mtype
= v
.mmodule
.model
.null_type
1549 redef class AIsaExpr
1550 # The static type to cast to.
1551 # (different from the static type of the expression that is `Bool`).
1552 var cast_type
: nullable MType
1553 redef fun accept_typing
(v
)
1555 v
.visit_expr
(n_expr
)
1557 var mtype
= v
.resolve_mtype
(n_type
)
1559 self.cast_type
= mtype
1561 var variable
= self.n_expr
.its_variable
1562 if variable
!= null then
1563 #var orig = self.n_expr.mtype
1564 #var from = if orig != null then orig.to_s else "invalid"
1565 #var to = if mtype != null then mtype.to_s else "invalid"
1566 #debug("adapt {variable}: {from} -> {to}")
1567 self.after_flow_context
.when_true
.set_var
(v
, variable
, mtype
)
1570 self.mtype
= v
.type_bool
(self)
1573 redef fun accept_post_typing
(v
)
1575 v
.check_expr_cast
(self, self.n_expr
, self.n_type
)
1579 redef class AAsCastExpr
1580 redef fun accept_typing
(v
)
1582 v
.visit_expr
(n_expr
)
1584 self.mtype
= v
.resolve_mtype
(n_type
)
1587 redef fun accept_post_typing
(v
)
1589 v
.check_expr_cast
(self, self.n_expr
, self.n_type
)
1593 redef class AAsNotnullExpr
1594 redef fun accept_typing
(v
)
1596 var mtype
= v
.visit_expr
(self.n_expr
)
1597 if mtype
== null then return # Forward error
1599 if mtype
isa MNullType then
1600 v
.error
(self, "Type Error: `as(not null)` on `null`.")
1604 if v
.can_be_null
(mtype
) then
1605 mtype
= mtype
.as_notnull
1611 redef fun accept_post_typing
(v
)
1613 var mtype
= n_expr
.mtype
1614 if mtype
== null then return
1615 v
.check_can_be_null
(n_expr
, mtype
)
1619 redef class AParExpr
1620 redef fun accept_typing
(v
)
1622 self.mtype
= v
.visit_expr
(self.n_expr
)
1626 redef class AOnceExpr
1627 redef fun accept_typing
(v
)
1629 self.mtype
= v
.visit_expr
(self.n_expr
)
1633 redef class ASelfExpr
1634 redef var its_variable
: nullable Variable
1635 redef fun accept_typing
(v
)
1637 if v
.is_toplevel_context
and not self isa AImplicitSelfExpr then
1638 v
.error
(self, "Error: `self` cannot be used in top-level method.")
1640 var variable
= v
.selfvariable
1641 self.its_variable
= variable
1642 self.mtype
= v
.get_variable
(self, variable
)
1646 redef class AImplicitSelfExpr
1647 # Is the implicit receiver `sys`?
1649 # By default, the implicit receiver is `self`.
1650 # But when there is not method for `self`, `sys` is used as a fall-back.
1651 # Is this case this flag is set to `true`.
1655 ## MESSAGE SENDING AND PROPERTY
1657 redef class ASendExpr
1658 # The property invoked by the send.
1659 var callsite
: nullable CallSite
1661 redef fun accept_typing
(v
)
1663 var nrecv
= self.n_expr
1664 var recvtype
= v
.visit_expr
(nrecv
)
1665 var name
= self.property_name
1666 var node
= self.property_node
1668 if recvtype
== null then return # Forward error
1671 var unsafe_type
= v
.anchor_to
(recvtype
)
1672 var mproperty
= v
.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
1673 if mproperty
== null and nrecv
isa AImplicitSelfExpr then
1674 # Special fall-back search in `sys` when noting found in the implicit receiver.
1675 var sysclass
= v
.try_get_mclass
(node
, "Sys")
1676 if sysclass
!= null then
1677 var systype
= sysclass
.mclass_type
1678 mproperty
= v
.try_get_mproperty_by_name2
(node
, systype
, name
)
1679 if mproperty
!= null then
1680 callsite
= v
.get_method
(node
, systype
, name
, false)
1681 if callsite
== null then return # Forward error
1682 # Update information, we are looking at `sys` now, not `self`
1684 nrecv
.its_variable
= null
1685 nrecv
.mtype
= systype
1690 if callsite
== null then
1691 # If still nothing, just exit
1692 callsite
= v
.get_method
(node
, recvtype
, name
, nrecv
isa ASelfExpr)
1693 if callsite
== null then return
1696 self.callsite
= callsite
1697 var msignature
= callsite
.msignature
1699 var args
= compute_raw_arguments
1701 callsite
.check_signature
(v
, args
)
1703 if callsite
.mproperty
.is_init
then
1704 var vmpropdef
= v
.mpropdef
1705 if not (vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_init
) then
1706 v
.error
(node
, "Error: an `init` can only be called from another `init`.")
1708 if vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_root_init
and not callsite
.mproperty
.is_root_init
then
1709 v
.error
(node
, "Error: `{vmpropdef}` cannot call a factory `{callsite.mproperty}`.")
1713 var ret
= msignature
.return_mtype
1717 self.is_typed
= true
1721 # The name of the property
1722 # Each subclass simply provide the correct name.
1723 private fun property_name
: String is abstract
1725 # The node identifying the name (id, operator, etc) for messages.
1727 # Is `self` by default
1728 private fun property_node
: ANode do return self
1730 # An array of all arguments (excluding self)
1731 fun raw_arguments
: Array[AExpr] do return compute_raw_arguments
1733 private fun compute_raw_arguments
: Array[AExpr] is abstract
1736 redef class ABinopExpr
1737 redef fun compute_raw_arguments
do return [n_expr2
]
1738 redef fun property_name
do return operator
1739 redef fun property_node
do return n_op
1742 redef class AEqFormExpr
1743 redef fun accept_typing
(v
)
1749 redef fun accept_post_typing
(v
)
1751 var mtype
= n_expr
.mtype
1752 var mtype2
= n_expr2
.mtype
1754 if mtype
== null or mtype2
== null then return
1756 if not mtype2
isa MNullType then return
1758 v
.check_can_be_null
(n_expr
, mtype
)
1762 redef class AUnaryopExpr
1763 redef fun property_name
do return "unary {operator}"
1764 redef fun compute_raw_arguments
do return new Array[AExpr]
1768 redef class ACallExpr
1769 redef fun property_name
do return n_qid
.n_id
.text
1770 redef fun property_node
do return n_qid
1771 redef fun compute_raw_arguments
do return n_args
.to_a
1774 redef class ACallAssignExpr
1775 redef fun property_name
do return n_qid
.n_id
.text
+ "="
1776 redef fun property_node
do return n_qid
1777 redef fun compute_raw_arguments
1779 var res
= n_args
.to_a
1785 redef class ABraExpr
1786 redef fun property_name
do return "[]"
1787 redef fun compute_raw_arguments
do return n_args
.to_a
1790 redef class ABraAssignExpr
1791 redef fun property_name
do return "[]="
1792 redef fun compute_raw_arguments
1794 var res
= n_args
.to_a
1800 redef class ASendReassignFormExpr
1801 # The property invoked for the writing
1802 var write_callsite
: nullable CallSite
1804 redef fun accept_typing
(v
)
1806 var recvtype
= v
.visit_expr
(self.n_expr
)
1807 var name
= self.property_name
1808 var node
= self.property_node
1810 if recvtype
== null then return # Forward error
1812 var for_self
= self.n_expr
isa ASelfExpr
1813 var callsite
= v
.get_method
(node
, recvtype
, name
, for_self
)
1815 if callsite
== null then return
1816 self.callsite
= callsite
1818 var args
= compute_raw_arguments
1820 callsite
.check_signature
(v
, args
)
1822 var readtype
= callsite
.msignature
.return_mtype
1823 if readtype
== null then
1824 v
.error
(node
, "Error: `{name}` is not a function.")
1828 var wcallsite
= v
.get_method
(node
, recvtype
, name
+ "=", self.n_expr
isa ASelfExpr)
1829 if wcallsite
== null then return
1830 self.write_callsite
= wcallsite
1832 var wtype
= self.resolve_reassignment
(v
, readtype
, wcallsite
.msignature
.mparameters
.last
.mtype
)
1833 if wtype
== null then return
1835 args
= args
.to_a
# duplicate so raw_arguments keeps only the getter args
1836 args
.add
(self.n_value
)
1837 wcallsite
.check_signature
(v
, args
)
1839 self.is_typed
= true
1843 redef class ACallReassignExpr
1844 redef fun property_name
do return n_qid
.n_id
.text
1845 redef fun property_node
do return n_qid
.n_id
1846 redef fun compute_raw_arguments
do return n_args
.to_a
1849 redef class ABraReassignExpr
1850 redef fun property_name
do return "[]"
1851 redef fun compute_raw_arguments
do return n_args
.to_a
1854 redef class AInitExpr
1855 redef fun property_name
do return "init"
1856 redef fun property_node
do return n_kwinit
1857 redef fun compute_raw_arguments
do return n_args
.to_a
1861 fun to_a
: Array[AExpr] do return self.n_exprs
.to_a
1866 redef class ASuperExpr
1867 # The method to call if the super is in fact a 'super init call'
1868 # Note: if the super is a normal call-next-method, then this attribute is null
1869 var callsite
: nullable CallSite
1871 # The method to call is the super is a standard `call-next-method` super-call
1872 # Note: if the super is a special super-init-call, then this attribute is null
1873 var mpropdef
: nullable MMethodDef
1875 redef fun accept_typing
(v
)
1877 var anchor
= v
.anchor
1878 assert anchor
!= null
1879 var recvtype
= v
.get_variable
(self, v
.selfvariable
)
1880 assert recvtype
!= null
1881 var mproperty
= v
.mpropdef
.mproperty
1882 if not mproperty
isa MMethod then
1883 v
.error
(self, "Error: `super` only usable in a `method`.")
1886 var superprops
= mproperty
.lookup_super_definitions
(v
.mmodule
, anchor
)
1887 if superprops
.length
== 0 then
1888 if mproperty
.is_init
and v
.mpropdef
.is_intro
then
1889 process_superinit
(v
)
1892 v
.error
(self, "Error: no super method to call for `{mproperty}`.")
1895 # FIXME: covariance of return type in linear extension?
1896 var superprop
= superprops
.first
1898 var msignature
= superprop
.msignature
.as(not null)
1899 msignature
= v
.resolve_for
(msignature
, recvtype
, true).as(MSignature)
1900 var args
= self.n_args
.to_a
1901 if args
.length
> 0 then
1902 signaturemap
= v
.check_signature
(self, args
, mproperty
, msignature
)
1904 self.mtype
= msignature
.return_mtype
1905 self.is_typed
= true
1906 v
.mpropdef
.has_supercall
= true
1907 mpropdef
= v
.mpropdef
.as(MMethodDef)
1910 # The mapping used on the call to associate arguments to parameters.
1911 # If null then no specific association is required.
1912 var signaturemap
: nullable SignatureMap
1914 private fun process_superinit
(v
: TypeVisitor)
1916 var anchor
= v
.anchor
1917 assert anchor
!= null
1918 var recvtype
= v
.get_variable
(self, v
.selfvariable
)
1919 assert recvtype
!= null
1920 var mpropdef
= v
.mpropdef
1921 assert mpropdef
isa MMethodDef
1922 var mproperty
= mpropdef
.mproperty
1923 var superprop
: nullable MMethodDef = null
1924 for msupertype
in mpropdef
.mclassdef
.supertypes
do
1925 msupertype
= msupertype
.anchor_to
(v
.mmodule
, anchor
)
1926 var errcount
= v
.modelbuilder
.toolcontext
.error_count
1927 var candidate
= v
.try_get_mproperty_by_name2
(self, msupertype
, mproperty
.name
).as(nullable MMethod)
1928 if candidate
== null then
1929 if v
.modelbuilder
.toolcontext
.error_count
> errcount
then return # Forward error
1930 continue # Try next super-class
1932 if superprop
!= null and candidate
.is_root_init
then
1935 if superprop
!= null and superprop
.mproperty
!= candidate
and not superprop
.mproperty
.is_root_init
then
1936 v
.error
(self, "Error: conflicting super constructor to call for `{mproperty}`: `{candidate.full_name}`, `{superprop.mproperty.full_name}`")
1939 var candidatedefs
= candidate
.lookup_definitions
(v
.mmodule
, anchor
)
1940 if superprop
!= null and superprop
.mproperty
== candidate
then
1941 if superprop
== candidatedefs
.first
then continue
1942 candidatedefs
.add
(superprop
)
1944 if candidatedefs
.length
> 1 then
1945 v
.error
(self, "Error: conflicting property definitions for property `{mproperty}` in `{recvtype}`: {candidatedefs.join(", ")}")
1948 superprop
= candidatedefs
.first
1950 if superprop
== null then
1951 v
.error
(self, "Error: no super method to call for `{mproperty}`.")
1955 var msignature
= superprop
.new_msignature
or else superprop
.msignature
.as(not null)
1956 msignature
= v
.resolve_for
(msignature
, recvtype
, true).as(MSignature)
1958 var callsite
= new CallSite(self, recvtype
, v
.mmodule
, v
.anchor
, true, superprop
.mproperty
, superprop
, msignature
, false)
1959 self.callsite
= callsite
1961 var args
= self.n_args
.to_a
1962 if args
.length
> 0 then
1963 callsite
.check_signature
(v
, args
)
1965 # Check there is at least enough parameters
1966 if mpropdef
.msignature
.arity
< msignature
.arity
then
1967 v
.error
(self, "Error: not enough implicit arguments to pass. Got `{mpropdef.msignature.arity}`, expected at least `{msignature.arity}`. Signature is `{msignature}`.")
1970 # Check that each needed parameter is conform
1972 for sp
in msignature
.mparameters
do
1973 var p
= mpropdef
.msignature
.mparameters
[i
]
1974 if not v
.is_subtype
(p
.mtype
, sp
.mtype
) then
1975 v
.error
(self, "Type Error: expected argument #{i} of type `{sp.mtype}`, got implicit argument `{p.name}` of type `{p.mtype}`. Signature is {msignature}")
1982 self.is_typed
= true
1988 redef class ANewExpr
1989 # The constructor invoked by the new.
1990 var callsite
: nullable CallSite
1992 # The designated type
1993 var recvtype
: nullable MClassType
1995 redef fun accept_typing
(v
)
1997 var recvtype
= v
.resolve_mtype
(self.n_type
)
1998 if recvtype
== null then return
2000 if not recvtype
isa MClassType then
2001 if recvtype
isa MNullableType then
2002 v
.error
(self, "Type Error: cannot instantiate the nullable type `{recvtype}`.")
2004 else if recvtype
isa MFormalType then
2005 v
.error
(self, "Type Error: cannot instantiate the formal type `{recvtype}`.")
2008 v
.error
(self, "Type Error: cannot instantiate the type `{recvtype}`.")
2013 self.recvtype
= recvtype
2014 var kind
= recvtype
.mclass
.kind
2017 var nqid
= self.n_qid
2019 if nqid
!= null then
2020 name
= nqid
.n_id
.text
2026 if name
== "intern" then
2027 if kind
!= concrete_kind
then
2028 v
.error
(self, "Type Error: cannot instantiate {kind} {recvtype}.")
2031 if n_args
.n_exprs
.not_empty
then
2032 v
.error
(n_args
, "Type Error: the intern constructor expects no arguments.")
2036 self.mtype
= recvtype
2040 var callsite
= v
.get_method
(node
, recvtype
, name
, false)
2041 if callsite
== null then return
2043 if not callsite
.mproperty
.is_new
then
2044 if kind
!= concrete_kind
then
2045 v
.error
(self, "Type Error: cannot instantiate {kind} `{recvtype}`.")
2048 self.mtype
= recvtype
2050 self.mtype
= callsite
.msignature
.return_mtype
2051 assert self.mtype
!= null
2054 self.callsite
= callsite
2056 if not callsite
.mproperty
.is_init_for
(recvtype
.mclass
) then
2057 v
.error
(self, "Error: `{name}` is not a constructor.")
2061 var args
= n_args
.to_a
2062 callsite
.check_signature
(v
, args
)
2068 redef class AAttrFormExpr
2069 # The attribute accessed.
2070 var mproperty
: nullable MAttribute
2072 # The static type of the attribute.
2073 var attr_type
: nullable MType
2075 # Resolve the attribute accessed.
2076 private fun resolve_property
(v
: TypeVisitor)
2078 var recvtype
= v
.visit_expr
(self.n_expr
)
2079 if recvtype
== null then return # Skip error
2080 var node
= self.n_id
2081 var name
= node
.text
2082 if recvtype
isa MNullType then
2083 v
.error
(node
, "Error: attribute `{name}` access on `null`.")
2087 var unsafe_type
= v
.anchor_to
(recvtype
)
2088 var mproperty
= v
.try_get_mproperty_by_name2
(node
, unsafe_type
, name
)
2089 if mproperty
== null then
2090 v
.modelbuilder
.error
(node
, "Error: attribute `{name}` does not exist in `{recvtype}`.")
2093 assert mproperty
isa MAttribute
2094 self.mproperty
= mproperty
2096 var mpropdefs
= mproperty
.lookup_definitions
(v
.mmodule
, unsafe_type
)
2097 assert mpropdefs
.length
== 1
2098 var mpropdef
= mpropdefs
.first
2099 var attr_type
= mpropdef
.static_mtype
2100 if attr_type
== null then return # skip error
2101 attr_type
= v
.resolve_for
(attr_type
, recvtype
, self.n_expr
isa ASelfExpr)
2102 self.attr_type
= attr_type
2106 redef class AAttrExpr
2107 redef fun accept_typing
(v
)
2109 self.resolve_property
(v
)
2110 self.mtype
= self.attr_type
2115 redef class AAttrAssignExpr
2116 redef fun accept_typing
(v
)
2118 self.resolve_property
(v
)
2119 var mtype
= self.attr_type
2121 v
.visit_expr_subtype
(self.n_value
, mtype
)
2122 self.is_typed
= mtype
!= null
2126 redef class AAttrReassignExpr
2127 redef fun accept_typing
(v
)
2129 self.resolve_property
(v
)
2130 var mtype
= self.attr_type
2131 if mtype
== null then return # Skip error
2133 var rettype
= self.resolve_reassignment
(v
, mtype
, mtype
)
2135 self.is_typed
= rettype
!= null
2139 redef class AIssetAttrExpr
2140 redef fun accept_typing
(v
)
2142 self.resolve_property
(v
)
2143 var mtype
= self.attr_type
2144 if mtype
== null then return # Skip error
2146 var recvtype
= self.n_expr
.mtype
.as(not null)
2147 var bound
= v
.resolve_for
(mtype
, recvtype
, false)
2148 if bound
isa MNullableType then
2149 v
.error
(n_id
, "Type Error: `isset` on a nullable attribute.")
2151 self.mtype
= v
.type_bool
(self)
2155 redef class AVarargExpr
2156 redef fun accept_typing
(v
)
2158 # This kind of pseudo-expression can be only processed trough a signature
2159 # See `check_signature`
2160 # Other cases are a syntax error.
2161 v
.error
(self, "Syntax Error: unexpected `...`.")
2167 redef class ADebugTypeExpr
2168 redef fun accept_typing
(v
)
2170 var expr
= v
.visit_expr
(self.n_expr
)
2171 if expr
== null then return
2172 var unsafe
= v
.anchor_to
(expr
)
2173 var ntype
= self.n_type
2174 var mtype
= v
.resolve_mtype
(ntype
)
2175 if mtype
!= null and mtype
!= expr
then
2176 var umtype
= v
.anchor_to
(mtype
)
2177 v
.modelbuilder
.warning
(self, "debug", "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")
2179 self.is_typed
= true