Make EmulateDispatchCfCode easier to understand

Change-Id: I66b1592276fdb8548fad4fcc936fe266f026ac0a
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/retargeter/DesugaredLibraryRetargeterSyntheticHelper.java b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/retargeter/DesugaredLibraryRetargeterSyntheticHelper.java
index b1e77cd..1a46480 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/retargeter/DesugaredLibraryRetargeterSyntheticHelper.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/desugaredlibrary/retargeter/DesugaredLibraryRetargeterSyntheticHelper.java
@@ -13,7 +13,7 @@
 import com.android.tools.r8.graph.MethodAccessFlags;
 import com.android.tools.r8.ir.desugar.desugaredlibrary.retargeter.DesugaredLibraryRetargeterSynthesizerEventConsumer.DesugaredLibraryRetargeterInstructionEventConsumer;
 import com.android.tools.r8.ir.desugar.desugaredlibrary.retargeter.DesugaredLibraryRetargeterSynthesizerEventConsumer.DesugaredLibraryRetargeterL8SynthesizerEventConsumer;
-import com.android.tools.r8.ir.synthetic.EmulateInterfaceSyntheticCfCodeProvider;
+import com.android.tools.r8.ir.synthetic.EmulateDispatchSyntheticCfCodeProvider;
 import com.android.tools.r8.synthesis.SyntheticClassBuilder;
 import com.android.tools.r8.synthesis.SyntheticNaming.SyntheticKind;
 import com.android.tools.r8.utils.DescriptorUtils;
