:wave: I’m attempting to use the FirTotalResolvePr...
# compiler
t
👋 I’m attempting to use the FirTotalResolveProcessor to process FirFiles in hopes of adding type attribution. The FirFile is in the
RAW_FILE
phase when I call
#process(List<FirFile)
and does not change even though the
transform
runs. I’d like to know what I am doing wrong since I am still learning about the Kotlin compiler. Questions: • Is there a recommended example of what I may look at to see how to progress a FirFile through the compilation phases? • Would it be possible to schedule a zoom to provide context about how we’re using the Kotlin compiler to construct a type-aware, whitespace-preserving AST? • Or could anyone take a look at what I have so far?
WIP compiler configuration so far.
d
There is no need to run resolve processors by hand. After creating session and FIR files you can just run
runResolution
function on
session
(like here)
But actually
runResolution
does exact same thing as you do (run total resolve processor)
If you are confused that
resolvePhase
has not changed, that it's ok. This field can be changed only in lazy mode in IDE. Regular CLI compiler does not take care about this phase and does not update it
t
Ah, good to know — I noticed
runResolution
, but wasn’t sure if I should use it. I’ve replaced it locally; thank you so much! • Would you happen to have any recommended examples related to session configuration? • In the Java compiler, each type contains a symbol that provides information like superclasses, fields, methods, etc. I’d like to use that information to connect to AST elements. Is or where is that represented in Kotlin? ◦ I’m not currently seeing anything like that, and I am under the impression that it exists, but it is missing due to a lack of configurations. Is my understanding correct? Example of `java.io.Serializable`:
Alternatively, is what I am looking for a part of Fir2Ir?
d
Would you happen to have any recommended examples related to session configuration?
FirFrontendFacade (from tests) or FirKotlinToJvmBytecodeCompiler (from CLI compiler)
I’d like to use that information to connect to AST elements
I'm not fully understand what do you want. You mean that you are trying to create mapping between FIR elements and PSI nodes (which represent C(oncrete)ST from parser)? Or smth else?
If you want to see the source of some FIR element, just check
FirElement.source
t
FirKotlinToJvmBytecodeCompiler (from CLI compiler)
This is precisely what I am looking for; thank you!
I’m not fully understand what do you want. You mean that you are trying to create mapping between FIR elements and PSI nodes (which represent C(oncrete)ST from parser)? Or smth else?
I may find what I need using the
KotlinToJVMBytecodeCompiler
to correctly configure the FirSession. (I’ve skipped a few steps.) The missing configuration may be causing the issue that I am experiencing. So, I’ll try that first — Thanks again!
@dmitriy.novozhilov, the FIR is converted successfully to an IR. Thanks again! Questions for Monday: • Could you provide a TLDR on the backend part of the compilation process or point me to documentation? Meaning what happens to the IR and how is the IR used to generate byte code? • Is there a utility that links the IR information, like a
List,
to its methods, fields, super types, etc.?
d
Could you provide a TLDR on the backend part of the compilation process or point me to documentation? Meaning what happens to the IR and how is the IR used to generate byte code?
TLDR: backend consist with two parts: lowerings and codegen Lowering is a pass which somehow transfroms the IR tree to something more similar to real code in bytecode/js/llvm. Examples of lowerings: • transformation of
for (x in 1..n)
loop into
for (int x = 1; x <= n; x++)
• transformation of extension receivers into parameters
Copy code
// before
fun String.foo(x: Int) {}

s.foo(10)
// after
fun foo(reciever: String, x: Int)

foo(s, 10)
• inlining of inline functions • transformation of
suspend
functions into state machine with continuation • etc (AFAIR there are several tens of lowering) Codegen is the part which takes lowered IR at input and produces bytecode/js/llvm bitcode from it For more information/articles/talks it's better to ask someone else in this channel. All materials I have are in Russian
Is there a utility that links the IR information, like a List, to its methods, fields, super types, etc.?
Could you elaborate question, please? All IR declarations contain all it's content right inside the tree
BTW I remembered that we have a detailed doc about coroutines codegen. It may be interesting for you
t
@dmitriy.novozhilov, thank you for the TLDR and documentation; I appreciate it.
Could you elaborate question, please?
All IR declarations contain all it’s content right inside the tree
I misunderstood the output when I was debugging — it looks like I have access to everything I need. Thank you for all your help
d
There are two steps for properties: 1. All property accesses are replaced with calls of getters/setters 2. If property has an extension receiver then it moved to parameters of getter/setter as it does for functions
m
For more information/articles/talks it's better to ask someone else in this channel. All materials I have are in Russian.
@dmitriy.novozhilov I would like to take a look at these russian materials. Are they public?
d
There were two great talks from my teammate https://kotlinlang.slack.com/archives/C7L3JB43G/p1615463600012400?thread_ts=1615394755.006000&amp;cid=C7L3JB43G Also I had a talk about compiler plugins in K2 compiler this summer on Podlodka platform, but it seems it's not public (but maybe you are subscriber)
Also there are a lot of interesting discussions in #compiler channel, you can check the history
m
That's right, but I recently searched for informations about old frontend ( FE 1.0 ) transformations. I have mostly worked with FIR and now I try to do the same stuff ( generating top-level method stubs and changing return types of existing functions ) in the old frontend to get IntelliJ support as long there's no Kotlin Plugin supporting FIR. I know a preview version can be built from the IntelliJ repo but I think it's a good idea to support the old frontend anyway.
d
Unfortunately, I don't aware about any materials about FE 1.0 (but I never searched for them) You can ask about it in #compiler, community may know more than me about this topic
m
I will try.