[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.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
}
@@ -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
)
}
@@ -16,7 +16,7 @@ data class CirSimpleTypeImpl(
override val arguments: List<CirTypeProjection>,
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
@@ -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<String, CirType>(node.targetDeclarations.size)
val supertypesMap = CommonizedGroupMap<CirTypeSignature, CirType>(node.targetDeclarations.size)
node.targetDeclarations.forEachIndexed { index, clazz ->
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.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<Pair<Name, String>>,
val extensionReceiverParameter: String?
val valueParameters: List<Pair<Name, CirTypeSignature>>,
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<Pair<Name, String>>
val valueParameters: List<Pair<Name, CirTypeSignature>>
) {
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.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
@@ -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<KotlinType>) {
private fun StringBuilder.buildTypeSignature(type: KotlinType, exploredTypeParameters: MutableSet<KotlinType>) {
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<String>()
// dedicated to hold unique entries of "signature"
private val typeSignatureInterner = Interner<CirTypeSignature>()
@@ -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) }