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