[Generator] Set up body for rangeTo and rangeUntil by default

Also use expression body formatting for `rangeTo`.
This commit is contained in:
Ilya Gorbunov
2023-04-22 05:13:45 +02:00
committed by Space Team
parent 097535aeea
commit 3f3cd2e87a
8 changed files with 86 additions and 126 deletions
@@ -447,7 +447,10 @@ abstract class BasePrimitivesGenerator(private val writer: PrintWriter) : BuiltI
}
returnType = "${opReturnType.capitalized}Range"
}
}.modifyGeneratedRangeTo(thisKind)
val thisCasted = "this${thisKind.castToIfNecessary(opReturnType)}"
val otherCasted = "other${otherKind.castToIfNecessary(opReturnType)}"
"${returnType}($thisCasted, $otherCasted)".addAsSingleLineBody(bodyOnNewLine = true)
}.modifyGeneratedRangeTo(thisKind, otherKind, opReturnType)
}
}
@@ -477,7 +480,8 @@ abstract class BasePrimitivesGenerator(private val writer: PrintWriter) : BuiltI
}
returnType = "${opReturnType.capitalized}Range"
}
}.modifyGeneratedRangeUntil(thisKind)
"this until $parameterName".addAsSingleLineBody(bodyOnNewLine = false)
}.modifyGeneratedRangeUntil(thisKind, otherKind, opReturnType)
}
}
@@ -620,8 +624,8 @@ abstract class BasePrimitivesGenerator(private val writer: PrintWriter) : BuiltI
internal open fun MethodBuilder.modifyGeneratedCompareTo(thisKind: PrimitiveType, otherKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedBinaryOperation(thisKind: PrimitiveType, otherKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedUnaryOperation(thisKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedRangeTo(thisKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedRangeUntil(thisKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedRangeTo(thisKind: PrimitiveType, otherKind: PrimitiveType, opReturnType: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedRangeUntil(thisKind: PrimitiveType, otherKind: PrimitiveType, opReturnType: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedBitShiftOperators(thisKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedBitwiseOperators(thisKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedConversions(thisKind: PrimitiveType) {}
@@ -24,6 +24,10 @@ class JsPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(write
}
}
override fun MethodBuilder.modifyGeneratedRangeTo(thisKind: PrimitiveType, otherKind: PrimitiveType, opReturnType: PrimitiveType) {
noBody()
}
override fun ClassBuilder.generateAdditionalMethods(thisKind: PrimitiveType) {
method {
signature {
@@ -33,8 +37,4 @@ class JsPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(write
}
}
}
override fun MethodBuilder.modifyGeneratedRangeUntil(thisKind: PrimitiveType) {
"this until $parameterName".addAsSingleLineBody(bodyOnNewLine = false)
}
}
@@ -12,4 +12,12 @@ class JvmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(writ
override fun ClassBuilder.modifyGeneratedClass(thisKind: PrimitiveType) {
appendDoc("On the JVM, non-nullable values of this type are represented as values of the primitive type `${thisKind.name.lowercase()}`.")
}
override fun MethodBuilder.modifyGeneratedRangeTo(thisKind: PrimitiveType, otherKind: PrimitiveType, opReturnType: PrimitiveType) {
noBody()
}
override fun MethodBuilder.modifyGeneratedRangeUntil(thisKind: PrimitiveType, otherKind: PrimitiveType, opReturnType: PrimitiveType) {
noBody()
}
}
@@ -101,17 +101,6 @@ class NativePrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(w
"$sign$thisCasted".addAsSingleLineBody(bodyOnNewLine = true)
}
override fun MethodBuilder.modifyGeneratedRangeTo(thisKind: PrimitiveType) {
val rangeType = PrimitiveType.valueOf(returnType.replace("Range", "").uppercase())
val thisCasted = "this" + thisKind.castToIfNecessary(rangeType)
val otherCasted = parameterName + parameterType.toPrimitiveType().castToIfNecessary(rangeType)
"return ${returnType}($thisCasted, $otherCasted)".addAsMultiLineBody()
}
override fun MethodBuilder.modifyGeneratedRangeUntil(thisKind: PrimitiveType) {
"this until $parameterName".addAsSingleLineBody(bodyOnNewLine = false)
}
override fun MethodBuilder.modifyGeneratedBitShiftOperators(thisKind: PrimitiveType) {
setAsExternal()
}
@@ -153,17 +153,6 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
}
}
override fun MethodBuilder.modifyGeneratedRangeTo(thisKind: PrimitiveType) {
val rangeType = PrimitiveType.valueOf(returnType.replace("Range", "").uppercase())
val thisCasted = "this" + thisKind.castToIfNecessary(rangeType)
val otherCasted = parameterName + parameterType.toPrimitiveType().castToIfNecessary(rangeType)
"return ${returnType}($thisCasted, $otherCasted)".addAsMultiLineBody()
}
override fun MethodBuilder.modifyGeneratedRangeUntil(thisKind: PrimitiveType) {
"this until $parameterName".addAsSingleLineBody(bodyOnNewLine = false)
}
override fun MethodBuilder.modifyGeneratedBitShiftOperators(thisKind: PrimitiveType) {
if (thisKind == PrimitiveType.INT) {
implementAsIntrinsic(thisKind, methodName)
@@ -273,6 +273,8 @@ internal class MethodBuilder : AnnotatedAndDocumented(), PrimitiveBuilder {
}
}
fun noBody() { body = null }
fun String.addAsSingleLineBody(bodyOnNewLine: Boolean = false) {
val skip = if (bodyOnNewLine) "$END_LINE " else " "
body = " =$skip$this"
@@ -299,24 +299,20 @@ public final class Byte private constructor() : Number(), Comparable<Byte> {
-this.toInt()
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Byte): IntRange {
return IntRange(this.toInt(), other.toInt())
}
public operator fun rangeTo(other: Byte): IntRange =
IntRange(this.toInt(), other.toInt())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Short): IntRange {
return IntRange(this.toInt(), other.toInt())
}
public operator fun rangeTo(other: Short): IntRange =
IntRange(this.toInt(), other.toInt())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Int): IntRange {
return IntRange(this.toInt(), other)
}
public operator fun rangeTo(other: Int): IntRange =
IntRange(this.toInt(), other)
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Long): LongRange {
return LongRange(this.toLong(), other)
}
public operator fun rangeTo(other: Long): LongRange =
LongRange(this.toLong(), other)
/**
* Creates a range from this value up to but excluding the specified [other] value.
@@ -730,24 +726,20 @@ public final class Short private constructor() : Number(), Comparable<Short> {
-this.toInt()
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Byte): IntRange {
return IntRange(this.toInt(), other.toInt())
}
public operator fun rangeTo(other: Byte): IntRange =
IntRange(this.toInt(), other.toInt())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Short): IntRange {
return IntRange(this.toInt(), other.toInt())
}
public operator fun rangeTo(other: Short): IntRange =
IntRange(this.toInt(), other.toInt())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Int): IntRange {
return IntRange(this.toInt(), other)
}
public operator fun rangeTo(other: Int): IntRange =
IntRange(this.toInt(), other)
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Long): LongRange {
return LongRange(this.toLong(), other)
}
public operator fun rangeTo(other: Long): LongRange =
LongRange(this.toLong(), other)
/**
* Creates a range from this value up to but excluding the specified [other] value.
@@ -1159,24 +1151,20 @@ public final class Int private constructor() : Number(), Comparable<Int> {
external public operator fun unaryMinus(): Int
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Byte): IntRange {
return IntRange(this, other.toInt())
}
public operator fun rangeTo(other: Byte): IntRange =
IntRange(this, other.toInt())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Short): IntRange {
return IntRange(this, other.toInt())
}
public operator fun rangeTo(other: Short): IntRange =
IntRange(this, other.toInt())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Int): IntRange {
return IntRange(this, other)
}
public operator fun rangeTo(other: Int): IntRange =
IntRange(this, other)
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Long): LongRange {
return LongRange(this.toLong(), other)
}
public operator fun rangeTo(other: Long): LongRange =
LongRange(this.toLong(), other)
/**
* Creates a range from this value up to but excluding the specified [other] value.
@@ -1641,24 +1629,20 @@ public final class Long private constructor() : Number(), Comparable<Long> {
external public operator fun unaryMinus(): Long
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Byte): LongRange {
return LongRange(this, other.toLong())
}
public operator fun rangeTo(other: Byte): LongRange =
LongRange(this, other.toLong())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Short): LongRange {
return LongRange(this, other.toLong())
}
public operator fun rangeTo(other: Short): LongRange =
LongRange(this, other.toLong())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Int): LongRange {
return LongRange(this, other.toLong())
}
public operator fun rangeTo(other: Int): LongRange =
LongRange(this, other.toLong())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Long): LongRange {
return LongRange(this, other)
}
public operator fun rangeTo(other: Long): LongRange =
LongRange(this, other)
/**
* Creates a range from this value up to but excluding the specified [other] value.
@@ -299,24 +299,20 @@ public class Byte private constructor(private val value: Byte) : Number(), Compa
-this.toInt()
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Byte): IntRange {
return IntRange(this.toInt(), other.toInt())
}
public operator fun rangeTo(other: Byte): IntRange =
IntRange(this.toInt(), other.toInt())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Short): IntRange {
return IntRange(this.toInt(), other.toInt())
}
public operator fun rangeTo(other: Short): IntRange =
IntRange(this.toInt(), other.toInt())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Int): IntRange {
return IntRange(this.toInt(), other)
}
public operator fun rangeTo(other: Int): IntRange =
IntRange(this.toInt(), other)
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Long): LongRange {
return LongRange(this.toLong(), other)
}
public operator fun rangeTo(other: Long): LongRange =
LongRange(this.toLong(), other)
/**
* Creates a range from this value up to but excluding the specified [other] value.
@@ -725,24 +721,20 @@ public class Short private constructor(private val value: Short) : Number(), Com
-this.toInt()
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Byte): IntRange {
return IntRange(this.toInt(), other.toInt())
}
public operator fun rangeTo(other: Byte): IntRange =
IntRange(this.toInt(), other.toInt())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Short): IntRange {
return IntRange(this.toInt(), other.toInt())
}
public operator fun rangeTo(other: Short): IntRange =
IntRange(this.toInt(), other.toInt())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Int): IntRange {
return IntRange(this.toInt(), other)
}
public operator fun rangeTo(other: Int): IntRange =
IntRange(this.toInt(), other)
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Long): LongRange {
return LongRange(this.toLong(), other)
}
public operator fun rangeTo(other: Long): LongRange =
LongRange(this.toLong(), other)
/**
* Creates a range from this value up to but excluding the specified [other] value.
@@ -1154,24 +1146,20 @@ public class Int private constructor(private val value: Int) : Number(), Compara
0 - this
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Byte): IntRange {
return IntRange(this, other.toInt())
}
public operator fun rangeTo(other: Byte): IntRange =
IntRange(this, other.toInt())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Short): IntRange {
return IntRange(this, other.toInt())
}
public operator fun rangeTo(other: Short): IntRange =
IntRange(this, other.toInt())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Int): IntRange {
return IntRange(this, other)
}
public operator fun rangeTo(other: Int): IntRange =
IntRange(this, other)
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Long): LongRange {
return LongRange(this.toLong(), other)
}
public operator fun rangeTo(other: Long): LongRange =
LongRange(this.toLong(), other)
/**
* Creates a range from this value up to but excluding the specified [other] value.
@@ -1656,24 +1644,20 @@ public class Long private constructor(private val value: Long) : Number(), Compa
0L - this
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Byte): LongRange {
return LongRange(this, other.toLong())
}
public operator fun rangeTo(other: Byte): LongRange =
LongRange(this, other.toLong())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Short): LongRange {
return LongRange(this, other.toLong())
}
public operator fun rangeTo(other: Short): LongRange =
LongRange(this, other.toLong())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Int): LongRange {
return LongRange(this, other.toLong())
}
public operator fun rangeTo(other: Int): LongRange =
LongRange(this, other.toLong())
/** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Long): LongRange {
return LongRange(this, other)
}
public operator fun rangeTo(other: Long): LongRange =
LongRange(this, other)
/**
* Creates a range from this value up to but excluding the specified [other] value.