From c3a89e25073ca706e3809f9f9484b2803fe82842 Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Tue, 22 Sep 2020 11:46:14 +0300 Subject: [PATCH] Move common part of typeSignatureMapping.kt to :core:compiler.common.jvm --- .../kotlin/codegen/ExpressionCodegen.java | 3 +- .../typeEnhancement/typeEnchancementUtils.kt | 7 ++ .../load/kotlin/typeSignatureMapping.kt | 102 ++++++++++++++++++ .../java/typeEnhancement/typeEnhancement.kt | 4 - ...=> descriptorBasedTypeSignatureMapping.kt} | 97 +---------------- 5 files changed, 112 insertions(+), 101 deletions(-) create mode 100644 core/compiler.common.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt rename core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/{typeSignatureMapping.kt => descriptorBasedTypeSignatureMapping.kt} (72%) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java index 5eefd262d8b..82ece1cadbe 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/ExpressionCodegen.java @@ -52,6 +52,7 @@ import org.jetbrains.kotlin.descriptors.impl.TypeAliasConstructorDescriptor; import org.jetbrains.kotlin.diagnostics.Errors; import org.jetbrains.kotlin.lexer.KtTokens; 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.TypeSignatureMappingKt; import org.jetbrains.kotlin.name.Name; @@ -1813,7 +1814,7 @@ public class ExpressionCodegen extends KtVisitor impleme FunctionDescriptor originalSuspendLambdaDescriptor = getOriginalSuspendLambdaDescriptorFromContext(context); boolean isVoidCoroutineLambda = - originalSuspendLambdaDescriptor != null && TypeSignatureMappingKt.hasVoidReturnType(originalSuspendLambdaDescriptor); + originalSuspendLambdaDescriptor != null && DescriptorBasedTypeSignatureMappingKt.hasVoidReturnType(originalSuspendLambdaDescriptor); KotlinType originalReturnTypeOfSuspendFunctionReturningUnboxedInlineClass = CoroutineCodegenUtilKt.originalReturnTypeOfSuspendFunctionReturningUnboxedInlineClass( diff --git a/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnchancementUtils.kt b/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnchancementUtils.kt index 2aa50e12529..b3629ec2e44 100644 --- a/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnchancementUtils.kt +++ b/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnchancementUtils.kt @@ -5,6 +5,10 @@ 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( nullability: NullabilityQualifier?, mutability: MutabilityQualifier?, @@ -36,3 +40,6 @@ fun Set.select(own: NullabilityQualifier?, isCovariant: Bo NullabilityQualifier.FORCE_FLEXIBILITY else select(NullabilityQualifier.NOT_NULL, NullabilityQualifier.NULLABLE, own, isCovariant) + +fun TypeSystemCommonBackendContext.hasEnhancedNullability(type: KotlinTypeMarker): Boolean = + type.hasAnnotation(JvmAnnotationNames.ENHANCED_NULLABILITY_ANNOTATION) diff --git a/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt b/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt new file mode 100644 index 00000000000..286f80c3ee2 --- /dev/null +++ b/core/compiler.common.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt @@ -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 { + 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 JvmTypeFactory.boxTypeIfNeeded(possiblyPrimitiveType: T, needBoxedType: Boolean): T = + if (needBoxedType) boxType(possiblyPrimitiveType) else possiblyPrimitiveType + +const val NON_EXISTENT_CLASS_NAME = "error/NonExistentClass" + +fun TypeSystemCommonBackendContext.mapBuiltInType( + type: KotlinTypeMarker, + typeFactory: JvmTypeFactory, + 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(private val jvmTypeFactory: JvmTypeFactory) { + 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) + } +} diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt index 6e7aa6ecaa8..5b1dad8017b 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/java/typeEnhancement/typeEnhancement.kt @@ -35,7 +35,6 @@ import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.constants.ConstantValue import org.jetbrains.kotlin.types.* 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.typeUtil.createProjection import org.jetbrains.kotlin.types.typeUtil.isTypeParameter @@ -43,9 +42,6 @@ import org.jetbrains.kotlin.types.typeUtil.isTypeParameter fun KotlinType.hasEnhancedNullability(): Boolean = SimpleClassicTypeSystemContext.hasEnhancedNullability(this) -fun TypeSystemCommonBackendContext.hasEnhancedNullability(type: KotlinTypeMarker): Boolean = - type.hasAnnotation(JvmAnnotationNames.ENHANCED_NULLABILITY_ANNOTATION) - class JavaTypeEnhancement(private val javaResolverSettings: JavaResolverSettings) { private open class Result(open val type: KotlinType, val subtreeSize: Int, val wereChanges: Boolean) { diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/descriptorBasedTypeSignatureMapping.kt similarity index 72% rename from core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt rename to core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/descriptorBasedTypeSignatureMapping.kt index bb9718fce64..fbb9379f43d 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/descriptorBasedTypeSignatureMapping.kt @@ -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. */ 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 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.resolve.jvm.JvmClassName -import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType import org.jetbrains.kotlin.types.* 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.representativeUpperBound import org.jetbrains.kotlin.utils.DO_NOTHING_3 -interface JvmTypeFactory { - 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 JvmTypeFactory.boxTypeIfNeeded(possiblyPrimitiveType: T, needBoxedType: Boolean) = - if (needBoxedType) boxType(possiblyPrimitiveType) else possiblyPrimitiveType - interface TypeMappingConfiguration { fun commonSupertype(types: Collection<@JvmSuppressWildcards KotlinType>): KotlinType fun getPredefinedTypeForClass(classDescriptor: ClassDescriptor): T? @@ -49,8 +29,6 @@ interface TypeMappingConfiguration { fun releaseCoroutines(): Boolean = true } -const val NON_EXISTENT_CLASS_NAME = "error/NonExistentClass" - fun mapType( kotlinType: KotlinType, factory: JvmTypeFactory, @@ -185,46 +163,12 @@ fun mapType( } } - fun hasVoidReturnType(descriptor: CallableDescriptor): Boolean { if (descriptor is ConstructorDescriptor) return true return KotlinBuiltIns.isUnit(descriptor.returnType!!) && !TypeUtils.isNullableType(descriptor.returnType!!) && descriptor !is PropertyGetterDescriptor } -fun TypeSystemCommonBackendContext.mapBuiltInType( - type: KotlinTypeMarker, - typeFactory: JvmTypeFactory, - 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( klass: ClassDescriptor, typeMappingConfiguration: TypeMappingConfiguration<*> = TypeMappingConfigurationImpl @@ -248,42 +192,3 @@ fun computeInternalName( return "$containerInternalName$$name" } - -open class JvmDescriptorTypeWriter(private val jvmTypeFactory: JvmTypeFactory) { - 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) - } -}