using System; using System.Linq; using System.Text; using ComputeSharp.D2D1.SourceGenerators.Models; using ComputeSharp.SourceGeneration.Helpers; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Text; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; namespace ComputeSharp.D2D1.SourceGenerators; /// partial class D2DPixelShaderSourceGenerator { /// partial class Execute { /// /// Creates a instance for the TryGetBytecode method. /// /// The input bytecode info. /// An opaque instance to transform the final tree into text. /// The resulting instance for the target method. public static MethodDeclarationSyntax GetSyntax(EmbeddedBytecodeMethodInfo bytecodeInfo, out Func fixup) { // Get the TypeSyntax for the return type (either ReadOnlySpan, or an invalid one). // This is done so that if the user picks an invalid type, the code is still generated, // just returning default, so that proper diagnostic can be generated, without also // having hard to understand compiler errors due to the mismatched return type here. TypeSyntax returnType = bytecodeInfo.InvalidReturnType is string invalidReturnType ? IdentifierName(invalidReturnType) : GenericName(Identifier("global::System.ReadOnlySpan")) .AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword))); BlockSyntax methodBlock; if (bytecodeInfo.Bytecode.IsDefaultOrEmpty) { // This code generates the following block: // // { // return default; // } methodBlock = Block(ReturnStatement( LiteralExpression( SyntaxKind.DefaultLiteralExpression, Token(SyntaxKind.DefaultKeyword)))); fixup = tree => tree.GetText(Encoding.UTF8); } else { // This code generates the following block: // // { // return new byte[] { __EMBEDDED_SHADER_BYTECODE }; // } // // As in the ID2D1Shader generator, the __EMBEDDED_SHADER_BYTECODE identifier will be replaced with the bytecode // after the full string for the syntax tree has been generated, to avoid every literal going through a token. methodBlock = Block( ReturnStatement( ArrayCreationExpression( ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword))) .AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList(OmittedArraySizeExpression())))) .WithInitializer( InitializerExpression( SyntaxKind.ArrayInitializerExpression, SingletonSeparatedList(IdentifierName("__EMBEDDED_SHADER_BYTECODE")))))); string bytecodeLiterals = SyntaxFormattingHelper.BuildByteArrayInitializationExpressionString(bytecodeInfo.Bytecode.AsSpan()); // Prepare the fixup function fixup = tree => SourceText.From(tree.ToFullString().Replace("__EMBEDDED_SHADER_BYTECODE", bytecodeLiterals), Encoding.UTF8); } // This code produces a branch as follows: // // /// // [global::System.CodeDom.Compiler.GeneratedCode("...", "...")] // [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] // () // return MethodDeclaration(returnType, Identifier(bytecodeInfo.MethodName)) .AddModifiers(bytecodeInfo.Modifiers.Select(static m => Token((SyntaxKind)m)).ToArray()) .AddAttributeLists( AttributeList(SingletonSeparatedList( Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode")) .AddArgumentListArguments( AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(D2DPixelShaderSourceAttribute).FullName))), AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(D2DPixelShaderSourceAttribute).Assembly.GetName().Version.ToString())))))) .WithOpenBracketToken(Token(TriviaList(Comment( $"/// ")), SyntaxKind.OpenBracketToken, TriviaList())), AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage"))))) .WithBody(methodBlock); } } }