:wave: Hi everyone, Is there a way to generate a `...
# compiler
d
đź‘‹ Hi everyone, Is there a way to generate a
.kt
file using Kotlin Poet during the FIR stage - for example, inside a FirFunctionCallChecker- and include it in the build source sets?
đźš« 2
j
I don't think so
h
You should also don't run any heavy tasks in Fir, if you enable FIR plugins in IntelliJ to get IDE support.
d
In my current project, we need to create a Kotlin file with several constants containing strings extracted from the FIR analysis. These constants will also be used in the source code. We are considering two approaches: • Generating the
.kt
file with Kotlin Poet, which isn’t recognized in the source set and causes the initial build to fail (though a second build recognizes the generated file because it already exists). • Creating a function declaration in the FIR stage using
FirDeclarationGenerationExtension
, then implementing the function body during the IR stage using
AbstractTransformerForGenerator
. However, this approach produces generated code that’s less transparent than a
.kt
file, making it harder to view the content easily. Additionally, we face challenges passing information from FIR to IR—*how can we facilitate communication between them?*
Are any of our methods suitable for extracting information from the FIR analysis to use during build time?
h
If you want to produce source code, use ksp (or the very experimental analytics api). Otherwise, use the approach of FIR and IR like you described with the downside/advantage of producing IR code.
It also depends what you want to do, what’s the use-case. And do you want to have IDE support?
And what data do you need to pass to IR? You could use a GeneratedDeclarationKey for data, I think.
d
Great, I'd like to explain our use case. We must analyze the parameters used to instantiate a particular class and extract their string representations (FirLiteralExpression) into another file. The analysis is implemented using
FirAdditionalCheckersExtension
but I don't know how to pass the Strings obtained during FIR, to the
IrGenerationExtension
that generates the code
IDE support is not required, with the generated classes included in the KMP source sets and recognized for autocompletion is enough
t
AFAIK you can just use a global variable to pass info from a FIR plugin to an IR one. I've asked the same question before and that was the answer.
👍 1
b
Another option for sharing data between FIR and IR is to create some custom data structure to hold this data and pass it to both your custom
FirExtensionRegistrar
and
IrGenerationExtension
since you have to manually create both in the plugin registrar and can pass them constructor parameters.