[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,
) : IrElementTransformerVoidWithContext(), BodyLoweringPass {
private var containerScope: ScopeWithIr? = null
private val elementsWithLocationToPatch = hashSetOf<IrGetValue>()
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 <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)
private fun irGetValueWithoutLocation(
symbol: IrValueSymbol,
origin: IrStatementOrigin? = null,
): IrGetValue {
return IrGetValueImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, symbol, origin).also {
elementsWithLocationToPatch += it
}
}
}