[xbuild] Fix bug #674337.
[mono.git] / mcs / mcs / dynamic.cs
index 3ee37f39bcbfb77d9f11a059d6fc0c9f9084c304..1533e543e7b0d4a392647668c8e6cab8a35f3790 100644 (file)
@@ -9,7 +9,7 @@
 //
 
 using System;
-using System.Reflection.Emit;
+using System.Linq;
 using SLE = System.Linq.Expressions;
 
 #if NET_4_0
@@ -50,11 +50,6 @@ namespace Mono.CSharp
                        eclass = ExprClass.Type;
                }
 
-               public override bool CheckAccessLevel (IMemberContext ds)
-               {
-                       return true;
-               }
-
                protected override TypeExpr DoResolveAsTypeStep (IMemberContext ec)
                {
                        return this;
@@ -71,22 +66,31 @@ namespace Mono.CSharp
 #if !NET_4_0
                public class DynamicMetaObject
                {
-                       public Type RuntimeType;
-                       public Type LimitType;
+                       public TypeSpec RuntimeType;
+                       public TypeSpec LimitType;
                        public SLE.Expression Expression;
                }
 #endif
 
                readonly DynamicMetaObject obj;
 
-               public RuntimeValueExpression (DynamicMetaObject obj, bool isCompileTimeType)
+               public RuntimeValueExpression (DynamicMetaObject obj, TypeSpec type)
                {
                        this.obj = obj;
-                       this.type = isCompileTimeType ? obj.LimitType : obj.RuntimeType;
-                       this.type = obj.LimitType;
+                       this.type = type;
                        this.eclass = ExprClass.Variable;
                }
 
+               #region Properties
+
+               public bool IsSuggestionOnly { get; set; }
+
+               public DynamicMetaObject MetaObject {
+                       get { return obj; }
+               }
+
+               #endregion
+
                public void AddressOf (EmitContext ec, AddressOp mode)
                {
                        throw new NotImplementedException ();
@@ -126,18 +130,29 @@ namespace Mono.CSharp
 
                #endregion
 
-               public SLE.Expression MakeAssignExpression (BuilderContext ctx)
+               public SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source)
                {
                        return obj.Expression;
                }
 
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       return SLE.Expression.Convert (obj.Expression, type);
-               }
+#if STATIC
+                       return base.MakeExpression (ctx);
+#else
 
-               public DynamicMetaObject MetaObject {
-                       get { return obj; }
+       #if NET_4_0             
+                               if (type.IsStruct && !obj.Expression.Type.IsValueType)
+                                       return SLE.Expression.Unbox (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
                }
        }
 
@@ -148,7 +163,7 @@ namespace Mono.CSharp
        //
        public class DynamicResultCast : ShimExpression
        {
-               public DynamicResultCast (Type type, Expression expr)
+               public DynamicResultCast (TypeSpec type, Expression expr)
                        : base (expr)
                {
                        this.type = type;
@@ -164,7 +179,11 @@ namespace Mono.CSharp
 #if NET_4_0
                public override SLE.Expression MakeExpression (BuilderContext ctx)
                {
-                       return SLE.Expression.Block (expr.MakeExpression (ctx), SLE.Expression.Default (type));
+#if STATIC
+                       return base.MakeExpression (ctx);
+#else
+                       return SLE.Expression.Block (expr.MakeExpression (ctx), SLE.Expression.Default (type.GetMetaInfo ()));
+#endif
                }
 #endif
        }
@@ -185,9 +204,7 @@ namespace Mono.CSharp
        //
        interface IDynamicAssign : IAssignMethod
        {
-#if NET_4_0
-               SLE.Expression MakeAssignExpression (BuilderContext ctx);
-#endif
+               SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source);
        }
 
        //
@@ -195,17 +212,6 @@ namespace Mono.CSharp
        //
        class DynamicExpressionStatement : ExpressionStatement
        {
-               class StaticDataClass : CompilerGeneratedClass
-               {
-                       public StaticDataClass ()
-                               : base (new RootDeclSpace (new NamespaceEntry (null, null, null)),
-                                       new MemberName (CompilerGeneratedClass.MakeName (null, "c", "DynamicSites", 0)),
-                                       Modifiers.INTERNAL | Modifiers.STATIC)
-                       {
-                               ModFlags &= ~Modifiers.SEALED;
-                       }
-               }
-
                //
                // Binder flag dynamic constant, the value is combination of
                // flags known at resolve stage and flags known only at emit
@@ -227,16 +233,12 @@ namespace Mono.CSharp
                        {
                                Child = new IntConstant ((int) (flags | statement.flags), statement.loc).Resolve (ec);
 
-                               type = TypeManager.binder_flags;
+                               type = ec.Module.PredefinedTypes.BinderFlags.Resolve (loc);
                                eclass = Child.eclass;
                                return this;
                        }
                }
 
