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

Mon, 16 Oct 2017 16:07:48 +0800

author
aoqi
date
Mon, 16 Oct 2017 16:07:48 +0800
changeset 2893
ca5783d9a597
parent 2814
380f6c17ea01
parent 2525
2eb010b6cb22
child 3446
e468915bad3a
permissions
-rw-r--r--

merge

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

mercurial