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