Single-branch if-statements
Single-branch if-expressions are now compiled to statements if the result is not used #KT-3036 In Progress
This commit is contained in:
@@ -32,4 +32,9 @@ public class CodegenStatementVisitor extends JetVisitor<StackValue, StackValue>
|
|||||||
public StackValue visitJetElement(JetElement element, StackValue receiver) {
|
public StackValue visitJetElement(JetElement element, StackValue receiver) {
|
||||||
return element.accept(codegen, receiver);
|
return element.accept(codegen, receiver);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StackValue visitIfExpression(JetIfExpression expression, StackValue receiver) {
|
||||||
|
return codegen.generateIfExpression(expression, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -358,6 +358,10 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public StackValue visitIfExpression(JetIfExpression expression, StackValue receiver) {
|
public StackValue visitIfExpression(JetIfExpression expression, StackValue receiver) {
|
||||||
|
return generateIfExpression(expression, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* package */ StackValue generateIfExpression(JetIfExpression expression, boolean isStatement) {
|
||||||
Type asmType = expressionType(expression);
|
Type asmType = expressionType(expression);
|
||||||
|
|
||||||
JetExpression thenExpression = expression.getThen();
|
JetExpression thenExpression = expression.getThen();
|
||||||
@@ -376,12 +380,12 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
|||||||
return StackValue.onStack(asmType);
|
return StackValue.onStack(asmType);
|
||||||
}
|
}
|
||||||
StackValue condition = gen(expression.getCondition());
|
StackValue condition = gen(expression.getCondition());
|
||||||
return generateSingleBranchIf(condition, elseExpression, false);
|
return generateSingleBranchIf(condition, expression, elseExpression, false, isStatement);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (isEmptyExpression(elseExpression)) {
|
if (isEmptyExpression(elseExpression)) {
|
||||||
StackValue condition = gen(expression.getCondition());
|
StackValue condition = gen(expression.getCondition());
|
||||||
return generateSingleBranchIf(condition, thenExpression, true);
|
return generateSingleBranchIf(condition, expression, thenExpression, true, isStatement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -967,27 +971,37 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
|
|||||||
throw new UnsupportedOperationException();
|
throw new UnsupportedOperationException();
|
||||||
}
|
}
|
||||||
|
|
||||||
private StackValue generateSingleBranchIf(StackValue condition, JetExpression expression, boolean inverse) {
|
private StackValue generateSingleBranchIf(
|
||||||
Type expressionType = expressionType(expression);
|
StackValue condition,
|
||||||
Type targetType = expressionType;
|
JetIfExpression ifExpression,
|
||||||
if (!expressionType.equals(JET_TUPLE0_TYPE)) {
|
JetExpression expression,
|
||||||
targetType = OBJECT_TYPE;
|
boolean inverse,
|
||||||
}
|
boolean isStatement
|
||||||
|
) {
|
||||||
Label elseLabel = new Label();
|
Label elseLabel = new Label();
|
||||||
condition.condJump(elseLabel, inverse, v);
|
condition.condJump(elseLabel, inverse, v);
|
||||||
|
|
||||||
gen(expression, expressionType);
|
if (isStatement) {
|
||||||
StackValue.coerce(expressionType, targetType, v);
|
gen(expression, Type.VOID_TYPE);
|
||||||
|
v.mark(elseLabel);
|
||||||
|
return StackValue.none();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Type type = expressionType(expression);
|
||||||
|
Type targetType = type.equals(JET_TUPLE0_TYPE) ? type : OBJECT_TYPE;
|
||||||
|
|
||||||
Label end = new Label();
|
gen(expression, targetType);
|
||||||
v.goTo(end);
|
|
||||||
|
|
||||||
v.mark(elseLabel);
|
Label end = new Label();
|
||||||
StackValue.putTuple0Instance(v);
|
v.goTo(end);
|
||||||
|
|
||||||
v.mark(end);
|
markLineNumber(ifExpression);
|
||||||
return StackValue.onStack(targetType);
|
v.mark(elseLabel);
|
||||||
|
StackValue.putTuple0Instance(v);
|
||||||
|
|
||||||
|
v.mark(end);
|
||||||
|
return StackValue.onStack(targetType);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
fun foo() {
|
||||||
|
if (0 < 1) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (1 < 2) else {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fun foo() {
|
||||||
|
if (0 < 1) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
val u: Unit = if (0 < 1) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2 3 6 7 6
|
||||||
@@ -295,6 +295,10 @@ public class LineNumberTest extends TestCaseWithTmpdir {
|
|||||||
doTest();
|
doTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testIfThen() {
|
||||||
|
doTestCustom();
|
||||||
|
}
|
||||||
|
|
||||||
public void testStaticDelegate() {
|
public void testStaticDelegate() {
|
||||||
JetFile foo = createPsiFile("staticDelegate/foo.kt");
|
JetFile foo = createPsiFile("staticDelegate/foo.kt");
|
||||||
JetFile bar = createPsiFile("staticDelegate/bar.kt");
|
JetFile bar = createPsiFile("staticDelegate/bar.kt");
|
||||||
|
|||||||
@@ -36,4 +36,8 @@ public class StatementGenTest extends CodegenTestCase {
|
|||||||
private void assertNoGetStatic(@NotNull String text) {
|
private void assertNoGetStatic(@NotNull String text) {
|
||||||
assertFalse(text, text.toLowerCase().contains("getstatic"));
|
assertFalse(text, text.toLowerCase().contains("getstatic"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testIfSingleBranch() {
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user