Search in sources :

Example 1 with VariableDeclarationExpression

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

the class TreeConverter method convertVariableExpression.

private VariableDeclarationExpression convertVariableExpression(JCTree.JCVariableDecl node) {
    VarSymbol var = node.sym;
    boolean isVarargs = (node.sym.flags() & Flags.VARARGS) > 0;
    Type newType = convertType(var.asType(), getPosition(node), isVarargs);
    VariableDeclarationFragment fragment = new VariableDeclarationFragment();
    fragment.setVariableElement(var).setInitializer((Expression) convert(node.getInitializer()));
    return new VariableDeclarationExpression().setType(newType).addFragment(fragment);
}
Also used : ParameterizedType(com.google.devtools.j2objc.ast.ParameterizedType) Type(com.google.devtools.j2objc.ast.Type) SimpleType(com.google.devtools.j2objc.ast.SimpleType) DeclaredType(javax.lang.model.type.DeclaredType) ArrayType(com.google.devtools.j2objc.ast.ArrayType) PrimitiveType(com.google.devtools.j2objc.ast.PrimitiveType) UnionType(com.google.devtools.j2objc.ast.UnionType) ExecutableType(javax.lang.model.type.ExecutableType) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) VarSymbol(com.sun.tools.javac.code.Symbol.VarSymbol)

Example 2 with VariableDeclarationExpression

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

the class StatementGenerator method printBasicTryWithResources.

/**
   * Print basic try-with-resources, as defined by JLS 14.20.3.1.
   */
private void printBasicTryWithResources(Block body, List<VariableDeclarationExpression> resources) {
    VariableDeclarationExpression resource = resources.get(0);
    // Resource declaration can only have one fragment.
    String resourceName = nameTable.getVariableQualifiedName(resource.getFragment(0).getVariableElement());
    String primaryExceptionName = UnicodeUtils.format("__primaryException%d", resources.size());
    buffer.append("{\n");
    resource.accept(this);
    buffer.append(";\n");
    buffer.append(UnicodeUtils.format("NSException *%s = nil;\n", primaryExceptionName));
    buffer.append("@try ");
    List<VariableDeclarationExpression> tail = resources.subList(1, resources.size());
    if (tail.isEmpty()) {
        body.accept(this);
    } else {
        printBasicTryWithResources(body, tail);
    }
    buffer.append(UnicodeUtils.format("@catch (NSException *e) {\n" + "%s = e;\n" + "@throw e;\n" + "}\n", primaryExceptionName));
    buffer.append(UnicodeUtils.format(// to compare to the JLS spec.
    "@finally {\n" + " if (%s != nil) {\n" + "  if (%s != nil) {\n" + "   @try {\n" + "    [%s close];\n" + "   } @catch (NSException *e) {\n" + "    [%s addSuppressedWithNSException:e];\n" + "   }\n" + "  } else {\n" + "   [%s close];\n" + "  }\n" + " }\n" + "}\n", resourceName, primaryExceptionName, resourceName, primaryExceptionName, resourceName));
    buffer.append("}\n");
}
Also used : VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression)

Example 3 with VariableDeclarationExpression

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

the class TreeConverter method convertVariableDeclarationExpression.

private static TreeNode convertVariableDeclarationExpression(org.eclipse.jdt.core.dom.VariableDeclarationExpression node) {
    VariableDeclarationExpression newNode = new VariableDeclarationExpression();
    convertExpression(node, newNode);
    for (Object fragment : node.fragments()) {
        newNode.addFragment((VariableDeclarationFragment) TreeConverter.convert(fragment));
    }
    return newNode.setType((Type) TreeConverter.convert(node.getType()));
}
Also used : VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression)

Example 4 with VariableDeclarationExpression

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

the class Rewriter method endVisit.

