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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations