Blame SOURCES/gcc8-pr100797.patch

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