From fe5eef423389b82e7eb2586fb7a16b96afd004f2 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 9 Oct 2017 10:16:12 -0700 Subject: Parser: fix precedence of cast expressions (#203) We were accidentally parsing this: (uint) a / b as this: (uint) ( a / b ) when it should be: ((uint) a) / b This is a bug that seems to have been inherited from a long time ago. It has taken a while to bite anybody because the only class of expressions it would hit are multiplicative ones, and in many cases the difference in the cast order won't be noticed for values in a limited range. --- source/slang/parser.cpp | 4 +++- tests/parser/cast-precedence.hlsl | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 tests/parser/cast-precedence.hlsl diff --git a/source/slang/parser.cpp b/source/slang/parser.cpp index 38d9c744d..a0e07ea12 100644 --- a/source/slang/parser.cpp +++ b/source/slang/parser.cpp @@ -3321,6 +3321,8 @@ namespace Slang return parseGenericApp(parser, base); } + static RefPtr parsePrefixExpr(Parser* parser); + static RefPtr parseAtomicExpr(Parser* parser) { switch( peekTokenType(parser) ) @@ -3348,7 +3350,7 @@ namespace Slang tcexpr->FunctionExpr = parser->ParseType(); parser->ReadToken(TokenType::RParent); - auto arg = parser->ParseExpression(Precedence::Multiplicative); // Note(tfoley): need to double-check this + auto arg = parsePrefixExpr(parser); tcexpr->Arguments.Add(arg); return tcexpr; diff --git a/tests/parser/cast-precedence.hlsl b/tests/parser/cast-precedence.hlsl new file mode 100644 index 000000000..d5d0b0322 --- /dev/null +++ b/tests/parser/cast-precedence.hlsl @@ -0,0 +1,15 @@ +//TEST:COMPARE_HLSL: -profile vs_5_0 + +// Confirm that type-cast expressions parse with +// the appropriate precedence. + +cbuffer C : register(b0) +{ + float a; + float b; +}; + +float4 main() : SV_Position +{ + return (uint) a / b; +} -- cgit v1.2.3