2 // driver.cs: The compiler command line driver.
\r
4 // Author: Miguel de Icaza (miguel@gnu.org)
\r
6 // Licensed under the terms of the GNU GPL
\r
8 // (C) 2001 Ximian, Inc (http://www.ximian.com)
\r
14 using System.Reflection;
\r
15 using System.Reflection.Emit;
\r
16 using System.Collections;
\r
22 /// The compiler driver.
\r
27 // Assemblies references to be linked. Initialized with
\r
28 // mscorlib.dll here.
\r
29 ArrayList references;
\r
32 ArrayList link_paths;
\r
34 RootContext context;
\r
36 bool yacc_verbose = false;
\r
38 int error_count = 0;
\r
40 public int parse (Tree context, string input_file)
\r
42 CSharpParser parser;
\r
43 System.IO.Stream input;
\r
47 input = System.IO.File.OpenRead (input_file);
\r
52 parser = new CSharpParser (context, input_file, input);
\r
53 parser.yacc_verbose = yacc_verbose;
\r
55 errors = parser.parse ();
\r
56 } catch (Exception ex) {
\r
57 Console.WriteLine (ex);
\r
58 Console.WriteLine ("Compilation aborted");
\r
65 public void Usage ()
\r
68 "compiler [options] source-files\n\n" +
\r
69 "-v Verbose parsing\n"+
\r
70 "-o Specifies output file\n" +
\r
71 "-L Specifies path for loading assemblies\n" +
\r
72 "--nostdlib Does not load core libraries\n" +
\r
73 "-r References an assembly\n");
\r
77 public ITreeDump lookup_dumper (string name)
\r
80 return new Generator.TreeDump ();
\r
82 // if (name == "il")
\r
83 // return new MSIL.Generator ();
\r
88 public static void error (string msg)
\r
90 Console.WriteLine ("Error: " + msg);
\r
93 public static void notice (string msg)
\r
95 Console.WriteLine (msg);
\r
98 public static int Main(string[] args)
\r
100 Driver driver = new Driver (args);
\r
102 return driver.error_count;
\r
105 public int LoadAssembly (string assembly)
\r
109 foreach (string dir in link_paths){
\r
110 string full_path = dir + "/" + assembly;
\r
113 Console.WriteLine ("Loading: " + assembly);
\r
114 a = Assembly.Load (assembly);
\r
115 } catch (FileNotFoundException f) {
\r
116 error ("// File not found: " + full_path);
\r
117 error ("Log: " + f.FusionLog);
\r
119 } catch (BadImageFormatException) {
\r
120 error ("// Bad file format: " + full_path);
\r
122 } catch (FileLoadException f){
\r
123 error ("// File Load Exception: " + full_path);
\r
124 error ("Log: " + f.FusionLog);
\r
126 } catch (ArgumentNullException){
\r
127 error ("// Argument Null exception " + full_path);
\r
131 context.TypeManager.AddAssembly (a);
\r
137 // Loads all assemblies referenced on the command line
\r
139 public int LoadReferences ()
\r
143 foreach (string r in references){
\r
144 errors += LoadAssembly (r);
\r
151 // Parses the arguments, and drives the compilation
\r
154 // TODO: Mostly structured to debug the compiler
\r
155 // now, needs to be turned into a real driver soon.
\r
157 public Driver (string [] args)
\r
159 ITreeDump generator = null;
\r
161 string output_file = null;
\r
163 context = new RootContext ();
\r
164 references = new ArrayList ();
\r
165 link_paths = new ArrayList ();
\r
170 link_paths.Add ("file:///C:/WINNT/Microsoft.NET/Framework/v1.0.2914");
\r
172 for (i = 0; i < args.Length; i++){
\r
173 string arg = args [i];
\r
175 if (arg.StartsWith ("-")){
\r
176 if (arg.StartsWith ("-v")){
\r
177 yacc_verbose = true;
\r
181 if (arg.StartsWith ("-t")){
\r
182 generator = lookup_dumper (args [++i]);
\r
186 if (arg.StartsWith ("-z")){
\r
187 generator.ParseOptions (args [++i]);
\r
191 if (arg.StartsWith ("-o")){
\r
193 output_file = args [++i];
\r
194 } catch (Exception){
\r
195 error ("Could not write to `"+args [i]);
\r
202 if (arg.StartsWith ("-r")){
\r
203 references.Add (args [++i]);
\r
207 if (arg.StartsWith ("-L")){
\r
208 link_paths.Add (args [++i]);
\r
212 if (arg == "--nostdlib"){
\r
213 context.StdLib = false;
\r
221 if (!arg.EndsWith (".cs")){
\r
222 error ("Do not know how to compile " + arg);
\r
227 errors += parse (context.Tree, arg);
\r
231 // Load Core Library for default compilation
\r
233 if (context.StdLib)
\r
234 references.Insert (0, "mscorlib");
\r
237 error ("Parsing failed");
\r
240 notice ("Parsing successful");
\r
243 // Load assemblies required
\r
245 errors += LoadReferences ();
\r
248 error ("Could not load one or more assemblies");
\r
254 // Dumping the parsed tree.
\r
256 // This code generation interface is only here
\r
257 // for debugging the parser.
\r
259 if (generator != null){
\r
260 if (output_file == null){
\r
261 error ("Error: no output file specified");
\r
265 Stream output_stream = File.Create (output_file);
\r
266 StreamWriter output = new StreamWriter (output_stream);
\r
268 errors += generator.Dump (context.Tree, output);
\r
271 error ("Compilation failed");
\r
274 notice ("Compilation successful");
\r
281 error_count = errors;
\r
286 if (output_file == null)
\r
287 output_file = "a.exe";
\r
289 context.CodeGen = new CilCodeGen (output_file, output_file);
\r
292 // The second pass of the compiler
\r
294 context.ResolveTree ();
\r
295 context.PopulateTypes ();
\r
297 if (context.Report.Errors > 0){
\r
298 error ("Compilation failed");
\r
302 context.CloseTypes ();
\r
304 context.CodeGen.Save (output_file);
\r
306 notice ("Success");
\r