Generate constant "" for effectively empty string

This commit is contained in:
Dmitry Petrov
2017-07-14 18:27:31 +03:00
parent 389c7d03b7
commit 8c7352e668
3 changed files with 28 additions and 10 deletions
@@ -831,24 +831,29 @@ public class ExpressionCodegen extends KtVisitor<StackValue, StackValue> impleme
public StackValue visitStringTemplateExpression(@NotNull KtStringTemplateExpression expression, StackValue receiver) {
List<StringTemplateEntry> entries = preprocessStringTemplate(expression);
if (entries.size() == 1) {
Type type = expressionType(expression);
if (entries.size() == 0) {
return StackValue.constant("", type);
}
else if (entries.size() == 1) {
StringTemplateEntry entry = entries.get(0);
if (entry instanceof StringTemplateEntry.Expression) {
KtExpression expr = ((StringTemplateEntry.Expression) entry).expression;
return genToString(gen(expr), expressionType(expr));
return genToString(gen(expr), type);
}
else {
Type type = expressionType(expression);
return StackValue.constant(((StringTemplateEntry.Constant) entry).value, type);
}
}
return StackValue.operation(JAVA_STRING_TYPE, v -> {
genStringBuilderConstructor(v);
invokeAppendForEntries(v, entries);
v.invokevirtual("java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
return Unit.INSTANCE;
});
else {
return StackValue.operation(type, v -> {
genStringBuilderConstructor(v);
invokeAppendForEntries(v, entries);
v.invokevirtual("java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
return Unit.INSTANCE;
});
}
}
private void invokeAppendForEntries(InstructionAdapter v, List<StringTemplateEntry> entries) {
@@ -0,0 +1,7 @@
const val empty = ""
val test1 = ""
val test2 = "abc$empty"
val test3 = "$empty$empty$empty"
// 0 StringBuilder
@@ -2090,6 +2090,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
doTest(fileName);
}
@TestMetadata("kt19037.kt")
public void testKt19037() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/stringOperations/kt19037.kt");
doTest(fileName);
}
@TestMetadata("nestedConcat.kt")
public void testNestedConcat() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/stringOperations/nestedConcat.kt");