From the `fir-plugins.md` doc file and the `fir-pl...
# k2-adopters
t
From the
fir-plugins.md
doc file and the
fir-plugin-prototype
it seems like there's no support for running a visitor/transformer similar to
IrGenerationExtension
. Is that a current limitation, or am I just missing something? I'd like to run a transformer visitor on FIR in any of the FIR phases. Is it supported and if not, will it be? Thanks! Or maybe if it'll still be through extensions like it is now, will there be support for more predicates, so we're not limited to annotations? For example a predicate like "all classes implementing an interface" or "all sealed interfaces", or even "all functions returning a type".
j
we were modifying function bodies with
FirBodyResolveTransformer
if I remember correctly
Have you checked all classes that implement
Copy code
abstract class FirTransformer<in D> : FirVisitor<FirElement, D>()
s
Hi!
IrGenerationExtension
is still supported in K2, note that FIR is a Compiler Frontend of K2, while IR is a backend part Take a look at Parcelize implementation, which supports both K1 and K2 compilers and uses
IrGenerationExtension
https://github.com/JetBrains/kotlin/blob/5802bf85878029ce840d0897d29ad5c4829c10e6/[…]c/org/jetbrains/kotlin/parcelize/ParcelizeComponentRegistrar.kt
Javier [8:49 PM]
we were modifying function bodies with
FirBodyResolveTransformer
if I remember correctly
Javier [8:49 PM]
> Have you checked all classes that implement
Copy code
abstract class FirTransformer<in D> : FirVisitor<FirElement, D>()
Note that
FirTransformer
and
FirBodyResolveTransformer
is actually a core internal API of FIR, that is highly-likely to change without prior notice Not sure how you can use it to change bodies, since its only usage is to resolve raw FIR
t
Thanks, I'll check the
FirBodyResolveTransformer
. I'm looking to edit the FIR so that there's new declarations visible to the source code, so
IrGenerationExtension
is too late for me
s
FIR part of a compiler plugin only needed if you want to generate user-visible declarations In order to do so, you will have to use
FirDeclarationGenerationExtension
t
Yeah, that's the one I played around with, but I haven't found how to generate anything out of nowhere, or how to predicate match something like "all interfaces"
s
Exactly! The problem is that the FIR actually runs in the IDE as well In order to match "all interfaces" you would need to be able to build FIR for every file in project, that would require substantial amount of time to complete
t
Yeah, but I don't need all of them at once, I can run for the current one, similar to how I can run for certain annotated types
Or maybe I'm misunderstanding the predicates altogether
s
Only if your plugin generates top-level callable declarations Otherwise, at the time when you generate say class, interfaces are not resolved yet
t
Interesting. I'm gonna dig deeper into the Kotlin codebase and possibly IDEA as well to see how that all works together, to see if what I'm trying to achieve is possible with today's APIs, or if there'd be something else needed. Thanks a lot for your help!
s
Let's imagine we have a predicate like
type that implements interface X
that is used to generate other classes, let's say
typeSome
Copy code
interface X {}

class My : MySome(), X {}

/* generated class MySome { ... } */
Here, in order to determine if
X
is really that
interface X
that we are looking for, we need to resolve the super-type list In order to do so, we need to query
FirDeclarationGenerationExtension
for a name
MySome
, which, in turn, will call predicate matching to find classes that implement
interface X
, and here we have a cycle
There are also good talks from KotlinConf that are explaining how FIR plugins work:

https://www.youtube.com/watch?v=Pl-89n9wDqo

https://www.youtube.com/watch?v=wUGfuWHCqrc