[Commonizer] Drop CirValueParameter interface for plain class

This commit is contained in:
sebastian.sellmair
2021-07-07 15:14:06 +02:00
committed by Space
parent e246a12cd3
commit 4a4516731a
4 changed files with 79 additions and 83 deletions
@@ -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<CirAnnotation>,
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<CirValueParameterInternedImpl>()
}
}
private class CirValueParameterInternedImpl(
class CirValueParameter private constructor(
override val annotations: List<CirAnnotation>,
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<CirValueParameter>()
fun createInterned(
annotations: List<CirAnnotation>,
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<CirAnnotation> = 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
)
}
}
@@ -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)
}
@@ -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<CirValueParameter, C
): CirValueParameter {
val returnType = mockClassType(returnTypeClassId)
return CirValueParameterTestImpl(
return CirValueParameter.createInterned(
name = CirName.create(name),
returnType = returnType,
varargElementType = returnType.takeIf { hasVarargElementType }, // the vararg type itself does not matter here, only it's presence matters
isCrossinline = isCrossinline,
isNoinline = isNoinline,
declaresDefaultValue = declaresDefaultValue
declaresDefaultValue = declaresDefaultValue,
annotations = emptyList()
)
}
}
}
internal data class CirValueParameterTestImpl(
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 {
override val annotations: List<CirAnnotation> 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))
}
@@ -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