[FIR2IR] Manage field symbols in declaration storage instead of declaration generator

This is needed to be able to implement creation of unbound symbols
  for references of corresponding declarations (KT-62856)
This commit is contained in:
Dmitriy Novozhilov
2023-11-03 15:24:40 +02:00
committed by Space Team
parent aadc9ce6d5
commit cd0baef087
3 changed files with 31 additions and 46 deletions
@@ -578,7 +578,7 @@ class Fir2IrDeclarationStorage(
} }
val backingFieldSymbol = runIf(property.delegate != null || property.hasBackingField) { val backingFieldSymbol = runIf(property.delegate != null || property.hasBackingField) {
createFieldSymbol() createFieldSymbol(signature = null)
} }
return PropertySymbols(propertySymbol, getterSymbol, setterSymbol, backingFieldSymbol) return PropertySymbols(propertySymbol, getterSymbol, setterSymbol, backingFieldSymbol)
@@ -798,8 +798,12 @@ class Fir2IrDeclarationStorage(
type: ConeKotlinType = field.returnTypeRef.coneType, type: ConeKotlinType = field.returnTypeRef.coneType,
origin: IrDeclarationOrigin = IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB origin: IrDeclarationOrigin = IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB
): IrField { ): IrField {
val irField = callablesGenerator.createIrField(field, irParent, type, origin)
val containingClassLookupTag = (irParent as IrClass?)?.classId?.toLookupTag() val containingClassLookupTag = (irParent as IrClass?)?.classId?.toLookupTag()
val signature = signatureComposer.composeSignature(field, containingClassLookupTag)
val symbol = createFieldSymbol(signature)
val irField = callablesGenerator.createIrField(field, irParent, symbol, type, origin)
val staticFakeOverrideKey = getFieldStaticFakeOverrideKey(field, containingClassLookupTag) val staticFakeOverrideKey = getFieldStaticFakeOverrideKey(field, containingClassLookupTag)
if (staticFakeOverrideKey == null) { if (staticFakeOverrideKey == null) {
fieldCache[field] = irField.symbol fieldCache[field] = irField.symbol
@@ -809,8 +813,11 @@ class Fir2IrDeclarationStorage(
return irField return irField
} }
private fun createFieldSymbol(): IrFieldSymbol { private fun createFieldSymbol(signature: IdSignature?): IrFieldSymbol {
return IrFieldSymbolImpl() return when {
signature != null -> symbolTable.referenceField(signature)
else -> IrFieldSymbolImpl()
}
} }
// This function returns null if this field/ownerClassId combination does not describe static fake override // This function returns null if this field/ownerClassId combination does not describe static fake override
@@ -549,29 +549,23 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
internal fun createIrField( internal fun createIrField(
field: FirField, field: FirField,
irParent: IrDeclarationParent?, irParent: IrDeclarationParent?,
symbol: IrFieldSymbol,
type: ConeKotlinType = field.returnTypeRef.coneType, type: ConeKotlinType = field.returnTypeRef.coneType,
origin: IrDeclarationOrigin = IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB origin: IrDeclarationOrigin = IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB
): IrField = convertCatching(field) { ): IrField = convertCatching(field) {
val irType = type.toIrType() val irType = type.toIrType()
val classId = (irParent as? IrClass)?.classId
val containingClassLookupTag = classId?.toLookupTag()
val signature = signatureComposer.composeSignature(field, containingClassLookupTag)
val parentIsExternal = irParent.isExternalParent() val parentIsExternal = irParent.isExternalParent()
if (field is FirJavaField && field.isStatic && field.isFinal && parentIsExternal && signature != null) { if (field is FirJavaField && field.isStatic && field.isFinal && parentIsExternal) {
// We are going to create IR for Java static final fields lazily because they can refer to some Kotlin const. // We are going to create IR for Java static final fields lazily because they can refer to some Kotlin const.
// This way we delay const evaluation of Java fields until IR tree is fully built, and we can run IR interpreter. // This way we delay const evaluation of Java fields until IR tree is fully built, and we can run IR interpreter.
return lazyDeclarationsGenerator.createIrLazyField(field, signature, irParent!!, origin).apply { return lazyDeclarationsGenerator.createIrLazyField(field, symbol, irParent!!, origin).apply {
setParent(irParent) setParent(irParent)
addDeclarationToParent(this, irParent) addDeclarationToParent(this, irParent)
} }
} }
return field.convertWithOffsets { startOffset, endOffset -> return field.convertWithOffsets { startOffset, endOffset ->
if (signature != null) {
symbolTable.declareField(
signature, symbolFactory = { IrFieldPublicSymbolImpl(signature) }
) { symbol ->
irFactory.createField( irFactory.createField(
startOffset = startOffset, startOffset = startOffset,
endOffset = endOffset, endOffset = endOffset,
@@ -583,22 +577,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
isFinal = field.modality == Modality.FINAL, isFinal = field.modality == Modality.FINAL,
isStatic = field.isStatic, isStatic = field.isStatic,
isExternal = false isExternal = false
) ).apply {
}
} else {
irFactory.createField(
startOffset = startOffset,
endOffset = endOffset,
origin = origin,
name = field.name,
visibility = components.visibilityConverter.convertToDescriptorVisibility(field.visibility),
symbol = IrFieldSymbolImpl(),
type = irType,
isFinal = field.modality == Modality.FINAL,
isStatic = field.isStatic,
isExternal = false
)
}.apply {
metadata = FirMetadataSource.Field(field) metadata = FirMetadataSource.Field(field)
val initializer = field.unwrapFakeOverrides().initializer val initializer = field.unwrapFakeOverrides().initializer
if (initializer is FirConstExpression<*>) { if (initializer is FirConstExpression<*>) {
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.fir.lazy.*
import org.jetbrains.kotlin.fir.resolve.providers.firProvider import org.jetbrains.kotlin.fir.resolve.providers.firProvider
import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol
import org.jetbrains.kotlin.ir.symbols.IrFieldSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrFieldPublicSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrFieldPublicSymbolImpl
import org.jetbrains.kotlin.ir.util.IdSignature import org.jetbrains.kotlin.ir.util.IdSignature
@@ -95,16 +96,14 @@ class Fir2IrLazyDeclarationsGenerator(val components: Fir2IrComponents) : Fir2Ir
fun createIrLazyField( fun createIrLazyField(
fir: FirField, fir: FirField,
signature: IdSignature, symbol: IrFieldSymbol,
lazyParent: IrDeclarationParent, lazyParent: IrDeclarationParent,
declarationOrigin: IrDeclarationOrigin declarationOrigin: IrDeclarationOrigin
): IrField { ): IrField {
return fir.convertWithOffsets { startOffset, endOffset -> return fir.convertWithOffsets { startOffset, endOffset ->
symbolTable.declareField(signature, symbolFactory = { IrFieldPublicSymbolImpl(signature) }) { symbol ->
Fir2IrLazyField( Fir2IrLazyField(
components, startOffset, endOffset, declarationOrigin, fir, (lazyParent as? Fir2IrLazyClass)?.fir, symbol components, startOffset, endOffset, declarationOrigin, fir, (lazyParent as? Fir2IrLazyClass)?.fir, symbol
) )
} }
} }
} }
}