[runtime] Fix MonoError lifecycle in do_try_exec_main (#3145)
[mono.git] / mcs / mcs / assembly.cs
index 0124ce9164140869395e6687439c899ce24a7145..d69feb3de5b15bd0bd0c08a986db9865c0098104 100644 (file)
@@ -7,7 +7,7 @@
 //
 // Copyright 2001, 2002, 2003 Ximian, Inc.
 // Copyright 2004-2011 Novell, Inc.
-// Copyright 2011 Xamarin Inc
+// Copyright 2011-2013 Xamarin Inc
 //
 
 
@@ -54,6 +54,7 @@ namespace Mono.CSharp
                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,10 @@ 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;
+               string pa_file_version, pa_assembly_version;
+
                protected AssemblyDefinition (ModuleContainer module, string name)
                {
                        this.module = module;
@@ -154,6 +159,8 @@ namespace Mono.CSharp
                        }
                }
 
+               public bool IsSatelliteAssembly { get; private set; }
+
                public string Name {
                        get {
                                return name;
@@ -201,7 +208,7 @@ namespace Mono.CSharp
                                        return;
 
                                if (Compiler.Settings.Target == Target.Exe) {
-                                       a.Error_AttributeEmitError ("The executables cannot be satelite assemblies, remove the attribute or keep it empty");
+                                       Report.Error (7059, a.Location, "Executables cannot be satellite assemblies. Remove the attribute or keep it empty");
                                        return;
                                }
 
@@ -214,6 +221,7 @@ namespace Mono.CSharp
                                        builder_extra.SetCulture (value, a.Location);
                                }
 
+                               IsSatelliteAssembly = true;
                                return;
                        }
 
@@ -224,7 +232,8 @@ namespace Mono.CSharp
 
                                var vinfo = IsValidAssemblyVersion (value, true);
                                if (vinfo == null) {
-                                       a.Error_AttributeEmitError (string.Format ("Specified version `{0}' is not valid", value));
+                                       Report.Error (7034, a.Location, "The specified version string `{0}' does not conform to the required format - major[.minor[.build[.revision]]]",
+                                               value);
                                        return;
                                }
 
@@ -232,6 +241,7 @@ namespace Mono.CSharp
                                        SetCustomAttribute (ctor, cdata);
                                } else {
                                        builder_extra.SetVersion (vinfo, a.Location);
+                                       pa_assembly_version = vinfo.ToString ();
                                }
 
                                return;
@@ -285,7 +295,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,17 +304,17 @@ 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;
                                }
 
-                               builder_extra.AddTypeForwarder (t.GetDefinition (), a.Location);
+                               AddTypeForwarders (t, a.Location);
                                return;
                        }
 
@@ -315,13 +325,18 @@ namespace Mono.CSharp
 
                        if (a.Type == pa.InternalsVisibleTo) {
                                string assembly_name = a.GetString ();
+                               if (assembly_name == null) {
+                                       Report.Error (7030, a.Location, "Friend assembly reference cannot have `null' value");
+                                       return;
+                               }
+
                                if (assembly_name.Length == 0)
                                        return;
 #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",
+                                       Report.Warning (1700, 3, a.Location, "Friend assembly reference `{0}' is invalid and cannot be resolved",
                                                assembly_name);
                                        return;
                                }
@@ -344,18 +359,56 @@ namespace Mono.CSharp
                        } 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) {
-                                       Report.Warning (1607, 1, a.Location, "The version number `{0}' specified for `{1}' is invalid",
-                                               value, a.Name);
+                               pa_file_version = a.GetString ();
+                               if (string.IsNullOrEmpty (pa_file_version) || IsValidAssemblyVersion (pa_file_version, false) == null) {
+                                       Report.Warning (7035, 1, a.Location, "The specified version string `{0}' does not conform to the recommended format major.minor.build.revision",
+                                                       pa_file_version, a.Name);
                                        return;
                                }
+
+                               // File version info decoding from blob is not supported
+                               var cab = new CustomAttributeBuilder ((ConstructorInfo)ctor.GetMetaInfo (), new object [] { pa_file_version });
+                               Builder.SetCustomAttribute (cab);
+                               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.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;
+                       } else if (a.Type == pa.AssemblyInformationalVersion) {
+                               vi_product_version = a.GetString ();
                        }
 
+                       //
+                       // Win32 version info attributes AssemblyDescription and AssemblyTitle cannot be
+                       // set using public API and because we have blob like attributes we need to use
+                       // special option DecodeVersionInfoAttributeBlobs to support values extraction
+                       //
 
                        SetCustomAttribute (ctor, cdata);
                }
 
+               void AddTypeForwarders (TypeSpec type, Location loc)
+               {
+                       builder_extra.AddTypeForwarder (type.GetDefinition (), loc);
+
+                       var ntypes = MemberCache.GetDeclaredNestedTypes (type);
+                       if (ntypes == null)
+                               return;
+                       
+                       foreach (var nested in ntypes) {
+                               if (nested.IsPrivate)
+                                       continue;
+
+                               AddTypeForwarders (nested, loc);
+                       }
+               }
+
                //
                // When using assembly public key attributes InternalsVisibleTo key
                // was not checked, we have to do it later when we actually know what
