2009-11-04 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / dynamic.cs
1 //
2 // dynamic.cs: support for dynamic expressions
3 //
4 // Authors: Marek Safar (marek.safar@gmail.com)
5 //
6 // Dual licensed under the terms of the MIT X11 or GNU GPL
7 //
8 // Copyright 2009 Novell, Inc
9 //
10
11 using System;
12 using System.Collections;
13 using System.Reflection.Emit;
14
15 #if NET_4_0
16 using System.Dynamic;
17 using SLE = System.Linq.Expressions;
18 #endif
19
20 namespace Mono.CSharp
21 {
22         //
23         // A copy of Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpBinderFlags.cs
24         // has to be kept in sync
25         //
26         [Flags]
27         public enum CSharpBinderFlags
28         {
29                 None = 0,
30                 CheckedContext = 1,
31                 InvokeSimpleName = 1 << 1,
32                 InvokeSpecialName = 1 << 2,
33                 BinaryOperationLogical = 1 << 3,
34                 ConvertExplicit = 1 << 4,
35                 ConvertArrayIndex = 1 << 5,
36                 ResultIndexed = 1 << 6,
37                 ValueFromCompoundAssignment = 1 << 7,
38                 ResultDiscarded = 1 << 8
39         }
40
41         //
42         // Type expression with internal dynamic type symbol
43         //
44         class DynamicTypeExpr : TypeExpr
45         {
46                 public DynamicTypeExpr (Location loc)
47                 {
48                         this.loc = loc;
49
50                         type = InternalType.Dynamic;
51                         eclass = ExprClass.Type;
52                 }
53
54                 public override bool CheckAccessLevel (IMemberContext ds)
55                 {
56                         return true;
57                 }
58
59                 protected override TypeExpr DoResolveAsTypeStep (IMemberContext ec)
60                 {
61                         return this;
62                 }
63         }
64
65         //
66         // Expression created from runtime dynamic object value
67         //
68         public class RuntimeValueExpression : Expression, IDynamicAssign, IMemoryLocation
69         {
70 #if !NET_4_0
71                 public class DynamicMetaObject { public Type RuntimeType; }
72 #endif
73
74                 readonly DynamicMetaObject obj;
75
76                 public RuntimeValueExpression (DynamicMetaObject obj)
77                 {
78                         this.obj = obj;
79                         this.type = obj.RuntimeType;
80                         this.eclass = ExprClass.Variable;
81                 }
82
83                 public void AddressOf (EmitContext ec, AddressOp mode)
84                 {
85                         throw new NotImplementedException ();
86                 }
87
88                 public override Expression CreateExpressionTree (ResolveContext ec)
89                 {
90                         throw new NotImplementedException ();
91                 }
92
93                 public override Expression DoResolve (ResolveContext ec)
94                 {
95                         return this;
96                 }
97
98                 public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
99                 {
100                         return this;
101                 }
102
103                 public override void Emit (EmitContext ec)
104                 {
105                         throw new NotImplementedException ();
106                 }
107
108                 #region IAssignMethod Members
109
110                 public void Emit (EmitContext ec, bool leave_copy)
111                 {
112                         throw new NotImplementedException ();
113                 }
114
115                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
116                 {
117                         throw new NotImplementedException ();
118                 }
119
120                 #endregion
121
122 #if NET_4_0
123                 public SLE.Expression MakeAssignExpression (BuilderContext ctx)
124                 {
125                         return obj.Expression;
126                 }
127
128                 public override SLE.Expression MakeExpression (BuilderContext ctx)
129                 {
130                         return SLE.Expression.Convert (obj.Expression, type);
131                 }
132 #endif
133
134                 public DynamicMetaObject MetaObject {
135                         get { return obj; }
136                 }
137         }
138
139         //
140         // Creates dynamic binder expression
141         //
142         interface IDynamicBinder
143         {
144                 Expression CreateCallSiteBinder (ResolveContext ec, Arguments args);
145         }
146
147         //
148         // Extends standard assignment interface for expressions
149         // supported by dynamic resolver
150         //
151         interface IDynamicAssign : IAssignMethod
152         {
153 #if NET_4_0
154                 SLE.Expression MakeAssignExpression (BuilderContext ctx);
155 #endif
156         }
157
158         //
159         // Base dynamic expression statement creator
160         //
161         class DynamicExpressionStatement : ExpressionStatement
162         {
163                 class StaticDataClass : CompilerGeneratedClass
164                 {
165                         public StaticDataClass ()
166                                 : base (new RootDeclSpace (new NamespaceEntry (null, null, null)),
167                                         new MemberName (CompilerGeneratedClass.MakeName (null, "c", "DynamicSites", 0)),
168                                         Modifiers.INTERNAL | Modifiers.STATIC)
169                         {
170                                 ModFlags &= ~Modifiers.SEALED;
171                         }
172                 }
173
174                 //
175                 // Binder flag dynamic constant, the value is combination of
176                 // flags known at resolve stage and flags known only at emit
177                 // stage
178                 //
179                 protected class BinderFlags : EnumConstant
180                 {
181                         DynamicExpressionStatement statement;
182                         CSharpBinderFlags flags;
183
184                         public BinderFlags (CSharpBinderFlags flags, DynamicExpressionStatement statement)
185                                 : base ()
186                         {
187                                 this.flags = flags;
188                                 this.statement = statement;
189                         }
190
191                         public override Expression DoResolve (ResolveContext ec)
192                         {
193                                 Child = new IntConstant ((int) (flags | statement.flags), statement.loc);
194
195                                 type = TypeManager.binder_flags;
196                                 eclass = Child.eclass;
197                                 return this;
198                         }
199                 }
200
201                 static StaticDataClass global_site_container;
202                 static int field_counter;
203                 static int container_counter;
204
205                 readonly Arguments arguments;
206                 protected IDynamicBinder binder;
207                 protected Expression binder_expr;
208
209                 // Used by BinderFlags
210                 protected CSharpBinderFlags flags;
211
212                 public DynamicExpressionStatement (IDynamicBinder binder, Arguments args, Location loc)
213                 {
214                         this.binder = binder;
215                         this.arguments = args;
216                         this.loc = loc;
217                 }
218
219                 public Arguments Arguments {
220                         get {
221                                 return arguments;
222                         }
223                 }
224
225                 static TypeContainer CreateSiteContainer ()
226                 {
227                         if (global_site_container == null) {
228                                 global_site_container = new StaticDataClass ();
229                                 RootContext.ToplevelTypes.AddCompilerGeneratedClass (global_site_container);
230                                 global_site_container.DefineType ();
231                                 global_site_container.Define ();
232 //                              global_site_container.EmitType ();
233
234                                 RootContext.RegisterCompilerGeneratedType (global_site_container.TypeBuilder);
235                         }
236
237                         return global_site_container;
238                 }
239
240                 static Field CreateSiteField (FullNamedExpression type)
241                 {
242                         TypeContainer site_container = CreateSiteContainer ();
243                         Field f = new Field (site_container, type, Modifiers.PUBLIC | Modifiers.STATIC,
244                                 new MemberName ("Site" +  field_counter++), null);
245                         f.Define ();
246
247                         site_container.AddField (f);
248                         return f;
249                 }
250
251                 public override Expression CreateExpressionTree (ResolveContext ec)
252                 {
253                         throw new NotImplementedException ();
254                 }
255
256                 public override Expression DoResolve (ResolveContext ec)
257                 {
258                         if (eclass != ExprClass.Invalid)
259                                 return this;
260
261                         if (DoResolveCore (ec))
262                                 binder_expr = binder.CreateCallSiteBinder (ec, arguments);
263
264                         return this;
265                 }
266
267                 protected bool DoResolveCore (ResolveContext rc)
268                 {
269                         int errors = rc.Report.Errors;
270
271                         if (TypeManager.call_site_type == null)
272                                 TypeManager.call_site_type = TypeManager.CoreLookupType (rc.Compiler,
273                                         "System.Runtime.CompilerServices", "CallSite", Kind.Class, true);
274
275                         if (TypeManager.generic_call_site_type == null)
276                                 TypeManager.generic_call_site_type = TypeManager.CoreLookupType (rc.Compiler,
277                                         "System.Runtime.CompilerServices", "CallSite`1", Kind.Class, true);
278
279                         if (TypeManager.binder_type == null) {
280                                 var t = TypeManager.CoreLookupType (rc.Compiler,
281                                         "Microsoft.CSharp.RuntimeBinder", "Binder", Kind.Class, true);
282                                 if (t != null)
283                                         TypeManager.binder_type = new TypeExpression (t, Location.Null);
284                         }
285
286                         if (TypeManager.binder_flags == null) {
287                                 TypeManager.binder_flags = TypeManager.CoreLookupType (rc.Compiler,
288                                         "Microsoft.CSharp.RuntimeBinder", "CSharpBinderFlags", Kind.Enum, true);
289                         }
290
291                         eclass = ExprClass.Value;
292
293                         if (type == null)
294                                 type = InternalType.Dynamic;
295
296                         return rc.Report.Errors == errors;
297                 }
298
299                 public override void Emit (EmitContext ec)
300                 {
301                         EmitCall (ec, binder_expr, arguments,  false);
302                 }
303
304                 public override void EmitStatement (EmitContext ec)
305                 {
306                         EmitCall (ec, binder_expr, arguments, true);
307                 }
308
309                 protected void EmitCall (EmitContext ec, Expression binder, Arguments arguments, bool isStatement)
310                 {
311                         int dyn_args_count = arguments == null ? 0 : arguments.Count;
312                         TypeExpr site_type = CreateSiteType (RootContext.ToplevelTypes.Compiler, arguments, dyn_args_count, isStatement);
313                         FieldExpr site_field_expr = new FieldExpr (CreateSiteField (site_type).FieldBuilder, loc);
314
315                         SymbolWriter.OpenCompilerGeneratedBlock (ec.ig);
316
317                         Arguments args = new Arguments (1);
318                         args.Add (new Argument (binder));
319                         StatementExpression s = new StatementExpression (new SimpleAssign (site_field_expr, new Invocation (new MemberAccess (site_type, "Create"), args)));
320                         
321                         BlockContext bc = new BlockContext (ec.MemberContext, null, TypeManager.void_type);             
322                         if (s.Resolve (bc)) {
323                                 Statement init = new If (new Binary (Binary.Operator.Equality, site_field_expr, new NullLiteral (loc)), s, loc);
324                                 init.Emit (ec);
325                         }
326
327                         args = new Arguments (1 + dyn_args_count);
328                         args.Add (new Argument (site_field_expr));
329                         if (arguments != null) {
330                                 foreach (Argument a in arguments) {
331                                         if (a is NamedArgument) {
332                                                 // Name is not valid in this context
333                                                 args.Add (new Argument (a.Expr, a.ArgType));
334                                                 continue;
335                                         }
336
337                                         args.Add (a);
338                                 }
339                         }
340
341                         Expression target = new DelegateInvocation (new MemberAccess (site_field_expr, "Target", loc).Resolve (bc), args, loc).Resolve (bc);
342                         if (target != null)
343                                 target.Emit (ec);
344
345                         SymbolWriter.CloseCompilerGeneratedBlock (ec.ig);
346                 }
347
348                 public static MemberAccess GetBinderNamespace (Location loc)
349                 {
350                         return new MemberAccess (new MemberAccess (
351                                 new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "Microsoft", loc), "CSharp", loc), "RuntimeBinder", loc);
352                 }
353
354                 public static MemberAccess GetBinder (string name, Location loc)
355                 {
356                         return new MemberAccess (TypeManager.binder_type, name, loc);
357                 }
358
359                 TypeExpr CreateSiteType (CompilerContext ctx, Arguments arguments, int dyn_args_count, bool is_statement)
360                 {
361                         int default_args = is_statement ? 1 : 2;
362
363                         bool has_ref_out_argument = false;
364                         FullNamedExpression[] targs = new FullNamedExpression[dyn_args_count + default_args];
365                         targs [0] = new TypeExpression (TypeManager.call_site_type, loc);
366                         for (int i = 0; i < dyn_args_count; ++i) {
367                                 Type arg_type;
368                                 Argument a = arguments [i];
369                                 if (a.Type == TypeManager.null_type)
370                                         arg_type = TypeManager.object_type;
371                                 else
372                                         arg_type = TypeManager.TypeToReflectionType (a.Type);
373
374                                 if (a.ArgType == Argument.AType.Out || a.ArgType == Argument.AType.Ref)
375                                         has_ref_out_argument = true;
376
377                                 targs [i + 1] = new TypeExpression (arg_type, loc);
378                         }
379
380                         TypeExpr del_type = null;
381                         if (!has_ref_out_argument) {
382                                 string d_name = is_statement ? "Action`" : "Func`";
383
384                                 Type t = TypeManager.CoreLookupType (ctx, "System", d_name + (dyn_args_count + default_args), Kind.Delegate, false);
385                                 if (t != null) {
386                                         if (!is_statement)
387                                                 targs [targs.Length - 1] = new TypeExpression (TypeManager.TypeToReflectionType (type), loc);
388
389                                         del_type = new GenericTypeExpr (t, new TypeArguments (targs), loc);
390                                 }
391                         }
392
393                         //
394                         // Create custom delegate when no appropriate predefined one is found
395                         //
396                         if (del_type == null) {
397                                 Type rt = is_statement ? TypeManager.void_type : type;
398                                 Parameter[] p = new Parameter [dyn_args_count + 1];
399                                 p[0] = new Parameter (targs [0], "p0", Parameter.Modifier.NONE, null, loc);
400
401                                 for (int i = 1; i < dyn_args_count + 1; ++i)
402                                         p[i] = new Parameter (targs[i], "p" + i.ToString ("X"), arguments[i - 1].Modifier, null, loc);
403
404                                 TypeContainer parent = CreateSiteContainer ();
405                                 Delegate d = new Delegate (parent.NamespaceEntry, parent, new TypeExpression (TypeManager.TypeToReflectionType (rt), loc),
406                                         Modifiers.INTERNAL | Modifiers.COMPILER_GENERATED,
407                                         new MemberName ("Container" + container_counter++.ToString ("X")),
408                                         new ParametersCompiled (p), null);
409
410                                 d.DefineType ();
411                                 d.Define ();
412
413                                 parent.AddDelegate (d);
414                                 del_type = new TypeExpression (d.TypeBuilder, loc);
415                         }
416
417                         TypeExpr site_type = new GenericTypeExpr (TypeManager.generic_call_site_type, new TypeArguments (del_type), loc);
418                         return site_type;
419                 }
420
421                 public static void Reset ()
422                 {
423                         global_site_container = null;
424                         field_counter = container_counter = 0;
425                 }
426         }
427
428         //
429         // Dynamic member access compound assignment for events
430         //
431         class DynamicEventCompoundAssign : DynamicExpressionStatement, IDynamicBinder
432         {
433                 string name;
434                 Statement condition;
435
436                 public DynamicEventCompoundAssign (string name, Arguments args, ExpressionStatement assignment, ExpressionStatement invoke, Location loc)
437                         : base (null, args, loc)
438                 {
439                         this.name = name;
440                         base.binder = this;
441
442                         // Used by += or -= only
443                         type = TypeManager.bool_type;
444
445                         condition = new If (
446                                 new Binary (Binary.Operator.Equality, this, new BoolLiteral (true, loc)),
447                                 new StatementExpression (invoke), new StatementExpression (assignment),
448                                 loc);
449                 }
450
451                 public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
452                 {
453                         Arguments binder_args = new Arguments (3);
454
455                         binder_args.Add (new Argument (new BinderFlags (0, this)));
456                         binder_args.Add (new Argument (new StringLiteral (name, loc)));
457                         binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
458
459                         return new Invocation (GetBinder ("IsEvent", loc), binder_args);
460                 }
461
462                 public override void EmitStatement (EmitContext ec)
463                 {
464                         condition.Emit (ec);
465                 }
466         }
467
468         class DynamicConversion : DynamicExpressionStatement, IDynamicBinder
469         {
470                 public DynamicConversion (Type targetType, CSharpBinderFlags flags, Arguments args, Location loc)
471                         : base (null, args, loc)
472                 {
473                         type = targetType;
474                         base.flags = flags;
475                         base.binder = this;
476                 }
477
478                 public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
479                 {
480                         Arguments binder_args = new Arguments (2);
481
482                         flags |= ec.HasSet (ResolveContext.Options.CheckedScope) ? CSharpBinderFlags.CheckedContext : 0;
483
484                         binder_args.Add (new Argument (new BinderFlags (flags, this)));
485                         binder_args.Add (new Argument (new TypeOf (new TypeExpression (type, loc), loc)));
486                         return new Invocation (GetBinder ("Convert", loc), binder_args);
487                 }
488         }
489
490         class DynamicConstructorBinder : DynamicExpressionStatement, IDynamicBinder
491         {
492                 public DynamicConstructorBinder (Type type, Arguments args, Location loc)
493                         : base (null, args, loc)
494                 {
495                         this.type = type;
496                         base.binder = this;
497                 }
498
499                 public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
500                 {
501                         Arguments binder_args = new Arguments (3);
502
503                         binder_args.Add (new Argument (new BinderFlags (0, this)));
504                         binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
505                         binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (), loc)));
506
507                         return new Invocation (GetBinder ("InvokeConstructor", loc), binder_args);
508                 }
509         }
510
511         class DynamicIndexBinder : DynamicMemberAssignable
512         {
513                 public DynamicIndexBinder (Arguments args, Location loc)
514                         : base (args, loc)
515                 {
516                 }
517
518                 protected override Expression CreateCallSiteBinder (ResolveContext ec, Arguments args, bool isSet)
519                 {
520                         Arguments binder_args = new Arguments (3);
521
522                         binder_args.Add (new Argument (new BinderFlags (0, this)));
523                         binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
524                         binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (), loc)));
525
526                         return new Invocation (GetBinder (isSet ? "SetIndex" : "GetIndex", loc), binder_args);
527                 }
528         }
529
530         class DynamicInvocation : DynamicExpressionStatement, IDynamicBinder
531         {
532                 ATypeNameExpression member;
533
534                 public DynamicInvocation (ATypeNameExpression member, Arguments args, Location loc)
535                         : base (null, args, loc)
536                 {
537                         base.binder = this;
538                         this.member = member;
539                 }
540
541                 public DynamicInvocation (ATypeNameExpression member, Arguments args, Type type, Location loc)
542                         : this (member, args, loc)
543                 {
544                         // When a return type is known not to be dynamic
545                         this.type = type;
546                 }
547
548                 public static DynamicInvocation CreateSpecialNameInvoke (ATypeNameExpression member, Arguments args, Location loc)
549                 {
550                         return new DynamicInvocation (member, args, loc) {
551                                 flags = CSharpBinderFlags.InvokeSpecialName
552                         };
553                 }
554
555                 public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
556                 {
557                         Arguments binder_args = new Arguments (member != null ? 5 : 3);
558                         bool is_member_access = member is MemberAccess;
559
560                         CSharpBinderFlags call_flags;
561                         if (!is_member_access && member is SimpleName) {
562                                 call_flags = CSharpBinderFlags.InvokeSimpleName;
563                                 is_member_access = true;
564                         } else {
565                                 call_flags = 0;
566                         }
567
568                         binder_args.Add (new Argument (new BinderFlags (call_flags, this)));
569
570                         if (is_member_access)
571                                 binder_args.Add (new Argument (new StringLiteral (member.Name, member.Location)));
572
573                         if (member != null && member.HasTypeArguments) {
574                                 TypeArguments ta = member.TypeArguments;
575                                 if (ta.Resolve (ec)) {
576                                         ArrayList targs = new ArrayList (ta.Count);
577                                         foreach (Type t in ta.Arguments)
578                                                 targs.Add (new TypeOf (new TypeExpression (t, loc), loc));
579
580                                         binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", targs, loc)));
581                                 }
582                         } else if (is_member_access) {
583                                 binder_args.Add (new Argument (new NullLiteral (loc)));
584                         }
585
586                         binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
587
588                         Expression real_args;
589                         if (args == null) {
590                                 // Cannot be null because .NET trips over
591                                 real_args = new ArrayCreation (new MemberAccess (GetBinderNamespace (loc), "CSharpArgumentInfo", loc), "[]", new ArrayList (0), loc);
592                         } else {
593                                 real_args = new ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (), loc);
594                         }
595
596                         binder_args.Add (new Argument (real_args));
597
598                         return new Invocation (GetBinder (is_member_access ? "InvokeMember" : "Invoke", loc), binder_args);
599                 }
600
601                 public override void EmitStatement (EmitContext ec)
602                 {
603                         flags |= CSharpBinderFlags.ResultDiscarded;
604                         base.EmitStatement (ec);
605                 }
606         }
607
608         class DynamicMemberBinder : DynamicMemberAssignable
609         {
610                 readonly string name;
611
612                 public DynamicMemberBinder (string name, Arguments args, Location loc)
613                         : base (args, loc)
614                 {
615                         this.name = name;
616                 }
617
618                 protected override Expression CreateCallSiteBinder (ResolveContext ec, Arguments args, bool isSet)
619                 {
620                         Arguments binder_args = new Arguments (4);
621
622                         binder_args.Add (new Argument (new BinderFlags (0, this)));
623                         binder_args.Add (new Argument (new StringLiteral (name, loc)));
624                         binder_args.Add (new Argument (new TypeOf (new TypeExpression (ec.CurrentType, loc), loc)));
625                         binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (), loc)));
626
627                         return new Invocation (GetBinder (isSet ? "SetMember" : "GetMember", loc), binder_args);
628                 }
629         }
630
631         //
632         // Any member binder which can be source and target of assignment
633         //
634         abstract class DynamicMemberAssignable : DynamicExpressionStatement, IDynamicBinder, IAssignMethod
635         {
636                 Expression setter;
637                 Arguments setter_args;
638
639                 protected DynamicMemberAssignable (Arguments args, Location loc)
640                         : base (null, args, loc)
641                 {
642                         base.binder = this;
643                 }
644
645                 public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
646                 {
647                         //
648                         // DoResolve always uses getter
649                         //
650                         return CreateCallSiteBinder (ec, args, false);
651                 }
652
653                 protected abstract Expression CreateCallSiteBinder (ResolveContext ec, Arguments args, bool isSet);
654
655                 public override Expression DoResolveLValue (ResolveContext rc, Expression right_side)
656                 {
657                         if (DoResolveCore (rc)) {
658                                 setter_args = new Arguments (Arguments.Count + 1);
659                                 setter_args.AddRange (Arguments);
660                                 setter_args.Add (new Argument (right_side));
661                                 setter = CreateCallSiteBinder (rc, setter_args, true);
662                         }
663
664                         eclass = ExprClass.Variable;
665                         return this;
666                 }
667
668                 public override void Emit (EmitContext ec)
669                 {
670                         // It's null for ResolveLValue used without assignment
671                         if (binder_expr == null)
672                                 EmitCall (ec, setter, Arguments, false);
673                         else
674                                 base.Emit (ec);
675                 }
676
677                 public override void EmitStatement (EmitContext ec)
678                 {
679                         // It's null for ResolveLValue used without assignment
680                         if (binder_expr == null)
681                                 EmitCall (ec, setter, Arguments, true);
682                         else
683                                 base.EmitStatement (ec);
684                 }
685
686                 #region IAssignMethod Members
687
688                 public void Emit (EmitContext ec, bool leave_copy)
689                 {
690                         throw new NotImplementedException ();
691                 }
692
693                 public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool prepare_for_load)
694                 {
695                         EmitCall (ec, setter, setter_args, !leave_copy);
696                 }
697
698                 #endregion
699         }
700
701         class DynamicUnaryConversion : DynamicExpressionStatement, IDynamicBinder
702         {
703                 string name;
704
705                 public DynamicUnaryConversion (string name, Arguments args, Location loc)
706                         : base (null, args, loc)
707                 {
708                         this.name = name;
709                         base.binder = this;
710                         if (name == "IsTrue" || name == "IsFalse")
711                                 type = TypeManager.bool_type;
712                 }
713
714                 public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
715                 {
716                         Arguments binder_args = new Arguments (3);
717
718                         MemberAccess sle = new MemberAccess (new MemberAccess (
719                                 new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "System", loc), "Linq", loc), "Expressions", loc);
720
721                         var flags = ec.HasSet (ResolveContext.Options.CheckedScope) ? CSharpBinderFlags.CheckedContext : 0;
722
723                         binder_args.Add (new Argument (new BinderFlags (flags, this)));
724                         binder_args.Add (new Argument (new MemberAccess (new MemberAccess (sle, "ExpressionType", loc), name, loc)));
725                         binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation ("[]", args.CreateDynamicBinderArguments (), loc)));
726
727                         return new Invocation (GetBinder ("UnaryOperation", loc), binder_args);
728                 }
729         }
730 }