2 // CSharpInvokeMemberBinder.cs
5 // Marek Safar <marek.safar@gmail.com>
7 // Copyright (C) 2009 Novell, Inc (http://www.novell.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
31 using System.Collections.Generic;
33 using Compiler = Mono.CSharp;
34 using SLE = System.Linq.Expressions;
36 namespace Microsoft.CSharp.RuntimeBinder
38 class CSharpInvokeMemberBinder : InvokeMemberBinder
41 // A custom runtime invocation is needed to deal with member invocation which
42 // is not real member invocation but invocation on invocalble member.
49 // dynamic d = new C ();
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
58 class Invocation : Compiler.Invocation
60 sealed class RuntimeDynamicInvocation : Compiler.ShimExpression
64 public RuntimeDynamicInvocation (Invocation invoke, Compiler.Expression memberExpr)
70 protected override Compiler.Expression DoResolve (Compiler.ResolveContext rc)
73 eclass = Compiler.ExprClass.Value;
78 // Creates an invoke call on invocable expression
80 public override System.Linq.Expressions.Expression MakeExpression (Compiler.BuilderContext ctx)
82 var invokeBinder = invoke.invokeBinder;
83 var binder = Binder.Invoke (invokeBinder.flags, invokeBinder.callingContext, invokeBinder.argumentInfo);
85 var args = invoke.Arguments;
86 var args_expr = new SLE.Expression[invokeBinder.argumentInfo.Count];
88 var types = new Type [args_expr.Length + 2];
90 // Required by MakeDynamic
91 types[0] = typeof (System.Runtime.CompilerServices.CallSite);
92 types[1] = expr.Type.GetMetaInfo ();
94 args_expr[0] = expr.MakeExpression (ctx);
96 for (int i = 0; i < args.Count; ++i) {
97 args_expr[i + 1] = args[i].Expr.MakeExpression (ctx);
99 int type_index = i + 2;
100 types[type_index] = args[i].Type.GetMetaInfo ();
102 types[type_index] = types[type_index].MakeByRefType ();
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;
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
113 Type delegateType = SLE.Expression.GetDelegateType (types);
114 return SLE.Expression.MakeDynamic (delegateType, binder, args_expr);
118 readonly CSharpInvokeMemberBinder invokeBinder;
120 public Invocation (Compiler.Expression expr, Compiler.Arguments arguments, CSharpInvokeMemberBinder invokeBinder)
121 : base (expr, arguments)
123 this.invokeBinder = invokeBinder;
126 protected override Compiler.Expression DoResolveDynamic (Compiler.ResolveContext ec, Compiler.Expression memberExpr)
128 return new RuntimeDynamicInvocation (this, memberExpr).Resolve (ec);
132 readonly CSharpBinderFlags flags;
133 IList<CSharpArgumentInfo> argumentInfo;
134 IList<Type> typeArguments;
137 public CSharpInvokeMemberBinder (CSharpBinderFlags flags, string name, Type callingContext, IEnumerable<Type> typeArguments, IEnumerable<CSharpArgumentInfo> argumentInfo)
138 : base (name, false, CSharpArgumentInfo.CreateCallInfo (argumentInfo, 1))
141 this.callingContext = callingContext;
142 this.argumentInfo = argumentInfo.ToReadOnly ();
143 this.typeArguments = typeArguments.ToReadOnly ();
146 public override DynamicMetaObject FallbackInvoke (DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
148 var b = new CSharpInvokeBinder (flags, callingContext, argumentInfo);
150 // TODO: Is errorSuggestion ever used?
151 return b.Defer (target, args);
154 public override DynamicMetaObject FallbackInvokeMember (DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
156 var ctx = DynamicContext.Create ();
157 var c_args = ctx.CreateCompilerArguments (argumentInfo.Skip (1), args);
158 var t_args = typeArguments == null ?
160 new Compiler.TypeArguments (typeArguments.Select (l => new Compiler.TypeExpression (ctx.ImportType (l), Compiler.Location.Null)).ToArray ());
162 var expr = ctx.CreateCompilerExpression (argumentInfo[0], target);
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
170 if ((flags & CSharpBinderFlags.InvokeSimpleName) != 0) {
171 var value = expr as Compiler.RuntimeValueExpression;
173 value.IsSuggestionOnly = true;
176 expr = new Compiler.MemberAccess (expr, Name, t_args, Compiler.Location.Null);
177 expr = new Invocation (expr, c_args, this);
179 if ((flags & CSharpBinderFlags.ResultDiscarded) == 0)
180 expr = new Compiler.Cast (new Compiler.TypeExpression (ctx.ImportType (ReturnType), Compiler.Location.Null), expr, Compiler.Location.Null);
182 expr = new Compiler.DynamicResultCast (ctx.ImportType (ReturnType), expr);
184 var binder = new CSharpBinder (this, expr, errorSuggestion);
185 binder.AddRestrictions (target);
186 binder.AddRestrictions (args);
188 if ((flags & CSharpBinderFlags.InvokeSpecialName) != 0)
189 binder.ResolveOptions |= Compiler.ResolveContext.Options.InvokeSpecialName;
191 return binder.Bind (ctx, callingContext);