001 /*
002 * Copyright 2001-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package com.sun.tools.javac.tree;
027
028 import com.sun.tools.javac.util.*;
029 import com.sun.tools.javac.tree.JCTree.*;
030
031 /** A subclass of Tree.Visitor, this class defines
032 * a general tree scanner pattern. Translation proceeds recursively in
033 * left-to-right order down a tree. There is one visitor method in this class
034 * for every possible kind of tree node. To obtain a specific
035 * scanner, it suffices to override those visitor methods which
036 * do some interesting work. The scanner class itself takes care of all
037 * navigational aspects.
038 *
039 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
040 * you write code that depends on this, you do so at your own risk.
041 * This code and its internal interfaces are subject to change or
042 * deletion without notice.</b>
043 *
044 * TreeScanners do not descend into brackets.
045 */
046 public class TreeScanner extends Visitor {
047
048 /** Visitor method: Scan a single node.
049 */
050 public void scan(JCTree tree) {
051 if(tree!=null) tree.accept(this);
052 }
053
054 /** Visitor method: scan a list of nodes.
055 */
056 public void scan(List<? extends JCTree> trees) {
057 if (trees != null)
058 for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail)
059 scan(l.head);
060 }
061
062
063 /* ***************************************************************************
064 * Visitor methods
065 ****************************************************************************/
066
067 public void visitTopLevel(JCCompilationUnit tree) {
068 scan(tree.packageAnnotations);
069 scan(tree.pid);
070 scan(tree.defs);
071 }
072
073 public void visitImport(JCImport tree) {
074 scan(tree.qualid);
075 }
076
077 public void visitClassDef(JCClassDecl tree) {
078 scan(tree.mods);
079 scan(tree.typarams);
080 scan(tree.extending);
081 scan(tree.implementing);
082 scan(tree.defs);
083 }
084
085 public void visitMethodDef(JCMethodDecl tree) {
086 scan(tree.mods);
087 scan(tree.restype);
088 scan(tree.typarams);
089 scan(tree.params);
090 scan(tree.thrown);
091 scan(tree.defaultValue);
092 scan(tree.body);
093 }
094
095 public void visitVarDef(JCVariableDecl tree) {
096 scan(tree.mods);
097 scan(tree.vartype);
098 scan(tree.init);
099 }
100
101 public void visitSkip(JCSkip tree) {
102 }
103
104 public void visitBlock(JCBlock tree) {
105 scan(tree.stats);
106 }
107
108 // mgr: staging additions
109 public void visitBracketExpr(JCBracketExpr that) {
110 // TreeScanner does a no-op for brackets.
111 }
112
113 public void visitDoLoop(JCDoWhileLoop tree) {
114 scan(tree.body);
115 scan(tree.cond);
116 }
117
118 public void visitWhileLoop(JCWhileLoop tree) {
119 scan(tree.cond);
120 scan(tree.body);
121 }
122
123 public void visitForLoop(JCForLoop tree) {
124 scan(tree.init);
125 scan(tree.cond);
126 scan(tree.step);
127 scan(tree.body);
128 }
129
130 public void visitForeachLoop(JCEnhancedForLoop tree) {
131 scan(tree.var);
132 scan(tree.expr);
133 scan(tree.body);
134 }
135
136 public void visitLabelled(JCLabeledStatement tree) {
137 scan(tree.body);
138 }
139
140 public void visitSwitch(JCSwitch tree) {
141 scan(tree.selector);
142 scan(tree.cases);
143 }
144
145 public void visitCase(JCCase tree) {
146 scan(tree.pat);
147 scan(tree.stats);
148 }
149
150 public void visitSynchronized(JCSynchronized tree) {
151 scan(tree.lock);
152 scan(tree.body);
153 }
154
155 public void visitTry(JCTry tree) {
156 scan(tree.body);
157 scan(tree.catchers);
158 scan(tree.finalizer);
159 }
160
161 public void visitCatch(JCCatch tree) {
162 scan(tree.param);
163 scan(tree.body);
164 }
165
166 public void visitConditional(JCConditional tree) {
167 scan(tree.cond);
168 scan(tree.truepart);
169 scan(tree.falsepart);
170 }
171
172 public void visitIf(JCIf tree) {
173 scan(tree.cond);
174 scan(tree.thenpart);
175 scan(tree.elsepart);
176 }
177
178 public void visitExec(JCExpressionStatement tree) {
179 scan(tree.expr);
180 }
181
182 public void visitBreak(JCBreak tree) {
183 }
184
185 public void visitContinue(JCContinue tree) {
186 }
187
188 public void visitReturn(JCReturn tree) {
189 scan(tree.expr);
190 }
191
192 public void visitThrow(JCThrow tree) {
193 scan(tree.expr);
194 }
195
196 public void visitAssert(JCAssert tree) {
197 scan(tree.cond);
198 scan(tree.detail);
199 }
200
201 public void visitApply(JCMethodInvocation tree) {
202 scan(tree.meth);
203 scan(tree.args);
204 }
205
206 public void visitNewClass(JCNewClass tree) {
207 scan(tree.encl);
208 scan(tree.clazz);
209 scan(tree.args);
210 scan(tree.def);
211 }
212
213 public void visitNewArray(JCNewArray tree) {
214 scan(tree.elemtype);
215 scan(tree.dims);
216 scan(tree.elems);
217 }
218
219 public void visitParens(JCParens tree) {
220 scan(tree.expr);
221 }
222
223 public void visitAssign(JCAssign tree) {
224 scan(tree.lhs);
225 scan(tree.rhs);
226 }
227
228 public void visitAssignop(JCAssignOp tree) {
229 scan(tree.lhs);
230 scan(tree.rhs);
231 }
232
233 public void visitUnary(JCUnary tree) {
234 scan(tree.arg);
235 }
236
237 public void visitBinary(JCBinary tree) {
238 scan(tree.lhs);
239 scan(tree.rhs);
240 }
241
242 public void visitTypeCast(JCTypeCast tree) {
243 scan(tree.clazz);
244 scan(tree.expr);
245 }
246
247 public void visitTypeTest(JCInstanceOf tree) {
248 scan(tree.expr);
249 scan(tree.clazz);
250 }
251
252 public void visitIndexed(JCArrayAccess tree) {
253 scan(tree.indexed);
254 scan(tree.index);
255 }
256
257 public void visitSelect(JCFieldAccess tree) {
258 scan(tree.selected);
259 }
260
261 public void visitIdent(JCIdent tree) {
262 }
263
264 public void visitLiteral(JCLiteral tree) {
265 }
266
267 public void visitTypeIdent(JCPrimitiveTypeTree tree) {
268 }
269
270 public void visitTypeArray(JCArrayTypeTree tree) {
271 scan(tree.elemtype);
272 }
273
274 public void visitTypeApply(JCTypeApply tree) {
275 scan(tree.clazz);
276 scan(tree.arguments);
277 }
278
279 public void visitTypeParameter(JCTypeParameter tree) {
280 scan(tree.bounds);
281 }
282
283 @Override
284 public void visitWildcard(JCWildcard tree) {
285 scan(tree.kind);
286 if (tree.inner != null)
287 scan(tree.inner);
288 }
289
290 @Override
291 public void visitTypeBoundKind(TypeBoundKind that) {
292 }
293
294 public void visitModifiers(JCModifiers tree) {
295 scan(tree.annotations);
296 }
297
298 public void visitAnnotation(JCAnnotation tree) {
299 scan(tree.annotationType);
300 scan(tree.args);
301 }
302
303 public void visitErroneous(JCErroneous tree) {
304 }
305
306 public void visitLetExpr(LetExpr tree) {
307 scan(tree.defs);
308 scan(tree.expr);
309 }
310
311 public void visitTree(JCTree tree) {
312 assert false;
313 }
314 }