Search in sources :

Example 11 with TreeNode

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

the class TreeConverter method convert.

public static TreeNode convert(Object obj) {
    if (obj == null) {
        return null;
    }
    ASTNode jdtNode = (ASTNode) obj;
    TreeNode node = convertInner(jdtNode).setPosition(getPosition(jdtNode));
    node.validate();
    return node;
}
Also used : TreeNode(com.google.devtools.j2objc.ast.TreeNode) ASTNode(org.eclipse.jdt.core.dom.ASTNode)

Example 12 with TreeNode

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

the class Autoboxer method endVisit.

@Override
public void endVisit(CastExpression node) {
    TypeMirror castType = node.getTypeMirror();
    Expression expr = node.getExpression();
    TypeMirror exprType = expr.getTypeMirror();
    if (castType.getKind().isPrimitive() && !exprType.getKind().isPrimitive()) {
        if (typeUtil.isAssignable(exprType, typeUtil.getJavaNumber().asType())) {
            // Casting a Number object to a primitive, convert to value method.
            unbox(expr, (PrimitiveType) castType);
        } else {
            // Casting an object to a primitive. Convert the cast type to the wrapper
            // so that we do a proper cast check, as Java would.
            castType = typeUtil.boxedClass((PrimitiveType) castType).asType();
            node.setType(Type.newType(castType));
            boxOrUnboxExpression(expr, castType);
        }
    } else {
        boxOrUnboxExpression(expr, castType);
    }
    Expression newExpr = node.getExpression();
    if (newExpr != expr) {
        TreeNode parent = node.getParent();
        if (parent instanceof ParenthesizedExpression) {
            parent.replaceWith(TreeUtil.remove(newExpr));
        } else {
            node.replaceWith(TreeUtil.remove(newExpr));
        }
    }
}
Also used : ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) 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) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) InfixExpression(com.google.devtools.j2objc.ast.InfixExpression) ParenthesizedExpression(com.google.devtools.j2objc.ast.ParenthesizedExpression) ConditionalExpression(com.google.devtools.j2objc.ast.ConditionalExpression) TreeNode(com.google.devtools.j2objc.ast.TreeNode)

Example 13 with TreeNode

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

the class UnsequencedExpressionRewriter method isUnsequenced.

private boolean isUnsequenced(VariableAccess modification, Set<TreeNode> modificationAncestors, VariableAccess access) {
    TreeNode commonAncestor = currentTopNode;
    TreeNode node = access.expression;
    while (node != currentTopNode) {
        if (modificationAncestors.contains(node)) {
            commonAncestor = node;
            break;
        }
        node = node.getParent();
    }
    // contain the other access, then they are not unsequenced.
    if (isWithinConditionalBranch(modification.expression, commonAncestor) || isWithinConditionalBranch(access.expression, commonAncestor)) {
        return false;
    } else if (commonAncestor instanceof CommaExpression) {
        return false;
    } else if (commonAncestor instanceof Assignment && modification.expression == commonAncestor) {
        // "i = 1 + i++;" is unsequenced (according to clang).
        return access.expression instanceof PrefixExpression || access.expression instanceof PostfixExpression;
    }
    return true;
}
Also used : Assignment(com.google.devtools.j2objc.ast.Assignment) CommaExpression(com.google.devtools.j2objc.ast.CommaExpression) TreeNode(com.google.devtools.j2objc.ast.TreeNode) PrefixExpression(com.google.devtools.j2objc.ast.PrefixExpression) PostfixExpression(com.google.devtools.j2objc.ast.PostfixExpression)

Example 14 with TreeNode

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

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

the class InnerClassExtractor method endHandleType.

private void endHandleType(AbstractTypeDeclaration node) {
    int insertIdx = typeOrderStack.remove(typeOrderStack.size() - 1);
    TreeNode parentNode = node.getParent();
    if (!(parentNode instanceof CompilationUnit)) {
        // Remove this type declaration from its current location.
        node.remove();
        if (parentNode instanceof TypeDeclarationStatement) {
            parentNode.remove();
        }
        addCaptureFields(node);
        // Make this node non-private, if necessary, and add it to the unit's type
        // list.
        node.removeModifiers(Modifier.PRIVATE);
        unitTypes.add(insertIdx, node);
        // Check for erroneous WeakOuter annotation on static inner class.
        TypeElement type = node.getTypeElement();
        if (ElementUtil.isStatic(type) && ElementUtil.hasAnnotation(type, WeakOuter.class)) {
            ErrorUtil.warning("static class " + type.getQualifiedName() + " has WeakOuter annotation");
        }
    }
}
Also used : CompilationUnit(com.google.devtools.j2objc.ast.CompilationUnit) TreeNode(com.google.devtools.j2objc.ast.TreeNode) TypeDeclarationStatement(com.google.devtools.j2objc.ast.TypeDeclarationStatement) TypeElement(javax.lang.model.element.TypeElement) WeakOuter(com.google.j2objc.annotations.WeakOuter)

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