Claudiu-Vlad Ursache
07/29/2021, 12:47 PMKtNamedFunction
from a KtCallExpression
, or something similar. Is that possible at all, maybe with a Resolver or something? I followed the instructions in the documentation about getReferences()
, but I get a null value for a very simple example like this one:
fun add(x: Int, y: Int): Int {
return x + y
}
fun main(args : Array<String>) {
println(add(1 + 2, 3)) // <- How do I get from the PSI node of add to the PSI node of add's declaration
}
Youssef Shoaib [MOD]
07/29/2021, 1:15 PMgetReferences()
only works in the context of an IDE plugin, and it has no value when dealing with the Kotlin Frontend. You can see an example of how to do it over at my repo (the code might not make too much sense though) but the quick TL;DR is that you need an AnalysisHandlerExtension
and then in its analysisCompleted
function use the bindingTrace.bindingContext
to call KtCallElement.getResolvedCall(BindingContext)
and that'll simply get you a ResolvedCall
object that has a resultingDescriptor
property that then simply has all the info and data about the function that the call element is referring to.
(Also if you do check the repo make sure to check out and possibly vote for KT-44530 because the whole repo is a POC of that issue. The TL;DR of that issue is that the Kotlin compiler currently can't, but should, be able to inline some lambda returning functions. if that issue does get implemented, that can result in things as crazy as actual zero-overhead structs being possible idiomatically)Claudiu-Vlad Ursache
07/29/2021, 1:18 PMYoussef Shoaib [MOD]
07/29/2021, 1:20 PMClaudiu-Vlad Ursache
07/29/2021, 1:21 PMYoussef Shoaib [MOD]
07/29/2021, 1:21 PMClaudiu-Vlad Ursache
07/29/2021, 1:22 PM