Compute the set of synthetics after finalization.

This is a prerequisite for using the synthetic contexts to
determine placement of synthetics during writing.

Change-Id: If198b8fbe51ad40c363b0b9be1b34b8768770d1f
diff --git a/src/main/java/com/android/tools/r8/synthesis/SyntheticFinalization.java b/src/main/java/com/android/tools/r8/synthesis/SyntheticFinalization.java
index 128fcef..a802761 100644
--- a/src/main/java/com/android/tools/r8/synthesis/SyntheticFinalization.java
+++ b/src/main/java/com/android/tools/r8/synthesis/SyntheticFinalization.java
@@ -45,6 +45,7 @@
 import java.util.Map.Entry;
 import java.util.Set;
 import java.util.TreeSet;
+import java.util.function.BiConsumer;
 import java.util.function.Function;
 
 public class SyntheticFinalization {
@@ -215,8 +216,12 @@
     assert verifyNoNestedSynthetics();
     DexApplication application;
     MainDexClasses mainDexClasses = appView.appInfo().getMainDexClasses();
-    List<DexProgramClass> finalSyntheticClasses = new ArrayList<>();
     Builder lensBuilder = new Builder();
+    ImmutableMap.Builder<DexType, SyntheticMethodReference> finalMethodsBuilder =
+        ImmutableMap.builder();
+    ImmutableMap.Builder<DexType, SyntheticProgramClassReference> finalClassesBuilder =
+        ImmutableMap.builder();
+    List<DexProgramClass> finalSyntheticProgramDefinitions = new ArrayList<>();
     {
       Map<DexType, NumberGenerator> generators = new IdentityHashMap<>();
       application =
@@ -226,22 +231,33 @@
               computeEquivalences(appView, synthetics.getNonLegacyClasses().values(), generators),
               mainDexClasses,
               lensBuilder,
-              finalSyntheticClasses);
+              (clazz, reference) -> {
+                finalSyntheticProgramDefinitions.add(clazz);
+                finalClassesBuilder.put(clazz.getType(), reference);
+              },
+              (clazz, reference) -> {
+                finalSyntheticProgramDefinitions.add(clazz);
+                finalMethodsBuilder.put(clazz.getType(), reference);
+              });
     }
+    ImmutableMap<DexType, SyntheticMethodReference> finalMethods = finalMethodsBuilder.build();
+    ImmutableMap<DexType, SyntheticProgramClassReference> finalClasses =
+        finalClassesBuilder.build();
 
     handleSynthesizedClassMapping(
-        finalSyntheticClasses, application, options, mainDexClasses, lensBuilder.typeMap);
+        finalSyntheticProgramDefinitions,
+        application,
+        options,
+        mainDexClasses,
+        lensBuilder.typeMap);
 
     assert appView.appInfo().getMainDexClasses() == mainDexClasses;
 
-    Set<DexType> finalSyntheticTypes = Sets.newIdentityHashSet();
-    finalSyntheticClasses.forEach(clazz -> finalSyntheticTypes.add(clazz.getType()));
-
     Set<DexType> prunedSynthetics = Sets.newIdentityHashSet();
     synthetics.forEachNonLegacyItem(
         reference -> {
           DexType type = reference.getHolder();
-          if (!finalSyntheticTypes.contains(type)) {
+          if (!finalMethods.containsKey(type) && !finalClasses.containsKey(type)) {
             prunedSynthetics.add(type);
           }
         });
@@ -251,7 +267,7 @@
             SyntheticItems.INVALID_ID_AFTER_SYNTHETIC_FINALIZATION,
             application,
             new CommittedSyntheticsCollection(
-                synthetics.getLegacyTypes(), ImmutableMap.of(), ImmutableMap.of()),
+                synthetics.getLegacyTypes(), finalMethods, finalClasses),
             ImmutableList.of()),
         lensBuilder.build(appView.graphLens(), appView.dexItemFactory()),
         PrunedItems.builder()
@@ -383,9 +399,11 @@
       Map<DexType, EquivalenceGroup<SyntheticProgramClassDefinition>> syntheticClassGroups,
       MainDexClasses mainDexClasses,
       Builder lensBuilder,
-      List<DexProgramClass> newSyntheticClasses) {
+      BiConsumer<DexProgramClass, SyntheticProgramClassReference> addFinalSyntheticClass,
+      BiConsumer<DexProgramClass, SyntheticMethodReference> addFinalSyntheticMethod) {
     DexApplication application = appView.appInfo().app();
     DexItemFactory factory = appView.dexItemFactory();
+    List<DexProgramClass> newProgramClasses = new ArrayList<>();
 
     // TODO(b/168584485): Remove this once class-mapping support is removed.
     Set<DexType> derivedMainDexTypes = Sets.newIdentityHashSet();
@@ -409,7 +427,7 @@
           context.registerPrefixRewriting(syntheticType, appView);
           DexProgramClass externalSyntheticClass =
               createExternalMethodClass(syntheticType, representative, factory);
-          newSyntheticClasses.add(externalSyntheticClass);
+          newProgramClasses.add(externalSyntheticClass);
           addSyntheticMarker(representative.getKind(), externalSyntheticClass, context, appView);
           assert externalSyntheticClass.getMethodCollection().size() == 1;
           DexEncodedMethod externalSyntheticMethod =
@@ -430,7 +448,7 @@
           SynthesizingContext context = representative.getContext();
           context.registerPrefixRewriting(syntheticType, appView);
           DexProgramClass externalSyntheticClass = representative.getHolder();
-          newSyntheticClasses.add(externalSyntheticClass);
+          newProgramClasses.add(externalSyntheticClass);
           addSyntheticMarker(representative.getKind(), externalSyntheticClass, context, appView);
           for (SyntheticProgramClassDefinition member : syntheticGroup.getMembers()) {
             DexProgramClass memberClass = member.getHolder();
@@ -446,7 +464,6 @@
           }
         });
 
-    List<DexProgramClass> newProgramClasses = new ArrayList<>(newSyntheticClasses);
     for (DexProgramClass clazz : application.classes()) {
       if (!pruned.contains(clazz.type)) {
         newProgramClasses.add(clazz);
@@ -460,8 +477,6 @@
       assert application.definitionFor(key) != null;
     }
 
-    newSyntheticClasses.clear();
-
     DexApplication.Builder<?> builder = application.builder();
     TreeFixerBase treeFixer =
         new TreeFixerBase(appView) {
@@ -494,7 +509,8 @@
     syntheticClassGroups.forEach(
         (syntheticType, syntheticGroup) -> {
           DexProgramClass externalSyntheticClass = appForLookup.programDefinitionFor(syntheticType);
-          newSyntheticClasses.add(externalSyntheticClass);
+          addFinalSyntheticClass.accept(
+              externalSyntheticClass, syntheticGroup.getRepresentative().toReference());
           for (SyntheticProgramClassDefinition member : syntheticGroup.getMembers()) {
             addMainDexAndSynthesizedFromForMember(
                 member,
@@ -507,7 +523,8 @@
     syntheticMethodGroups.forEach(
         (syntheticType, syntheticGroup) -> {
           DexProgramClass externalSyntheticClass = appForLookup.programDefinitionFor(syntheticType);
-          newSyntheticClasses.add(externalSyntheticClass);
+          addFinalSyntheticMethod.accept(
+              externalSyntheticClass, syntheticGroup.getRepresentative().toReference());
           for (SyntheticMethodDefinition member : syntheticGroup.getMembers()) {
             addMainDexAndSynthesizedFromForMember(
                 member,