From 6bc86bd42fa1380da0c991038be20209fc52e149 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Sun, 24 Jun 2018 01:42:32 +0300 Subject: [PATCH] IR: add `ClassifierDescriptor.toIrType` and `KotlinType.toIrType` (cherry picked from commit 3e09659) --- .../org/jetbrains/kotlin/ir/types/irTypes.kt | 36 +++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/irTypes.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/irTypes.kt index 4660d0d13e9..f59e38537a8 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/irTypes.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/types/irTypes.kt @@ -6,9 +6,12 @@ package org.jetbrains.kotlin.ir.types import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.descriptors.ClassifierDescriptor +import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol -import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeImpl -import org.jetbrains.kotlin.ir.types.impl.originalKotlinType +import org.jetbrains.kotlin.ir.symbols.impl.IrClassSymbolImpl +import org.jetbrains.kotlin.ir.symbols.impl.IrTypeParameterSymbolImpl +import org.jetbrains.kotlin.ir.types.impl.* import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.utils.addToStdlib.cast import org.jetbrains.kotlin.utils.addToStdlib.safeAs @@ -76,3 +79,32 @@ fun IrType.toKotlinType(): KotlinType { else -> TODO(toString()) } } + +fun ClassifierDescriptor.toIrType(hasQuestionMark: Boolean = false): IrType { + val symbol = getSymbol() + return IrSimpleTypeImpl(defaultType, symbol, hasQuestionMark, listOf(), listOf()) +} + +fun KotlinType.toIrType(): IrType? { + if (isDynamic()) return IrDynamicTypeImpl(this, listOf(), Variance.INVARIANT) + + val symbol = constructor.declarationDescriptor?.getSymbol() ?: return null + + val arguments = this.arguments.mapIndexed { i, projection -> + when (projection) { + is TypeProjectionImpl -> IrTypeProjectionImpl(projection.type.toIrType()!!, projection.projectionKind) + is StarProjectionImpl -> IrStarProjectionImpl + else -> error(projection) + } + } + + // TODO + val annotations = listOf() + return IrSimpleTypeImpl(this, symbol, isMarkedNullable, arguments, annotations) +} + +private fun ClassifierDescriptor.getSymbol(): IrClassifierSymbol = when (this) { + is ClassDescriptor -> IrClassSymbolImpl(this) + is TypeParameterDescriptor -> IrTypeParameterSymbolImpl(this) + else -> TODO() +} \ No newline at end of file