Copying latest mcs to the branch.
[mono.git] / mcs / gmcs / expression.cs
index 6f1413541baaa7648cc7c5e1a5e8f1fd8cd83fc2..eaccda581af45e785e97cd22a9fee2ed83d97f8a 100644 (file)
@@ -456,6 +456,12 @@ namespace Mono.CSharp {
                                        lr.local_info.Used = true;
                                }
 
+                               ParameterReference pr = Expr as ParameterReference;
+                               if ((pr != null) && pr.Parameter.IsCaptured) {
+                                       AnonymousMethod.Error_AddressOfCapturedVar (pr.Name, loc);
+                                       return null;
+                               }
+
                                // According to the specs, a variable is considered definitely assigned if you take
                                // its address.
                                if ((variable != null) && (variable.VariableInfo != null)){
@@ -719,8 +725,10 @@ namespace Mono.CSharp {
                        
                        StoreFromPtr (ec.ig, type);
                        
-                       if (temporary != null)
+                       if (temporary != null) {
                                temporary.Emit (ec);
+                               temporary.Release (ec);
+                       }
                }
                
                public void AddressOf (EmitContext ec, AddressOp Mode)
@@ -749,9 +757,7 @@ namespace Mono.CSharp {
                #region IVariable Members
 
                public VariableInfo VariableInfo {
-                       get {
-                               return null;
-                       }
+                       get { return null; }
                }
 
                public bool VerifyFixed ()
@@ -790,18 +796,18 @@ namespace Mono.CSharp {
                        PostIncrement  = IsPost,
                        PostDecrement  = IsPost | IsDecrement
                }
-               
+
                Mode mode;
                bool is_expr = false;
                bool recurse = false;
-               
+
                Expression expr;
 
                //
                // This is expensive for the simplest case.
                //
                StaticCallExpr method;
-                       
+
                public UnaryMutator (Mode m, Expression e, Location l)
                {
                        mode = m;
@@ -814,7 +820,7 @@ namespace Mono.CSharp {
                        return (mode == Mode.PreIncrement || mode == Mode.PostIncrement) ?
                                "++" : "--";
                }
-               
+
                /// <summary>
                ///   Returns whether an object of type `t' can be incremented
                ///   or decremented with add/sub (ie, basically whether we can
@@ -990,7 +996,7 @@ namespace Mono.CSharp {
                        }
                        
                }
-               
+
                void EmitCode (EmitContext ec, bool is_expr)
                {
                        recurse = true;
@@ -1006,7 +1012,7 @@ namespace Mono.CSharp {
                        // having to allocate another expression
                        //
                        if (recurse) {
-                               ((IAssignMethod) expr).Emit (ec, is_expr && (mode == Mode.PostIncrement  || mode == Mode.PostDecrement));
+                               ((IAssignMethod) expr).Emit (ec, is_expr && (mode == Mode.PostIncrement || mode == Mode.PostDecrement));
                                if (method == null)
                                        LoadOneAndEmitOp (ec, expr.Type);
                                else
@@ -1014,10 +1020,10 @@ namespace Mono.CSharp {
                                recurse = false;
                                return;
                        }
-                       
+
                        EmitCode (ec, true);
                }
-               
+
                public override void EmitStatement (EmitContext ec)
                {
                        EmitCode (ec, false);
@@ -1327,7 +1333,7 @@ namespace Mono.CSharp {
                        this.loc = loc;
 
                        if (target_type == TypeManager.system_void_expr)
-                               Report.Error (1547, loc, "Keyword `void' cannot be used in this context");
+                               Error_VoidInvalidInTheContext (loc);
                }
 
                public Expression TargetType {
@@ -1608,7 +1614,8 @@ namespace Mono.CSharp {
                
                void Error_OperatorCannotBeApplied ()
                {
-                       Error_OperatorCannotBeApplied (Location, OperName (oper), left.GetSignatureForError (), right.GetSignatureForError ());
+                       Error_OperatorCannotBeApplied (Location, OperName (oper), TypeManager.CSharpName (left.Type),
+                               TypeManager.CSharpName(right.Type));
                }
 
                static bool is_unsigned (Type t)
@@ -1923,8 +1930,8 @@ namespace Mono.CSharp {
                                                        r = right.Type;
                                                }
                                        }
-                               
-                                       if (TypeManager.IsDelegateType (r)){
+
+                                       if (TypeManager.IsDelegateType (r) || right is NullLiteral){
                                                MethodInfo method;
                                                ArrayList args = new ArrayList (2);
                                        
@@ -1937,7 +1944,7 @@ namespace Mono.CSharp {
                                                else
                                                        method = TypeManager.delegate_remove_delegate_delegate;
 
-                                               if (!TypeManager.IsEqual (l, r)) {
+                                               if (!TypeManager.IsEqual (l, r) && !(right is NullLiteral)) {
                                                        Error_OperatorCannotBeApplied ();
                                                        return null;
                                                }
@@ -3063,6 +3070,9 @@ namespace Mono.CSharp {
                        ig.MarkLabel (false_target);
                        op.Emit (ec);
                        ig.MarkLabel (end_target);
+
+                       // We release 'left_temp' here since 'op' may refer to it too
+                       left_temp.Release (ec);
                }
        }
 
@@ -3292,16 +3302,116 @@ namespace Mono.CSharp {
 
        }
 
+       public abstract class VariableReference : Expression, IAssignMethod, IMemoryLocation {
+               bool prepared;
+               LocalTemporary temp;
+
+               public abstract Variable Variable {
+                       get;
+               }
+
+               public abstract bool IsRef {
+                       get;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       Emit (ec, false);
+               }
+
+               //
+               // This method is used by parameters that are references, that are
+               // being passed as references:  we only want to pass the pointer (that
+               // is already stored in the parameter, not the address of the pointer,
+               // and not the value of the variable).
+               //
+               public void EmitLoad (EmitContext ec)
+               {
+                       Report.Debug (64, "VARIABLE EMIT LOAD", this, Variable, type, loc);
+                       if (!prepared)
+                               Variable.EmitInstance (ec);
+                       Variable.Emit (ec);
+               }
+               
+               public void Emit (EmitContext ec, bool leave_copy)
+               {
+                       Report.Debug (64, "VARIABLE EMIT", this, Variable, type, IsRef, loc);
+
+                       EmitLoad (ec);
+
+                       if (IsRef) {
+                               if (prepared)
+                                       ec.ig.Emit (OpCodes.Dup);
+       
+                               //
+                               // If we are a reference, we loaded on the stack a pointer
+                               // Now lets load the real value
+                               //
+                               LoadFromPtr (ec.ig, type);
+                       }
+
+                       if (leave_copy) {
+                               ec.ig.Emit (OpCodes.Dup);
+
+                               if (IsRef || Variable.NeedsTemporary) {
+                                       temp = new LocalTemporary (Type);
+                                       temp.Store (ec);
+                               }
+                       }
+               }
+
+               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy,
+                                       bool prepare_for_load)
+               {
+                       Report.Debug (64, "VARIABLE EMIT ASSIGN", this, Variable, type, IsRef,
+                                     source, loc);
+
+                       ILGenerator ig = ec.ig;
+                       prepared = prepare_for_load;
+
+                       Variable.EmitInstance (ec);
+                       if (prepare_for_load && Variable.HasInstance)
+                               ig.Emit (OpCodes.Dup);
+                       else if (IsRef && !prepared)
+                               Variable.Emit (ec);
+
+                       source.Emit (ec);
+
+                       if (leave_copy) {
+                               ig.Emit (OpCodes.Dup);
+                               if (IsRef || Variable.NeedsTemporary) {
+                                       temp = new LocalTemporary (Type);
+                                       temp.Store (ec);
+                               }
+                       }
+
+                       if (IsRef)
+                               StoreFromPtr (ig, type);
+                       else
+                               Variable.EmitAssign (ec);
+
+                       if (temp != null) {
+                               temp.Emit (ec);
+                               temp.Release (ec);
+                       }
+               }
+               
+               public void AddressOf (EmitContext ec, AddressOp mode)
+               {
+                       Variable.EmitInstance (ec);
+                       Variable.EmitAddressOf (ec);
+               }
+       }
+
        /// <summary>
        ///   Local variables
        /// </summary>
-       public class LocalVariableReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
+       public class LocalVariableReference : VariableReference, IVariable {
                public readonly string Name;
                public readonly Block Block;
                public LocalInfo local_info;
                bool is_readonly;
-               bool prepared;
-               LocalTemporary temp;
+               Variable variable;
 
                public LocalVariableReference (Block block, string name, Location l)
                {
@@ -3327,6 +3437,10 @@ namespace Mono.CSharp {
                        get { return local_info.VariableInfo; }
                }
 
+               public override bool IsRef {
+                       get { return false; }
+               }
+
                public bool IsReadOnly {
                        get { return is_readonly; }
                }
@@ -3356,20 +3470,19 @@ namespace Mono.CSharp {
                        if (!VerifyAssigned (ec))
                                return null;
 
-                       if (ec.CurrentAnonymousMethod != null){
-                               //
-                               // If we are referencing a variable from the external block
-                               // flag it for capturing
-                               //
-                               if ((local_info.Block.Toplevel != ec.CurrentBlock.Toplevel) ||
-                                   ec.CurrentAnonymousMethod.IsIterator)
-                               {
-                                       if (local_info.AddressTaken){
-                                               AnonymousMethod.Error_AddressOfCapturedVar (local_info.Name, loc);
-                                               return null;
-                                       }
-                                       ec.CaptureVariable (local_info);
+                       //
+                       // If we are referencing a variable from the external block
+                       // flag it for capturing
+                       //
+                       if (ec.MustCaptureVariable (local_info)) {
+                               if (local_info.AddressTaken){
+                                       AnonymousMethod.Error_AddressOfCapturedVar (local_info.Name, loc);
+                                       return null;
                                }
+
+                               ScopeInfo scope = local_info.Block.CreateScopeInfo ();
+                               variable = scope.AddLocal (local_info);
+                               type = variable.Type;
                        }
 
                        return this;
@@ -3432,90 +3545,8 @@ namespace Mono.CSharp {
                        return Name == lvr.Name && Block == lvr.Block;
                }
 
-               public override void Emit (EmitContext ec)
-               {
-                       ILGenerator ig = ec.ig;
-
-                       if (local_info.FieldBuilder == null){
-                               //
-                               // A local variable on the local CLR stack
-                               //
-                               ig.Emit (OpCodes.Ldloc, local_info.LocalBuilder);
-                       } else {
-                               //
-                               // A local variable captured by anonymous methods.
-                               //
-                               if (!prepared)
-                                       ec.EmitCapturedVariableInstance (local_info);
-                               
-                               ig.Emit (OpCodes.Ldfld, local_info.FieldBuilder);
-                       }
-               }
-               
-               public void Emit (EmitContext ec, bool leave_copy)
-               {
-                       Emit (ec);
-                       if (leave_copy){
-                               ec.ig.Emit (OpCodes.Dup);
-                               if (local_info.FieldBuilder != null){
-                                       temp = new LocalTemporary (Type);
-                                       temp.Store (ec);
-                               }
-                       }
-               }
-               
-               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
-               {
-                       ILGenerator ig = ec.ig;
-                       prepared = prepare_for_load;
-
-                       if (local_info.FieldBuilder == null){
-                               //
-                               // A local variable on the local CLR stack
-                               //
-                               if (local_info.LocalBuilder == null)
-                                       throw new Exception ("This should not happen: both Field and Local are null");
-
-                               source.Emit (ec);
-                               if (leave_copy)
-                                       ec.ig.Emit (OpCodes.Dup);
-                               ig.Emit (OpCodes.Stloc, local_info.LocalBuilder);
-                       } else {
-                               //
-                               // A local variable captured by anonymous methods or itereators.
-                               //
-                               ec.EmitCapturedVariableInstance (local_info);
-
-                               if (prepare_for_load)
-                                       ig.Emit (OpCodes.Dup);
-                               source.Emit (ec);
-                               if (leave_copy){
-                                       ig.Emit (OpCodes.Dup);
-                                       temp = new LocalTemporary (Type);
-                                       temp.Store (ec);
-                               }
-                               ig.Emit (OpCodes.Stfld, local_info.FieldBuilder);
-                               if (temp != null)
-                                       temp.Emit (ec);
-                       }
-               }
-               
-               public void AddressOf (EmitContext ec, AddressOp mode)
-               {
-                       ILGenerator ig = ec.ig;
-
-                       if (local_info.FieldBuilder == null){
-                               //
-                               // A local variable on the local CLR stack
-                               //
-                               ig.Emit (OpCodes.Ldloca, local_info.LocalBuilder);
-                       } else {
-                               //
-                               // A local variable captured by anonymous methods or iterators
-                               //
-                               ec.EmitCapturedVariableInstance (local_info);
-                               ig.Emit (OpCodes.Ldflda, local_info.FieldBuilder);
-                       }
+               public override Variable Variable {
+                       get { return variable != null ? variable : local_info.Variable; }
                }
 
                public override string ToString ()
@@ -3528,13 +3559,13 @@ namespace Mono.CSharp {
        ///   This represents a reference to a parameter in the intermediate
        ///   representation.
        /// </summary>
-       public class ParameterReference : Expression, IAssignMethod, IMemoryLocation, IVariable {
+       public class ParameterReference : VariableReference, IVariable {
                Parameter par;
                string name;
                int idx;
                Block block;
                VariableInfo vi;
-               public bool is_ref, is_out, prepared;
+               public bool is_ref, is_out;
 
                public bool IsOut {
                        get {
@@ -3542,13 +3573,25 @@ namespace Mono.CSharp {
                        }
                }
 
-               public bool IsRef {
+               public override bool IsRef {
                        get {
                                return is_ref;
                        }
                }
 
-               LocalTemporary temp;
+               public string Name {
+                       get {
+                               return name;
+                       }
+               }
+
+               public Parameter Parameter {
+                       get {
+                               return par;
+                       }
+               }
+
+               Variable variable;
                
                public ParameterReference (Parameter par, Block block, int idx, Location loc)
                {
@@ -3564,6 +3607,10 @@ namespace Mono.CSharp {
                        get { return vi; }
                }
 
+               public override Variable Variable {
+                       get { return variable != null ? variable : par.Variable; }
+               }
+
                public bool VerifyFixed ()
                {
                        // A parameter is fixed if it's a value parameter (i.e., no modifier like out, ref, param).
@@ -3617,23 +3664,33 @@ namespace Mono.CSharp {
                        if (is_out)
                                vi = block.ParameterMap [idx];
 
-                       if (ec.CurrentAnonymousMethod != null){
-                               if (is_ref && !block.Toplevel.IsLocalParameter (name)){
-                                       Report.Error (1628, Location, "Cannot use ref or out parameter `{0}' inside an anonymous method block",
-                                               par.Name);
-                                       return false;
-                               }
+                       AnonymousContainer am = ec.CurrentAnonymousMethod;
+                       if (am == null)
+                               return true;
 
-                               //
-                               // If we are referencing the parameter from the external block
-                               // flag it for capturing
-                               //
-                               //Console.WriteLine ("Is parameter `{0}' local? {1}", name, block.IsLocalParameter (name));
-                               if (!block.Toplevel.IsLocalParameter (name)){
-                                       ec.CaptureParameter (name, type, idx);
+                       if (is_ref && !block.Toplevel.IsLocalParameter (name)){
+                               Report.Error (1628, Location,
+                                             "Cannot use ref or out parameter `{0}' inside an " +
+                                             "anonymous method block", par.Name);
+                               return false;
+                       }
+
+                       if (!am.IsIterator && block.Toplevel.IsLocalParameter (name))
+                               return true;
+
+                       AnonymousMethodHost host = null;
+                       ToplevelBlock toplevel = block.Toplevel;
+                       while (toplevel != null) {
+                               if (toplevel.IsLocalParameter (name)) {
+                                       host = toplevel.AnonymousMethodHost;
+                                       break;
                                }
+
+                               toplevel = toplevel.Container;
                        }
 
+                       variable = host.AddParameter (par, idx);
+                       type = variable.Type;
                        return true;
                }
 
@@ -3668,7 +3725,8 @@ namespace Mono.CSharp {
                        if (!DoResolveBase (ec))
                                return null;
 
-                       if (is_out && ec.DoFlowAnalysis && (!ec.OmitStructFlowAnalysis || !vi.TypeInfo.IsStruct) && !IsAssigned (ec, loc))
+                       if (is_out && ec.DoFlowAnalysis &&
+                           (!ec.OmitStructFlowAnalysis || !vi.TypeInfo.IsStruct) && !IsAssigned (ec, loc))
                                return null;
 
                        return this;
@@ -3698,135 +3756,6 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Ldarg, x);
                }
                
-               //
-               // This method is used by parameters that are references, that are
-               // being passed as references:  we only want to pass the pointer (that
-               // is already stored in the parameter, not the address of the pointer,
-               // and not the value of the variable).
-               //
-               public void EmitLoad (EmitContext ec)
-               {
-                       ILGenerator ig = ec.ig;
-                       int arg_idx = idx;
-
-                       if (!ec.MethodIsStatic)
-                               arg_idx++;
-                       
-                       EmitLdArg (ig, arg_idx);
-
-                       //
-                       // FIXME: Review for anonymous methods
-                       //
-               }
-               
-               public override void Emit (EmitContext ec)
-               {
-                       Emit (ec, false);
-               }
-               
-               public void Emit (EmitContext ec, bool leave_copy)
-               {
-                       ILGenerator ig = ec.ig;
-                       int arg_idx = idx;
-
-                       if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){                               
-                               ec.EmitParameter (name, leave_copy, prepared, ref temp);
-                               return;
-                       }
-
-                       if (!ec.MethodIsStatic)
-                               arg_idx++;
-
-                       EmitLdArg (ig, arg_idx);
-
-                       if (is_ref) {
-                               if (prepared)
-                                       ec.ig.Emit (OpCodes.Dup);
-       
-                               //
-                               // If we are a reference, we loaded on the stack a pointer
-                               // Now lets load the real value
-                               //
-                               LoadFromPtr (ig, type);
-                       }
-                       
-                       if (leave_copy) {
-                               ec.ig.Emit (OpCodes.Dup);
-                               
-                               if (is_ref) {
-                                       temp = new LocalTemporary (type);
-                                       temp.Store (ec);
-                               }
-                       }
-               }
-               
-               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
-               {
-                       prepared = prepare_for_load;
-                       if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){
-                               ec.EmitAssignParameter (name, source, leave_copy, prepare_for_load, ref temp);
-                               return;
-                       }
-
-                       ILGenerator ig = ec.ig;
-                       int arg_idx = idx;
-                       
-                       
-                       
-                       if (!ec.MethodIsStatic)
-                               arg_idx++;
-
-                       if (is_ref && !prepared)
-                               EmitLdArg (ig, arg_idx);
-                       
-                       source.Emit (ec);
-
-                       if (leave_copy)
-                               ec.ig.Emit (OpCodes.Dup);
-                       
-                       if (is_ref) {
-                               if (leave_copy) {
-                                       temp = new LocalTemporary (type);
-                                       temp.Store (ec);
-                               }
-                               
-                               StoreFromPtr (ig, type);
-                               
-                               if (temp != null)
-                                       temp.Emit (ec);
-                       } else {
-                               if (arg_idx <= 255)
-                                       ig.Emit (OpCodes.Starg_S, (byte) arg_idx);
-                               else
-                                       ig.Emit (OpCodes.Starg, arg_idx);
-                       }
-               }
-
-               public void AddressOf (EmitContext ec, AddressOp mode)
-               {
-                       if (ec.HaveCaptureInfo && ec.IsParameterCaptured (name)){
-                               ec.EmitAddressOfParameter (name);
-                               return;
-                       }
-                       
-                       int arg_idx = idx;
-
-                       if (!ec.MethodIsStatic)
-                               arg_idx++;
-
-                       if (is_ref){
-                               if (arg_idx <= 255)
-                                       ec.ig.Emit (OpCodes.Ldarg_S, (byte) arg_idx);
-                               else
-                                       ec.ig.Emit (OpCodes.Ldarg, arg_idx);
-                       } else {
-                               if (arg_idx <= 255)
-                                       ec.ig.Emit (OpCodes.Ldarga_S, (byte) arg_idx);
-                               else
-                                       ec.ig.Emit (OpCodes.Ldarga, arg_idx);
-                       }
-               }
-
                public override string ToString ()
                {
                        return "ParameterReference[" + name + "]";
@@ -3911,20 +3840,17 @@ namespace Mono.CSharp {
 
                public bool Resolve (EmitContext ec, Location loc)
                {
-                       bool old_do_flow_analysis = ec.DoFlowAnalysis;
-                       ec.DoFlowAnalysis = true;
-
-                       // Verify that the argument is readable
-                       if (ArgType != AType.Out)
-                               Expr = Expr.Resolve (ec);
-
-                       // Verify that the argument is writeable
-                       if (Expr != null && (ArgType == AType.Out || ArgType == AType.Ref))
-                               Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess, loc);
+                       using (ec.With (EmitContext.Flags.DoFlowAnalysis, true)) {
+                               // Verify that the argument is readable
+                               if (ArgType != AType.Out)
+                                       Expr = Expr.Resolve (ec);
 
-                       ec.DoFlowAnalysis = old_do_flow_analysis;
+                               // Verify that the argument is writeable
+                               if (Expr != null && (ArgType == AType.Out || ArgType == AType.Ref))
+                                       Expr = Expr.ResolveLValue (ec, EmptyExpression.OutAccess, loc);
 
-                       return Expr != null;
+                               return Expr != null;
+                       }
                }
 
                public void Emit (EmitContext ec)
@@ -4088,7 +4014,15 @@ namespace Mono.CSharp {
                        if (!p.IsGenericParameter && q.IsGenericParameter)
                                return p;
 
-                       if (p.IsGenericType) {
+                       if (TypeManager.HasElementType (p)) {
+                               Type pe = TypeManager.GetElementType (p);
+                               Type qe = TypeManager.GetElementType (q);
+                               Type specific = MoreSpecific (pe, qe);
+                               if (specific == pe)
+                                       return p;
+                               if (specific == qe)
+                                       return q;
+                       } else if (p.IsGenericType) {
                                Type[] pargs = TypeManager.GetTypeArguments (p);
                                Type[] qargs = TypeManager.GetTypeArguments (q);
 
@@ -4107,14 +4041,6 @@ namespace Mono.CSharp {
                                        return p;
                                if (!p_specific_at_least_once && q_specific_at_least_once)
                                        return q;
-                       } else if (TypeManager.HasElementType (p)) {
-                               Type pe = TypeManager.GetElementType (p);
-                               Type qe = TypeManager.GetElementType (q);
-                               Type specific = MoreSpecific (pe, qe);
-                               if (specific == pe)
-                                       return p;
-                               if (specific == qe)
-                                       return q;
                        }
 
                        return null;
@@ -5150,8 +5076,10 @@ namespace Mono.CSharp {
                                if (this_arg != null)
                                        this_arg.Emit (ec);
                                
-                               for (int i = 0; i < top; i ++)
+                               for (int i = 0; i < top; i ++) {
                                        temps [i].Emit (ec);
+                                       temps [i].Release (ec);
+                               }
                        }
 
                        if (pd != null && pd.Count > top &&
@@ -6593,6 +6521,7 @@ namespace Mono.CSharp {
                {
                        eclass = ExprClass.Variable;
                        type = ec.ContainerType;
+                       variable = new SimpleThis (type);
                        return this;
                }
        }
@@ -6600,11 +6529,14 @@ namespace Mono.CSharp {
        /// <summary>
        ///   Represents the `this' construct
        /// </summary>
-       public class This : Expression, IAssignMethod, IMemoryLocation, IVariable {
 
+       public class This : VariableReference, IVariable
+       {
                Block block;
                VariableInfo variable_info;
-               
+               protected Variable variable;
+               bool is_struct;
+
                public This (Block block, Location loc)
                {
                        this.loc = loc;
@@ -6625,6 +6557,14 @@ namespace Mono.CSharp {
                        return !TypeManager.IsValueType (Type);
                }
 
+               public override bool IsRef {
+                       get { return is_struct; }
+               }
+
+               public override Variable Variable {
+                       get { return variable; }
+               }
+
                public bool ResolveBase (EmitContext ec)
                {
                        eclass = ExprClass.Variable;
@@ -6634,16 +6574,38 @@ namespace Mono.CSharp {
                        else
                                type = ec.ContainerType;
 
+                       is_struct = ec.TypeContainer is Struct;
+
                        if (ec.IsStatic) {
-                               Error (26, "Keyword `this' is not valid in a static property, static method, or static field initializer");
+                               Error (26, "Keyword `this' is not valid in a static property, " +
+                                      "static method, or static field initializer");
                                return false;
                        }
 
-                       if (block != null && block.Toplevel.ThisVariable != null)
-                               variable_info = block.Toplevel.ThisVariable.VariableInfo;
+                       if (block != null) {
+                               if (block.Toplevel.ThisVariable != null)
+                                       variable_info = block.Toplevel.ThisVariable.VariableInfo;
 
-                       if (ec.CurrentAnonymousMethod != null)
-                               ec.CaptureThis ();
+                               AnonymousContainer am = ec.CurrentAnonymousMethod;
+                               if (is_struct && (am != null) && !am.IsIterator) {
+                                       Report.Error (1673, loc, "Anonymous methods inside structs " +
+                                                     "cannot access instance members of `this'. " +
+                                                     "Consider copying `this' to a local variable " +
+                                                     "outside the anonymous method and using the " +
+                                                     "local instead.");
+                                       return false;
+                               }
+
+                               AnonymousMethodHost host = block.Toplevel.AnonymousMethodHost;
+                               if ((host != null) && (!is_struct || host.IsIterator)) {
+                                       variable = host.CaptureThis ();
+                                       type = variable.Type;
+                                       is_struct = false;
+                               }
+                       }
+
+                       if (variable == null)
+                               variable = new SimpleThis (type);
                        
                        return true;
                }
@@ -6653,8 +6615,10 @@ namespace Mono.CSharp {
                        if (!ResolveBase (ec))
                                return null;
 
-                       if ((variable_info != null) && !(type.IsValueType && ec.OmitStructFlowAnalysis) && !variable_info.IsAssigned (ec)) {
-                               Error (188, "The `this' object cannot be used before all of its fields are assigned to");
+                       if ((variable_info != null) && !(type.IsValueType && ec.OmitStructFlowAnalysis) &&
+                           !variable_info.IsAssigned (ec)) {
+                               Error (188, "The `this' object cannot be used before all of its " +
+                                      "fields are assigned to");
                                variable_info.SetAssigned (ec);
                                return this;
                        }
@@ -6682,47 +6646,6 @@ namespace Mono.CSharp {
 
                        return this;
                }
-
-               public void Emit (EmitContext ec, bool leave_copy)
-               {
-                       Emit (ec);
-                       if (leave_copy)
-                               ec.ig.Emit (OpCodes.Dup);
-               }
-               
-               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
-               {
-                       ILGenerator ig = ec.ig;
-                       
-                       if (ec.TypeContainer is Struct){
-                               ec.EmitThis (false);
-                               source.Emit (ec);
-                               
-                               LocalTemporary t = null;
-                               if (leave_copy) {
-                                       t = new LocalTemporary (type);
-                                       ec.ig.Emit (OpCodes.Dup);
-                                       t.Store (ec);
-                               }
-
-                               ig.Emit (OpCodes.Stobj, type);
-                               
-                               if (leave_copy)
-                                       t.Emit (ec);
-                       } else {
-                               throw new Exception ("how did you get here");
-                       }
-               }
-               
-               public override void Emit (EmitContext ec)
-               {
-                       ILGenerator ig = ec.ig;
-
-                       ec.EmitThis (false);
-                       if (ec.TypeContainer is Struct)
-                               ig.Emit (OpCodes.Ldobj, type);
-               }
-
                public override int GetHashCode()
                {
                        return block.GetHashCode ();
@@ -6737,18 +6660,46 @@ namespace Mono.CSharp {
                        return block == t.block;
                }
 
-               public void AddressOf (EmitContext ec, AddressOp mode)
+               protected class SimpleThis : Variable
                {
-                       ec.EmitThis (true);
+                       Type type;
 
-                       // FIMXE
-                       // FIGURE OUT WHY LDARG_S does not work
-                       //
-                       // consider: struct X { int val; int P { set { val = value; }}}
-                       //
-                       // Yes, this looks very bad. Look at `NOTAS' for
-                       // an explanation.
-                       // ec.ig.Emit (OpCodes.Ldarga_S, (byte) 0);
+                       public SimpleThis (Type type)
+                       {
+                               this.type = type;
+                       }
+
+                       public override Type Type {
+                               get { return type; }
+                       }
+
+                       public override bool HasInstance {
+                               get { return false; }
+                       }
+
+                       public override bool NeedsTemporary {
+                               get { return false; }
+                       }
+
+                       public override void EmitInstance (EmitContext ec)
+                       {
+                               // Do nothing.
+                       }
+
+                       public override void Emit (EmitContext ec)
+                       {
+                               ec.ig.Emit (OpCodes.Ldarg_0);
+                       }
+
+                       public override void EmitAssign (EmitContext ec)
+                       {
+                               throw new InvalidOperationException ();
+                       }
+
+                       public override void EmitAddressOf (EmitContext ec)
+                       {
+                               ec.ig.Emit (OpCodes.Ldarg_0);
+                       }
                }
        }
 
@@ -6894,6 +6845,14 @@ namespace Mono.CSharp {
 
                public override bool GetAttributableValue (Type valueType, out object value)
                {
+                       if (typearg.ContainsGenericParameters) {
+                               Report.SymbolRelatedToPreviousError(typearg);
+                               Report.Error(416, loc, "`{0}': an attribute argument cannot use type parameters",
+                                       TypeManager.CSharpName(typearg));
+                               value = null;
+                               return false;
+                       }
+
                        if (valueType == TypeManager.object_type) {
                                value = (object)typearg;
                                return true;
@@ -6901,6 +6860,14 @@ namespace Mono.CSharp {
                        value = typearg;
                        return true;
                }
+
+               public Type TypeArgument
+               {
+                       get
+                       {
+                               return typearg;
+                       }
+               }
        }
 
        /// <summary>
@@ -6947,6 +6914,13 @@ namespace Mono.CSharp {
                        }
 
                        type_queried = texpr.Type;
+                       if (type_queried.IsEnum)
+                               type_queried = TypeManager.EnumToUnderlying (type_queried);
+
+                       if (type_queried == TypeManager.void_type) {
+                               Expression.Error_VoidInvalidInTheContext (loc);
+                               return null;
+                       }
 
                        int size_of = GetTypeSize (type_queried);
                        if (size_of > 0) {
@@ -7331,14 +7305,8 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       bool last_check = ec.CheckState;
-                       bool last_const_check = ec.ConstantCheckState;
-
-                       ec.CheckState = true;
-                       ec.ConstantCheckState = true;
-                       Expr = Expr.Resolve (ec);
-                       ec.CheckState = last_check;
-                       ec.ConstantCheckState = last_const_check;
+                       using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
+                               Expr = Expr.Resolve (ec);
                        
                        if (Expr == null)
                                return null;
@@ -7353,16 +7321,15 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       bool last_check = ec.CheckState;
-                       bool last_const_check = ec.ConstantCheckState;
-                       
-                       ec.CheckState = true;
-                       ec.ConstantCheckState = true;
-                       Expr.Emit (ec);
-                       ec.CheckState = last_check;
-                       ec.ConstantCheckState = last_const_check;
+                       using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
+                               Expr.Emit (ec);
+               }
+
+               public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
+               {
+                       using (ec.With (EmitContext.Flags.AllCheckStateFlags, true))
+                               Expr.EmitBranchable (ec, target, onTrue);
                }
-               
        }
 
        /// <summary>
@@ -7380,14 +7347,8 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       bool last_check = ec.CheckState;
-                       bool last_const_check = ec.ConstantCheckState;
-
-                       ec.CheckState = false;
-                       ec.ConstantCheckState = false;
-                       Expr = Expr.Resolve (ec);
-                       ec.CheckState = last_check;
-                       ec.ConstantCheckState = last_const_check;
+                       using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
+                               Expr = Expr.Resolve (ec);
 
                        if (Expr == null)
                                return null;
@@ -7402,16 +7363,15 @@ namespace Mono.CSharp {
 
                public override void Emit (EmitContext ec)
                {
-                       bool last_check = ec.CheckState;
-                       bool last_const_check = ec.ConstantCheckState;
-                       
-                       ec.CheckState = false;
-                       ec.ConstantCheckState = false;
-                       Expr.Emit (ec);
-                       ec.CheckState = last_check;
-                       ec.ConstantCheckState = last_const_check;
+                       using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
+                               Expr.Emit (ec);
                }
                
+               public override void EmitBranchable (EmitContext ec, Label target, bool onTrue)
+               {
+                       using (ec.With (EmitContext.Flags.AllCheckStateFlags, false))
+                               Expr.EmitBranchable (ec, target, onTrue);
+               }
        }
 
        /// <summary>
@@ -7835,8 +7795,10 @@ namespace Mono.CSharp {
                                }
                                StoreFromPtr (ec.ig, t);
                                
-                               if (temp != null)
+                               if (temp != null) {
                                        temp.Emit (ec);
+                                       temp.Release (ec);
+                               }
                                
                                return;
                        }
@@ -7897,8 +7859,10 @@ namespace Mono.CSharp {
                                ig.Emit (OpCodes.Call, set);
                        }
                        
-                       if (temp != null)
+                       if (temp != null) {
                                temp.Emit (ec);
+                               temp.Release (ec);
+                       }
                }
 
                public void AddressOf (EmitContext ec, AddressOp mode)
@@ -8069,7 +8033,6 @@ namespace Mono.CSharp {
 
                public override Expression DoResolve (EmitContext ec)
                {
-                       ArrayList AllGetters = new ArrayList();
                        if (!CommonResolve (ec))
                                return null;
 
@@ -8079,37 +8042,35 @@ namespace Mono.CSharp {
                        //
                        // This is a group of properties, piles of them.  
 
-                       bool found_any = false, found_any_getters = false;
-                       Type lookup_type = indexer_type;
+                       ArrayList AllGetters = null;
 
-                       Indexers ilist = Indexers.GetIndexersForType (current_type, lookup_type);
+                       Indexers ilist = Indexers.GetIndexersForType (current_type, indexer_type);
                        if (ilist.Properties != null) {
-                               found_any = true;
+                               AllGetters = new ArrayList(ilist.Properties.Count);
                                foreach (Indexers.Indexer ix in ilist.Properties) {
                                        if (ix.Getter != null)
                                                AllGetters.Add (ix.Getter);
                                }
                        }
 
-                       if (AllGetters.Count > 0) {
-                               found_any_getters = true;
-                               get = (MethodInfo) Invocation.OverloadResolve (
-                                       ec, new MethodGroupExpr (AllGetters, loc),
-                                       arguments, false, loc);
-                       }
-
-                       if (!found_any) {
+                       if (AllGetters == null) {
                                Report.Error (21, loc, "Cannot apply indexing with [] to an expression of type `{0}'",
-                                             TypeManager.CSharpName (indexer_type));
+                                       TypeManager.CSharpName (indexer_type));
                                return null;
                        }
 
-                       if (!found_any_getters) {
+                       if (AllGetters.Count == 0) {
+                               // FIXME: we cannot simply select first one as the error message is missleading when
+                               // multiple indexers exist
+                               Indexers.Indexer first_indexer = (Indexers.Indexer)ilist.Properties[ilist.Properties.Count - 1];
                                Report.Error (154, loc, "The property or indexer `{0}' cannot be used in this context because it lacks the `get' accessor",
-                                             "XXXXXXXX");
+                                       TypeManager.GetFullNameSignature (first_indexer.PropertyInfo));
                                return null;
                        }
 
+                       get = (MethodInfo)Invocation.OverloadResolve (ec, new MethodGroupExpr (AllGetters, loc),
+                                       arguments, false, loc);
+
                        if (get == null) {
                                Invocation.Error_WrongNumArguments (loc, "this", arguments.Count);
                                return null;
@@ -8254,8 +8215,10 @@ namespace Mono.CSharp {
                        
                        Invocation.EmitCall (ec, is_base_indexer, false, instance_expr, set, set_arguments, loc, false, prepared);
                        
-                       if (temp != null)
+                       if (temp != null) {
                                temp.Emit (ec);
+                               temp.Release (ec);
+                       }
                }
                
                
@@ -8558,8 +8521,7 @@ namespace Mono.CSharp {
 
                        Type ltype = lexpr.Type;
                        if ((ltype == TypeManager.void_type) && (dim != "*")) {
-                               Report.Error (1547, Location,
-                                             "Keyword 'void' cannot be used in this context");
+                               Error_VoidInvalidInTheContext (loc);
                                return null;
                        }