@@ -10,45 +10,15 @@ static bool IsNumberChar(char C) {
1010 return isdigit (C); // integer support
1111}
1212
13- bool RootSignatureLexer::LexNumber (RootSignatureToken &Result) {
14- // Retrieve the possible number
15- StringRef NumSpelling = Buffer.take_while (IsNumberChar);
16-
17- // Parse the numeric value and do semantic checks on its specification
18- clang::NumericLiteralParser Literal (NumSpelling, SourceLoc,
19- PP.getSourceManager (), PP.getLangOpts (),
20- PP.getTargetInfo (), PP.getDiagnostics ());
21- if (Literal.hadError )
22- return true ; // Error has already been reported so just return
23-
24- // Note: if IsNumberChar allows for hexidecimal we will need to turn this
25- // into a diagnostics for potential fixed-point literals
26- assert (Literal.isIntegerLiteral () && " IsNumberChar will only support digits" );
27-
28- // Retrieve the number value to store into the token
29-
30- llvm::APSInt X = llvm::APSInt (32 , true );
31- if (Literal.GetIntegerValue (X)) {
32- // Report that the value has overflowed
33- PP.getDiagnostics ().Report (Result.TokLoc ,
34- diag::err_hlsl_number_literal_overflow)
35- << NumSpelling;
36- return true ;
37- }
38-
39- Result.Kind = TokenKind::int_literal;
40- Result.NumLiteral = APValue (X);
41-
42- AdvanceBuffer (NumSpelling.size ());
43- return false ;
44- }
45-
46- bool RootSignatureLexer::LexToken (RootSignatureToken &Result) {
13+ RootSignatureToken RootSignatureLexer::LexToken () {
4714 // Discard any leading whitespace
4815 AdvanceBuffer (Buffer.take_while (isspace).size ());
4916
17+ if (EndOfBuffer ())
18+ return RootSignatureToken (TokenKind::end_of_stream, SourceLoc);
19+
5020 // Record where this token is in the text for usage in parser diagnostics
51- Result = RootSignatureToken (SourceLoc);
21+ RootSignatureToken Result (SourceLoc);
5222
5323 char C = Buffer.front ();
5424
@@ -58,44 +28,33 @@ bool RootSignatureLexer::LexToken(RootSignatureToken &Result) {
5828 case Y: { \
5929 Result.Kind = TokenKind::pu_##X; \
6030 AdvanceBuffer (); \
61- return false ; \
31+ return Result; \
6232 }
6333#include " clang/Parse/HLSLRootSignatureTokenKinds.def"
6434 default :
6535 break ;
6636 }
6737
68- // Numeric constant
69- if (isdigit (C))
70- return LexNumber (Result);
38+ // Integer literal
39+ if (isdigit (C)) {
40+ Result.Kind = TokenKind::int_literal;
41+ Result.NumSpelling = Buffer.take_while (IsNumberChar);
42+ AdvanceBuffer (Result.NumSpelling .size ());
43+ return Result;
44+ }
7145
7246 // All following tokens require at least one additional character
7347 if (Buffer.size () <= 1 ) {
7448 Result = RootSignatureToken (TokenKind::invalid, SourceLoc);
75- return false ;
49+ return Result ;
7650 }
7751
7852 // Peek at the next character to deteremine token type
7953 char NextC = Buffer[1 ];
8054
8155 // Registers: [tsub][0-9+]
8256 if ((C == ' t' || C == ' s' || C == ' u' || C == ' b' ) && isdigit (NextC)) {
83- AdvanceBuffer ();
84-
85- if (LexNumber (Result))
86- return true ; // Error parsing number which is already reported
87-
88- // Lex number could also parse a float so ensure it was an unsigned int
89- if (Result.Kind != TokenKind::int_literal ||
90- Result.NumLiteral .getInt ().isSigned ()) {
91- // Return invalid number literal for register error
92- PP.getDiagnostics ().Report (Result.TokLoc ,
93- diag::err_hlsl_invalid_register_literal);
94- return true ;
95- }
96-
9757 // Convert character to the register type.
98- // This is done after LexNumber to override the TokenKind
9958 switch (C) {
10059 case ' b' :
10160 Result.Kind = TokenKind::bReg;
@@ -112,7 +71,14 @@ bool RootSignatureLexer::LexToken(RootSignatureToken &Result) {
11271 default :
11372 llvm_unreachable (" Switch for an expected token was not provided" );
11473 }
115- return false ;
74+
75+ AdvanceBuffer ();
76+
77+ // Lex the integer literal
78+ Result.NumSpelling = Buffer.take_while (IsNumberChar);
79+ AdvanceBuffer (Result.NumSpelling .size ());
80+
81+ return Result;
11682 }
11783
11884 // Keywords and Enums:
@@ -128,44 +94,26 @@ bool RootSignatureLexer::LexToken(RootSignatureToken &Result) {
12894 // Then attempt to retreive a string from it
12995 Result.Kind = Switch.Default (TokenKind::invalid);
13096 AdvanceBuffer (TokSpelling.size ());
131- return false ;
97+ return Result ;
13298}
13399
134- bool RootSignatureLexer::ConsumeToken () {
135- // If we previously peeked then just copy the value over
100+ RootSignatureToken RootSignatureLexer::ConsumeToken () {
101+ // If we previously peeked then just return the previous value over
136102 if (NextToken && NextToken->Kind != TokenKind::end_of_stream) {
137- // Propogate error up if error was encountered during previous peek
138- if (NextToken->Kind == TokenKind::error)
139- return true ;
140- CurToken = *NextToken;
103+ RootSignatureToken Result = *NextToken;
141104 NextToken = std::nullopt ;
142- return false ;
105+ return Result ;
143106 }
144-
145- // This will be implicity be true if NextToken->Kind == end_of_stream
146- if (EndOfBuffer ()) {
147- CurToken = RootSignatureToken (TokenKind::end_of_stream, SourceLoc);
148- return false ;
149- }
150-
151- return LexToken (CurToken);
107+ return LexToken ();
152108}
153109
154- std::optional< RootSignatureToken> RootSignatureLexer::PeekNextToken () {
110+ RootSignatureToken RootSignatureLexer::PeekNextToken () {
155111 // Already peeked from the current token
156- if (NextToken.has_value ())
157- return NextToken;
158-
159- RootSignatureToken Result;
160- if (EndOfBuffer ()) {
161- Result = RootSignatureToken (TokenKind::end_of_stream, SourceLoc);
162- } else if (LexToken (Result)) { // propogate lex error up
163- // store error token to prevent further peeking
164- NextToken = RootSignatureToken ();
165- return std::nullopt ;
166- }
167- NextToken = Result;
168- return Result;
112+ if (NextToken)
113+ return *NextToken;
114+
115+ NextToken = LexToken ();
116+ return *NextToken;
169117}
170118
171119} // namespace hlsl
0 commit comments