2008-12-28 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / flowanalysis.cs
index c8a4b23f8741ac25a784ad327ce0e9ff5dea2ef5..bedc9d74e5018bd446dc65dd6c36b38df9262d88 100644 (file)
@@ -3,8 +3,10 @@
 //
 // Author:
 //   Martin Baulig (martin@ximian.com)
+//   Raja R Harinath (rharinath@novell.com)
 //
-// (C) 2001, 2002, 2003 Ximian, Inc.
+// Copyright 2001, 2002, 2003 Ximian, Inc.
+// Copyright 2003-2008 Novell, Inc.
 //
 
 using System;
@@ -16,13 +18,6 @@ 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.
@@ -48,17 +43,20 @@ namespace Mono.CSharp
                        // part of a block headed by a jump target
                        Labeled,
 
-                       // Try/Catch block.
+                       // TryCatch block.
+                       TryCatch,
+
+                       // TryFinally, Using, Lock, CollectionForeach
                        Exception,
 
                        // Switch block.
                        Switch,
 
-                       // Switch section.
-                       SwitchSection,
-
                        // The toplevel block of a function
-                       Toplevel
+                       Toplevel,
+
+                       // An iterator block
+                       Iterator
                }
 
                // <summary>
@@ -73,147 +71,18 @@ namespace Mono.CSharp
                        Finally
                }
 
-               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; }
-                       }
-
-                       Reachability (TriState returns, TriState throws, TriState barrier)
-                       {
-                               this.returns = returns;
-                               this.throws = throws;
-                               this.barrier = barrier;
-                       }
-
-                       public Reachability Clone ()
-                       {
-                               return new Reachability (returns, throws, barrier);
-                       }
-
-                       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;
-                       }
-
-                       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; }
-                       }
-
-                       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:
                        case BranchingType.Labeled:
                        case BranchingType.Toplevel:
+                       case BranchingType.TryCatch:
                                throw new InvalidOperationException ();
 
                        case BranchingType.Switch:
                                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);
 
@@ -249,8 +118,6 @@ namespace Mono.CSharp
                // </summary>
                public readonly Location Location;
 
-               protected VariableMap param_map, local_map;
-
                static int next_id = 0;
                int id;
 
@@ -274,17 +141,6 @@ namespace Mono.CSharp
                        // </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.
-                       // </summary>
-                       public bool IsDirty;
-
-                       // <summary>
-                       //   The number of parameters in this block.
-                       // </summary>
-                       public readonly int CountParameters;
-
                        // <summary>
                        //   The number of locals in this block.
                        // </summary>
@@ -305,8 +161,8 @@ namespace Mono.CSharp
                        //
                        // Private.
                        //
-                       MyBitVector locals, parameters;
-                       Reachability reachability;
+                       MyBitVector locals;
+                       bool is_unreachable;
 
                        static int next_id = 0;
                        int id;
@@ -314,43 +170,41 @@ namespace Mono.CSharp
                        //
                        // Normally, you should not use any of these constructors.
                        //
-                       public UsageVector (SiblingType type, UsageVector parent, Block block, Location loc, int num_params, int num_locals)
+                       public UsageVector (SiblingType type, UsageVector parent, Block block, Location loc, int num_locals)
                        {
                                this.Type = type;
                                this.Block = block;
                                this.Location = loc;
                                this.InheritsFrom = parent;
-                               this.CountParameters = num_params;
                                this.CountLocals = num_locals;
 
                                locals = num_locals == 0 
                                        ? MyBitVector.Empty
                                        : new MyBitVector (parent == null ? MyBitVector.Empty : parent.locals, num_locals);
 
-                               parameters = num_params == 0
-                                       ? MyBitVector.Empty
-                                       : new MyBitVector (parent == null ? MyBitVector.Empty : parent.parameters, num_params);
-
-                               reachability = parent == null ? Reachability.Always () : parent.Reachability.Clone ();
+                               if (parent != null)
+                                       is_unreachable = parent.is_unreachable;
 
                                id = ++next_id;
+
                        }
 
                        public UsageVector (SiblingType type, UsageVector parent, Block block, Location loc)
-                               : this (type, parent, block, loc, parent.CountParameters, parent.CountLocals)
+                               : this (type, parent, block, loc, parent.CountLocals)
                        { }
 
-                       public UsageVector (MyBitVector parameters, MyBitVector locals, Reachability reachability, Block block, Location loc)
+                       private UsageVector (MyBitVector locals, bool is_unreachable, Block block, Location loc)
                        {
                                this.Type = SiblingType.Block;
                                this.Location = loc;
                                this.Block = block;
 
-                               this.reachability = reachability;
-                               this.parameters = parameters;
+                               this.is_unreachable = is_unreachable;
+
                                this.locals = locals;
 
                                id = ++next_id;
+
                        }
 
                        // <summary>
@@ -358,86 +212,89 @@ namespace Mono.CSharp
                        // </summary>
                        public UsageVector Clone ()
                        {
-                               UsageVector retval = new UsageVector (Type, null, Block, Location, CountParameters, CountLocals);
+                               UsageVector retval = new UsageVector (Type, null, Block, Location, CountLocals);
 
                                retval.locals = locals.Clone ();
-                               retval.parameters = parameters.Clone ();
-                               retval.reachability = reachability.Clone ();
+                               retval.is_unreachable = is_unreachable;
 
                                return retval;
                        }
 
                        public bool IsAssigned (VariableInfo var, bool ignoreReachability)
                        {
-                               if (!ignoreReachability && !var.IsParameter && Reachability.IsUnreachable)
+                               if (!ignoreReachability && !var.IsParameter && IsUnreachable)
                                        return true;
 
-                               return var.IsAssigned (var.IsParameter ? parameters : locals);
+                               return var.IsAssigned (locals);
                        }
 
                        public void SetAssigned (VariableInfo var)
                        {
-                               if (!var.IsParameter && Reachability.IsUnreachable)
+                               if (!var.IsParameter && IsUnreachable)
                                        return;
 
-                               IsDirty = true;
-                               var.SetAssigned (var.IsParameter ? parameters : locals);
+                               var.SetAssigned (locals);
                        }
 
                        public bool IsFieldAssigned (VariableInfo var, string name)
                        {
-                               if (!var.IsParameter && Reachability.IsUnreachable)
+                               if (!var.IsParameter && IsUnreachable)
                                        return true;
 
-                               return var.IsFieldAssigned (var.IsParameter ? parameters : locals, name);
+                               return var.IsFieldAssigned (locals, name);
                        }
 
                        public void SetFieldAssigned (VariableInfo var, string name)
                        {
-                               if (!var.IsParameter && Reachability.IsUnreachable)
+                               if (!var.IsParameter && IsUnreachable)
                                        return;
 
-                               IsDirty = true;
-                               var.SetFieldAssigned (var.IsParameter ? parameters : locals, name);
+                               var.SetFieldAssigned (locals, name);
                        }
 
