rangeUntil member operator in builtins #KT-52933

This commit is contained in:
Ilya Gorbunov
2022-08-11 00:41:04 +02:00
committed by Space
parent ec9c862034
commit 203a00151d
23 changed files with 1109 additions and 97 deletions
+19 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.
*/
@@ -178,6 +178,7 @@ class GeneratePrimitives(out: PrintWriter) : BuiltInsSourceGenerator(out) {
generateBinaryOperators(kind)
generateUnaryOperators(kind)
generateRangeTo(kind)
generateRangeUntil(kind)
if (kind == PrimitiveType.INT || kind == PrimitiveType.LONG) {
generateBitShiftOperators(kind)
@@ -250,6 +251,23 @@ class GeneratePrimitives(out: PrintWriter) : BuiltInsSourceGenerator(out) {
}
private fun generateRangeUntil(thisKind: PrimitiveType) {
for (otherKind in PrimitiveType.onlyNumeric) {
val returnType = maxByDomainCapacity(maxByDomainCapacity(thisKind, otherKind), PrimitiveType.INT)
if (returnType == PrimitiveType.DOUBLE || returnType == PrimitiveType.FLOAT)
continue
out.println(" /**")
out.println(" * Creates a range from this value up to but excluding the specified [other] value.")
out.println(" *")
out.println(" * If the [other] value is less than or equal to `this` value, then the returned range is empty.")
out.println(" */")
out.println(" @SinceKotlin(\"1.7\")")
out.println(" @ExperimentalStdlibApi")
out.println(" public operator fun rangeUntil(other: ${otherKind.capitalized}): ${returnType.capitalized}Range")
out.println()
}
}
private fun generateUnaryOperators(kind: PrimitiveType) {
for (name in listOf("inc", "dec")) {
out.println(incDecOperatorsDoc(name).replaceIndent(" "))
+18 -1
View File
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* 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.
*/
@@ -96,6 +96,7 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns
generateBinaryOperators()
generateUnaryOperators()
generateRangeTo()
generateRangeUntil()
if (type == UnsignedType.UINT || type == UnsignedType.ULONG) {
generateBitShiftOperators()
@@ -219,6 +220,22 @@ class UnsignedTypeGenerator(val type: UnsignedType, out: PrintWriter) : BuiltIns
out.println()
}
private fun generateRangeUntil() {
val rangeElementType = maxByDomainCapacity(type, UnsignedType.UINT)
val rangeType = rangeElementType.capitalized + "Range"
fun convert(name: String) = if (rangeElementType == type) name else "$name.to${rangeElementType.capitalized}()"
out.println(" /**")
out.println(" * Creates a range from this value up to but excluding the specified [other] value.")
out.println(" *")
out.println(" * If the [other] value is less than or equal to `this` value, then the returned range is empty.")
out.println(" */")
out.println(" @SinceKotlin(\"1.7\")")
out.println(" @ExperimentalStdlibApi")
out.println(" @kotlin.internal.InlineOnly")
out.println(" public inline operator fun rangeUntil(other: $className): $rangeType = ${convert("this")} until ${convert("other")}")
out.println()
}
private fun generateBitShiftOperators() {