author | jjg |
Mon, 06 Sep 2010 12:55:09 -0700 | |
changeset 6590 | f745e683da2c |
parent 6148 | 3a8158299c51 |
child 7074 | 0183c3f9614e |
permissions | -rw-r--r-- |
10 | 1 |
/* |
5520 | 2 |
* Copyright (c) 1999, 2006, Oracle and/or its affiliates. All rights reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.tree; |
|
27 |
||
28 |
import com.sun.tools.javac.util.*; |
|
29 |
import com.sun.tools.javac.tree.JCTree.*; |
|
30 |
||
31 |
/** A subclass of Tree.Visitor, this class defines |
|
32 |
* a general tree translator pattern. Translation proceeds recursively in |
|
33 |
* left-to-right order down a tree, constructing translated nodes by |
|
34 |
* overwriting existing ones. There is one visitor method in this class |
|
35 |
* for every possible kind of tree node. To obtain a specific |
|
36 |
* translator, it suffices to override those visitor methods which |
|
37 |
* do some interesting work. The translator class itself takes care of all |
|
38 |
* navigational aspects. |
|
39 |
* |
|
5847
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
40 |
* <p><b>This is NOT part of any supported API. |
1908176fd6e3
6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents:
5520
diff
changeset
|
41 |
* If you write code that depends on this, you do so at your own risk. |
10 | 42 |
* This code and its internal interfaces are subject to change or |
43 |
* deletion without notice.</b> |
|
44 |
*/ |
|
45 |
public class TreeTranslator extends JCTree.Visitor { |
|
46 |
||
47 |
/** Visitor result field: a tree |
|
48 |
*/ |
|
49 |
protected JCTree result; |
|
50 |
||
51 |
/** Visitor method: Translate a single node. |
|
52 |
*/ |
|
53 |
@SuppressWarnings("unchecked") |
|
54 |
public <T extends JCTree> T translate(T tree) { |
|
55 |
if (tree == null) { |
|
56 |
return null; |
|
57 |
} else { |
|
58 |
tree.accept(this); |
|
59 |
JCTree result = this.result; |
|
60 |
this.result = null; |
|
61 |
return (T)result; // XXX cast |
|
62 |
} |
|
63 |
} |
|
64 |
||
65 |
/** Visitor method: translate a list of nodes. |
|
66 |
*/ |
|
67 |
public <T extends JCTree> List<T> translate(List<T> trees) { |
|
68 |
if (trees == null) return null; |
|
69 |
for (List<T> l = trees; l.nonEmpty(); l = l.tail) |
|
70 |
l.head = translate(l.head); |
|
71 |
return trees; |
|
72 |
} |
|
73 |
||
74 |
/** Visitor method: translate a list of variable definitions. |
|
75 |
*/ |
|
76 |
public List<JCVariableDecl> translateVarDefs(List<JCVariableDecl> trees) { |
|
77 |
for (List<JCVariableDecl> l = trees; l.nonEmpty(); l = l.tail) |
|
78 |
l.head = translate(l.head); |
|
79 |
return trees; |
|
80 |
} |
|
81 |
||
82 |
/** Visitor method: translate a list of type parameters. |
|
83 |
*/ |
|
84 |
public List<JCTypeParameter> translateTypeParams(List<JCTypeParameter> trees) { |
|
85 |
for (List<JCTypeParameter> l = trees; l.nonEmpty(); l = l.tail) |
|
86 |
l.head = translate(l.head); |
|
87 |
return trees; |
|
88 |
} |
|
89 |
||
90 |
/** Visitor method: translate a list of case parts of switch statements. |
|
91 |
*/ |
|
92 |
public List<JCCase> translateCases(List<JCCase> trees) { |
|
93 |
for (List<JCCase> l = trees; l.nonEmpty(); l = l.tail) |
|
94 |
l.head = translate(l.head); |
|
95 |
return trees; |
|
96 |
} |
|
97 |
||
98 |
/** Visitor method: translate a list of catch clauses in try statements. |
|
99 |
*/ |
|
100 |
public List<JCCatch> translateCatchers(List<JCCatch> trees) { |
|
101 |
for (List<JCCatch> l = trees; l.nonEmpty(); l = l.tail) |
|
102 |
l.head = translate(l.head); |
|
103 |
return trees; |
|
104 |
} |
|
105 |
||
106 |
/** Visitor method: translate a list of catch clauses in try statements. |
|
107 |
*/ |
|
108 |
public List<JCAnnotation> translateAnnotations(List<JCAnnotation> trees) { |
|
109 |
for (List<JCAnnotation> l = trees; l.nonEmpty(); l = l.tail) |
|
110 |
l.head = translate(l.head); |
|
111 |
return trees; |
|
112 |
} |
|
113 |
||
114 |
/* *************************************************************************** |
|
115 |
* Visitor methods |
|
116 |
****************************************************************************/ |
|
117 |
||
118 |
public void visitTopLevel(JCCompilationUnit tree) { |
|
119 |
tree.pid = translate(tree.pid); |
|
120 |
tree.defs = translate(tree.defs); |
|
121 |
result = tree; |
|
122 |
} |
|
123 |
||
124 |
public void visitImport(JCImport tree) { |
|
125 |
tree.qualid = translate(tree.qualid); |
|
126 |
result = tree; |
|
127 |
} |
|
128 |
||
129 |
public void visitClassDef(JCClassDecl tree) { |
|
130 |
tree.mods = translate(tree.mods); |
|
131 |
tree.typarams = translateTypeParams(tree.typarams); |
|
132 |
tree.extending = translate(tree.extending); |
|
133 |
tree.implementing = translate(tree.implementing); |
|
134 |
tree.defs = translate(tree.defs); |
|
135 |
result = tree; |
|
136 |
} |
|
137 |
||
138 |
public void visitMethodDef(JCMethodDecl tree) { |
|
139 |
tree.mods = translate(tree.mods); |
|
140 |
tree.restype = translate(tree.restype); |
|
141 |
tree.typarams = translateTypeParams(tree.typarams); |
|
142 |
tree.params = translateVarDefs(tree.params); |
|
143 |
tree.thrown = translate(tree.thrown); |
|
144 |
tree.body = translate(tree.body); |
|
145 |
result = tree; |
|
146 |
} |
|
147 |
||
148 |
public void visitVarDef(JCVariableDecl tree) { |
|
149 |
tree.mods = translate(tree.mods); |
|
150 |
tree.vartype = translate(tree.vartype); |
|
151 |
tree.init = translate(tree.init); |
|
152 |
result = tree; |
|
153 |
} |
|
154 |
||
155 |
public void visitSkip(JCSkip tree) { |
|
156 |
result = tree; |
|
157 |
} |
|
158 |
||
159 |
public void visitBlock(JCBlock tree) { |
|
160 |
tree.stats = translate(tree.stats); |
|
161 |
result = tree; |
|
162 |
} |
|
163 |
||
164 |
public void visitDoLoop(JCDoWhileLoop tree) { |
|
165 |
tree.body = translate(tree.body); |
|
166 |
tree.cond = translate(tree.cond); |
|
167 |
result = tree; |
|
168 |
} |
|
169 |
||
170 |
public void visitWhileLoop(JCWhileLoop tree) { |
|
171 |
tree.cond = translate(tree.cond); |
|
172 |
tree.body = translate(tree.body); |
|
173 |
result = tree; |
|
174 |
} |
|
175 |
||
176 |
public void visitForLoop(JCForLoop tree) { |
|
177 |
tree.init = translate(tree.init); |
|
178 |
tree.cond = translate(tree.cond); |
|
179 |
tree.step = translate(tree.step); |
|
180 |
tree.body = translate(tree.body); |
|
181 |
result = tree; |
|
182 |
} |
|
183 |
||
184 |
public void visitForeachLoop(JCEnhancedForLoop tree) { |
|
185 |
tree.var = translate(tree.var); |
|
186 |
tree.expr = translate(tree.expr); |
|
187 |
tree.body = translate(tree.body); |
|
188 |
result = tree; |
|
189 |
} |
|
190 |
||
191 |
public void visitLabelled(JCLabeledStatement tree) { |
|
192 |
tree.body = translate(tree.body); |
|
193 |
result = tree; |
|
194 |
} |
|
195 |
||
196 |
public void visitSwitch(JCSwitch tree) { |
|
197 |
tree.selector = translate(tree.selector); |
|
198 |
tree.cases = translateCases(tree.cases); |
|
199 |
result = tree; |
|
200 |
} |
|
201 |
||
202 |
public void visitCase(JCCase tree) { |
|
203 |
tree.pat = translate(tree.pat); |
|
204 |
tree.stats = translate(tree.stats); |
|
205 |
result = tree; |
|
206 |
} |
|
207 |
||
208 |
public void visitSynchronized(JCSynchronized tree) { |
|
209 |
tree.lock = translate(tree.lock); |
|
210 |
tree.body = translate(tree.body); |
|
211 |
result = tree; |
|
212 |
} |
|
213 |
||
214 |
public void visitTry(JCTry tree) { |
|
6148
3a8158299c51
6911256: Project Coin: Support Automatic Resource Management (ARM) blocks in the compiler
darcy
parents:
5847
diff
changeset
|
215 |
tree.resources = translate(tree.resources); |
10 | 216 |
tree.body = translate(tree.body); |
217 |
tree.catchers = translateCatchers(tree.catchers); |
|
218 |
tree.finalizer = translate(tree.finalizer); |
|
219 |
result = tree; |
|
220 |
} |
|
221 |
||
222 |
public void visitCatch(JCCatch tree) { |
|
223 |
tree.param = translate(tree.param); |
|
224 |
tree.body = translate(tree.body); |
|
225 |
result = tree; |
|
226 |
} |
|
227 |
||
228 |
public void visitConditional(JCConditional tree) { |
|
229 |
tree.cond = translate(tree.cond); |
|
230 |
tree.truepart = translate(tree.truepart); |
|
231 |
tree.falsepart = translate(tree.falsepart); |
|
232 |
result = tree; |
|
233 |
} |
|
234 |
||
235 |
public void visitIf(JCIf tree) { |
|
236 |
tree.cond = translate(tree.cond); |
|
237 |
tree.thenpart = translate(tree.thenpart); |
|
238 |
tree.elsepart = translate(tree.elsepart); |
|
239 |
result = tree; |
|
240 |
} |
|
241 |
||
242 |
public void visitExec(JCExpressionStatement tree) { |
|
243 |
tree.expr = translate(tree.expr); |
|
244 |
result = tree; |
|
245 |
} |
|
246 |
||
247 |
public void visitBreak(JCBreak tree) { |
|
248 |
result = tree; |
|
249 |
} |
|
250 |
||
251 |
public void visitContinue(JCContinue tree) { |
|
252 |
result = tree; |
|
253 |
} |
|
254 |
||
255 |
public void visitReturn(JCReturn tree) { |
|
256 |
tree.expr = translate(tree.expr); |
|
257 |
result = tree; |
|
258 |
} |
|
259 |
||
260 |
public void visitThrow(JCThrow tree) { |
|
261 |
tree.expr = translate(tree.expr); |
|
262 |
result = tree; |
|
263 |
} |
|
264 |
||
265 |
public void visitAssert(JCAssert tree) { |
|
266 |
tree.cond = translate(tree.cond); |
|
267 |
tree.detail = translate(tree.detail); |
|
268 |
result = tree; |
|
269 |
} |
|
270 |
||
271 |
public void visitApply(JCMethodInvocation tree) { |
|
272 |
tree.meth = translate(tree.meth); |
|
273 |
tree.args = translate(tree.args); |
|
274 |
result = tree; |
|
275 |
} |
|
276 |
||
277 |
public void visitNewClass(JCNewClass tree) { |
|
278 |
tree.encl = translate(tree.encl); |
|
279 |
tree.clazz = translate(tree.clazz); |
|
280 |
tree.args = translate(tree.args); |
|
281 |
tree.def = translate(tree.def); |
|
282 |
result = tree; |
|
283 |
} |
|
284 |
||
285 |
public void visitNewArray(JCNewArray tree) { |
|
3149 | 286 |
tree.annotations = translate(tree.annotations); |
287 |
List<List<JCTypeAnnotation>> dimAnnos = List.nil(); |
|
288 |
for (List<JCTypeAnnotation> origDimAnnos : tree.dimAnnotations) |
|
289 |
dimAnnos = dimAnnos.append(translate(origDimAnnos)); |
|
290 |
tree.dimAnnotations = dimAnnos; |
|
10 | 291 |
tree.elemtype = translate(tree.elemtype); |
292 |
tree.dims = translate(tree.dims); |
|
293 |
tree.elems = translate(tree.elems); |
|
294 |
result = tree; |
|
295 |
} |
|
296 |
||
297 |
public void visitParens(JCParens tree) { |
|
298 |
tree.expr = translate(tree.expr); |
|
299 |
result = tree; |
|
300 |
} |
|
301 |
||
302 |
public void visitAssign(JCAssign tree) { |
|
303 |
tree.lhs = translate(tree.lhs); |
|
304 |
tree.rhs = translate(tree.rhs); |
|
305 |
result = tree; |
|
306 |
} |
|
307 |
||
308 |
public void visitAssignop(JCAssignOp tree) { |
|
309 |
tree.lhs = translate(tree.lhs); |
|
310 |
tree.rhs = translate(tree.rhs); |
|
311 |
result = tree; |
|
312 |
} |
|
313 |
||
314 |
public void visitUnary(JCUnary tree) { |
|
315 |
tree.arg = translate(tree.arg); |
|
316 |
result = tree; |
|
317 |
} |
|
318 |
||
319 |
public void visitBinary(JCBinary tree) { |
|
320 |
tree.lhs = translate(tree.lhs); |
|
321 |
tree.rhs = translate(tree.rhs); |
|
322 |
result = tree; |
|
323 |
} |
|
324 |
||
325 |
public void visitTypeCast(JCTypeCast tree) { |
|
326 |
tree.clazz = translate(tree.clazz); |
|
327 |
tree.expr = translate(tree.expr); |
|
328 |
result = tree; |
|
329 |
} |
|
330 |
||
331 |
public void visitTypeTest(JCInstanceOf tree) { |
|
332 |
tree.expr = translate(tree.expr); |
|
333 |
tree.clazz = translate(tree.clazz); |
|
334 |
result = tree; |
|
335 |
} |
|
336 |
||
337 |
public void visitIndexed(JCArrayAccess tree) { |
|
338 |
tree.indexed = translate(tree.indexed); |
|
339 |
tree.index = translate(tree.index); |
|
340 |
result = tree; |
|
341 |
} |
|
342 |
||
343 |
public void visitSelect(JCFieldAccess tree) { |
|
344 |
tree.selected = translate(tree.selected); |
|
345 |
result = tree; |
|
346 |
} |
|
347 |
||
348 |
public void visitIdent(JCIdent tree) { |
|
349 |
result = tree; |
|
350 |
} |
|
351 |
||
352 |
public void visitLiteral(JCLiteral tree) { |
|
353 |
result = tree; |
|
354 |
} |
|
355 |
||
356 |
public void visitTypeIdent(JCPrimitiveTypeTree tree) { |
|
357 |
result = tree; |
|
358 |
} |
|
359 |
||
360 |
public void visitTypeArray(JCArrayTypeTree tree) { |
|
361 |
tree.elemtype = translate(tree.elemtype); |
|
362 |
result = tree; |
|
363 |
} |
|
364 |
||
365 |
public void visitTypeApply(JCTypeApply tree) { |
|
366 |
tree.clazz = translate(tree.clazz); |
|
367 |
tree.arguments = translate(tree.arguments); |
|
368 |
result = tree; |
|
369 |
} |
|
370 |
||
5492
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
3149
diff
changeset
|
371 |
public void visitTypeDisjoint(JCTypeDisjoint tree) { |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
3149
diff
changeset
|
372 |
tree.components = translate(tree.components); |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
3149
diff
changeset
|
373 |
result = tree; |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
3149
diff
changeset
|
374 |
} |
515e4b33b335
6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents:
3149
diff
changeset
|
375 |
|
10 | 376 |
public void visitTypeParameter(JCTypeParameter tree) { |
3149 | 377 |
tree.annotations = translate(tree.annotations); |
10 | 378 |
tree.bounds = translate(tree.bounds); |
379 |
result = tree; |
|
380 |
} |
|
381 |
||
382 |
@Override |
|
383 |
public void visitWildcard(JCWildcard tree) { |
|
384 |
tree.kind = translate(tree.kind); |
|
385 |
tree.inner = translate(tree.inner); |
|
386 |
result = tree; |
|
387 |
} |
|
388 |
||
389 |
@Override |
|
390 |
public void visitTypeBoundKind(TypeBoundKind tree) { |
|
391 |
result = tree; |
|
392 |
} |
|
393 |
||
394 |
public void visitErroneous(JCErroneous tree) { |
|
395 |
result = tree; |
|
396 |
} |
|
397 |
||
398 |
public void visitLetExpr(LetExpr tree) { |
|
399 |
tree.defs = translateVarDefs(tree.defs); |
|
400 |
tree.expr = translate(tree.expr); |
|
401 |
result = tree; |
|
402 |
} |
|
403 |
||
404 |
public void visitModifiers(JCModifiers tree) { |
|
405 |
tree.annotations = translateAnnotations(tree.annotations); |
|
406 |
result = tree; |
|
407 |
} |
|
408 |
||
409 |
public void visitAnnotation(JCAnnotation tree) { |
|
410 |
tree.annotationType = translate(tree.annotationType); |
|
411 |
tree.args = translate(tree.args); |
|
412 |
result = tree; |
|
413 |
} |
|
414 |
||
3149 | 415 |
public void visitAnnotatedType(JCAnnotatedType tree) { |
416 |
tree.annotations = translate(tree.annotations); |
|
417 |
tree.underlyingType = translate(tree.underlyingType); |
|
418 |
result = tree; |
|
419 |
} |
|
420 |
||
10 | 421 |
public void visitTree(JCTree tree) { |
422 |
throw new AssertionError(tree); |
|
423 |
} |
|
424 |
} |