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 java.util.Iterator;
  29 
  30 import com.sun.source.tree.CaseTree.CaseKind;
  31 import com.sun.source.tree.ModuleTree.ModuleKind;
  32 import com.sun.tools.javac.code.*;
  33 import com.sun.tools.javac.code.Attribute.UnresolvedClass;
  34 import com.sun.tools.javac.code.Symbol.*;
  35 import com.sun.tools.javac.code.Type.*;
  36 import com.sun.tools.javac.util.*;
  37 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  38 
  39 import com.sun.tools.javac.tree.JCTree.*;
  40 
  41 import static com.sun.tools.javac.code.Flags.*;
  42 import static com.sun.tools.javac.code.Kinds.Kind.*;
  43 import static com.sun.tools.javac.code.TypeTag.*;
  44 
  45 /** Factory class for trees.
  46  *
  47  *  <p><b>This is NOT part of any supported API.
  48  *  If you write code that depends on this, you do so at your own risk.
  49  *  This code and its internal interfaces are subject to change or
  50  *  deletion without notice.</b>
  51  */
  52 public class TreeMaker implements JCTree.Factory {
  53 
  54     /** The context key for the tree factory. */
  55     protected static final Context.Key<TreeMaker> treeMakerKey = new Context.Key<>();
  56 
  57     /** Get the TreeMaker instance. */
  58     public static TreeMaker instance(Context context) {
  59         TreeMaker instance = context.get(treeMakerKey);
  60         if (instance == null)
  61             instance = new TreeMaker(context);
  62         return instance;
  63     }
  64 
  65     /** The position at which subsequent trees will be created.
  66      */
  67     public int pos = Position.NOPOS;
  68 
  69     /** The toplevel tree to which created trees belong.
  70      */
  71     public JCCompilationUnit toplevel;
  72 
  73     /** The current name table. */
  74     Names names;
  75 
  76     Types types;
  77 
  78     /** The current symbol table. */
  79     Symtab syms;
  80 
  81     /** Create a tree maker with null toplevel and NOPOS as initial position.
  82      */
  83     protected TreeMaker(Context context) {
  84         context.put(treeMakerKey, this);
  85         this.pos = Position.NOPOS;
  86         this.toplevel = null;
  87         this.names = Names.instance(context);
  88         this.syms = Symtab.instance(context);
  89         this.types = Types.instance(context);
  90     }
  91 
  92     /** Create a tree maker with a given toplevel and FIRSTPOS as initial position.
  93      */
  94     protected TreeMaker(JCCompilationUnit toplevel, Names names, Types types, Symtab syms) {
  95         this.pos = Position.FIRSTPOS;
  96         this.toplevel = toplevel;
  97         this.names = names;
  98         this.types = types;
  99         this.syms = syms;
 100     }
 101 
 102     /** Create a new tree maker for a given toplevel.
 103      */
 104     public TreeMaker forToplevel(JCCompilationUnit toplevel) {
 105         return new TreeMaker(toplevel, names, types, syms);
 106     }
 107 
 108     /** Reassign current position.
 109      */
 110     public TreeMaker at(int pos) {
 111         this.pos = pos;
 112         return this;
 113     }
 114 
 115     /** Reassign current position.
 116      */
 117     public TreeMaker at(DiagnosticPosition pos) {
 118         this.pos = (pos == null ? Position.NOPOS : pos.getStartPosition());
 119         return this;
 120     }
 121 
 122     /**
 123      * Create given tree node at current position.
 124      * @param defs a list of PackageDef, ClassDef, Import, and Skip
 125      */
 126     public JCCompilationUnit TopLevel(List<JCTree> defs) {
 127         for (JCTree node : defs)
 128             Assert.check(node instanceof JCClassDecl
 129                 || node instanceof JCPackageDecl
 130                 || node instanceof JCImport
 131                 || node instanceof JCModuleDecl
 132                 || node instanceof JCSkip
 133                 || node instanceof JCErroneous
 134                 || (node instanceof JCExpressionStatement
 135                     && ((JCExpressionStatement)node).expr instanceof JCErroneous),
 136                     () -> node.getClass().getSimpleName());
 137         JCCompilationUnit tree = new JCCompilationUnit(defs);
 138         tree.pos = pos;
 139         return tree;
 140     }
 141 
 142     public JCPackageDecl PackageDecl(List<JCAnnotation> annotations,
 143                                      JCExpression pid) {
 144         Assert.checkNonNull(annotations);
 145         Assert.checkNonNull(pid);
 146         JCPackageDecl tree = new JCPackageDecl(annotations, pid);
 147         tree.pos = pos;
 148         return tree;
 149     }
 150 
 151     public JCImport Import(JCTree qualid, boolean importStatic) {
 152         JCImport tree = new JCImport(qualid, importStatic);
 153         tree.pos = pos;
 154         return tree;
 155     }
 156 
 157     public JCClassDecl ClassDef(JCModifiers mods,
 158                                 Name name,
 159                                 List<JCTypeParameter> typarams,
 160                                 JCExpression extending,
 161                                 List<JCExpression> implementing,
 162                                 List<JCTree> defs)
 163     {
 164         return ClassDef(mods, name, typarams, extending, implementing, List.nil(), defs);
 165     }
 166 
 167     public JCClassDecl ClassDef(JCModifiers mods,
 168                                 Name name,
 169                                 List<JCTypeParameter> typarams,
 170                                 JCExpression extending,
 171                                 List<JCExpression> implementing,
 172                                 List<JCExpression> permitting,
 173                                 List<JCTree> defs)
 174     {
 175         JCClassDecl tree = new JCClassDecl(mods,
 176                                      name,
 177                                      typarams,
 178                                      extending,
 179                                      implementing,
 180                                      permitting,
 181                                      defs,
 182                                      null);
 183         tree.pos = pos;
 184         return tree;
 185     }
 186 
 187     public JCMethodDecl MethodDef(JCModifiers mods,
 188                                Name name,
 189                                JCExpression restype,
 190                                List<JCTypeParameter> typarams,
 191                                List<JCVariableDecl> params,
 192                                List<JCExpression> thrown,
 193                                JCBlock body,
 194                                JCExpression defaultValue) {
 195         return MethodDef(
 196                 mods, name, restype, typarams, null, params,
 197                 thrown, body, defaultValue);
 198     }
 199 
 200     public JCMethodDecl MethodDef(JCModifiers mods,
 201                                Name name,
 202                                JCExpression restype,
 203                                List<JCTypeParameter> typarams,
 204                                JCVariableDecl recvparam,
 205                                List<JCVariableDecl> params,
 206                                List<JCExpression> thrown,
 207                                JCBlock body,
 208                                JCExpression defaultValue)
 209     {
 210         JCMethodDecl tree = new JCMethodDecl(mods,
 211                                        name,
 212                                        restype,
 213                                        typarams,
 214                                        recvparam,
 215                                        params,
 216                                        thrown,
 217                                        body,
 218                                        defaultValue,
 219                                        null);
 220         tree.pos = pos;
 221         return tree;
 222     }
 223 
 224     public JCVariableDecl VarDef(JCModifiers mods, Name name, JCExpression vartype, JCExpression init) {
 225         return VarDef(mods, name, vartype, init, null);
 226     }
 227 
 228     public JCVariableDecl VarDef(JCModifiers mods, Name name, JCExpression vartype, JCExpression init, List<Pair<Accessors.Kind, Name>> accessors) {
 229         JCVariableDecl tree = new JCVariableDecl(mods, name, vartype, init, null, accessors);
 230         tree.pos = pos;
 231         return tree;
 232     }
 233 
 234     public JCVariableDecl ReceiverVarDef(JCModifiers mods, JCExpression name, JCExpression vartype) {
 235         JCVariableDecl tree = new JCVariableDecl(mods, name, vartype);
 236         tree.pos = pos;
 237         return tree;
 238     }
 239 
 240     public JCSkip Skip() {
 241         JCSkip tree = new JCSkip();
 242         tree.pos = pos;
 243         return tree;
 244     }
 245 
 246     public JCBlock Block(long flags, List<JCStatement> stats) {
 247         JCBlock tree = new JCBlock(flags, stats);
 248         tree.pos = pos;
 249         return tree;
 250     }
 251 
 252     public JCDoWhileLoop DoLoop(JCStatement body, JCExpression cond) {
 253         JCDoWhileLoop tree = new JCDoWhileLoop(body, cond);
 254         tree.pos = pos;
 255         return tree;
 256     }
 257 
 258     public JCWhileLoop WhileLoop(JCExpression cond, JCStatement body) {
 259         JCWhileLoop tree = new JCWhileLoop(cond, body);
 260         tree.pos = pos;
 261         return tree;
 262     }
 263 
 264     public JCForLoop ForLoop(List<JCStatement> init,
 265                            JCExpression cond,
 266                            List<JCExpressionStatement> step,
 267                            JCStatement body)
 268     {
 269         JCForLoop tree = new JCForLoop(init, cond, step, body);
 270         tree.pos = pos;
 271         return tree;
 272     }
 273 
 274     public JCEnhancedForLoop ForeachLoop(JCVariableDecl var, JCExpression expr, JCStatement body) {
 275         JCEnhancedForLoop tree = new JCEnhancedForLoop(var, expr, body);
 276         tree.pos = pos;
 277         return tree;
 278     }
 279 
 280     public JCLabeledStatement Labelled(Name label, JCStatement body) {
 281         JCLabeledStatement tree = new JCLabeledStatement(label, body);
 282         tree.pos = pos;
 283         return tree;
 284     }
 285 
 286     public JCSwitch Switch(JCExpression selector, List<JCCase> cases) {
 287         JCSwitch tree = new JCSwitch(selector, cases);
 288         tree.pos = pos;
 289         return tree;
 290     }
 291 
 292     public JCCase Case(@SuppressWarnings("removal") CaseKind caseKind, List<JCExpression> pats,
 293                        List<JCStatement> stats, JCTree body) {
 294         JCCase tree = new JCCase(caseKind, pats, stats, body);
 295         tree.pos = pos;
 296         return tree;
 297     }
 298 
 299     public JCSwitchExpression SwitchExpression(JCExpression selector, List<JCCase> cases) {
 300         JCSwitchExpression tree = new JCSwitchExpression(selector, cases);
 301         tree.pos = pos;
 302         return tree;
 303     }
 304 
 305     public JCSynchronized Synchronized(JCExpression lock, JCBlock body) {
 306         JCSynchronized tree = new JCSynchronized(lock, body);
 307         tree.pos = pos;
 308         return tree;
 309     }
 310 
 311     public JCTry Try(JCBlock body, List<JCCatch> catchers, JCBlock finalizer) {
 312         return Try(List.nil(), body, catchers, finalizer);
 313     }
 314 
 315     public JCTry Try(List<JCTree> resources,
 316                      JCBlock body,
 317                      List<JCCatch> catchers,
 318                      JCBlock finalizer) {
 319         JCTry tree = new JCTry(resources, body, catchers, finalizer);
 320         tree.pos = pos;
 321         return tree;
 322     }
 323 
 324     public JCCatch Catch(JCVariableDecl param, JCBlock body) {
 325         JCCatch tree = new JCCatch(param, body);
 326         tree.pos = pos;
 327         return tree;
 328     }
 329 
 330     public JCConditional Conditional(JCExpression cond,
 331                                    JCExpression thenpart,
 332                                    JCExpression elsepart)
 333     {
 334         JCConditional tree = new JCConditional(cond, thenpart, elsepart);
 335         tree.pos = pos;
 336         return tree;
 337     }
 338 
 339     public JCIf If(JCExpression cond, JCStatement thenpart, JCStatement elsepart) {
 340         JCIf tree = new JCIf(cond, thenpart, elsepart);
 341         tree.pos = pos;
 342         return tree;
 343     }
 344 
 345     public JCExpressionStatement Exec(JCExpression expr) {
 346         JCExpressionStatement tree = new JCExpressionStatement(expr);
 347         tree.pos = pos;
 348         return tree;
 349     }
 350 
 351     public JCBreak Break(Name label) {
 352         JCBreak tree = new JCBreak(label, null);
 353         tree.pos = pos;
 354         return tree;
 355     }
 356 
 357     public JCYield Yield(JCExpression value) {
 358         JCYield tree = new JCYield(value, null);
 359         tree.pos = pos;
 360         return tree;
 361     }
 362 
 363     public JCContinue Continue(Name label) {
 364         JCContinue tree = new JCContinue(label, null);
 365         tree.pos = pos;
 366         return tree;
 367     }
 368 
 369     public JCReturn Return(JCExpression expr) {
 370         JCReturn tree = new JCReturn(expr);
 371         tree.pos = pos;
 372         return tree;
 373     }
 374 
 375     public JCThrow Throw(JCExpression expr) {
 376         JCThrow tree = new JCThrow(expr);
 377         tree.pos = pos;
 378         return tree;
 379     }
 380 
 381     public JCAssert Assert(JCExpression cond, JCExpression detail) {
 382         JCAssert tree = new JCAssert(cond, detail);
 383         tree.pos = pos;
 384         return tree;
 385     }
 386 
 387     public JCMethodInvocation Apply(List<JCExpression> typeargs,
 388                        JCExpression fn,
 389                        List<JCExpression> args)
 390     {
 391         JCMethodInvocation tree = new JCMethodInvocation(typeargs, fn, args);
 392         tree.pos = pos;
 393         return tree;
 394     }
 395 
 396     public JCNewClass NewClass(JCExpression encl,
 397                              List<JCExpression> typeargs,
 398                              JCExpression clazz,
 399                              List<JCExpression> args,
 400                              JCClassDecl def)
 401     {
 402         return SpeculativeNewClass(encl, typeargs, clazz, args, def, false);
 403     }
 404 
 405     public JCNewClass SpeculativeNewClass(JCExpression encl,
 406                              List<JCExpression> typeargs,
 407                              JCExpression clazz,
 408                              List<JCExpression> args,
 409                              JCClassDecl def,
 410                              boolean classDefRemoved)
 411     {
 412         JCNewClass tree = classDefRemoved ?
 413                 new JCNewClass(encl, typeargs, clazz, args, def) {
 414                     @Override
 415                     public boolean classDeclRemoved() {
 416                         return true;
 417                     }
 418                 } :
 419                 new JCNewClass(encl, typeargs, clazz, args, def);
 420         tree.pos = pos;
 421         return tree;
 422     }
 423 
 424     public JCNewArray NewArray(JCExpression elemtype,
 425                              List<JCExpression> dims,
 426                              List<JCExpression> elems)
 427     {
 428         JCNewArray tree = new JCNewArray(elemtype, dims, elems);
 429         tree.pos = pos;
 430         return tree;
 431     }
 432 
 433     public JCLambda Lambda(List<JCVariableDecl> params,
 434                            JCTree body)
 435     {
 436         JCLambda tree = new JCLambda(params, body);
 437         tree.pos = pos;
 438         return tree;
 439     }
 440 
 441     public JCParens Parens(JCExpression expr) {
 442         JCParens tree = new JCParens(expr);
 443         tree.pos = pos;
 444         return tree;
 445     }
 446 
 447     public JCAssign Assign(JCExpression lhs, JCExpression rhs) {
 448         JCAssign tree = new JCAssign(lhs, rhs);
 449         tree.pos = pos;
 450         return tree;
 451     }
 452 
 453     public JCAssignOp Assignop(JCTree.Tag opcode, JCTree lhs, JCTree rhs) {
 454         JCAssignOp tree = new JCAssignOp(opcode, lhs, rhs, null);
 455         tree.pos = pos;
 456         return tree;
 457     }
 458 
 459     public JCUnary Unary(JCTree.Tag opcode, JCExpression arg) {
 460         JCUnary tree = new JCUnary(opcode, arg);
 461         tree.pos = pos;
 462         return tree;
 463     }
 464 
 465     public JCBinary Binary(JCTree.Tag opcode, JCExpression lhs, JCExpression rhs) {
 466         JCBinary tree = new JCBinary(opcode, lhs, rhs, null);
 467         tree.pos = pos;
 468         return tree;
 469     }
 470 
 471     public JCTypeCast TypeCast(JCTree clazz, JCExpression expr) {
 472         JCTypeCast tree = new JCTypeCast(clazz, expr);
 473         tree.pos = pos;
 474         return tree;
 475     }
 476 
 477     public JCInstanceOf TypeTest(JCExpression expr, JCTree clazz) {
 478         JCInstanceOf tree = new JCInstanceOf(expr, clazz);
 479         tree.pos = pos;
 480         return tree;
 481     }
 482 
 483     public JCBindingPattern BindingPattern(Name name, JCTree vartype) {
 484         JCBindingPattern tree = new JCBindingPattern(name, null, vartype);
 485         tree.pos = pos;
 486         return tree;
 487     }
 488 
 489     public JCArrayAccess Indexed(JCExpression indexed, JCExpression index) {
 490         JCArrayAccess tree = new JCArrayAccess(indexed, index);
 491         tree.pos = pos;
 492         return tree;
 493     }
 494 
 495     public JCFieldAccess Select(JCExpression selected, Name selector) {
 496         JCFieldAccess tree = new JCFieldAccess(selected, selector, null);
 497         tree.pos = pos;
 498         return tree;
 499     }
 500 
 501     public JCMemberReference Reference(JCMemberReference.ReferenceMode mode, Name name,
 502             JCExpression expr, List<JCExpression> typeargs) {
 503         JCMemberReference tree = new JCMemberReference(mode, name, expr, typeargs);
 504         tree.pos = pos;
 505         return tree;
 506     }
 507 
 508     public JCIdent Ident(Name name) {
 509         JCIdent tree = new JCIdent(name, null);
 510         tree.pos = pos;
 511         return tree;
 512     }
 513 
 514     public JCLiteral Literal(TypeTag tag, Object value) {
 515         JCLiteral tree = new JCLiteral(tag, value);
 516         tree.pos = pos;
 517         return tree;
 518     }
 519 
 520     public JCPrimitiveTypeTree TypeIdent(TypeTag typetag) {
 521         JCPrimitiveTypeTree tree = new JCPrimitiveTypeTree(typetag);
 522         tree.pos = pos;
 523         return tree;
 524     }
 525 
 526     public JCArrayTypeTree TypeArray(JCExpression elemtype) {
 527         JCArrayTypeTree tree = new JCArrayTypeTree(elemtype);
 528         tree.pos = pos;
 529         return tree;
 530     }
 531 
 532     public JCTypeApply TypeApply(JCExpression clazz, List<JCExpression> arguments) {
 533         JCTypeApply tree = new JCTypeApply(clazz, arguments);
 534         tree.pos = pos;
 535         return tree;
 536     }
 537 
 538     public JCTypeUnion TypeUnion(List<JCExpression> components) {
 539         JCTypeUnion tree = new JCTypeUnion(components);
 540         tree.pos = pos;
 541         return tree;
 542     }
 543 
 544     public JCTypeIntersection TypeIntersection(List<JCExpression> components) {
 545         JCTypeIntersection tree = new JCTypeIntersection(components);
 546         tree.pos = pos;
 547         return tree;
 548     }
 549 
 550     public JCTypeParameter TypeParameter(Name name, List<JCExpression> bounds) {
 551         return TypeParameter(name, bounds, List.nil());
 552     }
 553 
 554     public JCTypeParameter TypeParameter(Name name, List<JCExpression> bounds, List<JCAnnotation> annos) {
 555         JCTypeParameter tree = new JCTypeParameter(name, bounds, annos);
 556         tree.pos = pos;
 557         return tree;
 558     }
 559 
 560     public JCWildcard Wildcard(TypeBoundKind kind, JCTree type) {
 561         JCWildcard tree = new JCWildcard(kind, type);
 562         tree.pos = pos;
 563         return tree;
 564     }
 565 
 566     public TypeBoundKind TypeBoundKind(BoundKind kind) {
 567         TypeBoundKind tree = new TypeBoundKind(kind);
 568         tree.pos = pos;
 569         return tree;
 570     }
 571 
 572     public JCAnnotation Annotation(JCTree annotationType, List<JCExpression> args) {
 573         JCAnnotation tree = new JCAnnotation(Tag.ANNOTATION, annotationType, args);
 574         tree.pos = pos;
 575         return tree;
 576     }
 577 
 578     public JCAnnotation TypeAnnotation(JCTree annotationType, List<JCExpression> args) {
 579         JCAnnotation tree = new JCAnnotation(Tag.TYPE_ANNOTATION, annotationType, args);
 580         tree.pos = pos;
 581         return tree;
 582     }
 583 
 584     public JCModifiers Modifiers(long flags, List<JCAnnotation> annotations) {
 585         JCModifiers tree = new JCModifiers(flags, annotations);
 586         boolean noFlags = (flags & (Flags.ModifierFlags | Flags.ANNOTATION)) == 0;
 587         tree.pos = (noFlags && annotations.isEmpty()) ? Position.NOPOS : pos;
 588         return tree;
 589     }
 590 
 591     public JCModifiers Modifiers(long flags) {
 592         return Modifiers(flags, List.nil());
 593     }
 594 
 595     @Override
 596     public JCModuleDecl ModuleDef(JCModifiers mods, ModuleKind kind,
 597             JCExpression qualid, List<JCDirective> directives) {
 598         JCModuleDecl tree = new JCModuleDecl(mods, kind, qualid, directives);
 599         tree.pos = pos;
 600         return tree;
 601     }
 602 
 603     @Override
 604     public JCExports Exports(JCExpression qualId, List<JCExpression> moduleNames) {
 605         JCExports tree = new JCExports(qualId, moduleNames);
 606         tree.pos = pos;
 607         return tree;
 608     }
 609 
 610     @Override
 611     public JCOpens Opens(JCExpression qualId, List<JCExpression> moduleNames) {
 612         JCOpens tree = new JCOpens(qualId, moduleNames);
 613         tree.pos = pos;
 614         return tree;
 615     }
 616 
 617     @Override
 618     public JCProvides Provides(JCExpression serviceName, List<JCExpression> implNames) {
 619         JCProvides tree = new JCProvides(serviceName, implNames);
 620         tree.pos = pos;
 621         return tree;
 622     }
 623 
 624     @Override
 625     public JCRequires Requires(boolean isTransitive, boolean isStaticPhase, JCExpression qualId) {
 626         JCRequires tree = new JCRequires(isTransitive, isStaticPhase, qualId);
 627         tree.pos = pos;
 628         return tree;
 629     }
 630 
 631     @Override
 632     public JCUses Uses(JCExpression qualId) {
 633         JCUses tree = new JCUses(qualId);
 634         tree.pos = pos;
 635         return tree;
 636     }
 637 
 638     public JCAnnotatedType AnnotatedType(List<JCAnnotation> annotations, JCExpression underlyingType) {
 639         JCAnnotatedType tree = new JCAnnotatedType(annotations, underlyingType);
 640         tree.pos = pos;
 641         return tree;
 642     }
 643 
 644     public JCErroneous Erroneous() {
 645         return Erroneous(List.nil());
 646     }
 647 
 648     public JCErroneous Erroneous(List<? extends JCTree> errs) {
 649         JCErroneous tree = new JCErroneous(errs);
 650         tree.pos = pos;
 651         return tree;
 652     }
 653 
 654     public LetExpr LetExpr(List<JCStatement> defs, JCExpression expr) {
 655         LetExpr tree = new LetExpr(defs, expr);
 656         tree.pos = pos;
 657         return tree;
 658     }
 659 
 660 /* ***************************************************************************
 661  * Derived building blocks.
 662  ****************************************************************************/
 663 
 664     public JCClassDecl AnonymousClassDef(JCModifiers mods,
 665                                          List<JCTree> defs)
 666     {
 667         return ClassDef(mods,
 668                         names.empty,
 669                         List.nil(),
 670                         null,
 671                         List.nil(),
 672                         defs);
 673     }
 674 
 675     public LetExpr LetExpr(JCVariableDecl def, JCExpression expr) {
 676         LetExpr tree = new LetExpr(List.of(def), expr);
 677         tree.pos = pos;
 678         return tree;
 679     }
 680 
 681     /** Create an identifier from a symbol.
 682      */
 683     public JCIdent Ident(Symbol sym) {
 684         return (JCIdent)new JCIdent((sym.name != names.empty)
 685                                 ? sym.name
 686                                 : sym.flatName(), sym)
 687             .setPos(pos)
 688             .setType(sym.type);
 689     }
 690 
 691     /** Create a selection node from a qualifier tree and a symbol.
 692      *  @param base   The qualifier tree.
 693      */
 694     public JCExpression Select(JCExpression base, Symbol sym) {
 695         return new JCFieldAccess(base, sym.name, sym).setPos(pos).setType(sym.type);
 696     }
 697 
 698     /** Create a qualified identifier from a symbol, adding enough qualifications
 699      *  to make the reference unique.
 700      */
 701     public JCExpression QualIdent(Symbol sym) {
 702         return isUnqualifiable(sym)
 703             ? Ident(sym)
 704             : Select(QualIdent(sym.owner), sym);
 705     }
 706 
 707     /** Create an identifier that refers to the variable declared in given variable
 708      *  declaration.
 709      */
 710     public JCExpression Ident(JCVariableDecl param) {
 711         return Ident(param.sym);
 712     }
 713 
 714     /** Create a list of identifiers referring to the variables declared
 715      *  in given list of variable declarations.
 716      */
 717     public List<JCExpression> Idents(List<JCVariableDecl> params) {
 718         ListBuffer<JCExpression> ids = new ListBuffer<>();
 719         for (List<JCVariableDecl> l = params; l.nonEmpty(); l = l.tail)
 720             ids.append(Ident(l.head));
 721         return ids.toList();
 722     }
 723 
 724     /** Create a tree representing `this', given its type.
 725      */
 726     public JCExpression This(Type t) {
 727         return Ident(new VarSymbol(FINAL, names._this, t, t.tsym));
 728     }
 729 
 730     /** Create a tree representing qualified `this' given its type
 731      */
 732     public JCExpression QualThis(Type t) {
 733         return Select(Type(t), new VarSymbol(FINAL, names._this, t, t.tsym));
 734     }
 735 
 736     /** Create a tree representing a class literal.
 737      */
 738     public JCExpression ClassLiteral(ClassSymbol clazz) {
 739         return ClassLiteral(clazz.type);
 740     }
 741 
 742     /** Create a tree representing a class literal.
 743      */
 744     public JCExpression ClassLiteral(Type t) {
 745         VarSymbol lit = new VarSymbol(STATIC | PUBLIC | FINAL,
 746                                       names._class,
 747                                       t,
 748                                       t.tsym);
 749         return Select(Type(t), lit);
 750     }
 751 
 752     /** Create a tree representing `super', given its type and owner.
 753      */
 754     public JCIdent Super(Type t, TypeSymbol owner) {
 755         return Ident(new VarSymbol(FINAL, names._super, t, owner));
 756     }
 757 
 758     /**
 759      * Create a method invocation from a method tree and a list of
 760      * argument trees.
 761      */
 762     public JCMethodInvocation App(JCExpression meth, List<JCExpression> args) {
 763         return Apply(null, meth, args).setType(meth.type.getReturnType());
 764     }
 765 
 766     /**
 767      * Create a no-arg method invocation from a method tree
 768      */
 769     public JCMethodInvocation App(JCExpression meth) {
 770         return Apply(null, meth, List.nil()).setType(meth.type.getReturnType());
 771     }
 772 
 773     /** Create a method invocation from a method tree and a list of argument trees.
 774      */
 775     public JCExpression Create(Symbol ctor, List<JCExpression> args) {
 776         Type t = ctor.owner.erasure(types);
 777         JCNewClass newclass = NewClass(null, null, Type(t), args, null);
 778         newclass.constructor = ctor;
 779         newclass.setType(t);
 780         return newclass;
 781     }
 782 
 783     /** Create a tree representing given type.
 784      */
 785     public JCExpression Type(Type t) {
 786         if (t == null) return null;
 787         JCExpression tp;
 788         switch (t.getTag()) {
 789         case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
 790         case DOUBLE: case BOOLEAN: case VOID:
 791             tp = TypeIdent(t.getTag());
 792             break;
 793         case TYPEVAR:
 794             tp = Ident(t.tsym);
 795             break;
 796         case WILDCARD: {
 797             WildcardType a = ((WildcardType) t);
 798             tp = Wildcard(TypeBoundKind(a.kind), a.kind == BoundKind.UNBOUND ? null : Type(a.type));
 799             break;
 800         }
 801         case CLASS:
 802             switch (t.getKind()) {
 803             case UNION: {
 804                 UnionClassType tu = (UnionClassType)t;
 805                 ListBuffer<JCExpression> la = new ListBuffer<>();
 806                 for (Type ta : tu.getAlternativeTypes()) {
 807                     la.add(Type(ta));
 808                 }
 809                 tp = TypeUnion(la.toList());
 810                 break;
 811             }
 812             case INTERSECTION: {
 813                 IntersectionClassType it = (IntersectionClassType)t;
 814                 ListBuffer<JCExpression> la = new ListBuffer<>();
 815                 for (Type ta : it.getExplicitComponents()) {
 816                     la.add(Type(ta));
 817                 }
 818                 tp = TypeIntersection(la.toList());
 819                 break;
 820             }
 821             default: {
 822                 Type outer = t.getEnclosingType();
 823                 JCExpression clazz = outer.hasTag(CLASS) && t.tsym.owner.kind == TYP
 824                         ? Select(Type(outer), t.tsym)
 825                         : QualIdent(t.tsym);
 826                 tp = t.getTypeArguments().isEmpty()
 827                         ? clazz
 828                         : TypeApply(clazz, Types(t.getTypeArguments()));
 829                 break;
 830             }
 831             }
 832             break;
 833         case ARRAY:
 834             tp = TypeArray(Type(types.elemtype(t)));
 835             break;
 836         case ERROR:
 837             tp = TypeIdent(ERROR);
 838             break;
 839         default:
 840             throw new AssertionError("unexpected type: " + t);
 841         }
 842         return tp.setType(t);
 843     }
 844 
 845     /** Create a list of trees representing given list of types.
 846      */
 847     public List<JCExpression> Types(List<Type> ts) {
 848         ListBuffer<JCExpression> lb = new ListBuffer<>();
 849         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
 850             lb.append(Type(l.head));
 851         return lb.toList();
 852     }
 853 
 854     /** Create a variable definition from a variable symbol and an initializer
 855      *  expression.
 856      */
 857     public JCVariableDecl VarDef(VarSymbol v, JCExpression init) {
 858         return (JCVariableDecl)
 859             new JCVariableDecl(
 860                 Modifiers(v.flags(), Annotations(v.getRawAttributes())),
 861                 v.name,
 862                 Type(v.type),
 863                 init,
 864                 v, null).setPos(pos).setType(v.type);
 865     }
 866 
 867     /** Create annotation trees from annotations.
 868      */
 869     public List<JCAnnotation> Annotations(List<Attribute.Compound> attributes) {
 870         if (attributes == null) return List.nil();
 871         ListBuffer<JCAnnotation> result = new ListBuffer<>();
 872         for (List<Attribute.Compound> i = attributes; i.nonEmpty(); i=i.tail) {
 873             Attribute a = i.head;
 874             result.append(Annotation(a));
 875         }
 876         return result.toList();
 877     }
 878 
 879     public JCLiteral Literal(Object value) {
 880         JCLiteral result = null;
 881         if (value instanceof String) {
 882             result = Literal(CLASS, value).
 883                 setType(syms.stringType.constType(value));
 884         } else if (value instanceof Integer) {
 885             result = Literal(INT, value).
 886                 setType(syms.intType.constType(value));
 887         } else if (value instanceof Long) {
 888             result = Literal(LONG, value).
 889                 setType(syms.longType.constType(value));
 890         } else if (value instanceof Byte) {
 891             result = Literal(BYTE, value).
 892                 setType(syms.byteType.constType(value));
 893         } else if (value instanceof Character) {
 894             int v = (int) (((Character) value).toString().charAt(0));
 895             result = Literal(CHAR, v).
 896                 setType(syms.charType.constType(v));
 897         } else if (value instanceof Double) {
 898             result = Literal(DOUBLE, value).
 899                 setType(syms.doubleType.constType(value));
 900         } else if (value instanceof Float) {
 901             result = Literal(FLOAT, value).
 902                 setType(syms.floatType.constType(value));
 903         } else if (value instanceof Short) {
 904             result = Literal(SHORT, value).
 905                 setType(syms.shortType.constType(value));
 906         } else if (value instanceof Boolean) {
 907             int v = ((Boolean) value) ? 1 : 0;
 908             result = Literal(BOOLEAN, v).
 909                 setType(syms.booleanType.constType(v));
 910         } else {
 911             throw new AssertionError(value);
 912         }
 913         return result;
 914     }
 915 
 916     class AnnotationBuilder implements Attribute.Visitor {
 917         JCExpression result = null;
 918         public void visitConstant(Attribute.Constant v) {
 919             result = Literal(v.type.getTag(), v.value);
 920         }
 921         public void visitClass(Attribute.Class clazz) {
 922             result = ClassLiteral(clazz.classType).setType(syms.classType);
 923         }
 924         public void visitEnum(Attribute.Enum e) {
 925             result = QualIdent(e.value);
 926         }
 927         public void visitError(Attribute.Error e) {
 928             if (e instanceof UnresolvedClass) {
 929                 result = ClassLiteral(((UnresolvedClass) e).classType).setType(syms.classType);
 930             } else {
 931                 result = Erroneous();
 932             }
 933         }
 934         public void visitCompound(Attribute.Compound compound) {
 935             if (compound instanceof Attribute.TypeCompound) {
 936                 result = visitTypeCompoundInternal((Attribute.TypeCompound) compound);
 937             } else {
 938                 result = visitCompoundInternal(compound);
 939             }
 940         }
 941         public JCAnnotation visitCompoundInternal(Attribute.Compound compound) {
 942             ListBuffer<JCExpression> args = new ListBuffer<>();
 943             for (List<Pair<Symbol.MethodSymbol,Attribute>> values = compound.values; values.nonEmpty(); values=values.tail) {
 944                 Pair<MethodSymbol,Attribute> pair = values.head;
 945                 JCExpression valueTree = translate(pair.snd);
 946                 args.append(Assign(Ident(pair.fst), valueTree).setType(valueTree.type));
 947             }
 948             return Annotation(Type(compound.type), args.toList());
 949         }
 950         public JCAnnotation visitTypeCompoundInternal(Attribute.TypeCompound compound) {
 951             ListBuffer<JCExpression> args = new ListBuffer<>();
 952             for (List<Pair<Symbol.MethodSymbol,Attribute>> values = compound.values; values.nonEmpty(); values=values.tail) {
 953                 Pair<MethodSymbol,Attribute> pair = values.head;
 954                 JCExpression valueTree = translate(pair.snd);
 955                 args.append(Assign(Ident(pair.fst), valueTree).setType(valueTree.type));
 956             }
 957             return TypeAnnotation(Type(compound.type), args.toList());
 958         }
 959         public void visitArray(Attribute.Array array) {
 960             ListBuffer<JCExpression> elems = new ListBuffer<>();
 961             for (int i = 0; i < array.values.length; i++)
 962                 elems.append(translate(array.values[i]));
 963             result = NewArray(null, List.nil(), elems.toList()).setType(array.type);
 964         }
 965         JCExpression translate(Attribute a) {
 966             a.accept(this);
 967             return result;
 968         }
 969         JCAnnotation translate(Attribute.Compound a) {
 970             return visitCompoundInternal(a);
 971         }
 972         JCAnnotation translate(Attribute.TypeCompound a) {
 973             return visitTypeCompoundInternal(a);
 974         }
 975     }
 976 
 977     AnnotationBuilder annotationBuilder = new AnnotationBuilder();
 978 
 979     /** Create an annotation tree from an attribute.
 980      */
 981     public JCAnnotation Annotation(Attribute a) {
 982         return annotationBuilder.translate((Attribute.Compound)a);
 983     }
 984 
 985     public JCAnnotation TypeAnnotation(Attribute a) {
 986         return annotationBuilder.translate((Attribute.TypeCompound) a);
 987     }
 988 
 989     /** Create a method definition from a method symbol and a method body.
 990      */
 991     public JCMethodDecl MethodDef(MethodSymbol m, JCBlock body) {
 992         return MethodDef(m, m.type, body);
 993     }
 994 
 995     /** Create a method definition from a method symbol, method type
 996      *  and a method body.
 997      */
 998     public JCMethodDecl MethodDef(MethodSymbol m, Type mtype, JCBlock body) {
 999         return (JCMethodDecl)
1000             new JCMethodDecl(
1001                 Modifiers(m.flags(), Annotations(m.getRawAttributes())),
1002                 m.name,
1003                 Type(mtype.getReturnType()),
1004                 TypeParams(mtype.getTypeArguments()),
1005                 null, // receiver type
1006                 Params(mtype.getParameterTypes(), m),
1007                 Types(mtype.getThrownTypes()),
1008                 body,
1009                 null,
1010                 m).setPos(pos).setType(mtype);
1011     }
1012 
1013     /** Create a type parameter tree from its name and type.
1014      */
1015     public JCTypeParameter TypeParam(Name name, TypeVar tvar) {
1016         return (JCTypeParameter)
1017             TypeParameter(name, Types(types.getBounds(tvar))).setPos(pos).setType(tvar);
1018     }
1019 
1020     /** Create a list of type parameter trees from a list of type variables.
1021      */
1022     public List<JCTypeParameter> TypeParams(List<Type> typarams) {
1023         ListBuffer<JCTypeParameter> tparams = new ListBuffer<>();
1024         for (List<Type> l = typarams; l.nonEmpty(); l = l.tail)
1025             tparams.append(TypeParam(l.head.tsym.name, (TypeVar)l.head));
1026         return tparams.toList();
1027     }
1028 
1029     /** Create a value parameter tree from its name, type, and owner.
1030      */
1031     public JCVariableDecl Param(Name name, Type argtype, Symbol owner) {
1032         return VarDef(new VarSymbol(PARAMETER, name, argtype, owner), null);
1033     }
1034 
1035     /** Create a a list of value parameter trees x0, ..., xn from a list of
1036      *  their types and an their owner.
1037      */
1038     public List<JCVariableDecl> Params(List<Type> argtypes, Symbol owner) {
1039         ListBuffer<JCVariableDecl> params = new ListBuffer<>();
1040         MethodSymbol mth = (owner.kind == MTH) ? ((MethodSymbol)owner) : null;
1041         if (mth != null && mth.params != null && argtypes.length() == mth.params.length()) {
1042             for (VarSymbol param : ((MethodSymbol)owner).params)
1043                 params.append(VarDef(param, null));
1044         } else {
1045             int i = 0;
1046             for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
1047                 params.append(Param(paramName(i++), l.head, owner));
1048         }
1049         return params.toList();
1050     }
1051 
1052     /** Wrap a method invocation in an expression statement or return statement,
1053      *  depending on whether the method invocation expression's type is void.
1054      */
1055     public JCStatement Call(JCExpression apply) {
1056         return apply.type.hasTag(VOID) ? Exec(apply) : Return(apply);
1057     }
1058 
1059     /** Construct an assignment from a variable symbol and a right hand side.
1060      */
1061     public JCStatement Assignment(Symbol v, JCExpression rhs) {
1062         return Exec(Assign(Ident(v), rhs).setType(v.type));
1063     }
1064 
1065     /** Construct an index expression from a variable and an expression.
1066      */
1067     public JCArrayAccess Indexed(Symbol v, JCExpression index) {
1068         JCArrayAccess tree = new JCArrayAccess(QualIdent(v), index);
1069         tree.type = ((ArrayType)v.type).elemtype;
1070         return tree;
1071     }
1072 
1073     /** Make an attributed type cast expression.
1074      */
1075     public JCTypeCast TypeCast(Type type, JCExpression expr) {
1076         return (JCTypeCast)TypeCast(Type(type), expr).setType(type);
1077     }
1078 
1079 /* ***************************************************************************
1080  * Helper methods.
1081  ****************************************************************************/
1082 
1083     /** Can given symbol be referred to in unqualified form?
1084      */
1085     boolean isUnqualifiable(Symbol sym) {
1086         if (sym.name == names.empty ||
1087             sym.owner == null ||
1088             sym.owner == syms.rootPackage ||
1089             sym.owner.kind == MTH || sym.owner.kind == VAR) {
1090             return true;
1091         } else if (sym.kind == TYP && toplevel != null) {
1092             Iterator<Symbol> it = toplevel.namedImportScope.getSymbolsByName(sym.name).iterator();
1093             if (it.hasNext()) {
1094                 Symbol s = it.next();
1095                 return
1096                   s == sym &&
1097                   !it.hasNext();
1098             }
1099             it = toplevel.packge.members().getSymbolsByName(sym.name).iterator();
1100             if (it.hasNext()) {
1101                 Symbol s = it.next();
1102                 return
1103                   s == sym &&
1104                   !it.hasNext();
1105             }
1106             it = toplevel.starImportScope.getSymbolsByName(sym.name).iterator();
1107             if (it.hasNext()) {
1108                 Symbol s = it.next();
1109                 return
1110                   s == sym &&
1111                   !it.hasNext();
1112             }
1113         }
1114         return false;
1115     }
1116 
1117     /** The name of synthetic parameter number `i'.
1118      */
1119     public Name paramName(int i)   { return names.fromString("x" + i); }
1120 
1121     /** The name of synthetic type parameter number `i'.
1122      */
1123     public Name typaramName(int i) { return names.fromString("A" + i); }
1124 }