Removed explicit casts
[mono.git] / mcs / mcs / eval.cs
index a1ba9e235371cb7d79b0420af2067d871c645d43..6782d573a406830981cd1b06a90daff32eae586c 100644 (file)
@@ -57,9 +57,9 @@ namespace Mono.CSharp {
 
                static List<NamespaceEntry.UsingAliasEntry> using_alias_list = new List<NamespaceEntry.UsingAliasEntry> ();
                internal static List<NamespaceEntry.UsingEntry> using_list = new List<NamespaceEntry.UsingEntry> ();
-               static Dictionary<string, FieldInfo> fields = new Dictionary<string, FieldInfo> ();
+               static Dictionary<string, Tuple<FieldSpec, FieldInfo>> fields = new Dictionary<string, Tuple<FieldSpec, FieldInfo>> ();
 
-               static Type   interactive_base_class = typeof (InteractiveBase);
+               static TypeSpec interactive_base_class;
                static Driver driver;
                static bool inited;
 
@@ -127,8 +127,12 @@ namespace Mono.CSharp {
                                
                                CompilerCallableEntryPoint.Reset ();
                                RootContext.ToplevelTypes = new ModuleCompiled (ctx, true);
+                               /*var ctypes = */TypeManager.InitCoreTypes ();
 
+                               Import.Initialize ();
                                driver.LoadReferences ();
+                               TypeManager.InitOptionalCoreTypes (ctx);
+
                                RootContext.EvalMode = true;
                                inited = true;
 
@@ -144,6 +148,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 +165,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 +193,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>
@@ -743,31 +752,33 @@ 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
                        foreach (Field field in queued_fields){
                                FieldInfo fi = tt.GetField (field.Name);
-                               
-                               FieldInfo old;
+
+                               Tuple<FieldSpec, FieldInfo> old;
                                
                                // 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 (fields.TryGetValue (field.Name, out old)) {
+                                       if (old.Item1.MemberType.IsStruct) {
                                                //
                                                // TODO: Clear fields for structs
                                                //
                                        } else {
                                                try {
-                                                       old.SetValue (null, null);
+                                                       old.Item2.SetValue (null, null);
                                                } catch {
                                                }
                                        }
+
+                                       fields [field.Name] = Tuple.Create (old.Item1, fi);
+                               } else {
+                                       fields.Add (field.Name, Tuple.Create (field.Spec, fi));
                                }
-                               
-                               fields [field.Name] = fi;
                        }
                        //types.Add (tb);
 
@@ -790,12 +801,10 @@ namespace Mono.CSharp {
                public class NoValueSet {
                }
 
-               static internal FieldInfo LookupField (string name)
+               static internal Tuple<FieldSpec, FieldInfo> LookupField (string name)
                {
-                       FieldInfo fi;
-                       if (!fields.TryGetValue (name, out fi))
-                               return null;
-
+                       Tuple<FieldSpec, FieldInfo> fi;
+                       fields.TryGetValue (name, out fi);
                        return fi;
                }
 
@@ -859,24 +868,18 @@ namespace Mono.CSharp {
                                StringBuilder sb = new StringBuilder ();
                                
                                foreach (var de in fields){
-                                       FieldInfo fi = LookupField (de.Key);
-                                       object value = null;
-                                       bool error = false;
-                                       
+                                       var fi = LookupField (de.Key);
+                                       object value;
                                        try {
-                                               if (value == null)
-                                                       value = "null";
-                                               value = fi.GetValue (null);
+                                               value = fi.Item2.GetValue (null);
                                                if (value is string)
                                                        value = Quote ((string)value);
                                        } catch {
-                                               error = true;
+                                               value = "<error reading value>";
                                        }
-                                       
-                                       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));
+
+                                       sb.AppendFormat ("{0} {1} = {2}", fi.Item1.MemberType.GetSignatureForError (), de.Key, value);
+                                       sb.AppendLine ();
                                }
                                
                                return sb.ToString ();
@@ -900,8 +903,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);
                        }
                }
 
@@ -1046,17 +1050,17 @@ namespace Mono.CSharp {
                /// </summary>
                static public string help {
                        get {
-                               return  "Static methods:\n"+
-                                       "  Describe(obj)      - Describes the object's type\n" + 
-                                       "  LoadPackage (pkg); - Loads the given Package (like -pkg:FILE)\n" +
-                                       "  LoadAssembly (ass) - Loads the given assembly (like -r:ASS)\n" + 
-                                       "  ShowVars ();       - Shows defined local variables.\n" +
-                                       "  ShowUsing ();      - Show active using decltions.\n" +
-                                       "  Prompt             - The prompt used by the C# shell\n" +
-                                       "  ContinuationPrompt - The prompt for partial input\n" +
-                                       "  Time(() -> { })    - Times the specified code\n" +
-                                       "  quit;\n" +
-                                       "  help;\n";
+                               return "Static methods:\n" +
+                                       "  Describe (object)       - Describes the object's type\n" +
+                                       "  LoadPackage (package);  - Loads the given Package (like -pkg:FILE)\n" +
+                                       "  LoadAssembly (assembly) - Loads the given assembly (like -r:ASSEMBLY)\n" +
+                                       "  ShowVars ();            - Shows defined local variables.\n" +
+                                       "  ShowUsing ();           - Show active using declarations.\n" +
+                                       "  Prompt                  - The prompt used by the C# shell\n" +
+                                       "  ContinuationPrompt      - The prompt for partial input\n" +
+                                       "  Time(() -> { })         - Times the specified code\n" +
+                                       "  quit;                   - You'll never believe it - this quits the repl!\n" +
+                                       "  help;                   - This help text\n";
                        }
                }
 
@@ -1084,14 +1088,12 @@ namespace Mono.CSharp {
                static public string Describe (object x)
                {
                        if (x == null)
-                               return "";
-                       
-                       Type t = x as Type;
-                       if (t == null)
-                               t = x.GetType ();
+                               return "<null>";
+
+                       var type = x as Type ?? x.GetType ();
 
                        StringWriter sw = new StringWriter ();
-                       new Outline (t, sw, true, false, false).OutlineType ();
+                       new Outline (type, sw, true, false, false).OutlineType ();
                        return sw.ToString ();
                }
 #endif
@@ -1185,7 +1187,7 @@ namespace Mono.CSharp {
                                        }
                                        
                                        Arguments args = new Arguments (1);
-                                       args.Add (new Argument (new TypeOf (source, Location)));
+                                       args.Add (new Argument (new TypeOf ((TypeExpr) clone, Location)));
                                        source = new Invocation (new SimpleName ("Describe", Location), args).Resolve (ec);
                                }
                                Evaluator.SetPrinter (old_printer);