From 75891e860b3818688906fa56c82a7fd753fc4d37 Mon Sep 17 00:00:00 2001 From: pyos Date: Mon, 7 Sep 2020 14:40:57 +0200 Subject: [PATCH] FIR2IR: split getIrPropertyOrFieldSymbol, handle locals there --- .../kotlin/fir/backend/ConversionUtils.kt | 16 +--- .../fir/backend/Fir2IrDeclarationStorage.kt | 92 +++++++++---------- .../generators/FakeOverrideGenerator.kt | 2 +- .../kotlin/fir/lazy/Fir2IrLazyClass.kt | 2 +- 4 files changed, 50 insertions(+), 62 deletions(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt index 56d8ef04fda..013351245e2 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/ConversionUtils.kt @@ -130,7 +130,7 @@ fun FirReference.toSymbol( is FirClassSymbol<*> -> classifierStorage.getIrClassSymbol(boundSymbol).owner.thisReceiver?.symbol is FirFunctionSymbol -> declarationStorage.getIrFunctionSymbol(boundSymbol).owner.extensionReceiverParameter?.symbol is FirPropertySymbol -> { - val property = declarationStorage.getIrPropertyOrFieldSymbol(boundSymbol).owner as? IrProperty + val property = declarationStorage.getIrPropertySymbol(boundSymbol).owner as? IrProperty property?.let { conversionScope.parentAccessorOfPropertyFromStack(it) }?.symbol } else -> null @@ -149,22 +149,16 @@ private fun FirCallableSymbol<*>.toSymbol(declarationStorage: Fir2IrDeclarationS } else { syntheticProperty.setter!!.delegate.symbol.toSymbol(declarationStorage, preferGetter) } - } ?: fir.toSymbol(declarationStorage) + } ?: declarationStorage.getIrPropertySymbol(this) } - is FirPropertySymbol -> fir.toSymbol(declarationStorage) - is FirFieldSymbol -> declarationStorage.getIrPropertyOrFieldSymbol(this) + is FirPropertySymbol -> declarationStorage.getIrPropertySymbol(this) + is FirFieldSymbol -> declarationStorage.getIrFieldSymbol(this) is FirBackingFieldSymbol -> declarationStorage.getIrBackingFieldSymbol(this) is FirDelegateFieldSymbol<*> -> declarationStorage.getIrBackingFieldSymbol(this) is FirVariableSymbol<*> -> declarationStorage.getIrValueSymbol(this) else -> null } -private fun FirProperty.toSymbol(declarationStorage: Fir2IrDeclarationStorage): IrSymbol? = when { - !isLocal -> declarationStorage.getIrPropertyOrFieldSymbol(symbol) - delegate != null -> declarationStorage.getIrLocalDelegatedPropertySymbol(symbol) - else -> declarationStorage.getIrValueSymbol(symbol) -} - fun FirConstExpression<*>.getIrConstKind(): IrConstKind<*> = when (kind) { FirConstKind.IntegerLiteral -> { val type = typeRef.coneTypeUnsafe() @@ -327,7 +321,7 @@ internal fun FirProperty.generateOverriddenAccessorSymbols( if (it.fir.visibility == Visibilities.Private) { return@processDirectlyOverriddenProperties ProcessorAction.NEXT } - val overriddenProperty = declarationStorage.getIrPropertyOrFieldSymbol(it.unwrapSubstitutionOverrides()) as IrPropertySymbol + val overriddenProperty = declarationStorage.getIrPropertySymbol(it.unwrapSubstitutionOverrides()) as IrPropertySymbol val overriddenAccessor = if (isGetter) overriddenProperty.owner.getter?.symbol else overriddenProperty.owner.setter?.symbol if (overriddenAccessor != null) { overriddenSet += overriddenAccessor 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 1a11578adfa..1e508454762 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 @@ -9,8 +9,6 @@ import org.jetbrains.kotlin.KtNodeTypes import org.jetbrains.kotlin.builtins.StandardNames.BUILT_INS_PACKAGE_FQ_NAMES import org.jetbrains.kotlin.descriptors.Modality import org.jetbrains.kotlin.descriptors.PropertyDescriptor -import org.jetbrains.kotlin.descriptors.SourceElement -import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.fir.FirAnnotationContainer import org.jetbrains.kotlin.descriptors.Visibilities import org.jetbrains.kotlin.descriptors.Visibility @@ -1012,53 +1010,54 @@ class Fir2IrDeclarationStorage( } } - fun getIrPropertyOrFieldSymbol(firVariableSymbol: FirVariableSymbol<*>): IrSymbol { - return when (val fir = firVariableSymbol.fir) { - is FirProperty -> { - propertyCache[fir]?.let { return it.symbol } - val signature = signatureComposer.composeSignature(fir) - val irParent = findIrParent(fir) - val parentOrigin = (irParent as? IrDeclaration)?.origin ?: IrDeclarationOrigin.DEFINED - if (signature != null) { - symbolTable.referencePropertyIfAny(signature)?.let { irPropertySymbol -> - val irProperty = irPropertySymbol.owner - propertyCache[fir] = irProperty - return irPropertySymbol - } - // TODO: package fragment members (?) - if (irParent is Fir2IrLazyClass) { - assert(parentOrigin != IrDeclarationOrigin.DEFINED) { - "Should not have reference to public API uncached property from source code" + fun getIrPropertySymbol(firPropertySymbol: FirPropertySymbol): IrSymbol { + val fir = firPropertySymbol.fir + if (fir.isLocal) { + return localStorage.getDelegatedProperty(fir)?.symbol ?: getIrVariableSymbol(fir) + } + propertyCache[fir]?.let { return it.symbol } + val signature = signatureComposer.composeSignature(fir) + val irParent = findIrParent(fir) + val parentOrigin = (irParent as? IrDeclaration)?.origin ?: IrDeclarationOrigin.DEFINED + if (signature != null) { + symbolTable.referencePropertyIfAny(signature)?.let { irPropertySymbol -> + val irProperty = irPropertySymbol.owner + propertyCache[fir] = irProperty + return irPropertySymbol + } + // TODO: package fragment members (?) + if (irParent is Fir2IrLazyClass) { + assert(parentOrigin != IrDeclarationOrigin.DEFINED) { + "Should not have reference to public API uncached property from source code" + } + val symbol = Fir2IrPropertySymbol(signature, fir.containerSource) + val irProperty = fir.convertWithOffsets { startOffset, endOffset -> + symbolTable.declareProperty(signature, { symbol }) { + val isFakeOverride = + firPropertySymbol.isFakeOverride && + firPropertySymbol.callableId != firPropertySymbol.overriddenSymbol?.callableId + Fir2IrLazyProperty( + components, startOffset, endOffset, parentOrigin, fir, irParent.fir, symbol, isFakeOverride + ).apply { + parent = irParent } - val symbol = Fir2IrPropertySymbol(signature, fir.containerSource) - val irProperty = fir.convertWithOffsets { startOffset, endOffset -> - symbolTable.declareProperty(signature, { symbol }) { - val isFakeOverride = - firVariableSymbol is FirPropertySymbol && firVariableSymbol.isFakeOverride && - firVariableSymbol.callableId != firVariableSymbol.overriddenSymbol?.callableId - Fir2IrLazyProperty( - components, startOffset, endOffset, parentOrigin, fir, irParent.fir, symbol, isFakeOverride - ).apply { - parent = irParent - } - } - } - propertyCache[fir] = irProperty - return symbol } } - createIrProperty(fir, irParent, origin = parentOrigin).apply { - setAndModifyParent(irParent) - }.symbol + propertyCache[fir] = irProperty + return symbol } - is FirField -> { - fieldCache[fir]?.let { return it.symbol } - createIrField(fir).apply { - setAndModifyParent(findIrParent(fir)) - }.symbol - } - else -> throw IllegalArgumentException("Unexpected fir in property symbol: ${fir.render()}") } + return createIrProperty(fir, irParent, origin = parentOrigin).apply { + setAndModifyParent(irParent) + }.symbol + } + + fun getIrFieldSymbol(firFieldSymbol: FirFieldSymbol): IrSymbol { + val fir = firFieldSymbol.fir + val irProperty = fieldCache[fir] ?: createIrField(fir).apply { + setAndModifyParent(findIrParent(fir)) + } + return irProperty.symbol } fun getIrBackingFieldSymbol(firVariableSymbol: FirVariableSymbol<*>): IrSymbol { @@ -1085,11 +1084,6 @@ class Fir2IrDeclarationStorage( ?: throw IllegalArgumentException("Cannot find variable ${firVariable.render()} in local storage") } - fun getIrLocalDelegatedPropertySymbol(firPropertySymbol: FirPropertySymbol): IrSymbol { - return localStorage.getDelegatedProperty(firPropertySymbol.fir)?.symbol - ?: throw IllegalArgumentException("Cannot find delegated property ${firPropertySymbol.fir.render()} in local storage") - } - fun getIrValueSymbol(firVariableSymbol: FirVariableSymbol<*>): IrSymbol { return when (val firDeclaration = firVariableSymbol.fir) { is FirEnumEntry -> { diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/FakeOverrideGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/FakeOverrideGenerator.kt index 628691a2f5b..778c2798c9d 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/FakeOverrideGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/FakeOverrideGenerator.kt @@ -219,7 +219,7 @@ class FakeOverrideGenerator( isVar: Boolean, firOverriddenSymbol: FirPropertySymbol ): IrProperty { - val irSymbol = declarationStorage.getIrPropertyOrFieldSymbol(firOverriddenSymbol) as? IrPropertySymbol ?: return this + val irSymbol = declarationStorage.getIrPropertySymbol(firOverriddenSymbol) as? IrPropertySymbol ?: return this val overriddenProperty = irSymbol.owner getter?.apply { overriddenProperty.getter?.symbol?.let { overriddenSymbols = listOf(it) } diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyClass.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyClass.kt index ea356106697..ffbd698cc80 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyClass.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/lazy/Fir2IrLazyClass.kt @@ -155,7 +155,7 @@ class Fir2IrLazyClass( processedNames += declaration.name scope.processPropertiesByName(declaration.name) { if (it is FirPropertySymbol) { - result += declarationStorage.getIrPropertyOrFieldSymbol(it).owner as IrProperty + result += declarationStorage.getIrPropertySymbol(it).owner as IrProperty } } }