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