Update to the latest IKVM.Reflection
[mono.git] / mcs / class / IKVM.Reflection / Reader / AssemblyReader.cs
index 08d66ddf58c3a63db5ed785aefb41483a9717b49..c64189eeb48975e26bbb7a8c49bed648f68271c0 100644 (file)
@@ -49,11 +49,6 @@ namespace IKVM.Reflection.Reader
                        get { return location; }
                }
 
-               public override string FullName
-               {
-                       get { return GetName().FullName; }
-               }
-
                public override AssemblyName GetName()
                {
                        return GetNameImpl(ref manifestModule.AssemblyTable.records[0]);
@@ -74,15 +69,46 @@ namespace IKVM.Reflection.Reader
                        }
                        if (rec.Culture != 0)
                        {
-                               name.CultureInfo = new System.Globalization.CultureInfo(manifestModule.GetString(rec.Culture));
+                               name.Culture = manifestModule.GetString(rec.Culture);
                        }
                        else
                        {
-                               name.CultureInfo = System.Globalization.CultureInfo.InvariantCulture;
+                               name.Culture = "";
                        }
                        name.HashAlgorithm = (AssemblyHashAlgorithm)rec.HashAlgId;
                        name.CodeBase = this.CodeBase;
-                       name.Flags = (AssemblyNameFlags)rec.Flags;
+                       PortableExecutableKinds peKind;
+                       ImageFileMachine machine;
+                       manifestModule.GetPEKind(out peKind, out machine);
+                       switch (machine)
+                       {
+                               case ImageFileMachine.I386:
+                                       // FXBUG we copy the .NET bug that Preferred32Bit implies x86
+                                       if ((peKind & (PortableExecutableKinds.Required32Bit | PortableExecutableKinds.Preferred32Bit)) != 0)
+                                       {
+                                               name.ProcessorArchitecture = ProcessorArchitecture.X86;
+                                       }
+                                       else if ((rec.Flags & 0x70) == 0x70)
+                                       {
+                                               // it's a reference assembly
+                                               name.ProcessorArchitecture = ProcessorArchitecture.None;
+                                       }
+                                       else
+                                       {
+                                               name.ProcessorArchitecture = ProcessorArchitecture.MSIL;
+                                       }
+                                       break;
+                               case ImageFileMachine.IA64:
+                                       name.ProcessorArchitecture = ProcessorArchitecture.IA64;
+                                       break;
+                               case ImageFileMachine.AMD64:
+                                       name.ProcessorArchitecture = ProcessorArchitecture.Amd64;
+                                       break;
+                               case ImageFileMachine.ARM:
+                                       name.ProcessorArchitecture = ProcessorArchitecture.Arm;
+                                       break;
+                       }
+                       name.RawFlags = (AssemblyNameFlags)rec.Flags;
                        return name;
                }
 
@@ -101,14 +127,27 @@ namespace IKVM.Reflection.Reader
                        return list.ToArray();
                }
 
-               internal override Type GetTypeImpl(string typeName)
+               internal override Type FindType(TypeName typeName)
                {
-                       Type type = manifestModule.GetType(typeName);
+                       Type type = manifestModule.FindType(typeName);
                        for (int i = 0; type == null && i < externalModules.Length; i++)
                        {
                                if ((manifestModule.File.records[i].Flags & ContainsNoMetaData) == 0)
                                {
-                                       type = GetModule(i).GetType(typeName);
+                                       type = GetModule(i).FindType(typeName);
+                               }
+                       }
+                       return type;
+               }
+
+               internal override Type FindTypeIgnoreCase(TypeName lowerCaseName)
+               {
+                       Type type = manifestModule.FindTypeIgnoreCase(lowerCaseName);
+                       for (int i = 0; type == null && i < externalModules.Length; i++)
+                       {
+                               if ((manifestModule.File.records[i].Flags & ContainsNoMetaData) == 0)
+                               {
+                                       type = GetModule(i).FindTypeIgnoreCase(lowerCaseName);
                                }
                        }
                        return type;
@@ -116,7 +155,7 @@ namespace IKVM.Reflection.Reader
 
                public override string ImageRuntimeVersion
                {
-                       get { return manifestModule.ImageRuntimeVersion; }
+                       get { return manifestModule.__ImageRuntimeVersion; }
                }
 
                public override Module ManifestModule
@@ -191,19 +230,45 @@ namespace IKVM.Reflection.Reader
                        {
                                return externalModules[index];
                        }
-                       // TODO add ModuleResolve event
-                       string location = Path.Combine(Path.GetDirectoryName(this.location), manifestModule.GetString(manifestModule.File.records[index].Name));
-                       return LoadModule(index, File.ReadAllBytes(location), location);
+                       return LoadModule(index, null, manifestModule.GetString(manifestModule.File.records[index].Name));
                }
 
-               private Module LoadModule(int index, byte[] rawModule, string location)
+               private Module LoadModule(int index, byte[] rawModule, string name)
                {
+                       string location = name == null ? null : Path.Combine(Path.GetDirectoryName(this.location), name);
                        if ((manifestModule.File.records[index].Flags & ContainsNoMetaData) != 0)
                        {
-                               return externalModules[index] = new ResourceModule(this, manifestModule.GetString(manifestModule.File.records[index].Name), location);
+                               return externalModules[index] = new ResourceModule(manifestModule, index, location);
                        }
                        else
                        {
+                               if (rawModule == null)
+                               {
+                                       try
+                                       {
+                                               rawModule = File.ReadAllBytes(location);
+                                       }
+                                       catch (FileNotFoundException)
+                                       {
+                                               if (resolvers != null)
+                                               {
+                                                       ResolveEventArgs arg = new ResolveEventArgs(name, this);
+                                                       foreach (ModuleResolveEventHandler resolver in resolvers)
+                                                       {
+                                                               Module module = resolver(this, arg);
+                                                               if (module != null)
+                                                               {
+                                                                       return module;
+                                                               }
+                                                       }
+                                               }
+                                               if (universe.MissingMemberResolution)
+                                               {
+                                                       return externalModules[index] = new MissingModule(this);
+                                               }
+                                               throw;
+                                       }
+                               }
                                return externalModules[index] = new ModuleReader(this, manifestModule.universe, new MemoryStream(rawModule), location);
                        }
                }
@@ -247,9 +312,19 @@ namespace IKVM.Reflection.Reader
                        return manifestModule.__GetReferencedAssemblies();
                }
 
+               protected override AssemblyNameFlags GetAssemblyFlags()
+               {
+                       return (AssemblyNameFlags)manifestModule.AssemblyTable.records[0].Flags;
+               }
+
+               internal string Name
+               {
+                       get { return manifestModule.GetString(manifestModule.AssemblyTable.records[0].Name); }
+               }
+
                internal override IList<CustomAttributeData> GetCustomAttributesData(Type attributeType)
                {
-                       return manifestModule.GetCustomAttributes(0x20000001, attributeType);
+                       return CustomAttributeData.GetCustomAttributesImpl(null, manifestModule, 0x20000001, attributeType) ?? CustomAttributeData.EmptyList;
                }
        }
 }