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

Mon, 09 Sep 2013 23:13:45 +0200

author
jlahoda
date
Mon, 09 Sep 2013 23:13:45 +0200
changeset 2019
77d395862700
parent 1955
ec77c7b46c37
child 2027
4932bb04c4b8
permissions
-rw-r--r--

8019521: Enhanced rethrow disabled in lambdas
Summary: Fixing effectively final detection inside lambdas, small cleanup related to thrown types detection in lambdas
Reviewed-by: mcimadamore, jjg

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

mercurial