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 com.google.common.base.Optional; 20 import com.google.common.collect.ImmutableList; 21 import com.google.common.collect.ImmutableSet; 22 import com.google.turbine.diag.SourceFile; 23 import com.google.turbine.model.Const; 24 import com.google.turbine.model.TurbineConstantTypeKind; 25 import com.google.turbine.model.TurbineTyKind; 26 import java.util.Set; 27 28 /** An AST node. */ 29 public abstract class Tree { 30 31 public abstract Kind kind(); 32 33 public abstract <I, O> O accept(Visitor<I, O> visitor, I input); 34 35 private final int position; 36 37 protected Tree(int position) { 38 this.position = position; 39 } 40 41 public int position() { 42 return position; 43 } 44 45 @Override 46 public String toString() { 47 return Pretty.pretty(this); 48 } 49 50 /** Tree kind. */ 51 public enum Kind { 52 WILD_TY, 53 ARR_TY, 54 PRIM_TY, 55 VOID_TY, 56 CLASS_TY, 57 LITERAL, 58 TYPE_CAST, 59 UNARY, 60 BINARY, 61 CONST_VAR_NAME, 62 CLASS_LITERAL, 63 ASSIGN, 64 CONDITIONAL, 65 ARRAY_INIT, 66 COMP_UNIT, 67 IMPORT_DECL, 68 VAR_DECL, 69 METH_DECL, 70 ANNO, 71 ANNO_EXPR, 72 TY_DECL, 73 TY_PARAM, 74 PKG_DECL 75 } 76 77 /** A type use. */ 78 public abstract static class Type extends Tree { 79 private final ImmutableList<Anno> annos; 80 81 public Type(int position, ImmutableList<Anno> annos) { 82 super(position); 83 this.annos = annos; 84 } 85 86 public ImmutableList<Anno> annos() { 87 return annos; 88 } 89 } 90 91 /** An expression. */ 92 public abstract static class Expression extends Tree { 93 public Expression(int position) { 94 super(position); 95 } 96 } 97 98 /** A wildcard type, possibly with an upper or lower bound. */ 99 public static class WildTy extends Type { 100 private final Optional<Type> upper; 101 private final Optional<Type> lower; 102 103 public WildTy( 104 int position, ImmutableList<Anno> annos, Optional<Type> upper, Optional<Type> lower) { 105 super(position, annos); 106 this.upper = upper; 107 this.lower = lower; 108 } 109 110 @Override 111 public Kind kind() { 112 return Kind.WILD_TY; 113 } 114 115 @Override 116 public <I, O> O accept(Visitor<I, O> visitor, I input) { 117 return visitor.visitWildTy(this, input); 118 } 119 120 /** 121 * An optional upper (extends) bound. 122 * 123 * <p>At most one of {@link #upper} and {@link #lower} will be set. 124 */ 125 public Optional<Type> upper() { 126 return upper; 127 } 128 129 /** 130 * An optional lower (super) bound. 131 * 132 * <p>At most one of {@link #upper} and {@link #lower} will be set. 133 */ 134 public Optional<Type> lower() { 135 return lower; 136 } 137 } 138 139 /** An array type. */ 140 public static class ArrTy extends Type { 141 private final Type elem; 142 143 public ArrTy(int position, ImmutableList<Anno> annos, Type elem) { 144 super(position, annos); 145 this.elem = elem; 146 } 147 148 @Override 149 public Kind kind() { 150 return Kind.ARR_TY; 151 } 152 153 @Override 154 public <I, O> O accept(Visitor<I, O> visitor, I input) { 155 return visitor.visitArrTy(this, input); 156 } 157 158 /** 159 * The element type of the array. 160 * 161 * <p>Multi-dimensional arrays are represented as nested {@link ArrTy}s. 162 */ 163 public Type elem() { 164 return elem; 165 } 166 } 167 168 /** A primitive type. */ 169 public static class PrimTy extends Type { 170 private final TurbineConstantTypeKind tykind; 171 172 public PrimTy(int position, ImmutableList<Anno> annos, TurbineConstantTypeKind tykind) { 173 super(position, annos); 174 this.tykind = tykind; 175 } 176 177 @Override 178 public Kind kind() { 179 return Kind.PRIM_TY; 180 } 181 182 @Override 183 public <I, O> O accept(Visitor<I, O> visitor, I input) { 184 return visitor.visitPrimTy(this, input); 185 } 186 187 /** The primtiive type. */ 188 public TurbineConstantTypeKind tykind() { 189 return tykind; 190 } 191 } 192 193 /** The void type, used only for void-returning methods. */ 194 public static class VoidTy extends Type { 195 196 @Override 197 public Kind kind() { 198 return Kind.VOID_TY; 199 } 200 201 @Override 202 public <I, O> O accept(Visitor<I, O> visitor, I input) { 203 return visitor.visitVoidTy(this, input); 204 } 205 206 public VoidTy(int position) { 207 super(position, ImmutableList.of()); 208 } 209 } 210 211 /** A class, enum, interface, or annotation {@link Type}. */ 212 public static class ClassTy extends Type { 213 private final Optional<ClassTy> base; 214 private final String name; 215 private final ImmutableList<Type> tyargs; 216 217 public ClassTy( 218 int position, 219 Optional<ClassTy> base, 220 String name, 221 ImmutableList<Type> tyargs, 222 ImmutableList<Anno> annos) { 223 super(position, annos); 224 this.base = base; 225 this.name = name; 226 this.tyargs = tyargs; 227 } 228 229 @Override 230 public Kind kind() { 231 return Kind.CLASS_TY; 232 } 233 234 @Override 235 public <I, O> O accept(Visitor<I, O> visitor, I input) { 236 return visitor.visitClassTy(this, input); 237 } 238 239 /** 240 * The base type, for qualified type uses. 241 * 242 * <p>For example, {@code Map.Entry}. 243 */ 244 public Optional<ClassTy> base() { 245 return base; 246 } 247 248 /** The simple name of the type. */ 249 public String name() { 250 return name; 251 } 252 253 /** A possibly empty list of type arguments. */ 254 public ImmutableList<Type> tyargs() { 255 return tyargs; 256 } 257 } 258 259 /** A JLS 3.10 literal expression. */ 260 public static class Literal extends Expression { 261 private final TurbineConstantTypeKind tykind; 262 private final Const value; 263 264 public Literal(int position, TurbineConstantTypeKind tykind, Const value) { 265 super(position); 266 this.tykind = tykind; 267 this.value = value; 268 } 269 270 @Override 271 public Kind kind() { 272 return Kind.LITERAL; 273 } 274 275 @Override 276 public <I, O> O accept(Visitor<I, O> visitor, I input) { 277 return visitor.visitLiteral(this, input); 278 } 279 280 public TurbineConstantTypeKind tykind() { 281 return tykind; 282 } 283 284 public Const value() { 285 return value; 286 } 287 } 288 289 /** A JLS 15.16 cast expression. */ 290 public static class TypeCast extends Expression { 291 private final Type ty; 292 private final Expression expr; 293 294 public TypeCast(int position, Type ty, Expression expr) { 295 super(position); 296 this.ty = ty; 297 this.expr = expr; 298 } 299 300 @Override 301 public Kind kind() { 302 return Kind.TYPE_CAST; 303 } 304 305 @Override 306 public <I, O> O accept(Visitor<I, O> visitor, I input) { 307 return visitor.visitTypeCast(this, input); 308 } 309 310 public Type ty() { 311 return ty; 312 } 313 314 public Expression expr() { 315 return expr; 316 } 317 } 318 319 /** A JLS 15.14 - 14.15 unary expression. */ 320 public static class Unary extends Expression { 321 private final Expression expr; 322 private final TurbineOperatorKind op; 323 324 public Unary(int position, Expression expr, TurbineOperatorKind op) { 325 super(position); 326 this.expr = expr; 327 this.op = op; 328 } 329 330 @Override 331 public Kind kind() { 332 return Kind.UNARY; 333 } 334 335 @Override 336 public <I, O> O accept(Visitor<I, O> visitor, I input) { 337 return visitor.visitUnary(this, input); 338 } 339 340 public Expression expr() { 341 return expr; 342 } 343 344 public TurbineOperatorKind op() { 345 return op; 346 } 347 } 348 349 /** A JLS 15.17 - 14.24 binary expression. */ 350 public static class Binary extends Expression { 351 private final Expression lhs; 352 private final Expression rhs; 353 private final TurbineOperatorKind op; 354 355 public Binary(int position, Expression lhs, Expression rhs, TurbineOperatorKind op) { 356 super(position); 357 this.lhs = lhs; 358 this.rhs = rhs; 359 this.op = op; 360 } 361 362 @Override 363 public Kind kind() { 364 return Kind.BINARY; 365 } 366 367 @Override 368 public <I, O> O accept(Visitor<I, O> visitor, I input) { 369 return visitor.visitBinary(this, input); 370 } 371 372 public Expression lhs() { 373 return lhs; 374 } 375 376 public Expression rhs() { 377 return rhs; 378 } 379 380 public TurbineOperatorKind op() { 381 return op; 382 } 383 } 384 385 /** A JLS 6.5.6.1 simple name that refers to a JSL 4.12.4 constant variable. */ 386 public static class ConstVarName extends Expression { 387 private final ImmutableList<String> name; 388 389 public ConstVarName(int position, ImmutableList<String> name) { 390 super(position); 391 this.name = name; 392 } 393 394 @Override 395 public Kind kind() { 396 return Kind.CONST_VAR_NAME; 397 } 398 399 @Override 400 public <I, O> O accept(Visitor<I, O> visitor, I input) { 401 return visitor.visitConstVarName(this, input); 402 } 403 404 public ImmutableList<String> name() { 405 return name; 406 } 407 } 408 409 /** A JLS 15.8.2 class literal. */ 410 public static class ClassLiteral extends Expression { 411 412 private final Type type; 413 414 public ClassLiteral(int position, Type type) { 415 super(position); 416 this.type = type; 417 } 418 419 @Override 420 public Kind kind() { 421 return Kind.CLASS_LITERAL; 422 } 423 424 @Override 425 public <I, O> O accept(Visitor<I, O> visitor, I input) { 426 return visitor.visitClassLiteral(this, input); 427 } 428 429 public Type type() { 430 return type; 431 } 432 } 433 434 /** A JLS 15.26 assignment expression. */ 435 public static class Assign extends Expression { 436 private final String name; 437 private final Expression expr; 438 439 public Assign(int position, String name, Expression expr) { 440 super(position); 441 this.name = name; 442 this.expr = expr; 443 } 444 445 @Override 446 public Kind kind() { 447 return Kind.ASSIGN; 448 } 449 450 @Override 451 public <I, O> O accept(Visitor<I, O> visitor, I input) { 452 return visitor.visitAssign(this, input); 453 } 454 455 public String name() { 456 return name; 457 } 458 459 public Expression expr() { 460 return expr; 461 } 462 } 463 464 /** A JLS 15.25 conditional expression. */ 465 public static class Conditional extends Expression { 466 private final Expression cond; 467 private final Expression iftrue; 468 private final Expression iffalse; 469 470 public Conditional(int position, Expression cond, Expression iftrue, Expression iffalse) { 471 super(position); 472 this.cond = cond; 473 this.iftrue = iftrue; 474 this.iffalse = iffalse; 475 } 476 477 @Override 478 public Kind kind() { 479 return Kind.CONDITIONAL; 480 } 481 482 @Override 483 public <I, O> O accept(Visitor<I, O> visitor, I input) { 484 return visitor.visitConditional(this, input); 485 } 486 487 public Expression cond() { 488 return cond; 489 } 490 491 public Expression iftrue() { 492 return iftrue; 493 } 494 495 public Expression iffalse() { 496 return iffalse; 497 } 498 } 499 500 /** JLS 10.6 array initializer. */ 501 public static class ArrayInit extends Expression { 502 private final ImmutableList<Expression> exprs; 503 504 public ArrayInit(int position, ImmutableList<Expression> exprs) { 505 super(position); 506 this.exprs = exprs; 507 } 508 509 @Override 510 public Kind kind() { 511 return Kind.ARRAY_INIT; 512 } 513 514 @Override 515 public <I, O> O accept(Visitor<I, O> visitor, I input) { 516 return visitor.visitArrayInit(this, input); 517 } 518 519 public ImmutableList<Expression> exprs() { 520 return exprs; 521 } 522 } 523 524 /** A JLS 7.3 compilation unit. */ 525 public static class CompUnit extends Tree { 526 private final Optional<PkgDecl> pkg; 527 private final ImmutableList<ImportDecl> imports; 528 private final ImmutableList<TyDecl> decls; 529 private final SourceFile source; 530 531 public CompUnit( 532 int position, 533 Optional<PkgDecl> pkg, 534 ImmutableList<ImportDecl> imports, 535 ImmutableList<TyDecl> decls, 536 SourceFile source) { 537 super(position); 538 this.pkg = pkg; 539 this.imports = imports; 540 this.decls = decls; 541 this.source = source; 542 } 543 544 @Override 545 public Kind kind() { 546 return Kind.COMP_UNIT; 547 } 548 549 @Override 550 public <I, O> O accept(Visitor<I, O> visitor, I input) { 551 return visitor.visitCompUnit(this, input); 552 } 553 554 public Optional<PkgDecl> pkg() { 555 return pkg; 556 } 557 558 public ImmutableList<ImportDecl> imports() { 559 return imports; 560 } 561 562 public ImmutableList<TyDecl> decls() { 563 return decls; 564 } 565 566 public SourceFile source() { 567 return source; 568 } 569 } 570 571 /** A JLS 7.5 import declaration. */ 572 public static class ImportDecl extends Tree { 573 private final ImmutableList<String> type; 574 private final boolean stat; 575 private final boolean wild; 576 577 public ImportDecl(int position, ImmutableList<String> type, boolean stat, boolean wild) { 578 super(position); 579 this.type = type; 580 this.stat = stat; 581 this.wild = wild; 582 } 583 584 @Override 585 public Kind kind() { 586 return Kind.IMPORT_DECL; 587 } 588 589 @Override 590 public <I, O> O accept(Visitor<I, O> visitor, I input) { 591 return visitor.visitImportDecl(this, input); 592 } 593 594 public ImmutableList<String> type() { 595 return type; 596 } 597 598 /** Returns true for static member imports. */ 599 public boolean stat() { 600 return stat; 601 } 602 603 /** Returns true for wildcard imports. */ 604 public boolean wild() { 605 return wild; 606 } 607 } 608 609 /** A JLS 8.3 field declaration, JLS 8.4.1 formal method parameter, or JLS 14.4 variable. */ 610 public static class VarDecl extends Tree { 611 private final ImmutableSet<TurbineModifier> mods; 612 private final ImmutableList<Anno> annos; 613 private final Tree ty; 614 private final String name; 615 private final Optional<Expression> init; 616 617 public VarDecl( 618 int position, 619 Set<TurbineModifier> mods, 620 ImmutableList<Anno> annos, 621 Tree ty, 622 String name, 623 Optional<Expression> init) { 624 super(position); 625 this.mods = ImmutableSet.copyOf(mods); 626 this.annos = annos; 627 this.ty = ty; 628 this.name = name; 629 this.init = init; 630 } 631 632 @Override 633 public Kind kind() { 634 return Kind.VAR_DECL; 635 } 636 637 @Override 638 public <I, O> O accept(Visitor<I, O> visitor, I input) { 639 return visitor.visitVarDecl(this, input); 640 } 641 642 public ImmutableSet<TurbineModifier> mods() { 643 return mods; 644 } 645 646 public ImmutableList<Anno> annos() { 647 return annos; 648 } 649 650 public Tree ty() { 651 return ty; 652 } 653 654 public String name() { 655 return name; 656 } 657 658 public Optional<Expression> init() { 659 return init; 660 } 661 } 662 663 /** A JLS 8.4 method declaration. */ 664 public static class MethDecl extends Tree { 665 private final ImmutableSet<TurbineModifier> mods; 666 private final ImmutableList<Anno> annos; 667 private final ImmutableList<TyParam> typarams; 668 private final Optional<Tree> ret; 669 private final String name; 670 private final ImmutableList<VarDecl> params; 671 private final ImmutableList<ClassTy> exntys; 672 private final Optional<Tree> defaultValue; 673 674 public MethDecl( 675 int position, 676 Set<TurbineModifier> mods, 677 ImmutableList<Anno> annos, 678 ImmutableList<TyParam> typarams, 679 Optional<Tree> ret, 680 String name, 681 ImmutableList<VarDecl> params, 682 ImmutableList<ClassTy> exntys, 683 Optional<Tree> defaultValue) { 684 super(position); 685 this.mods = ImmutableSet.copyOf(mods); 686 this.annos = annos; 687 this.typarams = typarams; 688 this.ret = ret; 689 this.name = name; 690 this.params = params; 691 this.exntys = exntys; 692 this.defaultValue = defaultValue; 693 } 694 695 @Override 696 public Kind kind() { 697 return Kind.METH_DECL; 698 } 699 700 @Override 701 public <I, O> O accept(Visitor<I, O> visitor, I input) { 702 return visitor.visitMethDecl(this, input); 703 } 704 705 public ImmutableSet<TurbineModifier> mods() { 706 return mods; 707 } 708 709 public ImmutableList<Anno> annos() { 710 return annos; 711 } 712 713 public ImmutableList<TyParam> typarams() { 714 return typarams; 715 } 716 717 public Optional<Tree> ret() { 718 return ret; 719 } 720 721 public String name() { 722 return name; 723 } 724 725 public ImmutableList<VarDecl> params() { 726 return params; 727 } 728 729 public ImmutableList<ClassTy> exntys() { 730 return exntys; 731 } 732 733 public Optional<Tree> defaultValue() { 734 return defaultValue; 735 } 736 } 737 738 /** A JLS 9.7 annotation. */ 739 public static class Anno extends Tree { 740 private final ImmutableList<String> name; 741 private final ImmutableList<Expression> args; 742 743 public Anno(int position, ImmutableList<String> name, ImmutableList<Expression> args) { 744 super(position); 745 this.name = name; 746 this.args = args; 747 } 748 749 @Override 750 public Kind kind() { 751 return Kind.ANNO; 752 } 753 754 @Override 755 public <I, O> O accept(Visitor<I, O> visitor, I input) { 756 return visitor.visitAnno(this, input); 757 } 758 759 public ImmutableList<String> name() { 760 return name; 761 } 762 763 public ImmutableList<Expression> args() { 764 return args; 765 } 766 } 767 768 /** 769 * An annotation in an expression context, e.g. an annotation literal nested inside another 770 * annotation. 771 */ 772 public static class AnnoExpr extends Expression { 773 774 private final Anno value; 775 776 public AnnoExpr(int position, Anno value) { 777 super(position); 778 this.value = value; 779 } 780 781 /** The annotation. */ 782 public Anno value() { 783 return value; 784 } 785 786 @Override 787 public Kind kind() { 788 return Kind.ANNO_EXPR; 789 } 790 791 @Override 792 public <I, O> O accept(Visitor<I, O> visitor, I input) { 793 return visitor.visitAnno(value, input); 794 } 795 } 796 797 /** A JLS 7.6 or 8.5 type declaration. */ 798 public static class TyDecl extends Tree { 799 private final ImmutableSet<TurbineModifier> mods; 800 private final ImmutableList<Anno> annos; 801 private final String name; 802 private final ImmutableList<TyParam> typarams; 803 private final Optional<ClassTy> xtnds; 804 private final ImmutableList<ClassTy> impls; 805 private final ImmutableList<Tree> members; 806 private final TurbineTyKind tykind; 807 808 public TyDecl( 809 int position, 810 Set<TurbineModifier> mods, 811 ImmutableList<Anno> annos, 812 String name, 813 ImmutableList<TyParam> typarams, 814 Optional<ClassTy> xtnds, 815 ImmutableList<ClassTy> impls, 816 ImmutableList<Tree> members, 817 TurbineTyKind tykind) { 818 super(position); 819 this.mods = ImmutableSet.copyOf(mods); 820 this.annos = annos; 821 this.name = name; 822 this.typarams = typarams; 823 this.xtnds = xtnds; 824 this.impls = impls; 825 this.members = members; 826 this.tykind = tykind; 827 } 828 829 @Override 830 public Kind kind() { 831 return Kind.TY_DECL; 832 } 833 834 @Override 835 public <I, O> O accept(Visitor<I, O> visitor, I input) { 836 return visitor.visitTyDecl(this, input); 837 } 838 839 public ImmutableSet<TurbineModifier> mods() { 840 return mods; 841 } 842 843 public ImmutableList<Anno> annos() { 844 return annos; 845 } 846 847 public String name() { 848 return name; 849 } 850 851 public ImmutableList<TyParam> typarams() { 852 return typarams; 853 } 854 855 public Optional<ClassTy> xtnds() { 856 return xtnds; 857 } 858 859 public ImmutableList<ClassTy> impls() { 860 return impls; 861 } 862 863 public ImmutableList<Tree> members() { 864 return members; 865 } 866 867 public TurbineTyKind tykind() { 868 return tykind; 869 } 870 } 871 872 /** A JLS 4.4. type variable declaration. */ 873 public static class TyParam extends Tree { 874 private final String name; 875 private final ImmutableList<Tree> bounds; 876 private final ImmutableList<Anno> annos; 877 878 public TyParam( 879 int position, String name, ImmutableList<Tree> bounds, ImmutableList<Anno> annos) { 880 super(position); 881 this.name = name; 882 this.bounds = bounds; 883 this.annos = annos; 884 } 885 886 @Override 887 public Kind kind() { 888 return Kind.TY_PARAM; 889 } 890 891 @Override 892 public <I, O> O accept(Visitor<I, O> visitor, I input) { 893 return visitor.visitTyParam(this, input); 894 } 895 896 public String name() { 897 return name; 898 } 899 900 public ImmutableList<Tree> bounds() { 901 return bounds; 902 } 903 904 public ImmutableList<Anno> annos() { 905 return annos; 906 } 907 } 908 909 /** A JLS 7.4 package declaration. */ 910 public static class PkgDecl extends Tree { 911 private final ImmutableList<String> name; 912 private final ImmutableList<Anno> annos; 913 914 public PkgDecl(int position, ImmutableList<String> name, ImmutableList<Anno> annos) { 915 super(position); 916 this.name = name; 917 this.annos = annos; 918 } 919 920 @Override 921 public Kind kind() { 922 return Kind.PKG_DECL; 923 } 924 925 @Override 926 public <I, O> O accept(Visitor<I, O> visitor, I input) { 927 return visitor.visitPkgDecl(this, input); 928 } 929 930 public ImmutableList<String> name() { 931 return name; 932 } 933 934 public ImmutableList<Anno> annos() { 935 return annos; 936 } 937 } 938 939 /** A visitor for {@link Tree}s. */ 940 public interface Visitor<I, O> { 941 O visitWildTy(WildTy visitor, I input); 942 943 O visitArrTy(ArrTy arrTy, I input); 944 945 O visitPrimTy(PrimTy primTy, I input); 946 947 O visitVoidTy(VoidTy primTy, I input); 948 949 O visitClassTy(ClassTy visitor, I input); 950 951 O visitLiteral(Literal literal, I input); 952 953 O visitTypeCast(TypeCast typeCast, I input); 954 955 O visitUnary(Unary unary, I input); 956 957 O visitBinary(Binary binary, I input); 958 959 O visitConstVarName(ConstVarName constVarName, I input); 960 961 O visitClassLiteral(ClassLiteral classLiteral, I input); 962 963 O visitAssign(Assign assign, I input); 964 965 O visitConditional(Conditional conditional, I input); 966 967 O visitArrayInit(ArrayInit arrayInit, I input); 968 969 O visitCompUnit(CompUnit compUnit, I input); 970 971 O visitImportDecl(ImportDecl importDecl, I input); 972 973 O visitVarDecl(VarDecl varDecl, I input); 974 975 O visitMethDecl(MethDecl methDecl, I input); 976 977 O visitAnno(Anno anno, I input); 978 979 O visitTyDecl(TyDecl tyDecl, I input); 980 981 O visitTyParam(TyParam tyParam, I input); 982 983 O visitPkgDecl(PkgDecl pkgDecl, I input); 984 } 985 } 986