Search in sources :

Example 6 with SimpleName

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

the class SwitchRewriter method fixVariableDeclarations.

/**
   * Moves all variable declarations above the first case statement.
   */
private void fixVariableDeclarations(SwitchStatement node) {
    List<Statement> statements = node.getStatements();
    Block block = new Block();
    List<Statement> blockStmts = block.getStatements();
    for (int i = 0; i < statements.size(); i++) {
        Statement stmt = statements.get(i);
        if (stmt instanceof VariableDeclarationStatement) {
            VariableDeclarationStatement declStmt = (VariableDeclarationStatement) stmt;
            statements.remove(i--);
            List<VariableDeclarationFragment> fragments = declStmt.getFragments();
            for (VariableDeclarationFragment decl : fragments) {
                Expression initializer = decl.getInitializer();
                if (initializer != null) {
                    Assignment assignment = new Assignment(new SimpleName(decl.getVariableElement()), TreeUtil.remove(initializer));
                    statements.add(++i, new ExpressionStatement(assignment));
                }
            }
            blockStmts.add(declStmt);
        }
    }
    if (blockStmts.size() > 0) {
        // There is at least one variable declaration, so copy this switch
        // statement into the new block and replace it in the parent list.
        node.replaceWith(block);
        blockStmts.add(node);
    }
}
Also used : Assignment(com.google.devtools.j2objc.ast.Assignment) Expression(com.google.devtools.j2objc.ast.Expression) NativeExpression(com.google.devtools.j2objc.ast.NativeExpression) 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) VariableDeclarationFragment(com.google.devtools.j2objc.ast.VariableDeclarationFragment) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) Block(com.google.devtools.j2objc.ast.Block) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement)

Example 7 with SimpleName

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

the class UnsequencedExpressionRewriter method extractInfixConditional.

private void extractInfixConditional(List<Statement> stmtList, InfixExpression conditional, List<VariableAccess> toExtract) {
    InfixExpression.Operator op = conditional.getOperator();
    List<Expression> branches = conditional.getOperands();
    int lastIfExtractIdx = 0;
    VariableElement conditionalVar = null;
    int lastExtracted = 0;
    Expression lastBranch = null;
    for (int i = 0; i < toExtract.size(); i++) {
        VariableAccess access = toExtract.get(i);
        TreeNode node = access.expression;
        while (node.getParent() != conditional) {
            node = node.getParent();
        }
        assert node instanceof Expression;
        Expression branch = (Expression) node;
        // Extract all accesses from the previous branch.
        if (lastBranch != null && branch != lastBranch) {
            extractOrderedAccesses(stmtList, lastBranch, toExtract.subList(lastExtracted, i));
            lastExtracted = i;
        }
        lastBranch = branch;
        // If there's a new access in a new branch, then we extract an if-statement.
        if (branch != branches.get(lastIfExtractIdx)) {
            TypeMirror boolType = typeUtil.getBoolean();
            if (conditionalVar == null) {
                conditionalVar = GeneratedVariableElement.newLocalVar("unseq$" + count++, boolType, currentMethod);
                conditional.replaceWith(new SimpleName(conditionalVar));
                stmtList.add(new VariableDeclarationStatement(conditionalVar, null));
            }
            List<Expression> subBranches = branches.subList(lastIfExtractIdx, branches.indexOf(branch));
            IfStatement newIf = new IfStatement();
            Expression ifExpr = new Assignment(new SimpleName(conditionalVar), conditionalFromSubBranches(subBranches, op));
            if (op == InfixExpression.Operator.CONDITIONAL_OR) {
                ifExpr = new PrefixExpression(boolType, PrefixExpression.Operator.NOT, ParenthesizedExpression.parenthesize(ifExpr));
            }
            newIf.setExpression(ifExpr);
            stmtList.add(newIf);
            Block thenBlock = new Block();
            stmtList = thenBlock.getStatements();
            newIf.setThenStatement(thenBlock);
            lastIfExtractIdx = branches.indexOf(branch);
        }
    }
    extractOrderedAccesses(stmtList, lastBranch, toExtract.subList(lastExtracted, toExtract.size()));
    if (conditionalVar != null) {
        List<Expression> remainingBranches = Lists.newArrayList();
        remainingBranches.add(new SimpleName(conditionalVar));
        remainingBranches.addAll(branches.subList(lastIfExtractIdx, branches.size()));
        stmtList.add(new ExpressionStatement(new Assignment(new SimpleName(conditionalVar), conditionalFromSubBranches(remainingBranches, op))));
    }
}
Also used : 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) TypeMirror(javax.lang.model.type.TypeMirror) TreeNode(com.google.devtools.j2objc.ast.TreeNode) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) Block(com.google.devtools.j2objc.ast.Block)

Example 8 with SimpleName

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

the class UnsequencedExpressionRewriter method extractOrderedAccesses.

