1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
|
//
// dynamic.cs: support for dynamic expressions
//
// Authors: Marek Safar (marek.safar@gmail.com)
//
// 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 || MOBILE_DYNAMIC
using System.Dynamic;
#endif
namespace Mono.CSharp
{
//
// A copy of Microsoft.CSharp/Microsoft.CSharp.RuntimeBinder/CSharpBinderFlags.cs
// has to be kept in sync
//
[Flags]
public enum CSharpBinderFlags
{
None = 0,
CheckedContext = 1,
InvokeSimpleName = 1 << 1,
InvokeSpecialName = 1 << 2,
BinaryOperationLogical = 1 << 3,
ConvertExplicit = 1 << 4,
ConvertArrayIndex = 1 << 5,
ResultIndexed = 1 << 6,
ValueFromCompoundAssignment = 1 << 7,
ResultDiscarded = 1 << 8
}
//
// Type expression with internal dynamic type symbol
//
class DynamicTypeExpr : TypeExpr
{
public DynamicTypeExpr (Location loc)
{
this.loc = loc;
}
public override TypeSpec ResolveAsType (IMemberContext ec, bool allowUnboundTypeArguments)
{
eclass = ExprClass.Type;
type = ec.Module.Compiler.BuiltinTypes.Dynamic;
return type;
}
}
#region Dynamic runtime binder expressions
//
// Expression created from runtime dynamic object value by dynamic binder
//
public class RuntimeValueExpression : Expression, IDynamicAssign, IMemoryLocation
{
#if !NET_4_0 && !MOBILE_DYNAMIC
public class DynamicMetaObject
{
public TypeSpec RuntimeType;
public TypeSpec LimitType;
public SLE.Expression Expression;
}
#endif
readonly DynamicMetaObject obj;
public RuntimeValueExpression (DynamicMetaObject obj, TypeSpec type)
{
this.obj = obj;
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 ();
}
public override bool ContainsEmitWithAwait ()
{
throw new NotSupportedException ();
}
public override Expression CreateExpressionTree (ResolveContext ec)
{
throw new NotSupportedException ();
}
protected override Expression DoResolve (ResolveContext ec)
{
return this;
}
public override Expression DoResolveLValue (ResolveContext ec, Expression right_side)
{
return this;
}
public override void Emit (EmitContext ec)
{
throw new NotImplementedException ();
}
#region IAssignMethod Members
public void Emit (EmitContext ec, bool leave_copy)
{
throw new NotImplementedException ();
}
public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
{
throw new NotImplementedException ();
}
#endregion
public SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source)
{
return obj.Expression;
}
public override SLE.Expression MakeExpression (BuilderContext ctx)
{
#if STATIC
return base.MakeExpression (ctx);
#else
#if NET_4_0 || MOBILE_DYNAMIC
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
}
}
//
// Wraps runtime dynamic expression into expected type. Needed
// to satify expected type check by dynamic binder and no conversion
// is required (ResultDiscarded).
//
public class DynamicResultCast : ShimExpression
{
public DynamicResultCast (TypeSpec type, Expression expr)
: base (expr)
{
this.type = type;
}
protected override Expression DoResolve (ResolveContext ec)
{
expr = expr.Resolve (ec);
eclass = ExprClass.Value;
return this;
}
#if NET_4_0 || MOBILE_DYNAMIC
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
}
#endregion
//
// Creates dynamic binder expression
//
interface IDynamicBinder
{
Expression CreateCallSiteBinder (ResolveContext ec, Arguments args);
}
//
// Extends standard assignment interface for expressions
// supported by dynamic resolver
//
interface IDynamicAssign : IAssignMethod
{
SLE.Expression MakeAssignExpression (BuilderContext ctx, Expression source);
}
//
// Base dynamic expression statement creator
//
class DynamicExpressionStatement : ExpressionStatement
{
//
// Binder flag dynamic constant, the value is combination of
// flags known at resolve stage and flags known only at emit
// stage
//
protected class BinderFlags : EnumConstant
{
readonly DynamicExpressionStatement statement;
readonly CSharpBinderFlags flags;
public BinderFlags (CSharpBinderFlags flags, DynamicExpressionStatement statement)
: base (statement.loc)
{
this.flags = flags;
this.statement = statement;
eclass = 0;
}
protected override Expression DoResolve (ResolveContext ec)
{
Child = new IntConstant (ec.BuiltinTypes, (int) (flags | statement.flags), statement.loc);
type = ec.Module.PredefinedTypes.BinderFlags.Resolve ();
eclass = Child.eclass;
return this;
}
}
readonly Arguments arguments;
protected IDynamicBinder binder;
protected Expression binder_expr;
// Used by BinderFlags
protected CSharpBinderFlags flags;
TypeSpec binder_type;
TypeParameters context_mvars;
public DynamicExpressionStatement (IDynamicBinder binder, Arguments args, Location loc)
{
this.binder = binder;
this.arguments = args;
this.loc = loc;
}
public Arguments Arguments {
get {
return arguments;
}
}
public override bool ContainsEmitWithAwait ()
{
return arguments.ContainsEmitWithAwait ();
}
public override Expression CreateExpressionTree (ResolveContext ec)
{
ec.Report.Error (1963, loc, "An expression tree cannot contain a dynamic operation");
return null;
}
protected override Expression DoResolve (ResolveContext rc)
{
if (DoResolveCore (rc))
binder_expr = binder.CreateCallSiteBinder (rc, arguments);
return this;
}
protected bool DoResolveCore (ResolveContext rc)
{
foreach (var arg in arguments) {
if (arg.Type == InternalType.VarOutType) {
// Should be special error message about dynamic dispatch
rc.Report.Error (8047, arg.Expr.Location, "Declaration expression cannot be used in this context");
}
}
if (rc.CurrentTypeParameters != null && rc.CurrentTypeParameters[0].IsMethodTypeParameter)
context_mvars = rc.CurrentTypeParameters;
int errors = rc.Report.Errors;
var pt = rc.Module.PredefinedTypes;
binder_type = pt.Binder.Resolve ();
pt.CallSite.Resolve ();
pt.CallSiteGeneric.Resolve ();
eclass = ExprClass.Value;
if (type == null)
type = rc.BuiltinTypes.Dynamic;
if (rc.Report.Errors == errors)
return true;
rc.Report.Error (1969, loc,
"Dynamic operation cannot be compiled without `Microsoft.CSharp.dll' assembly reference");
return false;
}
public override void Emit (EmitContext ec)
{
EmitCall (ec, binder_expr, arguments, false);
}
public override void EmitStatement (EmitContext ec)
{
EmitCall (ec, binder_expr, arguments, true);
}
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;
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);
TypeExpression[] targs_for_instance = null;
TypeParameterMutator mutator;
var site_container = ec.CreateDynamicSite ();
if (context_mvars != null) {
TypeParameters tparam;
TypeContainer sc = site_container;
do {
tparam = sc.CurrentTypeParameters;
sc = sc.Parent;
} while (tparam == null);
mutator = new TypeParameterMutator (context_mvars, tparam);
if (!ec.IsAnonymousStoreyMutateRequired) {
targs_for_instance = new TypeExpression[targs.Length];
targs_for_instance[0] = targs[0];
}
} else {
mutator = null;
}
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;
var t = a.Type;
// Convert any internal type like dynamic or null to object
if (t.Kind == MemberKind.InternalCompilerType)
t = ec.BuiltinTypes.Object;
if (targs_for_instance != null)
targs_for_instance[i + 1] = new TypeExpression (t, loc);
if (mutator != null)
t = t.Mutate (mutator);
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 = isStatement ? "Action" : "Func";
TypeSpec 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);
if (mutator != null)
t = t.Mutate (mutator);
targs[targs.Length - 1] = new TypeExpression (t, loc);
}
del_type = new GenericTypeExpr (te, new TypeArguments (targs), loc);
if (targs_for_instance != null)
del_type_instance_access = new GenericTypeExpr (te, new TypeArguments (targs_for_instance), loc);
else
del_type_instance_access = del_type;
}
}
//
// Create custom delegate when no appropriate predefined delegate has been found
//
Delegate d;
if (del_type == null) {
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.Containers == null ? 0 : site.Containers.Count;
if (mutator != null)
rt = mutator.Mutate (rt);
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);
}
d = new Delegate (site, new TypeExpression (rt, loc),
Modifiers.INTERNAL | Modifiers.COMPILER_GENERATED,
new MemberName ("Container" + index.ToString ("X")),
new ParametersCompiled (p), null);
d.CreateContainer ();
d.DefineContainer ();
d.Define ();
d.PrepareEmit ();
site.AddTypeContainer (d);
//
// Add new container to inflated site container when the
// member cache already exists
//
if (site.CurrentType is InflatedTypeSpec && index > 0)
site.CurrentType.MemberCache.AddMember (d.CurrentType);
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;
}
}
Expression target = new DelegateInvocation (new MemberAccess (site_field_expr, "Target", loc).Resolve (bc), args, false, loc).Resolve (bc);
if (target != null)
target.Emit (ec);
}
}
public override void FlowAnalysis (FlowAnalysisContext fc)
{
arguments.FlowAnalysis (fc);
}
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);
}
}
//
// Dynamic member access compound assignment for events
//
class DynamicEventCompoundAssign : ExpressionStatement
{
class IsEvent : DynamicExpressionStatement, IDynamicBinder
{
string name;
public IsEvent (string name, Arguments args, Location loc)
: base (null, args, loc)
{
this.name = name;
binder = this;
}
public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
{
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 (ec.BuiltinTypes, name, loc)));
binder_args.Add (new Argument (new TypeOf (ec.CurrentType, loc)));
return new Invocation (GetBinder ("IsEvent", loc), binder_args);
}
}
Expression condition;
ExpressionStatement invoke, assign;
public DynamicEventCompoundAssign (string name, Arguments args, ExpressionStatement assignment, ExpressionStatement invoke, Location loc)
{
condition = new IsEvent (name, args, loc);
this.invoke = invoke;
this.assign = assignment;
this.loc = loc;
}
public override Expression CreateExpressionTree (ResolveContext ec)
{
return condition.CreateExpressionTree (ec);
}
protected override Expression DoResolve (ResolveContext rc)
{
type = rc.BuiltinTypes.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)
{
var stmt = new If (condition, new StatementExpression (invoke), new StatementExpression (assign), loc);
using (ec.With (BuilderContext.Options.OmitDebugInfo, true)) {
stmt.Emit (ec);
}
}
public override void FlowAnalysis (FlowAnalysisContext fc)
{
invoke.FlowAnalysis (fc);
}
}
class DynamicConversion : DynamicExpressionStatement, IDynamicBinder
{
public DynamicConversion (TypeSpec targetType, CSharpBinderFlags flags, Arguments args, Location loc)
: base (null, args, loc)
{
type = targetType;
base.flags = flags;
base.binder = this;
}
public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
{
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 (type, loc)));
binder_args.Add (new Argument (new TypeOf (ec.CurrentType, loc)));
return new Invocation (GetBinder ("Convert", loc), binder_args);
}
}
class DynamicConstructorBinder : DynamicExpressionStatement, IDynamicBinder
{
public DynamicConstructorBinder (TypeSpec type, Arguments args, Location loc)
: base (null, args, loc)
{
this.type = type;
base.binder = this;
}
public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
{
Arguments binder_args = new Arguments (3);
binder_args.Add (new Argument (new BinderFlags (0, this)));
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);
}
}
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 (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
{
readonly ATypeNameExpression member;
public DynamicInvocation (ATypeNameExpression member, Arguments args, Location loc)
: base (null, args, loc)
{
base.binder = this;
this.member = member;
}
public static DynamicInvocation CreateSpecialNameInvoke (ATypeNameExpression member, Arguments args, Location loc)
{
return new DynamicInvocation (member, args, loc) {
flags = CSharpBinderFlags.InvokeSpecialName
};
}
public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
{
Arguments binder_args = new Arguments (member != null ? 5 : 3);
bool is_member_access = member is MemberAccess;
CSharpBinderFlags call_flags;
if (!is_member_access && member is SimpleName) {
call_flags = CSharpBinderFlags.InvokeSimpleName;
is_member_access = true;
} else {
call_flags = 0;
}
binder_args.Add (new Argument (new BinderFlags (call_flags, this)));
if (is_member_access)
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, false)) {
var targs = new ArrayInitializer (ta.Count, loc);
foreach (TypeSpec t in ta.Arguments)
targs.Add (new TypeOf (t, loc));
binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation (targs, loc)));
}
} else if (is_member_access) {
binder_args.Add (new Argument (new NullLiteral (loc)));
}
binder_args.Add (new Argument (new TypeOf (ec.CurrentType, loc)));
Expression real_args;
if (args == null) {
// Cannot be null because .NET trips over
real_args = new ArrayCreation (
new MemberAccess (GetBinderNamespace (loc), "CSharpArgumentInfo", loc),
new ArrayInitializer (0, loc), loc);
} else {
real_args = new ImplicitlyTypedArrayCreation (args.CreateDynamicBinderArguments (ec), loc);
}
binder_args.Add (new Argument (real_args));
return new Invocation (GetBinder (is_member_access ? "InvokeMember" : "Invoke", loc), binder_args);
}
public override void EmitStatement (EmitContext ec)
{
flags |= CSharpBinderFlags.ResultDiscarded;
base.EmitStatement (ec);
}
}
class DynamicMemberBinder : DynamicMemberAssignable
{
readonly string name;
public DynamicMemberBinder (string name, Arguments args, Location loc)
: base (args, loc)
{
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 (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);
}
}
//
// Any member binder which can be source and target of assignment
//
abstract class DynamicMemberAssignable : DynamicExpressionStatement, IDynamicBinder, IAssignMethod
{
Expression setter;
Arguments setter_args;
protected DynamicMemberAssignable (Arguments args, Location loc)
: base (null, args, loc)
{
base.binder = this;
}
public Expression CreateCallSiteBinder (ResolveContext ec, Arguments args)
{
//
// DoResolve always uses getter
//
return CreateCallSiteBinder (ec, args, false);
}
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) {
right_side.DoResolveLValue (rc, this);
return null;
}
if (DoResolveCore (rc)) {
setter_args = CreateSetterArguments (rc, right_side);
setter = CreateCallSiteBinder (rc, setter_args, true);
}
eclass = ExprClass.Variable;
return this;
}
public override void Emit (EmitContext ec)
{
// It's null for ResolveLValue used without assignment
if (binder_expr == null)
EmitCall (ec, setter, Arguments, false);
else
base.Emit (ec);
}
public override void EmitStatement (EmitContext ec)
{
// It's null for ResolveLValue used without assignment
if (binder_expr == null)
EmitCall (ec, setter, Arguments, true);
else
base.EmitStatement (ec);
}
#region IAssignMethod Members
public void Emit (EmitContext ec, bool leave_copy)
{
throw new NotImplementedException ();
}
public void EmitAssign (EmitContext ec, Expression source, bool leave_copy, bool isCompound)
{
EmitCall (ec, setter, setter_args, !leave_copy);
}
#endregion
}
class DynamicUnaryConversion : DynamicExpressionStatement, IDynamicBinder
{
readonly string name;
public DynamicUnaryConversion (string name, Arguments args, Location loc)
: base (null, args, loc)
{
this.name = name;
base.binder = this;
}
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)
{
Arguments binder_args = new Arguments (4);
MemberAccess sle = new MemberAccess (new MemberAccess (
new QualifiedAliasMember (QualifiedAliasMember.GlobalAlias, "System", loc), "Linq", loc), "Expressions", loc);
var flags = ec.HasSet (ResolveContext.Options.CheckedScope) ? CSharpBinderFlags.CheckedContext : 0;
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 (ec.CurrentType, loc)));
binder_args.Add (new Argument (new ImplicitlyTypedArrayCreation (args.CreateDynamicBinderArguments (ec), loc)));
return new Invocation (GetBinder ("UnaryOperation", loc), binder_args);
}
}
sealed class DynamicSiteClass : HoistedStoreyClass
{
public DynamicSiteClass (TypeDefinition parent, MemberBase host, TypeParameters tparams)
: base (parent, MakeMemberName (host, "DynamicSite", parent.DynamicSitesCounter, tparams, Location.Null), tparams, Modifiers.STATIC, MemberKind.Class)
{
parent.DynamicSitesCounter++;
}
public FieldSpec CreateCallSiteField (FullNamedExpression type, Location loc)
{
int index = AnonymousMethodsCounter++;
Field f = new HoistedField (this, type, Modifiers.PUBLIC | Modifiers.STATIC, "Site" + index.ToString ("X"), null, loc);
f.Define ();
AddField (f);
return f.Spec;
}
}
}
|