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
12 // Copyright 2011 Xamarin Inc
16 using System.Threading;
17 using System.Collections.Generic;
18 using System.Reflection;
19 using System.Reflection.Emit;
28 /// Evaluator: provides an API to evaluate C# statements and
29 /// expressions dynamically.
32 /// This class exposes static methods to evaluate expressions in the
35 /// To initialize the evaluator with a number of compiler
36 /// options call the Init(string[]args) method with a set of
37 /// command line options that the compiler recognizes.
39 /// To interrupt execution of a statement, you can invoke the
40 /// Evaluator.Interrupt method.
42 public class Evaluator {
45 // Parse silently, do not output any error messages
48 // Report errors during parse
51 // Auto-complete, means that the tokenizer will start producing
52 // GETCOMPLETIONS tokens when it reaches a certain point.
56 static object evaluator_lock = new object ();
57 static volatile bool invoking;
62 static Thread invoke_thread;
64 readonly Dictionary<string, Tuple<FieldSpec, FieldInfo>> fields;
70 readonly CompilerContext ctx;
71 readonly ModuleContainer module;
72 readonly ReflectionImporter importer;
73 readonly CompilationSourceFile source_file;
75 public Evaluator (CompilerContext ctx)
79 module = new ModuleContainer (ctx);
80 module.Evaluator = this;
82 source_file = new CompilationSourceFile (module, null);
83 module.AddTypeContainer (source_file);
85 startup_files = ctx.SourceFiles.Count;
87 // FIXME: Importer needs this assembly for internalsvisibleto
88 module.SetDeclaringAssembly (new AssemblyDefinitionDynamic (module, "evaluator"));
89 importer = new ReflectionImporter (module, ctx.BuiltinTypes);
91 InteractiveBaseClass = typeof (InteractiveBase);
92 fields = new Dictionary<string, Tuple<FieldSpec, FieldInfo>> ();
97 var loader = new DynamicLoader (importer, ctx);
99 CompilerCallableEntryPoint.Reset ();
100 RootContext.ToplevelTypes = module;
102 //var startup_files = new List<string> ();
103 //foreach (CompilationUnit file in Location.SourceFiles)
104 // startup_files.Add (file.Path);
106 loader.LoadReferences (module);
107 ctx.BuiltinTypes.CheckDefinitions (module);
108 module.InitializePredefinedTypes ();
113 void ParseStartupFiles ()
115 Driver d = new Driver (ctx);
117 Location.Initialize (ctx.SourceFiles);
119 var parser_session = new ParserSession ();
120 for (int i = 0; i < startup_files; ++i) {
121 var sf = ctx.SourceFiles [i];
122 d.Parse (sf, module, parser_session, ctx.Report);
128 CompilerCallableEntryPoint.PartialReset ();
131 Location.Initialize (ctx.SourceFiles);
135 /// If true, turns type expressions into valid expressions
136 /// and calls the describe method on it
138 public bool DescribeTypeExpressions;
141 /// Whether the evaluator will use terse syntax, and the semicolons at the end are optional
143 public bool Terse = true;
146 /// The base class for the classes that host the user generated code
150 /// This is the base class that will host the code
151 /// executed by the Evaluator. By default
152 /// this is the Mono.CSharp.InteractiveBase class
153 /// which is useful for interactive use.
155 /// By changing this property you can control the
156 /// base class and the static members that are
157 /// available to your evaluated code.
159 public Type InteractiveBaseClass {
166 if (value != null && typeof (InteractiveBase).IsAssignableFrom (value))
167 InteractiveBase.Evaluator = this;
172 /// Interrupts the evaluation of an expression executing in Evaluate.
175 /// Use this method to interrupt long-running invocations.
177 public void Interrupt ()
179 if (!inited || !invoking)
182 if (invoke_thread != null)
183 invoke_thread.Abort ();
187 /// Compiles the input string and returns a delegate that represents the compiled code.
191 /// Compiles the input string as a C# expression or
192 /// statement, unlike the Evaluate method, the
193 /// resulting delegate can be invoked multiple times
194 /// without incurring in the compilation overhead.
196 /// If the return value of this function is null,
197 /// this indicates that the parsing was complete.
198 /// If the return value is a string it indicates
199 /// that the input string was partial and that the
200 /// invoking code should provide more code before
201 /// the code can be successfully compiled.
203 /// If you know that you will always get full expressions or
204 /// statements and do not care about partial input, you can use
205 /// the other Compile overload.
207 /// On success, in addition to returning null, the
208 /// compiled parameter will be set to the delegate
209 /// that can be invoked to execute the code.
212 public string Compile (string input, out CompiledMethod compiled)
214 if (input == null || input.Length == 0){
219 lock (evaluator_lock){
222 ParseStartupFiles ();
224 ctx.Report.Printer.Reset ();
228 CSharpParser parser = ParseString (ParseMode.Silent, input, out partial_input);
229 if (parser == null && Terse && partial_input){
231 parser = ParseString (ParseMode.Silent, input + ";", out ignore);
238 ParseString (ParseMode.ReportErrors, input, out partial_input);
242 Class parser_result = parser.InteractiveResult;
243 compiled = CompileBlock (parser_result, parser.undo, ctx.Report);
249 /// Compiles the input string and returns a delegate that represents the compiled code.
253 /// Compiles the input string as a C# expression or
254 /// statement, unlike the Evaluate method, the
255 /// resulting delegate can be invoked multiple times
256 /// without incurring in the compilation overhead.
258 /// This method can only deal with fully formed input
259 /// strings and does not provide a completion mechanism.
260 /// If you must deal with partial input (for example for
261 /// interactive use) use the other overload.
263 /// On success, a delegate is returned that can be used
264 /// to invoke the method.
267 public CompiledMethod Compile (string input)
269 CompiledMethod compiled;
271 // Ignore partial inputs
272 if (Compile (input, out compiled) != null){
273 // Error, the input was partial.
277 // Either null (on error) or the compiled method.
282 /// Evaluates and expression or statement and returns any result values.
285 /// Evaluates the input string as a C# expression or
286 /// statement. If the input string is an expression
287 /// the result will be stored in the result variable
288 /// and the result_set variable will be set to true.
290 /// It is necessary to use the result/result_set
291 /// pair to identify when a result was set (for
292 /// example, execution of user-provided input can be
293 /// an expression, a statement or others, and
294 /// result_set would only be set if the input was an
297 /// If the return value of this function is null,
298 /// this indicates that the parsing was complete.
299 /// If the return value is a string, it indicates
300 /// that the input is partial and that the user
301 /// should provide an updated string.
303 public string Evaluate (string input, out object result, out bool result_set)
305 CompiledMethod compiled;
310 input = Compile (input, out compiled);
314 if (compiled == null)
318 // The code execution does not need to keep the compiler lock
320 object retval = typeof (QuitValue);
323 invoke_thread = System.Threading.Thread.CurrentThread;
325 compiled (ref retval);
326 } catch (ThreadAbortException e){
327 Thread.ResetAbort ();
328 Console.WriteLine ("Interrupted!\n{0}", e);
334 // We use a reference to a compiler type, in this case
335 // Driver as a flag to indicate that this was a statement
337 if (!ReferenceEquals (retval, typeof (QuitValue))) {
345 public string [] GetCompletions (string input, out string prefix)
348 if (input == null || input.Length == 0)
351 lock (evaluator_lock){
356 CSharpParser parser = ParseString (ParseMode.GetCompletions, input, out partial_input);
361 Class parser_result = parser.InteractiveResult;
364 var access = AssemblyBuilderAccess.RunAndCollect;
366 var access = AssemblyBuilderAccess.Run;
368 var a = new AssemblyDefinitionDynamic (module, "completions");
369 a.Create (AppDomain.CurrentDomain, access);
370 module.SetDeclaringAssembly (a);
372 // Need to setup MemberCache
373 parser_result.CreateContainer ();
375 var method = parser_result.Members[0] as Method;
376 BlockContext bc = new BlockContext (method, method.Block, ctx.BuiltinTypes.Void);
379 method.Block.Resolve (null, bc, method);
380 } catch (CompletionResult cr) {
381 prefix = cr.BaseText;
389 /// Executes the given expression or statement.
392 /// Executes the provided statement, returns true
393 /// on success, false on parsing errors. Exceptions
394 /// might be thrown by the called code.
396 public bool Run (string statement)
401 return Evaluate (statement, out result, out result_set) == null;
405 /// Evaluates and expression or statement and returns the result.
408 /// Evaluates the input string as a C# expression or
409 /// statement and returns the value.
411 /// This method will throw an exception if there is a syntax error,
412 /// of if the provided input is not an expression but a statement.
414 public object Evaluate (string input)
419 string r = Evaluate (input, out result, out result_set);
422 throw new ArgumentException ("Syntax error on input: partial input");
424 if (result_set == false)
425 throw new ArgumentException ("The expression did not set a result");
432 StatementOrExpression,
438 // Deambiguates the input string to determine if we
439 // want to process a statement or if we want to
440 // process a compilation unit.
442 // This is done using a top-down predictive parser,
443 // since the yacc/jay parser can not deambiguage this
444 // without more than one lookahead token. There are very
447 InputKind ToplevelOrStatement (SeekableStreamReader seekable)
449 Tokenizer tokenizer = new Tokenizer (seekable, source_file, new ParserSession ());
451 int t = tokenizer.token ();
454 return InputKind.EOF;
456 // These are toplevels
458 case Token.OPEN_BRACKET:
462 case Token.INTERFACE:
464 case Token.NAMESPACE:
466 case Token.PROTECTED:
471 return InputKind.CompilationUnit;
473 // Definitely expression
490 return InputKind.StatementOrExpression;
492 // These need deambiguation help
494 t = tokenizer.token ();
496 return InputKind.EOF;
498 if (t == Token.IDENTIFIER)
499 return InputKind.CompilationUnit;
500 return InputKind.StatementOrExpression;
503 // Distinguish between:
504 // delegate opt_anonymous_method_signature block
507 t = tokenizer.token ();
509 return InputKind.EOF;
510 if (t == Token.OPEN_PARENS || t == Token.OPEN_BRACE)
511 return InputKind.StatementOrExpression;
512 return InputKind.CompilationUnit;
514 // Distinguih between:
516 // unsafe as modifier of a type declaration
518 t = tokenizer.token ();
520 return InputKind.EOF;
521 if (t == Token.OPEN_PARENS)
522 return InputKind.StatementOrExpression;
523 return InputKind.CompilationUnit;
525 // These are errors: we list explicitly what we had
526 // from the grammar, ERROR and then everything else
531 return InputKind.Error;
533 // This catches everything else allowed by
534 // expressions. We could add one-by-one use cases
537 return InputKind.StatementOrExpression;
542 // Parses the string @input and returns a CSharpParser if succeeful.
544 // if @silent is set to true then no errors are
545 // reported to the user. This is used to do various calls to the
546 // parser and check if the expression is parsable.
548 // @partial_input: if @silent is true, then it returns whether the
549 // parsed expression was partial, and more data is needed
551 CSharpParser ParseString (ParseMode mode, string input, out bool partial_input)
553 partial_input = false;
556 var enc = ctx.Settings.Encoding;
557 var s = new MemoryStream (enc.GetBytes (input));
558 SeekableStreamReader seekable = new SeekableStreamReader (s, enc);
560 InputKind kind = ToplevelOrStatement (seekable);
561 if (kind == InputKind.Error){
562 if (mode == ParseMode.ReportErrors)
563 ctx.Report.Error (-25, "Detection Parsing Error");
564 partial_input = false;
568 if (kind == InputKind.EOF){
569 if (mode == ParseMode.ReportErrors)
570 Console.Error.WriteLine ("Internal error: EOF condition should have been detected in a previous call with silent=true");
571 partial_input = true;
575 seekable.Position = 0;
577 source_file.DeclarationFound = false;
578 CSharpParser parser = new CSharpParser (seekable, source_file, new ParserSession ());
580 if (kind == InputKind.StatementOrExpression){
581 parser.Lexer.putback_char = Tokenizer.EvalStatementParserCharacter;
582 ctx.Settings.StatementMode = true;
584 parser.Lexer.putback_char = Tokenizer.EvalCompilationUnitParserCharacter;
585 ctx.Settings.StatementMode = false;
588 if (mode == ParseMode.GetCompletions)
589 parser.Lexer.CompleteOnEOF = true;
591 ReportPrinter old_printer = null;
592 if ((mode == ParseMode.Silent || mode == ParseMode.GetCompletions))
593 old_printer = ctx.Report.SetPrinter (new StreamReportPrinter (TextWriter.Null));
598 if (ctx.Report.Errors != 0){
599 if (mode != ParseMode.ReportErrors && parser.UnexpectedEOF)
600 partial_input = true;
602 if (parser.undo != null)
603 parser.undo.ExecuteUndo ();
608 if (old_printer != null)
609 ctx.Report.SetPrinter (old_printer);
614 CompiledMethod CompileBlock (Class host, Undo undo, Report Report)
617 throw new NotSupportedException ();
619 string current_debug_name = "eval-" + count + ".dll";
622 AssemblyDefinitionDynamic assembly;
623 AssemblyBuilderAccess access;
625 if (Environment.GetEnvironmentVariable ("SAVE") != null) {
626 access = AssemblyBuilderAccess.RunAndSave;
627 assembly = new AssemblyDefinitionDynamic (module, current_debug_name, current_debug_name);
628 assembly.Importer = importer;
631 access = AssemblyBuilderAccess.RunAndCollect;
633 access = AssemblyBuilderAccess.Run;
635 assembly = new AssemblyDefinitionDynamic (module, current_debug_name);
638 assembly.Create (AppDomain.CurrentDomain, access);
640 Method expression_method;
642 var base_class_imported = importer.ImportType (base_class);
643 var baseclass_list = new List<FullNamedExpression> (1) {
644 new TypeExpression (base_class_imported, host.Location)
647 host.AddBasesForPart (baseclass_list);
649 host.CreateContainer ();
650 host.DefineContainer ();
653 expression_method = (Method) host.Members[0];
655 expression_method = null;
658 module.CreateContainer ();
660 // Disable module and source file re-definition checks
661 module.EnableRedefinition ();
662 source_file.EnableRedefinition ();
666 if (Report.Errors != 0){
674 host.EmitContainer ();
677 module.EmitContainer ();
678 if (Report.Errors != 0){
684 module.CloseContainer ();
686 host.CloseContainer ();
688 if (access == AssemblyBuilderAccess.RunAndSave)
695 // Unlike Mono, .NET requires that the MethodInfo is fetched, it cant
696 // work from MethodBuilders. Retarded, I know.
698 var tt = assembly.Builder.GetType (host.TypeBuilder.Name);
699 var mi = tt.GetMethod (expression_method.MemberName.Name);
702 // We need to then go from FieldBuilder to FieldInfo
703 // or reflection gets confused (it basically gets confused, and variables override each
706 foreach (var member in host.Members) {
707 var field = member as Field;
711 var fi = tt.GetField (field.Name);
713 Tuple<FieldSpec, FieldInfo> old;
715 // If a previous value was set, nullify it, so that we do
717 if (fields.TryGetValue (field.Name, out old)) {
718 if (old.Item1.MemberType.IsStruct) {
720 // TODO: Clear fields for structs
724 old.Item2.SetValue (null, null);
730 fields[field.Name] = Tuple.Create (field.Spec, fi);
733 return (CompiledMethod) System.Delegate.CreateDelegate (typeof (CompiledMethod), mi);
738 /// A sentinel value used to indicate that no value was
739 /// was set by the compiled function. This is used to
740 /// differentiate between a function not returning a
743 internal static class QuitValue { }
745 internal Tuple<FieldSpec, FieldInfo> LookupField (string name)
747 Tuple<FieldSpec, FieldInfo> fi;
748 fields.TryGetValue (name, out fi);
752 static string Quote (string s)
754 if (s.IndexOf ('"') != -1)
755 s = s.Replace ("\"", "\\\"");
757 return "\"" + s + "\"";
760 public string GetUsing ()
762 StringBuilder sb = new StringBuilder ();
764 //foreach (object x in ns.using_alias_list)
765 // sb.AppendFormat ("using {0};\n", x);
767 foreach (var ue in source_file.Usings) {
768 sb.AppendFormat ("using {0};", ue.ToString ());
769 sb.Append (Environment.NewLine);
772 return sb.ToString ();
775 internal List<string> GetUsingList ()
777 var res = new List<string> ();
779 foreach (var ue in source_file.Usings) {
780 if (ue.Alias != null || ue.ResolvedExpression == null)
783 res.Add (ue.NamespaceExpression.Name);
789 internal string [] GetVarNames ()
791 lock (evaluator_lock){
792 return new List<string> (fields.Keys).ToArray ();
796 public string GetVars ()
798 lock (evaluator_lock){
799 StringBuilder sb = new StringBuilder ();
801 foreach (var de in fields){
802 var fi = LookupField (de.Key);
805 value = fi.Item2.GetValue (null);
807 value = Quote ((string)value);
809 value = "<error reading value>";
812 sb.AppendFormat ("{0} {1} = {2}", fi.Item1.MemberType.GetSignatureForError (), de.Key, value);
816 return sb.ToString ();
821 /// Loads the given assembly and exposes the API to the user.
823 public void LoadAssembly (string file)
825 var loader = new DynamicLoader (importer, ctx);
826 var assembly = loader.LoadAssemblyFile (file, false);
827 if (assembly == null)
830 lock (evaluator_lock){
831 importer.ImportAssembly (assembly, module.GlobalRootNamespace);
836 /// Exposes the API of the given assembly to the Evaluator
838 public void ReferenceAssembly (Assembly a)
840 lock (evaluator_lock){
841 importer.ImportAssembly (a, module.GlobalRootNamespace);
848 /// A delegate that can be used to invoke the
849 /// compiled expression or statement.
852 /// Since the Compile methods will compile
853 /// statements and expressions into the same
854 /// delegate, you can tell if a value was returned
855 /// by checking whether the returned value is of type
859 public delegate void CompiledMethod (ref object retvalue);
862 /// The default base class for every interaction line
865 /// The expressions and statements behave as if they were
866 /// a static method of this class. The InteractiveBase class
867 /// contains a number of useful methods, but can be overwritten
868 /// by setting the InteractiveBaseType property in the Evaluator
870 public class InteractiveBase {
872 /// Determines where the standard output of methods in this class will go.
874 public static TextWriter Output = Console.Out;
877 /// Determines where the standard error of methods in this class will go.
879 public static TextWriter Error = Console.Error;
882 /// The primary prompt used for interactive use.
884 public static string Prompt = "csharp> ";
887 /// The secondary prompt used for interactive use (used when
888 /// an expression is incomplete).
890 public static string ContinuationPrompt = " > ";
893 /// Used to signal that the user has invoked the `quit' statement.
895 public static bool QuitRequested;
897 public static Evaluator Evaluator;
900 /// Shows all the variables defined so far.
902 static public void ShowVars ()
904 Output.Write (Evaluator.GetVars ());
909 /// Displays the using statements in effect at this point.
911 static public void ShowUsing ()
913 Output.Write (Evaluator.GetUsing ());
918 /// Times the execution of the given delegate
920 static public TimeSpan Time (Action a)
922 DateTime start = DateTime.Now;
924 return DateTime.Now - start;
928 /// Loads the assemblies from a package
931 /// Loads the assemblies from a package. This is equivalent
932 /// to passing the -pkg: command line flag to the C# compiler
933 /// on the command line.
935 static public void LoadPackage (string pkg)
938 Error.WriteLine ("Invalid package specified");
942 string pkgout = Driver.GetPackageFlags (pkg, null);
944 string [] xargs = pkgout.Trim (new Char [] {' ', '\n', '\r', '\t'}).
945 Split (new Char [] { ' ', '\t'});
947 foreach (string s in xargs){
948 if (s.StartsWith ("-r:") || s.StartsWith ("/r:") || s.StartsWith ("/reference:")){
949 string lib = s.Substring (s.IndexOf (':')+1);
951 Evaluator.LoadAssembly (lib);
958 /// Loads the assembly
961 /// Loads the specified assembly and makes its types
962 /// available to the evaluator. This is equivalent
963 /// to passing the -pkg: command line flag to the C#
964 /// compiler on the command line.
966 static public void LoadAssembly (string assembly)
968 Evaluator.LoadAssembly (assembly);
971 static public void print (object obj)
973 Output.WriteLine (obj);
976 static public void print (string fmt, params object [] args)
978 Output.WriteLine (fmt, args);
982 /// Returns a list of available static methods.
984 static public string help {
986 return "Static methods:\n" +
988 " Describe (object); - Describes the object's type\n" +
990 " LoadPackage (package); - Loads the given Package (like -pkg:FILE)\n" +
991 " LoadAssembly (assembly); - Loads the given assembly (like -r:ASSEMBLY)\n" +
992 " ShowVars (); - Shows defined local variables.\n" +
993 " ShowUsing (); - Show active using declarations.\n" +
994 " Prompt - The prompt used by the C# shell\n" +
995 " ContinuationPrompt - The prompt for partial input\n" +
996 " Time (() => { }); - Times the specified code\n" +
997 " print (obj); - Shorthand for Console.WriteLine\n" +
998 " quit; - You'll never believe it - this quits the repl!\n" +
999 " help; - This help text\n";
1004 /// Indicates to the read-eval-print-loop that the interaction should be finished.
1006 static public object quit {
1008 QuitRequested = true;
1010 // To avoid print null at the exit
1011 return typeof (Evaluator.QuitValue);
1016 /// Same as quit - useful in script scenerios
1018 static public void Quit () {
1019 QuitRequested = true;
1024 /// Describes an object or a type.
1027 /// This method will show a textual representation
1028 /// of the object's type. If the object is a
1029 /// System.Type it renders the type directly,
1030 /// otherwise it renders the type returned by
1031 /// invoking GetType on the object.
1033 static public string Describe (object x)
1038 var type = x as Type ?? x.GetType ();
1040 StringWriter sw = new StringWriter ();
1041 new Outline (type, sw, true, false, false).OutlineType ();
1042 return sw.ToString ();
1047 class HoistedEvaluatorVariable : HoistedVariable
1049 public HoistedEvaluatorVariable (Field field)
1050 : base (null, field)
1054 protected override FieldExpr GetFieldExpression (EmitContext ec)
1056 return new FieldExpr (field, field.Location);
1061 /// A class used to assign values if the source expression is not void
1063 /// Used by the interactive shell to allow it to call this code to set
1064 /// the return value for an invocation.
1066 class OptionalAssign : SimpleAssign {
1067 public OptionalAssign (Expression t, Expression s, Location loc)
1072 protected override Expression DoResolve (ResolveContext ec)
1074 Expression clone = source.Clone (new CloneContext ());
1076 clone = clone.Resolve (ec);
1081 // A useful feature for the REPL: if we can resolve the expression
1082 // as a type, Describe the type;
1084 if (ec.Module.Evaluator.DescribeTypeExpressions){
1085 var old_printer = ec.Report.SetPrinter (new SessionReportPrinter ());
1088 // Note: clone context cannot be shared otherwise block mapping would leak
1089 tclone = source.Clone (new CloneContext ());
1090 tclone = tclone.Resolve (ec, ResolveFlags.Type);
1091 if (ec.Report.Errors > 0)
1094 ec.Report.SetPrinter (old_printer);
1097 if (tclone is TypeExpr) {
1098 Arguments args = new Arguments (1);
1099 args.Add (new Argument (new TypeOf ((TypeExpr) clone, Location)));
1100 return new Invocation (new SimpleName ("Describe", Location), args).Resolve (ec);
1104 // This means its really a statement.
1105 if (clone.Type.Kind == MemberKind.Void || clone is DynamicInvocation || clone is Assign) {
1110 return base.DoResolve (ec);
1116 List<Action> undo_actions;
1122 public void AddTypeContainer (TypeContainer current_container, TypeDefinition tc)
1124 if (current_container == tc){
1125 Console.Error.WriteLine ("Internal error: inserting container into itself");
1129 if (undo_actions == null)
1130 undo_actions = new List<Action> ();
1132 var existing = current_container.Containers.FirstOrDefault (l => l.Basename == tc.Basename);
1133 if (existing != null) {
1134 current_container.RemoveContainer (existing);
1135 undo_actions.Add (() => current_container.AddTypeContainer (existing));
1138 undo_actions.Add (() => current_container.RemoveContainer (tc));
1141 public void ExecuteUndo ()
1143 if (undo_actions == null)
1146 foreach (var p in undo_actions){
1150 undo_actions = null;