Mercurial > hg > graal-jvmci-8
comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @ 11809:c7769440afd8
improve NodeUtil.cloneNode.
author | Andreas Woess <andreas.woess@jku.at> |
---|---|
date | Wed, 25 Sep 2013 17:34:01 +0200 |
parents | e8dfad9a424f |
children | 099af41815ea |
comparison
equal
deleted
inserted
replaced
11808:0c4d7b468dd7 | 11809:c7769440afd8 |
---|---|
290 } | 290 } |
291 } | 291 } |
292 | 292 |
293 @SuppressWarnings("unchecked") | 293 @SuppressWarnings("unchecked") |
294 public static <T extends Node> T cloneNode(T orig) { | 294 public static <T extends Node> T cloneNode(T orig) { |
295 Class<? extends Node> clazz = orig.getClass(); | 295 final Node clone = orig.copy(); |
296 NodeClass nodeClass = NodeClass.get(clazz); | 296 NodeClass nodeClass = NodeClass.get(clone.getClass()); |
297 Node clone = orig.copy(); | |
298 if (clone == null) { | |
299 return null; | |
300 } | |
301 | 297 |
302 unsafe.putObject(clone, nodeClass.parentOffset, null); | 298 unsafe.putObject(clone, nodeClass.parentOffset, null); |
303 | 299 |
304 for (long fieldOffset : nodeClass.childOffsets) { | 300 for (long fieldOffset : nodeClass.childOffsets) { |
305 Node child = (Node) unsafe.getObject(orig, fieldOffset); | 301 Node child = (Node) unsafe.getObject(orig, fieldOffset); |
306 if (child != null) { | 302 if (child != null) { |
307 Node clonedChild = cloneNode(child); | 303 Node clonedChild = cloneNode(child); |
308 if (clonedChild == null) { | |
309 return null; | |
310 } | |
311 | |
312 unsafe.putObject(clonedChild, nodeClass.parentOffset, clone); | 304 unsafe.putObject(clonedChild, nodeClass.parentOffset, clone); |
313 unsafe.putObject(clone, fieldOffset, clonedChild); | 305 unsafe.putObject(clone, fieldOffset, clonedChild); |
314 } | 306 } |
315 } | 307 } |
316 for (long fieldOffset : nodeClass.childrenOffsets) { | 308 for (long fieldOffset : nodeClass.childrenOffsets) { |
317 Node[] children = (Node[]) unsafe.getObject(orig, fieldOffset); | 309 Node[] children = (Node[]) unsafe.getObject(orig, fieldOffset); |
318 if (children != null) { | 310 if (children != null) { |
319 Node[] clonedChildren = children.clone(); | 311 Node[] clonedChildren = (Node[]) Array.newInstance(children.getClass().getComponentType(), children.length); |
320 Arrays.fill(clonedChildren, null); | |
321 for (int i = 0; i < children.length; i++) { | 312 for (int i = 0; i < children.length; i++) { |
322 Node clonedChild = cloneNode(children[i]); | 313 if (children[i] != null) { |
323 if (clonedChild == null) { | 314 Node clonedChild = cloneNode(children[i]); |
324 return null; | 315 clonedChildren[i] = clonedChild; |
316 unsafe.putObject(clonedChild, nodeClass.parentOffset, clone); | |
325 } | 317 } |
326 | |
327 clonedChildren[i] = clonedChild; | |
328 unsafe.putObject(clonedChild, nodeClass.parentOffset, clone); | |
329 } | 318 } |
330 unsafe.putObject(clone, fieldOffset, clonedChildren); | 319 unsafe.putObject(clone, fieldOffset, clonedChildren); |
331 } | 320 } |
332 } | 321 } |
333 return (T) clone; | 322 return (T) clone; |