All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.backend.jvm.intrinsics.UnsafeCoerce.kt Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.backend.jvm.intrinsics

import org.jetbrains.kotlin.backend.jvm.codegen.*
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.org.objectweb.asm.Type

/**
 * Implicit coercion between IrTypes with the same underlying representation.
 *
 * A call of the form `coerce(x)` allows us to coerce the value of `x` to type `A` but treat the result as if
 * it had IrType `B`. This is useful for inline classes, whose coercion behavior depends on the IrType in
 * addition to the underlying asmType.
 */
object UnsafeCoerce : IntrinsicMethod() {
    override fun invoke(expression: IrFunctionAccessExpression, codegen: ExpressionCodegen, data: BlockInfo): PromisedValue {
        val from = expression.getTypeArgument(0)!!
        val to = expression.getTypeArgument(1)!!
        val fromType = codegen.typeMapper.mapType(from)
        val toType = codegen.typeMapper.mapType(to)
        require(fromType == toType) {
            "Inline class types should have the same representation: $fromType != $toType"
        }
        val arg = expression.getValueArgument(0)!!
        val result = arg.accept(codegen, data)
        return object : PromisedValue(codegen, toType, to) {
            override fun materializeAt(target: Type, irTarget: IrType) {
                result.materializeAt(fromType, from)
                super.materializeAt(target, irTarget)
            }

            override fun discard() {
                result.discard()
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy