Search in sources :

Example 31 with TreePath

use of com.sun.source.util.TreePath in project st-js by st-js.

the class TreeUtils method enclosingOfClass.

/**
 * Gets the first enclosing tree in path, of the specified class
 *
 * @param path
 *            the path defining the tree node
 * @param treeClass
 *            the class of the desired tree
 * @return the enclosing tree of the given type as given by the path
 */
public static <T extends Tree> T enclosingOfClass(final TreePath path, final Class<T> treeClass) {
    TreePath p = path;
    while (p != null) {
        Tree leaf = p.getLeaf();
        if (treeClass.isInstance(leaf)) {
            return treeClass.cast(leaf);
        }
        p = p.getParentPath();
    }
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) ReturnTree(com.sun.source.tree.ReturnTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewArrayTree(com.sun.source.tree.NewArrayTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) JCTree(com.sun.tools.javac.tree.JCTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) BlockTree(com.sun.source.tree.BlockTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) StatementTree(com.sun.source.tree.StatementTree)

Example 32 with TreePath

use of com.sun.source.util.TreePath in project st-js by st-js.

the class TreeUtils method getAssignmentContext.

/**
 * Returns the tree with the assignment context for the treePath leaf node.
 *
 * The assignment context for the treepath is the most enclosing tree of type:
 * <ul>
 * <li>AssignmentTree</li>
 * <li>CompoundAssignmentTree</li>
 * <li>MethodInvocationTree</li>
 * <li>NewArrayTree</li>
 * <li>NewClassTree</li>
 * <li>ReturnTree</li>
 * <li>VariableTree</li>
 * </ul>
 *
 * @param treePath
 *            a {@link com.sun.source.util.TreePath} object.
 * @return the assignment context as described.
 */
public static Tree getAssignmentContext(final TreePath treePath) {
    TreePath path = treePath.getParentPath();
    if (path == null) {
        return null;
    }
    Tree node = path.getLeaf();
    if ((node instanceof AssignmentTree) || (node instanceof CompoundAssignmentTree) || (node instanceof MethodInvocationTree) || (node instanceof NewArrayTree) || (node instanceof NewClassTree) || (node instanceof ReturnTree) || (node instanceof VariableTree)) {
        return node;
    }
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) NewArrayTree(com.sun.source.tree.NewArrayTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) VariableTree(com.sun.source.tree.VariableTree) ReturnTree(com.sun.source.tree.ReturnTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewArrayTree(com.sun.source.tree.NewArrayTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) JCTree(com.sun.tools.javac.tree.JCTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) BlockTree(com.sun.source.tree.BlockTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) StatementTree(com.sun.source.tree.StatementTree) NewClassTree(com.sun.source.tree.NewClassTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) AssignmentTree(com.sun.source.tree.AssignmentTree) ReturnTree(com.sun.source.tree.ReturnTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree)

Example 33 with TreePath

use of com.sun.source.util.TreePath in project st-js by st-js.

the class TreeUtils method pathTillOfKind.

/**
 * Gets path to the the first enclosing tree with any one of the specified kinds.
 *
 * @param path
 *            the path defining the tree node
 * @param kinds
 *            the set of kinds of the desired tree
 * @return the path to the enclosing tree of the given type
 */
public static TreePath pathTillOfKind(final TreePath path, final Set<Tree.Kind> kinds) {
    TreePath p = path;
    while (p != null) {
        Tree leaf = p.getLeaf();
        assert leaf != null;
        /* nninvariant */
        if (kinds.contains(leaf.getKind())) {
            return p;
        }
        p = p.getParentPath();
    }
    return null;
}
Also used : TreePath(com.sun.source.util.TreePath) ReturnTree(com.sun.source.tree.ReturnTree) ArrayAccessTree(com.sun.source.tree.ArrayAccessTree) CompoundAssignmentTree(com.sun.source.tree.CompoundAssignmentTree) LiteralTree(com.sun.source.tree.LiteralTree) MethodTree(com.sun.source.tree.MethodTree) BinaryTree(com.sun.source.tree.BinaryTree) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) TypeCastTree(com.sun.source.tree.TypeCastTree) NewClassTree(com.sun.source.tree.NewClassTree) ParameterizedTypeTree(com.sun.source.tree.ParameterizedTypeTree) IdentifierTree(com.sun.source.tree.IdentifierTree) NewArrayTree(com.sun.source.tree.NewArrayTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ParenthesizedTree(com.sun.source.tree.ParenthesizedTree) ExpressionTree(com.sun.source.tree.ExpressionTree) MemberSelectTree(com.sun.source.tree.MemberSelectTree) JCTree(com.sun.tools.javac.tree.JCTree) ExpressionStatementTree(com.sun.source.tree.ExpressionStatementTree) BlockTree(com.sun.source.tree.BlockTree) PrimitiveTypeTree(com.sun.source.tree.PrimitiveTypeTree) StatementTree(com.sun.source.tree.StatementTree)

Example 34 with TreePath

use of com.sun.source.util.TreePath in project st-js by st-js.

the class TypeCastWriter method visit.

@Override
public JS visit(WriterVisitor<JS> visitor, TypeCastTree tree, GenerationContext<JS> context) {
    TypeMirror type = context.getTrees().getTypeMirror(new TreePath(context.getCurrentPath(), tree.getType()));
    JS expr = visitor.scan(tree.getExpression(), context);
    ExpressionTree expression = tree.getExpression();
    TypeKind fromKind = getKind(expression);
    TypeKind toKind = type.getKind();
    JavaScriptBuilder<JS> b = context.js();
    if (needCastToInt(fromKind, toKind)) {
        // long l = 8*1024*1024*1024;
        // int a = (int) l;
        // var a = ((i)|0);
        JS or = b.binary(BinaryOperator.OR, asList(b.paren(expr), b.number(0)));
        return b.paren(or);
    }
    if (needCastToByte(fromKind, toKind)) {
        // long l = 8*1024*1024*1024;
        // byte a = (byte) l;
        // var a = (l<< 24 >> 24);
        JS lsh = b.binary(LEFT_SHIFT, asList(expr, b.number(BYTE_SHIFT)));
        JS rsh = b.binary(RIGHT_SHIFT, asList(lsh, b.number(BYTE_SHIFT)));
        return b.paren(rsh);
    }
    if (needCastToShort(fromKind, toKind)) {
        // int i = 2*1024*1024*1024; //MAX_VALUE
        // short a = (short) i;
        // var a = ((i)<<16>>16);
        JS lsh = b.binary(LEFT_SHIFT, asList(b.paren(expr), b.number(SHORT_SHIFT)));
        JS rsh = b.binary(RIGHT_SHIFT, asList(lsh, b.number(SHORT_SHIFT)));
        return b.paren(rsh);
    }
    if (needCastToChar(fromKind, toKind)) {
        // int i = 2*1024*1024*1024; //MAX_VALUE
        // char a = (char) i;
        // var a = ((i)&0xffff);
        JS and = b.binary(BinaryOperator.AND, asList(b.paren(expr), b.number(CHAR_MASK)));
        return b.paren(and);
    }
    if (TypesUtils.isIntegral(type)) {
        // add explicit cast in this case
        JS target = b.property(b.name("stjs"), "trunc");
        expr = b.functionCall(target, Collections.singleton(expr));
    }
    // otherwise skip to cast type - continue with the expression
    return expr;
}
Also used : TreePath(com.sun.source.util.TreePath) TypeMirror(javax.lang.model.type.TypeMirror) TypeKind(javax.lang.model.type.TypeKind) ExpressionTree(com.sun.source.tree.ExpressionTree)

Example 35 with TreePath

use of com.sun.source.util.TreePath in project st-js by st-js.

the class BinaryWriter method visit.

@Override
public JS visit(WriterVisitor<JS> visitor, BinaryTree tree, GenerationContext<JS> context) {
    JS left = visitor.scan(tree.getLeftOperand(), context);
    JS right = visitor.scan(tree.getRightOperand(), context);
    BinaryOperator op = BinaryOperator.valueOf(tree.getKind());
    assert op != null : "Unknow operator:" + tree.getKind();
    TypeMirror leftType = context.getTrees().getTypeMirror(new TreePath(context.getCurrentPath(), tree.getLeftOperand()));
    TypeMirror rightType = context.getTrees().getTypeMirror(new TreePath(context.getCurrentPath(), tree.getRightOperand()));
    JavaScriptBuilder<JS> b = context.js();
    if (isStringPlusChar(leftType, rightType)) {
        right = b.functionCall(b.property(b.name("String"), "fromCharCode"), asList(right));
    } else if (isStringPlusChar(rightType, leftType)) {
        left = b.functionCall(b.property(b.name("String"), "fromCharCode"), asList(left));
    }
    boolean integerDivision = tree.getKind() == Kind.DIVIDE && TypesUtils.isIntegral(leftType) && TypesUtils.isIntegral(rightType);
    @SuppressWarnings("unchecked") JS expr = b.binary(op, asList(left, right));
    if (integerDivision) {
        // force a cast for integer division to have the expected behavior in JavaScript too
        JS target = b.property(b.name("stjs"), "trunc");
        return b.functionCall(target, Collections.singleton(expr));
    }
    return expr;
}
Also used : TreePath(com.sun.source.util.TreePath) TypeMirror(javax.lang.model.type.TypeMirror) BinaryOperator(org.stjs.generator.javascript.BinaryOperator)

Aggregations

TreePath (com.sun.source.util.TreePath)151 ExpressionTree (com.sun.source.tree.ExpressionTree)60 Tree (com.sun.source.tree.Tree)60 VariableTree (com.sun.source.tree.VariableTree)50 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)46 MethodTree (com.sun.source.tree.MethodTree)46 ClassTree (com.sun.source.tree.ClassTree)45 MemberSelectTree (com.sun.source.tree.MemberSelectTree)39 IdentifierTree (com.sun.source.tree.IdentifierTree)37 BlockTree (com.sun.source.tree.BlockTree)36 NewClassTree (com.sun.source.tree.NewClassTree)32 StatementTree (com.sun.source.tree.StatementTree)32 JCTree (com.sun.tools.javac.tree.JCTree)31 AssignmentTree (com.sun.source.tree.AssignmentTree)27 BinaryTree (com.sun.source.tree.BinaryTree)27 TypeCastTree (com.sun.source.tree.TypeCastTree)26 ExpressionStatementTree (com.sun.source.tree.ExpressionStatementTree)25 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)25 LiteralTree (com.sun.source.tree.LiteralTree)25 CompoundAssignmentTree (com.sun.source.tree.CompoundAssignmentTree)23