-               static StaticDataClass global_site_container;
-               static int field_counter;
-               static int container_counter;
-
                readonly Arguments arguments;
                protected IDynamicBinder binder;
                protected Expression binder_expr;
@@ -244,6 +246,8 @@ namespace Mono.CSharp
                // Used by BinderFlags
                protected CSharpBinderFlags flags;
 
+               TypeSpec binder_type;
+
                public DynamicExpressionStatement (IDynamicBinder binder, Arguments args, Location loc)
                {
                        this.binder = binder;
@@ -257,30 +261,10 @@ namespace Mono.CSharp
                        }
                }
 
-               static TypeContainer CreateSiteContainer ()
+               FieldSpec CreateSiteField (EmitContext ec, FullNamedExpression type)
                {
-                       if (global_site_container == null) {
-                               global_site_container = new StaticDataClass ();
-                               RootContext.ToplevelTypes.AddCompilerGeneratedClass (global_site_container);
-                               global_site_container.DefineType ();
-                               global_site_container.Define ();
-//                             global_site_container.EmitType ();
-
-                               RootContext.RegisterCompilerGeneratedType (global_site_container.TypeBuilder);
-                       }
-
-                       return global_site_container;
-               }
-
-               static Field CreateSiteField (FullNamedExpression type)
-               {
-                       TypeContainer site_container = CreateSiteContainer ();
-                       Field f = new Field (site_container, type, Modifiers.PUBLIC | Modifiers.STATIC,
-                               new MemberName ("Site" +  field_counter++), null);
-                       f.Define ();
-
-                       site_container.AddField (f);
-                       return f;
+                       var site_container = ec.CreateDynamicSite ();
+                       return site_container.CreateCallSiteField (type, loc);
                }
 
                public override Expression CreateExpressionTree (ResolveContext ec)
@@ -300,26 +284,11 @@ namespace Mono.CSharp
                protected bool DoResolveCore (ResolveContext rc)
                {
                        int errors = rc.Report.Errors;
+                       var pt = rc.Module.PredefinedTypes;
 
-                       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 (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);
-
-                       if (TypeManager.binder_flags == null) {
-                               TypeManager.binder_flags = TypeManager.CoreLookupType (rc.Compiler,
-                                       "Microsoft.CSharp.RuntimeBinder", "CSharpBinderFlags", MemberKind.Enum, true);
-                       }
+                       binder_type = pt.Binder.Resolve (loc);
+                       pt.CallSite.Resolve (loc);
+                       pt.CallSiteGeneric.Resolve (loc);
 
                        eclass = ExprClass.Value;
 
@@ -347,10 +316,14 @@ namespace Mono.CSharp
                protected void EmitCall (EmitContext ec, Expression binder, Arguments arguments, bool isStatement)
                {
                        int dyn_args_count = arguments == null ? 0 : arguments.Count;
-                       TypeExpr site_type = CreateSiteType (RootContext.ToplevelTypes.Compiler, arguments, dyn_args_count, isStatement);
-                       FieldExpr site_field_expr = new FieldExpr (CreateSiteField (site_type), loc);
+                       TypeExpr site_type = CreateSiteType (ec, arguments, dyn_args_count, isStatement);
+                       var field = CreateSiteField (ec, site_type);
+                       if (field == null)
+                               return;
 
-                       SymbolWriter.OpenCompilerGeneratedBlock (ec.ig);
+                       FieldExpr site_field_expr = new FieldExpr (field, loc);
+
+                       SymbolWriter.OpenCompilerGeneratedBlock (ec);
 
                        Arguments args = new Arguments (1);
                        args.Add (new Argument (binder));
@@ -358,7 +331,7 @@ namespace Mono.CSharp
                        
                        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)), s, loc);
