comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java @ 12495:b7c8b843dc7b

Truffle: add sanity check.
author Andreas Woess <andreas.woess@jku.at>
date Sun, 20 Oct 2013 03:50:51 +0200
parents 57b8a41c0e18
children ffd4b6b4ae68
comparison
equal deleted inserted replaced
12494:57b8a41c0e18 12495:b7c8b843dc7b
169 * @param newNode the new node that is the replacement 169 * @param newNode the new node that is the replacement
170 * @param reason a description of the reason for the replacement 170 * @param reason a description of the reason for the replacement
171 * @return the new node 171 * @return the new node
172 */ 172 */
173 public final <T extends Node> T replace(T newNode, String reason) { 173 public final <T extends Node> T replace(T newNode, String reason) {
174 CompilerDirectives.transferToInterpreter();
174 if (this.getParent() == null) { 175 if (this.getParent() == null) {
175 throw new IllegalStateException("This node cannot be replaced, because it does not yet have a parent."); 176 throw new IllegalStateException("This node cannot be replaced, because it does not yet have a parent.");
176 } 177 }
177 if (sourceSection != null && newNode.getSourceSection() == null) { 178 if (sourceSection != null && newNode.getSourceSection() == null) {
178 // Pass on the source section to the new node. 179 // Pass on the source section to the new node.
194 private void fixupTree() { 195 private void fixupTree() {
195 Node rootNode = NodeUtil.findParent(this, RootNode.class); 196 Node rootNode = NodeUtil.findParent(this, RootNode.class);
196 if (rootNode == null) { 197 if (rootNode == null) {
197 throw new UnsupportedOperationException("Tree does not have a root node."); 198 throw new UnsupportedOperationException("Tree does not have a root node.");
198 } 199 }
199 rootNode.fixupChildren(); 200 int fixCount = rootNode.fixupChildren();
200 } 201 assert fixCount != 0 : "sanity check failed: missing @Child[ren] or adoptChild?";
201 202 // if nothing had to be fixed, rewrite failed due to node not being a proper child.
202 private void fixupChildren() { 203 }
204
205 private int fixupChildren() {
206 int fixCount = 0;
203 for (Node child : getChildren()) { 207 for (Node child : getChildren()) {
204 if (child != null) { 208 if (child != null) {
205 if (child.parent != this) { 209 if (child.parent != this) {
206 child.parent = this; 210 child.parent = this;
211 fixCount++;
207 } 212 }
208 child.fixupChildren(); 213 fixCount += child.fixupChildren();
209 } 214 }
210 } 215 }
216 return fixCount;
211 } 217 }
212 218
213 /** 219 /**
214 * Replaces this node with another node. If there is a source section (see 220 * Replaces this node with another node. If there is a source section (see
215 * {@link #getSourceSection()}) associated with this node, it is transferred to the new node. 221 * {@link #getSourceSection()}) associated with this node, it is transferred to the new node.