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