- Fixed baseline aligning calcs
[mono.git] / mcs / mbas / statement.cs
index f424cb4b45181fb9f66ee6cea7a4d720a7194f95..de580cd2f01c40c5f683eb422f6d8f51f5cb0ee9 100644 (file)
@@ -4,6 +4,7 @@
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
 //   Martin Baulig (martin@gnome.org)
+//      Anirban Bhattacharjee (banirban@novell.com)
 //
 // (C) 2001, 2002 Ximian, Inc.
 //
@@ -14,7 +15,7 @@ using System.Reflection;
 using System.Reflection.Emit;
 using System.Diagnostics;
 
-namespace Mono.CSharp {
+namespace Mono.MonoBASIC {
 
        using System.Collections;
        
@@ -33,7 +34,17 @@ namespace Mono.CSharp {
                /// <summary>
                ///   Return value indicates whether all code paths emitted return.
                /// </summary>
-               public abstract bool Emit (EmitContext ec);
+               protected abstract bool DoEmit (EmitContext ec);
+
+               /// <summary>
+               ///   Return value indicates whether all code paths emitted return.
+               /// </summary>
+               public virtual bool Emit (EmitContext ec)
+               {
+                       ec.Mark (loc);
+                       Report.Debug (8, "MARK", this, loc);
+                       return DoEmit (ec);
+               }
                
                public static Expression ResolveBoolean (EmitContext ec, Expression e, Location loc)
                {
@@ -42,8 +53,7 @@ namespace Mono.CSharp {
                                return null;
                        
                        if (e.Type != TypeManager.bool_type){
-                               e = Expression.ConvertImplicit (ec, e, TypeManager.bool_type,
-                                                               new Location (-1));
+                               e = Expression.ConvertImplicit (ec, e, TypeManager.bool_type, Location.Null);
                        }
 
                        if (e == null){
@@ -51,8 +61,7 @@ namespace Mono.CSharp {
                                        31, loc, "Can not convert the expression to a boolean");
                        }
 
-                       if (CodeGen.SymbolWriter != null)
-                               ec.Mark (loc);
+                       ec.Mark (loc);
 
                        return e;
                }
@@ -114,7 +123,7 @@ namespace Mono.CSharp {
                        return true;
                }
                
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        return false;
                }
@@ -173,7 +182,7 @@ namespace Mono.CSharp {
                        return true;
                }
                
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
                        Label false_target = ig.DefineLabel ();
@@ -226,14 +235,27 @@ namespace Mono.CSharp {
                }
        }
 
+       public enum DoOptions {
+               WHILE,
+               UNTIL,
+               TEST_BEFORE,
+               TEST_AFTER
+       };
+
        public class Do : Statement {
                public Expression expr;
                public readonly Statement  EmbeddedStatement;
+               //public DoOptions type;
+               public DoOptions test;
+               bool infinite, may_return;
+
                
-               public Do (Statement statement, Expression boolExpr, Location l)
+               public Do (Statement statement, Expression boolExpr, DoOptions do_test, Location l)
                {
                        expr = boolExpr;
                        EmbeddedStatement = statement;
+//                     type = do_type;
+                       test = do_test;
                        loc = l;
                }
 
@@ -246,72 +268,91 @@ namespace Mono.CSharp {
                        if (!EmbeddedStatement.Resolve (ec))
                                ok = false;
 
-                       ec.EndFlowBranching ();
-
                        expr = ResolveBoolean (ec, expr, loc);
                        if (expr == null)
                                ok = false;
-                       
+                       else if (expr is BoolConstant){
+                               bool res = ((BoolConstant) expr).Value;
+
+                               if (res)
+                                       infinite = true;
+                       }
+
+                       ec.CurrentBranching.Infinite = infinite;
+                       FlowReturns returns = ec.EndFlowBranching ();
+                       may_return = returns != FlowReturns.NEVER;
+
                        return ok;
                }
                
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
                        Label loop = ig.DefineLabel ();
                        Label old_begin = ec.LoopBegin;
                        Label old_end = ec.LoopEnd;
                        bool  old_inloop = ec.InLoop;
-                       bool old_breaks = ec.Breaks;
                        int old_loop_begin_try_catch_level = ec.LoopBeginTryCatchLevel;
                        
                        ec.LoopBegin = ig.DefineLabel ();
                        ec.LoopEnd = ig.DefineLabel ();
                        ec.InLoop = true;
                        ec.LoopBeginTryCatchLevel = ec.TryCatchLevel;
-                               
-                       ig.MarkLabel (loop);
-                       ec.Breaks = false;
-                       EmbeddedStatement.Emit (ec);
-                       bool breaks = ec.Breaks;
-                       ig.MarkLabel (ec.LoopBegin);
 
-                       //
-                       // Dead code elimination
-                       //
-                       if (expr is BoolConstant){
-                               bool res = ((BoolConstant) expr).Value;
+                       if (test == DoOptions.TEST_AFTER) {
+                               ig.MarkLabel (loop);
+                               EmbeddedStatement.Emit (ec);
+                               ig.MarkLabel (ec.LoopBegin);
 
-                               if (res)
-                                       ec.ig.Emit (OpCodes.Br, loop); 
-                       } else
-                               EmitBoolExpression (ec, expr, loop, true);
-                       
-                       ig.MarkLabel (ec.LoopEnd);
+                               //
+                               // Dead code elimination
+                               //
+                               if (expr is BoolConstant){
+                                       bool res = ((BoolConstant) expr).Value;
+
+                                       if (res)
+                                               ec.ig.Emit (OpCodes.Br, loop);
+                               } else
+                                       EmitBoolExpression (ec, expr, loop, true);
 
+                               ig.MarkLabel (ec.LoopEnd);
+                       }
+                       else
+                       {
+                               ig.MarkLabel (loop);
+                               ig.MarkLabel (ec.LoopBegin);
+
+                               //
+                               // Dead code elimination
+                               //
+                               if (expr is BoolConstant){
+                                       bool res = ((BoolConstant) expr).Value;
+
+                                       if (res)
+                                               ec.ig.Emit (OpCodes.Br, ec.LoopEnd);
+                               } else
+                                       EmitBoolExpression (ec, expr, ec.LoopEnd, true);
+
+                               EmbeddedStatement.Emit (ec);
+                               ec.ig.Emit (OpCodes.Br, loop);
+                               ig.MarkLabel (ec.LoopEnd);
+                       }
                        ec.LoopBeginTryCatchLevel = old_loop_begin_try_catch_level;
                        ec.LoopBegin = old_begin;
                        ec.LoopEnd = old_end;
                        ec.InLoop = old_inloop;
-                       ec.Breaks = old_breaks;
-
-                       //
-                       // Inform whether we are infinite or not
-                       //
-                       if (expr is BoolConstant){
-                               BoolConstant bc = (BoolConstant) expr;
 
-                               if (bc.Value == true)
-                                       return breaks == false;
-                       }
-                       
-                       return false;
+                       if (infinite)
+                               return may_return == false;
+                       else
+                               return false;
                }
        }
 
        public class While : Statement {
                public Expression expr;
                public readonly Statement Statement;
+               bool may_return, empty, infinite;
                
                public While (Expression boolExpr, Statement statement, Location l)
                {
@@ -330,22 +371,47 @@ namespace Mono.CSharp {
 
                        ec.StartFlowBranching (FlowBranchingType.LOOP_BLOCK, loc);
 
+                       //
+                       // Inform whether we are infinite or not
+                       //
+                       if (expr is BoolConstant){
+                               BoolConstant bc = (BoolConstant) expr;
+
+                               if (bc.Value == false){
+                                       Warning_DeadCodeFound (Statement.loc);
+                                       empty = true;
+                               } else
+                                       infinite = true;
+                       } else {
+                               //
+                               // We are not infinite, so the loop may or may not be executed.
+                               //
+                               ec.CurrentBranching.CreateSibling ();
+                       }
+
                        if (!Statement.Resolve (ec))
                                ok = false;
 
-                       ec.EndFlowBranching ();
+                       if (empty)
+                               ec.KillFlowBranching ();
+                       else {
+                               ec.CurrentBranching.Infinite = infinite;
+                               FlowReturns returns = ec.EndFlowBranching ();
+                               may_return = returns != FlowReturns.NEVER;
+                       }
 
                        return ok;
                }
                
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
+                       if (empty)
+                               return false;
+
                        ILGenerator ig = ec.ig;
                        Label old_begin = ec.LoopBegin;
                        Label old_end = ec.LoopEnd;
                        bool old_inloop = ec.InLoop;
-                       bool old_breaks = ec.Breaks;
-                       Label while_loop = ig.DefineLabel ();
                        int old_loop_begin_try_catch_level = ec.LoopBeginTryCatchLevel;
                        bool ret;
                        
@@ -354,9 +420,6 @@ namespace Mono.CSharp {
                        ec.InLoop = true;
                        ec.LoopBeginTryCatchLevel = ec.TryCatchLevel;
 
-                       ig.Emit (OpCodes.Br, ec.LoopBegin);
-                       ig.MarkLabel (while_loop);
-
                        //
                        // Inform whether we are infinite or not
                        //
@@ -364,25 +427,21 @@ namespace Mono.CSharp {
                                BoolConstant bc = (BoolConstant) expr;
 
                                ig.MarkLabel (ec.LoopBegin);
-                               if (bc.Value == false){
-                                       Warning_DeadCodeFound (Statement.loc);
-                                       ret = false;
-                               } else {
-                                       bool breaks;
-                                       
-                                       ec.Breaks = false;
-                                       Statement.Emit (ec);
-                                       breaks = ec.Breaks;
-                                       ig.Emit (OpCodes.Br, ec.LoopBegin);
+                               Statement.Emit (ec);
+                               ig.Emit (OpCodes.Br, ec.LoopBegin);
                                        
-                                       //
-                                       // Inform that we are infinite (ie, `we return'), only
-                                       // if we do not `break' inside the code.
-                                       //
-                                       ret = breaks == false;
-                               }
+                               //
+                               // Inform that we are infinite (ie, `we return'), only
+                               // if we do not `break' inside the code.
+                               //
+                               ret = may_return == false;
                                ig.MarkLabel (ec.LoopEnd);
                        } else {
+                               Label while_loop = ig.DefineLabel ();
+
+                               ig.Emit (OpCodes.Br, ec.LoopBegin);
+                               ig.MarkLabel (while_loop);
+
                                Statement.Emit (ec);
                        
                                ig.MarkLabel (ec.LoopBegin);
@@ -396,7 +455,6 @@ namespace Mono.CSharp {
                        ec.LoopBegin = old_begin;
                        ec.LoopEnd = old_end;
                        ec.InLoop = old_inloop;
-                       ec.Breaks = old_breaks;
                        ec.LoopBeginTryCatchLevel = old_loop_begin_try_catch_level;
 
                        return ret;
@@ -408,6 +466,7 @@ namespace Mono.CSharp {
                readonly Statement InitStatement;
                readonly Statement Increment;
                readonly Statement Statement;
+               bool may_return, infinite, empty;
                
                public For (Statement initStatement,
                            Expression test,
@@ -421,6 +480,7 @@ namespace Mono.CSharp {
                        Statement = statement;
                        loc = l;
                }
+               
 
                public override bool Resolve (EmitContext ec)
                {
@@ -435,7 +495,17 @@ namespace Mono.CSharp {
                                Test = ResolveBoolean (ec, Test, loc);
                                if (Test == null)
                                        ok = false;
-                       }
+                               else if (Test is BoolConstant){
+                                       BoolConstant bc = (BoolConstant) Test;
+
+                                       if (bc.Value == false){
+                                               Warning_DeadCodeFound (Statement.loc);
+                                               empty = true;
+                                       } else
+                                               infinite = true;
+                               }
+                       } else
+                               infinite = true;
 
                        if (Increment != null){
                                if (!Increment.Resolve (ec))
@@ -443,22 +513,32 @@ namespace Mono.CSharp {
                        }
 
                        ec.StartFlowBranching (FlowBranchingType.LOOP_BLOCK, loc);
+                       if (!infinite)
+                               ec.CurrentBranching.CreateSibling ();
 
                        if (!Statement.Resolve (ec))
                                ok = false;
 
-                       ec.EndFlowBranching ();
+                       if (empty)
+                               ec.KillFlowBranching ();
+                       else {
+                               ec.CurrentBranching.Infinite = infinite;
+                               FlowReturns returns = ec.EndFlowBranching ();
+                               may_return = returns != FlowReturns.NEVER;
+                       }
 
                        return ok;
                }
                
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
+                       if (empty)
+                               return false;
+
                        ILGenerator ig = ec.ig;
                        Label old_begin = ec.LoopBegin;
                        Label old_end = ec.LoopEnd;
                        bool old_inloop = ec.InLoop;
-                       bool old_breaks = ec.Breaks;
                        int old_loop_begin_try_catch_level = ec.LoopBeginTryCatchLevel;
                        Label loop = ig.DefineLabel ();
                        Label test = ig.DefineLabel ();
@@ -474,9 +554,7 @@ namespace Mono.CSharp {
 
                        ig.Emit (OpCodes.Br, test);
                        ig.MarkLabel (loop);
-                       ec.Breaks = false;
                        Statement.Emit (ec);
-                       bool breaks = ec.Breaks;
 
                        ig.MarkLabel (ec.LoopBegin);
                        if (!(Increment is EmptyStatement))
@@ -496,7 +574,6 @@ namespace Mono.CSharp {
                        ec.LoopBegin = old_begin;
                        ec.LoopEnd = old_end;
                        ec.InLoop = old_inloop;
-                       ec.Breaks = old_breaks;
                        ec.LoopBeginTryCatchLevel = old_loop_begin_try_catch_level;
                        
                        //
@@ -507,16 +584,16 @@ namespace Mono.CSharp {
                                        BoolConstant bc = (BoolConstant) Test;
 
                                        if (bc.Value)
-                                               return breaks == false;
+                                               return may_return == false;
                                }
                                return false;
                        } else
-                               return breaks == false;
+                               return may_return == false;
                }
        }
        
        public class StatementExpression : Statement {
-               Expression expr;
+               public Expression expr;
                
                public StatementExpression (ExpressionStatement expr, Location l)
                {
@@ -530,7 +607,7 @@ namespace Mono.CSharp {
                        return expr != null;
                }
                
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
                        
@@ -574,12 +651,15 @@ namespace Mono.CSharp {
 
                        if (ec.CurrentBranching.InTryBlock ())
                                ec.CurrentBranching.AddFinallyVector (vector);
+                       else
+                               vector.CheckOutParameters (ec.CurrentBranching);
 
                        vector.Returns = FlowReturns.ALWAYS;
+                       vector.Breaks = FlowReturns.ALWAYS;
                        return true;
                }
                
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        if (ec.InFinally){
                                Report.Error (157,loc,"Control can not leave the body of the finally block");
@@ -594,7 +674,7 @@ namespace Mono.CSharp {
                        } else {
                                if (Expr == null){
                                        Report.Error (126, loc, "An object of type `" +
-                                                     TypeManager.CSharpName (ec.ReturnType) + "' is " +
+                                                     TypeManager.MonoBASIC_Name (ec.ReturnType) + "' is " +
                                                      "expected for the return statement");
                                        return true;
                                }
@@ -662,7 +742,7 @@ namespace Mono.CSharp {
                        }
                }
 
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        Label l = label.LabelTarget (ec);
                        ec.ig.Emit (OpCodes.Br, l);
@@ -720,13 +800,17 @@ namespace Mono.CSharp {
                {
                        if (vectors != null)
                                ec.CurrentBranching.CurrentUsageVector.MergeJumpOrigins (vectors);
+                       else {
+                               ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.NEVER;
+                               ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.NEVER;
+                       }
 
                        referenced = true;
 
                        return true;
                }
 
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        LabelTarget (ec);
                        ec.ig.MarkLabel (label);
@@ -748,12 +832,11 @@ namespace Mono.CSharp {
 
                public override bool Resolve (EmitContext ec)
                {
-                       ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.UNREACHABLE;
-                       ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
+                       ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.UNREACHABLE;
                        return true;
                }
 
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        if (ec.Switch == null){
                                Report.Error (153, loc, "goto default is only valid in a switch statement");
@@ -814,12 +897,11 @@ namespace Mono.CSharp {
 
                        label = sl.ILLabelCode;
 
-                       ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.UNREACHABLE;
-                       ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
+                       ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.UNREACHABLE;
                        return true;
                }
 
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        ec.ig.Emit (OpCodes.Br, label);
                        return true;
@@ -855,7 +937,7 @@ namespace Mono.CSharp {
                                if ((t != TypeManager.exception_type) &&
                                    !t.IsSubclassOf (TypeManager.exception_type) &&
                                    !(expr is NullLiteral)) {
-                                       Report.Error (155, loc,
+                                       Report.Error (30665, loc,
                                                      "The type caught or thrown must be derived " +
                                                      "from System.Exception");
                                        return false;
@@ -863,10 +945,11 @@ namespace Mono.CSharp {
                        }
 
                        ec.CurrentBranching.CurrentUsageVector.Returns = FlowReturns.EXCEPTION;
+                       ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.EXCEPTION;
                        return true;
                }
                        
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        if (expr == null){
                                if (ec.InCatch)
@@ -897,11 +980,12 @@ namespace Mono.CSharp {
 
                public override bool Resolve (EmitContext ec)
                {
+                       ec.CurrentBranching.MayLeaveLoop = true;
                        ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
                        return true;
                }
 
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
 
@@ -910,7 +994,6 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       ec.Breaks = true;
                        if (ec.InTry || ec.InCatch)
                                ig.Emit (OpCodes.Leave, ec.LoopEnd);
                        else
@@ -919,6 +1002,78 @@ namespace Mono.CSharp {
                        return false;
                }
        }
+       
+       public enum ExitType {
+               DO, 
+               FOR, 
+               WHILE,
+               SELECT,
+               SUB,
+               FUNCTION,
+               PROPERTY,
+               TRY                     
+       };
+       
+       public class Exit : Statement {
+               public readonly ExitType type;
+               public Exit (ExitType t, Location l)
+               {
+                       loc = l;
+                       type = t;
+               }
+
+               public override bool Resolve (EmitContext ec)
+               {
+                       ec.CurrentBranching.MayLeaveLoop = true;
+                       ec.CurrentBranching.CurrentUsageVector.Breaks = FlowReturns.ALWAYS;
+                       return true;
+               }
+
+               protected override bool DoEmit (EmitContext ec)
+               {
+                       ILGenerator ig = ec.ig;
+
+                       if (type != ExitType.SUB && type != ExitType.FUNCTION && 
+                               type != ExitType.PROPERTY && type != ExitType.TRY) {
+                               if (ec.InLoop == false && ec.Switch == null){
+                                       if (type == ExitType.FOR)
+                                               Report.Error (30096, loc, "No enclosing FOR loop to exit from");
+                                       if (type == ExitType.WHILE) \r
+                                               Report.Error (30097, loc, "No enclosing WHILE loop to exit from");
+                                       if (type == ExitType.DO)
+                                               Report.Error (30089, loc, "No enclosing DO loop to exit from");
+                                       if (type == ExitType.SELECT)
+                                               Report.Error (30099, loc, "No enclosing SELECT to exit from");
+
+                                       return false;
+                               }
+
+                               if (ec.InTry || ec.InCatch)
+                                       ig.Emit (OpCodes.Leave, ec.LoopEnd);
+                               else
+                                       ig.Emit (OpCodes.Br, ec.LoopEnd);
+                       } else {                        
+                               if (ec.InFinally){
+                                       Report.Error (30393, loc, 
+                                               "Control can not leave the body of the finally block");
+                                       return false;
+                               }
+                       
+                               if (ec.InTry || ec.InCatch) {
+                                       if (!ec.HasReturnLabel) {
+                                               ec.ReturnLabel = ec.ig.DefineLabel ();
+                                               ec.HasReturnLabel = true;
+                                       }
+                                       ec.ig.Emit (OpCodes.Leave, ec.ReturnLabel);
+                               } else
+                                       ec.ig.Emit (OpCodes.Ret);
+
+                               return true; 
+                       }
+                       
+                       return false;
+               }
+       }       
 
        public class Continue : Statement {
                
@@ -933,7 +1088,7 @@ namespace Mono.CSharp {
                        return true;
                }
 
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        Label begin = ec.LoopBegin;
                        
@@ -1147,7 +1302,7 @@ namespace Mono.CSharp {
                        set {
                                initialize_vector ();
 
-                               for (int i = 0; i < Math.Min (vector.Count, value.Count); i++)
+                               for (int i = 0; i < System.Math.Min (vector.Count, value.Count); i++)
                                        vector [i] = value [i];
                        }
                }
@@ -1236,6 +1391,16 @@ namespace Mono.CSharp {
                // </summary>
                public ArrayList Siblings;
 
+               // <summary>
+               //   If this is an infinite loop.
+               // </summary>
+               public bool Infinite;
+
+               // <summary>
+               //   If we may leave the current loop.
+               // </summary>
+               public bool MayLeaveLoop;
+
                //
                // Private
                //
@@ -1319,7 +1484,7 @@ namespace Mono.CSharp {
                        //
                        MyBitVector locals, parameters;
                        FlowReturns real_returns, real_breaks;
-                       bool returns_set, breaks_set, is_finally;
+                       bool is_finally;
 
                        static int next_id = 0;
                        int id;
@@ -1339,6 +1504,8 @@ namespace Mono.CSharp {
                                        locals = new MyBitVector (parent.locals, CountLocals);
                                        if (num_params > 0)
                                                parameters = new MyBitVector (parent.parameters, num_params);
+                                       real_returns = parent.Returns;
+                                       real_breaks = parent.Breaks;
                                } else {
                                        locals = new MyBitVector (null, CountLocals);
                                        if (num_params > 0)
@@ -1420,6 +1587,10 @@ namespace Mono.CSharp {
 
                        // <summary>
                        //   Specifies when the current block returns.
+                       //   If this is FlowReturns.UNREACHABLE, then control can never reach the
+                       //   end of the method (so that we don't need to emit a return statement).
+                       //   The same applies for FlowReturns.EXCEPTION, but in this case the return
+                       //   value will never be used.
                        // </summary>
                        public FlowReturns Returns {
                                get {
@@ -1428,7 +1599,6 @@ namespace Mono.CSharp {
 
                                set {
                                        real_returns = value;
-                                       returns_set = true;
                                }
                        }
 
@@ -1436,6 +1606,8 @@ namespace Mono.CSharp {
                        //   Specifies whether control may return to our containing block
                        //   before reaching the end of this block.  This happens if there
                        //   is a break/continue/goto/return in it.
+                       //   This can also be used to find out whether the statement immediately
+                       //   following the current block may be reached or not.
                        // </summary>
                        public FlowReturns Breaks {
                                get {
@@ -1444,7 +1616,34 @@ namespace Mono.CSharp {
 
                                set {
                                        real_breaks = value;
-                                       breaks_set = true;
+                               }
+                       }
+
+                       public bool AlwaysBreaks {
+                               get {
+                                       return (Breaks == FlowReturns.ALWAYS) ||
+                                               (Breaks == FlowReturns.EXCEPTION) ||
+                                               (Breaks == FlowReturns.UNREACHABLE);
+                               }
+                       }
+
+                       public bool MayBreak {
+                               get {
+                                       return Breaks != FlowReturns.NEVER;
+                               }
+                       }
+
+                       public bool AlwaysReturns {
+                               get {
+                                       return (Returns == FlowReturns.ALWAYS) ||
+                                               (Returns == FlowReturns.EXCEPTION);
+                               }
+                       }
+
+                       public bool MayReturn {
+                               get {
+                                       return (Returns == FlowReturns.SOMETIMES) ||
+                                               (Returns == FlowReturns.ALWAYS);
                                }
                        }
 
@@ -1459,22 +1658,24 @@ namespace Mono.CSharp {
                                FlowReturns new_returns = FlowReturns.NEVER;
                                FlowReturns new_breaks = FlowReturns.NEVER;
                                bool new_returns_set = false, new_breaks_set = false;
-                               bool breaks;
 
-                               Report.Debug (1, "MERGING CHILDREN", branching, this);
+                               Report.Debug (2, "MERGING CHILDREN", branching, branching.Type,
+                                             this, children.Count);
 
                                foreach (UsageVector child in children) {
-                                       Report.Debug (1, "  MERGING CHILD", child, child.is_finally);
-
+                                       Report.Debug (2, "  MERGING CHILD", child, child.is_finally);
+                                       
                                        if (!child.is_finally) {
-                                               // If Returns is already set, perform an
-                                               // `And' operation on it, otherwise just set just.
-                                               if (!new_returns_set) {
-                                                       new_returns = child.Returns;
-                                                       new_returns_set = true;
-                                               } else
-                                                       new_returns = AndFlowReturns (
-                                                               new_returns, child.Returns);
+                                               if (child.Breaks != FlowReturns.UNREACHABLE) {
+                                                       // If Returns is already set, perform an
+                                                       // `And' operation on it, otherwise just set just.
+                                                       if (!new_returns_set) {
+                                                               new_returns = child.Returns;
+                                                               new_returns_set = true;
+                                                       } else
+                                                               new_returns = AndFlowReturns (
+                                                                       new_returns, child.Returns);
+                                               }
 
                                                // If Breaks is already set, perform an
                                                // `And' operation on it, otherwise just set just.
@@ -1484,19 +1685,7 @@ namespace Mono.CSharp {
                                                } else
                                                        new_breaks = AndFlowReturns (
                                                                new_breaks, child.Breaks);
-
-                                               // Check whether control may reach the end of this sibling.
-                                               // This happens unless we either always return or always break.
-                                               if ((child.Returns == FlowReturns.EXCEPTION) ||
-                                                   (child.Returns == FlowReturns.ALWAYS) ||
-                                                   ((branching.Type != FlowBranchingType.SWITCH_SECTION) &&
-                                                    (branching.Type != FlowBranchingType.LOOP_BLOCK) &&
-                                                    (child.Breaks == FlowReturns.ALWAYS)))
-                                                       breaks = true;
-                                               else
-                                                       breaks = false;
-                                       } else
-                                               breaks = false;
+                                       }
 
                                        // Ignore unreachable children.
                                        if (child.Returns == FlowReturns.UNREACHABLE)
@@ -1537,42 +1726,60 @@ namespace Mono.CSharp {
                                        // Here, `a' is initialized in line 3 and we must not look at
                                        // line 5 since it always returns.
                                        // 
-                                       if (!breaks) {
-                                               if (new_locals != null)
-                                                       new_locals.And (child.locals);
-                                               else {
+                                       if (child.is_finally) {
+                                               if (new_locals == null)
+                                                       new_locals = locals.Clone ();
+                                               new_locals.Or (child.locals);
+
+                                               if (parameters != null) {
+                                                       if (new_params == null)
+                                                               new_params = parameters.Clone ();
+                                                       new_params.Or (child.parameters);
+                                               }
+
+                                       } else {
+                                               if (!child.AlwaysReturns && !child.AlwaysBreaks) {
+                                                       if (new_locals != null)
+                                                               new_locals.And (child.locals);
+                                                       else {
+                                                               new_locals = locals.Clone ();
+                                                               new_locals.Or (child.locals);
+                                                       }
+                                               } else if (children.Count == 1) {
                                                        new_locals = locals.Clone ();
                                                        new_locals.Or (child.locals);
                                                }
-                                       }
 
-                                       // An `out' parameter must be assigned in all branches which do
-                                       // not always throw an exception.
-                                       if (!child.is_finally && (child.Returns != FlowReturns.EXCEPTION)) {
+                                               // An `out' parameter must be assigned in all branches which do
+                                               // not always throw an exception.
                                                if (parameters != null) {
-                                                       if (new_params != null)
-                                                               new_params.And (child.parameters);
-                                                       else {
+                                                       if (child.Breaks != FlowReturns.EXCEPTION) {
+                                                               if (new_params != null)
+                                                                       new_params.And (child.parameters);
+                                                               else {
+                                                                       new_params = parameters.Clone ();
+                                                                       new_params.Or (child.parameters);
+                                                               }
+                                                       } else if (children.Count == 1) {
                                                                new_params = parameters.Clone ();
                                                                new_params.Or (child.parameters);
                                                        }
                                                }
                                        }
-
-                                       // If we always return, check whether all `out' parameters have
-                                       // been assigned.
-                                       if ((child.Returns == FlowReturns.ALWAYS) && (child.parameters != null)) {
-                                               branching.CheckOutParameters (
-                                                       child.parameters, branching.Location);
-                                       }
                                }
 
-                               // Set new `Returns' status.
-                               if (!returns_set) {
-                                       Returns = new_returns;
-                                       returns_set = true;
-                               } else
-                                       Returns = AndFlowReturns (Returns, new_returns);
+                               Returns = new_returns;
+                               if ((branching.Type == FlowBranchingType.BLOCK) ||
+                                   (branching.Type == FlowBranchingType.EXCEPTION) ||
+                                   (new_breaks == FlowReturns.UNREACHABLE) ||
+                                   (new_breaks == FlowReturns.EXCEPTION))
+                                       Breaks = new_breaks;
+                               else if (branching.Type == FlowBranchingType.SWITCH_SECTION)
+                                       Breaks = new_returns;
+                               else if (branching.Type == FlowBranchingType.SWITCH){
+                                       if (new_breaks == FlowReturns.ALWAYS)
+                                               Breaks = FlowReturns.ALWAYS;
+                               }
 
                                //
                                // We've now either reached the point after the branching or we will
@@ -1583,45 +1790,50 @@ namespace Mono.CSharp {
                                // we need to look at (see above).
                                //
 
-                               bool or_locals = (Returns == FlowReturns.NEVER) ||
-                                       (Returns == FlowReturns.SOMETIMES);
-                               if ((branching.Type != FlowBranchingType.SWITCH_SECTION) &&
-                                   (branching.Type != FlowBranchingType.LOOP_BLOCK))
-                                       or_locals &= ((Breaks == FlowReturns.NEVER) ||
-                                                     (Breaks == FlowReturns.SOMETIMES));
+                               if (((new_breaks != FlowReturns.ALWAYS) &&
+                                    (new_breaks != FlowReturns.EXCEPTION) &&
+                                    (new_breaks != FlowReturns.UNREACHABLE)) ||
+                                   (children.Count == 1)) {
+                                       if (new_locals != null)
+                                               locals.Or (new_locals);
 
-                               if ((new_locals != null) && or_locals) {
-                                       locals.Or (new_locals);
+                                       if (new_params != null)
+                                               parameters.Or (new_params);
                                }
 
-                               if ((new_params != null) && (Breaks == FlowReturns.NEVER))
-                                       parameters.Or (new_params);
-
-                               //
-                               // If we may have returned (this only happens if there was a reachable
-                               // `return' statement in one of the branches), then we may return to our
-                               // parent block before reaching the end of the block, so set `Breaks'.
-                               //
-                               if ((Returns != FlowReturns.NEVER) && (Returns != FlowReturns.SOMETIMES)) {
-                                       // real_breaks = Returns;
-                                       // breaks_set = true;
-                               } else if (branching.Type == FlowBranchingType.BLOCK) {
-                                       //
-                                       // If this is not a loop or switch block, `break' actually breaks.
-                                       //
-
-                                       if (!breaks_set) {
-                                               Breaks = new_breaks;
-                                               breaks_set = true;
-                                       } else
-                                               Breaks = AndFlowReturns (Breaks, new_breaks);
+                               Report.Debug (2, "MERGING CHILDREN DONE", branching.Type,
+                                             new_params, new_locals, new_returns, new_breaks,
+                                             branching.Infinite, branching.MayLeaveLoop, this);
+
+                               if (branching.Type == FlowBranchingType.SWITCH_SECTION) {
+                                       if ((new_breaks != FlowReturns.ALWAYS) &&
+                                           (new_breaks != FlowReturns.EXCEPTION) &&
+                                           (new_breaks != FlowReturns.UNREACHABLE))
+                                               Report.Error (163, branching.Location,
+                                                             "Control cannot fall through from one " +
+                                                             "case label to another");
                                }
 
-                               if (new_returns == FlowReturns.EXCEPTION)
-                                       Breaks = FlowReturns.UNREACHABLE;
+                               if (branching.Infinite && !branching.MayLeaveLoop) {
+                                       Report.Debug (1, "INFINITE", new_returns, new_breaks,
+                                                     Returns, Breaks, this);
 
-                               Report.Debug (1, "MERGING CHILDREN DONE", new_params, new_locals,
-                                             new_returns, new_breaks, this);
+                                       // We're actually infinite.
+                                       if (new_returns == FlowReturns.NEVER) {
+                                               Breaks = FlowReturns.UNREACHABLE;
+                                               return FlowReturns.UNREACHABLE;
+                                       }
+
+                                       // 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).
+                                       if ((new_returns == FlowReturns.SOMETIMES) ||
+                                           (new_returns == FlowReturns.ALWAYS)) {
+                                               Returns = FlowReturns.ALWAYS;
+                                               return FlowReturns.ALWAYS;
+                                       }
+                               }
 
                                return new_returns;
                        }
@@ -1651,7 +1863,7 @@ namespace Mono.CSharp {
                                Report.Debug (1, "MERGING JUMP ORIGIN", this);
 
                                real_breaks = FlowReturns.NEVER;
-                               breaks_set = false;
+                               real_returns = FlowReturns.NEVER;
 
                                foreach (UsageVector vector in origin_vectors) {
                                        Report.Debug (1, "  MERGING JUMP ORIGIN", vector);
@@ -1660,6 +1872,7 @@ namespace Mono.CSharp {
                                        if (parameters != null)
                                                parameters.And (vector.parameters);
                                        Breaks = AndFlowReturns (Breaks, vector.Breaks);
+                                       Returns = AndFlowReturns (Returns, vector.Returns);
                                }
 
                                Report.Debug (1, "MERGING JUMP ORIGIN DONE", this);
@@ -1674,7 +1887,6 @@ namespace Mono.CSharp {
                                Report.Debug (1, "MERGING FINALLY ORIGIN", this);
 
                                real_breaks = FlowReturns.NEVER;
-                               breaks_set = false;
 
                                foreach (UsageVector vector in finally_vectors) {
                                        Report.Debug (1, "  MERGING FINALLY ORIGIN", vector);
@@ -1689,6 +1901,12 @@ namespace Mono.CSharp {
                                Report.Debug (1, "MERGING FINALLY ORIGIN DONE", this);
                        }
 
+                       public void CheckOutParameters (FlowBranching branching)
+                       {
+                               if (parameters != null)
+                                       branching.CheckOutParameters (parameters, branching.Location);
+                       }
+
                        // <summary>
                        //   Performs an `or' operation on the locals and the parameters.
                        // </summary>
@@ -1922,9 +2140,16 @@ namespace Mono.CSharp {
                // </summary>
                public FlowReturns MergeChild (FlowBranching child)
                {
-                       return CurrentUsageVector.MergeChildren (child, child.Siblings);
-               }
+                       FlowReturns returns = CurrentUsageVector.MergeChildren (child, child.Siblings);
 
+                       if (child.Type != FlowBranchingType.LOOP_BLOCK)
+                               MayLeaveLoop |= child.MayLeaveLoop;
+                       else
+                               MayLeaveLoop = false;
+
+                       return returns;
+               }
                // <summary>
                //   Does the toplevel merging.
                // </summary>
@@ -1935,17 +2160,21 @@ namespace Mono.CSharp {
 
                        UsageVector vector = new UsageVector (null, num_params, Block.CountVariables);
 
+                       Report.Debug (1, "MERGING TOP BLOCK", Location, vector);
+
                        vector.MergeChildren (this, Siblings);
 
                        Siblings.Clear ();
                        Siblings.Add (vector);
 
-                       Report.Debug (1, "MERGING TOP BLOCK", vector);
+                       Report.Debug (1, "MERGING TOP BLOCK DONE", Location, vector);
 
-                       if (vector.Returns != FlowReturns.EXCEPTION)
-                               CheckOutParameters (CurrentUsageVector.Parameters, Location);
-
-                       return vector.Returns;
+                       if (vector.Breaks != FlowReturns.EXCEPTION) {
+                               if (!vector.AlwaysBreaks)
+                                       CheckOutParameters (CurrentUsageVector.Parameters, Location);
+                               return vector.AlwaysBreaks ? FlowReturns.ALWAYS : vector.Returns;
+                       } else
+                               return FlowReturns.EXCEPTION;
                }
 
                public bool InTryBlock ()
@@ -1973,7 +2202,7 @@ namespace Mono.CSharp {
 
                public bool IsVariableAssigned (VariableInfo vi)
                {
-                       if (CurrentUsageVector.Breaks == FlowReturns.UNREACHABLE)
+                       if (CurrentUsageVector.AlwaysBreaks)
                                return true;
                        else
                                return CurrentUsageVector [vi, 0];
@@ -1981,7 +2210,7 @@ namespace Mono.CSharp {
 
                public bool IsVariableAssigned (VariableInfo vi, int field_idx)
                {
-                       if (CurrentUsageVector.Breaks == FlowReturns.UNREACHABLE)
+                       if (CurrentUsageVector.AlwaysBreaks)
                                return true;
                        else
                                return CurrentUsageVector [vi, field_idx];
@@ -1989,7 +2218,7 @@ namespace Mono.CSharp {
 
                public void SetVariableAssigned (VariableInfo vi)
                {
-                       if (CurrentUsageVector.Breaks == FlowReturns.UNREACHABLE)
+                       if (CurrentUsageVector.AlwaysBreaks)
                                return;
 
                        CurrentUsageVector [vi, 0] = true;
@@ -1997,7 +2226,7 @@ namespace Mono.CSharp {
 
                public void SetVariableAssigned (VariableInfo vi, int field_idx)
                {
-                       if (CurrentUsageVector.Breaks == FlowReturns.UNREACHABLE)
+                       if (CurrentUsageVector.AlwaysBreaks)
                                return;
 
                        CurrentUsageVector [vi, field_idx] = true;
@@ -2035,7 +2264,11 @@ namespace Mono.CSharp {
                        if (index == 0)
                                return true;
 
-                       int field_idx = struct_params [number] [field_name];
+                       MyStructInfo info = (MyStructInfo) struct_params [number];
+                       if (info == null)
+                               return true;
+
+                       int field_idx = info [field_name];
 
                        return CurrentUsageVector [index + field_idx];
                }
@@ -2045,7 +2278,7 @@ namespace Mono.CSharp {
                        if (param_map [number] == 0)
                                return;
 
-                       if (CurrentUsageVector.Breaks == FlowReturns.NEVER)
+                       if (!CurrentUsageVector.AlwaysBreaks)
                                CurrentUsageVector [param_map [number]] = true;
                }
 
@@ -2056,12 +2289,48 @@ namespace Mono.CSharp {
                        if (index == 0)
                                return;
 
-                       int field_idx = struct_params [number] [field_name];
+                       MyStructInfo info = (MyStructInfo) struct_params [number];
+                       if (info == null)
+                               return;
+
+                       int field_idx = info [field_name];
 
-                       if (CurrentUsageVector.Breaks == FlowReturns.NEVER)
+                       if (!CurrentUsageVector.AlwaysBreaks)
                                CurrentUsageVector [index + field_idx] = true;
                }
 
+               public bool IsReachable ()
+               {
+                       bool reachable;
+
+                       switch (Type) {
+                       case FlowBranchingType.SWITCH_SECTION:
+                               // The code following a switch block is reachable unless the switch
+                               // block always returns.
+                               reachable = !CurrentUsageVector.AlwaysReturns;
+                               break;
+
+                       case FlowBranchingType.LOOP_BLOCK:
+                               // The code following a loop is reachable unless the loop always
+                               // returns or it's an infinite loop without any `break's in it.
+                               reachable = !CurrentUsageVector.AlwaysReturns &&
+                                       (CurrentUsageVector.Breaks != FlowReturns.UNREACHABLE);
+                               break;
+
+                       default:
+                               // The code following a block or exception is reachable unless the
+                               // block either always returns or always breaks.
+                               reachable = !CurrentUsageVector.AlwaysBreaks &&
+                                       !CurrentUsageVector.AlwaysReturns;
+                               break;
+                       }
+
+                       Report.Debug (1, "REACHABLE", Type, CurrentUsageVector.Returns,
+                                     CurrentUsageVector.Breaks, CurrentUsageVector, reachable);
+
+                       return reachable;
+               }
+
                public override string ToString ()
                {
                        StringBuilder sb = new StringBuilder ("FlowBranching (");
@@ -2241,7 +2510,7 @@ namespace Mono.CSharp {
                }
 
                public bool IsAssigned (EmitContext ec, Location loc)
-               {
+               {/* FIXME: we shouldn't just skip this!!!
                        if (!ec.DoFlowAnalysis || ec.CurrentBranching.IsVariableAssigned (this))
                                return true;
 
@@ -2266,13 +2535,13 @@ namespace Mono.CSharp {
 
                                        FieldInfo field = struct_info [i];
                                        Report.Error (171, loc,
-                                                     "Field `" + TypeManager.CSharpName (VariableType) +
+                                                     "Field `" + TypeManager.MonoBASIC_Name (VariableType) +
                                                      "." + field.Name + "' must be fully initialized " +
                                                      "before control leaves the constructor");
                                        return false;
                                }
                        }
-
+*/
                        return true;
                }
 
@@ -2355,7 +2624,7 @@ namespace Mono.CSharp {
                //
                // The statements in this block
                //
-               ArrayList statements;
+               public ArrayList statements;
 
                //
                // An array of Blocks.  We keep track of children just
@@ -2369,21 +2638,21 @@ namespace Mono.CSharp {
                //
                // Labels.  (label, block) pairs.
                //
-               Hashtable labels;
+               CaseInsensitiveHashtable labels;
 
                //
                // Keeps track of (name, type) pairs
                //
-               Hashtable variables;
+               CaseInsensitiveHashtable variables;
 
                //
                // Keeps track of constants
-               Hashtable constants;
+               CaseInsensitiveHashtable constants;
 
                //
                // Maps variable names to ILGenerator.LocalBuilders
                //
-               Hashtable local_builders;
+               CaseInsensitiveHashtable local_builders;
 
                bool used = false;
 
@@ -2463,7 +2732,7 @@ namespace Mono.CSharp {
                public bool AddLabel (string name, LabeledStatement target)
                {
                        if (labels == null)
-                               labels = new Hashtable ();
+                               labels = new CaseInsensitiveHashtable ();
                        if (labels.Contains (name))
                                return false;
                        
@@ -2511,7 +2780,7 @@ namespace Mono.CSharp {
                public void AddChildVariableName (string name)
                {
                        if (child_variable_names == null)
-                               child_variable_names = new Hashtable ();
+                               child_variable_names = new CaseInsensitiveHashtable ();
 
                        if (!child_variable_names.Contains (name))
                                child_variable_names.Add (name, true);
@@ -2562,7 +2831,7 @@ namespace Mono.CSharp {
                        this_variable = new VariableInfo (tc, ID, l);
 
                        if (variables == null)
-                               variables = new Hashtable ();
+                               variables = new CaseInsensitiveHashtable ();
                        variables.Add ("this", this_variable);
 
                        return this_variable;
@@ -2571,7 +2840,7 @@ namespace Mono.CSharp {
                public VariableInfo AddVariable (Expression type, string name, Parameters pars, Location l)
                {
                        if (variables == null)
-                               variables = new Hashtable ();
+                               variables = new CaseInsensitiveHashtable ();
 
                        VariableInfo vi = GetVariableInfo (name);
                        if (vi != null) {
@@ -2626,7 +2895,7 @@ namespace Mono.CSharp {
                                return false;
                        
                        if (constants == null)
-                               constants = new Hashtable ();
+                               constants = new CaseInsensitiveHashtable ();
 
                        constants.Add (name, value);
                        return true;
@@ -2821,7 +3090,7 @@ namespace Mono.CSharp {
                        // Process this block variables
                        //
                        if (variables != null){
-                               local_builders = new Hashtable ();
+                               local_builders = new CaseInsensitiveHashtable ();
                                
                                foreach (DictionaryEntry de in variables){
                                        string name = (string) de.Key;
@@ -2898,6 +3167,8 @@ namespace Mono.CSharp {
                                        b.UsageWarning ();
                }
 
+               bool has_ret = false;
+
                public override bool Resolve (EmitContext ec)
                {
                        Block prev_block = ec.CurrentBlock;
@@ -2906,17 +3177,40 @@ namespace Mono.CSharp {
                        ec.CurrentBlock = this;
                        ec.StartFlowBranching (this);
 
-                       Report.Debug (1, "RESOLVE BLOCK", StartLocation);
+                       Report.Debug (1, "RESOLVE BLOCK", StartLocation, ec.CurrentBranching);
 
                        if (!variables_initialized)
                                UpdateVariableInfo (ec);
 
-                       foreach (Statement s in statements){
-                               if (s.Resolve (ec) == false)
-                                       ok = false;
+                       ArrayList new_statements = new ArrayList ();
+                       bool unreachable = false, warning_shown = false;
+
+                       foreach (Statement s in statements){
+                               if (unreachable && !(s is LabeledStatement)) {
+                                       if (!warning_shown && !(s is EmptyStatement)) {
+                                               warning_shown = true;
+                                               Warning_DeadCodeFound (s.loc);
+                                       }
+
+                                       continue;
+                               }
+
+                               if (s.Resolve (ec) == false) {
+                                       ok = false;
+                                       continue;
+                               }
+
+                               if (s is LabeledStatement)
+                                       unreachable = false;
+                               else
+                                       unreachable = ! ec.CurrentBranching.IsReachable ();
+
+                               new_statements.Add (s);
                        }
 
-                       Report.Debug (1, "RESOLVE BLOCK DONE", StartLocation);
+                       statements = new_statements;
+
+                       Report.Debug (1, "RESOLVE BLOCK DONE", StartLocation, ec.CurrentBranching);
 
                        FlowReturns returns = ec.EndFlowBranching ();
                        ec.CurrentBlock = prev_block;
@@ -2934,47 +3228,28 @@ namespace Mono.CSharp {
                                                                "This label has not been referenced");
                        }
 
+                       if ((returns == FlowReturns.ALWAYS) ||
+                           (returns == FlowReturns.EXCEPTION) ||
+                           (returns == FlowReturns.UNREACHABLE))
+                               has_ret = true;
+
                        return ok;
                }
                
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
-                       bool is_ret = false, this_ret = false;
                        Block prev_block = ec.CurrentBlock;
-                       bool warning_shown = false;
 
                        ec.CurrentBlock = this;
 
-                       if (CodeGen.SymbolWriter != null) {
-                               ec.Mark (StartLocation);
+                       ec.Mark (StartLocation);
+                       foreach (Statement s in statements)
+                               s.Emit (ec);
                                
-                               foreach (Statement s in statements) {
-                                       ec.Mark (s.loc);
-                                       
-                                       if (is_ret && !warning_shown && !(s is EmptyStatement)){
-                                               warning_shown = true;
-                                               Warning_DeadCodeFound (s.loc);
-                                       }
-                                       this_ret = s.Emit (ec);
-                                       if (this_ret)
-                                               is_ret = true;
-                               }
-
-                               ec.Mark (EndLocation); 
-                       } else {
-                               foreach (Statement s in statements){
-                                       if (is_ret && !warning_shown && !(s is EmptyStatement)){
-                                               warning_shown = true;
-                                               Warning_DeadCodeFound (s.loc);
-                                       }
-                                       this_ret = s.Emit (ec);
-                                       if (this_ret)
-                                               is_ret = true;
-                               }
-                       }
+                       ec.Mark (EndLocation); 
                        
                        ec.CurrentBlock = prev_block;
-                       return is_ret;
+                       return has_ret;
                }
        }
 
@@ -3158,7 +3433,7 @@ namespace Mono.CSharp {
                                if (converted != null){
                                        Report.Error (-12, loc, "More than one conversion to an integral " +
                                                      " type exists for type `" +
-                                                     TypeManager.CSharpName (Expr.Type)+"'");
+                                                     TypeManager.MonoBASIC_Name (Expr.Type)+"'");
                                        return null;
                                } else
                                        converted = e;
@@ -3184,7 +3459,7 @@ namespace Mono.CSharp {
                {
                        Type compare_type;
                        bool error = false;
-                       Elements = new Hashtable ();
+                       Elements = new CaseInsensitiveHashtable ();
                                
                        got_default = false;
 
@@ -3580,7 +3855,8 @@ namespace Mono.CSharp {
                                                fFoundDefault = true;
                                        }
                                }
-                               fAllReturn &= ss.Block.Emit (ec);
+                               bool returns = ss.Block.Emit (ec);
+                               fAllReturn &= returns;
                                //ig.Emit (OpCodes.Br, lblEnd);
                        }
                        
@@ -3628,9 +3904,6 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Call, TypeManager.string_isinterneted_string);
                                ig.Emit (OpCodes.Stloc, val);
                        }
-
-                       SwitchSection last_section;
-                       last_section = (SwitchSection) Sections [Sections.Count-1];
                        
                        foreach (SwitchSection ss in Sections){
                                Label sec_begin = ig.DefineLabel ();
@@ -3683,7 +3956,7 @@ namespace Mono.CSharp {
                                                }
                                        }
                                }
-                               if (label_count != 1 && ss != last_section)
+                               if (label_count != 1)
                                        ig.Emit (OpCodes.Br, next_test);
                                
                                if (null_found)
@@ -3691,7 +3964,9 @@ namespace Mono.CSharp {
                                ig.MarkLabel (sec_begin);
                                foreach (SwitchLabel sl in ss.Labels)
                                        ig.MarkLabel (sl.ILLabelCode);
-                               if (ss.Block.Emit (ec))
+
+                               bool returns = ss.Block.Emit (ec);
+                               if (returns)
                                        pending_goto_end = false;
                                else {
                                        all_return = false;
@@ -3744,13 +4019,17 @@ namespace Mono.CSharp {
                                        return false;
                        }
 
+
+                       if (!got_default)
+                               ec.CurrentBranching.CreateSibling ();
+
                        ec.EndFlowBranching ();
                        ec.Switch = old_switch;
 
                        return true;
                }
                
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        // Store variable for comparission purposes
                        LocalBuilder value = ec.ig.DeclareLocal (SwitchType);
@@ -3807,7 +4086,7 @@ namespace Mono.CSharp {
                        return Statement.Resolve (ec) && expr != null;
                }
                
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        Type type = expr.Type;
                        bool val;
@@ -3815,7 +4094,7 @@ namespace Mono.CSharp {
                        if (type.IsValueType){
                                Report.Error (185, loc, "lock statement requires the expression to be " +
                                              " a reference type (type is: `" +
-                                             TypeManager.CSharpName (type) + "'");
+                                             TypeManager.MonoBASIC_Name (type) + "'");
                                return false;
                        }
 
@@ -3861,7 +4140,7 @@ namespace Mono.CSharp {
                        return Block.Resolve (ec);
                }
                
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        bool previous_state = ec.CheckState;
                        bool previous_state_const = ec.ConstantCheckState;
@@ -3899,7 +4178,7 @@ namespace Mono.CSharp {
                        return ret;
                }
 
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        bool previous_state = ec.CheckState;
                        bool previous_state_const = ec.ConstantCheckState;
@@ -3935,7 +4214,7 @@ namespace Mono.CSharp {
                        return val;
                }
                
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        bool previous_state = ec.InUnsafe;
                        bool val;
@@ -4085,7 +4364,7 @@ namespace Mono.CSharp {
                        return statement.Resolve (ec);
                }
                
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
 
@@ -4170,16 +4449,19 @@ namespace Mono.CSharp {
        public class Catch {
                public readonly string Name;
                public readonly Block  Block;
+               public Expression Clause;
                public readonly Location Location;
 
                Expression type_expr;
+               //Expression clus_expr;
                Type type;
                
-               public Catch (Expression type, string name, Block block, Location l)
+               public Catch (Expression type, string name, Block block, Expression clause, Location l)
                {
                        type_expr = type;
                        Name = name;
                        Block = block;
+                       Clause = clause;
                        Location = l;
                }
 
@@ -4203,7 +4485,7 @@ namespace Mono.CSharp {
                                        return false;
 
                                if (type != TypeManager.exception_type && !type.IsSubclassOf (TypeManager.exception_type)){
-                                       Report.Error (155, Location,
+                                       Report.Error (30665, Location,
                                                      "The type caught or thrown must be derived " +
                                                      "from System.Exception");
                                        return false;
@@ -4211,6 +4493,13 @@ namespace Mono.CSharp {
                        } else
                                type = null;
 
+                       if (Clause != null)     {
+                               Clause = Statement.ResolveBoolean (ec, Clause, Location);
+                               if (Clause == null) {
+                                       return false;
+                               }
+                       }
+
                        if (!Block.Resolve (ec))
                                return false;
 
@@ -4281,12 +4570,12 @@ namespace Mono.CSharp {
 
                                FlowBranching.UsageVector current = ec.CurrentBranching.CurrentUsageVector;
 
-                               if ((current.Returns == FlowReturns.NEVER) ||
-                                   (current.Returns == FlowReturns.SOMETIMES)) {
+                               if (!current.AlwaysReturns && !current.AlwaysBreaks)
                                        vector.AndLocals (current);
-                               }
                        }
 
+                       Report.Debug (1, "END OF CATCH BLOCKS", ec.CurrentBranching);
+
                        if (General != null){
                                ec.CurrentBranching.CreateSibling ();
                                Report.Debug (1, "STARTED SIBLING FOR GENERAL", ec.CurrentBranching);
@@ -4301,16 +4590,16 @@ namespace Mono.CSharp {
 
                                FlowBranching.UsageVector current = ec.CurrentBranching.CurrentUsageVector;
 
-                               if ((current.Returns == FlowReturns.NEVER) ||
-                                   (current.Returns == FlowReturns.SOMETIMES)) {
+                               if (!current.AlwaysReturns && !current.AlwaysBreaks)
                                        vector.AndLocals (current);
-                               }
                        }
 
-                       ec.CurrentBranching.CreateSiblingForFinally ();
-                       Report.Debug (1, "STARTED SIBLING FOR FINALLY", ec.CurrentBranching, vector);
+                       Report.Debug (1, "END OF GENERAL CATCH BLOCKS", ec.CurrentBranching);
 
                        if (Fini != null) {
+                               ec.CurrentBranching.CreateSiblingForFinally ();
+                               Report.Debug (1, "STARTED SIBLING FOR FINALLY", ec.CurrentBranching, vector);
+
                                bool old_in_finally = ec.InFinally;
                                ec.InFinally = true;
 
@@ -4320,10 +4609,10 @@ namespace Mono.CSharp {
                                ec.InFinally = old_in_finally;
                        }
 
-                       FlowBranching.UsageVector f_vector = ec.CurrentBranching.CurrentUsageVector;
-
                        FlowReturns returns = ec.EndFlowBranching ();
 
+                       FlowBranching.UsageVector f_vector = ec.CurrentBranching.CurrentUsageVector;
+
                        Report.Debug (1, "END OF FINALLY", ec.CurrentBranching, returns, vector, f_vector);
 
                        if ((returns == FlowReturns.SOMETIMES) || (returns == FlowReturns.ALWAYS)) {
@@ -4337,7 +4626,7 @@ namespace Mono.CSharp {
                        return ok;
                }
                
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
                        Label end;
@@ -4372,17 +4661,47 @@ namespace Mono.CSharp {
                                        ig.Emit (OpCodes.Stloc, vi.LocalBuilder);
                                } else
                                        ig.Emit (OpCodes.Pop);
-                               
-                               if (!c.Block.Emit (ec))
-                                       returns = false;
+
+                               //
+                               // if when clause is there
+                               //
+                               if (c.Clause != null) {
+                                       if (c.Clause is BoolConstant) {
+                                               bool take = ((BoolConstant) c.Clause).Value;
+
+                                               if (take) 
+                                                       if (!c.Block.Emit (ec))
+                                                               returns = false;
+                                       } else {
+                                               EmitBoolExpression (ec, c.Clause, finish, false);
+                                               if (!c.Block.Emit (ec))
+                                                       returns = false;
+                                       }
+                               } else 
+                                       if (!c.Block.Emit (ec))
+                                               returns = false;
                        }
 
                        if (General != null){
                                ig.BeginCatchBlock (TypeManager.object_type);
                                ig.Emit (OpCodes.Pop);
-                               if (!General.Block.Emit (ec))
-                                       returns = false;
+
+                               if (General.Clause != null) {
+                                       if (General.Clause is BoolConstant) {
+                                               bool take = ((BoolConstant) General.Clause).Value;
+                                               if (take) 
+                                                       if (!General.Block.Emit (ec))
+                                                               returns = false;
+                                       } else {
+                                               EmitBoolExpression (ec, General.Clause, finish, false);
+                                               if (!General.Block.Emit (ec))
+                                                       returns = false;
+                                       }
+                               } else 
+                                       if (!General.Block.Emit (ec))
+                                               returns = false;
                        }
+
                        ec.InCatch = old_in_catch;
 
                        ig.MarkLabel (finish);
@@ -4413,10 +4732,6 @@ namespace Mono.CSharp {
                }
        }
 
-       //
-       // FIXME: We still do not support the expression variant of the using
-       // statement.
-       //
        public class Using : Statement {
                object expression_or_block;
                Statement Statement;
@@ -4460,7 +4775,7 @@ namespace Mono.CSharp {
                                        if (var == null)
                                                return false;
                                        
-                                       converted_vars [i] = Expression.ConvertImplicit (
+                                       converted_vars [i] = Expression.ConvertImplicitRequired (
                                                ec, var, TypeManager.idisposable_type, loc);
 
                                        if (converted_vars [i] == null)
@@ -4493,7 +4808,7 @@ namespace Mono.CSharp {
                bool ResolveExpression (EmitContext ec)
                {
                        if (!TypeManager.ImplementsInterface (expr_type, TypeManager.idisposable_type)){
-                               conv = Expression.ConvertImplicit (
+                               conv = Expression.ConvertImplicitRequired (
                                        ec, expr, TypeManager.idisposable_type, loc);
 
                                if (conv == null)
@@ -4601,7 +4916,7 @@ namespace Mono.CSharp {
                        return Statement.Resolve (ec);
                }
                
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        if (expression_or_block is DictionaryEntry)
                                return EmitLocalVariableDecls (ec);
@@ -4628,7 +4943,14 @@ namespace Mono.CSharp {
                public Foreach (Expression type, LocalVariableReference var, Expression expr,
                                Statement stmt, Location l)
                {
-                       this.type = type;
+                       if (type != null) {
+                               this.type = type;
+                       }
+                       else
+                       {
+                               VariableInfo vi = var.VariableInfo;
+                               this.type = vi.Type;
+                       }
                        this.variable = var;
                        this.expr = expr;
                        statement = stmt;
@@ -4636,7 +4958,7 @@ namespace Mono.CSharp {
                }
                
                public override bool Resolve (EmitContext ec)
-               {
+               {       
                        expr = expr.Resolve (ec);
                        if (expr == null)
                                return false;
@@ -4653,7 +4975,7 @@ namespace Mono.CSharp {
                        // out to return values in ExprClass?  I think they should.
                        //
                        if (!(expr.eclass == ExprClass.Variable || expr.eclass == ExprClass.Value ||
-                             expr.eclass == ExprClass.PropertyAccess)){
+                             expr.eclass == ExprClass.PropertyAccess || expr.eclass == ExprClass.IndexerAccess)){
                                error1579 (expr.Type);
                                return false;
                        }
@@ -4676,6 +4998,10 @@ namespace Mono.CSharp {
                                empty = new EmptyExpression (hm.element_type);
                        }
 
+                       ec.StartFlowBranching (FlowBranchingType.LOOP_BLOCK, loc);
+                       ec.CurrentBranching.CreateSibling ();
+
+                       //
                        //
                        // FIXME: maybe we can apply the same trick we do in the
                        // array handling to avoid creating empty and conv in some cases.
@@ -4683,16 +5009,18 @@ namespace Mono.CSharp {
                        // Although it is not as important in this case, as the type
                        // will not likely be object (what the enumerator will return).
                        //
-                       conv = Expression.ConvertExplicit (ec, empty, var_type, loc);
+                       conv = Expression.ConvertExplicit (ec, empty, var_type, false, loc);
                        if (conv == null)
                                return false;
 
                        if (variable.ResolveLValue (ec, empty) == null)
                                return false;
-
+                       
                        if (!statement.Resolve (ec))
                                return false;
 
+                       FlowReturns returns = ec.EndFlowBranching ();
+
                        return true;
                }
                
@@ -5134,7 +5462,7 @@ namespace Mono.CSharp {
                        return false;
                }
                
-               public override bool Emit (EmitContext ec)
+               protected override bool DoEmit (EmitContext ec)
                {
                        bool ret_val;
                        
@@ -5161,5 +5489,264 @@ namespace Mono.CSharp {
                        return ret_val;
                }
        }
-}
+       
+       /// <summary>
+       ///   AddHandler statement
+       /// </summary>
+       public class AddHandler : Statement {
+               Expression EvtId;
+               Expression EvtHandler;
+
+               //
+               // keeps track whether EvtId is already resolved
+               //
+               bool resolved;
+
+               public AddHandler (Expression evt_id, Expression evt_handler, Location l)
+               {
+                       EvtId = evt_id;
+                       EvtHandler = evt_handler;
+                       loc = l;
+                       resolved = false;
+                       //Console.WriteLine ("Adding handler '" + evt_handler + "' for Event '" + evt_id +"'");
+               }
+
+               public override bool Resolve (EmitContext ec)
+               {
+                       //
+                       // if EvetId is of EventExpr type that means
+                       // this is already resolved 
+                       //
+                       if (EvtId is EventExpr) {
+                               resolved = true;
+                               return true;
+                       }
+
+                       EvtId = EvtId.Resolve(ec);
+                       EvtHandler = EvtHandler.Resolve(ec,ResolveFlags.MethodGroup);
+                       if (EvtId == null || (!(EvtId is EventExpr))) {
+                               Report.Error (30676, "Need an event designator.");
+                               return false;
+                       }
 
+                       if (EvtHandler == null) 
+                       {
+                               Report.Error (999, "'AddHandler' statement needs an event handler.");
+                               return false;
+                       }
+
+                       return true;
+               }
+
+               protected override bool DoEmit (EmitContext ec)
+               {
+                       //
+                       // Already resolved and emitted don't do anything
+                       //
+                       if (resolved)
+                               return true;
+
+                       Expression e, d;
+                       ArrayList args = new ArrayList();
+                       Argument arg = new Argument (EvtHandler, Argument.AType.Expression);
+                       args.Add (arg);
+                       
+                       
+
+                       // The even type was already resolved to a delegate, so
+                       // we must un-resolve its name to generate a type expression
+                       string ts = (EvtId.Type.ToString()).Replace ('+','.');
+                       Expression dtype = Mono.MonoBASIC.Parser.DecomposeQI (ts, Location.Null);
+
+                       // which we can use to declare a new event handler
+                       // of the same type
+                       d = new New (dtype, args, Location.Null);
+                       d = d.Resolve(ec);
+                       e = new CompoundAssign(Binary.Operator.Addition, EvtId, d, Location.Null);
+
+                       // we resolve it all and emit the code
+                       e = e.Resolve(ec);
+                       if (e != null) 
+                       {
+                               e.Emit(ec);
+                               return true;
+                       }
+
+                       return false;
+               }
+       }
+
+       /// <summary>
+       ///   RemoveHandler statement
+       /// </summary>
+       public class RemoveHandler : Statement \r
+       {
+               Expression EvtId;
+               Expression EvtHandler;
+
+               public RemoveHandler (Expression evt_id, Expression evt_handler, Location l)
+               {
+                       EvtId = evt_id;
+                       EvtHandler = evt_handler;
+                       loc = l;
+               }
+
+               public override bool Resolve (EmitContext ec)
+               {
+                       EvtId = EvtId.Resolve(ec);
+                       EvtHandler = EvtHandler.Resolve(ec,ResolveFlags.MethodGroup);
+                       if (EvtId == null || (!(EvtId is EventExpr))) \r
+                       {
+                               Report.Error (30676, "Need an event designator.");
+                               return false;
+                       }
+
+                       if (EvtHandler == null) 
+                       {
+                               Report.Error (999, "'AddHandler' statement needs an event handler.");
+                               return false;
+                       }
+                       return true;
+               }
+
+               protected override bool DoEmit (EmitContext ec)
+               {
+                       Expression e, d;
+                       ArrayList args = new ArrayList();
+                       Argument arg = new Argument (EvtHandler, Argument.AType.Expression);
+                       args.Add (arg);
+                       
+                       // The even type was already resolved to a delegate, so
+                       // we must un-resolve its name to generate a type expression
+                       string ts = (EvtId.Type.ToString()).Replace ('+','.');
+                       Expression dtype = Mono.MonoBASIC.Parser.DecomposeQI (ts, Location.Null);
+
+                       // which we can use to declare a new event handler
+                       // of the same type
+                       d = new New (dtype, args, Location.Null);
+                       d = d.Resolve(ec);
+                       // detach the event
+                       e = new CompoundAssign(Binary.Operator.Subtraction, EvtId, d, Location.Null);
+
+                       // we resolve it all and emit the code
+                       e = e.Resolve(ec);
+                       if (e != null) 
+                       {
+                               e.Emit(ec);
+                               return true;
+                       }
+
+                       return false;
+               }
+       }
+
+       public class RedimClause {
+               public Expression Expr;
+               public ArrayList NewIndexes;
+               
+               public RedimClause (Expression e, ArrayList args)
+               {
+                       Expr = e;
+                       NewIndexes = args;
+               }
+       }
+
+       public class ReDim : Statement {
+               ArrayList RedimTargets;
+               Type BaseType;
+               bool Preserve;
+
+               private StatementExpression ReDimExpr;
+
+               public ReDim (ArrayList targets, bool opt_preserve, Location l)
+               {
+                       loc = l;
+                       RedimTargets = targets;
+                       Preserve = opt_preserve;
+               }
+
+               public override bool Resolve (EmitContext ec)
+               {
+                       Expression RedimTarget;
+                       ArrayList NewIndexes;
+
+                       foreach (RedimClause rc in RedimTargets) {
+                               RedimTarget = rc.Expr;
+                               NewIndexes = rc.NewIndexes;
+
+                               RedimTarget = RedimTarget.Resolve (ec);
+                               if (!RedimTarget.Type.IsArray)
+                                       Report.Error (49, "'ReDim' statement requires an array");
+
+                               ArrayList args = new ArrayList();
+                               foreach (Argument a in NewIndexes) {
+                                       if (a.Resolve(ec, loc))
+                                               args.Add (a.Expr);
+                               }
+
+                               for (int x = 0; x < args.Count; x++) {
+                                       args[x] = new Binary (Binary.Operator.Addition,
+                                                               (Expression) args[x], new IntLiteral (1), Location.Null);       
+                               }
+
+                               NewIndexes = args;
+                               if (RedimTarget.Type.GetArrayRank() != args.Count)
+                                       Report.Error (415, "'ReDim' cannot change the number of dimensions of an array.");
+
+                               BaseType = RedimTarget.Type.GetElementType();
+                               Expression BaseTypeExpr = MonoBASIC.Parser.DecomposeQI(BaseType.FullName.ToString(), Location.Null);
+                               ArrayCreation acExpr = new ArrayCreation (BaseTypeExpr, NewIndexes, "", null, Location.Null);   
+                               // TODO: we are in a foreach we probably can't reuse ReDimExpr, must turn it into an array(list)
+                               if (Preserve)
+                               {
+                                       ExpressionStatement PreserveExpr = (ExpressionStatement) new Preserve(RedimTarget, acExpr, loc);
+                                       ReDimExpr = (StatementExpression) new StatementExpression ((ExpressionStatement) new Assign (RedimTarget, PreserveExpr, loc), loc);
+                               }
+                               else
+                                       ReDimExpr = (StatementExpression) new StatementExpression ((ExpressionStatement) new Assign (RedimTarget, acExpr, loc), loc);
+                               ReDimExpr.Resolve(ec);
+                       }
+                       return true;
+               }
+                               
+               protected override bool DoEmit (EmitContext ec)
+               {
+                       ReDimExpr.Emit(ec);
+                       return false;
+               }               
+               
+       }
+       
+       public class Erase : Statement {
+               Expression EraseTarget;
+               
+               private StatementExpression EraseExpr;
+               
+               public Erase (Expression expr, Location l)
+               {
+                       loc = l;
+                       EraseTarget = expr;
+               }
+               
+               public override bool Resolve (EmitContext ec)
+               {
+                       EraseTarget = EraseTarget.Resolve (ec);
+                       if (!EraseTarget.Type.IsArray) 
+                               Report.Error (49, "'Erase' statement requires an array");
+
+                       EraseExpr = (StatementExpression) new StatementExpression ((ExpressionStatement) new Assign (EraseTarget, NullLiteral.Null, loc), loc);
+                       EraseExpr.Resolve(ec);
+                       
+                       return true;
+               }
+                               
+               protected override bool DoEmit (EmitContext ec)
+               {
+                       EraseExpr.Emit(ec);
+                       return false;
+               }               
+               
+       }
+       
+       
+}