Search in sources :

Example 16 with VariableDeclarationStatement

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

the class EnumRewriter method addSimpleNonArcInitialization.

private void addSimpleNonArcInitialization(EnumDeclaration node) {
    List<EnumConstantDeclaration> constants = node.getEnumConstants();
    List<Statement> stmts = node.getClassInitStatements().subList(0, 0);
    stmts.add(new NativeStatement("size_t objSize = class_getInstanceSize(self);"));
    stmts.add(new NativeStatement(UnicodeUtils.format("size_t allocSize = %s * objSize;", constants.size())));
    stmts.add(new NativeStatement("uintptr_t ptr = (uintptr_t)calloc(allocSize, 1);"));
    VariableElement localEnum = GeneratedVariableElement.newLocalVar("e", TypeUtil.ID_TYPE, null);
    stmts.add(new VariableDeclarationStatement(localEnum, null));
    // Create a local array of enum names only if reflection is stripped but
    // enum constants are not. For non-stripped classes, enum names are now
    // retrieved from metadata, to avoid duplicates.
    boolean useNamesArray = options.stripReflection() && !options.stripEnumConstants();
    if (useNamesArray) {
        StringBuilder sb = new StringBuilder("id names[] = {\n  ");
        for (EnumConstantDeclaration constant : constants) {
            sb.append("@\"" + ElementUtil.getName(constant.getVariableElement()) + "\", ");
        }
        sb.append("\n};");
        stmts.add(new NativeStatement(sb.toString()));
    }
    TypeMirror intType = typeUtil.getInt();
    GeneratedVariableElement loopCounterElement = GeneratedVariableElement.newLocalVar("i", intType, TreeUtil.getEnclosingElement(node));
    VariableDeclarationExpression loopCounter = new VariableDeclarationExpression().setType(Type.newType(loopCounterElement.asType())).addFragment(new VariableDeclarationFragment(loopCounterElement, TreeUtil.newLiteral(0, typeUtil)));
    Expression loopTest = new InfixExpression().setOperator(InfixExpression.Operator.LESS).setTypeMirror(intType).addOperand(new SimpleName(loopCounterElement)).addOperand(TreeUtil.newLiteral(constants.size(), typeUtil));
    Expression loopUpdater = new PostfixExpression(loopCounterElement, PostfixExpression.Operator.INCREMENT);
    Block loopBody = new Block();
    stmts.add(new ForStatement().addInitializer(loopCounter).setExpression(loopTest).addUpdater(loopUpdater).setBody(loopBody));
    String enumClassName = nameTable.getFullName(node.getTypeElement());
    loopBody.addStatement(new NativeStatement("((void)(" + enumClassName + "_values_[i] = e = objc_constructInstance(self, (void *)ptr)), ptr += objSize);"));
    if (useNamesArray) {
        loopBody.addStatement(new NativeStatement(enumClassName + "_initWithNSString_withInt_(e, names[i], i);"));
    } else if (options.stripEnumConstants()) {
        String statementText = enumClassName + "_initWithNSString_withInt_(e, " + ENUM_NAME_STRIPPED + ", i);";
        loopBody.addStatement(new NativeStatement(statementText));
    } else {
        loopBody.addStatement(new NativeStatement(enumClassName + "_initWithNSString_withInt_(e, JreEnumConstantName(" + enumClassName + "_class_(), i), i);"));
    }
}
Also used : GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) 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) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) EnumConstantDeclaration(com.google.devtools.j2objc.ast.EnumConstantDeclaration) NativeStatement(com.google.devtools.j2objc.ast.NativeStatement) TypeMirror(javax.lang.model.type.TypeMirror) CastExpression(com.google.devtools.j2objc.ast.CastExpression) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) VariableDeclarationExpression(com.google.devtools.j2objc.ast.VariableDeclarationExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Block(com.google.devtools.j2objc.ast.Block) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) ForStatement(com.google.devtools.j2objc.ast.ForStatement)

