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