FIR: Introduce new kotlin types aka 'cones' #KT-24063 Fixed

This commit is contained in:
Simon Ogorodnik
2018-03-20 01:22:50 +03:00
committed by Mikhail Glukhikh
parent 12acbaaa21
commit ce7456c93d
8 changed files with 160 additions and 16 deletions
@@ -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()
}
}
@@ -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<ConeKotlinTypeProjection>
}
abstract class ConeClassType : ConeKotlinType() {
abstract val fqName: UnambiguousFqName
}
@@ -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<ConeKotlinTypeProjection>
) : 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<ConeKotlinTypeProjection>
get() = emptyList()
override fun toString(): String {
return "<ERROR TYPE: $reason>"
}
}
@@ -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)
}
@@ -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 <R, D> accept(visitor: FirVisitor<R, D>, data: D): R =
visitor.visitResolvedType(this, data)
@@ -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<FirAnnotationCall>
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")
@@ -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<FirAnnotationCall>
get() = emptyList()
}
) : FirResolvedType
@@ -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/
}