using System.Collections.Generic; using ComputeSharp.SourceGeneration.Models; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; namespace ComputeSharp.D2D1.SourceGenerators; /// partial class ID2D1ShaderGenerator { /// /// Gets the shader type for a given shader, if any. /// /// The input instance to check. /// The instance currently in use. /// Whether or not is a D2D1 interface type. public static bool IsD2D1PixelShaderType(INamedTypeSymbol typeSymbol, Compilation compilation) { foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) { if (interfaceSymbol.Name == nameof(ID2D1PixelShader)) { INamedTypeSymbol d2D1PixelShaderSymbol = compilation.GetTypeByMetadataName("ComputeSharp.D2D1.ID2D1PixelShader")!; if (SymbolEqualityComparer.Default.Equals(interfaceSymbol, d2D1PixelShaderSymbol)) { return true; } } } return false; } /// /// Creates a instance wrapping the given method. /// /// The instance for the current type. /// The item to insert. /// Whether [SkipLocalsInit] can be used. /// Additional member declarations to also emit, if any. /// A object wrapping . private static CompilationUnitSyntax GetCompilationUnitFromMember( HierarchyInfo hierarchyInfo, MemberDeclarationSyntax memberDeclaration, bool canUseSkipLocalsInit, params MemberDeclarationSyntax[] additionalMemberDeclarations) { // Method attributes List attributes = new() { AttributeList(SingletonSeparatedList( Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode")).AddArgumentListArguments( AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ID2D1ShaderGenerator).FullName))), AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(ID2D1ShaderGenerator).Assembly.GetName().Version.ToString())))))), AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode")))), AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))), AttributeList(SingletonSeparatedList( Attribute(IdentifierName("global::System.ComponentModel.EditorBrowsable")).AddArgumentListArguments( AttributeArgument(ParseExpression("global::System.ComponentModel.EditorBrowsableState.Never"))))), AttributeList(SingletonSeparatedList( Attribute(IdentifierName("global::System.Obsolete")).AddArgumentListArguments( AttributeArgument(LiteralExpression( SyntaxKind.StringLiteralExpression, Literal("This method is not intended to be used directly by user code")))))) }; // Add [SkipLocalsInit] if the target project allows it if (canUseSkipLocalsInit) { attributes.Add(AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Runtime.CompilerServices.SkipLocalsInit"))))); } return hierarchyInfo.GetSyntax(new MemberDeclarationSyntax[] { memberDeclaration.AddAttributeLists(attributes.ToArray()) }, additionalMemberDeclarations); } }