mirror of https://github.com/icsharpcode/ILSpy.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
183 lines
7.4 KiB
183 lines
7.4 KiB
// Copyright (c) 2018 Daniel Grunwald |
|
// |
|
// Permission is hereby granted, free of charge, to any person obtaining a copy of this |
|
// software and associated documentation files (the "Software"), to deal in the Software |
|
// without restriction, including without limitation the rights to use, copy, modify, merge, |
|
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons |
|
// to whom the Software is furnished to do so, subject to the following conditions: |
|
// |
|
// The above copyright notice and this permission notice shall be included in all copies or |
|
// substantial portions of the Software. |
|
// |
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
|
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
|
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
|
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|
// DEALINGS IN THE SOFTWARE. |
|
|
|
using System; |
|
using System.Collections.Generic; |
|
using System.Linq; |
|
using SRM = System.Reflection.Metadata; |
|
using ICSharpCode.Decompiler.TypeSystem.Implementation; |
|
using ICSharpCode.Decompiler.Util; |
|
|
|
using static ICSharpCode.Decompiler.Metadata.MetadataExtensions; |
|
using System.Diagnostics; |
|
using System.Collections.Immutable; |
|
using ICSharpCode.Decompiler.Metadata; |
|
|
|
namespace ICSharpCode.Decompiler.TypeSystem |
|
{ |
|
/// <summary> |
|
/// Options that control how metadata is represented in the type system. |
|
/// </summary> |
|
[Flags] |
|
public enum TypeSystemOptions |
|
{ |
|
/// <summary> |
|
/// No options enabled; stay as close to the metadata as possible. |
|
/// </summary> |
|
None = 0, |
|
/// <summary> |
|
/// [DynamicAttribute] is used to replace 'object' types with the 'dynamic' type. |
|
/// |
|
/// If this option is not active, the 'dynamic' type is not used, and the attribute is preserved. |
|
/// </summary> |
|
Dynamic = 1, |
|
/// <summary> |
|
/// Tuple types are represented using the TupleType class. |
|
/// [TupleElementNames] is used to name the tuple elements. |
|
/// |
|
/// If this option is not active, the tuples are represented using their underlying type, and the attribute is preserved. |
|
/// </summary> |
|
Tuple = 2, |
|
/// <summary> |
|
/// If this option is active, [ExtensionAttribute] is removed and methods are marked as IsExtensionMethod. |
|
/// Otherwise, the attribute is preserved but the methods are not marked. |
|
/// </summary> |
|
ExtensionMethods = 4, |
|
/// <summary> |
|
/// Only load the public API into the type system. |
|
/// </summary> |
|
OnlyPublicAPI = 8, |
|
/// <summary> |
|
/// Default settings: all features enabled. |
|
/// </summary> |
|
Default = Dynamic | Tuple | ExtensionMethods |
|
} |
|
|
|
/// <summary> |
|
/// Manages the NRefactory type system for the decompiler. |
|
/// </summary> |
|
/// <remarks> |
|
/// This class is thread-safe. |
|
/// </remarks> |
|
public class DecompilerTypeSystem : SimpleCompilation, IDecompilerTypeSystem |
|
{ |
|
public DecompilerTypeSystem(PEFile mainModule, IAssemblyResolver assemblyResolver) |
|
: this(mainModule, assemblyResolver, TypeSystemOptions.Default) |
|
{ |
|
} |
|
|
|
static TypeSystemOptions GetOptions(DecompilerSettings settings) |
|
{ |
|
var typeSystemOptions = TypeSystemOptions.None; |
|
if (settings.Dynamic) |
|
typeSystemOptions |= TypeSystemOptions.Dynamic; |
|
if (settings.TupleTypes) |
|
typeSystemOptions |= TypeSystemOptions.Tuple; |
|
if (settings.ExtensionMethods) |
|
typeSystemOptions |= TypeSystemOptions.ExtensionMethods; |
|
return typeSystemOptions; |
|
} |
|
|
|
public DecompilerTypeSystem(PEFile mainModule, IAssemblyResolver assemblyResolver, DecompilerSettings settings) |
|
: this(mainModule, assemblyResolver, GetOptions(settings ?? throw new ArgumentNullException(nameof(settings)))) |
|
{ |
|
} |
|
|
|
public DecompilerTypeSystem(PEFile mainModule, IAssemblyResolver assemblyResolver, TypeSystemOptions typeSystemOptions) |
|
{ |
|
if (mainModule == null) |
|
throw new ArgumentNullException(nameof(mainModule)); |
|
if (assemblyResolver == null) |
|
throw new ArgumentNullException(nameof(assemblyResolver)); |
|
// Load referenced assemblies and type-forwarder references. |
|
// This is necessary to make .NET Core/PCL binaries work better. |
|
var referencedAssemblies = new List<PEFile>(); |
|
var assemblyReferenceQueue = new Queue<(bool IsAssembly, PEFile MainModule, object Reference)>(); |
|
var mainMetadata = mainModule.Metadata; |
|
foreach (var h in mainMetadata.GetModuleReferences()) { |
|
var moduleRef = mainMetadata.GetModuleReference(h); |
|
var moduleName = mainMetadata.GetString(moduleRef.Name); |
|
foreach (var fileHandle in mainMetadata.AssemblyFiles) { |
|
var file = mainMetadata.GetAssemblyFile(fileHandle); |
|
if (mainMetadata.StringComparer.Equals(file.Name, moduleName) && file.ContainsMetadata) { |
|
assemblyReferenceQueue.Enqueue((false, mainModule, moduleName)); |
|
break; |
|
} |
|
} |
|
} |
|
foreach (var refs in mainModule.AssemblyReferences) { |
|
assemblyReferenceQueue.Enqueue((true, mainModule, refs)); |
|
} |
|
var comparer = KeyComparer.Create(((bool IsAssembly, PEFile MainModule, object Reference) reference) => |
|
reference.IsAssembly ? "A:" + ((AssemblyReference)reference.Reference).FullName : |
|
"M:" + reference.Reference); |
|
var processedAssemblyReferences = new HashSet<(bool IsAssembly, PEFile Parent, object Reference)>(comparer); |
|
while (assemblyReferenceQueue.Count > 0) { |
|
var asmRef = assemblyReferenceQueue.Dequeue(); |
|
if (!processedAssemblyReferences.Add(asmRef)) |
|
continue; |
|
PEFile asm; |
|
if (asmRef.IsAssembly) { |
|
asm = assemblyResolver.Resolve((AssemblyReference)asmRef.Reference); |
|
} else { |
|
asm = assemblyResolver.ResolveModule(asmRef.MainModule, (string)asmRef.Reference); |
|
} |
|
if (asm != null) { |
|
referencedAssemblies.Add(asm); |
|
var metadata = asm.Metadata; |
|
foreach (var h in metadata.ExportedTypes) { |
|
var exportedType = metadata.GetExportedType(h); |
|
switch (exportedType.Implementation.Kind) { |
|
case SRM.HandleKind.AssemblyReference: |
|
assemblyReferenceQueue.Enqueue((true, asm, new AssemblyReference(asm, (SRM.AssemblyReferenceHandle)exportedType.Implementation))); |
|
break; |
|
case SRM.HandleKind.AssemblyFile: |
|
var file = metadata.GetAssemblyFile((SRM.AssemblyFileHandle)exportedType.Implementation); |
|
assemblyReferenceQueue.Enqueue((false, asm, metadata.GetString(file.Name))); |
|
break; |
|
} |
|
} |
|
} |
|
} |
|
var mainModuleWithOptions = mainModule.WithOptions(typeSystemOptions); |
|
var referencedAssembliesWithOptions = referencedAssemblies.Select(file => file.WithOptions(typeSystemOptions)); |
|
// Primitive types are necessary to avoid assertions in ILReader. |
|
// Fallback to MinimalCorlib to provide the primitive types. |
|
if (!HasType(KnownTypeCode.Void) || !HasType(KnownTypeCode.Int32)) { |
|
Init(mainModule.WithOptions(typeSystemOptions), referencedAssembliesWithOptions.Concat(new[] { MinimalCorlib.Instance })); |
|
} else { |
|
Init(mainModuleWithOptions, referencedAssembliesWithOptions); |
|
} |
|
this.MainModule = (MetadataModule)base.MainModule; |
|
|
|
bool HasType(KnownTypeCode code) |
|
{ |
|
TopLevelTypeName name = KnownTypeReference.Get(code).TypeName; |
|
if (mainModule.GetTypeDefinition(name) != null) |
|
return true; |
|
foreach (var file in referencedAssemblies) { |
|
if (file.GetTypeDefinition(name) != null) |
|
return true; |
|
} |
|
return false; |
|
} |
|
} |
|
|
|
public new MetadataModule MainModule { get; } |
|
} |
|
}
|
|
|