@Override
public void endVisit(ForStatement node) {
    // nodes in the initializers.
    if (node.getInitializers().size() == 1) {
        Object initializer = node.getInitializer(0);
        if (initializer instanceof VariableDeclarationExpression) {
            List<VariableDeclarationFragment> fragments = ((VariableDeclarationExpression) initializer).getFragments();
            for (VariableDeclarationFragment fragment : fragments) {
                if (ElementUtil.hasAnnotation(fragment.getVariableElement(), AutoreleasePool.class)) {
                    Statement loopBody = node.getBody();
                    if (!(loopBody instanceof Block)) {
                        Block block = new Block();
                        node.setBody(block);
                        block.addStatement(loopBody);
                    }
                    ((Block) node.getBody()).setHasAutoreleasePool(true);
                }
            }
        }
    }
}
Also used : VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) ThrowStatement(com.google.devtools.j2objc.ast.ThrowStatement) TryStatement(com.google.devtools.j2objc.ast.TryStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) Block(com.google.devtools.j2objc.ast.Block)

Example 5 with VariableDeclarationExpression

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

the class UnsequencedExpressionRewriter method visit.

@Override
public boolean visit(ForStatement node) {
    List<Expression> initializers = node.getInitializers();
    // expression or a list of initializer expressions.
    if (initializers.size() == 1 && initializers.get(0) instanceof VariableDeclarationExpression) {
        VariableDeclarationExpression decl = (VariableDeclarationExpression) initializers.get(0);
        extractVariableDeclarationFragments(decl.getFragments(), TreeUtil.asStatementList(node).subList(0, 0));
    } else {
        extractExpressionList(initializers, TreeUtil.asStatementList(node).subList(0, 0), false);
    }
    Expression expr = node.getExpression();
    if (expr != null) {
        newExpression(expr);
        expr.accept(this);
        List<VariableAccess> toExtract = getUnsequencedAccesses();
        if (!toExtract.isEmpty()) {
            // Convert "if (;cond;)" into "if (;;) { if (!(cond)) break; ...}".
            List<Statement> stmtList = TreeUtil.asStatementList(node.getBody()).subList(0, 0);
            extractOrderedAccesses(stmtList, currentTopNode, toExtract);
            stmtList.add(createLoopTermination(node.getExpression()));
            node.setExpression(null);
        }
    }
    extractExpressionList(node.getUpdaters(), TreeUtil.asStatementList(node.getBody()), true);
    node.getBody().accept(this);
    return false;
}
Also used : PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) AssertStatement(com.google.devtools.j2objc.ast.AssertStatement) ForStatement(com.google.devtools.j2objc.ast.ForStatement) SynchronizedStatement(com.google.devtools.j2objc.ast.SynchronizedStatement) DoStatement(com.google.devtools.j2objc.ast.DoStatement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) BreakStatement(com.google.devtools.j2objc.ast.BreakStatement) IfStatement(com.google.devtools.j2objc.ast.IfStatement) Statement(com.google.devtools.j2objc.ast.Statement) ReturnStatement(com.google.devtools.j2objc.ast.ReturnStatement) ThrowStatement(com.google.devtools.j2objc.ast.ThrowStatement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression)

Aggregations

VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)11 ForStatement (com.google.devtools.j2objc.ast.ForStatement)7 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)6 Expression (com.google.devtools.j2objc.ast.Expression)5 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)5 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)5 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)5 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)5 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)4 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)4 Statement (com.google.devtools.j2objc.ast.Statement)4 Block (com.google.devtools.j2objc.ast.Block)3 CastExpression (com.google.devtools.j2objc.ast.CastExpression)3 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)3 IfStatement (com.google.devtools.j2objc.ast.IfStatement)3 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)3 ThrowStatement (com.google.devtools.j2objc.ast.ThrowStatement)3 TreeNode (com.google.devtools.j2objc.ast.TreeNode)3 VariableElement (javax.lang.model.element.VariableElement)3 ArrayType (com.google.devtools.j2objc.ast.ArrayType)2