From f2c9478631e83b55c39e4c8de1d019afa6b392d6 Mon Sep 17 00:00:00 2001 From: Sergio Pedri Date: Sun, 29 May 2022 17:19:52 +0200 Subject: [PATCH] Fix rewriting of integer double literals --- .../SyntaxRewriters/HlslSourceRewriter.cs | 19 ++++++++++++++++--- .../SyntaxRewriters/HlslSourceRewriter.cs | 19 ++++++++++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs index 83e1cecd..0e43e9f7 100644 --- a/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.D2D1.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs @@ -190,7 +190,7 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter // used in the HLSL representation. Floating point values accept either f or F, but they don't work // when the literal doesn't contain a decimal point. Since 32 bit floating point values are the default // in HLSL, we can remove the suffix entirely. As for 64 bit values, we simply use the 'L' suffix. - if (type.GetFullMetadataName().Equals(typeof(float).FullName)) + if (type.SpecialType == SpecialType.System_Single) { string literal = updatedNode.Token.ValueText; @@ -202,9 +202,22 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter return updatedNode.WithToken(Literal(literal, 0f)); } - else if (type.GetFullMetadataName().Equals(typeof(double).FullName)) + else if (type.SpecialType == SpecialType.System_Double) { - return updatedNode.WithToken(Literal(updatedNode.Token.ValueText + "L", 0d)); + string literal = updatedNode.Token.ValueText; + + // If the numeric literal is neither a decimal nor an exponential, add the ".0L" suffix. + // This is necessary because otherwise integer literals would actually be of type long. + if (literal.IndexOfAny(new[] { '.', 'E' }) == -1) + { + literal += ".0L"; + } + else + { + literal += "L"; + } + + return updatedNode.WithToken(Literal(literal, 0d)); } } else if (updatedNode.IsKind(SyntaxKind.StringLiteralExpression)) diff --git a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs index b34408c7..757b8f33 100644 --- a/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs +++ b/src/ComputeSharp.SourceGenerators/SyntaxRewriters/HlslSourceRewriter.cs @@ -195,7 +195,7 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter // used in the HLSL representation. Floating point values accept either f or F, but they don't work // when the literal doesn't contain a decimal point. Since 32 bit floating point values are the default // in HLSL, we can remove the suffix entirely. As for 64 bit values, we simply use the 'L' suffix. - if (type.GetFullMetadataName().Equals(typeof(float).FullName)) + if (type.SpecialType == SpecialType.System_Single) { string literal = updatedNode.Token.ValueText; @@ -207,9 +207,22 @@ internal abstract partial class HlslSourceRewriter : CSharpSyntaxRewriter return updatedNode.WithToken(Literal(literal, 0f)); } - else if (type.GetFullMetadataName().Equals(typeof(double).FullName)) + else if (type.SpecialType == SpecialType.System_Double) { - return updatedNode.WithToken(Literal(updatedNode.Token.ValueText + "L", 0d)); + string literal = updatedNode.Token.ValueText; + + // If the numeric literal is neither a decimal nor an exponential, add the ".0L" suffix. + // This is necessary because otherwise integer literals would actually be of type long. + if (literal.IndexOfAny(new[] { '.', 'E' }) == -1) + { + literal += ".0L"; + } + else + { + literal += "L"; + } + + return updatedNode.WithToken(Literal(literal, 0d)); } } else if (updatedNode.IsKind(SyntaxKind.StringLiteralExpression))