Merge branch 'master' of github.com:mono/mono
[mono.git] / mcs / mcs / eval.cs
1 //
2 // eval.cs: Evaluation and Hosting API for the C# compiler
3 //
4 // Authors:
5 //   Miguel de Icaza (miguel@gnome.org)
6 //
7 // Dual licensed under the terms of the MIT X11 or GNU GPL
8 //
9 // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
10 // Copyright 2004, 2005, 2006, 2007, 2008 Novell, Inc
11 //
12 using System;
13 using System.Threading;
14 using System.Collections.Generic;
15 using System.Reflection;
16 using System.Reflection.Emit;
17 using System.IO;
18 using System.Globalization;
19 using System.Text;
20
21 namespace Mono.CSharp {
22
23         /// <summary>
24         ///   Evaluator: provides an API to evaluate C# statements and
25         ///   expressions dynamically.
26         /// </summary>
27         /// <remarks>
28         ///   This class exposes static methods to evaluate expressions in the
29         ///   current program.
30         ///
31         ///   To initialize the evaluator with a number of compiler
32         ///   options call the Init(string[]args) method with a set of
33         ///   command line options that the compiler recognizes.
34         ///
35         ///   To interrupt execution of a statement, you can invoke the
36         ///   Evaluator.Interrupt method.
37         /// </remarks>
38         public class Evaluator {
39
40                 enum ParseMode {
41                         // Parse silently, do not output any error messages
42                         Silent,
43
44                         // Report errors during parse
45                         ReportErrors,
46
47                         // Auto-complete, means that the tokenizer will start producing
48                         // GETCOMPLETIONS tokens when it reaches a certain point.
49                         GetCompletions
50                 }
51
52                 static object evaluator_lock = new object ();
53                 
54                 static string current_debug_name;
55                 static int count;
56                 static Thread invoke_thread;
57
58                 static List<NamespaceEntry.UsingAliasEntry> using_alias_list = new List<NamespaceEntry.UsingAliasEntry> ();
59                 internal static List<NamespaceEntry.UsingEntry> using_list = new List<NamespaceEntry.UsingEntry> ();
60                 static Dictionary<string, Tuple<FieldSpec, FieldInfo>> fields = new Dictionary<string, Tuple<FieldSpec, FieldInfo>> ();
61
62                 static TypeSpec interactive_base_class;
63                 static Driver driver;
64                 static bool inited;
65
66                 static CompilerContext ctx;
67                 
68                 public static TextWriter MessageOutput = Console.Out;
69
70                 /// <summary>
71                 ///   Optional initialization for the Evaluator.
72                 /// </summary>
73                 /// <remarks>
74                 ///  Initializes the Evaluator with the command line options
75                 ///  that would be processed by the command line compiler.  Only
76                 ///  the first call to Init will work, any future invocations are
77                 ///  ignored.
78                 ///
79                 ///  You can safely avoid calling this method if your application
80                 ///  does not need any of the features exposed by the command line
81                 ///  interface.
82                 /// </remarks>
83                 public static void Init (string [] args)
84                 {
85                         InitAndGetStartupFiles (args);
86                 }
87
88                 internal static ReportPrinter SetPrinter (ReportPrinter report_printer)
89                 {
90                         return ctx.Report.SetPrinter (report_printer);
91                 }                               
92
93                 /// <summary>
94                 ///   Optional initialization for the Evaluator.
95                 /// </summary>
96                 /// <remarks>
97                 ///  Initializes the Evaluator with the command line
98                 ///  options that would be processed by the command
99                 ///  line compiler.  Only the first call to
100                 ///  InitAndGetStartupFiles or Init will work, any future
101                 ///  invocations are ignored.
102                 ///
103                 ///  You can safely avoid calling this method if your application
104                 ///  does not need any of the features exposed by the command line
105                 ///  interface.
106                 ///
107                 ///  This method return an array of strings that contains any
108                 ///  files that were specified in `args'.
109                 /// </remarks>
110                 public static string [] InitAndGetStartupFiles (string [] args)
111                 {
112                         lock (evaluator_lock){
113                                 if (inited)
114                                         return new string [0];
115                                 
116                                 driver = Driver.Create (args, false, new ConsoleReportPrinter ());
117                                 if (driver == null)
118                                         throw new Exception ("Failed to create compiler driver with the given arguments");
119
120                                 ctx = driver.ctx;
121
122                                 RootContext.ToplevelTypes = new ModuleCompiled (ctx, true);
123                                 
124                                 driver.ProcessDefaultConfig ();
125
126                                 var startup_files = new List<string> ();
127                                 foreach (CompilationUnit file in Location.SourceFiles)
128                                         startup_files.Add (file.Path);
129                                 
130                                 CompilerCallableEntryPoint.Reset ();
131                                 RootContext.ToplevelTypes = new ModuleCompiled (ctx, true);
132                                 var ctypes = TypeManager.InitCoreTypes ();
133
134                                 ctx.MetaImporter.Initialize ();
135                                 driver.LoadReferences ();
136                                 TypeManager.InitCoreTypes (ctx, ctypes);
137                                 TypeManager.InitOptionalCoreTypes (ctx);
138
139                                 RootContext.EvalMode = true;
140                                 inited = true;
141
142                                 return startup_files.ToArray ();
143                         }
144                 }
145
146                 static void Init ()
147                 {
148                         Init (new string [0]);
149                 }
150                 
151                 static void Reset ()
152                 {
153                         CompilerCallableEntryPoint.PartialReset ();
154                         RootContext.PartialReset ();
155                         
156                         RootContext.ToplevelTypes = new ModuleCompiled (ctx, true);
157
158                         Location.AddFile (null, "{interactive}");
159                         Location.Initialize ();
160
161                         current_debug_name = "interactive" + (count++) + ".dll";
162                         if (Environment.GetEnvironmentVariable ("SAVE") != null){
163                                 CodeGen.Init (current_debug_name, current_debug_name, false, ctx);
164                         } else
165                                 CodeGen.InitDynamic (ctx, current_debug_name);
166                 }
167
168                 /// <summary>
169                 ///   The base class for the classes that host the user generated code
170                 /// </summary>
171                 /// <remarks>
172                 ///
173                 ///   This is the base class that will host the code
174                 ///   executed by the Evaluator.  By default
175                 ///   this is the Mono.CSharp.InteractiveBase class
176                 ///   which is useful for interactive use.
177                 ///
178                 ///   By changing this property you can control the
179                 ///   base class and the static members that are
180                 ///   available to your evaluated code.
181                 /// </remarks>
182                 static public TypeSpec InteractiveBaseClass {
183                         get {
184                                 if (interactive_base_class != null)
185                                         return interactive_base_class;
186
187                                 return ctx.MetaImporter.ImportType (typeof (InteractiveBase));
188                         }
189                 }
190
191                 public static void SetInteractiveBaseClass (Type type)
192                 {
193                         if (type == null)
194                                 throw new ArgumentNullException ();
195
196                         lock (evaluator_lock)
197                                 interactive_base_class = ctx.MetaImporter.ImportType (type);
198                 }
199
200                 /// <summary>
201                 ///   Interrupts the evaluation of an expression executing in Evaluate.
202                 /// </summary>
203                 /// <remarks>
204                 ///   Use this method to interrupt long-running invocations.
205                 /// </remarks>
206                 public static void Interrupt ()
207                 {
208                         if (!inited || !invoking)
209                                 return;
210                         
211                         if (invoke_thread != null)
212                                 invoke_thread.Abort ();
213                 }
214
215                 /// <summary>
216                 ///   Compiles the input string and returns a delegate that represents the compiled code.
217                 /// </summary>
218                 /// <remarks>
219                 ///
220                 ///   Compiles the input string as a C# expression or
221                 ///   statement, unlike the Evaluate method, the
222                 ///   resulting delegate can be invoked multiple times
223                 ///   without incurring in the compilation overhead.
224                 ///
225                 ///   If the return value of this function is null,
226                 ///   this indicates that the parsing was complete.
227                 ///   If the return value is a string it indicates
228                 ///   that the input string was partial and that the
229                 ///   invoking code should provide more code before
230                 ///   the code can be successfully compiled.
231                 ///
232                 ///   If you know that you will always get full expressions or
233                 ///   statements and do not care about partial input, you can use
234                 ///   the other Compile overload. 
235                 ///
236                 ///   On success, in addition to returning null, the
237                 ///   compiled parameter will be set to the delegate
238                 ///   that can be invoked to execute the code.
239                 ///
240                 /// </remarks>
241                 static public string Compile (string input, out CompiledMethod compiled)
242                 {
243                         if (input == null || input.Length == 0){
244                                 compiled = null;
245                                 return null;
246                         }
247                         
248                         lock (evaluator_lock){
249                                 if (!inited)
250                                         Init ();
251                                 
252                                 bool partial_input;
253                                 CSharpParser parser = ParseString (ParseMode.Silent, input, out partial_input);
254                                 if (parser == null){
255                                         compiled = null;
256                                         if (partial_input)
257                                                 return input;
258                                         
259                                         ParseString (ParseMode.ReportErrors, input, out partial_input);
260                                         return null;
261                                 }
262                                 
263                                 object parser_result = parser.InteractiveResult;
264                                 
265                                 if (!(parser_result is Class)){
266                                         int errors = ctx.Report.Errors;
267                                         
268                                         NamespaceEntry.VerifyAllUsing ();
269                                         if (errors == ctx.Report.Errors)
270                                                 parser.CurrentNamespace.Extract (using_alias_list, using_list);
271                                 }
272
273                                 compiled = CompileBlock (parser_result as Class, parser.undo, ctx.Report);
274                         }
275                         
276                         return null;
277                 }
278
279                 /// <summary>
280                 ///   Compiles the input string and returns a delegate that represents the compiled code.
281                 /// </summary>
282                 /// <remarks>
283                 ///
284                 ///   Compiles the input string as a C# expression or
285                 ///   statement, unlike the Evaluate method, the
286                 ///   resulting delegate can be invoked multiple times
287                 ///   without incurring in the compilation overhead.
288                 ///
289                 ///   This method can only deal with fully formed input
290                 ///   strings and does not provide a completion mechanism.
291                 ///   If you must deal with partial input (for example for
292                 ///   interactive use) use the other overload. 
293                 ///
294                 ///   On success, a delegate is returned that can be used
295                 ///   to invoke the method.
296                 ///
297                 /// </remarks>
298                 static public CompiledMethod Compile (string input)
299                 {
300                         CompiledMethod compiled;
301
302                         // Ignore partial inputs
303                         if (Compile (input, out compiled) != null){
304                                 // Error, the input was partial.
305                                 return null;
306                         }
307
308                         // Either null (on error) or the compiled method.
309                         return compiled;
310                 }
311
312                 //
313                 // Todo: Should we handle errors, or expect the calling code to setup
314                 // the recording themselves?
315                 //
316
317                 /// <summary>
318                 ///   Evaluates and expression or statement and returns any result values.
319                 /// </summary>
320                 /// <remarks>
321                 ///   Evaluates the input string as a C# expression or
322                 ///   statement.  If the input string is an expression
323                 ///   the result will be stored in the result variable
324                 ///   and the result_set variable will be set to true.
325                 ///
326                 ///   It is necessary to use the result/result_set
327                 ///   pair to identify when a result was set (for
328                 ///   example, execution of user-provided input can be
329                 ///   an expression, a statement or others, and
330                 ///   result_set would only be set if the input was an
331                 ///   expression.
332                 ///
333                 ///   If the return value of this function is null,
334                 ///   this indicates that the parsing was complete.
335                 ///   If the return value is a string, it indicates
336                 ///   that the input is partial and that the user
337                 ///   should provide an updated string.
338                 /// </remarks>
339                 public static string Evaluate (string input, out object result, out bool result_set)
340                 {
341                         CompiledMethod compiled;
342
343                         result_set = false;
344                         result = null;
345
346                         input = Compile (input, out compiled);
347                         if (input != null)
348                                 return input;
349                         
350                         if (compiled == null)
351                                 return null;
352                                 
353                         //
354                         // The code execution does not need to keep the compiler lock
355                         //
356                         object retval = typeof (NoValueSet);
357
358                         try {
359                                 invoke_thread = System.Threading.Thread.CurrentThread;
360                                 invoking = true;
361                                 compiled (ref retval);
362                         } catch (ThreadAbortException e){
363                                 Thread.ResetAbort ();
364                                 Console.WriteLine ("Interrupted!\n{0}", e);
365                         } finally {
366                                 invoking = false;
367                         }
368
369                         //
370                         // We use a reference to a compiler type, in this case
371                         // Driver as a flag to indicate that this was a statement
372                         //
373                         if (retval != typeof (NoValueSet)){
374                                 result_set = true;
375                                 result = retval; 
376                         }
377
378                         return null;
379                 }
380
381                 public static string [] GetCompletions (string input, out string prefix)
382                 {
383                         prefix = "";
384                         if (input == null || input.Length == 0)
385                                 return null;
386                         
387                         lock (evaluator_lock){
388                                 if (!inited)
389                                         Init ();
390                                 
391                                 bool partial_input;
392                                 CSharpParser parser = ParseString (ParseMode.GetCompletions, input, out partial_input);
393                                 if (parser == null){
394                                         if (CSharpParser.yacc_verbose_flag != 0)
395                                                 Console.WriteLine ("DEBUG: No completions available");
396                                         return null;
397                                 }
398                                 
399                                 Class parser_result = parser.InteractiveResult as Class;
400                                 
401                                 if (parser_result == null){
402                                         if (CSharpParser.yacc_verbose_flag != 0)
403                                                 Console.WriteLine ("Do not know how to cope with !Class yet");
404                                         return null;
405                                 }
406
407                                 try {
408                                         RootContext.ResolveTree ();
409                                         if (ctx.Report.Errors != 0)
410                                                 return null;
411                                         
412                                         RootContext.PopulateTypes ();
413                                         if (ctx.Report.Errors != 0)
414                                                 return null;
415
416                                         MethodOrOperator method = null;
417                                         foreach (MemberCore member in parser_result.Methods){
418                                                 if (member.Name != "Host")
419                                                         continue;
420                                                 
421                                                 method = (MethodOrOperator) member;
422                                                 break;
423                                         }
424                                         if (method == null)
425                                                 throw new InternalErrorException ("did not find the the Host method");
426
427                                         BlockContext bc = new BlockContext (method, method.Block, method.ReturnType);
428
429                                         try {
430                                                 method.Block.Resolve (null, bc, method);
431                                         } catch (CompletionResult cr){
432                                                 prefix = cr.BaseText;
433                                                 return cr.Result;
434                                         } 
435                                 } finally {
436                                         parser.undo.ExecuteUndo ();
437                                 }
438                                 
439                         }
440                         return null;
441                 }
442                 
443                 /// <summary>
444                 ///   Executes the given expression or statement.
445                 /// </summary>
446                 /// <remarks>
447                 ///    Executes the provided statement, returns true
448                 ///    on success, false on parsing errors.  Exceptions
449                 ///    might be thrown by the called code.
450                 /// </remarks>
451                 public static bool Run (string statement)
452                 {
453                         if (!inited)
454                                 Init ();
455
456                         object result;
457                         bool result_set;
458
459                         bool ok = Evaluate (statement, out result, out result_set) == null;
460                         
461                         return ok;
462                 }
463
464                 /// <summary>
465                 ///   Evaluates and expression or statement and returns the result.
466                 /// </summary>
467                 /// <remarks>
468                 ///   Evaluates the input string as a C# expression or
469                 ///   statement and returns the value.   
470                 ///
471                 ///   This method will throw an exception if there is a syntax error,
472                 ///   of if the provided input is not an expression but a statement.
473                 /// </remarks>
474                 public static object Evaluate (string input)
475                 {
476                         object result;
477                         bool result_set;
478                         
479                         string r = Evaluate (input, out result, out result_set);
480
481                         if (r != null)
482                                 throw new ArgumentException ("Syntax error on input: partial input");
483                         
484                         if (result_set == false)
485                                 throw new ArgumentException ("The expression did not set a result");
486
487                         return result;
488                 }
489         
490                 enum InputKind {
491                         EOF,
492                         StatementOrExpression,
493                         CompilationUnit,
494                         Error
495                 }
496
497                 //
498                 // Deambiguates the input string to determine if we
499                 // want to process a statement or if we want to
500                 // process a compilation unit.
501                 //
502                 // This is done using a top-down predictive parser,
503                 // since the yacc/jay parser can not deambiguage this
504                 // without more than one lookahead token.   There are very
505                 // few ambiguities.
506                 //
507                 static InputKind ToplevelOrStatement (SeekableStreamReader seekable)
508                 {
509                         Tokenizer tokenizer = new Tokenizer (seekable, (CompilationUnit) Location.SourceFiles [0], ctx);
510                         
511                         int t = tokenizer.token ();
512                         switch (t){
513                         case Token.EOF:
514                                 return InputKind.EOF;
515                                 
516                         // These are toplevels
517                         case Token.EXTERN:
518                         case Token.OPEN_BRACKET:
519                         case Token.ABSTRACT:
520                         case Token.CLASS:
521                         case Token.ENUM:
522                         case Token.INTERFACE:
523                         case Token.INTERNAL:
524                         case Token.NAMESPACE:
525                         case Token.PRIVATE:
526                         case Token.PROTECTED:
527                         case Token.PUBLIC:
528                         case Token.SEALED:
529                         case Token.STATIC:
530                         case Token.STRUCT:
531                                 return InputKind.CompilationUnit;
532                                 
533                         // Definitely expression
534                         case Token.FIXED:
535                         case Token.BOOL:
536                         case Token.BYTE:
537                         case Token.CHAR:
538                         case Token.DECIMAL:
539                         case Token.DOUBLE:
540                         case Token.FLOAT:
541                         case Token.INT:
542                         case Token.LONG:
543                         case Token.NEW:
544                         case Token.OBJECT:
545                         case Token.SBYTE:
546                         case Token.SHORT:
547                         case Token.STRING:
548                         case Token.UINT:
549                         case Token.ULONG:
550                                 return InputKind.StatementOrExpression;
551
552                         // These need deambiguation help
553                         case Token.USING:
554                                 t = tokenizer.token ();
555                                 if (t == Token.EOF)
556                                         return InputKind.EOF;
557
558                                 if (t == Token.IDENTIFIER)
559                                         return InputKind.CompilationUnit;
560                                 return InputKind.StatementOrExpression;
561
562
563                         // Distinguish between:
564                         //    delegate opt_anonymous_method_signature block
565                         //    delegate type 
566                         case Token.DELEGATE:
567                                 t = tokenizer.token ();
568                                 if (t == Token.EOF)
569                                         return InputKind.EOF;
570                                 if (t == Token.OPEN_PARENS || t == Token.OPEN_BRACE)
571                                         return InputKind.StatementOrExpression;
572                                 return InputKind.CompilationUnit;
573
574                         // Distinguih between:
575                         //    unsafe block
576                         //    unsafe as modifier of a type declaration
577                         case Token.UNSAFE:
578                                 t = tokenizer.token ();
579                                 if (t == Token.EOF)
580                                         return InputKind.EOF;
581                                 if (t == Token.OPEN_PARENS)
582                                         return InputKind.StatementOrExpression;
583                                 return InputKind.CompilationUnit;
584                                 
585                         // These are errors: we list explicitly what we had
586                         // from the grammar, ERROR and then everything else
587
588                         case Token.READONLY:
589                         case Token.OVERRIDE:
590                         case Token.ERROR:
591                                 return InputKind.Error;
592
593                         // This catches everything else allowed by
594                         // expressions.  We could add one-by-one use cases
595                         // if needed.
596                         default:
597                                 return InputKind.StatementOrExpression;
598                         }
599                 }
600                 
601                 //
602                 // Parses the string @input and returns a CSharpParser if succeeful.
603                 //
604                 // if @silent is set to true then no errors are
605                 // reported to the user.  This is used to do various calls to the
606                 // parser and check if the expression is parsable.
607                 //
608                 // @partial_input: if @silent is true, then it returns whether the
609                 // parsed expression was partial, and more data is needed
610                 //
611                 static CSharpParser ParseString (ParseMode mode, string input, out bool partial_input)
612                 {
613                         partial_input = false;
614                         Reset ();
615                         queued_fields.Clear ();
616                         Tokenizer.LocatedToken.Initialize ();
617
618                         Stream s = new MemoryStream (Encoding.Default.GetBytes (input));
619                         SeekableStreamReader seekable = new SeekableStreamReader (s, Encoding.Default);
620
621                         InputKind kind = ToplevelOrStatement (seekable);
622                         if (kind == InputKind.Error){
623                                 if (mode == ParseMode.ReportErrors)
624                                         ctx.Report.Error (-25, "Detection Parsing Error");
625                                 partial_input = false;
626                                 return null;
627                         }
628
629                         if (kind == InputKind.EOF){
630                                 if (mode == ParseMode.ReportErrors)
631                                         Console.Error.WriteLine ("Internal error: EOF condition should have been detected in a previous call with silent=true");
632                                 partial_input = true;
633                                 return null;
634                                 
635                         }
636                         seekable.Position = 0;
637
638                         CSharpParser parser = new CSharpParser (seekable, (CompilationUnit) Location.SourceFiles [0], ctx);
639
640                         if (kind == InputKind.StatementOrExpression){
641                                 parser.Lexer.putback_char = Tokenizer.EvalStatementParserCharacter;
642                                 RootContext.StatementMode = true;
643                         } else {
644                                 //
645                                 // Do not activate EvalCompilationUnitParserCharacter until
646                                 // I have figured out all the limitations to invoke methods
647                                 // in the generated classes.  See repl.txt
648                                 //
649                                 parser.Lexer.putback_char = Tokenizer.EvalUsingDeclarationsParserCharacter;
650                                 //parser.Lexer.putback_char = Tokenizer.EvalCompilationUnitParserCharacter;
651                                 RootContext.StatementMode = false;
652                         }
653
654                         if (mode == ParseMode.GetCompletions)
655                                 parser.Lexer.CompleteOnEOF = true;
656
657                         ReportPrinter old_printer = null;
658                         if ((mode == ParseMode.Silent || mode == ParseMode.GetCompletions) && CSharpParser.yacc_verbose_flag == 0)
659                                 old_printer = SetPrinter (new StreamReportPrinter (TextWriter.Null));
660
661                         try {
662                                 parser.parse ();
663                         } finally {
664                                 if (ctx.Report.Errors != 0){
665                                         if (mode != ParseMode.ReportErrors  && parser.UnexpectedEOF)
666                                                 partial_input = true;
667
668                                         parser.undo.ExecuteUndo ();
669                                         parser = null;
670                                 }
671
672                                 if (old_printer != null)
673                                         SetPrinter (old_printer);
674                         }
675                         return parser;
676                 }
677
678                 //
679                 // Queue all the fields that we use, as we need to then go from FieldBuilder to FieldInfo
680                 // or reflection gets confused (it basically gets confused, and variables override each
681                 // other).
682                 //
683                 static List<Field> queued_fields = new List<Field> ();
684                 
685                 //static ArrayList types = new ArrayList ();
686
687                 static volatile bool invoking;
688                 
689                 static CompiledMethod CompileBlock (Class host, Undo undo, Report Report)
690                 {
691                         RootContext.ResolveTree ();
692                         if (Report.Errors != 0){
693                                 undo.ExecuteUndo ();
694                                 return null;
695                         }
696                         
697                         RootContext.PopulateTypes ();
698
699                         if (Report.Errors != 0){
700                                 undo.ExecuteUndo ();
701                                 return null;
702                         }
703
704                         TypeBuilder tb = null;
705                         MethodBuilder mb = null;
706                                 
707                         if (host != null){
708                                 tb = host.TypeBuilder;
709                                 mb = null;
710                                 foreach (MemberCore member in host.Methods){
711                                         if (member.Name != "Host")
712                                                 continue;
713                                         
714                                         MethodOrOperator method = (MethodOrOperator) member;
715                                         mb = method.MethodBuilder;
716                                         break;
717                                 }
718
719                                 if (mb == null)
720                                         throw new Exception ("Internal error: did not find the method builder for the generated method");
721                         }
722                         
723                         RootContext.EmitCode ();
724                         if (Report.Errors != 0){
725                                 undo.ExecuteUndo ();
726                                 return null;
727                         }
728                         
729                         RootContext.CloseTypes (ctx);
730
731                         if (Environment.GetEnvironmentVariable ("SAVE") != null)
732                                 CodeGen.Save (current_debug_name, false, Report);
733
734                         if (host == null)
735                                 return null;
736                         
737                         //
738                         // Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
739                         // work from MethodBuilders.   Retarded, I know.
740                         //
741                         var tt = CodeGen.Assembly.Builder.GetType (tb.Name);
742                         MethodInfo mi = tt.GetMethod (mb.Name);
743                         
744                         // Pull the FieldInfos from the type, and keep track of them
745                         foreach (Field field in queued_fields){
746                                 FieldInfo fi = tt.GetField (field.Name);
747
748                                 Tuple<FieldSpec, FieldInfo> old;
749                                 
750                                 // If a previous value was set, nullify it, so that we do
751                                 // not leak memory
752                                 if (fields.TryGetValue (field.Name, out old)) {
753                                         if (old.Item1.MemberType.IsStruct) {
754                                                 //
755                                                 // TODO: Clear fields for structs
756                                                 //
757                                         } else {
758                                                 try {
759                                                         old.Item2.SetValue (null, null);
760                                                 } catch {
761                                                 }
762                                         }
763
764                                         fields [field.Name] = Tuple.Create (old.Item1, fi);
765                                 } else {
766                                         fields.Add (field.Name, Tuple.Create (field.Spec, fi));
767                                 }
768                         }
769                         //types.Add (tb);
770
771                         queued_fields.Clear ();
772                         
773                         return (CompiledMethod) System.Delegate.CreateDelegate (typeof (CompiledMethod), mi);
774                 }
775                 
776                 static internal void LoadAliases (NamespaceEntry ns)
777                 {
778                         ns.Populate (using_alias_list, using_list);
779                 }
780                 
781                 /// <summary>
782                 ///   A sentinel value used to indicate that no value was
783                 ///   was set by the compiled function.   This is used to
784                 ///   differentiate between a function not returning a
785                 ///   value and null.
786                 /// </summary>
787                 public class NoValueSet {
788                 }
789
790                 static internal Tuple<FieldSpec, FieldInfo> LookupField (string name)
791                 {
792                         Tuple<FieldSpec, FieldInfo> fi;
793                         fields.TryGetValue (name, out fi);
794                         return fi;
795                 }
796
797                 //
798                 // Puts the FieldBuilder into a queue of names that will be
799                 // registered.   We can not register FieldBuilders directly
800                 // we need to fetch the FieldInfo after Reflection cooks the
801                 // types, or bad things happen (bad means: FieldBuilders behave
802                 // incorrectly across multiple assemblies, causing assignments to
803                 // invalid areas
804                 //
805                 // This also serves for the parser to register Field classes
806                 // that should be exposed as global variables
807                 //
808                 static internal void QueueField (Field f)
809                 {
810                         queued_fields.Add (f);
811                 }
812
813                 static string Quote (string s)
814                 {
815                         if (s.IndexOf ('"') != -1)
816                                 s = s.Replace ("\"", "\\\"");
817                         
818                         return "\"" + s + "\"";
819                 }
820
821                 static public string GetUsing ()
822                 {
823                         lock (evaluator_lock){
824                                 StringBuilder sb = new StringBuilder ();
825                                 
826                                 foreach (object x in using_alias_list)
827                                         sb.Append (String.Format ("using {0};\n", x));
828                                 
829                                 foreach (object x in using_list)
830                                         sb.Append (String.Format ("using {0};\n", x));
831                                 
832                                 return sb.ToString ();
833                         }
834                 }
835
836                 static internal ICollection<string> GetUsingList ()
837                 {
838                         var res = new List<string> (using_list.Count);
839                         foreach (object ue in using_list)
840                                 res.Add (ue.ToString ());
841                         return res;
842                 }
843                 
844                 static internal string [] GetVarNames ()
845                 {
846                         lock (evaluator_lock){
847                                 return new List<string> (fields.Keys).ToArray ();
848                         }
849                 }
850                 
851                 static public string GetVars ()
852                 {
853                         lock (evaluator_lock){
854                                 StringBuilder sb = new StringBuilder ();
855                                 
856                                 foreach (var de in fields){
857                                         var fi = LookupField (de.Key);
858                                         object value;
859                                         try {
860                                                 value = fi.Item2.GetValue (null);
861                                                 if (value is string)
862                                                         value = Quote ((string)value);
863                                         } catch {
864                                                 value = "<error reading value>";
865                                         }
866
867                                         sb.AppendFormat ("{0} {1} = {2}", fi.Item1.MemberType.GetSignatureForError (), de.Key, value);
868                                         sb.AppendLine ();
869                                 }
870                                 
871                                 return sb.ToString ();
872                         }
873                 }
874
875                 /// <summary>
876                 ///    Loads the given assembly and exposes the API to the user.
877                 /// </summary>
878                 static public void LoadAssembly (string file)
879                 {
880                         lock (evaluator_lock){
881                                 driver.LoadAssembly (file, false);
882                                 ctx.GlobalRootNamespace.ComputeNamespaces (ctx);
883                         }
884                 }
885
886                 /// <summary>
887                 ///    Exposes the API of the given assembly to the Evaluator
888                 /// </summary>
889                 static public void ReferenceAssembly (Assembly a)
890                 {
891                         lock (evaluator_lock){
892 //                              GlobalRootNamespace.Instance.AddAssemblyReference (a);
893 //                              GlobalRootNamespace.Instance.ComputeNamespaces (ctx);
894                                 ctx.MetaImporter.ImportAssembly (a, ctx.GlobalRootNamespace);
895                         }
896                 }
897
898                 /// <summary>
899                 ///   If true, turns type expressions into valid expressions
900                 ///   and calls the describe method on it
901                 /// </summary>
902                 public static bool DescribeTypeExpressions;
903         }
904
905         
906         /// <summary>
907         ///   A delegate that can be used to invoke the
908         ///   compiled expression or statement.
909         /// </summary>
910         /// <remarks>
911         ///   Since the Compile methods will compile
912         ///   statements and expressions into the same
913         ///   delegate, you can tell if a value was returned
914         ///   by checking whether the returned value is of type
915         ///   NoValueSet.   
916         /// </remarks>
917         
918         public delegate void CompiledMethod (ref object retvalue);
919
920         /// <summary>
921         ///   The default base class for every interaction line
922         /// </summary>
923         /// <remarks>
924         ///   The expressions and statements behave as if they were
925         ///   a static method of this class.   The InteractiveBase class
926         ///   contains a number of useful methods, but can be overwritten
927         ///   by setting the InteractiveBaseType property in the Evaluator
928         /// </remarks>
929         public class InteractiveBase {
930                 /// <summary>
931                 ///   Determines where the standard output of methods in this class will go. 
932                 /// </summary>
933                 public static TextWriter Output = Console.Out;
934
935                 /// <summary>
936                 ///   Determines where the standard error of methods in this class will go. 
937                 /// </summary>
938                 public static TextWriter Error = Console.Error;
939
940                 /// <summary>
941                 ///   The primary prompt used for interactive use.
942                 /// </summary>
943                 public static string Prompt             = "csharp> ";
944
945                 /// <summary>
946                 ///   The secondary prompt used for interactive use (used when
947                 ///   an expression is incomplete).
948                 /// </summary>
949                 public static string ContinuationPrompt = "      > ";
950
951                 /// <summary>
952                 ///   Used to signal that the user has invoked the  `quit' statement.
953                 /// </summary>
954                 public static bool QuitRequested;
955                 
956                 /// <summary>
957                 ///   Shows all the variables defined so far.
958                 /// </summary>
959                 static public void ShowVars ()
960                 {
961                         Output.Write (Evaluator.GetVars ());
962                         Output.Flush ();
963                 }
964
965                 /// <summary>
966                 ///   Displays the using statements in effect at this point. 
967                 /// </summary>
968                 static public void ShowUsing ()
969                 {
970                         Output.Write (Evaluator.GetUsing ());
971                         Output.Flush ();
972                 }
973
974                 public delegate void Simple ();
975                 
976                 /// <summary>
977                 ///   Times the execution of the given delegate
978                 /// </summary>
979                 static public TimeSpan Time (Simple a)
980                 {
981                         DateTime start = DateTime.Now;
982                         a ();
983                         return DateTime.Now - start;
984                 }
985                 
986 #if !SMCS_SOURCE
987                 /// <summary>
988                 ///   Loads the assemblies from a package
989                 /// </summary>
990                 /// <remarks>
991                 ///   Loads the assemblies from a package.   This is equivalent
992                 ///   to passing the -pkg: command line flag to the C# compiler
993                 ///   on the command line. 
994                 /// </remarks>
995                 static public void LoadPackage (string pkg)
996                 {
997                         if (pkg == null){
998                                 Error.WriteLine ("Invalid package specified");
999                                 return;
1000                         }
1001
1002                         string pkgout = Driver.GetPackageFlags (pkg, false, RootContext.ToplevelTypes.Compiler.Report);
1003                         if (pkgout == null)
1004                                 return;
1005
1006                         string [] xargs = pkgout.Trim (new Char [] {' ', '\n', '\r', '\t'}).
1007                                 Split (new Char [] { ' ', '\t'});
1008
1009                         foreach (string s in xargs){
1010                                 if (s.StartsWith ("-r:") || s.StartsWith ("/r:") || s.StartsWith ("/reference:")){
1011                                         string lib = s.Substring (s.IndexOf (':')+1);
1012
1013                                         Evaluator.LoadAssembly (lib);
1014                                         continue;
1015                                 }
1016                         }
1017                 }
1018 #endif
1019
1020                 /// <summary>
1021                 ///   Loads the assembly
1022                 /// </summary>
1023                 /// <remarks>
1024                 ///   Loads the specified assembly and makes its types
1025                 ///   available to the evaluator.  This is equivalent
1026                 ///   to passing the -pkg: command line flag to the C#
1027                 ///   compiler on the command line.
1028                 /// </remarks>
1029                 static public void LoadAssembly (string assembly)
1030                 {
1031                         Evaluator.LoadAssembly (assembly);
1032                 }
1033                 
1034                 /// <summary>
1035                 ///   Returns a list of available static methods. 
1036                 /// </summary>
1037                 static public string help {
1038                         get {
1039                                 return "Static methods:\n" +
1040                                         "  Describe (object)       - Describes the object's type\n" +
1041                                         "  LoadPackage (package);  - Loads the given Package (like -pkg:FILE)\n" +
1042                                         "  LoadAssembly (assembly) - Loads the given assembly (like -r:ASSEMBLY)\n" +
1043                                         "  ShowVars ();            - Shows defined local variables.\n" +
1044                                         "  ShowUsing ();           - Show active using declarations.\n" +
1045                                         "  Prompt                  - The prompt used by the C# shell\n" +
1046                                         "  ContinuationPrompt      - The prompt for partial input\n" +
1047                                         "  Time(() -> { })         - Times the specified code\n" +
1048                                         "  quit;                   - You'll never believe it - this quits the repl!\n" +
1049                                         "  help;                   - This help text\n";
1050                         }
1051                 }
1052
1053                 /// <summary>
1054                 ///   Indicates to the read-eval-print-loop that the interaction should be finished. 
1055                 /// </summary>
1056                 static public object quit {
1057                         get {
1058                                 QuitRequested = true;
1059
1060                                 // To avoid print null at the exit
1061                                 return typeof (Evaluator.NoValueSet);
1062                         }
1063                 }
1064
1065 #if !NET_2_1
1066                 /// <summary>
1067                 ///   Describes an object or a type.
1068                 /// </summary>
1069                 /// <remarks>
1070                 ///   This method will show a textual representation
1071                 ///   of the object's type.  If the object is a
1072                 ///   System.Type it renders the type directly,
1073                 ///   otherwise it renders the type returned by
1074                 ///   invoking GetType on the object.
1075                 /// </remarks>
1076                 static public string Describe (object x)
1077                 {
1078                         if (x == null)
1079                                 return "<null>";
1080
1081                         var type = x as Type ?? x.GetType ();
1082
1083                         StringWriter sw = new StringWriter ();
1084                         new Outline (type, sw, true, false, false).OutlineType ();
1085                         return sw.ToString ();
1086                 }
1087 #endif
1088         }
1089
1090         class HoistedEvaluatorVariable : HoistedVariable
1091         {
1092                 public HoistedEvaluatorVariable (Field field)
1093                         : base (null, field)
1094                 {
1095                 }
1096
1097                 public override void EmitSymbolInfo ()
1098                 {
1099                 }
1100
1101                 protected override FieldExpr GetFieldExpression (EmitContext ec)
1102                 {
1103                         return new FieldExpr (field, field.Location);
1104                 }
1105         }
1106
1107         /// <summary>
1108         ///    A class used to assign values if the source expression is not void
1109         ///
1110         ///    Used by the interactive shell to allow it to call this code to set
1111         ///    the return value for an invocation.
1112         /// </summary>
1113         class OptionalAssign : SimpleAssign {
1114                 public OptionalAssign (Expression t, Expression s, Location loc)
1115                         : base (t, s, loc)
1116                 {
1117                 }
1118
1119                 protected override Expression DoResolve (ResolveContext ec)
1120                 {
1121                         CloneContext cc = new CloneContext ();
1122                         Expression clone = source.Clone (cc);
1123
1124                         //
1125                         // A useful feature for the REPL: if we can resolve the expression
1126                         // as a type, Describe the type;
1127                         //
1128                         if (Evaluator.DescribeTypeExpressions){
1129                                 var old_printer = Evaluator.SetPrinter (new StreamReportPrinter (TextWriter.Null));
1130                                 clone = clone.Resolve (ec);
1131                                 if (clone == null){
1132                                         clone = source.Clone (cc);
1133                                         clone = clone.Resolve (ec, ResolveFlags.Type);
1134                                         if (clone == null){
1135                                                 Evaluator.SetPrinter (old_printer);
1136                                                 clone = source.Clone (cc);
1137                                                 clone = clone.Resolve (ec);
1138                                                 return null;
1139                                         }
1140                                         
1141                                         Arguments args = new Arguments (1);
1142                                         args.Add (new Argument (new TypeOf ((TypeExpr) clone, Location)));
1143                                         source = new Invocation (new SimpleName ("Describe", Location), args).Resolve (ec);
1144                                 }
1145                                 Evaluator.SetPrinter (old_printer);
1146                         } else {
1147                                 clone = clone.Resolve (ec);
1148                                 if (clone == null)
1149                                         return null;
1150                         }
1151         
1152                         // This means its really a statement.
1153                         if (clone.Type == TypeManager.void_type || clone is DynamicInvocation || clone is Assign) {
1154                                 return clone;
1155                         }
1156
1157                         return base.DoResolve (ec);
1158                 }
1159         }
1160
1161         public class Undo {
1162                 List<KeyValuePair<TypeContainer, TypeContainer>> undo_types;
1163                 
1164                 public Undo ()
1165                 {
1166                         undo_types = new List<KeyValuePair<TypeContainer, TypeContainer>> ();
1167                 }
1168
1169                 public void AddTypeContainer (TypeContainer current_container, TypeContainer tc)
1170                 {
1171                         if (current_container == tc){
1172                                 Console.Error.WriteLine ("Internal error: inserting container into itself");
1173                                 return;
1174                         }
1175
1176                         if (undo_types == null)
1177                                 undo_types = new List<KeyValuePair<TypeContainer, TypeContainer>> ();
1178
1179                         undo_types.Add (new KeyValuePair<TypeContainer, TypeContainer> (current_container, tc));
1180                 }
1181
1182                 public void ExecuteUndo ()
1183                 {
1184                         if (undo_types == null)
1185                                 return;
1186
1187                         foreach (var p in undo_types){
1188                                 TypeContainer current_container = p.Key;
1189
1190                                 current_container.RemoveTypeContainer (p.Value);
1191                         }
1192                         undo_types = null;
1193                 }
1194         }
1195         
1196 }
1197