From 4c8f2333600d1c1fb25a0c942a9909e809f85ac6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Fri, 27 Nov 2020 16:26:52 +0100 Subject: [PATCH] Fixed fields generation with nested types --- .../FieldAttributeGenerator.cs | 70 +++++++++++++++++-- .../Mappings/HlslKnownSizes.cs | 16 +---- 2 files changed, 67 insertions(+), 19 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/FieldAttributeGenerator.cs b/src/ComputeSharp.SourceGenerators/FieldAttributeGenerator.cs index 5bd5e50f..8e0d189f 100644 --- a/src/ComputeSharp.SourceGenerators/FieldAttributeGenerator.cs +++ b/src/ComputeSharp.SourceGenerators/FieldAttributeGenerator.cs @@ -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 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 } } + /// + /// 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. + /// + /// The symbol of the target type to process. + /// The collection of attributes (of type ). + /// The map of currently discovered types. + /// Whether or not the current type can be processed. + [Pure] + private static bool AreDependentTypesDiscovered( + INamedTypeSymbol targetTypeSymbol, + IEnumerable attributes, + IReadOnlyCollection 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; + } + /// /// Gets a sequence of field declarations for a given type. /// /// The input sequence of data instances. /// An attribute for the layout of the kind, and the sequence of field declarations. - private static (AttributeSyntax Layout, IEnumerable Fields) GetProcessedFields(IEnumerable attributes) + private static (AttributeSyntax Layout, IEnumerable Fields) GetProcessedFields( + IEnumerable attributes, + IReadOnlyDictionary discoveredTypes, + out int size, + out int pack) { - int size = 0, pack = 0; + size = 0; + pack = 0; + List 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) }); diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownSizes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownSizes.cs index b8f86680..f870d3f7 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownSizes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownSizes.cs @@ -10,9 +10,9 @@ namespace ComputeSharp.SourceGenerators.Mappings internal static class HlslKnownSizes { /// - /// The mapping of supported known sizes to HLSL type names. + /// Gets the mapping of supported known sizes to HLSL type names. /// - private static readonly IReadOnlyDictionary KnownSizes = new Dictionary + public static IReadOnlyDictionary KnownSizes { get; } = new Dictionary { [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) }; - - /// - /// Tries to get the mapped HLSL size and packing for the input type name. - /// - /// The input fully qualified type name. - /// The mapped name, if one is found. - /// The HLSL size and packing that can be used in an HLSL shader. - [Pure] - public static bool TryGetMappedSize(string name, out (int Size, int Pack) mapped) - { - return KnownSizes.TryGetValue(name, out mapped); - } } }