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, ctx.BuildinTypes.Void);
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 ns.DeclarationFound = false;
552 CSharpParser parser = new CSharpParser (seekable, Location.SourceFiles [0], module, ns);
554 if (kind == InputKind.StatementOrExpression){
555 parser.Lexer.putback_char = Tokenizer.EvalStatementParserCharacter;
556 ctx.Settings.StatementMode = true;
558 parser.Lexer.putback_char = Tokenizer.EvalCompilationUnitParserCharacter;
559 ctx.Settings.StatementMode = false;
562 if (mode == ParseMode.GetCompletions)
563 parser.Lexer.CompleteOnEOF = true;
565 ReportPrinter old_printer = null;
566 if ((mode == ParseMode.Silent || mode == ParseMode.GetCompletions) && CSharpParser.yacc_verbose_flag == 0)
567 old_printer = ctx.Report.SetPrinter (new StreamReportPrinter (TextWriter.Null));
572 if (ctx.Report.Errors != 0){
573 if (mode != ParseMode.ReportErrors && parser.UnexpectedEOF)
574 partial_input = true;
576 if (parser.undo != null)
577 parser.undo.ExecuteUndo ();
582 if (old_printer != null)
583 ctx.Report.SetPrinter (old_printer);
588 CompiledMethod CompileBlock (Class host, Undo undo, Report Report)
590 string current_debug_name = "eval-" + count + ".dll";
593 throw new NotSupportedException ();
595 AssemblyDefinitionDynamic assembly;
596 AssemblyBuilderAccess access;
598 if (Environment.GetEnvironmentVariable ("SAVE") != null) {
599 access = AssemblyBuilderAccess.RunAndSave;
600 assembly = new AssemblyDefinitionDynamic (module, current_debug_name, current_debug_name);
601 assembly.Importer = importer;
604 access = AssemblyBuilderAccess.RunAndCollect;
606 access = AssemblyBuilderAccess.Run;
608 assembly = new AssemblyDefinitionDynamic (module, current_debug_name);
611 assembly.Create (AppDomain.CurrentDomain, access);
613 Method expression_method;
615 var base_class_imported = importer.ImportType (base_class);
616 var baseclass_list = new List<FullNamedExpression> (1) {
617 new TypeExpression (base_class_imported, host.Location)
620 host.AddBasesForPart (host, baseclass_list);
626 expression_method = (Method) host.Methods[0];
628 expression_method = null;
631 module.CreateType ();
634 if (Report.Errors != 0){
646 if (Report.Errors != 0){
656 if (access == AssemblyBuilderAccess.RunAndSave)
663 // Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
664 // work from MethodBuilders. Retarded, I know.
666 var tt = assembly.Builder.GetType (host.TypeBuilder.Name);
667 var mi = tt.GetMethod (expression_method.Name);
669 if (host.Fields != null) {
671 // We need to then go from FieldBuilder to FieldInfo
672 // or reflection gets confused (it basically gets confused, and variables override each
675 foreach (Field field in host.Fields) {
676 var fi = tt.GetField (field.Name);
678 Tuple<FieldSpec, FieldInfo> old;
680 // If a previous value was set, nullify it, so that we do
682 if (fields.TryGetValue (field.Name, out old)) {
683 if (old.Item1.MemberType.IsStruct) {
685 // TODO: Clear fields for structs
689 old.Item2.SetValue (null, null);
695 fields[field.Name] = Tuple.Create (field.Spec, fi);
699 return (CompiledMethod) System.Delegate.CreateDelegate (typeof (CompiledMethod), mi);
704 /// A sentinel value used to indicate that no value was
705 /// was set by the compiled function. This is used to
706 /// differentiate between a function not returning a
709 internal static class QuitValue { }
711 internal Tuple<FieldSpec, FieldInfo> LookupField (string name)
713 Tuple<FieldSpec, FieldInfo> fi;
714 fields.TryGetValue (name, out fi);
718 static string Quote (string s)
720 if (s.IndexOf ('"') != -1)
721 s = s.Replace ("\"", "\\\"");
723 return "\"" + s + "\"";
726 public string GetUsing ()
731 StringBuilder sb = new StringBuilder ();
733 //foreach (object x in ns.using_alias_list)
734 // sb.AppendFormat ("using {0};\n", x);
736 foreach (var ue in ns.Usings) {
737 sb.AppendFormat ("using {0};", ue.ToString ());
738 sb.Append (Environment.NewLine);
741 return sb.ToString ();
744 internal ICollection<string> GetUsingList ()
746 var res = new List<string> ();
748 foreach (var ue in ns.Usings)
749 res.Add (ue.ToString ());
753 internal string [] GetVarNames ()
755 lock (evaluator_lock){
756 return new List<string> (fields.Keys).ToArray ();
760 public string GetVars ()
762 lock (evaluator_lock){
763 StringBuilder sb = new StringBuilder ();
765 foreach (var de in fields){
766 var fi = LookupField (de.Key);
769 value = fi.Item2.GetValue (null);
771 value = Quote ((string)value);
773 value = "<error reading value>";
776 sb.AppendFormat ("{0} {1} = {2}", fi.Item1.MemberType.GetSignatureForError (), de.Key, value);
780 return sb.ToString ();
785 /// Loads the given assembly and exposes the API to the user.
787 public void LoadAssembly (string file)
789 var loader = new DynamicLoader (importer, ctx);
790 var assembly = loader.LoadAssemblyFile (file);
791 if (assembly == null)
794 lock (evaluator_lock){
795 importer.ImportAssembly (assembly, module.GlobalRootNamespace);
800 /// Exposes the API of the given assembly to the Evaluator
802 public void ReferenceAssembly (Assembly a)
804 lock (evaluator_lock){
805 importer.ImportAssembly (a, module.GlobalRootNamespace);
812 /// A delegate that can be used to invoke the
813 /// compiled expression or statement.
816 /// Since the Compile methods will compile
817 /// statements and expressions into the same
818 /// delegate, you can tell if a value was returned
819 /// by checking whether the returned value is of type
823 public delegate void CompiledMethod (ref object retvalue);
826 /// The default base class for every interaction line
829 /// The expressions and statements behave as if they were
830 /// a static method of this class. The InteractiveBase class
831 /// contains a number of useful methods, but can be overwritten
832 /// by setting the InteractiveBaseType property in the Evaluator
834 public class InteractiveBase {
836 /// Determines where the standard output of methods in this class will go.
838 public static TextWriter Output = Console.Out;
841 /// Determines where the standard error of methods in this class will go.
843 public static TextWriter Error = Console.Error;
846 /// The primary prompt used for interactive use.
848 public static string Prompt = "csharp> ";
851 /// The secondary prompt used for interactive use (used when
852 /// an expression is incomplete).
854 public static string ContinuationPrompt = " > ";
857 /// Used to signal that the user has invoked the `quit' statement.
859 public static bool QuitRequested;
861 public static Evaluator Evaluator;
864 /// Shows all the variables defined so far.
866 static public void ShowVars ()
868 Output.Write (Evaluator.GetVars ());
873 /// Displays the using statements in effect at this point.
875 static public void ShowUsing ()
877 Output.Write (Evaluator.GetUsing ());
882 /// Times the execution of the given delegate
884 static public TimeSpan Time (Action a)
886 DateTime start = DateTime.Now;
888 return DateTime.Now - start;
892 /// Loads the assemblies from a package
895 /// Loads the assemblies from a package. This is equivalent
896 /// to passing the -pkg: command line flag to the C# compiler
897 /// on the command line.
899 static public void LoadPackage (string pkg)
902 Error.WriteLine ("Invalid package specified");
906 string pkgout = Driver.GetPackageFlags (pkg, null);
908 string [] xargs = pkgout.Trim (new Char [] {' ', '\n', '\r', '\t'}).
909 Split (new Char [] { ' ', '\t'});
911 foreach (string s in xargs){
912 if (s.StartsWith ("-r:") || s.StartsWith ("/r:") || s.StartsWith ("/reference:")){
913 string lib = s.Substring (s.IndexOf (':')+1);
915 Evaluator.LoadAssembly (lib);
922 /// Loads the assembly
925 /// Loads the specified assembly and makes its types
926 /// available to the evaluator. This is equivalent
927 /// to passing the -pkg: command line flag to the C#
928 /// compiler on the command line.
930 static public void LoadAssembly (string assembly)
932 Evaluator.LoadAssembly (assembly);
935 static public void print (string obj)
937 Output.WriteLine (obj);
940 static public void print (string fmt, params object [] args)
942 Output.WriteLine (fmt, args);
946 /// Returns a list of available static methods.
948 static public string help {
950 return "Static methods:\n" +
951 " Describe (object) - Describes the object's type\n" +
952 " LoadPackage (package); - Loads the given Package (like -pkg:FILE)\n" +
953 " LoadAssembly (assembly) - Loads the given assembly (like -r:ASSEMBLY)\n" +
954 " ShowVars (); - Shows defined local variables.\n" +
955 " ShowUsing (); - Show active using declarations.\n" +
956 " Prompt - The prompt used by the C# shell\n" +
957 " ContinuationPrompt - The prompt for partial input\n" +
958 " Time(() -> { }) - Times the specified code\n" +
959 " print (obj) - Shorthand for Console.WriteLine\n" +
960 " quit; - You'll never believe it - this quits the repl!\n" +
961 " help; - This help text\n";
966 /// Indicates to the read-eval-print-loop that the interaction should be finished.
968 static public object quit {
970 QuitRequested = true;
972 // To avoid print null at the exit
973 return typeof (Evaluator.QuitValue);
979 /// Describes an object or a type.
982 /// This method will show a textual representation
983 /// of the object's type. If the object is a
984 /// System.Type it renders the type directly,
985 /// otherwise it renders the type returned by
986 /// invoking GetType on the object.
988 static public string Describe (object x)
993 var type = x as Type ?? x.GetType ();
995 StringWriter sw = new StringWriter ();
996 new Outline (type, sw, true, false, false).OutlineType ();
997 return sw.ToString ();
1002 class HoistedEvaluatorVariable : HoistedVariable
1004 public HoistedEvaluatorVariable (Field field)
1005 : base (null, field)
1009 public override void EmitSymbolInfo ()
1013 protected override FieldExpr GetFieldExpression (EmitContext ec)
1015 return new FieldExpr (field, field.Location);
1020 /// A class used to assign values if the source expression is not void
1022 /// Used by the interactive shell to allow it to call this code to set
1023 /// the return value for an invocation.
1025 class OptionalAssign : SimpleAssign {
1026 public OptionalAssign (Expression t, Expression s, Location loc)
1031 protected override Expression DoResolve (ResolveContext ec)
1033 CloneContext cc = new CloneContext ();
1034 Expression clone = source.Clone (cc);
1036 clone = clone.Resolve (ec);
1041 // A useful feature for the REPL: if we can resolve the expression
1042 // as a type, Describe the type;
1044 if (ec.Module.Evaluator.DescribeTypeExpressions){
1045 var old_printer = ec.Report.SetPrinter (new SessionReportPrinter ());
1048 tclone = source.Clone (cc);
1049 tclone = tclone.Resolve (ec, ResolveFlags.Type);
1050 if (ec.Report.Errors > 0)
1053 ec.Report.SetPrinter (old_printer);
1056 if (tclone != null) {
1057 Arguments args = new Arguments (1);
1058 args.Add (new Argument (new TypeOf ((TypeExpr) clone, Location)));
1059 return new Invocation (new SimpleName ("Describe", Location), args).Resolve (ec);
1063 // This means its really a statement.
1064 if (clone.Type.Kind == MemberKind.Void || clone is DynamicInvocation || clone is Assign) {
1069 return base.DoResolve (ec);
1075 List<Action> undo_actions;
1081 public void AddTypeContainer (TypeContainer current_container, TypeContainer tc)
1083 if (current_container == tc){
1084 Console.Error.WriteLine ("Internal error: inserting container into itself");
1088 if (undo_actions == null)
1089 undo_actions = new List<Action> ();
1091 var existing = current_container.Types.FirstOrDefault (l => l.MemberName.Basename == tc.MemberName.Basename);
1092 if (existing != null) {
1093 current_container.RemoveTypeContainer (existing);
1094 existing.NamespaceEntry.SlaveDeclSpace.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;