using System.Linq.Expressions;
using Compiler = Mono.CSharp;
using System.Reflection;
+using System.Collections.Generic;
namespace Microsoft.CSharp.RuntimeBinder
{
- class CSharpBinder
+ static class CSharpBinder
{
static ConstructorInfo binder_exception_ctor;
- static bool compiler_initialized;
static object compiler_initializer = new object ();
static object resolver = new object ();
- public static DynamicMetaObject Bind (DynamicMetaObject target, Compiler.Expression expr, BindingRestrictions restrictions, DynamicMetaObject errorSuggestion)
+ public static DynamicMetaObject Bind (DynamicMetaObjectBinder binder, Compiler.Expression expr, BindingRestrictions restrictions, DynamicMetaObject errorSuggestion)
{
- var report = new Compiler.Report (ErrorPrinter.Instance) { WarningLevel = 0 };
- var ctx = new Compiler.CompilerContext (report);
- Compiler.RootContext.ToplevelTypes = new Compiler.ModuleContainer (ctx, true);
+ return Bind (binder, expr, null, restrictions, errorSuggestion);
+ }
+
+ public static DynamicMetaObject Bind (DynamicMetaObjectBinder binder, Compiler.Expression expr, Type callingType, BindingRestrictions restrictions, DynamicMetaObject errorSuggestion)
+ {
+ var ctx = CreateDefaultCompilerContext ();
InitializeCompiler (ctx);
Expression res;
try {
// TODO: ResolveOptions
- Compiler.ResolveContext rc = new Compiler.ResolveContext (new RuntimeBinderContext (ctx));
+ Compiler.ResolveContext rc = new Compiler.ResolveContext (new RuntimeBinderContext (ctx, callingType));
// Static typemanager and internal caches are not thread-safe
lock (resolver) {
if (errorSuggestion != null)
return errorSuggestion;
- if (binder_exception_ctor == null)
- binder_exception_ctor = typeof (RuntimeBinderException).GetConstructor (new[] { typeof (string) });
-
- //
- // Uses target type to keep expressions composition working
- //
- res = Expression.Throw (Expression.New (binder_exception_ctor, Expression.Constant (e.Message)), target.LimitType);
+ res = CreateBinderException (binder, e.Message);
} catch (Exception) {
if (errorSuggestion != null)
return errorSuggestion;
return new DynamicMetaObject (res, restrictions);
}
+ public static Expression CreateBinderException (DynamicMetaObjectBinder binder, string message)
+ {
+ if (binder_exception_ctor == null)
+ binder_exception_ctor = typeof (RuntimeBinderException).GetConstructor (new[] { typeof (string) });
+
+ //
+ // Uses target type to keep expressions composition working
+ //
+ return Expression.Throw (Expression.New (binder_exception_ctor, Expression.Constant (message)), binder.ReturnType);
+ }
+
//
// Creates mcs expression from dynamic method object
//
- public static Compiler.Expression CreateCompilerExpression (CSharpArgumentInfo info, DynamicMetaObject value, bool typed)
+ public static Compiler.Expression CreateCompilerExpression (CSharpArgumentInfo info, DynamicMetaObject value)
{
- if (info != null && info.IsNamed)
- throw new NotImplementedException ("IsNamed");
-
if (value.Value == null)
return new Compiler.NullLiteral (value.LimitType, Compiler.Location.Null);
- if (info != null && (info.Flags & CSharpArgumentInfoFlags.LiteralConstant) != 0) {
- if (!typed)
- throw new NotImplementedException ("weakly typed constant");
+ if (info != null) {
+ if ((info.Flags & CSharpArgumentInfoFlags.LiteralConstant) != 0) {
+ InitializeCompiler (null);
+ return Compiler.Constant.CreateConstant (value.RuntimeType ?? value.LimitType, value.Value, Compiler.Location.Null);
+ }
- return Compiler.Constant.CreateConstant (value.RuntimeType ?? value.LimitType, value.Value, Compiler.Location.Null);
+ if ((info.Flags & CSharpArgumentInfoFlags.IsStaticType) != 0)
+ return new Compiler.TypeExpression ((Type) value.Value, Compiler.Location.Null);
}
- return new Compiler.RuntimeValueExpression (value, typed);
+ return new Compiler.RuntimeValueExpression (value);
+ }
+
+ public static Compiler.Arguments CreateCompilerArguments (IEnumerable<CSharpArgumentInfo> info, DynamicMetaObject[] args)
+ {
+ var res = new Compiler.Arguments (args.Length);
+ int pos = 0;
+ foreach (var item in info) {
+ var expr = CreateCompilerExpression (item, args [pos]);
+ if (item.IsNamed) {
+ res.Add (new Compiler.NamedArgument (new Compiler.LocatedToken (Compiler.Location.Null, item.Name), expr));
+ } else {
+ res.Add (new Compiler.Argument (expr, item.ArgumentModifier));
+ }
+ }
+
+ return res;
+ }
+
+ static Compiler.CompilerContext CreateDefaultCompilerContext ()
+ {
+ return new Compiler.CompilerContext (
+ new Compiler.Report (ErrorPrinter.Instance) {
+ WarningLevel = 0
+ }
+ );
}
public static BindingRestrictions CreateRestrictionsOnTarget (DynamicMetaObject arg)
BindingRestrictions.GetTypeRestriction (arg.Expression, arg.LimitType);
}
+ public static BindingRestrictions CreateRestrictionsOnTarget (DynamicMetaObject[] args)
+ {
+ if (args.Length == 0)
+ return BindingRestrictions.Empty;
+
+ var res = CreateRestrictionsOnTarget (args[0]);
+ for (int i = 1; i < args.Length; ++i)
+ res = res.Merge (CreateRestrictionsOnTarget (args[i]));
+
+ return res;
+ }
+
static void InitializeCompiler (Compiler.CompilerContext ctx)
{
- if (compiler_initialized)
+ if (Compiler.TypeManager.object_type != null)
return;
lock (compiler_initializer) {
- if (compiler_initialized)
+ if (Compiler.TypeManager.object_type != null)
return;
// TODO: This smells like pretty big issue
foreach (System.Reflection.Assembly a in AppDomain.CurrentDomain.GetAssemblies ())
Compiler.GlobalRootNamespace.Instance.AddAssemblyReference (a);
+ if (ctx == null)
+ ctx = CreateDefaultCompilerContext ();
+
+ // FIXME: this is wrong
+ Compiler.RootContext.ToplevelTypes = new Compiler.ModuleContainer (ctx, true);
+
Compiler.TypeManager.InitCoreTypes (ctx);
Compiler.TypeManager.InitOptionalCoreTypes (ctx);
- compiler_initialized = true;
}
}