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