[Generator] Always put expression body on a new line

This commit is contained in:
Ilya Gorbunov
2023-08-29 04:43:07 +02:00
committed by Space Team
parent 68a9b9c312
commit 1c149925b7
13 changed files with 463 additions and 277 deletions
@@ -451,7 +451,7 @@ abstract class BasePrimitivesGenerator(private val writer: PrintWriter) : BuiltI
}
val thisCasted = "this${thisKind.castToIfNecessary(opReturnType)}"
val otherCasted = "other${otherKind.castToIfNecessary(opReturnType)}"
"${returnType}($thisCasted, $otherCasted)".addAsSingleLineBody(bodyOnNewLine = true)
"${returnType}($thisCasted, $otherCasted)".setAsExpressionBody()
}.modifyGeneratedRangeTo(thisKind, otherKind, opReturnType)
}
}
@@ -482,7 +482,7 @@ abstract class BasePrimitivesGenerator(private val writer: PrintWriter) : BuiltI
}
returnType = "${opReturnType.capitalized}Range"
}
"this until $parameterName".addAsSingleLineBody(bodyOnNewLine = false)
"this until $parameterName".setAsExpressionBody()
}.modifyGeneratedRangeUntil(thisKind, otherKind, opReturnType)
}
}
@@ -211,12 +211,12 @@ class WasmBooleanGenerator(writer: PrintWriter) : BooleanGenerator(writer) {
}
override fun MethodBuilder.modifyGeneratedCompareTo() {
"wasm_i32_compareTo(this.toInt(), other.toInt())".addAsSingleLineBody(bodyOnNewLine = true)
"wasm_i32_compareTo(this.toInt(), other.toInt())".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedToString() {
modifySignature { visibility = null }
"if (this) \"true\" else \"false\"".addAsSingleLineBody(bodyOnNewLine = true)
"if (this) \"true\" else \"false\"".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedEquals() {
@@ -227,12 +227,12 @@ class WasmBooleanGenerator(writer: PrintWriter) : BooleanGenerator(writer) {
} else {
wasm_i32_eq(this.toInt(), other.toInt())
}
""".trimIndent().addAsMultiLineBody()
""".trimIndent().setAsBlockBody()
}
override fun MethodBuilder.modifyGeneratedHashCode() {
modifySignature { visibility = null }
"if (this) 1231 else 1237".addAsSingleLineBody(bodyOnNewLine = true)
"if (this) 1231 else 1237".setAsExpressionBody()
}
override fun ClassBuilder.generateAdditionalMethods() {
@@ -243,7 +243,7 @@ class WasmBooleanGenerator(writer: PrintWriter) : BooleanGenerator(writer) {
methodName = "toInt"
returnType = PrimitiveType.INT.capitalized
}
implementedAsIntrinsic.addAsSingleLineBody(bodyOnNewLine = true)
implementedAsIntrinsic.setAsExpressionBody()
}
}
}
@@ -275,11 +275,11 @@ class NativeBooleanGenerator(writer: PrintWriter) : BooleanGenerator(writer) {
}
override fun MethodBuilder.modifyGeneratedToString() {
"if (this) \"true\" else \"false\"".addAsSingleLineBody()
"if (this) \"true\" else \"false\"".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedEquals() {
"other is Boolean && kotlin.native.internal.areEqualByValue(this, other)".addAsSingleLineBody(bodyOnNewLine = true)
"other is Boolean && kotlin.native.internal.areEqualByValue(this, other)".setAsExpressionBody()
}
private fun ClassBuilder.generateCustomEquals() {
@@ -295,13 +295,13 @@ class NativeBooleanGenerator(writer: PrintWriter) : BooleanGenerator(writer) {
returnType = PrimitiveType.BOOLEAN.capitalized
}
"kotlin.native.internal.areEqualByValue(this, other)".addAsSingleLineBody(bodyOnNewLine = false)
"kotlin.native.internal.areEqualByValue(this, other)".setAsExpressionBody()
}
}
override fun MethodBuilder.modifyGeneratedHashCode() {
modifySignature { visibility = MethodVisibility.PUBLIC }
"if (this) 1231 else 1237".addAsSingleLineBody(bodyOnNewLine = true)
"if (this) 1231 else 1237".setAsExpressionBody()
}
override fun ClassBuilder.generateAdditionalMethods() {
+33 -33
View File
@@ -370,35 +370,35 @@ class JsCharGenerator(writer: PrintWriter) : CharGenerator(writer) {
}
override fun MethodBuilder.modifyGeneratedCompareTo() {
"value - other.value".addAsSingleLineBody(bodyOnNewLine = false)
"value - other.value".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedPlus() {
"(value + other).toChar()".addAsSingleLineBody(bodyOnNewLine = false)
"(value + other).toChar()".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedMinusChar() {
"value - other.value".addAsSingleLineBody(bodyOnNewLine = false)
"value - other.value".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedMinusInt() {
"(value - other).toChar()".addAsSingleLineBody(bodyOnNewLine = false)
"(value - other).toChar()".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedInc() {
"(value + 1).toChar()".addAsSingleLineBody(bodyOnNewLine = false)
"(value + 1).toChar()".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedDec() {
"(value - 1).toChar()".addAsSingleLineBody(bodyOnNewLine = false)
"(value - 1).toChar()".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedRangeTo() {
"CharRange(this, other)".addAsSingleLineBody(bodyOnNewLine = false)
"CharRange(this, other)".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedRangeUntil() {
"this until other".addAsSingleLineBody(bodyOnNewLine = false)
"this until other".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedConversions(otherKind: PrimitiveType) {
@@ -407,25 +407,25 @@ class JsCharGenerator(writer: PrintWriter) : CharGenerator(writer) {
PrimitiveType.INT -> "value"
else -> "value.$methodName()"
}
body.addAsSingleLineBody(bodyOnNewLine = false)
body.setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedEquals() {
"""
if (other !is Char) return false
return this.value == other.value
""".trimIndent().addAsMultiLineBody()
""".trimIndent().setAsBlockBody()
}
override fun MethodBuilder.modifyGeneratedToString() {
additionalComments = "TODO implicit usages of toString and valueOf must be covered in DCE"
annotations += "Suppress(\"JS_NAME_PROHIBITED_FOR_OVERRIDE\")"
annotations += "JsName(\"toString\")"
"return js(\"String\").fromCharCode(value).unsafeCast<String>()".addAsMultiLineBody()
"return js(\"String\").fromCharCode(value).unsafeCast<String>()".setAsBlockBody()
}
override fun MethodBuilder.modifyGeneratedHashCode() {
"value".addAsSingleLineBody(bodyOnNewLine = false)
"value".setAsExpressionBody()
}
}
@@ -489,40 +489,40 @@ class WasmCharGenerator(writer: PrintWriter) : CharGenerator(writer) {
}
override fun MethodBuilder.modifyGeneratedCompareTo() {
"wasm_i32_compareTo(this.code, other.code)".addAsSingleLineBody(bodyOnNewLine = true)
"wasm_i32_compareTo(this.code, other.code)".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedPlus() {
modifySignature { isInline = true }
"(this.code + other).toChar()".addAsSingleLineBody(bodyOnNewLine = true)
"(this.code + other).toChar()".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedMinusChar() {
modifySignature { isInline = true }
"(this.code - other.code)".addAsSingleLineBody(bodyOnNewLine = true)
"(this.code - other.code)".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedMinusInt() {
modifySignature { isInline = true }
"(this.code - other).toChar()".addAsSingleLineBody(bodyOnNewLine = true)
"(this.code - other).toChar()".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedInc() {
modifySignature { isInline = true }
"(this.code + 1).toChar()".addAsSingleLineBody(bodyOnNewLine = true)
"(this.code + 1).toChar()".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedDec() {
modifySignature { isInline = true }
"(this.code - 1).toChar()".addAsSingleLineBody(bodyOnNewLine = true)
"(this.code - 1).toChar()".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedRangeTo() {
"CharRange(this, other)".addAsSingleLineBody(bodyOnNewLine = true)
"CharRange(this, other)".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedRangeUntil() {
"this until other".addAsSingleLineBody(bodyOnNewLine = true)
"this until other".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedConversions(otherKind: PrimitiveType) {
@@ -535,7 +535,7 @@ class WasmCharGenerator(writer: PrintWriter) : CharGenerator(writer) {
}
else -> "this.code.$methodName()"
}
body.addAsSingleLineBody(bodyOnNewLine = true)
body.setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedEquals() {
@@ -543,7 +543,7 @@ class WasmCharGenerator(writer: PrintWriter) : CharGenerator(writer) {
if (other is Char)
return wasm_i32_eq(this.code, other.code)
return false
""".trimIndent().addAsMultiLineBody()
""".trimIndent().setAsBlockBody()
}
override fun MethodBuilder.modifyGeneratedToString() {
@@ -552,12 +552,12 @@ class WasmCharGenerator(writer: PrintWriter) : CharGenerator(writer) {
val array = WasmCharArray(1)
array.set(0, this)
return array.createString()
""".trimIndent().addAsMultiLineBody()
""".trimIndent().setAsBlockBody()
}
override fun MethodBuilder.modifyGeneratedHashCode() {
modifySignature { visibility = null }
"this.code.hashCode()".addAsSingleLineBody(bodyOnNewLine = true)
"this.code.hashCode()".setAsExpressionBody()
}
}
@@ -642,17 +642,17 @@ class NativeCharGenerator(writer: PrintWriter) : CharGenerator(writer) {
override fun MethodBuilder.modifyGeneratedPlus() {
modifySignature { isInline = true }
"(this.code + other).toChar()".addAsSingleLineBody(bodyOnNewLine = true)
"(this.code + other).toChar()".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedMinusChar() {
modifySignature { isInline = true }
"this.code - other.code".addAsSingleLineBody(bodyOnNewLine = true)
"this.code - other.code".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedMinusInt() {
modifySignature { isInline = true }
"(this.code - other).toChar()".addAsSingleLineBody(bodyOnNewLine = true)
"(this.code - other).toChar()".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedInc() {
@@ -664,11 +664,11 @@ class NativeCharGenerator(writer: PrintWriter) : CharGenerator(writer) {
}
override fun MethodBuilder.modifyGeneratedRangeTo() {
"CharRange(this, other)".addAsSingleLineBody(bodyOnNewLine = true)
"CharRange(this, other)".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedRangeUntil() {
"this until other".addAsSingleLineBody(bodyOnNewLine = true)
"this until other".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedConversions(otherKind: PrimitiveType) {
@@ -677,7 +677,7 @@ class NativeCharGenerator(writer: PrintWriter) : CharGenerator(writer) {
PrimitiveType.BYTE -> annotations += "TypedIntrinsic(IntrinsicType.INT_TRUNCATE)"
PrimitiveType.CHAR -> {
modifySignature { isInline = true }
"this".addAsSingleLineBody(bodyOnNewLine = true)
"this".setAsExpressionBody()
}
PrimitiveType.SHORT, PrimitiveType.INT, PrimitiveType.LONG -> annotations += "TypedIntrinsic(IntrinsicType.ZERO_EXTEND)"
PrimitiveType.FLOAT, PrimitiveType.DOUBLE -> annotations += "TypedIntrinsic(IntrinsicType.UNSIGNED_TO_FLOAT)"
@@ -686,7 +686,7 @@ class NativeCharGenerator(writer: PrintWriter) : CharGenerator(writer) {
}
override fun MethodBuilder.modifyGeneratedEquals() {
"other is Char && this.code == other.code".addAsSingleLineBody(bodyOnNewLine = true)
"other is Char && this.code == other.code".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedToString() {
@@ -707,12 +707,12 @@ class NativeCharGenerator(writer: PrintWriter) : CharGenerator(writer) {
returnType = PrimitiveType.BOOLEAN.capitalized
}
"this == other".addAsSingleLineBody(bodyOnNewLine = false)
"this == other".setAsExpressionBody()
}
}
override fun MethodBuilder.modifyGeneratedHashCode() {
"return this.code".addAsMultiLineBody()
"return this.code".setAsBlockBody()
}
override fun ClassBuilder.generateAdditionalMethods() {
@@ -54,10 +54,10 @@ class JsPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(write
"rem" -> "modulo"
else -> error("Unsupported binary operation: $methodName")
}
"this.$implMethod(other)".addAsSingleLineBody(false)
"this.$implMethod(other)".setAsExpressionBody()
} else {
modifySignature { isInline = true }
"this${thisKind.castToIfNecessary(otherKind)}.${methodName}(other${otherKind.castToIfNecessary(thisKind)})".addAsSingleLineBody(false)
"this${thisKind.castToIfNecessary(otherKind)}.${methodName}(other${otherKind.castToIfNecessary(thisKind)})".setAsExpressionBody()
}
}
}
@@ -73,7 +73,7 @@ class JsPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(write
"this"
}
else -> error(methodName)
}.addAsSingleLineBody(false)
}.setAsExpressionBody()
}
}
@@ -91,16 +91,16 @@ class JsPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(write
"ushr" -> "shiftRightUnsigned"
else -> error(methodName)
}
"$implMethod(bitCount)".addAsSingleLineBody(bodyOnNewLine = false)
"$implMethod(bitCount)".setAsExpressionBody()
}
}
override fun MethodBuilder.modifyGeneratedBitwiseOperators(thisKind: PrimitiveType) {
if (thisKind == PrimitiveType.LONG) {
if (methodName == "inv") {
"Long(low.inv(), high.inv())".addAsSingleLineBody(bodyOnNewLine = false)
"Long(low.inv(), high.inv())".setAsExpressionBody()
} else {
"Long(this.low $methodName other.low, this.high $methodName other.high)".addAsSingleLineBody(bodyOnNewLine = false)
"Long(this.low $methodName other.low, this.high $methodName other.high)".setAsExpressionBody()
}
}
}
@@ -116,19 +116,19 @@ class JsPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(write
PrimitiveType.FLOAT -> "toDouble().toFloat()"
PrimitiveType.DOUBLE -> "toNumber()"
else -> error("Unsupported type $otherKind for Long conversion")
}.addAsSingleLineBody(bodyOnNewLine = false)
}.setAsExpressionBody()
}
}
override fun MethodBuilder.modifyGeneratedEquals(thisKind: PrimitiveType) {
if (thisKind == PrimitiveType.LONG) {
"other is Long && equalsLong(other)".addAsSingleLineBody(false)
"other is Long && equalsLong(other)".setAsExpressionBody()
}
}
override fun MethodBuilder.modifyGeneratedToString(thisKind: PrimitiveType) {
if (thisKind == PrimitiveType.LONG) {
"this.toStringImpl(radix = 10)".addAsSingleLineBody(false)
"this.toStringImpl(radix = 10)".setAsExpressionBody()
}
}
@@ -140,7 +140,7 @@ class JsPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(write
returnType = PrimitiveType.INT.capitalized
}
if (thisKind == PrimitiveType.LONG) {
"hashCode(this)".addAsSingleLineBody(bodyOnNewLine = false)
"hashCode(this)".setAsExpressionBody()
}
}
@@ -160,7 +160,7 @@ class JsPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(write
methodName = "valueOf"
returnType = PrimitiveType.DOUBLE.capitalized
}
"toDouble()".addAsSingleLineBody(bodyOnNewLine = false)
"toDouble()".setAsExpressionBody()
}
}
}
@@ -56,7 +56,7 @@ class NativePrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(w
// Canonical NaN bit representation is higher than any other value's bit representation
return thisBits.compareTo(otherBits)
""".trimIndent().addAsMultiLineBody()
""".trimIndent().setAsBlockBody()
} else {
setAsExternal(thisKind)
}
@@ -70,7 +70,7 @@ class NativePrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(w
"-${otherCasted}.compareTo(this)"
} else {
"$thisCasted.compareTo($otherCasted)"
}.addAsSingleLineBody(bodyOnNewLine = true)
}.setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedBinaryOperation(thisKind: PrimitiveType, otherKind: PrimitiveType) {
@@ -84,7 +84,7 @@ class NativePrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(w
val returnTypeAsPrimitive = PrimitiveType.valueOf(returnType.uppercase())
val thisCasted = "this" + thisKind.castToIfNecessary(returnTypeAsPrimitive)
val otherCasted = parameterName + parameterType.toPrimitiveType().castToIfNecessary(returnTypeAsPrimitive)
"$thisCasted $sign $otherCasted".addAsSingleLineBody(bodyOnNewLine = true)
"$thisCasted $sign $otherCasted".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedUnaryOperation(thisKind: PrimitiveType) {
@@ -98,7 +98,7 @@ class NativePrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(w
val returnTypeAsPrimitive = PrimitiveType.valueOf(returnType.uppercase())
val thisCasted = "this" + thisKind.castToIfNecessary(returnTypeAsPrimitive)
val sign = if (methodName == "unaryMinus") "-" else ""
"$sign$thisCasted".addAsSingleLineBody(bodyOnNewLine = true)
"$sign$thisCasted".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedBitShiftOperators(thisKind: PrimitiveType) {
@@ -113,7 +113,7 @@ class NativePrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(w
when {
otherKind == thisKind -> {
modifySignature { isInline = true }
"this".addAsSingleLineBody(bodyOnNewLine = true)
"this".setAsExpressionBody()
}
thisKind !in PrimitiveType.floatingPoint -> {
modifySignature { isExternal = true }
@@ -127,7 +127,7 @@ class NativePrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(w
}
else -> {
if (otherKind in setOf(PrimitiveType.BYTE, PrimitiveType.SHORT, PrimitiveType.CHAR)) {
"this.toInt().to${returnType}()".addAsSingleLineBody(bodyOnNewLine = false)
"this.toInt().to${returnType}()".setAsExpressionBody()
return
}
@@ -153,7 +153,7 @@ class NativePrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(w
} else {
"kotlin.native.internal.areEqualByValue(this, $parameterName)"
}
"$parameterName is ${thisKind.capitalized} && $additionalCheck".addAsSingleLineBody(bodyOnNewLine = true)
"$parameterName is ${thisKind.capitalized} && $additionalCheck".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedToString(thisKind: PrimitiveType) {
@@ -166,7 +166,7 @@ class NativePrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(w
The exact bit pattern of a `NaN` ${thisKind.name.lowercase()} is not guaranteed to be preserved though.
""".trimIndent().let { appendDoc(it) }
"NumberConverter.convert(this)".addAsSingleLineBody(bodyOnNewLine = false)
"NumberConverter.convert(this)".setAsExpressionBody()
} else {
modifySignature { isExternal = true }
annotations += "GCUnsafeCall(\"Kotlin_${thisKind.capitalized}_toString\")"
@@ -194,7 +194,7 @@ class NativePrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(w
PrimitiveType.FLOAT -> "toBits()"
PrimitiveType.DOUBLE -> "toBits().hashCode()"
else -> "this${thisKind.castToIfNecessary(PrimitiveType.INT)}"
}.addAsSingleLineBody()
}.setAsExpressionBody()
}
}
@@ -212,8 +212,8 @@ class NativePrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(w
}
when (thisKind) {
in PrimitiveType.floatingPoint -> "toBits() == other.toBits()".addAsSingleLineBody(bodyOnNewLine = false)
else -> "kotlin.native.internal.areEqualByValue(this, other)".addAsSingleLineBody(bodyOnNewLine = false)
in PrimitiveType.floatingPoint -> "toBits() == other.toBits()".setAsExpressionBody()
else -> "kotlin.native.internal.areEqualByValue(this, other)".setAsExpressionBody()
}
}
}
@@ -67,7 +67,7 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
// Canonical NaN bit representation is higher than any other value's bit representation
return thisBits.compareTo(otherBits)
""".trimIndent().addAsMultiLineBody()
""".trimIndent().setAsBlockBody()
} else {
val body = when (thisKind) {
PrimitiveType.BYTE -> "wasm_i32_compareTo(this.toInt(), $parameterName.toInt())"
@@ -75,7 +75,7 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
PrimitiveType.INT, PrimitiveType.LONG -> "wasm_${thisKind.prefixLowercase}_compareTo(this, $parameterName)"
else -> throw IllegalArgumentException("Unsupported type $thisKind for generation `compareTo` method")
}
body.addAsSingleLineBody(bodyOnNewLine = true)
body.setAsExpressionBody()
}
return
}
@@ -85,7 +85,7 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
when {
thisKind == PrimitiveType.FLOAT && otherKind == PrimitiveType.DOUBLE -> "-${otherCasted}.compareTo(this)"
else -> "$thisCasted.compareTo($otherCasted)"
}.addAsSingleLineBody(bodyOnNewLine = true)
}.setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedBinaryOperation(thisKind: PrimitiveType, otherKind: PrimitiveType) {
@@ -107,7 +107,7 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
else -> return implementAsIntrinsic(thisKind, methodName)
}
else -> return implementAsIntrinsic(thisKind, methodName)
}.addAsSingleLineBody(bodyOnNewLine = true)
}.setAsExpressionBody()
return
}
@@ -115,7 +115,7 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
val returnTypeAsPrimitive = PrimitiveType.valueOf(returnType.uppercase())
val thisCasted = "this" + thisKind.castToIfNecessary(returnTypeAsPrimitive)
val otherCasted = parameterName + parameterType.toPrimitiveType().castToIfNecessary(returnTypeAsPrimitive)
"$thisCasted $sign $otherCasted".addAsSingleLineBody(bodyOnNewLine = true)
"$thisCasted $sign $otherCasted".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedUnaryOperation(thisKind: PrimitiveType) {
@@ -128,11 +128,11 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
if (methodName in setOf("inc", "dec")) {
val sign = if (methodName == "inc") "+" else "-"
when (thisKind) {
PrimitiveType.BYTE, PrimitiveType.SHORT -> "(this $sign 1).to${thisKind.capitalized}()".addAsSingleLineBody(bodyOnNewLine = true)
PrimitiveType.INT -> "this $sign 1".addAsSingleLineBody(bodyOnNewLine = true)
PrimitiveType.LONG -> "this $sign 1L".addAsSingleLineBody(bodyOnNewLine = true)
PrimitiveType.FLOAT -> "this $sign 1.0f".addAsSingleLineBody(bodyOnNewLine = true)
PrimitiveType.DOUBLE -> "this $sign 1.0".addAsSingleLineBody(bodyOnNewLine = true)
PrimitiveType.BYTE, PrimitiveType.SHORT -> "(this $sign 1).to${thisKind.capitalized}()".setAsExpressionBody()
PrimitiveType.INT -> "this $sign 1".setAsExpressionBody()
PrimitiveType.LONG -> "this $sign 1L".setAsExpressionBody()
PrimitiveType.FLOAT -> "this $sign 1.0f".setAsExpressionBody()
PrimitiveType.DOUBLE -> "this $sign 1.0".setAsExpressionBody()
else -> Unit
}
}
@@ -151,7 +151,7 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
else -> "-"
}
} else ""
"$sign$thisCasted".addAsSingleLineBody(bodyOnNewLine = true)
"$sign$thisCasted".setAsExpressionBody()
}
}
@@ -160,7 +160,7 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
implementAsIntrinsic(thisKind, methodName)
} else if (thisKind == PrimitiveType.LONG) {
modifySignature { isInline = true }
"wasm_i64_${methodName.toWasmOperator().lowercase()}(this, ${parameterName}.toLong())".addAsSingleLineBody(bodyOnNewLine = true)
"wasm_i64_${methodName.toWasmOperator().lowercase()}(this, ${parameterName}.toLong())".setAsExpressionBody()
}
}
@@ -168,7 +168,7 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
if (methodName == "inv") {
modifySignature { isInline = true }
val oneConst = if (thisKind == PrimitiveType.LONG) "-1L" else "-1"
"this.xor($oneConst)".addAsSingleLineBody(bodyOnNewLine = true)
"this.xor($oneConst)".setAsExpressionBody()
return
}
@@ -178,7 +178,7 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
override fun MethodBuilder.modifyGeneratedConversions(thisKind: PrimitiveType, otherKind: PrimitiveType) {
if (otherKind == thisKind) {
modifySignature { isInline = true }
"this".addAsSingleLineBody(bodyOnNewLine = true)
"this".setAsExpressionBody()
return
}
@@ -221,7 +221,7 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
else -> throw IllegalArgumentException("Unsupported type $otherKind for generation conversion method from type $thisKind")
}
else -> throw IllegalArgumentException("Unsupported type $thisKind to generate conversion methods")
}.addAsSingleLineBody(bodyOnNewLine = false)
}.setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedEquals(thisKind: PrimitiveType) {
@@ -232,7 +232,7 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
"wasm_i32_eq(this${thisKind.castToIfNecessary(PrimitiveType.INT)}, $parameterName${thisKind.castToIfNecessary(PrimitiveType.INT)})"
}
}
"$parameterName is ${thisKind.capitalized} && $additionalCheck".addAsSingleLineBody(bodyOnNewLine = true)
"$parameterName is ${thisKind.capitalized} && $additionalCheck".setAsExpressionBody()
}
override fun MethodBuilder.modifyGeneratedToString(thisKind: PrimitiveType) {
@@ -240,7 +240,7 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
in PrimitiveType.floatingPoint -> "dtoa(this${thisKind.castToIfNecessary(PrimitiveType.DOUBLE)})"
PrimitiveType.INT, PrimitiveType.LONG -> "itoa${thisKind.bitSize}(this, 10)"
else -> "this.toInt().toString()"
}.addAsSingleLineBody(bodyOnNewLine = true)
}.setAsExpressionBody()
}
override fun ClassBuilder.generateAdditionalMethods(thisKind: PrimitiveType) {
@@ -267,7 +267,7 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
PrimitiveType.FLOAT -> "toBits()"
PrimitiveType.DOUBLE -> "toBits().hashCode()"
else -> "this${thisKind.castToIfNecessary(PrimitiveType.INT)}"
}.addAsSingleLineBody()
}.setAsExpressionBody()
}
}
@@ -280,7 +280,7 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
methodName = "reinterpretAs${otherKind.capitalized}"
returnType = otherKind.capitalized
}
implementedAsIntrinsic.addAsSingleLineBody(bodyOnNewLine = true)
implementedAsIntrinsic.setAsExpressionBody()
}
}
@@ -305,7 +305,7 @@ class WasmPrimitivesGenerator(writer: PrintWriter) : BasePrimitivesGenerator(wri
internal fun MethodBuilder.implementAsIntrinsic(thisKind: PrimitiveType, methodName: String) {
modifySignature { isInline = false }
annotations += "WasmOp(WasmOp.${thisKind.prefixUppercase}_${methodName.toWasmOperator()})"
implementedAsIntrinsic.addAsSingleLineBody(bodyOnNewLine = true)
implementedAsIntrinsic.setAsExpressionBody()
}
private val PrimitiveType.prefixUppercase: String
+3 -4
View File
@@ -359,12 +359,11 @@ 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"
fun String.setAsExpressionBody() {
body = " =$END_LINE $this"
}
fun String.addAsMultiLineBody() {
fun String.setAsBlockBody() {
body = " {$END_LINE${this.shift()}$END_LINE}"
}
}