chore: add AutoOverload and OverloadType attribute

This commit is contained in:
Oliver Booth 2023-04-06 17:00:39 +01:00
parent 172380c57d
commit 7556efdfdd
No known key found for this signature in database
GPG Key ID: 20BEB9DC87961025
4 changed files with 89 additions and 0 deletions

View File

@ -0,0 +1,22 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace SourceGenerator;
[Generator]
internal sealed class MethodOverloadGenerator : ISourceGenerator
{
/// <inheritdoc />
public void Initialize(GeneratorInitializationContext context)
{
context.RegisterForSyntaxNotifications(() => new OverloadSyntaxReceiver());
}
/// <inheritdoc />
public void Execute(GeneratorExecutionContext context)
{
var syntaxReceiver = (OverloadSyntaxReceiver)context.SyntaxReceiver!;
IReadOnlyList<MethodDeclarationSyntax> candidateMethods = syntaxReceiver.CandidateMethods;
// TODO implement
}
}

View File

@ -0,0 +1,41 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using X10D.MetaServices;
namespace SourceGenerator;
public class OverloadSyntaxReceiver : ISyntaxReceiver
{
private readonly List<MethodDeclarationSyntax> _candidateMethods = new();
public IReadOnlyList<MethodDeclarationSyntax> CandidateMethods
{
get => _candidateMethods.AsReadOnly();
}
public void OnVisitSyntaxNode(SyntaxNode syntaxNode)
{
if (syntaxNode is not MethodDeclarationSyntax methodDeclarationSyntax)
{
return;
}
if (methodDeclarationSyntax.AttributeLists.Count == 0)
{
return;
}
string attributeName = nameof(AutoOverloadAttribute).Replace("Attribute", string.Empty);
foreach (AttributeListSyntax attributeListSyntax in methodDeclarationSyntax.AttributeLists)
{
foreach (AttributeSyntax attributeSyntax in attributeListSyntax.Attributes)
{
if (attributeSyntax.Name.ToString() == attributeName)
{
_candidateMethods.Add(methodDeclarationSyntax);
break;
}
}
}
}
}

View File

@ -0,0 +1,6 @@
namespace X10D.MetaServices;
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
public sealed class AutoOverloadAttribute : Attribute
{
}

View File

@ -0,0 +1,20 @@
namespace X10D.MetaServices;
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Parameter | AttributeTargets.ReturnValue, Inherited = false)]
public sealed class OverloadTypeAttribute : Attribute
{
/// <summary>
/// Initializes a new instance of the <see cref="OverloadTypeAttribute"/> class.
/// </summary>
/// <param name="types">The types to overload.</param>
public OverloadTypeAttribute(params Type[] types)
{
Types = (Type[])types.Clone();
}
/// <summary>
/// Gets an array of types to overload.
/// </summary>
/// <value>An array of types to overload.</value>
public Type[] Types { get; }
}