2 // settings.cs: All compiler settings
4 // Author: Miguel de Icaza (miguel@ximian.com)
5 // Ravi Pratap (ravi@ximian.com)
6 // Marek Safar (marek.safar@gmail.com)
9 // Dual licensed under the terms of the MIT X11 or GNU GPL
11 // Copyright 2001 Ximian, Inc (http://www.ximian.com)
12 // Copyright 2004-2008 Novell, Inc
13 // Copyright 2011 Xamarin, Inc (http://www.xamarin.com)
16 using System.Collections.Generic;
19 using System.Globalization;
22 namespace Mono.CSharp {
24 public enum LanguageVersion
34 Default = LanguageVersion.V_6,
37 public enum RuntimeVersion
46 Library, Exe, Module, WinExe
59 public class CompilerSettings
62 public Platform Platform;
63 public string TargetExt;
64 public bool VerifyClsCompliance;
66 public LanguageVersion Version;
67 public bool EnhancedWarnings;
68 public bool LoadDefaultReferences;
69 public string SdkVersion;
71 public string StrongNameKeyFile;
72 public string StrongNameKeyContainer;
73 public bool StrongNameDelaySign;
77 public bool WarningsAreErrors;
78 public int WarningLevel;
81 // Assemblies references to be loaded
83 public List<string> AssemblyReferences;
86 // External aliases for assemblies
88 public List<Tuple<string, string>> AssemblyReferencesAliases;
91 // Modules to be embedded
93 public List<string> Modules;
96 // Lookup paths for referenced assemblies
98 public List<string> ReferencesLookupPaths;
103 public Encoding Encoding;
106 // If set, enable XML documentation generation
108 public string DocumentationFile;
110 public string MainClass;
115 public string OutputFile;
118 // The default compiler checked state
123 // If true, the compiler is operating in statement mode,
124 // this currently turns local variable declaration into
125 // static variables of a class
127 public bool StatementMode; // TODO: SUPER UGLY
130 // Whether to allow Unsafe code
134 public string Win32ResourceFile;
135 public string Win32IconFile;
138 // A list of resource files for embedding
140 public List<AssemblyResource> Resources;
142 public bool GenerateDebugInfo;
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;
153 public List<string> GetResourceStrings;
155 public bool ShowFullPaths;
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.
164 public RuntimeVersion StdLibRuntimeVersion;
166 public string RuntimeMetadataVersion;
168 public bool WriteMetadataOnly;
170 readonly List<string> conditional_symbols;
172 readonly List<SourceFile> source_files;
174 List<int> warnings_as_error;
175 List<int> warnings_only;
176 HashSet<int> warning_ignore_table;
178 public CompilerSettings ()
183 Platform = Platform.AnyCPU;
184 Version = LanguageVersion.Default;
185 VerifyClsCompliance = true;
186 Encoding = Encoding.UTF8;
187 LoadDefaultReferences = true;
188 StdLibRuntimeVersion = RuntimeVersion.v4;
191 // Default to 1 or mdb files would be platform speficic
194 AssemblyReferences = new List<string> ();
195 AssemblyReferencesAliases = new List<Tuple<string, string>> ();
196 Modules = new List<string> ();
197 ReferencesLookupPaths = new List<string> ();
199 conditional_symbols = new List<string> ();
201 // Add default mcs define
203 conditional_symbols.Add ("__MonoCS__");
205 source_files = new List<SourceFile> ();
210 public SourceFile FirstSourceFile {
212 return source_files.Count > 0 ? source_files [0] : null;
216 public bool HasKeyFileOrContainer {
218 return StrongNameKeyFile != null || StrongNameKeyContainer != null;
222 public bool NeedsEntryPoint {
224 return Target == Target.Exe || Target == Target.WinExe;
228 public List<SourceFile> SourceFiles {
236 public void AddConditionalSymbol (string symbol)
238 if (!conditional_symbols.Contains (symbol))
239 conditional_symbols.Add (symbol);
242 public void AddWarningAsError (int id)
244 if (warnings_as_error == null)
245 warnings_as_error = new List<int> ();
247 warnings_as_error.Add (id);
250 public void AddWarningOnly (int id)
252 if (warnings_only == null)
253 warnings_only = new List<int> ();
255 warnings_only.Add (id);
258 public bool IsConditionalSymbolDefined (string symbol)
260 return conditional_symbols.Contains (symbol);
263 public bool IsWarningAsError (int code)
265 bool is_error = WarningsAreErrors;
267 // Check specific list
268 if (warnings_as_error != null)
269 is_error |= warnings_as_error.Contains (code);
271 // Ignore excluded warnings
272 if (warnings_only != null && warnings_only.Contains (code))
278 public bool IsWarningEnabled (int code, int level)
280 if (WarningLevel < level)
283 return !IsWarningDisabledGlobally (code);
286 public bool IsWarningDisabledGlobally (int code)
288 return warning_ignore_table != null && warning_ignore_table.Contains (code);
291 public void SetIgnoreWarning (int code)
293 if (warning_ignore_table == null)
294 warning_ignore_table = new HashSet<int> ();
296 warning_ignore_table.Add (code);
300 public class CommandLineParser
310 static readonly char[] argument_value_separator = { ';', ',' };
311 static readonly char[] numeric_value_separator = { ';', ',', ' ' };
313 readonly TextWriter output;
314 readonly Report report;
317 Dictionary<string, int> source_file_index;
319 public event Func<string[], int, int> UnknownOptionHandler;
321 CompilerSettings parser_settings;
323 public CommandLineParser (TextWriter errorOutput)
324 : this (errorOutput, Console.Out)
328 public CommandLineParser (TextWriter errorOutput, TextWriter messagesOutput)
330 var rp = new StreamReportPrinter (errorOutput);
332 parser_settings = new CompilerSettings ();
333 report = new Report (new CompilerContext (parser_settings, rp), rp);
334 this.output = messagesOutput;
337 public bool HasBeenStopped {
339 return stop_argument;
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" +
350 "For more information on Mono, visit the project Web site\n" +
351 " http://www.mono-project.com\n\n" +
353 "The compiler was written by Miguel de Icaza, Ravi Pratap, Martin Baulig, Marek Safar, Raja R Harinath, Atushi Enomoto");
356 public CompilerSettings ParseArguments (string[] args)
358 CompilerSettings settings = new CompilerSettings ();
359 if (!ParseArguments (settings, args))
365 public bool ParseArguments (CompilerSettings settings, string[] args)
367 if (settings == null)
368 throw new ArgumentNullException ("settings");
370 List<string> response_file_list = null;
371 bool parsing_options = true;
372 stop_argument = false;
373 source_file_index = new Dictionary<string, int> ();
375 for (int i = 0; i < args.Length; i++) {
376 string arg = args[i];
382 string response_file = arg.Substring (1);
384 if (response_file_list == null)
385 response_file_list = new List<string> ();
387 if (response_file_list.Contains (response_file)) {
388 report.Error (1515, "Response file `{0}' specified multiple times", response_file);
392 response_file_list.Add (response_file);
394 extra_args = LoadArgs (response_file);
395 if (extra_args == null) {
396 report.Error (2011, "Unable to open response file: " + response_file);
400 args = AddArgs (args, extra_args);
404 if (parsing_options) {
406 parsing_options = false;
410 bool dash_opt = arg[0] == '-';
411 bool slash_opt = arg[0] == '/';
413 switch (ParseOptionUnix (arg, ref args, ref i, settings)) {
414 case ParseResult.Error:
415 case ParseResult.Success:
417 case ParseResult.Stop:
418 stop_argument = true;
420 case ParseResult.UnknownOption:
421 if (UnknownOptionHandler != null) {
422 var ret = UnknownOptionHandler (args, i);
432 if (dash_opt || slash_opt) {
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:
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))
444 if (UnknownOptionHandler != null) {
445 var ret = UnknownOptionHandler (args, i);
452 Error_WrongOption (arg);
455 case ParseResult.Stop:
456 stop_argument = true;
462 ProcessSourceFiles (arg, false, settings.SourceFiles);
465 return report.Errors == 0;
468 void ProcessSourceFiles (string spec, bool recurse, List<SourceFile> sourceFiles)
470 string path, pattern;
472 SplitPathAndPattern (spec, out path, out pattern);
473 if (pattern.IndexOf ('*') == -1) {
474 AddSourceFile (spec, sourceFiles);
480 files = Directory.GetFiles (path, pattern);
481 } catch (System.IO.DirectoryNotFoundException) {
482 report.Error (2001, "Source file `" + spec + "' could not be found");
484 } catch (System.IO.IOException) {
485 report.Error (2001, "Source file `" + spec + "' could not be found");
488 foreach (string f in files) {
489 AddSourceFile (f, sourceFiles);
495 string[] dirs = null;
498 dirs = Directory.GetDirectories (path);
502 foreach (string d in dirs) {
504 // Don't include path in this string, as each
505 // directory entry already does
506 ProcessSourceFiles (d + "/" + pattern, true, sourceFiles);
510 static string[] AddArgs (string[] args, string[] extra_args)
513 new_args = new string[extra_args.Length + args.Length];
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);
524 args.CopyTo (new_args, 0);
525 extra_args.CopyTo (new_args, args.Length);
531 void AddAssemblyReference (string alias, string assembly, CompilerSettings settings)
533 if (assembly.Length == 0) {
534 report.Error (1680, "Invalid reference alias `{0}='. Missing filename", alias);
538 if (!IsExternAliasValid (alias)) {
539 report.Error (1679, "Invalid extern alias for -reference. Alias `{0}' is not a valid identifier", alias);
543 settings.AssemblyReferencesAliases.Add (Tuple.Create (alias, assembly));
546 void AddResource (AssemblyResource res, CompilerSettings settings)
548 if (settings.Resources == null) {
549 settings.Resources = new List<AssemblyResource> ();
550 settings.Resources.Add (res);
554 if (settings.Resources.Contains (res)) {
555 report.Error (1508, "The resource identifier `{0}' has already been used in this assembly", res.Name);
559 settings.Resources.Add (res);
562 void AddSourceFile (string fileName, List<SourceFile> sourceFiles)
564 string path = Path.GetFullPath (fileName);
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);
572 report.Warning (2002, 1, "Source filenames `{0}' and `{1}' both refer to the same file: {2}", fileName, other_name, path);
577 var unit = new SourceFile (fileName, path, sourceFiles.Count + 1);
578 sourceFiles.Add (unit);
579 source_file_index.Add (path, unit.Index);
582 public bool ProcessWarningsList (string text, Action<int> action)
585 foreach (string wid in text.Split (numeric_value_separator, StringSplitOptions.RemoveEmptyEntries)) {
587 if (warning.Length == 6 && warning [0] == 'C' && warning [1] == 'S')
588 warning = warning.Substring (2);
591 if (!int.TryParse (warning, NumberStyles.AllowLeadingWhite, CultureInfo.InvariantCulture, out id)) {
595 if (report.CheckWarningCode (id, Location.Null)) {
598 report.Error (1904, "`{0}' is not a valid warning number", wid);
606 void Error_RequiresArgument (string option)
608 report.Error (2006, "Missing argument for `{0}' option", option);
611 void Error_RequiresFileName (string option)
613 report.Error (2005, "Missing file specification for `{0}' option", option);
616 void Error_WrongOption (string option)
618 report.Error (2007, "Unrecognized command-line option: `{0}'", option);
621 static bool IsExternAliasValid (string identifier)
623 return Tokenizer.IsValidIdentifier (identifier);
626 static string[] LoadArgs (string file)
629 var args = new List<string> ();
632 f = new StreamReader (file);
637 StringBuilder sb = new StringBuilder ();
639 while ((line = f.ReadLine ()) != null) {
642 for (int i = 0; i < t; i++) {
645 if (c == '"' || c == '\'') {
648 for (i++; i < t; i++) {
655 } else if (c == ' ') {
657 args.Add (sb.ToString ());
664 args.Add (sb.ToString ());
669 return args.ToArray ();
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");
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.
692 ParseResult ParseOption (string option, ref string[] args, CompilerSettings settings)
694 int idx = option.IndexOf (':');
701 arg = option.Substring (0, idx);
703 value = option.Substring (idx + 1);
706 switch (arg.ToLowerInvariant ()) {
708 return ParseResult.Success;
714 settings.Target = Target.Exe;
718 settings.Target = Target.WinExe;
722 settings.Target = Target.Library;
723 settings.TargetExt = ".dll";
727 settings.Target = Target.Module;
728 settings.TargetExt = ".netmodule";
732 report.Error (2019, "Invalid target type for -target. Valid options are `exe', `winexe', `library' or `module'");
733 return ParseResult.Error;
735 return ParseResult.Success;
738 if (value.Length == 0) {
739 Error_RequiresFileName (option);
740 return ParseResult.Error;
742 settings.OutputFile = value;
743 return ParseResult.Success;
749 settings.Optimize = true;
750 return ParseResult.Success;
754 settings.Optimize = false;
755 return ParseResult.Success;
757 // TODO: Not supported by csc 3.5+
759 case "/incremental+":
760 case "/incremental-":
762 return ParseResult.Success;
766 if (value.Length == 0) {
767 Error_RequiresArgument (option);
768 return ParseResult.Error;
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);
778 settings.AddConditionalSymbol (conditional);
780 return ParseResult.Success;
785 // We should collect data, runtime, etc and store in the file specified
787 output.WriteLine ("To file bug reports, please visit: http://www.mono-project.com/Bugs");
788 return ParseResult.Success;
793 if (value.Length == 0) {
794 Error_RequiresArgument (option);
795 return ParseResult.Error;
797 packages = String.Join (" ", value.Split (new Char[] { ';', ',', '\n', '\r' }));
798 string pkgout = Driver.GetPackageFlags (packages, report);
801 return ParseResult.Error;
803 string[] xargs = pkgout.Trim (new Char[] { ' ', '\n', '\r', '\t' }).Split (new Char[] { ' ', '\t' });
804 args = AddArgs (args, xargs);
805 return ParseResult.Success;
809 case "/linkresource":
812 AssemblyResource res = null;
813 string[] s = value.Split (argument_value_separator, StringSplitOptions.RemoveEmptyEntries);
816 if (s[0].Length == 0)
818 res = new AssemblyResource (s[0], Path.GetFileName (s[0]));
821 res = new AssemblyResource (s[0], s[1]);
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;
828 res = new AssemblyResource (s[0], s[1], s[2] == "private");
831 report.Error (-2005, "Wrong number of arguments for option `{0}'", option);
832 return ParseResult.Error;
836 res.IsEmbeded = arg[1] == 'r' || arg[1] == 'R';
837 AddResource (res, settings);
840 return ParseResult.Success;
843 if (value.Length == 0) {
844 Error_RequiresFileName (option);
845 return ParseResult.Error;
847 ProcessSourceFiles (value, true, settings.SourceFiles);
848 return ParseResult.Success;
852 if (value.Length == 0) {
853 Error_RequiresFileName (option);
854 return ParseResult.Error;
857 string[] refs = value.Split (argument_value_separator);
858 foreach (string r in refs) {
863 int index = val.IndexOf ('=');
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;
873 settings.AssemblyReferences.Add (val);
876 return ParseResult.Success;
879 if (value.Length == 0) {
880 Error_RequiresFileName (option);
881 return ParseResult.Error;
884 string[] refs = value.Split (argument_value_separator);
885 foreach (string r in refs) {
886 settings.Modules.Add (r);
888 return ParseResult.Success;
891 if (value.Length == 0) {
892 Error_RequiresFileName (option);
893 return ParseResult.Error;
896 if (settings.Win32IconFile != null)
897 report.Error (1565, "Cannot specify the `win32res' and the `win32ico' compiler option at the same time");
899 settings.Win32ResourceFile = value;
900 return ParseResult.Success;
903 if (value.Length == 0) {
904 Error_RequiresFileName (option);
905 return ParseResult.Error;
908 if (settings.Win32ResourceFile != null)
909 report.Error (1565, "Cannot specify the `win32res' and the `win32ico' compiler option at the same time");
911 settings.Win32IconFile = value;
912 return ParseResult.Success;
915 if (value.Length == 0) {
916 Error_RequiresFileName (option);
917 return ParseResult.Error;
920 settings.DocumentationFile = value;
921 return ParseResult.Success;
926 if (value.Length == 0) {
927 return ParseResult.Error;
930 libdirs = value.Split (argument_value_separator);
931 foreach (string dir in libdirs)
932 settings.ReferencesLookupPaths.Add (dir);
933 return ParseResult.Success;
937 settings.GenerateDebugInfo = false;
938 return ParseResult.Success;
941 if (value.Equals ("full", StringComparison.OrdinalIgnoreCase) || value.Equals ("pdbonly", StringComparison.OrdinalIgnoreCase) || idx < 0) {
942 settings.GenerateDebugInfo = true;
943 return ParseResult.Success;
946 if (value.Length > 0) {
947 report.Error (1902, "Invalid debug option `{0}'. Valid options are `full' or `pdbonly'", value);
949 Error_RequiresArgument (option);
952 return ParseResult.Error;
955 settings.GenerateDebugInfo = true;
956 return ParseResult.Success;
960 settings.Checked = true;
961 return ParseResult.Success;
964 settings.Checked = false;
965 return ParseResult.Success;
969 settings.VerifyClsCompliance = true;
970 return ParseResult.Success;
973 settings.VerifyClsCompliance = false;
974 return ParseResult.Success;
978 settings.Unsafe = true;
979 return ParseResult.Success;
982 settings.Unsafe = false;
983 return ParseResult.Success;
986 case "/warnaserror+":
987 if (value.Length == 0) {
988 settings.WarningsAreErrors = true;
989 parser_settings.WarningsAreErrors = true;
991 if (!ProcessWarningsList (value, settings.AddWarningAsError))
992 return ParseResult.Error;
994 return ParseResult.Success;
996 case "/warnaserror-":
997 if (value.Length == 0) {
998 settings.WarningsAreErrors = false;
1000 if (!ProcessWarningsList (value, settings.AddWarningOnly))
1001 return ParseResult.Error;
1003 return ParseResult.Success;
1007 if (value.Length == 0) {
1008 Error_RequiresArgument (option);
1009 return ParseResult.Error;
1012 SetWarningLevel (value, settings);
1013 return ParseResult.Success;
1016 if (value.Length == 0) {
1017 Error_RequiresArgument (option);
1018 return ParseResult.Error;
1021 if (!ProcessWarningsList (value, settings.SetIgnoreWarning))
1022 return ParseResult.Error;
1024 return ParseResult.Success;
1027 settings.LoadDefaultReferences = false;
1028 return ParseResult.Success;
1031 if (value.Length == 0) {
1032 Error_RequiresArgument (option);
1033 return ParseResult.Error;
1036 switch (value.ToLowerInvariant ()) {
1038 settings.Platform = Platform.Arm;
1041 settings.Platform = Platform.AnyCPU;
1044 settings.Platform = Platform.X86;
1047 settings.Platform = Platform.X64;
1050 settings.Platform = Platform.IA64;
1052 case "anycpu32bitpreferred":
1053 settings.Platform = Platform.AnyCPU32Preferred;
1056 report.Error (1672, "Invalid -platform option `{0}'. Valid options are `anycpu', `anycpu32bitpreferred', `arm', `x86', `x64' or `itanium'",
1058 return ParseResult.Error;
1061 return ParseResult.Success;
1064 if (value.Length == 0) {
1065 Error_RequiresArgument (option);
1066 return ParseResult.Error;
1069 settings.SdkVersion = value;
1070 return ParseResult.Success;
1072 // We just ignore this.
1073 case "/errorreport":
1075 if (value.Length == 0) {
1076 Error_RequiresArgument (option);
1077 return ParseResult.Error;
1080 return ParseResult.Success;
1082 case "/helpinternal":
1084 return ParseResult.Stop;
1089 return ParseResult.Stop;
1093 if (value.Length == 0) {
1094 Error_RequiresArgument (option);
1095 return ParseResult.Error;
1097 settings.MainClass = value;
1098 return ParseResult.Success;
1102 settings.StdLib = false;
1103 return ParseResult.Success;
1106 settings.StdLib = true;
1107 return ParseResult.Success;
1110 settings.ShowFullPaths = true;
1111 return ParseResult.Success;
1114 if (value.Length == 0) {
1115 Error_RequiresFileName (option);
1116 return ParseResult.Error;
1119 settings.StrongNameKeyFile = value;
1120 return ParseResult.Success;
1122 case "/keycontainer":
1123 if (value.Length == 0) {
1124 Error_RequiresArgument (option);
1125 return ParseResult.Error;
1128 settings.StrongNameKeyContainer = value;
1129 return ParseResult.Success;
1133 settings.StrongNameDelaySign = true;
1134 return ParseResult.Success;
1137 settings.StrongNameDelaySign = false;
1138 return ParseResult.Success;
1140 case "/langversion":
1141 if (value.Length == 0) {
1142 Error_RequiresArgument (option);
1143 return ParseResult.Error;
1146 switch (value.ToLowerInvariant ()) {
1149 settings.Version = LanguageVersion.ISO_1;
1150 return ParseResult.Success;
1152 settings.Version = LanguageVersion.Default;
1153 return ParseResult.Success;
1156 settings.Version = LanguageVersion.ISO_2;
1157 return ParseResult.Success;
1159 settings.Version = LanguageVersion.V_3;
1160 return ParseResult.Success;
1162 settings.Version = LanguageVersion.V_4;
1163 return ParseResult.Success;
1165 settings.Version = LanguageVersion.V_5;
1166 return ParseResult.Success;
1168 settings.Version = LanguageVersion.V_6;
1169 return ParseResult.Success;
1170 case "experimental":
1171 settings.Version = LanguageVersion.Experimental;
1172 return ParseResult.Success;
1174 report.Warning (8000, 1, "Language version `future' is no longer supported");
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;
1182 if (value.Length == 0) {
1183 Error_RequiresArgument (option);
1184 return ParseResult.Error;
1189 settings.Encoding = Encoding.UTF8;
1192 settings.Encoding = Encoding.Default;
1196 settings.Encoding = Encoding.GetEncoding (int.Parse (value));
1198 report.Error (2016, "Code page `{0}' is invalid or not installed", value);
1200 return ParseResult.Error;
1202 return ParseResult.Success;
1204 case "runtimemetadataversion":
1205 if (value.Length == 0) {
1206 Error_RequiresArgument (option);
1207 return ParseResult.Error;
1210 settings.RuntimeMetadataVersion = value;
1211 return ParseResult.Success;
1213 // csc options that we don't support
1215 case "/subsystemversion":
1216 case "/highentropyva":
1217 case "/highentropyva+":
1218 case "/highentropyva-":
1219 case "/win32manifest":
1220 case "/nowin32manifest":
1221 return ParseResult.Success;
1224 return ParseResult.UnknownOption;
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.
1233 ParseResult ParseOptionUnix (string arg, ref string[] args, ref int i, CompilerSettings settings)
1237 settings.VerboseParserFlag++;
1238 return ParseResult.Success;
1242 return ParseResult.Stop;
1245 settings.ParseOnly = true;
1246 return ParseResult.Success;
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;
1254 settings.MainClass = args[++i];
1255 return ParseResult.Success;
1258 report.Warning (-29, 1, "Compatibility: Use -unsafe instead of --unsafe");
1259 settings.Unsafe = true;
1260 return ParseResult.Success;
1262 case "/?": case "/h": case "/help":
1265 return ParseResult.Stop;
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;
1274 settings.AddConditionalSymbol (args [++i]);
1275 return ParseResult.Success;
1278 settings.TokenizeOnly = true;
1279 return ParseResult.Success;
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;
1288 settings.OutputFile = args[++i];
1289 return ParseResult.Success;
1292 report.Warning (-29, 1, "Compatibility: Use -checked instead of --checked");
1293 settings.Checked = true;
1294 return ParseResult.Success;
1296 case "--stacktrace":
1297 settings.Stacktrace = true;
1298 return ParseResult.Success;
1300 case "--linkresource":
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;
1308 AddResource (new AssemblyResource (args[++i], args[i]), settings);
1309 return ParseResult.Success;
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;
1319 AddResource (new AssemblyResource (args[++i], args[i], true), settings);
1320 return ParseResult.Success;
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;
1329 string type = args [++i];
1332 settings.Target = Target.Library;
1333 settings.TargetExt = ".dll";
1337 settings.Target = Target.Exe;
1341 settings.Target = Target.WinExe;
1345 settings.Target = Target.Module;
1346 settings.TargetExt = ".dll";
1349 report.Error (2019, "Invalid target type for -target. Valid options are `exe', `winexe', `library' or `module'");
1352 return ParseResult.Success;
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;
1361 string val = args [++i];
1362 int idx = val.IndexOf ('=');
1364 string alias = val.Substring (0, idx);
1365 string assembly = val.Substring (idx + 1);
1366 AddAssemblyReference (alias, assembly, settings);
1367 return ParseResult.Success;
1370 settings.AssemblyReferences.Add (val);
1371 return ParseResult.Success;
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;
1379 settings.ReferencesLookupPaths.Add (args [++i]);
1380 return ParseResult.Success;
1383 settings.EnhancedWarnings = true;
1384 return ParseResult.Success;
1387 report.Warning (-29, 1, "Compatibility: Use -nostdlib instead of --nostdlib");
1388 settings.StdLib = false;
1389 return ParseResult.Success;
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;
1400 warn = int.Parse (args [++i]);
1403 Environment.Exit (1);
1405 settings.SetIgnoreWarning (warn);
1406 return ParseResult.Success;
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;
1415 SetWarningLevel (args [++i], settings);
1416 return ParseResult.Success;
1419 if ((i + 1) >= args.Length){
1420 Error_RequiresArgument (arg);
1421 return ParseResult.Error;
1425 settings.DebugFlags = int.Parse (args [++i]);
1427 Error_RequiresArgument (arg);
1428 return ParseResult.Error;
1431 return ParseResult.Success;
1435 return ParseResult.Stop;
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;
1443 ProcessSourceFiles (args [++i], true, settings.SourceFiles);
1444 return ParseResult.Success;
1447 settings.Timestamps = true;
1448 return ParseResult.Success;
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;
1456 report.Warning (-29, 1, "Compatibility: Use -noconfig option instead of --noconfig");
1457 settings.LoadDefaultReferences = false;
1458 return ParseResult.Success;
1460 case "--metadata-only":
1461 settings.WriteMetadataOnly = true;
1462 return ParseResult.Success;
1464 case "--break-on-ice":
1465 settings.BreakOnInternalError = true;
1466 return ParseResult.Success;
1469 if (arg.StartsWith ("--fatal", StringComparison.Ordinal)) {
1471 if (arg.StartsWith ("--fatal=", StringComparison.Ordinal))
1472 int.TryParse (arg.Substring (8), out fatal);
1474 settings.FatalCounter = fatal;
1475 return ParseResult.Success;
1477 if (arg.StartsWith ("--runtime:", StringComparison.Ordinal)) {
1478 string version = arg.Substring (10);
1483 settings.StdLibRuntimeVersion = RuntimeVersion.v1;
1487 settings.StdLibRuntimeVersion = RuntimeVersion.v2;
1491 settings.StdLibRuntimeVersion = RuntimeVersion.v4;
1494 return ParseResult.Success;
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;
1504 if (settings.GetResourceStrings == null)
1505 settings.GetResourceStrings = new List<string> ();
1507 settings.GetResourceStrings.Add (file);
1508 return ParseResult.Success;
1511 return ParseResult.UnknownOption;
1515 void SetWarningLevel (string s, CompilerSettings settings)
1520 level = int.Parse (s);
1523 if (level < 0 || level > 4) {
1524 report.Error (1900, "Warning level must be in the range 0-4");
1527 settings.WarningLevel = level;
1531 // Given a path specification, splits the path from the file/pattern
1533 static void SplitPathAndPattern (string spec, out string path, out string pattern)
1535 int p = spec.LastIndexOf ('/');
1538 // Windows does not like /file.cs, switch that to:
1543 pattern = spec.Substring (1);
1545 path = spec.Substring (0, p);
1546 pattern = spec.Substring (p + 1);
1551 p = spec.LastIndexOf ('\\');
1553 path = spec.Substring (0, p);
1554 pattern = spec.Substring (p + 1);
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" +
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");
1616 string version = System.Reflection.MethodBase.GetCurrentMethod ().DeclaringType.Assembly.GetName ().Version.ToString ();
1617 output.WriteLine ("Mono C# compiler version {0}", version);
1621 public class RootContext
1624 // Contains the parsed tree
1626 static ModuleContainer root;
1628 static public ModuleContainer ToplevelTypes {
1629 get { return root; }
1630 set { root = value; }