How can you get an IrClass (Reference) from an ann...
# compiler
h
How can you get an IrClass (Reference) from an annotation value used in a IrConstructorCall (via getAnnotation)? getAnnotationValueOrNull tries to cast the value to a IrConst, that misses IrClassReference as well as any const arrays that are valid in a JVM annotation.
p
You can find out by yourself. 1) Make sample code with needed declaration/expression. 2) In compiler plugin 'catch it' and log the dump() of needed ir element. Or debug your plugin and watch needed fields. To find out how to debug your plugin you can read this message (and message next to it): https://kotlinlang.slack.com/archives/C7L3JB43G/p1725626069009579?thread_ts=1725623682.461049&cid=C7L3JB43G
j
if you have the class id of that annotation, cannot you use
pluginContext.referenceClass(classId)
?
h
Thanks for the answers, it was more a question of: should the IR api include an IrConstExpression to model all possible constants annotation values?
i
This API indeed looks strange. I see that it is mostly used in Koltin/Native. Probably it should be renamed to something like
getConstAnnotationValueOrNull
. To get an argument from
IrConstructorCall
you can just call
someAnnotation.getValueArgument(index)
. The same way as you do for
IrCall
. But please be aware that this API will change in the nearest future.
h
Yes, I do use
getValueArgument
and changes are fine. But I still want to highlight if you want to add a IrConstExpression because for (jvm) annotation values, all values must be a compile constant, including const arrays and a class reference. So IMHO
getValueArgument
should not return
IrExpression
but a new
IrConstExpression
. (and maybe this also aligns with the const functions feature some day)
i
Yep, we are aware of this. Right now designing a new
IrConstExpression
node is not in out plans. As you said, we will get back to it when we start to design const functions because it is aligned.