Skip to content

Commit 1080617

Browse files
committed
Refactor syntax fixup handling and GeneratedType model
Removed IsPublicAccessibilityRequired from GeneratedType and moved accessibility and embedded attribute handling to SyntaxFixupType flags. Updated PolyfillsGenerator to use new flags for source adjustments, improving flexibility and maintainability of syntax fixup logic.
1 parent 1d7b263 commit 1080617

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

src/PolySharp.SourceGenerators/Models/GeneratedType.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,5 @@ namespace PolySharp.SourceGenerators.Models;
44
/// A model to hold information on a type to generate.
55
/// </summary>
66
/// <param name="FullyQualifiedMetadataName">The fully qualified type name of the type to generate.</param>
7-
/// <param name="IsPublicAccessibilityRequired">Whether to use public accessibility for the generated type.</param>
87
/// <param name="FixupType">The types of syntax fixups to apply.</param>
9-
internal sealed record GeneratedType(
10-
string FullyQualifiedMetadataName,
11-
bool IsPublicAccessibilityRequired,
12-
SyntaxFixupType FixupType);
8+
internal sealed record GeneratedType(string FullyQualifiedMetadataName, SyntaxFixupType FixupType);

src/PolySharp.SourceGenerators/Models/SyntaxFixupType.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,35 @@ internal enum SyntaxFixupType
1313
/// </summary>
1414
None = 0,
1515

16+
/// <summary>
17+
/// Uses the <see langword="public"/> accessibility modifier (instead of <see langword="internal"/>).
18+
/// </summary>
19+
UsePublicAccessibilityModifier = 1 << 0,
20+
21+
/// <summary>
22+
/// Remove all <c>[Embedded]</c> attributes.
23+
/// </summary>
24+
RemoveEmbeddedAttributes = 1 << 1,
25+
1626
/// <summary>
1727
/// Remove all <c>[MethodImpl]</c> attributes.
1828
/// </summary>
19-
RemoveMethodImplAttributes = 1 << 0,
29+
RemoveMethodImplAttributes = 1 << 2,
2030

2131
/// <summary>
2232
/// Remove all <c>[ExcludeFromCodeCoverage]</c> attributes.
2333
/// </summary>
24-
RemoveExcludeFromCodeCoverageAttributes = 1 << 1,
34+
RemoveExcludeFromCodeCoverageAttributes = 1 << 3,
2535

2636
/// <summary>
2737
/// Generates the <c>[UnmanagedCallersOnly]</c> type in the <c>InteropServices2</c> dummy namespace.
2838
/// </summary>
2939
/// <remarks>This is needed when methods annotated with the attribute have to be assigned to delegates, which Roslyn will otherwise block.</remarks>
30-
UseInteropServices2ForUnmanagedCallersOnlyAttribute = 1 << 2,
40+
UseInteropServices2ForUnmanagedCallersOnlyAttribute = 1 << 4,
3141

3242
/// <summary>
3343
/// Generates a global using for the <c>[InlineArray]</c> type in the dummy namespace.
3444
/// </summary>
3545
/// <remarks>This is always needed when the type is used, as the usage is entirely blocked in the normal namespace.</remarks>
36-
EmitGlobalUsingForInlineArrayAttribute = 1 << 3
46+
EmitGlobalUsingForInlineArrayAttribute = 1 << 5
3747
}

src/PolySharp.SourceGenerators/PolyfillsGenerator.Polyfills.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,11 @@ static SyntaxFixupType GetSyntaxFixupType(AvailableType availableType, Generatio
247247
// Combine the syntax type with any analyzer options
248248
SyntaxFixupType fixupType = info.AvailableType.FixupType | GetSyntaxFixupType(info.AvailableType, info.Options);
249249

250-
return new(info.AvailableType.FullyQualifiedMetadataName, info.Options.UsePublicAccessibilityForGeneratedTypes, fixupType);
250+
// Also combine additional flags from project settings
251+
fixupType |= info.Options.UsePublicAccessibilityForGeneratedTypes ? SyntaxFixupType.UsePublicAccessibilityModifier : SyntaxFixupType.None;
252+
fixupType |= !info.Options.UseEmbeddedAttributeForGeneratedTypes ? SyntaxFixupType.RemoveEmbeddedAttributes : SyntaxFixupType.None;
253+
254+
return new(info.AvailableType.FullyQualifiedMetadataName, fixupType);
251255
}
252256

253257
/// <summary>
@@ -285,8 +289,8 @@ private void EmitGeneratedType(SourceProductionContext context, GeneratedType ty
285289

286290
using Stream stream = typeof(PolyfillsGenerator).Assembly.GetManifestResourceStream(resourceName);
287291

288-
// If public accessibility has been requested or a syntax fixup is needed, we need to update the loaded source files
289-
if (type is { IsPublicAccessibilityRequired: true } or { FixupType: not SyntaxFixupType.None })
292+
// If a syntax fixup is needed, we need to update the loaded source files
293+
if (type is { FixupType: not SyntaxFixupType.None })
290294
{
291295
string adjustedSource;
292296

@@ -295,7 +299,7 @@ private void EmitGeneratedType(SourceProductionContext context, GeneratedType ty
295299
adjustedSource = reader.ReadToEnd();
296300
}
297301

298-
if (type.IsPublicAccessibilityRequired)
302+
if ((type.FixupType & SyntaxFixupType.UsePublicAccessibilityModifier) != 0)
299303
{
300304
// After reading the file, replace all internal keywords with public. Use a space before and after the identifier
301305
// to avoid potential false positives. This could also be done by loading the source tree and using a syntax
@@ -306,6 +310,12 @@ private void EmitGeneratedType(SourceProductionContext context, GeneratedType ty
306310
// If types are public, we also need to strip the '[Embedded]' attributes, as it's not allowed on public types
307311
adjustedSource = EmbeddedAttributeRegex.Replace(adjustedSource, "");
308312
}
313+
else if ((type.FixupType & SyntaxFixupType.RemoveEmbeddedAttributes) != 0)
314+
{
315+
// Remove the '[Embedded]' attributes as above, if requested. We only need this check if types
316+
// are not public, or these attributes would've already been stripped by the previous branch.
317+
adjustedSource = EmbeddedAttributeRegex.Replace(adjustedSource, "");
318+
}
309319

310320
if ((type.FixupType & SyntaxFixupType.RemoveMethodImplAttributes) != 0)
311321
{
@@ -344,7 +354,7 @@ namespace System.Runtime.CompilerServices2
344354
}
345355
else
346356
{
347-
// If the default accessibility is used, we can load the source directly
357+
// Otherwise if no fixups are needed, we can load the source directly
348358
sourceText = SourceText.From(stream, Encoding.UTF8, canBeEmbedded: true);
349359
}
350360

0 commit comments

Comments
 (0)