using System.Collections.Generic; using System.Linq; using ComputeSharp.SourceGeneration.Models; using ComputeSharp.SourceGenerators.Models; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; namespace ComputeSharp.SourceGenerators; /// partial class IShaderGenerator { /// /// Gets the shader type for a given shader, if any. /// /// The input instance to check. /// The instance currently in use. /// The shader type for , or . private static ShaderType? GetShaderType(INamedTypeSymbol typeSymbol, Compilation compilation) { foreach (INamedTypeSymbol interfaceSymbol in typeSymbol.AllInterfaces) { if (interfaceSymbol.Name == nameof(IComputeShader)) { INamedTypeSymbol computeShaderSymbol = compilation.GetTypeByMetadataName("ComputeSharp.IComputeShader")!; if (SymbolEqualityComparer.Default.Equals(interfaceSymbol, computeShaderSymbol)) { return ShaderType.ComputeShader; } } else if (interfaceSymbol is { IsGenericType: true, Name: nameof(IPixelShader) }) { INamedTypeSymbol pixelShaderSymbol = compilation.GetTypeByMetadataName("ComputeSharp.IPixelShader`1")!; if (SymbolEqualityComparer.Default.Equals(interfaceSymbol.ConstructedFrom, pixelShaderSymbol)) { return ShaderType.PixelShader; } } } return null; } /// /// Checks whether dynamic shaders are supported in a given compilation. /// /// The instance currently in use. /// Whether dynamic shaders are supported in the input compilation. private static bool IsDynamicCompilationSupported(Compilation compilation) { return compilation.ReferencedAssemblyNames.Any(static identity => identity.Name is "ComputeSharp.Dynamic"); } /// /// Creates a instance wrapping the given method. /// /// The instance for the current type. /// The item to insert. /// Whether [SkipLocalsInit] can be used. /// A object wrapping . private static CompilationUnitSyntax GetCompilationUnitFromMethod( HierarchyInfo hierarchyInfo, MethodDeclarationSyntax methodDeclaration, bool canUseSkipLocalsInit) { // Method attributes List attributes = new() { AttributeList(SingletonSeparatedList( Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode")).AddArgumentListArguments( AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(IShaderGenerator).FullName))), AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(IShaderGenerator).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(methodDeclaration.AddAttributeLists(attributes.ToArray())); } }