Example usage for org.eclipse.jdt.core.dom ConditionalExpression setElseExpression

List of usage examples for org.eclipse.jdt.core.dom ConditionalExpression setElseExpression

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.dom ConditionalExpression setElseExpression.

Prototype

public void setElseExpression(Expression expression) 

Source Link

Document

Sets the "else" part of this conditional expression.

Usage

From source file:com.google.devtools.j2cpp.translate.Autoboxer.java

License:Open Source License

@Override
public void endVisit(ConditionalExpression node) {
    ITypeBinding nodeType = getBoxType(node);

    Expression thenExpr = node.getThenExpression();
    ITypeBinding thenType = getBoxType(thenExpr);

    Expression elseExpr = node.getElseExpression();
    ITypeBinding elseType = getBoxType(elseExpr);

    if (thenType.isPrimitive() && !nodeType.isPrimitive()) {
        node.setThenExpression(box(thenExpr));
    } else if (!thenType.isPrimitive() && nodeType.isPrimitive()) {
        node.setThenExpression(unbox(thenExpr));
    }//w  w w .  j  ava2s. c om

    if (elseType.isPrimitive() && !nodeType.isPrimitive()) {
        node.setElseExpression(box(elseExpr));
    } else if (!elseType.isPrimitive() && nodeType.isPrimitive()) {
        node.setElseExpression(unbox(elseExpr));
    }
}

From source file:java5totext.input.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(org.eclipse.jdt.core.dom.ConditionalExpression node) {
    ConditionalExpression element = (ConditionalExpression) this.binding.get(node);
    this.initializeNode(element, node);

    if (this.binding.get(node.getElseExpression()) != null)
        element.setElseExpression((Expression) this.binding.get(node.getElseExpression()));
    if (this.binding.get(node.getExpression()) != null)
        element.setExpression((Expression) this.binding.get(node.getExpression()));
    if (this.binding.get(node.getThenExpression()) != null)
        element.setThenExpression((Expression) this.binding.get(node.getThenExpression()));
}

From source file:org.decojer.cavaj.transformers.TrExpressions.java

License:Open Source License

