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

Thu, 01 Nov 2012 10:48:36 +0100

author
ohrstrom
date
Thu, 01 Nov 2012 10:48:36 +0100
changeset 1384
bf54daa9dcd8
parent 1374
c002fdee76fd
child 1406
2901c7b5339e
permissions
-rw-r--r--

7153951: Add new lint option -Xlint:auxiliaryclass
Reviewed-by: jjg, mcimadamore, forax

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.*;
jjg@1374 43 import static com.sun.tools.javac.code.TypeTag.BOOLEAN;
jjg@1374 44 import static com.sun.tools.javac.code.TypeTag.VOID;
jjg@1127 45 import static com.sun.tools.javac.tree.JCTree.Tag.*;
duke@1 46
mcimadamore@1297 47 /** This pass implements dataflow analysis for Java programs though
mcimadamore@1297 48 * different AST visitor steps. Liveness analysis (see AliveAlanyzer) checks that
mcimadamore@1297 49 * every statement is reachable. Exception analysis (see FlowAnalyzer) ensures that
mcimadamore@1297 50 * every checked exception that is thrown is declared or caught. Definite assignment analysis
mcimadamore@1297 51 * (see AssignAnalyzer) ensures that each variable is assigned when used. Definite
mcimadamore@1297 52 * unassignment analysis (see AssignAnalyzer) in ensures that no final variable
mcimadamore@1297 53 * is assigned more than once. Finally, local variable capture analysis (see CaptureAnalyzer)
mcimadamore@1348 54 * determines that local variables accessed within the scope of an inner class/lambda
mcimadamore@1348 55 * are either final or effectively-final.
duke@1 56 *
jjh@972 57 * <p>The JLS has a number of problems in the
duke@1 58 * specification of these flow analysis problems. This implementation
duke@1 59 * attempts to address those issues.
duke@1 60 *
duke@1 61 * <p>First, there is no accommodation for a finally clause that cannot
duke@1 62 * complete normally. For liveness analysis, an intervening finally
duke@1 63 * clause can cause a break, continue, or return not to reach its
duke@1 64 * target. For exception analysis, an intervening finally clause can
duke@1 65 * cause any exception to be "caught". For DA/DU analysis, the finally
duke@1 66 * clause can prevent a transfer of control from propagating DA/DU
duke@1 67 * state to the target. In addition, code in the finally clause can
duke@1 68 * affect the DA/DU status of variables.
duke@1 69 *
duke@1 70 * <p>For try statements, we introduce the idea of a variable being
duke@1 71 * definitely unassigned "everywhere" in a block. A variable V is
duke@1 72 * "unassigned everywhere" in a block iff it is unassigned at the
duke@1 73 * beginning of the block and there is no reachable assignment to V
duke@1 74 * in the block. An assignment V=e is reachable iff V is not DA
duke@1 75 * after e. Then we can say that V is DU at the beginning of the
duke@1 76 * catch block iff V is DU everywhere in the try block. Similarly, V
duke@1 77 * is DU at the beginning of the finally block iff V is DU everywhere
duke@1 78 * in the try block and in every catch block. Specifically, the
duke@1 79 * following bullet is added to 16.2.2
duke@1 80 * <pre>
duke@1 81 * V is <em>unassigned everywhere</em> in a block if it is
duke@1 82 * unassigned before the block and there is no reachable
duke@1 83 * assignment to V within the block.
duke@1 84 * </pre>
duke@1 85 * <p>In 16.2.15, the third bullet (and all of its sub-bullets) for all
duke@1 86 * try blocks is changed to
duke@1 87 * <pre>
duke@1 88 * V is definitely unassigned before a catch block iff V is
duke@1 89 * definitely unassigned everywhere in the try block.
duke@1 90 * </pre>
duke@1 91 * <p>The last bullet (and all of its sub-bullets) for try blocks that
duke@1 92 * have a finally block is changed to
duke@1 93 * <pre>
duke@1 94 * V is definitely unassigned before the finally block iff
duke@1 95 * V is definitely unassigned everywhere in the try block
duke@1 96 * and everywhere in each catch block of the try statement.
duke@1 97 * </pre>
duke@1 98 * <p>In addition,
duke@1 99 * <pre>
duke@1 100 * V is definitely assigned at the end of a constructor iff
duke@1 101 * V is definitely assigned after the block that is the body
duke@1 102 * of the constructor and V is definitely assigned at every
duke@1 103 * return that can return from the constructor.
duke@1 104 * </pre>
duke@1 105 * <p>In addition, each continue statement with the loop as its target
duke@1 106 * is treated as a jump to the end of the loop body, and "intervening"
duke@1 107 * finally clauses are treated as follows: V is DA "due to the
duke@1 108 * continue" iff V is DA before the continue statement or V is DA at
duke@1 109 * the end of any intervening finally block. V is DU "due to the
duke@1 110 * continue" iff any intervening finally cannot complete normally or V
duke@1 111 * is DU at the end of every intervening finally block. This "due to
duke@1 112 * the continue" concept is then used in the spec for the loops.
duke@1 113 *
duke@1 114 * <p>Similarly, break statements must consider intervening finally
duke@1 115 * blocks. For liveness analysis, a break statement for which any
duke@1 116 * intervening finally cannot complete normally is not considered to
duke@1 117 * cause the target statement to be able to complete normally. Then
duke@1 118 * we say V is DA "due to the break" iff V is DA before the break or
duke@1 119 * V is DA at the end of any intervening finally block. V is DU "due
duke@1 120 * to the break" iff any intervening finally cannot complete normally
duke@1 121 * or V is DU at the break and at the end of every intervening
duke@1 122 * finally block. (I suspect this latter condition can be
duke@1 123 * simplified.) This "due to the break" is then used in the spec for
duke@1 124 * all statements that can be "broken".
duke@1 125 *
duke@1 126 * <p>The return statement is treated similarly. V is DA "due to a
duke@1 127 * return statement" iff V is DA before the return statement or V is
duke@1 128 * DA at the end of any intervening finally block. Note that we
duke@1 129 * don't have to worry about the return expression because this
duke@1 130 * concept is only used for construcrors.
duke@1 131 *
jjh@972 132 * <p>There is no spec in the JLS for when a variable is definitely
duke@1 133 * assigned at the end of a constructor, which is needed for final
duke@1 134 * fields (8.3.1.2). We implement the rule that V is DA at the end
duke@1 135 * of the constructor iff it is DA and the end of the body of the
duke@1 136 * constructor and V is DA "due to" every return of the constructor.
duke@1 137 *
duke@1 138 * <p>Intervening finally blocks similarly affect exception analysis. An
duke@1 139 * intervening finally that cannot complete normally allows us to ignore
duke@1 140 * an otherwise uncaught exception.
duke@1 141 *
duke@1 142 * <p>To implement the semantics of intervening finally clauses, all
duke@1 143 * nonlocal transfers (break, continue, return, throw, method call that
duke@1 144 * can throw a checked exception, and a constructor invocation that can
duke@1 145 * thrown a checked exception) are recorded in a queue, and removed
duke@1 146 * from the queue when we complete processing the target of the
duke@1 147 * nonlocal transfer. This allows us to modify the queue in accordance
duke@1 148 * with the above rules when we encounter a finally clause. The only
duke@1 149 * exception to this [no pun intended] is that checked exceptions that
duke@1 150 * are known to be caught or declared to be caught in the enclosing
duke@1 151 * method are not recorded in the queue, but instead are recorded in a
jjg@1358 152 * global variable "{@code Set<Type> thrown}" that records the type of all
duke@1 153 * exceptions that can be thrown.
duke@1 154 *
duke@1 155 * <p>Other minor issues the treatment of members of other classes
duke@1 156 * (always considered DA except that within an anonymous class
duke@1 157 * constructor, where DA status from the enclosing scope is
duke@1 158 * preserved), treatment of the case expression (V is DA before the
duke@1 159 * case expression iff V is DA after the switch expression),
duke@1 160 * treatment of variables declared in a switch block (the implied
duke@1 161 * DA/DU status after the switch expression is DU and not DA for
duke@1 162 * variables defined in a switch block), the treatment of boolean ?:
duke@1 163 * expressions (The JLS rules only handle b and c non-boolean; the
duke@1 164 * new rule is that if b and c are boolean valued, then V is
duke@1 165 * (un)assigned after a?b:c when true/false iff V is (un)assigned
duke@1 166 * after b when true/false and V is (un)assigned after c when
duke@1 167 * true/false).
duke@1 168 *
duke@1 169 * <p>There is the remaining question of what syntactic forms constitute a
duke@1 170 * reference to a variable. It is conventional to allow this.x on the
duke@1 171 * left-hand-side to initialize a final instance field named x, yet
duke@1 172 * this.x isn't considered a "use" when appearing on a right-hand-side
duke@1 173 * in most implementations. Should parentheses affect what is
duke@1 174 * considered a variable reference? The simplest rule would be to
duke@1 175 * allow unqualified forms only, parentheses optional, and phase out
duke@1 176 * support for assigning to a final field via this.x.
duke@1 177 *
jjg@581 178 * <p><b>This is NOT part of any supported API.
jjg@581 179 * If you write code that depends on this, you do so at your own risk.
duke@1 180 * This code and its internal interfaces are subject to change or
duke@1 181 * deletion without notice.</b>
duke@1 182 */
mcimadamore@1237 183 public class Flow {
duke@1 184 protected static final Context.Key<Flow> flowKey =
duke@1 185 new Context.Key<Flow>();
duke@1 186
jjg@113 187 private final Names names;
duke@1 188 private final Log log;
duke@1 189 private final Symtab syms;
duke@1 190 private final Types types;
duke@1 191 private final Check chk;
duke@1 192 private TreeMaker make;
mcimadamore@617 193 private final Resolve rs;
mcimadamore@1297 194 private final JCDiagnostic.Factory diags;
mcimadamore@617 195 private Env<AttrContext> attrEnv;
duke@1 196 private Lint lint;
mcimadamore@935 197 private final boolean allowImprovedRethrowAnalysis;
mcimadamore@935 198 private final boolean allowImprovedCatchAnalysis;
mcimadamore@1297 199 private final boolean allowEffectivelyFinalInInnerClasses;
duke@1 200
duke@1 201 public static Flow instance(Context context) {
duke@1 202 Flow instance = context.get(flowKey);
duke@1 203 if (instance == null)
duke@1 204 instance = new Flow(context);
duke@1 205 return instance;
duke@1 206 }
duke@1 207
mcimadamore@1237 208 public void analyzeTree(Env<AttrContext> env, TreeMaker make) {
mcimadamore@1297 209 new AliveAnalyzer().analyzeTree(env, make);
mcimadamore@1297 210 new AssignAnalyzer().analyzeTree(env, make);
mcimadamore@1237 211 new FlowAnalyzer().analyzeTree(env, make);
mcimadamore@1297 212 new CaptureAnalyzer().analyzeTree(env, make);
mcimadamore@1297 213 }
mcimadamore@1297 214
mcimadamore@1348 215 public void analyzeLambda(Env<AttrContext> env, JCLambda that, TreeMaker make, boolean speculative) {
mcimadamore@1348 216 java.util.Queue<JCDiagnostic> prevDeferredDiagnostics = log.deferredDiagnostics;
mcimadamore@1348 217 Filter<JCDiagnostic> prevDeferDiagsFilter = log.deferredDiagFilter;
mcimadamore@1348 218 //we need to disable diagnostics temporarily; the problem is that if
mcimadamore@1348 219 //a lambda expression contains e.g. an unreachable statement, an error
mcimadamore@1348 220 //message will be reported and will cause compilation to skip the flow analyis
mcimadamore@1348 221 //step - if we suppress diagnostics, we won't stop at Attr for flow-analysis
mcimadamore@1348 222 //related errors, which will allow for more errors to be detected
mcimadamore@1348 223 if (!speculative) {
mcimadamore@1348 224 log.deferAll();
mcimadamore@1348 225 log.deferredDiagnostics = ListBuffer.lb();
mcimadamore@1348 226 }
mcimadamore@1348 227 try {
mcimadamore@1348 228 new AliveAnalyzer().analyzeTree(env, that, make);
mcimadamore@1348 229 new FlowAnalyzer().analyzeTree(env, that, make);
mcimadamore@1348 230 } finally {
mcimadamore@1348 231 if (!speculative) {
mcimadamore@1348 232 log.deferredDiagFilter = prevDeferDiagsFilter;
mcimadamore@1348 233 log.deferredDiagnostics = prevDeferredDiagnostics;
mcimadamore@1348 234 }
mcimadamore@1348 235 }
mcimadamore@1348 236 }
mcimadamore@1348 237
mcimadamore@1297 238 /**
mcimadamore@1297 239 * Definite assignment scan mode
mcimadamore@1297 240 */
mcimadamore@1297 241 enum FlowKind {
mcimadamore@1297 242 /**
mcimadamore@1297 243 * This is the normal DA/DU analysis mode
mcimadamore@1297 244 */
mcimadamore@1297 245 NORMAL("var.might.already.be.assigned", false),
mcimadamore@1297 246 /**
mcimadamore@1297 247 * This is the speculative DA/DU analysis mode used to speculatively
mcimadamore@1297 248 * derive assertions within loop bodies
mcimadamore@1297 249 */
mcimadamore@1297 250 SPECULATIVE_LOOP("var.might.be.assigned.in.loop", true);
mcimadamore@1297 251
mcimadamore@1297 252 String errKey;
mcimadamore@1297 253 boolean isFinal;
mcimadamore@1297 254
mcimadamore@1297 255 FlowKind(String errKey, boolean isFinal) {
mcimadamore@1297 256 this.errKey = errKey;
mcimadamore@1297 257 this.isFinal = isFinal;
mcimadamore@1297 258 }
mcimadamore@1297 259
mcimadamore@1297 260 boolean isFinal() {
mcimadamore@1297 261 return isFinal;
mcimadamore@1297 262 }
mcimadamore@1237 263 }
mcimadamore@1237 264
duke@1 265 protected Flow(Context context) {
duke@1 266 context.put(flowKey, this);
jjg@113 267 names = Names.instance(context);
duke@1 268 log = Log.instance(context);
duke@1 269 syms = Symtab.instance(context);
duke@1 270 types = Types.instance(context);
duke@1 271 chk = Check.instance(context);
duke@1 272 lint = Lint.instance(context);
mcimadamore@617 273 rs = Resolve.instance(context);
mcimadamore@1297 274 diags = JCDiagnostic.Factory.instance(context);
mcimadamore@550 275 Source source = Source.instance(context);
mcimadamore@935 276 allowImprovedRethrowAnalysis = source.allowImprovedRethrowAnalysis();
mcimadamore@935 277 allowImprovedCatchAnalysis = source.allowImprovedCatchAnalysis();
mcimadamore@1297 278 Options options = Options.instance(context);
mcimadamore@1297 279 allowEffectivelyFinalInInnerClasses = source.allowEffectivelyFinalInInnerClasses() &&
mcimadamore@1297 280 options.isSet("allowEffectivelyFinalInInnerClasses"); //pre-lambda guard
duke@1 281 }
duke@1 282
mcimadamore@1237 283 /**
mcimadamore@1237 284 * Base visitor class for all visitors implementing dataflow analysis logic.
mcimadamore@1237 285 * This class define the shared logic for handling jumps (break/continue statements).
duke@1 286 */
mcimadamore@1237 287 static abstract class BaseAnalyzer<P extends BaseAnalyzer.PendingExit> extends TreeScanner {
duke@1 288
mcimadamore@1237 289 enum JumpKind {
mcimadamore@1237 290 BREAK(JCTree.Tag.BREAK) {
mcimadamore@1237 291 @Override
mcimadamore@1237 292 JCTree getTarget(JCTree tree) {
mcimadamore@1237 293 return ((JCBreak)tree).target;
mcimadamore@1237 294 }
mcimadamore@1237 295 },
mcimadamore@1237 296 CONTINUE(JCTree.Tag.CONTINUE) {
mcimadamore@1237 297 @Override
mcimadamore@1237 298 JCTree getTarget(JCTree tree) {
mcimadamore@1237 299 return ((JCContinue)tree).target;
mcimadamore@1237 300 }
mcimadamore@1237 301 };
duke@1 302
mcimadamore@1237 303 JCTree.Tag treeTag;
duke@1 304
mcimadamore@1237 305 private JumpKind(Tag treeTag) {
mcimadamore@1237 306 this.treeTag = treeTag;
mcimadamore@1237 307 }
mcimadamore@550 308
mcimadamore@1237 309 abstract JCTree getTarget(JCTree tree);
mcimadamore@1237 310 }
duke@1 311
mcimadamore@1237 312 /** The currently pending exits that go from current inner blocks
mcimadamore@1237 313 * to an enclosing block, in source order.
mcimadamore@1237 314 */
mcimadamore@1237 315 ListBuffer<P> pendingExits;
duke@1 316
mcimadamore@1237 317 /** A pending exit. These are the statements return, break, and
mcimadamore@1237 318 * continue. In addition, exception-throwing expressions or
mcimadamore@1237 319 * statements are put here when not known to be caught. This
mcimadamore@1237 320 * will typically result in an error unless it is within a
mcimadamore@1237 321 * try-finally whose finally block cannot complete normally.
mcimadamore@1237 322 */
mcimadamore@1297 323 static class PendingExit {
mcimadamore@1237 324 JCTree tree;
duke@1 325
mcimadamore@1237 326 PendingExit(JCTree tree) {
mcimadamore@1237 327 this.tree = tree;
mcimadamore@1237 328 }
duke@1 329
mcimadamore@1297 330 void resolveJump() {
mcimadamore@1297 331 //do nothing
mcimadamore@1297 332 }
mcimadamore@1237 333 }
duke@1 334
mcimadamore@1237 335 abstract void markDead();
duke@1 336
mcimadamore@1237 337 /** Record an outward transfer of control. */
mcimadamore@1237 338 void recordExit(JCTree tree, P pe) {
mcimadamore@1237 339 pendingExits.append(pe);
mcimadamore@1237 340 markDead();
mcimadamore@1237 341 }
duke@1 342
mcimadamore@1237 343 /** Resolve all jumps of this statement. */
mcimadamore@1237 344 private boolean resolveJump(JCTree tree,
mcimadamore@1237 345 ListBuffer<P> oldPendingExits,
mcimadamore@1237 346 JumpKind jk) {
mcimadamore@1237 347 boolean resolved = false;
mcimadamore@1237 348 List<P> exits = pendingExits.toList();
mcimadamore@1237 349 pendingExits = oldPendingExits;
mcimadamore@1237 350 for (; exits.nonEmpty(); exits = exits.tail) {
mcimadamore@1237 351 P exit = exits.head;
mcimadamore@1237 352 if (exit.tree.hasTag(jk.treeTag) &&
mcimadamore@1237 353 jk.getTarget(exit.tree) == tree) {
mcimadamore@1237 354 exit.resolveJump();
mcimadamore@1237 355 resolved = true;
mcimadamore@1237 356 } else {
mcimadamore@1237 357 pendingExits.append(exit);
mcimadamore@1237 358 }
mcimadamore@1237 359 }
mcimadamore@1237 360 return resolved;
mcimadamore@1237 361 }
duke@1 362
mcimadamore@1237 363 /** Resolve all breaks of this statement. */
mcimadamore@1237 364 boolean resolveContinues(JCTree tree) {
mcimadamore@1237 365 return resolveJump(tree, new ListBuffer<P>(), JumpKind.CONTINUE);
mcimadamore@1237 366 }
darcy@609 367
mcimadamore@1237 368 /** Resolve all continues of this statement. */
mcimadamore@1237 369 boolean resolveBreaks(JCTree tree, ListBuffer<P> oldPendingExits) {
mcimadamore@1237 370 return resolveJump(tree, oldPendingExits, JumpKind.BREAK);
duke@1 371 }
duke@1 372 }
duke@1 373
mcimadamore@1237 374 /**
mcimadamore@1297 375 * This pass implements the first step of the dataflow analysis, namely
mcimadamore@1297 376 * the liveness analysis check. This checks that every statement is reachable.
mcimadamore@1297 377 * The output of this analysis pass are used by other analyzers. This analyzer
mcimadamore@1297 378 * sets the 'finallyCanCompleteNormally' field in the JCTry class.
mcimadamore@1297 379 */
mcimadamore@1297 380 class AliveAnalyzer extends BaseAnalyzer<BaseAnalyzer.PendingExit> {
mcimadamore@1297 381
mcimadamore@1297 382 /** A flag that indicates whether the last statement could
mcimadamore@1297 383 * complete normally.
mcimadamore@1297 384 */
mcimadamore@1297 385 private boolean alive;
mcimadamore@1297 386
mcimadamore@1297 387 @Override
mcimadamore@1297 388 void markDead() {
mcimadamore@1297 389 alive = false;
mcimadamore@1297 390 }
mcimadamore@1297 391
mcimadamore@1297 392 /*************************************************************************
mcimadamore@1297 393 * Visitor methods for statements and definitions
mcimadamore@1297 394 *************************************************************************/
mcimadamore@1297 395
mcimadamore@1297 396 /** Analyze a definition.
mcimadamore@1297 397 */
mcimadamore@1297 398 void scanDef(JCTree tree) {
mcimadamore@1297 399 scanStat(tree);
mcimadamore@1297 400 if (tree != null && tree.hasTag(JCTree.Tag.BLOCK) && !alive) {
mcimadamore@1297 401 log.error(tree.pos(),
mcimadamore@1297 402 "initializer.must.be.able.to.complete.normally");
mcimadamore@1297 403 }
mcimadamore@1297 404 }
mcimadamore@1297 405
mcimadamore@1297 406 /** Analyze a statement. Check that statement is reachable.
mcimadamore@1297 407 */
mcimadamore@1297 408 void scanStat(JCTree tree) {
mcimadamore@1297 409 if (!alive && tree != null) {
mcimadamore@1297 410 log.error(tree.pos(), "unreachable.stmt");
mcimadamore@1297 411 if (!tree.hasTag(SKIP)) alive = true;
mcimadamore@1297 412 }
mcimadamore@1297 413 scan(tree);
mcimadamore@1297 414 }
mcimadamore@1297 415
mcimadamore@1297 416 /** Analyze list of statements.
mcimadamore@1297 417 */
mcimadamore@1297 418 void scanStats(List<? extends JCStatement> trees) {
mcimadamore@1297 419 if (trees != null)
mcimadamore@1297 420 for (List<? extends JCStatement> l = trees; l.nonEmpty(); l = l.tail)
mcimadamore@1297 421 scanStat(l.head);
mcimadamore@1297 422 }
mcimadamore@1297 423
mcimadamore@1297 424 /* ------------ Visitor methods for various sorts of trees -------------*/
mcimadamore@1297 425
mcimadamore@1297 426 public void visitClassDef(JCClassDecl tree) {
mcimadamore@1297 427 if (tree.sym == null) return;
mcimadamore@1297 428 boolean alivePrev = alive;
mcimadamore@1297 429 ListBuffer<PendingExit> pendingExitsPrev = pendingExits;
mcimadamore@1297 430 Lint lintPrev = lint;
mcimadamore@1297 431
mcimadamore@1297 432 pendingExits = new ListBuffer<PendingExit>();
jfranck@1313 433 lint = lint.augment(tree.sym.annotations);
mcimadamore@1297 434
mcimadamore@1297 435 try {
mcimadamore@1297 436 // process all the static initializers
mcimadamore@1297 437 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1297 438 if (!l.head.hasTag(METHODDEF) &&
mcimadamore@1297 439 (TreeInfo.flags(l.head) & STATIC) != 0) {
mcimadamore@1297 440 scanDef(l.head);
mcimadamore@1297 441 }
mcimadamore@1297 442 }
mcimadamore@1297 443
mcimadamore@1297 444 // process all the instance initializers
mcimadamore@1297 445 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1297 446 if (!l.head.hasTag(METHODDEF) &&
mcimadamore@1297 447 (TreeInfo.flags(l.head) & STATIC) == 0) {
mcimadamore@1297 448 scanDef(l.head);
mcimadamore@1297 449 }
mcimadamore@1297 450 }
mcimadamore@1297 451
mcimadamore@1297 452 // process all the methods
mcimadamore@1297 453 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1297 454 if (l.head.hasTag(METHODDEF)) {
mcimadamore@1297 455 scan(l.head);
mcimadamore@1297 456 }
mcimadamore@1297 457 }
mcimadamore@1297 458 } finally {
mcimadamore@1297 459 pendingExits = pendingExitsPrev;
mcimadamore@1297 460 alive = alivePrev;
mcimadamore@1297 461 lint = lintPrev;
mcimadamore@1297 462 }
mcimadamore@1297 463 }
mcimadamore@1297 464
mcimadamore@1297 465 public void visitMethodDef(JCMethodDecl tree) {
mcimadamore@1297 466 if (tree.body == null) return;
mcimadamore@1297 467 Lint lintPrev = lint;
mcimadamore@1297 468
jfranck@1313 469 lint = lint.augment(tree.sym.annotations);
mcimadamore@1297 470
mcimadamore@1297 471 Assert.check(pendingExits.isEmpty());
mcimadamore@1297 472
mcimadamore@1297 473 try {
mcimadamore@1297 474 alive = true;
mcimadamore@1297 475 scanStat(tree.body);
mcimadamore@1297 476
jjg@1374 477 if (alive && !tree.sym.type.getReturnType().hasTag(VOID))
mcimadamore@1297 478 log.error(TreeInfo.diagEndPos(tree.body), "missing.ret.stmt");
mcimadamore@1297 479
mcimadamore@1297 480 List<PendingExit> exits = pendingExits.toList();
mcimadamore@1297 481 pendingExits = new ListBuffer<PendingExit>();
mcimadamore@1297 482 while (exits.nonEmpty()) {
mcimadamore@1297 483 PendingExit exit = exits.head;
mcimadamore@1297 484 exits = exits.tail;
mcimadamore@1297 485 Assert.check(exit.tree.hasTag(RETURN));
mcimadamore@1297 486 }
mcimadamore@1297 487 } finally {
mcimadamore@1297 488 lint = lintPrev;
mcimadamore@1297 489 }
mcimadamore@1297 490 }
mcimadamore@1297 491
mcimadamore@1297 492 public void visitVarDef(JCVariableDecl tree) {
mcimadamore@1297 493 if (tree.init != null) {
mcimadamore@1297 494 Lint lintPrev = lint;
jfranck@1313 495 lint = lint.augment(tree.sym.annotations);
mcimadamore@1297 496 try{
mcimadamore@1297 497 scan(tree.init);
mcimadamore@1297 498 } finally {
mcimadamore@1297 499 lint = lintPrev;
mcimadamore@1297 500 }
mcimadamore@1297 501 }
mcimadamore@1297 502 }
mcimadamore@1297 503
mcimadamore@1297 504 public void visitBlock(JCBlock tree) {
mcimadamore@1297 505 scanStats(tree.stats);
mcimadamore@1297 506 }
mcimadamore@1297 507
mcimadamore@1297 508 public void visitDoLoop(JCDoWhileLoop tree) {
mcimadamore@1297 509 ListBuffer<PendingExit> prevPendingExits = pendingExits;
mcimadamore@1297 510 pendingExits = new ListBuffer<PendingExit>();
mcimadamore@1297 511 scanStat(tree.body);
mcimadamore@1297 512 alive |= resolveContinues(tree);
mcimadamore@1297 513 scan(tree.cond);
mcimadamore@1297 514 alive = alive && !tree.cond.type.isTrue();
mcimadamore@1297 515 alive |= resolveBreaks(tree, prevPendingExits);
mcimadamore@1297 516 }
mcimadamore@1297 517
mcimadamore@1297 518 public void visitWhileLoop(JCWhileLoop tree) {
mcimadamore@1297 519 ListBuffer<PendingExit> prevPendingExits = pendingExits;
mcimadamore@1297 520 pendingExits = new ListBuffer<PendingExit>();
mcimadamore@1297 521 scan(tree.cond);
mcimadamore@1297 522 alive = !tree.cond.type.isFalse();
mcimadamore@1297 523 scanStat(tree.body);
mcimadamore@1297 524 alive |= resolveContinues(tree);
mcimadamore@1297 525 alive = resolveBreaks(tree, prevPendingExits) ||
mcimadamore@1297 526 !tree.cond.type.isTrue();
mcimadamore@1297 527 }
mcimadamore@1297 528
mcimadamore@1297 529 public void visitForLoop(JCForLoop tree) {
mcimadamore@1297 530 ListBuffer<PendingExit> prevPendingExits = pendingExits;
mcimadamore@1297 531 scanStats(tree.init);
mcimadamore@1297 532 pendingExits = new ListBuffer<PendingExit>();
mcimadamore@1297 533 if (tree.cond != null) {
mcimadamore@1297 534 scan(tree.cond);
mcimadamore@1297 535 alive = !tree.cond.type.isFalse();
mcimadamore@1297 536 } else {
mcimadamore@1297 537 alive = true;
mcimadamore@1297 538 }
mcimadamore@1297 539 scanStat(tree.body);
mcimadamore@1297 540 alive |= resolveContinues(tree);
mcimadamore@1297 541 scan(tree.step);
mcimadamore@1297 542 alive = resolveBreaks(tree, prevPendingExits) ||
mcimadamore@1297 543 tree.cond != null && !tree.cond.type.isTrue();
mcimadamore@1297 544 }
mcimadamore@1297 545
mcimadamore@1297 546 public void visitForeachLoop(JCEnhancedForLoop tree) {
mcimadamore@1297 547 visitVarDef(tree.var);
mcimadamore@1297 548 ListBuffer<PendingExit> prevPendingExits = pendingExits;
mcimadamore@1297 549 scan(tree.expr);
mcimadamore@1297 550 pendingExits = new ListBuffer<PendingExit>();
mcimadamore@1297 551 scanStat(tree.body);
mcimadamore@1297 552 alive |= resolveContinues(tree);
mcimadamore@1297 553 resolveBreaks(tree, prevPendingExits);
mcimadamore@1297 554 alive = true;
mcimadamore@1297 555 }
mcimadamore@1297 556
mcimadamore@1297 557 public void visitLabelled(JCLabeledStatement tree) {
mcimadamore@1297 558 ListBuffer<PendingExit> prevPendingExits = pendingExits;
mcimadamore@1297 559 pendingExits = new ListBuffer<PendingExit>();
mcimadamore@1297 560 scanStat(tree.body);
mcimadamore@1297 561 alive |= resolveBreaks(tree, prevPendingExits);
mcimadamore@1297 562 }
mcimadamore@1297 563
mcimadamore@1297 564 public void visitSwitch(JCSwitch tree) {
mcimadamore@1297 565 ListBuffer<PendingExit> prevPendingExits = pendingExits;
mcimadamore@1297 566 pendingExits = new ListBuffer<PendingExit>();
mcimadamore@1297 567 scan(tree.selector);
mcimadamore@1297 568 boolean hasDefault = false;
mcimadamore@1297 569 for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
mcimadamore@1297 570 alive = true;
mcimadamore@1297 571 JCCase c = l.head;
mcimadamore@1297 572 if (c.pat == null)
mcimadamore@1297 573 hasDefault = true;
mcimadamore@1297 574 else
mcimadamore@1297 575 scan(c.pat);
mcimadamore@1297 576 scanStats(c.stats);
mcimadamore@1297 577 // Warn about fall-through if lint switch fallthrough enabled.
mcimadamore@1297 578 if (alive &&
mcimadamore@1297 579 lint.isEnabled(Lint.LintCategory.FALLTHROUGH) &&
mcimadamore@1297 580 c.stats.nonEmpty() && l.tail.nonEmpty())
mcimadamore@1297 581 log.warning(Lint.LintCategory.FALLTHROUGH,
mcimadamore@1297 582 l.tail.head.pos(),
mcimadamore@1297 583 "possible.fall-through.into.case");
mcimadamore@1297 584 }
mcimadamore@1297 585 if (!hasDefault) {
mcimadamore@1297 586 alive = true;
mcimadamore@1297 587 }
mcimadamore@1297 588 alive |= resolveBreaks(tree, prevPendingExits);
mcimadamore@1297 589 }
mcimadamore@1297 590
mcimadamore@1297 591 public void visitTry(JCTry tree) {
mcimadamore@1297 592 ListBuffer<PendingExit> prevPendingExits = pendingExits;
mcimadamore@1297 593 pendingExits = new ListBuffer<PendingExit>();
mcimadamore@1297 594 for (JCTree resource : tree.resources) {
mcimadamore@1297 595 if (resource instanceof JCVariableDecl) {
mcimadamore@1297 596 JCVariableDecl vdecl = (JCVariableDecl) resource;
mcimadamore@1297 597 visitVarDef(vdecl);
mcimadamore@1297 598 } else if (resource instanceof JCExpression) {
mcimadamore@1297 599 scan((JCExpression) resource);
mcimadamore@1297 600 } else {
mcimadamore@1297 601 throw new AssertionError(tree); // parser error
mcimadamore@1297 602 }
mcimadamore@1297 603 }
mcimadamore@1297 604
mcimadamore@1297 605 scanStat(tree.body);
mcimadamore@1297 606 boolean aliveEnd = alive;
mcimadamore@1297 607
mcimadamore@1297 608 for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
mcimadamore@1297 609 alive = true;
mcimadamore@1297 610 JCVariableDecl param = l.head.param;
mcimadamore@1297 611 scan(param);
mcimadamore@1297 612 scanStat(l.head.body);
mcimadamore@1297 613 aliveEnd |= alive;
mcimadamore@1297 614 }
mcimadamore@1297 615 if (tree.finalizer != null) {
mcimadamore@1297 616 ListBuffer<PendingExit> exits = pendingExits;
mcimadamore@1297 617 pendingExits = prevPendingExits;
mcimadamore@1297 618 alive = true;
mcimadamore@1297 619 scanStat(tree.finalizer);
mcimadamore@1297 620 tree.finallyCanCompleteNormally = alive;
mcimadamore@1297 621 if (!alive) {
mcimadamore@1297 622 if (lint.isEnabled(Lint.LintCategory.FINALLY)) {
mcimadamore@1297 623 log.warning(Lint.LintCategory.FINALLY,
mcimadamore@1297 624 TreeInfo.diagEndPos(tree.finalizer),
mcimadamore@1297 625 "finally.cannot.complete");
mcimadamore@1297 626 }
mcimadamore@1297 627 } else {
mcimadamore@1297 628 while (exits.nonEmpty()) {
mcimadamore@1297 629 pendingExits.append(exits.next());
mcimadamore@1297 630 }
mcimadamore@1297 631 alive = aliveEnd;
mcimadamore@1297 632 }
mcimadamore@1297 633 } else {
mcimadamore@1297 634 alive = aliveEnd;
mcimadamore@1297 635 ListBuffer<PendingExit> exits = pendingExits;
mcimadamore@1297 636 pendingExits = prevPendingExits;
mcimadamore@1297 637 while (exits.nonEmpty()) pendingExits.append(exits.next());
mcimadamore@1297 638 }
mcimadamore@1297 639 }
mcimadamore@1297 640
mcimadamore@1297 641 @Override
mcimadamore@1297 642 public void visitIf(JCIf tree) {
mcimadamore@1297 643 scan(tree.cond);
mcimadamore@1297 644 scanStat(tree.thenpart);
mcimadamore@1297 645 if (tree.elsepart != null) {
mcimadamore@1297 646 boolean aliveAfterThen = alive;
mcimadamore@1297 647 alive = true;
mcimadamore@1297 648 scanStat(tree.elsepart);
mcimadamore@1297 649 alive = alive | aliveAfterThen;
mcimadamore@1297 650 } else {
mcimadamore@1297 651 alive = true;
mcimadamore@1297 652 }
mcimadamore@1297 653 }
mcimadamore@1297 654
mcimadamore@1297 655 public void visitBreak(JCBreak tree) {
mcimadamore@1297 656 recordExit(tree, new PendingExit(tree));
mcimadamore@1297 657 }
mcimadamore@1297 658
mcimadamore@1297 659 public void visitContinue(JCContinue tree) {
mcimadamore@1297 660 recordExit(tree, new PendingExit(tree));
mcimadamore@1297 661 }
mcimadamore@1297 662
mcimadamore@1297 663 public void visitReturn(JCReturn tree) {
mcimadamore@1297 664 scan(tree.expr);
mcimadamore@1297 665 recordExit(tree, new PendingExit(tree));
mcimadamore@1297 666 }
mcimadamore@1297 667
mcimadamore@1297 668 public void visitThrow(JCThrow tree) {
mcimadamore@1297 669 scan(tree.expr);
mcimadamore@1297 670 markDead();
mcimadamore@1297 671 }
mcimadamore@1297 672
mcimadamore@1297 673 public void visitApply(JCMethodInvocation tree) {
mcimadamore@1297 674 scan(tree.meth);
mcimadamore@1297 675 scan(tree.args);
mcimadamore@1297 676 }
mcimadamore@1297 677
mcimadamore@1297 678 public void visitNewClass(JCNewClass tree) {
mcimadamore@1297 679 scan(tree.encl);
mcimadamore@1297 680 scan(tree.args);
mcimadamore@1297 681 if (tree.def != null) {
mcimadamore@1297 682 scan(tree.def);
mcimadamore@1297 683 }
mcimadamore@1297 684 }
mcimadamore@1297 685
mcimadamore@1348 686 @Override
mcimadamore@1348 687 public void visitLambda(JCLambda tree) {
mcimadamore@1348 688 if (tree.type != null &&
mcimadamore@1348 689 tree.type.isErroneous()) {
mcimadamore@1348 690 return;
mcimadamore@1348 691 }
mcimadamore@1348 692
mcimadamore@1348 693 ListBuffer<PendingExit> prevPending = pendingExits;
mcimadamore@1348 694 boolean prevAlive = alive;
mcimadamore@1348 695 try {
mcimadamore@1348 696 pendingExits = ListBuffer.lb();
mcimadamore@1348 697 alive = true;
mcimadamore@1348 698 scanStat(tree.body);
mcimadamore@1348 699 tree.canCompleteNormally = alive;
mcimadamore@1348 700 }
mcimadamore@1348 701 finally {
mcimadamore@1348 702 pendingExits = prevPending;
mcimadamore@1348 703 alive = prevAlive;
mcimadamore@1348 704 }
mcimadamore@1348 705 }
mcimadamore@1348 706
mcimadamore@1297 707 public void visitTopLevel(JCCompilationUnit tree) {
mcimadamore@1297 708 // Do nothing for TopLevel since each class is visited individually
mcimadamore@1297 709 }
mcimadamore@1297 710
mcimadamore@1297 711 /**************************************************************************
mcimadamore@1297 712 * main method
mcimadamore@1297 713 *************************************************************************/
mcimadamore@1297 714
mcimadamore@1297 715 /** Perform definite assignment/unassignment analysis on a tree.
mcimadamore@1297 716 */
mcimadamore@1297 717 public void analyzeTree(Env<AttrContext> env, TreeMaker make) {
mcimadamore@1348 718 analyzeTree(env, env.tree, make);
mcimadamore@1348 719 }
mcimadamore@1348 720 public void analyzeTree(Env<AttrContext> env, JCTree tree, TreeMaker make) {
mcimadamore@1297 721 try {
mcimadamore@1297 722 attrEnv = env;
mcimadamore@1297 723 Flow.this.make = make;
mcimadamore@1297 724 pendingExits = new ListBuffer<PendingExit>();
mcimadamore@1297 725 alive = true;
mcimadamore@1297 726 scan(env.tree);
mcimadamore@1297 727 } finally {
mcimadamore@1297 728 pendingExits = null;
mcimadamore@1297 729 Flow.this.make = null;
mcimadamore@1297 730 }
mcimadamore@1297 731 }
mcimadamore@1297 732 }
mcimadamore@1297 733
mcimadamore@1297 734 /**
mcimadamore@1297 735 * This pass implements the second step of the dataflow analysis, namely
mcimadamore@1297 736 * the exception analysis. This is to ensure that every checked exception that is
mcimadamore@1297 737 * thrown is declared or caught. The analyzer uses some info that has been set by
mcimadamore@1297 738 * the liveliness analyzer.
duke@1 739 */
mcimadamore@1237 740 class FlowAnalyzer extends BaseAnalyzer<FlowAnalyzer.FlowPendingExit> {
duke@1 741
mcimadamore@1237 742 /** A flag that indicates whether the last statement could
mcimadamore@1237 743 * complete normally.
mcimadamore@1237 744 */
mcimadamore@1237 745 HashMap<Symbol, List<Type>> preciseRethrowTypes;
mcimadamore@1237 746
mcimadamore@1237 747 /** The current class being defined.
mcimadamore@1237 748 */
mcimadamore@1237 749 JCClassDecl classDef;
mcimadamore@1237 750
mcimadamore@1237 751 /** The list of possibly thrown declarable exceptions.
mcimadamore@1237 752 */
mcimadamore@1237 753 List<Type> thrown;
mcimadamore@1237 754
mcimadamore@1237 755 /** The list of exceptions that are either caught or declared to be
mcimadamore@1237 756 * thrown.
mcimadamore@1237 757 */
mcimadamore@1237 758 List<Type> caught;
mcimadamore@1237 759
mcimadamore@1237 760 class FlowPendingExit extends BaseAnalyzer.PendingExit {
mcimadamore@1237 761
mcimadamore@1237 762 Type thrown;
mcimadamore@1237 763
mcimadamore@1237 764 FlowPendingExit(JCTree tree, Type thrown) {
mcimadamore@1237 765 super(tree);
mcimadamore@1237 766 this.thrown = thrown;
mcimadamore@1237 767 }
mcimadamore@1237 768 }
mcimadamore@1237 769
mcimadamore@1237 770 @Override
mcimadamore@1237 771 void markDead() {
mcimadamore@1297 772 //do nothing
mcimadamore@1237 773 }
mcimadamore@1237 774
mcimadamore@1237 775 /*-------------------- Exceptions ----------------------*/
mcimadamore@1237 776
mcimadamore@1237 777 /** Complain that pending exceptions are not caught.
mcimadamore@1237 778 */
mcimadamore@1237 779 void errorUncaught() {
mcimadamore@1237 780 for (FlowPendingExit exit = pendingExits.next();
mcimadamore@1237 781 exit != null;
mcimadamore@1237 782 exit = pendingExits.next()) {
mcimadamore@1237 783 if (classDef != null &&
mcimadamore@1237 784 classDef.pos == exit.tree.pos) {
mcimadamore@1237 785 log.error(exit.tree.pos(),
mcimadamore@1237 786 "unreported.exception.default.constructor",
mcimadamore@1237 787 exit.thrown);
mcimadamore@1237 788 } else if (exit.tree.hasTag(VARDEF) &&
mcimadamore@1237 789 ((JCVariableDecl)exit.tree).sym.isResourceVariable()) {
mcimadamore@1237 790 log.error(exit.tree.pos(),
mcimadamore@1237 791 "unreported.exception.implicit.close",
mcimadamore@1237 792 exit.thrown,
mcimadamore@1237 793 ((JCVariableDecl)exit.tree).sym.name);
mcimadamore@1237 794 } else {
mcimadamore@1237 795 log.error(exit.tree.pos(),
mcimadamore@1237 796 "unreported.exception.need.to.catch.or.throw",
mcimadamore@1237 797 exit.thrown);
mcimadamore@1237 798 }
mcimadamore@1237 799 }
mcimadamore@1237 800 }
mcimadamore@1237 801
mcimadamore@1237 802 /** Record that exception is potentially thrown and check that it
mcimadamore@1237 803 * is caught.
mcimadamore@1237 804 */
mcimadamore@1237 805 void markThrown(JCTree tree, Type exc) {
mcimadamore@1237 806 if (!chk.isUnchecked(tree.pos(), exc)) {
mcimadamore@1237 807 if (!chk.isHandled(exc, caught))
mcimadamore@1237 808 pendingExits.append(new FlowPendingExit(tree, exc));
mcimadamore@1237 809 thrown = chk.incl(exc, thrown);
mcimadamore@1237 810 }
mcimadamore@1237 811 }
mcimadamore@1237 812
mcimadamore@1237 813 /*************************************************************************
mcimadamore@1237 814 * Visitor methods for statements and definitions
mcimadamore@1237 815 *************************************************************************/
mcimadamore@1237 816
mcimadamore@1237 817 /* ------------ Visitor methods for various sorts of trees -------------*/
mcimadamore@1237 818
mcimadamore@1237 819 public void visitClassDef(JCClassDecl tree) {
mcimadamore@1237 820 if (tree.sym == null) return;
mcimadamore@1237 821
mcimadamore@1237 822 JCClassDecl classDefPrev = classDef;
mcimadamore@1237 823 List<Type> thrownPrev = thrown;
mcimadamore@1237 824 List<Type> caughtPrev = caught;
mcimadamore@1237 825 ListBuffer<FlowPendingExit> pendingExitsPrev = pendingExits;
mcimadamore@1237 826 Lint lintPrev = lint;
mcimadamore@1237 827
mcimadamore@1237 828 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 829 if (tree.name != names.empty) {
mcimadamore@1237 830 caught = List.nil();
mcimadamore@1237 831 }
mcimadamore@1237 832 classDef = tree;
mcimadamore@1237 833 thrown = List.nil();
jfranck@1313 834 lint = lint.augment(tree.sym.annotations);
mcimadamore@1237 835
mcimadamore@1237 836 try {
mcimadamore@1237 837 // process all the static initializers
mcimadamore@1237 838 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 839 if (!l.head.hasTag(METHODDEF) &&
mcimadamore@1237 840 (TreeInfo.flags(l.head) & STATIC) != 0) {
mcimadamore@1297 841 scan(l.head);
mcimadamore@1237 842 errorUncaught();
mcimadamore@1237 843 }
mcimadamore@1237 844 }
mcimadamore@1237 845
mcimadamore@1237 846 // add intersection of all thrown clauses of initial constructors
mcimadamore@1237 847 // to set of caught exceptions, unless class is anonymous.
mcimadamore@1237 848 if (tree.name != names.empty) {
mcimadamore@1237 849 boolean firstConstructor = true;
mcimadamore@1237 850 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 851 if (TreeInfo.isInitialConstructor(l.head)) {
mcimadamore@1237 852 List<Type> mthrown =
mcimadamore@1237 853 ((JCMethodDecl) l.head).sym.type.getThrownTypes();
mcimadamore@1237 854 if (firstConstructor) {
mcimadamore@1237 855 caught = mthrown;
mcimadamore@1237 856 firstConstructor = false;
mcimadamore@1237 857 } else {
mcimadamore@1237 858 caught = chk.intersect(mthrown, caught);
mcimadamore@1237 859 }
mcimadamore@1237 860 }
mcimadamore@1237 861 }
mcimadamore@1237 862 }
mcimadamore@1237 863
mcimadamore@1237 864 // process all the instance initializers
mcimadamore@1237 865 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 866 if (!l.head.hasTag(METHODDEF) &&
mcimadamore@1237 867 (TreeInfo.flags(l.head) & STATIC) == 0) {
mcimadamore@1297 868 scan(l.head);
mcimadamore@1237 869 errorUncaught();
mcimadamore@1237 870 }
mcimadamore@1237 871 }
mcimadamore@1237 872
mcimadamore@1237 873 // in an anonymous class, add the set of thrown exceptions to
mcimadamore@1237 874 // the throws clause of the synthetic constructor and propagate
mcimadamore@1237 875 // outwards.
mcimadamore@1237 876 // Changing the throws clause on the fly is okay here because
mcimadamore@1237 877 // the anonymous constructor can't be invoked anywhere else,
mcimadamore@1237 878 // and its type hasn't been cached.
mcimadamore@1237 879 if (tree.name == names.empty) {
mcimadamore@1237 880 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 881 if (TreeInfo.isInitialConstructor(l.head)) {
mcimadamore@1237 882 JCMethodDecl mdef = (JCMethodDecl)l.head;
mcimadamore@1237 883 mdef.thrown = make.Types(thrown);
mcimadamore@1237 884 mdef.sym.type = types.createMethodTypeWithThrown(mdef.sym.type, thrown);
mcimadamore@1237 885 }
mcimadamore@1237 886 }
mcimadamore@1237 887 thrownPrev = chk.union(thrown, thrownPrev);
mcimadamore@1237 888 }
mcimadamore@1237 889
mcimadamore@1237 890 // process all the methods
mcimadamore@1237 891 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 892 if (l.head.hasTag(METHODDEF)) {
mcimadamore@1237 893 scan(l.head);
mcimadamore@1237 894 errorUncaught();
mcimadamore@1237 895 }
mcimadamore@1237 896 }
mcimadamore@1237 897
mcimadamore@1237 898 thrown = thrownPrev;
mcimadamore@1237 899 } finally {
mcimadamore@1237 900 pendingExits = pendingExitsPrev;
mcimadamore@1237 901 caught = caughtPrev;
mcimadamore@1237 902 classDef = classDefPrev;
mcimadamore@1237 903 lint = lintPrev;
mcimadamore@1237 904 }
mcimadamore@1237 905 }
mcimadamore@1237 906
mcimadamore@1237 907 public void visitMethodDef(JCMethodDecl tree) {
mcimadamore@1237 908 if (tree.body == null) return;
mcimadamore@1237 909
mcimadamore@1237 910 List<Type> caughtPrev = caught;
mcimadamore@1237 911 List<Type> mthrown = tree.sym.type.getThrownTypes();
mcimadamore@1237 912 Lint lintPrev = lint;
mcimadamore@1237 913
jfranck@1313 914 lint = lint.augment(tree.sym.annotations);
mcimadamore@1237 915
mcimadamore@1237 916 Assert.check(pendingExits.isEmpty());
mcimadamore@1237 917
mcimadamore@1237 918 try {
mcimadamore@1237 919 for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 920 JCVariableDecl def = l.head;
mcimadamore@1237 921 scan(def);
mcimadamore@1237 922 }
mcimadamore@1237 923 if (TreeInfo.isInitialConstructor(tree))
mcimadamore@1237 924 caught = chk.union(caught, mthrown);
mcimadamore@1237 925 else if ((tree.sym.flags() & (BLOCK | STATIC)) != BLOCK)
mcimadamore@1237 926 caught = mthrown;
mcimadamore@1237 927 // else we are in an instance initializer block;
mcimadamore@1237 928 // leave caught unchanged.
mcimadamore@1237 929
mcimadamore@1297 930 scan(tree.body);
mcimadamore@1237 931
mcimadamore@1237 932 List<FlowPendingExit> exits = pendingExits.toList();
mcimadamore@1237 933 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 934 while (exits.nonEmpty()) {
mcimadamore@1237 935 FlowPendingExit exit = exits.head;
mcimadamore@1237 936 exits = exits.tail;
mcimadamore@1237 937 if (exit.thrown == null) {
mcimadamore@1237 938 Assert.check(exit.tree.hasTag(RETURN));
mcimadamore@1237 939 } else {
mcimadamore@1237 940 // uncaught throws will be reported later
mcimadamore@1237 941 pendingExits.append(exit);
mcimadamore@1237 942 }
mcimadamore@1237 943 }
mcimadamore@1237 944 } finally {
mcimadamore@1237 945 caught = caughtPrev;
mcimadamore@1237 946 lint = lintPrev;
mcimadamore@1237 947 }
mcimadamore@1237 948 }
mcimadamore@1237 949
mcimadamore@1237 950 public void visitVarDef(JCVariableDecl tree) {
mcimadamore@1237 951 if (tree.init != null) {
mcimadamore@1237 952 Lint lintPrev = lint;
jfranck@1313 953 lint = lint.augment(tree.sym.annotations);
mcimadamore@1237 954 try{
mcimadamore@1237 955 scan(tree.init);
mcimadamore@1237 956 } finally {
mcimadamore@1237 957 lint = lintPrev;
mcimadamore@1237 958 }
mcimadamore@1237 959 }
mcimadamore@1237 960 }
mcimadamore@1237 961
mcimadamore@1237 962 public void visitBlock(JCBlock tree) {
mcimadamore@1297 963 scan(tree.stats);
mcimadamore@1237 964 }
mcimadamore@1237 965
mcimadamore@1237 966 public void visitDoLoop(JCDoWhileLoop tree) {
mcimadamore@1237 967 ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 968 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1297 969 scan(tree.body);
mcimadamore@1297 970 resolveContinues(tree);
mcimadamore@1237 971 scan(tree.cond);
mcimadamore@1297 972 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 973 }
mcimadamore@1237 974
mcimadamore@1237 975 public void visitWhileLoop(JCWhileLoop tree) {
mcimadamore@1237 976 ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 977 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 978 scan(tree.cond);
mcimadamore@1297 979 scan(tree.body);
mcimadamore@1297 980 resolveContinues(tree);
mcimadamore@1297 981 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 982 }
mcimadamore@1237 983
mcimadamore@1237 984 public void visitForLoop(JCForLoop tree) {
mcimadamore@1237 985 ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
mcimadamore@1297 986 scan(tree.init);
mcimadamore@1237 987 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 988 if (tree.cond != null) {
mcimadamore@1237 989 scan(tree.cond);
mcimadamore@1237 990 }
mcimadamore@1297 991 scan(tree.body);
mcimadamore@1297 992 resolveContinues(tree);
mcimadamore@1237 993 scan(tree.step);
mcimadamore@1297 994 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 995 }
mcimadamore@1237 996
mcimadamore@1237 997 public void visitForeachLoop(JCEnhancedForLoop tree) {
mcimadamore@1237 998 visitVarDef(tree.var);
mcimadamore@1237 999 ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 1000 scan(tree.expr);
mcimadamore@1237 1001 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1297 1002 scan(tree.body);
mcimadamore@1297 1003 resolveContinues(tree);
mcimadamore@1237 1004 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 1005 }
mcimadamore@1237 1006
mcimadamore@1237 1007 public void visitLabelled(JCLabeledStatement tree) {
mcimadamore@1237 1008 ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 1009 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1297 1010 scan(tree.body);
mcimadamore@1297 1011 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 1012 }
mcimadamore@1237 1013
mcimadamore@1237 1014 public void visitSwitch(JCSwitch tree) {
mcimadamore@1237 1015 ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 1016 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 1017 scan(tree.selector);
mcimadamore@1237 1018 for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1019 JCCase c = l.head;
mcimadamore@1297 1020 if (c.pat != null) {
mcimadamore@1237 1021 scan(c.pat);
mcimadamore@1297 1022 }
mcimadamore@1297 1023 scan(c.stats);
mcimadamore@1237 1024 }
mcimadamore@1297 1025 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 1026 }
mcimadamore@1237 1027
mcimadamore@1237 1028 public void visitTry(JCTry tree) {
mcimadamore@1237 1029 List<Type> caughtPrev = caught;
mcimadamore@1237 1030 List<Type> thrownPrev = thrown;
mcimadamore@1237 1031 thrown = List.nil();
mcimadamore@1237 1032 for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1033 List<JCExpression> subClauses = TreeInfo.isMultiCatch(l.head) ?
mcimadamore@1237 1034 ((JCTypeUnion)l.head.param.vartype).alternatives :
mcimadamore@1237 1035 List.of(l.head.param.vartype);
mcimadamore@1237 1036 for (JCExpression ct : subClauses) {
mcimadamore@1237 1037 caught = chk.incl(ct.type, caught);
mcimadamore@1237 1038 }
mcimadamore@1237 1039 }
mcimadamore@1237 1040
mcimadamore@1237 1041 ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 1042 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 1043 for (JCTree resource : tree.resources) {
mcimadamore@1237 1044 if (resource instanceof JCVariableDecl) {
mcimadamore@1237 1045 JCVariableDecl vdecl = (JCVariableDecl) resource;
mcimadamore@1237 1046 visitVarDef(vdecl);
mcimadamore@1237 1047 } else if (resource instanceof JCExpression) {
mcimadamore@1237 1048 scan((JCExpression) resource);
mcimadamore@1237 1049 } else {
mcimadamore@1237 1050 throw new AssertionError(tree); // parser error
mcimadamore@1237 1051 }
mcimadamore@1237 1052 }
mcimadamore@1237 1053 for (JCTree resource : tree.resources) {
mcimadamore@1237 1054 List<Type> closeableSupertypes = resource.type.isCompound() ?
mcimadamore@1237 1055 types.interfaces(resource.type).prepend(types.supertype(resource.type)) :
mcimadamore@1237 1056 List.of(resource.type);
mcimadamore@1237 1057 for (Type sup : closeableSupertypes) {
mcimadamore@1237 1058 if (types.asSuper(sup, syms.autoCloseableType.tsym) != null) {
mcimadamore@1237 1059 Symbol closeMethod = rs.resolveQualifiedMethod(tree,
mcimadamore@1237 1060 attrEnv,
mcimadamore@1237 1061 sup,
mcimadamore@1237 1062 names.close,
mcimadamore@1237 1063 List.<Type>nil(),
mcimadamore@1237 1064 List.<Type>nil());
mcimadamore@1237 1065 if (closeMethod.kind == MTH) {
mcimadamore@1237 1066 for (Type t : ((MethodSymbol)closeMethod).getThrownTypes()) {
mcimadamore@1237 1067 markThrown(resource, t);
mcimadamore@1237 1068 }
mcimadamore@1237 1069 }
mcimadamore@1237 1070 }
mcimadamore@1237 1071 }
mcimadamore@1237 1072 }
mcimadamore@1297 1073 scan(tree.body);
mcimadamore@1237 1074 List<Type> thrownInTry = allowImprovedCatchAnalysis ?
mcimadamore@1237 1075 chk.union(thrown, List.of(syms.runtimeExceptionType, syms.errorType)) :
mcimadamore@1237 1076 thrown;
mcimadamore@1237 1077 thrown = thrownPrev;
mcimadamore@1237 1078 caught = caughtPrev;
mcimadamore@1237 1079
mcimadamore@1237 1080 List<Type> caughtInTry = List.nil();
mcimadamore@1237 1081 for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1082 JCVariableDecl param = l.head.param;
mcimadamore@1237 1083 List<JCExpression> subClauses = TreeInfo.isMultiCatch(l.head) ?
mcimadamore@1237 1084 ((JCTypeUnion)l.head.param.vartype).alternatives :
mcimadamore@1237 1085 List.of(l.head.param.vartype);
mcimadamore@1237 1086 List<Type> ctypes = List.nil();
mcimadamore@1237 1087 List<Type> rethrownTypes = chk.diff(thrownInTry, caughtInTry);
mcimadamore@1237 1088 for (JCExpression ct : subClauses) {
mcimadamore@1237 1089 Type exc = ct.type;
mcimadamore@1237 1090 if (exc != syms.unknownType) {
mcimadamore@1237 1091 ctypes = ctypes.append(exc);
mcimadamore@1237 1092 if (types.isSameType(exc, syms.objectType))
mcimadamore@1237 1093 continue;
mcimadamore@1237 1094 checkCaughtType(l.head.pos(), exc, thrownInTry, caughtInTry);
mcimadamore@1237 1095 caughtInTry = chk.incl(exc, caughtInTry);
mcimadamore@1237 1096 }
mcimadamore@1237 1097 }
mcimadamore@1237 1098 scan(param);
mcimadamore@1237 1099 preciseRethrowTypes.put(param.sym, chk.intersect(ctypes, rethrownTypes));
mcimadamore@1297 1100 scan(l.head.body);
mcimadamore@1237 1101 preciseRethrowTypes.remove(param.sym);
mcimadamore@1237 1102 }
mcimadamore@1237 1103 if (tree.finalizer != null) {
mcimadamore@1237 1104 List<Type> savedThrown = thrown;
mcimadamore@1237 1105 thrown = List.nil();
mcimadamore@1237 1106 ListBuffer<FlowPendingExit> exits = pendingExits;
mcimadamore@1237 1107 pendingExits = prevPendingExits;
mcimadamore@1297 1108 scan(tree.finalizer);
mcimadamore@1297 1109 if (!tree.finallyCanCompleteNormally) {
mcimadamore@1237 1110 // discard exits and exceptions from try and finally
mcimadamore@1237 1111 thrown = chk.union(thrown, thrownPrev);
mcimadamore@1237 1112 } else {
mcimadamore@1237 1113 thrown = chk.union(thrown, chk.diff(thrownInTry, caughtInTry));
mcimadamore@1237 1114 thrown = chk.union(thrown, savedThrown);
mcimadamore@1237 1115 // FIX: this doesn't preserve source order of exits in catch
mcimadamore@1237 1116 // versus finally!
mcimadamore@1237 1117 while (exits.nonEmpty()) {
mcimadamore@1237 1118 pendingExits.append(exits.next());
mcimadamore@1237 1119 }
mcimadamore@1237 1120 }
mcimadamore@1237 1121 } else {
mcimadamore@1237 1122 thrown = chk.union(thrown, chk.diff(thrownInTry, caughtInTry));
mcimadamore@1237 1123 ListBuffer<FlowPendingExit> exits = pendingExits;
mcimadamore@1237 1124 pendingExits = prevPendingExits;
mcimadamore@1237 1125 while (exits.nonEmpty()) pendingExits.append(exits.next());
mcimadamore@1237 1126 }
mcimadamore@1237 1127 }
mcimadamore@1237 1128
mcimadamore@1237 1129 @Override
mcimadamore@1237 1130 public void visitIf(JCIf tree) {
mcimadamore@1237 1131 scan(tree.cond);
mcimadamore@1297 1132 scan(tree.thenpart);
mcimadamore@1237 1133 if (tree.elsepart != null) {
mcimadamore@1297 1134 scan(tree.elsepart);
mcimadamore@1237 1135 }
mcimadamore@1237 1136 }
mcimadamore@1237 1137
mcimadamore@1237 1138 void checkCaughtType(DiagnosticPosition pos, Type exc, List<Type> thrownInTry, List<Type> caughtInTry) {
mcimadamore@1237 1139 if (chk.subset(exc, caughtInTry)) {
mcimadamore@1237 1140 log.error(pos, "except.already.caught", exc);
mcimadamore@1237 1141 } else if (!chk.isUnchecked(pos, exc) &&
mcimadamore@1237 1142 !isExceptionOrThrowable(exc) &&
mcimadamore@1237 1143 !chk.intersects(exc, thrownInTry)) {
mcimadamore@1237 1144 log.error(pos, "except.never.thrown.in.try", exc);
mcimadamore@1237 1145 } else if (allowImprovedCatchAnalysis) {
mcimadamore@1237 1146 List<Type> catchableThrownTypes = chk.intersect(List.of(exc), thrownInTry);
mcimadamore@1237 1147 // 'catchableThrownTypes' cannnot possibly be empty - if 'exc' was an
mcimadamore@1237 1148 // unchecked exception, the result list would not be empty, as the augmented
mcimadamore@1237 1149 // thrown set includes { RuntimeException, Error }; if 'exc' was a checked
mcimadamore@1237 1150 // exception, that would have been covered in the branch above
mcimadamore@1237 1151 if (chk.diff(catchableThrownTypes, caughtInTry).isEmpty() &&
mcimadamore@1237 1152 !isExceptionOrThrowable(exc)) {
mcimadamore@1237 1153 String key = catchableThrownTypes.length() == 1 ?
mcimadamore@1237 1154 "unreachable.catch" :
mcimadamore@1237 1155 "unreachable.catch.1";
mcimadamore@1237 1156 log.warning(pos, key, catchableThrownTypes);
mcimadamore@1237 1157 }
mcimadamore@1237 1158 }
mcimadamore@1237 1159 }
mcimadamore@1237 1160 //where
mcimadamore@1237 1161 private boolean isExceptionOrThrowable(Type exc) {
mcimadamore@1237 1162 return exc.tsym == syms.throwableType.tsym ||
mcimadamore@1237 1163 exc.tsym == syms.exceptionType.tsym;
mcimadamore@1237 1164 }
mcimadamore@1237 1165
mcimadamore@1237 1166 public void visitBreak(JCBreak tree) {
mcimadamore@1237 1167 recordExit(tree, new FlowPendingExit(tree, null));
mcimadamore@1237 1168 }
mcimadamore@1237 1169
mcimadamore@1237 1170 public void visitContinue(JCContinue tree) {
mcimadamore@1237 1171 recordExit(tree, new FlowPendingExit(tree, null));
mcimadamore@1237 1172 }
mcimadamore@1237 1173
mcimadamore@1237 1174 public void visitReturn(JCReturn tree) {
mcimadamore@1237 1175 scan(tree.expr);
mcimadamore@1237 1176 recordExit(tree, new FlowPendingExit(tree, null));
mcimadamore@1237 1177 }
mcimadamore@1237 1178
mcimadamore@1237 1179 public void visitThrow(JCThrow tree) {
mcimadamore@1237 1180 scan(tree.expr);
mcimadamore@1237 1181 Symbol sym = TreeInfo.symbol(tree.expr);
mcimadamore@1237 1182 if (sym != null &&
mcimadamore@1237 1183 sym.kind == VAR &&
mcimadamore@1237 1184 (sym.flags() & (FINAL | EFFECTIVELY_FINAL)) != 0 &&
mcimadamore@1237 1185 preciseRethrowTypes.get(sym) != null &&
mcimadamore@1237 1186 allowImprovedRethrowAnalysis) {
mcimadamore@1237 1187 for (Type t : preciseRethrowTypes.get(sym)) {
mcimadamore@1237 1188 markThrown(tree, t);
mcimadamore@1237 1189 }
mcimadamore@1237 1190 }
mcimadamore@1237 1191 else {
mcimadamore@1237 1192 markThrown(tree, tree.expr.type);
mcimadamore@1237 1193 }
mcimadamore@1237 1194 markDead();
mcimadamore@1237 1195 }
mcimadamore@1237 1196
mcimadamore@1237 1197 public void visitApply(JCMethodInvocation tree) {
mcimadamore@1237 1198 scan(tree.meth);
mcimadamore@1237 1199 scan(tree.args);
mcimadamore@1237 1200 for (List<Type> l = tree.meth.type.getThrownTypes(); l.nonEmpty(); l = l.tail)
mcimadamore@1237 1201 markThrown(tree, l.head);
mcimadamore@1237 1202 }
mcimadamore@1237 1203
mcimadamore@1237 1204 public void visitNewClass(JCNewClass tree) {
mcimadamore@1237 1205 scan(tree.encl);
mcimadamore@1237 1206 scan(tree.args);
mcimadamore@1237 1207 // scan(tree.def);
mcimadamore@1237 1208 for (List<Type> l = tree.constructorType.getThrownTypes();
mcimadamore@1237 1209 l.nonEmpty();
mcimadamore@1237 1210 l = l.tail) {
mcimadamore@1237 1211 markThrown(tree, l.head);
mcimadamore@1237 1212 }
mcimadamore@1237 1213 List<Type> caughtPrev = caught;
mcimadamore@1237 1214 try {
mcimadamore@1237 1215 // If the new class expression defines an anonymous class,
mcimadamore@1237 1216 // analysis of the anonymous constructor may encounter thrown
mcimadamore@1237 1217 // types which are unsubstituted type variables.
mcimadamore@1237 1218 // However, since the constructor's actual thrown types have
mcimadamore@1237 1219 // already been marked as thrown, it is safe to simply include
mcimadamore@1237 1220 // each of the constructor's formal thrown types in the set of
mcimadamore@1237 1221 // 'caught/declared to be thrown' types, for the duration of
mcimadamore@1237 1222 // the class def analysis.
mcimadamore@1237 1223 if (tree.def != null)
mcimadamore@1237 1224 for (List<Type> l = tree.constructor.type.getThrownTypes();
mcimadamore@1237 1225 l.nonEmpty();
mcimadamore@1237 1226 l = l.tail) {
mcimadamore@1237 1227 caught = chk.incl(l.head, caught);
mcimadamore@1237 1228 }
mcimadamore@1237 1229 scan(tree.def);
mcimadamore@1237 1230 }
mcimadamore@1237 1231 finally {
mcimadamore@1237 1232 caught = caughtPrev;
mcimadamore@1237 1233 }
mcimadamore@1237 1234 }
mcimadamore@1237 1235
mcimadamore@1348 1236 @Override
mcimadamore@1348 1237 public void visitLambda(JCLambda tree) {
mcimadamore@1348 1238 if (tree.type != null &&
mcimadamore@1348 1239 tree.type.isErroneous()) {
mcimadamore@1348 1240 return;
mcimadamore@1348 1241 }
mcimadamore@1348 1242 List<Type> prevCaught = caught;
mcimadamore@1348 1243 List<Type> prevThrown = thrown;
mcimadamore@1348 1244 ListBuffer<FlowPendingExit> prevPending = pendingExits;
mcimadamore@1348 1245 try {
mcimadamore@1348 1246 pendingExits = ListBuffer.lb();
mcimadamore@1348 1247 caught = List.of(syms.throwableType); //inhibit exception checking
mcimadamore@1348 1248 thrown = List.nil();
mcimadamore@1348 1249 scan(tree.body);
mcimadamore@1348 1250 tree.inferredThrownTypes = thrown;
mcimadamore@1348 1251 }
mcimadamore@1348 1252 finally {
mcimadamore@1348 1253 pendingExits = prevPending;
mcimadamore@1348 1254 caught = prevCaught;
mcimadamore@1348 1255 thrown = prevThrown;
mcimadamore@1348 1256 }
mcimadamore@1348 1257 }
mcimadamore@1348 1258
mcimadamore@1237 1259 public void visitTopLevel(JCCompilationUnit tree) {
mcimadamore@1237 1260 // Do nothing for TopLevel since each class is visited individually
mcimadamore@1237 1261 }
mcimadamore@1237 1262
mcimadamore@1237 1263 /**************************************************************************
mcimadamore@1237 1264 * main method
mcimadamore@1237 1265 *************************************************************************/
mcimadamore@1237 1266
mcimadamore@1237 1267 /** Perform definite assignment/unassignment analysis on a tree.
mcimadamore@1237 1268 */
mcimadamore@1237 1269 public void analyzeTree(Env<AttrContext> env, TreeMaker make) {
mcimadamore@1297 1270 analyzeTree(env, env.tree, make);
mcimadamore@1297 1271 }
mcimadamore@1297 1272 public void analyzeTree(Env<AttrContext> env, JCTree tree, TreeMaker make) {
mcimadamore@1237 1273 try {
mcimadamore@1237 1274 attrEnv = env;
mcimadamore@1237 1275 Flow.this.make = make;
mcimadamore@1237 1276 pendingExits = new ListBuffer<FlowPendingExit>();
mcimadamore@1237 1277 preciseRethrowTypes = new HashMap<Symbol, List<Type>>();
mcimadamore@1237 1278 this.thrown = this.caught = null;
mcimadamore@1237 1279 this.classDef = null;
mcimadamore@1237 1280 scan(tree);
mcimadamore@1237 1281 } finally {
mcimadamore@1237 1282 pendingExits = null;
mcimadamore@1237 1283 Flow.this.make = null;
mcimadamore@1237 1284 this.thrown = this.caught = null;
mcimadamore@1237 1285 this.classDef = null;
mcimadamore@878 1286 }
duke@1 1287 }
duke@1 1288 }
duke@1 1289
mcimadamore@1237 1290 /**
mcimadamore@1237 1291 * This pass implements (i) definite assignment analysis, which ensures that
mcimadamore@1237 1292 * each variable is assigned when used and (ii) definite unassignment analysis,
mcimadamore@1237 1293 * which ensures that no final variable is assigned more than once. This visitor
mcimadamore@1297 1294 * depends on the results of the liveliness analyzer. This pass is also used to mark
mcimadamore@1297 1295 * effectively-final local variables/parameters.
duke@1 1296 */
mcimadamore@1237 1297 class AssignAnalyzer extends BaseAnalyzer<AssignAnalyzer.AssignPendingExit> {
mcimadamore@1237 1298
mcimadamore@1237 1299 /** The set of definitely assigned variables.
mcimadamore@1237 1300 */
mcimadamore@1237 1301 Bits inits;
mcimadamore@1237 1302
mcimadamore@1237 1303 /** The set of definitely unassigned variables.
mcimadamore@1237 1304 */
mcimadamore@1237 1305 Bits uninits;
mcimadamore@1237 1306
mcimadamore@1237 1307 /** The set of variables that are definitely unassigned everywhere
mcimadamore@1237 1308 * in current try block. This variable is maintained lazily; it is
mcimadamore@1237 1309 * updated only when something gets removed from uninits,
mcimadamore@1237 1310 * typically by being assigned in reachable code. To obtain the
mcimadamore@1237 1311 * correct set of variables which are definitely unassigned
mcimadamore@1237 1312 * anywhere in current try block, intersect uninitsTry and
mcimadamore@1237 1313 * uninits.
mcimadamore@1237 1314 */
mcimadamore@1237 1315 Bits uninitsTry;
mcimadamore@1237 1316
mcimadamore@1237 1317 /** When analyzing a condition, inits and uninits are null.
mcimadamore@1237 1318 * Instead we have:
mcimadamore@1237 1319 */
mcimadamore@1237 1320 Bits initsWhenTrue;
mcimadamore@1237 1321 Bits initsWhenFalse;
mcimadamore@1237 1322 Bits uninitsWhenTrue;
mcimadamore@1237 1323 Bits uninitsWhenFalse;
mcimadamore@1237 1324
mcimadamore@1237 1325 /** A mapping from addresses to variable symbols.
mcimadamore@1237 1326 */
mcimadamore@1237 1327 VarSymbol[] vars;
mcimadamore@1237 1328
mcimadamore@1237 1329 /** The current class being defined.
mcimadamore@1237 1330 */
mcimadamore@1237 1331 JCClassDecl classDef;
mcimadamore@1237 1332
mcimadamore@1237 1333 /** The first variable sequence number in this class definition.
mcimadamore@1237 1334 */
mcimadamore@1237 1335 int firstadr;
mcimadamore@1237 1336
mcimadamore@1237 1337 /** The next available variable sequence number.
mcimadamore@1237 1338 */
mcimadamore@1237 1339 int nextadr;
mcimadamore@1237 1340
mcimadamore@1348 1341 /** The first variable sequence number in a block that can return.
mcimadamore@1348 1342 */
mcimadamore@1348 1343 int returnadr;
mcimadamore@1348 1344
mcimadamore@1237 1345 /** The list of unreferenced automatic resources.
mcimadamore@1237 1346 */
mcimadamore@1237 1347 Scope unrefdResources;
mcimadamore@1237 1348
mcimadamore@1237 1349 /** Set when processing a loop body the second time for DU analysis. */
mcimadamore@1297 1350 FlowKind flowKind = FlowKind.NORMAL;
mcimadamore@1297 1351
mcimadamore@1297 1352 /** The starting position of the analysed tree */
mcimadamore@1297 1353 int startPos;
mcimadamore@1237 1354
mcimadamore@1237 1355 class AssignPendingExit extends BaseAnalyzer.PendingExit {
mcimadamore@1237 1356
mcimadamore@1237 1357 Bits exit_inits;
mcimadamore@1237 1358 Bits exit_uninits;
mcimadamore@1237 1359
mcimadamore@1237 1360 AssignPendingExit(JCTree tree, Bits inits, Bits uninits) {
mcimadamore@1237 1361 super(tree);
mcimadamore@1237 1362 this.exit_inits = inits.dup();
mcimadamore@1237 1363 this.exit_uninits = uninits.dup();
mcimadamore@1237 1364 }
mcimadamore@1237 1365
mcimadamore@1237 1366 void resolveJump() {
mcimadamore@1237 1367 inits.andSet(exit_inits);
mcimadamore@1237 1368 uninits.andSet(exit_uninits);
mcimadamore@1237 1369 }
duke@1 1370 }
duke@1 1371
mcimadamore@1237 1372 @Override
mcimadamore@1237 1373 void markDead() {
mcimadamore@1348 1374 inits.inclRange(returnadr, nextadr);
mcimadamore@1348 1375 uninits.inclRange(returnadr, nextadr);
mcimadamore@1237 1376 }
duke@1 1377
mcimadamore@1237 1378 /*-------------- Processing variables ----------------------*/
duke@1 1379
mcimadamore@1237 1380 /** Do we need to track init/uninit state of this symbol?
mcimadamore@1237 1381 * I.e. is symbol either a local or a blank final variable?
mcimadamore@1237 1382 */
mcimadamore@1237 1383 boolean trackable(VarSymbol sym) {
mcimadamore@1237 1384 return
mcimadamore@1297 1385 sym.pos >= startPos &&
mcimadamore@1297 1386 ((sym.owner.kind == MTH ||
mcimadamore@1237 1387 ((sym.flags() & (FINAL | HASINIT | PARAMETER)) == FINAL &&
mcimadamore@1297 1388 classDef.sym.isEnclosedBy((ClassSymbol)sym.owner))));
duke@1 1389 }
duke@1 1390
mcimadamore@1237 1391 /** Initialize new trackable variable by setting its address field
mcimadamore@1237 1392 * to the next available sequence number and entering it under that
mcimadamore@1237 1393 * index into the vars array.
mcimadamore@1237 1394 */
mcimadamore@1237 1395 void newVar(VarSymbol sym) {
jjg@1339 1396 vars = ArrayUtils.ensureCapacity(vars, nextadr);
mcimadamore@1297 1397 if ((sym.flags() & FINAL) == 0) {
mcimadamore@1297 1398 sym.flags_field |= EFFECTIVELY_FINAL;
mcimadamore@1297 1399 }
mcimadamore@1237 1400 sym.adr = nextadr;
mcimadamore@1237 1401 vars[nextadr] = sym;
mcimadamore@1237 1402 inits.excl(nextadr);
mcimadamore@1237 1403 uninits.incl(nextadr);
mcimadamore@1237 1404 nextadr++;
mcimadamore@1237 1405 }
mcimadamore@1237 1406
mcimadamore@1237 1407 /** Record an initialization of a trackable variable.
mcimadamore@1237 1408 */
mcimadamore@1237 1409 void letInit(DiagnosticPosition pos, VarSymbol sym) {
mcimadamore@1237 1410 if (sym.adr >= firstadr && trackable(sym)) {
mcimadamore@1297 1411 if ((sym.flags() & EFFECTIVELY_FINAL) != 0) {
mcimadamore@1297 1412 if (!uninits.isMember(sym.adr)) {
mcimadamore@1297 1413 //assignment targeting an effectively final variable
mcimadamore@1297 1414 //makes the variable lose its status of effectively final
mcimadamore@1297 1415 //if the variable is _not_ definitively unassigned
mcimadamore@1297 1416 sym.flags_field &= ~EFFECTIVELY_FINAL;
mcimadamore@1297 1417 } else {
mcimadamore@1297 1418 uninit(sym);
mcimadamore@1297 1419 }
mcimadamore@1297 1420 }
mcimadamore@1297 1421 else if ((sym.flags() & FINAL) != 0) {
mcimadamore@1237 1422 if ((sym.flags() & PARAMETER) != 0) {
mcimadamore@1237 1423 if ((sym.flags() & UNION) != 0) { //multi-catch parameter
mcimadamore@1237 1424 log.error(pos, "multicatch.parameter.may.not.be.assigned",
mcimadamore@1237 1425 sym);
mcimadamore@1237 1426 }
mcimadamore@1237 1427 else {
mcimadamore@1237 1428 log.error(pos, "final.parameter.may.not.be.assigned",
mcimadamore@550 1429 sym);
mcimadamore@1237 1430 }
mcimadamore@1237 1431 } else if (!uninits.isMember(sym.adr)) {
mcimadamore@1297 1432 log.error(pos, flowKind.errKey, sym);
mcimadamore@1237 1433 } else {
mcimadamore@1297 1434 uninit(sym);
mcimadamore@550 1435 }
mcimadamore@1237 1436 }
mcimadamore@1237 1437 inits.incl(sym.adr);
mcimadamore@1237 1438 } else if ((sym.flags() & FINAL) != 0) {
mcimadamore@1237 1439 log.error(pos, "var.might.already.be.assigned", sym);
mcimadamore@1237 1440 }
mcimadamore@1237 1441 }
mcimadamore@1297 1442 //where
mcimadamore@1297 1443 void uninit(VarSymbol sym) {
mcimadamore@1297 1444 if (!inits.isMember(sym.adr)) {
mcimadamore@1297 1445 // reachable assignment
mcimadamore@1297 1446 uninits.excl(sym.adr);
mcimadamore@1297 1447 uninitsTry.excl(sym.adr);
mcimadamore@1297 1448 } else {
mcimadamore@1297 1449 //log.rawWarning(pos, "unreachable assignment");//DEBUG
mcimadamore@1297 1450 uninits.excl(sym.adr);
mcimadamore@1297 1451 }
mcimadamore@1297 1452 }
mcimadamore@1237 1453
mcimadamore@1237 1454 /** If tree is either a simple name or of the form this.name or
mcimadamore@1237 1455 * C.this.name, and tree represents a trackable variable,
mcimadamore@1237 1456 * record an initialization of the variable.
mcimadamore@1237 1457 */
mcimadamore@1237 1458 void letInit(JCTree tree) {
mcimadamore@1237 1459 tree = TreeInfo.skipParens(tree);
mcimadamore@1237 1460 if (tree.hasTag(IDENT) || tree.hasTag(SELECT)) {
mcimadamore@1237 1461 Symbol sym = TreeInfo.symbol(tree);
mcimadamore@1237 1462 if (sym.kind == VAR) {
mcimadamore@1237 1463 letInit(tree.pos(), (VarSymbol)sym);
duke@1 1464 }
duke@1 1465 }
duke@1 1466 }
duke@1 1467
mcimadamore@1237 1468 /** Check that trackable variable is initialized.
mcimadamore@1237 1469 */
mcimadamore@1237 1470 void checkInit(DiagnosticPosition pos, VarSymbol sym) {
mcimadamore@1237 1471 if ((sym.adr >= firstadr || sym.owner.kind != TYP) &&
mcimadamore@1237 1472 trackable(sym) &&
mcimadamore@1237 1473 !inits.isMember(sym.adr)) {
mcimadamore@1237 1474 log.error(pos, "var.might.not.have.been.initialized",
mcimadamore@1237 1475 sym);
mcimadamore@1237 1476 inits.incl(sym.adr);
mcimadamore@676 1477 }
duke@1 1478 }
duke@1 1479
mcimadamore@1237 1480 /** Split (duplicate) inits/uninits into WhenTrue/WhenFalse sets
mcimadamore@1237 1481 */
mcimadamore@1237 1482 void split(boolean setToNull) {
mcimadamore@1237 1483 initsWhenFalse = inits.dup();
mcimadamore@1237 1484 uninitsWhenFalse = uninits.dup();
mcimadamore@1237 1485 initsWhenTrue = inits;
mcimadamore@1237 1486 uninitsWhenTrue = uninits;
mcimadamore@1237 1487 if (setToNull)
mcimadamore@1237 1488 inits = uninits = null;
duke@1 1489 }
duke@1 1490
mcimadamore@1237 1491 /** Merge (intersect) inits/uninits from WhenTrue/WhenFalse sets.
mcimadamore@1237 1492 */
mcimadamore@1237 1493 void merge() {
mcimadamore@1237 1494 inits = initsWhenFalse.andSet(initsWhenTrue);
mcimadamore@1237 1495 uninits = uninitsWhenFalse.andSet(uninitsWhenTrue);
mcimadamore@1237 1496 }
duke@1 1497
mcimadamore@1237 1498 /* ************************************************************************
mcimadamore@1237 1499 * Visitor methods for statements and definitions
mcimadamore@1237 1500 *************************************************************************/
duke@1 1501
mcimadamore@1237 1502 /** Analyze an expression. Make sure to set (un)inits rather than
mcimadamore@1237 1503 * (un)initsWhenTrue(WhenFalse) on exit.
mcimadamore@1237 1504 */
mcimadamore@1237 1505 void scanExpr(JCTree tree) {
mcimadamore@1237 1506 if (tree != null) {
mcimadamore@1237 1507 scan(tree);
mcimadamore@1237 1508 if (inits == null) merge();
duke@1 1509 }
duke@1 1510 }
duke@1 1511
mcimadamore@1237 1512 /** Analyze a list of expressions.
mcimadamore@1237 1513 */
mcimadamore@1237 1514 void scanExprs(List<? extends JCExpression> trees) {
mcimadamore@1237 1515 if (trees != null)
mcimadamore@1237 1516 for (List<? extends JCExpression> l = trees; l.nonEmpty(); l = l.tail)
mcimadamore@1237 1517 scanExpr(l.head);
mcimadamore@1237 1518 }
mcimadamore@1237 1519
mcimadamore@1237 1520 /** Analyze a condition. Make sure to set (un)initsWhenTrue(WhenFalse)
mcimadamore@1237 1521 * rather than (un)inits on exit.
mcimadamore@1237 1522 */
mcimadamore@1237 1523 void scanCond(JCTree tree) {
mcimadamore@1237 1524 if (tree.type.isFalse()) {
mcimadamore@1237 1525 if (inits == null) merge();
mcimadamore@1237 1526 initsWhenTrue = inits.dup();
mcimadamore@1237 1527 initsWhenTrue.inclRange(firstadr, nextadr);
mcimadamore@1237 1528 uninitsWhenTrue = uninits.dup();
mcimadamore@1237 1529 uninitsWhenTrue.inclRange(firstadr, nextadr);
mcimadamore@1237 1530 initsWhenFalse = inits;
mcimadamore@1237 1531 uninitsWhenFalse = uninits;
mcimadamore@1237 1532 } else if (tree.type.isTrue()) {
mcimadamore@1237 1533 if (inits == null) merge();
mcimadamore@1237 1534 initsWhenFalse = inits.dup();
mcimadamore@1237 1535 initsWhenFalse.inclRange(firstadr, nextadr);
mcimadamore@1237 1536 uninitsWhenFalse = uninits.dup();
mcimadamore@1237 1537 uninitsWhenFalse.inclRange(firstadr, nextadr);
mcimadamore@1237 1538 initsWhenTrue = inits;
mcimadamore@1237 1539 uninitsWhenTrue = uninits;
duke@1 1540 } else {
mcimadamore@1237 1541 scan(tree);
mcimadamore@1237 1542 if (inits != null)
mcimadamore@1237 1543 split(tree.type != syms.unknownType);
duke@1 1544 }
mcimadamore@1237 1545 if (tree.type != syms.unknownType)
mcimadamore@1237 1546 inits = uninits = null;
duke@1 1547 }
duke@1 1548
mcimadamore@1237 1549 /* ------------ Visitor methods for various sorts of trees -------------*/
duke@1 1550
mcimadamore@1237 1551 public void visitClassDef(JCClassDecl tree) {
mcimadamore@1237 1552 if (tree.sym == null) return;
duke@1 1553
mcimadamore@1237 1554 JCClassDecl classDefPrev = classDef;
mcimadamore@1237 1555 int firstadrPrev = firstadr;
mcimadamore@1237 1556 int nextadrPrev = nextadr;
mcimadamore@1237 1557 ListBuffer<AssignPendingExit> pendingExitsPrev = pendingExits;
mcimadamore@1237 1558 Lint lintPrev = lint;
duke@1 1559
mcimadamore@1237 1560 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1561 if (tree.name != names.empty) {
mcimadamore@1237 1562 firstadr = nextadr;
mcimadamore@1237 1563 }
mcimadamore@1237 1564 classDef = tree;
jfranck@1313 1565 lint = lint.augment(tree.sym.annotations);
duke@1 1566
mcimadamore@1237 1567 try {
mcimadamore@1237 1568 // define all the static fields
duke@1 1569 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1570 if (l.head.hasTag(VARDEF)) {
mcimadamore@1237 1571 JCVariableDecl def = (JCVariableDecl)l.head;
mcimadamore@1237 1572 if ((def.mods.flags & STATIC) != 0) {
mcimadamore@1237 1573 VarSymbol sym = def.sym;
mcimadamore@1237 1574 if (trackable(sym))
mcimadamore@1237 1575 newVar(sym);
duke@1 1576 }
duke@1 1577 }
duke@1 1578 }
duke@1 1579
mcimadamore@1237 1580 // process all the static initializers
mcimadamore@1237 1581 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1582 if (!l.head.hasTag(METHODDEF) &&
mcimadamore@1237 1583 (TreeInfo.flags(l.head) & STATIC) != 0) {
mcimadamore@1237 1584 scan(l.head);
duke@1 1585 }
duke@1 1586 }
duke@1 1587
mcimadamore@1237 1588 // define all the instance fields
duke@1 1589 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1590 if (l.head.hasTag(VARDEF)) {
mcimadamore@1237 1591 JCVariableDecl def = (JCVariableDecl)l.head;
mcimadamore@1237 1592 if ((def.mods.flags & STATIC) == 0) {
mcimadamore@1237 1593 VarSymbol sym = def.sym;
mcimadamore@1237 1594 if (trackable(sym))
mcimadamore@1237 1595 newVar(sym);
mcimadamore@1237 1596 }
duke@1 1597 }
duke@1 1598 }
mcimadamore@1237 1599
mcimadamore@1237 1600 // process all the instance initializers
mcimadamore@1237 1601 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1602 if (!l.head.hasTag(METHODDEF) &&
mcimadamore@1237 1603 (TreeInfo.flags(l.head) & STATIC) == 0) {
mcimadamore@1237 1604 scan(l.head);
mcimadamore@1237 1605 }
mcimadamore@1237 1606 }
mcimadamore@1237 1607
mcimadamore@1237 1608 // process all the methods
mcimadamore@1237 1609 for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1610 if (l.head.hasTag(METHODDEF)) {
mcimadamore@1237 1611 scan(l.head);
mcimadamore@1237 1612 }
mcimadamore@1237 1613 }
mcimadamore@1237 1614 } finally {
mcimadamore@1237 1615 pendingExits = pendingExitsPrev;
mcimadamore@1237 1616 nextadr = nextadrPrev;
mcimadamore@1237 1617 firstadr = firstadrPrev;
mcimadamore@1237 1618 classDef = classDefPrev;
mcimadamore@1237 1619 lint = lintPrev;
duke@1 1620 }
mcimadamore@1237 1621 }
duke@1 1622
mcimadamore@1237 1623 public void visitMethodDef(JCMethodDecl tree) {
mcimadamore@1237 1624 if (tree.body == null) return;
mcimadamore@1237 1625
mcimadamore@1237 1626 Bits initsPrev = inits.dup();
mcimadamore@1237 1627 Bits uninitsPrev = uninits.dup();
mcimadamore@1237 1628 int nextadrPrev = nextadr;
mcimadamore@1237 1629 int firstadrPrev = firstadr;
mcimadamore@1348 1630 int returnadrPrev = returnadr;
mcimadamore@1237 1631 Lint lintPrev = lint;
mcimadamore@1237 1632
jfranck@1313 1633 lint = lint.augment(tree.sym.annotations);
mcimadamore@1237 1634
mcimadamore@1237 1635 Assert.check(pendingExits.isEmpty());
mcimadamore@1237 1636
mcimadamore@1237 1637 try {
mcimadamore@1237 1638 boolean isInitialConstructor =
mcimadamore@1237 1639 TreeInfo.isInitialConstructor(tree);
mcimadamore@1237 1640
mcimadamore@1237 1641 if (!isInitialConstructor)
mcimadamore@1237 1642 firstadr = nextadr;
mcimadamore@1237 1643 for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1644 JCVariableDecl def = l.head;
mcimadamore@1237 1645 scan(def);
mcimadamore@1237 1646 inits.incl(def.sym.adr);
mcimadamore@1237 1647 uninits.excl(def.sym.adr);
duke@1 1648 }
mcimadamore@1237 1649 // else we are in an instance initializer block;
mcimadamore@1237 1650 // leave caught unchanged.
mcimadamore@1237 1651 scan(tree.body);
duke@1 1652
mcimadamore@1237 1653 if (isInitialConstructor) {
mcimadamore@1237 1654 for (int i = firstadr; i < nextadr; i++)
mcimadamore@1237 1655 if (vars[i].owner == classDef.sym)
mcimadamore@1237 1656 checkInit(TreeInfo.diagEndPos(tree.body), vars[i]);
mcimadamore@1237 1657 }
mcimadamore@1237 1658 List<AssignPendingExit> exits = pendingExits.toList();
mcimadamore@1237 1659 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1660 while (exits.nonEmpty()) {
mcimadamore@1237 1661 AssignPendingExit exit = exits.head;
mcimadamore@1237 1662 exits = exits.tail;
mcimadamore@1237 1663 Assert.check(exit.tree.hasTag(RETURN), exit.tree);
duke@1 1664 if (isInitialConstructor) {
mcimadamore@1237 1665 inits = exit.exit_inits;
duke@1 1666 for (int i = firstadr; i < nextadr; i++)
duke@1 1667 checkInit(exit.tree.pos(), vars[i]);
duke@1 1668 }
duke@1 1669 }
duke@1 1670 } finally {
mcimadamore@1237 1671 inits = initsPrev;
mcimadamore@1237 1672 uninits = uninitsPrev;
mcimadamore@1237 1673 nextadr = nextadrPrev;
mcimadamore@1237 1674 firstadr = firstadrPrev;
mcimadamore@1348 1675 returnadr = returnadrPrev;
duke@1 1676 lint = lintPrev;
duke@1 1677 }
duke@1 1678 }
duke@1 1679
mcimadamore@1237 1680 public void visitVarDef(JCVariableDecl tree) {
mcimadamore@1237 1681 boolean track = trackable(tree.sym);
mcimadamore@1237 1682 if (track && tree.sym.owner.kind == MTH) newVar(tree.sym);
mcimadamore@1237 1683 if (tree.init != null) {
mcimadamore@1237 1684 Lint lintPrev = lint;
jfranck@1313 1685 lint = lint.augment(tree.sym.annotations);
mcimadamore@1237 1686 try{
mcimadamore@1237 1687 scanExpr(tree.init);
mcimadamore@1237 1688 if (track) letInit(tree.pos(), tree.sym);
mcimadamore@1237 1689 } finally {
mcimadamore@1237 1690 lint = lintPrev;
mcimadamore@1237 1691 }
mcimadamore@1237 1692 }
mcimadamore@1237 1693 }
duke@1 1694
mcimadamore@1237 1695 public void visitBlock(JCBlock tree) {
mcimadamore@1237 1696 int nextadrPrev = nextadr;
mcimadamore@1237 1697 scan(tree.stats);
mcimadamore@1237 1698 nextadr = nextadrPrev;
mcimadamore@1237 1699 }
duke@1 1700
mcimadamore@1237 1701 public void visitDoLoop(JCDoWhileLoop tree) {
mcimadamore@1237 1702 ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
mcimadamore@1297 1703 FlowKind prevFlowKind = flowKind;
mcimadamore@1297 1704 flowKind = FlowKind.NORMAL;
mcimadamore@1297 1705 Bits initsSkip = null;
mcimadamore@1297 1706 Bits uninitsSkip = null;
mcimadamore@1237 1707 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1708 int prevErrors = log.nerrors;
mcimadamore@1237 1709 do {
mcimadamore@1237 1710 Bits uninitsEntry = uninits.dup();
mcimadamore@1237 1711 uninitsEntry.excludeFrom(nextadr);
mcimadamore@1297 1712 scan(tree.body);
mcimadamore@1297 1713 resolveContinues(tree);
mcimadamore@1237 1714 scanCond(tree.cond);
mcimadamore@1297 1715 if (!flowKind.isFinal()) {
mcimadamore@1297 1716 initsSkip = initsWhenFalse;
mcimadamore@1297 1717 uninitsSkip = uninitsWhenFalse;
mcimadamore@1297 1718 }
mcimadamore@1237 1719 if (log.nerrors != prevErrors ||
mcimadamore@1297 1720 flowKind.isFinal() ||
mcimadamore@1237 1721 uninitsEntry.dup().diffSet(uninitsWhenTrue).nextBit(firstadr)==-1)
mcimadamore@1237 1722 break;
mcimadamore@1237 1723 inits = initsWhenTrue;
mcimadamore@1237 1724 uninits = uninitsEntry.andSet(uninitsWhenTrue);
mcimadamore@1297 1725 flowKind = FlowKind.SPECULATIVE_LOOP;
mcimadamore@1237 1726 } while (true);
mcimadamore@1297 1727 flowKind = prevFlowKind;
mcimadamore@1297 1728 inits = initsSkip;
mcimadamore@1297 1729 uninits = uninitsSkip;
mcimadamore@1237 1730 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 1731 }
duke@1 1732
mcimadamore@1237 1733 public void visitWhileLoop(JCWhileLoop tree) {
mcimadamore@1237 1734 ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
mcimadamore@1297 1735 FlowKind prevFlowKind = flowKind;
mcimadamore@1297 1736 flowKind = FlowKind.NORMAL;
mcimadamore@1297 1737 Bits initsSkip = null;
mcimadamore@1297 1738 Bits uninitsSkip = null;
mcimadamore@1237 1739 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1740 int prevErrors = log.nerrors;
mcimadamore@1297 1741 Bits uninitsEntry = uninits.dup();
mcimadamore@1297 1742 uninitsEntry.excludeFrom(nextadr);
mcimadamore@1237 1743 do {
duke@1 1744 scanCond(tree.cond);
mcimadamore@1297 1745 if (!flowKind.isFinal()) {
mcimadamore@1297 1746 initsSkip = initsWhenFalse;
mcimadamore@1297 1747 uninitsSkip = uninitsWhenFalse;
mcimadamore@1297 1748 }
duke@1 1749 inits = initsWhenTrue;
duke@1 1750 uninits = uninitsWhenTrue;
mcimadamore@1237 1751 scan(tree.body);
mcimadamore@1237 1752 resolveContinues(tree);
mcimadamore@1237 1753 if (log.nerrors != prevErrors ||
mcimadamore@1297 1754 flowKind.isFinal() ||
mcimadamore@1237 1755 uninitsEntry.dup().diffSet(uninits).nextBit(firstadr) == -1)
mcimadamore@1237 1756 break;
mcimadamore@1237 1757 uninits = uninitsEntry.andSet(uninits);
mcimadamore@1297 1758 flowKind = FlowKind.SPECULATIVE_LOOP;
mcimadamore@1237 1759 } while (true);
mcimadamore@1297 1760 flowKind = prevFlowKind;
mcimadamore@1297 1761 //a variable is DA/DU after the while statement, if it's DA/DU assuming the
mcimadamore@1297 1762 //branch is not taken AND if it's DA/DU before any break statement
mcimadamore@1297 1763 inits = initsSkip;
mcimadamore@1297 1764 uninits = uninitsSkip;
mcimadamore@1237 1765 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 1766 }
mcimadamore@1237 1767
mcimadamore@1237 1768 public void visitForLoop(JCForLoop tree) {
mcimadamore@1237 1769 ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
mcimadamore@1297 1770 FlowKind prevFlowKind = flowKind;
mcimadamore@1297 1771 flowKind = FlowKind.NORMAL;
mcimadamore@1237 1772 int nextadrPrev = nextadr;
mcimadamore@1237 1773 scan(tree.init);
mcimadamore@1297 1774 Bits initsSkip = null;
mcimadamore@1297 1775 Bits uninitsSkip = null;
mcimadamore@1237 1776 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1777 int prevErrors = log.nerrors;
mcimadamore@1237 1778 do {
mcimadamore@1237 1779 Bits uninitsEntry = uninits.dup();
mcimadamore@1237 1780 uninitsEntry.excludeFrom(nextadr);
mcimadamore@1237 1781 if (tree.cond != null) {
mcimadamore@1237 1782 scanCond(tree.cond);
mcimadamore@1297 1783 if (!flowKind.isFinal()) {
mcimadamore@1297 1784 initsSkip = initsWhenFalse;
mcimadamore@1297 1785 uninitsSkip = uninitsWhenFalse;
mcimadamore@1297 1786 }
mcimadamore@1237 1787 inits = initsWhenTrue;
mcimadamore@1237 1788 uninits = uninitsWhenTrue;
mcimadamore@1297 1789 } else if (!flowKind.isFinal()) {
mcimadamore@1297 1790 initsSkip = inits.dup();
mcimadamore@1297 1791 initsSkip.inclRange(firstadr, nextadr);
mcimadamore@1297 1792 uninitsSkip = uninits.dup();
mcimadamore@1297 1793 uninitsSkip.inclRange(firstadr, nextadr);
mcimadamore@1237 1794 }
mcimadamore@1237 1795 scan(tree.body);
mcimadamore@1237 1796 resolveContinues(tree);
mcimadamore@1237 1797 scan(tree.step);
mcimadamore@1237 1798 if (log.nerrors != prevErrors ||
mcimadamore@1297 1799 flowKind.isFinal() ||
mcimadamore@1237 1800 uninitsEntry.dup().diffSet(uninits).nextBit(firstadr) == -1)
mcimadamore@1237 1801 break;
mcimadamore@1237 1802 uninits = uninitsEntry.andSet(uninits);
mcimadamore@1297 1803 flowKind = FlowKind.SPECULATIVE_LOOP;
mcimadamore@1237 1804 } while (true);
mcimadamore@1297 1805 flowKind = prevFlowKind;
mcimadamore@1297 1806 //a variable is DA/DU after a for loop, if it's DA/DU assuming the
mcimadamore@1297 1807 //branch is not taken AND if it's DA/DU before any break statement
mcimadamore@1297 1808 inits = initsSkip;
mcimadamore@1297 1809 uninits = uninitsSkip;
mcimadamore@1237 1810 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 1811 nextadr = nextadrPrev;
mcimadamore@1237 1812 }
mcimadamore@1237 1813
mcimadamore@1237 1814 public void visitForeachLoop(JCEnhancedForLoop tree) {
mcimadamore@1237 1815 visitVarDef(tree.var);
mcimadamore@1237 1816
mcimadamore@1237 1817 ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
mcimadamore@1297 1818 FlowKind prevFlowKind = flowKind;
mcimadamore@1297 1819 flowKind = FlowKind.NORMAL;
mcimadamore@1237 1820 int nextadrPrev = nextadr;
mcimadamore@1237 1821 scan(tree.expr);
mcimadamore@1237 1822 Bits initsStart = inits.dup();
mcimadamore@1237 1823 Bits uninitsStart = uninits.dup();
mcimadamore@1237 1824
mcimadamore@1237 1825 letInit(tree.pos(), tree.var.sym);
mcimadamore@1237 1826 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1827 int prevErrors = log.nerrors;
mcimadamore@1237 1828 do {
mcimadamore@1237 1829 Bits uninitsEntry = uninits.dup();
mcimadamore@1237 1830 uninitsEntry.excludeFrom(nextadr);
mcimadamore@1237 1831 scan(tree.body);
mcimadamore@1237 1832 resolveContinues(tree);
mcimadamore@1237 1833 if (log.nerrors != prevErrors ||
mcimadamore@1297 1834 flowKind.isFinal() ||
mcimadamore@1237 1835 uninitsEntry.dup().diffSet(uninits).nextBit(firstadr) == -1)
mcimadamore@1237 1836 break;
mcimadamore@1237 1837 uninits = uninitsEntry.andSet(uninits);
mcimadamore@1297 1838 flowKind = FlowKind.SPECULATIVE_LOOP;
mcimadamore@1237 1839 } while (true);
mcimadamore@1297 1840 flowKind = prevFlowKind;
mcimadamore@1237 1841 inits = initsStart;
mcimadamore@1237 1842 uninits = uninitsStart.andSet(uninits);
mcimadamore@1237 1843 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 1844 nextadr = nextadrPrev;
mcimadamore@1237 1845 }
mcimadamore@1237 1846
mcimadamore@1237 1847 public void visitLabelled(JCLabeledStatement tree) {
mcimadamore@1237 1848 ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 1849 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1850 scan(tree.body);
mcimadamore@1237 1851 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 1852 }
mcimadamore@1237 1853
mcimadamore@1237 1854 public void visitSwitch(JCSwitch tree) {
mcimadamore@1237 1855 ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 1856 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1857 int nextadrPrev = nextadr;
mcimadamore@1237 1858 scanExpr(tree.selector);
mcimadamore@1237 1859 Bits initsSwitch = inits;
mcimadamore@1237 1860 Bits uninitsSwitch = uninits.dup();
mcimadamore@1237 1861 boolean hasDefault = false;
mcimadamore@1237 1862 for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1863 inits = initsSwitch.dup();
mcimadamore@1237 1864 uninits = uninits.andSet(uninitsSwitch);
mcimadamore@1237 1865 JCCase c = l.head;
mcimadamore@1237 1866 if (c.pat == null)
mcimadamore@1237 1867 hasDefault = true;
mcimadamore@1237 1868 else
mcimadamore@1237 1869 scanExpr(c.pat);
mcimadamore@1237 1870 scan(c.stats);
mcimadamore@1237 1871 addVars(c.stats, initsSwitch, uninitsSwitch);
mcimadamore@1237 1872 // Warn about fall-through if lint switch fallthrough enabled.
mcimadamore@1237 1873 }
mcimadamore@1237 1874 if (!hasDefault) {
mcimadamore@1237 1875 inits.andSet(initsSwitch);
mcimadamore@1237 1876 }
mcimadamore@1237 1877 resolveBreaks(tree, prevPendingExits);
mcimadamore@1237 1878 nextadr = nextadrPrev;
mcimadamore@1237 1879 }
mcimadamore@1237 1880 // where
mcimadamore@1237 1881 /** Add any variables defined in stats to inits and uninits. */
mcimadamore@1237 1882 private void addVars(List<JCStatement> stats, Bits inits,
mcimadamore@1237 1883 Bits uninits) {
mcimadamore@1237 1884 for (;stats.nonEmpty(); stats = stats.tail) {
mcimadamore@1237 1885 JCTree stat = stats.head;
mcimadamore@1237 1886 if (stat.hasTag(VARDEF)) {
mcimadamore@1237 1887 int adr = ((JCVariableDecl) stat).sym.adr;
mcimadamore@1237 1888 inits.excl(adr);
mcimadamore@1237 1889 uninits.incl(adr);
mcimadamore@1237 1890 }
mcimadamore@1237 1891 }
mcimadamore@1237 1892 }
mcimadamore@1237 1893
mcimadamore@1237 1894 public void visitTry(JCTry tree) {
mcimadamore@1237 1895 ListBuffer<JCVariableDecl> resourceVarDecls = ListBuffer.lb();
mcimadamore@1237 1896 Bits uninitsTryPrev = uninitsTry;
mcimadamore@1237 1897 ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
mcimadamore@1237 1898 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 1899 Bits initsTry = inits.dup();
mcimadamore@1237 1900 uninitsTry = uninits.dup();
mcimadamore@1237 1901 for (JCTree resource : tree.resources) {
mcimadamore@1237 1902 if (resource instanceof JCVariableDecl) {
mcimadamore@1237 1903 JCVariableDecl vdecl = (JCVariableDecl) resource;
mcimadamore@1237 1904 visitVarDef(vdecl);
mcimadamore@1237 1905 unrefdResources.enter(vdecl.sym);
mcimadamore@1237 1906 resourceVarDecls.append(vdecl);
mcimadamore@1237 1907 } else if (resource instanceof JCExpression) {
mcimadamore@1237 1908 scanExpr((JCExpression) resource);
mcimadamore@1237 1909 } else {
mcimadamore@1237 1910 throw new AssertionError(tree); // parser error
mcimadamore@1237 1911 }
mcimadamore@1237 1912 }
mcimadamore@1237 1913 scan(tree.body);
mcimadamore@1237 1914 uninitsTry.andSet(uninits);
mcimadamore@1237 1915 Bits initsEnd = inits;
mcimadamore@1237 1916 Bits uninitsEnd = uninits;
mcimadamore@1237 1917 int nextadrCatch = nextadr;
mcimadamore@1237 1918
mcimadamore@1237 1919 if (!resourceVarDecls.isEmpty() &&
mcimadamore@1237 1920 lint.isEnabled(Lint.LintCategory.TRY)) {
mcimadamore@1237 1921 for (JCVariableDecl resVar : resourceVarDecls) {
mcimadamore@1237 1922 if (unrefdResources.includes(resVar.sym)) {
mcimadamore@1237 1923 log.warning(Lint.LintCategory.TRY, resVar.pos(),
mcimadamore@1237 1924 "try.resource.not.referenced", resVar.sym);
mcimadamore@1237 1925 unrefdResources.remove(resVar.sym);
mcimadamore@1237 1926 }
mcimadamore@1237 1927 }
mcimadamore@1237 1928 }
mcimadamore@1237 1929
mcimadamore@1237 1930 for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
mcimadamore@1237 1931 JCVariableDecl param = l.head.param;
mcimadamore@1237 1932 inits = initsTry.dup();
mcimadamore@1237 1933 uninits = uninitsTry.dup();
mcimadamore@1237 1934 scan(param);
mcimadamore@1237 1935 inits.incl(param.sym.adr);
mcimadamore@1237 1936 uninits.excl(param.sym.adr);
mcimadamore@1237 1937 scan(l.head.body);
mcimadamore@1237 1938 initsEnd.andSet(inits);
mcimadamore@1237 1939 uninitsEnd.andSet(uninits);
mcimadamore@1237 1940 nextadr = nextadrCatch;
mcimadamore@1237 1941 }
mcimadamore@1237 1942 if (tree.finalizer != null) {
mcimadamore@1237 1943 inits = initsTry.dup();
mcimadamore@1237 1944 uninits = uninitsTry.dup();
mcimadamore@1237 1945 ListBuffer<AssignPendingExit> exits = pendingExits;
mcimadamore@1237 1946 pendingExits = prevPendingExits;
mcimadamore@1237 1947 scan(tree.finalizer);
mcimadamore@1237 1948 if (!tree.finallyCanCompleteNormally) {
mcimadamore@1237 1949 // discard exits and exceptions from try and finally
mcimadamore@1237 1950 } else {
mcimadamore@1237 1951 uninits.andSet(uninitsEnd);
mcimadamore@1237 1952 // FIX: this doesn't preserve source order of exits in catch
mcimadamore@1237 1953 // versus finally!
mcimadamore@1237 1954 while (exits.nonEmpty()) {
mcimadamore@1237 1955 AssignPendingExit exit = exits.next();
mcimadamore@1237 1956 if (exit.exit_inits != null) {
mcimadamore@1237 1957 exit.exit_inits.orSet(inits);
mcimadamore@1237 1958 exit.exit_uninits.andSet(uninits);
mcimadamore@1237 1959 }
mcimadamore@1237 1960 pendingExits.append(exit);
mcimadamore@1237 1961 }
mcimadamore@1237 1962 inits.orSet(initsEnd);
mcimadamore@1237 1963 }
duke@1 1964 } else {
mcimadamore@1237 1965 inits = initsEnd;
mcimadamore@1237 1966 uninits = uninitsEnd;
mcimadamore@1237 1967 ListBuffer<AssignPendingExit> exits = pendingExits;
mcimadamore@1237 1968 pendingExits = prevPendingExits;
mcimadamore@1237 1969 while (exits.nonEmpty()) pendingExits.append(exits.next());
duke@1 1970 }
mcimadamore@1237 1971 uninitsTry.andSet(uninitsTryPrev).andSet(uninits);
mcimadamore@1237 1972 }
duke@1 1973
mcimadamore@1237 1974 public void visitConditional(JCConditional tree) {
mcimadamore@1237 1975 scanCond(tree.cond);
mcimadamore@1237 1976 Bits initsBeforeElse = initsWhenFalse;
mcimadamore@1237 1977 Bits uninitsBeforeElse = uninitsWhenFalse;
mcimadamore@1237 1978 inits = initsWhenTrue;
mcimadamore@1237 1979 uninits = uninitsWhenTrue;
jjg@1374 1980 if (tree.truepart.type.hasTag(BOOLEAN) &&
jjg@1374 1981 tree.falsepart.type.hasTag(BOOLEAN)) {
mcimadamore@1237 1982 // if b and c are boolean valued, then
mcimadamore@1237 1983 // v is (un)assigned after a?b:c when true iff
mcimadamore@1237 1984 // v is (un)assigned after b when true and
mcimadamore@1237 1985 // v is (un)assigned after c when true
mcimadamore@1237 1986 scanCond(tree.truepart);
mcimadamore@1237 1987 Bits initsAfterThenWhenTrue = initsWhenTrue.dup();
mcimadamore@1237 1988 Bits initsAfterThenWhenFalse = initsWhenFalse.dup();
mcimadamore@1237 1989 Bits uninitsAfterThenWhenTrue = uninitsWhenTrue.dup();
mcimadamore@1237 1990 Bits uninitsAfterThenWhenFalse = uninitsWhenFalse.dup();
mcimadamore@1237 1991 inits = initsBeforeElse;
mcimadamore@1237 1992 uninits = uninitsBeforeElse;
mcimadamore@1237 1993 scanCond(tree.falsepart);
mcimadamore@1237 1994 initsWhenTrue.andSet(initsAfterThenWhenTrue);
mcimadamore@1237 1995 initsWhenFalse.andSet(initsAfterThenWhenFalse);
mcimadamore@1237 1996 uninitsWhenTrue.andSet(uninitsAfterThenWhenTrue);
mcimadamore@1237 1997 uninitsWhenFalse.andSet(uninitsAfterThenWhenFalse);
mcimadamore@1237 1998 } else {
mcimadamore@1237 1999 scanExpr(tree.truepart);
mcimadamore@1237 2000 Bits initsAfterThen = inits.dup();
mcimadamore@1237 2001 Bits uninitsAfterThen = uninits.dup();
mcimadamore@1237 2002 inits = initsBeforeElse;
mcimadamore@1237 2003 uninits = uninitsBeforeElse;
mcimadamore@1237 2004 scanExpr(tree.falsepart);
mcimadamore@1237 2005 inits.andSet(initsAfterThen);
mcimadamore@1237 2006 uninits.andSet(uninitsAfterThen);
duke@1 2007 }
duke@1 2008 }
duke@1 2009
mcimadamore@1237 2010 public void visitIf(JCIf tree) {
mcimadamore@1237 2011 scanCond(tree.cond);
mcimadamore@1237 2012 Bits initsBeforeElse = initsWhenFalse;
mcimadamore@1237 2013 Bits uninitsBeforeElse = uninitsWhenFalse;
mcimadamore@1237 2014 inits = initsWhenTrue;
mcimadamore@1237 2015 uninits = uninitsWhenTrue;
mcimadamore@1237 2016 scan(tree.thenpart);
mcimadamore@1237 2017 if (tree.elsepart != null) {
mcimadamore@1237 2018 Bits initsAfterThen = inits.dup();
mcimadamore@1237 2019 Bits uninitsAfterThen = uninits.dup();
mcimadamore@1237 2020 inits = initsBeforeElse;
mcimadamore@1237 2021 uninits = uninitsBeforeElse;
mcimadamore@1237 2022 scan(tree.elsepart);
mcimadamore@1237 2023 inits.andSet(initsAfterThen);
mcimadamore@1237 2024 uninits.andSet(uninitsAfterThen);
darcy@609 2025 } else {
mcimadamore@1237 2026 inits.andSet(initsBeforeElse);
mcimadamore@1237 2027 uninits.andSet(uninitsBeforeElse);
darcy@609 2028 }
darcy@609 2029 }
darcy@609 2030
mcimadamore@1237 2031 public void visitBreak(JCBreak tree) {
mcimadamore@1237 2032 recordExit(tree, new AssignPendingExit(tree, inits, uninits));
mcimadamore@1237 2033 }
mcimadamore@1237 2034
mcimadamore@1237 2035 public void visitContinue(JCContinue tree) {
mcimadamore@1237 2036 recordExit(tree, new AssignPendingExit(tree, inits, uninits));
mcimadamore@1237 2037 }
mcimadamore@1237 2038
mcimadamore@1237 2039 public void visitReturn(JCReturn tree) {
mcimadamore@1237 2040 scanExpr(tree.expr);
mcimadamore@1237 2041 recordExit(tree, new AssignPendingExit(tree, inits, uninits));
mcimadamore@1237 2042 }
mcimadamore@1237 2043
mcimadamore@1237 2044 public void visitThrow(JCThrow tree) {
mcimadamore@1237 2045 scanExpr(tree.expr);
mcimadamore@1237 2046 markDead();
mcimadamore@1237 2047 }
mcimadamore@1237 2048
mcimadamore@1237 2049 public void visitApply(JCMethodInvocation tree) {
mcimadamore@1237 2050 scanExpr(tree.meth);
mcimadamore@1237 2051 scanExprs(tree.args);
mcimadamore@1237 2052 }
mcimadamore@1237 2053
mcimadamore@1237 2054 public void visitNewClass(JCNewClass tree) {
mcimadamore@1237 2055 scanExpr(tree.encl);
mcimadamore@1237 2056 scanExprs(tree.args);
mcimadamore@1237 2057 scan(tree.def);
mcimadamore@1237 2058 }
mcimadamore@1237 2059
mcimadamore@1348 2060 @Override
mcimadamore@1348 2061 public void visitLambda(JCLambda tree) {
mcimadamore@1348 2062 Bits prevUninits = uninits;
mcimadamore@1348 2063 Bits prevInits = inits;
mcimadamore@1348 2064 int returnadrPrev = returnadr;
mcimadamore@1348 2065 ListBuffer<AssignPendingExit> prevPending = pendingExits;
mcimadamore@1348 2066 try {
mcimadamore@1348 2067 returnadr = nextadr;
mcimadamore@1348 2068 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1348 2069 for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
mcimadamore@1348 2070 JCVariableDecl def = l.head;
mcimadamore@1348 2071 scan(def);
mcimadamore@1348 2072 inits.incl(def.sym.adr);
mcimadamore@1348 2073 uninits.excl(def.sym.adr);
mcimadamore@1348 2074 }
mcimadamore@1348 2075 if (tree.getBodyKind() == JCLambda.BodyKind.EXPRESSION) {
mcimadamore@1348 2076 scanExpr(tree.body);
mcimadamore@1348 2077 } else {
mcimadamore@1348 2078 scan(tree.body);
mcimadamore@1348 2079 }
mcimadamore@1348 2080 }
mcimadamore@1348 2081 finally {
mcimadamore@1348 2082 returnadr = returnadrPrev;
mcimadamore@1348 2083 uninits = prevUninits;
mcimadamore@1348 2084 inits = prevInits;
mcimadamore@1348 2085 pendingExits = prevPending;
mcimadamore@1348 2086 }
mcimadamore@1348 2087 }
mcimadamore@1348 2088
mcimadamore@1237 2089 public void visitNewArray(JCNewArray tree) {
mcimadamore@1237 2090 scanExprs(tree.dims);
mcimadamore@1237 2091 scanExprs(tree.elems);
mcimadamore@1237 2092 }
mcimadamore@1237 2093
mcimadamore@1237 2094 public void visitAssert(JCAssert tree) {
mcimadamore@1237 2095 Bits initsExit = inits.dup();
mcimadamore@1237 2096 Bits uninitsExit = uninits.dup();
mcimadamore@1237 2097 scanCond(tree.cond);
mcimadamore@1237 2098 uninitsExit.andSet(uninitsWhenTrue);
mcimadamore@1237 2099 if (tree.detail != null) {
mcimadamore@1237 2100 inits = initsWhenFalse;
mcimadamore@1237 2101 uninits = uninitsWhenFalse;
mcimadamore@1237 2102 scanExpr(tree.detail);
duke@1 2103 }
mcimadamore@1237 2104 inits = initsExit;
mcimadamore@1237 2105 uninits = uninitsExit;
duke@1 2106 }
mcimadamore@1237 2107
mcimadamore@1237 2108 public void visitAssign(JCAssign tree) {
mcimadamore@1237 2109 JCTree lhs = TreeInfo.skipParens(tree.lhs);
mcimadamore@1297 2110 if (!(lhs instanceof JCIdent)) {
mcimadamore@1297 2111 scanExpr(lhs);
mcimadamore@1297 2112 }
mcimadamore@1237 2113 scanExpr(tree.rhs);
mcimadamore@1237 2114 letInit(lhs);
mcimadamore@1237 2115 }
mcimadamore@1237 2116
mcimadamore@1237 2117 public void visitAssignop(JCAssignOp tree) {
mcimadamore@1237 2118 scanExpr(tree.lhs);
mcimadamore@1237 2119 scanExpr(tree.rhs);
mcimadamore@1237 2120 letInit(tree.lhs);
mcimadamore@1237 2121 }
mcimadamore@1237 2122
mcimadamore@1237 2123 public void visitUnary(JCUnary tree) {
mcimadamore@1237 2124 switch (tree.getTag()) {
mcimadamore@1237 2125 case NOT:
mcimadamore@1237 2126 scanCond(tree.arg);
mcimadamore@1237 2127 Bits t = initsWhenFalse;
mcimadamore@1237 2128 initsWhenFalse = initsWhenTrue;
mcimadamore@1237 2129 initsWhenTrue = t;
mcimadamore@1237 2130 t = uninitsWhenFalse;
mcimadamore@1237 2131 uninitsWhenFalse = uninitsWhenTrue;
mcimadamore@1237 2132 uninitsWhenTrue = t;
mcimadamore@1237 2133 break;
mcimadamore@1237 2134 case PREINC: case POSTINC:
mcimadamore@1237 2135 case PREDEC: case POSTDEC:
mcimadamore@1237 2136 scanExpr(tree.arg);
mcimadamore@1237 2137 letInit(tree.arg);
mcimadamore@1237 2138 break;
mcimadamore@1237 2139 default:
mcimadamore@1237 2140 scanExpr(tree.arg);
duke@1 2141 }
duke@1 2142 }
duke@1 2143
mcimadamore@1237 2144 public void visitBinary(JCBinary tree) {
mcimadamore@1237 2145 switch (tree.getTag()) {
mcimadamore@1237 2146 case AND:
mcimadamore@1237 2147 scanCond(tree.lhs);
mcimadamore@1237 2148 Bits initsWhenFalseLeft = initsWhenFalse;
mcimadamore@1237 2149 Bits uninitsWhenFalseLeft = uninitsWhenFalse;
mcimadamore@1237 2150 inits = initsWhenTrue;
mcimadamore@1237 2151 uninits = uninitsWhenTrue;
mcimadamore@1237 2152 scanCond(tree.rhs);
mcimadamore@1237 2153 initsWhenFalse.andSet(initsWhenFalseLeft);
mcimadamore@1237 2154 uninitsWhenFalse.andSet(uninitsWhenFalseLeft);
mcimadamore@1237 2155 break;
mcimadamore@1237 2156 case OR:
mcimadamore@1237 2157 scanCond(tree.lhs);
mcimadamore@1237 2158 Bits initsWhenTrueLeft = initsWhenTrue;
mcimadamore@1237 2159 Bits uninitsWhenTrueLeft = uninitsWhenTrue;
mcimadamore@1237 2160 inits = initsWhenFalse;
mcimadamore@1237 2161 uninits = uninitsWhenFalse;
mcimadamore@1237 2162 scanCond(tree.rhs);
mcimadamore@1237 2163 initsWhenTrue.andSet(initsWhenTrueLeft);
mcimadamore@1237 2164 uninitsWhenTrue.andSet(uninitsWhenTrueLeft);
mcimadamore@1237 2165 break;
mcimadamore@1237 2166 default:
mcimadamore@1237 2167 scanExpr(tree.lhs);
mcimadamore@1237 2168 scanExpr(tree.rhs);
mcimadamore@1237 2169 }
mcimadamore@1237 2170 }
mcimadamore@1237 2171
mcimadamore@1237 2172 public void visitIdent(JCIdent tree) {
mcimadamore@1237 2173 if (tree.sym.kind == VAR) {
mcimadamore@1237 2174 checkInit(tree.pos(), (VarSymbol)tree.sym);
mcimadamore@1237 2175 referenced(tree.sym);
mcimadamore@1237 2176 }
mcimadamore@1237 2177 }
mcimadamore@1237 2178
mcimadamore@1237 2179 void referenced(Symbol sym) {
mcimadamore@1237 2180 unrefdResources.remove(sym);
mcimadamore@1237 2181 }
mcimadamore@1237 2182
mcimadamore@1237 2183 public void visitTopLevel(JCCompilationUnit tree) {
mcimadamore@1237 2184 // Do nothing for TopLevel since each class is visited individually
mcimadamore@1237 2185 }
mcimadamore@1237 2186
mcimadamore@1237 2187 /**************************************************************************
mcimadamore@1237 2188 * main method
mcimadamore@1237 2189 *************************************************************************/
mcimadamore@1237 2190
mcimadamore@1237 2191 /** Perform definite assignment/unassignment analysis on a tree.
mcimadamore@1237 2192 */
mcimadamore@1237 2193 public void analyzeTree(Env<AttrContext> env, TreeMaker make) {
mcimadamore@1297 2194 analyzeTree(env, env.tree, make);
mcimadamore@1297 2195 }
mcimadamore@1297 2196
mcimadamore@1297 2197 public void analyzeTree(Env<AttrContext> env, JCTree tree, TreeMaker make) {
mcimadamore@1237 2198 try {
mcimadamore@1237 2199 attrEnv = env;
mcimadamore@1237 2200 Flow.this.make = make;
mcimadamore@1297 2201 startPos = tree.pos().getStartPosition();
mcimadamore@1237 2202 inits = new Bits();
mcimadamore@1237 2203 uninits = new Bits();
mcimadamore@1237 2204 uninitsTry = new Bits();
mcimadamore@1237 2205 initsWhenTrue = initsWhenFalse =
mcimadamore@1237 2206 uninitsWhenTrue = uninitsWhenFalse = null;
mcimadamore@1237 2207 if (vars == null)
mcimadamore@1237 2208 vars = new VarSymbol[32];
mcimadamore@1237 2209 else
mcimadamore@1237 2210 for (int i=0; i<vars.length; i++)
mcimadamore@1237 2211 vars[i] = null;
mcimadamore@1237 2212 firstadr = 0;
mcimadamore@1237 2213 nextadr = 0;
mcimadamore@1237 2214 pendingExits = new ListBuffer<AssignPendingExit>();
mcimadamore@1237 2215 this.classDef = null;
mcimadamore@1237 2216 unrefdResources = new Scope(env.enclClass.sym);
mcimadamore@1237 2217 scan(tree);
mcimadamore@1237 2218 } finally {
mcimadamore@1237 2219 // note that recursive invocations of this method fail hard
mcimadamore@1297 2220 startPos = -1;
mcimadamore@1237 2221 inits = uninits = uninitsTry = null;
mcimadamore@1237 2222 initsWhenTrue = initsWhenFalse =
mcimadamore@1237 2223 uninitsWhenTrue = uninitsWhenFalse = null;
mcimadamore@1237 2224 if (vars != null) for (int i=0; i<vars.length; i++)
mcimadamore@1237 2225 vars[i] = null;
mcimadamore@1237 2226 firstadr = 0;
mcimadamore@1237 2227 nextadr = 0;
mcimadamore@1237 2228 pendingExits = null;
mcimadamore@1237 2229 Flow.this.make = null;
mcimadamore@1237 2230 this.classDef = null;
mcimadamore@1237 2231 unrefdResources = null;
mcimadamore@935 2232 }
mcimadamore@935 2233 }
mcimadamore@935 2234 }
mcimadamore@1297 2235
mcimadamore@1297 2236 /**
mcimadamore@1297 2237 * This pass implements the last step of the dataflow analysis, namely
mcimadamore@1297 2238 * the effectively-final analysis check. This checks that every local variable
mcimadamore@1297 2239 * reference from a lambda body/local inner class is either final or effectively final.
mcimadamore@1297 2240 * As effectively final variables are marked as such during DA/DU, this pass must run after
mcimadamore@1297 2241 * AssignAnalyzer.
mcimadamore@1297 2242 */
mcimadamore@1297 2243 class CaptureAnalyzer extends BaseAnalyzer<BaseAnalyzer.PendingExit> {
mcimadamore@1297 2244
mcimadamore@1297 2245 JCTree currentTree; //local class or lambda
mcimadamore@1297 2246
mcimadamore@1297 2247 @Override
mcimadamore@1297 2248 void markDead() {
mcimadamore@1297 2249 //do nothing
mcimadamore@1297 2250 }
mcimadamore@1297 2251
mcimadamore@1297 2252 @SuppressWarnings("fallthrough")
mcimadamore@1297 2253 void checkEffectivelyFinal(DiagnosticPosition pos, VarSymbol sym) {
mcimadamore@1297 2254 if (currentTree != null &&
mcimadamore@1297 2255 sym.owner.kind == MTH &&
mcimadamore@1297 2256 sym.pos < currentTree.getStartPosition()) {
mcimadamore@1297 2257 switch (currentTree.getTag()) {
mcimadamore@1297 2258 case CLASSDEF:
mcimadamore@1297 2259 if (!allowEffectivelyFinalInInnerClasses) {
mcimadamore@1297 2260 if ((sym.flags() & FINAL) == 0) {
mcimadamore@1297 2261 reportInnerClsNeedsFinalError(pos, sym);
mcimadamore@1297 2262 }
mcimadamore@1297 2263 break;
mcimadamore@1297 2264 }
mcimadamore@1297 2265 case LAMBDA:
mcimadamore@1297 2266 if ((sym.flags() & (EFFECTIVELY_FINAL | FINAL)) == 0) {
mcimadamore@1297 2267 reportEffectivelyFinalError(pos, sym);
mcimadamore@1297 2268 }
mcimadamore@1297 2269 }
mcimadamore@1297 2270 }
mcimadamore@1297 2271 }
mcimadamore@1297 2272
mcimadamore@1297 2273 @SuppressWarnings("fallthrough")
mcimadamore@1297 2274 void letInit(JCTree tree) {
mcimadamore@1297 2275 tree = TreeInfo.skipParens(tree);
mcimadamore@1297 2276 if (tree.hasTag(IDENT) || tree.hasTag(SELECT)) {
mcimadamore@1297 2277 Symbol sym = TreeInfo.symbol(tree);
mcimadamore@1297 2278 if (currentTree != null &&
mcimadamore@1297 2279 sym.kind == VAR &&
mcimadamore@1297 2280 sym.owner.kind == MTH &&
mcimadamore@1297 2281 ((VarSymbol)sym).pos < currentTree.getStartPosition()) {
mcimadamore@1297 2282 switch (currentTree.getTag()) {
mcimadamore@1297 2283 case CLASSDEF:
mcimadamore@1297 2284 if (!allowEffectivelyFinalInInnerClasses) {
mcimadamore@1297 2285 reportInnerClsNeedsFinalError(tree, sym);
mcimadamore@1297 2286 break;
mcimadamore@1297 2287 }
mcimadamore@1297 2288 case LAMBDA:
mcimadamore@1297 2289 reportEffectivelyFinalError(tree, sym);
mcimadamore@1297 2290 }
mcimadamore@1297 2291 }
mcimadamore@1297 2292 }
mcimadamore@1297 2293 }
mcimadamore@1297 2294
mcimadamore@1297 2295 void reportEffectivelyFinalError(DiagnosticPosition pos, Symbol sym) {
mcimadamore@1297 2296 String subKey = currentTree.hasTag(LAMBDA) ?
mcimadamore@1297 2297 "lambda" : "inner.cls";
mcimadamore@1297 2298 log.error(pos, "cant.ref.non.effectively.final.var", sym, diags.fragment(subKey));
mcimadamore@1297 2299 }
mcimadamore@1297 2300
mcimadamore@1297 2301 void reportInnerClsNeedsFinalError(DiagnosticPosition pos, Symbol sym) {
mcimadamore@1297 2302 log.error(pos,
mcimadamore@1297 2303 "local.var.accessed.from.icls.needs.final",
mcimadamore@1297 2304 sym);
mcimadamore@1297 2305 }
mcimadamore@1297 2306
mcimadamore@1297 2307 /*************************************************************************
mcimadamore@1297 2308 * Visitor methods for statements and definitions
mcimadamore@1297 2309 *************************************************************************/
mcimadamore@1297 2310
mcimadamore@1297 2311 /* ------------ Visitor methods for various sorts of trees -------------*/
mcimadamore@1297 2312
mcimadamore@1297 2313 public void visitClassDef(JCClassDecl tree) {
mcimadamore@1297 2314 JCTree prevTree = currentTree;
mcimadamore@1297 2315 try {
mcimadamore@1297 2316 currentTree = tree.sym.isLocal() ? tree : null;
mcimadamore@1297 2317 super.visitClassDef(tree);
mcimadamore@1297 2318 } finally {
mcimadamore@1297 2319 currentTree = prevTree;
mcimadamore@1297 2320 }
mcimadamore@1297 2321 }
mcimadamore@1297 2322
mcimadamore@1297 2323 @Override
mcimadamore@1297 2324 public void visitLambda(JCLambda tree) {
mcimadamore@1297 2325 JCTree prevTree = currentTree;
mcimadamore@1297 2326 try {
mcimadamore@1297 2327 currentTree = tree;
mcimadamore@1297 2328 super.visitLambda(tree);
mcimadamore@1297 2329 } finally {
mcimadamore@1297 2330 currentTree = prevTree;
mcimadamore@1297 2331 }
mcimadamore@1297 2332 }
mcimadamore@1297 2333
mcimadamore@1297 2334 @Override
mcimadamore@1297 2335 public void visitIdent(JCIdent tree) {
mcimadamore@1297 2336 if (tree.sym.kind == VAR) {
mcimadamore@1297 2337 checkEffectivelyFinal(tree, (VarSymbol)tree.sym);
mcimadamore@1297 2338 }
mcimadamore@1297 2339 }
mcimadamore@1297 2340
mcimadamore@1297 2341 public void visitAssign(JCAssign tree) {
mcimadamore@1297 2342 JCTree lhs = TreeInfo.skipParens(tree.lhs);
mcimadamore@1297 2343 if (!(lhs instanceof JCIdent)) {
mcimadamore@1297 2344 scan(lhs);
mcimadamore@1297 2345 }
mcimadamore@1297 2346 scan(tree.rhs);
mcimadamore@1297 2347 letInit(lhs);
mcimadamore@1297 2348 }
mcimadamore@1297 2349
mcimadamore@1297 2350 public void visitAssignop(JCAssignOp tree) {
mcimadamore@1297 2351 scan(tree.lhs);
mcimadamore@1297 2352 scan(tree.rhs);
mcimadamore@1297 2353 letInit(tree.lhs);
mcimadamore@1297 2354 }
mcimadamore@1297 2355
mcimadamore@1297 2356 public void visitUnary(JCUnary tree) {
mcimadamore@1297 2357 switch (tree.getTag()) {
mcimadamore@1297 2358 case PREINC: case POSTINC:
mcimadamore@1297 2359 case PREDEC: case POSTDEC:
mcimadamore@1297 2360 scan(tree.arg);
mcimadamore@1297 2361 letInit(tree.arg);
mcimadamore@1297 2362 break;
mcimadamore@1297 2363 default:
mcimadamore@1297 2364 scan(tree.arg);
mcimadamore@1297 2365 }
mcimadamore@1297 2366 }
mcimadamore@1297 2367
mcimadamore@1297 2368 public void visitTopLevel(JCCompilationUnit tree) {
mcimadamore@1297 2369 // Do nothing for TopLevel since each class is visited individually
mcimadamore@1297 2370 }
mcimadamore@1297 2371
mcimadamore@1297 2372 /**************************************************************************
mcimadamore@1297 2373 * main method
mcimadamore@1297 2374 *************************************************************************/
mcimadamore@1297 2375
mcimadamore@1297 2376 /** Perform definite assignment/unassignment analysis on a tree.
mcimadamore@1297 2377 */
mcimadamore@1297 2378 public void analyzeTree(Env<AttrContext> env, TreeMaker make) {
mcimadamore@1297 2379 analyzeTree(env, env.tree, make);
mcimadamore@1297 2380 }
mcimadamore@1297 2381 public void analyzeTree(Env<AttrContext> env, JCTree tree, TreeMaker make) {
mcimadamore@1297 2382 try {
mcimadamore@1297 2383 attrEnv = env;
mcimadamore@1297 2384 Flow.this.make = make;
mcimadamore@1297 2385 pendingExits = new ListBuffer<PendingExit>();
mcimadamore@1297 2386 scan(tree);
mcimadamore@1297 2387 } finally {
mcimadamore@1297 2388 pendingExits = null;
mcimadamore@1297 2389 Flow.this.make = null;
mcimadamore@1297 2390 }
mcimadamore@1297 2391 }
mcimadamore@1297 2392 }
duke@1 2393 }

mercurial