langtools/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java
author pgovereau
Tue, 22 Apr 2014 17:07:54 -0400
changeset 24069 dfb8f11542fc
parent 22440 d40c30326317
child 24219 e7dc661cafae
permissions -rw-r--r--
8034245: Refactor TopLevel tree node. Summary: Added JCPackageDecl to JCTree to store package-level information. Reviewed-by: jjg, jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
22440
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22163
diff changeset
     2
 * Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5492
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5492
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5492
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5492
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 5492
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12916
diff changeset
    28
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14722
diff changeset
    29
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.source.tree.Tree;
22440
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22163
diff changeset
    31
import com.sun.source.util.TreePath;
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12916
diff changeset
    32
import com.sun.tools.javac.code.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.comp.AttrContext;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.comp.Env;
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12916
diff changeset
    35
import com.sun.tools.javac.tree.JCTree.*;
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
    36
import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import static com.sun.tools.javac.code.Flags.*;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
    40
import static com.sun.tools.javac.code.TypeTag.BOT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    41
import static com.sun.tools.javac.tree.JCTree.Tag.*;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    42
import static com.sun.tools.javac.tree.JCTree.Tag.BLOCK;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    43
import static com.sun.tools.javac.tree.JCTree.Tag.SYNCHRONIZED;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
/** Utility class containing inspector methods for trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    47
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    48
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
public class TreeInfo {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 18414
diff changeset
    53
    protected static final Context.Key<TreeInfo> treeInfoKey = new Context.Key<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    public static TreeInfo instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        TreeInfo instance = context.get(treeInfoKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
            instance = new TreeInfo(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    /** The names of all operators.
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     */
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    64
    private Name[] opname = new Name[Tag.getNumberOfOperators()];
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    65
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    66
    private void setOpname(Tag tag, String name, Names names) {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    67
         setOpname(tag, names.fromString(name));
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    68
     }
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    69
     private void setOpname(Tag tag, Name name) {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    70
         opname[tag.operatorIndex()] = name;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    71
     }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    private TreeInfo(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
        context.put(treeInfoKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1206
diff changeset
    76
        Names names = Names.instance(context);
18414
ee1e93e83d2b 8016610: javac, add new internal symbols to make operator resolution faster
vromero
parents: 18010
diff changeset
    77
        /*  Internally we use +++, --- for unary +, - to reduce +, - operators
ee1e93e83d2b 8016610: javac, add new internal symbols to make operator resolution faster
vromero
parents: 18010
diff changeset
    78
         *  overloading
ee1e93e83d2b 8016610: javac, add new internal symbols to make operator resolution faster
vromero
parents: 18010
diff changeset
    79
         */
ee1e93e83d2b 8016610: javac, add new internal symbols to make operator resolution faster
vromero
parents: 18010
diff changeset
    80
        setOpname(POS, "+++", names);
ee1e93e83d2b 8016610: javac, add new internal symbols to make operator resolution faster
vromero
parents: 18010
diff changeset
    81
        setOpname(NEG, "---", names);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    82
        setOpname(NOT, "!", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    83
        setOpname(COMPL, "~", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    84
        setOpname(PREINC, "++", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    85
        setOpname(PREDEC, "--", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    86
        setOpname(POSTINC, "++", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    87
        setOpname(POSTDEC, "--", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    88
        setOpname(NULLCHK, "<*nullchk*>", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    89
        setOpname(OR, "||", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    90
        setOpname(AND, "&&", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    91
        setOpname(EQ, "==", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    92
        setOpname(NE, "!=", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    93
        setOpname(LT, "<", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    94
        setOpname(GT, ">", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    95
        setOpname(LE, "<=", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    96
        setOpname(GE, ">=", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    97
        setOpname(BITOR, "|", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    98
        setOpname(BITXOR, "^", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    99
        setOpname(BITAND, "&", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   100
        setOpname(SL, "<<", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   101
        setOpname(SR, ">>", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   102
        setOpname(USR, ">>>", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   103
        setOpname(PLUS, "+", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   104
        setOpname(MINUS, names.hyphen);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   105
        setOpname(MUL, names.asterisk);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   106
        setOpname(DIV, names.slash);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   107
        setOpname(MOD, "%", names);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
12080
23101f54df44 7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents: 11142
diff changeset
   110
    public static List<JCExpression> args(JCTree t) {
23101f54df44 7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents: 11142
diff changeset
   111
        switch (t.getTag()) {
23101f54df44 7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents: 11142
diff changeset
   112
            case APPLY:
23101f54df44 7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents: 11142
diff changeset
   113
                return ((JCMethodInvocation)t).args;
23101f54df44 7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents: 11142
diff changeset
   114
            case NEWCLASS:
23101f54df44 7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents: 11142
diff changeset
   115
                return ((JCNewClass)t).args;
23101f54df44 7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents: 11142
diff changeset
   116
            default:
23101f54df44 7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents: 11142
diff changeset
   117
                return null;
23101f54df44 7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents: 11142
diff changeset
   118
        }
23101f54df44 7144506: Attr.checkMethod should be called after inference variables have been fixed
mcimadamore
parents: 11142
diff changeset
   119
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    /** Return name of operator with given tree tag.
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
     */
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   123
    public Name operatorName(JCTree.Tag tag) {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   124
        return opname[tag.operatorIndex()];
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    /** Is tree a constructor declaration?
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    public static boolean isConstructor(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   130
        if (tree.hasTag(METHODDEF)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
            Name name = ((JCMethodDecl) tree).name;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1206
diff changeset
   132
            return name == name.table.names.init;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    /** Is there a constructor declaration in the given list of trees?
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
    public static boolean hasConstructors(List<JCTree> trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        for (List<JCTree> l = trees; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
            if (isConstructor(l.head)) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
   146
    public static boolean isMultiCatch(JCCatch catchClause) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   147
        return catchClause.param.vartype.hasTag(TYPEUNION);
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
   148
    }
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
   149
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    /** Is statement an initializer for a synthetic field?
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    public static boolean isSyntheticInit(JCTree stat) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   153
        if (stat.hasTag(EXEC)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
            JCExpressionStatement exec = (JCExpressionStatement)stat;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   155
            if (exec.expr.hasTag(ASSIGN)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
                JCAssign assign = (JCAssign)exec.expr;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   157
                if (assign.lhs.hasTag(SELECT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
                    JCFieldAccess select = (JCFieldAccess)assign.lhs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
                    if (select.sym != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
                        (select.sym.flags() & SYNTHETIC) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
                        Name selected = name(select.selected);
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1206
diff changeset
   162
                        if (selected != null && selected == selected.table.names._this)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    /** If the expression is a method call, return the method name, null
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
     *  otherwise. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    public static Name calledMethodName(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   174
        if (tree.hasTag(EXEC)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
            JCExpressionStatement exec = (JCExpressionStatement)tree;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   176
            if (exec.expr.hasTag(APPLY)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
                Name mname = TreeInfo.name(((JCMethodInvocation) exec.expr).meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
                return mname;
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    /** Is this a call to this or super?
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    public static boolean isSelfCall(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        Name name = calledMethodName(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        if (name != null) {
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1206
diff changeset
   189
            Names names = name.table.names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
            return name==names._this || name==names._super;
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    /** Is this a call to super?
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    public static boolean isSuperCall(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        Name name = calledMethodName(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
        if (name != null) {
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1206
diff changeset
   201
            Names names = name.table.names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
            return name==names._super;
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
    /** Is this a constructor whose first (non-synthetic) statement is not
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
     *  of the form this(...)?
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
    public static boolean isInitialConstructor(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        JCMethodInvocation app = firstConstructorCall(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        if (app == null) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        Name meth = name(app.meth);
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1206
diff changeset
   215
        return meth == null || meth != meth.table.names._this;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    /** Return the first call in a constructor definition. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
    public static JCMethodInvocation firstConstructorCall(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   220
        if (!tree.hasTag(METHODDEF)) return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        JCMethodDecl md = (JCMethodDecl) tree;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1206
diff changeset
   222
        Names names = md.name.table.names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        if (md.name != names.init) return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        if (md.body == null) return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
        List<JCStatement> stats = md.body.stats;
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        // Synthetic initializations can appear before the super call.
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        while (stats.nonEmpty() && isSyntheticInit(stats.head))
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
            stats = stats.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
        if (stats.isEmpty()) return null;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   230
        if (!stats.head.hasTag(EXEC)) return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        JCExpressionStatement exec = (JCExpressionStatement) stats.head;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   232
        if (!exec.expr.hasTag(APPLY)) return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        return (JCMethodInvocation)exec.expr;
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   236
    /** Return true if a tree represents a diamond new expr. */
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   237
    public static boolean isDiamond(JCTree tree) {
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   238
        switch(tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   239
            case TYPEAPPLY: return ((JCTypeApply)tree).getTypeArguments().isEmpty();
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   240
            case NEWCLASS: return isDiamond(((JCNewClass)tree).clazz);
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   241
            case ANNOTATED_TYPE: return isDiamond(((JCAnnotatedType)tree).underlyingType);
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   242
            default: return false;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   243
        }
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   244
    }
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   245
12916
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12080
diff changeset
   246
    public static boolean isEnumInit(JCTree tree) {
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12080
diff changeset
   247
        switch (tree.getTag()) {
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12080
diff changeset
   248
            case VARDEF:
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12080
diff changeset
   249
                return (((JCVariableDecl)tree).mods.flags & ENUM) != 0;
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12080
diff changeset
   250
            default:
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12080
diff changeset
   251
                return false;
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12080
diff changeset
   252
        }
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12080
diff changeset
   253
    }
021c069e8e27 7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents: 12080
diff changeset
   254
15374
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   255
    /** set 'polyKind' on given tree */
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   256
    public static void setPolyKind(JCTree tree, PolyKind pkind) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   257
        switch (tree.getTag()) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   258
            case APPLY:
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   259
                ((JCMethodInvocation)tree).polyKind = pkind;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   260
                break;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   261
            case NEWCLASS:
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   262
                ((JCNewClass)tree).polyKind = pkind;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   263
                break;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   264
            case REFERENCE:
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   265
                ((JCMemberReference)tree).refPolyKind = pkind;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   266
                break;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   267
            default:
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   268
                throw new AssertionError("Unexpected tree: " + tree);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   269
        }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   270
    }
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   271
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   272
    /** set 'varargsElement' on given tree */
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   273
    public static void setVarargsElement(JCTree tree, Type varargsElement) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   274
        switch (tree.getTag()) {
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   275
            case APPLY:
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   276
                ((JCMethodInvocation)tree).varargsElement = varargsElement;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   277
                break;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   278
            case NEWCLASS:
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   279
                ((JCNewClass)tree).varargsElement = varargsElement;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   280
                break;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   281
            case REFERENCE:
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   282
                ((JCMemberReference)tree).varargsElement = varargsElement;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   283
                break;
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   284
            default:
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   285
                throw new AssertionError("Unexpected tree: " + tree);
fb8f6acf09cc 8005244: Implement overload resolution as per latest spec EDR
mcimadamore
parents: 14952
diff changeset
   286
        }
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   287
    }
14722
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   288
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   289
    /** Return true if the tree corresponds to an expression statement */
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   290
    public static boolean isExpressionStatement(JCExpression tree) {
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   291
        switch(tree.getTag()) {
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   292
            case PREINC: case PREDEC:
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   293
            case POSTINC: case POSTDEC:
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   294
            case ASSIGN:
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   295
            case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG:
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   296
            case SL_ASG: case SR_ASG: case USR_ASG:
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   297
            case PLUS_ASG: case MINUS_ASG:
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   298
            case MUL_ASG: case DIV_ASG: case MOD_ASG:
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   299
            case APPLY: case NEWCLASS:
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   300
            case ERRONEOUS:
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   301
                return true;
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   302
            default:
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   303
                return false;
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   304
        }
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   305
    }
aaa39655aa2e 8004105: Expression statement lambdas should be void-compatible
mcimadamore
parents: 14359
diff changeset
   306
11142
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   307
    /**
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   308
     * Return true if the AST corresponds to a static select of the kind A.B
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   309
     */
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   310
    public static boolean isStaticSelector(JCTree base, Names names) {
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   311
        if (base == null)
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   312
            return false;
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   313
        switch (base.getTag()) {
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   314
            case IDENT:
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   315
                JCIdent id = (JCIdent)base;
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   316
                return id.name != names._this &&
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   317
                        id.name != names._super &&
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   318
                        isStaticSym(base);
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   319
            case SELECT:
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   320
                return isStaticSym(base) &&
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   321
                    isStaticSelector(((JCFieldAccess)base).selected, names);
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   322
            case TYPEAPPLY:
14062
b7439971a094 7177386: Add attribution support for method references
mcimadamore
parents: 14058
diff changeset
   323
            case TYPEARRAY:
11142
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   324
                return true;
15718
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   325
            case ANNOTATED_TYPE:
8e54c8e43d38 8008077: update reference impl for type-annotations
jjg
parents: 15385
diff changeset
   326
                return isStaticSelector(((JCAnnotatedType)base).underlyingType, names);
11142
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   327
            default:
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   328
                return false;
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   329
        }
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   330
    }
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   331
    //where
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   332
        private static boolean isStaticSym(JCTree tree) {
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   333
            Symbol sym = symbol(tree);
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   334
            return (sym.kind == Kinds.TYP ||
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   335
                    sym.kind == Kinds.PCK);
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   336
        }
45d0ec1e7463 7115049: Add AST node for method references
mcimadamore
parents: 11055
diff changeset
   337
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
    /** Return true if a tree represents the null literal. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
    public static boolean isNull(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   340
        if (!tree.hasTag(LITERAL))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   342
        JCLiteral lit = (JCLiteral) tree;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 14267
diff changeset
   343
        return (lit.typetag == BOT);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
22440
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22163
diff changeset
   346
    /** Return true iff this tree is a child of some annotation. */
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22163
diff changeset
   347
    public static boolean isInAnnotation(Env<?> env, JCTree tree) {
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22163
diff changeset
   348
        TreePath tp = TreePath.getPath(env.toplevel, tree);
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22163
diff changeset
   349
        if (tp != null) {
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22163
diff changeset
   350
            for (Tree t : tp) {
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22163
diff changeset
   351
                if (t.getKind() == Tree.Kind.ANNOTATION)
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22163
diff changeset
   352
                    return true;
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22163
diff changeset
   353
            }
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22163
diff changeset
   354
        }
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22163
diff changeset
   355
        return false;
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22163
diff changeset
   356
    }
d40c30326317 8028389: NullPointerException compiling annotation values that have bodies
alundblad
parents: 22163
diff changeset
   357
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12916
diff changeset
   358
    public static String getCommentText(Env<?> env, JCTree tree) {
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12916
diff changeset
   359
        DocCommentTable docComments = (tree.hasTag(JCTree.Tag.TOPLEVEL))
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12916
diff changeset
   360
                ? ((JCCompilationUnit) tree).docComments
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12916
diff changeset
   361
                : env.toplevel.docComments;
13078
11d5e1c88864 7178297: provide mapping from doc comment position to source file position
jjg
parents: 13077
diff changeset
   362
        return (docComments == null) ? null : docComments.getCommentText(tree);
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12916
diff changeset
   363
    }
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 12916
diff changeset
   364
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14722
diff changeset
   365
    public static DCTree.DCDocComment getCommentTree(Env<?> env, JCTree tree) {
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14722
diff changeset
   366
        DocCommentTable docComments = (tree.hasTag(JCTree.Tag.TOPLEVEL))
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14722
diff changeset
   367
                ? ((JCCompilationUnit) tree).docComments
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14722
diff changeset
   368
                : env.toplevel.docComments;
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14722
diff changeset
   369
        return (docComments == null) ? null : docComments.getCommentTree(tree);
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14722
diff changeset
   370
    }
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14722
diff changeset
   371
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
    /** The position of the first statement in a block, or the position of
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
     *  the block itself if it is empty.
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
    public static int firstStatPos(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   376
        if (tree.hasTag(BLOCK) && ((JCBlock) tree).stats.nonEmpty())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
            return ((JCBlock) tree).stats.head.pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
            return tree.pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
    /** The end position of given tree, if it is a block with
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
     *  defined endpos.
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
    public static int endPos(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   386
        if (tree.hasTag(BLOCK) && ((JCBlock) tree).endpos != Position.NOPOS)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
            return ((JCBlock) tree).endpos;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   388
        else if (tree.hasTag(SYNCHRONIZED))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
            return endPos(((JCSynchronized) tree).body);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   390
        else if (tree.hasTag(TRY)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
            JCTry t = (JCTry) tree;
13442
41b5b4736385 7178324: Crash when compiling for(i : x) try(AutoCloseable x = ...) {}
sundar
parents: 13078
diff changeset
   392
            return endPos((t.finalizer != null) ? t.finalizer
41b5b4736385 7178324: Crash when compiling for(i : x) try(AutoCloseable x = ...) {}
sundar
parents: 13078
diff changeset
   393
                          : (t.catchers.nonEmpty() ? t.catchers.last().body : t.body));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
            return tree.pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
    /** Get the start position for a tree node.  The start position is
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
     * defined to be the position of the first character of the first
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
     * token of the node's source text.
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
     * @param tree  The tree node
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
    public static int getStartPos(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
        if (tree == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
            return Position.NOPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
        switch(tree.getTag()) {
24069
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 22440
diff changeset
   409
            case PACKAGEDEF: {
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 22440
diff changeset
   410
                JCPackageDecl pd = (JCPackageDecl)tree;
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 22440
diff changeset
   411
                return pd.annotations.isEmpty() ? pd.pos :
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 22440
diff changeset
   412
                       pd.annotations.head.pos;
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 22440
diff changeset
   413
            }
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   414
            case APPLY:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   415
                return getStartPos(((JCMethodInvocation) tree).meth);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   416
            case ASSIGN:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   417
                return getStartPos(((JCAssign) tree).lhs);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   418
            case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   419
            case SL_ASG: case SR_ASG: case USR_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   420
            case PLUS_ASG: case MINUS_ASG: case MUL_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   421
            case DIV_ASG: case MOD_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   422
                return getStartPos(((JCAssignOp) tree).lhs);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   423
            case OR: case AND: case BITOR:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   424
            case BITXOR: case BITAND: case EQ:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   425
            case NE: case LT: case GT:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   426
            case LE: case GE: case SL:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   427
            case SR: case USR: case PLUS:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   428
            case MINUS: case MUL: case DIV:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   429
            case MOD:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   430
                return getStartPos(((JCBinary) tree).lhs);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   431
            case CLASSDEF: {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   432
                JCClassDecl node = (JCClassDecl)tree;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   433
                if (node.mods.pos != Position.NOPOS)
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   434
                    return node.mods.pos;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   435
                break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
            }
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   437
            case CONDEXPR:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   438
                return getStartPos(((JCConditional) tree).cond);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   439
            case EXEC:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   440
                return getStartPos(((JCExpressionStatement) tree).expr);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   441
            case INDEXED:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   442
                return getStartPos(((JCArrayAccess) tree).indexed);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   443
            case METHODDEF: {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   444
                JCMethodDecl node = (JCMethodDecl)tree;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   445
                if (node.mods.pos != Position.NOPOS)
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   446
                    return node.mods.pos;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   447
                if (node.typarams.nonEmpty()) // List.nil() used for no typarams
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   448
                    return getStartPos(node.typarams.head);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   449
                return node.restype == null ? node.pos : getStartPos(node.restype);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   450
            }
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   451
            case SELECT:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   452
                return getStartPos(((JCFieldAccess) tree).selected);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   453
            case TYPEAPPLY:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   454
                return getStartPos(((JCTypeApply) tree).clazz);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   455
            case TYPEARRAY:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   456
                return getStartPos(((JCArrayTypeTree) tree).elemtype);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   457
            case TYPETEST:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   458
                return getStartPos(((JCInstanceOf) tree).expr);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   459
            case POSTINC:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   460
            case POSTDEC:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   461
                return getStartPos(((JCUnary) tree).arg);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   462
            case ANNOTATED_TYPE: {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   463
                JCAnnotatedType node = (JCAnnotatedType) tree;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   464
                if (node.annotations.nonEmpty()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   465
                    if (node.underlyingType.hasTag(TYPEARRAY) ||
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   466
                            node.underlyingType.hasTag(SELECT)) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   467
                        return getStartPos(node.underlyingType);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   468
                    } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   469
                        return getStartPos(node.annotations.head);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   470
                    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   471
                } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   472
                    return getStartPos(node.underlyingType);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   473
                }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   474
            }
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   475
            case NEWCLASS: {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   476
                JCNewClass node = (JCNewClass)tree;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   477
                if (node.encl != null)
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   478
                    return getStartPos(node.encl);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   479
                break;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   480
            }
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   481
            case VARDEF: {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   482
                JCVariableDecl node = (JCVariableDecl)tree;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   483
                if (node.mods.pos != Position.NOPOS) {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   484
                    return node.mods.pos;
14058
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   485
                } else if (node.vartype == null) {
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   486
                    //if there's no type (partially typed lambda parameter)
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   487
                    //simply return node position
c7ec7facdd20 7177385: Add attribution support for lambda expressions
mcimadamore
parents: 14057
diff changeset
   488
                    return node.pos;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   489
                } else {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   490
                    return getStartPos(node.vartype);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   491
                }
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   492
            }
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   493
            case ERRONEOUS: {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   494
                JCErroneous node = (JCErroneous)tree;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   495
                if (node.errs != null && node.errs.nonEmpty())
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   496
                    return getStartPos(node.errs.head);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   497
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
        return tree.pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
    /** The end position of given tree, given  a table of end positions generated by the parser
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
     */
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   504
    public static int getEndPos(JCTree tree, EndPosTable endPosTable) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
        if (tree == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
            return Position.NOPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   508
        if (endPosTable == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
            // fall back on limited info in the tree
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
            return endPos(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   513
        int mapPos = endPosTable.getEndPos(tree);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   514
        if (mapPos != Position.NOPOS)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
            return mapPos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
        switch(tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   518
            case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   519
            case SL_ASG: case SR_ASG: case USR_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   520
            case PLUS_ASG: case MINUS_ASG: case MUL_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   521
            case DIV_ASG: case MOD_ASG:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   522
                return getEndPos(((JCAssignOp) tree).rhs, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   523
            case OR: case AND: case BITOR:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   524
            case BITXOR: case BITAND: case EQ:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   525
            case NE: case LT: case GT:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   526
            case LE: case GE: case SL:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   527
            case SR: case USR: case PLUS:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   528
            case MINUS: case MUL: case DIV:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   529
            case MOD:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   530
                return getEndPos(((JCBinary) tree).rhs, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   531
            case CASE:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   532
                return getEndPos(((JCCase) tree).stats.last(), endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   533
            case CATCH:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   534
                return getEndPos(((JCCatch) tree).body, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   535
            case CONDEXPR:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   536
                return getEndPos(((JCConditional) tree).falsepart, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   537
            case FORLOOP:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   538
                return getEndPos(((JCForLoop) tree).body, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   539
            case FOREACHLOOP:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   540
                return getEndPos(((JCEnhancedForLoop) tree).body, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   541
            case IF: {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   542
                JCIf node = (JCIf)tree;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   543
                if (node.elsepart == null) {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   544
                    return getEndPos(node.thenpart, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   545
                } else {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   546
                    return getEndPos(node.elsepart, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   547
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
            }
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   549
            case LABELLED:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   550
                return getEndPos(((JCLabeledStatement) tree).body, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   551
            case MODIFIERS:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   552
                return getEndPos(((JCModifiers) tree).annotations.last(), endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   553
            case SYNCHRONIZED:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   554
                return getEndPos(((JCSynchronized) tree).body, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   555
            case TOPLEVEL:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   556
                return getEndPos(((JCCompilationUnit) tree).defs.last(), endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   557
            case TRY: {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   558
                JCTry node = (JCTry)tree;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   559
                if (node.finalizer != null) {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   560
                    return getEndPos(node.finalizer, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   561
                } else if (!node.catchers.isEmpty()) {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   562
                    return getEndPos(node.catchers.last(), endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   563
                } else {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   564
                    return getEndPos(node.body, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   565
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
            }
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   567
            case WILDCARD:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   568
                return getEndPos(((JCWildcard) tree).inner, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   569
            case TYPECAST:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   570
                return getEndPos(((JCTypeCast) tree).expr, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   571
            case TYPETEST:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   572
                return getEndPos(((JCInstanceOf) tree).clazz, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   573
            case POS:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   574
            case NEG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   575
            case NOT:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   576
            case COMPL:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   577
            case PREINC:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   578
            case PREDEC:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   579
                return getEndPos(((JCUnary) tree).arg, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   580
            case WHILELOOP:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   581
                return getEndPos(((JCWhileLoop) tree).body, endPosTable);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   582
            case ANNOTATED_TYPE:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   583
                return getEndPos(((JCAnnotatedType) tree).underlyingType, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   584
            case ERRONEOUS: {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   585
                JCErroneous node = (JCErroneous)tree;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   586
                if (node.errs != null && node.errs.nonEmpty())
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   587
                    return getEndPos(node.errs.last(), endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   588
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
        return Position.NOPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
    /** A DiagnosticPosition with the preferred position set to the
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
     *  end position of given tree, if it is a block with
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
     *  defined endpos.
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
    public static DiagnosticPosition diagEndPos(final JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
        final int endPos = TreeInfo.endPos(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
        return new DiagnosticPosition() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
            public JCTree getTree() { return tree; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
            public int getStartPosition() { return TreeInfo.getStartPos(tree); }
06bc494ca11e Initial load
duke
parents:
diff changeset
   603
            public int getPreferredPosition() { return endPos; }
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   604
            public int getEndPosition(EndPosTable endPosTable) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
                return TreeInfo.getEndPos(tree, endPosTable);
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
    /** The position of the finalizer of given try/synchronized statement.
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
    public static int finalizerPos(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   613
        if (tree.hasTag(TRY)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
            JCTry t = (JCTry) tree;
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
   615
            Assert.checkNonNull(t.finalizer);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
            return firstStatPos(t.finalizer);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   617
        } else if (tree.hasTag(SYNCHRONIZED)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   618
            return endPos(((JCSynchronized) tree).body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
            throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
    /** Find the position for reporting an error about a symbol, where
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
     *  that symbol is defined somewhere in the given tree. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
    public static int positionFor(final Symbol sym, final JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
        JCTree decl = declarationFor(sym, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
        return ((decl != null) ? decl : tree).pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
    /** Find the position for reporting an error about a symbol, where
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
     *  that symbol is defined somewhere in the given tree. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
    public static DiagnosticPosition diagnosticPositionFor(final Symbol sym, final JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
        JCTree decl = declarationFor(sym, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
        return ((decl != null) ? decl : tree).pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
    /** Find the declaration for a symbol, where
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
     *  that symbol is defined somewhere in the given tree. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
    public static JCTree declarationFor(final Symbol sym, final JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
        class DeclScanner extends TreeScanner {
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
            JCTree result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
            public void scan(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
                if (tree!=null && result==null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
                    tree.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
            public void visitTopLevel(JCCompilationUnit that) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
                if (that.packge == sym) result = that;
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
                else super.visitTopLevel(that);
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
            public void visitClassDef(JCClassDecl that) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
                if (that.sym == sym) result = that;
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
                else super.visitClassDef(that);
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
            public void visitMethodDef(JCMethodDecl that) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
                if (that.sym == sym) result = that;
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
                else super.visitMethodDef(that);
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
            public void visitVarDef(JCVariableDecl that) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
                if (that.sym == sym) result = that;
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
                else super.visitVarDef(that);
06bc494ca11e Initial load
duke
parents:
diff changeset
   662
            }
6586
0d40dc0c06cb 6458823: Messager messages on TypeParamterElements to not include position information.
sundar
parents: 5847
diff changeset
   663
            public void visitTypeParameter(JCTypeParameter that) {
7619
1bedd48ff024 6990209: JCK7-compiler lang/ICLS/icls006/icls00603/icls00603a.html#icls00603src test fails.
jjh
parents: 7074
diff changeset
   664
                if (that.type != null && that.type.tsym == sym) result = that;
6586
0d40dc0c06cb 6458823: Messager messages on TypeParamterElements to not include position information.
sundar
parents: 5847
diff changeset
   665
                else super.visitTypeParameter(that);
0d40dc0c06cb 6458823: Messager messages on TypeParamterElements to not include position information.
sundar
parents: 5847
diff changeset
   666
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
        DeclScanner s = new DeclScanner();
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
        tree.accept(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
        return s.result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
    public static Env<AttrContext> scopeFor(JCTree node, JCCompilationUnit unit) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
        return scopeFor(pathFor(node, unit));
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
    public static Env<AttrContext> scopeFor(List<JCTree> path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
        // TODO: not implemented yet
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
        throw new UnsupportedOperationException("not implemented yet");
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
    public static List<JCTree> pathFor(final JCTree node, final JCCompilationUnit unit) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
        class Result extends Error {
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
            static final long serialVersionUID = -5942088234594905625L;
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
            List<JCTree> path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
            Result(List<JCTree> path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   687
                this.path = path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
        class PathFinder extends TreeScanner {
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
            List<JCTree> path = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
            public void scan(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
                if (tree != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
                    path = path.prepend(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
                    if (tree == node)
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
                        throw new Result(path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
                    super.scan(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
                    path = path.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
            new PathFinder().scan(unit);
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
        } catch (Result result) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
            return result.path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
        return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
    /** Return the statement referenced by a label.
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
     *  If the label refers to a loop or switch, return that switch
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
     *  otherwise return the labelled statement itself
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
    public static JCTree referencedStatement(JCLabeledStatement tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
        JCTree t = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
        do t = ((JCLabeledStatement) t).body;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   717
        while (t.hasTag(LABELLED));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
        switch (t.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   719
        case DOLOOP: case WHILELOOP: case FORLOOP: case FOREACHLOOP: case SWITCH:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
            return tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
    /** Skip parens and return the enclosed expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
    public static JCExpression skipParens(JCExpression tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   729
        while (tree.hasTag(PARENS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
            tree = ((JCParens) tree).expr;
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
        return tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
    /** Skip parens and return the enclosed expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
    public static JCTree skipParens(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   738
        if (tree.hasTag(PARENS))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
            return skipParens((JCParens)tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
            return tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
    /** Return the types of a list of trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
    public static List<Type> types(List<? extends JCTree> trees) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 18414
diff changeset
   747
        ListBuffer<Type> ts = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
        for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
            ts.append(l.head.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
        return ts.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
    /** If this tree is an identifier or a field or a parameterized type,
06bc494ca11e Initial load
duke
parents:
diff changeset
   754
     *  return its name, otherwise return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   755
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   756
    public static Name name(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   757
        switch (tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   758
        case IDENT:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   759
            return ((JCIdent) tree).name;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   760
        case SELECT:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   761
            return ((JCFieldAccess) tree).name;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   762
        case TYPEAPPLY:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   763
            return name(((JCTypeApply) tree).clazz);
06bc494ca11e Initial load
duke
parents:
diff changeset
   764
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
    /** If this tree is a qualified identifier, its return fully qualified name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   770
     *  otherwise return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   771
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   772
    public static Name fullName(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   773
        tree = skipParens(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   774
        switch (tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   775
        case IDENT:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   776
            return ((JCIdent) tree).name;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   777
        case SELECT:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   778
            Name sname = fullName(((JCFieldAccess) tree).selected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
            return sname == null ? null : sname.append('.', name(tree));
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
    public static Symbol symbolFor(JCTree node) {
17557
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   786
        Symbol sym = symbolForImpl(node);
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   787
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   788
        return sym != null ? sym.baseSymbol() : null;
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   789
    }
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   790
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   791
    private static Symbol symbolForImpl(JCTree node) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   792
        node = skipParens(node);
06bc494ca11e Initial load
duke
parents:
diff changeset
   793
        switch (node.getTag()) {
17557
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   794
        case TOPLEVEL:
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   795
            return ((JCCompilationUnit) node).packge;
24069
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 22440
diff changeset
   796
        case PACKAGEDEF:
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 22440
diff changeset
   797
            return ((JCPackageDecl) node).packge;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   798
        case CLASSDEF:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   799
            return ((JCClassDecl) node).sym;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   800
        case METHODDEF:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
            return ((JCMethodDecl) node).sym;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   802
        case VARDEF:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
            return ((JCVariableDecl) node).sym;
17557
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   804
        case IDENT:
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   805
            return ((JCIdent) node).sym;
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   806
        case SELECT:
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   807
            return ((JCFieldAccess) node).sym;
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   808
        case REFERENCE:
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   809
            return ((JCMemberReference) node).sym;
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   810
        case NEWCLASS:
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   811
            return ((JCNewClass) node).constructor;
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   812
        case APPLY:
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   813
            return symbolFor(((JCMethodInvocation) node).meth);
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   814
        case TYPEAPPLY:
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   815
            return symbolFor(((JCTypeApply) node).clazz);
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   816
        case ANNOTATION:
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   817
        case TYPE_ANNOTATION:
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   818
        case TYPEPARAMETER:
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   819
            if (node.type != null)
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   820
                return node.type.tsym;
9c6ace1881fe 8012929: Trees.getElement should work not only for declaration trees, but also for use-trees
jlahoda
parents: 16975
diff changeset
   821
            return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   824
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   826
6590
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 6586
diff changeset
   827
    public static boolean isDeclaration(JCTree node) {
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 6586
diff changeset
   828
        node = skipParens(node);
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 6586
diff changeset
   829
        switch (node.getTag()) {
24069
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 22440
diff changeset
   830
        case PACKAGEDEF:
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   831
        case CLASSDEF:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   832
        case METHODDEF:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   833
        case VARDEF:
6590
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 6586
diff changeset
   834
            return true;
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 6586
diff changeset
   835
        default:
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 6586
diff changeset
   836
            return false;
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 6586
diff changeset
   837
        }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 6586
diff changeset
   838
    }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 6586
diff changeset
   839
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   840
    /** If this tree is an identifier or a field, return its symbol,
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
     *  otherwise return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   842
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
    public static Symbol symbol(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
        tree = skipParens(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
        switch (tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   846
        case IDENT:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
            return ((JCIdent) tree).sym;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   848
        case SELECT:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
            return ((JCFieldAccess) tree).sym;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   850
        case TYPEAPPLY:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
            return symbol(((JCTypeApply) tree).clazz);
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   852
        case ANNOTATED_TYPE:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
   853
            return symbol(((JCAnnotatedType) tree).underlyingType);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   856
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   858
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
    /** Return true if this is a nonstatic selection. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
    public static boolean nonstaticSelect(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
        tree = skipParens(tree);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   862
        if (!tree.hasTag(SELECT)) return false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
        JCFieldAccess s = (JCFieldAccess) tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   864
        Symbol e = symbol(s.selected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
        return e == null || (e.kind != Kinds.PCK && e.kind != Kinds.TYP);
06bc494ca11e Initial load
duke
parents:
diff changeset
   866
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
    /** If this tree is an identifier or a field, set its symbol, otherwise skip.
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   870
    public static void setSymbol(JCTree tree, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
        tree = skipParens(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   872
        switch (tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   873
        case IDENT:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
            ((JCIdent) tree).sym = sym; break;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   875
        case SELECT:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   876
            ((JCFieldAccess) tree).sym = sym; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   878
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   880
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
    /** If this tree is a declaration or a block, return its flags field,
06bc494ca11e Initial load
duke
parents:
diff changeset
   882
     *  otherwise return 0.
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   884
    public static long flags(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
        switch (tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   886
        case VARDEF:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
            return ((JCVariableDecl) tree).mods.flags;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   888
        case METHODDEF:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
            return ((JCMethodDecl) tree).mods.flags;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   890
        case CLASSDEF:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
            return ((JCClassDecl) tree).mods.flags;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   892
        case BLOCK:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
            return ((JCBlock) tree).flags;
06bc494ca11e Initial load
duke
parents:
diff changeset
   894
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
            return 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   896
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
    /** Return first (smallest) flag in `flags':
06bc494ca11e Initial load
duke
parents:
diff changeset
   900
     *  pre: flags != 0
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
    public static long firstFlag(long flags) {
14267
6321fbe0cf50 7192245: Add parser support for default methods
mcimadamore
parents: 14062
diff changeset
   903
        long flag = 1;
6321fbe0cf50 7192245: Add parser support for default methods
mcimadamore
parents: 14062
diff changeset
   904
        while ((flag & flags & ExtendedStandardFlags) == 0)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
            flag = flag << 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
        return flag;
06bc494ca11e Initial load
duke
parents:
diff changeset
   907
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   908
06bc494ca11e Initial load
duke
parents:
diff changeset
   909
    /** Return flags as a string, separated by " ".
06bc494ca11e Initial load
duke
parents:
diff changeset
   910
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   911
    public static String flagNames(long flags) {
14267
6321fbe0cf50 7192245: Add parser support for default methods
mcimadamore
parents: 14062
diff changeset
   912
        return Flags.toString(flags & ExtendedStandardFlags).trim();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   913
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   914
06bc494ca11e Initial load
duke
parents:
diff changeset
   915
    /** Operator precedences values.
06bc494ca11e Initial load
duke
parents:
diff changeset
   916
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   917
    public static final int
06bc494ca11e Initial load
duke
parents:
diff changeset
   918
        notExpression = -1,   // not an expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   919
        noPrec = 0,           // no enclosing expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   920
        assignPrec = 1,
06bc494ca11e Initial load
duke
parents:
diff changeset
   921
        assignopPrec = 2,
06bc494ca11e Initial load
duke
parents:
diff changeset
   922
        condPrec = 3,
06bc494ca11e Initial load
duke
parents:
diff changeset
   923
        orPrec = 4,
06bc494ca11e Initial load
duke
parents:
diff changeset
   924
        andPrec = 5,
06bc494ca11e Initial load
duke
parents:
diff changeset
   925
        bitorPrec = 6,
06bc494ca11e Initial load
duke
parents:
diff changeset
   926
        bitxorPrec = 7,
06bc494ca11e Initial load
duke
parents:
diff changeset
   927
        bitandPrec = 8,
06bc494ca11e Initial load
duke
parents:
diff changeset
   928
        eqPrec = 9,
06bc494ca11e Initial load
duke
parents:
diff changeset
   929
        ordPrec = 10,
06bc494ca11e Initial load
duke
parents:
diff changeset
   930
        shiftPrec = 11,
06bc494ca11e Initial load
duke
parents:
diff changeset
   931
        addPrec = 12,
06bc494ca11e Initial load
duke
parents:
diff changeset
   932
        mulPrec = 13,
06bc494ca11e Initial load
duke
parents:
diff changeset
   933
        prefixPrec = 14,
06bc494ca11e Initial load
duke
parents:
diff changeset
   934
        postfixPrec = 15,
06bc494ca11e Initial load
duke
parents:
diff changeset
   935
        precCount = 16;
06bc494ca11e Initial load
duke
parents:
diff changeset
   936
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
06bc494ca11e Initial load
duke
parents:
diff changeset
   938
    /** Map operators to their precedence levels.
06bc494ca11e Initial load
duke
parents:
diff changeset
   939
     */
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   940
    public static int opPrec(JCTree.Tag op) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   941
        switch(op) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   942
        case POS:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   943
        case NEG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   944
        case NOT:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   945
        case COMPL:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   946
        case PREINC:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   947
        case PREDEC: return prefixPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   948
        case POSTINC:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   949
        case POSTDEC:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   950
        case NULLCHK: return postfixPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   951
        case ASSIGN: return assignPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   952
        case BITOR_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   953
        case BITXOR_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   954
        case BITAND_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   955
        case SL_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   956
        case SR_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   957
        case USR_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   958
        case PLUS_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   959
        case MINUS_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   960
        case MUL_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   961
        case DIV_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   962
        case MOD_ASG: return assignopPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   963
        case OR: return orPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   964
        case AND: return andPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   965
        case EQ:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   966
        case NE: return eqPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   967
        case LT:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   968
        case GT:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   969
        case LE:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   970
        case GE: return ordPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   971
        case BITOR: return bitorPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   972
        case BITXOR: return bitxorPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   973
        case BITAND: return bitandPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   974
        case SL:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   975
        case SR:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   976
        case USR: return shiftPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   977
        case PLUS:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   978
        case MINUS: return addPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   979
        case MUL:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   980
        case DIV:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   981
        case MOD: return mulPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   982
        case TYPETEST: return ordPrec;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   983
        default: throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
   984
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   985
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   986
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   987
    static Tree.Kind tagToKind(JCTree.Tag tag) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   988
        switch (tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   989
        // Postfix expressions
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   990
        case POSTINC:           // _ ++
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   991
            return Tree.Kind.POSTFIX_INCREMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   992
        case POSTDEC:           // _ --
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   993
            return Tree.Kind.POSTFIX_DECREMENT;
06bc494ca11e Initial load
duke
parents:
diff changeset
   994
06bc494ca11e Initial load
duke
parents:
diff changeset
   995
        // Unary operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   996
        case PREINC:            // ++ _
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   997
            return Tree.Kind.PREFIX_INCREMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   998
        case PREDEC:            // -- _
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   999
            return Tree.Kind.PREFIX_DECREMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1000
        case POS:               // +
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1001
            return Tree.Kind.UNARY_PLUS;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1002
        case NEG:               // -
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1003
            return Tree.Kind.UNARY_MINUS;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1004
        case COMPL:             // ~
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1005
            return Tree.Kind.BITWISE_COMPLEMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1006
        case NOT:               // !
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1007
            return Tree.Kind.LOGICAL_COMPLEMENT;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1008
06bc494ca11e Initial load
duke
parents:
diff changeset
  1009
        // Binary operators
06bc494ca11e Initial load
duke
parents:
diff changeset
  1010
06bc494ca11e Initial load
duke
parents:
diff changeset
  1011
        // Multiplicative operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1012
        case MUL:               // *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1013
            return Tree.Kind.MULTIPLY;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1014
        case DIV:               // /
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1015
            return Tree.Kind.DIVIDE;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1016
        case MOD:               // %
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1017
            return Tree.Kind.REMAINDER;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1018
06bc494ca11e Initial load
duke
parents:
diff changeset
  1019
        // Additive operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1020
        case PLUS:              // +
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1021
            return Tree.Kind.PLUS;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1022
        case MINUS:             // -
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1023
            return Tree.Kind.MINUS;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1024
06bc494ca11e Initial load
duke
parents:
diff changeset
  1025
        // Shift operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1026
        case SL:                // <<
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1027
            return Tree.Kind.LEFT_SHIFT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1028
        case SR:                // >>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1029
            return Tree.Kind.RIGHT_SHIFT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1030
        case USR:               // >>>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1031
            return Tree.Kind.UNSIGNED_RIGHT_SHIFT;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1032
06bc494ca11e Initial load
duke
parents:
diff changeset
  1033
        // Relational operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1034
        case LT:                // <
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1035
            return Tree.Kind.LESS_THAN;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1036
        case GT:                // >
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1037
            return Tree.Kind.GREATER_THAN;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1038
        case LE:                // <=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1039
            return Tree.Kind.LESS_THAN_EQUAL;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1040
        case GE:                // >=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1041
            return Tree.Kind.GREATER_THAN_EQUAL;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1042
06bc494ca11e Initial load
duke
parents:
diff changeset
  1043
        // Equality operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1044
        case EQ:                // ==
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1045
            return Tree.Kind.EQUAL_TO;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1046
        case NE:                // !=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1047
            return Tree.Kind.NOT_EQUAL_TO;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1048
06bc494ca11e Initial load
duke
parents:
diff changeset
  1049
        // Bitwise and logical operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1050
        case BITAND:            // &
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1051
            return Tree.Kind.AND;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1052
        case BITXOR:            // ^
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1053
            return Tree.Kind.XOR;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1054
        case BITOR:             // |
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1055
            return Tree.Kind.OR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1056
06bc494ca11e Initial load
duke
parents:
diff changeset
  1057
        // Conditional operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1058
        case AND:               // &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1059
            return Tree.Kind.CONDITIONAL_AND;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1060
        case OR:                // ||
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1061
            return Tree.Kind.CONDITIONAL_OR;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1062
06bc494ca11e Initial load
duke
parents:
diff changeset
  1063
        // Assignment operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1064
        case MUL_ASG:           // *=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1065
            return Tree.Kind.MULTIPLY_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1066
        case DIV_ASG:           // /=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1067
            return Tree.Kind.DIVIDE_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1068
        case MOD_ASG:           // %=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1069
            return Tree.Kind.REMAINDER_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1070
        case PLUS_ASG:          // +=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1071
            return Tree.Kind.PLUS_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1072
        case MINUS_ASG:         // -=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1073
            return Tree.Kind.MINUS_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1074
        case SL_ASG:            // <<=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1075
            return Tree.Kind.LEFT_SHIFT_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1076
        case SR_ASG:            // >>=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1077
            return Tree.Kind.RIGHT_SHIFT_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1078
        case USR_ASG:           // >>>=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1079
            return Tree.Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1080
        case BITAND_ASG:        // &=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1081
            return Tree.Kind.AND_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1082
        case BITXOR_ASG:        // ^=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1083
            return Tree.Kind.XOR_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1084
        case BITOR_ASG:         // |=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1085
            return Tree.Kind.OR_ASSIGNMENT;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1086
06bc494ca11e Initial load
duke
parents:
diff changeset
  1087
        // Null check (implementation detail), for example, __.getClass()
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1088
        case NULLCHK:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1089
            return Tree.Kind.OTHER;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1090
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1091
        case ANNOTATION:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1092
            return Tree.Kind.ANNOTATION;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1093
        case TYPE_ANNOTATION:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1094
            return Tree.Kind.TYPE_ANNOTATION;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1095
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1096
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
  1097
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
  1098
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
  1099
    }
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
  1100
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
  1101
    /**
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1102
     * Returns the underlying type of the tree if it is an annotated type,
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1103
     * or the tree itself otherwise.
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
  1104
     */
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
  1105
    public static JCExpression typeIn(JCExpression tree) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
  1106
        switch (tree.getTag()) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1107
        case ANNOTATED_TYPE:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1108
            return ((JCAnnotatedType)tree).underlyingType;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1109
        case IDENT: /* simple names */
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1110
        case TYPEIDENT: /* primitive name */
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1111
        case SELECT: /* qualified name */
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1112
        case TYPEARRAY: /* array types */
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1113
        case WILDCARD: /* wild cards */
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1114
        case TYPEPARAMETER: /* type parameters */
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
  1115
        case TYPEAPPLY: /* parameterized types */
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1116
        case ERRONEOUS: /* error tree TODO: needed for BadCast JSR308 test case. Better way? */
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
  1117
            return tree;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
  1118
        default:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
  1119
            throw new AssertionError("Unexpected type tree: " + tree);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
  1120
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
  1121
    }
4705
7fac532dc6fa 6917122: provide utility method to find the inner most type of a type tree
jjg
parents: 3765
diff changeset
  1122
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1123
    /* Return the inner-most type of a type tree.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1124
     * For an array that contains an annotated type, return that annotated type.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1125
     * TODO: currently only used by Pretty. Describe behavior better.
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1126
     */
4705
7fac532dc6fa 6917122: provide utility method to find the inner most type of a type tree
jjg
parents: 3765
diff changeset
  1127
    public static JCTree innermostType(JCTree type) {
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1128
        JCTree lastAnnotatedType = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1129
        JCTree cur = type;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1130
        loop: while (true) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1131
            switch (cur.getTag()) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1132
            case TYPEARRAY:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1133
                lastAnnotatedType = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1134
                cur = ((JCArrayTypeTree)cur).elemtype;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1135
                break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1136
            case WILDCARD:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1137
                lastAnnotatedType = null;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1138
                cur = ((JCWildcard)cur).inner;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1139
                break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1140
            case ANNOTATED_TYPE:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1141
                lastAnnotatedType = cur;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1142
                cur = ((JCAnnotatedType)cur).underlyingType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1143
                break;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1144
            default:
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1145
                break loop;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1146
            }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1147
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1148
        if (lastAnnotatedType!=null) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1149
            return lastAnnotatedType;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1150
        } else {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1151
            return cur;
4705
7fac532dc6fa 6917122: provide utility method to find the inner most type of a type tree
jjg
parents: 3765
diff changeset
  1152
        }
7fac532dc6fa 6917122: provide utility method to find the inner most type of a type tree
jjg
parents: 3765
diff changeset
  1153
    }
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1154
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1155
    private static class TypeAnnotationFinder extends TreeScanner {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1156
        public boolean foundTypeAnno = false;
18010
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17557
diff changeset
  1157
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17557
diff changeset
  1158
        @Override
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17557
diff changeset
  1159
        public void scan(JCTree tree) {
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17557
diff changeset
  1160
            if (foundTypeAnno || tree == null)
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17557
diff changeset
  1161
                return;
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17557
diff changeset
  1162
            super.scan(tree);
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17557
diff changeset
  1163
        }
604faee85350 8004643: Reduce javac space overhead introduced with compiler support for repeating annotations
jjg
parents: 17557
diff changeset
  1164
15385
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1165
        public void visitAnnotation(JCAnnotation tree) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1166
            foundTypeAnno = foundTypeAnno || tree.hasTag(TYPE_ANNOTATION);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1167
        }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1168
    }
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1169
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1170
    public static boolean containsTypeAnnotation(JCTree e) {
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1171
        TypeAnnotationFinder finder = new TypeAnnotationFinder();
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1172
        finder.scan(e);
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1173
        return finder.foundTypeAnno;
ee1eebe7e210 8006775: JSR 308: Compiler changes in JDK8
jjg
parents: 15374
diff changeset
  1174
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
  1175
}