my changes
[IRC.git] / Robust / src / Analysis / Disjoint / TaintSet.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 taint set is simply the union of possible
27 // taints for an abstract reference edge--in a
28 // concrete heap each reference would have
29 // exactly one taint
30
31 public class TaintSet extends Canonical {
32
33   protected HashSet<Taint> taints;
34
35   public static TaintSet factory(HashSet<Taint> taints) {
36     TaintSet out = new TaintSet(taints);
37     out = (TaintSet) Canonical.makeCanonical( out );
38     return out;
39   }
40
41   public TaintSet reTaint(FlatNode fn) {
42     HashSet<Taint> taintset=new HashSet<Taint>();
43     for(Taint t:taints) {
44       if (t.getWhereDefined()!=fn) {
45         t=t.reTaint(fn);
46       }
47       taintset.add(t);
48     }
49     
50     TaintSet out=new TaintSet(taintset);
51     out = (TaintSet) Canonical.makeCanonical( out );
52     return out;
53   }
54
55   public static TaintSet factory() {
56     TaintSet out = new TaintSet();
57     out = (TaintSet) Canonical.makeCanonical( out );
58     return out;
59   }
60
61   public static TaintSet factory( Taint t ) {
62     assert t != null;
63     assert t.isCanonical();
64     TaintSet out = new TaintSet();    
65     out.taints.add( t );
66     out = (TaintSet) Canonical.makeCanonical( out );
67     return out;
68   }
69
70   public static TaintSet factory( TaintSet     ts,
71                                   ExistPredSet preds ) {
72     assert ts != null;
73     assert ts.isCanonical();
74
75     TaintSet out = new TaintSet();
76
77     Iterator<Taint> tItr = ts.iterator();
78     while( tItr.hasNext() ) {
79       Taint t    = tItr.next();
80       Taint tOut = Taint.factory( t.sese,
81                                   t.stallSite,
82                                   t.var,
83                                   t.allocSite,
84                                   t.fnDefined,
85                                   preds );
86       out.taints.add( tOut );
87     }
88
89     out = (TaintSet) Canonical.makeCanonical( out );
90     return out;
91   }
92
93   public TaintSet add(Taint t) {
94     TaintSet newt=new TaintSet();
95     newt.taints.addAll(taints);
96     newt.taints.add(t);
97     return (TaintSet) Canonical.makeCanonical(newt);
98   }
99
100   public TaintSet merge(TaintSet ts) {
101     TaintSet newt=new TaintSet();
102     newt.taints.addAll(taints);
103     newt.taints.addAll(ts.taints);
104     return (TaintSet) Canonical.makeCanonical(newt);
105   }
106
107   protected TaintSet() {
108     taints = new HashSet<Taint>();
109   }
110
111   protected TaintSet(HashSet<Taint> taints) {
112     this.taints = taints;
113   }
114
115   public Set<Taint> getTaints() {
116     return taints;
117   }
118
119   public Iterator iterator() {
120     return taints.iterator();
121   }
122
123   public boolean isEmpty() {
124     return taints.isEmpty();
125   }
126
127   public Taint containsIgnorePreds( Taint t ) {
128     assert t != null;
129
130     Iterator<Taint> tItr = taints.iterator();
131     while( tItr.hasNext() ) {
132       Taint tThis = tItr.next();
133       if( tThis.equalsIgnorePreds( t ) ) {
134         return tThis;
135       }
136     }
137
138     return null;
139   }
140
141   public boolean equalsSpecific( Object o ) {
142     if( o == null ) {
143       return false;
144     }
145
146     if( !(o instanceof TaintSet) ) {
147       return false;
148     }
149
150     TaintSet ts = (TaintSet) o;
151     return taints.equals( ts.taints );
152   }
153   
154   public int hashCodeSpecific() {
155     return taints.hashCode();
156   }
157
158   public String toStringEscNewline() {
159     String s = "taints[";
160
161     Iterator<Taint> tItr = taints.iterator();
162     while( tItr.hasNext() ) {
163       Taint t = tItr.next();
164
165       s += t.toString();
166       if( tItr.hasNext() ) {
167         s += ",\\n";
168       }
169     }
170     s += "]";
171     return s;
172   }
173   
174   public String toString() {
175     return taints.toString();
176   }
177 }