JVM_IR. Support type annotations

This commit is contained in:
Mikhael Bogdanov
2020-01-28 13:06:22 +01:00
parent e4258e528f
commit 6c07dbf351
14 changed files with 191 additions and 70 deletions
@@ -40,6 +40,7 @@ import org.jetbrains.kotlin.types.FlexibleType;
import org.jetbrains.kotlin.types.FlexibleTypesKt;
import org.jetbrains.kotlin.types.KotlinType;
import org.jetbrains.kotlin.types.TypeUtils;
import org.jetbrains.kotlin.types.checker.SimpleClassicTypeSystemContext;
import org.jetbrains.org.objectweb.asm.*;
import java.lang.annotation.*;
@@ -675,9 +676,9 @@ public abstract class AnnotationCodegen {
return;
}
Iterable<TypePathInfo> infos =
new TypeAnnotationCollector().collectTypeAnnotations(type, TypeReference.METHOD_FORMAL_PARAMETER);
for (TypePathInfo info : infos) {
Iterable<TypePathInfo<AnnotationDescriptor>> infos =
new PsiTypeAnnotationCollector().collectTypeAnnotations(type);
for (TypePathInfo<AnnotationDescriptor> info : infos) {
for (AnnotationDescriptor annotationDescriptor : info.getAnnotations()) {
genAnnotation(annotationDescriptor, info.getPath(), true);
}
@@ -5,26 +5,29 @@
package org.jetbrains.kotlin.codegen
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor
import org.jetbrains.kotlin.load.kotlin.FileBasedKotlinClass
import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinarySourceElement
import org.jetbrains.kotlin.resolve.descriptorUtil.annotationClass
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeSystemCommonBackendContext
import org.jetbrains.kotlin.types.checker.SimpleClassicTypeSystemContext
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.TypeVariance
import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.TypePath
class TypePathInfo(
class TypePathInfo<T>(
val path: TypePath?,
val annotations: List<AnnotationDescriptor>
val annotations: List<T>
)
private class State(val type: Int, val path: MutableList<String>) {
private class State<T>(val path: MutableList<String>) {
val results = arrayListOf<TypePathInfo>()
val results = arrayListOf<TypePathInfo<T>>()
fun addStep(step: String) {
@@ -35,56 +38,69 @@ private class State(val type: Int, val path: MutableList<String>) {
path.removeAt(path.lastIndex)
}
fun rememberAnnotations(annotations: List<AnnotationDescriptor>) {
fun rememberAnnotations(annotations: List<T>) {
results.add(TypePathInfo(TypePath.fromString(path.joinToString("")), annotations))
}
}
class TypeAnnotationCollector {
class PsiTypeAnnotationCollector : TypeAnnotationCollector<AnnotationDescriptor>(SimpleClassicTypeSystemContext) {
private lateinit var state: State
override fun KotlinTypeMarker.extractAnnotations(): List<AnnotationDescriptor> {
require(this is KotlinType)
return annotations.filter {
//We only generate annotations which have the TYPE_USE Java target.
// Those are type annotations which were compiled with JVM target bytecode version 1.8 or greater
isCompiledToJvm8OrHigher(it.annotationClass)
}
}
}
fun collectTypeAnnotations(kotlinType: KotlinType, annotationType: Int): ArrayList<TypePathInfo> {
state = State(annotationType, arrayListOf())
kotlinType.collectTypeAnnotations()
abstract class TypeAnnotationCollector<T>(val context: TypeSystemCommonBackendContext) {
private lateinit var state: State<T>
fun collectTypeAnnotations(kotlinType: KotlinTypeMarker): ArrayList<TypePathInfo<T>> {
state = State(arrayListOf())
kotlinType.gatherTypeAnnotations()
return state.results
}
private fun KotlinType.collectTypeAnnotations() {
if (isFlexible()) {
return upperIfFlexible().collectTypeAnnotations()
} else if ((this.constructor.declarationDescriptor as? ClassDescriptor)?.isInner == true) {
//skip inner classes for now it's not clear should type annotations on outer be supported or not
return
}
private fun KotlinTypeMarker.gatherTypeAnnotations() {
with(context) {
if (isFlexible()) {
return upperBoundIfFlexible().gatherTypeAnnotations()
} else if (typeConstructor().isInnerClass()) {
//skip inner classes for now it's not clear should type annotations on outer be supported or not
return
}
typeAnnotations.takeIf { it.isNotEmpty() }?.let { state.rememberAnnotations(it) }
extractAnnotations().takeIf { it.isNotEmpty() }?.let { state.rememberAnnotations(it) }
arguments.forEachIndexed { index, type ->
//skip in/out variance for now it's not clear should type annotations on wildcard bound be supported or not
if (type.projectionKind == Variance.INVARIANT) {
when {
KotlinBuiltIns.isArray(this) -> type.type.process("[")
else -> type.type.process("$index;")
for (index in 0 until argumentsCount()) {
val type = getArgument(index)
//skip in/out variance for now it's not clear should type annotations on wildcard bound be supported or not
if (type.getVariance() == TypeVariance.INV) {
when {
this@gatherTypeAnnotations.isArrayOrNullableArray() -> type.getType().process("[")
else -> type.getType().process("$index;")
}
}
}
}
}
fun KotlinType.process(step: String) {
fun KotlinTypeMarker.process(step: String) {
state.addStep(step)
this.collectTypeAnnotations()
this.gatherTypeAnnotations()
state.removeStep(step)
}
private val KotlinType.typeAnnotations
get() = annotations.filter {
//We only generate annotations which have the TYPE_USE Java target.
// Those are type annotations which were compiled with JVM target bytecode version 1.8 or greater
(it.annotationClass as? DeserializedClassDescriptor)?.let { classDescriptor ->
((classDescriptor.source as? KotlinJvmBinarySourceElement)?.binaryClass as? FileBasedKotlinClass)?.classVersion ?: 0 >= Opcodes.V1_8
} ?: true
}
abstract fun KotlinTypeMarker.extractAnnotations(): List<T>
fun isCompiledToJvm8OrHigher(descriptor: ClassDescriptor?): Boolean =
(descriptor as? DeserializedClassDescriptor)?.let { classDescriptor ->
((classDescriptor.source as? KotlinJvmBinarySourceElement)?.binaryClass as? FileBasedKotlinClass)?.classVersion ?: 0 >= Opcodes.V1_8
} ?: true
}
@@ -461,6 +461,10 @@ interface ConeTypeContext : TypeSystemContext, TypeSystemOptimizationContext, Ty
return toFirRegularClass()?.isInline == true
}
override fun TypeConstructorMarker.isInnerClass(): Boolean {
return toFirRegularClass()?.isInner == true
}
override fun TypeParameterMarker.getRepresentativeUpperBound(): KotlinTypeMarker {
require(this is FirTypeParameterSymbol)
return this.fir.bounds.getOrNull(0)?.let { (it as? FirResolvedTypeRef)?.type }
@@ -23,6 +23,10 @@ import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
import org.jetbrains.kotlin.backend.jvm.JvmLoweredDeclarationOrigin
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.codegen.AsmUtil
import org.jetbrains.kotlin.codegen.TypeAnnotationCollector
import org.jetbrains.kotlin.codegen.TypePathInfo
import org.jetbrains.kotlin.config.JVMConfigurationKeys
import org.jetbrains.kotlin.config.JvmTarget.JVM_1_6
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.annotations.KotlinRetention
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
@@ -33,18 +37,21 @@ import org.jetbrains.kotlin.ir.types.classifierOrNull
import org.jetbrains.kotlin.ir.types.isMarkedNullable
import org.jetbrains.kotlin.ir.types.isNullable
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.load.kotlin.FileBasedKotlinClass
import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinarySourceElement
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.checkers.ExpectedActualDeclarationChecker
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedClassDescriptor
import org.jetbrains.kotlin.synthetic.isVisibleOutside
import org.jetbrains.org.objectweb.asm.AnnotationVisitor
import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.kotlin.types.TypeSystemCommonBackendContext
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.org.objectweb.asm.*
import java.lang.annotation.RetentionPolicy
class AnnotationCodegen(
abstract class AnnotationCodegen(
private val innerClassConsumer: InnerClassConsumer,
context: JvmBackendContext,
private val visitAnnotation: (descriptor: String, visible: Boolean) -> AnnotationVisitor
private val context: JvmBackendContext
) {
private val typeMapper = context.typeMapper
private val methodSignatureMapper = context.methodSignatureMapper
@@ -52,7 +59,7 @@ class AnnotationCodegen(
/**
* @param returnType can be null if not applicable (e.g. [annotated] is a class)
*/
fun genAnnotations(annotated: IrAnnotationContainer?, returnType: Type?) {
fun genAnnotations(annotated: IrAnnotationContainer?, returnType: Type?, typeForTypeAnnotations: IrType?) {
if (annotated == null) return
val annotationDescriptorsAlreadyPresent = mutableSetOf<String>()
@@ -84,14 +91,26 @@ class AnnotationCodegen(
}
}
genAnnotation(annotation)?.let { descriptor ->
genAnnotation(annotation, null, false)?.let { descriptor ->
annotationDescriptorsAlreadyPresent.add(descriptor)
}
}
generateAdditionalAnnotations(annotated, returnType, annotationDescriptorsAlreadyPresent)
generateTypeAnnotations(annotated, typeForTypeAnnotations)
}
abstract fun visitAnnotation(descr: String?, visible: Boolean): AnnotationVisitor
open fun visitTypeAnnotation(
descr: String?,
path: TypePath?,
visible: Boolean,
): AnnotationVisitor {
throw RuntimeException("Not implemented")
}
private fun generateAdditionalAnnotations(
annotated: IrAnnotationContainer,
returnType: Type?,
@@ -152,7 +171,7 @@ class AnnotationCodegen(
visitor.visitEnd()
}
private fun genAnnotation(annotation: IrConstructorCall): String? {
private fun genAnnotation(annotation: IrConstructorCall, path: TypePath?, isTypeAnnotation: Boolean): String? {
val annotationClass = annotation.annotationClass
val retentionPolicy = getRetentionPolicy(annotationClass)
if (retentionPolicy == RetentionPolicy.SOURCE) return null
@@ -167,7 +186,9 @@ class AnnotationCodegen(
innerClassConsumer.addInnerClassInfoFromAnnotation(annotationClass)
val asmTypeDescriptor = typeMapper.mapType(annotation.type).descriptor
val annotationVisitor = visitAnnotation(asmTypeDescriptor, retentionPolicy == RetentionPolicy.RUNTIME)
val annotationVisitor =
if (!isTypeAnnotation) visitAnnotation(asmTypeDescriptor, retentionPolicy == RetentionPolicy.RUNTIME) else
visitTypeAnnotation(asmTypeDescriptor, path, retentionPolicy == RetentionPolicy.RUNTIME)
genAnnotationArguments(annotation, annotationVisitor)
annotationVisitor.visitEnd()
@@ -281,6 +302,40 @@ class AnnotationCodegen(
val IrConstructorCall.annotationClass get() = symbol.owner.parentAsClass
}
private fun generateTypeAnnotations(
annotated: IrAnnotationContainer,
type: IrType?
) {
if ((annotated as? IrDeclaration)?.origin == JvmLoweredDeclarationOrigin.SYNTHETIC_ACCESSOR ||
type == null || context.state.target === JVM_1_6 ||
!context.state.configuration.getBoolean(JVMConfigurationKeys.EMIT_JVM_TYPE_ANNOTATIONS)
) {
return
}
val infos: Iterable<TypePathInfo<IrConstructorCall>> =
IrTypeAnnotationCollector(context.typeMapper.typeSystem).collectTypeAnnotations(type)
for (info in infos) {
for (annotation in info.annotations) {
genAnnotation(annotation, info.path, true)
}
}
}
private class IrTypeAnnotationCollector(context: TypeSystemCommonBackendContext) : TypeAnnotationCollector<IrConstructorCall>(context) {
override fun KotlinTypeMarker.extractAnnotations(): List<IrConstructorCall> {
require(this is IrType)
return annotations.filter {
//We only generate annotations which have the TYPE_USE Java target.
// Those are type annotations which were compiled with JVM target bytecode version 1.8 or greater
(it.annotationClass.origin != IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB &&
it.annotationClass.origin != IrDeclarationOrigin.IR_EXTERNAL_JAVA_DECLARATION_STUB) ||
isCompiledToJvm8OrHigher(it.annotationClass.descriptor)
}
}
}
}
interface InnerClassConsumer {
@@ -45,8 +45,7 @@ import org.jetbrains.kotlin.resolve.jvm.diagnostics.OtherOrigin
import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmClassSignature
import org.jetbrains.kotlin.serialization.DescriptorSerializer
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.org.objectweb.asm.*
import java.io.File
open class ClassCodegen protected constructor(
@@ -125,7 +124,15 @@ open class ClassCodegen protected constructor(
signature.superclassName,
signature.interfaces.toTypedArray()
)
AnnotationCodegen(this, context, visitor.visitor::visitAnnotation).genAnnotations(irClass, null)
object : AnnotationCodegen(this@ClassCodegen, context) {
override fun visitAnnotation(descr: String?, visible: Boolean): AnnotationVisitor {
return visitor.visitor.visitAnnotation(descr, visible)
}
}.genAnnotations(
irClass,
null,
null
)
val nestedClasses = irClass.declarations.mapNotNull { declaration ->
if (declaration is IrClass) {
@@ -328,7 +335,15 @@ open class ClassCodegen protected constructor(
fieldSignature, (field.initializer?.expression as? IrConst<*>)?.value
)
AnnotationCodegen(this, context, fv::visitAnnotation).genAnnotations(field, fieldType)
object : AnnotationCodegen(this@ClassCodegen, context) {
override fun visitAnnotation(descr: String?, visible: Boolean): AnnotationVisitor {
return fv.visitAnnotation(descr, visible)
}
override fun visitTypeAnnotation(descr: String?, path: TypePath?, visible: Boolean): AnnotationVisitor {
return fv.visitTypeAnnotation(TypeReference.newTypeReference(TypeReference.FIELD).value,path, descr, visible)
}
}.genAnnotations(field, fieldType, field.type)
val descriptor = field.metadata?.descriptor
if (descriptor != null) {
@@ -34,14 +34,13 @@ import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance
import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.kotlin.utils.sure
import org.jetbrains.org.objectweb.asm.MethodVisitor
import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.*
import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter
open class FunctionCodegen(
private val irFunction: IrFunction,
private val classCodegen: ClassCodegen,
private val inlinedInto: ExpressionCodegen? = null
private val inlinedInto: ExpressionCodegen? = null,
) {
val context = classCodegen.context
val state = classCodegen.state
@@ -65,9 +64,20 @@ open class FunctionCodegen(
}
if (irFunction.origin != IrDeclarationOrigin.FUNCTION_FOR_DEFAULT_PARAMETER) {
AnnotationCodegen(classCodegen, context, methodVisitor::visitAnnotation).genAnnotations(
object : AnnotationCodegen(classCodegen, context) {
override fun visitAnnotation(descr: String?, visible: Boolean): AnnotationVisitor {
return methodVisitor.visitAnnotation(descr, visible)
}
override fun visitTypeAnnotation(descr: String?, path: TypePath?, visible: Boolean): AnnotationVisitor {
return methodVisitor.visitTypeAnnotation(
TypeReference.newTypeReference(TypeReference.METHOD_RETURN).value, path, descr, visible
)
}
}.genAnnotations(
functionView,
signature.asmMethod.returnType
signature.asmMethod.returnType,
irFunction.returnType
)
// Not generating parameter annotations for default stubs fixes KT-7892, though
// this certainly looks like a workaround for a javac bug.
@@ -216,7 +226,14 @@ open class FunctionCodegen(
private fun generateAnnotationDefaultValueIfNeeded(methodVisitor: MethodVisitor) {
getAnnotationDefaultValueExpression()?.let { defaultValueExpression ->
val annotationCodegen = AnnotationCodegen(classCodegen, context) { _, _ -> methodVisitor.visitAnnotationDefault() }
val annotationCodegen = object: AnnotationCodegen(
classCodegen,
context
) {
override fun visitAnnotation(descr: String?, visible: Boolean): AnnotationVisitor {
return methodVisitor.visitAnnotationDefault()
}
}
annotationCodegen.generateAnnotationDefaultValue(defaultValueExpression)
}
}
@@ -282,13 +299,22 @@ private fun generateParameterAnnotations(
}
if (!kind.isSkippedInGenericSignature) {
AnnotationCodegen(innerClassConsumer, context) { descriptor, visible ->
mv.visitParameterAnnotation(
i - syntheticParameterCount,
descriptor,
visible
)
}.genAnnotations(annotated, parameterSignature.asmType)
object : AnnotationCodegen(innerClassConsumer, context) {
override fun visitAnnotation(descr: String?, visible: Boolean): AnnotationVisitor {
return mv.visitParameterAnnotation(
i - syntheticParameterCount,
descr,
visible
)
}
override fun visitTypeAnnotation(descr: String?, path: TypePath?, visible: Boolean): AnnotationVisitor {
return mv.visitTypeAnnotation(
TypeReference.newFormalParameterReference(i - syntheticParameterCount).value,
path, descr, visible
)
}
}.genAnnotations(annotated, parameterSignature.asmType, annotated?.type)
}
}
}
@@ -310,6 +310,9 @@ interface IrTypeSystemContext : TypeSystemContext, TypeSystemCommonSuperTypesCon
override fun TypeConstructorMarker.isInlineClass(): Boolean =
(this as? IrClassSymbol)?.owner?.isInline == true
override fun TypeConstructorMarker.isInnerClass(): Boolean =
(this as? IrClassSymbol)?.owner?.isInner == true
override fun TypeParameterMarker.getRepresentativeUpperBound(): KotlinTypeMarker =
(this as IrTypeParameterSymbol).owner.superTypes.firstOrNull {
val irClass = it.classOrNull?.owner ?: return@firstOrNull false
@@ -1,7 +1,6 @@
// KOTLIN_CONFIGURATION_FLAGS: +JVM.EMIT_JVM_TYPE_ANNOTATIONS
// TARGET_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JVM_IR
// JVM_TARGET: 1.8
// WITH_REFLECT
// FULL_JDK
@@ -1,7 +1,6 @@
// KOTLIN_CONFIGURATION_FLAGS: +JVM.EMIT_JVM_TYPE_ANNOTATIONS
// TARGET_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JVM_IR
// JVM_TARGET: 1.8
// WITH_REFLECT
// FULL_JDK
@@ -1,7 +1,6 @@
// KOTLIN_CONFIGURATION_FLAGS: +JVM.EMIT_JVM_TYPE_ANNOTATIONS
// TARGET_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JVM_IR
// JVM_TARGET: 1.8
// WITH_REFLECT
// FULL_JDK
@@ -1,7 +1,6 @@
// KOTLIN_CONFIGURATION_FLAGS: +JVM.EMIT_JVM_TYPE_ANNOTATIONS
// TARGET_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JVM_IR
// JVM_TARGET: 1.8
// WITH_REFLECT
// FULL_JDK
@@ -1,7 +1,6 @@
// KOTLIN_CONFIGURATION_FLAGS: +JVM.EMIT_JVM_TYPE_ANNOTATIONS
// TARGET_BACKEND: JVM
// IGNORE_BACKEND_FIR: JVM_IR
// IGNORE_BACKEND: JVM_IR
// JVM_TARGET: 1.8
// WITH_REFLECT
// FULL_JDK
@@ -32,6 +32,7 @@ interface TypeSystemCommonBackendContext : TypeSystemContext {
fun TypeConstructorMarker.getTypeParameterClassifier(): TypeParameterMarker?
fun TypeConstructorMarker.isInlineClass(): Boolean
fun TypeConstructorMarker.isInnerClass(): Boolean
fun TypeParameterMarker.getRepresentativeUpperBound(): KotlinTypeMarker
fun KotlinTypeMarker.getSubstitutedUnderlyingType(): KotlinTypeMarker?
@@ -556,6 +556,11 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy
return (declarationDescriptor as? ClassDescriptor)?.isInline == true
}
override fun TypeConstructorMarker.isInnerClass(): Boolean {
require(this is TypeConstructor, this::errorMessage)
return (declarationDescriptor as? ClassDescriptor)?.isInner == true
}
override fun TypeParameterMarker.getRepresentativeUpperBound(): KotlinTypeMarker {
require(this is TypeParameterDescriptor, this::errorMessage)
return representativeUpperBound