Search in sources :

Example 1 with InfixExpression

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

the class EnhancedForRewriter method handleArrayIteration.

private void handleArrayIteration(EnhancedForStatement node) {
    Expression expression = node.getExpression();
    ArrayType expressionType = (ArrayType) expression.getTypeMirror();
    VariableElement loopVariable = node.getParameter().getVariableElement();
    TypeMirror componentType = expressionType.getComponentType();
    TypeElement iosArrayType = typeUtil.getIosArray(componentType);
    TypeMirror bufferType = new PointerType(componentType);
    VariableElement arrayVariable = GeneratedVariableElement.newLocalVar("a__", expressionType, null);
    VariableElement bufferVariable = GeneratedVariableElement.newLocalVar("b__", bufferType, null).setTypeQualifiers("const*");
    VariableElement endVariable = GeneratedVariableElement.newLocalVar("e__", bufferType, null).setTypeQualifiers("const*");
    VariableElement bufferField = GeneratedVariableElement.newField("buffer", bufferType, iosArrayType).addModifiers(Modifier.PUBLIC);
    VariableElement sizeField = GeneratedVariableElement.newField("size", typeUtil.getInt(), iosArrayType).addModifiers(Modifier.PUBLIC);
    VariableDeclarationStatement arrayDecl = new VariableDeclarationStatement(arrayVariable, TreeUtil.remove(expression));
    FieldAccess bufferAccess = new FieldAccess(bufferField, new SimpleName(arrayVariable));
    VariableDeclarationStatement bufferDecl = new VariableDeclarationStatement(bufferVariable, bufferAccess);
    InfixExpression endInit = new InfixExpression(bufferType, InfixExpression.Operator.PLUS, new SimpleName(bufferVariable), new FieldAccess(sizeField, new SimpleName(arrayVariable)));
    VariableDeclarationStatement endDecl = new VariableDeclarationStatement(endVariable, endInit);
    WhileStatement loop = new WhileStatement();
    loop.setExpression(new InfixExpression(typeUtil.getBoolean(), InfixExpression.Operator.LESS, new SimpleName(bufferVariable), new SimpleName(endVariable)));
    Block newLoopBody = makeBlock(TreeUtil.remove(node.getBody()));
    loop.setBody(newLoopBody);
    newLoopBody.addStatement(0, new VariableDeclarationStatement(loopVariable, new PrefixExpression(componentType, PrefixExpression.Operator.DEREFERENCE, new PostfixExpression(bufferVariable, PostfixExpression.Operator.INCREMENT))));
    Block block = new Block();
    List<Statement> stmts = block.getStatements();
    stmts.add(arrayDecl);
    stmts.add(bufferDecl);
    stmts.add(endDecl);
    stmts.add(loop);
    replaceLoop(node, block, loop);
}
Also used : TypeElement(javax.lang.model.element.TypeElement) EnhancedForStatement(com.google.devtools.j2objc.ast.EnhancedForStatement) LabeledStatement(com.google.devtools.j2objc.ast.LabeledStatement) VariableDeclarationStatement(com.google.devtools.j2objc.ast.VariableDeclarationStatement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) Statement(com.google.devtools.j2objc.ast.Statement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) PointerType(com.google.devtools.j2objc.types.PointerType) VariableElement(javax.lang.model.element.VariableElement) GeneratedVariableElement(com.google.devtools.j2objc.types.GeneratedVariableElement) WhileStatement(com.google.devtools.j2objc.ast.WhileStatement) ArrayType(javax.lang.model.type.ArrayType) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression) Expression(com.google.devtools.j2objc.ast.Expression) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) TypeMirror(javax.lang.model.type.TypeMirror) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) 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) FieldAccess(com.google.devtools.j2objc.ast.FieldAccess)

Example 2 with InfixExpression

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

the class UnsequencedExpressionRewriter method conditionalFromSubBranches.

private Expression conditionalFromSubBranches(List<Expression> branches, InfixExpression.Operator op) {
    assert branches.size() >= 1;
    if (branches.size() == 1) {
        return branches.get(0).copy();
    } else {
        InfixExpression result = new InfixExpression(typeUtil.getBoolean(), op);
        TreeUtil.copyList(branches, result.getOperands());
        return result;
    }
}
Also used : InfixExpression(com.google.devtools.j2objc.ast.InfixExpression)

Example 3 with InfixExpression

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

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

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

the class UnsequencedExpressionRewriter method isConditional.

private boolean isConditional(TreeNode node) {
    if (node instanceof InfixExpression) {
        InfixExpression infixExpr = (InfixExpression) node;
        InfixExpression.Operator op = infixExpr.getOperator();
        if (op == InfixExpression.Operator.CONDITIONAL_AND || op == InfixExpression.Operator.CONDITIONAL_OR) {
            return true;
        }
    } else if (node instanceof ConditionalExpression) {
        return true;
    }
    return false;
}
Also used : ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression)

Aggregations

InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)18 Expression (com.google.devtools.j2objc.ast.Expression)13 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)8 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)8 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)7 VariableElement (javax.lang.model.element.VariableElement)7 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)6 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)6 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)6 TypeMirror (javax.lang.model.type.TypeMirror)6 SimpleName (com.google.devtools.j2objc.ast.SimpleName)5 CastExpression (com.google.devtools.j2objc.ast.CastExpression)4 ThisExpression (com.google.devtools.j2objc.ast.ThisExpression)4 VariableDeclarationStatement (com.google.devtools.j2objc.ast.VariableDeclarationStatement)4 GeneratedVariableElement (com.google.devtools.j2objc.types.GeneratedVariableElement)4 Block (com.google.devtools.j2objc.ast.Block)3 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)2 FunctionInvocation (com.google.devtools.j2objc.ast.FunctionInvocation)2 NativeExpression (com.google.devtools.j2objc.ast.NativeExpression)2 Statement (com.google.devtools.j2objc.ast.Statement)2