[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)
This commit is contained in:
Dmitriy Novozhilov
2023-11-03 15:46:00 +02:00
committed by Space Team
parent 36c0ed3c9e
commit 78259e3ee0
2 changed files with 22 additions and 52 deletions
@@ -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
@@ -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