Hey, Im trying to find the closest equivalent to `...
# compiler
s
Hey, Im trying to find the closest equivalent to
com.sun.tools.javac.code.Symbol
from the javac compiler API. In another project Im working on, we're able to keep a mapping of Symbol to a String and be able to lookup a symbol, be it from a return type declaration, variable type etc and retrieve its String association. What would be the best option in the kotlin compiler API? Im walking the trees of KtFiles via
KtTreeVisitorVoid
invoked in
analysisCompleted
from an
AnalysisHandlerExtension
subclass 🙂
y
using the various methods that need a
bindingContext
should do the trick. For example, there's
KtExpression?.getType(bindingContext)
. There's also
KtElement?.getResolvedCall(bindingContext)
. You can obtain a
bindingContext
inside of
analysisCompleted
by doing
bindingTrace.bindingContext
I make some use of both of those techniques over on my kotlin plugin project specifically in this AnalysisHandler. I basically use it to replace certain function calls to an external library by shadowing that function in a local file adding using
AnalysisResult.RetryWithAdditionalRoots
because I need the sources of those functions to perform my own inlining (long story but if you're interested check out KT-44530 but basically I'm implementing structs and currying and a bunch of other features with just one optimization).
TL;DR: It sounds like you simply need to use
expression.getType(bindingTrace.bindingContext)?.getJetTypeFqName()
s
Im not sure
getJetTypeFqName
is quite what I want, as I build the associated string based on the "path" to the construct e.g.
java/util/Arrays#asList().
for the
asList
method on the
java.util.Arrays
class. By having some class that lets me walk the "owner" hierarchy would be a generally good start, but I need the same class to be used (or derivable) at reference site to these symbols so I can extract the same string from the map In slightly other words, I need to be able to get
java/util/Arrays#asList().
from both the declaration site of that method as well as the usage/call/reference sites of that method
And for that I will need to know the class type of the key of the map
y
Then at call site you need to do
Copy code
val resolvedCall = KtCallExpression.getResolvedCall(bindingTrace.bindingContext)
from that you can do
resolvedCall.resultingDescriptor
and that resulting descriptor is usually a modified copy that includes info like filled in type parameters and so to get the "real" descriptor just call
.original
on that then with that descriptor you can do things like call
.containingDeclaration
on it which, as the name suggests, gives you the declaration that this method was defined in, which you can then call
.fqNameSafe
on
s
okay so it seems like
DeclarationDescriptor
is indeed the key type for the map. Was originally going that route but wasnt sure if it would be the right choice, but it seems like it is 🙂 thanks
y
For declaration site you can then do this in visitNamedFunction:
BindingUtils.getFunctionDescriptor(bindingTrace.bindingContext, expression)
and just in case call
.original
on that and that should give you the same DeclarationDescriptor as the one you get from the resolved call
👍 1