oops.
[mono.git] / mcs / mcs / statement.cs
old mode 100755 (executable)
new mode 100644 (file)
index 2d5370d..e050e8f
@@ -3,9 +3,10 @@
 //
 // Author:
 //   Miguel de Icaza (miguel@ximian.com)
-//   Martin Baulig (martin@gnome.org)
+//   Martin Baulig (martin@ximian.com)
 //
 // (C) 2001, 2002, 2003 Ximian, Inc.
+// (C) 2003, 2004 Novell, Inc.
 //
 
 using System;
@@ -45,16 +46,14 @@ namespace Mono.CSharp {
                        // in unreachable code, for instance.
                        //
 
+                       if (warn && (RootContext.WarningLevel >= 2))
+                               Report.Warning (162, loc, "Unreachable code detected");
+
                        ec.StartFlowBranching (FlowBranching.BranchingType.Block, loc);
                        bool ok = Resolve (ec);
                        ec.KillFlowBranching ();
 
-                       if (!ok)
-                               return false;
-
-                       if (warn && (RootContext.WarningLevel >= 2))
-                               Report.Warning (162, loc, "Unreachable code detected");
-                       return true;
+                       return ok;
                }
                
                protected void CheckObsolete (Type type)
@@ -147,6 +146,11 @@ namespace Mono.CSharp {
                                return false;
                        }
 
+                       Assign ass = expr as Assign;
+                       if (ass != null && ass.Source is Constant) {
+                               Report.Warning (665, 3, loc, "Assignment in conditional expression is always constant; did you mean to use == instead of = ?");
+                       }
+
                        //
                        // Dead code elimination
                        //
@@ -559,6 +563,11 @@ namespace Mono.CSharp {
                {
                        if (ec.ReturnType == null){
                                if (Expr != null){
+                                       if (ec.CurrentAnonymousMethod != null){
+                                               Report.Error (1662, loc, String.Format (
+                                                       "Anonymous method could not be converted to delegate " +
+                                                       "since the return value does not match the delegate value"));
+                                       }
                                        Error (127, "Return with a value not allowed here");
                                        return false;
                                }
@@ -598,6 +607,9 @@ namespace Mono.CSharp {
                        } else
                                vector.CheckOutParameters (ec.CurrentBranching);
 
+                       if (in_exc)
+                               ec.NeedReturnLabel ();
+
                        ec.CurrentBranching.CurrentUsageVector.Return ();
                        return true;
                }
@@ -611,12 +623,10 @@ namespace Mono.CSharp {
                                        ec.ig.Emit (OpCodes.Stloc, ec.TemporaryReturn ());
                        }
 
-                       if (in_exc) {
-                               ec.NeedReturnLabel ();
+                       if (in_exc)
                                ec.ig.Emit (OpCodes.Leave, ec.ReturnLabel);
-                       } else {
+                       else
                                ec.ig.Emit (OpCodes.Ret);
-                       }
                }
        }
 
