/
ISymbolExtensions.cs
113 lines (97 loc) · 4.01 KB
/
ISymbolExtensions.cs
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
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace Microsoft.CodeAnalysis
{
public static partial class ISymbolExtensions
{
/// <summary>
/// Returns the constructed form of the ReducedFrom property,
/// including the type arguments that were either inferred during reduction or supplied at the call site.
/// </summary>
public static IMethodSymbol GetConstructedReducedFrom(this IMethodSymbol method)
{
if (method.MethodKind != MethodKind.ReducedExtension)
{
// not a reduced extension method
return null;
}
var reducedFrom = method.ReducedFrom;
if (!reducedFrom.IsGenericMethod)
{
// not generic, no inferences were made
return reducedFrom;
}
var typeArgs = new ITypeSymbol[reducedFrom.TypeParameters.Length];
// first seed with any type arguments from reduced method
for (int i = 0, n = method.TypeParameters.Length; i < n; i++)
{
var arg = method.TypeArguments[i];
// make sure we don't construct with type parameters originating from reduced symbol.
if (arg.Equals(method.TypeParameters[i]))
{
arg = method.TypeParameters[i].ReducedFrom;
}
typeArgs[method.TypeParameters[i].ReducedFrom.Ordinal] = arg;
}
// add any inferences
for (int i = 0, n = reducedFrom.TypeParameters.Length; i < n; i++)
{
var inferredType = method.GetTypeInferredDuringReduction(reducedFrom.TypeParameters[i]);
if (inferredType != null)
{
typeArgs[i] = inferredType;
}
}
return reducedFrom.Construct(typeArgs);
}
/// <summary>
/// Returns true if a given field is a nondefault tuple element
/// </summary>
internal static bool IsDefaultTupleElement(this IFieldSymbol field)
{
return (object)field == field.CorrespondingTupleField;
}
/// <summary>
/// Returns true if a given field is a tuple element
/// </summary>
internal static bool IsTupleElement(this IFieldSymbol field)
{
return (object)field.CorrespondingTupleField != null;
}
/// <summary>
/// Return the name of the field if the field is an explicitly named tuple element.
/// Otherwise returns null.
/// </summary>
/// <remarks>
/// Note that it is possible for an element to be both "Default" and to have a user provided name.
/// That could happen if the provided name matches the default name such as "Item10"
/// </remarks>
internal static string ProvidedTupleElementNameOrNull(this IFieldSymbol field)
{
return field.IsTupleElement() && !field.IsImplicitlyDeclared ? field.Name : null;
}
internal static INamespaceSymbol GetNestedNamespace(this INamespaceSymbol container, string name)
{
foreach (var sym in container.GetMembers(name))
{
if (sym.Kind == SymbolKind.Namespace)
{
return (INamespaceSymbol)sym;
}
}
return null;
}
internal static bool IsNetModule(this IAssemblySymbol assembly) =>
assembly is ISourceAssemblySymbol sourceAssembly && sourceAssembly.Compilation.Options.OutputKind.IsNetModule();
internal static bool IsInSource(this ISymbol symbol)
{
foreach (var location in symbol.Locations)
{
if (location.IsInSource)
{
return true;
}
}
return false;
}
}
}