I am still struggling with generating IR for calli...
# compiler
j
I am still struggling with generating IR for calling a lambda. For example for
forEach
. I have difficulties setting the "action" parameter. This is the IR-dump I want to generate:
Copy code
action: FUN_EXPR type=kotlin.Function1<String, kotlin.Unit> origin=LAMBDA
          FUN LOCAL_FUNCTION_FOR_LAMBDA name:<anonymous> visibility:local modality:FINAL <> (it:String) returnType:kotlin.Unit
            VALUE_PARAMETER name:it index:0 type:String
            BLOCK_BODY
However, I can't get it to create a
FUN_EXPR
of type
kotlin.Function1<String, kotlin.Unit>
. Is
irFactory.buildFun
the proper approach? Thats the current code - which does not work -
Copy code
putValueArgument(0, irBlock(
              origin = IrStatementOrigin.LAMBDA,
              resultType = context.irBuiltIns.unitType,
            ) {
              +pluginContext.irFactory.buildFun {
                name = Name.identifier("")
                type = context.irBuiltIns.getKFunctionType(
                  returnType = context.irBuiltIns.unitType,
                  parameterTypes = listOf(context.irBuiltIns.stringType)
                )
                visibility = DescriptorVisibilities.LOCAL
                modality = Modality.FINAL
                origin = IrDeclarationOrigin.LOCAL_FUNCTION_FOR_LAMBDA
              }.also {
                it.dispatchReceiverParameter = null
                it.extensionReceiverParameter = null
                it.body = irBlockBody { }
              }
            })
Which generates following IR
Copy code
action: BLOCK type=kotlin.Unit origin=LAMBDA
          FUN LOCAL_FUNCTION_FOR_LAMBDA name: visibility:local modality:FINAL <> () returnType:<Uninitialized>
            BLOCK_BODY
Any hints or clues?
r
You need to create an instance of
IrFunctionExpression
, and pass that as the argument. This is a now quite out of date function I had to do that. Last time I was working with IR you also had to explicitly set the return type of the lambda