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

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

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright 2010-2020 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.codegen.AsmUtil
import org.jetbrains.kotlin.codegen.inline.ReifiedTypeInliner
import org.jetbrains.kotlin.ir.expressions.IrClassReference
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
import org.jetbrains.kotlin.ir.expressions.IrGetClass
import org.jetbrains.kotlin.ir.symbols.IrTypeParameterSymbol
import org.jetbrains.kotlin.ir.util.dump
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
import org.jetbrains.org.objectweb.asm.Type

object GetJavaObjectType : IntrinsicMethod() {

    override fun invoke(expression: IrFunctionAccessExpression, codegen: ExpressionCodegen, data: BlockInfo): PromisedValue? =
        when (val receiver = expression.extensionReceiver) {
            is IrClassReference -> {
                val symbol = receiver.symbol
                if (symbol is IrTypeParameterSymbol) {
                    assert(symbol.owner.isReified) {
                        "Non-reified type parameter under ::class should be rejected by type checker: ${symbol.owner.dump()}"
                    }
                    codegen.putReifiedOperationMarkerIfTypeIsReifiedParameter(
                        receiver.classType,
                        ReifiedTypeInliner.OperationKind.JAVA_CLASS
                    )
                }
                codegen.mv.aconst(AsmUtil.boxType(codegen.typeMapper.mapTypeAsDeclaration(receiver.classType)))

                with(codegen) { expression.onStack }
            }

            is IrGetClass -> {
                val argumentValue = receiver.argument.accept(codegen, data)
                argumentValue.materialize()
                val argumentType = argumentValue.type
                when {
                    argumentType == Type.VOID_TYPE ->
                        codegen.mv.aconst(AsmTypes.UNIT_TYPE)

                    AsmUtil.isPrimitive(argumentType) ||
                            AsmUtil.unboxPrimitiveTypeOrNull(argumentType) != null -> {
                        AsmUtil.pop(codegen.mv, argumentType)
                        codegen.mv.aconst(AsmUtil.boxType(argumentType))
                    }

                    else ->
                        codegen.mv.invokevirtual("java/lang/Object", "getClass", "()Ljava/lang/Class;", false)
                }

                with(codegen) { expression.onStack }
            }

            else ->
                null
        }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy