2004-10-14 Umadevi S <sumadevi@novell.com>
[mono.git] / mcs / gmcs / flowanalysis.cs
index a765bd4fe68e10cef0e3192244969fc9de322f51..18f5714392490aea3015ffdd0fffa4ec53737b3c 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;
+                       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
-                                       reachable = FlowReturns.Sometimes;
+                                               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);
@@ -434,6 +432,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.
@@ -474,9 +477,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;
@@ -503,15 +509,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;
@@ -525,7 +535,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 ();
@@ -617,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);
 
@@ -648,8 +660,12 @@ namespace Mono.CSharp
                                                        new_r.SetThrowsSometimes ();
                                                }
                        }
-                               } 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
@@ -733,8 +749,10 @@ namespace Mono.CSharp
 
                                reachability = Reachability.Never ();
 
-                               if (o_vectors == null)
+                               if (o_vectors == null) {
+                                       reachability.SetBarrier ();
                                        return;
+                               }
 
                                bool first = true;
 
@@ -938,13 +956,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);
@@ -957,15 +976,33 @@ 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 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>
@@ -984,7 +1021,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.");
                        }
                }
 
@@ -1075,7 +1112,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 ();
@@ -1097,13 +1135,15 @@ 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);
 
                        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;
@@ -1196,6 +1236,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);
@@ -1269,6 +1315,18 @@ namespace Mono.CSharp
                        sibling_list = sibling;
                }
 
+               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) {
@@ -1313,15 +1371,22 @@ namespace Mono.CSharp
 
        public class FlowBranchingException : FlowBranching
        {
+               ExceptionStatement stmt;
                UsageVector current_vector;
                UsageVector catch_vectors;
                UsageVector finally_vector;
                UsageVector finally_origins;
+               bool emit_finally;
                bool in_try;
 
-               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)
                {
@@ -1374,6 +1439,38 @@ 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;
+
+                       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);