X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mcs%2Fmcs%2Feval.cs;h=2c2f297266e895895e75f0b79ee8c3f56d4744c5;hb=732841a4f171062563a831fb8cd3165be1ddfd6e;hp=28ab1f96f43829e5fb73520b9394233ecd12b7fb;hpb=9a32bc55736b04b4ee6eb470a2e28fc028d77541;p=mono.git diff --git a/mcs/mcs/eval.cs b/mcs/mcs/eval.cs index 28ab1f96f43..2c2f297266e 100644 --- a/mcs/mcs/eval.cs +++ b/mcs/mcs/eval.cs @@ -11,7 +11,7 @@ // using System; using System.Threading; -using System.Collections; +using System.Collections.Generic; using System.Reflection; using System.Reflection.Emit; using System.IO; @@ -54,12 +54,12 @@ namespace Mono.CSharp { static string current_debug_name; static int count; static Thread invoke_thread; - - static ArrayList using_alias_list = new ArrayList (); - internal static ArrayList using_list = new ArrayList (); - static Hashtable fields = new Hashtable (); - static Type interactive_base_class = typeof (InteractiveBase); + static List using_alias_list = new List (); + internal static List using_list = new List (); + static Dictionary fields = new Dictionary (); + + static TypeSpec interactive_base_class; static Driver driver; static bool inited; @@ -85,6 +85,10 @@ namespace Mono.CSharp { InitAndGetStartupFiles (args); } + internal static ReportPrinter SetPrinter (ReportPrinter report_printer) + { + return ctx.Report.SetPrinter (report_printer); + } /// /// Optional initialization for the Evaluator. @@ -113,22 +117,27 @@ namespace Mono.CSharp { if (driver == null) throw new Exception ("Failed to create compiler driver with the given arguments"); - RootContext.ToplevelTypes = new ModuleContainer (ctx, true); + RootContext.ToplevelTypes = new ModuleCompiled (ctx, true); driver.ProcessDefaultConfig (); - ArrayList startup_files = new ArrayList (); + var startup_files = new List (); foreach (CompilationUnit file in Location.SourceFiles) startup_files.Add (file.Path); CompilerCallableEntryPoint.Reset (); - RootContext.ToplevelTypes = new ModuleContainer (ctx, true); + RootContext.ToplevelTypes = new ModuleCompiled (ctx, true); + /*var ctypes = */TypeManager.InitCoreTypes (); + TypeManager.InitExpressionTypes (); + Import.Initialize (); driver.LoadReferences (); + TypeManager.InitOptionalCoreTypes (ctx); + RootContext.EvalMode = true; inited = true; - return (string []) startup_files.ToArray (typeof (string)); + return startup_files.ToArray (); } } @@ -140,21 +149,26 @@ namespace Mono.CSharp { static void Reset () { CompilerCallableEntryPoint.PartialReset (); + RootContext.PartialReset (); // Workaround for API limitation where full message printer cannot be passed - ReportPrinter printer = MessageOutput == Console.Out || MessageOutput == Console.Error ? - new ConsoleReportPrinter (MessageOutput) : - new StreamReportPrinter (MessageOutput); + ReportPrinter printer; + if (MessageOutput == Console.Out || MessageOutput == Console.Error){ + var console_reporter = new ConsoleReportPrinter (MessageOutput); + console_reporter.Fatal = driver.fatal_errors; + printer = console_reporter; + } else + printer = new StreamReportPrinter (MessageOutput); ctx = new CompilerContext (new Report (printer)); - RootContext.ToplevelTypes = new ModuleContainer (ctx, true); + RootContext.ToplevelTypes = new ModuleCompiled (ctx, true); // // 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 (); @@ -180,18 +194,22 @@ namespace Mono.CSharp { /// base class and the static members that are /// available to your evaluated code. /// - 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); } /// @@ -305,7 +323,7 @@ namespace Mono.CSharp { // Either null (on error) or the compiled method. return compiled; } - + // // Todo: Should we handle errors, or expect the calling code to setup // the recording themselves? @@ -351,7 +369,7 @@ namespace Mono.CSharp { // The code execution does not need to keep the compiler lock // object retval = typeof (NoValueSet); - + try { invoke_thread = System.Threading.Thread.CurrentThread; invoking = true; @@ -428,7 +446,7 @@ namespace Mono.CSharp { } catch (CompletionResult cr){ prefix = cr.BaseText; return cr.Result; - } + } } finally { parser.undo.ExecuteUndo (); } @@ -610,6 +628,7 @@ namespace Mono.CSharp { partial_input = false; Reset (); queued_fields.Clear (); + Tokenizer.LocatedToken.Initialize (); Stream s = new MemoryStream (Encoding.Default.GetBytes (input)); SeekableStreamReader seekable = new SeekableStreamReader (s, Encoding.Default); @@ -650,14 +669,10 @@ namespace Mono.CSharp { if (mode == ParseMode.GetCompletions) parser.Lexer.CompleteOnEOF = true; - bool disable_error_reporting; + ReportPrinter old_printer = null; if ((mode == ParseMode.Silent || mode == ParseMode.GetCompletions) && CSharpParser.yacc_verbose_flag == 0) - disable_error_reporting = true; - else - disable_error_reporting = false; - - if (disable_error_reporting) - ctx.Report.DisableReporting (); + old_printer = SetPrinter (new StreamReportPrinter (TextWriter.Null)); + try { parser.parse (); } finally { @@ -669,8 +684,8 @@ namespace Mono.CSharp { parser = null; } - if (disable_error_reporting) - ctx.Report.EnableReporting (); + if (old_printer != null) + SetPrinter (old_printer); } return parser; } @@ -680,7 +695,7 @@ namespace Mono.CSharp { // or reflection gets confused (it basically gets confused, and variables override each // other). // - static ArrayList queued_fields = new ArrayList (); + static List queued_fields = new List (); //static ArrayList types = new ArrayList (); @@ -721,8 +736,10 @@ namespace Mono.CSharp { } RootContext.EmitCode (); - if (Report.Errors != 0) + if (Report.Errors != 0){ + undo.ExecuteUndo (); return null; + } RootContext.CloseTypes (); @@ -736,19 +753,19 @@ 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 = (FieldInfo) fields [field.Name]; + FieldInfo old; // If a previous value was set, nullify it, so that we do // not leak memory - if (old != null){ - if (TypeManager.IsStruct (old.FieldType)){ + if (fields.TryGetValue (field.Name, out old)){ + if (old.FieldType.IsValueType){ // // TODO: Clear fields for structs // @@ -785,7 +802,9 @@ namespace Mono.CSharp { static internal FieldInfo LookupField (string name) { - FieldInfo fi = (FieldInfo) fields [name]; + FieldInfo fi; + if (!fields.TryGetValue (name, out fi)) + return null; return fi; } @@ -829,9 +848,9 @@ namespace Mono.CSharp { } } - static internal ICollection GetUsingList () + static internal ICollection GetUsingList () { - ArrayList res = new ArrayList (using_list.Count); + var res = new List (using_list.Count); foreach (object ue in using_list) res.Add (ue.ToString ()); return res; @@ -840,7 +859,7 @@ namespace Mono.CSharp { static internal string [] GetVarNames () { lock (evaluator_lock){ - return (string []) new ArrayList (fields.Keys).ToArray (typeof (string)); + return new List (fields.Keys).ToArray (); } } @@ -849,10 +868,10 @@ namespace Mono.CSharp { lock (evaluator_lock){ StringBuilder sb = new StringBuilder (); - foreach (DictionaryEntry de in fields){ - FieldInfo fi = LookupField ((string) de.Key); + foreach (var de in fields){ + FieldInfo fi = LookupField (de.Key); object value = null; - bool error = false; + //bool error = false; try { if (value == null) @@ -861,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} ", 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} ", TypeManager.CSharpName(fi.FieldType), de.Key)); + //else + // sb.Append (String.Format ("{0} {1} = {2}", TypeManager.CSharpName(fi.FieldType), de.Key, value)); } return sb.ToString (); @@ -891,11 +911,17 @@ 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); } } - + + /// + /// If true, turns type expressions into valid expressions + /// and calls the describe method on it + /// + public static bool DescribeTypeExpressions; } @@ -1072,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 @@ -1148,15 +1174,39 @@ namespace Mono.CSharp { { } - public override Expression DoResolve (ResolveContext ec) + protected override Expression DoResolve (ResolveContext ec) { CloneContext cc = new CloneContext (); Expression clone = source.Clone (cc); - clone = clone.Resolve (ec); - if (clone == null) - return null; - + // + // 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){ + Evaluator.SetPrinter (old_printer); + clone = source.Clone (cc); + 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); + } + Evaluator.SetPrinter (old_printer); + } else { + clone = clone.Resolve (ec); + if (clone == null) + return null; + } + // This means its really a statement. if (clone.Type == TypeManager.void_type){ source = source.Resolve (ec); @@ -1187,11 +1237,11 @@ namespace Mono.CSharp { } public class Undo { - ArrayList undo_types; + List> undo_types; public Undo () { - undo_types = new ArrayList (); + undo_types = new List> (); } public void AddTypeContainer (TypeContainer current_container, TypeContainer tc) @@ -1200,10 +1250,11 @@ namespace Mono.CSharp { Console.Error.WriteLine ("Internal error: inserting container into itself"); return; } - + if (undo_types == null) - undo_types = new ArrayList (); - undo_types.Add (new Pair (current_container, tc)); + undo_types = new List> (); + + undo_types.Add (new KeyValuePair (current_container, tc)); } public void ExecuteUndo () @@ -1211,10 +1262,10 @@ namespace Mono.CSharp { if (undo_types == null) return; - foreach (Pair p in undo_types){ - TypeContainer current_container = (TypeContainer) p.First; + foreach (var p in undo_types){ + TypeContainer current_container = p.Key; - current_container.RemoveTypeContainer ((TypeContainer) p.Second); + current_container.RemoveTypeContainer (p.Value); } undo_types = null; }