JVM: optimize type mapping for primitive types
This commit is contained in:
@@ -16,16 +16,27 @@
|
||||
|
||||
package org.jetbrains.kotlin.codegen.signature
|
||||
|
||||
import org.jetbrains.kotlin.builtins.PrimitiveType
|
||||
import org.jetbrains.kotlin.codegen.AsmUtil
|
||||
import org.jetbrains.kotlin.load.kotlin.JvmTypeFactory
|
||||
import org.jetbrains.kotlin.resolve.jvm.AsmTypes
|
||||
import org.jetbrains.org.objectweb.asm.Type
|
||||
|
||||
object AsmTypeFactory : JvmTypeFactory<Type> {
|
||||
override fun boxType(possiblyPrimitiveType: Type) = AsmUtil.boxType(possiblyPrimitiveType)
|
||||
override fun createFromString(representation: String) = Type.getType(representation)
|
||||
override fun createObjectType(internalName: String) = Type.getObjectType(internalName)
|
||||
override fun toString(type: Type) = type.descriptor
|
||||
override fun boxType(possiblyPrimitiveType: Type): Type =
|
||||
AsmUtil.boxType(possiblyPrimitiveType)
|
||||
|
||||
override fun createFromString(representation: String): Type =
|
||||
Type.getType(representation)
|
||||
|
||||
override fun createPrimitiveType(primitiveType: PrimitiveType): Type =
|
||||
AsmTypes.valueTypeForPrimitive(primitiveType)
|
||||
|
||||
override fun createObjectType(internalName: String): Type =
|
||||
Type.getObjectType(internalName)
|
||||
|
||||
override fun toString(type: Type): String =
|
||||
type.descriptor
|
||||
|
||||
override val javaLangClassType: Type
|
||||
get() = AsmTypes.JAVA_CLASS_TYPE
|
||||
|
||||
+26
-1
@@ -6,6 +6,7 @@
|
||||
package org.jetbrains.kotlin.load.kotlin
|
||||
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.builtins.PrimitiveType
|
||||
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.load.java.BuiltinMethodsWithSpecialGenericSignature
|
||||
@@ -103,6 +104,17 @@ sealed class JvmType {
|
||||
class Array(val elementType: JvmType) : JvmType()
|
||||
|
||||
override fun toString() = JvmTypeFactoryImpl.toString(this)
|
||||
|
||||
companion object {
|
||||
internal val BOOLEAN = Primitive(JvmPrimitiveType.BOOLEAN)
|
||||
internal val CHAR = Primitive(JvmPrimitiveType.CHAR)
|
||||
internal val BYTE = Primitive(JvmPrimitiveType.BYTE)
|
||||
internal val SHORT = Primitive(JvmPrimitiveType.SHORT)
|
||||
internal val INT = Primitive(JvmPrimitiveType.INT)
|
||||
internal val FLOAT = Primitive(JvmPrimitiveType.FLOAT)
|
||||
internal val LONG = Primitive(JvmPrimitiveType.LONG)
|
||||
internal val DOUBLE = Primitive(JvmPrimitiveType.DOUBLE)
|
||||
}
|
||||
}
|
||||
|
||||
private object JvmTypeFactoryImpl : JvmTypeFactory<JvmType> {
|
||||
@@ -136,7 +148,20 @@ private object JvmTypeFactoryImpl : JvmTypeFactory<JvmType> {
|
||||
}
|
||||
}
|
||||
|
||||
override fun createObjectType(internalName: String) = JvmType.Object(internalName)
|
||||
override fun createPrimitiveType(primitiveType: PrimitiveType): JvmType =
|
||||
when (primitiveType) {
|
||||
PrimitiveType.BOOLEAN -> JvmType.BOOLEAN
|
||||
PrimitiveType.CHAR -> JvmType.CHAR
|
||||
PrimitiveType.BYTE -> JvmType.BYTE
|
||||
PrimitiveType.SHORT -> JvmType.SHORT
|
||||
PrimitiveType.INT -> JvmType.INT
|
||||
PrimitiveType.FLOAT -> JvmType.FLOAT
|
||||
PrimitiveType.LONG -> JvmType.LONG
|
||||
PrimitiveType.DOUBLE -> JvmType.DOUBLE
|
||||
}
|
||||
|
||||
override fun createObjectType(internalName: String): JvmType.Object =
|
||||
JvmType.Object(internalName)
|
||||
|
||||
override fun toString(type: JvmType): String =
|
||||
when (type) {
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
package org.jetbrains.kotlin.load.kotlin
|
||||
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import org.jetbrains.kotlin.builtins.PrimitiveType
|
||||
import org.jetbrains.kotlin.builtins.isSuspendFunctionType
|
||||
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
|
||||
import org.jetbrains.kotlin.builtins.transformSuspendFunctionToRuntimeFunctionType
|
||||
@@ -25,6 +26,7 @@ import org.jetbrains.kotlin.utils.DO_NOTHING_3
|
||||
interface JvmTypeFactory<T : Any> {
|
||||
fun boxType(possiblyPrimitiveType: T): T
|
||||
fun createFromString(representation: String): T
|
||||
fun createPrimitiveType(primitiveType: PrimitiveType): T
|
||||
fun createObjectType(internalName: String): T
|
||||
fun toString(type: T): String
|
||||
|
||||
@@ -40,6 +42,7 @@ interface TypeMappingConfiguration<out T : Any> {
|
||||
fun getPredefinedInternalNameForClass(classDescriptor: ClassDescriptor): String?
|
||||
fun getPredefinedFullInternalNameForClass(classDescriptor: ClassDescriptor): String? = null
|
||||
fun processErrorType(kotlinType: KotlinType, descriptor: ClassDescriptor)
|
||||
|
||||
// returns null when type doesn't need to be preprocessed
|
||||
fun preprocessType(kotlinType: KotlinType): KotlinType? = null
|
||||
|
||||
@@ -77,8 +80,8 @@ fun <T : Any> mapType(
|
||||
|
||||
val constructor = kotlinType.constructor
|
||||
if (constructor is IntersectionTypeConstructor) {
|
||||
val intersectionType = constructor.getAlternativeType() ?:
|
||||
typeMappingConfiguration.commonSupertype(constructor.supertypes)
|
||||
val intersectionType = constructor.getAlternativeType()
|
||||
?: typeMappingConfiguration.commonSupertype(constructor.supertypes)
|
||||
// interface In<in E>
|
||||
// open class A : In<A>
|
||||
// open class B : In<B>
|
||||
@@ -199,7 +202,7 @@ fun <T : Any> TypeSystemCommonBackendContext.mapBuiltInType(
|
||||
|
||||
val primitiveType = constructor.getPrimitiveType()
|
||||
if (primitiveType != null) {
|
||||
val jvmType = typeFactory.createFromString(JvmPrimitiveType.get(primitiveType).desc)
|
||||
val jvmType = typeFactory.createPrimitiveType(primitiveType)
|
||||
val isNullableInJava = type.isNullableType() || hasEnhancedNullability(type)
|
||||
return typeFactory.boxTypeIfNeeded(jvmType, isNullableInJava)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user