Fix partial type generation for nested non-class types

このコミットが含まれているのは:
Sergio Pedri committed 2022-04-24 21:04:29 +02:00
1 親 8de377b1d6
コミット 910df14429
7ファイル変更
+97 -26

検索条件に一致するファイルはありません

+1
ファイルの表示
@@ -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>
+10 -7
ファイルの表示
@@ -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);
}
}
}
+42
ファイルの表示
@@ -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)
};
}
}