2005-05-12 Jordi Mas i Hernandez <jordi@ximian.com>
[mono.git] / mcs / mcs / flowanalysis.cs
index 6dec02bec3a0d553e76c33bdcdb4d072357e75ee..d46f3c09a1872bb6b210be120539d3cad66fa3d3 100644 (file)
@@ -79,7 +79,7 @@ namespace Mono.CSharp
 
                public sealed class Reachability
                {
-                       FlowReturns returns, breaks, throws, barrier, reachable;
+                       FlowReturns returns, breaks, throws, barrier;
 
                        public FlowReturns Returns {
                                get { return returns; }
@@ -93,10 +93,6 @@ namespace Mono.CSharp
                        public FlowReturns Barrier {
                                get { return barrier; }
                        }
-                       public FlowReturns Reachable {
-                               get { return reachable; }
-                       }
-
                        public Reachability (FlowReturns returns, FlowReturns breaks,
                                             FlowReturns throws, FlowReturns barrier)
                        {
@@ -104,15 +100,11 @@ namespace Mono.CSharp
                                this.breaks = breaks;
                                this.throws = throws;
                                this.barrier = barrier;
-
-                               update ();
                        }
 
                        public Reachability Clone ()
                        {
-                               Reachability cloned = new Reachability (returns, breaks, throws, barrier);
-                               cloned.reachable = reachable;
-                               return cloned;
+                               return new Reachability (returns, breaks, throws, barrier);
                        }
 
                        // <summary>
@@ -231,7 +223,12 @@ namespace Mono.CSharp
                                a.throws = AndFlowReturns (a.throws, b.throws);
                                a.barrier = AndFlowReturns (a.barrier, b.barrier);
 
-                               a.reachable = AndFlowReturns (a.reachable, b.reachable);
+                               if (a_unreachable && b_unreachable)
+                                       a.barrier = FlowReturns.Always;
+                               else if (a_unreachable || b_unreachable)
+                                       a.barrier = FlowReturns.Sometimes;
+                               else
+                                       a.barrier = FlowReturns.Never;
                        }
 
                        public void Or (Reachability b)
@@ -240,8 +237,6 @@ namespace Mono.CSharp
                                breaks = OrFlowReturns (breaks, b.breaks);
                                throws = OrFlowReturns (throws, b.throws);
                                barrier = OrFlowReturns (barrier, b.barrier);
-
-                               update ();
                        }
 
                        public static Reachability Never ()
@@ -251,16 +246,21 @@ namespace Mono.CSharp
                                        FlowReturns.Never, FlowReturns.Never);
                        }
 
-                       void update ()
-                       {
-                               if ((returns == FlowReturns.Always) || (breaks == FlowReturns.Always) ||
-                                   (throws == FlowReturns.Always) || (barrier == FlowReturns.Always))
-                                       reachable = FlowReturns.Never;
-                               else if ((returns == FlowReturns.Never) && (breaks == FlowReturns.Never) &&
-                                        (throws == FlowReturns.Never) && (barrier == FlowReturns.Never))
-                                       reachable = FlowReturns.Always;
-                               else
-                                       reachable = FlowReturns.Sometimes;
+                       public FlowReturns Reachable {
+                               get {
+                                       if ((returns == FlowReturns.Always) ||
+                                           (breaks == FlowReturns.Always) ||
+                                           (throws == FlowReturns.Always) ||
+                                           (barrier == FlowReturns.Always))
+                                               return FlowReturns.Never;
+                                       else if ((returns == FlowReturns.Never) &&
+                                                (breaks == FlowReturns.Never) &&
+                                                (throws == FlowReturns.Never) &&
+                                                (barrier == FlowReturns.Never))
+                                               return FlowReturns.Always;
+                                       else
+                                               return FlowReturns.Sometimes;
+                               }
                        }
 
                        public bool AlwaysBreaks {
@@ -296,49 +296,47 @@ namespace Mono.CSharp
                        }
 
                        public bool IsUnreachable {
-                               get { return reachable == FlowReturns.Never; }
+                               get { return Reachable == FlowReturns.Never; }
                        }
 
                        public void SetReturns ()
                        {
                                returns = FlowReturns.Always;
-                               update ();
                        }
 
                        public void SetReturnsSometimes ()
                        {
                                returns = FlowReturns.Sometimes;
-                               update ();
                        }
 
                        public void SetBreaks ()
                        {
                                breaks = FlowReturns.Always;
-                               update ();
                        }
 
                        public void ResetBreaks ()
                        {
                                breaks = FlowReturns.Never;
-                               update ();
                        }
 
                        public void SetThrows ()
                        {
                                throws = FlowReturns.Always;
-                               update ();
                        }
 
                        public void SetThrowsSometimes ()
                        {
                                throws = FlowReturns.Sometimes;
-                               update ();
                        }
 
                        public void SetBarrier ()
                        {
                                barrier = FlowReturns.Always;
-                               update ();
+                       }
+
+                       public void ResetBarrier ()
+                       {
+                               barrier = FlowReturns.Never;
                        }
 
                        static string ShortName (FlowReturns returns)
@@ -358,7 +356,7 @@ namespace Mono.CSharp
                                return String.Format ("[{0}:{1}:{2}:{3}:{4}]",
                                                      ShortName (returns), ShortName (breaks),
                                                      ShortName (throws), ShortName (barrier),
-                                                     ShortName (reachable));
+                                                     ShortName (Reachable));
                        }
                }
 
