]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c, c++: Allow &__real__ static_var in constant expressions [PR121678]
authorJakub Jelinek <jakub@redhat.com>
Tue, 9 Sep 2025 14:44:48 +0000 (16:44 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Tue, 9 Sep 2025 14:44:48 +0000 (16:44 +0200)
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.

2025-09-09  Jakub Jelinek  <jakub@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/testsuite/g++.dg/ext/pr121678.C [new file with mode: 0644]
gcc/testsuite/gcc.dg/pr121678.c [new file with mode: 0644]
gcc/tree.cc

diff --git a/gcc/testsuite/g++.dg/ext/pr121678.C b/gcc/testsuite/g++.dg/ext/pr121678.C
new file mode 100644 (file)
index 0000000..e283ea1
--- /dev/null
@@ -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;
diff --git a/gcc/testsuite/gcc.dg/pr121678.c b/gcc/testsuite/gcc.dg/pr121678.c
new file mode 100644 (file)
index 0000000..afb6c20
--- /dev/null
@@ -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;
index 905c2d6657fbfc1914df75c32e081507bfb2dc48..2a3ab23f1dc7622295c66922fc7fdb9c1b9584df 100644 (file)
@@ -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;