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