[Commonizer] Reworked functions for making nullable CIR types

This commit is contained in:
Dmitriy Dolovov
2021-03-02 02:05:43 +03:00
parent f2db93a9d2
commit 43d9c6cc90
2 changed files with 34 additions and 25 deletions
@@ -67,7 +67,7 @@ object CirTypeFactory {
val expandedType = extractExpandedType(abbreviatedType) val expandedType = extractExpandedType(abbreviatedType)
val cirExpandedType = create(expandedType) as CirClassOrTypeAliasType val cirExpandedType = create(expandedType) as CirClassOrTypeAliasType
val cirExpandedTypeWithProperNullability = if (source.isMarkedNullable) makeNullable(cirExpandedType) else cirExpandedType val cirExpandedTypeWithProperNullability = makeNullableIfNecessary(cirExpandedType, source.isMarkedNullable)
createTypeAliasType( createTypeAliasType(
typeAliasId = classifierDescriptor.classifierId, typeAliasId = classifierDescriptor.classifierId,
@@ -128,25 +128,38 @@ object CirTypeFactory {
) )
} }
fun makeNullable(classOrTypeAliasType: CirClassOrTypeAliasType): CirClassOrTypeAliasType = fun <T : CirSimpleType> makeNullable(type: T): T {
if (classOrTypeAliasType.isMarkedNullable) if (type.isMarkedNullable)
classOrTypeAliasType return type
else
when (classOrTypeAliasType) { val result = when (type) {
is CirClassType -> createClassType( is CirClassType -> createClassType(
classId = classOrTypeAliasType.classifierId, classId = type.classifierId,
outerType = classOrTypeAliasType.outerType, outerType = type.outerType,
visibility = classOrTypeAliasType.visibility, visibility = type.visibility,
arguments = classOrTypeAliasType.arguments, arguments = type.arguments,
isMarkedNullable = true isMarkedNullable = true
) )
is CirTypeAliasType -> createTypeAliasType( is CirTypeAliasType -> createTypeAliasType(
typeAliasId = classOrTypeAliasType.classifierId, typeAliasId = type.classifierId,
underlyingType = makeNullable(classOrTypeAliasType.underlyingType), underlyingType = makeNullable(type.underlyingType),
arguments = classOrTypeAliasType.arguments, arguments = type.arguments,
isMarkedNullable = true isMarkedNullable = true
) )
} is CirTypeParameterType -> createTypeParameterType(
index = type.index,
isMarkedNullable = true
)
else -> error("Unsupported type: $type")
}
@Suppress("UNCHECKED_CAST")
return result as T
}
@Suppress("NOTHING_TO_INLINE")
inline fun <T : CirSimpleType> makeNullableIfNecessary(type: T, necessary: Boolean): T =
if (!necessary) type else makeNullable(type)
fun unabbreviate(type: CirClassOrTypeAliasType): CirClassType = when (type) { fun unabbreviate(type: CirClassOrTypeAliasType): CirClassType = when (type) {
is CirClassType -> { is CirClassType -> {
@@ -143,11 +143,7 @@ private class TypeAliasTypeCommonizer(private val classifiers: CirKnownClassifie
// type alias don't needs to be commonized because it is from the standard library // type alias don't needs to be commonized because it is from the standard library
fun forKnownUnderlyingType(underlyingType: CirClassOrTypeAliasType) = object : CommonizedTypeAliasTypeBuilder { fun forKnownUnderlyingType(underlyingType: CirClassOrTypeAliasType) = object : CommonizedTypeAliasTypeBuilder {
override fun build(typeAliasId: CirEntityId, arguments: List<CirTypeProjection>, isMarkedNullable: Boolean): CirTypeAliasType { override fun build(typeAliasId: CirEntityId, arguments: List<CirTypeProjection>, isMarkedNullable: Boolean): CirTypeAliasType {
val underlyingTypeWithProperNullability = if (isMarkedNullable && !underlyingType.isMarkedNullable) val underlyingTypeWithProperNullability = CirTypeFactory.makeNullableIfNecessary(underlyingType, isMarkedNullable)
CirTypeFactory.makeNullable(underlyingType)
else
underlyingType
return CirTypeFactory.createTypeAliasType( return CirTypeFactory.createTypeAliasType(
typeAliasId = typeAliasId, typeAliasId = typeAliasId,
underlyingType = underlyingTypeWithProperNullability, // TODO replace arguments??? underlyingType = underlyingTypeWithProperNullability, // TODO replace arguments???