blob: 3a9f6189b91d210f0e69bf116b612f0309d352be [file] [log] [blame]
// Copyright (c) 2019, 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.
package com.android.tools.r8.graph.classmerging;
import com.android.tools.r8.graph.AppView;
import com.android.tools.r8.graph.DexType;
import com.android.tools.r8.ir.optimize.lambda.LambdaGroup;
import com.android.tools.r8.shaking.AppInfoWithLiveness;
import com.android.tools.r8.utils.collections.BidirectionalManyToOneHashMap;
import com.android.tools.r8.utils.collections.BidirectionalManyToOneMap;
import com.android.tools.r8.utils.collections.MutableBidirectionalManyToOneMap;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
public class HorizontallyMergedLambdaClasses implements MergedClasses {
private final BidirectionalManyToOneMap<DexType, DexType> mergedClasses;
public HorizontallyMergedLambdaClasses(Map<DexType, LambdaGroup> lambdas) {
MutableBidirectionalManyToOneMap<DexType, DexType> mergedClasses =
new BidirectionalManyToOneHashMap<>();
lambdas.forEach((lambda, group) -> mergedClasses.put(lambda, group.getGroupClassType()));
this.mergedClasses = mergedClasses;
}
public static HorizontallyMergedLambdaClasses empty() {
return new HorizontallyMergedLambdaClasses(Collections.emptyMap());
}
@Override
public void forEachMergeGroup(BiConsumer<Set<DexType>, DexType> consumer) {
mergedClasses.forEachManyToOneMapping(consumer);
}
@Override
public boolean hasBeenMergedIntoDifferentType(DexType type) {
return mergedClasses.containsKey(type);
}
@Override
public boolean isMergeTarget(DexType type) {
return mergedClasses.containsValue(type);
}
@Override
public boolean verifyAllSourcesPruned(AppView<AppInfoWithLiveness> appView) {
for (DexType source : mergedClasses.keySet()) {
assert appView.appInfo().wasPruned(source)
: "Expected horizontally merged lambda class `"
+ source.toSourceString()
+ "` to be absent";
}
return true;
}
}