Fixed fields generation with nested types

このコミットが含まれているのは:
Sergio Pedri committed 2020-11-27 16:26:52 +01:00
1 親 b6cd1916c3
コミット 4c8f233360
2ファイル変更
+67 -19

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

+65 -5
ファイルの表示
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
@@ -36,12 +37,31 @@ namespace ComputeSharp.SourceGenerators
group attribute by structDeclaration into groups
select groups);
foreach (IGrouping<StructDeclarationSyntax, AttributeData> group in attributes)
// Get an incremental map of discovered types
var discoveredTypes = HlslKnownSizes.KnownSizes.ToDictionary(static p => p.Key, static p => p.Value);
while (attributes.Count > 0)
{
var group = attributes.Dequeue();
SemanticModel semanticModel = context.Compilation.GetSemanticModel(group.Key.SyntaxTree);
INamedTypeSymbol structDeclarationSymbol = semanticModel.GetDeclaredSymbol(group.Key)!;
var generatedTypeInfo = GetProcessedFields(group);
// Check if a type has all the dependent types resolved already. If this is not the
// case, add the type at the back of the queue and continue. The other types will be
// processed first, and the current one will be picked up again when more info is available.
if (!AreDependentTypesDiscovered(structDeclarationSymbol, group, discoveredTypes.Keys))
{
attributes.Enqueue(group);
continue;
}
// Process the type and generate the necessary fields
var generatedTypeInfo = GetProcessedFields(group, discoveredTypes, out int size, out int pack);
// Add the current type to the target map
discoveredTypes.Add(structDeclarationSymbol.GetFullMetadataName(), (size, pack));
// Extract the info on the type to process
var namespaceName = structDeclarationSymbol.ContainingNamespace.ToDisplayString(new(typeQualificationStyle: NameAndContainingTypesAndNamespaces));
@@ -79,14 +99,54 @@ namespace ComputeSharp.SourceGenerators
}
}
/// <summary>
/// Checks whether all dependent types for a target type have been discovered,
/// and whether the target type can be processed to generate the target fields.
/// </summary>
/// <param name="targetTypeSymbol">The symbol of the target type to process.</param>
/// <param name="attributes">The collection of attributes (of type <see cref="FieldAttribute"/>).</param>
/// <param name="discoveredTypes">The map of currently discovered types.</param>
/// <returns>Whether or not the current type can be processed.</returns>
[Pure]
private static bool AreDependentTypesDiscovered(
INamedTypeSymbol targetTypeSymbol,
IEnumerable<AttributeData> attributes,
IReadOnlyCollection<string> discoveredTypes)
{
foreach (var attribute in attributes)
{
INamedTypeSymbol fieldType = (INamedTypeSymbol)attribute.ConstructorArguments[1].Value!;
// The current type can be processed if all the declared fields have been discovered
// (so if they're of a known primitive type) or are of a type that comes from a separate
// assembly. If a type has not been discovered, then that one will need to be processed first
// to ensure the necessary size/pack info will be available during generation. If the type comes
// from a separate assembly, the generator will assume that type will already contain the necessary
// layout annotation, which will be read directly while processing that specific field.
if (!discoveredTypes.Contains(fieldType.GetFullMetadataName()) &&
fieldType.ContainingAssembly.Identity.Equals(targetTypeSymbol.ContainingAssembly.Identity))
{
return false;
}
}
return true;
}
/// <summary>
/// Gets a sequence of field declarations for a given type.
/// </summary>
/// <param name="attributes">The input sequence of <see cref="FieldAttribute"/> data instances.</param>
/// <returns>An attribute for the layout of the kind, and the sequence of field declarations.</returns>
private static (AttributeSyntax Layout, IEnumerable<FieldDeclarationSyntax> Fields) GetProcessedFields(IEnumerable<AttributeData> attributes)
private static (AttributeSyntax Layout, IEnumerable<FieldDeclarationSyntax> Fields) GetProcessedFields(
IEnumerable<AttributeData> attributes,
IReadOnlyDictionary<string, (int Size, int Pack)> discoveredTypes,
out int size,
out int pack)
{
int size = 0, pack = 0;
size = 0;
pack = 0;
List<FieldDeclarationSyntax> fieldDeclarations = new();
foreach (var attribute in attributes)
@@ -96,7 +156,7 @@ namespace ComputeSharp.SourceGenerators
INamedTypeSymbol fieldType = (INamedTypeSymbol)attribute.ConstructorArguments[1].Value!;
// Get the size and pack for the current field
if (!HlslKnownSizes.TryGetMappedSize(fieldType.GetFullMetadataName(), out var mapping))
if (!discoveredTypes.TryGetValue(fieldType.GetFullMetadataName(), out var mapping))
{
var structLayout = fieldType.GetAttributes().First(a => a.AttributeClass is { Name: nameof(StructLayoutAttribute) });
+2 -14
ファイルの表示
@@ -10,9 +10,9 @@ namespace ComputeSharp.SourceGenerators.Mappings
internal static class HlslKnownSizes
{
/// <summary>
/// The mapping of supported known sizes to HLSL type names.
/// Gets the mapping of supported known sizes to HLSL type names.
/// </summary>
private static readonly IReadOnlyDictionary<string, (int, int)> KnownSizes = new Dictionary<string, (int, int)>
public static IReadOnlyDictionary<string, (int Size, int Pack)> KnownSizes { get; } = new Dictionary<string, (int, int)>
{
[typeof(bool).FullName] = (4, 4),
[typeof(int).FullName] = (4, 4),
@@ -44,17 +44,5 @@ namespace ComputeSharp.SourceGenerators.Mappings
[typeof(Double3).FullName] = (24, 8),
[typeof(Double3).FullName] = (32, 8)
};
/// <summary>
/// Tries to get the mapped HLSL size and packing for the input type name.
/// </summary>
/// <param name="name">The input fully qualified type name.</param>
/// <param name="mapped">The mapped name, if one is found.</param>
/// <returns>The HLSL size and packing that can be used in an HLSL shader.</returns>
[Pure]
public static bool TryGetMappedSize(string name, out (int Size, int Pack) mapped)
{
return KnownSizes.TryGetValue(name, out mapped);
}
}
}