Hey all :slightly_smiling_face: I’m new to Kotlin...
# compiler
d
Hey all 🙂 I’m new to Kotlin compiler plugins and hope this is the right place to ask. I’m playing with the new IR compiler plugins. I created a simple test, that adds a function to an annotated class. The function just calls
println("I'm here");
I can compile the class and when I try to use
kotlinc
to compile another class that calls the generated function, I get:
Copy code
kotlinc -classpath . test.kt
test.kt:2:10: error: unresolved reference: generatedFunction
  Main().generatedFunction()
But when I do the same with
javac
, everything is ok. I can also run the class and I get the desired output
Copy code
java Test
I'm here
Any pointers would be highly appreciated 🙂 p.s. Is there any documentation about writing compiler plugins with IR out there?
d
Thanks 😊 I read this
r
The lack of resolution is likely because when you're manipulating IR, you're adding functions to the backend, while resolution is done by the frontend, and thus doesn't see your added functions. Until the new FE is done, you can do something like creating a
fun generatedFunction() = error("")
and replacing the body in IR, or use SyntheticResolveExtension (I forget the exact name, it might be SyntheticResolutionExtension).
d
Thank you!!! I’ll try with the Synthetic approach 😊
r
It needs to be part of the IDE plugin, FYI. There might be a more recent way, too, it's been a while since I tried to expose things.
d
Thanks! I know that, but I’m not that far. I want to be able to generate something that works first (cli) and will try to do the IDE plugin next.
@rnett Thank you! It works now. I register a
SyntheticResolveExtension
and generate a new synthetic method. And then in the
IrGenerationExtension
I generate the body of the function and it works great! Thanks!
@rnett and last question 🙂 Any docs for writing the IDE plugin?
r
I don't know of any great ones as far as compiler plugin companions go, but I think all you need to do is register the
SyntheticResolveExtension
. The full docs are [here](https://plugins.jetbrains.com/docs/intellij/welcome.html) but that's mostly irreverent to you. If I remember right you just need a gradle module w/ the right setup and a xml file that registers your
SyntheticResolveExtension
(presumably by depending on your compiler plugin).
d
Thanks! Will look into it 🙂