Fix warning.
[mono.git] / mcs / gmcs / driver.cs
index 37c27affbd5fcd861aadbd6611feb1d58b1dee6e..63d95d355c1fca7c30265f44e6ea8c372a90ff5b 100644 (file)
@@ -6,7 +6,7 @@
 // Licensed under the terms of the GNU GPL
 //
 // (C) 2001, 2002, 2003 Ximian, Inc (http://www.ximian.com)
-// (C) 2004 Novell, Inc
+// (C) 2004, 2005 Novell, Inc
 //
 
 namespace Mono.CSharp
@@ -92,16 +92,11 @@ namespace Mono.CSharp
                static DateTime last_time, first_time;
 
                //
-               // Encoding: ISO-Latin1 is 28591
+               // Encoding.
                //
+               static Encoding default_encoding;
                static Encoding encoding;
 
-               //
-               // Whether the user has specified a different encoder manually
-               //
-               static bool using_default_encoder = true;
-
-
                static public void Reset ()
                {
                        want_debugging_support = false;
@@ -114,8 +109,8 @@ namespace Mono.CSharp
                        win32ResourceFile = win32IconFile = null;
                        defines = null;
                        output_file = null;
-                       encoding = null;
-                       using_default_encoder = true;
+                       encoding = default_encoding = null;
+                       first_source = null;
                }
 
                public static void ShowTime (string msg)
