Blame SOURCES/gcc8-pr100797.patch

a54273
commit ebfe8b28d40746ff33724bd5b9ade2552e619213
a54273
Author: Jason Merrill <jason@redhat.com>
a54273
Date:   Thu May 27 23:54:52 2021 -0400
a54273
a54273
    c++: 'this' adjustment for devirtualized call
a54273
    
a54273
    My patch for 95719 made us do a better job of finding the actual virtual
a54273
    function we want to call, but didn't update the 'this' pointer adjustment to
a54273
    match.
a54273
    
a54273
    This backport also incorporates a bit of the r11-1638 reorganization.
a54273
    
a54273
            PR c++/100797
a54273
            PR c++/95719
a54273
    
a54273
    gcc/cp/ChangeLog:
a54273
    
a54273
            * call.c (build_over_call): Adjust base_binfo in
a54273
            resolves_to_fixed_type_p case.
a54273
    
a54273
    gcc/testsuite/ChangeLog:
a54273
    
a54273
            * g++.dg/inherit/virtual15.C: New test.
a54273
            * g++.dg/inherit/virtual15a.C: New test.
a54273
a54273
--- gcc/cp/call.c
a54273
+++ gcc/cp/call.c
a54273
@@ -8309,19 +8309,6 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
a54273
 	  || CLASSTYPE_FINAL (TYPE_METHOD_BASETYPE (TREE_TYPE (fn))))
a54273
 	flags |= LOOKUP_NONVIRTUAL;
a54273
 
a54273
-      /* If we know the dynamic type of the object, look up the final overrider
a54273
-	 in the BINFO.  */
a54273
-      if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
a54273
-	  && resolves_to_fixed_type_p (arg))
a54273
-	{
a54273
-	  tree binfo = cand->conversion_path;
a54273
-	  if (BINFO_TYPE (binfo) != DECL_CONTEXT (fn))
a54273
-	    binfo = lookup_base (binfo, DECL_CONTEXT (fn), ba_unique,
a54273
-				 NULL, complain);
a54273
-	  fn = lookup_vfn_in_binfo (DECL_VINDEX (fn), binfo);
a54273
-	  flags |= LOOKUP_NONVIRTUAL;
a54273
-	}
a54273
-
a54273
       /* [class.mfct.nonstatic]: If a nonstatic member function of a class
a54273
 	 X is called for an object that is not of type X, or of a type
a54273
 	 derived from X, the behavior is undefined.
a54273
@@ -8331,10 +8318,6 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
a54273
       gcc_assert (TYPE_PTR_P (parmtype));
a54273
       /* Convert to the base in which the function was declared.  */
a54273
       gcc_assert (cand->conversion_path != NULL_TREE);
a54273
-      converted_arg = build_base_path (PLUS_EXPR,
a54273
-				       arg,
a54273
-				       cand->conversion_path,
a54273
-				       1, complain);
a54273
       /* Check that the base class is accessible.  */
a54273
       if (!accessible_base_p (TREE_TYPE (argtype),
a54273
 			      BINFO_TYPE (cand->conversion_path), true))
a54273
@@ -8349,10 +8332,33 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
a54273
       /* If fn was found by a using declaration, the conversion path
a54273
 	 will be to the derived class, not the base declaring fn. We
a54273
 	 must convert from derived to base.  */
a54273
-      base_binfo = lookup_base (TREE_TYPE (TREE_TYPE (converted_arg)),
a54273
+      base_binfo = lookup_base (cand->conversion_path,
a54273
 				TREE_TYPE (parmtype), ba_unique,
a54273
 				NULL, complain);
a54273
-      converted_arg = build_base_path (PLUS_EXPR, converted_arg,
a54273
+
a54273
+      /* If we know the dynamic type of the object, look up the final overrider
a54273
+	 in the BINFO.  */
a54273
+      if (DECL_VINDEX (fn) && (flags & LOOKUP_NONVIRTUAL) == 0
a54273
+	  && resolves_to_fixed_type_p (arg))
a54273
+	{
a54273
+	  tree ov = lookup_vfn_in_binfo (DECL_VINDEX (fn), base_binfo);
a54273
+
a54273
+	  /* And unwind base_binfo to match.  If we don't find the type we're
a54273
+	     looking for in BINFO_INHERITANCE_CHAIN, we're looking at diamond
a54273
+	     inheritance; for now do a normal virtual call in that case.  */
a54273
+	  tree octx = DECL_CONTEXT (ov);
a54273
+	  tree obinfo = base_binfo;
a54273
+	  while (obinfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (obinfo), octx))
a54273
+	    obinfo = BINFO_INHERITANCE_CHAIN (obinfo);
a54273
+	  if (obinfo)
a54273
+	    {
a54273
+	      fn = ov;
a54273
+	      base_binfo = obinfo;
a54273
+	      flags |= LOOKUP_NONVIRTUAL;
a54273
+	    }
a54273
+	}
a54273
+
a54273
+      converted_arg = build_base_path (PLUS_EXPR, arg,
a54273
 				       base_binfo, 1, complain);
a54273
 
a54273
       argarray[j++] = converted_arg;
a54273
--- /dev/null
a54273
+++ gcc/testsuite/g++.dg/inherit/virtual15.C
a54273
@@ -0,0 +1,18 @@
a54273
+// PR c++/100797
a54273
+// { dg-do run }
a54273
+
a54273
+bool ok = false;
a54273
+struct S1 { virtual ~S1() {} };
a54273
+struct S2 { virtual void f1() = 0; };
a54273
+struct S3: S1, S2 {
a54273
+    void f1() { f2(); }
a54273
+    virtual void f2() = 0;
a54273
+};
a54273
+struct S4: S3 {
a54273
+  void f2() { ok = true; }
a54273
+  using S2::f1;
a54273
+};
a54273
+int main() {
a54273
+  S4().f1();
a54273
+  if (!ok) __builtin_abort ();
a54273
+}
a54273
--- /dev/null
a54273
+++ gcc/testsuite/g++.dg/inherit/virtual15a.C
a54273
@@ -0,0 +1,19 @@
a54273
+// PR c++/100797 plus diamond inheritance
a54273
+// { dg-do run }
a54273
+
a54273
+bool ok = false;
a54273
+struct S1 { virtual ~S1() {} };
a54273
+struct S2 { virtual void f1() = 0; };
a54273
+struct S3: S1, virtual S2 {
a54273
+    void f1() { f2(); }
a54273
+    virtual void f2() = 0;
a54273
+};
a54273
+struct SX: virtual S2 { };
a54273
+struct S4: SX, S3 {
a54273
+  void f2() { ok = true; }
a54273
+  using S2::f1;
a54273
+};
a54273
+int main() {
a54273
+  S4().f1();
a54273
+  if (!ok) __builtin_abort ();
a54273
+}