[FIR2IR] Part 3. Don't create type parameter symbols with signature

^KT-64990
This commit is contained in:
Dmitriy Novozhilov
2024-02-21 10:25:20 +02:00
committed by Space Team
parent d0c9353d41
commit e340286606
9 changed files with 19 additions and 28 deletions
@@ -78,15 +78,15 @@ class Fir2IrClassifierStorage(
// ------------------------------------ type parameters ------------------------------------
// Note: declareTypeParameters should be called before!
internal fun preCacheTypeParameters(owner: FirTypeParameterRefsOwner, irOwnerSymbol: IrSymbol) {
internal fun preCacheTypeParameters(owner: FirTypeParameterRefsOwner) {
for ((index, typeParameter) in owner.typeParameters.withIndex()) {
val original = typeParameter.symbol.fir
getCachedIrTypeParameter(original)
?: createAndCacheIrTypeParameter(original, index, irOwnerSymbol)
?: createAndCacheIrTypeParameter(original, index)
if (owner is FirProperty && owner.isVar) {
val context = ConversionTypeOrigin.SETTER
getCachedIrTypeParameter(original, context)
?: createAndCacheIrTypeParameter(original, index, irOwnerSymbol, context)
?: createAndCacheIrTypeParameter(original, index, context)
}
}
}
@@ -94,11 +94,10 @@ class Fir2IrClassifierStorage(
internal fun getIrTypeParameter(
typeParameter: FirTypeParameter,
index: Int,
ownerSymbol: IrSymbol,
typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT
): IrTypeParameter {
getCachedIrTypeParameter(typeParameter, typeOrigin)?.let { return it }
val irTypeParameter = createAndCacheIrTypeParameter(typeParameter, index, ownerSymbol, typeOrigin)
val irTypeParameter = createAndCacheIrTypeParameter(typeParameter, index, typeOrigin)
classifiersGenerator.initializeTypeParameterBounds(typeParameter, irTypeParameter)
return irTypeParameter
}
@@ -106,10 +105,9 @@ class Fir2IrClassifierStorage(
private fun createAndCacheIrTypeParameter(
typeParameter: FirTypeParameter,
index: Int,
ownerSymbol: IrSymbol,
typeOrigin: ConversionTypeOrigin = ConversionTypeOrigin.DEFAULT,
): IrTypeParameter {
val symbol = createTypeParameterSymbol(ownerSymbol, index)
val symbol = IrTypeParameterSymbolImpl()
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) {
@@ -120,13 +118,6 @@ 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
@@ -165,7 +156,7 @@ class Fir2IrClassifierStorage(
val isSetter = firTypeParameterOwner is FirPropertyAccessor && firTypeParameterOwner.isSetter
val conversionTypeOrigin = if (isSetter) ConversionTypeOrigin.SETTER else ConversionTypeOrigin.DEFAULT
return createAndCacheIrTypeParameter(firTypeParameter, index, IrTypeParameterSymbolImpl(), conversionTypeOrigin).also {
return createAndCacheIrTypeParameter(firTypeParameter, index, conversionTypeOrigin).also {
classifiersGenerator.initializeTypeParameterBounds(firTypeParameter, it)
}.symbol
}
@@ -111,7 +111,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
if (isLambda) ((function as FirAnonymousFunction).typeRef as? FirResolvedTypeRef)?.type?.isSuspendOrKSuspendFunctionType(session) == true
else function.isSuspend
val created = function.convertWithOffsets { startOffset, endOffset ->
classifierStorage.preCacheTypeParameters(function, symbol)
classifierStorage.preCacheTypeParameters(function)
irFactory.createSimpleFunction(
startOffset = if (updatedOrigin == IrDeclarationOrigin.DELEGATED_MEMBER) SYNTHETIC_OFFSET else startOffset,
endOffset = if (updatedOrigin == IrDeclarationOrigin.DELEGATED_MEMBER) SYNTHETIC_OFFSET else endOffset,
@@ -186,7 +186,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
}
val visibility = if (irParent.isAnonymousObject) Visibilities.Public else constructor.visibility
return constructor.convertWithOffsets { startOffset, endOffset ->
classifierStorage.preCacheTypeParameters(constructor, symbol)
classifierStorage.preCacheTypeParameters(constructor)
irFactory.createConstructor(
startOffset = startOffset,
endOffset = endOffset,
@@ -245,7 +245,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
return lazyDeclarationsGenerator.createIrLazyProperty(property, irParent!!, symbols, origin)
}
return property.convertWithOffsets { startOffset, endOffset ->
classifierStorage.preCacheTypeParameters(property, symbols.propertySymbol)
classifierStorage.preCacheTypeParameters(property)
irFactory.createProperty(
startOffset = startOffset,
endOffset = endOffset,
@@ -275,7 +275,7 @@ class Fir2IrCallableDeclarationsGenerator(val components: Fir2IrComponents) : Fi
if (delegate != null || property.hasBackingField) {
val backingField = if (delegate != null) {
((delegate as? FirQualifiedAccessExpression)?.calleeReference?.toResolvedBaseSymbol()?.fir as? FirTypeParameterRefsOwner)?.let {
classifierStorage.preCacheTypeParameters(it, symbol)
classifierStorage.preCacheTypeParameters(it)
}
createBackingField(
this,
@@ -121,7 +121,7 @@ class Fir2IrClassifiersGenerator(val components: Fir2IrComponents) : Fir2IrCompo
// `irClass` is a source class and definitely is not a lazy class
@OptIn(UnsafeDuringIrConstructionAPI::class)
private fun IrClass.declareTypeParameters(klass: FirClass) {
classifierStorage.preCacheTypeParameters(klass, symbol)
classifierStorage.preCacheTypeParameters(klass)
setTypeParameters(this, klass)
if (klass is FirRegularClass) {
val fieldsForContextReceiversOfCurrentClass = classifierStorage.getFieldsWithContextReceiversForClass(this, klass)
@@ -285,7 +285,7 @@ class Fir2IrClassifiersGenerator(val components: Fir2IrComponents) : Fir2IrCompo
parent: IrDeclarationParent,
symbol: IrTypeAliasSymbol,
): IrTypeAlias = typeAlias.convertWithOffsets { startOffset, endOffset ->
classifierStorage.preCacheTypeParameters(typeAlias, symbol)
classifierStorage.preCacheTypeParameters(typeAlias)
irFactory.createTypeAlias(
startOffset = startOffset,
endOffset = endOffset,
@@ -389,7 +389,7 @@ class Fir2IrClassifiersGenerator(val components: Fir2IrComponents) : Fir2IrCompo
) {
irOwner.typeParameters = owner.typeParameters.mapIndexedNotNull { index, typeParameter ->
if (typeParameter !is FirTypeParameter) return@mapIndexedNotNull null
classifierStorage.getIrTypeParameter(typeParameter, index, irOwner.symbol, typeOrigin).apply {
classifierStorage.getIrTypeParameter(typeParameter, index, typeOrigin).apply {
parent = irOwner
if (superTypes.isEmpty()) {
superTypes = typeParameter.bounds.map { it.toIrType(typeOrigin) }
@@ -44,7 +44,7 @@ class Fir2IrLazyClass(
IrMaybeDeserializedClass, DeserializableClass, Fir2IrComponents by components {
init {
symbol.bind(this)
classifierStorage.preCacheTypeParameters(fir, symbol)
classifierStorage.preCacheTypeParameters(fir)
}
override var annotations: List<IrConstructorCall> by createLazyAnnotations()
@@ -41,7 +41,7 @@ class Fir2IrLazyConstructor(
Fir2IrComponents by components {
init {
symbol.bind(this)
classifierStorage.preCacheTypeParameters(fir, symbol)
classifierStorage.preCacheTypeParameters(fir)
}
override var annotations: List<IrConstructorCall> by createLazyAnnotations()
@@ -47,7 +47,7 @@ class Fir2IrLazyProperty(
init {
symbol.bind(this)
classifierStorage.preCacheTypeParameters(fir, symbol)
classifierStorage.preCacheTypeParameters(fir)
}
override var annotations: List<IrConstructorCall> by createLazyAnnotations()
@@ -40,7 +40,7 @@ class Fir2IrLazySimpleFunction(
) : AbstractFir2IrLazyFunction<FirSimpleFunction>(components, startOffset, endOffset, origin, symbol, parent, isFakeOverride) {
init {
symbol.bind(this)
classifierStorage.preCacheTypeParameters(fir, symbol)
classifierStorage.preCacheTypeParameters(fir)
}
override var annotations: List<IrConstructorCall> by createLazyAnnotations()
@@ -34,7 +34,7 @@ class Fir2IrLazyTypeAlias(
) : IrTypeAlias(), AbstractFir2IrLazyDeclaration<FirTypeAlias>, Fir2IrTypeParametersContainer, Fir2IrComponents by components {
init {
symbol.bind(this)
classifierStorage.preCacheTypeParameters(fir, symbol)
classifierStorage.preCacheTypeParameters(fir)
}
override var annotations: List<IrConstructorCall> by createLazyAnnotations()
@@ -17,7 +17,7 @@ interface Fir2IrTypeParametersContainer : IrTypeParametersContainer, Fir2IrCompo
fun prepareTypeParameters() {
typeParameters = fir.typeParameters.mapIndexedNotNull { index, typeParameter ->
if (typeParameter !is FirTypeParameter) return@mapIndexedNotNull null
classifierStorage.getIrTypeParameter(typeParameter, index, symbol).apply {
classifierStorage.getIrTypeParameter(typeParameter, index).apply {
parent = this@Fir2IrTypeParametersContainer
if (superTypes.isEmpty()) {
superTypes = typeParameter.bounds.map { it.toIrType(typeConverter) }