Remove special handling of 'break'
[mono.git] / mcs / gmcs / flowanalysis.cs
index 9d8ca8dc9c30367c4106e079fa41263ca0ecb1fc..2d501b5a398e82ffcbc103ee92a212a3148a70d4 100644 (file)
@@ -16,6 +16,13 @@ using System.Diagnostics;
 
 namespace Mono.CSharp
 {
+       public enum TriState : byte {
+               // Never < Sometimes < Always
+               Never,
+               Sometimes,
+               Always
+       }
+
        // <summary>
        //   A new instance of this class is created every time a new block is resolved
        //   and if there's branching in the block's control flow.
@@ -25,12 +32,18 @@ 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,
+
+                       // The statement embedded inside a loop
+                       Embedded,
 
                        // Try/Catch block.
                        Exception,
@@ -45,7 +58,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,
@@ -53,41 +67,153 @@ namespace Mono.CSharp
                        Finally
                }
 
-               // <summary>
-               //   This is used in the control flow analysis code to specify whether the
-               //   current code block may return to its enclosing block before reaching
-               //   its end.
-               // </summary>
-               public enum FlowReturns {
-                       Undefined = 0,
+               public sealed class Reachability
+               {
+                       TriState returns, throws, barrier;
+
+                       public TriState Returns {
+                               get { return returns; }
+                       }
+                       public TriState Throws {
+                               get { return throws; }
+                       }
+                       public TriState Barrier {
+                               get { return barrier; }
+                       }
 
-                       // It can never return.
-                       Never,
+                       Reachability (TriState returns, TriState throws, TriState barrier)
+                       {
+                               this.returns = returns;
+                               this.throws = throws;
+                               this.barrier = barrier;
+                       }
 
-                       // This means that the block contains a conditional return statement
-                       // somewhere.
-                       Sometimes,
+                       public Reachability Clone ()
+                       {
+                               return new Reachability (returns, throws, barrier);
+                       }
 
-                       // The code always returns, ie. there's an unconditional return / break
-                       // statement in it.
-                       Always,
+                       public static TriState TriState_Meet (TriState a, TriState b)
+                       {
+                               // (1) if both are Never, return Never
+                               // (2) if both are Always, return Always
+                               // (3) otherwise, return Sometimes
+                               // note that (3) => (3') if both are Sometimes, return Sometimes
+                               return a == b ? a : TriState.Sometimes;
+                       }
 
-                       // The code always throws an exception.
-                       Exception,
+                       public static TriState TriState_Max (TriState a, TriState b)
+                       {
+                               return ((byte) a > (byte) b) ? a : b;
+                       }
+
+                       public void Meet (Reachability b)
+                       {
+                               if ((AlwaysReturns && b.AlwaysHasBarrier) || (AlwaysHasBarrier && b.AlwaysReturns))
+                                       returns = TriState.Always;
+                               else
+                                       returns = TriState_Meet (returns, b.returns);
+
+                               throws = TriState_Meet (throws, b.throws);
+                               barrier = TriState_Meet (barrier, b.barrier);
+                       }
+
+                       public void Or (Reachability b)
+                       {
+                               returns = TriState_Max (returns, b.returns);
+                               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 Unreachable {
+                               get { return TriState_Max (returns, TriState_Max (throws, barrier)); }
+                       }
+
+                       TriState Reachable {
+                               get {
+                                       TriState unreachable = Unreachable;
+                                       if (unreachable == TriState.Sometimes)
+                                               return TriState.Sometimes;
+                                       return unreachable == TriState.Always ? TriState.Never : TriState.Always;
+                               }
+                       }
+
+                       public bool AlwaysReturns {
+                               get { return returns == TriState.Always; }
+                       }
+
+                       public bool AlwaysThrows {
+                               get { return throws == TriState.Always; }
+                       }
 
-                       // The current code block is unreachable.  This happens if it's immediately
-                       // following a FlowReturns.Always block.
-                       Unreachable
+                       public bool AlwaysHasBarrier {
+                               get { return barrier == TriState.Always; }
+                       }
+
+                       public bool IsUnreachable {
+                               get { return Unreachable == TriState.Always; }
+                       }
+
+                       public void SetReturns ()
+                       {
+                               returns = TriState.Always;
+                       }
+
+                       public void SetThrows ()
+                       {
+                               throws = TriState.Always;
+                       }
+
+                       public void SetBarrier ()
+                       {
+                               barrier = TriState.Always;
+                       }
+
+                       static string ShortName (TriState returns)
+                       {
+                               switch (returns) {
+                               case TriState.Never:
+                                       return "N";
+                               case TriState.Sometimes:
+                                       return "S";
+                               default:
+                                       return "A";
+                               }
+                       }
+
+                       public override string ToString ()
+                       {
+                               return String.Format ("[{0}:{1}:{2}:{3}]",
+                                                     ShortName (returns), 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);
+                               return new FlowBranchingBreakable (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 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);
@@ -115,16 +241,6 @@ namespace Mono.CSharp
                // </summary>
                public readonly Location Location;
 
-               // <summary>
-               //   If this is an infinite loop.
-               // </summary>
-               public bool Infinite;
-
-               // <summary>
-               //   If we may leave the current loop.
-               // </summary>
-               public bool MayLeaveLoop;
-
                //
                // Private
                //
@@ -133,46 +249,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.
@@ -186,7 +262,12 @@ 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.
+                       // </summary>
+                       public readonly Block Block;
 
                        // <summary>
                        //   If this is true, then the usage vector has been modified and must be
@@ -211,12 +292,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 +309,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.Always ();
                                }
 
                                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)
+                       public bool IsAssigned (VariableInfo var, bool ignoreReachability)
                        {
-                               if (!var.IsParameter && AlwaysBreaks)
+                               if (!ignoreReachability && !var.IsParameter && Reachability.IsUnreachable)
                                        return true;
 
                                return var.IsAssigned (var.IsParameter ? parameters : locals);
@@ -281,15 +392,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 +409,50 @@ 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 {
-                               get {
-                                       return (Breaks == FlowReturns.Always) ||
-                                               (Breaks == FlowReturns.Exception) ||
-                                               (Breaks == FlowReturns.Unreachable);
-                               }
-                       }
-
-                       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 Reachability Reachability {
+                               get { return reachability; }
                        }
 
                        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 Throw ()
                        {
-                               // If we're already unreachable, don't modify the reason why.
-                               if (!IsUnreachable)
-                                       RealReachable = FlowReturns.Unreachable;
+                               if (!reachability.IsUnreachable) {
+                                       IsDirty = true;
+                                       reachability.SetThrows ();
+                                       reachability.SetBarrier ();
+                               }
                        }
 
-                       public void NeverReachable ()
+                       public void Goto ()
                        {
-                               // If we're already unreachable, don't modify the reason why.
-                               if (!IsUnreachable)
-                                       RealReachable = FlowReturns.Never;
-                       }
-
-                       public void Throw ()
-                       {
-                               // 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 (UsageVector child, bool implicit_block)
                        {
-                               Report.Debug (2, "MERGING CHILD", this, new_params, new_locals, new_returns, new_breaks,
-                                             new_reachable);
+                               Report.Debug (2, "    MERGING CHILD EFFECTS", this, child, IsDirty, reachability, Type);
 
-                               RealReturns = new_returns;
-                               RealBreaks = new_breaks;
-                               RealReachable = new_reachable;
+                               Reachability new_r = child.Reachability;
 
                                //
                                // We've now either reached the point after the branching or we will
@@ -424,27 +463,51 @@ 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 child;
+                               }
+
+                               if (locals != null && child.LocalVector != null)
+                                       locals.Or (child.LocalVector);
 
-                               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 (child.ParameterVector != null)
+                                       parameters.Or (child.ParameterVector);
 
-                                       if (new_locals != null)
-                                               locals.Or (new_locals);
+                               if (implicit_block)
+                                       reachability = new_r.Clone ();
+                               else
+                                       reachability.Or (new_r);
 
-                                       if (new_params != null)
-                                               parameters.Or (new_params);
+                               IsDirty = true;
+
+                               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);
                                }
+                       }
 
-                               Report.Debug (2, "MERGING CHILD DONE", this);
+                       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);
+                               }
 
