1 | //===--- LexHLSLRootSignature.h ---------------------------------*- C++ -*-===// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file defines the LexHLSLRootSignature interface. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_CLANG_LEX_LEXHLSLROOTSIGNATURE_H |
14 | #define LLVM_CLANG_LEX_LEXHLSLROOTSIGNATURE_H |
15 | |
16 | #include "clang/Basic/Diagnostic.h" |
17 | #include "clang/Basic/SourceLocation.h" |
18 | |
19 | #include "llvm/ADT/SmallVector.h" |
20 | #include "llvm/ADT/StringRef.h" |
21 | #include "llvm/ADT/StringSwitch.h" |
22 | |
23 | namespace clang { |
24 | namespace hlsl { |
25 | |
26 | struct RootSignatureToken { |
27 | enum Kind { |
28 | #define TOK(X, SPELLING) X, |
29 | #include "clang/Lex/HLSLRootSignatureTokenKinds.def" |
30 | }; |
31 | |
32 | Kind TokKind = Kind::invalid; |
33 | |
34 | // Retain the SouceLocation of the token for diagnostics |
35 | clang::SourceLocation TokLoc; |
36 | |
37 | // Retain spelling of an numeric constant to be parsed later |
38 | StringRef NumSpelling; |
39 | |
40 | // Constructors |
41 | RootSignatureToken(clang::SourceLocation TokLoc) : TokLoc(TokLoc) {} |
42 | RootSignatureToken(Kind TokKind, clang::SourceLocation TokLoc) |
43 | : TokKind(TokKind), TokLoc(TokLoc) {} |
44 | }; |
45 | |
46 | inline const DiagnosticBuilder & |
47 | operator<<(const DiagnosticBuilder &DB, const RootSignatureToken::Kind Kind) { |
48 | switch (Kind) { |
49 | #define TOK(X, SPELLING) \ |
50 | case RootSignatureToken::Kind::X: \ |
51 | DB << SPELLING; \ |
52 | break; |
53 | #include "clang/Lex/HLSLRootSignatureTokenKinds.def" |
54 | } |
55 | return DB; |
56 | } |
57 | |
58 | class RootSignatureLexer { |
59 | public: |
60 | RootSignatureLexer(StringRef Signature, clang::SourceLocation SourceLoc) |
61 | : Buffer(Signature), SourceLoc(SourceLoc) {} |
62 | |
63 | /// Consumes and returns the next token. |
64 | RootSignatureToken consumeToken(); |
65 | |
66 | /// Returns the token that proceeds CurToken |
67 | RootSignatureToken peekNextToken(); |
68 | |
69 | bool isEndOfBuffer() { |
70 | advanceBuffer(NumCharacters: Buffer.take_while(F: isspace).size()); |
71 | return Buffer.empty(); |
72 | } |
73 | |
74 | private: |
75 | // Internal buffer to iterate over |
76 | StringRef Buffer; |
77 | |
78 | // Current peek state |
79 | std::optional<RootSignatureToken> NextToken = std::nullopt; |
80 | |
81 | // Passed down parameters from Sema |
82 | clang::SourceLocation SourceLoc; |
83 | |
84 | /// Consumes the buffer and returns the lexed token. |
85 | RootSignatureToken lexToken(); |
86 | |
87 | /// Advance the buffer by the specified number of characters. |
88 | /// Updates the SourceLocation appropriately. |
89 | void advanceBuffer(unsigned NumCharacters = 1) { |
90 | Buffer = Buffer.drop_front(N: NumCharacters); |
91 | SourceLoc = SourceLoc.getLocWithOffset(Offset: NumCharacters); |
92 | } |
93 | }; |
94 | |
95 | } // namespace hlsl |
96 | } // namespace clang |
97 | |
98 | #endif // LLVM_CLANG_LEX_PARSEHLSLROOTSIGNATURE_H |
99 |