Hello, what is the difference between IrBlock vs I...
# compiler
m
Hello, what is the difference between IrBlock vs IrBlockBody vs IrExpressionBody vs IrReturnableBlock vs IrComposite? They all seem very similar. Some of them are rarely used. Also, how does this relate to https://youtrack.jetbrains.com/issue/KT-53992/Deprecate-IrExpressionBody?
i
These types of
IrElements
are all used to represent different code. I will try to explain the hierarchy. 1.
IrBody
. Used to represent function bodies. It has 3 implementations •
IrBlockBody
represents function body inside curly braces. •
IrExpressionBody
represents function body with single expression after
=
sign (see Single-expression functions). •
IrSyntheticBody
represents a body that wasn’t presented in source code. I don’t know much about this type but as I can see it is used only for enums. 2.
IrBlock
. Represents some block of code inside function. Has 3 implementations: •
IrBlockImpl
. Basic implementation that is used to represent all blocks of the form
if (true) { /* this is IrBlockImpl */ }
. •
IrReturnableBlock
. This is block of code that can have inner return in it. After
return
was executed we appear at the end of this block. Don’t confuse it with
IrBlockBody
. It is synthetic block, that can appear after some code transformations. I remember two cases when we create such block type: the first one after inlining to represent early returns and the second one in some try-catch cases. •
IrInlinedFunctionBlock
. This one is used to mark a code that was inlined. We store there some additional info to remember that was before inlining. 3.
IrComposite
. Mostly the same as
IrBlock
. Difference between these two elements is that after exiting
IrBlock
we “forget” about all declared entities inside that block. This is not the case for
IrComposite
. This is needed, for example, to store variables after transforming destructing declaration.
💜 1
j
Hi @Ivan Kylchik. Thank you so much for your kind and detailed explanation! You've been very helpful.
kodee happy 2