Search in sources :

Example 11 with FunctionInvocation

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

the class JavaCloneWriter method createReleaseStatement.

private Statement createReleaseStatement(VariableElement var) {
    if (options.useARC()) {
        TypeMirror voidType = typeUtil.getVoid();
        FunctionElement element = new FunctionElement("JreRelease", voidType, null).addParameters(TypeUtil.ID_TYPE);
        FunctionInvocation invocation = new FunctionInvocation(element, voidType);
        invocation.addArgument(new SimpleName(var));
        return new ExpressionStatement(invocation);
    } else {
        return new ExpressionStatement(new MethodInvocation(new ExecutablePair(releaseMethod), new SimpleName(var)));
    }
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Example 12 with FunctionInvocation

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

the class ArrayRewriter method newArrayAccess.

private Expression newArrayAccess(ArrayAccess arrayAccessNode, TypeMirror componentType, TypeElement iosArrayElement, boolean assignable) {
    String funcName = ElementUtil.getName(iosArrayElement) + "_Get";
    TypeMirror returnType = componentType;
    TypeMirror declaredReturnType = componentType.getKind().isPrimitive() ? componentType : TypeUtil.ID_TYPE;
    if (assignable) {
        funcName += "Ref";
        returnType = declaredReturnType = new PointerType(componentType);
    }
    FunctionElement element = new FunctionElement(funcName, declaredReturnType, iosArrayElement).addParameters(iosArrayElement.asType(), typeUtil.getInt());
    FunctionInvocation invocation = new FunctionInvocation(element, returnType);
    invocation.addArgument(arrayAccessNode.getArray().copy());
    invocation.addArgument(arrayAccessNode.getIndex().copy());
    if (assignable) {
        return new PrefixExpression(componentType, PrefixExpression.Operator.DEREFERENCE, invocation);
    }
    return invocation;
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) PointerType(com.google.devtools.j2objc.types.PointerType)

Example 13 with FunctionInvocation

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

the class Autoboxer method rewriteBoxedAssignment.

private void rewriteBoxedAssignment(Assignment node) {
    Expression lhs = node.getLeftHandSide();
    Expression rhs = node.getRightHandSide();
    TypeMirror type = lhs.getTypeMirror();
    TypeMirror primitiveType = typeUtil.unboxedType(type);
    if (primitiveType == null) {
        return;
    }
    TypeMirror pointerType = new PointerType(type);
    String funcName = "JreBoxed" + getAssignFunctionName(node.getOperator()) + translationUtil.getOperatorFunctionModifier(lhs) + NameTable.capitalize(primitiveType.toString());
    FunctionElement element = new FunctionElement(funcName, type, TypeUtil.asTypeElement(type)).addParameters(pointerType, primitiveType);
    FunctionInvocation invocation = new FunctionInvocation(element, type);
    invocation.addArgument(new PrefixExpression(pointerType, PrefixExpression.Operator.ADDRESS_OF, TreeUtil.remove(lhs)));
    invocation.addArgument(TreeUtil.remove(rhs));
    unbox(rhs);
    node.replaceWith(invocation);
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) 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) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) PointerType(com.google.devtools.j2objc.types.PointerType)

Example 14 with FunctionInvocation

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

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

the class CastResolver method endVisit.

/**
 * Adds a cast check to compareTo methods. This helps Comparable types behave
 * well in sorted collections which rely on Java's runtime type checking.
 */
@Override
@SuppressWarnings("TypeEquals")
public void endVisit(MethodDeclaration node) {
    ExecutableElement element = node.getExecutableElement();
    if (!ElementUtil.getName(element).equals("compareTo") || node.getBody() == null) {
        return;
    }
    DeclaredType comparableType = typeUtil.findSupertype(ElementUtil.getDeclaringClass(element).asType(), "java.lang.Comparable");
    if (comparableType == null) {
        return;
    }
    List<? extends TypeMirror> typeArguments = comparableType.getTypeArguments();
    List<? extends VariableElement> parameters = element.getParameters();
    if (typeArguments.size() != 1 || parameters.size() != 1 || !typeArguments.get(0).equals(parameters.get(0).asType())) {
        return;
    }
    VariableElement param = node.getParameter(0).getVariableElement();
    FunctionInvocation castCheck = createCastCheck(typeArguments.get(0), new SimpleName(param));
    if (castCheck != null) {
        node.getBody().addStatement(0, new ExpressionStatement(castCheck));
    }
}
Also used : FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableElement(javax.lang.model.element.VariableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Aggregations

FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)36 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)26 TypeMirror (javax.lang.model.type.TypeMirror)23 Expression (com.google.devtools.j2objc.ast.Expression)21 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)14 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)13 ExecutableElement (javax.lang.model.element.ExecutableElement)11 TypeElement (javax.lang.model.element.TypeElement)11 CastExpression (com.google.devtools.j2objc.ast.CastExpression)10 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)10 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)10 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)9 SimpleName (com.google.devtools.j2objc.ast.SimpleName)9 PointerType (com.google.devtools.j2objc.types.PointerType)9 VariableElement (javax.lang.model.element.VariableElement)9 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)8 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)8 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)6 InstanceofExpression (com.google.devtools.j2objc.ast.InstanceofExpression)5 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)5