simple way to generate short-circuit boolean && and ||
This commit is contained in:
@@ -497,12 +497,17 @@ public class ExpressionCodegen extends JetVisitor {
|
|||||||
final IElementType opToken = expression.getOperationReference().getReferencedNameElementType();
|
final IElementType opToken = expression.getOperationReference().getReferencedNameElementType();
|
||||||
if (opToken == JetTokens.EQ) {
|
if (opToken == JetTokens.EQ) {
|
||||||
generateAssignmentExpression(expression);
|
generateAssignmentExpression(expression);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
if (JetTokens.AUGMENTED_ASSIGNMENTS.contains(opToken)) {
|
else if (JetTokens.AUGMENTED_ASSIGNMENTS.contains(opToken)) {
|
||||||
generateAugmentedAssignment(expression);
|
generateAugmentedAssignment(expression);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
else if (opToken == JetTokens.ANDAND) {
|
||||||
|
generateBooleanAnd(expression);
|
||||||
|
}
|
||||||
|
else if (opToken == JetTokens.OROR) {
|
||||||
|
generateBooleanOr(expression);
|
||||||
|
}
|
||||||
|
else {
|
||||||
DeclarationDescriptor op = bindingContext.resolveReferenceExpression(expression.getOperationReference());
|
DeclarationDescriptor op = bindingContext.resolveReferenceExpression(expression.getOperationReference());
|
||||||
if (op instanceof FunctionDescriptor) {
|
if (op instanceof FunctionDescriptor) {
|
||||||
JetType returnType = bindingContext.getExpressionType(expression);
|
JetType returnType = bindingContext.getExpressionType(expression);
|
||||||
@@ -534,6 +539,33 @@ public class ExpressionCodegen extends JetVisitor {
|
|||||||
}
|
}
|
||||||
throw new UnsupportedOperationException("Don't know how to generate binary op " + expression);
|
throw new UnsupportedOperationException("Don't know how to generate binary op " + expression);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateBooleanAnd(JetBinaryExpression expression) {
|
||||||
|
gen(expression.getLeft(), Type.BOOLEAN_TYPE);
|
||||||
|
Label ifFalse = new Label();
|
||||||
|
v.ifeq(ifFalse);
|
||||||
|
gen(expression.getRight(), Type.BOOLEAN_TYPE);
|
||||||
|
Label end = new Label();
|
||||||
|
v.goTo(end);
|
||||||
|
v.mark(ifFalse);
|
||||||
|
v.aconst(false);
|
||||||
|
v.mark(end);
|
||||||
|
myStack.push(StackValue.onStack(Type.BOOLEAN_TYPE));
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateBooleanOr(JetBinaryExpression expression) {
|
||||||
|
gen(expression.getLeft(), Type.BOOLEAN_TYPE);
|
||||||
|
Label ifTrue = new Label();
|
||||||
|
v.ifne(ifTrue);
|
||||||
|
gen(expression.getRight(), Type.BOOLEAN_TYPE);
|
||||||
|
Label end = new Label();
|
||||||
|
v.goTo(end);
|
||||||
|
v.mark(ifTrue);
|
||||||
|
v.aconst(true);
|
||||||
|
v.mark(end);
|
||||||
|
myStack.push(StackValue.onStack(Type.BOOLEAN_TYPE));
|
||||||
|
}
|
||||||
|
|
||||||
private static boolean isNumberPrimitive(DeclarationDescriptor descriptor) {
|
private static boolean isNumberPrimitive(DeclarationDescriptor descriptor) {
|
||||||
if (!(descriptor instanceof ClassDescriptor)) {
|
if (!(descriptor instanceof ClassDescriptor)) {
|
||||||
|
|||||||
@@ -451,6 +451,36 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
|
|||||||
assertEquals(0, main.invoke(null, 4));
|
assertEquals(0, main.invoke(null, 4));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testAnd() throws Exception {
|
||||||
|
loadText("fun foo(a : Int): Boolean = a > 0 && a/0 > 0");
|
||||||
|
final Method main = generateFunction();
|
||||||
|
assertEquals(false, main.invoke(null, 0));
|
||||||
|
boolean hadException = false;
|
||||||
|
try {
|
||||||
|
main.invoke(null, 5);
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
if (e.getTargetException() instanceof ArithmeticException) {
|
||||||
|
hadException = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTrue(hadException);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testOr() throws Exception {
|
||||||
|
loadText("fun foo(a : Int): Boolean = a > 0 || a/0 > 0");
|
||||||
|
final Method main = generateFunction();
|
||||||
|
assertEquals(true, main.invoke(null, 5));
|
||||||
|
boolean hadException = false;
|
||||||
|
try {
|
||||||
|
main.invoke(null, 0);
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
if (e.getTargetException() instanceof ArithmeticException) {
|
||||||
|
hadException = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTrue(hadException);
|
||||||
|
}
|
||||||
|
|
||||||
private void binOpTest(final String text, final Object arg1, final Object arg2, final Object expected) throws Exception {
|
private void binOpTest(final String text, final Object arg1, final Object arg2, final Object expected) throws Exception {
|
||||||
loadText(text);
|
loadText(text);
|
||||||
System.out.println(generateToText());
|
System.out.println(generateToText());
|
||||||
|
|||||||
Reference in New Issue
Block a user