2005-05-12 Jordi Mas i Hernandez <jordi@ximian.com>
[mono.git] / mcs / mcs / flowanalysis.cs
index 9d8ca8dc9c30367c4106e079fa41263ca0ecb1fc..d46f3c09a1872bb6b210be120539d3cad66fa3d3 100644 (file)
@@ -25,12 +25,15 @@ namespace Mono.CSharp
                // <summary>
                //   The type of a FlowBranching.
                // </summary>
-               public enum BranchingType {
+               public enum BranchingType : byte {
                        // Normal (conditional or toplevel) block.
                        Block,
 
+                       // Conditional.
+                       Conditional,
+
                        // A loop block.
-                       LoopBlock,
+                       Loop,
 
                        // Try/Catch block.
                        Exception,
@@ -45,7 +48,8 @@ namespace Mono.CSharp
                // <summary>
                //   The type of one sibling of a branching.
                // </summary>
-               public enum SiblingType {
+               public enum SiblingType : byte {
+                       Block,
                        Conditional,
                        SwitchSection,
                        Try,
@@ -58,7 +62,7 @@ namespace Mono.CSharp
                //   current code block may return to its enclosing block before reaching
                //   its end.
                // </summary>
-               public enum FlowReturns {
+               public enum FlowReturns : byte {
                        Undefined = 0,
 
                        // It can never return.
@@ -70,25 +74,310 @@ namespace Mono.CSharp
 
                        // The code always returns, ie. there's an unconditional return / break
                        // statement in it.
-                       Always,
+                       Always
+               }
 
-                       // The code always throws an exception.
-                       Exception,
+               public sealed class Reachability
+               {
+                       FlowReturns returns, breaks, throws, barrier;
+
+                       public FlowReturns Returns {
+                               get { return returns; }
+                       }
+                       public FlowReturns Breaks {
+                               get { return breaks; }
+                       }
+                       public FlowReturns Throws {
+                               get { return throws; }
+                       }
+                       public FlowReturns Barrier {
+                               get { return barrier; }
+                       }
+                       public Reachability (FlowReturns returns, FlowReturns breaks,
+                                            FlowReturns throws, FlowReturns barrier)
+                       {
+                               this.returns = returns;
+                               this.breaks = breaks;
+                               this.throws = throws;
+                               this.barrier = barrier;
+                       }
+
+                       public Reachability Clone ()
+                       {
+                               return new Reachability (returns, breaks, throws, barrier);
+                       }
+
+                       // <summary>
+                       //   Performs an `And' operation on the FlowReturns status
+                       //   (for instance, a block only returns Always if all its siblings
+                       //   always return).
+                       // </summary>
+                       public static FlowReturns AndFlowReturns (FlowReturns a, FlowReturns b)
+                       {
+                               if (a == FlowReturns.Undefined)
+                                       return b;
+
+                               switch (a) {
+                               case FlowReturns.Never:
+                                       if (b == FlowReturns.Never)
+                                               return FlowReturns.Never;
+                                       else
+                                               return FlowReturns.Sometimes;
+
+                               case FlowReturns.Sometimes:
+                                       return FlowReturns.Sometimes;
+
+                               case FlowReturns.Always:
+                                       if (b == FlowReturns.Always)
+                                               return FlowReturns.Always;
+                                       else
+                                               return FlowReturns.Sometimes;
+
+                               default:
+                                       throw new ArgumentException ();
+                               }
+                       }
+
+                       public static FlowReturns OrFlowReturns (FlowReturns a, FlowReturns b)
+                       {
+                               if (a == FlowReturns.Undefined)
+                                       return b;
+
+                               switch (a) {
+                               case FlowReturns.Never:
+                                       return b;
+
+                               case FlowReturns.Sometimes:
+                                       if (b == FlowReturns.Always)
+                                               return FlowReturns.Always;
+                                       else
+                                               return FlowReturns.Sometimes;
+
+                               case FlowReturns.Always:
+                                       return FlowReturns.Always;
+
+                               default:
+                                       throw new ArgumentException ();
+                               }
+                       }
+
+                       public static void And (ref Reachability a, Reachability b, bool do_break)
+                       {
+                               if (a == null) {
+                                       a = b.Clone ();
+                                       return;
+                               }
+
+                               //
+                               // `break' does not "break" in a Switch or a LoopBlock
+                               //
+                               bool a_breaks = do_break && a.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 = a.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 = !a.AlwaysBreaks && a.AlwaysHasBarrier;
+                                       b_has_barrier = !b.AlwaysBreaks && b.AlwaysHasBarrier;
+                               }
+
+                               bool a_unreachable = a_breaks || a.AlwaysThrows || a_has_barrier;
+                               bool b_unreachable = b_breaks || b.AlwaysThrows || b_has_barrier;
+
+                               //
+                               // Do all code paths always return ?
+                               //
+                               if (a.AlwaysReturns) {
+                                       if (b.AlwaysReturns || b_unreachable)
+                                               a.returns = FlowReturns.Always;
+                                       else
+                                               a.returns = FlowReturns.Sometimes;
+                               } else if (b.AlwaysReturns) {
+                                       if (a.AlwaysReturns || a_unreachable)
+                                               a.returns = FlowReturns.Always;
+                                       else
+                                               a.returns = FlowReturns.Sometimes;
+                               } else if (!a.MayReturn) {
+                                       if (b.MayReturn)
+                                               a.returns = FlowReturns.Sometimes;
+                                       else
+                                               a.returns = FlowReturns.Never;
+                               } else if (!b.MayReturn) {
+                                       if (a.MayReturn)
+                                               a.returns = FlowReturns.Sometimes;
+                                       else
+                                               a.returns = FlowReturns.Never;
+                               }
+
+                               a.breaks = AndFlowReturns (a.breaks, b.breaks);
+                               a.throws = AndFlowReturns (a.throws, b.throws);
+                               a.barrier = AndFlowReturns (a.barrier, b.barrier);
+
+                               if (a_unreachable && b_unreachable)
+                                       a.barrier = FlowReturns.Always;
+                               else if (a_unreachable || b_unreachable)
+                                       a.barrier = FlowReturns.Sometimes;
+                               else
+                                       a.barrier = FlowReturns.Never;
+                       }
+
+                       public void Or (Reachability b)
+                       {
+                               returns = OrFlowReturns (returns, b.returns);
+                               breaks = OrFlowReturns (breaks, b.breaks);
+                               throws = OrFlowReturns (throws, b.throws);
+                               barrier = OrFlowReturns (barrier, b.barrier);
+                       }
+
+                       public static Reachability Never ()
+                       {
+                               return new Reachability (
+                                       FlowReturns.Never, FlowReturns.Never,
+                                       FlowReturns.Never, FlowReturns.Never);
+                       }
+
+                       public FlowReturns Reachable {
+                               get {
+                                       if ((returns == FlowReturns.Always) ||
+                                           (breaks == FlowReturns.Always) ||
+                                           (throws == FlowReturns.Always) ||
+                                           (barrier == FlowReturns.Always))
+                                               return FlowReturns.Never;
+                                       else if ((returns == FlowReturns.Never) &&
+                                                (breaks == FlowReturns.Never) &&
+                                                (throws == FlowReturns.Never) &&
+                                                (barrier == FlowReturns.Never))
+                                               return FlowReturns.Always;
+                                       else
+                                               return FlowReturns.Sometimes;
+                               }
+                       }
+
+                       public bool AlwaysBreaks {
+                               get { return breaks == FlowReturns.Always; }
+                       }
+
+                       public bool MayBreak {
+                               get { return breaks != FlowReturns.Never; }
+                       }
+
+                       public bool AlwaysReturns {
+                               get { return returns == FlowReturns.Always; }
+                       }
+
+                       public bool MayReturn {
+                               get { return returns != FlowReturns.Never; }
+                       }
+
+                       public bool AlwaysThrows {
+                               get { return throws == FlowReturns.Always; }
+                       }
+
+                       public bool MayThrow {
+                               get { return throws != FlowReturns.Never; }
+                       }
+
+                       public bool AlwaysHasBarrier {
+                               get { return barrier == FlowReturns.Always; }
+                       }
+
+                       public bool MayHaveBarrier {
+                               get { return barrier != FlowReturns.Never; }
+                       }
+
+                       public bool IsUnreachable {
+                               get { return Reachable == FlowReturns.Never; }
+                       }
+
+                       public void SetReturns ()
+                       {
+                               returns = FlowReturns.Always;
+                       }
+
+                       public void SetReturnsSometimes ()
+                       {
+                               returns = FlowReturns.Sometimes;
+                       }
+
+                       public void SetBreaks ()
+                       {
+                               breaks = FlowReturns.Always;
+                       }
+
+                       public void ResetBreaks ()
+                       {
+                               breaks = FlowReturns.Never;
+                       }
+
+                       public void SetThrows ()
+                       {
+                               throws = FlowReturns.Always;
+                       }
+
+                       public void SetThrowsSometimes ()
+                       {
+                               throws = FlowReturns.Sometimes;
+                       }
+
+                       public void SetBarrier ()
+                       {
+                               barrier = FlowReturns.Always;
+                       }
+
+                       public void ResetBarrier ()
+                       {
+                               barrier = FlowReturns.Never;
+                       }
+
+                       static string ShortName (FlowReturns returns)
+                       {
+                               switch (returns) {
+                               case FlowReturns.Never:
+                                       return "N";
+                               case FlowReturns.Sometimes:
+                                       return "S";
+                               default:
+                                       return "A";
+                               }
+                       }
 
-                       // The current code block is unreachable.  This happens if it's immediately
-                       // following a FlowReturns.Always block.
-                       Unreachable
+                       public override string ToString ()
+                       {
+                               return String.Format ("[{0}:{1}:{2}:{3}:{4}]",
+                                                     ShortName (returns), ShortName (breaks),
+                                                     ShortName (throws), ShortName (barrier),
+                                                     ShortName (Reachable));
+                       }
                }
 
                public static FlowBranching CreateBranching (FlowBranching parent, BranchingType type, Block block, Location loc)
                {
                        switch (type) {
                        case BranchingType.Exception:
-                               return new FlowBranchingException (parent, type, block, loc);
+                               throw new InvalidOperationException ();
 
                        case BranchingType.Switch:
                                return new FlowBranchingBlock (parent, type, SiblingType.SwitchSection, block, loc);
 
+                       case BranchingType.SwitchSection:
+                               return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
+
+                       case BranchingType.Block:
+                               return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
+
+                       case BranchingType.Loop:
+                               return new FlowBranchingLoop (parent, block, loc);
+
                        default:
                                return new FlowBranchingBlock (parent, type, SiblingType.Conditional, block, loc);
                        }
@@ -120,11 +409,6 @@ namespace Mono.CSharp
                // </summary>
                public bool Infinite;
 
-               // <summary>
-               //   If we may leave the current loop.
-               // </summary>
-               public bool MayLeaveLoop;
-
                //
                // Private
                //
@@ -133,46 +417,6 @@ namespace Mono.CSharp
                static int next_id = 0;
                int id;
 
-               // <summary>
-               //   Performs an `And' operation on the FlowReturns status
-               //   (for instance, a block only returns Always if all its siblings
-               //   always return).
-               // </summary>
-               public static FlowReturns AndFlowReturns (FlowReturns a, FlowReturns b)
-               {
-                       if (a == FlowReturns.Undefined)
-                               return b;
-                       if (b == FlowReturns.Unreachable)
-                               return a;
-
-                       switch (a) {
-                       case FlowReturns.Never:
-                               if (b == FlowReturns.Never)
-                                       return FlowReturns.Never;
-                               else
-                                       return FlowReturns.Sometimes;
-
-                       case FlowReturns.Sometimes:
-                               return FlowReturns.Sometimes;
-
-                       case FlowReturns.Always:
-                               if ((b == FlowReturns.Always) || (b == FlowReturns.Exception))
-                                       return FlowReturns.Always;
-                               else
-                                       return FlowReturns.Sometimes;
-
-                       case FlowReturns.Exception:
-                               if (b == FlowReturns.Exception)
-                                       return FlowReturns.Exception;
-                               else if (b == FlowReturns.Always)
-                                       return FlowReturns.Always;
-                               else
-                                       return FlowReturns.Sometimes;
-                       }
-
-                       return b;
-               }
-
                // <summary>
                //   The vector contains a BitArray with information about which local variables
                //   and parameters are already initialized at the current code position.
@@ -188,6 +432,11 @@ namespace Mono.CSharp
                        // </summary>
                        public readonly Location Location;
 
+                       // <summary>
+                       //   This is only valid for SwitchSection, Try, Catch and Finally.
+                       // </summary>
+                       public readonly Block Block;
+
                        // <summary>
                        //   If this is true, then the usage vector has been modified and must be
                        //   merged when we're done with this branching.
@@ -211,12 +460,16 @@ namespace Mono.CSharp
                        // </summary>
                        public readonly UsageVector InheritsFrom;
 
+                       // <summary>
+                       //   This is used to construct a list of UsageVector's.
+                       // </summary>
+                       public UsageVector Next;
+
                        //
                        // Private.
                        //
                        MyBitVector locals, parameters;
-                       FlowReturns RealReturns, RealBreaks, RealReachable;
-                       bool is_finally;
+                       Reachability reachability;
 
                        static int next_id = 0;
                        int id;
@@ -224,56 +477,82 @@ namespace Mono.CSharp
                        //
                        // Normally, you should not use any of these constructors.
                        //
-                       public UsageVector (SiblingType type, UsageVector parent, Location loc, int num_params, int num_locals)
+                       public UsageVector (SiblingType type, UsageVector parent,
+                                           Block block, Location loc,
+                                           int num_params, int num_locals)
                        {
                                this.Type = type;
+                               this.Block = block;
                                this.Location = loc;
                                this.InheritsFrom = parent;
                                this.CountParameters = num_params;
                                this.CountLocals = num_locals;
-                               this.RealReturns = FlowReturns.Never;
-                               this.RealBreaks = FlowReturns.Never;
-                               this.RealReachable = FlowReturns.Always;
 
                                if (parent != null) {
-                                       locals = new MyBitVector (parent.locals, CountLocals);
+                                       if (num_locals > 0)
+                                               locals = new MyBitVector (parent.locals, CountLocals);
+                                       
                                        if (num_params > 0)
                                                parameters = new MyBitVector (parent.parameters, num_params);
-                                       RealReturns = parent.Returns;
-                                       RealBreaks = parent.Breaks;
+
+                                       reachability = parent.Reachability.Clone ();
                                } else {
-                                       locals = new MyBitVector (null, CountLocals);
+                                       if (num_locals > 0)
+                                               locals = new MyBitVector (null, CountLocals);
+                                       
                                        if (num_params > 0)
                                                parameters = new MyBitVector (null, num_params);
+
+                                       reachability = Reachability.Never ();
                                }
 
                                id = ++next_id;
                        }
 
-                       public UsageVector (SiblingType type, UsageVector parent, Location loc)
-                               : this (type, parent, loc, parent.CountParameters, parent.CountLocals)
+                       public UsageVector (SiblingType type, UsageVector parent,
+                                           Block block, Location loc)
+                               : this (type, parent, block, loc,
+                                       parent.CountParameters, parent.CountLocals)
                        { }
 
+                       public UsageVector (MyBitVector parameters, MyBitVector locals,
+                                           Reachability reachability, Block block,
+                                           Location loc)
+                       {
+                               this.Type = SiblingType.Block;
+                               this.Location = loc;
+                               this.Block = block;
+
+                               this.reachability = reachability;
+                               this.parameters = parameters;
+                               this.locals = locals;
+
+                               id = ++next_id;
+                       }
+
                        // <summary>
                        //   This does a deep copy of the usage vector.
                        // </summary>
                        public UsageVector Clone ()
                        {
-                               UsageVector retval = new UsageVector (Type, null, Location, CountParameters, CountLocals);
+                               UsageVector retval = new UsageVector (
+                                       Type, null, Block, Location,
+                                       CountParameters, CountLocals);
 
-                               retval.locals = locals.Clone ();
+                               if (retval.locals != null)
+                                       retval.locals = locals.Clone ();
+                               
                                if (parameters != null)
                                        retval.parameters = parameters.Clone ();
-                               retval.RealReturns = RealReturns;
-                               retval.RealBreaks = RealBreaks;
-                               retval.RealReachable = RealReachable;
+                               
+                               retval.reachability = reachability.Clone ();
 
                                return retval;
                        }
 
                        public bool IsAssigned (VariableInfo var)
                        {
-                               if (!var.IsParameter && AlwaysBreaks)
+                               if (!var.IsParameter && Reachability.IsUnreachable)
                                        return true;
 
                                return var.IsAssigned (var.IsParameter ? parameters : locals);
@@ -281,15 +560,16 @@ namespace Mono.CSharp
 
                        public void SetAssigned (VariableInfo var)
                        {
-                               if (!var.IsParameter && AlwaysBreaks)
+                               if (!var.IsParameter && Reachability.IsUnreachable)
                                        return;
 
+                               IsDirty = true;
                                var.SetAssigned (var.IsParameter ? parameters : locals);
                        }
 
                        public bool IsFieldAssigned (VariableInfo var, string name)
                        {
-                               if (!var.IsParameter && AlwaysBreaks)
+                               if (!var.IsParameter && Reachability.IsUnreachable)
                                        return true;
 
                                return var.IsFieldAssigned (var.IsParameter ? parameters : locals, name);
@@ -297,123 +577,98 @@ namespace Mono.CSharp
 
                        public void SetFieldAssigned (VariableInfo var, string name)
                        {
-                               if (!var.IsParameter && AlwaysBreaks)
+                               if (!var.IsParameter && Reachability.IsUnreachable)
                                        return;
 
+                               IsDirty = true;
                                var.SetFieldAssigned (var.IsParameter ? parameters : locals, name);
                        }
 
-                       // <summary>
-                       //   Specifies when the current block returns.
-                       //   If this is FlowReturns.Unreachable, then control can never reach the
-                       //   end of the method (so that we don't need to emit a return statement).
-                       //   The same applies for FlowReturns.Exception, but in this case the return
-                       //   value will never be used.
-                       // </summary>
-                       public FlowReturns Returns {
-                               get {
-                                       return RealReturns;
-                               }
-                       }
-
-                       // <summary>
-                       //   Specifies whether control may return to our containing block
-                       //   before reaching the end of this block.  This happens if there
-                       //   is a break/continue/goto/return in it.
-                       //   This can also be used to find out whether the statement immediately
-                       //   following the current block may be reached or not.
-                       // </summary>
-                       public FlowReturns Breaks {
-                               get {
-                                       return RealBreaks;
-                               }
-                       }
-
-                       public FlowReturns Reachable {
-                               get {
-                                       return RealReachable;
-                               }
-                       }
-
-                       public bool AlwaysBreaks {
+                       public Reachability Reachability {
                                get {
-                                       return (Breaks == FlowReturns.Always) ||
-                                               (Breaks == FlowReturns.Exception) ||
-                                               (Breaks == FlowReturns.Unreachable);
+                                       return reachability;
                                }
                        }
 
-                       public bool MayBreak {
-                               get {
-                                       return Breaks != FlowReturns.Never;
-                               }
-                       }
-
-                       public bool AlwaysReturns {
-                               get {
-                                       return (Returns == FlowReturns.Always) ||
-                                               (Returns == FlowReturns.Exception);
-                               }
-                       }
-
-                       public bool MayReturn {
-                               get {
-                                       return (Returns == FlowReturns.Sometimes) ||
-                                               (Returns == FlowReturns.Always);
-                               }
-                       }
-
-                       public void Break ()
-                       {
-                               RealBreaks = FlowReturns.Always;
-                       }
-
                        public void Return ()
                        {
-                               RealReturns = FlowReturns.Always;
-                       }
-
-                       public bool IsUnreachable {
-                               get {
-                                       return (Reachable == FlowReturns.Exception) ||
-                                               (Reachable == FlowReturns.Unreachable);
+                               if (!reachability.IsUnreachable) {
+                                       IsDirty = true;
+                                       reachability.SetReturns ();
                                }
                        }
 
-                       public void Unreachable ()
+                       public void Break ()
                        {
-                               // If we're already unreachable, don't modify the reason why.
-                               if (!IsUnreachable)
-                                       RealReachable = FlowReturns.Unreachable;
+                               if (!reachability.IsUnreachable) {
+                                       IsDirty = true;
+                                       reachability.SetBreaks ();
+                               }
                        }
 
-                       public void NeverReachable ()
+                       public void Throw ()
                        {
-                               // If we're already unreachable, don't modify the reason why.
-                               if (!IsUnreachable)
-                                       RealReachable = FlowReturns.Never;
+                               if (!reachability.IsUnreachable) {
+                                       IsDirty = true;
+                                       reachability.SetThrows ();
+                               }
                        }
 
-                       public void Throw ()
+                       public void Goto ()
                        {
-                               // If we're already unreachable, don't modify the reason why.
-                               if (!IsUnreachable)
-                                       RealReachable = FlowReturns.Exception;
+                               if (!reachability.IsUnreachable) {
+                                       IsDirty = true;
+                                       reachability.SetBarrier ();
+                               }
                        }
 
                        // <summary>
                        //   Merges a child branching.
                        // </summary>
-                       public FlowReturns MergeChild (MyBitVector new_params, MyBitVector new_locals,
-                                                      FlowReturns new_returns, FlowReturns new_breaks,
-                                                      FlowReturns new_reachable)
+                       public UsageVector MergeChild (FlowBranching branching)
                        {
-                               Report.Debug (2, "MERGING CHILD", this, new_params, new_locals, new_returns, new_breaks,
-                                             new_reachable);
+                               UsageVector result = branching.Merge ();
+
+                               Report.Debug (2, "  MERGING CHILD", this, branching, IsDirty,
+                                             result.ParameterVector, result.LocalVector,
+                                             result.Reachability, reachability, Type);
+
+                               Reachability new_r = result.Reachability;
+
+                               if (branching.Type == BranchingType.Loop) {
+                                       bool may_leave_loop = new_r.MayBreak;
+                                       new_r.ResetBreaks ();
+
+                                       if (branching.Infinite && !may_leave_loop) {
+                                               if (new_r.Returns == FlowReturns.Sometimes) {
+                                                       // 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).
+                                                       new_r.SetReturns ();
+                                               }
 
-                               RealReturns = new_returns;
-                               RealBreaks = new_breaks;
-                               RealReachable = new_reachable;
+                                               new_r.SetBarrier ();
+                                       } else {
+                                               if (new_r.Returns == FlowReturns.Always) {
+                                                       // We're either finite or we may leave the loop.
+                                                       new_r.SetReturnsSometimes ();
+                                               }
+                                               if (new_r.Throws == FlowReturns.Always) {
+                                                       // We're either finite or we may leave the loop.
+                                                       new_r.SetThrowsSometimes ();
+                                               }
+
+                                               if (!new_r.MayReturn && !new_r.MayThrow)
+                                                       new_r.ResetBarrier ();
+                                       }
+                               } else if (branching.Type == BranchingType.Switch) {
+                                       if (new_r.MayBreak || new_r.MayReturn)
+                                               new_r.ResetBarrier ();
+
+                                       new_r.ResetBreaks ();
+                               }
 
                                //
                                // We've now either reached the point after the branching or we will
@@ -424,27 +679,54 @@ namespace Mono.CSharp
                                // we need to look at (see above).
                                //
 
-                               Report.Debug (2, "MERGING CHILD #1", this, Returns, Breaks, Reachable, new_locals, new_params);
+                               if ((Type == SiblingType.SwitchSection) && !new_r.IsUnreachable) {
+                                       Report.Error (163, Location,
+                                                     "Control cannot fall through from one " +
+                                                     "case label to another");
+                                       return result;
+                               }
 
-                               if ((Reachable == FlowReturns.Always) || (Reachable == FlowReturns.Sometimes) ||
-                                   (Reachable == FlowReturns.Never)) {
-                                       if ((Returns == FlowReturns.Always) || (Breaks == FlowReturns.Always))
-                                               RealReachable = FlowReturns.Never;
-                                       if ((Type == SiblingType.SwitchSection) && (Reachable != FlowReturns.Never)) {
-                                               Report.Error (163, Location, "Control cannot fall through from one " +
-                                                             "case label to another");
-                                       }
+                               if (locals != null && result.LocalVector != null)
+                                       locals.Or (result.LocalVector);
+
+                               if (result.ParameterVector != null)
+                                       parameters.Or (result.ParameterVector);
+
+                               if ((branching.Type == BranchingType.Block) && branching.Block.Implicit)
+                                       reachability = new_r.Clone ();
+                               else
+                                       reachability.Or (new_r);
 
-                                       if (new_locals != null)
-                                               locals.Or (new_locals);
+                               Report.Debug (2, "  MERGING CHILD DONE", this, result,
+                                             new_r, reachability);
 
-                                       if (new_params != null)
-                                               parameters.Or (new_params);
+                               IsDirty = true;
+
+                               return result;
+                       }
+
+                       protected void MergeFinally (FlowBranching branching, 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);
                                }
+                       }
 
-                               Report.Debug (2, "MERGING CHILD DONE", this);
+                       public void MergeFinally (FlowBranching branching, UsageVector f_vector,
+                                                 UsageVector f_origins)
+                       {
+                               if (parameters != null) {
+                                       if (f_vector != null) {
+                                               MergeFinally (branching, f_origins, f_vector.Parameters);
+                                               MyBitVector.Or (ref parameters, f_vector.ParameterVector);
+                                       } else
+                                               MergeFinally (branching, f_origins, parameters);
+                               }
 
-                               return Returns;
+                               if (f_vector != null && f_vector.LocalVector != null)
+                                       MyBitVector.Or (ref locals, f_vector.LocalVector);
                        }
 
                        // <summary>
@@ -467,53 +749,97 @@ namespace Mono.CSharp
                        //      8     Console.WriteLine (a);
                        //
                        // </summary>
-                       public void MergeJumpOrigins (ICollection origin_vectors)
+                       public void MergeJumpOrigins (UsageVector o_vectors)
                        {
-                               Report.Debug (1, "MERGING JUMP ORIGIN", this);
+                               Report.Debug (1, "  MERGING JUMP ORIGINS", this);
 
-                               RealBreaks = FlowReturns.Never;
-                               RealReturns = FlowReturns.Never;
-                               if (Reachable != FlowReturns.Always)
-                                       RealReachable = FlowReturns.Always;
+                               reachability = Reachability.Never ();
 
-                               if (origin_vectors == null)
+                               if (o_vectors == null) {
+                                       reachability.SetBarrier ();
                                        return;
+                               }
 
-                               foreach (UsageVector vector in origin_vectors) {
-                                       Report.Debug (1, "  MERGING JUMP ORIGIN", vector);
+                               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.And (ref reachability, vector.Reachability, true);
 
-                                       locals.And (vector.locals);
-                                       if (parameters != null)
-                                               parameters.And (vector.parameters);
-                                       RealBreaks = AndFlowReturns (RealBreaks, vector.Breaks);
-                                       RealReturns = AndFlowReturns (RealReturns, vector.Returns);
-                                       RealReachable = AndFlowReturns (RealReachable, vector.Reachable);
+                                       Report.Debug (1, "  MERGING JUMP ORIGIN #1", vector);
                                }
 
-                               Report.Debug (1, "MERGING JUMP ORIGIN DONE", this);
+                               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 (ICollection finally_vectors)
+                       public void MergeFinallyOrigins (UsageVector f_origins)
                        {
-                               Report.Debug (1, "MERGING FINALLY ORIGIN", this);
+                               Report.Debug (1, "  MERGING FINALLY ORIGIN", this);
 
-                               RealBreaks = FlowReturns.Never;
+                               reachability = Reachability.Never ();
 
-                               foreach (UsageVector vector in finally_vectors) {
-                                       Report.Debug (1, "  MERGING FINALLY ORIGIN", vector);
+                               for (UsageVector vector = f_origins; vector != null; vector = vector.Next) {
+                                       Report.Debug (1, "    MERGING FINALLY ORIGIN", vector);
 
                                        if (parameters != null)
                                                parameters.And (vector.parameters);
-                                       RealBreaks = AndFlowReturns (Breaks, vector.Breaks);
+
+                                       Reachability.And (ref reachability, vector.Reachability, true);
                                }
 
-                               is_finally = true;
+                               Report.Debug (1, "  MERGING FINALLY ORIGIN DONE", this);
+                       }
+
+                       public void MergeBreakOrigins (FlowBranching branching, UsageVector o_vectors)
+                       {
+                               Report.Debug (1, "  MERGING BREAK ORIGINS", this);
 
-                               Report.Debug (1, "MERGING FINALLY ORIGIN DONE", 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);
+
+                                       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);
+                                       }
+                               }
+
+                               Report.Debug (1, "  MERGING BREAK ORIGINS DONE", this);
                        }
 
                        public void CheckOutParameters (FlowBranching branching)
@@ -527,6 +853,7 @@ namespace Mono.CSharp
                        // </summary>
                        public void Or (UsageVector new_vector)
                        {
+                               IsDirty = true;
                                locals.Or (new_vector.locals);
                                if (parameters != null)
                                        parameters.Or (new_vector.parameters);
@@ -537,6 +864,7 @@ namespace Mono.CSharp
                        // </summary>
                        public void AndLocals (UsageVector new_vector)
                        {
+                               IsDirty = true;
                                locals.And (new_vector.locals);
                        }
 
@@ -569,7 +897,10 @@ namespace Mono.CSharp
                        // </summary>
                        public MyBitVector Locals {
                                get {
-                                       return locals.Clone ();
+                                       if (locals != null)
+                                               return locals.Clone ();
+                                       else
+                                               return null;
                                }
                        }
 
@@ -594,13 +925,13 @@ namespace Mono.CSharp
                                StringBuilder sb = new StringBuilder ();
 
                                sb.Append ("Vector (");
-                               sb.Append (id);
+                               sb.Append (Type);
                                sb.Append (",");
-                               sb.Append (Returns);
+                               sb.Append (id);
                                sb.Append (",");
-                               sb.Append (Breaks);
+                               sb.Append (IsDirty);
                                sb.Append (",");
-                               sb.Append (Reachable);
+                               sb.Append (reachability);
                                if (parameters != null) {
                                        sb.Append (" - ");
                                        sb.Append (parameters);
@@ -634,11 +965,14 @@ namespace Mono.CSharp
                                local_map = Block.LocalMap;
 
                                UsageVector parent_vector = parent != null ? parent.CurrentUsageVector : null;
-                               vector = new UsageVector (stype, parent_vector, loc, param_map.Length, local_map.Length);
+                               vector = new UsageVector (
+                                       stype, parent_vector, Block, loc,
+                                       param_map.Length, local_map.Length);
                        } else {
                                param_map = Parent.param_map;
                                local_map = Parent.local_map;
-                               vector = new UsageVector (stype, Parent.CurrentUsageVector, loc);
+                               vector = new UsageVector (
+                                       stype, Parent.CurrentUsageVector, null, loc);
                        }
 
                        AddSibling (vector);
@@ -651,20 +985,34 @@ namespace Mono.CSharp
                // <summary>
                //   Creates a sibling of the current usage vector.
                // </summary>
-               public virtual void CreateSibling (SiblingType type)
+               public virtual void CreateSibling (Block block, SiblingType type)
                {
-                       AddSibling (new UsageVector (type, Parent.CurrentUsageVector, Location));
+                       UsageVector vector = new UsageVector (
+                               type, Parent.CurrentUsageVector, block, Location);
+                       AddSibling (vector);
+
+                       Report.Debug (1, "  CREATED SIBLING", CurrentUsageVector);
+               }
 
-                       Report.Debug (1, "CREATED SIBLING", CurrentUsageVector);
+               public void CreateSibling ()
+               {
+                       CreateSibling (null, SiblingType.Conditional);
                }
 
                protected abstract void AddSibling (UsageVector uv);
 
-               public abstract void Break ();
-               public abstract void Return ();
-               public abstract void Goto ();
-               public abstract void Throw ();
-               public abstract void Label (ArrayList origin_vectors);
+               public virtual LabeledStatement LookupLabel (string name, Location loc)
+               {
+                       if (Parent != null)
+                               return Parent.LookupLabel (name, loc);
+
+                       Report.Error (
+                               159, loc,
+                               "No such label `" + name + "' in this scope");
+                       return null;
+               }
+
+               public abstract void Label (UsageVector origin_vectors);
 
                // <summary>
                //   Check whether all `out' parameters have been assigned.
@@ -681,60 +1029,33 @@ namespace Mono.CSharp
                                        continue;
 
                                Report.Error (177, loc, "The out parameter `" +
-                                             param_map.VariableNames [i] + "' must be " +
-                                             "assigned before control leave the current method.");
+                                             var.Name + "' must be " +
+                                             "assigned before control leaves the current method.");
                        }
                }
 
-               protected class MergeResult
+               protected UsageVector Merge (UsageVector sibling_list)
                {
-                       public MyBitVector Parameters;
-                       public MyBitVector Locals;
-                       public FlowReturns Returns;
-                       public FlowReturns Breaks;
-                       public FlowReturns Reachable;
-                       public bool MayLeaveLoop;
+                       if (sibling_list.Next == null)
+                               return sibling_list;
 
-                       public MergeResult (MyBitVector parameters, MyBitVector locals, FlowReturns returns, FlowReturns breaks,
-                                           FlowReturns reachable, bool may_leave_loop)
-                       {
-                               this.Parameters = parameters;
-                               this.Locals = locals;
-                               this.Returns = returns;
-                               this.Breaks = breaks;
-                               this.Reachable = reachable;
-                               this.MayLeaveLoop = may_leave_loop;
-                       }
-               }
-
-               protected MergeResult Merge (ArrayList children)
-               {
                        MyBitVector locals = null;
                        MyBitVector parameters = null;
 
-                       FlowReturns returns = FlowReturns.Undefined;
-                       FlowReturns breaks = FlowReturns.Undefined;
-                       FlowReturns reachable = FlowReturns.Undefined;
+                       Reachability reachability = null;
 
-                       Report.Debug (2, "MERGING CHILDREN", this, Type, children.Count);
+                       Report.Debug (2, "  MERGING SIBLINGS", this, Name);
 
-                       int children_count = children.Count;
-                       for (int ix = 0; ix < children_count; ix++){
-                               UsageVector child = (UsageVector) children [ix];
-                               
-                               Report.Debug (2, "  MERGING CHILD", child, child.AlwaysBreaks, child.AlwaysReturns,
-                                             child.IsUnreachable, child.Locals, child.Parameters,
-                                             child.Returns, child.Breaks, child.Reachable);
+                       for (UsageVector child = sibling_list; child != null; child = child.Next) {
+                               bool do_break = (Type != BranchingType.Switch) &&
+                                       (Type != BranchingType.Loop);
 
-                               reachable = AndFlowReturns (reachable, child.Reachable);
+                               Report.Debug (2, "    MERGING SIBLING   ", child,
+                                             child.ParameterVector, child.LocalVector,
+                                             reachability, child.Reachability, do_break);
 
-                               // Ignore unreachable children.
-                               if (child.IsUnreachable)
-                                       continue;
+                               Reachability.And (ref reachability, child.Reachability, do_break);
 
-                               returns = AndFlowReturns (returns, child.Returns);
-                               breaks = AndFlowReturns (breaks, child.Breaks);
-                                       
                                // A local variable is initialized after a flow branching if it
                                // has been initialized in all its branches which do neither
                                // always return or always throw an exception.
@@ -770,90 +1091,122 @@ namespace Mono.CSharp
                                // Here, `a' is initialized in line 3 and we must not look at
                                // line 5 since it always returns.
                                // 
-                               if (!child.AlwaysReturns && !child.AlwaysBreaks)
+                               bool do_break_2 = (child.Type != SiblingType.Block) &&
+                                       (child.Type != SiblingType.SwitchSection);
+                               bool always_throws = (child.Type != SiblingType.Try) &&
+                                       child.Reachability.AlwaysThrows;
+                               bool unreachable = always_throws ||
+                                       (do_break_2 && child.Reachability.AlwaysBreaks) ||
+                                       child.Reachability.AlwaysReturns ||
+                                       child.Reachability.AlwaysHasBarrier;
+
+                               Report.Debug (2, "    MERGING SIBLING #1", reachability,
+                                             Type, child.Type, child.Reachability.IsUnreachable,
+                                             do_break_2, always_throws, unreachable);
+
+                               if (!unreachable && (child.LocalVector != null))
                                        MyBitVector.And (ref locals, child.LocalVector);
 
                                // An `out' parameter must be assigned in all branches which do
                                // not always throw an exception.
-                               if ((child.Type != SiblingType.Catch) &&
-                                   (child.ParameterVector != null) && (child.Breaks != FlowReturns.Exception))
+                               if ((child.ParameterVector != null) && !child.Reachability.AlwaysThrows)
                                        MyBitVector.And (ref parameters, child.ParameterVector);
-                       }
-
-                       Report.Debug (2, "MERGING CHILDREN DONE", Type, parameters, locals, returns, breaks, reachable,
-                                     Infinite, MayLeaveLoop, this);
-
-                       if (Infinite && !MayLeaveLoop) {
-                               Report.Debug (1, "INFINITE", returns, breaks, this);
 
-                               if (returns == FlowReturns.Never) {
-                                       // We're actually infinite.
-                                       breaks = FlowReturns.Unreachable;
-                                       returns = FlowReturns.Unreachable;
-                               } else if ((returns == FlowReturns.Sometimes) || (returns == FlowReturns.Always)) {
-                                       // 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).
-                                       returns = FlowReturns.Always;
-                               }
+                               Report.Debug (2, "    MERGING SIBLING #2", parameters, locals);
                        }
 
-                       if (returns == FlowReturns.Undefined)
-                               returns = FlowReturns.Never;
-                       if (breaks == FlowReturns.Undefined)
-                               breaks = FlowReturns.Never;
+                       if (reachability == null)
+                               reachability = Reachability.Never ();
+
+                       Report.Debug (2, "  MERGING SIBLINGS DONE", parameters, locals,
+                                     reachability, Infinite);
 
-                       return new MergeResult (parameters, locals, returns, breaks, reachable, MayLeaveLoop);
+                       return new UsageVector (
+                               parameters, locals, reachability, null, Location);
                }
 
-               protected abstract MergeResult Merge ();
+               protected abstract UsageVector Merge ();
 
                // <summary>
                //   Merge a child branching.
                // </summary>
-               public FlowReturns MergeChild (FlowBranching child)
+               public UsageVector MergeChild (FlowBranching child)
                {
-                       MergeResult result = child.Merge ();
-
-                       CurrentUsageVector.MergeChild (
-                               result.Parameters, result.Locals, result.Returns, result.Breaks, result.Reachable);
-
-                       if ((child.Type != BranchingType.LoopBlock) && (child.Type != BranchingType.SwitchSection))
-                               MayLeaveLoop |= child.MayLeaveLoop;
-
-                       if (result.Reachable == FlowReturns.Exception)
-                               return FlowReturns.Exception;
-                       else
-                               return result.Returns;
+                       return CurrentUsageVector.MergeChild (child);
                }
 
                // <summary>
                //   Does the toplevel merging.
                // </summary>
-               public FlowReturns MergeTopBlock ()
+               public Reachability MergeTopBlock ()
                {
                        if ((Type != BranchingType.Block) || (Block == null))
                                throw new NotSupportedException ();
 
                        UsageVector vector = new UsageVector (
-                               SiblingType.Conditional, null, Location, param_map.Length, local_map.Length);
+                               SiblingType.Block, null, Block, Location,
+                               param_map.Length, local_map.Length);
+
+                       UsageVector result = vector.MergeChild (this);
 
-                       MergeResult result = Merge ();
-                       vector.MergeChild (result.Parameters, result.Locals, result.Returns, result.Breaks, result.Reachable);
+                       Report.Debug (4, "MERGE TOP BLOCK", Location, vector, result.Reachability);
 
-                       if (vector.Reachable != FlowReturns.Exception)
+                       if ((vector.Reachability.Throws != FlowReturns.Always) &&
+                           (vector.Reachability.Barrier != FlowReturns.Always))
                                CheckOutParameters (vector.Parameters, Location);
-                       else
-                               return FlowReturns.Exception;
 
-                       return result.Returns;
+                       return result.Reachability;
                }
 
-               public virtual bool InTryBlock ()
+               //
+               // Checks whether we're in a `try' block.
+               //
+               public virtual bool InTryOrCatch (bool is_return)
+               {
+                       if ((Block != null) && Block.IsDestructor)
+                               return true;
+                       else if (!is_return &&
+                           ((Type == BranchingType.Loop) || (Type == BranchingType.Switch)))
+                               return false;
+                       else if (Parent != null)
+                               return Parent.InTryOrCatch (is_return);
+                       else
+                               return false;
+               }
+
+               public virtual bool InTryWithCatch ()
                {
                        if (Parent != null)
-                               return Parent.InTryBlock ();
+                               return Parent.InTryWithCatch ();
+                       return false;
+               }
+
+               public virtual bool InLoop ()
+               {
+                       if (Type == BranchingType.Loop)
+                               return true;
+                       else if (Parent != null)
+                               return Parent.InLoop ();
+                       else
+                               return false;
+               }
+
+               public virtual bool InSwitch ()
+               {
+                       if (Type == BranchingType.Switch)
+                               return true;
+                       else if (Parent != null)
+                               return Parent.InSwitch ();
+                       else
+                               return false;
+               }
+
+               public virtual bool BreakCrossesTryCatchBoundary ()
+               {
+                       if ((Type == BranchingType.Loop) || (Type == BranchingType.Switch))
+                               return false;
+                       else if (Parent != null)
+                               return Parent.BreakCrossesTryCatchBoundary ();
                        else
                                return false;
                }
@@ -862,10 +1215,24 @@ namespace Mono.CSharp
                {
                        if (Parent != null)
                                Parent.AddFinallyVector (vector);
-                       else
+                       else if ((Block == null) || !Block.IsDestructor)
                                throw new NotSupportedException ();
                }
 
+               public virtual void AddBreakVector (UsageVector vector)
+               {
+                       if (Parent != null)
+                               Parent.AddBreakVector (vector);
+                       else if ((Block == null) || !Block.IsDestructor)
+                               throw new NotSupportedException ();
+               }
+
+               public virtual void StealFinallyClauses (ref ArrayList list)
+               {
+                       if (Parent != null)
+                               Parent.StealFinallyClauses (ref list);
+               }
+
                public bool IsAssigned (VariableInfo vi)
                {
                        return CurrentUsageVector.IsAssigned (vi);
@@ -911,94 +1278,116 @@ namespace Mono.CSharp
                        sb.Append (")");
                        return sb.ToString ();
                }
+
+               public string Name {
+                       get {
+                               return String.Format ("{0} ({1}:{2}:{3})",
+                                                     GetType (), id, Type, Location);
+                       }
+               }
        }
 
        public class FlowBranchingBlock : FlowBranching
        {
-               UsageVector current_vector;
-               ArrayList siblings = new ArrayList ();
+               UsageVector sibling_list = null;
 
-               public FlowBranchingBlock (FlowBranching parent, BranchingType type, SiblingType stype,
-                                          Block block, Location loc)
+               public FlowBranchingBlock (FlowBranching parent, BranchingType type,
+                                          SiblingType stype, Block block, Location loc)
                        : base (parent, type, stype, block, loc)
                { }
 
                public override UsageVector CurrentUsageVector {
-                       get { return current_vector; }
+                       get { return sibling_list; }
                }
 
                protected override void AddSibling (UsageVector sibling)
                {
-                       siblings.Add (sibling);
-                       current_vector = sibling;
+                       sibling.Next = sibling_list;
+                       sibling_list = sibling;
                }
 
-               public override void Break ()
+               public override LabeledStatement LookupLabel (string name, Location loc)
                {
-                       if (Type == BranchingType.SwitchSection)
-                               CurrentUsageVector.NeverReachable ();
-                       else {
-                               if (Type == BranchingType.LoopBlock)
-                                       MayLeaveLoop = true;
-                               CurrentUsageVector.Break ();
-                       }
-               }
+                       if (Block == null)
+                               return base.LookupLabel (name, loc);
 
-               public override void Return ()
-               {
-                       CurrentUsageVector.Return ();
+                       LabeledStatement s = Block.LookupLabel (name);
+                       if (s != null)
+                               return s;
+
+                       return base.LookupLabel (name, loc);
                }
 
-               public override void Goto ()
+               public override void Label (UsageVector origin_vectors)
                {
-                       CurrentUsageVector.Unreachable ();
+                       if (!CurrentUsageVector.Reachability.IsUnreachable) {
+                               UsageVector vector = CurrentUsageVector.Clone ();
+                               vector.Next = origin_vectors;
+                               origin_vectors = vector;
+                       }
+
+                       CurrentUsageVector.MergeJumpOrigins (origin_vectors);
                }
 
-               public override void Throw ()
+               protected override UsageVector Merge ()
                {
-                       CurrentUsageVector.Throw ();
+                       return Merge (sibling_list);
                }
+       }
+
+       public class FlowBranchingLoop : FlowBranchingBlock
+       {
+               UsageVector break_origins;
+
+               public FlowBranchingLoop (FlowBranching parent, Block block, Location loc)
+                       : base (parent, BranchingType.Loop, SiblingType.Conditional, block, loc)
+               { }
 
-               public override void Label (ArrayList origin_vectors)
+               public override void AddBreakVector (UsageVector vector)
                {
-                       CurrentUsageVector.MergeJumpOrigins (origin_vectors);
+                       vector = vector.Clone ();
+                       vector.Next = break_origins;
+                       break_origins = vector;
                }
 
-               protected override MergeResult Merge ()
+               protected override UsageVector Merge ()
                {
-                       MergeResult result = Merge (siblings);
-                       if (Type == BranchingType.LoopBlock)
-                               result.MayLeaveLoop = false;
-                       return result;
+                       UsageVector vector = base.Merge ();
+
+                       vector.MergeBreakOrigins (this, break_origins);
+
+                       return vector;
                }
        }
 
        public class FlowBranchingException : FlowBranching
        {
-               ArrayList finally_vectors;
-
-               bool has_params;
+               ExceptionStatement stmt;
                UsageVector current_vector;
-               UsageVector try_vector;
-               ArrayList catch_vectors = new ArrayList ();
+               UsageVector catch_vectors;
                UsageVector finally_vector;
+               UsageVector finally_origins;
+               bool emit_finally;
 
-               public FlowBranchingException (FlowBranching parent, BranchingType type, Block block, Location loc)
-                       : base (parent, type, SiblingType.Try, block, loc)
+               public FlowBranchingException (FlowBranching parent,
+                                              ExceptionStatement stmt)
+                       : base (parent, BranchingType.Exception, SiblingType.Try,
+                               null, stmt.loc)
                {
-                       finally_vectors = new ArrayList ();
-                       has_params = current_vector.HasParameters;
+                       this.stmt = stmt;
+                       this.emit_finally = true;
                }
 
                protected override void AddSibling (UsageVector sibling)
                {
                        if (sibling.Type == SiblingType.Try) {
-                               try_vector = sibling;
-                               catch_vectors.Add (sibling);
-                       } else if (sibling.Type == SiblingType.Catch)
-                               catch_vectors.Add (sibling);
-                       else if (sibling.Type == SiblingType.Finally) {
-                               // sibling.MergeFinallyOrigins (finally_vectors);
+                               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);
                                finally_vector = sibling;
                        } else
                                throw new InvalidOperationException ();
@@ -1010,67 +1399,81 @@ namespace Mono.CSharp
                        get { return current_vector; }
                }
 
-               public override bool InTryBlock ()
+               public override bool InTryOrCatch (bool is_return)
                {
-                       return true;
+                       return finally_vector == null;
                }
 
-               public override void AddFinallyVector (UsageVector vector)
+               public override bool InTryWithCatch ()
                {
-                       finally_vectors.Add (vector.Clone ());
-               }
+                       if (finally_vector == null) {
+                               Try t = stmt as Try;
+                               if (t != null && t.HasCatch)
+                                       return true;
+                       }
 
-               public override void Break ()
-               {
-                       CurrentUsageVector.Break ();
+                       if (Parent != null)
+                               return Parent.InTryWithCatch ();
+
+                       return false;
                }
 
-               public override void Return ()
+               public override bool BreakCrossesTryCatchBoundary ()
                {
-                       CurrentUsageVector.Return ();
+                       return true;
                }
 
-               public override void Goto ()
+               public override void AddFinallyVector (UsageVector vector)
                {
-                       CurrentUsageVector.Unreachable ();
+                       vector = vector.Clone ();
+                       vector.Next = finally_origins;
+                       finally_origins = vector;
                }
 
-               public override void Throw ()
+               public override void StealFinallyClauses (ref ArrayList list)
                {
-                       CurrentUsageVector.Throw ();
+                       if (list == null)
+                               list = new ArrayList ();
+                       list.Add (stmt);
+                       emit_finally = false;
+                       base.StealFinallyClauses (ref list);
                }
 
-               public override void Label (ArrayList origin_vectors)
-               {
-                       CurrentUsageVector.MergeJumpOrigins (origin_vectors);
+               public bool EmitFinally {
+                       get { return emit_finally; }
                }
 
-               protected void MergeFinally (MyBitVector f_params, ref MergeResult result)
+               public override LabeledStatement LookupLabel (string name, Location loc)
                {
-                       foreach (UsageVector vector in finally_vectors) {
-                               MyBitVector temp_params = f_params.Clone ();
-                               temp_params.Or (vector.Parameters);
+                       if (current_vector.Block == null)
+                               return base.LookupLabel (name, loc);
+
+                       LabeledStatement s = current_vector.Block.LookupLabel (name);
+                       if (s != null)
+                               return s;
 
-                               CheckOutParameters (temp_params, Location);
+                       if (finally_vector != null) {
+                               Report.Error (
+                                       157, loc, "Control can not leave the body " +
+                                       "of the finally block");
+                               return null;
                        }
+
+                       return base.LookupLabel (name, loc);
                }
 
-               protected override MergeResult Merge ()
+               public override void Label (UsageVector origin_vectors)
                {
-                       MergeResult result = Merge (catch_vectors);
+                       CurrentUsageVector.MergeJumpOrigins (origin_vectors);
+               }
 
-                       if (has_params) {
-                               if (finally_vector != null) {
-                                       MergeFinally (finally_vector.Parameters, ref result);
-                                       MyBitVector.Or (ref result.Parameters, finally_vector.ParameterVector);
-                               } else
-                                       MergeFinally (result.Parameters, ref result);
-                       }
+               protected override UsageVector Merge ()
+               {
+                       UsageVector vector = Merge (catch_vectors);
 
-                       if (finally_vector != null)
-                               MyBitVector.Or (ref result.Locals, finally_vector.LocalVector);
+                       vector.MergeFinally (this, finally_vector, finally_origins);
 
-                       return result;
+                       return vector;
                }
        }
 
@@ -1263,13 +1666,15 @@ namespace Mono.CSharp
                                if (type is TypeBuilder) {
                                        TypeContainer tc = TypeManager.LookupTypeContainer (type);
 
-                                       ArrayList fields = tc.Fields;
+                                       ArrayList fields = null;
+                                       if (tc != null)
+                                               fields = tc.Fields;
 
                                        ArrayList public_fields = new ArrayList ();
                                        ArrayList non_public_fields = new ArrayList ();
 
                                        if (fields != null) {
-                                               foreach (Field field in fields) {
+                                               foreach (FieldMember field in fields) {
                                                        if ((field.ModFlags & Modifiers.STATIC) != 0)
                                                                continue;
                                                        if ((field.ModFlags & Modifiers.PUBLIC) != 0)
@@ -1598,22 +2003,16 @@ namespace Mono.CSharp
                // <summary>
                public readonly int Length;
 
-               // <summary>
-               //   Type and name of all the variables.
-               //   Note that this is null for variables for which we do not need to compute
-               //   assignment info.
-               // </summary>
-               public readonly Type[] VariableTypes;
-               public readonly string[] VariableNames;
-
                VariableInfo[] map;
 
                public VariableMap (InternalParameters ip)
                {
                        Count = ip != null ? ip.Count : 0;
-                       map = new VariableInfo [Count];
-                       VariableNames = new string [Count];
-                       VariableTypes = new Type [Count];
+                       
+                       // Dont bother allocating anything!
+                       if (Count == 0)
+                               return;
+                       
                        Length = 0;
 
                        for (int i = 0; i < Count; i++) {
@@ -1621,11 +2020,14 @@ namespace Mono.CSharp
 
                                if ((mod & Parameter.Modifier.OUT) == 0)
                                        continue;
+                               
+                               // Dont allocate till we find an out var.
+                               if (map == null)
+                                       map = new VariableInfo [Count];
 
-                               VariableNames [i] = ip.ParameterName (i);
-                               VariableTypes [i] = TypeManager.GetElementType (ip.ParameterType (i));
-
-                               map [i] = new VariableInfo (VariableNames [i], VariableTypes [i], i, Length);
+                               map [i] = new VariableInfo (ip.ParameterName (i),
+                                       TypeManager.GetElementType (ip.ParameterType (i)), i, Length);
+                               
                                Length += map [i].Length;
                        }
                }
@@ -1637,21 +2039,21 @@ namespace Mono.CSharp
                public VariableMap (VariableMap parent, LocalInfo[] locals)
                {
                        int offset = 0, start = 0;
-                       if (parent != null) {
+                       if (parent != null && parent.map != null) {
                                offset = parent.Length;
                                start = parent.Count;
                        }
 
                        Count = locals.Length + start;
+                       
+                       if (Count == 0)
+                               return;
+                       
                        map = new VariableInfo [Count];
-                       VariableNames = new string [Count];
-                       VariableTypes = new Type [Count];
                        Length = offset;
 
-                       if (parent != null) {
+                       if (parent != null && parent.map != null) {
                                parent.map.CopyTo (map, 0);
-                               parent.VariableNames.CopyTo (VariableNames, 0);
-                               parent.VariableTypes.CopyTo (VariableTypes, 0);
                        }
 
                        for (int i = start; i < Count; i++) {
@@ -1660,9 +2062,6 @@ namespace Mono.CSharp
                                if (li.VariableType == null)
                                        continue;
 
-                               VariableNames [i] = li.Name;
-                               VariableTypes [i] = li.VariableType;
-
                                map [i] = li.VariableInfo = new VariableInfo (li, Length);
                                Length += map [i].Length;
                        }
@@ -1674,6 +2073,9 @@ namespace Mono.CSharp
                // </summary>
                public VariableInfo this [int index] {
                        get {
+                               if (map == null)
+                                       return null;
+                               
                                return map [index];
                        }
                }
@@ -1797,7 +2199,12 @@ namespace Mono.CSharp
                // </summary>
                public void And (MyBitVector new_vector)
                {
-                       BitArray new_array = new_vector.Vector;
+                       BitArray new_array;
+
+                       if (new_vector != null)
+                               new_array = new_vector.Vector;
+                       else
+                               new_array = new BitArray (Count, false);
 
                        initialize_vector ();
 
@@ -1868,7 +2275,7 @@ namespace Mono.CSharp
                {
                        if (vector != null)
                                return;
-
+                       
                        vector = new BitArray (Count, false);
                        if (InheritsFrom != null)
                                Vector = InheritsFrom.Vector;
@@ -1878,20 +2285,16 @@ namespace Mono.CSharp
 
                public override string ToString ()
                {
-                       StringBuilder sb = new StringBuilder ("MyBitVector (");
+                       StringBuilder sb = new StringBuilder ("{");
 
                        BitArray vector = Vector;
-                       sb.Append (Count);
-                       sb.Append (",");
                        if (!IsDirty)
-                               sb.Append ("INHERITED - ");
+                               sb.Append ("=");
                        for (int i = 0; i < vector.Count; i++) {
-                               if (i > 0)
-                                       sb.Append (",");
-                               sb.Append (vector [i]);
+                               sb.Append (vector [i] ? "1" : "0");
                        }
                        
-                       sb.Append (")");
+                       sb.Append ("}");
                        return sb.ToString ();
                }
        }