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