**** Merged from MCS ****
[mono.git] / mcs / gmcs / flowanalysis.cs
index 75eb9c2a19e2d0463d881d9675f23d92fcbf5c2c..2b227718a291bafed7146ac0fb2fbd9590d33fc0 100644 (file)
@@ -33,7 +33,7 @@ namespace Mono.CSharp
                        Conditional,
 
                        // A loop block.
-                       LoopBlock,
+                       Loop,
 
                        // Try/Catch block.
                        Exception,
@@ -115,6 +115,60 @@ namespace Mono.CSharp
                                return cloned;
                        }
 
+                       // <summary>
+                       //   Performs an `And' operation on the FlowReturns status
+                       //   (for instance, a block only returns Always if all its siblings
+                       //   always return).
+                       // </summary>
+                       public static FlowReturns AndFlowReturns (FlowReturns a, FlowReturns b)
+                       {
+                               if (a == FlowReturns.Undefined)
+                                       return b;
+
+                               switch (a) {
+                               case FlowReturns.Never:
+                                       if (b == FlowReturns.Never)
+                                               return FlowReturns.Never;
+                                       else
+                                               return FlowReturns.Sometimes;
+
+                               case FlowReturns.Sometimes:
+                                       return FlowReturns.Sometimes;
+
+                               case FlowReturns.Always:
+                                       if (b == FlowReturns.Always)
+                                               return FlowReturns.Always;
+                                       else
+                                               return FlowReturns.Sometimes;
+
+                               default:
+                                       throw new ArgumentException ();
+                               }
+                       }
+
+                       public static FlowReturns OrFlowReturns (FlowReturns a, FlowReturns b)
+                       {
+                               if (a == FlowReturns.Undefined)
+                                       return b;
+
+                               switch (a) {
+                               case FlowReturns.Never:
+                                       return b;
+
+                               case FlowReturns.Sometimes:
+                                       if (b == FlowReturns.Always)
+                                               return FlowReturns.Always;
+                                       else
+                                               return FlowReturns.Sometimes;
+
+                               case FlowReturns.Always:
+                                       return FlowReturns.Always;
+
+                               default:
+                                       throw new ArgumentException ();
+                               }
+                       }
+
                        public static void And (ref Reachability a, Reachability b, bool do_break)
                        {
                                if (a == null) {
@@ -180,6 +234,16 @@ namespace Mono.CSharp
                                a.reachable = AndFlowReturns (a.reachable, b.reachable);
                        }
 
+                       public void Or (Reachability b)
+                       {
+                               returns = OrFlowReturns (returns, b.returns);
+                               breaks = OrFlowReturns (breaks, b.breaks);
+                               throws = OrFlowReturns (throws, b.throws);
+                               barrier = OrFlowReturns (barrier, b.barrier);
+
+                               update ();
+                       }
+
                        public static Reachability Never ()
                        {
                                return new Reachability (
@@ -265,6 +329,12 @@ namespace Mono.CSharp
                                update ();
                        }
 
+                       public void SetThrowsSometimes ()
+                       {
+                               throws = FlowReturns.Sometimes;
+                               update ();
+                       }
+
                        public void SetBarrier ()
                        {
                                barrier = FlowReturns.Always;
@@ -296,7 +366,7 @@ namespace Mono.CSharp
                {
                        switch (type) {
                        case BranchingType.Exception:
-                               return new FlowBranchingException (parent, type, block, loc);
+                               return new FlowBranchingException (parent, block, loc);
 
                        case BranchingType.Switch:
                                return new FlowBranchingBlock (parent, type, SiblingType.SwitchSection, block, loc);
@@ -307,6 +377,9 @@ namespace Mono.CSharp
                        case BranchingType.Block:
                                return new FlowBranchingBlock (parent, type, SiblingType.Block, block, loc);
 
+                       case BranchingType.Loop:
+                               return new FlowBranchingLoop (parent, block, loc);
+
                        default:
                                return new FlowBranchingBlock (parent, type, SiblingType.Conditional, block, loc);
                        }
@@ -346,37 +419,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;
-
-                       switch (a) {
-                       case FlowReturns.Never:
-                               if (b == FlowReturns.Never)
-                                       return FlowReturns.Never;
-                               else
-                                       return FlowReturns.Sometimes;
-
-                       case FlowReturns.Sometimes:
-                               return FlowReturns.Sometimes;
-
-                       case FlowReturns.Always:
-                               if (b == FlowReturns.Always)
-                                       return FlowReturns.Always;
-                               else
-                                       return FlowReturns.Sometimes;
-
-                       default:
-                               throw new ArgumentException ();
-                       }
-               }
-
                // <summary>
                //   The vector contains a BitArray with information about which local variables
                //   and parameters are already initialized at the current code position.
@@ -392,6 +434,11 @@ namespace Mono.CSharp
                        // </summary>
                        public readonly Location Location;
 
+                       // <summary>
+                       //   This is only valid for SwitchSection, Try, Catch and Finally.
+                       // </summary>
+                       public readonly Block Block;
+
                        // <summary>
                        //   If this is true, then the usage vector has been modified and must be
                        //   merged when we're done with this branching.
@@ -432,22 +479,29 @@ 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;
 
                                if (parent != null) {
+                                       if (num_locals > 0)
                                        locals = new MyBitVector (parent.locals, CountLocals);
+                                       
                                        if (num_params > 0)
                                                parameters = new MyBitVector (parent.parameters, num_params);
 
                                        reachability = parent.Reachability.Clone ();
                                } else {
+                                       if (num_locals > 0)
                                        locals = new MyBitVector (null, CountLocals);
+                                       
                                        if (num_params > 0)
                                                parameters = new MyBitVector (null, num_params);
 
@@ -457,15 +511,19 @@ namespace Mono.CSharp
                                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, Location loc)
+                                           Reachability reachability, Block block,
+                                           Location loc)
                        {
                                this.Type = SiblingType.Block;
                                this.Location = loc;
+                               this.Block = block;
 
                                this.reachability = reachability;
                                this.parameters = parameters;
@@ -479,11 +537,16 @@ namespace Mono.CSharp
                        // </summary>
                        public UsageVector Clone ()
                        {
-                               UsageVector retval = new UsageVector (Type, null, Location, CountParameters, CountLocals);
+                               UsageVector retval = new UsageVector (
+                                       Type, null, Block, Location,
+                                       CountParameters, CountLocals);
 
+                               if (retval.locals != null)
                                retval.locals = locals.Clone ();
+                               
                                if (parameters != null)
                                        retval.parameters = parameters.Clone ();
+                               
                                retval.reachability = reachability.Clone ();
 
                                return retval;
@@ -491,7 +554,7 @@ namespace Mono.CSharp
 
                        public bool IsAssigned (VariableInfo var)
                        {
-                               if (!var.IsParameter && Reachability.AlwaysBreaks)
+                               if (!var.IsParameter && Reachability.IsUnreachable)
                                        return true;
 
                                return var.IsAssigned (var.IsParameter ? parameters : locals);
@@ -499,7 +562,7 @@ namespace Mono.CSharp
 
                        public void SetAssigned (VariableInfo var)
                        {
-                               if (!var.IsParameter && Reachability.AlwaysBreaks)
+                               if (!var.IsParameter && Reachability.IsUnreachable)
                                        return;
 
                                IsDirty = true;
@@ -508,7 +571,7 @@ namespace Mono.CSharp
 
                        public bool IsFieldAssigned (VariableInfo var, string name)
                        {
-                               if (!var.IsParameter && Reachability.AlwaysBreaks)
+                               if (!var.IsParameter && Reachability.IsUnreachable)
                                        return true;
 
                                return var.IsFieldAssigned (var.IsParameter ? parameters : locals, name);
@@ -516,7 +579,7 @@ namespace Mono.CSharp
 
                        public void SetFieldAssigned (VariableInfo var, string name)
                        {
-                               if (!var.IsParameter && Reachability.AlwaysBreaks)
+                               if (!var.IsParameter && Reachability.IsUnreachable)
                                        return;
 
                                IsDirty = true;
@@ -570,33 +633,37 @@ namespace Mono.CSharp
 
                                Report.Debug (2, "  MERGING CHILD", this, IsDirty,
                                              result.ParameterVector, result.LocalVector,
-                                             result.Reachability, Type);
+                                             result.Reachability, reachability, Type);
 
-                               reachability = result.Reachability;
+                               Reachability new_r = result.Reachability;
 
-                               if (branching.Type == BranchingType.LoopBlock) {
-                                       bool may_leave_loop = reachability.MayBreak;
-                                       reachability.ResetBreaks ();
+                               if (branching.Type == BranchingType.Loop) {
+                                       bool may_leave_loop = new_r.MayBreak;
+                                       new_r.ResetBreaks ();
 
                                        if (branching.Infinite && !may_leave_loop) {
-                                               if (reachability.Returns == FlowReturns.Sometimes) {
+                                               if (new_r.Returns == FlowReturns.Sometimes) {
                                                        // If we're an infinite loop and do not break,
                                                        // the code after the loop can never be reached.
                                                        // However, if we may return from the loop,
                                                        // then we do always return (or stay in the
                                                        // loop forever).
-                                                       reachability.SetReturns ();
+                                                       new_r.SetReturns ();
                        }
 
-                                               reachability.SetBarrier ();
+                                               new_r.SetBarrier ();
                                        } else {
-                                               if (reachability.Returns == FlowReturns.Always) {
+                                               if (new_r.Returns == FlowReturns.Always) {
                                                        // We're either finite or we may leave the loop.
-                                                       reachability.SetReturnsSometimes ();
+                                                       new_r.SetReturnsSometimes ();
                        }
+                                               if (new_r.Throws == FlowReturns.Always) {
+                                                       // We're either finite or we may leave the loop.
+                                                       new_r.SetThrowsSometimes ();
+                                               }
                        }
                                } else if (branching.Type == BranchingType.Switch)
-                                       reachability.ResetBreaks ();
+                                       new_r.ResetBreaks ();
 
                                //
                                // We've now either reached the point after the branching or we will
@@ -607,20 +674,23 @@ namespace Mono.CSharp
                                // we need to look at (see above).
                                //
 
-                               if ((Type == SiblingType.SwitchSection) && !reachability.IsUnreachable) {
+                               if ((Type == SiblingType.SwitchSection) && !new_r.IsUnreachable) {
                                        Report.Error (163, Location,
                                                      "Control cannot fall through from one " +
                                                              "case label to another");
                                        return result;
                                        }
 
-                               if (result.LocalVector != null)
+                               if (locals != null && result.LocalVector != null)
                                        locals.Or (result.LocalVector);
 
                                if (result.ParameterVector != null)
                                        parameters.Or (result.ParameterVector);
 
-                               Report.Debug (2, "  MERGING CHILD DONE", this, result);
+                               reachability.Or (new_r);
+
+                               Report.Debug (2, "  MERGING CHILD DONE", this, result,
+                                             new_r, reachability);
 
                                IsDirty = true;
 
@@ -647,7 +717,7 @@ namespace Mono.CSharp
                                                MergeFinally (branching, f_origins, parameters);
                                }
 
-                               if (f_vector != null)
+                               if (f_vector != null && f_vector.LocalVector != null)
                                        MyBitVector.Or (ref locals, f_vector.LocalVector);
                        }
 
@@ -671,26 +741,30 @@ 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 ORIGINS", this);
 
                                reachability = Reachability.Never ();
 
-                               if (origin_vectors == null)
+                               if (o_vectors == null)
                                        return;
 
                                bool first = true;
 
-                               foreach (UsageVector vector in origin_vectors) {
+                               for (UsageVector vector = o_vectors; vector != null;
+                                    vector = vector.Next) {
                                        Report.Debug (1, "  MERGING JUMP ORIGIN", vector);
 
                                        if (first) {
+                                               if (locals != null && vector.Locals != null)
                                                locals.Or (vector.locals);
+                                               
                                                if (parameters != null)
                                                        parameters.Or (vector.parameters);
                                                first = false;
                                        } else {
+                                               if (locals != null && vector.Locals != null)
                                        locals.And (vector.locals);
                                        if (parameters != null)
                                                parameters.And (vector.parameters);
@@ -724,6 +798,37 @@ namespace Mono.CSharp
                                Report.Debug (1, "  MERGING FINALLY ORIGIN DONE", this);
                        }
 
+                       public void MergeBreakOrigins (UsageVector o_vectors)
+                       {
+                               Report.Debug (1, "  MERGING BREAK ORIGINS", this);
+
+                               if (o_vectors == null)
+                                       return;
+
+                               bool first = true;
+
+                               for (UsageVector vector = o_vectors; vector != null;
+                                    vector = vector.Next) {
+                                       Report.Debug (1, "    MERGING BREAK ORIGIN", vector);
+
+                                       if (first) {
+                                               if (locals != null && vector.Locals != null)
+                                                       locals.Or (vector.locals);
+                                               
+                                               if (parameters != null)
+                                                       parameters.Or (vector.parameters);
+                                               first = false;
+                                       } else {
+                                               if (locals != null && vector.Locals != null)
+                                                       locals.And (vector.locals);
+                                               if (parameters != null)
+                                                       parameters.And (vector.parameters);
+                                       }
+                               }
+
+                               Report.Debug (1, "  MERGING BREAK ORIGINS DONE", this);
+                       }
+
                        public void CheckOutParameters (FlowBranching branching)
                        {
                                if (parameters != null)
@@ -779,7 +884,10 @@ namespace Mono.CSharp
                        // </summary>
                        public MyBitVector Locals {
                                get {
+                                       if (locals != null)
                                        return locals.Clone ();
+                                       else
+                                               return null;
                                }
                        }
 
@@ -804,6 +912,8 @@ namespace Mono.CSharp
                                StringBuilder sb = new StringBuilder ();
 
                                sb.Append ("Vector (");
+                               sb.Append (Type);
+                               sb.Append (",");
                                sb.Append (id);
                                sb.Append (",");
                                sb.Append (IsDirty);
@@ -842,11 +952,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);
@@ -859,16 +972,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);
                }
 
+               public void CreateSibling ()
+               {
+                       CreateSibling (null, SiblingType.Conditional);
+               }
+
                protected abstract void AddSibling (UsageVector uv);
 
-               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.
@@ -885,8 +1016,8 @@ namespace Mono.CSharp
                                        continue;
 
                                Report.Error (177, loc, "The out parameter `" +
-                                             param_map.VariableNames [i] + "' must be " +
-                                             "assigned before control leave the current method.");
+                                             var.Name + "' must be " +
+                                             "assigned before control leaves the current method.");
                        }
                }
 
@@ -904,10 +1035,10 @@ namespace Mono.CSharp
 
                        for (UsageVector child = sibling_list; child != null; child = child.Next) {
                                bool do_break = (Type != BranchingType.Switch) &&
-                                       (Type != BranchingType.LoopBlock);
+                                       (Type != BranchingType.Loop);
                                
                                Report.Debug (2, "    MERGING SIBLING   ", child,
-                                             child.Parameters, child.Locals,
+                                             child.ParameterVector, child.LocalVector,
                                              reachability, child.Reachability, do_break);
 
                                Reachability.And (ref reachability, child.Reachability, do_break);
@@ -949,16 +1080,18 @@ namespace Mono.CSharp
                                // 
                                bool do_break_2 = (child.Type != SiblingType.Block) &&
                                        (child.Type != SiblingType.SwitchSection);
-                               bool unreachable = (do_break_2 && child.Reachability.AlwaysBreaks) ||
-                                       child.Reachability.AlwaysThrows ||
+                               bool always_throws = (child.Type != SiblingType.Try) &&
+                                       child.Reachability.AlwaysThrows;
+                               bool unreachable = always_throws ||
+                                       (do_break_2 && child.Reachability.AlwaysBreaks) ||
                                        child.Reachability.AlwaysReturns ||
                                        child.Reachability.AlwaysHasBarrier;
 
                                Report.Debug (2, "    MERGING SIBLING #1", reachability,
                                              Type, child.Type, child.Reachability.IsUnreachable,
-                                             do_break_2, unreachable);
+                                             do_break_2, always_throws, unreachable);
 
-                               if (!unreachable)
+                               if (!unreachable && (child.LocalVector != null))
                                        MyBitVector.And (ref locals, child.LocalVector);
 
                                // An `out' parameter must be assigned in all branches which do
@@ -975,7 +1108,8 @@ namespace Mono.CSharp
                        Report.Debug (2, "  MERGING SIBLINGS DONE", parameters, locals,
                                      reachability, Infinite);
 
-                       return new UsageVector (parameters, locals, reachability, Location);
+                       return new UsageVector (
+                               parameters, locals, reachability, null, Location);
                }
 
                protected abstract UsageVector Merge ();
@@ -997,7 +1131,8 @@ namespace Mono.CSharp
                                throw new NotSupportedException ();
 
                        UsageVector vector = new UsageVector (
-                               SiblingType.Conditional, null, Location, param_map.Length, local_map.Length);
+                               SiblingType.Conditional, null, Block, Location,
+                               param_map.Length, local_map.Length);
 
                        UsageVector result = vector.MergeChild (this);
 
@@ -1009,10 +1144,73 @@ namespace Mono.CSharp
                        return result.Reachability;
                }
 
-               public virtual bool InTryBlock ()
+               //
+               // Checks whether we're in a `try' block.
+               //
+               public virtual bool InTryOrCatch (bool is_return)
+               {
+                       if ((Block != null) && Block.IsDestructor)
+                               return true;
+                       else if (!is_return &&
+                           ((Type == BranchingType.Loop) || (Type == BranchingType.Switch)))
+                               return false;
+                       else if (Parent != null)
+                               return Parent.InTryOrCatch (is_return);
+                       else
+                               return false;
+               }
+
+               //
+               // Checks whether we're in a `catch' block.
+               //
+               public virtual bool InCatch ()
                {
                        if (Parent != null)
-                               return Parent.InTryBlock ();
+                               return Parent.InCatch ();
+                       else
+                               return false;
+               }
+
+               //
+               // Checks whether we're in a `finally' block.
+               //
+               public virtual bool InFinally (bool is_return)
+               {
+                       if (!is_return &&
+                           ((Type == BranchingType.Loop) || (Type == BranchingType.Switch)))
+                               return false;
+                       else if (Parent != null)
+                               return Parent.InFinally (is_return);
+                       else
+                               return false;
+               }
+
+               public virtual bool InLoop ()
+               {
+                       if (Type == BranchingType.Loop)
+                               return true;
+                       else if (Parent != null)
+                               return Parent.InLoop ();
+                       else
+                               return false;
+               }
+
+               public virtual bool InSwitch ()
+               {
+                       if (Type == BranchingType.Switch)
+                               return true;
+                       else if (Parent != null)
+                               return Parent.InSwitch ();
+                       else
+                               return false;
+               }
+
+               public virtual bool BreakCrossesTryCatchBoundary ()
+               {
+                       if ((Type == BranchingType.Loop) || (Type == BranchingType.Switch))
+                               return false;
+                       else if (Parent != null)
+                               return Parent.BreakCrossesTryCatchBoundary ();
                        else
                                return false;
                }
@@ -1021,7 +1219,15 @@ namespace Mono.CSharp
                {
                        if (Parent != null)
                                Parent.AddFinallyVector (vector);
-                       else
+                       else if ((Block == null) || !Block.IsDestructor)
+                               throw new NotSupportedException ();
+               }
+
+               public virtual void AddBreakVector (UsageVector vector)
+               {
+                       if (Parent != null)
+                               Parent.AddBreakVector (vector);
+                       else if ((Block == null) || !Block.IsDestructor)
                                throw new NotSupportedException ();
                }
 
@@ -1083,8 +1289,8 @@ namespace Mono.CSharp
        {
                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)
                { }
 
@@ -1098,12 +1304,24 @@ namespace Mono.CSharp
                        sibling_list = sibling;
                }
 
-               public override void Label (ArrayList origin_vectors)
+               public override LabeledStatement LookupLabel (string name, Location loc)
+               {
+                       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) {
-                               if (origin_vectors == null)
-                                       origin_vectors = new ArrayList (1);
-                               origin_vectors.Add (CurrentUsageVector.Clone ());
+                               UsageVector vector = CurrentUsageVector.Clone ();
+                               vector.Next = origin_vectors;
+                               origin_vectors = vector;
                }
 
                        CurrentUsageVector.MergeJumpOrigins (origin_vectors);
@@ -1115,15 +1333,41 @@ namespace Mono.CSharp
                }
        }
 
+       public class FlowBranchingLoop : FlowBranchingBlock
+       {
+               UsageVector break_origins;
+
+               public FlowBranchingLoop (FlowBranching parent, Block block, Location loc)
+                       : base (parent, BranchingType.Loop, SiblingType.Conditional, block, loc)
+               { }
+
+               public override void AddBreakVector (UsageVector vector)
+               {
+                       vector = vector.Clone ();
+                       vector.Next = break_origins;
+                       break_origins = vector;
+               }
+
+               protected override UsageVector Merge ()
+               {
+                       UsageVector vector = base.Merge ();
+
+                       vector.MergeBreakOrigins (break_origins);
+
+                       return vector;
+               }
+       }
+
        public class FlowBranchingException : FlowBranching
        {
                UsageVector current_vector;
                UsageVector catch_vectors;
                UsageVector finally_vector;
                UsageVector finally_origins;
+               bool in_try;
 
-               public FlowBranchingException (FlowBranching parent, BranchingType type, Block block, Location loc)
-                       : base (parent, type, SiblingType.Try, block, loc)
+               public FlowBranchingException (FlowBranching parent, Block block, Location loc)
+                       : base (parent, BranchingType.Exception, SiblingType.Try, block, loc)
                { }
 
                protected override void AddSibling (UsageVector sibling)
@@ -1131,12 +1375,15 @@ namespace Mono.CSharp
                        if (sibling.Type == SiblingType.Try) {
                                sibling.Next = catch_vectors;
                                catch_vectors = sibling;
+                               in_try = true;
                        } else if (sibling.Type == SiblingType.Catch) {
                                sibling.Next = catch_vectors;
                                catch_vectors = sibling;
+                               in_try = false;
                        } else if (sibling.Type == SiblingType.Finally) {
                                sibling.MergeFinallyOrigins (finally_origins);
                                finally_vector = sibling;
+                               in_try = false;
                        } else
                                throw new InvalidOperationException ();
 
@@ -1147,7 +1394,22 @@ namespace Mono.CSharp
                        get { return current_vector; }
                }
 
