From 4a4516731aa9dc07d3f56030c21d62493c46a68a Mon Sep 17 00:00:00 2001 From: "sebastian.sellmair" Date: Wed, 7 Jul 2021 15:14:06 +0200 Subject: [PATCH] [Commonizer] Drop CirValueParameter interface for plain class --- .../commonizer/cir/CirValueParameter.kt | 91 +++++++++++-------- .../commonizer/metadata/CirSerializers.kt | 18 ++-- .../core/ValueParameterCommonizerTest.kt | 52 ++++------- .../core/ValueParameterListCommonizerTest.kt | 1 - 4 files changed, 79 insertions(+), 83 deletions(-) diff --git a/native/commonizer/src/org/jetbrains/kotlin/commonizer/cir/CirValueParameter.kt b/native/commonizer/src/org/jetbrains/kotlin/commonizer/cir/CirValueParameter.kt index 55e53993ffb..14f476647d1 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/commonizer/cir/CirValueParameter.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/commonizer/cir/CirValueParameter.kt @@ -9,47 +9,15 @@ import org.jetbrains.kotlin.commonizer.utils.Interner import org.jetbrains.kotlin.commonizer.utils.appendHashCode import org.jetbrains.kotlin.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( +class CirValueParameter private constructor( 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 { + val returnType: CirType, + val varargElementType: CirType?, + val declaresDefaultValue: Boolean, + val isCrossinline: Boolean, + val isNoinline: Boolean +) : CirHasAnnotations, CirHasName { // See also org.jetbrains.kotlin.types.KotlinType.cachedHashCode private var cachedHashCode = 0 @@ -72,7 +40,7 @@ private class CirValueParameterInternedImpl( override fun equals(other: Any?) = when { other === this -> true - other is CirValueParameterInternedImpl -> { + other is CirValueParameter -> { name == other.name && returnType == other.returnType && annotations == other.annotations @@ -90,4 +58,47 @@ private class CirValueParameterInternedImpl( append(": ") append(returnType) } + + companion object { + private val interner = Interner() + + fun createInterned( + annotations: List, + name: CirName, + returnType: CirType, + varargElementType: CirType?, + declaresDefaultValue: Boolean, + isCrossinline: Boolean, + isNoinline: Boolean + ): CirValueParameter = interner.intern( + CirValueParameter( + annotations = annotations, + name = name, + returnType = returnType, + varargElementType = varargElementType, + declaresDefaultValue = declaresDefaultValue, + isCrossinline = isCrossinline, + isNoinline = isNoinline + ) + ) + + fun CirValueParameter.copyInterned( + annotations: List = this.annotations, + name: CirName = this.name, + returnType: CirType = this.returnType, + varargElementType: CirType? = this.varargElementType, + declaresDefaultValue: Boolean = this.declaresDefaultValue, + isCrossinline: Boolean = this.isCrossinline, + isNoinline: Boolean = this.isNoinline + ): CirValueParameter = createInterned( + annotations = annotations, + name = name, + returnType = returnType, + varargElementType = varargElementType, + declaresDefaultValue = declaresDefaultValue, + isCrossinline = isCrossinline, + isNoinline = isNoinline + ) + } + } diff --git a/native/commonizer/src/org/jetbrains/kotlin/commonizer/metadata/CirSerializers.kt b/native/commonizer/src/org/jetbrains/kotlin/commonizer/metadata/CirSerializers.kt index b1321ccb56b..89147db9f0a 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/commonizer/metadata/CirSerializers.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/commonizer/metadata/CirSerializers.kt @@ -164,15 +164,15 @@ internal fun CirProperty.serializeProperty( property.receiverParameterType = receiver.type.serializeType(context) } setter?.takeIf { !it.isDefault }?.let { setter -> - property.setterParameter = object : CirValueParameter { - override val annotations get() = setter.parameterAnnotations - override val name get() = DEFAULT_SETTER_VALUE_NAME - override val returnType get() = this@serializeProperty.returnType - override val varargElementType: CirType? get() = null - override val declaresDefaultValue get() = false - override val isCrossinline get() = false - override val isNoinline get() = false - }.serializeValueParameter(context) + property.setterParameter = CirValueParameter.createInterned( + annotations = setter.parameterAnnotations, + name = DEFAULT_SETTER_VALUE_NAME, + returnType = this@serializeProperty.returnType, + varargElementType = null, + declaresDefaultValue = false, + isCrossinline = false, + isNoinline = false + ).serializeValueParameter(context) } property.returnType = returnType.serializeType(context) } diff --git a/native/commonizer/tests/org/jetbrains/kotlin/commonizer/core/ValueParameterCommonizerTest.kt b/native/commonizer/tests/org/jetbrains/kotlin/commonizer/core/ValueParameterCommonizerTest.kt index 0bee662ad46..1b5a1ed59ba 100644 --- a/native/commonizer/tests/org/jetbrains/kotlin/commonizer/core/ValueParameterCommonizerTest.kt +++ b/native/commonizer/tests/org/jetbrains/kotlin/commonizer/core/ValueParameterCommonizerTest.kt @@ -5,11 +5,8 @@ package org.jetbrains.kotlin.commonizer.core -import org.jetbrains.kotlin.commonizer.cir.CirAnnotation import org.jetbrains.kotlin.commonizer.cir.CirName -import org.jetbrains.kotlin.commonizer.cir.CirType import org.jetbrains.kotlin.commonizer.cir.CirValueParameter -import org.jetbrains.kotlin.commonizer.core.CirValueParameterTestImpl.Companion.areEqual import org.jetbrains.kotlin.commonizer.core.TypeCommonizerTest.Companion.areEqual import org.jetbrains.kotlin.commonizer.mergedtree.CirKnownClassifiers import org.jetbrains.kotlin.commonizer.utils.MOCK_CLASSIFIERS @@ -156,45 +153,34 @@ class ValueParameterCommonizerTest : AbstractCommonizerTest get() = emptyList() - - companion object { - fun areEqual(classifiers: CirKnownClassifiers, a: CirValueParameter, b: CirValueParameter): Boolean { - if (a.name != b.name - || !areEqual(classifiers, a.returnType, b.returnType) - || a.declaresDefaultValue != b.declaresDefaultValue - || a.isCrossinline != b.isCrossinline - || a.isNoinline != b.isNoinline - ) { - return false - } - - val aVarargElementType = a.varargElementType - val bVarargElementType = b.varargElementType - - return (aVarargElementType === bVarargElementType) - || (aVarargElementType != null && bVarargElementType != null - && areEqual(classifiers, aVarargElementType, bVarargElementType)) - } +fun areEqual(classifiers: CirKnownClassifiers, a: CirValueParameter, b: CirValueParameter): Boolean { + if (a.name != b.name + || !areEqual(classifiers, a.returnType, b.returnType) + || a.declaresDefaultValue != b.declaresDefaultValue + || a.isCrossinline != b.isCrossinline + || a.isNoinline != b.isNoinline + ) { + return false } + + val aVarargElementType = a.varargElementType + val bVarargElementType = b.varargElementType + + return (aVarargElementType === bVarargElementType) + || (aVarargElementType != null && bVarargElementType != null + && areEqual(classifiers, aVarargElementType, bVarargElementType)) } + diff --git a/native/commonizer/tests/org/jetbrains/kotlin/commonizer/core/ValueParameterListCommonizerTest.kt b/native/commonizer/tests/org/jetbrains/kotlin/commonizer/core/ValueParameterListCommonizerTest.kt index a7613accf1d..bd8820ce17a 100644 --- a/native/commonizer/tests/org/jetbrains/kotlin/commonizer/core/ValueParameterListCommonizerTest.kt +++ b/native/commonizer/tests/org/jetbrains/kotlin/commonizer/core/ValueParameterListCommonizerTest.kt @@ -6,7 +6,6 @@ package org.jetbrains.kotlin.commonizer.core import org.jetbrains.kotlin.commonizer.cir.CirValueParameter -import org.jetbrains.kotlin.commonizer.core.CirValueParameterTestImpl.Companion.areEqual import org.jetbrains.kotlin.commonizer.utils.MOCK_CLASSIFIERS import org.junit.Test