[Commonizer] Clean-up CirTypeSignature usages

This commit is contained in:
Dmitriy Dolovov
2020-07-02 18:45:14 +07:00
parent ffd0c69698
commit 3bb234b17c
8 changed files with 37 additions and 32 deletions
@@ -9,8 +9,10 @@ import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeFactory
import org.jetbrains.kotlin.types.AbbreviatedType import org.jetbrains.kotlin.types.AbbreviatedType
import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.Variance
typealias CirTypeSignature = String
sealed class CirType { 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 CirTypeProjection(val projectionKind: Variance, val isStarProjection: Boolean, val type: CirType)
data class CirFlexibleType(val lowerBound: CirSimpleType, val upperBound: CirSimpleType) : CirType() { data class CirFlexibleType(val lowerBound: CirSimpleType, val upperBound: CirSimpleType) : CirType() {
override val fqNameWithTypeParameters: String override val signature: CirTypeSignature
get() = lowerBound.fqNameWithTypeParameters get() = lowerBound.signature
} }
@@ -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.cir.impl.CirSimpleTypeImpl
import org.jetbrains.kotlin.descriptors.commonizer.utils.Interner import org.jetbrains.kotlin.descriptors.commonizer.utils.Interner
import org.jetbrains.kotlin.descriptors.commonizer.utils.declarationDescriptor 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.* import org.jetbrains.kotlin.types.*
object CirTypeFactory { object CirTypeFactory {
@@ -42,7 +42,7 @@ object CirTypeFactory {
}, },
isMarkedNullable = abbreviation.isMarkedNullable, isMarkedNullable = abbreviation.isMarkedNullable,
isDefinitelyNotNullType = abbreviation.isDefinitelyNotNullType, isDefinitelyNotNullType = abbreviation.isDefinitelyNotNullType,
fqNameWithTypeParameters = source.fqNameWithTypeParameters signature = source.signature
) )
} }
@@ -16,7 +16,7 @@ data class CirSimpleTypeImpl(
override val arguments: List<CirTypeProjection>, override val arguments: List<CirTypeProjection>,
override val isMarkedNullable: Boolean, override val isMarkedNullable: Boolean,
override val isDefinitelyNotNullType: Boolean, override val isDefinitelyNotNullType: Boolean,
override val fqNameWithTypeParameters: String override val signature: CirTypeSignature
) : CirSimpleType() { ) : CirSimpleType() {
// See also org.jetbrains.kotlin.types.KotlinType.cachedHashCode // See also org.jetbrains.kotlin.types.KotlinType.cachedHashCode
private var cachedHashCode = 0 private var cachedHashCode = 0
@@ -26,7 +26,7 @@ data class CirSimpleTypeImpl(
.appendHashCode(arguments) .appendHashCode(arguments)
.appendHashCode(isMarkedNullable) .appendHashCode(isMarkedNullable)
.appendHashCode(isDefinitelyNotNullType) .appendHashCode(isDefinitelyNotNullType)
.appendHashCode(fqNameWithTypeParameters) .appendHashCode(signature)
override fun hashCode(): Int { override fun hashCode(): Int {
var currentHashCode = cachedHashCode var currentHashCode = cachedHashCode
@@ -44,7 +44,7 @@ data class CirSimpleTypeImpl(
&& classifierId == other.classifierId && classifierId == other.classifierId
&& visibility == other.visibility && visibility == other.visibility
&& arguments == other.arguments && arguments == other.arguments
&& fqNameWithTypeParameters == other.fqNameWithTypeParameters && signature == other.signature
&& isDefinitelyNotNullType == other.isDefinitelyNotNullType && isDefinitelyNotNullType == other.isDefinitelyNotNullType
} }
else -> false else -> false
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.descriptors.commonizer.core package org.jetbrains.kotlin.descriptors.commonizer.core
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirType 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.mergedtree.*
import org.jetbrains.kotlin.descriptors.commonizer.utils.CommonizedGroupMap import org.jetbrains.kotlin.descriptors.commonizer.utils.CommonizedGroupMap
import org.jetbrains.kotlin.descriptors.commonizer.utils.internedClassId import org.jetbrains.kotlin.descriptors.commonizer.utils.internedClassId
@@ -92,10 +93,10 @@ internal class CommonizationVisitor(
} }
// find out common (and commonized) supertypes // find out common (and commonized) supertypes
val supertypesMap = CommonizedGroupMap<String, CirType>(node.targetDeclarations.size) val supertypesMap = CommonizedGroupMap<CirTypeSignature, CirType>(node.targetDeclarations.size)
node.targetDeclarations.forEachIndexed { index, clazz -> node.targetDeclarations.forEachIndexed { index, clazz ->
for (supertype in clazz!!.supertypes) { for (supertype in clazz!!.supertypes) {
supertypesMap[supertype.fqNameWithTypeParameters][index] = supertype supertypesMap[supertype.signature][index] = supertype
} }
} }
@@ -8,40 +8,41 @@ package org.jetbrains.kotlin.descriptors.commonizer.mergedtree
import org.jetbrains.kotlin.descriptors.ConstructorDescriptor import org.jetbrains.kotlin.descriptors.ConstructorDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor 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.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.intern
import org.jetbrains.kotlin.descriptors.commonizer.utils.signature
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
/** Used for approximation of [PropertyDescriptor]s before running concrete [Commonizer]s */ /** Used for approximation of [PropertyDescriptor]s before running concrete [Commonizer]s */
data class PropertyApproximationKey( data class PropertyApproximationKey(
val name: Name, val name: Name,
val extensionReceiverParameter: String? val extensionReceiverParameter: CirTypeSignature?
) { ) {
constructor(property: PropertyDescriptor) : this( constructor(property: PropertyDescriptor) : this(
property.name.intern(), property.name.intern(),
property.extensionReceiverParameter?.type?.fqNameWithTypeParameters property.extensionReceiverParameter?.type?.signature
) )
} }
/** Used for approximation of [SimpleFunctionDescriptor]s before running concrete [Commonizer]s */ /** Used for approximation of [SimpleFunctionDescriptor]s before running concrete [Commonizer]s */
data class FunctionApproximationKey( data class FunctionApproximationKey(
val name: Name, val name: Name,
val valueParameters: List<Pair<Name, String>>, val valueParameters: List<Pair<Name, CirTypeSignature>>,
val extensionReceiverParameter: String? val extensionReceiverParameter: CirTypeSignature?
) { ) {
constructor(function: SimpleFunctionDescriptor) : this( constructor(function: SimpleFunctionDescriptor) : this(
function.name.intern(), function.name.intern(),
function.valueParameters.map { it.name.intern() to it.type.fqNameWithTypeParameters }, function.valueParameters.map { it.name.intern() to it.type.signature },
function.extensionReceiverParameter?.type?.fqNameWithTypeParameters function.extensionReceiverParameter?.type?.signature
) )
} }
/** Used for approximation of [ConstructorDescriptor]s before running concrete [Commonizer]s */ /** Used for approximation of [ConstructorDescriptor]s before running concrete [Commonizer]s */
data class ConstructorApproximationKey( data class ConstructorApproximationKey(
val valueParameters: List<Pair<Name, String>> val valueParameters: List<Pair<Name, CirTypeSignature>>
) { ) {
constructor(constructor: ConstructorDescriptor) : this( constructor(constructor: ConstructorDescriptor) : this(
constructor.valueParameters.map { it.name.intern() to it.type.fqNameWithTypeParameters } constructor.valueParameters.map { it.name.intern() to it.type.signature }
) )
} }
@@ -8,7 +8,7 @@ package org.jetbrains.kotlin.descriptors.commonizer.stats
import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.commonizer.stats.DeclarationType.Companion.declarationType 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.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.konan.target.KonanTarget
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe
@@ -78,7 +78,7 @@ class RawStatsCollector(
// extension receiver // extension receiver
if (firstNotNull is PropertyDescriptor || firstNotNull is SimpleFunctionDescriptor) { if (firstNotNull is PropertyDescriptor || firstNotNull is SimpleFunctionDescriptor) {
statsRow.extensionReceiver = statsRow.extensionReceiver =
(firstNotNull as CallableDescriptor).extensionReceiverParameter?.type?.fqNameWithTypeParameters.orEmpty() (firstNotNull as CallableDescriptor).extensionReceiverParameter?.type?.signature.orEmpty()
} }
if (firstNotNull is ConstructorDescriptor || firstNotNull is SimpleFunctionDescriptor) { if (firstNotNull is ConstructorDescriptor || firstNotNull is SimpleFunctionDescriptor) {
@@ -86,7 +86,7 @@ class RawStatsCollector(
// parameter names // parameter names
statsRow.parameterNames = functionDescriptor.valueParameters.joinToString { it.name.asString() } statsRow.parameterNames = functionDescriptor.valueParameters.joinToString { it.name.asString() }
// parameter types // parameter types
statsRow.parameterTypes = functionDescriptor.valueParameters.joinToString { it.type.fqNameWithTypeParameters } statsRow.parameterTypes = functionDescriptor.valueParameters.joinToString { it.type.signature }
} }
var isLiftedUp = !lastIsNull var isLiftedUp = !lastIsNull
@@ -9,6 +9,7 @@ import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters import org.jetbrains.kotlin.descriptors.ClassifierDescriptorWithTypeParameters
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeSignature
import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.resolve.descriptorUtil.classId import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.types.AbbreviatedType 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") else -> error("Unexpected containing declaration type for $this: ${owner::class}, $owner")
} }
internal val KotlinType.fqNameWithTypeParameters: String internal val KotlinType.signature: CirTypeSignature
get() { get() {
// use of interner saves up to 95% of duplicates // 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<KotlinType>) { private fun StringBuilder.buildTypeSignature(type: KotlinType, exploredTypeParameters: MutableSet<KotlinType>) {
val typeParameterDescriptor = TypeUtils.getTypeParameterDescriptorOrNull(type) val typeParameterDescriptor = TypeUtils.getTypeParameterDescriptorOrNull(type)
if (typeParameterDescriptor != null) { if (typeParameterDescriptor != null) {
// N.B this is type parameter type // N.B this is type parameter type
@@ -44,7 +45,7 @@ private fun StringBuilder.buildFqNameWithTypeParameters(type: KotlinType, explor
typeParameterDescriptor.upperBounds.forEachIndexed { index, upperBound -> typeParameterDescriptor.upperBounds.forEachIndexed { index, upperBound ->
if (index > 0) if (index > 0)
append(",") append(",")
buildFqNameWithTypeParameters(upperBound, exploredTypeParameters) buildTypeSignature(upperBound, exploredTypeParameters)
} }
append("]") append("]")
} }
@@ -66,7 +67,7 @@ private fun StringBuilder.buildFqNameWithTypeParameters(type: KotlinType, explor
val variance = argument.projectionKind val variance = argument.projectionKind
if (variance != Variance.INVARIANT) if (variance != Variance.INVARIANT)
append(variance).append(" ") append(variance).append(" ")
buildFqNameWithTypeParameters(argument.type, exploredTypeParameters) buildTypeSignature(argument.type, exploredTypeParameters)
} }
} }
append(">") append(">")
@@ -77,5 +78,5 @@ private fun StringBuilder.buildFqNameWithTypeParameters(type: KotlinType, explor
append("?") append("?")
} }
// dedicated to hold unique entries of "fqNameWithTypeParameters" // dedicated to hold unique entries of "signature"
private val stringInterner = Interner<String>() private val typeSignatureInterner = Interner<CirTypeSignature>()
@@ -247,10 +247,10 @@ internal class ComparingDeclarationsVisitor(
context.assertSetsEqual(expectedSealedSubclassesFqNames, actualSealedSubclassesFqNames, "Sealed subclasses FQ names") context.assertSetsEqual(expectedSealedSubclassesFqNames, actualSealedSubclassesFqNames, "Sealed subclasses FQ names")
} }
val expectedSupertypeFqNames = expected.typeConstructor.supertypes.mapTo(HashSet()) { it.fqNameWithTypeParameters } val expectedSupertypeSignatures = expected.typeConstructor.supertypes.mapTo(HashSet()) { it.signature }
val actualSupertypeFqNames = actual.typeConstructor.supertypes.mapTo(HashSet()) { it.fqNameWithTypeParameters } 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()) { if (expected.constructors.isNotEmpty() || actual.constructors.isNotEmpty()) {
val expectedConstructors = expected.constructors.associateBy { ConstructorApproximationKey(it) } val expectedConstructors = expected.constructors.associateBy { ConstructorApproximationKey(it) }