I'm having trouble using a cinteropt enum in my Ko...
# kotlin-native
v
I'm having trouble using a cinteropt enum in my Kotlin code. Given the following C header:
Copy code
typedef enum
{
    BCM2835_GPIO_FSEL_INPT  = 0x00,
...
} bcm2835FunctionSelect;
I have added this as a strictEnum to `libbcm.def`:
Copy code
strictEnums = bcm2835FunctionSelect
Then when I try to use it in Kotlin code, there are two issues. Firstly, I have to use it rather verbosely:
Copy code
bcm2835_gpio_fsel(pin, bcm2835FunctionSelect.BCM2835_GPIO_FSEL_INPT.value.toUByte());
And secondly it doesn't compile, with the following error:
Copy code
e: kotlin.UninitializedPropertyAccessException: Parent not initialized: org.jetbrains.kotlin.ir.declarations.impl.IrEnumEntryImpl@31d85176
        at org.jetbrains.kotlin.ir.declarations.impl.IrDeclarationBase.getParent(IrDeclarationBase.kt:39)
        at org.jetbrains.kotlin.ir.util.IrUtilsKt.getParentAsClass(IrUtils.kt:286)
        at org.jetbrains.kotlin.backend.konan.ir.interop.cenum.CEnumCompanionGenerator.generateAliasGetterBody(CEnumCompanionGenerator.kt:84)
It looks like I've found a bug, but maybe I'm doing something wrong?
If I remove
strictEnums
from libbcm.def then
bcm2835FunctionSelect.BCM2835_GPIO_FSEL_INPT
isn't found at all.
d
interesting - does native have access to the legacy compiler?
I see ir in the namespace of stack and that is a newer compiler
n
By default the Kotlin Native compiler uses the old Kotlin compiler system.
d
But the namespace has ir in it?
m
K/N compiler was IR-based from the very beginning.
v
I'm afraid all of that's above my head... Wrong compiler?
The project I have in mind, for the raspberry Pi, has a lot of enums defined in a C header file. I suppose I could start moving them all to Kotlin enum classes?
s
It might be a compiler bug. What version of Kotlin do you use?
If I remove 
strictEnums
 from libbcm.def then 
bcm2835FunctionSelect.BCM2835_GPIO_FSEL_INPT
 isn't found at all.
It is generated as constant
const val BCM2835_GPIO_FSEL_INPT: bcm2835FunctionSelect
.
v
This is a new project so 1.4.10
s
Yeah, it's a bug indeed. As a workaround, you can use
nonStrictEnums
property in def file to force
const val
generation.
Created an issue (https://youtrack.jetbrains.com/issue/KT-43265) for the bug.