Move common part of typeSignatureMapping.kt to :core:compiler.common.jvm

This commit is contained in:
Dmitriy Novozhilov
2020-09-22 11:46:14 +03:00
parent d215c413cc
commit c3a89e2507
5 changed files with 112 additions and 101 deletions
@@ -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<StackValue, StackValue> impleme
FunctionDescriptor originalSuspendLambdaDescriptor = getOriginalSuspendLambdaDescriptorFromContext(context);
boolean isVoidCoroutineLambda =
originalSuspendLambdaDescriptor != null && TypeSignatureMappingKt.hasVoidReturnType(originalSuspendLambdaDescriptor);
originalSuspendLambdaDescriptor != null && DescriptorBasedTypeSignatureMappingKt.hasVoidReturnType(originalSuspendLambdaDescriptor);
KotlinType originalReturnTypeOfSuspendFunctionReturningUnboxedInlineClass =
CoroutineCodegenUtilKt.originalReturnTypeOfSuspendFunctionReturningUnboxedInlineClass(
@@ -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<NullabilityQualifier>.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)
@@ -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)
}
}
@@ -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) {
@@ -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<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> {
fun commonSupertype(types: Collection<@JvmSuppressWildcards KotlinType>): KotlinType
fun getPredefinedTypeForClass(classDescriptor: ClassDescriptor): T?
@@ -49,8 +29,6 @@ interface TypeMappingConfiguration<out T : Any> {
fun releaseCoroutines(): Boolean = true
}
const val NON_EXISTENT_CLASS_NAME = "error/NonExistentClass"
fun <T : Any> mapType(
kotlinType: KotlinType,
factory: JvmTypeFactory<T>,
@@ -185,46 +163,12 @@ fun <T : Any> 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 <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(
klass: ClassDescriptor,
typeMappingConfiguration: TypeMappingConfiguration<*> = TypeMappingConfigurationImpl
@@ -248,42 +192,3 @@ fun computeInternalName(
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)
}
}