-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathRcwReflectionFallbackGenerator.cs
339 lines (289 loc) · 15.3 KB
/
RcwReflectionFallbackGenerator.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
using WinRT.SourceGenerator;
#nullable enable
namespace Generator;
[Generator]
public sealed class RcwReflectionFallbackGenerator : IIncrementalGenerator
{
/// <inheritdoc/>
public void Initialize(IncrementalGeneratorInitializationContext context)
{
// Gather all PE references from the current compilation
IncrementalValuesProvider<EquatablePortableExecutableReference> executableReferences =
context.CompilationProvider
.SelectMany(static (compilation, token) =>
{
var executableReferences = ImmutableArray.CreateBuilder<EquatablePortableExecutableReference>();
foreach (MetadataReference metadataReference in compilation.References)
{
// We are only interested in PE references (not project references)
if (metadataReference is not PortableExecutableReference executableReference)
{
continue;
}
executableReferences.Add(new EquatablePortableExecutableReference(executableReference, compilation));
}
return executableReferences.ToImmutable();
});
// Get whether the current project is an .exe
IncrementalValueProvider<bool> isOutputTypeExe = context.CompilationProvider.Select(static (compilation, token) =>
{
return compilation.Options.OutputKind is OutputKind.ConsoleApplication or OutputKind.WindowsApplication or OutputKind.WindowsRuntimeApplication;
});
// Get whether the generator is explicitly set as opt-in
IncrementalValueProvider<bool> isGeneratorForceOptIn = context.AnalyzerConfigOptionsProvider.Select(static (options, token) =>
{
return options.GetCsWinRTRcwFactoryFallbackGeneratorForceOptIn();
});
// Get whether the generator is explicitly set as opt-out
IncrementalValueProvider<bool> isGeneratorForceOptOut = context.AnalyzerConfigOptionsProvider.Select(static (options, token) =>
{
return options.GetCsWinRTRcwFactoryFallbackGeneratorForceOptOut();
});
IncrementalValueProvider<bool> csWinRTAotWarningEnabled = context.AnalyzerConfigOptionsProvider.Select(static (options, token) =>
{
return options.GetCsWinRTAotWarningLevel() >= 1;
});
// Get whether the generator should actually run or not
IncrementalValueProvider<bool> isGeneratorEnabled =
isOutputTypeExe
.Combine(isGeneratorForceOptIn)
.Combine(isGeneratorForceOptOut)
.Select(static (flags, token) => (flags.Left.Left || flags.Left.Right) && !flags.Right);
// Bypass all items if the flag is not set
IncrementalValuesProvider<(EquatablePortableExecutableReference Value, bool)> enabledExecutableReferences =
executableReferences
.Combine(isGeneratorEnabled)
.Where(static item => item.Right);
// Get all the names of the projected types to root
IncrementalValuesProvider<EquatableArray<RcwReflectionFallbackType>> executableTypeNames = enabledExecutableReferences.Select(static (executableReference, token) =>
{
Compilation compilation = executableReference.Value.GetCompilationUnsafe();
// We only care about resolved assembly symbols (this should always be the case anyway)
if (compilation.GetAssemblyOrModuleSymbol(executableReference.Value.Reference) is not IAssemblySymbol assemblySymbol)
{
return EquatableArray<RcwReflectionFallbackType>.FromImmutableArray(ImmutableArray<RcwReflectionFallbackType>.Empty);
}
// If the assembly is not an old projections assembly, we have nothing to do
if (!GeneratorHelper.IsOldProjectionAssembly(assemblySymbol))
{
return EquatableArray<RcwReflectionFallbackType>.FromImmutableArray(ImmutableArray<RcwReflectionFallbackType>.Empty);
}
token.ThrowIfCancellationRequested();
ITypeSymbol attributeSymbol = compilation.GetTypeByMetadataName("System.Attribute")!;
ITypeSymbol windowsRuntimeTypeAttributeSymbol = compilation.GetTypeByMetadataName("WinRT.WindowsRuntimeTypeAttribute")!;
ImmutableArray<RcwReflectionFallbackType>.Builder executableTypeNames = ImmutableArray.CreateBuilder<RcwReflectionFallbackType>();
// Process all type symbols in the current assembly
foreach (INamedTypeSymbol typeSymbol in VisitNamedTypeSymbolsExceptABI(assemblySymbol))
{
token.ThrowIfCancellationRequested();
// We only care about public or internal classes
if (typeSymbol is not { TypeKind: TypeKind.Class, DeclaredAccessibility: Accessibility.Public or Accessibility.Internal })
{
continue;
}
// Ignore static types (we only care about actual RCW types we can instantiate)
if (typeSymbol.IsStatic)
{
continue;
}
// Ignore attribute types (they're never instantiated like normal RCWs)
if (IsDerivedFromType(typeSymbol, attributeSymbol))
{
continue;
}
// If the type is not a generated projected type, do nothing
if (!GeneratorHelper.HasAttributeWithType(typeSymbol, windowsRuntimeTypeAttributeSymbol))
{
continue;
}
// Double check we can in fact access this type (or we can't reference it)
if (!compilation.IsSymbolAccessibleWithin(typeSymbol, compilation.Assembly))
{
continue;
}
var typeName = typeSymbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat);
// These types are in the existing WinUI projection, but have been moved to the Windows SDK projection.
// So if we see those, we want to ignore them.
if (typeName == "global::Windows.UI.Text.ContentLinkInfo" ||
typeName == "global::Windows.UI.Text.RichEditTextDocument" ||
typeName == "global::Windows.UI.Text.RichEditTextRange")
{
continue;
}
// Check if we are able to resolve the type using GetTypeByMetadataName. If not,
// it indicates there are multiple definitions of this type in the references
// and us emitting a dependency on this type would cause compiler error. So emit
// a warning instead.
bool hasMultipleDefinitions = compilation.GetTypeByMetadataName(GeneratorHelper.TrimGlobalFromTypeName(typeName)) is null;
executableTypeNames.Add(new RcwReflectionFallbackType(typeName, hasMultipleDefinitions));
}
token.ThrowIfCancellationRequested();
return EquatableArray<RcwReflectionFallbackType>.FromImmutableArray(executableTypeNames.ToImmutable());
});
// Combine all names into a single sequence
IncrementalValueProvider<(ImmutableArray<RcwReflectionFallbackType>, bool)> projectedTypeNamesAndAotWarningEnabled =
executableTypeNames
.Where(static names => !names.IsEmpty)
.SelectMany(static (executableTypeNames, token) => executableTypeNames.AsImmutableArray())
.Collect()
.Combine(csWinRTAotWarningEnabled);
// Generate the [DynamicDependency] attributes
context.RegisterImplementationSourceOutput(projectedTypeNamesAndAotWarningEnabled, static (SourceProductionContext context, (ImmutableArray<RcwReflectionFallbackType> projectedTypeNames, bool csWinRTAotWarningEnabled) value) =>
{
if (value.projectedTypeNames.IsEmpty)
{
return;
}
StringBuilder builder = new();
builder.AppendLine("""
// <auto-generated/>
#pragma warning disable
namespace WinRT
{
using global::System.Runtime.CompilerServices;
using global::System.Diagnostics.CodeAnalysis;
/// <summary>
/// Roots RCW types for assemblies referencing old projections.
/// It is recommended to update those, to get binary size savings.
/// </summary>
internal static class RcwFallbackInitializer
{
/// <summary>
/// Roots all dependent RCW types.
/// </summary>
[ModuleInitializer]
""");
bool emittedDynamicDependency = false;
foreach (RcwReflectionFallbackType projectedTypeName in value.projectedTypeNames)
{
// If there are multiple definitions of the type, emitting a dependency would result in a compiler error.
// So instead, emit a diagnostic for it.
if (projectedTypeName.HasMultipleDefinitions)
{
var diagnosticDescriptor = value.csWinRTAotWarningEnabled ?
WinRTRules.ClassNotAotCompatibleOldProjectionMultipleInstancesWarning : WinRTRules.ClassNotAotCompatibleOldProjectionMultipleInstancesInfo;
// We have no location to emit the diagnostic as this is just a reference we detect.
context.ReportDiagnostic(Diagnostic.Create(diagnosticDescriptor, null, GeneratorHelper.TrimGlobalFromTypeName(projectedTypeName.TypeName)));
}
else
{
emittedDynamicDependency = true;
builder.Append(" [DynamicDependency(DynamicallyAccessedMemberTypes.NonPublicConstructors, typeof(");
builder.Append(projectedTypeName.TypeName);
builder.AppendLine("))]");
}
}
builder.Append("""
public static void InitializeRcwFallback()
{
}
}
}
""");
if (emittedDynamicDependency)
{
context.AddSource("RcwFallbackInitializer.g.cs", builder.ToString());
}
});
}
/// <summary>
/// Visits all named type symbols in a given assembly, except for ABI types.
/// </summary>
/// <param name="assemblySymbol">The assembly to inspect.</param>
/// <returns>All named type symbols in <paramref name="assemblySymbol"/>, except for ABI types.</returns>
private static IEnumerable<INamedTypeSymbol> VisitNamedTypeSymbolsExceptABI(IAssemblySymbol assemblySymbol)
{
static IEnumerable<INamedTypeSymbol> Visit(INamespaceOrTypeSymbol symbol)
{
foreach (ISymbol memberSymbol in symbol.GetMembers())
{
// Visit the current symbol if it's a type symbol
if (memberSymbol is INamedTypeSymbol typeSymbol)
{
yield return typeSymbol;
}
else if (memberSymbol is INamespaceSymbol { Name: not ("ABI" or "WinRT") } namespaceSymbol)
{
// If the symbol is a namespace, also recurse (ignore the ABI namespaces)
foreach (INamedTypeSymbol nestedTypeSymbol in Visit(namespaceSymbol))
{
yield return nestedTypeSymbol;
}
}
}
}
return Visit(assemblySymbol.GlobalNamespace);
}
/// <summary>
/// Checks whether a given type is derived from a specified type.
/// </summary>
/// <param name="typeSymbol">The input <see cref="ITypeSymbol"/> instance to check.</param>
/// <param name="baseTypeSymbol">The base type to look for.</param>
/// <returns>Whether <paramref name="typeSymbol"/> derives from <paramref name="baseTypeSymbol"/>.</returns>
private static bool IsDerivedFromType(ITypeSymbol typeSymbol, ITypeSymbol baseTypeSymbol)
{
for (ITypeSymbol? currentSymbol = typeSymbol.BaseType;
currentSymbol is { SpecialType: not SpecialType.System_Object };
currentSymbol = currentSymbol.BaseType)
{
if (SymbolEqualityComparer.Default.Equals(currentSymbol, baseTypeSymbol))
{
return true;
}
}
return false;
}
/// <summary>
/// An equatable <see cref="PortableExecutableReference"/> type that weakly references a <see cref="Microsoft.CodeAnalysis.Compilation"/> object.
/// </summary>
/// <param name="executableReference">The <see cref="PortableExecutableReference"/> object to wrap.</param>
/// <param name="compilation">The <see cref="Microsoft.CodeAnalysis.Compilation"/> instance where <paramref name="executableReference"/> comes from.</param>
public sealed class EquatablePortableExecutableReference(
PortableExecutableReference executableReference,
Compilation compilation) : IEquatable<EquatablePortableExecutableReference>
{
/// <summary>
/// A weak reference to the <see cref="Microsoft.CodeAnalysis.Compilation"/> object owning <see cref="Reference"/>.
/// </summary>
private readonly WeakReference<Compilation> Compilation = new(compilation);
/// <summary>
/// Gets the <see cref="PortableExecutableReference"/> object for this instance.
/// </summary>
public PortableExecutableReference Reference { get; } = executableReference;
/// <summary>
/// Gets the <see cref="Microsoft.CodeAnalysis.Compilation"/> object for <see cref="Reference"/>.
/// </summary>
/// <returns>The <see cref="Microsoft.CodeAnalysis.Compilation"/> object for <see cref="Reference"/>.</returns>
/// <exception cref="InvalidOperationException">Thrown if the <see cref="Microsoft.CodeAnalysis.Compilation"/> object has been collected.</exception>
/// <remarks>
/// This method should only be used from incremental steps immediately following a change in the metadata reference
/// being used, as that would guarantee that that <see cref="Microsoft.CodeAnalysis.Compilation"/> object would be alive.
/// </remarks>
public Compilation GetCompilationUnsafe()
{
if (Compilation.TryGetTarget(out Compilation? compilation))
{
return compilation;
}
throw new InvalidOperationException("No compilation object is available.");
}
/// <inheritdoc/>
public bool Equals(EquatablePortableExecutableReference other)
{
if (other is null)
{
return false;
}
return other.Reference.GetMetadataId() == Reference.GetMetadataId();
}
}
internal readonly record struct RcwReflectionFallbackType(string TypeName, bool HasMultipleDefinitions);
}