Skip to content

Commit 47139b6

Browse files
author
Finn Plummer
committed
NFC review: switch calling convention to use std::optional instead of bool
1 parent 5d1ec2f commit 47139b6

File tree

2 files changed

+86
-85
lines changed

2 files changed

+86
-85
lines changed

clang/include/clang/Parse/ParseHLSLRootSignature.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,26 @@ class RootSignatureParser {
6666
// expected, or, there is a lexing error
6767

6868
/// Root Element parse methods:
69-
bool parseDescriptorTable();
70-
bool parseDescriptorTableClause();
69+
std::optional<llvm::hlsl::rootsig::DescriptorTable> parseDescriptorTable();
70+
std::optional<llvm::hlsl::rootsig::DescriptorTableClause>
71+
parseDescriptorTableClause();
7172

7273
struct ParsedParams {
7374
std::optional<llvm::hlsl::rootsig::Register> Register;
7475
std::optional<uint32_t> Space;
7576
};
7677
/// Parses out a `ParsedParams` for the caller to use for construction
7778
/// of the in-memory representation of a Root Element.
78-
bool parseDescriptorTableClauseParams(ParsedParams &Params, RootSignatureToken::Kind RegType);
79+
std::optional<ParsedParams>
80+
parseDescriptorTableClauseParams(RootSignatureToken::Kind RegType);
7981

8082
/// Parameter parse methods corresponding to a ParamType
81-
bool parseUIntParam(uint32_t &X);
82-
bool parseRegister(llvm::hlsl::rootsig::Register &Reg);
83+
std::optional<uint32_t> parseUIntParam();
84+
std::optional<llvm::hlsl::rootsig::Register> parseRegister();
8385

8486
/// Use NumericLiteralParser to convert CurToken.NumSpelling into a unsigned
8587
/// 32-bit integer
86-
bool handleUIntLiteral(uint32_t &X);
88+
std::optional<uint32_t> handleUIntLiteral();
8789

8890
/// Invoke the Lexer to consume a token and update CurToken with the result
8991
void consumeNextToken() { CurToken = Lexer.ConsumeToken(); }

clang/lib/Parse/ParseHLSLRootSignature.cpp

Lines changed: 78 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,14 @@ RootSignatureParser::RootSignatureParser(SmallVector<RootElement> &Elements,
2626

2727
bool RootSignatureParser::parse() {
2828
// Iterate as many RootElements as possible
29-
while (tryConsumeExpectedToken(TokenKind::kw_DescriptorTable)) {
30-
// Dispatch onto parser method.
31-
// We guard against the unreachable here as we just ensured that CurToken
32-
// will be one of the kinds in the while condition
33-
switch (CurToken.TokKind) {
34-
case TokenKind::kw_DescriptorTable:
35-
if (parseDescriptorTable())
29+
do {
30+
if (tryConsumeExpectedToken(TokenKind::kw_DescriptorTable)) {
31+
auto Table = parseDescriptorTable();
32+
if (!Table.has_value())
3633
return true;
37-
break;
38-
default:
39-
llvm_unreachable("Switch for consumed token was not provided");
34+
Elements.push_back(*Table);
4035
}
41-
42-
if (!tryConsumeExpectedToken(TokenKind::pu_comma))
43-
break;
44-
}
36+
} while (tryConsumeExpectedToken(TokenKind::pu_comma));
4537

4638
if (consumeExpectedToken(TokenKind::end_of_stream,
4739
diag::err_hlsl_unexpected_end_of_params,
@@ -51,147 +43,153 @@ bool RootSignatureParser::parse() {
5143
return false;
5244
}
5345

54-
bool RootSignatureParser::parseDescriptorTable() {
46+
std::optional<DescriptorTable> RootSignatureParser::parseDescriptorTable() {
5547
assert(CurToken.TokKind == TokenKind::kw_DescriptorTable &&
5648
"Expects to only be invoked starting at given keyword");
5749

58-
DescriptorTable Table;
59-
6050
if (consumeExpectedToken(TokenKind::pu_l_paren, diag::err_expected_after,
6151
CurToken.TokKind))
62-
return true;
52+
return std::nullopt;
6353

64-
// Iterate as many Clauses as possible
65-
while (tryConsumeExpectedToken({TokenKind::kw_CBV, TokenKind::kw_SRV,
66-
TokenKind::kw_UAV, TokenKind::kw_Sampler})) {
67-
if (parseDescriptorTableClause())
68-
return true;
69-
70-
Table.NumClauses++;
54+
DescriptorTable Table;
7155

72-
if (!tryConsumeExpectedToken(TokenKind::pu_comma))
73-
break;
74-
}
56+
// Iterate as many Clauses as possible
57+
do {
58+
if (tryConsumeExpectedToken({TokenKind::kw_CBV, TokenKind::kw_SRV,
59+
TokenKind::kw_UAV, TokenKind::kw_Sampler})) {
60+
auto Clause = parseDescriptorTableClause();
61+
if (!Clause.has_value())
62+
return std::nullopt;
63+
Elements.push_back(*Clause);
64+
Table.NumClauses++;
65+
}
66+
} while (tryConsumeExpectedToken(TokenKind::pu_comma));
7567

7668
if (consumeExpectedToken(TokenKind::pu_r_paren,
7769
diag::err_hlsl_unexpected_end_of_params,
7870
/*param of=*/TokenKind::kw_DescriptorTable))
79-
return true;
71+
return std::nullopt;
8072

81-
Elements.push_back(Table);
82-
return false;
73+
return Table;
8374
}
8475

85-
bool RootSignatureParser::parseDescriptorTableClause() {
76+
std::optional<DescriptorTableClause>
77+
RootSignatureParser::parseDescriptorTableClause() {
8678
assert((CurToken.TokKind == TokenKind::kw_CBV ||
8779
CurToken.TokKind == TokenKind::kw_SRV ||
8880
CurToken.TokKind == TokenKind::kw_UAV ||
8981
CurToken.TokKind == TokenKind::kw_Sampler) &&
9082
"Expects to only be invoked starting at given keyword");
91-
TokenKind ParamKind = CurToken.TokKind; // retain for diagnostics
83+
84+
TokenKind ParamKind = CurToken.TokKind;
85+
86+
if (consumeExpectedToken(TokenKind::pu_l_paren, diag::err_expected_after,
87+
CurToken.TokKind))
88+
return std::nullopt;
9289

9390
DescriptorTableClause Clause;
94-
TokenKind ExpectedRegister;
91+
TokenKind ExpectedReg;
9592
switch (ParamKind) {
9693
default:
9794
llvm_unreachable("Switch for consumed token was not provided");
9895
case TokenKind::kw_CBV:
9996
Clause.Type = ClauseType::CBuffer;
100-
ExpectedRegister = TokenKind::bReg;
97+
ExpectedReg = TokenKind::bReg;
10198
break;
10299
case TokenKind::kw_SRV:
103100
Clause.Type = ClauseType::SRV;
104-
ExpectedRegister = TokenKind::tReg;
101+
ExpectedReg = TokenKind::tReg;
105102
break;
106103
case TokenKind::kw_UAV:
107104
Clause.Type = ClauseType::UAV;
108-
ExpectedRegister = TokenKind::uReg;
105+
ExpectedReg = TokenKind::uReg;
109106
break;
110107
case TokenKind::kw_Sampler:
111108
Clause.Type = ClauseType::Sampler;
112-
ExpectedRegister = TokenKind::sReg;
109+
ExpectedReg = TokenKind::sReg;
113110
break;
114111
}
115112

116-
if (consumeExpectedToken(TokenKind::pu_l_paren, diag::err_expected_after,
117-
ParamKind))
118-
return true;
119-
120-
ParsedParams Result;
121-
if (parseDescriptorTableClauseParams(Result, ExpectedRegister))
122-
return true;
113+
auto Params = parseDescriptorTableClauseParams(ExpectedReg);
114+
if (!Params.has_value())
115+
return std::nullopt;
123116

124117
// Check mandatory parameters were provided
125-
if (!Result.Register.has_value()) {
118+
if (!Params->Register.has_value()) {
126119
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_missing_param)
127-
<< ExpectedRegister;
128-
return true;
120+
<< ExpectedReg;
121+
return std::nullopt;
129122
}
130123

131-
Clause.Register = *Result.Register;
124+
Clause.Register = Params->Register.value();
132125

133-
if (Result.Space)
134-
Clause.Space = *Result.Space;
126+
// Fill in optional values
127+
if (Params->Space.has_value())
128+
Clause.Space = Params->Space.value();
135129

136130
if (consumeExpectedToken(TokenKind::pu_r_paren,
137131
diag::err_hlsl_unexpected_end_of_params,
138132
/*param of=*/ParamKind))
139-
return true;
133+
return std::nullopt;
140134

141-
Elements.push_back(Clause);
142-
return false;
135+
return Clause;
143136
}
144137

145-
bool RootSignatureParser::parseDescriptorTableClauseParams(ParsedParams &Params, TokenKind RegType) {
138+
std::optional<RootSignatureParser::ParsedParams>
139+
RootSignatureParser::parseDescriptorTableClauseParams(TokenKind RegType) {
146140
assert(CurToken.TokKind == TokenKind::pu_l_paren &&
147141
"Expects to only be invoked starting at given token");
148142

143+
ParsedParams Params;
149144
do {
150145
if (tryConsumeExpectedToken(RegType)) {
151146
if (Params.Register.has_value()) {
152147
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
153-
<< CurToken.TokKind;
154-
return true;
148+
<< CurToken.TokKind;
149+
return std::nullopt;
155150
}
156-
Register Reg;
157-
if (parseRegister(Reg))
158-
return true;
151+
auto Reg = parseRegister();
152+
if (!Reg.has_value())
153+
return std::nullopt;
159154
Params.Register = Reg;
160155
}
156+
161157
if (tryConsumeExpectedToken(TokenKind::kw_space)) {
162158
if (Params.Space.has_value()) {
163159
getDiags().Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
164-
<< CurToken.TokKind;
165-
return true;
160+
<< CurToken.TokKind;
161+
return std::nullopt;
166162
}
167163
if (consumeExpectedToken(TokenKind::pu_equal))
168-
return true;
169-
uint32_t Space;
170-
if (parseUIntParam(Space))
171-
return true;
164+
return std::nullopt;
165+
auto Space = parseUIntParam();
166+
if (!Space.has_value())
167+
return std::nullopt;
172168
Params.Space = Space;
173169
}
174170
} while (tryConsumeExpectedToken(TokenKind::pu_comma));
175171

176-
return false;
172+
return Params;
177173
}
178174

179-
bool RootSignatureParser::parseUIntParam(uint32_t &X) {
175+
std::optional<uint32_t> RootSignatureParser::parseUIntParam() {
180176
assert(CurToken.TokKind == TokenKind::pu_equal &&
181177
"Expects to only be invoked starting at given keyword");
182178
tryConsumeExpectedToken(TokenKind::pu_plus);
183-
return consumeExpectedToken(TokenKind::int_literal, diag::err_expected_after,
184-
CurToken.TokKind) ||
185-
handleUIntLiteral(X);
179+
if (consumeExpectedToken(TokenKind::int_literal, diag::err_expected_after,
180+
CurToken.TokKind))
181+
return std::nullopt;
182+
return handleUIntLiteral();
186183
}
187184

188-
bool RootSignatureParser::parseRegister(Register &Register) {
185+
std::optional<Register> RootSignatureParser::parseRegister() {
189186
assert((CurToken.TokKind == TokenKind::bReg ||
190187
CurToken.TokKind == TokenKind::tReg ||
191188
CurToken.TokKind == TokenKind::uReg ||
192189
CurToken.TokKind == TokenKind::sReg) &&
193190
"Expects to only be invoked starting at given keyword");
194191

192+
Register Register;
195193
switch (CurToken.TokKind) {
196194
default:
197195
llvm_unreachable("Switch for consumed token was not provided");
@@ -209,13 +207,15 @@ bool RootSignatureParser::parseRegister(Register &Register) {
209207
break;
210208
}
211209

212-
if (handleUIntLiteral(Register.Number))
213-
return true; // propogate NumericLiteralParser error
210+
auto Number = handleUIntLiteral();
211+
if (!Number.has_value())
212+
return std::nullopt; // propogate NumericLiteralParser error
214213

215-
return false;
214+
Register.Number = *Number;
215+
return Register;
216216
}
217217

218-
bool RootSignatureParser::handleUIntLiteral(uint32_t &X) {
218+
std::optional<uint32_t> RootSignatureParser::handleUIntLiteral() {
219219
// Parse the numeric value and do semantic checks on its specification
220220
clang::NumericLiteralParser Literal(CurToken.NumSpelling, CurToken.TokLoc,
221221
PP.getSourceManager(), PP.getLangOpts(),
@@ -231,11 +231,10 @@ bool RootSignatureParser::handleUIntLiteral(uint32_t &X) {
231231
PP.getDiagnostics().Report(CurToken.TokLoc,
232232
diag::err_hlsl_number_literal_overflow)
233233
<< 0 << CurToken.NumSpelling;
234-
return true;
234+
return std::nullopt;
235235
}
236236

237-
X = Val.getExtValue();
238-
return false;
237+
return Val.getExtValue();
239238
}
240239

241240
bool RootSignatureParser::peekExpectedToken(TokenKind Expected) {

0 commit comments

Comments
 (0)