Report less cascading errors
[mono.git] / mcs / mcs / dynamic.cs
index 2977df9534d05cabc6f802e1e06087d224af5947..0246c43a1b67fa9cba25972b19e283fd7558a5b2 100644 (file)
@@ -6,13 +6,14 @@
 // Dual licensed under the terms of the MIT X11 or GNU GPL
 //
 // Copyright 2009 Novell, Inc
+// Copyright 2011 Xamarin Inc.
 //
 
 using System;
 using System.Linq;
 using SLE = System.Linq.Expressions;
 
-#if NET_4_0
+#if NET_4_0 || MONODROID
 using System.Dynamic;
 #endif
 
@@ -45,19 +46,13 @@ namespace Mono.CSharp
                public DynamicTypeExpr (Location loc)
                {
                        this.loc = loc;
-
-                       type = InternalType.Dynamic;
-                       eclass = ExprClass.Type;
-               }
-
-               public override bool CheckAccessLevel (IMemberContext ds)
-               {
-                       return true;
                }
 
-               protected override TypeExpr DoResolveAsTypeStep (IMemberContext ec)
+               public override TypeSpec ResolveAsType (IMemberContext ec)
                {
-                       return this;
+                       eclass = ExprClass.Type;
+                       type = ec.Module.Compiler.BuiltinTypes.Dynamic;
+                       return type;
                }
        }
 
@@ -68,7 +63,7 @@ namespace Mono.CSharp
        //
        public class RuntimeValueExpression : Expression, IDynamicAssign, IMemoryLocation
        {
-#if !NET_4_0
+#if !NET_4_0 && !MONODROID
                public class DynamicMetaObject
                {
                        public TypeSpec RuntimeType;
@@ -101,9 +96,14 @@ namespace Mono.CSharp
                        throw new NotImplementedException ();
                }
 
+               public override bool ContainsEmitWithAwait ()
+               {
+                       throw new NotSupportedException ();
+               }
+
                public override Expression CreateExpressionTree (ResolveContext ec)
                {
-                       throw new NotImplementedException ();
+                       throw new NotSupportedException ();
                }
 
                protected override Expression DoResolve (ResolveContext ec)
@@ -128,7 +128,7 @@ namespace Mono.CSharp
                        throw new NotImplementedException ();
                }
 
-               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
+               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
                {
                        throw new NotImplementedException ();
                }
@@ -142,17 +142,22 @@ namespace Mono.CSharp
 
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-#if NET_4_0            
-                       if (type.IsStruct && !obj.Expression.Type.IsValueType)
-                               return SLE.Expression.Unbox (obj.Expression, type.GetMetaInfo ());
+#if STATIC
+                       return base.MakeExpression (ctx);
+#else
 
-                       if (obj.Expression.NodeType == SLE.ExpressionType.Parameter) {
-                               if (((SLE.ParameterExpression) obj.Expression).IsByRef)
-                                       return obj.Expression;
-                       }
-#endif
+#if NET_4_0 || MONODROID
+                               if (type.IsStruct && !obj.Expression.Type.IsValueType)
+                                       return SLE.Expression.Unbox (obj.Expression, type.GetMetaInfo ());
 
-                       return SLE.Expression.Convert (obj.Expression, type.GetMetaInfo ());
+                               if (obj.Expression.NodeType == SLE.ExpressionType.Parameter) {
+                                       if (((SLE.ParameterExpression) obj.Expression).IsByRef)
+                                               return obj.Expression;
+                               }
+       #endif
+
+                               return SLE.Expression.Convert (obj.Expression, type.GetMetaInfo ());
+#endif
                }
        }
 
@@ -176,10 +181,14 @@ namespace Mono.CSharp
                        return this;
                }
 
-#if NET_4_0
+#if NET_4_0 || MONODROID
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
+#if STATIC
+                       return base.MakeExpression (ctx);
+#else
                        return SLE.Expression.Block (expr.MakeExpression (ctx), SLE.Expression.Default (type.GetMetaInfo ()));
+#endif
                }
 #endif
        }
@@ -223,13 +232,14 @@ namespace Mono.CSharp
                        {
                                this.flags = flags;
                                this.statement = statement;
+                               eclass = 0;
                        }
 
                        protected override Expression DoResolve (ResolveContext ec)
                        {
-                               Child = new IntConstant ((int) (flags | statement.flags), statement.loc).Resolve (ec);
+                               Child = new IntConstant (ec.BuiltinTypes, (int) (flags | statement.flags), statement.loc);
 
-                               type = TypeManager.binder_flags;
+                               type = ec.Module.PredefinedTypes.BinderFlags.Resolve ();
                                eclass = Child.eclass;
                                return this;
                        }
