Merge pull request #234 from kripper/master
[mono.git] / mcs / mcs / assembly.cs
index afae0c57af695ede2bf0441fb7e87d46b24e417e..a2af418af5726fac72840bcae2148e8108462998 100644 (file)
@@ -6,7 +6,8 @@
 //   Marek Safar (marek.safar@gmail.com)
 //
 // Copyright 2001, 2002, 2003 Ximian, Inc.
-// Copyright 2004 Novell, Inc.
+// Copyright 2004-2011 Novell, Inc.
+// Copyright 2011 Xamarin Inc
 //
 
 
@@ -49,7 +50,7 @@ namespace Mono.CSharp
                // TODO: make it private and move all builder based methods here
                public AssemblyBuilder Builder;
                protected AssemblyBuilderExtension builder_extra;
-               MonoSymbolWriter symbol_writer;
+               MonoSymbolFile symbol_writer;
 
                bool is_cls_compliant;
                bool wrap_non_exception_throws;
@@ -178,6 +179,12 @@ namespace Mono.CSharp
                        }
                }
 
+               public MonoSymbolFile SymbolWriter {
+                       get {
+                               return symbol_writer;
+                       }
+               }
+
                #endregion
 
                public void AddModule (ImportedModuleDefinition module)
@@ -317,17 +324,16 @@ namespace Mono.CSharp
                                string assembly_name = a.GetString ();
                                if (assembly_name.Length == 0)
                                        return;
