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

Fri, 18 Oct 2013 15:03:34 -0700

author
jjg
date
Fri, 18 Oct 2013 15:03:34 -0700
changeset 2146
7de97abc4a5c
parent 2135
d7e155f874a7
child 2252
fa004631cf00
permissions
-rw-r--r--

8026749: Missing LV table in lambda bodies
Reviewed-by: vromero, jlahoda

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

mercurial