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

Wed, 17 Jul 2013 14:13:15 +0100

author
mcimadamore
date
Wed, 17 Jul 2013 14:13:15 +0100
changeset 1899
c60a5099863a
parent 1879
3b4f92a3797f
child 1914
0a9f5cbe37d9
permissions
-rw-r--r--

8020147: Spurious errors when compiling nested stuck lambdas
Summary: Scope of deferred types is not copied correctly; postAttr analyzer should not run on stuck expressions
Reviewed-by: jjg

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

mercurial