use of com.google.devtools.j2objc.ast.IfStatement in project j2objc by google.
the class ConstantBranchPruner method endVisit.
@Override
public void endVisit(IfStatement node) {
Expression expr = node.getExpression();
Boolean value = getKnownValue(expr);
if (value != null) {
Statement sideEffects = getSideEffects(expr);
if (sideEffects != null) {
TreeUtil.insertBefore(node, sideEffects);
}
if (value) {
node.replaceWith(TreeUtil.remove(node.getThenStatement()));
} else if (node.getElseStatement() != null) {
node.replaceWith(TreeUtil.remove(node.getElseStatement()));
} else {
node.remove();
}
}
}
use of com.google.devtools.j2objc.ast.IfStatement in project j2objc by google.
the class UnsequencedExpressionRewriter method extractInfixConditional.
private void extractInfixConditional(List<Statement> stmtList, InfixExpression conditional, List<VariableAccess> toExtract) {
InfixExpression.Operator op = conditional.getOperator();
List<Expression> branches = conditional.getOperands();
int lastIfExtractIdx = 0;
VariableElement conditionalVar = null;
int lastExtracted = 0;
Expression lastBranch = null;
for (int i = 0; i < toExtract.size(); i++) {
VariableAccess access = toExtract.get(i);
TreeNode node = access.expression;
while (node.getParent() != conditional) {
node = node.getParent();
}
assert node instanceof Expression;
Expression branch = (Expression) node;
// Extract all accesses from the previous branch.
if (lastBranch != null && branch != lastBranch) {
extractOrderedAccesses(stmtList, lastBranch, toExtract.subList(lastExtracted, i));
lastExtracted = i;
}
lastBranch = branch;
// If there's a new access in a new branch, then we extract an if-statement.
if (branch != branches.get(lastIfExtractIdx)) {
TypeMirror boolType = typeUtil.getBoolean();
if (conditionalVar == null) {
conditionalVar = GeneratedVariableElement.newLocalVar("unseq$" + count++, boolType, currentMethod);
conditional.replaceWith(new SimpleName(conditionalVar));
stmtList.add(new VariableDeclarationStatement(conditionalVar, null));
}
List<Expression> subBranches = branches.subList(lastIfExtractIdx, branches.indexOf(branch));
IfStatement newIf = new IfStatement();
Expression ifExpr = new Assignment(new SimpleName(conditionalVar), conditionalFromSubBranches(subBranches, op));
if (op == InfixExpression.Operator.CONDITIONAL_OR) {
ifExpr = new PrefixExpression(boolType, PrefixExpression.Operator.NOT, ParenthesizedExpression.parenthesize(ifExpr));
}
newIf.setExpression(ifExpr);
stmtList.add(newIf);
Block thenBlock = new Block();
stmtList = thenBlock.getStatements();
newIf.setThenStatement(thenBlock);
lastIfExtractIdx = branches.indexOf(branch);
}
}
extractOrderedAccesses(stmtList, lastBranch, toExtract.subList(lastExtracted, toExtract.size()));
if (conditionalVar != null) {
List<Expression> remainingBranches = Lists.newArrayList();
remainingBranches.add(new SimpleName(conditionalVar));
remainingBranches.addAll(branches.subList(lastIfExtractIdx, branches.size()));
stmtList.add(new ExpressionStatement(new Assignment(new SimpleName(conditionalVar), conditionalFromSubBranches(remainingBranches, op))));
}
}
use of com.google.devtools.j2objc.ast.IfStatement 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;
}
use of com.google.devtools.j2objc.ast.IfStatement 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;
}
use of com.google.devtools.j2objc.ast.IfStatement in project j2objc by google.
the class UnsequencedExpressionRewriter method visit.
@Override
public boolean visit(IfStatement node) {
visitAndExtract(node.getExpression(), node);
node.getThenStatement().accept(this);
Statement elseStmt = node.getElseStatement();
if (elseStmt != null) {
elseStmt.accept(this);
}
return false;
}
Aggregations