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

Fri, 31 May 2013 10:04:59 +0100

author
vromero
date
Fri, 31 May 2013 10:04:59 +0100
changeset 1790
9f11c7676cd5
parent 1713
2ca9e7d50136
child 1802
8fb68f73d4b1
permissions
-rw-r--r--

7179353: try-with-resources fails to compile with generic exception parameters
Reviewed-by: mcimadamore

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

mercurial