IR: add IrClass.getInlineClassRepresentation, serialize/deserialize it

The change in FirDeclarationUtil is needed because in case of unsigned
types loaded from the standard library, the primary constructor for some
reason is not the first, but the second in the list of constructors.
This commit is contained in:
Alexander Udalov
2021-03-30 18:16:37 +02:00
parent 4c7f207309
commit 54befa769f
33 changed files with 950 additions and 59 deletions
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.fir.backend
import com.intellij.psi.PsiCompiledElement
import org.jetbrains.kotlin.KtNodeTypes
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.InlineClassRepresentation
import org.jetbrains.kotlin.descriptors.Modality
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.fir.*
@@ -497,3 +498,14 @@ fun Fir2IrComponents.createTemporaryVariableForSafeCallConstruction(
conversionScope: Fir2IrConversionScope
): Pair<IrVariable, IrValueSymbol> =
createTemporaryVariable(receiverExpression, conversionScope, "safe_receiver")
// TODO: implement inlineClassRepresentation in FirRegularClass instead.
fun Fir2IrComponents.computeInlineClassRepresentation(klass: FirRegularClass): InlineClassRepresentation<IrSimpleType>? {
if (!klass.isInline) return null
val parameter = klass.getInlineClassUnderlyingParameter() ?: error("Inline class has no underlying parameter: ${klass.render()}")
val underlyingType = parameter.returnTypeRef.toIrType(typeConverter)
return InlineClassRepresentation(
parameter.name,
underlyingType as? IrSimpleType ?: error("Inline class underlying type is not a simple type: ${klass.render()}")
)
}
@@ -127,6 +127,10 @@ class Fir2IrClassifierStorage(
superTypes = klass.superTypeRefs.map { superTypeRef -> superTypeRef.toIrType() }
}
private fun IrClass.declareInlineClassRepresentation(klass: FirRegularClass) {
inlineClassRepresentation = computeInlineClassRepresentation(klass)
}
private fun IrClass.declareSupertypesAndTypeParameters(klass: FirClass<*>): IrClass {
declareTypeParameters(klass)
declareSupertypes(klass)
@@ -175,6 +179,7 @@ class Fir2IrClassifierStorage(
irClass.declareTypeParameters(regularClass)
irClass.setThisReceiver(regularClass.typeParameters)
irClass.declareSupertypes(regularClass)
irClass.declareInlineClassRepresentation(regularClass)
return irClass
}
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.fir.lazy
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.fir.backend.Fir2IrComponents
import org.jetbrains.kotlin.fir.backend.computeInlineClassRepresentation
import org.jetbrains.kotlin.fir.backend.declareThisReceiverParameter
import org.jetbrains.kotlin.fir.backend.toIrType
import org.jetbrains.kotlin.fir.declarations.*
@@ -21,6 +22,7 @@ import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.lazy.lazyVar
import org.jetbrains.kotlin.ir.expressions.IrConstructorCall
import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
import org.jetbrains.kotlin.ir.util.isFakeOverride
@@ -117,6 +119,12 @@ class Fir2IrLazyClass(
receiver
}
override var inlineClassRepresentation: InlineClassRepresentation<IrSimpleType>?
get() = computeInlineClassRepresentation(fir)
set(_) {
error("Mutating Fir2Ir lazy elements is not possible")
}
private val fakeOverridesByName = mutableMapOf<Name, Collection<IrDeclaration>>()
fun getFakeOverridesByName(name: Name): Collection<IrDeclaration> = fakeOverridesByName.getOrPut(name) {