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