-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethod.cs
More file actions
164 lines (133 loc) · 4.66 KB
/
Method.cs
File metadata and controls
164 lines (133 loc) · 4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
using Microsoft.CodeAnalysis;
using Mockolate.SourceGenerators.Internals;
namespace Mockolate.SourceGenerators.Entities;
internal record Method
{
public Method(IMethodSymbol methodSymbol, List<Method>? alreadyDefinedMethods)
{
Accessibility = methodSymbol.DeclaredAccessibility;
UseOverride = methodSymbol.IsVirtual || methodSymbol.IsAbstract;
IsAbstract = methodSymbol.IsAbstract;
ReturnType = methodSymbol.ReturnsVoid ? Type.Void : new Type(methodSymbol.ReturnType);
Name = methodSymbol.Name;
ContainingType = methodSymbol.ContainingType.ToDisplayString();
Parameters = new EquatableArray<MethodParameter>(
methodSymbol.Parameters.Select(x => new MethodParameter(x)).ToArray());
if (methodSymbol.IsGenericMethod)
{
GenericParameters = new EquatableArray<GenericParameter>(methodSymbol.TypeArguments
.Select(x => new GenericParameter((ITypeParameterSymbol)x)).ToArray());
Name += $"<{string.Join(", ", GenericParameters.Value.Select(x => x.Name))}>";
}
Attributes = methodSymbol.GetAttributes().ToAttributeArray();
if (alreadyDefinedMethods is not null)
{
if (alreadyDefinedMethods.Any(m =>
m.Name == Name &&
m.Parameters.Count == Parameters.Count &&
m.Parameters.SequenceEqual(Parameters)))
{
ExplicitImplementation = ContainingType;
}
alreadyDefinedMethods.Add(this);
}
}
public static IEqualityComparer<Method> EqualityComparer { get; } = new MethodEqualityComparer();
public static IEqualityComparer<Method> ContainingTypeIndependentEqualityComparer { get; } =
new ContainingTypeIndependentMethodEqualityComparer();
public EquatableArray<GenericParameter>? GenericParameters { get; }
public bool UseOverride { get; }
public bool IsAbstract { get; }
public Accessibility Accessibility { get; }
public Type ReturnType { get; }
public string Name { get; }
public string ContainingType { get; }
public EquatableArray<MethodParameter> Parameters { get; }
public string? ExplicitImplementation { get; }
public EquatableArray<Attribute>? Attributes { get; }
internal string GetUniqueNameString()
{
if (GenericParameters != null)
{
string name = Name.Substring(0, Name.IndexOf('<'));
string parameters = string.Join(", ",
GenericParameters.Value.Select(genericParameter => $"{{typeof({genericParameter.Name})}}"));
return $"$\"{ContainingType}.{name}<{parameters}>\"";
}
return $"\"{ContainingType}.{Name}\"";
}
public bool IsToString()
=> Name == "ToString" && Parameters.Count == 0;
public bool IsGetHashCode()
=> Name == "GetHashCode" && Parameters.Count == 0;
public bool IsEquals()
{
return Name == "Equals" && Parameters.Count == 1 && IsObjectOrNullableObject(Parameters.Single());
static bool IsObjectOrNullableObject(MethodParameter parameter)
{
return parameter.Type.Namespace == "System" &&
(parameter.Type.Fullname == "object" || parameter.Type.Fullname == "object?");
}
}
private sealed class MethodEqualityComparer : IEqualityComparer<Method>
{
public bool Equals(Method? x, Method? y)
=> (x is null && y is null) ||
(x is not null && y is not null &&
x.Name.Equals(y.Name) && x.ContainingType.Equals(y.ContainingType) &&
x.Parameters.Count == y.Parameters.Count &&
x.Parameters.SequenceEqual(y.Parameters));
public int GetHashCode(Method obj) => obj.Name.GetHashCode();
}
private sealed class ContainingTypeIndependentMethodEqualityComparer : IEqualityComparer<Method>
{
public bool Equals(Method? x, Method? y)
{
if (x is null && y is null)
{
return true;
}
if (x is null || y is null)
{
return false;
}
if (!x.Name.Equals(y.Name) || x.Parameters.Count != y.Parameters.Count)
{
return false;
}
// Compare parameters ignoring nullability annotations
MethodParameter[]? xParams = x.Parameters.AsArray();
MethodParameter[]? yParams = y.Parameters.AsArray();
if (xParams is null || yParams is null)
{
return xParams is null && yParams is null;
}
for (int i = 0; i < xParams.Length; i++)
{
MethodParameter xParam = xParams[i];
MethodParameter yParam = yParams[i];
if (xParam.RefKind != yParam.RefKind)
{
return false;
}
// Normalize type names by removing nullable annotation
string xTypeName = xParam.Type.Fullname;
string yTypeName = yParam.Type.Fullname;
if (xTypeName.EndsWith("?"))
{
xTypeName = xTypeName.Substring(0, xTypeName.Length - 1);
}
if (yTypeName.EndsWith("?"))
{
yTypeName = yTypeName.Substring(0, yTypeName.Length - 1);
}
if (!xTypeName.Equals(yTypeName))
{
return false;
}
}
return true;
}
public int GetHashCode(Method obj) => obj.Name.GetHashCode();
}
}