Search in sources :

Example 6 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 7 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 8 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 9 with TreeNode

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

the class StatementGenerator method visit.

@Override
public boolean visit(EmptyStatement node) {
    // Preserve line number difference with owner, to allow suppression of
    // clang empty-statement warnings in Java source.
    TreeNode parent = node.getParent();
    if (parent.getKind() != Kind.SWITCH_STATEMENT && node.getLineNumber() != parent.getLineNumber()) {
        buffer.newline();
        buffer.printIndent();
    }
    buffer.append(";\n");
    return false;
}
Also used : TreeNode(com.google.devtools.j2objc.ast.TreeNode)

Example 10 with TreeNode

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

the class TreeConverter method convertCompilationUnit.

public static CompilationUnit convertCompilationUnit(Options options, JavacEnvironment env, JCTree.JCCompilationUnit javacUnit) {
    String sourceFilePath = getPath(javacUnit.getSourceFile());
    try {
        TreeConverter converter = new TreeConverter(javacUnit, env);
        JavaFileObject sourceFile = javacUnit.getSourceFile();
        String source = sourceFile.getCharContent(false).toString();
        String mainTypeName = FileUtil.getMainTypeName(sourceFile);
        converter.newUnit = new CompilationUnit(new TranslationEnvironment(options, env), sourceFilePath, mainTypeName, source);
        PackageElement pkg = javacUnit.packge != null ? javacUnit.packge : env.defaultPackage();
        converter.newUnit.setPackage(converter.convertPackage(pkg, Trees.instance(env.task())));
        for (JCTree type : javacUnit.getTypeDecls()) {
            TreeNode newNode = converter.convert(type);
            if (newNode.getKind() != TreeNode.Kind.EMPTY_STATEMENT) {
                converter.newUnit.addType((AbstractTypeDeclaration) newNode);
            }
        }
        addOcniComments(converter.newUnit, options.jsniWarnings());
        return converter.newUnit;
    } catch (Throwable e) {
        ErrorUtil.fatalError(e, sourceFilePath);
        return null;
    }
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) JavaFileObject(javax.tools.JavaFileObject) TranslationEnvironment(com.google.devtools.j2objc.util.TranslationEnvironment) TreeNode(com.google.devtools.j2objc.ast.TreeNode) JCTree(com.sun.tools.javac.tree.JCTree) PackageElement(javax.lang.model.element.PackageElement)

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