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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 2424
7e97c65c373c
parent 0
959103a6100f
child 2893
ca5783d9a597
permissions
-rw-r--r--

merge

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

mercurial