n00b compiler plugin question: I've done a few KSP...
# compiler
t
n00b compiler plugin question: I've done a few KSP annotations, but now I think I need to do a compiler plugin. I want to write a logging library that uses C++'s (rust/zig/python) std::fmt syntax, and moves the data to another process (shared-mem) for the actual logging. So if I write something like
logDebug("name:{s} id:{d}", name, id)
I want to generate this function in the compiler plugin.
My questions are: Does this seem like a fir, or ir type thing? I don't think I can use kotlinPoet to generate the function iplm (can it?) How can I "name-mangle" the function so functions w/ the type types of arguments are unique? (kinda monoporphiaztion of each log call).
or any other advice.
I'm not worried about actually checking that the for fmt string and params have matching # of args and expected types.. At least not atm. I'll likely use C++ or Rust in the out-of-process "de-serlizer"...
c
You‘ll need fir for the function declaration and a ir for the actual implementation.
y
Sounds like an IR use-case IMO. Power Assert is tangentially similar, so it might help in implementation. My understanding is you don't need any functions to be visible to the user other than
logDebug
, correct? You'll do some monomorphization of calls, but those mangled functions won't be visible to e.g. the autocomplete in IDE?
t
@Youssef Shoaib [MOD] I think so.
logDebug
won't be defined anyplace until the plugin runs. I don't care about being able to "see" it's impl in the IDE or anyting (be handy for debugging though).
c
Seeing in the IDE also means that it will be marked as an error „function reference not found“ in the IDE though.
t
@Chrimaeon yeah. I already have that problem w/ KSP generated code. (It's a bazel thing). So I'm kinda used to it. Maybe create a stub that uses varargs?
y
Yeah if you just use a stub with varargs, and then replace it in the IR, you'll be fine. You can also, instead of using that syntax, simply taken a string template, and intercept its arguments:
logDebug("name:$name id:$id")
In fact, this is very similar in idea to what you want.
c
Why not just create the FIR declaration? That’s the propose of having FIR and IR separated.
t
oh, that is interesting. Is there a way to pull those apart progrmatically? Or do I need to write my own parser?
y
If the IDE doesn't need to see it, then you don't need to use FIR at all. I believe the IR literally just has an
IrInterpolatedString
or something, so you can extract them from there (and ofc the type information is available). Terpal definitely does this in IR, so you can see how they do it and copy it
c
FiR is used by the IDE to „compile“ your code and the error is not displayed any more. That’s why I think it’s the cleaner approach 🤷‍♂️
t
it's a polygot project. So I was hoping to use the same format as the other languages. std::fmt seems like the defacto-standard atm. Maybe I can translate from kotlin to std:fmt w/ this.
Then all the function signatures would be the same. Might make this a lot easier.
@Chrimaeon I think fir s stubs is a better idea. Thanks.
Is the Power Assert code in the compiler repro, or does it live someplace else?
t
Very helpful. Thanks to both of you.
Oh. One other question: Say as part of this I wanted to output a .h file as well (or some other language). Is there any magic needed to do tihs? Or can I just open a file and write?
h
You can just open a file, but ideally you want also to wire it with Gradle and bazel too, I guess, to register the output to your build system.
And you should create the file in IR to not write files when using the IDE (FIR).
t
good to know. thanks.
n
Somewhat related, so maybe useful. I have a pet-project that does some transformations to logging-logging API calls at the IR-layer. I did not need the FIR-layer as I’m transforming existing API calls. https://github.com/nemecec/kotlin-logging-compile-time-plugin Among other things, it replaces parameterised SLF4J-style API calls (e.g.
log.debug("Hello {}", world)
) with String concatenation (e.g.
log.debug("Hello " + world)
). I’m actually planning to remove that feature but maybe you find it useful as a starting point (or to copy some code).
t
@Neeme Praks thanks for the links. This looks pretty close in some regards. Will use as a reference for sure. Thanks!
🙌 1