@@ -153,12 +148,12 @@ namespace Mono.CSharp
                        try {
                                input = File.OpenRead (file.Name);
                        } catch {
-                               Report.Error (2001, "Source file '" + file.Name + "' could not be opened");
+                               Report.Error (2001, "Source file `" + file.Name + "' could not be found");
                                return;
                        }
 
                        using (input){
-                               SeekableStreamReader reader = new SeekableStreamReader (input, encoding, using_default_encoder);
+                               SeekableStreamReader reader = new SeekableStreamReader (input, encoding);
                                Tokenizer lexer = new Tokenizer (reader, file, defines);
                                int token, tokens = 0, errors = 0;
 
@@ -182,13 +177,22 @@ namespace Mono.CSharp
                        try {
                                input = File.OpenRead (file.Name);
                        } catch {
-                               Report.Error (2001, "Source file '" + file.Name + "' could not be opened");
+                               Report.Error (2001, "Source file `" + file.Name + "' could not be found");
                                return;
                        }
 
-                       SeekableStreamReader reader = new SeekableStreamReader (input, encoding, using_default_encoder);
-                               
+                       SeekableStreamReader reader = new SeekableStreamReader (input, encoding);
+
+                       // Check 'MZ' header
+                       if (reader.Read () == 77 && reader.Read () == 90) {
+                               Report.Error (2015, "Source file `{0}' is a binary file and not a text file", file.Name);
+                               input.Close ();
+                               return;
+                       }
+
+                       reader.Position = 0;
                        parser = new CSharpParser (reader, file, defines);
+                       parser.ErrorOutput = Report.Stderr;
                        try {
                                parser.parse ();
                        } catch (Exception ex) {
@@ -206,6 +210,7 @@ namespace Mono.CSharp
                                "   --parse            Only parses the source file\n" +
                                "   --stacktrace       Shows stack trace at error location\n" +
                                "   --timestamp        Displays time stamps of various compiler events\n" +
+                               "   --expect-error X   Expect that error X will be encountered\n" +
                                "   -2                 Enables experimental C# features\n" +
                                "   -v                 Verbose parsing (for debugging the parser)\n" + 
                                "   --mcs-debug X      Sets MCS debugging level to X\n");
@@ -214,31 +219,28 @@ namespace Mono.CSharp
                static void Usage ()
                {
                        Console.WriteLine (
-                               "Mono C# compiler, (C) 2001 - 2003 Ximian, Inc.\n" +
+                               "Mono C# compiler, (C) 2001 - 2005 Novell, Inc.\n" +
                                "mcs [options] source-files\n" +
                                "   --about            About the Mono C# compiler\n" +
                                "   -addmodule:MODULE  Adds the module to the generated assembly\n" + 
                                "   -checked[+|-]      Set default context to checked\n" +
-                               "   -codepage:ID       Sets code page to the one in ID\n" +
-                               "                      (number, `utf8' or `reset')\n" +
+                               "   -codepage:ID       Sets code page to the one in ID (number, utf8, reset)\n" +
                                "   -clscheck[+|-]     Disables CLS Compliance verifications" + Environment.NewLine +
                                "   -define:S1[;S2]    Defines one or more symbols (short: /d:)\n" +
-                               "   -debug[+|-]        Generate debugging information\n" + 
+                               "   -debug[+|-], -g    Generate debugging information\n" + 
                                "   -delaysign[+|-]    Only insert the public key into the assembly (no signing)\n" +
                                "   -doc:FILE          XML Documentation file to generate\n" + 
-                               "   -g                 Generate debugging information\n" +
                                "   -keycontainer:NAME The key pair container used to strongname the assembly\n" +
                                "   -keyfile:FILE      The strongname key file used to strongname the assembly\n" +
-                               "   -langversion:TEXT  Specifies language version modes: ISO-1 or Default" + Environment.NewLine +
+                               "   -langversion:TEXT  Specifies language version modes: ISO-1 or Default\n" + 
                                "   -lib:PATH1,PATH2   Adds the paths to the assembly link path\n" +
                                "   -main:class        Specified the class that contains the entry point\n" +
                                "   -noconfig[+|-]     Disables implicit references to assemblies\n" +
                                "   -nostdlib[+|-]     Does not load core libraries\n" +
                                "   -nowarn:W1[,W2]    Disables one or more warnings\n" + 
-                               "   -optimize[+|-]     Enables code optimalizations" + Environment.NewLine +
+                               "   -optimize[+|-]     Enables code optimalizations\n" + 
                                "   -out:FNAME         Specifies output file\n" +
                                "   -pkg:P1[,Pn]       References packages P1..Pn\n" + 
-                               "   --expect-error X   Expect that error X will be encountered\n" +
                                "   -recurse:SPEC      Recursively compiles the files in SPEC ([dir]/file)\n" + 
                                "   -reference:ASS     References the specified assembly (-r:ASS)\n" +
                                "   -target:KIND       Specifies the target (KIND is one of: exe, winexe,\n" +
@@ -259,13 +261,13 @@ namespace Mono.CSharp
 
                static void TargetUsage ()
                {
-                       Report.Error (2019, "Valid options for -target: are exe, winexe, library or module");
+                       Report.Error (2019, "Invalid target type for -target. Valid options are `exe', `winexe', `library' or `module'");
                }
                
                static void About ()
                {
                        Console.WriteLine (
-                               "The Mono C# compiler is (C) 2001, 2002, 2003 Ximian, Inc.\n\n" +
+                               "The Mono C# compiler is (C) 2001-2005, Novell, Inc.\n\n" +
                                "The compiler source code is released under the terms of the GNU GPL\n\n" +
 
                                "For more information on Mono, visit the project Web site\n" +
@@ -280,6 +282,8 @@ namespace Mono.CSharp
                public static int Main (string[] args)
                {
                        RootContext.Version = LanguageVersion.Default;
+                       Location.InEmacs = Environment.GetEnvironmentVariable ("EMACS") == "t";
+
                        bool ok = MainDriver (args);
                        
                        if (ok && Report.Errors == 0) {
@@ -603,25 +607,26 @@ namespace Mono.CSharp
                                soft_references.Insert (p++, def);
                }
 
-               static void SetOutputFile (string name)
+               public static string OutputFile
                {
-                       output_file = name;
+                       set {
+                               output_file = value;
+                       }
+                       get {
+                               return Path.GetFileName (output_file);
+                       }
                }
 
                static void SetWarningLevel (string s)
                {
-                       int level = 0;
+                       int level = -1;
 
                        try {
                                level = Int32.Parse (s);
                        } catch {
-                               Report.Error (
-                                       1900,
-                                       "--wlevel requires a value from 0 to 4");
-                               return;
                        }
                        if (level < 0 || level > 4){
-                               Report.Error (1900, "Warning level must be 0 to 4");
+                               Report.Error (1900, "Warning level must be in the range 0-4");
                                return;
                        }
                        RootContext.WarningLevel = level;
@@ -661,6 +666,7 @@ namespace Mono.CSharp
                                return true;
                                
                        case "--main": case "-m":
+                               Report.Warning (-29, "Compatibility: Use -main:CLASS instead of --main CLASS or -m CLASS");
                                if ((i + 1) >= args.Length){
                                        Usage ();
                                        Environment.Exit (1);
@@ -669,6 +675,7 @@ namespace Mono.CSharp
                                return true;
                                
                        case "--unsafe":
+                               Report.Warning (-29, "Compatibility: Use -unsafe instead of --unsafe");
                                RootContext.Unsafe = true;
                                return true;
                                
@@ -679,6 +686,7 @@ namespace Mono.CSharp
                                return true;
                                
                        case "--define":
+                               Report.Warning (-29, "Compatibility: Use -d:SYMBOL instead of --define SYMBOL");
                                if ((i + 1) >= args.Length){
                                        Usage ();
                                        Environment.Exit (1);
@@ -709,14 +717,16 @@ namespace Mono.CSharp
                                
                        case "-o": 
                        case "--output":
+                               Report.Warning (-29, "Compatibility: Use -out:FILE instead of --output FILE or -o FILE");
                                if ((i + 1) >= args.Length){
                                        Usage ();
                                        Environment.Exit (1);
                                }
-                               SetOutputFile (args [++i]);
+                               OutputFile = args [++i];
                                return true;
-                               
+
                        case "--checked":
+                               Report.Warning (-29, "Compatibility: Use -checked instead of --checked");
                                RootContext.Checked = true;
                                return true;
                                
@@ -726,6 +736,7 @@ namespace Mono.CSharp
                                
                        case "--linkresource":
                        case "--linkres":
+                               Report.Warning (-29, "Compatibility: Use -linkres:VALUE instead of --linkres VALUE");
                                if ((i + 1) >= args.Length){
                                        Usage ();
                                        Report.Error (5, "Missing argument to --linkres"); 
@@ -739,6 +750,7 @@ namespace Mono.CSharp
                                
                        case "--resource":
                        case "--res":
+                               Report.Warning (-29, "Compatibility: Use -res:VALUE instead of --res VALUE");
                                if ((i + 1) >= args.Length){
                                        Usage ();
                                        Report.Error (5, "Missing argument to --resource"); 
@@ -751,6 +763,7 @@ namespace Mono.CSharp
                                return true;
                                
                        case "--target":
+                               Report.Warning (-29, "Compatibility: Use -target:KIND instead of --target KIND");
                                if ((i + 1) >= args.Length){
                                        Environment.Exit (1);
                                        return true;
@@ -782,6 +795,7 @@ namespace Mono.CSharp
                                return true;
                                
                        case "-r":
+                               Report.Warning (-29, "Compatibility: Use -r:LIBRARY instead of -r library");
                                if ((i + 1) >= args.Length){
                                        Usage ();
                                        Environment.Exit (1);
@@ -791,6 +805,7 @@ namespace Mono.CSharp
                                return true;
                                
                        case "-L":
+                               Report.Warning (-29, "Compatibility: Use -lib:ARG instead of --L arg");
                                if ((i + 1) >= args.Length){
                                        Usage ();       
                                        Environment.Exit (1);
@@ -799,6 +814,7 @@ namespace Mono.CSharp
                                return true;
                                
                        case "--nostdlib":
+                               Report.Warning (-29, "Compatibility: Use -nostdlib instead of --nostdlib");
                                RootContext.StdLib = false;
                                return true;
                                
@@ -807,10 +823,12 @@ namespace Mono.CSharp
                                return true;
                                
                        case "--werror":
+                               Report.Warning (-29, "Compatibility: Use -warnaserror: option instead of --werror");
                                Report.WarningsAreErrors = true;
                                return true;
                                
                        case "--nowarn":
+                               Report.Warning (-29, "Compatibility: Use -nowarn instead of --nowarn");
                                if ((i + 1) >= args.Length){
                                        Usage ();
                                        Environment.Exit (1);
@@ -827,6 +845,7 @@ namespace Mono.CSharp
                                return true;
                                
                        case "--wlevel":
+                               Report.Warning (-29, "Compatibility: Use -warn:LEVEL instead of --wlevel LEVEL");
                                if ((i + 1) >= args.Length){
                                        Report.Error (
                                                1900,
@@ -856,6 +875,7 @@ namespace Mono.CSharp
                                return true;
                                
                        case "--recurse":
+                               Report.Warning (-29, "Compatibility: Use -recurse:PATTERN option instead --recurse PATTERN");
                                if ((i + 1) >= args.Length){
                                        Report.Error (5, "--recurse requires an argument");
                                        Environment.Exit (1);
@@ -873,10 +893,12 @@ namespace Mono.CSharp
                                return true;
                                
                        case "--debug": case "-g":
+                               Report.Warning (-29, "Compatibility: Use -debug option instead of -g or --debug");
                                want_debugging_support = true;
                                return true;
                                
                        case "--noconfig":
+                               Report.Warning (-29, "Compatibility: Use -noconfig option instead of --noconfig");
                                load_default_config = false;
                                return true;
                        }
@@ -885,8 +907,8 @@ namespace Mono.CSharp
                }
 
                //
-               // Currently it is very basic option parsing, but eventually, this will
-               // be the complete option parser
+               // This parses the -arg and /arg options to the compiler, even if the strings
+               // in the following text use "/arg" on the strings.
                //
                static bool CSCParseOption (string option, ref string [] args, ref int i)
                {
@@ -938,7 +960,7 @@ namespace Mono.CSharp
                                        Usage ();
                                        Environment.Exit (1);
                                }
-                               SetOutputFile (value);
+                               OutputFile = value;
                                return true;
 
                        case "/optimize":
@@ -972,6 +994,13 @@ namespace Mono.CSharp
                                return true;
                        }
 
+                       case "/bugreport":
+                               //
+                               // We should collect data, runtime, etc and store in the file specified
+                               //
+                               Console.WriteLine ("To file bug reports, please visit: http://www.mono-project.com/Bugs");
+                               return true;
+
                        case "/linkres":
                        case "/linkresource":
                                if (value == ""){
@@ -1037,10 +1066,10 @@ namespace Mono.CSharp
                                        embedded_resources = new ArrayList ();
                                
                                if (embedded_resources.Contains (value)) {
-                                       Report.Error (1508, String.Format ("The resource identifier '{0}' has already been used in this assembly.", value));
+                                       Report.Error (1508, String.Format ("The resource identifier `{0}' has already been used in this assembly.", value));
                                }
                                else if (value.IndexOf (',') != -1 && embedded_resources.Contains (value.Split (',')[1])) {
-                                       Report.Error (1508, String.Format ("The resource identifier '{0}' has already been used in this assembly.", value));
+                                       Report.Error (1508, String.Format ("The resource identifier `{0}' has already been used in this assembly.", value));
                                }
                                else {
                                embedded_resources.Add (value);
@@ -1185,7 +1214,7 @@ namespace Mono.CSharp
                                                }
                                                Report.SetIgnoreWarning (warn);
                                        } catch {
-                                               Report.Error (1904, String.Format("'{0}' is not a valid warning number", wc));
+                                               Report.Error (1904, String.Format("`{0}' is not a valid warning number", wc));
                                        }
                                }
                                return true;
@@ -1269,39 +1298,37 @@ namespace Mono.CSharp
                                                SetupV2 ();
                                                return true;
                                }
-                               Report.Error (1617, "Invalid option '{0}' for /langversion; must be ISO-1 or Default", value);
+                               Report.Error (1617, "Invalid option `{0}' for /langversion. It must be either `ISO-1' or `Default'", value);
                                return true;
 
                        case "/codepage":
-                               int cp = -1;
-
-                               if (value == "utf8"){
+                               switch (value) {
+                               case "utf8":
                                        encoding = new UTF8Encoding();
-                                       using_default_encoder = false;
-                                       return true;
-                               }
-                               if (value == "reset"){
-                                       //
-                                       // 28591 is the code page for ISO-8859-1 encoding.
-                                       //
-                                       cp = 28591;
-                                       using_default_encoder = true;
-                               }
-                               
-                               try {
-                                       cp = Int32.Parse (value);
-                                       encoding = Encoding.GetEncoding (cp);
-                                       using_default_encoder = false;
-                               } catch {
-                                       Report.Error (2016, String.Format("Code page '{0}' is invalid or not installed", cp));
+                                       break;
+                               case "reset":
+                                       encoding = default_encoding;
+                                       break;
+                               default:
+                                       try {
+                                               encoding = Encoding.GetEncoding (
+                                               Int32.Parse (value));
+                                       } catch {
+                                               Report.Error (2016, "Code page `{0}' is invalid or not installed", value);
+                                       }
+                                       break;
                                }
                                return true;
                        }
-                       //Report.Error (2007, String.Format ("Unrecognized command-line option: '{0}'", option));
-                       //Environment.Exit (1);
+
                        return false;
                }
                
+               static void Error_WrongOption (string option)
+               {
+                       Report.Error (2007, "Unrecognized command-line option: `{0}'", option);
+               }
+
                static string [] AddArgs (string [] args, string [] extra_args)
                {
                        string [] new_args;
@@ -1342,12 +1369,14 @@ namespace Mono.CSharp
                        bool parsing_options = true;
 
                        try {
-                               encoding = Encoding.GetEncoding (28591);
-                       } catch {
-                               Console.WriteLine ("Error: could not load encoding 28591, trying 1252");
-                               encoding = Encoding.GetEncoding (1252);
+                               // Latin1
+                               default_encoding = Encoding.GetEncoding (28591);
+                       } catch (Exception) {
+                               // iso-8859-1
+                               default_encoding = Encoding.GetEncoding (1252);
                        }
-                       
+                       encoding = default_encoding;
+
                        references = new ArrayList ();
                        soft_references = new ArrayList ();
                        modules = new ArrayList ();
@@ -1410,10 +1439,19 @@ namespace Mono.CSharp
                                                string csc_opt = "/" + arg.Substring (1);
                                                if (CSCParseOption (csc_opt, ref args, ref i))
                                                        continue;
+
+                                               Error_WrongOption (arg);
+                                               return false;
                                        } else {
-                                               if (arg.StartsWith ("/")){
+                                               if (arg [0] == '/'){
                                                        if (CSCParseOption (arg, ref args, ref i))
                                                                continue;
+
+                                                       // Need to skip `/home/test.cs' however /test.cs is considered as error
+                                                       if (arg.Length < 2 || arg.IndexOf ('/', 2) == -1) {
+                                                               Error_WrongOption (arg);
+                                                               return false;
+                                                       }
                                                }
                                        }
                                }
@@ -1545,6 +1583,8 @@ namespace Mono.CSharp
                        if (timestamps)
                                ShowTime ("   Core Types done");
 
+                       CodeGen.Module.ResolveAttributes ();
+
                        //
                        // The second pass of the compiler
                        //
@@ -1559,6 +1599,9 @@ namespace Mono.CSharp
                        if (!RootContext.StdLib)
                                RootContext.BootCorlib_PopulateCoreTypes ();
                        RootContext.PopulateTypes ();
+
+                       TypeManager.InitCodeHelpers ();
+
                        RootContext.DefineTypes ();
                        
                        if (RootContext.Documentation != null &&
@@ -1566,8 +1609,6 @@ namespace Mono.CSharp
                                        output_file))
                                return false;
 
-                       TypeManager.InitCodeHelpers ();
-
                        //
                        // Verify using aliases now
                        //
@@ -1577,14 +1618,15 @@ namespace Mono.CSharp
                                return false;
                        }
                        
-                       if (RootContext.VerifyClsCompliance) { 
+                       if (RootContext.VerifyClsCompliance) {
                                CodeGen.Assembly.ResolveClsCompliance ();
                                if (CodeGen.Assembly.IsClsCompliant) {
                                AttributeTester.VerifyModulesClsCompliance ();
                                TypeManager.LoadAllImportedTypes ();
-                                       AttributeTester.VerifyTopLevelNameClsCompliance ();
                                }
                        }
+                       if (Report.Errors > 0)
+                               return false;
                        
                        //
                        // The code generator
@@ -1622,30 +1664,30 @@ namespace Mono.CSharp
 
                                if (ep == null) {
                                        if (RootContext.MainClass != null) {
-                                               DeclSpace main_cont = RootContext.Tree.GetDecl (MemberName.FromDotted (RootContext.MainClass));
+                                               DeclSpace main_cont = RootContext.Tree.GetDecl (MemberName.FromDotted (RootContext.MainClass, Location.Null));
                                                if (main_cont == null) {
-                                                       Report.Error (1555, output_file, "Could not find '{0}' specified for Main method", RootContext.MainClass); 
+                                                       Report.Error (1555, "Could not find `{0}' specified for Main method", RootContext.MainClass); 
                                                        return false;
                                                }
 
                                                if (!(main_cont is ClassOrStruct)) {
-                                                       Report.Error (1556, output_file, "'{0}' specified for Main method must be a valid class or struct", RootContext.MainClass);
+                                                       Report.Error (1556, "`{0}' specified for Main method must be a valid class or struct", RootContext.MainClass);
                                                        return false;
                                                }
 
-                                               Report.Error (1558, main_cont.Location, "'{0}' does not have a suitable static Main method", main_cont.GetSignatureForError ());
+                                               Report.Error (1558, main_cont.Location, "`{0}' does not have a suitable static Main method", main_cont.GetSignatureForError ());
                                                return false;
                                        }
 
                                        if (Report.Errors == 0)
-                                               Report.Error (5001, "Program " + output_file +
-                                                             " does not have an entry point defined");
+                                               Report.Error (5001, "Program `{0}' does not contain a static `Main' method suitable for an entry point",
+                                                       output_file);
                                        return false;
                                }
                                
                                CodeGen.Assembly.Builder.SetEntryPoint (ep, k);
                        } else if (RootContext.MainClass != null) {
-                               Report.Error (2017, "Can not specify -main: when building module or library");
+                               Report.Error (2017, "Cannot specify -main if building a module or library");
                        }
 
                        //
@@ -1763,18 +1805,18 @@ namespace Mono.CSharp
        // This is the only public entry point
        //
        public class CompilerCallableEntryPoint : MarshalByRefObject {
-               static bool used = false;
-               
-               public static bool InvokeCompiler (string [] args)
+               public static bool InvokeCompiler (string [] args, TextWriter error)
                {
-                       if (used)
+                       Report.Stderr = error;
+                       try {
+                               return Driver.MainDriver (args) && Report.Errors == 0;
+                       }
+                       finally {
+                               Report.Stderr = Console.Error;
                                Reset ();
-                       else
-                               used = true;
-
-                       return Driver.MainDriver (args) && Report.Errors == 0;
+                       }
                }
-
+               
                static void Reset ()
                {
                        Driver.Reset ();