-                               return Returns;
+                               if (f_vector != null && f_vector.LocalVector != null)
+                                       MyBitVector.Or (ref locals, f_vector.LocalVector);
                        }
 
                        // <summary>
@@ -467,53 +530,95 @@ 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.Always ();
 
-                               if (origin_vectors == null)
+                               if (o_vectors == null) {
+                                       reachability.SetBarrier ();
                                        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);
+                                       }
 
-                               foreach (UsageVector vector in origin_vectors) {
-                                       Report.Debug (1, "  MERGING JUMP ORIGIN", vector);
+                                       reachability.Meet (vector.Reachability);
 
-                                       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.Always ();
 
-                               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.Meet (vector.Reachability);
                                }
 
-                               is_finally = true;
+                               Report.Debug (1, "  MERGING FINALLY ORIGIN DONE", this);
+                       }
+
+                       public void MergeOrigins (FlowBranching branching, UsageVector o_vectors)
+                       {
+                               Report.Debug (1, "  MERGING BREAK ORIGINS", this);
+
+                               if (o_vectors == null)
+                                       return;
+
+                               bool first = reachability.IsUnreachable;
+
+                               for (UsageVector vector = o_vectors; vector != null; vector = vector.Next) {
+                                       Report.Debug (1, "    MERGING BREAK ORIGIN", vector, first);
 
-                               Report.Debug (1, "MERGING FINALLY ORIGIN DONE", this);
+                                       if (first) {
+                                               locals = vector.Locals;
+                                               parameters = vector.Parameters;
+                                               first = false;
+                                       } else {
+                                               if (locals != null && vector.locals != null)
+                                                       locals.And (vector.locals);
+                                               if (parameters != null && vector.parameters != null)
+                                                       parameters.And (vector.parameters);
+                                       }
+
+                                       reachability.Meet (vector.Reachability);
+                               }
+
+                               Report.Debug (1, "  MERGING BREAK ORIGINS DONE", this);
                        }
 
                        public void CheckOutParameters (FlowBranching branching)
