IrType -> IrDynamicType, IrErrorType, IrSimpleType

This commit is contained in:
Dmitry Petrov
2018-04-20 12:13:52 +03:00
parent fdc0335b4a
commit 1be28d6032
4 changed files with 74 additions and 23 deletions
@@ -14,7 +14,11 @@ import org.jetbrains.kotlin.ir.declarations.IrDeclaration
import org.jetbrains.kotlin.ir.declarations.IrTypeParametersContainer
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.expressions.IrExpression
import org.jetbrains.kotlin.ir.types.impl.IrTypeImpl
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.IrTypeProjection
import org.jetbrains.kotlin.ir.types.impl.IrDynamicTypeImpl
import org.jetbrains.kotlin.ir.types.impl.IrErrorTypeImpl
import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl
import org.jetbrains.kotlin.ir.util.SymbolTable
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
import org.jetbrains.kotlin.ir.visitors.acceptChildrenVoid
@@ -46,20 +50,22 @@ class TypeTranslator(
typeParametersResolver.resolveScopedTypeParameter(typeParameterDescriptor)
?: symbolTable.referenceTypeParameter(typeParameterDescriptor)
fun translateType(ktType: KotlinType): IrTypeImpl =
translateType(ktType, Variance.INVARIANT)
fun translateType(ktType: KotlinType): IrType =
translateType(ktType, Variance.INVARIANT).type
private fun translateType(ktType: KotlinType, variance: Variance): IrTypeImpl {
if (ktType.isFlexible()) {
return translateType(ktType.upperIfFlexible(), variance)
private fun translateType(ktType: KotlinType, variance: Variance): IrTypeProjection {
when {
ktType.isError -> return IrErrorTypeImpl(translateTypeAnnotations(ktType.annotations), variance)
ktType.isFlexible() -> return translateType(ktType.upperIfFlexible(), variance)
ktType.isDynamic() -> return IrDynamicTypeImpl(translateTypeAnnotations(ktType.annotations), variance)
}
val ktTypeConstructor = ktType.constructor
val ktTypeDescriptor = ktTypeConstructor.declarationDescriptor
val ktTypeDescriptor = ktTypeConstructor.declarationDescriptor ?: throw AssertionError("No descriptor for type $ktType")
return when (ktTypeDescriptor) {
is TypeParameterDescriptor ->
IrTypeImpl(
IrSimpleTypeImpl(
resolveTypeParameter(ktTypeDescriptor),
ktType.isMarkedNullable,
emptyList(),
@@ -68,7 +74,7 @@ class TypeTranslator(
)
is ClassDescriptor ->
IrTypeImpl(
IrSimpleTypeImpl(
symbolTable.referenceClass(ktTypeDescriptor),
ktType.isMarkedNullable,
translateTypeArguments(ktType.arguments),
@@ -77,7 +83,7 @@ class TypeTranslator(
)
else ->
TODO()
throw AssertionError("Unexpected type descriptor $ktTypeDescriptor :: ${ktTypeDescriptor::class}")
}
}
@@ -10,10 +10,17 @@ import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.types.Variance
interface IrType {
val annotations: List<IrCall>
}
interface IrErrorType : IrType
interface IrDynamicType : IrType
interface IrSimpleType : IrType {
val classifier: IrClassifierSymbol
val hasQuestionMark: Boolean
val arguments: List<IrTypeProjection>
val annotations: List<IrCall>
}
interface IrTypeProjection {
@@ -7,17 +7,16 @@ package org.jetbrains.kotlin.ir.types.impl
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.IrTypeProjection
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.types.Variance
class IrTypeImpl(
class IrSimpleTypeImpl(
override val classifier: IrClassifierSymbol,
override val hasQuestionMark: Boolean,
override val arguments: List<IrTypeProjection>,
override val annotations: List<IrCall>,
override val variance: Variance
) : IrType, IrTypeProjection {
annotations: List<IrCall>,
variance: Variance
) : IrTypeBase(annotations, variance), IrSimpleType, IrTypeProjection {
constructor(
classifier: IrClassifierSymbol,
@@ -27,15 +26,22 @@ class IrTypeImpl(
) : this(classifier, hasQuestionMark, arguments, annotations, Variance.INVARIANT)
constructor(
other: IrType,
other: IrSimpleType,
variance: Variance
) : this(other.classifier, other.hasQuestionMark, other.arguments, other.annotations, variance)
override val type: IrType get() = this
}
class IrTypeProjectionImpl internal constructor(
override val type: IrType,
override val variance: Variance
) : IrTypeProjection
fun makeTypeProjection(type: IrType, variance: Variance): IrTypeProjection =
if (type is IrTypeImpl && type.variance == variance)
type
else
IrTypeImpl(type, variance)
when {
type is IrTypeProjection && type.variance == variance -> type
type is IrSimpleType -> IrSimpleTypeImpl(type, variance)
type is IrDynamicType -> IrDynamicTypeImpl(type.annotations, variance)
type is IrErrorType -> IrErrorTypeImpl(type.annotations, variance)
else -> IrTypeProjectionImpl(type, variance)
}
@@ -0,0 +1,32 @@
/*
* 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.ir.types.impl
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.types.IrDynamicType
import org.jetbrains.kotlin.ir.types.IrErrorType
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.IrTypeProjection
import org.jetbrains.kotlin.types.Variance
abstract class IrTypeBase(
override val annotations: List<IrCall>,
override val variance: Variance
) : IrType, IrTypeProjection {
override val type: IrType get() = this
}
class IrErrorTypeImpl(
annotations: List<IrCall>,
variance: Variance
) : IrTypeBase(annotations, variance), IrErrorType
class IrDynamicTypeImpl(
annotations: List<IrCall>,
variance: Variance
) : IrTypeBase(annotations, variance), IrDynamicType, IrTypeProjection