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