using System; using System.Text; using ComputeSharp.D2D1.__Internals; 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; #pragma warning disable CS0618 namespace ComputeSharp.D2D1.SourceGenerators; /// partial class ID2D1ShaderGenerator { /// partial class EffectId { /// /// Creates a instance for the EffectId property. /// /// The input instance. /// An opaque instance to transform the final tree into text. /// The resulting instance for the EffectId property. public static PropertyDeclarationSyntax GetSyntax(EffectIdInfo info, out Func fixup) { // Create the local declaration: // // global::System.ReadOnlySpan bytes = new byte[] { __EMBEDDED_EFFECT_ID_BYTES }; LocalDeclarationStatementSyntax guidBytesDeclaration = LocalDeclarationStatement( VariableDeclaration( GenericName(Identifier("global::System.ReadOnlySpan")) .AddTypeArgumentListArguments(PredefinedType(Token(SyntaxKind.ByteKeyword)))) .AddVariables( VariableDeclarator(Identifier("bytes")) .WithInitializer( EqualsValueClause( ArrayCreationExpression( ArrayType(PredefinedType(Token(SyntaxKind.ByteKeyword))) .AddRankSpecifiers(ArrayRankSpecifier(SingletonSeparatedList(OmittedArraySizeExpression())))) .WithInitializer( InitializerExpression( SyntaxKind.ArrayInitializerExpression, SingletonSeparatedList(IdentifierName("__EMBEDDED_EFFECT_ID_BYTES")))))))); // Prepare the initialization text string effectIdLiterals = SyntaxFormattingHelper.BuildByteArrayInitializationExpressionString(info.Bytes.AsSpan()); // Set the fixup function fixup = tree => SourceText.From(tree.ToFullString().Replace("__EMBEDDED_EFFECT_ID_BYTES", effectIdLiterals), Encoding.UTF8); // Prepare the return statement: // // return // ref global::System.Runtime.CompilerServices.Unsafe.As( // ref global::System.Runtime.InteropServices.MemoryMarshal.GetReference(bytes)); ReturnStatementSyntax returnStatement = ReturnStatement( RefExpression( InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, IdentifierName("global::System.Runtime.CompilerServices.Unsafe"), GenericName(Identifier("As")) .AddTypeArgumentListArguments( PredefinedType(Token(SyntaxKind.ByteKeyword)), IdentifierName("global::System.Guid")))) .AddArgumentListArguments( Argument( InvocationExpression( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, IdentifierName("global::System.Runtime.InteropServices.MemoryMarshal"), IdentifierName("GetReference"))) .AddArgumentListArguments(Argument(IdentifierName("bytes")))) .WithRefOrOutKeyword(Token(SyntaxKind.RefKeyword))))); // This code produces a method declaration as follows: // // readonly ref readonly global::System.Guid global::ComputeSharp.D2D1.__Internals.ID2D1Shader.EffectId // { // [global::System.Runtime.CompilerServices.MethodImpl(global::System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)] // get // { // // // } // } return PropertyDeclaration( RefType(IdentifierName("global::System.Guid")).WithReadOnlyKeyword(Token(SyntaxKind.ReadOnlyKeyword)), Identifier(nameof(EffectId))) .WithExplicitInterfaceSpecifier(ExplicitInterfaceSpecifier(IdentifierName($"global::ComputeSharp.D2D1.__Internals.{nameof(ID2D1Shader)}"))) .AddModifiers(Token(SyntaxKind.ReadOnlyKeyword)) .AddAccessorListAccessors( AccessorDeclaration(SyntaxKind.GetAccessorDeclaration) .AddAttributeLists( AttributeList(SingletonSeparatedList( Attribute(IdentifierName("global::System.Runtime.CompilerServices.MethodImpl")) .AddArgumentListArguments( AttributeArgument( MemberAccessExpression( SyntaxKind.SimpleMemberAccessExpression, IdentifierName("global::System.Runtime.CompilerServices.MethodImplOptions"), IdentifierName("AggressiveInlining"))))))) .AddBodyStatements(guidBytesDeclaration, returnStatement)); } } }