Search in sources :

Example 1 with IfStatement

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

the class ConstantBranchPruner method endVisit.

@Override
public void endVisit(IfStatement node) {
    Expression expr = node.getExpression();
    Boolean value = getKnownValue(expr);
    if (value != null) {
        Statement sideEffects = getSideEffects(expr);
        if (sideEffects != null) {
            TreeUtil.insertBefore(node, sideEffects);
        }
        if (value) {
            node.replaceWith(TreeUtil.remove(node.getThenStatement()));
        } else if (node.getElseStatement() != null) {
            node.replaceWith(TreeUtil.remove(node.getElseStatement()));
        } else {
            node.remove();
        }
    }
}
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) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement)

Example 2 with IfStatement

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

the class UnsequencedExpressionRewriter method extractInfixConditional.

private void extractInfixConditional(List<Statement> stmtList, InfixExpression conditional, List<VariableAccess> toExtract) {
    InfixExpression.Operator op = conditional.getOperator();
    List<Expression> branches = conditional.getOperands();
    int lastIfExtractIdx = 0;
    VariableElement conditionalVar = null;
    int lastExtracted = 0;
    Expression lastBranch = null;
    for (int i = 0; i < toExtract.size(); i++) {
        VariableAccess access = toExtract.get(i);
        TreeNode node = access.expression;
        while (node.getParent() != conditional) {
            node = node.getParent();
        }
        assert node instanceof Expression;
        Expression branch = (Expression) node;
        // Extract all accesses from the previous branch.
        if (lastBranch != null && branch != lastBranch) {
            extractOrderedAccesses(stmtList, lastBranch, toExtract.subList(lastExtracted, i));
            lastExtracted = i;
        }
        lastBranch = branch;
        // If there's a new access in a new branch, then we extract an if-statement.
        if (branch != branches.get(lastIfExtractIdx)) {
            TypeMirror boolType = typeUtil.getBoolean();
            if (conditionalVar == null) {
                conditionalVar = GeneratedVariableElement.newLocalVar("unseq$" + count++, boolType, currentMethod);
                conditional.replaceWith(new SimpleName(conditionalVar));
                stmtList.add(new VariableDeclarationStatement(conditionalVar, null));
            }
            List<Expression> subBranches = branches.subList(lastIfExtractIdx, branches.indexOf(branch));
            IfStatement newIf = new IfStatement();
            Expression ifExpr = new Assignment(new SimpleName(conditionalVar), conditionalFromSubBranches(subBranches, op));
            if (op == InfixExpression.Operator.CONDITIONAL_OR) {
                ifExpr = new PrefixExpression(boolType, PrefixExpression.Operator.NOT, ParenthesizedExpression.parenthesize(ifExpr));
            }
            newIf.setExpression(ifExpr);
            stmtList.add(newIf);
            Block thenBlock = new Block();
            stmtList = thenBlock.getStatements();
            newIf.setThenStatement(thenBlock);
            lastIfExtractIdx = branches.indexOf(branch);
        }
    }
    extractOrderedAccesses(stmtList, lastBranch, toExtract.subList(lastExtracted, toExtract.size()));
    if (conditionalVar != null) {
        List<Expression> remainingBranches = Lists.newArrayList();
        remainingBranches.add(new SimpleName(conditionalVar));
        remainingBranches.addAll(branches.subList(lastIfExtractIdx, branches.size()));
        stmtList.add(new ExpressionStatement(new Assignment(new SimpleName(conditionalVar), conditionalFromSubBranches(remainingBranches, op))));
    }
}
Also used : SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) Assignment(com.google.devtools.j2objc.ast.Assignment) IfStatement(com.google.devtools.j2objc.ast.IfStatement) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) TypeMirror(javax.lang.model.type.TypeMirror) TreeNode(com.google.devtools.j2objc.ast.TreeNode) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 3 with IfStatement

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

the class GwtConverter method visit.

@Override
public boolean visit(IfStatement node) {
    if (isGwtTest(node.getExpression())) {
        if (node.getElseStatement() != null) {
            // Replace this node with the else statement.
            Statement replacement = TreeUtil.remove(node.getElseStatement());
            node.replaceWith(replacement);
            replacement.accept(this);
        } else {
            // with an empty statement.
            if (node.getParent() instanceof Block) {
                node.remove();
            } else {
                node.replaceWith(new EmptyStatement());
            }
        }
        return false;
    }
    return true;
}
Also used : IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 4 with IfStatement

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

the class UnsequencedExpressionRewriter method createLoopTermination.

private IfStatement createLoopTermination(Expression loopCondition) {
    IfStatement newIf = new IfStatement();
    newIf.setExpression(new PrefixExpression(typeUtil.getBoolean(), PrefixExpression.Operator.NOT, ParenthesizedExpression.parenthesize(loopCondition.copy())));
    newIf.setThenStatement(new BreakStatement());
    return newIf;
}
Also used : BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression)

Example 5 with IfStatement

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

the class UnsequencedExpressionRewriter method visit.

@Override
public boolean visit(IfStatement node) {
    visitAndExtract(node.getExpression(), node);
    node.getThenStatement().accept(this);
    Statement elseStmt = node.getElseStatement();
    if (elseStmt != null) {
        elseStmt.accept(this);
    }
    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)

Aggregations

IfStatement (com.google.devtools.j2objc.ast.IfStatement)7 Statement (com.google.devtools.j2objc.ast.Statement)5 Block (com.google.devtools.j2objc.ast.Block)4 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)4 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)4 BreakStatement (com.google.devtools.j2objc.ast.BreakStatement)3 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)3 Expression (com.google.devtools.j2objc.ast.Expression)3 ForStatement (com.google.devtools.j2objc.ast.ForStatement)3 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)3 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)3 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)3 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)3 WhileStatement (com.google.devtools.j2objc.ast.WhileStatement)3 AssertStatement (com.google.devtools.j2objc.ast.AssertStatement)2 Assignment (com.google.devtools.j2objc.ast.Assignment)2 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)2 DoStatement (com.google.devtools.j2objc.ast.DoStatement)2 EmptyStatement (com.google.devtools.j2objc.ast.EmptyStatement)2 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)2