[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 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.descriptors.synthesizedString
import org.jetbrains.kotlin.backend.common.ir.* import org.jetbrains.kotlin.backend.common.ir.*
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities 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.IrConstructorSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol 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.IrType
import org.jetbrains.kotlin.ir.types.defaultType import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.types.isNullable import org.jetbrains.kotlin.ir.types.isNullable
@@ -73,14 +76,14 @@ open class DefaultArgumentStubGenerator(
newIrFunction.body = context.irFactory.createBlockBody(UNDEFINED_OFFSET, UNDEFINED_OFFSET) { newIrFunction.body = context.irFactory.createBlockBody(UNDEFINED_OFFSET, UNDEFINED_OFFSET) {
statements += builder.irBlockBody(newIrFunction) { statements += builder.irBlockBody(newIrFunction) {
val params = mutableListOf<IrValueDeclaration>() val params = mutableListOf<IrValueDeclaration>()
val variables = mutableMapOf<IrValueDeclaration, IrValueDeclaration>() val variables = mutableMapOf<IrValueSymbol, IrValueSymbol>()
irFunction.dispatchReceiverParameter?.let { irFunction.dispatchReceiverParameter?.let {
variables[it] = newIrFunction.dispatchReceiverParameter!! variables[it.symbol] = newIrFunction.dispatchReceiverParameter?.symbol!!
} }
irFunction.extensionReceiverParameter?.let { irFunction.extensionReceiverParameter?.let {
variables[it] = newIrFunction.extensionReceiverParameter!! variables[it.symbol] = newIrFunction.extensionReceiverParameter?.symbol!!
} }
// In order to deal with forward references in default value lambdas, // 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 // works correctly so that `f() { "OK" }` returns "OK" and
// `f()` throws a NullPointerException. // `f()` throws a NullPointerException.
irFunction.valueParameters.associateWithTo(variables) { irFunction.valueParameters.forEach {
newIrFunction.valueParameters[it.index] variables[it.symbol] = newIrFunction.valueParameters[it.index].symbol
} }
generateSuperCallHandlerCheckIfNeeded(irFunction, newIrFunction) generateSuperCallHandlerCheckIfNeeded(irFunction, newIrFunction)
@@ -116,19 +119,13 @@ open class DefaultArgumentStubGenerator(
val expression = defaultValue.expression val expression = defaultValue.expression
.prepareToBeUsedIn(newIrFunction) .prepareToBeUsedIn(newIrFunction)
.transform(object : IrElementTransformerVoid() { .transform(ValueRemapper(variables), null)
override fun visitGetValue(expression: IrGetValue): IrExpression {
log { "GetValue: ${expression.symbol.owner}" }
val valueSymbol = variables[expression.symbol.owner] ?: return expression
return irGet(valueSymbol)
}
}, null)
selectArgumentOrDefault(defaultFlag, parameter, expression) selectArgumentOrDefault(defaultFlag, parameter, expression)
} ?: parameter } ?: parameter
params.add(remapped) params.add(remapped)
variables[valueParameter] = remapped variables[valueParameter.symbol] = remapped.symbol
} }
when (irFunction) { 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.BodyLoweringPass
import org.jetbrains.kotlin.backend.common.DeclarationTransformer import org.jetbrains.kotlin.backend.common.DeclarationTransformer
import org.jetbrains.kotlin.backend.common.getOrPut 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.copyTo
import org.jetbrains.kotlin.backend.common.ir.copyTypeParametersFrom import org.jetbrains.kotlin.backend.common.ir.copyTypeParametersFrom
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities 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.builders.declarations.buildFun
import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.* 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.IrConstructorSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol 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.types.impl.IrSimpleTypeImpl
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.IrElementTransformer 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.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
@@ -140,21 +144,19 @@ class SecondaryConstructorLowering(val context: JsIrBackendContext) : Declaratio
ThisUsageReplaceTransformer( ThisUsageReplaceTransformer(
constructor.symbol, constructor.symbol,
delegate.symbol, delegate.symbol,
oldValueParameters.zip(delegate.valueParameters).toMap() oldValueParameters.zip(delegate.valueParameters).associate { (old, new) -> old.symbol to new.symbol }
) )
) )
} }
} }
} }
private class ThisUsageReplaceTransformer( private class ThisUsageReplaceTransformer(
val constructor: IrConstructorSymbol, val constructor: IrConstructorSymbol,
val function: IrFunctionSymbol, val function: IrFunctionSymbol,
val symbolMapping: Map<IrValueParameter, IrValueParameter> symbolMapping: Map<IrValueSymbol, IrValueSymbol>
) : IrElementTransformerVoid() { ) : ValueRemapper(symbolMapping) {
private val newThisSymbol = symbolMapping.values.last()
val newThisSymbol = symbolMapping.values.last().symbol
override fun visitReturn(expression: IrReturn): IrExpression = override fun visitReturn(expression: IrReturn): IrExpression =
if (expression.returnTargetSymbol != constructor) if (expression.returnTargetSymbol != constructor)
@@ -167,17 +169,6 @@ class SecondaryConstructorLowering(val context: JsIrBackendContext) : Declaratio
function, function,
IrGetValueImpl(expression.startOffset, expression.endOffset, newThisSymbol.owner.type, newThisSymbol) 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
}
} }
} }