FIR IDE: type-aware constant conversion
This commit is contained in:
committed by
Ilya Kirillov
parent
9e4f234941
commit
068e81570d
+19
-1
@@ -10,4 +10,22 @@ import org.jetbrains.kotlin.types.ConstantValueKind
|
||||
public sealed class KtConstantValue
|
||||
public object KtUnsupportedConstantValue : KtConstantValue()
|
||||
|
||||
public data class KtSimpleConstantValue<T>(val constantValueKind: ConstantValueKind<T>, val value: T) : KtConstantValue()
|
||||
public data class KtSimpleConstantValue<T>(val constantValueKind: ConstantValueKind<T>, val value: T) : KtConstantValue() {
|
||||
public fun toConst(): Any? {
|
||||
return (value as? Long)?.let {
|
||||
when (constantValueKind) {
|
||||
ConstantValueKind.Byte -> it.toByte()
|
||||
ConstantValueKind.Short -> it.toShort()
|
||||
ConstantValueKind.Int -> it.toInt()
|
||||
ConstantValueKind.Float -> it.toFloat()
|
||||
ConstantValueKind.Double -> it.toDouble()
|
||||
|
||||
ConstantValueKind.UnsignedByte -> it.toUByte()
|
||||
ConstantValueKind.UnsignedShort -> it.toUShort()
|
||||
ConstantValueKind.UnsignedInt -> it.toUInt()
|
||||
ConstantValueKind.UnsignedLong -> it.toULong()
|
||||
else -> it
|
||||
}
|
||||
} ?: value
|
||||
}
|
||||
}
|
||||
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
val p : Byte = <expr>42</expr>
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
expression: 42
|
||||
constant_value: KtSimpleConstantValue(constantValueKind=Byte, value=42)
|
||||
constant: 42
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
val p : Double = <expr>3.14</expr>
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
expression: 3.14
|
||||
constant_value: KtSimpleConstantValue(constantValueKind=Double, value=3.14)
|
||||
constant: 3.14
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
val p = <expr>3.14f</expr>
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
expression: 3.14f
|
||||
constant_value: KtSimpleConstantValue(constantValueKind=Float, value=3.14)
|
||||
constant: 3.14
|
||||
+1
@@ -0,0 +1 @@
|
||||
val p : Int = <expr>42</expr>
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
expression: 42
|
||||
constant_value: KtSimpleConstantValue(constantValueKind=Int, value=42)
|
||||
constant: 42
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
val p : Long = <expr>42</expr>
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
expression: 42
|
||||
constant_value: KtSimpleConstantValue(constantValueKind=Long, value=42)
|
||||
constant: 42
|
||||
Vendored
+1
@@ -0,0 +1 @@
|
||||
val p : UInt = <expr>42u</expr>
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
expression: 42u
|
||||
constant_value: KtSimpleConstantValue(constantValueKind=UInt, value=42)
|
||||
constant: 42
|
||||
Reference in New Issue
Block a user