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