|
| 1 | +//=== ParseHLSLRootSignature.cpp - Parse Root Signature -------------------===// |
| 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 | +#include "clang/Parse/ParseHLSLRootSignature.h" |
| 10 | + |
| 11 | +#include "llvm/Support/raw_ostream.h" |
| 12 | + |
| 13 | +using namespace llvm::hlsl::rootsig; |
| 14 | + |
| 15 | +namespace clang { |
| 16 | +namespace hlsl { |
| 17 | + |
| 18 | +RootSignatureParser::RootSignatureParser(SmallVector<RootElement> &Elements, |
| 19 | + RootSignatureLexer &Lexer, |
| 20 | + Preprocessor &PP) |
| 21 | + : Elements(Elements), Lexer(Lexer), PP(PP), CurToken(SourceLocation()) {} |
| 22 | + |
| 23 | +bool RootSignatureParser::parse() { |
| 24 | + // Iterate as many RootElements as possible |
| 25 | + while (tryConsumeExpectedToken(TokenKind::kw_DescriptorTable)) { |
| 26 | + // Dispatch onto parser method. |
| 27 | + // We guard against the unreachable here as we just ensured that CurToken |
| 28 | + // will be one of the kinds in the while condition |
| 29 | + switch (CurToken.Kind) { |
| 30 | + case TokenKind::kw_DescriptorTable: |
| 31 | + if (parseDescriptorTable()) |
| 32 | + return true; |
| 33 | + break; |
| 34 | + default: |
| 35 | + llvm_unreachable("Switch for consumed token was not provided"); |
| 36 | + } |
| 37 | + |
| 38 | + if (!tryConsumeExpectedToken(TokenKind::pu_comma)) |
| 39 | + break; |
| 40 | + } |
| 41 | + |
| 42 | + if (!tryConsumeExpectedToken(TokenKind::end_of_stream)) { |
| 43 | + getDiags().Report(CurToken.TokLoc, diag::err_hlsl_unexpected_end_of_params) |
| 44 | + << /*expected=*/TokenKind::end_of_stream |
| 45 | + << /*param of=*/TokenKind::kw_RootSignature; |
| 46 | + return true; |
| 47 | + } |
| 48 | + return false; |
| 49 | +} |
| 50 | + |
| 51 | +bool RootSignatureParser::parseDescriptorTable() { |
| 52 | + assert(CurToken.Kind == TokenKind::kw_DescriptorTable && |
| 53 | + "Expects to only be invoked starting at given keyword"); |
| 54 | + |
| 55 | + DescriptorTable Table; |
| 56 | + |
| 57 | + if (consumeExpectedToken(TokenKind::pu_l_paren, diag::err_expected_after, |
| 58 | + CurToken.Kind)) |
| 59 | + return true; |
| 60 | + |
| 61 | + // Iterate as many Clauses as possible |
| 62 | + while (tryConsumeExpectedToken({TokenKind::kw_CBV, TokenKind::kw_SRV, |
| 63 | + TokenKind::kw_UAV, TokenKind::kw_Sampler})) { |
| 64 | + if (parseDescriptorTableClause()) |
| 65 | + return true; |
| 66 | + |
| 67 | + Table.NumClauses++; |
| 68 | + |
| 69 | + if (!tryConsumeExpectedToken(TokenKind::pu_comma)) |
| 70 | + break; |
| 71 | + } |
| 72 | + |
| 73 | + if (!tryConsumeExpectedToken(TokenKind::pu_r_paren)) { |
| 74 | + getDiags().Report(CurToken.TokLoc, diag::err_hlsl_unexpected_end_of_params) |
| 75 | + << /*expected=*/TokenKind::pu_r_paren |
| 76 | + << /*param of=*/TokenKind::kw_DescriptorTable; |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + Elements.push_back(Table); |
| 81 | + return false; |
| 82 | +} |
| 83 | + |
| 84 | +bool RootSignatureParser::parseDescriptorTableClause() { |
| 85 | + assert((CurToken.Kind == TokenKind::kw_CBV || |
| 86 | + CurToken.Kind == TokenKind::kw_SRV || |
| 87 | + CurToken.Kind == TokenKind::kw_UAV || |
| 88 | + CurToken.Kind == TokenKind::kw_Sampler) && |
| 89 | + "Expects to only be invoked starting at given keyword"); |
| 90 | + |
| 91 | + DescriptorTableClause Clause; |
| 92 | + switch (CurToken.Kind) { |
| 93 | + default: |
| 94 | + llvm_unreachable("Switch for consumed token was not provided"); |
| 95 | + case TokenKind::kw_CBV: |
| 96 | + Clause.Type = ClauseType::CBuffer; |
| 97 | + break; |
| 98 | + case TokenKind::kw_SRV: |
| 99 | + Clause.Type = ClauseType::SRV; |
| 100 | + break; |
| 101 | + case TokenKind::kw_UAV: |
| 102 | + Clause.Type = ClauseType::UAV; |
| 103 | + break; |
| 104 | + case TokenKind::kw_Sampler: |
| 105 | + Clause.Type = ClauseType::Sampler; |
| 106 | + break; |
| 107 | + } |
| 108 | + |
| 109 | + if (consumeExpectedToken(TokenKind::pu_l_paren, diag::err_expected_after, |
| 110 | + CurToken.Kind)) |
| 111 | + return true; |
| 112 | + |
| 113 | + if (consumeExpectedToken(TokenKind::pu_r_paren, diag::err_expected_after, |
| 114 | + CurToken.Kind)) |
| 115 | + return true; |
| 116 | + |
| 117 | + Elements.push_back(Clause); |
| 118 | + return false; |
| 119 | +} |
| 120 | + |
| 121 | +bool RootSignatureParser::peekExpectedToken(TokenKind Expected) { |
| 122 | + return peekExpectedToken(ArrayRef{Expected}); |
| 123 | +} |
| 124 | + |
| 125 | +bool RootSignatureParser::peekExpectedToken(ArrayRef<TokenKind> AnyExpected) { |
| 126 | + RootSignatureToken Result = Lexer.PeekNextToken(); |
| 127 | + return llvm::is_contained(AnyExpected, Result.Kind); |
| 128 | +} |
| 129 | + |
| 130 | +bool RootSignatureParser::consumeExpectedToken(TokenKind Expected, |
| 131 | + unsigned DiagID, |
| 132 | + TokenKind Context) { |
| 133 | + if (tryConsumeExpectedToken(Expected)) |
| 134 | + return false; |
| 135 | + |
| 136 | + // Report unexpected token kind error |
| 137 | + DiagnosticBuilder DB = getDiags().Report(CurToken.TokLoc, DiagID); |
| 138 | + switch (DiagID) { |
| 139 | + case diag::err_expected: |
| 140 | + DB << Expected; |
| 141 | + break; |
| 142 | + case diag::err_expected_either: |
| 143 | + case diag::err_expected_after: |
| 144 | + DB << Expected << Context; |
| 145 | + break; |
| 146 | + default: |
| 147 | + break; |
| 148 | + } |
| 149 | + return true; |
| 150 | +} |
| 151 | + |
| 152 | +bool RootSignatureParser::tryConsumeExpectedToken(TokenKind Expected) { |
| 153 | + return tryConsumeExpectedToken(ArrayRef{Expected}); |
| 154 | +} |
| 155 | + |
| 156 | +bool RootSignatureParser::tryConsumeExpectedToken( |
| 157 | + ArrayRef<TokenKind> AnyExpected) { |
| 158 | + // If not the expected token just return |
| 159 | + if (!peekExpectedToken(AnyExpected)) |
| 160 | + return false; |
| 161 | + consumeNextToken(); |
| 162 | + return true; |
| 163 | +} |
| 164 | + |
| 165 | +} // namespace hlsl |
| 166 | +} // namespace clang |
0 commit comments