/
DescriptorBuilder.cs
246 lines (214 loc) · 9.17 KB
/
DescriptorBuilder.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using Microsoft.CodeAnalysis;
using Riok.Mapperly.Abstractions;
using Riok.Mapperly.Abstractions.ReferenceHandling;
using Riok.Mapperly.Configuration;
using Riok.Mapperly.Descriptors.ExternalMappings;
using Riok.Mapperly.Descriptors.FormatProviders;
using Riok.Mapperly.Descriptors.MappingBodyBuilders;
using Riok.Mapperly.Descriptors.MappingBuilders;
using Riok.Mapperly.Descriptors.Mappings.UserMappings;
using Riok.Mapperly.Descriptors.ObjectFactories;
using Riok.Mapperly.Diagnostics;
using Riok.Mapperly.Helpers;
using Riok.Mapperly.Symbols;
namespace Riok.Mapperly.Descriptors;
public class DescriptorBuilder
{
private const string UnsafeAccessorName = "System.Runtime.CompilerServices.UnsafeAccessorAttribute";
private readonly MapperDescriptor _mapperDescriptor;
private readonly SymbolAccessor _symbolAccessor;
private readonly WellKnownTypes _types;
private readonly MappingCollection _mappings = new();
private readonly InlinedExpressionMappingCollection _inlineMappings = new();
private readonly MethodNameBuilder _methodNameBuilder = new();
private readonly MappingBodyBuilder _mappingBodyBuilder;
private readonly SimpleMappingBuilderContext _builderContext;
private readonly DiagnosticCollection _diagnostics;
private readonly UnsafeAccessorContext _unsafeAccessorContext;
public DescriptorBuilder(
CompilationContext compilationContext,
MapperDeclaration mapperDeclaration,
SymbolAccessor symbolAccessor,
MapperConfiguration defaultMapperConfiguration
)
{
_mapperDescriptor = new MapperDescriptor(mapperDeclaration, _methodNameBuilder);
_symbolAccessor = symbolAccessor;
_types = compilationContext.Types;
_mappingBodyBuilder = new MappingBodyBuilder(_mappings);
_unsafeAccessorContext = new UnsafeAccessorContext(_methodNameBuilder, symbolAccessor);
var attributeAccessor = new AttributeDataAccessor(symbolAccessor);
var configurationReader = new MapperConfigurationReader(
attributeAccessor,
_types,
mapperDeclaration.Symbol,
defaultMapperConfiguration
);
_diagnostics = new DiagnosticCollection(mapperDeclaration.Syntax.GetLocation());
_builderContext = new SimpleMappingBuilderContext(
compilationContext,
configurationReader,
_symbolAccessor,
new GenericTypeChecker(_symbolAccessor, _types),
attributeAccessor,
_unsafeAccessorContext,
_diagnostics,
new MappingBuilder(_mappings, mapperDeclaration),
new ExistingTargetMappingBuilder(_mappings),
_inlineMappings,
mapperDeclaration.Syntax.GetLocation()
);
}
public (MapperDescriptor descriptor, DiagnosticCollection diagnostics) Build(CancellationToken cancellationToken)
{
ConfigureMemberVisibility();
ReserveMethodNames();
ExtractUserMappings();
// ExtractObjectFactories needs to be called after ExtractUserMappings due to configuring mapperDescriptor.Static
var objectFactories = ExtractObjectFactories();
var formatProviders = ExtractFormatProviders();
EnqueueUserMappings(objectFactories, formatProviders);
ExtractExternalMappings();
_mappingBodyBuilder.BuildMappingBodies(cancellationToken);
AddUserMappingDiagnostics();
BuildMappingMethodNames();
BuildReferenceHandlingParameters();
AddMappingsToDescriptor();
AddAccessorsToDescriptor();
return (_mapperDescriptor, _diagnostics);
}
/// <summary>
/// If <see cref="MemberVisibility.Accessible"/> is not set and the roslyn version does not have UnsafeAccessors
/// then emit a diagnostic and update the <see cref="MemberVisibility"/> for <see cref="SymbolAccessor"/>.
/// </summary>
private void ConfigureMemberVisibility()
{
var includedMembers = _builderContext.Configuration.Mapper.IncludedMembers;
if (_types.TryGet(UnsafeAccessorName) != null)
{
_symbolAccessor.SetMemberVisibility(includedMembers);
return;
}
if (includedMembers.HasFlag(MemberVisibility.Accessible))
return;
_diagnostics.ReportDiagnostic(DiagnosticDescriptors.UnsafeAccessorNotAvailable);
_symbolAccessor.SetMemberVisibility(includedMembers | MemberVisibility.Accessible);
}
private void ReserveMethodNames()
{
foreach (var methodSymbol in _symbolAccessor.GetAllMembers(_mapperDescriptor.Symbol))
{
_methodNameBuilder.Reserve(methodSymbol.Name);
}
}
private void ExtractUserMappings()
{
_mapperDescriptor.Static = _mapperDescriptor.Symbol.IsStatic;
IMethodSymbol? firstNonStaticUserMapping = null;
foreach (var userMapping in UserMethodMappingExtractor.ExtractUserMappings(_builderContext, _mapperDescriptor.Symbol))
{
// if a user defined mapping method is static, all of them need to be static to avoid confusion for mapping method resolution
// however, user implemented mapping methods are allowed to be static in a non-static context.
// Therefore we are only interested in partial method definitions here.
if (userMapping.Method is { IsStatic: true, IsPartialDefinition: true })
{
_mapperDescriptor.Static = true;
}
else if (firstNonStaticUserMapping == null && !userMapping.Method.IsStatic)
{
firstNonStaticUserMapping = userMapping.Method;
}
AddUserMapping(userMapping, false, true);
}
if (_mapperDescriptor.Static && firstNonStaticUserMapping is not null)
{
_diagnostics.ReportDiagnostic(
DiagnosticDescriptors.MixingStaticPartialWithInstanceMethod,
firstNonStaticUserMapping,
_mapperDescriptor.Symbol.ToDisplayString()
);
}
}
private ObjectFactoryCollection ExtractObjectFactories()
{
return ObjectFactoryBuilder.ExtractObjectFactories(_builderContext, _mapperDescriptor.Symbol, _mapperDescriptor.Static);
}
private void EnqueueUserMappings(ObjectFactoryCollection objectFactories, FormatProviderCollection formatProviders)
{
foreach (var userMapping in _mappings.UserMappings)
{
var ctx = new MappingBuilderContext(
_builderContext,
objectFactories,
formatProviders,
userMapping.Method,
new TypeMappingKey(userMapping.SourceType, userMapping.TargetType)
);
_mappings.EnqueueToBuildBody(userMapping, ctx);
}
}
private void ExtractExternalMappings()
{
foreach (var externalMapping in ExternalMappingsExtractor.ExtractExternalMappings(_builderContext, _mapperDescriptor.Symbol))
{
AddUserMapping(externalMapping, true, false);
}
}
private FormatProviderCollection ExtractFormatProviders()
{
return FormatProviderBuilder.ExtractFormatProviders(_builderContext, _mapperDescriptor.Symbol, _mapperDescriptor.Static);
}
private void BuildMappingMethodNames()
{
foreach (var methodMapping in _mappings.MethodMappings)
{
methodMapping.SetMethodNameIfNeeded(_methodNameBuilder.Build);
}
}
private void BuildReferenceHandlingParameters()
{
if (!_builderContext.Configuration.Mapper.UseReferenceHandling)
return;
foreach (var methodMapping in _mappings.MethodMappings)
{
methodMapping.EnableReferenceHandling(_builderContext.Types.Get<IReferenceHandler>());
}
}
private void AddMappingsToDescriptor()
{
// add generated mappings to the mapper
_mapperDescriptor.AddMethodMappings(_mappings.MethodMappings);
}
private void AddAccessorsToDescriptor()
{
// add generated accessors to the mapper
_mapperDescriptor.AddUnsafeAccessors(_unsafeAccessorContext.UnsafeAccessors);
}
private void AddUserMapping(IUserMapping mapping, bool ignoreDuplicates, bool named)
{
var name = named ? mapping.Method.Name : null;
var result = _mappings.AddUserMapping(mapping, name);
if (!ignoreDuplicates && mapping.Default == true && result == MappingCollectionAddResult.NotAddedDuplicated)
{
_diagnostics.ReportDiagnostic(
DiagnosticDescriptors.MultipleDefaultUserMappings,
mapping.Method,
mapping.SourceType.ToDisplayString(),
mapping.TargetType.ToDisplayString()
);
}
_inlineMappings.AddUserMapping(mapping, name);
}
private void AddUserMappingDiagnostics()
{
foreach (var mapping in _mappings.UsedDuplicatedNonDefaultNonReferencedUserMappings)
{
_diagnostics.ReportDiagnostic(
DiagnosticDescriptors.MultipleUserMappingsWithoutDefault,
mapping.Method,
mapping.SourceType.ToDisplayString(),
mapping.TargetType.ToDisplayString()
);
}
}
}