aboutsummaryrefslogtreecommitdiff
path: root/java/com/google/turbine/tree/Tree.java
blob: f7917b9b64ab548fadcab44b999a7e49b1155c95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
/*
 * Copyright 2016 Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.turbine.tree;

import static java.util.Objects.requireNonNull;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.Immutable;
import com.google.turbine.diag.SourceFile;
import com.google.turbine.model.Const;
import com.google.turbine.model.TurbineConstantTypeKind;
import com.google.turbine.model.TurbineTyKind;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Optional;
import java.util.Set;
import org.jspecify.nullness.Nullable;

/** An AST node. */
public abstract class Tree {

  public abstract Kind kind();

  public abstract <I extends @Nullable Object, O extends @Nullable Object> O accept(
      Visitor<I, O> visitor, I input);

  private final int position;

  protected Tree(int position) {
    this.position = position;
  }

  public int position() {
    return position;
  }

  @Override
  public String toString() {
    return Pretty.pretty(this);
  }

  /** Tree kind. */
  public enum Kind {
    IDENT,
    WILD_TY,
    ARR_TY,
    PRIM_TY,
    VOID_TY,
    CLASS_TY,
    LITERAL,
    PAREN,
    TYPE_CAST,
    UNARY,
    BINARY,
    CONST_VAR_NAME,
    CLASS_LITERAL,
    ASSIGN,
    CONDITIONAL,
    ARRAY_INIT,
    COMP_UNIT,
    IMPORT_DECL,
    VAR_DECL,
    METH_DECL,
    ANNO,
    ANNO_EXPR,
    TY_DECL,
    TY_PARAM,
    PKG_DECL,
    MOD_DECL,
    MOD_REQUIRES,
    MOD_EXPORTS,
    MOD_OPENS,
    MOD_USES,
    MOD_PROVIDES
  }

  /** An identifier. */
  @Immutable
  public static class Ident extends Tree {

    private final String value;

    public Ident(int position, String value) {
      super(position);
      this.value = value;
    }

    @Override
    public Kind kind() {
      return Kind.IDENT;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitIdent(this, input);
    }

    public String value() {
      return value;
    }

    @Override
    public String toString() {
      return value;
    }
  }

  /** A type use. */
  public abstract static class Type extends Tree {
    private final ImmutableList<Anno> annos;

    public Type(int position, ImmutableList<Anno> annos) {
      super(position);
      this.annos = annos;
    }

    public ImmutableList<Anno> annos() {
      return annos;
    }
  }

  /** An expression. */
  public abstract static class Expression extends Tree {
    public Expression(int position) {
      super(position);
    }
  }

  /** A wildcard type, possibly with an upper or lower bound. */
  public static class WildTy extends Type {
    private final Optional<Type> upper;
    private final Optional<Type> lower;

    public WildTy(
        int position, ImmutableList<Anno> annos, Optional<Type> upper, Optional<Type> lower) {
      super(position, annos);
      this.upper = upper;
      this.lower = lower;
    }

    @Override
    public Kind kind() {
      return Kind.WILD_TY;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitWildTy(this, input);
    }

    /**
     * An optional upper (extends) bound.
     *
     * <p>At most one of {@link #upper} and {@link #lower} will be set.
     */
    public Optional<Type> upper() {
      return upper;
    }

    /**
     * An optional lower (super) bound.
     *
     * <p>At most one of {@link #upper} and {@link #lower} will be set.
     */
    public Optional<Type> lower() {
      return lower;
    }
  }

  /** An array type. */
  public static class ArrTy extends Type {
    private final Type elem;

    public ArrTy(int position, ImmutableList<Anno> annos, Type elem) {
      super(position, annos);
      this.elem = elem;
    }

    @Override
    public Kind kind() {
      return Kind.ARR_TY;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitArrTy(this, input);
    }

