From 910df14429c5aecbd914c21196e0d7419a00b5e7 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Mon, 11 Apr 2022 22:03:36 +0200 Subject: [PATCH] Fix partial type generation for nested non-class types --- .../AutoConstructorGenerator.cs | 13 +++--- .../ID2D1ShaderGenerator.Helpers.cs | 11 +++-- .../ComputeSharp.SourceGeneration.projitems | 1 + .../Models/HierarchyInfo.cs | 17 ++++---- .../Models/TypeInfo.cs | 42 +++++++++++++++++++ .../IShaderGenerator.Helpers.cs | 11 +++-- .../AutoConstructorAttributeTests.cs | 28 +++++++++++++ 7 files changed, 97 insertions(+), 26 deletions(-) create mode 100644 src/ComputeSharp.SourceGeneration/Models/TypeInfo.cs diff --git a/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs b/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs index 3360921c..edffe701 100644 --- a/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs +++ b/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs @@ -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 // { // // } - 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); } diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs index 2e13636b..a033c0e8 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/ID2D1ShaderGenerator.Helpers.cs @@ -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 // { // // } - 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); } diff --git a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems index eff8ddd9..30b2bcf1 100644 --- a/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems +++ b/src/ComputeSharp.SourceGeneration/ComputeSharp.SourceGeneration.projitems @@ -21,5 +21,6 @@ + \ No newline at end of file diff --git a/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs b/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs index d9990a32..e2126d5e 100644 --- a/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs +++ b/src/ComputeSharp.SourceGeneration/Models/HierarchyInfo.cs @@ -14,8 +14,8 @@ namespace ComputeSharp.SourceGeneration.Models; /// The filename hint for the current type. /// The metadata name for the current type. /// Gets the namespace for the current type. -/// Gets the sequence of type definitions containing the current type. -internal sealed record HierarchyInfo(string FilenameHint, string MetadataName, string Namespace, ImmutableArray Names) +/// Gets the sequence of type definitions containing the current type. +internal sealed partial record HierarchyInfo(string FilenameHint, string MetadataName, string Namespace, ImmutableArray Hierarchy) { /// /// Creates a new instance from a given . @@ -24,20 +24,23 @@ internal sealed record HierarchyInfo(string FilenameHint, string MetadataName, s /// A instance describing . public static HierarchyInfo From(INamedTypeSymbol typeSymbol) { - ImmutableArray.Builder names = ImmutableArray.CreateBuilder(); + ImmutableArray.Builder hierarchy = ImmutableArray.CreateBuilder(); 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()); } /// @@ -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); } /// @@ -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); } } } diff --git a/src/ComputeSharp.SourceGeneration/Models/TypeInfo.cs b/src/ComputeSharp.SourceGeneration/Models/TypeInfo.cs new file mode 100644 index 00000000..f6d48c0a --- /dev/null +++ b/src/ComputeSharp.SourceGeneration/Models/TypeInfo.cs @@ -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; + +/// +/// A model describing a type info in a type hierarchy. +/// +/// The qualified name for the type. +/// The type of the type in the hierarchy. +/// Whether the type is a record type. +internal sealed record TypeInfo(string QualifiedName, TypeKind Kind, bool IsRecord) +{ + /// + /// Creates a instance for the current info. + /// + /// A instance for the current info. + public TypeDeclarationSyntax GetSyntax() + { + // Create the partial type declaration with the kind. + // This code produces a class declaration as follows: + // + // + // { + // } + // + // 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) + }; + } +} diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs index 2b39f4e1..3bffaf0c 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.Helpers.cs @@ -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 // { // // } - 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); } diff --git a/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs b/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs index 15ce06c5..469c9e25 100644 --- a/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs +++ b/tests/ComputeSharp.Tests/AutoConstructorAttributeTests.cs @@ -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); + } }