Analysis API: improve rendering of constant values

This commit is contained in:
Ilya Kirillov
2021-11-24 12:56:54 +01:00
parent e608f8d8dc
commit 7919dae558
7 changed files with 53 additions and 61 deletions
@@ -5,8 +5,6 @@
package org.jetbrains.kotlin.analysis.api.impl.base.test.components.compileTimeConstantProvider
import org.jetbrains.kotlin.analysis.api.annotations.render
import org.jetbrains.kotlin.analysis.api.base.render
import org.jetbrains.kotlin.analysis.api.impl.barebone.test.FrontendApiTestConfiguratorService
import org.jetbrains.kotlin.analysis.api.impl.barebone.test.expressionMarkerProvider
import org.jetbrains.kotlin.analysis.api.impl.base.test.test.framework.AbstractHLApiSingleFileTest
@@ -32,8 +30,8 @@ abstract class AbstractCompileTimeConstantEvaluatorTest(
}
val actual = buildString {
appendLine("expression: ${expression.text}")
appendLine("constant: ${constantValue?.render()}")
appendLine("constantValueKind: ${constantValue?.constantValueKind}")
appendLine("constant: ${constantValue?.renderAsKotlinConstant() ?: "NOT_EVALUATED"}")
appendLine("constantValueKind: ${constantValue?.constantValueKind ?: "NOT_EVALUATED"}")
}
testServices.assertions.assertEqualsToTestDataFileSibling(actual)
}
@@ -45,7 +45,7 @@ public object KtAnnotationValueRenderer {
}
private fun StringBuilder.renderConstantAnnotationValue(value: KtConstantAnnotationValue) {
append(KtConstantValueRenderer.render(value.constantValue))
append(value.constantValue.renderAsKotlinConstant())
}
private fun StringBuilder.renderEnumEntryConstantValue(value: KtEnumEntryAnnotationValue) {
@@ -1,32 +0,0 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.api.annotations
import org.jetbrains.kotlin.analysis.api.base.KtConstantValue
import org.jetbrains.kotlin.types.ConstantValueKind
public object KtConstantValueRenderer {
public fun render(value: KtConstantValue): String = buildString {
when (value) {
is KtConstantValue.KtStringConstantValue -> {
append('"')
append(value.value)
append('"')
}
is KtConstantValue.KtCharConstantValue -> {
append("'")
append(value.value)
append("'")
}
is KtConstantValue.KtErrorConstantValue -> {
append("error(\"${value.errorMessage}\")")
}
else -> {
append(value.value)
}
}
}
}
@@ -5,7 +5,6 @@
package org.jetbrains.kotlin.analysis.api.base
import org.jetbrains.kotlin.analysis.api.annotations.KtConstantValueRenderer
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.types.ConstantValueKind
@@ -14,85 +13,113 @@ public sealed class KtConstantValue(public val constantValueKind: ConstantValueK
public abstract val value: Any?
public abstract val sourcePsi: KtElement?
public abstract fun renderAsKotlinConstant(): String
public class KtNullConstantValue(override val sourcePsi: KtElement?) : KtConstantValue(ConstantValueKind.Null) {
override val value: Nothing? get() = null
override fun renderAsKotlinConstant(): String = "null"
}
public class KtBooleanConstantValue(
override val value: Boolean,
override val sourcePsi: KtElement?
) : KtConstantValue(ConstantValueKind.Boolean)
) : KtConstantValue(ConstantValueKind.Boolean) {
override fun renderAsKotlinConstant(): String = value.toString()
}
public class KtCharConstantValue(
override val value: Char,
override val sourcePsi: KtElement?
) : KtConstantValue(ConstantValueKind.Char)
) : KtConstantValue(ConstantValueKind.Char) {
override fun renderAsKotlinConstant(): String = "`$value`"
}
public class KtByteConstantValue(
override val value: Byte,
override val sourcePsi: KtElement?
) : KtConstantValue(ConstantValueKind.Byte)
) : KtConstantValue(ConstantValueKind.Byte) {
override fun renderAsKotlinConstant(): String = value.toString()
}
public class KtUnsignedByteConstantValue(
override val value: UByte,
override val sourcePsi: KtElement?
) : KtConstantValue(ConstantValueKind.UnsignedByte)
) : KtConstantValue(ConstantValueKind.UnsignedByte) {
override fun renderAsKotlinConstant(): String = "${value}u"
}
public class KtShortConstantValue(
override val value: Short,
override val sourcePsi: KtElement?
) : KtConstantValue(ConstantValueKind.Short)
) : KtConstantValue(ConstantValueKind.Short) {
override fun renderAsKotlinConstant(): String = value.toString()
}
public class KtUnsignedShortConstantValue(
override val value: UShort,
override val sourcePsi: KtElement?
) : KtConstantValue(ConstantValueKind.UnsignedShort)
) : KtConstantValue(ConstantValueKind.UnsignedShort) {
override fun renderAsKotlinConstant(): String = "${value}u"
}
public class KtIntConstantValue(
override val value: Int,
override val sourcePsi: KtElement?
) : KtConstantValue(ConstantValueKind.Int)
) : KtConstantValue(ConstantValueKind.Int) {
override fun renderAsKotlinConstant(): String = value.toString()
}
public class KtUnsignedIntConstantValue(
override val value: UInt,
override val sourcePsi: KtElement?
) : KtConstantValue(ConstantValueKind.UnsignedInt)
) : KtConstantValue(ConstantValueKind.UnsignedInt) {
override fun renderAsKotlinConstant(): String = "${value}u"
}
public class KtLongConstantValue(
override val value: Long,
override val sourcePsi: KtElement?
) : KtConstantValue(ConstantValueKind.Long)
) : KtConstantValue(ConstantValueKind.Long) {
override fun renderAsKotlinConstant(): String = value.toString()
}
public class KtUnsignedLongConstantValue(
override val value: ULong,
override val sourcePsi: KtElement?
) : KtConstantValue(ConstantValueKind.UnsignedLong)
) : KtConstantValue(ConstantValueKind.UnsignedLong) {
override fun renderAsKotlinConstant(): String = "${value}uL"
}
public class KtStringConstantValue(
override val value: String,
override val sourcePsi: KtElement?
) : KtConstantValue(ConstantValueKind.String)
) : KtConstantValue(ConstantValueKind.String) {
override fun renderAsKotlinConstant(): String = "\"${value}\""
}
public class KtFloatConstantValue(
override val value: Float,
override val sourcePsi: KtElement?
) : KtConstantValue(ConstantValueKind.Float)
) : KtConstantValue(ConstantValueKind.Float) {
override fun renderAsKotlinConstant(): String = "${value}f"
}
public class KtDoubleConstantValue(
override val value: Double,
override val sourcePsi: KtElement?
) : KtConstantValue(ConstantValueKind.Double)
) : KtConstantValue(ConstantValueKind.Double) {
override fun renderAsKotlinConstant(): String = value.toString()
}
public class KtErrorConstantValue(
public val errorMessage: String,
override val sourcePsi: KtElement?,
) : KtConstantValue(ConstantValueKind.Error) {
override val value: ERROR_VALUE get() = ERROR_VALUE
override val value: Nothing
get() = error("Cannot get value for KtErrorConstantValue")
public object ERROR_VALUE
override fun renderAsKotlinConstant(): String {
return "error(\"$errorMessage\")"
}
}
}
public fun KtConstantValue.render(): String =
KtConstantValueRenderer.render(this)
@@ -12,7 +12,6 @@ import org.jetbrains.kotlin.analysis.api.KtConstantInitializerValue
import org.jetbrains.kotlin.analysis.api.KtInitializerValue
import org.jetbrains.kotlin.analysis.api.KtNonConstantInitializerValue
import org.jetbrains.kotlin.analysis.api.annotations.*
import org.jetbrains.kotlin.analysis.api.base.render
import org.jetbrains.kotlin.analysis.api.components.KtSymbolInfoProviderMixIn
import org.jetbrains.kotlin.analysis.api.symbols.markers.*
import org.jetbrains.kotlin.analysis.api.types.KtClassErrorType
@@ -216,7 +215,7 @@ public object DebugSymbolRenderer {
when (value) {
is KtConstantInitializerValue -> {
append("KtConstantInitializerValue(")
append(value.constant.render())
append(value.constant.renderAsKotlinConstant())
append(")")
}
is KtNonConstantInitializerValue -> {
@@ -1,3 +1,3 @@
expression: 3.14f
constant: 3.14
constant: 3.14f
constantValueKind: Float
@@ -1,3 +1,3 @@
expression: 42u
constant: 42
constant: 42u
constantValueKind: UInt