{
Expression res;
try {
- var rc = new Compiler.ResolveContext (new RuntimeBinderContext (ctx, ctx.ImportType (callingType)), ResolveOptions);
+ var rc = new Compiler.ResolveContext (new RuntimeBinderContext (ctx, callingType), ResolveOptions);
// Static typemanager and internal caches are not thread-safe
lock (resolver) {
return res;
}
}
-
- class DynamicContext
- {
- static DynamicContext dc;
- static object compiler_initializer = new object ();
- static object lock_object = new object ();
-
- readonly Compiler.CompilerContext cc;
-
- private DynamicContext (Compiler.CompilerContext cc)
- {
- this.cc = cc;
- }
-
- public Compiler.CompilerContext CompilerContext {
- get {
- return cc;
- }
- }
-
- public static DynamicContext Create ()
- {
- if (dc != null)
- return dc;
-
- lock (compiler_initializer) {
- if (dc != null)
- return dc;
-
- var importer = new Compiler.ReflectionMetaImporter () {
- IgnorePrivateMembers = false
- };
-
- var reporter = new Compiler.Report (ErrorPrinter.Instance) {
- WarningLevel = 0
- };
-
- var cc = new Compiler.CompilerContext (importer, reporter) {
- IsRuntimeBinder = true
- };
-
- IList<Compiler.PredefinedTypeSpec> core_types = null;
- // HACK: To avoid re-initializing static TypeManager types, like string_type
- if (!Compiler.RootContext.EvalMode) {
- core_types = Compiler.TypeManager.InitCoreTypes ();
- }
-
- importer.Initialize ();
-
- //
- // Any later loaded assemblies are handled internally by GetAssemblyDefinition
- // domain.AssemblyLoad cannot be used as that would be too destructive as we
- // would hold all loaded assemblies even if they can be never visited
- //
- // TODO: Remove this code and rely on GetAssemblyDefinition only
- //
- Compiler.RootContext.ToplevelTypes = new Compiler.ModuleContainer (cc);
- var temp = Compiler.RootContext.ToplevelTypes.MakeExecutable ("dynamic");
-
- // Import all currently loaded assemblies
- var domain = AppDomain.CurrentDomain;
-
- temp.Create (domain, System.Reflection.Emit.AssemblyBuilderAccess.Run);
- foreach (var a in AppDomain.CurrentDomain.GetAssemblies ()) {
- importer.ImportAssembly (a, Compiler.RootContext.ToplevelTypes.GlobalRootNamespace);
- }
-
- if (!Compiler.RootContext.EvalMode) {
- Compiler.TypeManager.InitCoreTypes (Compiler.RootContext.ToplevelTypes, core_types);
- Compiler.TypeManager.InitOptionalCoreTypes (cc);
- }
-
- dc = new DynamicContext (cc);
- }
-
- return dc;
- }
-
- //
- // Creates mcs expression from dynamic object
- //
- public Compiler.Expression CreateCompilerExpression (CSharpArgumentInfo info, DynamicMetaObject value)
- {
- //
- // No type details provider, go with runtime type
- //
- if (info == null) {
- if (value.LimitType == typeof (object))
- return new Compiler.NullLiteral (Compiler.Location.Null);
-
- return new Compiler.RuntimeValueExpression (value, ImportType (value.RuntimeType));
- }
-
- //
- // Value is known to be a type
- //
- if ((info.Flags & CSharpArgumentInfoFlags.IsStaticType) != 0)
- return new Compiler.TypeExpression (ImportType ((Type) value.Value), Compiler.Location.Null);
-
- if (value.Value == null &&
- (info.Flags & (CSharpArgumentInfoFlags.IsOut | CSharpArgumentInfoFlags.IsRef | CSharpArgumentInfoFlags.UseCompileTimeType)) == 0 &&
- value.LimitType == typeof (object)) {
- return new Compiler.NullLiteral (Compiler.Location.Null);
- }
-
- //
- // Use compilation time type when type was known not to be dynamic during compilation
- //
- Type value_type = (info.Flags & CSharpArgumentInfoFlags.UseCompileTimeType) != 0 ? value.Expression.Type : value.LimitType;
- var type = ImportType (value_type);
-
- if ((info.Flags & CSharpArgumentInfoFlags.Constant) != 0)
- return Compiler.Constant.CreateConstantFromValue (type, value.Value, Compiler.Location.Null);
-
- return new Compiler.RuntimeValueExpression (value, type);
- }
-
- //
- // Creates mcs arguments from dynamic argument info
- //
- public Compiler.Arguments CreateCompilerArguments (IEnumerable<CSharpArgumentInfo> info, DynamicMetaObject[] args)
- {
- var res = new Compiler.Arguments (args.Length);
- int pos = 0;
-
- // enumerates over args
- foreach (var item in info) {
- var expr = CreateCompilerExpression (item, args[pos++]);
- if (item.IsNamed) {
- res.Add (new Compiler.NamedArgument (item.Name, Compiler.Location.Null, expr, item.ArgumentModifier));
- } else {
- res.Add (new Compiler.Argument (expr, item.ArgumentModifier));
- }
-
- if (pos == args.Length)
- break;
- }
-
- return res;
- }
-
- public Compiler.TypeSpec ImportType (Type type)
- {
- lock (lock_object) {
- return cc.MetaImporter.ImportType (type);
- }
- }
- }
}