[Commonizer] More compact approximation keys

^KT-34602
This commit is contained in:
Dmitriy Dolovov
2020-08-12 22:42:29 +07:00
parent 4418dc85ca
commit 3b901a28d5
2 changed files with 46 additions and 11 deletions
@@ -8,16 +8,19 @@ package org.jetbrains.kotlin.descriptors.commonizer.mergedtree
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeSignature
import org.jetbrains.kotlin.descriptors.commonizer.core.Commonizer
import org.jetbrains.kotlin.descriptors.commonizer.utils.intern
import org.jetbrains.kotlin.descriptors.commonizer.utils.signature
import org.jetbrains.kotlin.descriptors.commonizer.utils.hashCode
import org.jetbrains.kotlin.descriptors.commonizer.utils.appendHashCode
import org.jetbrains.kotlin.name.Name
/** Used for approximation of [PropertyDescriptor]s before running concrete [Commonizer]s */
data class PropertyApproximationKey(
val name: Name,
val extensionReceiverParameter: CirTypeSignature?
private val name: Name,
private val extensionReceiverParameterType: CirTypeSignature?
) {
constructor(property: PropertyDescriptor) : this(
property.name.intern(),
@@ -26,23 +29,49 @@ data class PropertyApproximationKey(
}
/** Used for approximation of [SimpleFunctionDescriptor]s before running concrete [Commonizer]s */
data class FunctionApproximationKey(
val name: Name,
val valueParameters: List<CirTypeSignature>,
val extensionReceiverParameter: CirTypeSignature?
class FunctionApproximationKey(
private val name: Name,
private val valueParametersTypes: Array<CirTypeSignature>,
private val extensionReceiverParameterType: CirTypeSignature?
) {
constructor(function: SimpleFunctionDescriptor) : this(
function.name.intern(),
function.valueParameters.map { it.type.signature },
function.valueParameters.toTypeSignatures(),
function.extensionReceiverParameter?.type?.signature
)
override fun equals(other: Any?): Boolean {
if (other !is FunctionApproximationKey)
return false
return name == other.name
&& valueParametersTypes.contentEquals(other.valueParametersTypes)
&& extensionReceiverParameterType == other.extensionReceiverParameterType
}
override fun hashCode() = hashCode(name)
.appendHashCode(valueParametersTypes)
.appendHashCode(extensionReceiverParameterType)
}
/** Used for approximation of [ConstructorDescriptor]s before running concrete [Commonizer]s */
data class ConstructorApproximationKey(
val valueParameters: List<CirTypeSignature>
class ConstructorApproximationKey(
private val valueParametersTypes: Array<CirTypeSignature>
) {
constructor(constructor: ConstructorDescriptor) : this(
constructor.valueParameters.map { it.type.signature }
constructor.valueParameters.toTypeSignatures()
)
}
override fun equals(other: Any?): Boolean {
if (other !is ConstructorApproximationKey)
return false
return valueParametersTypes.contentEquals(other.valueParametersTypes)
}
override fun hashCode() = hashCode(valueParametersTypes)
}
@Suppress("NOTHING_TO_INLINE")
private inline fun List<ValueParameterDescriptor>.toTypeSignatures(): Array<CirTypeSignature> =
Array(size) { index -> this[index].type.signature }
@@ -31,5 +31,11 @@ internal fun Any?.isNull(): Boolean = this == null
@Suppress("NOTHING_TO_INLINE")
inline fun hashCode(value: Any?): Int = value.hashCode()
@Suppress("NOTHING_TO_INLINE")
inline fun hashCode(array: Array<*>?): Int = array?.contentHashCode() ?: 0
@Suppress("NOTHING_TO_INLINE")
inline fun Int.appendHashCode(value: Any?): Int = 31 * this + hashCode(value)
@Suppress("NOTHING_TO_INLINE")
inline fun Int.appendHashCode(array: Array<*>?): Int = 31 * this + hashCode(array)