-                       public Reachability Reachability {
-                               get { return reachability; }
+                       public bool IsUnreachable {
+                               get { return is_unreachable; }
                        }
 
-                       public void Return ()
+                       public void ResetBarrier ()
                        {
-                               if (!reachability.IsUnreachable) {
-                                       IsDirty = true;
-                                       reachability.SetReturns ();
-                               }
+                               is_unreachable = false;
                        }
 
-                       public void Throw ()
+                       public void Goto ()
                        {
-                               if (!reachability.IsUnreachable) {
-                                       IsDirty = true;
-                                       reachability.SetThrows ();
-                                       reachability.SetBarrier ();
-                               }
+                               is_unreachable = true;
                        }
 
-                       public void Goto ()
+                       public static UsageVector MergeSiblings (UsageVector sibling_list, Location loc)
                        {
-                               if (!reachability.IsUnreachable) {
-                                       IsDirty = true;
-                                       reachability.SetBarrier ();
+                               if (sibling_list.Next == null)
+                                       return sibling_list;
+
+                               MyBitVector locals = null;
+                               bool is_unreachable = sibling_list.is_unreachable;
+
+                               if (!sibling_list.IsUnreachable)
+                                       locals &= sibling_list.locals;
+
+                               for (UsageVector child = sibling_list.Next; child != null; child = child.Next) {
+                                       is_unreachable &= child.is_unreachable;
+
+                                       if (!child.IsUnreachable)
+                                               locals &= child.locals;
                                }
+
+                               return new UsageVector (locals, is_unreachable, null, loc);
                        }
 
                        // <summary>
                        //   Merges a child branching.
                        // </summary>
-                       public UsageVector MergeChild (UsageVector child, bool implicit_block)
+                       public UsageVector MergeChild (UsageVector child, bool overwrite)
                        {
-                               Report.Debug (2, "    MERGING CHILD EFFECTS", this, child, IsDirty, reachability, Type);
+                               Report.Debug (2, "    MERGING CHILD EFFECTS", this, child, Type);
 
-                               Reachability new_r = child.Reachability;
+                               bool new_isunr = child.is_unreachable;
 
                                //
                                // We've now either reached the point after the branching or we will
@@ -448,74 +305,27 @@ namespace Mono.CSharp
                                // we need to look at (see above).
                                //
 
-                               if ((Type == SiblingType.SwitchSection) && !new_r.IsUnreachable) {
+                               if ((Type == SiblingType.SwitchSection) && !new_isunr) {
                                        Report.Error (163, Location,
                                                      "Control cannot fall through from one " +
                                                      "case label to another");
                                        return child;
                                }
 
-                               MyBitVector.Or (ref locals, child.LocalVector);
-                               MyBitVector.Or (ref parameters, child.ParameterVector);
+                               locals |= child.locals;
 
-                               if (implicit_block)
-                                       reachability = new_r.Clone ();
-                               else
-                                       reachability.Or (new_r);
+                               // throw away un-necessary information about variables in child blocks
+                               if (locals.Count != CountLocals)
+                                       locals = new MyBitVector (locals, CountLocals);
 
-                               IsDirty = true;
+                               if (overwrite)
+                                       is_unreachable = new_isunr;
+                               else
+                                       is_unreachable |= new_isunr;
 
                                return child;
                        }
 
-                       // <summary>
-                       //   Tells control flow analysis that the current code position may be reached with
-                       //   a forward jump from any of the origins listed in `origin_vectors' which is a
-                       //   list of UsageVectors.
-                       //
-                       //   This is used when resolving forward gotos - in the following example, the
-                       //   variable `a' is uninitialized in line 8 becase this line may be reached via
-                       //   the goto in line 4:
-                       //
-                       //      1     int a;
-                       //
-                       //      3     if (something)
-                       //      4        goto World;
-                       //
-                       //      6     a = 5;
-                       //
-                       //      7  World:
-                       //      8     Console.WriteLine (a);
-                       //
-                       // </summary>
-                       public void MergeJumpOrigins (UsageVector o_vectors)
-                       {
-                               Report.Debug (1, "  MERGING JUMP ORIGINS", this);
-
-                               if (o_vectors == null)
-                                       return;
-
-                               UsageVector vector = o_vectors;
-                               if (reachability.IsUnreachable) {
-                                       Report.Debug (1, "  MERGING JUMP ORIGIN INTO UNREACHABLE", this, vector);
-                                       MyBitVector.Or (ref locals, vector.locals);
-                                       MyBitVector.Or (ref parameters, vector.parameters);
-                                       reachability.Meet (vector.Reachability);
-                                       vector = vector.Next;
-                               }
-
-                               for (; vector != null; vector = vector.Next) {
-                                       Report.Debug (1, "  MERGING JUMP ORIGIN", this, vector);
-                                       MyBitVector.And (ref locals, vector.locals);
-                                       MyBitVector.And (ref parameters, vector.parameters);
-                                       reachability.Meet (vector.Reachability);
-
-                                       Report.Debug (1, "  MERGING JUMP ORIGIN #1", vector);
-                               }
-
-                               Report.Debug (1, "  MERGING JUMP ORIGINS DONE", this);
-                       }
-
                        public void MergeOrigins (UsageVector o_vectors)
                        {
                                Report.Debug (1, "  MERGING BREAK ORIGINS", this);
@@ -523,68 +333,27 @@ namespace Mono.CSharp
                                if (o_vectors == null)
                                        return;
 
-                               if (reachability.IsUnreachable) {
-                                       locals = null;
-                                       parameters = null;
-                               }
+                               if (IsUnreachable && locals != null)
+                                       locals.SetAll (true);
 
                                for (UsageVector vector = o_vectors; vector != null; vector = vector.Next) {
                                        Report.Debug (1, "    MERGING BREAK ORIGIN", vector);
-                                       MyBitVector.And (ref locals, vector.locals);
-                                       MyBitVector.And (ref parameters, vector.parameters);
-                                       reachability.Meet (vector.Reachability);
+                                       if (vector.IsUnreachable)
+                                               continue;
+                                       locals &= vector.locals;
+                                       is_unreachable &= vector.is_unreachable;
                                }
 
                                Report.Debug (1, "  MERGING BREAK ORIGINS DONE", this);
                        }
 
-                       // <summary>
-                       //   Returns a deep copy of the parameters.
-                       // </summary>
-                       public MyBitVector Parameters {
-                               get { return parameters == null ? null : parameters.Clone (); }
-                       }
-
-                       // <summary>
-                       //   Returns a deep copy of the locals.
-                       // </summary>
-                       public MyBitVector Locals {
-                               get { return locals == null ? null : locals.Clone (); }
-                       }
-
-                       public MyBitVector ParameterVector {
-                               get { return parameters; }
-                       }
-
-                       public MyBitVector LocalVector {
-                               get { return locals; }
-                       }
-
                        //
                        // Debugging stuff.
                        //
 
                        public override string ToString ()
                        {
-                               StringBuilder sb = new StringBuilder ();
-
-                               sb.Append ("Vector (");
-                               sb.Append (Type);
-                               sb.Append (",");
-                               sb.Append (id);
-                               sb.Append (",");
-                               sb.Append (IsDirty);
-                               sb.Append (",");
-                               sb.Append (reachability);
-                               if (parameters != null) {
-                                       sb.Append (" - ");
-                                       sb.Append (parameters);
-                               }
-                               sb.Append (" - ");
-                               sb.Append (locals);
-                               sb.Append (")");
-
-                               return sb.ToString ();
+                               return String.Format ("Vector ({0},{1},{2}-{3})", Type, id, is_unreachable, locals);
                        }
                }
 
@@ -605,18 +374,10 @@ namespace Mono.CSharp
 
                        UsageVector vector;
                        if (Block != null) {
-                               param_map = Block.ParameterMap;
-                               local_map = Block.LocalMap;
-
                                UsageVector parent_vector = parent != null ? parent.CurrentUsageVector : null;
-                               vector = new UsageVector (
-                                       stype, parent_vector, Block, loc,
-                                       param_map.Length, local_map.Length);
+                               vector = new UsageVector (stype, parent_vector, Block, loc, Block.AssignableSlots);
                        } else {
-                               param_map = Parent.param_map;
-                               local_map = Parent.local_map;
-                               vector = new UsageVector (
-                                       stype, Parent.CurrentUsageVector, null, loc);
+                               vector = new UsageVector (stype, Parent.CurrentUsageVector, null, loc);
                        }
 
                        AddSibling (vector);
@@ -645,115 +406,21 @@ namespace Mono.CSharp
 
                protected abstract void AddSibling (UsageVector uv);
 
-               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);
-
-               protected UsageVector Merge (UsageVector sibling_list)
-               {
-                       if (sibling_list.Next == null)
-                               return sibling_list;
-
-                       MyBitVector locals = null;
-                       MyBitVector parameters = null;
-
-                       Reachability reachability = null;
-
-                       Report.Debug (2, "  MERGING SIBLINGS", this, Name);
-
-                       for (UsageVector child = sibling_list; child != null; child = child.Next) {
-                               Report.Debug (2, "    MERGING SIBLING   ", reachability, child);
-
-                               if (reachability == null)
-                                       reachability = child.Reachability.Clone ();
-                               else
-                                       reachability.Meet (child.Reachability);
-
-                               // A local variable is initialized after a flow branching if it
-                               // has been initialized in all its branches which do neither
-                               // always return or always throw an exception.
-                               //
-                               // If a branch may return, but does not always return, then we
-                               // can treat it like a never-returning branch here: control will
-                               // only reach the code position after the branching if we did not
-                               // return here.
-                               //
-                               // It's important to distinguish between always and sometimes
-                               // returning branches here:
-                               //
-                               //    1   int a;
-                               //    2   if (something) {
-                               //    3      return;
-                               //    4      a = 5;
-                               //    5   }
-                               //    6   Console.WriteLine (a);
-                               //
-                               // The if block in lines 3-4 always returns, so we must not look
-                               // at the initialization of `a' in line 4 - thus it'll still be
-                               // uninitialized in line 6.
-                               //
-                               // On the other hand, the following is allowed:
-                               //
-                               //    1   int a;
-                               //    2   if (something)
-                               //    3      a = 5;
-                               //    4   else
-                               //    5      return;
-                               //    6   Console.WriteLine (a);
-                               //
-                               // Here, `a' is initialized in line 3 and we must not look at
-                               // line 5 since it always returns.
-                               // 
-                               bool unreachable = child.Reachability.IsUnreachable;
-
-                               Report.Debug (2, "    MERGING SIBLING #1", reachability,
-                                             Type, child.Type, child.Reachability.IsUnreachable, unreachable);
-
-                               if (!unreachable)
-                                       MyBitVector.And (ref locals, child.LocalVector);
-
-                               // An `out' parameter must be assigned in all branches which do
-                               // not always throw an exception.
-                               if (!child.Reachability.AlwaysThrows)
-                                       MyBitVector.And (ref parameters, child.ParameterVector);
-
-                               Report.Debug (2, "    MERGING SIBLING #2", parameters, locals);
-                       }
-
-                       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 UsageVector (parameters, locals, reachability, null, Location);
-               }
-
                protected abstract UsageVector Merge ();
 
-               // <summary>
-               //   Merge a child branching.
-               // </summary>
                public UsageVector MergeChild (FlowBranching child)
                {
-                       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;
+                       return CurrentUsageVector.MergeChild (child.Merge (), true);
                }
 
-               public virtual bool InTryWithCatch ()
+               public virtual bool CheckRethrow (Location loc)
                {
-                       return Parent.InTryWithCatch ();
+                       return Parent.CheckRethrow (loc);
+               }
+
+               public virtual bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
+               {
+                       return Parent.AddResumePoint (stmt, loc, out pc);
                }
 
                // returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
@@ -769,14 +436,15 @@ namespace Mono.CSharp
                }
 
                // returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
