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

Fri, 10 Jan 2014 12:47:15 +0100

author
alundblad
date
Fri, 10 Jan 2014 12:47:15 +0100
changeset 2814
380f6c17ea01
parent 2424
7e97c65c373c
child 2893
ca5783d9a597
child 3371
7220be8747f0
permissions
-rw-r--r--

8028389: NullPointerException compiling annotation values that have bodies
Summary: Made sure anonymous class declarations inside class- and package-level annotations are properly entered.
Reviewed-by: jfranck

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

mercurial