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