What's the recommended way to generate large amoun...
# compiler
m
What's the recommended way to generate large amounts of IR for native using a compiler plugin?
y
Do you mean like dealing with the compilation times for a huge amount of IR? Because yeah in that case you're gonna need to worry about how to make your code as efficient as possible. If you need a tutorial on setting up a plugin to generate IR, check out Brian Norm's Writing Your Second Kotlin Compiler Plugin
m
Moreso about how I'm gonna make effectively 300 lines of kotlin code through IR...
Most of this seems similar to building an AST tree which is gonna take me months to do, whereas if I could template a kotlin source file it'd be much easier
For example, that link doesn't cover cases like mine:
Copy code
private val getattroConfigurable = staticCFunction { self: PyObjectT, attr: PyObjectT ->
    val obj = self!!.kt.cast<Configurable>()
    val name = attr.toKotlin<String>()
    val attrObj = obj.attrs[name]

    if (attrObj != null) {
        attrObj.get().toPython()
    } else {
        PyObject_GenericGetAttr(self, attr)
    }
}
where I use lambda arguments, not null assertions, generics, type aliases, conditions, external method calls, and so on.
It does say it's recommended to dump valid code, but there's no testing framework for kotlin/native available as far as I know, meaning I can't test these things
y
2 things: 1. Creating it in the AST (IR really) is not that hard to do once you're familiar to it, but sure it's a lot of effort 2. if you already have a specific template file, there's a hook in the compiler called
CollectAdditionalSourcesExtension
. Using that, you can then in the IR phase look at the IR of your compiled template, and you can copy it over multiple times (using
IrElement.deepCopyWithSymbols
) and you can edit specific elements of it to match your desired output (for example, you can refer to specific functions, or you can delete certain elements, or get certain values).
Do you have a concrete example of what you want to do?
m
I basically just need to generate C functions to wrap a kotlin-defined class, meaning for every annotated method (which I've already filtered on) I can just put in a very generic piece of code to call said function and convert between the C and Kotlin types
then after that I just need to call a few other methods and generate one more method to fully initialize the module