diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/CirValueParameter.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/CirValueParameter.kt index 7c410874d65..8117ed67dcd 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/CirValueParameter.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/CirValueParameter.kt @@ -5,10 +5,89 @@ package org.jetbrains.kotlin.descriptors.commonizer.cir +import org.jetbrains.kotlin.descriptors.commonizer.utils.Interner +import org.jetbrains.kotlin.descriptors.commonizer.utils.appendHashCode +import org.jetbrains.kotlin.descriptors.commonizer.utils.hashCode + interface CirValueParameter : CirHasAnnotations, CirHasName { val returnType: CirType val varargElementType: CirType? val declaresDefaultValue: Boolean val isCrossinline: Boolean val isNoinline: Boolean + + companion object { + fun createInterned( + annotations: List, + name: CirName, + returnType: CirType, + varargElementType: CirType?, + declaresDefaultValue: Boolean, + isCrossinline: Boolean, + isNoinline: Boolean + ): CirValueParameter = interner.intern( + CirValueParameterInternedImpl( + annotations = annotations, + name = name, + returnType = returnType, + varargElementType = varargElementType, + declaresDefaultValue = declaresDefaultValue, + isCrossinline = isCrossinline, + isNoinline = isNoinline + ) + ) + + private val interner = Interner() + } +} + +private class CirValueParameterInternedImpl( + override val annotations: List, + override val name: CirName, + override val returnType: CirType, + override val varargElementType: CirType?, + override val declaresDefaultValue: Boolean, + override val isCrossinline: Boolean, + override val isNoinline: Boolean +) : CirValueParameter { + // See also org.jetbrains.kotlin.types.KotlinType.cachedHashCode + private var cachedHashCode = 0 + + private fun computeHashCode() = hashCode(name) + .appendHashCode(annotations) + .appendHashCode(returnType) + .appendHashCode(varargElementType) + .appendHashCode(declaresDefaultValue) + .appendHashCode(isCrossinline) + .appendHashCode(isNoinline) + + override fun hashCode(): Int { + var currentHashCode = cachedHashCode + if (currentHashCode != 0) return currentHashCode + + currentHashCode = computeHashCode() + cachedHashCode = currentHashCode + return currentHashCode + } + + override fun equals(other: Any?) = when { + other === this -> true + other is CirValueParameterInternedImpl -> { + name == other.name + && returnType == other.returnType + && annotations == other.annotations + && varargElementType == other.varargElementType + && declaresDefaultValue == other.declaresDefaultValue + && isCrossinline == other.isCrossinline + && isNoinline == other.isNoinline + } + else -> false + } + + override fun toString() = buildString { + if (varargElementType != null) append("vararg ") + append(name) + append(": ") + append(returnType) + } } diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirValueParameterFactory.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirValueParameterFactory.kt index dfe578af659..e80bef16064 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirValueParameterFactory.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirValueParameterFactory.kt @@ -8,17 +8,11 @@ package org.jetbrains.kotlin.descriptors.commonizer.cir.factory import kotlinx.metadata.Flag import kotlinx.metadata.KmValueParameter import kotlinx.metadata.klib.annotations -import org.jetbrains.kotlin.descriptors.commonizer.cir.CirAnnotation import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName -import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter -import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirValueParameterImpl -import org.jetbrains.kotlin.descriptors.commonizer.utils.Interner object CirValueParameterFactory { - private val interner = Interner() - - fun create(source: KmValueParameter, typeResolver: CirTypeResolver): CirValueParameter = create( + fun create(source: KmValueParameter, typeResolver: CirTypeResolver) = CirValueParameter.createInterned( annotations = CirAnnotationFactory.createAnnotations(source.flags, typeResolver, source::annotations), name = CirName.create(source.name), returnType = CirTypeFactory.create(source.type!!, typeResolver), @@ -27,26 +21,4 @@ object CirValueParameterFactory { isCrossinline = Flag.ValueParameter.IS_CROSSINLINE(source.flags), isNoinline = Flag.ValueParameter.IS_NOINLINE(source.flags) ) - - fun create( - annotations: List, - name: CirName, - returnType: CirType, - varargElementType: CirType?, - declaresDefaultValue: Boolean, - isCrossinline: Boolean, - isNoinline: Boolean - ): CirValueParameter { - return interner.intern( - CirValueParameterImpl( - annotations = annotations, - name = name, - returnType = returnType, - varargElementType = varargElementType, - declaresDefaultValue = declaresDefaultValue, - isCrossinline = isCrossinline, - isNoinline = isNoinline - ) - ) - } } diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/impl/CirValueParameterImpl.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/impl/CirValueParameterImpl.kt deleted file mode 100644 index 9acc38366f9..00000000000 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/impl/CirValueParameterImpl.kt +++ /dev/null @@ -1,57 +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.CirAnnotation -import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName -import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType -import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter -import org.jetbrains.kotlin.descriptors.commonizer.utils.appendHashCode -import org.jetbrains.kotlin.descriptors.commonizer.utils.hashCode - -data class CirValueParameterImpl( - override val annotations: List, - override val name: CirName, - override val returnType: CirType, - override val varargElementType: CirType?, - override val declaresDefaultValue: Boolean, - override val isCrossinline: Boolean, - override val isNoinline: Boolean -) : CirValueParameter { - // See also org.jetbrains.kotlin.types.KotlinType.cachedHashCode - private var cachedHashCode = 0 - - private fun computeHashCode() = hashCode(name) - .appendHashCode(annotations) - .appendHashCode(returnType) - .appendHashCode(varargElementType) - .appendHashCode(declaresDefaultValue) - .appendHashCode(isCrossinline) - .appendHashCode(isNoinline) - - 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 CirValueParameterImpl -> { - name == other.name - && returnType == other.returnType - && annotations == other.annotations - && varargElementType == other.varargElementType - && declaresDefaultValue == other.declaresDefaultValue - && isCrossinline == other.isCrossinline - && isNoinline == other.isNoinline - } - else -> false - } -} diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/CallableValueParametersCommonizer.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/CallableValueParametersCommonizer.kt index a5d1f482d30..cbe51e3e753 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/CallableValueParametersCommonizer.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/CallableValueParametersCommonizer.kt @@ -10,7 +10,6 @@ import org.jetbrains.kotlin.descriptors.commonizer.cir.CirCallableMemberWithPara import org.jetbrains.kotlin.descriptors.commonizer.cir.CirHasAnnotations import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter -import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirValueParameterFactory import org.jetbrains.kotlin.descriptors.commonizer.core.CallableValueParametersCommonizer.CallableToPatch.Companion.doNothing import org.jetbrains.kotlin.descriptors.commonizer.core.CallableValueParametersCommonizer.CallableToPatch.Companion.patchCallables import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers @@ -51,7 +50,7 @@ class CallableValueParametersCommonizer( callable.valueParameters = callable.valueParameters.compactMapIndexed { index, valueParameter -> val newName = newNames[index] if (valueParameter.name != newName) { - CirValueParameterFactory.create( + CirValueParameter.createInterned( annotations = valueParameter.annotations, name = newName, returnType = valueParameter.returnType, diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/ValueParameterCommonizer.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/ValueParameterCommonizer.kt index 3503b61479d..478cde4f286 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/ValueParameterCommonizer.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/ValueParameterCommonizer.kt @@ -8,7 +8,6 @@ package org.jetbrains.kotlin.descriptors.commonizer.core import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter -import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirValueParameterFactory import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers import org.jetbrains.kotlin.descriptors.commonizer.utils.isNull @@ -19,7 +18,7 @@ class ValueParameterCommonizer(classifiers: CirKnownClassifiers) : AbstractStand private var isCrossinline = true private var isNoinline = true - override fun commonizationResult() = CirValueParameterFactory.create( + override fun commonizationResult() = CirValueParameter.createInterned( annotations = emptyList(), name = name, returnType = returnType.result, diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/metadata/entityBuilders.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/metadata/entityBuilders.kt index 6bfe265a6dd..405c7f4911b 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/metadata/entityBuilders.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/metadata/entityBuilders.kt @@ -11,7 +11,6 @@ import org.jetbrains.kotlin.backend.common.serialization.metadata.DynamicTypeDes import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor import org.jetbrains.kotlin.descriptors.commonizer.cir.* import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeFactory -import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirValueParameterImpl import org.jetbrains.kotlin.descriptors.commonizer.core.computeExpandedType import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.* import org.jetbrains.kotlin.descriptors.commonizer.metadata.TypeAliasExpansion.* @@ -168,15 +167,15 @@ internal fun CirProperty.buildProperty( property.receiverParameterType = receiver.type.buildType(context) } setter?.takeIf { !it.isDefault }?.let { setter -> - property.setterParameter = CirValueParameterImpl( - annotations = setter.parameterAnnotations, - name = DEFAULT_SETTER_VALUE_NAME, - returnType = returnType, - varargElementType = null, - declaresDefaultValue = false, - isCrossinline = false, - isNoinline = false - ).buildValueParameter(context) + property.setterParameter = object : CirValueParameter { + override val annotations get() = setter.parameterAnnotations + override val name get() = DEFAULT_SETTER_VALUE_NAME + override val returnType get() = this@buildProperty.returnType + override val varargElementType: CirType? get() = null + override val declaresDefaultValue get() = false + override val isCrossinline get() = false + override val isNoinline get() = false + }.buildValueParameter(context) } property.returnType = returnType.buildType(context) } diff --git a/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/core/ValueParameterCommonizerTest.kt b/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/core/ValueParameterCommonizerTest.kt index ea63726d973..b68adf99aa3 100644 --- a/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/core/ValueParameterCommonizerTest.kt +++ b/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/core/ValueParameterCommonizerTest.kt @@ -9,7 +9,7 @@ import org.jetbrains.kotlin.descriptors.commonizer.cir.CirAnnotation import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter -import org.jetbrains.kotlin.descriptors.commonizer.core.CirTestValueParameter.Companion.areEqual +import org.jetbrains.kotlin.descriptors.commonizer.core.CirValueParameterTestImpl.Companion.areEqual import org.jetbrains.kotlin.descriptors.commonizer.core.TypeCommonizerTest.Companion.areEqual import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers import org.jetbrains.kotlin.descriptors.commonizer.utils.MOCK_CLASSIFIERS @@ -156,7 +156,7 @@ class ValueParameterCommonizerTest : AbstractCommonizerTest, override val returnType: CirType, diff --git a/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/core/ValueParameterListCommonizerTest.kt b/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/core/ValueParameterListCommonizerTest.kt index 08cf7cbd039..0d7237d57c3 100644 --- a/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/core/ValueParameterListCommonizerTest.kt +++ b/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/core/ValueParameterListCommonizerTest.kt @@ -6,7 +6,7 @@ package org.jetbrains.kotlin.descriptors.commonizer.core import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter -import org.jetbrains.kotlin.descriptors.commonizer.core.CirTestValueParameter.Companion.areEqual +import org.jetbrains.kotlin.descriptors.commonizer.core.CirValueParameterTestImpl.Companion.areEqual import org.jetbrains.kotlin.descriptors.commonizer.utils.MOCK_CLASSIFIERS import org.junit.Test