diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectDisplayNameValueAnalyzer.cs b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectDisplayNameValueAnalyzer.cs
index ec1cbabb..9fc3bd75 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectDisplayNameValueAnalyzer.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Diagnostics/Analyzers/InvalidEffectDisplayNameValueAnalyzer.cs
@@ -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,
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs b/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs
index 79148ef2..fcb4cba9 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/Helpers/D2D1EffectMetadataParser.cs
@@ -17,11 +17,11 @@ internal static class D2D1EffectMetadataParser
private static readonly Regex NewLinesRegex = new("[\r\n\v]", RegexOptions.Compiled);
///
- /// Checks whether a given value contains a valid effect display name.
+ /// Checks whether a given value contains a valid effect metadata name.
///
/// The input object to inspect.
- /// Whether contains a valid effect display name.
- public static bool IsValidEffectDisplayName(AttributeData attributeData)
+ /// Whether contains a valid effect metadata name.
+ public static bool IsValidEffectMetadataName(AttributeData attributeData)
{
if (attributeData.ConstructorArguments is [{ Value: string { Length: > 0 } value }])
{
@@ -36,14 +36,44 @@ internal static class D2D1EffectMetadataParser
}
///
- /// 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.
+ ///
+ /// The input object currently in use.
+ /// The input instance.
+ /// The fully qualified metadata name of the target attribute.
+ /// The resulting defined effect metadata name, if found.
+ /// Whether or not a defined effect metadata name could be found.
+ 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;
+ }
+
+ ///
+ /// Tries to get the defined effect metadata name for a given attribute.
///
/// The input object to inspect.
- /// The resulting defined effect display name, if found.
- /// Whether or not a defined effect display name could be found.
- public static bool TryGetEffectDisplayName(AttributeData attributeData, [NotNullWhen(true)] out string? effectDisplayName)
+ /// The resulting defined effect metadata name, if found.
+ /// Whether or not a defined effect metadata name could be found.
+ 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;
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs
deleted file mode 100644
index 556add8c..00000000
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.cs
+++ /dev/null
@@ -1,56 +0,0 @@
-using System.Diagnostics.CodeAnalysis;
-using ComputeSharp.D2D1.SourceGenerators.Helpers;
-using Microsoft.CodeAnalysis;
-
-namespace ComputeSharp.D2D1.SourceGenerators;
-
-///
-partial class ID2D1ShaderGenerator
-{
- ///
- /// A helper with all logic to generate the EffectDisplayName property.
- ///
- private static partial class EffectDisplayName
- {
- ///
- /// Extracts the effect display name info for the current shader.
- ///
- /// The input object currently in use.
- /// The for the shader type in use.
- /// The resulting effect display name, if available.
- public static string? GetInfo(Compilation compilation, INamedTypeSymbol structDeclarationSymbol)
- {
- if (TryGetDefinedEffectDisplayName(compilation, structDeclarationSymbol, out string? effectDisplayName))
- {
- return effectDisplayName;
- }
-
- return null;
- }
-
- ///
- /// Tries to get the defined effect display name for a given shader type.
- ///
- /// The input object currently in use.
- /// The input instance.
- /// The resulting defined effect display name, if found.
- /// Whether or not a defined effect display name could be found.
- 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;
- }
- }
-}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.Syntax.cs
similarity index 57%
rename from src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs
rename to src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.Syntax.cs
index 0dd5f13c..30fa468d 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectDisplayNameProperty.Syntax.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.Syntax.cs
@@ -11,30 +11,41 @@ namespace ComputeSharp.D2D1.SourceGenerators;
partial class ID2D1ShaderGenerator
{
///
- partial class EffectDisplayName
+ partial class EffectMetadata
{
///
/// Creates a instance for the EffectDisplayName property.
///
/// The input effect display name value, if available.
/// The resulting instance for the EffectDisplayName property.
- public static PropertyDeclarationSyntax GetSyntax(string? effectDisplayName)
+ public static PropertyDeclarationSyntax GetEffectDisplayNameSyntax(string? effectDisplayName)
+ {
+ return GetEffectMetadataSyntax("EffectDisplayName", effectDisplayName);
+ }
+
+ ///
+ /// Creates a instance for a given metadata property.
+ ///
+ /// The property name to generate.
+ /// The input effect metadata value, if available.
+ /// The resulting instance for the given metadata property.
+ 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 => ;
+ // readonly string? global::ComputeSharp.D2D1.__Internals.ID2D1Shader. => ;
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));
}
}
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs
new file mode 100644
index 00000000..61efa038
--- /dev/null
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateEffectMetadataProperties.cs
@@ -0,0 +1,34 @@
+using ComputeSharp.D2D1.SourceGenerators.Helpers;
+using Microsoft.CodeAnalysis;
+
+namespace ComputeSharp.D2D1.SourceGenerators;
+
+///
+partial class ID2D1ShaderGenerator
+{
+ ///
+ /// A helper with all logic to generate the effect metadata properties.
+ ///
+ private static partial class EffectMetadata
+ {
+ ///
+ /// Extracts the effect display name info for the current shader.
+ ///
+ /// The input object currently in use.
+ /// The for the shader type in use.
+ /// The resulting effect display name, if available.
+ public static string? GetEffectDisplayNameInfo(Compilation compilation, INamedTypeSymbol structDeclarationSymbol)
+ {
+ if (D2D1EffectMetadataParser.TryGetEffectMetadataName(
+ compilation,
+ structDeclarationSymbol,
+ "ComputeSharp.D2D1.D2DEffectDisplayNameAttribute",
+ out string? effectDisplayName))
+ {
+ return effectDisplayName;
+ }
+
+ return null;
+ }
+ }
+}
\ No newline at end of file
diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
index 3a4812e0..069525f1 100644
--- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
+++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.cs
@@ -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)