public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-6324] c++: Fix initializing empty base from prvalue [PR97597]
@ 2020-12-23 22:11 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2020-12-23 22:11 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:612cf351c700b6216209e3f3f4b3a0959bf2dee7

commit r11-6324-g612cf351c700b6216209e3f3f4b3a0959bf2dee7
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Dec 22 15:41:56 2020 -0500

    c++: Fix initializing empty base from prvalue [PR97597]
    
    unsafe_return_slot_p wasn't recognizing an empty base as
    potentially-overlapping, which it definitely is.
    
    The change to build_base_path is to make the virtual conversion also
    recognized by is_empty_base_ref; unsafe_return_slot_p doesn't to handle
    virtual conversions, but hypothetical future callers might.
    
    gcc/cp/ChangeLog:
    
            PR c++/97597
            * class.c (is_empty_base_ref): New.
            (build_base_path): Add NOP_EXPR after offset.
            * cp-tree.h (is_empty_base_ref): Declare it.
            * call.c (unsafe_return_slot_p): Call it.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/97597
            * g++.dg/init/empty3.C: New test.

Diff:
---
 gcc/cp/call.c                      |  4 ++++
 gcc/cp/class.c                     | 31 +++++++++++++++++++++++++++++--
 gcc/cp/cp-tree.h                   |  1 +
 gcc/testsuite/g++.dg/init/empty3.C | 18 ++++++++++++++++++
 4 files changed, 52 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index c2d62e582bf..65a4d73a7e0 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -8426,6 +8426,10 @@ call_copy_ctor (tree a, tsubst_flags_t complain)
 bool
 unsafe_return_slot_p (tree t)
 {
+  /* Check empty bases separately, they don't have fields.  */
+  if (is_empty_base_ref (t))
+    return true;
+
   STRIP_NOPS (t);
   if (TREE_CODE (t) == ADDR_EXPR)
     t = TREE_OPERAND (t, 0);
diff --git a/gcc/cp/class.c b/gcc/cp/class.c
index bc0d3d6bf86..fc5502aad98 100644
--- a/gcc/cp/class.c
+++ b/gcc/cp/class.c
@@ -494,8 +494,6 @@ build_base_path (enum tree_code code,
   if (want_pointer)
     target_type = ptr_target_type;
 
-  expr = build1 (NOP_EXPR, ptr_target_type, expr);
-
   if (!integer_zerop (offset))
     {
       offset = fold_convert (sizetype, offset);
@@ -506,6 +504,8 @@ build_base_path (enum tree_code code,
   else
     null_test = NULL;
 
+  expr = build1 (NOP_EXPR, ptr_target_type, expr);
+
  indout:
   if (!want_pointer)
     {
@@ -659,6 +659,33 @@ convert_to_base_statically (tree expr, tree base)
   return expr;
 }
 
+/* True IFF EXPR is a reference to an empty base class "subobject", as built in
+   convert_to_base_statically.  We look for the result of the fold_convert
+   call, a NOP_EXPR from one pointer type to another, where the target is an
+   empty base of the original type.  */
+
+bool
+is_empty_base_ref (tree expr)
+{
+  if (TREE_CODE (expr) == INDIRECT_REF)
+    expr = TREE_OPERAND (expr, 0);
+  if (TREE_CODE (expr) != NOP_EXPR)
+    return false;
+  tree type = TREE_TYPE (expr);
+  if (!POINTER_TYPE_P (type))
+    return false;
+  type = TREE_TYPE (type);
+  if (!is_empty_class (type))
+    return false;
+  STRIP_NOPS (expr);
+  tree fromtype = TREE_TYPE (expr);
+  if (!POINTER_TYPE_P (fromtype))
+    return false;
+  fromtype = TREE_TYPE (fromtype);
+  return (CLASS_TYPE_P (fromtype)
+	  && !same_type_ignoring_top_level_qualifiers_p (fromtype, type)
+	  && DERIVED_FROM_P (type, fromtype));
+}
 \f
 tree
 build_vfield_ref (tree datum, tree type)
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 6cce9e2b407..fc6a7898475 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -6484,6 +6484,7 @@ extern tree build_base_path			(enum tree_code, tree,
 extern tree convert_to_base			(tree, tree, bool, bool,
 						 tsubst_flags_t);
 extern tree convert_to_base_statically		(tree, tree);
+extern bool is_empty_base_ref			(tree);
 extern tree build_vtbl_ref			(tree, tree);
 extern tree build_vfn_ref			(tree, tree);
 extern tree get_vtable_decl			(tree, int);
diff --git a/gcc/testsuite/g++.dg/init/empty3.C b/gcc/testsuite/g++.dg/init/empty3.C
new file mode 100644
index 00000000000..510338b2a7e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/init/empty3.C
@@ -0,0 +1,18 @@
+// PR c++/97597
+
+struct pq {
+  pq (const pq &);
+};
+
+struct a9 {
+  operator pq () const;
+};
+
+struct zp : pq {
+  zp (const a9 &k3) : pq (k3) { }
+};
+
+int main()
+{
+  zp z = a9();
+}


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

only message in thread, other threads:[~2020-12-23 22:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-23 22:11 [gcc r11-6324] c++: Fix initializing empty base from prvalue [PR97597] Jason Merrill

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