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