[IR] Avoid subclassing IrGetValue in FunctionInlining

to avoid custom IrElement implementations.

#KT-65773 In Progress
This commit is contained in:
Wojciech Litewka
2024-03-04 12:46:46 +01:00
committed by Space Team
parent 747b69c6c0
commit be4e81fbce
@@ -85,6 +85,7 @@ class FunctionInlining(
private val inlineArgumentsWithOriginalOffset: Boolean = false, private val inlineArgumentsWithOriginalOffset: Boolean = false,
) : IrElementTransformerVoidWithContext(), BodyLoweringPass { ) : IrElementTransformerVoidWithContext(), BodyLoweringPass {
private var containerScope: ScopeWithIr? = null private var containerScope: ScopeWithIr? = null
private val elementsWithLocationToPatch = hashSetOf<IrGetValue>()
override fun lower(irBody: IrBody, container: IrDeclaration) { override fun lower(irBody: IrBody, container: IrDeclaration) {
// TODO container: IrSymbolDeclaration // TODO container: IrSymbolDeclaration
@@ -247,8 +248,8 @@ class FunctionInlining(
argument.transformChildrenVoid(this) // Default argument can contain subjects for substitution. argument.transformChildrenVoid(this) // Default argument can contain subjects for substitution.
val ret = val ret =
if (argument is IrGetValueWithoutLocation) if (argument is IrGetValue && argument in elementsWithLocationToPatch)
argument.withLocation(newExpression.startOffset, newExpression.endOffset) argument.copyWithOffsets(newExpression.startOffset, newExpression.endOffset)
else else
argument.copy() argument.copy()
@@ -436,8 +437,8 @@ class FunctionInlining(
val argument = val argument =
if (parameter !in unboundArgsSet) { if (parameter !in unboundArgsSet) {
val arg = boundFunctionParametersMap[parameter]!! val arg = boundFunctionParametersMap[parameter]!!
if (arg is IrGetValueWithoutLocation) if (arg is IrGetValue && arg in elementsWithLocationToPatch)
arg.withLocation(irCall.startOffset, irCall.endOffset) arg.copyWithOffsets(irCall.startOffset, irCall.endOffset)
else arg.copy() else arg.copy()
} else { } else {
if (unboundIndex == valueParameters.size && parameter.defaultValue != null) if (unboundIndex == valueParameters.size && parameter.defaultValue != null)
@@ -667,7 +668,7 @@ class FunctionInlining(
// Arguments may reference the previous ones - substitute them. // Arguments may reference the previous ones - substitute them.
val irExpression = it.argumentExpression.transform(substitutor, data = null) val irExpression = it.argumentExpression.transform(substitutor, data = null)
val newArgument = if (it.isImmutableVariableLoad) { val newArgument = if (it.isImmutableVariableLoad) {
IrGetValueWithoutLocation((irExpression as IrGetValue).symbol) irGetValueWithoutLocation((irExpression as IrGetValue).symbol)
} else { } else {
val newVariable = val newVariable =
currentScope.scope.createTemporaryVariable( currentScope.scope.createTemporaryVariable(
@@ -683,7 +684,7 @@ class FunctionInlining(
evaluationStatements.add(newVariable) evaluationStatements.add(newVariable)
IrGetValueWithoutLocation(newVariable.symbol) irGetValueWithoutLocation(newVariable.symbol)
} }
when (it.parameter) { when (it.parameter) {
referenced.dispatchReceiverParameter -> reference.dispatchReceiver = newArgument referenced.dispatchReceiverParameter -> reference.dispatchReceiver = newArgument
@@ -707,7 +708,7 @@ class FunctionInlining(
isMutable = false isMutable = false
) )
val newArgument = IrGetValueWithoutLocation(newVariable.symbol) val newArgument = irGetValueWithoutLocation(newVariable.symbol)
when { when {
reference.dispatchReceiver != null -> reference.dispatchReceiver = newArgument reference.dispatchReceiver != null -> reference.dispatchReceiver = newArgument
reference.extensionReceiver != null -> reference.extensionReceiver = newArgument reference.extensionReceiver != null -> reference.extensionReceiver = newArgument
@@ -775,12 +776,12 @@ class FunctionInlining(
if (shouldCreateTemporaryVariable) { if (shouldCreateTemporaryVariable) {
val newVariable = createTemporaryVariable(parameter, variableInitializer, argument.isDefaultArg, callee) val newVariable = createTemporaryVariable(parameter, variableInitializer, argument.isDefaultArg, callee)
if (argument.isDefaultArg) evaluationStatementsFromDefault.add(newVariable) else evaluationStatements.add(newVariable) if (argument.isDefaultArg) evaluationStatementsFromDefault.add(newVariable) else evaluationStatements.add(newVariable)
substituteMap[parameter] = IrGetValueWithoutLocation(newVariable.symbol) substituteMap[parameter] = irGetValueWithoutLocation(newVariable.symbol)
return@forEach return@forEach
} }
substituteMap[parameter] = if (variableInitializer is IrGetValue) { substituteMap[parameter] = if (variableInitializer is IrGetValue) {
IrGetValueWithoutLocation(variableInitializer.symbol) irGetValueWithoutLocation(variableInitializer.symbol)
} else { } else {
variableInitializer variableInitializer
} }
@@ -842,27 +843,13 @@ class FunctionInlining(
} }
} }
private class IrGetValueWithoutLocation( private fun irGetValueWithoutLocation(
override var symbol: IrValueSymbol, symbol: IrValueSymbol,
override var origin: IrStatementOrigin? = null origin: IrStatementOrigin? = null,
) : IrGetValue() { ): IrGetValue {
override val startOffset: Int get() = UNDEFINED_OFFSET return IrGetValueImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, symbol, origin).also {
override val endOffset: Int get() = UNDEFINED_OFFSET elementsWithLocationToPatch += it
}
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 <R, D> accept(visitor: IrElementVisitor<R, D>, data: D) =
visitor.visitGetValue(this, data)
fun withLocation(startOffset: Int, endOffset: Int) =
IrGetValueImpl(startOffset, endOffset, type, symbol, origin)
} }
} }