From a38d60b2756bb16265df82248f4655acf6707db1 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Tue, 24 Oct 2023 11:44:14 +0300 Subject: [PATCH] [FIR2IR] Make most functions of Fir2IrDeclarationStorage return symbols instead of IR elements This is a preparation to introducing unbound symbols in fir2ir conversion --- .../fir/backend/Fir2IrCommonMemberStorage.kt | 8 +- .../fir/backend/Fir2IrConversionScope.kt | 4 +- .../kotlin/fir/backend/Fir2IrConverter.kt | 5 +- .../fir/backend/Fir2IrDeclarationStorage.kt | 103 +++++++++--------- .../fir/backend/Fir2IrLocalCallableStorage.kt | 5 +- .../kotlin/fir/backend/Fir2IrVisitor.kt | 9 +- .../generators/ClassMemberGenerator.kt | 5 +- .../generators/FakeOverrideGenerator.kt | 63 ++++++----- 8 files changed, 110 insertions(+), 92 deletions(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrCommonMemberStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrCommonMemberStorage.kt index 0943327d295..85e2f589bb5 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrCommonMemberStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrCommonMemberStorage.kt @@ -37,11 +37,11 @@ class Fir2IrCommonMemberStorage( val localClassCache: MutableMap = mutableMapOf() - val functionCache: ConcurrentHashMap = ConcurrentHashMap() + val functionCache: ConcurrentHashMap = ConcurrentHashMap() - val constructorCache: ConcurrentHashMap = ConcurrentHashMap() + val constructorCache: ConcurrentHashMap = ConcurrentHashMap() - val propertyCache: ConcurrentHashMap = ConcurrentHashMap() + val propertyCache: ConcurrentHashMap = ConcurrentHashMap() val getterForPropertyCache: ConcurrentHashMap = ConcurrentHashMap() val setterForPropertyCache: ConcurrentHashMap = ConcurrentHashMap() val backingFieldForPropertyCache: ConcurrentHashMap = ConcurrentHashMap() @@ -50,5 +50,5 @@ class Fir2IrCommonMemberStorage( val fakeOverridesInClass: MutableMap> = mutableMapOf() - val irForFirSessionDependantDeclarationMap: MutableMap = mutableMapOf() + val irForFirSessionDependantDeclarationMap: MutableMap = mutableMapOf() } diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConversionScope.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConversionScope.kt index ae6ca690a17..9ad92d0a57f 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConversionScope.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConversionScope.kt @@ -195,7 +195,7 @@ class Fir2IrConversionScope(val configuration: Fir2IrConfiguration) { fun returnTarget(expression: FirReturnExpression, declarationStorage: Fir2IrDeclarationStorage): IrFunction { val irTarget = when (val firTarget = expression.target.labeledElement) { - is FirConstructor -> declarationStorage.getCachedIrConstructor(firTarget) + is FirConstructor -> declarationStorage.getCachedIrConstructorSymbol(firTarget)?.ownerIfBound() is FirPropertyAccessor -> { var answer: IrFunction? = null for ((property, firProperty) in propertyStack.asReversed()) { @@ -207,7 +207,7 @@ class Fir2IrConversionScope(val configuration: Fir2IrConfiguration) { } answer } - else -> declarationStorage.getCachedIrFunction(firTarget) + else -> declarationStorage.getCachedIrFunctionSymbol(firTarget)?.ownerIfBound() } for (potentialTarget in functionStack.asReversed()) { if (potentialTarget == irTarget) { diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt index f6cc8f9a19c..3381bf4f8e3 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrConverter.kt @@ -590,7 +590,10 @@ class Fir2IrConverter( } private fun FirProperty.evaluate(components: Fir2IrComponents, interpreter: IrInterpreter, mode: EvaluationMode): String? { - val irProperty = components.declarationStorage.getCachedIrProperty(this, fakeOverrideOwnerLookupTag = null) ?: return null + @OptIn(UnsafeDuringIrConstructionAPI::class) + val irProperty = components.declarationStorage.getCachedIrPropertySymbol( + property = this, fakeOverrideOwnerLookupTag = null + )?.owner ?: return null fun IrProperty.tryToGetConst(): IrConst<*>? = (backingField?.initializer?.expression as? IrConst<*>) fun IrConst<*>.asString(): String { 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 c049d61e9aa..515c28728ba 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 @@ -48,6 +48,7 @@ import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.resolve.jvm.JvmClassName import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerAbiStability import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource +import org.jetbrains.kotlin.utils.addToStdlib.runIf import org.jetbrains.kotlin.utils.threadLocal import java.util.concurrent.ConcurrentHashMap @@ -71,13 +72,13 @@ class Fir2IrDeclarationStorage( private val scriptCache: ConcurrentHashMap = ConcurrentHashMap() - private val functionCache: ConcurrentHashMap = commonMemberStorage.functionCache + private val functionCache: ConcurrentHashMap = commonMemberStorage.functionCache - private val constructorCache: ConcurrentHashMap = commonMemberStorage.constructorCache + private val constructorCache: ConcurrentHashMap = commonMemberStorage.constructorCache private val initializerCache: ConcurrentHashMap = ConcurrentHashMap() - private val propertyCache: ConcurrentHashMap = commonMemberStorage.propertyCache + private val propertyCache: ConcurrentHashMap = commonMemberStorage.propertyCache private val getterForPropertyCache: ConcurrentHashMap = commonMemberStorage.getterForPropertyCache private val setterForPropertyCache: ConcurrentHashMap = @@ -120,7 +121,7 @@ class Fir2IrDeclarationStorage( * The key here is a pair of the original function (first not f/o) and lookup tag of class for which this fake override was created * THe value is IR function, build for this fake override during fir2ir translation of the module that contains parent class of this function */ - private val irForFirSessionDependantDeclarationMap: MutableMap = + private val irForFirSessionDependantDeclarationMap: MutableMap = commonMemberStorage.irForFirSessionDependantDeclarationMap data class FakeOverrideIdentifier(val originalSymbol: FirCallableSymbol<*>, val dispatchReceiverLookupTag: ConeClassLikeLookupTag) @@ -144,7 +145,7 @@ class Fir2IrDeclarationStorage( // For pure fields (from Java) only private val fieldToPropertyCache: ConcurrentHashMap, IrProperty> = ConcurrentHashMap() - private val delegatedReverseCache: ConcurrentHashMap = ConcurrentHashMap() + private val delegatedReverseCache: ConcurrentHashMap = ConcurrentHashMap() private val fieldCache: ConcurrentHashMap = ConcurrentHashMap() @@ -232,33 +233,32 @@ class Fir2IrDeclarationStorage( // ------------------------------------ functions ------------------------------------ - fun getCachedIrFunction(function: FirFunction, fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag? = null): IrSimpleFunction? { - return if (function is FirSimpleFunction) getCachedIrFunction(function, fakeOverrideOwnerLookupTag) - else localStorage.getLocalFunction(function) + fun getCachedIrFunctionSymbol(function: FirFunction, fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag? = null): IrSimpleFunctionSymbol? { + return if (function is FirSimpleFunction) getCachedIrFunctionSymbol(function, fakeOverrideOwnerLookupTag) + else localStorage.getLocalFunctionSymbol(function) } - fun getCachedIrFunction(function: FirSimpleFunction, fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag? = null): IrSimpleFunction? { - return getCachedIrFunction(function, fakeOverrideOwnerLookupTag) { + fun getCachedIrFunctionSymbol(function: FirSimpleFunction, fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag? = null): IrSimpleFunctionSymbol? { + return getCachedIrFunctionSymbol(function, fakeOverrideOwnerLookupTag) { signatureComposer.composeSignature(function, fakeOverrideOwnerLookupTag) } } - fun getCachedIrFunction( + fun getCachedIrFunctionSymbol( function: FirSimpleFunction, fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag?, signatureCalculator: () -> IdSignature? - ): IrSimpleFunction? { + ): IrSimpleFunctionSymbol? { if (function.visibility == Visibilities.Local) { - return localStorage.getLocalFunction(function) + return localStorage.getLocalFunctionSymbol(function) } - val cachedIrCallable = getCachedIrCallable( + val cachedIrCallable = getCachedIrCallableSymbol( function, fakeOverrideOwnerLookupTag, functionCache, signatureCalculator ) { signature -> - @OptIn(UnsafeDuringIrConstructionAPI::class) - symbolTable.referenceSimpleFunctionIfAny(signature)?.owner + symbolTable.referenceSimpleFunctionIfAny(signature) } return cachedIrCallable } @@ -280,7 +280,7 @@ class Fir2IrDeclarationStorage( isLocal: Boolean = false, fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag? = null ): IrSimpleFunction { - getCachedIrFunction(function, fakeOverrideOwnerLookupTag)?.let { return it } + getCachedIrFunctionSymbol(function, fakeOverrideOwnerLookupTag)?.ownerIfBound()?.let { return it } /* * Declaration storage doesn't know how to create lazy fake-overrides, it is responsibility of * FakeOverrideGenerator. So if function to be created is potentially lazy fake-override, we @@ -290,7 +290,7 @@ class Fir2IrDeclarationStorage( */ if (fakeOverrideOwnerLookupTag != function.containingClassLookupTag()) { generateLazyFakeOverrides(function.nameOrSpecialName, fakeOverrideOwnerLookupTag) - getCachedIrFunction(function, fakeOverrideOwnerLookupTag)?.let { return it } + getCachedIrFunctionSymbol(function, fakeOverrideOwnerLookupTag)?.ownerIfBound()?.let { return it } } return createAndCacheIrFunction(function, irParent(), predefinedOrigin, isLocal, fakeOverrideOwnerLookupTag) } @@ -327,11 +327,11 @@ class Fir2IrDeclarationStorage( originalFunction.symbol, fakeOverrideOwnerLookupTag ?: function.containingClassLookupTag()!! ) - irForFirSessionDependantDeclarationMap[key] = irFunction + irForFirSessionDependantDeclarationMap[key] = irFunction.symbol } else -> { - functionCache[function] = irFunction + functionCache[function] = irFunction.symbol } } } @@ -346,26 +346,24 @@ class Fir2IrDeclarationStorage( } internal fun cacheDelegationFunction(function: FirSimpleFunction, irFunction: IrSimpleFunction) { - functionCache[function] = irFunction - delegatedReverseCache[irFunction] = function + val symbol = irFunction.symbol + functionCache[function] = symbol + delegatedReverseCache[symbol] = function } internal fun cacheGeneratedFunction(firFunction: FirSimpleFunction, irFunction: IrSimpleFunction) { - functionCache[firFunction] = irFunction + functionCache[firFunction] = irFunction.symbol } // ------------------------------------ constructors ------------------------------------ - fun getCachedIrConstructor( + fun getCachedIrConstructorSymbol( constructor: FirConstructor, signatureCalculator: () -> IdSignature? = { null } - ): IrConstructor? { + ): IrConstructorSymbol? { return constructorCache[constructor] ?: signatureCalculator()?.let { signature -> - symbolTable.referenceConstructorIfAny(signature)?.let { irConstructorSymbol -> - @OptIn(UnsafeDuringIrConstructionAPI::class) - val irConstructor = irConstructorSymbol.owner - constructorCache[constructor] = irConstructor - irConstructor + symbolTable.referenceConstructorIfAny(signature)?.also { irConstructorSymbol -> + constructorCache[constructor] = irConstructorSymbol } } } @@ -385,14 +383,14 @@ class Fir2IrDeclarationStorage( predefinedOrigin: IrDeclarationOrigin? = null, isLocal: Boolean = false, ): IrConstructor { - getCachedIrConstructor(constructor)?.let { return it } + getCachedIrConstructorSymbol(constructor)?.ownerIfBound()?.let { return it } // caching of created constructor is not called here, because `callablesGenerator` calls `cacheIrConstructor` by itself return callablesGenerator.createIrConstructor(constructor, irParent(), predefinedOrigin, isLocal) } @LeakedDeclarationCaches internal fun cacheIrConstructor(constructor: FirConstructor, irConstructor: IrConstructor) { - constructorCache[constructor] = irConstructor + constructorCache[constructor] = irConstructor.symbol } fun getIrConstructorSymbol(firConstructorSymbol: FirConstructorSymbol): IrConstructorSymbol { @@ -462,11 +460,11 @@ class Fir2IrDeclarationStorage( ): IrProperty { @Suppress("NAME_SHADOWING") val property = prepareProperty(property) - getCachedIrProperty(property, fakeOverrideOwnerLookupTag)?.let { return it } + getCachedIrPropertySymbol(property, fakeOverrideOwnerLookupTag)?.ownerIfBound()?.let { return it } // See comment in [getOrCreateIrFunction] if (fakeOverrideOwnerLookupTag != property.containingClassLookupTag()) { generateLazyFakeOverrides(property.name, fakeOverrideOwnerLookupTag) - getCachedIrProperty(property, fakeOverrideOwnerLookupTag)?.let { return it } + getCachedIrPropertySymbol(property, fakeOverrideOwnerLookupTag)?.ownerIfBound()?.let { return it } } return createAndCacheIrProperty(property, irParent(), predefinedOrigin, isLocal, fakeOverrideOwnerLookupTag) } @@ -522,9 +520,9 @@ class Fir2IrDeclarationStorage( originalProperty.symbol, fakeOverrideOwnerLookupTag ?: property.containingClassLookupTag()!! ) - irForFirSessionDependantDeclarationMap[key] = irProperty + irForFirSessionDependantDeclarationMap[key] = irProperty.symbol } else { - propertyCache[property] = irProperty + propertyCache[property] = irProperty.symbol } } @@ -563,21 +561,20 @@ class Fir2IrDeclarationStorage( return result.symbol } - fun getCachedIrProperty( + fun getCachedIrPropertySymbol( property: FirProperty, fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag?, signatureCalculator: () -> IdSignature? = { signatureComposer.composeSignature(property, fakeOverrideOwnerLookupTag) } - ): IrProperty? { + ): IrPropertySymbol? { @Suppress("NAME_SHADOWING") val property = prepareProperty(property) - return getCachedIrCallable( + return getCachedIrCallableSymbol( property, fakeOverrideOwnerLookupTag, propertyCache, signatureCalculator ) { signature -> - @OptIn(UnsafeDuringIrConstructionAPI::class) - symbolTable.referencePropertyIfAny(signature)?.owner + symbolTable.referencePropertyIfAny(signature) } } @@ -598,8 +595,9 @@ class Fir2IrDeclarationStorage( } internal fun cacheDelegatedProperty(property: FirProperty, irProperty: IrProperty) { - propertyCache[property] = irProperty - delegatedReverseCache[irProperty] = property + val symbol = irProperty.symbol + propertyCache[property] = symbol + delegatedReverseCache[symbol] = property } // ------------------------------------ fields ------------------------------------ @@ -658,7 +656,7 @@ class Fir2IrDeclarationStorage( if (fir.isLocal) { return localStorage.getDelegatedProperty(fir)?.delegate?.symbol ?: getIrVariableSymbol(fir) } - propertyCache[fir]?.let { return it.backingField!!.symbol } + propertyCache[fir]?.ownerIfBound()?.let { return it.backingField!!.symbol } val irParent = findIrParent(fir, fakeOverrideOwnerLookupTag = null) val parentOrigin = (irParent as? IrDeclaration)?.origin ?: IrDeclarationOrigin.DEFINED createAndCacheIrProperty(fir, irParent, predefinedOrigin = parentOrigin).backingField!!.symbol @@ -840,7 +838,7 @@ class Fir2IrDeclarationStorage( // ------------------------------------ callables ------------------------------------ fun originalDeclarationForDelegated(irDeclaration: IrDeclaration): FirDeclaration? { - return delegatedReverseCache[irDeclaration] + return delegatedReverseCache[irDeclaration.symbol] } internal fun saveFakeOverrideInClass( @@ -880,13 +878,13 @@ class Fir2IrDeclarationStorage( } } - private inline fun getCachedIrCallable( + private inline fun getCachedIrCallableSymbol( declaration: FC, fakeOverrideOwnerLookupTag: ConeClassLikeLookupTag?, - cache: MutableMap, + cache: MutableMap, signatureCalculator: () -> IdSignature?, - referenceIfAny: (IdSignature) -> IC? - ): IC? { + referenceIfAny: (IdSignature) -> IS? + ): IS? { /* * There should be two types of declarations: * 1. Real declarations. They are stored in simple FirDeclaration -> IrDeclaration [cache] @@ -909,7 +907,7 @@ class Fir2IrDeclarationStorage( declaration.unwrapFakeOverridesOrDelegated().symbol, fakeOverrideOwnerLookupTag ?: declaration.containingClassLookupTag()!! ) - irForFirSessionDependantDeclarationMap[key]?.let { return it as IC } + irForFirSessionDependantDeclarationMap[key]?.let { return it as IS } } else { cache[declaration]?.let { return it } } @@ -1173,3 +1171,8 @@ internal var FirProperty.isStubPropertyForPureField: Boolean? by FirDeclarationD */ @RequiresOptIn annotation class LeakedDeclarationCaches + +@OptIn(UnsafeDuringIrConstructionAPI::class) +internal fun IrBindableSymbol<*, D>.ownerIfBound(): D? { + return runIf(isBound) { owner } +} diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrLocalCallableStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrLocalCallableStorage.kt index 0a25316ee7f..54cb3deff0d 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrLocalCallableStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrLocalCallableStorage.kt @@ -7,6 +7,7 @@ package org.jetbrains.kotlin.fir.backend import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.ir.declarations.* +import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol class Fir2IrLocalCallableStorage { @@ -32,8 +33,8 @@ class Fir2IrLocalCallableStorage { fun getVariable(variable: FirVariable): IrVariable? = last { getVariable(variable) } - fun getLocalFunction(localFunction: FirFunction): IrSimpleFunction? = - last { getLocalFunction(localFunction) } + fun getLocalFunctionSymbol(localFunction: FirFunction): IrSimpleFunctionSymbol? = + last { getLocalFunction(localFunction)?.symbol } fun getDelegatedProperty(property: FirProperty): IrLocalDelegatedProperty? = last { getDelegatedProperty(property) } diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt index 60c1d4c3d4b..20dbebbb63e 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt @@ -383,7 +383,8 @@ class Fir2IrVisitor( // ================================================================================== override fun visitConstructor(constructor: FirConstructor, data: Any?): IrElement = whileAnalysing(session, constructor) { - val irConstructor = declarationStorage.getCachedIrConstructor(constructor)!! + @OptIn(UnsafeDuringIrConstructionAPI::class) + val irConstructor = declarationStorage.getCachedIrConstructorSymbol(constructor)!!.owner return conversionScope.withFunction(irConstructor) { memberGenerator.convertFunctionContent(irConstructor, constructor, containingClass = conversionScope.containerFirClass()) } @@ -406,7 +407,8 @@ class Fir2IrVisitor( simpleFunction, irParent = conversionScope.parent(), predefinedOrigin = IrDeclarationOrigin.LOCAL_FUNCTION, isLocal = true ) } else { - declarationStorage.getCachedIrFunction(simpleFunction)!! + @OptIn(UnsafeDuringIrConstructionAPI::class) + declarationStorage.getCachedIrFunctionSymbol(simpleFunction)!!.owner } return conversionScope.withFunction(irFunction) { memberGenerator.convertFunctionContent( @@ -496,7 +498,8 @@ class Fir2IrVisitor( override fun visitProperty(property: FirProperty, data: Any?): IrElement = whileAnalysing(session, property) { if (property.isLocal) return visitLocalVariable(property) - val irProperty = declarationStorage.getCachedIrProperty(property, fakeOverrideOwnerLookupTag = null) + @OptIn(UnsafeDuringIrConstructionAPI::class) + val irProperty = declarationStorage.getCachedIrPropertySymbol(property, fakeOverrideOwnerLookupTag = null)?.owner ?: return IrErrorExpressionImpl( UNDEFINED_OFFSET, UNDEFINED_OFFSET, IrErrorTypeImpl(null, emptyList(), Variance.INVARIANT), diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/ClassMemberGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/ClassMemberGenerator.kt index d7e571a56a5..8bfcaf07a3a 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/ClassMemberGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/ClassMemberGenerator.kt @@ -33,6 +33,7 @@ import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrFieldAccessExpression import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol +import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.util.constructedClassType import org.jetbrains.kotlin.ir.util.isSetter @@ -63,7 +64,9 @@ internal class ClassMemberGenerator( } val primaryConstructor = allDeclarations.firstOrNull { it is FirConstructor && it.isPrimary } as FirConstructor? - val irPrimaryConstructor = primaryConstructor?.let { declarationStorage.getCachedIrConstructor(it)!! } + + @OptIn(UnsafeDuringIrConstructionAPI::class) + val irPrimaryConstructor = primaryConstructor?.let { declarationStorage.getCachedIrConstructorSymbol(it)!!.owner } if (irPrimaryConstructor != null) { with(declarationStorage) { enterScope(irPrimaryConstructor.symbol) 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 04f290beb74..43c9eaa5261 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 @@ -18,10 +18,7 @@ import org.jetbrains.kotlin.fir.symbols.FirBasedSymbol import org.jetbrains.kotlin.fir.symbols.impl.* import org.jetbrains.kotlin.fir.types.coneType import org.jetbrains.kotlin.ir.declarations.* -import org.jetbrains.kotlin.ir.symbols.IrPropertySymbol -import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol -import org.jetbrains.kotlin.ir.symbols.IrSymbol -import org.jetbrains.kotlin.ir.symbols.UnsafeDuringIrConstructionAPI +import org.jetbrains.kotlin.ir.symbols.* import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.util.IdSignature import org.jetbrains.kotlin.ir.util.parentAsClass @@ -31,9 +28,9 @@ class FakeOverrideGenerator( private val components: Fir2IrComponents, private val conversionScope: Fir2IrConversionScope ) : Fir2IrComponents by components { - private val baseFunctionSymbols: MutableMap> = mutableMapOf() - private val basePropertySymbols: MutableMap> = mutableMapOf() - private val baseStaticFieldSymbols: MutableMap> = mutableMapOf() + private val baseFunctionSymbols: MutableMap> = mutableMapOf() + private val basePropertySymbols: MutableMap> = mutableMapOf() + private val baseStaticFieldSymbols: MutableMap> = mutableMapOf() private inline fun IrSimpleFunction.withFunction(f: IrSimpleFunction.() -> Unit) { conversionScope.withFunction(this, f) @@ -85,7 +82,7 @@ class FakeOverrideGenerator( } } - internal fun generateFakeOverridesForName( + private fun generateFakeOverridesForName( irClass: IrClass, useSiteOrStaticScope: FirScope, name: Name, @@ -95,10 +92,9 @@ class FakeOverrideGenerator( ) { val isLocal = firClass !is FirRegularClass || firClass.isLocal useSiteOrStaticScope.processFunctionsByName(name) { functionSymbol -> - @OptIn(LeakedDeclarationCaches::class) createFakeOverriddenIfNeeded( firClass, irClass, isLocal, functionSymbol, - declarationStorage::getCachedIrFunction, + declarationStorage::getCachedIrFunctionSymbol, declarationStorage::createAndCacheIrFunction, createFakeOverrideSymbol = { firFunction, callableSymbol -> val symbol = FirFakeOverrideGenerator.createSymbolForSubstitutionOverride(callableSymbol, firClass.symbol.classId) @@ -127,7 +123,7 @@ class FakeOverrideGenerator( @OptIn(LeakedDeclarationCaches::class) createFakeOverriddenIfNeeded( firClass, irClass, isLocal, propertyOrFieldSymbol, - declarationStorage::getCachedIrProperty, + declarationStorage::getCachedIrPropertySymbol, declarationStorage::createAndCacheIrProperty, createFakeOverrideSymbol = { firProperty, callableSymbol -> val symbolForOverride = @@ -156,7 +152,7 @@ class FakeOverrideGenerator( if (!propertyOrFieldSymbol.isStatic) return@processPropertiesByName createFakeOverriddenIfNeeded( firClass, irClass, isLocal, propertyOrFieldSymbol, - { field, _, _ -> declarationStorage.getCachedIrFieldStaticFakeOverrideByDeclaration(field) }, + { field, _, _ -> declarationStorage.getCachedIrFieldStaticFakeOverrideByDeclaration(field)?.symbol }, { field, irParent, _, _ -> declarationStorage.getOrCreateIrField(field, irParent) }, @@ -196,7 +192,7 @@ class FakeOverrideGenerator( } else { listOf(originalSymbol) } - baseFunctionSymbols[fakeOverride] = baseFirSymbolsForFakeOverride + baseFunctionSymbols[fakeOverride.symbol] = baseFirSymbolsForFakeOverride } internal fun calcBaseSymbolsForFakeOverrideProperty( @@ -212,23 +208,29 @@ class FakeOverrideGenerator( } else { listOf(originalSymbol) } - basePropertySymbols[fakeOverride] = baseFirSymbolsForFakeOverride + basePropertySymbols[fakeOverride.symbol] = baseFirSymbolsForFakeOverride } private fun FirCallableSymbol<*>.shouldHaveComputedBaseSymbolsForClass(classLookupTag: ConeClassLikeLookupTag): Boolean = fir.origin.fromSupertypes && dispatchReceiverClassLookupTagOrNull() == classLookupTag - private inline fun , reified I : IrDeclaration> createFakeOverriddenIfNeeded( + @Suppress("IncorrectFormatting") + private inline fun < + reified D : FirCallableDeclaration, + reified S : FirCallableSymbol, + reified ID : IrDeclaration, + reified IS : IrBindableSymbol<*, ID> + > createFakeOverriddenIfNeeded( klass: FirClass, irClass: IrClass, isLocal: Boolean, originalSymbol: FirCallableSymbol<*>, - cachedIrDeclaration: (firDeclaration: D, dispatchReceiverLookupTag: ConeClassLikeLookupTag?, () -> IdSignature?) -> I?, - createIrDeclaration: (firDeclaration: D, irParent: IrClass, origin: IrDeclarationOrigin, isLocal: Boolean) -> I, + cachedIrDeclarationSymbol: (firDeclaration: D, dispatchReceiverLookupTag: ConeClassLikeLookupTag?, () -> IdSignature?) -> IS?, + createIrDeclaration: (firDeclaration: D, irParent: IrClass, origin: IrDeclarationOrigin, isLocal: Boolean) -> ID, createFakeOverrideSymbol: (firDeclaration: D, baseSymbol: S) -> S, - baseSymbols: MutableMap>, - result: MutableList?, - containsErrorTypes: (I) -> Boolean, + baseSymbols: MutableMap>, + result: MutableList?, + containsErrorTypes: (ID) -> Boolean, realDeclarationSymbols: Set>, computeDirectOverridden: FirTypeScope.(S) -> List, scope: FirScope, @@ -264,25 +266,28 @@ class FakeOverrideGenerator( return } } - val irDeclaration = cachedIrDeclaration(fakeOverrideFirDeclaration, null) { + val irSymbol = cachedIrDeclarationSymbol(fakeOverrideFirDeclaration, null) { // Sometimes we can have clashing here when FIR substitution/intersection override // have the same signature. // Now we avoid this problem by signature caching, // so both FIR overrides correspond to one IR fake override signatureComposer.composeSignature(fakeOverrideFirDeclaration) - }?.takeIf { it.parent == irClass } + }?.takeIf { it.ownerIfBound()?.parent == irClass } ?: createIrDeclaration( fakeOverrideFirDeclaration, irClass, IrDeclarationOrigin.FAKE_OVERRIDE, isLocal - ) - if (containsErrorTypes(irDeclaration)) { + ).symbol as IS + + @OptIn(UnsafeDuringIrConstructionAPI::class) + val owner = irSymbol.owner + if (containsErrorTypes(owner)) { return } - baseSymbols[irDeclaration] = baseFirSymbolsForFakeOverride + baseSymbols[irSymbol] = baseFirSymbolsForFakeOverride - result?.add(irDeclaration) + result?.add(owner) } private inline fun > createFirFakeOverride( @@ -384,7 +389,7 @@ class FakeOverrideGenerator( } internal fun getOverriddenSymbolsForFakeOverride(function: IrSimpleFunction): List? { - val baseSymbols = baseFunctionSymbols[function] ?: return null + val baseSymbols = baseFunctionSymbols[function.symbol] ?: return null return getOverriddenSymbolsInSupertypes( function, baseSymbols @@ -411,7 +416,7 @@ class FakeOverrideGenerator( } internal fun getOverriddenSymbolsForFakeOverride(property: IrProperty): List? { - val baseSymbols = basePropertySymbols[property] ?: return null + val baseSymbols = basePropertySymbols[property.symbol] ?: return null return getOverriddenSymbolsInSupertypes( property, baseSymbols @@ -472,7 +477,7 @@ class FakeOverrideGenerator( } } is IrProperty -> { - val baseSymbols = basePropertySymbols[declaration]!! + val baseSymbols = basePropertySymbols[declaration.symbol]!! declaration.withProperty { discardAccessorsAccordingToBaseVisibility(baseSymbols) setOverriddenSymbolsForProperty(declarationStorage, declaration.isVar, baseSymbols)