Fixed a few minor bugs in token propagation, and major bug that every
[IRC.git] / Robust / src / Analysis / OwnershipAnalysis / ReachabilitySet.java
1 package Analysis.OwnershipAnalysis;
2
3 import IR.*;
4 import IR.Flat.*;
5 import java.util.*;
6 import java.io.*;
7
8
9 public class ReachabilitySet extends Canonical {
10
11     private HashSet<TokenTupleSet> possibleReachabilities;
12
13     public ReachabilitySet() {
14         possibleReachabilities = new HashSet<TokenTupleSet>();
15         TokenTupleSet ttsEmpty = new TokenTupleSet().makeCanonical();
16         possibleReachabilities.add( ttsEmpty ); 
17     }
18
19     public ReachabilitySet( TokenTupleSet tts ) {
20         this();
21         assert tts != null;
22         possibleReachabilities.add( tts );
23     }
24
25     public ReachabilitySet( TokenTuple tt ) {
26         this( new TokenTupleSet( tt ).makeCanonical() );
27     }
28
29     public ReachabilitySet( ReachabilitySet rs ) {
30         assert rs != null;
31         possibleReachabilities = (HashSet<TokenTupleSet>) rs.possibleReachabilities.clone(); // again, DEEP COPY?!
32     }
33
34     public ReachabilitySet makeCanonical() {
35         return (ReachabilitySet) Canonical.makeCanonical( this );
36     }
37
38     public boolean contains( TokenTupleSet tts ) {
39         assert tts != null;
40         return possibleReachabilities.contains( tts );
41     }
42
43     public Iterator iterator() {
44         return possibleReachabilities.iterator();
45     }
46
47     public ReachabilitySet union( ReachabilitySet rsIn ) {
48         assert rsIn != null;
49
50         ReachabilitySet rsOut = new ReachabilitySet( this );
51
52         System.out.println( "rsIn  = "+rsIn );
53         System.out.println( "rsOut = "+rsOut );
54
55         rsOut.possibleReachabilities.addAll( rsIn.possibleReachabilities );
56
57         System.out.println( "union = "+rsOut );
58
59         return rsOut.makeCanonical();
60     }
61
62     public ReachabilitySet union( TokenTupleSet ttsIn ) {
63         assert ttsIn != null;
64
65         ReachabilitySet rsOut = new ReachabilitySet( this );
66         rsOut.possibleReachabilities.add( ttsIn );
67         return rsOut.makeCanonical();
68     }
69
70     public ReachabilitySet intersection( ReachabilitySet rsIn ) {
71         assert rsIn != null;
72
73         ReachabilitySet rsOut = new ReachabilitySet();
74
75         Iterator i = this.iterator();
76         while( i.hasNext() ) {
77             TokenTupleSet tts = (TokenTupleSet) i.next();
78             if( rsIn.possibleReachabilities.contains( tts ) ) {
79                 rsOut.possibleReachabilities.add( tts );
80             }
81         }
82
83         return rsOut.makeCanonical();
84     }
85
86     public ChangeTupleSet unionUpArity( ReachabilitySet rsIn ) {
87         assert rsIn != null;
88
89         ChangeTupleSet ctsOut = new ChangeTupleSet();
90
91         Iterator itrO = this.iterator();
92         while( itrO.hasNext() ) {
93             TokenTupleSet o = (TokenTupleSet) itrO.next();
94
95             Iterator itrR = rsIn.iterator();
96             while( itrR.hasNext() ) {
97                 TokenTupleSet r = (TokenTupleSet) itrR.next();
98
99                 TokenTupleSet theUnion = new TokenTupleSet();
100
101                 Iterator itrRelement = r.iterator();
102                 while( itrRelement.hasNext() ) {
103                     TokenTuple e = (TokenTuple) itrRelement.next();
104
105                     if( o.containsToken( e.getToken() ) ) {
106                         theUnion = theUnion.union( new TokenTupleSet( e.increaseArity() ) ).makeCanonical();
107                     } else {
108                         theUnion = theUnion.union( new TokenTupleSet( e                 ) ).makeCanonical();
109                     }
110                 }
111
112                 Iterator itrOelement = o.iterator();
113                 while( itrOelement.hasNext() ) {
114                     TokenTuple e = (TokenTuple) itrOelement.next();
115
116                     if( !theUnion.containsToken( e.getToken() ) ) {
117                         theUnion = theUnion.union( new TokenTupleSet( e ) ).makeCanonical();
118                     }
119                 }
120
121                 if( !theUnion.isEmpty() ) {
122                     ctsOut = ctsOut.union( 
123                       new ChangeTupleSet( new ChangeTuple( o, theUnion ) )
124                                           );
125                 }
126             }
127         }
128
129         return ctsOut.makeCanonical();
130     }
131
132
133     public boolean equals( Object o ) {
134         if( !(o instanceof ReachabilitySet) ) {
135             return false;
136         }
137
138         ReachabilitySet rs = (ReachabilitySet) o;
139         return possibleReachabilities.equals( rs.possibleReachabilities );
140     }
141
142     public int hashCode() {
143         return possibleReachabilities.hashCode();
144     }
145
146
147     public String toStringEscapeNewline() {
148         String s = "[";
149
150         Iterator i = this.iterator();
151         while( i.hasNext() ) {
152             if( possibleReachabilities.size() > 1 ) {
153                 s += "\\n";
154             }
155             s += " "+i.next();
156         }
157
158         if( possibleReachabilities.size() > 1 ) {
159             s += "\\n";
160         }
161
162         s += " ]";
163
164         return s;       
165     }
166
167     public String toString() {
168         String s = "[";
169
170         Iterator i = this.iterator();
171         while( i.hasNext() ) {
172             if( possibleReachabilities.size() > 1 ) {
173                 s += "\n";
174             }
175             s += " "+i.next();
176         }
177         
178         if( possibleReachabilities.size() > 1 ) {
179             s += "\n";
180         }
181
182         s += " ]";
183
184         return s;       
185     }
186 }