changing to new traversers/examiners
[IRC.git] / Robust / src / Analysis / Disjoint / ExistPredSet.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 set of existence predicates that are
27 // OR'ed terms, if any predicate is true
28 // then the set evaluates to true
29
30 public class ExistPredSet extends Canonical {
31
32   protected Set<ExistPred> preds;
33
34   public static boolean debug = false;
35
36   
37   public static ExistPredSet factory() {
38     ExistPredSet out = new ExistPredSet();
39     out = (ExistPredSet) Canonical.makeCanonical( out );
40     return out;
41   }
42
43   public static ExistPredSet factory( ExistPred pred ) {
44     ExistPredSet out = new ExistPredSet();
45     out.preds.add( pred );
46     out = (ExistPredSet) Canonical.makeCanonical( out );
47     return out;
48   }
49
50   protected ExistPredSet() {
51     preds = new HashSet<ExistPred>();
52   }
53
54   
55   public Iterator<ExistPred> iterator() {
56     return preds.iterator();
57   }
58   
59
60   // only consider the subest of the caller elements that
61   // are reachable by callee when testing predicates
62   public ExistPredSet isSatisfiedBy( ReachGraph   rg,
63                                      Set<Integer> calleeReachableNodes
64                                      ) {
65     ExistPredSet predsOut = null;
66     
67     Iterator<ExistPred> predItr = preds.iterator();
68     while( predItr.hasNext() ) {
69       ExistPredSet predsFromSatisfier =
70         predItr.next().isSatisfiedBy( rg,
71                                       calleeReachableNodes );
72
73       if( predsFromSatisfier != null ) {
74         if( predsOut == null ) {
75           predsOut = predsFromSatisfier;
76         } else {
77           predsOut = Canonical.join( predsOut,
78                                      predsFromSatisfier );
79         }
80       }
81     }
82     
83     return predsOut;
84   }
85
86
87   public boolean isEmpty() {
88     return preds.isEmpty();
89   }
90
91
92   public boolean equalsSpecific( Object o ) {
93     if( o == null ) {
94       return false;
95     }
96
97     if( !(o instanceof ExistPredSet) ) {
98       return false;
99     }
100
101     ExistPredSet eps = (ExistPredSet) o;
102
103     return preds.equals( eps.preds );
104   }
105
106
107   public int hashCodeSpecific() {
108     return preds.hashCode();
109   }
110
111
112   public String toStringEscNewline() {
113     String s = "P[";
114
115     Iterator<ExistPred> predItr = preds.iterator();
116     while( predItr.hasNext() ) {
117       ExistPred pred = predItr.next();
118       s += pred.toString();
119       if( predItr.hasNext() ) {
120         s += " ||\\n";
121       }
122     }
123     s += "]";
124     return s;
125   }
126
127   
128   public String toString() {
129     String s = "P[";
130     Iterator<ExistPred> predItr = preds.iterator();
131     while( predItr.hasNext() ) {
132       ExistPred pred = predItr.next();
133       s += pred.toString();
134       if( predItr.hasNext() ) {
135         s += " || ";
136       }
137     }
138     s += "]";
139     return s;
140   }
141
142 }