@@ -242,6 +252,9 @@ namespace Mono.CSharp
                // Used by BinderFlags
                protected CSharpBinderFlags flags;
 
+               TypeSpec binder_type;
+               TypeParameters context_mvars;
+
                public DynamicExpressionStatement (IDynamicBinder binder, Arguments args, Location loc)
                {
                        this.binder = binder;
@@ -255,10 +268,9 @@ namespace Mono.CSharp
                        }
                }
 
-               FieldSpec CreateSiteField (EmitContext ec, FullNamedExpression type)
+               public override bool ContainsEmitWithAwait ()
                {
-                       var site_container = ec.CreateDynamicSite ();
-                       return site_container.CreateCallSiteField (type, loc);
+                       return arguments.ContainsEmitWithAwait ();
                }
 
                public override Expression CreateExpressionTree (ResolveContext ec)
@@ -267,42 +279,30 @@ namespace Mono.CSharp
                        return null;
                }
 
-               protected override Expression DoResolve (ResolveContext ec)
+               protected override Expression DoResolve (ResolveContext rc)
                {
-                       if (DoResolveCore (ec))
-                               binder_expr = binder.CreateCallSiteBinder (ec, arguments);
+                       if (DoResolveCore (rc))
+                               binder_expr = binder.CreateCallSiteBinder (rc, arguments);
 
                        return this;
                }
 
                protected bool DoResolveCore (ResolveContext rc)
                {
-                       int errors = rc.Report.Errors;
-
-                       if (TypeManager.binder_type == null) {
-                               var t = TypeManager.CoreLookupType (rc.Compiler,
-                                       "Microsoft.CSharp.RuntimeBinder", "Binder", MemberKind.Class, true);
-                               if (t != null)
-                                       TypeManager.binder_type = new TypeExpression (t, Location.Null);
-                       }
+                       if (rc.CurrentTypeParameters != null && rc.CurrentTypeParameters[0].IsMethodTypeParameter)
+                               context_mvars = rc.CurrentTypeParameters;
 
-                       if (TypeManager.call_site_type == null)
-                               TypeManager.call_site_type = TypeManager.CoreLookupType (rc.Compiler,
-                                       "System.Runtime.CompilerServices", "CallSite", MemberKind.Class, true);
-
-                       if (TypeManager.generic_call_site_type == null)
-                               TypeManager.generic_call_site_type = TypeManager.CoreLookupType (rc.Compiler,
-                                       "System.Runtime.CompilerServices", "CallSite", 1, MemberKind.Class, true);
+                       int errors = rc.Report.Errors;
+                       var pt = rc.Module.PredefinedTypes;
 
-                       if (TypeManager.binder_flags == null) {
-                               TypeManager.binder_flags = TypeManager.CoreLookupType (rc.Compiler,
-                                       "Microsoft.CSharp.RuntimeBinder", "CSharpBinderFlags", MemberKind.Enum, true);
-                       }
+                       binder_type = pt.Binder.Resolve ();
+                       pt.CallSite.Resolve ();
+                       pt.CallSiteGeneric.Resolve ();
 
                        eclass = ExprClass.Value;
 
                        if (type == null)
-                               type = InternalType.Dynamic;
+                               type = rc.BuiltinTypes.Dynamic;
 
                        if (rc.Report.Errors == errors)
                                return true;
@@ -324,121 +324,223 @@ namespace Mono.CSharp
 
                protected void EmitCall (EmitContext ec, Expression binder, Arguments arguments, bool isStatement)
                {
+                       //
+                       // This method generates all internal infrastructure for a dynamic call. The
+                       // reason why it's quite complicated is the mixture of dynamic and anonymous
+                       // methods. Dynamic itself requires a temporary class (ContainerX) and anonymous
+                       // methods can generate temporary storey as well (AnonStorey). Handling MVAR
+                       // type parameters rewrite is non-trivial in such case as there are various
+                       // combinations possible therefore the mutator is not straightforward. Secondly
+                       // we need to keep both MVAR(possibly VAR for anon storey) and type VAR to emit
+                       // correct Site field type and its access from EmitContext.
+                       //
+
                        int dyn_args_count = arguments == null ? 0 : arguments.Count;
-                       TypeExpr site_type = CreateSiteType (ec, arguments, dyn_args_count, isStatement);
+                       int default_args = isStatement ? 1 : 2;
+                       var module = ec.Module;
+
+                       bool has_ref_out_argument = false;
+                       var targs = new TypeExpression[dyn_args_count + default_args];
+                       targs[0] = new TypeExpression (module.PredefinedTypes.CallSite.TypeSpec, loc);
 
-                       FieldExpr site_field_expr = new FieldExpr (CreateSiteField (ec, site_type), loc);
+                       TypeExpression[] targs_for_instance = null;
+                       TypeParameterMutator mutator;
 
-                       SymbolWriter.OpenCompilerGeneratedBlock (ec);
+                       var site_container = ec.CreateDynamicSite ();
 
-                       Arguments args = new Arguments (1);
-                       args.Add (new Argument (binder));
-                       StatementExpression s = new StatementExpression (new SimpleAssign (site_field_expr, new Invocation (new MemberAccess (site_type, "Create"), args)));
-                       
-                       BlockContext bc = new BlockContext (ec.MemberContext, null, TypeManager.void_type);             
-                       if (s.Resolve (bc)) {
-                               Statement init = new If (new Binary (Binary.Operator.Equality, site_field_expr, new NullLiteral (loc), loc), s, loc);
-                               init.Emit (ec);
-                       }
+                       if (context_mvars != null) {
+                               TypeParameters tparam;
+                               TypeContainer sc = site_container;
+                               do {
+                                       tparam = sc.CurrentTypeParameters;
+                                       sc = sc.Parent;
+                               } while (tparam == null);
 
-                       args = new Arguments (1 + dyn_args_count);
-                       args.Add (new Argument (site_field_expr));
-                       if (arguments != null) {
-                               foreach (Argument a in arguments) {
-                                       if (a is NamedArgument) {
-                                               // Name is not valid in this context
-                                               args.Add (new Argument (a.Expr, a.ArgType));
-                                               continue;
-                                       }
+                               mutator = new TypeParameterMutator (context_mvars, tparam);
 
-                                       args.Add (a);
+                               if (!ec.IsAnonymousStoreyMutateRequired) {
+                                       targs_for_instance = new TypeExpression[targs.Length];
+                                       targs_for_instance[0] = targs[0];
                                }
+                       } else {
+                               mutator = null;
                        }
 
-                       Expression target = new DelegateInvocation (new MemberAccess (site_field_expr, "Target", loc).Resolve (bc), args, loc).Resolve (bc);
-                       if (target != null)
-                               target.Emit (ec);
-
-                       SymbolWriter.CloseCompilerGeneratedBlock (ec);
-               }
+                       for (int i = 0; i < dyn_args_count; ++i) {
+                               Argument a = arguments[i];
+                               if (a.ArgType == Argument.AType.Out || a.ArgType == Argument.AType.Ref)
+                                       has_ref_out_argument = true;
 
-               public static MemberAccess GetBinderNamespace (Location loc)
-               {
-                       return new MemberAccess (new MemberAccess (
-                               new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "Microsoft", loc), "CSharp", loc), "RuntimeBinder", loc);
-               }
+                               var t = a.Type;
 
-               public static MemberAccess GetBinder (string name, Location loc)
-               {
-                       return new MemberAccess (TypeManager.binder_type, name, loc);
-               }
+                               // Convert any internal type like dynamic or null to object
+                               if (t.Kind == MemberKind.InternalCompilerType)
+                                       t = ec.BuiltinTypes.Object;
 
-               TypeExpr CreateSiteType (EmitContext ec, Arguments arguments, int dyn_args_count, bool is_statement)
-               {
-                       int default_args = is_statement ? 1 : 2;
+                               if (targs_for_instance != null)
+                                       targs_for_instance[i + 1] = new TypeExpression (t, loc);
 
-                       bool has_ref_out_argument = false;
-                       var targs = new TypeExpression[dyn_args_count + default_args];
-                       targs [0] = new TypeExpression (TypeManager.call_site_type, loc);
-                       for (int i = 0; i < dyn_args_count; ++i) {
-                               Argument a = arguments [i];
-                               if (a.ArgType == Argument.AType.Out || a.ArgType == Argument.AType.Ref)
-                                       has_ref_out_argument = true;
+                               if (mutator != null)
+                                       t = t.Mutate (mutator);
 
-                               targs [i + 1] = new TypeExpression (a.Type, loc);
+                               targs[i + 1] = new TypeExpression (t, loc);
                        }
 
                        TypeExpr del_type = null;
+                       TypeExpr del_type_instance_access = null;
                        if (!has_ref_out_argument) {
-                               string d_name = is_statement ? "Action" : "Func";
+                               string d_name = isStatement ? "Action" : "Func";
 
-                               TypeSpec t = TypeManager.CoreLookupType (ec.MemberContext.Compiler, "System", d_name, dyn_args_count + default_args, MemberKind.Delegate, false);
-                               if (t != null) {
-                                       if (!is_statement)
-                                               targs [targs.Length - 1] = new TypeExpression (type, loc);
+                               TypeExpr te = null;
+                               Namespace type_ns = module.GlobalRootNamespace.GetNamespace ("System", true);
+                               if (type_ns != null) {
+                                       te = type_ns.LookupType (module, d_name, dyn_args_count + default_args, LookupMode.Normal, loc);
+                               }
+
+                               if (te != null) {
+                                       if (!isStatement) {
+                                               var t = type;
+                                               if (t.Kind == MemberKind.InternalCompilerType)
+                                                       t = ec.BuiltinTypes.Object;
+
+                                               if (targs_for_instance != null)
+                                                       targs_for_instance[targs_for_instance.Length - 1] = new TypeExpression (t, loc);
 
-                                       del_type = new GenericTypeExpr (t, new TypeArguments (targs), loc);
+                                               if (mutator != null)
+                                                       t = t.Mutate (mutator);
+
+                                               targs[targs.Length - 1] = new TypeExpression (t, loc);
+                                       }
+
+                                       del_type = new GenericTypeExpr (te.Type, new TypeArguments (targs), loc);
+                                       if (targs_for_instance != null)
+                                               del_type_instance_access = new GenericTypeExpr (te.Type, new TypeArguments (targs_for_instance), loc);
+                                       else
+                                               del_type_instance_access = del_type;
                                }
                        }
 
                        //
-                       // Create custom delegate when no appropriate predefined one is found
+                       // Create custom delegate when no appropriate predefined delegate has been found
                        //
+                       Delegate d;
                        if (del_type == null) {
-                               TypeSpec rt = is_statement ? TypeManager.void_type : type;
-                               Parameter[] p = new Parameter [dyn_args_count + 1];
-                               p[0] = new Parameter (targs [0], "p0", Parameter.Modifier.NONE, null, loc);
+                               TypeSpec rt = isStatement ? ec.BuiltinTypes.Void : type;
+                               Parameter[] p = new Parameter[dyn_args_count + 1];
+                               p[0] = new Parameter (targs[0], "p0", Parameter.Modifier.NONE, null, loc);
 
                                var site = ec.CreateDynamicSite ();
-                               int index = site.Types == null ? 0 : site.Types.Count;
+                               int index = site.Containers == null ? 0 : site.Containers.Count;
 
-                               if (site.Mutator != null)
-                                       rt = site.Mutator.Mutate (rt);
+                               if (mutator != null)
+                                       rt = mutator.Mutate (rt);
 
                                for (int i = 1; i < dyn_args_count + 1; ++i) {
-                                       var t = targs[i];
-                                       if (site.Mutator != null)
-                                               t.Type = site.Mutator.Mutate (t.Type);
-
-                                       p[i] = new Parameter (t, "p" + i.ToString ("X"), arguments[i - 1].Modifier, null, loc);
+                                       p[i] = new Parameter (targs[i], "p" + i.ToString ("X"), arguments[i - 1].Modifier, null, loc);
                                }
 
-                               Delegate d = new Delegate (site.NamespaceEntry, site, new TypeExpression (rt, loc),
+                               d = new Delegate (site, new TypeExpression (rt, loc),
                                        Modifiers.INTERNAL | Modifiers.COMPILER_GENERATED,
                                        new MemberName ("Container" + index.ToString ("X")),
                                        new ParametersCompiled (p), null);
 
-                               d.CreateType ();
-                               d.DefineType ();
+                               d.CreateContainer ();
+                               d.DefineContainer ();
                                d.Define ();
-                               d.Emit ();
+                               d.PrepareEmit ();
+
+                               site.AddTypeContainer (d);
+                               del_type = new TypeExpression (d.CurrentType, loc);
+                               if (targs_for_instance != null) {
+                                       del_type_instance_access = null;
+                               } else {
+                                       del_type_instance_access = del_type;
+                               }
+                       } else {
+                               d = null;
+                       }
+
+                       var site_type_decl = new GenericTypeExpr (module.PredefinedTypes.CallSiteGeneric.TypeSpec, new TypeArguments (del_type), loc);
+                       var field = site_container.CreateCallSiteField (site_type_decl, loc);
+                       if (field == null)
+                               return;
+
+                       if (del_type_instance_access == null) {
+                               var dt = d.CurrentType.DeclaringType.MakeGenericType (module, context_mvars.Types);
+                               del_type_instance_access = new TypeExpression (MemberCache.GetMember (dt, d.CurrentType), loc);
+                       }
+
+                       var instanceAccessExprType = new GenericTypeExpr (module.PredefinedTypes.CallSiteGeneric.TypeSpec,
+                               new TypeArguments (del_type_instance_access), loc);
+
+                       if (instanceAccessExprType.ResolveAsType (ec.MemberContext) == null)
+                               return;
+
+                       bool inflate_using_mvar = context_mvars != null && ec.IsAnonymousStoreyMutateRequired;
+
+                       TypeSpec gt;
+                       if (inflate_using_mvar || context_mvars == null) {
+                               gt = site_container.CurrentType;
+                       } else {
+                               gt = site_container.CurrentType.MakeGenericType (module, context_mvars.Types);
+                       }
+
+                       // When site container already exists the inflated version has to be
+                       // updated manually to contain newly created field
+                       if (gt is InflatedTypeSpec && site_container.AnonymousMethodsCounter > 1) {
+                               var tparams = gt.MemberDefinition.TypeParametersCount > 0 ? gt.MemberDefinition.TypeParameters : TypeParameterSpec.EmptyTypes;
+                               var inflator = new TypeParameterInflator (module, gt, tparams, gt.TypeArguments);
+                               gt.MemberCache.AddMember (field.InflateMember (inflator));
+                       }
+
+                       FieldExpr site_field_expr = new FieldExpr (MemberCache.GetMember (gt, field), loc);
+
+                       BlockContext bc = new BlockContext (ec.MemberContext, null, ec.BuiltinTypes.Void);
+
+                       Arguments args = new Arguments (1);
+                       args.Add (new Argument (binder));
+                       StatementExpression s = new StatementExpression (new SimpleAssign (site_field_expr, new Invocation (new MemberAccess (instanceAccessExprType, "Create"), args)));
+
+                       using (ec.With (BuilderContext.Options.OmitDebugInfo, true)) {
+                               if (s.Resolve (bc)) {
+                                       Statement init = new If (new Binary (Binary.Operator.Equality, site_field_expr, new NullLiteral (loc)), s, loc);
+                                       init.Emit (ec);
+                               }
+
+                               args = new Arguments (1 + dyn_args_count);
+                               args.Add (new Argument (site_field_expr));
+                               if (arguments != null) {
+                                       int arg_pos = 1;
+                                       foreach (Argument a in arguments) {
+                                               if (a is NamedArgument) {
+                                                       // Name is not valid in this context
+                                                       args.Add (new Argument (a.Expr, a.ArgType));
+                                               } else {
+                                                       args.Add (a);
+                                               }
+
+                                               if (inflate_using_mvar && a.Type != targs[arg_pos].Type)
+                                                       a.Expr.Type = targs[arg_pos].Type;
+
+                                               ++arg_pos;
+                                       }
+                               }
 
-                               var inflated = site.AddDelegate (d);
-                               del_type = new TypeExpression (inflated, loc);
+                               Expression target = new DelegateInvocation (new MemberAccess (site_field_expr, "Target", loc).Resolve (bc), args, loc).Resolve (bc);
+                               if (target != null)
+                                       target.Emit (ec);
                        }
+               }
 
-                       TypeExpr site_type = new GenericTypeExpr (TypeManager.generic_call_site_type, new TypeArguments (del_type), loc);
-                       return site_type;
+               public static MemberAccess GetBinderNamespace (Location loc)
+               {
+                       return new MemberAccess (new MemberAccess (
+                               new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "Microsoft", loc), "CSharp", loc), "RuntimeBinder", loc);
+               }
+
+               protected MemberAccess GetBinder (string name, Location loc)
+               {
+                       return new MemberAccess (new TypeExpression (binder_type, loc), name, loc);
                }
        }
 
@@ -460,13 +562,13 @@ namespace Mono.CSharp
 
                        public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
                        {
-                               type = TypeManager.bool_type;
+                               type = ec.BuiltinTypes.Bool;
 
                                Arguments binder_args = new Arguments (3);
 
                                binder_args.Add (new Argument (new BinderFlags (0, this)));
-                               binder_args.Add (new Argument (new StringLiteral (name, loc)));
-                               binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
+                               binder_args.Add (new Argument (new StringLiteral (ec.BuiltinTypes, name, loc)));
+                               binder_args.Add (new Argument (new TypeOf (ec.CurrentType, loc)));
 
                                return new Invocation (GetBinder ("IsEvent", loc), binder_args);
                        }
@@ -490,7 +592,7 @@ namespace Mono.CSharp
 
                protected override Expression DoResolve (ResolveContext rc)
                {
-                       type = InternalType.Dynamic;
+                       type = rc.BuiltinTypes.Dynamic;
                        eclass = ExprClass.Value;
                        condition = condition.Resolve (rc);
                        return this;
@@ -506,7 +608,9 @@ namespace Mono.CSharp
                public override void EmitStatement (EmitContext ec)
                {
                        var stmt = new If (condition, new StatementExpression (invoke), new StatementExpression (assign), loc);
-                       stmt.Emit (ec);
+                       using (ec.With (BuilderContext.Options.OmitDebugInfo, true)) {
+                               stmt.Emit (ec);
+                       }
                }
        }
 
@@ -527,8 +631,8 @@ namespace Mono.CSharp
                        flags |= ec.HasSet (ResolveContext.Options.CheckedScope) ? CSharpBinderFlags.CheckedContext : 0;
 
                        binder_args.Add (new Argument (new BinderFlags (flags, this)));
-                       binder_args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
-                       binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
+                       binder_args.Add (new Argument (new TypeOf (type, loc)));
+                       binder_args.Add (new Argument (new TypeOf (ec.CurrentType, loc)));
                        return new Invocation (GetBinder ("Convert", loc), binder_args);
                }
        }
@@ -547,7 +651,7 @@ namespace Mono.CSharp
                        Arguments binder_args = new Arguments (3);
 
                        binder_args.Add (new Argument (new BinderFlags (0, this)));
-                       binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
+                       binder_args.Add (new Argument (new TypeOf (ec.CurrentType, loc)));
                        binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation (args.CreateDynamicBinderArguments (ec), loc)));
 
                        return new Invocation (GetBinder ("InvokeConstructor", loc), binder_args);
