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

Thu, 21 Feb 2013 15:19:29 +0000

author
mcimadamore
date
Thu, 21 Feb 2013 15:19:29 +0000
changeset 1595
d686d8a7eb78
parent 1521
71f35e4b93a5
child 1693
c430f1cde21c
permissions
-rw-r--r--

8008227: Mixing lambdas with anonymous classes leads to NPE thrown by compiler
Summary: Disentangle cyclic dependency between static-ness of synthetic lambda method and static-ness of classes nested within lambdas
Reviewed-by: jjg

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

mercurial