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

Wed, 10 Jun 2015 14:22:04 -0700

author
mfang
date
Wed, 10 Jun 2015 14:22:04 -0700
changeset 2815
d4051d4f5daf
parent 2606
f7f3f96999ba
child 2702
9ca8d8713094
child 2820
7f6d6b80a58b
permissions
-rw-r--r--

8083601: jdk8u60 l10n resource file translation update 2
Reviewed-by: ksrini, yhuang

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

mercurial