Could anyone point me to an example of how I could...
# compiler
a
Could anyone point me to an example of how I could create Backend IR that makes a simple
() -> T
lambda function using IrBuilderWithScope? I'm seeing
builder.irFunctionReference
but I don't know how to use that.
a
There is an IR node called IrFunctionExpression that accepts a simple function declaration and its type. So, to create lambda you should do something like this: • Create a simple function with
LOCAL_FUNCTION_FOR_LAMBDA
origin (ofc, fill it with types you need): https://github.com/ForteScarlet/kotlin-suspend-transform-compiler-plugin/blob/2c3bf967fdc81e20fc73ac90e8e54ce51833d35b/compiler/suspend-transform-plugin/src/main/kotlin/love/forte/plugin/suspendtrans/utils/IrFunctionUtils.kt#L147 • Create IrFunctionExpression IR-node, that accepts the previously created simple function and its type. As far as I know, there is no IrBuilderWithScope method for this type of IR-node, but this is how we represent lambdas inside the compiler, so you can create it with the
IrFunctionExpressionImpl
constructor.
a
Thanks! Let me give this a shot. I had no idea how the builder-patterns work for IrBlockBodyBuilder and a bunch of other builders so this is super useful!
Another quick question, what is the
type
of the
IrFunctionExpressionImpl
supposed to be? Is it the type of the whole function?
Ah! I see what you did with
context.symbols.suspendFunctionN
. Very nice!
🫡 1
a
Yes, the type is the type of the whole function.
a
I keep getting
RETURN: Incompatible return type
. Any chance you've seen that error before?
NVM, figured it out. Suffice it to say the
createIrBuilder(symbol)
part is very important
1