-
-                               AssemblyName aname = null;
-                               try {
-                                       aname = new AssemblyName (assembly_name);
-                               } catch (Exception) {
+#if STATIC
+                               ParsedAssemblyName aname;
+                               ParseAssemblyResult r = Fusion.ParseAssemblyName (assembly_name, out aname);
+                               if (r != ParseAssemblyResult.OK) {
                                        Report.Warning (1700, 3, a.Location, "Assembly reference `{0}' is invalid and cannot be resolved",
                                                assembly_name);
                                        return;
                                }
 
-                               if (aname.Version != null || aname.CultureInfo != null || aname.ProcessorArchitecture != ProcessorArchitecture.None) {
+                               if (aname.Version != null || aname.Culture != null || aname.ProcessorArchitecture != ProcessorArchitecture.None) {
                                        Report.Error (1725, a.Location,
                                                "Friend assembly reference `{0}' is invalid. InternalsVisibleTo declarations cannot have a version, culture or processor architecture specified",
                                                assembly_name);
@@ -335,13 +341,13 @@ namespace Mono.CSharp
                                        return;
                                }
 
-                               // TODO: GetPublicKey () does not work on .NET when AssemblyName is constructed from a string
-                               if (public_key != null && aname.GetPublicKey () == null) {
+                               if (public_key != null && !aname.HasPublicKey) {
                                        Report.Error (1726, a.Location,
                                                "Friend assembly reference `{0}' is invalid. Strong named assemblies must specify a public key in their InternalsVisibleTo declarations",
                                                assembly_name);
                                        return;
                                }
+#endif
                        } else if (a.Type == pa.RuntimeCompatibility) {
                                wrap_non_exception_throws_custom = true;
                        } else if (a.Type == pa.AssemblyFileVersion) {
@@ -437,8 +443,8 @@ namespace Mono.CSharp
                {
                        if (Compiler.Settings.Target == Target.Module) {
                                module_target_attrs = new AssemblyAttributesPlaceholder (module, name);
-                               module_target_attrs.CreateType ();
-                               module_target_attrs.DefineType ();
+                               module_target_attrs.CreateContainer ();
+                               module_target_attrs.DefineContainer ();
                                module_target_attrs.Define ();
                                module.AddCompilerGeneratedClass (module_target_attrs);
                        } else if (added_modules != null) {
@@ -446,18 +452,10 @@ namespace Mono.CSharp
                        }
 
                        if (Compiler.Settings.GenerateDebugInfo) {
-                               symbol_writer = new MonoSymbolWriter (file_name);
-
-                               // Register all source files with symbol writer
-                               foreach (var source in Compiler.SourceFiles) {
-                                       source.DefineSymbolInfo (symbol_writer);
-                               }
-
-                               // TODO: global variables
-                               SymbolWriter.symwriter = symbol_writer;
+                               symbol_writer = new MonoSymbolFile ();
                        }
 
-                       module.Emit ();
+                       module.EmitContainer ();
 
                        if (module.HasExtensionMethod) {
                                var pa = module.PredefinedAttributes.Extension;
@@ -484,11 +482,7 @@ namespace Mono.CSharp
                                        Builder.__AddDeclarativeSecurity (entry);
                                }
 #else
-                               var args = new PermissionSet[3];
-                               declarative_security.TryGetValue (SecurityAction.RequestMinimum, out args[0]);
-                               declarative_security.TryGetValue (SecurityAction.RequestOptional, out args[1]);
-                               declarative_security.TryGetValue (SecurityAction.RequestRefuse, out args[2]);
-                               builder_extra.AddPermissionRequests (args);
+                               throw new NotSupportedException ("Assembly-level security");
 #endif
                        }
 
@@ -790,25 +784,38 @@ namespace Mono.CSharp
 
                public void Save ()
                {
-                       PortableExecutableKinds pekind;
+                       PortableExecutableKinds pekind = PortableExecutableKinds.ILOnly;
                        ImageFileMachine machine;
 
                        switch (Compiler.Settings.Platform) {
                        case Platform.X86:
-                               pekind = PortableExecutableKinds.Required32Bit | PortableExecutableKinds.ILOnly;
+                               pekind |= PortableExecutableKinds.Required32Bit;
                                machine = ImageFileMachine.I386;
                                break;
                        case Platform.X64:
-                               pekind = PortableExecutableKinds.ILOnly;
+                               pekind |= PortableExecutableKinds.PE32Plus;
                                machine = ImageFileMachine.AMD64;
                                break;
                        case Platform.IA64:
-                               pekind = PortableExecutableKinds.ILOnly;
                                machine = ImageFileMachine.IA64;
                                break;
+                       case Platform.AnyCPU32Preferred:
+#if STATIC
+                               pekind |= PortableExecutableKinds.Preferred32Bit;
+                               machine = ImageFileMachine.I386;
+                               break;
+#else
+                               throw new NotSupportedException ();
+#endif
+                       case Platform.Arm:
+#if STATIC
+                               machine = ImageFileMachine.ARM;
+                               break;
+#else
+                               throw new NotSupportedException ();
+#endif
                        case Platform.AnyCPU:
                        default:
-                               pekind = PortableExecutableKinds.ILOnly;
                                machine = ImageFileMachine.I386;
                                break;
                        }
@@ -826,10 +833,24 @@ namespace Mono.CSharp
                        Compiler.TimeReporter.Stop (TimeReporter.TimerType.OutputSave);
 
                        // Save debug symbols file
-                       if (symbol_writer != null) {
+                       if (symbol_writer != null && Compiler.Report.Errors == 0) {
                                // TODO: it should run in parallel
                                Compiler.TimeReporter.Start (TimeReporter.TimerType.DebugSave);
-                               symbol_writer.WriteSymbolFile (SymbolWriter.GetGuid (module.Builder));
+
+                               var filename = file_name + ".mdb";
+                               try {
+                                       // We mmap the file, so unlink the previous version since it may be in use
+                                       File.Delete (filename);
+                               } catch {
+                                       // We can safely ignore
+                               }
+
+                               module.WriteDebugSymbol (symbol_writer);
+
+                               using (FileStream fs = new FileStream (filename, FileMode.Create, FileAccess.Write)) {
+                                       symbol_writer.CreateSymbolFile (module.Builder.ModuleVersionId, fs);
+                               }
+
                                Compiler.TimeReporter.Stop (TimeReporter.TimerType.DebugSave);
                        }
                }
@@ -1089,8 +1110,7 @@ namespace Mono.CSharp
 
                public abstract bool HasObjectType (T assembly);
                protected abstract string[] GetDefaultReferences ();
-               public abstract T LoadAssemblyFile (string fileName);
-               public abstract T LoadAssemblyDefault (string assembly);
+               public abstract T LoadAssemblyFile (string fileName, bool isImplicitReference);
                public abstract void LoadReferences (ModuleContainer module);
 
                protected void Error_FileNotFound (string fileName)
@@ -1127,14 +1147,14 @@ namespace Mono.CSharp
                        // Load mscorlib.dll as the first
                        //
                        if (module.Compiler.Settings.StdLib) {
-                               corlib_assembly = LoadAssemblyDefault ("mscorlib.dll");
+                               corlib_assembly = LoadAssemblyFile ("mscorlib.dll", true);
                        } else {
                                corlib_assembly = default (T);
                        }
 
                        T a;
                        foreach (string r in module.Compiler.Settings.AssemblyReferences) {
-                               a = LoadAssemblyFile (r);
+                               a = LoadAssemblyFile (r, false);
                                if (a == null || EqualityComparer<T>.Default.Equals (a, corlib_assembly))
                                        continue;
 
@@ -1152,7 +1172,7 @@ namespace Mono.CSharp
                        }
 
                        foreach (var entry in module.Compiler.Settings.AssemblyReferencesAliases) {
-                               a = LoadAssemblyFile (entry.Item2);
+                               a = LoadAssemblyFile (entry.Item2, false);
                                if (a == null)
                                        continue;
 
@@ -1165,7 +1185,7 @@ namespace Mono.CSharp
 
                        if (compiler.Settings.LoadDefaultReferences) {
                                foreach (string r in GetDefaultReferences ()) {
-                                       a = LoadAssemblyDefault (r);
+                                       a = LoadAssemblyFile (r, true);
                                        if (a == null)
                                                continue;