https://kotlinlang.org logo
#compiler
Title
# compiler
t

Tracey Yoshima

10/24/2023, 10:34 PM
👋, I’m using the
convertToIrAndActualizeForJvm
function to convert the
FIR
to the
IR
, and for code like
val a = 1 + 1,
the IR contains changes that match the resultant byte code: I.E.
val a = 2
. Is it possible to configure the compiler to preserve
1 + 1
in the IR or is that only preserved in the FIR via metadata source?
Alternatively, I’m using the PSI-backed FIR configuration — after the IR is actualized and I have access to the symbol table and cached types, can it be accessed from an FIR element? I.E. Given a FirConstExpression, may I get the class symbol or IrSimpleType associated with the FIR element?
I think I found it, but I’d like an expert option: In the
Fir2IrActualizedResult
the
Fir2IrComponentStorage$typeConverter
contains a class
Fir2IrTypeConverter
, which I may use to retrieve the
IrType
. Is that the recommended approach?
d

dmitriy.novozhilov

10/25/2023, 8:28 AM
Are you asking about accessing all those things from API for compiler plugins or you develop some external tool which calls compiler internals on its own?
t

Tracey Yoshima

10/25/2023, 12:11 PM
Hi Dmitriy, it’s for a new revision of a Kotlin parser in OpenRewrite, an external tool that calls the compiler on it’s own. The goal is a tree like the psi with type attribution to enable automatic code remediation.
d

dmitriy.novozhilov

10/25/2023, 2:02 PM
In this case you can create IR with
Fir2IrConverter.createIrModuleFragment
and access all fir2ir services from
Fir2IrResult.components
They knows everything about fir <--> ir mapping
But I must warn you that fir2ir related API is very unstable now, so be ready to incompatibilities with each Kotlin update
t

Tracey Yoshima

10/25/2023, 3:35 PM
Thank you!
Hi Dmitriy,
createIrModuleFragment
and components were very helpful! I could associate most of the FIR elements to the IR to acquire type attribution through the caches and
typeRef.toIrType(Fir2IrTypeConverter)
. I didn’t find a way to link some of the elements: •
FirValueParameter
=>
IrValueParameter
FirCall
=>
IrCall
(FunctionCall or ConstructorCall) •
FirResolvedQualifier
=> IrElement I can access the base type through the symbol, but I’d like to preserve the bounded type attribution. I.E. listOf(“foo”) =>
kotlin.collections.List<kotlin.String>
instead of
kotlin.collections.List<T>
. Is there a way to use a
callableId
or a utility to get the IR from a
FirCall
,
FirValueParameter
, or
FirResolvedQualifier
?
d

dmitriy.novozhilov

10/26/2023, 6:16 AM
Check declaration storage from components
Also there is no mapping for expressions, we just covert them