@@ -164,9 +164,8 @@
               .setCode(
                   methodSig ->
                       appView.options().isDesugaredLibraryCompilation()
-                          ? new EmulateInterfaceSyntheticCfCodeProvider(
+                          ? new EmulateDispatchSyntheticCfCodeProvider(
                                   methodSig.getHolderType(),
-                                  emulatedDispatchMethod.getHolderType(),
                                   desugarMethod,
                                   itfMethod,
                                   Collections.emptyList(),
diff --git a/src/main/java/com/android/tools/r8/ir/desugar/itf/ProgramEmulatedInterfaceSynthesizer.java b/src/main/java/com/android/tools/r8/ir/desugar/itf/ProgramEmulatedInterfaceSynthesizer.java
index 5c2ec42..3d91f5c 100644
--- a/src/main/java/com/android/tools/r8/ir/desugar/itf/ProgramEmulatedInterfaceSynthesizer.java
+++ b/src/main/java/com/android/tools/r8/ir/desugar/itf/ProgramEmulatedInterfaceSynthesizer.java
@@ -16,7 +16,7 @@
 import com.android.tools.r8.ir.desugar.CfClassSynthesizerDesugaring;
 import com.android.tools.r8.ir.desugar.CfClassSynthesizerDesugaringEventConsumer;
 import com.android.tools.r8.ir.desugar.itf.EmulatedInterfaceSynthesizerEventConsumer.L8ProgramEmulatedInterfaceSynthesizerEventConsumer;
-import com.android.tools.r8.ir.synthetic.EmulateInterfaceSyntheticCfCodeProvider;
+import com.android.tools.r8.ir.synthetic.EmulateDispatchSyntheticCfCodeProvider;
 import com.android.tools.r8.synthesis.SyntheticMethodBuilder;
 import com.android.tools.r8.synthesis.SyntheticNaming;
 import com.android.tools.r8.synthesis.SyntheticProgramClassBuilder;
@@ -145,9 +145,8 @@
         helper.ensureDefaultAsMethodOfProgramCompanionClassStub(method).getReference();
     List<Pair<DexType, DexMethod>> extraDispatchCases =
         getDispatchCases(method, theInterface, companionMethod);
-    return new EmulateInterfaceSyntheticCfCodeProvider(
+    return new EmulateDispatchSyntheticCfCodeProvider(
             emulatedInterfaceMethod.getHolderType(),
-            method.getHolderType(),
             companionMethod,
             libraryMethod,
             extraDispatchCases,
diff --git a/src/main/java/com/android/tools/r8/ir/synthetic/EmulateInterfaceSyntheticCfCodeProvider.java b/src/main/java/com/android/tools/r8/ir/synthetic/EmulateDispatchSyntheticCfCodeProvider.java
similarity index 69%
rename from src/main/java/com/android/tools/r8/ir/synthetic/EmulateInterfaceSyntheticCfCodeProvider.java
rename to src/main/java/com/android/tools/r8/ir/synthetic/EmulateDispatchSyntheticCfCodeProvider.java
index e1ba3b2..4847e78 100644
--- a/src/main/java/com/android/tools/r8/ir/synthetic/EmulateInterfaceSyntheticCfCodeProvider.java
+++ b/src/main/java/com/android/tools/r8/ir/synthetic/EmulateDispatchSyntheticCfCodeProvider.java
@@ -1,4 +1,4 @@
-// Copyright (c) 2019, the R8 project authors. Please see the AUTHORS file
+// Copyright (c) 2022, the R8 project authors. Please see the AUTHORS file
 // for details. All rights reserved. Use of this source code is governed by a
 // BSD-style license that can be found in the LICENSE file.
 
@@ -28,24 +28,23 @@
 import java.util.List;
 import org.objectweb.asm.Opcodes;
 
-public class EmulateInterfaceSyntheticCfCodeProvider extends SyntheticCfCodeProvider {
+public class EmulateDispatchSyntheticCfCodeProvider extends SyntheticCfCodeProvider {
 
-  private final DexType interfaceType;
-  private final DexMethod companionMethod;
-  private final DexMethod libraryMethod;
+  private final DexType receiverType;
+  private final DexMethod forwardingMethod;
+  private final DexMethod interfaceMethod;
   private final List<Pair<DexType, DexMethod>> extraDispatchCases;
 
-  public EmulateInterfaceSyntheticCfCodeProvider(
+  public EmulateDispatchSyntheticCfCodeProvider(
       DexType holder,
-      DexType interfaceType,
-      DexMethod companionMethod,
-      DexMethod libraryMethod,
+      DexMethod forwardingMethod,
+      DexMethod interfaceMethod,
       List<Pair<DexType, DexMethod>> extraDispatchCases,
       AppView<?> appView) {
     super(appView, holder);
-    this.interfaceType = interfaceType;
-    this.companionMethod = companionMethod;
-    this.libraryMethod = libraryMethod;
+    this.receiverType = forwardingMethod.getParameter(0);
+    this.forwardingMethod = forwardingMethod;
+    this.interfaceMethod = interfaceMethod;
     this.extraDispatchCases = extraDispatchCases;
   }
 
@@ -60,22 +59,22 @@
 
     ImmutableInt2ReferenceSortedMap.Builder<FrameType> localsBuilder =
         ImmutableInt2ReferenceSortedMap.builder();
-    localsBuilder.put(0, FrameType.initialized(interfaceType));
+    localsBuilder.put(0, FrameType.initialized(receiverType));
     int index = 1;
-    for (DexType param : libraryMethod.proto.parameters.values) {
+    for (DexType param : interfaceMethod.proto.parameters.values) {
       localsBuilder.put(index++, FrameType.initialized(param));
     }
     ImmutableInt2ReferenceSortedMap<FrameType> locals = localsBuilder.build();
 
-    instructions.add(new CfLoad(ValueType.fromDexType(interfaceType), 0));
-    instructions.add(new CfInstanceOf(libraryMethod.holder));
+    instructions.add(new CfLoad(ValueType.fromDexType(receiverType), 0));
+    instructions.add(new CfInstanceOf(interfaceMethod.holder));
     instructions.add(new CfIf(If.Type.EQ, ValueType.INT, labels[nextLabel]));
 
     // Branch with library call.
-    instructions.add(new CfLoad(ValueType.fromDexType(interfaceType), 0));
-    instructions.add(new CfCheckCast(libraryMethod.holder));
+    instructions.add(new CfLoad(ValueType.fromDexType(receiverType), 0));
+    instructions.add(new CfCheckCast(interfaceMethod.holder));
     loadExtraParameters(instructions);
-    instructions.add(new CfInvoke(Opcodes.INVOKEINTERFACE, libraryMethod, true));
+    instructions.add(new CfInvoke(Opcodes.INVOKEINTERFACE, interfaceMethod, true));
     addReturn(instructions);
 
     // SubInterface dispatch (subInterfaces are ordered).
@@ -83,12 +82,12 @@
       // Type check basic block.
       instructions.add(labels[nextLabel++]);
       instructions.add(new CfFrame(locals, ImmutableDeque.of()));
-      instructions.add(new CfLoad(ValueType.fromDexType(interfaceType), 0));
+      instructions.add(new CfLoad(ValueType.fromDexType(receiverType), 0));
       instructions.add(new CfInstanceOf(dispatch.getFirst()));
       instructions.add(new CfIf(If.Type.EQ, ValueType.INT, labels[nextLabel]));
 
       // Call basic block.
-      instructions.add(new CfLoad(ValueType.fromDexType(interfaceType), 0));
+      instructions.add(new CfLoad(ValueType.fromDexType(receiverType), 0));
       instructions.add(new CfCheckCast(dispatch.getFirst()));
       loadExtraParameters(instructions);
       instructions.add(new CfInvoke(Opcodes.INVOKESTATIC, dispatch.getSecond(), false));
@@ -98,25 +97,25 @@
     // Branch with companion call.
     instructions.add(labels[nextLabel]);
     instructions.add(new CfFrame(locals, ImmutableDeque.of()));
-    instructions.add(new CfLoad(ValueType.fromDexType(interfaceType), 0));
+    instructions.add(new CfLoad(ValueType.fromDexType(receiverType), 0));
     loadExtraParameters(instructions);
-    instructions.add(new CfInvoke(Opcodes.INVOKESTATIC, companionMethod, false));
+    instructions.add(new CfInvoke(Opcodes.INVOKESTATIC, forwardingMethod, false));
     addReturn(instructions);
     return standardCfCodeFromInstructions(instructions);
   }
 
   private void loadExtraParameters(List<CfInstruction> instructions) {
     int index = 1;
-    for (DexType type : libraryMethod.proto.parameters.values) {
+    for (DexType type : interfaceMethod.proto.parameters.values) {
       instructions.add(new CfLoad(ValueType.fromDexType(type), index++));
     }
   }
 
   private void addReturn(List<CfInstruction> instructions) {
-    if (libraryMethod.proto.returnType == appView.dexItemFactory().voidType) {
+    if (interfaceMethod.proto.returnType == appView.dexItemFactory().voidType) {
       instructions.add(new CfReturnVoid());
     } else {
-      instructions.add(new CfReturn(ValueType.fromDexType(libraryMethod.proto.returnType)));
+      instructions.add(new CfReturn(ValueType.fromDexType(interfaceMethod.proto.returnType)));
     }
   }
 }