From: Fangrui Song Date: Sun, 26 Apr 2020 04:05:19 +0000 (-0700) Subject: [TableGen] Add TGParser::consume() X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e69605bfb6dd1fb7f2b590a8271ff3c3c1f2d753;p=platform%2Fupstream%2Fllvm.git [TableGen] Add TGParser::consume() --- diff --git a/llvm/lib/TableGen/TGParser.cpp b/llvm/lib/TableGen/TGParser.cpp index 08f7647..7d03e45 100644 --- a/llvm/lib/TableGen/TGParser.cpp +++ b/llvm/lib/TableGen/TGParser.cpp @@ -488,6 +488,14 @@ static bool isObjectStart(tgtok::TokKind K) { K == tgtok::Defset || K == tgtok::Defvar || K == tgtok::If; } +bool TGParser::consume(tgtok::TokKind K) { + if (Lex.getCode() == K) { + Lex.Lex(); + return true; + } + return false; +} + /// ParseObjectName - If a valid object name is specified, return it. If no /// name is specified, return the unset initializer. Return nullptr on parse /// error. @@ -591,11 +599,10 @@ ParseSubClassReference(Record *CurRec, bool isDefm) { if (!Result.Rec) return Result; // If there is no template arg list, we're done. - if (Lex.getCode() != tgtok::less) { + if (!consume(tgtok::less)) { Result.RefRange.End = Lex.getLoc(); return Result; } - Lex.Lex(); // Eat the '<' if (Lex.getCode() == tgtok::greater) { TokError("subclass reference requires a non-empty list of template values"); @@ -609,12 +616,11 @@ ParseSubClassReference(Record *CurRec, bool isDefm) { return Result; } - if (Lex.getCode() != tgtok::greater) { + if (!consume(tgtok::greater)) { TokError("expected '>' in template value list"); Result.Rec = nullptr; return Result; } - Lex.Lex(); Result.RefRange.End = Lex.getLoc(); return Result; @@ -636,11 +642,10 @@ ParseSubMultiClassReference(MultiClass *CurMC) { if (!Result.MC) return Result; // If there is no template arg list, we're done. - if (Lex.getCode() != tgtok::less) { + if (!consume(tgtok::less)) { Result.RefRange.End = Lex.getLoc(); return Result; } - Lex.Lex(); // Eat the '<' if (Lex.getCode() == tgtok::greater) { TokError("subclass reference requires a non-empty list of template values"); @@ -654,12 +659,11 @@ ParseSubMultiClassReference(MultiClass *CurMC) { return Result; } - if (Lex.getCode() != tgtok::greater) { + if (!consume(tgtok::greater)) { TokError("expected '>' in template value list"); Result.MC = nullptr; return Result; } - Lex.Lex(); Result.RefRange.End = Lex.getLoc(); return Result; @@ -731,36 +735,30 @@ void TGParser::ParseRangeList(SmallVectorImpl &Result) { Result.clear(); return; } - while (Lex.getCode() == tgtok::comma) { - Lex.Lex(); // Eat the comma. - + while (consume(tgtok::comma)) // Parse the next range piece. if (ParseRangePiece(Result)) { Result.clear(); return; } - } } /// ParseOptionalRangeList - Parse either a range list in <>'s or nothing. /// OptionalRangeList ::= '<' RangeList '>' /// OptionalRangeList ::= /*empty*/ bool TGParser::ParseOptionalRangeList(SmallVectorImpl &Ranges) { - if (Lex.getCode() != tgtok::less) - return false; - SMLoc StartLoc = Lex.getLoc(); - Lex.Lex(); // eat the '<' + if (!consume(tgtok::less)) + return false; // Parse the range list. ParseRangeList(Ranges); if (Ranges.empty()) return true; - if (Lex.getCode() != tgtok::greater) { + if (!consume(tgtok::greater)) { TokError("expected '>' at end of range list"); return Error(StartLoc, "to match this '<'"); } - Lex.Lex(); // eat the '>'. return false; } @@ -768,21 +766,18 @@ bool TGParser::ParseOptionalRangeList(SmallVectorImpl &Ranges) { /// OptionalBitList ::= '{' RangeList '}' /// OptionalBitList ::= /*empty*/ bool TGParser::ParseOptionalBitList(SmallVectorImpl &Ranges) { - if (Lex.getCode() != tgtok::l_brace) - return false; - SMLoc StartLoc = Lex.getLoc(); - Lex.Lex(); // eat the '{' + if (!consume(tgtok::l_brace)) + return false; // Parse the range list. ParseRangeList(Ranges); if (Ranges.empty()) return true; - if (Lex.getCode() != tgtok::r_brace) { + if (!consume(tgtok::r_brace)) { TokError("expected '}' at end of bit list"); return Error(StartLoc, "to match this '{'"); } - Lex.Lex(); // eat the '}'. return false; } @@ -835,11 +830,10 @@ RecTy *TGParser::ParseType() { RecTy *SubType = ParseType(); if (!SubType) return nullptr; - if (Lex.getCode() != tgtok::greater) { + if (!consume(tgtok::greater)) { TokError("expected '>' at end of list type"); return nullptr; } - Lex.Lex(); // Eat '>' return ListRecTy::get(SubType); } } @@ -974,11 +968,10 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { Code = UnOpInit::GETOP; break; } - if (Lex.getCode() != tgtok::l_paren) { + if (!consume(tgtok::l_paren)) { TokError("expected '(' after unary operator"); return nullptr; } - Lex.Lex(); // eat the '(' Init *LHS = ParseValue(CurRec); if (!LHS) return nullptr; @@ -1036,11 +1029,10 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { } } - if (Lex.getCode() != tgtok::r_paren) { + if (!consume(tgtok::r_paren)) { TokError("expected ')' in unary operator"); return nullptr; } - Lex.Lex(); // eat the ')' return (UnOpInit::get(Code, LHS, Type))->Fold(CurRec); } @@ -1052,21 +1044,19 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { if (!Type) return nullptr; - if (Lex.getCode() != tgtok::l_paren) { + if (!consume(tgtok::l_paren)) { TokError("expected '(' after type of !isa"); return nullptr; } - Lex.Lex(); // eat the '(' Init *LHS = ParseValue(CurRec); if (!LHS) return nullptr; - if (Lex.getCode() != tgtok::r_paren) { + if (!consume(tgtok::r_paren)) { TokError("expected ')' in !isa"); return nullptr; } - Lex.Lex(); // eat the ')' return (IsAOpInit::get(Type, LHS))->Fold(); } @@ -1168,11 +1158,10 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { return nullptr; } - if (Lex.getCode() != tgtok::l_paren) { + if (!consume(tgtok::l_paren)) { TokError("expected '(' after binary operator"); return nullptr; } - Lex.Lex(); // eat the '(' SmallVector InitList; @@ -1264,16 +1253,14 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { break; } - if (Lex.getCode() != tgtok::comma) + if (!consume(tgtok::comma)) break; - Lex.Lex(); // eat the ',' } - if (Lex.getCode() != tgtok::r_paren) { + if (!consume(tgtok::r_paren)) { TokError("expected ')' in operator"); return nullptr; } - Lex.Lex(); // eat the ')' // listconcat returns a list with type of the argument. if (Code == BinOpInit::LISTCONCAT) @@ -1317,6 +1304,7 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { } Init *LHS = StringInit::get(Lex.getCurStrVal()); + Lex.Lex(); if (CurRec && CurRec->getValue(LHS)) { TokError((Twine("iteration variable '") + LHS->getAsString() + @@ -1325,21 +1313,19 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { return nullptr; } - if (Lex.Lex() != tgtok::comma) { // eat the id + if (!consume(tgtok::comma)) { // eat the id TokError("expected ',' in ternary operator"); return nullptr; } - Lex.Lex(); // eat the ',' Init *MHS = ParseValue(CurRec); if (!MHS) return nullptr; - if (Lex.getCode() != tgtok::comma) { + if (!consume(tgtok::comma)) { TokError("expected ',' in ternary operator"); return nullptr; } - Lex.Lex(); // eat the ',' TypedInit *MHSt = dyn_cast(MHS); if (!MHSt) { @@ -1392,11 +1378,10 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { if (!RHS) return nullptr; - if (Lex.getCode() != tgtok::r_paren) { + if (!consume(tgtok::r_paren)) { TokError("expected ')' in binary operator"); return nullptr; } - Lex.Lex(); // eat the ')' RecTy *OutType; if (IsDAG) { @@ -1436,42 +1421,38 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { Code = TernOpInit::SUBST; break; } - if (Lex.getCode() != tgtok::l_paren) { + if (!consume(tgtok::l_paren)) { TokError("expected '(' after ternary operator"); return nullptr; } - Lex.Lex(); // eat the '(' Init *LHS = ParseValue(CurRec); if (!LHS) return nullptr; - if (Lex.getCode() != tgtok::comma) { + if (!consume(tgtok::comma)) { TokError("expected ',' in ternary operator"); return nullptr; } - Lex.Lex(); // eat the ',' SMLoc MHSLoc = Lex.getLoc(); Init *MHS = ParseValue(CurRec, ItemType); if (!MHS) return nullptr; - if (Lex.getCode() != tgtok::comma) { + if (!consume(tgtok::comma)) { TokError("expected ',' in ternary operator"); return nullptr; } - Lex.Lex(); // eat the ',' SMLoc RHSLoc = Lex.getLoc(); Init *RHS = ParseValue(CurRec, ItemType); if (!RHS) return nullptr; - if (Lex.getCode() != tgtok::r_paren) { + if (!consume(tgtok::r_paren)) { TokError("expected ')' in binary operator"); return nullptr; } - Lex.Lex(); // eat the ')' switch (LexCode) { default: llvm_unreachable("Unhandled code!"); @@ -1561,11 +1542,10 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { case tgtok::XFoldl: { // Value ::= !foldl '(' Id ',' Id ',' Value ',' Value ',' Value ')' Lex.Lex(); // eat the operation - if (Lex.getCode() != tgtok::l_paren) { + if (!consume(tgtok::l_paren)) { TokError("expected '(' after !foldl"); return nullptr; } - Lex.Lex(); // eat the '(' Init *StartUntyped = ParseValue(CurRec); if (!StartUntyped) @@ -1578,11 +1558,10 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { return nullptr; } - if (Lex.getCode() != tgtok::comma) { + if (!consume(tgtok::comma)) { TokError("expected ',' in !foldl"); return nullptr; } - Lex.Lex(); // eat the ',' Init *ListUntyped = ParseValue(CurRec); if (!ListUntyped) @@ -1674,11 +1653,10 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { return nullptr; } - if (Lex.getCode() != tgtok::r_paren) { + if (!consume(tgtok::r_paren)) { TokError("expected ')' in fold operator"); return nullptr; } - Lex.Lex(); // eat the ')' return FoldOpInit::get(Start, List, A, B, Expr, Start->getType()) ->Fold(CurRec); @@ -1694,11 +1672,10 @@ Init *TGParser::ParseOperation(Record *CurRec, RecTy *ItemType) { RecTy *TGParser::ParseOperatorType() { RecTy *Type = nullptr; - if (Lex.getCode() != tgtok::less) { + if (!consume(tgtok::less)) { TokError("expected type name for operator"); return nullptr; } - Lex.Lex(); // eat the < Type = ParseType(); @@ -1707,11 +1684,10 @@ RecTy *TGParser::ParseOperatorType() { return nullptr; } - if (Lex.getCode() != tgtok::greater) { + if (!consume(tgtok::greater)) { TokError("expected type name for operator"); return nullptr; } - Lex.Lex(); // eat the > return Type; } @@ -1719,47 +1695,40 @@ RecTy *TGParser::ParseOperatorType() { Init *TGParser::ParseOperationCond(Record *CurRec, RecTy *ItemType) { Lex.Lex(); // eat the operation 'cond' - if (Lex.getCode() != tgtok::l_paren) { - TokError("expected '(' after !cond operator"); - return nullptr; + if (!consume(tgtok::l_paren)) { + TokError("expected '(' after !cond operator"); + return nullptr; } - Lex.Lex(); // eat the '(' // Parse through '[Case: Val,]+' SmallVector Case; SmallVector Val; while (true) { - if (Lex.getCode() == tgtok::r_paren) { - Lex.Lex(); // eat the ')' + if (consume(tgtok::r_paren)) break; - } Init *V = ParseValue(CurRec); if (!V) return nullptr; Case.push_back(V); - if (Lex.getCode() != tgtok::colon) { + if (!consume(tgtok::colon)) { TokError("expected ':' following a condition in !cond operator"); return nullptr; } - Lex.Lex(); // eat the ':' V = ParseValue(CurRec, ItemType); if (!V) return nullptr; Val.push_back(V); - if (Lex.getCode() == tgtok::r_paren) { - Lex.Lex(); // eat the ')' + if (consume(tgtok::r_paren)) break; - } - if (Lex.getCode() != tgtok::comma) { + if (!consume(tgtok::comma)) { TokError("expected ',' or ')' following a value in !cond operator"); return nullptr; } - Lex.Lex(); // eat the ',' } if (Case.size() < 1) { @@ -1883,11 +1852,10 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType, ParseValueList(Args, CurRec, Class); if (Args.empty()) return nullptr; - if (Lex.getCode() != tgtok::greater) { + if (!consume(tgtok::greater)) { TokError("expected '>' at end of value list"); return nullptr; } - Lex.Lex(); // eat the '>' // Typecheck the template arguments list ArrayRef ExpectedArgs = Class->getTemplateArgs(); @@ -1996,28 +1964,24 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType, GivenListTy ? GivenListTy->getElementType() : nullptr); if (Vals.empty()) return nullptr; } - if (Lex.getCode() != tgtok::r_square) { + if (!consume(tgtok::r_square)) { TokError("expected ']' at end of list value"); return nullptr; } - Lex.Lex(); // eat the ']' RecTy *GivenEltTy = nullptr; - if (Lex.getCode() == tgtok::less) { + if (consume(tgtok::less)) { // Optional list element type - Lex.Lex(); // eat the '<' - GivenEltTy = ParseType(); if (!GivenEltTy) { // Couldn't parse element type return nullptr; } - if (Lex.getCode() != tgtok::greater) { + if (!consume(tgtok::greater)) { TokError("expected '>' at end of list element type"); return nullptr; } - Lex.Lex(); // eat the '>' } // Check elements @@ -2082,8 +2046,8 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType, // If the operator name is present, parse it. StringInit *OperatorName = nullptr; - if (Lex.getCode() == tgtok::colon) { - if (Lex.Lex() != tgtok::VarName) { // eat the ':' + if (consume(tgtok::colon)) { + if (Lex.getCode() != tgtok::VarName) { // eat the ':' TokError("expected variable name in dag operator"); return nullptr; } @@ -2097,11 +2061,10 @@ Init *TGParser::ParseSimpleValue(Record *CurRec, RecTy *ItemType, if (DagArgs.empty()) return nullptr; } - if (Lex.getCode() != tgtok::r_paren) { + if (!consume(tgtok::r_paren)) { TokError("expected ')' in dag init"); return nullptr; } - Lex.Lex(); // eat the ')' return DagInit::get(Operator, OperatorName, DagArgs); } @@ -2179,11 +2142,10 @@ Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) { } // Eat the '}'. - if (Lex.getCode() != tgtok::r_brace) { + if (!consume(tgtok::r_brace)) { TokError("expected '}' at end of bit range list"); return nullptr; } - Lex.Lex(); break; } case tgtok::l_square: { @@ -2200,11 +2162,10 @@ Init *TGParser::ParseValue(Record *CurRec, RecTy *ItemType, IDParseMode Mode) { } // Eat the ']'. - if (Lex.getCode() != tgtok::r_square) { + if (!consume(tgtok::r_square)) { TokError("expected ']' at end of list slice"); return nullptr; } - Lex.Lex(); break; } case tgtok::period: { @@ -2345,8 +2306,8 @@ void TGParser::ParseDagArgList( Result.push_back(std::make_pair(Val, VarName)); } - if (Lex.getCode() != tgtok::comma) break; - Lex.Lex(); // eat the ',' + if (!consume(tgtok::comma)) + break; } } @@ -2382,9 +2343,7 @@ void TGParser::ParseValueList(SmallVectorImpl &Result, Record *CurRec, return; } - while (Lex.getCode() == tgtok::comma) { - Lex.Lex(); // Eat the comma - + while (consume(tgtok::comma)) { // ignore trailing comma for lists if (Lex.getCode() == tgtok::r_square) return; @@ -2422,8 +2381,7 @@ void TGParser::ParseValueList(SmallVectorImpl &Result, Record *CurRec, Init *TGParser::ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs) { // Read the field prefix if present. - bool HasField = Lex.getCode() == tgtok::Field; - if (HasField) Lex.Lex(); + bool HasField = consume(tgtok::Field); RecTy *Type = ParseType(); if (!Type) return nullptr; @@ -2458,8 +2416,7 @@ Init *TGParser::ParseDeclaration(Record *CurRec, return nullptr; // If a value is present, parse it. - if (Lex.getCode() == tgtok::equal) { - Lex.Lex(); + if (consume(tgtok::equal)) { SMLoc ValLoc = Lex.getLoc(); Init *Val = ParseValue(CurRec, Type); if (!Val || @@ -2491,11 +2448,10 @@ VarInit *TGParser::ParseForeachDeclaration(Init *&ForeachListValue) { Lex.Lex(); // If a value is present, parse it. - if (Lex.getCode() != tgtok::equal) { + if (!consume(tgtok::equal)) { TokError("Expected '=' in foreach declaration"); return nullptr; } - Lex.Lex(); // Eat the '=' RecTy *IterType = nullptr; SmallVector Ranges; @@ -2504,11 +2460,10 @@ VarInit *TGParser::ParseForeachDeclaration(Init *&ForeachListValue) { case tgtok::l_brace: { // '{' RangeList '}' Lex.Lex(); // eat the '{' ParseRangeList(Ranges); - if (Lex.getCode() != tgtok::r_brace) { + if (!consume(tgtok::r_brace)) { TokError("expected '}' at end of bit range list"); return nullptr; } - Lex.Lex(); break; } @@ -2579,9 +2534,7 @@ bool TGParser::ParseTemplateArgList(Record *CurRec) { TheRecToAddTo->addTemplateArg(TemplArg); - while (Lex.getCode() == tgtok::comma) { - Lex.Lex(); // eat the ',' - + while (consume(tgtok::comma)) { // Read the following declarations. SMLoc Loc = Lex.getLoc(); TemplArg = ParseDeclaration(CurRec, true/*templateargs*/); @@ -2595,9 +2548,8 @@ bool TGParser::ParseTemplateArgList(Record *CurRec) { TheRecToAddTo->addTemplateArg(TemplArg); } - if (Lex.getCode() != tgtok::greater) + if (!consume(tgtok::greater)) return TokError("expected '>' at end of template argument list"); - Lex.Lex(); // eat the '>'. return false; } @@ -2667,15 +2619,11 @@ bool TGParser::ParseBodyItem(Record *CurRec) { /// bool TGParser::ParseBody(Record *CurRec) { // If this is a null definition, just eat the semi and return. - if (Lex.getCode() == tgtok::semi) { - Lex.Lex(); + if (consume(tgtok::semi)) return false; - } - if (Lex.getCode() != tgtok::l_brace) + if (!consume(tgtok::l_brace)) return TokError("Expected ';' or '{' to start body"); - // Eat the '{'. - Lex.Lex(); // An object body introduces a new scope for local variables. TGLocalVarScope *BodyScope = PushLocalScope(); @@ -2724,8 +2672,7 @@ bool TGParser::ApplyLetStack(RecordsEntry &Entry) { /// bool TGParser::ParseObjectBody(Record *CurRec) { // If there is a baseclass list, read it. - if (Lex.getCode() == tgtok::colon) { - Lex.Lex(); + if (consume(tgtok::colon)) { // Read all of the subclasses. SubClassReference SubClass = ParseSubClassReference(CurRec, false); @@ -2971,9 +2918,7 @@ bool TGParser::ParseIf(MultiClass *CurMultiClass) { // dangling-else ambiguity, and by greedily matching an else here if we can, // we implement the usual resolution of pairing with the innermost unmatched // if. - if (Lex.getCode() == tgtok::ElseKW) { - Lex.Lex(); // Eat the 'else' - + if (consume(tgtok::ElseKW)) { // The foreach containing the else-clause uses the same pair of lists as // above, but this time, selects SingletonList if the condition is *false*. Init *ElseClauseList = @@ -3200,11 +3145,9 @@ bool TGParser::ParseMultiClass() { bool inherits = false; // If there are submulticlasses, parse them. - if (Lex.getCode() == tgtok::colon) { + if (consume(tgtok::colon)) { inherits = true; - Lex.Lex(); - // Read all of the submulticlasses. SubMultiClassReference SubMultiClass = ParseSubMultiClassReference(CurMultiClass); @@ -3216,8 +3159,8 @@ bool TGParser::ParseMultiClass() { if (AddSubMultiClass(CurMultiClass, SubMultiClass)) return true; - if (Lex.getCode() != tgtok::comma) break; - Lex.Lex(); // eat ','. + if (!consume(tgtok::comma)) + break; SubMultiClass = ParseSubMultiClassReference(CurMultiClass); } } @@ -3225,9 +3168,8 @@ bool TGParser::ParseMultiClass() { if (Lex.getCode() != tgtok::l_brace) { if (!inherits) return TokError("expected '{' in multiclass definition"); - if (Lex.getCode() != tgtok::semi) + if (!consume(tgtok::semi)) return TokError("expected ';' in multiclass definition"); - Lex.Lex(); // eat the ';'. } else { if (Lex.Lex() == tgtok::r_brace) // eat the '{'. return TokError("multiclass must contain at least one def"); @@ -3334,8 +3276,8 @@ bool TGParser::ParseDefm(MultiClass *CurMultiClass) { &SubClassLoc)) return true; - if (Lex.getCode() != tgtok::comma) break; - Lex.Lex(); // eat ','. + if (!consume(tgtok::comma)) + break; if (Lex.getCode() != tgtok::Id) return TokError("expected identifier"); @@ -3368,8 +3310,8 @@ bool TGParser::ParseDefm(MultiClass *CurMultiClass) { return true; } - if (Lex.getCode() != tgtok::comma) break; - Lex.Lex(); // eat ','. + if (!consume(tgtok::comma)) + break; SubClass = ParseSubClassReference(nullptr, false); } } @@ -3381,9 +3323,8 @@ bool TGParser::ParseDefm(MultiClass *CurMultiClass) { addEntry(std::move(E)); } - if (Lex.getCode() != tgtok::semi) + if (!consume(tgtok::semi)) return TokError("expected ';' at end of defm"); - Lex.Lex(); return false; } diff --git a/llvm/lib/TableGen/TGParser.h b/llvm/lib/TableGen/TGParser.h index 872ca86..07a4003 100644 --- a/llvm/lib/TableGen/TGParser.h +++ b/llvm/lib/TableGen/TGParser.h @@ -210,6 +210,7 @@ private: // Semantic analysis methods. bool addDefOne(std::unique_ptr Rec); private: // Parser methods. + bool consume(tgtok::TokKind K); bool ParseObjectList(MultiClass *MC = nullptr); bool ParseObject(MultiClass *MC); bool ParseClass();