mirror of
https://github.com/oliverbooth/X10D
synced 2024-11-22 08:18:47 +00:00
chore: add AutoOverload and OverloadType attribute
This commit is contained in:
parent
172380c57d
commit
7556efdfdd
22
tools/SourceGenerator/MethodOverloadGenerator.cs
Normal file
22
tools/SourceGenerator/MethodOverloadGenerator.cs
Normal 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
|
||||
}
|
||||
}
|
41
tools/SourceGenerator/OverloadSyntaxReceiver.cs
Normal file
41
tools/SourceGenerator/OverloadSyntaxReceiver.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
6
tools/X10D.MetaServices/AutoOverloadAttribute.cs
Normal file
6
tools/X10D.MetaServices/AutoOverloadAttribute.cs
Normal file
@ -0,0 +1,6 @@
|
||||
namespace X10D.MetaServices;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
|
||||
public sealed class AutoOverloadAttribute : Attribute
|
||||
{
|
||||
}
|
20
tools/X10D.MetaServices/OverloadTypeAttribute.cs
Normal file
20
tools/X10D.MetaServices/OverloadTypeAttribute.cs
Normal 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; }
|
||||
}
|
Loading…
Reference in New Issue
Block a user