-
Notifications
You must be signed in to change notification settings - Fork 754
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1575 from bash/async-overloads-source-generator
Async overloads source generator
- Loading branch information
Showing
41 changed files
with
1,537 additions
and
1,819 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
Ix.NET/Source/System.Linq.Async.SourceGenerator/AsyncMethod.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace System.Linq.Async.SourceGenerator | ||
{ | ||
internal sealed record AsyncMethod(IMethodSymbol Symbol, MethodDeclarationSyntax Syntax); | ||
} |
8 changes: 8 additions & 0 deletions
8
Ix.NET/Source/System.Linq.Async.SourceGenerator/AsyncMethodGrouping.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System.Collections.Generic; | ||
|
||
using Microsoft.CodeAnalysis; | ||
|
||
namespace System.Linq.Async.SourceGenerator | ||
{ | ||
internal sealed record AsyncMethodGrouping(SyntaxTree SyntaxTree, IEnumerable<AsyncMethod> Methods); | ||
} |
116 changes: 116 additions & 0 deletions
116
Ix.NET/Source/System.Linq.Async.SourceGenerator/AsyncOverloadsGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; | ||
|
||
namespace System.Linq.Async.SourceGenerator | ||
{ | ||
[Generator] | ||
public sealed class AsyncOverloadsGenerator : ISourceGenerator | ||
{ | ||
private const string AttributeSource = | ||
"using System;\n" + | ||
"using System.Diagnostics;\n" + | ||
"namespace System.Linq\n" + | ||
"{\n" + | ||
" [AttributeUsage(AttributeTargets.Method)]\n" + | ||
" [Conditional(\"COMPILE_TIME_ONLY\")]\n" + | ||
" internal sealed class GenerateAsyncOverloadAttribute : Attribute { }\n" + | ||
"}\n"; | ||
|
||
public void Initialize(GeneratorInitializationContext context) | ||
{ | ||
context.RegisterForSyntaxNotifications(() => new SyntaxReceiver()); | ||
context.RegisterForPostInitialization(c => c.AddSource("GenerateAsyncOverloadAttribute", AttributeSource)); | ||
} | ||
|
||
public void Execute(GeneratorExecutionContext context) | ||
{ | ||
if (context.SyntaxReceiver is not SyntaxReceiver syntaxReceiver) return; | ||
|
||
var options = GetGenerationOptions(context); | ||
var methodsBySyntaxTree = GetMethodsGroupedBySyntaxTree(context, syntaxReceiver); | ||
|
||
foreach (var grouping in methodsBySyntaxTree) | ||
context.AddSource( | ||
$"{Path.GetFileNameWithoutExtension(grouping.SyntaxTree.FilePath)}.AsyncOverloads", | ||
GenerateOverloads(grouping, options)); | ||
} | ||
|
||
private static GenerationOptions GetGenerationOptions(GeneratorExecutionContext context) | ||
=> new(SupportFlatAsyncApi: context.ParseOptions.PreprocessorSymbolNames.Contains("SUPPORT_FLAT_ASYNC_API")); | ||
|
||
private static IEnumerable<AsyncMethodGrouping> GetMethodsGroupedBySyntaxTree(GeneratorExecutionContext context, SyntaxReceiver syntaxReceiver) | ||
=> GetMethodsGroupedBySyntaxTree( | ||
context, | ||
syntaxReceiver, | ||
GetAsyncOverloadAttributeSymbol(context)); | ||
|
||
private static string GenerateOverloads(AsyncMethodGrouping grouping, GenerationOptions options) | ||
{ | ||
var usings = grouping.SyntaxTree.GetRoot() is CompilationUnitSyntax compilationUnit | ||
? compilationUnit.Usings.ToString() | ||
: string.Empty; | ||
|
||
var overloads = new StringBuilder(); | ||
overloads.AppendLine("#nullable enable"); | ||
overloads.AppendLine(usings); | ||
overloads.AppendLine("namespace System.Linq"); | ||
overloads.AppendLine("{"); | ||
overloads.AppendLine(" partial class AsyncEnumerable"); | ||
overloads.AppendLine(" {"); | ||
|
||
foreach (var method in grouping.Methods) | ||
overloads.AppendLine(GenerateOverload(method, options)); | ||
|
||
overloads.AppendLine(" }"); | ||
overloads.AppendLine("}"); | ||
|
||
return overloads.ToString(); | ||
} | ||
|
||
private static string GenerateOverload(AsyncMethod method, GenerationOptions options) | ||
=> MethodDeclaration(method.Syntax.ReturnType, GetMethodName(method.Symbol, options)) | ||
.WithModifiers(TokenList(Token(SyntaxKind.PublicKeyword), Token(SyntaxKind.StaticKeyword))) | ||
.WithTypeParameterList(method.Syntax.TypeParameterList) | ||
.WithParameterList(method.Syntax.ParameterList) | ||
.WithConstraintClauses(method.Syntax.ConstraintClauses) | ||
.WithExpressionBody(ArrowExpressionClause( | ||
InvocationExpression( | ||
IdentifierName(method.Symbol.Name), | ||
ArgumentList( | ||
SeparatedList( | ||
method.Syntax.ParameterList.Parameters | ||
.Select(p => Argument(IdentifierName(p.Identifier)))))))) | ||
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken)) | ||
.WithLeadingTrivia(method.Syntax.GetLeadingTrivia().Where(t => t.GetStructure() is not DirectiveTriviaSyntax)) | ||
.NormalizeWhitespace() | ||
.ToFullString(); | ||
|
||
private static INamedTypeSymbol GetAsyncOverloadAttributeSymbol(GeneratorExecutionContext context) | ||
=> context.Compilation.GetTypeByMetadataName("System.Linq.GenerateAsyncOverloadAttribute") ?? throw new InvalidOperationException(); | ||
|
||
private static IEnumerable<AsyncMethodGrouping> GetMethodsGroupedBySyntaxTree(GeneratorExecutionContext context, SyntaxReceiver syntaxReceiver, INamedTypeSymbol attributeSymbol) | ||
=> from candidate in syntaxReceiver.Candidates | ||
group candidate by candidate.SyntaxTree into grouping | ||
let model = context.Compilation.GetSemanticModel(grouping.Key) | ||
select new AsyncMethodGrouping( | ||
grouping.Key, | ||
from methodSyntax in grouping | ||
let methodSymbol = model.GetDeclaredSymbol(methodSyntax) ?? throw new InvalidOperationException() | ||
where methodSymbol.GetAttributes().Any(a => SymbolEqualityComparer.Default.Equals(a.AttributeClass!, attributeSymbol)) | ||
select new AsyncMethod(methodSymbol, methodSyntax)); | ||
|
||
private static string GetMethodName(IMethodSymbol methodSymbol, GenerationOptions options) | ||
{ | ||
var methodName = methodSymbol.Name.Replace("Core", ""); | ||
return options.SupportFlatAsyncApi | ||
? methodName.Replace("Await", "").Replace("WithCancellation", "") | ||
: methodName; | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
Ix.NET/Source/System.Linq.Async.SourceGenerator/GenerationOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace System.Linq.Async.SourceGenerator | ||
{ | ||
internal sealed record GenerationOptions(bool SupportFlatAsyncApi); | ||
} |
20 changes: 20 additions & 0 deletions
20
Ix.NET/Source/System.Linq.Async.SourceGenerator/SyntaxReceiver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Collections.Generic; | ||
|
||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
namespace System.Linq.Async.SourceGenerator | ||
{ | ||
internal sealed class SyntaxReceiver : ISyntaxReceiver | ||
{ | ||
public IList<MethodDeclarationSyntax> Candidates { get; } = new List<MethodDeclarationSyntax>(); | ||
|
||
public void OnVisitSyntaxNode(SyntaxNode syntaxNode) | ||
{ | ||
if (syntaxNode is MethodDeclarationSyntax { AttributeLists: { Count: >0 } } methodDeclarationSyntax) | ||
{ | ||
Candidates.Add(methodDeclarationSyntax); | ||
} | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
Ix.NET/Source/System.Linq.Async.SourceGenerator/System.Linq.Async.SourceGenerator.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<LangVersion>9.0</LangVersion> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.9.0" PrivateAssets="all" /> | ||
<PackageReference Include="IsExternalInit" Version="1.0.0" PrivateAssets="all" /> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"solution": { | ||
"path": "Ix.NET.sln", | ||
"projects": [ | ||
"System.Linq.Async\\System.Linq.Async.csproj", | ||
"System.Linq.Async.Tests\\System.Linq.Async.Tests.csproj", | ||
"System.Linq.Async.SourceGenerator\\System.Linq.Async.SourceGenerator.csproj", | ||
"refs\\System.Linq.Async.Ref\\System.Linq.Async.Ref.csproj" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.