@@ -366,7 +364,7 @@ namespace Mono.CSharp
                {
                        switch (type) {
                        case BranchingType.Exception:
-                               return new FlowBranchingException (parent, block, loc);
+                               throw new InvalidOperationException ();
 
                        case BranchingType.Switch:
                                return new FlowBranchingBlock (parent, type, SiblingType.SwitchSection, block, loc);
@@ -631,7 +629,7 @@ namespace Mono.CSharp
                        {
                                UsageVector result = branching.Merge ();
 
-                               Report.Debug (2, "  MERGING CHILD", this, IsDirty,
+                               Report.Debug (2, "  MERGING CHILD", this, branching, IsDirty,
                                              result.ParameterVector, result.LocalVector,
                                              result.Reachability, reachability, Type);
 
@@ -661,9 +659,16 @@ namespace Mono.CSharp
                                                        // We're either finite or we may leave the loop.
                                                        new_r.SetThrowsSometimes ();
                                                }
+
+                                               if (!new_r.MayReturn && !new_r.MayThrow)
+                                                       new_r.ResetBarrier ();
                                        }
-                               } else if (branching.Type == BranchingType.Switch)
+                               } else if (branching.Type == BranchingType.Switch) {
+                                       if (new_r.MayBreak || new_r.MayReturn)
+                                               new_r.ResetBarrier ();
+
                                        new_r.ResetBreaks ();
+                               }
 
                                //
                                // We've now either reached the point after the branching or we will
@@ -687,7 +692,10 @@ namespace Mono.CSharp
                                if (result.ParameterVector != null)
                                        parameters.Or (result.ParameterVector);
 
-                               reachability.Or (new_r);
+                               if ((branching.Type == BranchingType.Block) && branching.Block.Implicit)
+                                       reachability = new_r.Clone ();
+                               else
+                                       reachability.Or (new_r);
 
                                Report.Debug (2, "  MERGING CHILD DONE", this, result,
                                              new_r, reachability);
@@ -747,14 +755,17 @@ namespace Mono.CSharp
 
                                reachability = Reachability.Never ();
 
-                               if (o_vectors == null)
+                               if (o_vectors == null) {
+                                       reachability.SetBarrier ();
                                        return;
+                               }
 
                                bool first = true;
 
                                for (UsageVector vector = o_vectors; vector != null;
                                     vector = vector.Next) {
-                                       Report.Debug (1, "    MERGING JUMP ORIGIN", vector);
+                                       Report.Debug (1, "  MERGING JUMP ORIGIN", vector,
+                                                     first, locals, vector.Locals);
 
                                        if (first) {
                                                if (locals != null && vector.Locals != null)
@@ -764,13 +775,15 @@ namespace Mono.CSharp
                                                        parameters.Or (vector.parameters);
                                                first = false;
                                        } else {
-                                               if (locals != null && vector.Locals != null)
+                                               if (locals != null)
                                                        locals.And (vector.locals);
                                                if (parameters != null)
                                                        parameters.And (vector.parameters);
                                        }
                                                
                                        Reachability.And (ref reachability, vector.Reachability, true);
+
+                                       Report.Debug (1, "  MERGING JUMP ORIGIN #1", vector);
                                }
 
                                Report.Debug (1, "  MERGING JUMP ORIGINS DONE", this);
@@ -798,14 +811,14 @@ namespace Mono.CSharp
                                Report.Debug (1, "  MERGING FINALLY ORIGIN DONE", this);
                        }
 
-                       public void MergeBreakOrigins (UsageVector o_vectors)
+                       public void MergeBreakOrigins (FlowBranching branching, UsageVector o_vectors)
                        {
                                Report.Debug (1, "  MERGING BREAK ORIGINS", this);
 
                                if (o_vectors == null)
                                        return;
 
-                               bool first = true;
+                               bool first = branching.Infinite;
 
                                for (UsageVector vector = o_vectors; vector != null;
                                     vector = vector.Next) {
@@ -1017,7 +1030,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.");
                        }
                }
 
