Search in sources :

Example 1 with EmptyStatement

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

the class SwitchRewriter method endVisit.

@Override
public void endVisit(SwitchStatement node) {
    fixVariableDeclarations(node);
    fixStringValue(node);
    fixEnumValue(node);
    List<Statement> stmts = node.getStatements();
    if (!stmts.isEmpty()) {
        Statement lastStmt = stmts.get(stmts.size() - 1);
        if (lastStmt instanceof SwitchCase) {
            // Last switch case doesn't have an associated statement, so add an empty one
            // with the same line number as the switch case to keep line numbers synced.
            EmptyStatement emptyStmt = new EmptyStatement();
            emptyStmt.setLineNumber(lastStmt.getLineNumber());
            stmts.add(emptyStmt);
        }
    }
}
Also used : SwitchCase(com.google.devtools.j2objc.ast.SwitchCase) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement)

Example 2 with EmptyStatement

use of com.google.devtools.j2objc.ast.EmptyStatement 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 3 with EmptyStatement

use of com.google.devtools.j2objc.ast.EmptyStatement 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 4 with EmptyStatement

use of com.google.devtools.j2objc.ast.EmptyStatement 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 5 with EmptyStatement

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

the class LabelRewriter method endVisit.

@Override
public void endVisit(LabeledStatement node) {
    Statement loopBody = getLoopBody(node.getBody());
    final String labelIdentifier = node.getLabel().getIdentifier();
    final boolean[] hasContinue = new boolean[1];
    final boolean[] hasBreak = new boolean[1];
    node.accept(new TreeVisitor() {

        @Override
        public void endVisit(ContinueStatement node) {
            if (node.getLabel() != null && node.getLabel().getIdentifier().equals(labelIdentifier)) {
                hasContinue[0] = true;
                node.setLabel(new SimpleName("continue_" + labelIdentifier));
            }
        }

        @Override
        public void endVisit(BreakStatement node) {
            if (node.getLabel() != null && node.getLabel().getIdentifier().equals(labelIdentifier)) {
                hasBreak[0] = true;
                node.setLabel(new SimpleName("break_" + labelIdentifier));
            }
        }
    });
    if (hasContinue[0]) {
        assert loopBody != null : "Continue statements must be inside a loop.";
        LabeledStatement newLabelStmt = new LabeledStatement("continue_" + labelIdentifier);
        newLabelStmt.setBody(new EmptyStatement());
        // Put the loop body into an inner block so the continue label is outside
        // the scope of any variable initializations.
        Block newBlock = new Block();
        loopBody.replaceWith(newBlock);
        newBlock.addStatement(loopBody);
        newBlock.addStatement(newLabelStmt);
    }
    if (hasBreak[0]) {
        LabeledStatement newLabelStmt = new LabeledStatement("break_" + labelIdentifier);
        newLabelStmt.setBody(new EmptyStatement());
        TreeUtil.insertAfter(node, newLabelStmt);
    }
    if (hasContinue[0] || hasBreak[0]) {
        // Replace this node with its statement, thus deleting the label.
        node.replaceWith(TreeUtil.remove(node.getBody()));
    }
}
Also used : TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) Statement(com.google.devtools.j2objc.ast.Statement) ContinueStatement(com.google.devtools.j2objc.ast.ContinueStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) DoStatement(com.google.devtools.j2objc.ast.DoStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) Block(com.google.devtools.j2objc.ast.Block) ContinueStatement(com.google.devtools.j2objc.ast.ContinueStatement)

Aggregations

EmptyStatement (com.google.devtools.j2objc.ast.EmptyStatement)6 Statement (com.google.devtools.j2objc.ast.Statement)6 Block (com.google.devtools.j2objc.ast.Block)5 ForStatement (com.google.devtools.j2objc.ast.ForStatement)4 IfStatement (com.google.devtools.j2objc.ast.IfStatement)4 LabeledStatement (com.google.devtools.j2objc.ast.LabeledStatement)4 BreakStatement (com.google.devtools.j2objc.ast.BreakStatement)1 ContinueStatement (com.google.devtools.j2objc.ast.ContinueStatement)1 DoStatement (com.google.devtools.j2objc.ast.DoStatement)1 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)1 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)1 SimpleName (com.google.devtools.j2objc.ast.SimpleName)1 SwitchCase (com.google.devtools.j2objc.ast.SwitchCase)1 SwitchStatement (com.google.devtools.j2objc.ast.SwitchStatement)1 TreeVisitor (com.google.devtools.j2objc.ast.TreeVisitor)1 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)1 WhileStatement (com.google.devtools.j2objc.ast.WhileStatement)1