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