diff --git a/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs b/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs index edffe701..8a5771e5 100644 --- a/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs +++ b/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs @@ -8,7 +8,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using TypeInfo = ComputeSharp.SourceGeneration.Models.TypeInfo; namespace ComputeSharp.Core.SourceGenerators; @@ -106,62 +105,7 @@ public sealed partial class AutoConstructorGenerator : IIncrementalGenerator AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))) }; - // Create the partial shader type declaration with the given method implementation. - // This code produces a struct declaration as follows: - // - // partial struct - // { - // - // } - TypeDeclarationSyntax typeDeclarationSyntax = - StructDeclaration(hierarchyInfo.Hierarchy[0].QualifiedName) - .AddModifiers(Token(SyntaxKind.PartialKeyword)) - .AddMembers(constructorDeclaration.AddAttributeLists(attributes)); - - // Add all parent types in ascending order, if any - foreach (TypeInfo parentType in hierarchyInfo.Hierarchy.AsSpan().Slice(1)) - { - typeDeclarationSyntax = - parentType.GetSyntax() - .AddModifiers(Token(SyntaxKind.PartialKeyword)) - .AddMembers(typeDeclarationSyntax); - } - - if (hierarchyInfo.Namespace is "") - { - // If there is no namespace, attach the pragma directly to the declared type, - // and skip the namespace declaration. This will produce code as follows: - // - // #pragma warning disable - // - // - return - CompilationUnit().AddMembers( - typeDeclarationSyntax - .WithModifiers(TokenList(Token( - TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), - SyntaxKind.PartialKeyword, - TriviaList())))) - .NormalizeWhitespace(eol: "\n"); - } - - // Create the compilation unit with disabled warnings, target namespace and generated type. - // This will produce code as follows: - // - // #pragma warning disable - // - // namespace ; - // - // - return - CompilationUnit().AddMembers( - FileScopedNamespaceDeclaration(IdentifierName(hierarchyInfo.Namespace)) - .AddMembers(typeDeclarationSyntax) - .WithNamespaceKeyword(Token(TriviaList( - Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), - SyntaxKind.NamespaceKeyword, - TriviaList()))) - .NormalizeWhitespace(eol: "\n"); + return hierarchyInfo.GetSyntax(constructorDeclaration.AddAttributeLists(attributes)); } } } diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs index a033c0e8..776958fe 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs @@ -4,7 +4,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using TypeInfo = ComputeSharp.SourceGeneration.Models.TypeInfo; namespace ComputeSharp.D2D1Interop.SourceGenerators; @@ -68,62 +67,6 @@ partial class ID2D1ShaderGenerator attributes.Add(AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Runtime.CompilerServices.SkipLocalsInit"))))); } - // Create the partial shader type declaration with the given method implementation. - // This code produces a struct declaration as follows: - // - // partial struct - // { - // - // } - TypeDeclarationSyntax typeDeclarationSyntax = - StructDeclaration(hierarchyInfo.Hierarchy[0].QualifiedName) - .AddModifiers(Token(SyntaxKind.PartialKeyword)) - .AddMembers(methodDeclaration.AddAttributeLists(attributes.ToArray())); - - // Add all parent types in ascending order, if any - foreach (TypeInfo parentType in hierarchyInfo.Hierarchy.AsSpan().Slice(1)) - { - typeDeclarationSyntax = - parentType.GetSyntax() - .AddModifiers(Token(SyntaxKind.PartialKeyword)) - .AddMembers(typeDeclarationSyntax); - } - - if (hierarchyInfo.Namespace is "") - { - // If there is no namespace, attach the pragma directly to the declared type, - // and skip the namespace declaration. This is the same additional path also - // used when generating custom constructors. This will produce code as follows: - // - // #pragma warning disable - // - // - return - CompilationUnit().AddMembers( - typeDeclarationSyntax - .WithModifiers(TokenList(Token( - TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), - SyntaxKind.PartialKeyword, - TriviaList())))) - .NormalizeWhitespace(eol: "\n"); - } - - // Create the compilation unit with disabled warnings, target namespace and generated type. - // This will produce code as follows: - // - // #pragma warning disable - // - // namespace ; - // - // - return - CompilationUnit().AddMembers( - FileScopedNamespaceDeclaration(IdentifierName(hierarchyInfo.Namespace)) - .AddMembers(typeDeclarationSyntax) - .WithNamespaceKeyword(Token(TriviaList( - Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), - SyntaxKind.NamespaceKeyword, - TriviaList()))) - .NormalizeWhitespace(eol: "\n"); + return hierarchyInfo.GetSyntax(methodDeclaration.AddAttributeLists(attributes.ToArray())); } } diff --git a/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs b/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs index e2126d5e..9b4243ff 100644 --- a/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs +++ b/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs @@ -4,6 +4,9 @@ using System.Collections.Immutable; using ComputeSharp.SourceGeneration.Extensions; using ComputeSharp.SourceGeneration.Helpers; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; using static Microsoft.CodeAnalysis.SymbolDisplayTypeQualificationStyle; namespace ComputeSharp.SourceGeneration.Models; @@ -43,6 +46,71 @@ internal sealed partial record HierarchyInfo(string FilenameHint, string Metadat hierarchy.ToImmutable()); } + /// + /// Creates a instance for the current hierarchy. + /// + /// The member declarations to add to the generated type. + /// A instance for the current hierarchy. + public CompilationUnitSyntax GetSyntax(params MemberDeclarationSyntax[] memberDeclarations) + { + // Create the partial type declaration with for the current hierarchy. + // This code produces a type declaration as follows: + // + // partial + // { + // + // } + TypeDeclarationSyntax typeDeclarationSyntax = + Hierarchy[0].GetSyntax() + .AddModifiers(Token(SyntaxKind.PartialKeyword)) + .AddMembers(memberDeclarations); + + // Add all parent types in ascending order, if any + foreach (TypeInfo parentType in Hierarchy.AsSpan().Slice(1)) + { + typeDeclarationSyntax = + parentType.GetSyntax() + .AddModifiers(Token(SyntaxKind.PartialKeyword)) + .AddMembers(typeDeclarationSyntax); + } + + if (Namespace is "") + { + // If there is no namespace, attach the pragma directly to the declared type, + // and skip the namespace declaration. This will produce code as follows: + // + // #pragma warning disable + // + // + return + CompilationUnit().AddMembers( + typeDeclarationSyntax + .WithModifiers(TokenList(Token( + TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), + SyntaxKind.PartialKeyword, + TriviaList())))) + .NormalizeWhitespace(eol: "\n"); + } + + // Create the compilation unit with disabled warnings, target namespace and generated type. + // This will produce code as follows: + // + // #pragma warning disable + // + // namespace ; + // + // + return + CompilationUnit().AddMembers( + FileScopedNamespaceDeclaration(IdentifierName(Namespace)) + .AddMembers(typeDeclarationSyntax) + .WithNamespaceKeyword(Token(TriviaList( + Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), + SyntaxKind.NamespaceKeyword, + TriviaList()))) + .NormalizeWhitespace(eol: "\n"); + } + /// /// An implementation for . /// diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs index 3bffaf0c..88d0550c 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs @@ -5,7 +5,6 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; -using TypeInfo = ComputeSharp.SourceGeneration.Models.TypeInfo; namespace ComputeSharp.SourceGenerators; @@ -80,62 +79,6 @@ partial class IShaderGenerator attributes.Add(AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Runtime.CompilerServices.SkipLocalsInit"))))); } - // Create the partial shader type declaration with the given method implementation. - // This code produces a struct declaration as follows: - // - // partial struct - // { - // - // } - TypeDeclarationSyntax typeDeclarationSyntax = - StructDeclaration(hierarchyInfo.Hierarchy[0].QualifiedName) - .AddModifiers(Token(SyntaxKind.PartialKeyword)) - .AddMembers(methodDeclaration.AddAttributeLists(attributes.ToArray())); - - // Add all parent types in ascending order, if any - foreach (TypeInfo parentType in hierarchyInfo.Hierarchy.AsSpan().Slice(1)) - { - typeDeclarationSyntax = - parentType.GetSyntax() - .AddModifiers(Token(SyntaxKind.PartialKeyword)) - .AddMembers(typeDeclarationSyntax); - } - - if (hierarchyInfo.Namespace is "") - { - // If there is no namespace, attach the pragma directly to the declared type, - // and skip the namespace declaration. This is the same additional path also - // used when generating custom constructors. This will produce code as follows: - // - // #pragma warning disable - // - // - return - CompilationUnit().AddMembers( - typeDeclarationSyntax - .WithModifiers(TokenList(Token( - TriviaList(Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), - SyntaxKind.PartialKeyword, - TriviaList())))) - .NormalizeWhitespace(eol: "\n"); - } - - // Create the compilation unit with disabled warnings, target namespace and generated type. - // This will produce code as follows: - // - // #pragma warning disable - // - // namespace ; - // - // - return - CompilationUnit().AddMembers( - FileScopedNamespaceDeclaration(IdentifierName(hierarchyInfo.Namespace)) - .AddMembers(typeDeclarationSyntax) - .WithNamespaceKeyword(Token(TriviaList( - Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))), - SyntaxKind.NamespaceKeyword, - TriviaList()))) - .NormalizeWhitespace(eol: "\n"); + return hierarchyInfo.GetSyntax(methodDeclaration.AddAttributeLists(attributes.ToArray())); } }