001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package EDU.purdue.cs.bloat.tree;
022:
023: import java.util.*;
024:
025: import EDU.purdue.cs.bloat.cfg.*;
026:
027: /**
028: * ReplaceVisitor traverses a tree and replaces each occurrence of one Node with
029: * another Node.
030: */
031: public class ReplaceVisitor extends TreeVisitor {
032: Node from;
033:
034: Node to;
035:
036: /**
037: * Constructor.
038: *
039: * @param from
040: * The "old" Node.
041: * @param to
042: * The "new" Node.
043: */
044: public ReplaceVisitor(final Node from, final Node to) {
045: this .from = from;
046: this .to = to;
047:
048: if (Tree.DEBUG) {
049: System.out.println("replace " + from + " VN="
050: + from.valueNumber() + " in " + from.parent
051: + " with " + to);
052: }
053: }
054:
055: public void visitTree(final Tree tree) {
056: if (to instanceof Stmt) {
057: ((Stmt) to).setParent(tree);
058:
059: // The most common statement replacement is the last statement.
060: // so search from the end of the statement list.
061: final ListIterator iter = tree.stmts
062: .listIterator(tree.stmts.size());
063:
064: while (iter.hasPrevious()) {
065: final Stmt s = (Stmt) iter.previous();
066: if (s == from) {
067: iter.set(to);
068: break;
069: }
070: }
071: } else {
072: tree.visitChildren(this );
073: }
074: }
075:
076: public void visitExprStmt(final ExprStmt stmt) {
077: if (stmt.expr == from) {
078: stmt.expr = (Expr) to;
079: ((Expr) to).setParent(stmt);
080: } else {
081: stmt.visitChildren(this );
082: }
083: }
084:
085: public void visitInitStmt(final InitStmt stmt) {
086: for (int i = 0; i < stmt.targets.length; i++) {
087: if (stmt.targets[i] == from) {
088: stmt.targets[i] = (LocalExpr) to;
089: ((LocalExpr) to).setParent(stmt);
090: return;
091: }
092: }
093:
094: stmt.visitChildren(this );
095: }
096:
097: public void visitGotoStmt(final GotoStmt stmt) {
098: stmt.visitChildren(this );
099: }
100:
101: public void visitMonitorStmt(final MonitorStmt stmt) {
102: if (stmt.object == from) {
103: stmt.object = (Expr) to;
104: ((Expr) to).setParent(stmt);
105: } else {
106: stmt.visitChildren(this );
107: }
108: }
109:
110: public void visitStackManipStmt(final StackManipStmt stmt) {
111: for (int i = 0; i < stmt.target.length; i++) {
112: if (stmt.target[i] == from) {
113: stmt.target[i] = (StackExpr) to;
114: ((Expr) to).setParent(stmt);
115: return;
116: }
117: }
118:
119: for (int i = 0; i < stmt.source.length; i++) {
120: if (stmt.source[i] == from) {
121: stmt.source[i] = (StackExpr) to;
122: ((Expr) to).setParent(stmt);
123: return;
124: }
125: }
126:
127: stmt.visitChildren(this );
128: }
129:
130: public void visitCatchExpr(final CatchExpr expr) {
131: expr.visitChildren(this );
132: }
133:
134: public void visitPhiJoinStmt(final PhiJoinStmt stmt) {
135: if (stmt.target == from) {
136: stmt.target = (VarExpr) to;
137: ((VarExpr) to).setParent(stmt);
138: } else {
139: final Iterator e = stmt.operands.keySet().iterator();
140:
141: while (e.hasNext()) {
142: final Block block = (Block) e.next();
143:
144: if (stmt.operandAt(block) == from) {
145: stmt.setOperandAt(block, (Expr) to);
146: ((Expr) to).setParent(stmt);
147: return;
148: }
149: }
150:
151: stmt.visitChildren(this );
152: }
153: }
154:
155: public void visitPhiCatchStmt(final PhiCatchStmt stmt) {
156: if (stmt.target == from) {
157: stmt.target = (LocalExpr) to;
158: ((LocalExpr) to).setParent(stmt);
159: } else {
160: final ListIterator e = stmt.operands.listIterator();
161:
162: while (e.hasNext()) {
163: final LocalExpr expr = (LocalExpr) e.next();
164:
165: if (expr == from) {
166: e.set(to);
167: from.cleanup();
168: ((LocalExpr) to).setParent(stmt);
169: return;
170: }
171: }
172:
173: stmt.visitChildren(this );
174: }
175: }
176:
177: public void visitRetStmt(final RetStmt stmt) {
178: stmt.visitChildren(this );
179: }
180:
181: public void visitReturnExprStmt(final ReturnExprStmt stmt) {
182: if (stmt.expr == from) {
183: stmt.expr = (Expr) to;
184: ((Expr) to).setParent(stmt);
185: } else {
186: stmt.visitChildren(this );
187: }
188: }
189:
190: public void visitReturnStmt(final ReturnStmt stmt) {
191: stmt.visitChildren(this );
192: }
193:
194: public void visitAddressStoreStmt(final AddressStoreStmt stmt) {
195: stmt.visitChildren(this );
196: }
197:
198: public void visitStoreExpr(final StoreExpr expr) {
199: if (expr.target == from) {
200: expr.target = (MemExpr) to;
201: ((MemExpr) to).setParent(expr);
202: } else if (expr.expr == from) {
203: expr.expr = (Expr) to;
204: ((Expr) to).setParent(expr);
205: } else {
206: expr.visitChildren(this );
207: }
208: }
209:
210: public void visitSwitchStmt(final SwitchStmt stmt) {
211: if (stmt.index == from) {
212: stmt.index = (Expr) to;
213: ((Expr) to).setParent(stmt);
214: } else {
215: stmt.visitChildren(this );
216: }
217: }
218:
219: public void visitThrowStmt(final ThrowStmt stmt) {
220: if (stmt.expr == from) {
221: stmt.expr = (Expr) to;
222: ((Expr) to).setParent(stmt);
223: } else {
224: stmt.visitChildren(this );
225: }
226: }
227:
228: public void visitSCStmt(final SCStmt stmt) {
229: if (stmt.array == from) {
230: stmt.array = (Expr) to;
231: ((Expr) to).setParent(stmt);
232: } else if (stmt.index == from) {
233: stmt.index = (Expr) to;
234: ((Expr) to).setParent(stmt);
235: } else {
236: stmt.visitChildren(this );
237: }
238: }
239:
240: public void visitSRStmt(final SRStmt stmt) {
241: if (stmt.array == from) {
242: stmt.array = (Expr) to;
243: ((Expr) to).setParent(stmt);
244: } else if (stmt.start == from) {
245: stmt.start = (Expr) to;
246: ((Expr) to).setParent(stmt);
247: } else if (stmt.end == from) {
248: stmt.end = (Expr) to;
249: ((Expr) to).setParent(stmt);
250: } else {
251: stmt.visitChildren(this );
252: }
253: }
254:
255: public void visitDefExpr(final DefExpr expr) {
256: expr.visitChildren(this );
257: }
258:
259: public void visitArrayLengthExpr(final ArrayLengthExpr expr) {
260: if (expr.array == from) {
261: expr.array = (Expr) to;
262: ((Expr) to).setParent(expr);
263: } else {
264: expr.visitChildren(this );
265: }
266: }
267:
268: public void visitArithExpr(final ArithExpr expr) {
269: if (expr.left == from) {
270: expr.left = (Expr) to;
271: ((Expr) to).setParent(expr);
272: } else if (expr.right == from) {
273: expr.right = (Expr) to;
274: ((Expr) to).setParent(expr);
275: } else {
276: expr.visitChildren(this );
277: }
278: }
279:
280: public void visitArrayRefExpr(final ArrayRefExpr expr) {
281: if (expr.array == from) {
282: expr.array = (Expr) to;
283: ((Expr) to).setParent(expr);
284: } else if (expr.index == from) {
285: expr.index = (Expr) to;
286: ((Expr) to).setParent(expr);
287: } else {
288: expr.visitChildren(this );
289: }
290: }
291:
292: public void visitCallMethodExpr(final CallMethodExpr expr) {
293: if (expr.receiver == from) {
294: expr.receiver = (Expr) to;
295: ((Expr) to).setParent(expr);
296: } else {
297: for (int i = 0; i < expr.params.length; i++) {
298: if (expr.params[i] == from) {
299: expr.params[i] = (Expr) to;
300: ((Expr) to).setParent(expr);
301: return;
302: }
303: }
304:
305: expr.visitChildren(this );
306: }
307: }
308:
309: public void visitCallStaticExpr(final CallStaticExpr expr) {
310: for (int i = 0; i < expr.params.length; i++) {
311: if (expr.params[i] == from) {
312: expr.params[i] = (Expr) to;
313: ((Expr) to).setParent(expr);
314: return;
315: }
316: }
317:
318: expr.visitChildren(this );
319: }
320:
321: public void visitCastExpr(final CastExpr expr) {
322: if (expr.expr == from) {
323: expr.expr = (Expr) to;
324: ((Expr) to).setParent(expr);
325: } else {
326: expr.visitChildren(this );
327: }
328: }
329:
330: public void visitConstantExpr(final ConstantExpr expr) {
331: expr.visitChildren(this );
332: }
333:
334: public void visitFieldExpr(final FieldExpr expr) {
335: if (expr.object == from) {
336: expr.object = (Expr) to;
337: ((Expr) to).setParent(expr);
338: } else {
339: expr.visitChildren(this );
340: }
341: }
342:
343: public void visitInstanceOfExpr(final InstanceOfExpr expr) {
344: if (expr.expr == from) {
345: expr.expr = (Expr) to;
346: ((Expr) to).setParent(expr);
347: } else {
348: expr.visitChildren(this );
349: }
350: }
351:
352: public void visitLocalExpr(final LocalExpr expr) {
353: expr.visitChildren(this );
354: }
355:
356: public void visitNegExpr(final NegExpr expr) {
357: if (expr.expr == from) {
358: expr.expr = (Expr) to;
359: ((Expr) to).setParent(expr);
360: } else {
361: expr.visitChildren(this );
362: }
363: }
364:
365: public void visitNewArrayExpr(final NewArrayExpr expr) {
366: if (expr.size == from) {
367: expr.size = (Expr) to;
368: ((Expr) to).setParent(expr);
369: } else {
370: expr.visitChildren(this );
371: }
372: }
373:
374: public void visitNewExpr(final NewExpr expr) {
375: expr.visitChildren(this );
376: }
377:
378: public void visitNewMultiArrayExpr(final NewMultiArrayExpr expr) {
379: for (int i = 0; i < expr.dimensions.length; i++) {
380: if (expr.dimensions[i] == from) {
381: expr.dimensions[i] = (Expr) to;
382: ((Expr) to).setParent(expr);
383: return;
384: }
385: }
386:
387: expr.visitChildren(this );
388: }
389:
390: public void visitIfZeroStmt(final IfZeroStmt stmt) {
391: if (stmt.expr == from) {
392: stmt.expr = (Expr) to;
393: ((Expr) to).setParent(stmt);
394: } else {
395: stmt.visitChildren(this );
396: }
397: }
398:
399: public void visitIfCmpStmt(final IfCmpStmt stmt) {
400: if (stmt.left == from) {
401: stmt.left = (Expr) to;
402: ((Expr) to).setParent(stmt);
403: } else if (stmt.right == from) {
404: stmt.right = (Expr) to;
405: ((Expr) to).setParent(stmt);
406: } else {
407: stmt.visitChildren(this );
408: }
409: }
410:
411: public void visitReturnAddressExpr(final ReturnAddressExpr expr) {
412: expr.visitChildren(this );
413: }
414:
415: public void visitShiftExpr(final ShiftExpr expr) {
416: if (expr.expr == from) {
417: expr.expr = (Expr) to;
418: ((Expr) to).setParent(expr);
419: } else if (expr.bits == from) {
420: expr.bits = (Expr) to;
421: ((Expr) to).setParent(expr);
422: } else {
423: expr.visitChildren(this );
424: }
425: }
426:
427: public void visitZeroCheckExpr(final ZeroCheckExpr expr) {
428: if (expr.expr == from) {
429: expr.expr = (Expr) to;
430: ((Expr) to).setParent(expr);
431: } else {
432: expr.visitChildren(this );
433: }
434: }
435:
436: public void visitRCExpr(final RCExpr expr) {
437: if (expr.expr == from) {
438: expr.expr = (Expr) to;
439: ((Expr) to).setParent(expr);
440: } else {
441: expr.visitChildren(this );
442: }
443: }
444:
445: public void visitUCExpr(final UCExpr expr) {
446: if (expr.expr == from) {
447: expr.expr = (Expr) to;
448: ((Expr) to).setParent(expr);
449: } else {
450: expr.visitChildren(this );
451: }
452: }
453:
454: public void visitStackExpr(final StackExpr expr) {
455: expr.visitChildren(this );
456: }
457:
458: public void visitStaticFieldExpr(final StaticFieldExpr expr) {
459: expr.visitChildren(this);
460: }
461: }
|