use of com.google.devtools.j2objc.ast.ConditionalExpression in project j2objc by google.
the class UnsequencedExpressionRewriter method extractOrderedAccesses.
private void extractOrderedAccesses(List<Statement> stmtList, TreeNode subExpr, List<VariableAccess> toExtract) {
for (int i = 0; i < toExtract.size(); i++) {
VariableAccess access = toExtract.get(i);
TreeNode topConditional = getTopConditional(access.expression, subExpr);
if (topConditional != null) {
// Conditional expressions require special handling when extracting the
// access because execution of the access may not be guaranteed.
// Here we collect all accesses that are decendant of the conditional
// expression and pass them to an appropriate extraction method.
int j = i + 1;
for (; j < toExtract.size(); j++) {
if (getTopConditional(toExtract.get(j).expression, subExpr) != topConditional) {
break;
}
}
if (topConditional instanceof InfixExpression) {
extractInfixConditional(stmtList, (InfixExpression) topConditional, toExtract.subList(i, j));
} else if (topConditional instanceof ConditionalExpression) {
extractConditionalExpression(stmtList, (ConditionalExpression) topConditional, toExtract.subList(i, j));
} else {
throw new AssertionError("Unexpected conditional node type: " + topConditional.getClass().toString());
}
i = j - 1;
} else {
VariableElement newVar = GeneratedVariableElement.newLocalVar("unseq$" + count++, access.expression.getTypeMirror(), currentMethod);
stmtList.add(new VariableDeclarationStatement(newVar, access.expression.copy()));
access.expression.replaceWith(new SimpleName(newVar));
}
}
}
use of com.google.devtools.j2objc.ast.ConditionalExpression in project j2objc by google.
the class UnsequencedExpressionRewriter method isConditional.
private boolean isConditional(TreeNode node) {
if (node instanceof InfixExpression) {
InfixExpression infixExpr = (InfixExpression) node;
InfixExpression.Operator op = infixExpr.getOperator();
if (op == InfixExpression.Operator.CONDITIONAL_AND || op == InfixExpression.Operator.CONDITIONAL_OR) {
return true;
}
} else if (node instanceof ConditionalExpression) {
return true;
}
return false;
}
use of com.google.devtools.j2objc.ast.ConditionalExpression in project j2objc by google.
the class Autoboxer method endVisit.
@Override
public void endVisit(ConditionalExpression node) {
Expression expr = node.getExpression();
if (!expr.getTypeMirror().getKind().isPrimitive()) {
unbox(expr);
}
boolean nodeIsPrimitive = node.getTypeMirror().getKind().isPrimitive();
Expression thenExpr = node.getThenExpression();
boolean thenIsPrimitive = thenExpr.getTypeMirror().getKind().isPrimitive();
Expression elseExpr = node.getElseExpression();
boolean elseIsPrimitive = elseExpr.getTypeMirror().getKind().isPrimitive();
if (thenIsPrimitive && !nodeIsPrimitive) {
box(thenExpr);
} else if (!thenIsPrimitive && nodeIsPrimitive) {
unbox(thenExpr);
}
if (elseIsPrimitive && !nodeIsPrimitive) {
box(elseExpr);
} else if (!elseIsPrimitive && nodeIsPrimitive) {
unbox(elseExpr);
}
}
use of com.google.devtools.j2objc.ast.ConditionalExpression in project j2objc by google.
the class CastResolver method maybeAddCast.
private void maybeAddCast(Expression expr, TypeMirror expectedType, boolean shouldCastFromId) {
if (expr instanceof ConditionalExpression) {
ConditionalExpression condExpr = (ConditionalExpression) expr;
maybeAddCast(condExpr.getThenExpression(), expectedType, shouldCastFromId);
maybeAddCast(condExpr.getElseExpression(), expectedType, shouldCastFromId);
return;
}
if (needsCast(expr, expectedType, shouldCastFromId)) {
addCast(expr);
}
}
use of com.google.devtools.j2objc.ast.ConditionalExpression in project j2objc by google.
the class ConstantBranchPruner method endVisit.
@Override
public void endVisit(ConditionalExpression node) {
Expression expr = node.getExpression();
Boolean value = getReplaceableValue(expr);
if (value != null) {
Expression result = value ? node.getThenExpression() : node.getElseExpression();
node.replaceWith(result.copy());
}
}
Aggregations