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

Tue, 09 Oct 2012 19:31:58 -0700

author
jjg
date
Tue, 09 Oct 2012 19:31:58 -0700
changeset 1358
fc123bdeddb8
parent 1348
573ceb23beeb
child 1374
c002fdee76fd
permissions
-rw-r--r--

8000208: fix langtools javadoc comment issues
Reviewed-by: bpatel, mcimadamore

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

mercurial