Compute super target from program context for virtual method merger

This will also change SourceCodeProvider to have ProgramMethod context
as parameter.

Bug: 187038790
Bug: 187374657
Change-Id: I3bfb0dea258d1f4b2adc4bacb8c34237e308f8c0
diff --git a/src/main/java/com/android/tools/r8/horizontalclassmerging/VirtualMethodMerger.java b/src/main/java/com/android/tools/r8/horizontalclassmerging/VirtualMethodMerger.java
index cb810de..7a13785 100644
--- a/src/main/java/com/android/tools/r8/horizontalclassmerging/VirtualMethodMerger.java
+++ b/src/main/java/com/android/tools/r8/horizontalclassmerging/VirtualMethodMerger.java
@@ -275,7 +275,8 @@
             group.getClassIdField(),
             superMethod,
             newMethodReference,
-            bridgeMethodReference);
+            bridgeMethodReference,
+            appView.dexItemFactory());
     DexEncodedMethod newMethod =
         new DexEncodedMethod(
             newMethodReference,
diff --git a/src/main/java/com/android/tools/r8/horizontalclassmerging/code/ConstructorEntryPointSynthesizedCode.java b/src/main/java/com/android/tools/r8/horizontalclassmerging/code/ConstructorEntryPointSynthesizedCode.java
index 8c649b0..da12b74 100644
--- a/src/main/java/com/android/tools/r8/horizontalclassmerging/code/ConstructorEntryPointSynthesizedCode.java
+++ b/src/main/java/com/android/tools/r8/horizontalclassmerging/code/ConstructorEntryPointSynthesizedCode.java
@@ -31,7 +31,7 @@
 
   @Override
   public SourceCodeProvider getSourceCodeProvider() {
-    return callerPosition ->
+    return (ignored, callerPosition) ->
         new ConstructorEntryPoint(
             typeConstructors, newConstructor, classIdField, callerPosition, originalMethod);
   }
diff --git a/src/main/java/com/android/tools/r8/horizontalclassmerging/code/VirtualMethodEntryPointSynthesizedCode.java b/src/main/java/com/android/tools/r8/horizontalclassmerging/code/VirtualMethodEntryPointSynthesizedCode.java
index 3995fd8..4402a83 100644
--- a/src/main/java/com/android/tools/r8/horizontalclassmerging/code/VirtualMethodEntryPointSynthesizedCode.java
+++ b/src/main/java/com/android/tools/r8/horizontalclassmerging/code/VirtualMethodEntryPointSynthesizedCode.java
@@ -5,7 +5,9 @@
 package com.android.tools.r8.horizontalclassmerging.code;
 
 import com.android.tools.r8.graph.DexField;
+import com.android.tools.r8.graph.DexItemFactory;
 import com.android.tools.r8.graph.DexMethod;
+import com.android.tools.r8.graph.ProgramMethod;
 import com.android.tools.r8.graph.UseRegistry;
 import com.android.tools.r8.horizontalclassmerging.VirtualMethodEntryPoint;
 import com.android.tools.r8.ir.synthetic.SynthesizedCode;
@@ -15,20 +17,38 @@
 public class VirtualMethodEntryPointSynthesizedCode extends SynthesizedCode {
   private final Int2ReferenceSortedMap<DexMethod> mappedMethods;
 
+  private final DexItemFactory factory;
+
   public VirtualMethodEntryPointSynthesizedCode(
       Int2ReferenceSortedMap<DexMethod> mappedMethods,
       DexField classIdField,
       DexMethod superMethod,
       DexMethod method,
-      DexMethod originalMethod) {
+      DexMethod originalMethod,
+      DexItemFactory factory) {
     super(
-        position ->
+        (context, position) ->
             new VirtualMethodEntryPoint(
-                mappedMethods, classIdField, superMethod, method, position, originalMethod));
-
+                mappedMethods,
+                classIdField,
+                computeSuperMethodTarget(superMethod, context, factory),
+                method,
+                position,
+                originalMethod));
+    this.factory = factory;
     this.mappedMethods = mappedMethods;
   }
 
