src/share/classes/com/sun/tools/javac/tree/TreeInfo.java

Sun, 17 Feb 2013 16:44:55 -0500

author
dholmes
date
Sun, 17 Feb 2013 16:44:55 -0500
changeset 1571
af8417e590f4
parent 1563
bc456436c613
child 1697
950e8ac120f0
permissions
-rw-r--r--

Merge

duke@1 1 /*
jjg@1521 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.tree;
duke@1 27
jjg@1280 28
jjg@1455 29
duke@1 30 import com.sun.source.tree.Tree;
jjg@1280 31 import com.sun.tools.javac.code.*;
duke@1 32 import com.sun.tools.javac.comp.AttrContext;
duke@1 33 import com.sun.tools.javac.comp.Env;
jjg@1280 34 import com.sun.tools.javac.tree.JCTree.*;
mcimadamore@1510 35 import com.sun.tools.javac.tree.JCTree.JCPolyExpression.*;
duke@1 36 import com.sun.tools.javac.util.*;
duke@1 37 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
duke@1 38 import static com.sun.tools.javac.code.Flags.*;
jjg@1374 39 import static com.sun.tools.javac.code.TypeTag.BOT;
jjg@1127 40 import static com.sun.tools.javac.tree.JCTree.Tag.*;
jjg@1127 41 import static com.sun.tools.javac.tree.JCTree.Tag.BLOCK;
jjg@1127 42 import static com.sun.tools.javac.tree.JCTree.Tag.SYNCHRONIZED;
duke@1 43
duke@1 44 /** Utility class containing inspector methods for trees.
duke@1 45 *
jjg@581 46 * <p><b>This is NOT part of any supported API.
jjg@581 47 * If you write code that depends on this, you do so at your own risk.
duke@1 48 * This code and its internal interfaces are subject to change or
duke@1 49 * deletion without notice.</b>
duke@1 50 */
duke@1 51 public class TreeInfo {
duke@1 52 protected static final Context.Key<TreeInfo> treeInfoKey =
duke@1 53 new Context.Key<TreeInfo>();
duke@1 54
duke@1 55 public static TreeInfo instance(Context context) {
duke@1 56 TreeInfo instance = context.get(treeInfoKey);
duke@1 57 if (instance == null)
duke@1 58 instance = new TreeInfo(context);
duke@1 59 return instance;
duke@1 60 }
duke@1 61
duke@1 62 /** The names of all operators.
duke@1 63 */
jjg@1127 64 private Name[] opname = new Name[Tag.getNumberOfOperators()];
jjg@1127 65
jjg@1127 66 private void setOpname(Tag tag, String name, Names names) {
jjg@1127 67 setOpname(tag, names.fromString(name));
jjg@1127 68 }
jjg@1127 69 private void setOpname(Tag tag, Name name) {
jjg@1127 70 opname[tag.operatorIndex()] = name;
jjg@1127 71 }
duke@1 72
duke@1 73 private TreeInfo(Context context) {
duke@1 74 context.put(treeInfoKey, this);
duke@1 75
jjg@113 76 Names names = Names.instance(context);
jjg@1127 77 setOpname(POS, "+", names);
jjg@1127 78 setOpname(NEG, names.hyphen);
jjg@1127 79 setOpname(NOT, "!", names);
jjg@1127 80 setOpname(COMPL, "~", names);
jjg@1127 81 setOpname(PREINC, "++", names);
jjg@1127 82 setOpname(PREDEC, "--", names);
jjg@1127 83 setOpname(POSTINC, "++", names);
jjg@1127 84 setOpname(POSTDEC, "--", names);
jjg@1127 85 setOpname(NULLCHK, "<*nullchk*>", names);
jjg@1127 86 setOpname(OR, "||", names);
jjg@1127 87 setOpname(AND, "&&", names);
jjg@1127 88 setOpname(EQ, "==", names);
jjg@1127 89 setOpname(NE, "!=", names);
jjg@1127 90 setOpname(LT, "<", names);
jjg@1127 91 setOpname(GT, ">", names);
jjg@1127 92 setOpname(LE, "<=", names);
jjg@1127 93 setOpname(GE, ">=", names);
jjg@1127 94 setOpname(BITOR, "|", names);
jjg@1127 95 setOpname(BITXOR, "^", names);
jjg@1127 96 setOpname(BITAND, "&", names);
jjg@1127 97 setOpname(SL, "<<", names);
jjg@1127 98 setOpname(SR, ">>", names);
jjg@1127 99 setOpname(USR, ">>>", names);
jjg@1127 100 setOpname(PLUS, "+", names);
jjg@1127 101 setOpname(MINUS, names.hyphen);
jjg@1127 102 setOpname(MUL, names.asterisk);
jjg@1127 103 setOpname(DIV, names.slash);
jjg@1127 104 setOpname(MOD, "%", names);
duke@1 105 }
duke@1 106
mcimadamore@1219 107 public static List<JCExpression> args(JCTree t) {
mcimadamore@1219 108 switch (t.getTag()) {
mcimadamore@1219 109 case APPLY:
mcimadamore@1219 110 return ((JCMethodInvocation)t).args;
mcimadamore@1219 111 case NEWCLASS:
mcimadamore@1219 112 return ((JCNewClass)t).args;
mcimadamore@1219 113 default:
mcimadamore@1219 114 return null;
mcimadamore@1219 115 }
mcimadamore@1219 116 }
duke@1 117
duke@1 118 /** Return name of operator with given tree tag.
duke@1 119 */
jjg@1127 120 public Name operatorName(JCTree.Tag tag) {
jjg@1127 121 return opname[tag.operatorIndex()];
duke@1 122 }
duke@1 123
duke@1 124 /** Is tree a constructor declaration?
duke@1 125 */
duke@1 126 public static boolean isConstructor(JCTree tree) {
jjg@1127 127 if (tree.hasTag(METHODDEF)) {
duke@1 128 Name name = ((JCMethodDecl) tree).name;
jjg@113 129 return name == name.table.names.init;
duke@1 130 } else {
duke@1 131 return false;
duke@1 132 }
duke@1 133 }
duke@1 134
duke@1 135 /** Is there a constructor declaration in the given list of trees?
duke@1 136 */
duke@1 137 public static boolean hasConstructors(List<JCTree> trees) {
duke@1 138 for (List<JCTree> l = trees; l.nonEmpty(); l = l.tail)
duke@1 139 if (isConstructor(l.head)) return true;
duke@1 140 return false;
duke@1 141 }
duke@1 142
mcimadamore@550 143 public static boolean isMultiCatch(JCCatch catchClause) {
jjg@1127 144 return catchClause.param.vartype.hasTag(TYPEUNION);
mcimadamore@550 145 }
mcimadamore@550 146
duke@1 147 /** Is statement an initializer for a synthetic field?
duke@1 148 */
duke@1 149 public static boolean isSyntheticInit(JCTree stat) {
jjg@1127 150 if (stat.hasTag(EXEC)) {
duke@1 151 JCExpressionStatement exec = (JCExpressionStatement)stat;
jjg@1127 152 if (exec.expr.hasTag(ASSIGN)) {
duke@1 153 JCAssign assign = (JCAssign)exec.expr;
jjg@1127 154 if (assign.lhs.hasTag(SELECT)) {
duke@1 155 JCFieldAccess select = (JCFieldAccess)assign.lhs;
duke@1 156 if (select.sym != null &&
duke@1 157 (select.sym.flags() & SYNTHETIC) != 0) {
duke@1 158 Name selected = name(select.selected);
jjg@113 159 if (selected != null && selected == selected.table.names._this)
duke@1 160 return true;
duke@1 161 }
duke@1 162 }
duke@1 163 }
duke@1 164 }
duke@1 165 return false;
duke@1 166 }
duke@1 167
duke@1 168 /** If the expression is a method call, return the method name, null
duke@1 169 * otherwise. */
duke@1 170 public static Name calledMethodName(JCTree tree) {
jjg@1127 171 if (tree.hasTag(EXEC)) {
duke@1 172 JCExpressionStatement exec = (JCExpressionStatement)tree;
jjg@1127 173 if (exec.expr.hasTag(APPLY)) {
duke@1 174 Name mname = TreeInfo.name(((JCMethodInvocation) exec.expr).meth);
duke@1 175 return mname;
duke@1 176 }
duke@1 177 }
duke@1 178 return null;
duke@1 179 }
duke@1 180
duke@1 181 /** Is this a call to this or super?
duke@1 182 */
duke@1 183 public static boolean isSelfCall(JCTree tree) {
duke@1 184 Name name = calledMethodName(tree);
duke@1 185 if (name != null) {
jjg@113 186 Names names = name.table.names;
duke@1 187 return name==names._this || name==names._super;
duke@1 188 } else {
duke@1 189 return false;
duke@1 190 }
duke@1 191 }
duke@1 192
duke@1 193 /** Is this a call to super?
duke@1 194 */
duke@1 195 public static boolean isSuperCall(JCTree tree) {
duke@1 196 Name name = calledMethodName(tree);
duke@1 197 if (name != null) {
jjg@113 198 Names names = name.table.names;
duke@1 199 return name==names._super;
duke@1 200 } else {
duke@1 201 return false;
duke@1 202 }
duke@1 203 }
duke@1 204
duke@1 205 /** Is this a constructor whose first (non-synthetic) statement is not
duke@1 206 * of the form this(...)?
duke@1 207 */
duke@1 208 public static boolean isInitialConstructor(JCTree tree) {
duke@1 209 JCMethodInvocation app = firstConstructorCall(tree);
duke@1 210 if (app == null) return false;
duke@1 211 Name meth = name(app.meth);
jjg@113 212 return meth == null || meth != meth.table.names._this;
duke@1 213 }
duke@1 214
duke@1 215 /** Return the first call in a constructor definition. */
duke@1 216 public static JCMethodInvocation firstConstructorCall(JCTree tree) {
jjg@1127 217 if (!tree.hasTag(METHODDEF)) return null;
duke@1 218 JCMethodDecl md = (JCMethodDecl) tree;
jjg@113 219 Names names = md.name.table.names;
duke@1 220 if (md.name != names.init) return null;
duke@1 221 if (md.body == null) return null;
duke@1 222 List<JCStatement> stats = md.body.stats;
duke@1 223 // Synthetic initializations can appear before the super call.
duke@1 224 while (stats.nonEmpty() && isSyntheticInit(stats.head))
duke@1 225 stats = stats.tail;
duke@1 226 if (stats.isEmpty()) return null;
jjg@1127 227 if (!stats.head.hasTag(EXEC)) return null;
duke@1 228 JCExpressionStatement exec = (JCExpressionStatement) stats.head;
jjg@1127 229 if (!exec.expr.hasTag(APPLY)) return null;
duke@1 230 return (JCMethodInvocation)exec.expr;
duke@1 231 }
duke@1 232
mcimadamore@537 233 /** Return true if a tree represents a diamond new expr. */
mcimadamore@537 234 public static boolean isDiamond(JCTree tree) {
mcimadamore@537 235 switch(tree.getTag()) {
jjg@1127 236 case TYPEAPPLY: return ((JCTypeApply)tree).getTypeArguments().isEmpty();
jjg@1127 237 case NEWCLASS: return isDiamond(((JCNewClass)tree).clazz);
jjg@1563 238 case ANNOTATED_TYPE: return isDiamond(((JCAnnotatedType)tree).underlyingType);
mcimadamore@537 239 default: return false;
mcimadamore@537 240 }
mcimadamore@537 241 }
mcimadamore@537 242
mcimadamore@1269 243 public static boolean isEnumInit(JCTree tree) {
mcimadamore@1269 244 switch (tree.getTag()) {
mcimadamore@1269 245 case VARDEF:
mcimadamore@1269 246 return (((JCVariableDecl)tree).mods.flags & ENUM) != 0;
mcimadamore@1269 247 default:
mcimadamore@1269 248 return false;
mcimadamore@1269 249 }
mcimadamore@1269 250 }
mcimadamore@1269 251
mcimadamore@1347 252 /** Return true if a a tree corresponds to a poly expression. */
mcimadamore@1347 253 public static boolean isPoly(JCTree tree, JCTree origin) {
mcimadamore@1347 254 switch (tree.getTag()) {
mcimadamore@1347 255 case APPLY:
mcimadamore@1347 256 case NEWCLASS:
mcimadamore@1347 257 case CONDEXPR:
mcimadamore@1347 258 return !origin.hasTag(TYPECAST);
mcimadamore@1347 259 case LAMBDA:
mcimadamore@1347 260 case REFERENCE:
mcimadamore@1347 261 return true;
mcimadamore@1347 262 case PARENS:
mcimadamore@1347 263 return isPoly(((JCParens)tree).expr, origin);
mcimadamore@1347 264 default:
mcimadamore@1347 265 return false;
mcimadamore@1347 266 }
mcimadamore@1347 267 }
mcimadamore@1347 268
mcimadamore@1510 269 /** set 'polyKind' on given tree */
mcimadamore@1510 270 public static void setPolyKind(JCTree tree, PolyKind pkind) {
mcimadamore@1510 271 switch (tree.getTag()) {
mcimadamore@1510 272 case APPLY:
mcimadamore@1510 273 ((JCMethodInvocation)tree).polyKind = pkind;
mcimadamore@1510 274 break;
mcimadamore@1510 275 case NEWCLASS:
mcimadamore@1510 276 ((JCNewClass)tree).polyKind = pkind;
mcimadamore@1510 277 break;
mcimadamore@1510 278 case REFERENCE:
mcimadamore@1510 279 ((JCMemberReference)tree).refPolyKind = pkind;
mcimadamore@1510 280 break;
mcimadamore@1510 281 default:
mcimadamore@1510 282 throw new AssertionError("Unexpected tree: " + tree);
mcimadamore@1510 283 }
mcimadamore@1510 284 }
mcimadamore@1510 285
mcimadamore@1510 286 /** set 'varargsElement' on given tree */
mcimadamore@1510 287 public static void setVarargsElement(JCTree tree, Type varargsElement) {
mcimadamore@1510 288 switch (tree.getTag()) {
mcimadamore@1510 289 case APPLY:
mcimadamore@1510 290 ((JCMethodInvocation)tree).varargsElement = varargsElement;
mcimadamore@1510 291 break;
mcimadamore@1510 292 case NEWCLASS:
mcimadamore@1510 293 ((JCNewClass)tree).varargsElement = varargsElement;
mcimadamore@1510 294 break;
mcimadamore@1510 295 case REFERENCE:
mcimadamore@1510 296 ((JCMemberReference)tree).varargsElement = varargsElement;
mcimadamore@1510 297 break;
mcimadamore@1510 298 default:
mcimadamore@1510 299 throw new AssertionError("Unexpected tree: " + tree);
mcimadamore@1510 300 }
mcimadamore@1348 301 }
mcimadamore@1433 302
mcimadamore@1433 303 /** Return true if the tree corresponds to an expression statement */
mcimadamore@1433 304 public static boolean isExpressionStatement(JCExpression tree) {
mcimadamore@1433 305 switch(tree.getTag()) {
mcimadamore@1433 306 case PREINC: case PREDEC:
mcimadamore@1433 307 case POSTINC: case POSTDEC:
mcimadamore@1433 308 case ASSIGN:
mcimadamore@1433 309 case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG:
mcimadamore@1433 310 case SL_ASG: case SR_ASG: case USR_ASG:
mcimadamore@1433 311 case PLUS_ASG: case MINUS_ASG:
mcimadamore@1433 312 case MUL_ASG: case DIV_ASG: case MOD_ASG:
mcimadamore@1433 313 case APPLY: case NEWCLASS:
mcimadamore@1433 314 case ERRONEOUS:
mcimadamore@1433 315 return true;
mcimadamore@1433 316 default:
mcimadamore@1433 317 return false;
mcimadamore@1433 318 }
mcimadamore@1433 319 }
mcimadamore@1433 320
mcimadamore@1143 321 /**
mcimadamore@1143 322 * Return true if the AST corresponds to a static select of the kind A.B
mcimadamore@1143 323 */
mcimadamore@1143 324 public static boolean isStaticSelector(JCTree base, Names names) {
mcimadamore@1143 325 if (base == null)
mcimadamore@1143 326 return false;
mcimadamore@1143 327 switch (base.getTag()) {
mcimadamore@1143 328 case IDENT:
mcimadamore@1143 329 JCIdent id = (JCIdent)base;
mcimadamore@1143 330 return id.name != names._this &&
mcimadamore@1143 331 id.name != names._super &&
mcimadamore@1143 332 isStaticSym(base);
mcimadamore@1143 333 case SELECT:
mcimadamore@1143 334 return isStaticSym(base) &&
mcimadamore@1143 335 isStaticSelector(((JCFieldAccess)base).selected, names);
mcimadamore@1143 336 case TYPEAPPLY:
mcimadamore@1352 337 case TYPEARRAY:
mcimadamore@1143 338 return true;
jjg@1563 339 case ANNOTATED_TYPE:
jjg@1563 340 return isStaticSelector(((JCAnnotatedType)base).underlyingType, names);
mcimadamore@1143 341 default:
mcimadamore@1143 342 return false;
mcimadamore@1143 343 }
mcimadamore@1143 344 }
mcimadamore@1143 345 //where
mcimadamore@1143 346 private static boolean isStaticSym(JCTree tree) {
mcimadamore@1143 347 Symbol sym = symbol(tree);
mcimadamore@1143 348 return (sym.kind == Kinds.TYP ||
mcimadamore@1143 349 sym.kind == Kinds.PCK);
mcimadamore@1143 350 }
mcimadamore@1143 351
duke@1 352 /** Return true if a tree represents the null literal. */
duke@1 353 public static boolean isNull(JCTree tree) {
jjg@1127 354 if (!tree.hasTag(LITERAL))
duke@1 355 return false;
duke@1 356 JCLiteral lit = (JCLiteral) tree;
jjg@1374 357 return (lit.typetag == BOT);
duke@1 358 }
duke@1 359
jjg@1280 360 public static String getCommentText(Env<?> env, JCTree tree) {
jjg@1280 361 DocCommentTable docComments = (tree.hasTag(JCTree.Tag.TOPLEVEL))
jjg@1280 362 ? ((JCCompilationUnit) tree).docComments
jjg@1280 363 : env.toplevel.docComments;
jjg@1281 364 return (docComments == null) ? null : docComments.getCommentText(tree);
jjg@1280 365 }
jjg@1280 366
jjg@1455 367 public static DCTree.DCDocComment getCommentTree(Env<?> env, JCTree tree) {
jjg@1455 368 DocCommentTable docComments = (tree.hasTag(JCTree.Tag.TOPLEVEL))
jjg@1455 369 ? ((JCCompilationUnit) tree).docComments
jjg@1455 370 : env.toplevel.docComments;
jjg@1455 371 return (docComments == null) ? null : docComments.getCommentTree(tree);
jjg@1455 372 }
jjg@1455 373
duke@1 374 /** The position of the first statement in a block, or the position of
duke@1 375 * the block itself if it is empty.
duke@1 376 */
duke@1 377 public static int firstStatPos(JCTree tree) {
jjg@1127 378 if (tree.hasTag(BLOCK) && ((JCBlock) tree).stats.nonEmpty())
duke@1 379 return ((JCBlock) tree).stats.head.pos;
duke@1 380 else
duke@1 381 return tree.pos;
duke@1 382 }
duke@1 383
duke@1 384 /** The end position of given tree, if it is a block with
duke@1 385 * defined endpos.
duke@1 386 */
duke@1 387 public static int endPos(JCTree tree) {
jjg@1127 388 if (tree.hasTag(BLOCK) && ((JCBlock) tree).endpos != Position.NOPOS)
duke@1 389 return ((JCBlock) tree).endpos;
jjg@1127 390 else if (tree.hasTag(SYNCHRONIZED))
duke@1 391 return endPos(((JCSynchronized) tree).body);
jjg@1127 392 else if (tree.hasTag(TRY)) {
duke@1 393 JCTry t = (JCTry) tree;
sundar@1300 394 return endPos((t.finalizer != null) ? t.finalizer
sundar@1300 395 : (t.catchers.nonEmpty() ? t.catchers.last().body : t.body));
duke@1 396 } else
duke@1 397 return tree.pos;
duke@1 398 }
duke@1 399
duke@1 400
duke@1 401 /** Get the start position for a tree node. The start position is
duke@1 402 * defined to be the position of the first character of the first
duke@1 403 * token of the node's source text.
duke@1 404 * @param tree The tree node
duke@1 405 */
duke@1 406 public static int getStartPos(JCTree tree) {
duke@1 407 if (tree == null)
duke@1 408 return Position.NOPOS;
duke@1 409
duke@1 410 switch(tree.getTag()) {
jjg@1127 411 case APPLY:
jjg@1127 412 return getStartPos(((JCMethodInvocation) tree).meth);
jjg@1127 413 case ASSIGN:
jjg@1127 414 return getStartPos(((JCAssign) tree).lhs);
jjg@1127 415 case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG:
jjg@1127 416 case SL_ASG: case SR_ASG: case USR_ASG:
jjg@1127 417 case PLUS_ASG: case MINUS_ASG: case MUL_ASG:
jjg@1127 418 case DIV_ASG: case MOD_ASG:
jjg@1127 419 return getStartPos(((JCAssignOp) tree).lhs);
jjg@1127 420 case OR: case AND: case BITOR:
jjg@1127 421 case BITXOR: case BITAND: case EQ:
jjg@1127 422 case NE: case LT: case GT:
jjg@1127 423 case LE: case GE: case SL:
jjg@1127 424 case SR: case USR: case PLUS:
jjg@1127 425 case MINUS: case MUL: case DIV:
jjg@1127 426 case MOD:
jjg@1127 427 return getStartPos(((JCBinary) tree).lhs);
jjg@1127 428 case CLASSDEF: {
jjg@1127 429 JCClassDecl node = (JCClassDecl)tree;
jjg@1127 430 if (node.mods.pos != Position.NOPOS)
jjg@1127 431 return node.mods.pos;
jjg@1127 432 break;
duke@1 433 }
jjg@1127 434 case CONDEXPR:
jjg@1127 435 return getStartPos(((JCConditional) tree).cond);
jjg@1127 436 case EXEC:
jjg@1127 437 return getStartPos(((JCExpressionStatement) tree).expr);
jjg@1127 438 case INDEXED:
jjg@1127 439 return getStartPos(((JCArrayAccess) tree).indexed);
jjg@1127 440 case METHODDEF: {
jjg@1127 441 JCMethodDecl node = (JCMethodDecl)tree;
jjg@1127 442 if (node.mods.pos != Position.NOPOS)
jjg@1127 443 return node.mods.pos;
jjg@1127 444 if (node.typarams.nonEmpty()) // List.nil() used for no typarams
jjg@1127 445 return getStartPos(node.typarams.head);
jjg@1127 446 return node.restype == null ? node.pos : getStartPos(node.restype);
jjg@1127 447 }
jjg@1127 448 case SELECT:
jjg@1127 449 return getStartPos(((JCFieldAccess) tree).selected);
jjg@1127 450 case TYPEAPPLY:
jjg@1127 451 return getStartPos(((JCTypeApply) tree).clazz);
jjg@1127 452 case TYPEARRAY:
jjg@1127 453 return getStartPos(((JCArrayTypeTree) tree).elemtype);
jjg@1127 454 case TYPETEST:
jjg@1127 455 return getStartPos(((JCInstanceOf) tree).expr);
jjg@1127 456 case POSTINC:
jjg@1127 457 case POSTDEC:
jjg@1127 458 return getStartPos(((JCUnary) tree).arg);
jjg@1521 459 case ANNOTATED_TYPE: {
jjg@1521 460 JCAnnotatedType node = (JCAnnotatedType) tree;
jjg@1521 461 if (node.annotations.nonEmpty()) {
jjg@1521 462 if (node.underlyingType.hasTag(TYPEARRAY) ||
jjg@1521 463 node.underlyingType.hasTag(SELECT)) {
jjg@1521 464 return getStartPos(node.underlyingType);
jjg@1521 465 } else {
jjg@1521 466 return getStartPos(node.annotations.head);
jjg@1521 467 }
jjg@1521 468 } else {
jjg@1521 469 return getStartPos(node.underlyingType);
jjg@1521 470 }
jjg@1521 471 }
jjg@1127 472 case NEWCLASS: {
jjg@1127 473 JCNewClass node = (JCNewClass)tree;
jjg@1127 474 if (node.encl != null)
jjg@1127 475 return getStartPos(node.encl);
jjg@1127 476 break;
jjg@1127 477 }
jjg@1127 478 case VARDEF: {
jjg@1127 479 JCVariableDecl node = (JCVariableDecl)tree;
jjg@1127 480 if (node.mods.pos != Position.NOPOS) {
jjg@1127 481 return node.mods.pos;
mcimadamore@1348 482 } else if (node.vartype == null) {
mcimadamore@1348 483 //if there's no type (partially typed lambda parameter)
mcimadamore@1348 484 //simply return node position
mcimadamore@1348 485 return node.pos;
jjg@1127 486 } else {
jjg@1127 487 return getStartPos(node.vartype);
jjg@1127 488 }
jjg@1127 489 }
jjg@1127 490 case ERRONEOUS: {
jjg@1127 491 JCErroneous node = (JCErroneous)tree;
jjg@1127 492 if (node.errs != null && node.errs.nonEmpty())
jjg@1127 493 return getStartPos(node.errs.head);
jjg@1127 494 }
duke@1 495 }
duke@1 496 return tree.pos;
duke@1 497 }
duke@1 498
duke@1 499 /** The end position of given tree, given a table of end positions generated by the parser
duke@1 500 */
ksrini@1138 501 public static int getEndPos(JCTree tree, EndPosTable endPosTable) {
duke@1 502 if (tree == null)
duke@1 503 return Position.NOPOS;
duke@1 504
ksrini@1138 505 if (endPosTable == null) {
duke@1 506 // fall back on limited info in the tree
duke@1 507 return endPos(tree);
duke@1 508 }
duke@1 509
ksrini@1138 510 int mapPos = endPosTable.getEndPos(tree);
ksrini@1138 511 if (mapPos != Position.NOPOS)
duke@1 512 return mapPos;
duke@1 513
duke@1 514 switch(tree.getTag()) {
jjg@1127 515 case BITOR_ASG: case BITXOR_ASG: case BITAND_ASG:
jjg@1127 516 case SL_ASG: case SR_ASG: case USR_ASG:
jjg@1127 517 case PLUS_ASG: case MINUS_ASG: case MUL_ASG:
jjg@1127 518 case DIV_ASG: case MOD_ASG:
ksrini@1138 519 return getEndPos(((JCAssignOp) tree).rhs, endPosTable);
jjg@1127 520 case OR: case AND: case BITOR:
jjg@1127 521 case BITXOR: case BITAND: case EQ:
jjg@1127 522 case NE: case LT: case GT:
jjg@1127 523 case LE: case GE: case SL:
jjg@1127 524 case SR: case USR: case PLUS:
jjg@1127 525 case MINUS: case MUL: case DIV:
jjg@1127 526 case MOD:
ksrini@1138 527 return getEndPos(((JCBinary) tree).rhs, endPosTable);
jjg@1127 528 case CASE:
ksrini@1138 529 return getEndPos(((JCCase) tree).stats.last(), endPosTable);
jjg@1127 530 case CATCH:
ksrini@1138 531 return getEndPos(((JCCatch) tree).body, endPosTable);
jjg@1127 532 case CONDEXPR:
ksrini@1138 533 return getEndPos(((JCConditional) tree).falsepart, endPosTable);
jjg@1127 534 case FORLOOP:
ksrini@1138 535 return getEndPos(((JCForLoop) tree).body, endPosTable);
jjg@1127 536 case FOREACHLOOP:
ksrini@1138 537 return getEndPos(((JCEnhancedForLoop) tree).body, endPosTable);
jjg@1127 538 case IF: {
jjg@1127 539 JCIf node = (JCIf)tree;
jjg@1127 540 if (node.elsepart == null) {
ksrini@1138 541 return getEndPos(node.thenpart, endPosTable);
jjg@1127 542 } else {
ksrini@1138 543 return getEndPos(node.elsepart, endPosTable);
jjg@1127 544 }
duke@1 545 }
jjg@1127 546 case LABELLED:
ksrini@1138 547 return getEndPos(((JCLabeledStatement) tree).body, endPosTable);
jjg@1127 548 case MODIFIERS:
ksrini@1138 549 return getEndPos(((JCModifiers) tree).annotations.last(), endPosTable);
jjg@1127 550 case SYNCHRONIZED:
ksrini@1138 551 return getEndPos(((JCSynchronized) tree).body, endPosTable);
jjg@1127 552 case TOPLEVEL:
ksrini@1138 553 return getEndPos(((JCCompilationUnit) tree).defs.last(), endPosTable);
jjg@1127 554 case TRY: {
jjg@1127 555 JCTry node = (JCTry)tree;
jjg@1127 556 if (node.finalizer != null) {
ksrini@1138 557 return getEndPos(node.finalizer, endPosTable);
jjg@1127 558 } else if (!node.catchers.isEmpty()) {
ksrini@1138 559 return getEndPos(node.catchers.last(), endPosTable);
jjg@1127 560 } else {
ksrini@1138 561 return getEndPos(node.body, endPosTable);
jjg@1127 562 }
duke@1 563 }
jjg@1127 564 case WILDCARD:
ksrini@1138 565 return getEndPos(((JCWildcard) tree).inner, endPosTable);
jjg@1127 566 case TYPECAST:
ksrini@1138 567 return getEndPos(((JCTypeCast) tree).expr, endPosTable);
jjg@1127 568 case TYPETEST:
ksrini@1138 569 return getEndPos(((JCInstanceOf) tree).clazz, endPosTable);
jjg@1127 570 case POS:
jjg@1127 571 case NEG:
jjg@1127 572 case NOT:
jjg@1127 573 case COMPL:
jjg@1127 574 case PREINC:
jjg@1127 575 case PREDEC:
ksrini@1138 576 return getEndPos(((JCUnary) tree).arg, endPosTable);
jjg@1127 577 case WHILELOOP:
ksrini@1138 578 return getEndPos(((JCWhileLoop) tree).body, endPosTable);
jjg@1521 579 case ANNOTATED_TYPE:
jjg@1521 580 return getEndPos(((JCAnnotatedType) tree).underlyingType, endPosTable);
jjg@1127 581 case ERRONEOUS: {
jjg@1127 582 JCErroneous node = (JCErroneous)tree;
jjg@1127 583 if (node.errs != null && node.errs.nonEmpty())
ksrini@1138 584 return getEndPos(node.errs.last(), endPosTable);
jjg@1127 585 }
duke@1 586 }
duke@1 587 return Position.NOPOS;
duke@1 588 }
duke@1 589
duke@1 590
duke@1 591 /** A DiagnosticPosition with the preferred position set to the
duke@1 592 * end position of given tree, if it is a block with
duke@1 593 * defined endpos.
duke@1 594 */
duke@1 595 public static DiagnosticPosition diagEndPos(final JCTree tree) {
duke@1 596 final int endPos = TreeInfo.endPos(tree);
duke@1 597 return new DiagnosticPosition() {
duke@1 598 public JCTree getTree() { return tree; }
duke@1 599 public int getStartPosition() { return TreeInfo.getStartPos(tree); }
duke@1 600 public int getPreferredPosition() { return endPos; }
ksrini@1138 601 public int getEndPosition(EndPosTable endPosTable) {
duke@1 602 return TreeInfo.getEndPos(tree, endPosTable);
duke@1 603 }
duke@1 604 };
duke@1 605 }
duke@1 606
duke@1 607 /** The position of the finalizer of given try/synchronized statement.
duke@1 608 */
duke@1 609 public static int finalizerPos(JCTree tree) {
jjg@1127 610 if (tree.hasTag(TRY)) {
duke@1 611 JCTry t = (JCTry) tree;
jjg@816 612 Assert.checkNonNull(t.finalizer);
duke@1 613 return firstStatPos(t.finalizer);
jjg@1127 614 } else if (tree.hasTag(SYNCHRONIZED)) {
duke@1 615 return endPos(((JCSynchronized) tree).body);
duke@1 616 } else {
duke@1 617 throw new AssertionError();
duke@1 618 }
duke@1 619 }
duke@1 620
duke@1 621 /** Find the position for reporting an error about a symbol, where
duke@1 622 * that symbol is defined somewhere in the given tree. */
duke@1 623 public static int positionFor(final Symbol sym, final JCTree tree) {
duke@1 624 JCTree decl = declarationFor(sym, tree);
duke@1 625 return ((decl != null) ? decl : tree).pos;
duke@1 626 }
duke@1 627
duke@1 628 /** Find the position for reporting an error about a symbol, where
duke@1 629 * that symbol is defined somewhere in the given tree. */
duke@1 630 public static DiagnosticPosition diagnosticPositionFor(final Symbol sym, final JCTree tree) {
duke@1 631 JCTree decl = declarationFor(sym, tree);
duke@1 632 return ((decl != null) ? decl : tree).pos();
duke@1 633 }
duke@1 634
duke@1 635 /** Find the declaration for a symbol, where
duke@1 636 * that symbol is defined somewhere in the given tree. */
duke@1 637 public static JCTree declarationFor(final Symbol sym, final JCTree tree) {
duke@1 638 class DeclScanner extends TreeScanner {
duke@1 639 JCTree result = null;
duke@1 640 public void scan(JCTree tree) {
duke@1 641 if (tree!=null && result==null)
duke@1 642 tree.accept(this);
duke@1 643 }
duke@1 644 public void visitTopLevel(JCCompilationUnit that) {
duke@1 645 if (that.packge == sym) result = that;
duke@1 646 else super.visitTopLevel(that);
duke@1 647 }
duke@1 648 public void visitClassDef(JCClassDecl that) {
duke@1 649 if (that.sym == sym) result = that;
duke@1 650 else super.visitClassDef(that);
duke@1 651 }
duke@1 652 public void visitMethodDef(JCMethodDecl that) {
duke@1 653 if (that.sym == sym) result = that;
duke@1 654 else super.visitMethodDef(that);
duke@1 655 }
duke@1 656 public void visitVarDef(JCVariableDecl that) {
duke@1 657 if (that.sym == sym) result = that;
duke@1 658 else super.visitVarDef(that);
duke@1 659 }
sundar@668 660 public void visitTypeParameter(JCTypeParameter that) {
jjh@771 661 if (that.type != null && that.type.tsym == sym) result = that;
sundar@668 662 else super.visitTypeParameter(that);
sundar@668 663 }
duke@1 664 }
duke@1 665 DeclScanner s = new DeclScanner();
duke@1 666 tree.accept(s);
duke@1 667 return s.result;
duke@1 668 }
duke@1 669
duke@1 670 public static Env<AttrContext> scopeFor(JCTree node, JCCompilationUnit unit) {
duke@1 671 return scopeFor(pathFor(node, unit));
duke@1 672 }
duke@1 673
duke@1 674 public static Env<AttrContext> scopeFor(List<JCTree> path) {
duke@1 675 // TODO: not implemented yet
duke@1 676 throw new UnsupportedOperationException("not implemented yet");
duke@1 677 }
duke@1 678
duke@1 679 public static List<JCTree> pathFor(final JCTree node, final JCCompilationUnit unit) {
duke@1 680 class Result extends Error {
duke@1 681 static final long serialVersionUID = -5942088234594905625L;
duke@1 682 List<JCTree> path;
duke@1 683 Result(List<JCTree> path) {
duke@1 684 this.path = path;
duke@1 685 }
duke@1 686 }
duke@1 687 class PathFinder extends TreeScanner {
duke@1 688 List<JCTree> path = List.nil();
duke@1 689 public void scan(JCTree tree) {
duke@1 690 if (tree != null) {
duke@1 691 path = path.prepend(tree);
duke@1 692 if (tree == node)
duke@1 693 throw new Result(path);
duke@1 694 super.scan(tree);
duke@1 695 path = path.tail;
duke@1 696 }
duke@1 697 }
duke@1 698 }
duke@1 699 try {
duke@1 700 new PathFinder().scan(unit);
duke@1 701 } catch (Result result) {
duke@1 702 return result.path;
duke@1 703 }
duke@1 704 return List.nil();
duke@1 705 }
duke@1 706
duke@1 707 /** Return the statement referenced by a label.
duke@1 708 * If the label refers to a loop or switch, return that switch
duke@1 709 * otherwise return the labelled statement itself
duke@1 710 */
duke@1 711 public static JCTree referencedStatement(JCLabeledStatement tree) {
duke@1 712 JCTree t = tree;
duke@1 713 do t = ((JCLabeledStatement) t).body;
jjg@1127 714 while (t.hasTag(LABELLED));
duke@1 715 switch (t.getTag()) {
jjg@1127 716 case DOLOOP: case WHILELOOP: case FORLOOP: case FOREACHLOOP: case SWITCH:
duke@1 717 return t;
duke@1 718 default:
duke@1 719 return tree;
duke@1 720 }
duke@1 721 }
duke@1 722
duke@1 723 /** Skip parens and return the enclosed expression
duke@1 724 */
duke@1 725 public static JCExpression skipParens(JCExpression tree) {
jjg@1127 726 while (tree.hasTag(PARENS)) {
duke@1 727 tree = ((JCParens) tree).expr;
duke@1 728 }
duke@1 729 return tree;
duke@1 730 }
duke@1 731
duke@1 732 /** Skip parens and return the enclosed expression
duke@1 733 */
duke@1 734 public static JCTree skipParens(JCTree tree) {
jjg@1127 735 if (tree.hasTag(PARENS))
duke@1 736 return skipParens((JCParens)tree);
duke@1 737 else
duke@1 738 return tree;
duke@1 739 }
duke@1 740
duke@1 741 /** Return the types of a list of trees.
duke@1 742 */
duke@1 743 public static List<Type> types(List<? extends JCTree> trees) {
duke@1 744 ListBuffer<Type> ts = new ListBuffer<Type>();
duke@1 745 for (List<? extends JCTree> l = trees; l.nonEmpty(); l = l.tail)
duke@1 746 ts.append(l.head.type);
duke@1 747 return ts.toList();
duke@1 748 }
duke@1 749
duke@1 750 /** If this tree is an identifier or a field or a parameterized type,
duke@1 751 * return its name, otherwise return null.
duke@1 752 */
duke@1 753 public static Name name(JCTree tree) {
duke@1 754 switch (tree.getTag()) {
jjg@1127 755 case IDENT:
duke@1 756 return ((JCIdent) tree).name;
jjg@1127 757 case SELECT:
duke@1 758 return ((JCFieldAccess) tree).name;
jjg@1127 759 case TYPEAPPLY:
duke@1 760 return name(((JCTypeApply) tree).clazz);
duke@1 761 default:
duke@1 762 return null;
duke@1 763 }
duke@1 764 }
duke@1 765
duke@1 766 /** If this tree is a qualified identifier, its return fully qualified name,
duke@1 767 * otherwise return null.
duke@1 768 */
duke@1 769 public static Name fullName(JCTree tree) {
duke@1 770 tree = skipParens(tree);
duke@1 771 switch (tree.getTag()) {
jjg@1127 772 case IDENT:
duke@1 773 return ((JCIdent) tree).name;
jjg@1127 774 case SELECT:
duke@1 775 Name sname = fullName(((JCFieldAccess) tree).selected);
duke@1 776 return sname == null ? null : sname.append('.', name(tree));
duke@1 777 default:
duke@1 778 return null;
duke@1 779 }
duke@1 780 }
duke@1 781
duke@1 782 public static Symbol symbolFor(JCTree node) {
duke@1 783 node = skipParens(node);
duke@1 784 switch (node.getTag()) {
jjg@1127 785 case CLASSDEF:
duke@1 786 return ((JCClassDecl) node).sym;
jjg@1127 787 case METHODDEF:
duke@1 788 return ((JCMethodDecl) node).sym;
jjg@1127 789 case VARDEF:
duke@1 790 return ((JCVariableDecl) node).sym;
duke@1 791 default:
duke@1 792 return null;
duke@1 793 }
duke@1 794 }
duke@1 795
jjg@672 796 public static boolean isDeclaration(JCTree node) {
jjg@672 797 node = skipParens(node);
jjg@672 798 switch (node.getTag()) {
jjg@1127 799 case CLASSDEF:
jjg@1127 800 case METHODDEF:
jjg@1127 801 case VARDEF:
jjg@672 802 return true;
jjg@672 803 default:
jjg@672 804 return false;
jjg@672 805 }
jjg@672 806 }
jjg@672 807
duke@1 808 /** If this tree is an identifier or a field, return its symbol,
duke@1 809 * otherwise return null.
duke@1 810 */
duke@1 811 public static Symbol symbol(JCTree tree) {
duke@1 812 tree = skipParens(tree);
duke@1 813 switch (tree.getTag()) {
jjg@1127 814 case IDENT:
duke@1 815 return ((JCIdent) tree).sym;
jjg@1127 816 case SELECT:
duke@1 817 return ((JCFieldAccess) tree).sym;
jjg@1127 818 case TYPEAPPLY:
duke@1 819 return symbol(((JCTypeApply) tree).clazz);
jjg@1521 820 case ANNOTATED_TYPE:
jjg@1521 821 return symbol(((JCAnnotatedType) tree).underlyingType);
duke@1 822 default:
duke@1 823 return null;
duke@1 824 }
duke@1 825 }
duke@1 826
duke@1 827 /** Return true if this is a nonstatic selection. */
duke@1 828 public static boolean nonstaticSelect(JCTree tree) {
duke@1 829 tree = skipParens(tree);
jjg@1127 830 if (!tree.hasTag(SELECT)) return false;
duke@1 831 JCFieldAccess s = (JCFieldAccess) tree;
duke@1 832 Symbol e = symbol(s.selected);
duke@1 833 return e == null || (e.kind != Kinds.PCK && e.kind != Kinds.TYP);
duke@1 834 }
duke@1 835
duke@1 836 /** If this tree is an identifier or a field, set its symbol, otherwise skip.
duke@1 837 */
duke@1 838 public static void setSymbol(JCTree tree, Symbol sym) {
duke@1 839 tree = skipParens(tree);
duke@1 840 switch (tree.getTag()) {
jjg@1127 841 case IDENT:
duke@1 842 ((JCIdent) tree).sym = sym; break;
jjg@1127 843 case SELECT:
duke@1 844 ((JCFieldAccess) tree).sym = sym; break;
duke@1 845 default:
duke@1 846 }
duke@1 847 }
duke@1 848
duke@1 849 /** If this tree is a declaration or a block, return its flags field,
duke@1 850 * otherwise return 0.
duke@1 851 */
duke@1 852 public static long flags(JCTree tree) {
duke@1 853 switch (tree.getTag()) {
jjg@1127 854 case VARDEF:
duke@1 855 return ((JCVariableDecl) tree).mods.flags;
jjg@1127 856 case METHODDEF:
duke@1 857 return ((JCMethodDecl) tree).mods.flags;
jjg@1127 858 case CLASSDEF:
duke@1 859 return ((JCClassDecl) tree).mods.flags;
jjg@1127 860 case BLOCK:
duke@1 861 return ((JCBlock) tree).flags;
duke@1 862 default:
duke@1 863 return 0;
duke@1 864 }
duke@1 865 }
duke@1 866
duke@1 867 /** Return first (smallest) flag in `flags':
duke@1 868 * pre: flags != 0
duke@1 869 */
duke@1 870 public static long firstFlag(long flags) {
mcimadamore@1366 871 long flag = 1;
mcimadamore@1366 872 while ((flag & flags & ExtendedStandardFlags) == 0)
duke@1 873 flag = flag << 1;
duke@1 874 return flag;
duke@1 875 }
duke@1 876
duke@1 877 /** Return flags as a string, separated by " ".
duke@1 878 */
duke@1 879 public static String flagNames(long flags) {
mcimadamore@1366 880 return Flags.toString(flags & ExtendedStandardFlags).trim();
duke@1 881 }
duke@1 882
duke@1 883 /** Operator precedences values.
duke@1 884 */
duke@1 885 public static final int
duke@1 886 notExpression = -1, // not an expression
duke@1 887 noPrec = 0, // no enclosing expression
duke@1 888 assignPrec = 1,
duke@1 889 assignopPrec = 2,
duke@1 890 condPrec = 3,
duke@1 891 orPrec = 4,
duke@1 892 andPrec = 5,
duke@1 893 bitorPrec = 6,
duke@1 894 bitxorPrec = 7,
duke@1 895 bitandPrec = 8,
duke@1 896 eqPrec = 9,
duke@1 897 ordPrec = 10,
duke@1 898 shiftPrec = 11,
duke@1 899 addPrec = 12,
duke@1 900 mulPrec = 13,
duke@1 901 prefixPrec = 14,
duke@1 902 postfixPrec = 15,
duke@1 903 precCount = 16;
duke@1 904
duke@1 905
duke@1 906 /** Map operators to their precedence levels.
duke@1 907 */
jjg@1127 908 public static int opPrec(JCTree.Tag op) {
duke@1 909 switch(op) {
jjg@1127 910 case POS:
jjg@1127 911 case NEG:
jjg@1127 912 case NOT:
jjg@1127 913 case COMPL:
jjg@1127 914 case PREINC:
jjg@1127 915 case PREDEC: return prefixPrec;
jjg@1127 916 case POSTINC:
jjg@1127 917 case POSTDEC:
jjg@1127 918 case NULLCHK: return postfixPrec;
jjg@1127 919 case ASSIGN: return assignPrec;
jjg@1127 920 case BITOR_ASG:
jjg@1127 921 case BITXOR_ASG:
jjg@1127 922 case BITAND_ASG:
jjg@1127 923 case SL_ASG:
jjg@1127 924 case SR_ASG:
jjg@1127 925 case USR_ASG:
jjg@1127 926 case PLUS_ASG:
jjg@1127 927 case MINUS_ASG:
jjg@1127 928 case MUL_ASG:
jjg@1127 929 case DIV_ASG:
jjg@1127 930 case MOD_ASG: return assignopPrec;
jjg@1127 931 case OR: return orPrec;
jjg@1127 932 case AND: return andPrec;
jjg@1127 933 case EQ:
jjg@1127 934 case NE: return eqPrec;
jjg@1127 935 case LT:
jjg@1127 936 case GT:
jjg@1127 937 case LE:
jjg@1127 938 case GE: return ordPrec;
jjg@1127 939 case BITOR: return bitorPrec;
jjg@1127 940 case BITXOR: return bitxorPrec;
jjg@1127 941 case BITAND: return bitandPrec;
jjg@1127 942 case SL:
jjg@1127 943 case SR:
jjg@1127 944 case USR: return shiftPrec;
jjg@1127 945 case PLUS:
jjg@1127 946 case MINUS: return addPrec;
jjg@1127 947 case MUL:
jjg@1127 948 case DIV:
jjg@1127 949 case MOD: return mulPrec;
jjg@1127 950 case TYPETEST: return ordPrec;
duke@1 951 default: throw new AssertionError();
duke@1 952 }
duke@1 953 }
duke@1 954
jjg@1127 955 static Tree.Kind tagToKind(JCTree.Tag tag) {
duke@1 956 switch (tag) {
duke@1 957 // Postfix expressions
jjg@1127 958 case POSTINC: // _ ++
duke@1 959 return Tree.Kind.POSTFIX_INCREMENT;
jjg@1127 960 case POSTDEC: // _ --
duke@1 961 return Tree.Kind.POSTFIX_DECREMENT;
duke@1 962
duke@1 963 // Unary operators
jjg@1127 964 case PREINC: // ++ _
duke@1 965 return Tree.Kind.PREFIX_INCREMENT;
jjg@1127 966 case PREDEC: // -- _
duke@1 967 return Tree.Kind.PREFIX_DECREMENT;
jjg@1127 968 case POS: // +
duke@1 969 return Tree.Kind.UNARY_PLUS;
jjg@1127 970 case NEG: // -
duke@1 971 return Tree.Kind.UNARY_MINUS;
jjg@1127 972 case COMPL: // ~
duke@1 973 return Tree.Kind.BITWISE_COMPLEMENT;
jjg@1127 974 case NOT: // !
duke@1 975 return Tree.Kind.LOGICAL_COMPLEMENT;
duke@1 976
duke@1 977 // Binary operators
duke@1 978
duke@1 979 // Multiplicative operators
jjg@1127 980 case MUL: // *
duke@1 981 return Tree.Kind.MULTIPLY;
jjg@1127 982 case DIV: // /
duke@1 983 return Tree.Kind.DIVIDE;
jjg@1127 984 case MOD: // %
duke@1 985 return Tree.Kind.REMAINDER;
duke@1 986
duke@1 987 // Additive operators
jjg@1127 988 case PLUS: // +
duke@1 989 return Tree.Kind.PLUS;
jjg@1127 990 case MINUS: // -
duke@1 991 return Tree.Kind.MINUS;
duke@1 992
duke@1 993 // Shift operators
jjg@1127 994 case SL: // <<
duke@1 995 return Tree.Kind.LEFT_SHIFT;
jjg@1127 996 case SR: // >>
duke@1 997 return Tree.Kind.RIGHT_SHIFT;
jjg@1127 998 case USR: // >>>
duke@1 999 return Tree.Kind.UNSIGNED_RIGHT_SHIFT;
duke@1 1000
duke@1 1001 // Relational operators
jjg@1127 1002 case LT: // <
duke@1 1003 return Tree.Kind.LESS_THAN;
jjg@1127 1004 case GT: // >
duke@1 1005 return Tree.Kind.GREATER_THAN;
jjg@1127 1006 case LE: // <=
duke@1 1007 return Tree.Kind.LESS_THAN_EQUAL;
jjg@1127 1008 case GE: // >=
duke@1 1009 return Tree.Kind.GREATER_THAN_EQUAL;
duke@1 1010
duke@1 1011 // Equality operators
jjg@1127 1012 case EQ: // ==
duke@1 1013 return Tree.Kind.EQUAL_TO;
jjg@1127 1014 case NE: // !=
duke@1 1015 return Tree.Kind.NOT_EQUAL_TO;
duke@1 1016
duke@1 1017 // Bitwise and logical operators
jjg@1127 1018 case BITAND: // &
duke@1 1019 return Tree.Kind.AND;
jjg@1127 1020 case BITXOR: // ^
duke@1 1021 return Tree.Kind.XOR;
jjg@1127 1022 case BITOR: // |
duke@1 1023 return Tree.Kind.OR;
duke@1 1024
duke@1 1025 // Conditional operators
jjg@1127 1026 case AND: // &&
duke@1 1027 return Tree.Kind.CONDITIONAL_AND;
jjg@1127 1028 case OR: // ||
duke@1 1029 return Tree.Kind.CONDITIONAL_OR;
duke@1 1030
duke@1 1031 // Assignment operators
jjg@1127 1032 case MUL_ASG: // *=
duke@1 1033 return Tree.Kind.MULTIPLY_ASSIGNMENT;
jjg@1127 1034 case DIV_ASG: // /=
duke@1 1035 return Tree.Kind.DIVIDE_ASSIGNMENT;
jjg@1127 1036 case MOD_ASG: // %=
duke@1 1037 return Tree.Kind.REMAINDER_ASSIGNMENT;
jjg@1127 1038 case PLUS_ASG: // +=
duke@1 1039 return Tree.Kind.PLUS_ASSIGNMENT;
jjg@1127 1040 case MINUS_ASG: // -=
duke@1 1041 return Tree.Kind.MINUS_ASSIGNMENT;
jjg@1127 1042 case SL_ASG: // <<=
duke@1 1043 return Tree.Kind.LEFT_SHIFT_ASSIGNMENT;
jjg@1127 1044 case SR_ASG: // >>=
duke@1 1045 return Tree.Kind.RIGHT_SHIFT_ASSIGNMENT;
jjg@1127 1046 case USR_ASG: // >>>=
duke@1 1047 return Tree.Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT;
jjg@1127 1048 case BITAND_ASG: // &=
duke@1 1049 return Tree.Kind.AND_ASSIGNMENT;
jjg@1127 1050 case BITXOR_ASG: // ^=
duke@1 1051 return Tree.Kind.XOR_ASSIGNMENT;
jjg@1127 1052 case BITOR_ASG: // |=
duke@1 1053 return Tree.Kind.OR_ASSIGNMENT;
duke@1 1054
duke@1 1055 // Null check (implementation detail), for example, __.getClass()
jjg@1127 1056 case NULLCHK:
duke@1 1057 return Tree.Kind.OTHER;
duke@1 1058
jjg@1521 1059 case ANNOTATION:
jjg@1521 1060 return Tree.Kind.ANNOTATION;
jjg@1521 1061 case TYPE_ANNOTATION:
jjg@1521 1062 return Tree.Kind.TYPE_ANNOTATION;
jjg@1521 1063
duke@1 1064 default:
duke@1 1065 return null;
duke@1 1066 }
duke@1 1067 }
jjg@308 1068
jjg@308 1069 /**
jjg@1521 1070 * Returns the underlying type of the tree if it is an annotated type,
jjg@1521 1071 * or the tree itself otherwise.
jjg@308 1072 */
jjg@308 1073 public static JCExpression typeIn(JCExpression tree) {
jjg@308 1074 switch (tree.getTag()) {
jjg@1521 1075 case ANNOTATED_TYPE:
jjg@1521 1076 return ((JCAnnotatedType)tree).underlyingType;
jjg@1127 1077 case IDENT: /* simple names */
jjg@1127 1078 case TYPEIDENT: /* primitive name */
jjg@1127 1079 case SELECT: /* qualified name */
jjg@1127 1080 case TYPEARRAY: /* array types */
jjg@1127 1081 case WILDCARD: /* wild cards */
jjg@1127 1082 case TYPEPARAMETER: /* type parameters */
jjg@1127 1083 case TYPEAPPLY: /* parameterized types */
jjg@1521 1084 case ERRONEOUS: /* error tree TODO: needed for BadCast JSR308 test case. Better way? */
jjg@308 1085 return tree;
jjg@308 1086 default:
jjg@308 1087 throw new AssertionError("Unexpected type tree: " + tree);
jjg@308 1088 }
jjg@308 1089 }
jjg@470 1090
jjg@1521 1091 /* Return the inner-most type of a type tree.
jjg@1521 1092 * For an array that contains an annotated type, return that annotated type.
jjg@1521 1093 * TODO: currently only used by Pretty. Describe behavior better.
jjg@1521 1094 */
jjg@470 1095 public static JCTree innermostType(JCTree type) {
jjg@1521 1096 JCTree lastAnnotatedType = null;
jjg@1521 1097 JCTree cur = type;
jjg@1521 1098 loop: while (true) {
jjg@1521 1099 switch (cur.getTag()) {
jjg@1521 1100 case TYPEARRAY:
jjg@1521 1101 lastAnnotatedType = null;
jjg@1521 1102 cur = ((JCArrayTypeTree)cur).elemtype;
jjg@1521 1103 break;
jjg@1521 1104 case WILDCARD:
jjg@1521 1105 lastAnnotatedType = null;
jjg@1521 1106 cur = ((JCWildcard)cur).inner;
jjg@1521 1107 break;
jjg@1521 1108 case ANNOTATED_TYPE:
jjg@1521 1109 lastAnnotatedType = cur;
jjg@1521 1110 cur = ((JCAnnotatedType)cur).underlyingType;
jjg@1521 1111 break;
jjg@1521 1112 default:
jjg@1521 1113 break loop;
jjg@1521 1114 }
jjg@1521 1115 }
jjg@1521 1116 if (lastAnnotatedType!=null) {
jjg@1521 1117 return lastAnnotatedType;
jjg@1521 1118 } else {
jjg@1521 1119 return cur;
jjg@470 1120 }
jjg@470 1121 }
jjg@1521 1122
jjg@1521 1123 private static class TypeAnnotationFinder extends TreeScanner {
jjg@1521 1124 public boolean foundTypeAnno = false;
jjg@1521 1125 public void visitAnnotation(JCAnnotation tree) {
jjg@1521 1126 foundTypeAnno = foundTypeAnno || tree.hasTag(TYPE_ANNOTATION);
jjg@1521 1127 }
jjg@1521 1128 }
jjg@1521 1129
jjg@1521 1130 public static boolean containsTypeAnnotation(JCTree e) {
jjg@1521 1131 TypeAnnotationFinder finder = new TypeAnnotationFinder();
jjg@1521 1132 finder.scan(e);
jjg@1521 1133 return finder.foundTypeAnno;
jjg@1521 1134 }
duke@1 1135 }

mercurial