-               public virtual bool AddReturnOrigin (UsageVector vector, Location loc)
+               public virtual bool AddReturnOrigin (UsageVector vector, ExitStatement stmt)
                {
-                       return Parent.AddReturnOrigin (vector, loc);
+                       return Parent.AddReturnOrigin (vector, stmt);
                }
 
-               public virtual void StealFinallyClauses (ref ArrayList list)
+               // returns true if we crossed an unwind-protected region (try/catch/finally, lock, using, ...)
+               public virtual bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
                {
-                       Parent.StealFinallyClauses (ref list);
+                       return Parent.AddGotoOrigin (vector, goto_stmt);
                }
 
                public bool IsAssigned (VariableInfo vi)
@@ -842,36 +510,36 @@ namespace Mono.CSharp
 
                protected override void AddSibling (UsageVector sibling)
                {
+                       if (sibling_list != null && sibling_list.Type == SiblingType.Block)
+                               throw new InternalErrorException ("Blocks don't have sibling flow paths");
                        sibling.Next = sibling_list;
                        sibling_list = sibling;
                }
 
-               public override LabeledStatement LookupLabel (string name, Location loc)
+               public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
                {
-                       if (Block == null)
-                               return base.LookupLabel (name, loc);
+                       LabeledStatement stmt = Block == null ? null : Block.LookupLabel (goto_stmt.Target);
+                       if (stmt == null)
+                               return Parent.AddGotoOrigin (vector, goto_stmt);
 
-                       LabeledStatement s = Block.LookupLabel (name);
-                       if (s != null)
-                               return s;
-
-                       return base.LookupLabel (name, loc);
+                       // forward jump
+                       goto_stmt.SetResolvedTarget (stmt);
+                       stmt.AddUsageVector (vector);
+                       return false;
                }
-
-               public override void Label (UsageVector origin_vectors)
+               
+               public static void Error_UnknownLabel (Location loc, string label)
                {
-                       if (!CurrentUsageVector.Reachability.IsUnreachable) {
-                               UsageVector vector = CurrentUsageVector.Clone ();
-                               vector.Next = origin_vectors;
-                               origin_vectors = vector;
-                       }
-
-                       CurrentUsageVector.MergeJumpOrigins (origin_vectors);
+                       Report.Error(159, loc, "The label `{0}:' could not be found within the scope of the goto statement",
+                               label);
                }
 
                protected override UsageVector Merge ()
                {
-                       return Merge (sibling_list);
+                       Report.Debug (2, "  MERGING SIBLINGS", Name);
+                       UsageVector vector = UsageVector.MergeSiblings (sibling_list, Location);
+                       Report.Debug (2, "  MERGING SIBLINGS DONE", Name, vector);
+                       return vector;
                }
        }
 
