Skip to content

Commit 7e2fe68

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 96772a4 commit 7e2fe68

File tree

4 files changed

+66
-132
lines changed

4 files changed

+66
-132
lines changed

clang/include/clang/Basic/DiagnosticParseKinds.td

-1
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

+15-8
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

+33-58
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";
@@ -243,42 +246,33 @@ std::optional<RootSignatureToken> RootSignatureLexer::PeekNextToken() {
243246

244247
// Parser Definitions
245248

246-
RootSignatureParser::RootSignatureParser(
247-
SmallVector<RootElement> &Elements,
248-
const SmallVector<RootSignatureToken> &Tokens, DiagnosticsEngine &Diags)
249-
: Elements(Elements), Diags(Diags) {
250-
CurTok = Tokens.begin();
251-
LastTok = Tokens.end();
252-
}
249+
RootSignatureParser::RootSignatureParser(SmallVector<RootElement> &Elements,
250+
RootSignatureLexer &Lexer,
251+
DiagnosticsEngine &Diags)
252+
: Elements(Elements), Lexer(Lexer), Diags(Diags) {}
253253

254254
bool RootSignatureParser::Parse() {
255255
// Handle edge-case of empty RootSignature()
256-
if (CurTok == LastTok)
256+
if (Lexer.EndOfBuffer())
257257
return false;
258258

259-
bool First = true;
260259
// Iterate as many RootElements as possible
261-
while (!ParseRootElement(First)) {
262-
First = false;
263-
// Avoid use of ConsumeNextToken here to skip incorrect end of tokens error
264-
CurTok++;
265-
if (CurTok == LastTok)
260+
while (!ParseRootElement()) {
261+
if (Lexer.EndOfBuffer())
266262
return false;
267-
if (EnsureExpectedToken(TokenKind::pu_comma))
263+
if (ConsumeExpectedToken(TokenKind::pu_comma))
268264
return true;
269265
}
270266

271267
return true;
272268
}
273269

274-
bool RootSignatureParser::ParseRootElement(bool First) {
275-
if (First && EnsureExpectedToken(TokenKind::kw_DescriptorTable))
276-
return true;
277-
if (!First && ConsumeExpectedToken(TokenKind::kw_DescriptorTable))
270+
bool RootSignatureParser::ParseRootElement() {
271+
if (ConsumeExpectedToken(TokenKind::kw_DescriptorTable))
278272
return true;
279273

280274
// Dispatch onto the correct parse method
281-
switch (CurTok->Kind) {
275+
switch (CurToken.Kind) {
282276
case TokenKind::kw_DescriptorTable:
283277
return ParseDescriptorTable();
284278
default:
@@ -305,8 +299,8 @@ bool RootSignatureParser::ParseDescriptorTable() {
305299
// Handle the visibility parameter
306300
if (!TryConsumeExpectedToken(TokenKind::kw_visibility)) {
307301
if (SeenVisibility) {
308-
Diags.Report(CurTok->TokLoc, diag::err_hlsl_rootsig_repeat_param)
309-
<< FormatTokenKinds(CurTok->Kind);
302+
Diags.Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
303+
<< FormatTokenKinds(CurToken.Kind);
310304
return true;
311305
}
312306
SeenVisibility = true;
@@ -334,7 +328,7 @@ bool RootSignatureParser::ParseDescriptorTableClause() {
334328
return true;
335329

336330
DescriptorTableClause Clause;
337-
switch (CurTok->Kind) {
331+
switch (CurToken.Kind) {
338332
case TokenKind::kw_CBV:
339333
Clause.Type = ClauseType::CBuffer;
340334
break;
@@ -419,9 +413,9 @@ bool RootSignatureParser::ParseOptionalParams(
419413
if (ConsumeExpectedToken(ParamKeywords))
420414
return true;
421415

422-
TokenKind ParamKind = CurTok->Kind;
416+
TokenKind ParamKind = CurToken.Kind;
423417
if (Seen.contains(ParamKind)) {
424-
Diags.Report(CurTok->TokLoc, diag::err_hlsl_rootsig_repeat_param)
418+
Diags.Report(CurToken.TokLoc, diag::err_hlsl_rootsig_repeat_param)
425419
<< FormatTokenKinds(ParamKind);
426420
return true;
427421
}
@@ -440,25 +434,25 @@ bool RootSignatureParser::ParseDescriptorRangeOffset(DescriptorRangeOffset *X) {
440434
return true;
441435

442436
// Edge case for the offset enum -> static value
443-
if (CurTok->Kind == TokenKind::en_DescriptorRangeOffsetAppend) {
437+
if (CurToken.Kind == TokenKind::en_DescriptorRangeOffsetAppend) {
444438
*X = DescriptorTableOffsetAppend;
445439
return false;
446440
}
447441

448-
*X = DescriptorRangeOffset(CurTok->NumLiteral.getInt().getExtValue());
442+
*X = DescriptorRangeOffset(CurToken.NumLiteral.getInt().getExtValue());
449443
return false;
450444
}
451445

452446
bool RootSignatureParser::ParseUInt(uint32_t *X) {
453447
if (ConsumeExpectedToken(TokenKind::int_literal))
454448
return true;
455449

456-
*X = CurTok->NumLiteral.getInt().getExtValue();
450+
*X = CurToken.NumLiteral.getInt().getExtValue();
457451
return false;
458452
}
459453

460454
bool RootSignatureParser::ParseRegister(Register *Register) {
461-
switch (CurTok->Kind) {
455+
switch (CurToken.Kind) {
462456
case TokenKind::bReg:
463457
Register->ViewType = RegisterType::BReg;
464458
break;
@@ -475,7 +469,7 @@ bool RootSignatureParser::ParseRegister(Register *Register) {
475469
llvm_unreachable("Switch for an expected token was not provided");
476470
}
477471

478-
Register->Number = CurTok->NumLiteral.getInt().getExtValue();
472+
Register->Number = CurToken.NumLiteral.getInt().getExtValue();
479473

480474
return false;
481475
}
@@ -494,9 +488,9 @@ bool RootSignatureParser::ParseEnum(
494488
return true;
495489

496490
// Handle the edge case when '0' is used to specify None
497-
if (CurTok->Kind == TokenKind::int_literal) {
498-
if (CurTok->NumLiteral.getInt() != 0) {
499-
Diags.Report(CurTok->TokLoc, diag::err_hlsl_rootsig_non_zero_flag);
491+
if (CurToken.Kind == TokenKind::int_literal) {
492+
if (CurToken.NumLiteral.getInt() != 0) {
493+
Diags.Report(CurToken.TokLoc, diag::err_hlsl_rootsig_non_zero_flag);
500494
return true;
501495
}
502496
// Set enum to None equivalent
@@ -506,7 +500,7 @@ bool RootSignatureParser::ParseEnum(
506500

507501
// Effectively a switch statement on the token kinds
508502
for (auto EnumPair : EnumMap)
509-
if (CurTok->Kind == EnumPair.first) {
503+
if (CurToken.Kind == EnumPair.first) {
510504
*Enum = EnumPair.second;
511505
return false;
512506
}
@@ -556,25 +550,6 @@ bool RootSignatureParser::ParseShaderVisibility(ShaderVisibility *Enum) {
556550
return ParseEnum(EnumMap, Enum);
557551
}
558552

559-
RootSignatureToken RootSignatureParser::PeekNextToken() {
560-
// Create an invalid token
561-
RootSignatureToken Token = RootSignatureToken(SourceLocation());
562-
if (CurTok != LastTok)
563-
Token = *(CurTok + 1);
564-
return Token;
565-
}
566-
567-
bool RootSignatureParser::ConsumeNextToken() {
568-
SourceLocation EndLoc = CurTok->TokLoc;
569-
CurTok++;
570-
if (LastTok == CurTok) {
571-
// Report unexpected end of tokens error
572-
Diags.Report(EndLoc, diag::err_hlsl_rootsig_unexpected_eos);
573-
return true;
574-
}
575-
return false;
576-
}
577-
578553
// Is given token one of the expected kinds
579554
static bool IsExpectedToken(TokenKind Kind, ArrayRef<TokenKind> AnyExpected) {
580555
for (auto Expected : AnyExpected)
@@ -588,11 +563,11 @@ bool RootSignatureParser::EnsureExpectedToken(TokenKind Expected) {
588563
}
589564

590565
bool RootSignatureParser::EnsureExpectedToken(ArrayRef<TokenKind> AnyExpected) {
591-
if (IsExpectedToken(CurTok->Kind, AnyExpected))
566+
if (IsExpectedToken(CurToken.Kind, AnyExpected))
592567
return false;
593568

594569
// Report unexpected token kind error
595-
Diags.Report(CurTok->TokLoc, diag::err_hlsl_rootsig_unexpected_token_kind)
570+
Diags.Report(CurToken.TokLoc, diag::err_hlsl_rootsig_unexpected_token_kind)
596571
<< (unsigned)(AnyExpected.size() != 1) << FormatTokenKinds(AnyExpected);
597572
return true;
598573
}
@@ -602,10 +577,10 @@ bool RootSignatureParser::PeekExpectedToken(TokenKind Expected) {
602577
}
603578

604579
bool RootSignatureParser::PeekExpectedToken(ArrayRef<TokenKind> AnyExpected) {
605-
RootSignatureToken Token = PeekNextToken();
606-
if (Token.Kind == TokenKind::invalid)
580+
auto Result = Lexer.PeekNextToken();
581+
if (!Result)
607582
return true;
608-
if (IsExpectedToken(Token.Kind, AnyExpected))
583+
if (IsExpectedToken(Result->Kind, AnyExpected))
609584
return false;
610585
return true;
611586
}

0 commit comments

Comments
 (0)