JVM_IR: Fix field initialization in secondary enum constructors.
This commit is contained in:
committed by
Alexander Udalov
parent
8cd6dc0cd9
commit
467e6e3d97
+30
@@ -68,6 +68,7 @@ private class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringP
|
||||
private lateinit var valueOfFunction: IrFunction
|
||||
|
||||
fun run() {
|
||||
insertInstanceInitializer()
|
||||
assignOrdinalsToEnumEntries()
|
||||
lowerEnumConstructors(irClass)
|
||||
lowerEnumEntries()
|
||||
@@ -75,6 +76,35 @@ private class EnumClassLowering(val context: JvmBackendContext) : ClassLoweringP
|
||||
lowerEnumClassBody()
|
||||
}
|
||||
|
||||
// Make sure that an instance initializer call exists, so that field initialization code will be generated.
|
||||
// TODO: This function is identical to the one used in the JS lowerings. Share them.
|
||||
private fun insertInstanceInitializer() {
|
||||
irClass.transformChildrenVoid(object : IrElementTransformerVoid() {
|
||||
override fun visitClass(declaration: IrClass) = declaration
|
||||
|
||||
override fun visitConstructor(declaration: IrConstructor): IrStatement {
|
||||
declaration.transformChildrenVoid(this)
|
||||
|
||||
val blockBody = declaration.body as IrBlockBody
|
||||
|
||||
if (!blockBody.statements.any { it is IrInstanceInitializerCall }) {
|
||||
blockBody.statements.transformFlat {
|
||||
if (it is IrEnumConstructorCall)
|
||||
listOf(
|
||||
it, IrInstanceInitializerCallImpl(
|
||||
declaration.startOffset, declaration.startOffset,
|
||||
irClass.symbol, context.irBuiltIns.unitType
|
||||
)
|
||||
)
|
||||
else null
|
||||
}
|
||||
}
|
||||
|
||||
return declaration
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private fun assignOrdinalsToEnumEntries() {
|
||||
var ordinal = 0
|
||||
irClass.declarations.forEach {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// IGNORE_BACKEND_FIR: JVM_IR
|
||||
// IGNORE_BACKEND: JVM_IR
|
||||
|
||||
enum class A1(val prop1: String) {
|
||||
X("asd"),
|
||||
Y() {
|
||||
|
||||
Reference in New Issue
Block a user