-               public override bool InTryBlock ()
+               public override bool InTryOrCatch (bool is_return)
+               {
+                       return finally_vector == null;
+               }
+
+               public override bool InCatch ()
+               {
+                       return !in_try && (finally_vector == null);
+               }
+
+               public override bool InFinally (bool is_return)
+               {
+                       return finally_vector != null;
+               }
+
+               public override bool BreakCrossesTryCatchBoundary ()
                {
                        return true;
                }
@@ -1159,7 +1421,26 @@ namespace Mono.CSharp
                        finally_origins = vector;
                }
 
-               public override void Label (ArrayList origin_vectors)
+               public override LabeledStatement LookupLabel (string name, Location loc)
+               {
+                       if (current_vector.Block == null)
+                               return base.LookupLabel (name, loc);
+
+                       LabeledStatement s = current_vector.Block.LookupLabel (name);
+                       if (s != null)
+                               return s;
+
+                       if (finally_vector != null) {
+                               Report.Error (
+                                       157, loc, "Control can not leave the body " +
+                                       "of the finally block");
+                               return null;
+                       }
+
+                       return base.LookupLabel (name, loc);
+               }
+
+               public override void Label (UsageVector origin_vectors)
                {
                        CurrentUsageVector.MergeJumpOrigins (origin_vectors);
                }
