1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package org.graalvm.compiler.graph; 24 25 import org.graalvm.compiler.nodeinfo.InputType; 26 27 /** 28 * Describes an edge slot for a {@link NodeClass}. 29 */ 30 public final class Position { 31 32 /** 33 * The edges in which this position lies. 34 */ 35 private final Edges edges; 36 37 /** 38 * Index of the {@link Node} or {@link NodeList} field denoted by this position. 39 */ 40 private final int index; 41 42 /** 43 * Index within a {@link NodeList} if {@link #index} denotes a {@link NodeList} field otherwise 44 * {@link Node#NOT_ITERABLE}. 45 */ 46 private final int subIndex; 47 48 public Position(Edges edges, int index, int subIndex) { 49 this.edges = edges; 50 this.index = index; 51 this.subIndex = subIndex; 52 } 53 54 public Node get(Node node) { 55 if (index < edges.getDirectCount()) { 56 return Edges.getNode(node, edges.getOffsets(), index); 57 } else { 58 return Edges.getNodeList(node, edges.getOffsets(), index).get(subIndex); 59 } 60 } 61 62 public InputType getInputType() { 63 return ((InputEdges) edges).getInputType(index); 64 } 65 66 public String getName() { 67 return edges.getName(index); 68 } 69 70 public boolean isInputOptional() { 71 return ((InputEdges) edges).isOptional(index); 72 } 73 74 public void set(Node node, Node value) { 75 if (index < edges.getDirectCount()) { 76 edges.setNode(node, index, value); 77 } else { 78 Edges.getNodeList(node, edges.getOffsets(), index).set(subIndex, value); 79 } 80 } 81 82 public void initialize(Node node, Node value) { 83 if (index < edges.getDirectCount()) { 84 edges.initializeNode(node, index, value); 85 } else { 86 Edges.getNodeList(node, edges.getOffsets(), index).initialize(subIndex, value); 87 } 88 } 89 90 @Override 91 public String toString() { 92 String res = edges.getType(index).getSimpleName() + ":" + edges.getName(index); 93 if (subIndex != Node.NOT_ITERABLE) { 94 res += "[" + subIndex + "]"; 95 } 96 return res; 97 } 98 99 @Override 100 public int hashCode() { 101 final int prime = 31; 102 int result = 1; 103 result = prime * result + index; 104 result = prime * result + edges.hashCode(); 105 result = prime * result + subIndex; 106 return result; 107 } 108 109 @Override 110 public boolean equals(Object obj) { 111 if (this == obj) { 112 return true; 113 } 114 if (obj == null) { 115 return false; 116 } 117 if (getClass() != obj.getClass()) { 118 return false; 119 } 120 Position other = (Position) obj; 121 if (index != other.index) { 122 return false; 123 } 124 if (edges != other.edges) { 125 return false; 126 } 127 if (subIndex != other.subIndex) { 128 return false; 129 } 130 return true; 131 } 132 133 /** 134 * Gets the index within a {@link NodeList} if {@link #getIndex()} denotes a {@link NodeList} 135 * field otherwise {@link Node#NOT_ITERABLE}. 136 */ 137 public int getSubIndex() { 138 return subIndex; 139 } 140 141 /** 142 * Gets the index of the {@link Node} or {@link NodeList} field denoted by this position. 143 */ 144 public int getIndex() { 145 return index; 146 } 147 }