changeset 18332:65a160d9d259

Truffle: add NodeInterface and require that all child fields be of this type
author Andreas Woess <andreas.woess@jku.at>
date Mon, 10 Nov 2014 19:29:08 +0100
parents 803b0b06e408
children db6b7d3dfa1d
files CHANGELOG.md graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/InterfaceChildFieldTest.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeInterface.java graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java
diffstat 5 files changed, 63 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/CHANGELOG.md	Mon Nov 10 19:08:52 2014 +0100
+++ b/CHANGELOG.md	Mon Nov 10 19:29:08 2014 +0100
@@ -36,6 +36,7 @@
 * Removed `FrameTypeConversion` interface and changed the corresponding `FrameDescriptor` constructor to have a default value parameter instead.
 * Removed `CompilerDirectives.unsafeFrameCast` (equivalent to a `(MaterializedFrame)` cast).
 * Added `TruffleRuntime#getCapability` API method.
+* Added `NodeInterface` and allowed child field to be declared with interfaces that extend it.
 
 ## Version 0.4
 19-Aug-2014, [Repository Revision](http://hg.openjdk.java.net/graal/graal/shortlog/graal-0.4)
--- a/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/InterfaceChildFieldTest.java	Mon Nov 10 19:08:52 2014 +0100
+++ b/graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/InterfaceChildFieldTest.java	Mon Nov 10 19:29:08 2014 +0100
@@ -91,7 +91,7 @@
         }
     }
 
-    interface TestChildInterface {
+    interface TestChildInterface extends NodeInterface {
         int executeIntf();
     }
 
--- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java	Mon Nov 10 19:08:52 2014 +0100
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java	Mon Nov 10 19:29:08 2014 +0100
@@ -36,7 +36,7 @@
 /**
  * Abstract base class for all Truffle nodes.
  */
-public abstract class Node implements Cloneable {
+public abstract class Node implements NodeInterface, Cloneable {
 
     @CompilationFinal private Node parent;
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeInterface.java	Mon Nov 10 19:29:08 2014 +0100
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package com.oracle.truffle.api.nodes;
+
+import com.oracle.truffle.api.source.*;
+
+/**
+ * Common base interface for all Truffle nodes.
+ *
+ * @see Node
+ */
+public interface NodeInterface {
+    SourceSection getSourceSection();
+
+    SourceSection getEncapsulatingSourceSection();
+
+    Node copy();
+
+    <T extends Node> T replace(T newNode, CharSequence reason);
+
+    <T extends Node> T replace(T newNode);
+
+    Node getParent();
+
+    void accept(NodeVisitor nodeVisitor);
+
+    Iterable<Node> getChildren();
+
+    RootNode getRootNode();
+}
--- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java	Mon Nov 10 19:08:52 2014 +0100
+++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java	Mon Nov 10 19:29:08 2014 +0100
@@ -224,9 +224,13 @@
             this.clazz = clazz;
         }
 
+        private static boolean isNodeType(Class<?> clazz) {
+            return Node.class.isAssignableFrom(clazz) || (clazz.isInterface() && NodeInterface.class.isAssignableFrom(clazz));
+        }
+
         private static void checkChildField(Field field) {
-            if (!(Node.class.isAssignableFrom(field.getType()) || field.getType().isInterface())) {
-                throw new AssertionError("@Child field type must be a subclass of Node or an interface (" + field + ")");
+            if (!isNodeType(field.getType())) {
+                throw new AssertionError("@Child field type must be a subclass of Node or an interface extending NodeInterface (" + field + ")");
             }
             if (Modifier.isFinal(field.getModifiers())) {
                 throw new AssertionError("@Child field must not be final (" + field + ")");
@@ -234,8 +238,8 @@
         }
 
         private static void checkChildrenField(Field field) {
-            if (!(field.getType().isArray() && (Node.class.isAssignableFrom(field.getType().getComponentType()) || field.getType().getComponentType().isInterface()))) {
-                throw new AssertionError("@Children field type must be an array of a subclass of Node or an interface (" + field + ")");
+            if (!(field.getType().isArray() && isNodeType(field.getType().getComponentType()))) {
+                throw new AssertionError("@Children field type must be an array of a subclass of Node or an interface extending NodeInterface (" + field + ")");
             }
             if (!Modifier.isFinal(field.getModifiers())) {
                 throw new AssertionError("@Children field must be final (" + field + ")");