Search in sources :

Example 31 with FunctionInvocation

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

the class Functionizer method functionizeInvocation.

private void functionizeInvocation(Expression node, ExecutableElement element, Expression receiver, List<Expression> methodArgs) {
    FunctionInvocation functionInvocation = new FunctionInvocation(newFunctionElement(element), node.getTypeMirror());
    List<Expression> funcArgs = functionInvocation.getArguments();
    TreeUtil.moveList(methodArgs, funcArgs);
    if (!ElementUtil.isStatic(element)) {
        if (receiver == null) {
            receiver = new ThisExpression(TreeUtil.getEnclosingTypeElement(node).asType());
        }
        funcArgs.add(0, TreeUtil.remove(receiver));
    }
    node.replaceWith(functionInvocation);
}
Also used : ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) Expression(com.google.devtools.j2objc.ast.Expression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression)

Example 32 with FunctionInvocation

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

the class SuperMethodInvocationRewriter method endVisit.

@Override
public void endVisit(SuperMethodInvocation node) {
    Expression receiver = node.getReceiver();
    ExecutableElement method = node.getExecutableElement();
    TypeMirror exprType = node.getTypeMirror();
    assert !ElementUtil.isDefault(method) : "Default methods are handled in Functionizer.java";
    if (receiver == null) {
        return;
    }
    VariableElement var = TreeUtil.getVariableElement(receiver);
    assert var != null : "Expected receiver to be a variable";
    TypeMirror receiverType = var.asType();
    TypeElement receiverElem = TypeUtil.asTypeElement(receiverType);
    SuperMethodElementPair superMethod = new SuperMethodElementPair(receiverElem, method);
    superMethods.add(superMethod);
    FunctionElement element = new FunctionElement(getSuperFunctionName(superMethod), exprType, receiverElem).addParameters(receiverType, TypeUtil.ID_TYPE).addParameters(ElementUtil.asTypes(method.getParameters()));
    FunctionInvocation invocation = new FunctionInvocation(element, exprType);
    List<Expression> args = invocation.getArguments();
    args.add(TreeUtil.remove(receiver));
    String selectorExpr = UnicodeUtils.format("@selector(%s)", nameTable.getMethodSelector(method));
    args.add(new NativeExpression(selectorExpr, TypeUtil.ID_TYPE));
    TreeUtil.copyList(node.getArguments(), args);
    node.replaceWith(invocation);
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) TypeMirror(javax.lang.model.type.TypeMirror) TypeElement(javax.lang.model.element.TypeElement) ExecutableElement(javax.lang.model.element.ExecutableElement) VariableElement(javax.lang.model.element.VariableElement)

Example 33 with FunctionInvocation

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

the class MetadataWriter method createContainerAnnotation.

// Creates the container annotation which value is an array with the repeated annotations.
private Expression createContainerAnnotation(List<Annotation> annotations) {
    DeclaredType annotationType = annotations.get(0).getAnnotationMirror().getAnnotationType();
    ArrayType arrayType = typeUtil.getArrayType(annotationType);
    DeclaredType containerType = (DeclaredType) ElementUtil.getAnnotationValue(ElementUtil.getAnnotation(annotationType.asElement(), Repeatable.class), "value");
    TypeElement containerElement = (TypeElement) containerType.asElement();
    FunctionElement element = new FunctionElement("create_" + nameTable.getFullName(containerElement), containerType, containerElement);
    FunctionInvocation invocation = new FunctionInvocation(element, containerType);
    element.addParameters(arrayType);
    List<Expression> array = annotations.stream().map(Annotation::getAnnotationMirror).map(translationUtil::createAnnotation).collect(Collectors.toList());
    invocation.addArgument(translationUtil.createObjectArray(array, arrayType));
    return invocation;
}
Also used : ArrayType(javax.lang.model.type.ArrayType) FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) GeneratedTypeElement(com.google.devtools.j2objc.types.GeneratedTypeElement) TypeElement(javax.lang.model.element.TypeElement) Annotation(com.google.devtools.j2objc.ast.Annotation) DeclaredType(javax.lang.model.type.DeclaredType)

Example 34 with FunctionInvocation

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

the class CastResolver method createCastCheck.

private FunctionInvocation createCastCheck(TypeMirror type, Expression expr) {
    type = typeUtil.erasure(type);
    TypeMirror idType = TypeUtil.ID_TYPE;
    if (TypeUtil.isInterface(type) || isObjectArray(type)) {
        // Interfaces and object arrays require an isInstance call.
        FunctionElement element = new FunctionElement("cast_check", idType, null).addParameters(idType, TypeUtil.IOS_CLASS.asType());
        FunctionInvocation invocation = new FunctionInvocation(element, idType);
        invocation.addArgument(TreeUtil.remove(expr));
        invocation.addArgument(new TypeLiteral(type, typeUtil));
        return invocation;
    } else if (TypeUtil.isArray(type) || (TypeUtil.isDeclaredType(type) && needsCastChk(expr, type))) {
        // Primitive array and non-interface type casts are checked using Objective-C's
        // isKindOfClass:.
        TypeElement objcClass = typeUtil.getObjcClass(type);
        FunctionElement checkFunction = new FunctionElement("cast_chk", idType, null).addParameters(idType, idType);
        FunctionInvocation invocation = new FunctionInvocation(checkFunction, idType);
        invocation.addArgument(TreeUtil.remove(expr));
        ExecutableElement classElement = GeneratedExecutableElement.newMethodWithSelector("class", idType, objcClass).addModifiers(Modifier.STATIC);
        MethodInvocation classInvocation = new MethodInvocation(new ExecutablePair(classElement), new SimpleName(objcClass));
        invocation.addArgument(classInvocation);
        return invocation;
    }
    return null;
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeLiteral(com.google.devtools.j2objc.ast.TypeLiteral) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) TypeElement(javax.lang.model.element.TypeElement) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Example 35 with FunctionInvocation

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

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