Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide an option to convert a method with IAsyncEnumerable to IList, ReadOnlySpan, Span #23

Draft
wants to merge 26 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
141c9ea
Create enum for valid collection types
ValerieFernandes Nov 13, 2023
1c44e77
Add a property for variations
ValerieFernandes Nov 13, 2023
8d360da
Assign values so property can use or flags
ValerieFernandes Nov 13, 2023
de71e8e
Add comments to pass style checking and include System
ValerieFernandes Nov 14, 2023
3171bb0
Provide async rewriter with dictionary of overrides to the typical re…
ValerieFernandes Nov 14, 2023
76a3101
Add variation namespaces and retireve value from attributedata
ValerieFernandes Nov 14, 2023
ead92c4
Add enum to library so it can be used in generator
ValerieFernandes Nov 14, 2023
4bd3631
Check property to find which variants have been chosen
ValerieFernandes Nov 14, 2023
250d380
Update to use differet types and IEnumerable when nothing has been se…
ValerieFernandes Nov 14, 2023
7da552e
Add tests for all variations of CollectionTypes
ValerieFernandes Nov 14, 2023
ad229e3
Move IEnumerable to front of enum and use its CollectionTypes values …
ValerieFernandes Nov 15, 2023
aee4d45
Add using system directive back in
ValerieFernandes Nov 15, 2023
646522b
Fix test failures, and alter test to use CollectionTypes.
ValerieFernandes Nov 15, 2023
da595e6
Remove private property
ValerieFernandes Nov 15, 2023
c8744a0
Match by parameter name instead of list index
ValerieFernandes Nov 15, 2023
f6cff45
Add attribute for options
ValerieFernandes Nov 15, 2023
1916f7c
Seperate the ReplaceWith Attribute
ValerieFernandes Nov 16, 2023
c12fb15
Remove attribute from generated function
ValerieFernandes Nov 16, 2023
09bd558
Add new paramter types to dictionary from replace attribute
ValerieFernandes Nov 16, 2023
d63db80
Remove attribute from duplicating
ValerieFernandes Nov 16, 2023
06d4e75
Stop tests from comparing with new generated attribute
ValerieFernandes Nov 16, 2023
9d5b638
Retireve attribute and find its variation
ValerieFernandes Nov 16, 2023
c4aa8b6
Add CollectionTypes enum as embedded resource (not working as of now)
ValerieFernandes Nov 16, 2023
1d94cb8
Fix references in test projects
ValerieFernandes Nov 17, 2023
8a6b1c9
Fix logical path
ValerieFernandes Nov 17, 2023
f3e9358
Generate enum from file instead of string
ValerieFernandes Nov 17, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/Zomp.SyncMethodGenerator/AsyncToSyncRewriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace Zomp.SyncMethodGenerator;
/// Creates a new instance of <see cref="AsyncToSyncRewriter"/>.
/// </remarks>
/// <param name="semanticModel">The semantic model.</param>
internal sealed class AsyncToSyncRewriter(SemanticModel semanticModel) : CSharpSyntaxRewriter
/// <param name="replacementOverrides">The type of collection that should be used to replace IAsyncEnumerable.</param>
internal sealed class AsyncToSyncRewriter(SemanticModel semanticModel, Dictionary<string, string?> replacementOverrides) : CSharpSyntaxRewriter
{
public const string SyncOnly = "SYNC_ONLY";
private const string SystemObject = "object";
Expand Down Expand Up @@ -58,6 +59,7 @@ internal sealed class AsyncToSyncRewriter(SemanticModel semanticModel) : CSharpS
private readonly HashSet<string> memoryToSpan = [];
private readonly Dictionary<string, string> renamedLocalFunctions = [];
private readonly ImmutableArray<Diagnostic>.Builder diagnostics = ImmutableArray.CreateBuilder<Diagnostic>();
private Dictionary<string, string> parametersWithTypes = [];

private enum SyncOnlyDirectiveType
{
Expand Down Expand Up @@ -213,7 +215,12 @@ static BinaryExpressionSyntax CheckNull(ExpressionSyntax expr) => BinaryExpressi
var genericName = GetNameWithoutTypeParams(symbol);

string? newType = null;
if (Replacements.TryGetValue(genericName, out var replacement))

//if (@base.Att)
// {
// }
if (replacementOverrides.TryGetValue(genericName, out var replacement) ||
Replacements.TryGetValue(genericName, out replacement))
{
if (replacement is not null)
{
Expand Down Expand Up @@ -867,17 +874,19 @@ bool ShouldRemoveAttribute(AttributeSyntax attributeSyntax)
var attributeContainingTypeSymbol = attributeSymbol.ContainingType;

// Is the attribute [CreateSyncVersion] attribute?
return IsCreateSyncVersionAttribute(attributeContainingTypeSymbol);
return IsCreateSyncVersionAttribute(attributeContainingTypeSymbol) || IsReplaceWithAttribute(attributeContainingTypeSymbol);
}

var @base = (AttributeListSyntax)base.VisitAttributeList(node)!;
node.Attributes.ToList().ForEach(a => AddParameterTypes(a));
var indices = node.Attributes.GetIndices((a, _) => ShouldRemoveAttribute(a));
var newList = RemoveAtRange(@base.Attributes, indices);
return @base.WithAttributes(newList);
}

public override SyntaxNode? VisitAttribute(AttributeSyntax node)
{
parametersWithTypes.ContainsKey("k");
var @base = (AttributeSyntax)base.VisitAttribute(node)!;

if (GetSymbol(node.Name) is not IMethodSymbol ms)
Expand Down Expand Up @@ -1013,6 +1022,9 @@ private static bool CanDropElse(ElseClauseSyntax @else)
private static bool IsCreateSyncVersionAttribute(INamedTypeSymbol s)
=> s.ToDisplayString() == SyncMethodSourceGenerator.QualifiedCreateSyncVersionAttribute;

private static bool IsReplaceWithAttribute(INamedTypeSymbol s)
=> s.ToDisplayString() == SyncMethodSourceGenerator.QualifiedReplaceWithAttribute;

private static SyntaxList<TNode> RemoveAtRange<TNode>(SyntaxList<TNode> list, IEnumerable<int> indices)
where TNode : SyntaxNode
{
Expand Down Expand Up @@ -1188,6 +1200,22 @@ private static InvocationExpressionSyntax UnwrapExtension(InvocationExpressionSy
return replacement;
}

private void AddParameterTypes(AttributeSyntax attributeSyntax)
{
if (GetSymbol(attributeSyntax) is IMethodSymbol attributeSymbol)
{
var attributeContainingTypeSymbol = attributeSymbol.ContainingType;
if (IsReplaceWithAttribute(attributeContainingTypeSymbol) && attributeSyntax.Parent?.Parent is ParameterSyntax param
&& GetSymbol(param) is IParameterSymbol parameterSymbol)
{
var variation = parameterSymbol.GetAttributes()[0].NamedArguments.FirstOrDefault(c => c.Key == "Variations").Value.Value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you're busy with school and there's 0 urgency on this one. Just making a comment to not forget.

[0] is going to grab a wrong attribute if you have something like [Obsolete] in the first position.


//.NamedArguments.FirstOrDefault(c => c.Key == "Variations").Value.Value;
parametersWithTypes.Add(param.Identifier.ValueText, param.Identifier.ValueText);
}
}
}

private bool PreProcess(
SyntaxList<StatementSyntax> statements,
Dictionary<int, ExtraNodeInfo> extraNodeInfoList,
Expand Down
29 changes: 29 additions & 0 deletions src/Zomp.SyncMethodGenerator/CollectionTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace Zomp.SyncMethodGenerator
{
/// <summary>
/// All types that an IAsyncEnumerable can be converted into.
/// </summary>
[Flags]
public enum CollectionTypes
{
/// <summary>
/// Type for System.Collections.Generic.IEnumerable .
/// </summary>
IEnumerable = 1,

/// <summary>
/// Type for System.Collections.Generic.IList .
/// </summary>
IList = 2,

/// <summary>
/// Type for System.ReadOnlySpan .
/// </summary>
ReadOnlySpan = 4,

/// <summary>
/// Type for System.Span .
/// </summary>
Span = 8,
}
}
16 changes: 16 additions & 0 deletions src/Zomp.SyncMethodGenerator/SourceGenerationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ namespace Zomp.SyncMethodGenerator
[System.AttributeUsage(System.AttributeTargets.Method)]
internal class CreateSyncVersionAttribute : System.Attribute
{
public virtual CollectionTypes Variations { get; set; }
}
}
""";

internal const string ReplaceWithAttributeSource = """
// <auto-generated/>
namespace Zomp.SyncMethodGenerator
{
/// <summary>
/// An attribute that can be used to specify the synchronous type a parameter should be converted into .
/// </summary>
[System.AttributeUsage(System.AttributeTargets.Parameter)]
internal class ReplaceWithAttribute : System.Attribute
{
public virtual CollectionTypes Variations { get; set; }
}
}
""";
Expand Down
111 changes: 85 additions & 26 deletions src/Zomp.SyncMethodGenerator/SyncMethodSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ public class SyncMethodSourceGenerator : IIncrementalGenerator
/// Create sync version attribute string.
/// </summary>
public const string CreateSyncVersionAttribute = "CreateSyncVersionAttribute";

/// <summary>
/// Replace with attribute string.
/// </summary>
public const string ReplaceWithAttribute = "ReplaceWithAttribute";
internal const string QualifiedCreateSyncVersionAttribute = $"{ThisAssembly.RootNamespace}.{CreateSyncVersionAttribute}";
internal const string QualifiedReplaceWithAttribute = $"{ThisAssembly.RootNamespace}.{ReplaceWithAttribute}";

/// <inheritdoc/>
public void Initialize(IncrementalGeneratorInitializationContext context)
Expand All @@ -22,9 +28,16 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
Debugger.Launch();
}
#endif

context.RegisterPostInitializationOutput(ctx => ctx.AddSource(
$"CollectionTypes.g.cs", SourceText.From(GetResourceText("Zomp.SyncMethodGenerator.tests.CollectionTypes.cs"), Encoding.UTF8)));

context.RegisterPostInitializationOutput(ctx => ctx.AddSource(
$"{CreateSyncVersionAttribute}.g.cs", SourceText.From(SourceGenerationHelper.CreateSyncVersionAttributeSource, Encoding.UTF8)));

context.RegisterPostInitializationOutput(ctx => ctx.AddSource(
$"{ReplaceWithAttribute}.g.cs", SourceText.From(SourceGenerationHelper.ReplaceWithAttributeSource, Encoding.UTF8)));

IncrementalValuesProvider<MethodDeclarationSyntax> methodDeclarations = context.SyntaxProvider
.ForAttributeWithMetadataName(
QualifiedCreateSyncVersionAttribute,
Expand Down Expand Up @@ -93,6 +106,7 @@ private static void Execute(Compilation compilation, ImmutableArray<MethodDeclar
private static List<MethodToGenerate> GetTypesToGenerate(SourceProductionContext context, Compilation compilation, IEnumerable<MethodDeclarationSyntax> methodDeclarations, CancellationToken ct)
{
var methodsToGenerate = new List<MethodToGenerate>();

INamedTypeSymbol? attribute = compilation.GetTypeByMetadataName(QualifiedCreateSyncVersionAttribute);
if (attribute == null)
{
Expand Down Expand Up @@ -120,13 +134,19 @@ private static List<MethodToGenerate> GetTypesToGenerate(SourceProductionContext

var methodName = methodSymbol.ToString();

var variations = CollectionTypes.IEnumerable;
foreach (AttributeData attributeData in methodSymbol.GetAttributes())
{
if (!attribute.Equals(attributeData.AttributeClass, SymbolEqualityComparer.Default))
{
continue;
}

if (attributeData.NamedArguments.Length >= 1 && attributeData.NamedArguments.FirstOrDefault(c => c.Key == "Variations").Value.Value is int value)
{
variations = (CollectionTypes)value;
}

break;
}

Expand Down Expand Up @@ -161,47 +181,86 @@ private static List<MethodToGenerate> GetTypesToGenerate(SourceProductionContext
continue;
}

var rewriter = new AsyncToSyncRewriter(semanticModel);
var sn = rewriter.Visit(methodDeclarationSyntax);
var content = sn.ToFullString();
var collections = new List<string>();

var diagnostics = rewriter.Diagnostics;
if ((variations & CollectionTypes.IList) == CollectionTypes.IList)
{
collections.Add("System.Collections.Generic.IList");
}

var hasErrors = false;
foreach (var diagnostic in diagnostics)
if ((variations & CollectionTypes.Span) == CollectionTypes.Span)
{
context.ReportDiagnostic(diagnostic);
hasErrors |= diagnostic.Severity == DiagnosticSeverity.Error;
collections.Add("System.Span");
}

if (hasErrors)
if ((variations & CollectionTypes.ReadOnlySpan) == CollectionTypes.ReadOnlySpan)
{
continue;
collections.Add("System.ReadOnlySpan");
}

var isNamespaceFileScoped = false;
var namespaces = new List<string>();
while (node is not null && node is not CompilationUnitSyntax)
if ((variations & CollectionTypes.IEnumerable) == CollectionTypes.IEnumerable || collections.Count == 0)
{
switch (node)
collections.Add("System.Collections.Generic.IEnumerable");
}

foreach (var collection in collections)
{
var replacementOverrides = new Dictionary<string, string?>
{
case NamespaceDeclarationSyntax nds:
namespaces.Insert(0, nds.Name.ToString());
break;
case FileScopedNamespaceDeclarationSyntax file:
namespaces.Add(file.Name.ToString());
isNamespaceFileScoped = true;
break;
default:
throw new InvalidOperationException($"Cannot handle {node}");
{ "System.Collections.Generic.IAsyncEnumerable", collection },
};
var rewriter = new AsyncToSyncRewriter(semanticModel, replacementOverrides);
var sn = rewriter.Visit(methodDeclarationSyntax);
var content = sn.ToFullString();

var diagnostics = rewriter.Diagnostics;

var hasErrors = false;
foreach (var diagnostic in diagnostics)
{
context.ReportDiagnostic(diagnostic);
hasErrors |= diagnostic.Severity == DiagnosticSeverity.Error;
}

node = node.Parent;
}
if (hasErrors)
{
continue;
}

methodsToGenerate.Add(new(namespaces, isNamespaceFileScoped, classes, methodDeclarationSyntax.Identifier.ValueText, content));
var isNamespaceFileScoped = false;
var namespaces = new List<string>();
while (node is not null && node is not CompilationUnitSyntax)
{
switch (node)
{
case NamespaceDeclarationSyntax nds:
namespaces.Insert(0, nds.Name.ToString());
break;
case FileScopedNamespaceDeclarationSyntax file:
namespaces.Add(file.Name.ToString());
isNamespaceFileScoped = true;
break;
default:
throw new InvalidOperationException($"Cannot handle {node}");
}

node = node.Parent;
}

methodsToGenerate.Add(new(namespaces, isNamespaceFileScoped, classes, methodDeclarationSyntax.Identifier.ValueText, content));
}
}

return methodsToGenerate;
}

private string GetResourceText(string name)
{
using var stream = GetType().Assembly.GetManifestResourceStream(name);
using var streamReader = new StreamReader(stream);
return $"""
// <auto-generated/>
{streamReader.ReadToEnd()}
""";
}
}
6 changes: 6 additions & 0 deletions src/Zomp.SyncMethodGenerator/Zomp.SyncMethodGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
<None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="..\..\src\Zomp.SyncMethodGenerator\CollectionTypes.cs" Link="CollectionTypes.cs">
<LogicalName>Zomp.SyncMethodGenerator.tests.CollectionTypes.cs</LogicalName>
</EmbeddedResource>
</ItemGroup>

<!-- https://stackoverflow.com/a/59893520/6461844 -->
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="BuildOnlySettings;ResolveReferences">
Expand Down
6 changes: 5 additions & 1 deletion tests/GenerationSandbox.Tests/GenerationSandbox.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<NoWarn>$(NoWarn);CA1812;CA1852;SA1402;CA1822;SA1201;CA1303;SA1124;IDE0035;CS0162;RS1035;CS8619;CS8603;CA5394</NoWarn>
<NoWarn>$(NoWarn);CA1812;CA1852;SA1402;CA1822;SA1201;CA1303;SA1124;IDE0035;CS0162;RS1035;SA1601;CS8619;CS8603;CA5394</NoWarn>
<ImplicitUsings>false</ImplicitUsings>
<TargetFrameworks>net7.0;net6.0</TargetFrameworks>
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">$(TargetFrameworks);net472</TargetFrameworks>
Expand All @@ -27,6 +27,10 @@
<Compile Remove="AsyncExtensions.GenericMath.cs" />
<None Include="AsyncExtensions.GenericMath.cs" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="..\..\src\Zomp.SyncMethodGenerator\CollectionTypes.cs" Link="CollectionTypes.cs">
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<Using Include="System.Runtime.CompilerServices" />
<Using Include="System.Numerics" Condition="$(TargetFramework) != 'net6.0'" />
Expand Down
5 changes: 5 additions & 0 deletions tests/Generator.Tests/Generator.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<PackageReference Include="xunit.runner.visualstudio" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
<PackageReference Include="coverlet.collector" PrivateAssets="all" IncludeAssets="runtime; build; native; contentfiles; analyzers; buildtransitive" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="..\..\src\Zomp.SyncMethodGenerator\CollectionTypes.cs" Link="CollectionTypes.cs">
<LogicalName>Generator.Tests.CollectionTypes.cs</LogicalName>
</EmbeddedResource>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Zomp.SyncMethodGenerator\Zomp.SyncMethodGenerator.csproj" />
</ItemGroup>
Expand Down
19 changes: 18 additions & 1 deletion tests/Generator.Tests/IntegrationTesting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,24 @@ async Task EnumeratorTestAsync(IAsyncEnumerable<int> range, CancellationToken ct

[Fact]
public Task CombineTwoLists() => """
[CreateSyncVersion]
[CreateSyncVersion(Variations = (CollectionTypes.IList | CollectionTypes.Span))]
public static async IAsyncEnumerable<(TLeft Left, TRight Right)> CombineAsync<TLeft, TRight>(this IAsyncEnumerable<TLeft> list1, [ReplaceWith(Variations = CollectionTypes.Span)]IAsyncEnumerable<TRight> list2, [EnumeratorCancellation] CancellationToken ct = default)
{
await using var enumerator2 = list2.GetAsyncEnumerator(ct);
await foreach (var item in list1.WithCancellation(ct).ConfigureAwait(false))
{
if (!(await enumerator2.MoveNextAsync().ConfigureAwait(false)))
{
throw new InvalidOperationException("Must have the same size");
}
yield return (item, enumerator2.Current);
}
}
""".Verify();

[Fact]
public Task CombineTwoListsAll() => """
[CreateSyncVersion(Variations = (CollectionTypes.IList | CollectionTypes.Span | CollectionTypes.ReadOnlySpan | CollectionTypes.IEnumerable))]
public static async IAsyncEnumerable<(TLeft Left, TRight Right)> CombineAsync<TLeft, TRight>(this IAsyncEnumerable<TLeft> list1, IAsyncEnumerable<TRight> list2, [EnumeratorCancellation] CancellationToken ct = default)
{
await using var enumerator2 = list2.GetAsyncEnumerator(ct);
Expand Down
Loading
Loading