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