Search in sources :

Example 16 with Expression

use of com.google.devtools.j2objc.ast.Expression 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 17 with Expression

use of com.google.devtools.j2objc.ast.Expression 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 18 with Expression

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

the class OperatorRewriter method getAssignmentFunctionName.

private String getAssignmentFunctionName(Assignment node, VariableElement var, boolean isRetainedWith) {
    if (!ElementUtil.isField(var)) {
        return null;
    }
    TypeMirror type = var.asType();
    boolean isPrimitive = type.getKind().isPrimitive();
    boolean isStrong = !isPrimitive && !ElementUtil.isWeakReference(var);
    boolean isVolatile = ElementUtil.isVolatile(var);
    if (isRetainedWith) {
        return isVolatile ? "JreVolatileRetainedWithAssign" : "JreRetainedWithAssign";
    }
    if (isVolatile) {
        // same field.
        return isStrong ? "JreVolatileStrongAssign" : "JreAssignVolatile" + (isPrimitive ? NameTable.capitalize(TypeUtil.getName(type)) : "Id");
    }
    if (isStrong && options.useReferenceCounting()) {
        String funcName = "JreStrongAssign";
        Expression retainedRhs = TranslationUtil.retainResult(node.getRightHandSide());
        if (retainedRhs != null) {
            funcName += "AndConsume";
            node.setRightHandSide(retainedRhs);
        }
        return funcName;
    }
    return null;
}
Also used : 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)

Example 19 with Expression

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

the class OperatorRewriter method getRetainedWithTarget.

// Gets the target object for a call to the RetainedWith wrapper.
private Expression getRetainedWithTarget(Assignment node, VariableElement var) {
    Expression lhs = node.getLeftHandSide();
    if (!(lhs instanceof FieldAccess)) {
        return new ThisExpression(ElementUtil.getDeclaringClass(var).asType());
    }
    // To avoid duplicating the target expression we must save the result to a local variable.
    FieldAccess fieldAccess = (FieldAccess) lhs;
    Expression target = fieldAccess.getExpression();
    VariableElement targetVar = GeneratedVariableElement.newLocalVar("__rw$" + rwCount++, target.getTypeMirror(), null);
    TreeUtil.asStatementList(TreeUtil.getOwningStatement(lhs)).add(0, new VariableDeclarationStatement(targetVar, null));
    fieldAccess.setExpression(new SimpleName(targetVar));
    CommaExpression commaExpr = new CommaExpression(new Assignment(new SimpleName(targetVar), target));
    node.replaceWith(commaExpr);
    commaExpr.addExpression(node);
    return new SimpleName(targetVar);
}
Also used : Assignment(com.google.devtools.j2objc.ast.Assignment) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) 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) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess) SuperFieldAccess(com.google.devtools.j2objc.ast.SuperFieldAccess)

Example 20 with Expression

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

the class OperatorRewriter method shouldRewriteCompoundAssign.

private static boolean shouldRewriteCompoundAssign(Assignment node) {
    Expression lhs = node.getLeftHandSide();
    TypeMirror lhsType = lhs.getTypeMirror();
    TypeMirror rhsType = node.getRightHandSide().getTypeMirror();
    switch(node.getOperator()) {
        case LEFT_SHIFT_ASSIGN:
        case RIGHT_SHIFT_SIGNED_ASSIGN:
        case RIGHT_SHIFT_UNSIGNED_ASSIGN:
            return true;
        case PLUS_ASSIGN:
        case MINUS_ASSIGN:
        case TIMES_ASSIGN:
        case DIVIDE_ASSIGN:
        case REMAINDER_ASSIGN:
            return isVolatile(lhs) || TypeUtil.isFloatingPoint(lhsType) || TypeUtil.isFloatingPoint(rhsType);
        default:
            return isVolatile(lhs);
    }
}
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) TypeMirror(javax.lang.model.type.TypeMirror)

Aggregations

Expression (com.google.devtools.j2objc.ast.Expression)106 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)80 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)68 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)60 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)57 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)50 CastExpression (com.google.devtools.j2objc.ast.CastExpression)46 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)45 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)42 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)32 TypeMirror (javax.lang.model.type.TypeMirror)32 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)30 VariableElement (javax.lang.model.element.VariableElement)30 LambdaExpression (com.google.devtools.j2objc.ast.LambdaExpression)26 SimpleName (com.google.devtools.j2objc.ast.SimpleName)25 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)19 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)19 FunctionalExpression (com.google.devtools.j2objc.ast.FunctionalExpression)15 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)14 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)14