Drop deprecated primitive ranges and progressions: for Byte and Short

This commit is contained in:
Ilya Gorbunov
2015-11-27 18:44:55 +03:00
parent e8621cb738
commit 6dd8470835
11 changed files with 14 additions and 640 deletions
@@ -7,7 +7,6 @@ fun ranges(): List<GenericFunction> {
val templates = arrayListOf<GenericFunction>()
val rangePrimitives = listOf(PrimitiveType.Int, PrimitiveType.Long, PrimitiveType.Char)
val floatingPointPrimitives = listOf(PrimitiveType.Double, PrimitiveType.Float)
fun rangeElementType(fromType: PrimitiveType, toType: PrimitiveType)
= maxByCapacity(fromType, toType).let { if (it == PrimitiveType.Char) it else maxByCapacity(it, PrimitiveType.Int) }
@@ -28,23 +27,6 @@ fun ranges(): List<GenericFunction> {
}
}
templates add f("reversed()") {
only(RangesOfPrimitives, ProgressionsOfPrimitives)
only(defaultPrimitives - PrimitiveType.Boolean - rangePrimitives - floatingPointPrimitives)
doc(ProgressionsOfPrimitives) { "Returns a progression that goes over the same range in the opposite direction with the same step." }
doc(RangesOfPrimitives) { "Returns a progression that goes over this range in reverse direction." }
returns("TProgression")
deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.WARNING))
deprecate(RangesOfPrimitives) { forBinaryCompatibility }
annotations("""@Suppress("DEPRECATION_ERROR")""")
body(RangesOfPrimitives) {
"return TProgression.fromClosedRange(last, first, -ONE)"
}
body(ProgressionsOfPrimitives) {
"return TProgression.fromClosedRange(last, first, -step)"
}
}
templates add f("step(step: SUM)") {
infix(true)
@@ -68,39 +50,6 @@ fun ranges(): List<GenericFunction> {
}
}
templates add f("step(step: SUM)") {
infix(true)
only(RangesOfPrimitives, ProgressionsOfPrimitives)
only(defaultPrimitives - PrimitiveType.Boolean - rangePrimitives - floatingPointPrimitives)
doc(ProgressionsOfPrimitives) { "Returns a progression that goes over the same range with the given step." }
doc(RangesOfPrimitives) { "Returns a progression that goes over this range with given step." }
returns("TProgression")
deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.WARNING))
deprecate(RangesOfPrimitives) { forBinaryCompatibility }
annotations("""@Suppress("DEPRECATION_ERROR")""")
body(RangesOfPrimitives) {
"""
checkStepIsPositive(step > 0, step)
return TProgression.fromClosedRange(first, last, step)
"""
}
bodyForTypes(RangesOfPrimitives, PrimitiveType.Float, PrimitiveType.Double) {
"""
if (step.isNaN()) throw IllegalArgumentException("Step must not be NaN.")
checkStepIsPositive(step > 0, step)
return TProgression.fromClosedRange(start, end, step)
"""
}
body(ProgressionsOfPrimitives) {
"""
checkStepIsPositive(step > 0, step)
return TProgression.fromClosedRange(first, last, if (this.step > 0) step else -step)
"""
}
}
fun downTo(fromType: PrimitiveType, toType: PrimitiveType) = f("downTo(to: $toType)") {
infix(true)
@@ -118,32 +67,6 @@ fun ranges(): List<GenericFunction> {
"""
}
if (!fromType.isIntegral() || !toType.isIntegral()) {
deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.WARNING))
annotations("""@Suppress("DEPRECATION_ERROR")""")
}
val fromExpr = if (elementType == fromType) "this" else "this.to$elementType()"
val toExpr = if (elementType == toType) "to" else "to.to$elementType()"
val incrementExpr = when (elementType) {
PrimitiveType.Long -> "-1L"
PrimitiveType.Float -> "-1.0F"
PrimitiveType.Double -> "-1.0"
else -> "-1"
}
body { "return $progressionType.fromClosedRange($fromExpr, $toExpr, $incrementExpr)" }
}
fun downToDeprecated(fromType: PrimitiveType, toType: PrimitiveType) = f("`-downTo`(to: $toType)") {
deprecate { forBinaryCompatibility }
sourceFile(SourceFile.Ranges)
only(Primitives)
only(fromType)
val elementType = maxByCapacity(fromType, toType)
val progressionType = elementType.name + "Progression"
returns(progressionType)
annotations("""@[kotlin.jvm.JvmName("downTo") kotlin.jvm.JvmVersion]""")
val fromExpr = if (elementType == fromType) "this" else "this.to$elementType()"
val toExpr = if (elementType == toType) "to" else "to.to$elementType()"
val incrementExpr = when (elementType) {
@@ -162,7 +85,6 @@ fun ranges(): List<GenericFunction> {
val integralPermutations = primitivePermutations.filter { it.first.isIntegral() && it.second.isIntegral() }
templates addAll integralPermutations.map { downTo(it.first, it.second) }
templates addAll listOf(PrimitiveType.Byte, PrimitiveType.Short).permutations().map { downToDeprecated(it.first, it.second) }
fun until(fromType: PrimitiveType, toType: PrimitiveType) = f("until(to: $toType)") {
infix(true)
@@ -181,11 +103,6 @@ fun ranges(): List<GenericFunction> {
"""
}
if (!fromType.isIntegral() || !toType.isIntegral()) {
deprecate(Deprecation("This range implementation has unclear semantics and will be removed soon.", level = DeprecationLevel.WARNING))
annotations("""@Suppress("DEPRECATION_ERROR")""")
}
val fromExpr = if (elementType == fromType) "this" else "this.to$elementType()"
if (elementType == toType) {
@@ -207,36 +124,7 @@ fun ranges(): List<GenericFunction> {
}
}
fun untilDeprecated(fromType: PrimitiveType, toType: PrimitiveType) = f("`-until`(to: $toType)") {
deprecate { forBinaryCompatibility }
sourceFile(SourceFile.Ranges)
only(Primitives)
only(fromType)
val elementType = maxByCapacity(fromType, toType)
val progressionType = elementType.name + "Range"
returns(progressionType)
annotations("""@[kotlin.jvm.JvmName("until") kotlin.jvm.JvmVersion]""")
val fromExpr = if (elementType == fromType) "this" else "this.to$elementType()"
if (elementType == toType) {
// hack to work around incorrect char overflow behavior in JVM and int overflow behavior in JS
val toExpr = "to"
body {
"""
val to_ = ($toExpr - 1).to$elementType()
if (to_ > to) throw IllegalArgumentException("The to argument value '${'$'}to' was too small.")
return $progressionType($fromExpr, to_)
"""
}
} else {
body { "return $progressionType($fromExpr, (to.to$elementType() - 1).to$elementType())" }
}
}
templates addAll integralPermutations.map { until(it.first, it.second) }
templates addAll listOf(PrimitiveType.Byte, PrimitiveType.Short).permutations().map { untilDeprecated(it.first, it.second) }
fun contains(rangeType: PrimitiveType, itemType: PrimitiveType) = f("contains(value: $itemType)") {
operator(true)