From be4e81fbce0998b5274c17b6457555e9c357dac5 Mon Sep 17 00:00:00 2001 From: Wojciech Litewka Date: Mon, 4 Mar 2024 12:46:46 +0100 Subject: [PATCH] [IR] Avoid subclassing IrGetValue in FunctionInlining to avoid custom IrElement implementations. #KT-65773 In Progress --- .../kotlin/ir/inline/FunctionInlining.kt | 47 +++++++------------ 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/compiler/ir/ir.inline/src/org/jetbrains/kotlin/ir/inline/FunctionInlining.kt b/compiler/ir/ir.inline/src/org/jetbrains/kotlin/ir/inline/FunctionInlining.kt index 24da046d01f..282fa69227a 100644 --- a/compiler/ir/ir.inline/src/org/jetbrains/kotlin/ir/inline/FunctionInlining.kt +++ b/compiler/ir/ir.inline/src/org/jetbrains/kotlin/ir/inline/FunctionInlining.kt @@ -85,6 +85,7 @@ class FunctionInlining( private val inlineArgumentsWithOriginalOffset: Boolean = false, ) : IrElementTransformerVoidWithContext(), BodyLoweringPass { private var containerScope: ScopeWithIr? = null + private val elementsWithLocationToPatch = hashSetOf() override fun lower(irBody: IrBody, container: IrDeclaration) { // TODO container: IrSymbolDeclaration @@ -247,8 +248,8 @@ class FunctionInlining( argument.transformChildrenVoid(this) // Default argument can contain subjects for substitution. val ret = - if (argument is IrGetValueWithoutLocation) - argument.withLocation(newExpression.startOffset, newExpression.endOffset) + if (argument is IrGetValue && argument in elementsWithLocationToPatch) + argument.copyWithOffsets(newExpression.startOffset, newExpression.endOffset) else argument.copy() @@ -436,8 +437,8 @@ class FunctionInlining( val argument = if (parameter !in unboundArgsSet) { val arg = boundFunctionParametersMap[parameter]!! - if (arg is IrGetValueWithoutLocation) - arg.withLocation(irCall.startOffset, irCall.endOffset) + if (arg is IrGetValue && arg in elementsWithLocationToPatch) + arg.copyWithOffsets(irCall.startOffset, irCall.endOffset) else arg.copy() } else { if (unboundIndex == valueParameters.size && parameter.defaultValue != null) @@ -667,7 +668,7 @@ class FunctionInlining( // Arguments may reference the previous ones - substitute them. val irExpression = it.argumentExpression.transform(substitutor, data = null) val newArgument = if (it.isImmutableVariableLoad) { - IrGetValueWithoutLocation((irExpression as IrGetValue).symbol) + irGetValueWithoutLocation((irExpression as IrGetValue).symbol) } else { val newVariable = currentScope.scope.createTemporaryVariable( @@ -683,7 +684,7 @@ class FunctionInlining( evaluationStatements.add(newVariable) - IrGetValueWithoutLocation(newVariable.symbol) + irGetValueWithoutLocation(newVariable.symbol) } when (it.parameter) { referenced.dispatchReceiverParameter -> reference.dispatchReceiver = newArgument @@ -707,7 +708,7 @@ class FunctionInlining( isMutable = false ) - val newArgument = IrGetValueWithoutLocation(newVariable.symbol) + val newArgument = irGetValueWithoutLocation(newVariable.symbol) when { reference.dispatchReceiver != null -> reference.dispatchReceiver = newArgument reference.extensionReceiver != null -> reference.extensionReceiver = newArgument @@ -775,12 +776,12 @@ class FunctionInlining( if (shouldCreateTemporaryVariable) { val newVariable = createTemporaryVariable(parameter, variableInitializer, argument.isDefaultArg, callee) if (argument.isDefaultArg) evaluationStatementsFromDefault.add(newVariable) else evaluationStatements.add(newVariable) - substituteMap[parameter] = IrGetValueWithoutLocation(newVariable.symbol) + substituteMap[parameter] = irGetValueWithoutLocation(newVariable.symbol) return@forEach } substituteMap[parameter] = if (variableInitializer is IrGetValue) { - IrGetValueWithoutLocation(variableInitializer.symbol) + irGetValueWithoutLocation(variableInitializer.symbol) } else { variableInitializer } @@ -842,27 +843,13 @@ class FunctionInlining( } } - private class IrGetValueWithoutLocation( - override var symbol: IrValueSymbol, - override var origin: IrStatementOrigin? = null - ) : IrGetValue() { - override val startOffset: Int get() = UNDEFINED_OFFSET - override val endOffset: Int get() = UNDEFINED_OFFSET - - override var type: IrType - get() = symbol.owner.type - set(value) { - symbol.owner.type = value - } - - override var attributeOwnerId: IrAttributeContainer = this - override var originalBeforeInline: IrAttributeContainer? = null - - override fun accept(visitor: IrElementVisitor, data: D) = - visitor.visitGetValue(this, data) - - fun withLocation(startOffset: Int, endOffset: Int) = - IrGetValueImpl(startOffset, endOffset, type, symbol, origin) + private fun irGetValueWithoutLocation( + symbol: IrValueSymbol, + origin: IrStatementOrigin? = null, + ): IrGetValue { + return IrGetValueImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, symbol, origin).also { + elementsWithLocationToPatch += it + } } }