comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @ 10845:e9248ebb1d79

Truffle: ignore null children in NodeUtil.findNodeChildren; refactoring.
author Andreas Woess <andreas.woess@jku.at>
date Mon, 22 Jul 2013 17:29:48 +0200
parents 6e12e0ace0d5
children 494b818b527c
comparison
equal deleted inserted replaced
10844:3cf5f371dc9f 10845:e9248ebb1d79
342 } 342 }
343 } 343 }
344 for (long fieldOffset : nodeClass.childrenOffsets) { 344 for (long fieldOffset : nodeClass.childrenOffsets) {
345 Node[] children = (Node[]) unsafe.getObject(node, fieldOffset); 345 Node[] children = (Node[]) unsafe.getObject(node, fieldOffset);
346 if (children != null) { 346 if (children != null) {
347 nodes.addAll(Arrays.asList(children)); 347 for (Node child : children) {
348 if (child != null) {
349 nodes.add(child);
350 }
351 }
348 } 352 }
349 } 353 }
350 354
351 return nodes; 355 return nodes;
352 } 356 }
543 public static int countNodes(Node root) { 547 public static int countNodes(Node root) {
544 return countNodes(root, null); 548 return countNodes(root, null);
545 } 549 }
546 550
547 public static int countNodes(Node root, Class<?> clazz) { 551 public static int countNodes(Node root, Class<?> clazz) {
548 NodeCountVisitor nodeCount = new NodeCountVisitor(clazz); 552 NodeCountVisitor nodeCount = new NodeCountVisitor(root, clazz);
549 root.accept(nodeCount); 553 root.accept(nodeCount);
550 return nodeCount.nodeCount; 554 return nodeCount.nodeCount;
551 } 555 }
552 556
553 private static final class NodeCountVisitor implements NodeVisitor { 557 private static final class NodeCountVisitor implements NodeVisitor {
554 558
559 int nodeCount;
560 private final Node root;
555 private final Class<?> clazz; 561 private final Class<?> clazz;
556 int nodeCount; 562
557 563 private NodeCountVisitor(Node root, Class<?> clazz) {
558 private NodeCountVisitor(Class<?> clazz) { 564 this.root = root;
559 this.clazz = clazz; 565 this.clazz = clazz;
560 } 566 }
561 567
562 @Override 568 @Override
563 public boolean visit(Node node) { 569 public boolean visit(Node node) {
564 if (node instanceof RootNode && nodeCount > 0) { 570 if (node instanceof RootNode && node != root) {
565 return false; 571 return false;
566 } 572 }
567 if (clazz == null || clazz.isInstance(node)) { 573 if (clazz == null || clazz.isInstance(node)) {
568 nodeCount++; 574 nodeCount++;
569 } 575 }
587 } 593 }
588 for (int i = 0; i < level; i++) { 594 for (int i = 0; i < level; i++) {
589 p.print(" "); 595 p.print(" ");
590 } 596 }
591 if (parent == null) { 597 if (parent == null) {
592 p.println(node.getClass().getSimpleName()); 598 p.println(nodeName(node));
593 } else { 599 } else {
594 String fieldName = "unknownField"; 600 String fieldName = "unknownField";
595 NodeField[] fields = NodeClass.get(parent.getClass()).fields; 601 NodeField[] fields = NodeClass.get(parent.getClass()).fields;
596 for (NodeField field : fields) { 602 for (NodeField field : fields) {
597 Object value = field.loadValue(parent); 603 Object value = field.loadValue(parent);
609 } 615 }
610 } 616 }
611 } 617 }
612 p.print(fieldName); 618 p.print(fieldName);
613 p.print(" = "); 619 p.print(" = ");
614 p.println(node.getClass().getSimpleName()); 620 p.println(nodeName(node));
615 } 621 }
616 622
617 for (Node child : node.getChildren()) { 623 for (Node child : node.getChildren()) {
618 printCompactTree(p, node, child, level + 1); 624 printCompactTree(p, node, child, level + 1);
619 } 625 }
647 if (node == null) { 653 if (node == null) {
648 p.print("null"); 654 p.print("null");
649 return; 655 return;
650 } 656 }
651 657
652 p.print(node.getClass().getSimpleName()); 658 p.print(nodeName(node));
653 659
654 ArrayList<NodeField> childFields = new ArrayList<>(); 660 ArrayList<NodeField> childFields = new ArrayList<>();
655 String sep = ""; 661 String sep = "";
656 p.print("("); 662 p.print("(");
657 for (NodeField field : NodeClass.get(node.getClass()).fields) { 663 for (NodeField field : NodeClass.get(node.getClass()).fields) {
702 for (int i = 0; i < level; i++) { 708 for (int i = 0; i < level; i++) {
703 p.print(" "); 709 p.print(" ");
704 } 710 }
705 } 711 }
706 712
713 private static String nodeName(Node node) {
714 return node.getClass().getSimpleName();
715 }
707 } 716 }