[IR] Replace adhoc remappers to common one ValueRemapper

This commit is contained in:
Roman Artemev
2021-10-25 13:06:55 +03:00
committed by Space
parent 9490f9da43
commit 1d58fd159a
2 changed files with 21 additions and 33 deletions
@@ -5,7 +5,9 @@
package org.jetbrains.kotlin.backend.common.lower
import org.jetbrains.kotlin.backend.common.*
import org.jetbrains.kotlin.backend.common.BodyLoweringPass
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.DeclarationTransformer
import org.jetbrains.kotlin.backend.common.descriptors.synthesizedString
import org.jetbrains.kotlin.backend.common.ir.*
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
@@ -22,6 +24,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.types.isNullable
@@ -73,14 +76,14 @@ open class DefaultArgumentStubGenerator(
newIrFunction.body = context.irFactory.createBlockBody(UNDEFINED_OFFSET, UNDEFINED_OFFSET) {
statements += builder.irBlockBody(newIrFunction) {
val params = mutableListOf<IrValueDeclaration>()
val variables = mutableMapOf<IrValueDeclaration, IrValueDeclaration>()
val variables = mutableMapOf<IrValueSymbol, IrValueSymbol>()
irFunction.dispatchReceiverParameter?.let {
variables[it] = newIrFunction.dispatchReceiverParameter!!
variables[it.symbol] = newIrFunction.dispatchReceiverParameter?.symbol!!
}
irFunction.extensionReceiverParameter?.let {
variables[it] = newIrFunction.extensionReceiverParameter!!
variables[it.symbol] = newIrFunction.extensionReceiverParameter?.symbol!!
}
// In order to deal with forward references in default value lambdas,
@@ -93,8 +96,8 @@ open class DefaultArgumentStubGenerator(
//
// works correctly so that `f() { "OK" }` returns "OK" and
// `f()` throws a NullPointerException.
irFunction.valueParameters.associateWithTo(variables) {
newIrFunction.valueParameters[it.index]
irFunction.valueParameters.forEach {
variables[it.symbol] = newIrFunction.valueParameters[it.index].symbol
}
generateSuperCallHandlerCheckIfNeeded(irFunction, newIrFunction)
@@ -116,19 +119,13 @@ open class DefaultArgumentStubGenerator(
val expression = defaultValue.expression
.prepareToBeUsedIn(newIrFunction)
.transform(object : IrElementTransformerVoid() {
override fun visitGetValue(expression: IrGetValue): IrExpression {
log { "GetValue: ${expression.symbol.owner}" }
val valueSymbol = variables[expression.symbol.owner] ?: return expression
return irGet(valueSymbol)
}
}, null)
.transform(ValueRemapper(variables), null)
selectArgumentOrDefault(defaultFlag, parameter, expression)
} ?: parameter
params.add(remapped)
variables[valueParameter] = remapped
variables[valueParameter.symbol] = remapped.symbol
}
when (irFunction) {
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.ir.backend.js.lower
import org.jetbrains.kotlin.backend.common.BodyLoweringPass
import org.jetbrains.kotlin.backend.common.DeclarationTransformer
import org.jetbrains.kotlin.backend.common.getOrPut
import org.jetbrains.kotlin.backend.common.ir.ValueRemapper
import org.jetbrains.kotlin.backend.common.ir.copyTo
import org.jetbrains.kotlin.backend.common.ir.copyTypeParametersFrom
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
@@ -20,14 +21,17 @@ import org.jetbrains.kotlin.ir.backend.js.ir.JsIrBuilder
import org.jetbrains.kotlin.ir.builders.declarations.buildFun
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrRawFunctionReferenceImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrReturnImpl
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrValueSymbol
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.name.Name
@@ -140,21 +144,19 @@ class SecondaryConstructorLowering(val context: JsIrBackendContext) : Declaratio
ThisUsageReplaceTransformer(
constructor.symbol,
delegate.symbol,
oldValueParameters.zip(delegate.valueParameters).toMap()
oldValueParameters.zip(delegate.valueParameters).associate { (old, new) -> old.symbol to new.symbol }
)
)
}
}
}
private class ThisUsageReplaceTransformer(
val constructor: IrConstructorSymbol,
val function: IrFunctionSymbol,
val symbolMapping: Map<IrValueParameter, IrValueParameter>
) : IrElementTransformerVoid() {
val newThisSymbol = symbolMapping.values.last().symbol
symbolMapping: Map<IrValueSymbol, IrValueSymbol>
) : ValueRemapper(symbolMapping) {
private val newThisSymbol = symbolMapping.values.last()
override fun visitReturn(expression: IrReturn): IrExpression =
if (expression.returnTargetSymbol != constructor)
@@ -167,17 +169,6 @@ class SecondaryConstructorLowering(val context: JsIrBackendContext) : Declaratio
function,
IrGetValueImpl(expression.startOffset, expression.endOffset, newThisSymbol.owner.type, newThisSymbol)
)
override fun visitGetValue(expression: IrGetValue) = symbolMapping[expression.symbol.owner]?.let {
expression.run { IrGetValueImpl(startOffset, endOffset, type, it.symbol, origin) }
} ?: expression
override fun visitSetValue(expression: IrSetValue): IrExpression {
expression.transformChildrenVoid()
return symbolMapping[expression.symbol.owner]?.let {
expression.run { IrSetValueImpl(startOffset, endOffset, type, it.symbol, expression.value, origin) }
} ?: expression
}
}
}