Skip to content

Commit

Permalink
Merge pull request #3092 from fowl2/assemblyUsedBy
Browse files Browse the repository at this point in the history
add "Referenced Types" under References
  • Loading branch information
siegfriedpammer authored Mar 16, 2024
2 parents e315e92 + 2e77720 commit f038055
Show file tree
Hide file tree
Showing 22 changed files with 1,010 additions and 48 deletions.
4 changes: 4 additions & 0 deletions ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
<Compile Include="Disassembler\IEntityProcessor.cs" />
<Compile Include="Disassembler\SortByNameProcessor.cs" />
<Compile Include="IL\ApplyPdbLocalTypeInfoTypeVisitor.cs" />
<Compile Include="Metadata\ModuleReferenceMetadata.cs" />
<Compile Include="NRTAttributes.cs" />
<Compile Include="PartialTypeInfo.cs" />
<Compile Include="CSharp\ProjectDecompiler\IProjectFileWriter.cs" />
Expand Down Expand Up @@ -362,6 +363,9 @@
<Compile Include="IL\Transforms\TransformDisplayClassUsage.cs" />
<Compile Include="IL\Transforms\UserDefinedLogicTransform.cs" />
<Compile Include="Metadata\AssemblyReferences.cs" />
<Compile Include="Metadata\ExportedTypeMetadata.cs" />
<Compile Include="Metadata\MemberReferenceMetadata.cs" />
<Compile Include="Metadata\TypeReferenceMetadata.cs" />
<Compile Include="Metadata\CodeMappingInfo.cs" />
<Compile Include="Metadata\EnumUnderlyingTypeResolveException.cs" />
<Compile Include="Metadata\MetadataTokenHelpers.cs" />
Expand Down
37 changes: 37 additions & 0 deletions ICSharpCode.Decompiler/Metadata/AssemblyReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#nullable enable

using System;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection;
Expand Down Expand Up @@ -275,6 +276,42 @@ public string FullName {
return bytes;
}

ImmutableArray<TypeReferenceMetadata> typeReferences;
public ImmutableArray<TypeReferenceMetadata> TypeReferences {
get {
var value = typeReferences;
if (value.IsDefault)
{
value = Metadata.TypeReferences
.Select(r => new TypeReferenceMetadata(Metadata, r))
.Where(r => r.ResolutionScope == Handle)
.OrderBy(r => r.Namespace)
.ThenBy(r => r.Name)
.ToImmutableArray();
typeReferences = value;
}
return value;
}
}

ImmutableArray<ExportedTypeMetadata> exportedTypes;
public ImmutableArray<ExportedTypeMetadata> ExportedTypes {
get {
var value = exportedTypes;
if (value.IsDefault)
{
value = Metadata.ExportedTypes
.Select(r => new ExportedTypeMetadata(Metadata, r))
.Where(r => r.Implementation == Handle)
.OrderBy(r => r.Namespace)
.ThenBy(r => r.Name)
.ToImmutableArray();
exportedTypes = value;
}
return value;
}
}

public AssemblyReference(MetadataReader metadata, AssemblyReferenceHandle handle)
{
if (metadata == null)
Expand Down
103 changes: 103 additions & 0 deletions ICSharpCode.Decompiler/Metadata/ExportedTypeMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright (c) 2023 James May
//
// 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.

#nullable enable

using System;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;

namespace ICSharpCode.Decompiler.Metadata
{
#if !VSADDIN
/// <summary>
/// Convenience wrapper for <see cref="ExportedType"/> and <see cref="ExportedTypeHandle"/>.
/// </summary>
public sealed class ExportedTypeMetadata
{
readonly ExportedType entry;

public MetadataReader Metadata { get; }
public ExportedTypeHandle Handle { get; }

string? name;
public string Name {
get {
try
{
return name ??= Metadata.GetString(entry.Name);
}
catch (BadImageFormatException)
{
return name = $"ET:{Handle}";
}
}
}

string? @namespace;
public string Namespace {
get {
try
{
return @namespace ??= Metadata.GetString(entry.Namespace);
}
catch (BadImageFormatException)
{
return @namespace = $"namespace(ET:{Handle})";
}
}
}

public EntityHandle Implementation => entry.Implementation;
public TypeAttributes Attributes => entry.Attributes;
public bool IsForwarder => entry.IsForwarder;
public NamespaceDefinition NamespaceDefinition => Metadata.GetNamespaceDefinition(entry.NamespaceDefinition);

ImmutableArray<ExportedTypeMetadata> exportedTypes;
public ImmutableArray<ExportedTypeMetadata> ExportedTypes {
get {
var value = exportedTypes;
if (value.IsDefault)
{
value = Metadata.ExportedTypes
.Select(r => new ExportedTypeMetadata(Metadata, r))
.Where(r => r.Implementation == Handle)
.OrderBy(r => r.Namespace)
.ThenBy(r => r.Name)
.ToImmutableArray();
exportedTypes = value;
}
return value;
}
}

public ExportedTypeMetadata(MetadataReader metadata, ExportedTypeHandle handle)
{
Metadata = metadata ?? throw new ArgumentNullException(nameof(metadata));
if (handle.IsNil)
throw new ArgumentNullException(nameof(handle));
Handle = handle;
entry = metadata.GetExportedType(handle);
}

public override string ToString() => $"{Namespace}::{Name}";
}
#endif
}
69 changes: 69 additions & 0 deletions ICSharpCode.Decompiler/Metadata/MemberReferenceMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) 2023 James May
//
// 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.

