Flatten nested string concatenation expressions into a single IrStringConcatenation, in a separate lowering phase.

Consolidating these into IrStringConcatenations allows the backend to produce efficient code for string concatenations (e.g., using StringBuilder for JVM).
This commit is contained in:
Mark Punzalan
2019-02-22 11:31:24 -08:00
committed by max-kammerer
parent ae22bdee15
commit d4d5a6011c
8 changed files with 212 additions and 14 deletions

View File

@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
class A() {
override fun toString(): String {

View File

@@ -1,16 +1,41 @@
// IGNORE_BACKEND: JVM_IR
// Uses 1 StringBuilder
fun test1(s1: String, s2: String, s3: String) =
(s1 + s2) + s3
// Uses 1 StringBuilder
fun test2(s1: String, s2: String, s3: String) =
s1 + (s2 + s3)
// Uses 1 StringBuilder
fun test3(s1: String, s2: String, s3: String, s4: String) =
((s1 + s2) + ((s3 + s4)))
// Combination of String.plus and string literal
// Uses 1 StringBuilder
fun test4(s1: String, s2: String, s3: String) =
"s1: $s1; " +
"s2: $s2; " +
"s3: $s3"
// 4 NEW java/lang/StringBuilder
// Combination of String.plus and nested string literal
// Uses 1 StringBuilder
fun test5(s1: String, s2: String, s3: String) =
"${"s1:" + "${" " + s1};"}; " +
"${"s2:" + "${" " + s2};"}; " +
"${"s3:" + "${" " + s3}"}"
// Top-level string concatenation element is a string literal
// Uses 1 StringBuilder
fun test6(s1: String, s2: String, s3: String) =
"${"s1:" + "${" " + s1};"} ${"s2:" + "${" " + s2};"} ${"s3:" + "${" " + s3}"}"
// Uses 3 StringBuilders:
// - In return expression
// - In argument to 1st call to foo()
// - In argument to 2nd call to foo() inside string literal
fun test7(s1: String, s2: String, s3: String): String {
fun foo(s: String) = s
return "foo: " + foo(s1 + s2 + " ${foo("\${s3.length} = ${s3.length}")}")
}
// 9 NEW java/lang/StringBuilder

View File

@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
class A() {
override fun toString(): String {

View File

@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
fun test(b: Byte, s: Short, i: Int, l: Long) {
"$b"
"$s"