public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-5503] c++: non-dependent .* operand folding [PR112427]
@ 2023-11-15 17:10 Patrick Palka
  0 siblings, 0 replies; only message in thread
From: Patrick Palka @ 2023-11-15 17:10 UTC (permalink / raw)
  To: gcc-cvs

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

commit r14-5503-gd3f48f682271ed94ab6e9f6bc62418a62bd8ff26
Author: Patrick Palka <ppalka@redhat.com>
Date:   Wed Nov 15 12:10:16 2023 -0500

    c++: non-dependent .* operand folding [PR112427]
    
    Here when building up the non-dependent .* expression, we crash from
    fold_convert on 'b.a' due to this (templated) COMPONENT_REF having an
    IDENTIFIER_NODE instead of FIELD_DECL operand that middle-end routines
    expect.  Like in r14-4899-gd80a26cca02587, this patch fixes this by
    replacing the problematic piecemeal folding with a single call to
    cp_fully_fold.  Also, don't bother building the POINTER_PLUS_EXPR in a
    template context.  This means the returned non-dependent tree might not
    have TREE_SIDE_EFFECTS set when it used to, so we need to compensate
    by making build_min_non_dep propagate TREE_SIDE_EFFECTS from the original
    arguments like buildN and build_min do.
    
            PR c++/112427
    
    gcc/cp/ChangeLog:
    
            * tree.cc (build_min_non_dep): Propagate TREE_SIDE_EFFECTS from
            the original arguments.
            (build_min_non_dep_call_vec): Likewise.
            * typeck2.cc (build_m_component_ref): Use cp_convert, build2 and
            cp_fully_fold instead of fold_build_pointer_plus and fold_convert.
            Don't build the POINTER_PLUS_EXPR in a template context.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/template/non-dependent29.C: New test.

Diff:
---
 gcc/cp/tree.cc                                  | 11 ++++++++++-
 gcc/cp/typeck2.cc                               |  6 +++++-
 gcc/testsuite/g++.dg/template/non-dependent29.C | 13 +++++++++++++
 3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index 417c92ba76f..dc4126f935c 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -3601,7 +3601,12 @@ build_min_non_dep (enum tree_code code, tree non_dep, ...)
   TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
 
   for (i = 0; i < length; i++)
-    TREE_OPERAND (t, i) = va_arg (p, tree);
+    {
+      tree x = va_arg (p, tree);
+      TREE_OPERAND (t, i) = x;
+      if (x && !TYPE_P (x))
+	TREE_SIDE_EFFECTS (t) |= TREE_SIDE_EFFECTS (x);
+    }
 
   va_end (p);
   return convert_from_reference (t);
@@ -3636,6 +3641,10 @@ build_min_non_dep_call_vec (tree non_dep, tree fn, vec<tree, va_gc> *argvec)
     non_dep = TREE_OPERAND (non_dep, 0);
   TREE_TYPE (t) = TREE_TYPE (non_dep);
   TREE_SIDE_EFFECTS (t) = TREE_SIDE_EFFECTS (non_dep);
+  if (argvec)
+    for (tree x : *argvec)
+      if (x && !TYPE_P (x))
+	TREE_SIDE_EFFECTS (t) |= TREE_SIDE_EFFECTS (x);
   return convert_from_reference (t);
 }
 
diff --git a/gcc/cp/typeck2.cc b/gcc/cp/typeck2.cc
index 309903afed8..a75f4f8c2d2 100644
--- a/gcc/cp/typeck2.cc
+++ b/gcc/cp/typeck2.cc
@@ -2378,7 +2378,11 @@ build_m_component_ref (tree datum, tree component, tsubst_flags_t complain)
       /* Build an expression for "object + offset" where offset is the
 	 value stored in the pointer-to-data-member.  */
       ptype = build_pointer_type (type);
-      datum = fold_build_pointer_plus (fold_convert (ptype, datum), component);
+      datum = cp_convert (ptype, datum, complain);
+      if (!processing_template_decl)
+	datum = build2 (POINTER_PLUS_EXPR, ptype,
+			datum, convert_to_ptrofftype (component));
+      datum = cp_fully_fold (datum);
       datum = cp_build_fold_indirect_ref (datum);
       if (datum == error_mark_node)
 	return error_mark_node;
diff --git a/gcc/testsuite/g++.dg/template/non-dependent29.C b/gcc/testsuite/g++.dg/template/non-dependent29.C
new file mode 100644
index 00000000000..41bd11ae6b4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/non-dependent29.C
@@ -0,0 +1,13 @@
+// PR c++/112427
+
+struct A { int m; void f(); };
+struct B { A a; };
+
+template<class T>
+void f(B b) {
+  int A::*pd = &A::m;
+  b.a.*pd;
+
+  void (A::*pf)() = &A::f;
+  (b.a.*pf)();
+}

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

only message in thread, other threads:[~2023-11-15 17:10 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-15 17:10 [gcc r14-5503] c++: non-dependent .* operand folding [PR112427] Patrick Palka

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).