📄 driver.cs
字号:
Environment.Exit (1); } response_file_list.Add (response_file, response_file); extra_args = LoadArgs (response_file); if (extra_args == null){ Report.Error (2011, "Unable to open response file: " + response_file); return false; } args = AddArgs (args, extra_args); continue; } if (parsing_options){ if (arg == "--"){ parsing_options = false; continue; } if (arg.StartsWith ("-")){ if (UnixParseOption (arg, ref args, ref i)) continue; // Try a -CSCOPTION string csc_opt = "/" + arg.Substring (1); if (CSCParseOption (csc_opt, ref args, ref i)) continue; Error_WrongOption (arg); return false; } else { if (arg [0] == '/'){ if (CSCParseOption (arg, ref args, ref i)) continue; // Need to skip `/home/test.cs' however /test.cs is considered as error if (arg.Length < 2 || arg.IndexOf ('/', 2) == -1) { Error_WrongOption (arg); return false; } } } } CompileFiles (arg, false); } ProcessFiles (); if (tokenize) return true; // // This will point to the NamespaceEntry of the last file that was parsed, and may // not be meaningful when resolving classes from other files. So, reset it to prevent // silent bugs. // RootContext.Tree.Types.NamespaceEntry = null; // // If we are an exe, require a source file for the entry point // if (RootContext.Target == Target.Exe || RootContext.Target == Target.WinExe || RootContext.Target == Target.Module){ if (first_source == null){ Report.Error (2008, "No files to compile were specified"); return false; } } // // If there is nothing to put in the assembly, and we are not a library // if (first_source == null && embedded_resources == null){ Report.Error (2008, "No files to compile were specified"); return false; } if (Report.Errors > 0) return false; if (parse_only) return true; // // Load Core Library for default compilation // if (RootContext.StdLib) references.Insert (0, "mscorlib"); if (load_default_config) DefineDefaultConfig (); if (Report.Errors > 0){ return false; } // // Load assemblies required // if (timestamps) ShowTime ("Loading references"); link_paths.Add (GetSystemDir ()); link_paths.Add (Directory.GetCurrentDirectory ()); LoadReferences (); if (timestamps) ShowTime (" References loaded"); if (Report.Errors > 0){ return false; } // // Quick hack // if (output_file == null){ int pos = first_source.LastIndexOf ('.'); if (pos > 0) output_file = first_source.Substring (0, pos) + RootContext.TargetExt; else output_file = first_source + RootContext.TargetExt; } if (!CodeGen.Init (output_file, output_file, want_debugging_support)) return false; if (RootContext.Target == Target.Module) { PropertyInfo module_only = typeof (AssemblyBuilder).GetProperty ("IsModuleOnly", BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic); if (module_only == null) { Report.RuntimeMissingSupport (Location.Null, "/target:module"); Environment.Exit (1); } MethodInfo set_method = module_only.GetSetMethod (true); set_method.Invoke (CodeGen.Assembly.Builder, BindingFlags.Default, null, new object[]{true}, null); } RootNamespace.Global.AddModuleReference (CodeGen.Module.Builder); if (modules.Count > 0) { MethodInfo adder_method = typeof (AssemblyBuilder).GetMethod ("AddModule", BindingFlags.Instance|BindingFlags.NonPublic); if (adder_method == null) { Report.RuntimeMissingSupport (Location.Null, "/addmodule"); Environment.Exit (1); } foreach (string module in modules) LoadModule (adder_method, module); } // // Before emitting, we need to get the core // types emitted from the user defined types // or from the system ones. // if (timestamps) ShowTime ("Initializing Core Types"); if (!RootContext.StdLib){ RootContext.ResolveCore (); if (Report.Errors > 0) return false; } TypeManager.InitCoreTypes (); if (timestamps) ShowTime (" Core Types done"); CodeGen.Module.ResolveAttributes (); // // The second pass of the compiler // if (timestamps) ShowTime ("Resolving tree"); RootContext.ResolveTree (); if (Report.Errors > 0) return false; if (timestamps) ShowTime ("Populate tree"); if (!RootContext.StdLib) RootContext.BootCorlib_PopulateCoreTypes (); RootContext.PopulateTypes (); TypeManager.InitCodeHelpers (); RootContext.DefineTypes (); if (RootContext.Documentation != null && !RootContext.Documentation.OutputDocComment ( output_file)) return false; // // Verify using aliases now // NamespaceEntry.VerifyAllUsing (); if (Report.Errors > 0){ return false; } CodeGen.Assembly.Resolve (); if (RootContext.VerifyClsCompliance) { if (CodeGen.Assembly.IsClsCompliant) { AttributeTester.VerifyModulesClsCompliance (); TypeManager.LoadAllImportedTypes (); } } if (Report.Errors > 0) return false; // // The code generator // if (timestamps) ShowTime ("Emitting code"); ShowTotalTime ("Total so far"); RootContext.EmitCode (); if (timestamps) ShowTime (" done"); if (Report.Errors > 0){ return false; } if (timestamps) ShowTime ("Closing types"); RootContext.CloseTypes (); PEFileKinds k = PEFileKinds.ConsoleApplication; switch (RootContext.Target) { case Target.Library: case Target.Module: k = PEFileKinds.Dll; break; case Target.Exe: k = PEFileKinds.ConsoleApplication; break; case Target.WinExe: k = PEFileKinds.WindowApplication; break; } if (RootContext.NeedsEntryPoint) { MethodInfo ep = RootContext.EntryPoint; if (ep == null) { if (RootContext.MainClass != null) { DeclSpace main_cont = RootContext.Tree.GetDecl (MemberName.FromDotted (RootContext.MainClass)); if (main_cont == null) { Report.Error (1555, "Could not find `{0}' specified for Main method", RootContext.MainClass); return false; } if (!(main_cont is ClassOrStruct)) { Report.Error (1556, "`{0}' specified for Main method must be a valid class or struct", RootContext.MainClass); return false; } Report.Error (1558, main_cont.Location, "`{0}' does not have a suitable static Main method", main_cont.GetSignatureForError ()); return false; } if (Report.Errors == 0) Report.Error (5001, "Program `{0}' does not contain a static `Main' method suitable for an entry point", output_file); return false; } CodeGen.Assembly.Builder.SetEntryPoint (ep, k); } else if (RootContext.MainClass != null) { Report.Error (2017, "Cannot specify -main if building a module or library"); } if (embedded_resources != null){ if (RootContext.Target == Target.Module) { Report.Error (1507, "Cannot link resource file when building a module"); return false; } embedded_resources.Emit (); } // // Add Win32 resources // CodeGen.Assembly.Builder.DefineVersionInfoResource (); if (win32ResourceFile != null) { try { CodeGen.Assembly.Builder.DefineUnmanagedResource (win32ResourceFile); } catch (ArgumentException) { Report.Warning (0, new Location (-1), "Cannot embed win32 resources on this runtime: try the Mono runtime instead."); } } if (win32IconFile != null) { MethodInfo define_icon = typeof (AssemblyBuilder).GetMethod ("DefineIconResource", BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic); if (define_icon == null) { Report.Warning (0, new Location (-1), "Cannot embed icon resource on this runtime: try the Mono runtime instead."); } define_icon.Invoke (CodeGen.Assembly.Builder, new object [] { win32IconFile }); } if (Report.Errors > 0) return false; CodeGen.Save (output_file); if (timestamps) { ShowTime ("Saved output"); ShowTotalTime ("Total"); } Timer.ShowTimers (); if (Report.ExpectedError != 0) { if (Report.Errors == 0) { Console.WriteLine ("Failed to report expected error " + Report.ExpectedError + ".\n" + "No other errors reported."); Environment.Exit (2); } else { Console.WriteLine ("Failed to report expected error " + Report.ExpectedError + ".\n" + "However, other errors were reported."); Environment.Exit (1); } return false; }#if DEBUGME Console.WriteLine ("Size of strings held: " + DeclSpace.length); Console.WriteLine ("Size of strings short: " + DeclSpace.small);#endif return (Report.Errors == 0); } } class Resources { interface IResource { void Emit (); string FileName { get; } } class EmbededResource : IResource { static MethodInfo embed_res; static EmbededResource () { Type[] argst = new Type [] { typeof (string), typeof (string), typeof (ResourceAttributes) }; embed_res = typeof (AssemblyBuilder).GetMethod ( "EmbedResourceFile", BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic, null, CallingConventions.Any, argst, null); if (embed_res == null) { Report.RuntimeMissingSupport (Location.Null, "Resource embedding"); } } readonly object[] args; public EmbededResource (string name, string file, bool isPrivate) { args = new object [3]; args [0] = name; args [1] = file; args [2] = isPrivate ? ResourceAttributes.Private : ResourceAttributes.Public; } public void Emit() { embed_res.Invoke (CodeGen.Assembly.Builder, args); } public string FileName { get { return (string)args [1]; } } } class LinkedResource : IResource { readonly string file; readonly string name; readonly ResourceAttributes attribute; public LinkedResource (string name, string file, bool isPrivate) { this.name = name; this.file = file; this.attribute = isPrivate ? ResourceAttributes.Private : ResourceAttributes.Public; } public void Emit () { CodeGen.Assembly.Builder.AddResourceFile (name, file, attribute); } public string FileName { get { return file; } } } IDictionary embedded_resources = new HybridDictionary (); public void Add (bool embeded, string file, string name) { Add (embeded, file, name, false); } public void Add (bool embeded, string file, string name, bool isPrivate) { if (embedded_resources.Contains (name)) { Report.Error (1508, "The resource identifier `{0}' has already been used in this assembly", name); return; } IResource r = embeded ? (IResource) new EmbededResource (name, file, isPrivate) : new LinkedResource (name, file, isPrivate); embedded_resources.Add (name, r); } public void Emit () { foreach (IResource r in embedded_resources.Values) { if (!File.Exists (r.FileName)) { Report.Error (1566, "Error reading resource file `{0}'", r.FileName); continue; } r.Emit (); } } } // // This is the only public entry point // public class CompilerCallableEntryPoint : MarshalByRefObject { public static bool InvokeCompiler (string [] args, TextWriter error) { Report.Stderr = error; try { return Driver.MainDriver (args) && Report.Errors == 0; } finally { Report.Stderr = Console.Error; Reset (); } } public static int[] AllWarningNumbers { get { return Report.AllWarnings; } } static void Reset () { Driver.Reset (); Location.Reset (); RootContext.Reset (); Report.Reset (); TypeManager.Reset (); TypeHandle.Reset (); RootNamespace.Reset (); NamespaceEntry.Reset (); CodeGen.Reset (); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -