using System.Diagnostics.CodeAnalysis; using ComputeSharp.D2D1.SourceGenerators.Helpers; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using ComputeSharp.SourceGeneration.Models; 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 collection of produced instances. /// The input object currently in use. /// The for the shader type in use. /// The resulting effect display name. public static string GetInfo( ImmutableArrayBuilder diagnostics, Compilation compilation, INamedTypeSymbol structDeclarationSymbol) { if (TryGetDefinedEffectDisplayName(compilation, structDeclarationSymbol, out string? effectDisplayName)) { return effectDisplayName; } return structDeclarationSymbol.GetFullyQualifiedMetadataName(); } /// /// 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; } } }