[Commonizer] Encapsulate interning inside of CIR entities: CirValueParameter

This commit is contained in:
Dmitriy Dolovov
2021-03-04 12:56:18 +03:00
parent f43626d545
commit 5e41a7c66b
8 changed files with 95 additions and 104 deletions
@@ -5,10 +5,89 @@
package org.jetbrains.kotlin.descriptors.commonizer.cir 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 { interface CirValueParameter : CirHasAnnotations, CirHasName {
val returnType: CirType val returnType: CirType
val varargElementType: CirType? val varargElementType: CirType?
val declaresDefaultValue: Boolean val declaresDefaultValue: Boolean
val isCrossinline: Boolean val isCrossinline: Boolean
val isNoinline: 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(
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 {
// 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)
}
} }
@@ -8,17 +8,11 @@ package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import kotlinx.metadata.Flag import kotlinx.metadata.Flag
import kotlinx.metadata.KmValueParameter import kotlinx.metadata.KmValueParameter
import kotlinx.metadata.klib.annotations 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.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter 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 { object CirValueParameterFactory {
private val interner = Interner<CirValueParameter>() fun create(source: KmValueParameter, typeResolver: CirTypeResolver) = CirValueParameter.createInterned(
fun create(source: KmValueParameter, typeResolver: CirTypeResolver): CirValueParameter = create(
annotations = CirAnnotationFactory.createAnnotations(source.flags, typeResolver, source::annotations), annotations = CirAnnotationFactory.createAnnotations(source.flags, typeResolver, source::annotations),
name = CirName.create(source.name), name = CirName.create(source.name),
returnType = CirTypeFactory.create(source.type!!, typeResolver), returnType = CirTypeFactory.create(source.type!!, typeResolver),
@@ -27,26 +21,4 @@ object CirValueParameterFactory {
isCrossinline = Flag.ValueParameter.IS_CROSSINLINE(source.flags), isCrossinline = Flag.ValueParameter.IS_CROSSINLINE(source.flags),
isNoinline = Flag.ValueParameter.IS_NOINLINE(source.flags) isNoinline = Flag.ValueParameter.IS_NOINLINE(source.flags)
) )
fun create(
annotations: List<CirAnnotation>,
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
)
)
}
} }
@@ -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<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 {
// 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
}
}
@@ -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.CirHasAnnotations
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter 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.doNothing
import org.jetbrains.kotlin.descriptors.commonizer.core.CallableValueParametersCommonizer.CallableToPatch.Companion.patchCallables import org.jetbrains.kotlin.descriptors.commonizer.core.CallableValueParametersCommonizer.CallableToPatch.Companion.patchCallables
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers
@@ -51,7 +50,7 @@ class CallableValueParametersCommonizer(
callable.valueParameters = callable.valueParameters.compactMapIndexed { index, valueParameter -> callable.valueParameters = callable.valueParameters.compactMapIndexed { index, valueParameter ->
val newName = newNames[index] val newName = newNames[index]
if (valueParameter.name != newName) { if (valueParameter.name != newName) {
CirValueParameterFactory.create( CirValueParameter.createInterned(
annotations = valueParameter.annotations, annotations = valueParameter.annotations,
name = newName, name = newName,
returnType = valueParameter.returnType, returnType = valueParameter.returnType,
@@ -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.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter 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.mergedtree.CirKnownClassifiers
import org.jetbrains.kotlin.descriptors.commonizer.utils.isNull import org.jetbrains.kotlin.descriptors.commonizer.utils.isNull
@@ -19,7 +18,7 @@ class ValueParameterCommonizer(classifiers: CirKnownClassifiers) : AbstractStand
private var isCrossinline = true private var isCrossinline = true
private var isNoinline = true private var isNoinline = true
override fun commonizationResult() = CirValueParameterFactory.create( override fun commonizationResult() = CirValueParameter.createInterned(
annotations = emptyList(), annotations = emptyList(),
name = name, name = name,
returnType = returnType.result, returnType = returnType.result,
@@ -11,7 +11,6 @@ import org.jetbrains.kotlin.backend.common.serialization.metadata.DynamicTypeDes
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.commonizer.cir.* import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeFactory 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.core.computeExpandedType
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.* import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.*
import org.jetbrains.kotlin.descriptors.commonizer.metadata.TypeAliasExpansion.* import org.jetbrains.kotlin.descriptors.commonizer.metadata.TypeAliasExpansion.*
@@ -168,15 +167,15 @@ internal fun CirProperty.buildProperty(
property.receiverParameterType = receiver.type.buildType(context) property.receiverParameterType = receiver.type.buildType(context)
} }
setter?.takeIf { !it.isDefault }?.let { setter -> setter?.takeIf { !it.isDefault }?.let { setter ->
property.setterParameter = CirValueParameterImpl( property.setterParameter = object : CirValueParameter {
annotations = setter.parameterAnnotations, override val annotations get() = setter.parameterAnnotations
name = DEFAULT_SETTER_VALUE_NAME, override val name get() = DEFAULT_SETTER_VALUE_NAME
returnType = returnType, override val returnType get() = this@buildProperty.returnType
varargElementType = null, override val varargElementType: CirType? get() = null
declaresDefaultValue = false, override val declaresDefaultValue get() = false
isCrossinline = false, override val isCrossinline get() = false
isNoinline = false override val isNoinline get() = false
).buildValueParameter(context) }.buildValueParameter(context)
} }
property.returnType = returnType.buildType(context) property.returnType = returnType.buildType(context)
} }
@@ -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.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter 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.core.TypeCommonizerTest.Companion.areEqual
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers
import org.jetbrains.kotlin.descriptors.commonizer.utils.MOCK_CLASSIFIERS import org.jetbrains.kotlin.descriptors.commonizer.utils.MOCK_CLASSIFIERS
@@ -156,7 +156,7 @@ class ValueParameterCommonizerTest : AbstractCommonizerTest<CirValueParameter, C
): CirValueParameter { ): CirValueParameter {
val returnType = mockClassType(returnTypeClassId) val returnType = mockClassType(returnTypeClassId)
return CirTestValueParameter( return CirValueParameterTestImpl(
name = CirName.create(name), name = CirName.create(name),
annotations = emptyList(), annotations = emptyList(),
returnType = returnType, returnType = returnType,
@@ -169,7 +169,7 @@ class ValueParameterCommonizerTest : AbstractCommonizerTest<CirValueParameter, C
} }
} }
internal data class CirTestValueParameter( internal data class CirValueParameterTestImpl(
override val name: CirName, override val name: CirName,
override val annotations: List<CirAnnotation>, override val annotations: List<CirAnnotation>,
override val returnType: CirType, override val returnType: CirType,
@@ -6,7 +6,7 @@
package org.jetbrains.kotlin.descriptors.commonizer.core package org.jetbrains.kotlin.descriptors.commonizer.core
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirValueParameter 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.jetbrains.kotlin.descriptors.commonizer.utils.MOCK_CLASSIFIERS
import org.junit.Test import org.junit.Test