[Commonizer] Put all CIR utils to a single file

This commit is contained in:
Dmitriy Dolovov
2021-03-05 01:24:01 +03:00
parent 20ab88c001
commit 6697c4d197
9 changed files with 94 additions and 89 deletions
@@ -30,6 +30,3 @@ object CirClassifierRecursionMarker : CirClassifier, CirRecursionMarker {
override val typeParameters get() = unsupported()
override val visibility get() = unsupported()
}
@Suppress("unused", "NOTHING_TO_INLINE")
internal inline fun CirDeclaration.unsupported(): Nothing = error("This method should never be called on ${this::class.java}, $this")
@@ -9,13 +9,14 @@ import kotlinx.metadata.KmTypeAlias
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirClassOrTypeAliasType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirName
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeAlias
import org.jetbrains.kotlin.descriptors.commonizer.cir.unabbreviate
import org.jetbrains.kotlin.descriptors.commonizer.metadata.decodeVisibility
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
object CirTypeAliasFactory {
fun create(name: CirName, source: KmTypeAlias, typeResolver: CirTypeResolver): CirTypeAlias {
val underlyingType = CirTypeFactory.create(source.underlyingType, typeResolver) as CirClassOrTypeAliasType
val expandedType = CirTypeFactory.unabbreviate(underlyingType)
val expandedType = underlyingType.unabbreviate()
return CirTypeAlias.create(
annotations = CirAnnotationFactory.createAnnotations(source.flags, typeResolver, source::annotations),
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.descriptors.commonizer.cir.factory
import gnu.trove.TIntObjectHashMap
import kotlinx.metadata.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.core.computeExpandedType
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirProvided
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirProvidedClassifiers
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.TypeParameterResolver
@@ -68,39 +67,6 @@ object CirTypeFactory {
}
}
fun <T : CirSimpleType> makeNullable(type: T): T {
if (type.isMarkedNullable)
return type
val result = when (type) {
is CirClassType -> CirClassType.createInterned(
classId = type.classifierId,
outerType = type.outerType,
visibility = type.visibility,
arguments = type.arguments,
isMarkedNullable = true
)
is CirTypeAliasType -> CirTypeAliasType.createInterned(
typeAliasId = type.classifierId,
underlyingType = makeNullable(type.underlyingType),
arguments = type.arguments,
isMarkedNullable = true
)
is CirTypeParameterType -> CirTypeParameterType.createInterned(
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)
@Suppress("NOTHING_TO_INLINE")
inline fun decodeVariance(variance: KmVariance): Variance = when (variance) {
KmVariance.INVARIANT -> Variance.INVARIANT
@@ -108,42 +74,6 @@ object CirTypeFactory {
KmVariance.OUT -> Variance.OUT_VARIANCE
}
fun unabbreviate(type: CirClassOrTypeAliasType): CirClassType = when (type) {
is CirClassType -> {
var hasAbbreviationsInArguments = false
val unabbreviatedArguments = type.arguments.compactMap { argument ->
val argumentType =
(argument as? CirTypeProjectionImpl)?.type as? CirClassOrTypeAliasType ?: return@compactMap argument
val unabbreviatedArgumentType = unabbreviate(argumentType)
if (argumentType == unabbreviatedArgumentType)
argument
else {
hasAbbreviationsInArguments = true
CirTypeProjectionImpl(
projectionKind = argument.projectionKind,
type = unabbreviatedArgumentType
)
}
}
val outerType = type.outerType
val unabbreviatedOuterType = outerType?.let(::unabbreviate)
if (!hasAbbreviationsInArguments && outerType == unabbreviatedOuterType)
type
else
CirClassType.createInterned(
classId = type.classifierId,
outerType = unabbreviatedOuterType,
visibility = type.visibility,
arguments = unabbreviatedArguments,
isMarkedNullable = type.isMarkedNullable
)
}
is CirTypeAliasType -> unabbreviate(computeExpandedType(type))
}
@Suppress("NOTHING_TO_INLINE")
private inline fun createArguments(arguments: List<KmTypeProjection>, typeResolver: CirTypeResolver): List<CirTypeProjection> {
return arguments.compactMap { argument ->
@@ -0,0 +1,87 @@
/*
* Copyright 2010-2021 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.descriptors.commonizer.cir
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMap
fun <T : CirSimpleType> T.makeNullable(): T {
if (isMarkedNullable)
return this
val result = when (this) {
is CirClassType -> CirClassType.createInterned(
classId = classifierId,
outerType = outerType,
visibility = visibility,
arguments = arguments,
isMarkedNullable = true
)
is CirTypeAliasType -> CirTypeAliasType.createInterned(
typeAliasId = classifierId,
underlyingType = underlyingType.makeNullable(),
arguments = arguments,
isMarkedNullable = true
)
is CirTypeParameterType -> CirTypeParameterType.createInterned(
index = index,
isMarkedNullable = true
)
else -> error("Unsupported type: $this")
}
@Suppress("UNCHECKED_CAST")
return result as T
}
@Suppress("NOTHING_TO_INLINE")
inline fun <T : CirSimpleType> T.makeNullableIfNecessary(necessary: Boolean): T =
if (!necessary) this else makeNullable()
fun CirClassOrTypeAliasType.unabbreviate(): CirClassType = when (this) {
is CirClassType -> {
var hasAbbreviationsInArguments = false
val unabbreviatedArguments = arguments.compactMap { argument ->
val argumentType =
(argument as? CirTypeProjectionImpl)?.type as? CirClassOrTypeAliasType ?: return@compactMap argument
val unabbreviatedArgumentType = argumentType.unabbreviate()
if (argumentType == unabbreviatedArgumentType)
argument
else {
hasAbbreviationsInArguments = true
CirTypeProjectionImpl(
projectionKind = argument.projectionKind,
type = unabbreviatedArgumentType
)
}
}
val outerType = outerType
val unabbreviatedOuterType = outerType?.unabbreviate()
if (!hasAbbreviationsInArguments && outerType == unabbreviatedOuterType)
this
else
CirClassType.createInterned(
classId = classifierId,
outerType = unabbreviatedOuterType,
visibility = visibility,
arguments = unabbreviatedArguments,
isMarkedNullable = isMarkedNullable
)
}
is CirTypeAliasType -> computeExpandedType(this).unabbreviate()
}
internal tailrec fun computeExpandedType(underlyingType: CirClassOrTypeAliasType): CirClassType {
return when (underlyingType) {
is CirClassType -> underlyingType
is CirTypeAliasType -> computeExpandedType(underlyingType.underlyingType)
}
}
@Suppress("unused", "NOTHING_TO_INLINE")
internal inline fun CirDeclaration.unsupported(): Nothing = error("This method should never be called on ${this::class.java}, $this")
@@ -7,7 +7,6 @@ package org.jetbrains.kotlin.descriptors.commonizer.core
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeFactory
import org.jetbrains.kotlin.descriptors.commonizer.core.CommonizedTypeAliasAnswer.Companion.FAILURE_MISSING_IN_SOME_TARGET
import org.jetbrains.kotlin.descriptors.commonizer.core.CommonizedTypeAliasAnswer.Companion.SUCCESS_FROM_DEPENDENCY_LIBRARY
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers
@@ -143,7 +142,7 @@ private class TypeAliasTypeCommonizer(private val classifiers: CirKnownClassifie
// type alias don't needs to be commonized because it is from the standard library
fun forKnownUnderlyingType(underlyingType: CirClassOrTypeAliasType) = object : CommonizedTypeAliasTypeBuilder {
override fun build(typeAliasId: CirEntityId, arguments: List<CirTypeProjection>, isMarkedNullable: Boolean): CirTypeAliasType {
val underlyingTypeWithProperNullability = CirTypeFactory.makeNullableIfNecessary(underlyingType, isMarkedNullable)
val underlyingTypeWithProperNullability = underlyingType.makeNullableIfNecessary(isMarkedNullable)
return CirTypeAliasType.createInterned(
typeAliasId = typeAliasId,
underlyingType = underlyingTypeWithProperNullability, // TODO replace arguments???
@@ -11,13 +11,6 @@ import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeAliasType
import org.jetbrains.kotlin.descriptors.commonizer.cir.CirTypeProjection
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirKnownClassifiers
tailrec fun computeExpandedType(underlyingType: CirClassOrTypeAliasType): CirClassType {
return when (underlyingType) {
is CirClassType -> underlyingType
is CirTypeAliasType -> computeExpandedType(underlyingType.underlyingType)
}
}
internal tailrec fun computeSuitableUnderlyingType(
classifiers: CirKnownClassifiers,
underlyingType: CirClassOrTypeAliasType
@@ -6,7 +6,6 @@
package org.jetbrains.kotlin.descriptors.commonizer.metadata
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeFactory
import org.jetbrains.kotlin.descriptors.commonizer.cir.factory.CirTypeResolver
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.CirProvided
import org.jetbrains.kotlin.descriptors.commonizer.utils.compactMapIndexed
@@ -52,7 +51,7 @@ object CirTypeAliasExpander {
}
val expandedType = expandedProjection.type as CirClassOrTypeAliasType
return CirTypeFactory.makeNullableIfNecessary(expandedType, expansion.isMarkedNullable)
return expandedType.makeNullableIfNecessary(expansion.isMarkedNullable)
}
private fun expandTypeProjection(
@@ -101,7 +100,7 @@ object CirTypeAliasExpander {
}
}
val substitutedType = CirTypeFactory.makeNullableIfNecessary(argumentType, type.isMarkedNullable)
val substitutedType = argumentType.makeNullableIfNecessary(type.isMarkedNullable)
CirTypeProjectionImpl(resultingVariance, substitutedType)
}
@@ -170,7 +169,7 @@ object CirTypeAliasExpander {
CirTypeProjectionImpl(
projectionKind = projection.projectionKind,
type = CirTypeFactory.makeNullable(projectionType)
type = projectionType.makeNullable()
)
}
}
@@ -10,7 +10,6 @@ import kotlinx.metadata.klib.*
import org.jetbrains.kotlin.backend.common.serialization.metadata.DynamicTypeDeserializer
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.descriptors.commonizer.cir.*
import org.jetbrains.kotlin.descriptors.commonizer.core.computeExpandedType
import org.jetbrains.kotlin.descriptors.commonizer.mergedtree.*
import org.jetbrains.kotlin.descriptors.commonizer.metadata.TypeAliasExpansion.*
import org.jetbrains.kotlin.descriptors.commonizer.utils.DEFAULT_SETTER_VALUE_NAME
@@ -30,7 +30,7 @@ internal fun mockTAType(
underlyingType: () -> CirClassOrTypeAliasType
): CirTypeAliasType = CirTypeAliasType.createInterned(
typeAliasId = createValidClassifierId(typeAliasId),
underlyingType = CirTypeFactory.makeNullableIfNecessary(underlyingType(), nullable),
underlyingType = underlyingType().makeNullableIfNecessary(nullable),
arguments = emptyList(),
isMarkedNullable = nullable
)