diff --git a/truffle/com.oracle.truffle.api/snapshot.sigtest b/truffle/com.oracle.truffle.api/snapshot.sigtest index 83320b5fd309..95083cef5040 100644 --- a/truffle/com.oracle.truffle.api/snapshot.sigtest +++ b/truffle/com.oracle.truffle.api/snapshot.sigtest @@ -695,12 +695,27 @@ intf java.lang.annotation.Annotation CLSS public abstract com.oracle.truffle.api.nodes.NodeClass cons public init(java.lang.Class) +meth protected abstract boolean isChildField(java.lang.Object) +meth protected abstract boolean isChildrenField(java.lang.Object) +meth protected abstract boolean isCloneableField(java.lang.Object) +meth protected abstract java.lang.Class getFieldType(java.lang.Object) +meth protected abstract java.lang.Iterable getNodeFields() +meth protected abstract java.lang.Object getFieldObject(java.lang.Object,com.oracle.truffle.api.nodes.Node) +meth protected abstract java.lang.Object getFieldValue(java.lang.Object,com.oracle.truffle.api.nodes.Node) +meth protected abstract java.lang.String getFieldName(java.lang.Object) +meth protected abstract void putFieldObject(java.lang.Object,com.oracle.truffle.api.nodes.Node,java.lang.Object) meth public abstract com.oracle.truffle.api.nodes.NodeFieldAccessor getNodeClassField() + anno 0 java.lang.Deprecated() meth public abstract com.oracle.truffle.api.nodes.NodeFieldAccessor getParentField() + anno 0 java.lang.Deprecated() meth public abstract com.oracle.truffle.api.nodes.NodeFieldAccessor[] getChildFields() + anno 0 java.lang.Deprecated() meth public abstract com.oracle.truffle.api.nodes.NodeFieldAccessor[] getChildrenFields() + anno 0 java.lang.Deprecated() meth public abstract com.oracle.truffle.api.nodes.NodeFieldAccessor[] getCloneableFields() + anno 0 java.lang.Deprecated() meth public abstract com.oracle.truffle.api.nodes.NodeFieldAccessor[] getFields() + anno 0 java.lang.Deprecated() meth public abstract java.lang.Class getType() meth public abstract java.util.Iterator makeIterator(com.oracle.truffle.api.nodes.Node) meth public static com.oracle.truffle.api.nodes.NodeClass get(com.oracle.truffle.api.nodes.Node) @@ -726,12 +741,14 @@ meth public static com.oracle.truffle.api.nodes.NodeCost[] values() supr java.lang.Enum CLSS public abstract com.oracle.truffle.api.nodes.NodeFieldAccessor + anno 0 java.lang.Deprecated() cons protected init(com.oracle.truffle.api.nodes.NodeFieldAccessor$NodeFieldKind,java.lang.Class,java.lang.String,java.lang.Class) fld protected final java.lang.Class type innr public abstract static AbstractUnsafeNodeFieldAccessor innr public final static !enum NodeFieldKind meth protected static com.oracle.truffle.api.nodes.NodeFieldAccessor create(com.oracle.truffle.api.nodes.NodeFieldAccessor$NodeFieldKind,java.lang.reflect.Field) meth public abstract java.lang.Object getObject(com.oracle.truffle.api.nodes.Node) + anno 0 java.lang.Deprecated() meth public abstract java.lang.Object loadValue(com.oracle.truffle.api.nodes.Node) meth public abstract void putObject(com.oracle.truffle.api.nodes.Node,java.lang.Object) anno 0 java.lang.Deprecated() @@ -739,6 +756,7 @@ meth public com.oracle.truffle.api.nodes.NodeFieldAccessor$NodeFieldKind getKind meth public java.lang.Class getDeclaringClass() meth public java.lang.Class getType() meth public java.lang.String getName() +meth public java.lang.String toString() supr java.lang.Object hfds USE_UNSAFE,declaringClass,kind,name hcls ReflectionNodeField,UnsafeNodeField @@ -793,6 +811,7 @@ meth public static boolean replaceChild(com.oracle.truffle.api.nodes.Node,com.or meth public static boolean verify(com.oracle.truffle.api.nodes.Node) meth public static com.oracle.truffle.api.nodes.Node getNthParent(com.oracle.truffle.api.nodes.Node,int) meth public static com.oracle.truffle.api.nodes.NodeFieldAccessor findChildField(com.oracle.truffle.api.nodes.Node,com.oracle.truffle.api.nodes.Node) + anno 0 java.lang.Deprecated() meth public static int countNodes(com.oracle.truffle.api.nodes.Node) meth public static int countNodes(com.oracle.truffle.api.nodes.Node,com.oracle.truffle.api.nodes.NodeUtil$NodeCountFilter) meth public static java.lang.String printCompactTreeToString(com.oracle.truffle.api.nodes.Node) diff --git a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/ProbeFailure.java b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/ProbeFailure.java index 115657725185..76d61741eca4 100644 --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/ProbeFailure.java +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/ProbeFailure.java @@ -25,7 +25,6 @@ package com.oracle.truffle.api.instrument; import com.oracle.truffle.api.nodes.Node; -import com.oracle.truffle.api.nodes.NodeFieldAccessor; import com.oracle.truffle.api.nodes.NodeUtil; /** @@ -139,13 +138,14 @@ public Object getWrapper() { } /** @since 0.8 or earlier */ + @SuppressWarnings("deprecation") public String getMessage() { final StringBuilder sb = new StringBuilder(reason.message + ": "); if (parent != null) { sb.append("parent=" + parent.getClass().getSimpleName() + " "); if (child != null) { sb.append("child=" + child.getClass().getSimpleName() + " "); - final NodeFieldAccessor field = NodeUtil.findChildField(parent, child); + final com.oracle.truffle.api.nodes.NodeFieldAccessor field = NodeUtil.findChildField(parent, child); if (field != null) { sb.append("field=" + field.getName() + " "); } diff --git a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/impl/DefaultASTPrinter.java b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/impl/DefaultASTPrinter.java index 50e279caeece..be9bb68a7f69 100644 --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/impl/DefaultASTPrinter.java +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/instrument/impl/DefaultASTPrinter.java @@ -30,8 +30,6 @@ import com.oracle.truffle.api.nodes.Node; import com.oracle.truffle.api.nodes.NodeClass; -import com.oracle.truffle.api.nodes.NodeFieldAccessor; -import com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind; import com.oracle.truffle.api.nodes.NodeUtil; import com.oracle.truffle.api.source.SourceSection; @@ -105,12 +103,12 @@ protected void printTree(PrintWriter p, Node node, int maxDepth, Node markNode, p.print(NodeUtil.printSyntaxTags(node)); - ArrayList childFields = new ArrayList<>(); + ArrayList childFields = new ArrayList<>(); - for (NodeFieldAccessor field : NodeClass.get(node).getFields()) { - if (field.getKind() == NodeFieldKind.CHILD || field.getKind() == NodeFieldKind.CHILDREN) { + for (com.oracle.truffle.api.nodes.NodeFieldAccessor field : NodeClass.get(node).getFields()) { + if (field.getKind() == com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind.CHILD || field.getKind() == com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind.CHILDREN) { childFields.add(field); - } else if (field.getKind() == NodeFieldKind.DATA) { + } else if (field.getKind() == com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind.DATA) { // p.print(sep); // sep = ", "; // @@ -129,16 +127,16 @@ protected void printTree(PrintWriter p, Node node, int maxDepth, Node markNode, if (childFields.size() != 0) { p.print(" {"); - for (NodeFieldAccessor field : childFields) { + for (com.oracle.truffle.api.nodes.NodeFieldAccessor field : childFields) { Object value = field.loadValue(node); if (value == null) { printNewLine(p, level); p.print(field.getName()); p.print(" = null "); - } else if (field.getKind() == NodeFieldKind.CHILD) { + } else if (field.getKind() == com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind.CHILD) { printChild(p, maxDepth, markNode, level, field, value); - } else if (field.getKind() == NodeFieldKind.CHILDREN) { + } else if (field.getKind() == com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind.CHILDREN) { printChildren(p, maxDepth, markNode, level, field, value); } else { printNewLine(p, level); @@ -151,7 +149,7 @@ protected void printTree(PrintWriter p, Node node, int maxDepth, Node markNode, } } - protected void printChildren(PrintWriter p, int maxDepth, Node markNode, int level, NodeFieldAccessor field, Object value) { + protected void printChildren(PrintWriter p, int maxDepth, Node markNode, int level, com.oracle.truffle.api.nodes.NodeFieldAccessor field, Object value) { printNewLine(p, level); p.print(field.getName()); Node[] children = (Node[]) value; @@ -165,7 +163,7 @@ protected void printChildren(PrintWriter p, int maxDepth, Node markNode, int lev p.print("]"); } - protected void printChild(PrintWriter p, int maxDepth, Node markNode, int level, NodeFieldAccessor field, Object value) { + protected void printChild(PrintWriter p, int maxDepth, Node markNode, int level, com.oracle.truffle.api.nodes.NodeFieldAccessor field, Object value) { final Node valueNode = (Node) value; printNewLine(p, level, valueNode == markNode); p.print(field.getName()); diff --git a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java index 9042e534ecfb..bfe9613db505 100644 --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/GraphPrintVisitor.java @@ -50,11 +50,10 @@ import javax.xml.stream.XMLStreamWriter; import com.oracle.truffle.api.TruffleOptions; -import com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind; /** * Utility class for creating output for the ideal graph visualizer. - * + * * @since 0.8 or earlier */ public class GraphPrintVisitor implements Closeable { @@ -488,18 +487,22 @@ private void copyDebugProperties(Node node) { } private void readNodeProperties(Node node) { - NodeFieldAccessor[] fields = NodeClass.get(node).getFields(); - for (NodeFieldAccessor field : fields) { - if (field.getKind() == NodeFieldKind.DATA) { - String key = field.getName(); + NodeClass nodeClass = NodeClass.get(node); + for (Object field : nodeClass.getNodeFields()) { + if (isDataField(nodeClass, field)) { + String key = nodeClass.getFieldName(field); if (!getElementByObject(node).getProperties().containsKey(key)) { - Object value = field.loadValue(node); + Object value = nodeClass.getFieldValue(field, node); setNodeProperty(node, key, value); } } } } + private static boolean isDataField(NodeClass nodeClass, Object field) { + return !nodeClass.isChildField(field) && !nodeClass.isChildrenField(field); + } + final void connectNodes(Object a, Object b, String label) { NodeElement fromNode = getElementByObject(a); NodeElement toNode = getElementByObject(b); @@ -564,19 +567,19 @@ private static LinkedHashMap findNamedNodeChildren(Node node) { LinkedHashMap nodes = new LinkedHashMap<>(); NodeClass nodeClass = NodeClass.get(node); - for (NodeFieldAccessor field : nodeClass.getFields()) { - NodeFieldKind kind = field.getKind(); - if (kind == NodeFieldKind.CHILD || kind == NodeFieldKind.CHILDREN) { - Object value = field.loadValue(node); + for (Object field : nodeClass.getNodeFields()) { + if (nodeClass.isChildField(field)) { + Object value = nodeClass.getFieldObject(field, node); + if (value != null) { + nodes.put(nodeClass.getFieldName(field), (Node) value); + } + } else if (nodeClass.isChildrenField(field)) { + Object value = nodeClass.getFieldObject(field, node); if (value != null) { - if (kind == NodeFieldKind.CHILD) { - nodes.put(field.getName(), (Node) value); - } else if (kind == NodeFieldKind.CHILDREN) { - Object[] children = (Object[]) value; - for (int i = 0; i < children.length; i++) { - if (children[i] != null) { - nodes.put(field.getName() + "[" + i + "]", (Node) children[i]); - } + Object[] children = (Object[]) value; + for (int i = 0; i < children.length; i++) { + if (children[i] != null) { + nodes.put(nodeClass.getFieldName(field) + "[" + i + "]", (Node) children[i]); } } } @@ -598,7 +601,7 @@ private static String safeToString(Object value) { public class GraphPrintAdapter { /** * Default constructor. - * + * * @since 0.8 or earlier */ public GraphPrintAdapter() { diff --git a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java index 9517964435b7..20463b97a4ae 100644 --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java @@ -87,6 +87,10 @@ NodeClass getNodeClass() { return nodeClass; } + void setParent(Node parent) { + this.parent = parent; + } + /** * Returns a rough estimate for the cost of this {@link Node}. This estimate can be used by * runtime systems or guest languages to implement heuristics based on Truffle ASTs. This method diff --git a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeClass.java b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeClass.java index 486dec059f5c..6fdad1fb53a4 100644 --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeClass.java +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeClass.java @@ -31,7 +31,7 @@ /** * Information about a {@link Node} class. A single instance of this class is allocated for every * subclass of {@link Node} that is used. - * + * * @since 0.8 or earlier */ public abstract class NodeClass { @@ -59,26 +59,37 @@ public static NodeClass get(Node node) { } /** @since 0.8 or earlier */ - @SuppressWarnings("unused") - public NodeClass(Class clazz) { + public NodeClass(@SuppressWarnings("unused") Class clazz) { } /** @since 0.8 or earlier */ + @SuppressWarnings("deprecation") + @Deprecated public abstract NodeFieldAccessor getNodeClassField(); /** @since 0.8 or earlier */ + @SuppressWarnings("deprecation") + @Deprecated public abstract NodeFieldAccessor[] getCloneableFields(); /** @since 0.8 or earlier */ + @SuppressWarnings("deprecation") + @Deprecated public abstract NodeFieldAccessor[] getFields(); /** @since 0.8 or earlier */ + @SuppressWarnings("deprecation") + @Deprecated public abstract NodeFieldAccessor getParentField(); /** @since 0.8 or earlier */ + @SuppressWarnings("deprecation") + @Deprecated public abstract NodeFieldAccessor[] getChildFields(); /** @since 0.8 or earlier */ + @SuppressWarnings("deprecation") + @Deprecated public abstract NodeFieldAccessor[] getChildrenFields(); /** @since 0.8 or earlier */ @@ -87,9 +98,50 @@ public NodeClass(Class clazz) { /** * The {@link Class} this NodeClass has been {@link #NodeClass(java.lang.Class) * created for}. - * + * * @return the clazz of node this NodeClass describes * @since 0.8 or earlier */ public abstract Class getType(); + + /** @since 0.14 */ + protected abstract Iterable getNodeFields(); + + /** @since 0.14 */ + protected abstract void putFieldObject(Object field, Node receiver, Object value); + + /** @since 0.14 */ + protected abstract Object getFieldObject(Object field, Node receiver); + + /** @since 0.14 */ + protected abstract Object getFieldValue(Object field, Node receiver); + + /** @since 0.14 */ + protected abstract Class getFieldType(Object field); + + /** @since 0.14 */ + protected abstract String getFieldName(Object field); + + /** @since 0.14 */ + protected abstract boolean isChildField(Object field); + + /** @since 0.14 */ + protected abstract boolean isChildrenField(Object field); + + /** @since 0.14 */ + protected abstract boolean isCloneableField(Object field); + + /** + * If and only if this method returns {@code true}, {@link #getNodeFields()} adheres to the + * following iteration order. + *
    + *
  • {@link Node.Child @Child} fields + *
  • {@link Node.Children @Children} fields + *
  • {@link NodeCloneable} fields + *
  • Other fields + *
+ */ + boolean nodeFieldsOrderedByKind() { + return false; + } } diff --git a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeClassImpl.java b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeClassImpl.java index 010ada97cbfe..2e65db1656c3 100644 --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeClassImpl.java +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeClassImpl.java @@ -27,28 +27,27 @@ import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.Iterator; import java.util.List; +import java.util.NoSuchElementException; import com.oracle.truffle.api.nodes.Node.Child; import com.oracle.truffle.api.nodes.Node.Children; -import com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind; /** * Information about a {@link Node} class. A single instance of this class is allocated for every * subclass of {@link Node} that is used. */ +@SuppressWarnings("deprecation") final class NodeClassImpl extends NodeClass { private static final NodeFieldAccessor[] EMPTY_NODE_FIELD_ARRAY = new NodeFieldAccessor[0]; // The comprehensive list of all fields. private final NodeFieldAccessor[] fields; - // Separate arrays for the frequently accessed fields. private final NodeFieldAccessor parentField; private final NodeFieldAccessor nodeClassField; - private final NodeFieldAccessor[] childFields; - private final NodeFieldAccessor[] childrenFields; - private final NodeFieldAccessor[] cloneableFields; private final Class clazz; @@ -61,36 +60,39 @@ final class NodeClassImpl extends NodeClass { List fieldsList = new ArrayList<>(); NodeFieldAccessor parentFieldTmp = null; NodeFieldAccessor nodeClassFieldTmp = null; - List childFieldList = new ArrayList<>(); - List childrenFieldList = new ArrayList<>(); - List cloneableFieldList = new ArrayList<>(); try { Field field = Node.class.getDeclaredField("parent"); assert Node.class.isAssignableFrom(field.getType()); - parentFieldTmp = NodeFieldAccessor.create(NodeFieldKind.PARENT, field); + parentFieldTmp = NodeFieldAccessor.create(NodeFieldAccessor.NodeFieldKind.PARENT, field); field = Node.class.getDeclaredField("nodeClass"); assert NodeClass.class.isAssignableFrom(field.getType()); - nodeClassFieldTmp = NodeFieldAccessor.create(NodeFieldKind.NODE_CLASS, field); + nodeClassFieldTmp = NodeFieldAccessor.create(NodeFieldAccessor.NodeFieldKind.NODE_CLASS, field); } catch (NoSuchFieldException e) { throw new AssertionError("Node field not found", e); } - collectInstanceFields(clazz, fieldsList, childFieldList, childrenFieldList, cloneableFieldList); + collectInstanceFields(clazz, fieldsList); + + Collections.sort(fieldsList, new Comparator() { + public int compare(NodeFieldAccessor o1, NodeFieldAccessor o2) { + return Integer.compare(order(o1), order(o2)); + } + + private int order(NodeFieldAccessor nodeField) { + return isChildField(nodeField) ? 0 : (isChildrenField(nodeField) ? 1 : (isCloneableField(nodeField) ? 2 : 3)); + } + }); this.fields = fieldsList.toArray(EMPTY_NODE_FIELD_ARRAY); this.nodeClassField = nodeClassFieldTmp; this.parentField = parentFieldTmp; - this.childFields = childFieldList.toArray(EMPTY_NODE_FIELD_ARRAY); - this.childrenFields = childrenFieldList.toArray(EMPTY_NODE_FIELD_ARRAY); - this.cloneableFields = cloneableFieldList.toArray(EMPTY_NODE_FIELD_ARRAY); this.clazz = clazz; } - private static void collectInstanceFields(Class clazz, List fieldsList, List childFieldList, List childrenFieldList, - List cloneableFieldList) { + private static void collectInstanceFields(Class clazz, List fieldsList) { if (clazz.getSuperclass() != null) { - collectInstanceFields(clazz.getSuperclass(), fieldsList, childFieldList, childrenFieldList, cloneableFieldList); + collectInstanceFields(clazz.getSuperclass(), fieldsList); } Field[] declaredFields = clazz.getDeclaredFields(); for (Field field : declaredFields) { @@ -103,17 +105,12 @@ private static void collectInstanceFields(Class clazz, List clazz) { return Node.class.isAssignableFrom(clazz) || (clazz.isInterface() && NodeInterface.class.isAssignableFrom(clazz)); } @@ -151,26 +143,11 @@ private static void checkChildrenField(Field field) { } } - @Override - public NodeFieldAccessor[] getFields() { - return fields; - } - @Override public NodeFieldAccessor getParentField() { return parentField; } - @Override - public NodeFieldAccessor[] getChildFields() { - return childFields; - } - - @Override - public NodeFieldAccessor[] getChildrenFields() { - return childrenFields; - } - @Override public int hashCode() { return clazz.hashCode(); @@ -196,64 +173,211 @@ public Class getType() { return clazz; } + @Override + protected Iterable getNodeFields() { + return getNodeFields(null); + } + + /** + * Functional interface equivalent to {@code Predicate}. + */ + private interface NodeFieldFilter { + boolean test(NodeFieldAccessor field); + } + + private Iterable getNodeFields(final NodeFieldFilter filter) { + return new Iterable() { + public Iterator iterator() { + return new Iterator() { + private int cursor = -1; + { + forward(); + } + + private void forward() { + for (int i = cursor + 1; i < fields.length; i++) { + NodeFieldAccessor field = fields[i]; + if (filter == null || filter.test(field)) { + cursor = i; + return; + } + } + cursor = fields.length; + } + + public boolean hasNext() { + assert cursor >= 0; + return cursor < fields.length; + } + + public NodeFieldAccessor next() { + if (hasNext()) { + NodeFieldAccessor next = fields[cursor]; + forward(); + return next; + } else { + throw new NoSuchElementException(); + } + } + + public void remove() { + throw new UnsupportedOperationException(); + } + }; + } + }; + } + + @Override + public NodeFieldAccessor[] getFields() { + return iterableToArray(getNodeFields()); + } + + @Override + public NodeFieldAccessor[] getChildFields() { + return iterableToArray(getNodeFields(new NodeFieldFilter() { + public boolean test(NodeFieldAccessor field) { + return isChildField(field); + } + })); + } + + @Override + public NodeFieldAccessor[] getChildrenFields() { + return iterableToArray(getNodeFields(new NodeFieldFilter() { + public boolean test(NodeFieldAccessor field) { + return isChildrenField(field); + } + })); + } + + @Override + public NodeFieldAccessor[] getCloneableFields() { + return iterableToArray(getNodeFields(new NodeFieldFilter() { + public boolean test(NodeFieldAccessor field) { + return isCloneableField(field); + } + })); + } + + private static NodeFieldAccessor[] iterableToArray(Iterable fields) { + ArrayList fieldList = new ArrayList<>(); + for (NodeFieldAccessor field : fields) { + fieldList.add(field); + } + return fieldList.toArray(new NodeFieldAccessor[0]); + } + + @Override + protected void putFieldObject(Object field, Node receiver, Object value) { + ((NodeFieldAccessor) field).putObject(receiver, value); + } + + @Override + protected Object getFieldObject(Object field, Node receiver) { + return ((NodeFieldAccessor) field).getObject(receiver); + } + + @Override + protected Object getFieldValue(Object field, Node receiver) { + return ((NodeFieldAccessor) field).loadValue(receiver); + } + + @Override + protected Class getFieldType(Object field) { + return ((NodeFieldAccessor) field).getType(); + } + + @Override + protected String getFieldName(Object field) { + return ((NodeFieldAccessor) field).getName(); + } + + @Override + protected boolean isChildField(Object field) { + return ((NodeFieldAccessor) field).getKind() == NodeFieldAccessor.NodeFieldKind.CHILD; + } + + @Override + protected boolean isChildrenField(Object field) { + return ((NodeFieldAccessor) field).getKind() == NodeFieldAccessor.NodeFieldKind.CHILDREN; + } + + @Override + protected boolean isCloneableField(Object field) { + return ((NodeFieldAccessor) field).getKind() == NodeFieldAccessor.NodeFieldKind.DATA && NodeCloneable.class.isAssignableFrom(((NodeFieldAccessor) field).getType()); + } + + @Override + boolean nodeFieldsOrderedByKind() { + return true; + } + private static final class NodeIterator implements Iterator { - private final NodeFieldAccessor[] childFields; - private final NodeFieldAccessor[] childrenFields; + private final NodeClassImpl nodeClass; + private final NodeFieldAccessor[] fields; private final Node node; - private final int childrenCount; - private int index; + + private int fieldIndex; + private Node next; + private int childrenIndex; + private Object[] children; protected NodeIterator(NodeClassImpl nodeClass, Node node) { - this.childFields = nodeClass.getChildFields(); - this.childrenFields = nodeClass.getChildrenFields(); + this.nodeClass = nodeClass; + this.fields = nodeClass.fields; this.node = node; - this.childrenCount = childrenCount(); - this.index = 0; + advance(); } - private int childrenCount() { - int nodeCount = childFields.length; - for (NodeFieldAccessor childrenField : childrenFields) { - Object[] children = ((Object[]) childrenField.getObject(node)); - if (children != null) { - nodeCount += children.length; - } + private void advance() { + if (advanceChildren()) { + return; } - return nodeCount; - } - - private Node nodeAt(int idx) { - int nodeCount = childFields.length; - if (idx < nodeCount) { - return (Node) childFields[idx].getObject(node); - } else { - for (NodeFieldAccessor childrenField : childrenFields) { - Object[] nodeArray = (Object[]) childrenField.getObject(node); - if (idx < nodeCount + nodeArray.length) { - return (Node) nodeArray[idx - nodeCount]; + while (fieldIndex < fields.length) { + NodeFieldAccessor field = fields[fieldIndex]; + fieldIndex++; + if (nodeClass.isChildField(field)) { + next = (Node) nodeClass.getFieldObject(field, node); + return; + } else if (nodeClass.isChildrenField(field)) { + children = (Object[]) nodeClass.getFieldObject(field, node); + childrenIndex = 0; + if (advanceChildren()) { + return; } - nodeCount += nodeArray.length; + } else if (nodeClass.nodeFieldsOrderedByKind()) { + break; } } - return null; + next = null; } - private void forward() { - if (index < childrenCount) { - index++; + private boolean advanceChildren() { + if (children == null) { + return false; + } else if (childrenIndex < children.length) { + next = (Node) children[childrenIndex]; + childrenIndex++; + return true; + } else { + children = null; + childrenIndex = 0; + return false; } } public boolean hasNext() { - return index < childrenCount; + return next != null; } public Node next() { - try { - return nodeAt(index); - } finally { - forward(); + Node result = next; + if (result == null) { + throw new NoSuchElementException(); } + advance(); + return result; } public void remove() { diff --git a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeFieldAccessor.java b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeFieldAccessor.java index 92102b0a8d18..00d18f8a6a5a 100644 --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeFieldAccessor.java +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeFieldAccessor.java @@ -26,46 +26,47 @@ import java.lang.reflect.Field; -import sun.misc.Unsafe; - import com.oracle.truffle.api.nodes.Node.Child; import com.oracle.truffle.api.nodes.Node.Children; +import sun.misc.Unsafe; + /** * Information about a field in a {@link Node} class. - * + * * @since 0.8 or earlier */ +@Deprecated public abstract class NodeFieldAccessor { /** @since 0.8 or earlier */ public enum NodeFieldKind { /** * The reference to the {@link NodeClass}. - * + * * @since 0.8 or earlier */ NODE_CLASS, /** * The single {@link Node#getParent() parent} field. - * + * * @since 0.8 or earlier */ PARENT, /** * A field annotated with {@link Child}. - * + * * @since 0.8 or earlier */ CHILD, /** * A field annotated with {@link Children}. - * + * * @since 0.8 or earlier */ CHILDREN, /** * A normal non-child data field of the node. - * + * * @since 0.8 or earlier */ DATA @@ -124,13 +125,24 @@ public String getName() { @Deprecated public abstract void putObject(Node receiver, Object value); - /** @since 0.8 or earlier */ + /** + * @deprecated The visibility of this method will be reduced to protected. Do not use. + * @since 0.8 or earlier + */ + @Deprecated public abstract Object getObject(Node receiver); /** @since 0.8 or earlier */ public abstract Object loadValue(Node node); + /** @since 0.14 */ + @Override + public String toString() { + return getDeclaringClass().getName() + "." + getName(); + } + /** @since 0.8 or earlier */ + @SuppressWarnings("deprecation") public abstract static class AbstractUnsafeNodeFieldAccessor extends NodeFieldAccessor { /** @since 0.8 or earlier */ protected AbstractUnsafeNodeFieldAccessor(NodeFieldKind kind, Class declaringClass, String name, Class type) { @@ -141,16 +153,19 @@ protected AbstractUnsafeNodeFieldAccessor(NodeFieldKind kind, Class declaring public abstract long getOffset(); /** @since 0.8 or earlier */ - @SuppressWarnings("deprecation") @Override public void putObject(Node receiver, Object value) { if (!type.isPrimitive() && value == null || type.isInstance(value)) { unsafe.putObject(receiver, getOffset(), value); } else { - throw new IllegalArgumentException(); + throw illegalArgumentException(value); } } + private IllegalArgumentException illegalArgumentException(Object value) { + return new IllegalArgumentException("Cannot set " + getType().getName() + " field " + toString() + " to " + (value == null ? "null" : value.getClass().getName())); + } + /** @since 0.8 or earlier */ @Override public Object getObject(Node receiver) { @@ -202,6 +217,7 @@ private static Unsafe getUnsafe() { } } + @SuppressWarnings("deprecation") private static final class UnsafeNodeField extends AbstractUnsafeNodeFieldAccessor { private final long offset; @@ -216,6 +232,7 @@ public long getOffset() { } } + @SuppressWarnings("deprecation") private static final class ReflectionNodeField extends NodeFieldAccessor { private final Field field; @@ -225,7 +242,6 @@ protected ReflectionNodeField(NodeFieldKind kind, Field field) { field.setAccessible(true); } - @SuppressWarnings("deprecation") @Override public void putObject(Node receiver, Object value) { assert !type.isPrimitive() && value == null || type.isInstance(value); diff --git a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java index bbeae4eb7ee1..6eb51afaad08 100644 --- a/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java +++ b/truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @@ -39,7 +39,6 @@ import com.oracle.truffle.api.CompilerAsserts; import com.oracle.truffle.api.TruffleOptions; -import com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind; import com.oracle.truffle.api.source.SourceSection; /** @@ -135,40 +134,41 @@ public static T cloneNode(T orig) { return (T) orig.deepCopy(); } - @SuppressWarnings("deprecation") static Node deepCopyImpl(Node orig) { CompilerAsserts.neverPartOfCompilation("do not call Node.deepCopyImpl from compiled code"); final Node clone = orig.copy(); NodeClass nodeClass = clone.getNodeClass(); - nodeClass.getParentField().putObject(clone, null); + clone.setParent(null); - for (NodeFieldAccessor childField : nodeClass.getChildFields()) { - Node child = (Node) childField.getObject(orig); - if (child != null) { - Node clonedChild = child.deepCopy(); - nodeClass.getParentField().putObject(clonedChild, clone); - childField.putObject(clone, clonedChild); - } - } - for (NodeFieldAccessor childrenField : nodeClass.getChildrenFields()) { - Object[] children = (Object[]) childrenField.getObject(orig); - if (children != null) { - Object[] clonedChildren = (Object[]) Array.newInstance(children.getClass().getComponentType(), children.length); - for (int i = 0; i < children.length; i++) { - if (children[i] != null) { - Node clonedChild = ((Node) children[i]).deepCopy(); - clonedChildren[i] = clonedChild; - nodeClass.getParentField().putObject(clonedChild, clone); + for (Object field : nodeClass.getNodeFields()) { + if (nodeClass.isChildField(field)) { + Node child = (Node) nodeClass.getFieldObject(field, orig); + if (child != null) { + Node clonedChild = child.deepCopy(); + clonedChild.setParent(clone); + nodeClass.putFieldObject(field, clone, clonedChild); + } + } else if (nodeClass.isChildrenField(field)) { + Object[] children = (Object[]) nodeClass.getFieldObject(field, orig); + if (children != null) { + Object[] clonedChildren = (Object[]) Array.newInstance(children.getClass().getComponentType(), children.length); + for (int i = 0; i < children.length; i++) { + if (children[i] != null) { + Node clonedChild = ((Node) children[i]).deepCopy(); + clonedChild.setParent(clone); + clonedChildren[i] = clonedChild; + } } + nodeClass.putFieldObject(field, clone, clonedChildren); } - childrenField.putObject(clone, clonedChildren); - } - } - for (NodeFieldAccessor cloneableField : nodeClass.getCloneableFields()) { - Object cloneable = cloneableField.getObject(clone); - if (cloneable != null && cloneable == cloneableField.getObject(orig)) { - cloneableField.putObject(clone, ((NodeCloneable) cloneable).clone()); + } else if (nodeClass.isCloneableField(field)) { + Object cloneable = nodeClass.getFieldObject(field, clone); + if (cloneable != null && cloneable == nodeClass.getFieldObject(field, orig)) { + nodeClass.putFieldObject(field, clone, ((NodeCloneable) cloneable).clone()); + } + } else if (nodeClass.nodeFieldsOrderedByKind()) { + break; } } return clone; @@ -180,23 +180,25 @@ public static List findNodeChildren(Node node) { List nodes = new ArrayList<>(); NodeClass nodeClass = node.getNodeClass(); - for (NodeFieldAccessor nodeField : nodeClass.getChildFields()) { - Object child = nodeField.getObject(node); - if (child != null) { - nodes.add((Node) child); - } - } - for (NodeFieldAccessor nodeField : nodeClass.getChildrenFields()) { - Object[] children = (Object[]) nodeField.getObject(node); - if (children != null) { - for (Object child : children) { - if (child != null) { - nodes.add((Node) child); + for (Object nodeField : nodeClass.getNodeFields()) { + if (nodeClass.isChildField(nodeField)) { + Object child = nodeClass.getFieldObject(nodeField, node); + if (child != null) { + nodes.add((Node) child); + } + } else if (nodeClass.isChildrenField(nodeField)) { + Object[] children = (Object[]) nodeClass.getFieldObject(nodeField, node); + if (children != null) { + for (Object child : children) { + if (child != null) { + nodes.add((Node) child); + } } } + } else if (nodeClass.nodeFieldsOrderedByKind()) { + break; } } - return nodes; } @@ -211,61 +213,47 @@ public static boolean replaceChild(Node parent, Node oldChild, Node newChild) { return replaceChild(parent, oldChild, newChild, false); } - @SuppressWarnings("deprecation") static boolean replaceChild(Node parent, Node oldChild, Node newChild, boolean adopt) { CompilerAsserts.neverPartOfCompilation("do not replace Node child from compiled code"); NodeClass nodeClass = parent.getNodeClass(); - for (NodeFieldAccessor nodeField : nodeClass.getChildFields()) { - if (nodeField.getObject(parent) == oldChild) { - assert assertAssignable(nodeField, newChild); - if (adopt) { - parent.adoptHelper(newChild); + for (Object nodeField : nodeClass.getNodeFields()) { + if (nodeClass.isChildField(nodeField)) { + if (nodeClass.getFieldObject(nodeField, parent) == oldChild) { + if (adopt) { + parent.adoptHelper(newChild); + } + nodeClass.putFieldObject(nodeField, parent, newChild); + return true; } - nodeField.putObject(parent, newChild); - return true; - } - } - - for (NodeFieldAccessor nodeField : nodeClass.getChildrenFields()) { - Object arrayObject = nodeField.getObject(parent); - if (arrayObject != null) { - Object[] array = (Object[]) arrayObject; - for (int i = 0; i < array.length; i++) { - if (array[i] == oldChild) { - assert assertAssignable(nodeField, newChild); - if (adopt) { - parent.adoptHelper(newChild); + } else if (nodeClass.isChildrenField(nodeField)) { + Object arrayObject = nodeClass.getFieldObject(nodeField, parent); + if (arrayObject != null) { + Object[] array = (Object[]) arrayObject; + for (int i = 0; i < array.length; i++) { + if (array[i] == oldChild) { + if (adopt) { + parent.adoptHelper(newChild); + } + try { + array[i] = newChild; + } catch (ArrayStoreException e) { + throw replaceChildIllegalArgumentException(nodeField, array.getClass(), newChild); + } + return true; } - array[i] = newChild; - return true; } } + } else if (nodeClass.nodeFieldsOrderedByKind()) { + break; } } + return false; } - private static boolean assertAssignable(NodeFieldAccessor field, Object newValue) { - if (newValue == null) { - return true; - } - if (field.getKind() == NodeFieldKind.CHILD) { - if (field.getType().isAssignableFrom(newValue.getClass())) { - return true; - } else { - assert false : "Child class " + newValue.getClass().getName() + " is not assignable to field \"" + field.getName() + "\" of type " + field.getType().getName(); - return false; - } - } else if (field.getKind() == NodeFieldKind.CHILDREN) { - if (field.getType().getComponentType().isAssignableFrom(newValue.getClass())) { - return true; - } else { - assert false : "Child class " + newValue.getClass().getName() + " is not assignable to field \"" + field.getName() + "\" of type " + field.getType().getName(); - return false; - } - } - throw new IllegalArgumentException(); + private static IllegalArgumentException replaceChildIllegalArgumentException(Object nodeField, Class fieldType, Node newChild) { + return new IllegalArgumentException("Cannot set element of " + fieldType.getName() + " field " + nodeField + " to " + (newChild == null ? "null" : newChild.getClass().getName())); } /** @@ -274,23 +262,25 @@ private static boolean assertAssignable(NodeFieldAccessor field, Object newValue * @return the field (possibly an array) holding the child, {@code null} if not found. * @since 0.8 or earlier */ + @SuppressWarnings("deprecation") + @Deprecated public static NodeFieldAccessor findChildField(Node parent, Node child) { assert child != null; NodeClass parentNodeClass = parent.getNodeClass(); - for (NodeFieldAccessor field : parentNodeClass.getChildFields()) { - if (field.getObject(parent) == child) { - return field; - } - } - - for (NodeFieldAccessor field : parentNodeClass.getChildrenFields()) { - Object arrayObject = field.getObject(parent); - if (arrayObject != null) { - Object[] array = (Object[]) arrayObject; - for (int i = 0; i < array.length; i++) { - if (array[i] == child) { - return field; + for (NodeFieldAccessor field : parentNodeClass.getFields()) { + if (field.getKind() == NodeFieldAccessor.NodeFieldKind.CHILD) { + if (field.getObject(parent) == child) { + return field; + } + } else if (field.getKind() == NodeFieldAccessor.NodeFieldKind.CHILDREN) { + Object arrayObject = field.getObject(parent); + if (arrayObject != null) { + Object[] array = (Object[]) arrayObject; + for (int i = 0; i < array.length; i++) { + if (array[i] == child) { + return field; + } } } } @@ -304,17 +294,26 @@ public static NodeFieldAccessor findChildField(Node parent, Node child) { * @since 0.8 or earlier */ public static boolean isReplacementSafe(Node parent, Node oldChild, Node newChild) { - assert newChild != null; + Objects.requireNonNull(oldChild); if (parent != null) { - final NodeFieldAccessor field = findChildField(parent, oldChild); - if (field != null) { - switch (field.getKind()) { - case CHILD: - return field.getType().isAssignableFrom(newChild.getClass()); - case CHILDREN: - return field.getType().getComponentType().isAssignableFrom(newChild.getClass()); - default: - throw new IllegalStateException(); + NodeClass nodeClass = parent.getNodeClass(); + for (Object field : nodeClass.getNodeFields()) { + if (nodeClass.isChildField(field)) { + if (nodeClass.getFieldObject(field, parent) == oldChild) { + return nodeClass.getFieldType(field).isAssignableFrom(newChild.getClass()); + } + } else if (nodeClass.isChildrenField(field)) { + Object arrayObject = nodeClass.getFieldObject(field, parent); + if (arrayObject != null) { + Object[] array = (Object[]) arrayObject; + for (int i = 0; i < array.length; i++) { + if (array[i] == oldChild) { + return nodeClass.getFieldType(field).getComponentType().isAssignableFrom(newChild.getClass()); + } + } + } + } else if (nodeClass.nodeFieldsOrderedByKind()) { + break; } } } @@ -330,29 +329,31 @@ public static boolean isReplacementSafe(Node parent, Node oldChild, Node newChil public static boolean forEachChild(Node parent, NodeVisitor visitor) { CompilerAsserts.neverPartOfCompilation("do not iterate over Node children from compiled code"); Objects.requireNonNull(visitor); - NodeClass parentNodeClass = parent.getNodeClass(); + NodeClass nodeClass = parent.getNodeClass(); - for (NodeFieldAccessor field : parentNodeClass.getChildFields()) { - Object child = field.getObject(parent); - if (child != null) { - if (!visitor.visit((Node) child)) { - return false; + for (Object field : nodeClass.getNodeFields()) { + if (nodeClass.isChildField(field)) { + Object child = nodeClass.getFieldObject(field, parent); + if (child != null) { + if (!visitor.visit((Node) child)) { + return false; + } } - } - } - - for (NodeFieldAccessor field : parentNodeClass.getChildrenFields()) { - Object arrayObject = field.getObject(parent); - if (arrayObject != null) { - Object[] array = (Object[]) arrayObject; - for (int i = 0; i < array.length; i++) { - Object child = array[i]; - if (child != null) { - if (!visitor.visit((Node) child)) { - return false; + } else if (nodeClass.isChildrenField(field)) { + Object arrayObject = nodeClass.getFieldObject(field, parent); + if (arrayObject != null) { + Object[] array = (Object[]) arrayObject; + for (int i = 0; i < array.length; i++) { + Object child = array[i]; + if (child != null) { + if (!visitor.visit((Node) child)) { + return false; + } } } } + } else if (nodeClass.nodeFieldsOrderedByKind()) { + break; } } @@ -360,24 +361,26 @@ public static boolean forEachChild(Node parent, NodeVisitor visitor) { } static boolean forEachChildRecursive(Node parent, NodeVisitor visitor) { - NodeClass parentNodeClass = parent.getNodeClass(); - - for (NodeFieldAccessor field : parentNodeClass.getChildFields()) { - if (!visitChild((Node) field.getObject(parent), visitor)) { - return false; - } - } + NodeClass nodeClass = parent.getNodeClass(); - for (NodeFieldAccessor field : parentNodeClass.getChildrenFields()) { - Object arrayObject = field.getObject(parent); - if (arrayObject == null) { - continue; - } - Object[] array = (Object[]) arrayObject; - for (int i = 0; i < array.length; i++) { - if (!visitChild((Node) array[i], visitor)) { + for (Object field : nodeClass.getNodeFields()) { + if (nodeClass.isChildField(field)) { + if (!visitChild((Node) nodeClass.getFieldObject(field, parent), visitor)) { return false; } + } else if (nodeClass.isChildrenField(field)) { + Object arrayObject = nodeClass.getFieldObject(field, parent); + if (arrayObject == null) { + continue; + } + Object[] array = (Object[]) arrayObject; + for (int i = 0; i < array.length; i++) { + if (!visitChild((Node) array[i], visitor)) { + return false; + } + } + } else if (nodeClass.nodeFieldsOrderedByKind()) { + break; } } @@ -625,19 +628,20 @@ private static void printSourceAttributionTree(PrintWriter p, Node parent, Node } private static String getNodeFieldName(Node parent, Node node, String defaultName) { - NodeFieldAccessor[] fields = parent.getNodeClass().getFields(); - for (NodeFieldAccessor field : fields) { - Object value = field.loadValue(parent); - if (field.getKind() == NodeFieldKind.CHILD && value == node) { - return field.getName(); - } else if (field.getKind() == NodeFieldKind.CHILDREN) { + NodeClass nodeClass = parent.getNodeClass(); + for (Object field : nodeClass.getNodeFields()) { + if (nodeClass.isChildField(field) && nodeClass.getFieldObject(field, parent) == node) { + return nodeClass.getFieldName(field); + } else if (nodeClass.isChildrenField(field)) { int index = 0; - for (Object arrayNode : (Object[]) value) { + for (Object arrayNode : (Object[]) nodeClass.getFieldObject(field, parent)) { if (arrayNode == node) { - return field.getName() + "[" + index + "]"; + return nodeClass.getFieldName(field) + "[" + index + "]"; } index++; } + } else if (nodeClass.nodeFieldsOrderedByKind()) { + break; } } return defaultName; @@ -689,36 +693,37 @@ private static void printTree(PrintWriter p, Node node, int level) { p.print(nodeName(node)); - ArrayList childFields = new ArrayList<>(); + ArrayList childFields = new ArrayList<>(); String sep = ""; p.print("("); - for (NodeFieldAccessor field : NodeClass.get(node).getFields()) { - if (field.getKind() == NodeFieldKind.CHILD || field.getKind() == NodeFieldKind.CHILDREN) { + NodeClass nodeClass = NodeClass.get(node); + for (Object field : nodeClass.getNodeFields()) { + if (nodeClass.isChildField(field) || nodeClass.isChildrenField(field)) { childFields.add(field); - } else if (field.getKind() == NodeFieldKind.DATA) { + } else { p.print(sep); sep = ", "; - p.print(field.getName()); + p.print(nodeClass.getFieldName(field)); p.print(" = "); - p.print(field.loadValue(node)); + p.print(nodeClass.getFieldValue(field, node)); } } p.print(")"); if (childFields.size() != 0) { p.print(" {"); - for (NodeFieldAccessor field : childFields) { + for (Object field : nodeClass.getNodeFields()) { printNewLine(p, level); - p.print(field.getName()); + p.print(nodeClass.getFieldName(field)); - Object value = field.loadValue(node); + Object value = nodeClass.getFieldValue(field, node); if (value == null) { p.print(" = null "); - } else if (field.getKind() == NodeFieldKind.CHILD) { + } else if (nodeClass.isChildField(field)) { p.print(" = "); printTree(p, (Node) value, level + 1); - } else if (field.getKind() == NodeFieldKind.CHILDREN) { + } else if (nodeClass.isChildrenField(field)) { printChildren(p, level, value); } } diff --git a/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/controlflow/SLBlockNode.java b/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/controlflow/SLBlockNode.java index 4c94348b7a78..5a51e2f94b90 100644 --- a/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/controlflow/SLBlockNode.java +++ b/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/nodes/controlflow/SLBlockNode.java @@ -40,6 +40,10 @@ */ package com.oracle.truffle.sl.nodes.controlflow; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + import com.oracle.truffle.api.CompilerAsserts; import com.oracle.truffle.api.frame.VirtualFrame; import com.oracle.truffle.api.nodes.ExplodeLoop; @@ -82,4 +86,8 @@ public void executeVoid(VirtualFrame frame) { statement.executeVoid(frame); } } + + public List getStatements() { + return Collections.unmodifiableList(Arrays.asList(bodyNodes)); + } } diff --git a/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SLNodeFactory.java b/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SLNodeFactory.java index ca056d9c1c6b..b5c589fb84b7 100644 --- a/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SLNodeFactory.java +++ b/truffle/com.oracle.truffle.sl/src/com/oracle/truffle/sl/parser/SLNodeFactory.java @@ -48,7 +48,6 @@ import com.oracle.truffle.api.frame.FrameDescriptor; import com.oracle.truffle.api.frame.FrameSlot; -import com.oracle.truffle.api.nodes.Node; import com.oracle.truffle.api.source.Source; import com.oracle.truffle.api.source.SourceSection; import com.oracle.truffle.sl.nodes.SLExpressionNode; @@ -204,12 +203,12 @@ private static boolean isHaltInCondition(SLStatementNode statement) { return (statement instanceof SLIfNode) || (statement instanceof SLWhileNode); } - private void flattenBlocks(Iterable bodyNodes, List flattenedNodes) { - for (Node n : bodyNodes) { + private void flattenBlocks(Iterable bodyNodes, List flattenedNodes) { + for (SLStatementNode n : bodyNodes) { if (n instanceof SLBlockNode) { - flattenBlocks(n.getChildren(), flattenedNodes); + flattenBlocks(((SLBlockNode) n).getStatements(), flattenedNodes); } else { - flattenedNodes.add((SLStatementNode) n); + flattenedNodes.add(n); } } } diff --git a/truffle/com.oracle.truffle.tools.debug.shell/src/com/oracle/truffle/tools/debug/shell/server/InstrumentationUtils.java b/truffle/com.oracle.truffle.tools.debug.shell/src/com/oracle/truffle/tools/debug/shell/server/InstrumentationUtils.java index 06b09e7ea9a8..b27ba83825fc 100644 --- a/truffle/com.oracle.truffle.tools.debug.shell/src/com/oracle/truffle/tools/debug/shell/server/InstrumentationUtils.java +++ b/truffle/com.oracle.truffle.tools.debug.shell/src/com/oracle/truffle/tools/debug/shell/server/InstrumentationUtils.java @@ -31,13 +31,12 @@ import com.oracle.truffle.api.instrumentation.InstrumentableFactory.WrapperNode; import com.oracle.truffle.api.nodes.Node; import com.oracle.truffle.api.nodes.NodeClass; -import com.oracle.truffle.api.nodes.NodeFieldAccessor; -import com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind; import com.oracle.truffle.api.source.SourceSection; /** * A language-agnostic for printing out various pieces of a Truffle AST. */ +@SuppressWarnings("deprecation") final class InstrumentationUtils { private InstrumentationUtils() { @@ -131,24 +130,25 @@ protected void printTree(PrintWriter p, Node node, int maxDepth, Node markNode, p.print(displayNodeWithInstrumentation(node)); } if (level <= maxDepth) { - ArrayList childFields = new ArrayList<>(); - for (NodeFieldAccessor field : NodeClass.get(node).getFields()) { - if (field.getKind() == NodeFieldKind.CHILD || field.getKind() == NodeFieldKind.CHILDREN) { + ArrayList childFields = new ArrayList<>(); + for (com.oracle.truffle.api.nodes.NodeFieldAccessor field : NodeClass.get(node).getFields()) { + if (field.getKind() == com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind.CHILD || + field.getKind() == com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind.CHILDREN) { childFields.add(field); } } if (childFields.size() != 0) { p.print(" {"); - for (NodeFieldAccessor field : childFields) { + for (com.oracle.truffle.api.nodes.NodeFieldAccessor field : childFields) { Object value = field.loadValue(node); if (value == null) { printNewLine(p, level); p.print(field.getName()); p.print(" = null "); - } else if (field.getKind() == NodeFieldKind.CHILD) { + } else if (field.getKind() == com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind.CHILD) { printChild(p, maxDepth, markNode, level, field, value); - } else if (field.getKind() == NodeFieldKind.CHILDREN) { + } else if (field.getKind() == com.oracle.truffle.api.nodes.NodeFieldAccessor.NodeFieldKind.CHILDREN) { printChildren(p, maxDepth, markNode, level, field, value); } else { printNewLine(p, level); @@ -161,7 +161,7 @@ protected void printTree(PrintWriter p, Node node, int maxDepth, Node markNode, } } - protected void printChildren(PrintWriter p, int maxDepth, Node markNode, int level, NodeFieldAccessor field, Object value) { + protected void printChildren(PrintWriter p, int maxDepth, Node markNode, int level, com.oracle.truffle.api.nodes.NodeFieldAccessor field, Object value) { printNewLine(p, level); p.print(field.getName()); Node[] children = (Node[]) value; @@ -175,7 +175,7 @@ protected void printChildren(PrintWriter p, int maxDepth, Node markNode, int lev p.print("]"); } - protected void printChild(PrintWriter p, int maxDepth, Node markNode, int level, NodeFieldAccessor field, Object value) { + protected void printChild(PrintWriter p, int maxDepth, Node markNode, int level, com.oracle.truffle.api.nodes.NodeFieldAccessor field, Object value) { final Node valueNode = (Node) value; printNewLine(p, level, valueNode == markNode); p.print(field.getName());