langtools/src/share/classes/com/sun/tools/javac/tree/TreeInfo.java
author ksrini
Mon, 14 Nov 2011 15:11:10 -0800
changeset 11055 ec1418effa77
parent 10950 e87b50888909
child 11142 45d0ec1e7463
permissions -rw-r--r--
7106166: (javac) re-factor EndPos parser Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
8031
d5fe2c1cecfc 6992999: fully remove JSR 308 from langtools
jjg
parents: 7681
diff changeset
     2
 * Copyright (c) 1999, 2011, 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
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.source.tree.Tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import com.sun.tools.javac.comp.AttrContext;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.comp.Env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.code.*;
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
    34
import com.sun.tools.javac.parser.EndPosTable;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import static com.sun.tools.javac.code.Flags.*;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    38
import static com.sun.tools.javac.tree.JCTree.Tag.*;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    39
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
    40
import static com.sun.tools.javac.tree.JCTree.Tag.SYNCHRONIZED;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
/** Utility class containing inspector methods for trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    44
 *  <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
    45
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
public class TreeInfo {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
    protected static final Context.Key<TreeInfo> treeInfoKey =
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        new Context.Key<TreeInfo>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    public static TreeInfo instance(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        TreeInfo instance = context.get(treeInfoKey);
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        if (instance == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
            instance = new TreeInfo(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    /** The names of all operators.
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     */
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    62
    private Name[] opname = new Name[Tag.getNumberOfOperators()];
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    63
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    64
    private void setOpname(Tag tag, String name, Names names) {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    65
         setOpname(tag, names.fromString(name));
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    66
     }
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    67
     private void setOpname(Tag tag, Name name) {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    68
         opname[tag.operatorIndex()] = name;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    69
     }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    private TreeInfo(Context context) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
        context.put(treeInfoKey, this);
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1206
diff changeset
    74
        Names names = Names.instance(context);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    75
        setOpname(POS, "+", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    76
        setOpname(NEG, names.hyphen);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    77
        setOpname(NOT, "!", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    78
        setOpname(COMPL, "~", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    79
        setOpname(PREINC, "++", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    80
        setOpname(PREDEC, "--", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    81
        setOpname(POSTINC, "++", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    82
        setOpname(POSTDEC, "--", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    83
        setOpname(NULLCHK, "<*nullchk*>", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    84
        setOpname(OR, "||", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    85
        setOpname(AND, "&&", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    86
        setOpname(EQ, "==", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    87
        setOpname(NE, "!=", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    88
        setOpname(LT, "<", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    89
        setOpname(GT, ">", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    90
        setOpname(LE, "<=", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    91
        setOpname(GE, ">=", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    92
        setOpname(BITOR, "|", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    93
        setOpname(BITXOR, "^", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    94
        setOpname(BITAND, "&", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    95
        setOpname(SL, "<<", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    96
        setOpname(SR, ">>", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    97
        setOpname(USR, ">>>", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    98
        setOpname(PLUS, "+", names);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
    99
        setOpname(MINUS, names.hyphen);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   100
        setOpname(MUL, names.asterisk);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   101
        setOpname(DIV, names.slash);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   102
        setOpname(MOD, "%", names);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    /** Return name of operator with given tree tag.
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     */
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   108
    public Name operatorName(JCTree.Tag tag) {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   109
        return opname[tag.operatorIndex()];
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    /** Is tree a constructor declaration?
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    public static boolean isConstructor(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   115
        if (tree.hasTag(METHODDEF)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
            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
   117
            return name == name.table.names.init;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    /** Is there a constructor declaration in the given list of trees?
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    public static boolean hasConstructors(List<JCTree> trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        for (List<JCTree> l = trees; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
            if (isConstructor(l.head)) return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
   131
    public static boolean isMultiCatch(JCCatch catchClause) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   132
        return catchClause.param.vartype.hasTag(TYPEUNION);
5492
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
   133
    }
515e4b33b335 6943289: Project Coin: Improved Exception Handling for Java (aka 'multicatch')
mcimadamore
parents: 5321
diff changeset
   134
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    /** Is statement an initializer for a synthetic field?
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    public static boolean isSyntheticInit(JCTree stat) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   138
        if (stat.hasTag(EXEC)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
            JCExpressionStatement exec = (JCExpressionStatement)stat;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   140
            if (exec.expr.hasTag(ASSIGN)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
                JCAssign assign = (JCAssign)exec.expr;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   142
                if (assign.lhs.hasTag(SELECT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
                    JCFieldAccess select = (JCFieldAccess)assign.lhs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
                    if (select.sym != null &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
                        (select.sym.flags() & SYNTHETIC) != 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
                        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
   147
                        if (selected != null && selected == selected.table.names._this)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
                            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    /** If the expression is a method call, return the method name, null
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     *  otherwise. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
    public static Name calledMethodName(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   159
        if (tree.hasTag(EXEC)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
            JCExpressionStatement exec = (JCExpressionStatement)tree;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   161
            if (exec.expr.hasTag(APPLY)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
                Name mname = TreeInfo.name(((JCMethodInvocation) exec.expr).meth);
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
                return mname;
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
    /** Is this a call to this or super?
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
    public static boolean isSelfCall(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        Name name = calledMethodName(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        if (name != null) {
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1206
diff changeset
   174
            Names names = name.table.names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
            return name==names._this || name==names._super;
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
    /** Is this a call to super?
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
    public static boolean isSuperCall(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        Name name = calledMethodName(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        if (name != null) {
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 1206
diff changeset
   186
            Names names = name.table.names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
            return name==names._super;
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    /** Is this a constructor whose first (non-synthetic) statement is not
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
     *  of the form this(...)?
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
    public static boolean isInitialConstructor(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        JCMethodInvocation app = firstConstructorCall(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        if (app == null) return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        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
   200
        return meth == null || meth != meth.table.names._this;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
    /** Return the first call in a constructor definition. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    public static JCMethodInvocation firstConstructorCall(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   205
        if (!tree.hasTag(METHODDEF)) return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
        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
   207
        Names names = md.name.table.names;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        if (md.name != names.init) return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        if (md.body == null) return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        List<JCStatement> stats = md.body.stats;
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
        // Synthetic initializations can appear before the super call.
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        while (stats.nonEmpty() && isSyntheticInit(stats.head))
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
            stats = stats.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        if (stats.isEmpty()) return null;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   215
        if (!stats.head.hasTag(EXEC)) return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        JCExpressionStatement exec = (JCExpressionStatement) stats.head;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   217
        if (!exec.expr.hasTag(APPLY)) return null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        return (JCMethodInvocation)exec.expr;
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   221
    /** Return true if a tree represents a diamond new expr. */
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   222
    public static boolean isDiamond(JCTree tree) {
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   223
        switch(tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   224
            case TYPEAPPLY: return ((JCTypeApply)tree).getTypeArguments().isEmpty();
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   225
            case NEWCLASS: return isDiamond(((JCNewClass)tree).clazz);
5321
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   226
            default: return false;
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   227
        }
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   228
    }
c8efe769cb3b 6939620: Switch to 'complex' diamond inference scheme
mcimadamore
parents: 5320
diff changeset
   229
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
    /** Return true if a tree represents the null literal. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
    public static boolean isNull(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   232
        if (!tree.hasTag(LITERAL))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        JCLiteral lit = (JCLiteral) tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        return (lit.typetag == TypeTags.BOT);
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
    /** The position of the first statement in a block, or the position of
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
     *  the block itself if it is empty.
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
    public static int firstStatPos(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   242
        if (tree.hasTag(BLOCK) && ((JCBlock) tree).stats.nonEmpty())
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
            return ((JCBlock) tree).stats.head.pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
            return tree.pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
    /** The end position of given tree, if it is a block with
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
     *  defined endpos.
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    public static int endPos(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   252
        if (tree.hasTag(BLOCK) && ((JCBlock) tree).endpos != Position.NOPOS)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            return ((JCBlock) tree).endpos;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   254
        else if (tree.hasTag(SYNCHRONIZED))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
            return endPos(((JCSynchronized) tree).body);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   256
        else if (tree.hasTag(TRY)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
            JCTry t = (JCTry) tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            return endPos((t.finalizer != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
                          ? t.finalizer
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
                          : t.catchers.last().body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
            return tree.pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
    /** Get the start position for a tree node.  The start position is
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
     * defined to be the position of the first character of the first
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
     * token of the node's source text.
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
     * @param tree  The tree node
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
    public static int getStartPos(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        if (tree == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
            return Position.NOPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
        switch(tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   276
            case APPLY:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   277
                return getStartPos(((JCMethodInvocation) tree).meth);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   278
            case ASSIGN:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   279
                return getStartPos(((JCAssign) tree).lhs);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   280
            case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   281
            case SL_ASG: case SR_ASG: case USR_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   282
            case PLUS_ASG: case MINUS_ASG: case MUL_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   283
            case DIV_ASG: case MOD_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   284
                return getStartPos(((JCAssignOp) tree).lhs);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   285
            case OR: case AND: case BITOR:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   286
            case BITXOR: case BITAND: case EQ:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   287
            case NE: case LT: case GT:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   288
            case LE: case GE: case SL:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   289
            case SR: case USR: case PLUS:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   290
            case MINUS: case MUL: case DIV:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   291
            case MOD:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   292
                return getStartPos(((JCBinary) tree).lhs);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   293
            case CLASSDEF: {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   294
                JCClassDecl node = (JCClassDecl)tree;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   295
                if (node.mods.pos != Position.NOPOS)
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   296
                    return node.mods.pos;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   297
                break;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
            }
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   299
            case CONDEXPR:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   300
                return getStartPos(((JCConditional) tree).cond);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   301
            case EXEC:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   302
                return getStartPos(((JCExpressionStatement) tree).expr);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   303
            case INDEXED:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   304
                return getStartPos(((JCArrayAccess) tree).indexed);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   305
            case METHODDEF: {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   306
                JCMethodDecl node = (JCMethodDecl)tree;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   307
                if (node.mods.pos != Position.NOPOS)
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   308
                    return node.mods.pos;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   309
                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
   310
                    return getStartPos(node.typarams.head);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   311
                return node.restype == null ? node.pos : getStartPos(node.restype);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   312
            }
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   313
            case SELECT:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   314
                return getStartPos(((JCFieldAccess) tree).selected);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   315
            case TYPEAPPLY:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   316
                return getStartPos(((JCTypeApply) tree).clazz);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   317
            case TYPEARRAY:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   318
                return getStartPos(((JCArrayTypeTree) tree).elemtype);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   319
            case TYPETEST:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   320
                return getStartPos(((JCInstanceOf) tree).expr);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   321
            case POSTINC:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   322
            case POSTDEC:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   323
                return getStartPos(((JCUnary) tree).arg);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   324
            case NEWCLASS: {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   325
                JCNewClass node = (JCNewClass)tree;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   326
                if (node.encl != null)
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   327
                    return getStartPos(node.encl);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   328
                break;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   329
            }
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   330
            case VARDEF: {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   331
                JCVariableDecl node = (JCVariableDecl)tree;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   332
                if (node.mods.pos != Position.NOPOS) {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   333
                    return node.mods.pos;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   334
                } else {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   335
                    return getStartPos(node.vartype);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   336
                }
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   337
            }
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   338
            case ERRONEOUS: {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   339
                JCErroneous node = (JCErroneous)tree;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   340
                if (node.errs != null && node.errs.nonEmpty())
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   341
                    return getStartPos(node.errs.head);
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   342
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
        return tree.pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
    /** The end position of given tree, given  a table of end positions generated by the parser
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
     */
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   349
    public static int getEndPos(JCTree tree, EndPosTable endPosTable) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
        if (tree == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
            return Position.NOPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   353
        if (endPosTable == null) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   354
            // fall back on limited info in the tree
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
            return endPos(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   358
        int mapPos = endPosTable.getEndPos(tree);
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   359
        if (mapPos != Position.NOPOS)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
            return mapPos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
06bc494ca11e Initial load
duke
parents:
diff changeset
   362
        switch(tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   363
            case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   364
            case SL_ASG: case SR_ASG: case USR_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   365
            case PLUS_ASG: case MINUS_ASG: case MUL_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   366
            case DIV_ASG: case MOD_ASG:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   367
                return getEndPos(((JCAssignOp) tree).rhs, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   368
            case OR: case AND: case BITOR:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   369
            case BITXOR: case BITAND: case EQ:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   370
            case NE: case LT: case GT:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   371
            case LE: case GE: case SL:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   372
            case SR: case USR: case PLUS:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   373
            case MINUS: case MUL: case DIV:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   374
            case MOD:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   375
                return getEndPos(((JCBinary) tree).rhs, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   376
            case CASE:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   377
                return getEndPos(((JCCase) tree).stats.last(), endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   378
            case CATCH:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   379
                return getEndPos(((JCCatch) tree).body, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   380
            case CONDEXPR:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   381
                return getEndPos(((JCConditional) tree).falsepart, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   382
            case FORLOOP:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   383
                return getEndPos(((JCForLoop) tree).body, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   384
            case FOREACHLOOP:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   385
                return getEndPos(((JCEnhancedForLoop) tree).body, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   386
            case IF: {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   387
                JCIf node = (JCIf)tree;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   388
                if (node.elsepart == null) {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   389
                    return getEndPos(node.thenpart, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   390
                } else {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   391
                    return getEndPos(node.elsepart, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   392
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   393
            }
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   394
            case LABELLED:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   395
                return getEndPos(((JCLabeledStatement) tree).body, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   396
            case MODIFIERS:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   397
                return getEndPos(((JCModifiers) tree).annotations.last(), endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   398
            case SYNCHRONIZED:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   399
                return getEndPos(((JCSynchronized) tree).body, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   400
            case TOPLEVEL:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   401
                return getEndPos(((JCCompilationUnit) tree).defs.last(), endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   402
            case TRY: {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   403
                JCTry node = (JCTry)tree;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   404
                if (node.finalizer != null) {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   405
                    return getEndPos(node.finalizer, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   406
                } else if (!node.catchers.isEmpty()) {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   407
                    return getEndPos(node.catchers.last(), endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   408
                } else {
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   409
                    return getEndPos(node.body, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   410
                }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
            }
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   412
            case WILDCARD:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   413
                return getEndPos(((JCWildcard) tree).inner, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   414
            case TYPECAST:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   415
                return getEndPos(((JCTypeCast) tree).expr, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   416
            case TYPETEST:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   417
                return getEndPos(((JCInstanceOf) tree).clazz, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   418
            case POS:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   419
            case NEG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   420
            case NOT:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   421
            case COMPL:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   422
            case PREINC:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   423
            case PREDEC:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   424
                return getEndPos(((JCUnary) tree).arg, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   425
            case WHILELOOP:
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   426
                return getEndPos(((JCWhileLoop) tree).body, endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   427
            case ERRONEOUS: {
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   428
                JCErroneous node = (JCErroneous)tree;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   429
                if (node.errs != null && node.errs.nonEmpty())
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   430
                    return getEndPos(node.errs.last(), endPosTable);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   431
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
        return Position.NOPOS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   435
06bc494ca11e Initial load
duke
parents:
diff changeset
   436
06bc494ca11e Initial load
duke
parents:
diff changeset
   437
    /** A DiagnosticPosition with the preferred position set to the
06bc494ca11e Initial load
duke
parents:
diff changeset
   438
     *  end position of given tree, if it is a block with
06bc494ca11e Initial load
duke
parents:
diff changeset
   439
     *  defined endpos.
06bc494ca11e Initial load
duke
parents:
diff changeset
   440
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   441
    public static DiagnosticPosition diagEndPos(final JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   442
        final int endPos = TreeInfo.endPos(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   443
        return new DiagnosticPosition() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
            public JCTree getTree() { return tree; }
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
            public int getStartPosition() { return TreeInfo.getStartPos(tree); }
06bc494ca11e Initial load
duke
parents:
diff changeset
   446
            public int getPreferredPosition() { return endPos; }
11055
ec1418effa77 7106166: (javac) re-factor EndPos parser
ksrini
parents: 10950
diff changeset
   447
            public int getEndPosition(EndPosTable endPosTable) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
                return TreeInfo.getEndPos(tree, endPosTable);
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   450
        };
06bc494ca11e Initial load
duke
parents:
diff changeset
   451
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   452
06bc494ca11e Initial load
duke
parents:
diff changeset
   453
    /** The position of the finalizer of given try/synchronized statement.
06bc494ca11e Initial load
duke
parents:
diff changeset
   454
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   455
    public static int finalizerPos(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   456
        if (tree.hasTag(TRY)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   457
            JCTry t = (JCTry) tree;
8032
e1aa25ccdabb 6396503: javac should not require assertions enabled
jjg
parents: 8031
diff changeset
   458
            Assert.checkNonNull(t.finalizer);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   459
            return firstStatPos(t.finalizer);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   460
        } else if (tree.hasTag(SYNCHRONIZED)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   461
            return endPos(((JCSynchronized) tree).body);
06bc494ca11e Initial load
duke
parents:
diff changeset
   462
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   463
            throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
   464
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   465
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   466
06bc494ca11e Initial load
duke
parents:
diff changeset
   467
    /** Find the position for reporting an error about a symbol, where
06bc494ca11e Initial load
duke
parents:
diff changeset
   468
     *  that symbol is defined somewhere in the given tree. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   469
    public static int positionFor(final Symbol sym, final JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   470
        JCTree decl = declarationFor(sym, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   471
        return ((decl != null) ? decl : tree).pos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   472
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   473
06bc494ca11e Initial load
duke
parents:
diff changeset
   474
    /** Find the position for reporting an error about a symbol, where
06bc494ca11e Initial load
duke
parents:
diff changeset
   475
     *  that symbol is defined somewhere in the given tree. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   476
    public static DiagnosticPosition diagnosticPositionFor(final Symbol sym, final JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   477
        JCTree decl = declarationFor(sym, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   478
        return ((decl != null) ? decl : tree).pos();
06bc494ca11e Initial load
duke
parents:
diff changeset
   479
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   480
06bc494ca11e Initial load
duke
parents:
diff changeset
   481
    /** Find the declaration for a symbol, where
06bc494ca11e Initial load
duke
parents:
diff changeset
   482
     *  that symbol is defined somewhere in the given tree. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   483
    public static JCTree declarationFor(final Symbol sym, final JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   484
        class DeclScanner extends TreeScanner {
06bc494ca11e Initial load
duke
parents:
diff changeset
   485
            JCTree result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   486
            public void scan(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   487
                if (tree!=null && result==null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   488
                    tree.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   489
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   490
            public void visitTopLevel(JCCompilationUnit that) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   491
                if (that.packge == sym) result = that;
06bc494ca11e Initial load
duke
parents:
diff changeset
   492
                else super.visitTopLevel(that);
06bc494ca11e Initial load
duke
parents:
diff changeset
   493
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   494
            public void visitClassDef(JCClassDecl that) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   495
                if (that.sym == sym) result = that;
06bc494ca11e Initial load
duke
parents:
diff changeset
   496
                else super.visitClassDef(that);
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
            public void visitMethodDef(JCMethodDecl that) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
                if (that.sym == sym) result = that;
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
                else super.visitMethodDef(that);
06bc494ca11e Initial load
duke
parents:
diff changeset
   501
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
            public void visitVarDef(JCVariableDecl that) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
                if (that.sym == sym) result = that;
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
                else super.visitVarDef(that);
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
            }
6586
0d40dc0c06cb 6458823: Messager messages on TypeParamterElements to not include position information.
sundar
parents: 5847
diff changeset
   506
            public void visitTypeParameter(JCTypeParameter that) {
7619
1bedd48ff024 6990209: JCK7-compiler lang/ICLS/icls006/icls00603/icls00603a.html#icls00603src test fails.
jjh
parents: 7074
diff changeset
   507
                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
   508
                else super.visitTypeParameter(that);
0d40dc0c06cb 6458823: Messager messages on TypeParamterElements to not include position information.
sundar
parents: 5847
diff changeset
   509
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
        DeclScanner s = new DeclScanner();
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
        tree.accept(s);
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
        return s.result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
    public static Env<AttrContext> scopeFor(JCTree node, JCCompilationUnit unit) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
        return scopeFor(pathFor(node, unit));
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
    public static Env<AttrContext> scopeFor(List<JCTree> path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
        // TODO: not implemented yet
06bc494ca11e Initial load
duke
parents:
diff changeset
   522
        throw new UnsupportedOperationException("not implemented yet");
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
06bc494ca11e Initial load
duke
parents:
diff changeset
   525
    public static List<JCTree> pathFor(final JCTree node, final JCCompilationUnit unit) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
        class Result extends Error {
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
            static final long serialVersionUID = -5942088234594905625L;
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
            List<JCTree> path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
            Result(List<JCTree> path) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
                this.path = path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   532
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
        class PathFinder extends TreeScanner {
06bc494ca11e Initial load
duke
parents:
diff changeset
   534
            List<JCTree> path = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   535
            public void scan(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   536
                if (tree != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   537
                    path = path.prepend(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   538
                    if (tree == node)
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
                        throw new Result(path);
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
                    super.scan(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
                    path = path.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   543
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   544
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
            new PathFinder().scan(unit);
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
        } catch (Result result) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   548
            return result.path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   549
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
        return List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
    /** Return the statement referenced by a label.
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
     *  If the label refers to a loop or switch, return that switch
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
     *  otherwise return the labelled statement itself
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
    public static JCTree referencedStatement(JCLabeledStatement tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
        JCTree t = tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
        do t = ((JCLabeledStatement) t).body;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   560
        while (t.hasTag(LABELLED));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
        switch (t.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   562
        case DOLOOP: case WHILELOOP: case FORLOOP: case FOREACHLOOP: case SWITCH:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   563
            return t;
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
            return tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   566
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
    /** Skip parens and return the enclosed expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
    public static JCExpression skipParens(JCExpression tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   572
        while (tree.hasTag(PARENS)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   573
            tree = ((JCParens) tree).expr;
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
        return tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
    /** Skip parens and return the enclosed expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
    public static JCTree skipParens(JCTree tree) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   581
        if (tree.hasTag(PARENS))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
            return skipParens((JCParens)tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
        else
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
            return tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
06bc494ca11e Initial load
duke
parents:
diff changeset
   587
    /** Return the types of a list of trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
    public static List<Type> types(List<? extends JCTree> trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
        ListBuffer<Type> ts = new ListBuffer<Type>();
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
        for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail)
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
            ts.append(l.head.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
        return ts.toList();
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
    /** If this tree is an identifier or a field or a parameterized type,
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
     *  return its name, otherwise return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
    public static Name name(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
        switch (tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   601
        case IDENT:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
            return ((JCIdent) tree).name;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   603
        case SELECT:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
            return ((JCFieldAccess) tree).name;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   605
        case TYPEAPPLY:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
            return name(((JCTypeApply) tree).clazz);
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
    /** If this tree is a qualified identifier, its return fully qualified name,
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
     *  otherwise return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
    public static Name fullName(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
        tree = skipParens(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
        switch (tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   618
        case IDENT:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
            return ((JCIdent) tree).name;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   620
        case SELECT:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
            Name sname = fullName(((JCFieldAccess) tree).selected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
            return sname == null ? null : sname.append('.', name(tree));
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
    public static Symbol symbolFor(JCTree node) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
        node = skipParens(node);
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
        switch (node.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   631
        case CLASSDEF:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
            return ((JCClassDecl) node).sym;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   633
        case METHODDEF:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
            return ((JCMethodDecl) node).sym;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   635
        case VARDEF:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   636
            return ((JCVariableDecl) node).sym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   639
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
6590
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 6586
diff changeset
   642
    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
   643
        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
   644
        switch (node.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   645
        case CLASSDEF:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   646
        case METHODDEF:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   647
        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
   648
            return true;
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 6586
diff changeset
   649
        default:
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 6586
diff changeset
   650
            return false;
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 6586
diff changeset
   651
        }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 6586
diff changeset
   652
    }
f745e683da2c 6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents: 6586
diff changeset
   653
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
    /** If this tree is an identifier or a field, return its symbol,
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
     *  otherwise return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
    public static Symbol symbol(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
        tree = skipParens(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
        switch (tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   660
        case IDENT:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
            return ((JCIdent) tree).sym;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   662
        case SELECT:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
            return ((JCFieldAccess) tree).sym;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   664
        case TYPEAPPLY:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
            return symbol(((JCTypeApply) tree).clazz);
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
    /** Return true if this is a nonstatic selection. */
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
    public static boolean nonstaticSelect(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
        tree = skipParens(tree);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   674
        if (!tree.hasTag(SELECT)) return false;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
        JCFieldAccess s = (JCFieldAccess) tree;
06bc494ca11e Initial load
duke
parents:
diff changeset
   676
        Symbol e = symbol(s.selected);
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
        return e == null || (e.kind != Kinds.PCK && e.kind != Kinds.TYP);
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
    /** If this tree is an identifier or a field, set its symbol, otherwise skip.
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   682
    public static void setSymbol(JCTree tree, Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
        tree = skipParens(tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   684
        switch (tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   685
        case IDENT:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
            ((JCIdent) tree).sym = sym; break;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   687
        case SELECT:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   688
            ((JCFieldAccess) tree).sym = sym; break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   689
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   690
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   691
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   692
06bc494ca11e Initial load
duke
parents:
diff changeset
   693
    /** If this tree is a declaration or a block, return its flags field,
06bc494ca11e Initial load
duke
parents:
diff changeset
   694
     *  otherwise return 0.
06bc494ca11e Initial load
duke
parents:
diff changeset
   695
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   696
    public static long flags(JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
        switch (tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   698
        case VARDEF:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
            return ((JCVariableDecl) tree).mods.flags;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   700
        case METHODDEF:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
            return ((JCMethodDecl) tree).mods.flags;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   702
        case CLASSDEF:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
            return ((JCClassDecl) tree).mods.flags;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   704
        case BLOCK:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
            return ((JCBlock) tree).flags;
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
            return 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   709
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
    /** Return first (smallest) flag in `flags':
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
     *  pre: flags != 0
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
    public static long firstFlag(long flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
        int flag = 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
        while ((flag & StandardFlags) != 0 && (flag & flags) == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
            flag = flag << 1;
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
        return flag;
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
    /** Return flags as a string, separated by " ".
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
    public static String flagNames(long flags) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
        return Flags.toString(flags & StandardFlags).trim();
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
    /** Operator precedences values.
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
    public static final int
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
        notExpression = -1,   // not an expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
        noPrec = 0,           // no enclosing expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
        assignPrec = 1,
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
        assignopPrec = 2,
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
        condPrec = 3,
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
        orPrec = 4,
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
        andPrec = 5,
06bc494ca11e Initial load
duke
parents:
diff changeset
   737
        bitorPrec = 6,
06bc494ca11e Initial load
duke
parents:
diff changeset
   738
        bitxorPrec = 7,
06bc494ca11e Initial load
duke
parents:
diff changeset
   739
        bitandPrec = 8,
06bc494ca11e Initial load
duke
parents:
diff changeset
   740
        eqPrec = 9,
06bc494ca11e Initial load
duke
parents:
diff changeset
   741
        ordPrec = 10,
06bc494ca11e Initial load
duke
parents:
diff changeset
   742
        shiftPrec = 11,
06bc494ca11e Initial load
duke
parents:
diff changeset
   743
        addPrec = 12,
06bc494ca11e Initial load
duke
parents:
diff changeset
   744
        mulPrec = 13,
06bc494ca11e Initial load
duke
parents:
diff changeset
   745
        prefixPrec = 14,
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
        postfixPrec = 15,
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
        precCount = 16;
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
    /** Map operators to their precedence levels.
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
     */
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   752
    public static int opPrec(JCTree.Tag op) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   753
        switch(op) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   754
        case POS:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   755
        case NEG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   756
        case NOT:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   757
        case COMPL:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   758
        case PREINC:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   759
        case PREDEC: return prefixPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   760
        case POSTINC:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   761
        case POSTDEC:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   762
        case NULLCHK: return postfixPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   763
        case ASSIGN: return assignPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   764
        case BITOR_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   765
        case BITXOR_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   766
        case BITAND_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   767
        case SL_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   768
        case SR_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   769
        case USR_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   770
        case PLUS_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   771
        case MINUS_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   772
        case MUL_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   773
        case DIV_ASG:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   774
        case MOD_ASG: return assignopPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   775
        case OR: return orPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   776
        case AND: return andPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   777
        case EQ:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   778
        case NE: return eqPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   779
        case LT:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   780
        case GT:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   781
        case LE:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   782
        case GE: return ordPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   783
        case BITOR: return bitorPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   784
        case BITXOR: return bitxorPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   785
        case BITAND: return bitandPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   786
        case SL:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   787
        case SR:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   788
        case USR: return shiftPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   789
        case PLUS:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   790
        case MINUS: return addPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   791
        case MUL:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   792
        case DIV:
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   793
        case MOD: return mulPrec;
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   794
        case TYPETEST: return ordPrec;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   795
        default: throw new AssertionError();
06bc494ca11e Initial load
duke
parents:
diff changeset
   796
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   797
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   798
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   799
    static Tree.Kind tagToKind(JCTree.Tag tag) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   800
        switch (tag) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   801
        // Postfix expressions
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   802
        case POSTINC:           // _ ++
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   803
            return Tree.Kind.POSTFIX_INCREMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   804
        case POSTDEC:           // _ --
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   805
            return Tree.Kind.POSTFIX_DECREMENT;
06bc494ca11e Initial load
duke
parents:
diff changeset
   806
06bc494ca11e Initial load
duke
parents:
diff changeset
   807
        // Unary operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   808
        case PREINC:            // ++ _
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   809
            return Tree.Kind.PREFIX_INCREMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   810
        case PREDEC:            // -- _
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   811
            return Tree.Kind.PREFIX_DECREMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   812
        case POS:               // +
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   813
            return Tree.Kind.UNARY_PLUS;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   814
        case NEG:               // -
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   815
            return Tree.Kind.UNARY_MINUS;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   816
        case COMPL:             // ~
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   817
            return Tree.Kind.BITWISE_COMPLEMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   818
        case NOT:               // !
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   819
            return Tree.Kind.LOGICAL_COMPLEMENT;
06bc494ca11e Initial load
duke
parents:
diff changeset
   820
06bc494ca11e Initial load
duke
parents:
diff changeset
   821
        // Binary operators
06bc494ca11e Initial load
duke
parents:
diff changeset
   822
06bc494ca11e Initial load
duke
parents:
diff changeset
   823
        // Multiplicative operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   824
        case MUL:               // *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   825
            return Tree.Kind.MULTIPLY;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   826
        case DIV:               // /
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   827
            return Tree.Kind.DIVIDE;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   828
        case MOD:               // %
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   829
            return Tree.Kind.REMAINDER;
06bc494ca11e Initial load
duke
parents:
diff changeset
   830
06bc494ca11e Initial load
duke
parents:
diff changeset
   831
        // Additive operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   832
        case PLUS:              // +
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   833
            return Tree.Kind.PLUS;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   834
        case MINUS:             // -
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   835
            return Tree.Kind.MINUS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   836
06bc494ca11e Initial load
duke
parents:
diff changeset
   837
        // Shift operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   838
        case SL:                // <<
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   839
            return Tree.Kind.LEFT_SHIFT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   840
        case SR:                // >>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   841
            return Tree.Kind.RIGHT_SHIFT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   842
        case USR:               // >>>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   843
            return Tree.Kind.UNSIGNED_RIGHT_SHIFT;
06bc494ca11e Initial load
duke
parents:
diff changeset
   844
06bc494ca11e Initial load
duke
parents:
diff changeset
   845
        // Relational operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   846
        case LT:                // <
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   847
            return Tree.Kind.LESS_THAN;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   848
        case GT:                // >
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   849
            return Tree.Kind.GREATER_THAN;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   850
        case LE:                // <=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   851
            return Tree.Kind.LESS_THAN_EQUAL;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   852
        case GE:                // >=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   853
            return Tree.Kind.GREATER_THAN_EQUAL;
06bc494ca11e Initial load
duke
parents:
diff changeset
   854
06bc494ca11e Initial load
duke
parents:
diff changeset
   855
        // Equality operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   856
        case EQ:                // ==
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   857
            return Tree.Kind.EQUAL_TO;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   858
        case NE:                // !=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   859
            return Tree.Kind.NOT_EQUAL_TO;
06bc494ca11e Initial load
duke
parents:
diff changeset
   860
06bc494ca11e Initial load
duke
parents:
diff changeset
   861
        // Bitwise and logical operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   862
        case BITAND:            // &
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   863
            return Tree.Kind.AND;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   864
        case BITXOR:            // ^
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   865
            return Tree.Kind.XOR;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   866
        case BITOR:             // |
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   867
            return Tree.Kind.OR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   868
06bc494ca11e Initial load
duke
parents:
diff changeset
   869
        // Conditional operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   870
        case AND:               // &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   871
            return Tree.Kind.CONDITIONAL_AND;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   872
        case OR:                // ||
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   873
            return Tree.Kind.CONDITIONAL_OR;
06bc494ca11e Initial load
duke
parents:
diff changeset
   874
06bc494ca11e Initial load
duke
parents:
diff changeset
   875
        // Assignment operators
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   876
        case MUL_ASG:           // *=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   877
            return Tree.Kind.MULTIPLY_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   878
        case DIV_ASG:           // /=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   879
            return Tree.Kind.DIVIDE_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   880
        case MOD_ASG:           // %=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   881
            return Tree.Kind.REMAINDER_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   882
        case PLUS_ASG:          // +=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   883
            return Tree.Kind.PLUS_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   884
        case MINUS_ASG:         // -=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   885
            return Tree.Kind.MINUS_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   886
        case SL_ASG:            // <<=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   887
            return Tree.Kind.LEFT_SHIFT_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   888
        case SR_ASG:            // >>=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   889
            return Tree.Kind.RIGHT_SHIFT_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   890
        case USR_ASG:           // >>>=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   891
            return Tree.Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   892
        case BITAND_ASG:        // &=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   893
            return Tree.Kind.AND_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   894
        case BITXOR_ASG:        // ^=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   895
            return Tree.Kind.XOR_ASSIGNMENT;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   896
        case BITOR_ASG:         // |=
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   897
            return Tree.Kind.OR_ASSIGNMENT;
06bc494ca11e Initial load
duke
parents:
diff changeset
   898
06bc494ca11e Initial load
duke
parents:
diff changeset
   899
        // Null check (implementation detail), for example, __.getClass()
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   900
        case NULLCHK:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   901
            return Tree.Kind.OTHER;
06bc494ca11e Initial load
duke
parents:
diff changeset
   902
06bc494ca11e Initial load
duke
parents:
diff changeset
   903
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   904
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   905
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   906
    }
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   907
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   908
    /**
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   909
     * Returns the underlying type of the tree if it is annotated type,
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   910
     * or the tree itself otherwise
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   911
     */
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   912
    public static JCExpression typeIn(JCExpression tree) {
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   913
        switch (tree.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   914
        case IDENT: /* simple names */
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   915
        case TYPEIDENT: /* primitive name */
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   916
        case SELECT: /* qualified name */
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   917
        case TYPEARRAY: /* array types */
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   918
        case WILDCARD: /* wild cards */
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   919
        case TYPEPARAMETER: /* type parameters */
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   920
        case TYPEAPPLY: /* parameterized types */
3149
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   921
            return tree;
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   922
        default:
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   923
            throw new AssertionError("Unexpected type tree: " + tree);
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   924
        }
0cd06d598d6f 6843077: JSR 308: Annotations on types
jjg
parents: 1264
diff changeset
   925
    }
4705
7fac532dc6fa 6917122: provide utility method to find the inner most type of a type tree
jjg
parents: 3765
diff changeset
   926
7fac532dc6fa 6917122: provide utility method to find the inner most type of a type tree
jjg
parents: 3765
diff changeset
   927
    public static JCTree innermostType(JCTree type) {
7fac532dc6fa 6917122: provide utility method to find the inner most type of a type tree
jjg
parents: 3765
diff changeset
   928
        switch (type.getTag()) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   929
        case TYPEARRAY:
4705
7fac532dc6fa 6917122: provide utility method to find the inner most type of a type tree
jjg
parents: 3765
diff changeset
   930
            return innermostType(((JCArrayTypeTree)type).elemtype);
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 9300
diff changeset
   931
        case WILDCARD:
4705
7fac532dc6fa 6917122: provide utility method to find the inner most type of a type tree
jjg
parents: 3765
diff changeset
   932
            return innermostType(((JCWildcard)type).inner);
7fac532dc6fa 6917122: provide utility method to find the inner most type of a type tree
jjg
parents: 3765
diff changeset
   933
        default:
7fac532dc6fa 6917122: provide utility method to find the inner most type of a type tree
jjg
parents: 3765
diff changeset
   934
            return type;
7fac532dc6fa 6917122: provide utility method to find the inner most type of a type tree
jjg
parents: 3765
diff changeset
   935
        }
7fac532dc6fa 6917122: provide utility method to find the inner most type of a type tree
jjg
parents: 3765
diff changeset
   936
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   937
}