Unify metadata info and syntax code in generator
このコミットが含まれているのは:
1 親
8edc54748f
コミット
2f0d5695b3
6ファイル変更
+96
-77
検索条件に一致するファイルはありません
+1
-1
@@ -47,7 +47,7 @@ public sealed class InvalidEffectDisplayNameValueAnalyzer : DiagnosticAnalyzer
|
||||
}
|
||||
|
||||
// Validate the effect display name
|
||||
if (!D2D1EffectMetadataParser.IsValidEffectDisplayName(attributeData))
|
||||
if (!D2D1EffectMetadataParser.IsValidEffectMetadataName(attributeData))
|
||||
{
|
||||
context.ReportDiagnostic(Diagnostic.Create(
|
||||
InvalidD2DEffectDisplayNameAttributeValue,
|
||||
|
||||
@@ -17,11 +17,11 @@ internal static class D2D1EffectMetadataParser
|
||||
private static readonly Regex NewLinesRegex = new("[\r\n\v]", RegexOptions.Compiled);
|
||||
|
||||
/// <summary>
|
||||
/// Checks whether a given <see cref="AttributeData"/> value contains a valid effect display name.
|
||||
/// Checks whether a given <see cref="AttributeData"/> value contains a valid effect metadata name.
|
||||
/// </summary>
|
||||
/// <param name="attributeData">The input <see cref="AttributeData"/> object to inspect.</param>
|
||||
/// <returns>Whether <paramref name="attributeData"/> contains a valid effect display name.</returns>
|
||||
public static bool IsValidEffectDisplayName(AttributeData attributeData)
|
||||
/// <returns>Whether <paramref name="attributeData"/> contains a valid effect metadata name.</returns>
|
||||
public static bool IsValidEffectMetadataName(AttributeData attributeData)
|
||||
{
|
||||
if (attributeData.ConstructorArguments is [{ Value: string { Length: > 0 } value }])
|
||||
{
|
||||
@@ -36,14 +36,44 @@ internal static class D2D1EffectMetadataParser
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the defined effect display name for a given attribute.
|
||||
/// Tries to get the defined effect metadata name for a given shader type.
|
||||
/// </summary>
|
||||
/// <param name="compilation">The input <see cref="Compilation"/> object currently in use.</param>
|
||||
/// <param name="typeSymbol">The input <see cref="INamedTypeSymbol"/> instance.</param>
|
||||
/// <param name="attributeFullyQualifiedMetadataName">The fully qualified metadata name of the target attribute.</param>
|
||||
/// <param name="effectMetadataName">The resulting defined effect metadata name, if found.</param>
|
||||
/// <returns>Whether or not a defined effect metadata name could be found.</returns>
|
||||
public static bool TryGetEffectMetadataName(
|
||||
Compilation compilation,
|
||||
INamedTypeSymbol typeSymbol,
|
||||
string attributeFullyQualifiedMetadataName,
|
||||
[NotNullWhen(true)] out string? effectMetadataName)
|
||||
{
|
||||
INamedTypeSymbol metadataAttributeSymbol = compilation.GetTypeByMetadataName(attributeFullyQualifiedMetadataName)!;
|
||||
|
||||
foreach (AttributeData attributeData in typeSymbol.GetAttributes())
|
||||
{
|
||||
// Check that the attribute is the one we're looking for
|
||||
if (SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, metadataAttributeSymbol))
|
||||
{
|
||||
return TryGetEffectMetadataName(attributeData, out effectMetadataName);
|
||||
}
|
||||
}
|
||||
|
||||
effectMetadataName = default;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the defined effect metadata name for a given attribute.
|
||||
/// </summary>
|
||||
/// <param name="attributeData">The input <see cref="AttributeData"/> object to inspect.</param>
|
||||
/// <param name="effectDisplayName">The resulting defined effect display name, if found.</param>
|
||||
/// <returns>Whether or not a defined effect display name could be found.</returns>
|
||||
public static bool TryGetEffectDisplayName(AttributeData attributeData, [NotNullWhen(true)] out string? effectDisplayName)
|
||||
/// <param name="effectMetadataName">The resulting defined effect metadata name, if found.</param>
|
||||
/// <returns>Whether or not a defined effect metadata name could be found.</returns>
|
||||
public static bool TryGetEffectMetadataName(AttributeData attributeData, [NotNullWhen(true)] out string? effectMetadataName)
|
||||
{
|
||||
// Check that the attribute is [D2DEffectDisplayName] and with a valid parameter
|
||||
// Check that the attribute has a valid parameter
|
||||
if (attributeData.ConstructorArguments is [{ Value: string { Length: > 0 } value }])
|
||||
{
|
||||
// Remove new lines (the values cannot span multiple lines in XML)
|
||||
@@ -55,13 +85,13 @@ internal static class D2D1EffectMetadataParser
|
||||
// Trim the display name as well
|
||||
if (escapedValue.AsSpan().Trim() is { Length: > 0 } trimmedValue)
|
||||
{
|
||||
effectDisplayName = trimmedValue.ToString();
|
||||
effectMetadataName = trimmedValue.ToString();
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
effectDisplayName = default;
|
||||
effectMetadataName = default;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
-56
@@ -1,56 +0,0 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using ComputeSharp.D2D1.SourceGenerators.Helpers;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.D2D1.SourceGenerators;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class ID2D1ShaderGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// A helper with all logic to generate the <c>EffectDisplayName</c> property.
|
||||
/// </summary>
|
||||
private static partial class EffectDisplayName
|
||||
{
|
||||
/// <summary>
|
||||
/// Extracts the effect display name info for the current shader.
|
||||
/// </summary>
|
||||
/// <param name="compilation">The input <see cref="Compilation"/> object currently in use.</param>
|
||||
/// <param name="structDeclarationSymbol">The <see cref="INamedTypeSymbol"/> for the shader type in use.</param>
|
||||
/// <returns>The resulting effect display name, if available.</returns>
|
||||
public static string? GetInfo(Compilation compilation, INamedTypeSymbol structDeclarationSymbol)
|
||||
{
|
||||
if (TryGetDefinedEffectDisplayName(compilation, structDeclarationSymbol, out string? effectDisplayName))
|
||||
{
|
||||
return effectDisplayName;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the defined effect display name for a given shader type.
|
||||
/// </summary>
|
||||
/// <param name="compilation">The input <see cref="Compilation"/> object currently in use.</param>
|
||||
/// <param name="typeSymbol">The input <see cref="INamedTypeSymbol"/> instance.</param>
|
||||
/// <param name="effectDisplayName">The resulting defined effect display name, if found.</param>
|
||||
/// <returns>Whether or not a defined effect display name could be found.</returns>
|
||||
private static bool TryGetDefinedEffectDisplayName(Compilation compilation, INamedTypeSymbol typeSymbol, [NotNullWhen(true)] out string? effectDisplayName)
|
||||
{
|
||||
INamedTypeSymbol effectIdAttributeSymbol = compilation.GetTypeByMetadataName("ComputeSharp.D2D1.D2DEffectDisplayNameAttribute")!;
|
||||
|
||||
foreach (AttributeData attributeData in typeSymbol.GetAttributes())
|
||||
{
|
||||
// Check that the attribute is [D2DEffectDisplayName]
|
||||
if (SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, effectIdAttributeSymbol))
|
||||
{
|
||||
return D2D1EffectMetadataParser.TryGetEffectDisplayName(attributeData, out effectDisplayName);
|
||||
}
|
||||
}
|
||||
|
||||
effectDisplayName = default;
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
-7
@@ -11,30 +11,41 @@ namespace ComputeSharp.D2D1.SourceGenerators;
|
||||
partial class ID2D1ShaderGenerator
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
partial class EffectDisplayName
|
||||
partial class EffectMetadata
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="PropertyDeclarationSyntax"/> instance for the <c>EffectDisplayName</c> property.
|
||||
/// </summary>
|
||||
/// <param name="effectDisplayName">The input effect display name value, if available.</param>
|
||||
/// <returns>The resulting <see cref="PropertyDeclarationSyntax"/> instance for the <c>EffectDisplayName</c> property.</returns>
|
||||
public static PropertyDeclarationSyntax GetSyntax(string? effectDisplayName)
|
||||
public static PropertyDeclarationSyntax GetEffectDisplayNameSyntax(string? effectDisplayName)
|
||||
{
|
||||
return GetEffectMetadataSyntax("EffectDisplayName", effectDisplayName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="PropertyDeclarationSyntax"/> instance for a given metadata property.
|
||||
/// </summary>
|
||||
/// <param name="propertyName">The property name to generate.</param>
|
||||
/// <param name="metadataValue">The input effect metadata value, if available.</param>
|
||||
/// <returns>The resulting <see cref="PropertyDeclarationSyntax"/> instance for the given metadata property.</returns>
|
||||
private static PropertyDeclarationSyntax GetEffectMetadataSyntax(string propertyName, string? metadataValue)
|
||||
{
|
||||
// Get the expression: either a string literal, or just null
|
||||
ExpressionSyntax displayNameExpression = effectDisplayName switch
|
||||
ExpressionSyntax metadataValueExpression = metadataValue switch
|
||||
{
|
||||
{ } => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(effectDisplayName)),
|
||||
{ } => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(metadataValue)),
|
||||
_ => LiteralExpression(SyntaxKind.NullLiteralExpression)
|
||||
};
|
||||
|
||||
// This code produces a method declaration as follows:
|
||||
//
|
||||
// readonly string? global::ComputeSharp.D2D1.__Internals.ID2D1Shader.EffectDisplayName => <EFFECT_DISPLAY_NAME>;
|
||||
// readonly string? global::ComputeSharp.D2D1.__Internals.ID2D1Shader.<PROPERTY_NAME> => <METADATA_VALUE>;
|
||||
return
|
||||
PropertyDeclaration(NullableType(PredefinedType(Token(SyntaxKind.StringKeyword))), Identifier(nameof(EffectDisplayName)))
|
||||
PropertyDeclaration(NullableType(PredefinedType(Token(SyntaxKind.StringKeyword))), Identifier(propertyName))
|
||||
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
|
||||
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
|
||||
.WithExpressionBody(ArrowExpressionClause(displayNameExpression))
|
||||
.WithExpressionBody(ArrowExpressionClause(metadataValueExpression))
|
||||
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
using ComputeSharp.D2D1.SourceGenerators.Helpers;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
||||
namespace ComputeSharp.D2D1.SourceGenerators;
|
||||
|
||||
/// <inheritdoc/>
|
||||
partial class ID2D1ShaderGenerator
|
||||
{
|
||||
/// <summary>
|
||||
/// A helper with all logic to generate the effect metadata properties.
|
||||
/// </summary>
|
||||
private static partial class EffectMetadata
|
||||
{
|
||||
/// <summary>
|
||||
/// Extracts the effect display name info for the current shader.
|
||||
/// </summary>
|
||||
/// <param name="compilation">The input <see cref="Compilation"/> object currently in use.</param>
|
||||
/// <param name="structDeclarationSymbol">The <see cref="INamedTypeSymbol"/> for the shader type in use.</param>
|
||||
/// <returns>The resulting effect display name, if available.</returns>
|
||||
public static string? GetEffectDisplayNameInfo(Compilation compilation, INamedTypeSymbol structDeclarationSymbol)
|
||||
{
|
||||
if (D2D1EffectMetadataParser.TryGetEffectMetadataName(
|
||||
compilation,
|
||||
structDeclarationSymbol,
|
||||
"ComputeSharp.D2D1.D2DEffectDisplayNameAttribute",
|
||||
out string? effectDisplayName))
|
||||
{
|
||||
return effectDisplayName;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,7 +58,7 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
// EffectDisplayName info
|
||||
string? effectDisplayName = EffectDisplayName.GetInfo(context.SemanticModel.Compilation, typeSymbol);
|
||||
string? effectDisplayName = EffectMetadata.GetEffectDisplayNameInfo(context.SemanticModel.Compilation, typeSymbol);
|
||||
|
||||
token.ThrowIfCancellationRequested();
|
||||
|
||||
@@ -156,10 +156,10 @@ public sealed partial class ID2D1ShaderGenerator : IIncrementalGenerator
|
||||
// Generate the EffectDisplayName properties
|
||||
context.RegisterSourceOutput(effectDisplayNameInfo, static (context, item) =>
|
||||
{
|
||||
PropertyDeclarationSyntax effectDisplayNameProperty = EffectDisplayName.GetSyntax(item.EffectDisplayName);
|
||||
PropertyDeclarationSyntax effectDisplayNameProperty = EffectMetadata.GetEffectDisplayNameSyntax(item.EffectDisplayName);
|
||||
CompilationUnitSyntax compilationUnit = GetCompilationUnitFromMember(item.Hierarchy, effectDisplayNameProperty, canUseSkipLocalsInit: false);
|
||||
|
||||
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.{nameof(EffectDisplayName)}.g.cs", compilationUnit.GetText(Encoding.UTF8));
|
||||
context.AddSource($"{item.Hierarchy.FullyQualifiedMetadataName}.EffectDisplayName.g.cs", compilationUnit.GetText(Encoding.UTF8));
|
||||
});
|
||||
|
||||
// Get the EffectId info (hierarchy and effect id info)
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする