org.jetbrains.kotlin.types.BoundsSubstitutor Maven / Gradle / Ivy
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.types;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.CallableDescriptor;
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor;
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
import org.jetbrains.kotlin.utils.DFS;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class BoundsSubstitutor {
private static final Function PROJECTIONS_TO_TYPES = new Function() {
@Override
public KotlinType apply(TypeProjection projection) {
return projection.getType();
}
};
private BoundsSubstitutor() {
}
@NotNull
public static D substituteBounds(@NotNull D functionDescriptor) {
List typeParameters = functionDescriptor.getTypeParameters();
if (typeParameters.isEmpty()) return functionDescriptor;
// TODO: this does not handle any recursion in the bounds
@SuppressWarnings("unchecked")
D substitutedFunction = (D) functionDescriptor.substitute(createUpperBoundsSubstitutor(typeParameters));
assert substitutedFunction != null : "Substituting upper bounds should always be legal";
return substitutedFunction;
}
@NotNull
public static TypeSubstitutor createUpperBoundsSubstitutor(@NotNull D callableDescriptor) {
return createUpperBoundsSubstitutor(callableDescriptor.getTypeParameters());
}
@NotNull
private static TypeSubstitutor createUpperBoundsSubstitutor(@NotNull List typeParameters) {
Map mutableSubstitution = new HashMap();
TypeSubstitutor substitutor = TypeSubstitutor.create(mutableSubstitution);
// todo assert: no loops
for (TypeParameterDescriptor descriptor : topologicallySortTypeParameters(typeParameters)) {
KotlinType upperBoundsAsType = TypeIntersector.getUpperBoundsAsType(descriptor);
KotlinType substitutedUpperBoundsAsType = substitutor.substitute(upperBoundsAsType, Variance.INVARIANT);
mutableSubstitution.put(descriptor.getTypeConstructor(), new TypeProjectionImpl(substitutedUpperBoundsAsType));
}
return substitutor;
}
@NotNull
private static List topologicallySortTypeParameters(@NotNull final List typeParameters) {
// In the end, we want every parameter to have no references to those after it in the list
// This gives us the reversed order: the one that refers to everybody else comes first
List topOrder = DFS.topologicalOrder(
typeParameters,
new DFS.Neighbors() {
@NotNull
@Override
public Iterable getNeighbors(TypeParameterDescriptor current) {
return getTypeParametersFromUpperBounds(current, typeParameters);
}
});
assert topOrder.size() == typeParameters.size() : "All type parameters must be visited, but only " + topOrder + " were";
// Now, the one that refers to everybody else stands in the last position
Collections.reverse(topOrder);
return topOrder;
}
@NotNull
private static List getTypeParametersFromUpperBounds(
@NotNull TypeParameterDescriptor current,
@NotNull final List typeParameters
) {
return DFS.dfs(
current.getUpperBounds(),
new DFS.Neighbors() {
@NotNull
@Override
public Iterable getNeighbors(KotlinType current) {
return Collections2.transform(current.getArguments(), PROJECTIONS_TO_TYPES);
}
},
new DFS.NodeHandlerWithListResult() {
@Override
public boolean beforeChildren(KotlinType current) {
ClassifierDescriptor declarationDescriptor = current.getConstructor().getDeclarationDescriptor();
// typeParameters in a list, but it contains very few elements, so it's fine to call contains() on it
//noinspection SuspiciousMethodCalls
if (typeParameters.contains(declarationDescriptor)) {
result.add((TypeParameterDescriptor) declarationDescriptor);
}
return true;
}
}
);
}
}