https://kotlinlang.org logo
p

pardom

05/06/2019, 4:59 AM
Hey, all. 👋 I’m writing a Kotlin compiler plugin and I’m trying to get the fully qualified name of a
KtCallExpression
. Anyone know how?
i

Ilmir Usmanov [JB]

05/06/2019, 5:03 AM
Copy code
KtExpression callee = expression.getCalleeExpression();
        Call call = bindingContext.get(CALL, callee);
        ResolvedCall<?> resolvedCall = bindingContext.get(RESOLVED_CALL, call);
        return resolvedCall.getResultingDescriptor().getContainingDeclaration().getFqName().child(resolvedCall.getResultingDescriptor().getName());
👀 1
p

pardom

05/06/2019, 5:06 AM
Trying it out now
I’m getting null back getting the
CALL
from callee.
It would actually be nicer to get the
KtFunction
for a
KtCallExpression
. Is that possible?
BindingContextUtils.getEnclosingFunctionDescriptor
🤔
i

Ilmir Usmanov [JB]

05/06/2019, 5:36 AM
Is call resolved? Maybe it would be easier to you to write a
CallChecker
and write necessary info to
BindingContext
, so, on later stages you can retrieve it. But without any clue about what you want to achieve I cannot tell what is better.
p

pardom

05/06/2019, 5:37 AM
I’ll give you some more context and sample code. One sec.
What I’m trying to do is create a pure function validator.
i

Ilmir Usmanov [JB]

05/06/2019, 5:40 AM
Yep. A
CallChecker
it is!
p

pardom

05/06/2019, 5:41 AM
Is that something I can use in this extension or is it another extension instance?
Looking through usages in jetbrains/kotlin right now
i

Ilmir Usmanov [JB]

05/06/2019, 5:48 AM
Or, you can override the other method,
analysisCompleted
, which is invoked after all analyses are, well, completed.
👍 1
p

pardom

05/06/2019, 5:49 AM
That worked!
Woohoo! I got the FQN back too.
Is it possible just to get the
KtFunction
instead?
i

Ilmir Usmanov [JB]

05/06/2019, 5:58 AM
KtFunction of the callee is not accessible, since it can come from another module or even be already compiled, or even java method. For containing function, invoke
getParent
until KtElement is KtFunction.
p

pardom

05/06/2019, 5:59 AM
Okay. I can still access the annotation entries though, right?
Right now my
isPure
function works on
KtFunction
, but I think it could be changed to work with a function descriptor.
i

Ilmir Usmanov [JB]

05/06/2019, 6:09 AM
Well, just curious, did you try annotation processor first? Other than that, almost all descriptors implement
Annotated
p

pardom

05/06/2019, 6:18 AM
As in apt/kapt? I want this to work with mpp so I skipped that.
i

Ilmir Usmanov [JB]

05/06/2019, 6:20 AM
Ah, I see.
p

pardom

05/06/2019, 6:22 AM
Anyway, it’s all working now. I’ll update the gist if you’re curious.
🎉 1
While I have your attention, is it possible to get the line/col from the element or descriptor for the error message?
I’m using
element.containingKtFile.virtualFilePath
for the path, but I haven’t found the line/col yet.
NVM. Just found this:
MessageUtil.psiElementToMessageLocation(element)
i

Ilmir Usmanov [JB]

05/06/2019, 6:39 AM
You can use
reportFromPlugin
for reporting. Like Parcelable plugin.
r

raulraja

05/06/2019, 8:08 PM
@pardom I'd love to give your plugin a shot or help out if it's open source. We were planning to do the same POC as a compiler plugin in Arrow.
Also interested into the actual mechanics to track purity. What kind of heuristic is this going to use to ensure effects are not performed when the function is invoked?. Tracking just invocations that return Unit is not enough since any call to a java lib or Kotlin fun can perform uncontrolled effects and still return a value.
p

pardom

05/06/2019, 9:06 PM
I just started toying with this over the weekend, so it’s not on Github yet. I’ll try to put it up this week (I’m at I/O).
Re: uncontrolled effects, that’s something I’m still thinking about. I’m thinking it might make sense to mark the known pure functions from the stdlib, but beyond that I’m not sure what’s appropriate. Should users be able to suppress an error if they know the function to be pure?
Same with immutable data structures.
Again, I just started toying with this a couple days ago and most of that time was figuring out the kotlin compiler plugin APIs 😕
r

raulraja

05/07/2019, 9:18 AM
Thanks so much Michael, I'll take a look