Search in sources :

Example 1 with BreakStatement

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

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

the class TreeConverter method convertBreakStatement.

private TreeNode convertBreakStatement(JCTree.JCBreak node) {
    BreakStatement newNode = new BreakStatement();
    Object label = node.getLabel();
    if (label != null) {
        newNode.setLabel((SimpleName) new SimpleName(label.toString()).setPosition(getPosition(node)));
    }
    return newNode;
}
Also used : BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) JavaFileObject(javax.tools.JavaFileObject)

Example 3 with BreakStatement

use of com.google.devtools.j2objc.ast.BreakStatement 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)

Example 4 with BreakStatement

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

the class LabelRewriter method visit.

@Override
public boolean visit(MethodDeclaration node) {
    // Rename any labels that have the same names; legal in Java but not C.
    final Map<String, Integer> labelCounts = new HashMap<>();
    node.accept(new TreeVisitor() {

        @Override
        public void endVisit(LabeledStatement labeledStatement) {
            final String name = labeledStatement.getLabel().getIdentifier();
            int value = labelCounts.containsKey(name) ? labelCounts.get(name) + 1 : 1;
            labelCounts.put(name, value);
            if (value > 1) {
                final String newName = name + '_' + value;
                labeledStatement.setLabel(new SimpleName(newName));
                // Update references to this label.
                labeledStatement.accept(new TreeVisitor() {

                    @Override
                    public void endVisit(ContinueStatement node) {
                        if (node.getLabel() != null && node.getLabel().getIdentifier().equals(name)) {
                            node.setLabel(new SimpleName(newName));
                        }
                    }

                    @Override
                    public void endVisit(BreakStatement node) {
                        if (node.getLabel() != null && node.getLabel().getIdentifier().equals(name)) {
                            node.setLabel(new SimpleName(newName));
                        }
                    }
                });
            }
        }
    });
    return true;
}
Also used : TreeVisitor(com.google.devtools.j2objc.ast.TreeVisitor) BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) HashMap(java.util.HashMap) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ContinueStatement(com.google.devtools.j2objc.ast.ContinueStatement)

Aggregations

BreakStatement (com.google.devtools.j2objc.ast.BreakStatement)4 SimpleName (com.google.devtools.j2objc.ast.SimpleName)3 ContinueStatement (com.google.devtools.j2objc.ast.ContinueStatement)2 LabeledStatement (com.google.devtools.j2objc.ast.LabeledStatement)2 TreeVisitor (com.google.devtools.j2objc.ast.TreeVisitor)2 Block (com.google.devtools.j2objc.ast.Block)1 DoStatement (com.google.devtools.j2objc.ast.DoStatement)1 EmptyStatement (com.google.devtools.j2objc.ast.EmptyStatement)1 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)1 ForStatement (com.google.devtools.j2objc.ast.ForStatement)1 IfStatement (com.google.devtools.j2objc.ast.IfStatement)1 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)1 Statement (com.google.devtools.j2objc.ast.Statement)1 WhileStatement (com.google.devtools.j2objc.ast.WhileStatement)1 HashMap (java.util.HashMap)1 JavaFileObject (javax.tools.JavaFileObject)1