diff --git a/ComputeSharp.sln b/ComputeSharp.sln index 6f92e011..4797a17e 100644 --- a/ComputeSharp.sln +++ b/ComputeSharp.sln @@ -512,7 +512,6 @@ Global src\ComputeSharp.CodeFixing\ComputeSharp.CodeFixing.projitems*{9b4448b1-200f-4966-8a13-a508691b3003}*SharedItemsImports = 5 src\ComputeSharp.Win32.D2D1\ComputeSharp.Win32.D2D1.projitems*{9da1da9f-f8b2-4b25-be80-c21f773029e3}*SharedItemsImports = 13 samples\ComputeSharp.SwapChain.Shaders.D2D1.Shared\ComputeSharp.SwapChain.Shaders.D2D1.Shared.projitems*{9ea5ae9d-c39a-4f43-b03e-0a848ea2558a}*SharedItemsImports = 5 - src\ComputeSharp.SourceGeneration\ComputeSharp.SourceGeneration.projitems*{bfb003d1-788b-48de-8065-39479c162cb0}*SharedItemsImports = 5 samples\ComputeSharp.SwapChain.Shaders.Shared\ComputeSharp.SwapChain.Shaders.Shared.projitems*{c12d7ace-98ed-4813-8118-6667c34f484f}*SharedItemsImports = 5 src\ComputeSharp.SourceGeneration.Hlsl\ComputeSharp.SourceGeneration.Hlsl.projitems*{e44053bd-a761-47fb-aa78-087a599672ea}*SharedItemsImports = 5 src\ComputeSharp.SourceGeneration\ComputeSharp.SourceGeneration.projitems*{e44053bd-a761-47fb-aa78-087a599672ea}*SharedItemsImports = 5 diff --git a/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs b/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs deleted file mode 100644 index 0b980bc7..00000000 --- a/src/ComputeSharp.Core.SourceGenerators/AutoConstructorGenerator.cs +++ /dev/null @@ -1,149 +0,0 @@ -using System.Linq; -using System.Text; -using ComputeSharp.Core.SourceGenerators.Models; -using ComputeSharp.SourceGeneration.Extensions; -using ComputeSharp.SourceGeneration.Helpers; -using ComputeSharp.SourceGeneration.Models; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -#pragma warning disable CS0618, IDE0300 - -namespace ComputeSharp.Core.SourceGenerators; - -/// -/// A source generator creating constructors for types annotated with . -/// -[Generator(LanguageNames.CSharp)] -public sealed class AutoConstructorGenerator : IIncrementalGenerator -{ - /// - public void Initialize(IncrementalGeneratorInitializationContext context) - { - // Get all declared struct symbols with the [AutoConstructor] attribute and - // extract the type hierarchy and fields info for each of the annotated types - IncrementalValuesProvider<(HierarchyInfo Left, ConstructorInfo Right)> constructorInfo = - context.SyntaxProvider - .ForAttributeWithMetadataName( - typeof(AutoConstructorAttribute).FullName, - static (node, _) => node is StructDeclarationSyntax structDeclaration, - static (context, _) => - { - INamedTypeSymbol typeSymbol = (INamedTypeSymbol)context.TargetSymbol; - - ConstructorInfo constructorInfo = Ctor.GetData(typeSymbol); - - // If the type has no parameters, just do nothing - if (constructorInfo.Parameters.IsEmpty) - { - return default; - } - - HierarchyInfo hierarchyInfo = HierarchyInfo.From(typeSymbol); - - return (Hierarchy: hierarchyInfo, constructorInfo); - }) - .Where(static item => item.Hierarchy is not null); - - // Generate the constructors - context.RegisterSourceOutput(constructorInfo, static (context, item) => - { - CompilationUnitSyntax compilationUnit = Ctor.GetSyntax(item.Left, item.Right); - - context.AddSource($"{item.Left.FullyQualifiedMetadataName}.Ctor.g.cs", compilationUnit.GetText(Encoding.UTF8)); - }); - } - - /// - /// A helper with all logic to generate the constructor declarations. - /// - private static class Ctor - { - /// - /// Gets the sequence of items for all fields in the input type. - /// - /// The being inspected. - /// The instance for . - public static ConstructorInfo GetData(INamedTypeSymbol structDeclarationSymbol) - { - using ImmutableArrayBuilder parameters = new(); - using ImmutableArrayBuilder defaulted = new(); - - 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.TryGetAttributeWithFullyQualifiedMetadataName("ComputeSharp.AutoConstructorBehaviorAttribute", out AttributeData? usageData) && - usageData.ConstructorArguments is [{ Value: (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.ToImmutable(), defaulted.ToImmutable()); - } - - /// - /// Gets the instance for a given type and sequence of parameters. - /// - /// The type hierarchy info. - /// The instance with the info for the constructor to generate. - /// A instance for a specified type. - 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: - // - // public MyType(Foo a, Bar b, Baz c, ...) - // { - // this.a = a; - // 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(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 (constructorInfo.Parameters.Any(static param => param.Type.Contains("*"))) - { - constructorDeclaration = constructorDeclaration.AddModifiers(Token(SyntaxKind.UnsafeKeyword)); - } - - // Constructor attributes - AttributeListSyntax[] attributes = - { - AttributeList(SingletonSeparatedList( - Attribute(IdentifierName("global::System.CodeDom.Compiler.GeneratedCode")).AddArgumentListArguments( - AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(AutoConstructorGenerator).FullName))), - AttributeArgument(LiteralExpression(SyntaxKind.StringLiteralExpression, Literal(typeof(AutoConstructorGenerator).Assembly.GetName().Version.ToString())))))), - AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.DebuggerNonUserCode")))), - AttributeList(SingletonSeparatedList(Attribute(IdentifierName("global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage")))) - }; - - return hierarchyInfo.GetSyntax(constructorDeclaration.AddAttributeLists(attributes)); - } - } -} \ No newline at end of file diff --git a/src/ComputeSharp.Core.SourceGenerators/ComputeSharp.Core.SourceGenerators.csproj b/src/ComputeSharp.Core.SourceGenerators/ComputeSharp.Core.SourceGenerators.csproj index 56bbb7f6..f42af995 100644 --- a/src/ComputeSharp.Core.SourceGenerators/ComputeSharp.Core.SourceGenerators.csproj +++ b/src/ComputeSharp.Core.SourceGenerators/ComputeSharp.Core.SourceGenerators.csproj @@ -2,15 +2,6 @@ netstandard2.0 - - - - - - - @@ -20,7 +11,4 @@ - - - diff --git a/src/ComputeSharp.Core.SourceGenerators/Models/ConstructorInfo.cs b/src/ComputeSharp.Core.SourceGenerators/Models/ConstructorInfo.cs deleted file mode 100644 index db5ee135..00000000 --- a/src/ComputeSharp.Core.SourceGenerators/Models/ConstructorInfo.cs +++ /dev/null @@ -1,10 +0,0 @@ -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(EquatableArray Parameters, EquatableArray DefaultedFields); \ No newline at end of file diff --git a/src/ComputeSharp.Core.SourceGenerators/Models/ParameterInfo.cs b/src/ComputeSharp.Core.SourceGenerators/Models/ParameterInfo.cs deleted file mode 100644 index 3f45388a..00000000 --- a/src/ComputeSharp.Core.SourceGenerators/Models/ParameterInfo.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace ComputeSharp.Core.SourceGenerators.Models; - -/// -/// A model describing info on a constructor parameter. -/// -/// The fully qualified type name. -/// The parameter name. -internal sealed record ParameterInfo(string Type, string Name); \ No newline at end of file