Merge pull request #1156 from felfert/master
[mono.git] / mcs / class / Microsoft.CSharp / Microsoft.CSharp.RuntimeBinder / DynamicContext.cs
index d1266003c7c2bd25bd1f372612d59d653b1abcca..9c466c5ed9c9875b31f69f0f0ec9654075f3ac37 100644 (file)
@@ -70,22 +70,14 @@ namespace Microsoft.CSharp.RuntimeBinder
                                if (dc != null)
                                        return dc;
 
-                               var reporter = new Compiler.Report (ErrorPrinter.Instance) {
+                               var settings = new Compiler.CompilerSettings () {
                                        WarningLevel = 0
                                };
 
-                               var settings = new Compiler.CompilerSettings ();
-
-                               var cc = new Compiler.CompilerContext (settings, reporter) {
+                               var cc = new Compiler.CompilerContext (settings, ErrorPrinter.Instance) {
                                        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 ();
-                               //}
-
                                //
                                // Any later loaded assemblies are handled internally by GetAssemblyDefinition
                                // domain.AssemblyLoad cannot be used as that would be too destructive as we
@@ -94,28 +86,26 @@ namespace Microsoft.CSharp.RuntimeBinder
                                // TODO: Remove this code and rely on GetAssemblyDefinition only
                                //
                                var module = new Compiler.ModuleContainer (cc);
+                               module.HasTypesFullyDefined = true;
+                               
+                               // Setup fake assembly, it's used mostly to simplify checks like friend-access
                                var temp = new Compiler.AssemblyDefinitionDynamic (module, "dynamic");
                                module.SetDeclaringAssembly (temp);
 
-                               // Import all currently loaded assemblies
-                               var domain = AppDomain.CurrentDomain;
-
-                               temp.Create (domain, System.Reflection.Emit.AssemblyBuilderAccess.Run);
                                var importer = new Compiler.ReflectionImporter (module, cc.BuiltinTypes) {
                                        IgnorePrivateMembers = false
                                };
 
-                               bool reinitialized = Compiler.RootContext.ToplevelTypes != null;
-                               Compiler.RootContext.ToplevelTypes = module;
-
+                               // Import all currently loaded assemblies
+                               // TODO: Rewrite this to populate type cache on-demand, that should greatly
+                               // reduce our start-up cost
+                               var domain = AppDomain.CurrentDomain;
                                foreach (var a in AppDomain.CurrentDomain.GetAssemblies ()) {
                                        importer.ImportAssembly (a, module.GlobalRootNamespace);
                                }
 
-                               if (!reinitialized) {
-                                       cc.BuiltinTypes.CheckDefinitions (module);
-                                       module.InitializePredefinedTypes ();
-                               }
+                               cc.BuiltinTypes.CheckDefinitions (module);
+                               module.InitializePredefinedTypes ();
 
                                dc = new DynamicContext (module, importer);
                        }
@@ -156,8 +146,14 @@ namespace Microsoft.CSharp.RuntimeBinder
                        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);
+                       if ((info.Flags & CSharpArgumentInfoFlags.Constant) != 0) {
+                               var c = Compiler.Constant.CreateConstantFromValue (type, value.Value, Compiler.Location.Null);
+                               //
+                               // It can be null for misused Constant flag
+                               //
+                               if (c != null)
+                                       return c;
+                       }
 
                        return new Compiler.RuntimeValueExpression (value, type);
                }