private boolean rewriteBooleanCompound(@Nonnull final BB bb) {
    // Is this is a short circuit compound? Example is A || C.
    // Is this a conditional compound (since JVM 4 with C/x is cond)? Example is A ? C : x.
    for (final E in : bb.getIns()) {
        // all following patterns have the following base form:
        // "A(final IfStmt) -> C(unique IfStmt) -> bb"
        final E c_bb = in.getRelevantIn();
        if (!c_bb.isCond()) {
            continue;
        }/*from   ww w  .j a v a2  s  .c o  m*/
        final BB c = c_bb.getStart();
        if (c.getStmts() != 1 || !c.isCond() || !c.isStackEmpty() || c.hasPred(bb)) {
            continue;
        }
        final E a_c = c.getRelevantIn();
        if (a_c == null || !a_c.isCond()) {
            continue;
        }
        final BB a = a_c.getStart();
        if (!a.isCond() || a.hasPred(c)) {
            continue;
        }
        // now we have the potential compound head, go down again and identify patterns
        final E c_bb2 = c_bb.isCondTrue() ? c.getFalseOut() : c.getTrueOut();
        if (c_bb2 == null) {
            assert false;
            continue;
        }
        final BB bb2 = c_bb2.getRelevantEnd();
        // condition wrong for org.objectweb.asm.ALLPerfTest.main(): last 2 conditions in loop
        // a=87 -> c=87 -> bb=92 and bb2=129 as loop head is combining end node
        // if (c.hasPred(bb2)) { continue; }
        final E a_x = a_c.isCondTrue() ? a.getFalseOut() : a.getTrueOut();
        if (a_x == null) {
            assert false;
            continue;
        }
        final BB x = a_x.getRelevantEnd();

        if (bb == x || bb2 == x) {
            // Match pattern from both sides (b2 is right here) because of multiple compounds!
            //
            // This is a short circuit compound, example is A || C:
            //
            // ...|.....
            // ...A.....
            // .t/.\f...
            // .|..C....
            // .|t/.\f../
            // .|/...\./
            // .B.....x. (bb and bb2 or vice versa! further incomings possible)
            // .|.....|.
            //
            // 4 combinations are possible for A -> B and C -> B:
            // - tt is || (see above)
            // - ft is ^||
            // - tf is ^&&
            // - ff is &&

            // rewrite AST
            final IfStatement ifStatement = (IfStatement) a.getFinalStmt();
            assert ifStatement != null;
            final Expression ifExpression = ifStatement.getExpression();
            assert ifExpression != null;
            final IfStatement rightIfStatement = (IfStatement) c.removeStmt(0);
            final Expression rightIfExpression = rightIfStatement.getExpression();
            assert rightIfExpression != null;
            final Op op = getOp(rightIfStatement);
            assert op != null;

            if (bb == x && c_bb.isCondTrue() || bb2 == x && c_bb2.isCondTrue() /* ?t */) {
                if (a_x.isCondTrue() /* tt */) {
                    ifStatement.setExpression(newInfixExpression(InfixExpression.Operator.CONDITIONAL_OR,
                            ifExpression, rightIfExpression, op));
                } else {
                    ifStatement.setExpression(newInfixExpression(InfixExpression.Operator.CONDITIONAL_OR,
                            not(ifExpression), rightIfExpression, op));
                }
            } else {
                if (a_x.isCondTrue() /* tf */) {
                    ifStatement.setExpression(newInfixExpression(InfixExpression.Operator.CONDITIONAL_AND,
                            not(ifExpression), rightIfExpression, op));
                } else {
                    ifStatement.setExpression(newInfixExpression(InfixExpression.Operator.CONDITIONAL_AND,
                            ifExpression, rightIfExpression, op));
                }
            }
            c.joinPredBb(a);
            return true;
        }
        if (x.getStmts() != 1 || !x.isCond() || !x.isStackEmpty() || x.hasPred(bb) || x.hasPred(bb2)) {
            continue;
        }
        // check cross...
        E x_bb = x.getTrueOut();
        assert x_bb != null;
        if (x_bb.getRelevantEnd() == bb) {
            final E x_bb2 = x.getFalseOut();
            assert x_bb2 != null;
            if (x_bb2.getRelevantEnd() != bb2) {
                continue;
            }
        } else {
            if (x_bb.getRelevantEnd() != bb2) {
                continue;
            }
            x_bb = x.getFalseOut();
            assert x_bb != null;
            if (x_bb.getRelevantEnd() != bb) {
                continue;
            }
        }

        // This is a conditional compound (since JVM 4 with C/x is cond), example is A ? C : x:
        //
        // ...|...
        // ...A...
        // .t/.\f.
        // .C...x.
        // .|\./|.
        // t|.x.|f/
        // .|/.\|/
        // .B...b. (bb and bb2 or vice versa! further incomings possible)
        // .|...|.
        //
        // Reduction of none-flat CFG for forward-edges into flat CFG.
        // (There are other none-flat CFGs, e.g. String-Switch with hash collision.)

        // rewrite AST
        final IfStatement ifStatement = (IfStatement) a.getFinalStmt();
        assert ifStatement != null;

        Expression expression = ifStatement.getExpression();
        assert expression != null;
        Expression thenExpression;
        Expression elseExpression;

        final Statement cStmt = c.removeStmt(0);
        assert cStmt instanceof IfStatement;
        final Statement xStmt = x.removeStmt(0);
        assert xStmt instanceof IfStatement;

        if (c.hasSourceBefore(x)) {
            if (a_c.isCondFalse()) {
                expression = not(expression);
            }
            thenExpression = ((IfStatement) cStmt).getExpression();
            elseExpression = ((IfStatement) xStmt).getExpression();
            if (c_bb.isCondTrue() ^ x_bb.isCondTrue()) {
                // cross is true/false mix, for join we must inverse the none-join node x
                assert elseExpression != null;
                elseExpression = not(elseExpression);
            }
        } else { /* x is before c */
            if (a_x.isCondFalse()) {
                expression = not(expression);
            }
            thenExpression = ((IfStatement) xStmt).getExpression();
            elseExpression = ((IfStatement) cStmt).getExpression();
            if (c_bb.isCondTrue() ^ x_bb.isCondTrue()) {
                // cross is true/false mix, for join we must inverse the none-join node x
                assert thenExpression != null;
                thenExpression = not(thenExpression);
            }
        }
        assert elseExpression != null;
        assert thenExpression != null;
        // not from "expression", we need the full resulting expression type
        final ConditionalExpression conditionalExpression = setOp(getAst().newConditionalExpression(),
                getOp(thenExpression));
        conditionalExpression.setExpression(wrap(expression, Priority.CONDITIONAL));
        conditionalExpression.setThenExpression(wrap(thenExpression, Priority.CONDITIONAL));
        conditionalExpression.setElseExpression(wrap(elseExpression, Priority.CONDITIONAL));
        ifStatement.setExpression(conditionalExpression);
        x.remove();
        c.joinPredBb(a);
        return true;
    }
    return false;
}

