[Commonizer] Make nodeBuilders contextless

This commit is contained in:
sebastian.sellmair
2021-03-25 10:56:46 +01:00
parent 66bc751407
commit cb1f3c3e32
7 changed files with 20 additions and 50 deletions
@@ -63,7 +63,7 @@ internal class CommonizationVisitor(
@Suppress("DuplicatedCode") @Suppress("DuplicatedCode")
override fun visitClassNode(node: CirClassNode, data: Unit) { override fun visitClassNode(node: CirClassNode, data: Unit) {
val commonClass = node.commonDeclaration() // commonized class val commonClass = node.commonDeclaration() ?: return // No need to commonize class members
node.constructors.values.forEach { constructor -> node.constructors.values.forEach { constructor ->
constructor.accept(this, Unit) constructor.accept(this, Unit)
@@ -81,22 +81,21 @@ internal class CommonizationVisitor(
clazz.accept(this, Unit) clazz.accept(this, Unit)
} }
if (commonClass != null) {
// companion object should have the same name for each target class, then it could be set to common class
val companionObjectName = node.targetDeclarations.mapTo(HashSet()) { it!!.companion }.singleOrNull()
if (companionObjectName != null) {
val companionObjectNode = node.classes[companionObjectName]
?: error("Can't find node for companion object $companionObjectName in node for class ${node.classifierName}")
if (companionObjectNode.commonDeclaration() != null) { // companion object should have the same name for each target class, then it could be set to common class
// companion object has been successfully commonized val companionObjectName = node.targetDeclarations.mapTo(HashSet()) { it!!.companion }.singleOrNull()
commonClass.companion = companionObjectName if (companionObjectName != null) {
} val companionObjectNode = node.classes[companionObjectName]
?: error("Can't find node for companion object $companionObjectName in node for class ${node.classifierName}")
if (companionObjectNode.commonDeclaration() != null) {
// companion object has been successfully commonized
commonClass.companion = companionObjectName
} }
// find out common (and commonized) supertypes
commonClass.commonizeSupertypes(node.collectCommonSupertypes())
} }
// find out common (and commonized) supertypes
commonClass.commonizeSupertypes(node.collectCommonSupertypes())
} }
override fun visitClassConstructorNode(node: CirClassConstructorNode, data: Unit) { override fun visitClassConstructorNode(node: CirClassConstructorNode, data: Unit) {
@@ -19,7 +19,7 @@ internal object ClassConstructorMerger {
) = with(context) { ) = with(context) {
val approximationKey = ConstructorApproximationKey(constructor, context.typeResolver) val approximationKey = ConstructorApproximationKey(constructor, context.typeResolver)
val constructorNode: CirClassConstructorNode = classNode.constructors.getOrPut(approximationKey) { val constructorNode: CirClassConstructorNode = classNode.constructors.getOrPut(approximationKey) {
buildClassConstructorNode(storageManager, targets, classifiers, classNode.commonDeclaration) buildClassConstructorNode(storageManager, targets, classifiers)
} }
constructorNode.targetDeclarations[context.targetIndex] = CirDeserializers.constructor( constructorNode.targetDeclarations[context.targetIndex] = CirDeserializers.constructor(
source = constructor, source = constructor,
@@ -26,9 +26,8 @@ internal class ClassMerger(
val classId = classEntry.classId val classId = classEntry.classId
val className = classId.relativeNameSegments.last() val className = classId.relativeNameSegments.last()
val maybeClassOwnerNode: CirClassNode? = ownerNode as? CirClassNode
val classNode: CirClassNode = ownerNode.classes.getOrPut(className) { val classNode: CirClassNode = ownerNode.classes.getOrPut(className) {
buildClassNode(storageManager, targets, classifiers, maybeClassOwnerNode?.commonDeclaration, classId) buildClassNode(storageManager, targets, classifiers, classId)
} }
val clazz: KmClass? val clazz: KmClass?
@@ -27,16 +27,14 @@ internal object FunctionMerger {
return return
} }
val maybeClassOwnerNode: CirClassNode? = ownerNode as? CirClassNode
val approximationKey = FunctionApproximationKey(function, context.typeResolver) val approximationKey = FunctionApproximationKey(function, context.typeResolver)
val functionNode: CirFunctionNode = ownerNode.functions.getOrPut(approximationKey) { val functionNode: CirFunctionNode = ownerNode.functions.getOrPut(approximationKey) {
buildFunctionNode(storageManager, targets, classifiers, maybeClassOwnerNode?.commonDeclaration) buildFunctionNode(storageManager, targets, classifiers)
} }
functionNode.targetDeclarations[context.targetIndex] = CirDeserializers.function( functionNode.targetDeclarations[context.targetIndex] = CirDeserializers.function(
name = approximationKey.name, name = approximationKey.name,
source = function, source = function,
containingClass = maybeClassOwnerNode?.targetDeclarations?.get(context.targetIndex), containingClass = ownerNode.run { this as? CirClassNode }?.targetDeclarations?.get(context.targetIndex),
typeResolver = context.typeResolver typeResolver = context.typeResolver
) )
} }
@@ -21,16 +21,14 @@ internal object PropertyMerger {
if (property.isFakeOverride()) if (property.isFakeOverride())
return return
val maybeClassOwnerNode: CirClassNode? = ownerNode as? CirClassNode
val approximationKey = PropertyApproximationKey(property, context.typeResolver) val approximationKey = PropertyApproximationKey(property, context.typeResolver)
val propertyNode: CirPropertyNode = ownerNode.properties.getOrPut(approximationKey) { val propertyNode: CirPropertyNode = ownerNode.properties.getOrPut(approximationKey) {
buildPropertyNode(storageManager, targets, classifiers, maybeClassOwnerNode?.commonDeclaration) buildPropertyNode(storageManager, targets, classifiers)
} }
propertyNode.targetDeclarations[context.targetIndex] = CirDeserializers.property( propertyNode.targetDeclarations[context.targetIndex] = CirDeserializers.property(
name = approximationKey.name, name = approximationKey.name,
source = property, source = property,
containingClass = maybeClassOwnerNode?.targetDeclarations?.get(context.targetIndex), containingClass = ownerNode.run { this as? CirClassNode }?.targetDeclarations?.get(context.targetIndex),
typeResolver = context.typeResolver typeResolver = context.typeResolver
) )
} }
@@ -48,11 +48,9 @@ internal fun buildPropertyNode(
storageManager: StorageManager, storageManager: StorageManager,
size: Int, size: Int,
classifiers: CirKnownClassifiers, classifiers: CirKnownClassifiers,
parentCommonDeclaration: NullableLazyValue<*>?
): CirPropertyNode = buildNode( ): CirPropertyNode = buildNode(
storageManager = storageManager, storageManager = storageManager,
size = size, size = size,
parentCommonDeclaration = parentCommonDeclaration,
commonizerProducer = { PropertyCommonizer(classifiers) }, commonizerProducer = { PropertyCommonizer(classifiers) },
nodeProducer = ::CirPropertyNode nodeProducer = ::CirPropertyNode
) )
@@ -61,11 +59,9 @@ internal fun buildFunctionNode(
storageManager: StorageManager, storageManager: StorageManager,
size: Int, size: Int,
classifiers: CirKnownClassifiers, classifiers: CirKnownClassifiers,
parentCommonDeclaration: NullableLazyValue<*>?
): CirFunctionNode = buildNode( ): CirFunctionNode = buildNode(
storageManager = storageManager, storageManager = storageManager,
size = size, size = size,
parentCommonDeclaration = parentCommonDeclaration,
commonizerProducer = { FunctionCommonizer(classifiers) }, commonizerProducer = { FunctionCommonizer(classifiers) },
nodeProducer = ::CirFunctionNode nodeProducer = ::CirFunctionNode
) )
@@ -74,12 +70,10 @@ internal fun buildClassNode(
storageManager: StorageManager, storageManager: StorageManager,
size: Int, size: Int,
classifiers: CirKnownClassifiers, classifiers: CirKnownClassifiers,
parentCommonDeclaration: NullableLazyValue<*>?,
classId: CirEntityId classId: CirEntityId
): CirClassNode = buildNode( ): CirClassNode = buildNode(
storageManager = storageManager, storageManager = storageManager,
size = size, size = size,
parentCommonDeclaration = parentCommonDeclaration,
commonizerProducer = { ClassCommonizer(classifiers) }, commonizerProducer = { ClassCommonizer(classifiers) },
recursionMarker = CirClassRecursionMarker, recursionMarker = CirClassRecursionMarker,
nodeProducer = { targetDeclarations, commonDeclaration -> nodeProducer = { targetDeclarations, commonDeclaration ->
@@ -93,11 +87,9 @@ internal fun buildClassConstructorNode(
storageManager: StorageManager, storageManager: StorageManager,
size: Int, size: Int,
classifiers: CirKnownClassifiers, classifiers: CirKnownClassifiers,
parentCommonDeclaration: NullableLazyValue<*>?
): CirClassConstructorNode = buildNode( ): CirClassConstructorNode = buildNode(
storageManager = storageManager, storageManager = storageManager,
size = size, size = size,
parentCommonDeclaration = parentCommonDeclaration,
commonizerProducer = { ClassConstructorCommonizer(classifiers) }, commonizerProducer = { ClassConstructorCommonizer(classifiers) },
nodeProducer = ::CirClassConstructorNode nodeProducer = ::CirClassConstructorNode
) )
@@ -122,14 +114,13 @@ internal fun buildTypeAliasNode(
private fun <T : CirDeclaration, R : CirDeclaration, N : CirNode<T, R>> buildNode( private fun <T : CirDeclaration, R : CirDeclaration, N : CirNode<T, R>> buildNode(
storageManager: StorageManager, storageManager: StorageManager,
size: Int, size: Int,
parentCommonDeclaration: NullableLazyValue<*>? = null,
commonizerProducer: () -> Commonizer<T, R>, commonizerProducer: () -> Commonizer<T, R>,
recursionMarker: R? = null, recursionMarker: R? = null,
nodeProducer: (CommonizedGroup<T>, NullableLazyValue<R>) -> N nodeProducer: (CommonizedGroup<T>, NullableLazyValue<R>) -> N
): N { ): N {
val targetDeclarations = CommonizedGroup<T>(size) val targetDeclarations = CommonizedGroup<T>(size)
val commonComputable = { commonize(parentCommonDeclaration, targetDeclarations, commonizerProducer()) } val commonComputable = { commonize(targetDeclarations, commonizerProducer()) }
val commonLazyValue = if (recursionMarker != null) val commonLazyValue = if (recursionMarker != null)
storageManager.createRecursionTolerantNullableLazyValue(commonComputable, recursionMarker) storageManager.createRecursionTolerantNullableLazyValue(commonComputable, recursionMarker)
@@ -150,17 +141,3 @@ internal fun <T : Any, R> commonize(
return commonizer.result return commonizer.result
} }
@Suppress("NOTHING_TO_INLINE")
private inline fun <T : Any, R> commonize(
parentCommonDeclaration: NullableLazyValue<*>?,
targetDeclarations: CommonizedGroup<T>,
commonizer: Commonizer<T, R>
): R? {
if (parentCommonDeclaration != null && parentCommonDeclaration.invoke() == null) {
// don't commonize declaration if it's parent failed to commonize
return null
}
return commonize(targetDeclarations, commonizer)
}
@@ -469,7 +469,6 @@ class TypeCommonizerTest : AbstractCommonizerTest<CirType, CirType>() {
storageManager = LockBasedStorageManager.NO_LOCKS, storageManager = LockBasedStorageManager.NO_LOCKS,
size = variants.size, size = variants.size,
classifiers = classifiers, classifiers = classifiers,
parentCommonDeclaration = null,
classId = type.classifierId classId = type.classifierId
) )
} }