KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > toolkits > scalar > ValueUnitPair


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 Navindra Umanee <navindra@cs.mcgill.ca>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.toolkits.scalar;
21
22 import soot.*;
23 import soot.jimple.Jimple;
24
25 /**
26  * Utility class used to package a Value and a Unit together.
27  *
28  * @author Navindra Umanee
29  **/

30 public class ValueUnitPair extends AbstractValueBox implements UnitBox, EquivTo
31 {
32     // oub was initially a private inner class. ended up being a
33
// *bad* *bad* idea with endless opportunity for *evil* *evil*
34
// pointer bugs. in the end so much code needed to be copy pasted
35
// that using an innerclass to reuse code from AbstractUnitBox was
36
// a losing proposition.
37
// protected UnitBox oub;
38
protected Unit unit;
39         
40     /**
41      * Constructs a ValueUnitPair from a Unit object and a Value object.
42      *
43      * @param value some Value
44      * @param unit some Unit.
45      **/

46     public ValueUnitPair(Value value, Unit unit)
47     {
48         setValue(value);
49         setUnit(unit);
50     }
51
52     public boolean canContainValue(Value value)
53     {
54     return true;
55     }
56
57     /**
58      * @see soot.UnitBox#setUnit(Unit)
59      **/

60     public void setUnit(Unit unit)
61     {
62         /* Code copied from AbstractUnitBox */
63         
64         if(!canContainUnit(unit))
65             throw new RuntimeException JavaDoc("Cannot put " + unit + " in this box");
66         
67         // Remove this from set of back pointers.
68
if(this.unit != null){
69             this.unit.removeBoxPointingToThis(this);
70         }
71
72         // Perform link
73
this.unit = unit;
74
75         // Add this to back pointers
76
if(this.unit != null){
77             this.unit.addBoxPointingToThis(this);
78         }
79     }
80
81     /**
82      * @see soot.UnitBox#getUnit()
83      **/

84     public Unit getUnit()
85     {
86         return unit;
87     }
88
89     /**
90      * @see soot.UnitBox#canContainUnit(Unit)
91      **/

92     public boolean canContainUnit(Unit u)
93     {
94         return true;
95     }
96
97     /**
98      * @see soot.UnitBox#isBranchTarget()
99      **/

100     public boolean isBranchTarget()
101     {
102         return true;
103     }
104
105     public String JavaDoc toString()
106     {
107         return "Value = " + getValue() + ", Unit = " + getUnit();
108     }
109
110     public void toString(UnitPrinter up)
111     {
112         super.toString(up);
113
114         if(isBranchTarget())
115             up.literal(", ");
116         else
117             up.literal(" #");
118
119         up.startUnitBox(this);
120         up.unitRef(unit, isBranchTarget());
121         up.endUnitBox(this);
122     }
123     
124     public int hashCode()
125     {
126         // If you need to change this implementation, please change it
127
// in a subclass. Otherwise, Shimple is likely to break in evil
128
// ways.
129
return super.hashCode();
130     }
131
132     public boolean equals(Object JavaDoc other)
133     {
134         // If you need to change this implementation, please change it
135
// in a subclass. Otherwise, Shimple is likely to break in evil
136
// ways.
137
return super.equals(other);
138     }
139     
140     /**
141      * Two ValueUnitPairs are equivTo iff they hold the same
142      * Unit objects and equivalent Value objects within them.
143      *
144      * @param other another ValueUnitPair
145      * @return true if other contains the same objects as this.
146      **/

147     public boolean equivTo(Object JavaDoc other)
148     {
149         return
150             (other instanceof ValueUnitPair) &&
151             ((ValueUnitPair) other).getValue().equivTo(this.getValue()) &&
152             ((ValueUnitPair) other).getUnit().equals(getUnit());
153     }
154
155     /**
156      * Non-deterministic hashcode consistent with equivTo()
157      * implementation.
158      *
159      * <p>
160      *
161      * <b>Note:</b> If you are concerned about non-determinism,
162      * remember that current implementations of equivHashCode() in
163      * other parts of Soot are non-deterministic as well (see
164      * Constant.java for example).
165      **/

166     public int equivHashCode()
167     {
168         // this is not deterministic because a Unit's hash code is
169
// non-deterministic.
170
return
171             (getUnit().hashCode() * 17) +
172             (getValue().equivHashCode() * 101);
173     }
174     
175     public Object JavaDoc clone()
176     {
177         // Note to self: Do not try to "fix" this. Yes, it should be
178
// a shallow copy in order to conform with the rest of Soot.
179
// When a body is cloned, the Values are cloned explicitly and
180
// replaced, and UnitBoxes are explicitly patched. See
181
// Body.importBodyContentsFrom for details.
182
Value cv = Jimple.cloneIfNecessary((Value) getValue());
183         Unit cu = (Unit) getUnit();
184         return new ValueUnitPair(cv, cu);
185     }
186 }
187
Popular Tags