Search in sources :

Example 1 with ForStatement

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

the class Rewriter method endVisit.

@Override
public void endVisit(ForStatement node) {
    // nodes in the initializers.
    if (node.getInitializers().size() == 1) {
        Object initializer = node.getInitializer(0);
        if (initializer instanceof VariableDeclarationExpression) {
            List<VariableDeclarationFragment> fragments = ((VariableDeclarationExpression) initializer).getFragments();
            for (VariableDeclarationFragment fragment : fragments) {
                if (ElementUtil.hasAnnotation(fragment.getVariableElement(), AutoreleasePool.class)) {
                    Statement loopBody = node.getBody();
                    if (!(loopBody instanceof Block)) {
                        Block block = new Block();
                        node.setBody(block);
                        block.addStatement(loopBody);
                    }
                    ((Block) node.getBody()).setHasAutoreleasePool(true);
                }
            }
        }
    }
}
Also used : VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) ThrowStatement(com.google.devtools.j2objc.ast.ThrowStatement) TryStatement(com.google.devtools.j2objc.ast.TryStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) Block(com.google.devtools.j2objc.ast.Block)

Example 2 with ForStatement

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

the class UnsequencedExpressionRewriter method visit.

@Override
public boolean visit(ForStatement node) {
    List<Expression> initializers = node.getInitializers();
    // expression or a list of initializer expressions.
    if (initializers.size() == 1 && initializers.get(0) instanceof VariableDeclarationExpression) {
        VariableDeclarationExpression decl = (VariableDeclarationExpression) initializers.get(0);
        extractVariableDeclarationFragments(decl.getFragments(), TreeUtil.asStatementList(node).subList(0, 0));
    } else {
        extractExpressionList(initializers, TreeUtil.asStatementList(node).subList(0, 0), false);
    }
    Expression expr = node.getExpression();
    if (expr != null) {
        newExpression(expr);
        expr.accept(this);
        List<VariableAccess> toExtract = getUnsequencedAccesses();
        if (!toExtract.isEmpty()) {
            // Convert "if (;cond;)" into "if (;;) { if (!(cond)) break; ...}".
            List<Statement> stmtList = TreeUtil.asStatementList(node.getBody()).subList(0, 0);
            extractOrderedAccesses(stmtList, currentTopNode, toExtract);
            stmtList.add(createLoopTermination(node.getExpression()));
            node.setExpression(null);
        }
    }
    extractExpressionList(node.getUpdaters(), TreeUtil.asStatementList(node.getBody()), true);
    node.getBody().accept(this);
    return false;
}
Also used : 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) 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) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression)

Example 3 with ForStatement

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

the class RewriterTest method testLabeledContinue.

public void testLabeledContinue() throws IOException {
    List<Statement> stmts = translateStatements("int i = 0; outer: for (; i < 10; i++) { " + "for (int j = 0; j < 10; j++) { continue outer; }}");
    assertEquals(2, stmts.size());
    Statement s = stmts.get(1);
    // not LabeledStatement
    assertTrue(s instanceof ForStatement);
    ForStatement fs = (ForStatement) s;
    Statement forStmt = fs.getBody();
    assertTrue(forStmt instanceof Block);
    stmts = ((Block) forStmt).getStatements();
    assertEquals(2, stmts.size());
    Statement lastStmt = stmts.get(1);
    assertTrue(lastStmt instanceof LabeledStatement);
    assertTrue(((LabeledStatement) lastStmt).getBody() instanceof EmptyStatement);
}
Also used : LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) Block(com.google.devtools.j2objc.ast.Block) ForStatement(com.google.devtools.j2objc.ast.ForStatement)

Example 4 with ForStatement

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

the class RewriterTest method testLabeledBreak.

public void testLabeledBreak() throws IOException {
    List<Statement> stmts = translateStatements("int i = 0; outer: for (; i < 10; i++) { " + "for (int j = 0; j < 10; j++) { break outer; }}");
    assertEquals(3, stmts.size());
    Statement s = stmts.get(1);
    // not LabeledStatement
    assertTrue(s instanceof ForStatement);
    ForStatement fs = (ForStatement) s;
    Statement forStmt = fs.getBody();
    assertTrue(forStmt instanceof Block);
    assertEquals(1, ((Block) forStmt).getStatements().size());
    Statement lastStmt = stmts.get(2);
    assertTrue(lastStmt instanceof LabeledStatement);
    assertTrue(((LabeledStatement) lastStmt).getBody() instanceof EmptyStatement);
}
Also used : LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) Block(com.google.devtools.j2objc.ast.Block) ForStatement(com.google.devtools.j2objc.ast.ForStatement)

Example 5 with ForStatement

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

the class TreeConverter method convertForLoop.

private TreeNode convertForLoop(JCTree.JCForLoop node) {
    ForStatement newNode = new ForStatement().setExpression((Expression) convert(node.getCondition())).setBody((Statement) convert(node.getStatement()));
    VariableDeclarationExpression lastVar = null;
    for (JCTree.JCStatement initializer : node.getInitializer()) {
        if (initializer.getKind() == Kind.VARIABLE) {
            JCTree.JCVariableDecl var = (JCTree.JCVariableDecl) initializer;
            VariableDeclarationExpression newVar = convertVariableExpression(var);
            if (lastVar == null) {
                newNode.addInitializer(newVar);
                lastVar = newVar;
            } else {
                lastVar.addFragment(TreeUtil.remove(newVar.getFragment(0)));
            }
        } else {
            assert initializer.getKind() == Kind.EXPRESSION_STATEMENT;
            newNode.addInitializer((Expression) convert(((JCTree.JCExpressionStatement) initializer).getExpression()));
        }
    }
    for (JCTree.JCExpressionStatement updater : node.getUpdate()) {
        newNode.addUpdater((Expression) convert(updater.getExpression()));
    }
    return newNode;
}
Also used : PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionalExpression(com.google.devtools.j2objc.ast.FunctionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl) JCTree(com.sun.tools.javac.tree.JCTree) ForStatement(com.google.devtools.j2objc.ast.ForStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) JCVariableDecl(com.sun.tools.javac.tree.JCTree.JCVariableDecl)

Aggregations

ForStatement (com.google.devtools.j2objc.ast.ForStatement)9 Statement (com.google.devtools.j2objc.ast.Statement)6 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)6 Block (com.google.devtools.j2objc.ast.Block)5 IfStatement (com.google.devtools.j2objc.ast.IfStatement)5 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)4 Expression (com.google.devtools.j2objc.ast.Expression)4 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)4 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)4 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)4 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)4 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)4 CastExpression (com.google.devtools.j2objc.ast.CastExpression)3 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)3 EmptyStatement (com.google.devtools.j2objc.ast.EmptyStatement)3 LabeledStatement (com.google.devtools.j2objc.ast.LabeledStatement)3 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)3 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)3 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)2 FunctionalExpression (com.google.devtools.j2objc.ast.FunctionalExpression)2