Search in sources :

Example 51 with Expression

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

the class CastResolver method rewriteFloatToIntegralCast.

private Expression rewriteFloatToIntegralCast(TypeMirror castType, Expression expr, String funcName, TypeMirror funcReturnType) {
    FunctionElement element = new FunctionElement(funcName, funcReturnType, null).addParameters(typeUtil.getDouble());
    FunctionInvocation invocation = new FunctionInvocation(element, funcReturnType);
    invocation.addArgument(TreeUtil.remove(expr));
    Expression newExpr = invocation;
    if (!typeUtil.isSameType(castType, funcReturnType)) {
        newExpr = new CastExpression(castType, newExpr);
    }
    return newExpr;
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) CastExpression(com.google.devtools.j2objc.ast.CastExpression) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression)

Example 52 with Expression

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

the class TypePrivateDeclarationGenerator method printDeadClassConstant.

@Override
protected void printDeadClassConstant(VariableDeclarationFragment fragment) {
    VariableElement var = fragment.getVariableElement();
    Object value = var.getConstantValue();
    assert value != null;
    String declType = getDeclarationType(var);
    declType += (declType.endsWith("*") ? "" : " ");
    String name = nameTable.getVariableShortName(var);
    if (ElementUtil.isPrimitiveConstant(var)) {
        printf("#define %s_%s %s\n", typeName, name, LiteralGenerator.generate(value));
    } else {
        print("static " + UnicodeUtils.format("%s%s_%s", declType, typeName, name));
        Expression initializer = fragment.getInitializer();
        if (initializer != null) {
            print(" = " + generateExpression(initializer));
        }
        println(";");
    }
}
Also used : Expression(com.google.devtools.j2objc.ast.Expression) VariableElement(javax.lang.model.element.VariableElement)

Example 53 with Expression

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

the class ArrayRewriter method visit.

// We must handle object array assignment before its children because if the
// rhs is an array creation, we can optimize with "SetAndConsume".
@Override
public boolean visit(Assignment node) {
    Expression lhs = node.getLeftHandSide();
    TypeMirror lhsType = lhs.getTypeMirror();
    if (lhs instanceof ArrayAccess && !lhsType.getKind().isPrimitive()) {
        FunctionInvocation newAssignment = newArrayAssignment(node, (ArrayAccess) lhs, lhsType);
        node.replaceWith(newAssignment);
        newAssignment.accept(this);
        return false;
    }
    return true;
}
Also used : ArrayAccess(com.google.devtools.j2objc.ast.ArrayAccess) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) TypeMirror(javax.lang.model.type.TypeMirror)

Example 54 with Expression

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

the class ArrayRewriter method createInvocation.

private MethodInvocation createInvocation(ArrayCreation node) {
    ArrayType arrayType = node.getTypeMirror();
    boolean retainedResult = node.hasRetainedResult() || options.useARC();
    ArrayInitializer initializer = node.getInitializer();
    if (initializer != null) {
        return newInitializedArrayInvocation(arrayType, initializer.getExpressions(), retainedResult);
    } else {
        List<Expression> dimensions = node.getDimensions();
        if (dimensions.size() == 1) {
            return newSingleDimensionArrayInvocation(arrayType, dimensions.get(0), retainedResult);
        } else {
            return newMultiDimensionArrayInvocation(arrayType, dimensions, retainedResult);
        }
    }
}
Also used : ArrayType(javax.lang.model.type.ArrayType) InstanceofExpression(com.google.devtools.j2objc.ast.InstanceofExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ArrayInitializer(com.google.devtools.j2objc.ast.ArrayInitializer)

Example 55 with Expression

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

the class Autoboxer method endVisit.

@Override
public void endVisit(ReturnStatement node) {
    Expression expr = node.getExpression();
    if (expr != null) {
        TypeMirror returnType = TreeUtil.getOwningReturnType(node);
        boolean returnsPrimitive = returnType.getKind().isPrimitive();
        boolean exprIsPrimitive = expr.getTypeMirror().getKind().isPrimitive();
        if (returnsPrimitive && !exprIsPrimitive) {
            unbox(expr, (PrimitiveType) returnType);
        }
        if (!returnsPrimitive && exprIsPrimitive) {
            box(expr);
        }
    }
}
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) TypeMirror(javax.lang.model.type.TypeMirror)

Aggregations

Expression (com.google.devtools.j2objc.ast.Expression)139 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)103 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)95 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)89 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)78 CastExpression (com.google.devtools.j2objc.ast.CastExpression)70 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)70 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)64 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)58 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)45 TypeMirror (javax.lang.model.type.TypeMirror)41 VariableElement (javax.lang.model.element.VariableElement)40 LambdaExpression (com.google.devtools.j2objc.ast.LambdaExpression)36 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)34 SimpleName (com.google.devtools.j2objc.ast.SimpleName)29 FunctionalExpression (com.google.devtools.j2objc.ast.FunctionalExpression)24 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)22 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)22 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)22 TypeElement (javax.lang.model.element.TypeElement)21