From 1e090d45d17bd3db3533fc08c1b147ba6f32417c Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Wed, 13 Sep 2023 17:33:48 +0300 Subject: [PATCH] [FIR2IR] Simplify Fir2IrDeclarationStorage.getIrConstructorSymbol For reasoning refer to the message of the previous commit It is the same as for `getIrFunctionSymbol` --- .../kotlin/fir/backend/ConversionUtils.kt | 15 +++++---- .../fir/backend/Fir2IrDeclarationStorage.kt | 32 +++++++++---------- .../Fir2IrCallableDeclarationsGenerator.kt | 13 ++++---- 3 files changed, 31 insertions(+), 29 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 da089b32f39..fdefdfce2a0 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 @@ -188,14 +188,14 @@ fun FirClassifierSymbol<*>.toSymbol( } context(Fir2IrComponents) -fun FirBasedSymbol<*>.toSymbolForCall( +private fun FirBasedSymbol<*>.toSymbolForCall( dispatchReceiver: FirExpression?, preferGetter: Boolean, explicitReceiver: FirExpression? = null, isDelegate: Boolean = false, isReference: Boolean = false ): IrSymbol? = when (this) { - is FirCallableSymbol<*> -> unwrapCallRepresentative().toSymbolForCall( + is FirCallableSymbol<*> -> toSymbolForCall( dispatchReceiver, preferGetter, explicitReceiver, @@ -207,16 +207,19 @@ fun FirBasedSymbol<*>.toSymbolForCall( else -> error("Unknown symbol: $this") } +context(Fir2IrComponents) fun FirReference.extractSymbolForCall(): FirBasedSymbol<*>? { if (this !is FirResolvedNamedReference) { return null } var symbol = resolvedSymbol - if (symbol is FirCallableSymbol<*> && symbol.origin == FirDeclarationOrigin.SubstitutionOverride.CallSite) { - symbol = symbol.fir.unwrapUseSiteSubstitutionOverrides().symbol + if (symbol is FirCallableSymbol<*>) { + if (symbol.origin == FirDeclarationOrigin.SubstitutionOverride.CallSite) { + symbol = symbol.fir.unwrapUseSiteSubstitutionOverrides().symbol + } + symbol = (symbol as FirCallableSymbol<*>).unwrapCallRepresentative() } - return symbol } @@ -302,7 +305,7 @@ fun FirCallableSymbol<*>.toSymbolForCall( } ?: declarationStorage.getIrPropertySymbol(this) } } - + is FirConstructorSymbol -> declarationStorage.getIrConstructorSymbol(fir.originalConstructorIfTypeAlias?.symbol ?: this) is FirFunctionSymbol<*> -> declarationStorage.getIrFunctionSymbol(this, fakeOverrideOwnerLookupTag) is FirPropertySymbol -> declarationStorage.getIrPropertySymbol(this, fakeOverrideOwnerLookupTag) is FirFieldSymbol -> declarationStorage.getOrCreateIrField(this, fakeOverrideOwnerLookupTag).symbol 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 8afafd9f868..2e9ce66de08 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 @@ -22,7 +22,6 @@ import org.jetbrains.kotlin.fir.descriptors.FirModuleDescriptor import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression import org.jetbrains.kotlin.fir.java.symbols.FirJavaOverriddenSyntheticPropertySymbol import org.jetbrains.kotlin.fir.lazy.Fir2IrLazyClass -import org.jetbrains.kotlin.fir.lazy.Fir2IrLazyConstructor import org.jetbrains.kotlin.fir.references.toResolvedValueParameterSymbol import org.jetbrains.kotlin.fir.resolve.providers.firProvider import org.jetbrains.kotlin.fir.resolve.toSymbol @@ -375,9 +374,19 @@ class Fir2IrDeclarationStorage( irParent: IrClass, predefinedOrigin: IrDeclarationOrigin? = null, isLocal: Boolean = false, + ): IrConstructor { + return getOrCreateIrConstructor(constructor, { irParent }, predefinedOrigin, isLocal) + } + + fun getOrCreateIrConstructor( + constructor: FirConstructor, + irParent: () -> IrClass, + predefinedOrigin: IrDeclarationOrigin? = null, + isLocal: Boolean = false, ): IrConstructor { getCachedIrConstructor(constructor)?.let { return it } - return callablesGenerator.createIrConstructor(constructor, irParent, predefinedOrigin, isLocal) + // caching of created constructor is not called here, because `callablesGenerator` calls `cacheIrConstructor` by itself + return callablesGenerator.createIrConstructor(constructor, irParent(), predefinedOrigin, isLocal) } @LeakedDeclarationCaches @@ -387,21 +396,10 @@ class Fir2IrDeclarationStorage( fun getIrConstructorSymbol(firConstructorSymbol: FirConstructorSymbol): IrConstructorSymbol { val fir = firConstructorSymbol.fir - return getIrCallableSymbol( - firConstructorSymbol, - fakeOverrideOwnerLookupTag = null, - getCachedIrDeclaration = { constructor: FirConstructor, _, calculator -> getCachedIrConstructor(constructor, calculator) }, - createIrDeclaration = { parent, origin -> - callablesGenerator.createIrConstructor(fir, parent as IrClass, predefinedOrigin = origin) - }, - createIrLazyDeclaration = { signature, lazyParent, declarationOrigin -> - val irConstructor = lazyDeclarationsGenerator.createIrLazyConstructor(fir, signature, declarationOrigin, lazyParent) - constructorCache[fir] = irConstructor - // NB: this is needed to prevent recursions in case of self bounds - (irConstructor as Fir2IrLazyConstructor).prepareTypeParameters() - irConstructor - }, - ) as IrConstructorSymbol + return getOrCreateIrConstructor( + fir, + { findIrParent(fir, fakeOverrideOwnerLookupTag = null) as IrClass } + ).symbol } 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 f39c9bdcb92..53b0e3121d3 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 @@ -197,12 +197,13 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi predefinedOrigin: IrDeclarationOrigin? = null, isLocal: Boolean = false, ): IrConstructor = convertCatching(constructor) { - val origin = constructor.computeIrOrigin(predefinedOrigin) + val origin = constructor.computeIrOrigin(predefinedOrigin, irParent.origin) val isPrimary = constructor.isPrimary - val signature = - runUnless(isLocal || !configuration.linkViaSignatures) { - signatureComposer.composeSignature(constructor) - } + // we need to compose signature even if `linkViaSignatures` set to false, because otherwise we will never + // create lazy constructor (it requires not nullable signature) + val signature = runUnless(isLocal) { + signatureComposer.composeSignature(constructor) + } if (irParent is Fir2IrLazyClass && signature != null) { val lazyConstructor = lazyDeclarationsGenerator.createIrLazyConstructor(constructor, signature, origin, irParent) as Fir2IrLazyConstructor @@ -215,7 +216,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi } val visibility = if (irParent.isAnonymousObject) Visibilities.Public else constructor.visibility return constructor.convertWithOffsets { startOffset, endOffset -> - declareIrConstructor(signature) { symbol -> + declareIrConstructor(signature.takeIf { components.configuration.linkViaSignatures }) { symbol -> classifierStorage.preCacheTypeParameters(constructor, symbol) irFactory.createConstructor( startOffset = startOffset,