Stable, partial implementation of method calls
[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   // acutally, multiple-object regions can be arity-many
22   // so isNewSummary actually means "multi-object" in
23   // this class.  CHANGE THIS SOMETIME!
24   public static final int ARITY_ONE  = 1;
25   public static final int ARITY_MANY = 2;
26   private int arity;
27
28
29   public TokenTuple(HeapRegionNode hrn) {
30     assert hrn != null;
31
32     token        = hrn.getID();
33     isNewSummary = hrn.isNewSummary();
34     arity        = ARITY_ONE;
35   }
36
37   public TokenTuple(Integer token,
38                     boolean isNewSummary,
39                     int arity) {
40     assert token != null;
41
42     this.token        = token;
43     this.isNewSummary = isNewSummary;
44     this.arity        = arity;
45   }
46
47
48   public TokenTuple makeCanonical() {
49     return (TokenTuple) Canonical.makeCanonical(this);
50   }
51
52
53   public Integer getToken() {
54     return token;
55   }
56   public int     getArity() {
57     return arity;
58   }
59
60
61   public TokenTuple increaseArity() {
62     if( isNewSummary ) {
63       return (TokenTuple) Canonical.makeCanonical(
64                new TokenTuple(token, isNewSummary, ARITY_MANY)
65                );
66     }
67     return this;
68   }
69
70
71   public TokenTuple changeTokenTo(Integer tokenToChangeTo) {
72     assert tokenToChangeTo != null;
73     assert isNewSummary    == false;
74
75     return new TokenTuple(tokenToChangeTo,
76                           isNewSummary,
77                           arity).makeCanonical();
78   }
79
80
81   public boolean equals(Object o) {
82     if( o == null ) {
83       return false;
84     }
85
86     if( !(o instanceof TokenTuple) ) {
87       return false;
88     }
89
90     TokenTuple tt = (TokenTuple) o;
91
92     return token.equals(tt.getToken() ) &&
93            arity ==      tt.getArity();
94   }
95
96   public int hashCode() {
97     return token.intValue()*31 + arity;
98   }
99
100
101   public String toString() {
102     String s = token.toString();
103
104     if( isNewSummary ) {
105       s += "S";
106     }
107
108     if( arity == ARITY_MANY ) {
109       s += "*";
110     }
111
112     return s;
113   }
114 }