From 00f1b8d91129562a3e86164d9250d5a320fc353d Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Wed, 2 Dec 2020 14:49:12 +0100 Subject: [PATCH] Added support for out discard expressions --- .../Extensions/ISymbolExtensions.cs | 27 ++++++++++++++++++- .../Extensions/SyntaxNodeExtensions.cs | 2 +- .../SyntaxRewriters/ShaderSourceRewriter.cs | 16 ++++++++++- 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs index 392a250c..a990d501 100644 --- a/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs +++ b/src/ComputeSharp.SourceGenerators/Extensions/ISymbolExtensions.cs @@ -1,7 +1,11 @@ -using System.Diagnostics.Contracts; +using System.Collections.Generic; +using System.Diagnostics.Contracts; 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 { @@ -95,5 +99,26 @@ namespace ComputeSharp.SourceGenerators.Extensions return $"[{nameof(ComputeSharp)}]_[{typeof(TGenerator).Name}]_[{fixedName}]"; } + + /// + /// 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. + [Pure] + public static TypeSyntax TrackType(this ITypeSymbol typeSymbol, ICollection discoveredTypes) + { + string typeName = typeSymbol.ToDisplayString(SyntaxNodeExtensions.FullyQualifiedWithoutGlobalFormat); + + discoveredTypes.Add((INamedTypeSymbol)typeSymbol); + + if (HlslKnownTypes.TryGetMappedName(typeName, out string? mappedName)) + { + return ParseTypeName(mappedName!); + } + + return ParseTypeName(typeName.Replace(".", "__")); + } } } diff --git a/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs b/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs index 78307cef..73bf2685 100644 --- a/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs +++ b/src/ComputeSharp.SourceGenerators/Extensions/SyntaxNodeExtensions.cs @@ -17,7 +17,7 @@ namespace ComputeSharp.SourceGenerators.Extensions /// /// A custom instance with fully qualified style, without global::. /// - private static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalFormat = new( + internal static readonly SymbolDisplayFormat FullyQualifiedWithoutGlobalFormat = new( globalNamespaceStyle: SymbolDisplayGlobalNamespaceStyle.Omitted, typeQualificationStyle: SymbolDisplayTypeQualificationStyle.NameAndContainingTypesAndNamespaces, genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters); diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs index 1b0e611a..9503cfa2 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/ShaderSourceRewriter.cs @@ -348,7 +348,21 @@ namespace ComputeSharp.SourceGenerators.SyntaxRewriters var updatedNode = (ArgumentSyntax)base.VisitArgument(node)!; // Strip the ref keywords from arguments - return updatedNode.WithRefKindKeyword(Token(SyntaxKind.None)); + updatedNode = updatedNode.WithRefKindKeyword(Token(SyntaxKind.None)); + + // Track and rewrite the discarded declaration + if (this.semanticModel.GetOperation(node.Expression) is IDiscardOperation operation) + { + TypeSyntax typeSyntax = operation.Type.TrackType(this.discoveredTypes); + string identifier = $"__implicit{this.implicitVariables.Count}"; + + // Add the variable to the list of implicit declarations + this.implicitVariables.Add(VariableDeclaration(typeSyntax).AddVariables(VariableDeclarator(identifier))); + + return updatedNode.WithExpression(IdentifierName(identifier)); + } + + return updatedNode; } ///