Skip to content

Commit 99b5691

Browse files
committed
review: rebase onto lexer api changes
- we have changed the api of the lexer to avoid pre-allocating all the tokens and instead to have the parser only invoke ConsumeToken/PeekNextToken when needed - the end of stream diagnostic is also moved to the lexer
1 parent 99f9afd commit 99b5691

File tree

4 files changed

+66
-132
lines changed

4 files changed

+66
-132
lines changed

clang/include/clang/Basic/DiagnosticParseKinds.td

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1807,7 +1807,6 @@ def err_hlsl_unsupported_component : Error<"invalid component '%0' used; expecte
18071807
def err_hlsl_packoffset_invalid_reg : Error<"invalid resource class specifier '%0' for packoffset, expected 'c'">;
18081808

18091809
// HLSL Root Signature Parser Diagnostics
1810-
def err_hlsl_rootsig_unexpected_eos : Error<"unexpected end to token stream">;
18111810
def err_hlsl_rootsig_unexpected_token_kind : Error<"expected the %select{following|one of the following}0 token kinds '%1'">;
18121811
def err_hlsl_rootsig_repeat_param : Error<"specified the same parameter '%0' multiple times">;
18131812
def err_hlsl_rootsig_non_zero_flag : Error<"specified a non-zero integer as a flag">;

