Refactor CirConstantValue similarly to KmAnnotationArgument
Introduce LiteralValue with generic value, and remove `value` from enum/array/null subclasses.
This commit is contained in:
@@ -11,13 +11,13 @@ import org.jetbrains.kotlin.commonizer.utils.hashCode
|
||||
|
||||
interface CirAnnotation {
|
||||
val type: CirClassType
|
||||
val constantValueArguments: Map<CirName, CirConstantValue<*>>
|
||||
val constantValueArguments: Map<CirName, CirConstantValue>
|
||||
val annotationValueArguments: Map<CirName, CirAnnotation>
|
||||
|
||||
companion object {
|
||||
fun createInterned(
|
||||
type: CirClassType,
|
||||
constantValueArguments: Map<CirName, CirConstantValue<*>>,
|
||||
constantValueArguments: Map<CirName, CirConstantValue>,
|
||||
annotationValueArguments: Map<CirName, CirAnnotation>
|
||||
): CirAnnotation = interner.intern(
|
||||
CirAnnotationInternedImpl(
|
||||
@@ -33,7 +33,7 @@ interface CirAnnotation {
|
||||
|
||||
private data class CirAnnotationInternedImpl(
|
||||
override val type: CirClassType,
|
||||
override val constantValueArguments: Map<CirName, CirConstantValue<*>>,
|
||||
override val constantValueArguments: Map<CirName, CirConstantValue>,
|
||||
override val annotationValueArguments: Map<CirName, CirAnnotation>
|
||||
) : CirAnnotation {
|
||||
// See also org.jetbrains.kotlin.types.KotlinType.cachedHashCode
|
||||
|
||||
@@ -5,41 +5,41 @@
|
||||
|
||||
package org.jetbrains.kotlin.commonizer.cir
|
||||
|
||||
sealed class CirConstantValue<out T> {
|
||||
abstract val value: T
|
||||
|
||||
data class StringValue(override val value: String) : CirConstantValue<String>()
|
||||
data class CharValue(override val value: Char) : CirConstantValue<Char>()
|
||||
|
||||
data class ByteValue(override val value: Byte) : CirConstantValue<Byte>()
|
||||
data class ShortValue(override val value: Short) : CirConstantValue<Short>()
|
||||
data class IntValue(override val value: Int) : CirConstantValue<Int>()
|
||||
data class LongValue(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>()
|
||||
data class BooleanValue(override val value: Boolean) : CirConstantValue<Boolean>()
|
||||
|
||||
data class EnumValue(val enumClassId: CirEntityId, val enumEntryName: CirName) : CirConstantValue<String>() {
|
||||
override val value: String = "${enumClassId}.${enumEntryName}"
|
||||
sealed class CirConstantValue {
|
||||
sealed class LiteralValue<out T> : CirConstantValue() {
|
||||
abstract val value: T
|
||||
}
|
||||
|
||||
data class ArrayValue(override val value: List<CirConstantValue<*>>) : CirConstantValue<List<CirConstantValue<*>>>()
|
||||
data class StringValue(override val value: String) : LiteralValue<String>()
|
||||
data class CharValue(override val value: Char) : LiteralValue<Char>()
|
||||
|
||||
object NullValue : CirConstantValue<Void?>() {
|
||||
override val value: Void? get() = null
|
||||
data class ByteValue(override val value: Byte) : LiteralValue<Byte>()
|
||||
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>()
|
||||
|
||||
// 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 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 EnumValue(val enumClassId: CirEntityId, val enumEntryName: CirName) : CirConstantValue()
|
||||
|
||||
data class ArrayValue(val elements: List<CirConstantValue>) : CirConstantValue()
|
||||
|
||||
object NullValue : CirConstantValue() {
|
||||
override fun toString() = "NullValue(value=null)"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,7 +19,7 @@ interface CirProperty : CirFunctionOrProperty, CirLiftedUpDeclaration {
|
||||
val setter: CirPropertySetter?
|
||||
val backingFieldAnnotations: List<CirAnnotation>
|
||||
val delegateFieldAnnotations: List<CirAnnotation>
|
||||
val compileTimeInitializer: CirConstantValue<*>
|
||||
val compileTimeInitializer: CirConstantValue
|
||||
|
||||
companion object {
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
@@ -42,7 +42,7 @@ interface CirProperty : CirFunctionOrProperty, CirLiftedUpDeclaration {
|
||||
setter: CirPropertySetter?,
|
||||
backingFieldAnnotations: List<CirAnnotation>,
|
||||
delegateFieldAnnotations: List<CirAnnotation>,
|
||||
compileTimeInitializer: CirConstantValue<*>
|
||||
compileTimeInitializer: CirConstantValue
|
||||
): CirProperty = CirPropertyImpl(
|
||||
annotations = annotations,
|
||||
name = name,
|
||||
@@ -86,7 +86,7 @@ data class CirPropertyImpl(
|
||||
override val setter: CirPropertySetter?,
|
||||
override val backingFieldAnnotations: List<CirAnnotation>,
|
||||
override val delegateFieldAnnotations: List<CirAnnotation>,
|
||||
override val compileTimeInitializer: CirConstantValue<*>
|
||||
override val compileTimeInitializer: CirConstantValue
|
||||
) : CirProperty {
|
||||
// const property in "common" fragment is already lifted up
|
||||
override val isLiftedUp get() = isConst
|
||||
|
||||
@@ -5,13 +5,13 @@
|
||||
|
||||
package org.jetbrains.kotlin.commonizer.core
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
import org.jetbrains.kotlin.commonizer.cir.*
|
||||
import org.jetbrains.kotlin.commonizer.cir.CirConstantValue.*
|
||||
import org.jetbrains.kotlin.commonizer.core.AnnotationsCommonizer.Companion.FALLBACK_MESSAGE
|
||||
import org.jetbrains.kotlin.commonizer.utils.DEPRECATED_ANNOTATION_CLASS_ID
|
||||
import org.jetbrains.kotlin.commonizer.utils.compactMap
|
||||
import org.jetbrains.kotlin.commonizer.utils.compactMapOf
|
||||
import org.jetbrains.kotlin.descriptors.Visibilities
|
||||
import kotlin.DeprecationLevel.WARNING
|
||||
|
||||
/**
|
||||
@@ -52,7 +52,7 @@ private class DeprecatedAnnotationCommonizer : Commonizer<CirAnnotation, CirAnno
|
||||
val level: DeprecationLevel = level ?: failInEmptyState()
|
||||
val messageValue: StringValue = message.toDeprecationMessageValue()
|
||||
|
||||
val constantValueArguments: Map<CirName, CirConstantValue<*>> =
|
||||
val constantValueArguments: Map<CirName, CirConstantValue> =
|
||||
if (level == WARNING) {
|
||||
// don't populate with the default level value
|
||||
compactMapOf(PROPERTY_NAME_MESSAGE, messageValue)
|
||||
@@ -186,17 +186,17 @@ private class DeprecatedAnnotationCommonizer : Commonizer<CirAnnotation, CirAnno
|
||||
private fun String.toReplaceWithValue(imports: List<String>): CirAnnotation =
|
||||
createReplaceWithAnnotation(this, imports)
|
||||
|
||||
private inline fun Map<CirName, CirConstantValue<*>>.getString(name: CirName): String? =
|
||||
private inline fun Map<CirName, CirConstantValue>.getString(name: CirName): String? =
|
||||
(this[name] as? StringValue)?.value
|
||||
|
||||
private inline fun Map<CirName, CirConstantValue<*>>.getEnumEntryName(name: CirName): String? =
|
||||
private inline fun Map<CirName, CirConstantValue>.getEnumEntryName(name: CirName): String? =
|
||||
(this[name] as? EnumValue)?.enumEntryName?.name
|
||||
|
||||
private inline fun Map<CirName, CirAnnotation>.getAnnotation(name: CirName): CirAnnotation? =
|
||||
this[name]
|
||||
|
||||
private inline fun Map<CirName, CirConstantValue<*>>.getStringArray(name: CirName): List<String>? {
|
||||
val elements: List<CirConstantValue<*>> = (this[name] as? ArrayValue)?.value ?: return null
|
||||
private inline fun Map<CirName, CirConstantValue>.getStringArray(name: CirName): List<String>? {
|
||||
val elements: List<CirConstantValue> = (this[name] as? ArrayValue)?.elements ?: return null
|
||||
if (elements.isEmpty()) return emptyList()
|
||||
|
||||
val result = ArrayList<String>(elements.size)
|
||||
|
||||
@@ -109,7 +109,7 @@ class PropertyCommonizer(classifiers: CirKnownClassifiers) : AbstractFunctionOrP
|
||||
val properties: MutableList<CirProperty> = mutableListOf()
|
||||
}
|
||||
|
||||
class ConstSameValue(val compileTimeInitializer: CirConstantValue<*>) : Const() {
|
||||
class ConstSameValue(val compileTimeInitializer: CirConstantValue) : Const() {
|
||||
init {
|
||||
check(compileTimeInitializer != CirConstantValue.NullValue)
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ object CirDeserializers {
|
||||
if (allValueArguments.isEmpty())
|
||||
return CirAnnotation.createInterned(type = type, constantValueArguments = emptyMap(), annotationValueArguments = emptyMap())
|
||||
|
||||
val constantValueArguments: MutableMap<CirName, CirConstantValue<*>> = THashMap(allValueArguments.size)
|
||||
val constantValueArguments: MutableMap<CirName, CirConstantValue> = THashMap(allValueArguments.size)
|
||||
val annotationValueArguments: MutableMap<CirName, CirAnnotation> = THashMap(allValueArguments.size)
|
||||
|
||||
allValueArguments.forEach { (name, constantValue) ->
|
||||
@@ -208,7 +208,7 @@ object CirDeserializers {
|
||||
constantValue: KmAnnotationArgument?,
|
||||
constantName: CirName? = null,
|
||||
owner: Any,
|
||||
): CirConstantValue<*> = constantValue(
|
||||
): CirConstantValue = constantValue(
|
||||
constantValue = constantValue,
|
||||
location = { "${owner::class.java}, $owner" + constantName?.toString()?.let { "[$it]" } }
|
||||
)
|
||||
@@ -217,7 +217,7 @@ object CirDeserializers {
|
||||
private fun constantValue(
|
||||
constantValue: KmAnnotationArgument?,
|
||||
location: () -> String
|
||||
): CirConstantValue<*> = when (constantValue) {
|
||||
): CirConstantValue = when (constantValue) {
|
||||
null -> CirConstantValue.NullValue
|
||||
|
||||
is KmAnnotationArgument.StringValue -> CirConstantValue.StringValue(constantValue.value)
|
||||
|
||||
@@ -197,7 +197,7 @@ internal fun CirFunction.serializeFunction(
|
||||
private fun CirAnnotation.serializeAnnotation(): KmAnnotation {
|
||||
val arguments = LinkedHashMap<String, KmAnnotationArgument>(constantValueArguments.size + annotationValueArguments.size, 1F)
|
||||
|
||||
constantValueArguments.forEach { (name: CirName, value: CirConstantValue<*>) ->
|
||||
constantValueArguments.forEach { (name: CirName, value: CirConstantValue) ->
|
||||
arguments[name.name] = value.serializeConstantValue()
|
||||
?: error("Unexpected <null> constant value inside of $this")
|
||||
}
|
||||
@@ -213,7 +213,7 @@ private fun CirAnnotation.serializeAnnotation(): KmAnnotation {
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalUnsignedTypes::class)
|
||||
private fun CirConstantValue<*>.serializeConstantValue(): KmAnnotationArgument? = when (this) {
|
||||
private fun CirConstantValue.serializeConstantValue(): KmAnnotationArgument? = when (this) {
|
||||
is CirConstantValue.StringValue -> KmAnnotationArgument.StringValue(value)
|
||||
is CirConstantValue.CharValue -> KmAnnotationArgument.CharValue(value)
|
||||
|
||||
@@ -234,7 +234,7 @@ private fun CirConstantValue<*>.serializeConstantValue(): KmAnnotationArgument?
|
||||
is CirConstantValue.EnumValue -> KmAnnotationArgument.EnumValue(enumClassId.toString(), enumEntryName.name)
|
||||
is CirConstantValue.NullValue -> null
|
||||
|
||||
is CirConstantValue.ArrayValue -> KmAnnotationArgument.ArrayValue(value.compactMap { element ->
|
||||
is CirConstantValue.ArrayValue -> KmAnnotationArgument.ArrayValue(elements.compactMap { element ->
|
||||
element.serializeConstantValue() ?: error("Unexpected <null> constant value inside of $this")
|
||||
})
|
||||
}
|
||||
|
||||
+2
-2
@@ -281,7 +281,7 @@ class AnnotationsCommonizerTest : AbstractCommonizerTest<List<CirAnnotation>, Li
|
||||
|
||||
private fun mockAnnotation(
|
||||
classId: String,
|
||||
constantValueArguments: Map<CirName, CirConstantValue<*>> = emptyMap(),
|
||||
constantValueArguments: Map<CirName, CirConstantValue> = emptyMap(),
|
||||
annotationValueArguments: Map<CirName, CirAnnotation> = emptyMap()
|
||||
): CirAnnotation = CirAnnotation.createInterned(
|
||||
type = mockClassType(classId),
|
||||
@@ -308,7 +308,7 @@ private fun mockDeprecated(
|
||||
|
||||
return mockAnnotation(
|
||||
classId = "kotlin/Deprecated",
|
||||
constantValueArguments = HashMap<CirName, CirConstantValue<*>>().apply {
|
||||
constantValueArguments = HashMap<CirName, CirConstantValue>().apply {
|
||||
this[CirName.create("message")] = StringValue(message)
|
||||
|
||||
if (level != WARNING)
|
||||
|
||||
Reference in New Issue
Block a user