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

Mon, 14 Nov 2011 15:11:10 -0800

author
ksrini
date
Mon, 14 Nov 2011 15:11:10 -0800
changeset 1138
7375d4979bd3
parent 1127
ca49d50318dc
child 1143
ec59a2ce9114
permissions
-rw-r--r--

7106166: (javac) re-factor EndPos parser
Reviewed-by: jjg

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

mercurial