blob: bff552867d73542a88911f292208d9069e3c1d8a [file] [log] [blame]
// Copyright (c) 2021, 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;
import com.android.tools.r8.ir.analysis.type.ArrayTypeElement;
import com.android.tools.r8.ir.analysis.type.ClassTypeElement;
import com.android.tools.r8.ir.analysis.type.TypeElement;
import com.google.common.collect.Iterables;
public class DexTypeUtils {
public static DexType computeLeastUpperBound(
AppView<? extends AppInfoWithClassHierarchy> appView, Iterable<DexType> types) {
TypeElement join =
TypeElement.join(Iterables.transform(types, type -> type.toTypeElement(appView)), appView);
return toDexType(appView, join);
}
private static DexType toDexType(
AppView<? extends AppInfoWithClassHierarchy> appView, TypeElement type) {
DexItemFactory dexItemFactory = appView.dexItemFactory();
if (type.isPrimitiveType()) {
return type.asPrimitiveType().toDexType(dexItemFactory);
}
if (type.isArrayType()) {
ArrayTypeElement arrayType = type.asArrayType();
DexType baseType = toDexType(appView, arrayType.getBaseType());
return baseType.toArrayType(arrayType.getNesting(), dexItemFactory);
}
assert type.isClassType();
ClassTypeElement classType = type.asClassType();
if (classType.getClassType() != dexItemFactory.objectType) {
return classType.getClassType();
}
if (classType.getInterfaces().hasSingleKnownInterface()) {
return classType.getInterfaces().getSingleKnownInterface();
}
return dexItemFactory.objectType;
}
}