Welcome to mirror list, hosted at ThFree Co, Russian Federation.

driver.cs « mcs « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fc9748e024fda0e63ff4d460abbbbb1c1f7a40e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//
// driver.cs: The compiler command line driver.
//
// Author: Miguel de Icaza (miguel@gnu.org)
//
// Licensed under the terms of the GNU GPL
//
// (C) 2001 Ximian, Inc (http://www.ximian.com)
//

namespace Mono.CSharp
{
	using System;
	using System.Reflection;
	using System.Reflection.Emit;
	using System.Collections;
	using System.IO;
	using Mono.Languages;

	/// <summary>
	///    The compiler driver.
	/// </summary>
	public class Driver
	{
		enum Target {
			Library, Exe, Module, WinExe
		};
		
		//
		// Assemblies references to be linked.   Initialized with
		// mscorlib.dll here.
		static ArrayList references;

		// Lookup paths
		static ArrayList link_paths;

		static bool yacc_verbose = false;

		static int error_count = 0;

		static string first_source;

		static Target target = Target.Exe;
		static string target_ext = ".exe";

		static bool parse_only = false;
		
		static int parse (string input_file)
		{
			CSharpParser parser;
			System.IO.Stream input;
			int errors;

			try {
				input = System.IO.File.OpenRead (input_file);
			} catch {
				Report.Error (2001, "Source file '" + input_file + "' could not be opened");
				return 1;
			}

			parser = new CSharpParser (input_file, input);
			parser.yacc_verbose = yacc_verbose;
			try {
				errors = parser.parse ();
			} catch (Exception ex) {
				Console.WriteLine (ex);
				Console.WriteLine ("Compilation aborted");
				return 1;
			}
			
			return errors;
		}
		
		static void Usage (bool is_error)
		{
			Console.WriteLine (
				"Mono C# compiler, (C) 2001 Ximian, Inc.\n" +
				"mcs [options] source-files\n" +
				"   --about         About the Mono C# compiler\n" +
				"   --checked       Set default context to checked\n" +
				"   --fatal         Makes errors fatal\n" +
				"   -L PATH         Adds PATH to the assembly link path\n" +
				"   --nostdlib      Does not load core libraries\n" +
				"   --nowarn XXX    Ignores warning number XXX\n" +
				"   -o FNAME        Specifies output file\n" +
				"   --optimize      Optimizes\n" +
				"   --parse         Only parses the source file\n" +
				"   --probe X L     Probes for the source to generate code X on line L\n" +
				"   --target KIND   Specifies the target (KIND is one of: exe, winexe, " +
				                    "library, module)\n" +
				"   --unsafe        Allows unsafe code\n" +
				"   --werror        Treat warnings as errors\n" +
				"   --wlevel LEVEL  Sets warning level (the highest is 4, the default)\n" +
				"   -r              References an assembly\n" +
				"   -v              Verbose parsing (for debugging the parser)\n");
			if (is_error)
				error_count++;
		}

		static void About ()
		{
			Console.WriteLine (
				"The Mono C# compiler is (C) 2001 Ximian, Inc.\n\n" +
				"The compiler source code is released under the terms of the GNU GPL\n\n" +

				"For more information on Mono, visit the project Web site\n" +
				"   http://www.go-mono.com\n\n" +

				"The compiler was written by Miguel de Icaza and Ravi Pratap");
		}
		
		static void error (string msg)
		{
			Console.WriteLine ("Error: " + msg);
		}

		static void notice (string msg)
		{
			Console.WriteLine (msg);
		}
		
		public static int Main (string[] args)
		{
			MainDriver (args);
			
			return error_count;
		}

		static public int LoadAssembly (string assembly)
		{
			Assembly a;
			string total_log = "";

			try {
				a = Assembly.Load (assembly);
				RootContext.TypeManager.AddAssembly (a);
				return 0;
			} catch (FileNotFoundException){
				foreach (string dir in link_paths){
					string full_path = dir + "/" + assembly + ".dll";

					try {
						a = Assembly.LoadFrom (full_path);
						RootContext.TypeManager.AddAssembly (a);
						return 0;
					} catch (FileNotFoundException ff) {
						total_log += ff.FusionLog;
						continue;
					}
				}
			} catch (BadImageFormatException f) {
				error ("// Bad file format while loading assembly");
				error ("Log: " + f.FusionLog);
				return 1;
			} catch (FileLoadException f){
				error ("// File Load Exception: ");
				error ("Log: " + f.FusionLog);
				return 1;
			} catch (ArgumentNullException){
				error ("// Argument Null exception ");
				return 1;
			}
			
			Report.Error (6, "Can not find assembly `" + assembly + "'" );
			Console.WriteLine ("Log: \n" + total_log);

			return 0;
		}

		/// <summary>
		///   Loads all assemblies referenced on the command line
		/// </summary>
		static public int LoadReferences ()
		{
			int errors = 0;
			
			foreach (string r in references){
				errors += LoadAssembly (r);
			}

			return errors;
		}

