Strum355
04/11/2021, 10:35 PMcom.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 🙂Youssef Shoaib [MOD]
04/11/2021, 10:46 PMbindingContext
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
Youssef Shoaib [MOD]
04/11/2021, 10:54 PMAnalysisResult.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).Youssef Shoaib [MOD]
04/11/2021, 11:01 PMexpression.getType(bindingTrace.bindingContext)?.getJetTypeFqName()
Strum355
04/11/2021, 11:06 PMgetJetTypeFqName
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 methodStrum355
04/11/2021, 11:07 PMYoussef Shoaib [MOD]
04/11/2021, 11:12 PMval 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
onStrum355
04/11/2021, 11:13 PMDeclarationDescriptor
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 🙂 thanksYoussef Shoaib [MOD]
04/11/2021, 11:22 PMBindingUtils.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