kotlin.reflect.jvm.internal.impl.resolve.calls.inference.CapturedTypeConstructor.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-reflect Show documentation
Show all versions of kotlin-reflect Show documentation
Kotlin Full Reflection Library
/*
* Copyright 2010-2016 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 kotlin.reflect.jvm.internal.impl.resolve.calls.inference
import kotlin.reflect.jvm.internal.impl.builtins.KotlinBuiltIns
import kotlin.reflect.jvm.internal.impl.descriptors.TypeParameterDescriptor
import kotlin.reflect.jvm.internal.impl.descriptors.annotations.Annotations
import kotlin.reflect.jvm.internal.impl.resolve.scopes.MemberScope
import kotlin.reflect.jvm.internal.impl.storage.LockBasedStorageManager
import kotlin.reflect.jvm.internal.impl.types.*
import kotlin.reflect.jvm.internal.impl.types.Variance.IN_VARIANCE
import kotlin.reflect.jvm.internal.impl.types.Variance.OUT_VARIANCE
import kotlin.reflect.jvm.internal.impl.types.checker.KotlinTypeRefiner
import kotlin.reflect.jvm.internal.impl.types.checker.NewCapturedTypeConstructor
import kotlin.reflect.jvm.internal.impl.types.model.CapturedTypeConstructorMarker
import kotlin.reflect.jvm.internal.impl.types.model.CapturedTypeMarker
import kotlin.reflect.jvm.internal.impl.types.refinement.TypeRefinement
import kotlin.reflect.jvm.internal.impl.types.typeUtil.builtIns
interface CapturedTypeConstructor : CapturedTypeConstructorMarker, TypeConstructor {
val projection: TypeProjection
}
class CapturedTypeConstructorImpl(
override val projection: TypeProjection
) : CapturedTypeConstructor {
var newTypeConstructor: NewCapturedTypeConstructor? = null
init {
assert(projection.projectionKind != Variance.INVARIANT) {
"Only nontrivial projections can be captured, not: $projection"
}
}
override fun getParameters(): List = listOf()
override fun getSupertypes(): Collection {
val superType = if (projection.projectionKind == Variance.OUT_VARIANCE)
projection.type
else
builtIns.nullableAnyType
return listOf(superType)
}
override fun isFinal() = true
override fun isDenotable() = false
override fun getDeclarationDescriptor() = null
override fun toString() = "CapturedTypeConstructor($projection)"
override fun getBuiltIns(): KotlinBuiltIns = projection.type.constructor.builtIns
@TypeRefinement
override fun refine(kotlinTypeRefiner: KotlinTypeRefiner) =
CapturedTypeConstructorImpl(projection.refine(kotlinTypeRefiner))
}
class CapturedType(
val typeProjection: TypeProjection,
override val constructor: CapturedTypeConstructor = CapturedTypeConstructorImpl(typeProjection),
override val isMarkedNullable: Boolean = false,
override val annotations: Annotations = Annotations.EMPTY
) : SimpleType(), SubtypingRepresentatives, CapturedTypeMarker {
override val arguments: List
get() = listOf()
override val memberScope: MemberScope
get() = ErrorUtils.createErrorScope(
"No member resolution should be done on captured type, it used only during constraint system resolution", true
)
override val subTypeRepresentative: KotlinType
get() = representative(OUT_VARIANCE, builtIns.nullableAnyType)
override val superTypeRepresentative: KotlinType
get() = representative(IN_VARIANCE, builtIns.nothingType)
private fun representative(variance: Variance, default: KotlinType) =
if (typeProjection.projectionKind == variance) typeProjection.type else default
override fun sameTypeConstructor(type: KotlinType) = constructor === type.constructor
override fun toString() = "Captured($typeProjection)" + if (isMarkedNullable) "?" else ""
override fun makeNullableAsSpecified(newNullability: Boolean): CapturedType {
if (newNullability == isMarkedNullable) return this
return CapturedType(typeProjection, constructor, newNullability, annotations)
}
override fun replaceAnnotations(newAnnotations: Annotations): CapturedType =
CapturedType(typeProjection, constructor, isMarkedNullable, newAnnotations)
@TypeRefinement
override fun refine(kotlinTypeRefiner: KotlinTypeRefiner) =
CapturedType(typeProjection.refine(kotlinTypeRefiner), constructor, isMarkedNullable, annotations)
}
fun createCapturedType(typeProjection: TypeProjection): KotlinType = CapturedType(typeProjection)
fun KotlinType.isCaptured(): Boolean = constructor is CapturedTypeConstructor
fun TypeSubstitution.wrapWithCapturingSubstitution(needApproximation: Boolean = true): TypeSubstitution =
if (this is IndexedParametersSubstitution)
IndexedParametersSubstitution(
this.parameters,
this.arguments.zip(this.parameters).map {
it.first.createCapturedIfNeeded(it.second)
}.toTypedArray(),
approximateCapturedTypes = needApproximation
)
else
object : DelegatedTypeSubstitution(this@wrapWithCapturingSubstitution) {
override fun approximateContravariantCapturedTypes() = needApproximation
override fun get(key: KotlinType) =
super.get(key)?.createCapturedIfNeeded(key.constructor.declarationDescriptor as? TypeParameterDescriptor)
}
private fun TypeProjection.createCapturedIfNeeded(typeParameterDescriptor: TypeParameterDescriptor?): TypeProjection {
if (typeParameterDescriptor == null || projectionKind == Variance.INVARIANT) return this
// Treat consistent projections as invariant
if (typeParameterDescriptor.variance == projectionKind) {
// TODO: Make star projection type lazy
return if (isStarProjection)
TypeProjectionImpl(LazyWrappedType(LockBasedStorageManager.NO_LOCKS) {
[email protected]
})
else
TypeProjectionImpl([email protected])
}
return TypeProjectionImpl(createCapturedType(this))
}