From b9f78d002a35260f2c311fd7355e33a0ec7f1242 Mon Sep 17 00:00:00 2001 From: Ilya Gorbunov Date: Thu, 2 Sep 2021 05:25:10 +0300 Subject: [PATCH] Do not duplicate Range/Progression sources in Native runtime --- kotlin-native/backend.native/build.gradle | 4 + .../kotlin/kotlin/internal/ProgressionUtil.kt | 72 ------- .../kotlin/ranges/ProgressionIterators.kt | 79 ------- .../main/kotlin/kotlin/ranges/Progressions.kt | 197 ------------------ .../src/main/kotlin/kotlin/ranges/Range.kt | 34 --- .../src/main/kotlin/kotlin/ranges/Ranges.kt | 100 --------- 6 files changed, 4 insertions(+), 482 deletions(-) delete mode 100644 kotlin-native/runtime/src/main/kotlin/kotlin/internal/ProgressionUtil.kt delete mode 100644 kotlin-native/runtime/src/main/kotlin/kotlin/ranges/ProgressionIterators.kt delete mode 100644 kotlin-native/runtime/src/main/kotlin/kotlin/ranges/Progressions.kt delete mode 100644 kotlin-native/runtime/src/main/kotlin/kotlin/ranges/Range.kt delete mode 100644 kotlin-native/runtime/src/main/kotlin/kotlin/ranges/Ranges.kt diff --git a/kotlin-native/backend.native/build.gradle b/kotlin-native/backend.native/build.gradle index 77c76a8742c..ca33599df46 100644 --- a/kotlin-native/backend.native/build.gradle +++ b/kotlin-native/backend.native/build.gradle @@ -147,6 +147,9 @@ classes.dependsOn 'compilerClasses', 'cli_bcClasses' task stdlib(dependsOn: "${hostName}Stdlib") def commonSrc = project(":kotlin-stdlib-common").files("src/kotlin", "src/generated", "../unsigned/src", "../src").files +def commonBuiltinsSrc = project(":core:builtins").files( + ["Progressions.kt", "ProgressionIterators.kt", "Range.kt", "Ranges.kt", "internal/progressionUtil.kt"] + .collect { "src/kotlin/$it" }).files def interopRuntimeCommonSrc = project(':kotlin-native:Interop:Runtime').file('src/main/kotlin') @@ -191,6 +194,7 @@ targetList.each { target -> '-opt-in=kotlin.native.internal.InternalForKotlinNative', '-opt-in=kotlin.native.SymbolNameIsDeprecated', *commonSrc.toList(), + *commonBuiltinsSrc.toList(), *testAnnotationCommon.toList(), *testCommon.toList(), "-Xcommon-sources=${commonSrc.join(',')}", diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/internal/ProgressionUtil.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/internal/ProgressionUtil.kt deleted file mode 100644 index 04b54c62168..00000000000 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/internal/ProgressionUtil.kt +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ - -package kotlin.internal - -// a mod b (in arithmetical sense) -private fun mod(a: Int, b: Int): Int { - val mod = a % b - return if (mod >= 0) mod else mod + b -} - -private fun mod(a: Long, b: Long): Long { - val mod = a % b - return if (mod >= 0) mod else mod + b -} - -// (a - b) mod c -private fun differenceModulo(a: Int, b: Int, c: Int): Int { - return mod(mod(a, c) - mod(b, c), c) -} - -private fun differenceModulo(a: Long, b: Long, c: Long): Long { - return mod(mod(a, c) - mod(b, c), c) -} - -/** - * Calculates the final element of a bounded arithmetic progression, i.e. the last element of the progression which is in the range - * from [start] to [end] in case of a positive [step], or from [end] to [start] in case of a negative - * [step]. - * - * No validation on passed parameters is performed. The given parameters should satisfy the condition: - * - * - either `step > 0` and `start <= end`, - * - or `step < 0` and `start >= end`. - * - * @param start first element of the progression - * @param end ending bound for the progression - * @param step increment, or difference of successive elements in the progression - * @return the final element of the progression - * @suppress - */ -@PublishedApi -internal fun getProgressionLastElement(start: Int, end: Int, step: Int): Int = when { - step > 0 -> if (start >= end) end else end - differenceModulo(end, start, step) - step < 0 -> if (start <= end) end else end + differenceModulo(start, end, -step) - else -> throw kotlin.IllegalArgumentException("Step is zero.") -} - -/** - * Calculates the final element of a bounded arithmetic progression, i.e. the last element of the progression which is in the range - * from [start] to [end] in case of a positive [step], or from [end] to [start] in case of a negative - * [step]. - * - * No validation on passed parameters is performed. The given parameters should satisfy the condition: - * - * - either `step > 0` and `start <= end`, - * - or `step < 0` and `start >= end`. - * - * @param start first element of the progression - * @param end ending bound for the progression - * @param step increment, or difference of successive elements in the progression - * @return the final element of the progression - * @suppress - */ -@PublishedApi -internal fun getProgressionLastElement(start: Long, end: Long, step: Long): Long = when { - step > 0 -> if (start >= end) end else end - differenceModulo(end, start, step) - step < 0 -> if (start <= end) end else end + differenceModulo(start, end, -step) - else -> throw kotlin.IllegalArgumentException("Step is zero.") -} diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/ranges/ProgressionIterators.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/ranges/ProgressionIterators.kt deleted file mode 100644 index 5cbbb654dd6..00000000000 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/ranges/ProgressionIterators.kt +++ /dev/null @@ -1,79 +0,0 @@ -/* - * 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 kotlin.ranges - -/** - * An iterator over a progression of values of type `Char`. - * @property step the number by which the value is incremented on each step. - */ -internal class CharProgressionIterator(first: Char, last: Char, val step: Int) : CharIterator() { - private val finalElement: Int = last.code - private var hasNext: Boolean = if (step > 0) first <= last else first >= last - private var next: Int = if (hasNext) first.code else finalElement - - override fun hasNext(): Boolean = hasNext - - override fun nextChar(): Char { - val value = next - if (value == finalElement) { - if (!hasNext) throw kotlin.NoSuchElementException() - hasNext = false - } - else { - next += step - } - return value.toChar() - } -} - -/** - * An iterator over a progression of values of type `Int`. - * @property step the number by which the value is incremented on each step. - */ -internal class IntProgressionIterator(first: Int, last: Int, val step: Int) : IntIterator() { - private val finalElement: Int = last - private var hasNext: Boolean = if (step > 0) first <= last else first >= last - private var next: Int = if (hasNext) first else finalElement - - override fun hasNext(): Boolean = hasNext - - override fun nextInt(): Int { - val value = next - if (value == finalElement) { - if (!hasNext) throw kotlin.NoSuchElementException() - hasNext = false - } - else { - next += step - } - return value - } -} - -/** - * An iterator over a progression of values of type `Long`. - * @property step the number by which the value is incremented on each step. - */ -internal class LongProgressionIterator(first: Long, last: Long, val step: Long) : LongIterator() { - private val finalElement: Long = last - private var hasNext: Boolean = if (step > 0) first <= last else first >= last - private var next: Long = if (hasNext) first else finalElement - - override fun hasNext(): Boolean = hasNext - - override fun nextLong(): Long { - val value = next - if (value == finalElement) { - if (!hasNext) throw kotlin.NoSuchElementException() - hasNext = false - } - else { - next += step - } - return value - } -} - diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/ranges/Progressions.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/ranges/Progressions.kt deleted file mode 100644 index dfffd6cae0a..00000000000 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/ranges/Progressions.kt +++ /dev/null @@ -1,197 +0,0 @@ -/* - * 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. - */ - -// A copy-paste from kotlin/core/builtins/src/kotlin/Progressions.kt - -package kotlin.ranges - -import kotlin.internal.getProgressionLastElement - -/** - * A progression of values of type `Char`. - */ -public open class CharProgression - internal constructor - ( - start: Char, - endInclusive: Char, - step: Int - ) : Iterable { - init { - if (step == 0) throw kotlin.IllegalArgumentException("Step must be non-zero.") - if (step == Int.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must be greater than Int.MIN_VALUE to avoid overflow on negation.") - } - - /** - * The first element in the progression. - */ - public val first: Char = start - - /** - * The last element in the progression. - */ - public val last: Char = getProgressionLastElement(start.code, endInclusive.code, step).toChar() - - /** - * The step of the progression. - */ - public val step: Int = step - - override fun iterator(): CharIterator = CharProgressionIterator(first, last, step) - - /** - * Checks if the progression is empty. - * - * Progression with a positive step is empty if its first element is greater than the last element. - * Progression with a negative step is empty if its first element is less than the last element. - */ - public open fun isEmpty(): Boolean = if (step > 0) first > last else first < last - - override fun equals(other: Any?): Boolean = - other is CharProgression && (isEmpty() && other.isEmpty() || - first == other.first && last == other.last && step == other.step) - - override fun hashCode(): Int = - if (isEmpty()) -1 else (31 * (31 * first.code + last.code) + step) - - override fun toString(): String = if (step > 0) "$first..$last step $step" else "$first downTo $last step ${-step}" - - companion object { - /** - * Creates CharProgression within the specified bounds of a closed range. - * - * The progression starts with the [rangeStart] value and goes toward the [rangeEnd] value not excluding it, with the specified [step]. - * In order to go backwards the [step] must be negative. - * - * [step] must be greater than `Int.MIN_VALUE` and not equal to zero. - */ - public fun fromClosedRange(rangeStart: Char, rangeEnd: Char, step: Int): CharProgression = CharProgression(rangeStart, rangeEnd, step) - } -} - -/** - * A progression of values of type `Int`. - */ -public open class IntProgression - internal constructor - ( - start: Int, - endInclusive: Int, - step: Int - ) : Iterable { - init { - if (step == 0) throw kotlin.IllegalArgumentException("Step must be non-zero.") - if (step == Int.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must be greater than Int.MIN_VALUE to avoid overflow on negation.") - } - - /** - * The first element in the progression. - */ - public val first: Int = start - - /** - * The last element in the progression. - */ - public val last: Int = getProgressionLastElement(start, endInclusive, step) - - /** - * The step of the progression. - */ - public val step: Int = step - - override fun iterator(): IntIterator = IntProgressionIterator(first, last, step) - - /** - * Checks if the progression is empty. - * - * Progression with a positive step is empty if its first element is greater than the last element. - * Progression with a negative step is empty if its first element is less than the last element. - */ - public open fun isEmpty(): Boolean = if (step > 0) first > last else first < last - - override fun equals(other: Any?): Boolean = - other is IntProgression && (isEmpty() && other.isEmpty() || - first == other.first && last == other.last && step == other.step) - - override fun hashCode(): Int = - if (isEmpty()) -1 else (31 * (31 * first + last) + step) - - override fun toString(): String = if (step > 0) "$first..$last step $step" else "$first downTo $last step ${-step}" - - companion object { - /** - * Creates IntProgression within the specified bounds of a closed range. - * - * The progression starts with the [rangeStart] value and goes toward the [rangeEnd] value not excluding it, with the specified [step]. - * In order to go backwards the [step] must be negative. - * - * [step] must be greater than `Int.MIN_VALUE` and not equal to zero. - */ - public fun fromClosedRange(rangeStart: Int, rangeEnd: Int, step: Int): IntProgression = IntProgression(rangeStart, rangeEnd, step) - } -} - -/** - * A progression of values of type `Long`. - */ -public open class LongProgression - internal constructor - ( - start: Long, - endInclusive: Long, - step: Long - ) : Iterable { - init { - if (step == 0L) throw kotlin.IllegalArgumentException("Step must be non-zero.") - if (step == Long.MIN_VALUE) throw kotlin.IllegalArgumentException("Step must be greater than Long.MIN_VALUE to avoid overflow on negation.") - } - - /** - * The first element in the progression. - */ - public val first: Long = start - - /** - * The last element in the progression. - */ - public val last: Long = getProgressionLastElement(start, endInclusive, step) - - /** - * The step of the progression. - */ - public val step: Long = step - - override fun iterator(): LongIterator = LongProgressionIterator(first, last, step) - - /** - * Checks if the progression is empty. - * - * Progression with a positive step is empty if its first element is greater than the last element. - * Progression with a negative step is empty if its first element is less than the last element. - */ - public open fun isEmpty(): Boolean = if (step > 0) first > last else first < last - - override fun equals(other: Any?): Boolean = - other is LongProgression && (isEmpty() && other.isEmpty() || - first == other.first && last == other.last && step == other.step) - - override fun hashCode(): Int = - if (isEmpty()) -1 else (31 * (31 * (first xor (first ushr 32)) + (last xor (last ushr 32))) + (step xor (step ushr 32))).toInt() - - override fun toString(): String = if (step > 0) "$first..$last step $step" else "$first downTo $last step ${-step}" - - companion object { - /** - * Creates LongProgression within the specified bounds of a closed range. - * - * The progression starts with the [rangeStart] value and goes toward the [rangeEnd] value not excluding it, with the specified [step]. - * In order to go backwards the [step] must be negative. - * - * [step] must be greater than `Long.MIN_VALUE` and not equal to zero. - */ - public fun fromClosedRange(rangeStart: Long, rangeEnd: Long, step: Long): LongProgression = LongProgression(rangeStart, rangeEnd, step) - } -} - diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/ranges/Range.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/ranges/Range.kt deleted file mode 100644 index 63bbd291664..00000000000 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/ranges/Range.kt +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license - * that can be found in the LICENSE file. - */ - -package kotlin.ranges - -/** - * Represents a range of values (for example, numbers or characters). - * See the [Kotlin language documentation](https://kotlinlang.org/docs/reference/ranges.html) for more information. - */ -public interface ClosedRange> { - /** - * The minimum value in the range. - */ - public val start: T - - /** - * The maximum value in the range (inclusive). - */ - public val endInclusive: T - - /** - * Checks whether the specified [value] belongs to the range. - */ - public operator fun contains(value: T): Boolean = value >= start && value <= endInclusive - - /** - * Checks whether the range is empty. - * - * The range is empty if its start value is greater than the end value. - */ - public fun isEmpty(): Boolean = start > endInclusive -} diff --git a/kotlin-native/runtime/src/main/kotlin/kotlin/ranges/Ranges.kt b/kotlin-native/runtime/src/main/kotlin/kotlin/ranges/Ranges.kt deleted file mode 100644 index b880c789c34..00000000000 --- a/kotlin-native/runtime/src/main/kotlin/kotlin/ranges/Ranges.kt +++ /dev/null @@ -1,100 +0,0 @@ -/* - * 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 kotlin.ranges - -/** - * A range of values of type `Char`. - */ -public class CharRange(start: Char, endInclusive: Char) : CharProgression(start, endInclusive, 1), ClosedRange { - override val start: Char get() = first - override val endInclusive: Char get() = last - - override fun contains(value: Char): Boolean = first <= value && value <= last - - /** - * Checks whether the range is empty. - * - * The range is empty if its start value is greater than the end value. - */ - override fun isEmpty(): Boolean = first > last - - override fun equals(other: Any?): Boolean = - other is CharRange && (isEmpty() && other.isEmpty() || - first == other.first && last == other.last) - - override fun hashCode(): Int = - if (isEmpty()) -1 else (31 * first.code + last.code) - - override fun toString(): String = "$first..$last" - - companion object { - /** An empty range of values of type Char. */ - public val EMPTY: CharRange = CharRange(1.toChar(), 0.toChar()) - } -} - -/** - * A range of values of type `Int`. - */ -public class IntRange(start: Int, endInclusive: Int) : IntProgression(start, endInclusive, 1), ClosedRange { - override val start: Int get() = first - override val endInclusive: Int get() = last - - override fun contains(value: Int): Boolean = first <= value && value <= last - - /** - * Checks whether the range is empty. - * - * The range is empty if its start value is greater than the end value. - */ - override fun isEmpty(): Boolean = first > last - - override fun equals(other: Any?): Boolean = - other is IntRange && (isEmpty() && other.isEmpty() || - first == other.first && last == other.last) - - override fun hashCode(): Int = - if (isEmpty()) -1 else (31 * first + last) - - override fun toString(): String = "$first..$last" - - companion object { - /** An empty range of values of type Int. */ - public val EMPTY: IntRange = IntRange(1, 0) - } -} - -/** - * A range of values of type `Long`. - */ -public class LongRange(start: Long, endInclusive: Long) : LongProgression(start, endInclusive, 1), ClosedRange { - override val start: Long get() = first - override val endInclusive: Long get() = last - - override fun contains(value: Long): Boolean = first <= value && value <= last - - /** - * Checks whether the range is empty. - * - * The range is empty if its start value is greater than the end value. - */ - override fun isEmpty(): Boolean = first > last - - override fun equals(other: Any?): Boolean = - other is LongRange && (isEmpty() && other.isEmpty() || - first == other.first && last == other.last) - - override fun hashCode(): Int = - if (isEmpty()) -1 else (31 * (first xor (first ushr 32)) + (last xor (last ushr 32))).toInt() - - override fun toString(): String = "$first..$last" - - companion object { - /** An empty range of values of type Long. */ - public val EMPTY: LongRange = LongRange(1, 0) - } -} -