From bbac1d802f753f269c797a3326f7ef32e0adff04 Mon Sep 17 00:00:00 2001 From: Georgy Bronnikov Date: Mon, 15 Oct 2018 17:33:30 +0300 Subject: [PATCH] JVM_IR. Support annotation classes with JvmField fields --- .../org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt | 3 +-- .../jetbrains/kotlin/backend/jvm/codegen/FunctionCodegen.kt | 2 +- .../backend/jvm/lower/ConstAndJvmFieldPropertiesLowering.kt | 2 +- .../backend/jvm/lower/MoveCompanionObjectFieldsLowering.kt | 3 ++- .../codegen/box/annotations/nestedClassesInAnnotations.kt | 1 - compiler/testData/codegen/box/jvmField/annotationCompanion.kt | 1 - .../codegen/box/jvmField/annotationCompanionWithJava.kt | 1 - 7 files changed, 5 insertions(+), 8 deletions(-) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt index ba3838e889c..24963f461a0 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ClassCodegen.kt @@ -289,8 +289,7 @@ private val MemberDescriptor.effectiveModality: Modality } } if (DescriptorUtils.isSealedClass(this) || - DescriptorUtils.isAnnotationClass(this) || - DescriptorUtils.isAnnotationClass(this.containingDeclaration) + DescriptorUtils.isAnnotationClass(this) ) { return Modality.ABSTRACT } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/FunctionCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/FunctionCodegen.kt index b8e1aacb104..ab658ecc3ed 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/FunctionCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/FunctionCodegen.kt @@ -76,7 +76,7 @@ open class FunctionCodegen(private val irFunction: IrFunction, private val class val deprecation = if (irFunction.hasAnnotation(FQ_NAMES.deprecated)) Opcodes.ACC_DEPRECATED else 0 val bridgeFlag = 0 //TODO val modalityFlag = when ((irFunction as? IrSimpleFunction)?.modality) { - Modality.FINAL -> if (!classCodegen.irClass.isAnnotationClass) Opcodes.ACC_FINAL else Opcodes.ACC_ABSTRACT + Modality.FINAL -> if (!classCodegen.irClass.isAnnotationClass || irFunction.isStatic) Opcodes.ACC_FINAL else Opcodes.ACC_ABSTRACT Modality.ABSTRACT -> Opcodes.ACC_ABSTRACT else -> if (classCodegen.irClass.isJvmInterface && irFunction.body == null) Opcodes.ACC_ABSTRACT else 0 //TODO transform interface modality on lowering to DefaultImpls } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/ConstAndJvmFieldPropertiesLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/ConstAndJvmFieldPropertiesLowering.kt index ee88e091831..9aaf2deb965 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/ConstAndJvmFieldPropertiesLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/ConstAndJvmFieldPropertiesLowering.kt @@ -40,7 +40,7 @@ class ConstAndJvmFieldPropertiesLowering(val context: CommonBackendContext) : Ir val irProperty = irSimpleFunction.correspondingProperty ?: return super.visitCall(expression) if (irProperty.isConst) { - (irProperty.backingField?.initializer?.expression as? IrConst<*>)?.let { return it } + (irProperty.backingField!!.initializer!!.expression as IrConst<*>).let { return it } } if (irProperty.backingField?.hasAnnotation(JVM_FIELD_ANNOTATION_FQ_NAME) == true) { diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/MoveCompanionObjectFieldsLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/MoveCompanionObjectFieldsLowering.kt index db4b6985aa9..50cd38b3e0a 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/MoveCompanionObjectFieldsLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/MoveCompanionObjectFieldsLowering.kt @@ -24,6 +24,7 @@ import org.jetbrains.kotlin.ir.symbols.impl.IrAnonymousInitializerSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrFieldSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrVariableSymbolImpl import org.jetbrains.kotlin.ir.util.hasAnnotation +import org.jetbrains.kotlin.ir.util.isAnnotationClass import org.jetbrains.kotlin.ir.util.isInterface import org.jetbrains.kotlin.ir.util.isObject import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid @@ -60,7 +61,7 @@ class MoveCompanionObjectFieldsLowering(val context: CommonBackendContext) : Cla val companion = irClass.declarations.find { it is IrClass && it.isCompanion } as IrClass? ?: return - if (irClass.isInterface && !companion.allFieldsAreJvmField()) return + if ((irClass.isInterface || irClass.isAnnotationClass) && !companion.allFieldsAreJvmField()) return companion.declarations.forEach { when (it) { is IrProperty -> { diff --git a/compiler/testData/codegen/box/annotations/nestedClassesInAnnotations.kt b/compiler/testData/codegen/box/annotations/nestedClassesInAnnotations.kt index a42ebdbd6a9..81cc76869d7 100644 --- a/compiler/testData/codegen/box/annotations/nestedClassesInAnnotations.kt +++ b/compiler/testData/codegen/box/annotations/nestedClassesInAnnotations.kt @@ -1,5 +1,4 @@ // !LANGUAGE: +NestedClassesInAnnotations -// IGNORE_BACKEND: JVM_IR // IGNORE_BACKEND: NATIVE annotation class Foo(val kind: Kind) { diff --git a/compiler/testData/codegen/box/jvmField/annotationCompanion.kt b/compiler/testData/codegen/box/jvmField/annotationCompanion.kt index f51034e952a..26741f1dda1 100644 --- a/compiler/testData/codegen/box/jvmField/annotationCompanion.kt +++ b/compiler/testData/codegen/box/jvmField/annotationCompanion.kt @@ -1,5 +1,4 @@ // !LANGUAGE: +JvmFieldInInterface +NestedClassesInAnnotations -// IGNORE_BACKEND: JVM_IR // TARGET_BACKEND: JVM // WITH_RUNTIME diff --git a/compiler/testData/codegen/box/jvmField/annotationCompanionWithJava.kt b/compiler/testData/codegen/box/jvmField/annotationCompanionWithJava.kt index dd153ad4792..1b8002ff7da 100644 --- a/compiler/testData/codegen/box/jvmField/annotationCompanionWithJava.kt +++ b/compiler/testData/codegen/box/jvmField/annotationCompanionWithJava.kt @@ -1,5 +1,4 @@ // !LANGUAGE: +JvmFieldInInterface +NestedClassesInAnnotations -// IGNORE_BACKEND: JVM_IR // TARGET_BACKEND: JVM // WITH_RUNTIME