@@ -556,26 +660,73 @@ namespace Mono.CSharp
 
        class DynamicIndexBinder : DynamicMemberAssignable
        {
+               bool can_be_mutator;
+
                public DynamicIndexBinder (Arguments args, Location loc)
                        : base (args, loc)
                {
                }
 
+               public DynamicIndexBinder (CSharpBinderFlags flags, Arguments args, Location loc)
+                       : this (args, loc)
+               {
+                       base.flags = flags;
+               }
+
+               protected override Expression DoResolve (ResolveContext ec)
+               {
+                       can_be_mutator = true;
+                       return base.DoResolve (ec);
+               }
+
                protected override Expression CreateCallSiteBinder (ResolveContext ec, Arguments args, bool isSet)
                {
                        Arguments binder_args = new Arguments (3);
 
-                       binder_args.Add (new Argument (new BinderFlags (0, this)));
-                       binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
+                       binder_args.Add (new Argument (new BinderFlags (flags, this)));
+                       binder_args.Add (new Argument (new TypeOf (ec.CurrentType, loc)));
                        binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation (args.CreateDynamicBinderArguments (ec), loc)));
 
+                       isSet |= (flags & CSharpBinderFlags.ValueFromCompoundAssignment) != 0;
                        return new Invocation (GetBinder (isSet ? "SetIndex" : "GetIndex", loc), binder_args);
                }
