KT-36810 Implement javaPrimitiveType intrinsic in JVM_IR

This commit is contained in:
Dmitry Petrov
2020-03-17 15:56:39 +03:00
parent 2851fab281
commit ade18d144a
9 changed files with 169 additions and 1 deletions
@@ -0,0 +1,77 @@
/*
* 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.ir.expressions.IrClassReference
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
import org.jetbrains.kotlin.ir.expressions.IrGetClass
import org.jetbrains.kotlin.ir.types.*
import org.jetbrains.kotlin.ir.util.isTypeParameter
import org.jetbrains.kotlin.ir.util.render
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
import org.jetbrains.org.objectweb.asm.Type
object GetJavaPrimitiveType : IntrinsicMethod() {
override fun invoke(expression: IrFunctionAccessExpression, codegen: ExpressionCodegen, data: BlockInfo): PromisedValue? {
val receiver = expression.extensionReceiver ?: return null
val argumentType =
when (receiver) {
is IrGetClass -> receiver.argument.type
is IrClassReference -> receiver.classType
else -> return null
}
if (argumentType.isTypeParameter()) return null
val argumentAsmType = codegen.typeMapper.mapTypeAsDeclaration(argumentType)
val isPrimitiveTypeOrWrapper =
argumentType.isPrimitiveType() ||
argumentType.isNullablePrimitiveType() ||
!argumentType.isInlineClassType() && argumentAsmType.isVoidOrPrimitiveWrapper()
return when (receiver) {
is IrGetClass -> {
if (!isPrimitiveTypeOrWrapper) return null
val argumentValue = receiver.argument.accept(codegen, data)
argumentValue.materialize()
AsmUtil.pop(codegen.mv, argumentValue.type)
putPrimitiveType(codegen, argumentAsmType)
with(codegen) { expression.onStack }
}
is IrClassReference -> {
if (!isPrimitiveTypeOrWrapper) {
codegen.mv.aconst(null)
} else {
putPrimitiveType(codegen, argumentAsmType)
}
with(codegen) { expression.onStack }
}
else ->
throw AssertionError("IrGetClass or IrClassReference expected: ${receiver.render()}")
}
}
private fun putPrimitiveType(codegen: ExpressionCodegen, type: Type) {
codegen.mv.getstatic(AsmUtil.boxType(type).internalName, "TYPE", "Ljava/lang/Class;")
}
private fun IrType.isInlineClassType(): Boolean {
return (classOrNull ?: return false).owner.isInline
}
private fun Type.isVoidOrPrimitiveWrapper(): Boolean =
this == AsmTypes.VOID_WRAPPER_TYPE || AsmUtil.unboxPrimitiveTypeOrNull(this) != null
}
@@ -44,6 +44,7 @@ class IrIntrinsicMethods(val irBuiltIns: IrBuiltIns, val symbols: JvmSymbols) {
listOf(
Key(kotlinJvm, FqName("T"), "<get-javaClass>", emptyList()) to JavaClassProperty,
Key(kotlinJvm, KotlinBuiltIns.FQ_NAMES.kClass.toSafe(), "<get-javaObjectType>", emptyList()) to GetJavaObjectType,
Key(kotlinJvm, KotlinBuiltIns.FQ_NAMES.kClass.toSafe(), "<get-javaPrimitiveType>", emptyList()) to GetJavaPrimitiveType,
Key(
kotlinJvm,
KotlinBuiltIns.FQ_NAMES.kClass.toSafe(),