@@ -1131,14 +1144,15 @@ namespace Mono.CSharp
                                throw new NotSupportedException ();
 
                        UsageVector vector = new UsageVector (
-                               SiblingType.Conditional, null, Block, Location,
+                               SiblingType.Block, null, Block, Location,
                                param_map.Length, local_map.Length);
 
                        UsageVector result = vector.MergeChild (this);
 
                        Report.Debug (4, "MERGE TOP BLOCK", Location, vector, result.Reachability);
 
-                       if (vector.Reachability.Throws != FlowReturns.Always)
+                       if ((vector.Reachability.Throws != FlowReturns.Always) &&
+                           (vector.Reachability.Barrier != FlowReturns.Always))
                                CheckOutParameters (vector.Parameters, Location);
 
                        return result.Reachability;
@@ -1160,29 +1174,11 @@ namespace Mono.CSharp
                                return false;
                }
 
-               //
-               // Checks whether we're in a `catch' block.
-               //
-               public virtual bool InCatch ()
+               public virtual bool InTryWithCatch ()
                {
                        if (Parent != null)
-                               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;
+                               return Parent.InTryWithCatch ();
+                       return false;
                }
 
                public virtual bool InLoop ()
@@ -1231,6 +1227,12 @@ namespace Mono.CSharp
                                throw new NotSupportedException ();
                }
 
+               public virtual void StealFinallyClauses (ref ArrayList list)
+               {
+                       if (Parent != null)
+                               Parent.StealFinallyClauses (ref list);
+               }
+
                public bool IsAssigned (VariableInfo vi)
                {
                        return CurrentUsageVector.IsAssigned (vi);
@@ -1352,7 +1354,7 @@ namespace Mono.CSharp
                {
                        UsageVector vector = base.Merge ();
 
-                       vector.MergeBreakOrigins (break_origins);
+                       vector.MergeBreakOrigins (this, break_origins);
 
                        return vector;
                }
@@ -1360,30 +1362,33 @@ namespace Mono.CSharp
 
        public class FlowBranchingException : FlowBranching
        {
+               ExceptionStatement stmt;
                UsageVector current_vector;
                UsageVector catch_vectors;
                UsageVector finally_vector;
                UsageVector finally_origins;
-               bool in_try;
+               bool emit_finally;
 
-               public FlowBranchingException (FlowBranching parent, Block block, Location loc)
-                       : base (parent, BranchingType.Exception, SiblingType.Try, block, loc)
-               { }
+               public FlowBranchingException (FlowBranching parent,
+                                              ExceptionStatement stmt)
+                       : base (parent, BranchingType.Exception, SiblingType.Try,
+                               null, stmt.loc)
+               {
+                       this.stmt = stmt;
+                       this.emit_finally = true;
+               }
 
                protected override void AddSibling (UsageVector sibling)
                {
                        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 ();
 
@@ -1399,14 +1404,18 @@ namespace Mono.CSharp
                        return finally_vector == null;
                }
 
-               public override bool InCatch ()
+               public override bool InTryWithCatch ()
                {
-                       return !in_try && (finally_vector == null);
-               }
+                       if (finally_vector == null) {
+                               Try t = stmt as Try;
+                               if (t != null && t.HasCatch)
+                                       return true;
+                       }
 
-               public override bool InFinally (bool is_return)
-               {
-                       return finally_vector != null;
+                       if (Parent != null)
+                               return Parent.InTryWithCatch ();
+
+                       return false;
                }
 
                public override bool BreakCrossesTryCatchBoundary ()
@@ -1421,8 +1430,24 @@ namespace Mono.CSharp
                        finally_origins = vector;
                }
 
+               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; }
+               }
+
                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;
@@ -1641,13 +1666,15 @@ namespace Mono.CSharp
                                if (type is TypeBuilder) {
                                        TypeContainer tc = TypeManager.LookupTypeContainer (type);
 
-                                       ArrayList fields = tc.Fields;
+                                       ArrayList fields = null;
+                                       if (tc != null)
+                                               fields = tc.Fields;
 
                                        ArrayList public_fields = new ArrayList ();
                                        ArrayList non_public_fields = new ArrayList ();
 
                                        if (fields != null) {
-                                               foreach (Field field in fields) {
+                                               foreach (FieldMember field in fields) {
                                                        if ((field.ModFlags & Modifiers.STATIC) != 0)
                                                                continue;
                                                        if ((field.ModFlags & Modifiers.PUBLIC) != 0)
@@ -2172,7 +2199,12 @@ namespace Mono.CSharp
                // </summary>
                public void And (MyBitVector new_vector)
                {
-                       BitArray new_array = new_vector.Vector;
+                       BitArray new_array;
+
+                       if (new_vector != null)
+                               new_array = new_vector.Vector;
+                       else
+                               new_array = new BitArray (Count, false);
 
                        initialize_vector ();