@@ -926,47 +594,79 @@ namespace Mono.CSharp
        public class FlowBranchingLabeled : FlowBranchingBlock
        {
                LabeledStatement stmt;
+               UsageVector actual;
+
                public FlowBranchingLabeled (FlowBranching parent, LabeledStatement stmt)
                        : base (parent, BranchingType.Labeled, SiblingType.Conditional, null, stmt.loc)
                {
                        this.stmt = stmt;
+                       CurrentUsageVector.MergeOrigins (stmt.JumpOrigins);
+                       actual = CurrentUsageVector.Clone ();
+
+                       // stand-in for backward jumps
+                       CurrentUsageVector.ResetBarrier ();
                }
-       }
 
-       public class FlowBranchingToplevel : FlowBranchingBlock
-       {
-               public FlowBranchingToplevel (FlowBranching parent, ToplevelBlock stmt)
-                       : base (parent, BranchingType.Toplevel, SiblingType.Conditional, stmt, stmt.loc)
+               public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
                {
+                       if (goto_stmt.Target != stmt.Name)
+                               return Parent.AddGotoOrigin (vector, goto_stmt);
+
+                       // backward jump
+                       goto_stmt.SetResolvedTarget (stmt);
+                       actual.MergeOrigins (vector.Clone ());
+
+                       return false;
                }
 
-               // <summary>
-               //   Check whether all `out' parameters have been assigned.
-               // </summary>
-               void CheckOutParameters (MyBitVector parameters, Location loc)
+               protected override UsageVector Merge ()
                {
-                       if (parameters == null)
-                               return;
+                       UsageVector vector = base.Merge ();
 
-                       for (int i = 0; i < param_map.Count; i++) {
-                               VariableInfo var = param_map [i];
+                       if (actual.IsUnreachable)
+                               Report.Warning (162, 2, stmt.loc, "Unreachable code detected");
 
-                               if (var == null)
-                                       continue;
+                       actual.MergeChild (vector, false);
+                       return actual;
+               }
+       }
 
-                               if (var.IsAssigned (parameters))
-                                       continue;
+       public class FlowBranchingIterator : FlowBranchingBlock
+       {
+               Iterator iterator;
+               public FlowBranchingIterator (FlowBranching parent, Iterator iterator)
+                       : base (parent, BranchingType.Iterator, SiblingType.Block, null, iterator.Location)
+               {
+                       this.iterator = iterator;
+               }
 
-                               Report.Error (177, loc, "The out parameter `{0}' must be assigned to before control leaves the current method",
-                                       var.Name);
-                       }
+               public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
+               {
+                       pc = iterator.AddResumePoint (stmt);
+                       return false;
+               }
+       }
+
+       public class FlowBranchingToplevel : FlowBranchingBlock
+       {
+               UsageVector return_origins;
+
+               public FlowBranchingToplevel (FlowBranching parent, ToplevelBlock stmt)
+                       : base (parent, BranchingType.Toplevel, SiblingType.Conditional, stmt, stmt.loc)
+               {
                }
 
-               public override bool InTryWithCatch ()
+               public override bool CheckRethrow (Location loc)
                {
+                       Report.Error (156, loc, "A throw statement with no arguments is not allowed outside of a catch clause");
                        return false;
                }
 
+               public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
+               {
+                       throw new InternalErrorException ("A yield in a non-iterator block");
+               }
+
                public override bool AddBreakOrigin (UsageVector vector, Location loc)
                {
                        Report.Error (139, loc, "No enclosing loop out of which to break or continue");
@@ -979,27 +679,103 @@ namespace Mono.CSharp
                        return false;
                }
 
-               public override bool AddReturnOrigin (UsageVector vector, Location loc)
+               public override bool AddReturnOrigin (UsageVector vector, ExitStatement stmt)
                {
-                       CheckOutParameters (vector.Parameters, loc);
+                       vector = vector.Clone ();
+                       vector.Location = stmt.loc;
+                       vector.Next = return_origins;
+                       return_origins = vector;
                        return false;
                }
 
-               public override void StealFinallyClauses (ref ArrayList list)
+               public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
                {
-                       // nothing to do
+                       string name = goto_stmt.Target;
+                       LabeledStatement s = Block.LookupLabel (name);
+                       if (s != null)
+                               throw new InternalErrorException ("Shouldn't get here");
+
+                       if (Parent == null) {
+                               Error_UnknownLabel (goto_stmt.loc, name);
+                               return false;
+                       }
+
+                       int errors = Report.Errors;
+                       Parent.AddGotoOrigin (vector, goto_stmt);
+                       if (errors == Report.Errors)
+                               Report.Error (1632, goto_stmt.loc, "Control cannot leave the body of an anonymous method");
+                       return false;
                }
 
-               public Reachability End ()
+               protected override UsageVector Merge ()
                {
-                       UsageVector result = Merge ();
+                       for (UsageVector origin = return_origins; origin != null; origin = origin.Next)
+                               Block.Toplevel.CheckOutParameters (origin, origin.Location);
 
-                       Report.Debug (4, "MERGE TOP BLOCK", Location, result);
+                       UsageVector vector = base.Merge ();
+                       Block.Toplevel.CheckOutParameters (vector, Block.loc);
+                       // Note: we _do_not_ merge in the return origins
+                       return vector;
+               }
 
-                       if (!result.Reachability.AlwaysThrows && !result.Reachability.AlwaysHasBarrier)
-                               CheckOutParameters (result.Parameters, Location);
+               public bool End ()
+               {
+                       return Merge ().IsUnreachable;
+               }
+       }
 
-                       return result.Reachability;
+       public class FlowBranchingTryCatch : FlowBranchingBlock
+       {
+               TryCatch stmt;
+               public FlowBranchingTryCatch (FlowBranching parent, TryCatch stmt)
+                       : base (parent, BranchingType.Block, SiblingType.Try, null, stmt.loc)
+               {
+                       this.stmt = stmt;
+               }
+
+               public override bool CheckRethrow (Location loc)
+               {
+                       return CurrentUsageVector.Next != null || Parent.CheckRethrow (loc);
+               }
+
+               public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
+               {
+                       int errors = Report.Errors;
+                       Parent.AddResumePoint (stmt, loc, out pc);
+                       if (errors == Report.Errors) {
+                               if (CurrentUsageVector.Next == null)
+                                       Report.Error (1626, loc, "Cannot yield a value in the body of a try block with a catch clause");
+                               else
+                                       Report.Error (1631, loc, "Cannot yield a value in the body of a catch clause");
+                       }
+                       return true;
+               }
+
+               public override bool AddBreakOrigin (UsageVector vector, Location loc)
+               {
+                       Parent.AddBreakOrigin (vector, loc);
+                       stmt.SomeCodeFollows ();
+                       return true;
+               }
+
+               public override bool AddContinueOrigin (UsageVector vector, Location loc)
+               {
+                       Parent.AddContinueOrigin (vector, loc);
+                       stmt.SomeCodeFollows ();
+                       return true;
+               }
+
+               public override bool AddReturnOrigin (UsageVector vector, ExitStatement exit_stmt)
+               {
+                       Parent.AddReturnOrigin (vector, exit_stmt);
+                       stmt.SomeCodeFollows ();
+                       return true;
+               }
+
+               public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
+               {
+                       Parent.AddGotoOrigin (vector, goto_stmt);
+                       return true;
                }
        }
 
