comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/Node.java @ 15346:1cd02b4d90d1

onAdopt callback for ASTs
author Michael Haupt <michael.haupt@oracle.com>
date Wed, 23 Apr 2014 15:23:18 +0200
parents 64dcb92ee75a
children 2cea065e419d
comparison
equal deleted inserted replaced
15311:820c6d353358 15346:1cd02b4d90d1
189 private void adoptHelper(final Node newChild) { 189 private void adoptHelper(final Node newChild) {
190 assert newChild != null; 190 assert newChild != null;
191 if (newChild == this) { 191 if (newChild == this) {
192 throw new IllegalStateException("The parent of a node can never be the node itself."); 192 throw new IllegalStateException("The parent of a node can never be the node itself.");
193 } 193 }
194 boolean isInserted = newChild.parent == null;
194 newChild.parent = this; 195 newChild.parent = this;
195 newChild.adoptHelper(); 196 newChild.adoptHelper();
197 if (isInserted) {
198 newChild.onAdopt();
199 }
196 } 200 }
197 201
198 private void adoptHelper() { 202 private void adoptHelper() {
199 Iterable<Node> children = this.getChildren(); 203 Iterable<Node> children = this.getChildren();
200 for (Node child : children) { 204 for (Node child : children) {
207 private void adoptUnadoptedHelper(final Node newChild) { 211 private void adoptUnadoptedHelper(final Node newChild) {
208 assert newChild != null; 212 assert newChild != null;
209 if (newChild == this) { 213 if (newChild == this) {
210 throw new IllegalStateException("The parent of a node can never be the node itself."); 214 throw new IllegalStateException("The parent of a node can never be the node itself.");
211 } 215 }
216 boolean isInserted = newChild.parent == null;
212 newChild.parent = this; 217 newChild.parent = this;
213 newChild.adoptUnadoptedHelper(); 218 newChild.adoptUnadoptedHelper();
219 if (isInserted) {
220 newChild.onAdopt();
221 }
214 } 222 }
215 223
216 private void adoptUnadoptedHelper() { 224 private void adoptUnadoptedHelper() {
217 Iterable<Node> children = this.getChildren(); 225 Iterable<Node> children = this.getChildren();
218 for (Node child : children) { 226 for (Node child : children) {
352 return; 360 return;
353 } 361 }
354 362
355 PrintStream out = System.out; 363 PrintStream out = System.out;
356 out.printf("[truffle] rewrite %-50s |From %-40s |To %-40s |Reason %s.%n", this.toString(), formatNodeInfo(this), formatNodeInfo(newNode), reason); 364 out.printf("[truffle] rewrite %-50s |From %-40s |To %-40s |Reason %s.%n", this.toString(), formatNodeInfo(this), formatNodeInfo(newNode), reason);
365 }
366
367 /**
368 * Subclasses of {@link Node} can implement this method to execute extra functionality when a
369 * node is effectively inserted into the AST. The {@code onAdopt} callback is called after the
370 * node has been effectively inserted, and it is guaranteed to be called only once for any given
371 * node.
372 */
373 protected void onAdopt() {
374 // empty default
357 } 375 }
358 376
359 private static String formatNodeInfo(Node node) { 377 private static String formatNodeInfo(Node node) {
360 String cost = "?"; 378 String cost = "?";
361 switch (node.getCost()) { 379 switch (node.getCost()) {