https://kotlinlang.org logo
#compiler
Title
# compiler
f

Federico Tomassetti

03/31/2024, 11:07 AM
How can I invoke a method on an object companion in an Ir Plugin?
Copy code
private fun IrBuilderWithScope.dataType(pluginContext: IrPluginContext, irType: IrType): IrExpression {
             ...
            val primitiveTypeClass = pluginContext.referenceClass(PrimitiveType::class.classId)!!
            val primitiveTypeCompanionClass = (primitiveTypeClass.owner as IrClass).nestedClasses.find { it.isCompanion }!!
            val primitiveTypeCompanionGet = primitiveTypeCompanionClass.functions.find { it.name.asString() == PrimitiveType.Companion::get.name }!!
            primitiveTypeClass.owner.companionObject()
            // FIXME I think I need somehow to indicate the PrimitiveType Companion Object
            return irCall(primitiveTypeCompanionGet).apply {
                // MAYBE SHOULD I SET THE DISPATCHER RECEIVER? HOW? TO WHICH VALUE?
                // Pass the primitive type name
                putValueArgument(0, irString(irType.classFqName!!.asString()))
            }
        }
    }
j

Javier

03/31/2024, 1:04 PM
I highly recommend to set in the test file exactly what you want to do but “duplicated” and then debug to see what dispatchers you are missing and so on.
Copy code
fun box():String {
    val isSame = whatever() == Foo2.some2()

    return if (isSame) “Ok” else “Fail”
}

class Foo {
    companion object {
        some() = “some”
    }
}

class Foo2 {
    companion object {
        some2() = “some”
    }
}
whatever()
should be
Foo.some()
, you need to compare what you are building with the
Foo2.some2()
you already have in the tree to extract it and see each value.
f

Federico Tomassetti

04/01/2024, 8:45 AM
In the end I needed to use
irGetObject(CompanionClass)