00001 package ca.mcgill.sable.soot.jimple;
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 import ca.mcgill.sable.soot.*;
00083 import ca.mcgill.sable.util.*;
00084
00085 public class JLookupSwitchStmt extends AbstractStmt implements LookupSwitchStmt
00086 {
00087 UnitBox defaultTargetBox;
00088 ValueBox keyBox;
00089 List lookupValues;
00090 protected UnitBox[] targetBoxes;
00091
00092 List stmtBoxes;
00093
00094 JLookupSwitchStmt(Value key, List lookupValues, List targets, Unit defaultTarget)
00095 {
00096 this(Jimple.v().newImmediateBox(key),
00097 lookupValues, getTargetBoxesArray(targets),
00098 Jimple.v().newStmtBox(defaultTarget));
00099 }
00100 protected JLookupSwitchStmt(ValueBox keyBox, List lookupValues,
00101 UnitBox[] targetBoxes,
00102 UnitBox defaultTargetBox)
00103 {
00104 this.keyBox = keyBox;
00105 this.defaultTargetBox = defaultTargetBox;
00106 this.targetBoxes = targetBoxes;
00107
00108 this.lookupValues = new ArrayList();
00109 this.lookupValues.addAll(lookupValues);
00110
00111
00112 {
00113 stmtBoxes = new ArrayList();
00114
00115 for(int i = 0; i < targetBoxes.length; i++)
00116 stmtBoxes.add(targetBoxes[i]);
00117
00118 stmtBoxes.add(defaultTargetBox);
00119 stmtBoxes = Collections.unmodifiableList(stmtBoxes);
00120 }
00121 }
00122 public void apply(Switch sw)
00123 {
00124 ((StmtSwitch) sw).caseLookupSwitchStmt(this);
00125 }
00126 public Unit getDefaultTarget()
00127 {
00128 return defaultTargetBox.getUnit();
00129 }
00130 public UnitBox getDefaultTargetBox()
00131 {
00132 return defaultTargetBox;
00133 }
00134 public List getDefBoxes()
00135 {
00136 return emptyList;
00137 }
00138 public Value getKey()
00139 {
00140 return keyBox.getValue();
00141 }
00142 public ValueBox getKeyBox()
00143 {
00144 return keyBox;
00145 }
00146 public int getLookupValue(int index)
00147 {
00148 return ((Integer) lookupValues.get(index)).intValue();
00149 }
00150 public List getLookupValues()
00151 {
00152 return Collections.unmodifiableList(lookupValues);
00153 }
00154 public Unit getTarget(int index)
00155 {
00156 return targetBoxes[index].getUnit();
00157 }
00158 public UnitBox getTargetBox(int index)
00159 {
00160 return targetBoxes[index];
00161 }
00162
00163 private static UnitBox[] getTargetBoxesArray(List targets)
00164 {
00165 UnitBox[] targetBoxes = new UnitBox[targets.size()];
00166
00167 for(int i = 0; i < targetBoxes.length; i++)
00168 targetBoxes[i] = Jimple.v().newStmtBox((Stmt) targets.get(i));
00169
00170 return targetBoxes;
00171 }
00172 public int getTargetCount()
00173 {
00174 return targetBoxes.length;
00175 }
00176 public List getTargets()
00177 {
00178 List targets = new ArrayList();
00179
00180 for(int i = 0; i < targetBoxes.length; i++)
00181 targets.add(targetBoxes[i].getUnit());
00182
00183 return targets;
00184 }
00185 public List getUnitBoxes()
00186 {
00187 return stmtBoxes;
00188 }
00189 public List getUseBoxes()
00190 {
00191 List list = new ArrayList();
00192
00193 list.addAll(keyBox.getValue().getUseBoxes());
00194 list.add(keyBox);
00195
00196 return list;
00197 }
00198 public void setDefaultTarget(Unit defaultTarget)
00199 {
00200 defaultTargetBox.setUnit(defaultTarget);
00201 }
00202 public void setKey(Value key)
00203 {
00204 keyBox.setValue(key);
00205 }
00206 public void setLookupValue(int index, int value)
00207 {
00208 this.lookupValues.set(index, new Integer(value));
00209 }
00210 public void setLookupValues(List lookupValues)
00211 {
00212 this.lookupValues = new ArrayList();
00213 this.lookupValues.addAll(lookupValues);
00214 }
00215 public void setTarget(int index, Unit target)
00216 {
00217 targetBoxes[index].setUnit(target);
00218 }
00219 public void setTargets(Unit[] targets)
00220 {
00221 for(int i = 0; i < targets.length; i++)
00222 targetBoxes[i].setUnit(targets[i]);
00223 }
00224 protected String toString(boolean isBrief, Map stmtToName, String indentation)
00225 {
00226 StringBuffer buffer = new StringBuffer();
00227 String endOfLine = (indentation.equals("")) ? " " : "\n";
00228
00229 buffer.append(indentation + "lookupswitch(" + ((isBrief) ? ((ToBriefString) keyBox.getValue()).toBriefString() :
00230 keyBox.getValue().toString()) + ")" + endOfLine);
00231
00232 buffer.append(indentation + "{" + endOfLine);
00233
00234 for(int i = 0; i < lookupValues.size(); i++)
00235 {
00236 buffer.append(indentation + " case " + lookupValues.get(i) + ": goto " +
00237 (String) stmtToName.get(getTarget(i)) + ";" + endOfLine);
00238 }
00239
00240 buffer.append(indentation + " default: goto " + (String) stmtToName.get(getDefaultTarget()) + ";" + endOfLine);
00241 buffer.append(indentation + "}");
00242
00243 return buffer.toString();
00244 }
00245 }