2007-03-15 Igor Zelmanovich <igorz@mainsoft.com>
[mono.git] / mcs / mcs / flowanalysis.cs
index 4213bc0c49da8e5b3ee889c930d38bfb7220f339..e739d6c16c3c90cd9770422ffadc2557cf23def8 100644 (file)
@@ -76,28 +76,24 @@ namespace Mono.CSharp
 
                public sealed class Reachability
                {
-                       TriState returns, throws, barrier;
+                       TriState returns, 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)
+                       Reachability (TriState returns, TriState barrier)
                        {
                                this.returns = returns;
-                               this.throws = throws;
                                this.barrier = barrier;
                        }
 
                        public Reachability Clone ()
                        {
-                               return new Reachability (returns, throws, barrier);
+                               return new Reachability (returns, barrier);
                        }
 
                        public static TriState TriState_Meet (TriState a, TriState b)
@@ -121,24 +117,22 @@ namespace Mono.CSharp
                                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);
+                               return new Reachability (TriState.Never, TriState.Never);
                        }
 
                        TriState Unreachable {
-                               get { return TriState_Max (returns, TriState_Max (throws, barrier)); }
+                               get { return TriState_Max (returns, barrier); }
                        }
 
                        TriState Reachable {
@@ -154,10 +148,6 @@ namespace Mono.CSharp
                                get { return returns == TriState.Always; }
                        }
 
-                       public bool AlwaysThrows {
-                               get { return throws == TriState.Always; }
-                       }
-
                        public bool AlwaysHasBarrier {
                                get { return barrier == TriState.Always; }
                        }
@@ -171,11 +161,6 @@ namespace Mono.CSharp
                                returns = TriState.Always;
                        }
 
-                       public void SetThrows ()
-                       {
-                               throws = TriState.Always;
-                       }
-
                        public void SetBarrier ()
                        {
                                barrier = TriState.Always;
@@ -195,9 +180,8 @@ namespace Mono.CSharp
 
                        public override string ToString ()
                        {
-                               return String.Format ("[{0}:{1}:{2}:{3}]",
-                                                     ShortName (returns), ShortName (throws), ShortName (barrier),
-                                                     ShortName (Reachable));
+                               return String.Format ("[{0}:{1}:{2}]",
+                                                     ShortName (returns), ShortName (barrier), ShortName (Reachable));
                        }
                }
 
@@ -404,14 +388,6 @@ namespace Mono.CSharp
                                        reachability.SetReturns ();
                        }
 
-                       public void Throw ()
-                       {
-                               if (!reachability.IsUnreachable) {
-                                       reachability.SetThrows ();
-                                       reachability.SetBarrier ();
-                               }
-                       }
-
                        public void Goto ()
                        {
                                if (!reachability.IsUnreachable)
@@ -425,69 +401,21 @@ namespace Mono.CSharp
 
                                MyBitVector locals = null;
                                MyBitVector parameters = null;
-                               Reachability reachability = null;
+                               Reachability reachability = sibling_list.Reachability.Clone ();
 
-                               for (UsageVector child = sibling_list; child != null; child = child.Next) {
-                                       Report.Debug (2, "    MERGING SIBLING   ", reachability, child);
+                               if (!sibling_list.Reachability.IsUnreachable) {
+                                       locals &= sibling_list.locals;
+                                       parameters &= sibling_list.parameters;
+                               }
 
-                                       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,
-                                                     child.Type, child.Reachability.IsUnreachable, unreachable);
-
-                                       if (!unreachable)
-                                               locals &= child.locals;
+                               for (UsageVector child = sibling_list.Next; child != null; child = child.Next) {
+                                       reachability.Meet (child.Reachability);
 
-                                       // An `out' parameter must be assigned in all branches which do
-                                       // not always throw an exception.
-                                       if (!child.Reachability.AlwaysThrows)
+                                       if (!child.Reachability.IsUnreachable) {
+                                               locals &= child.locals;
                                                parameters &= child.parameters;
-
-                                       Report.Debug (2, "    MERGING SIBLING #2", parameters, locals);
+                                       }
                                }
-                               
-                               if (reachability == null)
-                                       throw new InternalErrorException ("Cannot happen: the loop above runs at least twice");
 
                                return new UsageVector (parameters, locals, reachability, null, loc);
                        }
@@ -1327,7 +1255,7 @@ namespace Mono.CSharp
                                        ArrayList non_public_fields = new ArrayList ();
 
                                        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)
@@ -1761,24 +1689,29 @@ namespace Mono.CSharp
                public readonly int Count;
                public static readonly MyBitVector Empty = new MyBitVector ();
 
+               // 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 ()
                {
-                       vector = null;
                        shared = new BitArray (0, false);
-                       Count = 0;
                }
 
                public MyBitVector (MyBitVector InheritsFrom, int Count)
                {
-                       vector = null;
-                       shared = InheritsFrom == null ? null : InheritsFrom.Shared;
+                       if (InheritsFrom != null)
+                               shared = InheritsFrom.Shared;
+
                        this.Count = Count;
                }
 
+               // Use this accessor to get a shareable copy of the underlying BitArray representation
                BitArray Shared {
                        get {
+                               // Post-condition: vector == null
                                if (shared == null) {
                                        shared = vector;
                                        vector = null;
@@ -1820,28 +1753,86 @@ namespace Mono.CSharp
                // </summary>
                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 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.Shared;
+                                               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;
@@ -1851,8 +1842,8 @@ namespace Mono.CSharp
 
                public static MyBitVector operator & (MyBitVector a, MyBitVector b)
                {
-                       if (a == null && b == null)
-                               return null;
+                       if (a == b)
+                               return a;
                        if (a == null)
                                return b.Clone ();
                        if (b == null)
@@ -1865,8 +1856,8 @@ namespace Mono.CSharp
 
                public static MyBitVector operator | (MyBitVector a, MyBitVector b)
                {
-                       if (a == null && b == null)
-                               return null;
+                       if (a == b)
+                               return a;
                        if (a == null)
                                return new MyBitVector (null, b.Count);
                        if (b == null)
@@ -1893,20 +1884,15 @@ namespace Mono.CSharp
 
                void initialize_vector ()
                {
+                       // Post-condition: vector != null
                        if (shared == null) {
                                vector = new BitArray (Count, true);
                                return;
                        }
 
-                       vector = new BitArray (Count, false);
-
-                       int min = shared.Count;
-                       if (min > Count)
-                               min = Count;
-
-                       for (int i = 0; i < min; i++)
-                               vector [i] = shared [i];
-
+                       vector = new BitArray (shared);
+                       if (Count != vector.Count)
+                               vector.Length = Count;
                        shared = null;
                }