How do I say compiler (or its JVM backend at least...
# compiler
g
How do I say compiler (or its JVM backend at least (for now)) to make some member function of a class to be the class's method instead of static function (with additional first argument)? I wrote a plugin that creates a class with a method. Here is a small snippet of the code:
Copy code
val benchmarkClass = irFactory.buildClass {
    origin = pluginDeclarationOrigin
    name = Name.identifier("KoneBenchmarkClassFor${subdeclaration.name.identifier.replaceFirstChar { it.uppercaseChar() }}")
    kind = CLASS
    modality = OPEN
    visibility = DescriptorVisibilities.PUBLIC
}
val setupFunction = benchmarkClass.addFunction {
    origin = pluginDeclarationOrigin
    name = Name.identifier("setup")
    visibility = DescriptorVisibilities.PUBLIC
    returnType = pluginContext.irBuiltIns.unitType
}.apply {
    addValueParameter {
        origin = pluginDeclarationOrigin
        name = SpecialNames.THIS
        visibility = DescriptorVisibilities.PUBLIC
        kind = DispatchReceiver
        type = benchmarkClass.symbol.defaultType
    }
}
I thought that the only criterion of a function (
setupFunction
here) to be a method (instead of static member) is to have a dispatch receiver. But this does not work for me. After compiling some code with the plugin I find the function to be static member in JVM bytecode. What should I do in such a case?
✅ 1
y
This is a cop-out answer, I know, but write Kotlin source code of what you want, and compare the dumped IR. There are instructions on how to do so here, but I personally like writing a box test or similar using
internal-test-framework
, then using a debugger in my
IrGenerationExtension
and exploring the IR's structure that way
g
Oh, I usually do this as well. But forgot about it for now until you reminded me. Thanks, I'll try it right now.
I guess, I found the culprit...
y
Pure guess: surely there's some
addDispatchParameter
or something like that. Maybe look for usages of
IrParameterKind.DispatchReceiver
g
Autocomplete finds nothing 😞 I'll try the looking for usages.
I think,
buildReceiverParameter
is what we are looking for. But I would never guess it was called just RECEIVER parameter...
👀 1
Just for the record. The problem is solved. Now everything compiles well and the way I needed!
❤️ 1