public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r13-5323] c++: Handle structured bindings like anon unions in initializers [PR108474]
@ 2023-01-24 10:32 Jakub Jelinek
  0 siblings, 0 replies; only message in thread
From: Jakub Jelinek @ 2023-01-24 10:32 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:b84e21115700523b4d0ac44275443f7b9c670344

commit r13-5323-gb84e21115700523b4d0ac44275443f7b9c670344
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Tue Jan 24 11:28:00 2023 +0100

    c++: Handle structured bindings like anon unions in initializers [PR108474]
    
    As reported by Andrew Pinski, structured bindings (with the exception
    of the ones using std::tuple_{size,element} and get which are really
    standalone variables in addition to the binding one) also use
    DECL_VALUE_EXPR and needs the same treatment in static initializers.
    
    On Sun, Jan 22, 2023 at 07:19:07PM -0500, Jason Merrill wrote:
    > Though, actually, why not instead fix expand_expr_real_1 (and staticp) to
    > look through DECL_VALUE_EXPR?
    
    Doing it when emitting the initializers seems to be too late to me,
    we in various spots try to put parts of the static var DECL_INITIAL expressions
    into the IL, or e.g. for varpool purposes remember which vars are referenced
    there.
    
    This patch moves it to record_reference, which is called from varpool_node::analyze
    and so about the same time as gimplification of the bodies which also
    replaces DECL_VALUE_EXPRs.
    
    2023-01-24  Jakub Jelinek  <jakub@redhat.com>
    
            PR c++/108474
            * cgraphbuild.cc: Include gimplify.h.
            (record_reference): Replace VAR_DECLs with DECL_HAS_VALUE_EXPR_P with
            their corresponding DECL_VALUE_EXPR expressions after unsharing.
    
            * cp-gimplify.cc (cp_fold_r): Revert 2023-01-19 changes.
    
            * g++.dg/cpp1z/decomp57.C: New test.
            * g++.dg/cpp1z/decomp58.C: New test.

Diff:
---
 gcc/cgraphbuild.cc                    | 12 +++++++++++
 gcc/cp/cp-gimplify.cc                 | 10 ---------
 gcc/testsuite/g++.dg/cpp1z/decomp57.C | 27 ++++++++++++++++++++++++
 gcc/testsuite/g++.dg/cpp1z/decomp58.C | 39 +++++++++++++++++++++++++++++++++++
 4 files changed, 78 insertions(+), 10 deletions(-)

diff --git a/gcc/cgraphbuild.cc b/gcc/cgraphbuild.cc
index f13abca1b93..597831e0a16 100644
--- a/gcc/cgraphbuild.cc
+++ b/gcc/cgraphbuild.cc
@@ -31,6 +31,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "gimple-walk.h"
 #include "ipa-utils.h"
 #include "except.h"
+#include "gimplify.h"
 
 /* Context of record_reference.  */
 struct record_reference_ctx
@@ -79,6 +80,17 @@ record_reference (tree *tp, int *walk_subtrees, void *data)
 
       if (VAR_P (decl))
 	{
+	  /* Replace vars with their DECL_VALUE_EXPR if any.
+	     This is normally done during gimplification, but
+	     static var initializers are never gimplified.  */
+	  if (DECL_HAS_VALUE_EXPR_P (decl))
+	    {
+	      tree *p;
+	      for (p = tp; *p != decl; p = &TREE_OPERAND (*p, 0))
+		;
+	      *p = unshare_expr (DECL_VALUE_EXPR (decl));
+	      return record_reference (tp, walk_subtrees, data);
+	    }
 	  varpool_node *vnode = varpool_node::get_create (decl);
 	  ctx->varpool_node->create_reference (vnode, IPA_REF_ADDR);
 	}
diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc
index 92cd309e670..a35cedd05cc 100644
--- a/gcc/cp/cp-gimplify.cc
+++ b/gcc/cp/cp-gimplify.cc
@@ -1035,16 +1035,6 @@ cp_fold_r (tree *stmt_p, int *walk_subtrees, void *data_)
 	}
       break;
 
-    case VAR_DECL:
-      /* In initializers replace anon union artificial VAR_DECLs
-	 with their DECL_VALUE_EXPRs, as nothing will do it later.  */
-      if (DECL_ANON_UNION_VAR_P (stmt) && !data->genericize)
-	{
-	  *stmt_p = stmt = unshare_expr (DECL_VALUE_EXPR (stmt));
-	  break;
-	}
-      break;
-
     default:
       break;
     }
diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp57.C b/gcc/testsuite/g++.dg/cpp1z/decomp57.C
new file mode 100644
index 00000000000..923862e78d1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/decomp57.C
@@ -0,0 +1,27 @@
+// PR c++/108474
+// { dg-do link { target c++17 } }
+
+struct T { int i, j; };
+T h;
+auto [i, j] = h;
+int &r = i;
+int s = i;
+int *t = &i;
+
+void
+foo (int **p, int *q)
+{
+  static int &u = i;
+  static int v = i;
+  static int *w = &i;
+  int &x = i;
+  int y = i;
+  int *z = &i;
+  *p = &i;
+  *q = i;
+}
+
+int
+main ()
+{
+}
diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp58.C b/gcc/testsuite/g++.dg/cpp1z/decomp58.C
new file mode 100644
index 00000000000..b2604373bde
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/decomp58.C
@@ -0,0 +1,39 @@
+// PR c++/108474
+// { dg-do link { target c++17 } }
+
+namespace std {
+  template <typename T> struct tuple_size;
+  template <int, typename> struct tuple_element;
+}
+
+struct A {
+  int i;
+  template <int I> int& get() { return i; }
+};
+
+template <> struct std::tuple_size <A> { static const int value = 2; };
+template <int I> struct std::tuple_element <I, A> { using type = int; };
+
+struct A a;
+auto [i, j] = a;
+int &r = i;
+int s = i;
+int *t = &i;
+
+void
+foo (int **p, int *q)
+{
+  static int &u = i;
+  static int v = i;
+  static int *w = &i;
+  int &x = i;
+  int y = i;
+  int *z = &i;
+  *p = &i;
+  *q = i;
+}
+
+int
+main ()
+{
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-01-24 10:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-24 10:32 [gcc r13-5323] c++: Handle structured bindings like anon unions in initializers [PR108474] Jakub Jelinek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).