[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" 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" 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.modifyGeneratedCompareTo(thisKind: PrimitiveType, otherKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedBinaryOperation(thisKind: PrimitiveType, otherKind: PrimitiveType) {} internal open fun MethodBuilder.modifyGeneratedBinaryOperation(thisKind: PrimitiveType, otherKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedUnaryOperation(thisKind: PrimitiveType) {} internal open fun MethodBuilder.modifyGeneratedUnaryOperation(thisKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedRangeTo(thisKind: PrimitiveType) {} internal open fun MethodBuilder.modifyGeneratedRangeTo(thisKind: PrimitiveType, otherKind: PrimitiveType, opReturnType: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedRangeUntil(thisKind: PrimitiveType) {} internal open fun MethodBuilder.modifyGeneratedRangeUntil(thisKind: PrimitiveType, otherKind: PrimitiveType, opReturnType: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedBitShiftOperators(thisKind: PrimitiveType) {} internal open fun MethodBuilder.modifyGeneratedBitShiftOperators(thisKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedBitwiseOperators(thisKind: PrimitiveType) {} internal open fun MethodBuilder.modifyGeneratedBitwiseOperators(thisKind: PrimitiveType) {}
internal open fun MethodBuilder.modifyGeneratedConversions(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) { override fun ClassBuilder.generateAdditionalMethods(thisKind: PrimitiveType) {
method { method {
signature { 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) { 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()}`.") 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) "$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) { override fun MethodBuilder.modifyGeneratedBitShiftOperators(thisKind: PrimitiveType) {
setAsExternal() 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) { override fun MethodBuilder.modifyGeneratedBitShiftOperators(thisKind: PrimitiveType) {
if (thisKind == PrimitiveType.INT) { if (thisKind == PrimitiveType.INT) {
implementAsIntrinsic(thisKind, methodName) implementAsIntrinsic(thisKind, methodName)
@@ -273,6 +273,8 @@ internal class MethodBuilder : AnnotatedAndDocumented(), PrimitiveBuilder {
} }
} }
fun noBody() { body = null }
fun String.addAsSingleLineBody(bodyOnNewLine: Boolean = false) { fun String.addAsSingleLineBody(bodyOnNewLine: Boolean = false) {
val skip = if (bodyOnNewLine) "$END_LINE " else " " val skip = if (bodyOnNewLine) "$END_LINE " else " "
body = " =$skip$this" body = " =$skip$this"
@@ -299,24 +299,20 @@ public final class Byte private constructor() : Number(), Comparable<Byte> {
-this.toInt() -this.toInt()
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Byte): IntRange { public operator fun rangeTo(other: Byte): IntRange =
return IntRange(this.toInt(), other.toInt()) IntRange(this.toInt(), other.toInt())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Short): IntRange { public operator fun rangeTo(other: Short): IntRange =
return IntRange(this.toInt(), other.toInt()) IntRange(this.toInt(), other.toInt())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Int): IntRange { public operator fun rangeTo(other: Int): IntRange =
return IntRange(this.toInt(), other) IntRange(this.toInt(), other)
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Long): LongRange { public operator fun rangeTo(other: Long): LongRange =
return LongRange(this.toLong(), other) LongRange(this.toLong(), other)
}
/** /**
* Creates a range from this value up to but excluding the specified [other] value. * 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() -this.toInt()
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Byte): IntRange { public operator fun rangeTo(other: Byte): IntRange =
return IntRange(this.toInt(), other.toInt()) IntRange(this.toInt(), other.toInt())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Short): IntRange { public operator fun rangeTo(other: Short): IntRange =
return IntRange(this.toInt(), other.toInt()) IntRange(this.toInt(), other.toInt())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Int): IntRange { public operator fun rangeTo(other: Int): IntRange =
return IntRange(this.toInt(), other) IntRange(this.toInt(), other)
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Long): LongRange { public operator fun rangeTo(other: Long): LongRange =
return LongRange(this.toLong(), other) LongRange(this.toLong(), other)
}
/** /**
* Creates a range from this value up to but excluding the specified [other] value. * 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 external public operator fun unaryMinus(): Int
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Byte): IntRange { public operator fun rangeTo(other: Byte): IntRange =
return IntRange(this, other.toInt()) IntRange(this, other.toInt())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Short): IntRange { public operator fun rangeTo(other: Short): IntRange =
return IntRange(this, other.toInt()) IntRange(this, other.toInt())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Int): IntRange { public operator fun rangeTo(other: Int): IntRange =
return IntRange(this, other) IntRange(this, other)
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Long): LongRange { public operator fun rangeTo(other: Long): LongRange =
return LongRange(this.toLong(), other) LongRange(this.toLong(), other)
}
/** /**
* Creates a range from this value up to but excluding the specified [other] value. * 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 external public operator fun unaryMinus(): Long
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Byte): LongRange { public operator fun rangeTo(other: Byte): LongRange =
return LongRange(this, other.toLong()) LongRange(this, other.toLong())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Short): LongRange { public operator fun rangeTo(other: Short): LongRange =
return LongRange(this, other.toLong()) LongRange(this, other.toLong())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Int): LongRange { public operator fun rangeTo(other: Int): LongRange =
return LongRange(this, other.toLong()) LongRange(this, other.toLong())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Long): LongRange { public operator fun rangeTo(other: Long): LongRange =
return LongRange(this, other) LongRange(this, other)
}
/** /**
* Creates a range from this value up to but excluding the specified [other] value. * 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() -this.toInt()
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Byte): IntRange { public operator fun rangeTo(other: Byte): IntRange =
return IntRange(this.toInt(), other.toInt()) IntRange(this.toInt(), other.toInt())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Short): IntRange { public operator fun rangeTo(other: Short): IntRange =
return IntRange(this.toInt(), other.toInt()) IntRange(this.toInt(), other.toInt())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Int): IntRange { public operator fun rangeTo(other: Int): IntRange =
return IntRange(this.toInt(), other) IntRange(this.toInt(), other)
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Long): LongRange { public operator fun rangeTo(other: Long): LongRange =
return LongRange(this.toLong(), other) LongRange(this.toLong(), other)
}
/** /**
* Creates a range from this value up to but excluding the specified [other] value. * 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() -this.toInt()
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Byte): IntRange { public operator fun rangeTo(other: Byte): IntRange =
return IntRange(this.toInt(), other.toInt()) IntRange(this.toInt(), other.toInt())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Short): IntRange { public operator fun rangeTo(other: Short): IntRange =
return IntRange(this.toInt(), other.toInt()) IntRange(this.toInt(), other.toInt())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Int): IntRange { public operator fun rangeTo(other: Int): IntRange =
return IntRange(this.toInt(), other) IntRange(this.toInt(), other)
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Long): LongRange { public operator fun rangeTo(other: Long): LongRange =
return LongRange(this.toLong(), other) LongRange(this.toLong(), other)
}
/** /**
* Creates a range from this value up to but excluding the specified [other] value. * 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 0 - this
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Byte): IntRange { public operator fun rangeTo(other: Byte): IntRange =
return IntRange(this, other.toInt()) IntRange(this, other.toInt())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Short): IntRange { public operator fun rangeTo(other: Short): IntRange =
return IntRange(this, other.toInt()) IntRange(this, other.toInt())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Int): IntRange { public operator fun rangeTo(other: Int): IntRange =
return IntRange(this, other) IntRange(this, other)
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Long): LongRange { public operator fun rangeTo(other: Long): LongRange =
return LongRange(this.toLong(), other) LongRange(this.toLong(), other)
}
/** /**
* Creates a range from this value up to but excluding the specified [other] value. * 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 0L - this
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Byte): LongRange { public operator fun rangeTo(other: Byte): LongRange =
return LongRange(this, other.toLong()) LongRange(this, other.toLong())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Short): LongRange { public operator fun rangeTo(other: Short): LongRange =
return LongRange(this, other.toLong()) LongRange(this, other.toLong())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Int): LongRange { public operator fun rangeTo(other: Int): LongRange =
return LongRange(this, other.toLong()) LongRange(this, other.toLong())
}
/** Creates a range from this value to the specified [other] value. */ /** Creates a range from this value to the specified [other] value. */
public operator fun rangeTo(other: Long): LongRange { public operator fun rangeTo(other: Long): LongRange =
return LongRange(this, other) LongRange(this, other)
}
/** /**
* Creates a range from this value up to but excluding the specified [other] value. * Creates a range from this value up to but excluding the specified [other] value.