In mcs:
[mono.git] / mcs / mcs / flowanalysis.cs
index 70c9c851cd88a4ae9cb145217db963e13755a3d7..f24ede7b7f99cce46c7aec29a8d39c8da1fc35dc 100644 (file)
@@ -42,6 +42,12 @@ namespace Mono.CSharp
                        // A loop block.
                        Loop,
 
+                       // The statement embedded inside a loop
+                       Embedded,
+
+                       // part of a block headed by a jump target
+                       Labeled,
+
                        // Try/Catch block.
                        Exception,
 
@@ -49,7 +55,10 @@ namespace Mono.CSharp
                        Switch,
 
                        // Switch section.
-                       SwitchSection
+                       SwitchSection,
+
+                       // The toplevel block of a function
+                       Toplevel
                }
 
                // <summary>
@@ -66,14 +75,11 @@ namespace Mono.CSharp
 
                public sealed class Reachability
                {
-                       TriState returns, breaks, throws, barrier;
+                       TriState returns, throws, barrier;
 
                        public TriState Returns {
                                get { return returns; }
                        }
-                       public TriState Breaks {
-                               get { return breaks; }
-                       }
                        public TriState Throws {
                                get { return throws; }
                        }
@@ -81,17 +87,16 @@ namespace Mono.CSharp
                                get { return barrier; }
                        }
 
-                       Reachability (TriState returns, TriState breaks, TriState throws, TriState barrier)
+                       Reachability (TriState returns, TriState throws, TriState barrier)
                        {
                                this.returns = returns;
-                               this.breaks = breaks;
                                this.throws = throws;
                                this.barrier = barrier;
                        }
 
                        public Reachability Clone ()
                        {
-                               return new Reachability (returns, breaks, throws, barrier);
+                               return new Reachability (returns, throws, barrier);
                        }
 
                        public static TriState TriState_Meet (TriState a, TriState b)
@@ -108,137 +113,56 @@ namespace Mono.CSharp
                                return ((byte) a > (byte) b) ? a : b;
                        }
 
-                       public void Meet (Reachability b, bool do_break)
+                       public void Meet (Reachability b)
                        {
-                               //
-                               // `break' does not "break" in a Switch or a LoopBlock
-                               //
-                               bool a_breaks = do_break && AlwaysBreaks;
-                               bool b_breaks = do_break && b.AlwaysBreaks;
-
-                               bool a_has_barrier, b_has_barrier;
-                               if (do_break) {
-                                       //
-                                       // This is the normal case: the code following a barrier
-                                       // cannot be reached.
-                                       //
-                                       a_has_barrier = AlwaysHasBarrier;
-                                       b_has_barrier = b.AlwaysHasBarrier;
-                               } else {
-                                       //
-                                       // Special case for Switch and LoopBlocks: we can reach the
-                                       // code after the barrier via the `break'.
-                                       //
-                                       a_has_barrier = !AlwaysBreaks && AlwaysHasBarrier;
-                                       b_has_barrier = !b.AlwaysBreaks && b.AlwaysHasBarrier;
-                               }
-
-                               bool a_unreachable = a_breaks || a_has_barrier;
-                               bool b_unreachable = b_breaks || b_has_barrier;
-
-                               //
-                               // Do all code paths always return ?
-                               //
-                               if (AlwaysReturns) {
-                                       if (b.AlwaysReturns || b_unreachable)
-                                               returns = TriState.Always;
-                                       else
-                                               returns = TriState.Sometimes;
-                               } else if (b.AlwaysReturns) {
-                                       if (AlwaysReturns || a_unreachable)
-                                               returns = TriState.Always;
-                                       else
-                                               returns = TriState.Sometimes;
-                               } else if (!MayReturn) {
-                                       if (b.MayReturn)
-                                               returns = TriState.Sometimes;
-                                       else
-                                               returns = TriState.Never;
-                               } else if (!b.MayReturn) {
-                                       if (MayReturn)
-                                               returns = TriState.Sometimes;
-                                       else
-                                               returns = TriState.Never;
-                               }
+                               if ((AlwaysReturns && b.AlwaysHasBarrier) || (AlwaysHasBarrier && b.AlwaysReturns))
+                                       returns = TriState.Always;
+                               else
+                                       returns = TriState_Meet (returns, b.returns);
 
-                               breaks = TriState_Meet (breaks, b.breaks);
                                throws = TriState_Meet (throws, b.throws);
                                barrier = TriState_Meet (barrier, b.barrier);
-
-                               if (a_unreachable && b_unreachable)
-                                       barrier = TriState.Always;
-                               else if (a_unreachable || b_unreachable)
-                                       barrier = TriState.Sometimes;
-                               else
-                                       barrier = TriState.Never;
                        }
 
                        public void Or (Reachability b)
                        {
                                returns = TriState_Max (returns, b.returns);
-                               breaks = TriState_Max (breaks, b.breaks);
                                throws = TriState_Max (throws, b.throws);
                                barrier = TriState_Max (barrier, b.barrier);
                        }
 
                        public static Reachability Always ()
                        {
-                               return new Reachability (
-                                       TriState.Never, TriState.Never,
-                                       TriState.Never, TriState.Never);
+                               return new Reachability (TriState.Never, TriState.Never, TriState.Never);
+                       }
+
+                       TriState Unreachable {
+                               get { return TriState_Max (returns, TriState_Max (throws, barrier)); }
                        }
 
