make sure change sets ignore predicates hanging off reach states and alter reach...
[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   
35   public static ExistPredSet factory() {
36     ExistPredSet out = new ExistPredSet();
37     out = (ExistPredSet) Canonical.makeCanonical( out );
38     return out;
39   }
40
41   public static ExistPredSet factory( ExistPred pred ) {
42     ExistPredSet out = new ExistPredSet();
43     out.preds.add( pred );
44     out = (ExistPredSet) Canonical.makeCanonical( out );
45     return out;
46   }
47
48   protected ExistPredSet() {
49     preds = new HashSet<ExistPred>();
50   }
51
52   
53   public Iterator<ExistPred> iterator() {
54     return preds.iterator();
55   }
56   
57
58   // only consider the subest of the caller elements that
59   // are reachable by callee when testing predicates
60   public ExistPredSet isSatisfiedBy( ReachGraph   rg,
61                                      Set<Integer> calleeReachableNodes
62                                      ) {
63     ExistPredSet predsOut = null;
64     
65     Iterator<ExistPred> predItr = preds.iterator();
66     while( predItr.hasNext() ) {
67       ExistPredSet predsFromSatisfier =
68         predItr.next().isSatisfiedBy( rg,
69                                       calleeReachableNodes );
70
71       if( predsFromSatisfier != null ) {
72         if( predsOut == null ) {
73           predsOut = predsFromSatisfier;
74         } else {
75           predsOut = Canonical.join( predsOut,
76                                      predsFromSatisfier );
77         }
78       }
79     }
80     
81     return predsOut;
82   }
83
84
85   public boolean equals( Object o ) {
86     if( o == null ) {
87       return false;
88     }
89
90     if( !(o instanceof ExistPredSet) ) {
91       return false;
92     }
93
94     ExistPredSet eps = (ExistPredSet) o;
95
96     return preds.equals( eps.preds );
97   }
98
99
100   public int hashCodeSpecific() {
101     return preds.hashCode();
102   }
103
104   
105   public String toString() {
106     String s = "P[";
107     Iterator<ExistPred> predItr = preds.iterator();
108     while( predItr.hasNext() ) {
109       ExistPred pred = predItr.next();
110       s += pred.toString();
111       if( predItr.hasNext() ) {
112         s += " || ";
113       }
114     }
115     s += "]";
116     return s;
117   }
118
119 }