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