From 94b40e5d6166cddcb71ce673de55fbebcee05638 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Thu, 2 Jun 2022 22:47:36 +0200 Subject: [PATCH] Handle keywords in field names for custom types --- ...1ShaderGenerator.CreateBuildHlslSourceMethod.cs | 14 ++++++++++---- ...IShaderGenerator.CreateBuildHlslSourceMethod.cs | 14 ++++++++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs index af38826c..a895ec4c 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/ID2D1ShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -337,15 +337,21 @@ partial class ID2D1ShaderGenerator INamedTypeSymbol fieldType = (INamedTypeSymbol)field.Type; // Convert the name to the fully qualified HLSL version - if (!HlslKnownTypes.TryGetMappedName(fieldType.GetFullMetadataName(), out string? mapped)) + if (!HlslKnownTypes.TryGetMappedName(fieldType.GetFullMetadataName(), out string? mappedType)) { - mapped = fieldType.GetFullMetadataName().ToHlslIdentifierName(); + mappedType = fieldType.GetFullMetadataName().ToHlslIdentifierName(); + } + + // Get the field name as a valid HLSL identifier + if (!HlslKnownKeywords.TryGetMappedName(field.Name, out string? mappedName)) + { + mappedName = field.Name; } structDeclaration = structDeclaration.AddMembers( FieldDeclaration(VariableDeclaration( - IdentifierName(mapped!)).AddVariables( - VariableDeclarator(Identifier(field.Name))))); + IdentifierName(mappedType!)).AddVariables( + VariableDeclarator(Identifier(mappedName!))))); } // Insert the trailing ; right after the closing bracket (after normalization) diff --git a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs index 7bf7efdf..990a6aae 100644 --- a/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs +++ b/src/ComputeSharp.SourceGenerators/IShaderGenerator.CreateBuildHlslSourceMethod.cs @@ -465,15 +465,21 @@ partial class IShaderGenerator INamedTypeSymbol fieldType = (INamedTypeSymbol)field.Type; // Convert the name to the fully qualified HLSL version - if (!HlslKnownTypes.TryGetMappedName(fieldType.GetFullMetadataName(), out string? mapped)) + if (!HlslKnownTypes.TryGetMappedName(fieldType.GetFullMetadataName(), out string? mappedType)) { - mapped = fieldType.GetFullMetadataName().ToHlslIdentifierName(); + mappedType = fieldType.GetFullMetadataName().ToHlslIdentifierName(); + } + + // Get the field name as a valid HLSL identifier + if (!HlslKnownKeywords.TryGetMappedName(field.Name, out string? mappedName)) + { + mappedName = field.Name; } structDeclaration = structDeclaration.AddMembers( FieldDeclaration(VariableDeclaration( - IdentifierName(mapped!)).AddVariables( - VariableDeclarator(Identifier(field.Name))))); + IdentifierName(mappedType!)).AddVariables( + VariableDeclarator(Identifier(mappedName!))))); } // Insert the trailing ; right after the closing bracket (after normalization)