Reduce code duplication for type hierarchy generation
このコミットが含まれているのは:
1 親
910df14429
コミット
9609cb6607
4ファイル変更
+71
-173
検索条件に一致するファイルはありません
@@ -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 <SHADER_TYPE>
|
||||
// {
|
||||
// <METHOD>
|
||||
// }
|
||||
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
|
||||
//
|
||||
// <TYPE_HIERARCHY>
|
||||
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 <NAMESPACE>;
|
||||
//
|
||||
// <TYPE_HIERARCHY>
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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 <SHADER_TYPE>
|
||||
// {
|
||||
// <METHOD>
|
||||
// }
|
||||
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
|
||||
//
|
||||
// <TYPE_HIERARCHY>
|
||||
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 <NAMESPACE>;
|
||||
//
|
||||
// <TYPE_HIERARCHY>
|
||||
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()));
|
||||
}
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a <see cref="CompilationUnitSyntax"/> instance for the current hierarchy.
|
||||
/// </summary>
|
||||
/// <param name="memberDeclarations">The member declarations to add to the generated type.</param>
|
||||
/// <returns>A <see cref="CompilationUnitSyntax"/> instance for the current hierarchy.</returns>
|
||||
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 <TYPE_KIND> <TYPE_NAME>
|
||||
// {
|
||||
// <MEMBER_DECLARATIONS>
|
||||
// }
|
||||
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
|
||||
//
|
||||
// <TYPE_HIERARCHY>
|
||||
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 <NAMESPACE>;
|
||||
//
|
||||
// <TYPE_HIERARCHY>
|
||||
return
|
||||
CompilationUnit().AddMembers(
|
||||
FileScopedNamespaceDeclaration(IdentifierName(Namespace))
|
||||
.AddMembers(typeDeclarationSyntax)
|
||||
.WithNamespaceKeyword(Token(TriviaList(
|
||||
Trivia(PragmaWarningDirectiveTrivia(Token(SyntaxKind.DisableKeyword), true))),
|
||||
SyntaxKind.NamespaceKeyword,
|
||||
TriviaList())))
|
||||
.NormalizeWhitespace(eol: "\n");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IEqualityComparer{T}"/> implementation for <see cref="HierarchyInfo"/>.
|
||||
/// </summary>
|
||||
|
||||
@@ -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 <SHADER_TYPE>
|
||||
// {
|
||||
// <METHOD>
|
||||
// }
|
||||
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
|
||||
//
|
||||
// <TYPE_HIERARCHY>
|
||||
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 <NAMESPACE>;
|
||||
//
|
||||
// <TYPE_HIERARCHY>
|
||||
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()));
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする