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