return name && id_equal (name, "construct_at");
}
+/* Overload for the above taking constexpr_call*. */
+
+static inline bool
+is_std_construct_at (const constexpr_call *call)
+{
+ return (call
+ && call->fundef
+ && is_std_construct_at (call->fundef->decl));
+}
+
/* Return true if FNDECL is std::allocator<T>::{,de}allocate. */
static inline bool
return decl_in_std_namespace_p (decl);
}
+/* Overload for the above taking constexpr_call*. */
+
+static inline bool
+is_std_allocator_allocate (const constexpr_call *call)
+{
+ return (call
+ && call->fundef
+ && is_std_allocator_allocate (call->fundef->decl));
+}
+
/* Return true if FNDECL is __dynamic_cast. */
static inline bool
if (TREE_CODE (t) == CALL_EXPR
&& cxx_replaceable_global_alloc_fn (fun)
&& (CALL_FROM_NEW_OR_DELETE_P (t)
- || (ctx->call
- && ctx->call->fundef
- && is_std_allocator_allocate (ctx->call->fundef->decl))))
+ || is_std_allocator_allocate (ctx->call)))
{
const int nargs = call_expr_nargs (t);
tree arg0 = NULL_TREE;
argument. */
if (TREE_CODE (t) == CALL_EXPR
&& cxx_placement_new_fn (fun)
- && ctx->call
- && ctx->call->fundef
- && is_std_construct_at (ctx->call->fundef->decl))
+ && is_std_construct_at (ctx->call))
{
const int nargs = call_expr_nargs (t);
tree arg1 = NULL_TREE;
return t;
}
+ /* [expr.const]: a conversion from type cv void* to a pointer-to-object
+ type cannot be part of a core constant expression as a resolution to
+ DR 1312. */
+ if (integer_zerop (op) /* FIXME: Remove in GCC 12. */
+ && TYPE_PTROB_P (type)
+ && TYPE_PTR_P (TREE_TYPE (op))
+ && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (op)))
+ /* Inside a call to std::construct_at or to
+ std::allocator<T>::{,de}allocate, we permit casting from void*
+ because that is compiler-generated code. */
+ && !is_std_construct_at (ctx->call)
+ && !is_std_allocator_allocate (ctx->call))
+ {
+ /* Likewise, don't error when casting from void* when OP is
+ &heap uninit and similar. */
+ tree sop = tree_strip_nop_conversions (op);
+ if (TREE_CODE (sop) == ADDR_EXPR
+ && VAR_P (TREE_OPERAND (sop, 0))
+ && DECL_ARTIFICIAL (TREE_OPERAND (sop, 0)))
+ /* OK */;
+ else
+ {
+ if (!ctx->quiet)
+ error_at (loc, "cast from %qT is not allowed",
+ TREE_TYPE (op));
+ *non_constant_p = true;
+ return t;
+ }
+ }
+
if (TREE_CODE (op) == PTRMEM_CST && !TYPE_PTRMEM_P (type))
op = cplus_expand_constant (op);
if (TYPE_REF_P (type))
{
if (!ctx->quiet)
- error_at (loc,
- "dereferencing a null pointer");
+ error_at (loc, "dereferencing a null pointer");
*non_constant_p = true;
return t;
}
- else if (TYPE_PTR_P (TREE_TYPE (op)))
- {
- tree from = TREE_TYPE (op);
-
- if (!can_convert (type, from, tf_none))
- {
- if (!ctx->quiet)
- error_at (loc,
- "conversion of %qT null pointer to %qT "
- "is not a constant expression",
- from, type);
- *non_constant_p = true;
- return t;
- }
- }
}
else
{
--- /dev/null
+// DR 1312 - Simulated reinterpret_cast in constant expressions.
+// PR c++/99176
+// { dg-do compile { target c++11 } }
+
+static int i;
+constexpr void *vp0 = nullptr;
+constexpr void *vpi = &i;
+constexpr int *p1 = (int *) vp0; // { dg-error "cast from .void\\*. is not allowed" }
+constexpr int *p2 = (int *) vpi; // { dg-error "cast from .void\\*. is not allowed" "integer_zerop" { xfail *-*-* } }
+constexpr int *p3 = static_cast<int *>(vp0); // { dg-error "cast from .void\\*. is not allowed" }
+constexpr int *p4 = static_cast<int *>(vpi); // { dg-error "cast from .void\\*. is not allowed" "integer_zerop" { xfail *-*-* } }
+constexpr void *p5 = vp0;
+constexpr void *p6 = vpi;
+
+constexpr int *pi = &i;
+constexpr bool b = ((int *)(void *) pi == pi); // { dg-error "cast from .void\\*. is not allowed" "integer_zerop" { xfail *-*-* } }
constexpr void *pv3 = pv2; // { dg-error "invalid conversion|not a constant expression" }
constexpr const void *pv4 = pv2;
-constexpr X *px4 = pv0; // { dg-error "invalid conversion|not a constant expression" }
+constexpr X *px4 = pv0; // { dg-error "cast from|invalid conversion|not a constant expression" }
}
constexpr A *pa4 = static_cast<A*>(pd0); // { dg-error "ambiguous base" }
constexpr B *pb1 = pa0; // { dg-error "invalid conversion|not a constant expression" }
-constexpr B *pb2 = static_cast<B*>(pa0); // { dg-error "not a constant expression" }
+constexpr B *pb2 = static_cast<B*>(pa0);
constexpr C *pc1 = pa0; // { dg-error "invalid conversion|not a constant expression" }
constexpr D *pd1 = pa0; // { dg-error "ambiguous base|invalid conversion" }