    /**
     * The element type of the array.
     *
     * <p>Multi-dimensional arrays are represented as nested {@link ArrTy}s.
     */
    public Type elem() {
      return elem;
    }
  }

  /** A primitive type. */
  public static class PrimTy extends Type {
    private final TurbineConstantTypeKind tykind;

    public PrimTy(int position, ImmutableList<Anno> annos, TurbineConstantTypeKind tykind) {
      super(position, annos);
      this.tykind = tykind;
    }

    @Override
    public Kind kind() {
      return Kind.PRIM_TY;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitPrimTy(this, input);
    }

    /** The primtiive type. */
    public TurbineConstantTypeKind tykind() {
      return tykind;
    }
  }

  /** The void type, used only for void-returning methods. */
  public static class VoidTy extends Type {

    @Override
    public Kind kind() {
      return Kind.VOID_TY;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitVoidTy(this, input);
    }

    public VoidTy(int position) {
      super(position, ImmutableList.of());
    }
  }

  /** A class, enum, interface, or annotation {@link Type}. */
  public static class ClassTy extends Type {
    private final Optional<ClassTy> base;
    private final Ident name;
    private final ImmutableList<Type> tyargs;

    public ClassTy(
        int position,
        Optional<ClassTy> base,
        Ident name,
        ImmutableList<Type> tyargs,
        ImmutableList<Anno> annos) {
      super(position, annos);
      this.base = base;
      this.name = name;
      this.tyargs = tyargs;
    }

    @Override
    public Kind kind() {
      return Kind.CLASS_TY;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitClassTy(this, input);
    }

    /**
     * The base type, for qualified type uses.
     *
     * <p>For example, {@code Map.Entry}.
     */
    public Optional<ClassTy> base() {
      return base;
    }

    /** The simple name of the type. */
    public Ident name() {
      return name;
    }

    /** A possibly empty list of type arguments. */
    public ImmutableList<Type> tyargs() {
      return tyargs;
    }
  }

  /** A JLS 3.10 literal expression. */
  public static class Literal extends Expression {
    private final TurbineConstantTypeKind tykind;
    private final Const value;

    public Literal(int position, TurbineConstantTypeKind tykind, Const value) {
      super(position);
      this.tykind = tykind;
      this.value = value;
    }

    @Override
    public Kind kind() {
      return Kind.LITERAL;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitLiteral(this, input);
    }

    public TurbineConstantTypeKind tykind() {
      return tykind;
    }

    public Const value() {
      return value;
    }
  }

  /** A JLS 15.8.5 parenthesized expression. */
  public static class Paren extends Expression {
    private final Expression expr;

    public Paren(int position, Expression expr) {
      super(position);
      this.expr = expr;
    }

    @Override
    public Kind kind() {
      return Kind.PAREN;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitParen(this, input);
    }

    public Expression expr() {
      return expr;
    }
  }

  /** A JLS 15.16 cast expression. */
  public static class TypeCast extends Expression {
    private final Type ty;
    private final Expression expr;

    public TypeCast(int position, Type ty, Expression expr) {
      super(position);
      this.ty = ty;
      this.expr = expr;
    }

    @Override
    public Kind kind() {
      return Kind.TYPE_CAST;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitTypeCast(this, input);
    }

    public Type ty() {
      return ty;
    }

    public Expression expr() {
      return expr;
    }
  }

  /** A JLS 15.14 - 14.15 unary expression. */
  public static class Unary extends Expression {
    private final Expression expr;
    private final TurbineOperatorKind op;

    public Unary(int position, Expression expr, TurbineOperatorKind op) {
      super(position);
      this.expr = expr;
      this.op = op;
    }

    @Override
    public Kind kind() {
      return Kind.UNARY;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitUnary(this, input);
    }

    public Expression expr() {
      return expr;
    }

    public TurbineOperatorKind op() {
      return op;
    }
  }

  /** A JLS 15.17 - 14.24 binary expression. */
  public static class Binary extends Expression {
    private final Expression lhs;
    private final Expression rhs;
    private final TurbineOperatorKind op;

