end
# Check that `sub` is a subtype of `sup`.
- # If `sub` is not a valid suptype, then display an error on `node` an return null.
- # If `sub` is a safe subtype of `sup` then return `sub`.
- # If `sub` is an unsafe subtype (ie an implicit cast is required), then return `sup`.
+ # If `sub` is not a valid suptype, then display an error on `node` and return `null`.
+ # If `sub` is a safe subtype of `sup`, then return `sub`.
+ # If `sub` is an unsafe subtype (i.e., an implicit cast is required), then return `sup`.
#
# The point of the return type is to determinate the usable type on an expression when `autocast` is true:
# If the suptype is safe, then the return type is the one on the expression typed by `sub`.
#node.debug("Unsafe typing: expected {sup}, got {sub}")
return sup
end
+ if sup isa MErrorType then return null # Skip error
if sub.need_anchor then
var u = anchor_to(sub)
self.modelbuilder.error(node, "Type Error: expected `{sup}`, got `{sub}: {u}`.")
end
return null # forward error
end
- self.error(nexpr, "Error: expected an expression.")
+ var more_message = null
+ var p = nexpr.parent
+ if p != null then more_message = p.bad_expr_message(nexpr)
+ if more_message == null then more_message = "" else more_message = " " + more_message
+ self.error(nexpr, "Error: expected an expression{more_message}.")
return null
end
continue # skip the vararg
end
- var paramtype = param.mtype
- self.visit_expr_subtype(arg, paramtype)
+ if not param.is_vararg then
+ var paramtype = param.mtype
+ self.visit_expr_subtype(arg, paramtype)
+ else
+ check_one_vararg(arg, param)
+ end
end
if min_arity > 0 then
var paramtype = msignature.mparameters[vararg_rank].mtype
var first = args[vararg_rank]
if vararg_decl == 0 then
- var mclass = get_mclass(node, "Array")
- if mclass == null then return null # Forward error
- var array_mtype = mclass.get_mtype([paramtype])
- if first isa AVarargExpr then
- self.visit_expr_subtype(first.n_expr, array_mtype)
- first.mtype = first.n_expr.mtype
- else
- # only one vararg, maybe `...` was forgot, so be gentle!
- var t = visit_expr(first)
- if t == null then return null # Forward error
- if not is_subtype(t, paramtype) and is_subtype(t, array_mtype) then
- # Not acceptable but could be a `...`
- error(first, "Type Error: expected `{paramtype}`, got `{t}`. Is an ellipsis `...` missing on the argument?")
- return null
- end
- # Standard valid vararg, finish the job
- map.vararg_decl = 1
- self.visit_expr_subtype(first, paramtype)
- end
+ if not check_one_vararg(first, msignature.mparameters[vararg_rank]) then return null
else
- map.vararg_decl = vararg_decl + 1
+ first.vararg_decl = vararg_decl + 1
for i in [vararg_rank..vararg_rank+vararg_decl] do
self.visit_expr_subtype(args[i], paramtype)
end
return map
end
+ # Check an expression as a single vararg.
+ # The main point of the method if to handle the case of reversed vararg (see `AVarargExpr`)
+ fun check_one_vararg(arg: AExpr, param: MParameter): Bool
+ do
+ var paramtype = param.mtype
+ var mclass = get_mclass(arg, "Array")
+ if mclass == null then return false # Forward error
+ var array_mtype = mclass.get_mtype([paramtype])
+ if arg isa AVarargExpr then
+ self.visit_expr_subtype(arg.n_expr, array_mtype)
+ arg.mtype = arg.n_expr.mtype
+ else
+ # only one vararg, maybe `...` was forgot, so be gentle!
+ var t = visit_expr(arg)
+ if t == null then return false # Forward error
+ if not is_subtype(t, paramtype) and is_subtype(t, array_mtype) then
+ # Not acceptable but could be a `...`
+ error(arg, "Type Error: expected `{paramtype}`, got `{t}`. Is an ellipsis `...` missing on the argument?")
+ return false
+ end
+ # Standard valid vararg, finish the job
+ arg.vararg_decl = 1
+ self.visit_expr_subtype(arg, paramtype)
+ end
+ return true
+ end
+
fun error(node: ANode, message: String)
do
self.modelbuilder.error(node, message)
class SignatureMap
# Associate a parameter to an argument
var map = new ArrayMap[Int, Int]
-
- # The length of the vararg sequence
- # 0 if no vararg or if reverse vararg (cf `AVarargExpr`)
- var vararg_decl: Int = 0
end
# A specific method call site with its associated informations.
class CallSite
super MEntity
- # The associated location of the callsite
- var location: Location
+ redef var location: Location
# The static type of the receiver (possibly unresolved)
var recv: MType
if map == null then is_broken = true
return map == null
end
+
+ # Information about the callsite to display on a node
+ fun dump_info(v: ASTDump): String do
+ return "{recv}.{mpropdef}{msignature}"
+ end
end
redef class Variable
redef class ANode
private fun accept_post_typing(v: TypeVisitor) do end
+
+ # An additional information message to explain the role of a child expression.
+ #
+ # The point of the method is to allow some kind of double dispatch so the parent
+ # choose how to describe its children.
+ private fun bad_expr_message(child: AExpr): nullable String do return null
end
redef class AAttrPropdef
# The result of the evaluation of `self` must be
# stored inside the designated array (there is an implicit `push`)
var comprehension: nullable AArrayExpr = null
+
+ # It indicates the number of arguments collected as a vararg.
+ #
+ # When 0, the argument is used as is, without transformation.
+ # When 1, the argument is transformed into an singleton array.
+ # Above 1, the arguments and the next ones are transformed into a common array.
+ #
+ # This attribute is meaning less on expressions not used as attributes.
+ var vararg_decl: Int = 0
+
+ redef fun dump_info(v) do
+ var res = super
+ var mtype = self.mtype
+ if mtype != null then
+ res += v.yellow(":{mtype}")
+ end
+ var ict = self.implicit_cast_to
+ if ict != null then
+ res += v.yellow("(.as({ict}))")
+ end
+ return res
+ end
end
redef class ABlockExpr
redef fun accept_typing(v)
do
v.visit_stmt(n_block)
+ v.visit_stmt(n_catch)
self.is_typed = true
end
end
var mtype = v.visit_expr(g.n_expr)
if mtype == null then return
g.do_type_iterator(v, mtype)
+ if g.is_broken then is_broken = true
end
v.visit_stmt(n_block)
end
redef class ACharExpr
- redef fun accept_typing(v)
- do
- var mclass = v.get_mclass(self, "Char")
+ redef fun accept_typing(v) do
+ var mclass: nullable MClass = null
+ if is_ascii then
+ mclass = v.get_mclass(self, "Byte")
+ else if is_code_point then
+ mclass = v.get_mclass(self, "Int")
+ else
+ mclass = v.get_mclass(self, "Char")
+ end
if mclass == null then return # Forward error
self.mtype = mclass.mclass_type
end
end
-redef class AStringFormExpr
- redef fun accept_typing(v)
- do
+redef class AugmentedStringFormExpr
+ super AExpr
+
+ # Text::to_re, used for prefix `re`
+ var to_re: nullable CallSite = null
+ # Regex::ignore_case, used for suffix `i` on `re`
+ var ignore_case: nullable CallSite = null
+ # Regex::newline, used for suffix `m` on `re`
+ var newline: nullable CallSite = null
+ # Regex::extended, used for suffix `b` on `re`
+ var extended: nullable CallSite = null
+ # CString::to_bytes_with_copy, used for prefix `b`
+ var to_bytes_with_copy: nullable CallSite = null
+
+ redef fun accept_typing(v) do
var mclass = v.get_mclass(self, "String")
if mclass == null then return # Forward error
- self.mtype = mclass.mclass_type
+ if is_bytestring then
+ to_bytes_with_copy = v.get_method(self, v.mmodule.c_string_type, "to_bytes_with_copy", false)
+ mclass = v.get_mclass(self, "Bytes")
+ else if is_re then
+ to_re = v.get_method(self, mclass.mclass_type, "to_re", false)
+ for i in suffix.chars do
+ mclass = v.get_mclass(self, "Regex")
+ if mclass == null then
+ v.error(self, "Error: `Regex` class unknown")
+ return
+ end
+ var service = ""
+ if i == 'i' then
+ service = "ignore_case="
+ ignore_case = v.get_method(self, mclass.mclass_type, service, false)
+ else if i == 'm' then
+ service = "newline="
+ newline = v.get_method(self, mclass.mclass_type, service, false)
+ else if i == 'b' then
+ service = "extended="
+ extended = v.get_method(self, mclass.mclass_type, service, false)
+ else
+ v.error(self, "Type Error: Unrecognized suffix {i} in prefixed Regex")
+ abort
+ end
+ end
+ end
+ if mclass == null then return # Forward error
+ mtype = mclass.mclass_type
end
end
redef class ASuperstringExpr
redef fun accept_typing(v)
do
- var mclass = v.get_mclass(self, "String")
- if mclass == null then return # Forward error
- self.mtype = mclass.mclass_type
+ super
var objclass = v.get_mclass(self, "Object")
if objclass == null then return # Forward error
var objtype = objclass.mclass_type
var variable = self.n_expr.its_variable
if variable != null then
- #var orig = self.n_expr.mtype
+ var orig = self.n_expr.mtype
#var from = if orig != null then orig.to_s else "invalid"
#var to = if mtype != null then mtype.to_s else "invalid"
#debug("adapt {variable}: {from} -> {to}")
- self.after_flow_context.when_true.set_var(v, variable, mtype)
+
+ # Do not adapt if there is no information gain (i.e. adapt to a supertype)
+ if mtype == null or orig == null or not v.is_subtype(orig, mtype) then
+ self.after_flow_context.when_true.set_var(v, variable, mtype)
+ end
end
self.mtype = v.type_bool(self)
do
v.check_expr_cast(self, self.n_expr, self.n_type)
end
+
+ redef fun dump_info(v) do
+ var res = super
+ var mtype = self.cast_type
+ if mtype != null then
+ res += v.yellow(".as({mtype})")
+ end
+ return res
+ end
+
end
redef class AAsCastExpr
# The property invoked by the send.
var callsite: nullable CallSite
+ redef fun bad_expr_message(child)
+ do
+ if child == self.n_expr then
+ return "to be the receiver of `{self.property_name}`"
+ end
+ return null
+ end
+
redef fun accept_typing(v)
do
var nrecv = self.n_expr
fun raw_arguments: Array[AExpr] do return compute_raw_arguments
private fun compute_raw_arguments: Array[AExpr] is abstract
+
+ redef fun dump_info(v) do
+ var res = super
+ var callsite = self.callsite
+ if callsite != null then
+ res += v.yellow(" call="+callsite.dump_info(v))
+ end
+ return res
+ end
end
redef class ABinopExpr
self.is_typed = true
end
+
+ redef fun dump_info(v) do
+ var res = super
+ var callsite = self.callsite
+ if callsite != null then
+ res += v.yellow(" super-init="+callsite.dump_info(v))
+ end
+ var mpropdef = self.mpropdef
+ if mpropdef != null then
+ res += v.yellow(" call-next-method="+mpropdef.to_s)
+ end
+ return res
+ end
end
####
var args = n_args.to_a
callsite.check_signature(v, node, args)
end
+
+ redef fun dump_info(v) do
+ var res = super
+ var callsite = self.callsite
+ if callsite != null then
+ res += v.yellow(" call="+callsite.dump_info(v))
+ end
+ return res
+ end
end
####
attr_type = v.resolve_for(attr_type, recvtype, self.n_expr isa ASelfExpr)
self.attr_type = attr_type
end
+
+ redef fun dump_info(v) do
+ var res = super
+ var mproperty = self.mproperty
+ var attr_type = self.attr_type
+ if mproperty != null then
+ res += v.yellow(" attr={mproperty}:{attr_type or else "BROKEN"}")
+ end
+ return res
+ end
end
redef class AAttrExpr