[Commonizer] Encapsulate interning inside of CIR entities: CirTypeParameterType

This commit is contained in:
Dmitriy Dolovov
2021-03-05 00:00:22 +03:00
parent 3012839f49
commit c6d8cddcd9
3 changed files with 26 additions and 21 deletions
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir package org.jetbrains.kotlin.descriptors.commonizer.cir
import kotlinx.metadata.KmType import kotlinx.metadata.KmType
import org.jetbrains.kotlin.descriptors.commonizer.utils.Interner
import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.Variance
typealias CirTypeSignature = String typealias CirTypeSignature = String
@@ -46,14 +47,27 @@ sealed class CirSimpleType : CirType() {
} }
} }
data class CirTypeParameterType( abstract class CirTypeParameterType : CirSimpleType() {
val index: Int, abstract val index: Int
override val isMarkedNullable: Boolean
) : CirSimpleType() {
override fun appendDescriptionTo(builder: StringBuilder) { override fun appendDescriptionTo(builder: StringBuilder) {
builder.append('#').append(index) builder.append('#').append(index)
super.appendDescriptionTo(builder) super.appendDescriptionTo(builder)
} }
companion object {
fun createInterned(
index: Int,
isMarkedNullable: Boolean
): CirTypeParameterType = interner.intern(
CirTypeParameterTypeInternedImpl(
index = index,
isMarkedNullable = isMarkedNullable
)
)
private val interner = Interner<CirTypeParameterTypeInternedImpl>()
}
} }
sealed class CirClassOrTypeAliasType : CirSimpleType() { sealed class CirClassOrTypeAliasType : CirSimpleType() {
@@ -111,3 +125,8 @@ data class CirTypeProjectionImpl(val projectionKind: Variance, val type: CirType
type.appendDescriptionTo(this) type.appendDescriptionTo(this)
} }
} }
private data class CirTypeParameterTypeInternedImpl(
override val index: Int,
override val isMarkedNullable: Boolean
) : CirTypeParameterType()
@@ -34,7 +34,7 @@ object CirAnnotationFactory {
arguments = clazz.typeParameters.compactMap { typeParameter -> arguments = clazz.typeParameters.compactMap { typeParameter ->
CirTypeProjectionImpl( CirTypeProjectionImpl(
projectionKind = typeParameter.variance, projectionKind = typeParameter.variance,
type = CirTypeFactory.createTypeParameterType( type = CirTypeParameterType.createInterned(
index = typeParameter.index, index = typeParameter.index,
isMarkedNullable = false isMarkedNullable = false
) )
@@ -32,7 +32,6 @@ object CirTypeFactory {
private val classTypeInterner = Interner<CirClassType>() private val classTypeInterner = Interner<CirClassType>()
private val typeAliasTypeInterner = Interner<CirTypeAliasType>() private val typeAliasTypeInterner = Interner<CirTypeAliasType>()
private val typeParameterTypeInterner = Interner<CirTypeParameterType>()
fun create(source: KmType, typeResolver: CirTypeResolver): CirType { fun create(source: KmType, typeResolver: CirTypeResolver): CirType {
@Suppress("NAME_SHADOWING") @Suppress("NAME_SHADOWING")
@@ -76,7 +75,7 @@ object CirTypeFactory {
) )
} }
is KmClassifier.TypeParameter -> { is KmClassifier.TypeParameter -> {
createTypeParameterType( CirTypeParameterType.createInterned(
index = typeResolver.resolveTypeParameterIndex(classifier.id), index = typeResolver.resolveTypeParameterIndex(classifier.id),
isMarkedNullable = isMarkedNullable isMarkedNullable = isMarkedNullable
) )
@@ -118,19 +117,6 @@ object CirTypeFactory {
) )
} }
@Suppress("MemberVisibilityCanBePrivate")
fun createTypeParameterType(
index: Int,
isMarkedNullable: Boolean
): CirTypeParameterType {
return typeParameterTypeInterner.intern(
CirTypeParameterType(
index = index,
isMarkedNullable = isMarkedNullable
)
)
}
fun <T : CirSimpleType> makeNullable(type: T): T { fun <T : CirSimpleType> makeNullable(type: T): T {
if (type.isMarkedNullable) if (type.isMarkedNullable)
return type return type
@@ -149,7 +135,7 @@ object CirTypeFactory {
arguments = type.arguments, arguments = type.arguments,
isMarkedNullable = true isMarkedNullable = true
) )
is CirTypeParameterType -> createTypeParameterType( is CirTypeParameterType -> CirTypeParameterType.createInterned(
index = type.index, index = type.index,
isMarkedNullable = true isMarkedNullable = true
) )