[Commonizer] Encapsulate interning inside of CIR entities: CirTypeAliasType
This commit is contained in:
@@ -133,6 +133,24 @@ abstract class CirTypeAliasType : CirClassOrTypeAliasType() {
|
|||||||
builder.append(" -> ")
|
builder.append(" -> ")
|
||||||
underlyingType.appendDescriptionTo(builder)
|
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
|
sealed class CirTypeProjection
|
||||||
@@ -189,3 +207,36 @@ private class CirClassTypeInternedImpl(
|
|||||||
else -> false
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
+1
-1
@@ -121,7 +121,7 @@ object CirTypeAliasExpander {
|
|||||||
val nestedExpansion = CirTypeAliasExpansion(typeAlias, expandedArguments, type.isMarkedNullable, expansion.typeResolver)
|
val nestedExpansion = CirTypeAliasExpansion(typeAlias, expandedArguments, type.isMarkedNullable, expansion.typeResolver)
|
||||||
val nestedExpandedType = expand(nestedExpansion)
|
val nestedExpandedType = expand(nestedExpansion)
|
||||||
|
|
||||||
return CirTypeFactory.createTypeAliasType(
|
return CirTypeAliasType.createInterned(
|
||||||
typeAliasId = type.typeAliasId,
|
typeAliasId = type.typeAliasId,
|
||||||
underlyingType = nestedExpandedType,
|
underlyingType = nestedExpandedType,
|
||||||
arguments = expandedArguments,
|
arguments = expandedArguments,
|
||||||
|
|||||||
+2
-21
@@ -9,7 +9,6 @@ import gnu.trove.TIntObjectHashMap
|
|||||||
import kotlinx.metadata.*
|
import kotlinx.metadata.*
|
||||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||||
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
|
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.core.computeExpandedType
|
||||||
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirProvided
|
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirProvided
|
||||||
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirProvidedClassifiers
|
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 {
|
fun create(source: KmType, typeResolver: CirTypeResolver): CirType {
|
||||||
@Suppress("NAME_SHADOWING")
|
@Suppress("NAME_SHADOWING")
|
||||||
val source = source.abbreviatedType ?: source
|
val source = source.abbreviatedType ?: source
|
||||||
@@ -64,7 +61,7 @@ object CirTypeFactory {
|
|||||||
CirTypeAliasExpansion.create(typeAliasId, arguments, isMarkedNullable, typeResolver)
|
CirTypeAliasExpansion.create(typeAliasId, arguments, isMarkedNullable, typeResolver)
|
||||||
)
|
)
|
||||||
|
|
||||||
createTypeAliasType(
|
CirTypeAliasType.createInterned(
|
||||||
typeAliasId = typeAliasId,
|
typeAliasId = typeAliasId,
|
||||||
underlyingType = underlyingType,
|
underlyingType = underlyingType,
|
||||||
arguments = arguments,
|
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 {
|
fun <T : CirSimpleType> makeNullable(type: T): T {
|
||||||
if (type.isMarkedNullable)
|
if (type.isMarkedNullable)
|
||||||
return type
|
return type
|
||||||
@@ -108,7 +89,7 @@ object CirTypeFactory {
|
|||||||
arguments = type.arguments,
|
arguments = type.arguments,
|
||||||
isMarkedNullable = true
|
isMarkedNullable = true
|
||||||
)
|
)
|
||||||
is CirTypeAliasType -> createTypeAliasType(
|
is CirTypeAliasType -> CirTypeAliasType.createInterned(
|
||||||
typeAliasId = type.classifierId,
|
typeAliasId = type.classifierId,
|
||||||
underlyingType = makeNullable(type.underlyingType),
|
underlyingType = makeNullable(type.underlyingType),
|
||||||
arguments = type.arguments,
|
arguments = type.arguments,
|
||||||
|
|||||||
-48
@@ -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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
-1
@@ -144,7 +144,7 @@ private class TypeAliasTypeCommonizer(private val classifiers: CirKnownClassifie
|
|||||||
fun forKnownUnderlyingType(underlyingType: CirClassOrTypeAliasType) = object : CommonizedTypeAliasTypeBuilder {
|
fun forKnownUnderlyingType(underlyingType: CirClassOrTypeAliasType) = object : CommonizedTypeAliasTypeBuilder {
|
||||||
override fun build(typeAliasId: CirEntityId, arguments: List<CirTypeProjection>, isMarkedNullable: Boolean): CirTypeAliasType {
|
override fun build(typeAliasId: CirEntityId, arguments: List<CirTypeProjection>, isMarkedNullable: Boolean): CirTypeAliasType {
|
||||||
val underlyingTypeWithProperNullability = CirTypeFactory.makeNullableIfNecessary(underlyingType, isMarkedNullable)
|
val underlyingTypeWithProperNullability = CirTypeFactory.makeNullableIfNecessary(underlyingType, isMarkedNullable)
|
||||||
return CirTypeFactory.createTypeAliasType(
|
return CirTypeAliasType.createInterned(
|
||||||
typeAliasId = typeAliasId,
|
typeAliasId = typeAliasId,
|
||||||
underlyingType = underlyingTypeWithProperNullability, // TODO replace arguments???
|
underlyingType = underlyingTypeWithProperNullability, // TODO replace arguments???
|
||||||
arguments = arguments,
|
arguments = arguments,
|
||||||
|
|||||||
+1
-2
@@ -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.CirClassType
|
||||||
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeAliasType
|
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeAliasType
|
||||||
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeProjection
|
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
|
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers
|
||||||
|
|
||||||
tailrec fun computeExpandedType(underlyingType: CirClassOrTypeAliasType): CirClassType {
|
tailrec fun computeExpandedType(underlyingType: CirClassOrTypeAliasType): CirClassType {
|
||||||
@@ -62,7 +61,7 @@ private fun CirTypeAliasType.withCommonizedArguments(classifiers: CirKnownClassi
|
|||||||
} ?: return null
|
} ?: return null
|
||||||
|
|
||||||
return if (newArguments !== existingArguments || newUnderlyingType !== existingUnderlyingType)
|
return if (newArguments !== existingArguments || newUnderlyingType !== existingUnderlyingType)
|
||||||
CirTypeFactory.createTypeAliasType(
|
CirTypeAliasType.createInterned(
|
||||||
typeAliasId = classifierId,
|
typeAliasId = classifierId,
|
||||||
underlyingType = newUnderlyingType,
|
underlyingType = newUnderlyingType,
|
||||||
arguments = newArguments,
|
arguments = newArguments,
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ internal fun mockTAType(
|
|||||||
typeAliasId: String,
|
typeAliasId: String,
|
||||||
nullable: Boolean = false,
|
nullable: Boolean = false,
|
||||||
underlyingType: () -> CirClassOrTypeAliasType
|
underlyingType: () -> CirClassOrTypeAliasType
|
||||||
): CirTypeAliasType = CirTypeFactory.createTypeAliasType(
|
): CirTypeAliasType = CirTypeAliasType.createInterned(
|
||||||
typeAliasId = createValidClassifierId(typeAliasId),
|
typeAliasId = createValidClassifierId(typeAliasId),
|
||||||
underlyingType = CirTypeFactory.makeNullableIfNecessary(underlyingType(), nullable),
|
underlyingType = CirTypeFactory.makeNullableIfNecessary(underlyingType(), nullable),
|
||||||
arguments = emptyList(),
|
arguments = emptyList(),
|
||||||
|
|||||||
Reference in New Issue
Block a user