Search in sources :

Example 1 with BooleanLiteral

use of com.google.devtools.j2objc.ast.BooleanLiteral in project j2objc by google.

the class UnsequencedExpressionRewriter method visit.

@Override
public boolean visit(DoStatement node) {
    node.getBody().accept(this);
    newExpression(node.getExpression());
    node.getExpression().accept(this);
    List<VariableAccess> toExtract = getUnsequencedAccesses();
    if (!toExtract.isEmpty()) {
        // Convert "while (cond)" into "while (true) { if (!(cond)) break; ... }".
        List<Statement> stmtList = TreeUtil.asStatementList(node.getBody());
        extractOrderedAccesses(stmtList, currentTopNode, toExtract);
        stmtList.add(createLoopTermination(node.getExpression()));
        node.setExpression(new BooleanLiteral(true, typeUtil));
    }
    return false;
}
Also used : ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) AssertStatement(com.google.devtools.j2objc.ast.AssertStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) SynchronizedStatement(com.google.devtools.j2objc.ast.SynchronizedStatement) DoStatement(com.google.devtools.j2objc.ast.DoStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) ThrowStatement(com.google.devtools.j2objc.ast.ThrowStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) BooleanLiteral(com.google.devtools.j2objc.ast.BooleanLiteral)

Example 2 with BooleanLiteral

use of com.google.devtools.j2objc.ast.BooleanLiteral in project j2objc by google.

the class UnsequencedExpressionRewriter method visit.

@Override
public boolean visit(WhileStatement node) {
    node.getBody().accept(this);
    newExpression(node.getExpression());
    node.getExpression().accept(this);
    List<VariableAccess> toExtract = getUnsequencedAccesses();
    if (!toExtract.isEmpty()) {
        // Convert "while (cond)" into "while (true) { if (!(cond)) break; ... }".
        List<Statement> stmtList = TreeUtil.asStatementList(node.getBody()).subList(0, 0);
        extractOrderedAccesses(stmtList, currentTopNode, toExtract);
        stmtList.add(createLoopTermination(node.getExpression()));
        node.setExpression(new BooleanLiteral(true, typeUtil));
    }
    return false;
}
Also used : ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) AssertStatement(com.google.devtools.j2objc.ast.AssertStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) SynchronizedStatement(com.google.devtools.j2objc.ast.SynchronizedStatement) DoStatement(com.google.devtools.j2objc.ast.DoStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) ThrowStatement(com.google.devtools.j2objc.ast.ThrowStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) BooleanLiteral(com.google.devtools.j2objc.ast.BooleanLiteral)

Example 3 with BooleanLiteral

use of com.google.devtools.j2objc.ast.BooleanLiteral in project j2objc by google.

the class ConstantBranchPruner method endVisit.

@Override
public void endVisit(InfixExpression node) {
    InfixExpression.Operator operator = node.getOperator();
    if (operator != CONDITIONAL_AND && operator != CONDITIONAL_OR) {
        return;
    }
    List<Expression> operands = node.getOperands();
    int lastSideEffect = -1;
    for (int i = 0; i < operands.size(); i++) {
        Expression expr = operands.get(i);
        if (TranslationUtil.hasSideEffect(expr)) {
            lastSideEffect = i;
        }
        Boolean knownVal = getKnownValue(expr);
        if (knownVal == null) {
            continue;
        }
        if (knownVal == (operator == CONDITIONAL_OR)) {
            // Whole expression evaluates to 'knownVal'.
            operands.subList(lastSideEffect + 1, operands.size()).clear();
            if (lastSideEffect < i) {
                operands.add(new BooleanLiteral(knownVal, typeUtil));
            }
            break;
        } else if (lastSideEffect < i) {
            // Else remove unnecessary constant value.
            operands.remove(i--);
        }
    }
    if (operands.size() == 0) {
        if (operator == CONDITIONAL_OR) {
            // All constants must have been false, because a true value would have
            // caused us to return in the loop above.
            node.replaceWith(new BooleanLiteral(false, typeUtil));
        } else {
            // Likewise, all constants must have been true.
            node.replaceWith(new BooleanLiteral(true, typeUtil));
        }
    } else if (operands.size() == 1) {
        node.replaceWith(operands.remove(0));
    }
}
Also used : Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) BooleanLiteral(com.google.devtools.j2objc.ast.BooleanLiteral) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression)

Example 4 with BooleanLiteral

use of com.google.devtools.j2objc.ast.BooleanLiteral in project j2objc by google.

the class GwtConverter method visit.

@Override
public boolean visit(MethodInvocation node) {
    ExecutableElement method = node.getExecutableElement();
    List<Expression> args = node.getArguments();
    if (ElementUtil.getName(method).equals("create") && ElementUtil.getQualifiedName(ElementUtil.getDeclaringClass(method)).equals(GWT_CLASS) && args.size() == 1) {
        // Convert GWT.create(Foo.class) to Foo.class.newInstance().
        ExecutableElement newMethod = ElementUtil.findMethod(typeUtil.getJavaClass(), "newInstance");
        Expression clazz = args.remove(0);
        node.setExpression(clazz);
        node.setExecutablePair(new ExecutablePair(newMethod));
    } else if (isGwtTest(node)) {
        node.replaceWith(new BooleanLiteral(false, typeUtil));
    }
    return true;
}
Also used : Expression(com.google.devtools.j2objc.ast.Expression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) BooleanLiteral(com.google.devtools.j2objc.ast.BooleanLiteral) ExecutableElement(javax.lang.model.element.ExecutableElement)

Aggregations

BooleanLiteral (com.google.devtools.j2objc.ast.BooleanLiteral)4 AssertStatement (com.google.devtools.j2objc.ast.AssertStatement)2 BreakStatement (com.google.devtools.j2objc.ast.BreakStatement)2 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)2 DoStatement (com.google.devtools.j2objc.ast.DoStatement)2 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)2 Expression (com.google.devtools.j2objc.ast.Expression)2 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)2 ForStatement (com.google.devtools.j2objc.ast.ForStatement)2 IfStatement (com.google.devtools.j2objc.ast.IfStatement)2 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)2 Statement (com.google.devtools.j2objc.ast.Statement)2 SwitchStatement (com.google.devtools.j2objc.ast.SwitchStatement)2 SynchronizedStatement (com.google.devtools.j2objc.ast.SynchronizedStatement)2 ThrowStatement (com.google.devtools.j2objc.ast.ThrowStatement)2 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)2 WhileStatement (com.google.devtools.j2objc.ast.WhileStatement)2 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)1 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)1 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)1