Did anyone figured out how to replace IrFunctionAccessExpression with another one in BE IR? I really don't want to rewrite an entire function just to replace a single function invocation
Big Chungus
06/12/2022, 11:14 AM
This doesn't seem to work even though the logged dump does return the call I want
Copy code
override fun visitFunctionAccess(expression: IrFunctionAccessExpression): IrExpression {
val candidates = context.referenceFunctions(expression.symbol.owner.kotlinFqName)
val pick = candidates.first { fn ->
fn.owner.valueParameters.any { it.type.classOrNull == klipContextClass }
}
debug { "pick: $pick" }
val irBuilder = DeclarationIrBuilder(context, expression.symbol)
val rewrite = irBuilder.irCall(pick).also {
it.putValueArgument(0, klipContextConstructorCall)
for (i in 0 until expression.valueArgumentsCount) {
it.putValueArgument(i + 1, expression.getValueArgument(i))
}
for (i in 0 until expression.typeArgumentsCount) {
it.putTypeArgument(i, expression.getTypeArgument(i))
}
debug { it.dump() }
}
return rewrite
}
s
shikasd
06/12/2022, 3:18 PM
That should work, although you maybe want to visitIrCall instead
How do you run this transform?
Btw, you can dump on the file/module level instead to check if it got applied
b
Big Chungus
06/12/2022, 3:36 PM
It did work, i just forgot to return it upstream
Big Chungus
06/12/2022, 3:36 PM
But thanks for the tips anyways!
Big Chungus
06/12/2022, 3:37 PM
How is visitIrCall different from visitIrFunctionAccess?
s
shikasd
06/12/2022, 3:40 PM
FunctionAccess is something more generic, so you should be prepared to handle additional cases there
shikasd
06/12/2022, 3:41 PM
You can check IrFunctionAccess interface for things that extend it