/
SymbolExtensions.cs
179 lines (142 loc) · 6.97 KB
/
SymbolExtensions.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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using System.Diagnostics.CodeAnalysis;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Riok.Mapperly.Symbols;
namespace Riok.Mapperly.Helpers;
internal static class SymbolExtensions
{
internal static bool HasAttribute(this ISymbol symbol, INamedTypeSymbol attributeSymbol) =>
symbol.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass, attributeSymbol));
internal static bool IsImmutable(this ISymbol symbol) =>
symbol is INamedTypeSymbol namedSymbol && (namedSymbol.IsReadOnly || namedSymbol.SpecialType == SpecialType.System_String);
internal static bool IsAccessible(this ISymbol symbol, bool allowProtected = false) =>
symbol.DeclaredAccessibility.HasFlag(Accessibility.Internal)
|| symbol.DeclaredAccessibility.HasFlag(Accessibility.Public)
|| (symbol.DeclaredAccessibility.HasFlag(Accessibility.Protected) && allowProtected);
internal static bool HasAccessibleParameterlessConstructor(this ITypeSymbol symbol, bool allowProtected = false) =>
symbol is INamedTypeSymbol { IsAbstract: false } namedTypeSymbol
&& namedTypeSymbol.Constructors.Any(c => c.Parameters.IsDefaultOrEmpty && c.IsAccessible(allowProtected));
internal static bool IsArrayType(this ITypeSymbol symbol) => symbol is IArrayTypeSymbol;
internal static bool IsEnum(this ITypeSymbol t) => TryGetEnumUnderlyingType(t, out _);
internal static bool TryGetEnumUnderlyingType(this ITypeSymbol t, [NotNullWhen(true)] out INamedTypeSymbol? enumType)
{
enumType = (t.NonNullable() as INamedTypeSymbol)?.EnumUnderlyingType;
return enumType != null;
}
internal static IEnumerable<IMethodSymbol> GetAllMethods(this ITypeSymbol symbol) => symbol.GetAllMembers().OfType<IMethodSymbol>();
internal static IEnumerable<IMethodSymbol> GetAllMethods(this ITypeSymbol symbol, string name) =>
symbol.GetAllMembers(name).OfType<IMethodSymbol>();
internal static IEnumerable<IPropertySymbol> GetAllProperties(this ITypeSymbol symbol, string name) =>
symbol.GetAllMembers(name).OfType<IPropertySymbol>();
internal static IEnumerable<ISymbol> GetAllMembers(this ITypeSymbol symbol)
{
var members = symbol.GetMembers();
if (symbol.TypeKind == TypeKind.Interface)
{
var interfaceProperties = symbol.AllInterfaces.SelectMany(i => i.GetAllMembers());
return members.Concat(interfaceProperties);
}
return symbol.BaseType == null ? members : members.Concat(symbol.BaseType.GetAllMembers());
}
internal static IEnumerable<IMappableMember> GetMappableMembers(
this ITypeSymbol symbol,
string name,
IEqualityComparer<string> comparer
)
{
return symbol.GetAllMembers().Where(x => !x.IsStatic && comparer.Equals(name, x.Name)).Select(MappableMember.Create).WhereNotNull();
}
internal static IEnumerable<IMappableMember> GetAccessibleMappableMembers(this ITypeSymbol symbol)
{
return symbol
.GetAllMembers()
.Where(x => !x.IsStatic && x.IsAccessible())
.DistinctBy(x => x.Name)
.Select(MappableMember.Create)
.WhereNotNull();
}
internal static IMethodSymbol? GetStaticGenericMethod(this INamedTypeSymbol namedType, string methodName)
{
return namedType.GetMembers(methodName).OfType<IMethodSymbol>().FirstOrDefault(m => m.IsStatic && m.IsGenericMethod);
}
internal static bool ImplementsGeneric(
this ITypeSymbol t,
INamedTypeSymbol genericInterfaceSymbol,
[NotNullWhen(true)] out INamedTypeSymbol? typedInterface
)
{
if (SymbolEqualityComparer.Default.Equals(t.OriginalDefinition, genericInterfaceSymbol))
{
typedInterface = (INamedTypeSymbol)t;
return true;
}
typedInterface = t.AllInterfaces.FirstOrDefault(
x => x.IsGenericType && SymbolEqualityComparer.Default.Equals(x.OriginalDefinition, genericInterfaceSymbol)
);
return typedInterface != null;
}
internal static bool ImplementsGeneric(
this ITypeSymbol t,
INamedTypeSymbol genericInterfaceSymbol,
string symbolName,
[NotNullWhen(true)] out INamedTypeSymbol? typedInterface,
out bool isExplicit
)
{
if (SymbolEqualityComparer.Default.Equals(t.OriginalDefinition, genericInterfaceSymbol))
{
typedInterface = (INamedTypeSymbol)t;
isExplicit = false;
return true;
}
typedInterface = t.AllInterfaces.FirstOrDefault(
x => x.IsGenericType && SymbolEqualityComparer.Default.Equals(x.OriginalDefinition, genericInterfaceSymbol)
);
if (typedInterface == null)
{
isExplicit = false;
return false;
}
if (t.IsAbstract)
{
isExplicit = false;
return true;
}
var interfaceSymbol = typedInterface.GetMembers(symbolName).First();
var symbolImplementaton = t.FindImplementationForInterfaceMember(interfaceSymbol);
// if null then the method is unimplemented
// symbol implements genericInterface but has not implemented the corresponding methods
// this can only occur in unit tests
if (symbolImplementaton == null)
throw new NotSupportedException("Symbol implementation cannot be null for objects implementing interface.");
// check if symbol is explicit
isExplicit = symbolImplementaton switch
{
IMethodSymbol methodSymbol => methodSymbol.ExplicitInterfaceImplementations.Any(),
IPropertySymbol propertySymbol => propertySymbol.ExplicitInterfaceImplementations.Any(),
_ => throw new NotImplementedException(),
};
return true;
}
internal static bool HasImplicitGenericImplementation(this ITypeSymbol symbol, INamedTypeSymbol inter, string methodName) =>
symbol.ImplementsGeneric(inter, methodName, out _, out var isExplicit) && !isExplicit;
internal static bool CanConsumeType(this ITypeParameterSymbol typeParameter, Compilation compilation, ITypeSymbol type)
{
if (typeParameter.HasConstructorConstraint && !type.HasAccessibleParameterlessConstructor())
return false;
if (typeParameter.HasNotNullConstraint && type.IsNullable())
return false;
if (typeParameter.HasValueTypeConstraint && !type.IsValueType)
return false;
if (typeParameter.HasReferenceTypeConstraint && !type.IsReferenceType)
return false;
foreach (var constraintType in typeParameter.ConstraintTypes)
{
if (!compilation.ClassifyConversion(type, constraintType.UpgradeNullable()).IsImplicit)
return false;
}
return true;
}
private static IEnumerable<ISymbol> GetAllMembers(this ITypeSymbol symbol, string name) =>
symbol.GetAllMembers().Where(x => name.Equals(x.Name));
}