@@ -363,34 +416,40 @@ namespace Mono.CSharp
                //
                void CheckReferencesPublicToken ()
                {
-                       // TODO: It should check only references assemblies but there is
-                       // no working SRE API
-                       foreach (var entry in Importer.Assemblies) {
-                               var a = entry as ImportedAssemblyDefinition;
-                               if (a == null)
-                                       continue;
-
-                               if (public_key != null && !a.HasStrongName) {
+                       foreach (var an in builder_extra.GetReferencedAssemblies ()) {
+                               if (public_key != null && an.GetPublicKey ().Length == 0) {
                                        Report.Error (1577, "Referenced assembly `{0}' does not have a strong name",
-                                               a.FullName);
+                                               an.FullName);
                                }
 
-                               var ci = a.Assembly.GetName ().CultureInfo;
-                               if (!ci.Equals (System.Globalization.CultureInfo.InvariantCulture)) {
-                                       Report.Warning (1607, 1, "Referenced assembly `{0}' has different culture setting of `{1}'",
-                                               a.Name, ci.Name);
+                               var ci = an.CultureInfo;
+                               if (!ci.Equals (CultureInfo.InvariantCulture)) {
+                                       Report.Warning (8009, 1, "Referenced assembly `{0}' has different culture setting of `{1}'",
+                                               an.Name, ci.Name);
                                }
 
-                               if (!a.IsFriendAssemblyTo (this))
+                               var ia = Importer.GetImportedAssemblyDefinition (an);
+                               if (ia == null)
                                        continue;
 
-                               var attr = a.GetAssemblyVisibleToName (this);
+                               var references = GetNotUnifiedReferences (an);
+                               if (references != null) {
+                                       foreach (var r in references) {
+                                               Report.SymbolRelatedToPreviousError ( r[0]);
+                                               Report.Error (1705, r [1]);
+                                       }
+                               }
+
+                               if (!ia.IsFriendAssemblyTo (this))
+                                       continue;
+                               
+                               var attr = ia.GetAssemblyVisibleToName (this);
                                var atoken = attr.GetPublicKeyToken ();
 
                                if (ArrayComparer.IsEqual (GetPublicKeyToken (), atoken))
                                        continue;
 
-                               Report.SymbolRelatedToPreviousError (a.Location);
+                               Report.SymbolRelatedToPreviousError (ia.Location);
                                Report.Error (281,
                                        "Friend access was granted to `{0}', but the output assembly is named `{1}'. Try adding a reference to `{0}' or change the output assembly name to match it",
                                        attr.FullName, FullName);
@@ -457,26 +516,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
-                               throw new NotSupportedException ("Assembly-level security");
+                                       throw new NotSupportedException ("Assembly-level security");
 #endif
+                               }
                        }
 
                        CheckReferencesPublicToken ();
@@ -498,6 +570,11 @@ namespace Mono.CSharp
                        return public_key_token;
                }
 
+               protected virtual List<string[]> GetNotUnifiedReferences (AssemblyName assemblyName)
+               {
+                       return null;
+               }
+
                //
                // Either keyFile or keyContainer has to be non-null
                //
@@ -744,7 +821,11 @@ namespace Mono.CSharp
                        if (Compiler.Settings.Win32ResourceFile != null) {
                                Builder.DefineUnmanagedResource (Compiler.Settings.Win32ResourceFile);
                        } else {
-                               Builder.DefineVersionInfoResource ();
+                               Builder.DefineVersionInfoResource (vi_product, 
+                                                                  vi_product_version ?? pa_file_version ?? pa_assembly_version,
+                                                                  vi_company,
+                                                                  vi_copyright,
+                                                                  vi_trademark);
                        }
 
                        if (Compiler.Settings.Win32IconFile != null) {
@@ -827,8 +908,10 @@ namespace Mono.CSharp
                                } else {
                                        Builder.Save (module.Builder.ScopeName, pekind, machine);
                                }
+                       } catch (ArgumentOutOfRangeException) {
+                               Report.Error (16, "Output file `{0}' exceeds the 4GB limit");
                        } catch (Exception e) {
-                               Report.Error (16, "Could not write to file `" + name + "', cause: " + e.Message);
+                               Report.Error (16, "Could not write to file `{0}'. {1}", name, e.Message);
                        }
                        Compiler.TimeReporter.Stop (TimeReporter.TimerType.OutputSave);
 
@@ -902,7 +985,7 @@ namespace Mono.CSharp
                                                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;
@@ -1044,7 +1127,7 @@ namespace Mono.CSharp
        //
        public class AssemblyBuilderExtension
        {
-               readonly CompilerContext ctx;
+               protected readonly CompilerContext ctx;
 
                public AssemblyBuilderExtension (CompilerContext ctx)
                {
@@ -1072,6 +1155,11 @@ namespace Mono.CSharp
                        ctx.Report.RuntimeMissingSupport (Location.Null, "-win32icon");
                }
 
+               public virtual AssemblyName[] GetReferencedAssemblies ()
+               {
+                       return null;
+               }
+
                public virtual void SetAlgorithmId (uint value, Location loc)
                {
                        ctx.Report.RuntimeMissingSupport (loc, "AssemblyAlgorithmIdAttribute");
@@ -1093,19 +1181,19 @@ 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);
@@ -1162,15 +1250,27 @@ 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, false);
                                if (a == null)