+
+               protected override Arguments CreateSetterArguments (ResolveContext rc, Expression rhs)
+               {
+                       //
+                       // Indexer has arguments which complicates things as the setter and getter
+                       // are called in two steps when unary mutator is used. We have to make a
+                       // copy of all variable arguments to not duplicate any side effect.
+                       //
+                       // ++d[++arg, Foo ()]
+                       //
+
+                       if (!can_be_mutator)
+                               return base.CreateSetterArguments (rc, rhs);
+
+                       var setter_args = new Arguments (Arguments.Count + 1);
+                       for (int i = 0; i < Arguments.Count; ++i) {
+                               var expr = Arguments[i].Expr;
+
+                               if (expr is Constant || expr is VariableReference || expr is This) {
+                                       setter_args.Add (Arguments [i]);
+                                       continue;
+                               }
+
+                               LocalVariable temp = LocalVariable.CreateCompilerGenerated (expr.Type, rc.CurrentBlock, loc);
+                               expr = new SimpleAssign (temp.CreateReferenceExpression (rc, expr.Location), expr).Resolve (rc);
+                               Arguments[i].Expr = temp.CreateReferenceExpression (rc, expr.Location).Resolve (rc);
+                               setter_args.Add (Arguments [i].Clone (expr));
+                       }
+
+                       setter_args.Add (new Argument (rhs));
+                       return setter_args;
+               }
        }
 
        class DynamicInvocation : DynamicExpressionStatement, IDynamicBinder
        {
-               ATypeNameExpression member;
+               readonly ATypeNameExpression member;
 
                public DynamicInvocation (ATypeNameExpression member, Arguments args, Location loc)
                        : base (null, args, loc)
@@ -584,15 +735,6 @@ namespace Mono.CSharp
                        this.member = member;
                }
 
