Introduce AbstractTypeMapper based on type markers

This commit is contained in:
Dmitriy Novozhilov
2020-09-22 18:20:57 +03:00
parent 7380abac13
commit 86d4d320c4
5 changed files with 208 additions and 51 deletions
@@ -10,11 +10,7 @@ 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.TypeSystemCommonBackendContext
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.types.model.KotlinTypeMarker
import org.jetbrains.kotlin.types.model.SimpleTypeMarker
import org.jetbrains.kotlin.types.model.TypeConstructorMarker
import org.jetbrains.kotlin.types.typeUtil.representativeUpperBound
internal fun computeUnderlyingType(inlineClassType: KotlinType): KotlinType? {
@@ -27,52 +23,6 @@ internal fun computeUnderlyingType(inlineClassType: KotlinType): KotlinType? {
inlineClassType.substitutedUnderlyingType()
}
fun TypeSystemCommonBackendContext.computeExpandedTypeForInlineClass(inlineClassType: KotlinTypeMarker): KotlinTypeMarker? =
computeExpandedTypeInner(inlineClassType, hashSetOf())
private fun TypeSystemCommonBackendContext.computeExpandedTypeInner(
kotlinType: KotlinTypeMarker, visitedClassifiers: HashSet<TypeConstructorMarker>
): KotlinTypeMarker? {
val classifier = kotlinType.typeConstructor()
if (!visitedClassifiers.add(classifier)) return null
val typeParameter = classifier.getTypeParameterClassifier()
return when {
typeParameter != null ->
computeExpandedTypeInner(typeParameter.getRepresentativeUpperBound(), visitedClassifiers)
?.let { expandedUpperBound ->
if (expandedUpperBound.isNullableType() || !kotlinType.isMarkedNullable())
expandedUpperBound
else
expandedUpperBound.makeNullable()
}
classifier.isInlineClass() -> {
// kotlinType is the boxed inline class type
val underlyingType = kotlinType.getSubstitutedUnderlyingType() ?: return null
val expandedUnderlyingType = computeExpandedTypeInner(underlyingType, visitedClassifiers) ?: return null
when {
!kotlinType.isNullableType() -> expandedUnderlyingType
// Here inline class type is nullable. Apply nullability to the expandedUnderlyingType.
// Nullable types become inline class boxes
expandedUnderlyingType.isNullableType() -> kotlinType
// Primitives become inline class boxes
expandedUnderlyingType is SimpleTypeMarker && expandedUnderlyingType.isPrimitiveType() -> kotlinType
// Non-null reference types become nullable reference types
else -> expandedUnderlyingType.makeNullable()
}
}
else -> kotlinType
}
}
internal fun shouldUseUnderlyingType(inlineClassType: KotlinType): Boolean {
val underlyingType = inlineClassType.unsubstitutedUnderlyingType() ?: return false