From source file:org.decojer.cavaj.transformers.TrExpressions.java

License:Open Source License

private boolean rewriteConditional(@Nonnull final BB bb) {
    // Is this a conditional compound value? Example is A ? C : x:
    ///*ww w . ja v a2 s . c  o  m*/
    // ...|...
    // ...A...
    // .t/.\f.
    // .C...x.
    // ..\./..
    // ...B...
    // ...|...
    final List<E> ins = bb.getIns();
    if (ins.size() < 2) {
        // this has 3 preds: a == null ? 0 : a.length() == 0 ? 0 : 1
        // even more preds possible with boolean conditionals
        return false;
    }
    for (final E in : ins) {
        final E c_bb = in.getRelevantIn();
        final BB c = c_bb.getStart();
        // in Scala exists a more complex ternary variant with sub statements
        if (c.getTop() != 1 || c.getStmts() != 0 || c.getIns().size() != 1) {
            continue;
        }
        final E a_c = c.getRelevantIn();
        if (a_c == null) {
            assert false;
            continue;
        }
        final BB a = a_c.getStart();
        if (!a.isCond()) {
            continue;
        }
        // now we have the potential compound head, go down again and identify pattern
        final E a_x = a_c.isCondTrue() ? a.getFalseOut() : a.getTrueOut();
        if (a_x == null) {
            assert false;
            continue;
        }
        final BB x = a_x.getRelevantEnd();
        if (x.getTop() != 1 || x.getStmts() != 0 || x.getIns().size() != 1) {
            continue;
        }
        final E x_bb = x.getRelevantOut();
        if (x_bb == null) {
            assert false;
            continue;
        }
        if (x_bb.getEnd() != bb) {
            continue;
        }
        // is a conditional compound value
        Expression thenExpression = c.peek();
        Expression elseExpression = x.peek();
        final Statement aStmt = a.removeFinalStmt();
        if (!(aStmt instanceof IfStatement)) {
            assert false;
            continue;
        }
        Expression expression = ((IfStatement) aStmt).getExpression();
        if (expression == null) {
            assert false;
            continue;
        }
        final Boolean thenBooleanConst = getBooleanValue(thenExpression);
        final Boolean elseBooleanConst = getBooleanValue(elseExpression);
        rewrite: if (thenBooleanConst != null && elseBooleanConst != null) {
            // expression: A ? true : false => A,
            // accept if one is BooleanLiteral - merging didn't work ;)
            if (a_c.isCondTrue() ^ thenBooleanConst) {
                expression = not(expression);
            }
            // use one of the constants, so that we get the correct out type
            setOp(expression, getOp(thenExpression));
        } else {
            classLiteral: if (expression instanceof InfixExpression) {
                // Class-literals unknown in pre JVM 1.5 bytecode
                // (only primitive wrappers have constants like
                // getstatic java.lang.Void.TYPE : java.lang.Class)
                // ...construct Class-literals with synthetic local method:
                // static synthetic java.lang.Class class$(java.lang.String x0);
                // ...and cache this Class-literals in synthetic local fields:
                // static synthetic java.lang.Class class$java$lang$String;
                // static synthetic java.lang.Class array$$I;
                // resulting conditional code:
                // DecTestFields.array$$I != null ? DecTestFields.array$$I :
                // (DecTestFields.array$$I = DecTestFields.class$("[[I"))
                // ...convert too: int[][].class
                final InfixExpression equalsExpression = (InfixExpression) expression;
                if (!(equalsExpression.getRightOperand() instanceof NullLiteral)) {
                    break classLiteral;
                }
                final Assignment assignment;
                if (equalsExpression.getOperator() == InfixExpression.Operator.EQUALS) {
                    // JVM < 1.3
                    if (!(thenExpression instanceof Assignment)) {
                        break classLiteral;
                    }
                    assignment = (Assignment) thenExpression;
                } else if (equalsExpression.getOperator() == InfixExpression.Operator.NOT_EQUALS) {
                    // JVM >= 1.3
                    if (!(elseExpression instanceof Assignment)) {
                        break classLiteral;
                    }
                    assignment = (Assignment) elseExpression;
                } else {
                    break classLiteral;
                }
                if (!(assignment.getRightHandSide() instanceof MethodInvocation)) {
                    break classLiteral;
                }
                final MethodInvocation methodInvocation = (MethodInvocation) assignment.getRightHandSide();
                if (!"class$".equals(methodInvocation.getName().getIdentifier())) {
                    break classLiteral;
                }
                if (methodInvocation.arguments().size() != 1) {
                    break classLiteral;
                }
                if (getCfg().getT().isAtLeast(Version.JVM_5)) {
                    log.warn(getM() + ": Unexpected class literal code with class$() in >= JVM 5 code!");
                }
                try {
                    final String classInfo = ((StringLiteral) methodInvocation.arguments().get(0))
                            .getLiteralValue();
                    assert classInfo != null;
                    expression = newLiteral(getCfg().getDu().getT(Class.class),
                            getCfg().getDu().getT(classInfo), getM(), getOp(equalsExpression));
                    break rewrite;
                } catch (final Exception e) {
                    // rewrite to class literal didn't work
                }
            }
            // use one of the constants, so that we get the correct out type
            final ConditionalExpression conditionalExpression = setOp(getAst().newConditionalExpression(),
                    getOp(thenExpression));
            if (!c.hasSourceBefore(x)) {
                final Expression swapExpression = thenExpression;
                thenExpression = elseExpression;
                elseExpression = swapExpression;
                if (a_c.isCondTrue()) {
                    expression = not(expression);
                }
            } else if (a_c.isCondFalse()) {
                expression = not(expression);
            }
            conditionalExpression.setExpression(wrap(expression, Priority.CONDITIONAL));
            conditionalExpression.setThenExpression(wrap(thenExpression, Priority.CONDITIONAL));
            conditionalExpression.setElseExpression(wrap(elseExpression, Priority.CONDITIONAL));
            expression = conditionalExpression;
        }
        a.push(expression);
        a.setSucc(bb);
        c.remove();
        x.remove();
        return true;
    }
    return false;
}