    public Binary(int position, Expression lhs, Expression rhs, TurbineOperatorKind op) {
      super(position);
      this.lhs = lhs;
      this.rhs = rhs;
      this.op = op;
    }

    @Override
    public Kind kind() {
      return Kind.BINARY;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitBinary(this, input);
    }

    public Iterable<Expression> children() {
      ImmutableList.Builder<Expression> children = ImmutableList.builder();
      Deque<Expression> stack = new ArrayDeque<>();
      stack.addFirst(rhs);
      stack.addFirst(lhs);
      while (!stack.isEmpty()) {
        Expression curr = stack.removeFirst();
        if (curr.kind().equals(Kind.BINARY)) {
          Binary b = ((Binary) curr);
          if (b.op().equals(op())) {
            stack.addFirst(b.rhs);
            stack.addFirst(b.lhs);
            continue;
          }
        }
        children.add(curr);
      }
      return children.build();
    }

    public TurbineOperatorKind op() {
      return op;
    }
  }

  /** A JLS 6.5.6.1 simple name that refers to a JSL 4.12.4 constant variable. */
  public static class ConstVarName extends Expression {
    private final ImmutableList<Ident> name;

    public ConstVarName(int position, ImmutableList<Ident> name) {
      super(position);
      this.name = name;
    }

    @Override
    public Kind kind() {
      return Kind.CONST_VAR_NAME;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitConstVarName(this, input);
    }

    public ImmutableList<Ident> name() {
      return name;
    }
  }

  /** A JLS 15.8.2 class literal. */
  public static class ClassLiteral extends Expression {

    private final Type type;

    public ClassLiteral(int position, Type type) {
      super(position);
      this.type = type;
    }

    @Override
    public Kind kind() {
      return Kind.CLASS_LITERAL;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitClassLiteral(this, input);
    }

    public Type type() {
      return type;
    }
  }

  /** A JLS 15.26 assignment expression. */
  public static class Assign extends Expression {
    private final Ident name;
    private final Expression expr;

    public Assign(int position, Ident name, Expression expr) {
      super(position);
      this.name = requireNonNull(name);
      this.expr = requireNonNull(expr);
    }

    @Override
    public Kind kind() {
      return Kind.ASSIGN;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitAssign(this, input);
    }

    public Ident name() {
      return name;
    }

    public Expression expr() {
      return expr;
    }
  }

  /** A JLS 15.25 conditional expression. */
  public static class Conditional extends Expression {
    private final Expression cond;
    private final Expression iftrue;
    private final Expression iffalse;

    public Conditional(int position, Expression cond, Expression iftrue, Expression iffalse) {
      super(position);
      this.cond = cond;
      this.iftrue = iftrue;
      this.iffalse = iffalse;
    }

    @Override
    public Kind kind() {
      return Kind.CONDITIONAL;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitConditional(this, input);
    }

    public Expression cond() {
      return cond;
    }

    public Expression iftrue() {
      return iftrue;
    }

    public Expression iffalse() {
      return iffalse;
    }
  }

  /** JLS 10.6 array initializer. */
  public static class ArrayInit extends Expression {
    private final ImmutableList<Expression> exprs;

    public ArrayInit(int position, ImmutableList<Expression> exprs) {
      super(position);
      this.exprs = exprs;
    }

    @Override
    public Kind kind() {
      return Kind.ARRAY_INIT;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitArrayInit(this, input);
    }

    public ImmutableList<Expression> exprs() {
      return exprs;
    }
  }

  /** A JLS 7.3 compilation unit. */
  public static class CompUnit extends Tree {
    private final Optional<PkgDecl> pkg;
    private final Optional<ModDecl> mod;
    private final ImmutableList<ImportDecl> imports;
    private final ImmutableList<TyDecl> decls;
    private final SourceFile source;

