[IR] Make IrClassifierSymbol a sealed interface

This commit is contained in:
Sergej Jaskiewicz
2023-05-05 12:59:18 +02:00
committed by Space Team
parent 6f20ac4f38
commit 1a371350ea
16 changed files with 38 additions and 38 deletions
@@ -158,7 +158,7 @@ class Fir2IrDeclarationStorage(
val irClass = irClassifierSymbol.owner val irClass = irClassifierSymbol.owner
firType is ConeClassLikeType && irClass.name == firType.lookupTag.name firType is ConeClassLikeType && irClass.name == firType.lookupTag.name
} }
else -> { is IrScriptSymbol -> {
false false
} }
} }
@@ -14,6 +14,7 @@ import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable
import org.jetbrains.kotlin.ir.util.isEffectivelyExternal import org.jetbrains.kotlin.ir.util.isEffectivelyExternal
import org.jetbrains.kotlin.ir.util.unexpectedSymbolKind
import org.jetbrains.kotlin.js.backend.ast.JsNameRef import org.jetbrains.kotlin.js.backend.ast.JsNameRef
import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty import org.jetbrains.kotlin.utils.addToStdlib.ifNotEmpty
@@ -44,18 +45,13 @@ private fun IrTypeArgument.asString(): String = when (this) {
private fun IrClassifierSymbol.asString() = when (this) { private fun IrClassifierSymbol.asString() = when (this) {
is IrTypeParameterSymbol -> this.owner.name.asString() is IrTypeParameterSymbol -> this.owner.name.asString()
is IrClassSymbol -> this.owner.fqNameWhenAvailable!!.asString() is IrClassSymbol -> this.owner.fqNameWhenAvailable!!.asString()
else -> error("Unexpected kind of IrClassifierSymbol: " + javaClass.typeName) is IrScriptSymbol -> unexpectedSymbolKind<IrClassifierSymbol>()
} }
tailrec fun erase(type: IrType): IrClass? { tailrec fun erase(type: IrType): IrClass? = when (val classifier = type.classifierOrFail) {
val classifier = type.classifierOrFail is IrClassSymbol -> classifier.owner
is IrTypeParameterSymbol -> erase(classifier.owner.superTypes.first())
return when (classifier) { is IrScriptSymbol -> null
is IrClassSymbol -> classifier.owner
is IrTypeParameterSymbol -> erase(classifier.owner.superTypes.first())
is IrScriptSymbol -> null
else -> error(classifier)
}
} }
fun IrType.getClassRef(context: JsGenerationContext): JsNameRef = fun IrType.getClassRef(context: JsGenerationContext): JsNameRef =
@@ -13,6 +13,7 @@ import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl import org.jetbrains.kotlin.ir.expressions.impl.IrCallImpl
import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrScriptSymbol import org.jetbrains.kotlin.ir.symbols.IrScriptSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.*
@@ -21,6 +22,7 @@ import org.jetbrains.kotlin.ir.types.impl.IrStarProjectionImpl
import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection import org.jetbrains.kotlin.ir.types.impl.makeTypeProjection
import org.jetbrains.kotlin.ir.util.isLocal import org.jetbrains.kotlin.ir.util.isLocal
import org.jetbrains.kotlin.ir.util.render import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.ir.util.unexpectedSymbolKind
/** /**
* Perform as much type erasure as is significant for JVM signature generation. * Perform as much type erasure as is significant for JVM signature generation.
@@ -87,7 +89,7 @@ val IrType.erasedUpperBound: IrClass
is IrClassSymbol -> classifier.owner is IrClassSymbol -> classifier.owner
is IrTypeParameterSymbol -> classifier.owner.erasedUpperBound is IrTypeParameterSymbol -> classifier.owner.erasedUpperBound
is IrScriptSymbol -> classifier.owner.targetClass!!.owner is IrScriptSymbol -> classifier.owner.targetClass!!.owner
else -> if (this is IrErrorType) symbol.owner else error(render()) null -> if (this is IrErrorType) symbol.owner else error(render())
} }
/** /**
@@ -139,7 +141,7 @@ fun IrType.eraseToScope(visibleTypeParameters: Set<IrTypeParameter>): IrType {
this this
else else
upperBound.mergeNullability(this) upperBound.mergeNullability(this)
else -> error("unknown IrType classifier kind: ${classifier.owner.render()}") is IrScriptSymbol -> classifier.unexpectedSymbolKind<IrClassifierSymbol>()
} }
} }
@@ -1211,7 +1211,6 @@ private fun makeKotlinType(
KotlinTypeFactory.EMPTY_REFINED_TYPE_FACTORY KotlinTypeFactory.EMPTY_REFINED_TYPE_FACTORY
).makeNullableAsSpecified(hasQuestionMark) ).makeNullableAsSpecified(hasQuestionMark)
} }
else -> error("unknown classifier kind $classifier")
} }
/* When IR-based descriptors are used from Psi2Ir, symbols may be unbound, thus we may need to resort to real descriptors. */ /* When IR-based descriptors are used from Psi2Ir, symbols may be unbound, thus we may need to resort to real descriptors. */
@@ -69,7 +69,7 @@ interface IrEnumEntrySymbol : IrBindableSymbol<ClassDescriptor, IrEnumEntry>
interface IrFieldSymbol : IrBindableSymbol<PropertyDescriptor, IrField> interface IrFieldSymbol : IrBindableSymbol<PropertyDescriptor, IrField>
interface IrClassifierSymbol : IrSymbol, TypeConstructorMarker { sealed interface IrClassifierSymbol : IrSymbol, TypeConstructorMarker {
@ObsoleteDescriptorBasedAPI @ObsoleteDescriptorBasedAPI
override val descriptor: ClassifierDescriptor override val descriptor: ClassifierDescriptor
} }
@@ -14,7 +14,7 @@ import org.jetbrains.kotlin.types.AbstractTypeChecker
fun IrClassifierSymbol.superTypes(): List<IrType> = when (this) { fun IrClassifierSymbol.superTypes(): List<IrType> = when (this) {
is IrClassSymbol -> owner.superTypes is IrClassSymbol -> owner.superTypes
is IrTypeParameterSymbol -> owner.superTypes is IrTypeParameterSymbol -> owner.superTypes
else -> emptyList() is IrScriptSymbol -> emptyList()
} }
fun IrClassifierSymbol.isSubtypeOfClass(superClass: IrClassSymbol): Boolean = fun IrClassifierSymbol.isSubtypeOfClass(superClass: IrClassSymbol): Boolean =
@@ -44,7 +44,6 @@ fun IrType.isNullable(): Boolean =
SimpleTypeNullability.DEFINITELY_NOT_NULL -> false SimpleTypeNullability.DEFINITELY_NOT_NULL -> false
} }
is IrScriptSymbol -> nullability == SimpleTypeNullability.MARKED_NULLABLE is IrScriptSymbol -> nullability == SimpleTypeNullability.MARKED_NULLABLE
else -> error("Unsupported classifier: $classifier")
} }
is IrDynamicType -> true is IrDynamicType -> true
is IrErrorType -> this.isMarkedNullable is IrErrorType -> this.isMarkedNullable
@@ -163,7 +163,7 @@ val IrClassifierSymbol.defaultType: IrType
get() = when (this) { get() = when (this) {
is IrClassSymbol -> owner.defaultType is IrClassSymbol -> owner.defaultType
is IrTypeParameterSymbol -> owner.defaultType is IrTypeParameterSymbol -> owner.defaultType
else -> error("Unexpected classifier symbol type $this") is IrScriptSymbol -> unexpectedSymbolKind<IrClassifierSymbol>()
} }
val IrTypeParameter.defaultType: IrType val IrTypeParameter.defaultType: IrType
@@ -231,7 +231,6 @@ open class DeepCopySymbolRemapper(
is IrClassSymbol -> classes.getReferenced(symbol) is IrClassSymbol -> classes.getReferenced(symbol)
is IrScriptSymbol -> scripts.getReferenced(symbol) is IrScriptSymbol -> scripts.getReferenced(symbol)
is IrTypeParameterSymbol -> typeParameters.getReferenced(symbol) is IrTypeParameterSymbol -> typeParameters.getReferenced(symbol)
else -> throw IllegalArgumentException("Unexpected symbol $symbol")
} }
override fun getReferencedTypeAlias(symbol: IrTypeAliasSymbol): IrTypeAliasSymbol = typeAliases.getReferenced(symbol) override fun getReferencedTypeAlias(symbol: IrTypeAliasSymbol): IrTypeAliasSymbol = typeAliases.getReferenced(symbol)
@@ -1422,3 +1422,7 @@ fun IrAttributeContainer.extractRelatedDeclaration(): IrDeclaration? {
else -> null else -> null
} }
} }
inline fun <reified Symbol : IrSymbol> IrSymbol.unexpectedSymbolKind(): Nothing {
throw IllegalArgumentException("Unexpected kind of ${Symbol::class.java.typeName}: $this")
}
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.ir.IrBuiltIns
import org.jetbrains.kotlin.ir.IrElement import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrScriptSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
@@ -133,6 +134,7 @@ open class IrMangleComputer(
when (val classifier = type.classifier) { when (val classifier = type.classifier) {
is IrClassSymbol -> with(copy(MangleMode.FQNAME)) { classifier.owner.visit() } is IrClassSymbol -> with(copy(MangleMode.FQNAME)) { classifier.owner.visit() }
is IrTypeParameterSymbol -> tBuilder.mangleTypeParameterReference(classifier) is IrTypeParameterSymbol -> tBuilder.mangleTypeParameterReference(classifier)
is IrScriptSymbol -> {}
} }
mangleTypeArguments(tBuilder, type, null) mangleTypeArguments(tBuilder, type, null)
@@ -16,6 +16,8 @@ import org.jetbrains.kotlin.ir.declarations.IrProperty
import org.jetbrains.kotlin.ir.declarations.inlineClassRepresentation import org.jetbrains.kotlin.ir.declarations.inlineClassRepresentation
import org.jetbrains.kotlin.ir.declarations.isSingleFieldValueClass import org.jetbrains.kotlin.ir.declarations.isSingleFieldValueClass
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrScriptSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.IrType import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.classifierOrFail import org.jetbrains.kotlin.ir.types.classifierOrFail
@@ -284,13 +286,10 @@ internal object IrTypeInlineClassesSupport : InlineClassesSupport<IrClass, IrTyp
} }
} }
override fun computeFullErasure(type: IrType): Sequence<IrClass> { override fun computeFullErasure(type: IrType): Sequence<IrClass> = when (val classifier = type.classifierOrFail) {
val classifier = type.classifierOrFail is IrClassSymbol -> sequenceOf(classifier.owner)
return when (classifier) { is IrTypeParameterSymbol -> classifier.owner.superTypes.asSequence().flatMap { computeFullErasure(it) }
is IrClassSymbol -> sequenceOf(classifier.owner) is IrScriptSymbol -> classifier.unexpectedSymbolKind<IrClassifierSymbol>()
is IrTypeParameterSymbol -> classifier.owner.superTypes.asSequence().flatMap { computeFullErasure(it) }
else -> error(classifier)
}
} }
override fun hasInlineModifier(clazz: IrClass): Boolean = clazz.isSingleFieldValueClass override fun hasInlineModifier(clazz: IrClass): Boolean = clazz.isSingleFieldValueClass
@@ -119,7 +119,7 @@ internal class ExpectToActualDefaultValueCopier(private val irModule: IrModuleFr
override fun getReferencedClassifier(symbol: IrClassifierSymbol): IrClassifierSymbol = when (symbol) { override fun getReferencedClassifier(symbol: IrClassifierSymbol): IrClassifierSymbol = when (symbol) {
is IrClassSymbol -> getReferencedClass(symbol) is IrClassSymbol -> getReferencedClass(symbol)
is IrTypeParameterSymbol -> remapExpectTypeParameter(symbol).symbol is IrTypeParameterSymbol -> remapExpectTypeParameter(symbol).symbol
else -> error("Unexpected symbol $symbol ${symbol.descriptor}") is IrScriptSymbol -> symbol.unexpectedSymbolKind<IrClassifierSymbol>()
} }
override fun getReferencedConstructor(symbol: IrConstructorSymbol) = override fun getReferencedConstructor(symbol: IrConstructorSymbol) =
@@ -15,6 +15,8 @@ import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrScriptSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
@@ -65,7 +67,7 @@ internal class KTypeGenerator(
// Leave upper bounds of non-reified type parameters as is, even if they are reified themselves. // Leave upper bounds of non-reified type parameters as is, even if they are reified themselves.
irKTypeParameter(classifier.owner, leaveReifiedForLater = false, seenTypeParameters = seenTypeParameters) irKTypeParameter(classifier.owner, leaveReifiedForLater = false, seenTypeParameters = seenTypeParameters)
} }
else -> TODO("Unexpected classifier: $classifier") is IrScriptSymbol -> classifier.unexpectedSymbolKind<IrClassifierSymbol>()
} }
return irKTypeImpl( return irKTypeImpl(
@@ -14,6 +14,8 @@ import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.expressions.IrTypeOperator import org.jetbrains.kotlin.ir.expressions.IrTypeOperator
import org.jetbrains.kotlin.ir.expressions.IrTypeOperatorCall import org.jetbrains.kotlin.ir.expressions.IrTypeOperatorCall
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrScriptSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.util.*
@@ -36,7 +38,7 @@ internal fun IrType.erasure(): IrType {
// In the second case, there is only a single supertype. // In the second case, there is only a single supertype.
classifier.owner.superTypes.first().erasure() classifier.owner.superTypes.first().erasure()
} }
else -> TODO(classifier.toString()) is IrScriptSymbol -> classifier.unexpectedSymbolKind<IrClassifierSymbol>()
} }
return upperBound.mergeNullability(this) return upperBound.mergeNullability(this)
@@ -27,10 +27,7 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrValueParameterImpl
import org.jetbrains.kotlin.ir.declarations.impl.IrVariableImpl import org.jetbrains.kotlin.ir.declarations.impl.IrVariableImpl
import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.* import org.jetbrains.kotlin.ir.expressions.impl.*
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.*
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.symbols.impl.IrFieldSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrFieldSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrValueParameterSymbolImpl
import org.jetbrains.kotlin.ir.symbols.impl.IrVariableSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrVariableSymbolImpl
@@ -172,8 +169,7 @@ fun IrFunctionAccessExpression.addArguments(args: Map<IrValueParameter, IrExpres
fun IrType.substitute(map: Map<IrTypeParameterSymbol, IrType>): IrType { fun IrType.substitute(map: Map<IrTypeParameterSymbol, IrType>): IrType {
if (this !is IrSimpleType) return this if (this !is IrSimpleType) return this
val classifier = this.classifier return when (val classifier = this.classifier) {
return when (classifier) {
is IrTypeParameterSymbol -> is IrTypeParameterSymbol ->
map[classifier]?.mergeNullability(this) ?: this map[classifier]?.mergeNullability(this) ?: this
is IrClassSymbol -> if (this.arguments.isEmpty()) { is IrClassSymbol -> if (this.arguments.isEmpty()) {
@@ -187,7 +183,7 @@ fun IrType.substitute(map: Map<IrTypeParameterSymbol, IrType>): IrType {
} }
IrSimpleTypeImpl(classifier, nullability, newArguments, annotations) IrSimpleTypeImpl(classifier, nullability, newArguments, annotations)
} }
else -> error(classifier) is IrScriptSymbol -> classifier.unexpectedSymbolKind<IrClassifierSymbol>()
} }
} }
@@ -248,11 +248,11 @@ fun findSerializerConstructorForTypeArgumentsSerializers(serializer: IrClass): I
}?.symbol }?.symbol
} }
fun IrType.classOrUpperBound(): IrClassSymbol? = when(val cls = classifierOrNull) { fun IrType.classOrUpperBound(): IrClassSymbol? = when (val cls = classifierOrNull) {
is IrClassSymbol -> cls is IrClassSymbol -> cls
is IrScriptSymbol -> cls.owner.targetClass is IrScriptSymbol -> cls.owner.targetClass
is IrTypeParameterSymbol -> cls.owner.representativeUpperBound.classOrUpperBound() is IrTypeParameterSymbol -> cls.owner.representativeUpperBound.classOrUpperBound()
else -> null null -> null
} }
/** /**