Blame SOURCES/gcc8-pr100797.patch

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