1 /*
  2  * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
  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
  7  * published by the Free Software Foundation.  Oracle designates this
  8  * particular file as subject to the "Classpath" exception as provided
  9  * by Oracle in the LICENSE file that accompanied this code.
 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  *
 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.
 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  *
 40  *  <p><b>This is NOT part of any supported API.
 41  *  If you write code that depends on this, you do so at your own risk.
 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 tmpResult = this.result;
 60             this.result = null;
 61             return (T)tmpResult; // 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.defs = translate(tree.defs);
120         result = tree;
121     }
122 
123     public void visitPackageDef(JCPackageDecl tree) {
124         tree.annotations = translate(tree.annotations);
125         tree.pid = translate(tree.pid);
126         result = tree;
127     }
128 
129     public void visitImport(JCImport tree) {
130         tree.qualid = translate(tree.qualid);
131         result = tree;
132     }
133 
134     public void visitClassDef(JCClassDecl tree) {
135         tree.mods = translate(tree.mods);
136         tree.typarams = translateTypeParams(tree.typarams);
137         tree.extending = translate(tree.extending);
138         tree.implementing = translate(tree.implementing);
139         tree.defs = translate(tree.defs);
140         result = tree;
141     }
142 
143     public void visitMethodDef(JCMethodDecl tree) {
144         tree.mods = translate(tree.mods);
145         tree.restype = translate(tree.restype);
146         tree.typarams = translateTypeParams(tree.typarams);
147         tree.recvparam = translate(tree.recvparam);
148         tree.params = translateVarDefs(tree.params);
149         tree.thrown = translate(tree.thrown);
150         tree.body = translate(tree.body);
151         result = tree;
152     }
153 
154     public void visitVarDef(JCVariableDecl tree) {
155         tree.mods = translate(tree.mods);
156         tree.nameexpr = translate(tree.nameexpr);
157         tree.vartype = translate(tree.vartype);
158         tree.init = translate(tree.init);
159         result = tree;
160     }
161 
162     public void visitSkip(JCSkip tree) {
163         result = tree;
164     }
165 
166     public void visitBlock(JCBlock tree) {
167         tree.stats = translate(tree.stats);
168         result = tree;
169     }
170 
171     public void visitDoLoop(JCDoWhileLoop tree) {
172         tree.body = translate(tree.body);
173         tree.cond = translate(tree.cond);
174         result = tree;
175     }
176 
177     public void visitWhileLoop(JCWhileLoop tree) {
178         tree.cond = translate(tree.cond);
179         tree.body = translate(tree.body);
180         result = tree;
181     }
182 
183     public void visitWithField(JCWithField tree) {
184         tree.field = translate(tree.field);
185         tree.value = translate(tree.value);
186         result = tree;
187     }
188 
189     public void visitForLoop(JCForLoop tree) {
190         tree.init = translate(tree.init);
191         tree.cond = translate(tree.cond);
192         tree.step = translate(tree.step);
193         tree.body = translate(tree.body);
194         result = tree;
195     }
196 
197     public void visitForeachLoop(JCEnhancedForLoop tree) {
198         tree.var = translate(tree.var);
199         tree.expr = translate(tree.expr);
200         tree.body = translate(tree.body);
201         result = tree;
202     }
203 
204     public void visitLabelled(JCLabeledStatement tree) {
205         tree.body = translate(tree.body);
206         result = tree;
207     }
208 
209     public void visitSwitch(JCSwitch tree) {
210         tree.selector = translate(tree.selector);
211         tree.cases = translateCases(tree.cases);
212         result = tree;
213     }
214 
215     public void visitCase(JCCase tree) {
216         tree.labels = translate(tree.labels);
217         tree.stats = translate(tree.stats);
218         result = tree;
219     }
220 
221     public void visitSwitchExpression(JCSwitchExpression tree) {
222         tree.selector = translate(tree.selector);
223         tree.cases = translateCases(tree.cases);
224         result = tree;
225     }
226 
227     public void visitSynchronized(JCSynchronized tree) {
228         tree.lock = translate(tree.lock);
229         tree.body = translate(tree.body);
230         result = tree;
231     }
232 
233     public void visitTry(JCTry tree) {
234         tree.resources = translate(tree.resources);
235         tree.body = translate(tree.body);
236         tree.catchers = translateCatchers(tree.catchers);
237         tree.finalizer = translate(tree.finalizer);
238         result = tree;
239     }
240 
241     public void visitCatch(JCCatch tree) {
242         tree.param = translate(tree.param);
243         tree.body = translate(tree.body);
244         result = tree;
245     }
246 
247     public void visitConditional(JCConditional tree) {
248         tree.cond = translate(tree.cond);
249         tree.truepart = translate(tree.truepart);
250         tree.falsepart = translate(tree.falsepart);
251         result = tree;
252     }
253 
254     public void visitIf(JCIf tree) {
255         tree.cond = translate(tree.cond);
256         tree.thenpart = translate(tree.thenpart);
257         tree.elsepart = translate(tree.elsepart);
258         result = tree;
259     }
260 
261     public void visitExec(JCExpressionStatement tree) {
262         tree.expr = translate(tree.expr);
263         result = tree;
264     }
265 
266     public void visitBreak(JCBreak tree) {
267         result = tree;
268     }
269 
270     public void visitYield(JCYield tree) {
271         tree.value = translate(tree.value);
272         result = tree;
273     }
274 
275     public void visitContinue(JCContinue tree) {
276         result = tree;
277     }
278 
279     public void visitReturn(JCReturn tree) {
280         tree.expr = translate(tree.expr);
281         result = tree;
282     }
283 
284     public void visitThrow(JCThrow tree) {
285         tree.expr = translate(tree.expr);
286         result = tree;
287     }
288 
289     public void visitAssert(JCAssert tree) {
290         tree.cond = translate(tree.cond);
291         tree.detail = translate(tree.detail);
292         result = tree;
293     }
294 
295     public void visitApply(JCMethodInvocation tree) {
296         tree.meth = translate(tree.meth);
297         tree.args = translate(tree.args);
298         result = tree;
299     }
300 
301     public void visitNewClass(JCNewClass tree) {
302         tree.encl = translate(tree.encl);
303         tree.clazz = translate(tree.clazz);
304         tree.args = translate(tree.args);
305         tree.def = translate(tree.def);
306         result = tree;
307     }
308 
309     public void visitLambda(JCLambda tree) {
310         tree.params = translate(tree.params);
311         tree.body = translate(tree.body);
312         result = tree;
313     }
314 
315     public void visitNewArray(JCNewArray tree) {
316         tree.annotations = translate(tree.annotations);
317         List<List<JCAnnotation>> dimAnnos = List.nil();
318         for (List<JCAnnotation> origDimAnnos : tree.dimAnnotations)
319             dimAnnos = dimAnnos.append(translate(origDimAnnos));
320         tree.dimAnnotations = dimAnnos;
321         tree.elemtype = translate(tree.elemtype);
322         tree.dims = translate(tree.dims);
323         tree.elems = translate(tree.elems);
324         result = tree;
325     }
326 
327     public void visitParens(JCParens tree) {
328         tree.expr = translate(tree.expr);
329         result = tree;
330     }
331 
332     public void visitAssign(JCAssign tree) {
333         tree.lhs = translate(tree.lhs);
334         tree.rhs = translate(tree.rhs);
335         result = tree;
336     }
337 
338     public void visitAssignop(JCAssignOp tree) {
339         tree.lhs = translate(tree.lhs);
340         tree.rhs = translate(tree.rhs);
341         result = tree;
342     }
343 
344     public void visitUnary(JCUnary tree) {
345         tree.arg = translate(tree.arg);
346         result = tree;
347     }
348 
349     public void visitBinary(JCBinary tree) {
350         tree.lhs = translate(tree.lhs);
351         tree.rhs = translate(tree.rhs);
352         result = tree;
353     }
354 
355     public void visitTypeCast(JCTypeCast tree) {
356         tree.clazz = translate(tree.clazz);
357         tree.expr = translate(tree.expr);
358         result = tree;
359     }
360 
361     public void visitTypeTest(JCInstanceOf tree) {
362         tree.expr = translate(tree.expr);
363         tree.pattern = translate(tree.pattern);
364         result = tree;
365     }
366 
367     public void visitBindingPattern(JCBindingPattern tree) {
368         tree.var = translate(tree.var);
369         result = tree;
370     }
371 
372     @Override
373     public void visitDefaultCaseLabel(JCDefaultCaseLabel tree) {
374         result = tree;
375     }
376 
377     @Override
378     public void visitParenthesizedPattern(JCParenthesizedPattern tree) {
379         tree.pattern = translate(tree.pattern);
380         result = tree;
381     }
382 
383     @Override
384     public void visitGuardPattern(JCGuardPattern tree) {
385         tree.patt = translate(tree.patt);
386         tree.expr = translate(tree.expr);
387         result = tree;
388     }
389 
390     public void visitIndexed(JCArrayAccess tree) {
391         tree.indexed = translate(tree.indexed);
392         tree.index = translate(tree.index);
393         result = tree;
394     }
395 
396     public void visitSelect(JCFieldAccess tree) {
397         tree.selected = translate(tree.selected);
398         result = tree;
399     }
400 
401     public void visitDefaultValue(JCDefaultValue tree) {
402         tree.clazz = translate(tree.clazz);
403         result = tree;
404     }
405 
406     public void visitReference(JCMemberReference tree) {
407         tree.expr = translate(tree.expr);
408         result = tree;
409     }
410 
411     public void visitIdent(JCIdent tree) {
412         result = tree;
413     }
414 
415     public void visitLiteral(JCLiteral tree) {
416         result = tree;
417     }
418 
419     public void visitTypeIdent(JCPrimitiveTypeTree tree) {
420         result = tree;
421     }
422 
423     public void visitTypeArray(JCArrayTypeTree tree) {
424         tree.elemtype = translate(tree.elemtype);
425         result = tree;
426     }
427 
428     public void visitTypeApply(JCTypeApply tree) {
429         tree.clazz = translate(tree.clazz);
430         tree.arguments = translate(tree.arguments);
431         result = tree;
432     }
433 
434     public void visitTypeUnion(JCTypeUnion tree) {
435         tree.alternatives = translate(tree.alternatives);
436         result = tree;
437     }
438 
439     public void visitTypeIntersection(JCTypeIntersection tree) {
440         tree.bounds = translate(tree.bounds);
441         result = tree;
442     }
443 
444     public void visitTypeParameter(JCTypeParameter tree) {
445         tree.annotations = translate(tree.annotations);
446         tree.bounds = translate(tree.bounds);
447         result = tree;
448     }
449 
450     @Override
451     public void visitWildcard(JCWildcard tree) {
452         tree.kind = translate(tree.kind);
453         tree.inner = translate(tree.inner);
454         result = tree;
455     }
456 
457     @Override
458     public void visitTypeBoundKind(TypeBoundKind tree) {
459         result = tree;
460     }
461 
462     public void visitErroneous(JCErroneous tree) {
463         result = tree;
464     }
465 
466     public void visitLetExpr(LetExpr tree) {
467         tree.defs = translate(tree.defs);
468         tree.expr = translate(tree.expr);
469         result = tree;
470     }
471 
472     public void visitModifiers(JCModifiers tree) {
473         tree.annotations = translateAnnotations(tree.annotations);
474         result = tree;
475     }
476 
477     public void visitAnnotation(JCAnnotation tree) {
478         tree.annotationType = translate(tree.annotationType);
479         tree.args = translate(tree.args);
480         result = tree;
481     }
482 
483     public void visitAnnotatedType(JCAnnotatedType tree) {
484         tree.annotations = translate(tree.annotations);
485         tree.underlyingType = translate(tree.underlyingType);
486         result = tree;
487     }
488 
489     public void visitTree(JCTree tree) {
490         throw new AssertionError(tree);
491     }
492 }