    public CompUnit(
        int position,
        Optional<PkgDecl> pkg,
        Optional<ModDecl> mod,
        ImmutableList<ImportDecl> imports,
        ImmutableList<TyDecl> decls,
        SourceFile source) {
      super(position);
      this.pkg = pkg;
      this.mod = mod;
      this.imports = imports;
      this.decls = decls;
      this.source = source;
    }

    @Override
    public Kind kind() {
      return Kind.COMP_UNIT;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitCompUnit(this, input);
    }

    public Optional<PkgDecl> pkg() {
      return pkg;
    }

    public Optional<ModDecl> mod() {
      return mod;
    }

    public ImmutableList<ImportDecl> imports() {
      return imports;
    }

    public ImmutableList<TyDecl> decls() {
      return decls;
    }

    public SourceFile source() {
      return source;
    }
  }

  /** A JLS 7.5 import declaration. */
  public static class ImportDecl extends Tree {
    private final ImmutableList<Ident> type;
    private final boolean stat;
    private final boolean wild;

    public ImportDecl(int position, ImmutableList<Ident> type, boolean stat, boolean wild) {
      super(position);
      this.type = type;
      this.stat = stat;
      this.wild = wild;
    }

    @Override
    public Kind kind() {
      return Kind.IMPORT_DECL;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitImportDecl(this, input);
    }

    public ImmutableList<Ident> type() {
      return type;
    }

    /** Returns true for static member imports. */
    public boolean stat() {
      return stat;
    }

    /** Returns true for wildcard imports. */
    public boolean wild() {
      return wild;
    }
  }

  /** A JLS 8.3 field declaration, JLS 8.4.1 formal method parameter, or JLS 14.4 variable. */
  public static class VarDecl extends Tree {
    private final ImmutableSet<TurbineModifier> mods;
    private final ImmutableList<Anno> annos;
    private final Tree ty;
    private final Ident name;
    private final Optional<Expression> init;
    private final @Nullable String javadoc;

    public VarDecl(
        int position,
        Set<TurbineModifier> mods,
        ImmutableList<Anno> annos,
        Tree ty,
        Ident name,
        Optional<Expression> init,
        @Nullable String javadoc) {
      super(position);
      this.mods = ImmutableSet.copyOf(mods);
      this.annos = annos;
      this.ty = ty;
      this.name = name;
      this.init = init;
      this.javadoc = javadoc;
    }

    @Override
    public Kind kind() {
      return Kind.VAR_DECL;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitVarDecl(this, input);
    }

    public ImmutableSet<TurbineModifier> mods() {
      return mods;
    }

    public ImmutableList<Anno> annos() {
      return annos;
    }

    public Tree ty() {
      return ty;
    }

    public Ident name() {
      return name;
    }

    public Optional<Expression> init() {
      return init;
    }

    /**
     * A javadoc comment, excluding the opening and closing delimiters but including all interior
     * characters and whitespace.
     */
    public @Nullable String javadoc() {
      return javadoc;
    }
  }

  /** A JLS 8.4 method declaration. */
  public static class MethDecl extends Tree {
    private final ImmutableSet<TurbineModifier> mods;
    private final ImmutableList<Anno> annos;
    private final ImmutableList<TyParam> typarams;
    private final Optional<Tree> ret;
    private final Ident name;
    private final ImmutableList<VarDecl> params;
    private final ImmutableList<ClassTy> exntys;
    private final Optional<Tree> defaultValue;
    private final String javadoc;

    public 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) {
      super(position);
      this.mods = ImmutableSet.copyOf(mods);
      this.annos = annos;
      this.typarams = typarams;
      this.ret = ret;
      this.name = name;
      this.params = params;
      this.exntys = exntys;
      this.defaultValue = defaultValue;
      this.javadoc = javadoc;
    }

    @Override
    public Kind kind() {
      return Kind.METH_DECL;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitMethDecl(this, input);
    }

    public ImmutableSet<TurbineModifier> mods() {
      return mods;
    }

