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