Example 17 with VariableDeclarationStatement

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

the class UnsequencedExpressionRewriter method extractConditionalExpression.

private void extractConditionalExpression(List<Statement> stmtList, ConditionalExpression conditional, List<VariableAccess> toExtract) {
    Expression condition = conditional.getExpression();
    Expression thenExpr = conditional.getThenExpression();
    Expression elseExpr = conditional.getElseExpression();
    List<VariableAccess> conditionAccesses = Lists.newArrayList();
    List<VariableAccess> thenAccesses = Lists.newArrayList();
    List<VariableAccess> elseAccesses = Lists.newArrayList();
    boolean needsExtraction = false;
    for (VariableAccess access : toExtract) {
        TreeNode node = access.expression;
        while (node.getParent() != conditional) {
            node = node.getParent();
        }
        if (node == condition) {
            conditionAccesses.add(access);
        } else if (node == thenExpr) {
            thenAccesses.add(access);
        } else if (node == elseExpr) {
            elseAccesses.add(access);
        } else {
            throw new AssertionError();
        }
        if (node != condition) {
            // We need to extract an if-statement if there is an access that
            // executes conditionally.
            needsExtraction = true;
        }
    }
    extractOrderedAccesses(stmtList, condition, conditionAccesses);
    // The recursive call might replace the condition child.
    condition = conditional.getExpression();
    if (needsExtraction) {
        VariableElement resultVar = GeneratedVariableElement.newLocalVar("unseq$" + count++, conditional.getTypeMirror(), currentMethod);
        conditional.replaceWith(new SimpleName(resultVar));
        stmtList.add(new VariableDeclarationStatement(resultVar, null));
        IfStatement newIf = new IfStatement();
        newIf.setExpression(condition.copy());
        stmtList.add(newIf);
        Block thenBlock = new Block();
        newIf.setThenStatement(thenBlock);
        List<Statement> thenStmts = thenBlock.getStatements();
        extractOrderedAccesses(thenStmts, thenExpr, thenAccesses);
        // The recursive call might replace the then expression child.
        thenExpr = conditional.getThenExpression();
        thenStmts.add(new ExpressionStatement(new Assignment(new SimpleName(resultVar), thenExpr.copy())));
        Block elseBlock = new Block();
        newIf.setElseStatement(elseBlock);
        List<Statement> elseStmts = elseBlock.getStatements();
        extractOrderedAccesses(elseStmts, elseExpr, elseAccesses);
        // The recursive call might replace the else expression child.
        elseExpr = conditional.getElseExpression();
        elseStmts.add(new ExpressionStatement(new Assignment(new SimpleName(resultVar), elseExpr.copy())));
    } else {
        extractOrderedAccesses(stmtList, thenExpr, thenAccesses);
        extractOrderedAccesses(stmtList, elseExpr, elseAccesses);
    }
}
Also used : 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) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) Assignment(com.google.devtools.j2objc.ast.Assignment) IfStatement(com.google.devtools.j2objc.ast.IfStatement) 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) TreeNode(com.google.devtools.j2objc.ast.TreeNode) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 18 with VariableDeclarationStatement

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

the class UnsequencedExpressionRewriter method extractVariableDeclarationFragments.

private void extractVariableDeclarationFragments(List<VariableDeclarationFragment> fragments, List<Statement> stmtList) {
    for (int i = 0; i < fragments.size(); i++) {
        VariableDeclarationFragment frag = fragments.get(i);
        Expression init = frag.getInitializer();
        if (init == null) {
            continue;
        }
        newExpression(init);
        init.accept(this);
        List<VariableAccess> toExtract = getUnsequencedAccesses();
        if (!toExtract.isEmpty()) {
            if (i > 0) {
                // Extract all fragments before the current one to preserve ordering.
                VariableDeclarationStatement newDecl = new VariableDeclarationStatement(fragments.get(0).copy());
                for (int j = 1; j < i; j++) {
                    newDecl.addFragment(fragments.get(j).copy());
                }
                stmtList.add(newDecl);
                fragments.subList(0, i).clear();
            }
            extractOrderedAccesses(stmtList, currentTopNode, toExtract);
            i = 0;
        }
    }
}
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) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement)

