Search in sources :

Example 6 with InfixExpression

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

the class NilCheckResolver method visit.

@Override
public boolean visit(InfixExpression node) {
    InfixExpression.Operator op = node.getOperator();
    boolean logicalAnd = op == InfixExpression.Operator.CONDITIONAL_AND;
    boolean logicalOr = op == InfixExpression.Operator.CONDITIONAL_OR;
    if (logicalAnd || logicalOr) {
        return handleConditionalOperator(node, logicalAnd);
    }
    boolean equals = op == InfixExpression.Operator.EQUALS;
    boolean notEquals = op == InfixExpression.Operator.NOT_EQUALS;
    if (equals || notEquals) {
        Expression lhs = node.getOperand(0);
        Expression rhs = node.getOperand(1);
        VariableElement maybeNullVar = null;
        if (lhs instanceof NullLiteral) {
            maybeNullVar = TreeUtil.getVariableElement(rhs);
        } else if (rhs instanceof NullLiteral) {
            maybeNullVar = TreeUtil.getVariableElement(lhs);
        }
        if (maybeNullVar != null) {
            if (equals) {
                setConditionalSafeVars(node, EMPTY_VARS, Collections.singleton(maybeNullVar));
            } else {
                setConditionalSafeVars(node, Collections.singleton(maybeNullVar), EMPTY_VARS);
            }
        }
    }
    return true;
}
Also used : Expression(com.google.devtools.j2objc.ast.Expression) 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) VariableElement(javax.lang.model.element.VariableElement) NullLiteral(com.google.devtools.j2objc.ast.NullLiteral)

Example 7 with InfixExpression

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

the class OperatorRewriter method getStringAppendOperands.

private List<Expression> getStringAppendOperands(Assignment node) {
    Expression rhs = node.getRightHandSide();
    if (rhs instanceof InfixExpression && typeUtil.isString(rhs.getTypeMirror())) {
        InfixExpression infixExpr = (InfixExpression) rhs;
        if (infixExpr.getOperator() == InfixExpression.Operator.PLUS) {
            List<Expression> operands = infixExpr.getOperands();
            List<Expression> result = Lists.newArrayListWithCapacity(operands.size());
            TreeUtil.moveList(operands, result);
            return coalesceStringLiterals(result);
        }
    }
    return Collections.singletonList(TreeUtil.remove(rhs));
}
Also used : Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression)

Example 8 with InfixExpression

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

the class OperatorRewriter method rewriteStringConcatenation.

private void rewriteStringConcatenation(InfixExpression node) {
    List<Expression> childOperands = node.getOperands();
    List<Expression> operands = Lists.newArrayListWithCapacity(childOperands.size());
    TreeUtil.moveList(childOperands, operands);
    operands = coalesceStringLiterals(operands);
    if (operands.size() == 1 && typeUtil.isString(operands.get(0).getTypeMirror())) {
        node.replaceWith(operands.get(0));
        return;
    }
    TypeMirror stringType = typeUtil.getJavaString().asType();
    FunctionElement element = new FunctionElement("JreStrcat", stringType, null).addParameters(TypeUtil.NATIVE_CHAR_PTR).setIsVarargs(true);
    FunctionInvocation invocation = new FunctionInvocation(element, stringType);
    List<Expression> args = invocation.getArguments();
    args.add(getStrcatTypesCString(operands));
    args.addAll(operands);
    node.replaceWith(invocation);
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) TypeMirror(javax.lang.model.type.TypeMirror)

Example 9 with InfixExpression

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

the class OperatorRewriter method endVisit.

@Override
public void endVisit(InfixExpression node) {
    InfixExpression.Operator op = node.getOperator();
    TypeMirror nodeType = node.getTypeMirror();
    String funcName = getInfixFunction(op, nodeType);
    if (funcName != null) {
        Iterator<Expression> operandIter = node.getOperands().iterator();
        Expression leftOperand = operandIter.next();
        operandIter.remove();
        // translated here are all left-associative.
        while (operandIter.hasNext()) {
            Expression rightOperand = operandIter.next();
            operandIter.remove();
            FunctionElement element = new FunctionElement(funcName, nodeType, null).addParameters(leftOperand.getTypeMirror(), rightOperand.getTypeMirror());
            FunctionInvocation invocation = new FunctionInvocation(element, nodeType);
            List<Expression> args = invocation.getArguments();
            args.add(leftOperand);
            args.add(rightOperand);
            leftOperand = invocation;
        }
        node.replaceWith(leftOperand);
    } else if (op == InfixExpression.Operator.PLUS && typeUtil.isString(nodeType) && !isStringAppend(node.getParent())) {
        rewriteStringConcatenation(node);
    }
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression)

Example 10 with InfixExpression

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

the class Rewriter method rewriteStringConcat.

private void rewriteStringConcat(InfixExpression node) {
    // Collect all non-string operands that precede the first string operand.
    // If there are multiple such operands, move them into a sub-expression.
    List<Expression> nonStringOperands = new ArrayList<>();
    TypeMirror nonStringExprType = null;
    for (Expression operand : node.getOperands()) {
        TypeMirror operandType = operand.getTypeMirror();
        if (typeUtil.isString(operandType)) {
            break;
        }
        nonStringOperands.add(operand);
        nonStringExprType = getAdditionType(nonStringExprType, operandType);
    }
    if (nonStringOperands.size() < 2) {
        return;
    }
    InfixExpression nonStringExpr = new InfixExpression(nonStringExprType, InfixExpression.Operator.PLUS);
    for (Expression operand : nonStringOperands) {
        nonStringExpr.addOperand(TreeUtil.remove(operand));
    }
    node.addOperand(0, nonStringExpr);
}
Also used : Expression(com.google.devtools.j2objc.ast.Expression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) TypeMirror(javax.lang.model.type.TypeMirror) ArrayList(java.util.ArrayList) 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