		/// <summary>
		///    Parses the arguments, and drives the compilation
		///    process.
		/// </summary>
		///
		/// <remarks>
		///    TODO: Mostly structured to debug the compiler
		///    now, needs to be turned into a real driver soon.
		/// </remarks>
		static void MainDriver (string [] args)
		{
			int errors = 0, i;
			string output_file = null;

			references = new ArrayList ();
			link_paths = new ArrayList ();

			//
			// Setup defaults
			//
			// This is not required because Assembly.Load knows about this
			// path.
			//
			link_paths.Add ("file:///C:/WINNT/Microsoft.NET/Framework/v1.0.2914");

			int argc = args.Length;
			for (i = 0; i < argc; i++){
				string arg = args [i];

				if (arg.StartsWith ("-")){
					switch (arg){
					case "-v":
						yacc_verbose = true;
						continue;

					case "--parse":
						parse_only = true;
						continue;

					case "--main": case "-m":
						if ((i + 1) >= argc){
							Usage (true);
							return;
						}
						RootContext.MainClass = args [++i];
						continue;

					case "--unsafe":
						RootContext.Unsafe = true;
						break;
						
					case "--optimize":
						RootContext.Optimize = true;
						continue;

					case "--help":
						Usage (false);
						return;
						
					case "--probe": {
						int code, line;
						
						code = Int32.Parse (args [++i], 0);
						line = Int32.Parse (args [++i], 0);
						Report.SetProbe (code, line);
						continue;
					}

					case "-o": case "--output":
						if ((i + 1) >= argc){
							Usage (true);
							return;
						}
						output_file = args [++i];
						continue;
						
					case "--checked":
						RootContext.Checked = true;
						continue;
						
					case "--target":
						if ((i + 1) >= argc){
							Usage (true);
							return;
						}

						string type = args [++i];
						switch (type){
						case "library":
							target = Target.Library;
							target_ext = ".dll";
							break;
							
						case "exe":
							target = Target.Exe;
							break;
							
						case "winexe":
							target = Target.WinExe;
							break;
							
						case "module":
							target = Target.Module;
							target_ext = ".dll";
							break;
						}
						continue;

					case "-r":
						if ((i + 1) >= argc){
							Usage (true);
							return;
						}
						
						references.Add (args [++i]);
						continue;
						
					case "-L":
						if ((i + 1) >= argc){
							Usage (true);
							return;
						}
						link_paths.Add (args [++i]);
						continue;
						
					case "--nostdlib":
						RootContext.StdLib = false;
						continue;
						
					case "--fatal":
						Report.Fatal = true;
						continue;

					case "--werror":
						Report.WarningsAreErrors = true;
						continue;

					case "--nowarn":
						if ((i + 1) >= argc){
							Usage (true);
							return;
						}
						int warn;
						
						try {
							warn = Int32.Parse (args [++i]);
						} catch {
							Usage (true);
							return;
						}
						Report.SetIgnoreWarning (warn);
						continue;

					case "--wlevel":
						if ((i + 1) >= argc){
							Usage (true);
							error_count++;
							return;
						}
						int level;
						
						try {
							level = Int32.Parse (args [++i]);
						} catch {
							Usage (true);
							return;
						}
						if (level < 0 || level > 4){
							Report.Error (1900, "Warning level must be 0 to 4");
							return;
						} else
							RootContext.WarningLevel = level;
						continue;
						
					case "--about":
						About ();
						return;
						
					default:
						Usage (true);
						return;
					}
				}

				if (first_source == null)
					first_source = arg;

				string [] files = Directory.GetFiles (".", arg);
				foreach (string f in files){
					if (!f.ToLower ().EndsWith (".cs")){
						error ("Do not know how to compile " + arg);
						errors++;
						continue;
					}
					errors += parse (f);
				}
			}

			if (first_source == null){
				Report.Error (2008, "No files to compile were specified");
				return;
			}

			if (Report.Errors > 0)
				return;
			
			if (parse_only)
				return;
			
			//
			// Load Core Library for default compilation
			//
			if (RootContext.StdLib){
				references.Insert (0, "mscorlib");
				references.Insert (1, "System");
			}

			if (errors > 0){
				error ("Parsing failed");
				return;
			}

			//
			// Load assemblies required
			//
			errors += LoadReferences ();

			if (errors > 0){
				error ("Could not load one or more assemblies");
				return;
			}

			error_count = errors;

			//
			// Quick hack
			//
			if (output_file == null){
				int pos = first_source.LastIndexOf (".");

				output_file = first_source.Substring (0, pos) + target_ext;
			}

			RootContext.CodeGen = new CodeGen (output_file, output_file);

			//
			// Before emitting, we need to get the core
			// types emitted from the user defined types
			// or from the system ones.
			//
			RootContext.TypeManager.InitCoreTypes ();

			RootContext.TypeManager.AddModule (RootContext.CodeGen.ModuleBuilder);
			
			//
			// The second pass of the compiler
			//
			RootContext.ResolveTree ();
			RootContext.PopulateTypes ();
			
			if (Report.Errors > 0){
				error ("Compilation failed");
				return;
			}
			
			//
			// The code generator
			//
			RootContext.EmitCode ();
			
			if (Report.Errors > 0){
				error ("Compilation failed");
				return;
			}
			
			RootContext.CloseTypes ();

			PEFileKinds k = PEFileKinds.ConsoleApplication;
				
			if (target == Target.Library || target == Target.Module)
				k = PEFileKinds.Dll;
			else if (target == Target.Exe)
				k = PEFileKinds.ConsoleApplication;
			else if (target == Target.WinExe)
				k = PEFileKinds.WindowApplication;

			if (target == Target.Exe || target == Target.WinExe){
				MethodInfo ep = RootContext.EntryPoint;

				if (ep == null){
					Report.Error (5001, "Program " + output_file +
							      " does not have an entry point defined");
					return;
				}
				
				RootContext.CodeGen.AssemblyBuilder.SetEntryPoint (ep, k);
			}
			
			RootContext.CodeGen.Save (output_file);

			if (Report.Errors > 0){
				error ("Compilation failed");
				return;
			} else if (Report.ProbeCode != 0){
				error ("Failed to report code " + Report.ProbeCode);
				Environment.Exit (124);
			}
		}

	}
}