Example 19 with VariableDeclarationStatement

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

the class UnsequencedExpressionRewriter method visit.

@Override
public boolean visit(AssertStatement node) {
    Expression expr = node.getExpression();
    visitAndExtract(expr, node);
    Expression msg = node.getMessage();
    if (msg != null) {
        newExpression(msg);
        msg.accept(this);
        List<VariableAccess> toExtract = getUnsequencedAccesses();
        if (!toExtract.isEmpty()) {
            // If the message expression needs any extraction, then we first extract
            // the entire boolean expression to preserve ordering between the two.
            VariableElement exprVar = GeneratedVariableElement.newLocalVar("unseq$" + count++, expr.getTypeMirror(), currentMethod);
            TreeUtil.insertBefore(node, new VariableDeclarationStatement(exprVar, node.getExpression().copy()));
            node.setExpression(new SimpleName(exprVar));
            extractOrderedAccesses(TreeUtil.asStatementList(node).subList(0, 0), currentTopNode, toExtract);
        }
    }
    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) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 20 with VariableDeclarationStatement

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

the class SwitchRewriterTest method testVariableDeclarationsInSwitchStatement2.

public void testVariableDeclarationsInSwitchStatement2() throws IOException {
    CompilationUnit unit = translateType("A", "public class A { public void doSomething(int i) { switch (i) { " + "case 1: int j = i * 2; log(j); break; " + "case 2: log(i); break; " + "case 3: log(i); int k = i, l = 42; break; }}" + "private void log(int i) {}}");
    TypeDeclaration testType = (TypeDeclaration) unit.getTypes().get(0);
    // First MethodDeclaration is the implicit default constructor.
    MethodDeclaration method = TreeUtil.getMethodDeclarationsList(testType).get(1);
    List<Statement> stmts = method.getBody().getStatements();
    assertEquals(1, stmts.size());
    Block block = (Block) stmts.get(0);
    stmts = block.getStatements();
    assertEquals(4, stmts.size());
    assertTrue(stmts.get(0) instanceof VariableDeclarationStatement);
    assertTrue(stmts.get(1) instanceof VariableDeclarationStatement);
    assertTrue(stmts.get(2) instanceof VariableDeclarationStatement);
    assertTrue(stmts.get(3) instanceof SwitchStatement);
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) MethodDeclaration(com.google.devtools.j2objc.ast.MethodDeclaration) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Statement(com.google.devtools.j2objc.ast.Statement) SwitchStatement(com.google.devtools.j2objc.ast.SwitchStatement) Block(com.google.devtools.j2objc.ast.Block) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) TypeDeclaration(com.google.devtools.j2objc.ast.TypeDeclaration)

Aggregations

VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)20 SimpleName (com.google.devtools.j2objc.ast.SimpleName)13 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)13 VariableElement (javax.lang.model.element.VariableElement)13 Expression (com.google.devtools.j2objc.ast.Expression)12 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)12 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)10 Statement (com.google.devtools.j2objc.ast.Statement)10 Block (com.google.devtools.j2objc.ast.Block)9 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)9 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)8 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)8 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)7 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)7 VariableDeclarationFragment (com.google.devtools.j2objc.ast.VariableDeclarationFragment)7 Assignment (com.google.devtools.j2objc.ast.Assignment)6 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)6 ForStatement (com.google.devtools.j2objc.ast.ForStatement)6 TreeNode (com.google.devtools.j2objc.ast.TreeNode)5 EnhancedForStatement (com.google.devtools.j2objc.ast.EnhancedForStatement)4