FIR IDE: more comprehensive abstractions of annotation values

This commit is contained in:
Jinseong Jeon
2021-09-16 12:04:28 -07:00
committed by Ilya Kirillov
parent fe41c4513f
commit 6ef2dad895
36 changed files with 316 additions and 52 deletions
@@ -5,14 +5,14 @@
package org.jetbrains.kotlin.analysis.api.components
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtSimpleConstantValue
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtConstantValue
import org.jetbrains.kotlin.psi.KtExpression
public abstract class KtCompileTimeConstantProvider : KtAnalysisSessionComponent() {
public abstract fun evaluate(expression: KtExpression): KtSimpleConstantValue<*>?
public abstract fun evaluate(expression: KtExpression): KtConstantValue?
}
public interface KtCompileTimeConstantProviderMixIn : KtAnalysisSessionMixIn {
public fun KtExpression.evaluate(): KtSimpleConstantValue<*>? =
public fun KtExpression.evaluate(): KtConstantValue? =
analysisSession.compileTimeConstantProvider.evaluate(this)
}
@@ -87,6 +87,7 @@ public object DebugSymbolRenderer {
is KtFunctionSymbol -> renderValue(value.callableIdIfNonLocal ?: "<local>/${value.name}")
is KtSamConstructorSymbol -> renderValue(value.callableIdIfNonLocal ?: "<local>/${value.name}")
is KtConstructorSymbol -> "<constructor>"
is KtEnumEntrySymbol -> renderValue(value.callableIdIfNonLocal ?: "<local>/${value.name}")
is KtNamedSymbol -> renderValue(value.name)
is KtPropertyGetterSymbol -> "<getter>"
is KtPropertySetterSymbol -> "<setter>"
@@ -95,6 +96,7 @@ public object DebugSymbolRenderer {
"${value::class.simpleName!!}($symbolTag)"
}
is KtSimpleConstantValue<*> -> renderValue(value.value)
is KtEnumEntryValue -> "KtEnumEntryValue(${renderValue(value.enumEntrySymbol)})"
is KtNamedConstantValue -> "${renderValue(value.name)} = ${renderValue(value.expression)}"
is KtAnnotationCall -> buildString {
append(renderValue(value.classId))
@@ -5,11 +5,40 @@
package org.jetbrains.kotlin.analysis.api.symbols.markers
import org.jetbrains.kotlin.analysis.api.symbols.KtEnumEntrySymbol
import org.jetbrains.kotlin.types.ConstantValueKind
/**
* Annotation values are expected to be compile-time constants. According to the
* [spec](https://kotlinlang.org/spec/annotations.html#annotation-values),
* allowed kinds are:
* * integer types,
* * string type,
* * enum types,
* * other annotation types, and
* * array of aforementioned types
*
* [KtSimpleConstantValue] covers first two kinds;
* [KtEnumEntryValue] corresponds to enum types;
* [KtAnnotationConstantValue] represents annotation types (with annotation fq name and arguments); and
* [KtArrayConstantValue] abstracts an array of [KtConstantValue]s.
*/
public sealed class KtConstantValue
public object KtUnsupportedConstantValue : KtConstantValue()
public class KtArrayConstantValue(
public val values: Collection<KtConstantValue>
) : KtConstantValue()
public class KtAnnotationConstantValue(
public val fqName: String?,
public val arguments: List<KtNamedConstantValue>
) : KtConstantValue()
public class KtEnumEntryValue(
public val enumEntrySymbol: KtEnumEntrySymbol
) : KtConstantValue()
public data class KtSimpleConstantValue<T>(val constantValueKind: ConstantValueKind<T>, val value: T) : KtConstantValue() {
public fun toConst(): Any? {
return (value as? Long)?.let {
@@ -0,0 +1,6 @@
annotation class Annotation(vararg val strings: String)
annotation class AnnotationInner(val value: Annotation)
@AnnotationInner(<expr>Annotation(strings = arrayOf("v1", "v2"))</expr>)
class C
@@ -0,0 +1,6 @@
expression: Annotation(strings = arrayOf("v1", "v2"))
constant_value: KtAnnotationConstantValue(Annotation, (strings = KtArrayConstantValue [
KtSimpleConstantValue(constantValueKind=String, value=v1)
KtSimpleConstantValue(constantValueKind=String, value=v2)
]))
constant: null
@@ -0,0 +1,6 @@
annotation class Annotation(vararg val strings: String)
annotation class AnnotationInner(val value: Annotation)
@AnnotationInner(<expr>Annotation(["v1", "v2"])</expr>)
class C
@@ -0,0 +1,6 @@
expression: Annotation(["v1", "v2"])
constant_value: KtAnnotationConstantValue(Annotation, (strings = KtArrayConstantValue [
KtSimpleConstantValue(constantValueKind=String, value=v1)
KtSimpleConstantValue(constantValueKind=String, value=v2)
]))
constant: null
@@ -0,0 +1,6 @@
annotation class Annotation(vararg val strings: String)
annotation class AnnotationArray(vararg val annos: Annotation)
@AnnotationArray(<expr>annos = arrayOf(Annotation("v1", "v2"), Annotation(strings = arrayOf("v3", "v4")))</expr>)
class C
@@ -0,0 +1,12 @@
expression: arrayOf(Annotation("v1", "v2"), Annotation(strings = arrayOf("v3", "v4")))
constant_value: KtArrayConstantValue [
KtAnnotationConstantValue(Annotation, (strings = KtArrayConstantValue [
KtSimpleConstantValue(constantValueKind=String, value=v1)
KtSimpleConstantValue(constantValueKind=String, value=v2)
]))
KtAnnotationConstantValue(Annotation, (strings = KtArrayConstantValue [
KtSimpleConstantValue(constantValueKind=String, value=v3)
KtSimpleConstantValue(constantValueKind=String, value=v4)
]))
]
constant: null
@@ -0,0 +1,6 @@
annotation class Annotation(vararg val strings: String)
annotation class AnnotationArray(vararg val annos: Annotation)
@AnnotationArray(<expr>[Annotation("v1", "v2"), Annotation(["v3", "v4"])]</expr>)
class C
@@ -0,0 +1,12 @@
expression: [Annotation("v1", "v2"), Annotation(["v3", "v4"])]
constant_value: KtArrayConstantValue [
KtAnnotationConstantValue(Annotation, (strings = KtArrayConstantValue [
KtSimpleConstantValue(constantValueKind=String, value=v1)
KtSimpleConstantValue(constantValueKind=String, value=v2)
]))
KtAnnotationConstantValue(Annotation, (strings = KtArrayConstantValue [
KtSimpleConstantValue(constantValueKind=String, value=v3)
KtSimpleConstantValue(constantValueKind=String, value=v4)
]))
]
constant: null
@@ -0,0 +1,10 @@
enum class Color {
R,
G,
B
}
annotation class Annotation(val color : Color)
@Annotation(<expr>Color.R</expr>)
class C
@@ -0,0 +1,3 @@
expression: Color.R
constant_value: KtEnumEntryValue(/Color.R)
constant: null
@@ -0,0 +1,4 @@
annotation class Annotation(vararg val values: String)
@Annotation(<expr>"42"</expr>)
class C
@@ -0,0 +1,3 @@
expression: "42"
constant_value: KtSimpleConstantValue(constantValueKind=String, value=42)
constant: 42
@@ -296,7 +296,7 @@ KtFirKotlinPropertySymbol:
kotlin/Deprecated
]
annotations: [
kotlin/Deprecated(level = KtUnsupportedConstantValue, message = don't use j)
kotlin/Deprecated(level = KtEnumEntryValue(KtFirEnumEntrySymbol(kotlin/DeprecationLevel.ERROR)), message = don't use j)
psi: KtAnnotationEntry
]
callableIdIfNonLocal: /j
@@ -332,7 +332,7 @@ KtFirKotlinPropertySymbol:
kotlin/Deprecated
]
annotations: [
kotlin/Deprecated(level = KtUnsupportedConstantValue, message = don't use j2)
kotlin/Deprecated(level = KtEnumEntryValue(KtFirEnumEntrySymbol(kotlin/DeprecationLevel.HIDDEN)), message = don't use j2)
psi: KtAnnotationEntry
]
callableIdIfNonLocal: /j2
@@ -3,7 +3,7 @@ KtFirNamedClassOrObjectSymbol:
kotlin/annotation/Target
]
annotations: [
kotlin/annotation/Target(allowedTargets = KtUnsupportedConstantValue)
kotlin/annotation/Target(allowedTargets = KtEnumEntryValue(KtFirEnumEntrySymbol(kotlin/annotation/AnnotationTarget.TYPE)))
psi: KtAnnotationEntry
]
classIdIfNonLocal: Anno1
@@ -30,7 +30,7 @@ KtFirNamedClassOrObjectSymbol:
kotlin/annotation/Target
]
annotations: [
kotlin/annotation/Target(allowedTargets = KtUnsupportedConstantValue)
kotlin/annotation/Target(allowedTargets = KtEnumEntryValue(KtFirEnumEntrySymbol(kotlin/annotation/AnnotationTarget.TYPE)))
psi: KtAnnotationEntry
]
classIdIfNonLocal: Anno2
@@ -57,7 +57,7 @@ KtFirNamedClassOrObjectSymbol:
kotlin/annotation/Target
]
annotations: [
kotlin/annotation/Target(allowedTargets = KtUnsupportedConstantValue)
kotlin/annotation/Target(allowedTargets = KtEnumEntryValue(KtFirEnumEntrySymbol(kotlin/annotation/AnnotationTarget.TYPE)))
psi: KtAnnotationEntry
]
classIdIfNonLocal: Anno3
@@ -84,7 +84,7 @@ KtFirNamedClassOrObjectSymbol:
kotlin/annotation/Target
]
annotations: [
kotlin/annotation/Target(allowedTargets = KtUnsupportedConstantValue)
kotlin/annotation/Target(allowedTargets = KtEnumEntryValue(KtFirEnumEntrySymbol(kotlin/annotation/AnnotationTarget.TYPE)))
psi: KtAnnotationEntry
]
classIdIfNonLocal: Anno4