Search in sources :

Example 1 with ConditionalExpression

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));
        }
    }
}
Also used : TreeNode(com.google.devtools.j2objc.ast.TreeNode) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) SimpleName(com.google.devtools.j2objc.ast.SimpleName) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 2 with ConditionalExpression

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;
}
Also used : ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression)

Example 3 with ConditionalExpression

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);
    }
}
Also used : CastExpression(com.google.devtools.j2objc.ast.CastExpression) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression)

Example 4 with ConditionalExpression

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);
    }
}
Also used : ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression)

Example 5 with ConditionalExpression

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());
    }
}
Also used : Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression)

Aggregations

ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)8 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)7 Expression (com.google.devtools.j2objc.ast.Expression)5 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)5 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)4 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)3 CastExpression (com.google.devtools.j2objc.ast.CastExpression)2 SimpleName (com.google.devtools.j2objc.ast.SimpleName)2 TreeNode (com.google.devtools.j2objc.ast.TreeNode)2 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)2 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)2 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)2 AssertStatement (com.google.devtools.j2objc.ast.AssertStatement)1 Assignment (com.google.devtools.j2objc.ast.Assignment)1 Block (com.google.devtools.j2objc.ast.Block)1 BreakStatement (com.google.devtools.j2objc.ast.BreakStatement)1 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)1 DoStatement (com.google.devtools.j2objc.ast.DoStatement)1 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)1 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)1