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