|
| 1 | +//===--- ParseHLSLRootSignature.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 RootSignatureParser interface. |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +#ifndef LLVM_CLANG_PARSE_PARSEHLSLROOTSIGNATURE_H |
| 14 | +#define LLVM_CLANG_PARSE_PARSEHLSLROOTSIGNATURE_H |
| 15 | + |
| 16 | +#include "clang/Basic/DiagnosticParse.h" |
| 17 | +#include "clang/Lex/LexHLSLRootSignature.h" |
| 18 | +#include "clang/Lex/Preprocessor.h" |
| 19 | + |
| 20 | +#include "llvm/ADT/SmallVector.h" |
| 21 | +#include "llvm/ADT/StringRef.h" |
| 22 | + |
| 23 | +#include "llvm/Frontend/HLSL/HLSLRootSignature.h" |
| 24 | + |
| 25 | +namespace clang { |
| 26 | +namespace hlsl { |
| 27 | + |
| 28 | +class RootSignatureParser { |
| 29 | +public: |
| 30 | + RootSignatureParser(SmallVector<llvm::hlsl::rootsig::RootElement> &Elements, |
| 31 | + RootSignatureLexer &Lexer, clang::Preprocessor &PP); |
| 32 | + |
| 33 | + /// Consumes tokens from the Lexer and constructs the in-memory |
| 34 | + /// representations of the RootElements. Tokens are consumed until an |
| 35 | + /// error is encountered or the end of the buffer. |
| 36 | + /// |
| 37 | + /// Returns true if a parsing error is encountered. |
| 38 | + bool parse(); |
| 39 | + |
| 40 | +private: |
| 41 | + DiagnosticsEngine &getDiags() { return PP.getDiagnostics(); } |
| 42 | + |
| 43 | + // All private Parse.* methods follow a similar pattern: |
| 44 | + // - Each method will start with an assert to denote what the CurToken is |
| 45 | + // expected to be and will parse from that token forward |
| 46 | + // |
| 47 | + // - Therefore, it is the callers responsibility to ensure that you are |
| 48 | + // at the correct CurToken. This should be done with the pattern of: |
| 49 | + // |
| 50 | + // if (TryConsumeExpectedToken(RootSignatureToken::Kind)) |
| 51 | + // if (Parse.*()) |
| 52 | + // return true; |
| 53 | + // |
| 54 | + // or, |
| 55 | + // |
| 56 | + // if (ConsumeExpectedToken(RootSignatureToken::Kind, ...)) |
| 57 | + // return true; |
| 58 | + // if (Parse.*()) |
| 59 | + // return true; |
| 60 | + // |
| 61 | + // - All methods return true if a parsing error is encountered. It is the |
| 62 | + // callers responsibility to propogate this error up, or deal with it |
| 63 | + // otherwise |
| 64 | + // |
| 65 | + // - An error will be raised if the proceeding tokens are not what is |
| 66 | + // expected, or, there is a lexing error |
| 67 | + |
| 68 | + /// Root Element parse methods: |
| 69 | + bool parseDescriptorTable(); |
| 70 | + bool parseDescriptorTableClause(); |
| 71 | + |
| 72 | + /// Invoke the Lexer to consume a token and update CurToken with the result |
| 73 | + void consumeNextToken() { CurToken = Lexer.ConsumeToken(); } |
| 74 | + |
| 75 | + /// Return true if the next token one of the expected kinds |
| 76 | + bool peekExpectedToken(RootSignatureToken::Kind Expected); |
| 77 | + bool peekExpectedToken(ArrayRef<RootSignatureToken::Kind> AnyExpected); |
| 78 | + |
| 79 | + /// Consumes the next token and report an error if it is not of the expected |
| 80 | + /// kind. |
| 81 | + /// |
| 82 | + /// Returns true if there was an error reported. |
| 83 | + bool consumeExpectedToken( |
| 84 | + RootSignatureToken::Kind Expected, unsigned DiagID = diag::err_expected, |
| 85 | + RootSignatureToken::Kind Context = RootSignatureToken::Kind::invalid); |
| 86 | + |
| 87 | + /// Peek if the next token is of the expected kind and if it is then consume |
| 88 | + /// it. |
| 89 | + /// |
| 90 | + /// Returns true if it successfully matches the expected kind and the token |
| 91 | + /// was consumed. |
| 92 | + bool tryConsumeExpectedToken(RootSignatureToken::Kind Expected); |
| 93 | + bool tryConsumeExpectedToken(ArrayRef<RootSignatureToken::Kind> Expected); |
| 94 | + |
| 95 | +private: |
| 96 | + SmallVector<llvm::hlsl::rootsig::RootElement> &Elements; |
| 97 | + RootSignatureLexer &Lexer; |
| 98 | + |
| 99 | + clang::Preprocessor &PP; |
| 100 | + |
| 101 | + RootSignatureToken CurToken; |
| 102 | +}; |
| 103 | + |
| 104 | +} // namespace hlsl |
| 105 | +} // namespace clang |
| 106 | + |
| 107 | +#endif // LLVM_CLANG_PARSE_PARSEHLSLROOTSIGNATURE_H |
0 commit comments