Address parts of #58244 -- most of what's left is in the runtime
authorRaja R Harinath <harinath@hurrynot.org>
Wed, 8 Nov 2006 15:19:54 +0000 (15:19 -0000)
committerRaja R Harinath <harinath@hurrynot.org>
Wed, 8 Nov 2006 15:19:54 +0000 (15:19 -0000)
* driver.cs (LoadAssembly): Simplify slightly.  Add CS0009 and
CS1509 error checks, and handle them for all assembly loads, not
just the first invocation.
(LoadModule): Likewise.  Move handling of 'adder_method' ...
* codegen.cs (AssemblyClass.AddModule): ... here.

svn path=/trunk/mcs/; revision=67538

mcs/errors/cs0006.cs
mcs/mcs/ChangeLog
mcs/mcs/codegen.cs
mcs/mcs/driver.cs

index 72dd50b54ae3c66d0bc096d9a08951d4fbbee658..298ba25b258ae95c5f0625316a45d317809371c4 100644 (file)
@@ -1,7 +1,7 @@
-// CS0006: Cannot find assembly file `a:b'\r
+// CS0006: cannot find metadata file `a:b'\r
 // Line: 0\r
 // Compiler options: -r:a:b\r
 \r
 class C\r
 {\r
-}
\ No newline at end of file
+}\r
index cf411b5402078220c12c533cfaec42229609f343..31343300392dd7cbdbaa17cd3e29d47e55bf6aec 100644 (file)
@@ -1,3 +1,12 @@
+2006-11-08  Raja R Harinath  <rharinath@novell.com>
+
+       Address parts of #58244 -- most of what's left is in the runtime
+       * driver.cs (LoadAssembly): Simplify slightly.  Add CS0009 and
+       CS1509 error checks, and handle them for all assembly loads, not
+       just the first invocation.
+       (LoadModule): Likewise.  Move handling of 'adder_method' ...
+       * codegen.cs (AssemblyClass.AddModule): ... here.
+
 2006-11-02  Marek Safar  <marek.safar@gmail.com>
 
        * statement.cs.cs (CollectionForeach.TryType): Issue a error when
index 2bd9539df1c0fa9f52504e201354c2a0cfc1c485..7f95441bc020d33c5cf7108c698c8babace64b7d 100644 (file)
@@ -1364,6 +1364,30 @@ namespace Mono.CSharp {
                                return attribute_targets;
                        }
                }
+
+               // Wrapper for AssemblyBuilder.AddModule
+               static MethodInfo adder_method;
+               static public MethodInfo AddModule_Method {
+                       get {
+                               if (adder_method == null)
+                                       adder_method = typeof (AssemblyBuilder).GetMethod ("AddModule", BindingFlags.Instance|BindingFlags.NonPublic);
+                               return adder_method;
+                       }
+               }
+               public Module AddModule (string module)
+               {
+                       MethodInfo m = AddModule_Method;
+                       if (m == null) {
+                               Report.RuntimeMissingSupport (Location.Null, "/addmodule");
+                               Environment.Exit (1);
+                       }
+
+                       try {
+                               return (Module) m.Invoke (Builder, new object [] { module });
+                       } catch (TargetInvocationException ex) {
+                               throw ex.InnerException;
+                       }
+               }               
        }
 
        public class ModuleClass : CommonAssemblyModulClass {
index a21a326adb5fff20866599d55089051947d33a58..ec82e7fefc712016d394f72b7d6dd9714471ecb7 100644 (file)
@@ -314,103 +314,124 @@ namespace Mono.CSharp
                        LoadAssembly (assembly, null, soft);
                }
 