@@ -527,6 +632,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,52 +643,38 @@ namespace Mono.CSharp
                        // </summary>
                        public void AndLocals (UsageVector new_vector)
                        {
+                               IsDirty = true;
                                locals.And (new_vector.locals);
                        }
 
                        public bool HasParameters {
-                               get {
-                                       return parameters != null;
-                               }
+                               get { return parameters != null; }
                        }
 
                        public bool HasLocals {
-                               get {
-                                       return locals != null;
-                               }
+                               get { return locals != null; }
                        }
 
                        // <summary>
                        //   Returns a deep copy of the parameters.
                        // </summary>
                        public MyBitVector Parameters {
-                               get {
-                                       if (parameters != null)
-                                               return parameters.Clone ();
-                                       else
-                                               return null;
-                               }
+                               get { return parameters == null ? null : parameters.Clone (); }
                        }
 
                        // <summary>
                        //   Returns a deep copy of the locals.
                        // </summary>
                        public MyBitVector Locals {
-                               get {
-                                       return locals.Clone ();
-                               }
+                               get { return locals == null ? null : locals.Clone (); }
                        }
 
                        public MyBitVector ParameterVector {
-                               get {
-                                       return parameters;
-                               }
+                               get { return parameters; }
                        }
 
                        public MyBitVector LocalVector {
-                               get {
-                                       return locals;
-                               }
+                               get { return locals; }
                        }
 
                        //
@@ -594,13 +686,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 +726,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 +746,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.
@@ -680,61 +789,31 @@ namespace Mono.CSharp
                                if (var.IsAssigned (parameters))
                                        continue;
 
-                               Report.Error (177, loc, "The out parameter `" +
-                                             param_map.VariableNames [i] + "' must be " +
-                                             "assigned before control leave the current method.");
+                               Report.Error (177, loc, "The out parameter `{0}' must be assigned to before control leaves the current method",
+                                       var.Name);
                        }
                }
 
