KickJava   Java API By Example, From Geeks To Geeks.

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


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.tagkit.*;
34 import soot.*;
35 import soot.jimple.*;
36 import soot.baf.*;
37 import soot.jimple.*;
38 import soot.util.*;
39 import java.util.*;
40
41 public abstract class AbstractNewMultiArrayExpr implements NewMultiArrayExpr, ConvertToBaf
42 {
43     ArrayType baseType;
44     protected ValueBox[] sizeBoxes;
45
46     public abstract Object JavaDoc clone();
47     
48     protected AbstractNewMultiArrayExpr(ArrayType type, ValueBox[] sizeBoxes)
49     {
50         this.baseType = type; this.sizeBoxes = sizeBoxes;
51     }
52
53     public boolean equivTo(Object JavaDoc o)
54     {
55         if (o instanceof AbstractNewMultiArrayExpr)
56         {
57             AbstractNewMultiArrayExpr ae = (AbstractNewMultiArrayExpr)o;
58             if (!baseType.equals(ae.baseType) ||
59                     sizeBoxes.length != ae.sizeBoxes.length)
60                 return false;
61             for (int i = 0; i < sizeBoxes.length; i++)
62                 if (sizeBoxes[i] != ae.sizeBoxes[i])
63                     return false;
64             return true;
65         }
66         return false;
67     }
68
69     /** Returns a hash code for this object, consistent with structural equality. */
70     public int equivHashCode()
71     {
72         return baseType.hashCode();
73     }
74
75     public String JavaDoc toString()
76     {
77         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
78
79         Type t = baseType.baseType;
80     buffer.append(Jimple.v().NEWMULTIARRAY + " (" + t.toString() + ")");
81
82         for(int i = 0; i < sizeBoxes.length; i++)
83             buffer.append("[" + sizeBoxes[i].getValue().toString() + "]");
84
85         for(int i = 0; i < baseType.numDimensions - sizeBoxes.length; i++)
86             buffer.append("[]");
87
88         return buffer.toString();
89     }
90     
91     public void toString(UnitPrinter up)
92     {
93         Type t = baseType.baseType;
94         
95         up.literal(Jimple.v().NEWMULTIARRAY);
96         up.literal(" (");
97         up.type(t);
98         up.literal(")");
99
100         for(int i = 0; i < sizeBoxes.length; i++) {
101             up.literal("[");
102             sizeBoxes[i].toString(up);
103             up.literal("]");
104         }
105         
106         for(int i = 0; i < baseType.numDimensions - sizeBoxes.length; i++) {
107             up.literal("[]");
108         }
109     }
110
111     public ArrayType getBaseType()
112     {
113         return baseType;
114     }
115
116     public void setBaseType(ArrayType baseType)
117     {
118         this.baseType = baseType;
119     }
120
121     public ValueBox getSizeBox(int index)
122     {
123         return sizeBoxes[index];
124     }
125
126     public int getSizeCount()
127     {
128         return sizeBoxes.length;
129     }
130
131     public Value getSize(int index)
132     {
133         return sizeBoxes[index].getValue();
134     }
135
136     public List getSizes()
137     {
138         List toReturn = new ArrayList();
139
140         for(int i = 0; i < sizeBoxes.length; i++)
141             toReturn.add(sizeBoxes[i].getValue());
142
143         return toReturn;
144     }
145
146     public void setSize(int index, Value size)
147     {
148         sizeBoxes[index].setValue(size);
149     }
150
151     public List getUseBoxes()
152     {
153         List list = new ArrayList();
154
155         for(int i = 0; i < sizeBoxes.length; i++)
156         {
157             list.addAll(sizeBoxes[i].getValue().getUseBoxes());
158             list.add(sizeBoxes[i]);
159         }
160
161         return list;
162     }
163
164     public Type getType()
165     {
166         return baseType;
167     }
168
169     public void apply(Switch sw)
170     {
171         ((ExprSwitch) sw).caseNewMultiArrayExpr(this);
172     }
173
174     public void convertToBaf(JimpleToBafContext context, List out)
175     {
176         List sizes = getSizes();
177
178         for(int i = 0; i < sizes.size(); i++)
179             ((ConvertToBaf)(sizes.get(i))).convertToBaf(context, out);
180     
181     Unit u;
182         out.add(u = Baf.v().newNewMultiArrayInst(getBaseType(), sizes.size()));
183
184     Unit currentUnit = context.getCurrentUnit();
185     Iterator it = currentUnit.getTags().iterator();
186     while(it.hasNext()) {
187         u.addTag((Tag) it.next());
188     }
189     
190     }
191 }
192
Popular Tags