diff --git a/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs b/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs index b7584b81..cc46fd43 100644 --- a/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs +++ b/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs @@ -1,5 +1,4 @@ -using System.Collections.Generic; -using System.Collections.Immutable; +using System.Collections.Immutable; using System.Linq; using System.Text; using ComputeSharp.Core.SourceGenerators.Models; @@ -31,11 +30,11 @@ public sealed class AutoConstructorGenerator : IIncrementalGenerator symbol.GetAttributes().Any(static a => a.AttributeClass?.ToDisplayString() == typeof(AutoConstructorAttribute).FullName))!; // Get the type hierarchy and fields info - IncrementalValuesProvider<(HierarchyInfo Left, ImmutableArray Right)> constructorInfo = + IncrementalValuesProvider<(HierarchyInfo Left, ConstructorInfo Right)> constructorInfo = structDeclarations - .Select(static (item, token) => (Hierarchy: HierarchyInfo.From(item), Parameters: Ctor.GetData(item))) - .Where(static item => !item.Parameters.IsEmpty) - .WithComparers(HierarchyInfo.Comparer.Default, EqualityComparer.Default.ForImmutableArray()); + .Select(static (item, token) => (Hierarchy: HierarchyInfo.From(item), Info: Ctor.GetData(item))) + .Where(static item => !item.Info.Parameters.IsEmpty) + .WithComparers(HierarchyInfo.Comparer.Default, ConstructorInfo.Comparer.Default); // Generate the constructors context.RegisterSourceOutput(constructorInfo, static (context, item) => @@ -55,23 +54,46 @@ public sealed class AutoConstructorGenerator : IIncrementalGenerator /// Gets the sequence of items for all fields in the input type. /// /// The being inspected. - /// The sequence of items for all fields in . - public static ImmutableArray GetData(INamedTypeSymbol structDeclarationSymbol) + /// The instance for . + public static ConstructorInfo GetData(INamedTypeSymbol structDeclarationSymbol) { - return ( - from fieldSymbol in structDeclarationSymbol.GetMembers().OfType() - where fieldSymbol is { IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false } - let typeName = fieldSymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) - select new ParameterInfo(typeName, fieldSymbol.Name)).ToImmutableArray(); + ImmutableArray.Builder parameters = ImmutableArray.CreateBuilder(); + ImmutableArray.Builder defaulted = ImmutableArray.CreateBuilder(); + + foreach (IFieldSymbol fieldSymbol in structDeclarationSymbol.GetMembers().OfType()) + { + // Skip fields that are not instance ones (and also ignore generated fields and fixed size buffers) + if (fieldSymbol is not { IsConst: false, IsStatic: false, IsFixedSizeBuffer: false, IsImplicitlyDeclared: false }) + { + continue; + } + + // Check whether the field is annotated to have a special behavior + if (fieldSymbol.Type.TryGetAttributeWithFullMetadataName("ComputeSharp.AutoConstructorBehaviorAttribute", out AttributeData? usageData) && + usageData!.ConstructorArguments is { Length: 1 } arguments && + arguments[0].Value is (int)AutoConstructorBehavior.IgnoreAndSetToDefault) + { + defaulted.Add(fieldSymbol.Name); + } + else + { + // Track the field normally + string typeName = fieldSymbol.Type.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); + + parameters.Add(new ParameterInfo(typeName, fieldSymbol.Name)); + } + } + + return new(parameters.ToImmutableArray(), defaulted.ToImmutableArray()); } /// /// Gets the instance for a given type and sequence of parameters. /// /// The type hierarchy info. - /// The sequence of parameters to initialize. + /// The instance with the info for the constructor to generate. /// A instance for a specified type. - public static CompilationUnitSyntax GetSyntax(HierarchyInfo hierarchyInfo, ImmutableArray parameters) + public static CompilationUnitSyntax GetSyntax(HierarchyInfo hierarchyInfo, ConstructorInfo constructorInfo) { // Create the constructor declaration for the type. This will // produce a constructor with simple initialization of all variables: @@ -82,15 +104,20 @@ public sealed class AutoConstructorGenerator : IIncrementalGenerator // this.b = b; // this.c = c; // ... + // this.ignored1 = default; + // this.ignored2 = default; + // this.ignored3 = default; + // ... // } ConstructorDeclarationSyntax constructorDeclaration = 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()); + .AddParameterListParameters(constructorInfo.Parameters.Select(field => Parameter(Identifier(field.Name)).WithType(IdentifierName(field.Type))).ToArray()) + .AddBodyStatements(constructorInfo.Parameters.Select(field => ParseStatement($"this.{field.Name} = {field.Name};")).ToArray()) + .AddBodyStatements(constructorInfo.DefaultedFields.Select(field => ParseStatement($"this.{field} = default;")).ToArray()); // Add the unsafe modifier, if needed - if (parameters.Any(static param => param.Type.Contains("*"))) + if (constructorInfo.Parameters.Any(static param => param.Type.Contains("*"))) { constructorDeclaration = constructorDeclaration.AddModifiers(Token(SyntaxKind.UnsafeKeyword)); } diff --git a/src/ComputeSharp.Core.SourceGenerators/ComputeSharp.Core.SourceGenerators.csproj b/src/ComputeSharp.Core.SourceGenerators/ComputeSharp.Core.SourceGenerators.csproj index 17c0c1bd..4c8fc75c 100644 --- a/src/ComputeSharp.Core.SourceGenerators/ComputeSharp.Core.SourceGenerators.csproj +++ b/src/ComputeSharp.Core.SourceGenerators/ComputeSharp.Core.SourceGenerators.csproj @@ -16,6 +16,8 @@ and to match members based on full name, so the types being in the generator assembly are not an issue.--> + + diff --git a/src/ComputeSharp.Core.SourceGenerators/Models/ConstructorInfo.cs b/src/ComputeSharp.Core.SourceGenerators/Models/ConstructorInfo.cs new file mode 100644 index 00000000..684014b0 --- /dev/null +++ b/src/ComputeSharp.Core.SourceGenerators/Models/ConstructorInfo.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Collections.Immutable; +using ComputeSharp.SourceGeneration.Extensions; +using ComputeSharp.SourceGeneration.Helpers; + +namespace ComputeSharp.Core.SourceGenerators.Models; + +/// +/// A model containiing information on a constructor to generate. +/// +/// The values for the constructor. +/// The names of ignored fields that should be defaulted. +internal sealed record ConstructorInfo(ImmutableArray Parameters, ImmutableArray DefaultedFields) +{ + /// + /// An implementation for . + /// + public sealed class Comparer : Comparer + { + /// + protected override void AddToHashCode(ref HashCode hashCode, ConstructorInfo obj) + { + hashCode.AddRange(obj.Parameters); + hashCode.AddRange(obj.DefaultedFields); + } + + /// + protected override bool AreEqual(ConstructorInfo x, ConstructorInfo y) + { + return + x.Parameters.SequenceEqual(y.Parameters) && + x.DefaultedFields.SequenceEqual(y.DefaultedFields); + } + } +} \ No newline at end of file diff --git a/src/ComputeSharp.Core/Attributes/Internals/AutoConstructorBehaviorAttribute.cs b/src/ComputeSharp.Core/Attributes/Internals/AutoConstructorBehaviorAttribute.cs index 35d32d62..f25456c6 100644 --- a/src/ComputeSharp.Core/Attributes/Internals/AutoConstructorBehaviorAttribute.cs +++ b/src/ComputeSharp.Core/Attributes/Internals/AutoConstructorBehaviorAttribute.cs @@ -5,7 +5,7 @@ namespace ComputeSharp; /// /// An attribute that indicates that a target shader type should get an automatic constructor for all fields. /// -[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] +[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false)] internal sealed class AutoConstructorBehaviorAttribute : Attribute { ///