@@ -1386,6 +1667,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);
@@ -1698,22 +1983,16 @@ namespace Mono.CSharp
                // <summary>
                public readonly int Length;
 
-               // <summary>
-               //   Type and name of all the variables.
-               //   Note that this is null for variables for which we do not need to compute
-               //   assignment info.
-               // </summary>
-               public readonly Type[] VariableTypes;
-               public readonly string[] VariableNames;
-
                VariableInfo[] map;
 
                public VariableMap (InternalParameters ip)
                {
                        Count = ip != null ? ip.Count : 0;
-                       map = new VariableInfo [Count];
-                       VariableNames = new string [Count];
-                       VariableTypes = new Type [Count];
+                       
+                       // Dont bother allocating anything!
+                       if (Count == 0)
+                               return;
+                       
                        Length = 0;
 
                        for (int i = 0; i < Count; i++) {
@@ -1722,10 +2001,13 @@ namespace Mono.CSharp
                                if ((mod & Parameter.Modifier.OUT) == 0)
                                        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;
                        }
                }
@@ -1737,21 +2019,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++) {
@@ -1760,9 +2042,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;
                        }
@@ -1774,6 +2053,9 @@ namespace Mono.CSharp
                // </summary>
                public VariableInfo this [int index] {
                        get {
+                               if (map == null)
+                                       return null;
+                               
                                return map [index];
                        }
                }