diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/CirType.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/CirType.kt index f11f8b6aaa0..edcc0de6823 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/CirType.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/CirType.kt @@ -133,6 +133,24 @@ abstract class CirTypeAliasType : CirClassOrTypeAliasType() { builder.append(" -> ") underlyingType.appendDescriptionTo(builder) } + + companion object { + fun createInterned( + typeAliasId: CirEntityId, + underlyingType: CirClassOrTypeAliasType, + arguments: List, + isMarkedNullable: Boolean + ): CirTypeAliasType = interner.intern( + CirTypeAliasTypeInternedImpl( + classifierId = typeAliasId, + underlyingType = underlyingType, + arguments = arguments, + isMarkedNullable = isMarkedNullable + ) + ) + + private val interner = Interner() + } } 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, + 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 + } +} diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeAliasExpander.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeAliasExpander.kt index 57b84ae3752..2d60c89086d 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeAliasExpander.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeAliasExpander.kt @@ -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, diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeFactory.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeFactory.kt index 44367ec37f1..5e46b5334d9 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeFactory.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeFactory.kt @@ -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() - 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, - isMarkedNullable: Boolean - ): CirTypeAliasType { - return typeAliasTypeInterner.intern( - CirTypeAliasTypeImpl( - classifierId = typeAliasId, - underlyingType = underlyingType, - arguments = arguments, - isMarkedNullable = isMarkedNullable - ) - ) - } - fun 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, diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/impl/CirTypeAliasTypeImpl.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/impl/CirTypeAliasTypeImpl.kt deleted file mode 100644 index 65726178a9a..00000000000 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/impl/CirTypeAliasTypeImpl.kt +++ /dev/null @@ -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, - 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 - } -} diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/TypeCommonizer.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/TypeCommonizer.kt index fc97f013c7b..49db914ff18 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/TypeCommonizer.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/TypeCommonizer.kt @@ -144,7 +144,7 @@ private class TypeAliasTypeCommonizer(private val classifiers: CirKnownClassifie fun forKnownUnderlyingType(underlyingType: CirClassOrTypeAliasType) = object : CommonizedTypeAliasTypeBuilder { override fun build(typeAliasId: CirEntityId, arguments: List, isMarkedNullable: Boolean): CirTypeAliasType { val underlyingTypeWithProperNullability = CirTypeFactory.makeNullableIfNecessary(underlyingType, isMarkedNullable) - return CirTypeFactory.createTypeAliasType( + return CirTypeAliasType.createInterned( typeAliasId = typeAliasId, underlyingType = underlyingTypeWithProperNullability, // TODO replace arguments??? arguments = arguments, diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/typeAliasUtils.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/typeAliasUtils.kt index 21b5a7d3bb3..2c0fdd118bb 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/typeAliasUtils.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/typeAliasUtils.kt @@ -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, diff --git a/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/utils/mocks.kt b/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/utils/mocks.kt index adff0044ab5..2bb4b35501d 100644 --- a/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/utils/mocks.kt +++ b/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/utils/mocks.kt @@ -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(),