@@ -1007,14 +783,87 @@ namespace Mono.CSharp
        {
                ExceptionStatement stmt;
                UsageVector current_vector;
-               UsageVector catch_vectors;
+               UsageVector try_vector;
                UsageVector finally_vector;
 
-               UsageVector break_origins;
-               UsageVector continue_origins;
-               UsageVector return_origins;
+               abstract class SavedOrigin {
+                       public readonly SavedOrigin Next;
+                       public readonly UsageVector Vector;
+
+                       protected SavedOrigin (SavedOrigin next, UsageVector vector)
+                       {
+                               Next = next;
+                               Vector = vector.Clone ();
+                       }
+
+                       protected abstract void DoPropagateFinally (FlowBranching parent);
+                       public void PropagateFinally (UsageVector finally_vector, FlowBranching parent)
+                       {
+                               if (finally_vector != null)
+                                       Vector.MergeChild (finally_vector, false);
+                               DoPropagateFinally (parent);
+                       }
+               }
+
+               class BreakOrigin : SavedOrigin {
+                       Location Loc;
+                       public BreakOrigin (SavedOrigin next, UsageVector vector, Location loc)
+                               : base (next, vector)
+                       {
+                               Loc = loc;
+                       }
+
+                       protected override void DoPropagateFinally (FlowBranching parent)
+                       {
+                               parent.AddBreakOrigin (Vector, Loc);
+                       }
+               }
+
+               class ContinueOrigin : SavedOrigin {
+                       Location Loc;
+                       public ContinueOrigin (SavedOrigin next, UsageVector vector, Location loc)
+                               : base (next, vector)
+                       {
+                               Loc = loc;
+                       }
+
+                       protected override void DoPropagateFinally (FlowBranching parent)
+                       {
+                               parent.AddContinueOrigin (Vector, Loc);
+                       }
+               }
+
+               class ReturnOrigin : SavedOrigin {
+                       public ExitStatement Stmt;
+
+                       public ReturnOrigin (SavedOrigin next, UsageVector vector, ExitStatement stmt)
+                               : base (next, vector)
+                       {
+                               Stmt = stmt;
+                       }
+
+                       protected override void DoPropagateFinally (FlowBranching parent)
+                       {
+                               parent.AddReturnOrigin (Vector, Stmt);
+                       }
+               }
+
+               class GotoOrigin : SavedOrigin {
+                       public Goto Stmt;
+
+                       public GotoOrigin (SavedOrigin next, UsageVector vector, Goto stmt)
+                               : base (next, vector)
+                       {
+                               Stmt = stmt;
+                       }
 
-               bool emit_finally;
+                       protected override void DoPropagateFinally (FlowBranching parent)
+                       {
+                               parent.AddGotoOrigin (Vector, Stmt);
+                       }
+               }
+
+               SavedOrigin saved_origins;
 
                public FlowBranchingException (FlowBranching parent,
                                               ExceptionStatement stmt)
@@ -1022,16 +871,13 @@ namespace Mono.CSharp
                                null, stmt.loc)
                {
                        this.stmt = stmt;
-                       this.emit_finally = true;
                }
 
                protected override void AddSibling (UsageVector sibling)
                {
                        switch (sibling.Type) {
                        case SiblingType.Try:
-                       case SiblingType.Catch:
-                               sibling.Next = catch_vectors;
-                               catch_vectors = sibling;
+                               try_vector = sibling;
                                break;
                        case SiblingType.Finally:
                                finally_vector = sibling;
@@ -1046,119 +892,103 @@ namespace Mono.CSharp
                        get { return current_vector; }
                }
 
-               public override bool InTryWithCatch ()
+               public override bool CheckRethrow (Location loc)
                {
-                       if (finally_vector == null) {
-                               Try t = stmt as Try;
-                               if (t != null && t.HasCatch)
-                                       return true;
-                       }
+                       if (!Parent.CheckRethrow (loc))
+                               return false;
+                       if (finally_vector == null)
+                               return true;
+                       Report.Error (724, loc, "A throw statement with no arguments is not allowed inside of a finally clause nested inside of the innermost catch clause");
+                       return false;
+               }
 
-                       return base.InTryWithCatch ();
+               public override bool AddResumePoint (ResumableStatement stmt, Location loc, out int pc)
+               {
+                       int errors = Report.Errors;
+                       Parent.AddResumePoint (this.stmt, loc, out pc);
+                       if (errors == Report.Errors) {
+                               if (finally_vector == null)
+                                       this.stmt.AddResumePoint (stmt, pc);
+                               else
+                                       Report.Error (1625, loc, "Cannot yield in the body of a finally clause");
+                       }
+                       return true;
                }
 
                public override bool AddBreakOrigin (UsageVector vector, Location loc)
                {
                        if (finally_vector != null) {
-                               Report.Error (157, loc, "Control cannot leave the body of a finally clause");
+                               int errors = Report.Errors;
+                               Parent.AddBreakOrigin (vector, loc);
+                               if (errors == Report.Errors)
+                                       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;
+                               saved_origins = new BreakOrigin (saved_origins, vector, loc);
                        }
+
+                       // either the loop test or a back jump will follow code
+                       stmt.SomeCodeFollows ();
                        return true;
                }
 
                public override bool AddContinueOrigin (UsageVector vector, Location loc)
                {
                        if (finally_vector != null) {
-                               Report.Error (157, loc, "Control cannot leave the body of a finally clause");
+                               int errors = Report.Errors;
+                               Parent.AddContinueOrigin (vector, loc);
+                               if (errors == Report.Errors)
+                                       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;
+                               saved_origins = new ContinueOrigin (saved_origins, vector, loc);
                        }
+
+                       // either the loop test or a back jump will follow code
+                       stmt.SomeCodeFollows ();
                        return true;
                }
 
-               public override bool AddReturnOrigin (UsageVector vector, Location loc)
+               public override bool AddReturnOrigin (UsageVector vector, ExitStatement exit_stmt)
                {
                        if (finally_vector != null) {
-                               Report.Error (157, loc, "Control cannot leave the body of a finally clause");
+                               int errors = Report.Errors;
+                               Parent.AddReturnOrigin (vector, exit_stmt);
+                               if (errors == Report.Errors)
+                                       exit_stmt.Error_FinallyClause ();
                        } else {
-                               vector = vector.Clone ();
-                               vector.Location = loc;
-                               vector.Next = return_origins;
-                               return_origins = vector;
+                               saved_origins = new ReturnOrigin (saved_origins, vector, exit_stmt);
                        }
-                       return true;
-               }
-
-               public override void StealFinallyClauses (ref ArrayList list)
-               {
-                       if (list == null)
-                               list = new ArrayList ();
-                       list.Add (stmt);
-                       emit_finally = false;
-                       base.StealFinallyClauses (ref list);
-               }
 
-               public bool EmitFinally {
-                       get { return emit_finally; }
+                       // sets ec.NeedReturnLabel()
+                       stmt.SomeCodeFollows ();
+                       return true;
                }
 
-               public override LabeledStatement LookupLabel (string name, Location loc)
+               public override bool AddGotoOrigin (UsageVector vector, Goto goto_stmt)
                {
-                       if (current_vector.Block == null)
-                               return base.LookupLabel (name, loc);
-
-                       LabeledStatement s = current_vector.Block.LookupLabel (name);
+                       LabeledStatement s = current_vector.Block == null ? null : current_vector.Block.LookupLabel (goto_stmt.Target);
                        if (s != null)
-                               return s;
+                               throw new InternalErrorException ("Shouldn't get here");
 
                        if (finally_vector != null) {
-                               Report.Error (157, loc,
-                                       "Control cannot leave the body of a finally clause");
-                               return null;
+                               int errors = Report.Errors;
+                               Parent.AddGotoOrigin (vector, goto_stmt);
+                               if (errors == Report.Errors)
+                                       Report.Error (157, goto_stmt.loc, "Control cannot leave the body of a finally clause");
+                       } else {
+                               saved_origins = new GotoOrigin (saved_origins, vector, goto_stmt);
                        }
-
-                       return base.LookupLabel (name, loc);
-               }
-
-               public override void Label (UsageVector origin_vectors)
-               {
-                       CurrentUsageVector.MergeJumpOrigins (origin_vectors);
+                       return true;
                }
 
                protected override UsageVector Merge ()
                {
-                       UsageVector vector = Merge (catch_vectors);
+                       UsageVector vector = try_vector.Clone ();
 
                        if (finally_vector != null)
                                vector.MergeChild (finally_vector, false);
 
-                       for (UsageVector origin = break_origins; origin != null; origin = origin.Next) {
-                               if (finally_vector != null)
-                                       origin.MergeChild (finally_vector, false);
-                               if (!origin.Reachability.IsUnreachable)
-                                       Parent.AddBreakOrigin (origin, origin.Location);
-                       }
-
-                       for (UsageVector origin = continue_origins; origin != null; origin = origin.Next) {
-                               if (finally_vector != null)
-                                       origin.MergeChild (finally_vector, false);
-                               if (!origin.Reachability.IsUnreachable)
-                                       Parent.AddContinueOrigin (origin, origin.Location);
-                       }
-
-                       for (UsageVector origin = return_origins; origin != null; origin = origin.Next) {
-                               if (finally_vector != null)
-                                       origin.MergeChild (finally_vector, false);
-                               if (!origin.Reachability.IsUnreachable)
-                                       Parent.AddReturnOrigin (origin, origin.Location);
-                       }
+                       for (SavedOrigin origin = saved_origins; origin != null; origin = origin.Next)
+                               origin.PropagateFinally (finally_vector, Parent);
 
                        return vector;
                }
@@ -1308,9 +1138,16 @@ namespace Mono.CSharp
                                FieldInfo field = struct_info.Fields [i];
 
                                if (!branching.IsFieldAssigned (vi, field.Name)) {
-                                       Report.Error (171, loc,
-                                               "Field `{0}' must be fully assigned before control leaves the constructor",
-                                               TypeManager.GetFullNameSignature (field));
+                                       FieldBase fb = TypeManager.GetField (field);
+                                       if (fb != null && (fb.ModFlags & Modifiers.BACKING_FIELD) != 0) {
+                                               Report.Error (843, loc,
+                                                       "An automatically implemented property `{0}' must be fully assigned before control leaves the constructor. Consider calling default contructor",
+                                                       fb.GetSignatureForError ());
+                                       } else {
+                                               Report.Error (171, loc,
+                                                       "Field `{0}' must be fully assigned before control leaves the constructor",
+                                                       TypeManager.GetFullNameSignature (field));
+                                       }
                                        ok = false;
                                }
                        }
