Skip to content

Commit 56ab9c0

Browse files
committed
Use GetProperty.Match
1 parent c700890 commit 56ab9c0

3 files changed

Lines changed: 38 additions & 89 deletions

File tree

ReflectionAnalyzers/Helpers/Reflection/Filters/Types.cs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ internal bool TryMostSpecific(ISymbol? x, ISymbol? y, Compilation compilation, [
115115
return false;
116116
}
117117

118-
return this.TryMostSpecific(x as IMethodSymbol, y as IMethodSymbol, compilation, out unique);
118+
return this.TryMostSpecific(Parameters(x), Parameters(y), compilation, out unique);
119119

120120
static bool ByNull(ISymbol? first, ISymbol? other, [NotNullWhen(true)] out ISymbol? result)
121121
{
@@ -129,6 +129,16 @@ static bool ByNull(ISymbol? first, ISymbol? other, [NotNullWhen(true)] out ISymb
129129
result = null;
130130
return false;
131131
}
132+
133+
static ImmutableArray<IParameterSymbol> Parameters(ISymbol symbol)
134+
{
135+
return symbol switch
136+
{
137+
IMethodSymbol method => method.Parameters,
138+
IPropertySymbol property => property.Parameters,
139+
_ => ImmutableArray<IParameterSymbol>.Empty,
140+
};
141+
}
132142
}
133143

134144
private static bool TryGetTypesArgument(InvocationExpressionSyntax invocation, IMethodSymbol getX, [NotNullWhen(true)] out ArgumentSyntax? argument)
@@ -138,11 +148,11 @@ private static bool TryGetTypesArgument(InvocationExpressionSyntax invocation, I
138148
invocation.TryFindArgument(parameter, out argument);
139149
}
140150

141-
private bool TryMostSpecific(IMethodSymbol? x, IMethodSymbol? y, Compilation compilation, [NotNullWhen(true)] out ISymbol? unique)
151+
private bool TryMostSpecific(ImmutableArray<IParameterSymbol> x, ImmutableArray<IParameterSymbol> y, Compilation compilation, [NotNullWhen(true)] out ISymbol? unique)
142152
{
143153
if (this.Argument is null ||
144-
x is null ||
145-
y is null)
154+
x.IsDefaultOrEmpty ||
155+
y.IsDefaultOrEmpty)
146156
{
147157
unique = null;
148158
return false;
@@ -154,8 +164,8 @@ x is null ||
154164
return true;
155165
}
156166

157-
if (this.Matches(x.Parameters, compilation) &&
158-
this.Matches(y.Parameters, compilation))
167+
if (this.Matches(x, compilation) &&
168+
this.Matches(y, compilation))
159169
{
160170
var sum = 0;
161171
for (var i = 0; i < this.Symbols.Length; i++)
@@ -169,50 +179,50 @@ x is null ||
169179
return false;
170180
}
171181

172-
unique = sum < 0 ? x : y;
182+
unique = sum < 0 ? x[0].ContainingSymbol : y[0].ContainingSymbol;
173183
return true;
174184
}
175185

176-
if (this.Matches(x.Parameters, compilation))
186+
if (this.Matches(x, compilation))
177187
{
178-
unique = x;
188+
unique = x[0].ContainingSymbol;
179189
return true;
180190
}
181191

182-
if (this.Matches(y.Parameters, compilation))
192+
if (this.Matches(y, compilation))
183193
{
184-
unique = y;
194+
unique = y[0].ContainingSymbol;
185195
return true;
186196
}
187197

188198
unique = null;
189199
return false;
190200

191-
static bool TryExact(ImmutableArray<ITypeSymbol> symbols, IMethodSymbol method, [NotNullWhen(true)] out ISymbol? result)
201+
static bool TryExact(ImmutableArray<ITypeSymbol> symbols, ImmutableArray<IParameterSymbol> parameters, [NotNullWhen(true)] out ISymbol? result)
192202
{
193-
if (method.Parameters.Length != symbols.Length)
203+
if (parameters.Length != symbols.Length)
194204
{
195205
result = null;
196206
return false;
197207
}
198208

199-
for (var i = 0; i < method.Parameters.Length; i++)
209+
for (var i = 0; i < parameters.Length; i++)
200210
{
201-
if (!TypeSymbolComparer.Equal(symbols[i], method.Parameters[i].Type))
211+
if (!TypeSymbolComparer.Equal(symbols[i], parameters[i].Type))
202212
{
203213
result = null;
204214
return false;
205215
}
206216
}
207217

208-
result = method;
218+
result = parameters[0].ContainingSymbol;
209219
return true;
210220
}
211221

