1 /*
   2  * Copyright (c) 1999, 2021, 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;
  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 expressionStatement
 135                     && expressionStatement.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         JCVariableDecl tree = new JCVariableDecl(mods, name, vartype, init, null);
 226         tree.pos = pos;
 227         return tree;
 228     }
 229 
 230     public JCVariableDecl VarDef(JCModifiers mods, Name name, JCExpression vartype, JCExpression init, boolean declaredUsingVar) {
 231         JCVariableDecl tree = new JCVariableDecl(mods, name, vartype, init, null, declaredUsingVar);
 232         tree.pos = pos;
 233         return tree;
 234     }
 235 
 236     public JCVariableDecl ReceiverVarDef(JCModifiers mods, JCExpression name, JCExpression vartype) {
 237         JCVariableDecl tree = new JCVariableDecl(mods, name, vartype);
 238         tree.pos = pos;
 239         return tree;
 240     }
 241 
 242     public JCSkip Skip() {
 243         JCSkip tree = new JCSkip();
 244         tree.pos = pos;
 245         return tree;
 246     }
 247 
 248     public JCBlock Block(long flags, List<JCStatement> stats) {
 249         JCBlock tree = new JCBlock(flags, stats);
 250         tree.pos = pos;
 251         return tree;
 252     }
 253 
 254     public JCDoWhileLoop DoLoop(JCStatement body, JCExpression cond) {
 255         JCDoWhileLoop tree = new JCDoWhileLoop(body, cond);
 256         tree.pos = pos;
 257         return tree;
 258     }
 259 
 260     public JCWhileLoop WhileLoop(JCExpression cond, JCStatement body) {
 261         JCWhileLoop tree = new JCWhileLoop(cond, body);
 262         tree.pos = pos;
 263         return tree;
 264     }
 265 
 266     public JCWithField WithField(JCExpression field, JCExpression value) {
 267         JCWithField tree = new JCWithField(field, value);
 268         tree.pos = pos;
 269         return tree;
 270     }
 271 
 272     public JCForLoop ForLoop(List<JCStatement> init,
 273                            JCExpression cond,
 274                            List<JCExpressionStatement> step,
 275                            JCStatement body)
 276     {
 277         JCForLoop tree = new JCForLoop(init, cond, step, body);
 278         tree.pos = pos;
 279         return tree;
 280     }
 281 
 282     public JCEnhancedForLoop ForeachLoop(JCVariableDecl var, JCExpression expr, JCStatement body) {
 283         JCEnhancedForLoop tree = new JCEnhancedForLoop(var, expr, body);
 284         tree.pos = pos;
 285         return tree;
 286     }
 287 
 288     public JCLabeledStatement Labelled(Name label, JCStatement body) {
 289         JCLabeledStatement tree = new JCLabeledStatement(label, body);
 290         tree.pos = pos;
 291         return tree;
 292     }
 293 
 294     public JCSwitch Switch(JCExpression selector, List<JCCase> cases) {
 295         JCSwitch tree = new JCSwitch(selector, cases);
 296         tree.pos = pos;
 297         return tree;
 298     }
 299 
 300     public JCCase Case(CaseTree.CaseKind caseKind, List<JCCaseLabel> labels,
 301                        List<JCStatement> stats, JCTree body) {
 302         JCCase tree = new JCCase(caseKind, labels, stats, body);
 303         tree.pos = pos;
 304         return tree;
 305     }
 306 
 307     public JCDefaultValue DefaultValue(JCExpression type) {
 308         JCDefaultValue tree = new JCDefaultValue(type);
 309         tree.pos = pos;
 310         return tree;
 311     }
 312 
 313     public JCSwitchExpression SwitchExpression(JCExpression selector, List<JCCase> cases) {
 314         JCSwitchExpression tree = new JCSwitchExpression(selector, cases);
 315         tree.pos = pos;
 316         return tree;
 317     }
 318 
 319     public JCSynchronized Synchronized(JCExpression lock, JCBlock body) {
 320         JCSynchronized tree = new JCSynchronized(lock, body);
 321         tree.pos = pos;
 322         return tree;
 323     }
 324 
 325     public JCTry Try(JCBlock body, List<JCCatch> catchers, JCBlock finalizer) {
 326         return Try(List.nil(), body, catchers, finalizer);
 327     }
 328 
 329     public JCTry Try(List<JCTree> resources,
 330                      JCBlock body,
 331                      List<JCCatch> catchers,
 332                      JCBlock finalizer) {
 333         JCTry tree = new JCTry(resources, body, catchers, finalizer);
 334         tree.pos = pos;
 335         return tree;
 336     }
 337 
 338     public JCCatch Catch(JCVariableDecl param, JCBlock body) {
 339         JCCatch tree = new JCCatch(param, body);
 340         tree.pos = pos;
 341         return tree;
 342     }
 343 
 344     public JCConditional Conditional(JCExpression cond,
 345                                    JCExpression thenpart,
 346                                    JCExpression elsepart)
 347     {
 348         JCConditional tree = new JCConditional(cond, thenpart, elsepart);
 349         tree.pos = pos;
 350         return tree;
 351     }
 352 
 353     public JCIf If(JCExpression cond, JCStatement thenpart, JCStatement elsepart) {
 354         JCIf tree = new JCIf(cond, thenpart, elsepart);
 355         tree.pos = pos;
 356         return tree;
 357     }
 358 
 359     public JCExpressionStatement Exec(JCExpression expr) {
 360         JCExpressionStatement tree = new JCExpressionStatement(expr);
 361         tree.pos = pos;
 362         return tree;
 363     }
 364 
 365     public JCBreak Break(Name label) {
 366         JCBreak tree = new JCBreak(label, null);
 367         tree.pos = pos;
 368         return tree;
 369     }
 370 
 371     public JCYield Yield(JCExpression value) {
 372         JCYield tree = new JCYield(value, null);
 373         tree.pos = pos;
 374         return tree;
 375     }
 376 
 377     public JCContinue Continue(Name label) {
 378         JCContinue tree = new JCContinue(label, null);
 379         tree.pos = pos;
 380         return tree;
 381     }
 382 
 383     public JCReturn Return(JCExpression expr) {
 384         JCReturn tree = new JCReturn(expr);
 385         tree.pos = pos;
 386         return tree;
 387     }
 388 
 389     public JCThrow Throw(JCExpression expr) {
 390         JCThrow tree = new JCThrow(expr);
 391         tree.pos = pos;
 392         return tree;
 393     }
 394 
 395     public JCAssert Assert(JCExpression cond, JCExpression detail) {
 396         JCAssert tree = new JCAssert(cond, detail);
 397         tree.pos = pos;
 398         return tree;
 399     }
 400 
 401     public JCMethodInvocation Apply(List<JCExpression> typeargs,
 402                        JCExpression fn,
 403                        List<JCExpression> args)
 404     {
 405         JCMethodInvocation tree = new JCMethodInvocation(typeargs, fn, args);
 406         tree.pos = pos;
 407         return tree;
 408     }
 409 
 410     public JCNewClass NewClass(JCExpression encl,
 411                              List<JCExpression> typeargs,
 412                              JCExpression clazz,
 413                              List<JCExpression> args,
 414                              JCClassDecl def)
 415     {
 416         return SpeculativeNewClass(encl, typeargs, clazz, args, def, false);
 417     }
 418 
 419     public JCNewClass SpeculativeNewClass(JCExpression encl,
 420                              List<JCExpression> typeargs,
 421                              JCExpression clazz,
 422                              List<JCExpression> args,
 423                              JCClassDecl def,
 424                              boolean classDefRemoved)
 425     {
 426         JCNewClass tree = classDefRemoved ?
 427                 new JCNewClass(encl, typeargs, clazz, args, def) {
 428                     @Override
 429                     public boolean classDeclRemoved() {
 430                         return true;
 431                     }
 432                 } :
 433                 new JCNewClass(encl, typeargs, clazz, args, def);
 434         tree.pos = pos;
 435         return tree;
 436     }
 437 
 438     public JCNewArray NewArray(JCExpression elemtype,
 439                              List<JCExpression> dims,
 440                              List<JCExpression> elems)
 441     {
 442         JCNewArray tree = new JCNewArray(elemtype, dims, elems);
 443         tree.pos = pos;
 444         return tree;
 445     }
 446 
 447     public JCLambda Lambda(List<JCVariableDecl> params,
 448                            JCTree body)
 449     {
 450         JCLambda tree = new JCLambda(params, body);
 451         tree.pos = pos;
 452         return tree;
 453     }
 454 
 455     public JCParens Parens(JCExpression expr) {
 456         JCParens tree = new JCParens(expr);
 457         tree.pos = pos;
 458         return tree;
 459     }
 460 
 461     public JCAssign Assign(JCExpression lhs, JCExpression rhs) {
 462         JCAssign tree = new JCAssign(lhs, rhs);
 463         tree.pos = pos;
 464         return tree;
 465     }
 466 
 467     public JCAssignOp Assignop(JCTree.Tag opcode, JCTree lhs, JCTree rhs) {
 468         JCAssignOp tree = new JCAssignOp(opcode, lhs, rhs, null);
 469         tree.pos = pos;
 470         return tree;
 471     }
 472 
 473     public JCUnary Unary(JCTree.Tag opcode, JCExpression arg) {
 474         JCUnary tree = new JCUnary(opcode, arg);
 475         tree.pos = pos;
 476         return tree;
 477     }
 478 
 479     public JCBinary Binary(JCTree.Tag opcode, JCExpression lhs, JCExpression rhs) {
 480         JCBinary tree = new JCBinary(opcode, lhs, rhs, null);
 481         tree.pos = pos;
 482         return tree;
 483     }
 484 
 485     public JCTypeCast TypeCast(JCTree clazz, JCExpression expr) {
 486         JCTypeCast tree = new JCTypeCast(clazz, expr);
 487         tree.pos = pos;
 488         return tree;
 489     }
 490 
 491     public JCInstanceOf TypeTest(JCExpression expr, JCTree clazz) {
 492         JCInstanceOf tree = new JCInstanceOf(expr, clazz);
 493         tree.pos = pos;
 494         return tree;
 495     }
 496 
 497     public JCBindingPattern BindingPattern(JCVariableDecl var) {
 498         JCBindingPattern tree = new JCBindingPattern(var);
 499         tree.pos = pos;
 500         return tree;
 501     }
 502 
 503     public JCDefaultCaseLabel DefaultCaseLabel() {
 504         JCDefaultCaseLabel tree = new JCDefaultCaseLabel();
 505         tree.pos = pos;
 506         return tree;
 507     }
 508 
 509     public JCParenthesizedPattern ParenthesizedPattern(JCPattern pattern) {
 510         JCParenthesizedPattern tree = new JCParenthesizedPattern(pattern);
 511         tree.pos = pos;
 512         return tree;
 513     }
 514 
 515     public JCGuardPattern GuardPattern(JCPattern guardedPattern, JCExpression expr) {
 516         JCGuardPattern tree = new JCGuardPattern(guardedPattern, expr);
 517         tree.pos = pos;
 518         return tree;
 519     }
 520 
 521     public JCArrayAccess Indexed(JCExpression indexed, JCExpression index) {
 522         JCArrayAccess tree = new JCArrayAccess(indexed, index);
 523         tree.pos = pos;
 524         return tree;
 525     }
 526 
 527     public JCFieldAccess Select(JCExpression selected, Name selector) {
 528         JCFieldAccess tree = new JCFieldAccess(selected, selector, null);
 529         tree.pos = pos;
 530         return tree;
 531     }
 532 
 533     public JCMemberReference Reference(JCMemberReference.ReferenceMode mode, Name name,
 534             JCExpression expr, List<JCExpression> typeargs) {
 535         JCMemberReference tree = new JCMemberReference(mode, name, expr, typeargs);
 536         tree.pos = pos;
 537         return tree;
 538     }
 539 
 540     public JCIdent Ident(Name name) {
 541         JCIdent tree = new JCIdent(name, null);
 542         tree.pos = pos;
 543         return tree;
 544     }
 545 
 546     public JCLiteral Literal(TypeTag tag, Object value) {
 547         JCLiteral tree = new JCLiteral(tag, value);
 548         tree.pos = pos;
 549         return tree;
 550     }
 551 
 552     public JCPrimitiveTypeTree TypeIdent(TypeTag typetag) {
 553         JCPrimitiveTypeTree tree = new JCPrimitiveTypeTree(typetag);
 554         tree.pos = pos;
 555         return tree;
 556     }
 557 
 558     public JCArrayTypeTree TypeArray(JCExpression elemtype) {
 559         JCArrayTypeTree tree = new JCArrayTypeTree(elemtype);
 560         tree.pos = pos;
 561         return tree;
 562     }
 563 
 564     public JCTypeApply TypeApply(JCExpression clazz, List<JCExpression> arguments) {
 565         JCTypeApply tree = new JCTypeApply(clazz, arguments);
 566         tree.pos = pos;
 567         return tree;
 568     }
 569 
 570     public JCTypeUnion TypeUnion(List<JCExpression> components) {
 571         JCTypeUnion tree = new JCTypeUnion(components);
 572         tree.pos = pos;
 573         return tree;
 574     }
 575 
 576     public JCTypeIntersection TypeIntersection(List<JCExpression> components) {
 577         JCTypeIntersection tree = new JCTypeIntersection(components);
 578         tree.pos = pos;
 579         return tree;
 580     }
 581 
 582     public JCTypeParameter TypeParameter(Name name, List<JCExpression> bounds) {
 583         return TypeParameter(name, bounds, List.nil());
 584     }
 585 
 586     public JCTypeParameter TypeParameter(Name name, List<JCExpression> bounds, List<JCAnnotation> annos) {
 587         JCTypeParameter tree = new JCTypeParameter(name, bounds, annos);
 588         tree.pos = pos;
 589         return tree;
 590     }
 591 
 592     public JCWildcard Wildcard(TypeBoundKind kind, JCTree type) {
 593         JCWildcard tree = new JCWildcard(kind, type);
 594         tree.pos = pos;
 595         return tree;
 596     }
 597 
 598     public TypeBoundKind TypeBoundKind(BoundKind kind) {
 599         TypeBoundKind tree = new TypeBoundKind(kind);
 600         tree.pos = pos;
 601         return tree;
 602     }
 603 
 604     public JCAnnotation Annotation(JCTree annotationType, List<JCExpression> args) {
 605         JCAnnotation tree = new JCAnnotation(Tag.ANNOTATION, annotationType, args);
 606         tree.pos = pos;
 607         return tree;
 608     }
 609 
 610     public JCAnnotation TypeAnnotation(JCTree annotationType, List<JCExpression> args) {
 611         JCAnnotation tree = new JCAnnotation(Tag.TYPE_ANNOTATION, annotationType, args);
 612         tree.pos = pos;
 613         return tree;
 614     }
 615 
 616     public JCModifiers Modifiers(long flags, List<JCAnnotation> annotations) {
 617         JCModifiers tree = new JCModifiers(flags, annotations);
 618         boolean noFlags = (flags & (Flags.ModifierFlags | Flags.ANNOTATION)) == 0;
 619         tree.pos = (noFlags && annotations.isEmpty()) ? Position.NOPOS : pos;
 620         return tree;
 621     }
 622 
 623     public JCModifiers Modifiers(long flags) {
 624         return Modifiers(flags, List.nil());
 625     }
 626 
 627     @Override
 628     public JCModuleDecl ModuleDef(JCModifiers mods, ModuleKind kind,
 629             JCExpression qualid, List<JCDirective> directives) {
 630         JCModuleDecl tree = new JCModuleDecl(mods, kind, qualid, directives);
 631         tree.pos = pos;
 632         return tree;
 633     }
 634 
 635     @Override
 636     public JCExports Exports(JCExpression qualId, List<JCExpression> moduleNames) {
 637         JCExports tree = new JCExports(qualId, moduleNames);
 638         tree.pos = pos;
 639         return tree;
 640     }
 641 
 642     @Override
 643     public JCOpens Opens(JCExpression qualId, List<JCExpression> moduleNames) {
 644         JCOpens tree = new JCOpens(qualId, moduleNames);
 645         tree.pos = pos;
 646         return tree;
 647     }
 648 
 649     @Override
 650     public JCProvides Provides(JCExpression serviceName, List<JCExpression> implNames) {
 651         JCProvides tree = new JCProvides(serviceName, implNames);
 652         tree.pos = pos;
 653         return tree;
 654     }
 655 
 656     @Override
 657     public JCRequires Requires(boolean isTransitive, boolean isStaticPhase, JCExpression qualId) {
 658         JCRequires tree = new JCRequires(isTransitive, isStaticPhase, qualId);
 659         tree.pos = pos;
 660         return tree;
 661     }
 662 
 663     @Override
 664     public JCUses Uses(JCExpression qualId) {
 665         JCUses tree = new JCUses(qualId);
 666         tree.pos = pos;
 667         return tree;
 668     }
 669 
 670     public JCAnnotatedType AnnotatedType(List<JCAnnotation> annotations, JCExpression underlyingType) {
 671         JCAnnotatedType tree = new JCAnnotatedType(annotations, underlyingType);
 672         tree.pos = pos;
 673         return tree;
 674     }
 675 
 676     public JCErroneous Erroneous() {
 677         return Erroneous(List.nil());
 678     }
 679 
 680     public JCErroneous Erroneous(List<? extends JCTree> errs) {
 681         JCErroneous tree = new JCErroneous(errs);
 682         tree.pos = pos;
 683         return tree;
 684     }
 685 
 686     public LetExpr LetExpr(List<JCStatement> defs, JCExpression expr) {
 687         LetExpr tree = new LetExpr(defs, expr);
 688         tree.pos = pos;
 689         return tree;
 690     }
 691 
 692 /* ***************************************************************************
 693  * Derived building blocks.
 694  ****************************************************************************/
 695 
 696     public JCClassDecl AnonymousClassDef(JCModifiers mods,
 697                                          List<JCTree> defs)
 698     {
 699         return ClassDef(mods,
 700                         names.empty,
 701                         List.nil(),
 702                         null,
 703                         List.nil(),
 704                         defs);
 705     }
 706 
 707     public LetExpr LetExpr(JCVariableDecl def, JCExpression expr) {
 708         LetExpr tree = new LetExpr(List.of(def), expr);
 709         tree.pos = pos;
 710         return tree;
 711     }
 712 
 713     /** Create an identifier from a symbol.
 714      */
 715     public JCIdent Ident(Symbol sym) {
 716         return (JCIdent)new JCIdent((sym.name != names.empty)
 717                                 ? sym.name
 718                                 : sym.flatName(), sym)
 719             .setPos(pos)
 720             .setType(sym.type);
 721     }
 722 
 723     /** Create a selection node from a qualifier tree and a symbol.
 724      *  @param base   The qualifier tree.
 725      */
 726     public JCExpression Select(JCExpression base, Symbol sym) {
 727         return new JCFieldAccess(base, sym.name, sym).setPos(pos).setType(sym.type);
 728     }
 729 
 730     /** Create a qualified identifier from a symbol, adding enough qualifications
 731      *  to make the reference unique.
 732      */
 733     public JCExpression QualIdent(Symbol sym) {
 734         return isUnqualifiable(sym)
 735             ? Ident(sym)
 736             : Select(QualIdent(sym.owner), sym);
 737     }
 738 
 739     /** Create an identifier that refers to the variable declared in given variable
 740      *  declaration.
 741      */
 742     public JCExpression Ident(JCVariableDecl param) {
 743         return Ident(param.sym);
 744     }
 745 
 746     /** Create a list of identifiers referring to the variables declared
 747      *  in given list of variable declarations.
 748      */
 749     public List<JCExpression> Idents(List<JCVariableDecl> params) {
 750         ListBuffer<JCExpression> ids = new ListBuffer<>();
 751         for (List<JCVariableDecl> l = params; l.nonEmpty(); l = l.tail)
 752             ids.append(Ident(l.head));
 753         return ids.toList();
 754     }
 755 
 756     /** Create a tree representing `this', given its type.
 757      */
 758     public JCExpression This(Type t) {
 759         return Ident(new VarSymbol(FINAL, names._this, t, t.tsym));
 760     }
 761 
 762     /** Create a tree representing qualified `this' given its type
 763      */
 764     public JCExpression QualThis(Type t) {
 765         return Select(Type(t), new VarSymbol(FINAL, names._this, t, t.tsym));
 766     }
 767 
 768     /** Create a tree representing a class literal.
 769      */
 770     public JCExpression ClassLiteral(ClassSymbol clazz) {
 771         return ClassLiteral(clazz.type);
 772     }
 773 
 774     /** Create a tree representing a class literal.
 775      */
 776     public JCExpression ClassLiteral(Type t) {
 777         VarSymbol lit = new VarSymbol(STATIC | PUBLIC | FINAL,
 778                                       names._class,
 779                                       t,
 780                                       t.tsym);
 781         return Select(Type(t), lit);
 782     }
 783 
 784     /** Create a tree representing `super', given its type and owner.
 785      */
 786     public JCIdent Super(Type t, TypeSymbol owner) {
 787         return Ident(new VarSymbol(FINAL, names._super, t, owner));
 788     }
 789 
 790     /**
 791      * Create a method invocation from a method tree and a list of
 792      * argument trees.
 793      */
 794     public JCMethodInvocation App(JCExpression meth, List<JCExpression> args) {
 795         return Apply(null, meth, args).setType(meth.type.getReturnType());
 796     }
 797 
 798     /**
 799      * Create a no-arg method invocation from a method tree
 800      */
 801     public JCMethodInvocation App(JCExpression meth) {
 802         return Apply(null, meth, List.nil()).setType(meth.type.getReturnType());
 803     }
 804 
 805     /** Create a method invocation from a method tree and a list of argument trees.
 806      */
 807     public JCExpression Create(Symbol ctor, List<JCExpression> args) {
 808         Type t = ctor.owner.erasure(types);
 809         JCNewClass newclass = NewClass(null, null, Type(t), args, null);
 810         newclass.constructor = ctor;
 811         newclass.setType(t);
 812         return newclass;
 813     }
 814 
 815     /** Create a tree representing given type.
 816      */
 817     public JCExpression Type(Type t) {
 818         if (t == null) return null;
 819         JCExpression tp;
 820         switch (t.getTag()) {
 821         case BYTE: case CHAR: case SHORT: case INT: case LONG: case FLOAT:
 822         case DOUBLE: case BOOLEAN: case VOID:
 823             tp = TypeIdent(t.getTag());
 824             break;
 825         case TYPEVAR:
 826             tp = Ident(t.tsym);
 827             break;
 828         case WILDCARD: {
 829             WildcardType a = ((WildcardType) t);
 830             tp = Wildcard(TypeBoundKind(a.kind), a.kind == BoundKind.UNBOUND ? null : Type(a.type));
 831             break;
 832         }
 833         case CLASS:
 834             switch (t.getKind()) {
 835             case UNION: {
 836                 UnionClassType tu = (UnionClassType)t;
 837                 ListBuffer<JCExpression> la = new ListBuffer<>();
 838                 for (Type ta : tu.getAlternativeTypes()) {
 839                     la.add(Type(ta));
 840                 }
 841                 tp = TypeUnion(la.toList());
 842                 break;
 843             }
 844             case INTERSECTION: {
 845                 IntersectionClassType it = (IntersectionClassType)t;
 846                 ListBuffer<JCExpression> la = new ListBuffer<>();
 847                 for (Type ta : it.getExplicitComponents()) {
 848                     la.add(Type(ta));
 849                 }
 850                 tp = TypeIntersection(la.toList());
 851                 break;
 852             }
 853             default: {
 854                 if (t.isReferenceProjection()) {
 855                     // For parameterized types, we want V.ref<A1 ... An> not V<A1 ... An>.ref
 856                     JCExpression vp = Type(t.asValueType());
 857                     if (vp.hasTag(Tag.TYPEAPPLY)) {
 858                         // vp now is V<A1 ... An>, build V.ref<A1 ... An>
 859                         JCFieldAccess f = (JCFieldAccess) Select(((JCTypeApply) vp).clazz, t.tsym);
 860                         f.name = names.ref;
 861                         tp = TypeApply(f, ((JCTypeApply) vp).arguments);
 862                     } else {
 863                         JCFieldAccess f = (JCFieldAccess) Select(vp, t.tsym);
 864                         f.name = names.ref;
 865                         tp = f;
 866                     }
 867                 } else if (t.isValueProjection()) {
 868                     // For parameterized types, we want V.val<A1 ... An> not V<A1 ... An>.val
 869                     JCExpression vp = Type(t.referenceProjection());
 870                     if (vp.hasTag(Tag.TYPEAPPLY)) {
 871                         // vp now is V<A1 ... An>, build V.val<A1 ... An>
 872                         JCFieldAccess f = (JCFieldAccess) Select(((JCTypeApply) vp).clazz, t.tsym);
 873                         f.name = names.val;
 874                         tp = TypeApply(f, ((JCTypeApply) vp).arguments);
 875                     } else {
 876                         JCFieldAccess f = (JCFieldAccess) Select(vp, t.tsym);
 877                         f.name = names.val;
 878                         tp = f;
 879                     }
 880                 } else {
 881                     Type outer = t.getEnclosingType();
 882                     JCExpression clazz = outer.hasTag(CLASS) && t.tsym.owner.kind == TYP
 883                             ? Select(Type(outer), t.tsym)
 884                             : QualIdent(t.tsym);
 885                     tp = t.getTypeArguments().isEmpty()
 886                             ? clazz
 887                             : TypeApply(clazz, Types(t.getTypeArguments()));
 888                 }
 889                 break;
 890             }
 891             }
 892             break;
 893         case ARRAY:
 894             tp = TypeArray(Type(types.elemtype(t)));
 895             break;
 896         case ERROR:
 897             tp = TypeIdent(ERROR);
 898             break;
 899         default:
 900             throw new AssertionError("unexpected type: " + t);
 901         }
 902         return tp.setType(t);
 903     }
 904 
 905     /** Create a list of trees representing given list of types.
 906      */
 907     public List<JCExpression> Types(List<Type> ts) {
 908         ListBuffer<JCExpression> lb = new ListBuffer<>();
 909         for (List<Type> l = ts; l.nonEmpty(); l = l.tail)
 910             lb.append(Type(l.head));
 911         return lb.toList();
 912     }
 913 
 914     /** Create a variable definition from a variable symbol and an initializer
 915      *  expression.
 916      */
 917     public JCVariableDecl VarDef(VarSymbol v, JCExpression init) {
 918         return (JCVariableDecl)
 919             new JCVariableDecl(
 920                 Modifiers(v.flags(), Annotations(v.getRawAttributes())),
 921                 v.name,
 922                 Type(v.type),
 923                 init,
 924                 v).setPos(pos).setType(v.type);
 925     }
 926 
 927     /** Create annotation trees from annotations.
 928      */
 929     public List<JCAnnotation> Annotations(List<Attribute.Compound> attributes) {
 930         if (attributes == null) return List.nil();
 931         ListBuffer<JCAnnotation> result = new ListBuffer<>();
 932         for (List<Attribute.Compound> i = attributes; i.nonEmpty(); i=i.tail) {
 933             Attribute a = i.head;
 934             result.append(Annotation(a));
 935         }
 936         return result.toList();
 937     }
 938 
 939     public JCLiteral Literal(Object value) {
 940         JCLiteral result = null;
 941         if (value instanceof String) {
 942             result = Literal(CLASS, value).
 943                 setType(syms.stringType.constType(value));
 944         } else if (value instanceof Integer) {
 945             result = Literal(INT, value).
 946                 setType(syms.intType.constType(value));
 947         } else if (value instanceof Long) {
 948             result = Literal(LONG, value).
 949                 setType(syms.longType.constType(value));
 950         } else if (value instanceof Byte) {
 951             result = Literal(BYTE, value).
 952                 setType(syms.byteType.constType(value));
 953         } else if (value instanceof Character charVal) {
 954             int v = charVal.toString().charAt(0);
 955             result = Literal(CHAR, v).
 956                 setType(syms.charType.constType(v));
 957         } else if (value instanceof Double) {
 958             result = Literal(DOUBLE, value).
 959                 setType(syms.doubleType.constType(value));
 960         } else if (value instanceof Float) {
 961             result = Literal(FLOAT, value).
 962                 setType(syms.floatType.constType(value));
 963         } else if (value instanceof Short) {
 964             result = Literal(SHORT, value).
 965                 setType(syms.shortType.constType(value));
 966         } else if (value instanceof Boolean boolVal) {
 967             int v = boolVal ? 1 : 0;
 968             result = Literal(BOOLEAN, v).
 969                 setType(syms.booleanType.constType(v));
 970         } else {
 971             throw new AssertionError(value);
 972         }
 973         return result;
 974     }
 975 
 976     class AnnotationBuilder implements Attribute.Visitor {
 977         JCExpression result = null;
 978         public void visitConstant(Attribute.Constant v) {
 979             result = Literal(v.type.getTag(), v.value);
 980         }
 981         public void visitClass(Attribute.Class clazz) {
 982             result = ClassLiteral(clazz.classType).setType(syms.classType);
 983         }
 984         public void visitEnum(Attribute.Enum e) {
 985             result = QualIdent(e.value);
 986         }
 987         public void visitError(Attribute.Error e) {
 988             if (e instanceof UnresolvedClass unresolvedClass) {
 989                 result = ClassLiteral(unresolvedClass.classType).setType(syms.classType);
 990             } else {
 991                 result = Erroneous();
 992             }
 993         }
 994         public void visitCompound(Attribute.Compound compound) {
 995             if (compound instanceof Attribute.TypeCompound typeCompound) {
 996                 result = visitTypeCompoundInternal(typeCompound);
 997             } else {
 998                 result = visitCompoundInternal(compound);
 999             }
1000         }
1001         public JCAnnotation visitCompoundInternal(Attribute.Compound compound) {
1002             ListBuffer<JCExpression> args = new ListBuffer<>();
1003             for (List<Pair<Symbol.MethodSymbol,Attribute>> values = compound.values; values.nonEmpty(); values=values.tail) {
1004                 Pair<MethodSymbol,Attribute> pair = values.head;
1005                 JCExpression valueTree = translate(pair.snd);
1006                 args.append(Assign(Ident(pair.fst), valueTree).setType(valueTree.type));
1007             }
1008             return Annotation(Type(compound.type), args.toList());
1009         }
1010         public JCAnnotation visitTypeCompoundInternal(Attribute.TypeCompound compound) {
1011             ListBuffer<JCExpression> args = new ListBuffer<>();
1012             for (List<Pair<Symbol.MethodSymbol,Attribute>> values = compound.values; values.nonEmpty(); values=values.tail) {
1013                 Pair<MethodSymbol,Attribute> pair = values.head;
1014                 JCExpression valueTree = translate(pair.snd);
1015                 args.append(Assign(Ident(pair.fst), valueTree).setType(valueTree.type));
1016             }
1017             return TypeAnnotation(Type(compound.type), args.toList());
1018         }
1019         public void visitArray(Attribute.Array array) {
1020             ListBuffer<JCExpression> elems = new ListBuffer<>();
1021             for (int i = 0; i < array.values.length; i++)
1022                 elems.append(translate(array.values[i]));
1023             result = NewArray(null, List.nil(), elems.toList()).setType(array.type);
1024         }
1025         JCExpression translate(Attribute a) {
1026             a.accept(this);
1027             return result;
1028         }
1029         JCAnnotation translate(Attribute.Compound a) {
1030             return visitCompoundInternal(a);
1031         }
1032         JCAnnotation translate(Attribute.TypeCompound a) {
1033             return visitTypeCompoundInternal(a);
1034         }
1035     }
1036 
1037     AnnotationBuilder annotationBuilder = new AnnotationBuilder();
1038 
1039     /** Create an annotation tree from an attribute.
1040      */
1041     public JCAnnotation Annotation(Attribute a) {
1042         return annotationBuilder.translate((Attribute.Compound)a);
1043     }
1044 
1045     public JCAnnotation TypeAnnotation(Attribute a) {
1046         return annotationBuilder.translate((Attribute.TypeCompound) a);
1047     }
1048 
1049     /** Create a method definition from a method symbol and a method body.
1050      */
1051     public JCMethodDecl MethodDef(MethodSymbol m, JCBlock body) {
1052         return MethodDef(m, m.type, body);
1053     }
1054 
1055     /** Create a method definition from a method symbol, method type
1056      *  and a method body.
1057      */
1058     public JCMethodDecl MethodDef(MethodSymbol m, Type mtype, JCBlock body) {
1059         return (JCMethodDecl)
1060             new JCMethodDecl(
1061                 Modifiers(m.flags(), Annotations(m.getRawAttributes())),
1062                 m.name,
1063                 m.name != names.init ? Type(mtype.getReturnType()) : null,
1064                 TypeParams(mtype.getTypeArguments()),
1065                 null, // receiver type
1066                 Params(mtype.getParameterTypes(), m),
1067                 Types(mtype.getThrownTypes()),
1068                 body,
1069                 null,
1070                 m).setPos(pos).setType(mtype);
1071     }
1072 
1073     /** Create a type parameter tree from its name and type.
1074      */
1075     public JCTypeParameter TypeParam(Name name, TypeVar tvar) {
1076         return (JCTypeParameter)
1077             TypeParameter(name, Types(types.getBounds(tvar))).setPos(pos).setType(tvar);
1078     }
1079 
1080     /** Create a list of type parameter trees from a list of type variables.
1081      */
1082     public List<JCTypeParameter> TypeParams(List<Type> typarams) {
1083         ListBuffer<JCTypeParameter> tparams = new ListBuffer<>();
1084         for (List<Type> l = typarams; l.nonEmpty(); l = l.tail)
1085             tparams.append(TypeParam(l.head.tsym.name, (TypeVar)l.head));
1086         return tparams.toList();
1087     }
1088 
1089     /** Create a value parameter tree from its name, type, and owner.
1090      */
1091     public JCVariableDecl Param(Name name, Type argtype, Symbol owner) {
1092         return VarDef(new VarSymbol(PARAMETER, name, argtype, owner), null);
1093     }
1094 
1095     /** Create a a list of value parameter trees x0, ..., xn from a list of
1096      *  their types and an their owner.
1097      */
1098     public List<JCVariableDecl> Params(List<Type> argtypes, Symbol owner) {
1099         ListBuffer<JCVariableDecl> params = new ListBuffer<>();
1100         MethodSymbol mth = (owner.kind == MTH) ? ((MethodSymbol)owner) : null;
1101         if (mth != null && mth.params != null && argtypes.length() == mth.params.length()) {
1102             for (VarSymbol param : ((MethodSymbol)owner).params)
1103                 params.append(VarDef(param, null));
1104         } else {
1105             int i = 0;
1106             for (List<Type> l = argtypes; l.nonEmpty(); l = l.tail)
1107                 params.append(Param(paramName(i++), l.head, owner));
1108         }
1109         return params.toList();
1110     }
1111 
1112     /** Wrap a method invocation in an expression statement or return statement,
1113      *  depending on whether the method invocation expression's type is void.
1114      */
1115     public JCStatement Call(JCExpression apply) {
1116         return apply.type.hasTag(VOID) ? Exec(apply) : Return(apply);
1117     }
1118 
1119     /** Construct an assignment from a variable symbol and a right hand side.
1120      */
1121     public JCStatement Assignment(Symbol v, JCExpression rhs) {
1122         return Exec(Assign(Ident(v), rhs).setType(v.type));
1123     }
1124 
1125     /** Construct an index expression from a variable and an expression.
1126      */
1127     public JCArrayAccess Indexed(Symbol v, JCExpression index) {
1128         JCArrayAccess tree = new JCArrayAccess(QualIdent(v), index);
1129         tree.type = ((ArrayType)v.type).elemtype;
1130         return tree;
1131     }
1132 
1133     /** Make an attributed type cast expression.
1134      */
1135     public JCTypeCast TypeCast(Type type, JCExpression expr) {
1136         return (JCTypeCast)TypeCast(Type(type), expr).setType(type);
1137     }
1138 
1139 /* ***************************************************************************
1140  * Helper methods.
1141  ****************************************************************************/
1142 
1143     /** Can given symbol be referred to in unqualified form?
1144      */
1145     boolean isUnqualifiable(Symbol sym) {
1146         if (sym.name == names.empty ||
1147             sym.owner == null ||
1148             sym.owner == syms.rootPackage ||
1149             sym.owner.kind == MTH || sym.owner.kind == VAR) {
1150             return true;
1151         } else if (sym.kind == TYP && toplevel != null) {
1152             Iterator<Symbol> it = toplevel.namedImportScope.getSymbolsByName(sym.name).iterator();
1153             if (it.hasNext()) {
1154                 Symbol s = it.next();
1155                 return
1156                   s == sym &&
1157                   !it.hasNext();
1158             }
1159             it = toplevel.packge.members().getSymbolsByName(sym.name).iterator();
1160             if (it.hasNext()) {
1161                 Symbol s = it.next();
1162                 return
1163                   s == sym &&
1164                   !it.hasNext();
1165             }
1166             it = toplevel.starImportScope.getSymbolsByName(sym.name).iterator();
1167             if (it.hasNext()) {
1168                 Symbol s = it.next();
1169                 return
1170                   s == sym &&
1171                   !it.hasNext();
1172             }
1173         }
1174         return false;
1175     }
1176 
1177     /** The name of synthetic parameter number `i'.
1178      */
1179     public Name paramName(int i)   { return names.fromString("x" + i); }
1180 
1181     /** The name of synthetic type parameter number `i'.
1182      */
1183     public Name typaramName(int i) { return names.fromString("A" + i); }
1184 }