Search in sources :

Example 1 with CastExpression

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

the class TreeConverter method convertCastExpression.

private static TreeNode convertCastExpression(org.eclipse.jdt.core.dom.CastExpression node) {
    CastExpression newNode = new CastExpression();
    convertExpression(node, newNode);
    return newNode.setType((Type) convert(node.getType())).setExpression((Expression) convert(node.getExpression()));
}
Also used : ParameterizedType(com.google.devtools.j2objc.ast.ParameterizedType) QualifiedType(com.google.devtools.j2objc.ast.QualifiedType) AnnotatableType(com.google.devtools.j2objc.ast.AnnotatableType) Type(com.google.devtools.j2objc.ast.Type) NameQualifiedType(com.google.devtools.j2objc.ast.NameQualifiedType) SimpleType(com.google.devtools.j2objc.ast.SimpleType) DeclaredType(javax.lang.model.type.DeclaredType) ArrayType(com.google.devtools.j2objc.ast.ArrayType) PrimitiveType(com.google.devtools.j2objc.ast.PrimitiveType) IntersectionType(com.google.devtools.j2objc.ast.IntersectionType) UnionType(com.google.devtools.j2objc.ast.UnionType) ExecutableType(javax.lang.model.type.ExecutableType) CastExpression(com.google.devtools.j2objc.ast.CastExpression)

Example 2 with CastExpression

use of com.google.devtools.j2objc.ast.CastExpression 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 CastExpression(typeUtil.getVoid(), new ParenthesizedExpression(new Assignment(new SimpleName(targetVar), target))));
    node.replaceWith(commaExpr);
    commaExpr.addExpression(node);
    return new SimpleName(targetVar);
}
Also used : ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) 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) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) CastExpression(com.google.devtools.j2objc.ast.CastExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) VariableElement(javax.lang.model.element.VariableElement) CastExpression(com.google.devtools.j2objc.ast.CastExpression) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess) SuperFieldAccess(com.google.devtools.j2objc.ast.SuperFieldAccess)

Example 3 with CastExpression

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

the class EnumRewriter method addNonArcInitialization.

private void addNonArcInitialization(EnumDeclaration node) {
    TypeElement type = node.getTypeElement();
    int baseTypeCount = 0;
    List<Statement> sizeStatements = new ArrayList<>();
    List<Statement> initStatements = new ArrayList<>();
    TypeMirror voidType = typeUtil.getVoid();
    VariableElement localEnum = GeneratedVariableElement.newLocalVar("e", TypeUtil.ID_TYPE, null);
    int i = 0;
    for (EnumConstantDeclaration constant : node.getEnumConstants()) {
        VariableElement varElement = constant.getVariableElement();
        String varName = ElementUtil.getName(varElement);
        ExecutableElement methodElement = constant.getExecutableElement();
        TypeElement valueType = ElementUtil.getDeclaringClass(methodElement);
        boolean isAnonymous = valueType != type;
        String classExpr = isAnonymous ? "[" + nameTable.getFullName(valueType) + " class]" : "self";
        String sizeName = "objSize" + (isAnonymous ? "_" + varName : "");
        if (isAnonymous) {
            sizeStatements.add(new NativeStatement(UnicodeUtils.format("size_t %s = class_getInstanceSize(%s);", sizeName, classExpr)));
            sizeStatements.add(new NativeStatement(UnicodeUtils.format("allocSize += %s;", sizeName)));
        } else {
            baseTypeCount++;
        }
        initStatements.add(new ExpressionStatement(new CommaExpression(new CastExpression(voidType, new ParenthesizedExpression(new Assignment(new SimpleName(varElement), new Assignment(new SimpleName(localEnum), new NativeExpression(UnicodeUtils.format("objc_constructInstance(%s, (void *)ptr)", classExpr), type.asType()))))), new NativeExpression("ptr += " + sizeName, voidType))));
        String initName = nameTable.getFullFunctionName(methodElement);
        FunctionElement initElement = new FunctionElement(initName, voidType, valueType).addParameters(valueType.asType()).addParameters(ElementUtil.asTypes(methodElement.getParameters()));
        FunctionInvocation initFunc = new FunctionInvocation(initElement, voidType);
        initFunc.addArgument(new SimpleName(localEnum));
        TreeUtil.copyList(constant.getArguments(), initFunc.getArguments());
        initFunc.addArgument(new StringLiteral(varName, typeUtil));
        initFunc.addArgument(new NumberLiteral(i++, typeUtil));
        initStatements.add(new ExpressionStatement(initFunc));
    }
    List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
    if (baseTypeCount == 0) {
        stmts.add(new NativeStatement("size_t allocSize = 0;"));
    } else {
        stmts.add(new NativeStatement("size_t objSize = class_getInstanceSize(self);"));
        stmts.add(new NativeStatement(UnicodeUtils.format("size_t allocSize = %s * objSize;", baseTypeCount)));
    }
    stmts.addAll(sizeStatements);
    stmts.add(new NativeStatement("uintptr_t ptr = (uintptr_t)calloc(allocSize, 1);"));
    stmts.add(new VariableDeclarationStatement(localEnum, null));
    stmts.addAll(initStatements);
}
Also used : ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) Assignment(com.google.devtools.j2objc.ast.Assignment) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) TypeMirror(javax.lang.model.type.TypeMirror) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) CastExpression(com.google.devtools.j2objc.ast.CastExpression) NumberLiteral(com.google.devtools.j2objc.ast.NumberLiteral)

Example 4 with CastExpression

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

the class CastResolver method endVisit.

@Override
public void endVisit(CastExpression node) {
    TypeMirror type = node.getType().getTypeMirror();
    Expression expr = node.getExpression();
    TypeMirror exprType = expr.getTypeMirror();
    if (TypeUtil.isFloatingPoint(exprType)) {
        // Java wouldn't allow a cast from primitive to non-primitive.
        assert type.getKind().isPrimitive();
        switch(type.getKind()) {
            case LONG:
                node.replaceWith(rewriteFloatToIntegralCast(type, expr, "JreFpToLong", type));
                return;
            case CHAR:
                node.replaceWith(rewriteFloatToIntegralCast(type, expr, "JreFpToChar", type));
                return;
            case BYTE:
            case SHORT:
            case INT:
                node.replaceWith(rewriteFloatToIntegralCast(type, expr, "JreFpToInt", typeUtil.getInt()));
                return;
            // Fall through.
            default:
        }
    }
    // Lean on Java's type-checking.
    if (!type.getKind().isPrimitive() && typeUtil.isAssignable(exprType, typeUtil.erasure(type))) {
        node.replaceWith(TreeUtil.remove(expr));
        return;
    }
    FunctionInvocation castCheck = createCastCheck(type, expr);
    if (castCheck != null) {
        node.setExpression(castCheck);
    }
}
Also used : FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) 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)

Example 5 with CastExpression

use of com.google.devtools.j2objc.ast.CastExpression 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)

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