From f2572975058c1cd95f3bd16be4fda0951b4b39bd Mon Sep 17 00:00:00 2001 From: Pavel Kirpichenkov Date: Mon, 7 Feb 2022 18:14:25 +0300 Subject: [PATCH] [MPP] Commonize to platform integers considering target's bit width Commonized type should match the platform's `platform.posix.ssize_t` type alias' width. The implementation hard-codes platform widths for all the targets, this can be potentially changed to search for ssize_t in dependencies and get width from its type. Use real targets in platform int commonization tests. KT-41509 --- .../core/ClassOrTypeAliasTypeCommonizer.kt | 15 +- .../core/PlatformIntegerCommonizer.kt | 252 ++++++++---- .../mergedtree/PlatformWidthIndex.kt | 58 +++ .../kotlin/commonizer/PlatformBitWidthTest.kt | 104 +++++ ...rchicalPlatformIntegerCommonizationTest.kt | 377 ++++++++++++++---- .../konan/PlatformIntegerClassIds.kt | 4 +- 6 files changed, 640 insertions(+), 170 deletions(-) create mode 100644 native/commonizer/src/org/jetbrains/kotlin/commonizer/mergedtree/PlatformWidthIndex.kt create mode 100644 native/commonizer/tests/org/jetbrains/kotlin/commonizer/PlatformBitWidthTest.kt diff --git a/native/commonizer/src/org/jetbrains/kotlin/commonizer/core/ClassOrTypeAliasTypeCommonizer.kt b/native/commonizer/src/org/jetbrains/kotlin/commonizer/core/ClassOrTypeAliasTypeCommonizer.kt index 822a3ec5db7..ca2661aa859 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/commonizer/core/ClassOrTypeAliasTypeCommonizer.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/commonizer/core/ClassOrTypeAliasTypeCommonizer.kt @@ -30,7 +30,7 @@ internal class ClassOrTypeAliasTypeCommonizer( ) private val isMarkedNullableCommonizer = TypeNullabilityCommonizer(typeCommonizer.context) - private val platformIntegerCommonizer = PlatformIntegerCommonizer(typeCommonizer) + private val platformIntegerCommonizer = PlatformIntegerCommonizer(typeCommonizer, classifiers) private val typeDistanceMeasurement = TypeDistanceMeasurement(typeCommonizer.context) override fun invoke(values: List): CirClassOrTypeAliasType? { @@ -39,11 +39,16 @@ internal class ClassOrTypeAliasTypeCommonizer( val isMarkedNullable = isMarkedNullableCommonizer.commonize(expansions.map { it.isMarkedNullable }) ?: return null val substitutedTypes = substituteTypesIfNecessary(values) - ?: isPlatformIntegerCommonizationEnabled.ifTrue { - return platformIntegerCommonizer.commonize(expansions)?.makeNullableIfNecessary(isMarkedNullable) + + if (substitutedTypes == null) { + val integerCommonizationResultIfApplicable = isPlatformIntegerCommonizationEnabled.ifTrue { + platformIntegerCommonizer(expansions)?.makeNullableIfNecessary(isMarkedNullable) } ?: isOptimisticNumberTypeCommonizationEnabled.ifTrue { - return OptimisticNumbersTypeCommonizer.commonize(expansions)?.makeNullableIfNecessary(isMarkedNullable) - } ?: return null + OptimisticNumbersTypeCommonizer.commonize(expansions)?.makeNullableIfNecessary(isMarkedNullable) + } + + return integerCommonizationResultIfApplicable + } val classifierId = substitutedTypes.singleDistinctValueOrNull { it.classifierId } ?: return null diff --git a/native/commonizer/src/org/jetbrains/kotlin/commonizer/core/PlatformIntegerCommonizer.kt b/native/commonizer/src/org/jetbrains/kotlin/commonizer/core/PlatformIntegerCommonizer.kt index d2a95baa5d9..c7af5739212 100644 --- a/native/commonizer/src/org/jetbrains/kotlin/commonizer/core/PlatformIntegerCommonizer.kt +++ b/native/commonizer/src/org/jetbrains/kotlin/commonizer/core/PlatformIntegerCommonizer.kt @@ -5,113 +5,213 @@ package org.jetbrains.kotlin.commonizer.core +import org.jetbrains.kotlin.commonizer.CommonizerTarget import org.jetbrains.kotlin.commonizer.cir.* +import org.jetbrains.kotlin.commonizer.mergedtree.CirKnownClassifiers +import org.jetbrains.kotlin.commonizer.mergedtree.PlatformIntWidth +import org.jetbrains.kotlin.commonizer.mergedtree.PlatformWidthIndex import org.jetbrains.kotlin.descriptors.konan.* import org.jetbrains.kotlin.name.ClassId import org.jetbrains.kotlin.utils.SmartList class PlatformIntegerCommonizer( - private val typeArgumentListCommonizer: TypeArgumentListCommonizer -) : AssociativeCommonizer { - constructor(typeCommonizer: TypeCommonizer) : this(TypeArgumentListCommonizer(typeCommonizer)) + typeArgumentListCommonizer: TypeArgumentListCommonizer, + classifiers: CirKnownClassifiers, +) : NullableSingleInvocationCommonizer { + constructor(typeCommonizer: TypeCommonizer, classifiers: CirKnownClassifiers) + : this(TypeArgumentListCommonizer(typeCommonizer), classifiers) - override fun commonize(first: CirClassOrTypeAliasType, second: CirClassOrTypeAliasType): CirClassOrTypeAliasType? { - return when { - both(first, second) { it.classifierId in commonizableSignedIntegerIds } -> platformIntType - both(first, second) { it.classifierId in commonizableUnsignedIntegerIds } -> platformUIntType - both(first, second) { it.classifierId in commonizableSignedVarIds } -> commonizeVarOf(first, second, isSigned = true) - both(first, second) { it.classifierId in commonizableUnsignedVarIds } -> commonizeVarOf(first, second, isSigned = false) - both(first, second) { it.classifierId in commonizableSignedArrayIds } -> platformIntArrayType - both(first, second) { it.classifierId in commonizableUnsignedArrayIds } -> platformUIntArrayType - both(first, second) { it.classifierId in commonizableSignedRangeIds } -> platformIntRangeType - both(first, second) { it.classifierId in commonizableUnsignedRangeIds } -> platformUIntRangeType - both(first, second) { it.classifierId in commonizableSignedProgressionIds } -> platformIntProgressionType - both(first, second) { it.classifierId in commonizableUnsignedProgressionIds } -> platformUIntProgressionType - else -> null + override fun invoke(values: List): CirClassOrTypeAliasType? { + return platformDependentTypeCommonizers.firstNotNullOfOrNull { commonizer -> + commonizer.invoke(values) } } - - private inline fun both(first: T, second: T, predicate: (T) -> Boolean) = - predicate(first) && predicate(second) - private fun commonizeVarOf( - first: CirClassOrTypeAliasType, - second: CirClassOrTypeAliasType, - isSigned: Boolean - ): CirClassOrTypeAliasType? { - if (first !is CirClassType || second !is CirClassType) return null + private val platformDependentTypeCommonizers = listOf( + PlatformIntCommonizer(classifiers), + PlatformUIntCommonizer(classifiers), + PlatformIntArrayCommonizer(classifiers), + PlatformUIntArrayCommonizer(classifiers), + PlatformIntRangeCommonizer(classifiers), + PlatformUIntRangeCommonizer(classifiers), + PlatformIntProgressionCommonizer(classifiers), + PlatformUIntProgressionCommonizer(classifiers), + PlatformIntVarOfCommonizer(classifiers, typeArgumentListCommonizer), + PlatformUIntVarOfCommonizer(classifiers, typeArgumentListCommonizer), + ) +} - val argument = typeArgumentListCommonizer.commonize(listOf(first.arguments, second.arguments))?.singleOrNull() +private sealed class PlatformDependentTypeCommonizer( + private val classifiers: CirKnownClassifiers, + private val intPlatformId: CirEntityId, + private val longPlatformId: CirEntityId, + private val mixedPlatformId: CirEntityId, +) : NullableSingleInvocationCommonizer { + + protected abstract fun doCommonize(values: List): CirClassOrTypeAliasType? + + override fun invoke(values: List): CirClassOrTypeAliasType? { + val typesToCommonizeWithTargets = values.zip(classifiers.classifierIndices.targets) + if (typesToCommonizeWithTargets.any { (type, target) -> !inputTypeIsKnownAndMatchesPlatformBitWidth(type, target) }) return null + + return doCommonize(values) + } + + private fun inputTypeIsKnownAndMatchesPlatformBitWidth(type: CirClassOrTypeAliasType, target: CommonizerTarget): Boolean = + when (PlatformWidthIndex.platformWidthOf(target)) { + PlatformIntWidth.INT -> type.classifierId == intPlatformId + PlatformIntWidth.LONG -> type.classifierId == longPlatformId + PlatformIntWidth.MIXED -> type.classifierId == mixedPlatformId + null -> false + } +} + +private abstract class PlatformDependentTypeWithoutTypeArgumentCommonizer( + classifiers: CirKnownClassifiers, + intPlatformId: CirEntityId, + longPlatformId: CirEntityId, + mixedPlatformId: CirEntityId, + private val resultingType: CirClassType, +) : PlatformDependentTypeCommonizer(classifiers, intPlatformId, longPlatformId, mixedPlatformId) { + + override fun doCommonize(values: List): CirClassOrTypeAliasType? = + resultingType +} + +private abstract class PlatformDependentTypeWithSingleArgumentCommonizer( + classifiers: CirKnownClassifiers, + private val typeArgumentListCommonizer: TypeArgumentListCommonizer, + intPlatformId: CirEntityId, + longPlatformId: CirEntityId, + private val mixedPlatformId: CirEntityId, +) : PlatformDependentTypeCommonizer(classifiers, intPlatformId, longPlatformId, mixedPlatformId) { + + override fun doCommonize(values: List): CirClassOrTypeAliasType? { + val commonTypeArgument = typeArgumentListCommonizer.commonize(values.map { it.arguments })?.singleOrNull() ?: return null - return createCommonVarOfType(isSigned = isSigned, argument = argument) + return createCirTypeWithOneArgument(entityId = mixedPlatformId, argument = commonTypeArgument) } } -// commonizable groups -private val commonizableSignedIntegerIds: Set = listOf( - KOTLIN_INT_ID, KOTLIN_LONG_ID, PLATFORM_INT_ID -).toCirEntityIds() +private class PlatformIntCommonizer( + classifiers: CirKnownClassifiers, +) : PlatformDependentTypeWithoutTypeArgumentCommonizer( + classifiers, + intPlatformId = KOTLIN_INT_ID.toCirEntityId(), + longPlatformId = KOTLIN_LONG_ID.toCirEntityId(), + mixedPlatformId = PLATFORM_INT_ID.toCirEntityId(), + resultingType = platformIntType, +) -private val commonizableUnsignedIntegerIds: Set = listOf( - KOTLIN_UINT_ID, KOTLIN_ULONG_ID, PLATFORM_UINT_ID -).toCirEntityIds() +private class PlatformUIntCommonizer( + classifiers: CirKnownClassifiers, +) : PlatformDependentTypeWithoutTypeArgumentCommonizer( + classifiers, + intPlatformId = KOTLIN_UINT_ID.toCirEntityId(), + longPlatformId = KOTLIN_ULONG_ID.toCirEntityId(), + mixedPlatformId = PLATFORM_UINT_ID.toCirEntityId(), + resultingType = platformUIntType, +) -private val commonizableSignedVarIds: Set = listOf( - INT_VAR_OF_ID, LONG_VAR_OF_ID, PLATFORM_INT_VAR_OF_ID -).toCirEntityIds() +private class PlatformIntArrayCommonizer( + classifiers: CirKnownClassifiers, +) : PlatformDependentTypeWithoutTypeArgumentCommonizer( + classifiers = classifiers, + intPlatformId = INT_ARRAY_ID.toCirEntityId(), + longPlatformId = LONG_ARRAY_ID.toCirEntityId(), + mixedPlatformId = PLATFORM_INT_ARRAY_ID.toCirEntityId(), + resultingType = platformIntArrayType, +) -private val commonizableUnsignedVarIds: Set = listOf( - UINT_VAR_OF_ID, ULONG_VAR_OF_ID, PLATFORM_UINT_VAR_OF_ID -).toCirEntityIds() +private class PlatformUIntArrayCommonizer( + classifiers: CirKnownClassifiers, +) : PlatformDependentTypeWithoutTypeArgumentCommonizer( + classifiers, + intPlatformId = UINT_ARRAY_ID.toCirEntityId(), + longPlatformId = ULONG_ARRAY_ID.toCirEntityId(), + mixedPlatformId = PLATFORM_UINT_ARRAY_ID.toCirEntityId(), + resultingType = platformUIntArrayType, +) -private val commonizableSignedArrayIds: Set = listOf( - INT_ARRAY_ID, LONG_ARRAY_ID, PLATFORM_INT_ARRAY_ID -).toCirEntityIds() +private class PlatformIntRangeCommonizer( + classifiers: CirKnownClassifiers, +) : PlatformDependentTypeWithoutTypeArgumentCommonizer( + classifiers, + intPlatformId = INT_RANGE_ID.toCirEntityId(), + longPlatformId = LONG_RANGE_ID.toCirEntityId(), + mixedPlatformId = PLATFORM_INT_RANGE_ID.toCirEntityId(), + resultingType = platformIntRangeType, +) -private val commonizableUnsignedArrayIds: Set = listOf( - UINT_ARRAY_ID, ULONG_ARRAY_ID, PLATFORM_UINT_ARRAY_ID -).toCirEntityIds() +private class PlatformUIntRangeCommonizer( + classifiers: CirKnownClassifiers, +) : PlatformDependentTypeWithoutTypeArgumentCommonizer( + classifiers, + intPlatformId = UINT_RANGE_ID.toCirEntityId(), + longPlatformId = ULONG_RANGE_ID.toCirEntityId(), + mixedPlatformId = PLATFORM_UINT_RANGE_ID.toCirEntityId(), + resultingType = platformUIntRangeType, +) -private val commonizableSignedRangeIds: Set = listOf( - INT_RANGE_ID, LONG_RANGE_ID, PLATFORM_INT_RANGE_ID -).toCirEntityIds() +private class PlatformIntProgressionCommonizer( + classifiers: CirKnownClassifiers, +) : PlatformDependentTypeWithoutTypeArgumentCommonizer( + classifiers, + intPlatformId = INT_PROGRESSION_ID.toCirEntityId(), + longPlatformId = LONG_PROGRESSION_ID.toCirEntityId(), + mixedPlatformId = PLATFORM_INT_PROGRESSION_ID.toCirEntityId(), + resultingType = platformIntProgressionType, +) -private val commonizableUnsignedRangeIds: Set = listOf( - UINT_RANGE_ID, ULONG_RANGE_ID, PLATFORM_UINT_RANGE_ID -).toCirEntityIds() +private class PlatformUIntProgressionCommonizer( + classifiers: CirKnownClassifiers, +) : PlatformDependentTypeWithoutTypeArgumentCommonizer( + classifiers, + intPlatformId = UINT_PROGRESSION_ID.toCirEntityId(), + longPlatformId = ULONG_PROGRESSION_ID.toCirEntityId(), + mixedPlatformId = PLATFORM_UINT_PROGRESSION_ID.toCirEntityId(), + resultingType = platformUIntProgressionType, +) -private val commonizableSignedProgressionIds: Set = listOf( - INT_PROGRESSION_ID, LONG_PROGRESSION_ID, PLATFORM_INT_PROGRESSION_ID -).toCirEntityIds() +private class PlatformIntVarOfCommonizer( + classifiers: CirKnownClassifiers, + typeArgumentListCommonizer: TypeArgumentListCommonizer, +) : PlatformDependentTypeWithSingleArgumentCommonizer( + classifiers, + typeArgumentListCommonizer, + intPlatformId = INT_VAR_OF_ID.toCirEntityId(), + longPlatformId = LONG_VAR_OF_ID.toCirEntityId(), + mixedPlatformId = PLATFORM_INT_VAR_OF_ID.toCirEntityId(), +) -private val commonizableUnsignedProgressionIds: Set = listOf( - UINT_PROGRESSION, ULONG_PROGRESSION, PLATFORM_UINT_PROGRESSION_ID -).toCirEntityIds() +private class PlatformUIntVarOfCommonizer( + classifiers: CirKnownClassifiers, + typeArgumentListCommonizer: TypeArgumentListCommonizer, +) : PlatformDependentTypeWithSingleArgumentCommonizer( + classifiers, + typeArgumentListCommonizer, + intPlatformId = UINT_VAR_OF_ID.toCirEntityId(), + longPlatformId = ULONG_VAR_OF_ID.toCirEntityId(), + mixedPlatformId = PLATFORM_UINT_VAR_OF_ID.toCirEntityId(), +) private fun ClassId.toCirEntityId(): CirEntityId = CirEntityId.create(this) -private fun Collection.toCirEntityIds(): Set = - map { it.toCirEntityId()}.toSet() +private val platformIntType: CirClassType = createCirTypeWithoutArguments(PLATFORM_INT_ID.toCirEntityId()) +private val platformUIntType: CirClassType = createCirTypeWithoutArguments(PLATFORM_UINT_ID.toCirEntityId()) -// plain integers -private val platformIntType: CirClassType = createSimpleCirTypeWithoutArguments(PLATFORM_INT_ID.toCirEntityId()) -private val platformUIntType: CirClassType = createSimpleCirTypeWithoutArguments(PLATFORM_UINT_ID.toCirEntityId()) +private val platformIntArrayType: CirClassType = createCirTypeWithoutArguments(PLATFORM_INT_ARRAY_ID.toCirEntityId()) +private val platformUIntArrayType: CirClassType = createCirTypeWithoutArguments(PLATFORM_UINT_ARRAY_ID.toCirEntityId()) -// arrays -private val platformIntArrayType: CirClassType = createSimpleCirTypeWithoutArguments(PLATFORM_INT_ARRAY_ID.toCirEntityId()) -private val platformUIntArrayType: CirClassType = createSimpleCirTypeWithoutArguments(PLATFORM_UINT_ARRAY_ID.toCirEntityId()) +private val platformIntRangeType: CirClassType = createCirTypeWithoutArguments(PLATFORM_INT_RANGE_ID.toCirEntityId()) +private val platformUIntRangeType: CirClassType = createCirTypeWithoutArguments(PLATFORM_UINT_RANGE_ID.toCirEntityId()) -// ranges -private val platformIntRangeType: CirClassType = createSimpleCirTypeWithoutArguments(PLATFORM_INT_RANGE_ID.toCirEntityId()) -private val platformUIntRangeType: CirClassType = createSimpleCirTypeWithoutArguments(PLATFORM_UINT_RANGE_ID.toCirEntityId()) +private val platformIntProgressionType: CirClassType = createCirTypeWithoutArguments(PLATFORM_INT_PROGRESSION_ID.toCirEntityId()) +private val platformUIntProgressionType: CirClassType = createCirTypeWithoutArguments(PLATFORM_UINT_PROGRESSION_ID.toCirEntityId()) -// progressions -private val platformIntProgressionType: CirClassType = createSimpleCirTypeWithoutArguments(PLATFORM_INT_PROGRESSION_ID.toCirEntityId()) -private val platformUIntProgressionType: CirClassType = createSimpleCirTypeWithoutArguments(PLATFORM_UINT_PROGRESSION_ID.toCirEntityId()) - -private fun createSimpleCirTypeWithoutArguments(id: CirEntityId): CirClassType = +private fun createCirTypeWithoutArguments(id: CirEntityId): CirClassType = CirClassType.createInterned( classId = id, outerType = null, @@ -119,9 +219,9 @@ private fun createSimpleCirTypeWithoutArguments(id: CirEntityId): CirClassType = isMarkedNullable = false, ) -private fun createCommonVarOfType(isSigned: Boolean, argument: CirTypeProjection): CirClassType { +private fun createCirTypeWithOneArgument(entityId: CirEntityId, argument: CirTypeProjection): CirClassType { return CirClassType.createInterned( - classId = if (isSigned) PLATFORM_INT_VAR_OF_ID.toCirEntityId() else PLATFORM_UINT_VAR_OF_ID.toCirEntityId(), + classId = entityId, outerType = null, arguments = SmartList(argument), isMarkedNullable = false, diff --git a/native/commonizer/src/org/jetbrains/kotlin/commonizer/mergedtree/PlatformWidthIndex.kt b/native/commonizer/src/org/jetbrains/kotlin/commonizer/mergedtree/PlatformWidthIndex.kt new file mode 100644 index 00000000000..05f5b15ecad --- /dev/null +++ b/native/commonizer/src/org/jetbrains/kotlin/commonizer/mergedtree/PlatformWidthIndex.kt @@ -0,0 +1,58 @@ +/* + * Copyright 2010-2022 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.commonizer.mergedtree + +import org.jetbrains.kotlin.commonizer.CommonizerTarget +import org.jetbrains.kotlin.commonizer.LeafCommonizerTarget +import org.jetbrains.kotlin.commonizer.SharedCommonizerTarget +import org.jetbrains.kotlin.commonizer.allLeaves +import org.jetbrains.kotlin.commonizer.utils.singleDistinctValueOrNull +import org.jetbrains.kotlin.konan.target.KonanTarget +import org.jetbrains.kotlin.utils.addToStdlib.ifTrue + +enum class PlatformIntWidth { + INT, LONG, MIXED +} + +object PlatformWidthIndex { + private val widthByLeafTargets = mapOf( + LeafCommonizerTarget(KonanTarget.IOS_ARM32) to PlatformIntWidth.INT, + LeafCommonizerTarget(KonanTarget.IOS_ARM64) to PlatformIntWidth.LONG, + LeafCommonizerTarget(KonanTarget.IOS_X64) to PlatformIntWidth.LONG, + LeafCommonizerTarget(KonanTarget.IOS_SIMULATOR_ARM64) to PlatformIntWidth.LONG, + LeafCommonizerTarget(KonanTarget.WATCHOS_ARM32) to PlatformIntWidth.INT, + LeafCommonizerTarget(KonanTarget.WATCHOS_ARM64) to PlatformIntWidth.INT, + LeafCommonizerTarget(KonanTarget.WATCHOS_X86) to PlatformIntWidth.INT, + LeafCommonizerTarget(KonanTarget.WATCHOS_X64) to PlatformIntWidth.LONG, + LeafCommonizerTarget(KonanTarget.WATCHOS_SIMULATOR_ARM64) to PlatformIntWidth.LONG, + LeafCommonizerTarget(KonanTarget.TVOS_ARM64) to PlatformIntWidth.LONG, + LeafCommonizerTarget(KonanTarget.TVOS_X64) to PlatformIntWidth.LONG, + LeafCommonizerTarget(KonanTarget.TVOS_SIMULATOR_ARM64) to PlatformIntWidth.LONG, + LeafCommonizerTarget(KonanTarget.LINUX_X64) to PlatformIntWidth.LONG, + LeafCommonizerTarget(KonanTarget.MINGW_X86) to PlatformIntWidth.INT, + LeafCommonizerTarget(KonanTarget.MINGW_X64) to PlatformIntWidth.LONG, + LeafCommonizerTarget(KonanTarget.MACOS_X64) to PlatformIntWidth.LONG, + LeafCommonizerTarget(KonanTarget.MACOS_ARM64) to PlatformIntWidth.LONG, + LeafCommonizerTarget(KonanTarget.LINUX_ARM64) to PlatformIntWidth.LONG, + LeafCommonizerTarget(KonanTarget.LINUX_ARM32_HFP) to PlatformIntWidth.INT, + LeafCommonizerTarget(KonanTarget.LINUX_MIPS32) to PlatformIntWidth.INT, + LeafCommonizerTarget(KonanTarget.LINUX_MIPSEL32) to PlatformIntWidth.INT, + LeafCommonizerTarget(KonanTarget.WASM32) to PlatformIntWidth.INT, + ) + + fun platformWidthOf(target: CommonizerTarget): PlatformIntWidth? { + return when (target) { + is LeafCommonizerTarget -> widthByLeafTargets[target] + is SharedCommonizerTarget -> target.allLeaves().toList().let { leafTargets -> + val sameForAllLeaves = leafTargets.singleDistinctValueOrNull { platformWidthOf(it) } + if (sameForAllLeaves != null) return@let sameForAllLeaves + + leafTargets.all { platformWidthOf(it) != null }.ifTrue { PlatformIntWidth.MIXED } + } + } + } +} + diff --git a/native/commonizer/tests/org/jetbrains/kotlin/commonizer/PlatformBitWidthTest.kt b/native/commonizer/tests/org/jetbrains/kotlin/commonizer/PlatformBitWidthTest.kt new file mode 100644 index 00000000000..ff23cad637c --- /dev/null +++ b/native/commonizer/tests/org/jetbrains/kotlin/commonizer/PlatformBitWidthTest.kt @@ -0,0 +1,104 @@ +/* + * Copyright 2010-2022 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.commonizer + +import org.jetbrains.kotlin.commonizer.mergedtree.PlatformIntWidth +import org.jetbrains.kotlin.commonizer.mergedtree.PlatformWidthIndex +import org.jetbrains.kotlin.konan.target.KonanTarget +import org.junit.Test +import kotlin.test.assertEquals + +class PlatformBitWidthTest { + private fun createPlatformBitWidthIndex(): PlatformWidthIndex = PlatformWidthIndex + + @Test + fun `test leaf int`() { + assertEquals( + PlatformIntWidth.INT, + createPlatformBitWidthIndex().platformWidthOf(CommonizerTarget(KonanTarget.IOS_ARM32)) + ) + } + + @Test + fun `test leaf long`() { + assertEquals( + PlatformIntWidth.LONG, + createPlatformBitWidthIndex().platformWidthOf(CommonizerTarget(KonanTarget.MACOS_X64)) + ) + } + + @Test + fun `test shared int`() { + assertEquals( + PlatformIntWidth.INT, + createPlatformBitWidthIndex().platformWidthOf(CommonizerTarget(KonanTarget.IOS_ARM32, KonanTarget.LINUX_MIPS32)) + ) + } + + @Test + fun `test shared long`() { + assertEquals( + PlatformIntWidth.LONG, + createPlatformBitWidthIndex().platformWidthOf(CommonizerTarget(KonanTarget.MACOS_X64, KonanTarget.LINUX_X64)) + ) + } + + @Test + fun `test int and long`() { + assertEquals( + PlatformIntWidth.MIXED, + createPlatformBitWidthIndex().platformWidthOf(CommonizerTarget(KonanTarget.IOS_ARM32, KonanTarget.MACOS_X64)) + ) + } + + @Test + fun `test int and unknown`() { + assertEquals( + null, + createPlatformBitWidthIndex().platformWidthOf( + CommonizerTarget( + LeafCommonizerTarget(KonanTarget.IOS_ARM32), + LeafCommonizerTarget("unknown_target") + ) + ) + ) + } + + @Test + fun `test long and unknown`() { + assertEquals( + null, + createPlatformBitWidthIndex().platformWidthOf( + CommonizerTarget( + LeafCommonizerTarget(KonanTarget.MACOS_X64), + LeafCommonizerTarget("unknown_target") + ) + ) + ) + } + + @Test + fun `test the good, the bad and the ugly`() { + assertEquals( + null, + createPlatformBitWidthIndex().platformWidthOf( + CommonizerTarget( + LeafCommonizerTarget(KonanTarget.MACOS_X64), + LeafCommonizerTarget(KonanTarget.IOS_ARM32), + LeafCommonizerTarget("unknown_target"), + ) + ) + ) + } + + @Test + fun `test watchosArm64 is considered int`() { + assertEquals( + PlatformIntWidth.INT, + createPlatformBitWidthIndex().platformWidthOf(CommonizerTarget(KonanTarget.WATCHOS_ARM64)) + ) + } +} diff --git a/native/commonizer/tests/org/jetbrains/kotlin/commonizer/hierarchical/HierarchicalPlatformIntegerCommonizationTest.kt b/native/commonizer/tests/org/jetbrains/kotlin/commonizer/hierarchical/HierarchicalPlatformIntegerCommonizationTest.kt index b1ac6c18acd..de8dcfdd92c 100644 --- a/native/commonizer/tests/org/jetbrains/kotlin/commonizer/hierarchical/HierarchicalPlatformIntegerCommonizationTest.kt +++ b/native/commonizer/tests/org/jetbrains/kotlin/commonizer/hierarchical/HierarchicalPlatformIntegerCommonizationTest.kt @@ -6,32 +6,61 @@ package org.jetbrains.kotlin.commonizer.hierarchical import org.jetbrains.kotlin.commonizer.AbstractInlineSourcesCommonizationTest +import org.jetbrains.kotlin.commonizer.OptimisticNumberCommonizationEnabledKey import org.jetbrains.kotlin.commonizer.PlatformIntegerCommonizationEnabledKey import org.jetbrains.kotlin.commonizer.assertCommonized +import org.jetbrains.kotlin.konan.target.KonanTarget.* class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommonizationTest() { - fun `test signed ints`() { + fun `test signed ints without optimistic commonization`() { val result = commonize { - outputTarget("(a, b)") + outputTarget("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") setting(PlatformIntegerCommonizationEnabledKey, true) - registerFakeStdlibIntegersDependency("(a, b)") + setting(OptimisticNumberCommonizationEnabledKey, false) + registerFakeStdlibIntegersDependency("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") - "a" withSource """ + LINUX_ARM64.name withSource """ typealias X = Int typealias Y = Long """.trimIndent() - "b" withSource """ + LINUX_ARM32_HFP.name withSource """ typealias X = Long typealias Y = Int """.trimIndent() } result.assertCommonized( - "(a, b)", """ - @UnsafeNumber(["a: kotlin.Int", "b: kotlin.Long"]) - typealias X = PlatformInt - @UnsafeNumber(["a: kotlin.Long", "b: kotlin.Int"]) + "(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})", """ + expect class X : Number + typealias Y = PlatformInt + """.trimIndent() + ) + } + + fun `test signed ints with optimistic commonization backup`() { + val result = commonize { + outputTarget("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") + setting(PlatformIntegerCommonizationEnabledKey, true) + setting(OptimisticNumberCommonizationEnabledKey, true) + registerFakeStdlibIntegersDependency("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") + + LINUX_ARM64.name withSource """ + typealias X = Int + typealias Y = Long + """.trimIndent() + + LINUX_ARM32_HFP.name withSource """ + typealias X = Long + typealias Y = Int + """.trimIndent() + } + + result.assertCommonized( + "(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})", """ + @UnsafeNumber(["${LINUX_ARM32_HFP.name}: kotlin.Long", "${LINUX_ARM64.name}: kotlin.Int"]) + typealias X = Int + @UnsafeNumber(["${LINUX_ARM32_HFP.name}: kotlin.Int", "${LINUX_ARM64.name}: kotlin.Long"]) typealias Y = PlatformInt """.trimIndent() ) @@ -39,26 +68,26 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon fun `test unsigned ints`() { val result = commonize { - outputTarget("(a, b)") + outputTarget("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") setting(PlatformIntegerCommonizationEnabledKey, true) - registerFakeStdlibIntegersDependency("(a, b)") + registerFakeStdlibIntegersDependency("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") - "a" withSource """ + LINUX_ARM64.name withSource """ typealias X = UInt typealias Y = ULong """.trimIndent() - "b" withSource """ + LINUX_ARM32_HFP.name withSource """ typealias X = ULong typealias Y = UInt """.trimIndent() } result.assertCommonized( - "(a, b)", """ - @UnsafeNumber(["a: kotlin.UInt", "b: kotlin.ULong"]) - typealias X = PlatformUInt - @UnsafeNumber(["a: kotlin.ULong", "b: kotlin.UInt"]) + "(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})", """ + @UnsafeNumber(["${LINUX_ARM32_HFP.name}: kotlin.ULong", "${LINUX_ARM64.name}: kotlin.UInt"]) + typealias X = UInt + @UnsafeNumber(["${LINUX_ARM32_HFP.name}: kotlin.UInt", "${LINUX_ARM64.name}: kotlin.ULong"]) typealias Y = PlatformUInt """.trimIndent() ) @@ -66,11 +95,11 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon fun `test signed vars`() { val result = commonize { - outputTarget("(a, b)") + outputTarget("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") setting(PlatformIntegerCommonizationEnabledKey, true) - registerFakeStdlibIntegersDependency("(a, b)") + registerFakeStdlibIntegersDependency("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") - "a" withSource """ + LINUX_ARM64.name withSource """ import kotlinx.cinterop.* typealias AX = Int @@ -79,7 +108,7 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon typealias Y = LongVarOf """.trimIndent() - "b" withSource """ + LINUX_ARM32_HFP.name withSource """ import kotlinx.cinterop.* typealias AX = Long @@ -90,16 +119,16 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon } result.assertCommonized( - "(a, b)", """ + "(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})", """ import kotlinx.cinterop.* - @UnsafeNumber(["a: kotlin.Int", "b: kotlin.Long"]) - typealias AX = PlatformInt - @UnsafeNumber(["a: kotlin.Long", "b: kotlin.Int"]) + @UnsafeNumber(["${LINUX_ARM32_HFP.name}: kotlin.Long", "${LINUX_ARM64.name}: kotlin.Int"]) + typealias AX = Int + @UnsafeNumber(["${LINUX_ARM32_HFP.name}: kotlin.Int", "${LINUX_ARM64.name}: kotlin.Long"]) typealias AY = PlatformInt - @UnsafeNumber(["a: kotlinx.cinterop.IntVarOf", "b: kotlinx.cinterop.LongVarOf"]) - typealias X = PlatformIntVarOf - @UnsafeNumber(["a: kotlinx.cinterop.LongVarOf", "b: kotlinx.cinterop.IntVarOf"]) + @UnsafeNumber(["${LINUX_ARM32_HFP.name}: kotlinx.cinterop.LongVarOf", "${LINUX_ARM64.name}: kotlinx.cinterop.IntVarOf"]) + typealias X = IntVarOf + @UnsafeNumber(["${LINUX_ARM32_HFP.name}: kotlinx.cinterop.IntVarOf", "${LINUX_ARM64.name}: kotlinx.cinterop.LongVarOf"]) typealias Y = PlatformIntVarOf """.trimIndent() ) @@ -107,11 +136,11 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon fun `test unsigned vars`() { val result = commonize { - outputTarget("(a, b)") + outputTarget("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") setting(PlatformIntegerCommonizationEnabledKey, true) - registerFakeStdlibIntegersDependency("(a, b)") + registerFakeStdlibIntegersDependency("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") - "a" withSource """ + LINUX_ARM64.name withSource """ import kotlinx.cinterop.* typealias AX = UInt @@ -120,7 +149,7 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon typealias Y = ULongVarOf """.trimIndent() - "b" withSource """ + LINUX_ARM32_HFP.name withSource """ import kotlinx.cinterop.* typealias AX = ULong @@ -131,16 +160,16 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon } result.assertCommonized( - "(a, b)", """ + "(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})", """ import kotlinx.cinterop.* - @UnsafeNumber(["a: kotlin.UInt", "b: kotlin.ULong"]) - typealias AX = PlatformUInt - @UnsafeNumber(["a: kotlin.ULong", "b: kotlin.UInt"]) + @UnsafeNumber(["${LINUX_ARM32_HFP.name}: kotlin.ULong", "${LINUX_ARM64.name}: kotlin.UInt"]) + typealias AX = UInt + @UnsafeNumber(["${LINUX_ARM32_HFP.name}: kotlin.UInt", "${LINUX_ARM64.name}: kotlin.ULong"]) typealias AY = PlatformUInt - @UnsafeNumber(["a: kotlinx.cinterop.UIntVarOf", "b: kotlinx.cinterop.ULongVarOf"]) - typealias X = PlatformUIntVarOf - @UnsafeNumber(["a: kotlinx.cinterop.ULongVarOf", "b: kotlinx.cinterop.UIntVarOf"]) + @UnsafeNumber(["${LINUX_ARM32_HFP.name}: kotlinx.cinterop.ULongVarOf", "${LINUX_ARM64.name}: kotlinx.cinterop.UIntVarOf"]) + typealias X = UIntVarOf + @UnsafeNumber(["${LINUX_ARM32_HFP.name}: kotlinx.cinterop.UIntVarOf", "${LINUX_ARM64.name}: kotlinx.cinterop.ULongVarOf"]) typealias Y = PlatformUIntVarOf """.trimIndent() ) @@ -148,26 +177,26 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon fun `test signed arrays`() { val result = commonize { - outputTarget("(a, b)") + outputTarget("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") setting(PlatformIntegerCommonizationEnabledKey, true) - registerFakeStdlibIntegersDependency("(a, b)") + registerFakeStdlibIntegersDependency("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") - "a" withSource """ + LINUX_ARM64.name withSource """ typealias X = IntArray typealias Y = LongArray """.trimIndent() - "b" withSource """ + LINUX_ARM32_HFP.name withSource """ typealias X = LongArray typealias Y = IntArray """.trimIndent() } result.assertCommonized( - "(a, b)", """ + "(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})", """ import kotlinx.cinterop.* - typealias X = PlatformIntArray + expect class X typealias Y = PlatformIntArray """.trimIndent() ) @@ -175,26 +204,26 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon fun `test unsigned arrays`() { val result = commonize { - outputTarget("(a, b)") + outputTarget("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") setting(PlatformIntegerCommonizationEnabledKey, true) - registerFakeStdlibIntegersDependency("(a, b)") + registerFakeStdlibIntegersDependency("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") - "a" withSource """ + LINUX_ARM64.name withSource """ typealias X = UIntArray typealias Y = ULongArray """.trimIndent() - "b" withSource """ + LINUX_ARM32_HFP.name withSource """ typealias X = ULongArray typealias Y = UIntArray """.trimIndent() } result.assertCommonized( - "(a, b)", """ + "(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})", """ import kotlinx.cinterop.* - typealias X = PlatformUIntArray + expect class X typealias Y = PlatformUIntArray """.trimIndent() ) @@ -202,28 +231,28 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon fun `test signed ranges`() { val result = commonize { - outputTarget("(a, b)") + outputTarget("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") setting(PlatformIntegerCommonizationEnabledKey, true) - registerFakeStdlibIntegersDependency("(a, b)") + registerFakeStdlibIntegersDependency("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") - "a" withSource """ + LINUX_ARM64.name withSource """ import kotlin.ranges.* typealias X = IntRange typealias Y = LongRange """.trimIndent() - "b" withSource """ + LINUX_ARM32_HFP.name withSource """ typealias X = LongRange typealias Y = IntRange """.trimIndent() } result.assertCommonized( - "(a, b)", """ + "(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})", """ import kotlin.ranges.* - typealias X = PlatformIntRange + expect class X typealias Y = PlatformIntRange """.trimIndent() ) @@ -231,28 +260,28 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon fun `test unsigned ranges`() { val result = commonize { - outputTarget("(a, b)") + outputTarget("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") setting(PlatformIntegerCommonizationEnabledKey, true) - registerFakeStdlibIntegersDependency("(a, b)") + registerFakeStdlibIntegersDependency("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") - "a" withSource """ + LINUX_ARM64.name withSource """ import kotlin.ranges.* typealias X = UIntRange typealias Y = ULongRange """.trimIndent() - "b" withSource """ + LINUX_ARM32_HFP.name withSource """ typealias X = ULongRange typealias Y = UIntRange """.trimIndent() } result.assertCommonized( - "(a, b)", """ + "(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})", """ import kotlin.ranges.* - typealias X = PlatformUIntRange + expect class X typealias Y = PlatformUIntRange """.trimIndent() ) @@ -260,28 +289,28 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon fun `test signed progressions`() { val result = commonize { - outputTarget("(a, b)") + outputTarget("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") setting(PlatformIntegerCommonizationEnabledKey, true) - registerFakeStdlibIntegersDependency("(a, b)") + registerFakeStdlibIntegersDependency("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") - "a" withSource """ + LINUX_ARM64.name withSource """ import kotlin.ranges.* typealias X = IntProgression typealias Y = LongProgression """.trimIndent() - "b" withSource """ + LINUX_ARM32_HFP.name withSource """ typealias X = LongProgression typealias Y = IntProgression """.trimIndent() } result.assertCommonized( - "(a, b)", """ + "(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})", """ import kotlin.ranges.* - typealias X = PlatformIntProgression + expect class X typealias Y = PlatformIntProgression """.trimIndent() ) @@ -289,28 +318,28 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon fun `test unsigned progressions`() { val result = commonize { - outputTarget("(a, b)") + outputTarget("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") setting(PlatformIntegerCommonizationEnabledKey, true) - registerFakeStdlibIntegersDependency("(a, b)") + registerFakeStdlibIntegersDependency("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") - "a" withSource """ + LINUX_ARM64.name withSource """ import kotlin.ranges.* typealias X = UIntProgression typealias Y = ULongProgression """.trimIndent() - "b" withSource """ + LINUX_ARM32_HFP.name withSource """ typealias X = ULongProgression typealias Y = UIntProgression """.trimIndent() } result.assertCommonized( - "(a, b)", """ + "(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})", """ import kotlin.ranges.* - typealias X = PlatformUIntProgression + expect class X typealias Y = PlatformUIntProgression """.trimIndent() ) @@ -318,11 +347,11 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon fun `test platform types in return positions`() { val result = commonize { - outputTarget("(a, b)") + outputTarget("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") setting(PlatformIntegerCommonizationEnabledKey, true) - registerFakeStdlibIntegersDependency("(a, b)") + registerFakeStdlibIntegersDependency("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") - "a" withSource """ + LINUX_ARM32_HFP.name withSource """ import kotlin.ranges.* import kotlinx.cinterop.* @@ -336,7 +365,7 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon fun p(): IntProgression = null!! """.trimIndent() - "b" withSource """ + LINUX_ARM64.name withSource """ import kotlin.ranges.* import kotlinx.cinterop.* @@ -352,12 +381,14 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon } result.assertCommonized( - "(a, b)", """ + "(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})", """ import kotlin.ranges.* import kotlinx.cinterop.* expect class C() { + @UnsafeNumber(["${LINUX_ARM32_HFP.name}: kotlin.Int", "${LINUX_ARM64.name}: kotlin.Long"]) val i: PlatformInt + @UnsafeNumber(["${LINUX_ARM32_HFP.name}: kotlinx.cinterop.IntVarOf", "${LINUX_ARM64.name}: kotlinx.cinterop.LongVarOf"]) fun v(): PlatformIntVarOf fun r(): PlatformIntRange } @@ -370,11 +401,11 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon fun `test platform types in signatures`() { val result = commonize { - outputTarget("(a, b)") + outputTarget("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") setting(PlatformIntegerCommonizationEnabledKey, true) - registerFakeStdlibIntegersDependency("(a, b)") + registerFakeStdlibIntegersDependency("(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})") - "a" withSource """ + LINUX_ARM64.name withSource """ import kotlin.ranges.* import kotlinx.cinterop.* @@ -388,7 +419,7 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon fun p(arg: IntProgression) {} """.trimIndent() - "b" withSource """ + LINUX_ARM32_HFP.name withSource """ import kotlin.ranges.* import kotlinx.cinterop.* @@ -404,12 +435,184 @@ class HierarchicalPlatformIntegerCommonizationTest : AbstractInlineSourcesCommon } result.assertCommonized( - "(a, b)", """ + "(${LINUX_ARM64.name}, ${LINUX_ARM32_HFP.name})", """ import kotlin.ranges.* import kotlinx.cinterop.* - expect class C() { - } + expect class C() + """.trimIndent() + ) + } + + fun `test platform integers in multi-target commonization`() { + val intTarget1 = LINUX_ARM32_HFP.name + val intTarget2 = LINUX_MIPS32.name + val intTarget3 = LINUX_MIPSEL32.name + val longTarget1 = LINUX_X64.name + val longTarget2 = LINUX_ARM64.name + val longTarget3 = MACOS_X64.name + + val outputCommonizerTargets = arrayOf( + "($intTarget1, $intTarget2)", "($longTarget1, $longTarget2)", "($intTarget3, $longTarget3)", + "($intTarget1, $intTarget2, $intTarget3)", "($longTarget1, $longTarget2, $longTarget3)", + "($intTarget1, $intTarget2, $intTarget3, $longTarget1)", + "($intTarget1, $intTarget2, $longTarget1, $longTarget2)", + "($longTarget1, $longTarget2, $longTarget3, $intTarget1)", + "($intTarget1, $intTarget2, $intTarget3, $longTarget1, $longTarget2, $longTarget3)", + ) + + val result = commonize { + setting(PlatformIntegerCommonizationEnabledKey, true) + outputTarget(*outputCommonizerTargets) + registerFakeStdlibIntegersDependency(*outputCommonizerTargets) + + intTarget1 withSource """ + import kotlinx.cinterop.* + + typealias X = Int + typealias XV = IntVarOf + """.trimIndent() + + intTarget2 withSource """ + import kotlinx.cinterop.* + + typealias X = Int + typealias XV = IntVarOf + """.trimIndent() + + intTarget3 withSource """ + import kotlinx.cinterop.* + + typealias X = Int + typealias XV = IntVarOf + """.trimIndent() + + longTarget1 withSource """ + import kotlinx.cinterop.* + + typealias X = Long + typealias XV = LongVarOf + """.trimIndent() + + longTarget2 withSource """ + import kotlinx.cinterop.* + + typealias X = Long + typealias XV = LongVarOf + """.trimIndent() + + longTarget3 withSource """ + import kotlinx.cinterop.* + + typealias X = Long + typealias XV = LongVarOf + """.trimIndent() + } + + result.assertCommonized( + "($intTarget1, $intTarget2)", """ + import kotlinx.cinterop.* + + typealias X = Int + typealias XV = IntVarOf + """.trimIndent() + ) + + result.assertCommonized( + "($intTarget1, $intTarget2, $intTarget3)", """ + import kotlinx.cinterop.* + + typealias X = Int + typealias XV = IntVarOf + """.trimIndent() + ) + + result.assertCommonized( + "($longTarget1, $longTarget2)", """ + import kotlinx.cinterop.* + + typealias X = Long + typealias XV = LongVarOf + """.trimIndent() + ) + + result.assertCommonized( + "($longTarget1, $longTarget2, $longTarget3)", """ + import kotlinx.cinterop.* + + typealias X = Long + typealias XV = LongVarOf + """.trimIndent() + ) + + result.assertCommonized( + "($intTarget3, $longTarget3)", """ + import kotlinx.cinterop.* + + @UnsafeNumber(["${LINUX_MIPSEL32.name}: kotlin.Int", "${MACOS_X64.name}: kotlin.Long"]) + typealias X = PlatformInt + @UnsafeNumber(["${LINUX_MIPSEL32.name}: kotlinx.cinterop.IntVarOf", "${MACOS_X64.name}: kotlinx.cinterop.LongVarOf"]) + typealias XV = PlatformIntVarOf + """.trimIndent() + ) + + result.assertCommonized( + "($intTarget1, $intTarget2, $longTarget1, $longTarget2)", """ + import kotlinx.cinterop.* + + @UnsafeNumber([ + "${LINUX_ARM32_HFP.name}: kotlin.Int", "${LINUX_ARM64.name}: kotlin.Long", + "${LINUX_MIPS32.name}: kotlin.Int", "${LINUX_X64.name}: kotlin.Long" + ]) + typealias X = PlatformInt + @UnsafeNumber([ + "${LINUX_ARM32_HFP.name}: kotlinx.cinterop.IntVarOf", "${LINUX_ARM64.name}: kotlinx.cinterop.LongVarOf", + "${LINUX_MIPS32.name}: kotlinx.cinterop.IntVarOf", "${LINUX_X64.name}: kotlinx.cinterop.LongVarOf" + ]) + typealias XV = PlatformIntVarOf + """.trimIndent() + ) + + result.assertCommonized( + "($intTarget1, $intTarget2, $intTarget3, $longTarget1)", """ + import kotlinx.cinterop.* + + @UnsafeNumber([ + "${LINUX_ARM32_HFP.name}: kotlin.Int", "${LINUX_MIPS32.name}: kotlin.Int", + "${LINUX_MIPSEL32.name}: kotlin.Int", "${LINUX_X64.name}: kotlin.Long" + ]) + typealias X = PlatformInt + @UnsafeNumber([ + "${LINUX_ARM32_HFP.name}: kotlinx.cinterop.IntVarOf", "${LINUX_MIPS32.name}: kotlinx.cinterop.IntVarOf", + "${LINUX_MIPSEL32.name}: kotlinx.cinterop.IntVarOf", "${LINUX_X64.name}: kotlinx.cinterop.LongVarOf" + ]) + typealias XV = PlatformIntVarOf + """.trimIndent() + ) + + result.assertCommonized( + "($longTarget1, $longTarget2, $longTarget3, $intTarget1)", """ + import kotlinx.cinterop.* + + @UnsafeNumber([ + "${LINUX_ARM32_HFP.name}: kotlin.Int", "${LINUX_ARM64.name}: kotlin.Long", + "${LINUX_X64.name}: kotlin.Long", "${MACOS_X64.name}: kotlin.Long" + ]) + typealias X = PlatformInt + @UnsafeNumber([ + "${LINUX_ARM32_HFP.name}: kotlinx.cinterop.IntVarOf", "${LINUX_ARM64.name}: kotlinx.cinterop.LongVarOf", + "${LINUX_X64.name}: kotlinx.cinterop.LongVarOf", "${MACOS_X64.name}: kotlinx.cinterop.LongVarOf" + ]) + typealias XV = PlatformIntVarOf + """.trimIndent() + ) + + result.assertCommonized( + "($intTarget1, $intTarget2, $intTarget3, $longTarget1, $longTarget2, $longTarget3)", """ + import kotlinx.cinterop.* + + typealias X = PlatformInt + typealias XV = PlatformIntVarOf """.trimIndent() ) } diff --git a/native/frontend/src/org/jetbrains/kotlin/descriptors/konan/PlatformIntegerClassIds.kt b/native/frontend/src/org/jetbrains/kotlin/descriptors/konan/PlatformIntegerClassIds.kt index 53d8ff37b5f..20101b7405c 100644 --- a/native/frontend/src/org/jetbrains/kotlin/descriptors/konan/PlatformIntegerClassIds.kt +++ b/native/frontend/src/org/jetbrains/kotlin/descriptors/konan/PlatformIntegerClassIds.kt @@ -34,8 +34,8 @@ val ULONG_RANGE_ID = ClassId.fromString("kotlin/ranges/ULongRange") val INT_PROGRESSION_ID = ClassId.fromString("kotlin/ranges/IntProgression") val LONG_PROGRESSION_ID = ClassId.fromString("kotlin/ranges/LongProgression") -val UINT_PROGRESSION = ClassId.fromString("kotlin/ranges/UIntProgression") -val ULONG_PROGRESSION = ClassId.fromString("kotlin/ranges/ULongProgression") +val UINT_PROGRESSION_ID = ClassId.fromString("kotlin/ranges/UIntProgression") +val ULONG_PROGRESSION_ID = ClassId.fromString("kotlin/ranges/ULongProgression") val PLATFORM_INT_ID = ClassId.fromString("kotlin/PlatformInt") val PLATFORM_UINT_ID = ClassId.fromString("kotlin/PlatformUInt")