Move/inline stuff from TypeUtils to other modules

This commit is contained in:
Alexander Udalov
2015-10-24 02:33:32 +03:00
parent 5677ff60e7
commit 7046f63511
4 changed files with 71 additions and 87 deletions
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.resolve.calls
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.ReflectionTypes
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.resolve.FunctionDescriptorUtil
@@ -43,7 +44,6 @@ import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.TypeUtils.DONT_CARE
import org.jetbrains.kotlin.types.TypeUtils.makeConstantSubstitutor
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils
@@ -324,4 +324,16 @@ fun getResolutionResultsCachedData(expression: KtExpression?, context: Resolutio
val argumentCall = expression?.getCall(context.trace.getBindingContext()) ?: return null
return context.resolutionResultsCache[argumentCall]
}
}
fun makeConstantSubstitutor(typeParameterDescriptors: Collection<TypeParameterDescriptor>, type: KotlinType): TypeSubstitutor {
val constructors = typeParameterDescriptors.map { it.typeConstructor }.toSet()
val projection = TypeProjectionImpl(type)
return TypeSubstitutor.create(object : TypeConstructorSubstitution() {
override operator fun get(key: TypeConstructor) =
if (key in constructors) projection else null
override fun isEmpty() = false
})
}
@@ -29,10 +29,10 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt;
import org.jetbrains.kotlin.resolve.scopes.KtScope;
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
import org.jetbrains.kotlin.types.typeUtil.TypeUtilsKt;
import org.jetbrains.kotlin.utils.DFS;
import java.util.*;
import static org.jetbrains.kotlin.types.TypeUtils.topologicallySortSuperclassesAndRecordAllInstances;
import static org.jetbrains.kotlin.types.Variance.IN_VARIANCE;
import static org.jetbrains.kotlin.types.Variance.OUT_VARIANCE;
@@ -329,4 +329,57 @@ public class CommonSupertypes {
markAll(type.getConstructor(), markerSet);
}
}
@NotNull
public static List<TypeConstructor> topologicallySortSuperclassesAndRecordAllInstances(
@NotNull KotlinType type,
@NotNull final Map<TypeConstructor, Set<KotlinType>> constructorToAllInstances,
@NotNull final Set<TypeConstructor> visited
) {
return DFS.dfs(
Collections.singletonList(type),
new DFS.Neighbors<KotlinType>() {
@NotNull
@Override
public Iterable<KotlinType> getNeighbors(KotlinType current) {
TypeSubstitutor substitutor = TypeSubstitutor.create(current);
Collection<KotlinType> supertypes = current.getConstructor().getSupertypes();
List<KotlinType> result = new ArrayList<KotlinType>(supertypes.size());
for (KotlinType supertype : supertypes) {
if (visited.contains(supertype.getConstructor())) {
continue;
}
result.add(substitutor.safeSubstitute(supertype, Variance.INVARIANT));
}
return result;
}
},
new DFS.Visited<KotlinType>() {
@Override
public boolean checkAndMarkVisited(KotlinType current) {
return visited.add(current.getConstructor());
}
},
new DFS.NodeHandlerWithListResult<KotlinType, TypeConstructor>() {
@Override
public boolean beforeChildren(KotlinType current) {
TypeConstructor constructor = current.getConstructor();
Set<KotlinType> instances = constructorToAllInstances.get(constructor);
if (instances == null) {
instances = new HashSet<KotlinType>();
constructorToAllInstances.put(constructor, instances);
}
instances.add(current);
return true;
}
@Override
public void afterChildren(KotlinType current) {
result.addFirst(current.getConstructor());
}
}
);
}
}
@@ -28,14 +28,11 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.resolve.constants.IntegerValueTypeConstructor;
import org.jetbrains.kotlin.resolve.scopes.KtScope;
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
import org.jetbrains.kotlin.utils.DFS;
import java.util.*;
public class TypeUtils {
public static final KotlinType DONT_CARE = ErrorUtils.createErrorTypeWithCustomDebugName("DONT_CARE");
public static final KotlinType PLACEHOLDER_FUNCTION_TYPE = ErrorUtils.createErrorTypeWithCustomDebugName("PLACEHOLDER_FUNCTION_TYPE");
public static final KotlinType CANT_INFER_FUNCTION_PARAM_TYPE = ErrorUtils.createErrorType("Cannot be inferred");
public static class SpecialType implements KotlinType {
@@ -331,10 +328,8 @@ public class TypeUtils {
if (FlexibleTypesKt.isFlexible(type) && acceptsNullable(FlexibleTypesKt.flexibility(type).getUpperBound())) {
return true;
}
if (isTypeParameter(type)) {
return hasNullableLowerBound((TypeParameterDescriptor) type.getConstructor().getDeclarationDescriptor());
}
return false;
TypeParameterDescriptor typeParameterDescriptor = getTypeParameterDescriptorOrNull(type);
return typeParameterDescriptor != null && hasNullableLowerBound(typeParameterDescriptor);
}
public static boolean hasNullableSuperType(@NotNull KotlinType type) {
@@ -493,82 +488,6 @@ public class TypeUtils {
return getDefaultPrimitiveNumberType(numberValueTypeConstructor);
}
public static List<TypeConstructor> topologicallySortSuperclassesAndRecordAllInstances(
@NotNull KotlinType type,
@NotNull final Map<TypeConstructor, Set<KotlinType>> constructorToAllInstances,
@NotNull final Set<TypeConstructor> visited
) {
return DFS.dfs(
Collections.singletonList(type),
new DFS.Neighbors<KotlinType>() {
@NotNull
@Override
public Iterable<KotlinType> getNeighbors(KotlinType current) {
TypeSubstitutor substitutor = TypeSubstitutor.create(current);
Collection<KotlinType> supertypes = current.getConstructor().getSupertypes();
List<KotlinType> result = new ArrayList<KotlinType>(supertypes.size());
for (KotlinType supertype : supertypes) {
if (visited.contains(supertype.getConstructor())) {
continue;
}
result.add(substitutor.safeSubstitute(supertype, Variance.INVARIANT));
}
return result;
}
},
new DFS.Visited<KotlinType>() {
@Override
public boolean checkAndMarkVisited(KotlinType current) {
return visited.add(current.getConstructor());
}
},
new DFS.NodeHandlerWithListResult<KotlinType, TypeConstructor>() {
@Override
public boolean beforeChildren(KotlinType current) {
TypeConstructor constructor = current.getConstructor();
Set<KotlinType> instances = constructorToAllInstances.get(constructor);
if (instances == null) {
instances = new HashSet<KotlinType>();
constructorToAllInstances.put(constructor, instances);
}
instances.add(current);
return true;
}
@Override
public void afterChildren(KotlinType current) {
result.addFirst(current.getConstructor());
}
}
);
}
public static TypeSubstitutor makeConstantSubstitutor(Collection<TypeParameterDescriptor> typeParameterDescriptors, KotlinType type) {
final Set<TypeConstructor> constructors = org.jetbrains.kotlin.utils.CollectionsKt
.newHashSetWithExpectedSize(typeParameterDescriptors.size());
for (TypeParameterDescriptor typeParameterDescriptor : typeParameterDescriptors) {
constructors.add(typeParameterDescriptor.getTypeConstructor());
}
final TypeProjection projection = new TypeProjectionImpl(type);
return TypeSubstitutor.create(new TypeConstructorSubstitution() {
@Override
public TypeProjection get(@NotNull TypeConstructor key) {
if (constructors.contains(key)) {
return projection;
}
return null;
}
@Override
public boolean isEmpty() {
return false;
}
});
}
public static boolean isTypeParameter(@NotNull KotlinType type) {
return getTypeParameterDescriptorOrNull(type) != null;
}
@@ -48,9 +48,9 @@ import org.jetbrains.kotlin.psi.KtObjectDeclaration
import org.jetbrains.kotlin.psi.KtSecondaryConstructor
import org.jetbrains.kotlin.resolve.BindingContextUtils
import org.jetbrains.kotlin.resolve.DescriptorUtils.*
import org.jetbrains.kotlin.types.CommonSupertypes.topologicallySortSuperclassesAndRecordAllInstances
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeConstructor
import org.jetbrains.kotlin.types.TypeUtils.topologicallySortSuperclassesAndRecordAllInstances
import org.jetbrains.kotlin.utils.identity
import java.util.*