IR use abstract classes in IrType hierarchy

This commit is contained in:
Dmitry Petrov
2022-02-10 16:30:16 +03:00
committed by Space
parent 54299e83e8
commit 53210770a6
5 changed files with 26 additions and 20 deletions
@@ -8,10 +8,12 @@ package org.jetbrains.kotlin.ir.types
import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer import org.jetbrains.kotlin.ir.declarations.IrAnnotationContainer
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.symbols.IrTypeAliasSymbol import org.jetbrains.kotlin.ir.symbols.IrTypeAliasSymbol
import org.jetbrains.kotlin.ir.types.impl.IrTypeBase
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.model.* import org.jetbrains.kotlin.types.model.*
interface IrType : KotlinTypeMarker, IrAnnotationContainer { abstract class IrType : KotlinTypeMarker, IrAnnotationContainer {
/** /**
* @return true if this type is equal to [other] symbolically. Note that this is NOT EQUIVALENT to the full type checking algorithm * @return true if this type is equal to [other] symbolically. Note that this is NOT EQUIVALENT to the full type checking algorithm
@@ -21,29 +23,28 @@ interface IrType : KotlinTypeMarker, IrAnnotationContainer {
* Classes are compared by FQ names, which means that even if two types refer to different symbols of the class with the same FQ name, * Classes are compared by FQ names, which means that even if two types refer to different symbols of the class with the same FQ name,
* such types will be considered equal. Type annotations do not have any effect on the behavior of this method. * such types will be considered equal. Type annotations do not have any effect on the behavior of this method.
*/ */
override fun equals(other: Any?): Boolean abstract override fun equals(other: Any?): Boolean
override fun hashCode(): Int abstract override fun hashCode(): Int
} }
interface IrErrorType : IrType abstract class IrErrorType(kotlinType: KotlinType?) : IrTypeBase(kotlinType)
interface IrDynamicType : IrType, DynamicTypeMarker abstract class IrDynamicType(kotlinType: KotlinType?) : IrTypeBase(kotlinType), DynamicTypeMarker
interface IrDefinitelyNotNullType : IrType, DefinitelyNotNullTypeMarker { abstract class IrDefinitelyNotNullType(kotlinType: KotlinType?) : IrTypeBase(kotlinType), DefinitelyNotNullTypeMarker {
val original: IrType abstract val original: IrType
} }
interface IrSimpleType : IrType, SimpleTypeMarker, TypeArgumentListMarker { abstract class IrSimpleType(kotlinType: KotlinType?) : IrTypeBase(kotlinType), SimpleTypeMarker, TypeArgumentListMarker {
val classifier: IrClassifierSymbol abstract val classifier: IrClassifierSymbol
val hasQuestionMark: Boolean abstract val hasQuestionMark: Boolean
val arguments: List<IrTypeArgument> abstract val arguments: List<IrTypeArgument>
val abbreviation: IrTypeAbbreviation? abstract val abbreviation: IrTypeAbbreviation?
} }
interface IrTypeArgument : TypeArgumentMarker { interface IrTypeArgument : TypeArgumentMarker {
override fun equals(other: Any?): Boolean override fun equals(other: Any?): Boolean
override fun hashCode(): Int override fun hashCode(): Int
} }
@@ -14,7 +14,7 @@ import org.jetbrains.kotlin.types.Variance
class IrDefinitelyNotNullTypeImpl( class IrDefinitelyNotNullTypeImpl(
kotlinType: KotlinType?, kotlinType: KotlinType?,
override val original: IrType, override val original: IrType,
) : IrTypeBase(kotlinType), IrDefinitelyNotNullType { ) : IrDefinitelyNotNullType(kotlinType) {
override val annotations: List<IrConstructorCall> override val annotations: List<IrConstructorCall>
get() = original.annotations get() = original.annotations
@@ -12,7 +12,7 @@ import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.types.Variance
abstract class IrAbstractSimpleType(kotlinType: KotlinType?) : IrTypeBase(kotlinType), IrSimpleType { abstract class IrAbstractSimpleType(kotlinType: KotlinType?) : IrSimpleType(kotlinType) {
override val variance: Variance override val variance: Variance
get() = Variance.INVARIANT get() = Variance.INVARIANT
@@ -17,7 +17,7 @@ import org.jetbrains.kotlin.types.model.CapturedTypeConstructorMarker
import org.jetbrains.kotlin.types.model.CapturedTypeMarker import org.jetbrains.kotlin.types.model.CapturedTypeMarker
import org.jetbrains.kotlin.utils.addToStdlib.safeAs import org.jetbrains.kotlin.utils.addToStdlib.safeAs
abstract class IrTypeBase(val kotlinType: KotlinType?) : IrType, IrTypeProjection { abstract class IrTypeBase(val kotlinType: KotlinType?) : IrType(), IrTypeProjection {
override val type: IrType get() = this override val type: IrType get() = this
} }
@@ -25,7 +25,7 @@ class IrErrorTypeImpl(
kotlinType: KotlinType?, kotlinType: KotlinType?,
override val annotations: List<IrConstructorCall>, override val annotations: List<IrConstructorCall>,
override val variance: Variance, override val variance: Variance,
) : IrTypeBase(kotlinType), IrErrorType { ) : IrErrorType(kotlinType) {
override fun equals(other: Any?): Boolean = other is IrErrorTypeImpl override fun equals(other: Any?): Boolean = other is IrErrorTypeImpl
override fun hashCode(): Int = IrErrorTypeImpl::class.java.hashCode() override fun hashCode(): Int = IrErrorTypeImpl::class.java.hashCode()
@@ -35,7 +35,7 @@ class IrDynamicTypeImpl(
kotlinType: KotlinType?, kotlinType: KotlinType?,
override val annotations: List<IrConstructorCall>, override val annotations: List<IrConstructorCall>,
override val variance: Variance, override val variance: Variance,
) : IrTypeBase(kotlinType), IrDynamicType { ) : IrDynamicType(kotlinType) {
override fun equals(other: Any?): Boolean = other is IrDynamicTypeImpl override fun equals(other: Any?): Boolean = other is IrDynamicTypeImpl
override fun hashCode(): Int = IrDynamicTypeImpl::class.java.hashCode() override fun hashCode(): Int = IrDynamicTypeImpl::class.java.hashCode()
@@ -62,7 +62,7 @@ object IrStarProjectionImpl : IrStarProjection {
* however this could lead to a situation where we forget to set return type sometimes. This would result in crashes at unexpected moments, * however this could lead to a situation where we forget to set return type sometimes. This would result in crashes at unexpected moments,
* especially in Kotlin/JS where function return types are not present in the resulting binary files. * especially in Kotlin/JS where function return types are not present in the resulting binary files.
*/ */
object IrUninitializedType : IrType { object IrUninitializedType : IrType() {
override val annotations: List<IrConstructorCall> = emptyList() override val annotations: List<IrConstructorCall> = emptyList()
override fun equals(other: Any?): Boolean = this === other override fun equals(other: Any?): Boolean = this === other
@@ -81,7 +81,10 @@ class IrCapturedType(
val lowerType: IrType?, val lowerType: IrType?,
projection: IrTypeArgument, projection: IrTypeArgument,
typeParameter: IrTypeParameter typeParameter: IrTypeParameter
) : IrSimpleType, CapturedTypeMarker { ) : IrSimpleType(null), CapturedTypeMarker {
override val variance: Variance
get() = TODO("Not yet implemented")
class Constructor(val argument: IrTypeArgument, val typeParameter: IrTypeParameter) : class Constructor(val argument: IrTypeArgument, val typeParameter: IrTypeParameter) :
CapturedTypeConstructorMarker { CapturedTypeConstructorMarker {
@@ -806,6 +806,8 @@ private fun IrDeclaration.renderDeclarationParentFqn(sb: StringBuilder) {
fun IrType.render() = RenderIrElementVisitor().renderType(this) fun IrType.render() = RenderIrElementVisitor().renderType(this)
fun IrSimpleType.render() = (this as IrType).render()
fun IrTypeArgument.render() = fun IrTypeArgument.render() =
when (this) { when (this) {
is IrStarProjection -> "*" is IrStarProjection -> "*"