Search in sources :

Example 16 with InfixExpression

use of com.google.devtools.j2objc.ast.InfixExpression in project j2objc by google.

the class StatementGenerator method visit.

@Override
public boolean visit(InfixExpression node) {
    InfixExpression.Operator op = node.getOperator();
    List<Expression> operands = node.getOperands();
    assert operands.size() >= 2;
    if ((op.equals(InfixExpression.Operator.EQUALS) || op.equals(InfixExpression.Operator.NOT_EQUALS))) {
        Expression lhs = operands.get(0);
        Expression rhs = operands.get(1);
        if (lhs instanceof StringLiteral || rhs instanceof StringLiteral) {
            if (!(lhs instanceof StringLiteral)) {
                // In case the lhs can't call isEqual.
                lhs = operands.get(1);
                rhs = operands.get(0);
            }
            buffer.append(op.equals(InfixExpression.Operator.NOT_EQUALS) ? "![" : "[");
            lhs.accept(this);
            buffer.append(" isEqual:");
            rhs.accept(this);
            buffer.append("]");
            return false;
        }
    }
    String opStr = ' ' + op.toString() + ' ';
    boolean isFirst = true;
    for (Expression operand : operands) {
        if (!isFirst) {
            buffer.append(opStr);
        }
        isFirst = false;
        operand.accept(this);
    }
    return false;
}
Also used : StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) CStringLiteral(com.google.devtools.j2objc.ast.CStringLiteral) LambdaExpression(com.google.devtools.j2objc.ast.LambdaExpression) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression)

Example 17 with InfixExpression

use of com.google.devtools.j2objc.ast.InfixExpression in project j2objc by google.

the class TreeConverter method convertInfixExpression.

private static TreeNode convertInfixExpression(org.eclipse.jdt.core.dom.InfixExpression node) {
    InfixExpression newNode = new InfixExpression();
    convertExpression(node, newNode);
    newNode.setTypeMirror(BindingConverter.getType(node.resolveTypeBinding())).setOperator(InfixExpression.Operator.parse(node.getOperator().toString()));
    // The JDT parser apparently does not always take advantage of extended
    // operands, resulting in potentially very deep trees that can overflow the
    // stack. This code traverses the subtree non-recursively and merges all
    // children that have the same operator into this node using extended
    // operands.
    List<StackState> stack = Lists.newArrayList();
    stack.add(new StackState(node));
    while (!stack.isEmpty()) {
        StackState currentState = stack.get(stack.size() - 1);
        org.eclipse.jdt.core.dom.Expression child = currentState.nextChild();
        if (child == null) {
            stack.remove(stack.size() - 1);
            continue;
        }
        if (child instanceof org.eclipse.jdt.core.dom.InfixExpression) {
            org.eclipse.jdt.core.dom.InfixExpression infixChild = (org.eclipse.jdt.core.dom.InfixExpression) child;
            if (infixChild.getOperator().equals(node.getOperator())) {
                stack.add(new StackState(infixChild));
                continue;
            }
        }
        newNode.addOperand((Expression) TreeConverter.convert(child));
    }
    return newNode;
}
Also used : InfixExpression(com.google.devtools.j2objc.ast.InfixExpression)

Example 18 with InfixExpression

use of com.google.devtools.j2objc.ast.InfixExpression in project j2objc by google.

the class Autoboxer method endVisit.

@Override
public void endVisit(InfixExpression node) {
    InfixExpression.Operator op = node.getOperator();
    List<Expression> operands = node.getOperands();
    // Don't unbox for equality tests where both operands are boxed types.
    if ((op == InfixExpression.Operator.EQUALS || op == InfixExpression.Operator.NOT_EQUALS) && !operands.get(0).getTypeMirror().getKind().isPrimitive() && !operands.get(1).getTypeMirror().getKind().isPrimitive()) {
        return;
    }
    // Don't unbox for string concatenation.
    if (op == InfixExpression.Operator.PLUS && typeUtil.isString(node.getTypeMirror())) {
        return;
    }
    for (Expression operand : operands) {
        if (!operand.getTypeMirror().getKind().isPrimitive()) {
            unbox(operand);
        }
    }
}
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) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression)

Aggregations

InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)18 Expression (com.google.devtools.j2objc.ast.Expression)13 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)8 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)8 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)7 VariableElement (javax.lang.model.element.VariableElement)7 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)6 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)6 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)6 TypeMirror (javax.lang.model.type.TypeMirror)6 SimpleName (com.google.devtools.j2objc.ast.SimpleName)5 CastExpression (com.google.devtools.j2objc.ast.CastExpression)4 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)4 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)4 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)4 Block (com.google.devtools.j2objc.ast.Block)3 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)2 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)2 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)2 Statement (com.google.devtools.j2objc.ast.Statement)2