private void extractOrderedAccesses(List<Statement> stmtList, TreeNode subExpr, List<VariableAccess> toExtract) {
    for (int i = 0; i < toExtract.size(); i++) {
        VariableAccess access = toExtract.get(i);
        TreeNode topConditional = getTopConditional(access.expression, subExpr);
        if (topConditional != null) {
            // Conditional expressions require special handling when extracting the
            // access because execution of the access may not be guaranteed.
            // Here we collect all accesses that are decendant of the conditional
            // expression and pass them to an appropriate extraction method.
            int j = i + 1;
            for (; j < toExtract.size(); j++) {
                if (getTopConditional(toExtract.get(j).expression, subExpr) != topConditional) {
                    break;
                }
            }
            if (topConditional instanceof InfixExpression) {
                extractInfixConditional(stmtList, (InfixExpression) topConditional, toExtract.subList(i, j));
            } else if (topConditional instanceof ConditionalExpression) {
                extractConditionalExpression(stmtList, (ConditionalExpression) topConditional, toExtract.subList(i, j));
            } else {
                throw new AssertionError("Unexpected conditional node type: " + topConditional.getClass().toString());
            }
            i = j - 1;
        } else {
            VariableElement newVar = GeneratedVariableElement.newLocalVar("unseq$" + count++, access.expression.getTypeMirror(), currentMethod);
            stmtList.add(new VariableDeclarationStatement(newVar, access.expression.copy()));
            access.expression.replaceWith(new SimpleName(newVar));
        }
    }
}
Also used : TreeNode(com.google.devtools.j2objc.ast.TreeNode) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) SimpleName(com.google.devtools.j2objc.ast.SimpleName) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement)

Example 9 with SimpleName

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

the class OperatorRewriter method getRetainedWithTarget.

// Gets the target object for a call to the RetainedWith wrapper.
private Expression getRetainedWithTarget(Assignment node, VariableElement var) {
    Expression lhs = node.getLeftHandSide();
    if (!(lhs instanceof FieldAccess)) {
        return new ThisExpression(ElementUtil.getDeclaringClass(var).asType());
    }
    // To avoid duplicating the target expression we must save the result to a local variable.
    FieldAccess fieldAccess = (FieldAccess) lhs;
    Expression target = fieldAccess.getExpression();
    VariableElement targetVar = GeneratedVariableElement.newLocalVar("__rw$" + rwCount++, target.getTypeMirror(), null);
    TreeUtil.asStatementList(TreeUtil.getOwningStatement(lhs)).add(0, new VariableDeclarationStatement(targetVar, null));
    fieldAccess.setExpression(new SimpleName(targetVar));
    CommaExpression commaExpr = new CommaExpression(new Assignment(new SimpleName(targetVar), target));
    node.replaceWith(commaExpr);
    commaExpr.addExpression(node);
    return new SimpleName(targetVar);
}
Also used : Assignment(com.google.devtools.j2objc.ast.Assignment) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) ThisExpression(com.google.devtools.j2objc.ast.ThisExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) 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) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess) SuperFieldAccess(com.google.devtools.j2objc.ast.SuperFieldAccess)

Example 10 with SimpleName

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

the class JavaCloneWriter method createReleaseStatement.

private Statement createReleaseStatement(VariableElement var) {
    if (options.useARC()) {
        TypeMirror voidType = typeUtil.getVoid();
        FunctionElement element = new FunctionElement("JreRelease", voidType, null).addParameters(TypeUtil.ID_TYPE);
        FunctionInvocation invocation = new FunctionInvocation(element, voidType);
        invocation.addArgument(new SimpleName(var));
        return new ExpressionStatement(invocation);
    } else {
        return new ExpressionStatement(new MethodInvocation(new ExecutablePair(releaseMethod), new SimpleName(var)));
    }
}
Also used : FunctionElement(com.google.devtools.j2objc.types.FunctionElement) FunctionInvocation(com.google.devtools.j2objc.ast.FunctionInvocation) TypeMirror(javax.lang.model.type.TypeMirror) ExecutablePair(com.google.devtools.j2objc.types.ExecutablePair) SimpleName(com.google.devtools.j2objc.ast.SimpleName) ExpressionStatement(com.google.devtools.j2objc.ast.ExpressionStatement) SuperMethodInvocation(com.google.devtools.j2objc.ast.SuperMethodInvocation) MethodInvocation(com.google.devtools.j2objc.ast.MethodInvocation)

Aggregations

SimpleName (com.google.devtools.j2objc.ast.SimpleName)48 VariableElement (javax.lang.model.element.VariableElement)26 Expression (com.google.devtools.j2objc.ast.Expression)25 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)16 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)16 TypeMirror (javax.lang.model.type.TypeMirror)15 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)14 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)14 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)13 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)13 ExecutablePair (com.google.devtools.j2objc.types.ExecutablePair)12 Block (com.google.devtools.j2objc.ast.Block)11 Statement (com.google.devtools.j2objc.ast.Statement)11 ExecutableElement (javax.lang.model.element.ExecutableElement)11 TypeElement (javax.lang.model.element.TypeElement)10 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)9 MethodInvocation (com.google.devtools.j2objc.ast.MethodInvocation)9 GeneratedExecutableElement (com.google.devtools.j2objc.types.GeneratedExecutableElement)9 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)8 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)8