Initial annotation support
This commit is contained in:
committed by
Dmitry Petrov
parent
0bc3cc5e20
commit
4b6ddc4a9d
+25
-4
@@ -18,10 +18,10 @@ package org.jetbrains.kotlin.backend.jvm.codegen
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
|
||||
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
|
||||
import org.jetbrains.kotlin.backend.jvm.descriptors.JvmSpecialDescriptor
|
||||
import org.jetbrains.kotlin.backend.jvm.lower.FileClassDescriptor
|
||||
import org.jetbrains.kotlin.codegen.*
|
||||
import org.jetbrains.kotlin.codegen.ExpressionCodegen
|
||||
import org.jetbrains.kotlin.codegen.MemberCodegen.badDescriptor
|
||||
import org.jetbrains.kotlin.codegen.binding.CodegenBinding
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.name.SpecialNames
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils.isTopLevelDeclaration
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.OtherOrigin
|
||||
import org.jetbrains.kotlin.resolve.source.PsiSourceElement
|
||||
@@ -38,7 +39,7 @@ import org.jetbrains.org.objectweb.asm.Opcodes
|
||||
import org.jetbrains.org.objectweb.asm.Type
|
||||
import java.lang.RuntimeException
|
||||
|
||||
class ClassCodegen private constructor(val irClass: IrClass, val context: JvmBackendContext, val parentClassCodegen: ClassCodegen? = null) {
|
||||
class ClassCodegen private constructor(val irClass: IrClass, val context: JvmBackendContext, val parentClassCodegen: ClassCodegen? = null) : InnerClassConsumer {
|
||||
|
||||
private val innerClasses = mutableListOf<ClassDescriptor>()
|
||||
|
||||
@@ -69,6 +70,7 @@ class ClassCodegen private constructor(val irClass: IrClass, val context: JvmBac
|
||||
signature.superclassName,
|
||||
signature.interfaces.toTypedArray()
|
||||
)
|
||||
AnnotationCodegen.forClass(visitor.visitor, this, typeMapper).genAnnotations(descriptor, null)
|
||||
|
||||
irClass.declarations.forEach {
|
||||
generateDeclaration(it)
|
||||
@@ -121,8 +123,15 @@ class ClassCodegen private constructor(val irClass: IrClass, val context: JvmBac
|
||||
fun generateField(field: IrField) {
|
||||
val fieldType = typeMapper.mapType(field.descriptor)
|
||||
val fieldSignature = typeMapper.mapFieldSignature(field.descriptor.type, field.descriptor)
|
||||
visitor.newField(field.OtherOrigin, field.descriptor.calculateCommonFlags(), field.descriptor.name.asString(), fieldType.descriptor,
|
||||
fieldSignature, null/*TODO support default values*/)
|
||||
val fv = visitor.newField(field.OtherOrigin, field.descriptor.calculateCommonFlags(), field.descriptor.name.asString(), fieldType.descriptor,
|
||||
fieldSignature, null/*TODO support default values*/)
|
||||
|
||||
if (field.origin == JvmLoweredDeclarationOrigin.FIELD_FOR_ENUM_ENTRY) {
|
||||
AnnotationCodegen.forField(fv, this, typeMapper).genAnnotations(field.descriptor, null)
|
||||
}
|
||||
else {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fun generateMethod(method: IrFunction) {
|
||||
@@ -157,6 +166,18 @@ class ClassCodegen private constructor(val irClass: IrClass, val context: JvmBac
|
||||
return if (parentClassCodegen != null) descriptor else null
|
||||
}
|
||||
|
||||
// It's necessary for proper recovering of classId by plain string JVM descriptor when loading annotations
|
||||
// See FileBasedKotlinClass.convertAnnotationVisitor
|
||||
override fun addInnerClassInfoFromAnnotation(classDescriptor: ClassDescriptor) {
|
||||
var current: DeclarationDescriptor? = classDescriptor
|
||||
while (current != null && !isTopLevelDeclaration(current)) {
|
||||
if (current is ClassDescriptor) {
|
||||
innerClasses.add(current)
|
||||
}
|
||||
current = current.containingDeclaration
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun ClassDescriptor.calculateClassFlags(): Int {
|
||||
|
||||
+10
-5
@@ -313,11 +313,16 @@ class ExpressionCodegen(
|
||||
}
|
||||
|
||||
private fun generateLocal(descriptor: CallableDescriptor, type: Type): StackValue {
|
||||
val index = findLocalIndex(descriptor)
|
||||
StackValue.local(index, type).put(type, mv)
|
||||
return onStack(type)
|
||||
}
|
||||
|
||||
private fun findLocalIndex(descriptor: CallableDescriptor): Int {
|
||||
val index = frame.getIndex(descriptor).apply {
|
||||
if (this < 0) throw AssertionError("Non-mapped local variable descriptor: $descriptor")
|
||||
}
|
||||
StackValue.local(index, type).put(type, mv)
|
||||
return onStack(type)
|
||||
return index
|
||||
}
|
||||
|
||||
override fun visitGetObjectValue(expression: IrGetObjectValue, data: BlockInfo): StackValue {
|
||||
@@ -330,7 +335,7 @@ class ExpressionCodegen(
|
||||
|
||||
override fun visitSetVariable(expression: IrSetVariable, data: BlockInfo): StackValue {
|
||||
val value = expression.value.accept(this, data)
|
||||
StackValue.local(frame.getIndex(expression.descriptor), expression.descriptor.asmType).store(value, mv)
|
||||
StackValue.local(findLocalIndex(expression.descriptor), expression.descriptor.asmType).store(value, mv)
|
||||
return none()
|
||||
}
|
||||
|
||||
@@ -494,8 +499,8 @@ class ExpressionCodegen(
|
||||
val end = Label()
|
||||
|
||||
thenBranch.apply {
|
||||
gen(this, data)
|
||||
coerceNotToUnit(this.asmType, type)
|
||||
gen(this, type, data)
|
||||
//coerceNotToUnit(this.asmType, type)
|
||||
}
|
||||
|
||||
mv.goTo(end)
|
||||
|
||||
+5
-5
@@ -17,16 +17,13 @@
|
||||
package org.jetbrains.kotlin.backend.jvm.codegen
|
||||
|
||||
import org.jetbrains.kotlin.backend.jvm.lower.InitializersLowering
|
||||
import org.jetbrains.kotlin.codegen.AsmUtil
|
||||
import org.jetbrains.kotlin.codegen.*
|
||||
import org.jetbrains.kotlin.codegen.AsmUtil.isStaticMethod
|
||||
import org.jetbrains.kotlin.codegen.FrameMap
|
||||
import org.jetbrains.kotlin.codegen.JvmCodegenUtil
|
||||
import org.jetbrains.kotlin.codegen.OwnerKind
|
||||
import org.jetbrains.kotlin.codegen.FunctionCodegen
|
||||
import org.jetbrains.kotlin.codegen.state.GenerationState
|
||||
import org.jetbrains.kotlin.descriptors.ClassConstructorDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||
import org.jetbrains.kotlin.ir.util.dump
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
|
||||
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodParameterKind
|
||||
@@ -60,6 +57,9 @@ class FunctionCodegen(val irFunction: IrFunction, val classCodegen: ClassCodegen
|
||||
signature.asmMethod.name, signature.asmMethod.descriptor,
|
||||
signature.genericsSignature, null/*TODO support exception*/)
|
||||
|
||||
FunctionCodegen.generateMethodAnnotations(descriptor, signature.asmMethod, methodVisitor, classCodegen, state.typeMapper)
|
||||
FunctionCodegen.generateParameterAnnotations(descriptor, methodVisitor, signature, classCodegen, state)
|
||||
|
||||
if (!state.classBuilderMode.generateBodies || flags.and(Opcodes.ACC_ABSTRACT) != 0) {
|
||||
methodVisitor.visitEnd()
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user