Merge branch 'master' into msbuilddll2
[mono.git] / mcs / mcs / assembly.cs
index 1539340009f475ec2c2ca29eaf0231a24fecc6b8..6c99245480b93f6766af3adb8b86f5beddb78c0c 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-2013 Xamarin Inc
 //
 
 
@@ -35,7 +36,6 @@ namespace Mono.CSharp
        public interface IAssemblyDefinition
        {
                string FullName { get; }
-               bool HasExtensionMethod { get; }
                bool IsCLSCompliant { get; }
                bool IsMissing { get; }
                string Name { get; }
@@ -49,11 +49,12 @@ 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;
                bool wrap_non_exception_throws_custom;
+               bool has_user_debuggable;
 
                protected ModuleContainer module;
                readonly string name;
@@ -74,6 +75,9 @@ namespace Mono.CSharp
                Dictionary<ITypeDefinition, Attribute> emitted_forwarders;
                AssemblyAttributesPlaceholder module_target_attrs;
 
+               // Win32 version info values
+               string vi_product, vi_product_version, vi_company, vi_copyright, vi_trademark;
+
                protected AssemblyDefinition (ModuleContainer module, string name)
                {
                        this.module = module;
@@ -131,12 +135,6 @@ namespace Mono.CSharp
                        }
                }
 
-               public bool HasExtensionMethod {
-                       get {
-                               return module.HasExtensionMethod;
-                       }
-               }
-
                public bool HasCLSCompliantAttribute {
                        get {
                                return cls_attribute != null;
@@ -160,6 +158,8 @@ namespace Mono.CSharp
                        }
                }
 
+               public bool IsSatelliteAssembly { get; private set; }
+
                public string Name {
                        get {
                                return name;
@@ -178,6 +178,12 @@ namespace Mono.CSharp
                        }
                }
 
+               public MonoSymbolFile SymbolWriter {
+                       get {
+                               return symbol_writer;
+                       }
+               }
+
                #endregion
 
                public void AddModule (ImportedModuleDefinition module)
@@ -214,6 +220,7 @@ namespace Mono.CSharp
                                        builder_extra.SetCulture (value, a.Location);
                                }
 
+                               IsSatelliteAssembly = true;
                                return;
                        }
 
@@ -285,7 +292,7 @@ namespace Mono.CSharp
                                } else if (emitted_forwarders.ContainsKey (t.MemberDefinition)) {
                                        Report.SymbolRelatedToPreviousError (emitted_forwarders[t.MemberDefinition].Location, null);
                                        Report.Error (739, a.Location, "A duplicate type forward of type `{0}'",
-                                               TypeManager.CSharpName (t));
+                                               t.GetSignatureForError ());
                                        return;
                                }
 
@@ -294,13 +301,13 @@ namespace Mono.CSharp
                                if (t.MemberDefinition.DeclaringAssembly == this) {
                                        Report.SymbolRelatedToPreviousError (t);
                                        Report.Error (729, a.Location, "Cannot forward type `{0}' because it is defined in this assembly",
-                                               TypeManager.CSharpName (t));
+                                               t.GetSignatureForError ());
                                        return;
                                }
 
                                if (t.IsNested) {
                                        Report.Error (730, a.Location, "Cannot forward type `{0}' because it is a nested type",
-                                               TypeManager.CSharpName (t));
+                                               t.GetSignatureForError ());
                                        return;
                                }
 
@@ -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,25 +341,36 @@ 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) {
-                               string value = a.GetString ();
-                               if (string.IsNullOrEmpty (value) || IsValidAssemblyVersion (value, false) == null) {
+                               vi_product_version = a.GetString ();
+                               if (string.IsNullOrEmpty (vi_product_version) || IsValidAssemblyVersion (vi_product_version, false) == null) {
                                        Report.Warning (1607, 1, a.Location, "The version number `{0}' specified for `{1}' is invalid",
-                                               value, a.Name);
+                                               vi_product_version, a.Name);
                                        return;
                                }
+                       } else if (a.Type == pa.AssemblyProduct) {
+                               vi_product = a.GetString ();
+                       } else if (a.Type == pa.AssemblyCompany) {
+                               vi_company = a.GetString ();
+                       } else if (a.Type == pa.AssemblyDescription) {
+                               // TODO: Needs extra api
+                       } else if (a.Type == pa.AssemblyCopyright) {
+                               vi_copyright = a.GetString ();
+                       } else if (a.Type == pa.AssemblyTrademark) {
+                               vi_trademark = a.GetString ();
+                       } else if (a.Type == pa.Debuggable) {
+                               has_user_debuggable = true;
                        }
 
