Search in sources :

Example 6 with CastExpression

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

the class TreeConverter method convertFunctionalExpression.

private static TreeNode convertFunctionalExpression(org.eclipse.jdt.core.dom.Expression node, FunctionalExpression newNode) {
    convertExpression(node, newNode);
    ITypeBinding typeBinding = node.resolveTypeBinding();
    newNode.setTypeMirror(BindingConverter.getType(typeBinding));
    ASTNode parent = node.getParent();
    // all of the target types in the node's type binding, so we check for a parent CastExpression.
    if (parent instanceof org.eclipse.jdt.core.dom.CastExpression) {
        typeBinding = ((org.eclipse.jdt.core.dom.CastExpression) parent).resolveTypeBinding();
    }
    if (BindingUtil.isIntersectionType(typeBinding)) {
        for (ITypeBinding i : typeBinding.getInterfaces()) {
            newNode.addTargetType(BindingConverter.getType(i));
        }
    } else {
        newNode.addTargetType(BindingConverter.getType(typeBinding));
    }
    return newNode;
}
Also used : ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) CastExpression(com.google.devtools.j2objc.ast.CastExpression)

Example 7 with CastExpression

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

the class Autoboxer method endVisit.

@Override
public void endVisit(CastExpression node) {
    TypeMirror castType = node.getTypeMirror();
    Expression expr = node.getExpression();
    TypeMirror exprType = expr.getTypeMirror();
    if (castType.getKind().isPrimitive() && !exprType.getKind().isPrimitive()) {
        if (typeUtil.isAssignable(exprType, typeUtil.getJavaNumber().asType())) {
            // Casting a Number object to a primitive, convert to value method.
            unbox(expr, (PrimitiveType) castType);
        } else if (exprType == typeUtil.boxedClass(typeUtil.getChar()).asType()) {
            // Unboxing and casting Character, which does not have number value functions.
            unbox(expr);
            if (castType.getKind() != TypeKind.CHAR) {
                // If the resulting type is not char - keep the cast, to preserve type information in
                // case of reboxing.
                CastExpression castExpr = new CastExpression(castType, null);
                Expression unboxedExpression = node.getExpression();
                unboxedExpression.replaceWith(castExpr);
                castExpr.setExpression(unboxedExpression);
            }
        } else {
            // Casting an object to a primitive. Convert the cast type to the wrapper
            // so that we do a proper cast check, as Java would.
            castType = typeUtil.boxedClass((PrimitiveType) castType).asType();
            node.setType(Type.newType(castType));
            boxOrUnboxExpression(expr, castType);
        }
    } else {
        boxOrUnboxExpression(expr, castType);
    }
    Expression newExpr = node.getExpression();
    if (newExpr != expr) {
        TreeNode parent = node.getParent();
        if (parent instanceof ParenthesizedExpression) {
            parent.replaceWith(TreeUtil.remove(newExpr));
        } else {
            node.replaceWith(TreeUtil.remove(newExpr));
        }
    }
}
Also used : ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) TypeMirror(javax.lang.model.type.TypeMirror) 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) TreeNode(com.google.devtools.j2objc.ast.TreeNode) PrimitiveType(javax.lang.model.type.PrimitiveType) CastExpression(com.google.devtools.j2objc.ast.CastExpression)

Example 8 with CastExpression

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

the class CastResolver method endVisit.

@Override
public void endVisit(ConditionalExpression node) {
    Expression thenExpr = node.getThenExpression();
    Expression elseExpr = node.getElseExpression();
    if (incompatibleTypes(thenExpr, elseExpr)) {
        // Add (id) cast to else expression.
        node.setElseExpression(new CastExpression(TypeUtil.ID_TYPE, TreeUtil.remove(elseExpr)));
    }
}
Also used : 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 9 with CastExpression

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

the class CastResolver method addCast.

private void addCast(Expression expr) {
    CastExpression castExpr = new CastExpression(expr.getTypeMirror(), null);
    expr.replaceWith(ParenthesizedExpression.parenthesize(castExpr));
    castExpr.setExpression(expr);
}
Also used : CastExpression(com.google.devtools.j2objc.ast.CastExpression)

Aggregations

CastExpression (com.google.devtools.j2objc.ast.CastExpression)9 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)6 Expression (com.google.devtools.j2objc.ast.Expression)5 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)5 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)4 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)3 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)3 TypeMirror (javax.lang.model.type.TypeMirror)3 Assignment (com.google.devtools.j2objc.ast.Assignment)2 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)2 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)2 SimpleName (com.google.devtools.j2objc.ast.SimpleName)2 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)2 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)2 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)2 VariableElement (javax.lang.model.element.VariableElement)2 AnnotatableType (com.google.devtools.j2objc.ast.AnnotatableType)1 ArrayType (com.google.devtools.j2objc.ast.ArrayType)1 EnumConstantDeclaration (com.google.devtools.j2objc.ast.EnumConstantDeclaration)1 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)1