FIR: refactor cone types (arguments, projections)

This commit is contained in:
Simon Ogorodnik
2018-04-03 19:25:22 +03:00
committed by Mikhail Glukhikh
parent 02bedeca05
commit 6c3fe5dc98
6 changed files with 49 additions and 29 deletions
@@ -9,27 +9,46 @@ import org.jetbrains.kotlin.fir.symbols.ConeClassLikeSymbol
import org.jetbrains.kotlin.fir.symbols.ConeSymbol import org.jetbrains.kotlin.fir.symbols.ConeSymbol
import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterSymbol import org.jetbrains.kotlin.fir.symbols.ConeTypeParameterSymbol
sealed class ConeKotlinTypeProjection(val kind: ProjectionKind) sealed class ConeKotlinTypeProjection {
abstract val kind: ProjectionKind
companion object {
val EMPTY_ARRAY = arrayOf<ConeKotlinTypeProjection>()
}
}
enum class ProjectionKind { enum class ProjectionKind {
STAR, IN, OUT, INVARIANT STAR, IN, OUT, INVARIANT
} }
object StarProjection : ConeKotlinTypeProjection(ProjectionKind.STAR) object StarProjection : ConeKotlinTypeProjection() {
override val kind: ProjectionKind
abstract class ConeKotlinTypeProjectionIn : ConeKotlinTypeProjection(ProjectionKind.IN) { get() = ProjectionKind.STAR
abstract val type: ConeKotlinType
} }
abstract class ConeKotlinTypeProjectionOut : ConeKotlinTypeProjection(ProjectionKind.OUT) { class ConeKotlinTypeProjectionIn(val type: ConeKotlinType) : ConeKotlinTypeProjection() {
abstract val type: ConeKotlinType override val kind: ProjectionKind
get() = ProjectionKind.IN
}
class ConeKotlinTypeProjectionOut(val type: ConeKotlinType) : ConeKotlinTypeProjection() {
override val kind: ProjectionKind
get() = ProjectionKind.OUT
} }
// We assume type IS an invariant type projection to prevent additional wrapper here // We assume type IS an invariant type projection to prevent additional wrapper here
// (more exactly, invariant type projection contains type) // (more exactly, invariant type projection contains type)
sealed class ConeKotlinType : ConeKotlinTypeProjection(ProjectionKind.INVARIANT) sealed class ConeKotlinType : ConeKotlinTypeProjection() {
override val kind: ProjectionKind
get() = ProjectionKind.INVARIANT
abstract val typeArguments: Array<out ConeKotlinTypeProjection>
}
class ConeKotlinErrorType(val reason: String) : ConeKotlinType() { class ConeKotlinErrorType(val reason: String) : ConeKotlinType() {
override val typeArguments: Array<out ConeKotlinTypeProjection>
get() = EMPTY_ARRAY
override fun toString(): String { override fun toString(): String {
return "<ERROR TYPE: $reason>" return "<ERROR TYPE: $reason>"
} }
@@ -38,8 +57,9 @@ class ConeKotlinErrorType(val reason: String) : ConeKotlinType() {
class ConeClassErrorType(val reason: String) : ConeClassLikeType() { class ConeClassErrorType(val reason: String) : ConeClassLikeType() {
override val symbol: ConeClassLikeSymbol override val symbol: ConeClassLikeSymbol
get() = error("!") get() = error("!")
override val typeArguments: List<ConeKotlinTypeProjection>
get() = emptyList() override val typeArguments: Array<out ConeKotlinTypeProjection>
get() = EMPTY_ARRAY
override fun toString(): String { override fun toString(): String {
return "<ERROR CLASS: $reason>" return "<ERROR CLASS: $reason>"
@@ -52,8 +72,6 @@ sealed class ConeSymbolBasedType : ConeKotlinType() {
abstract class ConeClassLikeType : ConeSymbolBasedType() { abstract class ConeClassLikeType : ConeSymbolBasedType() {
abstract override val symbol: ConeClassLikeSymbol abstract override val symbol: ConeClassLikeSymbol
abstract val typeArguments: List<ConeKotlinTypeProjection>
} }
abstract class ConeAbbreviatedType : ConeClassLikeType() { abstract class ConeAbbreviatedType : ConeClassLikeType() {
@@ -7,9 +7,13 @@ package org.jetbrains.kotlin.fir.types.impl
import org.jetbrains.kotlin.fir.types.ConeFunctionType import org.jetbrains.kotlin.fir.types.ConeFunctionType
import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.ConeKotlinTypeProjection
class ConeFunctionTypeImpl( class ConeFunctionTypeImpl(
override val receiverType: ConeKotlinType?, override val receiverType: ConeKotlinType?,
override val parameterTypes: List<ConeKotlinType>, override val parameterTypes: List<ConeKotlinType>,
override val returnType: ConeKotlinType override val returnType: ConeKotlinType
) : ConeFunctionType() ) : ConeFunctionType() {
override val typeArguments: Array<out ConeKotlinTypeProjection>
get() = EMPTY_ARRAY
}
@@ -11,20 +11,19 @@ import org.jetbrains.kotlin.fir.types.*
open class ConeClassTypeImpl( open class ConeClassTypeImpl(
override val symbol: ConeClassLikeSymbol, override val symbol: ConeClassLikeSymbol,
override val typeArguments: List<ConeKotlinTypeProjection> override val typeArguments: Array<ConeKotlinTypeProjection>
) : ConeClassLikeType() ) : ConeClassLikeType()
class ConeKotlinTypeProjectionInImpl(override val type: ConeKotlinType) : ConeKotlinTypeProjectionIn()
class ConeKotlinTypeProjectionOutImpl(override val type: ConeKotlinType) : ConeKotlinTypeProjectionOut()
class ConeAbbreviatedTypeImpl( class ConeAbbreviatedTypeImpl(
override val abbreviationSymbol: ConeClassLikeSymbol, override val abbreviationSymbol: ConeClassLikeSymbol,
override val typeArguments: List<ConeKotlinTypeProjection>, override val typeArguments: Array<ConeKotlinTypeProjection>,
override val directExpansion: ConeClassLikeType override val directExpansion: ConeClassLikeType
) : ConeAbbreviatedType() { ) : ConeAbbreviatedType() {
override val symbol: ConeClassLikeSymbol override val symbol: ConeClassLikeSymbol
get() = abbreviationSymbol get() = abbreviationSymbol
} }
class ConeTypeParameterTypeImpl(override val symbol: ConeTypeParameterSymbol) : ConeTypeParameterType() class ConeTypeParameterTypeImpl(override val symbol: ConeTypeParameterSymbol) : ConeTypeParameterType() {
override val typeArguments: Array<out ConeKotlinTypeProjection>
get() = EMPTY_ARRAY
}
@@ -13,8 +13,6 @@ import org.jetbrains.kotlin.fir.symbols.LibraryTypeParameterSymbol
import org.jetbrains.kotlin.fir.types.* import org.jetbrains.kotlin.fir.types.*
import org.jetbrains.kotlin.fir.types.impl.ConeAbbreviatedTypeImpl import org.jetbrains.kotlin.fir.types.impl.ConeAbbreviatedTypeImpl
import org.jetbrains.kotlin.fir.types.impl.ConeClassTypeImpl import org.jetbrains.kotlin.fir.types.impl.ConeClassTypeImpl
import org.jetbrains.kotlin.fir.types.impl.ConeKotlinTypeProjectionInImpl
import org.jetbrains.kotlin.fir.types.impl.ConeKotlinTypeProjectionOutImpl
import org.jetbrains.kotlin.metadata.ProtoBuf import org.jetbrains.kotlin.metadata.ProtoBuf
import org.jetbrains.kotlin.metadata.deserialization.* import org.jetbrains.kotlin.metadata.deserialization.*
import org.jetbrains.kotlin.serialization.deserialization.ProtoEnumFlags import org.jetbrains.kotlin.serialization.deserialization.ProtoEnumFlags
@@ -78,7 +76,7 @@ class FirTypeDeserializer(
val arguments = proto.collectAllArguments().mapIndexed { index, proto -> val arguments = proto.collectAllArguments().mapIndexed { index, proto ->
typeArgument(constructor.typeParameters.getOrNull(index), proto) typeArgument(constructor.typeParameters.getOrNull(index), proto)
}.toList() }.toTypedArray()
val simpleType = if (Flags.SUSPEND_TYPE.get(proto.flags)) { val simpleType = if (Flags.SUSPEND_TYPE.get(proto.flags)) {
//createSuspendFunctionType(annotations, constructor, arguments, proto.nullable) //createSuspendFunctionType(annotations, constructor, arguments, proto.nullable)
@@ -121,8 +119,8 @@ class FirTypeDeserializer(
val coneType = type(type) val coneType = type(type)
return when (projection) { return when (projection) {
Variance.INVARIANT -> coneType Variance.INVARIANT -> coneType
Variance.IN_VARIANCE -> ConeKotlinTypeProjectionInImpl(coneType) Variance.IN_VARIANCE -> ConeKotlinTypeProjectionIn(coneType)
Variance.OUT_VARIANCE -> ConeKotlinTypeProjectionOutImpl(coneType) Variance.OUT_VARIANCE -> ConeKotlinTypeProjectionOut(coneType)
} }
} }
@@ -25,14 +25,14 @@ class FirTypeResolverImpl : FirTypeResolver {
val type = (it.type as FirResolvedType).type val type = (it.type as FirResolvedType).type
when (it.variance) { when (it.variance) {
Variance.INVARIANT -> type Variance.INVARIANT -> type
Variance.IN_VARIANCE -> ConeKotlinTypeProjectionInImpl(type) Variance.IN_VARIANCE -> ConeKotlinTypeProjectionIn(type)
Variance.OUT_VARIANCE -> ConeKotlinTypeProjectionOutImpl(type) Variance.OUT_VARIANCE -> ConeKotlinTypeProjectionOut(type)
} }
} }
else -> error("!") else -> error("!")
} }
} }
} }.toTypedArray()
private fun ConeSymbol.toConeKotlinType(parts: List<FirQualifierPart>): ConeKotlinType? { private fun ConeSymbol.toConeKotlinType(parts: List<FirQualifierPart>): ConeKotlinType? {
@@ -10,6 +10,7 @@ import org.jetbrains.kotlin.fir.FirSession
import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall import org.jetbrains.kotlin.fir.expressions.FirAnnotationCall
import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol import org.jetbrains.kotlin.fir.symbols.impl.FirClassSymbol
import org.jetbrains.kotlin.fir.types.ConeKotlinType import org.jetbrains.kotlin.fir.types.ConeKotlinType
import org.jetbrains.kotlin.fir.types.ConeKotlinTypeProjection
import org.jetbrains.kotlin.fir.types.FirResolvedType import org.jetbrains.kotlin.fir.types.FirResolvedType
import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
@@ -27,7 +28,7 @@ sealed class FirBuiltinType(
Name.identifier(name) Name.identifier(name)
) )
), ),
emptyList() ConeKotlinTypeProjection.EMPTY_ARRAY
) )
final override val isNullable = false final override val isNullable = false