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

Wed, 08 Aug 2012 22:17:27 +0530

author
sundar
date
Wed, 08 Aug 2012 22:17:27 +0530
changeset 1300
f071cd32d297
parent 1281
067f51db3402
child 1347
1408af4cd8b0
permissions
-rw-r--r--

7178324: Crash when compiling for(i : x) try(AutoCloseable x = ...) {}
Reviewed-by: darcy, jjg

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

mercurial