Search in sources :

Example 1 with TreeNode

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

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

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

the class UnsequencedExpressionRewriter method isWithinConditionalBranch.

private boolean isWithinConditionalBranch(TreeNode node, TreeNode limit) {
    while (node != limit) {
        TreeNode parent = node.getParent();
        if (isConditional(parent) && getConditionChild(parent) != node) {
            return true;
        }
        node = parent;
    }
    return false;
}
Also used : TreeNode(com.google.devtools.j2objc.ast.TreeNode)

Example 4 with TreeNode

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

the class ComplexExpressionExtractor method endVisit.

@Override
public void endVisit(PrefixExpression node) {
    TreeNode parent = node.getParent();
    if (parent == null) {
        return;
    }
    // Check for balancing dereference and address-of operators.
    if (parent instanceof PrefixExpression) {
        PrefixExpression.Operator thisOp = node.getOperator();
        PrefixExpression.Operator parentOp = ((PrefixExpression) parent).getOperator();
        if ((thisOp == PrefixExpression.Operator.DEREFERENCE && parentOp == PrefixExpression.Operator.ADDRESS_OF) || (thisOp == PrefixExpression.Operator.ADDRESS_OF && parentOp == PrefixExpression.Operator.DEREFERENCE)) {
            parent.replaceWith(TreeUtil.remove(node.getOperand()));
            return;
        }
    }
    // without checking if parentheses were necessary.
    switch(parent.getKind()) {
        case POSTFIX_EXPRESSION:
        case // Parentheses not needed, but better for readability.
        PREFIX_EXPRESSION:
            ParenthesizedExpression.parenthesizeAndReplace(node);
            break;
        default:
    }
}
Also used : TreeNode(com.google.devtools.j2objc.ast.TreeNode) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression)

Example 5 with TreeNode

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

the class JavadocConverter method visitParam.

@Override
public Void visitParam(ParamTree node, TagElement tag) {
    DCTree.DCIdentifier identifier = (DCTree.DCIdentifier) node.getName();
    if (identifier == null || node.isTypeParameter()) {
        return null;
    }
    List<? extends VariableElement> params = element instanceof ExecutableElement ? ((ExecutableElement) element).getParameters() : Collections.emptyList();
    tag.setTagName(TagElement.TAG_PARAM);
    String name = identifier.toString();
    VariableElement param = null;
    for (VariableElement p : params) {
        if (name.equals(p.getSimpleName().toString())) {
            param = p;
            break;
        }
    }
    // param will be null if the @param tag refers to a nonexistent parameter.
    TreeNode nameNode = param != null ? new SimpleName(param) : new SimpleName(name);
    setPos(identifier, nameNode);
    tag.addFragment(nameNode);
    scan(node.getDescription(), tag);
    int lastEnd = nameNode.getStartPosition();
    for (TreeNode fragment : tag.getFragments()) {
        // TODO(tball): remove and fix JavadocGenerator after javac switch.
        if (fragment.getKind() == TreeNode.Kind.TEXT_ELEMENT) {
            TextElement text = (TextElement) fragment;
            text.setText(" " + text.getText());
            text.setSourceRange(text.getStartPosition(), text.getLength() + 1);
        }
        int thisEnd = lastEnd + fragment.getLength();
        setPos(fragment, lastEnd, thisEnd);
        lastEnd = thisEnd;
    }
    setPos(tag, pos(node), endPos(node));
    tag.setLineNumber(nameNode.getLineNumber());
    return null;
}
Also used : TextElement(com.google.devtools.j2objc.ast.TextElement) TreeNode(com.google.devtools.j2objc.ast.TreeNode) ExecutableElement(javax.lang.model.element.ExecutableElement) SimpleName(com.google.devtools.j2objc.ast.SimpleName) VariableElement(javax.lang.model.element.VariableElement) DCTree(com.sun.tools.javac.tree.DCTree)

Aggregations

TreeNode (com.google.devtools.j2objc.ast.TreeNode)24 PrefixExpression (com.google.devtools.j2objc.ast.PrefixExpression)7 ConditionalExpression (com.google.devtools.j2objc.ast.ConditionalExpression)6 InfixExpression (com.google.devtools.j2objc.ast.InfixExpression)6 PostfixExpression (com.google.devtools.j2objc.ast.PostfixExpression)6 SimpleName (com.google.devtools.j2objc.ast.SimpleName)6 VariableElement (javax.lang.model.element.VariableElement)6 Expression (com.google.devtools.j2objc.ast.Expression)5 ParenthesizedExpression (com.google.devtools.j2objc.ast.ParenthesizedExpression)5 TextElement (com.google.devtools.j2objc.ast.TextElement)5 JCTree (com.sun.tools.javac.tree.JCTree)5 Block (com.google.devtools.j2objc.ast.Block)4 VariableDeclarationExpression (com.google.devtools.j2objc.ast.VariableDeclarationExpression)4 DCTree (com.sun.tools.javac.tree.DCTree)4 Assignment (com.google.devtools.j2objc.ast.Assignment)3 CastExpression (com.google.devtools.j2objc.ast.CastExpression)3 CommaExpression (com.google.devtools.j2objc.ast.CommaExpression)3 CompilationUnit (com.google.devtools.j2objc.ast.CompilationUnit)3 AbstractTypeDeclaration (com.google.devtools.j2objc.ast.AbstractTypeDeclaration)2 ExpressionStatement (com.google.devtools.j2objc.ast.ExpressionStatement)2