Did anyone figured out how to replace IrFunctionAc...
# compiler
b
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
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
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
It did work, i just forgot to return it upstream
But thanks for the tips anyways!
How is visitIrCall different from visitIrFunctionAccess?
s
FunctionAccess is something more generic, so you should be prepared to handle additional cases there
You can check IrFunctionAccess interface for things that extend it