2010-04-29 Marek Safar <marek.safar@gmail.com>
[mono.git] / mcs / mcs / eval.cs
index ba5162a9ac29e574454d34873771d736a9759c09..2c2f297266e895895e75f0b79ee8c3f56d4744c5 100644 (file)
@@ -59,7 +59,7 @@ namespace Mono.CSharp {
                internal static List<NamespaceEntry.UsingEntry> using_list = new List<NamespaceEntry.UsingEntry> ();
                static Dictionary<string, FieldInfo> fields = new Dictionary<string, FieldInfo> ();
 
-               static Type   interactive_base_class = typeof (InteractiveBase);
+               static TypeSpec interactive_base_class;
                static Driver driver;
                static bool inited;
 
@@ -89,7 +89,7 @@ namespace Mono.CSharp {
                {
                        return ctx.Report.SetPrinter (report_printer);
                }                               
-               
+
                /// <summary>
                ///   Optional initialization for the Evaluator.
                /// </summary>
@@ -127,8 +127,13 @@ namespace Mono.CSharp {
                                
                                CompilerCallableEntryPoint.Reset ();
                                RootContext.ToplevelTypes = new ModuleCompiled (ctx, true);
+                               /*var ctypes = */TypeManager.InitCoreTypes ();
+                               TypeManager.InitExpressionTypes ();
 
+                               Import.Initialize ();
                                driver.LoadReferences ();
+                               TypeManager.InitOptionalCoreTypes (ctx);
+
                                RootContext.EvalMode = true;
                                inited = true;
 
@@ -144,6 +149,7 @@ namespace Mono.CSharp {
                static void Reset ()
                {
                        CompilerCallableEntryPoint.PartialReset ();
+                       RootContext.PartialReset ();
                        
                        // Workaround for API limitation where full message printer cannot be passed
                        ReportPrinter printer;
@@ -160,9 +166,9 @@ namespace Mono.CSharp {
                        //
                        // PartialReset should not reset the core types, this is very redundant.
                        //
-                       if (!TypeManager.InitCoreTypes (ctx))
-                               throw new Exception ("Failed to InitCoreTypes");
-                       TypeManager.InitOptionalCoreTypes (ctx);
+//                     if (!TypeManager.InitCoreTypes (ctx, null))
+//                             throw new Exception ("Failed to InitCoreTypes");
+//                     TypeManager.InitOptionalCoreTypes (ctx);
                        
                        Location.AddFile (null, "{interactive}");
                        Location.Initialize ();
@@ -188,18 +194,22 @@ namespace Mono.CSharp {
                ///   base class and the static members that are
                ///   available to your evaluated code.
                /// </remarks>
-               static public Type InteractiveBaseClass {
+               static public TypeSpec InteractiveBaseClass {
                        get {
-                               return interactive_base_class;
+                               if (interactive_base_class != null)
+                                       return interactive_base_class;
+
+                               return Import.ImportType (typeof (InteractiveBase));
                        }
+               }
 
-                       set {
-                               if (value == null)
-                                       throw new ArgumentNullException ();
+               public static void SetInteractiveBaseClass (Type type)
+               {
+                       if (type == null)
+                               throw new ArgumentNullException ();
 
-                               lock (evaluator_lock)
-                                       interactive_base_class = value;
-                       }
+                       lock (evaluator_lock)
+                               interactive_base_class = Import.ImportType (type);
                }
 
                /// <summary>
@@ -726,8 +736,10 @@ namespace Mono.CSharp {
                        }
                        
                        RootContext.EmitCode ();
-                       if (Report.Errors != 0)
+                       if (Report.Errors != 0){
+                               undo.ExecuteUndo ();
                                return null;
+                       }
                        
                        RootContext.CloseTypes ();
 
@@ -741,7 +753,7 @@ namespace Mono.CSharp {
                        // Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
                        // work from MethodBuilders.   Retarded, I know.
                        //
-                       Type tt = CodeGen.Assembly.Builder.GetType (tb.Name);
+                       var tt = CodeGen.Assembly.Builder.GetType (tb.Name);
                        MethodInfo mi = tt.GetMethod (mb.Name);
                        
                        // Pull the FieldInfos from the type, and keep track of them
@@ -753,7 +765,7 @@ namespace Mono.CSharp {
                                // If a previous value was set, nullify it, so that we do
                                // not leak memory
                                if (fields.TryGetValue (field.Name, out old)){
-                                       if (TypeManager.IsStruct (old.FieldType)){
+                                       if (old.FieldType.IsValueType){
                                                //
                                                // TODO: Clear fields for structs
                                                //
@@ -859,7 +871,7 @@ namespace Mono.CSharp {
                                foreach (var de in fields){
                                        FieldInfo fi = LookupField (de.Key);
                                        object value = null;
-                                       bool error = false;
+                                       //bool error = false;
                                        
                                        try {
                                                if (value == null)
@@ -868,13 +880,14 @@ namespace Mono.CSharp {
                                                if (value is string)
                                                        value = Quote ((string)value);
                                        } catch {
-                                               error = true;
+                                               //error = true;
                                        }
-                                       
-                                       if (error)
-                                               sb.Append (String.Format ("{0} {1} <error reading value>", TypeManager.CSharpName(fi.FieldType), de.Key));
-                                       else
-                                               sb.Append (String.Format ("{0} {1} = {2}", TypeManager.CSharpName(fi.FieldType), de.Key, value));
+
+                                       throw new NotImplementedException ("net");
+                                       //if (error)
+                                       //    sb.Append (String.Format ("{0} {1} <error reading value>", TypeManager.CSharpName(fi.FieldType), de.Key));
+                                       //else
+                                       //    sb.Append (String.Format ("{0} {1} = {2}", TypeManager.CSharpName(fi.FieldType), de.Key, value));
                                }
                                
                                return sb.ToString ();
@@ -898,8 +911,9 @@ namespace Mono.CSharp {
                static public void ReferenceAssembly (Assembly a)
                {
                        lock (evaluator_lock){
-                               GlobalRootNamespace.Instance.AddAssemblyReference (a);
-                               GlobalRootNamespace.Instance.ComputeNamespaces (ctx);
+//                             GlobalRootNamespace.Instance.AddAssemblyReference (a);
+//                             GlobalRootNamespace.Instance.ComputeNamespaces (ctx);
+                               GlobalRootNamespace.Instance.ImportAssembly (a);
                        }
                }
 
@@ -1084,12 +1098,12 @@ namespace Mono.CSharp {
                        if (x == null)
                                return "";
                        
-                       Type t = x as Type;
-                       if (t == null)
-                               t = x.GetType ();
+                       TypeSpec t = x as TypeSpec;
+//                     if (t == null)
+//                             t = x.GetType ();
 
                        StringWriter sw = new StringWriter ();
-                       new Outline (t, sw, true, false, false).OutlineType ();
+                       new Outline (t.GetMetaInfo (), sw, true, false, false).OutlineType ();
                        return sw.ToString ();
                }
 #endif
@@ -1165,14 +1179,14 @@ namespace Mono.CSharp {
                        CloneContext cc = new CloneContext ();
                        Expression clone = source.Clone (cc);
 
-                       var old_printer = Evaluator.SetPrinter (new StreamReportPrinter (TextWriter.Null));
-                       clone = clone.Resolve (ec);
-                       if (clone == null){
-                               //
-                               // A useful feature for the REPL: if we can resolve the expression
-                               // as a type, Describe the type;
-                               //
-                               if (Evaluator.DescribeTypeExpressions){
+                       //
+                       // A useful feature for the REPL: if we can resolve the expression
+                       // as a type, Describe the type;
+                       //
+                       if (Evaluator.DescribeTypeExpressions){
+                               var old_printer = Evaluator.SetPrinter (new StreamReportPrinter (TextWriter.Null));
+                               clone = clone.Resolve (ec);
+                               if (clone == null){
                                        clone = source.Clone (cc);
                                        clone = clone.Resolve (ec, ResolveFlags.Type);
                                        if (clone == null){
@@ -1181,16 +1195,17 @@ namespace Mono.CSharp {
                                                clone = clone.Resolve (ec);
                                                return null;
                                        }
-
+                                       
                                        Arguments args = new Arguments (1);
                                        args.Add (new Argument (new TypeOf (source, Location)));
                                        source = new Invocation (new SimpleName ("Describe", Location), args).Resolve (ec);
-                               } else {
-                                       Evaluator.SetPrinter (old_printer);
-                                       return null;
                                }
+                               Evaluator.SetPrinter (old_printer);
+                       } else {
+                               clone = clone.Resolve (ec);
+                               if (clone == null)
+                                       return null;
                        }
-                       Evaluator.SetPrinter (old_printer);
        
                        // This means its really a statement.
                        if (clone.Type == TypeManager.void_type){