Sat, 18 Sep 2010 09:54:51 -0700
6980862: too aggressive compiler optimization causes stale results of Types.implementation()
Summary: use a scope counter in order to determine when/if the implementation cache entries are stale
Reviewed-by: jjg
duke@1 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 1999, 2009, 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; |
darcy@609 | 31 | import java.util.Map; |
darcy@609 | 32 | import java.util.LinkedHashMap; |
mcimadamore@550 | 33 | |
duke@1 | 34 | import com.sun.tools.javac.code.*; |
duke@1 | 35 | import com.sun.tools.javac.tree.*; |
duke@1 | 36 | import com.sun.tools.javac.util.*; |
duke@1 | 37 | import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition; |
duke@1 | 38 | |
duke@1 | 39 | import com.sun.tools.javac.code.Symbol.*; |
mcimadamore@617 | 40 | import com.sun.tools.javac.comp.Resolve; |
duke@1 | 41 | import com.sun.tools.javac.tree.JCTree.*; |
duke@1 | 42 | |
duke@1 | 43 | import static com.sun.tools.javac.code.Flags.*; |
duke@1 | 44 | import static com.sun.tools.javac.code.Kinds.*; |
duke@1 | 45 | import static com.sun.tools.javac.code.TypeTags.*; |
duke@1 | 46 | |
duke@1 | 47 | /** This pass implements dataflow analysis for Java programs. |
duke@1 | 48 | * Liveness analysis checks that every statement is reachable. |
duke@1 | 49 | * Exception analysis ensures that every checked exception that is |
duke@1 | 50 | * thrown is declared or caught. Definite assignment analysis |
duke@1 | 51 | * ensures that each variable is assigned when used. Definite |
duke@1 | 52 | * unassignment analysis ensures that no final variable is assigned |
duke@1 | 53 | * more than once. |
duke@1 | 54 | * |
duke@1 | 55 | * <p>The second edition of the JLS has a number of problems in the |
duke@1 | 56 | * specification of these flow analysis problems. This implementation |
duke@1 | 57 | * attempts to address those issues. |
duke@1 | 58 | * |
duke@1 | 59 | * <p>First, there is no accommodation for a finally clause that cannot |
duke@1 | 60 | * complete normally. For liveness analysis, an intervening finally |
duke@1 | 61 | * clause can cause a break, continue, or return not to reach its |
duke@1 | 62 | * target. For exception analysis, an intervening finally clause can |
duke@1 | 63 | * cause any exception to be "caught". For DA/DU analysis, the finally |
duke@1 | 64 | * clause can prevent a transfer of control from propagating DA/DU |
duke@1 | 65 | * state to the target. In addition, code in the finally clause can |
duke@1 | 66 | * affect the DA/DU status of variables. |
duke@1 | 67 | * |
duke@1 | 68 | * <p>For try statements, we introduce the idea of a variable being |
duke@1 | 69 | * definitely unassigned "everywhere" in a block. A variable V is |
duke@1 | 70 | * "unassigned everywhere" in a block iff it is unassigned at the |
duke@1 | 71 | * beginning of the block and there is no reachable assignment to V |
duke@1 | 72 | * in the block. An assignment V=e is reachable iff V is not DA |
duke@1 | 73 | * after e. Then we can say that V is DU at the beginning of the |
duke@1 | 74 | * catch block iff V is DU everywhere in the try block. Similarly, V |
duke@1 | 75 | * is DU at the beginning of the finally block iff V is DU everywhere |
duke@1 | 76 | * in the try block and in every catch block. Specifically, the |
duke@1 | 77 | * following bullet is added to 16.2.2 |
duke@1 | 78 | * <pre> |
duke@1 | 79 | * V is <em>unassigned everywhere</em> in a block if it is |
duke@1 | 80 | * unassigned before the block and there is no reachable |
duke@1 | 81 | * assignment to V within the block. |
duke@1 | 82 | * </pre> |
duke@1 | 83 | * <p>In 16.2.15, the third bullet (and all of its sub-bullets) for all |
duke@1 | 84 | * try blocks is changed to |
duke@1 | 85 | * <pre> |
duke@1 | 86 | * V is definitely unassigned before a catch block iff V is |
duke@1 | 87 | * definitely unassigned everywhere in the try block. |
duke@1 | 88 | * </pre> |
duke@1 | 89 | * <p>The last bullet (and all of its sub-bullets) for try blocks that |
duke@1 | 90 | * have a finally block is changed to |
duke@1 | 91 | * <pre> |
duke@1 | 92 | * V is definitely unassigned before the finally block iff |
duke@1 | 93 | * V is definitely unassigned everywhere in the try block |
duke@1 | 94 | * and everywhere in each catch block of the try statement. |
duke@1 | 95 | * </pre> |
duke@1 | 96 | * <p>In addition, |
duke@1 | 97 | * <pre> |
duke@1 | 98 | * V is definitely assigned at the end of a constructor iff |
duke@1 | 99 | * V is definitely assigned after the block that is the body |
duke@1 | 100 | * of the constructor and V is definitely assigned at every |
duke@1 | 101 | * return that can return from the constructor. |
duke@1 | 102 | * </pre> |
duke@1 | 103 | * <p>In addition, each continue statement with the loop as its target |
duke@1 | 104 | * is treated as a jump to the end of the loop body, and "intervening" |
duke@1 | 105 | * finally clauses are treated as follows: V is DA "due to the |
duke@1 | 106 | * continue" iff V is DA before the continue statement or V is DA at |
duke@1 | 107 | * the end of any intervening finally block. V is DU "due to the |
duke@1 | 108 | * continue" iff any intervening finally cannot complete normally or V |
duke@1 | 109 | * is DU at the end of every intervening finally block. This "due to |
duke@1 | 110 | * the continue" concept is then used in the spec for the loops. |
duke@1 | 111 | * |
duke@1 | 112 | * <p>Similarly, break statements must consider intervening finally |
duke@1 | 113 | * blocks. For liveness analysis, a break statement for which any |
duke@1 | 114 | * intervening finally cannot complete normally is not considered to |
duke@1 | 115 | * cause the target statement to be able to complete normally. Then |
duke@1 | 116 | * we say V is DA "due to the break" iff V is DA before the break or |
duke@1 | 117 | * V is DA at the end of any intervening finally block. V is DU "due |
duke@1 | 118 | * to the break" iff any intervening finally cannot complete normally |
duke@1 | 119 | * or V is DU at the break and at the end of every intervening |
duke@1 | 120 | * finally block. (I suspect this latter condition can be |
duke@1 | 121 | * simplified.) This "due to the break" is then used in the spec for |
duke@1 | 122 | * all statements that can be "broken". |
duke@1 | 123 | * |
duke@1 | 124 | * <p>The return statement is treated similarly. V is DA "due to a |
duke@1 | 125 | * return statement" iff V is DA before the return statement or V is |
duke@1 | 126 | * DA at the end of any intervening finally block. Note that we |
duke@1 | 127 | * don't have to worry about the return expression because this |
duke@1 | 128 | * concept is only used for construcrors. |
duke@1 | 129 | * |
duke@1 | 130 | * <p>There is no spec in JLS2 for when a variable is definitely |
duke@1 | 131 | * assigned at the end of a constructor, which is needed for final |
duke@1 | 132 | * fields (8.3.1.2). We implement the rule that V is DA at the end |
duke@1 | 133 | * of the constructor iff it is DA and the end of the body of the |
duke@1 | 134 | * constructor and V is DA "due to" every return of the constructor. |
duke@1 | 135 | * |
duke@1 | 136 | * <p>Intervening finally blocks similarly affect exception analysis. An |
duke@1 | 137 | * intervening finally that cannot complete normally allows us to ignore |
duke@1 | 138 | * an otherwise uncaught exception. |
duke@1 | 139 | * |
duke@1 | 140 | * <p>To implement the semantics of intervening finally clauses, all |
duke@1 | 141 | * nonlocal transfers (break, continue, return, throw, method call that |
duke@1 | 142 | * can throw a checked exception, and a constructor invocation that can |
duke@1 | 143 | * thrown a checked exception) are recorded in a queue, and removed |
duke@1 | 144 | * from the queue when we complete processing the target of the |
duke@1 | 145 | * nonlocal transfer. This allows us to modify the queue in accordance |
duke@1 | 146 | * with the above rules when we encounter a finally clause. The only |
duke@1 | 147 | * exception to this [no pun intended] is that checked exceptions that |
duke@1 | 148 | * are known to be caught or declared to be caught in the enclosing |
duke@1 | 149 | * method are not recorded in the queue, but instead are recorded in a |
duke@1 | 150 | * global variable "Set<Type> thrown" that records the type of all |
duke@1 | 151 | * exceptions that can be thrown. |
duke@1 | 152 | * |
duke@1 | 153 | * <p>Other minor issues the treatment of members of other classes |
duke@1 | 154 | * (always considered DA except that within an anonymous class |
duke@1 | 155 | * constructor, where DA status from the enclosing scope is |
duke@1 | 156 | * preserved), treatment of the case expression (V is DA before the |
duke@1 | 157 | * case expression iff V is DA after the switch expression), |
duke@1 | 158 | * treatment of variables declared in a switch block (the implied |
duke@1 | 159 | * DA/DU status after the switch expression is DU and not DA for |
duke@1 | 160 | * variables defined in a switch block), the treatment of boolean ?: |
duke@1 | 161 | * expressions (The JLS rules only handle b and c non-boolean; the |
duke@1 | 162 | * new rule is that if b and c are boolean valued, then V is |
duke@1 | 163 | * (un)assigned after a?b:c when true/false iff V is (un)assigned |
duke@1 | 164 | * after b when true/false and V is (un)assigned after c when |
duke@1 | 165 | * true/false). |
duke@1 | 166 | * |
duke@1 | 167 | * <p>There is the remaining question of what syntactic forms constitute a |
duke@1 | 168 | * reference to a variable. It is conventional to allow this.x on the |
duke@1 | 169 | * left-hand-side to initialize a final instance field named x, yet |
duke@1 | 170 | * this.x isn't considered a "use" when appearing on a right-hand-side |
duke@1 | 171 | * in most implementations. Should parentheses affect what is |
duke@1 | 172 | * considered a variable reference? The simplest rule would be to |
duke@1 | 173 | * allow unqualified forms only, parentheses optional, and phase out |
duke@1 | 174 | * support for assigning to a final field via this.x. |
duke@1 | 175 | * |
jjg@581 | 176 | * <p><b>This is NOT part of any supported API. |
jjg@581 | 177 | * If you write code that depends on this, you do so at your own risk. |
duke@1 | 178 | * This code and its internal interfaces are subject to change or |
duke@1 | 179 | * deletion without notice.</b> |
duke@1 | 180 | */ |
duke@1 | 181 | public class Flow extends TreeScanner { |
duke@1 | 182 | protected static final Context.Key<Flow> flowKey = |
duke@1 | 183 | new Context.Key<Flow>(); |
duke@1 | 184 | |
jjg@113 | 185 | private final Names names; |
duke@1 | 186 | private final Log log; |
duke@1 | 187 | private final Symtab syms; |
duke@1 | 188 | private final Types types; |
duke@1 | 189 | private final Check chk; |
duke@1 | 190 | private TreeMaker make; |
mcimadamore@617 | 191 | private final Resolve rs; |
mcimadamore@617 | 192 | private Env<AttrContext> attrEnv; |
duke@1 | 193 | private Lint lint; |
mcimadamore@550 | 194 | private final boolean allowRethrowAnalysis; |
duke@1 | 195 | |
duke@1 | 196 | public static Flow instance(Context context) { |
duke@1 | 197 | Flow instance = context.get(flowKey); |
duke@1 | 198 | if (instance == null) |
duke@1 | 199 | instance = new Flow(context); |
duke@1 | 200 | return instance; |
duke@1 | 201 | } |
duke@1 | 202 | |
duke@1 | 203 | protected Flow(Context context) { |
duke@1 | 204 | context.put(flowKey, this); |
jjg@113 | 205 | names = Names.instance(context); |
duke@1 | 206 | log = Log.instance(context); |
duke@1 | 207 | syms = Symtab.instance(context); |
duke@1 | 208 | types = Types.instance(context); |
duke@1 | 209 | chk = Check.instance(context); |
duke@1 | 210 | lint = Lint.instance(context); |
mcimadamore@617 | 211 | rs = Resolve.instance(context); |
mcimadamore@550 | 212 | Source source = Source.instance(context); |
mcimadamore@550 | 213 | allowRethrowAnalysis = source.allowMulticatch(); |
duke@1 | 214 | } |
duke@1 | 215 | |
duke@1 | 216 | /** A flag that indicates whether the last statement could |
duke@1 | 217 | * complete normally. |
duke@1 | 218 | */ |
duke@1 | 219 | private boolean alive; |
duke@1 | 220 | |
duke@1 | 221 | /** The set of definitely assigned variables. |
duke@1 | 222 | */ |
duke@1 | 223 | Bits inits; |
duke@1 | 224 | |
duke@1 | 225 | /** The set of definitely unassigned variables. |
duke@1 | 226 | */ |
duke@1 | 227 | Bits uninits; |
duke@1 | 228 | |
mcimadamore@550 | 229 | HashMap<Symbol, List<Type>> multicatchTypes; |
mcimadamore@550 | 230 | |
duke@1 | 231 | /** The set of variables that are definitely unassigned everywhere |
duke@1 | 232 | * in current try block. This variable is maintained lazily; it is |
duke@1 | 233 | * updated only when something gets removed from uninits, |
duke@1 | 234 | * typically by being assigned in reachable code. To obtain the |
duke@1 | 235 | * correct set of variables which are definitely unassigned |
duke@1 | 236 | * anywhere in current try block, intersect uninitsTry and |
duke@1 | 237 | * uninits. |
duke@1 | 238 | */ |
duke@1 | 239 | Bits uninitsTry; |
duke@1 | 240 | |
duke@1 | 241 | /** When analyzing a condition, inits and uninits are null. |
duke@1 | 242 | * Instead we have: |
duke@1 | 243 | */ |
duke@1 | 244 | Bits initsWhenTrue; |
duke@1 | 245 | Bits initsWhenFalse; |
duke@1 | 246 | Bits uninitsWhenTrue; |
duke@1 | 247 | Bits uninitsWhenFalse; |
duke@1 | 248 | |
duke@1 | 249 | /** A mapping from addresses to variable symbols. |
duke@1 | 250 | */ |
duke@1 | 251 | VarSymbol[] vars; |
duke@1 | 252 | |
duke@1 | 253 | /** The current class being defined. |
duke@1 | 254 | */ |
duke@1 | 255 | JCClassDecl classDef; |
duke@1 | 256 | |
duke@1 | 257 | /** The first variable sequence number in this class definition. |
duke@1 | 258 | */ |
duke@1 | 259 | int firstadr; |
duke@1 | 260 | |
duke@1 | 261 | /** The next available variable sequence number. |
duke@1 | 262 | */ |
duke@1 | 263 | int nextadr; |
duke@1 | 264 | |
duke@1 | 265 | /** The list of possibly thrown declarable exceptions. |
duke@1 | 266 | */ |
duke@1 | 267 | List<Type> thrown; |
duke@1 | 268 | |
duke@1 | 269 | /** The list of exceptions that are either caught or declared to be |
duke@1 | 270 | * thrown. |
duke@1 | 271 | */ |
duke@1 | 272 | List<Type> caught; |
duke@1 | 273 | |
darcy@609 | 274 | /** The list of unreferenced automatic resources. |
darcy@609 | 275 | */ |
darcy@609 | 276 | Map<VarSymbol, JCVariableDecl> unrefdResources; |
darcy@609 | 277 | |
duke@1 | 278 | /** Set when processing a loop body the second time for DU analysis. */ |
duke@1 | 279 | boolean loopPassTwo = false; |
duke@1 | 280 | |
duke@1 | 281 | /*-------------------- Environments ----------------------*/ |
duke@1 | 282 | |
duke@1 | 283 | /** A pending exit. These are the statements return, break, and |
duke@1 | 284 | * continue. In addition, exception-throwing expressions or |
duke@1 | 285 | * statements are put here when not known to be caught. This |
duke@1 | 286 | * will typically result in an error unless it is within a |
duke@1 | 287 | * try-finally whose finally block cannot complete normally. |
duke@1 | 288 | */ |
duke@1 | 289 | static class PendingExit { |
duke@1 | 290 | JCTree tree; |
duke@1 | 291 | Bits inits; |
duke@1 | 292 | Bits uninits; |
duke@1 | 293 | Type thrown; |
duke@1 | 294 | PendingExit(JCTree tree, Bits inits, Bits uninits) { |
duke@1 | 295 | this.tree = tree; |
duke@1 | 296 | this.inits = inits.dup(); |
duke@1 | 297 | this.uninits = uninits.dup(); |
duke@1 | 298 | } |
duke@1 | 299 | PendingExit(JCTree tree, Type thrown) { |
duke@1 | 300 | this.tree = tree; |
duke@1 | 301 | this.thrown = thrown; |
duke@1 | 302 | } |
duke@1 | 303 | } |
duke@1 | 304 | |
duke@1 | 305 | /** The currently pending exits that go from current inner blocks |
duke@1 | 306 | * to an enclosing block, in source order. |
duke@1 | 307 | */ |
duke@1 | 308 | ListBuffer<PendingExit> pendingExits; |
duke@1 | 309 | |
duke@1 | 310 | /*-------------------- Exceptions ----------------------*/ |
duke@1 | 311 | |
duke@1 | 312 | /** Complain that pending exceptions are not caught. |
duke@1 | 313 | */ |
duke@1 | 314 | void errorUncaught() { |
duke@1 | 315 | for (PendingExit exit = pendingExits.next(); |
duke@1 | 316 | exit != null; |
duke@1 | 317 | exit = pendingExits.next()) { |
duke@1 | 318 | boolean synthetic = classDef != null && |
duke@1 | 319 | classDef.pos == exit.tree.pos; |
duke@1 | 320 | log.error(exit.tree.pos(), |
duke@1 | 321 | synthetic |
duke@1 | 322 | ? "unreported.exception.default.constructor" |
duke@1 | 323 | : "unreported.exception.need.to.catch.or.throw", |
duke@1 | 324 | exit.thrown); |
duke@1 | 325 | } |
duke@1 | 326 | } |
duke@1 | 327 | |
duke@1 | 328 | /** Record that exception is potentially thrown and check that it |
duke@1 | 329 | * is caught. |
duke@1 | 330 | */ |
duke@1 | 331 | void markThrown(JCTree tree, Type exc) { |
duke@1 | 332 | if (!chk.isUnchecked(tree.pos(), exc)) { |
duke@1 | 333 | if (!chk.isHandled(exc, caught)) |
duke@1 | 334 | pendingExits.append(new PendingExit(tree, exc)); |
duke@1 | 335 | thrown = chk.incl(exc, thrown); |
duke@1 | 336 | } |
duke@1 | 337 | } |
duke@1 | 338 | |
duke@1 | 339 | /*-------------- Processing variables ----------------------*/ |
duke@1 | 340 | |
duke@1 | 341 | /** Do we need to track init/uninit state of this symbol? |
duke@1 | 342 | * I.e. is symbol either a local or a blank final variable? |
duke@1 | 343 | */ |
duke@1 | 344 | boolean trackable(VarSymbol sym) { |
duke@1 | 345 | return |
duke@1 | 346 | (sym.owner.kind == MTH || |
duke@1 | 347 | ((sym.flags() & (FINAL | HASINIT | PARAMETER)) == FINAL && |
duke@1 | 348 | classDef.sym.isEnclosedBy((ClassSymbol)sym.owner))); |
duke@1 | 349 | } |
duke@1 | 350 | |
duke@1 | 351 | /** Initialize new trackable variable by setting its address field |
duke@1 | 352 | * to the next available sequence number and entering it under that |
duke@1 | 353 | * index into the vars array. |
duke@1 | 354 | */ |
duke@1 | 355 | void newVar(VarSymbol sym) { |
duke@1 | 356 | if (nextadr == vars.length) { |
duke@1 | 357 | VarSymbol[] newvars = new VarSymbol[nextadr * 2]; |
duke@1 | 358 | System.arraycopy(vars, 0, newvars, 0, nextadr); |
duke@1 | 359 | vars = newvars; |
duke@1 | 360 | } |
duke@1 | 361 | sym.adr = nextadr; |
duke@1 | 362 | vars[nextadr] = sym; |
duke@1 | 363 | inits.excl(nextadr); |
duke@1 | 364 | uninits.incl(nextadr); |
duke@1 | 365 | nextadr++; |
duke@1 | 366 | } |
duke@1 | 367 | |
duke@1 | 368 | /** Record an initialization of a trackable variable. |
duke@1 | 369 | */ |
duke@1 | 370 | void letInit(DiagnosticPosition pos, VarSymbol sym) { |
duke@1 | 371 | if (sym.adr >= firstadr && trackable(sym)) { |
duke@1 | 372 | if ((sym.flags() & FINAL) != 0) { |
duke@1 | 373 | if ((sym.flags() & PARAMETER) != 0) { |
mcimadamore@550 | 374 | if ((sym.flags() & DISJOINT) != 0) { //multi-catch parameter |
mcimadamore@550 | 375 | log.error(pos, "multicatch.parameter.may.not.be.assigned", |
mcimadamore@550 | 376 | sym); |
mcimadamore@550 | 377 | } |
mcimadamore@550 | 378 | else { |
mcimadamore@550 | 379 | log.error(pos, "final.parameter.may.not.be.assigned", |
duke@1 | 380 | sym); |
mcimadamore@550 | 381 | } |
duke@1 | 382 | } else if (!uninits.isMember(sym.adr)) { |
duke@1 | 383 | log.error(pos, |
duke@1 | 384 | loopPassTwo |
duke@1 | 385 | ? "var.might.be.assigned.in.loop" |
duke@1 | 386 | : "var.might.already.be.assigned", |
duke@1 | 387 | sym); |
duke@1 | 388 | } else if (!inits.isMember(sym.adr)) { |
duke@1 | 389 | // reachable assignment |
duke@1 | 390 | uninits.excl(sym.adr); |
duke@1 | 391 | uninitsTry.excl(sym.adr); |
duke@1 | 392 | } else { |
duke@1 | 393 | //log.rawWarning(pos, "unreachable assignment");//DEBUG |
duke@1 | 394 | uninits.excl(sym.adr); |
duke@1 | 395 | } |
duke@1 | 396 | } |
duke@1 | 397 | inits.incl(sym.adr); |
duke@1 | 398 | } else if ((sym.flags() & FINAL) != 0) { |
duke@1 | 399 | log.error(pos, "var.might.already.be.assigned", sym); |
duke@1 | 400 | } |
duke@1 | 401 | } |
duke@1 | 402 | |
duke@1 | 403 | /** If tree is either a simple name or of the form this.name or |
duke@1 | 404 | * C.this.name, and tree represents a trackable variable, |
duke@1 | 405 | * record an initialization of the variable. |
duke@1 | 406 | */ |
duke@1 | 407 | void letInit(JCTree tree) { |
duke@1 | 408 | tree = TreeInfo.skipParens(tree); |
duke@1 | 409 | if (tree.getTag() == JCTree.IDENT || tree.getTag() == JCTree.SELECT) { |
duke@1 | 410 | Symbol sym = TreeInfo.symbol(tree); |
mcimadamore@676 | 411 | if (sym.kind == VAR) { |
mcimadamore@676 | 412 | letInit(tree.pos(), (VarSymbol)sym); |
mcimadamore@676 | 413 | } |
duke@1 | 414 | } |
duke@1 | 415 | } |
duke@1 | 416 | |
duke@1 | 417 | /** Check that trackable variable is initialized. |
duke@1 | 418 | */ |
duke@1 | 419 | void checkInit(DiagnosticPosition pos, VarSymbol sym) { |
duke@1 | 420 | if ((sym.adr >= firstadr || sym.owner.kind != TYP) && |
duke@1 | 421 | trackable(sym) && |
duke@1 | 422 | !inits.isMember(sym.adr)) { |
duke@1 | 423 | log.error(pos, "var.might.not.have.been.initialized", |
duke@1 | 424 | sym); |
duke@1 | 425 | inits.incl(sym.adr); |
duke@1 | 426 | } |
duke@1 | 427 | } |
duke@1 | 428 | |
duke@1 | 429 | /*-------------------- Handling jumps ----------------------*/ |
duke@1 | 430 | |
duke@1 | 431 | /** Record an outward transfer of control. */ |
duke@1 | 432 | void recordExit(JCTree tree) { |
duke@1 | 433 | pendingExits.append(new PendingExit(tree, inits, uninits)); |
duke@1 | 434 | markDead(); |
duke@1 | 435 | } |
duke@1 | 436 | |
duke@1 | 437 | /** Resolve all breaks of this statement. */ |
duke@1 | 438 | boolean resolveBreaks(JCTree tree, |
duke@1 | 439 | ListBuffer<PendingExit> oldPendingExits) { |
duke@1 | 440 | boolean result = false; |
duke@1 | 441 | List<PendingExit> exits = pendingExits.toList(); |
duke@1 | 442 | pendingExits = oldPendingExits; |
duke@1 | 443 | for (; exits.nonEmpty(); exits = exits.tail) { |
duke@1 | 444 | PendingExit exit = exits.head; |
duke@1 | 445 | if (exit.tree.getTag() == JCTree.BREAK && |
duke@1 | 446 | ((JCBreak) exit.tree).target == tree) { |
duke@1 | 447 | inits.andSet(exit.inits); |
duke@1 | 448 | uninits.andSet(exit.uninits); |
duke@1 | 449 | result = true; |
duke@1 | 450 | } else { |
duke@1 | 451 | pendingExits.append(exit); |
duke@1 | 452 | } |
duke@1 | 453 | } |
duke@1 | 454 | return result; |
duke@1 | 455 | } |
duke@1 | 456 | |
duke@1 | 457 | /** Resolve all continues of this statement. */ |
duke@1 | 458 | boolean resolveContinues(JCTree tree) { |
duke@1 | 459 | boolean result = false; |
duke@1 | 460 | List<PendingExit> exits = pendingExits.toList(); |
duke@1 | 461 | pendingExits = new ListBuffer<PendingExit>(); |
duke@1 | 462 | for (; exits.nonEmpty(); exits = exits.tail) { |
duke@1 | 463 | PendingExit exit = exits.head; |
duke@1 | 464 | if (exit.tree.getTag() == JCTree.CONTINUE && |
duke@1 | 465 | ((JCContinue) exit.tree).target == tree) { |
duke@1 | 466 | inits.andSet(exit.inits); |
duke@1 | 467 | uninits.andSet(exit.uninits); |
duke@1 | 468 | result = true; |
duke@1 | 469 | } else { |
duke@1 | 470 | pendingExits.append(exit); |
duke@1 | 471 | } |
duke@1 | 472 | } |
duke@1 | 473 | return result; |
duke@1 | 474 | } |
duke@1 | 475 | |
duke@1 | 476 | /** Record that statement is unreachable. |
duke@1 | 477 | */ |
duke@1 | 478 | void markDead() { |
duke@1 | 479 | inits.inclRange(firstadr, nextadr); |
duke@1 | 480 | uninits.inclRange(firstadr, nextadr); |
duke@1 | 481 | alive = false; |
duke@1 | 482 | } |
duke@1 | 483 | |
duke@1 | 484 | /** Split (duplicate) inits/uninits into WhenTrue/WhenFalse sets |
duke@1 | 485 | */ |
mcimadamore@676 | 486 | void split(boolean setToNull) { |
duke@1 | 487 | initsWhenFalse = inits.dup(); |
duke@1 | 488 | uninitsWhenFalse = uninits.dup(); |
duke@1 | 489 | initsWhenTrue = inits; |
duke@1 | 490 | uninitsWhenTrue = uninits; |
mcimadamore@676 | 491 | if (setToNull) |
mcimadamore@676 | 492 | inits = uninits = null; |
duke@1 | 493 | } |
duke@1 | 494 | |
duke@1 | 495 | /** Merge (intersect) inits/uninits from WhenTrue/WhenFalse sets. |
duke@1 | 496 | */ |
duke@1 | 497 | void merge() { |
duke@1 | 498 | inits = initsWhenFalse.andSet(initsWhenTrue); |
duke@1 | 499 | uninits = uninitsWhenFalse.andSet(uninitsWhenTrue); |
duke@1 | 500 | } |
duke@1 | 501 | |
duke@1 | 502 | /* ************************************************************************ |
duke@1 | 503 | * Visitor methods for statements and definitions |
duke@1 | 504 | *************************************************************************/ |
duke@1 | 505 | |
duke@1 | 506 | /** Analyze a definition. |
duke@1 | 507 | */ |
duke@1 | 508 | void scanDef(JCTree tree) { |
duke@1 | 509 | scanStat(tree); |
duke@1 | 510 | if (tree != null && tree.getTag() == JCTree.BLOCK && !alive) { |
duke@1 | 511 | log.error(tree.pos(), |
duke@1 | 512 | "initializer.must.be.able.to.complete.normally"); |
duke@1 | 513 | } |
duke@1 | 514 | } |
duke@1 | 515 | |
duke@1 | 516 | /** Analyze a statement. Check that statement is reachable. |
duke@1 | 517 | */ |
duke@1 | 518 | void scanStat(JCTree tree) { |
duke@1 | 519 | if (!alive && tree != null) { |
duke@1 | 520 | log.error(tree.pos(), "unreachable.stmt"); |
duke@1 | 521 | if (tree.getTag() != JCTree.SKIP) alive = true; |
duke@1 | 522 | } |
duke@1 | 523 | scan(tree); |
duke@1 | 524 | } |
duke@1 | 525 | |
duke@1 | 526 | /** Analyze list of statements. |
duke@1 | 527 | */ |
duke@1 | 528 | void scanStats(List<? extends JCStatement> trees) { |
duke@1 | 529 | if (trees != null) |
duke@1 | 530 | for (List<? extends JCStatement> l = trees; l.nonEmpty(); l = l.tail) |
duke@1 | 531 | scanStat(l.head); |
duke@1 | 532 | } |
duke@1 | 533 | |
duke@1 | 534 | /** Analyze an expression. Make sure to set (un)inits rather than |
duke@1 | 535 | * (un)initsWhenTrue(WhenFalse) on exit. |
duke@1 | 536 | */ |
duke@1 | 537 | void scanExpr(JCTree tree) { |
duke@1 | 538 | if (tree != null) { |
duke@1 | 539 | scan(tree); |
duke@1 | 540 | if (inits == null) merge(); |
duke@1 | 541 | } |
duke@1 | 542 | } |
duke@1 | 543 | |
duke@1 | 544 | /** Analyze a list of expressions. |
duke@1 | 545 | */ |
duke@1 | 546 | void scanExprs(List<? extends JCExpression> trees) { |
duke@1 | 547 | if (trees != null) |
duke@1 | 548 | for (List<? extends JCExpression> l = trees; l.nonEmpty(); l = l.tail) |
duke@1 | 549 | scanExpr(l.head); |
duke@1 | 550 | } |
duke@1 | 551 | |
duke@1 | 552 | /** Analyze a condition. Make sure to set (un)initsWhenTrue(WhenFalse) |
duke@1 | 553 | * rather than (un)inits on exit. |
duke@1 | 554 | */ |
duke@1 | 555 | void scanCond(JCTree tree) { |
duke@1 | 556 | if (tree.type.isFalse()) { |
duke@1 | 557 | if (inits == null) merge(); |
duke@1 | 558 | initsWhenTrue = inits.dup(); |
duke@1 | 559 | initsWhenTrue.inclRange(firstadr, nextadr); |
duke@1 | 560 | uninitsWhenTrue = uninits.dup(); |
duke@1 | 561 | uninitsWhenTrue.inclRange(firstadr, nextadr); |
duke@1 | 562 | initsWhenFalse = inits; |
duke@1 | 563 | uninitsWhenFalse = uninits; |
duke@1 | 564 | } else if (tree.type.isTrue()) { |
duke@1 | 565 | if (inits == null) merge(); |
duke@1 | 566 | initsWhenFalse = inits.dup(); |
duke@1 | 567 | initsWhenFalse.inclRange(firstadr, nextadr); |
duke@1 | 568 | uninitsWhenFalse = uninits.dup(); |
duke@1 | 569 | uninitsWhenFalse.inclRange(firstadr, nextadr); |
duke@1 | 570 | initsWhenTrue = inits; |
duke@1 | 571 | uninitsWhenTrue = uninits; |
duke@1 | 572 | } else { |
duke@1 | 573 | scan(tree); |
mcimadamore@676 | 574 | if (inits != null) |
mcimadamore@676 | 575 | split(tree.type != syms.unknownType); |
duke@1 | 576 | } |
mcimadamore@676 | 577 | if (tree.type != syms.unknownType) |
mcimadamore@676 | 578 | inits = uninits = null; |
duke@1 | 579 | } |
duke@1 | 580 | |
duke@1 | 581 | /* ------------ Visitor methods for various sorts of trees -------------*/ |
duke@1 | 582 | |
duke@1 | 583 | public void visitClassDef(JCClassDecl tree) { |
duke@1 | 584 | if (tree.sym == null) return; |
duke@1 | 585 | |
duke@1 | 586 | JCClassDecl classDefPrev = classDef; |
duke@1 | 587 | List<Type> thrownPrev = thrown; |
duke@1 | 588 | List<Type> caughtPrev = caught; |
duke@1 | 589 | boolean alivePrev = alive; |
duke@1 | 590 | int firstadrPrev = firstadr; |
duke@1 | 591 | int nextadrPrev = nextadr; |
duke@1 | 592 | ListBuffer<PendingExit> pendingExitsPrev = pendingExits; |
duke@1 | 593 | Lint lintPrev = lint; |
duke@1 | 594 | |
duke@1 | 595 | pendingExits = new ListBuffer<PendingExit>(); |
duke@1 | 596 | if (tree.name != names.empty) { |
duke@1 | 597 | caught = List.nil(); |
duke@1 | 598 | firstadr = nextadr; |
duke@1 | 599 | } |
duke@1 | 600 | classDef = tree; |
duke@1 | 601 | thrown = List.nil(); |
duke@1 | 602 | lint = lint.augment(tree.sym.attributes_field); |
duke@1 | 603 | |
duke@1 | 604 | try { |
duke@1 | 605 | // define all the static fields |
duke@1 | 606 | for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) { |
duke@1 | 607 | if (l.head.getTag() == JCTree.VARDEF) { |
duke@1 | 608 | JCVariableDecl def = (JCVariableDecl)l.head; |
duke@1 | 609 | if ((def.mods.flags & STATIC) != 0) { |
duke@1 | 610 | VarSymbol sym = def.sym; |
duke@1 | 611 | if (trackable(sym)) |
duke@1 | 612 | newVar(sym); |
duke@1 | 613 | } |
duke@1 | 614 | } |
duke@1 | 615 | } |
duke@1 | 616 | |
duke@1 | 617 | // process all the static initializers |
duke@1 | 618 | for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) { |
duke@1 | 619 | if (l.head.getTag() != JCTree.METHODDEF && |
duke@1 | 620 | (TreeInfo.flags(l.head) & STATIC) != 0) { |
duke@1 | 621 | scanDef(l.head); |
duke@1 | 622 | errorUncaught(); |
duke@1 | 623 | } |
duke@1 | 624 | } |
duke@1 | 625 | |
duke@1 | 626 | // add intersection of all thrown clauses of initial constructors |
duke@1 | 627 | // to set of caught exceptions, unless class is anonymous. |
duke@1 | 628 | if (tree.name != names.empty) { |
duke@1 | 629 | boolean firstConstructor = true; |
duke@1 | 630 | for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) { |
duke@1 | 631 | if (TreeInfo.isInitialConstructor(l.head)) { |
duke@1 | 632 | List<Type> mthrown = |
duke@1 | 633 | ((JCMethodDecl) l.head).sym.type.getThrownTypes(); |
duke@1 | 634 | if (firstConstructor) { |
duke@1 | 635 | caught = mthrown; |
duke@1 | 636 | firstConstructor = false; |
duke@1 | 637 | } else { |
duke@1 | 638 | caught = chk.intersect(mthrown, caught); |
duke@1 | 639 | } |
duke@1 | 640 | } |
duke@1 | 641 | } |
duke@1 | 642 | } |
duke@1 | 643 | |
duke@1 | 644 | // define all the instance fields |
duke@1 | 645 | for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) { |
duke@1 | 646 | if (l.head.getTag() == JCTree.VARDEF) { |
duke@1 | 647 | JCVariableDecl def = (JCVariableDecl)l.head; |
duke@1 | 648 | if ((def.mods.flags & STATIC) == 0) { |
duke@1 | 649 | VarSymbol sym = def.sym; |
duke@1 | 650 | if (trackable(sym)) |
duke@1 | 651 | newVar(sym); |
duke@1 | 652 | } |
duke@1 | 653 | } |
duke@1 | 654 | } |
duke@1 | 655 | |
duke@1 | 656 | // process all the instance initializers |
duke@1 | 657 | for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) { |
duke@1 | 658 | if (l.head.getTag() != JCTree.METHODDEF && |
duke@1 | 659 | (TreeInfo.flags(l.head) & STATIC) == 0) { |
duke@1 | 660 | scanDef(l.head); |
duke@1 | 661 | errorUncaught(); |
duke@1 | 662 | } |
duke@1 | 663 | } |
duke@1 | 664 | |
duke@1 | 665 | // in an anonymous class, add the set of thrown exceptions to |
duke@1 | 666 | // the throws clause of the synthetic constructor and propagate |
duke@1 | 667 | // outwards. |
duke@1 | 668 | if (tree.name == names.empty) { |
duke@1 | 669 | for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) { |
duke@1 | 670 | if (TreeInfo.isInitialConstructor(l.head)) { |
duke@1 | 671 | JCMethodDecl mdef = (JCMethodDecl)l.head; |
duke@1 | 672 | mdef.thrown = make.Types(thrown); |
duke@1 | 673 | mdef.sym.type.setThrown(thrown); |
duke@1 | 674 | } |
duke@1 | 675 | } |
duke@1 | 676 | thrownPrev = chk.union(thrown, thrownPrev); |
duke@1 | 677 | } |
duke@1 | 678 | |
duke@1 | 679 | // process all the methods |
duke@1 | 680 | for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) { |
duke@1 | 681 | if (l.head.getTag() == JCTree.METHODDEF) { |
duke@1 | 682 | scan(l.head); |
duke@1 | 683 | errorUncaught(); |
duke@1 | 684 | } |
duke@1 | 685 | } |
duke@1 | 686 | |
duke@1 | 687 | thrown = thrownPrev; |
duke@1 | 688 | } finally { |
duke@1 | 689 | pendingExits = pendingExitsPrev; |
duke@1 | 690 | alive = alivePrev; |
duke@1 | 691 | nextadr = nextadrPrev; |
duke@1 | 692 | firstadr = firstadrPrev; |
duke@1 | 693 | caught = caughtPrev; |
duke@1 | 694 | classDef = classDefPrev; |
duke@1 | 695 | lint = lintPrev; |
duke@1 | 696 | } |
duke@1 | 697 | } |
duke@1 | 698 | |
duke@1 | 699 | public void visitMethodDef(JCMethodDecl tree) { |
duke@1 | 700 | if (tree.body == null) return; |
duke@1 | 701 | |
duke@1 | 702 | List<Type> caughtPrev = caught; |
duke@1 | 703 | List<Type> mthrown = tree.sym.type.getThrownTypes(); |
duke@1 | 704 | Bits initsPrev = inits.dup(); |
duke@1 | 705 | Bits uninitsPrev = uninits.dup(); |
duke@1 | 706 | int nextadrPrev = nextadr; |
duke@1 | 707 | int firstadrPrev = firstadr; |
duke@1 | 708 | Lint lintPrev = lint; |
duke@1 | 709 | |
duke@1 | 710 | lint = lint.augment(tree.sym.attributes_field); |
duke@1 | 711 | |
duke@1 | 712 | assert pendingExits.isEmpty(); |
duke@1 | 713 | |
duke@1 | 714 | try { |
duke@1 | 715 | boolean isInitialConstructor = |
duke@1 | 716 | TreeInfo.isInitialConstructor(tree); |
duke@1 | 717 | |
duke@1 | 718 | if (!isInitialConstructor) |
duke@1 | 719 | firstadr = nextadr; |
duke@1 | 720 | for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) { |
duke@1 | 721 | JCVariableDecl def = l.head; |
duke@1 | 722 | scan(def); |
duke@1 | 723 | inits.incl(def.sym.adr); |
duke@1 | 724 | uninits.excl(def.sym.adr); |
duke@1 | 725 | } |
duke@1 | 726 | if (isInitialConstructor) |
duke@1 | 727 | caught = chk.union(caught, mthrown); |
duke@1 | 728 | else if ((tree.sym.flags() & (BLOCK | STATIC)) != BLOCK) |
duke@1 | 729 | caught = mthrown; |
duke@1 | 730 | // else we are in an instance initializer block; |
duke@1 | 731 | // leave caught unchanged. |
duke@1 | 732 | |
duke@1 | 733 | alive = true; |
duke@1 | 734 | scanStat(tree.body); |
duke@1 | 735 | |
duke@1 | 736 | if (alive && tree.sym.type.getReturnType().tag != VOID) |
duke@1 | 737 | log.error(TreeInfo.diagEndPos(tree.body), "missing.ret.stmt"); |
duke@1 | 738 | |
duke@1 | 739 | if (isInitialConstructor) { |
duke@1 | 740 | for (int i = firstadr; i < nextadr; i++) |
duke@1 | 741 | if (vars[i].owner == classDef.sym) |
duke@1 | 742 | checkInit(TreeInfo.diagEndPos(tree.body), vars[i]); |
duke@1 | 743 | } |
duke@1 | 744 | List<PendingExit> exits = pendingExits.toList(); |
duke@1 | 745 | pendingExits = new ListBuffer<PendingExit>(); |
duke@1 | 746 | while (exits.nonEmpty()) { |
duke@1 | 747 | PendingExit exit = exits.head; |
duke@1 | 748 | exits = exits.tail; |
duke@1 | 749 | if (exit.thrown == null) { |
duke@1 | 750 | assert exit.tree.getTag() == JCTree.RETURN; |
duke@1 | 751 | if (isInitialConstructor) { |
duke@1 | 752 | inits = exit.inits; |
duke@1 | 753 | for (int i = firstadr; i < nextadr; i++) |
duke@1 | 754 | checkInit(exit.tree.pos(), vars[i]); |
duke@1 | 755 | } |
duke@1 | 756 | } else { |
duke@1 | 757 | // uncaught throws will be reported later |
duke@1 | 758 | pendingExits.append(exit); |
duke@1 | 759 | } |
duke@1 | 760 | } |
duke@1 | 761 | } finally { |
duke@1 | 762 | inits = initsPrev; |
duke@1 | 763 | uninits = uninitsPrev; |
duke@1 | 764 | nextadr = nextadrPrev; |
duke@1 | 765 | firstadr = firstadrPrev; |
duke@1 | 766 | caught = caughtPrev; |
duke@1 | 767 | lint = lintPrev; |
duke@1 | 768 | } |
duke@1 | 769 | } |
duke@1 | 770 | |
duke@1 | 771 | public void visitVarDef(JCVariableDecl tree) { |
duke@1 | 772 | boolean track = trackable(tree.sym); |
duke@1 | 773 | if (track && tree.sym.owner.kind == MTH) newVar(tree.sym); |
duke@1 | 774 | if (tree.init != null) { |
duke@1 | 775 | Lint lintPrev = lint; |
duke@1 | 776 | lint = lint.augment(tree.sym.attributes_field); |
duke@1 | 777 | try{ |
duke@1 | 778 | scanExpr(tree.init); |
duke@1 | 779 | if (track) letInit(tree.pos(), tree.sym); |
duke@1 | 780 | } finally { |
duke@1 | 781 | lint = lintPrev; |
duke@1 | 782 | } |
duke@1 | 783 | } |
duke@1 | 784 | } |
duke@1 | 785 | |
duke@1 | 786 | public void visitBlock(JCBlock tree) { |
duke@1 | 787 | int nextadrPrev = nextadr; |
duke@1 | 788 | scanStats(tree.stats); |
duke@1 | 789 | nextadr = nextadrPrev; |
duke@1 | 790 | } |
duke@1 | 791 | |
duke@1 | 792 | public void visitDoLoop(JCDoWhileLoop tree) { |
duke@1 | 793 | ListBuffer<PendingExit> prevPendingExits = pendingExits; |
duke@1 | 794 | boolean prevLoopPassTwo = loopPassTwo; |
duke@1 | 795 | pendingExits = new ListBuffer<PendingExit>(); |
duke@1 | 796 | do { |
duke@1 | 797 | Bits uninitsEntry = uninits.dup(); |
duke@1 | 798 | scanStat(tree.body); |
duke@1 | 799 | alive |= resolveContinues(tree); |
duke@1 | 800 | scanCond(tree.cond); |
duke@1 | 801 | if (log.nerrors != 0 || |
duke@1 | 802 | loopPassTwo || |
duke@1 | 803 | uninitsEntry.diffSet(uninitsWhenTrue).nextBit(firstadr)==-1) |
duke@1 | 804 | break; |
duke@1 | 805 | inits = initsWhenTrue; |
duke@1 | 806 | uninits = uninitsEntry.andSet(uninitsWhenTrue); |
duke@1 | 807 | loopPassTwo = true; |
duke@1 | 808 | alive = true; |
duke@1 | 809 | } while (true); |
duke@1 | 810 | loopPassTwo = prevLoopPassTwo; |
duke@1 | 811 | inits = initsWhenFalse; |
duke@1 | 812 | uninits = uninitsWhenFalse; |
duke@1 | 813 | alive = alive && !tree.cond.type.isTrue(); |
duke@1 | 814 | alive |= resolveBreaks(tree, prevPendingExits); |
duke@1 | 815 | } |
duke@1 | 816 | |
duke@1 | 817 | public void visitWhileLoop(JCWhileLoop tree) { |
duke@1 | 818 | ListBuffer<PendingExit> prevPendingExits = pendingExits; |
duke@1 | 819 | boolean prevLoopPassTwo = loopPassTwo; |
duke@1 | 820 | Bits initsCond; |
duke@1 | 821 | Bits uninitsCond; |
duke@1 | 822 | pendingExits = new ListBuffer<PendingExit>(); |
duke@1 | 823 | do { |
duke@1 | 824 | Bits uninitsEntry = uninits.dup(); |
duke@1 | 825 | scanCond(tree.cond); |
duke@1 | 826 | initsCond = initsWhenFalse; |
duke@1 | 827 | uninitsCond = uninitsWhenFalse; |
duke@1 | 828 | inits = initsWhenTrue; |
duke@1 | 829 | uninits = uninitsWhenTrue; |
duke@1 | 830 | alive = !tree.cond.type.isFalse(); |
duke@1 | 831 | scanStat(tree.body); |
duke@1 | 832 | alive |= resolveContinues(tree); |
duke@1 | 833 | if (log.nerrors != 0 || |
duke@1 | 834 | loopPassTwo || |
duke@1 | 835 | uninitsEntry.diffSet(uninits).nextBit(firstadr) == -1) |
duke@1 | 836 | break; |
duke@1 | 837 | uninits = uninitsEntry.andSet(uninits); |
duke@1 | 838 | loopPassTwo = true; |
duke@1 | 839 | alive = true; |
duke@1 | 840 | } while (true); |
duke@1 | 841 | loopPassTwo = prevLoopPassTwo; |
duke@1 | 842 | inits = initsCond; |
duke@1 | 843 | uninits = uninitsCond; |
duke@1 | 844 | alive = resolveBreaks(tree, prevPendingExits) || |
duke@1 | 845 | !tree.cond.type.isTrue(); |
duke@1 | 846 | } |
duke@1 | 847 | |
duke@1 | 848 | public void visitForLoop(JCForLoop tree) { |
duke@1 | 849 | ListBuffer<PendingExit> prevPendingExits = pendingExits; |
duke@1 | 850 | boolean prevLoopPassTwo = loopPassTwo; |
duke@1 | 851 | int nextadrPrev = nextadr; |
duke@1 | 852 | scanStats(tree.init); |
duke@1 | 853 | Bits initsCond; |
duke@1 | 854 | Bits uninitsCond; |
duke@1 | 855 | pendingExits = new ListBuffer<PendingExit>(); |
duke@1 | 856 | do { |
duke@1 | 857 | Bits uninitsEntry = uninits.dup(); |
duke@1 | 858 | if (tree.cond != null) { |
duke@1 | 859 | scanCond(tree.cond); |
duke@1 | 860 | initsCond = initsWhenFalse; |
duke@1 | 861 | uninitsCond = uninitsWhenFalse; |
duke@1 | 862 | inits = initsWhenTrue; |
duke@1 | 863 | uninits = uninitsWhenTrue; |
duke@1 | 864 | alive = !tree.cond.type.isFalse(); |
duke@1 | 865 | } else { |
duke@1 | 866 | initsCond = inits.dup(); |
duke@1 | 867 | initsCond.inclRange(firstadr, nextadr); |
duke@1 | 868 | uninitsCond = uninits.dup(); |
duke@1 | 869 | uninitsCond.inclRange(firstadr, nextadr); |
duke@1 | 870 | alive = true; |
duke@1 | 871 | } |
duke@1 | 872 | scanStat(tree.body); |
duke@1 | 873 | alive |= resolveContinues(tree); |
duke@1 | 874 | scan(tree.step); |
duke@1 | 875 | if (log.nerrors != 0 || |
duke@1 | 876 | loopPassTwo || |
duke@1 | 877 | uninitsEntry.dup().diffSet(uninits).nextBit(firstadr) == -1) |
duke@1 | 878 | break; |
duke@1 | 879 | uninits = uninitsEntry.andSet(uninits); |
duke@1 | 880 | loopPassTwo = true; |
duke@1 | 881 | alive = true; |
duke@1 | 882 | } while (true); |
duke@1 | 883 | loopPassTwo = prevLoopPassTwo; |
duke@1 | 884 | inits = initsCond; |
duke@1 | 885 | uninits = uninitsCond; |
duke@1 | 886 | alive = resolveBreaks(tree, prevPendingExits) || |
duke@1 | 887 | tree.cond != null && !tree.cond.type.isTrue(); |
duke@1 | 888 | nextadr = nextadrPrev; |
duke@1 | 889 | } |
duke@1 | 890 | |
duke@1 | 891 | public void visitForeachLoop(JCEnhancedForLoop tree) { |
duke@1 | 892 | visitVarDef(tree.var); |
duke@1 | 893 | |
duke@1 | 894 | ListBuffer<PendingExit> prevPendingExits = pendingExits; |
duke@1 | 895 | boolean prevLoopPassTwo = loopPassTwo; |
duke@1 | 896 | int nextadrPrev = nextadr; |
duke@1 | 897 | scan(tree.expr); |
duke@1 | 898 | Bits initsStart = inits.dup(); |
duke@1 | 899 | Bits uninitsStart = uninits.dup(); |
duke@1 | 900 | |
duke@1 | 901 | letInit(tree.pos(), tree.var.sym); |
duke@1 | 902 | pendingExits = new ListBuffer<PendingExit>(); |
duke@1 | 903 | do { |
duke@1 | 904 | Bits uninitsEntry = uninits.dup(); |
duke@1 | 905 | scanStat(tree.body); |
duke@1 | 906 | alive |= resolveContinues(tree); |
duke@1 | 907 | if (log.nerrors != 0 || |
duke@1 | 908 | loopPassTwo || |
duke@1 | 909 | uninitsEntry.diffSet(uninits).nextBit(firstadr) == -1) |
duke@1 | 910 | break; |
duke@1 | 911 | uninits = uninitsEntry.andSet(uninits); |
duke@1 | 912 | loopPassTwo = true; |
duke@1 | 913 | alive = true; |
duke@1 | 914 | } while (true); |
duke@1 | 915 | loopPassTwo = prevLoopPassTwo; |
duke@1 | 916 | inits = initsStart; |
duke@1 | 917 | uninits = uninitsStart.andSet(uninits); |
duke@1 | 918 | resolveBreaks(tree, prevPendingExits); |
duke@1 | 919 | alive = true; |
duke@1 | 920 | nextadr = nextadrPrev; |
duke@1 | 921 | } |
duke@1 | 922 | |
duke@1 | 923 | public void visitLabelled(JCLabeledStatement tree) { |
duke@1 | 924 | ListBuffer<PendingExit> prevPendingExits = pendingExits; |
duke@1 | 925 | pendingExits = new ListBuffer<PendingExit>(); |
duke@1 | 926 | scanStat(tree.body); |
duke@1 | 927 | alive |= resolveBreaks(tree, prevPendingExits); |
duke@1 | 928 | } |
duke@1 | 929 | |
duke@1 | 930 | public void visitSwitch(JCSwitch tree) { |
duke@1 | 931 | ListBuffer<PendingExit> prevPendingExits = pendingExits; |
duke@1 | 932 | pendingExits = new ListBuffer<PendingExit>(); |
duke@1 | 933 | int nextadrPrev = nextadr; |
duke@1 | 934 | scanExpr(tree.selector); |
duke@1 | 935 | Bits initsSwitch = inits; |
duke@1 | 936 | Bits uninitsSwitch = uninits.dup(); |
duke@1 | 937 | boolean hasDefault = false; |
duke@1 | 938 | for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) { |
duke@1 | 939 | alive = true; |
duke@1 | 940 | inits = initsSwitch.dup(); |
duke@1 | 941 | uninits = uninits.andSet(uninitsSwitch); |
duke@1 | 942 | JCCase c = l.head; |
duke@1 | 943 | if (c.pat == null) |
duke@1 | 944 | hasDefault = true; |
duke@1 | 945 | else |
duke@1 | 946 | scanExpr(c.pat); |
duke@1 | 947 | scanStats(c.stats); |
duke@1 | 948 | addVars(c.stats, initsSwitch, uninitsSwitch); |
duke@1 | 949 | // Warn about fall-through if lint switch fallthrough enabled. |
duke@1 | 950 | if (!loopPassTwo && |
duke@1 | 951 | alive && |
duke@1 | 952 | lint.isEnabled(Lint.LintCategory.FALLTHROUGH) && |
duke@1 | 953 | c.stats.nonEmpty() && l.tail.nonEmpty()) |
jjg@612 | 954 | log.warning(Lint.LintCategory.FALLTHROUGH, |
jjg@612 | 955 | l.tail.head.pos(), |
duke@1 | 956 | "possible.fall-through.into.case"); |
duke@1 | 957 | } |
duke@1 | 958 | if (!hasDefault) { |
duke@1 | 959 | inits.andSet(initsSwitch); |
duke@1 | 960 | alive = true; |
duke@1 | 961 | } |
duke@1 | 962 | alive |= resolveBreaks(tree, prevPendingExits); |
duke@1 | 963 | nextadr = nextadrPrev; |
duke@1 | 964 | } |
duke@1 | 965 | // where |
duke@1 | 966 | /** Add any variables defined in stats to inits and uninits. */ |
duke@1 | 967 | private static void addVars(List<JCStatement> stats, Bits inits, |
duke@1 | 968 | Bits uninits) { |
duke@1 | 969 | for (;stats.nonEmpty(); stats = stats.tail) { |
duke@1 | 970 | JCTree stat = stats.head; |
duke@1 | 971 | if (stat.getTag() == JCTree.VARDEF) { |
duke@1 | 972 | int adr = ((JCVariableDecl) stat).sym.adr; |
duke@1 | 973 | inits.excl(adr); |
duke@1 | 974 | uninits.incl(adr); |
duke@1 | 975 | } |
duke@1 | 976 | } |
duke@1 | 977 | } |
duke@1 | 978 | |
duke@1 | 979 | public void visitTry(JCTry tree) { |
duke@1 | 980 | List<Type> caughtPrev = caught; |
duke@1 | 981 | List<Type> thrownPrev = thrown; |
darcy@609 | 982 | Map<VarSymbol, JCVariableDecl> unrefdResourcesPrev = unrefdResources; |
duke@1 | 983 | thrown = List.nil(); |
mcimadamore@550 | 984 | for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) { |
mcimadamore@550 | 985 | List<JCExpression> subClauses = TreeInfo.isMultiCatch(l.head) ? |
mcimadamore@550 | 986 | ((JCTypeDisjoint)l.head.param.vartype).components : |
mcimadamore@550 | 987 | List.of(l.head.param.vartype); |
mcimadamore@550 | 988 | for (JCExpression ct : subClauses) { |
mcimadamore@550 | 989 | caught = chk.incl(ct.type, caught); |
mcimadamore@550 | 990 | } |
mcimadamore@550 | 991 | } |
duke@1 | 992 | Bits uninitsTryPrev = uninitsTry; |
duke@1 | 993 | ListBuffer<PendingExit> prevPendingExits = pendingExits; |
duke@1 | 994 | pendingExits = new ListBuffer<PendingExit>(); |
duke@1 | 995 | Bits initsTry = inits.dup(); |
duke@1 | 996 | uninitsTry = uninits.dup(); |
darcy@609 | 997 | unrefdResources = new LinkedHashMap<VarSymbol, JCVariableDecl>(); |
darcy@609 | 998 | for (JCTree resource : tree.resources) { |
darcy@609 | 999 | if (resource instanceof JCVariableDecl) { |
darcy@609 | 1000 | JCVariableDecl vdecl = (JCVariableDecl) resource; |
darcy@609 | 1001 | visitVarDef(vdecl); |
darcy@609 | 1002 | unrefdResources.put(vdecl.sym, vdecl); |
darcy@609 | 1003 | } else if (resource instanceof JCExpression) { |
darcy@609 | 1004 | scanExpr((JCExpression) resource); |
darcy@609 | 1005 | } else { |
darcy@609 | 1006 | throw new AssertionError(tree); // parser error |
darcy@609 | 1007 | } |
darcy@609 | 1008 | } |
darcy@609 | 1009 | for (JCTree resource : tree.resources) { |
darcy@609 | 1010 | List<Type> closeableSupertypes = resource.type.isCompound() ? |
darcy@609 | 1011 | types.interfaces(resource.type).prepend(types.supertype(resource.type)) : |
darcy@609 | 1012 | List.of(resource.type); |
darcy@609 | 1013 | for (Type sup : closeableSupertypes) { |
darcy@609 | 1014 | if (types.asSuper(sup, syms.autoCloseableType.tsym) != null) { |
mcimadamore@676 | 1015 | Symbol closeMethod = rs.resolveQualifiedMethod(tree, |
mcimadamore@617 | 1016 | attrEnv, |
mcimadamore@617 | 1017 | sup, |
mcimadamore@617 | 1018 | names.close, |
mcimadamore@617 | 1019 | List.<Type>nil(), |
mcimadamore@617 | 1020 | List.<Type>nil()); |
mcimadamore@617 | 1021 | if (closeMethod.kind == MTH) { |
mcimadamore@617 | 1022 | for (Type t : ((MethodSymbol)closeMethod).getThrownTypes()) { |
mcimadamore@617 | 1023 | markThrown(tree.body, t); |
mcimadamore@617 | 1024 | } |
darcy@609 | 1025 | } |
darcy@609 | 1026 | } |
darcy@609 | 1027 | } |
darcy@609 | 1028 | } |
duke@1 | 1029 | scanStat(tree.body); |
duke@1 | 1030 | List<Type> thrownInTry = thrown; |
duke@1 | 1031 | thrown = thrownPrev; |
duke@1 | 1032 | caught = caughtPrev; |
duke@1 | 1033 | boolean aliveEnd = alive; |
duke@1 | 1034 | uninitsTry.andSet(uninits); |
duke@1 | 1035 | Bits initsEnd = inits; |
duke@1 | 1036 | Bits uninitsEnd = uninits; |
duke@1 | 1037 | int nextadrCatch = nextadr; |
duke@1 | 1038 | |
darcy@609 | 1039 | if (!unrefdResources.isEmpty() && |
darcy@609 | 1040 | lint.isEnabled(Lint.LintCategory.ARM)) { |
darcy@609 | 1041 | for (Map.Entry<VarSymbol, JCVariableDecl> e : unrefdResources.entrySet()) { |
darcy@609 | 1042 | log.warning(e.getValue().pos(), |
darcy@609 | 1043 | "automatic.resource.not.referenced", e.getKey()); |
darcy@609 | 1044 | } |
darcy@609 | 1045 | } |
darcy@609 | 1046 | |
duke@1 | 1047 | List<Type> caughtInTry = List.nil(); |
duke@1 | 1048 | for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) { |
duke@1 | 1049 | alive = true; |
duke@1 | 1050 | JCVariableDecl param = l.head.param; |
mcimadamore@550 | 1051 | List<JCExpression> subClauses = TreeInfo.isMultiCatch(l.head) ? |
mcimadamore@550 | 1052 | ((JCTypeDisjoint)l.head.param.vartype).components : |
mcimadamore@550 | 1053 | List.of(l.head.param.vartype); |
mcimadamore@550 | 1054 | List<Type> ctypes = List.nil(); |
mcimadamore@550 | 1055 | List<Type> rethrownTypes = chk.diff(thrownInTry, caughtInTry); |
mcimadamore@550 | 1056 | for (JCExpression ct : subClauses) { |
mcimadamore@550 | 1057 | Type exc = ct.type; |
mcimadamore@676 | 1058 | if (exc != syms.unknownType) { |
mcimadamore@676 | 1059 | ctypes = ctypes.append(exc); |
mcimadamore@676 | 1060 | if (types.isSameType(exc, syms.objectType)) |
mcimadamore@676 | 1061 | continue; |
mcimadamore@676 | 1062 | if (chk.subset(exc, caughtInTry)) { |
mcimadamore@676 | 1063 | log.error(l.head.pos(), |
mcimadamore@676 | 1064 | "except.already.caught", exc); |
mcimadamore@676 | 1065 | } else if (!chk.isUnchecked(l.head.pos(), exc) && |
mcimadamore@676 | 1066 | exc.tsym != syms.throwableType.tsym && |
mcimadamore@676 | 1067 | exc.tsym != syms.exceptionType.tsym && |
mcimadamore@676 | 1068 | !chk.intersects(exc, thrownInTry)) { |
mcimadamore@676 | 1069 | log.error(l.head.pos(), |
mcimadamore@676 | 1070 | "except.never.thrown.in.try", exc); |
mcimadamore@676 | 1071 | } |
mcimadamore@676 | 1072 | caughtInTry = chk.incl(exc, caughtInTry); |
mcimadamore@550 | 1073 | } |
duke@1 | 1074 | } |
duke@1 | 1075 | inits = initsTry.dup(); |
duke@1 | 1076 | uninits = uninitsTry.dup(); |
duke@1 | 1077 | scan(param); |
duke@1 | 1078 | inits.incl(param.sym.adr); |
duke@1 | 1079 | uninits.excl(param.sym.adr); |
mcimadamore@550 | 1080 | multicatchTypes.put(param.sym, chk.intersect(ctypes, rethrownTypes)); |
duke@1 | 1081 | scanStat(l.head.body); |
duke@1 | 1082 | initsEnd.andSet(inits); |
duke@1 | 1083 | uninitsEnd.andSet(uninits); |
duke@1 | 1084 | nextadr = nextadrCatch; |
mcimadamore@550 | 1085 | multicatchTypes.remove(param.sym); |
duke@1 | 1086 | aliveEnd |= alive; |
duke@1 | 1087 | } |
duke@1 | 1088 | if (tree.finalizer != null) { |
duke@1 | 1089 | List<Type> savedThrown = thrown; |
duke@1 | 1090 | thrown = List.nil(); |
duke@1 | 1091 | inits = initsTry.dup(); |
duke@1 | 1092 | uninits = uninitsTry.dup(); |
duke@1 | 1093 | ListBuffer<PendingExit> exits = pendingExits; |
duke@1 | 1094 | pendingExits = prevPendingExits; |
duke@1 | 1095 | alive = true; |
duke@1 | 1096 | scanStat(tree.finalizer); |
duke@1 | 1097 | if (!alive) { |
duke@1 | 1098 | // discard exits and exceptions from try and finally |
duke@1 | 1099 | thrown = chk.union(thrown, thrownPrev); |
duke@1 | 1100 | if (!loopPassTwo && |
duke@1 | 1101 | lint.isEnabled(Lint.LintCategory.FINALLY)) { |
jjg@612 | 1102 | log.warning(Lint.LintCategory.FINALLY, |
jjg@612 | 1103 | TreeInfo.diagEndPos(tree.finalizer), |
jjg@612 | 1104 | "finally.cannot.complete"); |
duke@1 | 1105 | } |
duke@1 | 1106 | } else { |
duke@1 | 1107 | thrown = chk.union(thrown, chk.diff(thrownInTry, caughtInTry)); |
duke@1 | 1108 | thrown = chk.union(thrown, savedThrown); |
duke@1 | 1109 | uninits.andSet(uninitsEnd); |
duke@1 | 1110 | // FIX: this doesn't preserve source order of exits in catch |
duke@1 | 1111 | // versus finally! |
duke@1 | 1112 | while (exits.nonEmpty()) { |
duke@1 | 1113 | PendingExit exit = exits.next(); |
duke@1 | 1114 | if (exit.inits != null) { |
duke@1 | 1115 | exit.inits.orSet(inits); |
duke@1 | 1116 | exit.uninits.andSet(uninits); |
duke@1 | 1117 | } |
duke@1 | 1118 | pendingExits.append(exit); |
duke@1 | 1119 | } |
duke@1 | 1120 | inits.orSet(initsEnd); |
duke@1 | 1121 | alive = aliveEnd; |
duke@1 | 1122 | } |
duke@1 | 1123 | } else { |
duke@1 | 1124 | thrown = chk.union(thrown, chk.diff(thrownInTry, caughtInTry)); |
duke@1 | 1125 | inits = initsEnd; |
duke@1 | 1126 | uninits = uninitsEnd; |
duke@1 | 1127 | alive = aliveEnd; |
duke@1 | 1128 | ListBuffer<PendingExit> exits = pendingExits; |
duke@1 | 1129 | pendingExits = prevPendingExits; |
duke@1 | 1130 | while (exits.nonEmpty()) pendingExits.append(exits.next()); |
duke@1 | 1131 | } |
duke@1 | 1132 | uninitsTry.andSet(uninitsTryPrev).andSet(uninits); |
darcy@609 | 1133 | unrefdResources = unrefdResourcesPrev; |
duke@1 | 1134 | } |
duke@1 | 1135 | |
duke@1 | 1136 | public void visitConditional(JCConditional tree) { |
duke@1 | 1137 | scanCond(tree.cond); |
duke@1 | 1138 | Bits initsBeforeElse = initsWhenFalse; |
duke@1 | 1139 | Bits uninitsBeforeElse = uninitsWhenFalse; |
duke@1 | 1140 | inits = initsWhenTrue; |
duke@1 | 1141 | uninits = uninitsWhenTrue; |
duke@1 | 1142 | if (tree.truepart.type.tag == BOOLEAN && |
duke@1 | 1143 | tree.falsepart.type.tag == BOOLEAN) { |
duke@1 | 1144 | // if b and c are boolean valued, then |
duke@1 | 1145 | // v is (un)assigned after a?b:c when true iff |
duke@1 | 1146 | // v is (un)assigned after b when true and |
duke@1 | 1147 | // v is (un)assigned after c when true |
duke@1 | 1148 | scanCond(tree.truepart); |
duke@1 | 1149 | Bits initsAfterThenWhenTrue = initsWhenTrue.dup(); |
duke@1 | 1150 | Bits initsAfterThenWhenFalse = initsWhenFalse.dup(); |
duke@1 | 1151 | Bits uninitsAfterThenWhenTrue = uninitsWhenTrue.dup(); |
duke@1 | 1152 | Bits uninitsAfterThenWhenFalse = uninitsWhenFalse.dup(); |
duke@1 | 1153 | inits = initsBeforeElse; |
duke@1 | 1154 | uninits = uninitsBeforeElse; |
duke@1 | 1155 | scanCond(tree.falsepart); |
duke@1 | 1156 | initsWhenTrue.andSet(initsAfterThenWhenTrue); |
duke@1 | 1157 | initsWhenFalse.andSet(initsAfterThenWhenFalse); |
duke@1 | 1158 | uninitsWhenTrue.andSet(uninitsAfterThenWhenTrue); |
duke@1 | 1159 | uninitsWhenFalse.andSet(uninitsAfterThenWhenFalse); |
duke@1 | 1160 | } else { |
duke@1 | 1161 | scanExpr(tree.truepart); |
duke@1 | 1162 | Bits initsAfterThen = inits.dup(); |
duke@1 | 1163 | Bits uninitsAfterThen = uninits.dup(); |
duke@1 | 1164 | inits = initsBeforeElse; |
duke@1 | 1165 | uninits = uninitsBeforeElse; |
duke@1 | 1166 | scanExpr(tree.falsepart); |
duke@1 | 1167 | inits.andSet(initsAfterThen); |
duke@1 | 1168 | uninits.andSet(uninitsAfterThen); |
duke@1 | 1169 | } |
duke@1 | 1170 | } |
duke@1 | 1171 | |
duke@1 | 1172 | public void visitIf(JCIf tree) { |
duke@1 | 1173 | scanCond(tree.cond); |
duke@1 | 1174 | Bits initsBeforeElse = initsWhenFalse; |
duke@1 | 1175 | Bits uninitsBeforeElse = uninitsWhenFalse; |
duke@1 | 1176 | inits = initsWhenTrue; |
duke@1 | 1177 | uninits = uninitsWhenTrue; |
duke@1 | 1178 | scanStat(tree.thenpart); |
duke@1 | 1179 | if (tree.elsepart != null) { |
duke@1 | 1180 | boolean aliveAfterThen = alive; |
duke@1 | 1181 | alive = true; |
duke@1 | 1182 | Bits initsAfterThen = inits.dup(); |
duke@1 | 1183 | Bits uninitsAfterThen = uninits.dup(); |
duke@1 | 1184 | inits = initsBeforeElse; |
duke@1 | 1185 | uninits = uninitsBeforeElse; |
duke@1 | 1186 | scanStat(tree.elsepart); |
duke@1 | 1187 | inits.andSet(initsAfterThen); |
duke@1 | 1188 | uninits.andSet(uninitsAfterThen); |
duke@1 | 1189 | alive = alive | aliveAfterThen; |
duke@1 | 1190 | } else { |
duke@1 | 1191 | inits.andSet(initsBeforeElse); |
duke@1 | 1192 | uninits.andSet(uninitsBeforeElse); |
duke@1 | 1193 | alive = true; |
duke@1 | 1194 | } |
duke@1 | 1195 | } |
duke@1 | 1196 | |
duke@1 | 1197 | |
duke@1 | 1198 | |
duke@1 | 1199 | public void visitBreak(JCBreak tree) { |
duke@1 | 1200 | recordExit(tree); |
duke@1 | 1201 | } |
duke@1 | 1202 | |
duke@1 | 1203 | public void visitContinue(JCContinue tree) { |
duke@1 | 1204 | recordExit(tree); |
duke@1 | 1205 | } |
duke@1 | 1206 | |
duke@1 | 1207 | public void visitReturn(JCReturn tree) { |
duke@1 | 1208 | scanExpr(tree.expr); |
duke@1 | 1209 | // if not initial constructor, should markDead instead of recordExit |
duke@1 | 1210 | recordExit(tree); |
duke@1 | 1211 | } |
duke@1 | 1212 | |
duke@1 | 1213 | public void visitThrow(JCThrow tree) { |
duke@1 | 1214 | scanExpr(tree.expr); |
mcimadamore@550 | 1215 | Symbol sym = TreeInfo.symbol(tree.expr); |
mcimadamore@550 | 1216 | if (sym != null && |
mcimadamore@550 | 1217 | sym.kind == VAR && |
mcimadamore@550 | 1218 | (sym.flags() & FINAL) != 0 && |
mcimadamore@550 | 1219 | multicatchTypes.get(sym) != null && |
mcimadamore@550 | 1220 | allowRethrowAnalysis) { |
mcimadamore@550 | 1221 | for (Type t : multicatchTypes.get(sym)) { |
mcimadamore@550 | 1222 | markThrown(tree, t); |
mcimadamore@550 | 1223 | } |
mcimadamore@550 | 1224 | } |
mcimadamore@550 | 1225 | else { |
mcimadamore@550 | 1226 | markThrown(tree, tree.expr.type); |
mcimadamore@550 | 1227 | } |
duke@1 | 1228 | markDead(); |
duke@1 | 1229 | } |
duke@1 | 1230 | |
duke@1 | 1231 | public void visitApply(JCMethodInvocation tree) { |
duke@1 | 1232 | scanExpr(tree.meth); |
duke@1 | 1233 | scanExprs(tree.args); |
duke@1 | 1234 | for (List<Type> l = tree.meth.type.getThrownTypes(); l.nonEmpty(); l = l.tail) |
duke@1 | 1235 | markThrown(tree, l.head); |
duke@1 | 1236 | } |
duke@1 | 1237 | |
duke@1 | 1238 | public void visitNewClass(JCNewClass tree) { |
duke@1 | 1239 | scanExpr(tree.encl); |
duke@1 | 1240 | scanExprs(tree.args); |
duke@1 | 1241 | // scan(tree.def); |
mcimadamore@186 | 1242 | for (List<Type> l = tree.constructorType.getThrownTypes(); |
duke@1 | 1243 | l.nonEmpty(); |
mcimadamore@186 | 1244 | l = l.tail) { |
duke@1 | 1245 | markThrown(tree, l.head); |
mcimadamore@186 | 1246 | } |
mcimadamore@186 | 1247 | List<Type> caughtPrev = caught; |
mcimadamore@186 | 1248 | try { |
mcimadamore@186 | 1249 | // If the new class expression defines an anonymous class, |
mcimadamore@186 | 1250 | // analysis of the anonymous constructor may encounter thrown |
mcimadamore@186 | 1251 | // types which are unsubstituted type variables. |
mcimadamore@186 | 1252 | // However, since the constructor's actual thrown types have |
mcimadamore@186 | 1253 | // already been marked as thrown, it is safe to simply include |
mcimadamore@186 | 1254 | // each of the constructor's formal thrown types in the set of |
mcimadamore@186 | 1255 | // 'caught/declared to be thrown' types, for the duration of |
mcimadamore@186 | 1256 | // the class def analysis. |
mcimadamore@186 | 1257 | if (tree.def != null) |
mcimadamore@186 | 1258 | for (List<Type> l = tree.constructor.type.getThrownTypes(); |
mcimadamore@186 | 1259 | l.nonEmpty(); |
mcimadamore@186 | 1260 | l = l.tail) { |
mcimadamore@186 | 1261 | caught = chk.incl(l.head, caught); |
mcimadamore@186 | 1262 | } |
mcimadamore@186 | 1263 | scan(tree.def); |
mcimadamore@186 | 1264 | } |
mcimadamore@186 | 1265 | finally { |
mcimadamore@186 | 1266 | caught = caughtPrev; |
mcimadamore@186 | 1267 | } |
duke@1 | 1268 | } |
duke@1 | 1269 | |
duke@1 | 1270 | public void visitNewArray(JCNewArray tree) { |
duke@1 | 1271 | scanExprs(tree.dims); |
duke@1 | 1272 | scanExprs(tree.elems); |
duke@1 | 1273 | } |
duke@1 | 1274 | |
duke@1 | 1275 | public void visitAssert(JCAssert tree) { |
duke@1 | 1276 | Bits initsExit = inits.dup(); |
duke@1 | 1277 | Bits uninitsExit = uninits.dup(); |
duke@1 | 1278 | scanCond(tree.cond); |
duke@1 | 1279 | uninitsExit.andSet(uninitsWhenTrue); |
duke@1 | 1280 | if (tree.detail != null) { |
duke@1 | 1281 | inits = initsWhenFalse; |
duke@1 | 1282 | uninits = uninitsWhenFalse; |
duke@1 | 1283 | scanExpr(tree.detail); |
duke@1 | 1284 | } |
duke@1 | 1285 | inits = initsExit; |
duke@1 | 1286 | uninits = uninitsExit; |
duke@1 | 1287 | } |
duke@1 | 1288 | |
duke@1 | 1289 | public void visitAssign(JCAssign tree) { |
duke@1 | 1290 | JCTree lhs = TreeInfo.skipParens(tree.lhs); |
duke@1 | 1291 | if (!(lhs instanceof JCIdent)) scanExpr(lhs); |
duke@1 | 1292 | scanExpr(tree.rhs); |
duke@1 | 1293 | letInit(lhs); |
duke@1 | 1294 | } |
duke@1 | 1295 | |
duke@1 | 1296 | public void visitAssignop(JCAssignOp tree) { |
duke@1 | 1297 | scanExpr(tree.lhs); |
duke@1 | 1298 | scanExpr(tree.rhs); |
duke@1 | 1299 | letInit(tree.lhs); |
duke@1 | 1300 | } |
duke@1 | 1301 | |
duke@1 | 1302 | public void visitUnary(JCUnary tree) { |
duke@1 | 1303 | switch (tree.getTag()) { |
duke@1 | 1304 | case JCTree.NOT: |
duke@1 | 1305 | scanCond(tree.arg); |
duke@1 | 1306 | Bits t = initsWhenFalse; |
duke@1 | 1307 | initsWhenFalse = initsWhenTrue; |
duke@1 | 1308 | initsWhenTrue = t; |
duke@1 | 1309 | t = uninitsWhenFalse; |
duke@1 | 1310 | uninitsWhenFalse = uninitsWhenTrue; |
duke@1 | 1311 | uninitsWhenTrue = t; |
duke@1 | 1312 | break; |
duke@1 | 1313 | case JCTree.PREINC: case JCTree.POSTINC: |
duke@1 | 1314 | case JCTree.PREDEC: case JCTree.POSTDEC: |
duke@1 | 1315 | scanExpr(tree.arg); |
duke@1 | 1316 | letInit(tree.arg); |
duke@1 | 1317 | break; |
duke@1 | 1318 | default: |
duke@1 | 1319 | scanExpr(tree.arg); |
duke@1 | 1320 | } |
duke@1 | 1321 | } |
duke@1 | 1322 | |
duke@1 | 1323 | public void visitBinary(JCBinary tree) { |
duke@1 | 1324 | switch (tree.getTag()) { |
duke@1 | 1325 | case JCTree.AND: |
duke@1 | 1326 | scanCond(tree.lhs); |
duke@1 | 1327 | Bits initsWhenFalseLeft = initsWhenFalse; |
duke@1 | 1328 | Bits uninitsWhenFalseLeft = uninitsWhenFalse; |
duke@1 | 1329 | inits = initsWhenTrue; |
duke@1 | 1330 | uninits = uninitsWhenTrue; |
duke@1 | 1331 | scanCond(tree.rhs); |
duke@1 | 1332 | initsWhenFalse.andSet(initsWhenFalseLeft); |
duke@1 | 1333 | uninitsWhenFalse.andSet(uninitsWhenFalseLeft); |
duke@1 | 1334 | break; |
duke@1 | 1335 | case JCTree.OR: |
duke@1 | 1336 | scanCond(tree.lhs); |
duke@1 | 1337 | Bits initsWhenTrueLeft = initsWhenTrue; |
duke@1 | 1338 | Bits uninitsWhenTrueLeft = uninitsWhenTrue; |
duke@1 | 1339 | inits = initsWhenFalse; |
duke@1 | 1340 | uninits = uninitsWhenFalse; |
duke@1 | 1341 | scanCond(tree.rhs); |
duke@1 | 1342 | initsWhenTrue.andSet(initsWhenTrueLeft); |
duke@1 | 1343 | uninitsWhenTrue.andSet(uninitsWhenTrueLeft); |
duke@1 | 1344 | break; |
duke@1 | 1345 | default: |
duke@1 | 1346 | scanExpr(tree.lhs); |
duke@1 | 1347 | scanExpr(tree.rhs); |
duke@1 | 1348 | } |
duke@1 | 1349 | } |
duke@1 | 1350 | |
jjg@308 | 1351 | public void visitAnnotatedType(JCAnnotatedType tree) { |
jjg@308 | 1352 | // annotations don't get scanned |
jjg@308 | 1353 | tree.underlyingType.accept(this); |
jjg@308 | 1354 | } |
jjg@308 | 1355 | |
duke@1 | 1356 | public void visitIdent(JCIdent tree) { |
darcy@609 | 1357 | if (tree.sym.kind == VAR) { |
duke@1 | 1358 | checkInit(tree.pos(), (VarSymbol)tree.sym); |
darcy@609 | 1359 | referenced(tree.sym); |
darcy@609 | 1360 | } |
darcy@609 | 1361 | } |
darcy@609 | 1362 | |
darcy@609 | 1363 | void referenced(Symbol sym) { |
darcy@609 | 1364 | if (unrefdResources != null && unrefdResources.containsKey(sym)) { |
darcy@609 | 1365 | unrefdResources.remove(sym); |
darcy@609 | 1366 | } |
duke@1 | 1367 | } |
duke@1 | 1368 | |
duke@1 | 1369 | public void visitTypeCast(JCTypeCast tree) { |
duke@1 | 1370 | super.visitTypeCast(tree); |
duke@1 | 1371 | if (!tree.type.isErroneous() |
duke@1 | 1372 | && lint.isEnabled(Lint.LintCategory.CAST) |
jjg@308 | 1373 | && types.isSameType(tree.expr.type, tree.clazz.type) |
jjg@308 | 1374 | && !(ignoreAnnotatedCasts && containsTypeAnnotation(tree.clazz))) { |
jjg@612 | 1375 | log.warning(Lint.LintCategory.CAST, |
jjg@612 | 1376 | tree.pos(), "redundant.cast", tree.expr.type); |
duke@1 | 1377 | } |
duke@1 | 1378 | } |
duke@1 | 1379 | |
duke@1 | 1380 | public void visitTopLevel(JCCompilationUnit tree) { |
duke@1 | 1381 | // Do nothing for TopLevel since each class is visited individually |
duke@1 | 1382 | } |
duke@1 | 1383 | |
duke@1 | 1384 | /************************************************************************** |
jjg@308 | 1385 | * utility methods for ignoring type-annotated casts lint checking |
jjg@308 | 1386 | *************************************************************************/ |
jjg@308 | 1387 | private static final boolean ignoreAnnotatedCasts = true; |
jjg@308 | 1388 | private static class AnnotationFinder extends TreeScanner { |
jjg@308 | 1389 | public boolean foundTypeAnno = false; |
jjg@308 | 1390 | public void visitAnnotation(JCAnnotation tree) { |
jjg@308 | 1391 | foundTypeAnno = foundTypeAnno || (tree instanceof JCTypeAnnotation); |
jjg@308 | 1392 | } |
jjg@308 | 1393 | } |
jjg@308 | 1394 | |
jjg@308 | 1395 | private boolean containsTypeAnnotation(JCTree e) { |
jjg@308 | 1396 | AnnotationFinder finder = new AnnotationFinder(); |
jjg@308 | 1397 | finder.scan(e); |
jjg@308 | 1398 | return finder.foundTypeAnno; |
jjg@308 | 1399 | } |
jjg@308 | 1400 | |
jjg@308 | 1401 | /************************************************************************** |
duke@1 | 1402 | * main method |
duke@1 | 1403 | *************************************************************************/ |
duke@1 | 1404 | |
duke@1 | 1405 | /** Perform definite assignment/unassignment analysis on a tree. |
duke@1 | 1406 | */ |
mcimadamore@617 | 1407 | public void analyzeTree(Env<AttrContext> env, TreeMaker make) { |
duke@1 | 1408 | try { |
mcimadamore@617 | 1409 | attrEnv = env; |
mcimadamore@617 | 1410 | JCTree tree = env.tree; |
duke@1 | 1411 | this.make = make; |
duke@1 | 1412 | inits = new Bits(); |
duke@1 | 1413 | uninits = new Bits(); |
duke@1 | 1414 | uninitsTry = new Bits(); |
duke@1 | 1415 | initsWhenTrue = initsWhenFalse = |
duke@1 | 1416 | uninitsWhenTrue = uninitsWhenFalse = null; |
duke@1 | 1417 | if (vars == null) |
duke@1 | 1418 | vars = new VarSymbol[32]; |
duke@1 | 1419 | else |
duke@1 | 1420 | for (int i=0; i<vars.length; i++) |
duke@1 | 1421 | vars[i] = null; |
duke@1 | 1422 | firstadr = 0; |
duke@1 | 1423 | nextadr = 0; |
duke@1 | 1424 | pendingExits = new ListBuffer<PendingExit>(); |
mcimadamore@550 | 1425 | multicatchTypes = new HashMap<Symbol, List<Type>>(); |
duke@1 | 1426 | alive = true; |
duke@1 | 1427 | this.thrown = this.caught = null; |
duke@1 | 1428 | this.classDef = null; |
duke@1 | 1429 | scan(tree); |
duke@1 | 1430 | } finally { |
duke@1 | 1431 | // note that recursive invocations of this method fail hard |
duke@1 | 1432 | inits = uninits = uninitsTry = null; |
duke@1 | 1433 | initsWhenTrue = initsWhenFalse = |
duke@1 | 1434 | uninitsWhenTrue = uninitsWhenFalse = null; |
duke@1 | 1435 | if (vars != null) for (int i=0; i<vars.length; i++) |
duke@1 | 1436 | vars[i] = null; |
duke@1 | 1437 | firstadr = 0; |
duke@1 | 1438 | nextadr = 0; |
duke@1 | 1439 | pendingExits = null; |
duke@1 | 1440 | this.make = null; |
duke@1 | 1441 | this.thrown = this.caught = null; |
duke@1 | 1442 | this.classDef = null; |
duke@1 | 1443 | } |
duke@1 | 1444 | } |
duke@1 | 1445 | } |