On 8/27/25 4:13 AM, Jakub Jelinek wrote:
Hi!

When looking at constexpr references, I've noticed staticp handles
COMPONENT_REFs and ARRAY_REFs (the latter if the index is INTEGER_CST),
but not {REAL,IMAG}PART_EXPR.  I think that is incorrect and causes
rejection of constexpr (for C++) or static const (for C) addresses
of __real__ or __imag__ parts of static vars.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

Makes sense to me, OK if no objection from C.

2025-08-27  Jakub Jelinek  <ja...@redhat.com>

        PR c++/121678
        * tree.cc (staticp): Handle REALPART_EXPR and IMAGPART_EXPR.

        * g++.dg/ext/pr121678.C: New test.
        * gcc.dg/pr121678.c: New test.

--- gcc/tree.cc.jj      2025-08-23 15:00:05.019777931 +0200
+++ gcc/tree.cc 2025-08-26 17:55:55.786162975 +0200
@@ -3935,6 +3935,10 @@ staticp (tree arg)
        else
        return NULL;
+ case REALPART_EXPR:
+    case IMAGPART_EXPR:
+      return staticp (TREE_OPERAND (arg, 0));
+
      case COMPOUND_LITERAL_EXPR:
        return TREE_STATIC (COMPOUND_LITERAL_EXPR_DECL (arg)) ? arg : NULL;
--- gcc/testsuite/g++.dg/ext/pr121678.C.jj 2025-08-26 18:21:05.593623718 +0200
+++ gcc/testsuite/g++.dg/ext/pr121678.C 2025-08-26 18:21:22.661403914 +0200
@@ -0,0 +1,7 @@
+// PR c++/121678
+// { dg-do compile { target c++11 } }
+// { dg-options "" }
+
+static constexpr _Complex double a = 1.0;
+static constexpr double *r = &__real__ a;
+static constexpr double *i = &__imag__ a;
--- gcc/testsuite/gcc.dg/pr121678.c.jj  2025-08-26 18:20:08.453359588 +0200
+++ gcc/testsuite/gcc.dg/pr121678.c     2025-08-26 18:20:37.667983352 +0200
@@ -0,0 +1,7 @@
+/* PR c++/121678 */
+/* { dg-do compile } */
+/* { dg-options "" } */
+
+static const _Complex double a = 1.0;
+static const double *const r = &__real__ a;
+static const double *const i = &__imag__ a;

        Jakub


Reply via email to