using ComputeSharp.D2D1.__Internals;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
#pragma warning disable CS0618
namespace ComputeSharp.D2D1.SourceGenerators;
///
partial class ID2D1ShaderGenerator
{
///
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 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 metadataValueExpression = metadataValue switch
{
{ } => LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(metadataValue)),
_ => LiteralExpression(SyntaxKind.NullLiteralExpression)
};
// This code produces a method declaration as follows:
//
// readonly string? global::ComputeSharp.D2D1.__Internals.ID2D1Shader. => ;
return
PropertyDeclaration(NullableType(PredefinedType(Token(SyntaxKind.StringKeyword))), Identifier(propertyName))
.WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}")))
.AddModifiers(Token(SyntaxKind.ReadOnlyKeyword))
.WithExpressionBody(ArrowExpressionClause(metadataValueExpression))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken));
}
}
}