From 9c6292db6f7df36bb367656dc0f34eecc716c05a Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Fri, 28 Jan 2022 14:34:00 +0300 Subject: [PATCH] [FIR] Split cone types declarations into multiple files --- .../fir/types/ConeIntegerLiteralType.kt | 41 ++++ .../kotlin/fir/types/ConeTypeProjection.kt | 68 ++++++ .../fir/types/ConeTypeVariableAndStubTypes.kt | 125 +++++++++++ .../jetbrains/kotlin/fir/types/ConeTypes.kt | 206 ------------------ 4 files changed, 234 insertions(+), 206 deletions(-) create mode 100644 compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeIntegerLiteralType.kt create mode 100644 compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypeProjection.kt create mode 100644 compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypeVariableAndStubTypes.kt diff --git a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeIntegerLiteralType.kt b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeIntegerLiteralType.kt new file mode 100644 index 00000000000..fcf39552500 --- /dev/null +++ b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeIntegerLiteralType.kt @@ -0,0 +1,41 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.fir.types + +import org.jetbrains.kotlin.types.model.TypeConstructorMarker + +abstract class ConeIntegerLiteralType( + val value: Long, + val isUnsigned: Boolean, + override val nullability: ConeNullability +) : ConeSimpleKotlinType(), TypeConstructorMarker { + abstract val possibleTypes: Collection + abstract val supertypes: List + + override val typeArguments: Array = emptyArray() + + abstract fun getApproximatedType(expectedType: ConeKotlinType? = null): ConeClassLikeType + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as ConeIntegerLiteralType + + if (possibleTypes != other.possibleTypes) return false + if (nullability != other.nullability) return false + + return true + } + + override fun hashCode(): Int { + return 31 * possibleTypes.hashCode() + nullability.hashCode() + } +} + +fun ConeIntegerLiteralType.canBeInt(): Boolean { + return value in Int.MIN_VALUE..Int.MAX_VALUE +} diff --git a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypeProjection.kt b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypeProjection.kt new file mode 100644 index 00000000000..2df8ee1f6b1 --- /dev/null +++ b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypeProjection.kt @@ -0,0 +1,68 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.fir.types + +import org.jetbrains.kotlin.types.model.TypeArgumentMarker + +enum class ProjectionKind { + STAR, IN, OUT, INVARIANT; +} + +sealed class ConeTypeProjection : TypeArgumentMarker { + abstract val kind: ProjectionKind + + companion object { + val EMPTY_ARRAY = arrayOf() + } +} + +object ConeStarProjection : ConeTypeProjection() { + override val kind: ProjectionKind + get() = ProjectionKind.STAR +} + +sealed class ConeKotlinTypeProjection : ConeTypeProjection() { + abstract val type: ConeKotlinType + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is ConeKotlinTypeProjection) return false + + if (kind != other.kind) return false + if (type != other.type) return false + + return true + } + + override fun hashCode(): Int { + return type.hashCode() * 31 + kind.hashCode() + } +} + +data class ConeKotlinTypeProjectionIn(override val type: ConeKotlinType) : ConeKotlinTypeProjection() { + override val kind: ProjectionKind + get() = ProjectionKind.IN +} + +data class ConeKotlinTypeProjectionOut(override val type: ConeKotlinType) : ConeKotlinTypeProjection() { + override val kind: ProjectionKind + get() = ProjectionKind.OUT +} + +data class ConeKotlinTypeConflictingProjection(override val type: ConeKotlinType) : ConeKotlinTypeProjection() { + override val kind: ProjectionKind + get() = ProjectionKind.INVARIANT +} + +val ConeTypeProjection.type: ConeKotlinType? + get() = when (this) { + ConeStarProjection -> null + is ConeKotlinTypeProjection -> type + is ConeKotlinType -> this + } + +val ConeTypeProjection.isStarProjection: Boolean + get() = this == ConeStarProjection diff --git a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypeVariableAndStubTypes.kt b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypeVariableAndStubTypes.kt new file mode 100644 index 00000000000..e0ec63584d9 --- /dev/null +++ b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypeVariableAndStubTypes.kt @@ -0,0 +1,125 @@ +/* + * Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors. + * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. + */ + +package org.jetbrains.kotlin.fir.types + +import org.jetbrains.kotlin.fir.symbols.ConeClassifierLookupTag +import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.types.model.* + +// ----------------------------------- Type variable type ----------------------------------- + +class ConeTypeVariableType( + override val nullability: ConeNullability, + override val lookupTag: ConeTypeVariableTypeConstructor, + override val attributes: ConeAttributes = ConeAttributes.Empty, +) : ConeLookupTagBasedType() { + override val typeArguments: Array get() = emptyArray() + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is ConeTypeVariableType) return false + + if (nullability != other.nullability) return false + if (lookupTag != other.lookupTag) return false + + return true + } + + override fun hashCode(): Int { + var result = 0 + result = 31 * result + nullability.hashCode() + result = 31 * result + lookupTag.hashCode() + return result + } +} + +class ConeTypeVariableTypeConstructor( + val debugName: String, + val originalTypeParameter: TypeParameterMarker? +) : ConeClassifierLookupTag(), TypeVariableTypeConstructorMarker { + override val name: Name get() = Name.identifier(debugName) + + var isContainedInInvariantOrContravariantPositions: Boolean = false + private set + + fun recordInfoAboutTypeVariableUsagesAsInvariantOrContravariantParameter() { + isContainedInInvariantOrContravariantPositions = true + } +} + +// ----------------------------------- Stub types ----------------------------------- + +data class ConeStubTypeConstructor( + val variable: ConeTypeVariable, + val isTypeVariableInSubtyping: Boolean, + val isForFixation: Boolean = false, +) : TypeConstructorMarker + +sealed class ConeStubType(val constructor: ConeStubTypeConstructor, override val nullability: ConeNullability) : StubTypeMarker, + ConeSimpleKotlinType() { + + override val typeArguments: Array + get() = emptyArray() + + override val attributes: ConeAttributes + get() = ConeAttributes.Empty + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (javaClass != other?.javaClass) return false + + other as ConeStubType + + if (constructor != other.constructor) return false + if (nullability != other.nullability) return false + + return true + } + + override fun hashCode(): Int { + var result = 0 + result = 31 * result + constructor.hashCode() + result = 31 * result + nullability.hashCode() + return result + } +} + +open class ConeStubTypeForChainInference( + constructor: ConeStubTypeConstructor, + nullability: ConeNullability +) : ConeStubType(constructor, nullability) { + constructor(variable: ConeTypeVariable, nullability: ConeNullability) : this( + ConeStubTypeConstructor( + variable, + isTypeVariableInSubtyping = false + ), nullability + ) +} + +class ConeStubTypeForSyntheticFixation( + constructor: ConeStubTypeConstructor, + nullability: ConeNullability +) : ConeStubTypeForChainInference(constructor, nullability) + +class ConeStubTypeForTypeVariableInSubtyping( + constructor: ConeStubTypeConstructor, + nullability: ConeNullability +) : ConeStubType(constructor, nullability) { + constructor(variable: ConeTypeVariable, nullability: ConeNullability) : this( + ConeStubTypeConstructor( + variable, + isTypeVariableInSubtyping = true + ), nullability + ) +} + +open class ConeTypeVariable(name: String, originalTypeParameter: TypeParameterMarker? = null) : TypeVariableMarker { + val typeConstructor = ConeTypeVariableTypeConstructor(name, originalTypeParameter) + val defaultType = ConeTypeVariableType(ConeNullability.NOT_NULL, typeConstructor) + + override fun toString(): String { + return defaultType.toString() + } +} diff --git a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypes.kt b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypes.kt index cac6c2dae19..1cd24cac783 100644 --- a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypes.kt +++ b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeTypes.kt @@ -9,70 +9,9 @@ import org.jetbrains.kotlin.fir.diagnostics.ConeDiagnostic import org.jetbrains.kotlin.fir.symbols.ConeClassLikeLookupTag import org.jetbrains.kotlin.fir.symbols.ConeClassifierLookupTag import org.jetbrains.kotlin.name.ClassId -import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.types.model.* import org.jetbrains.kotlin.utils.addToStdlib.foldMap -enum class ProjectionKind { - STAR, IN, OUT, INVARIANT; -} - -sealed class ConeTypeProjection : TypeArgumentMarker { - abstract val kind: ProjectionKind - - companion object { - val EMPTY_ARRAY = arrayOf() - } -} - -object ConeStarProjection : ConeTypeProjection() { - override val kind: ProjectionKind - get() = ProjectionKind.STAR -} - -sealed class ConeKotlinTypeProjection : ConeTypeProjection() { - abstract val type: ConeKotlinType - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other !is ConeKotlinTypeProjection) return false - - if (kind != other.kind) return false - if (type != other.type) return false - - return true - } - - override fun hashCode(): Int { - return type.hashCode() * 31 + kind.hashCode() - } -} - -data class ConeKotlinTypeProjectionIn(override val type: ConeKotlinType) : ConeKotlinTypeProjection() { - override val kind: ProjectionKind - get() = ProjectionKind.IN -} - -data class ConeKotlinTypeProjectionOut(override val type: ConeKotlinType) : ConeKotlinTypeProjection() { - override val kind: ProjectionKind - get() = ProjectionKind.OUT -} - -data class ConeKotlinTypeConflictingProjection(override val type: ConeKotlinType) : ConeKotlinTypeProjection() { - override val kind: ProjectionKind - get() = ProjectionKind.INVARIANT -} - -val ConeTypeProjection.type: ConeKotlinType? - get() = when (this) { - ConeStarProjection -> null - is ConeKotlinTypeProjection -> type - is ConeKotlinType -> this - } - -val ConeTypeProjection.isStarProjection: Boolean - get() = this == ConeStarProjection - // We assume type IS an invariant type projection to prevent additional wrapper here // (more exactly, invariant type projection contains type) sealed class ConeKotlinType : ConeKotlinTypeProjection(), KotlinTypeMarker, TypeArgumentListMarker { @@ -237,31 +176,6 @@ data class ConeCapturedType( } } -class ConeTypeVariableType( - override val nullability: ConeNullability, - override val lookupTag: ConeTypeVariableTypeConstructor, - override val attributes: ConeAttributes = ConeAttributes.Empty, -) : ConeLookupTagBasedType() { - override val typeArguments: Array get() = emptyArray() - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (other !is ConeTypeVariableType) return false - - if (nullability != other.nullability) return false - if (lookupTag != other.lookupTag) return false - - return true - } - - override fun hashCode(): Int { - var result = 0 - result = 31 * result + nullability.hashCode() - result = 31 * result + lookupTag.hashCode() - return result - } - - -} data class ConeDefinitelyNotNullType(val original: ConeSimpleKotlinType) : ConeSimpleKotlinType(), DefinitelyNotNullTypeMarker { override val typeArguments: Array @@ -328,123 +242,3 @@ fun ConeIntersectionType.withAlternative(alternativeType: ConeKotlinType): ConeI fun ConeIntersectionType.mapTypes(func: (ConeKotlinType) -> ConeKotlinType): ConeIntersectionType { return ConeIntersectionType(intersectedTypes.map(func), alternativeType?.let(func)) } - -data class ConeStubTypeConstructor( - val variable: ConeTypeVariable, - val isTypeVariableInSubtyping: Boolean, - val isForFixation: Boolean = false, -) : TypeConstructorMarker - -sealed class ConeStubType(val constructor: ConeStubTypeConstructor, override val nullability: ConeNullability) : StubTypeMarker, - ConeSimpleKotlinType() { - - override val typeArguments: Array - get() = emptyArray() - - override val attributes: ConeAttributes - get() = ConeAttributes.Empty - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as ConeStubType - - if (constructor != other.constructor) return false - if (nullability != other.nullability) return false - - return true - } - - override fun hashCode(): Int { - var result = 0 - result = 31 * result + constructor.hashCode() - result = 31 * result + nullability.hashCode() - return result - } -} - -open class ConeStubTypeForChainInference( - constructor: ConeStubTypeConstructor, - nullability: ConeNullability -) : ConeStubType(constructor, nullability) { - constructor(variable: ConeTypeVariable, nullability: ConeNullability) : this( - ConeStubTypeConstructor( - variable, - isTypeVariableInSubtyping = false - ), nullability - ) -} - -class ConeStubTypeForSyntheticFixation( - constructor: ConeStubTypeConstructor, - nullability: ConeNullability -) : ConeStubTypeForChainInference(constructor, nullability) - -class ConeStubTypeForTypeVariableInSubtyping( - constructor: ConeStubTypeConstructor, - nullability: ConeNullability -) : ConeStubType(constructor, nullability) { - constructor(variable: ConeTypeVariable, nullability: ConeNullability) : this( - ConeStubTypeConstructor( - variable, - isTypeVariableInSubtyping = true - ), nullability - ) -} - -open class ConeTypeVariable(name: String, originalTypeParameter: TypeParameterMarker? = null) : TypeVariableMarker { - val typeConstructor = ConeTypeVariableTypeConstructor(name, originalTypeParameter) - val defaultType = ConeTypeVariableType(ConeNullability.NOT_NULL, typeConstructor) - - override fun toString(): String { - return defaultType.toString() - } -} - -class ConeTypeVariableTypeConstructor( - val debugName: String, - val originalTypeParameter: TypeParameterMarker? -) : ConeClassifierLookupTag(), TypeVariableTypeConstructorMarker { - override val name: Name get() = Name.identifier(debugName) - - var isContainedInInvariantOrContravariantPositions: Boolean = false - private set - - fun recordInfoAboutTypeVariableUsagesAsInvariantOrContravariantParameter() { - isContainedInInvariantOrContravariantPositions = true - } -} - -abstract class ConeIntegerLiteralType( - val value: Long, - val isUnsigned: Boolean, - override val nullability: ConeNullability -) : ConeSimpleKotlinType(), TypeConstructorMarker { - abstract val possibleTypes: Collection - abstract val supertypes: List - - override val typeArguments: Array = emptyArray() - - abstract fun getApproximatedType(expectedType: ConeKotlinType? = null): ConeClassLikeType - - override fun equals(other: Any?): Boolean { - if (this === other) return true - if (javaClass != other?.javaClass) return false - - other as ConeIntegerLiteralType - - if (possibleTypes != other.possibleTypes) return false - if (nullability != other.nullability) return false - - return true - } - - override fun hashCode(): Int { - return 31 * possibleTypes.hashCode() + nullability.hashCode() - } -} - -fun ConeIntegerLiteralType.canBeInt(): Boolean { - return value in Int.MIN_VALUE..Int.MAX_VALUE -}