diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/CirType.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/CirType.kt index 4ffc1b716e1..603a4c6e0a5 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/CirType.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/CirType.kt @@ -9,8 +9,10 @@ import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeFactory import org.jetbrains.kotlin.types.AbbreviatedType import org.jetbrains.kotlin.types.Variance +typealias CirTypeSignature = String + sealed class CirType { - abstract val fqNameWithTypeParameters: String + abstract val signature: CirTypeSignature } /** @@ -34,6 +36,6 @@ abstract class CirSimpleType : CirType(), CirHasVisibility { data class CirTypeProjection(val projectionKind: Variance, val isStarProjection: Boolean, val type: CirType) data class CirFlexibleType(val lowerBound: CirSimpleType, val upperBound: CirSimpleType) : CirType() { - override val fqNameWithTypeParameters: String - get() = lowerBound.fqNameWithTypeParameters + override val signature: CirTypeSignature + get() = lowerBound.signature } diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeFactory.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeFactory.kt index 374e63aff85..30e92bfaa9f 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeFactory.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeFactory.kt @@ -13,7 +13,7 @@ import org.jetbrains.kotlin.descriptors.commonizer.cir.* import org.jetbrains.kotlin.descriptors.commonizer.cir.impl.CirSimpleTypeImpl import org.jetbrains.kotlin.descriptors.commonizer.utils.Interner import org.jetbrains.kotlin.descriptors.commonizer.utils.declarationDescriptor -import org.jetbrains.kotlin.descriptors.commonizer.utils.fqNameWithTypeParameters +import org.jetbrains.kotlin.descriptors.commonizer.utils.signature import org.jetbrains.kotlin.types.* object CirTypeFactory { @@ -42,7 +42,7 @@ object CirTypeFactory { }, isMarkedNullable = abbreviation.isMarkedNullable, isDefinitelyNotNullType = abbreviation.isDefinitelyNotNullType, - fqNameWithTypeParameters = source.fqNameWithTypeParameters + signature = source.signature ) } diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/impl/CirSimpleTypeImpl.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/impl/CirSimpleTypeImpl.kt index 7da5d873218..59469774f97 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/impl/CirSimpleTypeImpl.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/impl/CirSimpleTypeImpl.kt @@ -16,7 +16,7 @@ data class CirSimpleTypeImpl( override val arguments: List, override val isMarkedNullable: Boolean, override val isDefinitelyNotNullType: Boolean, - override val fqNameWithTypeParameters: String + override val signature: CirTypeSignature ) : CirSimpleType() { // See also org.jetbrains.kotlin.types.KotlinType.cachedHashCode private var cachedHashCode = 0 @@ -26,7 +26,7 @@ data class CirSimpleTypeImpl( .appendHashCode(arguments) .appendHashCode(isMarkedNullable) .appendHashCode(isDefinitelyNotNullType) - .appendHashCode(fqNameWithTypeParameters) + .appendHashCode(signature) override fun hashCode(): Int { var currentHashCode = cachedHashCode @@ -44,7 +44,7 @@ data class CirSimpleTypeImpl( && classifierId == other.classifierId && visibility == other.visibility && arguments == other.arguments - && fqNameWithTypeParameters == other.fqNameWithTypeParameters + && signature == other.signature && isDefinitelyNotNullType == other.isDefinitelyNotNullType } else -> false diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/CommonizationVisitor.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/CommonizationVisitor.kt index b7934bcd2a3..eed94b29caf 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/CommonizationVisitor.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/CommonizationVisitor.kt @@ -6,6 +6,7 @@ package org.jetbrains.kotlin.descriptors.commonizer.core import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType +import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeSignature import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.* import org.jetbrains.kotlin.descriptors.commonizer.utils.CommonizedGroupMap import org.jetbrains.kotlin.descriptors.commonizer.utils.internedClassId @@ -92,10 +93,10 @@ internal class CommonizationVisitor( } // find out common (and commonized) supertypes - val supertypesMap = CommonizedGroupMap(node.targetDeclarations.size) + val supertypesMap = CommonizedGroupMap(node.targetDeclarations.size) node.targetDeclarations.forEachIndexed { index, clazz -> for (supertype in clazz!!.supertypes) { - supertypesMap[supertype.fqNameWithTypeParameters][index] = supertype + supertypesMap[supertype.signature][index] = supertype } } diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/approximationKeys.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/approximationKeys.kt index 466805da0a7..85d8b07d913 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/approximationKeys.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/mergedtree/approximationKeys.kt @@ -8,40 +8,41 @@ 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.commonizer.cir.CirTypeSignature import org.jetbrains.kotlin.descriptors.commonizer.core.Commonizer -import org.jetbrains.kotlin.descriptors.commonizer.utils.fqNameWithTypeParameters import org.jetbrains.kotlin.descriptors.commonizer.utils.intern +import org.jetbrains.kotlin.descriptors.commonizer.utils.signature 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: String? + val extensionReceiverParameter: CirTypeSignature? ) { constructor(property: PropertyDescriptor) : this( property.name.intern(), - property.extensionReceiverParameter?.type?.fqNameWithTypeParameters + property.extensionReceiverParameter?.type?.signature ) } /** Used for approximation of [SimpleFunctionDescriptor]s before running concrete [Commonizer]s */ data class FunctionApproximationKey( val name: Name, - val valueParameters: List>, - val extensionReceiverParameter: String? + val valueParameters: List>, + val extensionReceiverParameter: CirTypeSignature? ) { constructor(function: SimpleFunctionDescriptor) : this( function.name.intern(), - function.valueParameters.map { it.name.intern() to it.type.fqNameWithTypeParameters }, - function.extensionReceiverParameter?.type?.fqNameWithTypeParameters + function.valueParameters.map { it.name.intern() to it.type.signature }, + function.extensionReceiverParameter?.type?.signature ) } /** Used for approximation of [ConstructorDescriptor]s before running concrete [Commonizer]s */ data class ConstructorApproximationKey( - val valueParameters: List> + val valueParameters: List> ) { constructor(constructor: ConstructorDescriptor) : this( - constructor.valueParameters.map { it.name.intern() to it.type.fqNameWithTypeParameters } + constructor.valueParameters.map { it.name.intern() to it.type.signature } ) } \ No newline at end of file diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/stats/RawStatsCollector.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/stats/RawStatsCollector.kt index 27178545c74..073482d5121 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/stats/RawStatsCollector.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/stats/RawStatsCollector.kt @@ -8,7 +8,7 @@ package org.jetbrains.kotlin.descriptors.commonizer.stats import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.commonizer.stats.DeclarationType.Companion.declarationType import org.jetbrains.kotlin.descriptors.commonizer.utils.firstNonNull -import org.jetbrains.kotlin.descriptors.commonizer.utils.fqNameWithTypeParameters +import org.jetbrains.kotlin.descriptors.commonizer.utils.signature import org.jetbrains.kotlin.konan.target.KonanTarget import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe @@ -78,7 +78,7 @@ class RawStatsCollector( // extension receiver if (firstNotNull is PropertyDescriptor || firstNotNull is SimpleFunctionDescriptor) { statsRow.extensionReceiver = - (firstNotNull as CallableDescriptor).extensionReceiverParameter?.type?.fqNameWithTypeParameters.orEmpty() + (firstNotNull as CallableDescriptor).extensionReceiverParameter?.type?.signature.orEmpty() } if (firstNotNull is ConstructorDescriptor || firstNotNull is SimpleFunctionDescriptor) { @@ -86,7 +86,7 @@ class RawStatsCollector( // parameter names statsRow.parameterNames = functionDescriptor.valueParameters.joinToString { it.name.asString() } // parameter types - statsRow.parameterTypes = functionDescriptor.valueParameters.joinToString { it.type.fqNameWithTypeParameters } + statsRow.parameterTypes = functionDescriptor.valueParameters.joinToString { it.type.signature } } var isLiftedUp = !lastIsNull diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/utils/type.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/utils/type.kt index 4ae48838391..c2f4b813bc3 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/utils/type.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/utils/type.kt @@ -9,6 +9,7 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor import org.jetbrains.kotlin.descriptors.ClassifierDescriptor import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor +import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeSignature import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.resolve.descriptorUtil.classId import org.jetbrains.kotlin.types.AbbreviatedType @@ -27,13 +28,13 @@ internal val ClassifierDescriptorWithTypeParameters.internedClassId: ClassId else -> error("Unexpected containing declaration type for $this: ${owner::class}, $owner") } -internal val KotlinType.fqNameWithTypeParameters: String +internal val KotlinType.signature: CirTypeSignature get() { // use of interner saves up to 95% of duplicates - return stringInterner.intern(buildString { buildFqNameWithTypeParameters(this@fqNameWithTypeParameters, HashSet()) }) + return typeSignatureInterner.intern(buildString { buildTypeSignature(this@signature, HashSet()) }) } -private fun StringBuilder.buildFqNameWithTypeParameters(type: KotlinType, exploredTypeParameters: MutableSet) { +private fun StringBuilder.buildTypeSignature(type: KotlinType, exploredTypeParameters: MutableSet) { val typeParameterDescriptor = TypeUtils.getTypeParameterDescriptorOrNull(type) if (typeParameterDescriptor != null) { // N.B this is type parameter type @@ -44,7 +45,7 @@ private fun StringBuilder.buildFqNameWithTypeParameters(type: KotlinType, explor typeParameterDescriptor.upperBounds.forEachIndexed { index, upperBound -> if (index > 0) append(",") - buildFqNameWithTypeParameters(upperBound, exploredTypeParameters) + buildTypeSignature(upperBound, exploredTypeParameters) } append("]") } @@ -66,7 +67,7 @@ private fun StringBuilder.buildFqNameWithTypeParameters(type: KotlinType, explor val variance = argument.projectionKind if (variance != Variance.INVARIANT) append(variance).append(" ") - buildFqNameWithTypeParameters(argument.type, exploredTypeParameters) + buildTypeSignature(argument.type, exploredTypeParameters) } } append(">") @@ -77,5 +78,5 @@ private fun StringBuilder.buildFqNameWithTypeParameters(type: KotlinType, explor append("?") } -// dedicated to hold unique entries of "fqNameWithTypeParameters" -private val stringInterner = Interner() +// dedicated to hold unique entries of "signature" +private val typeSignatureInterner = Interner() diff --git a/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/utils/ComparingDeclarationsVisitor.kt b/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/utils/ComparingDeclarationsVisitor.kt index bb3a8797f19..c7fb7e02b0b 100644 --- a/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/utils/ComparingDeclarationsVisitor.kt +++ b/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/utils/ComparingDeclarationsVisitor.kt @@ -247,10 +247,10 @@ internal class ComparingDeclarationsVisitor( context.assertSetsEqual(expectedSealedSubclassesFqNames, actualSealedSubclassesFqNames, "Sealed subclasses FQ names") } - val expectedSupertypeFqNames = expected.typeConstructor.supertypes.mapTo(HashSet()) { it.fqNameWithTypeParameters } - val actualSupertypeFqNames = actual.typeConstructor.supertypes.mapTo(HashSet()) { it.fqNameWithTypeParameters } + val expectedSupertypeSignatures = expected.typeConstructor.supertypes.mapTo(HashSet()) { it.signature } + val actualSupertypeSignatures = actual.typeConstructor.supertypes.mapTo(HashSet()) { it.signature } - context.assertSetsEqual(expectedSupertypeFqNames, actualSupertypeFqNames, "Supertypes FQ names") + context.assertSetsEqual(expectedSupertypeSignatures, actualSupertypeSignatures, "Supertypes signatures") if (expected.constructors.isNotEmpty() || actual.constructors.isNotEmpty()) { val expectedConstructors = expected.constructors.associateBy { ConstructorApproximationKey(it) }