-                       public TriState Reachable {
+                       TriState Reachable {
                                get {
-                                       if ((returns == TriState.Always) ||
-                                           (breaks == TriState.Always) ||
-                                           (throws == TriState.Always) ||
-                                           (barrier == TriState.Always))
-                                               return TriState.Never;
-                                       else if ((returns == TriState.Never) &&
-                                                (breaks == TriState.Never) &&
-                                                (throws == TriState.Never) &&
-                                                (barrier == TriState.Never))
-                                               return TriState.Always;
-                                       else
+                                       TriState unreachable = Unreachable;
+                                       if (unreachable == TriState.Sometimes)
                                                return TriState.Sometimes;
+                                       return unreachable == TriState.Always ? TriState.Never : TriState.Always;
                                }
                        }
 
-                       public bool AlwaysBreaks {
-                               get { return breaks == TriState.Always; }
-                       }
-
-                       public bool MayBreak {
-                               get { return breaks != TriState.Never; }
-                       }
-
                        public bool AlwaysReturns {
                                get { return returns == TriState.Always; }
                        }
 
-                       public bool MayReturn {
-                               get { return returns != TriState.Never; }
-                       }
-
                        public bool AlwaysThrows {
                                get { return throws == TriState.Always; }
                        }
 
-                       public bool MayThrow {
-                               get { return throws != TriState.Never; }
-                       }
-
                        public bool AlwaysHasBarrier {
                                get { return barrier == TriState.Always; }
                        }
 
-                       public bool MayHaveBarrier {
-                               get { return barrier != TriState.Never; }
-                       }
-
                        public bool IsUnreachable {
-                               get { return Reachable == TriState.Never; }
+                               get { return Unreachable == TriState.Always; }
                        }
 
                        public void SetReturns ()
@@ -246,16 +170,6 @@ namespace Mono.CSharp
                                returns = TriState.Always;
                        }
 
-                       public void SetBreaks ()
-                       {
-                               breaks = TriState.Always;
-                       }
-
-                       public void ResetBreaks ()
-                       {
-                               breaks = TriState.Never;
-                       }
-
                        public void SetThrows ()
                        {
                                throws = TriState.Always;
@@ -266,11 +180,6 @@ namespace Mono.CSharp
                                barrier = TriState.Always;
                        }
 
