summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Jelinek <jakub@redhat.com>2022-08-07 10:07:38 +0200
committerJakub Jelinek <jakub@redhat.com>2022-08-07 10:07:38 +0200
commit190776773516955df480bfa75731c34c5aaf2306 (patch)
treeb3140f5eccf67869abd92bdc051b0dc6c1487622
parenta46bca36b7b3a8a7e15b04225fb2b4f9b1bed62c (diff)
We claim we support P0415R1 (constexpr complex), but e.g. #include <complex> constexpr bool foo () { std::complex<double> a (1.0, 2.0); a += 3.0; a.real (6.0); return a.real () == 6.0 && a.imag () == 2.0; } static_assert (foo ()); fails with test.C:12:20: error: non-constant condition for static assertion 12 | static_assert (foo ()); | ~~~~^~ test.C:12:20: in ‘constexpr’ expansion of ‘foo()’ test.C:8:10: in ‘constexpr’ expansion of ‘a.std::complex<double>::real(6.0e+0)’ test.C:12:20: error: modification of ‘__real__ a.std::complex<double>::_M_value’ is not a constant expression The problem is we don't handle REALPART_EXPR and IMAGPART_EXPR in cxx_eval_store_expression. The following patch attempts to support it (with a requirement that those are the outermost expressions, ARRAY_REF/COMPONENT_REF etc. are just not possible on the result of these, BIT_FIELD_REF would be theoretically possible if trying to extract some bits from one part of a complex int, but I don't see how it could appear in the FE trees. For these references, the code handles value being COMPLEX_CST, COMPLEX_EXPR or CONSTRUCTOR_NO_CLEARING empty CONSTRUCTOR (what we use to represent uninitialized values for C++20 and later) and the code starts by rewriting it to COMPLEX_EXPR, so that we can freely adjust the individual parts and later on possibly optimize it back to COMPLEX_CST if both halves are constant. 2022-08-07 Jakub Jelinek <jakub@redhat.com> PR c++/88174 * constexpr.cc (cxx_eval_store_expression): Handle REALPART_EXPR and IMAGPART_EXPR. Change ctors from releasing_vec to auto_vec<tree *>, adjust all uses. For !preeval, update ctors vector. * g++.dg/cpp1y/constexpr-complex1.C: New test.
-rw-r--r--gcc/cp/constexpr.cc94
-rw-r--r--gcc/testsuite/g++.dg/cpp1y/constexpr-complex1.C24
2 files changed, 104 insertions, 14 deletions
diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 5e0d3399172..c047fe4a2a1 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -5732,6 +5732,20 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
}
break;
+ case REALPART_EXPR:
+ gcc_assert (probe == target);
+ vec_safe_push (refs, probe);
+ vec_safe_push (refs, TREE_TYPE (probe));
+ probe = TREE_OPERAND (probe, 0);
+ break;
+
+ case IMAGPART_EXPR:
+ gcc_assert (probe == target);
+ vec_safe_push (refs, probe);
+ vec_safe_push (refs, TREE_TYPE (probe));
+ probe = TREE_OPERAND (probe, 0);
+ break;
+
default:
if (evaluated)
object = probe;
@@ -5770,7 +5784,8 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
type = TREE_TYPE (object);
bool no_zero_init = true;
- releasing_vec ctors, indexes;
+ auto_vec<tree *> ctors;
+ releasing_vec indexes;
auto_vec<int> index_pos_hints;
bool activated_union_member_p = false;
bool empty_base = false;
@@ -5810,14 +5825,36 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
*valp = ary_ctor;
}
- /* If the value of object is already zero-initialized, any new ctors for
- subobjects will also be zero-initialized. */
- no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
-
enum tree_code code = TREE_CODE (type);
tree reftype = refs->pop();
tree index = refs->pop();
+ if (code == COMPLEX_TYPE)
+ {
+ if (TREE_CODE (*valp) == COMPLEX_CST)
+ *valp = build2 (COMPLEX_EXPR, type, TREE_REALPART (*valp),
+ TREE_IMAGPART (*valp));
+ else if (TREE_CODE (*valp) == CONSTRUCTOR
+ && CONSTRUCTOR_NELTS (*valp) == 0
+ && CONSTRUCTOR_NO_CLEARING (*valp))
+ {
+ tree r = build_constructor (reftype, NULL);
+ CONSTRUCTOR_NO_CLEARING (r) = 1;
+ *valp = build2 (COMPLEX_EXPR, type, r, r);
+ }
+ gcc_assert (TREE_CODE (*valp) == COMPLEX_EXPR);
+ ctors.safe_push (valp);
+ vec_safe_push (indexes, index);
+ valp = &TREE_OPERAND (*valp, TREE_CODE (index) == IMAGPART_EXPR);
+ gcc_checking_assert (refs->is_empty ());
+ type = reftype;
+ break;
+ }
+
+ /* If the value of object is already zero-initialized, any new ctors for
+ subobjects will also be zero-initialized. */
+ no_zero_init = CONSTRUCTOR_NO_CLEARING (*valp);
+
if (code == RECORD_TYPE && is_empty_field (index))
/* Don't build a sub-CONSTRUCTOR for an empty base or field, as they
have no data and might have an offset lower than previously declared
@@ -5860,7 +5897,7 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
no_zero_init = true;
}
- vec_safe_push (ctors, *valp);
+ ctors.safe_push (valp);
vec_safe_push (indexes, index);
constructor_elt *cep
@@ -5922,11 +5959,11 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
semantics are not applied on an object under construction.
They come into effect when the constructor for the most
derived object ends." */
- for (tree elt : *ctors)
+ for (tree *elt : ctors)
if (same_type_ignoring_top_level_qualifiers_p
- (TREE_TYPE (const_object_being_modified), TREE_TYPE (elt)))
+ (TREE_TYPE (const_object_being_modified), TREE_TYPE (*elt)))
{
- fail = TREE_READONLY (elt);
+ fail = TREE_READONLY (*elt);
break;
}
}
@@ -5967,6 +6004,7 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
valp = ctx->global->values.get (object);
for (unsigned i = 0; i < vec_safe_length (indexes); i++)
{
+ ctors[i] = valp;
constructor_elt *cep
= get_or_insert_ctor_field (*valp, indexes[i], index_pos_hints[i]);
valp = &cep->value;
@@ -6029,17 +6067,45 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
CONSTRUCTORs, if any. */
bool c = TREE_CONSTANT (init);
bool s = TREE_SIDE_EFFECTS (init);
+ if (!indexes->is_empty ())
+ {
+ tree last = indexes->last ();
+ if (TREE_CODE (last) == REALPART_EXPR
+ || TREE_CODE (last) == IMAGPART_EXPR)
+ {
+ /* And canonicalize COMPLEX_EXPR into COMPLEX_CST if
+ possible. */
+ tree *cexpr = ctors.last ();
+ if (tree c = const_binop (COMPLEX_EXPR, TREE_TYPE (*cexpr),
+ TREE_OPERAND (*cexpr, 0),
+ TREE_OPERAND (*cexpr, 1)))
+ *cexpr = c;
+ else
+ {
+ TREE_CONSTANT (*cexpr)
+ = (TREE_CONSTANT (TREE_OPERAND (*cexpr, 0))
+ & TREE_CONSTANT (TREE_OPERAND (*cexpr, 1)));
+ TREE_SIDE_EFFECTS (*cexpr)
+ = (TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 0))
+ | TREE_SIDE_EFFECTS (TREE_OPERAND (*cexpr, 1)));
+ }
+ c = TREE_CONSTANT (*cexpr);
+ s = TREE_SIDE_EFFECTS (*cexpr);
+ }
+ }
if (!c || s || activated_union_member_p)
- for (tree elt : *ctors)
+ for (tree *elt : ctors)
{
+ if (TREE_CODE (*elt) != CONSTRUCTOR)
+ continue;
if (!c)
- TREE_CONSTANT (elt) = false;
+ TREE_CONSTANT (*elt) = false;
if (s)
- TREE_SIDE_EFFECTS (elt) = true;
+ TREE_SIDE_EFFECTS (*elt) = true;
/* Clear CONSTRUCTOR_NO_CLEARING since we've activated a member of
this union. */
- if (TREE_CODE (TREE_TYPE (elt)) == UNION_TYPE)
- CONSTRUCTOR_NO_CLEARING (elt) = false;
+ if (TREE_CODE (TREE_TYPE (*elt)) == UNION_TYPE)
+ CONSTRUCTOR_NO_CLEARING (*elt) = false;
}
if (lval)
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-complex1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-complex1.C
new file mode 100644
index 00000000000..8bb24cb2775
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-complex1.C
@@ -0,0 +1,24 @@
+// PR c++/88174
+// { dg-do compile { target c++14 } }
+
+constexpr bool
+foo (double x, double y, double z, double w)
+{
+ __complex__ double a = 0;
+ __real__ a = x;
+ __imag__ a = y;
+#if __cpp_constexpr >= 201907L
+ __complex__ double b;
+ __real__ b = z;
+#else
+ __complex__ double b = z;
+#endif
+ __imag__ b = w;
+ a += b;
+ a -= b;
+ a *= b;
+ a /= b;
+ return __real__ a == x && __imag__ a == y;
+}
+
+static_assert (foo (1.0, 2.0, 3.0, 4.0), "");