    public ImmutableList<Anno> annos() {
      return annos;
    }

    public ImmutableList<TyParam> typarams() {
      return typarams;
    }

    public Optional<Tree> ret() {
      return ret;
    }

    public Ident name() {
      return name;
    }

    public ImmutableList<VarDecl> params() {
      return params;
    }

    public ImmutableList<ClassTy> exntys() {
      return exntys;
    }

    public Optional<Tree> defaultValue() {
      return defaultValue;
    }
    /**
     * A javadoc comment, excluding the opening and closing delimiters but including all interior
     * characters and whitespace.
     */
    public String javadoc() {
      return javadoc;
    }
  }

  /** A JLS 9.7 annotation. */
  public static class Anno extends Tree {
    private final ImmutableList<Ident> name;
    private final ImmutableList<Expression> args;

    public Anno(int position, ImmutableList<Ident> name, ImmutableList<Expression> args) {
      super(position);
      this.name = name;
      this.args = args;
    }

    @Override
    public Kind kind() {
      return Kind.ANNO;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitAnno(this, input);
    }

    public ImmutableList<Ident> name() {
      return name;
    }

    public ImmutableList<Expression> args() {
      return args;
    }
  }

  /**
   * An annotation in an expression context, e.g. an annotation literal nested inside another
   * annotation.
   */
  public static class AnnoExpr extends Expression {

    private final Anno value;

    public AnnoExpr(int position, Anno value) {
      super(position);
      this.value = value;
    }

    /** The annotation. */
    public Anno value() {
      return value;
    }

    @Override
    public Kind kind() {
      return Kind.ANNO_EXPR;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitAnno(value, input);
    }
  }

  /** A JLS 7.6 or 8.5 type declaration. */
  public static class TyDecl extends Tree {
    private final ImmutableSet<TurbineModifier> mods;
    private final ImmutableList<Anno> annos;
    private final Ident name;
    private final ImmutableList<TyParam> typarams;
    private final Optional<ClassTy> xtnds;
    private final ImmutableList<ClassTy> impls;
    private final ImmutableList<ClassTy> permits;
    private final ImmutableList<Tree> members;
    private final ImmutableList<VarDecl> components;
    private final TurbineTyKind tykind;
    private final @Nullable String javadoc;

    public 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) {
      super(position);
      this.mods = ImmutableSet.copyOf(mods);
      this.annos = annos;
      this.name = name;
      this.typarams = typarams;
      this.xtnds = xtnds;
      this.impls = impls;
      this.permits = permits;
      this.members = members;
      this.components = components;
      this.tykind = tykind;
      this.javadoc = javadoc;
    }

    @Override
    public Kind kind() {
      return Kind.TY_DECL;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitTyDecl(this, input);
    }

    public ImmutableSet<TurbineModifier> mods() {
      return mods;
    }

    public ImmutableList<Anno> annos() {
      return annos;
    }

    public Ident name() {
      return name;
    }

    public ImmutableList<TyParam> typarams() {
      return typarams;
    }

    public Optional<ClassTy> xtnds() {
      return xtnds;
    }

    public ImmutableList<ClassTy> impls() {
      return impls;
    }

    public ImmutableList<ClassTy> permits() {
      return permits;
    }

    public ImmutableList<Tree> members() {
      return members;
    }

    public ImmutableList<VarDecl> components() {
      return components;
    }

    public TurbineTyKind tykind() {
      return tykind;
    }
    /**
     * A javadoc comment, excluding the opening and closing delimiters but including all interior
     * characters and whitespace.
     */
    public @Nullable String javadoc() {
      return javadoc;
    }
  }

  /** A JLS 4.4. type variable declaration. */
  public static class TyParam extends Tree {
    private final Ident name;
    private final ImmutableList<Tree> bounds;
    private final ImmutableList<Anno> annos;

    public TyParam(
        int position, Ident name, ImmutableList<Tree> bounds, ImmutableList<Anno> annos) {
      super(position);
      this.name = name;
      this.bounds = bounds;
      this.annos = annos;
    }

    @Override
    public Kind kind() {
      return Kind.TY_PARAM;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitTyParam(this, input);
    }

    public Ident name() {
      return name;
    }

    public ImmutableList<Tree> bounds() {
      return bounds;
    }

    public ImmutableList<Anno> annos() {
      return annos;
    }
  }

  /** A JLS 7.4 package declaration. */
  public static class PkgDecl extends Tree {
    private final ImmutableList<Ident> name;
    private final ImmutableList<Anno> annos;

    public PkgDecl(int position, ImmutableList<Ident> name, ImmutableList<Anno> annos) {
      super(position);
      this.name = name;
      this.annos = annos;
    }

    @Override
    public Kind kind() {
      return Kind.PKG_DECL;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitPkgDecl(this, input);
    }

    public ImmutableList<Ident> name() {
      return name;
    }

    public ImmutableList<Anno> annos() {
      return annos;
    }
  }

  /** A JLS 7.7 module declaration. */
  public static class ModDecl extends Tree {

    private final ImmutableList<Anno> annos;
    private final boolean open;
    private final String moduleName;
    private final ImmutableList<ModDirective> directives;

    public ModDecl(
        int position,
        ImmutableList<Anno> annos,
        boolean open,
        String moduleName,
        ImmutableList<ModDirective> directives) {
      super(position);
      this.annos = annos;
      this.open = open;
      this.moduleName = moduleName;
      this.directives = directives;
    }

    public boolean open() {
      return open;
    }

    public ImmutableList<Anno> annos() {
      return annos;
    }

    public String moduleName() {
      return moduleName;
    }

    public ImmutableList<ModDirective> directives() {
      return directives;
    }

    @Override
    public Kind kind() {
      return Kind.MOD_DECL;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitModDecl(this, input);
    }
  }

  /** A kind of module directive. */
  public abstract static class ModDirective extends Tree {

    /** A module directive kind. */
    public enum DirectiveKind {
      REQUIRES,
      EXPORTS,
      OPENS,
      USES,
      PROVIDES
    }

    public abstract DirectiveKind directiveKind();

    protected ModDirective(int position) {
      super(position);
    }
  }

  /** A JLS 7.7.1 module requires directive. */
  public static class ModRequires extends ModDirective {

    private final ImmutableSet<TurbineModifier> mods;
    private final String moduleName;

    @Override
    public Kind kind() {
      return Kind.MOD_REQUIRES;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitModRequires(this, input);
    }

    public ModRequires(int position, ImmutableSet<TurbineModifier> mods, String moduleName) {
      super(position);
      this.mods = mods;
      this.moduleName = moduleName;
    }

    public ImmutableSet<TurbineModifier> mods() {
      return mods;
    }

    public String moduleName() {
      return moduleName;
    }

    @Override
    public DirectiveKind directiveKind() {
      return DirectiveKind.REQUIRES;
    }
  }

  /** A JLS 7.7.2 module exports directive. */
  public static class ModExports extends ModDirective {

    private final String packageName;
    private final ImmutableList<String> moduleNames;

    @Override
    public Kind kind() {
      return Kind.MOD_EXPORTS;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitModExports(this, input);
    }

    public ModExports(int position, String packageName, ImmutableList<String> moduleNames) {
      super(position);
      this.packageName = packageName;
      this.moduleNames = moduleNames;
    }

    public String packageName() {
      return packageName;
    }

    public ImmutableList<String> moduleNames() {
      return moduleNames;
    }

    @Override
    public DirectiveKind directiveKind() {
      return DirectiveKind.EXPORTS;
    }
  }

  /** A JLS 7.7.2 module opens directive. */
  public static class ModOpens extends ModDirective {

    private final String packageName;
    private final ImmutableList<String> moduleNames;

    public ModOpens(int position, String packageName, ImmutableList<String> moduleNames) {
      super(position);
      this.packageName = packageName;
      this.moduleNames = moduleNames;
    }

    public String packageName() {
      return packageName;
    }

    public ImmutableList<String> moduleNames() {
      return moduleNames;
    }

    @Override
    public Kind kind() {
      return Kind.MOD_OPENS;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitModOpens(this, input);
    }

    @Override
    public DirectiveKind directiveKind() {
      return DirectiveKind.OPENS;
    }
  }

  /** A JLS 7.7.3 module uses directive. */
  public static class ModUses extends ModDirective {

    private final ImmutableList<Ident> typeName;

    public ModUses(int position, ImmutableList<Ident> typeName) {
      super(position);
      this.typeName = typeName;
    }

    public ImmutableList<Ident> typeName() {
      return typeName;
    }

    @Override
    public Kind kind() {
      return Kind.MOD_USES;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitModUses(this, input);
    }

    @Override
    public DirectiveKind directiveKind() {
      return DirectiveKind.USES;
    }
  }

  /** A JLS 7.7.4 module uses directive. */
  public static class ModProvides extends ModDirective {

    private final ImmutableList<Ident> typeName;
    private final ImmutableList<ImmutableList<Ident>> implNames;

    public ModProvides(
        int position,
        ImmutableList<Ident> typeName,
        ImmutableList<ImmutableList<Ident>> implNames) {
      super(position);
      this.typeName = typeName;
      this.implNames = implNames;
    }

    public ImmutableList<Ident> typeName() {
      return typeName;
    }

    public ImmutableList<ImmutableList<Ident>> implNames() {
      return implNames;
    }

    @Override
    public Kind kind() {
      return Kind.MOD_PROVIDES;
    }

    @Override
    public <I extends @Nullable Object, O extends @Nullable Object> O accept(
        Visitor<I, O> visitor, I input) {
      return visitor.visitModProvides(this, input);
    }

    @Override
    public DirectiveKind directiveKind() {
      return DirectiveKind.PROVIDES;
    }
  }

  /** A visitor for {@link Tree}s. */
  public interface Visitor<I extends @Nullable Object, O extends @Nullable Object> {
    O visitIdent(Ident ident, I input);

    O visitWildTy(WildTy visitor, I input);

    O visitArrTy(ArrTy arrTy, I input);

    O visitPrimTy(PrimTy primTy, I input);

    O visitVoidTy(VoidTy primTy, I input);

    O visitClassTy(ClassTy visitor, I input);

    O visitLiteral(Literal literal, I input);

    O visitParen(Paren unary, I input);

    O visitTypeCast(TypeCast typeCast, I input);

    O visitUnary(Unary unary, I input);

    O visitBinary(Binary binary, I input);

    O visitConstVarName(ConstVarName constVarName, I input);

    O visitClassLiteral(ClassLiteral classLiteral, I input);

    O visitAssign(Assign assign, I input);

    O visitConditional(Conditional conditional, I input);

    O visitArrayInit(ArrayInit arrayInit, I input);

    O visitCompUnit(CompUnit compUnit, I input);

    O visitImportDecl(ImportDecl importDecl, I input);

    O visitVarDecl(VarDecl varDecl, I input);

    O visitMethDecl(MethDecl methDecl, I input);

    O visitAnno(Anno anno, I input);

    O visitTyDecl(TyDecl tyDecl, I input);

    O visitTyParam(TyParam tyParam, I input);

    O visitPkgDecl(PkgDecl pkgDecl, I input);

    O visitModDecl(ModDecl modDecl, I input);

    O visitModRequires(ModRequires modRequires, I input);

    O visitModExports(ModExports modExports, I input);

    O visitModOpens(ModOpens modOpens, I input);

    O visitModUses(ModUses modUses, I input);

    O visitModProvides(ModProvides modProvides, I input);
  }
}