Skip to content

Commit f27066c

Browse files
Add ExtensionInfo: mapping of extension members to extension implementations and more.
1 parent 496da98 commit f27066c

File tree

7 files changed

+234
-0
lines changed

7 files changed

+234
-0
lines changed

ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemLoaderTests.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,5 +1993,23 @@ public void HasSpecialName()
19931993
Assert.That(@class.HasAttribute(KnownAttribute.SpecialName));
19941994
Assert.That(@struct.HasAttribute(KnownAttribute.SpecialName));
19951995
}
1996+
1997+
[Test]
1998+
public void ExtensionEverything()
1999+
{
2000+
var extensionEverything = GetTypeDefinition(typeof(ExtensionEverything));
2001+
Assert.That(extensionEverything.IsStatic, Is.True, "ExtensionEverything should be static");
2002+
Assert.That(extensionEverything.HasExtensions, Is.True, "ExtensionEverything should have extensions");
2003+
var info = extensionEverything.ExtensionInfo;
2004+
Assert.That(info, Is.Not.Null, "ExtensionEverything should have ExtensionInfo");
2005+
foreach (var method in extensionEverything.Methods)
2006+
{
2007+
Assert.That(method.IsStatic, Is.True, "Method should be static: " + method.Name);
2008+
ExtensionMemberInfo? infoOfImpl = info.InfoOfImplementationMember(method);
2009+
Assert.That(infoOfImpl, Is.Not.Null, "Method should have implementation info: " + method.Name);
2010+
ExtensionMemberInfo? infoOfExtension = info.InfoOfExtensionMember(infoOfImpl.Value.ExtensionMember);
2011+
Assert.That(infoOfExtension, Is.EqualTo(infoOfImpl), "Info of extension member should be equal to info of implementation member: " + method.Name);
2012+
}
2013+
}
19962014
}
19972015
}

ICSharpCode.Decompiler.Tests/TypeSystem/TypeSystemTestCase.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -753,4 +753,24 @@ public interface IMarshalAsTests
753753
[DispId(11)]
754754
void StopRouter();
755755
}
756+
757+
public static class ExtensionEverything
758+
{
759+
extension(int input)
760+
{
761+
public void Method() { }
762+
public void Method(char c) { }
763+
public string AsString => input.ToString();
764+
public string Test {
765+
get => "Test";
766+
set { }
767+
}
768+
public static void StaticMethod() { }
769+
public static void StaticMethod(double x) { }
770+
public static string StaticProperty => "StaticProperty";
771+
public static void GenericMethod<T>(T value)
772+
{
773+
}
774+
}
775+
}
756776
}

