2 // eval.cs: Evaluation and Hosting API for the C# compiler
5 // Miguel de Icaza (miguel@gnome.org)
6 // Marek Safar (marek.safar@gmail.com)
8 // Dual licensed under the terms of the MIT X11 or GNU GPL
10 // Copyright 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
11 // Copyright 2004-2011 Novell, Inc
15 using System.Threading;
16 using System.Collections.Generic;
17 using System.Reflection;
18 using System.Reflection.Emit;
27 /// Evaluator: provides an API to evaluate C# statements and
28 /// expressions dynamically.
31 /// This class exposes static methods to evaluate expressions in the
34 /// To initialize the evaluator with a number of compiler
35 /// options call the Init(string[]args) method with a set of
36 /// command line options that the compiler recognizes.
38 /// To interrupt execution of a statement, you can invoke the
39 /// Evaluator.Interrupt method.
41 public class Evaluator {
44 // Parse silently, do not output any error messages
47 // Report errors during parse
50 // Auto-complete, means that the tokenizer will start producing
51 // GETCOMPLETIONS tokens when it reaches a certain point.
55 static object evaluator_lock = new object ();
56 static volatile bool invoking;
59 static Thread invoke_thread;
61 readonly Dictionary<string, Tuple<FieldSpec, FieldInfo>> fields;
67 readonly CompilerContext ctx;
68 readonly ModuleContainer module;
69 readonly ReflectionImporter importer;
70 readonly CompilationSourceFile source_file;
72 public Evaluator (CompilerSettings settings, Report report)
74 ctx = new CompilerContext (settings, report);
76 module = new ModuleContainer (ctx);
77 module.Evaluator = this;
79 source_file = new CompilationSourceFile ("{interactive}", "", 1);
80 source_file.NamespaceContainer = new NamespaceContainer (null, module, null, source_file);
82 startup_files = ctx.SourceFiles.Count;
83 ctx.SourceFiles.Add (source_file);
85 // FIXME: Importer needs this assembly for internalsvisibleto
86 module.SetDeclaringAssembly (new AssemblyDefinitionDynamic (module, "evaluator"));
87 importer = new ReflectionImporter (module, ctx.BuiltinTypes);
89 InteractiveBaseClass = typeof (InteractiveBase);
90 fields = new Dictionary<string, Tuple<FieldSpec, FieldInfo>> ();
95 var loader = new DynamicLoader (importer, ctx);
97 CompilerCallableEntryPoint.Reset ();
98 RootContext.ToplevelTypes = module;
100 //var startup_files = new List<string> ();
101 //foreach (CompilationUnit file in Location.SourceFiles)
102 // startup_files.Add (file.Path);
104 loader.LoadReferences (module);
105 ctx.BuiltinTypes.CheckDefinitions (module);
106 module.InitializePredefinedTypes ();
111 void ParseStartupFiles ()
113 Driver d = new Driver (ctx);
115 Location.Initialize (ctx.SourceFiles);
117 for (int i = 0; i < startup_files; ++i) {
118 var sf = ctx.Settings.SourceFiles [i];
119 d.Parse (sf, module);
125 CompilerCallableEntryPoint.PartialReset ();
128 Location.Initialize (ctx.SourceFiles);
132 /// If true, turns type expressions into valid expressions
133 /// and calls the describe method on it
135 public bool DescribeTypeExpressions;
138 /// The base class for the classes that host the user generated code
142 /// This is the base class that will host the code
143 /// executed by the Evaluator. By default
144 /// this is the Mono.CSharp.InteractiveBase class
145 /// which is useful for interactive use.
147 /// By changing this property you can control the
148 /// base class and the static members that are
149 /// available to your evaluated code.
151 public Type InteractiveBaseClass {
158 if (value != null && typeof (InteractiveBase).IsAssignableFrom (value))
159 InteractiveBase.Evaluator = this;
164 /// Interrupts the evaluation of an expression executing in Evaluate.
167 /// Use this method to interrupt long-running invocations.
169 public void Interrupt ()
171 if (!inited || !invoking)
174 if (invoke_thread != null)
175 invoke_thread.Abort ();
179 /// Compiles the input string and returns a delegate that represents the compiled code.
183 /// Compiles the input string as a C# expression or
184 /// statement, unlike the Evaluate method, the
185 /// resulting delegate can be invoked multiple times
186 /// without incurring in the compilation overhead.
188 /// If the return value of this function is null,
189 /// this indicates that the parsing was complete.
190 /// If the return value is a string it indicates
191 /// that the input string was partial and that the
192 /// invoking code should provide more code before
193 /// the code can be successfully compiled.
195 /// If you know that you will always get full expressions or
196 /// statements and do not care about partial input, you can use
197 /// the other Compile overload.
199 /// On success, in addition to returning null, the
200 /// compiled parameter will be set to the delegate
201 /// that can be invoked to execute the code.
204 public string Compile (string input, out CompiledMethod compiled)
206 if (input == null || input.Length == 0){
211 lock (evaluator_lock){
214 ParseStartupFiles ();
216 ctx.Report.Printer.Reset ();
220 CSharpParser parser = ParseString (ParseMode.Silent, input, out partial_input);
226 ParseString (ParseMode.ReportErrors, input, out partial_input);
230 Class parser_result = parser.InteractiveResult;
231 compiled = CompileBlock (parser_result, parser.undo, ctx.Report);
237 /// Compiles the input string and returns a delegate that represents the compiled code.
241 /// Compiles the input string as a C# expression or
242 /// statement, unlike the Evaluate method, the
243 /// resulting delegate can be invoked multiple times
244 /// without incurring in the compilation overhead.
246 /// This method can only deal with fully formed input
247 /// strings and does not provide a completion mechanism.
248 /// If you must deal with partial input (for example for
249 /// interactive use) use the other overload.
251 /// On success, a delegate is returned that can be used
252 /// to invoke the method.
255 public CompiledMethod Compile (string input)
257 CompiledMethod compiled;
259 // Ignore partial inputs
260 if (Compile (input, out compiled) != null){
261 // Error, the input was partial.
265 // Either null (on error) or the compiled method.
270 /// Evaluates and expression or statement and returns any result values.
273 /// Evaluates the input string as a C# expression or
274 /// statement. If the input string is an expression
275 /// the result will be stored in the result variable
276 /// and the result_set variable will be set to true.
278 /// It is necessary to use the result/result_set
279 /// pair to identify when a result was set (for
280 /// example, execution of user-provided input can be
281 /// an expression, a statement or others, and
282 /// result_set would only be set if the input was an
285 /// If the return value of this function is null,
286 /// this indicates that the parsing was complete.
287 /// If the return value is a string, it indicates
288 /// that the input is partial and that the user
289 /// should provide an updated string.
291 public string Evaluate (string input, out object result, out bool result_set)
293 CompiledMethod compiled;
298 input = Compile (input, out compiled);
302 if (compiled == null)
306 // The code execution does not need to keep the compiler lock
308 object retval = typeof (QuitValue);
311 invoke_thread = System.Threading.Thread.CurrentThread;
313 compiled (ref retval);
314 } catch (ThreadAbortException e){
315 Thread.ResetAbort ();
316 Console.WriteLine ("Interrupted!\n{0}", e);
322 // We use a reference to a compiler type, in this case
323 // Driver as a flag to indicate that this was a statement
325 if (!ReferenceEquals (retval, typeof (QuitValue))) {
333 public string [] GetCompletions (string input, out string prefix)
336 if (input == null || input.Length == 0)
339 lock (evaluator_lock){
344 CSharpParser parser = ParseString (ParseMode.GetCompletions, input, out partial_input);
346 if (CSharpParser.yacc_verbose_flag != 0)
347 Console.WriteLine ("DEBUG: No completions available");
351 Class parser_result = parser.InteractiveResult;
354 var access = AssemblyBuilderAccess.RunAndCollect;
356 var access = AssemblyBuilderAccess.Run;
358 var a = new AssemblyDefinitionDynamic (module, "completions");
359 a.Create (AppDomain.CurrentDomain, access);
360 module.SetDeclaringAssembly (a);
362 // Need to setup MemberCache
363 parser_result.CreateType ();
365 var method = parser_result.Methods[0] as Method;
366 BlockContext bc = new BlockContext (method, method.Block, ctx.BuiltinTypes.Void);
369 method.Block.Resolve (null, bc, method);
370 } catch (CompletionResult cr) {
371 prefix = cr.BaseText;
379 /// Executes the given expression or statement.
382 /// Executes the provided statement, returns true
383 /// on success, false on parsing errors. Exceptions
384 /// might be thrown by the called code.
386 public bool Run (string statement)
391 return Evaluate (statement, out result, out result_set) == null;
395 /// Evaluates and expression or statement and returns the result.
398 /// Evaluates the input string as a C# expression or
399 /// statement and returns the value.
401 /// This method will throw an exception if there is a syntax error,
402 /// of if the provided input is not an expression but a statement.
404 public object Evaluate (string input)
409 string r = Evaluate (input, out result, out result_set);
412 throw new ArgumentException ("Syntax error on input: partial input");
414 if (result_set == false)
415 throw new ArgumentException ("The expression did not set a result");
422 StatementOrExpression,
428 // Deambiguates the input string to determine if we
429 // want to process a statement or if we want to
430 // process a compilation unit.
432 // This is done using a top-down predictive parser,
433 // since the yacc/jay parser can not deambiguage this
434 // without more than one lookahead token. There are very
437 InputKind ToplevelOrStatement (SeekableStreamReader seekable)
439 Tokenizer tokenizer = new Tokenizer (seekable, source_file, ctx);
441 int t = tokenizer.token ();
444 return InputKind.EOF;
446 // These are toplevels
448 case Token.OPEN_BRACKET:
452 case Token.INTERFACE:
454 case Token.NAMESPACE:
456 case Token.PROTECTED:
461 return InputKind.CompilationUnit;
463 // Definitely expression
480 return InputKind.StatementOrExpression;
482 // These need deambiguation help
484 t = tokenizer.token ();
486 return InputKind.EOF;
488 if (t == Token.IDENTIFIER)
489 return InputKind.CompilationUnit;
490 return InputKind.StatementOrExpression;
493 // Distinguish between:
494 // delegate opt_anonymous_method_signature block
497 t = tokenizer.token ();
499 return InputKind.EOF;
500 if (t == Token.OPEN_PARENS || t == Token.OPEN_BRACE)
501 return InputKind.StatementOrExpression;
502 return InputKind.CompilationUnit;
504 // Distinguih between:
506 // unsafe as modifier of a type declaration
508 t = tokenizer.token ();
510 return InputKind.EOF;
511 if (t == Token.OPEN_PARENS)
512 return InputKind.StatementOrExpression;
513 return InputKind.CompilationUnit;
515 // These are errors: we list explicitly what we had
516 // from the grammar, ERROR and then everything else
521 return InputKind.Error;
523 // This catches everything else allowed by
524 // expressions. We could add one-by-one use cases
527 return InputKind.StatementOrExpression;
532 // Parses the string @input and returns a CSharpParser if succeeful.
534 // if @silent is set to true then no errors are
535 // reported to the user. This is used to do various calls to the
536 // parser and check if the expression is parsable.
538 // @partial_input: if @silent is true, then it returns whether the
539 // parsed expression was partial, and more data is needed
541 CSharpParser ParseString (ParseMode mode, string input, out bool partial_input)
543 partial_input = false;
545 Tokenizer.LocatedToken.Initialize ();
547 var enc = ctx.Settings.Encoding;
548 var s = new MemoryStream (enc.GetBytes (input));
549 SeekableStreamReader seekable = new SeekableStreamReader (s, enc);
551 InputKind kind = ToplevelOrStatement (seekable);
552 if (kind == InputKind.Error){
553 if (mode == ParseMode.ReportErrors)
554 ctx.Report.Error (-25, "Detection Parsing Error");
555 partial_input = false;
559 if (kind == InputKind.EOF){
560 if (mode == ParseMode.ReportErrors)
561 Console.Error.WriteLine ("Internal error: EOF condition should have been detected in a previous call with silent=true");
562 partial_input = true;
566 seekable.Position = 0;
568 source_file.NamespaceContainer.DeclarationFound = false;
569 CSharpParser parser = new CSharpParser (seekable, source_file);
571 if (kind == InputKind.StatementOrExpression){
572 parser.Lexer.putback_char = Tokenizer.EvalStatementParserCharacter;
573 ctx.Settings.StatementMode = true;
575 parser.Lexer.putback_char = Tokenizer.EvalCompilationUnitParserCharacter;
576 ctx.Settings.StatementMode = false;
579 if (mode == ParseMode.GetCompletions)
580 parser.Lexer.CompleteOnEOF = true;
582 ReportPrinter old_printer = null;
583 if ((mode == ParseMode.Silent || mode == ParseMode.GetCompletions) && CSharpParser.yacc_verbose_flag == 0)
584 old_printer = ctx.Report.SetPrinter (new StreamReportPrinter (TextWriter.Null));
589 if (ctx.Report.Errors != 0){
590 if (mode != ParseMode.ReportErrors && parser.UnexpectedEOF)
591 partial_input = true;
593 if (parser.undo != null)
594 parser.undo.ExecuteUndo ();
599 if (old_printer != null)
600 ctx.Report.SetPrinter (old_printer);
605 CompiledMethod CompileBlock (Class host, Undo undo, Report Report)
607 string current_debug_name = "eval-" + count + ".dll";
610 throw new NotSupportedException ();
612 AssemblyDefinitionDynamic assembly;
613 AssemblyBuilderAccess access;
615 if (Environment.GetEnvironmentVariable ("SAVE") != null) {
616 access = AssemblyBuilderAccess.RunAndSave;
617 assembly = new AssemblyDefinitionDynamic (module, current_debug_name, current_debug_name);
618 assembly.Importer = importer;
621 access = AssemblyBuilderAccess.RunAndCollect;
623 access = AssemblyBuilderAccess.Run;
625 assembly = new AssemblyDefinitionDynamic (module, current_debug_name);
628 assembly.Create (AppDomain.CurrentDomain, access);
630 Method expression_method;
632 var base_class_imported = importer.ImportType (base_class);
633 var baseclass_list = new List<FullNamedExpression> (1) {
634 new TypeExpression (base_class_imported, host.Location)
637 host.AddBasesForPart (host, baseclass_list);
643 expression_method = (Method) host.Methods[0];
645 expression_method = null;
648 module.CreateType ();
651 if (Report.Errors != 0){
663 if (Report.Errors != 0){
673 if (access == AssemblyBuilderAccess.RunAndSave)
680 // Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
681 // work from MethodBuilders. Retarded, I know.
683 var tt = assembly.Builder.GetType (host.TypeBuilder.Name);
684 var mi = tt.GetMethod (expression_method.Name);
686 if (host.Fields != null) {
688 // We need to then go from FieldBuilder to FieldInfo
689 // or reflection gets confused (it basically gets confused, and variables override each
692 foreach (Field field in host.Fields) {
693 var fi = tt.GetField (field.Name);
695 Tuple<FieldSpec, FieldInfo> old;
697 // If a previous value was set, nullify it, so that we do
699 if (fields.TryGetValue (field.Name, out old)) {
700 if (old.Item1.MemberType.IsStruct) {
702 // TODO: Clear fields for structs
706 old.Item2.SetValue (null, null);
712 fields[field.Name] = Tuple.Create (field.Spec, fi);
716 return (CompiledMethod) System.Delegate.CreateDelegate (typeof (CompiledMethod), mi);
721 /// A sentinel value used to indicate that no value was
722 /// was set by the compiled function. This is used to
723 /// differentiate between a function not returning a
726 internal static class QuitValue { }
728 internal Tuple<FieldSpec, FieldInfo> LookupField (string name)
730 Tuple<FieldSpec, FieldInfo> fi;
731 fields.TryGetValue (name, out fi);
735 static string Quote (string s)
737 if (s.IndexOf ('"') != -1)
738 s = s.Replace ("\"", "\\\"");
740 return "\"" + s + "\"";
743 public string GetUsing ()
745 StringBuilder sb = new StringBuilder ();
747 //foreach (object x in ns.using_alias_list)
748 // sb.AppendFormat ("using {0};\n", x);
750 foreach (var ue in source_file.NamespaceContainer.Usings) {
751 sb.AppendFormat ("using {0};", ue.ToString ());
752 sb.Append (Environment.NewLine);
755 return sb.ToString ();
758 internal ICollection<string> GetUsingList ()
760 var res = new List<string> ();
762 foreach (var ue in source_file.NamespaceContainer.Usings)
767 internal string [] GetVarNames ()
769 lock (evaluator_lock){
770 return new List<string> (fields.Keys).ToArray ();
774 public string GetVars ()
776 lock (evaluator_lock){
777 StringBuilder sb = new StringBuilder ();
779 foreach (var de in fields){
780 var fi = LookupField (de.Key);
783 value = fi.Item2.GetValue (null);
785 value = Quote ((string)value);
787 value = "<error reading value>";
790 sb.AppendFormat ("{0} {1} = {2}", fi.Item1.MemberType.GetSignatureForError (), de.Key, value);
794 return sb.ToString ();
799 /// Loads the given assembly and exposes the API to the user.
801 public void LoadAssembly (string file)
803 var loader = new DynamicLoader (importer, ctx);
804 var assembly = loader.LoadAssemblyFile (file);
805 if (assembly == null)
808 lock (evaluator_lock){
809 importer.ImportAssembly (assembly, module.GlobalRootNamespace);
814 /// Exposes the API of the given assembly to the Evaluator
816 public void ReferenceAssembly (Assembly a)
818 lock (evaluator_lock){
819 importer.ImportAssembly (a, module.GlobalRootNamespace);
826 /// A delegate that can be used to invoke the
827 /// compiled expression or statement.
830 /// Since the Compile methods will compile
831 /// statements and expressions into the same
832 /// delegate, you can tell if a value was returned
833 /// by checking whether the returned value is of type
837 public delegate void CompiledMethod (ref object retvalue);
840 /// The default base class for every interaction line
843 /// The expressions and statements behave as if they were
844 /// a static method of this class. The InteractiveBase class
845 /// contains a number of useful methods, but can be overwritten
846 /// by setting the InteractiveBaseType property in the Evaluator
848 public class InteractiveBase {
850 /// Determines where the standard output of methods in this class will go.
852 public static TextWriter Output = Console.Out;
855 /// Determines where the standard error of methods in this class will go.
857 public static TextWriter Error = Console.Error;
860 /// The primary prompt used for interactive use.
862 public static string Prompt = "csharp> ";
865 /// The secondary prompt used for interactive use (used when
866 /// an expression is incomplete).
868 public static string ContinuationPrompt = " > ";
871 /// Used to signal that the user has invoked the `quit' statement.
873 public static bool QuitRequested;
875 public static Evaluator Evaluator;
878 /// Shows all the variables defined so far.
880 static public void ShowVars ()
882 Output.Write (Evaluator.GetVars ());
887 /// Displays the using statements in effect at this point.
889 static public void ShowUsing ()
891 Output.Write (Evaluator.GetUsing ());
896 /// Times the execution of the given delegate
898 static public TimeSpan Time (Action a)
900 DateTime start = DateTime.Now;
902 return DateTime.Now - start;
906 /// Loads the assemblies from a package
909 /// Loads the assemblies from a package. This is equivalent
910 /// to passing the -pkg: command line flag to the C# compiler
911 /// on the command line.
913 static public void LoadPackage (string pkg)
916 Error.WriteLine ("Invalid package specified");
920 string pkgout = Driver.GetPackageFlags (pkg, null);
922 string [] xargs = pkgout.Trim (new Char [] {' ', '\n', '\r', '\t'}).
923 Split (new Char [] { ' ', '\t'});
925 foreach (string s in xargs){
926 if (s.StartsWith ("-r:") || s.StartsWith ("/r:") || s.StartsWith ("/reference:")){
927 string lib = s.Substring (s.IndexOf (':')+1);
929 Evaluator.LoadAssembly (lib);
936 /// Loads the assembly
939 /// Loads the specified assembly and makes its types
940 /// available to the evaluator. This is equivalent
941 /// to passing the -pkg: command line flag to the C#
942 /// compiler on the command line.
944 static public void LoadAssembly (string assembly)
946 Evaluator.LoadAssembly (assembly);
949 static public void print (object obj)
951 Output.WriteLine (obj);
954 static public void print (string fmt, params object [] args)
956 Output.WriteLine (fmt, args);
960 /// Returns a list of available static methods.
962 static public string help {
964 return "Static methods:\n" +
965 " Describe (object); - Describes the object's type\n" +
966 " LoadPackage (package); - Loads the given Package (like -pkg:FILE)\n" +
967 " LoadAssembly (assembly); - Loads the given assembly (like -r:ASSEMBLY)\n" +
968 " ShowVars (); - Shows defined local variables.\n" +
969 " ShowUsing (); - Show active using declarations.\n" +
970 " Prompt - The prompt used by the C# shell\n" +
971 " ContinuationPrompt - The prompt for partial input\n" +
972 " Time (() => { }); - Times the specified code\n" +
973 " print (obj); - Shorthand for Console.WriteLine\n" +
974 " quit; - You'll never believe it - this quits the repl!\n" +
975 " help; - This help text\n";
980 /// Indicates to the read-eval-print-loop that the interaction should be finished.
982 static public object quit {
984 QuitRequested = true;
986 // To avoid print null at the exit
987 return typeof (Evaluator.QuitValue);
993 /// Describes an object or a type.
996 /// This method will show a textual representation
997 /// of the object's type. If the object is a
998 /// System.Type it renders the type directly,
999 /// otherwise it renders the type returned by
1000 /// invoking GetType on the object.
1002 static public string Describe (object x)
1007 var type = x as Type ?? x.GetType ();
1009 StringWriter sw = new StringWriter ();
1010 new Outline (type, sw, true, false, false).OutlineType ();
1011 return sw.ToString ();
1016 class HoistedEvaluatorVariable : HoistedVariable
1018 public HoistedEvaluatorVariable (Field field)
1019 : base (null, field)
1023 public override void EmitSymbolInfo ()
1027 protected override FieldExpr GetFieldExpression (EmitContext ec)
1029 return new FieldExpr (field, field.Location);
1034 /// A class used to assign values if the source expression is not void
1036 /// Used by the interactive shell to allow it to call this code to set
1037 /// the return value for an invocation.
1039 class OptionalAssign : SimpleAssign {
1040 public OptionalAssign (Expression t, Expression s, Location loc)
1045 protected override Expression DoResolve (ResolveContext ec)
1047 Expression clone = source.Clone (new CloneContext ());
1049 clone = clone.Resolve (ec);
1054 // A useful feature for the REPL: if we can resolve the expression
1055 // as a type, Describe the type;
1057 if (ec.Module.Evaluator.DescribeTypeExpressions){
1058 var old_printer = ec.Report.SetPrinter (new SessionReportPrinter ());
1061 // Note: clone context cannot be shared otherwise block mapping would leak
1062 tclone = source.Clone (new CloneContext ());
1063 tclone = tclone.Resolve (ec, ResolveFlags.Type);
1064 if (ec.Report.Errors > 0)
1067 ec.Report.SetPrinter (old_printer);
1070 if (tclone != null) {
1071 Arguments args = new Arguments (1);
1072 args.Add (new Argument (new TypeOf ((TypeExpr) clone, Location)));
1073 return new Invocation (new SimpleName ("Describe", Location), args).Resolve (ec);
1077 // This means its really a statement.
1078 if (clone.Type.Kind == MemberKind.Void || clone is DynamicInvocation || clone is Assign) {
1083 return base.DoResolve (ec);
1089 List<Action> undo_actions;
1095 public void AddTypeContainer (TypeContainer current_container, TypeContainer tc)
1097 if (current_container == tc){
1098 Console.Error.WriteLine ("Internal error: inserting container into itself");
1102 if (undo_actions == null)
1103 undo_actions = new List<Action> ();
1105 var existing = current_container.Types.FirstOrDefault (l => l.MemberName.Basename == tc.MemberName.Basename);
1106 if (existing != null) {
1107 current_container.RemoveTypeContainer (existing);
1108 existing.NamespaceEntry.SlaveDeclSpace.RemoveTypeContainer (existing);
1109 undo_actions.Add (() => current_container.AddTypeContainer (existing));
1112 undo_actions.Add (() => current_container.RemoveTypeContainer (tc));
1115 public void ExecuteUndo ()
1117 if (undo_actions == null)
1120 foreach (var p in undo_actions){
1124 undo_actions = null;