https://kotlinlang.org logo
Title
s

sam

06/21/2022, 9:22 AM
I'm trying to add a top level function which I can then invoke from native using
-entry
I can see the function looks ok using dump, but I get an error saying it cannot be found. If I create the function manually, then it looks identical to my generated output. Is there something special that needs to be done for the native task to pick up the entry point ?
I create the function like this:
val main = pluginContext.irFactory.buildFun {
               name = Name.identifier("mymain")
               returnType = pluginContext.irBuiltIns.unitType
               visibility = DescriptorVisibilities.PUBLIC
               modality = Modality.FINAL
            }.also { func: IrSimpleFunction ->
               func.body = DeclarationIrBuilder(pluginContext, func.symbol).irBlockBody {
                  val callPrintln = irCall(funPrintln)
                  callPrintln.putValueArgument(0, irString("Hello, World!"))
                  +callPrintln
               }
            }
I am adding the function to an arbitrary file, by using
fragment.files.first().addChild(main)
inside
visitModuleFragment
Dump from the generated function:
FUN name:mymain visibility:public modality:FINAL <> () returnType:kotlin.Unit
  BLOCK_BODY
    CALL 'public final fun println (message: kotlin.Any?): kotlin.Unit declared in <http://kotlin.io|kotlin.io>' type=kotlin.Unit origin=null
      message: CONST String type=kotlin.String value="Hello, World!"
y

Youssef Shoaib [MOD]

06/21/2022, 9:32 AM
Maybe it needs a
IrDeclarationOrigin.DEFINED
?
s

sam

06/21/2022, 9:35 AM
like this?
val main = pluginContext.irFactory.buildFun {
               name = Name.identifier("mymain")
               returnType = pluginContext.irBuiltIns.unitType
               visibility = DescriptorVisibilities.PUBLIC
               modality = Modality.FINAL
               origin = IrDeclarationOrigin.DEFINED
            }
(That didn't work)