KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > jimple > internal > JArrayRef


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 1999 Patrick Lam
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 /*
21  * Modified by the Sable Research Group and others 1997-1999.
22  * See the 'credits' file distributed with Soot for the complete list of
23  * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24  */

25
26
27
28
29
30
31 package soot.jimple.internal;
32
33 import soot.*;
34 import soot.tagkit.*;
35 import soot.jimple.*;
36 import soot.baf.*;
37 import soot.jimple.*;
38 import soot.util.*;
39 import java.util.*;
40
41 public class JArrayRef implements ArrayRef, ConvertToBaf
42 {
43     protected ValueBox baseBox;
44     protected ValueBox indexBox;
45
46     public JArrayRef(Value base, Value index)
47     {
48         this(Jimple.v().newLocalBox(base),
49              Jimple.v().newImmediateBox(index));
50     }
51
52     protected JArrayRef(ValueBox baseBox, ValueBox indexBox)
53     {
54         this.baseBox = baseBox;
55         this.indexBox = indexBox;
56     }
57     
58     public Object JavaDoc clone()
59     {
60         return new JArrayRef(Jimple.cloneIfNecessary(getBase()), Jimple.cloneIfNecessary(getIndex()));
61     }
62
63     public boolean equivTo(Object JavaDoc o)
64     {
65         if (o instanceof ArrayRef)
66           {
67             return (getBase().equivTo(((ArrayRef)o).getBase())
68                     && getIndex().equivTo(((ArrayRef)o).getIndex()));
69           }
70         return false;
71     }
72
73     /** Returns a hash code for this object, consistent with structural equality. */
74     public int equivHashCode()
75     {
76         return getBase().equivHashCode() * 101 + getIndex().equivHashCode() + 17;
77     }
78
79     public String JavaDoc toString()
80     {
81         return baseBox.getValue().toString() + "[" + indexBox.getValue().toString() + "]";
82     }
83     
84     public void toString(UnitPrinter up) {
85         baseBox.toString(up);
86         up.literal("[");
87         indexBox.toString(up);
88         up.literal("]");
89     }
90
91     public Value getBase()
92     {
93         return baseBox.getValue();
94     }
95
96     public void setBase(Local base)
97     {
98         baseBox.setValue(base);
99     }
100
101     public ValueBox getBaseBox()
102     {
103         return baseBox;
104     }
105
106     public Value getIndex()
107     {
108         return indexBox.getValue();
109     }
110
111     public void setIndex(Value index)
112     {
113         indexBox.setValue(index);
114     }
115
116     public ValueBox getIndexBox()
117     {
118         return indexBox;
119     }
120
121     public List getUseBoxes()
122     {
123         List useBoxes = new ArrayList();
124
125         useBoxes.addAll(baseBox.getValue().getUseBoxes());
126         useBoxes.add(baseBox);
127
128         useBoxes.addAll(indexBox.getValue().getUseBoxes());
129         useBoxes.add(indexBox);
130
131         return useBoxes;
132     }
133
134     public Type getType()
135     {
136         Value base = (Value) baseBox.getValue();
137         Type type = base.getType();
138
139         if(type.equals(UnknownType.v()))
140             return UnknownType.v();
141         else if(type.equals(NullType.v()))
142             return NullType.v();
143         else {
144             ArrayType arrayType = (ArrayType) type;
145
146             if(arrayType.numDimensions == 1)
147                 return arrayType.baseType;
148             else
149                 return ArrayType.v(arrayType.baseType, arrayType.numDimensions - 1);
150         }
151     }
152
153     public void apply(Switch sw)
154     {
155         ((RefSwitch) sw).caseArrayRef(this);
156     }
157
158     public void convertToBaf(JimpleToBafContext context, List out)
159     {
160         ((ConvertToBaf)getBase()).convertToBaf(context, out);
161         ((ConvertToBaf)getIndex()).convertToBaf(context, out);
162     
163     Unit currentUnit = context.getCurrentUnit();
164
165     Unit x;
166
167         out.add(x = Baf.v().newArrayReadInst(getType()));
168
169     Iterator it = currentUnit.getTags().iterator();
170     while(it.hasNext()) {
171         x.addTag((Tag) it.next());
172     }
173
174     }
175 }
176
177
178
179
Popular Tags