Negative header ids should be valid
[mono.git] / mcs / mcs / anonymous.cs
index 91781cf97b254c4dc5c04de30715c4908ca45404..dcfbaa1d04a9753e097a39bc06e90b4d72e67282 100644 (file)
@@ -199,7 +199,7 @@ namespace Mono.CSharp {
 
                        protected override void DoEmit (EmitContext ec)
                        {
-                               hoisted_this.EmitHoistingAssignment (ec);
+                               hoisted_this.EmitAssign (ec, new CompilerGeneratedThis (ec.CurrentType, loc), false, false);
                        }
 
                        protected override void CloneTo (CloneContext clonectx, Statement target)
@@ -211,7 +211,7 @@ namespace Mono.CSharp {
                // Unique storey ID
                public readonly int ID;
 
-               public readonly Block OriginalSourceBlock;
+               public readonly ExplicitBlock OriginalSourceBlock;
 
                // A list of StoreyFieldPair with local field keeping parent storey instance
                List<StoreyFieldPair> used_parent_storeys;
@@ -219,6 +219,7 @@ namespace Mono.CSharp {
 
                // A list of hoisted parameters
                protected List<HoistedParameter> hoisted_params;
+               List<HoistedParameter> hoisted_local_params;
                protected List<HoistedVariable> hoisted_locals;
 
                // Hoisted this
@@ -227,7 +228,9 @@ namespace Mono.CSharp {
                // Local variable which holds this storey instance
                public Expression Instance;
 
-               public AnonymousMethodStorey (Block block, TypeDefinition parent, MemberBase host, TypeParameters tparams, string name, MemberKind kind)
+               bool initialize_hoisted_this;
+
+               public AnonymousMethodStorey (ExplicitBlock block, TypeDefinition parent, MemberBase host, TypeParameters tparams, string name, MemberKind kind)
                        : base (parent, MakeMemberName (host, name, parent.Module.CounterAnonymousContainers, tparams, block.StartLocation),
                                tparams, 0, kind)
                {
@@ -238,18 +241,10 @@ namespace Mono.CSharp {
                public void AddCapturedThisField (EmitContext ec)
                {
                        TypeExpr type_expr = new TypeExpression (ec.CurrentType, Location);
-                       Field f = AddCompilerGeneratedField ("<>f__this", type_expr);
-                       f.Define ();
+                       Field f = AddCompilerGeneratedField ("$this", type_expr);
                        hoisted_this = new HoistedThis (this, f);
 
-                       // Inflated type instance has to be updated manually
-                       if (Instance.Type is InflatedTypeSpec) {
-                               var inflator = new TypeParameterInflator (this, Instance.Type, TypeParameterSpec.EmptyTypes, TypeSpec.EmptyTypes);
-                               Instance.Type.MemberCache.AddMember (f.Spec.InflateMember (inflator));
-
-                               inflator = new TypeParameterInflator (this, f.Parent.CurrentType, TypeParameterSpec.EmptyTypes, TypeSpec.EmptyTypes);
-                               f.Parent.CurrentType.MemberCache.AddMember (f.Spec.InflateMember (inflator));
-                       }
+                       initialize_hoisted_this = true;
                }
 
                public Field AddCapturedVariable (string name, TypeSpec type)
@@ -309,38 +304,93 @@ namespace Mono.CSharp {
                        used_parent_storeys.Add (new StoreyFieldPair (storey, f));
                }
 
-               public void CaptureLocalVariable (ResolveContext ec, LocalVariable local_info)
+               public void CaptureLocalVariable (ResolveContext ec, LocalVariable localVariable)
                {
-                       ec.CurrentBlock.Explicit.HasCapturedVariable = true;
-                       if (ec.CurrentBlock.Explicit != local_info.Block.Explicit)
-                               AddReferenceFromChildrenBlock (ec.CurrentBlock.Explicit);
+                       if (this is StateMachine) {
+                               if (ec.CurrentBlock.ParametersBlock != localVariable.Block.ParametersBlock)
+                                       ec.CurrentBlock.Explicit.HasCapturedVariable = true;
+                       } else {
+                               ec.CurrentBlock.Explicit.HasCapturedVariable = true;
+                       }
 
-                       if (local_info.HoistedVariant != null)
-                               return;
+                       var hoisted = localVariable.HoistedVariant;
+                       if (hoisted != null && hoisted.Storey != this && hoisted.Storey is StateMachine) {
+                               // TODO: It's too late the field is defined in HoistedLocalVariable ctor
+                               hoisted.Storey.hoisted_locals.Remove (hoisted);
+                               hoisted = null;
+                       }
+
+                       if (hoisted == null) {
+                               hoisted = new HoistedLocalVariable (this, localVariable, GetVariableMangledName (localVariable));
+                               localVariable.HoistedVariant = hoisted;
 
-                       HoistedVariable var = new HoistedLocalVariable (this, local_info, GetVariableMangledName (local_info));
-                       local_info.HoistedVariant = var;
+                               if (hoisted_locals == null)
+                                       hoisted_locals = new List<HoistedVariable> ();
 
-                       if (hoisted_locals == null)
-                               hoisted_locals = new List<HoistedVariable> ();
+                               hoisted_locals.Add (hoisted);
+                       }
 
-                       hoisted_locals.Add (var);
+                       if (ec.CurrentBlock.Explicit != localVariable.Block.Explicit && !(hoisted.Storey is StateMachine))
+                               hoisted.Storey.AddReferenceFromChildrenBlock (ec.CurrentBlock.Explicit);
                }
 
-               public void CaptureParameter (ResolveContext ec, ParameterReference param_ref)
+               public void CaptureParameter (ResolveContext ec, ParametersBlock.ParameterInfo parameterInfo, ParameterReference parameterReference)
                {
-                       ec.CurrentBlock.Explicit.HasCapturedVariable = true;
-                       AddReferenceFromChildrenBlock (ec.CurrentBlock.Explicit);
+                       if (!(this is StateMachine)) {
+                               ec.CurrentBlock.Explicit.HasCapturedVariable = true;
+                       }
 
-                       if (param_ref.GetHoistedVariable (ec) != null)
-                               return;
+                       var hoisted = parameterInfo.Parameter.HoistedVariant;
+
+                       if (parameterInfo.Block.StateMachine != null) {
+                               //
+                               // Another storey in same block exists but state machine does not
+                               // have parameter captured. We need to add it there as well to
+                               // proxy parameter value correctly.
+                               //
+                               if (hoisted == null && parameterInfo.Block.StateMachine != this) {
+                                       var storey = parameterInfo.Block.StateMachine;
 
-                       if (hoisted_params == null)
-                               hoisted_params = new List<HoistedParameter> (2);
+                                       hoisted = new HoistedParameter (storey, parameterReference);
+                                       parameterInfo.Parameter.HoistedVariant = hoisted;
 
-                       var expr = new HoistedParameter (this, param_ref);
-                       param_ref.Parameter.HoistedVariant = expr;
-                       hoisted_params.Add (expr);
+                                       if (storey.hoisted_params == null)
+                                               storey.hoisted_params = new List<HoistedParameter> ();
+
+                                       storey.hoisted_params.Add (hoisted);
+                               }
+
+                               //
+                               // Lift captured parameter from value type storey to reference type one. Otherwise
+                               // any side effects would be done on a copy
+                               //
+                               if (hoisted != null && hoisted.Storey != this && hoisted.Storey is StateMachine) {
+                                       if (hoisted_local_params == null)
+                                               hoisted_local_params = new List<HoistedParameter> ();
+
+                                       hoisted_local_params.Add (hoisted);
+                                       hoisted = null;
+                               }
+                       }
+
+                       if (hoisted == null) {
+                               hoisted = new HoistedParameter (this, parameterReference);
+                               parameterInfo.Parameter.HoistedVariant = hoisted;
+
+                               if (hoisted_params == null)
+                                       hoisted_params = new List<HoistedParameter> ();
+
+                               hoisted_params.Add (hoisted);
+                       }
+
+                       //
+                       // Register link between current block and parameter storey. It will
+                       // be used when setting up storey definition to deploy storey reference
+                       // when parameters are used from multiple blocks
+                       //
+                       if (ec.CurrentBlock.Explicit != parameterInfo.Block) {
+                               hoisted.Storey.AddReferenceFromChildrenBlock (ec.CurrentBlock.Explicit);
+                       }
                }
 
                TypeExpr CreateStoreyTypeExpression (EmitContext ec)
@@ -437,7 +487,7 @@ namespace Mono.CSharp {
                        // When the current context is async (or iterator) lift local storey
                        // instantiation to the currect storey
                        //
-                       if (ec.CurrentAnonymousMethod is StateMachineInitializer) {
+                       if (ec.CurrentAnonymousMethod is StateMachineInitializer && (block.HasYield || block.HasAwait)) {
                                //
                                // Unfortunately, normal capture mechanism could not be used because we are
                                // too late in the pipeline and standart assign cannot be used either due to
@@ -496,10 +546,10 @@ namespace Mono.CSharp {
                        }
 
                        //
-                       // Define hoisted `this' in top-level storey only 
+                       // Initialize hoisted `this' only once, everywhere else will be
+                       // referenced indirectly
                        //
-                       if (OriginalSourceBlock.Explicit.HasCapturedThis && !(Parent is AnonymousMethodStorey)) {
-                               AddCapturedThisField (ec);
+                       if (initialize_hoisted_this) {
                                rc.CurrentBlock.AddScopeStatement (new ThisInitializer (hoisted_this));
                        }
 
@@ -516,9 +566,20 @@ namespace Mono.CSharp {
                        ec.CurrentAnonymousMethod = ae;
                }
 
-               protected virtual void EmitHoistedParameters (EmitContext ec, IList<HoistedParameter> hoisted)
+               protected virtual void EmitHoistedParameters (EmitContext ec, List<HoistedParameter> hoisted)
                {
                        foreach (HoistedParameter hp in hoisted) {
+                               //
+                               // Parameters could be proxied via local fields for value type storey
+                               //
+                               if (hoisted_local_params != null) {
+                                       var local_param = hoisted_local_params.Find (l => l.Parameter.Parameter == hp.Parameter.Parameter);
+                                       var source = new FieldExpr (local_param.Field, Location);
+                                       source.InstanceExpression = new CompilerGeneratedThis (CurrentType, Location);
+                                       hp.EmitAssign (ec, source, false, false);
+                                       continue;
+                               }
+
                                hp.EmitHoistingAssignment (ec);
                        }
                }
@@ -591,7 +652,12 @@ namespace Mono.CSharp {
                }
 
                public HoistedThis HoistedThis {
-                       get { return hoisted_this; }
+                       get {
+                               return hoisted_this;
+                       }
+                       set {
+                               hoisted_this = value;
+                       }
                }
 
                public IList<ExplicitBlock> ReferencesFromChildrenBlock {
@@ -635,7 +701,7 @@ namespace Mono.CSharp {
                        public override void Emit (EmitContext ec)
                        {
                                ResolveContext rc = new ResolveContext (ec.MemberContext);
-                               Expression e = hv.GetFieldExpression (ec).CreateExpressionTree (rc);
+                               Expression e = hv.GetFieldExpression (ec).CreateExpressionTree (rc, false);
                                // This should never fail
                                e = e.Resolve (rc);
                                if (e != null)
@@ -659,6 +725,12 @@ namespace Mono.CSharp {
                        this.field = field;
                }
 
+               public AnonymousMethodStorey Storey {
+                       get {
+                               return storey;
+                       }
+               }
+
                public void AddressOf (EmitContext ec, AddressOp mode)
                {
                        GetFieldExpression (ec).AddressOf (ec, mode);
@@ -740,10 +812,10 @@ namespace Mono.CSharp {
 
        public class HoistedParameter : HoistedVariable
        {
-               sealed class HoistedFieldAssign : Assign
+               sealed class HoistedFieldAssign : CompilerAssign
                {
                        public HoistedFieldAssign (Expression target, Expression source)
-                               : base (target, source, source.Location)
+                               : base (target, source, target.Location)
                        {
                        }
 
@@ -771,23 +843,32 @@ namespace Mono.CSharp {
                        this.parameter = hp.parameter;
                }
 
+               #region Properties
+
                public Field Field {
                        get {
                                return field;
                        }
                }
 
+               public ParameterReference Parameter {
+                       get {
+                               return parameter;
+                       }
+               }
+
+               #endregion
+
                public void EmitHoistingAssignment (EmitContext ec)
                {
                        //
                        // Remove hoisted redirection to emit assignment from original parameter
                        //
-                       HoistedVariable temp = parameter.Parameter.HoistedVariant;
+                       var temp = parameter.Parameter.HoistedVariant;
                        parameter.Parameter.HoistedVariant = null;
 
-                       Assign a = new HoistedFieldAssign (GetFieldExpression (ec), parameter);
-                       if (a.Resolve (new ResolveContext (ec.MemberContext)) != null)
-                               a.EmitStatement (ec);
+                       var a = new HoistedFieldAssign (GetFieldExpression (ec), parameter);
+                       a.EmitStatement (ec);
 
                        parameter.Parameter.HoistedVariant = temp;
                }
@@ -813,13 +894,6 @@ namespace Mono.CSharp {
                                return field;
                        }
                }
-
-               public void EmitHoistingAssignment (EmitContext ec)
-               {
-                       SimpleAssign a = new SimpleAssign (GetFieldExpression (ec), new CompilerGeneratedThis (ec.CurrentType, field.Location));
-                       if (a.Resolve (new ResolveContext (ec.MemberContext)) != null)
-                               a.EmitStatement (ec);
-               }
        }
 
        //
@@ -886,6 +960,10 @@ namespace Mono.CSharp {
                        }
                }
 
+               public ReportPrinter TypeInferenceReportPrinter {
+                       get; set;
+               }
+
                #endregion
 
                //
@@ -896,7 +974,13 @@ namespace Mono.CSharp {
                {
                        using (ec.With (ResolveContext.Options.InferReturnType, false)) {
                                using (ec.Set (ResolveContext.Options.ProbingMode)) {
-                                       return Compatible (ec, delegate_type) != null;
+                                       var prev = ec.Report.SetPrinter (TypeInferenceReportPrinter ?? new NullReportPrinter ());
+
+                                       var res = Compatible (ec, delegate_type) != null;
+
+                                       ec.Report.SetPrinter (prev);
+
+                                       return res;
                                }
                        }
                }
@@ -1011,9 +1095,19 @@ namespace Mono.CSharp {
                        if (d_params.Count != Parameters.Count)
                                return false;
 
+                       var ptypes = Parameters.Types;
+                       var dtypes = d_params.Types;
                        for (int i = 0; i < Parameters.Count; ++i) {
-                               if (type_inference.ExactInference (Parameters.Types[i], d_params.Types[i]) == 0)
+                               if (type_inference.ExactInference (ptypes[i], dtypes[i]) == 0) {
+                                       //
+                                       // Continue when 0 (quick path) does not mean inference failure. Checking for
+                                       // same type handles cases like int -> int
+                                       //
+                                       if (ptypes[i] == dtypes[i])
+                                               continue;
+
                                        return false;
+                               }
                        }
 
                        return true;
@@ -1030,16 +1124,23 @@ namespace Mono.CSharp {
                        }
 
                        using (ec.Set (ResolveContext.Options.ProbingMode | ResolveContext.Options.InferReturnType)) {
-                               var body = CompatibleMethodBody (ec, tic, InternalType.Arglist, delegate_type);
-                               if (body != null) {
-                                       if (Block.IsAsync) {
-                                               AsyncInitializer.Create (ec, body.Block, body.Parameters, ec.CurrentMemberDefinition.Parent.PartialContainer, null, loc);
-                                       }
+                               ReportPrinter prev;
+                               if (TypeInferenceReportPrinter != null) {
+                                       prev = ec.Report.SetPrinter (TypeInferenceReportPrinter);
+                               } else {
+                                       prev = null;
+                               }
 
+                               var body = CompatibleMethodBody (ec, tic, null, delegate_type);
+                               if (body != null) {
                                        am = body.Compatible (ec, body);
                                } else {
                                        am = null;
                                }
+
+                               if (TypeInferenceReportPrinter != null) {
+                                       ec.Report.SetPrinter (prev);
+                               }
                        }
 
                        if (am == null)
@@ -1122,18 +1223,6 @@ namespace Mono.CSharp {
                                                        am = CreateExpressionTree (ec, delegate_type);
                                        }
                                } else {
-                                       if (Block.IsAsync) {
-                                               var rt = body.ReturnType;
-                                               if (rt.Kind != MemberKind.Void &&
-                                                       rt != ec.Module.PredefinedTypes.Task.TypeSpec &&
-                                                       !rt.IsGenericTask) {
-                                                       ec.Report.Error (4010, loc, "Cannot convert async {0} to delegate type `{1}'",
-                                                               GetSignatureForError (), type.GetSignatureForError ());
-                                               }
-
-                                               AsyncInitializer.Create (ec, body.Block, body.Parameters, ec.CurrentMemberDefinition.Parent.PartialContainer, rt, loc);
-                                       }
-
                                        am = body.Compatible (ec);
                                }
                        } catch (CompletionResult) {
@@ -1261,7 +1350,19 @@ namespace Mono.CSharp {
 
                        ParametersBlock b = ec.IsInProbingMode ? (ParametersBlock) Block.PerformClone () : Block;
 
-                       return CompatibleMethodFactory (return_type, delegate_type, p, b);
+                       if (b.IsAsync) {
+                               var rt = return_type;
+                               if (rt != null && rt.Kind != MemberKind.Void && rt != ec.Module.PredefinedTypes.Task.TypeSpec && !rt.IsGenericTask) {
+                                       ec.Report.Error (4010, loc, "Cannot convert async {0} to delegate type `{1}'",
+                                               GetSignatureForError (), delegate_type.GetSignatureForError ());
+
+                                       return null;
+                               }
+
+                               b = b.ConvertToAsyncTask (ec, ec.CurrentMemberDefinition.Parent.PartialContainer, p, return_type, loc);
+                       }
+
+                       return CompatibleMethodFactory (return_type ?? InternalType.Arglist, delegate_type, p, b);
                }
 
                protected virtual AnonymousMethodBody CompatibleMethodFactory (TypeSpec return_type, TypeSpec delegate_type, ParametersCompiled p, ParametersBlock b)
@@ -1361,6 +1462,15 @@ namespace Mono.CSharp {
                public abstract bool IsIterator { get; }
                public abstract AnonymousMethodStorey Storey { get; }
 
+               //
+               // The block that makes up the body for the anonymous method
+               //
+               public ParametersBlock Block {
+                       get {
+                               return block;
+                       }
+               }
+
                public AnonymousExpression Compatible (ResolveContext ec)
                {
                        return Compatible (ec, this);
@@ -1392,8 +1502,15 @@ namespace Mono.CSharp {
                        if (ec.HasSet (ResolveContext.Options.ExpressionTreeConversion))
                                flags |= ResolveContext.Options.ExpressionTreeConversion;
 
+                       if (ec.HasSet (ResolveContext.Options.BaseInitializer))
+                               flags |= ResolveContext.Options.BaseInitializer;
+
                        aec.Set (flags);
 
+                       var bc = ec as BlockContext;
+                       if (bc != null)
+                               aec.FlowOffset = bc.FlowOffset;
+
                        var errors = ec.Report.Errors;
 
                        bool res = Block.Resolve (ec.CurrentBranching, aec, null);
@@ -1434,16 +1551,6 @@ namespace Mono.CSharp {
                                b = b.Parent == null ? null : b.Parent.Explicit;
                        } while (b != null);
                }
-
-               //
-               // The block that makes up the body for the anonymous method
-               //
-               public ParametersBlock Block {
-                       get {
-                               return block;
-                       }
-               }
-
        }
 
        public class AnonymousMethodBody : AnonymousExpression
@@ -1531,17 +1638,47 @@ namespace Mono.CSharp {
                        //
 
                        Modifiers modifiers;
-                       if (Block.HasCapturedVariable || Block.HasCapturedThis) {
-                               storey = FindBestMethodStorey ();
+                       TypeDefinition parent = null;
+
+                       var src_block = Block.Original.Explicit;
+                       if (src_block.HasCapturedVariable || src_block.HasCapturedThis) {
+                               parent = storey = FindBestMethodStorey ();
+
+                               if (storey == null) {
+                                       var sm = src_block.ParametersBlock.TopBlock.StateMachine;
+
+                                       //
+                                       // Remove hoisted this demand when simple instance method is enough (no hoisted variables only this)
+                                       //
+                                       if (src_block.HasCapturedThis && src_block.ParametersBlock.StateMachine == null) {
+                                               src_block.ParametersBlock.TopBlock.RemoveThisReferenceFromChildrenBlock (src_block);
+
+                                               //
+                                               // Special case where parent class is used to emit instance method
+                                               // because currect storey is of value type (async host) and we don't
+                                               // want to create another childer storey to host this reference only
+                                               //
+                                               if (sm != null && sm.Kind == MemberKind.Struct)
+                                                       parent = sm.Parent.PartialContainer;
+                                       }
+
+                                       //
+                                       // For iterators we can host everything in one class
+                                       //
+                                       if (sm is IteratorStorey)
+                                               parent = storey = sm;
+                               }
+
                                modifiers = storey != null ? Modifiers.INTERNAL : Modifiers.PRIVATE;
                        } else {
                                if (ec.CurrentAnonymousMethod != null)
-                                       storey = ec.CurrentAnonymousMethod.Storey;
+                                       parent = storey = ec.CurrentAnonymousMethod.Storey;
 
                                modifiers = Modifiers.STATIC | Modifiers.PRIVATE;
                        }
 
-                       var parent = storey != null ? storey : ec.CurrentTypeDefinition.Parent.PartialContainer;
+                       if (parent == null)
+                               parent = ec.CurrentTypeDefinition.Parent.PartialContainer;
 
                        string name = CompilerGeneratedContainer.MakeName (parent != storey ? block_name : null,
                                "m", null, ec.Module.CounterAnonymousMethods++);
@@ -1646,6 +1783,17 @@ namespace Mono.CSharp {
                                }
                        } else {
                                ec.EmitThis ();
+
+                               //
+                               // Special case for value type storey where this is not lifted but
+                               // droped off to parent class
+                               //
+                               for (var b = Block.Parent; b != null; b = b.Parent) {
+                                       if (b.ParametersBlock.StateMachine != null) {
+                                               ec.Emit (OpCodes.Ldfld, b.ParametersBlock.StateMachine.HoistedThis.Field.Spec);
+                                               break;
+                                       }
+                               }
                        }
 
                        var delegate_method = method.Spec;
@@ -1656,9 +1804,7 @@ namespace Mono.CSharp {
                                // Mutate anonymous method instance type if we are in nested
                                // hoisted generic anonymous method storey
                                //
-                               if (ec.CurrentAnonymousMethod != null &&
-                                       ec.CurrentAnonymousMethod.Storey != null &&
-                                       ec.CurrentAnonymousMethod.Storey.Mutator != null) {
+                               if (ec.IsAnonymousStoreyMutateRequired) {
                                        t = storey.Mutator.Mutate (t);
                                }
 
@@ -1772,7 +1918,7 @@ namespace Mono.CSharp {
                        c.Block = new ToplevelBlock (parent.Module.Compiler, c.ParameterInfo, loc);
 
                        // 
-                       // Create fields and contructor body with field initialization
+                       // Create fields and constructor body with field initialization
                        //
                        bool error = false;
                        for (int i = 0; i < parameters.Count; ++i) {
@@ -1875,11 +2021,11 @@ namespace Mono.CSharp {
 
                                IntConstant FNV_prime = new IntConstant (Compiler.BuiltinTypes, 16777619, loc);                         
                                rs_hashcode = new Binary (Binary.Operator.Multiply,
-                                       new Binary (Binary.Operator.ExclusiveOr, rs_hashcode, field_hashcode, loc),
-                                       FNV_prime, loc);
+                                       new Binary (Binary.Operator.ExclusiveOr, rs_hashcode, field_hashcode),
+                                       FNV_prime);
 
                                Expression field_to_string = new Conditional (new BooleanExpression (new Binary (Binary.Operator.Inequality,
-                                       new MemberAccess (new This (f.Location), f.Name), new NullLiteral (loc), loc)),
+                                       new MemberAccess (new This (f.Location), f.Name), new NullLiteral (loc))),
                                        new Invocation (new MemberAccess (
                                                new MemberAccess (new This (f.Location), f.Name), "ToString"), null),
                                        new StringConstant (Compiler.BuiltinTypes, string.Empty, loc), loc);
@@ -1890,9 +2036,7 @@ namespace Mono.CSharp {
                                                string_concat,
                                                new Binary (Binary.Operator.Addition,
                                                        new StringConstant (Compiler.BuiltinTypes, " " + p.Name + " = ", loc),
-                                                       field_to_string,
-                                                       loc),
-                                               loc);
+                                                       field_to_string));
                                        continue;
                                }
 
@@ -1902,18 +2046,15 @@ namespace Mono.CSharp {
                                string_concat = new Binary (Binary.Operator.Addition,
                                        new Binary (Binary.Operator.Addition,
                                                string_concat,
-                                               new StringConstant (Compiler.BuiltinTypes, ", " + p.Name + " = ", loc),
-                                               loc),
-                                       field_to_string,
-                                       loc);
+                                               new StringConstant (Compiler.BuiltinTypes, ", " + p.Name + " = ", loc)),
+                                       field_to_string);
 
-                               rs_equals = new Binary (Binary.Operator.LogicalAnd, rs_equals, field_equal, loc);
+                               rs_equals = new Binary (Binary.Operator.LogicalAnd, rs_equals, field_equal);
                        }
 
                        string_concat = new Binary (Binary.Operator.Addition,
                                string_concat,
-                               new StringConstant (Compiler.BuiltinTypes, " }", loc),
-                               loc);
+                               new StringConstant (Compiler.BuiltinTypes, " }", loc));
 
                        //
                        // Equals (object obj) override
@@ -1924,9 +2065,9 @@ namespace Mono.CSharp {
                                        new As (equals_block.GetParameterReference (0, loc),
                                                current_type, loc), loc)));
 
-                       Expression equals_test = new Binary (Binary.Operator.Inequality, other_variable, new NullLiteral (loc), loc);
+                       Expression equals_test = new Binary (Binary.Operator.Inequality, other_variable, new NullLiteral (loc));
                        if (rs_equals != null)
-                               equals_test = new Binary (Binary.Operator.LogicalAnd, equals_test, rs_equals, loc);
+                               equals_test = new Binary (Binary.Operator.LogicalAnd, equals_test, rs_equals);
                        equals_block.AddStatement (new Return (equals_test, loc));
 
                        equals.Block = equals_block;
@@ -1968,19 +2109,19 @@ namespace Mono.CSharp {
                        var hash_variable = new LocalVariableReference (li_hash, loc);
                        hashcode_block.AddStatement (new StatementExpression (
                                new CompoundAssign (Binary.Operator.Addition, hash_variable,
-                                       new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (Compiler.BuiltinTypes, 13, loc), loc), loc)));
+                                       new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (Compiler.BuiltinTypes, 13, loc)))));
                        hashcode_block.AddStatement (new StatementExpression (
                                new CompoundAssign (Binary.Operator.ExclusiveOr, hash_variable,
-                                       new Binary (Binary.Operator.RightShift, hash_variable, new IntConstant (Compiler.BuiltinTypes, 7, loc), loc), loc)));
+                                       new Binary (Binary.Operator.RightShift, hash_variable, new IntConstant (Compiler.BuiltinTypes, 7, loc)))));
                        hashcode_block.AddStatement (new StatementExpression (
                                new CompoundAssign (Binary.Operator.Addition, hash_variable,
-                                       new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (Compiler.BuiltinTypes, 3, loc), loc), loc)));
+                                       new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (Compiler.BuiltinTypes, 3, loc)))));
                        hashcode_block.AddStatement (new StatementExpression (
                                new CompoundAssign (Binary.Operator.ExclusiveOr, hash_variable,
-                                       new Binary (Binary.Operator.RightShift, hash_variable, new IntConstant (Compiler.BuiltinTypes, 17, loc), loc), loc)));
+                                       new Binary (Binary.Operator.RightShift, hash_variable, new IntConstant (Compiler.BuiltinTypes, 17, loc)))));
                        hashcode_block.AddStatement (new StatementExpression (
                                new CompoundAssign (Binary.Operator.Addition, hash_variable,
-                                       new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (Compiler.BuiltinTypes, 5, loc), loc), loc)));
+                                       new Binary (Binary.Operator.LeftShift, hash_variable, new IntConstant (Compiler.BuiltinTypes, 5, loc)))));
 
                        hashcode_block.AddStatement (new Return (hash_variable, loc));
                        hashcode.Block = hashcode_top;