ICSharpCode.Decompiler/ICSharpCode.Decompiler.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@
155155
<Compile Include="Metadata\MetadataGenericContext.cs" />
156156
<Compile Include="Metadata\ReferenceLoadInfo.cs" />
157157
<Compile Include="Properties\DecompilerVersionInfo.cs" />
158+
<Compile Include="TypeSystem\ExtensionInfo.cs" />
158159
<Compile Include="TypeSystem\ITypeDefinitionOrUnknown.cs" />
159160
<Compile Include="Util\BitOperations.cs" />
160161
<Compile Include="Util\DelegateComparer.cs" />
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// Copyright (c) 2025 Daniel Grunwald
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
4+
// software and associated documentation files (the "Software"), to deal in the Software
5+
// without restriction, including without limitation the rights to use, copy, modify, merge,
6+
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
7+
// to whom the Software is furnished to do so, subject to the following conditions:
8+
//
9+
// The above copyright notice and this permission notice shall be included in all copies or
10+
// substantial portions of the Software.
11+
//
12+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
13+
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14+
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
15+
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
16+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
17+
// DEALINGS IN THE SOFTWARE.
18+
19+
#nullable enable
20+
21+
using System.Collections.Generic;
22+
using System.Linq;
23+
using System.Reflection.Metadata;
24+
25+
namespace ICSharpCode.Decompiler.TypeSystem
26+
{
27+
public class ExtensionInfo
28+
{
29+
readonly Dictionary<IMember, ExtensionMemberInfo> extensionMemberMap;
30+
readonly Dictionary<IMember, ExtensionMemberInfo> implementationMemberMap;
31+
32+
public ExtensionInfo(MetadataModule module, ITypeDefinition extensionContainer)
33+
{
34+
this.extensionMemberMap = new();
35+
this.implementationMemberMap = new();
36+
37+
var metadata = module.MetadataFile.Metadata;
38+
39+
foreach (var extGroup in extensionContainer.NestedTypes)
40+
{
41+
if (!(extGroup is { Kind: TypeKind.Class, IsSealed: true }
42+
&& extGroup.Name.StartsWith("<>E__", System.StringComparison.Ordinal)))
43+
{
44+
continue;
45+
}
46+
47+
TypeDefinition td = metadata.GetTypeDefinition((TypeDefinitionHandle)extGroup.MetadataToken);
48+
IMethod? marker = null;
49+
bool hasMultipleMarkers = false;
50+
List<IMethod> extensionMethods = [];
51+
52+
// For easier access to accessors we use SRM
53+
foreach (var h in td.GetMethods())
54+
{
55+
var method = module.GetDefinition(h);
56+
57+
if (method.SymbolKind is SymbolKind.Constructor)
58+
continue;
59+
if (method is { Name: "<Extension>$", IsStatic: true, Parameters.Count: 1 })
60+
{
61+
if (marker == null)
62+
marker = method;
63+
else
64+
hasMultipleMarkers = true;
65+
continue;
66+
}
67+
68+
extensionMethods.Add(method);
69+
}
70+
71+
if (marker == null || hasMultipleMarkers)
72+
continue;
73+
74+
foreach (var extension in extensionMethods)
75+
{
76+
int expectedTypeParameterCount = extension.TypeParameters.Count + extGroup.TypeParameterCount;
77+
bool hasInstance = !extension.IsStatic;
78+
int parameterOffset = hasInstance ? 1 : 0;
79+
int expectedParameterCount = extension.Parameters.Count + parameterOffset;
80+
TypeParameterSubstitution subst = new TypeParameterSubstitution([], [.. extGroup.TypeArguments, .. extension.TypeArguments]);
81+
82+
bool IsMatchingImplementation(IMethod impl)
83+
{
84+
if (!impl.IsStatic)
85+
return false;
86+
if (extension.Name != impl.Name)
87+
return false;
88+
if (expectedTypeParameterCount != impl.TypeParameters.Count)
89+
return false;
90+
if (expectedParameterCount != impl.Parameters.Count)
91+
return false;
92+
if (hasInstance)
93+
{
94+
IType ti = impl.Parameters[0].Type.AcceptVisitor(subst);
95+
IType tm = marker.Parameters.Single().Type;
96+
if (!NormalizeTypeVisitor.TypeErasure.EquivalentTypes(ti, tm))
97+
return false;
98+
}
99+
for (int i = 0; i < extension.Parameters.Count; i++)
100+
{
101+
IType ti = impl.Parameters[i + parameterOffset].Type.AcceptVisitor(subst);
102+
IType tm = extension.Parameters[i].Type;
103+
if (!NormalizeTypeVisitor.TypeErasure.EquivalentTypes(ti, tm))
104+
return false;
105+
}
106+
return NormalizeTypeVisitor.TypeErasure.EquivalentTypes(
107+
impl.ReturnType.AcceptVisitor(subst),
108+
extension.ReturnType
109+
);
110+
}
111+
112+
foreach (var impl in extensionContainer.Methods)
113+
{
114+
if (!IsMatchingImplementation(impl))
115+
continue;
116+
var emi = new ExtensionMemberInfo(marker, extension, impl);
117+
extensionMemberMap[extension] = emi;
118+
implementationMemberMap[impl] = emi;
119+
}
120+
}
121+
}
122+
123+
}
124+
125+
public ExtensionMemberInfo? InfoOfExtensionMember(IMethod method)
126+
{
127+
return this.extensionMemberMap.TryGetValue(method, out var value) ? value : null;
128+
}
129+
130+
public ExtensionMemberInfo? InfoOfImplementationMember(IMethod method)
131+
{
132+
return this.implementationMemberMap.TryGetValue(method, out var value) ? value : null;
133+
}
134+
135+
public IEnumerable<IGrouping<IMethod, ExtensionMemberInfo>> GetGroups()
136+
{
137+
return this.extensionMemberMap.Values.GroupBy(x => x.ExtensionMarkerMethod);
138+
}
139+
}
140+
141+
public readonly struct ExtensionMemberInfo(IMethod marker, IMethod extension, IMethod implementation)
142+
{
143+
/// <summary>
144+
/// Metadata-only method called '&lt;Extension&gt;$'. Has the C# signature for the extension declaration.
145+
///
146+
/// <code>extension(ReceiverType name) {} -> void &lt;Extension&gt;$(ReceiverType name) {}</code>
147+
/// </summary>
148+
public readonly IMethod ExtensionMarkerMethod = marker;
149+
/// <summary>
150+
/// Metadata-only method with a signature as declared in C# within the extension declaration.
151+
/// This could also be an accessor of an extension property.
152+
/// </summary>
153+
public readonly IMethod ExtensionMember = extension;
154+
/// <summary>
155+
/// The actual implementation method in the outer class. The signature is a concatenation
156+
/// of the extension marker and the extension member's signatures.
157+
/// </summary>
158+
public readonly IMethod ImplementationMethod = implementation;
159+
160+
/// <summary>
161+
/// This is the enclosing static class.
162+
/// </summary>
163+
public ITypeDefinition ExtensionContainer => ImplementationMethod.DeclaringTypeDefinition!;
164+
165+
/// <summary>
166+
/// This is the compiler-generated class containing the extension members. Has type parameters
167+
/// from the extension declaration with minimal constraints.
168+
/// </summary>
169+
public ITypeDefinition ExtensionGroupingType => ExtensionMember.DeclaringTypeDefinition!;
170+
171+
/// <summary>
172+
/// This class holds the type parameters for the extension declaration with full fidelity of C# constraints.
173+
/// </summary>
174+
public ITypeDefinition ExtensionMarkerType => ExtensionMarkerMethod.DeclaringTypeDefinition!;
175+
}
176+
}

