[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
import kotlinx.metadata.KmType
import org.jetbrains.kotlin.descriptors.commonizer.utils.Interner
import org.jetbrains.kotlin.types.Variance
typealias CirTypeSignature = String
@@ -46,14 +47,27 @@ sealed class CirSimpleType : CirType() {
}
}
data class CirTypeParameterType(
val index: Int,
override val isMarkedNullable: Boolean
) : CirSimpleType() {
abstract class CirTypeParameterType : CirSimpleType() {
abstract val index: Int
override fun appendDescriptionTo(builder: StringBuilder) {
builder.append('#').append(index)
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() {
@@ -111,3 +125,8 @@ data class CirTypeProjectionImpl(val projectionKind: Variance, val type: CirType
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 ->
CirTypeProjectionImpl(
projectionKind = typeParameter.variance,
type = CirTypeFactory.createTypeParameterType(
type = CirTypeParameterType.createInterned(
index = typeParameter.index,
isMarkedNullable = false
)
@@ -32,7 +32,6 @@ object CirTypeFactory {
private val classTypeInterner = Interner<CirClassType>()
private val typeAliasTypeInterner = Interner<CirTypeAliasType>()
private val typeParameterTypeInterner = Interner<CirTypeParameterType>()
fun create(source: KmType, typeResolver: CirTypeResolver): CirType {
@Suppress("NAME_SHADOWING")
@@ -76,7 +75,7 @@ object CirTypeFactory {
)
}
is KmClassifier.TypeParameter -> {
createTypeParameterType(
CirTypeParameterType.createInterned(
index = typeResolver.resolveTypeParameterIndex(classifier.id),
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 {
if (type.isMarkedNullable)
return type
@@ -149,7 +135,7 @@ object CirTypeFactory {
arguments = type.arguments,
isMarkedNullable = true
)
is CirTypeParameterType -> createTypeParameterType(
is CirTypeParameterType -> CirTypeParameterType.createInterned(
index = type.index,
isMarkedNullable = true
)