From d399144b80e95a9593f0ba95c69b333e3913aa98 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Thu, 27 Jun 2019 16:48:09 +0200 Subject: [PATCH] Use TypeSystemCommonBackendContext in mapBuiltInType --- .../java/typeEnhancement/typeEnhancement.kt | 8 +++++- .../load/kotlin/typeSignatureMapping.kt | 28 ++++++++++--------- .../types/TypeSystemCommonBackendContext.kt | 8 ++++++ .../types/checker/ClassicTypeSystemContext.kt | 23 +++++++++++++++ 4 files changed, 53 insertions(+), 14 deletions(-) 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 f73f8c9de52..1ec73eecced 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 @@ -33,6 +33,8 @@ import org.jetbrains.kotlin.name.FqName 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.typeUtil.createProjection import org.jetbrains.kotlin.types.typeUtil.isTypeParameter @@ -42,7 +44,11 @@ import org.jetbrains.kotlin.types.typeUtil.isTypeParameter // For flexible types, both bounds are indexed in the same way: `(A..C)` gives `0 - (A..C), 1 - B and D`. fun KotlinType.enhance(qualifiers: (Int) -> JavaTypeQualifiers) = unwrap().enhancePossiblyFlexible(qualifiers, 0).typeIfChanged -fun KotlinType.hasEnhancedNullability() = annotations.findAnnotation(JvmAnnotationNames.ENHANCED_NULLABILITY_ANNOTATION) != null +fun KotlinType.hasEnhancedNullability(): Boolean = + SimpleClassicTypeSystemContext.hasEnhancedNullability(this) + +fun TypeSystemCommonBackendContext.hasEnhancedNullability(type: KotlinTypeMarker): Boolean = + type.hasAnnotation(JvmAnnotationNames.ENHANCED_NULLABILITY_ANNOTATION) enum class TypeComponentPosition { FLEXIBLE_LOWER, diff --git a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt index 532e0a676e9..f185043a72b 100644 --- a/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt +++ b/core/descriptors.jvm/src/org/jetbrains/kotlin/load/kotlin/typeSignatureMapping.kt @@ -13,11 +13,11 @@ 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.descriptorUtil.fqNameUnsafe 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 @@ -69,7 +69,9 @@ fun mapType( ) } - mapBuiltInType(kotlinType, factory, mode)?.let { builtInType -> + with(SimpleClassicTypeSystemContext) { + mapBuiltInType(kotlinType, factory, mode) + }?.let { builtInType -> val jvmType = factory.boxTypeIfNeeded(builtInType, mode.needPrimitiveBoxing) writeGenericType(kotlinType, jvmType, mode) return jvmType @@ -203,31 +205,31 @@ fun hasVoidReturnType(descriptor: CallableDescriptor): Boolean { && descriptor !is PropertyGetterDescriptor } -private fun mapBuiltInType( - type: KotlinType, +fun TypeSystemCommonBackendContext.mapBuiltInType( + type: KotlinTypeMarker, typeFactory: JvmTypeFactory, mode: TypeMappingMode ): T? { - val descriptor = type.constructor.declarationDescriptor as? ClassDescriptor ?: return null + val constructor = type.typeConstructor() + if (!constructor.isClassTypeConstructor()) return null - val primitiveType = KotlinBuiltIns.getPrimitiveType(descriptor) + val primitiveType = constructor.getPrimitiveType() if (primitiveType != null) { val jvmType = typeFactory.createFromString(JvmPrimitiveType.get(primitiveType).desc) - val isNullableInJava = TypeUtils.isNullableType(type) || type.hasEnhancedNullability() + val isNullableInJava = type.isNullableType() || hasEnhancedNullability(type) return typeFactory.boxTypeIfNeeded(jvmType, isNullableInJava) } - val arrayElementType = KotlinBuiltIns.getPrimitiveArrayType(descriptor) + val arrayElementType = constructor.getPrimitiveArrayType() if (arrayElementType != null) { return typeFactory.createFromString("[" + JvmPrimitiveType.get(arrayElementType).desc) } - if (KotlinBuiltIns.isUnderKotlinPackage(descriptor)) { - val classId = JavaToKotlinClassMap.mapKotlinToJava(descriptor.fqNameUnsafe) + if (constructor.isUnderKotlinPackage()) { + val classId = constructor.getClassFqNameUnsafe()?.let(JavaToKotlinClassMap::mapKotlinToJava) if (classId != null) { - if (!mode.kotlinCollectionsToJavaCollections && - JavaToKotlinClassMap.mutabilityMappings.any { it.javaClass == classId } - ) return null + if (!mode.kotlinCollectionsToJavaCollections && JavaToKotlinClassMap.mutabilityMappings.any { it.javaClass == classId }) + return null return typeFactory.createObjectType(JvmClassName.byClassId(classId).internalName) } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/TypeSystemCommonBackendContext.kt b/core/descriptors/src/org/jetbrains/kotlin/types/TypeSystemCommonBackendContext.kt index 4ce26d7c58c..60b8b6301bf 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/TypeSystemCommonBackendContext.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/TypeSystemCommonBackendContext.kt @@ -5,7 +5,9 @@ package org.jetbrains.kotlin.types +import org.jetbrains.kotlin.builtins.PrimitiveType import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.FqNameUnsafe import org.jetbrains.kotlin.types.model.* interface TypeSystemCommonBackendContext : TypeSystemContext { @@ -33,4 +35,10 @@ interface TypeSystemCommonBackendContext : TypeSystemContext { fun KotlinTypeMarker.makeNullable(): KotlinTypeMarker = asSimpleType()?.withNullability(true) ?: this + + fun TypeConstructorMarker.getPrimitiveType(): PrimitiveType? + fun TypeConstructorMarker.getPrimitiveArrayType(): PrimitiveType? + + fun TypeConstructorMarker.isUnderKotlinPackage(): Boolean + fun TypeConstructorMarker.getClassFqNameUnsafe(): FqNameUnsafe? } diff --git a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt index ff2d68fcb8f..03586174fe9 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/types/checker/ClassicTypeSystemContext.kt @@ -7,12 +7,15 @@ package org.jetbrains.kotlin.types.checker import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.KotlinBuiltIns.FQ_NAMES +import org.jetbrains.kotlin.builtins.PrimitiveType import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.name.FqName +import org.jetbrains.kotlin.name.FqNameUnsafe import org.jetbrains.kotlin.resolve.calls.inference.CapturedType import org.jetbrains.kotlin.resolve.constants.IntegerLiteralTypeConstructor +import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe import org.jetbrains.kotlin.resolve.descriptorUtil.hasExactAnnotation import org.jetbrains.kotlin.resolve.descriptorUtil.hasNoInferAnnotation import org.jetbrains.kotlin.resolve.descriptorUtil.isExactAnnotation @@ -517,6 +520,26 @@ interface ClassicTypeSystemContext : TypeSystemInferenceExtensionContext, TypeSy require(this is KotlinType, this::errorMessage) return substitutedUnderlyingType() } + + override fun TypeConstructorMarker.getPrimitiveType(): PrimitiveType? { + require(this is TypeConstructor, this::errorMessage) + return KotlinBuiltIns.getPrimitiveType(declarationDescriptor as ClassDescriptor) + } + + override fun TypeConstructorMarker.getPrimitiveArrayType(): PrimitiveType? { + require(this is TypeConstructor, this::errorMessage) + return KotlinBuiltIns.getPrimitiveArrayType(declarationDescriptor as ClassDescriptor) + } + + override fun TypeConstructorMarker.isUnderKotlinPackage(): Boolean { + require(this is TypeConstructor, this::errorMessage) + return declarationDescriptor?.let(KotlinBuiltIns::isUnderKotlinPackage) == true + } + + override fun TypeConstructorMarker.getClassFqNameUnsafe(): FqNameUnsafe { + require(this is TypeConstructor, this::errorMessage) + return (declarationDescriptor as ClassDescriptor).fqNameUnsafe + } } fun TypeVariance.convertVariance(): Variance {