1 /* 2 * Copyright 2016 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.turbine.tree; 18 19 import static java.util.Objects.requireNonNull; 20 21 import com.google.common.collect.ImmutableList; 22 import com.google.common.collect.ImmutableSet; 23 import com.google.errorprone.annotations.Immutable; 24 import com.google.turbine.diag.SourceFile; 25 import com.google.turbine.model.Const; 26 import com.google.turbine.model.TurbineConstantTypeKind; 27 import com.google.turbine.model.TurbineTyKind; 28 import java.util.ArrayDeque; 29 import java.util.Deque; 30 import java.util.Optional; 31 import java.util.Set; 32 import org.jspecify.nullness.Nullable; 33 34 /** An AST node. */ 35 public abstract class Tree { 36 kind()37 public abstract Kind kind(); 38 accept( Visitor<I, O> visitor, I input)39 public abstract <I extends @Nullable Object, O extends @Nullable Object> O accept( 40 Visitor<I, O> visitor, I input); 41 42 private final int position; 43 Tree(int position)44 protected Tree(int position) { 45 this.position = position; 46 } 47 position()48 public int position() { 49 return position; 50 } 51 52 @Override toString()53 public String toString() { 54 return Pretty.pretty(this); 55 } 56 57 /** Tree kind. */ 58 public enum Kind { 59 IDENT, 60 WILD_TY, 61 ARR_TY, 62 PRIM_TY, 63 VOID_TY, 64 CLASS_TY, 65 LITERAL, 66 PAREN, 67 TYPE_CAST, 68 UNARY, 69 BINARY, 70 CONST_VAR_NAME, 71 CLASS_LITERAL, 72 ASSIGN, 73 CONDITIONAL, 74 ARRAY_INIT, 75 COMP_UNIT, 76 IMPORT_DECL, 77 VAR_DECL, 78 METH_DECL, 79 ANNO, 80 ANNO_EXPR, 81 TY_DECL, 82 TY_PARAM, 83 PKG_DECL, 84 MOD_DECL, 85 MOD_REQUIRES, 86 MOD_EXPORTS, 87 MOD_OPENS, 88 MOD_USES, 89 MOD_PROVIDES 90 } 91 92 /** An identifier. */ 93 @Immutable 94 public static class Ident extends Tree { 95 96 private final String value; 97 Ident(int position, String value)98 public Ident(int position, String value) { 99 super(position); 100 this.value = value; 101 } 102 103 @Override kind()104 public Kind kind() { 105 return Kind.IDENT; 106 } 107 108 @Override accept( Visitor<I, O> visitor, I input)109 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 110 Visitor<I, O> visitor, I input) { 111 return visitor.visitIdent(this, input); 112 } 113 value()114 public String value() { 115 return value; 116 } 117 118 @Override toString()119 public String toString() { 120 return value; 121 } 122 } 123 124 /** A type use. */ 125 public abstract static class Type extends Tree { 126 private final ImmutableList<Anno> annos; 127 Type(int position, ImmutableList<Anno> annos)128 public Type(int position, ImmutableList<Anno> annos) { 129 super(position); 130 this.annos = annos; 131 } 132 annos()133 public ImmutableList<Anno> annos() { 134 return annos; 135 } 136 } 137 138 /** An expression. */ 139 public abstract static class Expression extends Tree { Expression(int position)140 public Expression(int position) { 141 super(position); 142 } 143 } 144 145 /** A wildcard type, possibly with an upper or lower bound. */ 146 public static class WildTy extends Type { 147 private final Optional<Type> upper; 148 private final Optional<Type> lower; 149 WildTy( int position, ImmutableList<Anno> annos, Optional<Type> upper, Optional<Type> lower)150 public WildTy( 151 int position, ImmutableList<Anno> annos, Optional<Type> upper, Optional<Type> lower) { 152 super(position, annos); 153 this.upper = upper; 154 this.lower = lower; 155 } 156 157 @Override kind()158 public Kind kind() { 159 return Kind.WILD_TY; 160 } 161 162 @Override accept( Visitor<I, O> visitor, I input)163 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 164 Visitor<I, O> visitor, I input) { 165 return visitor.visitWildTy(this, input); 166 } 167 168 /** 169 * An optional upper (extends) bound. 170 * 171 * <p>At most one of {@link #upper} and {@link #lower} will be set. 172 */ upper()173 public Optional<Type> upper() { 174 return upper; 175 } 176 177 /** 178 * An optional lower (super) bound. 179 * 180 * <p>At most one of {@link #upper} and {@link #lower} will be set. 181 */ lower()182 public Optional<Type> lower() { 183 return lower; 184 } 185 } 186 187 /** An array type. */ 188 public static class ArrTy extends Type { 189 private final Type elem; 190 ArrTy(int position, ImmutableList<Anno> annos, Type elem)191 public ArrTy(int position, ImmutableList<Anno> annos, Type elem) { 192 super(position, annos); 193 this.elem = elem; 194 } 195 196 @Override kind()197 public Kind kind() { 198 return Kind.ARR_TY; 199 } 200 201 @Override accept( Visitor<I, O> visitor, I input)202 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 203 Visitor<I, O> visitor, I input) { 204 return visitor.visitArrTy(this, input); 205 } 206 207 /** 208 * The element type of the array. 209 * 210 * <p>Multi-dimensional arrays are represented as nested {@link ArrTy}s. 211 */ elem()212 public Type elem() { 213 return elem; 214 } 215 } 216 217 /** A primitive type. */ 218 public static class PrimTy extends Type { 219 private final TurbineConstantTypeKind tykind; 220 PrimTy(int position, ImmutableList<Anno> annos, TurbineConstantTypeKind tykind)221 public PrimTy(int position, ImmutableList<Anno> annos, TurbineConstantTypeKind tykind) { 222 super(position, annos); 223 this.tykind = tykind; 224 } 225 226 @Override kind()227 public Kind kind() { 228 return Kind.PRIM_TY; 229 } 230 231 @Override accept( Visitor<I, O> visitor, I input)232 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 233 Visitor<I, O> visitor, I input) { 234 return visitor.visitPrimTy(this, input); 235 } 236 237 /** The primtiive type. */ tykind()238 public TurbineConstantTypeKind tykind() { 239 return tykind; 240 } 241 } 242 243 /** The void type, used only for void-returning methods. */ 244 public static class VoidTy extends Type { 245 246 @Override kind()247 public Kind kind() { 248 return Kind.VOID_TY; 249 } 250 251 @Override accept( Visitor<I, O> visitor, I input)252 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 253 Visitor<I, O> visitor, I input) { 254 return visitor.visitVoidTy(this, input); 255 } 256 VoidTy(int position)257 public VoidTy(int position) { 258 super(position, ImmutableList.of()); 259 } 260 } 261 262 /** A class, enum, interface, or annotation {@link Type}. */ 263 public static class ClassTy extends Type { 264 private final Optional<ClassTy> base; 265 private final Ident name; 266 private final ImmutableList<Type> tyargs; 267 ClassTy( int position, Optional<ClassTy> base, Ident name, ImmutableList<Type> tyargs, ImmutableList<Anno> annos)268 public ClassTy( 269 int position, 270 Optional<ClassTy> base, 271 Ident name, 272 ImmutableList<Type> tyargs, 273 ImmutableList<Anno> annos) { 274 super(position, annos); 275 this.base = base; 276 this.name = name; 277 this.tyargs = tyargs; 278 } 279 280 @Override kind()281 public Kind kind() { 282 return Kind.CLASS_TY; 283 } 284 285 @Override accept( Visitor<I, O> visitor, I input)286 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 287 Visitor<I, O> visitor, I input) { 288 return visitor.visitClassTy(this, input); 289 } 290 291 /** 292 * The base type, for qualified type uses. 293 * 294 * <p>For example, {@code Map.Entry}. 295 */ base()296 public Optional<ClassTy> base() { 297 return base; 298 } 299 300 /** The simple name of the type. */ name()301 public Ident name() { 302 return name; 303 } 304 305 /** A possibly empty list of type arguments. */ tyargs()306 public ImmutableList<Type> tyargs() { 307 return tyargs; 308 } 309 } 310 311 /** A JLS 3.10 literal expression. */ 312 public static class Literal extends Expression { 313 private final TurbineConstantTypeKind tykind; 314 private final Const value; 315 Literal(int position, TurbineConstantTypeKind tykind, Const value)316 public Literal(int position, TurbineConstantTypeKind tykind, Const value) { 317 super(position); 318 this.tykind = tykind; 319 this.value = value; 320 } 321 322 @Override kind()323 public Kind kind() { 324 return Kind.LITERAL; 325 } 326 327 @Override accept( Visitor<I, O> visitor, I input)328 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 329 Visitor<I, O> visitor, I input) { 330 return visitor.visitLiteral(this, input); 331 } 332 tykind()333 public TurbineConstantTypeKind tykind() { 334 return tykind; 335 } 336 value()337 public Const value() { 338 return value; 339 } 340 } 341 342 /** A JLS 15.8.5 parenthesized expression. */ 343 public static class Paren extends Expression { 344 private final Expression expr; 345 Paren(int position, Expression expr)346 public Paren(int position, Expression expr) { 347 super(position); 348 this.expr = expr; 349 } 350 351 @Override kind()352 public Kind kind() { 353 return Kind.PAREN; 354 } 355 356 @Override accept( Visitor<I, O> visitor, I input)357 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 358 Visitor<I, O> visitor, I input) { 359 return visitor.visitParen(this, input); 360 } 361 expr()362 public Expression expr() { 363 return expr; 364 } 365 } 366 367 /** A JLS 15.16 cast expression. */ 368 public static class TypeCast extends Expression { 369 private final Type ty; 370 private final Expression expr; 371 TypeCast(int position, Type ty, Expression expr)372 public TypeCast(int position, Type ty, Expression expr) { 373 super(position); 374 this.ty = ty; 375 this.expr = expr; 376 } 377 378 @Override kind()379 public Kind kind() { 380 return Kind.TYPE_CAST; 381 } 382 383 @Override accept( Visitor<I, O> visitor, I input)384 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 385 Visitor<I, O> visitor, I input) { 386 return visitor.visitTypeCast(this, input); 387 } 388 ty()389 public Type ty() { 390 return ty; 391 } 392 expr()393 public Expression expr() { 394 return expr; 395 } 396 } 397 398 /** A JLS 15.14 - 14.15 unary expression. */ 399 public static class Unary extends Expression { 400 private final Expression expr; 401 private final TurbineOperatorKind op; 402 Unary(int position, Expression expr, TurbineOperatorKind op)403 public Unary(int position, Expression expr, TurbineOperatorKind op) { 404 super(position); 405 this.expr = expr; 406 this.op = op; 407 } 408 409 @Override kind()410 public Kind kind() { 411 return Kind.UNARY; 412 } 413 414 @Override accept( Visitor<I, O> visitor, I input)415 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 416 Visitor<I, O> visitor, I input) { 417 return visitor.visitUnary(this, input); 418 } 419 expr()420 public Expression expr() { 421 return expr; 422 } 423 op()424 public TurbineOperatorKind op() { 425 return op; 426 } 427 } 428 429 /** A JLS 15.17 - 14.24 binary expression. */ 430 public static class Binary extends Expression { 431 private final Expression lhs; 432 private final Expression rhs; 433 private final TurbineOperatorKind op; 434 Binary(int position, Expression lhs, Expression rhs, TurbineOperatorKind op)435 public Binary(int position, Expression lhs, Expression rhs, TurbineOperatorKind op) { 436 super(position); 437 this.lhs = lhs; 438 this.rhs = rhs; 439 this.op = op; 440 } 441 442 @Override kind()443 public Kind kind() { 444 return Kind.BINARY; 445 } 446 447 @Override accept( Visitor<I, O> visitor, I input)448 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 449 Visitor<I, O> visitor, I input) { 450 return visitor.visitBinary(this, input); 451 } 452 children()453 public Iterable<Expression> children() { 454 ImmutableList.Builder<Expression> children = ImmutableList.builder(); 455 Deque<Expression> stack = new ArrayDeque<>(); 456 stack.addFirst(rhs); 457 stack.addFirst(lhs); 458 while (!stack.isEmpty()) { 459 Expression curr = stack.removeFirst(); 460 if (curr.kind().equals(Kind.BINARY)) { 461 Binary b = ((Binary) curr); 462 if (b.op().equals(op())) { 463 stack.addFirst(b.rhs); 464 stack.addFirst(b.lhs); 465 continue; 466 } 467 } 468 children.add(curr); 469 } 470 return children.build(); 471 } 472 op()473 public TurbineOperatorKind op() { 474 return op; 475 } 476 } 477 478 /** A JLS 6.5.6.1 simple name that refers to a JSL 4.12.4 constant variable. */ 479 public static class ConstVarName extends Expression { 480 private final ImmutableList<Ident> name; 481 ConstVarName(int position, ImmutableList<Ident> name)482 public ConstVarName(int position, ImmutableList<Ident> name) { 483 super(position); 484 this.name = name; 485 } 486 487 @Override kind()488 public Kind kind() { 489 return Kind.CONST_VAR_NAME; 490 } 491 492 @Override accept( Visitor<I, O> visitor, I input)493 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 494 Visitor<I, O> visitor, I input) { 495 return visitor.visitConstVarName(this, input); 496 } 497 name()498 public ImmutableList<Ident> name() { 499 return name; 500 } 501 } 502 503 /** A JLS 15.8.2 class literal. */ 504 public static class ClassLiteral extends Expression { 505 506 private final Type type; 507 ClassLiteral(int position, Type type)508 public ClassLiteral(int position, Type type) { 509 super(position); 510 this.type = type; 511 } 512 513 @Override kind()514 public Kind kind() { 515 return Kind.CLASS_LITERAL; 516 } 517 518 @Override accept( Visitor<I, O> visitor, I input)519 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 520 Visitor<I, O> visitor, I input) { 521 return visitor.visitClassLiteral(this, input); 522 } 523 type()524 public Type type() { 525 return type; 526 } 527 } 528 529 /** A JLS 15.26 assignment expression. */ 530 public static class Assign extends Expression { 531 private final Ident name; 532 private final Expression expr; 533 Assign(int position, Ident name, Expression expr)534 public Assign(int position, Ident name, Expression expr) { 535 super(position); 536 this.name = requireNonNull(name); 537 this.expr = requireNonNull(expr); 538 } 539 540 @Override kind()541 public Kind kind() { 542 return Kind.ASSIGN; 543 } 544 545 @Override accept( Visitor<I, O> visitor, I input)546 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 547 Visitor<I, O> visitor, I input) { 548 return visitor.visitAssign(this, input); 549 } 550 name()551 public Ident name() { 552 return name; 553 } 554 expr()555 public Expression expr() { 556 return expr; 557 } 558 } 559 560 /** A JLS 15.25 conditional expression. */ 561 public static class Conditional extends Expression { 562 private final Expression cond; 563 private final Expression iftrue; 564 private final Expression iffalse; 565 Conditional(int position, Expression cond, Expression iftrue, Expression iffalse)566 public Conditional(int position, Expression cond, Expression iftrue, Expression iffalse) { 567 super(position); 568 this.cond = cond; 569 this.iftrue = iftrue; 570 this.iffalse = iffalse; 571 } 572 573 @Override kind()574 public Kind kind() { 575 return Kind.CONDITIONAL; 576 } 577 578 @Override accept( Visitor<I, O> visitor, I input)579 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 580 Visitor<I, O> visitor, I input) { 581 return visitor.visitConditional(this, input); 582 } 583 cond()584 public Expression cond() { 585 return cond; 586 } 587 iftrue()588 public Expression iftrue() { 589 return iftrue; 590 } 591 iffalse()592 public Expression iffalse() { 593 return iffalse; 594 } 595 } 596 597 /** JLS 10.6 array initializer. */ 598 public static class ArrayInit extends Expression { 599 private final ImmutableList<Expression> exprs; 600 ArrayInit(int position, ImmutableList<Expression> exprs)601 public ArrayInit(int position, ImmutableList<Expression> exprs) { 602 super(position); 603 this.exprs = exprs; 604 } 605 606 @Override kind()607 public Kind kind() { 608 return Kind.ARRAY_INIT; 609 } 610 611 @Override accept( Visitor<I, O> visitor, I input)612 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 613 Visitor<I, O> visitor, I input) { 614 return visitor.visitArrayInit(this, input); 615 } 616 exprs()617 public ImmutableList<Expression> exprs() { 618 return exprs; 619 } 620 } 621 622 /** A JLS 7.3 compilation unit. */ 623 public static class CompUnit extends Tree { 624 private final Optional<PkgDecl> pkg; 625 private final Optional<ModDecl> mod; 626 private final ImmutableList<ImportDecl> imports; 627 private final ImmutableList<TyDecl> decls; 628 private final SourceFile source; 629 CompUnit( int position, Optional<PkgDecl> pkg, Optional<ModDecl> mod, ImmutableList<ImportDecl> imports, ImmutableList<TyDecl> decls, SourceFile source)630 public CompUnit( 631 int position, 632 Optional<PkgDecl> pkg, 633 Optional<ModDecl> mod, 634 ImmutableList<ImportDecl> imports, 635 ImmutableList<TyDecl> decls, 636 SourceFile source) { 637 super(position); 638 this.pkg = pkg; 639 this.mod = mod; 640 this.imports = imports; 641 this.decls = decls; 642 this.source = source; 643 } 644 645 @Override kind()646 public Kind kind() { 647 return Kind.COMP_UNIT; 648 } 649 650 @Override accept( Visitor<I, O> visitor, I input)651 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 652 Visitor<I, O> visitor, I input) { 653 return visitor.visitCompUnit(this, input); 654 } 655 pkg()656 public Optional<PkgDecl> pkg() { 657 return pkg; 658 } 659 mod()660 public Optional<ModDecl> mod() { 661 return mod; 662 } 663 imports()664 public ImmutableList<ImportDecl> imports() { 665 return imports; 666 } 667 decls()668 public ImmutableList<TyDecl> decls() { 669 return decls; 670 } 671 source()672 public SourceFile source() { 673 return source; 674 } 675 } 676 677 /** A JLS 7.5 import declaration. */ 678 public static class ImportDecl extends Tree { 679 private final ImmutableList<Ident> type; 680 private final boolean stat; 681 private final boolean wild; 682 ImportDecl(int position, ImmutableList<Ident> type, boolean stat, boolean wild)683 public ImportDecl(int position, ImmutableList<Ident> type, boolean stat, boolean wild) { 684 super(position); 685 this.type = type; 686 this.stat = stat; 687 this.wild = wild; 688 } 689 690 @Override kind()691 public Kind kind() { 692 return Kind.IMPORT_DECL; 693 } 694 695 @Override accept( Visitor<I, O> visitor, I input)696 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 697 Visitor<I, O> visitor, I input) { 698 return visitor.visitImportDecl(this, input); 699 } 700 type()701 public ImmutableList<Ident> type() { 702 return type; 703 } 704 705 /** Returns true for static member imports. */ stat()706 public boolean stat() { 707 return stat; 708 } 709 710 /** Returns true for wildcard imports. */ wild()711 public boolean wild() { 712 return wild; 713 } 714 } 715 716 /** A JLS 8.3 field declaration, JLS 8.4.1 formal method parameter, or JLS 14.4 variable. */ 717 public static class VarDecl extends Tree { 718 private final ImmutableSet<TurbineModifier> mods; 719 private final ImmutableList<Anno> annos; 720 private final Tree ty; 721 private final Ident name; 722 private final Optional<Expression> init; 723 private final @Nullable String javadoc; 724 VarDecl( int position, Set<TurbineModifier> mods, ImmutableList<Anno> annos, Tree ty, Ident name, Optional<Expression> init, @Nullable String javadoc)725 public VarDecl( 726 int position, 727 Set<TurbineModifier> mods, 728 ImmutableList<Anno> annos, 729 Tree ty, 730 Ident name, 731 Optional<Expression> init, 732 @Nullable String javadoc) { 733 super(position); 734 this.mods = ImmutableSet.copyOf(mods); 735 this.annos = annos; 736 this.ty = ty; 737 this.name = name; 738 this.init = init; 739 this.javadoc = javadoc; 740 } 741 742 @Override kind()743 public Kind kind() { 744 return Kind.VAR_DECL; 745 } 746 747 @Override accept( Visitor<I, O> visitor, I input)748 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 749 Visitor<I, O> visitor, I input) { 750 return visitor.visitVarDecl(this, input); 751 } 752 mods()753 public ImmutableSet<TurbineModifier> mods() { 754 return mods; 755 } 756 annos()757 public ImmutableList<Anno> annos() { 758 return annos; 759 } 760 ty()761 public Tree ty() { 762 return ty; 763 } 764 name()765 public Ident name() { 766 return name; 767 } 768 init()769 public Optional<Expression> init() { 770 return init; 771 } 772 773 /** 774 * A javadoc comment, excluding the opening and closing delimiters but including all interior 775 * characters and whitespace. 776 */ javadoc()777 public @Nullable String javadoc() { 778 return javadoc; 779 } 780 } 781 782 /** A JLS 8.4 method declaration. */ 783 public static class MethDecl extends Tree { 784 private final ImmutableSet<TurbineModifier> mods; 785 private final ImmutableList<Anno> annos; 786 private final ImmutableList<TyParam> typarams; 787 private final Optional<Tree> ret; 788 private final Ident name; 789 private final ImmutableList<VarDecl> params; 790 private final ImmutableList<ClassTy> exntys; 791 private final Optional<Tree> defaultValue; 792 private final String javadoc; 793 MethDecl( int position, Set<TurbineModifier> mods, ImmutableList<Anno> annos, ImmutableList<TyParam> typarams, Optional<Tree> ret, Ident name, ImmutableList<VarDecl> params, ImmutableList<ClassTy> exntys, Optional<Tree> defaultValue, String javadoc)794 public MethDecl( 795 int position, 796 Set<TurbineModifier> mods, 797 ImmutableList<Anno> annos, 798 ImmutableList<TyParam> typarams, 799 Optional<Tree> ret, 800 Ident name, 801 ImmutableList<VarDecl> params, 802 ImmutableList<ClassTy> exntys, 803 Optional<Tree> defaultValue, 804 String javadoc) { 805 super(position); 806 this.mods = ImmutableSet.copyOf(mods); 807 this.annos = annos; 808 this.typarams = typarams; 809 this.ret = ret; 810 this.name = name; 811 this.params = params; 812 this.exntys = exntys; 813 this.defaultValue = defaultValue; 814 this.javadoc = javadoc; 815 } 816 817 @Override kind()818 public Kind kind() { 819 return Kind.METH_DECL; 820 } 821 822 @Override accept( Visitor<I, O> visitor, I input)823 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 824 Visitor<I, O> visitor, I input) { 825 return visitor.visitMethDecl(this, input); 826 } 827 mods()828 public ImmutableSet<TurbineModifier> mods() { 829 return mods; 830 } 831 annos()832 public ImmutableList<Anno> annos() { 833 return annos; 834 } 835 typarams()836 public ImmutableList<TyParam> typarams() { 837 return typarams; 838 } 839 ret()840 public Optional<Tree> ret() { 841 return ret; 842 } 843 name()844 public Ident name() { 845 return name; 846 } 847 params()848 public ImmutableList<VarDecl> params() { 849 return params; 850 } 851 exntys()852 public ImmutableList<ClassTy> exntys() { 853 return exntys; 854 } 855 defaultValue()856 public Optional<Tree> defaultValue() { 857 return defaultValue; 858 } 859 /** 860 * A javadoc comment, excluding the opening and closing delimiters but including all interior 861 * characters and whitespace. 862 */ javadoc()863 public String javadoc() { 864 return javadoc; 865 } 866 } 867 868 /** A JLS 9.7 annotation. */ 869 public static class Anno extends Tree { 870 private final ImmutableList<Ident> name; 871 private final ImmutableList<Expression> args; 872 Anno(int position, ImmutableList<Ident> name, ImmutableList<Expression> args)873 public Anno(int position, ImmutableList<Ident> name, ImmutableList<Expression> args) { 874 super(position); 875 this.name = name; 876 this.args = args; 877 } 878 879 @Override kind()880 public Kind kind() { 881 return Kind.ANNO; 882 } 883 884 @Override accept( Visitor<I, O> visitor, I input)885 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 886 Visitor<I, O> visitor, I input) { 887 return visitor.visitAnno(this, input); 888 } 889 name()890 public ImmutableList<Ident> name() { 891 return name; 892 } 893 args()894 public ImmutableList<Expression> args() { 895 return args; 896 } 897 } 898 899 /** 900 * An annotation in an expression context, e.g. an annotation literal nested inside another 901 * annotation. 902 */ 903 public static class AnnoExpr extends Expression { 904 905 private final Anno value; 906 AnnoExpr(int position, Anno value)907 public AnnoExpr(int position, Anno value) { 908 super(position); 909 this.value = value; 910 } 911 912 /** The annotation. */ value()913 public Anno value() { 914 return value; 915 } 916 917 @Override kind()918 public Kind kind() { 919 return Kind.ANNO_EXPR; 920 } 921 922 @Override accept( Visitor<I, O> visitor, I input)923 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 924 Visitor<I, O> visitor, I input) { 925 return visitor.visitAnno(value, input); 926 } 927 } 928 929 /** A JLS 7.6 or 8.5 type declaration. */ 930 public static class TyDecl extends Tree { 931 private final ImmutableSet<TurbineModifier> mods; 932 private final ImmutableList<Anno> annos; 933 private final Ident name; 934 private final ImmutableList<TyParam> typarams; 935 private final Optional<ClassTy> xtnds; 936 private final ImmutableList<ClassTy> impls; 937 private final ImmutableList<ClassTy> permits; 938 private final ImmutableList<Tree> members; 939 private final ImmutableList<VarDecl> components; 940 private final TurbineTyKind tykind; 941 private final @Nullable String javadoc; 942 TyDecl( int position, Set<TurbineModifier> mods, ImmutableList<Anno> annos, Ident name, ImmutableList<TyParam> typarams, Optional<ClassTy> xtnds, ImmutableList<ClassTy> impls, ImmutableList<ClassTy> permits, ImmutableList<Tree> members, ImmutableList<VarDecl> components, TurbineTyKind tykind, @Nullable String javadoc)943 public TyDecl( 944 int position, 945 Set<TurbineModifier> mods, 946 ImmutableList<Anno> annos, 947 Ident name, 948 ImmutableList<TyParam> typarams, 949 Optional<ClassTy> xtnds, 950 ImmutableList<ClassTy> impls, 951 ImmutableList<ClassTy> permits, 952 ImmutableList<Tree> members, 953 ImmutableList<VarDecl> components, 954 TurbineTyKind tykind, 955 @Nullable String javadoc) { 956 super(position); 957 this.mods = ImmutableSet.copyOf(mods); 958 this.annos = annos; 959 this.name = name; 960 this.typarams = typarams; 961 this.xtnds = xtnds; 962 this.impls = impls; 963 this.permits = permits; 964 this.members = members; 965 this.components = components; 966 this.tykind = tykind; 967 this.javadoc = javadoc; 968 } 969 970 @Override kind()971 public Kind kind() { 972 return Kind.TY_DECL; 973 } 974 975 @Override accept( Visitor<I, O> visitor, I input)976 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 977 Visitor<I, O> visitor, I input) { 978 return visitor.visitTyDecl(this, input); 979 } 980 mods()981 public ImmutableSet<TurbineModifier> mods() { 982 return mods; 983 } 984 annos()985 public ImmutableList<Anno> annos() { 986 return annos; 987 } 988 name()989 public Ident name() { 990 return name; 991 } 992 typarams()993 public ImmutableList<TyParam> typarams() { 994 return typarams; 995 } 996 xtnds()997 public Optional<ClassTy> xtnds() { 998 return xtnds; 999 } 1000 impls()1001 public ImmutableList<ClassTy> impls() { 1002 return impls; 1003 } 1004 permits()1005 public ImmutableList<ClassTy> permits() { 1006 return permits; 1007 } 1008 members()1009 public ImmutableList<Tree> members() { 1010 return members; 1011 } 1012 components()1013 public ImmutableList<VarDecl> components() { 1014 return components; 1015 } 1016 tykind()1017 public TurbineTyKind tykind() { 1018 return tykind; 1019 } 1020 /** 1021 * A javadoc comment, excluding the opening and closing delimiters but including all interior 1022 * characters and whitespace. 1023 */ javadoc()1024 public @Nullable String javadoc() { 1025 return javadoc; 1026 } 1027 } 1028 1029 /** A JLS 4.4. type variable declaration. */ 1030 public static class TyParam extends Tree { 1031 private final Ident name; 1032 private final ImmutableList<Tree> bounds; 1033 private final ImmutableList<Anno> annos; 1034 TyParam( int position, Ident name, ImmutableList<Tree> bounds, ImmutableList<Anno> annos)1035 public TyParam( 1036 int position, Ident name, ImmutableList<Tree> bounds, ImmutableList<Anno> annos) { 1037 super(position); 1038 this.name = name; 1039 this.bounds = bounds; 1040 this.annos = annos; 1041 } 1042 1043 @Override kind()1044 public Kind kind() { 1045 return Kind.TY_PARAM; 1046 } 1047 1048 @Override accept( Visitor<I, O> visitor, I input)1049 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 1050 Visitor<I, O> visitor, I input) { 1051 return visitor.visitTyParam(this, input); 1052 } 1053 name()1054 public Ident name() { 1055 return name; 1056 } 1057 bounds()1058 public ImmutableList<Tree> bounds() { 1059 return bounds; 1060 } 1061 annos()1062 public ImmutableList<Anno> annos() { 1063 return annos; 1064 } 1065 } 1066 1067 /** A JLS 7.4 package declaration. */ 1068 public static class PkgDecl extends Tree { 1069 private final ImmutableList<Ident> name; 1070 private final ImmutableList<Anno> annos; 1071 PkgDecl(int position, ImmutableList<Ident> name, ImmutableList<Anno> annos)1072 public PkgDecl(int position, ImmutableList<Ident> name, ImmutableList<Anno> annos) { 1073 super(position); 1074 this.name = name; 1075 this.annos = annos; 1076 } 1077 1078 @Override kind()1079 public Kind kind() { 1080 return Kind.PKG_DECL; 1081 } 1082 1083 @Override accept( Visitor<I, O> visitor, I input)1084 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 1085 Visitor<I, O> visitor, I input) { 1086 return visitor.visitPkgDecl(this, input); 1087 } 1088 name()1089 public ImmutableList<Ident> name() { 1090 return name; 1091 } 1092 annos()1093 public ImmutableList<Anno> annos() { 1094 return annos; 1095 } 1096 } 1097 1098 /** A JLS 7.7 module declaration. */ 1099 public static class ModDecl extends Tree { 1100 1101 private final ImmutableList<Anno> annos; 1102 private final boolean open; 1103 private final String moduleName; 1104 private final ImmutableList<ModDirective> directives; 1105 ModDecl( int position, ImmutableList<Anno> annos, boolean open, String moduleName, ImmutableList<ModDirective> directives)1106 public ModDecl( 1107 int position, 1108 ImmutableList<Anno> annos, 1109 boolean open, 1110 String moduleName, 1111 ImmutableList<ModDirective> directives) { 1112 super(position); 1113 this.annos = annos; 1114 this.open = open; 1115 this.moduleName = moduleName; 1116 this.directives = directives; 1117 } 1118 open()1119 public boolean open() { 1120 return open; 1121 } 1122 annos()1123 public ImmutableList<Anno> annos() { 1124 return annos; 1125 } 1126 moduleName()1127 public String moduleName() { 1128 return moduleName; 1129 } 1130 directives()1131 public ImmutableList<ModDirective> directives() { 1132 return directives; 1133 } 1134 1135 @Override kind()1136 public Kind kind() { 1137 return Kind.MOD_DECL; 1138 } 1139 1140 @Override accept( Visitor<I, O> visitor, I input)1141 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 1142 Visitor<I, O> visitor, I input) { 1143 return visitor.visitModDecl(this, input); 1144 } 1145 } 1146 1147 /** A kind of module directive. */ 1148 public abstract static class ModDirective extends Tree { 1149 1150 /** A module directive kind. */ 1151 public enum DirectiveKind { 1152 REQUIRES, 1153 EXPORTS, 1154 OPENS, 1155 USES, 1156 PROVIDES 1157 } 1158 directiveKind()1159 public abstract DirectiveKind directiveKind(); 1160 ModDirective(int position)1161 protected ModDirective(int position) { 1162 super(position); 1163 } 1164 } 1165 1166 /** A JLS 7.7.1 module requires directive. */ 1167 public static class ModRequires extends ModDirective { 1168 1169 private final ImmutableSet<TurbineModifier> mods; 1170 private final String moduleName; 1171 1172 @Override kind()1173 public Kind kind() { 1174 return Kind.MOD_REQUIRES; 1175 } 1176 1177 @Override accept( Visitor<I, O> visitor, I input)1178 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 1179 Visitor<I, O> visitor, I input) { 1180 return visitor.visitModRequires(this, input); 1181 } 1182 ModRequires(int position, ImmutableSet<TurbineModifier> mods, String moduleName)1183 public ModRequires(int position, ImmutableSet<TurbineModifier> mods, String moduleName) { 1184 super(position); 1185 this.mods = mods; 1186 this.moduleName = moduleName; 1187 } 1188 mods()1189 public ImmutableSet<TurbineModifier> mods() { 1190 return mods; 1191 } 1192 moduleName()1193 public String moduleName() { 1194 return moduleName; 1195 } 1196 1197 @Override directiveKind()1198 public DirectiveKind directiveKind() { 1199 return DirectiveKind.REQUIRES; 1200 } 1201 } 1202 1203 /** A JLS 7.7.2 module exports directive. */ 1204 public static class ModExports extends ModDirective { 1205 1206 private final String packageName; 1207 private final ImmutableList<String> moduleNames; 1208 1209 @Override kind()1210 public Kind kind() { 1211 return Kind.MOD_EXPORTS; 1212 } 1213 1214 @Override accept( Visitor<I, O> visitor, I input)1215 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 1216 Visitor<I, O> visitor, I input) { 1217 return visitor.visitModExports(this, input); 1218 } 1219 ModExports(int position, String packageName, ImmutableList<String> moduleNames)1220 public ModExports(int position, String packageName, ImmutableList<String> moduleNames) { 1221 super(position); 1222 this.packageName = packageName; 1223 this.moduleNames = moduleNames; 1224 } 1225 packageName()1226 public String packageName() { 1227 return packageName; 1228 } 1229 moduleNames()1230 public ImmutableList<String> moduleNames() { 1231 return moduleNames; 1232 } 1233 1234 @Override directiveKind()1235 public DirectiveKind directiveKind() { 1236 return DirectiveKind.EXPORTS; 1237 } 1238 } 1239 1240 /** A JLS 7.7.2 module opens directive. */ 1241 public static class ModOpens extends ModDirective { 1242 1243 private final String packageName; 1244 private final ImmutableList<String> moduleNames; 1245 ModOpens(int position, String packageName, ImmutableList<String> moduleNames)1246 public ModOpens(int position, String packageName, ImmutableList<String> moduleNames) { 1247 super(position); 1248 this.packageName = packageName; 1249 this.moduleNames = moduleNames; 1250 } 1251 packageName()1252 public String packageName() { 1253 return packageName; 1254 } 1255 moduleNames()1256 public ImmutableList<String> moduleNames() { 1257 return moduleNames; 1258 } 1259 1260 @Override kind()1261 public Kind kind() { 1262 return Kind.MOD_OPENS; 1263 } 1264 1265 @Override accept( Visitor<I, O> visitor, I input)1266 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 1267 Visitor<I, O> visitor, I input) { 1268 return visitor.visitModOpens(this, input); 1269 } 1270 1271 @Override directiveKind()1272 public DirectiveKind directiveKind() { 1273 return DirectiveKind.OPENS; 1274 } 1275 } 1276 1277 /** A JLS 7.7.3 module uses directive. */ 1278 public static class ModUses extends ModDirective { 1279 1280 private final ImmutableList<Ident> typeName; 1281 ModUses(int position, ImmutableList<Ident> typeName)1282 public ModUses(int position, ImmutableList<Ident> typeName) { 1283 super(position); 1284 this.typeName = typeName; 1285 } 1286 typeName()1287 public ImmutableList<Ident> typeName() { 1288 return typeName; 1289 } 1290 1291 @Override kind()1292 public Kind kind() { 1293 return Kind.MOD_USES; 1294 } 1295 1296 @Override accept( Visitor<I, O> visitor, I input)1297 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 1298 Visitor<I, O> visitor, I input) { 1299 return visitor.visitModUses(this, input); 1300 } 1301 1302 @Override directiveKind()1303 public DirectiveKind directiveKind() { 1304 return DirectiveKind.USES; 1305 } 1306 } 1307 1308 /** A JLS 7.7.4 module uses directive. */ 1309 public static class ModProvides extends ModDirective { 1310 1311 private final ImmutableList<Ident> typeName; 1312 private final ImmutableList<ImmutableList<Ident>> implNames; 1313 ModProvides( int position, ImmutableList<Ident> typeName, ImmutableList<ImmutableList<Ident>> implNames)1314 public ModProvides( 1315 int position, 1316 ImmutableList<Ident> typeName, 1317 ImmutableList<ImmutableList<Ident>> implNames) { 1318 super(position); 1319 this.typeName = typeName; 1320 this.implNames = implNames; 1321 } 1322 typeName()1323 public ImmutableList<Ident> typeName() { 1324 return typeName; 1325 } 1326 implNames()1327 public ImmutableList<ImmutableList<Ident>> implNames() { 1328 return implNames; 1329 } 1330 1331 @Override kind()1332 public Kind kind() { 1333 return Kind.MOD_PROVIDES; 1334 } 1335 1336 @Override accept( Visitor<I, O> visitor, I input)1337 public <I extends @Nullable Object, O extends @Nullable Object> O accept( 1338 Visitor<I, O> visitor, I input) { 1339 return visitor.visitModProvides(this, input); 1340 } 1341 1342 @Override directiveKind()1343 public DirectiveKind directiveKind() { 1344 return DirectiveKind.PROVIDES; 1345 } 1346 } 1347 1348 /** A visitor for {@link Tree}s. */ 1349 public interface Visitor<I extends @Nullable Object, O extends @Nullable Object> { visitIdent(Ident ident, I input)1350 O visitIdent(Ident ident, I input); 1351 visitWildTy(WildTy visitor, I input)1352 O visitWildTy(WildTy visitor, I input); 1353 visitArrTy(ArrTy arrTy, I input)1354 O visitArrTy(ArrTy arrTy, I input); 1355 visitPrimTy(PrimTy primTy, I input)1356 O visitPrimTy(PrimTy primTy, I input); 1357 visitVoidTy(VoidTy primTy, I input)1358 O visitVoidTy(VoidTy primTy, I input); 1359 visitClassTy(ClassTy visitor, I input)1360 O visitClassTy(ClassTy visitor, I input); 1361 visitLiteral(Literal literal, I input)1362 O visitLiteral(Literal literal, I input); 1363 visitParen(Paren unary, I input)1364 O visitParen(Paren unary, I input); 1365 visitTypeCast(TypeCast typeCast, I input)1366 O visitTypeCast(TypeCast typeCast, I input); 1367 visitUnary(Unary unary, I input)1368 O visitUnary(Unary unary, I input); 1369 visitBinary(Binary binary, I input)1370 O visitBinary(Binary binary, I input); 1371 visitConstVarName(ConstVarName constVarName, I input)1372 O visitConstVarName(ConstVarName constVarName, I input); 1373 visitClassLiteral(ClassLiteral classLiteral, I input)1374 O visitClassLiteral(ClassLiteral classLiteral, I input); 1375 visitAssign(Assign assign, I input)1376 O visitAssign(Assign assign, I input); 1377 visitConditional(Conditional conditional, I input)1378 O visitConditional(Conditional conditional, I input); 1379 visitArrayInit(ArrayInit arrayInit, I input)1380 O visitArrayInit(ArrayInit arrayInit, I input); 1381 visitCompUnit(CompUnit compUnit, I input)1382 O visitCompUnit(CompUnit compUnit, I input); 1383 visitImportDecl(ImportDecl importDecl, I input)1384 O visitImportDecl(ImportDecl importDecl, I input); 1385 visitVarDecl(VarDecl varDecl, I input)1386 O visitVarDecl(VarDecl varDecl, I input); 1387 visitMethDecl(MethDecl methDecl, I input)1388 O visitMethDecl(MethDecl methDecl, I input); 1389 visitAnno(Anno anno, I input)1390 O visitAnno(Anno anno, I input); 1391 visitTyDecl(TyDecl tyDecl, I input)1392 O visitTyDecl(TyDecl tyDecl, I input); 1393 visitTyParam(TyParam tyParam, I input)1394 O visitTyParam(TyParam tyParam, I input); 1395 visitPkgDecl(PkgDecl pkgDecl, I input)1396 O visitPkgDecl(PkgDecl pkgDecl, I input); 1397 visitModDecl(ModDecl modDecl, I input)1398 O visitModDecl(ModDecl modDecl, I input); 1399 visitModRequires(ModRequires modRequires, I input)1400 O visitModRequires(ModRequires modRequires, I input); 1401 visitModExports(ModExports modExports, I input)1402 O visitModExports(ModExports modExports, I input); 1403 visitModOpens(ModOpens modOpens, I input)1404 O visitModOpens(ModOpens modOpens, I input); 1405 visitModUses(ModUses modUses, I input)1406 O visitModUses(ModUses modUses, I input); 1407 visitModProvides(ModProvides modProvides, I input)1408 O visitModProvides(ModProvides modProvides, I input); 1409 } 1410 } 1411