Search in sources :

Example 6 with ThisExpression

use of com.google.devtools.j2objc.ast.ThisExpression 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();
    // Handle default method invocation: SomeInterface.super.method(...)
    if (ElementUtil.isDefault(method)) {
        FunctionElement element = new FunctionElement(nameTable.getFullFunctionName(method), exprType, ElementUtil.getDeclaringClass(method)).addParameters(TypeUtil.ID_TYPE).addParameters(ElementUtil.asTypes(method.getParameters()));
        FunctionInvocation invocation = new FunctionInvocation(element, exprType);
        List<Expression> args = invocation.getArguments();
        if (receiver == null) {
            args.add(new ThisExpression(TreeUtil.getEnclosingTypeElement(node).asType()));
        } else {
            // OuterReferenceResolver has provided an outer path.
            args.add(TreeUtil.remove(receiver));
        }
        TreeUtil.copyList(node.getArguments(), args);
        node.replaceWith(invocation);
        return;
    }
    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) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) Expression(com.google.devtools.j2objc.ast.Expression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) 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 7 with ThisExpression

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

the class Functionizer method setFunctionCaller.

/**
   *  Replace method block statements with single statement that invokes function.
   */
private void setFunctionCaller(MethodDeclaration method, ExecutableElement methodElement) {
    TypeMirror returnType = methodElement.getReturnType();
    TypeElement declaringClass = ElementUtil.getDeclaringClass(methodElement);
    Block body = new Block();
    method.setBody(body);
    method.removeModifiers(Modifier.NATIVE);
    List<Statement> stmts = body.getStatements();
    FunctionInvocation invocation = new FunctionInvocation(newFunctionElement(methodElement), returnType);
    List<Expression> args = invocation.getArguments();
    if (!ElementUtil.isStatic(methodElement)) {
        args.add(new ThisExpression(declaringClass.asType()));
    }
    for (SingleVariableDeclaration param : method.getParameters()) {
        args.add(new SimpleName(param.getVariableElement()));
    }
    if (TypeUtil.isVoid(returnType)) {
        stmts.add(new ExpressionStatement(invocation));
        if (ElementUtil.isConstructor(methodElement)) {
            stmts.add(new ReturnStatement(new ThisExpression(declaringClass.asType())));
        }
    } else {
        stmts.add(new ReturnStatement(invocation));
    }
}
Also used : FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeElement(javax.lang.model.element.TypeElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) Statement(com.google.devtools.j2objc.ast.Statement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) SingleVariableDeclaration(com.google.devtools.j2objc.ast.SingleVariableDeclaration) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) TypeMirror(javax.lang.model.type.TypeMirror) Expression(com.google.devtools.j2objc.ast.Expression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 8 with ThisExpression

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

the class Functionizer method endVisit.

@Override
public void endVisit(ConstructorInvocation node) {
    ExecutableElement element = node.getExecutableElement();
    TypeElement declaringClass = ElementUtil.getDeclaringClass(element);
    FunctionElement funcElement = newFunctionElement(element);
    FunctionInvocation invocation = new FunctionInvocation(funcElement, typeUtil.getVoid());
    List<Expression> args = invocation.getArguments();
    args.add(new ThisExpression(declaringClass.asType()));
    for (VariableElement captureParam : captureInfo.getImplicitPrefixParams(declaringClass)) {
        args.add(new SimpleName(captureParam));
    }
    TreeUtil.moveList(node.getArguments(), args);
    for (VariableElement captureParam : captureInfo.getImplicitPostfixParams(declaringClass)) {
        args.add(new SimpleName(captureParam));
    }
    node.replaceWith(new ExpressionStatement(invocation));
    assert funcElement.getParameterTypes().size() == args.size();
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) 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) 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) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) VariableElement(javax.lang.model.element.VariableElement)

Example 9 with ThisExpression

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

the class Functionizer method endVisit.

@Override
public void endVisit(MethodInvocation node) {
    ExecutableElement element = node.getExecutableElement();
    if (!ElementUtil.isStatic(element) && !functionizableMethods.contains(element)) {
        return;
    }
    FunctionInvocation functionInvocation = new FunctionInvocation(newFunctionElement(element), node.getTypeMirror());
    List<Expression> args = functionInvocation.getArguments();
    TreeUtil.moveList(node.getArguments(), args);
    if (!ElementUtil.isStatic(element)) {
        Expression expr = node.getExpression();
        if (expr == null) {
            expr = new ThisExpression(TreeUtil.getEnclosingTypeElement(node).asType());
        }
        args.add(0, TreeUtil.remove(expr));
    }
    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) GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement)

Example 10 with ThisExpression

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

the class DestructorGenerator method createRelease.

private Statement createRelease(VariableElement var) {
    TypeMirror varType = var.asType();
    if (ElementUtil.isStatic(var) || varType.getKind().isPrimitive() || ElementUtil.isWeakReference(var)) {
        return null;
    }
    boolean isVolatile = ElementUtil.isVolatile(var);
    boolean isRetainedWith = ElementUtil.isRetainedWithField(var);
    String funcName = null;
    if (isRetainedWith) {
        funcName = isVolatile ? "JreVolatileRetainedWithRelease" : "JreRetainedWithRelease";
    } else if (isVolatile) {
        funcName = "JreReleaseVolatile";
    } else if (options.useReferenceCounting()) {
        funcName = "RELEASE_";
    }
    if (funcName == null) {
        return null;
    }
    TypeMirror voidType = typeUtil.getVoid();
    TypeMirror idType = TypeUtil.ID_TYPE;
    FunctionElement element = new FunctionElement(funcName, voidType, null);
    FunctionInvocation releaseInvocation = new FunctionInvocation(element, voidType);
    if (isRetainedWith) {
        element.addParameters(idType);
        releaseInvocation.addArgument(new ThisExpression(ElementUtil.getDeclaringClass(var).asType()));
    }
    element.addParameters(isVolatile ? TypeUtil.ID_PTR_TYPE : idType);
    Expression arg = new SimpleName(var);
    if (isVolatile) {
        arg = new PrefixExpression(new PointerType(varType), PrefixExpression.Operator.ADDRESS_OF, arg);
    }
    releaseInvocation.addArgument(arg);
    return new ExpressionStatement(releaseInvocation);
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) 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) SimpleName(com.google.devtools.j2objc.ast.SimpleName) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) PointerType(com.google.devtools.j2objc.types.PointerType)

Aggregations

ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)11 Expression (com.google.devtools.j2objc.ast.Expression)9 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)7 SimpleName (com.google.devtools.j2objc.ast.SimpleName)6 TypeElement (javax.lang.model.element.TypeElement)6 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)5 ExecutableElement (javax.lang.model.element.ExecutableElement)5 VariableElement (javax.lang.model.element.VariableElement)5 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)4 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)4 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)3 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)3 TypeMirror (javax.lang.model.type.TypeMirror)3 Block (com.google.devtools.j2objc.ast.Block)2 FieldAccess (com.google.devtools.j2objc.ast.FieldAccess)2 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)2 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)2 SuperFieldAccess (com.google.devtools.j2objc.ast.SuperFieldAccess)2 TypeLiteral (com.google.devtools.j2objc.ast.TypeLiteral)2 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)1