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

Thu, 02 Aug 2012 18:23:21 +0100

author
mcimadamore
date
Thu, 02 Aug 2012 18:23:21 +0100
changeset 1297
e5cf1569d3a4
parent 1290
934a89402f85
child 1313
873ddd9f4900
permissions
-rw-r--r--

7175538: Integrate efectively final check with DA/DU analysis
Summary: Allow generalized effectively-final analysis for all local variables
Reviewed-by: jjg, dlsmith

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

mercurial