[mcs] Accept and ignore command line args supported by csc that we don't
[mono.git] / mcs / mcs / settings.cs
1 //
2 // settings.cs: All compiler settings
3 //
4 // Author: Miguel de Icaza (miguel@ximian.com)
5 //            Ravi Pratap  (ravi@ximian.com)
6 //            Marek Safar  (marek.safar@gmail.com)
7 //
8 //
9 // Dual licensed under the terms of the MIT X11 or GNU GPL
10 //
11 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
12 // Copyright 2004-2008 Novell, Inc
13 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
14 //
15
16 using System.Collections.Generic;
17 using System.IO;
18 using System.Text;
19 using System.Globalization;
20 using System;
21
22 namespace Mono.CSharp {
23
24         public enum LanguageVersion
25         {
26                 ISO_1 = 1,
27                 ISO_2 = 2,
28                 V_3 = 3,
29                 V_4 = 4,
30                 V_5 = 5,
31                 V_6 = 6,
32                 Experimental = 100,
33
34                 Default = LanguageVersion.V_6,
35         }
36
37         public enum RuntimeVersion
38         {
39                 v1,
40                 v2,
41                 v4
42         }
43
44         public enum Target
45         {
46                 Library, Exe, Module, WinExe
47         }
48
49         public enum Platform
50         {
51                 AnyCPU,
52                 AnyCPU32Preferred,
53                 Arm,
54                 X86,
55                 X64,
56                 IA64
57         }
58
59         public class CompilerSettings
60         {
61                 public Target Target;
62                 public Platform Platform;
63                 public string TargetExt;
64                 public bool VerifyClsCompliance;
65                 public bool Optimize;
66                 public LanguageVersion Version;
67                 public bool EnhancedWarnings;
68                 public bool LoadDefaultReferences;
69                 public string SdkVersion;
70
71                 public string StrongNameKeyFile;
72                 public string StrongNameKeyContainer;
73                 public bool StrongNameDelaySign;
74
75                 public int TabSize;
76
77                 public bool WarningsAreErrors;
78                 public int WarningLevel;
79
80                 //
81                 // Assemblies references to be loaded
82                 //
83                 public List<string> AssemblyReferences;
84
85                 // 
86                 // External aliases for assemblies
87                 //
88                 public List<Tuple<string, string>> AssemblyReferencesAliases;
89
90                 //
91                 // Modules to be embedded
92                 //
93                 public List<string> Modules;
94
95                 //
96                 // Lookup paths for referenced assemblies
97                 //
98                 public List<string> ReferencesLookupPaths;
99
100                 //
101                 // Encoding.
102                 //
103                 public Encoding Encoding;
104
105                 //
106                 // If set, enable XML documentation generation
107                 //
108                 public string DocumentationFile;
109
110                 public string MainClass;
111
112                 //
113                 // Output file
114                 //
115                 public string OutputFile;
116
117                 // 
118                 // The default compiler checked state
119                 //
120                 public bool Checked;
121
122                 //
123                 // If true, the compiler is operating in statement mode,
124                 // this currently turns local variable declaration into
125                 // static variables of a class
126                 //
127                 public bool StatementMode;      // TODO: SUPER UGLY
128                 
129                 //
130                 // Whether to allow Unsafe code
131                 //
132                 public bool Unsafe;
133
134                 public string Win32ResourceFile;
135                 public string Win32IconFile;
136
137                 //
138                 // A list of resource files for embedding
139                 //
140                 public List<AssemblyResource> Resources;
141
142                 public bool GenerateDebugInfo;
143
144                 #region Compiler debug flags only
145                 public bool ParseOnly, TokenizeOnly, Timestamps;
146                 public int DebugFlags;
147                 public int VerboseParserFlag;
148                 public int FatalCounter;
149                 public bool Stacktrace;
150                 public bool BreakOnInternalError;
151                 #endregion
152
153                 public List<string> GetResourceStrings;
154
155                 public bool ShowFullPaths;
156
157                 //
158                 // Whether we are being linked against the standard libraries.
159                 // This is only used to tell whether `System.Object' should
160                 // have a base class or not.
161                 //
162                 public bool StdLib;
163
164                 public RuntimeVersion StdLibRuntimeVersion;
165
166                 public string RuntimeMetadataVersion;
167
168                 public bool WriteMetadataOnly;
169
170                 readonly List<string> conditional_symbols;
171
172                 readonly List<SourceFile> source_files;
173
174                 List<int> warnings_as_error;
175                 List<int> warnings_only;
176                 HashSet<int> warning_ignore_table;
177
178                 public CompilerSettings ()
179                 {
180                         StdLib = true;
181                         Target = Target.Exe;
182                         TargetExt = ".exe";
183                         Platform = Platform.AnyCPU;
184                         Version = LanguageVersion.Default;
185                         VerifyClsCompliance = true;
186                         Encoding = Encoding.UTF8;
187                         LoadDefaultReferences = true;
188                         StdLibRuntimeVersion = RuntimeVersion.v4;
189                         WarningLevel = 4;
190
191                         // Default to 1 or mdb files would be platform speficic
192                         TabSize = 1;
193
194                         AssemblyReferences = new List<string> ();
195                         AssemblyReferencesAliases = new List<Tuple<string, string>> ();
196                         Modules = new List<string> ();
197                         ReferencesLookupPaths = new List<string> ();
198
199                         conditional_symbols = new List<string> ();
200                         //
201                         // Add default mcs define
202                         //
203                         conditional_symbols.Add ("__MonoCS__");
204
205                         source_files = new List<SourceFile> ();
206                 }
207
208                 #region Properties
209
210                 public SourceFile FirstSourceFile {
211                         get {
212                                 return source_files.Count > 0 ? source_files [0] : null;
213                         }
214                 }
215
216                 public bool HasKeyFileOrContainer {
217                         get {
218                                 return StrongNameKeyFile != null || StrongNameKeyContainer != null;
219                         }
220                 }
221
222                 public bool NeedsEntryPoint {
223                         get {
224                                 return Target == Target.Exe || Target == Target.WinExe;
225                         }
226                 }
227
228                 public List<SourceFile> SourceFiles {
229                         get {
230                                 return source_files;
231                         }
232                 }
233
234                 #endregion
235
236                 public void AddConditionalSymbol (string symbol)
237                 {
238                         if (!conditional_symbols.Contains (symbol))
239                                 conditional_symbols.Add (symbol);
240                 }
241
242                 public void AddWarningAsError (int id)
243                 {
244                         if (warnings_as_error == null)
245                                 warnings_as_error = new List<int> ();
246
247                         warnings_as_error.Add (id);
248                 }
249
250                 public void AddWarningOnly (int id)
251                 {
252                         if (warnings_only == null)
253                                 warnings_only = new List<int> ();
254
255                         warnings_only.Add (id);
256                 }
257
258                 public bool IsConditionalSymbolDefined (string symbol)
259                 {
260                         return conditional_symbols.Contains (symbol);
261                 }
262
263                 public bool IsWarningAsError (int code)
264                 {
265                         bool is_error = WarningsAreErrors;
266
267                         // Check specific list
268                         if (warnings_as_error != null)
269                                 is_error |= warnings_as_error.Contains (code);
270
271                         // Ignore excluded warnings
272                         if (warnings_only != null && warnings_only.Contains (code))
273                                 is_error = false;
274
275                         return is_error;
276                 }
277
278                 public bool IsWarningEnabled (int code, int level)
279                 {
280                         if (WarningLevel < level)
281                                 return false;
282
283                         return !IsWarningDisabledGlobally (code);
284                 }
285
286                 public bool IsWarningDisabledGlobally (int code)
287                 {
288                         return warning_ignore_table != null && warning_ignore_table.Contains (code);
289                 }
290
291                 public void SetIgnoreWarning (int code)
292                 {
293                         if (warning_ignore_table == null)
294                                 warning_ignore_table = new HashSet<int> ();
295
296                         warning_ignore_table.Add (code);
297                 }
298         }
299
300         public class CommandLineParser
301         {
302                 enum ParseResult
303                 {
304                         Success,
305                         Error,
306                         Stop,
307                         UnknownOption
308                 }
309
310                 static readonly char[] argument_value_separator = { ';', ',' };
311                 static readonly char[] numeric_value_separator = { ';', ',', ' ' };
312
313                 readonly TextWriter output;
314                 readonly Report report;
315                 bool stop_argument;
316
317                 Dictionary<string, int> source_file_index;
318
319                 public event Func<string[], int, int> UnknownOptionHandler;
320
321                 CompilerSettings parser_settings;
322
323                 public CommandLineParser (TextWriter errorOutput)
324                         : this (errorOutput, Console.Out)
325                 {
326                 }
327
328                 public CommandLineParser (TextWriter errorOutput, TextWriter messagesOutput)
329                 {
330                         var rp = new StreamReportPrinter (errorOutput);
331
332                         parser_settings = new CompilerSettings ();
333                         report = new Report (new CompilerContext (parser_settings, rp), rp);
334                         this.output = messagesOutput;
335                 }
336
337                 public bool HasBeenStopped {
338                         get {
339                                 return stop_argument;
340                         }
341                 }
342
343                 void About ()
344                 {
345                         output.WriteLine (
346                                 "The Mono C# compiler is Copyright 2001-2011, Novell, Inc.\n\n" +
347                                 "The compiler source code is released under the terms of the \n" +
348                                 "MIT X11 or GNU GPL licenses\n\n" +
349
350                                 "For more information on Mono, visit the project Web site\n" +
351                                 "   http://www.mono-project.com\n\n" +
352
353                                 "The compiler was written by Miguel de Icaza, Ravi Pratap, Martin Baulig, Marek Safar, Raja R Harinath, Atushi Enomoto");
354                 }
355
356                 public CompilerSettings ParseArguments (string[] args)
357                 {
358                         CompilerSettings settings = new CompilerSettings ();
359                         if (!ParseArguments (settings, args))
360                                 return null;
361
362                         return settings;
363                 }
364
365                 public bool ParseArguments (CompilerSettings settings, string[] args)
366                 {
367                         if (settings == null)
368                                 throw new ArgumentNullException ("settings");
369
370                         List<string> response_file_list = null;
371                         bool parsing_options = true;
372                         stop_argument = false;
373                         source_file_index = new Dictionary<string, int> ();
374
375                         for (int i = 0; i < args.Length; i++) {
376                                 string arg = args[i];
377                                 if (arg.Length == 0)
378                                         continue;
379
380                                 if (arg[0] == '@') {
381                                         string[] extra_args;
382                                         string response_file = arg.Substring (1);
383
384                                         if (response_file_list == null)
385                                                 response_file_list = new List<string> ();
386
387                                         if (response_file_list.Contains (response_file)) {
388                                                 report.Error (1515, "Response file `{0}' specified multiple times", response_file);
389                                                 return false;
390                                         }
391
392                                         response_file_list.Add (response_file);
393
394                                         extra_args = LoadArgs (response_file);
395                                         if (extra_args == null) {
396                                                 report.Error (2011, "Unable to open response file: " + response_file);
397                                                 return false;
398                                         }
399
400                                         args = AddArgs (args, extra_args);
401                                         continue;
402                                 }
403
404                                 if (parsing_options) {
405                                         if (arg == "--") {
406                                                 parsing_options = false;
407                                                 continue;
408                                         }
409
410                                         bool dash_opt = arg[0] == '-';
411                                         bool slash_opt = arg[0] == '/';
412                                         if (dash_opt) {
413                                                 switch (ParseOptionUnix (arg, ref args, ref i, settings)) {
414                                                 case ParseResult.Error:
415                                                 case ParseResult.Success:
416                                                         continue;
417                                                 case ParseResult.Stop:
418                                                         stop_argument = true;
419                                                         return true;
420                                                 case ParseResult.UnknownOption:
421                                                         if (UnknownOptionHandler != null) {
422                                                                 var ret = UnknownOptionHandler (args, i);
423                                                                 if (ret != -1) {
424                                                                         i = ret;
425                                                                         continue;
426                                                                 }
427                                                         }
428                                                         break;
429                                                 }
430                                         }
431
432                                         if (dash_opt || slash_opt) {
433                                                 // Try a -CSCOPTION
434                                                 string csc_opt = dash_opt ? "/" + arg.Substring (1) : arg;
435                                                 switch (ParseOption (csc_opt, ref args, settings)) {
436                                                 case ParseResult.Error:
437                                                 case ParseResult.Success:
438                                                         continue;
439                                                 case ParseResult.UnknownOption:
440                                                         // Need to skip `/home/test.cs' however /test.cs is considered as error
441                                                         if ((slash_opt && arg.Length > 3 && arg.IndexOf ('/', 2) > 0))
442                                                                 break;
443
444                                                         if (UnknownOptionHandler != null) {
445                                                                 var ret = UnknownOptionHandler (args, i);
446                                                                 if (ret != -1) {
447                                                                         i = ret;
448                                                                         continue;
449                                                                 }
450                                                         }
451
452                                                         Error_WrongOption (arg);
453                                                         return false;
454
455                                                 case ParseResult.Stop:
456                                                         stop_argument = true;
457                                                         return true;
458                                                 }
459                                         }
460                                 }
461
462                                 ProcessSourceFiles (arg, false, settings.SourceFiles);
463                         }
464
465                         return report.Errors == 0;
466                 }
467
468                 void ProcessSourceFiles (string spec, bool recurse, List<SourceFile> sourceFiles)
469                 {
470                         string path, pattern;
471
472                         SplitPathAndPattern (spec, out path, out pattern);
473                         if (pattern.IndexOf ('*') == -1) {
474                                 AddSourceFile (spec, sourceFiles);
475                                 return;
476                         }
477
478                         string[] files;
479                         try {
480                                 files = Directory.GetFiles (path, pattern);
481                         } catch (System.IO.DirectoryNotFoundException) {
482                                 report.Error (2001, "Source file `" + spec + "' could not be found");
483                                 return;
484                         } catch (System.IO.IOException) {
485                                 report.Error (2001, "Source file `" + spec + "' could not be found");
486                                 return;
487                         }
488                         foreach (string f in files) {
489                                 AddSourceFile (f, sourceFiles);
490                         }
491
492                         if (!recurse)
493                                 return;
494
495                         string[] dirs = null;
496
497                         try {
498                                 dirs = Directory.GetDirectories (path);
499                         } catch {
500                         }
501
502                         foreach (string d in dirs) {
503
504                                 // Don't include path in this string, as each
505                                 // directory entry already does
506                                 ProcessSourceFiles (d + "/" + pattern, true, sourceFiles);
507                         }
508                 }
509
510                 static string[] AddArgs (string[] args, string[] extra_args)
511                 {
512                         string[] new_args;
513                         new_args = new string[extra_args.Length + args.Length];
514
515                         // if args contains '--' we have to take that into account
516                         // split args into first half and second half based on '--'
517                         // and add the extra_args before --
518                         int split_position = Array.IndexOf (args, "--");
519                         if (split_position != -1) {
520                                 Array.Copy (args, new_args, split_position);
521                                 extra_args.CopyTo (new_args, split_position);
522                                 Array.Copy (args, split_position, new_args, split_position + extra_args.Length, args.Length - split_position);
523                         } else {
524                                 args.CopyTo (new_args, 0);
525                                 extra_args.CopyTo (new_args, args.Length);
526                         }
527
528                         return new_args;
529                 }
530
531                 void AddAssemblyReference (string alias, string assembly, CompilerSettings settings)
532                 {
533                         if (assembly.Length == 0) {
534                                 report.Error (1680, "Invalid reference alias `{0}='. Missing filename", alias);
535                                 return;
536                         }
537
538                         if (!IsExternAliasValid (alias)) {
539                                 report.Error (1679, "Invalid extern alias for -reference. Alias `{0}' is not a valid identifier", alias);
540                                 return;
541                         }
542
543                         settings.AssemblyReferencesAliases.Add (Tuple.Create (alias, assembly));
544                 }
545
546                 void AddResource (AssemblyResource res, CompilerSettings settings)
547                 {
548                         if (settings.Resources == null) {
549                                 settings.Resources = new List<AssemblyResource> ();
550                                 settings.Resources.Add (res);
551                                 return;
552                         }
553
554                         if (settings.Resources.Contains (res)) {
555                                 report.Error (1508, "The resource identifier `{0}' has already been used in this assembly", res.Name);
556                                 return;
557                         }
558
559                         settings.Resources.Add (res);
560                 }
561
562                 void AddSourceFile (string fileName, List<SourceFile> sourceFiles)
563                 {
564                         string path = Path.GetFullPath (fileName);
565
566                         int index;
567                         if (source_file_index.TryGetValue (path, out index)) {
568                                 string other_name = sourceFiles[index - 1].Name;
569                                 if (fileName.Equals (other_name))
570                                         report.Warning (2002, 1, "Source file `{0}' specified multiple times", other_name);
571                                 else
572                                         report.Warning (2002, 1, "Source filenames `{0}' and `{1}' both refer to the same file: {2}", fileName, other_name, path);
573
574                                 return;
575                         }
576
577                         var unit = new SourceFile (fileName, path, sourceFiles.Count + 1);
578                         sourceFiles.Add (unit);
579                         source_file_index.Add (path, unit.Index);
580                 }
581
582                 public bool ProcessWarningsList (string text, Action<int> action)
583                 {
584                         bool valid = true;
585                         foreach (string wid in text.Split (numeric_value_separator, StringSplitOptions.RemoveEmptyEntries)) {
586                                 var warning = wid;
587                                 if (warning.Length == 6 && warning [0] == 'C' && warning [1] == 'S')
588                                         warning = warning.Substring (2);
589
590                                 int id;
591                                 if (!int.TryParse (warning, NumberStyles.AllowLeadingWhite, CultureInfo.InvariantCulture, out id)) {
592                                         continue;
593                                 }
594
595                                 if (report.CheckWarningCode (id, Location.Null)) {
596                                         action (id);
597                                 } else {
598                                         report.Error (1904, "`{0}' is not a valid warning number", wid);
599                                         valid = false;
600                                 }
601                         }
602
603                         return valid;
604                 }
605
606                 void Error_RequiresArgument (string option)
607                 {
608                         report.Error (2006, "Missing argument for `{0}' option", option);
609                 }
610
611                 void Error_RequiresFileName (string option)
612                 {
613                         report.Error (2005, "Missing file specification for `{0}' option", option);
614                 }
615
616                 void Error_WrongOption (string option)
617                 {
618                         report.Error (2007, "Unrecognized command-line option: `{0}'", option);
619                 }
620
621                 static bool IsExternAliasValid (string identifier)
622                 {
623                         return Tokenizer.IsValidIdentifier (identifier);
624                 }
625
626                 static string[] LoadArgs (string file)
627                 {
628                         StreamReader f;
629                         var args = new List<string> ();
630                         string line;
631                         try {
632                                 f = new StreamReader (file);
633                         } catch {
634                                 return null;
635                         }
636
637                         StringBuilder sb = new StringBuilder ();
638
639                         while ((line = f.ReadLine ()) != null) {
640                                 int t = line.Length;
641
642                                 for (int i = 0; i < t; i++) {
643                                         char c = line[i];
644
645                                         if (c == '"' || c == '\'') {
646                                                 char end = c;
647
648                                                 for (i++; i < t; i++) {
649                                                         c = line[i];
650
651                                                         if (c == end)
652                                                                 break;
653                                                         sb.Append (c);
654                                                 }
655                                         } else if (c == ' ') {
656                                                 if (sb.Length > 0) {
657                                                         args.Add (sb.ToString ());
658                                                         sb.Length = 0;
659                                                 }
660                                         } else
661                                                 sb.Append (c);
662                                 }
663                                 if (sb.Length > 0) {
664                                         args.Add (sb.ToString ());
665                                         sb.Length = 0;
666                                 }
667                         }
668
669                         return args.ToArray ();
670                 }
671
672                 void OtherFlags ()
673                 {
674                         output.WriteLine (
675                                 "Other flags in the compiler\n" +
676                                 "   --fatal[=COUNT]    Makes error after COUNT fatal\n" +
677                                 "   --lint             Enhanced warnings\n" +
678                                 "   --metadata-only    Produced assembly will contain metadata only\n" +
679                                 "   --parse            Only parses the source file\n" +
680                                 "   --runtime:VERSION  Sets mscorlib.dll metadata version: v1, v2, v4\n" +
681                                 "   --stacktrace       Shows stack trace at error location\n" +
682                                 "   --timestamp        Displays time stamps of various compiler events\n" +
683                                 "   -v                 Verbose parsing (for debugging the parser)\n" +
684                                 "   --mcs-debug X      Sets MCS debugging level to X\n" +
685                                 "   --break-on-ice     Breaks compilation on internal compiler error");
686                 }
687
688                 //
689                 // This parses the -arg and /arg options to the compiler, even if the strings
690                 // in the following text use "/arg" on the strings.
691                 //
692                 ParseResult ParseOption (string option, ref string[] args, CompilerSettings settings)
693                 {
694                         int idx = option.IndexOf (':');
695                         string arg, value;
696
697                         if (idx == -1) {
698                                 arg = option;
699                                 value = "";
700                         } else {
701                                 arg = option.Substring (0, idx);
702
703                                 value = option.Substring (idx + 1);
704                         }
705
706                         switch (arg.ToLowerInvariant ()) {
707                         case "/nologo":
708                                 return ParseResult.Success;
709
710                         case "/t":
711                         case "/target":
712                                 switch (value) {
713                                 case "exe":
714                                         settings.Target = Target.Exe;
715                                         break;
716
717                                 case "winexe":
718                                         settings.Target = Target.WinExe;
719                                         break;
720
721                                 case "library":
722                                         settings.Target = Target.Library;
723                                         settings.TargetExt = ".dll";
724                                         break;
725
726                                 case "module":
727                                         settings.Target = Target.Module;
728                                         settings.TargetExt = ".netmodule";
729                                         break;
730
731                                 default:
732                                         report.Error (2019, "Invalid target type for -target. Valid options are `exe', `winexe', `library' or `module'");
733                                         return ParseResult.Error;
734                                 }
735                                 return ParseResult.Success;
736
737                         case "/out":
738                                 if (value.Length == 0) {
739                                         Error_RequiresFileName (option);
740                                         return ParseResult.Error;
741                                 }
742                                 settings.OutputFile = value;
743                                 return ParseResult.Success;
744
745                         case "/o":
746                         case "/o+":
747                         case "/optimize":
748                         case "/optimize+":
749                                 settings.Optimize = true;
750                                 return ParseResult.Success;
751
752                         case "/o-":
753                         case "/optimize-":
754                                 settings.Optimize = false;
755                                 return ParseResult.Success;
756
757                         // TODO: Not supported by csc 3.5+
758                         case "/incremental":
759                         case "/incremental+":
760                         case "/incremental-":
761                                 // nothing.
762                                 return ParseResult.Success;
763
764                         case "/d":
765                         case "/define": {
766                                         if (value.Length == 0) {
767                                                 Error_RequiresArgument (option);
768                                                 return ParseResult.Error;
769                                         }
770
771                                         foreach (string d in value.Split (argument_value_separator)) {
772                                                 string conditional = d.Trim ();
773                                                 if (!Tokenizer.IsValidIdentifier (conditional)) {
774                                                         report.Warning (2029, 1, "Invalid conditional define symbol `{0}'", conditional);
775                                                         continue;
776                                                 }
777
778                                                 settings.AddConditionalSymbol (conditional);
779                                         }
780                                         return ParseResult.Success;
781                                 }
782
783                         case "/bugreport":
784                                 //
785                                 // We should collect data, runtime, etc and store in the file specified
786                                 //
787                                 output.WriteLine ("To file bug reports, please visit: http://www.mono-project.com/Bugs");
788                                 return ParseResult.Success;
789
790                         case "/pkg": {
791                                         string packages;
792
793                                         if (value.Length == 0) {
794                                                 Error_RequiresArgument (option);
795                                                 return ParseResult.Error;
796                                         }
797                                         packages = String.Join (" ", value.Split (new Char[] { ';', ',', '\n', '\r' }));
798                                         string pkgout = Driver.GetPackageFlags (packages, report);
799
800                                         if (pkgout == null)
801                                                 return ParseResult.Error;
802
803                                         string[] xargs = pkgout.Trim (new Char[] { ' ', '\n', '\r', '\t' }).Split (new Char[] { ' ', '\t' });
804                                         args = AddArgs (args, xargs);
805                                         return ParseResult.Success;
806                                 }
807
808                         case "/linkres":
809                         case "/linkresource":
810                         case "/res":
811                         case "/resource":
812                                 AssemblyResource res = null;
813                                 string[] s = value.Split (argument_value_separator, StringSplitOptions.RemoveEmptyEntries);
814                                 switch (s.Length) {
815                                 case 1:
816                                         if (s[0].Length == 0)
817                                                 goto default;
818                                         res = new AssemblyResource (s[0], Path.GetFileName (s[0]));
819                                         break;
820                                 case 2:
821                                         res = new AssemblyResource (s[0], s[1]);
822                                         break;
823                                 case 3:
824                                         if (s[2] != "public" && s[2] != "private") {
825                                                 report.Error (1906, "Invalid resource visibility option `{0}'. Use either `public' or `private' instead", s[2]);
826                                                 return ParseResult.Error;
827                                         }
828                                         res = new AssemblyResource (s[0], s[1], s[2] == "private");
829                                         break;
830                                 default:
831                                         report.Error (-2005, "Wrong number of arguments for option `{0}'", option);
832                                         return ParseResult.Error;
833                                 }
834
835                                 if (res != null) {
836                                         res.IsEmbeded = arg[1] == 'r' || arg[1] == 'R';
837                                         AddResource (res, settings);
838                                 }
839
840                                 return ParseResult.Success;
841
842                         case "/recurse":
843                                 if (value.Length == 0) {
844                                         Error_RequiresFileName (option);
845                                         return ParseResult.Error;
846                                 }
847                                 ProcessSourceFiles (value, true, settings.SourceFiles);
848                                 return ParseResult.Success;
849
850                         case "/r":
851                         case "/reference": {
852                                         if (value.Length == 0) {
853                                                 Error_RequiresFileName (option);
854                                                 return ParseResult.Error;
855                                         }
856
857                                         string[] refs = value.Split (argument_value_separator);
858                                         foreach (string r in refs) {
859                                                 if (r.Length == 0)
860                                                         continue;
861
862                                                 string val = r;
863                                                 int index = val.IndexOf ('=');
864                                                 if (index > -1) {
865                                                         string alias = r.Substring (0, index);
866                                                         string assembly = r.Substring (index + 1);
867                                                         AddAssemblyReference (alias, assembly, settings);
868                                                         if (refs.Length != 1) {
869                                                                 report.Error (2034, "Cannot specify multiple aliases using single /reference option");
870                                                                 return ParseResult.Error;
871                                                         }
872                                                 } else {
873                                                         settings.AssemblyReferences.Add (val);
874                                                 }
875                                         }
876                                         return ParseResult.Success;
877                                 }
878                         case "/addmodule": {
879                                         if (value.Length == 0) {
880                                                 Error_RequiresFileName (option);
881                                                 return ParseResult.Error;
882                                         }
883
884                                         string[] refs = value.Split (argument_value_separator);
885                                         foreach (string r in refs) {
886                                                 settings.Modules.Add (r);
887                                         }
888                                         return ParseResult.Success;
889                                 }
890                         case "/win32res": {
891                                         if (value.Length == 0) {
892                                                 Error_RequiresFileName (option);
893                                                 return ParseResult.Error;
894                                         }
895
896                                         if (settings.Win32IconFile != null)
897                                                 report.Error (1565, "Cannot specify the `win32res' and the `win32ico' compiler option at the same time");
898
899                                         settings.Win32ResourceFile = value;
900                                         return ParseResult.Success;
901                                 }
902                         case "/win32icon": {
903                                         if (value.Length == 0) {
904                                                 Error_RequiresFileName (option);
905                                                 return ParseResult.Error;
906                                         }
907
908                                         if (settings.Win32ResourceFile != null)
909                                                 report.Error (1565, "Cannot specify the `win32res' and the `win32ico' compiler option at the same time");
910
911                                         settings.Win32IconFile = value;
912                                         return ParseResult.Success;
913                                 }
914                         case "/doc": {
915                                         if (value.Length == 0) {
916                                                 Error_RequiresFileName (option);
917                                                 return ParseResult.Error;
918                                         }
919
920                                         settings.DocumentationFile = value;
921                                         return ParseResult.Success;
922                                 }
923                         case "/lib": {
924                                         string[] libdirs;
925
926                                         if (value.Length == 0) {
927                                                 return ParseResult.Error;
928                                         }
929
930                                         libdirs = value.Split (argument_value_separator);
931                                         foreach (string dir in libdirs)
932                                                 settings.ReferencesLookupPaths.Add (dir);
933                                         return ParseResult.Success;
934                                 }
935
936                         case "/debug-":
937                                 settings.GenerateDebugInfo = false;
938                                 return ParseResult.Success;
939
940                         case "/debug":
941                                 if (value.Equals ("full", StringComparison.OrdinalIgnoreCase) || value.Equals ("pdbonly", StringComparison.OrdinalIgnoreCase) || idx < 0) {
942                                         settings.GenerateDebugInfo = true;
943                                         return ParseResult.Success;
944                                 }
945
946                                 if (value.Length > 0) {
947                                         report.Error (1902, "Invalid debug option `{0}'. Valid options are `full' or `pdbonly'", value);
948                                 } else {
949                                         Error_RequiresArgument (option);
950                                 }
951
952                                 return ParseResult.Error;
953
954                         case "/debug+":
955                                 settings.GenerateDebugInfo = true;
956                                 return ParseResult.Success;
957
958                         case "/checked":
959                         case "/checked+":
960                                 settings.Checked = true;
961                                 return ParseResult.Success;
962
963                         case "/checked-":
964                                 settings.Checked = false;
965                                 return ParseResult.Success;
966
967                         case "/clscheck":
968                         case "/clscheck+":
969                                 settings.VerifyClsCompliance = true;
970                                 return ParseResult.Success;
971
972                         case "/clscheck-":
973                                 settings.VerifyClsCompliance = false;
974                                 return ParseResult.Success;
975
976                         case "/unsafe":
977                         case "/unsafe+":
978                                 settings.Unsafe = true;
979                                 return ParseResult.Success;
980
981                         case "/unsafe-":
982                                 settings.Unsafe = false;
983                                 return ParseResult.Success;
984
985                         case "/warnaserror":
986                         case "/warnaserror+":
987                                 if (value.Length == 0) {
988                                         settings.WarningsAreErrors = true;
989                                         parser_settings.WarningsAreErrors = true;
990                                 } else {
991                                         if (!ProcessWarningsList (value, settings.AddWarningAsError))
992                                                 return ParseResult.Error;
993                                 }
994                                 return ParseResult.Success;
995
996                         case "/warnaserror-":
997                                 if (value.Length == 0) {
998                                         settings.WarningsAreErrors = false;
999                                 } else {
1000                                         if (!ProcessWarningsList (value, settings.AddWarningOnly))
1001                                                 return ParseResult.Error;
1002                                 }
1003                                 return ParseResult.Success;
1004
1005                         case "/warn":
1006                         case "/w":
1007                                 if (value.Length == 0) {
1008                                         Error_RequiresArgument (option);
1009                                         return ParseResult.Error;
1010                                 }
1011
1012                                 SetWarningLevel (value, settings);
1013                                 return ParseResult.Success;
1014
1015                         case "/nowarn":
1016                                 if (value.Length == 0) {
1017                                         Error_RequiresArgument (option);
1018                                         return ParseResult.Error;
1019                                 }
1020
1021                                 if (!ProcessWarningsList (value, settings.SetIgnoreWarning))
1022                                         return ParseResult.Error;
1023
1024                                 return ParseResult.Success;
1025
1026                         case "/noconfig":
1027                                 settings.LoadDefaultReferences = false;
1028                                 return ParseResult.Success;
1029
1030                         case "/platform":
1031                                 if (value.Length == 0) {
1032                                         Error_RequiresArgument (option);
1033                                         return ParseResult.Error;
1034                                 }
1035
1036                                 switch (value.ToLowerInvariant ()) {
1037                                 case "arm":
1038                                         settings.Platform = Platform.Arm;
1039                                         break;
1040                                 case "anycpu":
1041                                         settings.Platform = Platform.AnyCPU;
1042                                         break;
1043                                 case "x86":
1044                                         settings.Platform = Platform.X86;
1045                                         break;
1046                                 case "x64":
1047                                         settings.Platform = Platform.X64;
1048                                         break;
1049                                 case "itanium":
1050                                         settings.Platform = Platform.IA64;
1051                                         break;
1052                                 case "anycpu32bitpreferred":
1053                                         settings.Platform = Platform.AnyCPU32Preferred;
1054                                         break;
1055                                 default:
1056                                         report.Error (1672, "Invalid -platform option `{0}'. Valid options are `anycpu', `anycpu32bitpreferred', `arm', `x86', `x64' or `itanium'",
1057                                                 value);
1058                                         return ParseResult.Error;
1059                                 }
1060
1061                                 return ParseResult.Success;
1062
1063                         case "/sdk":
1064                                 if (value.Length == 0) {
1065                                         Error_RequiresArgument (option);
1066                                         return ParseResult.Error;
1067                                 }
1068
1069                                 settings.SdkVersion = value;
1070                                 return ParseResult.Success;
1071
1072                         // We just ignore this.
1073                         case "/errorreport":
1074                         case "/filealign":
1075                                 if (value.Length == 0) {
1076                                         Error_RequiresArgument (option);
1077                                         return ParseResult.Error;
1078                                 }
1079
1080                                 return ParseResult.Success;
1081
1082                         case "/helpinternal":
1083                                 OtherFlags ();
1084                                 return ParseResult.Stop;
1085
1086                         case "/help":
1087                         case "/?":
1088                                 Usage ();
1089                                 return ParseResult.Stop;
1090
1091                         case "/main":
1092                         case "/m":
1093                                 if (value.Length == 0) {
1094                                         Error_RequiresArgument (option);
1095                                         return ParseResult.Error;
1096                                 }
1097                                 settings.MainClass = value;
1098                                 return ParseResult.Success;
1099
1100                         case "/nostdlib":
1101                         case "/nostdlib+":
1102                                 settings.StdLib = false;
1103                                 return ParseResult.Success;
1104
1105                         case "/nostdlib-":
1106                                 settings.StdLib = true;
1107                                 return ParseResult.Success;
1108
1109                         case "/fullpaths":
1110                                 settings.ShowFullPaths = true;
1111                                 return ParseResult.Success;
1112
1113                         case "/keyfile":
1114                                 if (value.Length == 0) {
1115                                         Error_RequiresFileName (option);
1116                                         return ParseResult.Error;
1117                                 }
1118
1119                                 settings.StrongNameKeyFile = value;
1120                                 return ParseResult.Success;
1121
1122                         case "/keycontainer":
1123                                 if (value.Length == 0) {
1124                                         Error_RequiresArgument (option);
1125                                         return ParseResult.Error;
1126                                 }
1127
1128                                 settings.StrongNameKeyContainer = value;
1129                                 return ParseResult.Success;
1130
1131                         case "/delaysign+":
1132                         case "/delaysign":
1133                                 settings.StrongNameDelaySign = true;
1134                                 return ParseResult.Success;
1135
1136                         case "/delaysign-":
1137                                 settings.StrongNameDelaySign = false;
1138                                 return ParseResult.Success;
1139
1140                         case "/langversion":
1141                                 if (value.Length == 0) {
1142                                         Error_RequiresArgument (option);
1143                                         return ParseResult.Error;
1144                                 }
1145
1146                                 switch (value.ToLowerInvariant ()) {
1147                                 case "iso-1":
1148                                 case "1":
1149                                         settings.Version = LanguageVersion.ISO_1;
1150                                         return ParseResult.Success;
1151                                 case "default":
1152                                         settings.Version = LanguageVersion.Default;
1153                                         return ParseResult.Success;
1154                                 case "2":
1155                                 case "iso-2":
1156                                         settings.Version = LanguageVersion.ISO_2;
1157                                         return ParseResult.Success;
1158                                 case "3":
1159                                         settings.Version = LanguageVersion.V_3;
1160                                         return ParseResult.Success;
1161                                 case "4":
1162                                         settings.Version = LanguageVersion.V_4;
1163                                         return ParseResult.Success;
1164                                 case "5":
1165                                         settings.Version = LanguageVersion.V_5;
1166                                         return ParseResult.Success;
1167                                 case "6":
1168                                         settings.Version = LanguageVersion.V_6;
1169                                         return ParseResult.Success;
1170                                 case "experimental":
1171                                         settings.Version = LanguageVersion.Experimental;
1172                                         return ParseResult.Success;
1173                                 case "future":
1174                                         report.Warning (8000, 1, "Language version `future' is no longer supported");
1175                                         goto case "6";
1176                                 }
1177
1178                                 report.Error (1617, "Invalid -langversion option `{0}'. It must be `ISO-1', `ISO-2', Default or value in range 1 to 6", value);
1179                                 return ParseResult.Error;
1180
1181                         case "/codepage":
1182                                 if (value.Length == 0) {
1183                                         Error_RequiresArgument (option);
1184                                         return ParseResult.Error;
1185                                 }
1186
1187                                 switch (value) {
1188                                 case "utf8":
1189                                         settings.Encoding = Encoding.UTF8;
1190                                         break;
1191                                 case "reset":
1192                                         settings.Encoding = Encoding.Default;
1193                                         break;
1194                                 default:
1195                                         try {
1196                                                 settings.Encoding = Encoding.GetEncoding (int.Parse (value));
1197                                         } catch {
1198                                                 report.Error (2016, "Code page `{0}' is invalid or not installed", value);
1199                                         }
1200                                         return ParseResult.Error;
1201                                 }
1202                                 return ParseResult.Success;
1203
1204                         case "runtimemetadataversion":
1205                                 if (value.Length == 0) {
1206                                         Error_RequiresArgument (option);
1207                                         return ParseResult.Error;
1208                                 }
1209
1210                                 settings.RuntimeMetadataVersion = value;
1211                                 return ParseResult.Success;
1212
1213                         // csc options that we don't support
1214                         case "/utf8output":
1215                         case "/subsystemversion":
1216                         case "/highentropyva":
1217                         case "/highentropyva+":
1218                         case "/highentropyva-":
1219                         case "/win32manifest":
1220                         case "/nowin32manifest":
1221                                 return ParseResult.Success;
1222
1223                         default:
1224                                 return ParseResult.UnknownOption;
1225                         }
1226                 }
1227
1228                 //
1229                 // Currently handles the Unix-like command line options, but will be
1230                 // deprecated in favor of the CSCParseOption, which will also handle the
1231                 // options that start with a dash in the future.
1232                 //
1233                 ParseResult ParseOptionUnix (string arg, ref string[] args, ref int i, CompilerSettings settings)
1234                 {
1235                         switch (arg){
1236                         case "-v":
1237                                 settings.VerboseParserFlag++;
1238                                 return ParseResult.Success;
1239
1240                         case "--version":
1241                                 Version ();
1242                                 return ParseResult.Stop;
1243                                 
1244                         case "--parse":
1245                                 settings.ParseOnly = true;
1246                                 return ParseResult.Success;
1247                                 
1248                         case "--main": case "-m":
1249                                 report.Warning (-29, 1, "Compatibility: Use -main:CLASS instead of --main CLASS or -m CLASS");
1250                                 if ((i + 1) >= args.Length){
1251                                         Error_RequiresArgument (arg);
1252                                         return ParseResult.Error;
1253                                 }
1254                                 settings.MainClass = args[++i];
1255                                 return ParseResult.Success;
1256                                 
1257                         case "--unsafe":
1258                                 report.Warning (-29, 1, "Compatibility: Use -unsafe instead of --unsafe");
1259                                 settings.Unsafe = true;
1260                                 return ParseResult.Success;
1261                                 
1262                         case "/?": case "/h": case "/help":
1263                         case "--help":
1264                                 Usage ();
1265                                 return ParseResult.Stop;
1266
1267                         case "--define":
1268                                 report.Warning (-29, 1, "Compatibility: Use -d:SYMBOL instead of --define SYMBOL");
1269                                 if ((i + 1) >= args.Length){
1270                                         Error_RequiresArgument (arg);
1271                                         return ParseResult.Error;
1272                                 }
1273
1274                                 settings.AddConditionalSymbol (args [++i]);
1275                                 return ParseResult.Success;
1276
1277                         case "--tokenize":
1278                                 settings.TokenizeOnly = true;
1279                                 return ParseResult.Success;
1280                                 
1281                         case "-o": 
1282                         case "--output":
1283                                 report.Warning (-29, 1, "Compatibility: Use -out:FILE instead of --output FILE or -o FILE");
1284                                 if ((i + 1) >= args.Length){
1285                                         Error_RequiresArgument (arg);
1286                                         return ParseResult.Error;
1287                                 }
1288                                 settings.OutputFile = args[++i];
1289                                 return ParseResult.Success;
1290
1291                         case "--checked":
1292                                 report.Warning (-29, 1, "Compatibility: Use -checked instead of --checked");
1293                                 settings.Checked = true;
1294                                 return ParseResult.Success;
1295                                 
1296                         case "--stacktrace":
1297                                 settings.Stacktrace = true;
1298                                 return ParseResult.Success;
1299                                 
1300                         case "--linkresource":
1301                         case "--linkres":
1302                                 report.Warning (-29, 1, "Compatibility: Use -linkres:VALUE instead of --linkres VALUE");
1303                                 if ((i + 1) >= args.Length){
1304                                         Error_RequiresArgument (arg);
1305                                         return ParseResult.Error;
1306                                 }
1307
1308                                 AddResource (new AssemblyResource (args[++i], args[i]), settings);
1309                                 return ParseResult.Success;
1310                                 
1311                         case "--resource":
1312                         case "--res":
1313                                 report.Warning (-29, 1, "Compatibility: Use -res:VALUE instead of --res VALUE");
1314                                 if ((i + 1) >= args.Length){
1315                                         Error_RequiresArgument (arg);
1316                                         return ParseResult.Error;
1317                                 }
1318
1319                                 AddResource (new AssemblyResource (args[++i], args[i], true), settings);
1320                                 return ParseResult.Success;
1321                                 
1322                         case "--target":
1323                                 report.Warning (-29, 1, "Compatibility: Use -target:KIND instead of --target KIND");
1324                                 if ((i + 1) >= args.Length){
1325                                         Error_RequiresArgument (arg);
1326                                         return ParseResult.Error;
1327                                 }
1328                                 
1329                                 string type = args [++i];
1330                                 switch (type){
1331                                 case "library":
1332                                         settings.Target = Target.Library;
1333                                         settings.TargetExt = ".dll";
1334                                         break;
1335                                         
1336                                 case "exe":
1337                                         settings.Target = Target.Exe;
1338                                         break;
1339                                         
1340                                 case "winexe":
1341                                         settings.Target = Target.WinExe;
1342                                         break;
1343                                         
1344                                 case "module":
1345                                         settings.Target = Target.Module;
1346                                         settings.TargetExt = ".dll";
1347                                         break;
1348                                 default:
1349                                         report.Error (2019, "Invalid target type for -target. Valid options are `exe', `winexe', `library' or `module'");
1350                                         break;
1351                                 }
1352                                 return ParseResult.Success;
1353                                 
1354                         case "-r":
1355                                 report.Warning (-29, 1, "Compatibility: Use -r:LIBRARY instead of -r library");
1356                                 if ((i + 1) >= args.Length){
1357                                         Error_RequiresArgument (arg);
1358                                         return ParseResult.Error;
1359                                 }
1360                                 
1361                                 string val = args [++i];
1362                                 int idx = val.IndexOf ('=');
1363                                 if (idx > -1) {
1364                                         string alias = val.Substring (0, idx);
1365                                         string assembly = val.Substring (idx + 1);
1366                                         AddAssemblyReference (alias, assembly, settings);
1367                                         return ParseResult.Success;
1368                                 }
1369
1370                                 settings.AssemblyReferences.Add (val);
1371                                 return ParseResult.Success;
1372                                 
1373                         case "-L":
1374                                 report.Warning (-29, 1, "Compatibility: Use -lib:ARG instead of --L arg");
1375                                 if ((i + 1) >= args.Length){
1376                                         Error_RequiresArgument (arg);
1377                                         return ParseResult.Error;
1378                                 }
1379                                 settings.ReferencesLookupPaths.Add (args [++i]);
1380                                 return ParseResult.Success;
1381
1382                         case "--lint":
1383                                 settings.EnhancedWarnings = true;
1384                                 return ParseResult.Success;
1385                                 
1386                         case "--nostdlib":
1387                                 report.Warning (-29, 1, "Compatibility: Use -nostdlib instead of --nostdlib");
1388                                 settings.StdLib = false;
1389                                 return ParseResult.Success;
1390                                 
1391                         case "--nowarn":
1392                                 report.Warning (-29, 1, "Compatibility: Use -nowarn instead of --nowarn");
1393                                 if ((i + 1) >= args.Length){
1394                                         Error_RequiresArgument (arg);
1395                                         return ParseResult.Error;
1396                                 }
1397                                 int warn = 0;
1398                                 
1399                                 try {
1400                                         warn = int.Parse (args [++i]);
1401                                 } catch {
1402                                         Usage ();
1403                                         Environment.Exit (1);
1404                                 }
1405                                 settings.SetIgnoreWarning (warn);
1406                                 return ParseResult.Success;
1407
1408                         case "--wlevel":
1409                                 report.Warning (-29, 1, "Compatibility: Use -warn:LEVEL instead of --wlevel LEVEL");
1410                                 if ((i + 1) >= args.Length){
1411                                         Error_RequiresArgument (arg);
1412                                         return ParseResult.Error;
1413                                 }
1414
1415                                 SetWarningLevel (args [++i], settings);
1416                                 return ParseResult.Success;
1417
1418                         case "--mcs-debug":
1419                                 if ((i + 1) >= args.Length){
1420                                         Error_RequiresArgument (arg);
1421                                         return ParseResult.Error;
1422                                 }
1423
1424                                 try {
1425                                         settings.DebugFlags = int.Parse (args [++i]);
1426                                 } catch {
1427                                         Error_RequiresArgument (arg);
1428                                         return ParseResult.Error;
1429                                 }
1430
1431                                 return ParseResult.Success;
1432                                 
1433                         case "--about":
1434                                 About ();
1435                                 return ParseResult.Stop;
1436                                 
1437                         case "--recurse":
1438                                 report.Warning (-29, 1, "Compatibility: Use -recurse:PATTERN option instead --recurse PATTERN");
1439                                 if ((i + 1) >= args.Length){
1440                                         Error_RequiresArgument (arg);
1441                                         return ParseResult.Error;
1442                                 }
1443                                 ProcessSourceFiles (args [++i], true, settings.SourceFiles);
1444                                 return ParseResult.Success;
1445                                 
1446                         case "--timestamp":
1447                                 settings.Timestamps = true;
1448                                 return ParseResult.Success;
1449
1450                         case "--debug": case "-g":
1451                                 report.Warning (-29, 1, "Compatibility: Use -debug option instead of -g or --debug");
1452                                 settings.GenerateDebugInfo = true;
1453                                 return ParseResult.Success;
1454                                 
1455                         case "--noconfig":
1456                                 report.Warning (-29, 1, "Compatibility: Use -noconfig option instead of --noconfig");
1457                                 settings.LoadDefaultReferences = false;
1458                                 return ParseResult.Success;
1459
1460                         case "--metadata-only":
1461                                 settings.WriteMetadataOnly = true;
1462                                 return ParseResult.Success;
1463
1464                         case "--break-on-ice":
1465                                 settings.BreakOnInternalError = true;
1466                                 return ParseResult.Success;
1467
1468                         default:
1469                                 if (arg.StartsWith ("--fatal", StringComparison.Ordinal)) {
1470                                         int fatal = 1;
1471                                         if (arg.StartsWith ("--fatal=", StringComparison.Ordinal))
1472                                                 int.TryParse (arg.Substring (8), out fatal);
1473
1474                                         settings.FatalCounter = fatal;
1475                                         return ParseResult.Success;
1476                                 }
1477                                 if (arg.StartsWith ("--runtime:", StringComparison.Ordinal)) {
1478                                         string version = arg.Substring (10);
1479
1480                                         switch (version) {
1481                                         case "v1":
1482                                         case "V1":
1483                                                 settings.StdLibRuntimeVersion = RuntimeVersion.v1;
1484                                                 break;
1485                                         case "v2":
1486                                         case "V2":
1487                                                 settings.StdLibRuntimeVersion = RuntimeVersion.v2;
1488                                                 break;
1489                                         case "v4":
1490                                         case "V4":
1491                                                 settings.StdLibRuntimeVersion = RuntimeVersion.v4;
1492                                                 break;
1493                                         }
1494                                         return ParseResult.Success;
1495                                 }
1496
1497                                 if (arg.StartsWith ("--getresourcestrings:", StringComparison.Ordinal)) {
1498                                         string file = arg.Substring (21).Trim ();
1499                                         if (file.Length < 1) {
1500                                                 Error_RequiresArgument (arg);
1501                                                 return ParseResult.Error;
1502                                         }
1503
1504                                         if (settings.GetResourceStrings == null)
1505                                                 settings.GetResourceStrings = new List<string> ();
1506
1507                                         settings.GetResourceStrings.Add (file);
1508                                         return ParseResult.Success;
1509                                 }
1510
1511                                 return ParseResult.UnknownOption;
1512                         }
1513                 }
1514
1515                 void SetWarningLevel (string s, CompilerSettings settings)
1516                 {
1517                         int level = -1;
1518
1519                         try {
1520                                 level = int.Parse (s);
1521                         } catch {
1522                         }
1523                         if (level < 0 || level > 4) {
1524                                 report.Error (1900, "Warning level must be in the range 0-4");
1525                                 return;
1526                         }
1527                         settings.WarningLevel = level;
1528                 }
1529
1530                 //
1531                 // Given a path specification, splits the path from the file/pattern
1532                 //
1533                 static void SplitPathAndPattern (string spec, out string path, out string pattern)
1534                 {
1535                         int p = spec.LastIndexOf ('/');
1536                         if (p != -1) {
1537                                 //
1538                                 // Windows does not like /file.cs, switch that to:
1539                                 // "\", "file.cs"
1540                                 //
1541                                 if (p == 0) {
1542                                         path = "\\";
1543                                         pattern = spec.Substring (1);
1544                                 } else {
1545                                         path = spec.Substring (0, p);
1546                                         pattern = spec.Substring (p + 1);
1547                                 }
1548                                 return;
1549                         }
1550
1551                         p = spec.LastIndexOf ('\\');
1552                         if (p != -1) {
1553                                 path = spec.Substring (0, p);
1554                                 pattern = spec.Substring (p + 1);
1555                                 return;
1556                         }
1557
1558                         path = ".";
1559                         pattern = spec;
1560                 }
1561
1562                 void Usage ()
1563                 {
1564                         output.WriteLine (
1565                                 "Mono C# compiler, Copyright 2001-2011 Novell, Inc., Copyright 2011-2012 Xamarin, Inc\n" +
1566                                 "mcs [options] source-files\n" +
1567                                 "   --about              About the Mono C# compiler\n" +
1568                                 "   -addmodule:M1[,Mn]   Adds the module to the generated assembly\n" +
1569                                 "   -checked[+|-]        Sets default aritmetic overflow context\n" +
1570                                 "   -clscheck[+|-]       Disables CLS Compliance verifications\n" +
1571                                 "   -codepage:ID         Sets code page to the one in ID (number, utf8, reset)\n" +
1572                                 "   -define:S1[;S2]      Defines one or more conditional symbols (short: -d)\n" +
1573                                 "   -debug[+|-], -g      Generate debugging information\n" +
1574                                 "   -delaysign[+|-]      Only insert the public key into the assembly (no signing)\n" +
1575                                 "   -doc:FILE            Process documentation comments to XML file\n" +
1576                                 "   -fullpaths           Any issued error or warning uses absolute file path\n" +
1577                                 "   -help                Lists all compiler options (short: -?)\n" +
1578                                 "   -keycontainer:NAME   The key pair container used to sign the output assembly\n" +
1579                                 "   -keyfile:FILE        The key file used to strongname the ouput assembly\n" +
1580                                 "   -langversion:TEXT    Specifies language version: ISO-1, ISO-2, 3, 4, 5, Default or Experimental\n" +
1581                                 "   -lib:PATH1[,PATHn]   Specifies the location of referenced assemblies\n" +
1582                                 "   -main:CLASS          Specifies the class with the Main method (short: -m)\n" +
1583                                 "   -noconfig            Disables implicitly referenced assemblies\n" +
1584                                 "   -nostdlib[+|-]       Does not reference mscorlib.dll library\n" +
1585                                 "   -nowarn:W1[,Wn]      Suppress one or more compiler warnings\n" +
1586                                 "   -optimize[+|-]       Enables advanced compiler optimizations (short: -o)\n" +
1587                                 "   -out:FILE            Specifies output assembly name\n" +
1588                                 "   -pkg:P1[,Pn]         References packages P1..Pn\n" +
1589                                 "   -platform:ARCH       Specifies the target platform of the output assembly\n" +
1590                                 "                        ARCH can be one of: anycpu, anycpu32bitpreferred, arm,\n" +
1591                                 "                        x86, x64 or itanium. The default is anycpu.\n" +
1592                                 "   -recurse:SPEC        Recursively compiles files according to SPEC pattern\n" +
1593                                 "   -reference:A1[,An]   Imports metadata from the specified assembly (short: -r)\n" +
1594                                 "   -reference:ALIAS=A   Imports metadata using specified extern alias (short: -r)\n" +
1595                                 "   -sdk:VERSION         Specifies SDK version of referenced assemblies\n" +
1596                                 "                        VERSION can be one of: 2, 4, 4.5 (default) or a custom value\n" +
1597                                 "   -target:KIND         Specifies the format of the output assembly (short: -t)\n" +
1598                                 "                        KIND can be one of: exe, winexe, library, module\n" +
1599                                 "   -unsafe[+|-]         Allows to compile code which uses unsafe keyword\n" +
1600                                 "   -warnaserror[+|-]    Treats all warnings as errors\n" +
1601                                 "   -warnaserror[+|-]:W1[,Wn] Treats one or more compiler warnings as errors\n" +
1602                                 "   -warn:0-4            Sets warning level, the default is 4 (short -w:)\n" +
1603                                 "   -helpinternal        Shows internal and advanced compiler options\n" +
1604                                 "\n" +
1605                                 "Resources:\n" +
1606                                 "   -linkresource:FILE[,ID] Links FILE as a resource (short: -linkres)\n" +
1607                                 "   -resource:FILE[,ID]     Embed FILE as a resource (short: -res)\n" +
1608                                 "   -win32res:FILE          Specifies Win32 resource file (.res)\n" +
1609                                 "   -win32icon:FILE         Use this icon for the output\n" +
1610                                                                 "   @file                   Read response file for more options\n\n" +
1611                                 "Options can be of the form -option or /option");
1612                 }
1613
1614                 void Version ()
1615                 {
1616                         string version = System.Reflection.MethodBase.GetCurrentMethod ().DeclaringType.Assembly.GetName ().Version.ToString ();
1617                         output.WriteLine ("Mono C# compiler version {0}", version);
1618                 }
1619         }
1620
1621         public class RootContext
1622         {
1623                 //
1624                 // Contains the parsed tree
1625                 //
1626                 static ModuleContainer root;
1627
1628                 static public ModuleContainer ToplevelTypes {
1629                         get { return root; }
1630                         set { root = value; }
1631                 }
1632         }
1633 }