author | mcimadamore |
Thu, 31 May 2012 17:44:04 +0100 | |
changeset 12916 | 021c069e8e27 |
parent 12080 | 23101f54df44 |
child 13077 | 16fb753bb5dc |
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 |
|
12916
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
240 |
public static boolean isEnumInit(JCTree tree) { |
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
241 |
switch (tree.getTag()) { |
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
242 |
case VARDEF: |
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
243 |
return (((JCVariableDecl)tree).mods.flags & ENUM) != 0; |
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
244 |
default: |
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
245 |
return false; |
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
246 |
} |
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
247 |
} |
021c069e8e27
7160084: javac fails to compile an apparently valid class/interface combination
mcimadamore
parents:
12080
diff
changeset
|
248 |
|
11142 | 249 |
/** |
250 |
* Return true if the AST corresponds to a static select of the kind A.B |
|
251 |
*/ |
|
252 |
public static boolean isStaticSelector(JCTree base, Names names) { |
|
253 |
if (base == null) |
|
254 |
return false; |
|
255 |
switch (base.getTag()) { |
|
256 |
case IDENT: |
|
257 |
JCIdent id = (JCIdent)base; |
|
258 |
return id.name != names._this && |
|
259 |
id.name != names._super && |
|
260 |
isStaticSym(base); |
|
261 |
case SELECT: |
|
262 |
return isStaticSym(base) && |
|
263 |
isStaticSelector(((JCFieldAccess)base).selected, names); |
|
264 |
case TYPEAPPLY: |
|
265 |
return true; |
|
266 |
default: |
|
267 |
return false; |
|
268 |
} |
|
269 |
} |
|
270 |
//where |
|
271 |
private static boolean isStaticSym(JCTree tree) { |
|
272 |
Symbol sym = symbol(tree); |
|
273 |
return (sym.kind == Kinds.TYP || |
|
274 |
sym.kind == Kinds.PCK); |
|
275 |
} |
|
276 |
||
10 | 277 |
/** Return true if a tree represents the null literal. */ |
278 |
public static boolean isNull(JCTree tree) { |
|
10950 | 279 |
if (!tree.hasTag(LITERAL)) |
10 | 280 |
return false; |
281 |
JCLiteral lit = (JCLiteral) tree; |
|
282 |
return (lit.typetag == TypeTags.BOT); |
|
283 |
} |
|
284 |
||
285 |
/** The position of the first statement in a block, or the position of |
|
286 |
* the block itself if it is empty. |
|
287 |
*/ |
|
288 |
public static int firstStatPos(JCTree tree) { |
|
10950 | 289 |
if (tree.hasTag(BLOCK) && ((JCBlock) tree).stats.nonEmpty()) |
10 | 290 |
return ((JCBlock) tree).stats.head.pos; |
291 |
else |
|
292 |
return tree.pos; |
|
293 |
} |
|
294 |
||
295 |
/** The end position of given tree, if it is a block with |
|
296 |
* defined endpos. |
|
297 |
*/ |
|
298 |
public static int endPos(JCTree tree) { |
|
10950 | 299 |
if (tree.hasTag(BLOCK) && ((JCBlock) tree).endpos != Position.NOPOS) |
10 | 300 |
return ((JCBlock) tree).endpos; |
10950 | 301 |
else if (tree.hasTag(SYNCHRONIZED)) |
10 | 302 |
return endPos(((JCSynchronized) tree).body); |
10950 | 303 |
else if (tree.hasTag(TRY)) { |
10 | 304 |
JCTry t = (JCTry) tree; |
305 |
return endPos((t.finalizer != null) |
|
306 |
? t.finalizer |
|
307 |
: t.catchers.last().body); |
|
308 |
} else |
|
309 |
return tree.pos; |
|
310 |
} |
|
311 |
||
312 |
||
313 |
/** Get the start position for a tree node. The start position is |
|
314 |
* defined to be the position of the first character of the first |
|
315 |
* token of the node's source text. |
|
316 |
* @param tree The tree node |
|
317 |
*/ |
|
318 |
public static int getStartPos(JCTree tree) { |
|
319 |
if (tree == null) |
|
320 |
return Position.NOPOS; |
|
321 |
||
322 |
switch(tree.getTag()) { |
|
10950 | 323 |
case APPLY: |
324 |
return getStartPos(((JCMethodInvocation) tree).meth); |
|
325 |
case ASSIGN: |
|
326 |
return getStartPos(((JCAssign) tree).lhs); |
|
327 |
case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG: |
|
328 |
case SL_ASG: case SR_ASG: case USR_ASG: |
|
329 |
case PLUS_ASG: case MINUS_ASG: case MUL_ASG: |
|
330 |
case DIV_ASG: case MOD_ASG: |
|
331 |
return getStartPos(((JCAssignOp) tree).lhs); |
|
332 |
case OR: case AND: case BITOR: |
|
333 |
case BITXOR: case BITAND: case EQ: |
|
334 |
case NE: case LT: case GT: |
|
335 |
case LE: case GE: case SL: |
|
336 |
case SR: case USR: case PLUS: |
|
337 |
case MINUS: case MUL: case DIV: |
|
338 |
case MOD: |
|
339 |
return getStartPos(((JCBinary) tree).lhs); |
|
340 |
case CLASSDEF: { |
|
341 |
JCClassDecl node = (JCClassDecl)tree; |
|
342 |
if (node.mods.pos != Position.NOPOS) |
|
343 |
return node.mods.pos; |
|
344 |
break; |
|
10 | 345 |
} |
10950 | 346 |
case CONDEXPR: |
347 |
return getStartPos(((JCConditional) tree).cond); |
|
348 |
case EXEC: |
|
349 |
return getStartPos(((JCExpressionStatement) tree).expr); |
|
350 |
case INDEXED: |
|
351 |
return getStartPos(((JCArrayAccess) tree).indexed); |
|
352 |
case METHODDEF: { |
|
353 |
JCMethodDecl node = (JCMethodDecl)tree; |
|
354 |
if (node.mods.pos != Position.NOPOS) |
|
355 |
return node.mods.pos; |
|
356 |
if (node.typarams.nonEmpty()) // List.nil() used for no typarams |
|
357 |
return getStartPos(node.typarams.head); |
|
358 |
return node.restype == null ? node.pos : getStartPos(node.restype); |
|
359 |
} |
|
360 |
case SELECT: |
|
361 |
return getStartPos(((JCFieldAccess) tree).selected); |
|
362 |
case TYPEAPPLY: |
|
363 |
return getStartPos(((JCTypeApply) tree).clazz); |
|
364 |
case TYPEARRAY: |
|
365 |
return getStartPos(((JCArrayTypeTree) tree).elemtype); |
|
366 |
case TYPETEST: |
|
367 |
return getStartPos(((JCInstanceOf) tree).expr); |
|
368 |
case POSTINC: |
|
369 |
case POSTDEC: |
|
370 |
return getStartPos(((JCUnary) tree).arg); |
|
371 |
case NEWCLASS: { |
|
372 |
JCNewClass node = (JCNewClass)tree; |
|
373 |
if (node.encl != null) |
|
374 |
return getStartPos(node.encl); |
|
375 |
break; |
|
376 |
} |
|
377 |
case VARDEF: { |
|
378 |
JCVariableDecl node = (JCVariableDecl)tree; |
|
379 |
if (node.mods.pos != Position.NOPOS) { |
|
380 |
return node.mods.pos; |
|
381 |
} else { |
|
382 |
return getStartPos(node.vartype); |
|
383 |
} |
|
384 |
} |
|
385 |
case ERRONEOUS: { |
|
386 |
JCErroneous node = (JCErroneous)tree; |
|
387 |
if (node.errs != null && node.errs.nonEmpty()) |
|
388 |
return getStartPos(node.errs.head); |
|
389 |
} |
|
10 | 390 |
} |
391 |
return tree.pos; |
|
392 |
} |
|
393 |
||
394 |
/** The end position of given tree, given a table of end positions generated by the parser |
|
395 |
*/ |
|
11055 | 396 |
public static int getEndPos(JCTree tree, EndPosTable endPosTable) { |
10 | 397 |
if (tree == null) |
398 |
return Position.NOPOS; |
|
399 |
||
11055 | 400 |
if (endPosTable == null) { |
10 | 401 |
// fall back on limited info in the tree |
402 |
return endPos(tree); |
|
403 |
} |
|
404 |
||
11055 | 405 |
int mapPos = endPosTable.getEndPos(tree); |
406 |
if (mapPos != Position.NOPOS) |
|
10 | 407 |
return mapPos; |
408 |
||
409 |
switch(tree.getTag()) { |
|
10950 | 410 |
case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG: |
411 |
case SL_ASG: case SR_ASG: case USR_ASG: |
|
412 |
case PLUS_ASG: case MINUS_ASG: case MUL_ASG: |
|
413 |
case DIV_ASG: case MOD_ASG: |
|
11055 | 414 |
return getEndPos(((JCAssignOp) tree).rhs, endPosTable); |
10950 | 415 |
case OR: case AND: case BITOR: |
416 |
case BITXOR: case BITAND: case EQ: |
|
417 |
case NE: case LT: case GT: |
|
418 |
case LE: case GE: case SL: |
|
419 |
case SR: case USR: case PLUS: |
|
420 |
case MINUS: case MUL: case DIV: |
|
421 |
case MOD: |
|
11055 | 422 |
return getEndPos(((JCBinary) tree).rhs, endPosTable); |
10950 | 423 |
case CASE: |
11055 | 424 |
return getEndPos(((JCCase) tree).stats.last(), endPosTable); |
10950 | 425 |
case CATCH: |
11055 | 426 |
return getEndPos(((JCCatch) tree).body, endPosTable); |
10950 | 427 |
case CONDEXPR: |
11055 | 428 |
return getEndPos(((JCConditional) tree).falsepart, endPosTable); |
10950 | 429 |
case FORLOOP: |
11055 | 430 |
return getEndPos(((JCForLoop) tree).body, endPosTable); |
10950 | 431 |
case FOREACHLOOP: |
11055 | 432 |
return getEndPos(((JCEnhancedForLoop) tree).body, endPosTable); |
10950 | 433 |
case IF: { |
434 |
JCIf node = (JCIf)tree; |
|
435 |
if (node.elsepart == null) { |
|
11055 | 436 |
return getEndPos(node.thenpart, endPosTable); |
10950 | 437 |
} else { |
11055 | 438 |
return getEndPos(node.elsepart, endPosTable); |
10950 | 439 |
} |
10 | 440 |
} |
10950 | 441 |
case LABELLED: |
11055 | 442 |
return getEndPos(((JCLabeledStatement) tree).body, endPosTable); |
10950 | 443 |
case MODIFIERS: |
11055 | 444 |
return getEndPos(((JCModifiers) tree).annotations.last(), endPosTable); |
10950 | 445 |
case SYNCHRONIZED: |
11055 | 446 |
return getEndPos(((JCSynchronized) tree).body, endPosTable); |
10950 | 447 |
case TOPLEVEL: |
11055 | 448 |
return getEndPos(((JCCompilationUnit) tree).defs.last(), endPosTable); |
10950 | 449 |
case TRY: { |
450 |
JCTry node = (JCTry)tree; |
|
451 |
if (node.finalizer != null) { |
|
11055 | 452 |
return getEndPos(node.finalizer, endPosTable); |
10950 | 453 |
} else if (!node.catchers.isEmpty()) { |
11055 | 454 |
return getEndPos(node.catchers.last(), endPosTable); |
10950 | 455 |
} else { |
11055 | 456 |
return getEndPos(node.body, endPosTable); |
10950 | 457 |
} |
10 | 458 |
} |
10950 | 459 |
case WILDCARD: |
11055 | 460 |
return getEndPos(((JCWildcard) tree).inner, endPosTable); |
10950 | 461 |
case TYPECAST: |
11055 | 462 |
return getEndPos(((JCTypeCast) tree).expr, endPosTable); |
10950 | 463 |
case TYPETEST: |
11055 | 464 |
return getEndPos(((JCInstanceOf) tree).clazz, endPosTable); |
10950 | 465 |
case POS: |
466 |
case NEG: |
|
467 |
case NOT: |
|
468 |
case COMPL: |
|
469 |
case PREINC: |
|
470 |
case PREDEC: |
|
11055 | 471 |
return getEndPos(((JCUnary) tree).arg, endPosTable); |
10950 | 472 |
case WHILELOOP: |
11055 | 473 |
return getEndPos(((JCWhileLoop) tree).body, endPosTable); |
10950 | 474 |
case ERRONEOUS: { |
475 |
JCErroneous node = (JCErroneous)tree; |
|
476 |
if (node.errs != null && node.errs.nonEmpty()) |
|
11055 | 477 |
return getEndPos(node.errs.last(), endPosTable); |
10950 | 478 |
} |
10 | 479 |
} |
480 |
return Position.NOPOS; |
|
481 |
} |
|
482 |
||
483 |
||
484 |
/** A DiagnosticPosition with the preferred position set to the |
|
485 |
* end position of given tree, if it is a block with |
|
486 |
* defined endpos. |
|
487 |
*/ |
|
488 |
public static DiagnosticPosition diagEndPos(final JCTree tree) { |
|
489 |
final int endPos = TreeInfo.endPos(tree); |
|
490 |
return new DiagnosticPosition() { |
|
491 |
public JCTree getTree() { return tree; } |
|
492 |
public int getStartPosition() { return TreeInfo.getStartPos(tree); } |
|
493 |
public int getPreferredPosition() { return endPos; } |
|
11055 | 494 |
public int getEndPosition(EndPosTable endPosTable) { |
10 | 495 |
return TreeInfo.getEndPos(tree, endPosTable); |
496 |
} |
|
497 |
}; |
|
498 |
} |
|
499 |
||
500 |
/** The position of the finalizer of given try/synchronized statement. |
|
501 |
*/ |
|
502 |
public static int finalizerPos(JCTree tree) { |
|
10950 | 503 |
if (tree.hasTag(TRY)) { |
10 | 504 |
JCTry t = (JCTry) tree; |
8032 | 505 |
Assert.checkNonNull(t.finalizer); |
10 | 506 |
return firstStatPos(t.finalizer); |
10950 | 507 |
} else if (tree.hasTag(SYNCHRONIZED)) { |
10 | 508 |
return endPos(((JCSynchronized) tree).body); |
509 |
} else { |
|
510 |
throw new AssertionError(); |
|
511 |
} |
|
512 |
} |
|
513 |
||
514 |
/** Find the position for reporting an error about a symbol, where |
|
515 |
* that symbol is defined somewhere in the given tree. */ |
|
516 |
public static int positionFor(final Symbol sym, final JCTree tree) { |
|
517 |
JCTree decl = declarationFor(sym, tree); |
|
518 |
return ((decl != null) ? decl : tree).pos; |
|
519 |
} |
|
520 |
||
521 |
/** Find the position for reporting an error about a symbol, where |
|
522 |
* that symbol is defined somewhere in the given tree. */ |
|
523 |
public static DiagnosticPosition diagnosticPositionFor(final Symbol sym, final JCTree tree) { |
|
524 |
JCTree decl = declarationFor(sym, tree); |
|
525 |
return ((decl != null) ? decl : tree).pos(); |
|
526 |
} |
|
527 |
||
528 |
/** Find the declaration for a symbol, where |
|
529 |
* that symbol is defined somewhere in the given tree. */ |
|
530 |
public static JCTree declarationFor(final Symbol sym, final JCTree tree) { |
|
531 |
class DeclScanner extends TreeScanner { |
|
532 |
JCTree result = null; |
|
533 |
public void scan(JCTree tree) { |
|
534 |
if (tree!=null && result==null) |
|
535 |
tree.accept(this); |
|
536 |
} |
|
537 |
public void visitTopLevel(JCCompilationUnit that) { |
|
538 |
if (that.packge == sym) result = that; |
|
539 |
else super.visitTopLevel(that); |
|
540 |
} |
|
541 |
public void visitClassDef(JCClassDecl that) { |
|
542 |
if (that.sym == sym) result = that; |
|
543 |
else super.visitClassDef(that); |
|
544 |
} |
|
545 |
public void visitMethodDef(JCMethodDecl that) { |
|
546 |
if (that.sym == sym) result = that; |
|
547 |
else super.visitMethodDef(that); |
|
548 |
} |
|
549 |
public void visitVarDef(JCVariableDecl that) { |
|
550 |
if (that.sym == sym) result = that; |
|
551 |
else super.visitVarDef(that); |
|
552 |
} |
|
6586
0d40dc0c06cb
6458823: Messager messages on TypeParamterElements to not include position information.
sundar
parents:
5847
diff
changeset
|
553 |
public void visitTypeParameter(JCTypeParameter that) { |
7619
1bedd48ff024
6990209: JCK7-compiler lang/ICLS/icls006/icls00603/icls00603a.html#icls00603src test fails.
jjh
parents:
7074
diff
changeset
|
554 |
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
|
555 |
else super.visitTypeParameter(that); |
0d40dc0c06cb
6458823: Messager messages on TypeParamterElements to not include position information.
sundar
parents:
5847
diff
changeset
|
556 |
} |
10 | 557 |
} |
558 |
DeclScanner s = new DeclScanner(); |
|
559 |
tree.accept(s); |
|
560 |
return s.result; |
|
561 |
} |
|
562 |
||
563 |
public static Env<AttrContext> scopeFor(JCTree node, JCCompilationUnit unit) { |
|
564 |
return scopeFor(pathFor(node, unit)); |
|
565 |
} |
|
566 |
||
567 |
public static Env<AttrContext> scopeFor(List<JCTree> path) { |
|
568 |
// TODO: not implemented yet |
|
569 |
throw new UnsupportedOperationException("not implemented yet"); |
|
570 |
} |
|
571 |
||
572 |
public static List<JCTree> pathFor(final JCTree node, final JCCompilationUnit unit) { |
|
573 |
class Result extends Error { |
|
574 |
static final long serialVersionUID = -5942088234594905625L; |
|
575 |
List<JCTree> path; |
|
576 |
Result(List<JCTree> path) { |
|
577 |
this.path = path; |
|
578 |
} |
|
579 |
} |
|
580 |
class PathFinder extends TreeScanner { |
|
581 |
List<JCTree> path = List.nil(); |
|
582 |
public void scan(JCTree tree) { |
|
583 |
if (tree != null) { |
|
584 |
path = path.prepend(tree); |
|
585 |
if (tree == node) |
|
586 |
throw new Result(path); |
|
587 |
super.scan(tree); |
|
588 |
path = path.tail; |
|
589 |
} |
|
590 |
} |
|
591 |
} |
|
592 |
try { |
|
593 |
new PathFinder().scan(unit); |
|
594 |
} catch (Result result) { |
|
595 |
return result.path; |
|
596 |
} |
|
597 |
return List.nil(); |
|
598 |
} |
|
599 |
||
600 |
/** Return the statement referenced by a label. |
|
601 |
* If the label refers to a loop or switch, return that switch |
|
602 |
* otherwise return the labelled statement itself |
|
603 |
*/ |
|
604 |
public static JCTree referencedStatement(JCLabeledStatement tree) { |
|
605 |
JCTree t = tree; |
|
606 |
do t = ((JCLabeledStatement) t).body; |
|
10950 | 607 |
while (t.hasTag(LABELLED)); |
10 | 608 |
switch (t.getTag()) { |
10950 | 609 |
case DOLOOP: case WHILELOOP: case FORLOOP: case FOREACHLOOP: case SWITCH: |
10 | 610 |
return t; |
611 |
default: |
|
612 |
return tree; |
|
613 |
} |
|
614 |
} |
|
615 |
||
616 |
/** Skip parens and return the enclosed expression |
|
617 |
*/ |
|
618 |
public static JCExpression skipParens(JCExpression tree) { |
|
10950 | 619 |
while (tree.hasTag(PARENS)) { |
10 | 620 |
tree = ((JCParens) tree).expr; |
621 |
} |
|
622 |
return tree; |
|
623 |
} |
|
624 |
||
625 |
/** Skip parens and return the enclosed expression |
|
626 |
*/ |
|
627 |
public static JCTree skipParens(JCTree tree) { |
|
10950 | 628 |
if (tree.hasTag(PARENS)) |
10 | 629 |
return skipParens((JCParens)tree); |
630 |
else |
|
631 |
return tree; |
|
632 |
} |
|
633 |
||
634 |
/** Return the types of a list of trees. |
|
635 |
*/ |
|
636 |
public static List<Type> types(List<? extends JCTree> trees) { |
|
637 |
ListBuffer<Type> ts = new ListBuffer<Type>(); |
|
638 |
for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail) |
|
639 |
ts.append(l.head.type); |
|
640 |
return ts.toList(); |
|
641 |
} |
|
642 |
||
643 |
/** If this tree is an identifier or a field or a parameterized type, |
|
644 |
* return its name, otherwise return null. |
|
645 |
*/ |
|
646 |
public static Name name(JCTree tree) { |
|
647 |
switch (tree.getTag()) { |
|
10950 | 648 |
case IDENT: |
10 | 649 |
return ((JCIdent) tree).name; |
10950 | 650 |
case SELECT: |
10 | 651 |
return ((JCFieldAccess) tree).name; |
10950 | 652 |
case TYPEAPPLY: |
10 | 653 |
return name(((JCTypeApply) tree).clazz); |
654 |
default: |
|
655 |
return null; |
|
656 |
} |
|
657 |
} |
|
658 |
||
659 |
/** If this tree is a qualified identifier, its return fully qualified name, |
|
660 |
* otherwise return null. |
|
661 |
*/ |
|
662 |
public static Name fullName(JCTree tree) { |
|
663 |
tree = skipParens(tree); |
|
664 |
switch (tree.getTag()) { |
|
10950 | 665 |
case IDENT: |
10 | 666 |
return ((JCIdent) tree).name; |
10950 | 667 |
case SELECT: |
10 | 668 |
Name sname = fullName(((JCFieldAccess) tree).selected); |
669 |
return sname == null ? null : sname.append('.', name(tree)); |
|
670 |
default: |
|
671 |
return null; |
|
672 |
} |
|
673 |
} |
|
674 |
||
675 |
public static Symbol symbolFor(JCTree node) { |
|
676 |
node = skipParens(node); |
|
677 |
switch (node.getTag()) { |
|
10950 | 678 |
case CLASSDEF: |
10 | 679 |
return ((JCClassDecl) node).sym; |
10950 | 680 |
case METHODDEF: |
10 | 681 |
return ((JCMethodDecl) node).sym; |
10950 | 682 |
case VARDEF: |
10 | 683 |
return ((JCVariableDecl) node).sym; |
684 |
default: |
|
685 |
return null; |
|
686 |
} |
|
687 |
} |
|
688 |
||
6590
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
6586
diff
changeset
|
689 |
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
|
690 |
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
|
691 |
switch (node.getTag()) { |
10950 | 692 |
case CLASSDEF: |
693 |
case METHODDEF: |
|
694 |
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
|
695 |
return true; |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
6586
diff
changeset
|
696 |
default: |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
6586
diff
changeset
|
697 |
return false; |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
6586
diff
changeset
|
698 |
} |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
6586
diff
changeset
|
699 |
} |
f745e683da2c
6930507: Symbols for anonymous and local classes made too late for use by java tree API
jjg
parents:
6586
diff
changeset
|
700 |
|
10 | 701 |
/** If this tree is an identifier or a field, return its symbol, |
702 |
* otherwise return null. |
|
703 |
*/ |
|
704 |
public static Symbol symbol(JCTree tree) { |
|
705 |
tree = skipParens(tree); |
|
706 |
switch (tree.getTag()) { |
|
10950 | 707 |
case IDENT: |
10 | 708 |
return ((JCIdent) tree).sym; |
10950 | 709 |
case SELECT: |
10 | 710 |
return ((JCFieldAccess) tree).sym; |
10950 | 711 |
case TYPEAPPLY: |
10 | 712 |
return symbol(((JCTypeApply) tree).clazz); |
713 |
default: |
|
714 |
return null; |
|
715 |
} |
|
716 |
} |
|
717 |
||
718 |
/** Return true if this is a nonstatic selection. */ |
|
719 |
public static boolean nonstaticSelect(JCTree tree) { |
|
720 |
tree = skipParens(tree); |
|
10950 | 721 |
if (!tree.hasTag(SELECT)) return false; |
10 | 722 |
JCFieldAccess s = (JCFieldAccess) tree; |
723 |
Symbol e = symbol(s.selected); |
|
724 |
return e == null || (e.kind != Kinds.PCK && e.kind != Kinds.TYP); |
|
725 |
} |
|
726 |
||
727 |
/** If this tree is an identifier or a field, set its symbol, otherwise skip. |
|
728 |
*/ |
|
729 |
public static void setSymbol(JCTree tree, Symbol sym) { |
|
730 |
tree = skipParens(tree); |
|
731 |
switch (tree.getTag()) { |
|
10950 | 732 |
case IDENT: |
10 | 733 |
((JCIdent) tree).sym = sym; break; |
10950 | 734 |
case SELECT: |
10 | 735 |
((JCFieldAccess) tree).sym = sym; break; |
736 |
default: |
|
737 |
} |
|
738 |
} |
|
739 |
||
740 |
/** If this tree is a declaration or a block, return its flags field, |
|
741 |
* otherwise return 0. |
|
742 |
*/ |
|
743 |
public static long flags(JCTree tree) { |
|
744 |
switch (tree.getTag()) { |
|
10950 | 745 |
case VARDEF: |
10 | 746 |
return ((JCVariableDecl) tree).mods.flags; |
10950 | 747 |
case METHODDEF: |
10 | 748 |
return ((JCMethodDecl) tree).mods.flags; |
10950 | 749 |
case CLASSDEF: |
10 | 750 |
return ((JCClassDecl) tree).mods.flags; |
10950 | 751 |
case BLOCK: |
10 | 752 |
return ((JCBlock) tree).flags; |
753 |
default: |
|
754 |
return 0; |
|
755 |
} |
|
756 |
} |
|
757 |
||
758 |
/** Return first (smallest) flag in `flags': |
|
759 |
* pre: flags != 0 |
|
760 |
*/ |
|
761 |
public static long firstFlag(long flags) { |
|
762 |
int flag = 1; |
|
763 |
while ((flag & StandardFlags) != 0 && (flag & flags) == 0) |
|
764 |
flag = flag << 1; |
|
765 |
return flag; |
|
766 |
} |
|
767 |
||
768 |
/** Return flags as a string, separated by " ". |
|
769 |
*/ |
|
770 |
public static String flagNames(long flags) { |
|
771 |
return Flags.toString(flags & StandardFlags).trim(); |
|
772 |
} |
|
773 |
||
774 |
/** Operator precedences values. |
|
775 |
*/ |
|
776 |
public static final int |
|
777 |
notExpression = -1, // not an expression |
|
778 |
noPrec = 0, // no enclosing expression |
|
779 |
assignPrec = 1, |
|
780 |
assignopPrec = 2, |
|
781 |
condPrec = 3, |
|
782 |
orPrec = 4, |
|
783 |
andPrec = 5, |
|
784 |
bitorPrec = 6, |
|
785 |
bitxorPrec = 7, |
|
786 |
bitandPrec = 8, |
|
787 |
eqPrec = 9, |
|
788 |
ordPrec = 10, |
|
789 |
shiftPrec = 11, |
|
790 |
addPrec = 12, |
|
791 |
mulPrec = 13, |
|
792 |
prefixPrec = 14, |
|
793 |
postfixPrec = 15, |
|
794 |
precCount = 16; |
|
795 |
||
796 |
||
797 |
/** Map operators to their precedence levels. |
|
798 |
*/ |
|
10950 | 799 |
public static int opPrec(JCTree.Tag op) { |
10 | 800 |
switch(op) { |
10950 | 801 |
case POS: |
802 |
case NEG: |
|
803 |
case NOT: |
|
804 |
case COMPL: |
|
805 |
case PREINC: |
|
806 |
case PREDEC: return prefixPrec; |
|
807 |
case POSTINC: |
|
808 |
case POSTDEC: |
|
809 |
case NULLCHK: return postfixPrec; |
|
810 |
case ASSIGN: return assignPrec; |
|
811 |
case BITOR_ASG: |
|
812 |
case BITXOR_ASG: |
|
813 |
case BITAND_ASG: |
|
814 |
case SL_ASG: |
|
815 |
case SR_ASG: |
|
816 |
case USR_ASG: |
|
817 |
case PLUS_ASG: |
|
818 |
case MINUS_ASG: |
|
819 |
case MUL_ASG: |
|
820 |
case DIV_ASG: |
|
821 |
case MOD_ASG: return assignopPrec; |
|
822 |
case OR: return orPrec; |
|
823 |
case AND: return andPrec; |
|
824 |
case EQ: |
|
825 |
case NE: return eqPrec; |
|
826 |
case LT: |
|
827 |
case GT: |
|
828 |
case LE: |
|
829 |
case GE: return ordPrec; |
|
830 |
case BITOR: return bitorPrec; |
|
831 |
case BITXOR: return bitxorPrec; |
|
832 |
case BITAND: return bitandPrec; |
|
833 |
case SL: |
|
834 |
case SR: |
|
835 |
case USR: return shiftPrec; |
|
836 |
case PLUS: |
|
837 |
case MINUS: return addPrec; |
|
838 |
case MUL: |
|
839 |
case DIV: |
|
840 |
case MOD: return mulPrec; |
|
841 |
case TYPETEST: return ordPrec; |
|
10 | 842 |
default: throw new AssertionError(); |
843 |
} |
|
844 |
} |
|
845 |
||
10950 | 846 |
static Tree.Kind tagToKind(JCTree.Tag tag) { |
10 | 847 |
switch (tag) { |
848 |
// Postfix expressions |
|
10950 | 849 |
case POSTINC: // _ ++ |
10 | 850 |
return Tree.Kind.POSTFIX_INCREMENT; |
10950 | 851 |
case POSTDEC: // _ -- |
10 | 852 |
return Tree.Kind.POSTFIX_DECREMENT; |
853 |
||
854 |
// Unary operators |
|
10950 | 855 |
case PREINC: // ++ _ |
10 | 856 |
return Tree.Kind.PREFIX_INCREMENT; |
10950 | 857 |
case PREDEC: // -- _ |
10 | 858 |
return Tree.Kind.PREFIX_DECREMENT; |
10950 | 859 |
case POS: // + |
10 | 860 |
return Tree.Kind.UNARY_PLUS; |
10950 | 861 |
case NEG: // - |
10 | 862 |
return Tree.Kind.UNARY_MINUS; |
10950 | 863 |
case COMPL: // ~ |
10 | 864 |
return Tree.Kind.BITWISE_COMPLEMENT; |
10950 | 865 |
case NOT: // ! |
10 | 866 |
return Tree.Kind.LOGICAL_COMPLEMENT; |
867 |
||
868 |
// Binary operators |
|
869 |
||
870 |
// Multiplicative operators |
|
10950 | 871 |
case MUL: // * |
10 | 872 |
return Tree.Kind.MULTIPLY; |
10950 | 873 |
case DIV: // / |
10 | 874 |
return Tree.Kind.DIVIDE; |
10950 | 875 |
case MOD: // % |
10 | 876 |
return Tree.Kind.REMAINDER; |
877 |
||
878 |
// Additive operators |
|
10950 | 879 |
case PLUS: // + |
10 | 880 |
return Tree.Kind.PLUS; |
10950 | 881 |
case MINUS: // - |
10 | 882 |
return Tree.Kind.MINUS; |
883 |
||
884 |
// Shift operators |
|
10950 | 885 |
case SL: // << |
10 | 886 |
return Tree.Kind.LEFT_SHIFT; |
10950 | 887 |
case SR: // >> |
10 | 888 |
return Tree.Kind.RIGHT_SHIFT; |
10950 | 889 |
case USR: // >>> |
10 | 890 |
return Tree.Kind.UNSIGNED_RIGHT_SHIFT; |
891 |
||
892 |
// Relational operators |
|
10950 | 893 |
case LT: // < |
10 | 894 |
return Tree.Kind.LESS_THAN; |
10950 | 895 |
case GT: // > |
10 | 896 |
return Tree.Kind.GREATER_THAN; |
10950 | 897 |
case LE: // <= |
10 | 898 |
return Tree.Kind.LESS_THAN_EQUAL; |
10950 | 899 |
case GE: // >= |
10 | 900 |
return Tree.Kind.GREATER_THAN_EQUAL; |
901 |
||
902 |
// Equality operators |
|
10950 | 903 |
case EQ: // == |
10 | 904 |
return Tree.Kind.EQUAL_TO; |
10950 | 905 |
case NE: // != |
10 | 906 |
return Tree.Kind.NOT_EQUAL_TO; |
907 |
||
908 |
// Bitwise and logical operators |
|
10950 | 909 |
case BITAND: // & |
10 | 910 |
return Tree.Kind.AND; |
10950 | 911 |
case BITXOR: // ^ |
10 | 912 |
return Tree.Kind.XOR; |
10950 | 913 |
case BITOR: // | |
10 | 914 |
return Tree.Kind.OR; |
915 |
||
916 |
// Conditional operators |
|
10950 | 917 |
case AND: // && |
10 | 918 |
return Tree.Kind.CONDITIONAL_AND; |
10950 | 919 |
case OR: // || |
10 | 920 |
return Tree.Kind.CONDITIONAL_OR; |
921 |
||
922 |
// Assignment operators |
|
10950 | 923 |
case MUL_ASG: // *= |
10 | 924 |
return Tree.Kind.MULTIPLY_ASSIGNMENT; |
10950 | 925 |
case DIV_ASG: // /= |
10 | 926 |
return Tree.Kind.DIVIDE_ASSIGNMENT; |
10950 | 927 |
case MOD_ASG: // %= |
10 | 928 |
return Tree.Kind.REMAINDER_ASSIGNMENT; |
10950 | 929 |
case PLUS_ASG: // += |
10 | 930 |
return Tree.Kind.PLUS_ASSIGNMENT; |
10950 | 931 |
case MINUS_ASG: // -= |
10 | 932 |
return Tree.Kind.MINUS_ASSIGNMENT; |
10950 | 933 |
case SL_ASG: // <<= |
10 | 934 |
return Tree.Kind.LEFT_SHIFT_ASSIGNMENT; |
10950 | 935 |
case SR_ASG: // >>= |
10 | 936 |
return Tree.Kind.RIGHT_SHIFT_ASSIGNMENT; |
10950 | 937 |
case USR_ASG: // >>>= |
10 | 938 |
return Tree.Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT; |
10950 | 939 |
case BITAND_ASG: // &= |
10 | 940 |
return Tree.Kind.AND_ASSIGNMENT; |
10950 | 941 |
case BITXOR_ASG: // ^= |
10 | 942 |
return Tree.Kind.XOR_ASSIGNMENT; |
10950 | 943 |
case BITOR_ASG: // |= |
10 | 944 |
return Tree.Kind.OR_ASSIGNMENT; |
945 |
||
946 |
// Null check (implementation detail), for example, __.getClass() |
|
10950 | 947 |
case NULLCHK: |
10 | 948 |
return Tree.Kind.OTHER; |
949 |
||
950 |
default: |
|
951 |
return null; |
|
952 |
} |
|
953 |
} |
|
3149 | 954 |
|
955 |
/** |
|
956 |
* Returns the underlying type of the tree if it is annotated type, |
|
957 |
* or the tree itself otherwise |
|
958 |
*/ |
|
959 |
public static JCExpression typeIn(JCExpression tree) { |
|
960 |
switch (tree.getTag()) { |
|
10950 | 961 |
case IDENT: /* simple names */ |
962 |
case TYPEIDENT: /* primitive name */ |
|
963 |
case SELECT: /* qualified name */ |
|
964 |
case TYPEARRAY: /* array types */ |
|
965 |
case WILDCARD: /* wild cards */ |
|
966 |
case TYPEPARAMETER: /* type parameters */ |
|
967 |
case TYPEAPPLY: /* parameterized types */ |
|
3149 | 968 |
return tree; |
969 |
default: |
|
970 |
throw new AssertionError("Unexpected type tree: " + tree); |
|
971 |
} |
|
972 |
} |
|
4705
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 |
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
|
975 |
switch (type.getTag()) { |
10950 | 976 |
case TYPEARRAY: |
4705
7fac532dc6fa
6917122: provide utility method to find the inner most type of a type tree
jjg
parents:
3765
diff
changeset
|
977 |
return innermostType(((JCArrayTypeTree)type).elemtype); |
10950 | 978 |
case WILDCARD: |
4705
7fac532dc6fa
6917122: provide utility method to find the inner most type of a type tree
jjg
parents:
3765
diff
changeset
|
979 |
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
|
980 |
default: |
7fac532dc6fa
6917122: provide utility method to find the inner most type of a type tree
jjg
parents:
3765
diff
changeset
|
981 |
return type; |
7fac532dc6fa
6917122: provide utility method to find the inner most type of a type tree
jjg
parents:
3765
diff
changeset
|
982 |
} |
7fac532dc6fa
6917122: provide utility method to find the inner most type of a type tree
jjg
parents:
3765
diff
changeset
|
983 |
} |
10 | 984 |
} |