From source file:org.decojer.cavaj.utils.Expressions.java

License:Open Source License

/**
 * Not expression.//from   w  w w  .j  a  v a 2 s  .  com
 *
 * @param operand
 *            operand expression
 * @return !expression
 */
@Nonnull
@SuppressWarnings("null")
public static Expression not(@Nonnull final Expression operand) {
    if (operand instanceof ParenthesizedExpression) {
        return not(((ParenthesizedExpression) operand).getExpression());
    }
    if (operand instanceof PrefixExpression) {
        if (((PrefixExpression) operand).getOperator() == PrefixExpression.Operator.NOT) {
            // !!a => a
            return unwrap(((PrefixExpression) operand).getOperand());
        }
    } else if (operand instanceof InfixExpression) {
        final InfixExpression infixExpression = (InfixExpression) operand;
        if (infixExpression.getOperator() == InfixExpression.Operator.EQUALS) {
            // operator priority doesn't change here, reuse for all such cases...
            infixExpression.setOperator(InfixExpression.Operator.NOT_EQUALS);
            return infixExpression;
        }
        if (infixExpression.getOperator() == InfixExpression.Operator.GREATER) {
            infixExpression.setOperator(InfixExpression.Operator.LESS_EQUALS);
            return infixExpression;
        }
        if (infixExpression.getOperator() == InfixExpression.Operator.GREATER_EQUALS) {
            infixExpression.setOperator(InfixExpression.Operator.LESS);
            return infixExpression;
        }
        if (infixExpression.getOperator() == InfixExpression.Operator.LESS) {
            infixExpression.setOperator(InfixExpression.Operator.GREATER_EQUALS);
            return infixExpression;
        }
        if (infixExpression.getOperator() == InfixExpression.Operator.LESS_EQUALS) {
            infixExpression.setOperator(InfixExpression.Operator.GREATER);
            return infixExpression;
        }
        if (infixExpression.getOperator() == InfixExpression.Operator.NOT_EQUALS) {
            infixExpression.setOperator(InfixExpression.Operator.EQUALS);
            return infixExpression;
        }
        if (infixExpression.getOperator() == InfixExpression.Operator.CONDITIONAL_AND
                || infixExpression.getOperator() == InfixExpression.Operator.CONDITIONAL_OR) {
            // operator priorities change, don't reuse
            return newInfixExpression(
                    infixExpression.getOperator() == InfixExpression.Operator.CONDITIONAL_AND
                            ? InfixExpression.Operator.CONDITIONAL_OR
                            : InfixExpression.Operator.CONDITIONAL_AND,
                    not(infixExpression.getLeftOperand()), not(infixExpression.getRightOperand()),
                    getOp(infixExpression));
        }
    } else if (operand instanceof ConditionalExpression) {
        // conditional has very low operator priority (before assignment), reuse possible
        final ConditionalExpression conditionalExpression = (ConditionalExpression) operand;
        final Expression thenExpression = not(conditionalExpression.getThenExpression());
        if (conditionalExpression.getThenExpression() != thenExpression) {
            conditionalExpression.setThenExpression(wrap(thenExpression, Priority.CONDITIONAL));
        }
        final Expression elseExpression = not(conditionalExpression.getElseExpression());
        if (conditionalExpression.getElseExpression() != elseExpression) {
            conditionalExpression.setElseExpression(wrap(elseExpression, Priority.CONDITIONAL));
        }
        return conditionalExpression;
    }
    final PrefixExpression prefixExpression = setOp(operand.getAST().newPrefixExpression(), getOp(operand));
    prefixExpression.setOperator(PrefixExpression.Operator.NOT);
    prefixExpression.setOperand(wrap(operand, Priority.priority(prefixExpression)));
    return prefixExpression;
}

