public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-1114] c++: 'this' adjustment for devirtualized call
Date: Fri, 28 May 2021 12:58:33 +0000 (GMT)	[thread overview]
Message-ID: <20210528125833.3C6D43857C52@sourceware.org> (raw)

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

commit r12-1114-gf838e3ccf8d2849980e9d0f70aa60ecd2eb5772c
Author: Jason Merrill <jason@redhat.com>
Date:   Thu May 27 23:54:52 2021 -0400

    c++: 'this' adjustment for devirtualized call
    
    My patch for 95719 made us do a better job of finding the actual virtual
    function we want to call, but didn't update the 'this' pointer adjustment to
    match.
    
            PR c++/100797
            PR c++/95719
    
    gcc/cp/ChangeLog:
    
            * call.c (build_over_call): Adjust base_binfo in
            resolves_to_fixed_type_p case.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/inherit/virtual15.C: New test.

Diff:
---
 gcc/cp/call.c                             | 22 ++++++++++++++++++----
 gcc/testsuite/g++.dg/inherit/virtual15.C  | 18 ++++++++++++++++++
 gcc/testsuite/g++.dg/inherit/virtual15a.C | 19 +++++++++++++++++++
 3 files changed, 55 insertions(+), 4 deletions(-)

diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 3076fe67d5c..bf524b50bc4 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -9152,18 +9152,32 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
 	  if (base_binfo == error_mark_node)
 	    return error_mark_node;
 	}
-      tree converted_arg = build_base_path (PLUS_EXPR, arg,
-					    base_binfo, 1, complain);
 
       /* If we know the dynamic type of the object, look up the final overrider
 	 in the BINFO.  */
       if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
 	  && resolves_to_fixed_type_p (arg))
 	{
-	  fn = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo);
-	  flags |= LOOKUP_NONVIRTUAL;
+	  tree ov = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo);
+
+	  /* And unwind base_binfo to match.  If we don't find the type we're
+	     looking for in BINFO_INHERITANCE_CHAIN, we're looking at diamond
+	     inheritance; for now do a normal virtual call in that case.  */
+	  tree octx = DECL_CONTEXT (ov);
+	  tree obinfo = base_binfo;
+	  while (obinfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (obinfo), octx))
+	    obinfo = BINFO_INHERITANCE_CHAIN (obinfo);
+	  if (obinfo)
+	    {
+	      fn = ov;
+	      base_binfo = obinfo;
+	      flags |= LOOKUP_NONVIRTUAL;
+	    }
 	}
 
+      tree converted_arg = build_base_path (PLUS_EXPR, arg,
+					    base_binfo, 1, complain);
+
       argarray[j++] = converted_arg;
       parm = TREE_CHAIN (parm);
       if (first_arg != NULL_TREE)
diff --git a/gcc/testsuite/g++.dg/inherit/virtual15.C b/gcc/testsuite/g++.dg/inherit/virtual15.C
new file mode 100644
index 00000000000..ebd8e3ad29b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/inherit/virtual15.C
@@ -0,0 +1,18 @@
+// PR c++/100797
+// { dg-do run }
+
+bool ok = false;
+struct S1 { virtual ~S1() {} };
+struct S2 { virtual void f1() = 0; };
+struct S3: S1, S2 {
+    void f1() { f2(); }
+    virtual void f2() = 0;
+};
+struct S4: S3 {
+  void f2() { ok = true; }
+  using S2::f1;
+};
+int main() {
+  S4().f1();
+  if (!ok) __builtin_abort ();
+}
diff --git a/gcc/testsuite/g++.dg/inherit/virtual15a.C b/gcc/testsuite/g++.dg/inherit/virtual15a.C
new file mode 100644
index 00000000000..6139385192d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/inherit/virtual15a.C
@@ -0,0 +1,19 @@
+// PR c++/100797 plus diamond inheritance
+// { dg-do run }
+
+bool ok = false;
+struct S1 { virtual ~S1() {} };
+struct S2 { virtual void f1() = 0; };
+struct S3: S1, virtual S2 {
+    void f1() { f2(); }
+    virtual void f2() = 0;
+};
+struct SX: virtual S2 { };
+struct S4: SX, S3 {
+  void f2() { ok = true; }
+  using S2::f1;
+};
+int main() {
+  S4().f1();
+  if (!ok) __builtin_abort ();
+}


                 reply	other threads:[~2021-05-28 12:58 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210528125833.3C6D43857C52@sourceware.org \
    --to=jason@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).