Negative header ids should be valid
[mono.git] / mcs / mcs / anonymous.cs
index 0c05744fb9ff976d35d62978bbe6218a1927455e..dcfbaa1d04a9753e097a39bc06e90b4d72e67282 100644 (file)
@@ -314,7 +314,7 @@ namespace Mono.CSharp {
                        }
 
                        var hoisted = localVariable.HoistedVariant;
-                       if (hoisted != null && hoisted.Storey != this && hoisted.Storey.Kind == MemberKind.Struct) {
+                       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;
@@ -330,7 +330,7 @@ namespace Mono.CSharp {
                                hoisted_locals.Add (hoisted);
                        }
 
-                       if (ec.CurrentBlock.Explicit != localVariable.Block.Explicit)
+                       if (ec.CurrentBlock.Explicit != localVariable.Block.Explicit && !(hoisted.Storey is StateMachine))
                                hoisted.Storey.AddReferenceFromChildrenBlock (ec.CurrentBlock.Explicit);
                }
 
@@ -342,7 +342,7 @@ namespace Mono.CSharp {
 
                        var hoisted = parameterInfo.Parameter.HoistedVariant;
 
-                       if (parameterInfo.Block.StateMachine is AsyncTaskStorey) {
+                       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
@@ -364,7 +364,7 @@ namespace Mono.CSharp {
                                // 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.Kind == MemberKind.Struct) {
+                               if (hoisted != null && hoisted.Storey != this && hoisted.Storey is StateMachine) {
                                        if (hoisted_local_params == null)
                                                hoisted_local_params = new List<HoistedParameter> ();
 
@@ -487,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
@@ -701,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)
@@ -815,7 +815,7 @@ namespace Mono.CSharp {
                sealed class HoistedFieldAssign : CompilerAssign
                {
                        public HoistedFieldAssign (Expression target, Expression source)
-                               : base (target, source, source.Location)
+                               : base (target, source, target.Location)
                        {
                        }
 
@@ -960,6 +960,10 @@ namespace Mono.CSharp {
                        }
                }
 
+               public ReportPrinter TypeInferenceReportPrinter {
+                       get; set;
+               }
+
                #endregion
 
                //
@@ -970,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;
                                }
                        }
                }
@@ -1085,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;
@@ -1104,12 +1124,23 @@ namespace Mono.CSharp {
                        }
 
                        using (ec.Set (ResolveContext.Options.ProbingMode | ResolveContext.Options.InferReturnType)) {
+                               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)
@@ -1471,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);
@@ -1610,9 +1648,9 @@ namespace Mono.CSharp {
                                        var sm = src_block.ParametersBlock.TopBlock.StateMachine;
 
                                        //
-                                       // Remove hoisted this demand when simple instance method is enough
+                                       // Remove hoisted this demand when simple instance method is enough (no hoisted variables only this)
                                        //
-                                       if (src_block.HasCapturedThis) {
+                                       if (src_block.HasCapturedThis && src_block.ParametersBlock.StateMachine == null) {
                                                src_block.ParametersBlock.TopBlock.RemoveThisReferenceFromChildrenBlock (src_block);
 
                                                //
@@ -1983,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);
@@ -1998,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;
                                }
 
@@ -2010,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
@@ -2032,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;
@@ -2076,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;