Move common part of typeSignatureMapping.kt to :core:compiler.common.jvm
This commit is contained in:
@@ -52,6 +52,7 @@ import org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor;
|
|||||||
import org.jetbrains.kotlin.diagnostics.Errors;
|
import org.jetbrains.kotlin.diagnostics.Errors;
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens;
|
import org.jetbrains.kotlin.lexer.KtTokens;
|
||||||
import org.jetbrains.kotlin.load.java.DescriptorsJvmAbiUtil;
|
import org.jetbrains.kotlin.load.java.DescriptorsJvmAbiUtil;
|
||||||
|
import org.jetbrains.kotlin.load.kotlin.DescriptorBasedTypeSignatureMappingKt;
|
||||||
import org.jetbrains.kotlin.load.kotlin.MethodSignatureMappingKt;
|
import org.jetbrains.kotlin.load.kotlin.MethodSignatureMappingKt;
|
||||||
import org.jetbrains.kotlin.load.kotlin.TypeSignatureMappingKt;
|
import org.jetbrains.kotlin.load.kotlin.TypeSignatureMappingKt;
|
||||||
import org.jetbrains.kotlin.name.Name;
|
import org.jetbrains.kotlin.name.Name;
|
||||||
@@ -1813,7 +1814,7 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
|
|||||||
|
|
||||||
FunctionDescriptor originalSuspendLambdaDescriptor = getOriginalSuspendLambdaDescriptorFromContext(context);
|
FunctionDescriptor originalSuspendLambdaDescriptor = getOriginalSuspendLambdaDescriptorFromContext(context);
|
||||||
boolean isVoidCoroutineLambda =
|
boolean isVoidCoroutineLambda =
|
||||||
originalSuspendLambdaDescriptor != null && TypeSignatureMappingKt.hasVoidReturnType(originalSuspendLambdaDescriptor);
|
originalSuspendLambdaDescriptor != null && DescriptorBasedTypeSignatureMappingKt.hasVoidReturnType(originalSuspendLambdaDescriptor);
|
||||||
|
|
||||||
KotlinType originalReturnTypeOfSuspendFunctionReturningUnboxedInlineClass =
|
KotlinType originalReturnTypeOfSuspendFunctionReturningUnboxedInlineClass =
|
||||||
CoroutineCodegenUtilKt.originalReturnTypeOfSuspendFunctionReturningUnboxedInlineClass(
|
CoroutineCodegenUtilKt.originalReturnTypeOfSuspendFunctionReturningUnboxedInlineClass(
|
||||||
|
|||||||
+7
@@ -5,6 +5,10 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.load.java.typeEnhancement
|
package org.jetbrains.kotlin.load.java.typeEnhancement
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
|
||||||
|
import org.jetbrains.kotlin.types.TypeSystemCommonBackendContext
|
||||||
|
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
|
||||||
|
|
||||||
fun createJavaTypeQualifiers(
|
fun createJavaTypeQualifiers(
|
||||||
nullability: NullabilityQualifier?,
|
nullability: NullabilityQualifier?,
|
||||||
mutability: MutabilityQualifier?,
|
mutability: MutabilityQualifier?,
|
||||||
@@ -36,3 +40,6 @@ fun Set<NullabilityQualifier>.select(own: NullabilityQualifier?, isCovariant: Bo
|
|||||||
NullabilityQualifier.FORCE_FLEXIBILITY
|
NullabilityQualifier.FORCE_FLEXIBILITY
|
||||||
else
|
else
|
||||||
select(NullabilityQualifier.NOT_NULL, NullabilityQualifier.NULLABLE, own, isCovariant)
|
select(NullabilityQualifier.NOT_NULL, NullabilityQualifier.NULLABLE, own, isCovariant)
|
||||||
|
|
||||||
|
fun TypeSystemCommonBackendContext.hasEnhancedNullability(type: KotlinTypeMarker): Boolean =
|
||||||
|
type.hasAnnotation(JvmAnnotationNames.ENHANCED_NULLABILITY_ANNOTATION)
|
||||||
|
|||||||
+102
@@ -0,0 +1,102 @@
|
|||||||
|
/*
|
||||||
|
* 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.load.kotlin
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.builtins.PrimitiveType
|
||||||
|
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
|
||||||
|
import org.jetbrains.kotlin.load.java.typeEnhancement.hasEnhancedNullability
|
||||||
|
import org.jetbrains.kotlin.name.Name
|
||||||
|
import org.jetbrains.kotlin.resolve.jvm.JvmClassName
|
||||||
|
import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType
|
||||||
|
import org.jetbrains.kotlin.types.TypeSystemCommonBackendContext
|
||||||
|
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
val javaLangClassType: T
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T : Any> JvmTypeFactory<T>.boxTypeIfNeeded(possiblyPrimitiveType: T, needBoxedType: Boolean): T =
|
||||||
|
if (needBoxedType) boxType(possiblyPrimitiveType) else possiblyPrimitiveType
|
||||||
|
|
||||||
|
const val NON_EXISTENT_CLASS_NAME = "error/NonExistentClass"
|
||||||
|
|
||||||
|
fun <T : Any> TypeSystemCommonBackendContext.mapBuiltInType(
|
||||||
|
type: KotlinTypeMarker,
|
||||||
|
typeFactory: JvmTypeFactory<T>,
|
||||||
|
mode: TypeMappingMode
|
||||||
|
): T? {
|
||||||
|
val constructor = type.typeConstructor()
|
||||||
|
if (!constructor.isClassTypeConstructor()) return null
|
||||||
|
|
||||||
|
val primitiveType = constructor.getPrimitiveType()
|
||||||
|
if (primitiveType != null) {
|
||||||
|
val jvmType = typeFactory.createPrimitiveType(primitiveType)
|
||||||
|
val isNullableInJava = type.isNullableType() || hasEnhancedNullability(type)
|
||||||
|
return typeFactory.boxTypeIfNeeded(jvmType, isNullableInJava)
|
||||||
|
}
|
||||||
|
|
||||||
|
val arrayElementType = constructor.getPrimitiveArrayType()
|
||||||
|
if (arrayElementType != null) {
|
||||||
|
return typeFactory.createFromString("[" + JvmPrimitiveType.get(arrayElementType).desc)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (constructor.isUnderKotlinPackage()) {
|
||||||
|
val classId = constructor.getClassFqNameUnsafe()?.let(JavaToKotlinClassMap::mapKotlinToJava)
|
||||||
|
if (classId != null) {
|
||||||
|
if (!mode.kotlinCollectionsToJavaCollections && JavaToKotlinClassMap.mutabilityMappings.any { it.javaClass == classId })
|
||||||
|
return null
|
||||||
|
|
||||||
|
return typeFactory.createObjectType(JvmClassName.byClassId(classId).internalName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
|
||||||
|
open class JvmDescriptorTypeWriter<T : Any>(private val jvmTypeFactory: JvmTypeFactory<T>) {
|
||||||
|
private var jvmCurrentTypeArrayLevel: Int = 0
|
||||||
|
protected var jvmCurrentType: T? = null
|
||||||
|
private set
|
||||||
|
|
||||||
|
protected fun clearCurrentType() {
|
||||||
|
jvmCurrentType = null
|
||||||
|
jvmCurrentTypeArrayLevel = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
open fun writeArrayType() {
|
||||||
|
if (jvmCurrentType == null) {
|
||||||
|
++jvmCurrentTypeArrayLevel
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
open fun writeArrayEnd() {
|
||||||
|
}
|
||||||
|
|
||||||
|
open fun writeClass(objectType: T) {
|
||||||
|
writeJvmTypeAsIs(objectType)
|
||||||
|
}
|
||||||
|
|
||||||
|
protected fun writeJvmTypeAsIs(type: T) {
|
||||||
|
if (jvmCurrentType == null) {
|
||||||
|
jvmCurrentType =
|
||||||
|
if (jvmCurrentTypeArrayLevel > 0) {
|
||||||
|
jvmTypeFactory.createFromString("[".repeat(jvmCurrentTypeArrayLevel) + jvmTypeFactory.toString(type))
|
||||||
|
} else {
|
||||||
|
type
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
open fun writeTypeVariable(name: Name, type: T) {
|
||||||
|
writeJvmTypeAsIs(type)
|
||||||
|
}
|
||||||
|
}
|
||||||
-4
@@ -35,7 +35,6 @@ import org.jetbrains.kotlin.name.Name
|
|||||||
import org.jetbrains.kotlin.resolve.constants.ConstantValue
|
import org.jetbrains.kotlin.resolve.constants.ConstantValue
|
||||||
import org.jetbrains.kotlin.types.*
|
import org.jetbrains.kotlin.types.*
|
||||||
import org.jetbrains.kotlin.types.checker.SimpleClassicTypeSystemContext
|
import org.jetbrains.kotlin.types.checker.SimpleClassicTypeSystemContext
|
||||||
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
|
|
||||||
import org.jetbrains.kotlin.types.refinement.TypeRefinement
|
import org.jetbrains.kotlin.types.refinement.TypeRefinement
|
||||||
import org.jetbrains.kotlin.types.typeUtil.createProjection
|
import org.jetbrains.kotlin.types.typeUtil.createProjection
|
||||||
import org.jetbrains.kotlin.types.typeUtil.isTypeParameter
|
import org.jetbrains.kotlin.types.typeUtil.isTypeParameter
|
||||||
@@ -43,9 +42,6 @@ import org.jetbrains.kotlin.types.typeUtil.isTypeParameter
|
|||||||
fun KotlinType.hasEnhancedNullability(): Boolean =
|
fun KotlinType.hasEnhancedNullability(): Boolean =
|
||||||
SimpleClassicTypeSystemContext.hasEnhancedNullability(this)
|
SimpleClassicTypeSystemContext.hasEnhancedNullability(this)
|
||||||
|
|
||||||
fun TypeSystemCommonBackendContext.hasEnhancedNullability(type: KotlinTypeMarker): Boolean =
|
|
||||||
type.hasAnnotation(JvmAnnotationNames.ENHANCED_NULLABILITY_ANNOTATION)
|
|
||||||
|
|
||||||
class JavaTypeEnhancement(private val javaResolverSettings: JavaResolverSettings) {
|
class JavaTypeEnhancement(private val javaResolverSettings: JavaResolverSettings) {
|
||||||
|
|
||||||
private open class Result(open val type: KotlinType, val subtreeSize: Int, val wereChanges: Boolean) {
|
private open class Result(open val type: KotlinType, val subtreeSize: Int, val wereChanges: Boolean) {
|
||||||
|
|||||||
+1
-96
@@ -1,41 +1,21 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
* 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.
|
* 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.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.transformSuspendFunctionToRuntimeFunctionType
|
import org.jetbrains.kotlin.builtins.transformSuspendFunctionToRuntimeFunctionType
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
import org.jetbrains.kotlin.load.java.typeEnhancement.hasEnhancedNullability
|
|
||||||
import org.jetbrains.kotlin.name.Name
|
|
||||||
import org.jetbrains.kotlin.name.SpecialNames
|
import org.jetbrains.kotlin.name.SpecialNames
|
||||||
import org.jetbrains.kotlin.resolve.jvm.JvmClassName
|
|
||||||
import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType
|
|
||||||
import org.jetbrains.kotlin.types.*
|
import org.jetbrains.kotlin.types.*
|
||||||
import org.jetbrains.kotlin.types.checker.SimpleClassicTypeSystemContext
|
import org.jetbrains.kotlin.types.checker.SimpleClassicTypeSystemContext
|
||||||
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
|
|
||||||
import org.jetbrains.kotlin.types.typeUtil.replaceArgumentsWithStarProjections
|
import org.jetbrains.kotlin.types.typeUtil.replaceArgumentsWithStarProjections
|
||||||
import org.jetbrains.kotlin.types.typeUtil.representativeUpperBound
|
import org.jetbrains.kotlin.types.typeUtil.representativeUpperBound
|
||||||
import org.jetbrains.kotlin.utils.DO_NOTHING_3
|
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
|
|
||||||
|
|
||||||
val javaLangClassType: T
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun <T : Any> JvmTypeFactory<T>.boxTypeIfNeeded(possiblyPrimitiveType: T, needBoxedType: Boolean) =
|
|
||||||
if (needBoxedType) boxType(possiblyPrimitiveType) else possiblyPrimitiveType
|
|
||||||
|
|
||||||
interface TypeMappingConfiguration<out T : Any> {
|
interface TypeMappingConfiguration<out T : Any> {
|
||||||
fun commonSupertype(types: Collection<@JvmSuppressWildcards KotlinType>): KotlinType
|
fun commonSupertype(types: Collection<@JvmSuppressWildcards KotlinType>): KotlinType
|
||||||
fun getPredefinedTypeForClass(classDescriptor: ClassDescriptor): T?
|
fun getPredefinedTypeForClass(classDescriptor: ClassDescriptor): T?
|
||||||
@@ -49,8 +29,6 @@ interface TypeMappingConfiguration<out T : Any> {
|
|||||||
fun releaseCoroutines(): Boolean = true
|
fun releaseCoroutines(): Boolean = true
|
||||||
}
|
}
|
||||||
|
|
||||||
const val NON_EXISTENT_CLASS_NAME = "error/NonExistentClass"
|
|
||||||
|
|
||||||
fun <T : Any> mapType(
|
fun <T : Any> mapType(
|
||||||
kotlinType: KotlinType,
|
kotlinType: KotlinType,
|
||||||
factory: JvmTypeFactory<T>,
|
factory: JvmTypeFactory<T>,
|
||||||
@@ -185,46 +163,12 @@ fun <T : Any> mapType(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fun hasVoidReturnType(descriptor: CallableDescriptor): Boolean {
|
fun hasVoidReturnType(descriptor: CallableDescriptor): Boolean {
|
||||||
if (descriptor is ConstructorDescriptor) return true
|
if (descriptor is ConstructorDescriptor) return true
|
||||||
return KotlinBuiltIns.isUnit(descriptor.returnType!!) && !TypeUtils.isNullableType(descriptor.returnType!!)
|
return KotlinBuiltIns.isUnit(descriptor.returnType!!) && !TypeUtils.isNullableType(descriptor.returnType!!)
|
||||||
&& descriptor !is PropertyGetterDescriptor
|
&& descriptor !is PropertyGetterDescriptor
|
||||||
}
|
}
|
||||||
|
|
||||||
fun <T : Any> TypeSystemCommonBackendContext.mapBuiltInType(
|
|
||||||
type: KotlinTypeMarker,
|
|
||||||
typeFactory: JvmTypeFactory<T>,
|
|
||||||
mode: TypeMappingMode
|
|
||||||
): T? {
|
|
||||||
val constructor = type.typeConstructor()
|
|
||||||
if (!constructor.isClassTypeConstructor()) return null
|
|
||||||
|
|
||||||
val primitiveType = constructor.getPrimitiveType()
|
|
||||||
if (primitiveType != null) {
|
|
||||||
val jvmType = typeFactory.createPrimitiveType(primitiveType)
|
|
||||||
val isNullableInJava = type.isNullableType() || hasEnhancedNullability(type)
|
|
||||||
return typeFactory.boxTypeIfNeeded(jvmType, isNullableInJava)
|
|
||||||
}
|
|
||||||
|
|
||||||
val arrayElementType = constructor.getPrimitiveArrayType()
|
|
||||||
if (arrayElementType != null) {
|
|
||||||
return typeFactory.createFromString("[" + JvmPrimitiveType.get(arrayElementType).desc)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (constructor.isUnderKotlinPackage()) {
|
|
||||||
val classId = constructor.getClassFqNameUnsafe()?.let(JavaToKotlinClassMap::mapKotlinToJava)
|
|
||||||
if (classId != null) {
|
|
||||||
if (!mode.kotlinCollectionsToJavaCollections && JavaToKotlinClassMap.mutabilityMappings.any { it.javaClass == classId })
|
|
||||||
return null
|
|
||||||
|
|
||||||
return typeFactory.createObjectType(JvmClassName.byClassId(classId).internalName)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
fun computeInternalName(
|
fun computeInternalName(
|
||||||
klass: ClassDescriptor,
|
klass: ClassDescriptor,
|
||||||
typeMappingConfiguration: TypeMappingConfiguration<*> = TypeMappingConfigurationImpl
|
typeMappingConfiguration: TypeMappingConfiguration<*> = TypeMappingConfigurationImpl
|
||||||
@@ -248,42 +192,3 @@ fun computeInternalName(
|
|||||||
|
|
||||||
return "$containerInternalName$$name"
|
return "$containerInternalName$$name"
|
||||||
}
|
}
|
||||||
|
|
||||||
open class JvmDescriptorTypeWriter<T : Any>(private val jvmTypeFactory: JvmTypeFactory<T>) {
|
|
||||||
private var jvmCurrentTypeArrayLevel: Int = 0
|
|
||||||
protected var jvmCurrentType: T? = null
|
|
||||||
private set
|
|
||||||
|
|
||||||
protected fun clearCurrentType() {
|
|
||||||
jvmCurrentType = null
|
|
||||||
jvmCurrentTypeArrayLevel = 0
|
|
||||||
}
|
|
||||||
|
|
||||||
open fun writeArrayType() {
|
|
||||||
if (jvmCurrentType == null) {
|
|
||||||
++jvmCurrentTypeArrayLevel
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
open fun writeArrayEnd() {
|
|
||||||
}
|
|
||||||
|
|
||||||
open fun writeClass(objectType: T) {
|
|
||||||
writeJvmTypeAsIs(objectType)
|
|
||||||
}
|
|
||||||
|
|
||||||
protected fun writeJvmTypeAsIs(type: T) {
|
|
||||||
if (jvmCurrentType == null) {
|
|
||||||
jvmCurrentType =
|
|
||||||
if (jvmCurrentTypeArrayLevel > 0) {
|
|
||||||
jvmTypeFactory.createFromString("[".repeat(jvmCurrentTypeArrayLevel) + jvmTypeFactory.toString(type))
|
|
||||||
} else {
|
|
||||||
type
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
open fun writeTypeVariable(name: Name, type: T) {
|
|
||||||
writeJvmTypeAsIs(type)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user