[IR] Make IrTypeArgument a sealed interface

This commit is contained in:
Sergej Jaskiewicz
2023-01-19 19:35:41 +01:00
committed by Space Team
parent c65b689fa0
commit 1f76d39e66
25 changed files with 59 additions and 73 deletions
@@ -1174,7 +1174,6 @@ private fun makeKotlinType(
when (it) {
is IrTypeProjection -> TypeProjectionImpl(it.variance, it.type.toIrBasedKotlinType())
is IrStarProjection -> StarProjectionImpl(classDescriptor.typeConstructor.parameters[index])
else -> error(it)
}
}
@@ -92,7 +92,10 @@ abstract class IrSimpleType(kotlinType: KotlinType?) : IrTypeBase(kotlinType), S
get() = nullability == SimpleTypeNullability.MARKED_NULLABLE
}
interface IrTypeArgument : TypeArgumentMarker {
/**
* An argument for a generic parameter. Can be either [IrTypeProjection], or [IrStarProjection].
*/
sealed interface IrTypeArgument : TypeArgumentMarker {
override fun equals(other: Any?): Boolean
override fun hashCode(): Int
}
@@ -31,7 +31,6 @@ abstract class AbstractIrTypeSubstitutor(private val irBuiltIns: IrBuiltIns) : T
when (val typeArgument = getSubstitutionArgument(it)) {
is IrStarProjection -> irBuiltIns.anyNType // TODO upper bound for T
is IrTypeProjection -> typeArgument.type.run { if (type.isMarkedNullable()) makeNullable() else this }
else -> error("unknown type argument")
}
} ?: substituteType(type)
}
@@ -52,21 +51,22 @@ abstract class AbstractIrTypeSubstitutor(private val irBuiltIns: IrBuiltIns) : T
}
private fun substituteTypeArgument(typeArgument: IrTypeArgument): IrTypeArgument {
if (typeArgument is IrStarProjection) return typeArgument
require(typeArgument is IrTypeProjection)
val type = typeArgument.type
if (type is IrSimpleType) {
val classifier = type.classifier
if (classifier is IrTypeParameterSymbol) {
val newArgument = getSubstitutionArgument(classifier)
return if (newArgument is IrTypeProjection) {
makeTypeProjection(newArgument.type, typeArgument.variance)
} else newArgument
when (typeArgument) {
is IrStarProjection -> return typeArgument
is IrTypeProjection -> {
val type = typeArgument.type
if (type is IrSimpleType) {
val classifier = type.classifier
if (classifier is IrTypeParameterSymbol) {
val newArgument = getSubstitutionArgument(classifier)
return if (newArgument is IrTypeProjection) {
makeTypeProjection(newArgument.type, typeArgument.variance)
} else newArgument
}
}
return makeTypeProjection(substituteType(typeArgument.type), typeArgument.variance)
}
}
return makeTypeProjection(substituteType(typeArgument.type), typeArgument.variance)
}
}
@@ -118,4 +118,4 @@ class IrCapturedTypeSubstitutor(
}
override fun isEmptySubstitution(): Boolean = oldSubstitution.isEmpty()
}
}
@@ -61,8 +61,8 @@ fun IrType.getArrayElementType(irBuiltIns: IrBuiltIns): IrType =
argument.type
is IrStarProjection ->
irBuiltIns.anyNType
else ->
error("Unexpected array argument type: $argument")
null ->
error("Unexpected array argument type: null")
}
} else {
val classifier = this.classOrNull!!
@@ -150,7 +150,6 @@ private fun makeKotlinType(
when (it) {
is IrTypeProjection -> TypeProjectionImpl(it.variance, it.type.toKotlinType())
is IrStarProjection -> StarProjectionImpl((classifier.descriptor as ClassDescriptor).typeConstructor.parameters[index])
else -> error(it)
}
}
return classifier.descriptor.defaultType.replace(newArguments = kotlinTypeArguments).makeNullableAsSpecified(hasQuestionMark)
@@ -41,10 +41,10 @@ class DeepCopyTypeRemapper(
}
private fun remapTypeArgument(typeArgument: IrTypeArgument): IrTypeArgument =
if (typeArgument is IrTypeProjection)
makeTypeProjection(this.remapType(typeArgument.type), typeArgument.variance)
else
typeArgument
when (typeArgument) {
is IrTypeProjection -> makeTypeProjection(this.remapType(typeArgument.type), typeArgument.variance)
is IrStarProjection -> typeArgument
}
private fun IrTypeAbbreviation.remapTypeAbbreviation() =
IrTypeAbbreviationImpl(
@@ -53,4 +53,4 @@ class DeepCopyTypeRemapper(
arguments.map { remapTypeArgument(it) },
annotations
)
}
}
@@ -43,10 +43,10 @@ class IrTypeParameterRemapper(
?: this
private fun IrTypeArgument.remap() =
if (this is IrTypeProjection)
makeTypeProjection(remapType(type), variance)
else
this
when (this) {
is IrTypeProjection -> makeTypeProjection(remapType(type), variance)
is IrStarProjection -> this
}
private fun IrTypeAbbreviation.remap() =
IrTypeAbbreviationImpl(
@@ -100,10 +100,9 @@ fun IrType.substitute(substitutionMap: Map<IrTypeParameterSymbol, IrType>): IrTy
}
val newArguments = arguments.map {
if (it is IrTypeProjection) {
makeTypeProjection(it.type.substitute(substitutionMap), it.variance)
} else {
it
when (it) {
is IrTypeProjection -> makeTypeProjection(it.type.substitute(substitutionMap), it.variance)
is IrStarProjection -> it
}
}
@@ -987,7 +987,7 @@ fun IrType.remapTypeParameters(
it.type.remapTypeParameters(source, target, srcToDstParameterMap),
it.variance
)
else -> it
is IrStarProjection -> it
}
},
annotations
@@ -593,7 +593,6 @@ fun IrTypeArgument.render() =
when (this) {
is IrStarProjection -> "*"
is IrTypeProjection -> "$variance ${type.render()}"
else -> throw AssertionError("Unexpected IrTypeArgument: $this")
}
internal inline fun <T, Buffer : Appendable> Buffer.appendIterableWith(
@@ -785,8 +784,6 @@ private fun IrTypeArgument.renderTypeArgument(renderer: RenderIrElementVisitor?,
if (variance != Variance.INVARIANT) append(' ')
append(type.renderTypeWithRenderer(renderer, verboseErrorTypes))
}
else -> "IrTypeArgument[$this]"
}
private fun renderTypeAnnotations(annotations: List<IrConstructorCall>, renderer: RenderIrElementVisitor?, verboseErrorTypes: Boolean) =
@@ -42,10 +42,10 @@ class SimpleTypeRemapper(
}
private fun remapTypeArgument(typeArgument: IrTypeArgument): IrTypeArgument =
if (typeArgument is IrTypeProjection)
makeTypeProjection(this.remapType(typeArgument.type), typeArgument.variance)
else
typeArgument
when (typeArgument) {
is IrTypeProjection -> makeTypeProjection(this.remapType(typeArgument.type), typeArgument.variance)
is IrStarProjection -> typeArgument
}
private fun IrTypeAbbreviation.remapTypeAbbreviation() =
IrTypeAbbreviationImpl(