-               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) {
+                               Report.Debug (2, "    MERGING SIBLING   ", reachability, child);
 
-                               reachable = AndFlowReturns (reachable, child.Reachable);
-
-                               // Ignore unreachable children.
-                               if (child.IsUnreachable)
-                                       continue;
+                               if (reachability == null)
+                                       reachability = child.Reachability.Clone ();
+                               else
+                                       reachability.Meet (child.Reachability);
 
-                               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,113 +849,119 @@ 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 unreachable = child.Reachability.IsUnreachable;
+
+                               Report.Debug (2, "    MERGING SIBLING #1", reachability,
+                                             Type, child.Type, child.Reachability.IsUnreachable, 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)
+                               throw new InternalErrorException ("Cannot happen: the loop above runs at least twice");
+
+                       Report.Debug (2, "  MERGING SIBLINGS DONE", parameters, locals, reachability);
 
-                       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;
+                       bool implicit_block = child.Type == BranchingType.Block && child.Block.Implicit;
+                       Report.Debug (2, "  MERGING CHILD", this, child);
+                       UsageVector result = CurrentUsageVector.MergeChild (child.Merge (), implicit_block);
+                       Report.Debug (2, "  MERGING CHILD DONE", this, result);
+                       return result;
                }
 
                // <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);
+                       UsageVector result = Merge ();
 
-                       MergeResult result = Merge ();
-                       vector.MergeChild (result.Parameters, result.Locals, result.Returns, result.Breaks, result.Reachable);
+                       Report.Debug (4, "MERGE TOP BLOCK", Location, result);
 
-                       if (vector.Reachable != FlowReturns.Exception)
-                               CheckOutParameters (vector.Parameters, Location);
-                       else
-                               return FlowReturns.Exception;
+                       if (!result.Reachability.AlwaysThrows && !result.Reachability.AlwaysHasBarrier)
+                               CheckOutParameters (result.Parameters, Location);
 
-                       return result.Returns;
+                       return result.Reachability;
                }
 
-               public virtual bool InTryBlock ()
+               //
+               // Checks whether we're in a `try' block.
+               //
+               public virtual bool InTryOrCatch ()
                {
-                       if (Parent != null)
-                               return Parent.InTryBlock ();
-                       else
-                               return false;
+                       if (Block != null && Block.IsDestructor)
+                               return true;
+                       return Parent != null && Parent.InTryOrCatch ();
+               }
+
+               public virtual bool InTryWithCatch ()
+               {
+                       return Parent != null && Parent.InTryWithCatch ();
                }
 
                public virtual void AddFinallyVector (UsageVector vector)
                {
                        if (Parent != null)
                                Parent.AddFinallyVector (vector);
-                       else
+                       else if ((Block == null) || !Block.IsDestructor)
                                throw new NotSupportedException ();
                }
 
+               // returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
+               public virtual bool AddBreakOrigin (UsageVector vector, Location loc)
+               {
+                       if (Parent != null)
+                               return Parent.AddBreakOrigin (vector, loc);
+
+                       Report.Error (139, loc, "No enclosing loop out of which to break or continue");
+                       return false;
+               }
+
+               // 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)
+                               return Parent.AddContinueOrigin (vector, loc);
+
+                       Report.Error (139, loc, "No enclosing loop out of which to break or continue");
+                       return false;
+               }
+
+               public virtual void StealFinallyClauses (ref ArrayList list)
+               {
+                       if (Parent != null)
+                               Parent.StealFinallyClauses (ref list);
+               }
+
                public bool IsAssigned (VariableInfo vi)
                {
-                       return CurrentUsageVector.IsAssigned (vi);
+                       return CurrentUsageVector.IsAssigned (vi, false);
                }
 
                public bool IsFieldAssigned (VariableInfo vi, string field_name)
                {
-                       if (CurrentUsageVector.IsAssigned (vi))
-                               return true;
-
-                       return CurrentUsageVector.IsFieldAssigned (vi, field_name);
+                       return CurrentUsageVector.IsAssigned (vi, false) || CurrentUsageVector.IsFieldAssigned (vi, field_name);
                }
 
                public void SetAssigned (VariableInfo vi)
