https://kotlinlang.org logo
#arrow-meta
Title
# arrow-meta
a

appmattus

03/29/2023, 4:57 PM
hey, so i’m currently in the process of writing a kotlin compiler plugin using meta, although struggling with my approach for modifying IR.
the gist of what i’m trying to do is to alter a function call,
someFunction
in this example, to automagically specify a missing parameter, as an example:
Copy code
class MyClass : ViewModel() {
    val result = viewModelScope.someFunction()
}

fun CoroutineScope.someFunction(value: String? = null) {
    println(value)
}
With the idea being I can populate the value in
someFunction
based on something else in the class - for example the class name. Intercepting the
IrCall
seems easy enough as well as adding the additional argument to the call:
Copy code
private val Meta.somePlugin: CliPlugin
    get() = "Some Plugin" {
        meta(
            irCall { irCall ->
                if (irCall.symbol.owner.kotlinFqName.asString() == "someFunction") {
                    // do the magic argument adding
                 }
            }
        )
The problem I’m having though is there seems no obvious way to figure out the parent of an `IrCall`; unlike
IrFunction
there is no
parentClassOrNull
Is there a better approach for being able to figure out where the
IrCall
came from or performing the transformation? I’m assuming I should be looking at using
transformChildren
although just not 100% sure what type of
IrElement
I should apply that to!
r

raulraja

03/29/2023, 5:11 PM
Hi @appmattus, Instead of intercepting
irCall
you could intercept
irDeclaration
(the parent) and the on each intercepted declaration search inside for calls
a

appmattus

03/29/2023, 8:04 PM
ah thanks, got it working i first tried
irDeclaration
but noticed the
IrCall
was found twice, once for the property and the other for the backing field of the property. now i’m specifically using
transformChildren
on
irProperty
. it’s great to see the class file transformed 🙂 meta has certainly made the process of creating a compiler plugin seem pretty trivial, great work
r

raulraja

03/30/2023, 7:22 AM
We hope to improve it once the new FIR K2 is stable and we can develop utilities for it like the ones we will port to meta from our tests at arrow-reflection https://github.com/arrow-kt/arrow-reflection/tree/main/arrow-reflect-annotations/src/main/kotlin/arrow/meta/samples
If it all goes well this would add macro suport to the frontend or similar features that can interop with IDEA as compiler plugins.
23 Views