diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/Extensions/ISymbolExtensions.cs index d2d8a5ed..623a2a82 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/Extensions/ISymbolExtensions.cs @@ -1,11 +1,7 @@ -using System.Collections.Generic; -using System.Collections.Immutable; +using System.Collections.Immutable; using System.Linq; using System.Text; -using ComputeSharp.D2D1Interop.SourceGenerators.Mappings; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; namespace ComputeSharp.D2D1Interop.SourceGenerators.Extensions; @@ -17,10 +13,9 @@ internal static class ISymbolExtensions /// /// A custom instance with fully qualified style, without global::. /// - public static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalFormat = new( - globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Omitted, - typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces, - genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters); + public static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalFormat = + SymbolDisplayFormat.FullyQualifiedFormat + .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted); /// /// A custom instance with fully qualified style, without global:: and parameters. @@ -30,6 +25,16 @@ internal static class ISymbolExtensions .WithMemberOptions(SymbolDisplayMemberOptions.IncludeContainingType) .WithParameterOptions(SymbolDisplayParameterOptions.None); + /// + /// Gets the fully qualified name for a given symbol. + /// + /// The input instance. + /// The fully qualified name for . + public static string GetFullyQualifiedName(this ISymbol symbol) + { + return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat); + } + /// /// Gets the full metadata name for a given instance. /// @@ -113,26 +118,6 @@ internal static class ISymbolExtensions return symbol.GetFullMetadataName().Replace('`', '-').Replace('+', '.'); } - /// - /// Tracks an instance and returns an HLSL compatible object. - /// - /// The input instance to process. - /// The collection of currently discovered types. - /// A instance that represents a type compatible with HLSL. - public static TypeSyntax TrackType(this ITypeSymbol typeSymbol, ICollection discoveredTypes) - { - string typeName = typeSymbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat); - - discoveredTypes.Add((INamedTypeSymbol)typeSymbol); - - if (HlslKnownTypes.TryGetMappedName(typeName, out string? mappedName)) - { - return ParseTypeName(mappedName!); - } - - return ParseTypeName(typeName.ToHlslIdentifierName()); - } - /// /// Tries to get an attribute with the specified full metadata name. /// diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownTypes.cs index 858c4c1a..60804779 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -199,6 +199,26 @@ internal static class HlslKnownTypes return KnownHlslTypes.TryGetValue(originalName, out mappedName); } + /// + /// Tracks an instance and returns an HLSL compatible type name. + /// + /// The input instance to process. + /// The collection of currently discovered types. + /// A type name that represents a type compatible with HLSL. + public static string TrackType(ITypeSymbol typeSymbol, ICollection discoveredTypes) + { + string typeName = typeSymbol.GetFullyQualifiedName(); + + discoveredTypes.Add((INamedTypeSymbol)typeSymbol); + + if (TryGetMappedName(typeName, out string? mappedName)) + { + return mappedName!; + } + + return typeName.ToHlslIdentifierName(); + } + /// /// Gets the sequence of unique custom types from a collection of discovered types. /// diff --git a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 5eafdbcf..89f17e1c 100644 --- a/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.D2D1Interop.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -318,7 +318,7 @@ internal sealed class ShaderSourceRewriter : HlslSourceRewriter // Track and rewrite the discarded declaration if (SemanticModel.For(node).GetOperation(node.Expression) is IDiscardOperation operation) { - TypeSyntax typeSyntax = operation.Type!.TrackType(DiscoveredTypes); + TypeSyntax typeSyntax = ParseTypeName(HlslKnownTypes.TrackType(operation.Type!, DiscoveredTypes)); string identifier = $"__implicit{this.implicitVariables.Count}"; // Add the variable to the list of implicit declarations diff --git a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs index 0974e9b6..7dcfd56f 100644 --- a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs @@ -1,10 +1,6 @@ -using System.Collections.Generic; -using System.Linq; +using System.Linq; using System.Text; -using ComputeSharp.SourceGenerators.Mappings; using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; namespace ComputeSharp.SourceGenerators.Extensions; @@ -16,10 +12,9 @@ internal static class ISymbolExtensions /// /// A custom instance with fully qualified style, without global::. /// - public static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalFormat = new( - globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Omitted, - typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces, - genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters); + public static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalFormat = + SymbolDisplayFormat.FullyQualifiedFormat + .WithGlobalNamespaceStyle(SymbolDisplayGlobalNamespaceStyle.Omitted); /// /// A custom instance with fully qualified style, without global:: and parameters. @@ -29,6 +24,16 @@ internal static class ISymbolExtensions .WithMemberOptions(SymbolDisplayMemberOptions.IncludeContainingType) .WithParameterOptions(SymbolDisplayParameterOptions.None); + /// + /// Gets the fully qualified name for a given symbol. + /// + /// The input instance. + /// The fully qualified name for . + public static string GetFullyQualifiedName(this ISymbol symbol) + { + return symbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat); + } + /// /// Gets the full metadata name for a given instance. /// @@ -111,24 +116,4 @@ internal static class ISymbolExtensions { return symbol.GetFullMetadataName().Replace('`', '-').Replace('+', '.'); } - - /// - /// Tracks an instance and returns an HLSL compatible object. - /// - /// The input instance to process. - /// The collection of currently discovered types. - /// A instance that represents a type compatible with HLSL. - public static TypeSyntax TrackType(this ITypeSymbol typeSymbol, ICollection discoveredTypes) - { - string typeName = typeSymbol.ToDisplayString(FullyQualifiedWithoutGlobalFormat); - - discoveredTypes.Add((INamedTypeSymbol)typeSymbol); - - if (HlslKnownTypes.TryGetMappedName(typeName, out string? mappedName)) - { - return ParseTypeName(mappedName!); - } - - return ParseTypeName(typeName.ToHlslIdentifierName()); - } } diff --git a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs index 2625d3f7..99dc3681 100644 --- a/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs +++ b/src/ComputeSharp.SourceGenerators/Mappings/HlslKnownTypes.cs @@ -365,6 +365,26 @@ internal static class HlslKnownTypes return KnownHlslTypes.TryGetValue(originalName, out mappedName); } + /// + /// Tracks an instance and returns an HLSL compatible type name. + /// + /// The input instance to process. + /// The collection of currently discovered types. + /// A type name that represents a type compatible with HLSL. + public static string TrackType(ITypeSymbol typeSymbol, ICollection discoveredTypes) + { + string typeName = typeSymbol.GetFullyQualifiedName(); + + discoveredTypes.Add((INamedTypeSymbol)typeSymbol); + + if (TryGetMappedName(typeName, out string? mappedName)) + { + return mappedName!; + } + + return typeName.ToHlslIdentifierName(); + } + /// /// Gets the sequence of unique custom types from a collection of discovered types. /// diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 0cf712f9..4bf37a62 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -429,7 +429,7 @@ internal sealed class ShaderSourceRewriter : HlslSourceRewriter // Track and rewrite the discarded declaration if (SemanticModel.For(node).GetOperation(node.Expression) is IDiscardOperation operation) { - TypeSyntax typeSyntax = operation.Type!.TrackType(DiscoveredTypes); + TypeSyntax typeSyntax = ParseTypeName(HlslKnownTypes.TrackType(operation.Type!, DiscoveredTypes)); string identifier = $"__implicit{this.implicitVariables.Count}"; // Add the variable to the list of implicit declarations