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;
66 readonly CompilerContext ctx;
67 readonly ModuleContainer module;
68 readonly ReflectionImporter importer;
70 // TODO: somehow merge with module
73 public Evaluator (CompilerSettings settings, Report report)
75 ctx = new CompilerContext (settings, report);
77 module = new ModuleContainer (ctx);
78 module.Evaluator = this;
80 // FIXME: Importer needs this assembly for internalsvisibleto
81 module.SetDeclaringAssembly (new AssemblyDefinitionDynamic (module, "evaluator"));
82 importer = new ReflectionImporter (module, ctx.BuildinTypes);
84 InteractiveBaseClass = typeof (InteractiveBase);
85 fields = new Dictionary<string, Tuple<FieldSpec, FieldInfo>> ();
90 var loader = new DynamicLoader (importer, ctx);
92 CompilerCallableEntryPoint.Reset ();
93 RootContext.ToplevelTypes = module;
95 //var startup_files = new List<string> ();
96 //foreach (CompilationUnit file in Location.SourceFiles)
97 // startup_files.Add (file.Path);
99 loader.LoadReferences (module);
100 ctx.BuildinTypes.CheckDefinitions (module);
101 module.InitializePredefinedTypes ();
108 CompilerCallableEntryPoint.PartialReset ();
110 Location.AddFile (null, "{interactive}");
111 Location.Initialize ();
115 /// If true, turns type expressions into valid expressions
116 /// and calls the describe method on it
118 public bool DescribeTypeExpressions;
121 /// The base class for the classes that host the user generated code
125 /// This is the base class that will host the code
126 /// executed by the Evaluator. By default
127 /// this is the Mono.CSharp.InteractiveBase class
128 /// which is useful for interactive use.
130 /// By changing this property you can control the
131 /// base class and the static members that are
132 /// available to your evaluated code.
134 public Type InteractiveBaseClass {
141 if (value != null && typeof (InteractiveBase).IsAssignableFrom (value))
142 InteractiveBase.Evaluator = this;
147 /// Interrupts the evaluation of an expression executing in Evaluate.
150 /// Use this method to interrupt long-running invocations.
152 public void Interrupt ()
154 if (!inited || !invoking)
157 if (invoke_thread != null)
158 invoke_thread.Abort ();
162 /// Compiles the input string and returns a delegate that represents the compiled code.
166 /// Compiles the input string as a C# expression or
167 /// statement, unlike the Evaluate method, the
168 /// resulting delegate can be invoked multiple times
169 /// without incurring in the compilation overhead.
171 /// If the return value of this function is null,
172 /// this indicates that the parsing was complete.
173 /// If the return value is a string it indicates
174 /// that the input string was partial and that the
175 /// invoking code should provide more code before
176 /// the code can be successfully compiled.
178 /// If you know that you will always get full expressions or
179 /// statements and do not care about partial input, you can use
180 /// the other Compile overload.
182 /// On success, in addition to returning null, the
183 /// compiled parameter will be set to the delegate
184 /// that can be invoked to execute the code.
187 public string Compile (string input, out CompiledMethod compiled)
189 if (input == null || input.Length == 0){
194 lock (evaluator_lock){
198 ctx.Report.Printer.Reset ();
201 CSharpParser parser = ParseString (ParseMode.Silent, input, out partial_input);
207 ParseString (ParseMode.ReportErrors, input, out partial_input);
211 Class parser_result = parser.InteractiveResult;
212 compiled = CompileBlock (parser_result, parser.undo, ctx.Report);
218 /// Compiles the input string and returns a delegate that represents the compiled code.
222 /// Compiles the input string as a C# expression or
223 /// statement, unlike the Evaluate method, the
224 /// resulting delegate can be invoked multiple times
225 /// without incurring in the compilation overhead.
227 /// This method can only deal with fully formed input
228 /// strings and does not provide a completion mechanism.
229 /// If you must deal with partial input (for example for
230 /// interactive use) use the other overload.
232 /// On success, a delegate is returned that can be used
233 /// to invoke the method.
236 public CompiledMethod Compile (string input)
238 CompiledMethod compiled;
240 // Ignore partial inputs
241 if (Compile (input, out compiled) != null){
242 // Error, the input was partial.
246 // Either null (on error) or the compiled method.
251 /// Evaluates and expression or statement and returns any result values.
254 /// Evaluates the input string as a C# expression or
255 /// statement. If the input string is an expression
256 /// the result will be stored in the result variable
257 /// and the result_set variable will be set to true.
259 /// It is necessary to use the result/result_set
260 /// pair to identify when a result was set (for
261 /// example, execution of user-provided input can be
262 /// an expression, a statement or others, and
263 /// result_set would only be set if the input was an
266 /// If the return value of this function is null,
267 /// this indicates that the parsing was complete.
268 /// If the return value is a string, it indicates
269 /// that the input is partial and that the user
270 /// should provide an updated string.
272 public string Evaluate (string input, out object result, out bool result_set)
274 CompiledMethod compiled;
279 input = Compile (input, out compiled);
283 if (compiled == null)
287 // The code execution does not need to keep the compiler lock
289 object retval = typeof (QuitValue);
292 invoke_thread = System.Threading.Thread.CurrentThread;
294 compiled (ref retval);
295 } catch (ThreadAbortException e){
296 Thread.ResetAbort ();
297 Console.WriteLine ("Interrupted!\n{0}", e);
303 // We use a reference to a compiler type, in this case
304 // Driver as a flag to indicate that this was a statement
306 if (!ReferenceEquals (retval, typeof (QuitValue))) {
314 public string [] GetCompletions (string input, out string prefix)
317 if (input == null || input.Length == 0)
320 lock (evaluator_lock){
325 CSharpParser parser = ParseString (ParseMode.GetCompletions, input, out partial_input);
327 if (CSharpParser.yacc_verbose_flag != 0)
328 Console.WriteLine ("DEBUG: No completions available");
332 Class parser_result = parser.InteractiveResult;
335 var access = AssemblyBuilderAccess.RunAndCollect;
337 var access = AssemblyBuilderAccess.Run;
339 var a = new AssemblyDefinitionDynamic (module, "completions");
340 a.Create (AppDomain.CurrentDomain, access);
341 module.SetDeclaringAssembly (a);
343 // Need to setup MemberCache
344 parser_result.CreateType ();
346 var method = parser_result.Methods[0] as Method;
347 BlockContext bc = new BlockContext (method, method.Block, TypeManager.void_type);
350 method.Block.Resolve (null, bc, method);
351 } catch (CompletionResult cr) {
352 prefix = cr.BaseText;
360 /// Executes the given expression or statement.
363 /// Executes the provided statement, returns true
364 /// on success, false on parsing errors. Exceptions
365 /// might be thrown by the called code.
367 public bool Run (string statement)
372 return Evaluate (statement, out result, out result_set) == null;
376 /// Evaluates and expression or statement and returns the result.
379 /// Evaluates the input string as a C# expression or
380 /// statement and returns the value.
382 /// This method will throw an exception if there is a syntax error,
383 /// of if the provided input is not an expression but a statement.
385 public object Evaluate (string input)
390 string r = Evaluate (input, out result, out result_set);
393 throw new ArgumentException ("Syntax error on input: partial input");
395 if (result_set == false)
396 throw new ArgumentException ("The expression did not set a result");
403 StatementOrExpression,
409 // Deambiguates the input string to determine if we
410 // want to process a statement or if we want to
411 // process a compilation unit.
413 // This is done using a top-down predictive parser,
414 // since the yacc/jay parser can not deambiguage this
415 // without more than one lookahead token. There are very
418 InputKind ToplevelOrStatement (SeekableStreamReader seekable)
420 Tokenizer tokenizer = new Tokenizer (seekable, (CompilationUnit) Location.SourceFiles [0], ctx);
422 int t = tokenizer.token ();
425 return InputKind.EOF;
427 // These are toplevels
429 case Token.OPEN_BRACKET:
433 case Token.INTERFACE:
435 case Token.NAMESPACE:
437 case Token.PROTECTED:
442 return InputKind.CompilationUnit;
444 // Definitely expression
461 return InputKind.StatementOrExpression;
463 // These need deambiguation help
465 t = tokenizer.token ();
467 return InputKind.EOF;
469 if (t == Token.IDENTIFIER)
470 return InputKind.CompilationUnit;
471 return InputKind.StatementOrExpression;
474 // Distinguish between:
475 // delegate opt_anonymous_method_signature block
478 t = tokenizer.token ();
480 return InputKind.EOF;
481 if (t == Token.OPEN_PARENS || t == Token.OPEN_BRACE)
482 return InputKind.StatementOrExpression;
483 return InputKind.CompilationUnit;
485 // Distinguih between:
487 // unsafe as modifier of a type declaration
489 t = tokenizer.token ();
491 return InputKind.EOF;
492 if (t == Token.OPEN_PARENS)
493 return InputKind.StatementOrExpression;
494 return InputKind.CompilationUnit;
496 // These are errors: we list explicitly what we had
497 // from the grammar, ERROR and then everything else
502 return InputKind.Error;
504 // This catches everything else allowed by
505 // expressions. We could add one-by-one use cases
508 return InputKind.StatementOrExpression;
513 // Parses the string @input and returns a CSharpParser if succeeful.
515 // if @silent is set to true then no errors are
516 // reported to the user. This is used to do various calls to the
517 // parser and check if the expression is parsable.
519 // @partial_input: if @silent is true, then it returns whether the
520 // parsed expression was partial, and more data is needed
522 CSharpParser ParseString (ParseMode mode, string input, out bool partial_input)
524 partial_input = false;
526 Tokenizer.LocatedToken.Initialize ();
528 Stream s = new MemoryStream (Encoding.Default.GetBytes (input));
529 SeekableStreamReader seekable = new SeekableStreamReader (s, Encoding.Default);
531 InputKind kind = ToplevelOrStatement (seekable);
532 if (kind == InputKind.Error){
533 if (mode == ParseMode.ReportErrors)
534 ctx.Report.Error (-25, "Detection Parsing Error");
535 partial_input = false;
539 if (kind == InputKind.EOF){
540 if (mode == ParseMode.ReportErrors)
541 Console.Error.WriteLine ("Internal error: EOF condition should have been detected in a previous call with silent=true");
542 partial_input = true;
546 seekable.Position = 0;
549 ns = new NamespaceEntry (module, null, Location.SourceFiles[0], null);
551 CSharpParser parser = new CSharpParser (seekable, Location.SourceFiles [0], module, ns);
553 if (kind == InputKind.StatementOrExpression){
554 parser.Lexer.putback_char = Tokenizer.EvalStatementParserCharacter;
555 ctx.Settings.StatementMode = true;
557 parser.Lexer.putback_char = Tokenizer.EvalCompilationUnitParserCharacter;
558 ctx.Settings.StatementMode = false;
561 if (mode == ParseMode.GetCompletions)
562 parser.Lexer.CompleteOnEOF = true;
564 ReportPrinter old_printer = null;
565 if ((mode == ParseMode.Silent || mode == ParseMode.GetCompletions) && CSharpParser.yacc_verbose_flag == 0)
566 old_printer = ctx.Report.SetPrinter (new StreamReportPrinter (TextWriter.Null));
571 if (ctx.Report.Errors != 0){
572 if (mode != ParseMode.ReportErrors && parser.UnexpectedEOF)
573 partial_input = true;
575 if (parser.undo != null)
576 parser.undo.ExecuteUndo ();
581 if (old_printer != null)
582 ctx.Report.SetPrinter (old_printer);
587 CompiledMethod CompileBlock (Class host, Undo undo, Report Report)
589 string current_debug_name = "eval-" + count + ".dll";
592 throw new NotSupportedException ();
594 AssemblyDefinitionDynamic assembly;
595 AssemblyBuilderAccess access;
597 if (Environment.GetEnvironmentVariable ("SAVE") != null) {
598 access = AssemblyBuilderAccess.RunAndSave;
599 assembly = new AssemblyDefinitionDynamic (module, current_debug_name, current_debug_name);
600 assembly.Importer = importer;
603 access = AssemblyBuilderAccess.RunAndCollect;
605 access = AssemblyBuilderAccess.Run;
607 assembly = new AssemblyDefinitionDynamic (module, current_debug_name);
610 assembly.Create (AppDomain.CurrentDomain, access);
612 Method expression_method;
614 var base_class_imported = importer.ImportType (base_class);
615 var baseclass_list = new List<FullNamedExpression> (1) {
616 new TypeExpression (base_class_imported, host.Location)
619 host.AddBasesForPart (host, baseclass_list);
625 expression_method = (Method) host.Methods[0];
627 expression_method = null;
630 module.CreateType ();
633 if (Report.Errors != 0){
645 if (Report.Errors != 0){
655 if (access == AssemblyBuilderAccess.RunAndSave)
662 // Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
663 // work from MethodBuilders. Retarded, I know.
665 var tt = assembly.Builder.GetType (host.TypeBuilder.Name);
666 var mi = tt.GetMethod (expression_method.Name);
668 if (host.Fields != null) {
670 // We need to then go from FieldBuilder to FieldInfo
671 // or reflection gets confused (it basically gets confused, and variables override each
674 foreach (Field field in host.Fields) {
675 var fi = tt.GetField (field.Name);
677 Tuple<FieldSpec, FieldInfo> old;
679 // If a previous value was set, nullify it, so that we do
681 if (fields.TryGetValue (field.Name, out old)) {
682 if (old.Item1.MemberType.IsStruct) {
684 // TODO: Clear fields for structs
688 old.Item2.SetValue (null, null);
694 fields[field.Name] = Tuple.Create (field.Spec, fi);
698 return (CompiledMethod) System.Delegate.CreateDelegate (typeof (CompiledMethod), mi);
703 /// A sentinel value used to indicate that no value was
704 /// was set by the compiled function. This is used to
705 /// differentiate between a function not returning a
708 internal static class QuitValue { }
710 internal Tuple<FieldSpec, FieldInfo> LookupField (string name)
712 Tuple<FieldSpec, FieldInfo> fi;
713 fields.TryGetValue (name, out fi);
717 static string Quote (string s)
719 if (s.IndexOf ('"') != -1)
720 s = s.Replace ("\"", "\\\"");
722 return "\"" + s + "\"";
725 public string GetUsing ()
730 StringBuilder sb = new StringBuilder ();
732 //foreach (object x in ns.using_alias_list)
733 // sb.AppendFormat ("using {0};\n", x);
735 foreach (var ue in ns.Usings) {
736 sb.AppendFormat ("using {0};", ue.ToString ());
737 sb.Append (Environment.NewLine);
740 return sb.ToString ();
743 internal ICollection<string> GetUsingList ()
745 var res = new List<string> ();
747 foreach (var ue in ns.Usings)
748 res.Add (ue.ToString ());
752 internal string [] GetVarNames ()
754 lock (evaluator_lock){
755 return new List<string> (fields.Keys).ToArray ();
759 public string GetVars ()
761 lock (evaluator_lock){
762 StringBuilder sb = new StringBuilder ();
764 foreach (var de in fields){
765 var fi = LookupField (de.Key);
768 value = fi.Item2.GetValue (null);
770 value = Quote ((string)value);
772 value = "<error reading value>";
775 sb.AppendFormat ("{0} {1} = {2}", fi.Item1.MemberType.GetSignatureForError (), de.Key, value);
779 return sb.ToString ();
784 /// Loads the given assembly and exposes the API to the user.
786 public void LoadAssembly (string file)
788 var loader = new DynamicLoader (importer, ctx);
789 var assembly = loader.LoadAssemblyFile (file);
790 if (assembly == null)
793 lock (evaluator_lock){
794 importer.ImportAssembly (assembly, module.GlobalRootNamespace);
799 /// Exposes the API of the given assembly to the Evaluator
801 public void ReferenceAssembly (Assembly a)
803 lock (evaluator_lock){
804 importer.ImportAssembly (a, module.GlobalRootNamespace);
811 /// A delegate that can be used to invoke the
812 /// compiled expression or statement.
815 /// Since the Compile methods will compile
816 /// statements and expressions into the same
817 /// delegate, you can tell if a value was returned
818 /// by checking whether the returned value is of type
822 public delegate void CompiledMethod (ref object retvalue);
825 /// The default base class for every interaction line
828 /// The expressions and statements behave as if they were
829 /// a static method of this class. The InteractiveBase class
830 /// contains a number of useful methods, but can be overwritten
831 /// by setting the InteractiveBaseType property in the Evaluator
833 public class InteractiveBase {
835 /// Determines where the standard output of methods in this class will go.
837 public static TextWriter Output = Console.Out;
840 /// Determines where the standard error of methods in this class will go.
842 public static TextWriter Error = Console.Error;
845 /// The primary prompt used for interactive use.
847 public static string Prompt = "csharp> ";
850 /// The secondary prompt used for interactive use (used when
851 /// an expression is incomplete).
853 public static string ContinuationPrompt = " > ";
856 /// Used to signal that the user has invoked the `quit' statement.
858 public static bool QuitRequested;
860 public static Evaluator Evaluator;
863 /// Shows all the variables defined so far.
865 static public void ShowVars ()
867 Output.Write (Evaluator.GetVars ());
872 /// Displays the using statements in effect at this point.
874 static public void ShowUsing ()
876 Output.Write (Evaluator.GetUsing ());
881 /// Times the execution of the given delegate
883 static public TimeSpan Time (Action a)
885 DateTime start = DateTime.Now;
887 return DateTime.Now - start;
891 /// Loads the assemblies from a package
894 /// Loads the assemblies from a package. This is equivalent
895 /// to passing the -pkg: command line flag to the C# compiler
896 /// on the command line.
898 static public void LoadPackage (string pkg)
901 Error.WriteLine ("Invalid package specified");
905 string pkgout = Driver.GetPackageFlags (pkg, null);
907 string [] xargs = pkgout.Trim (new Char [] {' ', '\n', '\r', '\t'}).
908 Split (new Char [] { ' ', '\t'});
910 foreach (string s in xargs){
911 if (s.StartsWith ("-r:") || s.StartsWith ("/r:") || s.StartsWith ("/reference:")){
912 string lib = s.Substring (s.IndexOf (':')+1);
914 Evaluator.LoadAssembly (lib);
921 /// Loads the assembly
924 /// Loads the specified assembly and makes its types
925 /// available to the evaluator. This is equivalent
926 /// to passing the -pkg: command line flag to the C#
927 /// compiler on the command line.
929 static public void LoadAssembly (string assembly)
931 Evaluator.LoadAssembly (assembly);
934 static public void print (string obj)
936 Output.WriteLine (obj);
939 static public void print (string fmt, params object [] args)
941 Output.WriteLine (fmt, args);
945 /// Returns a list of available static methods.
947 static public string help {
949 return "Static methods:\n" +
950 " Describe (object) - Describes the object's type\n" +
951 " LoadPackage (package); - Loads the given Package (like -pkg:FILE)\n" +
952 " LoadAssembly (assembly) - Loads the given assembly (like -r:ASSEMBLY)\n" +
953 " ShowVars (); - Shows defined local variables.\n" +
954 " ShowUsing (); - Show active using declarations.\n" +
955 " Prompt - The prompt used by the C# shell\n" +
956 " ContinuationPrompt - The prompt for partial input\n" +
957 " Time(() -> { }) - Times the specified code\n" +
958 " print (obj) - Shorthand for Console.WriteLine\n" +
959 " quit; - You'll never believe it - this quits the repl!\n" +
960 " help; - This help text\n";
965 /// Indicates to the read-eval-print-loop that the interaction should be finished.
967 static public object quit {
969 QuitRequested = true;
971 // To avoid print null at the exit
972 return typeof (Evaluator.QuitValue);
978 /// Describes an object or a type.
981 /// This method will show a textual representation
982 /// of the object's type. If the object is a
983 /// System.Type it renders the type directly,
984 /// otherwise it renders the type returned by
985 /// invoking GetType on the object.
987 static public string Describe (object x)
992 var type = x as Type ?? x.GetType ();
994 StringWriter sw = new StringWriter ();
995 new Outline (type, sw, true, false, false).OutlineType ();
996 return sw.ToString ();
1001 class HoistedEvaluatorVariable : HoistedVariable
1003 public HoistedEvaluatorVariable (Field field)
1004 : base (null, field)
1008 public override void EmitSymbolInfo ()
1012 protected override FieldExpr GetFieldExpression (EmitContext ec)
1014 return new FieldExpr (field, field.Location);
1019 /// A class used to assign values if the source expression is not void
1021 /// Used by the interactive shell to allow it to call this code to set
1022 /// the return value for an invocation.
1024 class OptionalAssign : SimpleAssign {
1025 public OptionalAssign (Expression t, Expression s, Location loc)
1030 protected override Expression DoResolve (ResolveContext ec)
1032 CloneContext cc = new CloneContext ();
1033 Expression clone = source.Clone (cc);
1036 // A useful feature for the REPL: if we can resolve the expression
1037 // as a type, Describe the type;
1039 if (ec.Module.Evaluator.DescribeTypeExpressions){
1040 var old_printer = ec.Report.SetPrinter (new SessionReportPrinter ());
1042 clone = clone.Resolve (ec);
1043 if (clone == null) {
1044 clone = source.Clone (cc);
1045 clone = clone.Resolve (ec, ResolveFlags.Type);
1046 if (clone == null) {
1047 clone = source.Clone (cc);
1048 clone = clone.Resolve (ec);
1052 Arguments args = new Arguments (1);
1053 args.Add (new Argument (new TypeOf ((TypeExpr) clone, Location)));
1054 source = new Invocation (new SimpleName ("Describe", Location), args).Resolve (ec);
1057 ec.Report.SetPrinter (old_printer);
1060 clone = clone.Resolve (ec);
1065 // This means its really a statement.
1066 if (clone.Type == TypeManager.void_type || clone is DynamicInvocation || clone is Assign) {
1070 return base.DoResolve (ec);
1076 List<Action> undo_actions;
1082 public void AddTypeContainer (TypeContainer current_container, TypeContainer tc)
1084 if (current_container == tc){
1085 Console.Error.WriteLine ("Internal error: inserting container into itself");
1089 if (undo_actions == null)
1090 undo_actions = new List<Action> ();
1092 var existing = current_container.Types.FirstOrDefault (l => l.MemberName.Basename == tc.MemberName.Basename);
1093 if (existing != null) {
1094 current_container.RemoveTypeContainer (existing);
1095 undo_actions.Add (() => current_container.AddTypeContainer (existing));
1098 undo_actions.Add (() => current_container.RemoveTypeContainer (tc));
1101 public void ExecuteUndo ()
1103 if (undo_actions == null)
1106 foreach (var p in undo_actions){
1110 undo_actions = null;