**** Merged from MCS ****
[mono.git] / mcs / gmcs / flowanalysis.cs
index fdcbf56c745b77b311688435a248051c943c57a9..2b227718a291bafed7146ac0fb2fbd9590d33fc0 100644 (file)
@@ -329,6 +329,12 @@ namespace Mono.CSharp
                                update ();
                        }
 
+                       public void SetThrowsSometimes ()
+                       {
+                               throws = FlowReturns.Sometimes;
+                               update ();
+                       }
+
                        public void SetBarrier ()
                        {
                                barrier = FlowReturns.Always;
@@ -371,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);
                        }
@@ -425,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.
@@ -465,9 +479,12 @@ 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;
@@ -494,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;
@@ -516,7 +537,9 @@ 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 ();
@@ -531,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);
@@ -539,7 +562,7 @@ namespace Mono.CSharp
 
                        public void SetAssigned (VariableInfo var)
                        {
-                               if (!var.IsParameter && Reachability.AlwaysBreaks)
+                               if (!var.IsParameter && Reachability.IsUnreachable)
                                        return;
 
                                IsDirty = true;
@@ -548,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);
@@ -556,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;
@@ -634,6 +657,10 @@ namespace Mono.CSharp
                                                        // We're either finite or we may leave the loop.
                                                        new_r.SetReturnsSometimes ();
                        }
+                                               if (new_r.Throws == FlowReturns.Always) {
+                                                       // We're either finite or we may leave the loop.
+                                                       new_r.SetThrowsSometimes ();
+                                               }
                        }
                                } else if (branching.Type == BranchingType.Switch)
                                        new_r.ResetBreaks ();
@@ -714,18 +741,19 @@ 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) {
@@ -770,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)
@@ -893,13 +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);
@@ -912,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.
@@ -939,7 +1017,7 @@ namespace Mono.CSharp
 
                                Report.Error (177, loc, "The out parameter `" +
                                              var.Name + "' must be " +
-                                             "assigned before control leave the current method.");
+                                             "assigned before control leaves the current method.");
                        }
                }
 
@@ -1030,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 ();
@@ -1052,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);
 
@@ -1143,6 +1223,14 @@ namespace Mono.CSharp
                                throw new NotSupportedException ();
                }
 
+               public virtual void AddBreakVector (UsageVector vector)
+               {
+                       if (Parent != null)
+                               Parent.AddBreakVector (vector);
+                       else if ((Block == null) || !Block.IsDestructor)
+                               throw new NotSupportedException ();
+               }
+
                public bool IsAssigned (VariableInfo vi)
                {
                        return CurrentUsageVector.IsAssigned (vi);
@@ -1216,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);
@@ -1233,6 +1333,31 @@ 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;
@@ -1296,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);
                }
@@ -1523,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);