From ce7456c93d04a91ef215bb34e9bdf49b2365b6ae Mon Sep 17 00:00:00 2001 From: Simon Ogorodnik Date: Tue, 20 Mar 2018 01:22:50 +0300 Subject: [PATCH] FIR: Introduce new kotlin types aka 'cones' #KT-24063 Fixed --- .../jetbrains/kotlin/fir/UnambiguousFqName.kt | 22 ++++++++++ .../kotlin/fir/types/ConeKotlinType.kt | 33 ++++++++++++++ .../jetbrains/kotlin/fir/types/impl/Impl.kt | 27 ++++++++++++ .../org/jetbrains/kotlin/fir/FirRenderer.kt | 27 +++++++++++- .../kotlin/fir/types/FirResolvedType.kt | 2 +- .../kotlin/fir/types/impl/FirBuiltinType.kt | 43 +++++++++++++++++++ ...{FirUnitType.kt => FirResolvedTypeImpl.kt} | 20 +++------ .../rawBuilder/declarations/simpleClass.txt | 2 +- 8 files changed, 160 insertions(+), 16 deletions(-) create mode 100644 compiler/fir/cones/src/org/jetbrains/kotlin/fir/UnambiguousFqName.kt create mode 100644 compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeKotlinType.kt create mode 100644 compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/impl/Impl.kt create mode 100644 compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/impl/FirBuiltinType.kt rename compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/impl/{FirUnitType.kt => FirResolvedTypeImpl.kt} (52%) diff --git a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/UnambiguousFqName.kt b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/UnambiguousFqName.kt new file mode 100644 index 00000000000..b6325b346cb --- /dev/null +++ b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/UnambiguousFqName.kt @@ -0,0 +1,22 @@ + +/* + * Copyright 2010-2018 JetBrains s.r.o. 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 + +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.FqNameUnsafe + +/* + * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the license/LICENSE.txt file. + */ + +data class UnambiguousFqName(val packageFqName: FqNameUnsafe, val classFqName: FqName) { + + override fun toString(): String { + return packageFqName.asString() + "/" + classFqName.asString() + } +} \ No newline at end of file diff --git a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeKotlinType.kt b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeKotlinType.kt new file mode 100644 index 00000000000..a85a03a5958 --- /dev/null +++ b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/ConeKotlinType.kt @@ -0,0 +1,33 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. 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.UnambiguousFqName + +sealed class ConeKotlinTypeProjection(val kind: ProjectionKind) + +enum class ProjectionKind { + STAR, IN, OUT, INVARIANT +} + + +object StarProjection : ConeKotlinTypeProjection(ProjectionKind.STAR) + +abstract class ConeKotlinTypeProjectionIn : ConeKotlinTypeProjection(ProjectionKind.IN) { + abstract val type: ConeKotlinType +} + +abstract class ConeKotlinTypeProjectionOut : ConeKotlinTypeProjection(ProjectionKind.OUT) { + abstract val type: ConeKotlinType +} + +abstract class ConeKotlinType : ConeKotlinTypeProjection(ProjectionKind.INVARIANT) { + abstract val typeArguments: List +} + +abstract class ConeClassType : ConeKotlinType() { + abstract val fqName: UnambiguousFqName +} \ No newline at end of file diff --git a/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/impl/Impl.kt b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/impl/Impl.kt new file mode 100644 index 00000000000..5f7f4f34e84 --- /dev/null +++ b/compiler/fir/cones/src/org/jetbrains/kotlin/fir/types/impl/Impl.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2010-2018 JetBrains s.r.o. 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.impl + +import org.jetbrains.kotlin.fir.UnambiguousFqName +import org.jetbrains.kotlin.fir.types.* + +class ConeClassTypeImpl( + override val fqName: UnambiguousFqName, + override val typeArguments: List +) : ConeClassType() + +class NewKotlinTypeProjectionInImpl(override val type: ConeKotlinType) : ConeKotlinTypeProjectionIn() + +class ConeKotlinTypeProjectionOutImpl(override val type: ConeKotlinType) : ConeKotlinTypeProjectionOut() + +class ConeKotlinErrorType(val reason: String) : ConeKotlinType() { + override val typeArguments: List + get() = emptyList() + + override fun toString(): String { + return "" + } +} \ No newline at end of file diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/FirRenderer.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/FirRenderer.kt index 640602c9d83..ed886a3961c 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/FirRenderer.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/FirRenderer.kt @@ -8,6 +8,7 @@ package org.jetbrains.kotlin.fir import org.jetbrains.kotlin.fir.declarations.* import org.jetbrains.kotlin.fir.expressions.* import org.jetbrains.kotlin.fir.types.* +import org.jetbrains.kotlin.fir.types.impl.ConeKotlinErrorType import org.jetbrains.kotlin.fir.visitors.FirVisitorVoid import org.jetbrains.kotlin.types.Variance import org.jetbrains.kotlin.utils.Printer @@ -364,10 +365,34 @@ class FirRenderer(builder: StringBuilder) : FirVisitorVoid() { visitTypeWithNullability(functionType) } + private fun ConeKotlinType.asString(): String { + return when (this) { + is ConeKotlinErrorType -> "error: $reason" + is ConeClassType -> { + val sb = StringBuilder() + val fqName = fqName + sb.append(fqName.packageFqName.asString().replace('.', '/')) + sb.append('.') + sb.append(fqName.classFqName.asString()) + sb.append(typeArguments.joinToString { it -> + when (it) { + StarProjection -> "*" + is ConeKotlinTypeProjectionIn -> "in ${it.type.asString()}" + is ConeKotlinTypeProjectionOut -> "out ${it.type.asString()}" + is ConeKotlinType -> it.asString() + } + }) + sb.toString() + } + else -> "Unsupported: $this" + } + } + override fun visitResolvedType(resolvedType: FirResolvedType) { resolvedType.annotations.renderAnnotations() print("R/") - print(resolvedType.type) + val coneType = resolvedType.type + print(coneType.asString()) print("/") visitTypeWithNullability(resolvedType) } diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/FirResolvedType.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/FirResolvedType.kt index 42f85ca73dc..34d089c38e5 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/FirResolvedType.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/FirResolvedType.kt @@ -9,7 +9,7 @@ import org.jetbrains.kotlin.fir.visitors.FirVisitor import org.jetbrains.kotlin.types.KotlinType interface FirResolvedType : FirTypeWithNullability { - val type: KotlinType + val type: ConeKotlinType override fun accept(visitor: FirVisitor, data: D): R = visitor.visitResolvedType(this, data) diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/impl/FirBuiltinType.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/impl/FirBuiltinType.kt new file mode 100644 index 00000000000..718949dce0c --- /dev/null +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/impl/FirBuiltinType.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2000-2018 JetBrains s.r.o. 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.impl + +import com.intellij.psi.PsiElement +import org.jetbrains.kotlin.fir.FirSession +import org.jetbrains.kotlin.fir.UnambiguousFqName +import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall +import org.jetbrains.kotlin.fir.types.ConeKotlinType +import org.jetbrains.kotlin.fir.types.FirResolvedType +import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.FqNameUnsafe +import org.jetbrains.kotlin.name.Name + +sealed class FirBuiltinType( + final override val session: FirSession, + final override val psi: PsiElement?, + val name: String +) : FirResolvedType { + final override val type: ConeKotlinType = ConeClassTypeImpl( + UnambiguousFqName( + KOTLIN_PACKAGE_FQ_NAME, + FqName.topLevel(Name.identifier(name)) + ), emptyList() + ) + + final override val isNullable = false + + final override val annotations: List + get() = emptyList() + + companion object { + private val KOTLIN_PACKAGE_FQ_NAME = FqNameUnsafe.topLevel(Name.identifier("kotlin")) + } +} + +class FirUnitType( + session: FirSession, + psi: PsiElement? +) : FirBuiltinType(session, psi, "Unit") \ No newline at end of file diff --git a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/impl/FirUnitType.kt b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/impl/FirResolvedTypeImpl.kt similarity index 52% rename from compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/impl/FirUnitType.kt rename to compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/impl/FirResolvedTypeImpl.kt index d2c0ddf3a13..0254f2b09ca 100644 --- a/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/impl/FirUnitType.kt +++ b/compiler/fir/tree/src/org/jetbrains/kotlin/fir/types/impl/FirResolvedTypeImpl.kt @@ -1,26 +1,20 @@ /* - * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * Copyright 2010-2018 JetBrains s.r.o. 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.impl import com.intellij.psi.PsiElement -import org.jetbrains.kotlin.builtins.DefaultBuiltIns import org.jetbrains.kotlin.fir.FirSession import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall +import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.FirResolvedType -import org.jetbrains.kotlin.types.KotlinType -class FirUnitType( +class FirResolvedTypeImpl( override val session: FirSession, - override val psi: PsiElement? -) : FirResolvedType { - override val type: KotlinType - get() = DefaultBuiltIns.Instance.unitType - - override val isNullable = false - + override val psi: PsiElement?, + override val type: ConeKotlinType, + override val isNullable: Boolean, override val annotations: List - get() = emptyList() -} \ No newline at end of file +) : FirResolvedType \ No newline at end of file diff --git a/compiler/testData/fir/rawBuilder/declarations/simpleClass.txt b/compiler/testData/fir/rawBuilder/declarations/simpleClass.txt index 5a27f16d46c..242b71899d6 100644 --- a/compiler/testData/fir/rawBuilder/declarations/simpleClass.txt +++ b/compiler/testData/fir/rawBuilder/declarations/simpleClass.txt @@ -22,6 +22,6 @@ FILE: simpleClass.kt unknown final property fau(var): Double unknown get(): Double - unknown set(value: Double): R/Unit/ + unknown set(value: Double): R/kotlin.Unit/ }