lots of untested code, but compiles, for attaching preds to reach states
[IRC.git] / Robust / src / Analysis / Disjoint / HeapRegionNode.java
1 package Analysis.Disjoint;
2
3 import IR.*;
4 import IR.Flat.*;
5 import java.util.*;
6
7 public class HeapRegionNode extends RefSrcNode {
8
9   protected Integer id;
10
11   protected boolean isSingleObject;
12   protected boolean isFlagged;
13   protected boolean isNewSummary;
14
15   // special nodes that represent heap parts
16   // outside of the current method context
17   protected boolean isOutOfContext;
18
19   protected HashSet<RefEdge> referencers;
20
21   protected TypeDescriptor type;
22
23   protected AllocSite allocSite;
24
25   // some reachability states are inherent
26   // to a node by its definition
27   protected ReachSet inherent;  
28
29   // use alpha for the current reach states
30   // and alphaNew during iterative calculations
31   // to update alpha
32   protected ReachSet alpha;
33   protected ReachSet alphaNew;
34
35   protected String description;
36
37   // existence predicates must be true in a caller
38   // context for this node to transfer from this
39   // callee to that context
40   protected ExistPredSet preds;
41
42
43   public HeapRegionNode( Integer        id,
44                          boolean        isSingleObject,
45                          boolean        isFlagged,
46                          boolean        isNewSummary,
47                          boolean        isOutOfContext,
48                          TypeDescriptor type,
49                          AllocSite      allocSite,
50                          ReachSet       inherent,
51                          ReachSet       alpha,
52                          ExistPredSet   preds,
53                          String         description
54                          ) {
55
56     this.id             = id;
57     this.isSingleObject = isSingleObject;
58     this.isFlagged      = isFlagged;
59     this.isNewSummary   = isNewSummary;
60     this.isOutOfContext = isOutOfContext;
61     this.type           = type;
62     this.allocSite      = allocSite;
63     this.inherent       = inherent;
64     this.alpha          = alpha;
65     this.preds          = preds;
66     this.description    = description;
67
68     referencers = new HashSet<RefEdge>();
69     alphaNew    = ReachSet.factory();
70   }
71
72   public HeapRegionNode copy() {
73     return new HeapRegionNode( id,
74                                isSingleObject,
75                                isFlagged,
76                                isNewSummary,
77                                isOutOfContext,
78                                type,
79                                allocSite,
80                                inherent,
81                                alpha,
82                                preds,
83                                description );
84   }
85
86
87   public Integer getID() {
88     return id;
89   }
90
91
92   // alpha and preds contribute towards reaching the
93   // fixed point, so use this method to determine if
94   // a node is "equal" to some previous visit, basically
95   public boolean equalsIncludingAlphaAndPreds( HeapRegionNode hrn ) {
96
97     return equals( hrn ) && 
98       alpha.equals( hrn.alpha ) && 
99       preds.equals( hrn.preds );
100   }
101
102
103   public boolean equals( Object o ) {
104     if( o == null ) {
105       return false;
106     }
107
108     if( !(o instanceof HeapRegionNode) ) {
109       return false;
110     }
111
112     HeapRegionNode hrn = (HeapRegionNode) o;
113
114     if( !id.equals( hrn.getID() ) ) {
115       return false;
116     }
117
118     assert isSingleObject == hrn.isSingleObject();
119     assert isFlagged      == hrn.isFlagged();
120     assert isNewSummary   == hrn.isNewSummary();
121     assert isOutOfContext == hrn.isOutOfContext();
122     assert description.equals( hrn.getDescription() );
123
124     return true;
125   }
126
127   public int hashCode() {
128     return id.intValue()*17;
129   }
130
131
132   public boolean isSingleObject() {
133     return isSingleObject;
134   }
135
136   public boolean isFlagged() {
137     return isFlagged;
138   }
139
140   public boolean isNewSummary() {
141     return isNewSummary;
142   }
143
144   public boolean isOutOfContext() {
145     return isOutOfContext;
146   }
147
148
149   public Iterator<RefEdge> iteratorToReferencers() {
150     return referencers.iterator();
151   }
152
153   public Iterator<RefEdge> iteratorToReferencersClone() {
154     HashSet<RefEdge> clone = (HashSet<RefEdge>)referencers.clone();
155     return clone.iterator();
156   }
157
158   public int getNumReferencers() {
159     return referencers.size();
160   }
161
162
163   // in other words, this node is not functionally 
164   // part of the graph (anymore)
165   public boolean isWiped() {
166     return 
167       getNumReferencers() == 0 &&
168       getNumReferencees() == 0;
169   }
170
171
172   public void addReferencer( RefEdge edge ) {
173     assert edge != null;
174
175     referencers.add( edge );
176   }
177
178   public void removeReferencer( RefEdge edge ) {
179     assert edge != null;
180     assert referencers.contains( edge );
181
182     referencers.remove( edge );
183   }
184
185   public RefEdge getReferenceFrom( RefSrcNode     rsn,
186                                    TypeDescriptor type,
187                                    String         field
188                                    ) {
189     assert rsn != null;
190
191     Iterator<RefEdge> itrEdge = referencers.iterator();
192     while( itrEdge.hasNext() ) {
193       RefEdge edge = itrEdge.next();
194
195       if( edge.getSrc().equals( rsn ) &&
196           edge.typeEquals( type )     &&
197           edge.fieldEquals( field ) 
198           ) {
199         return edge;
200       }
201     }
202
203     return null;
204   }
205
206
207   public TypeDescriptor getType() {
208     return type;
209   }  
210
211   public AllocSite getAllocSite() {
212     return allocSite;
213   }
214
215   
216   public ReachSet getInherent() {
217     return inherent;
218   }
219   
220   public ReachSet getAlpha() {
221     return alpha;
222   }
223
224   public void setAlpha( ReachSet alpha ) {
225     this.alpha = alpha;
226   }
227
228   public ReachSet getAlphaNew() {
229     return alphaNew;
230   }
231
232   public void setAlphaNew( ReachSet alpha ) {
233     this.alphaNew = alpha;
234   }
235
236   public void applyAlphaNew() {
237     assert alphaNew != null;
238     alpha = alphaNew;
239     alphaNew = ReachSet.factory();
240   }
241
242
243   public ExistPredSet getPreds() {
244     return preds;
245   }
246
247   public void setPreds( ExistPredSet preds ) {
248     this.preds = preds;
249   }
250
251
252   public String getIDString() {
253     String s;
254
255     if( id < 0 ) {
256       s = "minus" + new Integer( -id ).toString();
257     } else {
258       s = id.toString();
259     }
260
261     return s;
262   }
263
264   public String getDescription() {
265     return description;
266   }  
267
268   public String toStringDOT( boolean hideSubsetReach ) {
269     
270     String attributes = "";
271     
272     if( isSingleObject ) {
273       attributes += "shape=box";
274     } else {
275       attributes += "shape=Msquare";
276     }
277
278     if( isFlagged ) {
279       attributes += ",style=filled,fillcolor=lightgrey";
280     }
281
282     String typeStr;
283     if( type == null ) {
284       typeStr = "null";
285     } else {
286       typeStr = type.toPrettyString();
287     }
288
289     return new String( "["+attributes+
290                        ",label=\"ID"+getIDString()+"\\n"+
291                        typeStr+"\\n"+
292                        description+"\\n"+
293                        alpha.toStringEscNewline( hideSubsetReach )+"\\n"+
294                        preds+"\"]"
295                        );
296   }
297
298   public String toString() {
299     return "HRN"+getIDString();
300   }
301 }