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