From 1be28d603250ca5623e15eec2455763e7d699e84 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Fri, 20 Apr 2018 12:13:52 +0300 Subject: [PATCH] IrType -> IrDynamicType, IrErrorType, IrSimpleType --- .../kotlin/psi2ir/types/TypeTranslator.kt | 26 +++++++++------ .../org/jetbrains/kotlin/ir/types/IrType.kt | 9 +++++- .../{IrTypeImpl.kt => IrSimpleTypeImpl.kt} | 30 ++++++++++------- .../kotlin/ir/types/impl/IrTypeBase.kt | 32 +++++++++++++++++++ 4 files changed, 74 insertions(+), 23 deletions(-) rename compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/{IrTypeImpl.kt => IrSimpleTypeImpl.kt} (60%) create mode 100644 compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrTypeBase.kt diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/types/TypeTranslator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/types/TypeTranslator.kt index 1e10397f2c7..8d108068a93 100644 --- a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/types/TypeTranslator.kt +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/types/TypeTranslator.kt @@ -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}") } } diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrType.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrType.kt index 0e8f5713a5a..51ebca098a7 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrType.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/IrType.kt @@ -10,10 +10,17 @@ import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol import org.jetbrains.kotlin.types.Variance interface IrType { + val annotations: List +} + +interface IrErrorType : IrType + +interface IrDynamicType : IrType + +interface IrSimpleType : IrType { val classifier: IrClassifierSymbol val hasQuestionMark: Boolean val arguments: List - val annotations: List } interface IrTypeProjection { diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrTypeImpl.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrSimpleTypeImpl.kt similarity index 60% rename from compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrTypeImpl.kt rename to compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrSimpleTypeImpl.kt index 35c7b2c3eee..44fc9bf9f3a 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrTypeImpl.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrSimpleTypeImpl.kt @@ -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, - override val annotations: List, - override val variance: Variance -) : IrType, IrTypeProjection { + annotations: List, + 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) \ No newline at end of file + 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) + } \ No newline at end of file diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrTypeBase.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrTypeBase.kt new file mode 100644 index 00000000000..d09d7c6192c --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/impl/IrTypeBase.kt @@ -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, + override val variance: Variance +) : IrType, IrTypeProjection { + + override val type: IrType get() = this + +} + +class IrErrorTypeImpl( + annotations: List, + variance: Variance +) : IrTypeBase(annotations, variance), IrErrorType + +class IrDynamicTypeImpl( + annotations: List, + variance: Variance +) : IrTypeBase(annotations, variance), IrDynamicType, IrTypeProjection \ No newline at end of file