+               static void Error6 (string name, string log)
+               {
+                       if (log != null && log.Length > 0)
+                               Report.ExtraInformation (Location.Null, "Log:\n" + log + "\n(log related to previous ");
+                       Report.Error (6, "cannot find metadata file `{0}'", name);
+               }
+
+               static void Error9 (string type, string filename, string log)
+               {
+                       if (log != null && log.Length > 0)
+                               Report.ExtraInformation (Location.Null, "Log:\n" + log + "\n(log related to previous ");
+                       Report.Error (9, "file `{0}' has invalid `{1}' metadata", filename, type);
+               }
+
+               static void BadAssembly (string filename, string log)
+               {
+                       MethodInfo adder_method = AssemblyClass.AddModule_Method;
+
+                       if (adder_method != null) {
+                               AssemblyName an = new AssemblyName ();
+                               an.Name = ".temp";
+                               AssemblyBuilder ab = AppDomain.CurrentDomain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Run);
+                               if (adder_method.Invoke (ab, new object [] { filename }) != null) {
+                                       Report.Error (1509, "referenced file `{0}' is not an assembly; try using the '-addmodule' option", filename);
+                                       return;
+                               }
+                       }
+                       Error9 ("assembly", filename, log);
+               }
+
                static public void LoadAssembly (string assembly, string alias, bool soft)
                {
-                       Assembly a;
+                       Assembly a = null;
                        string total_log = "";
 
                        try {
-                               char[] path_chars = { '/', '\\' };
+                               try {
+                                       char[] path_chars = { '/', '\\' };
 
-                               if (assembly.IndexOfAny (path_chars) != -1) {
-                                       a = Assembly.LoadFrom (assembly);
-                               } else {
-                                       string ass = assembly;
-                                       if (ass.EndsWith (".dll") || ass.EndsWith (".exe"))
-                                               ass = assembly.Substring (0, assembly.Length - 4);
-                                       a = Assembly.Load (ass);
+                                       if (assembly.IndexOfAny (path_chars) != -1) {
+                                               a = Assembly.LoadFrom (assembly);
+                                       } else {
+                                               string ass = assembly;
+                                               if (ass.EndsWith (".dll") || ass.EndsWith (".exe"))
+                                                       ass = assembly.Substring (0, assembly.Length - 4);
+                                               a = Assembly.Load (ass);
+                                       }
+                               } catch (FileNotFoundException) {
+                                       bool err = !soft;
+                                       foreach (string dir in link_paths) {
+                                               string full_path = Path.Combine (dir, assembly);
+                                               if (!assembly.EndsWith (".dll") && !assembly.EndsWith (".exe"))
+                                                       full_path += ".dll";
+
+                                               try {
+                                                       a = Assembly.LoadFrom (full_path);
+                                                       err = false;
+                                                       break;
+                                               } catch (FileNotFoundException ff) {
+                                                       total_log += ff.FusionLog;
+                                               }
+                                       }
+                                       if (err) {
+                                               Error6 (assembly, total_log);
+                                               return;
+                                       }
                                }
+
                                // Extern aliased refs require special handling
                                if (alias == null)
                                        RootNamespace.Global.AddAssemblyReference (a);
                                else
                                        RootNamespace.DefineRootNamespace (alias, a);
 
-                       } catch (FileNotFoundException){
-                               foreach (string dir in link_paths){
-                                       string full_path = Path.Combine (dir, assembly);
-                                       if (!assembly.EndsWith (".dll") && !assembly.EndsWith (".exe"))
-                                               full_path += ".dll";
-
-                                       try {
-                                               a = Assembly.LoadFrom (full_path);
-                                               if (alias == null)
-                                                       RootNamespace.Global.AddAssemblyReference (a);
-                                               else
-                                                       RootNamespace.DefineRootNamespace (alias, a);
-                                               return;
-                                       } catch (FileNotFoundException ff) {
-                                               total_log += ff.FusionLog;
-                                               continue;
-                                       }
-                               }
-                               if (!soft) {
-                                       Report.Error (6, "Cannot find assembly file `{0}'", assembly);
-                                       Report.Stderr.WriteLine ("Log: \n" + total_log);
-                               }
                        } catch (BadImageFormatException f) {
-                               Report.Error(6, "Cannot load assembly (bad file format)" + f.FusionLog);
-                       } catch (FileLoadException f){
-                               Report.SymbolRelatedToPreviousError (Location.Null, f.FusionLog);
-                               Report.Error(6, "Cannot load assembly file `{0}'", f.FileName);
-                       } catch (ArgumentNullException){
-                               Report.Error(6, "Cannot load assembly (null argument)");
+                               // .NET 2.0 throws this if we try to load a module without an assembly manifest ...
+                               BadAssembly (f.FileName, f.FusionLog);
+                       } catch (FileLoadException f) {
+                               // ... while .NET 1.1 throws this
+                               BadAssembly (f.FileName, f.FusionLog);
                        }
                }
 
-               static public void LoadModule (MethodInfo adder_method, string module)
+               static public void LoadModule (string module)
                {
-                       Module m;
+                       Module m = null;
                        string total_log = "";
 
                        try {
                                try {
-                                       m = (Module)adder_method.Invoke (CodeGen.Assembly.Builder, new object [] { module });
-                               }
-                               catch (TargetInvocationException ex) {
-                                       throw ex.InnerException;
-                               }
-                               RootNamespace.Global.AddModuleReference (m);
+                                       m = CodeGen.Assembly.AddModule (module);
+                               } catch (FileNotFoundException) {
+                                       bool err = true;
+                                       foreach (string dir in link_paths) {
+                                               string full_path = Path.Combine (dir, module);
+                                               if (!module.EndsWith (".netmodule"))
+                                                       full_path += ".netmodule";
 
-                       } 
-                       catch (FileNotFoundException){
-                               foreach (string dir in link_paths){
-                                       string full_path = Path.Combine (dir, module);
-                                       if (!module.EndsWith (".netmodule"))
-                                               full_path += ".netmodule";
-
-                                       try {
                                                try {
-                                                       m = (Module)adder_method.Invoke (CodeGen.Assembly.Builder, new object [] { full_path });
-                                               }
-                                               catch (TargetInvocationException ex) {
-                                                       throw ex.InnerException;
+                                                       m = CodeGen.Assembly.AddModule (full_path);
+                                                       err = false;
+                                                       break;
+                                               } catch (FileNotFoundException ff) {
+                                                       total_log += ff.FusionLog;
                                                }
-                                               RootNamespace.Global.AddModuleReference (m);
+                                       }
+                                       if (err) {
+                                               Error6 (module, total_log);
                                                return;
-                                       } catch (FileNotFoundException ff) {
-                                               total_log += ff.FusionLog;
-                                               continue;
                                        }
                                }
-                               Report.Error (6, "Cannot find module `" + module + "'" );
-                               Console.WriteLine ("Log: \n" + total_log);
+
+                               RootNamespace.Global.AddModuleReference (m);
+
                        } catch (BadImageFormatException f) {
-                               Report.Error(6, "Cannot load module (bad file format)" + f.FusionLog);
-                       } catch (FileLoadException f){
-                               Report.Error(6, "Cannot load module " + f.FusionLog);
-                       } catch (ArgumentNullException){
-                               Report.Error(6, "Cannot load module (null argument)");
+                               Error9 ("module", f.FileName, f.FusionLog);
+                       } catch (FileLoadException f) {
+                               Error9 ("module", f.FileName, f.FusionLog);
                        }
                }
 
@@ -1626,14 +1647,8 @@ namespace Mono.CSharp
                        RootNamespace.Global.AddModuleReference (CodeGen.Module.Builder);
 
                        if (modules.Count > 0) {
-                               MethodInfo adder_method = typeof (AssemblyBuilder).GetMethod ("AddModule", BindingFlags.Instance|BindingFlags.NonPublic);
-                               if (adder_method == null) {
-                                       Report.RuntimeMissingSupport (Location.Null, "/addmodule");
-                                       Environment.Exit (1);
-                               }
-
                                foreach (string module in modules)
-                                       LoadModule (adder_method, module);
+                                       LoadModule (module);
                        }
                        
                        //