71db77b17c8892d6726f33fcfa9a54d7dae66072
[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
28
29   public HeapRegionNode(Integer id,
30                         boolean isSingleObject,
31                         boolean isFlagged,
32                         boolean isParameter,
33                         boolean isNewSummary,
34                         TypeDescriptor type,
35                         AllocationSite allocSite,
36                         ReachabilitySet alpha,
37                         String description) {
38     this.id = id;
39     this.isSingleObject = isSingleObject;
40     this.isFlagged      = isFlagged;
41     this.isParameter    = isParameter;
42     this.isNewSummary   = isNewSummary;
43     this.type           = type;
44     this.allocSite      = allocSite;
45     this.alpha          = alpha;
46     this.description    = description;
47
48     referencers = new HashSet<ReferenceEdge>();
49     alphaNew    = new ReachabilitySet().makeCanonical();
50   }
51
52   public HeapRegionNode copy() {
53     return new HeapRegionNode(id,
54                               isSingleObject,
55                               isFlagged,
56                               isParameter,
57                               isNewSummary,
58                               type,
59                               allocSite,
60                               alpha,
61                               description);
62   }
63
64
65   public Integer getID() {
66     return id;
67   }
68
69
70   public boolean equalsIncludingAlpha(HeapRegionNode hrn) {
71     return equals(hrn) && alpha.equals(hrn.alpha);
72   }
73
74
75   public boolean equals(Object o) {
76     if( o == null ) {
77       return false;
78     }
79
80     if( !( o instanceof HeapRegionNode) ) {
81       return false;
82     }
83
84     HeapRegionNode hrn = (HeapRegionNode) o;
85
86     if( !id.equals(hrn.getID() ) ) {
87       return false;
88     }
89
90     assert isSingleObject == hrn.isSingleObject();
91     assert isFlagged      == hrn.isFlagged();
92     assert isParameter    == hrn.isParameter();
93     assert isNewSummary   == hrn.isNewSummary();
94     assert description.equals(hrn.getDescription() );
95
96     return true;
97   }
98
99   public int hashCode() {
100     return id.intValue()*17;
101   }
102
103
104   public boolean isSingleObject() {
105     return isSingleObject;
106   }
107
108   public boolean isFlagged() {
109     return isFlagged;
110   }
111
112   public boolean isParameter() {
113     return isParameter;
114   }
115
116   public boolean isNewSummary() {
117     return isNewSummary;
118   }
119
120
121
122   public Iterator<ReferenceEdge> iteratorToReferencers() {
123     return referencers.iterator();
124   }
125
126   public Iterator<ReferenceEdge> iteratorToReferencersClone() {
127     HashSet<ReferenceEdge> clone = (HashSet<ReferenceEdge>)referencers.clone();
128     return clone.iterator();
129   }
130
131   public int getNumReferencers() {
132     return referencers.size();
133   }
134
135
136   public void addReferencer(ReferenceEdge edge) {
137     assert edge != null;
138
139     referencers.add(edge);
140   }
141
142   public void removeReferencer(ReferenceEdge edge) {
143     assert edge != null;
144     assert referencers.contains(edge);
145
146     referencers.remove(edge);
147   }
148
149   public ReferenceEdge getReferenceFrom(OwnershipNode on,
150                                         TypeDescriptor type,
151                                         String field) {
152     assert on != null;
153
154     Iterator<ReferenceEdge> itrEdge = referencers.iterator();
155     while( itrEdge.hasNext() ) {
156       ReferenceEdge edge = itrEdge.next();
157       if( edge.getSrc().equals(on) &&
158           edge.typeEquals(type) &&
159           edge.fieldEquals(field) ) {
160         return edge;
161       }
162     }
163
164     return null;
165   }
166
167
168   public TypeDescriptor getType() {
169     return type;
170   }  
171
172   public AllocationSite getAllocationSite() {
173     return allocSite;
174   }
175
176
177   public void setAlpha(ReachabilitySet alpha) {
178     this.alpha = alpha;
179   }
180
181   public ReachabilitySet getAlpha() {
182     return alpha;
183   }
184
185   public ReachabilitySet getAlphaNew() {
186     return alphaNew;
187   }
188
189   public void setAlphaNew(ReachabilitySet alpha) {
190     this.alphaNew = alpha;
191   }
192
193   public void applyAlphaNew() {
194     assert alphaNew != null;
195     alpha = alphaNew;
196     alphaNew = new ReachabilitySet().makeCanonical();
197   }
198
199
200   public String getIDString() {
201     String s;
202
203     if( id < 0 ) {
204       s = "minus" + new Integer(-id).toString();
205     } else {
206       s = id.toString();
207     }
208
209     return s;
210   }
211
212   public String getAlphaString( boolean hideSubsetReachability ) {
213     return alpha.toStringEscapeNewline(hideSubsetReachability);
214   }
215
216   public String toString() {
217     return "HRN"+getIDString();
218   }
219
220   // WHY WHY WHY WHY WHY WHY?!
221   public String getDescription() {
222     return new String(description);
223     //return new String( description+" ID "+getIDString() );
224   }
225 }