[Commonizer] Encapsulate interning inside of CIR entities: CirTypeAliasType

This commit is contained in:
Dmitriy Dolovov
2021-03-05 00:16:25 +03:00
parent 36294352ef
commit d7ed8ff804
7 changed files with 57 additions and 74 deletions
@@ -133,6 +133,24 @@ abstract class CirTypeAliasType : CirClassOrTypeAliasType() {
builder.append(" -> ")
underlyingType.appendDescriptionTo(builder)
}
companion object {
fun createInterned(
typeAliasId: CirEntityId,
underlyingType: CirClassOrTypeAliasType,
arguments: List<CirTypeProjection>,
isMarkedNullable: Boolean
): CirTypeAliasType = interner.intern(
CirTypeAliasTypeInternedImpl(
classifierId = typeAliasId,
underlyingType = underlyingType,
arguments = arguments,
isMarkedNullable = isMarkedNullable
)
)
private val interner = Interner<CirTypeAliasTypeInternedImpl>()
}
}
sealed class CirTypeProjection
@@ -189,3 +207,36 @@ private class CirClassTypeInternedImpl(
else -> false
}
}
private class CirTypeAliasTypeInternedImpl(
override val classifierId: CirEntityId,
override val underlyingType: CirClassOrTypeAliasType,
override val arguments: List<CirTypeProjection>,
override val isMarkedNullable: Boolean
) : CirTypeAliasType() {
// See also org.jetbrains.kotlin.types.KotlinType.cachedHashCode
private var cachedHashCode = 0
private fun computeHashCode() = hashCode(classifierId)
.appendHashCode(underlyingType)
.appendHashCode(arguments)
.appendHashCode(isMarkedNullable)
override fun hashCode(): Int {
var currentHashCode = cachedHashCode
if (currentHashCode != 0) return currentHashCode
currentHashCode = computeHashCode()
cachedHashCode = currentHashCode
return currentHashCode
}
override fun equals(other: Any?): Boolean = when {
other === this -> true
other is CirTypeAliasType -> classifierId == other.classifierId
&& underlyingType == other.underlyingType
&& isMarkedNullable == other.isMarkedNullable
&& arguments == other.arguments
else -> false
}
}
@@ -121,7 +121,7 @@ object CirTypeAliasExpander {
val nestedExpansion = CirTypeAliasExpansion(typeAlias, expandedArguments, type.isMarkedNullable, expansion.typeResolver)
val nestedExpandedType = expand(nestedExpansion)
return CirTypeFactory.createTypeAliasType(
return CirTypeAliasType.createInterned(
typeAliasId = type.typeAliasId,
underlyingType = nestedExpandedType,
arguments = expandedArguments,
@@ -9,7 +9,6 @@ import gnu.trove.TIntObjectHashMap
import kotlinx.metadata.*
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirTypeAliasTypeImpl
import org.jetbrains.kotlin.descriptors.commonizer.core.computeExpandedType
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirProvided
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirProvidedClassifiers
@@ -28,8 +27,6 @@ object CirTypeFactory {
)
}
private val typeAliasTypeInterner = Interner<CirTypeAliasType>()
fun create(source: KmType, typeResolver: CirTypeResolver): CirType {
@Suppress("NAME_SHADOWING")
val source = source.abbreviatedType ?: source
@@ -64,7 +61,7 @@ object CirTypeFactory {
CirTypeAliasExpansion.create(typeAliasId, arguments, isMarkedNullable, typeResolver)
)
createTypeAliasType(
CirTypeAliasType.createInterned(
typeAliasId = typeAliasId,
underlyingType = underlyingType,
arguments = arguments,
@@ -80,22 +77,6 @@ object CirTypeFactory {
}
}
fun createTypeAliasType(
typeAliasId: CirEntityId,
underlyingType: CirClassOrTypeAliasType,
arguments: List<CirTypeProjection>,
isMarkedNullable: Boolean
): CirTypeAliasType {
return typeAliasTypeInterner.intern(
CirTypeAliasTypeImpl(
classifierId = typeAliasId,
underlyingType = underlyingType,
arguments = arguments,
isMarkedNullable = isMarkedNullable
)
)
}
fun <T : CirSimpleType> makeNullable(type: T): T {
if (type.isMarkedNullable)
return type
@@ -108,7 +89,7 @@ object CirTypeFactory {
arguments = type.arguments,
isMarkedNullable = true
)
is CirTypeAliasType -> createTypeAliasType(
is CirTypeAliasType -> CirTypeAliasType.createInterned(
typeAliasId = type.classifierId,
underlyingType = makeNullable(type.underlyingType),
arguments = type.arguments,
@@ -1,48 +0,0 @@
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.descriptors.commonizer.cir.impl
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClassOrTypeAliasType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirEntityId
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeAliasType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeProjection
import org.jetbrains.kotlin.descriptors.commonizer.utils.appendHashCode
import org.jetbrains.kotlin.descriptors.commonizer.utils.hashCode
data class CirTypeAliasTypeImpl(
override val classifierId: CirEntityId,
override val underlyingType: CirClassOrTypeAliasType,
override val arguments: List<CirTypeProjection>,
override val isMarkedNullable: Boolean
) : CirTypeAliasType() {
// See also org.jetbrains.kotlin.types.KotlinType.cachedHashCode
private var cachedHashCode = 0
private fun computeHashCode() = hashCode(classifierId)
.appendHashCode(underlyingType)
.appendHashCode(arguments)
.appendHashCode(isMarkedNullable)
override fun hashCode(): Int {
var currentHashCode = cachedHashCode
if (currentHashCode != 0) return currentHashCode
currentHashCode = computeHashCode()
cachedHashCode = currentHashCode
return currentHashCode
}
override fun equals(other: Any?): Boolean = when {
other === this -> true
other is CirTypeAliasType -> {
classifierId == other.classifierId
&& underlyingType == other.underlyingType
&& isMarkedNullable == other.isMarkedNullable
&& arguments == other.arguments
}
else -> false
}
}
@@ -144,7 +144,7 @@ private class TypeAliasTypeCommonizer(private val classifiers: CirKnownClassifie
fun forKnownUnderlyingType(underlyingType: CirClassOrTypeAliasType) = object : CommonizedTypeAliasTypeBuilder {
override fun build(typeAliasId: CirEntityId, arguments: List<CirTypeProjection>, isMarkedNullable: Boolean): CirTypeAliasType {
val underlyingTypeWithProperNullability = CirTypeFactory.makeNullableIfNecessary(underlyingType, isMarkedNullable)
return CirTypeFactory.createTypeAliasType(
return CirTypeAliasType.createInterned(
typeAliasId = typeAliasId,
underlyingType = underlyingTypeWithProperNullability, // TODO replace arguments???
arguments = arguments,
@@ -9,7 +9,6 @@ import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClassOrTypeAliasType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClassType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeAliasType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeProjection
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeFactory
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers
tailrec fun computeExpandedType(underlyingType: CirClassOrTypeAliasType): CirClassType {
@@ -62,7 +61,7 @@ private fun CirTypeAliasType.withCommonizedArguments(classifiers: CirKnownClassi
} ?: return null
return if (newArguments !== existingArguments || newUnderlyingType !== existingUnderlyingType)
CirTypeFactory.createTypeAliasType(
CirTypeAliasType.createInterned(
typeAliasId = classifierId,
underlyingType = newUnderlyingType,
arguments = newArguments,
@@ -28,7 +28,7 @@ internal fun mockTAType(
typeAliasId: String,
nullable: Boolean = false,
underlyingType: () -> CirClassOrTypeAliasType
): CirTypeAliasType = CirTypeFactory.createTypeAliasType(
): CirTypeAliasType = CirTypeAliasType.createInterned(
typeAliasId = createValidClassifierId(typeAliasId),
underlyingType = CirTypeFactory.makeNullableIfNecessary(underlyingType(), nullable),
arguments = emptyList(),