212222
int ByIndex(int index, ImmutableArray<ITypeSymbol> symbols)
213223
{
214-
var xt = x!.Parameters[index].Type;
215-
var yt = y!.Parameters[index].Type;
224+
var xt = x[index].Type;
225+
var yt = y[index].Type;
216226
if (TypeSymbolComparer.Equal(xt, yt))
217227
{
218228
return 0;

ReflectionAnalyzers/Helpers/Reflection/GetX.cs

Lines changed: 0 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -29,75 +29,6 @@ internal static bool TryMatchGetNestedType(InvocationExpressionSyntax invocation
2929
return TryMatchGetX(invocation, KnownSymbol.Type.GetNestedType, semanticModel, cancellationToken, out member, out name, out flags);
3030
}
3131

32-
/// <summary>
33-
/// Check if <paramref name="invocation"/> is a call to Type.GetProperty.
34-
/// </summary>
35-
internal static bool TryMatchGetProperty(InvocationExpressionSyntax invocation, SemanticModel semanticModel, CancellationToken cancellationToken, out ReflectedMember member, out Name name, out Flags flags, out Types types)
36-
{
37-
if (invocation.TryGetTarget(KnownSymbol.Type.GetProperty, semanticModel, cancellationToken, out var getX))
38-
{
39-
if (ReflectedMember.TryGetType(invocation, semanticModel, cancellationToken, out var type, out var typeSource) &&
40-
IsKnownSignature(invocation, getX) &&
41-
Name.TryCreate(invocation, getX, semanticModel, cancellationToken, out name) &&
42-
Flags.TryCreate(invocation, getX, semanticModel, cancellationToken, out flags) &&
43-
Types.TryCreate(invocation, getX, semanticModel, cancellationToken, out types))
44-
{
45-
return ReflectedMember.TryCreate(getX, invocation, type, typeSource, name, flags.Effective, types, semanticModel.Compilation, out member);
46-
}
47-
48-
if (Flags.TryCreate(invocation, getX, semanticModel, cancellationToken, out flags) &&
49-
flags.AreInSufficient)
50-
{
51-
_ = Name.TryCreate(invocation, getX, semanticModel, cancellationToken, out name);
52-
_ = Types.TryCreate(invocation, getX, semanticModel, cancellationToken, out types);
53-
member = new ReflectedMember(type, typeSource, null, getX, invocation, FilterMatch.InSufficientFlags);
54-
return true;
55-
}
56-
}
57-
58-
member = default;
59-
flags = default;
60-
name = default;
61-
types = default;
62-
return false;
63-
}
64-
65-
/// <summary>
66-
/// Defensive check to only handle known cases. Don't know how the binder works.
67-
/// </summary>
68-
private static bool IsKnownSignature(InvocationExpressionSyntax invocation, IMethodSymbol getX)
69-
{
70-
foreach (var parameter in getX.Parameters)
71-
{
72-
if (!IsKnownArgument(parameter))
73-
{
74-
return false;
75-
}
76-
}
77-
78-
return true;
79-
bool IsKnownArgument(IParameterSymbol parameter)
80-
{
81-
if (parameter.Type == KnownSymbol.String ||
82-
parameter.Type == KnownSymbol.BindingFlags ||
83-
parameter.Name == "types")
84-
{
85-
return true;
86-
}
87-
88-
if (parameter.Type == KnownSymbol.Binder &&
89-
invocation.TryFindArgument(parameter, out var binderArg))
90-
{
91-
return binderArg.Expression?.IsKind(SyntaxKind.NullLiteralExpression) == true ||
92-
(binderArg.Expression is MemberAccessExpressionSyntax memberAccess &&
93-
memberAccess.Name.Identifier.ValueText == "DefaultBinder");
94-
}
95-
96-
return invocation.TryFindArgument(parameter, out var argument) &&
97-
argument.Expression?.IsKind(SyntaxKind.NullLiteralExpression) == true;
98-
}
99-
}
100-
10132
/// <summary>
10233
/// Handles GetField, GetEvent, GetMember, GetMethod...
10334
/// </summary>

ReflectionAnalyzers/NodeAnalzers/GetXAnalyzer.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,15 @@ private static bool TryGetX(SyntaxNodeAnalysisContext context, out ReflectedMemb
274274
return true;
275275
}
276276

277+
if (GetProperty.Match(candidate, context.SemanticModel, context.CancellationToken) is { } getProperty)
278+
{
279+
member = getProperty.Member;
280+
name = getProperty.Name;
281+
flags = getProperty.Flags;
282+
types = getProperty.Types;
283+
return true;
284+
}
285+
277286
if (GetMethod.Match(candidate, context.SemanticModel, context.CancellationToken) is { } getMethod)
278287
{
279288
member = getMethod.Member;
@@ -285,8 +294,7 @@ private static bool TryGetX(SyntaxNodeAnalysisContext context, out ReflectedMemb
285294

286295
types = default;
287296
return GetX.TryMatchGetEvent(candidate, context.SemanticModel, context.CancellationToken, out member, out name, out flags) ||
288-
GetX.TryMatchGetNestedType(candidate, context.SemanticModel, context.CancellationToken, out member, out name, out flags) ||
289-
GetX.TryMatchGetProperty(candidate, context.SemanticModel, context.CancellationToken, out member, out name, out flags, out types);
297+
GetX.TryMatchGetNestedType(candidate, context.SemanticModel, context.CancellationToken, out member, out name, out flags);
290298
}
291299

292300
member = default;

0 commit comments

Comments
 (0)