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.pats = translate(tree.pats);
 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.clazz = translate(tree.clazz);
 364         result = tree;
 365     }
 366 
 367     public void visitIndexed(JCArrayAccess tree) {
 368         tree.indexed = translate(tree.indexed);
 369         tree.index = translate(tree.index);
 370         result = tree;
 371     }
 372 
 373     public void visitSelect(JCFieldAccess tree) {
 374         tree.selected = translate(tree.selected);
 375         result = tree;
 376     }
 377 
 378     public void visitReference(JCMemberReference tree) {
 379         tree.expr = translate(tree.expr);
 380         result = tree;
 381     }
 382 
 383     public void visitIdent(JCIdent tree) {
 384         result = tree;
 385     }
 386 
 387     public void visitLiteral(JCLiteral tree) {
 388         result = tree;
 389     }
 390 
 391     public void visitTypeIdent(JCPrimitiveTypeTree tree) {
 392         result = tree;
 393     }
 394 
 395     public void visitTypeArray(JCArrayTypeTree tree) {
 396         tree.elemtype = translate(tree.elemtype);
 397         result = tree;
 398     }
 399 
 400     public void visitTypeApply(JCTypeApply tree) {
 401         tree.clazz = translate(tree.clazz);
 402         tree.arguments = translate(tree.arguments);
 403         result = tree;
 404     }
 405 
 406     public void visitTypeUnion(JCTypeUnion tree) {
 407         tree.alternatives = translate(tree.alternatives);
 408         result = tree;
 409     }
 410 
 411     public void visitTypeIntersection(JCTypeIntersection tree) {
 412         tree.bounds = translate(tree.bounds);
 413         result = tree;
 414     }
 415 
 416     public void visitTypeParameter(JCTypeParameter tree) {
 417         tree.annotations = translate(tree.annotations);
 418         tree.bounds = translate(tree.bounds);
 419         result = tree;
 420     }
 421 
 422     @Override
 423     public void visitWildcard(JCWildcard tree) {
 424         tree.kind = translate(tree.kind);
 425         tree.inner = translate(tree.inner);
 426         result = tree;
 427     }
 428 
 429     @Override
 430     public void visitTypeBoundKind(TypeBoundKind tree) {
 431         result = tree;
 432     }
 433 
 434     public void visitErroneous(JCErroneous tree) {
 435         result = tree;
 436     }
 437 
 438     public void visitLetExpr(LetExpr tree) {
 439         tree.defs = translate(tree.defs);
 440         tree.expr = translate(tree.expr);
 441         result = tree;
 442     }
 443 
 444     public void visitModifiers(JCModifiers tree) {
 445         tree.annotations = translateAnnotations(tree.annotations);
 446         result = tree;
 447     }
 448 
 449     public void visitAnnotation(JCAnnotation tree) {
 450         tree.annotationType = translate(tree.annotationType);
 451         tree.args = translate(tree.args);
 452         result = tree;
 453     }
 454 
 455     public void visitAnnotatedType(JCAnnotatedType tree) {
 456         tree.annotations = translate(tree.annotations);
 457         tree.underlyingType = translate(tree.underlyingType);
 458         result = tree;
 459     }
 460 
 461     public void visitTree(JCTree tree) {
 462         throw new AssertionError(tree);
 463     }
 464 }