[FIR2IR] Make most field-related functions of Fir2IrDeclarationStorage return symbols

This is a preparation to introducing unbound symbols in fir2ir conversion
This commit is contained in:
Dmitriy Novozhilov
2023-10-24 12:16:40 +03:00
committed by Space Team
parent 429010d70e
commit 2ba7194467
3 changed files with 15 additions and 13 deletions
@@ -147,11 +147,11 @@ class Fir2IrDeclarationStorage(
private val delegatedReverseCache: ConcurrentHashMap<IrSymbol, FirDeclaration> = ConcurrentHashMap() private val delegatedReverseCache: ConcurrentHashMap<IrSymbol, FirDeclaration> = ConcurrentHashMap()
private val fieldCache: ConcurrentHashMap<FirField, IrField> = ConcurrentHashMap() private val fieldCache: ConcurrentHashMap<FirField, IrFieldSymbol> = ConcurrentHashMap()
private data class FieldStaticOverrideKey(val lookupTag: ConeClassLikeLookupTag, val name: Name) private data class FieldStaticOverrideKey(val lookupTag: ConeClassLikeLookupTag, val name: Name)
private val fieldStaticOverrideCache: ConcurrentHashMap<FieldStaticOverrideKey, IrField> = ConcurrentHashMap() private val fieldStaticOverrideCache: ConcurrentHashMap<FieldStaticOverrideKey, IrFieldSymbol> = ConcurrentHashMap()
private val localStorage: Fir2IrLocalCallableStorage by threadLocal { Fir2IrLocalCallableStorage() } private val localStorage: Fir2IrLocalCallableStorage by threadLocal { Fir2IrLocalCallableStorage() }
@@ -609,11 +609,12 @@ class Fir2IrDeclarationStorage(
val fir = firFieldSymbol.fir val fir = firFieldSymbol.fir
val staticFakeOverrideKey = getFieldStaticFakeOverrideKey(fir, fakeOverrideOwnerLookupTag) val staticFakeOverrideKey = getFieldStaticFakeOverrideKey(fir, fakeOverrideOwnerLookupTag)
if (staticFakeOverrideKey == null) { if (staticFakeOverrideKey == null) {
fieldCache[fir]?.let { return it } fieldCache[fir]?.ownerIfBound()?.let { return it }
} else { } else {
generateLazyFakeOverrides(fir.name, fakeOverrideOwnerLookupTag) generateLazyFakeOverrides(fir.name, fakeOverrideOwnerLookupTag)
// Lazy static fake override should always exist // Lazy static fake override should always exist
return fieldStaticOverrideCache[staticFakeOverrideKey]!! @OptIn(UnsafeDuringIrConstructionAPI::class)
return fieldStaticOverrideCache[staticFakeOverrideKey]!!.owner
} }
// In case of type parameters from the parent as the field's return type, find the parent ahead to cache type parameters. // In case of type parameters from the parent as the field's return type, find the parent ahead to cache type parameters.
val irParent = findIrParent(fir, fakeOverrideOwnerLookupTag) val irParent = findIrParent(fir, fakeOverrideOwnerLookupTag)
@@ -628,11 +629,11 @@ class Fir2IrDeclarationStorage(
// TODO: there is a mess with methods for fields // TODO: there is a mess with methods for fields
// we have three (!) different functions to getOrCreate field in different circumstances // we have three (!) different functions to getOrCreate field in different circumstances
fun getOrCreateIrField(field: FirField, irParent: IrDeclarationParent?): IrField { fun getOrCreateIrField(field: FirField, irParent: IrDeclarationParent?): IrField {
getCachedIrField(field, irParent)?.let { return it } getCachedIrFieldSymbol(field, irParent)?.ownerIfBound()?.let { return it }
return createAndCacheIrField(field, irParent) return createAndCacheIrField(field, irParent)
} }
private fun getCachedIrField(field: FirField, irParent: IrDeclarationParent?): IrField? { private fun getCachedIrFieldSymbol(field: FirField, irParent: IrDeclarationParent?): IrFieldSymbol? {
val containingClassLookupTag = (irParent as IrClass?)?.classId?.toLookupTag() val containingClassLookupTag = (irParent as IrClass?)?.classId?.toLookupTag()
val staticFakeOverrideKey = getFieldStaticFakeOverrideKey(field, containingClassLookupTag) val staticFakeOverrideKey = getFieldStaticFakeOverrideKey(field, containingClassLookupTag)
return if (staticFakeOverrideKey == null) { return if (staticFakeOverrideKey == null) {
@@ -667,11 +668,11 @@ class Fir2IrDeclarationStorage(
} }
} }
fun getCachedIrDelegateOrBackingField(field: FirField): IrField? { fun getCachedIrDelegateOrBackingFieldSymbol(field: FirField): IrFieldSymbol? {
return fieldCache[field] return fieldCache[field]
} }
fun getCachedIrFieldStaticFakeOverrideByDeclaration(field: FirField): IrField? { fun getCachedIrFieldStaticFakeOverrideSymbolByDeclaration(field: FirField): IrFieldSymbol? {
val ownerLookupTag = field.containingClassLookupTag() ?: return null val ownerLookupTag = field.containingClassLookupTag() ?: return null
return fieldStaticOverrideCache[FieldStaticOverrideKey(ownerLookupTag, field.name)] return fieldStaticOverrideCache[FieldStaticOverrideKey(ownerLookupTag, field.name)]
} }
@@ -688,7 +689,7 @@ class Fir2IrDeclarationStorage(
if (constructorProperty != null) { if (constructorProperty != null) {
val irProperty = getOrCreateIrProperty(constructorProperty, irClass) val irProperty = getOrCreateIrProperty(constructorProperty, irClass)
val backingField = irProperty.backingField!! val backingField = irProperty.backingField!!
fieldCache[field] = backingField fieldCache[field] = backingField.symbol
return backingField return backingField
} }
} }
@@ -711,9 +712,9 @@ class Fir2IrDeclarationStorage(
val containingClassLookupTag = (irParent as IrClass?)?.classId?.toLookupTag() val containingClassLookupTag = (irParent as IrClass?)?.classId?.toLookupTag()
val staticFakeOverrideKey = getFieldStaticFakeOverrideKey(field, containingClassLookupTag) val staticFakeOverrideKey = getFieldStaticFakeOverrideKey(field, containingClassLookupTag)
if (staticFakeOverrideKey == null) { if (staticFakeOverrideKey == null) {
fieldCache[field] = irField fieldCache[field] = irField.symbol
} else { } else {
fieldStaticOverrideCache[staticFakeOverrideKey] = irField fieldStaticOverrideCache[staticFakeOverrideKey] = irField.symbol
} }
return irField return irField
} }
@@ -95,7 +95,8 @@ class Fir2IrVisitor(
override fun visitField(field: FirField, data: Any?): IrField = whileAnalysing(session, field) { override fun visitField(field: FirField, data: Any?): IrField = whileAnalysing(session, field) {
if (field.isSynthetic) { if (field.isSynthetic) {
return declarationStorage.getCachedIrDelegateOrBackingField(field)!!.apply { @OptIn(UnsafeDuringIrConstructionAPI::class)
return declarationStorage.getCachedIrDelegateOrBackingFieldSymbol(field)!!.owner.apply {
// If this is a property backing field, then it has no separate initializer, // If this is a property backing field, then it has no separate initializer,
// so we shouldn't convert it // so we shouldn't convert it
if (correspondingPropertySymbol == null) { if (correspondingPropertySymbol == null) {
@@ -152,7 +152,7 @@ class FakeOverrideGenerator(
if (!propertyOrFieldSymbol.isStatic) return@processPropertiesByName if (!propertyOrFieldSymbol.isStatic) return@processPropertiesByName
createFakeOverriddenIfNeeded( createFakeOverriddenIfNeeded(
firClass, irClass, isLocal, propertyOrFieldSymbol, firClass, irClass, isLocal, propertyOrFieldSymbol,
{ field, _, _ -> declarationStorage.getCachedIrFieldStaticFakeOverrideByDeclaration(field)?.symbol }, { field, _, _ -> declarationStorage.getCachedIrFieldStaticFakeOverrideSymbolByDeclaration(field) },
{ field, irParent, _, _ -> { field, irParent, _, _ ->
declarationStorage.getOrCreateIrField(field, irParent) declarationStorage.getOrCreateIrField(field, irParent)
}, },