Search in sources :

Example 36 with Statement

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

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

the class NilCheckResolver method visit.

@Override
public boolean visit(LabeledStatement node) {
    Statement body = node.getBody();
    if (body != null) {
        pushLabeledScope(node.getLabel().getIdentifier());
        body.accept(this);
        popAndMerge();
    }
    return false;
}
Also used : ForStatement(com.google.devtools.j2objc.ast.ForStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) ContinueStatement(com.google.devtools.j2objc.ast.ContinueStatement) ThrowStatement(com.google.devtools.j2objc.ast.ThrowStatement) DoStatement(com.google.devtools.j2objc.ast.DoStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) TryStatement(com.google.devtools.j2objc.ast.TryStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement)

Example 38 with Statement

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

the class SwitchRewriter method fixStringValue.

private void fixStringValue(SwitchStatement node) {
    Expression expr = node.getExpression();
    TypeMirror type = expr.getTypeMirror();
    if (!typeUtil.isString(type)) {
        return;
    }
    ArrayType arrayType = typeUtil.getArrayType(type);
    ArrayInitializer arrayInit = new ArrayInitializer(arrayType);
    int idx = 0;
    for (Statement stmt : node.getStatements()) {
        if (stmt instanceof SwitchCase) {
            SwitchCase caseStmt = (SwitchCase) stmt;
            if (!caseStmt.isDefault()) {
                arrayInit.addExpression(TreeUtil.remove(caseStmt.getExpression()));
                caseStmt.setExpression(NumberLiteral.newIntLiteral(idx++, typeUtil));
            }
        }
    }
    TypeMirror intType = typeUtil.getInt();
    FunctionElement indexOfFunc = new FunctionElement("JreIndexOfStr", intType, null).addParameters(type, arrayType, intType);
    FunctionInvocation invocation = new FunctionInvocation(indexOfFunc, intType);
    invocation.addArgument(TreeUtil.remove(expr)).addArgument(arrayInit).addArgument(NumberLiteral.newIntLiteral(idx, typeUtil));
    node.setExpression(invocation);
}
Also used : ArrayType(javax.lang.model.type.ArrayType) FunctionElement(com.google.devtools.j2objc.types.FunctionElement) SwitchCase(com.google.devtools.j2objc.ast.SwitchCase) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) TypeMirror(javax.lang.model.type.TypeMirror) 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) ArrayInitializer(com.google.devtools.j2objc.ast.ArrayInitializer)

Example 39 with Statement

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

the class RewriterTest method testLabeledBreakWithNonBlockParent.

public void testLabeledBreakWithNonBlockParent() throws IOException {
    List<Statement> stmts = translateStatements("int i = 0; if (i == 0) outer: for (; i < 10; i++) { " + "for (int j = 0; j < 10; j++) { break outer; }}");
    assertEquals(2, stmts.size());
    Statement s = stmts.get(1);
    assertTrue(s instanceof IfStatement);
    s = ((IfStatement) s).getThenStatement();
    assertTrue(s instanceof Block);
    stmts = ((Block) s).getStatements();
    assertEquals(2, stmts.size());
    s = stmts.get(0);
    // 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 labelStmt = stmts.get(1);
    assertTrue(labelStmt instanceof LabeledStatement);
    assertTrue(((LabeledStatement) labelStmt).getBody() instanceof EmptyStatement);
}
Also used : IfStatement(com.google.devtools.j2objc.ast.IfStatement) 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 40 with Statement

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

the class SwitchRewriterTest method testVariableDeclarationsInSwitchStatement2.

public void testVariableDeclarationsInSwitchStatement2() throws IOException {
    CompilationUnit unit = translateType("A", "public class A { public void doSomething(int i) { switch (i) { " + "case 1: int j = i * 2; log(j); break; " + "case 2: log(i); break; " + "case 3: log(i); int k = i, l = 42; break; }}" + "private void log(int i) {}}");
    TypeDeclaration testType = (TypeDeclaration) unit.getTypes().get(0);
    // First MethodDeclaration is the implicit default constructor.
    MethodDeclaration method = TreeUtil.getMethodDeclarationsList(testType).get(1);
    List<Statement> stmts = method.getBody().getStatements();
    assertEquals(1, stmts.size());
    Block block = (Block) stmts.get(0);
    stmts = block.getStatements();
    assertEquals(4, stmts.size());
    assertTrue(stmts.get(0) instanceof VariableDeclarationStatement);
    assertTrue(stmts.get(1) instanceof VariableDeclarationStatement);
    assertTrue(stmts.get(2) instanceof VariableDeclarationStatement);
    assertTrue(stmts.get(3) instanceof SwitchStatement);
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) Block(com.google.devtools.j2objc.ast.Block) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Aggregations

Statement (com.google.devtools.j2objc.ast.Statement)40 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)23 ForStatement (com.google.devtools.j2objc.ast.ForStatement)22 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)22 Block (com.google.devtools.j2objc.ast.Block)21 IfStatement (com.google.devtools.j2objc.ast.IfStatement)20 WhileStatement (com.google.devtools.j2objc.ast.WhileStatement)17 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)15 ReturnStatement (com.google.devtools.j2objc.ast.ReturnStatement)15 SwitchStatement (com.google.devtools.j2objc.ast.SwitchStatement)15 DoStatement (com.google.devtools.j2objc.ast.DoStatement)14 BreakStatement (com.google.devtools.j2objc.ast.BreakStatement)13 ThrowStatement (com.google.devtools.j2objc.ast.ThrowStatement)13 Expression (com.google.devtools.j2objc.ast.Expression)12 LabeledStatement (com.google.devtools.j2objc.ast.LabeledStatement)12 SimpleName (com.google.devtools.j2objc.ast.SimpleName)12 EmptyStatement (com.google.devtools.j2objc.ast.EmptyStatement)11 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)11 VariableElement (javax.lang.model.element.VariableElement)11 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)9