Search in sources :

Example 16 with FunctionElement

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

the class ArrayRewriter method newArrayAssignment.

private FunctionInvocation newArrayAssignment(Assignment assignmentNode, ArrayAccess arrayAccessNode, TypeMirror componentType) {
    Assignment.Operator op = assignmentNode.getOperator();
    assert !componentType.getKind().isPrimitive();
    assert op == Assignment.Operator.ASSIGN;
    Expression value = TreeUtil.remove(assignmentNode.getRightHandSide());
    Expression retainedValue = TranslationUtil.retainResult(value);
    String funcName = "IOSObjectArray_Set";
    if (retainedValue != null) {
        funcName = "IOSObjectArray_SetAndConsume";
        value = retainedValue;
    }
    TypeElement objArrayType = TypeUtil.IOS_OBJECT_ARRAY;
    TypeMirror idType = TypeUtil.ID_TYPE;
    FunctionElement element = new FunctionElement(funcName, idType, objArrayType).addParameters(objArrayType.asType(), typeUtil.getInt(), idType);
    FunctionInvocation invocation = new FunctionInvocation(element, componentType);
    List<Expression> args = invocation.getArguments();
    args.add(TreeUtil.remove(arrayAccessNode.getArray()));
    args.add(TreeUtil.remove(arrayAccessNode.getIndex()));
    args.add(value);
    return invocation;
}
Also used : Assignment(com.google.devtools.j2objc.ast.Assignment) FunctionElement(com.google.devtools.j2objc.types.FunctionElement) 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) TypeElement(javax.lang.model.element.TypeElement)

Example 17 with FunctionElement

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

the class Autoboxer method rewriteBoxedPrefixOrPostfix.

private void rewriteBoxedPrefixOrPostfix(TreeNode node, Expression operand, String funcName) {
    TypeMirror type = operand.getTypeMirror();
    TypeMirror primitiveType = typeUtil.unboxedType(type);
    if (primitiveType == null) {
        return;
    }
    TypeMirror pointerType = new PointerType(type);
    funcName = "JreBoxed" + funcName + translationUtil.getOperatorFunctionModifier(operand) + NameTable.capitalize(primitiveType.toString());
    FunctionElement element = new FunctionElement(funcName, type, TypeUtil.asTypeElement(type)).addParameters(pointerType);
    FunctionInvocation invocation = new FunctionInvocation(element, type);
    invocation.addArgument(new PrefixExpression(pointerType, PrefixExpression.Operator.ADDRESS_OF, TreeUtil.remove(operand)));
    node.replaceWith(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 18 with FunctionElement

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

the class SwitchRewriter method fixStringValue.

private void fixStringValue(SwitchStatement node) {
    Expression expr = node.getExpression();
    TypeMirror type = expr.getTypeMirror();
    if (!typeUtil.isString(type)) {
        return;
    }
    ArrayType arrayType = typeUtil.getArrayType(type);
    ArrayInitializer arrayInit = new ArrayInitializer(arrayType);
    int idx = 0;
    for (Statement stmt : node.getStatements()) {
        if (stmt instanceof SwitchCase) {
            SwitchCase caseStmt = (SwitchCase) stmt;
            if (!caseStmt.isDefault()) {
                arrayInit.addExpression(TreeUtil.remove(caseStmt.getExpression()));
                caseStmt.setExpression(NumberLiteral.newIntLiteral(idx++, typeUtil));
            }
        }
    }
    TypeMirror intType = typeUtil.getInt();
    FunctionElement indexOfFunc = new FunctionElement("JreIndexOfStr", intType, null).addParameters(type, arrayType, intType);
    FunctionInvocation invocation = new FunctionInvocation(indexOfFunc, intType);
    invocation.addArgument(TreeUtil.remove(expr)).addArgument(arrayInit).addArgument(NumberLiteral.newIntLiteral(idx, typeUtil));
    node.setExpression(invocation);
}
Also used : ArrayType(javax.lang.model.type.ArrayType) FunctionElement(com.google.devtools.j2objc.types.FunctionElement) SwitchCase(com.google.devtools.j2objc.ast.SwitchCase) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) TypeMirror(javax.lang.model.type.TypeMirror) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) EmptyStatement(com.google.devtools.j2objc.ast.EmptyStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) ArrayInitializer(com.google.devtools.j2objc.ast.ArrayInitializer)

Example 19 with FunctionElement

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

the class TranslationUtil method createAnnotation.

public Expression createAnnotation(AnnotationMirror annotationMirror) {
    DeclaredType type = annotationMirror.getAnnotationType();
    TypeElement typeElem = (TypeElement) type.asElement();
    FunctionElement element = new FunctionElement("create_" + nameTable.getFullName(typeElem), type, typeElem);
    FunctionInvocation invocation = new FunctionInvocation(element, type);
    Map<? extends ExecutableElement, ? extends AnnotationValue> values = typeUtil.elementUtil().getElementValuesWithDefaults(annotationMirror);
    for (ExecutableElement member : ElementUtil.getSortedAnnotationMembers(typeElem)) {
        TypeMirror valueType = member.getReturnType();
        element.addParameters(valueType);
        invocation.addArgument(createAnnotationValue(valueType, values.get(member)));
    }
    return invocation;
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) DeclaredType(javax.lang.model.type.DeclaredType)

Example 20 with FunctionElement

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

the class JavaCloneWriter method createVolatileCloneStatement.

private Statement createVolatileCloneStatement(VariableElement var, VariableElement originalVar, boolean isWeak) {
    TypeMirror voidType = typeUtil.getVoid();
    TypeMirror pointerType = new PointerType(var.asType());
    String funcName = "JreCloneVolatile" + (isWeak ? "" : "Strong");
    FunctionElement element = new FunctionElement(funcName, voidType, null).addParameters(pointerType, pointerType);
    FunctionInvocation invocation = new FunctionInvocation(element, voidType);
    invocation.addArgument(new PrefixExpression(pointerType, PrefixExpression.Operator.ADDRESS_OF, new SimpleName(var)));
    invocation.addArgument(new PrefixExpression(pointerType, PrefixExpression.Operator.ADDRESS_OF, new FieldAccess(var, new SimpleName(originalVar))));
    return new ExpressionStatement(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) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) PointerType(com.google.devtools.j2objc.types.PointerType) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess)

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