use of org.jetbrains.kotlin.descriptors.TypeParameterDescriptor in project kotlin by JetBrains.
the class TypeUtils method substituteProjectionsForParameters.
@NotNull
public static KotlinType substituteProjectionsForParameters(@NotNull ClassDescriptor clazz, @NotNull List<TypeProjection> projections) {
List<TypeParameterDescriptor> clazzTypeParameters = clazz.getTypeConstructor().getParameters();
if (clazzTypeParameters.size() != projections.size()) {
throw new IllegalArgumentException("type parameter counts do not match: " + clazz + ", " + projections);
}
Map<TypeConstructor, TypeProjection> substitutions = org.jetbrains.kotlin.utils.CollectionsKt.newHashMapWithExpectedSize(clazzTypeParameters.size());
for (int i = 0; i < clazzTypeParameters.size(); ++i) {
TypeConstructor typeConstructor = clazzTypeParameters.get(i).getTypeConstructor();
substitutions.put(typeConstructor, projections.get(i));
}
return TypeSubstitutor.create(substitutions).substitute(clazz.getDefaultType(), Variance.INVARIANT);
}
use of org.jetbrains.kotlin.descriptors.TypeParameterDescriptor in project kotlin by JetBrains.
the class TypeSubstitutorTest method stringsToSubstitutionMap.
private Map<TypeConstructor, TypeProjection> stringsToSubstitutionMap(Pair<String, String>[] substitutionStrs) {
Map<TypeConstructor, TypeProjection> map = Maps.newHashMap();
for (Pair<String, String> pair : substitutionStrs) {
String typeParameterName = pair.first;
String replacementProjectionString = pair.second;
ClassifierDescriptor classifier = ScopeUtilsKt.findClassifier(scope, Name.identifier(typeParameterName), NoLookupLocation.FROM_TEST);
assertNotNull("No type parameter named " + typeParameterName, classifier);
assertTrue(typeParameterName + " is not a type parameter: " + classifier, classifier instanceof TypeParameterDescriptor);
String typeStr = "C<" + replacementProjectionString + ">";
KotlinType typeWithArgument = resolveType(typeStr);
assert !typeWithArgument.getArguments().isEmpty() : "No arguments: " + typeWithArgument + " from " + typeStr;
map.put(classifier.getTypeConstructor(), typeWithArgument.getArguments().get(0));
}
return map;
}
use of org.jetbrains.kotlin.descriptors.TypeParameterDescriptor in project kotlin by JetBrains.
the class TypeSubstitutorTest method getContextScope.
private LexicalScope getContextScope() throws IOException {
// todo comments
String text = FileUtil.loadFile(new File("compiler/testData/type-substitutor.kt"), true);
KtFile ktFile = KtPsiFactoryKt.KtPsiFactory(getProject()).createFile(text);
AnalysisResult analysisResult = JvmResolveUtil.analyze(ktFile, getEnvironment());
ModuleDescriptor module = analysisResult.getModuleDescriptor();
LexicalScope topLevelScope = analysisResult.getBindingContext().get(BindingContext.LEXICAL_SCOPE, ktFile);
final ClassifierDescriptor contextClass = ScopeUtilsKt.findClassifier(topLevelScope, Name.identifier("___Context"), NoLookupLocation.FROM_TEST);
assert contextClass instanceof ClassDescriptor;
LocalRedeclarationChecker redeclarationChecker = new ThrowingLocalRedeclarationChecker(new OverloadChecker(TypeSpecificityComparator.NONE.INSTANCE));
LexicalScope typeParameters = new LexicalScopeImpl(topLevelScope, module, false, null, LexicalScopeKind.SYNTHETIC, redeclarationChecker, new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {
@Override
public Unit invoke(LexicalScopeImpl.InitializeHandler handler) {
for (TypeParameterDescriptor parameterDescriptor : contextClass.getTypeConstructor().getParameters()) {
handler.addClassifierDescriptor(parameterDescriptor);
}
return Unit.INSTANCE;
}
});
return new LexicalChainedScope(typeParameters, module, false, null, LexicalScopeKind.SYNTHETIC, Arrays.asList(contextClass.getDefaultType().getMemberScope(), module.getBuiltIns().getBuiltInsPackageScope()));
}
use of org.jetbrains.kotlin.descriptors.TypeParameterDescriptor in project kotlin by JetBrains.
the class TypeCheckingProcedure method equalTypes.
public boolean equalTypes(@NotNull KotlinType type1, @NotNull KotlinType type2) {
if (type1 == type2)
return true;
if (FlexibleTypesKt.isFlexible(type1)) {
if (FlexibleTypesKt.isFlexible(type2)) {
return !type1.isError() && !type2.isError() && isSubtypeOf(type1, type2) && isSubtypeOf(type2, type1);
}
return heterogeneousEquivalence(type2, type1);
} else if (FlexibleTypesKt.isFlexible(type2)) {
return heterogeneousEquivalence(type1, type2);
}
if (type1.isMarkedNullable() != type2.isMarkedNullable()) {
return false;
}
if (type1.isMarkedNullable()) {
// Then type2 is nullable, too (see the previous condition
return constraints.assertEqualTypes(TypeUtils.makeNotNullable(type1), TypeUtils.makeNotNullable(type2), this);
}
TypeConstructor constructor1 = type1.getConstructor();
TypeConstructor constructor2 = type2.getConstructor();
if (!constraints.assertEqualTypeConstructors(constructor1, constructor2)) {
return false;
}
List<TypeProjection> type1Arguments = type1.getArguments();
List<TypeProjection> type2Arguments = type2.getArguments();
if (type1Arguments.size() != type2Arguments.size()) {
return false;
}
for (int i = 0; i < type1Arguments.size(); i++) {
TypeProjection typeProjection1 = type1Arguments.get(i);
TypeProjection typeProjection2 = type2Arguments.get(i);
if (typeProjection1.isStarProjection() && typeProjection2.isStarProjection()) {
continue;
}
TypeParameterDescriptor typeParameter1 = constructor1.getParameters().get(i);
TypeParameterDescriptor typeParameter2 = constructor2.getParameters().get(i);
if (capture(typeProjection1, typeProjection2, typeParameter1)) {
continue;
}
if (getEffectiveProjectionKind(typeParameter1, typeProjection1) != getEffectiveProjectionKind(typeParameter2, typeProjection2)) {
return false;
}
if (!constraints.assertEqualTypes(typeProjection1.getType(), typeProjection2.getType(), this)) {
return false;
}
}
return true;
}
use of org.jetbrains.kotlin.descriptors.TypeParameterDescriptor in project kotlin by JetBrains.
the class TypeCheckingProcedure method checkSubtypeForTheSameConstructor.
private boolean checkSubtypeForTheSameConstructor(@NotNull KotlinType subtype, @NotNull KotlinType supertype) {
TypeConstructor constructor = subtype.getConstructor();
// this assert was moved to checker/utils.kt
//assert constraints.assertEqualTypeConstructors(constructor, supertype.getConstructor()) : constructor + " is not " + supertype.getConstructor();
List<TypeProjection> subArguments = subtype.getArguments();
List<TypeProjection> superArguments = supertype.getArguments();
if (subArguments.size() != superArguments.size())
return false;
List<TypeParameterDescriptor> parameters = constructor.getParameters();
for (int i = 0; i < parameters.size(); i++) {
TypeParameterDescriptor parameter = parameters.get(i);
TypeProjection superArgument = superArguments.get(i);
TypeProjection subArgument = subArguments.get(i);
if (superArgument.isStarProjection())
continue;
if (capture(subArgument, superArgument, parameter))
continue;
boolean argumentIsErrorType = subArgument.getType().isError() || superArgument.getType().isError();
if (!argumentIsErrorType && parameter.getVariance() == INVARIANT && subArgument.getProjectionKind() == INVARIANT && superArgument.getProjectionKind() == INVARIANT) {
if (!constraints.assertEqualTypes(subArgument.getType(), superArgument.getType(), this))
return false;
continue;
}
KotlinType superOut = getOutType(parameter, superArgument);
KotlinType subOut = getOutType(parameter, subArgument);
if (!constraints.assertSubtype(subOut, superOut, this))
return false;
KotlinType superIn = getInType(parameter, superArgument);
KotlinType subIn = getInType(parameter, subArgument);
if (superArgument.getProjectionKind() != Variance.OUT_VARIANCE) {
if (!constraints.assertSubtype(superIn, subIn, this))
return false;
} else {
assert KotlinBuiltIns.isNothing(superIn) : "In component must be Nothing for out-projection";
}
}
return true;
}
Aggregations