redef fun expr(v)
do
var recv = v.expr(self.n_expr, null)
+ if is_safe then
+ v.add "if ({recv}!=NULL) \{"
+ end
var callsite = self.callsite.as(not null)
if callsite.is_broken then return null
var args = v.varargize(callsite.mpropdef, callsite.signaturemap, recv, self.raw_arguments)
- return v.compile_callsite(callsite, args)
+ var res = v.compile_callsite(callsite, args)
+ if is_safe then
+ if res != null then
+ var orig_res = res
+ res = v.new_var(self.mtype.as(not null))
+ v.add("{res} = {orig_res};")
+ v.add("\} else \{")
+ v.add("{res} = NULL;")
+ end
+ v.add("\}")
+ end
+ return res
end
end
end
end
+redef class ASafeExpr
+ redef fun expr(v)
+ do
+ return v.expr(self.n_expr, null)
+ end
+end
+
redef class ANamedargExpr
redef fun expr(v)
do
do
var recv = v.expr(self.n_expr)
if recv == null then return null
+
+ # Safe call shortcut if recv is null
+ if is_safe and recv.is_null then
+ return recv
+ end
+
var args = v.varargize(callsite.mpropdef, callsite.signaturemap, recv, self.raw_arguments)
if args == null then return null
end
end
+redef class ASafeExpr
+ redef fun expr(v)
+ do
+ return v.expr(self.n_expr)
+ end
+end
+
redef class ANamedargExpr
redef fun expr(v)
do
# The property invoked by the send.
var callsite: nullable CallSite
+ # Is self a safe call (with `x?.foo`)?
+ # If so and the receiver is null, then the arguments won't be evaluated
+ # and the call skipped (replaced with null).
+ var is_safe: Bool = false
+
redef fun bad_expr_message(child)
do
if child == self.n_expr then
do
var nrecv = self.n_expr
var recvtype = v.visit_expr(nrecv)
+
+ if nrecv isa ASafeExpr then
+ # Has the receiver the form `x?.foo`?
+ # For parsing "reasons" the `?` is in the receiver node, not the call node.
+ is_safe = true
+ end
+
var name = self.property_name
var node = self.property_node
var ret = msignature.return_mtype
if ret != null then
+ if is_safe then
+ # A safe receiver makes that the call is not executed and returns null
+ ret = ret.as_nullable
+ end
self.mtype = ret
else
self.is_typed = true
end
end
+redef class ASafeExpr
+ redef fun accept_typing(v)
+ do
+ var mtype = v.visit_expr(n_expr)
+ if mtype == null then return # Skip error
+
+ if mtype isa MNullType then
+ # While `null?.foo` is semantically well defined and should not execute `foo` and just return `null`,
+ # currently `null.foo` is forbidden so it seems coherent to also forbid `null?.foo`
+ v.modelbuilder.error(self, "Error: safe operator `?` on `null`.")
+ return
+ end
+
+ self.mtype = mtype.as_notnull
+
+ if not v.can_be_null(mtype) then
+ v.modelbuilder.warning(self, "useless-safe", "Warning: useless safe operator `?` on non-nullable value.")
+ return
+ end
+ end
+end
+
redef class AVarargExpr
redef fun accept_typing(v)
do