@@ -1349,18 +1186,20 @@ namespace Mono.CSharp
 
                                field_type_hash.Add (type, this);
 
-                               if (type is TypeBuilder) {
-                                       TypeContainer tc = TypeManager.LookupTypeContainer (type);
-
-                                       ArrayList fields = null;
-                                       if (tc != null)
-                                               fields = tc.Fields;
+                               if (type.Module == CodeGen.Module.Builder) {
+                                       TypeContainer tc = TypeManager.LookupTypeContainer (TypeManager.DropGenericTypeArguments (type));
 
                                        ArrayList public_fields = new ArrayList ();
                                        ArrayList non_public_fields = new ArrayList ();
 
+                                       //
+                                       // TODO: tc != null is needed because FixedBuffers are not cached
+                                       //
+                                       if (tc != null) {                                       
+                                       ArrayList fields = tc.Fields;
+
                                        if (fields != null) {
-                                               foreach (FieldMember field in fields) {
+                                               foreach (FieldBase field in fields) {
                                                        if ((field.ModFlags & Modifiers.STATIC) != 0)
                                                                continue;
                                                        if ((field.ModFlags & Modifiers.PUBLIC) != 0)
@@ -1370,6 +1209,23 @@ namespace Mono.CSharp
                                                }
                                        }
 
+                                       if (tc.Events != null) {
+                                               foreach (Event e in tc.Events) {
+                                                       if ((e.ModFlags & Modifiers.STATIC) != 0)
+                                                               continue;
+
+                                                       EventField ef = e as EventField;
+                                                       if (ef == null)
+                                                               continue;
+
+                                                       if ((ef.ModFlags & Modifiers.PUBLIC) != 0)
+                                                               public_fields.Add (ef.FieldBuilder);
+                                                       else
+                                                               non_public_fields.Add (ef.FieldBuilder);
+                                               }
+                                       }
+                                       }
+
                                        CountPublic = public_fields.Count;
                                        CountNonPublic = non_public_fields.Count;
                                        Count = CountPublic + CountNonPublic;
@@ -1377,6 +1233,12 @@ namespace Mono.CSharp
                                        Fields = new FieldInfo [Count];
                                        public_fields.CopyTo (Fields, 0);
                                        non_public_fields.CopyTo (Fields, CountPublic);
+#if GMCS_SOURCE
+                               } else if (type is GenericTypeParameterBuilder) {
+                                       CountPublic = CountNonPublic = Count = 0;
+
+                                       Fields = new FieldInfo [0];
+#endif
                                } else {
                                        FieldInfo[] public_fields = type.GetFields (
                                                BindingFlags.Instance|BindingFlags.Public);
@@ -1455,6 +1317,9 @@ namespace Mono.CSharp
                                    TypeManager.IsBuiltinType (type))
                                        return null;
 
+                               if (TypeManager.IsGenericParameter (type))
+                                       return null;
+
                                StructInfo info = (StructInfo) field_type_hash [type];
                                if (info != null)
                                        return info;
@@ -1501,11 +1366,15 @@ namespace Mono.CSharp
                public readonly bool IsParameter;
 
                public readonly LocalInfo LocalInfo;
-               public readonly int ParameterIndex;
 
                readonly VariableInfo Parent;
                VariableInfo[] sub_info;
 
+               bool is_ever_assigned;
+               public bool IsEverAssigned {
+                       get { return is_ever_assigned; }
+               }
+
                protected VariableInfo (string name, Type type, int offset)
                {
                        this.Name = name;
@@ -1527,7 +1396,6 @@ namespace Mono.CSharp
 
                        this.IsParameter = parent.IsParameter;
                        this.LocalInfo = parent.LocalInfo;
-                       this.ParameterIndex = parent.ParameterIndex;
 
                        Initialize ();
                }
@@ -1552,10 +1420,9 @@ namespace Mono.CSharp
                        this.IsParameter = false;
                }
 
-               public VariableInfo (string name, Type type, int param_idx, int offset)
-                       : this (name, type, offset)
+               public VariableInfo (Parameters ip, int i, int offset)
+                       : this (ip.FixedParameters [i].Name, ip.Types [i], offset)
                {
-                       this.ParameterIndex = param_idx;
                        this.IsParameter = true;
                }
 
@@ -1585,9 +1452,14 @@ namespace Mono.CSharp
                        if (vector [Offset])
                                return true;
 
-                       for (VariableInfo parent = Parent; parent != null; parent = parent.Parent)
-                               if (vector [parent.Offset])
+                       // FIXME: Fix SetFieldAssigned to set the whole range like SetAssigned below. Then, get rid of this stanza
+                       for (VariableInfo parent = Parent; parent != null; parent = parent.Parent) {
+                               if (vector [parent.Offset]) {
+                                       // 'parent' is assigned, but someone forgot to note that all its components are assigned too
+                                       parent.SetAssigned (vector);
                                        return true;
+                               }
+                       }
 
                        // Return unless this is a struct.
                        if (!TypeInfo.IsStruct)
@@ -1610,6 +1482,7 @@ namespace Mono.CSharp
                        }
 
                        vector [Offset] = true;
