JVM IR: get rid of toIrBasedKotlinType in MethodSignatureMapper

Commonize (in terms of TypeSystemCommonBackendContext implementations
for KotlinType/IrType) code that computes optimal TypeMappingMode to
apply to different positions where inline class types can be present.
This commit is contained in:
Alexander Udalov
2021-03-31 19:30:30 +02:00
parent b59ac5d8f6
commit ce0c0ad2e3
7 changed files with 37 additions and 45 deletions
@@ -5,28 +5,32 @@
package org.jetbrains.kotlin.load.kotlin
import org.jetbrains.kotlin.resolve.isInlineClassType
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeSystemCommonBackendContext
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
fun TypeMappingMode.Companion.getOptimalModeForValueParameter(
type: KotlinType
fun TypeSystemCommonBackendContext.getOptimalModeForValueParameter(
type: KotlinTypeMarker
): TypeMappingMode = getOptimalModeForSignaturePart(type, canBeUsedInSupertypePosition = true)
fun TypeMappingMode.Companion.getOptimalModeForReturnType(
type: KotlinType,
fun TypeSystemCommonBackendContext.getOptimalModeForReturnType(
type: KotlinTypeMarker,
isAnnotationMethod: Boolean
): TypeMappingMode {
return if (isAnnotationMethod)
VALUE_FOR_ANNOTATION
TypeMappingMode.VALUE_FOR_ANNOTATION
else
getOptimalModeForSignaturePart(type, canBeUsedInSupertypePosition = false)
}
@OptIn(TypeMappingModeInternals::class)
private fun getOptimalModeForSignaturePart(type: KotlinType, canBeUsedInSupertypePosition: Boolean): TypeMappingMode {
if (type.arguments.isEmpty()) return TypeMappingMode.DEFAULT
private fun TypeSystemCommonBackendContext.getOptimalModeForSignaturePart(
type: KotlinTypeMarker,
canBeUsedInSupertypePosition: Boolean
): TypeMappingMode {
if (type.argumentsCount() == 0) return TypeMappingMode.DEFAULT
if (type.isInlineClassType() && shouldUseUnderlyingType(type)) {
val isInlineClassType = type.typeConstructor().isInlineClass()
if (isInlineClassType && shouldUseUnderlyingType(type)) {
val underlyingType = computeUnderlyingType(type)
if (underlyingType != null) {
return getOptimalModeForSignaturePart(underlyingType, canBeUsedInSupertypePosition).dontWrapInlineClassesMode()
@@ -50,6 +54,6 @@ private fun getOptimalModeForSignaturePart(type: KotlinType, canBeUsedInSupertyp
skipDeclarationSiteWildcardsIfPossible = true,
genericContravariantArgumentMode = contravariantArgumentMode,
genericInvariantArgumentMode = invariantArgumentMode,
needInlineClassWrapping = !type.isInlineClassType()
needInlineClassWrapping = !isInlineClassType
)
}
@@ -5,27 +5,21 @@
package org.jetbrains.kotlin.load.kotlin
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.resolve.substitutedUnderlyingType
import org.jetbrains.kotlin.resolve.unsubstitutedUnderlyingType
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.typeUtil.representativeUpperBound
import org.jetbrains.kotlin.types.TypeSystemCommonBackendContext
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.SimpleTypeMarker
internal fun computeUnderlyingType(inlineClassType: KotlinType): KotlinType? {
internal fun TypeSystemCommonBackendContext.computeUnderlyingType(inlineClassType: KotlinTypeMarker): KotlinTypeMarker? {
if (!shouldUseUnderlyingType(inlineClassType)) return null
val descriptor = inlineClassType.unsubstitutedUnderlyingType()?.constructor?.declarationDescriptor ?: return null
return if (descriptor is TypeParameterDescriptor)
descriptor.representativeUpperBound
else
inlineClassType.substitutedUnderlyingType()
val underlyingType = inlineClassType.getUnsubstitutedUnderlyingType() ?: return null
return underlyingType.typeConstructor().getTypeParameterClassifier()?.getRepresentativeUpperBound()
?: inlineClassType.getSubstitutedUnderlyingType()
}
internal fun shouldUseUnderlyingType(inlineClassType: KotlinType): Boolean {
val underlyingType = inlineClassType.unsubstitutedUnderlyingType() ?: return false
internal fun TypeSystemCommonBackendContext.shouldUseUnderlyingType(inlineClassType: KotlinTypeMarker): Boolean {
val underlyingType = inlineClassType.getUnsubstitutedUnderlyingType() ?: return false
return !inlineClassType.isMarkedNullable ||
!TypeUtils.isNullableType(underlyingType) && !KotlinBuiltIns.isPrimitiveType(underlyingType)
return !inlineClassType.isMarkedNullable() ||
!underlyingType.isNullableType() && !(underlyingType is SimpleTypeMarker && underlyingType.isPrimitiveType())
}