-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Compilation_UsedAssemblies.cs
261 lines (222 loc) · 11.5 KB
/
Compilation_UsedAssemblies.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Threading;
using Microsoft.CodeAnalysis.CSharp.Symbols;
using Microsoft.CodeAnalysis.CSharp.Symbols.Retargeting;
using Microsoft.CodeAnalysis.PooledObjects;
using Roslyn.Utilities;
#nullable enable
namespace Microsoft.CodeAnalysis.CSharp
{
public partial class CSharpCompilation
{
private ConcurrentSet<AssemblySymbol>? _lazyUsedAssemblyReferences;
private bool _usedAssemblyReferencesFrozen;
public override ImmutableArray<MetadataReference> GetUsedAssemblyReferences(CancellationToken cancellationToken = default)
{
ConcurrentSet<AssemblySymbol>? usedAssemblies = GetCompleteSetOfUsedAssemblies(cancellationToken);
if (usedAssemblies is null)
{
return ImmutableArray<MetadataReference>.Empty;
}
var setOfReferences = new HashSet<MetadataReference>(ReferenceEqualityComparer.Instance);
ImmutableDictionary<MetadataReference, ImmutableArray<MetadataReference>> mergedAssemblyReferencesMap = GetBoundReferenceManager().MergedAssemblyReferencesMap;
foreach (var reference in References)
{
if (reference.Properties.Kind == MetadataImageKind.Assembly)
{
Symbol? symbol = GetBoundReferenceManager().GetReferencedAssemblySymbol(reference);
if (symbol is object && usedAssemblies.Contains((AssemblySymbol)symbol) &&
setOfReferences.Add(reference) &&
mergedAssemblyReferencesMap.TryGetValue(reference, out ImmutableArray<MetadataReference> merged))
{
// Include all "merged" references as well because they might "define" used extern aliases.
setOfReferences.AddAll(merged);
}
}
}
// Use stable ordering for the result, matching the order in References.
var builder = ArrayBuilder<MetadataReference>.GetInstance(setOfReferences.Count);
foreach (var reference in References)
{
if (setOfReferences.Contains(reference))
{
builder.Add(reference);
}
}
return builder.ToImmutableAndFree();
}
private ConcurrentSet<AssemblySymbol>? GetCompleteSetOfUsedAssemblies(CancellationToken cancellationToken)
{
if (!_usedAssemblyReferencesFrozen && !Volatile.Read(ref _usedAssemblyReferencesFrozen))
{
var diagnostics = new BindingDiagnosticBag(DiagnosticBag.GetInstance(), new ConcurrentSet<AssemblySymbol>());
RoslynDebug.Assert(diagnostics.DiagnosticBag is object);
GetDiagnosticsWithoutFiltering(CompilationStage.Declare, includeEarlierStages: true, diagnostics, cancellationToken);
bool seenErrors = diagnostics.HasAnyErrors();
if (!seenErrors)
{
diagnostics.DiagnosticBag.Clear();
GetDiagnosticsForAllMethodBodies(diagnostics, doLowering: true, cancellationToken);
seenErrors = diagnostics.HasAnyErrors();
if (!seenErrors)
{
AddUsedAssemblies(diagnostics.DependenciesBag);
}
}
completeTheSetOfUsedAssemblies(seenErrors, cancellationToken);
diagnostics.DiagnosticBag.Free();
}
return _lazyUsedAssemblyReferences;
void addUsedAssembly(AssemblySymbol dependency, ArrayBuilder<AssemblySymbol> stack)
{
if (AddUsedAssembly(dependency))
{
stack.Push(dependency);
}
}
void addReferencedAssemblies(AssemblySymbol assembly, bool includeMainModule, ArrayBuilder<AssemblySymbol> stack)
{
for (int i = (includeMainModule ? 0 : 1); i < assembly.Modules.Length; i++)
{
foreach (var dependency in assembly.Modules[i].ReferencedAssemblySymbols)
{
addUsedAssembly(dependency, stack);
}
}
}
void completeTheSetOfUsedAssemblies(bool seenErrors, CancellationToken cancellationToken)
{
if (_usedAssemblyReferencesFrozen || Volatile.Read(ref _usedAssemblyReferencesFrozen))
{
return;
}
if (seenErrors)
{
// Add all referenced assemblies
foreach (var assembly in SourceModule.ReferencedAssemblySymbols)
{
AddUsedAssembly(assembly);
}
}
else
{
// Assume that all assemblies used by the added modules are also used
for (int i = 1; i < SourceAssembly.Modules.Length; i++)
{
foreach (var dependency in SourceAssembly.Modules[i].ReferencedAssemblySymbols)
{
AddUsedAssembly(dependency);
}
}
if (_usedAssemblyReferencesFrozen || Volatile.Read(ref _usedAssemblyReferencesFrozen))
{
return;
}
// Assume that all assemblies used by the used assemblies are also used
// This, for example, takes care of including facade assemblies that forward types around.
if (_lazyUsedAssemblyReferences is object)
{
lock (_lazyUsedAssemblyReferences)
{
if (_usedAssemblyReferencesFrozen || Volatile.Read(ref _usedAssemblyReferencesFrozen))
{
return;
}
var stack = ArrayBuilder<AssemblySymbol>.GetInstance(_lazyUsedAssemblyReferences.Count);
stack.AddRange(_lazyUsedAssemblyReferences);
while (stack.Count != 0)
{
AssemblySymbol current = stack.Pop();
ConcurrentSet<AssemblySymbol>? usedAssemblies;
switch (current)
{
case SourceAssemblySymbol sourceAssembly:
// The set of assemblies used by the referenced compilation feels like
// a reasonable approximation to the set of assembly references that would
// be emitted into the resulting binary for that compilation. An alternative
// would be to attempt to emit and get the exact set of emitted references
// in case of success. This might be too slow though.
usedAssemblies = sourceAssembly.DeclaringCompilation.GetCompleteSetOfUsedAssemblies(cancellationToken);
if (usedAssemblies is object)
{
foreach (AssemblySymbol dependency in usedAssemblies)
{
Debug.Assert(!dependency.IsLinked);
addUsedAssembly(dependency, stack);
}
}
break;
case RetargetingAssemblySymbol retargetingAssembly:
usedAssemblies = retargetingAssembly.UnderlyingAssembly.DeclaringCompilation.GetCompleteSetOfUsedAssemblies(cancellationToken);
if (usedAssemblies is object)
{
foreach (AssemblySymbol underlyingDependency in retargetingAssembly.UnderlyingAssembly.SourceModule.ReferencedAssemblySymbols)
{
if (!underlyingDependency.IsLinked && usedAssemblies.Contains(underlyingDependency))
{
AssemblySymbol dependency;
if (!((RetargetingModuleSymbol)retargetingAssembly.Modules[0]).RetargetingDefinitions(underlyingDependency, out dependency))
{
Debug.Assert(retargetingAssembly.Modules[0].ReferencedAssemblySymbols.Contains(underlyingDependency));
dependency = underlyingDependency;
}
addUsedAssembly(dependency, stack);
}
}
}
addReferencedAssemblies(retargetingAssembly, includeMainModule: false, stack);
break;
default:
addReferencedAssemblies(current, includeMainModule: true, stack);
break;
}
}
stack.Free();
}
}
if (SourceAssembly.CorLibrary is object)
{
// Add core library
AddUsedAssembly(SourceAssembly.CorLibrary);
}
}
_usedAssemblyReferencesFrozen = true;
}
}
internal void AddUsedAssemblies(ICollection<AssemblySymbol>? assemblies)
{
if (!assemblies.IsNullOrEmpty())
{
foreach (var candidate in assemblies)
{
AddUsedAssembly(candidate);
}
}
}
internal bool AddUsedAssembly(AssemblySymbol? assembly)
{
if (assembly is null || assembly == SourceAssembly || assembly.IsMissing)
{
return false;
}
if (_lazyUsedAssemblyReferences is null)
{
Interlocked.CompareExchange(ref _lazyUsedAssemblyReferences, new ConcurrentSet<AssemblySymbol>(), null);
}
#if DEBUG
bool wasFrozen = _usedAssemblyReferencesFrozen;
#endif
bool added = _lazyUsedAssemblyReferences.Add(assembly);
#if DEBUG
Debug.Assert(!added || !wasFrozen);
#endif
return added;
}
}
}