[Interop][Lowering] Support constants

Previously `const val`s from metadata-based interop libraries accidentally
were linked as IrField. It is fixed, and now we should properly lower these constants.
This commit is contained in:
Sergey Bogolepov
2020-02-25 17:28:28 +07:00
committed by Sergey Bogolepov
parent 6b0cdf7be6
commit 9f6d92d4d4
@@ -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) {