@@ -911,94 +996,140 @@ 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);
+
+                       LabeledStatement s = Block.LookupLabel (name);
+                       if (s != null)
+                               return s;
+
+                       return base.LookupLabel (name, loc);
+               }
+
+               public override void Label (UsageVector origin_vectors)
+               {
+                       if (!CurrentUsageVector.Reachability.IsUnreachable) {
+                               UsageVector vector = CurrentUsageVector.Clone ();
+                               vector.Next = origin_vectors;
+                               origin_vectors = vector;
                        }
+
+                       CurrentUsageVector.MergeJumpOrigins (origin_vectors);
                }
 
-               public override void Return ()
+               protected override UsageVector Merge ()
                {
-                       CurrentUsageVector.Return ();
+                       return Merge (sibling_list);
                }
+       }
+
+       public class FlowBranchingBreakable : FlowBranchingBlock
+       {
+               UsageVector break_origins;
 
-               public override void Goto ()
+               public FlowBranchingBreakable (FlowBranching parent, BranchingType type, SiblingType stype, Block block, Location loc)
+                       : base (parent, type, stype, block, loc)
+               { }
+
+               public override bool AddBreakOrigin (UsageVector vector, Location loc)
                {
-                       CurrentUsageVector.Unreachable ();
+                       vector = vector.Clone ();
+                       vector.Next = break_origins;
+                       break_origins = vector;
+                       return false;
                }
 
-               public override void Throw ()
+               protected override UsageVector Merge ()
                {
-                       CurrentUsageVector.Throw ();
+                       UsageVector vector = base.Merge ();
+                       vector.MergeOrigins (this, break_origins);
+                       return vector;
                }
+       }
 
-               public override void Label (ArrayList origin_vectors)
+       public class FlowBranchingContinuable : FlowBranchingBlock
+       {
+               UsageVector continue_origins;
+
+               public FlowBranchingContinuable (FlowBranching parent, BranchingType type, SiblingType stype, Block block, Location loc)
+                       : base (parent, type, stype, block, loc)
+               { }
+
+               public override bool AddContinueOrigin (UsageVector vector, Location loc)
                {
-                       CurrentUsageVector.MergeJumpOrigins (origin_vectors);
+                       vector = vector.Clone ();
+                       vector.Next = continue_origins;
+                       continue_origins = vector;
+                       return false;
                }
 
-               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.MergeOrigins (this, continue_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;
+
+               UsageVector break_origins;
+               UsageVector continue_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 +1141,116 @@ namespace Mono.CSharp
                        get { return current_vector; }
                }
 
-               public override bool InTryBlock ()
+               public override bool InTryOrCatch ()
                {
-                       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;
+                       }
+
+                       return base.InTryWithCatch ();
                }
 
-               public override void Break ()
+               public override void AddFinallyVector (UsageVector vector)
                {
-                       CurrentUsageVector.Break ();
+                       vector = vector.Clone ();
+                       vector.Next = finally_origins;
+                       finally_origins = vector;
                }
 
-               public override void Return ()
+               public override bool AddBreakOrigin (UsageVector vector, Location loc)
                {
-                       CurrentUsageVector.Return ();
+                       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 Goto ()
+               public override bool AddContinueOrigin (UsageVector vector, Location loc)
                {
-                       CurrentUsageVector.Unreachable ();
+                       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 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 cannot leave the body of a finally clause");
+                               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.MergeChild (finally_vector, false);
 
-                       return result;
+                       // FIXME: this should probably go away.  I think it's harmless right now
+                       vector.MergeFinally (finally_vector, finally_origins);
+
+                       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);
+                       }
+
+                       return vector;
                }
        }
 
@@ -1219,9 +1399,8 @@ namespace Mono.CSharp
 
                                if (!branching.IsFieldAssigned (vi, field.Name)) {
                                        Report.Error (171, loc,
-                                                     "Field `" + TypeManager.CSharpName (Type) +
-                                                     "." + field.Name + "' must be fully initialized " +
-                                                     "before control leaves the constructor");
+                                               "Field `{0}' must be fully assigned before control leaves the constructor",
+                                               TypeManager.GetFullNameSignature (field));
                                        ok = false;
                                }
                        }
@@ -1263,13 +1442,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)
@@ -1286,6 +1467,10 @@ namespace Mono.CSharp
                                        Fields = new FieldInfo [Count];
                                        public_fields.CopyTo (Fields, 0);
                                        non_public_fields.CopyTo (Fields, CountPublic);
