Search in sources :

Example 11 with FunctionElement

use of com.google.devtools.j2objc.types.FunctionElement in project j2objc by google.

the class OperatorRewriter method endVisit.

@Override
public void endVisit(InfixExpression node) {
    InfixExpression.Operator op = node.getOperator();
    TypeMirror nodeType = node.getTypeMirror();
    String funcName = getInfixFunction(op, nodeType);
    if (funcName != null) {
        Iterator<Expression> operandIter = node.getOperands().iterator();
        Expression leftOperand = operandIter.next();
        operandIter.remove();
        // translated here are all left-associative.
        while (operandIter.hasNext()) {
            Expression rightOperand = operandIter.next();
            operandIter.remove();
            FunctionElement element = new FunctionElement(funcName, nodeType, null).addParameters(leftOperand.getTypeMirror(), rightOperand.getTypeMirror());
            FunctionInvocation invocation = new FunctionInvocation(element, nodeType);
            List<Expression> args = invocation.getArguments();
            args.add(leftOperand);
            args.add(rightOperand);
            leftOperand = invocation;
        }
        node.replaceWith(leftOperand);
    } else if (op == InfixExpression.Operator.PLUS && typeUtil.isString(nodeType) && !isStringAppend(node.getParent())) {
        rewriteStringConcatenation(node);
    }
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) 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) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression)

Example 12 with FunctionElement

use of com.google.devtools.j2objc.types.FunctionElement 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 13 with FunctionElement

use of com.google.devtools.j2objc.types.FunctionElement 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 FunctionElement

use of com.google.devtools.j2objc.types.FunctionElement 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 (!castType.equals(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) 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 15 with FunctionElement

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

Aggregations

FunctionElement (com.google.devtools.j2objc.types.FunctionElement)27 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)25 TypeMirror (javax.lang.model.type.TypeMirror)20 Expression (com.google.devtools.j2objc.ast.Expression)14 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)12 TypeElement (javax.lang.model.element.TypeElement)11 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)10 PointerType (com.google.devtools.j2objc.types.PointerType)9 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)8 ExecutableElement (javax.lang.model.element.ExecutableElement)8 VariableElement (javax.lang.model.element.VariableElement)8 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)7 SimpleName (com.google.devtools.j2objc.ast.SimpleName)7 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)6 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)6 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)5 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)3 Assignment (com.google.devtools.j2objc.ast.Assignment)2 CastExpression (com.google.devtools.j2objc.ast.CastExpression)2 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)2