adding a test case
[IRC.git] / Robust / src / Analysis / OwnershipAnalysis / HeapRegionNode.java
1 package Analysis.OwnershipAnalysis;
2
3 import IR.*;
4 import IR.Flat.*;
5 import java.util.*;
6
7 public class HeapRegionNode extends OwnershipNode {
8
9   protected Integer id;
10
11   protected boolean isSingleObject;
12   protected boolean isFlagged;
13   protected boolean isParameter;
14   protected boolean isNewSummary;
15
16   protected HashSet<ReferenceEdge> referencers;
17
18   protected TypeDescriptor type;
19
20   protected AllocationSite allocSite;
21
22   protected ReachabilitySet alpha;
23   protected ReachabilitySet alphaNew;
24
25   protected String description;
26
27   protected String globalIdentifier;
28
29
30
31   public HeapRegionNode(Integer id,
32                         boolean isSingleObject,
33                         boolean isFlagged,
34                         boolean isParameter,
35                         boolean isNewSummary,
36                         TypeDescriptor type,
37                         AllocationSite allocSite,
38                         ReachabilitySet alpha,
39                         String description,
40                         String globalIdentifier) {
41     this.id = id;
42     this.isSingleObject = isSingleObject;
43     this.isFlagged      = isFlagged;
44     this.isParameter    = isParameter;
45     this.isNewSummary   = isNewSummary;
46     this.type           = type;
47     this.allocSite      = allocSite;
48     this.alpha          = alpha;
49     this.description    = description;
50     this.globalIdentifier = globalIdentifier;
51
52     referencers = new HashSet<ReferenceEdge>();
53     alphaNew    = new ReachabilitySet().makeCanonical();
54   }
55
56   public HeapRegionNode copy() {
57     return new HeapRegionNode(id,
58                               isSingleObject,
59                               isFlagged,
60                               isParameter,
61                               isNewSummary,
62                               type,
63                               allocSite,
64                               alpha,
65                               description,
66                               globalIdentifier);
67   }
68
69
70   public Integer getID() {
71     return id;
72   }
73
74
75   public boolean equalsIncludingAlpha(HeapRegionNode hrn) {
76     return equals(hrn) && alpha.equals(hrn.alpha);
77   }
78
79
80   public boolean equals(Object o) {
81     if( o == null ) {
82       return false;
83     }
84
85     if( !( o instanceof HeapRegionNode) ) {
86       return false;
87     }
88
89     HeapRegionNode hrn = (HeapRegionNode) o;
90
91     if( !id.equals(hrn.getID() ) ) {
92       return false;
93     }
94
95     assert isSingleObject == hrn.isSingleObject();
96     assert isFlagged      == hrn.isFlagged();
97     assert isParameter    == hrn.isParameter();
98     assert isNewSummary   == hrn.isNewSummary();
99     assert description.equals(hrn.getDescription() );
100
101     return true;
102   }
103
104   public int hashCode() {
105     return id.intValue()*17;
106   }
107
108
109   public boolean isSingleObject() {
110     return isSingleObject;
111   }
112
113   public boolean isFlagged() {
114     return isFlagged;
115   }
116
117   public boolean isParameter() {
118     return isParameter;
119   }
120
121   public boolean isNewSummary() {
122     return isNewSummary;
123   }
124
125
126
127   public Iterator<ReferenceEdge> iteratorToReferencers() {
128     return referencers.iterator();
129   }
130
131   public Iterator<ReferenceEdge> iteratorToReferencersClone() {
132     HashSet<ReferenceEdge> clone = (HashSet<ReferenceEdge>)referencers.clone();
133     return clone.iterator();
134   }
135
136   public int getNumReferencers() {
137     return referencers.size();
138   }
139
140
141   public void addReferencer(ReferenceEdge edge) {
142     assert edge != null;
143
144     referencers.add(edge);
145   }
146
147   public void removeReferencer(ReferenceEdge edge) {
148     assert edge != null;
149     assert referencers.contains(edge);
150
151     referencers.remove(edge);
152   }
153
154   public ReferenceEdge getReferenceFrom(OwnershipNode on,
155                                         TypeDescriptor type,
156                                         String field) {
157     assert on != null;
158
159     Iterator<ReferenceEdge> itrEdge = referencers.iterator();
160     while( itrEdge.hasNext() ) {
161       ReferenceEdge edge = itrEdge.next();
162       if( edge.getSrc().equals(on) &&
163           edge.typeEquals(type) &&
164           edge.fieldEquals(field) ) {
165         return edge;
166       }
167     }
168
169     return null;
170   }
171
172
173   public TypeDescriptor getType() {
174     return type;
175   }
176
177   public AllocationSite getAllocationSite() {
178     return allocSite;
179   }
180
181
182   public void setAlpha(ReachabilitySet alpha) {
183     this.alpha = alpha;
184   }
185
186   public ReachabilitySet getAlpha() {
187     return alpha;
188   }
189
190   public ReachabilitySet getAlphaNew() {
191     return alphaNew;
192   }
193
194   public void setAlphaNew(ReachabilitySet alpha) {
195     this.alphaNew = alpha;
196   }
197
198   public void applyAlphaNew() {
199     assert alphaNew != null;
200     alpha = alphaNew;
201     alphaNew = new ReachabilitySet().makeCanonical();
202   }
203
204
205   public String getIDString() {
206     String s;
207
208     if( id < 0 ) {
209       s = "minus" + new Integer(-id).toString();
210     } else {
211       s = id.toString();
212     }
213
214     return s;
215   }
216
217   public String getAlphaString(boolean hideSubsetReachability) {
218     return alpha.toStringEscapeNewline(hideSubsetReachability);
219   }
220
221   public String toString() {
222     return "HRN"+getIDString();
223   }
224
225   // WHY WHY WHY WHY WHY WHY?!
226   public String getDescription() {
227     return new String(description);
228     //return new String( description+" ID "+getIDString() );
229   }
230
231   public String getGloballyUniqueIdentifier() {
232     return globalIdentifier;
233   }
234
235   public long getGloballyUniqueIntegerIdentifier() {
236     String fristpart = globalIdentifier;
237     fristpart = fristpart.replaceAll("FN", "1");
238     fristpart = fristpart.replaceAll("FM", "2");
239     int idx = fristpart.indexOf(".");
240     String endpart = fristpart.substring(idx + 1);
241     endpart = endpart.replaceAll("S", "1");
242     endpart = endpart.replaceAll("P", "2");
243     endpart = endpart.replaceAll("A", "3");
244     endpart = endpart.replace(".", "");
245     String modified = fristpart.substring(0, idx) + endpart;
246     return Long.parseLong(modified);
247   }
248 }