-               //
-               // When a return type is known not to be dynamic
-               //
-               public DynamicInvocation (ATypeNameExpression member, Arguments args, TypeSpec type, Location loc)
-                       : this (member, args, loc)
-               {
-                       this.type = type;
-               }
-
                public static DynamicInvocation CreateSpecialNameInvoke (ATypeNameExpression member, Arguments args, Location loc)
                {
                        return new DynamicInvocation (member, args, loc) {
@@ -616,14 +758,14 @@ namespace Mono.CSharp
                        binder_args.Add (new Argument (new BinderFlags (call_flags, this)));
 
                        if (is_member_access)
-                               binder_args.Add (new Argument (new StringLiteral (member.Name, member.Location)));
+                               binder_args.Add (new Argument (new StringLiteral (ec.BuiltinTypes, member.Name, member.Location)));
 
                        if (member != null && member.HasTypeArguments) {
                                TypeArguments ta = member.TypeArguments;
                                if (ta.Resolve (ec)) {
                                        var targs = new ArrayInitializer (ta.Count, loc);
                                        foreach (TypeSpec t in ta.Arguments)
-                                               targs.Add (new TypeOf (new TypeExpression (t, loc), loc));
+                                               targs.Add (new TypeOf (t, loc));
 
                                        binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation (targs, loc)));
                                }
@@ -631,7 +773,7 @@ namespace Mono.CSharp
                                binder_args.Add (new Argument (new NullLiteral (loc)));
                        }
 
