From f704fd44af725e5798276cf8e7fc68d920d20d2c Mon Sep 17 00:00:00 2001 From: Svyatoslav Scherbina Date: Mon, 17 Jun 2019 16:37:00 +0300 Subject: [PATCH] Move some of relevant code to ReflectionSupport.kt --- .../backend/konan/lower/DelegationLowering.kt | 53 -------------- .../backend/konan/lower/ReflectionSupport.kt | 71 +++++++++++++++++++ 2 files changed, 71 insertions(+), 53 deletions(-) create mode 100644 backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ReflectionSupport.kt diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DelegationLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DelegationLowering.kt index 2bc9214999e..67e8d9946ae 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DelegationLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DelegationLowering.kt @@ -11,11 +11,7 @@ import org.jetbrains.kotlin.backend.common.descriptors.WrappedFieldDescriptor import org.jetbrains.kotlin.backend.common.lower.createIrBuilder import org.jetbrains.kotlin.backend.common.lower.irBlock import org.jetbrains.kotlin.backend.konan.Context -import org.jetbrains.kotlin.backend.konan.InteropFqNames import org.jetbrains.kotlin.backend.konan.descriptors.synthesizedName -import org.jetbrains.kotlin.backend.konan.KonanBackendContext -import org.jetbrains.kotlin.backend.konan.ir.typeWithStarProjections -import org.jetbrains.kotlin.backend.konan.isObjCClass import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptorImpl import org.jetbrains.kotlin.descriptors.annotations.Annotations @@ -26,8 +22,6 @@ import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrLocalDelegatedPropertyReference import org.jetbrains.kotlin.ir.expressions.IrPropertyReference import org.jetbrains.kotlin.ir.expressions.impl.* -import org.jetbrains.kotlin.ir.symbols.IrClassSymbol -import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol import org.jetbrains.kotlin.ir.symbols.IrConstructorSymbol import org.jetbrains.kotlin.ir.symbols.impl.IrFieldSymbolImpl import org.jetbrains.kotlin.ir.types.* @@ -36,8 +30,6 @@ import org.jetbrains.kotlin.ir.types.typeWith import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid import org.jetbrains.kotlin.name.Name -import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe -import org.jetbrains.kotlin.resolve.descriptorUtil.getAllSuperClassifiers internal class PropertyDelegationLowering(val context: Context) : FileLoweringPass { private var tempIndex = 0 @@ -303,48 +295,3 @@ internal class PropertyDelegationLowering(val context: Context) : FileLoweringPa private object DECLARATION_ORIGIN_KPROPERTIES_FOR_DELEGATION : IrDeclarationOriginImpl("KPROPERTIES_FOR_DELEGATION") } - -internal fun IrBuilderWithScope.irKType(context: KonanBackendContext, type: IrType): IrExpression { - val kTypeImplSymbol = context.ir.symbols.kTypeImpl - val kTypeImplForGenericsSymbol = context.ir.symbols.kTypeImplForGenerics - - val kTypeImplConstructorSymbol = kTypeImplSymbol.constructors.single() - val kTypeImplForGenericsConstructorSymbol = kTypeImplForGenericsSymbol.constructors.single() - - val classifierOrNull = type.classifierOrNull - - return if (classifierOrNull !is IrClassSymbol) { - // IrTypeParameterSymbol - irCall(kTypeImplForGenericsConstructorSymbol) - } else { - val returnKClass = irKClass(context, classifierOrNull) - irCall(kTypeImplConstructorSymbol).apply { - putValueArgument(0, returnKClass) - putValueArgument(1, irBoolean(type.isMarkedNullable())) - } - } -} - -internal fun IrBuilderWithScope.irKClass(context: KonanBackendContext, symbol: IrClassifierSymbol): IrExpression { - val symbols = context.ir.symbols - return when { - symbol !is IrClassSymbol -> // E.g. for `T::class` in a body of an inline function itself. - irCall(symbols.ThrowNullPointerException.owner) - - symbol.descriptor.isObjCClass() -> - irKClassUnsupported(context, "KClass for Objective-C classes is not supported yet") - - symbol.descriptor.getAllSuperClassifiers().any { - it is ClassDescriptor && it.fqNameUnsafe == InteropFqNames.nativePointed - } -> irKClassUnsupported(context, "KClass for interop types is not supported yet") - - else -> irCall(symbols.kClassImplConstructor.owner).apply { - putValueArgument(0, irCall(symbols.getClassTypeInfo, listOf(symbol.typeWithStarProjections))) - } - } -} - -private fun IrBuilderWithScope.irKClassUnsupported(context: KonanBackendContext, message: String) = - irCall(context.ir.symbols.kClassUnsupportedImplConstructor.owner).apply { - putValueArgument(0, irString(message)) - } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ReflectionSupport.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ReflectionSupport.kt new file mode 100644 index 00000000000..1a7ff0a91ac --- /dev/null +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/ReflectionSupport.kt @@ -0,0 +1,71 @@ +/* + * Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +package org.jetbrains.kotlin.backend.konan.lower + +import org.jetbrains.kotlin.backend.konan.InteropFqNames +import org.jetbrains.kotlin.backend.konan.KonanBackendContext +import org.jetbrains.kotlin.backend.konan.ir.typeWithStarProjections +import org.jetbrains.kotlin.backend.konan.isObjCClass +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.ir.builders.IrBuilderWithScope +import org.jetbrains.kotlin.ir.builders.irBoolean +import org.jetbrains.kotlin.ir.builders.irCall +import org.jetbrains.kotlin.ir.builders.irString +import org.jetbrains.kotlin.ir.expressions.IrExpression +import org.jetbrains.kotlin.ir.symbols.IrClassSymbol +import org.jetbrains.kotlin.ir.symbols.IrClassifierSymbol +import org.jetbrains.kotlin.ir.types.IrType +import org.jetbrains.kotlin.ir.types.classifierOrNull +import org.jetbrains.kotlin.ir.types.isMarkedNullable +import org.jetbrains.kotlin.ir.util.constructors +import org.jetbrains.kotlin.ir.util.irCall +import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe +import org.jetbrains.kotlin.resolve.descriptorUtil.getAllSuperClassifiers + +internal fun IrBuilderWithScope.irKType(context: KonanBackendContext, type: IrType): IrExpression { + val kTypeImplSymbol = context.ir.symbols.kTypeImpl + val kTypeImplForGenericsSymbol = context.ir.symbols.kTypeImplForGenerics + + val kTypeImplConstructorSymbol = kTypeImplSymbol.constructors.single() + val kTypeImplForGenericsConstructorSymbol = kTypeImplForGenericsSymbol.constructors.single() + + val classifierOrNull = type.classifierOrNull + + return if (classifierOrNull !is IrClassSymbol) { + // IrTypeParameterSymbol + irCall(kTypeImplForGenericsConstructorSymbol) + } else { + val returnKClass = irKClass(context, classifierOrNull) + irCall(kTypeImplConstructorSymbol).apply { + putValueArgument(0, returnKClass) + putValueArgument(1, irBoolean(type.isMarkedNullable())) + } + } +} + +internal fun IrBuilderWithScope.irKClass(context: KonanBackendContext, symbol: IrClassifierSymbol): IrExpression { + val symbols = context.ir.symbols + return when { + symbol !is IrClassSymbol -> // E.g. for `T::class` in a body of an inline function itself. + irCall(symbols.ThrowNullPointerException.owner) + + symbol.descriptor.isObjCClass() -> + irKClassUnsupported(context, "KClass for Objective-C classes is not supported yet") + + symbol.descriptor.getAllSuperClassifiers().any { + it is ClassDescriptor && it.fqNameUnsafe == InteropFqNames.nativePointed + } -> irKClassUnsupported(context, "KClass for interop types is not supported yet") + + else -> irCall(symbols.kClassImplConstructor.owner).apply { + putValueArgument(0, irCall(symbols.getClassTypeInfo, listOf(symbol.typeWithStarProjections))) + } + } +} + +private fun IrBuilderWithScope.irKClassUnsupported(context: KonanBackendContext, message: String) = + irCall(context.ir.symbols.kClassUnsupportedImplConstructor.owner).apply { + putValueArgument(0, irString(message)) + }