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

Mon, 17 Dec 2012 07:47:05 -0800

author
jjg
date
Mon, 17 Dec 2012 07:47:05 -0800
changeset 1455
75ab654b5cd5
parent 1433
4f9853659bf1
child 1510
7873d37f5b37
permissions
-rw-r--r--

8004832: Add new doclint package
Reviewed-by: mcimadamore

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

mercurial