From source file:org.eclipse.jdt.core.dom.ASTConverter.java

License:Open Source License

public ConditionalExpression convert(org.eclipse.jdt.internal.compiler.ast.ConditionalExpression expression) {
    ConditionalExpression conditionalExpression = new ConditionalExpression(this.ast);
    if (this.resolveBindings) {
        recordNodes(conditionalExpression, expression);
    }//from   w  ww .  j a  v  a  2s  .c  o m
    conditionalExpression.setSourceRange(expression.sourceStart,
            expression.sourceEnd - expression.sourceStart + 1);
    conditionalExpression.setExpression(convert(expression.condition));
    conditionalExpression.setThenExpression(convert(expression.valueIfTrue));
    conditionalExpression.setElseExpression(convert(expression.valueIfFalse));
    return conditionalExpression;
}

From source file:org.eclipse.modisco.java.discoverer.internal.io.java.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(final org.eclipse.jdt.core.dom.ConditionalExpression node) {
    ConditionalExpression element = (ConditionalExpression) this.binding.get(node);
    initializeNode(element, node);/*from   w  w w  .  j  a  va 2  s .  c  o m*/

    if (this.binding.get(node.getElseExpression()) != null) {
        element.setElseExpression(
                JDTVisitorUtils.completeExpression(this.binding.get(node.getElseExpression()), this));
    }
    if (this.binding.get(node.getExpression()) != null) {
        element.setExpression(JDTVisitorUtils.completeExpression(this.binding.get(node.getExpression()), this));
    }
    if (this.binding.get(node.getThenExpression()) != null) {
        element.setThenExpression(
                JDTVisitorUtils.completeExpression(this.binding.get(node.getThenExpression()), this));
    }
}

From source file:org.whole.lang.java.util.JDTTransformerVisitor.java

License:Open Source License

@Override
public boolean visit(ConditionalExpression node) {
    org.whole.lang.java.model.ConditionalExpression condExp = lf.createConditionalExpression();
    if (acceptChild(node.getExpression()))
        condExp.setExpression(exp);// ww w . ja v a2s. c om
    if (acceptChild(node.getThenExpression()))
        condExp.setThenExpression(exp);
    if (acceptChild(node.getElseExpression()))
        condExp.setElseExpression(exp);
    exp = condExp;
    return false;
}