< prev index next >

src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Flow.java

Print this page
rev 52724 : imported patch 8214031
rev 52725 : [mq]: 8214031-review1

@@ -298,11 +298,11 @@
 
     /**
      * Base visitor class for all visitors implementing dataflow analysis logic.
      * This class define the shared logic for handling jumps (break/continue statements).
      */
-    static abstract class BaseAnalyzer<P extends BaseAnalyzer.PendingExit> extends TreeScanner {
+    static abstract class BaseAnalyzer extends TreeScanner {
 
         enum JumpKind {
             BREAK(JCTree.Tag.BREAK) {
                 @Override
                 JCTree getTarget(JCTree tree) {

@@ -326,11 +326,11 @@
         }
 
         /** The currently pending exits that go from current inner blocks
          *  to an enclosing block, in source order.
          */
-        ListBuffer<P> pendingExits;
+        ListBuffer<PendingExit> pendingExits;
 
         /** A pending exit.  These are the statements return, break, and
          *  continue.  In addition, exception-throwing expressions or
          *  statements are put here when not known to be caught.  This
          *  will typically result in an error unless it is within a

@@ -349,24 +349,24 @@
         }
 
         abstract void markDead();
 
         /** Record an outward transfer of control. */
-        void recordExit(P pe) {
+        void recordExit(PendingExit pe) {
             pendingExits.append(pe);
             markDead();
         }
 
         /** Resolve all jumps of this statement. */
         private Liveness resolveJump(JCTree tree,
-                         ListBuffer<P> oldPendingExits,
+                         ListBuffer<PendingExit> oldPendingExits,
                          JumpKind jk) {
             boolean resolved = false;
-            List<P> exits = pendingExits.toList();
+            List<PendingExit> exits = pendingExits.toList();
             pendingExits = oldPendingExits;
             for (; exits.nonEmpty(); exits = exits.tail) {
-                P exit = exits.head;
+                PendingExit exit = exits.head;
                 if (exit.tree.hasTag(jk.treeTag) &&
                         jk.getTarget(exit.tree) == tree) {
                     exit.resolveJump();
                     resolved = true;
                 } else {

@@ -376,15 +376,15 @@
             return Liveness.from(resolved);
         }
 
         /** Resolve all continues of this statement. */
         Liveness resolveContinues(JCTree tree) {
-            return resolveJump(tree, new ListBuffer<P>(), JumpKind.CONTINUE);
+            return resolveJump(tree, new ListBuffer<PendingExit>(), JumpKind.CONTINUE);
         }
 
         /** Resolve all breaks of this statement. */
-        Liveness resolveBreaks(JCTree tree, ListBuffer<P> oldPendingExits) {
+        Liveness resolveBreaks(JCTree tree, ListBuffer<PendingExit> oldPendingExits) {
             return resolveJump(tree, oldPendingExits, JumpKind.BREAK);
         }
 
         @Override
         public void scan(JCTree tree) {

@@ -410,11 +410,11 @@
      * This pass implements the first step of the dataflow analysis, namely
      * the liveness analysis check. This checks that every statement is reachable.
      * The output of this analysis pass are used by other analyzers. This analyzer
      * sets the 'finallyCanCompleteNormally' field in the JCTry class.
      */
-    class AliveAnalyzer extends BaseAnalyzer<BaseAnalyzer.PendingExit> {
+    class AliveAnalyzer extends BaseAnalyzer {
 
         /** A flag that indicates whether the last statement could
          *  complete normally.
          */
         private Liveness alive;

@@ -829,11 +829,11 @@
      * This pass implements the second step of the dataflow analysis, namely
      * the exception analysis. This is to ensure that every checked exception that is
      * thrown is declared or caught. The analyzer uses some info that has been set by
      * the liveliness analyzer.
      */
-    class FlowAnalyzer extends BaseAnalyzer<FlowAnalyzer.FlowPendingExit> {
+    class FlowAnalyzer extends BaseAnalyzer {
 
         /** A flag that indicates whether the last statement could
          *  complete normally.
          */
         HashMap<Symbol, List<Type>> preciseRethrowTypes;

@@ -849,15 +849,15 @@
         /** The list of exceptions that are either caught or declared to be
          *  thrown.
          */
         List<Type> caught;
 
-        class FlowPendingExit extends BaseAnalyzer.PendingExit {
+        class ThrownPendingExit extends BaseAnalyzer.PendingExit {
 
             Type thrown;
 
-            FlowPendingExit(JCTree tree, Type thrown) {
+            ThrownPendingExit(JCTree tree, Type thrown) {
                 super(tree);
                 this.thrown = thrown;
             }
         }
 

@@ -869,36 +869,38 @@
         /*-------------------- Exceptions ----------------------*/
 
         /** Complain that pending exceptions are not caught.
          */
         void errorUncaught() {
-            for (FlowPendingExit exit = pendingExits.next();
+            for (PendingExit exit = pendingExits.next();
                  exit != null;
                  exit = pendingExits.next()) {
+                Assert.check(exit instanceof ThrownPendingExit);
+                ThrownPendingExit thrownExit = (ThrownPendingExit) exit;
                 if (classDef != null &&
                     classDef.pos == exit.tree.pos) {
                     log.error(exit.tree.pos(),
-                              Errors.UnreportedExceptionDefaultConstructor(exit.thrown));
+                              Errors.UnreportedExceptionDefaultConstructor(thrownExit.thrown));
                 } else if (exit.tree.hasTag(VARDEF) &&
                         ((JCVariableDecl)exit.tree).sym.isResourceVariable()) {
                     log.error(exit.tree.pos(),
-                              Errors.UnreportedExceptionImplicitClose(exit.thrown,
+                              Errors.UnreportedExceptionImplicitClose(thrownExit.thrown,
                                                                       ((JCVariableDecl)exit.tree).sym.name));
                 } else {
                     log.error(exit.tree.pos(),
-                              Errors.UnreportedExceptionNeedToCatchOrThrow(exit.thrown));
+                              Errors.UnreportedExceptionNeedToCatchOrThrow(thrownExit.thrown));
                 }
             }
         }
 
         /** Record that exception is potentially thrown and check that it
          *  is caught.
          */
         void markThrown(JCTree tree, Type exc) {
             if (!chk.isUnchecked(tree.pos(), exc)) {
                 if (!chk.isHandled(exc, caught)) {
-                    pendingExits.append(new FlowPendingExit(tree, exc));
+                    pendingExits.append(new ThrownPendingExit(tree, exc));
                 }
                 thrown = chk.incl(exc, thrown);
             }
         }
 

@@ -912,11 +914,11 @@
             if (tree.sym == null) return;
 
             JCClassDecl classDefPrev = classDef;
             List<Type> thrownPrev = thrown;
             List<Type> caughtPrev = caught;
-            ListBuffer<FlowPendingExit> pendingExitsPrev = pendingExits;
+            ListBuffer<PendingExit> pendingExitsPrev = pendingExits;
             Lint lintPrev = lint;
             boolean anonymousClass = tree.name == names.empty;
             pendingExits = new ListBuffer<>();
             if (!anonymousClass) {
                 caught = List.nil();

@@ -1022,16 +1024,16 @@
                 // else we are in an instance initializer block;
                 // leave caught unchanged.
 
                 scan(tree.body);
 
-                List<FlowPendingExit> exits = pendingExits.toList();
+                List<PendingExit> exits = pendingExits.toList();
                 pendingExits = new ListBuffer<>();
                 while (exits.nonEmpty()) {
-                    FlowPendingExit exit = exits.head;
+                    PendingExit exit = exits.head;
                     exits = exits.tail;
-                    if (exit.thrown == null) {
+                    if (!(exit instanceof ThrownPendingExit)) {
                         Assert.check(exit.tree.hasTag(RETURN));
                     } else {
                         // uncaught throws will be reported later
                         pendingExits.append(exit);
                     }

@@ -1057,29 +1059,29 @@
         public void visitBlock(JCBlock tree) {
             scan(tree.stats);
         }
 
         public void visitDoLoop(JCDoWhileLoop tree) {
-            ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
+            ListBuffer<PendingExit> prevPendingExits = pendingExits;
             pendingExits = new ListBuffer<>();
             scan(tree.body);
             resolveContinues(tree);
             scan(tree.cond);
             resolveBreaks(tree, prevPendingExits);
         }
 
         public void visitWhileLoop(JCWhileLoop tree) {
-            ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
+            ListBuffer<PendingExit> prevPendingExits = pendingExits;
             pendingExits = new ListBuffer<>();
             scan(tree.cond);
             scan(tree.body);
             resolveContinues(tree);
             resolveBreaks(tree, prevPendingExits);
         }
 
         public void visitForLoop(JCForLoop tree) {
-            ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
+            ListBuffer<PendingExit> prevPendingExits = pendingExits;
             scan(tree.init);
             pendingExits = new ListBuffer<>();
             if (tree.cond != null) {
                 scan(tree.cond);
             }

@@ -1089,20 +1091,20 @@
             resolveBreaks(tree, prevPendingExits);
         }
 
         public void visitForeachLoop(JCEnhancedForLoop tree) {
             visitVarDef(tree.var);
-            ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
+            ListBuffer<PendingExit> prevPendingExits = pendingExits;
             scan(tree.expr);
             pendingExits = new ListBuffer<>();
             scan(tree.body);
             resolveContinues(tree);
             resolveBreaks(tree, prevPendingExits);
         }
 
         public void visitLabelled(JCLabeledStatement tree) {
-            ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
+            ListBuffer<PendingExit> prevPendingExits = pendingExits;
             pendingExits = new ListBuffer<>();
             scan(tree.body);
             resolveBreaks(tree, prevPendingExits);
         }
 

@@ -1114,11 +1116,11 @@
         public void visitSwitchExpression(JCSwitchExpression tree) {
             handleSwitch(tree, tree.selector, tree.cases);
         }
 
         private void handleSwitch(JCTree tree, JCExpression selector, List<JCCase> cases) {
-            ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
+            ListBuffer<PendingExit> prevPendingExits = pendingExits;
             pendingExits = new ListBuffer<>();
             scan(selector);
             for (List<JCCase> l = cases; l.nonEmpty(); l = l.tail) {
                 JCCase c = l.head;
                 scan(c.pats);

@@ -1138,11 +1140,11 @@
                 for (JCExpression ct : subClauses) {
                     caught = chk.incl(ct.type, caught);
                 }
             }
 
-            ListBuffer<FlowPendingExit> prevPendingExits = pendingExits;
+            ListBuffer<PendingExit> prevPendingExits = pendingExits;
             pendingExits = new ListBuffer<>();
             for (JCTree resource : tree.resources) {
                 if (resource instanceof JCVariableDecl) {
                     JCVariableDecl vdecl = (JCVariableDecl) resource;
                     visitVarDef(vdecl);

@@ -1202,11 +1204,11 @@
                 preciseRethrowTypes.remove(param.sym);
             }
             if (tree.finalizer != null) {
                 List<Type> savedThrown = thrown;
                 thrown = List.nil();
-                ListBuffer<FlowPendingExit> exits = pendingExits;
+                ListBuffer<PendingExit> exits = pendingExits;
                 pendingExits = prevPendingExits;
                 scan(tree.finalizer);
                 if (!tree.finallyCanCompleteNormally) {
                     // discard exits and exceptions from try and finally
                     thrown = chk.union(thrown, thrownPrev);

@@ -1219,11 +1221,11 @@
                         pendingExits.append(exits.next());
                     }
                 }
             } else {
                 thrown = chk.union(thrown, chk.diff(thrownInTry, caughtInTry));
-                ListBuffer<FlowPendingExit> exits = pendingExits;
+                ListBuffer<PendingExit> exits = pendingExits;
                 pendingExits = prevPendingExits;
                 while (exits.nonEmpty()) pendingExits.append(exits.next());
             }
         }
 

@@ -1265,20 +1267,20 @@
             }
 
         public void visitBreak(JCBreak tree) {
             if (tree.isValueBreak())
                 scan(tree.value);
-            recordExit(new FlowPendingExit(tree, null));
+            recordExit(new PendingExit(tree));
         }
 
         public void visitContinue(JCContinue tree) {
-            recordExit(new FlowPendingExit(tree, null));
+            recordExit(new PendingExit(tree));
         }
 
         public void visitReturn(JCReturn tree) {
             scan(tree.expr);
-            recordExit(new FlowPendingExit(tree, null));
+            recordExit(new PendingExit(tree));
         }
 
         public void visitThrow(JCThrow tree) {
             scan(tree.expr);
             Symbol sym = TreeInfo.symbol(tree.expr);

@@ -1341,22 +1343,22 @@
                     tree.type.isErroneous()) {
                 return;
             }
             List<Type> prevCaught = caught;
             List<Type> prevThrown = thrown;
-            ListBuffer<FlowPendingExit> prevPending = pendingExits;
+            ListBuffer<PendingExit> prevPending = pendingExits;
             try {
                 pendingExits = new ListBuffer<>();
                 caught = tree.getDescriptorType(types).getThrownTypes();
                 thrown = List.nil();
                 scan(tree.body);
-                List<FlowPendingExit> exits = pendingExits.toList();
+                List<PendingExit> exits = pendingExits.toList();
                 pendingExits = new ListBuffer<>();
                 while (exits.nonEmpty()) {
-                    FlowPendingExit exit = exits.head;
+                    PendingExit exit = exits.head;
                     exits = exits.tail;
-                    if (exit.thrown == null) {
+                    if (!(exit instanceof ThrownPendingExit)) {
                         Assert.check(exit.tree.hasTag(RETURN));
                     } else {
                         // uncaught throws will be reported later
                         pendingExits.append(exit);
                     }

@@ -1486,11 +1488,11 @@
                     tree.type.isErroneous()) || inLambda) {
                 return;
             }
             List<Type> prevCaught = caught;
             List<Type> prevThrown = thrown;
-            ListBuffer<FlowPendingExit> prevPending = pendingExits;
+            ListBuffer<PendingExit> prevPending = pendingExits;
             inLambda = true;
             try {
                 pendingExits = new ListBuffer<>();
                 caught = List.of(syms.throwableType);
                 thrown = List.nil();

@@ -1515,11 +1517,11 @@
      * which ensures that no final variable is assigned more than once. This visitor
      * depends on the results of the liveliness analyzer. This pass is also used to mark
      * effectively-final local variables/parameters.
      */
 
-    public class AssignAnalyzer extends BaseAnalyzer<AssignAnalyzer.AssignPendingExit> {
+    public class AssignAnalyzer extends BaseAnalyzer {
 
         /** The set of definitely assigned variables.
          */
         final Bits inits;
 

@@ -1833,11 +1835,11 @@
                 }
 
                 JCClassDecl classDefPrev = classDef;
                 int firstadrPrev = firstadr;
                 int nextadrPrev = nextadr;
-                ListBuffer<AssignPendingExit> pendingExitsPrev = pendingExits;
+                ListBuffer<PendingExit> pendingExitsPrev = pendingExits;
 
                 pendingExits = new ListBuffer<>();
                 if (tree.name != names.empty) {
                     firstadr = nextadr;
                 }

@@ -1968,18 +1970,19 @@
                                     checkInit(TreeInfo.diagEndPos(tree.body), var);
                                 }
                             }
                         }
                     }
-                    List<AssignPendingExit> exits = pendingExits.toList();
+                    List<PendingExit> exits = pendingExits.toList();
                     pendingExits = new ListBuffer<>();
                     while (exits.nonEmpty()) {
-                        AssignPendingExit exit = exits.head;
+                        PendingExit exit = exits.head;
                         exits = exits.tail;
                         Assert.check(exit.tree.hasTag(RETURN), exit.tree);
                         if (isInitialConstructor) {
-                            inits.assign(exit.exit_inits);
+                            Assert.check(exit instanceof AssignPendingExit);
+                            inits.assign(((AssignPendingExit) exit).exit_inits);
                             for (int i = firstadr; i < nextadr; i++) {
                                 checkInit(exit.tree.pos(), vardecls[i].sym);
                             }
                         }
                     }

@@ -2025,11 +2028,11 @@
             scan(tree.stats);
             nextadr = nextadrPrev;
         }
 
         public void visitDoLoop(JCDoWhileLoop tree) {
-            ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
+            ListBuffer<PendingExit> prevPendingExits = pendingExits;
             FlowKind prevFlowKind = flowKind;
             flowKind = FlowKind.NORMAL;
             final Bits initsSkip = new Bits(true);
             final Bits uninitsSkip = new Bits(true);
             pendingExits = new ListBuffer<>();

@@ -2057,11 +2060,11 @@
             uninits.assign(uninitsSkip);
             resolveBreaks(tree, prevPendingExits);
         }
 
         public void visitWhileLoop(JCWhileLoop tree) {
-            ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
+            ListBuffer<PendingExit> prevPendingExits = pendingExits;
             FlowKind prevFlowKind = flowKind;
             flowKind = FlowKind.NORMAL;
             final Bits initsSkip = new Bits(true);
             final Bits uninitsSkip = new Bits(true);
             pendingExits = new ListBuffer<>();

@@ -2093,11 +2096,11 @@
             uninits.assign(uninitsSkip);
             resolveBreaks(tree, prevPendingExits);
         }
 
         public void visitForLoop(JCForLoop tree) {
-            ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
+            ListBuffer<PendingExit> prevPendingExits = pendingExits;
             FlowKind prevFlowKind = flowKind;
             flowKind = FlowKind.NORMAL;
             int nextadrPrev = nextadr;
             scan(tree.init);
             final Bits initsSkip = new Bits(true);

@@ -2141,11 +2144,11 @@
         }
 
         public void visitForeachLoop(JCEnhancedForLoop tree) {
             visitVarDef(tree.var);
 
-            ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
+            ListBuffer<PendingExit> prevPendingExits = pendingExits;
             FlowKind prevFlowKind = flowKind;
             flowKind = FlowKind.NORMAL;
             int nextadrPrev = nextadr;
             scan(tree.expr);
             final Bits initsStart = new Bits(inits);

@@ -2172,11 +2175,11 @@
             resolveBreaks(tree, prevPendingExits);
             nextadr = nextadrPrev;
         }
 
         public void visitLabelled(JCLabeledStatement tree) {
-            ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
+            ListBuffer<PendingExit> prevPendingExits = pendingExits;
             pendingExits = new ListBuffer<>();
             scan(tree.body);
             resolveBreaks(tree, prevPendingExits);
         }
 

@@ -2187,11 +2190,11 @@
         public void visitSwitchExpression(JCSwitchExpression tree) {
             handleSwitch(tree, tree.selector, tree.cases);
         }
 
         private void handleSwitch(JCTree tree, JCExpression selector, List<JCCase> cases) {
-            ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
+            ListBuffer<PendingExit> prevPendingExits = pendingExits;
             pendingExits = new ListBuffer<>();
             int nextadrPrev = nextadr;
             scanExpr(selector);
             final Bits initsSwitch = new Bits(inits);
             final Bits uninitsSwitch = new Bits(uninits);

@@ -2243,11 +2246,11 @@
             }
 
         public void visitTry(JCTry tree) {
             ListBuffer<JCVariableDecl> resourceVarDecls = new ListBuffer<>();
             final Bits uninitsTryPrev = new Bits(uninitsTry);
-            ListBuffer<AssignPendingExit> prevPendingExits = pendingExits;
+            ListBuffer<PendingExit> prevPendingExits = pendingExits;
             pendingExits = new ListBuffer<>();
             final Bits initsTry = new Bits(inits);
             uninitsTry.assign(uninits);
             for (JCTree resource : tree.resources) {
                 if (resource instanceof JCVariableDecl) {

@@ -2300,33 +2303,33 @@
                 nextadr = nextadrCatch;
             }
             if (tree.finalizer != null) {
                 inits.assign(initsTry);
                 uninits.assign(uninitsTry);
-                ListBuffer<AssignPendingExit> exits = pendingExits;
+                ListBuffer<PendingExit> exits = pendingExits;
                 pendingExits = prevPendingExits;
                 scan(tree.finalizer);
                 if (!tree.finallyCanCompleteNormally) {
                     // discard exits and exceptions from try and finally
                 } else {
                     uninits.andSet(uninitsEnd);
                     // FIX: this doesn't preserve source order of exits in catch
                     // versus finally!
                     while (exits.nonEmpty()) {
-                        AssignPendingExit exit = exits.next();
-                        if (exit.exit_inits != null) {
-                            exit.exit_inits.orSet(inits);
-                            exit.exit_uninits.andSet(uninits);
+                        PendingExit exit = exits.next();
+                        if (exit instanceof AssignPendingExit) {
+                            ((AssignPendingExit) exit).exit_inits.orSet(inits);
+                            ((AssignPendingExit) exit).exit_uninits.andSet(uninits);
                         }
                         pendingExits.append(exit);
                     }
                     inits.orSet(initsEnd);
                 }
             } else {
                 inits.assign(initsEnd);
                 uninits.assign(uninitsEnd);
-                ListBuffer<AssignPendingExit> exits = pendingExits;
+                ListBuffer<PendingExit> exits = pendingExits;
                 pendingExits = prevPendingExits;
                 while (exits.nonEmpty()) pendingExits.append(exits.next());
             }
             uninitsTry.andSet(uninitsTryPrev).andSet(uninits);
         }

@@ -2388,12 +2391,38 @@
             }
         }
 
         @Override
         public void visitBreak(JCBreak tree) {
-            if (tree.isValueBreak())
+            if (tree.isValueBreak()) {
+                if (tree.target.hasTag(SWITCH_EXPRESSION)) {
+                    JCSwitchExpression expr = (JCSwitchExpression) tree.target;
+                    if (expr.type.hasTag(BOOLEAN)) {
+                        scanCond(tree.value);
+                        Bits initsAfterBreakWhenTrue = new Bits(initsWhenTrue);
+                        Bits initsAfterBreakWhenFalse = new Bits(initsWhenFalse);
+                        Bits uninitsAfterBreakWhenTrue = new Bits(uninitsWhenTrue);
+                        Bits uninitsAfterBreakWhenFalse = new Bits(uninitsWhenFalse);
+                        PendingExit exit = new PendingExit(tree) {
+                            @Override
+                            void resolveJump() {
+                                if (!inits.isReset()) {
+                                    split(true);
+                                }
+                                initsWhenTrue.andSet(initsAfterBreakWhenTrue);
+                                initsWhenFalse.andSet(initsAfterBreakWhenFalse);
+                                uninitsWhenTrue.andSet(uninitsAfterBreakWhenTrue);
+                                uninitsWhenFalse.andSet(uninitsAfterBreakWhenFalse);
+                            }
+                        };
+                        merge();
+                        recordExit(exit);
+                        return ;
+                    }
+                }
                 scan(tree.value);
+            }
             recordExit(new AssignPendingExit(tree, inits, uninits));
         }
 
         @Override
         public void visitContinue(JCContinue tree) {

@@ -2426,11 +2455,11 @@
         public void visitLambda(JCLambda tree) {
             final Bits prevUninits = new Bits(uninits);
             final Bits prevInits = new Bits(inits);
             int returnadrPrev = returnadr;
             int nextadrPrev = nextadr;
-            ListBuffer<AssignPendingExit> prevPending = pendingExits;
+            ListBuffer<PendingExit> prevPending = pendingExits;
             try {
                 returnadr = nextadr;
                 pendingExits = new ListBuffer<>();
                 for (List<JCVariableDecl> l = tree.params; l.nonEmpty(); l = l.tail) {
                     JCVariableDecl def = l.head;

@@ -2616,11 +2645,11 @@
      * Additional this also checks that every variable that is used as an operand to
      * try-with-resources is final or effectively final.
      * As effectively final variables are marked as such during DA/DU, this pass must run after
      * AssignAnalyzer.
      */
-    class CaptureAnalyzer extends BaseAnalyzer<BaseAnalyzer.PendingExit> {
+    class CaptureAnalyzer extends BaseAnalyzer {
 
         JCTree currentTree; //local class or lambda
 
         @Override
         void markDead() {
< prev index next >