IR: rename and move deepCopyWithVariables

Rename it to `deepCopyWithoutPatchingParents`, move to
`org.jetbrains.kotlin.ir.util`, make it inline+reified, and extract the
common implementation with `deepCopyWithSymbols` into `deepCopyImpl`.
This commit is contained in:
Alexander Udalov
2024-03-07 14:14:40 +01:00
committed by Space Team
parent 1d38c01afc
commit d5c2aa4c0c
11 changed files with 35 additions and 36 deletions
@@ -19,7 +19,6 @@ import org.jetbrains.kotlin.ir.IrBuiltIns
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.builders.declarations.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.deepCopyWithVariables
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrDelegatingConstructorCallImpl
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
@@ -282,7 +281,7 @@ class JvmAnnotationImplementationTransformer(val jvmContext: JvmBackendContext,
fallbackPrimaryCtorParamsMap[propName]?.defaultValue?.takeIf { it.expression !is IrErrorExpression }
else -> null
}
parameter.defaultValue = newDefaultValue?.deepCopyWithVariables()
parameter.defaultValue = newDefaultValue?.deepCopyWithoutPatchingParents()
?.also { if (defaultValueTransformer != null) it.transformChildrenVoid(defaultValueTransformer) }
ctorBody.statements += with(ctorBodyBuilder) {
@@ -24,7 +24,6 @@ import org.jetbrains.kotlin.ir.builders.declarations.buildConstructor
import org.jetbrains.kotlin.ir.builders.declarations.buildField
import org.jetbrains.kotlin.ir.builders.declarations.buildFun
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.deepCopyWithVariables
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.types.IrSimpleType
@@ -64,7 +63,7 @@ fun createLeafMfvcNode(
oldBackingField.metadata = null
}.apply {
this.parent = oldBackingField.parent
this.annotations = fieldAnnotations.map { it.deepCopyWithVariables() }
this.annotations = fieldAnnotations.map { it.deepCopyWithoutPatchingParents() }
}
}
@@ -1,19 +0,0 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.ir
import org.jetbrains.kotlin.ir.util.DeepCopyIrTreeWithSymbols
import org.jetbrains.kotlin.ir.util.DeepCopySymbolRemapper
import org.jetbrains.kotlin.ir.util.NullDescriptorsRemapper
import org.jetbrains.kotlin.ir.visitors.acceptVoid
@Suppress("UNCHECKED_CAST")
fun <T : IrElement> T.deepCopyWithVariables(): T {
val symbolsRemapper = DeepCopySymbolRemapper(NullDescriptorsRemapper)
acceptVoid(symbolsRemapper)
return this.transform(DeepCopyIrTreeWithSymbols(symbolsRemapper), null) as T
}
@@ -27,10 +27,19 @@ inline fun <reified T : IrElement> T.deepCopyWithSymbols(
initialParent: IrDeclarationParent? = null,
createTypeRemapper: (SymbolRemapper) -> TypeRemapper = ::DeepCopyTypeRemapper
): T {
return (deepCopyImpl(createTypeRemapper) as T).patchDeclarationParents(initialParent)
}
inline fun <reified T : IrElement> T.deepCopyWithoutPatchingParents(): T {
return deepCopyImpl(::DeepCopyTypeRemapper) as T
}
@PublishedApi
internal inline fun <T : IrElement> T.deepCopyImpl(createTypeRemapper: (SymbolRemapper) -> TypeRemapper): IrElement {
val symbolRemapper = DeepCopySymbolRemapper()
acceptVoid(symbolRemapper)
val typeRemapper = createTypeRemapper(symbolRemapper)
return transform(DeepCopyIrTreeWithSymbols(symbolRemapper, typeRemapper), null).patchDeclarationParents(initialParent) as T
return transform(DeepCopyIrTreeWithSymbols(symbolRemapper, typeRemapper), null)
}
@OptIn(ObsoleteDescriptorBasedAPI::class)
@@ -7,7 +7,9 @@ package org.jetbrains.kotlin.ir.util
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.IrFileEntry
import org.jetbrains.kotlin.ir.declarations.IrDeclarationParent
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.visitors.acceptVoid
@Deprecated(
"Use the overload with DumpIrTreeOptions instead.",
@@ -69,3 +71,16 @@ abstract class SymbolRenamer private constructor() {
@Deprecated("Used from Compose.")
companion object DEFAULT : SymbolRenamer()
}
// This member is left for compatibility with compose.
@Deprecated("Use the other deepCopyWithSymbols instead.")
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
@kotlin.internal.LowPriorityInOverloadResolution // To fix bootstrap with K1 which cannot distinguish between the other deepCopyWithSymbols by lambda type.
inline fun <reified T : IrElement> T.deepCopyWithSymbols(
initialParent: IrDeclarationParent?,
createCopier: (SymbolRemapper, TypeRemapper) -> DeepCopyIrTreeWithSymbols,
): T {
val symbolRemapper = DeepCopySymbolRemapper()
acceptVoid(symbolRemapper)
return transform(createCopier(symbolRemapper, DeepCopyTypeRemapper(symbolRemapper)), null).patchDeclarationParents(initialParent) as T
}
@@ -9,8 +9,8 @@ import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrFile
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
import org.jetbrains.kotlin.ir.deepCopyWithVariables
import org.jetbrains.kotlin.ir.expressions.IrReturn
import org.jetbrains.kotlin.ir.util.deepCopyWithoutPatchingParents
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
import org.jetbrains.kotlin.name.Name
@@ -37,7 +37,7 @@ class BodyWithDefaultValueReplacer : IrElementVisitorVoid {
}
override fun visitReturn(expression: IrReturn) {
expression.value = defaultValue.expression.deepCopyWithVariables()
expression.value = defaultValue.expression.deepCopyWithoutPatchingParents()
}
}
declaration.body?.acceptChildrenVoid(bodyReplacer)
@@ -15,7 +15,6 @@ import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.deepCopyWithVariables
import org.jetbrains.kotlin.ir.expressions.IrClassReference
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
@@ -13,12 +13,12 @@ import org.jetbrains.kotlin.ir.builders.irGetField
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrProperty
import org.jetbrains.kotlin.ir.declarations.IrValueParameter
import org.jetbrains.kotlin.ir.deepCopyWithVariables
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
import org.jetbrains.kotlin.ir.expressions.IrGetValue
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
import org.jetbrains.kotlin.ir.util.constructors
import org.jetbrains.kotlin.ir.util.deepCopyWithoutPatchingParents
import org.jetbrains.kotlin.ir.util.properties
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
@@ -114,7 +114,7 @@ fun createInitializerAdapter(
} else {
rawExpression
}
return expression.deepCopyWithVariables().transform(initializerTransformer, null)
return expression.deepCopyWithoutPatchingParents().transform(initializerTransformer, null)
}
}
@@ -125,4 +125,4 @@ private fun extractDefaultValuesFromConstructor(irClass: IrClass?): Map<IrValueS
val defaultsMap: Map<IrValueSymbol, IrExpression?> =
original?.valueParameters?.associate { it.symbol to it.defaultValue?.expression } ?: emptyMap()
return defaultsMap + extractDefaultValuesFromConstructor(irClass.getSuperClassNotAny())
}
}
@@ -19,7 +19,6 @@ import org.jetbrains.kotlin.ir.builders.declarations.addProperty
import org.jetbrains.kotlin.ir.builders.declarations.buildField
import org.jetbrains.kotlin.ir.builders.declarations.buildFun
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.deepCopyWithVariables
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
@@ -432,7 +431,7 @@ interface IrBuilderWithPluginContext {
}
fun IrBuilderWithScope.copyAnnotationsFrom(annotations: List<IrConstructorCall>): List<IrExpression> =
annotations.filter { it.symbol.owner.parentAsClass.isSerialInfoAnnotation }.map { it.deepCopyWithVariables() }
annotations.filter { it.symbol.owner.parentAsClass.isSerialInfoAnnotation }.map { it.deepCopyWithoutPatchingParents() }
@OptIn(ObsoleteDescriptorBasedAPI::class)
fun IrBuilderWithScope.wrapperClassReference(classType: IrType): IrClassReference {
@@ -13,7 +13,6 @@ import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.ir.IrStatement
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.deepCopyWithVariables
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
import org.jetbrains.kotlin.ir.expressions.IrStatementOrigin
@@ -95,7 +94,7 @@ class SerializableIrGenerator(
}
}
it is IrAnonymousInitializer -> {
val statements = it.body.deepCopyWithVariables().statements
val statements = it.body.deepCopyWithoutPatchingParents().statements
statementsAfterSerializableProperty.getOrPutNullable(current, { mutableListOf() })
.addAll(statements)
}
@@ -14,7 +14,6 @@ import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.deepCopyWithVariables
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrExpressionBody
@@ -461,7 +460,7 @@ open class SerializerIrGenerator(
val decodeSequentiallyCall = irInvoke(localInput.get(), inputClass.functionByName(CallingConventions.decodeSequentially))
val sequentialPart = irBlock {
decoderCalls.forEach { (_, expr) -> +expr.deepCopyWithVariables() }
decoderCalls.forEach { (_, expr) -> +expr.deepCopyWithoutPatchingParents() }
}
val byIndexPart: IrExpression = irWhile().also { loop ->