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());
}
}
);
}
}