@@ -665,6 +675,7 @@ namespace Mono.CSharp {
                bool defined;
                bool referenced;
                Label label;
+               ILGenerator ig;
 
                FlowBranching.UsageVector vectors;
                
@@ -677,6 +688,7 @@ namespace Mono.CSharp {
                {
                        if (defined)
                                return label;
+                       ig = ec.ig;
                        label = ec.ig.DefineLabel ();
                        defined = true;
 
@@ -713,6 +725,10 @@ namespace Mono.CSharp {
 
                protected override void DoEmit (EmitContext ec)
                {
+                       if (ig != null && ig != ec.ig) {
+                               Report.Error (1632, "Control cannot leave body of anonymous method");
+                               return;
+                       }
                        LabelTarget (ec);
                        ec.ig.MarkLabel (label);
                }
@@ -755,7 +771,7 @@ namespace Mono.CSharp {
        /// </summary>
        public class GotoCase : Statement {
                Expression expr;
-               Label label;
+               SwitchLabel sl;
                
                public GotoCase (Expression e, Location l)
                {
@@ -785,7 +801,7 @@ namespace Mono.CSharp {
                        if (val == null)
                                return false;
                                        
-                       SwitchLabel sl = (SwitchLabel) ec.Switch.Elements [val];
+                       sl = (SwitchLabel) ec.Switch.Elements [val];
 
                        if (sl == null){
                                Report.Error (
@@ -794,15 +810,13 @@ namespace Mono.CSharp {
                                return false;
                        }
 
-                       label = sl.ILLabelCode;
-
                        ec.CurrentBranching.CurrentUsageVector.Goto ();
                        return true;
                }
 
                protected override void DoEmit (EmitContext ec)
                {
-                       ec.ig.Emit (OpCodes.Br, label);
+                       ec.ig.Emit (OpCodes.Br, sl.GetILLabelCode (ec));
                }
        }
        
@@ -817,7 +831,6 @@ namespace Mono.CSharp {
 
                public override bool Resolve (EmitContext ec)
                {
-                       bool in_catch = ec.CurrentBranching.InCatch ();
                        ec.CurrentBranching.CurrentUsageVector.Throw ();
 
                        if (expr != null){
@@ -828,7 +841,7 @@ namespace Mono.CSharp {
                                ExprClass eclass = expr.eclass;
 
                                if (!(eclass == ExprClass.Variable || eclass == ExprClass.PropertyAccess ||
-                                     eclass == ExprClass.Value || eclass == ExprClass.IndexerAccess)) {
+                                       eclass == ExprClass.Value || eclass == ExprClass.IndexerAccess)) {
                                        expr.Error_UnexpectedKind ("value, variable, property or indexer access ", loc);
                                        return false;
                                }
@@ -836,20 +849,25 @@ namespace Mono.CSharp {
                                Type t = expr.Type;
                                
                                if ((t != TypeManager.exception_type) &&
-                                   !t.IsSubclassOf (TypeManager.exception_type) &&
-                                   !(expr is NullLiteral)) {
+                                       !t.IsSubclassOf (TypeManager.exception_type) &&
+                                       !(expr is NullLiteral)) {
                                        Error (155,
-                                              "The type caught or thrown must be derived " +
-                                              "from System.Exception");
+                                               "The type caught or thrown must be derived " +
+                                               "from System.Exception");
                                        return false;
                                }
-                       } else if (!in_catch) {
-                               Error (156,
-                                      "A throw statement with no argument is only " +
-                                      "allowed in a catch clause");
+                               return true;
+                       }
+
+                       if (ec.CurrentBranching.InFinally (true)) {
+                               Error (724, "A throw statement with no argument is only allowed in a catch clause nested inside of the innermost catch clause");
                                return false;
                        }
 
+                       if (!ec.CurrentBranching.InCatch ()) {
+                               Error (156, "A throw statement with no argument is only allowed in a catch clause");
+                               return false;
+                       }
                        return true;
                }
                        
@@ -891,6 +909,9 @@ namespace Mono.CSharp {
 
                        crossing_exc = ec.CurrentBranching.BreakCrossesTryCatchBoundary ();
 
+                       if (!crossing_exc)
+                               ec.NeedReturnLabel ();
+
                        ec.CurrentBranching.CurrentUsageVector.Break ();
                        return true;
                }
@@ -902,7 +923,6 @@ namespace Mono.CSharp {
                        if (crossing_exc)
                                ig.Emit (OpCodes.Leave, ec.LoopEnd);
                        else {
-                               ec.NeedReturnLabel ();
                                ig.Emit (OpCodes.Br, ec.LoopEnd);
                        }
                }
@@ -945,13 +965,17 @@ namespace Mono.CSharp {
                }
        }
 
+       //
+       // The information about a user-perceived local variable
+       //
        public class LocalInfo {
                public Expression Type;
 
                //
                // Most of the time a variable will be stored in a LocalBuilder
                //
-               // But sometimes, it will be stored in a field.  The context of the field will
+               // But sometimes, it will be stored in a field (variables that have been
+               // hoisted by iterators or by anonymous methods).  The context of the field will
                // be stored in the EmitContext
                //
                //
@@ -969,7 +993,9 @@ namespace Mono.CSharp {
                        Used = 1,
                        ReadOnly = 2,
                        Pinned = 4,
-                       IsThis = 8      
+                       IsThis = 8,
+                       Captured = 16,
+                       AddressTaken = 32
                }
 
                Flags flags;
@@ -1011,12 +1037,11 @@ namespace Mono.CSharp {
                public bool Resolve (EmitContext ec)
                {
                        if (VariableType == null) {
-                               Type = Type.ResolveAsTypeTerminal (ec, false);
-
-                               if (Type == null)
+                               TypeExpr texpr = Type.ResolveAsTypeTerminal (ec, false);
+                               if (texpr == null)
                                        return false;
                                
-                               VariableType = Type.Type;
+                               VariableType = texpr.ResolveType (ec);
                        }
 
                        if (VariableType == TypeManager.void_type) {
@@ -1048,6 +1073,26 @@ namespace Mono.CSharp {
                        }
                }
 
+               public bool IsCaptured {
+                       get {
+                               return (flags & Flags.Captured) != 0;
+                       }
+
+                       set {
+                               flags |= Flags.Captured;
+                       }
+               }
+
+               public bool AddressTaken {
+                       get {
+                               return (flags & Flags.AddressTaken) != 0;
+                       }
+
+                       set {
+                               flags |= Flags.AddressTaken;
+                       }
+               }
+
                public override string ToString ()
                {
                        return String.Format ("LocalInfo ({0},{1},{2},{3})",
@@ -1059,7 +1104,7 @@ namespace Mono.CSharp {
                                return (flags & Flags.Used) != 0;
                        }
                        set {
-                               flags = value ? (flags | Flags.Used) : (flags & ~Flags.Used);
+                               flags = value ? (flags | Flags.Used) : (unchecked (flags & ~Flags.Used));
                        }
                }
 
@@ -1068,7 +1113,7 @@ namespace Mono.CSharp {
                                return (flags & Flags.ReadOnly) != 0;
                        }
                        set {
-                               flags = value ? (flags | Flags.ReadOnly) : (flags & ~Flags.ReadOnly);
+                               flags = value ? (flags | Flags.ReadOnly) : (unchecked (flags & ~Flags.ReadOnly));
                        }
                }
 
@@ -1110,12 +1155,12 @@ namespace Mono.CSharp {
        ///   they contain extra information that is not necessary on normal blocks.
        /// </remarks>
        public class Block : Statement {
-               public readonly Block     Parent;
+               public Block    Parent;
                public readonly Location  StartLocation;
-               public Location           EndLocation = Location.Null;
+               public Location EndLocation = Location.Null;
 
                [Flags]
-               public enum Flags : byte {
+               public enum Flags {
                        Implicit  = 1,
                        Unchecked = 2,
                        BlockUsed = 4,
@@ -1123,7 +1168,8 @@ namespace Mono.CSharp {
                        HasRet = 16,
                        IsDestructor = 32,
                        HasVarargs = 64,
-                       Unsafe = 128,
+                       IsToplevel = 128,
+                       Unsafe = 256
                }
                Flags flags;
 
@@ -1192,12 +1238,17 @@ namespace Mono.CSharp {
                // Keeps track of constants
                Hashtable constants;
 
+               //
+               // The parameters for the block, this is only needed on the toplevel block really
+               // TODO: move `parameters' into ToplevelBlock
+               Parameters parameters;
+               
                //
                // If this is a switch section, the enclosing switch block.
                //
                Block switch_block;
 
-               static int id;
+               protected static int id;
 
                int this_id;
                
@@ -1382,12 +1433,12 @@ namespace Mono.CSharp {
                // </summary>
                public LocalInfo ThisVariable {
                        get {
-                               if (this_variable != null)
-                                       return this_variable;
-                               else if (Parent != null)
-                                       return Parent.ThisVariable;
-                               else
-                                       return null;
+                               for (Block b = this; b != null; b = b.Parent) {
+                                       if (b.this_variable != null)
+                                               return b.this_variable;
+                               }
+                               
+                               return null;
                        }
                }
 
@@ -1403,8 +1454,7 @@ namespace Mono.CSharp {
                        if (child_variable_names == null)
                                child_variable_names = new Hashtable ();
 
-                       if (!child_variable_names.Contains (name))
-                               child_variable_names.Add (name, true);
+                       child_variable_names [name] = null;
                }
 
                // <summary>
@@ -1565,17 +1615,79 @@ namespace Mono.CSharp {
                        
                        return e != null;
                }
-               
-               Parameters parameters = null;
-               public Parameters Parameters {
-                       get {
-                               Block b = this;
-                               while (b.Parent != null)
-                                       b = b.Parent;
-                               return b.parameters;
-                       }
+
+               //
+               // Returns a `ParameterReference' for the given name, or null if there
+               // is no such parameter
+               //
+               public ParameterReference GetParameterReference (string name, Location loc)
+               {
+                       Block b = this;
+
+                       do {
+                               Parameters pars = b.parameters;
+                               
+                               if (pars != null){
+                                       Parameter par;
+                                       int idx;
+                                       
+                                       par = pars.GetParameterByName (name, out idx);
+                                       if (par != null){
+                                               ParameterReference pr;
+
+                                               pr = new ParameterReference (pars, this, idx, name, loc);
+                                               return pr;
+                                       }
+                               }
+                               b = b.Parent;
+                       } while (b != null);
+                       return null;
                }
 
+               //
+               // Whether the parameter named `name' is local to this block, 
+               // or false, if the parameter belongs to an encompassing block.
+               //
+               public bool IsLocalParameter (string name)
+               {
+                       Block b = this;
+                       int toplevel_count = 0;
+
+                       do {
+                               if (this is ToplevelBlock)
+                                       toplevel_count++;
+
+                               Parameters pars = b.parameters;
+                               if (pars != null){
+                                       if (pars.GetParameterByName (name) != null)
+                                               return true;
+                                       return false;
+                               }
+                               if (toplevel_count > 0)
+                                       return false;
+                               b = b.Parent;
+                       } while (b != null);
+                       return false;
+               }
+               
+               //
+               // Whether the `name' is a parameter reference
+               //
+               public bool IsParameterReference (string name)
+               {
+                       Block b = this;
+
+                       do {
+                               Parameters pars = b.parameters;
+                               
+                               if (pars != null)
+                                       if (pars.GetParameterByName (name) != null)
+                                               return true;
+                               b = b.Parent;
+                       } while (b != null);
+                       return false;
+               }
+               
                /// <returns>
                ///   A list of labels that were not used within this block
                /// </returns>
@@ -1624,7 +1736,7 @@ namespace Mono.CSharp {
                public VariableMap ParameterMap {
                        get {
                                if ((flags & Flags.VariablesInitialized) == 0)
-                                       throw new Exception ();
+                                       throw new Exception ("Variables have not been initialized yet");
 
                                return param_map;
                        }
@@ -1633,17 +1745,12 @@ namespace Mono.CSharp {
                public VariableMap LocalMap {
                        get {
                                if ((flags & Flags.VariablesInitialized) == 0)
-                                       throw new Exception ();
+                                       throw new Exception ("Variables have not been initialized yet");
 
                                return local_map;
                        }
                }
 
-               public bool LiftVariable (LocalInfo local_info)
-               {
-                       return false;
-               }
-               
                /// <summary>
                ///   Emits the variable declarations and labels.
                /// </summary>
@@ -1651,10 +1758,8 @@ namespace Mono.CSharp {
                ///   tc: is our typecontainer (to resolve type references)
                ///   ig: is the code generator:
                /// </remarks>
-               public void EmitMeta (EmitContext ec, InternalParameters ip)
+               public void ResolveMeta (ToplevelBlock toplevel, EmitContext ec, InternalParameters ip)
                {
-                       ILGenerator ig = ec.ig;
-
                        bool old_unsafe = ec.InUnsafe;
 
                        // If some parent block was unsafe, we remain unsafe even if this block
@@ -1688,8 +1793,7 @@ namespace Mono.CSharp {
 
                        bool old_check_state = ec.ConstantCheckState;
                        ec.ConstantCheckState = (flags & Flags.Unchecked) == 0;
-                       bool remap_locals = ec.RemapToProxy;
-                               
+                       
                        //
                        // Process this block variables
                        //
@@ -1714,6 +1818,7 @@ namespace Mono.CSharp {
                                                        continue;
                                        }
 
+#if false
                                        if (remap_locals)
                                                vi.FieldBuilder = ec.MapVariable (name, vi.VariableType);
                                        else if (vi.Pinned)
@@ -1724,6 +1829,7 @@ namespace Mono.CSharp {
                                                vi.LocalBuilder = TypeManager.DeclareLocalPinned (ig, vi.VariableType);
                                        else if (!vi.IsThis)
                                                vi.LocalBuilder = ig.DeclareLocal (vi.VariableType);
+#endif
 
                                        if (constants == null)
                                                continue;
@@ -1762,12 +1868,49 @@ namespace Mono.CSharp {
                        //
                        if (children != null){
                                foreach (Block b in children)
-                                       b.EmitMeta (ec, ip);
+                                       b.ResolveMeta (toplevel, ec, ip);
                        }
-
                        ec.InUnsafe = old_unsafe;
                }
 
+               //
+               // Emits the local variable declarations for a block
+               //
+               public void EmitMeta (EmitContext ec)
+               {
+                       ILGenerator ig = ec.ig;
+                       
+                       if (variables != null){
+                               bool have_captured_vars = ec.HaveCapturedVariables ();
+                               bool remap_locals = ec.RemapToProxy;
+                               
+                               foreach (DictionaryEntry de in variables){
+                                       LocalInfo vi = (LocalInfo) de.Value;
+
+                                       if (have_captured_vars && ec.IsCaptured (vi))
+                                               continue;
+
+                                       if (remap_locals){
+                                               vi.FieldBuilder = ec.MapVariable (vi.Name, vi.VariableType);
+                                       } else {
+                                               if (vi.Pinned)
+                                                       //
+                                                       // This is needed to compile on both .NET 1.x and .NET 2.x
+                                                       // the later introduced `DeclareLocal (Type t, bool pinned)'
+                                                       //
+                                                       vi.LocalBuilder = TypeManager.DeclareLocalPinned (ig, vi.VariableType);
+                                               else if (!vi.IsThis)
+                                                       vi.LocalBuilder = ig.DeclareLocal (vi.VariableType);
+                                       }
+                               }
+                       }
+
+                       if (children != null){
+                               foreach (Block b in children)
+                                       b.EmitMeta (ec);
+                       }
+               }
+
                void UsageWarning (FlowBranching.UsageVector vector)
                {
                        string name;
@@ -1935,28 +2078,126 @@ namespace Mono.CSharp {
 
                        ec.CurrentBlock = prev_block;
                }
+
+               public ToplevelBlock Toplevel {
+                       get {
+                               Block b = this;
+                               while (b.Parent != null){
+                                       if ((b.flags & Flags.IsToplevel) != 0)
+                                               break;
+                                       b = b.Parent;
+                               }
+
+                               return (ToplevelBlock) b;
+                       }
+               }
+
+               //
+               // Returns true if we ar ea child of `b'.
+               //
+               public bool IsChildOf (Block b)
+               {
+                       Block current = this;
+                       
+                       do {
+                               if (current.Parent == b)
+                                       return true;
+                               current = current.Parent;
+                       } while (current != null);
+                       return false;
+               }
        }
 
        //
+       // A toplevel block contains extra information, the split is done
+       // only to separate information that would otherwise bloat the more
+       // lightweight Block.
+       //
+       // In particular, this was introduced when the support for Anonymous
+       // Methods was implemented. 
        // 
        public class ToplevelBlock : Block {
+               //
+               // Pointer to the host of this anonymous method, or null
+               // if we are the topmost block
+               //
+               public ToplevelBlock Container;
+               CaptureContext capture_context;
+
+               Hashtable capture_contexts;
+
+               static int did = 0;
+               
+                       
+               public void RegisterCaptureContext (CaptureContext cc)
+               {
+                       if (capture_contexts == null)
+                               capture_contexts = new Hashtable ();
+                       capture_contexts [cc] = cc;
+               }
+
+               public void CompleteContexts ()
+               {
+                       if (capture_contexts == null)
+                               return;
+
+                       foreach (CaptureContext cc in capture_contexts.Keys){
+                               cc.AdjustScopes ();
+                       }
+               }
+               
+               public CaptureContext ToplevelBlockCaptureContext {
+                       get {
+                               return capture_context;
+                       }
+               }
+               
+               //
+               // Parent is only used by anonymous blocks to link back to their
+               // parents
+               //
+               public ToplevelBlock (ToplevelBlock container, Parameters parameters, Location start) :
+                       base (null, Flags.IsToplevel, parameters, start, Location.Null)
+               {
+                       Container = container;
+               }
+               
                public ToplevelBlock (Parameters parameters, Location start) :
-                       base (null, parameters, start, Location.Null)
+                       base (null, Flags.IsToplevel, parameters, start, Location.Null)
                {
                }
 
                public ToplevelBlock (Flags flags, Parameters parameters, Location start) :
-                       base (null, flags, parameters, start, Location.Null)
+                       base (null, flags | Flags.IsToplevel, parameters, start, Location.Null)
                {
                }
+
+               public ToplevelBlock (Location loc) : base (null, Flags.IsToplevel, loc, loc)
+               {
+               }
+
+               public void SetHaveAnonymousMethods (Location loc, AnonymousMethod host)
+               {
+                       if (capture_context == null)
+                               capture_context = new CaptureContext (this, loc, host);
+               }
+
+               public CaptureContext CaptureContext {
+                       get {
+                               return capture_context;
+                       }
+               }
        }
        
        public class SwitchLabel {
                Expression label;
                object converted;
                public Location loc;
-               public Label ILLabel;
-               public Label ILLabelCode;
+
+               Label il_label;
+               bool  il_label_set;
+               Label il_label_code;
+               bool  il_label_code_set;
 
                //
                // if expr == null, then it is the default case.
@@ -1979,15 +2220,30 @@ namespace Mono.CSharp {
                        }
                }
 
+               public Label GetILLabel (EmitContext ec)
+               {
+                       if (!il_label_set){
+                               il_label = ec.ig.DefineLabel ();
+                               il_label_set = true;
+                       }
+                       return il_label;
+               }
+
+               public Label GetILLabelCode (EmitContext ec)
+               {
+                       if (!il_label_code_set){
+                               il_label_code = ec.ig.DefineLabel ();
+                               il_label_code_set = true;
+                       }
+                       return il_label_code;
+               }                               
+               
                //
                // Resolves the expression, reduces it to a literal if possible
                // and then converts it to the requested type.
                //
                public bool ResolveAndReduce (EmitContext ec, Type required_type)
                {
-                       ILLabel = ec.ig.DefineLabel ();
-                       ILLabelCode = ec.ig.DefineLabel ();
-
                        if (label == null)
                                return true;
                        
@@ -2004,7 +2260,6 @@ namespace Mono.CSharp {
                        if (e is StringConstant || e is NullLiteral){
                                if (required_type == TypeManager.string_type){
                                        converted = e;
-                                       ILLabel = ec.ig.DefineLabel ();
                                        return true;
                                }
                        }
@@ -2101,12 +2356,12 @@ namespace Mono.CSharp {
 
                        if (allowed_types == null){
                                allowed_types = new Type [] {
+                                       TypeManager.int32_type,
+                                       TypeManager.uint32_type,
                                        TypeManager.sbyte_type,
                                        TypeManager.byte_type,
                                        TypeManager.short_type,
                                        TypeManager.ushort_type,
-                                       TypeManager.int32_type,
-                                       TypeManager.uint32_type,
                                        TypeManager.int64_type,
                                        TypeManager.uint64_type,
                                        TypeManager.char_type,
@@ -2129,13 +2384,26 @@ namespace Mono.CSharp {
                                if (e == null)
                                        continue;
 
+                               //
+                               // Ignore over-worked ImplicitUserConversions that do
+                               // an implicit conversion in addition to the user conversion.
+                               // 
+                               if (e is UserCast){
+                                       UserCast ue = e as UserCast;
+
+                                       if (ue.Source != Expr)
+                                               e = null;
+                               }
+                               
                                if (converted != null){
-                                       Report.Error (-12, loc, "More than one conversion to an integral " +
-                                                     " type exists for type `" +
-                                                     TypeManager.CSharpName (Expr.Type)+"'");
+                                       Report.ExtraInformation (
+                                               loc,
+                                               String.Format ("reason: more than one conversion to an integral type exist for type {0}",
+                                                              TypeManager.CSharpName (Expr.Type)));
                                        return null;
-                               } else
+                               } else {
                                        converted = e;
+                               }
                        }
                        return converted;
                }
@@ -2471,7 +2739,7 @@ namespace Mono.CSharp {
                                                ig.Emit (OpCodes.Ldloc, val);
                                                EmitObjectInteger (ig, key);
                                                SwitchLabel sl = (SwitchLabel) Elements [key];
-                                               ig.Emit (OpCodes.Beq, sl.ILLabel);
+                                               ig.Emit (OpCodes.Beq, sl.GetILLabel (ec));
                                        }
                                }
                                else
@@ -2527,7 +2795,7 @@ namespace Mono.CSharp {
                                                if (System.Convert.ToInt64 (key) == kb.nFirst + iJump)
                                                {
                                                        SwitchLabel sl = (SwitchLabel) Elements [key];
-                                                       rgLabels [iJump] = sl.ILLabel;
+                                                       rgLabels [iJump] = sl.GetILLabel (ec);
                                                        iKey++;
                                                }
                                                else
@@ -2555,8 +2823,8 @@ namespace Mono.CSharp {
                        {
                                foreach (SwitchLabel sl in ss.Labels)
                                {
-                                       ig.MarkLabel (sl.ILLabel);
-                                       ig.MarkLabel (sl.ILLabelCode);
+                                       ig.MarkLabel (sl.GetILLabel (ec));
+                                       ig.MarkLabel (sl.GetILLabelCode (ec));
                                        if (sl.Label == null)
                                        {
                                                ig.MarkLabel (lblDefault);
@@ -2614,7 +2882,7 @@ namespace Mono.CSharp {
                                null_found = false;
                                for (int label = 0; label < label_count; label++){
                                        SwitchLabel sl = (SwitchLabel) ss.Labels [label];
-                                       ig.MarkLabel (sl.ILLabel);
+                                       ig.MarkLabel (sl.GetILLabel (ec));
                                        
                                        if (!first_test){
                                                ig.MarkLabel (next_test);
@@ -2656,7 +2924,7 @@ namespace Mono.CSharp {
                                        ig.MarkLabel (null_target);
                                ig.MarkLabel (sec_begin);
                                foreach (SwitchLabel sl in ss.Labels)
-                                       ig.MarkLabel (sl.ILLabelCode);
+                                       ig.MarkLabel (sl.GetILLabelCode (ec));
 
                                if (mark_default)
                                        ig.MarkLabel (default_target);
@@ -3052,11 +3320,11 @@ namespace Mono.CSharp {
                                return false;
                        }
                        
-                       type = type.ResolveAsTypeTerminal (ec, false);
-                       if (type == null)
+                       TypeExpr texpr = type.ResolveAsTypeTerminal (ec, false);
+                       if (texpr == null)
                                return false;
 
-                       expr_type = type.Type;
+                       expr_type = texpr.ResolveType (ec);
 
                        CheckObsolete (expr_type);
 
@@ -3325,7 +3593,7 @@ namespace Mono.CSharp {
                                if (te == null)
                                        return false;
 
-                               type = te.Type;
+                               type = te.ResolveType (ec);
 
                                CheckObsolete (type);
 
@@ -3527,11 +3795,11 @@ namespace Mono.CSharp {
                {
                        int i = 0;
 
-                       expr = expr.ResolveAsTypeTerminal (ec, false);
-                       if (expr == null)
+                       TypeExpr texpr = expr.ResolveAsTypeTerminal (ec, false);
+                       if (texpr == null)
                                return false;
 
-                       expr_type = expr.Type;
+                       expr_type = texpr.ResolveType (ec);
 
                        //
                        // The type must be an IDisposable or an implicit conversion
@@ -3625,7 +3893,7 @@ namespace Mono.CSharp {
                        ILGenerator ig = ec.ig;
 
                        int i = assign.Length;
-                       foreach (DictionaryEntry e in var_list){
+                       for (int ii = 0; ii < var_list.Count; ++ii){
                                Expression var = resolved_vars [--i];
                                Label skip = ig.DefineLabel ();
 
@@ -3699,12 +3967,39 @@ namespace Mono.CSharp {
                void EmitExpressionFinally (EmitContext ec)
                {
                        ILGenerator ig = ec.ig;
-                       Label skip = ig.DefineLabel ();
-                       ig.Emit (OpCodes.Ldloc, local_copy);
-                       ig.Emit (OpCodes.Brfalse, skip);
-                       ig.Emit (OpCodes.Ldloc, local_copy);
-                       ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
-                       ig.MarkLabel (skip);
+                       if (!local_copy.LocalType.IsValueType) {
+                               Label skip = ig.DefineLabel ();
+                               ig.Emit (OpCodes.Ldloc, local_copy);
+                               ig.Emit (OpCodes.Brfalse, skip);
+                               ig.Emit (OpCodes.Ldloc, local_copy);
+                               ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
+                               ig.MarkLabel (skip);
+                       } else {
+                               Expression ml = Expression.MemberLookup(ec, TypeManager.idisposable_type, local_copy.LocalType, "Dispose", Mono.CSharp.Location.Null);
+
+                               if (!(ml is MethodGroupExpr)) {
+                                       ig.Emit (OpCodes.Ldloc, local_copy);
+                                       ig.Emit (OpCodes.Box, local_copy.LocalType);
+                                       ig.Emit (OpCodes.Callvirt, TypeManager.void_dispose_void);
+                               } else {
+                                       MethodInfo mi = null;
+
+                                       foreach (MethodInfo mk in ((MethodGroupExpr) ml).Methods) {
+                                               if (TypeManager.GetArgumentTypes (mk).Length == 0) {
+                                                       mi = mk;
+                                                       break;
+                                               }
+                                       }
+
+                                       if (mi == null) {
+                                               Report.Error(-100, Mono.CSharp.Location.Null, "Internal error: No Dispose method which takes 0 parameters.");
+                                               return;
+                                       }
+
+                                       ig.Emit (OpCodes.Ldloca, local_copy);
+                                       ig.Emit (OpCodes.Call, mi);
+                               }
+                       }
                }
                
                public override bool Resolve (EmitContext ec)
@@ -3799,11 +4094,16 @@ namespace Mono.CSharp {
                        if (expr == null)
                                return false;
 
-                       type = type.ResolveAsTypeTerminal (ec, false);
-                       if (type == null)
+                       if (expr is NullLiteral) {
+                               Report.Error (186, expr.Location, "Use of null is not valid in this context");
+                               return false;
+                       }
+
+                       TypeExpr texpr = type.ResolveAsTypeTerminal (ec, false);
+                       if (texpr == null)
                                return false;
 
-                       var_type = type.Type;
+                       var_type = texpr.ResolveType (ec);
                        
                        //
                        // We need an instance variable.  Not sure this is the best
@@ -3828,8 +4128,12 @@ namespace Mono.CSharp {
                                if (hm == null){
                                        error1579 (expr.Type);
                                        return false;
-                               }                       
+                               }
 
+                               // When ProbeCollection reported error
+                               if (hm.move_next == null)
+                                       return false;
+       
                                array_type = expr.Type;
                                element_type = hm.element_type;
 
@@ -3966,10 +4270,12 @@ namespace Mono.CSharp {
                        public Type element_type;
                        public Type enumerator_type;
                        public bool is_disposable;
+                       public readonly Location Location;
 
-                       public ForeachHelperMethods (EmitContext ec)
+                       public ForeachHelperMethods (EmitContext ec, Location loc)
                        {
                                this.ec = ec;
+                               this.Location = loc;
                                this.element_type = TypeManager.object_type;
                                this.enumerator_type = TypeManager.ienumerator_type;
                                this.is_disposable = true;
@@ -3994,7 +4300,6 @@ namespace Mono.CSharp {
                                        return false;
                        }
                        ForeachHelperMethods hm = (ForeachHelperMethods) criteria;
-                       EmitContext ec = hm.ec;
 
                        // Check whether GetEnumerator is public
                        if ((mi.Attributes & MethodAttributes.Public) != MethodAttributes.Public)
@@ -4044,6 +4349,16 @@ namespace Mono.CSharp {
 
                        } else {
 
+                               if (return_type.IsPointer || return_type.IsArray) {
+                                       Report.SymbolRelatedToPreviousError (mi);
+                                       Type t = return_type.GetElementType ();
+                                       Report.SymbolRelatedToPreviousError (t);
+                                       Report.Error (202, hm.Location, "foreach requires that the return type '{0}' of '{1}' must have a suitable public MoveNext method and public Current property", 
+                                               TypeManager.CSharpName (return_type), TypeManager.GetFullNameSignature (m));
+                                       hm.get_enumerator = mi;
+                                       return false;
+                               }
+
                                //
                                // Ok, so they dont return an IEnumerable, we will have to
                                // find if they support the GetEnumerator pattern.
@@ -4108,7 +4423,7 @@ namespace Mono.CSharp {
                //
                ForeachHelperMethods ProbeCollectionType (EmitContext ec, Type t)
                {
-                       ForeachHelperMethods hm = new ForeachHelperMethods (ec);
+                       ForeachHelperMethods hm = new ForeachHelperMethods (ec, loc);
 
                        for (Type tt = t; tt != null && tt != TypeManager.object_type;){
                                if (TryType (tt, hm))
@@ -4201,12 +4516,13 @@ namespace Mono.CSharp {
                        ig.Emit (OpCodes.Brfalse, end_try);
 
                        if (ec.InIterator)
-                               enumerator.EmitThis (ig);
+                               ig.Emit (OpCodes.Ldarg_0);
+                       
                        enumerator.EmitCall (ig, hm.get_current);
 
                        if (ec.InIterator){
                                conv.Emit (ec);
-                               ig.Emit (OpCodes.Stfld, ((FieldExpr) variable).FieldInfo);
+                               ig.Emit (OpCodes.Stfld, ((LocalVariableReference) variable).local_info.FieldBuilder);
                        } else 
                                ((IAssignMethod)variable).EmitAssign (ec, conv, false, false);
                                
@@ -4298,7 +4614,7 @@ namespace Mono.CSharp {
                                ig.MarkLabel (loop);
 
                                if (ec.InIterator)
-                                       ec.EmitThis ();
+                                       ig.Emit (OpCodes.Ldarg_0);
                                
                                copy.EmitThis (ig);
                                copy.EmitLoad (ig);
@@ -4312,7 +4628,7 @@ namespace Mono.CSharp {
                                ArrayAccess.EmitLoadOpcode (ig, element_type);
                                if (ec.InIterator){
                                        conv.Emit (ec);
-                                       ig.Emit (OpCodes.Stfld, ((FieldExpr) variable).FieldInfo);
+                                       ig.Emit (OpCodes.Stfld, ((LocalVariableReference) variable).local_info.FieldBuilder);
                                } else 
                                        ((IAssignMethod)variable).EmitAssign (ec, conv, false, false);
 
@@ -4367,7 +4683,8 @@ namespace Mono.CSharp {
                                }
 
                                if (ec.InIterator)
-                                       ec.EmitThis ();
+                                       ig.Emit (OpCodes.Ldarg_0);
+                               
                                copy.EmitThis (ig);
                                copy.EmitLoad (ig);
                                for (dim = 0; dim < rank; dim++){
@@ -4392,7 +4709,7 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Call, get);
                                if (ec.InIterator){
                                        conv.Emit (ec);
-                                       ig.Emit (OpCodes.Stfld, ((FieldExpr) variable).FieldInfo);
+                                       ig.Emit (OpCodes.Stfld, ((LocalVariableReference) variable).local_info.FieldBuilder);
                                } else 
                                        ((IAssignMethod)variable).EmitAssign (ec, conv, false, false);
                                statement.Emit (ec);