Skip to content

Commit

Permalink
add ExportedType, nested TypeReferences
Browse files Browse the repository at this point in the history
  • Loading branch information
James May committed Oct 5, 2023
1 parent d11b4a1 commit 55b100e
Show file tree
Hide file tree
Showing 12 changed files with 223 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
using ICSharpCode.Decompiler.IL;
using ICSharpCode.Decompiler.Metadata;

using SRM = System.Reflection.Metadata;

namespace ICSharpCode.Decompiler.Disassembler
{
/// <summary>
Expand Down Expand Up @@ -2016,7 +2018,7 @@ public void WriteModuleHeader(PEFile module, bool skipMVID = false)
{
var metadata = module.Metadata;

void WriteExportedType(ExportedType exportedType)
void WriteExportedType(SRM.ExportedType exportedType)
{
if (!exportedType.Namespace.IsNil)
{
Expand Down
1 change: 1 addition & 0 deletions ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@
<Compile Include="IL\Transforms\TransformDisplayClassUsage.cs" />
<Compile Include="IL\Transforms\UserDefinedLogicTransform.cs" />
<Compile Include="Metadata\AssemblyReferences.cs" />
<Compile Include="Metadata\ExportedType.cs" />
<Compile Include="Metadata\MemberReference.cs" />
<Compile Include="Metadata\TypeReference.cs" />
<Compile Include="Metadata\CodeMappingInfo.cs" />
Expand Down
21 changes: 19 additions & 2 deletions ICSharpCode.Decompiler/Metadata/AssemblyReferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,6 @@ public string FullName {
}

ImmutableArray<TypeReference> typeReferences;

public ImmutableArray<TypeReference> TypeReferences {
get {
var value = typeReferences;
Expand All @@ -287,14 +286,32 @@ public ImmutableArray<TypeReference> TypeReferences {
.Select(r => new TypeReference(Metadata, r))
.Where(r => r.ResolutionScope == Handle)
.OrderBy(r => r.Namespace)
.ThenBy(r=> r.Name)
.ThenBy(r => r.Name)
.ToImmutableArray();
typeReferences = value;
}
return value;
}
}

ImmutableArray<ExportedType> exportedTypes;
public ImmutableArray<ExportedType> ExportedTypes {
get {
var value = exportedTypes;
if (value.IsDefault)
{
value = Metadata.ExportedTypes
.Select(r => new ExportedType(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
100 changes: 100 additions & 0 deletions ICSharpCode.Decompiler/Metadata/ExportedType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// 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 sealed class ExportedType
{
readonly System.Reflection.Metadata.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<ExportedType> exportedTypes;
public ImmutableArray<ExportedType> ExportedTypes {
get {
var value = exportedTypes;
if (value.IsDefault)
{
value = Metadata.ExportedTypes
.Select(r => new ExportedType(Metadata, r))
.Where(r => r.Implementation == Handle)
.OrderBy(r => r.Namespace)
.ThenBy(r => r.Name)
.ToImmutableArray();
exportedTypes = value;
}
return value;
}
}

public ExportedType(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
}
17 changes: 17 additions & 0 deletions ICSharpCode.Decompiler/Metadata/TypeReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,23 @@ public ImmutableArray<MemberReference> MemberReferences {
}
}

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

public TypeReference(MetadataReader metadata, TypeReferenceHandle handle)
{
Metadata = metadata ?? throw new ArgumentNullException(nameof(metadata));
Expand Down
6 changes: 4 additions & 2 deletions ICSharpCode.Decompiler/TypeSystem/MetadataModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
using ICSharpCode.Decompiler.TypeSystem.Implementation;
using ICSharpCode.Decompiler.Util;

using SRM = System.Reflection.Metadata;

namespace ICSharpCode.Decompiler.TypeSystem
{
/// <summary>
Expand Down Expand Up @@ -885,7 +887,7 @@ void AddTypeForwarderAttributes(ref AttributeListBuilder b)
}
}

IType ResolveForwardedType(ExportedType forwarder)
IType ResolveForwardedType(SRM.ExportedType forwarder)
{
IModule module = ResolveModule(forwarder);
var typeName = forwarder.GetFullTypeName(metadata);
Expand All @@ -904,7 +906,7 @@ IType ResolveForwardedType(ExportedType forwarder)
}
return new UnknownType(typeName);

IModule ResolveModule(ExportedType type)
IModule ResolveModule(SRM.ExportedType type)
{
switch (type.Implementation.Kind)
{
Expand Down
1 change: 1 addition & 0 deletions ILSpy/Languages/CSharpLanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,7 @@ public override string GetEntityName(PEFile module, EntityHandle handle, bool fu
if (fullName && !declaringType.IsNil)
return ToCSharpString(metadata, declaringType, fullName, omitGenerics) + "." + metadata.GetString(pd.Name);
return metadata.GetString(pd.Name);
case HandleKind.ExportedType:
case HandleKind.TypeReference:
case HandleKind.MemberReference:
// TODO: C# Implementation
Expand Down
3 changes: 3 additions & 0 deletions ILSpy/Languages/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,9 @@ public virtual string GetEntityName(PEFile module, EntityHandle handle, bool ful
if (fullName)
return EscapeName(declaringType.GetFullTypeName(metadata).ToILNameString(omitGenerics) + "." + metadata.GetString(pd.Name));
return EscapeName(metadata.GetString(pd.Name));
case HandleKind.ExportedType:
var et = metadata.GetExportedType((ExportedTypeHandle)handle);
return et.GetFullTypeName(metadata).ToILNameString();
case HandleKind.TypeReference:
var tr = metadata.GetTypeReference((TypeReferenceHandle)handle);
if (fullName)
Expand Down
6 changes: 4 additions & 2 deletions ILSpy/Metadata/CorTables/ExportedTypeTableTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
using ICSharpCode.Decompiler.IL;
using ICSharpCode.Decompiler.Metadata;

using SRM = System.Reflection.Metadata;

namespace ICSharpCode.ILSpy.Metadata
{
internal class ExportedTypeTableTreeNode : MetadataTableTreeNode
Expand Down Expand Up @@ -76,7 +78,7 @@ struct ExportedTypeEntry
readonly PEFile module;
readonly MetadataReader metadata;
readonly ExportedTypeHandle handle;
readonly ExportedType type;
readonly SRM.ExportedType type;

public int RID => MetadataTokens.GetRowNumber(handle);

Expand Down Expand Up @@ -121,7 +123,7 @@ public void OnImplementationClick()
string implementationTooltip;
public string ImplementationTooltip => GenerateTooltip(ref implementationTooltip, module, type.Implementation);

public ExportedTypeEntry(int metadataOffset, PEFile module, ExportedTypeHandle handle, ExportedType type)
public ExportedTypeEntry(int metadataOffset, PEFile module, ExportedTypeHandle handle, SRM.ExportedType type)
{
this.metadataOffset = metadataOffset;
this.module = module;
Expand Down
7 changes: 5 additions & 2 deletions ILSpy/TreeNodes/AssemblyReferenceReferencedTypesTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ public AssemblyReferenceReferencedTypesTreeNode(PEFile module, AssemblyReference
this.LazyLoading = true;
}

public override object Text => $"Referenced Types ({r.TypeReferences.Length})";
public override object Text => $"Referenced Types ({r.TypeReferences.Length + r.ExportedTypes.Length})";
public override object Icon => Images.Class;

protected override void LoadChildren()
{
foreach (var typeRef in r.TypeReferences)
this.Children.Add(new TypeReferenceTreeNode(module, typeRef));

foreach (var exportedType in r.ExportedTypes)
this.Children.Add(new ExportedTypeTreeNode(module, exportedType));
}

public override bool ShowExpander => !r.TypeReferences.IsEmpty;
public override bool ShowExpander => !r.TypeReferences.IsEmpty || !r.ExportedTypes.IsEmpty;

public override void Decompile(Language language, ITextOutput output, DecompilationOptions options)
{
Expand Down
62 changes: 62 additions & 0 deletions ILSpy/TreeNodes/ExportedTypeTreeNode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// 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.

using System;

using ICSharpCode.Decompiler;
using ICSharpCode.Decompiler.Metadata;

using ExportedType = ICSharpCode.Decompiler.Metadata.ExportedType;

namespace ICSharpCode.ILSpy.TreeNodes
{
/// <summary>
/// exported type within assembly reference list.
/// </summary>
public sealed class ExportedTypeTreeNode : ILSpyTreeNode
{
readonly PEFile module;
private readonly ExportedType r;

public ExportedTypeTreeNode(PEFile module, ExportedType r)
{
this.module = module ?? throw new ArgumentNullException(nameof(module));
this.r = r ?? throw new ArgumentNullException(nameof(r));

this.LazyLoading = true;
}

public override object Text
=> Language.GetEntityName(module, r.Handle, fullName: true, omitGenerics: false) + GetSuffixString(r.Handle);

public override object Icon => Images.Library;

protected override void LoadChildren()
{
foreach (var exportedType in r.ExportedTypes)
this.Children.Add(new ExportedTypeTreeNode(module, exportedType));
}

public override bool ShowExpander => !r.ExportedTypes.IsEmpty;

public override void Decompile(Language language, ITextOutput output, DecompilationOptions options)
{
language.WriteCommentLine(output, $"{Language.GetEntityName(module, r.Handle, fullName: true, omitGenerics: false)} (Exported, IsForwarder: {r.IsForwarder}, Attributes: {r.Attributes})");
}
}
}
5 changes: 4 additions & 1 deletion ILSpy/TreeNodes/TypeReferenceTreeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,14 @@ public override object Text

protected override void LoadChildren()
{
foreach (var typeRef in r.TypeReferences)
this.Children.Add(new TypeReferenceTreeNode(module, typeRef));

foreach (var memberRef in r.MemberReferences)
this.Children.Add(new MemberReferenceTreeNode(module, memberRef));
}

public override bool ShowExpander => !r.MemberReferences.IsEmpty;
public override bool ShowExpander => !r.TypeReferences.IsEmpty || !r.MemberReferences.IsEmpty;

public override void Decompile(Language language, ITextOutput output, DecompilationOptions options)
{
Expand Down

0 comments on commit 55b100e

Please sign in to comment.