[FIR2IR] Simplify Fir2IrDeclarationStorage.getIrConstructorSymbol

For reasoning refer to the message of the previous commit
It is the same as for `getIrFunctionSymbol`
This commit is contained in:
Dmitriy Novozhilov
2023-09-13 17:33:48 +03:00
committed by Space Team
parent c7c83de528
commit 1e090d45d1
3 changed files with 31 additions and 29 deletions
@@ -188,14 +188,14 @@ fun FirClassifierSymbol<*>.toSymbol(
} }
context(Fir2IrComponents) context(Fir2IrComponents)
fun FirBasedSymbol<*>.toSymbolForCall( private fun FirBasedSymbol<*>.toSymbolForCall(
dispatchReceiver: FirExpression?, dispatchReceiver: FirExpression?,
preferGetter: Boolean, preferGetter: Boolean,
explicitReceiver: FirExpression? = null, explicitReceiver: FirExpression? = null,
isDelegate: Boolean = false, isDelegate: Boolean = false,
isReference: Boolean = false isReference: Boolean = false
): IrSymbol? = when (this) { ): IrSymbol? = when (this) {
is FirCallableSymbol<*> -> unwrapCallRepresentative().toSymbolForCall( is FirCallableSymbol<*> -> toSymbolForCall(
dispatchReceiver, dispatchReceiver,
preferGetter, preferGetter,
explicitReceiver, explicitReceiver,
@@ -207,16 +207,19 @@ fun FirBasedSymbol<*>.toSymbolForCall(
else -> error("Unknown symbol: $this") else -> error("Unknown symbol: $this")
} }
context(Fir2IrComponents)
fun FirReference.extractSymbolForCall(): FirBasedSymbol<*>? { fun FirReference.extractSymbolForCall(): FirBasedSymbol<*>? {
if (this !is FirResolvedNamedReference) { if (this !is FirResolvedNamedReference) {
return null return null
} }
var symbol = resolvedSymbol var symbol = resolvedSymbol
if (symbol is FirCallableSymbol<*> && symbol.origin == FirDeclarationOrigin.SubstitutionOverride.CallSite) { if (symbol is FirCallableSymbol<*>) {
symbol = symbol.fir.unwrapUseSiteSubstitutionOverrides<FirCallableDeclaration>().symbol if (symbol.origin == FirDeclarationOrigin.SubstitutionOverride.CallSite) {
symbol = symbol.fir.unwrapUseSiteSubstitutionOverrides<FirCallableDeclaration>().symbol
}
symbol = (symbol as FirCallableSymbol<*>).unwrapCallRepresentative()
} }
return symbol return symbol
} }
@@ -302,7 +305,7 @@ fun FirCallableSymbol<*>.toSymbolForCall(
} ?: declarationStorage.getIrPropertySymbol(this) } ?: declarationStorage.getIrPropertySymbol(this)
} }
} }
is FirConstructorSymbol -> declarationStorage.getIrConstructorSymbol(fir.originalConstructorIfTypeAlias?.symbol ?: this)
is FirFunctionSymbol<*> -> declarationStorage.getIrFunctionSymbol(this, fakeOverrideOwnerLookupTag) is FirFunctionSymbol<*> -> declarationStorage.getIrFunctionSymbol(this, fakeOverrideOwnerLookupTag)
is FirPropertySymbol -> declarationStorage.getIrPropertySymbol(this, fakeOverrideOwnerLookupTag) is FirPropertySymbol -> declarationStorage.getIrPropertySymbol(this, fakeOverrideOwnerLookupTag)
is FirFieldSymbol -> declarationStorage.getOrCreateIrField(this, fakeOverrideOwnerLookupTag).symbol is FirFieldSymbol -> declarationStorage.getOrCreateIrField(this, fakeOverrideOwnerLookupTag).symbol
@@ -22,7 +22,6 @@ import org.jetbrains.kotlin.fir.descriptors.FirModuleDescriptor
import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression
import org.jetbrains.kotlin.fir.java.symbols.FirJavaOverriddenSyntheticPropertySymbol import org.jetbrains.kotlin.fir.java.symbols.FirJavaOverriddenSyntheticPropertySymbol
import org.jetbrains.kotlin.fir.lazy.Fir2IrLazyClass 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.references.toResolvedValueParameterSymbol
import org.jetbrains.kotlin.fir.resolve.providers.firProvider import org.jetbrains.kotlin.fir.resolve.providers.firProvider
import org.jetbrains.kotlin.fir.resolve.toSymbol import org.jetbrains.kotlin.fir.resolve.toSymbol
@@ -375,9 +374,19 @@ class Fir2IrDeclarationStorage(
irParent: IrClass, irParent: IrClass,
predefinedOrigin: IrDeclarationOrigin? = null, predefinedOrigin: IrDeclarationOrigin? = null,
isLocal: Boolean = false, isLocal: Boolean = false,
): IrConstructor {
return getOrCreateIrConstructor(constructor, { irParent }, predefinedOrigin, isLocal)
}
fun getOrCreateIrConstructor(
constructor: FirConstructor,
irParent: () -> IrClass,
predefinedOrigin: IrDeclarationOrigin? = null,
isLocal: Boolean = false,
): IrConstructor { ): IrConstructor {
getCachedIrConstructor(constructor)?.let { return it } 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 @LeakedDeclarationCaches
@@ -387,21 +396,10 @@ class Fir2IrDeclarationStorage(
fun getIrConstructorSymbol(firConstructorSymbol: FirConstructorSymbol): IrConstructorSymbol { fun getIrConstructorSymbol(firConstructorSymbol: FirConstructorSymbol): IrConstructorSymbol {
val fir = firConstructorSymbol.fir val fir = firConstructorSymbol.fir
return getIrCallableSymbol( return getOrCreateIrConstructor(
firConstructorSymbol, fir,
fakeOverrideOwnerLookupTag = null, { findIrParent(fir, fakeOverrideOwnerLookupTag = null) as IrClass }
getCachedIrDeclaration = { constructor: FirConstructor, _, calculator -> getCachedIrConstructor(constructor, calculator) }, ).symbol
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
} }
@@ -197,12 +197,13 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
predefinedOrigin: IrDeclarationOrigin? = null, predefinedOrigin: IrDeclarationOrigin? = null,
isLocal: Boolean = false, isLocal: Boolean = false,
): IrConstructor = convertCatching(constructor) { ): IrConstructor = convertCatching(constructor) {
val origin = constructor.computeIrOrigin(predefinedOrigin) val origin = constructor.computeIrOrigin(predefinedOrigin, irParent.origin)
val isPrimary = constructor.isPrimary val isPrimary = constructor.isPrimary
val signature = // we need to compose signature even if `linkViaSignatures` set to false, because otherwise we will never
runUnless(isLocal || !configuration.linkViaSignatures) { // create lazy constructor (it requires not nullable signature)
signatureComposer.composeSignature(constructor) val signature = runUnless(isLocal) {
} signatureComposer.composeSignature(constructor)
}
if (irParent is Fir2IrLazyClass && signature != null) { if (irParent is Fir2IrLazyClass && signature != null) {
val lazyConstructor = lazyDeclarationsGenerator.createIrLazyConstructor(constructor, signature, origin, irParent) as Fir2IrLazyConstructor 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 val visibility = if (irParent.isAnonymousObject) Visibilities.Public else constructor.visibility
return constructor.convertWithOffsets { startOffset, endOffset -> return constructor.convertWithOffsets { startOffset, endOffset ->
declareIrConstructor(signature) { symbol -> declareIrConstructor(signature.takeIf { components.configuration.linkViaSignatures }) { symbol ->
classifierStorage.preCacheTypeParameters(constructor, symbol) classifierStorage.preCacheTypeParameters(constructor, symbol)
irFactory.createConstructor( irFactory.createConstructor(
startOffset = startOffset, startOffset = startOffset,