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

Mon, 26 Mar 2012 15:27:51 +0100

author
mcimadamore
date
Mon, 26 Mar 2012 15:27:51 +0100
changeset 1237
568e70bbd9aa
parent 1127
ca49d50318dc
child 1290
934a89402f85
permissions
-rw-r--r--

7151580: Separate DA/DU logic from exception checking logic in Flow.java
Summary: DA/DU analysis and exception checking analysis should live in two separate tree visitors
Reviewed-by: gafter, dlsmith, jjg

duke@1 1 /*
mcimadamore@1237 2 * Copyright (c) 1999, 2012, 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.*;
jjg@1127 41 import static com.sun.tools.javac.code.Flags.BLOCK;
duke@1 42 import static com.sun.tools.javac.code.Kinds.*;
duke@1 43 import static com.sun.tools.javac.code.TypeTags.*;
jjg@1127 44 import static com.sun.tools.javac.tree.JCTree.Tag.*;
duke@1 45
duke@1 46 /** This pass implements dataflow analysis for Java programs.
duke@1 47 * Liveness analysis checks that every statement is reachable.
duke@1 48 * Exception analysis ensures that every checked exception that is
duke@1 49 * thrown is declared or caught. Definite assignment analysis
duke@1 50 * ensures that each variable is assigned when used. Definite
duke@1 51 * unassignment analysis ensures that no final variable is assigned
duke@1 52 * more than once.
duke@1 53 *
jjh@972 54 * <p>The JLS has a number of problems in the
duke@1 55 * specification of these flow analysis problems. This implementation
duke@1 56 * attempts to address those issues.
duke@1 57 *
duke@1 58 * <p>First, there is no accommodation for a finally clause that cannot
duke@1 59 * complete normally. For liveness analysis, an intervening finally
duke@1 60 * clause can cause a break, continue, or return not to reach its
duke@1 61 * target. For exception analysis, an intervening finally clause can
duke@1 62 * cause any exception to be "caught". For DA/DU analysis, the finally
duke@1 63 * clause can prevent a transfer of control from propagating DA/DU
duke@1 64 * state to the target. In addition, code in the finally clause can
duke@1 65 * affect the DA/DU status of variables.
duke@1 66 *
duke@1 67 * <p>For try statements, we introduce the idea of a variable being
duke@1 68 * definitely unassigned "everywhere" in a block. A variable V is
duke@1 69 * "unassigned everywhere" in a block iff it is unassigned at the
duke@1 70 * beginning of the block and there is no reachable assignment to V
duke@1 71 * in the block. An assignment V=e is reachable iff V is not DA
duke@1 72 * after e. Then we can say that V is DU at the beginning of the
duke@1 73 * catch block iff V is DU everywhere in the try block. Similarly, V
duke@1 74 * is DU at the beginning of the finally block iff V is DU everywhere
duke@1 75 * in the try block and in every catch block. Specifically, the
duke@1 76 * following bullet is added to 16.2.2
duke@1 77 * <pre>
duke@1 78 * V is <em>unassigned everywhere</em> in a block if it is
duke@1 79 * unassigned before the block and there is no reachable
duke@1 80 * assignment to V within the block.
duke@1 81 * </pre>
duke@1 82 * <p>In 16.2.15, the third bullet (and all of its sub-bullets) for all
duke@1 83 * try blocks is changed to
duke@1 84 * <pre>
duke@1 85 * V is definitely unassigned before a catch block iff V is
duke@1 86 * definitely unassigned everywhere in the try block.
duke@1 87 * </pre>
duke@1 88 * <p>The last bullet (and all of its sub-bullets) for try blocks that
duke@1 89 * have a finally block is changed to
duke@1 90 * <pre>
duke@1 91 * V is definitely unassigned before the finally block iff
duke@1 92 * V is definitely unassigned everywhere in the try block
duke@1 93 * and everywhere in each catch block of the try statement.
duke@1 94 * </pre>
duke@1 95 * <p>In addition,
duke@1 96 * <pre>
duke@1 97 * V is definitely assigned at the end of a constructor iff
duke@1 98 * V is definitely assigned after the block that is the body
duke@1 99 * of the constructor and V is definitely assigned at every
duke@1 100 * return that can return from the constructor.
duke@1 101 * </pre>
duke@1 102 * <p>In addition, each continue statement with the loop as its target
duke@1 103 * is treated as a jump to the end of the loop body, and "intervening"
duke@1 104 * finally clauses are treated as follows: V is DA "due to the
duke@1 105 * continue" iff V is DA before the continue statement or V is DA at
duke@1 106 * the end of any intervening finally block. V is DU "due to the
duke@1 107 * continue" iff any intervening finally cannot complete normally or V
duke@1 108 * is DU at the end of every intervening finally block. This "due to
duke@1 109 * the continue" concept is then used in the spec for the loops.
duke@1 110 *
duke@1 111 * <p>Similarly, break statements must consider intervening finally
duke@1 112 * blocks. For liveness analysis, a break statement for which any
duke@1 113 * intervening finally cannot complete normally is not considered to
duke@1 114 * cause the target statement to be able to complete normally. Then
duke@1 115 * we say V is DA "due to the break" iff V is DA before the break or
duke@1 116 * V is DA at the end of any intervening finally block. V is DU "due
duke@1 117 * to the break" iff any intervening finally cannot complete normally
duke@1 118 * or V is DU at the break and at the end of every intervening
duke@1 119 * finally block. (I suspect this latter condition can be
duke@1 120 * simplified.) This "due to the break" is then used in the spec for
duke@1 121 * all statements that can be "broken".
duke@1 122 *
duke@1 123 * <p>The return statement is treated similarly. V is DA "due to a
duke@1 124 * return statement" iff V is DA before the return statement or V is
duke@1 125 * DA at the end of any intervening finally block. Note that we
duke@1 126 * don't have to worry about the return expression because this
duke@1 127 * concept is only used for construcrors.
duke@1 128 *
jjh@972 129 * <p>There is no spec in the JLS for when a variable is definitely
duke@1 130 * assigned at the end of a constructor, which is needed for final
duke@1 131 * fields (8.3.1.2). We implement the rule that V is DA at the end
duke@1 132 * of the constructor iff it is DA and the end of the body of the
duke@1 133 * constructor and V is DA "due to" every return of the constructor.
duke@1 134 *
duke@1 135 * <p>Intervening finally blocks similarly affect exception analysis. An
duke@1 136 * intervening finally that cannot complete normally allows us to ignore
duke@1 137 * an otherwise uncaught exception.
duke@1 138 *
duke@1 139 * <p>To implement the semantics of intervening finally clauses, all
duke@1 140 * nonlocal transfers (break, continue, return, throw, method call that
duke@1 141 * can throw a checked exception, and a constructor invocation that can
duke@1 142 * thrown a checked exception) are recorded in a queue, and removed
duke@1 143 * from the queue when we complete processing the target of the
duke@1 144 * nonlocal transfer. This allows us to modify the queue in accordance
duke@1 145 * with the above rules when we encounter a finally clause. The only
duke@1 146 * exception to this [no pun intended] is that checked exceptions that
duke@1 147 * are known to be caught or declared to be caught in the enclosing
duke@1 148 * method are not recorded in the queue, but instead are recorded in a
duke@1 149 * global variable "Set<Type> thrown" that records the type of all
duke@1 150 * exceptions that can be thrown.
duke@1 151 *
duke@1 152 * <p>Other minor issues the treatment of members of other classes
duke@1 153 * (always considered DA except that within an anonymous class
duke@1 154 * constructor, where DA status from the enclosing scope is
duke@1 155 * preserved), treatment of the case expression (V is DA before the
duke@1 156 * case expression iff V is DA after the switch expression),
duke@1 157 * treatment of variables declared in a switch block (the implied
duke@1 158 * DA/DU status after the switch expression is DU and not DA for
duke@1 159 * variables defined in a switch block), the treatment of boolean ?:
duke@1 160 * expressions (The JLS rules only handle b and c non-boolean; the
duke@1 161 * new rule is that if b and c are boolean valued, then V is
duke@1 162 * (un)assigned after a?b:c when true/false iff V is (un)assigned
duke@1 163 * after b when true/false and V is (un)assigned after c when
duke@1 164 * true/false).
duke@1 165 *
duke@1 166 * <p>There is the remaining question of what syntactic forms constitute a
duke@1 167 * reference to a variable. It is conventional to allow this.x on the
duke@1 168 * left-hand-side to initialize a final instance field named x, yet
duke@1 169 * this.x isn't considered a "use" when appearing on a right-hand-side
duke@1 170 * in most implementations. Should parentheses affect what is
duke@1 171 * considered a variable reference? The simplest rule would be to
duke@1 172 * allow unqualified forms only, parentheses optional, and phase out
duke@1 173 * support for assigning to a final field via this.x.
duke@1 174 *
jjg@581 175 * <p><b>This is NOT part of any supported API.
jjg@581 176 * If you write code that depends on this, you do so at your own risk.
duke@1 177 * This code and its internal interfaces are subject to change or
duke@1 178 * deletion without notice.</b>
duke@1 179 */
mcimadamore@1237 180 public class Flow {
duke@1 181 protected static final Context.Key<Flow> flowKey =
duke@1 182 new Context.Key<Flow>();
duke@1 183
jjg@113 184 private final Names names;
duke@1 185 private final Log log;
duke@1 186 private final Symtab syms;
duke@1 187 private final Types types;
duke@1 188 private final Check chk;
duke@1 189 private TreeMaker make;
mcimadamore@617 190 private final Resolve rs;
mcimadamore@617 191 private Env<AttrContext> attrEnv;
duke@1 192 private Lint lint;
mcimadamore@935 193 private final boolean allowImprovedRethrowAnalysis;
mcimadamore@935 194 private final boolean allowImprovedCatchAnalysis;
duke@1 195
duke@1 196 public static Flow instance(Context context) {
duke@1 197 Flow instance = context.get(flowKey);
duke@1 198 if (instance == null)
duke@1 199 instance = new Flow(context);
duke@1 200 return instance;
duke@1 201 }
duke@1 202
mcimadamore@1237 203 public void analyzeTree(Env<AttrContext> env, TreeMaker make) {
mcimadamore@1237 204 new FlowAnalyzer().analyzeTree(env, make);
mcimadamore@1237 205 new AssignAnalyzer().analyzeTree(env, make);
mcimadamore@1237 206 }
mcimadamore@1237 207
duke@1 208 protected Flow(Context context) {
duke@1 209 context.put(flowKey, this);
jjg@113 210 names = Names.instance(context);
duke@1 211 log = Log.instance(context);
duke@1 212 syms = Symtab.instance(context);
duke@1 213 types = Types.instance(context);
duke@1 214 chk = Check.instance(context);
duke@1 215 lint = Lint.instance(context);
mcimadamore@617 216 rs = Resolve.instance(context);
mcimadamore@550 217 Source source = Source.instance(context);
mcimadamore@935 218 allowImprovedRethrowAnalysis = source.allowImprovedRethrowAnalysis();
mcimadamore@935 219 allowImprovedCatchAnalysis = source.allowImprovedCatchAnalysis();
duke@1 220 }
duke@1 221
mcimadamore@1237 222 /**
mcimadamore@1237 223 * Base visitor class for all visitors implementing dataflow analysis logic.
mcimadamore@1237 224 * This class define the shared logic for handling jumps (break/continue statements).
duke@1 225 */
mcimadamore@1237 226 static abstract class BaseAnalyzer<P extends BaseAnalyzer.PendingExit> extends TreeScanner {
duke@1 227
mcimadamore@1237 228 enum JumpKind {
mcimadamore@1237 229 BREAK(JCTree.Tag.BREAK) {
mcimadamore@1237 230 @Override
mcimadamore@1237 231 JCTree getTarget(JCTree tree) {
mcimadamore@1237 232 return ((JCBreak)tree).target;
mcimadamore@1237 233 }
mcimadamore@1237 234 },
mcimadamore@1237 235 CONTINUE(JCTree.Tag.CONTINUE) {
mcimadamore@1237 236 @Override
mcimadamore@1237 237 JCTree getTarget(JCTree tree) {
mcimadamore@1237 238 return ((JCContinue)tree).target;
mcimadamore@1237 239 }
mcimadamore@1237 240 };
duke@1 241
mcimadamore@1237 242 JCTree.Tag treeTag;
duke@1 243
mcimadamore@1237 244 private JumpKind(Tag treeTag) {
mcimadamore@1237 245 this.treeTag = treeTag;
mcimadamore@1237 246 }
mcimadamore@550 247
mcimadamore@1237 248 abstract JCTree getTarget(JCTree tree);
mcimadamore@1237 249 }
duke@1 250
mcimadamore@1237 251 /** The currently pending exits that go from current inner blocks
mcimadamore@1237 252 * to an enclosing block, in source order.
mcimadamore@1237 253 */
mcimadamore@1237 254 ListBuffer<P> pendingExits;
duke@1 255
mcimadamore@1237 256 /** A pending exit. These are the statements return, break, and
mcimadamore@1237 257 * continue. In addition, exception-throwing expressions or
mcimadamore@1237 258 * statements are put here when not known to be caught. This
mcimadamore@1237 259 * will typically result in an error unless it is within a
mcimadamore@1237 260 * try-finally whose finally block cannot complete normally.
mcimadamore@1237 261 */
mcimadamore@1237 262 abstract static class PendingExit {
mcimadamore@1237 263 JCTree tree;
duke@1 264
mcimadamore@1237 265 PendingExit(JCTree tree) {
mcimadamore@1237 266 this.tree = tree;
mcimadamore@1237 267 }
duke@1 268
mcimadamore@1237 269 abstract void resolveJump();
mcimadamore@1237 270 }
duke@1 271
mcimadamore@1237 272 abstract void markDead();
duke@1 273
mcimadamore@1237 274 /** Record an outward transfer of control. */
mcimadamore@1237 275 void recordExit(JCTree tree, P pe) {
mcimadamore@1237 276 pendingExits.append(pe);
mcimadamore@1237 277 markDead();
mcimadamore@1237 278 }
duke@1 279
mcimadamore@1237 280 /** Resolve all jumps of this statement. */
mcimadamore@1237 281 private boolean resolveJump(JCTree tree,
mcimadamore@1237 282 ListBuffer<P> oldPendingExits,
mcimadamore@1237 283 JumpKind jk) {
mcimadamore@1237 284 boolean resolved = false;
mcimadamore@1237 285 List<P> exits = pendingExits.toList();
mcimadamore@1237 286 pendingExits = oldPendingExits;
mcimadamore@1237 287 for (; exits.nonEmpty(); exits = exits.tail) {
mcimadamore@1237 288 P exit = exits.head;
mcimadamore@1237 289 if (exit.tree.hasTag(jk.treeTag) &&
mcimadamore@1237 290 jk.getTarget(exit.tree) == tree) {
mcimadamore@1237 291 exit.resolveJump();
mcimadamore@1237 292 resolved = true;
mcimadamore@1237 293 } else {
mcimadamore@1237 294 pendingExits.append(exit);
mcimadamore@1237 295 }
mcimadamore@1237 296 }
mcimadamore@1237 297 return resolved;
mcimadamore@1237 298 }
duke@1 299
mcimadamore@1237 300 /** Resolve all breaks of this statement. */
mcimadamore@1237 301 boolean resolveContinues(JCTree tree) {
mcimadamore@1237 302 return resolveJump(tree, new ListBuffer<P>(), JumpKind.CONTINUE);
mcimadamore@1237 303 }
darcy@609 304
mcimadamore@1237 305 /** Resolve all continues of this statement. */
mcimadamore@1237 306 boolean resolveBreaks(JCTree tree, ListBuffer<P> oldPendingExits) {
mcimadamore@1237 307 return resolveJump(tree, oldPendingExits, JumpKind.BREAK);
duke@1 308 }
duke@1 309 }
duke@1 310
mcimadamore@1237 311 /**
mcimadamore@1237 312 * This pass implements the first two steps of the dataflow analysis:
mcimadamore@1237 313 * (i) liveness analysis checks that every statement is reachable and (ii)
mcimadamore@1237 314 * exception analysis to ensure that every checked exception that is
mcimadamore@1237 315 * thrown is declared or caught.
duke@1 316 */
mcimadamore@1237 317 class FlowAnalyzer extends BaseAnalyzer<FlowAnalyzer.FlowPendingExit> {
duke@1 318
mcimadamore@1237 319 /** A flag that indicates whether the last statement could
mcimadamore@1237 320 * complete normally.
mcimadamore@1237 321 */
mcimadamore@1237 322 private boolean alive;
duke@1 323
mcimadamore@1237 324 HashMap<Symbol, List<Type>> preciseRethrowTypes;
mcimadamore@1237 325
mcimadamore@1237 326 /** The current class being defined.
mcimadamore@1237 327 */
mcimadamore@1237 328 JCClassDecl classDef;
mcimadamore@1237 329
mcimadamore@1237 330 /** The list of possibly thrown declarable exceptions.
mcimadamore@1237 331 */
mcimadamore@1237 332 List<Type> thrown;
mcimadamore@1237 333
mcimadamore@1237 334 /** The list of exceptions that are either caught or declared to be
mcimadamore@1237 335 * thrown.
mcimadamore@1237 336 */
mcimadamore@1237 337 List<Type> caught;
mcimadamore@1237 338
mcimadamore@1237 339 class FlowPendingExit extends BaseAnalyzer.PendingExit {
mcimadamore@1237 340
mcimadamore@1237 341 Type thrown;
mcimadamore@1237 342
mcimadamore@1237 343 FlowPendingExit(JCTree tree, Type thrown) {
mcimadamore@1237 344 super(tree);
mcimadamore@1237 345 this.thrown = thrown;
mcimadamore@1237 346 }
mcimadamore@1237 347
mcimadamore@1237 348 void resolveJump() { /*do nothing*/ }
mcimadamore@1237 349 }
mcimadamore@1237 350
mcimadamore@1237 351 @Override
mcimadamore@1237 352 void markDead() {
mcimadamore@1237 353 alive = false;
mcimadamore@1237 354 }
mcimadamore@1237 355
mcimadamore@1237 356 /*-------------------- Exceptions ----------------------*/
mcimadamore@1237 357
mcimadamore@1237 358 /** Complain that pending exceptions are not caught.
mcimadamore@1237 359 */
mcimadamore@1237 360 void errorUncaught() {
mcimadamore@1237 361 for (FlowPendingExit exit = pendingExits.next();
mcimadamore@1237 362 exit != null;
mcimadamore@1237 363 exit = pendingExits.next()) {
mcimadamore@1237 364 if (classDef != null &&
mcimadamore@1237 365 classDef.pos == exit.tree.pos) {
mcimadamore@1237 366 log.error(exit.tree.pos(),
mcimadamore@1237 367 "unreported.exception.default.constructor",
mcimadamore@1237 368 exit.thrown);
mcimadamore@1237 369 } else if (exit.tree.hasTag(VARDEF) &&
mcimadamore@1237 370 ((JCVariableDecl)exit.tree).sym.isResourceVariable()) {
mcimadamore@1237 371 log.error(exit.tree.pos(),
mcimadamore@1237 372 "unreported.exception.implicit.close",
mcimadamore@1237 373 exit.thrown,
mcimadamore@1237 374 ((JCVariableDecl)exit.tree).sym.name);
mcimadamore@1237 375 } else {
mcimadamore@1237 376 log.error(exit.tree.pos(),
mcimadamore@1237 377 "unreported.exception.need.to.catch.or.throw",
mcimadamore@1237 378 exit.thrown);
mcimadamore@1237 379 }
mcimadamore@1237 380 }
mcimadamore@1237 381 }
mcimadamore@1237 382
mcimadamore@1237 383 /** Record that exception is potentially thrown and check that it
mcimadamore@1237 384 * is caught.
mcimadamore@1237 385 */
mcimadamore@1237 386 void markThrown(JCTree tree, Type exc) {
mcimadamore@1237 387 if (!chk.isUnchecked(tree.pos(), exc)) {
mcimadamore@1237 388 if (!chk.isHandled(exc, caught))
mcimadamore@1237 389 pendingExits.append(new FlowPendingExit(tree, exc));
mcimadamore@1237 390 thrown = chk.incl(exc, thrown);
mcimadamore@1237 391 }
mcimadamore@1237 392 }
mcimadamore@1237 393
mcimadamore@1237 394 /*************************************************************************
mcimadamore@1237 395 * Visitor methods for statements and definitions
mcimadamore@1237 396 *************************************************************************/
mcimadamore@1237 397
mcimadamore@1237 398 /** Analyze a definition.
mcimadamore@1237 399 */
mcimadamore@1237 400 void scanDef(JCTree tree) {
mcimadamore@1237 401 scanStat(tree);
mcimadamore@1237 402 if (tree != null && tree.hasTag(JCTree.Tag.BLOCK) && !alive) {
mcimadamore@1237 403 log.error(tree.pos(),
mcimadamore@1237 404 "initializer.must.be.able.to.complete.normally");
mcimadamore@1237 405 }
mcimadamore@1237 406 }
mcimadamore@1237 407
mcimadamore@1237 408 /** Analyze a statement. Check that statement is reachable.
mcimadamore@1237 409 */
mcimadamore@1237 410 void scanStat(JCTree tree) {
mcimadamore@1237 411 if (!alive && tree != null) {
mcimadamore@1237 412 log.error(tree.pos(), "unreachable.stmt");
mcimadamore@1237 413 if (!tree.hasTag(SKIP)) alive = true;
mcimadamore@1237 414 }
mcimadamore@1237 415 scan(tree);
mcimadamore@1237 416 }
mcimadamore@1237 417
mcimadamore@1237 418 /** Analyze list of statements.
mcimadamore@1237 419 */
mcimadamore@1237 420 void scanStats(List<? extends JCStatement> trees) {
mcimadamore@1237 421 if (trees != null)
mcimadamore@1237 422 for (List<? extends JCStatement> l = trees; l.nonEmpty(); l = l.tail)
mcimadamore@1237 423 scanStat(l.head);
mcimadamore@1237 424 }
mcimadamore@1237 425
mcimadamore@1237 426 /* ------------ Visitor methods for various sorts of trees -------------*/
mcimadamore@1237 427
mcimadamore@1237 428 public void visitClassDef(JCClassDecl tree) {
mcimadamore@1237 429 if (tree.sym == null) return;
mcimadamore@1237 430
mcimadamore@1237 431 JCClassDecl classDefPrev = classDef;
mcimadamore@1237 432 List<Type> thrownPrev = thrown;
mcimadamore@1237 433 List<Type> caughtPrev = caught;
mcimadamore@1237 434 boolean alivePrev = alive;
mcimadamore@1237 435 ListBuffer<FlowPendingExit> pendingExitsPrev = pendingExits;
mcimadamore@1237 436 Lint lintPrev = lint;
mcimadamore@1237 437
mcimadamore@1237 438 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 439 if (tree.name != names.empty) {
mcimadamore@1237 440 caught = List.nil();
mcimadamore@1237 441 }
mcimadamore@1237 442 classDef = tree;
mcimadamore@1237 443 thrown = List.nil();
mcimadamore@1237 444 lint = lint.augment(tree.sym.attributes_field);
mcimadamore@1237 445
mcimadamore@1237 446 try {
mcimadamore@1237 447 // process all the static initializers
mcimadamore@1237 448 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 449 if (!l.head.hasTag(METHODDEF) &&
mcimadamore@1237 450 (TreeInfo.flags(l.head) & STATIC) != 0) {
mcimadamore@1237 451 scanDef(l.head);
mcimadamore@1237 452 errorUncaught();
mcimadamore@1237 453 }
mcimadamore@1237 454 }
mcimadamore@1237 455
mcimadamore@1237 456 // add intersection of all thrown clauses of initial constructors
mcimadamore@1237 457 // to set of caught exceptions, unless class is anonymous.
mcimadamore@1237 458 if (tree.name != names.empty) {
mcimadamore@1237 459 boolean firstConstructor = true;
mcimadamore@1237 460 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 461 if (TreeInfo.isInitialConstructor(l.head)) {
mcimadamore@1237 462 List<Type> mthrown =
mcimadamore@1237 463 ((JCMethodDecl) l.head).sym.type.getThrownTypes();
mcimadamore@1237 464 if (firstConstructor) {
mcimadamore@1237 465 caught = mthrown;
mcimadamore@1237 466 firstConstructor = false;
mcimadamore@1237 467 } else {
mcimadamore@1237 468 caught = chk.intersect(mthrown, caught);
mcimadamore@1237 469 }
mcimadamore@1237 470 }
mcimadamore@1237 471 }
mcimadamore@1237 472 }
mcimadamore@1237 473
mcimadamore@1237 474 // process all the instance initializers
mcimadamore@1237 475 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 476 if (!l.head.hasTag(METHODDEF) &&
mcimadamore@1237 477 (TreeInfo.flags(l.head) & STATIC) == 0) {
mcimadamore@1237 478 scanDef(l.head);
mcimadamore@1237 479 errorUncaught();
mcimadamore@1237 480 }
mcimadamore@1237 481 }
mcimadamore@1237 482
mcimadamore@1237 483 // in an anonymous class, add the set of thrown exceptions to
mcimadamore@1237 484 // the throws clause of the synthetic constructor and propagate
mcimadamore@1237 485 // outwards.
mcimadamore@1237 486 // Changing the throws clause on the fly is okay here because
mcimadamore@1237 487 // the anonymous constructor can't be invoked anywhere else,
mcimadamore@1237 488 // and its type hasn't been cached.
mcimadamore@1237 489 if (tree.name == names.empty) {
mcimadamore@1237 490 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 491 if (TreeInfo.isInitialConstructor(l.head)) {
mcimadamore@1237 492 JCMethodDecl mdef = (JCMethodDecl)l.head;
mcimadamore@1237 493 mdef.thrown = make.Types(thrown);
mcimadamore@1237 494 mdef.sym.type = types.createMethodTypeWithThrown(mdef.sym.type, thrown);
mcimadamore@1237 495 }
mcimadamore@1237 496 }
mcimadamore@1237 497 thrownPrev = chk.union(thrown, thrownPrev);
mcimadamore@1237 498 }
mcimadamore@1237 499
mcimadamore@1237 500 // process all the methods
mcimadamore@1237 501 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 502 if (l.head.hasTag(METHODDEF)) {
mcimadamore@1237 503 scan(l.head);
mcimadamore@1237 504 errorUncaught();
mcimadamore@1237 505 }
mcimadamore@1237 506 }
mcimadamore@1237 507
mcimadamore@1237 508 thrown = thrownPrev;
mcimadamore@1237 509 } finally {
mcimadamore@1237 510 pendingExits = pendingExitsPrev;
mcimadamore@1237 511 alive = alivePrev;
mcimadamore@1237 512 caught = caughtPrev;
mcimadamore@1237 513 classDef = classDefPrev;
mcimadamore@1237 514 lint = lintPrev;
mcimadamore@1237 515 }
mcimadamore@1237 516 }
mcimadamore@1237 517
mcimadamore@1237 518 public void visitMethodDef(JCMethodDecl tree) {
mcimadamore@1237 519 if (tree.body == null) return;
mcimadamore@1237 520
mcimadamore@1237 521 List<Type> caughtPrev = caught;
mcimadamore@1237 522 List<Type> mthrown = tree.sym.type.getThrownTypes();
mcimadamore@1237 523 Lint lintPrev = lint;
mcimadamore@1237 524
mcimadamore@1237 525 lint = lint.augment(tree.sym.attributes_field);
mcimadamore@1237 526
mcimadamore@1237 527 Assert.check(pendingExits.isEmpty());
mcimadamore@1237 528
mcimadamore@1237 529 try {
mcimadamore@1237 530 for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 531 JCVariableDecl def = l.head;
mcimadamore@1237 532 scan(def);
mcimadamore@1237 533 }
mcimadamore@1237 534 if (TreeInfo.isInitialConstructor(tree))
mcimadamore@1237 535 caught = chk.union(caught, mthrown);
mcimadamore@1237 536 else if ((tree.sym.flags() & (BLOCK | STATIC)) != BLOCK)
mcimadamore@1237 537 caught = mthrown;
mcimadamore@1237 538 // else we are in an instance initializer block;
mcimadamore@1237 539 // leave caught unchanged.
mcimadamore@1237 540
mcimadamore@1237 541 alive = true;
mcimadamore@1237 542 scanStat(tree.body);
mcimadamore@1237 543
mcimadamore@1237 544 if (alive && tree.sym.type.getReturnType().tag != VOID)
mcimadamore@1237 545 log.error(TreeInfo.diagEndPos(tree.body), "missing.ret.stmt");
mcimadamore@1237 546
mcimadamore@1237 547 List<FlowPendingExit> exits = pendingExits.toList();
mcimadamore@1237 548 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 549 while (exits.nonEmpty()) {
mcimadamore@1237 550 FlowPendingExit exit = exits.head;
mcimadamore@1237 551 exits = exits.tail;
mcimadamore@1237 552 if (exit.thrown == null) {
mcimadamore@1237 553 Assert.check(exit.tree.hasTag(RETURN));
mcimadamore@1237 554 } else {
mcimadamore@1237 555 // uncaught throws will be reported later
mcimadamore@1237 556 pendingExits.append(exit);
mcimadamore@1237 557 }
mcimadamore@1237 558 }
mcimadamore@1237 559 } finally {
mcimadamore@1237 560 caught = caughtPrev;
mcimadamore@1237 561 lint = lintPrev;
mcimadamore@1237 562 }
mcimadamore@1237 563 }
mcimadamore@1237 564
mcimadamore@1237 565 public void visitVarDef(JCVariableDecl tree) {
mcimadamore@1237 566 if (tree.init != null) {
mcimadamore@1237 567 Lint lintPrev = lint;
mcimadamore@1237 568 lint = lint.augment(tree.sym.attributes_field);
mcimadamore@1237 569 try{
mcimadamore@1237 570 scan(tree.init);
mcimadamore@1237 571 } finally {
mcimadamore@1237 572 lint = lintPrev;
mcimadamore@1237 573 }
mcimadamore@1237 574 }
mcimadamore@1237 575 }
mcimadamore@1237 576
mcimadamore@1237 577 public void visitBlock(JCBlock tree) {
mcimadamore@1237 578 scanStats(tree.stats);
mcimadamore@1237 579 }
mcimadamore@1237 580
mcimadamore@1237 581 public void visitDoLoop(JCDoWhileLoop tree) {
mcimadamore@1237 582 ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 583 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 584 scanStat(tree.body);
mcimadamore@1237 585 alive |= resolveContinues(tree);
mcimadamore@1237 586 scan(tree.cond);
mcimadamore@1237 587 alive = alive && !tree.cond.type.isTrue();
mcimadamore@1237 588 alive |= resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 589 }
mcimadamore@1237 590
mcimadamore@1237 591 public void visitWhileLoop(JCWhileLoop tree) {
mcimadamore@1237 592 ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 593 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 594 scan(tree.cond);
mcimadamore@1237 595 alive = !tree.cond.type.isFalse();
mcimadamore@1237 596 scanStat(tree.body);
mcimadamore@1237 597 alive |= resolveContinues(tree);
mcimadamore@1237 598 alive = resolveBreaks(tree, prevPendingExits) ||
mcimadamore@1237 599 !tree.cond.type.isTrue();
mcimadamore@1237 600 }
mcimadamore@1237 601
mcimadamore@1237 602 public void visitForLoop(JCForLoop tree) {
mcimadamore@1237 603 ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 604 scanStats(tree.init);
mcimadamore@1237 605 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 606 if (tree.cond != null) {
mcimadamore@1237 607 scan(tree.cond);
mcimadamore@1237 608 alive = !tree.cond.type.isFalse();
mcimadamore@878 609 } else {
mcimadamore@1237 610 alive = true;
mcimadamore@1237 611 }
mcimadamore@1237 612 scanStat(tree.body);
mcimadamore@1237 613 alive |= resolveContinues(tree);
mcimadamore@1237 614 scan(tree.step);
mcimadamore@1237 615 alive = resolveBreaks(tree, prevPendingExits) ||
mcimadamore@1237 616 tree.cond != null && !tree.cond.type.isTrue();
mcimadamore@1237 617 }
mcimadamore@1237 618
mcimadamore@1237 619 public void visitForeachLoop(JCEnhancedForLoop tree) {
mcimadamore@1237 620 visitVarDef(tree.var);
mcimadamore@1237 621 ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 622 scan(tree.expr);
mcimadamore@1237 623 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 624 scanStat(tree.body);
mcimadamore@1237 625 alive |= resolveContinues(tree);
mcimadamore@1237 626 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 627 alive = true;
mcimadamore@1237 628 }
mcimadamore@1237 629
mcimadamore@1237 630 public void visitLabelled(JCLabeledStatement tree) {
mcimadamore@1237 631 ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 632 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 633 scanStat(tree.body);
mcimadamore@1237 634 alive |= resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 635 }
mcimadamore@1237 636
mcimadamore@1237 637 public void visitSwitch(JCSwitch tree) {
mcimadamore@1237 638 ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 639 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 640 scan(tree.selector);
mcimadamore@1237 641 boolean hasDefault = false;
mcimadamore@1237 642 for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 643 alive = true;
mcimadamore@1237 644 JCCase c = l.head;
mcimadamore@1237 645 if (c.pat == null)
mcimadamore@1237 646 hasDefault = true;
mcimadamore@1237 647 else
mcimadamore@1237 648 scan(c.pat);
mcimadamore@1237 649 scanStats(c.stats);
mcimadamore@1237 650 // Warn about fall-through if lint switch fallthrough enabled.
mcimadamore@1237 651 if (alive &&
mcimadamore@1237 652 lint.isEnabled(Lint.LintCategory.FALLTHROUGH) &&
mcimadamore@1237 653 c.stats.nonEmpty() && l.tail.nonEmpty())
mcimadamore@1237 654 log.warning(Lint.LintCategory.FALLTHROUGH,
mcimadamore@1237 655 l.tail.head.pos(),
mcimadamore@1237 656 "possible.fall-through.into.case");
mcimadamore@1237 657 }
mcimadamore@1237 658 if (!hasDefault) {
mcimadamore@1237 659 alive = true;
mcimadamore@1237 660 }
mcimadamore@1237 661 alive |= resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 662 }
mcimadamore@1237 663
mcimadamore@1237 664 public void visitTry(JCTry tree) {
mcimadamore@1237 665 List<Type> caughtPrev = caught;
mcimadamore@1237 666 List<Type> thrownPrev = thrown;
mcimadamore@1237 667 thrown = List.nil();
mcimadamore@1237 668 for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 669 List<JCExpression> subClauses = TreeInfo.isMultiCatch(l.head) ?
mcimadamore@1237 670 ((JCTypeUnion)l.head.param.vartype).alternatives :
mcimadamore@1237 671 List.of(l.head.param.vartype);
mcimadamore@1237 672 for (JCExpression ct : subClauses) {
mcimadamore@1237 673 caught = chk.incl(ct.type, caught);
mcimadamore@1237 674 }
mcimadamore@1237 675 }
mcimadamore@1237 676
mcimadamore@1237 677 ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 678 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 679 for (JCTree resource : tree.resources) {
mcimadamore@1237 680 if (resource instanceof JCVariableDecl) {
mcimadamore@1237 681 JCVariableDecl vdecl = (JCVariableDecl) resource;
mcimadamore@1237 682 visitVarDef(vdecl);
mcimadamore@1237 683 } else if (resource instanceof JCExpression) {
mcimadamore@1237 684 scan((JCExpression) resource);
mcimadamore@1237 685 } else {
mcimadamore@1237 686 throw new AssertionError(tree); // parser error
mcimadamore@1237 687 }
mcimadamore@1237 688 }
mcimadamore@1237 689 for (JCTree resource : tree.resources) {
mcimadamore@1237 690 List<Type> closeableSupertypes = resource.type.isCompound() ?
mcimadamore@1237 691 types.interfaces(resource.type).prepend(types.supertype(resource.type)) :
mcimadamore@1237 692 List.of(resource.type);
mcimadamore@1237 693 for (Type sup : closeableSupertypes) {
mcimadamore@1237 694 if (types.asSuper(sup, syms.autoCloseableType.tsym) != null) {
mcimadamore@1237 695 Symbol closeMethod = rs.resolveQualifiedMethod(tree,
mcimadamore@1237 696 attrEnv,
mcimadamore@1237 697 sup,
mcimadamore@1237 698 names.close,
mcimadamore@1237 699 List.<Type>nil(),
mcimadamore@1237 700 List.<Type>nil());
mcimadamore@1237 701 if (closeMethod.kind == MTH) {
mcimadamore@1237 702 for (Type t : ((MethodSymbol)closeMethod).getThrownTypes()) {
mcimadamore@1237 703 markThrown(resource, t);
mcimadamore@1237 704 }
mcimadamore@1237 705 }
mcimadamore@1237 706 }
mcimadamore@1237 707 }
mcimadamore@1237 708 }
mcimadamore@1237 709 scanStat(tree.body);
mcimadamore@1237 710 List<Type> thrownInTry = allowImprovedCatchAnalysis ?
mcimadamore@1237 711 chk.union(thrown, List.of(syms.runtimeExceptionType, syms.errorType)) :
mcimadamore@1237 712 thrown;
mcimadamore@1237 713 thrown = thrownPrev;
mcimadamore@1237 714 caught = caughtPrev;
mcimadamore@1237 715 boolean aliveEnd = alive;
mcimadamore@1237 716
mcimadamore@1237 717 List<Type> caughtInTry = List.nil();
mcimadamore@1237 718 for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 719 alive = true;
mcimadamore@1237 720 JCVariableDecl param = l.head.param;
mcimadamore@1237 721 List<JCExpression> subClauses = TreeInfo.isMultiCatch(l.head) ?
mcimadamore@1237 722 ((JCTypeUnion)l.head.param.vartype).alternatives :
mcimadamore@1237 723 List.of(l.head.param.vartype);
mcimadamore@1237 724 List<Type> ctypes = List.nil();
mcimadamore@1237 725 List<Type> rethrownTypes = chk.diff(thrownInTry, caughtInTry);
mcimadamore@1237 726 for (JCExpression ct : subClauses) {
mcimadamore@1237 727 Type exc = ct.type;
mcimadamore@1237 728 if (exc != syms.unknownType) {
mcimadamore@1237 729 ctypes = ctypes.append(exc);
mcimadamore@1237 730 if (types.isSameType(exc, syms.objectType))
mcimadamore@1237 731 continue;
mcimadamore@1237 732 checkCaughtType(l.head.pos(), exc, thrownInTry, caughtInTry);
mcimadamore@1237 733 caughtInTry = chk.incl(exc, caughtInTry);
mcimadamore@1237 734 }
mcimadamore@1237 735 }
mcimadamore@1237 736 scan(param);
mcimadamore@1237 737 preciseRethrowTypes.put(param.sym, chk.intersect(ctypes, rethrownTypes));
mcimadamore@1237 738 scanStat(l.head.body);
mcimadamore@1237 739 preciseRethrowTypes.remove(param.sym);
mcimadamore@1237 740 aliveEnd |= alive;
mcimadamore@1237 741 }
mcimadamore@1237 742 if (tree.finalizer != null) {
mcimadamore@1237 743 List<Type> savedThrown = thrown;
mcimadamore@1237 744 thrown = List.nil();
mcimadamore@1237 745 ListBuffer<FlowPendingExit> exits = pendingExits;
mcimadamore@1237 746 pendingExits = prevPendingExits;
mcimadamore@1237 747 alive = true;
mcimadamore@1237 748 scanStat(tree.finalizer);
mcimadamore@1237 749 if (!alive) {
mcimadamore@1237 750 // discard exits and exceptions from try and finally
mcimadamore@1237 751 thrown = chk.union(thrown, thrownPrev);
mcimadamore@1237 752 if (lint.isEnabled(Lint.LintCategory.FINALLY)) {
mcimadamore@1237 753 log.warning(Lint.LintCategory.FINALLY,
mcimadamore@1237 754 TreeInfo.diagEndPos(tree.finalizer),
mcimadamore@1237 755 "finally.cannot.complete");
mcimadamore@1237 756 }
mcimadamore@1237 757 } else {
mcimadamore@1237 758 thrown = chk.union(thrown, chk.diff(thrownInTry, caughtInTry));
mcimadamore@1237 759 thrown = chk.union(thrown, savedThrown);
mcimadamore@1237 760 // FIX: this doesn't preserve source order of exits in catch
mcimadamore@1237 761 // versus finally!
mcimadamore@1237 762 while (exits.nonEmpty()) {
mcimadamore@1237 763 pendingExits.append(exits.next());
mcimadamore@1237 764 }
mcimadamore@1237 765 alive = aliveEnd;
mcimadamore@1237 766 }
mcimadamore@1237 767 tree.finallyCanCompleteNormally = alive;
mcimadamore@1237 768 } else {
mcimadamore@1237 769 thrown = chk.union(thrown, chk.diff(thrownInTry, caughtInTry));
mcimadamore@1237 770 alive = aliveEnd;
mcimadamore@1237 771 ListBuffer<FlowPendingExit> exits = pendingExits;
mcimadamore@1237 772 pendingExits = prevPendingExits;
mcimadamore@1237 773 while (exits.nonEmpty()) pendingExits.append(exits.next());
mcimadamore@1237 774 }
mcimadamore@1237 775 }
mcimadamore@1237 776
mcimadamore@1237 777 @Override
mcimadamore@1237 778 public void visitIf(JCIf tree) {
mcimadamore@1237 779 scan(tree.cond);
mcimadamore@1237 780 scanStat(tree.thenpart);
mcimadamore@1237 781 if (tree.elsepart != null) {
mcimadamore@1237 782 boolean aliveAfterThen = alive;
mcimadamore@1237 783 alive = true;
mcimadamore@1237 784 scanStat(tree.elsepart);
mcimadamore@1237 785 alive = alive | aliveAfterThen;
mcimadamore@1237 786 } else {
mcimadamore@1237 787 alive = true;
mcimadamore@1237 788 }
mcimadamore@1237 789 }
mcimadamore@1237 790
mcimadamore@1237 791 void checkCaughtType(DiagnosticPosition pos, Type exc, List<Type> thrownInTry, List<Type> caughtInTry) {
mcimadamore@1237 792 if (chk.subset(exc, caughtInTry)) {
mcimadamore@1237 793 log.error(pos, "except.already.caught", exc);
mcimadamore@1237 794 } else if (!chk.isUnchecked(pos, exc) &&
mcimadamore@1237 795 !isExceptionOrThrowable(exc) &&
mcimadamore@1237 796 !chk.intersects(exc, thrownInTry)) {
mcimadamore@1237 797 log.error(pos, "except.never.thrown.in.try", exc);
mcimadamore@1237 798 } else if (allowImprovedCatchAnalysis) {
mcimadamore@1237 799 List<Type> catchableThrownTypes = chk.intersect(List.of(exc), thrownInTry);
mcimadamore@1237 800 // 'catchableThrownTypes' cannnot possibly be empty - if 'exc' was an
mcimadamore@1237 801 // unchecked exception, the result list would not be empty, as the augmented
mcimadamore@1237 802 // thrown set includes { RuntimeException, Error }; if 'exc' was a checked
mcimadamore@1237 803 // exception, that would have been covered in the branch above
mcimadamore@1237 804 if (chk.diff(catchableThrownTypes, caughtInTry).isEmpty() &&
mcimadamore@1237 805 !isExceptionOrThrowable(exc)) {
mcimadamore@1237 806 String key = catchableThrownTypes.length() == 1 ?
mcimadamore@1237 807 "unreachable.catch" :
mcimadamore@1237 808 "unreachable.catch.1";
mcimadamore@1237 809 log.warning(pos, key, catchableThrownTypes);
mcimadamore@1237 810 }
mcimadamore@1237 811 }
mcimadamore@1237 812 }
mcimadamore@1237 813 //where
mcimadamore@1237 814 private boolean isExceptionOrThrowable(Type exc) {
mcimadamore@1237 815 return exc.tsym == syms.throwableType.tsym ||
mcimadamore@1237 816 exc.tsym == syms.exceptionType.tsym;
mcimadamore@1237 817 }
mcimadamore@1237 818
mcimadamore@1237 819 public void visitBreak(JCBreak tree) {
mcimadamore@1237 820 recordExit(tree, new FlowPendingExit(tree, null));
mcimadamore@1237 821 }
mcimadamore@1237 822
mcimadamore@1237 823 public void visitContinue(JCContinue tree) {
mcimadamore@1237 824 recordExit(tree, new FlowPendingExit(tree, null));
mcimadamore@1237 825 }
mcimadamore@1237 826
mcimadamore@1237 827 public void visitReturn(JCReturn tree) {
mcimadamore@1237 828 scan(tree.expr);
mcimadamore@1237 829 // if not initial constructor, should markDead instead of recordExit
mcimadamore@1237 830 recordExit(tree, new FlowPendingExit(tree, null));
mcimadamore@1237 831 }
mcimadamore@1237 832
mcimadamore@1237 833 public void visitThrow(JCThrow tree) {
mcimadamore@1237 834 scan(tree.expr);
mcimadamore@1237 835 Symbol sym = TreeInfo.symbol(tree.expr);
mcimadamore@1237 836 if (sym != null &&
mcimadamore@1237 837 sym.kind == VAR &&
mcimadamore@1237 838 (sym.flags() & (FINAL | EFFECTIVELY_FINAL)) != 0 &&
mcimadamore@1237 839 preciseRethrowTypes.get(sym) != null &&
mcimadamore@1237 840 allowImprovedRethrowAnalysis) {
mcimadamore@1237 841 for (Type t : preciseRethrowTypes.get(sym)) {
mcimadamore@1237 842 markThrown(tree, t);
mcimadamore@1237 843 }
mcimadamore@1237 844 }
mcimadamore@1237 845 else {
mcimadamore@1237 846 markThrown(tree, tree.expr.type);
mcimadamore@1237 847 }
mcimadamore@1237 848 markDead();
mcimadamore@1237 849 }
mcimadamore@1237 850
mcimadamore@1237 851 public void visitApply(JCMethodInvocation tree) {
mcimadamore@1237 852 scan(tree.meth);
mcimadamore@1237 853 scan(tree.args);
mcimadamore@1237 854 for (List<Type> l = tree.meth.type.getThrownTypes(); l.nonEmpty(); l = l.tail)
mcimadamore@1237 855 markThrown(tree, l.head);
mcimadamore@1237 856 }
mcimadamore@1237 857
mcimadamore@1237 858 public void visitNewClass(JCNewClass tree) {
mcimadamore@1237 859 scan(tree.encl);
mcimadamore@1237 860 scan(tree.args);
mcimadamore@1237 861 // scan(tree.def);
mcimadamore@1237 862 for (List<Type> l = tree.constructorType.getThrownTypes();
mcimadamore@1237 863 l.nonEmpty();
mcimadamore@1237 864 l = l.tail) {
mcimadamore@1237 865 markThrown(tree, l.head);
mcimadamore@1237 866 }
mcimadamore@1237 867 List<Type> caughtPrev = caught;
mcimadamore@1237 868 try {
mcimadamore@1237 869 // If the new class expression defines an anonymous class,
mcimadamore@1237 870 // analysis of the anonymous constructor may encounter thrown
mcimadamore@1237 871 // types which are unsubstituted type variables.
mcimadamore@1237 872 // However, since the constructor's actual thrown types have
mcimadamore@1237 873 // already been marked as thrown, it is safe to simply include
mcimadamore@1237 874 // each of the constructor's formal thrown types in the set of
mcimadamore@1237 875 // 'caught/declared to be thrown' types, for the duration of
mcimadamore@1237 876 // the class def analysis.
mcimadamore@1237 877 if (tree.def != null)
mcimadamore@1237 878 for (List<Type> l = tree.constructor.type.getThrownTypes();
mcimadamore@1237 879 l.nonEmpty();
mcimadamore@1237 880 l = l.tail) {
mcimadamore@1237 881 caught = chk.incl(l.head, caught);
mcimadamore@1237 882 }
mcimadamore@1237 883 scan(tree.def);
mcimadamore@1237 884 }
mcimadamore@1237 885 finally {
mcimadamore@1237 886 caught = caughtPrev;
mcimadamore@1237 887 }
mcimadamore@1237 888 }
mcimadamore@1237 889
mcimadamore@1237 890 public void visitTopLevel(JCCompilationUnit tree) {
mcimadamore@1237 891 // Do nothing for TopLevel since each class is visited individually
mcimadamore@1237 892 }
mcimadamore@1237 893
mcimadamore@1237 894 /**************************************************************************
mcimadamore@1237 895 * main method
mcimadamore@1237 896 *************************************************************************/
mcimadamore@1237 897
mcimadamore@1237 898 /** Perform definite assignment/unassignment analysis on a tree.
mcimadamore@1237 899 */
mcimadamore@1237 900 public void analyzeTree(Env<AttrContext> env, TreeMaker make) {
mcimadamore@1237 901 try {
mcimadamore@1237 902 attrEnv = env;
mcimadamore@1237 903 JCTree tree = env.tree;
mcimadamore@1237 904 Flow.this.make = make;
mcimadamore@1237 905 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 906 preciseRethrowTypes = new HashMap<Symbol, List<Type>>();
mcimadamore@1237 907 alive = true;
mcimadamore@1237 908 this.thrown = this.caught = null;
mcimadamore@1237 909 this.classDef = null;
mcimadamore@1237 910 scan(tree);
mcimadamore@1237 911 } finally {
mcimadamore@1237 912 pendingExits = null;
mcimadamore@1237 913 Flow.this.make = null;
mcimadamore@1237 914 this.thrown = this.caught = null;
mcimadamore@1237 915 this.classDef = null;
mcimadamore@878 916 }
duke@1 917 }
duke@1 918 }
duke@1 919
mcimadamore@1237 920 /**
mcimadamore@1237 921 * This pass implements (i) definite assignment analysis, which ensures that
mcimadamore@1237 922 * each variable is assigned when used and (ii) definite unassignment analysis,
mcimadamore@1237 923 * which ensures that no final variable is assigned more than once. This visitor
mcimadamore@1237 924 * depends on the results of the liveliness analyzer.
duke@1 925 */
mcimadamore@1237 926 class AssignAnalyzer extends BaseAnalyzer<AssignAnalyzer.AssignPendingExit> {
mcimadamore@1237 927
mcimadamore@1237 928 /** The set of definitely assigned variables.
mcimadamore@1237 929 */
mcimadamore@1237 930 Bits inits;
mcimadamore@1237 931
mcimadamore@1237 932 /** The set of definitely unassigned variables.
mcimadamore@1237 933 */
mcimadamore@1237 934 Bits uninits;
mcimadamore@1237 935
mcimadamore@1237 936 /** The set of variables that are definitely unassigned everywhere
mcimadamore@1237 937 * in current try block. This variable is maintained lazily; it is
mcimadamore@1237 938 * updated only when something gets removed from uninits,
mcimadamore@1237 939 * typically by being assigned in reachable code. To obtain the
mcimadamore@1237 940 * correct set of variables which are definitely unassigned
mcimadamore@1237 941 * anywhere in current try block, intersect uninitsTry and
mcimadamore@1237 942 * uninits.
mcimadamore@1237 943 */
mcimadamore@1237 944 Bits uninitsTry;
mcimadamore@1237 945
mcimadamore@1237 946 /** When analyzing a condition, inits and uninits are null.
mcimadamore@1237 947 * Instead we have:
mcimadamore@1237 948 */
mcimadamore@1237 949 Bits initsWhenTrue;
mcimadamore@1237 950 Bits initsWhenFalse;
mcimadamore@1237 951 Bits uninitsWhenTrue;
mcimadamore@1237 952 Bits uninitsWhenFalse;
mcimadamore@1237 953
mcimadamore@1237 954 /** A mapping from addresses to variable symbols.
mcimadamore@1237 955 */
mcimadamore@1237 956 VarSymbol[] vars;
mcimadamore@1237 957
mcimadamore@1237 958 /** The current class being defined.
mcimadamore@1237 959 */
mcimadamore@1237 960 JCClassDecl classDef;
mcimadamore@1237 961
mcimadamore@1237 962 /** The first variable sequence number in this class definition.
mcimadamore@1237 963 */
mcimadamore@1237 964 int firstadr;
mcimadamore@1237 965
mcimadamore@1237 966 /** The next available variable sequence number.
mcimadamore@1237 967 */
mcimadamore@1237 968 int nextadr;
mcimadamore@1237 969
mcimadamore@1237 970 /** The list of unreferenced automatic resources.
mcimadamore@1237 971 */
mcimadamore@1237 972 Scope unrefdResources;
mcimadamore@1237 973
mcimadamore@1237 974 /** Set when processing a loop body the second time for DU analysis. */
mcimadamore@1237 975 boolean loopPassTwo = false;
mcimadamore@1237 976
mcimadamore@1237 977 class AssignPendingExit extends BaseAnalyzer.PendingExit {
mcimadamore@1237 978
mcimadamore@1237 979 Bits exit_inits;
mcimadamore@1237 980 Bits exit_uninits;
mcimadamore@1237 981
mcimadamore@1237 982 AssignPendingExit(JCTree tree, Bits inits, Bits uninits) {
mcimadamore@1237 983 super(tree);
mcimadamore@1237 984 this.exit_inits = inits.dup();
mcimadamore@1237 985 this.exit_uninits = uninits.dup();
mcimadamore@1237 986 }
mcimadamore@1237 987
mcimadamore@1237 988 void resolveJump() {
mcimadamore@1237 989 inits.andSet(exit_inits);
mcimadamore@1237 990 uninits.andSet(exit_uninits);
mcimadamore@1237 991 }
duke@1 992 }
duke@1 993
mcimadamore@1237 994 @Override
mcimadamore@1237 995 void markDead() {
mcimadamore@1237 996 inits.inclRange(firstadr, nextadr);
mcimadamore@1237 997 uninits.inclRange(firstadr, nextadr);
mcimadamore@1237 998 }
duke@1 999
mcimadamore@1237 1000 /*-------------- Processing variables ----------------------*/
duke@1 1001
mcimadamore@1237 1002 /** Do we need to track init/uninit state of this symbol?
mcimadamore@1237 1003 * I.e. is symbol either a local or a blank final variable?
mcimadamore@1237 1004 */
mcimadamore@1237 1005 boolean trackable(VarSymbol sym) {
mcimadamore@1237 1006 return
mcimadamore@1237 1007 (sym.owner.kind == MTH ||
mcimadamore@1237 1008 ((sym.flags() & (FINAL | HASINIT | PARAMETER)) == FINAL &&
mcimadamore@1237 1009 classDef.sym.isEnclosedBy((ClassSymbol)sym.owner)));
duke@1 1010 }
duke@1 1011
mcimadamore@1237 1012 /** Initialize new trackable variable by setting its address field
mcimadamore@1237 1013 * to the next available sequence number and entering it under that
mcimadamore@1237 1014 * index into the vars array.
mcimadamore@1237 1015 */
mcimadamore@1237 1016 void newVar(VarSymbol sym) {
mcimadamore@1237 1017 if (nextadr == vars.length) {
mcimadamore@1237 1018 VarSymbol[] newvars = new VarSymbol[nextadr * 2];
mcimadamore@1237 1019 System.arraycopy(vars, 0, newvars, 0, nextadr);
mcimadamore@1237 1020 vars = newvars;
mcimadamore@1237 1021 }
mcimadamore@1237 1022 sym.adr = nextadr;
mcimadamore@1237 1023 vars[nextadr] = sym;
mcimadamore@1237 1024 inits.excl(nextadr);
mcimadamore@1237 1025 uninits.incl(nextadr);
mcimadamore@1237 1026 nextadr++;
mcimadamore@1237 1027 }
mcimadamore@1237 1028
mcimadamore@1237 1029 /** Record an initialization of a trackable variable.
mcimadamore@1237 1030 */
mcimadamore@1237 1031 void letInit(DiagnosticPosition pos, VarSymbol sym) {
mcimadamore@1237 1032 if (sym.adr >= firstadr && trackable(sym)) {
mcimadamore@1237 1033 if ((sym.flags() & FINAL) != 0) {
mcimadamore@1237 1034 if ((sym.flags() & PARAMETER) != 0) {
mcimadamore@1237 1035 if ((sym.flags() & UNION) != 0) { //multi-catch parameter
mcimadamore@1237 1036 log.error(pos, "multicatch.parameter.may.not.be.assigned",
mcimadamore@1237 1037 sym);
mcimadamore@1237 1038 }
mcimadamore@1237 1039 else {
mcimadamore@1237 1040 log.error(pos, "final.parameter.may.not.be.assigned",
mcimadamore@550 1041 sym);
mcimadamore@1237 1042 }
mcimadamore@1237 1043 } else if (!uninits.isMember(sym.adr)) {
mcimadamore@1237 1044 log.error(pos,
mcimadamore@1237 1045 loopPassTwo
mcimadamore@1237 1046 ? "var.might.be.assigned.in.loop"
mcimadamore@1237 1047 : "var.might.already.be.assigned",
mcimadamore@1237 1048 sym);
mcimadamore@1237 1049 } else if (!inits.isMember(sym.adr)) {
mcimadamore@1237 1050 // reachable assignment
mcimadamore@1237 1051 uninits.excl(sym.adr);
mcimadamore@1237 1052 uninitsTry.excl(sym.adr);
mcimadamore@1237 1053 } else {
mcimadamore@1237 1054 //log.rawWarning(pos, "unreachable assignment");//DEBUG
mcimadamore@1237 1055 uninits.excl(sym.adr);
mcimadamore@550 1056 }
mcimadamore@1237 1057 }
mcimadamore@1237 1058 inits.incl(sym.adr);
mcimadamore@1237 1059 } else if ((sym.flags() & FINAL) != 0) {
mcimadamore@1237 1060 log.error(pos, "var.might.already.be.assigned", sym);
mcimadamore@1237 1061 }
mcimadamore@1237 1062 }
mcimadamore@1237 1063
mcimadamore@1237 1064 /** If tree is either a simple name or of the form this.name or
mcimadamore@1237 1065 * C.this.name, and tree represents a trackable variable,
mcimadamore@1237 1066 * record an initialization of the variable.
mcimadamore@1237 1067 */
mcimadamore@1237 1068 void letInit(JCTree tree) {
mcimadamore@1237 1069 tree = TreeInfo.skipParens(tree);
mcimadamore@1237 1070 if (tree.hasTag(IDENT) || tree.hasTag(SELECT)) {
mcimadamore@1237 1071 Symbol sym = TreeInfo.symbol(tree);
mcimadamore@1237 1072 if (sym.kind == VAR) {
mcimadamore@1237 1073 letInit(tree.pos(), (VarSymbol)sym);
duke@1 1074 }
duke@1 1075 }
duke@1 1076 }
duke@1 1077
mcimadamore@1237 1078 /** Check that trackable variable is initialized.
mcimadamore@1237 1079 */
mcimadamore@1237 1080 void checkInit(DiagnosticPosition pos, VarSymbol sym) {
mcimadamore@1237 1081 if ((sym.adr >= firstadr || sym.owner.kind != TYP) &&
mcimadamore@1237 1082 trackable(sym) &&
mcimadamore@1237 1083 !inits.isMember(sym.adr)) {
mcimadamore@1237 1084 log.error(pos, "var.might.not.have.been.initialized",
mcimadamore@1237 1085 sym);
mcimadamore@1237 1086 inits.incl(sym.adr);
mcimadamore@676 1087 }
duke@1 1088 }
duke@1 1089
mcimadamore@1237 1090 /** Split (duplicate) inits/uninits into WhenTrue/WhenFalse sets
mcimadamore@1237 1091 */
mcimadamore@1237 1092 void split(boolean setToNull) {
mcimadamore@1237 1093 initsWhenFalse = inits.dup();
mcimadamore@1237 1094 uninitsWhenFalse = uninits.dup();
mcimadamore@1237 1095 initsWhenTrue = inits;
mcimadamore@1237 1096 uninitsWhenTrue = uninits;
mcimadamore@1237 1097 if (setToNull)
mcimadamore@1237 1098 inits = uninits = null;
duke@1 1099 }
duke@1 1100
mcimadamore@1237 1101 /** Merge (intersect) inits/uninits from WhenTrue/WhenFalse sets.
mcimadamore@1237 1102 */
mcimadamore@1237 1103 void merge() {
mcimadamore@1237 1104 inits = initsWhenFalse.andSet(initsWhenTrue);
mcimadamore@1237 1105 uninits = uninitsWhenFalse.andSet(uninitsWhenTrue);
mcimadamore@1237 1106 }
duke@1 1107
mcimadamore@1237 1108 /* ************************************************************************
mcimadamore@1237 1109 * Visitor methods for statements and definitions
mcimadamore@1237 1110 *************************************************************************/
duke@1 1111
mcimadamore@1237 1112 /** Analyze an expression. Make sure to set (un)inits rather than
mcimadamore@1237 1113 * (un)initsWhenTrue(WhenFalse) on exit.
mcimadamore@1237 1114 */
mcimadamore@1237 1115 void scanExpr(JCTree tree) {
mcimadamore@1237 1116 if (tree != null) {
mcimadamore@1237 1117 scan(tree);
mcimadamore@1237 1118 if (inits == null) merge();
duke@1 1119 }
duke@1 1120 }
duke@1 1121
mcimadamore@1237 1122 /** Analyze a list of expressions.
mcimadamore@1237 1123 */
mcimadamore@1237 1124 void scanExprs(List<? extends JCExpression> trees) {
mcimadamore@1237 1125 if (trees != null)
mcimadamore@1237 1126 for (List<? extends JCExpression> l = trees; l.nonEmpty(); l = l.tail)
mcimadamore@1237 1127 scanExpr(l.head);
mcimadamore@1237 1128 }
mcimadamore@1237 1129
mcimadamore@1237 1130 /** Analyze a condition. Make sure to set (un)initsWhenTrue(WhenFalse)
mcimadamore@1237 1131 * rather than (un)inits on exit.
mcimadamore@1237 1132 */
mcimadamore@1237 1133 void scanCond(JCTree tree) {
mcimadamore@1237 1134 if (tree.type.isFalse()) {
mcimadamore@1237 1135 if (inits == null) merge();
mcimadamore@1237 1136 initsWhenTrue = inits.dup();
mcimadamore@1237 1137 initsWhenTrue.inclRange(firstadr, nextadr);
mcimadamore@1237 1138 uninitsWhenTrue = uninits.dup();
mcimadamore@1237 1139 uninitsWhenTrue.inclRange(firstadr, nextadr);
mcimadamore@1237 1140 initsWhenFalse = inits;
mcimadamore@1237 1141 uninitsWhenFalse = uninits;
mcimadamore@1237 1142 } else if (tree.type.isTrue()) {
mcimadamore@1237 1143 if (inits == null) merge();
mcimadamore@1237 1144 initsWhenFalse = inits.dup();
mcimadamore@1237 1145 initsWhenFalse.inclRange(firstadr, nextadr);
mcimadamore@1237 1146 uninitsWhenFalse = uninits.dup();
mcimadamore@1237 1147 uninitsWhenFalse.inclRange(firstadr, nextadr);
mcimadamore@1237 1148 initsWhenTrue = inits;
mcimadamore@1237 1149 uninitsWhenTrue = uninits;
duke@1 1150 } else {
mcimadamore@1237 1151 scan(tree);
mcimadamore@1237 1152 if (inits != null)
mcimadamore@1237 1153 split(tree.type != syms.unknownType);
duke@1 1154 }
mcimadamore@1237 1155 if (tree.type != syms.unknownType)
mcimadamore@1237 1156 inits = uninits = null;
duke@1 1157 }
duke@1 1158
mcimadamore@1237 1159 /* ------------ Visitor methods for various sorts of trees -------------*/
duke@1 1160
mcimadamore@1237 1161 public void visitClassDef(JCClassDecl tree) {
mcimadamore@1237 1162 if (tree.sym == null) return;
duke@1 1163
mcimadamore@1237 1164 JCClassDecl classDefPrev = classDef;
mcimadamore@1237 1165 int firstadrPrev = firstadr;
mcimadamore@1237 1166 int nextadrPrev = nextadr;
mcimadamore@1237 1167 ListBuffer<AssignPendingExit> pendingExitsPrev = pendingExits;
mcimadamore@1237 1168 Lint lintPrev = lint;
duke@1 1169
mcimadamore@1237 1170 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1171 if (tree.name != names.empty) {
mcimadamore@1237 1172 firstadr = nextadr;
mcimadamore@1237 1173 }
mcimadamore@1237 1174 classDef = tree;
mcimadamore@1237 1175 lint = lint.augment(tree.sym.attributes_field);
duke@1 1176
mcimadamore@1237 1177 try {
mcimadamore@1237 1178 // define all the static fields
duke@1 1179 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1180 if (l.head.hasTag(VARDEF)) {
mcimadamore@1237 1181 JCVariableDecl def = (JCVariableDecl)l.head;
mcimadamore@1237 1182 if ((def.mods.flags & STATIC) != 0) {
mcimadamore@1237 1183 VarSymbol sym = def.sym;
mcimadamore@1237 1184 if (trackable(sym))
mcimadamore@1237 1185 newVar(sym);
duke@1 1186 }
duke@1 1187 }
duke@1 1188 }
duke@1 1189
mcimadamore@1237 1190 // process all the static initializers
mcimadamore@1237 1191 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1192 if (!l.head.hasTag(METHODDEF) &&
mcimadamore@1237 1193 (TreeInfo.flags(l.head) & STATIC) != 0) {
mcimadamore@1237 1194 scan(l.head);
duke@1 1195 }
duke@1 1196 }
duke@1 1197
mcimadamore@1237 1198 // define all the instance fields
duke@1 1199 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1200 if (l.head.hasTag(VARDEF)) {
mcimadamore@1237 1201 JCVariableDecl def = (JCVariableDecl)l.head;
mcimadamore@1237 1202 if ((def.mods.flags & STATIC) == 0) {
mcimadamore@1237 1203 VarSymbol sym = def.sym;
mcimadamore@1237 1204 if (trackable(sym))
mcimadamore@1237 1205 newVar(sym);
mcimadamore@1237 1206 }
duke@1 1207 }
duke@1 1208 }
mcimadamore@1237 1209
mcimadamore@1237 1210 // process all the instance initializers
mcimadamore@1237 1211 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1212 if (!l.head.hasTag(METHODDEF) &&
mcimadamore@1237 1213 (TreeInfo.flags(l.head) & STATIC) == 0) {
mcimadamore@1237 1214 scan(l.head);
mcimadamore@1237 1215 }
mcimadamore@1237 1216 }
mcimadamore@1237 1217
mcimadamore@1237 1218 // process all the methods
mcimadamore@1237 1219 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1220 if (l.head.hasTag(METHODDEF)) {
mcimadamore@1237 1221 scan(l.head);
mcimadamore@1237 1222 }
mcimadamore@1237 1223 }
mcimadamore@1237 1224 } finally {
mcimadamore@1237 1225 pendingExits = pendingExitsPrev;
mcimadamore@1237 1226 nextadr = nextadrPrev;
mcimadamore@1237 1227 firstadr = firstadrPrev;
mcimadamore@1237 1228 classDef = classDefPrev;
mcimadamore@1237 1229 lint = lintPrev;
duke@1 1230 }
mcimadamore@1237 1231 }
duke@1 1232
mcimadamore@1237 1233 public void visitMethodDef(JCMethodDecl tree) {
mcimadamore@1237 1234 if (tree.body == null) return;
mcimadamore@1237 1235
mcimadamore@1237 1236 Bits initsPrev = inits.dup();
mcimadamore@1237 1237 Bits uninitsPrev = uninits.dup();
mcimadamore@1237 1238 int nextadrPrev = nextadr;
mcimadamore@1237 1239 int firstadrPrev = firstadr;
mcimadamore@1237 1240 Lint lintPrev = lint;
mcimadamore@1237 1241
mcimadamore@1237 1242 lint = lint.augment(tree.sym.attributes_field);
mcimadamore@1237 1243
mcimadamore@1237 1244 Assert.check(pendingExits.isEmpty());
mcimadamore@1237 1245
mcimadamore@1237 1246 try {
mcimadamore@1237 1247 boolean isInitialConstructor =
mcimadamore@1237 1248 TreeInfo.isInitialConstructor(tree);
mcimadamore@1237 1249
mcimadamore@1237 1250 if (!isInitialConstructor)
mcimadamore@1237 1251 firstadr = nextadr;
mcimadamore@1237 1252 for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1253 JCVariableDecl def = l.head;
mcimadamore@1237 1254 scan(def);
mcimadamore@1237 1255 inits.incl(def.sym.adr);
mcimadamore@1237 1256 uninits.excl(def.sym.adr);
duke@1 1257 }
mcimadamore@1237 1258 // else we are in an instance initializer block;
mcimadamore@1237 1259 // leave caught unchanged.
mcimadamore@1237 1260 scan(tree.body);
duke@1 1261
mcimadamore@1237 1262 if (isInitialConstructor) {
mcimadamore@1237 1263 for (int i = firstadr; i < nextadr; i++)
mcimadamore@1237 1264 if (vars[i].owner == classDef.sym)
mcimadamore@1237 1265 checkInit(TreeInfo.diagEndPos(tree.body), vars[i]);
mcimadamore@1237 1266 }
mcimadamore@1237 1267 List<AssignPendingExit> exits = pendingExits.toList();
mcimadamore@1237 1268 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1269 while (exits.nonEmpty()) {
mcimadamore@1237 1270 AssignPendingExit exit = exits.head;
mcimadamore@1237 1271 exits = exits.tail;
mcimadamore@1237 1272 Assert.check(exit.tree.hasTag(RETURN), exit.tree);
duke@1 1273 if (isInitialConstructor) {
mcimadamore@1237 1274 inits = exit.exit_inits;
duke@1 1275 for (int i = firstadr; i < nextadr; i++)
duke@1 1276 checkInit(exit.tree.pos(), vars[i]);
duke@1 1277 }
duke@1 1278 }
duke@1 1279 } finally {
mcimadamore@1237 1280 inits = initsPrev;
mcimadamore@1237 1281 uninits = uninitsPrev;
mcimadamore@1237 1282 nextadr = nextadrPrev;
mcimadamore@1237 1283 firstadr = firstadrPrev;
duke@1 1284 lint = lintPrev;
duke@1 1285 }
duke@1 1286 }
duke@1 1287
mcimadamore@1237 1288 public void visitVarDef(JCVariableDecl tree) {
mcimadamore@1237 1289 boolean track = trackable(tree.sym);
mcimadamore@1237 1290 if (track && tree.sym.owner.kind == MTH) newVar(tree.sym);
mcimadamore@1237 1291 if (tree.init != null) {
mcimadamore@1237 1292 Lint lintPrev = lint;
mcimadamore@1237 1293 lint = lint.augment(tree.sym.attributes_field);
mcimadamore@1237 1294 try{
mcimadamore@1237 1295 scanExpr(tree.init);
mcimadamore@1237 1296 if (track) letInit(tree.pos(), tree.sym);
mcimadamore@1237 1297 } finally {
mcimadamore@1237 1298 lint = lintPrev;
mcimadamore@1237 1299 }
mcimadamore@1237 1300 }
mcimadamore@1237 1301 }
duke@1 1302
mcimadamore@1237 1303 public void visitBlock(JCBlock tree) {
mcimadamore@1237 1304 int nextadrPrev = nextadr;
mcimadamore@1237 1305 scan(tree.stats);
mcimadamore@1237 1306 nextadr = nextadrPrev;
mcimadamore@1237 1307 }
duke@1 1308
mcimadamore@1237 1309 public void visitDoLoop(JCDoWhileLoop tree) {
mcimadamore@1237 1310 ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 1311 boolean prevLoopPassTwo = loopPassTwo;
mcimadamore@1237 1312 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1313 int prevErrors = log.nerrors;
mcimadamore@1237 1314 do {
mcimadamore@1237 1315 Bits uninitsEntry = uninits.dup();
mcimadamore@1237 1316 uninitsEntry.excludeFrom(nextadr);
mcimadamore@1237 1317 scan(tree.body);
mcimadamore@1237 1318 resolveContinues(tree);
mcimadamore@1237 1319 scanCond(tree.cond);
mcimadamore@1237 1320 if (log.nerrors != prevErrors ||
mcimadamore@1237 1321 loopPassTwo ||
mcimadamore@1237 1322 uninitsEntry.dup().diffSet(uninitsWhenTrue).nextBit(firstadr)==-1)
mcimadamore@1237 1323 break;
mcimadamore@1237 1324 inits = initsWhenTrue;
mcimadamore@1237 1325 uninits = uninitsEntry.andSet(uninitsWhenTrue);
mcimadamore@1237 1326 loopPassTwo = true;
mcimadamore@1237 1327 } while (true);
mcimadamore@1237 1328 loopPassTwo = prevLoopPassTwo;
mcimadamore@1237 1329 inits = initsWhenFalse;
mcimadamore@1237 1330 uninits = uninitsWhenFalse;
mcimadamore@1237 1331 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 1332 }
duke@1 1333
mcimadamore@1237 1334 public void visitWhileLoop(JCWhileLoop tree) {
mcimadamore@1237 1335 ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 1336 boolean prevLoopPassTwo = loopPassTwo;
mcimadamore@1237 1337 Bits initsCond;
mcimadamore@1237 1338 Bits uninitsCond;
mcimadamore@1237 1339 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1340 int prevErrors = log.nerrors;
mcimadamore@1237 1341 do {
mcimadamore@1237 1342 Bits uninitsEntry = uninits.dup();
mcimadamore@1237 1343 uninitsEntry.excludeFrom(nextadr);
duke@1 1344 scanCond(tree.cond);
duke@1 1345 initsCond = initsWhenFalse;
duke@1 1346 uninitsCond = uninitsWhenFalse;
duke@1 1347 inits = initsWhenTrue;
duke@1 1348 uninits = uninitsWhenTrue;
mcimadamore@1237 1349 scan(tree.body);
mcimadamore@1237 1350 resolveContinues(tree);
mcimadamore@1237 1351 if (log.nerrors != prevErrors ||
mcimadamore@1237 1352 loopPassTwo ||
mcimadamore@1237 1353 uninitsEntry.dup().diffSet(uninits).nextBit(firstadr) == -1)
mcimadamore@1237 1354 break;
mcimadamore@1237 1355 uninits = uninitsEntry.andSet(uninits);
mcimadamore@1237 1356 loopPassTwo = true;
mcimadamore@1237 1357 } while (true);
mcimadamore@1237 1358 loopPassTwo = prevLoopPassTwo;
mcimadamore@1237 1359 inits = initsCond;
mcimadamore@1237 1360 uninits = uninitsCond;
mcimadamore@1237 1361 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 1362 }
mcimadamore@1237 1363
mcimadamore@1237 1364 public void visitForLoop(JCForLoop tree) {
mcimadamore@1237 1365 ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 1366 boolean prevLoopPassTwo = loopPassTwo;
mcimadamore@1237 1367 int nextadrPrev = nextadr;
mcimadamore@1237 1368 scan(tree.init);
mcimadamore@1237 1369 Bits initsCond;
mcimadamore@1237 1370 Bits uninitsCond;
mcimadamore@1237 1371 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1372 int prevErrors = log.nerrors;
mcimadamore@1237 1373 do {
mcimadamore@1237 1374 Bits uninitsEntry = uninits.dup();
mcimadamore@1237 1375 uninitsEntry.excludeFrom(nextadr);
mcimadamore@1237 1376 if (tree.cond != null) {
mcimadamore@1237 1377 scanCond(tree.cond);
mcimadamore@1237 1378 initsCond = initsWhenFalse;
mcimadamore@1237 1379 uninitsCond = uninitsWhenFalse;
mcimadamore@1237 1380 inits = initsWhenTrue;
mcimadamore@1237 1381 uninits = uninitsWhenTrue;
mcimadamore@1237 1382 } else {
mcimadamore@1237 1383 initsCond = inits.dup();
mcimadamore@1237 1384 initsCond.inclRange(firstadr, nextadr);
mcimadamore@1237 1385 uninitsCond = uninits.dup();
mcimadamore@1237 1386 uninitsCond.inclRange(firstadr, nextadr);
mcimadamore@1237 1387 }
mcimadamore@1237 1388 scan(tree.body);
mcimadamore@1237 1389 resolveContinues(tree);
mcimadamore@1237 1390 scan(tree.step);
mcimadamore@1237 1391 if (log.nerrors != prevErrors ||
mcimadamore@1237 1392 loopPassTwo ||
mcimadamore@1237 1393 uninitsEntry.dup().diffSet(uninits).nextBit(firstadr) == -1)
mcimadamore@1237 1394 break;
mcimadamore@1237 1395 uninits = uninitsEntry.andSet(uninits);
mcimadamore@1237 1396 loopPassTwo = true;
mcimadamore@1237 1397 } while (true);
mcimadamore@1237 1398 loopPassTwo = prevLoopPassTwo;
mcimadamore@1237 1399 inits = initsCond;
mcimadamore@1237 1400 uninits = uninitsCond;
mcimadamore@1237 1401 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 1402 nextadr = nextadrPrev;
mcimadamore@1237 1403 }
mcimadamore@1237 1404
mcimadamore@1237 1405 public void visitForeachLoop(JCEnhancedForLoop tree) {
mcimadamore@1237 1406 visitVarDef(tree.var);
mcimadamore@1237 1407
mcimadamore@1237 1408 ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 1409 boolean prevLoopPassTwo = loopPassTwo;
mcimadamore@1237 1410 int nextadrPrev = nextadr;
mcimadamore@1237 1411 scan(tree.expr);
mcimadamore@1237 1412 Bits initsStart = inits.dup();
mcimadamore@1237 1413 Bits uninitsStart = uninits.dup();
mcimadamore@1237 1414
mcimadamore@1237 1415 letInit(tree.pos(), tree.var.sym);
mcimadamore@1237 1416 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1417 int prevErrors = log.nerrors;
mcimadamore@1237 1418 do {
mcimadamore@1237 1419 Bits uninitsEntry = uninits.dup();
mcimadamore@1237 1420 uninitsEntry.excludeFrom(nextadr);
mcimadamore@1237 1421 scan(tree.body);
mcimadamore@1237 1422 resolveContinues(tree);
mcimadamore@1237 1423 if (log.nerrors != prevErrors ||
mcimadamore@1237 1424 loopPassTwo ||
mcimadamore@1237 1425 uninitsEntry.dup().diffSet(uninits).nextBit(firstadr) == -1)
mcimadamore@1237 1426 break;
mcimadamore@1237 1427 uninits = uninitsEntry.andSet(uninits);
mcimadamore@1237 1428 loopPassTwo = true;
mcimadamore@1237 1429 } while (true);
mcimadamore@1237 1430 loopPassTwo = prevLoopPassTwo;
mcimadamore@1237 1431 inits = initsStart;
mcimadamore@1237 1432 uninits = uninitsStart.andSet(uninits);
mcimadamore@1237 1433 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 1434 nextadr = nextadrPrev;
mcimadamore@1237 1435 }
mcimadamore@1237 1436
mcimadamore@1237 1437 public void visitLabelled(JCLabeledStatement tree) {
mcimadamore@1237 1438 ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 1439 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1440 scan(tree.body);
mcimadamore@1237 1441 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 1442 }
mcimadamore@1237 1443
mcimadamore@1237 1444 public void visitSwitch(JCSwitch tree) {
mcimadamore@1237 1445 ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 1446 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1447 int nextadrPrev = nextadr;
mcimadamore@1237 1448 scanExpr(tree.selector);
mcimadamore@1237 1449 Bits initsSwitch = inits;
mcimadamore@1237 1450 Bits uninitsSwitch = uninits.dup();
mcimadamore@1237 1451 boolean hasDefault = false;
mcimadamore@1237 1452 for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1453 inits = initsSwitch.dup();
mcimadamore@1237 1454 uninits = uninits.andSet(uninitsSwitch);
mcimadamore@1237 1455 JCCase c = l.head;
mcimadamore@1237 1456 if (c.pat == null)
mcimadamore@1237 1457 hasDefault = true;
mcimadamore@1237 1458 else
mcimadamore@1237 1459 scanExpr(c.pat);
mcimadamore@1237 1460 scan(c.stats);
mcimadamore@1237 1461 addVars(c.stats, initsSwitch, uninitsSwitch);
mcimadamore@1237 1462 // Warn about fall-through if lint switch fallthrough enabled.
mcimadamore@1237 1463 }
mcimadamore@1237 1464 if (!hasDefault) {
mcimadamore@1237 1465 inits.andSet(initsSwitch);
mcimadamore@1237 1466 }
mcimadamore@1237 1467 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 1468 nextadr = nextadrPrev;
mcimadamore@1237 1469 }
mcimadamore@1237 1470 // where
mcimadamore@1237 1471 /** Add any variables defined in stats to inits and uninits. */
mcimadamore@1237 1472 private void addVars(List<JCStatement> stats, Bits inits,
mcimadamore@1237 1473 Bits uninits) {
mcimadamore@1237 1474 for (;stats.nonEmpty(); stats = stats.tail) {
mcimadamore@1237 1475 JCTree stat = stats.head;
mcimadamore@1237 1476 if (stat.hasTag(VARDEF)) {
mcimadamore@1237 1477 int adr = ((JCVariableDecl) stat).sym.adr;
mcimadamore@1237 1478 inits.excl(adr);
mcimadamore@1237 1479 uninits.incl(adr);
mcimadamore@1237 1480 }
mcimadamore@1237 1481 }
mcimadamore@1237 1482 }
mcimadamore@1237 1483
mcimadamore@1237 1484 public void visitTry(JCTry tree) {
mcimadamore@1237 1485 ListBuffer<JCVariableDecl> resourceVarDecls = ListBuffer.lb();
mcimadamore@1237 1486 Bits uninitsTryPrev = uninitsTry;
mcimadamore@1237 1487 ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 1488 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1489 Bits initsTry = inits.dup();
mcimadamore@1237 1490 uninitsTry = uninits.dup();
mcimadamore@1237 1491 for (JCTree resource : tree.resources) {
mcimadamore@1237 1492 if (resource instanceof JCVariableDecl) {
mcimadamore@1237 1493 JCVariableDecl vdecl = (JCVariableDecl) resource;
mcimadamore@1237 1494 visitVarDef(vdecl);
mcimadamore@1237 1495 unrefdResources.enter(vdecl.sym);
mcimadamore@1237 1496 resourceVarDecls.append(vdecl);
mcimadamore@1237 1497 } else if (resource instanceof JCExpression) {
mcimadamore@1237 1498 scanExpr((JCExpression) resource);
mcimadamore@1237 1499 } else {
mcimadamore@1237 1500 throw new AssertionError(tree); // parser error
mcimadamore@1237 1501 }
mcimadamore@1237 1502 }
mcimadamore@1237 1503 scan(tree.body);
mcimadamore@1237 1504 uninitsTry.andSet(uninits);
mcimadamore@1237 1505 Bits initsEnd = inits;
mcimadamore@1237 1506 Bits uninitsEnd = uninits;
mcimadamore@1237 1507 int nextadrCatch = nextadr;
mcimadamore@1237 1508
mcimadamore@1237 1509 if (!resourceVarDecls.isEmpty() &&
mcimadamore@1237 1510 lint.isEnabled(Lint.LintCategory.TRY)) {
mcimadamore@1237 1511 for (JCVariableDecl resVar : resourceVarDecls) {
mcimadamore@1237 1512 if (unrefdResources.includes(resVar.sym)) {
mcimadamore@1237 1513 log.warning(Lint.LintCategory.TRY, resVar.pos(),
mcimadamore@1237 1514 "try.resource.not.referenced", resVar.sym);
mcimadamore@1237 1515 unrefdResources.remove(resVar.sym);
mcimadamore@1237 1516 }
mcimadamore@1237 1517 }
mcimadamore@1237 1518 }
mcimadamore@1237 1519
mcimadamore@1237 1520 for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1521 JCVariableDecl param = l.head.param;
mcimadamore@1237 1522 inits = initsTry.dup();
mcimadamore@1237 1523 uninits = uninitsTry.dup();
mcimadamore@1237 1524 scan(param);
mcimadamore@1237 1525 inits.incl(param.sym.adr);
mcimadamore@1237 1526 uninits.excl(param.sym.adr);
mcimadamore@1237 1527 scan(l.head.body);
mcimadamore@1237 1528 initsEnd.andSet(inits);
mcimadamore@1237 1529 uninitsEnd.andSet(uninits);
mcimadamore@1237 1530 nextadr = nextadrCatch;
mcimadamore@1237 1531 }
mcimadamore@1237 1532 if (tree.finalizer != null) {
mcimadamore@1237 1533 inits = initsTry.dup();
mcimadamore@1237 1534 uninits = uninitsTry.dup();
mcimadamore@1237 1535 ListBuffer<AssignPendingExit> exits = pendingExits;
mcimadamore@1237 1536 pendingExits = prevPendingExits;
mcimadamore@1237 1537 scan(tree.finalizer);
mcimadamore@1237 1538 if (!tree.finallyCanCompleteNormally) {
mcimadamore@1237 1539 // discard exits and exceptions from try and finally
mcimadamore@1237 1540 } else {
mcimadamore@1237 1541 uninits.andSet(uninitsEnd);
mcimadamore@1237 1542 // FIX: this doesn't preserve source order of exits in catch
mcimadamore@1237 1543 // versus finally!
mcimadamore@1237 1544 while (exits.nonEmpty()) {
mcimadamore@1237 1545 AssignPendingExit exit = exits.next();
mcimadamore@1237 1546 if (exit.exit_inits != null) {
mcimadamore@1237 1547 exit.exit_inits.orSet(inits);
mcimadamore@1237 1548 exit.exit_uninits.andSet(uninits);
mcimadamore@1237 1549 }
mcimadamore@1237 1550 pendingExits.append(exit);
mcimadamore@1237 1551 }
mcimadamore@1237 1552 inits.orSet(initsEnd);
mcimadamore@1237 1553 }
duke@1 1554 } else {
mcimadamore@1237 1555 inits = initsEnd;
mcimadamore@1237 1556 uninits = uninitsEnd;
mcimadamore@1237 1557 ListBuffer<AssignPendingExit> exits = pendingExits;
mcimadamore@1237 1558 pendingExits = prevPendingExits;
mcimadamore@1237 1559 while (exits.nonEmpty()) pendingExits.append(exits.next());
duke@1 1560 }
mcimadamore@1237 1561 uninitsTry.andSet(uninitsTryPrev).andSet(uninits);
mcimadamore@1237 1562 }
duke@1 1563
mcimadamore@1237 1564 public void visitConditional(JCConditional tree) {
mcimadamore@1237 1565 scanCond(tree.cond);
mcimadamore@1237 1566 Bits initsBeforeElse = initsWhenFalse;
mcimadamore@1237 1567 Bits uninitsBeforeElse = uninitsWhenFalse;
mcimadamore@1237 1568 inits = initsWhenTrue;
mcimadamore@1237 1569 uninits = uninitsWhenTrue;
mcimadamore@1237 1570 if (tree.truepart.type.tag == BOOLEAN &&
mcimadamore@1237 1571 tree.falsepart.type.tag == BOOLEAN) {
mcimadamore@1237 1572 // if b and c are boolean valued, then
mcimadamore@1237 1573 // v is (un)assigned after a?b:c when true iff
mcimadamore@1237 1574 // v is (un)assigned after b when true and
mcimadamore@1237 1575 // v is (un)assigned after c when true
mcimadamore@1237 1576 scanCond(tree.truepart);
mcimadamore@1237 1577 Bits initsAfterThenWhenTrue = initsWhenTrue.dup();
mcimadamore@1237 1578 Bits initsAfterThenWhenFalse = initsWhenFalse.dup();
mcimadamore@1237 1579 Bits uninitsAfterThenWhenTrue = uninitsWhenTrue.dup();
mcimadamore@1237 1580 Bits uninitsAfterThenWhenFalse = uninitsWhenFalse.dup();
mcimadamore@1237 1581 inits = initsBeforeElse;
mcimadamore@1237 1582 uninits = uninitsBeforeElse;
mcimadamore@1237 1583 scanCond(tree.falsepart);
mcimadamore@1237 1584 initsWhenTrue.andSet(initsAfterThenWhenTrue);
mcimadamore@1237 1585 initsWhenFalse.andSet(initsAfterThenWhenFalse);
mcimadamore@1237 1586 uninitsWhenTrue.andSet(uninitsAfterThenWhenTrue);
mcimadamore@1237 1587 uninitsWhenFalse.andSet(uninitsAfterThenWhenFalse);
mcimadamore@1237 1588 } else {
mcimadamore@1237 1589 scanExpr(tree.truepart);
mcimadamore@1237 1590 Bits initsAfterThen = inits.dup();
mcimadamore@1237 1591 Bits uninitsAfterThen = uninits.dup();
mcimadamore@1237 1592 inits = initsBeforeElse;
mcimadamore@1237 1593 uninits = uninitsBeforeElse;
mcimadamore@1237 1594 scanExpr(tree.falsepart);
mcimadamore@1237 1595 inits.andSet(initsAfterThen);
mcimadamore@1237 1596 uninits.andSet(uninitsAfterThen);
duke@1 1597 }
duke@1 1598 }
duke@1 1599
mcimadamore@1237 1600 public void visitIf(JCIf tree) {
mcimadamore@1237 1601 scanCond(tree.cond);
mcimadamore@1237 1602 Bits initsBeforeElse = initsWhenFalse;
mcimadamore@1237 1603 Bits uninitsBeforeElse = uninitsWhenFalse;
mcimadamore@1237 1604 inits = initsWhenTrue;
mcimadamore@1237 1605 uninits = uninitsWhenTrue;
mcimadamore@1237 1606 scan(tree.thenpart);
mcimadamore@1237 1607 if (tree.elsepart != null) {
mcimadamore@1237 1608 Bits initsAfterThen = inits.dup();
mcimadamore@1237 1609 Bits uninitsAfterThen = uninits.dup();
mcimadamore@1237 1610 inits = initsBeforeElse;
mcimadamore@1237 1611 uninits = uninitsBeforeElse;
mcimadamore@1237 1612 scan(tree.elsepart);
mcimadamore@1237 1613 inits.andSet(initsAfterThen);
mcimadamore@1237 1614 uninits.andSet(uninitsAfterThen);
darcy@609 1615 } else {
mcimadamore@1237 1616 inits.andSet(initsBeforeElse);
mcimadamore@1237 1617 uninits.andSet(uninitsBeforeElse);
darcy@609 1618 }
darcy@609 1619 }
darcy@609 1620
mcimadamore@1237 1621 public void visitBreak(JCBreak tree) {
mcimadamore@1237 1622 recordExit(tree, new AssignPendingExit(tree, inits, uninits));
mcimadamore@1237 1623 }
mcimadamore@1237 1624
mcimadamore@1237 1625 public void visitContinue(JCContinue tree) {
mcimadamore@1237 1626 recordExit(tree, new AssignPendingExit(tree, inits, uninits));
mcimadamore@1237 1627 }
mcimadamore@1237 1628
mcimadamore@1237 1629 public void visitReturn(JCReturn tree) {
mcimadamore@1237 1630 scanExpr(tree.expr);
mcimadamore@1237 1631 // if not initial constructor, should markDead instead of recordExit
mcimadamore@1237 1632 recordExit(tree, new AssignPendingExit(tree, inits, uninits));
mcimadamore@1237 1633 }
mcimadamore@1237 1634
mcimadamore@1237 1635 public void visitThrow(JCThrow tree) {
mcimadamore@1237 1636 scanExpr(tree.expr);
mcimadamore@1237 1637 markDead();
mcimadamore@1237 1638 }
mcimadamore@1237 1639
mcimadamore@1237 1640 public void visitApply(JCMethodInvocation tree) {
mcimadamore@1237 1641 scanExpr(tree.meth);
mcimadamore@1237 1642 scanExprs(tree.args);
mcimadamore@1237 1643 }
mcimadamore@1237 1644
mcimadamore@1237 1645 public void visitNewClass(JCNewClass tree) {
mcimadamore@1237 1646 scanExpr(tree.encl);
mcimadamore@1237 1647 scanExprs(tree.args);
mcimadamore@1237 1648 scan(tree.def);
mcimadamore@1237 1649 }
mcimadamore@1237 1650
mcimadamore@1237 1651 public void visitNewArray(JCNewArray tree) {
mcimadamore@1237 1652 scanExprs(tree.dims);
mcimadamore@1237 1653 scanExprs(tree.elems);
mcimadamore@1237 1654 }
mcimadamore@1237 1655
mcimadamore@1237 1656 public void visitAssert(JCAssert tree) {
mcimadamore@1237 1657 Bits initsExit = inits.dup();
mcimadamore@1237 1658 Bits uninitsExit = uninits.dup();
mcimadamore@1237 1659 scanCond(tree.cond);
mcimadamore@1237 1660 uninitsExit.andSet(uninitsWhenTrue);
mcimadamore@1237 1661 if (tree.detail != null) {
mcimadamore@1237 1662 inits = initsWhenFalse;
mcimadamore@1237 1663 uninits = uninitsWhenFalse;
mcimadamore@1237 1664 scanExpr(tree.detail);
duke@1 1665 }
mcimadamore@1237 1666 inits = initsExit;
mcimadamore@1237 1667 uninits = uninitsExit;
duke@1 1668 }
mcimadamore@1237 1669
mcimadamore@1237 1670 public void visitAssign(JCAssign tree) {
mcimadamore@1237 1671 JCTree lhs = TreeInfo.skipParens(tree.lhs);
mcimadamore@1237 1672 if (!(lhs instanceof JCIdent)) scanExpr(lhs);
mcimadamore@1237 1673 scanExpr(tree.rhs);
mcimadamore@1237 1674 letInit(lhs);
mcimadamore@1237 1675 }
mcimadamore@1237 1676
mcimadamore@1237 1677 public void visitAssignop(JCAssignOp tree) {
mcimadamore@1237 1678 scanExpr(tree.lhs);
mcimadamore@1237 1679 scanExpr(tree.rhs);
mcimadamore@1237 1680 letInit(tree.lhs);
mcimadamore@1237 1681 }
mcimadamore@1237 1682
mcimadamore@1237 1683 public void visitUnary(JCUnary tree) {
mcimadamore@1237 1684 switch (tree.getTag()) {
mcimadamore@1237 1685 case NOT:
mcimadamore@1237 1686 scanCond(tree.arg);
mcimadamore@1237 1687 Bits t = initsWhenFalse;
mcimadamore@1237 1688 initsWhenFalse = initsWhenTrue;
mcimadamore@1237 1689 initsWhenTrue = t;
mcimadamore@1237 1690 t = uninitsWhenFalse;
mcimadamore@1237 1691 uninitsWhenFalse = uninitsWhenTrue;
mcimadamore@1237 1692 uninitsWhenTrue = t;
mcimadamore@1237 1693 break;
mcimadamore@1237 1694 case PREINC: case POSTINC:
mcimadamore@1237 1695 case PREDEC: case POSTDEC:
mcimadamore@1237 1696 scanExpr(tree.arg);
mcimadamore@1237 1697 letInit(tree.arg);
mcimadamore@1237 1698 break;
mcimadamore@1237 1699 default:
mcimadamore@1237 1700 scanExpr(tree.arg);
duke@1 1701 }
duke@1 1702 }
duke@1 1703
mcimadamore@1237 1704 public void visitBinary(JCBinary tree) {
mcimadamore@1237 1705 switch (tree.getTag()) {
mcimadamore@1237 1706 case AND:
mcimadamore@1237 1707 scanCond(tree.lhs);
mcimadamore@1237 1708 Bits initsWhenFalseLeft = initsWhenFalse;
mcimadamore@1237 1709 Bits uninitsWhenFalseLeft = uninitsWhenFalse;
mcimadamore@1237 1710 inits = initsWhenTrue;
mcimadamore@1237 1711 uninits = uninitsWhenTrue;
mcimadamore@1237 1712 scanCond(tree.rhs);
mcimadamore@1237 1713 initsWhenFalse.andSet(initsWhenFalseLeft);
mcimadamore@1237 1714 uninitsWhenFalse.andSet(uninitsWhenFalseLeft);
mcimadamore@1237 1715 break;
mcimadamore@1237 1716 case OR:
mcimadamore@1237 1717 scanCond(tree.lhs);
mcimadamore@1237 1718 Bits initsWhenTrueLeft = initsWhenTrue;
mcimadamore@1237 1719 Bits uninitsWhenTrueLeft = uninitsWhenTrue;
mcimadamore@1237 1720 inits = initsWhenFalse;
mcimadamore@1237 1721 uninits = uninitsWhenFalse;
mcimadamore@1237 1722 scanCond(tree.rhs);
mcimadamore@1237 1723 initsWhenTrue.andSet(initsWhenTrueLeft);
mcimadamore@1237 1724 uninitsWhenTrue.andSet(uninitsWhenTrueLeft);
mcimadamore@1237 1725 break;
mcimadamore@1237 1726 default:
mcimadamore@1237 1727 scanExpr(tree.lhs);
mcimadamore@1237 1728 scanExpr(tree.rhs);
mcimadamore@1237 1729 }
mcimadamore@1237 1730 }
mcimadamore@1237 1731
mcimadamore@1237 1732 public void visitIdent(JCIdent tree) {
mcimadamore@1237 1733 if (tree.sym.kind == VAR) {
mcimadamore@1237 1734 checkInit(tree.pos(), (VarSymbol)tree.sym);
mcimadamore@1237 1735 referenced(tree.sym);
mcimadamore@1237 1736 }
mcimadamore@1237 1737 }
mcimadamore@1237 1738
mcimadamore@1237 1739 void referenced(Symbol sym) {
mcimadamore@1237 1740 unrefdResources.remove(sym);
mcimadamore@1237 1741 }
mcimadamore@1237 1742
mcimadamore@1237 1743 public void visitTopLevel(JCCompilationUnit tree) {
mcimadamore@1237 1744 // Do nothing for TopLevel since each class is visited individually
mcimadamore@1237 1745 }
mcimadamore@1237 1746
mcimadamore@1237 1747 /**************************************************************************
mcimadamore@1237 1748 * main method
mcimadamore@1237 1749 *************************************************************************/
mcimadamore@1237 1750
mcimadamore@1237 1751 /** Perform definite assignment/unassignment analysis on a tree.
mcimadamore@1237 1752 */
mcimadamore@1237 1753 public void analyzeTree(Env<AttrContext> env, TreeMaker make) {
mcimadamore@1237 1754 try {
mcimadamore@1237 1755 attrEnv = env;
mcimadamore@1237 1756 JCTree tree = env.tree;
mcimadamore@1237 1757 Flow.this.make = make;
mcimadamore@1237 1758 inits = new Bits();
mcimadamore@1237 1759 uninits = new Bits();
mcimadamore@1237 1760 uninitsTry = new Bits();
mcimadamore@1237 1761 initsWhenTrue = initsWhenFalse =
mcimadamore@1237 1762 uninitsWhenTrue = uninitsWhenFalse = null;
mcimadamore@1237 1763 if (vars == null)
mcimadamore@1237 1764 vars = new VarSymbol[32];
mcimadamore@1237 1765 else
mcimadamore@1237 1766 for (int i=0; i<vars.length; i++)
mcimadamore@1237 1767 vars[i] = null;
mcimadamore@1237 1768 firstadr = 0;
mcimadamore@1237 1769 nextadr = 0;
mcimadamore@1237 1770 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1771 this.classDef = null;
mcimadamore@1237 1772 unrefdResources = new Scope(env.enclClass.sym);
mcimadamore@1237 1773 scan(tree);
mcimadamore@1237 1774 } finally {
mcimadamore@1237 1775 // note that recursive invocations of this method fail hard
mcimadamore@1237 1776 inits = uninits = uninitsTry = null;
mcimadamore@1237 1777 initsWhenTrue = initsWhenFalse =
mcimadamore@1237 1778 uninitsWhenTrue = uninitsWhenFalse = null;
mcimadamore@1237 1779 if (vars != null) for (int i=0; i<vars.length; i++)
mcimadamore@1237 1780 vars[i] = null;
mcimadamore@1237 1781 firstadr = 0;
mcimadamore@1237 1782 nextadr = 0;
mcimadamore@1237 1783 pendingExits = null;
mcimadamore@1237 1784 Flow.this.make = null;
mcimadamore@1237 1785 this.classDef = null;
mcimadamore@1237 1786 unrefdResources = null;
mcimadamore@935 1787 }
mcimadamore@935 1788 }
mcimadamore@935 1789 }
duke@1 1790 }

mercurial