Hi, debugging a compiler extension plugin using IR...
# arrow-meta
j
Hi, debugging a compiler extension plugin using IR Api with an implementation of AnalysisHandlerExtension and IrGeneratorExtension, I see that IR Generation comes after analysis. Is there any extension point, that runs after generation, so one could analyse all effective sources ? So that any generated/transformed sources are again available for introspection/analysis. Is this possible in arrow-meta already ?
r
Hi @Jörg Winter, There are no extension points after generation AFAIK. Even during backend IR sources may no longer match the representation one to one and many nodes may no longer have a PSI element
If you can describe what kind of transformation or feature you are trying to implement I may be able to point you to the best way to do it. Also if you are planning to use FIR K2 vs Analysis the approach is different
j
Hi Raul, the thing I would like to implement is, at a minimum, collect all dependencies in a compilation unit/module (including the generated/transfomed parts!) . This could be done by collecting all PsiImportDeclaration for example. As for "FIR K2" I have to admit, that I don't know what it is and in what way this differs from "IR"-
wether this can be collected by only import declarations or if I have to analyse the full AST, is of secondary concern....
r
Current stable compiler is old legazy frontend + backend IR. There is a new frontend in development called FIR / k2 (Frontend IR) FIR is not stable but it's what modern compiler plugins should target. Not sure when it comes out stable maybe around 1.10. Collecting all dependencies is currently very slow with old frontend. You can recursively walk the ModuleDescriptor package fragments for this by implementing the
org.jetbrains.kotlin.resolve.extensions.AnalysisHandlerExtension
If you use the new FIR you can do similar with the symbolProvider and it's also slow as this may load properties in the tree that are lazy to compute it all. Alternatively depending on how you plan on using the output you can use a library like Classgraph which we have used for similar purposes in some plugins. Also slow. What we do is usually emit an index object in a package that declares a single list with all dependencies so later it's fast to read it https://github.com/arrow-kt/arrow-reflection/blob/main/arrow-reflect-compiler-plug[…]n/arrow/reflect/compiler/plugin/targets/ClasspathMetaScanner.kt The use the kotlin reflection api to get a KClass for any java class classgraph returns