================ @@ -148,6 +148,333 @@ bool RootSignatureLexer::LexToken(RootSignatureToken &Result) { return false; } +// Parser Definitions + +RootSignatureParser::RootSignatureParser( + SmallVector<RootElement> &Elements, + const SmallVector<RootSignatureToken> &Tokens) + : Elements(Elements) { + CurTok = Tokens.begin(); + LastTok = Tokens.end(); +} + +bool RootSignatureParser::ReportError() { return true; } + +bool RootSignatureParser::Parse() { + CurTok--; // Decrement once here so we can use the ...ExpectedToken api + + // Iterate as many RootElements as possible + bool HasComma = true; + while (HasComma && + !TryConsumeExpectedToken(ArrayRef{TokenKind::kw_DescriptorTable})) { + if (ParseRootElement()) + return true; + HasComma = !TryConsumeExpectedToken(TokenKind::pu_comma); + } + if (HasComma) + return ReportError(); // report 'comma' denotes a required extra item + + // Ensure that we are at the end of the tokens + CurTok++; + if (CurTok != LastTok) + return ReportError(); // report expected end of input but got more + return false; +} + +bool RootSignatureParser::ParseRootElement() { + // Dispatch onto the correct parse method + switch (CurTok->Kind) { + case TokenKind::kw_DescriptorTable: + return ParseDescriptorTable(); + default: + llvm_unreachable("Switch for an expected token was not provided"); + return true; + } +} + +bool RootSignatureParser::ParseDescriptorTable() { + DescriptorTable Table; + + if (ConsumeExpectedToken(TokenKind::pu_l_paren)) + return true; + + // Iterate as many DescriptorTableClaues as possible + bool HasComma = true; + while (!TryConsumeExpectedToken({TokenKind::kw_CBV, TokenKind::kw_SRV, + TokenKind::kw_UAV, TokenKind::kw_Sampler})) { + if (ParseDescriptorTableClause()) + return true; + Table.NumClauses++; + HasComma = !TryConsumeExpectedToken(TokenKind::pu_comma); + } + + // Consume optional 'visibility' paramater + if (HasComma && !TryConsumeExpectedToken(TokenKind::kw_visibility)) { + if (ConsumeExpectedToken(TokenKind::pu_equal)) + return true; + + if (ParseShaderVisibility(Table.Visibility)) + return true; + + HasComma = !TryConsumeExpectedToken(TokenKind::pu_comma); + } + + if (HasComma && Table.NumClauses != 0) + return ReportError(); // report 'comma' denotes a required extra item + + if (ConsumeExpectedToken(TokenKind::pu_r_paren)) + return true; + + Elements.push_back(RootElement(Table)); + return false; +} + +bool RootSignatureParser::ParseDescriptorTableClause() { + // Determine the type of Clause first so we can initialize the struct with + // the correct default flags + ClauseType CT; + switch (CurTok->Kind) { + case TokenKind::kw_CBV: + CT = ClauseType::CBV; + break; + case TokenKind::kw_SRV: + CT = ClauseType::SRV; + break; + case TokenKind::kw_UAV: + CT = ClauseType::UAV; + break; + case TokenKind::kw_Sampler: + CT = ClauseType::Sampler; + break; + default: + llvm_unreachable("Switch for an expected token was not provided"); + return true; + } + DescriptorTableClause Clause(CT); + + if (ConsumeExpectedToken(TokenKind::pu_l_paren)) + return true; + + // Consume mandatory Register paramater + if (ConsumeExpectedToken( + {TokenKind::bReg, TokenKind::tReg, TokenKind::uReg, TokenKind::sReg})) + return true; + if (ParseRegister(Clause.Register)) + return true; + + // Start parsing the optional parameters ---------------- inbelic wrote:
Confirmed that we should accept the parameters in any order. So will need to refactor this. https://github.com/llvm/llvm-project/pull/122982 _______________________________________________ llvm-branch-commits mailing list llvm-branch-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits