Fix partial type generation for nested non-class types
このコミットが含まれているのは:
1 親
8de377b1d6
コミット
910df14429
7ファイル変更
+97
-26
検索条件に一致するファイルはありません
@@ -8,6 +8,7 @@ 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;
|
||||
|
||||
@@ -83,7 +84,7 @@ public sealed partial class AutoConstructorGenerator : IIncrementalGenerator
|
||||
// ...
|
||||
// }
|
||||
ConstructorDeclarationSyntax constructorDeclaration =
|
||||
ConstructorDeclaration(hierarchyInfo.Names[0])
|
||||
ConstructorDeclaration(hierarchyInfo.Hierarchy[0].QualifiedName)
|
||||
.AddModifiers(Token(SyntaxKind.PublicKeyword))
|
||||
.AddParameterListParameters(parameters.Select(field => Parameter(Identifier(field.Name)).WithType(IdentifierName(field.Type))).ToArray())
|
||||
.AddBodyStatements(parameters.Select(field => ParseStatement($"this.{field.Name} = {field.Name};")).ToArray());
|
||||
@@ -112,18 +113,16 @@ public sealed partial class AutoConstructorGenerator : IIncrementalGenerator
|
||||
// {
|
||||
// <METHOD>
|
||||
// }
|
||||
StructDeclarationSyntax structDeclarationSyntax =
|
||||
StructDeclaration(hierarchyInfo.Names[0])
|
||||
TypeDeclarationSyntax typeDeclarationSyntax =
|
||||
StructDeclaration(hierarchyInfo.Hierarchy[0].QualifiedName)
|
||||
.AddModifiers(Token(SyntaxKind.PartialKeyword))
|
||||
.AddMembers(constructorDeclaration.AddAttributeLists(attributes));
|
||||
|
||||
TypeDeclarationSyntax typeDeclarationSyntax = structDeclarationSyntax;
|
||||
|
||||
// Add all parent types in ascending order, if any
|
||||
foreach (string parentType in hierarchyInfo.Names.AsSpan().Slice(1))
|
||||
foreach (TypeInfo parentType in hierarchyInfo.Hierarchy.AsSpan().Slice(1))
|
||||
{
|
||||
typeDeclarationSyntax =
|
||||
ClassDeclaration(parentType)
|
||||
parentType.GetSyntax()
|
||||
.AddModifiers(Token(SyntaxKind.PartialKeyword))
|
||||
.AddMembers(typeDeclarationSyntax);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ 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;
|
||||
|
||||
@@ -74,18 +75,16 @@ partial class ID2D1ShaderGenerator
|
||||
// {
|
||||
// <METHOD>
|
||||
// }
|
||||
StructDeclarationSyntax structDeclarationSyntax =
|
||||
StructDeclaration(hierarchyInfo.Names[0])
|
||||
TypeDeclarationSyntax typeDeclarationSyntax =
|
||||
StructDeclaration(hierarchyInfo.Hierarchy[0].QualifiedName)
|
||||
.AddModifiers(Token(SyntaxKind.PartialKeyword))
|
||||
.AddMembers(methodDeclaration.AddAttributeLists(attributes.ToArray()));
|
||||
|
||||
TypeDeclarationSyntax typeDeclarationSyntax = structDeclarationSyntax;
|
||||
|
||||
// Add all parent types in ascending order, if any
|
||||
foreach (string parentType in hierarchyInfo.Names.AsSpan().Slice(1))
|
||||
foreach (TypeInfo parentType in hierarchyInfo.Hierarchy.AsSpan().Slice(1))
|
||||
{
|
||||
typeDeclarationSyntax =
|
||||
ClassDeclaration(parentType)
|
||||
parentType.GetSyntax()
|
||||
.AddModifiers(Token(SyntaxKind.PartialKeyword))
|
||||
.AddMembers(typeDeclarationSyntax);
|
||||
}
|
||||
|
||||
@@ -21,5 +21,6 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Models\DiagnosticInfo.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Models\HierarchyInfo.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Models\Result{T}.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)Models\TypeInfo.cs" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -14,8 +14,8 @@ namespace ComputeSharp.SourceGeneration.Models;
|
||||
/// <param name="FilenameHint">The filename hint for the current type.</param>
|
||||
/// <param name="MetadataName">The metadata name for the current type.</param>
|
||||
/// <param name="Namespace">Gets the namespace for the current type.</param>
|
||||
/// <param name="Names">Gets the sequence of type definitions containing the current type.</param>
|
||||
internal sealed record HierarchyInfo(string FilenameHint, string MetadataName, string Namespace, ImmutableArray<string> Names)
|
||||
/// <param name="Hierarchy">Gets the sequence of type definitions containing the current type.</param>
|
||||
internal sealed partial record HierarchyInfo(string FilenameHint, string MetadataName, string Namespace, ImmutableArray<TypeInfo> Hierarchy)
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="HierarchyInfo"/> instance from a given <see cref="INamedTypeSymbol"/>.
|
||||
@@ -24,20 +24,23 @@ internal sealed record HierarchyInfo(string FilenameHint, string MetadataName, s
|
||||
/// <returns>A <see cref="HierarchyInfo"/> instance describing <paramref name="typeSymbol"/>.</returns>
|
||||
public static HierarchyInfo From(INamedTypeSymbol typeSymbol)
|
||||
{
|
||||
ImmutableArray<string>.Builder names = ImmutableArray.CreateBuilder<string>();
|
||||
ImmutableArray<TypeInfo>.Builder hierarchy = ImmutableArray.CreateBuilder<TypeInfo>();
|
||||
|
||||
for (INamedTypeSymbol? parent = typeSymbol;
|
||||
parent is not null;
|
||||
parent = parent.ContainingType)
|
||||
{
|
||||
names.Add(parent.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
|
||||
hierarchy.Add(new TypeInfo(
|
||||
parent.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat),
|
||||
parent.TypeKind,
|
||||
parent.IsRecord));
|
||||
}
|
||||
|
||||
return new(
|
||||
typeSymbol.GetGeneratedFileName(),
|
||||
typeSymbol.MetadataName,
|
||||
typeSymbol.ContainingNamespace.ToDisplayString(new(typeQualificationStyle: NameAndContainingTypesAndNamespaces)),
|
||||
names.ToImmutable());
|
||||
hierarchy.ToImmutable());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -51,7 +54,7 @@ internal sealed record HierarchyInfo(string FilenameHint, string MetadataName, s
|
||||
hashCode.Add(obj.FilenameHint);
|
||||
hashCode.Add(obj.MetadataName);
|
||||
hashCode.Add(obj.Namespace);
|
||||
hashCode.AddRange(obj.Names);
|
||||
hashCode.AddRange(obj.Hierarchy);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
@@ -61,7 +64,7 @@ internal sealed record HierarchyInfo(string FilenameHint, string MetadataName, s
|
||||
x.FilenameHint == y.FilenameHint &&
|
||||
x.MetadataName == y.MetadataName &&
|
||||
x.Namespace == y.Namespace &&
|
||||
x.Names.SequenceEqual(y.Names);
|
||||
x.Hierarchy.SequenceEqual(y.Hierarchy);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using Microsoft.CodeAnalysis.CSharp.Syntax;
|
||||
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
|
||||
|
||||
namespace ComputeSharp.SourceGeneration.Models;
|
||||
|
||||
/// <summary>
|
||||
/// A model describing a type info in a type hierarchy.
|
||||
/// </summary>
|
||||
/// <param name="QualifiedName">The qualified name for the type.</param>
|
||||
/// <param name="Kind">The type of the type in the hierarchy.</param>
|
||||
/// <param name="IsRecord">Whether the type is a record type.</param>
|
||||
internal sealed record TypeInfo(string QualifiedName, TypeKind Kind, bool IsRecord)
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a <see cref="TypeDeclarationSyntax"/> instance for the current info.
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="TypeDeclarationSyntax"/> instance for the current info.</returns>
|
||||
public TypeDeclarationSyntax GetSyntax()
|
||||
{
|
||||
// Create the partial type declaration with the kind.
|
||||
// This code produces a class declaration as follows:
|
||||
//
|
||||
// <TYPE_KIND> <TYPE_NAME>
|
||||
// {
|
||||
// }
|
||||
//
|
||||
// Note that specifically for record declarations, we also need to explicitly add the open
|
||||
// and close brace tokens, otherwise member declarations will not be formatted correctly.
|
||||
return Kind switch
|
||||
{
|
||||
TypeKind.Struct => StructDeclaration(QualifiedName),
|
||||
TypeKind.Interface => InterfaceDeclaration(QualifiedName),
|
||||
TypeKind.Class when IsRecord =>
|
||||
RecordDeclaration(Token(SyntaxKind.RecordKeyword), QualifiedName)
|
||||
.WithOpenBraceToken(Token(SyntaxKind.OpenBraceToken))
|
||||
.WithCloseBraceToken(Token(SyntaxKind.CloseBraceToken)),
|
||||
_ => ClassDeclaration(QualifiedName)
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ 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;
|
||||
|
||||
@@ -86,18 +87,16 @@ partial class IShaderGenerator
|
||||
// {
|
||||
// <METHOD>
|
||||
// }
|
||||
StructDeclarationSyntax structDeclarationSyntax =
|
||||
StructDeclaration(hierarchyInfo.Names[0])
|
||||
TypeDeclarationSyntax typeDeclarationSyntax =
|
||||
StructDeclaration(hierarchyInfo.Hierarchy[0].QualifiedName)
|
||||
.AddModifiers(Token(SyntaxKind.PartialKeyword))
|
||||
.AddMembers(methodDeclaration.AddAttributeLists(attributes.ToArray()));
|
||||
|
||||
TypeDeclarationSyntax typeDeclarationSyntax = structDeclarationSyntax;
|
||||
|
||||
// Add all parent types in ascending order, if any
|
||||
foreach (string parentType in hierarchyInfo.Names.AsSpan().Slice(1))
|
||||
foreach (TypeInfo parentType in hierarchyInfo.Hierarchy.AsSpan().Slice(1))
|
||||
{
|
||||
typeDeclarationSyntax =
|
||||
ClassDeclaration(parentType)
|
||||
parentType.GetSyntax()
|
||||
.AddModifiers(Token(SyntaxKind.PartialKeyword))
|
||||
.AddMembers(typeDeclarationSyntax);
|
||||
}
|
||||
|
||||
@@ -26,4 +26,32 @@ public unsafe partial class AutoConstructorAttributeTests
|
||||
Assert.AreEqual(instance.b, 3.14f);
|
||||
Assert.IsTrue(instance.c == &c);
|
||||
}
|
||||
|
||||
public partial interface ISomeInterface
|
||||
{
|
||||
public partial record SomeRecord
|
||||
{
|
||||
public partial struct SomeStruct
|
||||
{
|
||||
[AutoConstructor]
|
||||
public partial struct AnotherStruct
|
||||
{
|
||||
public int a;
|
||||
public float b;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void GenerateConstructorWithNestedTypes()
|
||||
{
|
||||
int a = 42;
|
||||
float b = 3.14f;
|
||||
|
||||
ISomeInterface.SomeRecord.SomeStruct.AnotherStruct instance = new(a, b);
|
||||
|
||||
Assert.AreEqual(instance.a, a);
|
||||
Assert.AreEqual(instance.b, b);
|
||||
}
|
||||
}
|
||||
新しいイシューから参照
ユーザーをブロックする