src/share/classes/com/sun/tools/javac/comp/Flow.java

Thu, 10 Jun 2010 17:09:56 -0700

author
jjg
date
Thu, 10 Jun 2010 17:09:56 -0700
changeset 582
366a7b9b5627
parent 581
f2fdd52e4e87
child 609
13354e1abba7
permissions
-rw-r--r--

6960407: Potential rebranding issues in openjdk/langtools repository sources
Reviewed-by: darcy

duke@1 1 /*
ohair@554 2 * Copyright (c) 1999, 2009, 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 //todo: one might eliminate uninits.andSets when monotonic
duke@1 27
duke@1 28 package com.sun.tools.javac.comp;
duke@1 29
mcimadamore@550 30 import java.util.HashMap;
mcimadamore@550 31
duke@1 32 import com.sun.tools.javac.code.*;
duke@1 33 import com.sun.tools.javac.tree.*;
duke@1 34 import com.sun.tools.javac.util.*;
duke@1 35 import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
duke@1 36
duke@1 37 import com.sun.tools.javac.code.Symbol.*;
duke@1 38 import com.sun.tools.javac.tree.JCTree.*;
duke@1 39
duke@1 40 import static com.sun.tools.javac.code.Flags.*;
duke@1 41 import static com.sun.tools.javac.code.Kinds.*;
duke@1 42 import static com.sun.tools.javac.code.TypeTags.*;
duke@1 43
duke@1 44 /** This pass implements dataflow analysis for Java programs.
duke@1 45 * Liveness analysis checks that every statement is reachable.
duke@1 46 * Exception analysis ensures that every checked exception that is
duke@1 47 * thrown is declared or caught. Definite assignment analysis
duke@1 48 * ensures that each variable is assigned when used. Definite
duke@1 49 * unassignment analysis ensures that no final variable is assigned
duke@1 50 * more than once.
duke@1 51 *
duke@1 52 * <p>The second edition of the JLS has a number of problems in the
duke@1 53 * specification of these flow analysis problems. This implementation
duke@1 54 * attempts to address those issues.
duke@1 55 *
duke@1 56 * <p>First, there is no accommodation for a finally clause that cannot
duke@1 57 * complete normally. For liveness analysis, an intervening finally
duke@1 58 * clause can cause a break, continue, or return not to reach its
duke@1 59 * target. For exception analysis, an intervening finally clause can
duke@1 60 * cause any exception to be "caught". For DA/DU analysis, the finally
duke@1 61 * clause can prevent a transfer of control from propagating DA/DU
duke@1 62 * state to the target. In addition, code in the finally clause can
duke@1 63 * affect the DA/DU status of variables.
duke@1 64 *
duke@1 65 * <p>For try statements, we introduce the idea of a variable being
duke@1 66 * definitely unassigned "everywhere" in a block. A variable V is
duke@1 67 * "unassigned everywhere" in a block iff it is unassigned at the
duke@1 68 * beginning of the block and there is no reachable assignment to V
duke@1 69 * in the block. An assignment V=e is reachable iff V is not DA
duke@1 70 * after e. Then we can say that V is DU at the beginning of the
duke@1 71 * catch block iff V is DU everywhere in the try block. Similarly, V
duke@1 72 * is DU at the beginning of the finally block iff V is DU everywhere
duke@1 73 * in the try block and in every catch block. Specifically, the
duke@1 74 * following bullet is added to 16.2.2
duke@1 75 * <pre>
duke@1 76 * V is <em>unassigned everywhere</em> in a block if it is
duke@1 77 * unassigned before the block and there is no reachable
duke@1 78 * assignment to V within the block.
duke@1 79 * </pre>
duke@1 80 * <p>In 16.2.15, the third bullet (and all of its sub-bullets) for all
duke@1 81 * try blocks is changed to
duke@1 82 * <pre>
duke@1 83 * V is definitely unassigned before a catch block iff V is
duke@1 84 * definitely unassigned everywhere in the try block.
duke@1 85 * </pre>
duke@1 86 * <p>The last bullet (and all of its sub-bullets) for try blocks that
duke@1 87 * have a finally block is changed to
duke@1 88 * <pre>
duke@1 89 * V is definitely unassigned before the finally block iff
duke@1 90 * V is definitely unassigned everywhere in the try block
duke@1 91 * and everywhere in each catch block of the try statement.
duke@1 92 * </pre>
duke@1 93 * <p>In addition,
duke@1 94 * <pre>
duke@1 95 * V is definitely assigned at the end of a constructor iff
duke@1 96 * V is definitely assigned after the block that is the body
duke@1 97 * of the constructor and V is definitely assigned at every
duke@1 98 * return that can return from the constructor.
duke@1 99 * </pre>
duke@1 100 * <p>In addition, each continue statement with the loop as its target
duke@1 101 * is treated as a jump to the end of the loop body, and "intervening"
duke@1 102 * finally clauses are treated as follows: V is DA "due to the
duke@1 103 * continue" iff V is DA before the continue statement or V is DA at
duke@1 104 * the end of any intervening finally block. V is DU "due to the
duke@1 105 * continue" iff any intervening finally cannot complete normally or V
duke@1 106 * is DU at the end of every intervening finally block. This "due to
duke@1 107 * the continue" concept is then used in the spec for the loops.
duke@1 108 *
duke@1 109 * <p>Similarly, break statements must consider intervening finally
duke@1 110 * blocks. For liveness analysis, a break statement for which any
duke@1 111 * intervening finally cannot complete normally is not considered to
duke@1 112 * cause the target statement to be able to complete normally. Then
duke@1 113 * we say V is DA "due to the break" iff V is DA before the break or
duke@1 114 * V is DA at the end of any intervening finally block. V is DU "due
duke@1 115 * to the break" iff any intervening finally cannot complete normally
duke@1 116 * or V is DU at the break and at the end of every intervening
duke@1 117 * finally block. (I suspect this latter condition can be
duke@1 118 * simplified.) This "due to the break" is then used in the spec for
duke@1 119 * all statements that can be "broken".
duke@1 120 *
duke@1 121 * <p>The return statement is treated similarly. V is DA "due to a
duke@1 122 * return statement" iff V is DA before the return statement or V is
duke@1 123 * DA at the end of any intervening finally block. Note that we
duke@1 124 * don't have to worry about the return expression because this
duke@1 125 * concept is only used for construcrors.
duke@1 126 *
duke@1 127 * <p>There is no spec in JLS2 for when a variable is definitely
duke@1 128 * assigned at the end of a constructor, which is needed for final
duke@1 129 * fields (8.3.1.2). We implement the rule that V is DA at the end
duke@1 130 * of the constructor iff it is DA and the end of the body of the
duke@1 131 * constructor and V is DA "due to" every return of the constructor.
duke@1 132 *
duke@1 133 * <p>Intervening finally blocks similarly affect exception analysis. An
duke@1 134 * intervening finally that cannot complete normally allows us to ignore
duke@1 135 * an otherwise uncaught exception.
duke@1 136 *
duke@1 137 * <p>To implement the semantics of intervening finally clauses, all
duke@1 138 * nonlocal transfers (break, continue, return, throw, method call that
duke@1 139 * can throw a checked exception, and a constructor invocation that can
duke@1 140 * thrown a checked exception) are recorded in a queue, and removed
duke@1 141 * from the queue when we complete processing the target of the
duke@1 142 * nonlocal transfer. This allows us to modify the queue in accordance
duke@1 143 * with the above rules when we encounter a finally clause. The only
duke@1 144 * exception to this [no pun intended] is that checked exceptions that
duke@1 145 * are known to be caught or declared to be caught in the enclosing
duke@1 146 * method are not recorded in the queue, but instead are recorded in a
duke@1 147 * global variable "Set<Type> thrown" that records the type of all
duke@1 148 * exceptions that can be thrown.
duke@1 149 *
duke@1 150 * <p>Other minor issues the treatment of members of other classes
duke@1 151 * (always considered DA except that within an anonymous class
duke@1 152 * constructor, where DA status from the enclosing scope is
duke@1 153 * preserved), treatment of the case expression (V is DA before the
duke@1 154 * case expression iff V is DA after the switch expression),
duke@1 155 * treatment of variables declared in a switch block (the implied
duke@1 156 * DA/DU status after the switch expression is DU and not DA for
duke@1 157 * variables defined in a switch block), the treatment of boolean ?:
duke@1 158 * expressions (The JLS rules only handle b and c non-boolean; the
duke@1 159 * new rule is that if b and c are boolean valued, then V is
duke@1 160 * (un)assigned after a?b:c when true/false iff V is (un)assigned
duke@1 161 * after b when true/false and V is (un)assigned after c when
duke@1 162 * true/false).
duke@1 163 *
duke@1 164 * <p>There is the remaining question of what syntactic forms constitute a
duke@1 165 * reference to a variable. It is conventional to allow this.x on the
duke@1 166 * left-hand-side to initialize a final instance field named x, yet
duke@1 167 * this.x isn't considered a "use" when appearing on a right-hand-side
duke@1 168 * in most implementations. Should parentheses affect what is
duke@1 169 * considered a variable reference? The simplest rule would be to
duke@1 170 * allow unqualified forms only, parentheses optional, and phase out
duke@1 171 * support for assigning to a final field via this.x.
duke@1 172 *
jjg@581 173 * <p><b>This is NOT part of any supported API.
jjg@581 174 * If you write code that depends on this, you do so at your own risk.
duke@1 175 * This code and its internal interfaces are subject to change or
duke@1 176 * deletion without notice.</b>
duke@1 177 */
duke@1 178 public class Flow extends TreeScanner {
duke@1 179 protected static final Context.Key<Flow> flowKey =
duke@1 180 new Context.Key<Flow>();
duke@1 181
jjg@113 182 private final Names names;
duke@1 183 private final Log log;
duke@1 184 private final Symtab syms;
duke@1 185 private final Types types;
duke@1 186 private final Check chk;
duke@1 187 private TreeMaker make;
duke@1 188 private Lint lint;
mcimadamore@550 189 private final boolean allowRethrowAnalysis;
duke@1 190
duke@1 191 public static Flow instance(Context context) {
duke@1 192 Flow instance = context.get(flowKey);
duke@1 193 if (instance == null)
duke@1 194 instance = new Flow(context);
duke@1 195 return instance;
duke@1 196 }
duke@1 197
duke@1 198 protected Flow(Context context) {
duke@1 199 context.put(flowKey, this);
jjg@113 200 names = Names.instance(context);
duke@1 201 log = Log.instance(context);
duke@1 202 syms = Symtab.instance(context);
duke@1 203 types = Types.instance(context);
duke@1 204 chk = Check.instance(context);
duke@1 205 lint = Lint.instance(context);
mcimadamore@550 206 Source source = Source.instance(context);
mcimadamore@550 207 allowRethrowAnalysis = source.allowMulticatch();
duke@1 208 }
duke@1 209
duke@1 210 /** A flag that indicates whether the last statement could
duke@1 211 * complete normally.
duke@1 212 */
duke@1 213 private boolean alive;
duke@1 214
duke@1 215 /** The set of definitely assigned variables.
duke@1 216 */
duke@1 217 Bits inits;
duke@1 218
duke@1 219 /** The set of definitely unassigned variables.
duke@1 220 */
duke@1 221 Bits uninits;
duke@1 222
mcimadamore@550 223 HashMap<Symbol, List<Type>> multicatchTypes;
mcimadamore@550 224
duke@1 225 /** The set of variables that are definitely unassigned everywhere
duke@1 226 * in current try block. This variable is maintained lazily; it is
duke@1 227 * updated only when something gets removed from uninits,
duke@1 228 * typically by being assigned in reachable code. To obtain the
duke@1 229 * correct set of variables which are definitely unassigned
duke@1 230 * anywhere in current try block, intersect uninitsTry and
duke@1 231 * uninits.
duke@1 232 */
duke@1 233 Bits uninitsTry;
duke@1 234
duke@1 235 /** When analyzing a condition, inits and uninits are null.
duke@1 236 * Instead we have:
duke@1 237 */
duke@1 238 Bits initsWhenTrue;
duke@1 239 Bits initsWhenFalse;
duke@1 240 Bits uninitsWhenTrue;
duke@1 241 Bits uninitsWhenFalse;
duke@1 242
duke@1 243 /** A mapping from addresses to variable symbols.
duke@1 244 */
duke@1 245 VarSymbol[] vars;
duke@1 246
duke@1 247 /** The current class being defined.
duke@1 248 */
duke@1 249 JCClassDecl classDef;
duke@1 250
duke@1 251 /** The first variable sequence number in this class definition.
duke@1 252 */
duke@1 253 int firstadr;
duke@1 254
duke@1 255 /** The next available variable sequence number.
duke@1 256 */
duke@1 257 int nextadr;
duke@1 258
duke@1 259 /** The list of possibly thrown declarable exceptions.
duke@1 260 */
duke@1 261 List<Type> thrown;
duke@1 262
duke@1 263 /** The list of exceptions that are either caught or declared to be
duke@1 264 * thrown.
duke@1 265 */
duke@1 266 List<Type> caught;
duke@1 267
duke@1 268 /** Set when processing a loop body the second time for DU analysis. */
duke@1 269 boolean loopPassTwo = false;
duke@1 270
duke@1 271 /*-------------------- Environments ----------------------*/
duke@1 272
duke@1 273 /** A pending exit. These are the statements return, break, and
duke@1 274 * continue. In addition, exception-throwing expressions or
duke@1 275 * statements are put here when not known to be caught. This
duke@1 276 * will typically result in an error unless it is within a
duke@1 277 * try-finally whose finally block cannot complete normally.
duke@1 278 */
duke@1 279 static class PendingExit {
duke@1 280 JCTree tree;
duke@1 281 Bits inits;
duke@1 282 Bits uninits;
duke@1 283 Type thrown;
duke@1 284 PendingExit(JCTree tree, Bits inits, Bits uninits) {
duke@1 285 this.tree = tree;
duke@1 286 this.inits = inits.dup();
duke@1 287 this.uninits = uninits.dup();
duke@1 288 }
duke@1 289 PendingExit(JCTree tree, Type thrown) {
duke@1 290 this.tree = tree;
duke@1 291 this.thrown = thrown;
duke@1 292 }
duke@1 293 }
duke@1 294
duke@1 295 /** The currently pending exits that go from current inner blocks
duke@1 296 * to an enclosing block, in source order.
duke@1 297 */
duke@1 298 ListBuffer<PendingExit> pendingExits;
duke@1 299
duke@1 300 /*-------------------- Exceptions ----------------------*/
duke@1 301
duke@1 302 /** Complain that pending exceptions are not caught.
duke@1 303 */
duke@1 304 void errorUncaught() {
duke@1 305 for (PendingExit exit = pendingExits.next();
duke@1 306 exit != null;
duke@1 307 exit = pendingExits.next()) {
duke@1 308 boolean synthetic = classDef != null &&
duke@1 309 classDef.pos == exit.tree.pos;
duke@1 310 log.error(exit.tree.pos(),
duke@1 311 synthetic
duke@1 312 ? "unreported.exception.default.constructor"
duke@1 313 : "unreported.exception.need.to.catch.or.throw",
duke@1 314 exit.thrown);
duke@1 315 }
duke@1 316 }
duke@1 317
duke@1 318 /** Record that exception is potentially thrown and check that it
duke@1 319 * is caught.
duke@1 320 */
duke@1 321 void markThrown(JCTree tree, Type exc) {
duke@1 322 if (!chk.isUnchecked(tree.pos(), exc)) {
duke@1 323 if (!chk.isHandled(exc, caught))
duke@1 324 pendingExits.append(new PendingExit(tree, exc));
duke@1 325 thrown = chk.incl(exc, thrown);
duke@1 326 }
duke@1 327 }
duke@1 328
duke@1 329 /*-------------- Processing variables ----------------------*/
duke@1 330
duke@1 331 /** Do we need to track init/uninit state of this symbol?
duke@1 332 * I.e. is symbol either a local or a blank final variable?
duke@1 333 */
duke@1 334 boolean trackable(VarSymbol sym) {
duke@1 335 return
duke@1 336 (sym.owner.kind == MTH ||
duke@1 337 ((sym.flags() & (FINAL | HASINIT | PARAMETER)) == FINAL &&
duke@1 338 classDef.sym.isEnclosedBy((ClassSymbol)sym.owner)));
duke@1 339 }
duke@1 340
duke@1 341 /** Initialize new trackable variable by setting its address field
duke@1 342 * to the next available sequence number and entering it under that
duke@1 343 * index into the vars array.
duke@1 344 */
duke@1 345 void newVar(VarSymbol sym) {
duke@1 346 if (nextadr == vars.length) {
duke@1 347 VarSymbol[] newvars = new VarSymbol[nextadr * 2];
duke@1 348 System.arraycopy(vars, 0, newvars, 0, nextadr);
duke@1 349 vars = newvars;
duke@1 350 }
duke@1 351 sym.adr = nextadr;
duke@1 352 vars[nextadr] = sym;
duke@1 353 inits.excl(nextadr);
duke@1 354 uninits.incl(nextadr);
duke@1 355 nextadr++;
duke@1 356 }
duke@1 357
duke@1 358 /** Record an initialization of a trackable variable.
duke@1 359 */
duke@1 360 void letInit(DiagnosticPosition pos, VarSymbol sym) {
duke@1 361 if (sym.adr >= firstadr && trackable(sym)) {
duke@1 362 if ((sym.flags() & FINAL) != 0) {
duke@1 363 if ((sym.flags() & PARAMETER) != 0) {
mcimadamore@550 364 if ((sym.flags() & DISJOINT) != 0) { //multi-catch parameter
mcimadamore@550 365 log.error(pos, "multicatch.parameter.may.not.be.assigned",
mcimadamore@550 366 sym);
mcimadamore@550 367 }
mcimadamore@550 368 else {
mcimadamore@550 369 log.error(pos, "final.parameter.may.not.be.assigned",
duke@1 370 sym);
mcimadamore@550 371 }
duke@1 372 } else if (!uninits.isMember(sym.adr)) {
duke@1 373 log.error(pos,
duke@1 374 loopPassTwo
duke@1 375 ? "var.might.be.assigned.in.loop"
duke@1 376 : "var.might.already.be.assigned",
duke@1 377 sym);
duke@1 378 } else if (!inits.isMember(sym.adr)) {
duke@1 379 // reachable assignment
duke@1 380 uninits.excl(sym.adr);
duke@1 381 uninitsTry.excl(sym.adr);
duke@1 382 } else {
duke@1 383 //log.rawWarning(pos, "unreachable assignment");//DEBUG
duke@1 384 uninits.excl(sym.adr);
duke@1 385 }
duke@1 386 }
duke@1 387 inits.incl(sym.adr);
duke@1 388 } else if ((sym.flags() & FINAL) != 0) {
duke@1 389 log.error(pos, "var.might.already.be.assigned", sym);
duke@1 390 }
duke@1 391 }
duke@1 392
duke@1 393 /** If tree is either a simple name or of the form this.name or
duke@1 394 * C.this.name, and tree represents a trackable variable,
duke@1 395 * record an initialization of the variable.
duke@1 396 */
duke@1 397 void letInit(JCTree tree) {
duke@1 398 tree = TreeInfo.skipParens(tree);
duke@1 399 if (tree.getTag() == JCTree.IDENT || tree.getTag() == JCTree.SELECT) {
duke@1 400 Symbol sym = TreeInfo.symbol(tree);
duke@1 401 letInit(tree.pos(), (VarSymbol)sym);
duke@1 402 }
duke@1 403 }
duke@1 404
duke@1 405 /** Check that trackable variable is initialized.
duke@1 406 */
duke@1 407 void checkInit(DiagnosticPosition pos, VarSymbol sym) {
duke@1 408 if ((sym.adr >= firstadr || sym.owner.kind != TYP) &&
duke@1 409 trackable(sym) &&
duke@1 410 !inits.isMember(sym.adr)) {
duke@1 411 log.error(pos, "var.might.not.have.been.initialized",
duke@1 412 sym);
duke@1 413 inits.incl(sym.adr);
duke@1 414 }
duke@1 415 }
duke@1 416
duke@1 417 /*-------------------- Handling jumps ----------------------*/
duke@1 418
duke@1 419 /** Record an outward transfer of control. */
duke@1 420 void recordExit(JCTree tree) {
duke@1 421 pendingExits.append(new PendingExit(tree, inits, uninits));
duke@1 422 markDead();
duke@1 423 }
duke@1 424
duke@1 425 /** Resolve all breaks of this statement. */
duke@1 426 boolean resolveBreaks(JCTree tree,
duke@1 427 ListBuffer<PendingExit> oldPendingExits) {
duke@1 428 boolean result = false;
duke@1 429 List<PendingExit> exits = pendingExits.toList();
duke@1 430 pendingExits = oldPendingExits;
duke@1 431 for (; exits.nonEmpty(); exits = exits.tail) {
duke@1 432 PendingExit exit = exits.head;
duke@1 433 if (exit.tree.getTag() == JCTree.BREAK &&
duke@1 434 ((JCBreak) exit.tree).target == tree) {
duke@1 435 inits.andSet(exit.inits);
duke@1 436 uninits.andSet(exit.uninits);
duke@1 437 result = true;
duke@1 438 } else {
duke@1 439 pendingExits.append(exit);
duke@1 440 }
duke@1 441 }
duke@1 442 return result;
duke@1 443 }
duke@1 444
duke@1 445 /** Resolve all continues of this statement. */
duke@1 446 boolean resolveContinues(JCTree tree) {
duke@1 447 boolean result = false;
duke@1 448 List<PendingExit> exits = pendingExits.toList();
duke@1 449 pendingExits = new ListBuffer<PendingExit>();
duke@1 450 for (; exits.nonEmpty(); exits = exits.tail) {
duke@1 451 PendingExit exit = exits.head;
duke@1 452 if (exit.tree.getTag() == JCTree.CONTINUE &&
duke@1 453 ((JCContinue) exit.tree).target == tree) {
duke@1 454 inits.andSet(exit.inits);
duke@1 455 uninits.andSet(exit.uninits);
duke@1 456 result = true;
duke@1 457 } else {
duke@1 458 pendingExits.append(exit);
duke@1 459 }
duke@1 460 }
duke@1 461 return result;
duke@1 462 }
duke@1 463
duke@1 464 /** Record that statement is unreachable.
duke@1 465 */
duke@1 466 void markDead() {
duke@1 467 inits.inclRange(firstadr, nextadr);
duke@1 468 uninits.inclRange(firstadr, nextadr);
duke@1 469 alive = false;
duke@1 470 }
duke@1 471
duke@1 472 /** Split (duplicate) inits/uninits into WhenTrue/WhenFalse sets
duke@1 473 */
duke@1 474 void split() {
duke@1 475 initsWhenFalse = inits.dup();
duke@1 476 uninitsWhenFalse = uninits.dup();
duke@1 477 initsWhenTrue = inits;
duke@1 478 uninitsWhenTrue = uninits;
duke@1 479 inits = uninits = null;
duke@1 480 }
duke@1 481
duke@1 482 /** Merge (intersect) inits/uninits from WhenTrue/WhenFalse sets.
duke@1 483 */
duke@1 484 void merge() {
duke@1 485 inits = initsWhenFalse.andSet(initsWhenTrue);
duke@1 486 uninits = uninitsWhenFalse.andSet(uninitsWhenTrue);
duke@1 487 }
duke@1 488
duke@1 489 /* ************************************************************************
duke@1 490 * Visitor methods for statements and definitions
duke@1 491 *************************************************************************/
duke@1 492
duke@1 493 /** Analyze a definition.
duke@1 494 */
duke@1 495 void scanDef(JCTree tree) {
duke@1 496 scanStat(tree);
duke@1 497 if (tree != null && tree.getTag() == JCTree.BLOCK && !alive) {
duke@1 498 log.error(tree.pos(),
duke@1 499 "initializer.must.be.able.to.complete.normally");
duke@1 500 }
duke@1 501 }
duke@1 502
duke@1 503 /** Analyze a statement. Check that statement is reachable.
duke@1 504 */
duke@1 505 void scanStat(JCTree tree) {
duke@1 506 if (!alive && tree != null) {
duke@1 507 log.error(tree.pos(), "unreachable.stmt");
duke@1 508 if (tree.getTag() != JCTree.SKIP) alive = true;
duke@1 509 }
duke@1 510 scan(tree);
duke@1 511 }
duke@1 512
duke@1 513 /** Analyze list of statements.
duke@1 514 */
duke@1 515 void scanStats(List<? extends JCStatement> trees) {
duke@1 516 if (trees != null)
duke@1 517 for (List<? extends JCStatement> l = trees; l.nonEmpty(); l = l.tail)
duke@1 518 scanStat(l.head);
duke@1 519 }
duke@1 520
duke@1 521 /** Analyze an expression. Make sure to set (un)inits rather than
duke@1 522 * (un)initsWhenTrue(WhenFalse) on exit.
duke@1 523 */
duke@1 524 void scanExpr(JCTree tree) {
duke@1 525 if (tree != null) {
duke@1 526 scan(tree);
duke@1 527 if (inits == null) merge();
duke@1 528 }
duke@1 529 }
duke@1 530
duke@1 531 /** Analyze a list of expressions.
duke@1 532 */
duke@1 533 void scanExprs(List<? extends JCExpression> trees) {
duke@1 534 if (trees != null)
duke@1 535 for (List<? extends JCExpression> l = trees; l.nonEmpty(); l = l.tail)
duke@1 536 scanExpr(l.head);
duke@1 537 }
duke@1 538
duke@1 539 /** Analyze a condition. Make sure to set (un)initsWhenTrue(WhenFalse)
duke@1 540 * rather than (un)inits on exit.
duke@1 541 */
duke@1 542 void scanCond(JCTree tree) {
duke@1 543 if (tree.type.isFalse()) {
duke@1 544 if (inits == null) merge();
duke@1 545 initsWhenTrue = inits.dup();
duke@1 546 initsWhenTrue.inclRange(firstadr, nextadr);
duke@1 547 uninitsWhenTrue = uninits.dup();
duke@1 548 uninitsWhenTrue.inclRange(firstadr, nextadr);
duke@1 549 initsWhenFalse = inits;
duke@1 550 uninitsWhenFalse = uninits;
duke@1 551 } else if (tree.type.isTrue()) {
duke@1 552 if (inits == null) merge();
duke@1 553 initsWhenFalse = inits.dup();
duke@1 554 initsWhenFalse.inclRange(firstadr, nextadr);
duke@1 555 uninitsWhenFalse = uninits.dup();
duke@1 556 uninitsWhenFalse.inclRange(firstadr, nextadr);
duke@1 557 initsWhenTrue = inits;
duke@1 558 uninitsWhenTrue = uninits;
duke@1 559 } else {
duke@1 560 scan(tree);
duke@1 561 if (inits != null) split();
duke@1 562 }
duke@1 563 inits = uninits = null;
duke@1 564 }
duke@1 565
duke@1 566 /* ------------ Visitor methods for various sorts of trees -------------*/
duke@1 567
duke@1 568 public void visitClassDef(JCClassDecl tree) {
duke@1 569 if (tree.sym == null) return;
duke@1 570
duke@1 571 JCClassDecl classDefPrev = classDef;
duke@1 572 List<Type> thrownPrev = thrown;
duke@1 573 List<Type> caughtPrev = caught;
duke@1 574 boolean alivePrev = alive;
duke@1 575 int firstadrPrev = firstadr;
duke@1 576 int nextadrPrev = nextadr;
duke@1 577 ListBuffer<PendingExit> pendingExitsPrev = pendingExits;
duke@1 578 Lint lintPrev = lint;
duke@1 579
duke@1 580 pendingExits = new ListBuffer<PendingExit>();
duke@1 581 if (tree.name != names.empty) {
duke@1 582 caught = List.nil();
duke@1 583 firstadr = nextadr;
duke@1 584 }
duke@1 585 classDef = tree;
duke@1 586 thrown = List.nil();
duke@1 587 lint = lint.augment(tree.sym.attributes_field);
duke@1 588
duke@1 589 try {
duke@1 590 // define all the static fields
duke@1 591 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
duke@1 592 if (l.head.getTag() == JCTree.VARDEF) {
duke@1 593 JCVariableDecl def = (JCVariableDecl)l.head;
duke@1 594 if ((def.mods.flags & STATIC) != 0) {
duke@1 595 VarSymbol sym = def.sym;
duke@1 596 if (trackable(sym))
duke@1 597 newVar(sym);
duke@1 598 }
duke@1 599 }
duke@1 600 }
duke@1 601
duke@1 602 // process all the static initializers
duke@1 603 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
duke@1 604 if (l.head.getTag() != JCTree.METHODDEF &&
duke@1 605 (TreeInfo.flags(l.head) & STATIC) != 0) {
duke@1 606 scanDef(l.head);
duke@1 607 errorUncaught();
duke@1 608 }
duke@1 609 }
duke@1 610
duke@1 611 // add intersection of all thrown clauses of initial constructors
duke@1 612 // to set of caught exceptions, unless class is anonymous.
duke@1 613 if (tree.name != names.empty) {
duke@1 614 boolean firstConstructor = true;
duke@1 615 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
duke@1 616 if (TreeInfo.isInitialConstructor(l.head)) {
duke@1 617 List<Type> mthrown =
duke@1 618 ((JCMethodDecl) l.head).sym.type.getThrownTypes();
duke@1 619 if (firstConstructor) {
duke@1 620 caught = mthrown;
duke@1 621 firstConstructor = false;
duke@1 622 } else {
duke@1 623 caught = chk.intersect(mthrown, caught);
duke@1 624 }
duke@1 625 }
duke@1 626 }
duke@1 627 }
duke@1 628
duke@1 629 // define all the instance fields
duke@1 630 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
duke@1 631 if (l.head.getTag() == JCTree.VARDEF) {
duke@1 632 JCVariableDecl def = (JCVariableDecl)l.head;
duke@1 633 if ((def.mods.flags & STATIC) == 0) {
duke@1 634 VarSymbol sym = def.sym;
duke@1 635 if (trackable(sym))
duke@1 636 newVar(sym);
duke@1 637 }
duke@1 638 }
duke@1 639 }
duke@1 640
duke@1 641 // process all the instance initializers
duke@1 642 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
duke@1 643 if (l.head.getTag() != JCTree.METHODDEF &&
duke@1 644 (TreeInfo.flags(l.head) & STATIC) == 0) {
duke@1 645 scanDef(l.head);
duke@1 646 errorUncaught();
duke@1 647 }
duke@1 648 }
duke@1 649
duke@1 650 // in an anonymous class, add the set of thrown exceptions to
duke@1 651 // the throws clause of the synthetic constructor and propagate
duke@1 652 // outwards.
duke@1 653 if (tree.name == names.empty) {
duke@1 654 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
duke@1 655 if (TreeInfo.isInitialConstructor(l.head)) {
duke@1 656 JCMethodDecl mdef = (JCMethodDecl)l.head;
duke@1 657 mdef.thrown = make.Types(thrown);
duke@1 658 mdef.sym.type.setThrown(thrown);
duke@1 659 }
duke@1 660 }
duke@1 661 thrownPrev = chk.union(thrown, thrownPrev);
duke@1 662 }
duke@1 663
duke@1 664 // process all the methods
duke@1 665 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
duke@1 666 if (l.head.getTag() == JCTree.METHODDEF) {
duke@1 667 scan(l.head);
duke@1 668 errorUncaught();
duke@1 669 }
duke@1 670 }
duke@1 671
duke@1 672 thrown = thrownPrev;
duke@1 673 } finally {
duke@1 674 pendingExits = pendingExitsPrev;
duke@1 675 alive = alivePrev;
duke@1 676 nextadr = nextadrPrev;
duke@1 677 firstadr = firstadrPrev;
duke@1 678 caught = caughtPrev;
duke@1 679 classDef = classDefPrev;
duke@1 680 lint = lintPrev;
duke@1 681 }
duke@1 682 }
duke@1 683
duke@1 684 public void visitMethodDef(JCMethodDecl tree) {
duke@1 685 if (tree.body == null) return;
duke@1 686
duke@1 687 List<Type> caughtPrev = caught;
duke@1 688 List<Type> mthrown = tree.sym.type.getThrownTypes();
duke@1 689 Bits initsPrev = inits.dup();
duke@1 690 Bits uninitsPrev = uninits.dup();
duke@1 691 int nextadrPrev = nextadr;
duke@1 692 int firstadrPrev = firstadr;
duke@1 693 Lint lintPrev = lint;
duke@1 694
duke@1 695 lint = lint.augment(tree.sym.attributes_field);
duke@1 696
duke@1 697 assert pendingExits.isEmpty();
duke@1 698
duke@1 699 try {
duke@1 700 boolean isInitialConstructor =
duke@1 701 TreeInfo.isInitialConstructor(tree);
duke@1 702
duke@1 703 if (!isInitialConstructor)
duke@1 704 firstadr = nextadr;
duke@1 705 for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
duke@1 706 JCVariableDecl def = l.head;
duke@1 707 scan(def);
duke@1 708 inits.incl(def.sym.adr);
duke@1 709 uninits.excl(def.sym.adr);
duke@1 710 }
duke@1 711 if (isInitialConstructor)
duke@1 712 caught = chk.union(caught, mthrown);
duke@1 713 else if ((tree.sym.flags() & (BLOCK | STATIC)) != BLOCK)
duke@1 714 caught = mthrown;
duke@1 715 // else we are in an instance initializer block;
duke@1 716 // leave caught unchanged.
duke@1 717
duke@1 718 alive = true;
duke@1 719 scanStat(tree.body);
duke@1 720
duke@1 721 if (alive && tree.sym.type.getReturnType().tag != VOID)
duke@1 722 log.error(TreeInfo.diagEndPos(tree.body), "missing.ret.stmt");
duke@1 723
duke@1 724 if (isInitialConstructor) {
duke@1 725 for (int i = firstadr; i < nextadr; i++)
duke@1 726 if (vars[i].owner == classDef.sym)
duke@1 727 checkInit(TreeInfo.diagEndPos(tree.body), vars[i]);
duke@1 728 }
duke@1 729 List<PendingExit> exits = pendingExits.toList();
duke@1 730 pendingExits = new ListBuffer<PendingExit>();
duke@1 731 while (exits.nonEmpty()) {
duke@1 732 PendingExit exit = exits.head;
duke@1 733 exits = exits.tail;
duke@1 734 if (exit.thrown == null) {
duke@1 735 assert exit.tree.getTag() == JCTree.RETURN;
duke@1 736 if (isInitialConstructor) {
duke@1 737 inits = exit.inits;
duke@1 738 for (int i = firstadr; i < nextadr; i++)
duke@1 739 checkInit(exit.tree.pos(), vars[i]);
duke@1 740 }
duke@1 741 } else {
duke@1 742 // uncaught throws will be reported later
duke@1 743 pendingExits.append(exit);
duke@1 744 }
duke@1 745 }
duke@1 746 } finally {
duke@1 747 inits = initsPrev;
duke@1 748 uninits = uninitsPrev;
duke@1 749 nextadr = nextadrPrev;
duke@1 750 firstadr = firstadrPrev;
duke@1 751 caught = caughtPrev;
duke@1 752 lint = lintPrev;
duke@1 753 }
duke@1 754 }
duke@1 755
duke@1 756 public void visitVarDef(JCVariableDecl tree) {
duke@1 757 boolean track = trackable(tree.sym);
duke@1 758 if (track && tree.sym.owner.kind == MTH) newVar(tree.sym);
duke@1 759 if (tree.init != null) {
duke@1 760 Lint lintPrev = lint;
duke@1 761 lint = lint.augment(tree.sym.attributes_field);
duke@1 762 try{
duke@1 763 scanExpr(tree.init);
duke@1 764 if (track) letInit(tree.pos(), tree.sym);
duke@1 765 } finally {
duke@1 766 lint = lintPrev;
duke@1 767 }
duke@1 768 }
duke@1 769 }
duke@1 770
duke@1 771 public void visitBlock(JCBlock tree) {
duke@1 772 int nextadrPrev = nextadr;
duke@1 773 scanStats(tree.stats);
duke@1 774 nextadr = nextadrPrev;
duke@1 775 }
duke@1 776
duke@1 777 public void visitDoLoop(JCDoWhileLoop tree) {
duke@1 778 ListBuffer<PendingExit> prevPendingExits = pendingExits;
duke@1 779 boolean prevLoopPassTwo = loopPassTwo;
duke@1 780 pendingExits = new ListBuffer<PendingExit>();
duke@1 781 do {
duke@1 782 Bits uninitsEntry = uninits.dup();
duke@1 783 scanStat(tree.body);
duke@1 784 alive |= resolveContinues(tree);
duke@1 785 scanCond(tree.cond);
duke@1 786 if (log.nerrors != 0 ||
duke@1 787 loopPassTwo ||
duke@1 788 uninitsEntry.diffSet(uninitsWhenTrue).nextBit(firstadr)==-1)
duke@1 789 break;
duke@1 790 inits = initsWhenTrue;
duke@1 791 uninits = uninitsEntry.andSet(uninitsWhenTrue);
duke@1 792 loopPassTwo = true;
duke@1 793 alive = true;
duke@1 794 } while (true);
duke@1 795 loopPassTwo = prevLoopPassTwo;
duke@1 796 inits = initsWhenFalse;
duke@1 797 uninits = uninitsWhenFalse;
duke@1 798 alive = alive && !tree.cond.type.isTrue();
duke@1 799 alive |= resolveBreaks(tree, prevPendingExits);
duke@1 800 }
duke@1 801
duke@1 802 public void visitWhileLoop(JCWhileLoop tree) {
duke@1 803 ListBuffer<PendingExit> prevPendingExits = pendingExits;
duke@1 804 boolean prevLoopPassTwo = loopPassTwo;
duke@1 805 Bits initsCond;
duke@1 806 Bits uninitsCond;
duke@1 807 pendingExits = new ListBuffer<PendingExit>();
duke@1 808 do {
duke@1 809 Bits uninitsEntry = uninits.dup();
duke@1 810 scanCond(tree.cond);
duke@1 811 initsCond = initsWhenFalse;
duke@1 812 uninitsCond = uninitsWhenFalse;
duke@1 813 inits = initsWhenTrue;
duke@1 814 uninits = uninitsWhenTrue;
duke@1 815 alive = !tree.cond.type.isFalse();
duke@1 816 scanStat(tree.body);
duke@1 817 alive |= resolveContinues(tree);
duke@1 818 if (log.nerrors != 0 ||
duke@1 819 loopPassTwo ||
duke@1 820 uninitsEntry.diffSet(uninits).nextBit(firstadr) == -1)
duke@1 821 break;
duke@1 822 uninits = uninitsEntry.andSet(uninits);
duke@1 823 loopPassTwo = true;
duke@1 824 alive = true;
duke@1 825 } while (true);
duke@1 826 loopPassTwo = prevLoopPassTwo;
duke@1 827 inits = initsCond;
duke@1 828 uninits = uninitsCond;
duke@1 829 alive = resolveBreaks(tree, prevPendingExits) ||
duke@1 830 !tree.cond.type.isTrue();
duke@1 831 }
duke@1 832
duke@1 833 public void visitForLoop(JCForLoop tree) {
duke@1 834 ListBuffer<PendingExit> prevPendingExits = pendingExits;
duke@1 835 boolean prevLoopPassTwo = loopPassTwo;
duke@1 836 int nextadrPrev = nextadr;
duke@1 837 scanStats(tree.init);
duke@1 838 Bits initsCond;
duke@1 839 Bits uninitsCond;
duke@1 840 pendingExits = new ListBuffer<PendingExit>();
duke@1 841 do {
duke@1 842 Bits uninitsEntry = uninits.dup();
duke@1 843 if (tree.cond != null) {
duke@1 844 scanCond(tree.cond);
duke@1 845 initsCond = initsWhenFalse;
duke@1 846 uninitsCond = uninitsWhenFalse;
duke@1 847 inits = initsWhenTrue;
duke@1 848 uninits = uninitsWhenTrue;
duke@1 849 alive = !tree.cond.type.isFalse();
duke@1 850 } else {
duke@1 851 initsCond = inits.dup();
duke@1 852 initsCond.inclRange(firstadr, nextadr);
duke@1 853 uninitsCond = uninits.dup();
duke@1 854 uninitsCond.inclRange(firstadr, nextadr);
duke@1 855 alive = true;
duke@1 856 }
duke@1 857 scanStat(tree.body);
duke@1 858 alive |= resolveContinues(tree);
duke@1 859 scan(tree.step);
duke@1 860 if (log.nerrors != 0 ||
duke@1 861 loopPassTwo ||
duke@1 862 uninitsEntry.dup().diffSet(uninits).nextBit(firstadr) == -1)
duke@1 863 break;
duke@1 864 uninits = uninitsEntry.andSet(uninits);
duke@1 865 loopPassTwo = true;
duke@1 866 alive = true;
duke@1 867 } while (true);
duke@1 868 loopPassTwo = prevLoopPassTwo;
duke@1 869 inits = initsCond;
duke@1 870 uninits = uninitsCond;
duke@1 871 alive = resolveBreaks(tree, prevPendingExits) ||
duke@1 872 tree.cond != null && !tree.cond.type.isTrue();
duke@1 873 nextadr = nextadrPrev;
duke@1 874 }
duke@1 875
duke@1 876 public void visitForeachLoop(JCEnhancedForLoop tree) {
duke@1 877 visitVarDef(tree.var);
duke@1 878
duke@1 879 ListBuffer<PendingExit> prevPendingExits = pendingExits;
duke@1 880 boolean prevLoopPassTwo = loopPassTwo;
duke@1 881 int nextadrPrev = nextadr;
duke@1 882 scan(tree.expr);
duke@1 883 Bits initsStart = inits.dup();
duke@1 884 Bits uninitsStart = uninits.dup();
duke@1 885
duke@1 886 letInit(tree.pos(), tree.var.sym);
duke@1 887 pendingExits = new ListBuffer<PendingExit>();
duke@1 888 do {
duke@1 889 Bits uninitsEntry = uninits.dup();
duke@1 890 scanStat(tree.body);
duke@1 891 alive |= resolveContinues(tree);
duke@1 892 if (log.nerrors != 0 ||
duke@1 893 loopPassTwo ||
duke@1 894 uninitsEntry.diffSet(uninits).nextBit(firstadr) == -1)
duke@1 895 break;
duke@1 896 uninits = uninitsEntry.andSet(uninits);
duke@1 897 loopPassTwo = true;
duke@1 898 alive = true;
duke@1 899 } while (true);
duke@1 900 loopPassTwo = prevLoopPassTwo;
duke@1 901 inits = initsStart;
duke@1 902 uninits = uninitsStart.andSet(uninits);
duke@1 903 resolveBreaks(tree, prevPendingExits);
duke@1 904 alive = true;
duke@1 905 nextadr = nextadrPrev;
duke@1 906 }
duke@1 907
duke@1 908 public void visitLabelled(JCLabeledStatement tree) {
duke@1 909 ListBuffer<PendingExit> prevPendingExits = pendingExits;
duke@1 910 pendingExits = new ListBuffer<PendingExit>();
duke@1 911 scanStat(tree.body);
duke@1 912 alive |= resolveBreaks(tree, prevPendingExits);
duke@1 913 }
duke@1 914
duke@1 915 public void visitSwitch(JCSwitch tree) {
duke@1 916 ListBuffer<PendingExit> prevPendingExits = pendingExits;
duke@1 917 pendingExits = new ListBuffer<PendingExit>();
duke@1 918 int nextadrPrev = nextadr;
duke@1 919 scanExpr(tree.selector);
duke@1 920 Bits initsSwitch = inits;
duke@1 921 Bits uninitsSwitch = uninits.dup();
duke@1 922 boolean hasDefault = false;
duke@1 923 for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
duke@1 924 alive = true;
duke@1 925 inits = initsSwitch.dup();
duke@1 926 uninits = uninits.andSet(uninitsSwitch);
duke@1 927 JCCase c = l.head;
duke@1 928 if (c.pat == null)
duke@1 929 hasDefault = true;
duke@1 930 else
duke@1 931 scanExpr(c.pat);
duke@1 932 scanStats(c.stats);
duke@1 933 addVars(c.stats, initsSwitch, uninitsSwitch);
duke@1 934 // Warn about fall-through if lint switch fallthrough enabled.
duke@1 935 if (!loopPassTwo &&
duke@1 936 alive &&
duke@1 937 lint.isEnabled(Lint.LintCategory.FALLTHROUGH) &&
duke@1 938 c.stats.nonEmpty() && l.tail.nonEmpty())
duke@1 939 log.warning(l.tail.head.pos(),
duke@1 940 "possible.fall-through.into.case");
duke@1 941 }
duke@1 942 if (!hasDefault) {
duke@1 943 inits.andSet(initsSwitch);
duke@1 944 alive = true;
duke@1 945 }
duke@1 946 alive |= resolveBreaks(tree, prevPendingExits);
duke@1 947 nextadr = nextadrPrev;
duke@1 948 }
duke@1 949 // where
duke@1 950 /** Add any variables defined in stats to inits and uninits. */
duke@1 951 private static void addVars(List<JCStatement> stats, Bits inits,
duke@1 952 Bits uninits) {
duke@1 953 for (;stats.nonEmpty(); stats = stats.tail) {
duke@1 954 JCTree stat = stats.head;
duke@1 955 if (stat.getTag() == JCTree.VARDEF) {
duke@1 956 int adr = ((JCVariableDecl) stat).sym.adr;
duke@1 957 inits.excl(adr);
duke@1 958 uninits.incl(adr);
duke@1 959 }
duke@1 960 }
duke@1 961 }
duke@1 962
duke@1 963 public void visitTry(JCTry tree) {
duke@1 964 List<Type> caughtPrev = caught;
duke@1 965 List<Type> thrownPrev = thrown;
duke@1 966 thrown = List.nil();
mcimadamore@550 967 for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
mcimadamore@550 968 List<JCExpression> subClauses = TreeInfo.isMultiCatch(l.head) ?
mcimadamore@550 969 ((JCTypeDisjoint)l.head.param.vartype).components :
mcimadamore@550 970 List.of(l.head.param.vartype);
mcimadamore@550 971 for (JCExpression ct : subClauses) {
mcimadamore@550 972 caught = chk.incl(ct.type, caught);
mcimadamore@550 973 }
mcimadamore@550 974 }
duke@1 975 Bits uninitsTryPrev = uninitsTry;
duke@1 976 ListBuffer<PendingExit> prevPendingExits = pendingExits;
duke@1 977 pendingExits = new ListBuffer<PendingExit>();
duke@1 978 Bits initsTry = inits.dup();
duke@1 979 uninitsTry = uninits.dup();
duke@1 980 scanStat(tree.body);
duke@1 981 List<Type> thrownInTry = thrown;
duke@1 982 thrown = thrownPrev;
duke@1 983 caught = caughtPrev;
duke@1 984 boolean aliveEnd = alive;
duke@1 985 uninitsTry.andSet(uninits);
duke@1 986 Bits initsEnd = inits;
duke@1 987 Bits uninitsEnd = uninits;
duke@1 988 int nextadrCatch = nextadr;
duke@1 989
duke@1 990 List<Type> caughtInTry = List.nil();
duke@1 991 for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
duke@1 992 alive = true;
duke@1 993 JCVariableDecl param = l.head.param;
mcimadamore@550 994 List<JCExpression> subClauses = TreeInfo.isMultiCatch(l.head) ?
mcimadamore@550 995 ((JCTypeDisjoint)l.head.param.vartype).components :
mcimadamore@550 996 List.of(l.head.param.vartype);
mcimadamore@550 997 List<Type> ctypes = List.nil();
mcimadamore@550 998 List<Type> rethrownTypes = chk.diff(thrownInTry, caughtInTry);
mcimadamore@550 999 for (JCExpression ct : subClauses) {
mcimadamore@550 1000 Type exc = ct.type;
mcimadamore@550 1001 ctypes = ctypes.append(exc);
mcimadamore@550 1002 if (types.isSameType(exc, syms.objectType))
mcimadamore@550 1003 continue;
mcimadamore@550 1004 if (chk.subset(exc, caughtInTry)) {
mcimadamore@550 1005 log.error(l.head.pos(),
mcimadamore@550 1006 "except.already.caught", exc);
mcimadamore@550 1007 } else if (!chk.isUnchecked(l.head.pos(), exc) &&
mcimadamore@550 1008 exc.tsym != syms.throwableType.tsym &&
mcimadamore@550 1009 exc.tsym != syms.exceptionType.tsym &&
mcimadamore@550 1010 !chk.intersects(exc, thrownInTry)) {
mcimadamore@550 1011 log.error(l.head.pos(),
mcimadamore@550 1012 "except.never.thrown.in.try", exc);
mcimadamore@550 1013 }
mcimadamore@550 1014 caughtInTry = chk.incl(exc, caughtInTry);
duke@1 1015 }
duke@1 1016 inits = initsTry.dup();
duke@1 1017 uninits = uninitsTry.dup();
duke@1 1018 scan(param);
duke@1 1019 inits.incl(param.sym.adr);
duke@1 1020 uninits.excl(param.sym.adr);
mcimadamore@550 1021 multicatchTypes.put(param.sym, chk.intersect(ctypes, rethrownTypes));
duke@1 1022 scanStat(l.head.body);
duke@1 1023 initsEnd.andSet(inits);
duke@1 1024 uninitsEnd.andSet(uninits);
duke@1 1025 nextadr = nextadrCatch;
mcimadamore@550 1026 multicatchTypes.remove(param.sym);
duke@1 1027 aliveEnd |= alive;
duke@1 1028 }
duke@1 1029 if (tree.finalizer != null) {
duke@1 1030 List<Type> savedThrown = thrown;
duke@1 1031 thrown = List.nil();
duke@1 1032 inits = initsTry.dup();
duke@1 1033 uninits = uninitsTry.dup();
duke@1 1034 ListBuffer<PendingExit> exits = pendingExits;
duke@1 1035 pendingExits = prevPendingExits;
duke@1 1036 alive = true;
duke@1 1037 scanStat(tree.finalizer);
duke@1 1038 if (!alive) {
duke@1 1039 // discard exits and exceptions from try and finally
duke@1 1040 thrown = chk.union(thrown, thrownPrev);
duke@1 1041 if (!loopPassTwo &&
duke@1 1042 lint.isEnabled(Lint.LintCategory.FINALLY)) {
duke@1 1043 log.warning(TreeInfo.diagEndPos(tree.finalizer),
duke@1 1044 "finally.cannot.complete");
duke@1 1045 }
duke@1 1046 } else {
duke@1 1047 thrown = chk.union(thrown, chk.diff(thrownInTry, caughtInTry));
duke@1 1048 thrown = chk.union(thrown, savedThrown);
duke@1 1049 uninits.andSet(uninitsEnd);
duke@1 1050 // FIX: this doesn't preserve source order of exits in catch
duke@1 1051 // versus finally!
duke@1 1052 while (exits.nonEmpty()) {
duke@1 1053 PendingExit exit = exits.next();
duke@1 1054 if (exit.inits != null) {
duke@1 1055 exit.inits.orSet(inits);
duke@1 1056 exit.uninits.andSet(uninits);
duke@1 1057 }
duke@1 1058 pendingExits.append(exit);
duke@1 1059 }
duke@1 1060 inits.orSet(initsEnd);
duke@1 1061 alive = aliveEnd;
duke@1 1062 }
duke@1 1063 } else {
duke@1 1064 thrown = chk.union(thrown, chk.diff(thrownInTry, caughtInTry));
duke@1 1065 inits = initsEnd;
duke@1 1066 uninits = uninitsEnd;
duke@1 1067 alive = aliveEnd;
duke@1 1068 ListBuffer<PendingExit> exits = pendingExits;
duke@1 1069 pendingExits = prevPendingExits;
duke@1 1070 while (exits.nonEmpty()) pendingExits.append(exits.next());
duke@1 1071 }
duke@1 1072 uninitsTry.andSet(uninitsTryPrev).andSet(uninits);
duke@1 1073 }
duke@1 1074
duke@1 1075 public void visitConditional(JCConditional tree) {
duke@1 1076 scanCond(tree.cond);
duke@1 1077 Bits initsBeforeElse = initsWhenFalse;
duke@1 1078 Bits uninitsBeforeElse = uninitsWhenFalse;
duke@1 1079 inits = initsWhenTrue;
duke@1 1080 uninits = uninitsWhenTrue;
duke@1 1081 if (tree.truepart.type.tag == BOOLEAN &&
duke@1 1082 tree.falsepart.type.tag == BOOLEAN) {
duke@1 1083 // if b and c are boolean valued, then
duke@1 1084 // v is (un)assigned after a?b:c when true iff
duke@1 1085 // v is (un)assigned after b when true and
duke@1 1086 // v is (un)assigned after c when true
duke@1 1087 scanCond(tree.truepart);
duke@1 1088 Bits initsAfterThenWhenTrue = initsWhenTrue.dup();
duke@1 1089 Bits initsAfterThenWhenFalse = initsWhenFalse.dup();
duke@1 1090 Bits uninitsAfterThenWhenTrue = uninitsWhenTrue.dup();
duke@1 1091 Bits uninitsAfterThenWhenFalse = uninitsWhenFalse.dup();
duke@1 1092 inits = initsBeforeElse;
duke@1 1093 uninits = uninitsBeforeElse;
duke@1 1094 scanCond(tree.falsepart);
duke@1 1095 initsWhenTrue.andSet(initsAfterThenWhenTrue);
duke@1 1096 initsWhenFalse.andSet(initsAfterThenWhenFalse);
duke@1 1097 uninitsWhenTrue.andSet(uninitsAfterThenWhenTrue);
duke@1 1098 uninitsWhenFalse.andSet(uninitsAfterThenWhenFalse);
duke@1 1099 } else {
duke@1 1100 scanExpr(tree.truepart);
duke@1 1101 Bits initsAfterThen = inits.dup();
duke@1 1102 Bits uninitsAfterThen = uninits.dup();
duke@1 1103 inits = initsBeforeElse;
duke@1 1104 uninits = uninitsBeforeElse;
duke@1 1105 scanExpr(tree.falsepart);
duke@1 1106 inits.andSet(initsAfterThen);
duke@1 1107 uninits.andSet(uninitsAfterThen);
duke@1 1108 }
duke@1 1109 }
duke@1 1110
duke@1 1111 public void visitIf(JCIf tree) {
duke@1 1112 scanCond(tree.cond);
duke@1 1113 Bits initsBeforeElse = initsWhenFalse;
duke@1 1114 Bits uninitsBeforeElse = uninitsWhenFalse;
duke@1 1115 inits = initsWhenTrue;
duke@1 1116 uninits = uninitsWhenTrue;
duke@1 1117 scanStat(tree.thenpart);
duke@1 1118 if (tree.elsepart != null) {
duke@1 1119 boolean aliveAfterThen = alive;
duke@1 1120 alive = true;
duke@1 1121 Bits initsAfterThen = inits.dup();
duke@1 1122 Bits uninitsAfterThen = uninits.dup();
duke@1 1123 inits = initsBeforeElse;
duke@1 1124 uninits = uninitsBeforeElse;
duke@1 1125 scanStat(tree.elsepart);
duke@1 1126 inits.andSet(initsAfterThen);
duke@1 1127 uninits.andSet(uninitsAfterThen);
duke@1 1128 alive = alive | aliveAfterThen;
duke@1 1129 } else {
duke@1 1130 inits.andSet(initsBeforeElse);
duke@1 1131 uninits.andSet(uninitsBeforeElse);
duke@1 1132 alive = true;
duke@1 1133 }
duke@1 1134 }
duke@1 1135
duke@1 1136
duke@1 1137
duke@1 1138 public void visitBreak(JCBreak tree) {
duke@1 1139 recordExit(tree);
duke@1 1140 }
duke@1 1141
duke@1 1142 public void visitContinue(JCContinue tree) {
duke@1 1143 recordExit(tree);
duke@1 1144 }
duke@1 1145
duke@1 1146 public void visitReturn(JCReturn tree) {
duke@1 1147 scanExpr(tree.expr);
duke@1 1148 // if not initial constructor, should markDead instead of recordExit
duke@1 1149 recordExit(tree);
duke@1 1150 }
duke@1 1151
duke@1 1152 public void visitThrow(JCThrow tree) {
duke@1 1153 scanExpr(tree.expr);
mcimadamore@550 1154 Symbol sym = TreeInfo.symbol(tree.expr);
mcimadamore@550 1155 if (sym != null &&
mcimadamore@550 1156 sym.kind == VAR &&
mcimadamore@550 1157 (sym.flags() & FINAL) != 0 &&
mcimadamore@550 1158 multicatchTypes.get(sym) != null &&
mcimadamore@550 1159 allowRethrowAnalysis) {
mcimadamore@550 1160 for (Type t : multicatchTypes.get(sym)) {
mcimadamore@550 1161 markThrown(tree, t);
mcimadamore@550 1162 }
mcimadamore@550 1163 }
mcimadamore@550 1164 else {
mcimadamore@550 1165 markThrown(tree, tree.expr.type);
mcimadamore@550 1166 }
duke@1 1167 markDead();
duke@1 1168 }
duke@1 1169
duke@1 1170 public void visitApply(JCMethodInvocation tree) {
duke@1 1171 scanExpr(tree.meth);
duke@1 1172 scanExprs(tree.args);
duke@1 1173 for (List<Type> l = tree.meth.type.getThrownTypes(); l.nonEmpty(); l = l.tail)
duke@1 1174 markThrown(tree, l.head);
duke@1 1175 }
duke@1 1176
duke@1 1177 public void visitNewClass(JCNewClass tree) {
duke@1 1178 scanExpr(tree.encl);
duke@1 1179 scanExprs(tree.args);
duke@1 1180 // scan(tree.def);
mcimadamore@186 1181 for (List<Type> l = tree.constructorType.getThrownTypes();
duke@1 1182 l.nonEmpty();
mcimadamore@186 1183 l = l.tail) {
duke@1 1184 markThrown(tree, l.head);
mcimadamore@186 1185 }
mcimadamore@186 1186 List<Type> caughtPrev = caught;
mcimadamore@186 1187 try {
mcimadamore@186 1188 // If the new class expression defines an anonymous class,
mcimadamore@186 1189 // analysis of the anonymous constructor may encounter thrown
mcimadamore@186 1190 // types which are unsubstituted type variables.
mcimadamore@186 1191 // However, since the constructor's actual thrown types have
mcimadamore@186 1192 // already been marked as thrown, it is safe to simply include
mcimadamore@186 1193 // each of the constructor's formal thrown types in the set of
mcimadamore@186 1194 // 'caught/declared to be thrown' types, for the duration of
mcimadamore@186 1195 // the class def analysis.
mcimadamore@186 1196 if (tree.def != null)
mcimadamore@186 1197 for (List<Type> l = tree.constructor.type.getThrownTypes();
mcimadamore@186 1198 l.nonEmpty();
mcimadamore@186 1199 l = l.tail) {
mcimadamore@186 1200 caught = chk.incl(l.head, caught);
mcimadamore@186 1201 }
mcimadamore@186 1202 scan(tree.def);
mcimadamore@186 1203 }
mcimadamore@186 1204 finally {
mcimadamore@186 1205 caught = caughtPrev;
mcimadamore@186 1206 }
duke@1 1207 }
duke@1 1208
duke@1 1209 public void visitNewArray(JCNewArray tree) {
duke@1 1210 scanExprs(tree.dims);
duke@1 1211 scanExprs(tree.elems);
duke@1 1212 }
duke@1 1213
duke@1 1214 public void visitAssert(JCAssert tree) {
duke@1 1215 Bits initsExit = inits.dup();
duke@1 1216 Bits uninitsExit = uninits.dup();
duke@1 1217 scanCond(tree.cond);
duke@1 1218 uninitsExit.andSet(uninitsWhenTrue);
duke@1 1219 if (tree.detail != null) {
duke@1 1220 inits = initsWhenFalse;
duke@1 1221 uninits = uninitsWhenFalse;
duke@1 1222 scanExpr(tree.detail);
duke@1 1223 }
duke@1 1224 inits = initsExit;
duke@1 1225 uninits = uninitsExit;
duke@1 1226 }
duke@1 1227
duke@1 1228 public void visitAssign(JCAssign tree) {
duke@1 1229 JCTree lhs = TreeInfo.skipParens(tree.lhs);
duke@1 1230 if (!(lhs instanceof JCIdent)) scanExpr(lhs);
duke@1 1231 scanExpr(tree.rhs);
duke@1 1232 letInit(lhs);
duke@1 1233 }
duke@1 1234
duke@1 1235 public void visitAssignop(JCAssignOp tree) {
duke@1 1236 scanExpr(tree.lhs);
duke@1 1237 scanExpr(tree.rhs);
duke@1 1238 letInit(tree.lhs);
duke@1 1239 }
duke@1 1240
duke@1 1241 public void visitUnary(JCUnary tree) {
duke@1 1242 switch (tree.getTag()) {
duke@1 1243 case JCTree.NOT:
duke@1 1244 scanCond(tree.arg);
duke@1 1245 Bits t = initsWhenFalse;
duke@1 1246 initsWhenFalse = initsWhenTrue;
duke@1 1247 initsWhenTrue = t;
duke@1 1248 t = uninitsWhenFalse;
duke@1 1249 uninitsWhenFalse = uninitsWhenTrue;
duke@1 1250 uninitsWhenTrue = t;
duke@1 1251 break;
duke@1 1252 case JCTree.PREINC: case JCTree.POSTINC:
duke@1 1253 case JCTree.PREDEC: case JCTree.POSTDEC:
duke@1 1254 scanExpr(tree.arg);
duke@1 1255 letInit(tree.arg);
duke@1 1256 break;
duke@1 1257 default:
duke@1 1258 scanExpr(tree.arg);
duke@1 1259 }
duke@1 1260 }
duke@1 1261
duke@1 1262 public void visitBinary(JCBinary tree) {
duke@1 1263 switch (tree.getTag()) {
duke@1 1264 case JCTree.AND:
duke@1 1265 scanCond(tree.lhs);
duke@1 1266 Bits initsWhenFalseLeft = initsWhenFalse;
duke@1 1267 Bits uninitsWhenFalseLeft = uninitsWhenFalse;
duke@1 1268 inits = initsWhenTrue;
duke@1 1269 uninits = uninitsWhenTrue;
duke@1 1270 scanCond(tree.rhs);
duke@1 1271 initsWhenFalse.andSet(initsWhenFalseLeft);
duke@1 1272 uninitsWhenFalse.andSet(uninitsWhenFalseLeft);
duke@1 1273 break;
duke@1 1274 case JCTree.OR:
duke@1 1275 scanCond(tree.lhs);
duke@1 1276 Bits initsWhenTrueLeft = initsWhenTrue;
duke@1 1277 Bits uninitsWhenTrueLeft = uninitsWhenTrue;
duke@1 1278 inits = initsWhenFalse;
duke@1 1279 uninits = uninitsWhenFalse;
duke@1 1280 scanCond(tree.rhs);
duke@1 1281 initsWhenTrue.andSet(initsWhenTrueLeft);
duke@1 1282 uninitsWhenTrue.andSet(uninitsWhenTrueLeft);
duke@1 1283 break;
duke@1 1284 default:
duke@1 1285 scanExpr(tree.lhs);
duke@1 1286 scanExpr(tree.rhs);
duke@1 1287 }
duke@1 1288 }
duke@1 1289
jjg@308 1290 public void visitAnnotatedType(JCAnnotatedType tree) {
jjg@308 1291 // annotations don't get scanned
jjg@308 1292 tree.underlyingType.accept(this);
jjg@308 1293 }
jjg@308 1294
duke@1 1295 public void visitIdent(JCIdent tree) {
duke@1 1296 if (tree.sym.kind == VAR)
duke@1 1297 checkInit(tree.pos(), (VarSymbol)tree.sym);
duke@1 1298 }
duke@1 1299
duke@1 1300 public void visitTypeCast(JCTypeCast tree) {
duke@1 1301 super.visitTypeCast(tree);
duke@1 1302 if (!tree.type.isErroneous()
duke@1 1303 && lint.isEnabled(Lint.LintCategory.CAST)
jjg@308 1304 && types.isSameType(tree.expr.type, tree.clazz.type)
jjg@308 1305 && !(ignoreAnnotatedCasts && containsTypeAnnotation(tree.clazz))) {
duke@1 1306 log.warning(tree.pos(), "redundant.cast", tree.expr.type);
duke@1 1307 }
duke@1 1308 }
duke@1 1309
duke@1 1310 public void visitTopLevel(JCCompilationUnit tree) {
duke@1 1311 // Do nothing for TopLevel since each class is visited individually
duke@1 1312 }
duke@1 1313
duke@1 1314 /**************************************************************************
jjg@308 1315 * utility methods for ignoring type-annotated casts lint checking
jjg@308 1316 *************************************************************************/
jjg@308 1317 private static final boolean ignoreAnnotatedCasts = true;
jjg@308 1318 private static class AnnotationFinder extends TreeScanner {
jjg@308 1319 public boolean foundTypeAnno = false;
jjg@308 1320 public void visitAnnotation(JCAnnotation tree) {
jjg@308 1321 foundTypeAnno = foundTypeAnno || (tree instanceof JCTypeAnnotation);
jjg@308 1322 }
jjg@308 1323 }
jjg@308 1324
jjg@308 1325 private boolean containsTypeAnnotation(JCTree e) {
jjg@308 1326 AnnotationFinder finder = new AnnotationFinder();
jjg@308 1327 finder.scan(e);
jjg@308 1328 return finder.foundTypeAnno;
jjg@308 1329 }
jjg@308 1330
jjg@308 1331 /**************************************************************************
duke@1 1332 * main method
duke@1 1333 *************************************************************************/
duke@1 1334
duke@1 1335 /** Perform definite assignment/unassignment analysis on a tree.
duke@1 1336 */
duke@1 1337 public void analyzeTree(JCTree tree, TreeMaker make) {
duke@1 1338 try {
duke@1 1339 this.make = make;
duke@1 1340 inits = new Bits();
duke@1 1341 uninits = new Bits();
duke@1 1342 uninitsTry = new Bits();
duke@1 1343 initsWhenTrue = initsWhenFalse =
duke@1 1344 uninitsWhenTrue = uninitsWhenFalse = null;
duke@1 1345 if (vars == null)
duke@1 1346 vars = new VarSymbol[32];
duke@1 1347 else
duke@1 1348 for (int i=0; i<vars.length; i++)
duke@1 1349 vars[i] = null;
duke@1 1350 firstadr = 0;
duke@1 1351 nextadr = 0;
duke@1 1352 pendingExits = new ListBuffer<PendingExit>();
mcimadamore@550 1353 multicatchTypes = new HashMap<Symbol, List<Type>>();
duke@1 1354 alive = true;
duke@1 1355 this.thrown = this.caught = null;
duke@1 1356 this.classDef = null;
duke@1 1357 scan(tree);
duke@1 1358 } finally {
duke@1 1359 // note that recursive invocations of this method fail hard
duke@1 1360 inits = uninits = uninitsTry = null;
duke@1 1361 initsWhenTrue = initsWhenFalse =
duke@1 1362 uninitsWhenTrue = uninitsWhenFalse = null;
duke@1 1363 if (vars != null) for (int i=0; i<vars.length; i++)
duke@1 1364 vars[i] = null;
duke@1 1365 firstadr = 0;
duke@1 1366 nextadr = 0;
duke@1 1367 pendingExits = null;
duke@1 1368 this.make = null;
duke@1 1369 this.thrown = this.caught = null;
duke@1 1370 this.classDef = null;
duke@1 1371 }
duke@1 1372 }
duke@1 1373 }

mercurial