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

Wed, 07 Jun 2017 00:04:12 -0700

author
shshahma
date
Wed, 07 Jun 2017 00:04:12 -0700
changeset 3497
08a21473de54
parent 3371
7220be8747f0
child 3446
e468915bad3a
permissions
-rw-r--r--

8180660: missing LNT entry for finally block
Reviewed-by: mcimadamore, vromero
Contributed-by: maurizio.cimadamore@oracle.com, vicente.romero@oracle.com

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

mercurial