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