ICSharpCode.Decompiler/TypeSystem/ITypeDefinition.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace ICSharpCode.Decompiler.TypeSystem
2828
/// </summary>
2929
public interface ITypeDefinition : ITypeDefinitionOrUnknown, IType, IEntity
3030
{
31+
ExtensionInfo? ExtensionInfo { get; }
3132
IReadOnlyList<ITypeDefinition> NestedTypes { get; }
3233
IReadOnlyList<IMember> Members { get; }
3334

ICSharpCode.Decompiler/TypeSystem/Implementation/MetadataTypeDefinition.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ sealed class MetadataTypeDefinition : ITypeDefinition
4141
// eagerly loaded:
4242
readonly FullTypeName fullTypeName;
4343
readonly TypeAttributes attributes;
44+
4445
public TypeKind Kind { get; }
4546
public bool IsByRefLike { get; }
4647
public bool IsReadOnly { get; }
@@ -143,6 +144,22 @@ public override string ToString()
143144
return $"{MetadataTokens.GetToken(handle):X8} {fullTypeName}";
144145
}
145146

147+
private ExtensionInfo extensionInfo;
148+
149+
public ExtensionInfo ExtensionInfo {
150+
get {
151+
if (!HasExtensions)
152+
return null;
153+
var extensionInfo = LazyInit.VolatileRead(ref this.extensionInfo);
154+
if (extensionInfo != null)
155+
return extensionInfo;
156+
extensionInfo = new ExtensionInfo(module, this);
157+
if ((module.TypeSystemOptions & TypeSystemOptions.Uncached) != 0)
158+
return extensionInfo;
159+
return LazyInit.GetOrSet(ref this.extensionInfo, extensionInfo);
160+
}
161+
}
162+
146163
ITypeDefinition[] nestedTypes;
147164

148165
public IReadOnlyList<ITypeDefinition> NestedTypes {

ICSharpCode.Decompiler/TypeSystem/Implementation/MinimalCorlib.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ public CorlibTypeDefinition(MinimalCorlib corlib, KnownTypeCode typeCode)
174174
IType IEntity.DeclaringType => null;
175175

176176
bool ITypeDefinition.HasExtensions => false;
177+
ExtensionInfo ITypeDefinition.ExtensionInfo => null;
177178
bool ITypeDefinition.IsReadOnly => false;
178179

179180
TypeKind IType.Kind => typeKind;

0 commit comments

Comments
 (0)