Hi everyone, I have a question and not sure if it...
# compiler
a
Hi everyone, I have a question and not sure if it is a best channel to ask so please let me know if I should ask somewhere else. I also left a question on SO but seems like it is not a very popular topic there. Prerequisites: I’m using
"org.jetbrains.kotlin:kotlin-compiler-embeddable:1.5.32"
, the project targets JVM only(android), and I create a simple gradle plugin to analyze some kotlin source files. I create a
KotlinCoreEnvironment
and pass there all the paths to analyze. I add them to the config like:
Copy code
configuration.addKotlinSourceRoots(kotlinFiles)
configuration.addJvmClasspathRoots(classpath)
Then I create the binding context like:
Copy code
analyzer.analyzeAndReport(files) {
    TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(
        environment.project,
        files,
        NoScopeRecordCliBindingTrace(),
        environment.configuration,
        environment::createPackagePartProvider,
        ::FileBasedDeclarationProviderFactory
    )
}

return analyzer.analysisResult.bindingContext // I use this bindingContext
I have the binding context and start analyzing the file. The goal is to get some types and classes for methods from the expressions like:
Copy code
Foo()
  .bar() // returns Bar object
  .baz() // returns Baz object
where
Foo
,
Bar
and
Baz
are 3 different classes in different kt files.I want to understand all the types by parsing such piece of code but not able to. I searched this channel for
BindingContext
,
BindingTrace
and other keywords but nothing worked for me. I tried calling
getResolvedCall
and
getCall
on above expressions, everything returns null, same for trying to get
Type
or anything that I think may help me to resolve the target file that contains the functions from the code snippet above. Also, played with
BindingUtils
with no luck. I’m just learning how compiler works but the information is relatively limited so I mostly use some existing projects to understand the internals. Please let me know how if what I’m indenting to do is achievable. Thanks in advance
I figured out the problem, I was visiting the file that wasn’t analyzed, that’s why my bindingContext was returning null for everything even though the context itself was not empty.
s
Do you need a separate invocation of compiler? The same can be achieved with compiler plugin and AnalysisHandlerExtension or KSP with better documented apis
a
hey, this analysis should only be performed as a part of a gradle task on our CI, I wonder if compiler plugin fits well for such a use-case? On another note, can you please share the
AnalysisHandlerExtension
docs/examples? So far I’ve seen in in arrow-kt I guess
s
Maybe KSP is more correct for your use case, it has a plethora of examples and much better documentation
a
are there any kotlin version limitations for KSP? I’m bound to 1.5.31 atm
s
I think yes, they are targeting the latest version for latest features, but I am pretty sure 1.5.31 should have a matching ksp version available
a
got it, will take a look, thank you
s
For analysishandler, I don't have any examples on hand unfortunately, but it gives you two hooks - analysis start and analysis completed When analysis is completed, you receive the same binding context as in your example, with the benefit that compilation is already configured for you
👍 1
a
I see that KSP is built on top of PSI, is is a lightweight api to get AST/type info w/o doing the full compilation?
s
I don't have know much about KSP internals, probably it uses similar apis to what ide does, but not exactly sure
a
see this in KSP docs under the limitations
The following are not goals of KSP:
• Examining expression-level information of source code.
I need to analyze KtExpression so probably not gonna work for me
s
Ah, maybe you're right
You can try analysis handler in that case and just abort compilation in your plugin, similar to what e.g. this plugin does: https://github.com/JetBrains/kotlin/tree/master/plugins/jvm-abi-gen
a
will take a look, currently, I haver very basic understanding of the kotlin compiler pipeline so need to spend some time
but thanks