Mercurial > hg > truffle
comparison graal/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/NodeUtil.java @ 19141:67d9e635102f
Truffle/Instrumentation: refine checks for safe node replacement
author | Michael Van De Vanter <michael.van.de.vanter@oracle.com> |
---|---|
date | Tue, 03 Feb 2015 11:48:25 -0800 |
parents | f5b83e7b2b4c |
children | 73811d1b4cd0 |
comparison
equal
deleted
inserted
replaced
19103:ccabd82be35c | 19141:67d9e635102f |
---|---|
674 } | 674 } |
675 return null; | 675 return null; |
676 } | 676 } |
677 | 677 |
678 /** | 678 /** |
679 * Determines whether a proposed child replacement would be type safe. | 679 * Determines whether a proposed child replacement would be safe: structurally and type. |
680 * | |
681 * @param parent non-null node | |
682 * @param oldChild non-null existing child of parent | |
683 * @param newChild non-null proposed replacement for existing child | |
684 */ | 680 */ |
685 public static boolean isReplacementSafe(Node parent, Node oldChild, Node newChild) { | 681 public static boolean isReplacementSafe(Node parent, Node oldChild, Node newChild) { |
686 assert newChild != null; | 682 assert newChild != null; |
687 final NodeField field = findChildField(parent, oldChild); | 683 if (parent != null) { |
688 if (field == null) { | 684 final NodeField field = findChildField(parent, oldChild); |
689 throw new IllegalArgumentException(); | 685 if (field != null) { |
690 } | 686 switch (field.getKind()) { |
691 switch (field.getKind()) { | 687 case CHILD: |
692 case CHILD: | 688 return field.getType().isAssignableFrom(newChild.getClass()); |
693 return field.getType().isAssignableFrom(newChild.getClass()); | 689 case CHILDREN: |
694 case CHILDREN: | 690 return field.getType().getComponentType().isAssignableFrom(newChild.getClass()); |
695 return field.getType().getComponentType().isAssignableFrom(newChild.getClass()); | 691 default: |
696 default: | 692 throw new IllegalStateException(); |
697 throw new IllegalArgumentException(); | 693 } |
698 } | 694 } |
695 } | |
696 return false; | |
699 } | 697 } |
700 | 698 |
701 /** Returns all declared fields in the class hierarchy. */ | 699 /** Returns all declared fields in the class hierarchy. */ |
702 private static Field[] getAllFields(Class<? extends Object> clazz) { | 700 private static Field[] getAllFields(Class<? extends Object> clazz) { |
703 Field[] declaredFields = clazz.getDeclaredFields(); | 701 Field[] declaredFields = clazz.getDeclaredFields(); |