Browse Source

CecilLoader: When loading a multi-module assembly, load the main module only.

We cannot merge all the types from multi-module assemblies into the same IAssembly because duplicate type names may exist.
pull/32/merge
Daniel Grunwald 13 years ago
parent
commit
ab7b057e5d
  1. 74
      ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs

74
ICSharpCode.NRefactory/TypeSystem/CecilLoader.cs

@ -157,31 +157,46 @@ namespace ICSharpCode.NRefactory.TypeSystem
/// <summary> /// <summary>
/// Loads the assembly definition into a project content. /// Loads the assembly definition into a project content.
/// </summary> /// </summary>
/// <returns>IProjectContent that represents the assembly</returns> /// <returns>Unresolved type system representing the assembly</returns>
[CLSCompliant(false)] [CLSCompliant(false)]
public IUnresolvedAssembly LoadAssembly(AssemblyDefinition assemblyDefinition) public IUnresolvedAssembly LoadAssembly(AssemblyDefinition assemblyDefinition)
{ {
if (assemblyDefinition == null) if (assemblyDefinition == null)
throw new ArgumentNullException("assemblyDefinition"); throw new ArgumentNullException("assemblyDefinition");
return LoadModule(assemblyDefinition.MainModule);
}
/// <summary>
/// Loads the module definition into a project content.
/// </summary>
/// <returns>Unresolved type system representing the assembly</returns>
[CLSCompliant(false)]
public IUnresolvedAssembly LoadModule(ModuleDefinition moduleDefinition)
{
if (moduleDefinition == null)
throw new ArgumentNullException("moduleDefinition");
this.currentModule = assemblyDefinition.MainModule; this.currentModule = moduleDefinition;
// Read assembly and module attributes // Read assembly and module attributes
IList<IUnresolvedAttribute> assemblyAttributes = new List<IUnresolvedAttribute>(); IList<IUnresolvedAttribute> assemblyAttributes = new List<IUnresolvedAttribute>();
IList<IUnresolvedAttribute> moduleAttributes = new List<IUnresolvedAttribute>(); IList<IUnresolvedAttribute> moduleAttributes = new List<IUnresolvedAttribute>();
AddAttributes(assemblyDefinition, assemblyAttributes); AssemblyDefinition assemblyDefinition = moduleDefinition.Assembly;
AddAttributes(assemblyDefinition.MainModule, moduleAttributes); if (assemblyDefinition != null) {
AddAttributes(assemblyDefinition, assemblyAttributes);
}
AddAttributes(moduleDefinition, moduleAttributes);
assemblyAttributes = interningProvider.InternList(assemblyAttributes); assemblyAttributes = interningProvider.InternList(assemblyAttributes);
moduleAttributes = interningProvider.InternList(moduleAttributes); moduleAttributes = interningProvider.InternList(moduleAttributes);
this.currentAssembly = new CecilUnresolvedAssembly(assemblyDefinition.Name, this.DocumentationProvider); this.currentAssembly = new CecilUnresolvedAssembly(assemblyDefinition != null ? assemblyDefinition.Name.FullName : moduleDefinition.Name, this.DocumentationProvider);
currentAssembly.Location = assemblyDefinition.MainModule.FullyQualifiedName; currentAssembly.Location = moduleDefinition.FullyQualifiedName;
currentAssembly.AssemblyAttributes.AddRange(assemblyAttributes); currentAssembly.AssemblyAttributes.AddRange(assemblyAttributes);
currentAssembly.ModuleAttributes.AddRange(assemblyAttributes); currentAssembly.ModuleAttributes.AddRange(assemblyAttributes);
// Register type forwarders: // Register type forwarders:
foreach (ExportedType type in assemblyDefinition.MainModule.ExportedTypes) { foreach (ExportedType type in moduleDefinition.ExportedTypes) {
if (type.IsForwarder) { if (type.IsForwarder) {
int typeParameterCount; int typeParameterCount;
string ns = type.Namespace; string ns = type.Namespace;
@ -199,25 +214,23 @@ namespace ICSharpCode.NRefactory.TypeSystem
CecilLoader cecilLoaderCloneForLazyLoading = LazyLoad ? new CecilLoader(this) : null; CecilLoader cecilLoaderCloneForLazyLoading = LazyLoad ? new CecilLoader(this) : null;
List<TypeDefinition> cecilTypeDefs = new List<TypeDefinition>(); List<TypeDefinition> cecilTypeDefs = new List<TypeDefinition>();
List<DefaultUnresolvedTypeDefinition> typeDefs = new List<DefaultUnresolvedTypeDefinition>(); List<DefaultUnresolvedTypeDefinition> typeDefs = new List<DefaultUnresolvedTypeDefinition>();
foreach (ModuleDefinition module in assemblyDefinition.Modules) { foreach (TypeDefinition td in moduleDefinition.Types) {
foreach (TypeDefinition td in module.Types) { this.CancellationToken.ThrowIfCancellationRequested();
this.CancellationToken.ThrowIfCancellationRequested(); if (this.IncludeInternalMembers || (td.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public) {
if (this.IncludeInternalMembers || (td.Attributes & TypeAttributes.VisibilityMask) == TypeAttributes.Public) { string name = td.Name;
string name = td.Name; if (name.Length == 0)
if (name.Length == 0) continue;
continue;
if (this.LazyLoad) {
if (this.LazyLoad) { var t = new LazyCecilTypeDefinition(cecilLoaderCloneForLazyLoading, td);
var t = new LazyCecilTypeDefinition(cecilLoaderCloneForLazyLoading, td); currentAssembly.AddTypeDefinition(t);
currentAssembly.AddTypeDefinition(t); RegisterCecilObject(t, td);
RegisterCecilObject(t, td); } else {
} else { var t = CreateTopLevelTypeDefinition(td);
var t = CreateTopLevelTypeDefinition(td); cecilTypeDefs.Add(td);
cecilTypeDefs.Add(td); typeDefs.Add(t);
typeDefs.Add(t); currentAssembly.AddTypeDefinition(t);
currentAssembly.AddTypeDefinition(t); // The registration will happen after the members are initialized
// The registration will happen after the members are initialized
}
} }
} }
} }
@ -276,10 +289,9 @@ namespace ICSharpCode.NRefactory.TypeSystem
{ {
readonly IDocumentationProvider documentationProvider; readonly IDocumentationProvider documentationProvider;
public CecilUnresolvedAssembly(AssemblyNameDefinition assemblyName, IDocumentationProvider documentationProvider) public CecilUnresolvedAssembly(string fullAssemblyName, IDocumentationProvider documentationProvider)
: base(assemblyName.FullName) : base(fullAssemblyName)
{ {
Debug.Assert(assemblyName != null);
this.documentationProvider = documentationProvider; this.documentationProvider = documentationProvider;
} }
@ -299,8 +311,8 @@ namespace ICSharpCode.NRefactory.TypeSystem
if (fileName == null) if (fileName == null)
throw new ArgumentNullException("fileName"); throw new ArgumentNullException("fileName");
var param = new ReaderParameters { AssemblyResolver = new DummyAssemblyResolver() }; var param = new ReaderParameters { AssemblyResolver = new DummyAssemblyResolver() };
AssemblyDefinition asm = AssemblyDefinition.ReadAssembly(fileName, param); ModuleDefinition module = ModuleDefinition.ReadModule(fileName, param);
return LoadAssembly(asm); return LoadModule(module);
} }
// used to prevent Cecil from loading referenced assemblies // used to prevent Cecil from loading referenced assemblies

Loading…
Cancel
Save