+                       is_ever_assigned = true;
                        return true;
                }
 
@@ -1621,7 +1494,11 @@ namespace Mono.CSharp
 
                public void SetAssigned (MyBitVector vector)
                {
-                       vector [Offset] = true;
+                       if (Length == 1)
+                               vector [Offset] = true;
+                       else
+                               vector.SetRange (Offset, Length);
+                       is_ever_assigned = true;
                }
 
                public bool IsFieldAssigned (EmitContext ec, string name, Location loc)
@@ -1661,6 +1538,7 @@ namespace Mono.CSharp
                                return;
 
                        vector [Offset + field_idx] = true;
+                       is_ever_assigned = true;
                }
 
                public VariableInfo GetSubStruct (string name)
@@ -1680,105 +1558,6 @@ namespace Mono.CSharp
                }
        }
 
-       // <summary>
-       //   This is used by the flow code to hold the `layout' of the flow vector for
-       //   all locals and all parameters (ie. we create one instance of this class for the
-       //   locals and another one for the params).
-       // </summary>
-       public class VariableMap {
-               // <summary>
-               //   The number of variables in the map.
-               // </summary>
-               public readonly int Count;
-
-               // <summary>
-               //   Total length of the flow vector for this map.
-               // <summary>
-               public readonly int Length;
-
-               VariableInfo[] map;
-
-               public VariableMap (Parameters ip)
-               {
-                       Count = ip != null ? ip.Count : 0;
-                       
-                       // 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) != Parameter.Modifier.OUT)
-                                       continue;
-
-                               // 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);
-
-                               Length += map [i].Length;
-                       }
-               }
-
-               public VariableMap (LocalInfo[] locals)
-                       : this (null, locals)
-               { }
-
-               public VariableMap (VariableMap parent, LocalInfo[] locals)
-               {
-                       int offset = 0, start = 0;
-                       if (parent != null && parent.map != null) {
-                               offset = parent.Length;
-                               start = parent.Count;
-                       }
-
-                       Count = locals.Length + start;
-                       
-                       if (Count == 0)
-                               return;
-                       
-                       map = new VariableInfo [Count];
-                       Length = offset;
-
-                       if (parent != null && parent.map != null) {
-                               parent.map.CopyTo (map, 0);
-                       }
-
-                       for (int i = start; i < Count; i++) {
-                               LocalInfo li = locals [i-start];
-
-                               if (li.VariableType == null)
-                                       continue;
-
-                               map [i] = li.VariableInfo = new VariableInfo (li, Length);
-                               Length += map [i].Length;
-                       }
-               }
-
-               // <summary>
-               //   Returns the VariableInfo for variable @index or null if we don't need to
-               //   compute assignment info for this variable.
-               // </summary>
-               public VariableInfo this [int index] {
-                       get {
-                               if (map == null)
-                                       return null;
-                               
-                               return map [index];
-                       }
-               }
-
-               public override string ToString ()
-               {
-                       return String.Format ("VariableMap ({0}:{1})", Count, Length);
-               }
-       }
-
        // <summary>
        //   This is a special bit vector which can inherit from another bit vector doing a
        //   copy-on-write strategy.  The inherited vector may have a smaller size than the
