From 1d58fd159ae877c1812bf5586c770ef8674c40be Mon Sep 17 00:00:00 2001 From: Roman Artemev Date: Mon, 25 Oct 2021 13:06:55 +0300 Subject: [PATCH] [IR] Replace adhoc remappers to common one `ValueRemapper` --- .../lower/DefaultArgumentStubGenerator.kt | 25 +++++++--------- .../backend/js/lower/SecondaryCtorLowering.kt | 29 +++++++------------ 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/DefaultArgumentStubGenerator.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/DefaultArgumentStubGenerator.kt index ffbbd6b703e..32e96cea43b 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/DefaultArgumentStubGenerator.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/DefaultArgumentStubGenerator.kt @@ -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() - val variables = mutableMapOf() + val variables = mutableMapOf() 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) { diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/SecondaryCtorLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/SecondaryCtorLowering.kt index 32eba2f10bd..eef482afe75 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/SecondaryCtorLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/SecondaryCtorLowering.kt @@ -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 - ) : IrElementTransformerVoid() { - - val newThisSymbol = symbolMapping.values.last().symbol + symbolMapping: Map + ) : 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 - } } }