Merge pull request #498 from Unroll-Me/master
[mono.git] / mcs / mcs / ikvm.cs
index 4b90e23bb097c77a50b32e7fcada73450141d035..2f234e01e54244fc1518978c2b429e72789a93bb 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
 //
 //
 
@@ -88,24 +89,24 @@ 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
                        // into 2 or more global aliases
-                       var definition = GetAssemblyDefinition (assembly);
+                       // TODO: Should be just Add
+                       GetAssemblyDefinition (assembly);
 
                        var all_types = assembly.GetTypes ();
-                       ImportTypes (all_types, targetNamespace, definition.HasExtensionMethod);
+                       ImportTypes (all_types, targetNamespace, true);
+
+                       all_types = assembly.ManifestModule.__GetExportedTypes ();
+                       if (all_types.Length != 0)
+                               ImportForwardedTypes (all_types, targetNamespace);
                }
 
                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 ();
@@ -114,6 +115,31 @@ namespace Mono.CSharp
                        return module_definition;
                }
 
+               void ImportForwardedTypes (MetaType[] types, Namespace targetNamespace)
+               {
+                       Namespace ns = targetNamespace;
+                       string prev_namespace = null;
+                       foreach (var t in types) {
+                               // IsMissing tells us the type has been forwarded and target assembly is missing 
+                               if (!t.__IsMissing)
+                                       continue;
+
+                               if (t.Name[0] == '<')
+                                       continue;
+
+                               var it = CreateType (t, null, new DynamicTypeReader (t), true);
+                               if (it == null)
+                                       continue;
+
+                               if (prev_namespace != t.Namespace) {
+                                       ns = t.Namespace == null ? targetNamespace : targetNamespace.GetNamespace (t.Namespace, true);
+                                       prev_namespace = t.Namespace;
+                               }
+
+                               ns.AddType (module, it);
+                       }
+               }
+
                public void InitializeBuiltinTypes (BuiltinTypes builtin, Assembly corlib)
                {
                        //
@@ -204,6 +230,7 @@ namespace Mono.CSharp
                        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)
@@ -215,30 +242,33 @@ namespace Mono.CSharp
                        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;
+                       if (compiler.Settings.StdLib) {
+                               var corlib_path = Path.GetDirectoryName (typeof (object).Assembly.Location);
+                               string fx_path = corlib_path.Substring (0, corlib_path.LastIndexOf (Path.DirectorySeparatorChar));
 
-                       string sdk_version = compiler.Settings.SdkVersion ?? "4";
-                       string[] sdk_sub_dirs;
+                               string sdk_path = null;
 
-                       if (!sdk_directory.TryGetValue (sdk_version, out sdk_sub_dirs))
-                               sdk_sub_dirs = new string[] { sdk_version };
+                               string sdk_version = compiler.Settings.SdkVersion ?? "4.5";
+                               string[] sdk_sub_dirs;
 
-                       foreach (var dir in sdk_sub_dirs) {
-                               sdk_path = Path.Combine (fx_path, dir);
-                               if (File.Exists (Path.Combine (sdk_path, "mscorlib.dll")))
-                                       break;
+                               if (!sdk_directory.TryGetValue (sdk_version, out sdk_sub_dirs))
+                                       sdk_sub_dirs = new string[] { sdk_version };
 
-                               sdk_path = null;
-                       }
+                               foreach (var dir in sdk_sub_dirs) {
+                                       sdk_path = Path.Combine (fx_path, dir);
+                                       if (File.Exists (Path.Combine (sdk_path, "mscorlib.dll")))
+                                               break;
 
-                       if (sdk_path == null) {
-                               compiler.Report.Warning (-1, 1, "SDK path could not be resolved");
-                               sdk_path = corlib_path;
-                       }
+                                       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
@@ -247,9 +277,6 @@ namespace Mono.CSharp
                        get {
                                return corlib;
                        }
-                       set {
-                               corlib = value;
-                       }
                }
 
                public Universe Domain {
@@ -369,7 +396,7 @@ namespace Mono.CSharp
                        return assembly.GetType (compiler.BuiltinTypes.Object.FullName) != null;
                }
 
-               public override Assembly LoadAssemblyFile (string fileName)
+               public override Assembly LoadAssemblyFile (string fileName, bool isImplicitReference)
                {
                        bool? has_extension = null;
                        foreach (var path in paths) {
@@ -406,7 +433,7 @@ namespace Mono.CSharp
                                                        if (an.Name != loaded_name.Name)
                                                                continue;
 
-                                                       if (an.CodeBase == loaded_name.CodeBase)
+                                                       if (module.ModuleVersionId == entry.Item3.ManifestModule.ModuleVersionId)
                                                                return entry.Item3;
                                                        
                                                        if (((an.Flags | loaded_name.Flags) & AssemblyNameFlags.PublicKey) == 0) {
@@ -438,12 +465,16 @@ namespace Mono.CSharp
                                                return assembly;
                                        }
                                } catch {
-                                       Error_FileCorrupted (file);
+                                       if (!isImplicitReference)
+                                               Error_FileCorrupted (file);
+
                                        return null;
                                }
                        }
 
-                       Error_FileNotFound (fileName);
+                       if (!isImplicitReference)
+                               Error_FileNotFound (fileName);
+
                        return null;
                }
 
@@ -472,39 +503,6 @@ namespace Mono.CSharp
                        return null;                            
                }
 
-               //
-               // Optimized default assembly loader version
-               //
-               public override Assembly LoadAssemblyDefault (string assembly)
-               {
-                       foreach (var path in paths) {
-                               var file = Path.Combine (path, assembly);
-
-                               if (compiler.Settings.DebugFlags > 0)
-                                       Console.WriteLine ("Probing default assembly location `{0}'", file);
-
-                               if (!File.Exists (file))
-                                       continue;
-
-                               try {
-                                       if (compiler.Settings.DebugFlags > 0)
-                                               Console.WriteLine ("Loading default assembly `{0}'", file);
-
-                                       var a = domain.LoadFile (file);
-                                       if (a != null) {
-                                               loaded_names.Add (Tuple.Create (a.GetName (), file, a));
-                                       }
-
-                                       return a;
-                               } catch {
-                                       // Default assemblies can fail to load without error
-                                       return null;
-                               }
-                       }
-
-                       return null;
-               }
-
                public override void LoadReferences (ModuleContainer module)
                {
                        List<Tuple<RootNamespace, Assembly>> loaded;
@@ -535,7 +533,7 @@ namespace Mono.CSharp
                                        continue;
 
                                if (m.IsManifestModule) {
-                                       Error_FileCorrupted (moduleName);
+                                       Error_ModuleIsAssembly (moduleName);
                                        continue;
                                }
 
@@ -577,7 +575,7 @@ namespace Mono.CSharp
 
                public override void SetFlags (uint flags, Location loc)
                {
-                       builder.__SetAssemblyFlags ((AssemblyNameFlags) flags);
+                       builder.__AssemblyFlags = (AssemblyNameFlags) flags;
                }
 
                public override void SetVersion (Version version, Location loc)
@@ -585,4 +583,4 @@ namespace Mono.CSharp
                        builder.__SetAssemblyVersion (version);
                }
        }
-}
\ No newline at end of file
+}