From 6697c4d19702bcc2ab4bec21744c8f06f8ba9938 Mon Sep 17 00:00:00 2001 From: Dmitriy Dolovov Date: Fri, 5 Mar 2021 01:24:01 +0300 Subject: [PATCH] [Commonizer] Put all CIR utils to a single file --- .../commonizer/cir/RecursionMarkers.kt | 3 - .../cir/factory/CirTypeAliasFactory.kt | 3 +- .../commonizer/cir/factory/CirTypeFactory.kt | 70 --------------- .../descriptors/commonizer/cir/utils.kt | 87 +++++++++++++++++++ .../commonizer/core/TypeCommonizer.kt | 3 +- .../commonizer/core/typeAliasUtils.kt | 7 -- .../metadata/CirTypeAliasExpander.kt | 7 +- .../commonizer/metadata/entityBuilders.kt | 1 - .../descriptors/commonizer/utils/mocks.kt | 2 +- 9 files changed, 94 insertions(+), 89 deletions(-) create mode 100644 native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/utils.kt diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/RecursionMarkers.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/RecursionMarkers.kt index d4abfa30203..f1bf232d6a2 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/RecursionMarkers.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/RecursionMarkers.kt @@ -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") diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeAliasFactory.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeAliasFactory.kt index 2b1f5f5ca74..ff0606509db 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeAliasFactory.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeAliasFactory.kt @@ -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), diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeFactory.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeFactory.kt index ae0c5800d89..ddc4a1b7d51 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeFactory.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/factory/CirTypeFactory.kt @@ -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 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 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, typeResolver: CirTypeResolver): List { return arguments.compactMap { argument -> diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/utils.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/utils.kt new file mode 100644 index 00000000000..ab15b56d105 --- /dev/null +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/cir/utils.kt @@ -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.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.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") diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/TypeCommonizer.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/TypeCommonizer.kt index 49db914ff18..bca8b66753b 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/TypeCommonizer.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/TypeCommonizer.kt @@ -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, isMarkedNullable: Boolean): CirTypeAliasType { - val underlyingTypeWithProperNullability = CirTypeFactory.makeNullableIfNecessary(underlyingType, isMarkedNullable) + val underlyingTypeWithProperNullability = underlyingType.makeNullableIfNecessary(isMarkedNullable) return CirTypeAliasType.createInterned( typeAliasId = typeAliasId, underlyingType = underlyingTypeWithProperNullability, // TODO replace arguments??? diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/typeAliasUtils.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/typeAliasUtils.kt index 2c0fdd118bb..986f51590ea 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/typeAliasUtils.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/core/typeAliasUtils.kt @@ -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 diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/metadata/CirTypeAliasExpander.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/metadata/CirTypeAliasExpander.kt index b1491bf3ef6..cf2dd22a9c5 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/metadata/CirTypeAliasExpander.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/metadata/CirTypeAliasExpander.kt @@ -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() ) } } diff --git a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/metadata/entityBuilders.kt b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/metadata/entityBuilders.kt index ea93b99a48c..6cef873b7bd 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/metadata/entityBuilders.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/descriptors/commonizer/metadata/entityBuilders.kt @@ -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 diff --git a/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/utils/mocks.kt b/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/utils/mocks.kt index dd8b86598d1..b9510cab17c 100644 --- a/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/utils/mocks.kt +++ b/native/commonizer/tests/org/jetbrains/kotlin/descriptors/commonizer/utils/mocks.kt @@ -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 )