Search in sources :

Example 11 with ExpressionStatement

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

the class CastResolver method returnValueNeedsIntCast.

// Some native objective-c methods are declared to return NSUInteger.
private boolean returnValueNeedsIntCast(Expression arg) {
    ExecutableElement methodElement = TreeUtil.getExecutableElement(arg);
    assert methodElement != null;
    if (arg.getParent() instanceof ExpressionStatement) {
        // Avoid "unused return value" warning.
        return false;
    }
    String methodName = nameTable.getMethodSelector(methodElement);
    if (methodName.equals("hash") && methodElement.getReturnType().getKind() == TypeKind.INT) {
        return true;
    }
    return false;
}
Also used : GeneratedExecutableElement(com.google.devtools.j2objc.types.GeneratedExecutableElement) ExecutableElement(javax.lang.model.element.ExecutableElement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement)

Example 12 with ExpressionStatement

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

Example 13 with ExpressionStatement

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

the class EnumRewriter method addArcInitialization.

// ARC does not allow using "objc_constructInstance" so ARC code doesn't get
// the shared allocation optimization.
private void addArcInitialization(EnumDeclaration node) {
    List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
    int i = 0;
    for (EnumConstantDeclaration constant : node.getEnumConstants()) {
        VariableElement varElement = constant.getVariableElement();
        ClassInstanceCreation creation = new ClassInstanceCreation(constant.getExecutablePair());
        TreeUtil.copyList(constant.getArguments(), creation.getArguments());
        creation.addArgument(new StringLiteral(ElementUtil.getName(varElement), typeUtil));
        creation.addArgument(new NumberLiteral(i++, typeUtil));
        creation.setHasRetainedResult(true);
        stmts.add(new ExpressionStatement(new Assignment(new SimpleName(varElement), creation)));
    }
}
Also used : ClassInstanceCreation(com.google.devtools.j2objc.ast.ClassInstanceCreation) Assignment(com.google.devtools.j2objc.ast.Assignment) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) StringLiteral(com.google.devtools.j2objc.ast.StringLiteral) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) NumberLiteral(com.google.devtools.j2objc.ast.NumberLiteral)

Example 14 with ExpressionStatement

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

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

Aggregations

ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)17 SimpleName (com.google.devtools.j2objc.ast.SimpleName)13 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)9 VariableElement (javax.lang.model.element.VariableElement)9 Expression (com.google.devtools.j2objc.ast.Expression)8 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)8 TypeMirror (javax.lang.model.type.TypeMirror)8 Statement (com.google.devtools.j2objc.ast.Statement)7 FunctionElement (com.google.devtools.j2objc.types.FunctionElement)7 ExecutableElement (javax.lang.model.element.ExecutableElement)7 TypeElement (javax.lang.model.element.TypeElement)7 Block (com.google.devtools.j2objc.ast.Block)6 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)6 Assignment (com.google.devtools.j2objc.ast.Assignment)5 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)5 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)5 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)4 NativeStatement (com.google.devtools.j2objc.ast.NativeStatement)4 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)4 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)3