/** @since 0.8 or earlier */ public static int countNodes(Node root) { return countNodes(root, NodeCountFilter.NO_FILTER); }
/** * Creates a deep copy of this node. * * @return the new deep copy */ public Node deepCopy() { return NodeUtil.deepCopyImpl(this); }
public static <T> T findFirstNodeInstance(Node root, Class<T> clazz) { for (Node childNode : findNodeChildren(root)) { if (clazz.isInstance(childNode)) { return clazz.cast(childNode); } else { T node = findFirstNodeInstance(childNode, clazz); if (node != null) { return node; } } } return null; }
private static void printSourceAttributionTree(PrintWriter p, Node parent, Node node, int level) { if (node == null) { return; } if (parent == null) { // Add some preliminary information before starting with the root node final SourceSection sourceSection = node.getSourceSection(); if (sourceSection != null) { final String txt = sourceSection.getSource().getCode(); p.println("Full source len=(" + txt.length() + ") ___" + txt + "___"); p.println("AST source attribution:"); } } final StringBuilder sb = new StringBuilder(); for (int i = 0; i < level; i++) { sb.append("| "); } if (parent != null) { sb.append(getNodeFieldName(parent, node, "")); } sb.append(" (" + node.getClass().getSimpleName() + ") "); sb.append(printSyntaxTags(node)); sb.append(displaySourceAttribution(node)); p.println(sb.toString()); for (Node child : node.getChildren()) { printSourceAttributionTree(p, node, child, level + 1); } p.flush(); }
static void traceRewrite(Node oldNode, Node newNode, CharSequence reason) { if (TruffleOptions.TraceRewritesFilterFromCost != null) { if (filterByKind(oldNode, TruffleOptions.TraceRewritesFilterFromCost)) { return; } } if (TruffleOptions.TraceRewritesFilterToCost != null) { if (filterByKind(newNode, TruffleOptions.TraceRewritesFilterToCost)) { return; } } String filter = TruffleOptions.TraceRewritesFilterClass; Class<? extends Node> from = oldNode.getClass(); Class<? extends Node> to = newNode.getClass(); if (filter != null && (filterByContainsClassName(from, filter) || filterByContainsClassName(to, filter))) { return; } final SourceSection reportedSourceSection = oldNode.getEncapsulatingSourceSection(); PrintStream out = System.out; out.printf("[truffle] rewrite %-50s |From %-40s |To %-40s |Reason %s %s%n", oldNode.toString(), formatNodeInfo(oldNode), formatNodeInfo(newNode), reason != null && reason.length() > 0 ? reason : "unknown", formatLocation(reportedSourceSection)); }
static void traceRewrite(Node oldNode, Node newNode, CharSequence reason) { if (TruffleOptions.TraceRewritesFilterFromCost != null) { if (filterByKind(oldNode, TruffleOptions.TraceRewritesFilterFromCost)) { return; } } if (TruffleOptions.TraceRewritesFilterToCost != null) { if (filterByKind(newNode, TruffleOptions.TraceRewritesFilterToCost)) { return; } } String filter = TruffleOptions.TraceRewritesFilterClass; Class<? extends Node> from = oldNode.getClass(); Class<? extends Node> to = newNode.getClass(); if (filter != null && (filterByContainsClassName(from, filter) || filterByContainsClassName(to, filter))) { return; } final SourceSection reportedSourceSection = oldNode.getEncapsulatingSourceSection(); PrintStream out = System.out; out.printf("[truffle] rewrite %-50s |From %-40s |To %-40s |Reason %s%s%n", oldNode.toString(), formatNodeInfo(oldNode), formatNodeInfo(newNode), reason != null && reason.length() > 0 ? reason : "unknown", reportedSourceSection != null ? " at " + reportedSourceSection.getShortDescription() : ""); }
/** find annotation in class/interface hierarchy. */ public static <T extends Annotation> T findAnnotation(Class<?> clazz, Class<T> annotationClass) { if (clazz.getAnnotation(annotationClass) != null) { return clazz.getAnnotation(annotationClass); } else { for (Class<?> intf : clazz.getInterfaces()) { if (intf.getAnnotation(annotationClass) != null) { return intf.getAnnotation(annotationClass); } } if (clazz.getSuperclass() != null) { return findAnnotation(clazz.getSuperclass(), annotationClass); } } return null; }
/** @since 0.8 or earlier */ public static <T> List<T> findAllParents(Node start, Class<T> clazz) { List<T> parents = new ArrayList<>(); T parent = findParent(start, clazz); while (parent != null) { parents.add(parent); parent = findParent((Node) parent, clazz); } return parents; }
/** * Invokes the {@link NodeVisitor#visit(Node)} method for this node and recursively also for all * child nodes. * * @param nodeVisitor the visitor * @since 0.8 or earlier */ public final void accept(NodeVisitor nodeVisitor) { if (nodeVisitor.visit(this)) { NodeUtil.forEachChildRecursive(this, nodeVisitor); } }
/** @since 0.8 or earlier */ public static <T> T findFirstNodeInstance(Node root, Class<T> clazz) { if (clazz.isInstance(root)) { return clazz.cast(root); } for (Node child : root.getChildren()) { T node = findFirstNodeInstance(child, clazz); if (node != null) { return node; } } return null; }
private static Node findLastNode(Node contextNode, Set<Class<? extends Tag>> tags) { if (isTaggedWith(contextNode, tags)) { return contextNode; } List<Node> children = NodeUtil.findNodeChildren(contextNode); for (int i = children.size() - 1; i >= 0; i--) { Node ch = children.get(i); if (ch instanceof WrapperNode) { ch = ((WrapperNode) ch).getDelegateNode(); } Node last = findLastNode(ch, tags); if (last != null) { return last; } } return null; }
private static void printSourceAttributionTree(PrintWriter p, Node parent, Node node, int level) { if (node == null) { return; } if (parent == null) { // Add some preliminary information before starting with the root node final SourceSection sourceSection = node.getSourceSection(); if (sourceSection != null) { final String txt = sourceSection.getSource().getCharacters().toString(); p.println("Full source len=(" + txt.length() + ") ___" + txt + "___"); p.println("AST source attribution:"); } } final StringBuilder sb = new StringBuilder(); for (int i = 0; i < level; i++) { sb.append("| "); } if (parent != null) { sb.append(getNodeFieldName(parent, node, "")); } sb.append(" (" + node.getClass().getSimpleName() + ") "); sb.append(printSyntaxTags(node)); sb.append(displaySourceAttribution(node)); p.println(sb.toString()); for (Node child : node.getChildren()) { printSourceAttributionTree(p, node, child, level + 1); } p.flush(); }
static void traceRewrite(Node oldNode, Node newNode, CharSequence reason) { if (TruffleOptions.TraceRewritesFilterFromCost != null) { if (filterByKind(oldNode, TruffleOptions.TraceRewritesFilterFromCost)) { return; } } if (TruffleOptions.TraceRewritesFilterToCost != null) { if (filterByKind(newNode, TruffleOptions.TraceRewritesFilterToCost)) { return; } } String filter = TruffleOptions.TraceRewritesFilterClass; Class<? extends Node> from = oldNode.getClass(); Class<? extends Node> to = newNode.getClass(); if (filter != null && (filterByContainsClassName(from, filter) || filterByContainsClassName(to, filter))) { return; } final SourceSection reportedSourceSection = oldNode.getEncapsulatingSourceSection(); PrintStream out = System.out; out.printf("[truffle] rewrite %-50s |From %-40s |To %-40s |Reason %s %s%n", oldNode.toString(), formatNodeInfo(oldNode), formatNodeInfo(newNode), reason != null && reason.length() > 0 ? reason : "unknown", formatLocation(reportedSourceSection)); }
/** * Find annotation in class/interface hierarchy. * * @since 0.8 or earlier */ public static <T extends Annotation> T findAnnotation(Class<?> clazz, Class<T> annotationClass) { if (clazz.getAnnotation(annotationClass) != null) { return clazz.getAnnotation(annotationClass); } else { if (!TruffleOptions.AOT) { for (Class<?> intf : clazz.getInterfaces()) { if (intf.getAnnotation(annotationClass) != null) { return intf.getAnnotation(annotationClass); } } } if (clazz.getSuperclass() != null) { return findAnnotation(clazz.getSuperclass(), annotationClass); } } return null; }
public static <T> List<T> findAllParents(Node start, Class<T> clazz) { List<T> parents = new ArrayList<>(); T parent = findParent(start, clazz); while (parent != null) { parents.add(parent); parent = findParent((Node) parent, clazz); } return parents; }
private static boolean visitChild(Node child, NodeVisitor visitor) { if (child == null) { return true; } if (!visitor.visit(child)) { return false; } if (!forEachChildRecursive(child, visitor)) { return false; } return true; }
/** @since 0.8 or earlier */ public static <T> T findFirstNodeInstance(Node root, Class<T> clazz) { if (clazz.isInstance(root)) { return clazz.cast(root); } for (Node child : root.getChildren()) { T node = findFirstNodeInstance(child, clazz); if (node != null) { return node; } } return null; }
private static Node findLastNode(Node contextNode, Set<Class<? extends Tag>> tags) { if (isTaggedWith(contextNode, tags)) { return contextNode; } List<Node> children = NodeUtil.findNodeChildren(contextNode); for (int i = children.size() - 1; i >= 0; i--) { Node ch = children.get(i); if (ch instanceof WrapperNode) { ch = ((WrapperNode) ch).getDelegateNode(); } Node last = findLastNode(ch, tags); if (last != null) { return last; } } return null; }
private static void printSourceAttributionTree(PrintWriter p, Node parent, Node node, int level) { if (node == null) { return; } if (parent == null) { // Add some preliminary information before starting with the root node final SourceSection sourceSection = node.getSourceSection(); if (sourceSection != null) { final String txt = sourceSection.getSource().getCharacters().toString(); p.println("Full source len=(" + txt.length() + ") ___" + txt + "___"); p.println("AST source attribution:"); } } final StringBuilder sb = new StringBuilder(); for (int i = 0; i < level; i++) { sb.append("| "); } if (parent != null) { sb.append(getNodeFieldName(parent, node, "")); } sb.append(" (" + node.getClass().getSimpleName() + ") "); sb.append(printSyntaxTags(node)); sb.append(displaySourceAttribution(node)); p.println(sb.toString()); for (Node child : node.getChildren()) { printSourceAttributionTree(p, node, child, level + 1); } p.flush(); }
/** @since 0.8 or earlier */ public static int countNodes(Node root) { return countNodes(root, NodeCountFilter.NO_FILTER); }