04173760b7e56b67867651839c50241fa05c4a2b
[IRC.git] / Robust / src / Analysis / OwnershipAnalysis / TokenTuple.java
1 package Analysis.OwnershipAnalysis;
2
3 import IR.*;
4 import IR.Flat.*;
5 import java.util.*;
6 import java.io.*;
7
8
9 // a token touple is a pair that indicates a
10 // heap region node and an arity
11
12 // THIS CLASS IS IMMUTABLE!
13
14 public class TokenTuple extends Canonical {
15
16   private Integer token;
17   private boolean isNewSummary;
18
19
20   // only summary tokens should have ARITY_MANY?
21   public static final int ARITY_ONE  = 1;
22   public static final int ARITY_MANY = 2;
23   private int arity;
24
25
26   public TokenTuple(HeapRegionNode hrn) {
27     assert hrn != null;
28
29     token        = hrn.getID();
30     isNewSummary = hrn.isNewSummary();
31     arity        = ARITY_ONE;
32   }
33
34   public TokenTuple(Integer token,
35                     boolean isNewSummary,
36                     int arity) {
37     assert token != null;
38
39     this.token        = token;
40     this.isNewSummary = isNewSummary;
41     this.arity        = arity;
42   }
43
44
45   public TokenTuple makeCanonical() {
46     return (TokenTuple) Canonical.makeCanonical(this);
47   }
48
49
50   public Integer getToken() {
51     return token;
52   }
53   public int     getArity() {
54     return arity;
55   }
56
57
58   public TokenTuple increaseArity() {
59     if( isNewSummary ) {
60       return (TokenTuple) Canonical.makeCanonical(
61                new TokenTuple(token, isNewSummary, ARITY_MANY)
62                );
63     }
64     return this;
65   }
66
67
68   public TokenTuple changeTokenTo(Integer tokenToChangeTo) {
69     assert tokenToChangeTo != null;
70     assert isNewSummary    == false;
71
72     return new TokenTuple(tokenToChangeTo,
73                           isNewSummary,
74                           arity).makeCanonical();
75   }
76
77
78   public boolean equals(Object o) {
79     if( o == null ) {
80       return false;
81     }
82
83     if( !(o instanceof TokenTuple) ) {
84       return false;
85     }
86
87     TokenTuple tt = (TokenTuple) o;
88
89     return token.equals(tt.getToken() ) &&
90            arity ==      tt.getArity();
91   }
92
93   public int hashCode() {
94     return token.intValue()*31 + arity;
95   }
96
97
98   public String toString() {
99     String s = token.toString();
100
101     if( isNewSummary ) {
102       s += "S";
103     }
104
105     if( arity == ARITY_MANY ) {
106       s += "*";
107     }
108
109     return s;
110   }
111 }