From b3efad5428e4ee602a2b78df6ff5591bccd7f028 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Mon, 28 Aug 2023 14:24:51 +0300 Subject: [PATCH] [FIR2IR] Extract logic of IR declarations generation into separate component. Part 6 Move caching of created IrProperty back to Fir2IrDeclarationStorage --- .../fir/backend/Fir2IrDeclarationStorage.kt | 44 ++++++++++++++++--- .../generators/DelegatedMemberGenerator.kt | 9 ++-- .../generators/FakeOverrideGenerator.kt | 2 +- .../Fir2IrCallableDeclarationsGenerator.kt | 22 +--------- 4 files changed, 45 insertions(+), 32 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 b26ac7542a8..92654d791d0 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 @@ -517,10 +517,12 @@ class Fir2IrDeclarationStorage( fun getOrCreateIrProperty( property: FirProperty, irParent: IrDeclarationParent?, + predefinedOrigin: IrDeclarationOrigin? = null, isLocal: Boolean = false, + fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag? = null ): IrProperty { getCachedIrProperty(property)?.let { return it } - return callablesGenerator.createIrProperty(property, irParent, isLocal = isLocal) + return createAndCacheIrProperty(property, irParent, predefinedOrigin, isLocal, fakeOverrideOwnerLookupTag) } fun getOrCreateIrPropertyByPureField( @@ -529,7 +531,7 @@ class Fir2IrDeclarationStorage( ): IrProperty { return fieldToPropertyCache.getOrPut(field to irParent) { val containingClassId = (irParent as? IrClass)?.classId - callablesGenerator.createIrProperty( + createAndCacheIrProperty( field.toStubProperty(), irParent, fakeOverrideOwnerLookupTag = containingClassId?.toLookupTag() @@ -537,6 +539,38 @@ class Fir2IrDeclarationStorage( } } + private fun createAndCacheIrProperty( + property: FirProperty, + irParent: IrDeclarationParent?, + predefinedOrigin: IrDeclarationOrigin? = null, + isLocal: Boolean = false, + fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag? = null + ): IrProperty { + val irProperty = callablesGenerator.createIrProperty(property, irParent, predefinedOrigin, isLocal, fakeOverrideOwnerLookupTag) + val irPropertySymbol = irProperty.symbol + irProperty.backingField?.symbol?.let { + backingFieldForPropertyCache[irPropertySymbol] = it + propertyForBackingFieldCache[it] = irPropertySymbol + } + irProperty.getter?.let { + getterForPropertyCache[irPropertySymbol] = it.symbol + } + irProperty.setter?.let { + setterForPropertyCache[irPropertySymbol] = it.symbol + } + if (property.isFakeOverride(fakeOverrideOwnerLookupTag)) { + val originalProperty = property.unwrapFakeOverrides() + val key = FakeOverrideIdentifier( + originalProperty.symbol, + fakeOverrideOwnerLookupTag ?: property.containingClassLookupTag()!! + ) + irFakeOverridesForFirFakeOverrideMap[key] = irProperty + } else { + propertyCache[property] = irProperty + } + return irProperty + } + private fun FirField.toStubProperty(): FirProperty { val field = this return buildProperty { @@ -817,7 +851,7 @@ class Fir2IrDeclarationStorage( fakeOverrideOwnerLookupTag = this, getCachedIrDeclaration = ::getCachedIrProperty, createIrDeclaration = { parent, origin -> - callablesGenerator.createIrProperty( + createAndCacheIrProperty( fir, parent, predefinedOrigin = origin, fakeOverrideOwnerLookupTag = fakeOverrideOwnerLookupTag, ) }, @@ -1006,9 +1040,7 @@ class Fir2IrDeclarationStorage( propertyCache[fir]?.let { return it.backingField!!.symbol } val irParent = findIrParent(fir) val parentOrigin = (irParent as? IrDeclaration)?.origin ?: IrDeclarationOrigin.DEFINED - callablesGenerator.createIrProperty(fir, irParent, predefinedOrigin = parentOrigin).also { - callablesGenerator.setAndModifyParent(it, irParent) - }.backingField!!.symbol + createAndCacheIrProperty(fir, irParent, predefinedOrigin = parentOrigin).backingField!!.symbol } else -> { getIrVariableSymbol(fir) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/DelegatedMemberGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/DelegatedMemberGenerator.kt index deb10926b1a..089839dc37b 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/DelegatedMemberGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/DelegatedMemberGenerator.kt @@ -295,11 +295,10 @@ class DelegatedMemberGenerator(private val components: Fir2IrComponents) : Fir2I firSubClass: FirClass, firDelegateProperty: FirProperty ): IrProperty { - val delegateProperty = - callablesGenerator.createIrProperty( - firDelegateProperty, subClass, predefinedOrigin = IrDeclarationOrigin.DELEGATED_MEMBER, - fakeOverrideOwnerLookupTag = firSubClass.symbol.toLookupTag() - ) + val delegateProperty = declarationStorage.getOrCreateIrProperty( + firDelegateProperty, subClass, predefinedOrigin = IrDeclarationOrigin.DELEGATED_MEMBER, + fakeOverrideOwnerLookupTag = firSubClass.symbol.toLookupTag() + ) // the overridden symbols should be collected only after all fake overrides for all superclases are created and bound to their // overridden symbols, otherwise in some cases they will be left in inconsistent state leading to the errors in IR val baseSymbols = mutableListOf() 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 05ed532e565..5f8f3cc2201 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 @@ -150,7 +150,7 @@ class FakeOverrideGenerator( createFakeOverriddenIfNeeded( firClass, irClass, isLocal, propertyOrFieldSymbol, declarationStorage::getCachedIrProperty, - callablesGenerator::createIrProperty, + declarationStorage::getOrCreateIrProperty, createFakeOverrideSymbol = { firProperty, callableSymbol -> val symbolForOverride = FirFakeOverrideGenerator.createSymbolForSubstitutionOverride(callableSymbol, firClass.symbol.classId) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrCallableDeclarationsGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrCallableDeclarationsGenerator.kt index d4059d0c12f..d5e4d315ff7 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrCallableDeclarationsGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrCallableDeclarationsGenerator.kt @@ -661,10 +661,6 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi } } } - backingField.symbol.let { - backingFieldForPropertyCache[symbol] = it - propertyForBackingFieldCache[it] = symbol - } this.backingField = backingField } if (irParent != null) { @@ -684,9 +680,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi startOffset, endOffset, dontUseSignature = signature == null, fakeOverrideOwnerLookupTag, property.unwrapFakeOverrides().getter, - ).also { - getterForPropertyCache[symbol] = it.symbol - } + ) if (property.isVar) { this.setter = createIrPropertyAccessor( setter, property, this, type, irParent, true, @@ -700,23 +694,11 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi startOffset, endOffset, dontUseSignature = signature == null, fakeOverrideOwnerLookupTag, property.unwrapFakeOverrides().setter, - ).also { - setterForPropertyCache[symbol] = it.symbol - } + ) } } } } - if (property.isFakeOverride(fakeOverrideOwnerLookupTag)) { - val originalProperty = property.unwrapFakeOverrides() - val key = Fir2IrDeclarationStorage.FakeOverrideIdentifier( - originalProperty.symbol, - fakeOverrideOwnerLookupTag ?: property.containingClassLookupTag()!! - ) - irFakeOverridesForFirFakeOverrideMap[key] = result - } else { - propertyCache[property] = result - } result } }