+                               Statement init = new If (new Binary (Binary.Operator.Equality, site_field_expr, new NullLiteral (loc), loc), s, loc);
                                init.Emit (ec);
                        }
 
@@ -380,7 +353,7 @@ namespace Mono.CSharp
                        if (target != null)
                                target.Emit (ec);
 
-                       SymbolWriter.CloseCompilerGeneratedBlock (ec.ig);
+                       SymbolWriter.CloseCompilerGeneratedBlock (ec);
                }
 
                public static MemberAccess GetBinderNamespace (Location loc)
@@ -389,42 +362,48 @@ namespace Mono.CSharp
                                new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "Microsoft", loc), "CSharp", loc), "RuntimeBinder", loc);
                }
 
-               public static MemberAccess GetBinder (string name, Location loc)
+               protected MemberAccess GetBinder (string name, Location loc)
                {
-                       return new MemberAccess (TypeManager.binder_type, name, loc);
+                       return new MemberAccess (new TypeExpression (binder_type, loc), name, loc);
                }
 
-               TypeExpr CreateSiteType (CompilerContext ctx, Arguments arguments, int dyn_args_count, bool is_statement)
+               TypeExpr CreateSiteType (EmitContext ec, Arguments arguments, int dyn_args_count, bool is_statement)
                {
                        int default_args = is_statement ? 1 : 2;
+                       var module = ec.Module;
 
                        bool has_ref_out_argument = false;
-                       FullNamedExpression[] targs = new FullNamedExpression[dyn_args_count + default_args];
-                       targs [0] = new TypeExpression (TypeManager.call_site_type, loc);
+                       var targs = new TypeExpression[dyn_args_count + default_args];
+                       targs [0] = new TypeExpression (module.PredefinedTypes.CallSite.TypeSpec, loc);
                        for (int i = 0; i < dyn_args_count; ++i) {
-                               Type arg_type;
                                Argument a = arguments [i];
-                               if (a.Type == TypeManager.null_type)
-                                       arg_type = TypeManager.object_type;
-                               else
-                                       arg_type = a.Type;
-
                                if (a.ArgType == Argument.AType.Out || a.ArgType == Argument.AType.Ref)
                                        has_ref_out_argument = true;
 
-                               targs [i + 1] = new TypeExpression (arg_type, loc);
+                               var t = a.Type;
+
+                               // Convert any internal type like dynamic or null to object
+                               if (t.Kind == MemberKind.InternalCompilerType)
+                                       t = TypeManager.object_type;
+
+                               targs [i + 1] = new TypeExpression (t, loc);
                        }
 
                        TypeExpr del_type = null;
                        if (!has_ref_out_argument) {
-                               string d_name = is_statement ? "Action`" : "Func`";
+                               string d_name = is_statement ? "Action" : "Func";
 
-                               Type t = TypeManager.CoreLookupType (ctx, "System", d_name + (dyn_args_count + default_args), MemberKind.Delegate, false);
-                               if (t != null) {
+                               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, true, Location.Null);
+                               }
+                       
+                               if (te != null) {
                                        if (!is_statement)
                                                targs [targs.Length - 1] = new TypeExpression (type, loc);
 
-                                       del_type = new GenericTypeExpr (t, new TypeArguments (targs), loc);
+                                       del_type = new GenericTypeExpr (te.Type, new TypeArguments (targs), loc);
                                }
                        }
 
@@ -432,81 +411,114 @@ namespace Mono.CSharp
                        // Create custom delegate when no appropriate predefined one is found
                        //
                        if (del_type == null) {
-                               Type rt = is_statement ? TypeManager.void_type : type;
+                               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);
 
-                               for (int i = 1; i < dyn_args_count + 1; ++i)
-                                       p[i] = new Parameter (targs[i], "p" + i.ToString ("X"), arguments[i - 1].Modifier, null, loc);
+                               var site = ec.CreateDynamicSite ();
+                               int index = site.Types == null ? 0 : site.Types.Count;
 
-                               TypeContainer parent = CreateSiteContainer ();
-                               Delegate d = new Delegate (parent.NamespaceEntry, parent, new TypeExpression (rt, loc),
+                               if (site.Mutator != null)
+                                       rt = site.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);
+                               }
+
+                               Delegate d = new Delegate (site.NamespaceEntry, site, new TypeExpression (rt, loc),
                                        Modifiers.INTERNAL | Modifiers.COMPILER_GENERATED,
-                                       new MemberName ("Container" + container_counter++.ToString ("X")),
-                                       new ParametersCompiled (ctx, p), null);
+                                       new MemberName ("Container" + index.ToString ("X")),
+                                       new ParametersCompiled (p), null);
 
