JCTree.java revision 2571:10fc81ac75b4
1/*
2 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.javac.tree;
27
28import java.io.IOException;
29import java.io.StringWriter;
30import java.util.*;
31
32import javax.lang.model.element.Modifier;
33import javax.lang.model.type.TypeKind;
34import javax.tools.JavaFileObject;
35
36import com.sun.source.tree.*;
37import com.sun.source.tree.LambdaExpressionTree.BodyKind;
38import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
39import com.sun.tools.javac.code.*;
40import com.sun.tools.javac.code.Scope.*;
41import com.sun.tools.javac.code.Symbol.*;
42import com.sun.tools.javac.util.*;
43import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
44import com.sun.tools.javac.util.List;
45import static com.sun.tools.javac.tree.JCTree.Tag.*;
46
47/**
48 * Root class for abstract syntax tree nodes. It provides definitions
49 * for specific tree nodes as subclasses nested inside.
50 *
51 * <p>Each subclass is highly standardized.  It generally contains
52 * only tree fields for the syntactic subcomponents of the node.  Some
53 * classes that represent identifier uses or definitions also define a
54 * Symbol field that denotes the represented identifier.  Classes for
55 * non-local jumps also carry the jump target as a field.  The root
56 * class Tree itself defines fields for the tree's type and position.
57 * No other fields are kept in a tree node; instead parameters are
58 * passed to methods accessing the node.
59 *
60 * <p>Except for the methods defined by com.sun.source, the only
61 * method defined in subclasses is `visit' which applies a given
62 * visitor to the tree. The actual tree processing is done by visitor
63 * classes in other packages. The abstract class Visitor, as well as
64 * an Factory interface for trees, are defined as inner classes in
65 * Tree.
66 *
67 * <p>To avoid ambiguities with the Tree API in com.sun.source all sub
68 * classes should, by convention, start with JC (javac).
69 *
70 * <p><b>This is NOT part of any supported API.
71 * If you write code that depends on this, you do so at your own risk.
72 * This code and its internal interfaces are subject to change or
73 * deletion without notice.</b>
74 *
75 * @see TreeMaker
76 * @see TreeInfo
77 * @see TreeTranslator
78 * @see Pretty
79 */
80public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
81
82    /* Tree tag values, identifying kinds of trees */
83    public enum Tag {
84        /** For methods that return an invalid tag if a given condition is not met
85         */
86        NO_TAG,
87
88        /** Toplevel nodes, of type TopLevel, representing entire source files.
89        */
90        TOPLEVEL,
91
92        /** Package level definitions.
93         */
94        PACKAGEDEF,
95
96        /** Import clauses, of type Import.
97         */
98        IMPORT,
99
100        /** Class definitions, of type ClassDef.
101         */
102        CLASSDEF,
103
104        /** Method definitions, of type MethodDef.
105         */
106        METHODDEF,
107
108        /** Variable definitions, of type VarDef.
109         */
110        VARDEF,
111
112        /** The no-op statement ";", of type Skip
113         */
114        SKIP,
115
116        /** Blocks, of type Block.
117         */
118        BLOCK,
119
120        /** Do-while loops, of type DoLoop.
121         */
122        DOLOOP,
123
124        /** While-loops, of type WhileLoop.
125         */
126        WHILELOOP,
127
128        /** For-loops, of type ForLoop.
129         */
130        FORLOOP,
131
132        /** Foreach-loops, of type ForeachLoop.
133         */
134        FOREACHLOOP,
135
136        /** Labelled statements, of type Labelled.
137         */
138        LABELLED,
139
140        /** Switch statements, of type Switch.
141         */
142        SWITCH,
143
144        /** Case parts in switch statements, of type Case.
145         */
146        CASE,
147
148        /** Synchronized statements, of type Synchonized.
149         */
150        SYNCHRONIZED,
151
152        /** Try statements, of type Try.
153         */
154        TRY,
155
156        /** Catch clauses in try statements, of type Catch.
157         */
158        CATCH,
159
160        /** Conditional expressions, of type Conditional.
161         */
162        CONDEXPR,
163
164        /** Conditional statements, of type If.
165         */
166        IF,
167
168        /** Expression statements, of type Exec.
169         */
170        EXEC,
171
172        /** Break statements, of type Break.
173         */
174        BREAK,
175
176        /** Continue statements, of type Continue.
177         */
178        CONTINUE,
179
180        /** Return statements, of type Return.
181         */
182        RETURN,
183
184        /** Throw statements, of type Throw.
185         */
186        THROW,
187
188        /** Assert statements, of type Assert.
189         */
190        ASSERT,
191
192        /** Method invocation expressions, of type Apply.
193         */
194        APPLY,
195
196        /** Class instance creation expressions, of type NewClass.
197         */
198        NEWCLASS,
199
200        /** Array creation expressions, of type NewArray.
201         */
202        NEWARRAY,
203
204        /** Lambda expression, of type Lambda.
205         */
206        LAMBDA,
207
208        /** Parenthesized subexpressions, of type Parens.
209         */
210        PARENS,
211
212        /** Assignment expressions, of type Assign.
213         */
214        ASSIGN,
215
216        /** Type cast expressions, of type TypeCast.
217         */
218        TYPECAST,
219
220        /** Type test expressions, of type TypeTest.
221         */
222        TYPETEST,
223
224        /** Indexed array expressions, of type Indexed.
225         */
226        INDEXED,
227
228        /** Selections, of type Select.
229         */
230        SELECT,
231
232        /** Member references, of type Reference.
233         */
234        REFERENCE,
235
236        /** Simple identifiers, of type Ident.
237         */
238        IDENT,
239
240        /** Literals, of type Literal.
241         */
242        LITERAL,
243
244        /** Basic type identifiers, of type TypeIdent.
245         */
246        TYPEIDENT,
247
248        /** Array types, of type TypeArray.
249         */
250        TYPEARRAY,
251
252        /** Parameterized types, of type TypeApply.
253         */
254        TYPEAPPLY,
255
256        /** Union types, of type TypeUnion.
257         */
258        TYPEUNION,
259
260        /** Intersection types, of type TypeIntersection.
261         */
262        TYPEINTERSECTION,
263
264        /** Formal type parameters, of type TypeParameter.
265         */
266        TYPEPARAMETER,
267
268        /** Type argument.
269         */
270        WILDCARD,
271
272        /** Bound kind: extends, super, exact, or unbound
273         */
274        TYPEBOUNDKIND,
275
276        /** metadata: Annotation.
277         */
278        ANNOTATION,
279
280        /** metadata: Type annotation.
281         */
282        TYPE_ANNOTATION,
283
284        /** metadata: Modifiers
285         */
286        MODIFIERS,
287
288        /** An annotated type tree.
289         */
290        ANNOTATED_TYPE,
291
292        /** Error trees, of type Erroneous.
293         */
294        ERRONEOUS,
295
296        /** Unary operators, of type Unary.
297         */
298        POS,                             // +
299        NEG,                             // -
300        NOT,                             // !
301        COMPL,                           // ~
302        PREINC,                          // ++ _
303        PREDEC,                          // -- _
304        POSTINC,                         // _ ++
305        POSTDEC,                         // _ --
306
307        /** unary operator for null reference checks, only used internally.
308         */
309        NULLCHK,
310
311        /** Binary operators, of type Binary.
312         */
313        OR,                              // ||
314        AND,                             // &&
315        BITOR,                           // |
316        BITXOR,                          // ^
317        BITAND,                          // &
318        EQ,                              // ==
319        NE,                              // !=
320        LT,                              // <
321        GT,                              // >
322        LE,                              // <=
323        GE,                              // >=
324        SL,                              // <<
325        SR,                              // >>
326        USR,                             // >>>
327        PLUS,                            // +
328        MINUS,                           // -
329        MUL,                             // *
330        DIV,                             // /
331        MOD,                             // %
332
333        /** Assignment operators, of type Assignop.
334         */
335        BITOR_ASG(BITOR),                // |=
336        BITXOR_ASG(BITXOR),              // ^=
337        BITAND_ASG(BITAND),              // &=
338
339        SL_ASG(SL),                      // <<=
340        SR_ASG(SR),                      // >>=
341        USR_ASG(USR),                    // >>>=
342        PLUS_ASG(PLUS),                  // +=
343        MINUS_ASG(MINUS),                // -=
344        MUL_ASG(MUL),                    // *=
345        DIV_ASG(DIV),                    // /=
346        MOD_ASG(MOD),                    // %=
347
348        /** A synthetic let expression, of type LetExpr.
349         */
350        LETEXPR;                         // ala scheme
351
352        private final Tag noAssignTag;
353
354        private static final int numberOfOperators = MOD.ordinal() - POS.ordinal() + 1;
355
356        private Tag(Tag noAssignTag) {
357            this.noAssignTag = noAssignTag;
358        }
359
360        private Tag() {
361            this(null);
362        }
363
364        public static int getNumberOfOperators() {
365            return numberOfOperators;
366        }
367
368        public Tag noAssignOp() {
369            if (noAssignTag != null)
370                return noAssignTag;
371            throw new AssertionError("noAssignOp() method is not available for non assignment tags");
372        }
373
374        public boolean isPostUnaryOp() {
375            return (this == POSTINC || this == POSTDEC);
376        }
377
378        public boolean isIncOrDecUnaryOp() {
379            return (this == PREINC || this == PREDEC || this == POSTINC || this == POSTDEC);
380        }
381
382        public boolean isAssignop() {
383            return noAssignTag != null;
384        }
385
386        public int operatorIndex() {
387            return (this.ordinal() - POS.ordinal());
388        }
389    }
390
391    /* The (encoded) position in the source file. @see util.Position.
392     */
393    public int pos;
394
395    /* The type of this node.
396     */
397    public Type type;
398
399    /* The tag of this node -- one of the constants declared above.
400     */
401    public abstract Tag getTag();
402
403    /* Returns true if the tag of this node is equals to tag.
404     */
405    public boolean hasTag(Tag tag) {
406        return tag == getTag();
407    }
408
409    /** Convert a tree to a pretty-printed string. */
410    @Override
411    public String toString() {
412        StringWriter s = new StringWriter();
413        try {
414            new Pretty(s, false).printExpr(this);
415        }
416        catch (IOException e) {
417            // should never happen, because StringWriter is defined
418            // never to throw any IOExceptions
419            throw new AssertionError(e);
420        }
421        return s.toString();
422    }
423
424    /** Set position field and return this tree.
425     */
426    public JCTree setPos(int pos) {
427        this.pos = pos;
428        return this;
429    }
430
431    /** Set type field and return this tree.
432     */
433    public JCTree setType(Type type) {
434        this.type = type;
435        return this;
436    }
437
438    /** Visit this tree with a given visitor.
439     */
440    public abstract void accept(Visitor v);
441
442    public abstract <R,D> R accept(TreeVisitor<R,D> v, D d);
443
444    /** Return a shallow copy of this tree.
445     */
446    @Override
447    public Object clone() {
448        try {
449            return super.clone();
450        } catch(CloneNotSupportedException e) {
451            throw new RuntimeException(e);
452        }
453    }
454
455    /** Get a default position for this tree node.
456     */
457    public DiagnosticPosition pos() {
458        return this;
459    }
460
461    // for default DiagnosticPosition
462    public JCTree getTree() {
463        return this;
464    }
465
466    // for default DiagnosticPosition
467    public int getStartPosition() {
468        return TreeInfo.getStartPos(this);
469    }
470
471    // for default DiagnosticPosition
472    public int getPreferredPosition() {
473        return pos;
474    }
475
476    // for default DiagnosticPosition
477    public int getEndPosition(EndPosTable endPosTable) {
478        return TreeInfo.getEndPos(this, endPosTable);
479    }
480
481    /**
482     * Everything in one source file is kept in a {@linkplain JCCompilationUnit} structure.
483     */
484    public static class JCCompilationUnit extends JCTree implements CompilationUnitTree {
485        /** All definitions in this file (ClassDef, Import, and Skip) */
486        public List<JCTree> defs;
487        /* The source file name. */
488        public JavaFileObject sourcefile;
489        /** The package to which this compilation unit belongs. */
490        public PackageSymbol packge;
491        /** A scope containing top level classes. */
492        public WriteableScope toplevelScope;
493        /** A scope for all named imports. */
494        public NamedImportScope namedImportScope;
495        /** A scope for all import-on-demands. */
496        public StarImportScope starImportScope;
497        /** Line starting positions, defined only if option -g is set. */
498        public Position.LineMap lineMap = null;
499        /** A table that stores all documentation comments indexed by the tree
500         * nodes they refer to. defined only if option -s is set. */
501        public DocCommentTable docComments = null;
502        /* An object encapsulating ending positions of source ranges indexed by
503         * the tree nodes they belong to. Defined only if option -Xjcov is set. */
504        public EndPosTable endPositions = null;
505        protected JCCompilationUnit(List<JCTree> defs) {
506            this.defs = defs;
507        }
508        @Override
509        public void accept(Visitor v) { v.visitTopLevel(this); }
510
511        public Kind getKind() { return Kind.COMPILATION_UNIT; }
512
513        public JCPackageDecl getPackage() {
514            // PackageDecl must be the first entry if it exists
515            if (!defs.isEmpty() && defs.head.hasTag(PACKAGEDEF))
516                return (JCPackageDecl)defs.head;
517            return null;
518        }
519        public List<JCAnnotation> getPackageAnnotations() {
520            JCPackageDecl pd = getPackage();
521            return pd != null ? pd.getAnnotations() : List.<JCAnnotation>nil();
522        }
523        public ExpressionTree getPackageName() {
524            JCPackageDecl pd = getPackage();
525            return pd != null ? pd.getPackageName() : null;
526        }
527
528        public List<JCImport> getImports() {
529            ListBuffer<JCImport> imports = new ListBuffer<>();
530            for (JCTree tree : defs) {
531                if (tree.hasTag(IMPORT))
532                    imports.append((JCImport)tree);
533                else if (!tree.hasTag(PACKAGEDEF) && !tree.hasTag(SKIP))
534                    break;
535            }
536            return imports.toList();
537        }
538        public JavaFileObject getSourceFile() {
539            return sourcefile;
540        }
541        public Position.LineMap getLineMap() {
542            return lineMap;
543        }
544        public List<JCTree> getTypeDecls() {
545            List<JCTree> typeDefs;
546            for (typeDefs = defs; !typeDefs.isEmpty(); typeDefs = typeDefs.tail)
547                if (!typeDefs.head.hasTag(PACKAGEDEF) && !typeDefs.head.hasTag(IMPORT))
548                    break;
549            return typeDefs;
550        }
551        @Override
552        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
553            return v.visitCompilationUnit(this, d);
554        }
555
556        @Override
557        public Tag getTag() {
558            return TOPLEVEL;
559        }
560    }
561
562    /**
563     * Package definition.
564     */
565    public static class JCPackageDecl extends JCTree implements PackageTree {
566        public List<JCAnnotation> annotations;
567        /** The tree representing the package clause. */
568        public JCExpression pid;
569        public PackageSymbol packge;
570        public JCPackageDecl(List<JCAnnotation> annotations, JCExpression pid) {
571            this.annotations = annotations;
572            this.pid = pid;
573        }
574        @Override
575        public void accept(Visitor v) { v.visitPackageDef(this); }
576        public Kind getKind() {
577            return Kind.PACKAGE;
578        }
579        public List<JCAnnotation> getAnnotations() {
580            return annotations;
581        }
582        public JCExpression getPackageName() {
583            return pid;
584        }
585        @Override
586        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
587            return v.visitPackage(this, d);
588        }
589        @Override
590        public Tag getTag() {
591            return PACKAGEDEF;
592        }
593    }
594
595    /**
596     * An import clause.
597     */
598    public static class JCImport extends JCTree implements ImportTree {
599        public boolean staticImport;
600        /** The imported class(es). */
601        public JCTree qualid;
602        protected JCImport(JCTree qualid, boolean importStatic) {
603            this.qualid = qualid;
604            this.staticImport = importStatic;
605        }
606        @Override
607        public void accept(Visitor v) { v.visitImport(this); }
608
609        public boolean isStatic() { return staticImport; }
610        public JCTree getQualifiedIdentifier() { return qualid; }
611
612        public Kind getKind() { return Kind.IMPORT; }
613        @Override
614        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
615            return v.visitImport(this, d);
616        }
617
618        @Override
619        public Tag getTag() {
620            return IMPORT;
621        }
622    }
623
624    public static abstract class JCStatement extends JCTree implements StatementTree {
625        @Override
626        public JCStatement setType(Type type) {
627            super.setType(type);
628            return this;
629        }
630        @Override
631        public JCStatement setPos(int pos) {
632            super.setPos(pos);
633            return this;
634        }
635    }
636
637    public static abstract class JCExpression extends JCTree implements ExpressionTree {
638        @Override
639        public JCExpression setType(Type type) {
640            super.setType(type);
641            return this;
642        }
643        @Override
644        public JCExpression setPos(int pos) {
645            super.setPos(pos);
646            return this;
647        }
648
649        public boolean isPoly() { return false; }
650        public boolean isStandalone() { return true; }
651    }
652
653    /**
654     * Common supertype for all poly expression trees (lambda, method references,
655     * conditionals, method and constructor calls)
656     */
657    public static abstract class JCPolyExpression extends JCExpression {
658
659        /**
660         * A poly expression can only be truly 'poly' in certain contexts
661         */
662        public enum PolyKind {
663            /** poly expression to be treated as a standalone expression */
664            STANDALONE,
665            /** true poly expression */
666            POLY
667        }
668
669        /** is this poly expression a 'true' poly expression? */
670        public PolyKind polyKind;
671
672        @Override public boolean isPoly() { return polyKind == PolyKind.POLY; }
673        @Override public boolean isStandalone() { return polyKind == PolyKind.STANDALONE; }
674    }
675
676    /**
677     * Common supertype for all functional expression trees (lambda and method references)
678     */
679    public static abstract class JCFunctionalExpression extends JCPolyExpression {
680
681        public JCFunctionalExpression() {
682            //a functional expression is always a 'true' poly
683            polyKind = PolyKind.POLY;
684        }
685
686        /** list of target types inferred for this functional expression. */
687        public List<Type> targets;
688
689        public Type getDescriptorType(Types types) {
690            return targets.nonEmpty() ? types.findDescriptorType(targets.head) : types.createErrorType(null);
691        }
692    }
693
694    /**
695     * A class definition.
696     */
697    public static class JCClassDecl extends JCStatement implements ClassTree {
698        /** the modifiers */
699        public JCModifiers mods;
700        /** the name of the class */
701        public Name name;
702        /** formal class parameters */
703        public List<JCTypeParameter> typarams;
704        /** the classes this class extends */
705        public JCExpression extending;
706        /** the interfaces implemented by this class */
707        public List<JCExpression> implementing;
708        /** all variables and methods defined in this class */
709        public List<JCTree> defs;
710        /** the symbol */
711        public ClassSymbol sym;
712        protected JCClassDecl(JCModifiers mods,
713                           Name name,
714                           List<JCTypeParameter> typarams,
715                           JCExpression extending,
716                           List<JCExpression> implementing,
717                           List<JCTree> defs,
718                           ClassSymbol sym)
719        {
720            this.mods = mods;
721            this.name = name;
722            this.typarams = typarams;
723            this.extending = extending;
724            this.implementing = implementing;
725            this.defs = defs;
726            this.sym = sym;
727        }
728        @Override
729        public void accept(Visitor v) { v.visitClassDef(this); }
730
731        public Kind getKind() {
732            if ((mods.flags & Flags.ANNOTATION) != 0)
733                return Kind.ANNOTATION_TYPE;
734            else if ((mods.flags & Flags.INTERFACE) != 0)
735                return Kind.INTERFACE;
736            else if ((mods.flags & Flags.ENUM) != 0)
737                return Kind.ENUM;
738            else
739                return Kind.CLASS;
740        }
741
742        public JCModifiers getModifiers() { return mods; }
743        public Name getSimpleName() { return name; }
744        public List<JCTypeParameter> getTypeParameters() {
745            return typarams;
746        }
747        public JCExpression getExtendsClause() { return extending; }
748        public List<JCExpression> getImplementsClause() {
749            return implementing;
750        }
751        public List<JCTree> getMembers() {
752            return defs;
753        }
754        @Override
755        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
756            return v.visitClass(this, d);
757        }
758
759        @Override
760        public Tag getTag() {
761            return CLASSDEF;
762        }
763    }
764
765    /**
766     * A method definition.
767     */
768    public static class JCMethodDecl extends JCTree implements MethodTree {
769        /** method modifiers */
770        public JCModifiers mods;
771        /** method name */
772        public Name name;
773        /** type of method return value */
774        public JCExpression restype;
775        /** type parameters */
776        public List<JCTypeParameter> typarams;
777        /** receiver parameter */
778        public JCVariableDecl recvparam;
779        /** value parameters */
780        public List<JCVariableDecl> params;
781        /** exceptions thrown by this method */
782        public List<JCExpression> thrown;
783        /** statements in the method */
784        public JCBlock body;
785        /** default value, for annotation types */
786        public JCExpression defaultValue;
787        /** method symbol */
788        public MethodSymbol sym;
789        protected JCMethodDecl(JCModifiers mods,
790                            Name name,
791                            JCExpression restype,
792                            List<JCTypeParameter> typarams,
793                            JCVariableDecl recvparam,
794                            List<JCVariableDecl> params,
795                            List<JCExpression> thrown,
796                            JCBlock body,
797                            JCExpression defaultValue,
798                            MethodSymbol sym)
799        {
800            this.mods = mods;
801            this.name = name;
802            this.restype = restype;
803            this.typarams = typarams;
804            this.params = params;
805            this.recvparam = recvparam;
806            // TODO: do something special if the given type is null?
807            // receiver != null ? receiver : List.<JCTypeAnnotation>nil());
808            this.thrown = thrown;
809            this.body = body;
810            this.defaultValue = defaultValue;
811            this.sym = sym;
812        }
813        @Override
814        public void accept(Visitor v) { v.visitMethodDef(this); }
815
816        public Kind getKind() { return Kind.METHOD; }
817        public JCModifiers getModifiers() { return mods; }
818        public Name getName() { return name; }
819        public JCTree getReturnType() { return restype; }
820        public List<JCTypeParameter> getTypeParameters() {
821            return typarams;
822        }
823        public List<JCVariableDecl> getParameters() {
824            return params;
825        }
826        public JCVariableDecl getReceiverParameter() { return recvparam; }
827        public List<JCExpression> getThrows() {
828            return thrown;
829        }
830        public JCBlock getBody() { return body; }
831        public JCTree getDefaultValue() { // for annotation types
832            return defaultValue;
833        }
834        @Override
835        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
836            return v.visitMethod(this, d);
837        }
838
839        @Override
840        public Tag getTag() {
841            return METHODDEF;
842        }
843  }
844
845    /**
846     * A variable definition.
847     */
848    public static class JCVariableDecl extends JCStatement implements VariableTree {
849        /** variable modifiers */
850        public JCModifiers mods;
851        /** variable name */
852        public Name name;
853        /** variable name expression */
854        public JCExpression nameexpr;
855        /** type of the variable */
856        public JCExpression vartype;
857        /** variable's initial value */
858        public JCExpression init;
859        /** symbol */
860        public VarSymbol sym;
861
862        protected JCVariableDecl(JCModifiers mods,
863                         Name name,
864                         JCExpression vartype,
865                         JCExpression init,
866                         VarSymbol sym) {
867            this.mods = mods;
868            this.name = name;
869            this.vartype = vartype;
870            this.init = init;
871            this.sym = sym;
872        }
873
874        protected JCVariableDecl(JCModifiers mods,
875                         JCExpression nameexpr,
876                         JCExpression vartype) {
877            this(mods, null, vartype, null, null);
878            this.nameexpr = nameexpr;
879            if (nameexpr.hasTag(Tag.IDENT)) {
880                this.name = ((JCIdent)nameexpr).name;
881            } else {
882                // Only other option is qualified name x.y.this;
883                this.name = ((JCFieldAccess)nameexpr).name;
884            }
885        }
886
887        @Override
888        public void accept(Visitor v) { v.visitVarDef(this); }
889
890        public Kind getKind() { return Kind.VARIABLE; }
891        public JCModifiers getModifiers() { return mods; }
892        public Name getName() { return name; }
893        public JCExpression getNameExpression() { return nameexpr; }
894        public JCTree getType() { return vartype; }
895        public JCExpression getInitializer() {
896            return init;
897        }
898        @Override
899        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
900            return v.visitVariable(this, d);
901        }
902
903        @Override
904        public Tag getTag() {
905            return VARDEF;
906        }
907    }
908
909    /**
910     * A no-op statement ";".
911     */
912    public static class JCSkip extends JCStatement implements EmptyStatementTree {
913        protected JCSkip() {
914        }
915        @Override
916        public void accept(Visitor v) { v.visitSkip(this); }
917
918        public Kind getKind() { return Kind.EMPTY_STATEMENT; }
919        @Override
920        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
921            return v.visitEmptyStatement(this, d);
922        }
923
924        @Override
925        public Tag getTag() {
926            return SKIP;
927        }
928    }
929
930    /**
931     * A statement block.
932     */
933    public static class JCBlock extends JCStatement implements BlockTree {
934        /** flags */
935        public long flags;
936        /** statements */
937        public List<JCStatement> stats;
938        /** Position of closing brace, optional. */
939        public int endpos = Position.NOPOS;
940        protected JCBlock(long flags, List<JCStatement> stats) {
941            this.stats = stats;
942            this.flags = flags;
943        }
944        @Override
945        public void accept(Visitor v) { v.visitBlock(this); }
946
947        public Kind getKind() { return Kind.BLOCK; }
948        public List<JCStatement> getStatements() {
949            return stats;
950        }
951        public boolean isStatic() { return (flags & Flags.STATIC) != 0; }
952        @Override
953        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
954            return v.visitBlock(this, d);
955        }
956
957        @Override
958        public Tag getTag() {
959            return BLOCK;
960        }
961    }
962
963    /**
964     * A do loop
965     */
966    public static class JCDoWhileLoop extends JCStatement implements DoWhileLoopTree {
967        public JCStatement body;
968        public JCExpression cond;
969        protected JCDoWhileLoop(JCStatement body, JCExpression cond) {
970            this.body = body;
971            this.cond = cond;
972        }
973        @Override
974        public void accept(Visitor v) { v.visitDoLoop(this); }
975
976        public Kind getKind() { return Kind.DO_WHILE_LOOP; }
977        public JCExpression getCondition() { return cond; }
978        public JCStatement getStatement() { return body; }
979        @Override
980        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
981            return v.visitDoWhileLoop(this, d);
982        }
983
984        @Override
985        public Tag getTag() {
986            return DOLOOP;
987        }
988    }
989
990    /**
991     * A while loop
992     */
993    public static class JCWhileLoop extends JCStatement implements WhileLoopTree {
994        public JCExpression cond;
995        public JCStatement body;
996        protected JCWhileLoop(JCExpression cond, JCStatement body) {
997            this.cond = cond;
998            this.body = body;
999        }
1000        @Override
1001        public void accept(Visitor v) { v.visitWhileLoop(this); }
1002
1003        public Kind getKind() { return Kind.WHILE_LOOP; }
1004        public JCExpression getCondition() { return cond; }
1005        public JCStatement getStatement() { return body; }
1006        @Override
1007        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1008            return v.visitWhileLoop(this, d);
1009        }
1010
1011        @Override
1012        public Tag getTag() {
1013            return WHILELOOP;
1014        }
1015    }
1016
1017    /**
1018     * A for loop.
1019     */
1020    public static class JCForLoop extends JCStatement implements ForLoopTree {
1021        public List<JCStatement> init;
1022        public JCExpression cond;
1023        public List<JCExpressionStatement> step;
1024        public JCStatement body;
1025        protected JCForLoop(List<JCStatement> init,
1026                          JCExpression cond,
1027                          List<JCExpressionStatement> update,
1028                          JCStatement body)
1029        {
1030            this.init = init;
1031            this.cond = cond;
1032            this.step = update;
1033            this.body = body;
1034        }
1035        @Override
1036        public void accept(Visitor v) { v.visitForLoop(this); }
1037
1038        public Kind getKind() { return Kind.FOR_LOOP; }
1039        public JCExpression getCondition() { return cond; }
1040        public JCStatement getStatement() { return body; }
1041        public List<JCStatement> getInitializer() {
1042            return init;
1043        }
1044        public List<JCExpressionStatement> getUpdate() {
1045            return step;
1046        }
1047        @Override
1048        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1049            return v.visitForLoop(this, d);
1050        }
1051
1052        @Override
1053        public Tag getTag() {
1054            return FORLOOP;
1055        }
1056    }
1057
1058    /**
1059     * The enhanced for loop.
1060     */
1061    public static class JCEnhancedForLoop extends JCStatement implements EnhancedForLoopTree {
1062        public JCVariableDecl var;
1063        public JCExpression expr;
1064        public JCStatement body;
1065        protected JCEnhancedForLoop(JCVariableDecl var, JCExpression expr, JCStatement body) {
1066            this.var = var;
1067            this.expr = expr;
1068            this.body = body;
1069        }
1070        @Override
1071        public void accept(Visitor v) { v.visitForeachLoop(this); }
1072
1073        public Kind getKind() { return Kind.ENHANCED_FOR_LOOP; }
1074        public JCVariableDecl getVariable() { return var; }
1075        public JCExpression getExpression() { return expr; }
1076        public JCStatement getStatement() { return body; }
1077        @Override
1078        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1079            return v.visitEnhancedForLoop(this, d);
1080        }
1081        @Override
1082        public Tag getTag() {
1083            return FOREACHLOOP;
1084        }
1085    }
1086
1087    /**
1088     * A labelled expression or statement.
1089     */
1090    public static class JCLabeledStatement extends JCStatement implements LabeledStatementTree {
1091        public Name label;
1092        public JCStatement body;
1093        protected JCLabeledStatement(Name label, JCStatement body) {
1094            this.label = label;
1095            this.body = body;
1096        }
1097        @Override
1098        public void accept(Visitor v) { v.visitLabelled(this); }
1099        public Kind getKind() { return Kind.LABELED_STATEMENT; }
1100        public Name getLabel() { return label; }
1101        public JCStatement getStatement() { return body; }
1102        @Override
1103        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1104            return v.visitLabeledStatement(this, d);
1105        }
1106        @Override
1107        public Tag getTag() {
1108            return LABELLED;
1109        }
1110    }
1111
1112    /**
1113     * A "switch ( ) { }" construction.
1114     */
1115    public static class JCSwitch extends JCStatement implements SwitchTree {
1116        public JCExpression selector;
1117        public List<JCCase> cases;
1118        protected JCSwitch(JCExpression selector, List<JCCase> cases) {
1119            this.selector = selector;
1120            this.cases = cases;
1121        }
1122        @Override
1123        public void accept(Visitor v) { v.visitSwitch(this); }
1124
1125        public Kind getKind() { return Kind.SWITCH; }
1126        public JCExpression getExpression() { return selector; }
1127        public List<JCCase> getCases() { return cases; }
1128        @Override
1129        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1130            return v.visitSwitch(this, d);
1131        }
1132        @Override
1133        public Tag getTag() {
1134            return SWITCH;
1135        }
1136    }
1137
1138    /**
1139     * A "case  :" of a switch.
1140     */
1141    public static class JCCase extends JCStatement implements CaseTree {
1142        public JCExpression pat;
1143        public List<JCStatement> stats;
1144        protected JCCase(JCExpression pat, List<JCStatement> stats) {
1145            this.pat = pat;
1146            this.stats = stats;
1147        }
1148        @Override
1149        public void accept(Visitor v) { v.visitCase(this); }
1150
1151        public Kind getKind() { return Kind.CASE; }
1152        public JCExpression getExpression() { return pat; }
1153        public List<JCStatement> getStatements() { return stats; }
1154        @Override
1155        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1156            return v.visitCase(this, d);
1157        }
1158        @Override
1159        public Tag getTag() {
1160            return CASE;
1161        }
1162    }
1163
1164    /**
1165     * A synchronized block.
1166     */
1167    public static class JCSynchronized extends JCStatement implements SynchronizedTree {
1168        public JCExpression lock;
1169        public JCBlock body;
1170        protected JCSynchronized(JCExpression lock, JCBlock body) {
1171            this.lock = lock;
1172            this.body = body;
1173        }
1174        @Override
1175        public void accept(Visitor v) { v.visitSynchronized(this); }
1176
1177        public Kind getKind() { return Kind.SYNCHRONIZED; }
1178        public JCExpression getExpression() { return lock; }
1179        public JCBlock getBlock() { return body; }
1180        @Override
1181        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1182            return v.visitSynchronized(this, d);
1183        }
1184        @Override
1185        public Tag getTag() {
1186            return SYNCHRONIZED;
1187        }
1188    }
1189
1190    /**
1191     * A "try { } catch ( ) { } finally { }" block.
1192     */
1193    public static class JCTry extends JCStatement implements TryTree {
1194        public JCBlock body;
1195        public List<JCCatch> catchers;
1196        public JCBlock finalizer;
1197        public List<JCTree> resources;
1198        public boolean finallyCanCompleteNormally;
1199        protected JCTry(List<JCTree> resources,
1200                        JCBlock body,
1201                        List<JCCatch> catchers,
1202                        JCBlock finalizer) {
1203            this.body = body;
1204            this.catchers = catchers;
1205            this.finalizer = finalizer;
1206            this.resources = resources;
1207        }
1208        @Override
1209        public void accept(Visitor v) { v.visitTry(this); }
1210
1211        public Kind getKind() { return Kind.TRY; }
1212        public JCBlock getBlock() { return body; }
1213        public List<JCCatch> getCatches() {
1214            return catchers;
1215        }
1216        public JCBlock getFinallyBlock() { return finalizer; }
1217        @Override
1218        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1219            return v.visitTry(this, d);
1220        }
1221        @Override
1222        public List<JCTree> getResources() {
1223            return resources;
1224        }
1225        @Override
1226        public Tag getTag() {
1227            return TRY;
1228        }
1229    }
1230
1231    /**
1232     * A catch block.
1233     */
1234    public static class JCCatch extends JCTree implements CatchTree {
1235        public JCVariableDecl param;
1236        public JCBlock body;
1237        protected JCCatch(JCVariableDecl param, JCBlock body) {
1238            this.param = param;
1239            this.body = body;
1240        }
1241        @Override
1242        public void accept(Visitor v) { v.visitCatch(this); }
1243
1244        public Kind getKind() { return Kind.CATCH; }
1245        public JCVariableDecl getParameter() { return param; }
1246        public JCBlock getBlock() { return body; }
1247        @Override
1248        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1249            return v.visitCatch(this, d);
1250        }
1251        @Override
1252        public Tag getTag() {
1253            return CATCH;
1254        }
1255    }
1256
1257    /**
1258     * A ( ) ? ( ) : ( ) conditional expression
1259     */
1260    public static class JCConditional extends JCPolyExpression implements ConditionalExpressionTree {
1261        public JCExpression cond;
1262        public JCExpression truepart;
1263        public JCExpression falsepart;
1264        protected JCConditional(JCExpression cond,
1265                              JCExpression truepart,
1266                              JCExpression falsepart)
1267        {
1268            this.cond = cond;
1269            this.truepart = truepart;
1270            this.falsepart = falsepart;
1271        }
1272        @Override
1273        public void accept(Visitor v) { v.visitConditional(this); }
1274
1275        public Kind getKind() { return Kind.CONDITIONAL_EXPRESSION; }
1276        public JCExpression getCondition() { return cond; }
1277        public JCExpression getTrueExpression() { return truepart; }
1278        public JCExpression getFalseExpression() { return falsepart; }
1279        @Override
1280        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1281            return v.visitConditionalExpression(this, d);
1282        }
1283        @Override
1284        public Tag getTag() {
1285            return CONDEXPR;
1286        }
1287    }
1288
1289    /**
1290     * An "if ( ) { } else { }" block
1291     */
1292    public static class JCIf extends JCStatement implements IfTree {
1293        public JCExpression cond;
1294        public JCStatement thenpart;
1295        public JCStatement elsepart;
1296        protected JCIf(JCExpression cond,
1297                     JCStatement thenpart,
1298                     JCStatement elsepart)
1299        {
1300            this.cond = cond;
1301            this.thenpart = thenpart;
1302            this.elsepart = elsepart;
1303        }
1304        @Override
1305        public void accept(Visitor v) { v.visitIf(this); }
1306
1307        public Kind getKind() { return Kind.IF; }
1308        public JCExpression getCondition() { return cond; }
1309        public JCStatement getThenStatement() { return thenpart; }
1310        public JCStatement getElseStatement() { return elsepart; }
1311        @Override
1312        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1313            return v.visitIf(this, d);
1314        }
1315        @Override
1316        public Tag getTag() {
1317            return IF;
1318        }
1319    }
1320
1321    /**
1322     * an expression statement
1323     */
1324    public static class JCExpressionStatement extends JCStatement implements ExpressionStatementTree {
1325        /** expression structure */
1326        public JCExpression expr;
1327        protected JCExpressionStatement(JCExpression expr)
1328        {
1329            this.expr = expr;
1330        }
1331        @Override
1332        public void accept(Visitor v) { v.visitExec(this); }
1333
1334        public Kind getKind() { return Kind.EXPRESSION_STATEMENT; }
1335        public JCExpression getExpression() { return expr; }
1336        @Override
1337        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1338            return v.visitExpressionStatement(this, d);
1339        }
1340        @Override
1341        public Tag getTag() {
1342            return EXEC;
1343        }
1344
1345        /** Convert a expression-statement tree to a pretty-printed string. */
1346        @Override
1347        public String toString() {
1348            StringWriter s = new StringWriter();
1349            try {
1350                new Pretty(s, false).printStat(this);
1351            }
1352            catch (IOException e) {
1353                // should never happen, because StringWriter is defined
1354                // never to throw any IOExceptions
1355                throw new AssertionError(e);
1356            }
1357            return s.toString();
1358        }
1359    }
1360
1361    /**
1362     * A break from a loop or switch.
1363     */
1364    public static class JCBreak extends JCStatement implements BreakTree {
1365        public Name label;
1366        public JCTree target;
1367        protected JCBreak(Name label, JCTree target) {
1368            this.label = label;
1369            this.target = target;
1370        }
1371        @Override
1372        public void accept(Visitor v) { v.visitBreak(this); }
1373
1374        public Kind getKind() { return Kind.BREAK; }
1375        public Name getLabel() { return label; }
1376        @Override
1377        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1378            return v.visitBreak(this, d);
1379        }
1380        @Override
1381        public Tag getTag() {
1382            return BREAK;
1383        }
1384    }
1385
1386    /**
1387     * A continue of a loop.
1388     */
1389    public static class JCContinue extends JCStatement implements ContinueTree {
1390        public Name label;
1391        public JCTree target;
1392        protected JCContinue(Name label, JCTree target) {
1393            this.label = label;
1394            this.target = target;
1395        }
1396        @Override
1397        public void accept(Visitor v) { v.visitContinue(this); }
1398
1399        public Kind getKind() { return Kind.CONTINUE; }
1400        public Name getLabel() { return label; }
1401        @Override
1402        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1403            return v.visitContinue(this, d);
1404        }
1405        @Override
1406        public Tag getTag() {
1407            return CONTINUE;
1408        }
1409    }
1410
1411    /**
1412     * A return statement.
1413     */
1414    public static class JCReturn extends JCStatement implements ReturnTree {
1415        public JCExpression expr;
1416        protected JCReturn(JCExpression expr) {
1417            this.expr = expr;
1418        }
1419        @Override
1420        public void accept(Visitor v) { v.visitReturn(this); }
1421
1422        public Kind getKind() { return Kind.RETURN; }
1423        public JCExpression getExpression() { return expr; }
1424        @Override
1425        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1426            return v.visitReturn(this, d);
1427        }
1428        @Override
1429        public Tag getTag() {
1430            return RETURN;
1431        }
1432    }
1433
1434    /**
1435     * A throw statement.
1436     */
1437    public static class JCThrow extends JCStatement implements ThrowTree {
1438        public JCExpression expr;
1439        protected JCThrow(JCExpression expr) {
1440            this.expr = expr;
1441        }
1442        @Override
1443        public void accept(Visitor v) { v.visitThrow(this); }
1444
1445        public Kind getKind() { return Kind.THROW; }
1446        public JCExpression getExpression() { return expr; }
1447        @Override
1448        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1449            return v.visitThrow(this, d);
1450        }
1451        @Override
1452        public Tag getTag() {
1453            return THROW;
1454        }
1455    }
1456
1457    /**
1458     * An assert statement.
1459     */
1460    public static class JCAssert extends JCStatement implements AssertTree {
1461        public JCExpression cond;
1462        public JCExpression detail;
1463        protected JCAssert(JCExpression cond, JCExpression detail) {
1464            this.cond = cond;
1465            this.detail = detail;
1466        }
1467        @Override
1468        public void accept(Visitor v) { v.visitAssert(this); }
1469
1470        public Kind getKind() { return Kind.ASSERT; }
1471        public JCExpression getCondition() { return cond; }
1472        public JCExpression getDetail() { return detail; }
1473        @Override
1474        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1475            return v.visitAssert(this, d);
1476        }
1477        @Override
1478        public Tag getTag() {
1479            return ASSERT;
1480        }
1481    }
1482
1483    /**
1484     * A method invocation
1485     */
1486    public static class JCMethodInvocation extends JCPolyExpression implements MethodInvocationTree {
1487        public List<JCExpression> typeargs;
1488        public JCExpression meth;
1489        public List<JCExpression> args;
1490        public Type varargsElement;
1491        protected JCMethodInvocation(List<JCExpression> typeargs,
1492                        JCExpression meth,
1493                        List<JCExpression> args)
1494        {
1495            this.typeargs = (typeargs == null) ? List.<JCExpression>nil()
1496                                               : typeargs;
1497            this.meth = meth;
1498            this.args = args;
1499        }
1500        @Override
1501        public void accept(Visitor v) { v.visitApply(this); }
1502
1503        public Kind getKind() { return Kind.METHOD_INVOCATION; }
1504        public List<JCExpression> getTypeArguments() {
1505            return typeargs;
1506        }
1507        public JCExpression getMethodSelect() { return meth; }
1508        public List<JCExpression> getArguments() {
1509            return args;
1510        }
1511        @Override
1512        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1513            return v.visitMethodInvocation(this, d);
1514        }
1515        @Override
1516        public JCMethodInvocation setType(Type type) {
1517            super.setType(type);
1518            return this;
1519        }
1520        @Override
1521        public Tag getTag() {
1522            return(APPLY);
1523        }
1524    }
1525
1526    /**
1527     * A new(...) operation.
1528     */
1529    public static class JCNewClass extends JCPolyExpression implements NewClassTree {
1530        public JCExpression encl;
1531        public List<JCExpression> typeargs;
1532        public JCExpression clazz;
1533        public List<JCExpression> args;
1534        public JCClassDecl def;
1535        public Symbol constructor;
1536        public Type varargsElement;
1537        public Type constructorType;
1538        protected JCNewClass(JCExpression encl,
1539                           List<JCExpression> typeargs,
1540                           JCExpression clazz,
1541                           List<JCExpression> args,
1542                           JCClassDecl def)
1543        {
1544            this.encl = encl;
1545            this.typeargs = (typeargs == null) ? List.<JCExpression>nil()
1546                                               : typeargs;
1547            this.clazz = clazz;
1548            this.args = args;
1549            this.def = def;
1550        }
1551        @Override
1552        public void accept(Visitor v) { v.visitNewClass(this); }
1553
1554        public Kind getKind() { return Kind.NEW_CLASS; }
1555        public JCExpression getEnclosingExpression() { // expr.new C< ... > ( ... )
1556            return encl;
1557        }
1558        public List<JCExpression> getTypeArguments() {
1559            return typeargs;
1560        }
1561        public JCExpression getIdentifier() { return clazz; }
1562        public List<JCExpression> getArguments() {
1563            return args;
1564        }
1565        public JCClassDecl getClassBody() { return def; }
1566        @Override
1567        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1568            return v.visitNewClass(this, d);
1569        }
1570        @Override
1571        public Tag getTag() {
1572            return NEWCLASS;
1573        }
1574    }
1575
1576    /**
1577     * A new[...] operation.
1578     */
1579    public static class JCNewArray extends JCExpression implements NewArrayTree {
1580        public JCExpression elemtype;
1581        public List<JCExpression> dims;
1582        // type annotations on inner-most component
1583        public List<JCAnnotation> annotations;
1584        // type annotations on dimensions
1585        public List<List<JCAnnotation>> dimAnnotations;
1586        public List<JCExpression> elems;
1587        protected JCNewArray(JCExpression elemtype,
1588                           List<JCExpression> dims,
1589                           List<JCExpression> elems)
1590        {
1591            this.elemtype = elemtype;
1592            this.dims = dims;
1593            this.annotations = List.nil();
1594            this.dimAnnotations = List.nil();
1595            this.elems = elems;
1596        }
1597        @Override
1598        public void accept(Visitor v) { v.visitNewArray(this); }
1599
1600        public Kind getKind() { return Kind.NEW_ARRAY; }
1601        public JCExpression getType() { return elemtype; }
1602        public List<JCExpression> getDimensions() {
1603            return dims;
1604        }
1605        public List<JCExpression> getInitializers() {
1606            return elems;
1607        }
1608        @Override
1609        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1610            return v.visitNewArray(this, d);
1611        }
1612        @Override
1613        public Tag getTag() {
1614            return NEWARRAY;
1615        }
1616
1617        @Override
1618        public List<JCAnnotation> getAnnotations() {
1619            return annotations;
1620        }
1621
1622        @Override
1623        public List<List<JCAnnotation>> getDimAnnotations() {
1624            return dimAnnotations;
1625        }
1626    }
1627
1628    /**
1629     * A lambda expression.
1630     */
1631    public static class JCLambda extends JCFunctionalExpression implements LambdaExpressionTree {
1632
1633        public enum ParameterKind {
1634            IMPLICIT,
1635            EXPLICIT
1636        }
1637
1638        public List<JCVariableDecl> params;
1639        public JCTree body;
1640        public boolean canCompleteNormally = true;
1641        public ParameterKind paramKind;
1642
1643        public JCLambda(List<JCVariableDecl> params,
1644                        JCTree body) {
1645            this.params = params;
1646            this.body = body;
1647            if (params.isEmpty() ||
1648                params.head.vartype != null) {
1649                paramKind = ParameterKind.EXPLICIT;
1650            } else {
1651                paramKind = ParameterKind.IMPLICIT;
1652            }
1653        }
1654        @Override
1655        public Tag getTag() {
1656            return LAMBDA;
1657        }
1658        @Override
1659        public void accept(Visitor v) {
1660            v.visitLambda(this);
1661        }
1662        @Override
1663        public <R, D> R accept(TreeVisitor<R, D> v, D d) {
1664            return v.visitLambdaExpression(this, d);
1665        }
1666        public Kind getKind() {
1667            return Kind.LAMBDA_EXPRESSION;
1668        }
1669        public JCTree getBody() {
1670            return body;
1671        }
1672        public java.util.List<? extends VariableTree> getParameters() {
1673            return params;
1674        }
1675        @Override
1676        public JCLambda setType(Type type) {
1677            super.setType(type);
1678            return this;
1679        }
1680        @Override
1681        public BodyKind getBodyKind() {
1682            return body.hasTag(BLOCK) ?
1683                    BodyKind.STATEMENT :
1684                    BodyKind.EXPRESSION;
1685        }
1686    }
1687
1688    /**
1689     * A parenthesized subexpression ( ... )
1690     */
1691    public static class JCParens extends JCExpression implements ParenthesizedTree {
1692        public JCExpression expr;
1693        protected JCParens(JCExpression expr) {
1694            this.expr = expr;
1695        }
1696        @Override
1697        public void accept(Visitor v) { v.visitParens(this); }
1698
1699        public Kind getKind() { return Kind.PARENTHESIZED; }
1700        public JCExpression getExpression() { return expr; }
1701        @Override
1702        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1703            return v.visitParenthesized(this, d);
1704        }
1705        @Override
1706        public Tag getTag() {
1707            return PARENS;
1708        }
1709    }
1710
1711    /**
1712     * A assignment with "=".
1713     */
1714    public static class JCAssign extends JCExpression implements AssignmentTree {
1715        public JCExpression lhs;
1716        public JCExpression rhs;
1717        protected JCAssign(JCExpression lhs, JCExpression rhs) {
1718            this.lhs = lhs;
1719            this.rhs = rhs;
1720        }
1721        @Override
1722        public void accept(Visitor v) { v.visitAssign(this); }
1723
1724        public Kind getKind() { return Kind.ASSIGNMENT; }
1725        public JCExpression getVariable() { return lhs; }
1726        public JCExpression getExpression() { return rhs; }
1727        @Override
1728        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1729            return v.visitAssignment(this, d);
1730        }
1731        @Override
1732        public Tag getTag() {
1733            return ASSIGN;
1734        }
1735    }
1736
1737    /**
1738     * An assignment with "+=", "|=" ...
1739     */
1740    public static class JCAssignOp extends JCExpression implements CompoundAssignmentTree {
1741        private Tag opcode;
1742        public JCExpression lhs;
1743        public JCExpression rhs;
1744        public Symbol operator;
1745        protected JCAssignOp(Tag opcode, JCTree lhs, JCTree rhs, Symbol operator) {
1746            this.opcode = opcode;
1747            this.lhs = (JCExpression)lhs;
1748            this.rhs = (JCExpression)rhs;
1749            this.operator = operator;
1750        }
1751        @Override
1752        public void accept(Visitor v) { v.visitAssignop(this); }
1753
1754        public Kind getKind() { return TreeInfo.tagToKind(getTag()); }
1755        public JCExpression getVariable() { return lhs; }
1756        public JCExpression getExpression() { return rhs; }
1757        public Symbol getOperator() {
1758            return operator;
1759        }
1760        @Override
1761        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1762            return v.visitCompoundAssignment(this, d);
1763        }
1764        @Override
1765        public Tag getTag() {
1766            return opcode;
1767        }
1768    }
1769
1770    /**
1771     * A unary operation.
1772     */
1773    public static class JCUnary extends JCExpression implements UnaryTree {
1774        private Tag opcode;
1775        public JCExpression arg;
1776        public Symbol operator;
1777        protected JCUnary(Tag opcode, JCExpression arg) {
1778            this.opcode = opcode;
1779            this.arg = arg;
1780        }
1781        @Override
1782        public void accept(Visitor v) { v.visitUnary(this); }
1783
1784        public Kind getKind() { return TreeInfo.tagToKind(getTag()); }
1785        public JCExpression getExpression() { return arg; }
1786        public Symbol getOperator() {
1787            return operator;
1788        }
1789        @Override
1790        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1791            return v.visitUnary(this, d);
1792        }
1793        @Override
1794        public Tag getTag() {
1795            return opcode;
1796        }
1797
1798        public void setTag(Tag tag) {
1799            opcode = tag;
1800        }
1801    }
1802
1803    /**
1804     * A binary operation.
1805     */
1806    public static class JCBinary extends JCExpression implements BinaryTree {
1807        private Tag opcode;
1808        public JCExpression lhs;
1809        public JCExpression rhs;
1810        public Symbol operator;
1811        protected JCBinary(Tag opcode,
1812                         JCExpression lhs,
1813                         JCExpression rhs,
1814                         Symbol operator) {
1815            this.opcode = opcode;
1816            this.lhs = lhs;
1817            this.rhs = rhs;
1818            this.operator = operator;
1819        }
1820        @Override
1821        public void accept(Visitor v) { v.visitBinary(this); }
1822
1823        public Kind getKind() { return TreeInfo.tagToKind(getTag()); }
1824        public JCExpression getLeftOperand() { return lhs; }
1825        public JCExpression getRightOperand() { return rhs; }
1826        public Symbol getOperator() {
1827            return operator;
1828        }
1829        @Override
1830        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1831            return v.visitBinary(this, d);
1832        }
1833        @Override
1834        public Tag getTag() {
1835            return opcode;
1836        }
1837    }
1838
1839    /**
1840     * A type cast.
1841     */
1842    public static class JCTypeCast extends JCExpression implements TypeCastTree {
1843        public JCTree clazz;
1844        public JCExpression expr;
1845        protected JCTypeCast(JCTree clazz, JCExpression expr) {
1846            this.clazz = clazz;
1847            this.expr = expr;
1848        }
1849        @Override
1850        public void accept(Visitor v) { v.visitTypeCast(this); }
1851
1852        public Kind getKind() { return Kind.TYPE_CAST; }
1853        public JCTree getType() { return clazz; }
1854        public JCExpression getExpression() { return expr; }
1855        @Override
1856        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1857            return v.visitTypeCast(this, d);
1858        }
1859        @Override
1860        public Tag getTag() {
1861            return TYPECAST;
1862        }
1863    }
1864
1865    /**
1866     * A type test.
1867     */
1868    public static class JCInstanceOf extends JCExpression implements InstanceOfTree {
1869        public JCExpression expr;
1870        public JCTree clazz;
1871        protected JCInstanceOf(JCExpression expr, JCTree clazz) {
1872            this.expr = expr;
1873            this.clazz = clazz;
1874        }
1875        @Override
1876        public void accept(Visitor v) { v.visitTypeTest(this); }
1877
1878        public Kind getKind() { return Kind.INSTANCE_OF; }
1879        public JCTree getType() { return clazz; }
1880        public JCExpression getExpression() { return expr; }
1881        @Override
1882        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1883            return v.visitInstanceOf(this, d);
1884        }
1885        @Override
1886        public Tag getTag() {
1887            return TYPETEST;
1888        }
1889    }
1890
1891    /**
1892     * An array selection
1893     */
1894    public static class JCArrayAccess extends JCExpression implements ArrayAccessTree {
1895        public JCExpression indexed;
1896        public JCExpression index;
1897        protected JCArrayAccess(JCExpression indexed, JCExpression index) {
1898            this.indexed = indexed;
1899            this.index = index;
1900        }
1901        @Override
1902        public void accept(Visitor v) { v.visitIndexed(this); }
1903
1904        public Kind getKind() { return Kind.ARRAY_ACCESS; }
1905        public JCExpression getExpression() { return indexed; }
1906        public JCExpression getIndex() { return index; }
1907        @Override
1908        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1909            return v.visitArrayAccess(this, d);
1910        }
1911        @Override
1912        public Tag getTag() {
1913            return INDEXED;
1914        }
1915    }
1916
1917    /**
1918     * Selects through packages and classes
1919     */
1920    public static class JCFieldAccess extends JCExpression implements MemberSelectTree {
1921        /** selected Tree hierarchy */
1922        public JCExpression selected;
1923        /** name of field to select thru */
1924        public Name name;
1925        /** symbol of the selected class */
1926        public Symbol sym;
1927        protected JCFieldAccess(JCExpression selected, Name name, Symbol sym) {
1928            this.selected = selected;
1929            this.name = name;
1930            this.sym = sym;
1931        }
1932        @Override
1933        public void accept(Visitor v) { v.visitSelect(this); }
1934
1935        public Kind getKind() { return Kind.MEMBER_SELECT; }
1936        public JCExpression getExpression() { return selected; }
1937        @Override
1938        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
1939            return v.visitMemberSelect(this, d);
1940        }
1941        public Name getIdentifier() { return name; }
1942        @Override
1943        public Tag getTag() {
1944            return SELECT;
1945        }
1946    }
1947
1948    /**
1949     * Selects a member expression.
1950     */
1951    public static class JCMemberReference extends JCFunctionalExpression implements MemberReferenceTree {
1952
1953        public ReferenceMode mode;
1954        public ReferenceKind kind;
1955        public Name name;
1956        public JCExpression expr;
1957        public List<JCExpression> typeargs;
1958        public Symbol sym;
1959        public Type varargsElement;
1960        public PolyKind refPolyKind;
1961        public boolean ownerAccessible;
1962        public OverloadKind overloadKind;
1963
1964        public enum OverloadKind {
1965            OVERLOADED,
1966            UNOVERLOADED
1967        }
1968
1969        /**
1970         * Javac-dependent classification for member references, based
1971         * on relevant properties w.r.t. code-generation
1972         */
1973        public enum ReferenceKind {
1974            /** super # instMethod */
1975            SUPER(ReferenceMode.INVOKE, false),
1976            /** Type # instMethod */
1977            UNBOUND(ReferenceMode.INVOKE, true),
1978            /** Type # staticMethod */
1979            STATIC(ReferenceMode.INVOKE, false),
1980            /** Expr # instMethod */
1981            BOUND(ReferenceMode.INVOKE, false),
1982            /** Inner # new */
1983            IMPLICIT_INNER(ReferenceMode.NEW, false),
1984            /** Toplevel # new */
1985            TOPLEVEL(ReferenceMode.NEW, false),
1986            /** ArrayType # new */
1987            ARRAY_CTOR(ReferenceMode.NEW, false);
1988
1989            final ReferenceMode mode;
1990            final boolean unbound;
1991
1992            private ReferenceKind(ReferenceMode mode, boolean unbound) {
1993                this.mode = mode;
1994                this.unbound = unbound;
1995            }
1996
1997            public boolean isUnbound() {
1998                return unbound;
1999            }
2000        }
2001
2002        protected JCMemberReference(ReferenceMode mode, Name name, JCExpression expr, List<JCExpression> typeargs) {
2003            this.mode = mode;
2004            this.name = name;
2005            this.expr = expr;
2006            this.typeargs = typeargs;
2007        }
2008        @Override
2009        public void accept(Visitor v) { v.visitReference(this); }
2010
2011        public Kind getKind() { return Kind.MEMBER_REFERENCE; }
2012        @Override
2013        public ReferenceMode getMode() { return mode; }
2014        @Override
2015        public JCExpression getQualifierExpression() { return expr; }
2016        @Override
2017        public Name getName() { return name; }
2018        @Override
2019        public List<JCExpression> getTypeArguments() { return typeargs; }
2020
2021        @Override
2022        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2023            return v.visitMemberReference(this, d);
2024        }
2025        @Override
2026        public Tag getTag() {
2027            return REFERENCE;
2028        }
2029        public boolean hasKind(ReferenceKind kind) {
2030            return this.kind == kind;
2031        }
2032    }
2033
2034    /**
2035     * An identifier
2036     */
2037    public static class JCIdent extends JCExpression implements IdentifierTree {
2038        /** the name */
2039        public Name name;
2040        /** the symbol */
2041        public Symbol sym;
2042        protected JCIdent(Name name, Symbol sym) {
2043            this.name = name;
2044            this.sym = sym;
2045        }
2046        @Override
2047        public void accept(Visitor v) { v.visitIdent(this); }
2048
2049        public Kind getKind() { return Kind.IDENTIFIER; }
2050        public Name getName() { return name; }
2051        @Override
2052        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2053            return v.visitIdentifier(this, d);
2054        }
2055        @Override
2056        public Tag getTag() {
2057            return IDENT;
2058        }
2059    }
2060
2061    /**
2062     * A constant value given literally.
2063     */
2064    public static class JCLiteral extends JCExpression implements LiteralTree {
2065        public TypeTag typetag;
2066        /** value representation */
2067        public Object value;
2068        protected JCLiteral(TypeTag typetag, Object value) {
2069            this.typetag = typetag;
2070            this.value = value;
2071        }
2072        @Override
2073        public void accept(Visitor v) { v.visitLiteral(this); }
2074
2075        public Kind getKind() {
2076            return typetag.getKindLiteral();
2077        }
2078
2079        public Object getValue() {
2080            switch (typetag) {
2081                case BOOLEAN:
2082                    int bi = (Integer) value;
2083                    return (bi != 0);
2084                case CHAR:
2085                    int ci = (Integer) value;
2086                    char c = (char) ci;
2087                    if (c != ci)
2088                        throw new AssertionError("bad value for char literal");
2089                    return c;
2090                default:
2091                    return value;
2092            }
2093        }
2094        @Override
2095        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2096            return v.visitLiteral(this, d);
2097        }
2098        @Override
2099        public JCLiteral setType(Type type) {
2100            super.setType(type);
2101            return this;
2102        }
2103        @Override
2104        public Tag getTag() {
2105            return LITERAL;
2106        }
2107    }
2108
2109    /**
2110     * Identifies a basic type.
2111     * @see TypeTag
2112     */
2113    public static class JCPrimitiveTypeTree extends JCExpression implements PrimitiveTypeTree {
2114        /** the basic type id */
2115        public TypeTag typetag;
2116        protected JCPrimitiveTypeTree(TypeTag typetag) {
2117            this.typetag = typetag;
2118        }
2119        @Override
2120        public void accept(Visitor v) { v.visitTypeIdent(this); }
2121
2122        public Kind getKind() { return Kind.PRIMITIVE_TYPE; }
2123        public TypeKind getPrimitiveTypeKind() {
2124            return typetag.getPrimitiveTypeKind();
2125        }
2126
2127        @Override
2128        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2129            return v.visitPrimitiveType(this, d);
2130        }
2131        @Override
2132        public Tag getTag() {
2133            return TYPEIDENT;
2134        }
2135    }
2136
2137    /**
2138     * An array type, A[]
2139     */
2140    public static class JCArrayTypeTree extends JCExpression implements ArrayTypeTree {
2141        public JCExpression elemtype;
2142        protected JCArrayTypeTree(JCExpression elemtype) {
2143            this.elemtype = elemtype;
2144        }
2145        @Override
2146        public void accept(Visitor v) { v.visitTypeArray(this); }
2147
2148        public Kind getKind() { return Kind.ARRAY_TYPE; }
2149        public JCTree getType() { return elemtype; }
2150        @Override
2151        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2152            return v.visitArrayType(this, d);
2153        }
2154        @Override
2155        public Tag getTag() {
2156            return TYPEARRAY;
2157        }
2158    }
2159
2160    /**
2161     * A parameterized type, {@literal T<...>}
2162     */
2163    public static class JCTypeApply extends JCExpression implements ParameterizedTypeTree {
2164        public JCExpression clazz;
2165        public List<JCExpression> arguments;
2166        protected JCTypeApply(JCExpression clazz, List<JCExpression> arguments) {
2167            this.clazz = clazz;
2168            this.arguments = arguments;
2169        }
2170        @Override
2171        public void accept(Visitor v) { v.visitTypeApply(this); }
2172
2173        public Kind getKind() { return Kind.PARAMETERIZED_TYPE; }
2174        public JCTree getType() { return clazz; }
2175        public List<JCExpression> getTypeArguments() {
2176            return arguments;
2177        }
2178        @Override
2179        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2180            return v.visitParameterizedType(this, d);
2181        }
2182        @Override
2183        public Tag getTag() {
2184            return TYPEAPPLY;
2185        }
2186    }
2187
2188    /**
2189     * A union type, T1 | T2 | ... Tn (used in multicatch statements)
2190     */
2191    public static class JCTypeUnion extends JCExpression implements UnionTypeTree {
2192
2193        public List<JCExpression> alternatives;
2194
2195        protected JCTypeUnion(List<JCExpression> components) {
2196            this.alternatives = components;
2197        }
2198        @Override
2199        public void accept(Visitor v) { v.visitTypeUnion(this); }
2200
2201        public Kind getKind() { return Kind.UNION_TYPE; }
2202
2203        public List<JCExpression> getTypeAlternatives() {
2204            return alternatives;
2205        }
2206        @Override
2207        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2208            return v.visitUnionType(this, d);
2209        }
2210        @Override
2211        public Tag getTag() {
2212            return TYPEUNION;
2213        }
2214    }
2215
2216    /**
2217     * An intersection type, T1 & T2 & ... Tn (used in cast expressions)
2218     */
2219    public static class JCTypeIntersection extends JCExpression implements IntersectionTypeTree {
2220
2221        public List<JCExpression> bounds;
2222
2223        protected JCTypeIntersection(List<JCExpression> bounds) {
2224            this.bounds = bounds;
2225        }
2226        @Override
2227        public void accept(Visitor v) { v.visitTypeIntersection(this); }
2228
2229        public Kind getKind() { return Kind.INTERSECTION_TYPE; }
2230
2231        public List<JCExpression> getBounds() {
2232            return bounds;
2233        }
2234        @Override
2235        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2236            return v.visitIntersectionType(this, d);
2237        }
2238        @Override
2239        public Tag getTag() {
2240            return TYPEINTERSECTION;
2241        }
2242    }
2243
2244    /**
2245     * A formal class parameter.
2246     */
2247    public static class JCTypeParameter extends JCTree implements TypeParameterTree {
2248        /** name */
2249        public Name name;
2250        /** bounds */
2251        public List<JCExpression> bounds;
2252        /** type annotations on type parameter */
2253        public List<JCAnnotation> annotations;
2254        protected JCTypeParameter(Name name, List<JCExpression> bounds, List<JCAnnotation> annotations) {
2255            this.name = name;
2256            this.bounds = bounds;
2257            this.annotations = annotations;
2258        }
2259        @Override
2260        public void accept(Visitor v) { v.visitTypeParameter(this); }
2261
2262        public Kind getKind() { return Kind.TYPE_PARAMETER; }
2263        public Name getName() { return name; }
2264        public List<JCExpression> getBounds() {
2265            return bounds;
2266        }
2267        public List<JCAnnotation> getAnnotations() {
2268            return annotations;
2269        }
2270        @Override
2271        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2272            return v.visitTypeParameter(this, d);
2273        }
2274        @Override
2275        public Tag getTag() {
2276            return TYPEPARAMETER;
2277        }
2278    }
2279
2280    public static class JCWildcard extends JCExpression implements WildcardTree {
2281        public TypeBoundKind kind;
2282        public JCTree inner;
2283        protected JCWildcard(TypeBoundKind kind, JCTree inner) {
2284            kind.getClass(); // null-check
2285            this.kind = kind;
2286            this.inner = inner;
2287        }
2288        @Override
2289        public void accept(Visitor v) { v.visitWildcard(this); }
2290
2291        public Kind getKind() {
2292            switch (kind.kind) {
2293            case UNBOUND:
2294                return Kind.UNBOUNDED_WILDCARD;
2295            case EXTENDS:
2296                return Kind.EXTENDS_WILDCARD;
2297            case SUPER:
2298                return Kind.SUPER_WILDCARD;
2299            default:
2300                throw new AssertionError("Unknown wildcard bound " + kind);
2301            }
2302        }
2303        public JCTree getBound() { return inner; }
2304        @Override
2305        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2306            return v.visitWildcard(this, d);
2307        }
2308        @Override
2309        public Tag getTag() {
2310            return Tag.WILDCARD;
2311        }
2312    }
2313
2314    public static class TypeBoundKind extends JCTree {
2315        public BoundKind kind;
2316        protected TypeBoundKind(BoundKind kind) {
2317            this.kind = kind;
2318        }
2319        @Override
2320        public void accept(Visitor v) { v.visitTypeBoundKind(this); }
2321
2322        public Kind getKind() {
2323            throw new AssertionError("TypeBoundKind is not part of a public API");
2324        }
2325        @Override
2326        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2327            throw new AssertionError("TypeBoundKind is not part of a public API");
2328        }
2329        @Override
2330        public Tag getTag() {
2331            return TYPEBOUNDKIND;
2332        }
2333    }
2334
2335    public static class JCAnnotation extends JCExpression implements AnnotationTree {
2336        // Either Tag.ANNOTATION or Tag.TYPE_ANNOTATION
2337        private Tag tag;
2338
2339        public JCTree annotationType;
2340        public List<JCExpression> args;
2341
2342        // Attribute.Compound if tag is ANNOTATION
2343        // Attribute.TypeCompound if tag is TYPE_ANNOTATION
2344        //
2345        // NOTE: This field is slated for removal in the future.  Do
2346        // not use it for anything new.
2347        public Attribute.Compound attribute;
2348
2349        protected JCAnnotation(Tag tag, JCTree annotationType, List<JCExpression> args) {
2350            this.tag = tag;
2351            this.annotationType = annotationType;
2352            this.args = args;
2353        }
2354
2355        @Override
2356        public void accept(Visitor v) { v.visitAnnotation(this); }
2357
2358        public Kind getKind() { return TreeInfo.tagToKind(getTag()); }
2359
2360        public JCTree getAnnotationType() { return annotationType; }
2361        public List<JCExpression> getArguments() {
2362            return args;
2363        }
2364        @Override
2365        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2366            return v.visitAnnotation(this, d);
2367        }
2368        @Override
2369        public Tag getTag() {
2370            return tag;
2371        }
2372    }
2373
2374    public static class JCModifiers extends JCTree implements com.sun.source.tree.ModifiersTree {
2375        public long flags;
2376        public List<JCAnnotation> annotations;
2377        protected JCModifiers(long flags, List<JCAnnotation> annotations) {
2378            this.flags = flags;
2379            this.annotations = annotations;
2380        }
2381        @Override
2382        public void accept(Visitor v) { v.visitModifiers(this); }
2383
2384        public Kind getKind() { return Kind.MODIFIERS; }
2385        public Set<Modifier> getFlags() {
2386            return Flags.asModifierSet(flags);
2387        }
2388        public List<JCAnnotation> getAnnotations() {
2389            return annotations;
2390        }
2391        @Override
2392        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2393            return v.visitModifiers(this, d);
2394        }
2395        @Override
2396        public Tag getTag() {
2397            return MODIFIERS;
2398        }
2399    }
2400
2401    public static class JCAnnotatedType extends JCExpression implements com.sun.source.tree.AnnotatedTypeTree {
2402        // type annotations
2403        public List<JCAnnotation> annotations;
2404        public JCExpression underlyingType;
2405
2406        protected JCAnnotatedType(List<JCAnnotation> annotations, JCExpression underlyingType) {
2407            Assert.check(annotations != null && annotations.nonEmpty());
2408            this.annotations = annotations;
2409            this.underlyingType = underlyingType;
2410        }
2411        @Override
2412        public void accept(Visitor v) { v.visitAnnotatedType(this); }
2413
2414        public Kind getKind() { return Kind.ANNOTATED_TYPE; }
2415        public List<JCAnnotation> getAnnotations() {
2416            return annotations;
2417        }
2418        public JCExpression getUnderlyingType() {
2419            return underlyingType;
2420        }
2421        @Override
2422        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2423            return v.visitAnnotatedType(this, d);
2424        }
2425        @Override
2426        public Tag getTag() {
2427            return ANNOTATED_TYPE;
2428        }
2429    }
2430
2431    public static class JCErroneous extends JCExpression
2432            implements com.sun.source.tree.ErroneousTree {
2433        public List<? extends JCTree> errs;
2434        protected JCErroneous(List<? extends JCTree> errs) {
2435            this.errs = errs;
2436        }
2437        @Override
2438        public void accept(Visitor v) { v.visitErroneous(this); }
2439
2440        public Kind getKind() { return Kind.ERRONEOUS; }
2441
2442        public List<? extends JCTree> getErrorTrees() {
2443            return errs;
2444        }
2445
2446        @Override
2447        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2448            return v.visitErroneous(this, d);
2449        }
2450        @Override
2451        public Tag getTag() {
2452            return ERRONEOUS;
2453        }
2454    }
2455
2456    /** (let int x = 3; in x+2) */
2457    public static class LetExpr extends JCExpression {
2458        public List<JCVariableDecl> defs;
2459        public JCTree expr;
2460        protected LetExpr(List<JCVariableDecl> defs, JCTree expr) {
2461            this.defs = defs;
2462            this.expr = expr;
2463        }
2464        @Override
2465        public void accept(Visitor v) { v.visitLetExpr(this); }
2466
2467        public Kind getKind() {
2468            throw new AssertionError("LetExpr is not part of a public API");
2469        }
2470        @Override
2471        public <R,D> R accept(TreeVisitor<R,D> v, D d) {
2472            throw new AssertionError("LetExpr is not part of a public API");
2473        }
2474        @Override
2475        public Tag getTag() {
2476            return LETEXPR;
2477        }
2478    }
2479
2480    /** An interface for tree factories
2481     */
2482    public interface Factory {
2483        JCCompilationUnit TopLevel(List<JCTree> defs);
2484        JCPackageDecl PackageDecl(List<JCAnnotation> annotations,
2485                                  JCExpression pid);
2486        JCImport Import(JCTree qualid, boolean staticImport);
2487        JCClassDecl ClassDef(JCModifiers mods,
2488                          Name name,
2489                          List<JCTypeParameter> typarams,
2490                          JCExpression extending,
2491                          List<JCExpression> implementing,
2492                          List<JCTree> defs);
2493        JCMethodDecl MethodDef(JCModifiers mods,
2494                            Name name,
2495                            JCExpression restype,
2496                            List<JCTypeParameter> typarams,
2497                            JCVariableDecl recvparam,
2498                            List<JCVariableDecl> params,
2499                            List<JCExpression> thrown,
2500                            JCBlock body,
2501                            JCExpression defaultValue);
2502        JCVariableDecl VarDef(JCModifiers mods,
2503                      Name name,
2504                      JCExpression vartype,
2505                      JCExpression init);
2506        JCSkip Skip();
2507        JCBlock Block(long flags, List<JCStatement> stats);
2508        JCDoWhileLoop DoLoop(JCStatement body, JCExpression cond);
2509        JCWhileLoop WhileLoop(JCExpression cond, JCStatement body);
2510        JCForLoop ForLoop(List<JCStatement> init,
2511                        JCExpression cond,
2512                        List<JCExpressionStatement> step,
2513                        JCStatement body);
2514        JCEnhancedForLoop ForeachLoop(JCVariableDecl var, JCExpression expr, JCStatement body);
2515        JCLabeledStatement Labelled(Name label, JCStatement body);
2516        JCSwitch Switch(JCExpression selector, List<JCCase> cases);
2517        JCCase Case(JCExpression pat, List<JCStatement> stats);
2518        JCSynchronized Synchronized(JCExpression lock, JCBlock body);
2519        JCTry Try(JCBlock body, List<JCCatch> catchers, JCBlock finalizer);
2520        JCTry Try(List<JCTree> resources,
2521                  JCBlock body,
2522                  List<JCCatch> catchers,
2523                  JCBlock finalizer);
2524        JCCatch Catch(JCVariableDecl param, JCBlock body);
2525        JCConditional Conditional(JCExpression cond,
2526                                JCExpression thenpart,
2527                                JCExpression elsepart);
2528        JCIf If(JCExpression cond, JCStatement thenpart, JCStatement elsepart);
2529        JCExpressionStatement Exec(JCExpression expr);
2530        JCBreak Break(Name label);
2531        JCContinue Continue(Name label);
2532        JCReturn Return(JCExpression expr);
2533        JCThrow Throw(JCExpression expr);
2534        JCAssert Assert(JCExpression cond, JCExpression detail);
2535        JCMethodInvocation Apply(List<JCExpression> typeargs,
2536                    JCExpression fn,
2537                    List<JCExpression> args);
2538        JCNewClass NewClass(JCExpression encl,
2539                          List<JCExpression> typeargs,
2540                          JCExpression clazz,
2541                          List<JCExpression> args,
2542                          JCClassDecl def);
2543        JCNewArray NewArray(JCExpression elemtype,
2544                          List<JCExpression> dims,
2545                          List<JCExpression> elems);
2546        JCParens Parens(JCExpression expr);
2547        JCAssign Assign(JCExpression lhs, JCExpression rhs);
2548        JCAssignOp Assignop(Tag opcode, JCTree lhs, JCTree rhs);
2549        JCUnary Unary(Tag opcode, JCExpression arg);
2550        JCBinary Binary(Tag opcode, JCExpression lhs, JCExpression rhs);
2551        JCTypeCast TypeCast(JCTree expr, JCExpression type);
2552        JCInstanceOf TypeTest(JCExpression expr, JCTree clazz);
2553        JCArrayAccess Indexed(JCExpression indexed, JCExpression index);
2554        JCFieldAccess Select(JCExpression selected, Name selector);
2555        JCIdent Ident(Name idname);
2556        JCLiteral Literal(TypeTag tag, Object value);
2557        JCPrimitiveTypeTree TypeIdent(TypeTag typetag);
2558        JCArrayTypeTree TypeArray(JCExpression elemtype);
2559        JCTypeApply TypeApply(JCExpression clazz, List<JCExpression> arguments);
2560        JCTypeParameter TypeParameter(Name name, List<JCExpression> bounds);
2561        JCWildcard Wildcard(TypeBoundKind kind, JCTree type);
2562        TypeBoundKind TypeBoundKind(BoundKind kind);
2563        JCAnnotation Annotation(JCTree annotationType, List<JCExpression> args);
2564        JCModifiers Modifiers(long flags, List<JCAnnotation> annotations);
2565        JCErroneous Erroneous(List<? extends JCTree> errs);
2566        LetExpr LetExpr(List<JCVariableDecl> defs, JCTree expr);
2567    }
2568
2569    /** A generic visitor class for trees.
2570     */
2571    public static abstract class Visitor {
2572        public void visitTopLevel(JCCompilationUnit that)    { visitTree(that); }
2573        public void visitPackageDef(JCPackageDecl that)      { visitTree(that); }
2574        public void visitImport(JCImport that)               { visitTree(that); }
2575        public void visitClassDef(JCClassDecl that)          { visitTree(that); }
2576        public void visitMethodDef(JCMethodDecl that)        { visitTree(that); }
2577        public void visitVarDef(JCVariableDecl that)         { visitTree(that); }
2578        public void visitSkip(JCSkip that)                   { visitTree(that); }
2579        public void visitBlock(JCBlock that)                 { visitTree(that); }
2580        public void visitDoLoop(JCDoWhileLoop that)          { visitTree(that); }
2581        public void visitWhileLoop(JCWhileLoop that)         { visitTree(that); }
2582        public void visitForLoop(JCForLoop that)             { visitTree(that); }
2583        public void visitForeachLoop(JCEnhancedForLoop that) { visitTree(that); }
2584        public void visitLabelled(JCLabeledStatement that)   { visitTree(that); }
2585        public void visitSwitch(JCSwitch that)               { visitTree(that); }
2586        public void visitCase(JCCase that)                   { visitTree(that); }
2587        public void visitSynchronized(JCSynchronized that)   { visitTree(that); }
2588        public void visitTry(JCTry that)                     { visitTree(that); }
2589        public void visitCatch(JCCatch that)                 { visitTree(that); }
2590        public void visitConditional(JCConditional that)     { visitTree(that); }
2591        public void visitIf(JCIf that)                       { visitTree(that); }
2592        public void visitExec(JCExpressionStatement that)    { visitTree(that); }
2593        public void visitBreak(JCBreak that)                 { visitTree(that); }
2594        public void visitContinue(JCContinue that)           { visitTree(that); }
2595        public void visitReturn(JCReturn that)               { visitTree(that); }
2596        public void visitThrow(JCThrow that)                 { visitTree(that); }
2597        public void visitAssert(JCAssert that)               { visitTree(that); }
2598        public void visitApply(JCMethodInvocation that)      { visitTree(that); }
2599        public void visitNewClass(JCNewClass that)           { visitTree(that); }
2600        public void visitNewArray(JCNewArray that)           { visitTree(that); }
2601        public void visitLambda(JCLambda that)               { visitTree(that); }
2602        public void visitParens(JCParens that)               { visitTree(that); }
2603        public void visitAssign(JCAssign that)               { visitTree(that); }
2604        public void visitAssignop(JCAssignOp that)           { visitTree(that); }
2605        public void visitUnary(JCUnary that)                 { visitTree(that); }
2606        public void visitBinary(JCBinary that)               { visitTree(that); }
2607        public void visitTypeCast(JCTypeCast that)           { visitTree(that); }
2608        public void visitTypeTest(JCInstanceOf that)         { visitTree(that); }
2609        public void visitIndexed(JCArrayAccess that)         { visitTree(that); }
2610        public void visitSelect(JCFieldAccess that)          { visitTree(that); }
2611        public void visitReference(JCMemberReference that)   { visitTree(that); }
2612        public void visitIdent(JCIdent that)                 { visitTree(that); }
2613        public void visitLiteral(JCLiteral that)             { visitTree(that); }
2614        public void visitTypeIdent(JCPrimitiveTypeTree that) { visitTree(that); }
2615        public void visitTypeArray(JCArrayTypeTree that)     { visitTree(that); }
2616        public void visitTypeApply(JCTypeApply that)         { visitTree(that); }
2617        public void visitTypeUnion(JCTypeUnion that)         { visitTree(that); }
2618        public void visitTypeIntersection(JCTypeIntersection that)  { visitTree(that); }
2619        public void visitTypeParameter(JCTypeParameter that) { visitTree(that); }
2620        public void visitWildcard(JCWildcard that)           { visitTree(that); }
2621        public void visitTypeBoundKind(TypeBoundKind that)   { visitTree(that); }
2622        public void visitAnnotation(JCAnnotation that)       { visitTree(that); }
2623        public void visitModifiers(JCModifiers that)         { visitTree(that); }
2624        public void visitAnnotatedType(JCAnnotatedType that) { visitTree(that); }
2625        public void visitErroneous(JCErroneous that)         { visitTree(that); }
2626        public void visitLetExpr(LetExpr that)               { visitTree(that); }
2627
2628        public void visitTree(JCTree that)                   { Assert.error(); }
2629    }
2630
2631}
2632