Refactoring. Remove getSubstitution from KotlinType.

This commit is contained in:
Stanislav Erokhin
2016-04-25 18:35:05 +03:00
parent 5fe48313e9
commit 926da77abe
11 changed files with 19 additions and 63 deletions
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2015 JetBrains s.r.o. * Copyright 2010-2016 JetBrains s.r.o.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -23,10 +23,7 @@ import org.jetbrains.kotlin.diagnostics.DiagnosticSink
import org.jetbrains.kotlin.diagnostics.Errors import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.psi.KtClass import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.TypeConstructor
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.typeUtil.boundClosure import org.jetbrains.kotlin.types.typeUtil.boundClosure
import org.jetbrains.kotlin.types.typeUtil.constituentTypes import org.jetbrains.kotlin.types.typeUtil.constituentTypes
import org.jetbrains.kotlin.utils.DFS import org.jetbrains.kotlin.utils.DFS
@@ -118,7 +115,7 @@ object NonExpansiveInheritanceRestrictionChecker {
val originalTypeParameter = constituentTypeConstructor.parameters[i] val originalTypeParameter = constituentTypeConstructor.parameters[i]
val bounds = hashSetOf<KotlinType>() val bounds = hashSetOf<KotlinType>()
val substitutor = constituentType.substitution.buildSubstitutor() val substitutor = TypeConstructorSubstitution.create(constituentType).buildSubstitutor()
val adaptedUpperBounds = originalTypeParameter.upperBounds.mapNotNull { substitutor.substitute(it, Variance.INVARIANT) } val adaptedUpperBounds = originalTypeParameter.upperBounds.mapNotNull { substitutor.substitute(it, Variance.INVARIANT) }
bounds.addAll(adaptedUpperBounds) bounds.addAll(adaptedUpperBounds)
@@ -121,7 +121,6 @@ internal object RawSubstitution : TypeSubstitution() {
parameter -> parameter ->
computeProjection(parameter, attr) computeProjection(parameter, attr)
}, },
RawSubstitution,
declaration.getMemberScope(RawSubstitution), declaration.getMemberScope(RawSubstitution),
RawTypeCapabilities RawTypeCapabilities
) )
@@ -136,7 +136,6 @@ private fun KotlinType.enhanceInflexible(qualifiers: (Int) -> JavaTypeQualifiers
typeConstructor, typeConstructor,
enhancedNullability, enhancedNullability,
enhancedArguments, enhancedArguments,
newSubstitution,
if (enhancedClassifier is ClassDescriptor) if (enhancedClassifier is ClassDescriptor)
enhancedClassifier.getMemberScope(newSubstitution) enhancedClassifier.getMemberScope(newSubstitution)
else enhancedClassifier.getDefaultType().getMemberScope(), else enhancedClassifier.getDefaultType().getMemberScope(),
@@ -34,10 +34,6 @@ abstract class AbstractLazyType(storageManager: StorageManager) : AbstractKotlin
protected abstract fun computeArguments(): List<TypeProjection> protected abstract fun computeArguments(): List<TypeProjection>
override fun getSubstitution() = computeCustomSubstitution() ?: TypeConstructorSubstitution.create(constructor, getArguments())
protected open fun computeCustomSubstitution(): TypeSubstitution? = getCapability<RawTypeCapability>()?.substitution
private val memberScope = storageManager.createLazyValue { computeMemberScope() } private val memberScope = storageManager.createLazyValue { computeMemberScope() }
override fun getMemberScope() = memberScope() override fun getMemberScope() = memberScope()
@@ -45,7 +41,11 @@ abstract class AbstractLazyType(storageManager: StorageManager) : AbstractKotlin
val descriptor = constructor.declarationDescriptor val descriptor = constructor.declarationDescriptor
return when (descriptor) { return when (descriptor) {
is TypeParameterDescriptor -> descriptor.getDefaultType().memberScope is TypeParameterDescriptor -> descriptor.getDefaultType().memberScope
is ClassDescriptor -> descriptor.getMemberScope(substitution) is ClassDescriptor -> {
val substitution = getCapability<RawTypeCapability>()?.substitution
?: TypeConstructorSubstitution.create(constructor, getArguments())
descriptor.getMemberScope(substitution)
}
else -> throw IllegalStateException("Unsupported classifier: $descriptor") else -> throw IllegalStateException("Unsupported classifier: $descriptor")
} }
} }
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2015 JetBrains s.r.o. * Copyright 2010-2016 JetBrains s.r.o.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -39,12 +39,6 @@ public abstract class DelegatingType implements KotlinType {
return getDelegate().getArguments(); return getDelegate().getArguments();
} }
@NotNull
@Override
public TypeSubstitution getSubstitution() {
return getDelegate().getSubstitution();
}
@Override @Override
public boolean isMarkedNullable() { public boolean isMarkedNullable() {
return getDelegate().isMarkedNullable(); return getDelegate().isMarkedNullable();
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2015 JetBrains s.r.o. * Copyright 2010-2016 JetBrains s.r.o.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -515,12 +515,6 @@ public class ErrorUtils {
return arguments; return arguments;
} }
@NotNull
@Override
public TypeSubstitution getSubstitution() {
return TypeConstructorSubstitution.create(constructor, arguments);
}
@Override @Override
public boolean isMarkedNullable() { public boolean isMarkedNullable() {
return false; return false;
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2015 JetBrains s.r.o. * Copyright 2010-2016 JetBrains s.r.o.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -36,9 +36,6 @@ public interface KotlinType extends Annotated {
@ReadOnly @ReadOnly
List<TypeProjection> getArguments(); List<TypeProjection> getArguments();
@NotNull
TypeSubstitution getSubstitution();
boolean isMarkedNullable(); boolean isMarkedNullable();
@NotNull @NotNull
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2015 JetBrains s.r.o. * Copyright 2010-2016 JetBrains s.r.o.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@ private constructor(
private val constructor: TypeConstructor, private val constructor: TypeConstructor,
private val nullable: Boolean, private val nullable: Boolean,
private val arguments: List<TypeProjection>, private val arguments: List<TypeProjection>,
private val substitution: TypeSubstitution?,
private val memberScope: MemberScope private val memberScope: MemberScope
) : AbstractKotlinType() { ) : AbstractKotlinType() {
@@ -37,20 +36,19 @@ private constructor(
arguments: List<TypeProjection>, arguments: List<TypeProjection>,
memberScope: MemberScope): KotlinTypeImpl memberScope: MemberScope): KotlinTypeImpl
= KotlinTypeImpl(annotations, constructor, nullable, arguments, null, memberScope) = KotlinTypeImpl(annotations, constructor, nullable, arguments, memberScope)
@JvmStatic fun create(annotations: Annotations, @JvmStatic fun create(annotations: Annotations,
constructor: TypeConstructor, constructor: TypeConstructor,
nullable: Boolean, nullable: Boolean,
arguments: List<TypeProjection>, arguments: List<TypeProjection>,
substitution: TypeSubstitution,
memberScope: MemberScope, memberScope: MemberScope,
capabilities: TypeCapabilities capabilities: TypeCapabilities
): KotlinTypeImpl { ): KotlinTypeImpl {
if (capabilities !== TypeCapabilities.NONE) { if (capabilities !== TypeCapabilities.NONE) {
return WithCapabilities(annotations, constructor, nullable, arguments, substitution, memberScope, capabilities) return WithCapabilities(annotations, constructor, nullable, arguments, memberScope, capabilities)
} }
return KotlinTypeImpl(annotations, constructor, nullable, arguments, substitution, memberScope) return KotlinTypeImpl(annotations, constructor, nullable, arguments, memberScope)
} }
@JvmStatic fun create(annotations: Annotations, @JvmStatic fun create(annotations: Annotations,
@@ -59,7 +57,7 @@ private constructor(
arguments: List<TypeProjection>): KotlinTypeImpl arguments: List<TypeProjection>): KotlinTypeImpl
= KotlinTypeImpl( = KotlinTypeImpl(
annotations, descriptor.typeConstructor, nullable, arguments, null, descriptor.getMemberScope(arguments) annotations, descriptor.typeConstructor, nullable, arguments, descriptor.getMemberScope(arguments)
) )
} }
@@ -68,10 +66,9 @@ private constructor(
constructor: TypeConstructor, constructor: TypeConstructor,
nullable: Boolean, nullable: Boolean,
arguments: List<TypeProjection>, arguments: List<TypeProjection>,
substitution: TypeSubstitution?,
memberScope: MemberScope, memberScope: MemberScope,
private val typeCapabilities: TypeCapabilities private val typeCapabilities: TypeCapabilities
) : KotlinTypeImpl(annotations, constructor, nullable, arguments, substitution, memberScope) { ) : KotlinTypeImpl(annotations, constructor, nullable, arguments, memberScope) {
override fun getCapabilities(): TypeCapabilities = typeCapabilities override fun getCapabilities(): TypeCapabilities = typeCapabilities
} }
@@ -83,13 +80,6 @@ private constructor(
override fun getAnnotations() = annotations override fun getAnnotations() = annotations
override fun getSubstitution(): TypeSubstitution {
if (substitution == null) {
return TypeConstructorSubstitution.create(getConstructor(), getArguments())
}
return substitution
}
override fun getConstructor() = constructor override fun getConstructor() = constructor
override fun getArguments() = arguments override fun getArguments() = arguments
@@ -137,7 +137,6 @@ fun KotlinType.replace(
constructor, constructor,
isMarkedNullable, isMarkedNullable,
arguments, arguments,
substitution,
memberScope, memberScope,
newCapabilities newCapabilities
) )
@@ -156,7 +155,6 @@ fun KotlinType.replace(
constructor, constructor,
isMarkedNullable, isMarkedNullable,
newArguments, newArguments,
newSubstitution,
newScope, newScope,
newCapabilities newCapabilities
) )
@@ -54,12 +54,6 @@ public class TypeUtils {
throw new IllegalStateException(name); throw new IllegalStateException(name);
} }
@NotNull
@Override
public TypeSubstitution getSubstitution() {
throw new IllegalStateException(name);
}
@Override @Override
public boolean isMarkedNullable() { public boolean isMarkedNullable() {
throw new IllegalStateException(name); throw new IllegalStateException(name);
@@ -553,12 +547,6 @@ public class TypeUtils {
return delegate.getAnnotations(); return delegate.getAnnotations();
} }
@NotNull
@Override
public TypeSubstitution getSubstitution() {
return delegate.getSubstitution();
}
@Nullable @Nullable
@Override @Override
public <T extends TypeCapability> T getCapability(@NotNull Class<T> capabilityClass) { public <T extends TypeCapability> T getCapability(@NotNull Class<T> capabilityClass) {
@@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2015 JetBrains s.r.o. * Copyright 2010-2016 JetBrains s.r.o.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -89,7 +89,7 @@ private object ForceTypeCopySubstitution : TypeSubstitution() {
override fun get(key: KotlinType) = override fun get(key: KotlinType) =
with(key) { with(key) {
KotlinTypeImpl.create( KotlinTypeImpl.create(
annotations, constructor, isMarkedNullable, arguments, substitution, memberScope, capabilities).asTypeProjection() annotations, constructor, isMarkedNullable, arguments, memberScope, capabilities).asTypeProjection()
} }
override fun isEmpty() = false override fun isEmpty() = false