+  private static DexMethod computeSuperMethodTarget(
+      DexMethod superMethod, ProgramMethod method, DexItemFactory factory) {
+    // We are only using superMethod as a bit but if this is changed to generate CfCode directly,
+    // the superMethod needs to be computed by mapping through the lens.
+    if (superMethod == null) {
+      return null;
+    }
+    return method.getReference().withHolder(method.getHolder().superType, factory);
+  }
+
   @Override
   public Consumer<UseRegistry> getRegistryCallback() {
     return this::registerReachableDefinitions;
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/itf/ClassProcessor.java b/src/main/java/com/android/tools/r8/ir/desugar/itf/ClassProcessor.java
index 956cd2c..4c784f0 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/itf/ClassProcessor.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/itf/ClassProcessor.java
@@ -765,7 +765,7 @@
             DexAnnotationSet.empty(),
             ParameterAnnotationsList.empty(),
             new SynthesizedCode(
-                callerPosition ->
+                (ignored, callerPosition) ->
                     new ExceptionThrowingSourceCode(clazz.type, method, callerPosition, errorType)),
             true);
     addSyntheticMethod(clazz.asProgramClass(), newEncodedMethod);
diff --git a/src/main/java/com/android/tools/r8/ir/synthetic/AbstractSynthesizedCode.java b/src/main/java/com/android/tools/r8/ir/synthetic/AbstractSynthesizedCode.java
index ad43a6c..4dc56de 100644
--- a/src/main/java/com/android/tools/r8/ir/synthetic/AbstractSynthesizedCode.java
+++ b/src/main/java/com/android/tools/r8/ir/synthetic/AbstractSynthesizedCode.java
@@ -25,7 +25,7 @@
 public abstract class AbstractSynthesizedCode extends Code {
 
   public interface SourceCodeProvider {
-    SourceCode get(Position callerPosition);
+    SourceCode get(ProgramMethod context, Position callerPosition);
   }
 
   public abstract SourceCodeProvider getSourceCodeProvider();
@@ -39,7 +39,7 @@
 
   @Override
   public final IRCode buildIR(ProgramMethod method, AppView<?> appView, Origin origin) {
-    return IRBuilder.create(method, appView, getSourceCodeProvider().get(null), origin)
+    return IRBuilder.create(method, appView, getSourceCodeProvider().get(method, null), origin)
         .build(method);
   }
 
@@ -55,7 +55,7 @@
     return IRBuilder.createForInlining(
             method,
             appView,
-            getSourceCodeProvider().get(callerPosition),
+            getSourceCodeProvider().get(context, callerPosition),
             origin,
             methodProcessor,
             valueNumberGenerator)
diff --git a/src/main/java/com/android/tools/r8/ir/synthetic/ForwardMethodSourceCode.java b/src/main/java/com/android/tools/r8/ir/synthetic/ForwardMethodSourceCode.java
index e5a79bb..9e708cc 100644
--- a/src/main/java/com/android/tools/r8/ir/synthetic/ForwardMethodSourceCode.java
+++ b/src/main/java/com/android/tools/r8/ir/synthetic/ForwardMethodSourceCode.java
@@ -7,6 +7,7 @@
 import com.android.tools.r8.errors.Unimplemented;
 import com.android.tools.r8.graph.DexMethod;
 import com.android.tools.r8.graph.DexType;
+import com.android.tools.r8.graph.ProgramMethod;
 import com.android.tools.r8.ir.code.Invoke;
 import com.android.tools.r8.ir.code.Invoke.Type;
 import com.android.tools.r8.ir.code.Position;
@@ -86,7 +87,7 @@
       return this;
     }
 
-    public ForwardMethodSourceCode build(Position callerPosition) {
+    public ForwardMethodSourceCode build(ProgramMethod context, Position callerPosition) {
       return new ForwardMethodSourceCode(
           receiver,
           method,
diff --git a/src/test/java/com/android/tools/r8/maindexlist/MainDexListTests.java b/src/test/java/com/android/tools/r8/maindexlist/MainDexListTests.java
index 36ebfd8..a01f7dc 100644
--- a/src/test/java/com/android/tools/r8/maindexlist/MainDexListTests.java
+++ b/src/test/java/com/android/tools/r8/maindexlist/MainDexListTests.java
@@ -854,7 +854,7 @@
                 DexString.EMPTY_ARRAY);
         Code code =
             new SynthesizedCode(
-                callerPosition -> new ReturnVoidCode(voidReturnMethod, callerPosition));
+                (ignored, callerPosition) -> new ReturnVoidCode(voidReturnMethod, callerPosition));
         DexEncodedMethod method =
             new DexEncodedMethod(
                 voidReturnMethod,