clang/include/clang/Parse/ParseHLSLRootSignature.h

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ class RootSignatureLexer {
9999
class RootSignatureParser {
100100
public:
101101
RootSignatureParser(SmallVector<llvm::hlsl::rootsig::RootElement> &Elements,
102-
const SmallVector<RootSignatureToken> &Tokens,
103-
DiagnosticsEngine &Diags);
102+
RootSignatureLexer &Lexer, DiagnosticsEngine &Diags);
104103

105104
/// Iterates over the provided tokens and constructs the in-memory
106105
/// representations of the RootElements.
@@ -112,7 +111,7 @@ class RootSignatureParser {
112111

113112
private:
114113
// Root Element helpers
115-
bool ParseRootElement(bool First);
114+
bool ParseRootElement();
116115
bool ParseDescriptorTable();
117116
bool ParseDescriptorTableClause();
118117

@@ -147,9 +146,17 @@ class RootSignatureParser {
147146
ParseDescriptorRangeFlags(llvm::hlsl::rootsig::DescriptorRangeFlags *Enum);
148147
bool ParseShaderVisibility(llvm::hlsl::rootsig::ShaderVisibility *Enum);
149148

150-
/// Increment the token iterator if we have not reached the end.
149+
/// Invoke the lexer to consume a token and update CurToken with the result
150+
///
151151
/// Return value denotes if we were already at the last token.
152-
bool ConsumeNextToken();
152+
///
153+
/// This is used to avoid having to constantly access the Lexer's CurToken
154+
bool ConsumeNextToken() {
155+
if (Lexer.ConsumeToken())
156+
return true; // Report lexer err
157+
CurToken = Lexer.GetCurToken();
158+
return false;
159+
}
153160

154161
/// Attempt to retrieve the next token, if TokenKind is invalid then there was
155162
/// no next token.
@@ -185,10 +192,10 @@ class RootSignatureParser {
185192

186193
private:
187194
SmallVector<llvm::hlsl::rootsig::RootElement> &Elements;
188-
SmallVector<RootSignatureToken>::const_iterator CurTok;
189-
SmallVector<RootSignatureToken>::const_iterator LastTok;
190-
195+
RootSignatureLexer &Lexer;
191196
DiagnosticsEngine &Diags;
197+
198+
RootSignatureToken CurToken;
192199
};
193200

194201
} // namespace hlsl

clang/lib/Parse/ParseHLSLRootSignature.cpp

Lines changed: 33 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ static std::string FormatTokenKinds(ArrayRef<TokenKind> Kinds) {
1818
Out << ", ";
1919
switch (Kind) {
2020
case TokenKind::invalid:
21+
Out << "uninitialized";
22+
break;
23+
case TokenKind::end_of_stream:
2124
break;
2225
case TokenKind::int_literal:
2326
Out << "integer literal";
@@ -215,42 +218,33 @@ std::optional<RootSignatureToken> RootSignatureLexer::PeekNextToken() {
215218

216219
// Parser Definitions
217220

218-
RootSignatureParser::RootSignatureParser(
219-
SmallVector<RootElement> &Elements,
220-
const SmallVector<RootSignatureToken> &Tokens, DiagnosticsEngine &Diags)
221-
: Elements(Elements), Diags(Diags) {
222-
CurTok = Tokens.begin();
223-
LastTok = Tokens.end();
224-
}
221+
RootSignatureParser::RootSignatureParser(SmallVector<RootElement> &Elements,
222+
RootSignatureLexer &Lexer,
223+
DiagnosticsEngine &Diags)
224+
: Elements(Elements), Lexer(Lexer), Diags(Diags) {}
225225

226226
bool RootSignatureParser::Parse() {
227227
// Handle edge-case of empty RootSignature()
228-
if (CurTok == LastTok)
228+
if (Lexer.EndOfBuffer())
229229
return false;
230230

231-
bool First = true;
232231
// Iterate as many RootElements as possible
233-
while (!ParseRootElement(First)) {
234-
First = false;
235-
// Avoid use of ConsumeNextToken here to skip incorrect end of tokens error
236-
CurTok++;
237-
if (CurTok == LastTok)
232+
while (!ParseRootElement()) {
233+
if (Lexer.EndOfBuffer())
238234
return false;
239-
if (EnsureExpectedToken(TokenKind::pu_comma))
235+
if (ConsumeExpectedToken(TokenKind::pu_comma))
240236
return true;
241237
}
242238

243239
return true;
244240
}
245241

246-
bool RootSignatureParser::ParseRootElement(bool First) {
247-
if (First && EnsureExpectedToken(TokenKind::kw_DescriptorTable))
248-
return true;
249-
if (!First && ConsumeExpectedToken(TokenKind::kw_DescriptorTable))
242+
bool RootSignatureParser::ParseRootElement() {
243+
if (ConsumeExpectedToken(TokenKind::kw_DescriptorTable))
250244
return true;
251245

252246
// Dispatch onto the correct parse method
253-
switch (CurTok->Kind) {
247+
switch (CurToken.Kind) {
254248
case TokenKind::kw_DescriptorTable:
255249
return ParseDescriptorTable();
256250
default:
@@ -277,8 +271,8 @@ bool RootSignatureParser::ParseDescriptorTable() {
277271
// Handle the visibility parameter
278272
if (!TryConsumeExpectedToken(TokenKind::kw_visibility)) {
279273
if (SeenVisibility) {
280-
Diags.Report(CurTok->TokLoc, diag::err_hlsl_rootsig_repeat_param)
281-
<< FormatTokenKinds(CurTok->Kind);
274+
Diags.Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
275+
<< FormatTokenKinds(CurToken.Kind);
282276
return true;
283277
}
284278
SeenVisibility = true;
@@ -306,7 +300,7 @@ bool RootSignatureParser::ParseDescriptorTableClause() {
306300
return true;
307301

308302
DescriptorTableClause Clause;
309-
switch (CurTok->Kind) {
303+
switch (CurToken.Kind) {
310304
case TokenKind::kw_CBV:
311305
Clause.Type = ClauseType::CBuffer;
312306
break;
@@ -391,9 +385,9 @@ bool RootSignatureParser::ParseOptionalParams(
391385
if (ConsumeExpectedToken(ParamKeywords))
392386
return true;
393387

394-
TokenKind ParamKind = CurTok->Kind;
388+
TokenKind ParamKind = CurToken.Kind;
395389
if (Seen.contains(ParamKind)) {
396-
Diags.Report(CurTok->TokLoc, diag::err_hlsl_rootsig_repeat_param)
390+
Diags.Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
397391
<< FormatTokenKinds(ParamKind);
398392
return true;
399393
}
@@ -412,25 +406,25 @@ bool RootSignatureParser::ParseDescriptorRangeOffset(DescriptorRangeOffset *X) {
412406
return true;
413407

414408
// Edge case for the offset enum -> static value
415-
if (CurTok->Kind == TokenKind::en_DescriptorRangeOffsetAppend) {
409+
if (CurToken.Kind == TokenKind::en_DescriptorRangeOffsetAppend) {
416410
*X = DescriptorTableOffsetAppend;
417411
return false;
418412
}
419413

420-
*X = DescriptorRangeOffset(CurTok->NumLiteral.getInt().getExtValue());
414+
*X = DescriptorRangeOffset(CurToken.NumLiteral.getInt().getExtValue());
421415
return false;
422416
}
423417

424418
bool RootSignatureParser::ParseUInt(uint32_t *X) {
425419
if (ConsumeExpectedToken(TokenKind::int_literal))
426420
return true;
427421

428-
*X = CurTok->NumLiteral.getInt().getExtValue();
422+
*X = CurToken.NumLiteral.getInt().getExtValue();
429423
return false;
430424
}
431425

432426
bool RootSignatureParser::ParseRegister(Register *Register) {
433-
switch (CurTok->Kind) {
427+
switch (CurToken.Kind) {
434428
case TokenKind::bReg:
435429
Register->ViewType = RegisterType::BReg;
436430
break;
@@ -447,7 +441,7 @@ bool RootSignatureParser::ParseRegister(Register *Register) {
447441
llvm_unreachable("Switch for an expected token was not provided");
448442
}
449443

450-
Register->Number = CurTok->NumLiteral.getInt().getExtValue();
444+
Register->Number = CurToken.NumLiteral.getInt().getExtValue();
451445

452446
return false;
453447
}
@@ -466,9 +460,9 @@ bool RootSignatureParser::ParseEnum(
466460
return true;
467461

468462
// Handle the edge case when '0' is used to specify None
469-
if (CurTok->Kind == TokenKind::int_literal) {
470-
if (CurTok->NumLiteral.getInt() != 0) {
471-
Diags.Report(CurTok->TokLoc, diag::err_hlsl_rootsig_non_zero_flag);
463+
if (CurToken.Kind == TokenKind::int_literal) {
464+
if (CurToken.NumLiteral.getInt() != 0) {
465+
Diags.Report(CurToken.TokLoc, diag::err_hlsl_rootsig_non_zero_flag);
472466
return true;
473467
}
474468
// Set enum to None equivalent
@@ -478,7 +472,7 @@ bool RootSignatureParser::ParseEnum(
478472

479473
// Effectively a switch statement on the token kinds
480474
for (auto EnumPair : EnumMap)
481-
if (CurTok->Kind == EnumPair.first) {
475+
if (CurToken.Kind == EnumPair.first) {
482476
*Enum = EnumPair.second;
483477
return false;
484478
}
@@ -528,25 +522,6 @@ bool RootSignatureParser::ParseShaderVisibility(ShaderVisibility *Enum) {
528522
return ParseEnum(EnumMap, Enum);
529523
}
530524

531-
RootSignatureToken RootSignatureParser::PeekNextToken() {
532-
// Create an invalid token
533-
RootSignatureToken Token = RootSignatureToken(SourceLocation());
534-
if (CurTok != LastTok)
535-
Token = *(CurTok + 1);
536-
return Token;
537-
}
538-
539-
bool RootSignatureParser::ConsumeNextToken() {
540-
SourceLocation EndLoc = CurTok->TokLoc;
541-
CurTok++;
542-
if (LastTok == CurTok) {
543-
// Report unexpected end of tokens error
544-
Diags.Report(EndLoc, diag::err_hlsl_rootsig_unexpected_eos);
545-
return true;
546-
}
547-
return false;
548-
}
549-
550525
// Is given token one of the expected kinds
551526
static bool IsExpectedToken(TokenKind Kind, ArrayRef<TokenKind> AnyExpected) {
552527
for (auto Expected : AnyExpected)
@@ -560,11 +535,11 @@ bool RootSignatureParser::EnsureExpectedToken(TokenKind Expected) {
560535
}
561536

562537
bool RootSignatureParser::EnsureExpectedToken(ArrayRef<TokenKind> AnyExpected) {
563-
if (IsExpectedToken(CurTok->Kind, AnyExpected))
538+
if (IsExpectedToken(CurToken.Kind, AnyExpected))
564539
return false;
565540

566541
// Report unexpected token kind error
567-
Diags.Report(CurTok->TokLoc, diag::err_hlsl_rootsig_unexpected_token_kind)
542+
Diags.Report(CurToken.TokLoc, diag::err_hlsl_rootsig_unexpected_token_kind)
568543
<< (unsigned)(AnyExpected.size() != 1) << FormatTokenKinds(AnyExpected);
569544
return true;
570545
}
@@ -574,10 +549,10 @@ bool RootSignatureParser::PeekExpectedToken(TokenKind Expected) {
574549
}
575550

576551
bool RootSignatureParser::PeekExpectedToken(ArrayRef<TokenKind> AnyExpected) {
577-
RootSignatureToken Token = PeekNextToken();
578-
if (Token.Kind == TokenKind::invalid)
552+
auto Result = Lexer.PeekNextToken();
553+
if (!Result)
579554
return true;
580-
if (IsExpectedToken(Token.Kind, AnyExpected))
555+
if (IsExpectedToken(Result->Kind, AnyExpected))
581556
return false;
582557
return true;
583558
}

0 commit comments

Comments
 (0)