#nullable enable

using System;
using System.Reflection.Metadata;

namespace ICSharpCode.Decompiler.Metadata
{
#if !VSADDIN
/// <summary>
/// Convenience wrapper for <see cref="MemberReference"/> and <see cref="MemberReferenceHandle"/>.
/// </summary>
public sealed class MemberReferenceMetadata
{
readonly MemberReference entry;

public MetadataReader Metadata { get; }
public MemberReferenceHandle Handle { get; }

string? name;

public string Name {
get {
try
{
return name ??= Metadata.GetString(entry.Name);
}
catch (BadImageFormatException)
{
return name = $"MR:{Handle}";
}
}
}

public EntityHandle Parent => entry.Parent;

public MemberReferenceKind MemberReferenceKind => entry.GetKind();

public MemberReferenceMetadata(MetadataReader metadata, MemberReferenceHandle handle)
{
Metadata = metadata ?? throw new ArgumentNullException(nameof(metadata));
if (handle.IsNil)
throw new ArgumentNullException(nameof(handle));
Handle = handle;
entry = metadata.GetMemberReference(handle);
}

public override string ToString()
=> Name;
}
#endif
}
132 changes: 132 additions & 0 deletions ICSharpCode.Decompiler/Metadata/ModuleReferenceMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright (c) 2023 James May
//
// 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.

#nullable enable

using System;
using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata;

namespace ICSharpCode.Decompiler.Metadata
{
#if !VSADDIN
public class ModuleReferenceMetadata /* : IModuleReference*/
{
readonly ModuleReference entry;

public MetadataReader Metadata { get; }
public ModuleReferenceHandle Handle { get; }

string? name;
public string Name {
get {
if (name == null)
{
try
{
name = Metadata.GetString(entry.Name);
}
catch (BadImageFormatException)
{
name = $"AR:{Handle}";
}
}
return name;
}
}

ImmutableArray<CustomAttribute> attributes;
public ImmutableArray<CustomAttribute> Attributes {
get {
var value = attributes;
if (value.IsDefault)
{
value = entry.GetCustomAttributes().Select(Metadata.GetCustomAttribute).ToImmutableArray();
attributes = value;
}
return value;
}
}

ImmutableArray<TypeReferenceMetadata> typeReferences;
public ImmutableArray<TypeReferenceMetadata> TypeReferences {
get {
var value = typeReferences;
if (value.IsDefault)
{
value = Metadata.TypeReferences
.Select(r => new TypeReferenceMetadata(Metadata, r))
.Where(r => r.ResolutionScope == Handle)
.OrderBy(r => r.Namespace)
.ThenBy(r => r.Name)
.ToImmutableArray();
typeReferences = value;
}
return value;
}
}

ImmutableArray<ExportedTypeMetadata> exportedTypes;
public ImmutableArray<ExportedTypeMetadata> ExportedTypes {
get {
var value = exportedTypes;
if (value.IsDefault)
{
value = Metadata.ExportedTypes
.Select(r => new ExportedTypeMetadata(Metadata, r))
.Where(r => r.Implementation == Handle)
.OrderBy(r => r.Namespace)
.ThenBy(r => r.Name)
.ToImmutableArray();
exportedTypes = value;
}
return value;
}
}

public ModuleReferenceMetadata(MetadataReader metadata, ModuleReferenceHandle handle)
{
if (metadata == null)
throw new ArgumentNullException(nameof(metadata));
if (handle.IsNil)
throw new ArgumentNullException(nameof(handle));
Metadata = metadata;
Handle = handle;
entry = metadata.GetModuleReference(handle);
}

public ModuleReferenceMetadata(PEFile module, ModuleReferenceHandle handle)
{
if (module == null)
throw new ArgumentNullException(nameof(module));
if (handle.IsNil)
throw new ArgumentNullException(nameof(handle));
Metadata = module.Metadata;
Handle = handle;
entry = Metadata.GetModuleReference(handle);
}

public override string ToString()
{
return Name;
}
}
#endif
}
16 changes: 16 additions & 0 deletions ICSharpCode.Decompiler/Metadata/PEFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@ public ImmutableArray<AssemblyReference> AssemblyReferences {
}
}

ImmutableArray<ModuleReferenceMetadata> moduleReferences;
public ImmutableArray<ModuleReferenceMetadata> ModuleReferences {
get {
var value = moduleReferences;
if (value.IsDefault)
{
value = Metadata.GetModuleReferences()
.Select(m => new ModuleReferenceMetadata(this, m))
.ToImmutableArray();

moduleReferences = value;
}
return value;
}
}

public ImmutableArray<Resource> Resources => GetResources().ToImmutableArray();

IEnumerable<Resource> GetResources()
Expand Down
Loading

0 comments on commit f038055

Please sign in to comment.