Improve API of annotation arguments in kotlinx-metadata

See changes in ChangeLog.md for more information
This commit is contained in:
Alexander Udalov
2021-03-26 20:51:52 +01:00
parent 1a82158472
commit 5f7f4a4ac8
13 changed files with 99 additions and 70 deletions
@@ -534,28 +534,28 @@ private class MappingExtensions(
TypeArgument.Variance.OUT -> KmVariance.OUT
}
fun ConstantStub.mapToAnnotationArgument(): KmAnnotationArgument<*> = when (this) {
fun ConstantStub.mapToAnnotationArgument(): KmAnnotationArgument = when (this) {
is StringConstantStub -> KmAnnotationArgument.StringValue(value)
is IntegralConstantStub -> when (size) {
1 -> if (isSigned) {
KmAnnotationArgument.ByteValue(value.toByte())
} else {
KmAnnotationArgument.UByteValue(value.toByte())
KmAnnotationArgument.UByteValue(value.toUByte())
}
2 -> if (isSigned) {
KmAnnotationArgument.ShortValue(value.toShort())
} else {
KmAnnotationArgument.UShortValue(value.toShort())
KmAnnotationArgument.UShortValue(value.toUShort())
}
4 -> if (isSigned) {
KmAnnotationArgument.IntValue(value.toInt())
} else {
KmAnnotationArgument.UIntValue(value.toInt())
KmAnnotationArgument.UIntValue(value.toUInt())
}
8 -> if (isSigned) {
KmAnnotationArgument.LongValue(value)
} else {
KmAnnotationArgument.ULongValue(value)
KmAnnotationArgument.ULongValue(value.toULong())
}
else -> error("Integral constant of value $value with unexpected size of $size.")
@@ -4,6 +4,14 @@
- Update to Kotlin 1.5 with metadata version 1.5.
Note: metadata of version 1.5 is readable by Kotlin compiler/reflection of versions 1.4 and later.
- Breaking change: improve API of annotation arguments.
`KmAnnotationArgument` doesn't have `val value: T` anymore, it was moved to a subclass named `KmAnnotationArgument.LiteralValue<T>`.
The property `value` is:
- renamed to `annotation` in `AnnotationValue`
- renamed to `elements` in `ArrayValue`
- removed in favor of `enumClassName`/`enumEntryName` in `EnumValue`
- removed in favor of `className`/`arrayDimensionCount` in `KClassValue`
- changed type from signed to unsigned integer types in `UByteValue`, `UShortValue`, `UIntValue`, `ULongValue`
- [`KT-44783`](https://youtrack.jetbrains.com/issue/KT-44783) Add Flag.IS_VALUE for value classes
- Breaking change: `Flag.IS_INLINE` is deprecated, use `Flag.IS_VALUE` instead
- Breaking change: deprecate `KotlinClassHeader.bytecodeVersion` and `KotlinClassHeader`'s constructor that takes a bytecode version array.
@@ -118,7 +118,7 @@ internal class KlibPropertyExtension : KlibPropertyExtensionVisitor(), KmPropert
val setterAnnotations: MutableList<KmAnnotation> = mutableListOf()
var uniqId: UniqId? = null
var file: Int? = null
var compileTimeValue: KmAnnotationArgument<*>? = null
var compileTimeValue: KmAnnotationArgument? = null
override fun visitAnnotation(annotation: KmAnnotation) {
annotations += annotation
@@ -140,7 +140,7 @@ internal class KlibPropertyExtension : KlibPropertyExtensionVisitor(), KmPropert
this.uniqId = uniqId
}
override fun visitCompileTimeValue(value: KmAnnotationArgument<*>) {
override fun visitCompileTimeValue(value: KmAnnotationArgument) {
this.compileTimeValue = value
}
@@ -299,7 +299,7 @@ internal class KlibMetadataExtensions : MetadataExtensions {
)
}
override fun visitCompileTimeValue(value: KmAnnotationArgument<*>) {
override fun visitCompileTimeValue(value: KmAnnotationArgument) {
proto.setExtension(
KlibMetadataProtoBuf.compileTimeValue,
value.writeAnnotationArgument(c.strings).build()
@@ -62,7 +62,7 @@ var KmProperty.file: Int?
klibExtensions.file = value
}
var KmProperty.compileTimeValue: KmAnnotationArgument<*>?
var KmProperty.compileTimeValue: KmAnnotationArgument?
get() = klibExtensions.compileTimeValue
set(value) {
klibExtensions.compileTimeValue = value
@@ -65,7 +65,7 @@ abstract class KlibPropertyExtensionVisitor : KmPropertyExtensionVisitor {
abstract fun visitUniqId(uniqId: UniqId)
abstract fun visitCompileTimeValue(value: KmAnnotationArgument<*>)
abstract fun visitCompileTimeValue(value: KmAnnotationArgument)
override val type: KmExtensionType
get() = TYPE
@@ -15,43 +15,53 @@ package kotlinx.metadata
* @param arguments explicitly specified arguments to the annotation; does not include default values for annotation parameters
* (specified in the annotation class declaration)
*/
data class KmAnnotation(val className: ClassName, val arguments: Map<String, KmAnnotationArgument<*>>)
data class KmAnnotation(val className: ClassName, val arguments: Map<String, KmAnnotationArgument>)
/**
* Represents an argument to the annotation.
*
* @param T the type of the value of this argument
*/
sealed class KmAnnotationArgument<out T : Any> {
sealed class KmAnnotationArgument {
/**
* The value of this argument.
* A kind of annotation argument, whose value is directly accessible via [value].
* This is possible for annotation arguments of primitive types, unsigned types and strings.
*
* @param T the type of the value of this argument
*/
abstract val value: T
data class ByteValue(override val value: Byte) : KmAnnotationArgument<Byte>()
data class CharValue(override val value: Char) : KmAnnotationArgument<Char>()
data class ShortValue(override val value: Short) : KmAnnotationArgument<Short>()
data class IntValue(override val value: Int) : KmAnnotationArgument<Int>()
data class LongValue(override val value: Long) : KmAnnotationArgument<Long>()
data class FloatValue(override val value: Float) : KmAnnotationArgument<Float>()
data class DoubleValue(override val value: Double) : KmAnnotationArgument<Double>()
data class BooleanValue(override val value: Boolean) : KmAnnotationArgument<Boolean>()
data class UByteValue(override val value: Byte) : KmAnnotationArgument<Byte>()
data class UShortValue(override val value: Short) : KmAnnotationArgument<Short>()
data class UIntValue(override val value: Int) : KmAnnotationArgument<Int>()
data class ULongValue(override val value: Long) : KmAnnotationArgument<Long>()
data class StringValue(override val value: String) : KmAnnotationArgument<String>()
data class KClassValue(val className: ClassName, val arrayDimensionCount: Int) : KmAnnotationArgument<String>() {
override val value: String = className + "[]".repeat(arrayDimensionCount)
sealed class LiteralValue<out T : Any> : KmAnnotationArgument() {
/**
* The value of this argument.
*/
abstract val value: T
}
data class EnumValue(val enumClassName: ClassName, val enumEntryName: String) : KmAnnotationArgument<String>() {
override val value: String = "$enumClassName.$enumEntryName"
}
data class ByteValue(override val value: Byte) : LiteralValue<Byte>()
data class CharValue(override val value: Char) : LiteralValue<Char>()
data class ShortValue(override val value: Short) : LiteralValue<Short>()
data class IntValue(override val value: Int) : LiteralValue<Int>()
data class LongValue(override val value: Long) : LiteralValue<Long>()
data class FloatValue(override val value: Float) : LiteralValue<Float>()
data class DoubleValue(override val value: Double) : LiteralValue<Double>()
data class BooleanValue(override val value: Boolean) : LiteralValue<Boolean>()
data class AnnotationValue(override val value: KmAnnotation) : KmAnnotationArgument<KmAnnotation>()
data class ArrayValue(override val value: List<KmAnnotationArgument<*>>) : KmAnnotationArgument<List<KmAnnotationArgument<*>>>()
// TODO: remove @ExperimentalUnsignedTypes once bootstrap stdlib has stable unsigned types.
@ExperimentalUnsignedTypes
data class UByteValue(override val value: UByte) : LiteralValue<UByte>()
@ExperimentalUnsignedTypes
data class UShortValue(override val value: UShort) : LiteralValue<UShort>()
@ExperimentalUnsignedTypes
data class UIntValue(override val value: UInt) : LiteralValue<UInt>()
@ExperimentalUnsignedTypes
data class ULongValue(override val value: ULong) : LiteralValue<ULong>()
data class StringValue(override val value: String) : LiteralValue<String>()
data class KClassValue(val className: ClassName, val arrayDimensionCount: Int) : KmAnnotationArgument()
data class EnumValue(val enumClassName: ClassName, val enumEntryName: String) : KmAnnotationArgument()
data class AnnotationValue(val annotation: KmAnnotation) : KmAnnotationArgument()
data class ArrayValue(val elements: List<KmAnnotationArgument>) : KmAnnotationArgument()
}
@@ -23,13 +23,14 @@ fun ProtoBuf.Annotation.readAnnotation(strings: NameResolver): KmAnnotation =
}.toMap()
)
fun ProtoBuf.Annotation.Argument.Value.readAnnotationArgument(strings: NameResolver): KmAnnotationArgument<*>? {
@OptIn(ExperimentalUnsignedTypes::class)
fun ProtoBuf.Annotation.Argument.Value.readAnnotationArgument(strings: NameResolver): KmAnnotationArgument? {
if (Flags.IS_UNSIGNED[flags]) {
return when (type) {
BYTE -> KmAnnotationArgument.UByteValue(intValue.toByte())
SHORT -> KmAnnotationArgument.UShortValue(intValue.toShort())
INT -> KmAnnotationArgument.UIntValue(intValue.toInt())
LONG -> KmAnnotationArgument.ULongValue(intValue)
BYTE -> KmAnnotationArgument.UByteValue(intValue.toByte().toUByte())
SHORT -> KmAnnotationArgument.UShortValue(intValue.toShort().toUShort())
INT -> KmAnnotationArgument.UIntValue(intValue.toInt().toUInt())
LONG -> KmAnnotationArgument.ULongValue(intValue.toULong())
else -> error("Cannot read value of unsigned type: $type")
}
}
@@ -24,7 +24,8 @@ fun KmAnnotation.writeAnnotation(strings: StringTable): ProtoBuf.Annotation.Buil
}
}
fun KmAnnotationArgument<*>.writeAnnotationArgument(strings: StringTable): ProtoBuf.Annotation.Argument.Value.Builder =
@OptIn(ExperimentalUnsignedTypes::class)
fun KmAnnotationArgument.writeAnnotationArgument(strings: StringTable): ProtoBuf.Annotation.Argument.Value.Builder =
ProtoBuf.Annotation.Argument.Value.newBuilder().apply {
when (this@writeAnnotationArgument) {
is KmAnnotationArgument.ByteValue -> {
@@ -76,7 +77,7 @@ fun KmAnnotationArgument<*>.writeAnnotationArgument(strings: StringTable): Proto
}
is KmAnnotationArgument.ULongValue -> {
this.type = ProtoBuf.Annotation.Argument.Value.Type.LONG
this.intValue = value
this.intValue = value.toLong()
this.flags = Flags.IS_UNSIGNED.toFlags(true)
}
is KmAnnotationArgument.StringValue -> {
@@ -95,11 +96,11 @@ fun KmAnnotationArgument<*>.writeAnnotationArgument(strings: StringTable): Proto
}
is KmAnnotationArgument.AnnotationValue -> {
this.type = ProtoBuf.Annotation.Argument.Value.Type.ANNOTATION
this.annotation = value.writeAnnotation(strings).build()
this.annotation = this@writeAnnotationArgument.annotation.writeAnnotation(strings).build()
}
is KmAnnotationArgument.ArrayValue -> {
this.type = ProtoBuf.Annotation.Argument.Value.Type.ARRAY
for (element in value) {
for (element in elements) {
this.addArrayElement(element.writeAnnotationArgument(strings))
}
}
@@ -446,7 +446,7 @@ private fun renderAnnotation(annotation: KmAnnotation): String =
}
@OptIn(ExperimentalUnsignedTypes::class)
private fun renderAnnotationArgument(arg: KmAnnotationArgument<*>): String =
private fun renderAnnotationArgument(arg: KmAnnotationArgument): String =
when (arg) {
is KmAnnotationArgument.ByteValue -> arg.value.toString() + ".toByte()"
is KmAnnotationArgument.CharValue -> "'${arg.value.toString().sanitize(quote = '\'')}'"
@@ -455,10 +455,10 @@ private fun renderAnnotationArgument(arg: KmAnnotationArgument<*>): String =
is KmAnnotationArgument.LongValue -> arg.value.toString() + "L"
is KmAnnotationArgument.FloatValue -> arg.value.toString() + "f"
is KmAnnotationArgument.DoubleValue -> arg.value.toString()
is KmAnnotationArgument.UByteValue -> arg.value.toUByte().toString() + ".toUByte()"
is KmAnnotationArgument.UShortValue -> arg.value.toUShort().toString() + ".toUShort()"
is KmAnnotationArgument.UIntValue -> arg.value.toUInt().toString() + "u"
is KmAnnotationArgument.ULongValue -> arg.value.toULong().toString() + "uL"
is KmAnnotationArgument.UByteValue -> arg.value.toString() + ".toUByte()"
is KmAnnotationArgument.UShortValue -> arg.value.toString() + ".toUShort()"
is KmAnnotationArgument.UIntValue -> arg.value.toString() + "u"
is KmAnnotationArgument.ULongValue -> arg.value.toString() + "uL"
is KmAnnotationArgument.BooleanValue -> arg.value.toString()
is KmAnnotationArgument.StringValue -> "\"${arg.value.sanitize(quote = '"')}\""
is KmAnnotationArgument.KClassValue -> buildString {
@@ -466,14 +466,14 @@ private fun renderAnnotationArgument(arg: KmAnnotationArgument<*>): String =
append(arg.className).append("::class")
repeat(arg.arrayDimensionCount) { append(">") }
}
is KmAnnotationArgument.EnumValue -> arg.value
is KmAnnotationArgument.AnnotationValue -> arg.value.let { annotation ->
is KmAnnotationArgument.EnumValue -> "${arg.enumClassName}.${arg.enumEntryName}"
is KmAnnotationArgument.AnnotationValue -> arg.annotation.let { annotation ->
val args = annotation.arguments.entries.joinToString { (name, argument) ->
"$name = ${renderAnnotationArgument(argument)}"
}
"${annotation.className}($args)"
}
is KmAnnotationArgument.ArrayValue -> arg.value.joinToString(prefix = "[", postfix = "]", transform = ::renderAnnotationArgument)
is KmAnnotationArgument.ArrayValue -> arg.elements.joinToString(prefix = "[", postfix = "]", transform = ::renderAnnotationArgument)
}
private fun String.sanitize(quote: Char): String =
@@ -16,10 +16,17 @@ sealed class CirConstantValue<out T> {
data class IntValue(override val value: Int) : CirConstantValue<Int>()
data class LongValue(override val value: Long) : CirConstantValue<Long>()
data class UByteValue(override val value: Byte) : CirConstantValue<Byte>()
data class UShortValue(override val value: Short) : CirConstantValue<Short>()
data class UIntValue(override val value: Int) : CirConstantValue<Int>()
data class ULongValue(override val value: Long) : CirConstantValue<Long>()
@ExperimentalUnsignedTypes
data class UByteValue(override val value: UByte) : CirConstantValue<UByte>()
@ExperimentalUnsignedTypes
data class UShortValue(override val value: UShort) : CirConstantValue<UShort>()
@ExperimentalUnsignedTypes
data class UIntValue(override val value: UInt) : CirConstantValue<UInt>()
@ExperimentalUnsignedTypes
data class ULongValue(override val value: ULong) : CirConstantValue<ULong>()
data class FloatValue(override val value: Float) : CirConstantValue<Float>()
data class DoubleValue(override val value: Double) : CirConstantValue<Double>()
@@ -11,10 +11,10 @@ import kotlinx.metadata.klib.annotations
import kotlinx.metadata.klib.compileTimeValue
import kotlinx.metadata.klib.getterAnnotations
import kotlinx.metadata.klib.setterAnnotations
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.commonizer.cir.*
import org.jetbrains.kotlin.commonizer.mergedtree.CirProvided
import org.jetbrains.kotlin.commonizer.utils.*
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.types.Variance
object CirDeserializers {
@@ -45,7 +45,7 @@ object CirDeserializers {
isMarkedNullable = false
)
val allValueArguments: Map<String, KmAnnotationArgument<*>> = source.arguments
val allValueArguments: Map<String, KmAnnotationArgument> = source.arguments
if (allValueArguments.isEmpty())
return CirAnnotation.createInterned(type = type, constantValueArguments = emptyMap(), annotationValueArguments = emptyMap())
@@ -55,7 +55,7 @@ object CirDeserializers {
allValueArguments.forEach { (name, constantValue) ->
val cirName = CirName.create(name)
if (constantValue is KmAnnotationArgument.AnnotationValue)
annotationValueArguments[cirName] = annotation(source = constantValue.value, typeResolver)
annotationValueArguments[cirName] = annotation(source = constantValue.annotation, typeResolver)
else
constantValueArguments[cirName] = constantValue(
constantValue = constantValue,
@@ -205,7 +205,7 @@ object CirDeserializers {
)
private fun constantValue(
constantValue: KmAnnotationArgument<*>?,
constantValue: KmAnnotationArgument?,
constantName: CirName? = null,
owner: Any,
): CirConstantValue<*> = constantValue(
@@ -213,8 +213,9 @@ object CirDeserializers {
location = { "${owner::class.java}, $owner" + constantName?.toString()?.let { "[$it]" } }
)
@OptIn(ExperimentalUnsignedTypes::class)
private fun constantValue(
constantValue: KmAnnotationArgument<*>?,
constantValue: KmAnnotationArgument?,
location: () -> String
): CirConstantValue<*> = when (constantValue) {
null -> CirConstantValue.NullValue
@@ -242,7 +243,7 @@ object CirDeserializers {
)
is KmAnnotationArgument.ArrayValue -> CirConstantValue.ArrayValue(
constantValue.value.compactMapIndexed { index, innerConstantValue ->
constantValue.elements.compactMapIndexed { index, innerConstantValue ->
constantValue(
constantValue = innerConstantValue,
location = { "${location()}[$index]" }
@@ -8,13 +8,13 @@ package org.jetbrains.kotlin.commonizer.metadata
import kotlinx.metadata.*
import kotlinx.metadata.klib.*
import org.jetbrains.kotlin.backend.common.serialization.metadata.DynamicTypeDeserializer
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.commonizer.cir.*
import org.jetbrains.kotlin.commonizer.mergedtree.*
import org.jetbrains.kotlin.commonizer.metadata.TypeAliasExpansion.*
import org.jetbrains.kotlin.commonizer.utils.DEFAULT_SETTER_VALUE_NAME
import org.jetbrains.kotlin.commonizer.utils.SPECIAL_CLASS_WITHOUT_SUPERTYPES_CLASS_NAMES
import org.jetbrains.kotlin.commonizer.utils.compactMap
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.types.Variance
internal fun CirModule.serializeModule(
@@ -195,7 +195,7 @@ internal fun CirFunction.serializeFunction(
}
private fun CirAnnotation.serializeAnnotation(): KmAnnotation {
val arguments = LinkedHashMap<String, KmAnnotationArgument<*>>(constantValueArguments.size + annotationValueArguments.size, 1F)
val arguments = LinkedHashMap<String, KmAnnotationArgument>(constantValueArguments.size + annotationValueArguments.size, 1F)
constantValueArguments.forEach { (name: CirName, value: CirConstantValue<*>) ->
arguments[name.name] = value.serializeConstantValue()
@@ -212,7 +212,8 @@ private fun CirAnnotation.serializeAnnotation(): KmAnnotation {
)
}
private fun CirConstantValue<*>.serializeConstantValue(): KmAnnotationArgument<*>? = when (this) {
@OptIn(ExperimentalUnsignedTypes::class)
private fun CirConstantValue<*>.serializeConstantValue(): KmAnnotationArgument? = when (this) {
is CirConstantValue.StringValue -> KmAnnotationArgument.StringValue(value)
is CirConstantValue.CharValue -> KmAnnotationArgument.CharValue(value)