001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.dev.jjs.ast;
017:
018: import com.google.gwt.dev.jjs.InternalCompilerException;
019: import com.google.gwt.dev.jjs.ast.js.JClassSeed;
020: import com.google.gwt.dev.jjs.ast.js.JMultiExpression;
021: import com.google.gwt.dev.jjs.ast.js.JsniFieldRef;
022: import com.google.gwt.dev.jjs.ast.js.JsniMethodBody;
023: import com.google.gwt.dev.jjs.ast.js.JsniMethodRef;
024: import com.google.gwt.dev.jjs.ast.js.JsonArray;
025: import com.google.gwt.dev.jjs.ast.js.JsonObject;
026: import com.google.gwt.dev.jjs.ast.js.JsonObject.JsonPropInit;
027:
028: import java.util.Iterator;
029: import java.util.List;
030:
031: /**
032: * A visitor for iterating through an AST.
033: */
034: public class JVisitor {
035:
036: protected static final Context UNMODIFIABLE_CONTEXT = new Context() {
037:
038: public boolean canInsert() {
039: return false;
040: }
041:
042: public boolean canRemove() {
043: return false;
044: }
045:
046: public void insertAfter(JNode node) {
047: throw new UnsupportedOperationException();
048: }
049:
050: public void insertBefore(JNode node) {
051: throw new UnsupportedOperationException();
052: }
053:
054: public void removeMe() {
055: throw new UnsupportedOperationException();
056: }
057:
058: public void replaceMe(JNode node) {
059: throw new UnsupportedOperationException();
060: }
061:
062: };
063:
064: public final JExpression accept(JExpression node) {
065: return (JExpression) doAccept(node);
066: }
067:
068: public final JNode accept(JNode node) {
069: return doAccept(node);
070: }
071:
072: public final JStatement accept(JStatement node) {
073: return (JStatement) doAccept(node);
074: }
075:
076: public final void accept(List list) {
077: doAccept(list);
078: }
079:
080: public final void acceptWithInsertRemove(List list) {
081: doAcceptWithInsertRemove(list);
082: }
083:
084: public boolean didChange() {
085: throw new UnsupportedOperationException();
086: }
087:
088: public void endVisit(JAbsentArrayDimension x, Context ctx) {
089: }
090:
091: public void endVisit(JArrayRef x, Context ctx) {
092: }
093:
094: public void endVisit(JArrayType x, Context ctx) {
095: }
096:
097: public void endVisit(JAssertStatement x, Context ctx) {
098: }
099:
100: public void endVisit(JBinaryOperation x, Context ctx) {
101: }
102:
103: public void endVisit(JBlock x, Context ctx) {
104: }
105:
106: public void endVisit(JBooleanLiteral x, Context ctx) {
107: }
108:
109: public void endVisit(JBreakStatement x, Context ctx) {
110: }
111:
112: public void endVisit(JCaseStatement x, Context ctx) {
113: }
114:
115: public void endVisit(JCastOperation x, Context ctx) {
116: }
117:
118: public void endVisit(JCharLiteral x, Context ctx) {
119: }
120:
121: public void endVisit(JClassLiteral x, Context ctx) {
122: }
123:
124: public void endVisit(JClassSeed x, Context ctx) {
125: }
126:
127: public void endVisit(JClassType x, Context ctx) {
128: }
129:
130: public void endVisit(JConditional x, Context ctx) {
131: }
132:
133: public void endVisit(JContinueStatement x, Context ctx) {
134: }
135:
136: public void endVisit(JDoStatement x, Context ctx) {
137: }
138:
139: public void endVisit(JDoubleLiteral x, Context ctx) {
140: }
141:
142: public void endVisit(JExpressionStatement x, Context ctx) {
143: }
144:
145: public void endVisit(JField x, Context ctx) {
146: }
147:
148: public void endVisit(JFieldRef x, Context ctx) {
149: }
150:
151: public void endVisit(JFloatLiteral x, Context ctx) {
152: }
153:
154: public void endVisit(JForStatement x, Context ctx) {
155: }
156:
157: public void endVisit(JIfStatement x, Context ctx) {
158: }
159:
160: public void endVisit(JInstanceOf x, Context ctx) {
161: }
162:
163: public void endVisit(JInterfaceType x, Context ctx) {
164: }
165:
166: public void endVisit(JIntLiteral x, Context ctx) {
167: }
168:
169: public void endVisit(JLabel x, Context ctx) {
170: }
171:
172: public void endVisit(JLabeledStatement x, Context ctx) {
173: }
174:
175: public void endVisit(JLocal x, Context ctx) {
176: }
177:
178: public void endVisit(JLocalDeclarationStatement x, Context ctx) {
179: }
180:
181: public void endVisit(JLocalRef x, Context ctx) {
182: }
183:
184: public void endVisit(JLongLiteral x, Context ctx) {
185: }
186:
187: public void endVisit(JMethod x, Context ctx) {
188: }
189:
190: public void endVisit(JMethodBody x, Context ctx) {
191: }
192:
193: public void endVisit(JMethodCall x, Context ctx) {
194: }
195:
196: public void endVisit(JMultiExpression x, Context ctx) {
197: }
198:
199: public void endVisit(JNewArray x, Context ctx) {
200: }
201:
202: public void endVisit(JNewInstance x, Context ctx) {
203: }
204:
205: public void endVisit(JNullLiteral x, Context ctx) {
206: }
207:
208: public void endVisit(JNullType x, Context ctx) {
209: }
210:
211: public void endVisit(JParameter x, Context ctx) {
212: }
213:
214: public void endVisit(JParameterRef x, Context ctx) {
215: }
216:
217: public void endVisit(JPostfixOperation x, Context ctx) {
218: }
219:
220: public void endVisit(JPrefixOperation x, Context ctx) {
221: }
222:
223: public void endVisit(JPrimitiveType x, Context ctx) {
224: }
225:
226: public void endVisit(JProgram x, Context ctx) {
227: }
228:
229: public void endVisit(JReturnStatement x, Context ctx) {
230: }
231:
232: public void endVisit(JsniFieldRef x, Context ctx) {
233: }
234:
235: public void endVisit(JsniMethodBody x, Context ctx) {
236: }
237:
238: public void endVisit(JsniMethodRef x, Context ctx) {
239: }
240:
241: public void endVisit(JsonArray x, Context ctx) {
242: }
243:
244: public void endVisit(JsonObject x, Context ctx) {
245: }
246:
247: public void endVisit(JsonPropInit x, Context ctx) {
248: }
249:
250: public void endVisit(JStringLiteral x, Context ctx) {
251: }
252:
253: public void endVisit(JSwitchStatement x, Context ctx) {
254: }
255:
256: public void endVisit(JThisRef x, Context ctx) {
257: }
258:
259: public void endVisit(JThrowStatement x, Context ctx) {
260: }
261:
262: public void endVisit(JTryStatement x, Context ctx) {
263: }
264:
265: public void endVisit(JWhileStatement x, Context ctx) {
266: }
267:
268: public boolean visit(JAbsentArrayDimension x, Context ctx) {
269: return true;
270: }
271:
272: public boolean visit(JArrayRef x, Context ctx) {
273: return true;
274: }
275:
276: public boolean visit(JArrayType x, Context ctx) {
277: return true;
278: }
279:
280: public boolean visit(JAssertStatement x, Context ctx) {
281: return true;
282: }
283:
284: public boolean visit(JBinaryOperation x, Context ctx) {
285: return true;
286: }
287:
288: public boolean visit(JBlock x, Context ctx) {
289: return true;
290: }
291:
292: public boolean visit(JBooleanLiteral x, Context ctx) {
293: return true;
294: }
295:
296: public boolean visit(JBreakStatement x, Context ctx) {
297: return true;
298: }
299:
300: public boolean visit(JCaseStatement x, Context ctx) {
301: return true;
302: }
303:
304: public boolean visit(JCastOperation x, Context ctx) {
305: return true;
306: }
307:
308: public boolean visit(JCharLiteral x, Context ctx) {
309: return true;
310: }
311:
312: public boolean visit(JClassLiteral x, Context ctx) {
313: return true;
314: }
315:
316: public boolean visit(JClassSeed x, Context ctx) {
317: return true;
318: }
319:
320: public boolean visit(JClassType x, Context ctx) {
321: return true;
322: }
323:
324: public boolean visit(JConditional x, Context ctx) {
325: return true;
326: }
327:
328: public boolean visit(JContinueStatement x, Context ctx) {
329: return true;
330: }
331:
332: public boolean visit(JDoStatement x, Context ctx) {
333: return true;
334: }
335:
336: public boolean visit(JDoubleLiteral x, Context ctx) {
337: return true;
338: }
339:
340: public boolean visit(JExpressionStatement x, Context ctx) {
341: return true;
342: }
343:
344: public boolean visit(JField x, Context ctx) {
345: return true;
346: }
347:
348: public boolean visit(JFieldRef x, Context ctx) {
349: return true;
350: }
351:
352: public boolean visit(JFloatLiteral x, Context ctx) {
353: return true;
354: }
355:
356: public boolean visit(JForStatement x, Context ctx) {
357: return true;
358: }
359:
360: public boolean visit(JIfStatement x, Context ctx) {
361: return true;
362: }
363:
364: public boolean visit(JInstanceOf x, Context ctx) {
365: return true;
366: }
367:
368: public boolean visit(JInterfaceType x, Context ctx) {
369: return true;
370: }
371:
372: public boolean visit(JIntLiteral x, Context ctx) {
373: return true;
374: }
375:
376: public boolean visit(JLabel x, Context ctx) {
377: return true;
378: }
379:
380: public boolean visit(JLabeledStatement x, Context ctx) {
381: return true;
382: }
383:
384: public boolean visit(JLocal x, Context ctx) {
385: return true;
386: }
387:
388: public boolean visit(JLocalDeclarationStatement x, Context ctx) {
389: return true;
390: }
391:
392: public boolean visit(JLocalRef x, Context ctx) {
393: return true;
394: }
395:
396: public boolean visit(JLongLiteral x, Context ctx) {
397: return true;
398: }
399:
400: public boolean visit(JMethod x, Context ctx) {
401: return true;
402: }
403:
404: public boolean visit(JMethodBody x, Context ctx) {
405: return true;
406: }
407:
408: public boolean visit(JMethodCall x, Context ctx) {
409: return true;
410: }
411:
412: public boolean visit(JMultiExpression x, Context ctx) {
413: return true;
414: }
415:
416: public boolean visit(JNewArray x, Context ctx) {
417: return true;
418: }
419:
420: public boolean visit(JNewInstance x, Context ctx) {
421: return true;
422: }
423:
424: public boolean visit(JNullLiteral x, Context ctx) {
425: return true;
426: }
427:
428: public boolean visit(JNullType x, Context ctx) {
429: return true;
430: }
431:
432: public boolean visit(JParameter x, Context ctx) {
433: return true;
434: }
435:
436: public boolean visit(JParameterRef x, Context ctx) {
437: return true;
438: }
439:
440: public boolean visit(JPostfixOperation x, Context ctx) {
441: return true;
442: }
443:
444: public boolean visit(JPrefixOperation x, Context ctx) {
445: return true;
446: }
447:
448: public boolean visit(JPrimitiveType x, Context ctx) {
449: return true;
450: }
451:
452: public boolean visit(JProgram x, Context ctx) {
453: return true;
454: }
455:
456: public boolean visit(JReturnStatement x, Context ctx) {
457: return true;
458: }
459:
460: public boolean visit(JsniFieldRef x, Context ctx) {
461: return true;
462: }
463:
464: public boolean visit(JsniMethodBody x, Context ctx) {
465: return true;
466: }
467:
468: public boolean visit(JsniMethodRef x, Context ctx) {
469: return true;
470: }
471:
472: public boolean visit(JsonArray x, Context ctx) {
473: return true;
474: }
475:
476: public boolean visit(JsonObject x, Context ctx) {
477: return true;
478: }
479:
480: public boolean visit(JsonPropInit x, Context ctx) {
481: return true;
482: }
483:
484: public boolean visit(JStringLiteral x, Context ctx) {
485: return true;
486: }
487:
488: public boolean visit(JSwitchStatement x, Context ctx) {
489: return true;
490: }
491:
492: public boolean visit(JThisRef x, Context ctx) {
493: return true;
494: }
495:
496: public boolean visit(JThrowStatement x, Context ctx) {
497: return true;
498: }
499:
500: public boolean visit(JTryStatement x, Context ctx) {
501: return true;
502: }
503:
504: public boolean visit(JWhileStatement x, Context ctx) {
505: return true;
506: }
507:
508: protected JNode doAccept(JNode node) {
509: doTraverse(node, UNMODIFIABLE_CONTEXT);
510: return node;
511: }
512:
513: protected void doAccept(List list) {
514: for (Iterator it = list.iterator(); it.hasNext();) {
515: doTraverse((JNode) it.next(), UNMODIFIABLE_CONTEXT);
516: }
517: }
518:
519: protected void doAcceptWithInsertRemove(List list) {
520: for (Iterator it = list.iterator(); it.hasNext();) {
521: doTraverse((JNode) it.next(), UNMODIFIABLE_CONTEXT);
522: }
523: }
524:
525: protected final void doTraverse(JNode node, Context ctx) {
526: try {
527: node.traverse(this , ctx);
528: } catch (Throwable e) {
529: throw translateException(node, e);
530: }
531: }
532:
533: private InternalCompilerException translateException(JNode node,
534: Throwable e) {
535: InternalCompilerException ice;
536: if (e instanceof InternalCompilerException) {
537: ice = (InternalCompilerException) e;
538: } else {
539: ice = new InternalCompilerException(
540: "Unexpected error during visit.", e);
541: }
542: ice.addNode(node);
543: return ice;
544: }
545: }
|