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