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

Tue, 13 Nov 2012 15:09:15 -0800

author
jjg
date
Tue, 13 Nov 2012 15:09:15 -0800
changeset 1406
2901c7b5339e
parent 1374
c002fdee76fd
child 1415
01c9d4161882
permissions
-rw-r--r--

8003299: Cleanup javac Log support for deferred diagnostics
Reviewed-by: mcimadamore, jfranck

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

mercurial