From 547d6066a9d639359b40763680ca4a584f79f57e Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Thu, 9 Nov 2023 14:30:05 +0200 Subject: [PATCH] [FIR2IR] Change API of local storages to work with symbols instead of IR elements --- .../fir/backend/Fir2IrDeclarationStorage.kt | 25 +++++----- .../fir/backend/Fir2IrLocalCallableStorage.kt | 37 ++++++++------ .../kotlin/fir/backend/Fir2IrScopeCache.kt | 49 ++++++++++--------- 3 files changed, 61 insertions(+), 50 deletions(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt index 1b2033047fc..e2f71683a9d 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrDeclarationStorage.kt @@ -334,7 +334,7 @@ class Fir2IrDeclarationStorage( private fun cacheIrFunction(function: FirFunction, irFunction: IrSimpleFunction, fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag?) { when { irFunction.visibility == DescriptorVisibilities.LOCAL -> { - localStorage.putLocalFunction(function, irFunction) + localStorage.putLocalFunction(function, irFunction.symbol) } function.isFakeOverrideOrDelegated(fakeOverrideOwnerLookupTag) -> { @@ -356,7 +356,7 @@ class Fir2IrDeclarationStorage( val contextReceivers = function.contextReceiversForFunctionOrContainingProperty() for ((firParameter, irParameter) in function.valueParameters.zip(valueParameters.drop(contextReceivers.size))) { - localStorage.putParameter(firParameter, irParameter) + localStorage.putParameter(firParameter, irParameter.symbol) } return this } @@ -638,7 +638,7 @@ class Fir2IrDeclarationStorage( ): IrSymbol { val firProperty = prepareProperty(firPropertySymbol.fir) if (firProperty.isLocal) { - return localStorage.getDelegatedProperty(firProperty)?.symbol ?: getIrVariableSymbol(firProperty) + return localStorage.getDelegatedProperty(firProperty) ?: getIrVariableSymbol(firProperty) } val result = getOrCreateIrProperty( firProperty, @@ -744,7 +744,10 @@ class Fir2IrDeclarationStorage( return when (fir) { is FirProperty -> { if (fir.isLocal) { - return localStorage.getDelegatedProperty(fir)?.delegate?.symbol ?: getIrVariableSymbol(fir) + // local property cannot be referenced before declaration, so it's safe to take an owner from the symbol + @OptIn(UnsafeDuringIrConstructionAPI::class) + val delegatedProperty = localStorage.getDelegatedProperty(fir)?.owner + return delegatedProperty?.delegate?.symbol ?: getIrVariableSymbol(fir) } @OptIn(UnsafeDuringIrConstructionAPI::class) propertyCache[fir]?.ownerIfBound()?.let { return it.backingField!!.symbol } @@ -850,7 +853,7 @@ class Fir2IrDeclarationStorage( skipDefaultParameter, forcedDefaultValueConversion ).also { - localStorage.putParameter(valueParameter, it) + localStorage.putParameter(valueParameter, it.symbol) } } @@ -878,7 +881,7 @@ class Fir2IrDeclarationStorage( delegateVariableForPropertyCache[symbol] = irProperty.delegate.symbol getterForPropertyCache[symbol] = irProperty.getter.symbol irProperty.setter?.let { setterForPropertyCache[symbol] = it.symbol } - localStorage.putDelegatedProperty(property, irProperty) + localStorage.putDelegatedProperty(property, symbol) return irProperty } @@ -899,7 +902,7 @@ class Fir2IrDeclarationStorage( givenOrigin: IrDeclarationOrigin? = null ): IrVariable { return callablesGenerator.createIrVariable(variable, irParent, givenOrigin).also { - localStorage.putVariable(variable, it) + localStorage.putVariable(variable, it.symbol) } } @@ -919,7 +922,7 @@ class Fir2IrDeclarationStorage( ).symbol } is FirValueParameter -> { - localStorage.getParameter(firDeclaration)?.symbol + localStorage.getParameter(firDeclaration) // catch parameter is FirValueParameter in FIR but IrVariable in IR ?: return getIrVariableSymbol(firDeclaration) } @@ -930,10 +933,8 @@ class Fir2IrDeclarationStorage( } private fun getIrVariableSymbol(firVariable: FirVariable): IrVariableSymbol { - return localStorage.getVariable(firVariable)?.symbol - ?: run { - throw IllegalArgumentException("Cannot find variable ${firVariable.render()} in local storage") - } + return localStorage.getVariable(firVariable) + ?: error("Cannot find variable ${firVariable.render()} in local storage") } // ------------------------------------ anonymous initializers ------------------------------------ diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrLocalCallableStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrLocalCallableStorage.kt index 54cb3deff0d..54dc5ba17aa 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrLocalCallableStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrLocalCallableStorage.kt @@ -6,8 +6,10 @@ package org.jetbrains.kotlin.fir.backend import org.jetbrains.kotlin.fir.declarations.* -import org.jetbrains.kotlin.ir.declarations.* +import org.jetbrains.kotlin.ir.symbols.IrLocalDelegatedPropertySymbol import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol +import org.jetbrains.kotlin.ir.symbols.IrValueParameterSymbol +import org.jetbrains.kotlin.ir.symbols.IrVariableSymbol class Fir2IrLocalCallableStorage { @@ -22,7 +24,7 @@ class Fir2IrLocalCallableStorage { cacheStack.removeAt(cacheStack.size - 1) } - fun getParameter(parameter: FirValueParameter): IrValueParameter? { + fun getParameter(parameter: FirValueParameter): IrValueParameterSymbol? { for (cache in cacheStack.asReversed()) { val local = cache.getParameter(parameter) if (local != null) return local @@ -30,14 +32,17 @@ class Fir2IrLocalCallableStorage { return null } - fun getVariable(variable: FirVariable): IrVariable? = - last { getVariable(variable) } + fun getVariable(variable: FirVariable): IrVariableSymbol? { + return last { getVariable(variable) } + } - fun getLocalFunctionSymbol(localFunction: FirFunction): IrSimpleFunctionSymbol? = - last { getLocalFunction(localFunction)?.symbol } + fun getLocalFunctionSymbol(localFunction: FirFunction): IrSimpleFunctionSymbol? { + return last { getLocalFunction(localFunction) } + } - fun getDelegatedProperty(property: FirProperty): IrLocalDelegatedProperty? = - last { getDelegatedProperty(property) } + fun getDelegatedProperty(property: FirProperty): IrLocalDelegatedPropertySymbol? { + return last { getDelegatedProperty(property) } + } private inline fun last(getter: Fir2IrScopeCache.() -> T?): T? { for (cache in cacheStack.asReversed()) { @@ -46,19 +51,19 @@ class Fir2IrLocalCallableStorage { return null } - fun putParameter(firParameter: FirValueParameter, irParameter: IrValueParameter) { - cacheStack.last().putParameter(firParameter, irParameter) + fun putParameter(firParameter: FirValueParameter, irParameterSymbol: IrValueParameterSymbol) { + cacheStack.last().putParameter(firParameter, irParameterSymbol) } - fun putVariable(firVariable: FirVariable, irVariable: IrVariable) { - cacheStack.last().putVariable(firVariable, irVariable) + fun putVariable(firVariable: FirVariable, irVariableSymbol: IrVariableSymbol) { + cacheStack.last().putVariable(firVariable, irVariableSymbol) } - fun putLocalFunction(firFunction: FirFunction, irFunction: IrSimpleFunction) { - cacheStack.last().putLocalFunction(firFunction, irFunction) + fun putLocalFunction(firFunction: FirFunction, irFunctionSymbol: IrSimpleFunctionSymbol) { + cacheStack.last().putLocalFunction(firFunction, irFunctionSymbol) } - fun putDelegatedProperty(firProperty: FirProperty, irProperty: IrLocalDelegatedProperty) { - cacheStack.last().putDelegatedProperty(firProperty, irProperty) + fun putDelegatedProperty(firProperty: FirProperty, irPropertySymbol: IrLocalDelegatedPropertySymbol) { + cacheStack.last().putDelegatedProperty(firProperty, irPropertySymbol) } } diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrScopeCache.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrScopeCache.kt index 43b33317664..e9c1523bcd5 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrScopeCache.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrScopeCache.kt @@ -8,43 +8,48 @@ package org.jetbrains.kotlin.fir.backend import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.declarations.utils.visibility -import org.jetbrains.kotlin.ir.declarations.IrLocalDelegatedProperty -import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction -import org.jetbrains.kotlin.ir.declarations.IrValueParameter -import org.jetbrains.kotlin.ir.declarations.IrVariable +import org.jetbrains.kotlin.ir.symbols.* class Fir2IrScopeCache { - private val parameterCache = mutableMapOf() + private val parameterCache = mutableMapOf() - private val variableCache = mutableMapOf() + private val variableCache = mutableMapOf() - private val localFunctionCache = mutableMapOf() + private val localFunctionCache = mutableMapOf() - private val delegatedPropertyCache = mutableMapOf() + private val delegatedPropertyCache = mutableMapOf() - fun getParameter(parameter: FirValueParameter): IrValueParameter? = parameterCache[parameter] - - fun putParameter(firParameter: FirValueParameter, irParameter: IrValueParameter) { - parameterCache[firParameter] = irParameter + fun getParameter(parameter: FirValueParameter): IrValueParameterSymbol? { + return parameterCache[parameter] } - fun getVariable(variable: FirVariable): IrVariable? = variableCache[variable] - - fun putVariable(firVariable: FirVariable, irVariable: IrVariable) { - variableCache[firVariable] = irVariable + fun putParameter(firParameter: FirValueParameter, irParameterSymbol: IrValueParameterSymbol) { + parameterCache[firParameter] = irParameterSymbol } - fun getLocalFunction(localFunction: FirFunction): IrSimpleFunction? = localFunctionCache[localFunction] + fun getVariable(variable: FirVariable): IrVariableSymbol? { + return variableCache[variable] + } - fun putLocalFunction(localFunction: FirFunction, irFunction: IrSimpleFunction) { + fun putVariable(firVariable: FirVariable, irVariableSymbol: IrVariableSymbol) { + variableCache[firVariable] = irVariableSymbol + } + + fun getLocalFunction(localFunction: FirFunction): IrSimpleFunctionSymbol? { + return localFunctionCache[localFunction] + } + + fun putLocalFunction(localFunction: FirFunction, irFunctionSymbol: IrSimpleFunctionSymbol) { require(localFunction !is FirSimpleFunction || localFunction.visibility == Visibilities.Local) - localFunctionCache[localFunction] = irFunction + localFunctionCache[localFunction] = irFunctionSymbol } - fun getDelegatedProperty(property: FirProperty): IrLocalDelegatedProperty? = delegatedPropertyCache[property] + fun getDelegatedProperty(property: FirProperty): IrLocalDelegatedPropertySymbol? { + return delegatedPropertyCache[property] + } - fun putDelegatedProperty(firProperty: FirProperty, irProperty: IrLocalDelegatedProperty) { - delegatedPropertyCache[firProperty] = irProperty + fun putDelegatedProperty(firProperty: FirProperty, irPropertySymbol: IrLocalDelegatedPropertySymbol) { + delegatedPropertyCache[firProperty] = irPropertySymbol } fun clear() {