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.Linq.Expressions;
32 using Compiler = Mono.CSharp;
33 using System.Reflection;
34 using System.Collections.Generic;
36 namespace Microsoft.CSharp.RuntimeBinder
40 static ConstructorInfo binder_exception_ctor;
41 static object resolver = new object ();
43 DynamicMetaObjectBinder binder;
44 Compiler.Expression expr;
45 BindingRestrictions restrictions;
46 DynamicMetaObject errorSuggestion;
48 public CSharpBinder (DynamicMetaObjectBinder binder, Compiler.Expression expr, DynamicMetaObject errorSuggestion)
52 this.restrictions = BindingRestrictions.Empty;
53 this.errorSuggestion = errorSuggestion;
56 public Compiler.ResolveContext.Options ResolveOptions { get; set; }
58 public void AddRestrictions (DynamicMetaObject arg)
60 restrictions = restrictions.Merge (CreateRestrictionsOnTarget (arg));
63 public void AddRestrictions (DynamicMetaObject[] args)
65 restrictions = restrictions.Merge (CreateRestrictionsOnTarget (args));
68 public DynamicMetaObject Bind (DynamicContext ctx, Type callingType)
72 var rc = new Compiler.ResolveContext (new RuntimeBinderContext (ctx, callingType), ResolveOptions);
74 // Static typemanager and internal caches are not thread-safe
76 expr = expr.Resolve (rc, Compiler.ResolveFlags.VariableOrValue);
80 throw new RuntimeBinderInternalCompilerException ("Expression resolved to null");
82 res = expr.MakeExpression (new Compiler.BuilderContext ());
83 } catch (RuntimeBinderException e) {
84 if (errorSuggestion != null)
85 return errorSuggestion;
87 res = CreateBinderException (e.Message);
89 if (errorSuggestion != null)
90 return errorSuggestion;
95 return new DynamicMetaObject (res, restrictions);
98 Expression CreateBinderException (string message)
100 if (binder_exception_ctor == null)
101 binder_exception_ctor = typeof (RuntimeBinderException).GetConstructor (new[] { typeof (string) });
104 // Uses target type to keep expressions composition working
106 return Expression.Throw (Expression.New (binder_exception_ctor, Expression.Constant (message)), binder.ReturnType);
109 static BindingRestrictions CreateRestrictionsOnTarget (DynamicMetaObject arg)
111 return arg.HasValue && arg.Value == null ?
112 BindingRestrictions.GetInstanceRestriction (arg.Expression, null) :
113 BindingRestrictions.GetTypeRestriction (arg.Expression, arg.LimitType);
116 public static BindingRestrictions CreateRestrictionsOnTarget (DynamicMetaObject[] args)
118 if (args.Length == 0)
119 return BindingRestrictions.Empty;
121 var res = CreateRestrictionsOnTarget (args[0]);
122 for (int i = 1; i < args.Length; ++i)
123 res = res.Merge (CreateRestrictionsOnTarget (args[i]));