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
38 static class CSharpBinder
40 static ConstructorInfo binder_exception_ctor;
41 static object compiler_initializer = new object ();
42 static object resolver = new object ();
44 public static DynamicMetaObject Bind (DynamicMetaObject target, Compiler.Expression expr, BindingRestrictions restrictions, DynamicMetaObject errorSuggestion)
46 return Bind (target, expr, null, restrictions, errorSuggestion);
49 public static DynamicMetaObject Bind (DynamicMetaObject target, Compiler.Expression expr, Type callingType, BindingRestrictions restrictions, DynamicMetaObject errorSuggestion)
51 var ctx = CreateDefaultCompilerContext ();
53 InitializeCompiler (ctx);
57 // TODO: ResolveOptions
58 Compiler.ResolveContext rc = new Compiler.ResolveContext (new RuntimeBinderContext (ctx, callingType));
60 // Static typemanager and internal caches are not thread-safe
62 expr = expr.Resolve (rc);
66 throw new RuntimeBinderInternalCompilerException ("Expression resolved to null");
68 res = expr.MakeExpression (new Compiler.BuilderContext ());
69 } catch (RuntimeBinderException e) {
70 if (errorSuggestion != null)
71 return errorSuggestion;
73 res = CreateBinderException (target, e.Message);
75 if (errorSuggestion != null)
76 return errorSuggestion;
81 return new DynamicMetaObject (res, restrictions);
84 public static Expression CreateBinderException (DynamicMetaObject target, string message)
86 if (binder_exception_ctor == null)
87 binder_exception_ctor = typeof (RuntimeBinderException).GetConstructor (new[] { typeof (string) });
90 // Uses target type to keep expressions composition working
92 return Expression.Throw (Expression.New (binder_exception_ctor, Expression.Constant (message)), target.LimitType);
96 // Creates mcs expression from dynamic method object
98 public static Compiler.Expression CreateCompilerExpression (CSharpArgumentInfo info, DynamicMetaObject value)
100 if (value.Value == null)
101 return new Compiler.NullLiteral (value.LimitType, Compiler.Location.Null);
104 if ((info.Flags & CSharpArgumentInfoFlags.LiteralConstant) != 0) {
105 InitializeCompiler (null);
106 return Compiler.Constant.CreateConstant (value.RuntimeType ?? value.LimitType, value.Value, Compiler.Location.Null);
109 if ((info.Flags & CSharpArgumentInfoFlags.IsStaticType) != 0)
110 return new Compiler.TypeExpression ((Type) value.Value, Compiler.Location.Null);
113 return new Compiler.RuntimeValueExpression (value);
116 public static Compiler.Arguments CreateCompilerArguments (IEnumerable<CSharpArgumentInfo> info, DynamicMetaObject[] args)
118 var res = new Compiler.Arguments (args.Length);
120 foreach (var item in info) {
121 var expr = CreateCompilerExpression (item, args [pos]);
123 res.Add (new Compiler.NamedArgument (new Compiler.LocatedToken (Compiler.Location.Null, item.Name), expr));
125 res.Add (new Compiler.Argument (expr, item.ArgumentModifier));
132 static Compiler.CompilerContext CreateDefaultCompilerContext ()
134 return new Compiler.CompilerContext (
135 new Compiler.Report (ErrorPrinter.Instance) {
141 public static BindingRestrictions CreateRestrictionsOnTarget (DynamicMetaObject arg)
143 return arg.HasValue && arg.Value == null ?
144 BindingRestrictions.GetInstanceRestriction (arg.Expression, null) :
145 BindingRestrictions.GetTypeRestriction (arg.Expression, arg.LimitType);
148 public static BindingRestrictions CreateRestrictionsOnTarget (DynamicMetaObject[] args)
150 if (args.Length == 0)
151 return BindingRestrictions.Empty;
153 var res = CreateRestrictionsOnTarget (args[0]);
154 for (int i = 1; i < args.Length; ++i)
155 res = res.Merge (CreateRestrictionsOnTarget (args[i]));
160 static void InitializeCompiler (Compiler.CompilerContext ctx)
162 if (Compiler.TypeManager.object_type != null)
165 lock (compiler_initializer) {
166 if (Compiler.TypeManager.object_type != null)
169 // TODO: This smells like pretty big issue
170 // AppDomain.CurrentDomain.AssemblyLoad += (sender, e) => { throw new NotImplementedException (); };
172 // Add all currently loaded assemblies
173 foreach (System.Reflection.Assembly a in AppDomain.CurrentDomain.GetAssemblies ())
174 Compiler.GlobalRootNamespace.Instance.AddAssemblyReference (a);
177 ctx = CreateDefaultCompilerContext ();
179 // FIXME: this is wrong
180 Compiler.RootContext.ToplevelTypes = new Compiler.ModuleContainer (ctx, true);
182 Compiler.TypeManager.InitCoreTypes (ctx);
183 Compiler.TypeManager.InitOptionalCoreTypes (ctx);
187 public static DynamicMetaObject Bind (DynamicMetaObject target, DynamicMetaObject errorSuggestion, DynamicMetaObject[] args)
189 return Bind (target, errorSuggestion);
192 public static DynamicMetaObject Bind (DynamicMetaObject target, DynamicMetaObject errorSuggestion)
194 return errorSuggestion ??
195 new DynamicMetaObject (
196 Expression.Constant (new object ()),
197 target.Restrictions.Merge (
198 BindingRestrictions.GetTypeRestriction (
199 target.Expression, target.LimitType)));