@@ -1786,49 +1565,61 @@ namespace Mono.CSharp
        // </summary>
        public class MyBitVector {
                public readonly int Count;
-               public MyBitVector InheritsFrom;
                public static readonly MyBitVector Empty = new MyBitVector ();
 
-               BitArray vector;
+               // Invariant: vector != null => vector.Count == Count
+               // Invariant: vector == null || shared == null
+               //            i.e., at most one of 'vector' and 'shared' can be non-null.  They can both be null -- that means all-ones
+               // The object in 'shared' cannot be modified, while 'vector' can be freely modified
+               BitArray vector, shared;
 
                MyBitVector ()
                {
-                       InheritsFrom = null;
-                       Count = 0;
+                       shared = new BitArray (0, false);
                }
 
                public MyBitVector (MyBitVector InheritsFrom, int Count)
                {
-                       if (InheritsFrom != null) {
-                               while (InheritsFrom.InheritsFrom != null)
-                                       InheritsFrom = InheritsFrom.InheritsFrom;                               
-                               if (InheritsFrom.Count >= Count && InheritsFrom.vector == null)
-                                       InheritsFrom = null;
-                       }
+                       if (InheritsFrom != null)
+                               shared = InheritsFrom.MakeShared (Count);
 
-                       this.InheritsFrom = InheritsFrom;
                        this.Count = Count;
                }
 
+               BitArray MakeShared (int new_count)
+               {
+                       // Post-condition: vector == null
+
+                       // ensure we don't leak out dirty bits from the BitVector we inherited from
+                       if (new_count > Count &&
+                           ((shared != null && shared.Count > Count) ||
+                            (shared == null && vector == null)))
+                               initialize_vector ();
+
+                       if (vector != null) {
+                               shared = vector;
+                               vector = null;
+                       }
+
+                       return shared;
+               }
+
                // <summary>
                //   Get/set bit `index' in the bit vector.
                // </summary>
-               public bool this [int index]
-               {
+               public bool this [int index] {
                        get {
                                if (index >= Count)
-                                       throw new ArgumentOutOfRangeException ();
-
-                               // We're doing a "copy-on-write" strategy here; as long
-                               // as nobody writes to the array, we can use our parent's
-                               // copy instead of duplicating the vector.
+                                       // FIXME: Disabled due to missing anonymous method flow analysis
+                                       // throw new ArgumentOutOfRangeException ();
+                                       return true; 
 
                                if (vector != null)
                                        return vector [index];
-                               if (InheritsFrom == null)
+                               if (shared == null)
                                        return true;
-                               if (index < InheritsFrom.Count)
-                                       return InheritsFrom [index];
+                               if (index < shared.Count)
+                                       return shared [index];
                                return false;
                        }
 
@@ -1846,90 +1637,184 @@ namespace Mono.CSharp
                //   Performs an `or' operation on the bit vector.  The `new_vector' may have a
                //   different size than the current one.
                // </summary>
-               private void Or (MyBitVector new_vector)
+               private MyBitVector Or (MyBitVector new_vector)
                {
-                       int min = new_vector.Count;
+                       if (Count == 0 || new_vector.Count == 0)
+                               return this;
+
+                       BitArray o = new_vector.vector != null ? new_vector.vector : new_vector.shared;
+
+                       if (o == null) {
+                               int n = new_vector.Count;
+                               if (n < Count) {
+                                       for (int i = 0; i < n; ++i)
+                                               this [i] = true;
+                               } else {
+                                       SetAll (true);
+                               }
+                               return this;
+                       }
+
+                       if (Count == o.Count) {
+                               if (vector == null) {
+                                       if (shared == null)
+                                               return this;
+                                       initialize_vector ();
+                               }
+                               vector.Or (o);
+                               return this;
+                       }
+
+                       int min = o.Count;
                        if (Count < min)
                                min = Count;
 
-                       for (int i = 0; i < min; i++)
-                               this [i] |= new_vector [i];
+                       for (int i = 0; i < min; i++) {
+                               if (o [i])
+                                       this [i] = true;
+                       }
+
+                       return this;
                }
 
                // <summary>
-               //   Perfonrms an `and' operation on the bit vector.  The `new_vector' may have
+               //   Performs an `and' operation on the bit vector.  The `new_vector' may have
                //   a different size than the current one.
                // </summary>
-               private void And (MyBitVector new_vector)
+               private MyBitVector And (MyBitVector new_vector)
                {
-                       int min = new_vector.Count;
+                       if (Count == 0)
+                               return this;
+
+                       BitArray o = new_vector.vector != null ? new_vector.vector : new_vector.shared;
+
+                       if (o == null) {
+                               for (int i = new_vector.Count; i < Count; ++i)
+                                       this [i] = false;
+                               return this;
+                       }
+
+                       if (o.Count == 0) {
+                               SetAll (false);
+                               return this;
+                       }
+
+                       if (Count == o.Count) {
+                               if (vector == null) {
+                                       if (shared == null) {
+                                               shared = new_vector.MakeShared (Count);
+                                               return this;
+                                       }
+                                       initialize_vector ();
+                               }
+                               vector.And (o);
+                               return this;
+                       }
+
+                       int min = o.Count;
                        if (Count < min)
                                min = Count;
 
-                       for (int i = 0; i < min; i++)
-                               this [i] &= new_vector [i];
+                       for (int i = 0; i < min; i++) {
+                               if (! o [i])
+                                       this [i] = false;
+                       }
 
                        for (int i = min; i < Count; i++)
                                this [i] = false;
+
+                       return this;
                }
 
-               public static void And (ref MyBitVector target, MyBitVector vector)
+               public static MyBitVector operator & (MyBitVector a, MyBitVector b)
                {
-                       if (vector == null)
-                               return;
-                       if (target == null)
-                               target = vector.Clone ();
+                       if (a == b)
+                               return a;
+                       if (a == null)
+                               return b.Clone ();
+                       if (b == null)
+                               return a.Clone ();
+                       if (a.Count > b.Count)
+                               return a.Clone ().And (b);
                        else
-                               target.And (vector);
+                               return b.Clone ().And (a);                                      
                }
 
-               public static void Or (ref MyBitVector target, MyBitVector vector)
+               public static MyBitVector operator | (MyBitVector a, MyBitVector b)
                {
-                       if (target == null)
-                               return;
-                       if (vector == null)
-                               target = null;
+                       if (a == b)
+                               return a;
+                       if (a == null)
+                               return new MyBitVector (null, b.Count);
+                       if (b == null)
+                               return new MyBitVector (null, a.Count);
+                       if (a.Count > b.Count)
+                               return a.Clone ().Or (b);
                        else
-                               target.Or (vector);
+                               return b.Clone ().Or (a);
                }
 
-               // <summary>
-               //   This does a deep copy of the bit vector.
-               // </summary>
                public MyBitVector Clone ()
                {
-                       if (Count == 0)
-                               return Empty;
-                       MyBitVector retval = new MyBitVector (this, Count);
-                       retval.initialize_vector ();
-                       return retval;
+                       return Count == 0 ? Empty : new MyBitVector (this, Count);
                }
 
-               void initialize_vector ()
+               public void SetRange (int offset, int length)
                {
-                       if (InheritsFrom == null) {
-                               vector = new BitArray (Count, true);
+                       if (offset > Count || offset + length > Count)
+                               throw new ArgumentOutOfRangeException ();
+
+                       if (shared == null && vector == null)
                                return;
+
+                       int i = 0;
+                       if (shared != null) {
+                               if (offset + length <= shared.Count) {
+                                       for (; i < length; ++i)
+                                               if (!shared [i+offset])
+                                                   break;
+                                       if (i == length)
+                                               return;
+                               }
+                               initialize_vector ();
                        }
+                       for (; i < length; ++i)
+                               vector [i+offset] = true;
 
-                       vector = new BitArray (Count, false);
+               }
 
-                       int min = InheritsFrom.Count;
-                       if (min > Count)
-                               min = Count;
+               public void SetAll (bool value)
+               {
+                       // Don't clobber Empty
+                       if (Count == 0)
+                               return;
+                       shared = value ? null : Empty.MakeShared (Count);
+                       vector = null;
+               }
 
-                       for (int i = 0; i < min; i++)
-                               vector [i] = InheritsFrom [i];
+               void initialize_vector ()
+               {
+                       // Post-condition: vector != null
+                       if (shared == null) {
+                               vector = new BitArray (Count, true);
+                               return;
+                       }
 
-                       InheritsFrom = null;
+                       vector = new BitArray (shared);
+                       if (Count != vector.Count)
+                               vector.Length = Count;
+                       shared = null;
                }
 
                StringBuilder Dump (StringBuilder sb)
                {
-                       if (vector == null)
-                               return InheritsFrom == null ? sb.Append ("/") : InheritsFrom.Dump (sb.Append ("="));
-                       for (int i = 0; i < Count; i++)
-                               sb.Append (this [i] ? "1" : "0");
+                       BitArray dump = vector == null ? shared : vector;
+                       if (dump == null)
+                               return sb.Append ("/");
+                       if (dump == shared)
+                               sb.Append ("=");
+                       for (int i = 0; i < dump.Count; i++)
+                               sb.Append (dump [i] ? "1" : "0");
                        return sb;
                }