-                       binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
+                       binder_args.Add (new Argument (new TypeOf (ec.CurrentType, loc)));
 
                        Expression real_args;
                        if (args == null) {
@@ -665,15 +807,22 @@ namespace Mono.CSharp
                        this.name = name;
                }
 
+               public DynamicMemberBinder (string name, CSharpBinderFlags flags, Arguments args, Location loc)
+                       : this (name, args, loc)
+               {
+                       base.flags = flags;
+               }
+
                protected override Expression CreateCallSiteBinder (ResolveContext ec, Arguments args, bool isSet)
                {
                        Arguments binder_args = new Arguments (4);
 
-                       binder_args.Add (new Argument (new BinderFlags (0, this)));
-                       binder_args.Add (new Argument (new StringLiteral (name, loc)));
-                       binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
+                       binder_args.Add (new Argument (new BinderFlags (flags, this)));
+                       binder_args.Add (new Argument (new StringLiteral (ec.BuiltinTypes, name, loc)));
+                       binder_args.Add (new Argument (new TypeOf (ec.CurrentType, loc)));
                        binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation (args.CreateDynamicBinderArguments (ec), loc)));
 
+                       isSet |= (flags & CSharpBinderFlags.ValueFromCompoundAssignment) != 0;
                        return new Invocation (GetBinder (isSet ? "SetMember" : "GetMember", loc), binder_args);
                }
        }
