From 6f72c681ed6a19433cd5fe8f36f04d42198931c7 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 9 Jul 2021 19:00:31 +0200 Subject: [PATCH] JVM IR: fix name and parent of JvmSymbols.kClassJava In contrast to other top-level functions/properties declared in JvmSymbols (unsafeCoerce, signatureString, etc), kClassJava refers to a real symbol from the library. Since not all calls to it are intrinsified (see KClassJavaProperty.kt:30), it makes sense to allow to reference it when constructing IR. For that, it needs to have the correct file facade as the parent, and the JvmName annotation so that its name is mapped correctly in the codegen. Co-authored-by: Leonid Startsev --- .../org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt | 13 ++++++++++++- .../ir/expressions/impl/IrConstructorCallImpl.kt | 6 +++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt index 6f72debb4e2..0e10d9ac3ee 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt @@ -689,12 +689,23 @@ class JvmSymbols( val genericToArray: IrSimpleFunctionSymbol = collectionToArrayClass.functions.single { it.owner.name.asString() == "toArray" && it.owner.valueParameters.size == 2 } + val jvmName: IrClassSymbol = createClass(FqName("kotlin.jvm.JvmName"), ClassKind.ANNOTATION_CLASS) { klass -> + klass.addConstructor().apply { + addValueParameter("name", irBuiltIns.stringType) + } + } + val kClassJava: IrPropertySymbol = irFactory.buildProperty { name = Name.identifier("java") }.apply { - parent = kotlinJvmPackage + parent = createClass(FqName("kotlin.jvm.JvmClassMappingKt")).owner addGetter().apply { + annotations = listOf( + IrConstructorCallImpl.fromSymbolOwner(jvmName.typeWith(), jvmName.constructors.single()).apply { + putValueArgument(0, IrConstImpl.string(UNDEFINED_OFFSET, UNDEFINED_OFFSET, irBuiltIns.stringType, "getJavaClass")) + } + ) addExtensionReceiver(irBuiltIns.kClassClass.starProjectedType) returnType = javaLangClass.starProjectedType } diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/impl/IrConstructorCallImpl.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/impl/IrConstructorCallImpl.kt index 270a3ebbc5d..ca03b0981c2 100644 --- a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/impl/IrConstructorCallImpl.kt +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/impl/IrConstructorCallImpl.kt @@ -88,7 +88,11 @@ class IrConstructorCallImpl( return fromSymbolOwner(startOffset, endOffset, type, constructorSymbol, classTypeParametersCount, origin) } - fun fromSymbolOwner(type: IrType, constructorSymbol: IrConstructorSymbol, origin: IrStatementOrigin? = null) = + fun fromSymbolOwner( + type: IrType, + constructorSymbol: IrConstructorSymbol, + origin: IrStatementOrigin? = null + ): IrConstructorCallImpl = fromSymbolOwner( UNDEFINED_OFFSET, UNDEFINED_OFFSET, type, constructorSymbol, constructorSymbol.owner.parentAsClass.typeParameters.size, origin