[FIR] Support deserialization of value parameter annotations

There are still some problems, see KT-39837
This commit is contained in:
Dmitriy Novozhilov
2020-06-25 18:26:01 +03:00
parent 2b2f9b3386
commit 7834284bec
22 changed files with 191 additions and 96 deletions
@@ -17,11 +17,13 @@ import org.jetbrains.kotlin.metadata.ProtoBuf
import org.jetbrains.kotlin.metadata.deserialization.NameResolver
import org.jetbrains.kotlin.metadata.deserialization.TypeTable
import org.jetbrains.kotlin.metadata.deserialization.getExtensionOrNull
import org.jetbrains.kotlin.metadata.deserialization.hasReceiver
import org.jetbrains.kotlin.metadata.jvm.JvmProtoBuf
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmProtoBufUtil
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.protobuf.MessageLite
import org.jetbrains.kotlin.serialization.deserialization.ProtoContainer
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedContainerSource
class JvmBinaryAnnotationDeserializer(
@@ -33,12 +35,6 @@ class JvmBinaryAnnotationDeserializer(
session.loadMemberAnnotations(kotlinBinaryClass, byteContent)
}
private enum class CallableKind {
PROPERTY_GETTER,
PROPERTY_SETTER,
OTHERS
}
override fun loadTypeAnnotations(typeProto: ProtoBuf.Type, nameResolver: NameResolver): List<FirAnnotationCall> {
val annotations = typeProto.getExtension(JvmProtoBuf.typeAnnotation).orEmpty()
return annotations.map { deserializeAnnotation(it, nameResolver) }
@@ -86,6 +82,36 @@ class JvmBinaryAnnotationDeserializer(
return findJvmBinaryClassAndLoadMemberAnnotations(containerSource, signature)
}
override fun loadValueParameterAnnotations(
containerSource: DeserializedContainerSource?,
callableProto: MessageLite,
valueParameterProto: ProtoBuf.ValueParameter,
nameResolver: NameResolver,
typeTable: TypeTable,
kind: CallableKind,
parameterIndex: Int,
): List<FirAnnotationCall> {
val methodSignature = getCallableSignature(callableProto, nameResolver, typeTable, kind) ?: return emptyList()
val index = parameterIndex + computeJvmParameterIndexShift(callableProto)
val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, index)
return findJvmBinaryClassAndLoadMemberAnnotations(containerSource, paramSignature)
}
/*
* TODO: Support container proto and fix index shift for
* constructors of inner classes and enums
*
* See [AbstractBinaryClassAnnotationAndConstantLoader]
*/
private fun computeJvmParameterIndexShift(message: MessageLite): Int {
return when (message) {
is ProtoBuf.Function -> if (message.hasReceiver()) 1 else 0
is ProtoBuf.Property -> if (message.hasReceiver()) 1 else 0
is ProtoBuf.Constructor -> 0
else -> throw UnsupportedOperationException("Unsupported message: ${message::class.java}")
}
}
private fun getCallableSignature(
proto: MessageLite,
nameResolver: NameResolver,