From 78259e3ee096be73d47c8079381c8d25cfc228c4 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Fri, 3 Nov 2023 15:46:00 +0200 Subject: [PATCH] [FIR2IR] Manage type parameter 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) --- .../fir/backend/Fir2IrClassifierStorage.kt | 11 +++- .../generators/Fir2IrClassifiersGenerator.kt | 63 ++++--------------- 2 files changed, 22 insertions(+), 52 deletions(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrClassifierStorage.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrClassifierStorage.kt index 25be17cfdd3..c62d9fc6dfd 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrClassifierStorage.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrClassifierStorage.kt @@ -21,6 +21,7 @@ import org.jetbrains.kotlin.fir.symbols.impl.FirTypeParameterSymbol import org.jetbrains.kotlin.fir.types.FirTypeRef import org.jetbrains.kotlin.ir.UNDEFINED_OFFSET import org.jetbrains.kotlin.ir.declarations.* +import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol import org.jetbrains.kotlin.ir.symbols.IrSymbol import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.symbols.impl.IrFieldSymbolImpl @@ -93,7 +94,8 @@ class Fir2IrClassifierStorage( ownerSymbol: IrSymbol, typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT, ): IrTypeParameter { - val irTypeParameter = classifiersGenerator.createIrTypeParameterWithoutBounds(typeParameter, index, ownerSymbol) + val symbol = createTypeParameterSymbol(ownerSymbol, index) + val irTypeParameter = classifiersGenerator.createIrTypeParameterWithoutBounds(typeParameter, index, symbol) // Cache the type parameter BEFORE processing its bounds/supertypes, to properly handle recursive type bounds. if (typeOrigin.forSetter) { typeParameterCacheForSetter[typeParameter] = irTypeParameter @@ -103,6 +105,13 @@ class Fir2IrClassifierStorage( return irTypeParameter } + private fun createTypeParameterSymbol(ownerSymbol: IrSymbol, index: Int): IrTypeParameterSymbol { + if (ownerSymbol !is IrClassifierSymbol) return IrTypeParameterSymbolImpl() + val signature = signatureComposer.composeTypeParameterSignature(index, ownerSymbol.signature) + ?: return IrTypeParameterSymbolImpl() + return symbolTable.referenceTypeParameter(signature) + } + internal fun getCachedIrTypeParameter( typeParameter: FirTypeParameter, typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrClassifiersGenerator.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrClassifiersGenerator.kt index f1c384d41d7..c56530be470 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrClassifiersGenerator.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/generators/Fir2IrClassifiersGenerator.kt @@ -36,60 +36,21 @@ class Fir2IrClassifiersGenerator(val components: Fir2IrComponents) : Fir2IrCompo fun createIrTypeParameterWithoutBounds( typeParameter: FirTypeParameter, index: Int, - ownerSymbol: IrSymbol, + symbol: IrTypeParameterSymbol ): IrTypeParameter { require(index >= 0) val origin = typeParameter.computeIrOrigin() - val irTypeParameter = with(typeParameter) { - convertWithOffsets { startOffset, endOffset -> - signatureComposer.composeTypeParameterSignature( - index, ownerSymbol.signature - )?.let { signature -> - if (ownerSymbol is IrClassifierSymbol) { - symbolTable.declareGlobalTypeParameter( - signature, - symbolFactory = { IrTypeParameterPublicSymbolImpl(signature) } - ) { symbol -> - irFactory.createTypeParameter( - startOffset = startOffset, - endOffset = endOffset, - origin = origin, - name = name, - symbol = symbol, - variance = variance, - index = index, - isReified = isReified, - ) - } - } else { - symbolTable.declareScopedTypeParameter( - signature, - symbolFactory = { IrTypeParameterPublicSymbolImpl(signature) } - ) { symbol -> - irFactory.createTypeParameter( - startOffset = startOffset, - endOffset = endOffset, - origin = origin, - name = name, - symbol = symbol, - variance = variance, - index = index, - isReified = isReified, - ) - } - - } - } ?: irFactory.createTypeParameter( - startOffset = startOffset, - endOffset = endOffset, - origin = origin, - name = name, - symbol = IrTypeParameterSymbolImpl(), - variance = variance, - index = index, - isReified = isReified, - ) - } + val irTypeParameter = typeParameter.convertWithOffsets { startOffset, endOffset -> + irFactory.createTypeParameter( + startOffset = startOffset, + endOffset = endOffset, + origin = origin, + name = typeParameter.name, + symbol = symbol, + variance = typeParameter.variance, + index = index, + isReified = typeParameter.isReified, + ) } annotationGenerator.generate(irTypeParameter, typeParameter) return irTypeParameter