hi folks! I'm working on Kotlin/Python backend (<#...
# kontributors
p
hi folks! I'm working on Kotlin/Python backend (#python, #python-contributors) and adjusting lazy properties init and I have a question about writing lowerings. I'm struggling with the internal compiler API. For each IrFile, a variable 
properties_initialized_someFileName_kt
 is checked inside the init functions (we base our Python backend on the JS one, so this also happens in JS/IR backend). The problem in Python is that variables need to be initialized before they are read, so for each IrFile I need to produce a top-level assignment 
properties_initialized_someFileName_kt = False
. Something along these lines (forgive the imprecise arguments, the returned type is the real problem):
Copy code
JsIrBuilder.buildSetField(
    symbol = ...Name.identifier("properties initialized $fileName")...,
    receiver = null,
    value = JsIrBuilder.buildBoolean(context.irBuiltIns.booleanType, false)
)
I tried adapting existing lowerings to emit such IR entity, but I don't know how to put 
IrSetFieldImpl
 in the IR output because lowerings produce a list of 
IrDeclaration
 and the types just don't match. I sort of understand lowerings that transform some existing IR node to something else, but here I need to produce something new (not map from A to B). It would be cool if you could guide me how to write such lowering, and maybe also refer me to some docs about internal compiler API.
1
if you want to see a working example to reproduce the issue, here's a work-in-progress PR: https://github.com/krzema12/kotlin-python/pull/12
b
Can’t you init a property/field while declaring (using initializer)?
1
Alternatively, you can try to support and use
IrAnonymousInitializer
on file level.
1
p
I appreciate the high-level hints, but the problem is that I cannot translate them into lowering logic. Could you write an example (pseudo)code that does one of the above?
solved, thanks @Roman Artemev [JB]! the source of my confusion: it turned out the initialization expression is not taken in the factory functions, one has to mutate the field like this:
Copy code
initializedField.initializer = irFactory.createExpressionBody(JsIrBuilder.buildBoolean(context.irBuiltIns.booleanType, false))
I got this Python thing working 🎉
🦜 1
b
👍 great!