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.io.IOException;
  29 import java.io.StringWriter;
  30 import java.util.*;
  31 
  32 import javax.lang.model.element.Modifier;
  33 import javax.lang.model.type.TypeKind;
  34 import javax.tools.JavaFileObject;
  35 
  36 import com.sun.source.tree.*;
  37 import com.sun.tools.javac.code.*;
  38 import com.sun.tools.javac.code.Directive.RequiresDirective;
  39 import com.sun.tools.javac.code.Scope.*;
  40 import com.sun.tools.javac.code.Symbol.*;
  41 import com.sun.tools.javac.util.*;
  42 import com.sun.tools.javac.util.DefinedBy.Api;
  43 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
  44 import com.sun.tools.javac.util.List;
  45 
  46 import static com.sun.tools.javac.tree.JCTree.Tag.*;
  47 
  48 import javax.tools.JavaFileManager.Location;
  49 
  50 import com.sun.source.tree.ModuleTree.ModuleKind;
  51 import com.sun.tools.javac.code.Directive.ExportsDirective;
  52 import com.sun.tools.javac.code.Directive.OpensDirective;
  53 import com.sun.tools.javac.code.Type.ModuleType;
  54 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.PolyKind;
  55 
  56 /**
  57  * Root class for abstract syntax tree nodes. It provides definitions
  58  * for specific tree nodes as subclasses nested inside.
  59  *
  60  * <p>Each subclass is highly standardized.  It generally contains
  61  * only tree fields for the syntactic subcomponents of the node.  Some
  62  * classes that represent identifier uses or definitions also define a
  63  * Symbol field that denotes the represented identifier.  Classes for
  64  * non-local jumps also carry the jump target as a field.  The root
  65  * class Tree itself defines fields for the tree's type and position.
  66  * No other fields are kept in a tree node; instead parameters are
  67  * passed to methods accessing the node.
  68  *
  69  * <p>Except for the methods defined by com.sun.source, the only
  70  * method defined in subclasses is `visit' which applies a given
  71  * visitor to the tree. The actual tree processing is done by visitor
  72  * classes in other packages. The abstract class Visitor, as well as
  73  * an Factory interface for trees, are defined as inner classes in
  74  * Tree.
  75  *
  76  * <p>To avoid ambiguities with the Tree API in com.sun.source all sub
  77  * classes should, by convention, start with JC (javac).
  78  *
  79  * <p><b>This is NOT part of any supported API.
  80  * If you write code that depends on this, you do so at your own risk.
  81  * This code and its internal interfaces are subject to change or
  82  * deletion without notice.</b>
  83  *
  84  * @see TreeMaker
  85  * @see TreeInfo
  86  * @see TreeTranslator
  87  * @see Pretty
  88  */
  89 public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
  90 
  91     /* Tree tag values, identifying kinds of trees */
  92     public enum Tag {
  93         /** For methods that return an invalid tag if a given condition is not met
  94          */
  95         NO_TAG,
  96 
  97         /** Toplevel nodes, of type TopLevel, representing entire source files.
  98         */
  99         TOPLEVEL,
 100 
 101         /** Package level definitions.
 102          */
 103         PACKAGEDEF,
 104 
 105         /** Import clauses, of type Import.
 106          */
 107         IMPORT,
 108 
 109         /** Class definitions, of type ClassDef.
 110          */
 111         CLASSDEF,
 112 
 113         /** Method definitions, of type MethodDef.
 114          */
 115         METHODDEF,
 116 
 117         /** Variable definitions, of type VarDef.
 118          */
 119         VARDEF,
 120 
 121         /** The no-op statement ";", of type Skip
 122          */
 123         SKIP,
 124 
 125         /** Blocks, of type Block.
 126          */
 127         BLOCK,
 128 
 129         /** Do-while loops, of type DoLoop.
 130          */
 131         DOLOOP,
 132 
 133         /** While-loops, of type WhileLoop.
 134          */
 135         WHILELOOP,
 136 
 137         /** For-loops, of type ForLoop.
 138          */
 139         FORLOOP,
 140 
 141         /** Foreach-loops, of type ForeachLoop.
 142          */
 143         FOREACHLOOP,
 144 
 145         /** Labelled statements, of type Labelled.
 146          */
 147         LABELLED,
 148 
 149         /** Switch statements, of type Switch.
 150          */
 151         SWITCH,
 152 
 153         /** Case parts in switch statements/expressions, of type Case.
 154          */
 155         CASE,
 156 
 157         /** Switch expression statements, of type Switch.
 158          */
 159         SWITCH_EXPRESSION,
 160 
 161         /** Synchronized statements, of type Synchonized.
 162          */
 163         SYNCHRONIZED,
 164 
 165         /** Try statements, of type Try.
 166          */
 167         TRY,
 168 
 169         /** Catch clauses in try statements, of type Catch.
 170          */
 171         CATCH,
 172 
 173         /** Conditional expressions, of type Conditional.
 174          */
 175         CONDEXPR,
 176 
 177         /** Conditional statements, of type If.
 178          */
 179         IF,
 180 
 181         /** Expression statements, of type Exec.
 182          */
 183         EXEC,
 184 
 185         /** Break statements, of type Break.
 186          */
 187         BREAK,
 188 
 189         /** Yield statements, of type Yield.
 190          */
 191         YIELD,
 192 
 193         /** Continue statements, of type Continue.
 194          */
 195         CONTINUE,
 196 
 197         /** Return statements, of type Return.
 198          */
 199         RETURN,
 200 
 201         /** Throw statements, of type Throw.
 202          */
 203         THROW,
 204 
 205         /** Assert statements, of type Assert.
 206          */
 207         ASSERT,
 208 
 209         /** Method invocation expressions, of type Apply.
 210          */
 211         APPLY,
 212 
 213         /** Class instance creation expressions, of type NewClass.
 214          */
 215         NEWCLASS,
 216 
 217         /** Array creation expressions, of type NewArray.
 218          */
 219         NEWARRAY,
 220 
 221         /** Lambda expression, of type Lambda.
 222          */
 223         LAMBDA,
 224 
 225         /** Parenthesized subexpressions, of type Parens.
 226          */
 227         PARENS,
 228 
 229         /** Assignment expressions, of type Assign.
 230          */
 231         ASSIGN,
 232 
 233         /** Type cast expressions, of type TypeCast.
 234          */
 235         TYPECAST,
 236 
 237         /** Type test expressions, of type TypeTest.
 238          */
 239         TYPETEST,
 240 
 241         /** Indexed array expressions, of type Indexed.
 242          */
 243         INDEXED,
 244 
 245         /** Selections, of type Select.
 246          */
 247         SELECT,
 248 
 249         /** Member references, of type Reference.
 250          */
 251         REFERENCE,
 252 
 253         /** Simple identifiers, of type Ident.
 254          */
 255         IDENT,
 256 
 257         /** Literals, of type Literal.
 258          */
 259         LITERAL,
 260 
 261         /** Basic type identifiers, of type TypeIdent.
 262          */
 263         TYPEIDENT,
 264 
 265         /** Array types, of type TypeArray.
 266          */
 267         TYPEARRAY,
 268 
 269         /** Parameterized types, of type TypeApply.
 270          */
 271         TYPEAPPLY,
 272 
 273         /** Union types, of type TypeUnion.
 274          */
 275         TYPEUNION,
 276 
 277         /** Intersection types, of type TypeIntersection.
 278          */
 279         TYPEINTERSECTION,
 280 
 281         /** Formal type parameters, of type TypeParameter.
 282          */
 283         TYPEPARAMETER,
 284 
 285         /** Type argument.
 286          */
 287         WILDCARD,
 288 
 289         /** Bound kind: extends, super, exact, or unbound
 290          */
 291         TYPEBOUNDKIND,
 292 
 293         /** metadata: Annotation.
 294          */
 295         ANNOTATION,
 296 
 297         /** metadata: Type annotation.
 298          */
 299         TYPE_ANNOTATION,
 300 
 301         /** metadata: Modifiers
 302          */
 303         MODIFIERS,
 304 
 305         /** An annotated type tree.
 306          */
 307         ANNOTATED_TYPE,
 308 
 309         /** Error trees, of type Erroneous.
 310          */
 311         ERRONEOUS,
 312 
 313         /** Unary operators, of type Unary.
 314          */
 315         POS,                             // +
 316         NEG,                             // -
 317         NOT,                             // !
 318         COMPL,                           // ~
 319         PREINC,                          // ++ _
 320         PREDEC,                          // -- _
 321         POSTINC,                         // _ ++
 322         POSTDEC,                         // _ --
 323 
 324         /** unary operator for null reference checks, only used internally.
 325          */
 326         NULLCHK,
 327 
 328         /** Binary operators, of type Binary.
 329          */
 330         OR,                              // ||
 331         AND,                             // &&
 332         BITOR,                           // |
 333         BITXOR,                          // ^
 334         BITAND,                          // &
 335         EQ,                              // ==
 336         NE,                              // !=
 337         LT,                              // <
 338         GT,                              // >
 339         LE,                              // <=
 340         GE,                              // >=
 341         SL,                              // <<
 342         SR,                              // >>
 343         USR,                             // >>>
 344         PLUS,                            // +
 345         MINUS,                           // -
 346         MUL,                             // *
 347         DIV,                             // /
 348         MOD,                             // %
 349 
 350         /** Assignment operators, of type Assignop.
 351          */
 352         BITOR_ASG(BITOR),                // |=
 353         BITXOR_ASG(BITXOR),              // ^=
 354         BITAND_ASG(BITAND),              // &=
 355 
 356         SL_ASG(SL),                      // <<=
 357         SR_ASG(SR),                      // >>=
 358         USR_ASG(USR),                    // >>>=
 359         PLUS_ASG(PLUS),                  // +=
 360         MINUS_ASG(MINUS),                // -=
 361         MUL_ASG(MUL),                    // *=
 362         DIV_ASG(DIV),                    // /=
 363         MOD_ASG(MOD),                    // %=
 364 
 365         MODULEDEF,
 366         EXPORTS,
 367         OPENS,
 368         PROVIDES,
 369         REQUIRES,
 370         USES,
 371 
 372         /** A synthetic let expression, of type LetExpr.
 373          */
 374         LETEXPR;                         // ala scheme
 375 
 376         private final Tag noAssignTag;
 377 
 378         private static final int numberOfOperators = MOD.ordinal() - POS.ordinal() + 1;
 379 
 380         private Tag(Tag noAssignTag) {
 381             this.noAssignTag = noAssignTag;
 382         }
 383 
 384         private Tag() {
 385             this(null);
 386         }
 387 
 388         public static int getNumberOfOperators() {
 389             return numberOfOperators;
 390         }
 391 
 392         public Tag noAssignOp() {
 393             if (noAssignTag != null)
 394                 return noAssignTag;
 395             throw new AssertionError("noAssignOp() method is not available for non assignment tags");
 396         }
 397 
 398         public boolean isPostUnaryOp() {
 399             return (this == POSTINC || this == POSTDEC);
 400         }
 401 
 402         public boolean isIncOrDecUnaryOp() {
 403             return (this == PREINC || this == PREDEC || this == POSTINC || this == POSTDEC);
 404         }
 405 
 406         public boolean isAssignop() {
 407             return noAssignTag != null;
 408         }
 409 
 410         public int operatorIndex() {
 411             return (this.ordinal() - POS.ordinal());
 412         }
 413     }
 414 
 415     /* The (encoded) position in the source file. @see util.Position.
 416      */
 417     public int pos;
 418 
 419     /* The type of this node.
 420      */
 421     public Type type;
 422 
 423     /* The tag of this node -- one of the constants declared above.
 424      */
 425     public abstract Tag getTag();
 426 
 427     /* Returns true if the tag of this node is equals to tag.
 428      */
 429     public boolean hasTag(Tag tag) {
 430         return tag == getTag();
 431     }
 432 
 433     /** Convert a tree to a pretty-printed string. */
 434     @Override
 435     public String toString() {
 436         StringWriter s = new StringWriter();
 437         try {
 438             new Pretty(s, false).printExpr(this);
 439         }
 440         catch (IOException e) {
 441             // should never happen, because StringWriter is defined
 442             // never to throw any IOExceptions
 443             throw new AssertionError(e);
 444         }
 445         return s.toString();
 446     }
 447 
 448     /** Set position field and return this tree.
 449      */
 450     public JCTree setPos(int pos) {
 451         this.pos = pos;
 452         return this;
 453     }
 454 
 455     /** Set type field and return this tree.
 456      */
 457     public JCTree setType(Type type) {
 458         this.type = type;
 459         return this;
 460     }
 461 
 462     /** Visit this tree with a given visitor.
 463      */
 464     public abstract void accept(Visitor v);
 465 
 466     @DefinedBy(Api.COMPILER_TREE)
 467     public abstract <R,D> R accept(TreeVisitor<R,D> v, D d);
 468 
 469     /** Return a shallow copy of this tree.
 470      */
 471     @Override
 472     public Object clone() {
 473         try {
 474             return super.clone();
 475         } catch(CloneNotSupportedException e) {
 476             throw new RuntimeException(e);
 477         }
 478     }
 479 
 480     /** Get a default position for this tree node.
 481      */
 482     public DiagnosticPosition pos() {
 483         return this;
 484     }
 485 
 486     // for default DiagnosticPosition
 487     public JCTree getTree() {
 488         return this;
 489     }
 490 
 491     // for default DiagnosticPosition
 492     public int getStartPosition() {
 493         return TreeInfo.getStartPos(this);
 494     }
 495 
 496     // for default DiagnosticPosition
 497     public int getPreferredPosition() {
 498         return pos;
 499     }
 500 
 501     // for default DiagnosticPosition
 502     public int getEndPosition(EndPosTable endPosTable) {
 503         return TreeInfo.getEndPos(this, endPosTable);
 504     }
 505 
 506     /**
 507      * Everything in one source file is kept in a {@linkplain JCCompilationUnit} structure.
 508      */
 509     public static class JCCompilationUnit extends JCTree implements CompilationUnitTree {
 510         /** All definitions in this file (ClassDef, Import, and Skip) */
 511         public List<JCTree> defs;
 512         /** The source file name. */
 513         public JavaFileObject sourcefile;
 514         /** The module to which this compilation unit belongs. */
 515         public ModuleSymbol modle;
 516         /** The location in which this compilation unit was found. */
 517         public Location locn;
 518         /** The package to which this compilation unit belongs. */
 519         public PackageSymbol packge;
 520         /** A scope containing top level classes. */
 521         public WriteableScope toplevelScope;
 522         /** A scope for all named imports. */
 523         public NamedImportScope namedImportScope;
 524         /** A scope for all import-on-demands. */
 525         public StarImportScope starImportScope;
 526         /** Line starting positions, defined only if option -g is set. */
 527         public Position.LineMap lineMap = null;
 528         /** A table that stores all documentation comments indexed by the tree
 529          * nodes they refer to. defined only if option -s is set. */
 530         public DocCommentTable docComments = null;
 531         /* An object encapsulating ending positions of source ranges indexed by
 532          * the tree nodes they belong to. Defined only if option -Xjcov is set. */
 533         public EndPosTable endPositions = null;
 534         protected JCCompilationUnit(List<JCTree> defs) {
 535             this.defs = defs;
 536         }
 537         @Override
 538         public void accept(Visitor v) { v.visitTopLevel(this); }
 539 
 540         @DefinedBy(Api.COMPILER_TREE)
 541         public Kind getKind() { return Kind.COMPILATION_UNIT; }
 542 
 543         public JCModuleDecl getModuleDecl() {
 544             for (JCTree tree : defs) {
 545                 if (tree.hasTag(MODULEDEF)) {
 546                     return (JCModuleDecl) tree;
 547                 }
 548             }
 549 
 550             return null;
 551         }
 552 
 553         @DefinedBy(Api.COMPILER_TREE)
 554         public JCPackageDecl getPackage() {
 555             // PackageDecl must be the first entry if it exists
 556             if (!defs.isEmpty() && defs.head.hasTag(PACKAGEDEF))
 557                 return (JCPackageDecl)defs.head;
 558             return null;
 559         }
 560         @DefinedBy(Api.COMPILER_TREE)
 561         public List<JCAnnotation> getPackageAnnotations() {
 562             JCPackageDecl pd = getPackage();
 563             return pd != null ? pd.getAnnotations() : List.nil();
 564         }
 565         @DefinedBy(Api.COMPILER_TREE)
 566         public ExpressionTree getPackageName() {
 567             JCPackageDecl pd = getPackage();
 568             return pd != null ? pd.getPackageName() : null;
 569         }
 570 
 571         @DefinedBy(Api.COMPILER_TREE)
 572         public List<JCImport> getImports() {
 573             ListBuffer<JCImport> imports = new ListBuffer<>();
 574             for (JCTree tree : defs) {
 575                 if (tree.hasTag(IMPORT))
 576                     imports.append((JCImport)tree);
 577                 else if (!tree.hasTag(PACKAGEDEF) && !tree.hasTag(SKIP))
 578                     break;
 579             }
 580             return imports.toList();
 581         }
 582         @DefinedBy(Api.COMPILER_TREE)
 583         public JavaFileObject getSourceFile() {
 584             return sourcefile;
 585         }
 586         @DefinedBy(Api.COMPILER_TREE)
 587         public Position.LineMap getLineMap() {
 588             return lineMap;
 589         }
 590         @DefinedBy(Api.COMPILER_TREE)
 591         public List<JCTree> getTypeDecls() {
 592             List<JCTree> typeDefs;
 593             for (typeDefs = defs; !typeDefs.isEmpty(); typeDefs = typeDefs.tail)
 594                 if (!typeDefs.head.hasTag(PACKAGEDEF) && !typeDefs.head.hasTag(IMPORT))
 595                     break;
 596             return typeDefs;
 597         }
 598         @Override @DefinedBy(Api.COMPILER_TREE)
 599         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
 600             return v.visitCompilationUnit(this, d);
 601         }
 602 
 603         @Override
 604         public Tag getTag() {
 605             return TOPLEVEL;
 606         }
 607     }
 608 
 609     /**
 610      * Package definition.
 611      */
 612     public static class JCPackageDecl extends JCTree implements PackageTree {
 613         public List<JCAnnotation> annotations;
 614         /** The tree representing the package clause. */
 615         public JCExpression pid;
 616         public PackageSymbol packge;
 617         public JCPackageDecl(List<JCAnnotation> annotations, JCExpression pid) {
 618             this.annotations = annotations;
 619             this.pid = pid;
 620         }
 621         @Override
 622         public void accept(Visitor v) { v.visitPackageDef(this); }
 623         @DefinedBy(Api.COMPILER_TREE)
 624         public Kind getKind() {
 625             return Kind.PACKAGE;
 626         }
 627         @DefinedBy(Api.COMPILER_TREE)
 628         public List<JCAnnotation> getAnnotations() {
 629             return annotations;
 630         }
 631         @DefinedBy(Api.COMPILER_TREE)
 632         public JCExpression getPackageName() {
 633             return pid;
 634         }
 635         @Override @DefinedBy(Api.COMPILER_TREE)
 636         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
 637             return v.visitPackage(this, d);
 638         }
 639         @Override
 640         public Tag getTag() {
 641             return PACKAGEDEF;
 642         }
 643     }
 644 
 645     /**
 646      * An import clause.
 647      */
 648     public static class JCImport extends JCTree implements ImportTree {
 649         public boolean staticImport;
 650         /** The imported class(es). */
 651         public JCTree qualid;
 652         public com.sun.tools.javac.code.Scope importScope;
 653         protected JCImport(JCTree qualid, boolean importStatic) {
 654             this.qualid = qualid;
 655             this.staticImport = importStatic;
 656         }
 657         @Override
 658         public void accept(Visitor v) { v.visitImport(this); }
 659 
 660         @DefinedBy(Api.COMPILER_TREE)
 661         public boolean isStatic() { return staticImport; }
 662         @DefinedBy(Api.COMPILER_TREE)
 663         public JCTree getQualifiedIdentifier() { return qualid; }
 664 
 665         @DefinedBy(Api.COMPILER_TREE)
 666         public Kind getKind() { return Kind.IMPORT; }
 667         @Override @DefinedBy(Api.COMPILER_TREE)
 668         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
 669             return v.visitImport(this, d);
 670         }
 671 
 672         @Override
 673         public Tag getTag() {
 674             return IMPORT;
 675         }
 676     }
 677 
 678     public static abstract class JCStatement extends JCTree implements StatementTree {
 679         @Override
 680         public JCStatement setType(Type type) {
 681             super.setType(type);
 682             return this;
 683         }
 684         @Override
 685         public JCStatement setPos(int pos) {
 686             super.setPos(pos);
 687             return this;
 688         }
 689     }
 690 
 691     public static abstract class JCExpression extends JCTree implements ExpressionTree {
 692         @Override
 693         public JCExpression setType(Type type) {
 694             super.setType(type);
 695             return this;
 696         }
 697         @Override
 698         public JCExpression setPos(int pos) {
 699             super.setPos(pos);
 700             return this;
 701         }
 702 
 703         public boolean isPoly() { return false; }
 704         public boolean isStandalone() { return true; }
 705     }
 706 
 707     /**
 708      * Common supertype for all poly expression trees (lambda, method references,
 709      * conditionals, method and constructor calls)
 710      */
 711     public static abstract class JCPolyExpression extends JCExpression {
 712 
 713         /**
 714          * A poly expression can only be truly 'poly' in certain contexts
 715          */
 716         public enum PolyKind {
 717             /** poly expression to be treated as a standalone expression */
 718             STANDALONE,
 719             /** true poly expression */
 720             POLY
 721         }
 722 
 723         /** is this poly expression a 'true' poly expression? */
 724         public PolyKind polyKind;
 725 
 726         @Override public boolean isPoly() { return polyKind == PolyKind.POLY; }
 727         @Override public boolean isStandalone() { return polyKind == PolyKind.STANDALONE; }
 728     }
 729 
 730     /**
 731      * Common supertype for all functional expression trees (lambda and method references)
 732      */
 733     public static abstract class JCFunctionalExpression extends JCPolyExpression {
 734 
 735         public JCFunctionalExpression() {
 736             //a functional expression is always a 'true' poly
 737             polyKind = PolyKind.POLY;
 738         }
 739 
 740         /** list of target types inferred for this functional expression. */
 741         public Type target;
 742 
 743         public Type getDescriptorType(Types types) {
 744             return target != null ? types.findDescriptorType(target) : types.createErrorType(null);
 745         }
 746     }
 747 
 748     /**
 749      * A class definition.
 750      */
 751     public static class JCClassDecl extends JCStatement implements ClassTree {
 752         /** the modifiers */
 753         public JCModifiers mods;
 754         /** the name of the class */
 755         public Name name;
 756         /** formal class parameters */
 757         public List<JCTypeParameter> typarams;
 758         /** the classes this class extends */
 759         public JCExpression extending;
 760         /** the interfaces implemented by this class */
 761         public List<JCExpression> implementing;
 762         /** all variables and methods defined in this class */
 763         public List<JCTree> defs;
 764         /** the symbol */
 765         public ClassSymbol sym;
 766         protected JCClassDecl(JCModifiers mods,
 767                            Name name,
 768                            List<JCTypeParameter> typarams,
 769                            JCExpression extending,
 770                            List<JCExpression> implementing,
 771                            List<JCTree> defs,
 772                            ClassSymbol sym)
 773         {
 774             this.mods = mods;
 775             this.name = name;
 776             this.typarams = typarams;
 777             this.extending = extending;
 778             this.implementing = implementing;
 779             this.defs = defs;
 780             this.sym = sym;
 781         }
 782         @Override
 783         public void accept(Visitor v) { v.visitClassDef(this); }
 784 
 785         @DefinedBy(Api.COMPILER_TREE)
 786         public Kind getKind() {
 787             if ((mods.flags & Flags.ANNOTATION) != 0)
 788                 return Kind.ANNOTATION_TYPE;
 789             else if ((mods.flags & Flags.INTERFACE) != 0)
 790                 return Kind.INTERFACE;
 791             else if ((mods.flags & Flags.ENUM) != 0)
 792                 return Kind.ENUM;
 793             else
 794                 return Kind.CLASS;
 795         }
 796 
 797         @DefinedBy(Api.COMPILER_TREE)
 798         public JCModifiers getModifiers() { return mods; }
 799         @DefinedBy(Api.COMPILER_TREE)
 800         public Name getSimpleName() { return name; }
 801         @DefinedBy(Api.COMPILER_TREE)
 802         public List<JCTypeParameter> getTypeParameters() {
 803             return typarams;
 804         }
 805         @DefinedBy(Api.COMPILER_TREE)
 806         public JCExpression getExtendsClause() { return extending; }
 807         @DefinedBy(Api.COMPILER_TREE)
 808         public List<JCExpression> getImplementsClause() {
 809             return implementing;
 810         }
 811         @DefinedBy(Api.COMPILER_TREE)
 812         public List<JCTree> getMembers() {
 813             return defs;
 814         }
 815         @Override @DefinedBy(Api.COMPILER_TREE)
 816         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
 817             return v.visitClass(this, d);
 818         }
 819 
 820         @Override
 821         public Tag getTag() {
 822             return CLASSDEF;
 823         }
 824     }
 825 
 826     /**
 827      * A method definition.
 828      */
 829     public static class JCMethodDecl extends JCTree implements MethodTree {
 830         /** method modifiers */
 831         public JCModifiers mods;
 832         /** method name */
 833         public Name name;
 834         /** type of method return value */
 835         public JCExpression restype;
 836         /** type parameters */
 837         public List<JCTypeParameter> typarams;
 838         /** receiver parameter */
 839         public JCVariableDecl recvparam;
 840         /** value parameters */
 841         public List<JCVariableDecl> params;
 842         /** exceptions thrown by this method */
 843         public List<JCExpression> thrown;
 844         /** statements in the method */
 845         public JCBlock body;
 846         /** default value, for annotation types */
 847         public JCExpression defaultValue;
 848         /** method symbol */
 849         public MethodSymbol sym;
 850         protected JCMethodDecl(JCModifiers mods,
 851                             Name name,
 852                             JCExpression restype,
 853                             List<JCTypeParameter> typarams,
 854                             JCVariableDecl recvparam,
 855                             List<JCVariableDecl> params,
 856                             List<JCExpression> thrown,
 857                             JCBlock body,
 858                             JCExpression defaultValue,
 859                             MethodSymbol sym)
 860         {
 861             this.mods = mods;
 862             this.name = name;
 863             this.restype = restype;
 864             this.typarams = typarams;
 865             this.params = params;
 866             this.recvparam = recvparam;
 867             // TODO: do something special if the given type is null?
 868             // receiver != null ? receiver : List.<JCTypeAnnotation>nil());
 869             this.thrown = thrown;
 870             this.body = body;
 871             this.defaultValue = defaultValue;
 872             this.sym = sym;
 873         }
 874         @Override
 875         public void accept(Visitor v) { v.visitMethodDef(this); }
 876 
 877         @DefinedBy(Api.COMPILER_TREE)
 878         public Kind getKind() { return Kind.METHOD; }
 879         @DefinedBy(Api.COMPILER_TREE)
 880         public JCModifiers getModifiers() { return mods; }
 881         @DefinedBy(Api.COMPILER_TREE)
 882         public Name getName() { return name; }
 883         @DefinedBy(Api.COMPILER_TREE)
 884         public JCTree getReturnType() { return restype; }
 885         @DefinedBy(Api.COMPILER_TREE)
 886         public List<JCTypeParameter> getTypeParameters() {
 887             return typarams;
 888         }
 889         @DefinedBy(Api.COMPILER_TREE)
 890         public List<JCVariableDecl> getParameters() {
 891             return params;
 892         }
 893         @DefinedBy(Api.COMPILER_TREE)
 894         public JCVariableDecl getReceiverParameter() { return recvparam; }
 895         @DefinedBy(Api.COMPILER_TREE)
 896         public List<JCExpression> getThrows() {
 897             return thrown;
 898         }
 899         @DefinedBy(Api.COMPILER_TREE)
 900         public JCBlock getBody() { return body; }
 901         @DefinedBy(Api.COMPILER_TREE)
 902         public JCTree getDefaultValue() { // for annotation types
 903             return defaultValue;
 904         }
 905         @Override @DefinedBy(Api.COMPILER_TREE)
 906         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
 907             return v.visitMethod(this, d);
 908         }
 909 
 910         @Override
 911         public Tag getTag() {
 912             return METHODDEF;
 913         }
 914   }
 915 
 916     /**
 917      * A variable definition.
 918      */
 919     public static class JCVariableDecl extends JCStatement implements VariableTree {
 920         /** variable modifiers */
 921         public JCModifiers mods;
 922         /** variable name */
 923         public Name name;
 924         /** variable name expression */
 925         public JCExpression nameexpr;
 926         /** type of the variable */
 927         public JCExpression vartype;
 928         /** variable's initial value */
 929         public JCExpression init;
 930         /** symbol */
 931         public VarSymbol sym;
 932         /** explicit start pos */
 933         public int startPos = Position.NOPOS;
 934 
 935         protected JCVariableDecl(JCModifiers mods,
 936                          Name name,
 937                          JCExpression vartype,
 938                          JCExpression init,
 939                          VarSymbol sym) {
 940             this.mods = mods;
 941             this.name = name;
 942             this.vartype = vartype;
 943             this.init = init;
 944             this.sym = sym;
 945         }
 946 
 947         protected JCVariableDecl(JCModifiers mods,
 948                          JCExpression nameexpr,
 949                          JCExpression vartype) {
 950             this(mods, null, vartype, null, null);
 951             this.nameexpr = nameexpr;
 952             if (nameexpr.hasTag(Tag.IDENT)) {
 953                 this.name = ((JCIdent)nameexpr).name;
 954             } else {
 955                 // Only other option is qualified name x.y.this;
 956                 this.name = ((JCFieldAccess)nameexpr).name;
 957             }
 958         }
 959 
 960         public boolean isImplicitlyTyped() {
 961             return vartype == null;
 962         }
 963 
 964         @Override
 965         public void accept(Visitor v) { v.visitVarDef(this); }
 966 
 967         @DefinedBy(Api.COMPILER_TREE)
 968         public Kind getKind() { return Kind.VARIABLE; }
 969         @DefinedBy(Api.COMPILER_TREE)
 970         public JCModifiers getModifiers() { return mods; }
 971         @DefinedBy(Api.COMPILER_TREE)
 972         public Name getName() { return name; }
 973         @DefinedBy(Api.COMPILER_TREE)
 974         public JCExpression getNameExpression() { return nameexpr; }
 975         @DefinedBy(Api.COMPILER_TREE)
 976         public JCTree getType() { return vartype; }
 977         @DefinedBy(Api.COMPILER_TREE)
 978         public JCExpression getInitializer() {
 979             return init;
 980         }
 981         @Override @DefinedBy(Api.COMPILER_TREE)
 982         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
 983             return v.visitVariable(this, d);
 984         }
 985 
 986         @Override
 987         public Tag getTag() {
 988             return VARDEF;
 989         }
 990     }
 991 
 992     /**
 993      * A no-op statement ";".
 994      */
 995     public static class JCSkip extends JCStatement implements EmptyStatementTree {
 996         protected JCSkip() {
 997         }
 998         @Override
 999         public void accept(Visitor v) { v.visitSkip(this); }
1000 
1001         @DefinedBy(Api.COMPILER_TREE)
1002         public Kind getKind() { return Kind.EMPTY_STATEMENT; }
1003         @Override @DefinedBy(Api.COMPILER_TREE)
1004         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1005             return v.visitEmptyStatement(this, d);
1006         }
1007 
1008         @Override
1009         public Tag getTag() {
1010             return SKIP;
1011         }
1012     }
1013 
1014     /**
1015      * A statement block.
1016      */
1017     public static class JCBlock extends JCStatement implements BlockTree {
1018         /** flags */
1019         public long flags;
1020         /** statements */
1021         public List<JCStatement> stats;
1022         /** Position of closing brace, optional. */
1023         public int endpos = Position.NOPOS;
1024         protected JCBlock(long flags, List<JCStatement> stats) {
1025             this.stats = stats;
1026             this.flags = flags;
1027         }
1028         @Override
1029         public void accept(Visitor v) { v.visitBlock(this); }
1030 
1031         @DefinedBy(Api.COMPILER_TREE)
1032         public Kind getKind() { return Kind.BLOCK; }
1033         @DefinedBy(Api.COMPILER_TREE)
1034         public List<JCStatement> getStatements() {
1035             return stats;
1036         }
1037         @DefinedBy(Api.COMPILER_TREE)
1038         public boolean isStatic() { return (flags & Flags.STATIC) != 0; }
1039         @Override @DefinedBy(Api.COMPILER_TREE)
1040         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1041             return v.visitBlock(this, d);
1042         }
1043 
1044         @Override
1045         public Tag getTag() {
1046             return BLOCK;
1047         }
1048     }
1049 
1050     /**
1051      * A do loop
1052      */
1053     public static class JCDoWhileLoop extends JCStatement implements DoWhileLoopTree {
1054         public JCStatement body;
1055         public JCExpression cond;
1056         protected JCDoWhileLoop(JCStatement body, JCExpression cond) {
1057             this.body = body;
1058             this.cond = cond;
1059         }
1060         @Override
1061         public void accept(Visitor v) { v.visitDoLoop(this); }
1062 
1063         @DefinedBy(Api.COMPILER_TREE)
1064         public Kind getKind() { return Kind.DO_WHILE_LOOP; }
1065         @DefinedBy(Api.COMPILER_TREE)
1066         public JCExpression getCondition() { return cond; }
1067         @DefinedBy(Api.COMPILER_TREE)
1068         public JCStatement getStatement() { return body; }
1069         @Override @DefinedBy(Api.COMPILER_TREE)
1070         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1071             return v.visitDoWhileLoop(this, d);
1072         }
1073 
1074         @Override
1075         public Tag getTag() {
1076             return DOLOOP;
1077         }
1078     }
1079 
1080     /**
1081      * A while loop
1082      */
1083     public static class JCWhileLoop extends JCStatement implements WhileLoopTree {
1084         public JCExpression cond;
1085         public JCStatement body;
1086         protected JCWhileLoop(JCExpression cond, JCStatement body) {
1087             this.cond = cond;
1088             this.body = body;
1089         }
1090         @Override
1091         public void accept(Visitor v) { v.visitWhileLoop(this); }
1092 
1093         @DefinedBy(Api.COMPILER_TREE)
1094         public Kind getKind() { return Kind.WHILE_LOOP; }
1095         @DefinedBy(Api.COMPILER_TREE)
1096         public JCExpression getCondition() { return cond; }
1097         @DefinedBy(Api.COMPILER_TREE)
1098         public JCStatement getStatement() { return body; }
1099         @Override @DefinedBy(Api.COMPILER_TREE)
1100         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1101             return v.visitWhileLoop(this, d);
1102         }
1103 
1104         @Override
1105         public Tag getTag() {
1106             return WHILELOOP;
1107         }
1108     }
1109 
1110     /**
1111      * A for loop.
1112      */
1113     public static class JCForLoop extends JCStatement implements ForLoopTree {
1114         public List<JCStatement> init;
1115         public JCExpression cond;
1116         public List<JCExpressionStatement> step;
1117         public JCStatement body;
1118         protected JCForLoop(List<JCStatement> init,
1119                           JCExpression cond,
1120                           List<JCExpressionStatement> update,
1121                           JCStatement body)
1122         {
1123             this.init = init;
1124             this.cond = cond;
1125             this.step = update;
1126             this.body = body;
1127         }
1128         @Override
1129         public void accept(Visitor v) { v.visitForLoop(this); }
1130 
1131         @DefinedBy(Api.COMPILER_TREE)
1132         public Kind getKind() { return Kind.FOR_LOOP; }
1133         @DefinedBy(Api.COMPILER_TREE)
1134         public JCExpression getCondition() { return cond; }
1135         @DefinedBy(Api.COMPILER_TREE)
1136         public JCStatement getStatement() { return body; }
1137         @DefinedBy(Api.COMPILER_TREE)
1138         public List<JCStatement> getInitializer() {
1139             return init;
1140         }
1141         @DefinedBy(Api.COMPILER_TREE)
1142         public List<JCExpressionStatement> getUpdate() {
1143             return step;
1144         }
1145         @Override @DefinedBy(Api.COMPILER_TREE)
1146         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1147             return v.visitForLoop(this, d);
1148         }
1149 
1150         @Override
1151         public Tag getTag() {
1152             return FORLOOP;
1153         }
1154     }
1155 
1156     /**
1157      * The enhanced for loop.
1158      */
1159     public static class JCEnhancedForLoop extends JCStatement implements EnhancedForLoopTree {
1160         public JCVariableDecl var;
1161         public JCExpression expr;
1162         public JCStatement body;
1163         protected JCEnhancedForLoop(JCVariableDecl var, JCExpression expr, JCStatement body) {
1164             this.var = var;
1165             this.expr = expr;
1166             this.body = body;
1167         }
1168         @Override
1169         public void accept(Visitor v) { v.visitForeachLoop(this); }
1170 
1171         @DefinedBy(Api.COMPILER_TREE)
1172         public Kind getKind() { return Kind.ENHANCED_FOR_LOOP; }
1173         @DefinedBy(Api.COMPILER_TREE)
1174         public JCVariableDecl getVariable() { return var; }
1175         @DefinedBy(Api.COMPILER_TREE)
1176         public JCExpression getExpression() { return expr; }
1177         @DefinedBy(Api.COMPILER_TREE)
1178         public JCStatement getStatement() { return body; }
1179         @Override @DefinedBy(Api.COMPILER_TREE)
1180         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1181             return v.visitEnhancedForLoop(this, d);
1182         }
1183         @Override
1184         public Tag getTag() {
1185             return FOREACHLOOP;
1186         }
1187     }
1188 
1189     /**
1190      * A labelled expression or statement.
1191      */
1192     public static class JCLabeledStatement extends JCStatement implements LabeledStatementTree {
1193         public Name label;
1194         public JCStatement body;
1195         protected JCLabeledStatement(Name label, JCStatement body) {
1196             this.label = label;
1197             this.body = body;
1198         }
1199         @Override
1200         public void accept(Visitor v) { v.visitLabelled(this); }
1201         @DefinedBy(Api.COMPILER_TREE)
1202         public Kind getKind() { return Kind.LABELED_STATEMENT; }
1203         @DefinedBy(Api.COMPILER_TREE)
1204         public Name getLabel() { return label; }
1205         @DefinedBy(Api.COMPILER_TREE)
1206         public JCStatement getStatement() { return body; }
1207         @Override @DefinedBy(Api.COMPILER_TREE)
1208         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1209             return v.visitLabeledStatement(this, d);
1210         }
1211         @Override
1212         public Tag getTag() {
1213             return LABELLED;
1214         }
1215     }
1216 
1217     /**
1218      * A "switch ( ) { }" construction.
1219      */
1220     public static class JCSwitch extends JCStatement implements SwitchTree {
1221         public JCExpression selector;
1222         public List<JCCase> cases;
1223         protected JCSwitch(JCExpression selector, List<JCCase> cases) {
1224             this.selector = selector;
1225             this.cases = cases;
1226         }
1227         @Override
1228         public void accept(Visitor v) { v.visitSwitch(this); }
1229 
1230         @DefinedBy(Api.COMPILER_TREE)
1231         public Kind getKind() { return Kind.SWITCH; }
1232         @DefinedBy(Api.COMPILER_TREE)
1233         public JCExpression getExpression() { return selector; }
1234         @DefinedBy(Api.COMPILER_TREE)
1235         public List<JCCase> getCases() { return cases; }
1236         @Override @DefinedBy(Api.COMPILER_TREE)
1237         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1238             return v.visitSwitch(this, d);
1239         }
1240         @Override
1241         public Tag getTag() {
1242             return SWITCH;
1243         }
1244     }
1245 
1246     /**
1247      * A "case  :" of a switch.
1248      */
1249     public static class JCCase extends JCStatement implements CaseTree {
1250         //as CaseKind is deprecated for removal (as it is part of a preview feature),
1251         //using indirection through these fields to avoid unnecessary @SuppressWarnings:
1252         public static final CaseKind STATEMENT = CaseKind.STATEMENT;
1253         public static final CaseKind RULE = CaseKind.RULE;
1254         public final CaseKind caseKind;
1255         public List<JCExpression> pats;
1256         public List<JCStatement> stats;
1257         public JCTree body;
1258         public boolean completesNormally;
1259         protected JCCase(CaseKind caseKind, List<JCExpression> pats,
1260                          List<JCStatement> stats, JCTree body) {
1261             Assert.checkNonNull(pats);
1262             Assert.check(pats.isEmpty() || pats.head != null);
1263             this.caseKind = caseKind;
1264             this.pats = pats;
1265             this.stats = stats;
1266             this.body = body;
1267         }
1268         @Override
1269         public void accept(Visitor v) { v.visitCase(this); }
1270 
1271         @Override @DefinedBy(Api.COMPILER_TREE)
1272         public Kind getKind() { return Kind.CASE; }
1273         @Override @Deprecated @DefinedBy(Api.COMPILER_TREE)
1274         public JCExpression getExpression() { return pats.head; }
1275         @Override @DefinedBy(Api.COMPILER_TREE)
1276         public List<JCExpression> getExpressions() { return pats; }
1277         @Override @DefinedBy(Api.COMPILER_TREE)
1278         public List<JCStatement> getStatements() {
1279             return caseKind == CaseKind.STATEMENT ? stats : null;
1280         }
1281         @Override @DefinedBy(Api.COMPILER_TREE)
1282         public JCTree getBody() { return body; }
1283         @Override @DefinedBy(Api.COMPILER_TREE)
1284         public CaseKind getCaseKind() {
1285             return caseKind;
1286         }
1287         @Override @DefinedBy(Api.COMPILER_TREE)
1288         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1289             return v.visitCase(this, d);
1290         }
1291         @Override
1292         public Tag getTag() {
1293             return CASE;
1294         }
1295     }
1296 
1297     /**
1298      * A "switch ( ) { }" construction.
1299      */
1300     public static class JCSwitchExpression extends JCPolyExpression implements SwitchExpressionTree {
1301         public JCExpression selector;
1302         public List<JCCase> cases;
1303         /** Position of closing brace, optional. */
1304         public int endpos = Position.NOPOS;
1305         protected JCSwitchExpression(JCExpression selector, List<JCCase> cases) {
1306             this.selector = selector;
1307             this.cases = cases;
1308         }
1309         @Override
1310         public void accept(Visitor v) { v.visitSwitchExpression(this); }
1311 
1312         @DefinedBy(Api.COMPILER_TREE)
1313         public Kind getKind() { return Kind.SWITCH_EXPRESSION; }
1314         @DefinedBy(Api.COMPILER_TREE)
1315         public JCExpression getExpression() { return selector; }
1316         @DefinedBy(Api.COMPILER_TREE)
1317         public List<JCCase> getCases() { return cases; }
1318         @Override @DefinedBy(Api.COMPILER_TREE)
1319         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1320             return v.visitSwitchExpression(this, d);
1321         }
1322         @Override
1323         public Tag getTag() {
1324             return SWITCH_EXPRESSION;
1325         }
1326     }
1327 
1328     /**
1329      * A synchronized block.
1330      */
1331     public static class JCSynchronized extends JCStatement implements SynchronizedTree {
1332         public JCExpression lock;
1333         public JCBlock body;
1334         protected JCSynchronized(JCExpression lock, JCBlock body) {
1335             this.lock = lock;
1336             this.body = body;
1337         }
1338         @Override
1339         public void accept(Visitor v) { v.visitSynchronized(this); }
1340 
1341         @DefinedBy(Api.COMPILER_TREE)
1342         public Kind getKind() { return Kind.SYNCHRONIZED; }
1343         @DefinedBy(Api.COMPILER_TREE)
1344         public JCExpression getExpression() { return lock; }
1345         @DefinedBy(Api.COMPILER_TREE)
1346         public JCBlock getBlock() { return body; }
1347         @Override @DefinedBy(Api.COMPILER_TREE)
1348         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1349             return v.visitSynchronized(this, d);
1350         }
1351         @Override
1352         public Tag getTag() {
1353             return SYNCHRONIZED;
1354         }
1355     }
1356 
1357     /**
1358      * A "try { } catch ( ) { } finally { }" block.
1359      */
1360     public static class JCTry extends JCStatement implements TryTree {
1361         public JCBlock body;
1362         public List<JCCatch> catchers;
1363         public JCBlock finalizer;
1364         public List<JCTree> resources;
1365         public boolean finallyCanCompleteNormally;
1366         protected JCTry(List<JCTree> resources,
1367                         JCBlock body,
1368                         List<JCCatch> catchers,
1369                         JCBlock finalizer) {
1370             this.body = body;
1371             this.catchers = catchers;
1372             this.finalizer = finalizer;
1373             this.resources = resources;
1374         }
1375         @Override
1376         public void accept(Visitor v) { v.visitTry(this); }
1377 
1378         @DefinedBy(Api.COMPILER_TREE)
1379         public Kind getKind() { return Kind.TRY; }
1380         @DefinedBy(Api.COMPILER_TREE)
1381         public JCBlock getBlock() { return body; }
1382         @DefinedBy(Api.COMPILER_TREE)
1383         public List<JCCatch> getCatches() {
1384             return catchers;
1385         }
1386         @DefinedBy(Api.COMPILER_TREE)
1387         public JCBlock getFinallyBlock() { return finalizer; }
1388         @Override @DefinedBy(Api.COMPILER_TREE)
1389         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1390             return v.visitTry(this, d);
1391         }
1392         @Override @DefinedBy(Api.COMPILER_TREE)
1393         public List<JCTree> getResources() {
1394             return resources;
1395         }
1396         @Override
1397         public Tag getTag() {
1398             return TRY;
1399         }
1400     }
1401 
1402     /**
1403      * A catch block.
1404      */
1405     public static class JCCatch extends JCTree implements CatchTree {
1406         public JCVariableDecl param;
1407         public JCBlock body;
1408         protected JCCatch(JCVariableDecl param, JCBlock body) {
1409             this.param = param;
1410             this.body = body;
1411         }
1412         @Override
1413         public void accept(Visitor v) { v.visitCatch(this); }
1414 
1415         @DefinedBy(Api.COMPILER_TREE)
1416         public Kind getKind() { return Kind.CATCH; }
1417         @DefinedBy(Api.COMPILER_TREE)
1418         public JCVariableDecl getParameter() { return param; }
1419         @DefinedBy(Api.COMPILER_TREE)
1420         public JCBlock getBlock() { return body; }
1421         @Override @DefinedBy(Api.COMPILER_TREE)
1422         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1423             return v.visitCatch(this, d);
1424         }
1425         @Override
1426         public Tag getTag() {
1427             return CATCH;
1428         }
1429     }
1430 
1431     /**
1432      * A ( ) ? ( ) : ( ) conditional expression
1433      */
1434     public static class JCConditional extends JCPolyExpression implements ConditionalExpressionTree {
1435         public JCExpression cond;
1436         public JCExpression truepart;
1437         public JCExpression falsepart;
1438         protected JCConditional(JCExpression cond,
1439                               JCExpression truepart,
1440                               JCExpression falsepart)
1441         {
1442             this.cond = cond;
1443             this.truepart = truepart;
1444             this.falsepart = falsepart;
1445         }
1446         @Override
1447         public void accept(Visitor v) { v.visitConditional(this); }
1448 
1449         @DefinedBy(Api.COMPILER_TREE)
1450         public Kind getKind() { return Kind.CONDITIONAL_EXPRESSION; }
1451         @DefinedBy(Api.COMPILER_TREE)
1452         public JCExpression getCondition() { return cond; }
1453         @DefinedBy(Api.COMPILER_TREE)
1454         public JCExpression getTrueExpression() { return truepart; }
1455         @DefinedBy(Api.COMPILER_TREE)
1456         public JCExpression getFalseExpression() { return falsepart; }
1457         @Override @DefinedBy(Api.COMPILER_TREE)
1458         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1459             return v.visitConditionalExpression(this, d);
1460         }
1461         @Override
1462         public Tag getTag() {
1463             return CONDEXPR;
1464         }
1465     }
1466 
1467     /**
1468      * An "if ( ) { } else { }" block
1469      */
1470     public static class JCIf extends JCStatement implements IfTree {
1471         public JCExpression cond;
1472         public JCStatement thenpart;
1473         public JCStatement elsepart;
1474         protected JCIf(JCExpression cond,
1475                      JCStatement thenpart,
1476                      JCStatement elsepart)
1477         {
1478             this.cond = cond;
1479             this.thenpart = thenpart;
1480             this.elsepart = elsepart;
1481         }
1482         @Override
1483         public void accept(Visitor v) { v.visitIf(this); }
1484 
1485         @DefinedBy(Api.COMPILER_TREE)
1486         public Kind getKind() { return Kind.IF; }
1487         @DefinedBy(Api.COMPILER_TREE)
1488         public JCExpression getCondition() { return cond; }
1489         @DefinedBy(Api.COMPILER_TREE)
1490         public JCStatement getThenStatement() { return thenpart; }
1491         @DefinedBy(Api.COMPILER_TREE)
1492         public JCStatement getElseStatement() { return elsepart; }
1493         @Override @DefinedBy(Api.COMPILER_TREE)
1494         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1495             return v.visitIf(this, d);
1496         }
1497         @Override
1498         public Tag getTag() {
1499             return IF;
1500         }
1501     }
1502 
1503     /**
1504      * an expression statement
1505      */
1506     public static class JCExpressionStatement extends JCStatement implements ExpressionStatementTree {
1507         /** expression structure */
1508         public JCExpression expr;
1509         protected JCExpressionStatement(JCExpression expr)
1510         {
1511             this.expr = expr;
1512         }
1513         @Override
1514         public void accept(Visitor v) { v.visitExec(this); }
1515 
1516         @DefinedBy(Api.COMPILER_TREE)
1517         public Kind getKind() { return Kind.EXPRESSION_STATEMENT; }
1518         @DefinedBy(Api.COMPILER_TREE)
1519         public JCExpression getExpression() { return expr; }
1520         @Override @DefinedBy(Api.COMPILER_TREE)
1521         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1522             return v.visitExpressionStatement(this, d);
1523         }
1524         @Override
1525         public Tag getTag() {
1526             return EXEC;
1527         }
1528 
1529         /** Convert a expression-statement tree to a pretty-printed string. */
1530         @Override
1531         public String toString() {
1532             StringWriter s = new StringWriter();
1533             try {
1534                 new Pretty(s, false).printStat(this);
1535             }
1536             catch (IOException e) {
1537                 // should never happen, because StringWriter is defined
1538                 // never to throw any IOExceptions
1539                 throw new AssertionError(e);
1540             }
1541             return s.toString();
1542         }
1543     }
1544 
1545     /**
1546      * A break from a loop or switch.
1547      */
1548     public static class JCBreak extends JCStatement implements BreakTree {
1549         public Name label;
1550         public JCTree target;
1551         protected JCBreak(Name label, JCTree target) {
1552             this.label = label;
1553             this.target = target;
1554         }
1555         @Override
1556         public void accept(Visitor v) { v.visitBreak(this); }
1557         public boolean isValueBreak() {
1558             return target != null && target.hasTag(SWITCH_EXPRESSION);
1559         }
1560 
1561         @DefinedBy(Api.COMPILER_TREE)
1562         public Kind getKind() { return Kind.BREAK; }
1563         @DefinedBy(Api.COMPILER_TREE)
1564         public Name getLabel() {
1565             return label;
1566         }
1567         @Override @DefinedBy(Api.COMPILER_TREE)
1568         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1569             return v.visitBreak(this, d);
1570         }
1571         @Override
1572         public Tag getTag() {
1573             return BREAK;
1574         }
1575     }
1576 
1577     /**
1578      * A break-with from a switch expression.
1579      */
1580     public static class JCYield extends JCStatement implements YieldTree {
1581         public JCExpression value;
1582         public JCTree target;
1583         protected JCYield(JCExpression value, JCTree target) {
1584             this.value = value;
1585             this.target = target;
1586         }
1587         @Override
1588         public void accept(Visitor v) { v.visitYield(this); }
1589         @DefinedBy(Api.COMPILER_TREE)
1590         public Kind getKind() { return Kind.YIELD; }
1591         @DefinedBy(Api.COMPILER_TREE)
1592         public JCExpression getValue() { return value; }
1593         @Override @DefinedBy(Api.COMPILER_TREE)
1594         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1595             return v.visitYield(this, d);
1596         }
1597         @Override
1598         public Tag getTag() {
1599             return YIELD;
1600         }
1601     }
1602 
1603     /**
1604      * A continue of a loop.
1605      */
1606     public static class JCContinue extends JCStatement implements ContinueTree {
1607         public Name label;
1608         public JCTree target;
1609         protected JCContinue(Name label, JCTree target) {
1610             this.label = label;
1611             this.target = target;
1612         }
1613         @Override
1614         public void accept(Visitor v) { v.visitContinue(this); }
1615 
1616         @DefinedBy(Api.COMPILER_TREE)
1617         public Kind getKind() { return Kind.CONTINUE; }
1618         @DefinedBy(Api.COMPILER_TREE)
1619         public Name getLabel() { return label; }
1620         @Override @DefinedBy(Api.COMPILER_TREE)
1621         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1622             return v.visitContinue(this, d);
1623         }
1624         @Override
1625         public Tag getTag() {
1626             return CONTINUE;
1627         }
1628     }
1629 
1630     /**
1631      * A return statement.
1632      */
1633     public static class JCReturn extends JCStatement implements ReturnTree {
1634         public JCExpression expr;
1635         protected JCReturn(JCExpression expr) {
1636             this.expr = expr;
1637         }
1638         @Override
1639         public void accept(Visitor v) { v.visitReturn(this); }
1640 
1641         @DefinedBy(Api.COMPILER_TREE)
1642         public Kind getKind() { return Kind.RETURN; }
1643         @DefinedBy(Api.COMPILER_TREE)
1644         public JCExpression getExpression() { return expr; }
1645         @Override @DefinedBy(Api.COMPILER_TREE)
1646         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1647             return v.visitReturn(this, d);
1648         }
1649         @Override
1650         public Tag getTag() {
1651             return RETURN;
1652         }
1653     }
1654 
1655     /**
1656      * A throw statement.
1657      */
1658     public static class JCThrow extends JCStatement implements ThrowTree {
1659         public JCExpression expr;
1660         protected JCThrow(JCExpression expr) {
1661             this.expr = expr;
1662         }
1663         @Override
1664         public void accept(Visitor v) { v.visitThrow(this); }
1665 
1666         @DefinedBy(Api.COMPILER_TREE)
1667         public Kind getKind() { return Kind.THROW; }
1668         @DefinedBy(Api.COMPILER_TREE)
1669         public JCExpression getExpression() { return expr; }
1670         @Override @DefinedBy(Api.COMPILER_TREE)
1671         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1672             return v.visitThrow(this, d);
1673         }
1674         @Override
1675         public Tag getTag() {
1676             return THROW;
1677         }
1678     }
1679 
1680     /**
1681      * An assert statement.
1682      */
1683     public static class JCAssert extends JCStatement implements AssertTree {
1684         public JCExpression cond;
1685         public JCExpression detail;
1686         protected JCAssert(JCExpression cond, JCExpression detail) {
1687             this.cond = cond;
1688             this.detail = detail;
1689         }
1690         @Override
1691         public void accept(Visitor v) { v.visitAssert(this); }
1692 
1693         @DefinedBy(Api.COMPILER_TREE)
1694         public Kind getKind() { return Kind.ASSERT; }
1695         @DefinedBy(Api.COMPILER_TREE)
1696         public JCExpression getCondition() { return cond; }
1697         @DefinedBy(Api.COMPILER_TREE)
1698         public JCExpression getDetail() { return detail; }
1699         @Override @DefinedBy(Api.COMPILER_TREE)
1700         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1701             return v.visitAssert(this, d);
1702         }
1703         @Override
1704         public Tag getTag() {
1705             return ASSERT;
1706         }
1707     }
1708 
1709     /**
1710      * A method invocation
1711      */
1712     public static class JCMethodInvocation extends JCPolyExpression implements MethodInvocationTree {
1713         public List<JCExpression> typeargs;
1714         public JCExpression meth;
1715         public List<JCExpression> args;
1716         public Type varargsElement;
1717         protected JCMethodInvocation(List<JCExpression> typeargs,
1718                         JCExpression meth,
1719                         List<JCExpression> args)
1720         {
1721             this.typeargs = (typeargs == null) ? List.nil()
1722                                                : typeargs;
1723             this.meth = meth;
1724             this.args = args;
1725         }
1726         @Override
1727         public void accept(Visitor v) { v.visitApply(this); }
1728 
1729         @DefinedBy(Api.COMPILER_TREE)
1730         public Kind getKind() { return Kind.METHOD_INVOCATION; }
1731         @DefinedBy(Api.COMPILER_TREE)
1732         public List<JCExpression> getTypeArguments() {
1733             return typeargs;
1734         }
1735         @DefinedBy(Api.COMPILER_TREE)
1736         public JCExpression getMethodSelect() { return meth; }
1737         @DefinedBy(Api.COMPILER_TREE)
1738         public List<JCExpression> getArguments() {
1739             return args;
1740         }
1741         @Override @DefinedBy(Api.COMPILER_TREE)
1742         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1743             return v.visitMethodInvocation(this, d);
1744         }
1745         @Override
1746         public JCMethodInvocation setType(Type type) {
1747             super.setType(type);
1748             return this;
1749         }
1750         @Override
1751         public Tag getTag() {
1752             return(APPLY);
1753         }
1754     }
1755 
1756     /**
1757      * A new(...) operation.
1758      */
1759     public static class JCNewClass extends JCPolyExpression implements NewClassTree {
1760         public JCExpression encl;
1761         public List<JCExpression> typeargs;
1762         public JCExpression clazz;
1763         public List<JCExpression> args;
1764         public JCClassDecl def;
1765         public Symbol constructor;
1766         public Type varargsElement;
1767         public Type constructorType;
1768         protected JCNewClass(JCExpression encl,
1769                            List<JCExpression> typeargs,
1770                            JCExpression clazz,
1771                            List<JCExpression> args,
1772                            JCClassDecl def)
1773         {
1774             this.encl = encl;
1775             this.typeargs = (typeargs == null) ? List.nil()
1776                                                : typeargs;
1777             this.clazz = clazz;
1778             this.args = args;
1779             this.def = def;
1780         }
1781         @Override
1782         public void accept(Visitor v) { v.visitNewClass(this); }
1783 
1784         @DefinedBy(Api.COMPILER_TREE)
1785         public Kind getKind() { return Kind.NEW_CLASS; }
1786         @DefinedBy(Api.COMPILER_TREE)
1787         public JCExpression getEnclosingExpression() { // expr.new C< ... > ( ... )
1788             return encl;
1789         }
1790         @DefinedBy(Api.COMPILER_TREE)
1791         public List<JCExpression> getTypeArguments() {
1792             return typeargs;
1793         }
1794         @DefinedBy(Api.COMPILER_TREE)
1795         public JCExpression getIdentifier() { return clazz; }
1796         @DefinedBy(Api.COMPILER_TREE)
1797         public List<JCExpression> getArguments() {
1798             return args;
1799         }
1800         @DefinedBy(Api.COMPILER_TREE)
1801         public JCClassDecl getClassBody() { return def; }
1802         @Override @DefinedBy(Api.COMPILER_TREE)
1803         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1804             return v.visitNewClass(this, d);
1805         }
1806         @Override
1807         public Tag getTag() {
1808             return NEWCLASS;
1809         }
1810 
1811         public boolean classDeclRemoved() {
1812             return false;
1813         }
1814     }
1815 
1816     /**
1817      * A new[...] operation.
1818      */
1819     public static class JCNewArray extends JCExpression implements NewArrayTree {
1820         public JCExpression elemtype;
1821         public List<JCExpression> dims;
1822         // type annotations on inner-most component
1823         public List<JCAnnotation> annotations;
1824         // type annotations on dimensions
1825         public List<List<JCAnnotation>> dimAnnotations;
1826         public List<JCExpression> elems;
1827         protected JCNewArray(JCExpression elemtype,
1828                            List<JCExpression> dims,
1829                            List<JCExpression> elems)
1830         {
1831             this.elemtype = elemtype;
1832             this.dims = dims;
1833             this.annotations = List.nil();
1834             this.dimAnnotations = List.nil();
1835             this.elems = elems;
1836         }
1837         @Override
1838         public void accept(Visitor v) { v.visitNewArray(this); }
1839 
1840         @DefinedBy(Api.COMPILER_TREE)
1841         public Kind getKind() { return Kind.NEW_ARRAY; }
1842         @DefinedBy(Api.COMPILER_TREE)
1843         public JCExpression getType() { return elemtype; }
1844         @DefinedBy(Api.COMPILER_TREE)
1845         public List<JCExpression> getDimensions() {
1846             return dims;
1847         }
1848         @DefinedBy(Api.COMPILER_TREE)
1849         public List<JCExpression> getInitializers() {
1850             return elems;
1851         }
1852         @Override @DefinedBy(Api.COMPILER_TREE)
1853         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1854             return v.visitNewArray(this, d);
1855         }
1856         @Override
1857         public Tag getTag() {
1858             return NEWARRAY;
1859         }
1860 
1861         @Override @DefinedBy(Api.COMPILER_TREE)
1862         public List<JCAnnotation> getAnnotations() {
1863             return annotations;
1864         }
1865 
1866         @Override @DefinedBy(Api.COMPILER_TREE)
1867         public List<List<JCAnnotation>> getDimAnnotations() {
1868             return dimAnnotations;
1869         }
1870     }
1871 
1872     /**
1873      * A lambda expression.
1874      */
1875     public static class JCLambda extends JCFunctionalExpression implements LambdaExpressionTree {
1876 
1877         public enum ParameterKind {
1878             IMPLICIT,
1879             EXPLICIT
1880         }
1881 
1882         public List<JCVariableDecl> params;
1883         public JCTree body;
1884         public boolean canCompleteNormally = true;
1885         public ParameterKind paramKind;
1886 
1887         public JCLambda(List<JCVariableDecl> params,
1888                         JCTree body) {
1889             this.params = params;
1890             this.body = body;
1891             if (params.isEmpty() ||
1892                 params.head.vartype != null) {
1893                 paramKind = ParameterKind.EXPLICIT;
1894             } else {
1895                 paramKind = ParameterKind.IMPLICIT;
1896             }
1897         }
1898         @Override
1899         public Tag getTag() {
1900             return LAMBDA;
1901         }
1902         @Override
1903         public void accept(Visitor v) {
1904             v.visitLambda(this);
1905         }
1906         @Override @DefinedBy(Api.COMPILER_TREE)
1907         public <R, D> R accept(TreeVisitor<R, D> v, D d) {
1908             return v.visitLambdaExpression(this, d);
1909         }
1910         @DefinedBy(Api.COMPILER_TREE)
1911         public Kind getKind() {
1912             return Kind.LAMBDA_EXPRESSION;
1913         }
1914         @DefinedBy(Api.COMPILER_TREE)
1915         public JCTree getBody() {
1916             return body;
1917         }
1918         @DefinedBy(Api.COMPILER_TREE)
1919         public java.util.List<? extends VariableTree> getParameters() {
1920             return params;
1921         }
1922         @Override
1923         public JCLambda setType(Type type) {
1924             super.setType(type);
1925             return this;
1926         }
1927         @Override @DefinedBy(Api.COMPILER_TREE)
1928         public BodyKind getBodyKind() {
1929             return body.hasTag(BLOCK) ?
1930                     BodyKind.STATEMENT :
1931                     BodyKind.EXPRESSION;
1932         }
1933     }
1934 
1935     /**
1936      * A parenthesized subexpression ( ... )
1937      */
1938     public static class JCParens extends JCExpression implements ParenthesizedTree {
1939         public JCExpression expr;
1940         protected JCParens(JCExpression expr) {
1941             this.expr = expr;
1942         }
1943         @Override
1944         public void accept(Visitor v) { v.visitParens(this); }
1945 
1946         @DefinedBy(Api.COMPILER_TREE)
1947         public Kind getKind() { return Kind.PARENTHESIZED; }
1948         @DefinedBy(Api.COMPILER_TREE)
1949         public JCExpression getExpression() { return expr; }
1950         @Override @DefinedBy(Api.COMPILER_TREE)
1951         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1952             return v.visitParenthesized(this, d);
1953         }
1954         @Override
1955         public Tag getTag() {
1956             return PARENS;
1957         }
1958     }
1959 
1960     /**
1961      * A assignment with "=".
1962      */
1963     public static class JCAssign extends JCExpression implements AssignmentTree {
1964         public JCExpression lhs;
1965         public JCExpression rhs;
1966         protected JCAssign(JCExpression lhs, JCExpression rhs) {
1967             this.lhs = lhs;
1968             this.rhs = rhs;
1969         }
1970         @Override
1971         public void accept(Visitor v) { v.visitAssign(this); }
1972 
1973         @DefinedBy(Api.COMPILER_TREE)
1974         public Kind getKind() { return Kind.ASSIGNMENT; }
1975         @DefinedBy(Api.COMPILER_TREE)
1976         public JCExpression getVariable() { return lhs; }
1977         @DefinedBy(Api.COMPILER_TREE)
1978         public JCExpression getExpression() { return rhs; }
1979         @Override @DefinedBy(Api.COMPILER_TREE)
1980         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1981             return v.visitAssignment(this, d);
1982         }
1983         @Override
1984         public Tag getTag() {
1985             return ASSIGN;
1986         }
1987     }
1988 
1989     public static abstract class JCOperatorExpression extends JCExpression {
1990         public enum OperandPos {
1991             LEFT,
1992             RIGHT
1993         }
1994 
1995         protected Tag opcode;
1996         public OperatorSymbol operator;
1997 
1998         public OperatorSymbol getOperator() {
1999             return operator;
2000         }
2001 
2002         @Override
2003         public Tag getTag() {
2004             return opcode;
2005         }
2006 
2007         public abstract JCExpression getOperand(OperandPos pos);
2008     }
2009 
2010     /**
2011      * An assignment with "+=", "|=" ...
2012      */
2013     public static class JCAssignOp extends JCOperatorExpression implements CompoundAssignmentTree {
2014         public JCExpression lhs;
2015         public JCExpression rhs;
2016         protected JCAssignOp(Tag opcode, JCTree lhs, JCTree rhs, OperatorSymbol operator) {
2017             this.opcode = opcode;
2018             this.lhs = (JCExpression)lhs;
2019             this.rhs = (JCExpression)rhs;
2020             this.operator = operator;
2021         }
2022         @Override
2023         public void accept(Visitor v) { v.visitAssignop(this); }
2024 
2025         @DefinedBy(Api.COMPILER_TREE)
2026         public Kind getKind() { return TreeInfo.tagToKind(getTag()); }
2027         @DefinedBy(Api.COMPILER_TREE)
2028         public JCExpression getVariable() { return lhs; }
2029         @DefinedBy(Api.COMPILER_TREE)
2030         public JCExpression getExpression() { return rhs; }
2031         @Override @DefinedBy(Api.COMPILER_TREE)
2032         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2033             return v.visitCompoundAssignment(this, d);
2034         }
2035         @Override
2036         public JCExpression getOperand(OperandPos pos) {
2037             return pos == OperandPos.LEFT ? lhs : rhs;
2038         }
2039     }
2040 
2041     /**
2042      * A unary operation.
2043      */
2044     public static class JCUnary extends JCOperatorExpression implements UnaryTree {
2045         public JCExpression arg;
2046         protected JCUnary(Tag opcode, JCExpression arg) {
2047             this.opcode = opcode;
2048             this.arg = arg;
2049         }
2050         @Override
2051         public void accept(Visitor v) { v.visitUnary(this); }
2052 
2053         @DefinedBy(Api.COMPILER_TREE)
2054         public Kind getKind() { return TreeInfo.tagToKind(getTag()); }
2055         @DefinedBy(Api.COMPILER_TREE)
2056         public JCExpression getExpression() { return arg; }
2057         @Override @DefinedBy(Api.COMPILER_TREE)
2058         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2059             return v.visitUnary(this, d);
2060         }
2061         public void setTag(Tag tag) {
2062             opcode = tag;
2063         }
2064         @Override
2065         public JCExpression getOperand(OperandPos pos) {
2066             return arg;
2067         }
2068     }
2069 
2070     /**
2071      * A binary operation.
2072      */
2073     public static class JCBinary extends JCOperatorExpression implements BinaryTree {
2074         public JCExpression lhs;
2075         public JCExpression rhs;
2076         protected JCBinary(Tag opcode,
2077                          JCExpression lhs,
2078                          JCExpression rhs,
2079                          OperatorSymbol operator) {
2080             this.opcode = opcode;
2081             this.lhs = lhs;
2082             this.rhs = rhs;
2083             this.operator = operator;
2084         }
2085         @Override
2086         public void accept(Visitor v) { v.visitBinary(this); }
2087 
2088         @DefinedBy(Api.COMPILER_TREE)
2089         public Kind getKind() { return TreeInfo.tagToKind(getTag()); }
2090         @DefinedBy(Api.COMPILER_TREE)
2091         public JCExpression getLeftOperand() { return lhs; }
2092         @DefinedBy(Api.COMPILER_TREE)
2093         public JCExpression getRightOperand() { return rhs; }
2094         @Override @DefinedBy(Api.COMPILER_TREE)
2095         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2096             return v.visitBinary(this, d);
2097         }
2098         @Override
2099         public JCExpression getOperand(OperandPos pos) {
2100             return pos == OperandPos.LEFT ? lhs : rhs;
2101         }
2102     }
2103 
2104     /**
2105      * A type cast.
2106      */
2107     public static class JCTypeCast extends JCExpression implements TypeCastTree {
2108         public JCTree clazz;
2109         public JCExpression expr;
2110         protected JCTypeCast(JCTree clazz, JCExpression expr) {
2111             this.clazz = clazz;
2112             this.expr = expr;
2113         }
2114         @Override
2115         public void accept(Visitor v) { v.visitTypeCast(this); }
2116 
2117         @DefinedBy(Api.COMPILER_TREE)
2118         public Kind getKind() { return Kind.TYPE_CAST; }
2119         @DefinedBy(Api.COMPILER_TREE)
2120         public JCTree getType() { return clazz; }
2121         @DefinedBy(Api.COMPILER_TREE)
2122         public JCExpression getExpression() { return expr; }
2123         @Override @DefinedBy(Api.COMPILER_TREE)
2124         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2125             return v.visitTypeCast(this, d);
2126         }
2127         @Override
2128         public Tag getTag() {
2129             return TYPECAST;
2130         }
2131     }
2132 
2133     /**
2134      * A type test.
2135      */
2136     public static class JCInstanceOf extends JCExpression implements InstanceOfTree {
2137         public JCExpression expr;
2138         public JCTree clazz;
2139         protected JCInstanceOf(JCExpression expr, JCTree clazz) {
2140             this.expr = expr;
2141             this.clazz = clazz;
2142         }
2143         @Override
2144         public void accept(Visitor v) { v.visitTypeTest(this); }
2145 
2146         @DefinedBy(Api.COMPILER_TREE)
2147         public Kind getKind() { return Kind.INSTANCE_OF; }
2148         @DefinedBy(Api.COMPILER_TREE)
2149         public JCTree getType() { return clazz; }
2150         @DefinedBy(Api.COMPILER_TREE)
2151         public JCExpression getExpression() { return expr; }
2152         @Override @DefinedBy(Api.COMPILER_TREE)
2153         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2154             return v.visitInstanceOf(this, d);
2155         }
2156         @Override
2157         public Tag getTag() {
2158             return TYPETEST;
2159         }
2160     }
2161 
2162     /**
2163      * An array selection
2164      */
2165     public static class JCArrayAccess extends JCExpression implements ArrayAccessTree {
2166         public JCExpression indexed;
2167         public JCExpression index;
2168         protected JCArrayAccess(JCExpression indexed, JCExpression index) {
2169             this.indexed = indexed;
2170             this.index = index;
2171         }
2172         @Override
2173         public void accept(Visitor v) { v.visitIndexed(this); }
2174 
2175         @DefinedBy(Api.COMPILER_TREE)
2176         public Kind getKind() { return Kind.ARRAY_ACCESS; }
2177         @DefinedBy(Api.COMPILER_TREE)
2178         public JCExpression getExpression() { return indexed; }
2179         @DefinedBy(Api.COMPILER_TREE)
2180         public JCExpression getIndex() { return index; }
2181         @Override @DefinedBy(Api.COMPILER_TREE)
2182         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2183             return v.visitArrayAccess(this, d);
2184         }
2185         @Override
2186         public Tag getTag() {
2187             return INDEXED;
2188         }
2189     }
2190 
2191     /**
2192      * Selects through packages and classes
2193      */
2194     public static class JCFieldAccess extends JCExpression implements MemberSelectTree {
2195         /** selected Tree hierarchy */
2196         public JCExpression selected;
2197         /** name of field to select thru */
2198         public Name name;
2199         /** symbol of the selected class */
2200         public Symbol sym;
2201         protected JCFieldAccess(JCExpression selected, Name name, Symbol sym) {
2202             this.selected = selected;
2203             this.name = name;
2204             this.sym = sym;
2205         }
2206         @Override
2207         public void accept(Visitor v) { v.visitSelect(this); }
2208 
2209         @DefinedBy(Api.COMPILER_TREE)
2210         public Kind getKind() { return Kind.MEMBER_SELECT; }
2211         @DefinedBy(Api.COMPILER_TREE)
2212         public JCExpression getExpression() { return selected; }
2213         @Override @DefinedBy(Api.COMPILER_TREE)
2214         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2215             return v.visitMemberSelect(this, d);
2216         }
2217         @DefinedBy(Api.COMPILER_TREE)
2218         public Name getIdentifier() { return name; }
2219         @Override
2220         public Tag getTag() {
2221             return SELECT;
2222         }
2223     }
2224 
2225     /**
2226      * Selects a member expression.
2227      */
2228     public static class JCMemberReference extends JCFunctionalExpression implements MemberReferenceTree {
2229 
2230         public ReferenceMode mode;
2231         public ReferenceKind kind;
2232         public Name name;
2233         public JCExpression expr;
2234         public List<JCExpression> typeargs;
2235         public Symbol sym;
2236         public Type varargsElement;
2237         public PolyKind refPolyKind;
2238         public boolean ownerAccessible;
2239         private OverloadKind overloadKind;
2240         public Type referentType;
2241 
2242         public enum OverloadKind {
2243             OVERLOADED,
2244             UNOVERLOADED,
2245             ERROR
2246         }
2247 
2248         /**
2249          * Javac-dependent classification for member references, based
2250          * on relevant properties w.r.t. code-generation
2251          */
2252         public enum ReferenceKind {
2253             /** super # instMethod */
2254             SUPER(ReferenceMode.INVOKE, false),
2255             /** Type # instMethod */
2256             UNBOUND(ReferenceMode.INVOKE, true),
2257             /** Type # staticMethod */
2258             STATIC(ReferenceMode.INVOKE, false),
2259             /** Expr # instMethod */
2260             BOUND(ReferenceMode.INVOKE, false),
2261             /** Inner # new */
2262             IMPLICIT_INNER(ReferenceMode.NEW, false),
2263             /** Toplevel # new */
2264             TOPLEVEL(ReferenceMode.NEW, false),
2265             /** ArrayType # new */
2266             ARRAY_CTOR(ReferenceMode.NEW, false);
2267 
2268             final ReferenceMode mode;
2269             final boolean unbound;
2270 
2271             private ReferenceKind(ReferenceMode mode, boolean unbound) {
2272                 this.mode = mode;
2273                 this.unbound = unbound;
2274             }
2275 
2276             public boolean isUnbound() {
2277                 return unbound;
2278             }
2279         }
2280 
2281         public JCMemberReference(ReferenceMode mode, Name name, JCExpression expr, List<JCExpression> typeargs) {
2282             this.mode = mode;
2283             this.name = name;
2284             this.expr = expr;
2285             this.typeargs = typeargs;
2286         }
2287         @Override
2288         public void accept(Visitor v) { v.visitReference(this); }
2289 
2290         @DefinedBy(Api.COMPILER_TREE)
2291         public Kind getKind() { return Kind.MEMBER_REFERENCE; }
2292         @Override @DefinedBy(Api.COMPILER_TREE)
2293         public ReferenceMode getMode() { return mode; }
2294         @Override @DefinedBy(Api.COMPILER_TREE)
2295         public JCExpression getQualifierExpression() { return expr; }
2296         @Override @DefinedBy(Api.COMPILER_TREE)
2297         public Name getName() { return name; }
2298         @Override @DefinedBy(Api.COMPILER_TREE)
2299         public List<JCExpression> getTypeArguments() { return typeargs; }
2300 
2301         @Override @DefinedBy(Api.COMPILER_TREE)
2302         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2303             return v.visitMemberReference(this, d);
2304         }
2305         @Override
2306         public Tag getTag() {
2307             return REFERENCE;
2308         }
2309         public boolean hasKind(ReferenceKind kind) {
2310             return this.kind == kind;
2311         }
2312 
2313         /**
2314          * @return the overloadKind
2315          */
2316         public OverloadKind getOverloadKind() {
2317             return overloadKind;
2318         }
2319 
2320         /**
2321          * @param overloadKind the overloadKind to set
2322          */
2323         public void setOverloadKind(OverloadKind overloadKind) {
2324             this.overloadKind = overloadKind;
2325         }
2326     }
2327 
2328     /**
2329      * An identifier
2330      */
2331     public static class JCIdent extends JCExpression implements IdentifierTree {
2332         /** the name */
2333         public Name name;
2334         /** the symbol */
2335         public Symbol sym;
2336         protected JCIdent(Name name, Symbol sym) {
2337             this.name = name;
2338             this.sym = sym;
2339         }
2340         @Override
2341         public void accept(Visitor v) { v.visitIdent(this); }
2342 
2343         @DefinedBy(Api.COMPILER_TREE)
2344         public Kind getKind() { return Kind.IDENTIFIER; }
2345         @DefinedBy(Api.COMPILER_TREE)
2346         public Name getName() { return name; }
2347         @Override @DefinedBy(Api.COMPILER_TREE)
2348         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2349             return v.visitIdentifier(this, d);
2350         }
2351         @Override
2352         public Tag getTag() {
2353             return IDENT;
2354         }
2355     }
2356 
2357     /**
2358      * A constant value given literally.
2359      */
2360     public static class JCLiteral extends JCExpression implements LiteralTree {
2361         public TypeTag typetag;
2362         /** value representation */
2363         public Object value;
2364         protected JCLiteral(TypeTag typetag, Object value) {
2365             this.typetag = typetag;
2366             this.value = value;
2367         }
2368         @Override
2369         public void accept(Visitor v) { v.visitLiteral(this); }
2370 
2371         @DefinedBy(Api.COMPILER_TREE)
2372         public Kind getKind() {
2373             return typetag.getKindLiteral();
2374         }
2375 
2376         @DefinedBy(Api.COMPILER_TREE)
2377         public Object getValue() {
2378             switch (typetag) {
2379                 case BOOLEAN:
2380                     int bi = (Integer) value;
2381                     return (bi != 0);
2382                 case CHAR:
2383                     int ci = (Integer) value;
2384                     char c = (char) ci;
2385                     if (c != ci)
2386                         throw new AssertionError("bad value for char literal");
2387                     return c;
2388                 default:
2389                     return value;
2390             }
2391         }
2392         @Override @DefinedBy(Api.COMPILER_TREE)
2393         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2394             return v.visitLiteral(this, d);
2395         }
2396         @Override
2397         public JCLiteral setType(Type type) {
2398             super.setType(type);
2399             return this;
2400         }
2401         @Override
2402         public Tag getTag() {
2403             return LITERAL;
2404         }
2405     }
2406 
2407     /**
2408      * Identifies a basic type.
2409      * @see TypeTag
2410      */
2411     public static class JCPrimitiveTypeTree extends JCExpression implements PrimitiveTypeTree {
2412         /** the basic type id */
2413         public TypeTag typetag;
2414         protected JCPrimitiveTypeTree(TypeTag typetag) {
2415             this.typetag = typetag;
2416         }
2417         @Override
2418         public void accept(Visitor v) { v.visitTypeIdent(this); }
2419 
2420         @DefinedBy(Api.COMPILER_TREE)
2421         public Kind getKind() { return Kind.PRIMITIVE_TYPE; }
2422         @DefinedBy(Api.COMPILER_TREE)
2423         public TypeKind getPrimitiveTypeKind() {
2424             return typetag.getPrimitiveTypeKind();
2425         }
2426 
2427         @Override @DefinedBy(Api.COMPILER_TREE)
2428         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2429             return v.visitPrimitiveType(this, d);
2430         }
2431         @Override
2432         public Tag getTag() {
2433             return TYPEIDENT;
2434         }
2435     }
2436 
2437     /**
2438      * An array type, A[]
2439      */
2440     public static class JCArrayTypeTree extends JCExpression implements ArrayTypeTree {
2441         public JCExpression elemtype;
2442         protected JCArrayTypeTree(JCExpression elemtype) {
2443             this.elemtype = elemtype;
2444         }
2445         @Override
2446         public void accept(Visitor v) { v.visitTypeArray(this); }
2447 
2448         @DefinedBy(Api.COMPILER_TREE)
2449         public Kind getKind() { return Kind.ARRAY_TYPE; }
2450         @DefinedBy(Api.COMPILER_TREE)
2451         public JCTree getType() { return elemtype; }
2452         @Override @DefinedBy(Api.COMPILER_TREE)
2453         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2454             return v.visitArrayType(this, d);
2455         }
2456         @Override
2457         public Tag getTag() {
2458             return TYPEARRAY;
2459         }
2460     }
2461 
2462     /**
2463      * A parameterized type, {@literal T<...>}
2464      */
2465     public static class JCTypeApply extends JCExpression implements ParameterizedTypeTree {
2466         public JCExpression clazz;
2467         public List<JCExpression> arguments;
2468         protected JCTypeApply(JCExpression clazz, List<JCExpression> arguments) {
2469             this.clazz = clazz;
2470             this.arguments = arguments;
2471         }
2472         @Override
2473         public void accept(Visitor v) { v.visitTypeApply(this); }
2474 
2475         @DefinedBy(Api.COMPILER_TREE)
2476         public Kind getKind() { return Kind.PARAMETERIZED_TYPE; }
2477         @DefinedBy(Api.COMPILER_TREE)
2478         public JCTree getType() { return clazz; }
2479         @DefinedBy(Api.COMPILER_TREE)
2480         public List<JCExpression> getTypeArguments() {
2481             return arguments;
2482         }
2483         @Override @DefinedBy(Api.COMPILER_TREE)
2484         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2485             return v.visitParameterizedType(this, d);
2486         }
2487         @Override
2488         public Tag getTag() {
2489             return TYPEAPPLY;
2490         }
2491     }
2492 
2493     /**
2494      * A union type, T1 | T2 | ... Tn (used in multicatch statements)
2495      */
2496     public static class JCTypeUnion extends JCExpression implements UnionTypeTree {
2497 
2498         public List<JCExpression> alternatives;
2499 
2500         protected JCTypeUnion(List<JCExpression> components) {
2501             this.alternatives = components;
2502         }
2503         @Override
2504         public void accept(Visitor v) { v.visitTypeUnion(this); }
2505 
2506         @DefinedBy(Api.COMPILER_TREE)
2507         public Kind getKind() { return Kind.UNION_TYPE; }
2508 
2509         @DefinedBy(Api.COMPILER_TREE)
2510         public List<JCExpression> getTypeAlternatives() {
2511             return alternatives;
2512         }
2513         @Override @DefinedBy(Api.COMPILER_TREE)
2514         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2515             return v.visitUnionType(this, d);
2516         }
2517         @Override
2518         public Tag getTag() {
2519             return TYPEUNION;
2520         }
2521     }
2522 
2523     /**
2524      * An intersection type, {@code T1 & T2 & ... Tn} (used in cast expressions)
2525      */
2526     public static class JCTypeIntersection extends JCExpression implements IntersectionTypeTree {
2527 
2528         public List<JCExpression> bounds;
2529 
2530         protected JCTypeIntersection(List<JCExpression> bounds) {
2531             this.bounds = bounds;
2532         }
2533         @Override
2534         public void accept(Visitor v) { v.visitTypeIntersection(this); }
2535 
2536         @DefinedBy(Api.COMPILER_TREE)
2537         public Kind getKind() { return Kind.INTERSECTION_TYPE; }
2538 
2539         @DefinedBy(Api.COMPILER_TREE)
2540         public List<JCExpression> getBounds() {
2541             return bounds;
2542         }
2543         @Override @DefinedBy(Api.COMPILER_TREE)
2544         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2545             return v.visitIntersectionType(this, d);
2546         }
2547         @Override
2548         public Tag getTag() {
2549             return TYPEINTERSECTION;
2550         }
2551     }
2552 
2553     /**
2554      * A formal class parameter.
2555      */
2556     public static class JCTypeParameter extends JCTree implements TypeParameterTree {
2557         /** name */
2558         public Name name;
2559         /** bounds */
2560         public List<JCExpression> bounds;
2561         /** type annotations on type parameter */
2562         public List<JCAnnotation> annotations;
2563         protected JCTypeParameter(Name name, List<JCExpression> bounds, List<JCAnnotation> annotations) {
2564             this.name = name;
2565             this.bounds = bounds;
2566             this.annotations = annotations;
2567         }
2568         @Override
2569         public void accept(Visitor v) { v.visitTypeParameter(this); }
2570 
2571         @DefinedBy(Api.COMPILER_TREE)
2572         public Kind getKind() { return Kind.TYPE_PARAMETER; }
2573         @DefinedBy(Api.COMPILER_TREE)
2574         public Name getName() { return name; }
2575         @DefinedBy(Api.COMPILER_TREE)
2576         public List<JCExpression> getBounds() {
2577             return bounds;
2578         }
2579         @DefinedBy(Api.COMPILER_TREE)
2580         public List<JCAnnotation> getAnnotations() {
2581             return annotations;
2582         }
2583         @Override @DefinedBy(Api.COMPILER_TREE)
2584         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2585             return v.visitTypeParameter(this, d);
2586         }
2587         @Override
2588         public Tag getTag() {
2589             return TYPEPARAMETER;
2590         }
2591     }
2592 
2593     public static class JCWildcard extends JCExpression implements WildcardTree {
2594         public TypeBoundKind kind;
2595         public JCTree inner;
2596         protected JCWildcard(TypeBoundKind kind, JCTree inner) {
2597             this.kind = Assert.checkNonNull(kind);
2598             this.inner = inner;
2599         }
2600         @Override
2601         public void accept(Visitor v) { v.visitWildcard(this); }
2602 
2603         @DefinedBy(Api.COMPILER_TREE)
2604         public Kind getKind() {
2605             switch (kind.kind) {
2606             case UNBOUND:
2607                 return Kind.UNBOUNDED_WILDCARD;
2608             case EXTENDS:
2609                 return Kind.EXTENDS_WILDCARD;
2610             case SUPER:
2611                 return Kind.SUPER_WILDCARD;
2612             default:
2613                 throw new AssertionError("Unknown wildcard bound " + kind);
2614             }
2615         }
2616         @DefinedBy(Api.COMPILER_TREE)
2617         public JCTree getBound() { return inner; }
2618         @Override @DefinedBy(Api.COMPILER_TREE)
2619         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2620             return v.visitWildcard(this, d);
2621         }
2622         @Override
2623         public Tag getTag() {
2624             return Tag.WILDCARD;
2625         }
2626     }
2627 
2628     public static class TypeBoundKind extends JCTree {
2629         public BoundKind kind;
2630         protected TypeBoundKind(BoundKind kind) {
2631             this.kind = kind;
2632         }
2633         @Override
2634         public void accept(Visitor v) { v.visitTypeBoundKind(this); }
2635 
2636         @DefinedBy(Api.COMPILER_TREE)
2637         public Kind getKind() {
2638             throw new AssertionError("TypeBoundKind is not part of a public API");
2639         }
2640         @Override @DefinedBy(Api.COMPILER_TREE)
2641         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2642             throw new AssertionError("TypeBoundKind is not part of a public API");
2643         }
2644         @Override
2645         public Tag getTag() {
2646             return TYPEBOUNDKIND;
2647         }
2648     }
2649 
2650     public static class JCAnnotation extends JCExpression implements AnnotationTree {
2651         // Either Tag.ANNOTATION or Tag.TYPE_ANNOTATION
2652         private Tag tag;
2653 
2654         public JCTree annotationType;
2655         public List<JCExpression> args;
2656         public Attribute.Compound attribute;
2657 
2658         protected JCAnnotation(Tag tag, JCTree annotationType, List<JCExpression> args) {
2659             this.tag = tag;
2660             this.annotationType = annotationType;
2661             this.args = args;
2662         }
2663 
2664         @Override
2665         public void accept(Visitor v) { v.visitAnnotation(this); }
2666 
2667         @DefinedBy(Api.COMPILER_TREE)
2668         public Kind getKind() { return TreeInfo.tagToKind(getTag()); }
2669 
2670         @DefinedBy(Api.COMPILER_TREE)
2671         public JCTree getAnnotationType() { return annotationType; }
2672         @DefinedBy(Api.COMPILER_TREE)
2673         public List<JCExpression> getArguments() {
2674             return args;
2675         }
2676         @Override @DefinedBy(Api.COMPILER_TREE)
2677         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2678             return v.visitAnnotation(this, d);
2679         }
2680         @Override
2681         public Tag getTag() {
2682             return tag;
2683         }
2684     }
2685 
2686     public static class JCModifiers extends JCTree implements com.sun.source.tree.ModifiersTree {
2687         public long flags;
2688         public List<JCAnnotation> annotations;
2689         protected JCModifiers(long flags, List<JCAnnotation> annotations) {
2690             this.flags = flags;
2691             this.annotations = annotations;
2692         }
2693         @Override
2694         public void accept(Visitor v) { v.visitModifiers(this); }
2695 
2696         @DefinedBy(Api.COMPILER_TREE)
2697         public Kind getKind() { return Kind.MODIFIERS; }
2698         @DefinedBy(Api.COMPILER_TREE)
2699         public Set<Modifier> getFlags() {
2700             return Flags.asModifierSet(flags);
2701         }
2702         @DefinedBy(Api.COMPILER_TREE)
2703         public List<JCAnnotation> getAnnotations() {
2704             return annotations;
2705         }
2706         @Override @DefinedBy(Api.COMPILER_TREE)
2707         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2708             return v.visitModifiers(this, d);
2709         }
2710         @Override
2711         public Tag getTag() {
2712             return MODIFIERS;
2713         }
2714     }
2715 
2716     public static class JCAnnotatedType extends JCExpression implements com.sun.source.tree.AnnotatedTypeTree {
2717         // type annotations
2718         public List<JCAnnotation> annotations;
2719         public JCExpression underlyingType;
2720 
2721         protected JCAnnotatedType(List<JCAnnotation> annotations, JCExpression underlyingType) {
2722             Assert.check(annotations != null && annotations.nonEmpty());
2723             this.annotations = annotations;
2724             this.underlyingType = underlyingType;
2725         }
2726         @Override
2727         public void accept(Visitor v) { v.visitAnnotatedType(this); }
2728 
2729         @DefinedBy(Api.COMPILER_TREE)
2730         public Kind getKind() { return Kind.ANNOTATED_TYPE; }
2731         @DefinedBy(Api.COMPILER_TREE)
2732         public List<JCAnnotation> getAnnotations() {
2733             return annotations;
2734         }
2735         @DefinedBy(Api.COMPILER_TREE)
2736         public JCExpression getUnderlyingType() {
2737             return underlyingType;
2738         }
2739         @Override @DefinedBy(Api.COMPILER_TREE)
2740         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2741             return v.visitAnnotatedType(this, d);
2742         }
2743         @Override
2744         public Tag getTag() {
2745             return ANNOTATED_TYPE;
2746         }
2747     }
2748 
2749     public static abstract class JCDirective extends JCTree
2750         implements DirectiveTree {
2751     }
2752 
2753     public static class JCModuleDecl extends JCTree implements ModuleTree {
2754         public JCModifiers mods;
2755         public ModuleType type;
2756         private final ModuleKind kind;
2757         public JCExpression qualId;
2758         public List<JCDirective> directives;
2759         public ModuleSymbol sym;
2760 
2761         protected JCModuleDecl(JCModifiers mods, ModuleKind kind,
2762                 JCExpression qualId, List<JCDirective> directives) {
2763             this.mods = mods;
2764             this.kind = kind;
2765             this.qualId = qualId;
2766             this.directives = directives;
2767         }
2768 
2769         @Override
2770         public void accept(Visitor v) { v.visitModuleDef(this); }
2771 
2772         @Override @DefinedBy(Api.COMPILER_TREE)
2773         public Kind getKind() {
2774             return Kind.MODULE;
2775         }
2776 
2777         @Override @DefinedBy(Api.COMPILER_TREE)
2778         public List<? extends AnnotationTree> getAnnotations() {
2779             return mods.annotations;
2780         }
2781 
2782         @Override @DefinedBy(Api.COMPILER_TREE)
2783         public ModuleKind getModuleType() {
2784             return kind;
2785         }
2786 
2787         @Override @DefinedBy(Api.COMPILER_TREE)
2788         public JCExpression getName() {
2789             return qualId;
2790         }
2791 
2792         @Override @DefinedBy(Api.COMPILER_TREE)
2793         public List<JCDirective> getDirectives() {
2794             return directives;
2795         }
2796 
2797         @Override @DefinedBy(Api.COMPILER_TREE)
2798         public <R, D> R accept(TreeVisitor<R, D> v, D d) {
2799             return v.visitModule(this, d);
2800         }
2801 
2802         @Override
2803         public Tag getTag() {
2804             return MODULEDEF;
2805         }
2806     }
2807 
2808     public static class JCExports extends JCDirective
2809             implements ExportsTree {
2810         public JCExpression qualid;
2811         public List<JCExpression> moduleNames;
2812         public ExportsDirective directive;
2813 
2814         protected JCExports(JCExpression qualId, List<JCExpression> moduleNames) {
2815             this.qualid = qualId;
2816             this.moduleNames = moduleNames;
2817         }
2818 
2819         @Override
2820         public void accept(Visitor v) { v.visitExports(this); }
2821 
2822         @Override @DefinedBy(Api.COMPILER_TREE)
2823         public Kind getKind() {
2824             return Kind.EXPORTS;
2825         }
2826 
2827         @Override @DefinedBy(Api.COMPILER_TREE)
2828         public JCExpression getPackageName() {
2829             return qualid;
2830         }
2831 
2832         @Override @DefinedBy(Api.COMPILER_TREE)
2833         public List<JCExpression> getModuleNames() {
2834             return moduleNames;
2835         }
2836 
2837         @Override @DefinedBy(Api.COMPILER_TREE)
2838         public <R, D> R accept(TreeVisitor<R, D> v, D d) {
2839             return v.visitExports(this, d);
2840         }
2841 
2842         @Override
2843         public Tag getTag() {
2844             return Tag.EXPORTS;
2845         }
2846     }
2847 
2848     public static class JCOpens extends JCDirective
2849             implements OpensTree {
2850         public JCExpression qualid;
2851         public List<JCExpression> moduleNames;
2852         public OpensDirective directive;
2853 
2854         protected JCOpens(JCExpression qualId, List<JCExpression> moduleNames) {
2855             this.qualid = qualId;
2856             this.moduleNames = moduleNames;
2857         }
2858 
2859         @Override
2860         public void accept(Visitor v) { v.visitOpens(this); }
2861 
2862         @Override @DefinedBy(Api.COMPILER_TREE)
2863         public Kind getKind() {
2864             return Kind.OPENS;
2865         }
2866 
2867         @Override @DefinedBy(Api.COMPILER_TREE)
2868         public JCExpression getPackageName() {
2869             return qualid;
2870         }
2871 
2872         @Override @DefinedBy(Api.COMPILER_TREE)
2873         public List<JCExpression> getModuleNames() {
2874             return moduleNames;
2875         }
2876 
2877         @Override @DefinedBy(Api.COMPILER_TREE)
2878         public <R, D> R accept(TreeVisitor<R, D> v, D d) {
2879             return v.visitOpens(this, d);
2880         }
2881 
2882         @Override
2883         public Tag getTag() {
2884             return Tag.OPENS;
2885         }
2886     }
2887 
2888     public static class JCProvides extends JCDirective
2889             implements ProvidesTree {
2890         public JCExpression serviceName;
2891         public List<JCExpression> implNames;
2892 
2893         protected JCProvides(JCExpression serviceName, List<JCExpression> implNames) {
2894             this.serviceName = serviceName;
2895             this.implNames = implNames;
2896         }
2897 
2898         @Override
2899         public void accept(Visitor v) { v.visitProvides(this); }
2900 
2901         @Override @DefinedBy(Api.COMPILER_TREE)
2902         public Kind getKind() {
2903             return Kind.PROVIDES;
2904         }
2905 
2906         @Override @DefinedBy(Api.COMPILER_TREE)
2907         public <R, D> R accept(TreeVisitor<R, D> v, D d) {
2908             return v.visitProvides(this, d);
2909         }
2910 
2911         @Override @DefinedBy(Api.COMPILER_TREE)
2912         public JCExpression getServiceName() {
2913             return serviceName;
2914         }
2915 
2916         @Override @DefinedBy(Api.COMPILER_TREE)
2917         public List<JCExpression> getImplementationNames() {
2918             return implNames;
2919         }
2920 
2921         @Override
2922         public Tag getTag() {
2923             return PROVIDES;
2924         }
2925     }
2926 
2927     public static class JCRequires extends JCDirective
2928             implements RequiresTree {
2929         public boolean isTransitive;
2930         public boolean isStaticPhase;
2931         public JCExpression moduleName;
2932         public RequiresDirective directive;
2933 
2934         protected JCRequires(boolean isTransitive, boolean isStaticPhase, JCExpression moduleName) {
2935             this.isTransitive = isTransitive;
2936             this.isStaticPhase = isStaticPhase;
2937             this.moduleName = moduleName;
2938         }
2939 
2940         @Override
2941         public void accept(Visitor v) { v.visitRequires(this); }
2942 
2943         @Override @DefinedBy(Api.COMPILER_TREE)
2944         public Kind getKind() {
2945             return Kind.REQUIRES;
2946         }
2947 
2948         @Override @DefinedBy(Api.COMPILER_TREE)
2949         public <R, D> R accept(TreeVisitor<R, D> v, D d) {
2950             return v.visitRequires(this, d);
2951         }
2952 
2953         @Override @DefinedBy(Api.COMPILER_TREE)
2954         public boolean isTransitive() {
2955             return isTransitive;
2956         }
2957 
2958         @Override @DefinedBy(Api.COMPILER_TREE)
2959         public boolean isStatic() {
2960             return isStaticPhase;
2961         }
2962 
2963         @Override @DefinedBy(Api.COMPILER_TREE)
2964         public JCExpression getModuleName() {
2965             return moduleName;
2966         }
2967 
2968         @Override
2969         public Tag getTag() {
2970             return REQUIRES;
2971         }
2972     }
2973 
2974     public static class JCUses extends JCDirective
2975             implements UsesTree {
2976         public JCExpression qualid;
2977 
2978         protected JCUses(JCExpression qualId) {
2979             this.qualid = qualId;
2980         }
2981 
2982         @Override
2983         public void accept(Visitor v) { v.visitUses(this); }
2984 
2985         @Override @DefinedBy(Api.COMPILER_TREE)
2986         public Kind getKind() {
2987             return Kind.USES;
2988         }
2989 
2990         @Override @DefinedBy(Api.COMPILER_TREE)
2991         public JCExpression getServiceName() {
2992             return qualid;
2993         }
2994 
2995         @Override @DefinedBy(Api.COMPILER_TREE)
2996         public <R, D> R accept(TreeVisitor<R, D> v, D d) {
2997             return v.visitUses(this, d);
2998         }
2999 
3000         @Override
3001         public Tag getTag() {
3002             return USES;
3003         }
3004     }
3005 
3006     public static class JCErroneous extends JCExpression
3007             implements ErroneousTree {
3008         public List<? extends JCTree> errs;
3009         protected JCErroneous(List<? extends JCTree> errs) {
3010             this.errs = errs;
3011         }
3012         @Override
3013         public void accept(Visitor v) { v.visitErroneous(this); }
3014 
3015         @DefinedBy(Api.COMPILER_TREE)
3016         public Kind getKind() { return Kind.ERRONEOUS; }
3017 
3018         @DefinedBy(Api.COMPILER_TREE)
3019         public List<? extends JCTree> getErrorTrees() {
3020             return errs;
3021         }
3022 
3023         @Override @DefinedBy(Api.COMPILER_TREE)
3024         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
3025             return v.visitErroneous(this, d);
3026         }
3027         @Override
3028         public Tag getTag() {
3029             return ERRONEOUS;
3030         }
3031     }
3032 
3033     /** (let int x = 3; in x+2) */
3034     public static class LetExpr extends JCExpression {
3035         public List<JCStatement> defs;
3036         public JCExpression expr;
3037         /**true if a expr should be run through Gen.genCond:*/
3038         public boolean needsCond;
3039         protected LetExpr(List<JCStatement> defs, JCExpression expr) {
3040             this.defs = defs;
3041             this.expr = expr;
3042         }
3043         @Override
3044         public void accept(Visitor v) { v.visitLetExpr(this); }
3045 
3046         @DefinedBy(Api.COMPILER_TREE)
3047         public Kind getKind() {
3048             throw new AssertionError("LetExpr is not part of a public API");
3049         }
3050         @Override @DefinedBy(Api.COMPILER_TREE)
3051         public <R,D> R accept(TreeVisitor<R,D> v, D d) {
3052             throw new AssertionError("LetExpr is not part of a public API");
3053         }
3054         @Override
3055         public Tag getTag() {
3056             return LETEXPR;
3057         }
3058     }
3059 
3060     /** An interface for tree factories
3061      */
3062     public interface Factory {
3063         JCCompilationUnit TopLevel(List<JCTree> defs);
3064         JCPackageDecl PackageDecl(List<JCAnnotation> annotations,
3065                                   JCExpression pid);
3066         JCImport Import(JCTree qualid, boolean staticImport);
3067         JCClassDecl ClassDef(JCModifiers mods,
3068                           Name name,
3069                           List<JCTypeParameter> typarams,
3070                           JCExpression extending,
3071                           List<JCExpression> implementing,
3072                           List<JCTree> defs);
3073         JCMethodDecl MethodDef(JCModifiers mods,
3074                             Name name,
3075                             JCExpression restype,
3076                             List<JCTypeParameter> typarams,
3077                             JCVariableDecl recvparam,
3078                             List<JCVariableDecl> params,
3079                             List<JCExpression> thrown,
3080                             JCBlock body,
3081                             JCExpression defaultValue);
3082         JCVariableDecl VarDef(JCModifiers mods,
3083                       Name name,
3084                       JCExpression vartype,
3085                       JCExpression init);
3086         JCSkip Skip();
3087         JCBlock Block(long flags, List<JCStatement> stats);
3088         JCDoWhileLoop DoLoop(JCStatement body, JCExpression cond);
3089         JCWhileLoop WhileLoop(JCExpression cond, JCStatement body);
3090         JCForLoop ForLoop(List<JCStatement> init,
3091                         JCExpression cond,
3092                         List<JCExpressionStatement> step,
3093                         JCStatement body);
3094         JCEnhancedForLoop ForeachLoop(JCVariableDecl var, JCExpression expr, JCStatement body);
3095         JCLabeledStatement Labelled(Name label, JCStatement body);
3096         JCSwitch Switch(JCExpression selector, List<JCCase> cases);
3097         JCSwitchExpression SwitchExpression(JCExpression selector, List<JCCase> cases);
3098         JCCase Case(CaseTree.CaseKind caseKind, List<JCExpression> pat,
3099                     List<JCStatement> stats, JCTree body);
3100         JCSynchronized Synchronized(JCExpression lock, JCBlock body);
3101         JCTry Try(JCBlock body, List<JCCatch> catchers, JCBlock finalizer);
3102         JCTry Try(List<JCTree> resources,
3103                   JCBlock body,
3104                   List<JCCatch> catchers,
3105                   JCBlock finalizer);
3106         JCCatch Catch(JCVariableDecl param, JCBlock body);
3107         JCConditional Conditional(JCExpression cond,
3108                                 JCExpression thenpart,
3109                                 JCExpression elsepart);
3110         JCIf If(JCExpression cond, JCStatement thenpart, JCStatement elsepart);
3111         JCExpressionStatement Exec(JCExpression expr);
3112         JCBreak Break(Name label);
3113         JCYield Yield(JCExpression value);
3114         JCContinue Continue(Name label);
3115         JCReturn Return(JCExpression expr);
3116         JCThrow Throw(JCExpression expr);
3117         JCAssert Assert(JCExpression cond, JCExpression detail);
3118         JCMethodInvocation Apply(List<JCExpression> typeargs,
3119                     JCExpression fn,
3120                     List<JCExpression> args);
3121         JCNewClass NewClass(JCExpression encl,
3122                           List<JCExpression> typeargs,
3123                           JCExpression clazz,
3124                           List<JCExpression> args,
3125                           JCClassDecl def);
3126         JCNewArray NewArray(JCExpression elemtype,
3127                           List<JCExpression> dims,
3128                           List<JCExpression> elems);
3129         JCParens Parens(JCExpression expr);
3130         JCAssign Assign(JCExpression lhs, JCExpression rhs);
3131         JCAssignOp Assignop(Tag opcode, JCTree lhs, JCTree rhs);
3132         JCUnary Unary(Tag opcode, JCExpression arg);
3133         JCBinary Binary(Tag opcode, JCExpression lhs, JCExpression rhs);
3134         JCTypeCast TypeCast(JCTree expr, JCExpression type);
3135         JCInstanceOf TypeTest(JCExpression expr, JCTree clazz);
3136         JCArrayAccess Indexed(JCExpression indexed, JCExpression index);
3137         JCFieldAccess Select(JCExpression selected, Name selector);
3138         JCIdent Ident(Name idname);
3139         JCLiteral Literal(TypeTag tag, Object value);
3140         JCPrimitiveTypeTree TypeIdent(TypeTag typetag);
3141         JCArrayTypeTree TypeArray(JCExpression elemtype);
3142         JCTypeApply TypeApply(JCExpression clazz, List<JCExpression> arguments);
3143         JCTypeParameter TypeParameter(Name name, List<JCExpression> bounds);
3144         JCWildcard Wildcard(TypeBoundKind kind, JCTree type);
3145         TypeBoundKind TypeBoundKind(BoundKind kind);
3146         JCAnnotation Annotation(JCTree annotationType, List<JCExpression> args);
3147         JCModifiers Modifiers(long flags, List<JCAnnotation> annotations);
3148         JCErroneous Erroneous(List<? extends JCTree> errs);
3149         JCModuleDecl ModuleDef(JCModifiers mods, ModuleKind kind, JCExpression qualId, List<JCDirective> directives);
3150         JCExports Exports(JCExpression qualId, List<JCExpression> moduleNames);
3151         JCOpens Opens(JCExpression qualId, List<JCExpression> moduleNames);
3152         JCProvides Provides(JCExpression serviceName, List<JCExpression> implNames);
3153         JCRequires Requires(boolean isTransitive, boolean isStaticPhase, JCExpression qualId);
3154         JCUses Uses(JCExpression qualId);
3155         LetExpr LetExpr(List<JCStatement> defs, JCExpression expr);
3156     }
3157 
3158     /** A generic visitor class for trees.
3159      */
3160     public static abstract class Visitor {
3161         public void visitTopLevel(JCCompilationUnit that)    { visitTree(that); }
3162         public void visitPackageDef(JCPackageDecl that)      { visitTree(that); }
3163         public void visitImport(JCImport that)               { visitTree(that); }
3164         public void visitClassDef(JCClassDecl that)          { visitTree(that); }
3165         public void visitMethodDef(JCMethodDecl that)        { visitTree(that); }
3166         public void visitVarDef(JCVariableDecl that)         { visitTree(that); }
3167         public void visitSkip(JCSkip that)                   { visitTree(that); }
3168         public void visitBlock(JCBlock that)                 { visitTree(that); }
3169         public void visitDoLoop(JCDoWhileLoop that)          { visitTree(that); }
3170         public void visitWhileLoop(JCWhileLoop that)         { visitTree(that); }
3171         public void visitForLoop(JCForLoop that)             { visitTree(that); }
3172         public void visitForeachLoop(JCEnhancedForLoop that) { visitTree(that); }
3173         public void visitLabelled(JCLabeledStatement that)   { visitTree(that); }
3174         public void visitSwitch(JCSwitch that)               { visitTree(that); }
3175         public void visitCase(JCCase that)                   { visitTree(that); }
3176         public void visitSwitchExpression(JCSwitchExpression that)               { visitTree(that); }
3177         public void visitSynchronized(JCSynchronized that)   { visitTree(that); }
3178         public void visitTry(JCTry that)                     { visitTree(that); }
3179         public void visitCatch(JCCatch that)                 { visitTree(that); }
3180         public void visitConditional(JCConditional that)     { visitTree(that); }
3181         public void visitIf(JCIf that)                       { visitTree(that); }
3182         public void visitExec(JCExpressionStatement that)    { visitTree(that); }
3183         public void visitBreak(JCBreak that)                 { visitTree(that); }
3184         public void visitYield(JCYield that)                 { visitTree(that); }
3185         public void visitContinue(JCContinue that)           { visitTree(that); }
3186         public void visitReturn(JCReturn that)               { visitTree(that); }
3187         public void visitThrow(JCThrow that)                 { visitTree(that); }
3188         public void visitAssert(JCAssert that)               { visitTree(that); }
3189         public void visitApply(JCMethodInvocation that)      { visitTree(that); }
3190         public void visitNewClass(JCNewClass that)           { visitTree(that); }
3191         public void visitNewArray(JCNewArray that)           { visitTree(that); }
3192         public void visitLambda(JCLambda that)               { visitTree(that); }
3193         public void visitParens(JCParens that)               { visitTree(that); }
3194         public void visitAssign(JCAssign that)               { visitTree(that); }
3195         public void visitAssignop(JCAssignOp that)           { visitTree(that); }
3196         public void visitUnary(JCUnary that)                 { visitTree(that); }
3197         public void visitBinary(JCBinary that)               { visitTree(that); }
3198         public void visitTypeCast(JCTypeCast that)           { visitTree(that); }
3199         public void visitTypeTest(JCInstanceOf that)         { visitTree(that); }
3200         public void visitIndexed(JCArrayAccess that)         { visitTree(that); }
3201         public void visitSelect(JCFieldAccess that)          { visitTree(that); }
3202         public void visitReference(JCMemberReference that)   { visitTree(that); }
3203         public void visitIdent(JCIdent that)                 { visitTree(that); }
3204         public void visitLiteral(JCLiteral that)             { visitTree(that); }
3205         public void visitTypeIdent(JCPrimitiveTypeTree that) { visitTree(that); }
3206         public void visitTypeArray(JCArrayTypeTree that)     { visitTree(that); }
3207         public void visitTypeApply(JCTypeApply that)         { visitTree(that); }
3208         public void visitTypeUnion(JCTypeUnion that)         { visitTree(that); }
3209         public void visitTypeIntersection(JCTypeIntersection that)  { visitTree(that); }
3210         public void visitTypeParameter(JCTypeParameter that) { visitTree(that); }
3211         public void visitWildcard(JCWildcard that)           { visitTree(that); }
3212         public void visitTypeBoundKind(TypeBoundKind that)   { visitTree(that); }
3213         public void visitAnnotation(JCAnnotation that)       { visitTree(that); }
3214         public void visitModifiers(JCModifiers that)         { visitTree(that); }
3215         public void visitAnnotatedType(JCAnnotatedType that) { visitTree(that); }
3216         public void visitErroneous(JCErroneous that)         { visitTree(that); }
3217         public void visitModuleDef(JCModuleDecl that)        { visitTree(that); }
3218         public void visitExports(JCExports that)             { visitTree(that); }
3219         public void visitOpens(JCOpens that)                 { visitTree(that); }
3220         public void visitProvides(JCProvides that)           { visitTree(that); }
3221         public void visitRequires(JCRequires that)           { visitTree(that); }
3222         public void visitUses(JCUses that)                   { visitTree(that); }
3223         public void visitLetExpr(LetExpr that)               { visitTree(that); }
3224 
3225         public void visitTree(JCTree that)                   { Assert.error(); }
3226     }
3227 
3228 }