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