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

Fri, 19 Jul 2013 07:22:53 -0700

author
ksrini
date
Fri, 19 Jul 2013 07:22:53 -0700
changeset 1914
0a9f5cbe37d9
parent 1899
c60a5099863a
child 1955
ec77c7b46c37
permissions
-rw-r--r--

8017216: javac doesn't fill in end position for some errors of type not found
8019421: Javac doesn't fill in end position for some annotation related errors
8019422: Javac doesn't fill in end position for uninitialized variable errors
Reviewed-by: jjg, mcimadamore

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

mercurial