Updated with Xamarin copyrights
[mono.git] / mcs / mcs / ikvm.cs
index 9d5155b28e31dcbf152a9934c21eda2757c1142d..18c97861a74fc71ae6a7cd91dad77cdd8f465447 100644 (file)
@@ -6,6 +6,7 @@
 // Dual licensed under the terms of the MIT X11 or GNU GPL
 //
 // Copyright 2009-2010 Novell, Inc. 
+// Copyright 2011 Xamarin Inc
 //
 //
 
@@ -22,7 +23,7 @@ namespace Mono.CSharp
 #if !STATIC
        public class StaticImporter
        {
-               public StaticImporter (BuildinTypes buildin)
+               public StaticImporter (BuiltinTypes builtin)
                {
                        throw new NotSupportedException ();
                }
@@ -47,10 +48,21 @@ namespace Mono.CSharp
 
        sealed class StaticImporter : MetadataImporter
        {
-               public StaticImporter ()
+               public StaticImporter (ModuleContainer module)
+                       : base (module)
                {
                }
 
+               public void AddCompiledAssembly (AssemblyDefinitionStatic assembly)
+               {
+                       assembly_2_definition.Add (assembly.Builder, assembly);
+               }
+
+               public override void AddCompiledType (TypeBuilder type, TypeSpec spec)
+               {
+                       compiled_types.Add (type, spec);
+               }
+
                protected override MemberKind DetermineKindFromBaseType (MetaType baseType)
                {
                        string name = baseType.Name;
@@ -77,11 +89,6 @@ namespace Mono.CSharp
                        return false;
                }
 
-               public override void GetCustomAttributeTypeName (CustomAttributeData cad, out string typeNamespace, out string typeName)
-               {
-                       cad.__TryReadTypeName (out typeNamespace, out typeName);
-               }
-
                public void ImportAssembly (Assembly assembly, RootNamespace targetNamespace)
                {
                        // It can be used more than once when importing same assembly
@@ -94,7 +101,7 @@ namespace Mono.CSharp
 
                public ImportedModuleDefinition ImportModule (Module module, RootNamespace targetNamespace)
                {
-                       var module_definition = new ImportedModuleDefinition (module, this);
+                       var module_definition = new ImportedModuleDefinition (module);
                        module_definition.ReadAttributes ();
 
                        var all_types = module.GetTypes ();
@@ -103,13 +110,13 @@ namespace Mono.CSharp
                        return module_definition;
                }
 
-               public void InitializeBuildinTypes (BuildinTypes buildin, Assembly corlib)
+               public void InitializeBuiltinTypes (BuiltinTypes builtin, Assembly corlib)
                {
                        //
                        // Setup mapping for build-in types to avoid duplication of their definition
                        //
-                       foreach (var type in buildin.AllTypes) {
-                               buildin_types.Add (corlib.GetType (type.FullName), type);
+                       foreach (var type in builtin.AllTypes) {
+                               compiled_types.Add (corlib.GetType (type.FullName), type);
                        }
                }
        }
@@ -117,29 +124,37 @@ namespace Mono.CSharp
 
        class AssemblyDefinitionStatic : AssemblyDefinition
        {
+               readonly StaticLoader loader;
+
                //
                // Assembly container with file output
                //
-               public AssemblyDefinitionStatic (ModuleContainer module, string name, string fileName)
+               public AssemblyDefinitionStatic (ModuleContainer module, StaticLoader loader, string name, string fileName)
                        : base (module, name, fileName)
                {
+                       this.loader = loader;
+                       Importer = loader.MetadataImporter;
                }
 
                //
-               // Initializes the code generator
+               // Initializes the assembly SRE domain
                //
-               public bool Create (StaticLoader loader)
+               public void Create (Universe domain)
                {
                        ResolveAssemblySecurityAttributes ();
                        var an = CreateAssemblyName ();
 
-                       Builder = loader.Domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, Path.GetDirectoryName (file_name));
+                       Builder = domain.DefineDynamicAssembly (an, AssemblyBuilderAccess.Save, Path.GetDirectoryName (file_name));
+                       module.Create (this, CreateModuleBuilder ());
+               }
 
-                       if (loader.Corlib != null) {
+               public override void Emit ()
+               {
+                       if (loader.Corlib != null && !(loader.Corlib is AssemblyBuilder)) {
                                Builder.__SetImageRuntimeVersion (loader.Corlib.ImageRuntimeVersion, 0x20000);
                        } else {
                                // Sets output file metadata version when there is no mscorlib
-                               switch (RootContext.StdLibRuntimeVersion) {
+                               switch (module.Compiler.Settings.StdLibRuntimeVersion) {
                                case RuntimeVersion.v4:
                                        Builder.__SetImageRuntimeVersion ("v4.0.30319", 0x20000);
                                        break;
@@ -157,7 +172,8 @@ namespace Mono.CSharp
                        }
 
                        builder_extra = new AssemblyBuilderIKVM (Builder, Compiler);
-                       return true;
+
+                       base.Emit ();
                }
 
                public Module IncludeModule (RawModule moduleFile)
@@ -177,13 +193,14 @@ namespace Mono.CSharp
                readonly Universe domain;
                Assembly corlib;
                List<Tuple<AssemblyName, string, Assembly>> loaded_names;
-               static readonly Dictionary<SdkVersion, string[]> sdk_directory;
+               static readonly Dictionary<string, string[]> sdk_directory;
 
                static StaticLoader ()
                {
-                       sdk_directory = new Dictionary<SdkVersion, string[]> ();
-                       sdk_directory.Add (SdkVersion.v2, new string[] { "2.0", "net_2_0", "v2.0.50727" });
-                       sdk_directory.Add (SdkVersion.v4, new string[] { "4.0", "net_4_0", "v4.0.30319" });
+                       sdk_directory = new Dictionary<string, string[]> ();
+                       sdk_directory.Add ("2", new string[] { "2.0", "net_2_0", "v2.0.50727" });
+                       sdk_directory.Add ("4", new string[] { "4.0", "net_4_0", "v4.0.30319" });
+                       sdk_directory.Add ("4.5", new string[] { "4.5", "net_4_5", "v4.0.30319" });
                }
 
                public StaticLoader (StaticImporter importer, CompilerContext compiler)
@@ -191,29 +208,41 @@ namespace Mono.CSharp
                {
                        this.importer = importer;
                        domain = new Universe ();
+                       domain.EnableMissingMemberResolution ();
                        domain.AssemblyResolve += AssemblyReferenceResolver;
                        loaded_names = new List<Tuple<AssemblyName, string, Assembly>> ();
 
                        var corlib_path = Path.GetDirectoryName (typeof (object).Assembly.Location);
                        string fx_path = corlib_path.Substring (0, corlib_path.LastIndexOf (Path.DirectorySeparatorChar));
-                       string sdk_path = null;
 
-                       foreach (var dir in sdk_directory[RootContext.SdkVersion]) {
-                               sdk_path = Path.Combine (fx_path, dir);
-                               if (File.Exists (Path.Combine (sdk_path, "mscorlib.dll")))
-                                       break;
+                       if (compiler.Settings.StdLib) {
+                               string sdk_path = null;
 
-                               sdk_path = null;
-                       }
+                               string sdk_version = compiler.Settings.SdkVersion ?? "4.5";
+                               string[] sdk_sub_dirs;
 
-                       if (sdk_path == null) {
-                               compiler.Report.Warning (-1, 1, "SDK path could not be resolved");
-                               sdk_path = corlib_path;
-                       }
+                               if (!sdk_directory.TryGetValue (sdk_version, out sdk_sub_dirs))
+                                       sdk_sub_dirs = new string[] { sdk_version };
+
+                               foreach (var dir in sdk_sub_dirs) {
+                                       sdk_path = Path.Combine (fx_path, dir);
+                                       if (File.Exists (Path.Combine (sdk_path, "mscorlib.dll")))
+                                               break;
+
+                                       sdk_path = null;
+                               }
+
+                               if (sdk_path == null) {
+                                       compiler.Report.Warning (-1, 1, "SDK path could not be resolved");
+                                       sdk_path = corlib_path;
+                               }
 
-                       paths.Add (sdk_path);
+                               paths.Add (sdk_path);
+                       }
                }
 
+               #region Properties
+
                public Assembly Corlib {
                        get {
                                return corlib;
@@ -229,6 +258,14 @@ namespace Mono.CSharp
                        }
                }
 
+               public StaticImporter MetadataImporter {
+                       get {
+                               return importer;
+                       }
+               }
+
+               #endregion
+
                Assembly AssemblyReferenceResolver (object sender, IKVM.Reflection.ResolveEventArgs args)
                {
                        var refname = args.Name;
@@ -236,47 +273,63 @@ namespace Mono.CSharp
                                return corlib;
 
                        Assembly version_mismatch = null;
-                       foreach (var assembly in domain.GetAssemblies ()) {
-                               // TODO: Cannot handle unification into current assembly yet
-                               if (assembly is AssemblyBuilder)
-                                       continue;
+                       bool is_fx_assembly = false;
 
+                       foreach (var assembly in domain.GetAssemblies ()) {
                                AssemblyComparisonResult result;
                                if (!Fusion.CompareAssemblyIdentityPure (refname, false, assembly.FullName, false, out result)) {
                                        if ((result == AssemblyComparisonResult.NonEquivalentVersion || result == AssemblyComparisonResult.NonEquivalentPartialVersion) &&
-                                               (version_mismatch == null || version_mismatch.GetName ().Version < assembly.GetName ().Version)) {
+                                               (version_mismatch == null || version_mismatch.GetName ().Version < assembly.GetName ().Version) &&
+                                               !is_fx_assembly) {
                                                version_mismatch = assembly;
                                        }
 
                                        continue;
                                }
 
-                               if (result == AssemblyComparisonResult.EquivalentFXUnified ||
-                                       result == AssemblyComparisonResult.EquivalentFullMatch ||
+                               if (result == AssemblyComparisonResult.EquivalentFullMatch ||
                                        result == AssemblyComparisonResult.EquivalentWeakNamed ||
                                        result == AssemblyComparisonResult.EquivalentPartialMatch) {
                                        return assembly;
                                }
 
+                               if (result == AssemblyComparisonResult.EquivalentFXUnified) {
+                                       is_fx_assembly = true;
+
+                                       if (version_mismatch == null || version_mismatch.GetName ().Version < assembly.GetName ().Version)
+                                               version_mismatch = assembly;
+
+                                       continue;
+                               }
+
                                throw new NotImplementedException ("Assembly equality = " + result.ToString ());
                        }
 
                        if (version_mismatch != null) {
+                               if (version_mismatch is AssemblyBuilder)
+                                       return version_mismatch;
+
                                var v1 = new AssemblyName (refname).Version;
                                var v2 = version_mismatch.GetName ().Version;
 
                                if (v1 > v2) {
 //                                     compiler.Report.SymbolRelatedToPreviousError (args.RequestingAssembly.Location);
-                                       compiler.Report.Error (1705, "Assembly `{0}' references `{1}' which has higher version number than imported assembly `{2}'",
+                                       compiler.Report.Error (1705, "Assembly `{0}' references `{1}' which has higher version number than imported assembly `{2}'",
                                                args.RequestingAssembly.FullName, refname, version_mismatch.GetName ().FullName);
-                               } else if (v1.Major != v2.Major || v1.Minor != v2.Minor) {
-                                       compiler.Report.Warning (1701, 2,
-                                               "Assuming assembly reference `{0}' matches assembly `{1}'. You may need to supply runtime policy",
-                                               refname, version_mismatch.GetName ().FullName);
-                               } else {
-                                       compiler.Report.Warning (1702, 3,
-                                               "Assuming assembly reference `{0}' matches assembly `{1}'. You may need to supply runtime policy",
-                                               refname, version_mismatch.GetName ().FullName);
+
+                                       return domain.CreateMissingAssembly (args.Name);
+                               }
+
+                               if (!is_fx_assembly) {
+                                       if (v1.Major != v2.Major || v1.Minor != v2.Minor) {
+                                               compiler.Report.Warning (1701, 2,
+                                                       "Assuming assembly reference `{0}' matches assembly `{1}'. You may need to supply runtime policy",
+                                                       refname, version_mismatch.GetName ().FullName);
+                                       } else {
+                                               compiler.Report.Warning (1702, 3,
+                                                       "Assuming assembly reference `{0}' matches assembly `{1}'. You may need to supply runtime policy",
+                                                       refname, version_mismatch.GetName ().FullName);
+                                       }
                                }
 
                                return version_mismatch;
@@ -302,18 +355,18 @@ namespace Mono.CSharp
 
                        default_references.Add ("System.dll");
                        default_references.Add ("System.Xml.dll");
+                       default_references.Add ("System.Core.dll");
 
-                       if (RootContext.Version > LanguageVersion.ISO_2)
-                               default_references.Add ("System.Core.dll");
-                       if (RootContext.Version > LanguageVersion.V_3)
+                       if (corlib != null && corlib.GetName ().Version.Major >= 4) {
                                default_references.Add ("Microsoft.CSharp.dll");
+                       }
 
                        return default_references.ToArray ();
                }
 
                public override bool HasObjectType (Assembly assembly)
                {
-                       return assembly.GetType (compiler.BuildinTypes.Object.FullName) != null;
+                       return assembly.GetType (compiler.BuiltinTypes.Object.FullName) != null;
                }
 
                public override Assembly LoadAssemblyFile (string fileName)
@@ -321,7 +374,7 @@ namespace Mono.CSharp
                        bool? has_extension = null;
                        foreach (var path in paths) {
                                var file = Path.Combine (path, fileName);
-                               if (Report.DebugFlags > 0)
+                               if (compiler.Settings.DebugFlags > 0)
                                        Console.WriteLine ("Probing assembly location `{0}'", file);
 
                                if (!File.Exists (file)) {
@@ -375,7 +428,7 @@ namespace Mono.CSharp
                                                        }
                                                }
 
-                                               if (Report.DebugFlags > 0)
+                                               if (compiler.Settings.DebugFlags > 0)
                                                        Console.WriteLine ("Loading assembly `{0}'", fileName);
 
                                                var assembly = domain.LoadAssembly (module);
@@ -427,14 +480,14 @@ namespace Mono.CSharp
                        foreach (var path in paths) {
                                var file = Path.Combine (path, assembly);
 
-                               if (Report.DebugFlags > 0)
+                               if (compiler.Settings.DebugFlags > 0)
                                        Console.WriteLine ("Probing default assembly location `{0}'", file);
 
                                if (!File.Exists (file))
                                        continue;
 
                                try {
-                                       if (Report.DebugFlags > 0)
+                                       if (compiler.Settings.DebugFlags > 0)
                                                Console.WriteLine ("Loading default assembly `{0}'", file);
 
                                        var a = domain.LoadFile (file);
@@ -457,28 +510,32 @@ namespace Mono.CSharp
                        List<Tuple<RootNamespace, Assembly>> loaded;
                        base.LoadReferencesCore (module, out corlib, out loaded);
 
-                       if (corlib != null) {
-                               importer.InitializeBuildinTypes (compiler.BuildinTypes, corlib);
+                       compiler.TimeReporter.Start (TimeReporter.TimerType.ReferencesImporting);
+
+                       if (corlib == null) {
+                               // System.Object was not found in any referenced assembly, use compiled assembly as corlib
+                               corlib = module.DeclaringAssembly.Builder;
+                       } else {
+                               importer.InitializeBuiltinTypes (compiler.BuiltinTypes, corlib);
                                importer.ImportAssembly (corlib, module.GlobalRootNamespace);
                        }
 
                        foreach (var entry in loaded) {
                                importer.ImportAssembly (entry.Item2, entry.Item1);
                        }
+
+                       compiler.TimeReporter.Stop (TimeReporter.TimerType.ReferencesImporting);
                }
 
                public void LoadModules (AssemblyDefinitionStatic assembly, RootNamespace targetNamespace)
                {
-                       if (RootContext.Modules.Count == 0)
-                               return;
-
-                       foreach (var moduleName in RootContext.Modules) {
+                       foreach (var moduleName in compiler.Settings.Modules) {
                                var m = LoadModuleFile (moduleName);
                                if (m == null)
                                        continue;
 
                                if (m.IsManifestModule) {
-                                       Error_FileCorrupted (moduleName);
+                                       Error_ModuleIsAssembly (moduleName);
                                        continue;
                                }