[sgen] Lock around worker start, not only finishing
[mono.git] / mcs / class / Microsoft.CSharp / Microsoft.CSharp.RuntimeBinder / CSharpInvokeMemberBinder.cs
1 //
2 // CSharpInvokeMemberBinder.cs
3 //
4 // Authors:
5 //      Marek Safar  <marek.safar@gmail.com>
6 //
7 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Dynamic;
31 using System.Collections.Generic;
32 using System.Linq;
33 using Compiler = Mono.CSharp;
34 using SLE = System.Linq.Expressions;
35
36 namespace Microsoft.CSharp.RuntimeBinder
37 {
38         class CSharpInvokeMemberBinder : InvokeMemberBinder
39         {
40                 //
41                 // A custom runtime invocation is needed to deal with member invocation which
42                 // is not real member invocation but invocation on invocalble member.
43                 //
44                 // An example:
45                 // class C {
46                 //              dynamic f;
47                 //              void Foo ()
48                 //              {
49                 //                      dynamic d = new C ();
50                 //                      d.f.M ();
51                 //              }
52                 //
53                 // The runtime value of `f' can be a delegate in which case we are invoking result
54                 // of member invocation, this is already handled by DoResolveDynamic but we need
55                 // more runtime dependencies which require Microsoft.CSharp assembly reference or
56                 // a lot of reflection calls
57                 //
58                 class Invocation : Compiler.Invocation
59                 {
60                         sealed class RuntimeDynamicInvocation : Compiler.ShimExpression
61                         {
62                                 Invocation invoke;
63
64                                 public RuntimeDynamicInvocation (Invocation invoke, Compiler.Expression memberExpr)
65                                         : base (memberExpr)
66                                 {
67                                         this.invoke = invoke;
68                                 }
69
70                                 protected override Compiler.Expression DoResolve (Compiler.ResolveContext rc)
71                                 {
72                                         type = expr.Type;
73                                         eclass = Compiler.ExprClass.Value;
74                                         return this;
75                                 }
76
77                                 //
78                                 // Creates an invoke call on invocable expression
79                                 //
80                                 public override System.Linq.Expressions.Expression MakeExpression (Compiler.BuilderContext ctx)
81                                 {
82                                         var invokeBinder = invoke.invokeBinder;
83                                         var binder = Binder.Invoke (invokeBinder.flags, invokeBinder.callingContext, invokeBinder.argumentInfo);
84
85                                         var args = invoke.Arguments;
86                                         var args_expr = new SLE.Expression[invokeBinder.argumentInfo.Count];
87
88                                         var types = new Type [args_expr.Length + 2];
89
90                                         // Required by MakeDynamic
91                                         types[0] = typeof (System.Runtime.CompilerServices.CallSite);
92                                         types[1] = expr.Type.GetMetaInfo ();
93
94                                         args_expr[0] = expr.MakeExpression (ctx);
95
96                                         for (int i = 0; i < args.Count; ++i) {
97                                                 args_expr[i + 1] = args[i].Expr.MakeExpression (ctx);
98
99                                                 int type_index = i + 2;
100                                                 types[type_index] = args[i].Type.GetMetaInfo ();
101                                                 if (args[i].IsByRef)
102                                                         types[type_index] = types[type_index].MakeByRefType ();
103                                         }
104
105                                         // Return type goes last
106                                         bool void_result = (invokeBinder.flags & CSharpBinderFlags.ResultDiscarded) != 0;
107                                         types[types.Length - 1] = void_result ? typeof (void) : invokeBinder.ReturnType;
108
109                                         //
110                                         // Much easier to use Expression.Dynamic cannot be used because it ignores ByRef arguments
111                                         // and it always generates either Func or Action and any value type argument is lost
112                                         //
113                                         Type delegateType = SLE.Expression.GetDelegateType (types);
114                                         return SLE.Expression.MakeDynamic (delegateType, binder, args_expr);
115                                 }
116                         }
117
118                         readonly CSharpInvokeMemberBinder invokeBinder;
119
120                         public Invocation (Compiler.Expression expr, Compiler.Arguments arguments, CSharpInvokeMemberBinder invokeBinder)
121                                 : base (expr, arguments)
122                         {
123                                 this.invokeBinder = invokeBinder;
124                         }
125
126                         protected override Compiler.Expression DoResolveDynamic (Compiler.ResolveContext ec, Compiler.Expression memberExpr)
127                         {
128                                 return new RuntimeDynamicInvocation (this, memberExpr).Resolve (ec);
129                         }
130                 }
131
132                 readonly CSharpBinderFlags flags;
133                 IList<CSharpArgumentInfo> argumentInfo;
134                 IList<Type> typeArguments;
135                 Type callingContext;
136                 
137                 public CSharpInvokeMemberBinder (CSharpBinderFlags flags, string name, Type callingContext, IEnumerable<Type> typeArguments, IEnumerable<CSharpArgumentInfo> argumentInfo)
138                         : base (name, false, CSharpArgumentInfo.CreateCallInfo (argumentInfo, 1))
139                 {
140                         this.flags = flags;
141                         this.callingContext = callingContext;
142                         this.argumentInfo = argumentInfo.ToReadOnly ();
143                         this.typeArguments = typeArguments.ToReadOnly ();
144                 }
145                 
146                 public override DynamicMetaObject FallbackInvoke (DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
147                 {
148                         var b = new CSharpInvokeBinder (flags, callingContext, argumentInfo);
149                         
150                         // TODO: Is errorSuggestion ever used?
151                         return b.Defer (target, args);
152                 }
153                 
154                 public override DynamicMetaObject FallbackInvokeMember (DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
155                 {
156                         var ctx = DynamicContext.Create ();
157                         var c_args = ctx.CreateCompilerArguments (argumentInfo.Skip (1), args);
158                         var t_args = typeArguments == null ?
159                                 null :
160                                 new Compiler.TypeArguments (typeArguments.Select (l => new Compiler.TypeExpression (ctx.ImportType (l), Compiler.Location.Null)).ToArray ());
161
162                         var expr = ctx.CreateCompilerExpression (argumentInfo[0], target);
163
164                         //
165                         // Simple name invocation is actually member access invocation
166                         // to capture original this argument. This  brings problem when
167                         // simple name is resolved as a static invocation and member access
168                         // has to be reduced back to simple name without reporting an error
169                         //
170                         if ((flags & CSharpBinderFlags.InvokeSimpleName) != 0) {
171                                 var value = expr as Compiler.RuntimeValueExpression;
172                                 if (value != null)
173                                         value.IsSuggestionOnly = true;
174                         }
175
176                         expr = new Compiler.MemberAccess (expr, Name, t_args, Compiler.Location.Null);
177                         expr = new Invocation (expr, c_args, this);
178
179                         if ((flags & CSharpBinderFlags.ResultDiscarded) == 0)
180                                 expr = new Compiler.Cast (new Compiler.TypeExpression (ctx.ImportType (ReturnType), Compiler.Location.Null), expr, Compiler.Location.Null);
181                         else
182                                 expr = new Compiler.DynamicResultCast (ctx.ImportType (ReturnType), expr);
183
184                         var binder = new CSharpBinder (this, expr, errorSuggestion);
185                         binder.AddRestrictions (target);
186                         binder.AddRestrictions (args);
187
188                         if ((flags & CSharpBinderFlags.InvokeSpecialName) != 0)
189                                 binder.ResolveOptions |= Compiler.ResolveContext.Options.InvokeSpecialName;
190
191                         return binder.Bind (ctx, callingContext);
192                 }
193         }
194 }