[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")
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 ->
constructor.accept(this, Unit)
@@ -81,22 +81,21 @@ internal class CommonizationVisitor(
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 has been successfully commonized
commonClass.companion = companionObjectName
}
// 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 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) {
@@ -19,7 +19,7 @@ internal object ClassConstructorMerger {
) = with(context) {
val approximationKey = ConstructorApproximationKey(constructor, context.typeResolver)
val constructorNode: CirClassConstructorNode = classNode.constructors.getOrPut(approximationKey) {
buildClassConstructorNode(storageManager, targets, classifiers, classNode.commonDeclaration)
buildClassConstructorNode(storageManager, targets, classifiers)
}
constructorNode.targetDeclarations[context.targetIndex] = CirDeserializers.constructor(
source = constructor,
@@ -26,9 +26,8 @@ internal class ClassMerger(
val classId = classEntry.classId
val className = classId.relativeNameSegments.last()
val maybeClassOwnerNode: CirClassNode? = ownerNode as? CirClassNode
val classNode: CirClassNode = ownerNode.classes.getOrPut(className) {
buildClassNode(storageManager, targets, classifiers, maybeClassOwnerNode?.commonDeclaration, classId)
buildClassNode(storageManager, targets, classifiers, classId)
}
val clazz: KmClass?
@@ -27,16 +27,14 @@ internal object FunctionMerger {
return
}
val maybeClassOwnerNode: CirClassNode? = ownerNode as? CirClassNode
val approximationKey = FunctionApproximationKey(function, context.typeResolver)
val functionNode: CirFunctionNode = ownerNode.functions.getOrPut(approximationKey) {
buildFunctionNode(storageManager, targets, classifiers, maybeClassOwnerNode?.commonDeclaration)
buildFunctionNode(storageManager, targets, classifiers)
}
functionNode.targetDeclarations[context.targetIndex] = CirDeserializers.function(
name = approximationKey.name,
source = function,
containingClass = maybeClassOwnerNode?.targetDeclarations?.get(context.targetIndex),
containingClass = ownerNode.run { this as? CirClassNode }?.targetDeclarations?.get(context.targetIndex),
typeResolver = context.typeResolver
)
}
@@ -21,16 +21,14 @@ internal object PropertyMerger {
if (property.isFakeOverride())
return
val maybeClassOwnerNode: CirClassNode? = ownerNode as? CirClassNode
val approximationKey = PropertyApproximationKey(property, context.typeResolver)
val propertyNode: CirPropertyNode = ownerNode.properties.getOrPut(approximationKey) {
buildPropertyNode(storageManager, targets, classifiers, maybeClassOwnerNode?.commonDeclaration)
buildPropertyNode(storageManager, targets, classifiers)
}
propertyNode.targetDeclarations[context.targetIndex] = CirDeserializers.property(
name = approximationKey.name,
source = property,
containingClass = maybeClassOwnerNode?.targetDeclarations?.get(context.targetIndex),
containingClass = ownerNode.run { this as? CirClassNode }?.targetDeclarations?.get(context.targetIndex),
typeResolver = context.typeResolver
)
}
@@ -48,11 +48,9 @@ internal fun buildPropertyNode(
storageManager: StorageManager,
size: Int,
classifiers: CirKnownClassifiers,
parentCommonDeclaration: NullableLazyValue<*>?
): CirPropertyNode = buildNode(
storageManager = storageManager,
size = size,
parentCommonDeclaration = parentCommonDeclaration,
commonizerProducer = { PropertyCommonizer(classifiers) },
nodeProducer = ::CirPropertyNode
)
@@ -61,11 +59,9 @@ internal fun buildFunctionNode(
storageManager: StorageManager,
size: Int,
classifiers: CirKnownClassifiers,
parentCommonDeclaration: NullableLazyValue<*>?
): CirFunctionNode = buildNode(
storageManager = storageManager,
size = size,
parentCommonDeclaration = parentCommonDeclaration,
commonizerProducer = { FunctionCommonizer(classifiers) },
nodeProducer = ::CirFunctionNode
)
@@ -74,12 +70,10 @@ internal fun buildClassNode(
storageManager: StorageManager,
size: Int,
classifiers: CirKnownClassifiers,
parentCommonDeclaration: NullableLazyValue<*>?,
classId: CirEntityId
): CirClassNode = buildNode(
storageManager = storageManager,
size = size,
parentCommonDeclaration = parentCommonDeclaration,
commonizerProducer = { ClassCommonizer(classifiers) },
recursionMarker = CirClassRecursionMarker,
nodeProducer = { targetDeclarations, commonDeclaration ->
@@ -93,11 +87,9 @@ internal fun buildClassConstructorNode(
storageManager: StorageManager,
size: Int,
classifiers: CirKnownClassifiers,
parentCommonDeclaration: NullableLazyValue<*>?
): CirClassConstructorNode = buildNode(
storageManager = storageManager,
size = size,
parentCommonDeclaration = parentCommonDeclaration,
commonizerProducer = { ClassConstructorCommonizer(classifiers) },
nodeProducer = ::CirClassConstructorNode
)
@@ -122,14 +114,13 @@ internal fun buildTypeAliasNode(
private fun <T : CirDeclaration, R : CirDeclaration, N : CirNode<T, R>> buildNode(
storageManager: StorageManager,
size: Int,
parentCommonDeclaration: NullableLazyValue<*>? = null,
commonizerProducer: () -> Commonizer<T, R>,
recursionMarker: R? = null,
nodeProducer: (CommonizedGroup<T>, NullableLazyValue<R>) -> N
): N {
val targetDeclarations = CommonizedGroup<T>(size)
val commonComputable = { commonize(parentCommonDeclaration, targetDeclarations, commonizerProducer()) }
val commonComputable = { commonize(targetDeclarations, commonizerProducer()) }
val commonLazyValue = if (recursionMarker != null)
storageManager.createRecursionTolerantNullableLazyValue(commonComputable, recursionMarker)
@@ -150,17 +141,3 @@ internal fun <T : Any, R> commonize(
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,
size = variants.size,
classifiers = classifiers,
parentCommonDeclaration = null,
classId = type.classifierId
)
}