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