Fix tabbing.... Please fix your editors so they do tabbing correctly!!! (Spaces...
[IRC.git] / Robust / src / Analysis / Disjoint / ReachState.java
1 package Analysis.Disjoint;
2
3 import IR.*;
4 import IR.Flat.*;
5 import java.util.*;
6 import java.io.*;
7
8 ///////////////////////////////////////////
9 //  IMPORTANT
10 //  This class is an immutable Canonical, so
11 //
12 //  0) construct them with a factory pattern
13 //  to ensure only canonical versions escape
14 //
15 //  1) any operation that modifies a Canonical
16 //  is a static method in the Canonical class
17 //
18 //  2) operations that just read this object
19 //  should be defined here
20 //
21 //  3) every Canonical subclass hashCode should
22 //  throw an error if the hash ever changes
23 //
24 ///////////////////////////////////////////
25
26 // a reach state is a set of reach tuples
27 // such that any heap region ID in a tuple
28 // appears at most once in the state
29
30 public class ReachState extends Canonical {
31
32   protected HashSet<ReachTuple> reachTuples;
33
34   // existance predicates must be true in a caller
35   // context for this state to transfer from this
36   // callee to that context
37   protected ExistPredSet preds;
38
39
40   public static ReachState factory() {
41     ReachState out = new ReachState();
42     out = (ReachState) Canonical.makeCanonical(out);
43     return out;
44   }
45
46   public static ReachState factory(ReachTuple rt) {
47     assert rt != null;
48     assert rt.isCanonical();
49     ReachState out = new ReachState();
50     out.reachTuples.add(rt);
51     out = (ReachState) Canonical.makeCanonical(out);
52     return out;
53   }
54
55   public static ReachState factory(HashSet<ReachTuple> reachTuples,
56                                    ExistPredSet preds
57                                    ) {
58     assert reachTuples != null;
59     assert preds != null;
60     assert preds.isCanonical();
61     ReachState out = new ReachState();
62     out.reachTuples.addAll(reachTuples);
63     out.preds = preds;
64     out = (ReachState) Canonical.makeCanonical(out);
65     return out;
66   }
67
68   protected ReachState() {
69     reachTuples = new HashSet<ReachTuple>();
70     preds       = ExistPredSet.factory();
71   }
72
73
74   public Iterator iterator() {
75     return reachTuples.iterator();
76   }
77
78   public boolean isEmpty() {
79     return reachTuples.isEmpty();
80   }
81
82   public boolean isSubset(ReachState rsIn) {
83     assert rsIn != null;
84     return rsIn.reachTuples.containsAll(this.reachTuples);
85   }
86
87   public boolean containsTuple(ReachTuple rt) {
88     assert rt != null;
89     return reachTuples.contains(rt);
90   }
91
92   // this should be a hash table so we can do this by key
93   public ReachTuple containsHrnID(Integer hrnID,
94                                   boolean isOutOfContext) {
95     assert hrnID != null;
96
97     Iterator<ReachTuple> rtItr = reachTuples.iterator();
98     while( rtItr.hasNext() ) {
99       ReachTuple rt = rtItr.next();
100       if( hrnID.equals(rt.getHrnID() ) &&
101           isOutOfContext == rt.isOutOfContext()
102           ) {
103         return rt;
104       }
105     }
106
107     return null;
108   }
109
110   public ExistPredSet getPreds() {
111     return preds;
112   }
113
114
115   public boolean equalsSpecific(Object o) {
116     if( o == null ) {
117       return false;
118     }
119
120     if( !(o instanceof ReachState) ) {
121       return false;
122     }
123
124     ReachState rs = (ReachState) o;
125     return
126       reachTuples.equals(rs.reachTuples) &&
127       preds.equals(rs.preds);
128   }
129
130
131   public int hashCodeSpecific() {
132     return
133       reachTuples.hashCode() ^
134       preds.hashCode();
135   }
136
137
138   public boolean equalsIgnorePreds(Object o) {
139     if( o == null ) {
140       return false;
141     }
142
143     if( !(o instanceof ReachState) ) {
144       return false;
145     }
146
147     ReachState rs = (ReachState) o;
148     return
149       reachTuples.equals(rs.reachTuples);
150   }
151
152
153
154   public String toString() {
155     return reachTuples.toString();
156     //return reachTuples+":"+preds;
157   }
158
159   public String toStringPreds() {
160     return reachTuples+":"+preds;
161   }
162 }