Is there a way to list all of the IrFunction symbo...
# compiler
a
Is there a way to list all of the IrFunction symbols in scope of some IrExpression with some given name? For example say I am transforming some expression
myExpr
in a local scope:
Copy code
fun foo() { ... }

fun myLocalScope() {
  fun foo() { ... }
  myExpr
}
I want to get a list of all the
foo
functions that can be called in that scope. I've tried every variation of IrPluginContext.referenceFunctions that I can think of but nothing seems to be working.
d
There is no scope concept in backend IR, as all resolution happens in frontend and there is no need for backend to perform it
Also it's technically impossible, because import statements are erased at backend level
Hm, after some thinking I realised, that I kinda lied above All I told above was about frontend-like scopes Actually you can call absolutely any function if it is visible in the given context (e.g. it's not private) and you can provide all required arguments/receivers But still there is no way to acquire all functions with given name. Sure you can traverse the whole ir module for sources and collect them, but you can't do the same for dependencies, as they are loaded only on demand by given callable id
a
Let’s say I just want to call the function
foo
inside the myLocalScope function above. How do I do that?
d
Find the corresponding symbol (e.g. by traversing parents) and create an IrCall
a
You mean the IrDeclarationParent of myExpr? How do I create a CallableId from that?
d
Yes, this parent You don't need callable id if you already have required IrFunction or its symbol
You can create a visitor which will traverse any IR subtree you want and find all functions foo in it
a
I don’t have the IrFunction or IrFunctionSymbol. That’s the problem. The only thing I have is the IrDeclarationParent of myExpr and the name of the function to call. I’m guessing that because there’s no concept of “local scope” in the backend-ir I’d need to traverse the ItDeclarationParent and any preset packages?
Is there a way to traverse all existing extension functions defined on some symbol irregardless of package?
d
I was talking about smth like this
Copy code
fun findFunctions(expression: IrExpression): List<IrSimpleFunction> {
	val functions = mutableListOf<IrSimpleFunction>()
	val visitor = object : IrVisitorVoid {
		override fun visitElement(element: IrElement) {
			element.acceptChildrenVoid(this)
		}

		override fun visitSimpleFunction(function: IrSimpleFunction) {
			if (function.name.asString() == "foo") {
				functions += function
			}
			visitElement(function)
		}
	}
	expression.parent.acceptVoid(visitor)
	// or expression.containingFile.acceptVoid(visitor)
	// or expression.containingFile.parent.acceptVoid(visitor)
	return functions
}
Is there a way to traverse all existing extension functions defined on some symbol irregardless of package?
You can traverse all source functions in module using approach I showed to collect all functions from sources For functions in dependencies there is no way, you can find them only if you know CallableId