-                       public void ResetBarrier ()
-                       {
-                               barrier = TriState.Never;
-                       }
-
                        static string ShortName (TriState returns)
                        {
                                switch (returns) {
@@ -285,9 +194,8 @@ namespace Mono.CSharp
 
                        public override string ToString ()
                        {
-                               return String.Format ("[{0}:{1}:{2}:{3}:{4}]",
-                                                     ShortName (returns), ShortName (breaks),
-                                                     ShortName (throws), ShortName (barrier),
+                               return String.Format ("[{0}:{1}:{2}:{3}]",
+                                                     ShortName (returns), ShortName (throws), ShortName (barrier),
                                                      ShortName (Reachable));
                        }
                }
@@ -296,10 +204,12 @@ namespace Mono.CSharp
                {
                        switch (type) {
                        case BranchingType.Exception:
+                       case BranchingType.Labeled:
+                       case BranchingType.Toplevel:
                                throw new InvalidOperationException ();
 
                        case BranchingType.Switch:
-                               return new FlowBranchingSwitch (parent, block, loc);
+                               return new FlowBranchingBreakable (parent, type, SiblingType.SwitchSection, block, loc);
 
                        case BranchingType.SwitchSection:
                                return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
@@ -308,7 +218,10 @@ namespace Mono.CSharp
                                return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
 
                        case BranchingType.Loop:
-                               return new FlowBranchingLoop (parent, block, loc);
+                               return new FlowBranchingBreakable (parent, type, SiblingType.Conditional, block, loc);
+
+                       case BranchingType.Embedded:
+                               return new FlowBranchingContinuable (parent, type, SiblingType.Conditional, block, loc);
 
                        default:
                                return new FlowBranchingBlock (parent, type, SiblingType.Conditional, block, loc);
@@ -336,11 +249,6 @@ namespace Mono.CSharp
                // </summary>
                public readonly Location Location;
 
-               // <summary>
-               //   If this is an infinite loop.
-               // </summary>
-               public bool Infinite;
-
                //
                // Private
                //
@@ -362,7 +270,7 @@ namespace Mono.CSharp
                        // <summary>
                        //   Start location of this branching.
                        // </summary>
-                       public readonly Location Location;
+                       public Location Location;
 
                        // <summary>
                        //   This is only valid for SwitchSection, Try, Catch and Finally.
@@ -528,14 +436,6 @@ namespace Mono.CSharp
                                }
                        }
 
-                       public void Break ()
-                       {
-                               if (!reachability.IsUnreachable) {
-                                       IsDirty = true;
-                                       reachability.SetBreaks ();
-                               }
-                       }
-
                        public void Throw ()
                        {
                                if (!reachability.IsUnreachable) {
@@ -594,30 +494,6 @@ namespace Mono.CSharp
                                return child;
                        }
 
-                       protected static void MergeFinally (UsageVector f_origins,
-                                                    MyBitVector f_params)
-                       {
-                               for (UsageVector vector = f_origins; vector != null; vector = vector.Next) {
-                                       MyBitVector temp_params = f_params.Clone ();
-                                       temp_params.Or (vector.Parameters);
-                               }
-                       }
-
-                       public void MergeFinally (UsageVector f_vector,
-                                                 UsageVector f_origins)
-                       {
-                               if (parameters != null) {
-                                       if (f_vector != null) {
-                                               MergeFinally (f_origins, f_vector.Parameters);
-                                               MyBitVector.Or (ref parameters, f_vector.ParameterVector);
-                                       } else
-                                               MergeFinally (f_origins, parameters);
-                               }
-
-                               if (f_vector != null && f_vector.LocalVector != null)
-                                       MyBitVector.Or (ref locals, f_vector.LocalVector);
-                       }
-
                        // <summary>
                        //   Tells control flow analysis that the current code position may be reached with
                        //   a forward jump from any of the origins listed in `origin_vectors' which is a
@@ -642,101 +518,62 @@ namespace Mono.CSharp
                        {
                                Report.Debug (1, "  MERGING JUMP ORIGINS", this);
 
-                               reachability = Reachability.Always ();
-
-                               if (o_vectors == null) {
-                                       reachability.SetBarrier ();
+                               if (o_vectors == null)
                                        return;
-                               }
-
-                               bool first = true;
-
-                               for (UsageVector vector = o_vectors; vector != null;
-                                    vector = vector.Next) {
-                                       Report.Debug (1, "  MERGING JUMP ORIGIN", vector,
-                                                     first, locals, vector.Locals);
-
-                                       if (first) {
-                                               if (locals != null && vector.Locals != null)
-                                                       locals.Or (vector.locals);
-                                               
-                                               if (parameters != null)
-                                                       parameters.Or (vector.parameters);
-                                               first = false;
-                                       } else {
-                                               if (locals != null)
-                                                       locals.And (vector.locals);
-                                               if (parameters != null)
-                                                       parameters.And (vector.parameters);
-                                       }
 
-                                       reachability.Meet (vector.Reachability, true);
-
-                                       Report.Debug (1, "  MERGING JUMP ORIGIN #1", vector);
+                               UsageVector vector = o_vectors;
+                               if (reachability.IsUnreachable) {
+                                       Report.Debug (1, "  MERGING JUMP ORIGIN INTO UNREACHABLE", this, vector);
+                                       if (locals != null && vector.Locals != null)
+                                               locals.Or (vector.locals);
+                                       if (parameters != null)
+                                               parameters.Or (vector.parameters);
+                                       reachability.Meet (vector.Reachability);
+                                       vector = vector.Next;
                                }
 
-                               Report.Debug (1, "  MERGING JUMP ORIGINS DONE", this);
-                       }
-
-                       // <summary>
-                       //   This is used at the beginning of a finally block if there were
-                       //   any return statements in the try block or one of the catch blocks.
-                       // </summary>
-                       public void MergeFinallyOrigins (UsageVector f_origins)
-                       {
-                               Report.Debug (1, "  MERGING FINALLY ORIGIN", this);
-
-                               reachability = Reachability.Always ();
-
-                               for (UsageVector vector = f_origins; vector != null; vector = vector.Next) {
-                                       Report.Debug (1, "    MERGING FINALLY ORIGIN", vector);
+                               for (; vector != null; vector = vector.Next) {
+                                       Report.Debug (1, "  MERGING JUMP ORIGIN", this, vector);
 
+                                       if (locals != null)
+                                               locals.And (vector.locals);
                                        if (parameters != null)
                                                parameters.And (vector.parameters);
+                                       reachability.Meet (vector.Reachability);
 
-                                       reachability.Meet (vector.Reachability, true);
+                                       Report.Debug (1, "  MERGING JUMP ORIGIN #1", vector);
                                }
 
-                               Report.Debug (1, "  MERGING FINALLY ORIGIN DONE", this);
+                               Report.Debug (1, "  MERGING JUMP ORIGINS DONE", this);
                        }
 
-                       public void MergeBreakOrigins (FlowBranching branching, UsageVector o_vectors)
+                       public void MergeOrigins (UsageVector o_vectors)
                        {
                                Report.Debug (1, "  MERGING BREAK ORIGINS", this);
 
                                if (o_vectors == null)
                                        return;
 
-                               bool first = branching.Infinite;
-
-                               for (UsageVector vector = o_vectors; vector != null;
-                                    vector = vector.Next) {
-                                       Report.Debug (1, "    MERGING BREAK ORIGIN", vector, first);
-
-                                       if (first) {
-                                               if (locals != null && vector.Locals != null)
-                                                       locals.Or (vector.locals);
-                                               
-                                               if (parameters != null)
-                                                       parameters.Or (vector.parameters);
-                                               first = false;
-                                       } else {
-                                               if (locals != null && vector.Locals != null)
-                                                       locals.And (vector.locals);
-                                               if (parameters != null)
-                                                       parameters.And (vector.parameters);
-                                       }
+                               UsageVector vector = o_vectors;
 
-                                       reachability.Meet (vector.Reachability, false);
+                               if (reachability.IsUnreachable) {
+                                       Report.Debug (1, "    MERGING BREAK ORIGIN INTO UNREACHABLE", vector);
+                                       locals = vector.Locals;
+                                       parameters = vector.Parameters;
+                                       reachability.Meet (vector.Reachability);
+                                       vector = vector.Next;
                                }
 
-                               Report.Debug (1, "  MERGING BREAK ORIGINS DONE", this);
-                       }
+                               for (; vector != null; vector = vector.Next) {
+                                       Report.Debug (1, "    MERGING BREAK ORIGIN", vector);
+                                       if (locals != null && vector.locals != null)
+                                               locals.And (vector.locals);
+                                       if (parameters != null && vector.parameters != null)
+                                               parameters.And (vector.parameters);
+                                       reachability.Meet (vector.Reachability);
+                               }
 
-                       public void CheckOutParameters (FlowBranching branching)
-                       {
-                               if (parameters != null)
-                                       branching.CheckOutParameters (parameters, branching.Location);
+                               Report.Debug (1, "  MERGING BREAK ORIGINS DONE", this);
                        }
 
                        // <summary>
@@ -892,6 +729,9 @@ namespace Mono.CSharp
                // </summary>
                public void CheckOutParameters (MyBitVector parameters, Location loc)
                {
+                       if (parameters == null)
+                               return;
+
                        for (int i = 0; i < param_map.Count; i++) {
                                VariableInfo var = param_map [i];
 
@@ -919,17 +759,12 @@ namespace Mono.CSharp
                        Report.Debug (2, "  MERGING SIBLINGS", this, Name);
 
                        for (UsageVector child = sibling_list; child != null; child = child.Next) {
-                               bool do_break = (Type != BranchingType.Switch) &&
-                                       (Type != BranchingType.Loop);
-
-                               Report.Debug (2, "    MERGING SIBLING   ", child,
-                                             child.ParameterVector, child.LocalVector,
-                                             reachability, child.Reachability, do_break);
+                               Report.Debug (2, "    MERGING SIBLING   ", reachability, child);
 
                                if (reachability == null)
                                        reachability = child.Reachability.Clone ();
                                else
-                                       reachability.Meet (child.Reachability, do_break);
+                                       reachability.Meet (child.Reachability);
 
                                // A local variable is initialized after a flow branching if it
                                // has been initialized in all its branches which do neither
@@ -966,16 +801,10 @@ namespace Mono.CSharp
                                // Here, `a' is initialized in line 3 and we must not look at
                                // line 5 since it always returns.
                                // 
-                               bool do_break_2 = (child.Type != SiblingType.Block) &&
-                                       (child.Type != SiblingType.SwitchSection);
-                               bool unreachable =
-                                       (do_break_2 && child.Reachability.AlwaysBreaks) ||
-                                       child.Reachability.AlwaysReturns ||
-                                       child.Reachability.AlwaysHasBarrier;
+                               bool unreachable = child.Reachability.IsUnreachable;
 
                                Report.Debug (2, "    MERGING SIBLING #1", reachability,
-                                             Type, child.Type, child.Reachability.IsUnreachable,
-                                             do_break_2, unreachable);
+                                             Type, child.Type, child.Reachability.IsUnreachable, unreachable);
 
                                if (!unreachable && (child.LocalVector != null))
                                        MyBitVector.And (ref locals, child.LocalVector);
@@ -991,11 +820,9 @@ namespace Mono.CSharp
                        if (reachability == null)
                                throw new InternalErrorException ("Cannot happen: the loop above runs at least twice");
 
-                       Report.Debug (2, "  MERGING SIBLINGS DONE", parameters, locals,
-                                     reachability, Infinite);
+                       Report.Debug (2, "  MERGING SIBLINGS DONE", parameters, locals, reachability);
 
-                       return new UsageVector (
-                               parameters, locals, reachability, null, Location);
+                       return new UsageVector (parameters, locals, reachability, null, Location);
                }
 
                protected abstract UsageVector Merge ();
@@ -1017,7 +844,7 @@ namespace Mono.CSharp
                // </summary>
                public Reachability MergeTopBlock ()
                {
-                       if ((Type != BranchingType.Block) || (Block == null))
+                       if ((Type != BranchingType.Toplevel) || (Block == null))
                                throw new NotSupportedException ();
 
                        UsageVector result = Merge ();
@@ -1030,52 +857,39 @@ namespace Mono.CSharp
                        return result.Reachability;
                }
 
-               //
-               // Checks whether we're in a `try' block.
-               //
-               public virtual bool InTryOrCatch (bool is_return)
-               {
-                       if (Block != null && Block.IsDestructor)
-                               return true;
-                       if (!is_return && (Type == BranchingType.Loop || Type == BranchingType.Switch))
-                               return false;
-                       return Parent != null && Parent.InTryOrCatch (is_return);
-               }
-
                public virtual bool InTryWithCatch ()
                {
                        return Parent != null && Parent.InTryWithCatch ();
                }
 
-               public virtual bool InLoop ()
-               {
-                       return Parent != null && Parent.InLoop ();
-               }
-
-               public virtual bool InSwitch ()
+               // returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
+               public virtual bool AddBreakOrigin (UsageVector vector, Location loc)
                {
-                       return Parent != null && Parent.InSwitch ();
-               }
+                       if (Parent != null)
+                               return Parent.AddBreakOrigin (vector, loc);
 
-               public virtual bool BreakCrossesTryCatchBoundary ()
-               {
-                       return Parent != null && Parent.BreakCrossesTryCatchBoundary ();
+                       Report.Error (139, loc, "No enclosing loop out of which to break or continue");
+                       return false;
                }
 
-               public virtual void AddFinallyVector (UsageVector vector)
+               // returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
+               public virtual bool AddContinueOrigin (UsageVector vector, Location loc)
                {
                        if (Parent != null)
-                               Parent.AddFinallyVector (vector);
-                       else if ((Block == null) || !Block.IsDestructor)
-                               throw new NotSupportedException ();
+                               return Parent.AddContinueOrigin (vector, loc);
+
+                       Report.Error (139, loc, "No enclosing loop out of which to break or continue");
+                       return false;
                }
 
-               public virtual void AddBreakVector (UsageVector vector)
+               // returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
+               public virtual bool AddReturnOrigin (UsageVector vector, Location loc)
                {
                        if (Parent != null)
-                               Parent.AddBreakVector (vector);
-                       else if ((Block == null) || !Block.IsDestructor)
-                               throw new NotSupportedException ();
+                               return Parent.AddReturnOrigin (vector, loc);
+
+                       CheckOutParameters (vector.Parameters, loc);
+                       return false;
                }
 
                public virtual void StealFinallyClauses (ref ArrayList list)
@@ -1180,109 +994,84 @@ namespace Mono.CSharp
                }
        }
 
-       public class FlowBranchingLoop : FlowBranchingBlock
+       public class FlowBranchingBreakable : FlowBranchingBlock
        {
                UsageVector break_origins;
 
-               public FlowBranchingLoop (FlowBranching parent, Block block, Location loc)
-                       : base (parent, BranchingType.Loop, SiblingType.Conditional, block, loc)
+               public FlowBranchingBreakable (FlowBranching parent, BranchingType type, SiblingType stype, Block block, Location loc)
+                       : base (parent, type, stype, block, loc)
                { }
 
-               public override void AddBreakVector (UsageVector vector)
+               public override bool AddBreakOrigin (UsageVector vector, Location loc)
                {
                        vector = vector.Clone ();
                        vector.Next = break_origins;
                        break_origins = vector;
-               }
-
-               public override bool InLoop ()
-               {
-                       return true;
-               }
-
-               public override bool BreakCrossesTryCatchBoundary ()
-               {
                        return false;
                }
 
                protected override UsageVector Merge ()
                {
                        UsageVector vector = base.Merge ();
-
-                       vector.MergeBreakOrigins (this, break_origins);
-
-                       Reachability r = vector.Reachability;
-
-                       if (r.MayBreak) {
-                               r.ResetBarrier ();
-                       } else if (Infinite) {
-                               r.SetBarrier ();
-                               if (r.MayReturn) {
-                                       // If we're an infinite loop and do not break,
-                                       // the code after the loop can never be reached.
-                                       // However, if we may return from the loop,
-                                       // then we do always return (or stay in the
-                                       // loop forever).
-                                       r.SetReturns ();
-                               }
-                       }
-
-                       // swallow up the 'break'
-                       r.ResetBreaks ();
-
+                       vector.MergeOrigins (break_origins);
                        return vector;
                }
        }
 
-       public class FlowBranchingSwitch : FlowBranchingBlock
+       public class FlowBranchingContinuable : FlowBranchingBlock
        {
-               UsageVector break_origins;
+               UsageVector continue_origins;
 
-               public FlowBranchingSwitch (FlowBranching parent, Block block, Location loc)
-                       : base (parent, BranchingType.Switch, SiblingType.SwitchSection, block, loc)
+               public FlowBranchingContinuable (FlowBranching parent, BranchingType type, SiblingType stype, Block block, Location loc)
+                       : base (parent, type, stype, block, loc)
                { }
 
-               public override void AddBreakVector (UsageVector vector)
+               public override bool AddContinueOrigin (UsageVector vector, Location loc)
                {
                        vector = vector.Clone ();
-                       vector.Next = break_origins;
-                       break_origins = vector;
+                       vector.Next = continue_origins;
+                       continue_origins = vector;
+                       return false;
                }
 
-               public override bool InSwitch ()
+               protected override UsageVector Merge ()
                {
-                       return true;
+                       UsageVector vector = base.Merge ();
+                       vector.MergeOrigins (continue_origins);
+                       return vector;
                }
+       }
 
-               public override bool BreakCrossesTryCatchBoundary ()
+       public class FlowBranchingLabeled : FlowBranchingBlock
+       {
+               LabeledStatement stmt;
+               public FlowBranchingLabeled (FlowBranching parent, LabeledStatement stmt)
+                       : base (parent, BranchingType.Labeled, SiblingType.Conditional, null, stmt.loc)
                {
-                       return false;
+                       this.stmt = stmt;
                }
+       }
 
-               protected override UsageVector Merge ()
+       public class FlowBranchingToplevel : FlowBranchingBlock
+       {
+               public FlowBranchingToplevel (FlowBranching parent, ToplevelBlock stmt)
+                       : base (parent, BranchingType.Toplevel, SiblingType.Conditional, stmt, stmt.loc)
                {
-                       UsageVector vector = base.Merge ();
-
-                       vector.MergeBreakOrigins (this, break_origins);
-
-                       Reachability r = vector.Reachability;
-
-                       if (r.MayBreak || r.MayReturn)
-                               r.ResetBarrier ();
-
-                       r.ResetBreaks ();
-
-                       return vector;
                }
        }
 
+
        public class FlowBranchingException : FlowBranching
        {
                ExceptionStatement stmt;
                UsageVector current_vector;
                UsageVector catch_vectors;
                UsageVector finally_vector;
-               UsageVector finally_origins;
+
+               UsageVector break_origins;
+               UsageVector continue_origins;
+               UsageVector return_origins;
+
                bool emit_finally;
 
                public FlowBranchingException (FlowBranching parent,
@@ -1296,18 +1085,18 @@ namespace Mono.CSharp
 
                protected override void AddSibling (UsageVector sibling)
                {
-                       if (sibling.Type == SiblingType.Try) {
+                       switch (sibling.Type) {
+                       case SiblingType.Try:
+                       case SiblingType.Catch:
                                sibling.Next = catch_vectors;
                                catch_vectors = sibling;
-                       } else if (sibling.Type == SiblingType.Catch) {
-                               sibling.Next = catch_vectors;
-                               catch_vectors = sibling;
-                       } else if (sibling.Type == SiblingType.Finally) {
-                               sibling.MergeFinallyOrigins (finally_origins);
+                               break;
+                       case SiblingType.Finally:
                                finally_vector = sibling;
-                       } else
+                               break;
+                       default:
                                throw new InvalidOperationException ();
-
+                       }
                        current_vector = sibling;
                }
 
@@ -1315,11 +1104,6 @@ namespace Mono.CSharp
                        get { return current_vector; }
                }
 
-               public override bool InTryOrCatch (bool is_return)
-               {
-                       return finally_vector == null;
-               }
-
                public override bool InTryWithCatch ()
                {
                        if (finally_vector == null) {
@@ -1331,16 +1115,43 @@ namespace Mono.CSharp
                        return base.InTryWithCatch ();
                }
 
-               public override bool BreakCrossesTryCatchBoundary ()
+               public override bool AddBreakOrigin (UsageVector vector, Location loc)
                {
+                       if (finally_vector != null) {
+                               Report.Error (157, loc, "Control cannot leave the body of a finally clause");
+                       } else {
+                               vector = vector.Clone ();
+                               vector.Location = loc;
+                               vector.Next = break_origins;
+                               break_origins = vector;
+                       }
                        return true;
                }
 
-               public override void AddFinallyVector (UsageVector vector)
+               public override bool AddContinueOrigin (UsageVector vector, Location loc)
                {
-                       vector = vector.Clone ();
-                       vector.Next = finally_origins;
-                       finally_origins = vector;
+                       if (finally_vector != null) {
+                               Report.Error (157, loc, "Control cannot leave the body of a finally clause");
+                       } else {
+                               vector = vector.Clone ();
+                               vector.Location = loc;
+                               vector.Next = continue_origins;
+                               continue_origins = vector;
+                       }
+                       return true;
+               }
+
+               public override bool AddReturnOrigin (UsageVector vector, Location loc)
+               {
+                       if (finally_vector != null) {
+                               Report.Error (157, loc, "Control cannot leave the body of a finally clause");
+                       } else {
+                               vector = vector.Clone ();
+                               vector.Location = loc;
+                               vector.Next = return_origins;
+                               return_origins = vector;
+                       }
+                       return true;
                }
 
                public override void StealFinallyClauses (ref ArrayList list)
@@ -1383,7 +1194,29 @@ namespace Mono.CSharp
                {
                        UsageVector vector = Merge (catch_vectors);
 
-                       vector.MergeFinally (finally_vector, finally_origins);
+                       if (finally_vector != null)
+                               vector.MergeChild (finally_vector, false);
+
+                       for (UsageVector origin = break_origins; origin != null; origin = origin.Next) {
+                               if (finally_vector != null)
+                                       origin.MergeChild (finally_vector, false);
+                               if (!origin.Reachability.IsUnreachable)
+                                       Parent.AddBreakOrigin (origin, origin.Location);
+                       }
+
+                       for (UsageVector origin = continue_origins; origin != null; origin = origin.Next) {
+                               if (finally_vector != null)
+                                       origin.MergeChild (finally_vector, false);
+                               if (!origin.Reachability.IsUnreachable)
+                                       Parent.AddContinueOrigin (origin, origin.Location);
+                       }
+
+                       for (UsageVector origin = return_origins; origin != null; origin = origin.Next) {
+                               if (finally_vector != null)
+                                       origin.MergeChild (finally_vector, false);
+                               if (!origin.Reachability.IsUnreachable)
+                                       Parent.AddReturnOrigin (origin, origin.Location);
+                       }
 
                        return vector;
                }