-
                        SetCustomAttribute (ctor, cdata);
                }
 
@@ -368,7 +385,7 @@ namespace Mono.CSharp
                        // no working SRE API
                        foreach (var entry in Importer.Assemblies) {
                                var a = entry as ImportedAssemblyDefinition;
-                               if (a == null)
+                               if (a == null || a.IsMissing)
                                        continue;
 
                                if (public_key != null && !a.HasStrongName) {
@@ -437,8 +454,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 +463,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;
@@ -466,30 +475,39 @@ namespace Mono.CSharp
                                }
                        }
 
-                       if (!wrap_non_exception_throws_custom) {
-                               PredefinedAttribute pa = module.PredefinedAttributes.RuntimeCompatibility;
-                               if (pa.IsDefined && pa.ResolveBuilder ()) {
-                                       var prop = module.PredefinedMembers.RuntimeCompatibilityWrapNonExceptionThrows.Get ();
-                                       if (prop != null) {
-                                               AttributeEncoder encoder = new AttributeEncoder ();
-                                               encoder.EncodeNamedPropertyArgument (prop, new BoolLiteral (Compiler.BuiltinTypes, true, Location.Null));
-                                               SetCustomAttribute (pa.Constructor, encoder.ToArray ());
+                       if (!IsSatelliteAssembly) {
+                               if (!has_user_debuggable && Compiler.Settings.GenerateDebugInfo) {
+                                       var pa = module.PredefinedAttributes.Debuggable;
+                                       if (pa.IsDefined) {
+                                               var modes = System.Diagnostics.DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints;
+                                               if (!Compiler.Settings.Optimize)
+                                                       modes |= System.Diagnostics.DebuggableAttribute.DebuggingModes.DisableOptimizations;
+
+                                               pa.EmitAttribute (Builder, modes);
                                        }
                                }
-                       }
 
-                       if (declarative_security != null) {
-#if STATIC
-                               foreach (var entry in declarative_security) {
-                                       Builder.__AddDeclarativeSecurity (entry);
+                               if (!wrap_non_exception_throws_custom) {
+                                       PredefinedAttribute pa = module.PredefinedAttributes.RuntimeCompatibility;
+                                       if (pa.IsDefined && pa.ResolveBuilder ()) {
+                                               var prop = module.PredefinedMembers.RuntimeCompatibilityWrapNonExceptionThrows.Get ();
+                                               if (prop != null) {
+                                                       AttributeEncoder encoder = new AttributeEncoder ();
+                                                       encoder.EncodeNamedPropertyArgument (prop, new BoolLiteral (Compiler.BuiltinTypes, true, Location.Null));
+                                                       SetCustomAttribute (pa.Constructor, encoder.ToArray ());
+                                               }
+                                       }
                                }
+
+                               if (declarative_security != null) {
+#if STATIC
+                                       foreach (var entry in declarative_security) {
+                                               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
+                               }
                        }
 
                        CheckReferencesPublicToken ();
@@ -630,9 +648,16 @@ namespace Mono.CSharp
                                        new MemberAccess (system_security_permissions, "SecurityPermissionAttribute"),
                                        new Arguments[] { pos, named }, loc, false);
                                g.AttachTo (module, module);
-                               var ctor = g.Resolve ();
-                               if (ctor != null) {
-                                       g.ExtractSecurityPermissionSet (ctor, ref declarative_security);
+
+                               // Disable no-location warnings (e.g. obsolete) for compiler generated attribute
+                               Compiler.Report.DisableReporting ();
+                               try {
+                                       var ctor = g.Resolve ();
+                                       if (ctor != null) {
+                                               g.ExtractSecurityPermissionSet (ctor, ref declarative_security);
+                                       }
+                               } finally {
+                                       Compiler.Report.EnableReporting ();
                                }
                        }
 
@@ -750,7 +775,7 @@ namespace Mono.CSharp
                        if (Compiler.Settings.Win32ResourceFile != null) {
                                Builder.DefineUnmanagedResource (Compiler.Settings.Win32ResourceFile);
                        } else {
-                               Builder.DefineVersionInfoResource ();
+                               Builder.DefineVersionInfoResource (vi_product, vi_product_version, vi_company, vi_copyright, vi_trademark);
                        }
 
                        if (Compiler.Settings.Win32IconFile != null) {
@@ -790,25 +815,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 +864,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);
                        }
                }
@@ -875,13 +927,13 @@ namespace Mono.CSharp
                                string main_class = Compiler.Settings.MainClass;
                                if (main_class != null) {
                                        // TODO: Handle dotted names
-                                       var texpr = module.GlobalRootNamespace.LookupType (module, main_class, 0, Location.Null);
+                                       var texpr = module.GlobalRootNamespace.LookupType (module, main_class, 0, LookupMode.Probing, Location.Null);
                                        if (texpr == null) {
                                                Report.Error (1555, "Could not find `{0}' specified for Main method", main_class);
                                                return;
                                        }
 
-                                       var mtype = texpr.Type.MemberDefinition as ClassOrStruct;
+                                       var mtype = texpr.MemberDefinition as ClassOrStruct;
                                        if (mtype == null) {
                                                Report.Error (1556, "`{0}' specified for Main method must be a valid class or struct", main_class);
                                                return;
@@ -989,7 +1041,7 @@ namespace Mono.CSharp
        //
        // A placeholder class for assembly attributes when emitting module
        //
-       class AssemblyAttributesPlaceholder : CompilerGeneratedClass
+       class AssemblyAttributesPlaceholder : CompilerGeneratedContainer
        {
                static readonly string TypeNamePrefix = "<$AssemblyAttributes${0}>";
                public static readonly string AssemblyFieldName = "attributes";
@@ -997,7 +1049,7 @@ namespace Mono.CSharp
                Field assembly;
 
                public AssemblyAttributesPlaceholder (ModuleContainer parent, string outputName)
-                       : base (parent, new MemberName (GetGeneratedName (outputName)), Modifiers.STATIC)
+                       : base (parent, new MemberName (GetGeneratedName (outputName)), Modifiers.STATIC | Modifiers.INTERNAL)
                {
                        assembly = new Field (this, new TypeExpression (parent.Compiler.BuiltinTypes.Object, Location), Modifiers.PUBLIC | Modifiers.STATIC,
                                new MemberName (AssemblyFieldName), null);
@@ -1072,25 +1124,24 @@ namespace Mono.CSharp
                }
        }
 
-       abstract class AssemblyReferencesLoader<T>
+       abstract class AssemblyReferencesLoader<T> where T : class
        {
                protected readonly CompilerContext compiler;
 
                protected readonly List<string> paths;
 
-               public AssemblyReferencesLoader (CompilerContext compiler)
+               protected AssemblyReferencesLoader (CompilerContext compiler)
                {
                        this.compiler = compiler;
 
                        paths = new List<string> ();
-                       paths.AddRange (compiler.Settings.ReferencesLookupPaths);
                        paths.Add (Directory.GetCurrentDirectory ());
+                       paths.AddRange (compiler.Settings.ReferencesLookupPaths);
                }
 
                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 +1178,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;
 
@@ -1142,17 +1193,29 @@ namespace Mono.CSharp
                                if (loaded.Contains (key))
                                        continue;
 
-                               // A corlib assembly is the first assembly which contains System.Object
-                               if (corlib_assembly == null && HasObjectType (a)) {
-                                       corlib_assembly = a;
-                                       continue;
-                               }
-
                                loaded.Add (key);
                        }
 
+                       if (corlib_assembly == null) {
+                               //
+                               // Requires second pass because HasObjectType can trigger assembly load event
+                               //
+                               for (int i = 0; i < loaded.Count; ++i) {
+                                       var assembly = loaded [i];
+
+                                       //
+                                       // corlib assembly is the first referenced assembly which contains System.Object
+                                       //
+                                       if (HasObjectType (assembly.Item2)) {
+                                               corlib_assembly = assembly.Item2;
+                                               loaded.RemoveAt (i);
+                                               break;
+                                       }
+                               }
+                       }
+
                        foreach (var entry in module.Compiler.Settings.AssemblyReferencesAliases) {
-                               a = LoadAssemblyFile (entry.Item2);
+                               a = LoadAssemblyFile (entry.Item2, false);
                                if (a == null)
                                        continue;
 
@@ -1165,7 +1228,7 @@ namespace Mono.CSharp
 
                        if (compiler.Settings.LoadDefaultReferences) {
                                foreach (string r in GetDefaultReferences ()) {
-                                       a = LoadAssemblyDefault (r);
+                                       a = LoadAssemblyFile (r, true);
                                        if (a == null)
                                                continue;