@@ -702,17 +851,23 @@ namespace Mono.CSharp
 
                protected abstract Expression CreateCallSiteBinder (ResolveContext ec, Arguments args, bool isSet);
 
+               protected virtual Arguments CreateSetterArguments (ResolveContext rc, Expression rhs)
+               {
+                       var setter_args = new Arguments (Arguments.Count + 1);
+                       setter_args.AddRange (Arguments);
+                       setter_args.Add (new Argument (rhs));
+                       return setter_args;
+               }
+
                public override Expression DoResolveLValue (ResolveContext rc, Expression right_side)
                {
-                       if (right_side == EmptyExpression.OutAccess.Instance) {
+                       if (right_side == EmptyExpression.OutAccess) {
                                right_side.DoResolveLValue (rc, this);
                                return null;
                        }
 
                        if (DoResolveCore (rc)) {
-                               setter_args = new Arguments (Arguments.Count + 1);
-                               setter_args.AddRange (Arguments);
-                               setter_args.Add (new Argument (right_side));
+                               setter_args = CreateSetterArguments (rc, right_side);
                                setter = CreateCallSiteBinder (rc, setter_args, true);
                        }
 
@@ -745,7 +900,7 @@ namespace Mono.CSharp
                        throw new NotImplementedException ();
                }
 
-               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
+               public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
                {
                        EmitCall (ec, setter, setter_args, !leave_copy);
                }
@@ -755,15 +910,23 @@ namespace Mono.CSharp
 
        class DynamicUnaryConversion : DynamicExpressionStatement, IDynamicBinder
        {
-               string name;
+               readonly string name;
 
                public DynamicUnaryConversion (string name, Arguments args, Location loc)
                        : base (null, args, loc)
                {
                        this.name = name;
                        base.binder = this;
-                       if (name == "IsTrue" || name == "IsFalse")
-                               type = TypeManager.bool_type;
+               }
+
+               public static DynamicUnaryConversion CreateIsTrue (ResolveContext rc, Arguments args, Location loc)
+               {
+                       return new DynamicUnaryConversion ("IsTrue", args, loc) { type = rc.BuiltinTypes.Bool };
+               }
+
+               public static DynamicUnaryConversion CreateIsFalse (ResolveContext rc, Arguments args, Location loc)
+               {
+                       return new DynamicUnaryConversion ("IsFalse", args, loc) { type = rc.BuiltinTypes.Bool };
                }
 
                public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
@@ -777,79 +940,29 @@ namespace Mono.CSharp
 
                        binder_args.Add (new Argument (new BinderFlags (flags, this)));
                        binder_args.Add (new Argument (new MemberAccess (new MemberAccess (sle, "ExpressionType", loc), name, loc)));
-                       binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
+                       binder_args.Add (new Argument (new TypeOf (ec.CurrentType, loc)));
                        binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation (args.CreateDynamicBinderArguments (ec), loc)));
 
                        return new Invocation (GetBinder ("UnaryOperation", loc), binder_args);
                }
        }
 
-       public class DynamicSiteClass : HoistedStoreyClass
+       sealed class DynamicSiteClass : HoistedStoreyClass
        {
-               //
-               // Holds the type to access the site. It gets inflated
-               // by MVARs for generic call sites
-               //
-               TypeSpec instance_type;
-
-               public DynamicSiteClass (TypeContainer parent, MemberBase host, TypeParameter[] tparams)
-                       : base (parent, MakeMemberName (host, "DynamicSite", parent.DynamicSitesCounter, tparams, Location.Null), tparams, Modifiers.STATIC)
+               public DynamicSiteClass (TypeDefinition parent, MemberBase host, TypeParameters tparams)
+                       : base (parent, MakeMemberName (host, "DynamicSite", parent.DynamicSitesCounter, tparams, Location.Null), tparams, Modifiers.STATIC, MemberKind.Class)
                {
-                       if (tparams != null) {
-                               mutator = new TypeParameterMutator (tparams, CurrentTypeParameters);
-                       }
-
                        parent.DynamicSitesCounter++;
                }
 
-               public override TypeSpec AddDelegate (Delegate d)
-               {
-                       TypeSpec inflated;
-
-                       base.AddDelegate (d);
-
-                       // Inflated type instance has to be updated manually
-                       if (instance_type is InflatedTypeSpec) {
-                               var inflator = new TypeParameterInflator (instance_type, TypeParameterSpec.EmptyTypes, TypeSpec.EmptyTypes);
-                               inflated = (TypeSpec) d.CurrentType.InflateMember (inflator);
-                               instance_type.MemberCache.AddMember (inflated);
-
-                               //inflator = new TypeParameterInflator (d.Parent.CurrentType, TypeParameterSpec.EmptyTypes, TypeSpec.EmptyTypes);
-                               //d.Parent.CurrentType.MemberCache.AddMember (d.CurrentType.InflateMember (inflator));
-                       } else {
-                               inflated = d.CurrentType;
-                       }
-
-                       return inflated;
-               }
-
                public FieldSpec CreateCallSiteField (FullNamedExpression type, Location loc)
                {
-                       int index = fields == null ? 0 : fields.Count;
+                       int index = AnonymousMethodsCounter++;
                        Field f = new HoistedField (this, type, Modifiers.PUBLIC | Modifiers.STATIC, "Site" + index.ToString ("X"), null, loc);
                        f.Define ();
 
                        AddField (f);
-
-                       var fs = f.Spec;
-                       if (mutator != null) {
-                               //
-                               // Inflate the field, no need to keep it in MemberCache as it's accessed only once
-                               //
-                               var inflator = new TypeParameterInflator (instance_type, spec.MemberDefinition.TypeParameters, instance_type.TypeArguments);
-                               fs = (FieldSpec) fs.InflateMember (inflator);
-                       }
-
-                       return fs;
-               }
-
-               protected override bool DoResolveTypeParameters ()
-               {
-                       instance_type = spec;
-                       if (mutator != null)
-                               instance_type = instance_type.MakeGenericType (mutator.MethodTypeParameters.Select (l => l.Type).ToArray ());
-
-                       return true;
+                       return f.Spec;
                }
        }
 }