diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InteropLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InteropLowering.kt index 1b86086f9b1..29fa257f2b4 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InteropLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/InteropLowering.kt @@ -16,6 +16,7 @@ import org.jetbrains.kotlin.backend.common.push import org.jetbrains.kotlin.backend.konan.* import org.jetbrains.kotlin.backend.konan.cgen.* import org.jetbrains.kotlin.backend.konan.descriptors.allOverriddenFunctions +import org.jetbrains.kotlin.backend.konan.descriptors.isFromInteropLibrary import org.jetbrains.kotlin.backend.konan.descriptors.synthesizedName import org.jetbrains.kotlin.backend.konan.ir.* import org.jetbrains.kotlin.backend.konan.ir.companionObject @@ -881,6 +882,29 @@ private class InteropTransformer(val context: Context, override val irFile: IrFi return expression } + /** + * Handle `const val`s that come from interop libraries. + * + * We extract constant value from the backing field, and replace getter invocation with it. + */ + private fun tryGenerateInteropConstantRead(expression: IrCall): IrExpression? { + val function = expression.symbol.owner + + if (!function.descriptor.module.isFromInteropLibrary()) return null + if (!function.isGetter) return null + + val constantProperty = (function as? IrSimpleFunction) + ?.correspondingPropertySymbol + ?.owner + ?.takeIf { it.isConst } + ?: return null + + return constantProperty.backingField + ?.initializer + ?.expression + ?: error("Constant property ${constantProperty.name} has no initializer!") + } + override fun visitCall(expression: IrCall): IrExpression { expression.transformChildrenVoid(this) @@ -903,6 +927,8 @@ private class InteropTransformer(val context: Context, override val irFile: IrFi tryGenerateInteropMemberAccess(expression, symbols, builder)?.let { return it } + tryGenerateInteropConstantRead(expression)?.let { return it } + val intrinsicType = tryGetIntrinsicType(expression) if (intrinsicType != null) {