+                               } else if (type is GenericTypeParameterBuilder) {
+                                       CountPublic = CountNonPublic = Count = 0;
+
+                                       Fields = new FieldInfo [0];
                                } else {
                                        FieldInfo[] public_fields = type.GetFields (
                                                BindingFlags.Instance|BindingFlags.Public);
@@ -1318,7 +1503,7 @@ namespace Mono.CSharp
                                                field_hash.Add (field.Name, ++Length);
                                        else if (sinfo [i].InTransit) {
                                                Report.Error (523, String.Format (
-                                                                     "Struct member '{0}.{1}' of type '{2}' causes " +
+                                                                     "Struct member `{0}.{1}' of type `{2}' causes " +
                                                                      "a cycle in the structure layout",
                                                                      type, field.Name, sinfo [i].Type));
                                                sinfo [i] = null;
@@ -1470,7 +1655,9 @@ namespace Mono.CSharp
 
                public bool IsAssigned (EmitContext ec)
                {
-                       return !ec.DoFlowAnalysis || ec.CurrentBranching.IsAssigned (this);
+                       return !ec.DoFlowAnalysis ||
+                               ec.OmitStructFlowAnalysis && TypeInfo.IsStruct ||
+                               ec.CurrentBranching.IsAssigned (this);
                }
 
                public bool IsAssigned (EmitContext ec, Location loc)
@@ -1530,7 +1717,9 @@ namespace Mono.CSharp
 
                public bool IsFieldAssigned (EmitContext ec, string name, Location loc)
                {
-                       if (!ec.DoFlowAnalysis || ec.CurrentBranching.IsFieldAssigned (this, name))
+                       if (!ec.DoFlowAnalysis ||
+                               ec.OmitStructFlowAnalysis && TypeInfo.IsStruct ||
+                               ec.CurrentBranching.IsFieldAssigned (this, name))
                                return true;
 
                        Report.Error (170, loc,
@@ -1598,34 +1787,31 @@ 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)
+               public VariableMap (Parameters 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++) {
                                Parameter.Modifier mod = ip.ParameterModifier (i);
 
-                               if ((mod & Parameter.Modifier.OUT) == 0)
+                               if ((mod & Parameter.Modifier.OUT) != Parameter.Modifier.OUT)
                                        continue;
 
-                               VariableNames [i] = ip.ParameterName (i);
-                               VariableTypes [i] = TypeManager.GetElementType (ip.ParameterType (i));
+                               // Dont allocate till we find an out var.
+                               if (map == null)
+                                       map = new VariableInfo [Count];
+
+                               map [i] = new VariableInfo (ip.ParameterName (i),
+                                       TypeManager.GetElementType (ip.ParameterType (i)), i, Length);
 
-                               map [i] = new VariableInfo (VariableNames [i], VariableTypes [i], i, Length);
                                Length += map [i].Length;
                        }
                }
@@ -1637,21 +1823,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 +1846,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 +1857,9 @@ namespace Mono.CSharp
                // </summary>
                public VariableInfo this [int index] {
                        get {
+                               if (map == null)
+                                       return null;
+                               
                                return map [index];
                        }
                }
@@ -1711,9 +1897,7 @@ namespace Mono.CSharp
                //   we won't use the inherited vector anymore, but our own copy of it.
                // </summary>
                public bool IsDirty {
-                       get {
-                               return is_dirty;
-                       }
+                       get { return is_dirty; }
 
                        set {
                                if (!is_dirty)
@@ -1777,6 +1961,9 @@ namespace Mono.CSharp
                // </summary>
                public void Or (MyBitVector new_vector)
                {
+                       // Treat null 'new_vector' as all false, just like the And() below
+                       if (new_vector == null)
+                               return;
                        BitArray new_array = new_vector.Vector;
 
                        initialize_vector ();
@@ -1797,7 +1984,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 +2060,7 @@ namespace Mono.CSharp
                {
                        if (vector != null)
                                return;
-
+                       
                        vector = new BitArray (Count, false);
                        if (InheritsFrom != null)
                                Vector = InheritsFrom.Vector;
@@ -1878,20 +2070,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 ();
                }
        }