[FIR2IR] Change API of local storages to work with symbols instead of IR elements

This commit is contained in:
Dmitriy Novozhilov
2023-11-09 14:30:05 +02:00
committed by Space Team
parent bedac19b99
commit 547d6066a9
3 changed files with 61 additions and 50 deletions
@@ -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 ------------------------------------
@@ -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 <T> 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)
}
}
@@ -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<FirValueParameter, IrValueParameter>()
private val parameterCache = mutableMapOf<FirValueParameter, IrValueParameterSymbol>()
private val variableCache = mutableMapOf<FirVariable, IrVariable>()
private val variableCache = mutableMapOf<FirVariable, IrVariableSymbol>()
private val localFunctionCache = mutableMapOf<FirFunction, IrSimpleFunction>()
private val localFunctionCache = mutableMapOf<FirFunction, IrSimpleFunctionSymbol>()
private val delegatedPropertyCache = mutableMapOf<FirProperty, IrLocalDelegatedProperty>()
private val delegatedPropertyCache = mutableMapOf<FirProperty, IrLocalDelegatedPropertySymbol>()
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() {