+                               d.CreateType ();
                                d.DefineType ();
                                d.Define ();
                                d.Emit ();
 
-                               parent.AddDelegate (d);
-                               del_type = new TypeExpression (d.TypeBuilder, loc);
+                               var inflated = site.AddDelegate (d);
+                               del_type = new TypeExpression (inflated, loc);
                        }
 
-                       TypeExpr site_type = new GenericTypeExpr (TypeManager.generic_call_site_type, new TypeArguments (del_type), loc);
+                       TypeExpr site_type = new GenericTypeExpr (module.PredefinedTypes.CallSiteGeneric.TypeSpec, new TypeArguments (del_type), loc);
                        return site_type;
                }
-
-               public static void Reset ()
-               {
-                       global_site_container = null;
-                       field_counter = container_counter = 0;
-               }
        }
 
        //
        // Dynamic member access compound assignment for events
        //
-       class DynamicEventCompoundAssign : DynamicExpressionStatement, IDynamicBinder
+       class DynamicEventCompoundAssign : ExpressionStatement
        {
-               string name;
-               Statement condition;
-
-               public DynamicEventCompoundAssign (string name, Arguments args, ExpressionStatement assignment, ExpressionStatement invoke, Location loc)
-                       : base (null, args, loc)
+               class IsEvent : DynamicExpressionStatement, IDynamicBinder
                {
-                       this.name = name;
-                       base.binder = this;
+                       string name;
+
+                       public IsEvent (string name, Arguments args, Location loc)
+                               : base (null, args, loc)
+                       {
+                               this.name = name;
+                               binder = this;
+                       }
 
-                       // Used by += or -= only
-                       type = TypeManager.bool_type;
+                       public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
+                       {
+                               type = TypeManager.bool_type;
+
+                               Arguments binder_args = new Arguments (3);
 
-                       condition = new If (
-                               new Binary (Binary.Operator.Equality, this, new BoolLiteral (true, loc)),
-                               new StatementExpression (invoke), new StatementExpression (assignment),
-                               loc);
+                               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)));
+
+                               return new Invocation (GetBinder ("IsEvent", loc), binder_args);
+                       }
                }
 
-               public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
+               Expression condition;
+               ExpressionStatement invoke, assign;
+
+               public DynamicEventCompoundAssign (string name, Arguments args, ExpressionStatement assignment, ExpressionStatement invoke, Location loc)
                {
-                       Arguments binder_args = new Arguments (3);
+                       condition = new IsEvent (name, args, loc);
+                       this.invoke = invoke;
+                       this.assign = assignment;
+                       this.loc = loc;
+               }
 
-                       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)));
+               public override Expression CreateExpressionTree (ResolveContext ec)
+               {
+                       return condition.CreateExpressionTree (ec);
+               }
 
-                       return new Invocation (GetBinder ("IsEvent", loc), binder_args);
+               protected override Expression DoResolve (ResolveContext rc)
+               {
+                       type = InternalType.Dynamic;
+                       eclass = ExprClass.Value;
+                       condition = condition.Resolve (rc);
+                       return this;
+               }
+
+               public override void Emit (EmitContext ec)
+               {
+                       var rc = new ResolveContext (ec.MemberContext);
+                       var expr = new Conditional (new BooleanExpression (condition), invoke, assign, loc).Resolve (rc);
+                       expr.Emit (ec);
                }
 
                public override void EmitStatement (EmitContext ec)
                {
-                       condition.Emit (ec);
+                       var stmt = new If (condition, new StatementExpression (invoke), new StatementExpression (assign), loc);
+                       stmt.Emit (ec);
                }
        }
 
        class DynamicConversion : DynamicExpressionStatement, IDynamicBinder
        {
-               public DynamicConversion (Type targetType, CSharpBinderFlags flags, Arguments args, Location loc)
+               public DynamicConversion (TypeSpec targetType, CSharpBinderFlags flags, Arguments args, Location loc)
                        : base (null, args, loc)
                {
                        type = targetType;
@@ -516,19 +528,20 @@ namespace Mono.CSharp
 
                public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
                {
-                       Arguments binder_args = new Arguments (2);
+                       Arguments binder_args = new Arguments (3);
 
                        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)));
                        return new Invocation (GetBinder ("Convert", loc), binder_args);
                }
        }
 
        class DynamicConstructorBinder : DynamicExpressionStatement, IDynamicBinder
        {
-               public DynamicConstructorBinder (Type type, Arguments args, Location loc)
+               public DynamicConstructorBinder (TypeSpec type, Arguments args, Location loc)
                        : base (null, args, loc)
                {
                        this.type = type;
@@ -541,7 +554,7 @@ namespace Mono.CSharp
 
                        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 ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (ec), loc)));
