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;
}
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;
}
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);
}
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);
}
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);
}
Aggregations