+                       binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation (args.CreateDynamicBinderArguments (ec), loc)));
 
                        return new Invocation (GetBinder ("InvokeConstructor", loc), binder_args);
                }
@@ -549,21 +562,68 @@ 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 BinderFlags (flags, this)));
                        binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
-                       binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (ec), 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
@@ -577,10 +637,12 @@ namespace Mono.CSharp
                        this.member = member;
                }
 
-               public DynamicInvocation (ATypeNameExpression member, Arguments args, Type type, Location loc)
+               //
+               // When a return type is known not to be dynamic
+               //
+               public DynamicInvocation (ATypeNameExpression member, Arguments args, TypeSpec type, Location loc)
                        : this (member, args, loc)
                {
-                       // When a return type is known not to be dynamic
                        this.type = type;
                }
 
@@ -613,10 +675,10 @@ namespace Mono.CSharp
                                TypeArguments ta = member.TypeArguments;
                                if (ta.Resolve (ec)) {
                                        var targs = new ArrayInitializer (ta.Count, loc);
-                                       foreach (Type t in ta.Arguments)
+                                       foreach (TypeSpec t in ta.Arguments)
                                                targs.Add (new TypeOf (new TypeExpression (t, loc), loc));
 
-                                       binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", targs, loc)));
+                                       binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation (targs, loc)));
                                }
                        } else if (is_member_access) {
                                binder_args.Add (new Argument (new NullLiteral (loc)));
@@ -628,10 +690,10 @@ namespace Mono.CSharp
                        if (args == null) {
                                // Cannot be null because .NET trips over
                                real_args = new ArrayCreation (
-                                       new MemberAccess (GetBinderNamespace (loc), "CSharpArgumentInfo", loc), "[]",
+                                       new MemberAccess (GetBinderNamespace (loc), "CSharpArgumentInfo", loc),
                                        new ArrayInitializer (0, loc), loc);
                        } else {
-                               real_args = new ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (ec), loc);
+                               real_args = new ImplicitlyTypedArrayCreation (args.CreateDynamicBinderArguments (ec), loc);
                        }
 
                        binder_args.Add (new Argument (real_args));
@@ -656,15 +718,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 BinderFlags (flags, 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 ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (ec), 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);
                }
        }
@@ -693,6 +762,14 @@ 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) {
@@ -701,9 +778,7 @@ namespace Mono.CSharp
                        }
 
                        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);
                        }
 
@@ -746,20 +821,28 @@ 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 (Arguments args, Location loc)
+               {
+                       return new DynamicUnaryConversion ("IsTrue", args, loc) { type = TypeManager.bool_type };
+               }
+
+               public static DynamicUnaryConversion CreateIsFalse (Arguments args, Location loc)
+               {
+                       return new DynamicUnaryConversion ("IsFalse", args, loc) { type = TypeManager.bool_type };
                }
 
                public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
                {
-                       Arguments binder_args = new Arguments (3);
+                       Arguments binder_args = new Arguments (4);
 
                        MemberAccess sle = new MemberAccess (new MemberAccess (
                                new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "System", loc), "Linq", loc), "Expressions", loc);
@@ -768,9 +851,79 @@ 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 ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (ec), loc)));
+                       binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
+                       binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation (args.CreateDynamicBinderArguments (ec), loc)));
 
                        return new Invocation (GetBinder ("UnaryOperation", loc), binder_args);
                }
        }
+
+       public 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)
+               {
+                       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 (this, 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;
+                       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 (this, 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 (this, mutator.MethodTypeParameters.Select (l => l.Type).ToArray ());
+
+                       return true;
+               }
+       }
 }