Path: blob/main/contrib/llvm-project/llvm/lib/TableGen/TGParser.h
35234 views
//===- TGParser.h - Parser for TableGen Files -------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This class represents the Parser for tablegen files.9//10//===----------------------------------------------------------------------===//1112#ifndef LLVM_LIB_TABLEGEN_TGPARSER_H13#define LLVM_LIB_TABLEGEN_TGPARSER_H1415#include "TGLexer.h"16#include "llvm/TableGen/Error.h"17#include "llvm/TableGen/Record.h"18#include <map>1920namespace llvm {21class SourceMgr;22class Twine;23struct ForeachLoop;24struct MultiClass;25struct SubClassReference;26struct SubMultiClassReference;2728struct LetRecord {29StringInit *Name;30std::vector<unsigned> Bits;31Init *Value;32SMLoc Loc;33LetRecord(StringInit *N, ArrayRef<unsigned> B, Init *V, SMLoc L)34: Name(N), Bits(B), Value(V), Loc(L) {}35};3637/// RecordsEntry - Holds exactly one of a Record, ForeachLoop, or38/// AssertionInfo.39struct RecordsEntry {40std::unique_ptr<Record> Rec;41std::unique_ptr<ForeachLoop> Loop;42std::unique_ptr<Record::AssertionInfo> Assertion;43std::unique_ptr<Record::DumpInfo> Dump;4445void dump() const;4647RecordsEntry() = default;48RecordsEntry(std::unique_ptr<Record> Rec) : Rec(std::move(Rec)) {}49RecordsEntry(std::unique_ptr<ForeachLoop> Loop) : Loop(std::move(Loop)) {}50RecordsEntry(std::unique_ptr<Record::AssertionInfo> Assertion)51: Assertion(std::move(Assertion)) {}52RecordsEntry(std::unique_ptr<Record::DumpInfo> Dump)53: Dump(std::move(Dump)) {}54};5556/// ForeachLoop - Record the iteration state associated with a for loop.57/// This is used to instantiate items in the loop body.58///59/// IterVar is allowed to be null, in which case no iteration variable is60/// defined in the loop at all. (This happens when a ForeachLoop is61/// constructed by desugaring an if statement.)62struct ForeachLoop {63SMLoc Loc;64VarInit *IterVar;65Init *ListValue;66std::vector<RecordsEntry> Entries;6768void dump() const;6970ForeachLoop(SMLoc Loc, VarInit *IVar, Init *LValue)71: Loc(Loc), IterVar(IVar), ListValue(LValue) {}72};7374struct DefsetRecord {75SMLoc Loc;76RecTy *EltTy = nullptr;77SmallVector<Init *, 16> Elements;78};7980struct MultiClass {81Record Rec; // Placeholder for template args and Name.82std::vector<RecordsEntry> Entries;8384void dump() const;8586MultiClass(StringRef Name, SMLoc Loc, RecordKeeper &Records)87: Rec(Name, Loc, Records, Record::RK_MultiClass) {}88};8990class TGVarScope {91public:92enum ScopeKind { SK_Local, SK_Record, SK_ForeachLoop, SK_MultiClass };9394private:95ScopeKind Kind;96std::unique_ptr<TGVarScope> Parent;97// A scope to hold variable definitions from defvar.98std::map<std::string, Init *, std::less<>> Vars;99Record *CurRec = nullptr;100ForeachLoop *CurLoop = nullptr;101MultiClass *CurMultiClass = nullptr;102103public:104TGVarScope(std::unique_ptr<TGVarScope> Parent)105: Kind(SK_Local), Parent(std::move(Parent)) {}106TGVarScope(std::unique_ptr<TGVarScope> Parent, Record *Rec)107: Kind(SK_Record), Parent(std::move(Parent)), CurRec(Rec) {}108TGVarScope(std::unique_ptr<TGVarScope> Parent, ForeachLoop *Loop)109: Kind(SK_ForeachLoop), Parent(std::move(Parent)), CurLoop(Loop) {}110TGVarScope(std::unique_ptr<TGVarScope> Parent, MultiClass *Multiclass)111: Kind(SK_MultiClass), Parent(std::move(Parent)),112CurMultiClass(Multiclass) {}113114std::unique_ptr<TGVarScope> extractParent() {115// This is expected to be called just before we are destructed, so116// it doesn't much matter what state we leave 'parent' in.117return std::move(Parent);118}119120Init *getVar(RecordKeeper &Records, MultiClass *ParsingMultiClass,121StringInit *Name, SMRange NameLoc,122bool TrackReferenceLocs) const;123124bool varAlreadyDefined(StringRef Name) const {125// When we check whether a variable is already defined, for the purpose of126// reporting an error on redefinition, we don't look up to the parent127// scope, because it's all right to shadow an outer definition with an128// inner one.129return Vars.find(Name) != Vars.end();130}131132void addVar(StringRef Name, Init *I) {133bool Ins = Vars.insert(std::make_pair(std::string(Name), I)).second;134(void)Ins;135assert(Ins && "Local variable already exists");136}137138bool isOutermost() const { return Parent == nullptr; }139};140141class TGParser {142TGLexer Lex;143std::vector<SmallVector<LetRecord, 4>> LetStack;144std::map<std::string, std::unique_ptr<MultiClass>> MultiClasses;145std::map<std::string, RecTy *> TypeAliases;146147/// Loops - Keep track of any foreach loops we are within.148///149std::vector<std::unique_ptr<ForeachLoop>> Loops;150151SmallVector<DefsetRecord *, 2> Defsets;152153/// CurMultiClass - If we are parsing a 'multiclass' definition, this is the154/// current value.155MultiClass *CurMultiClass;156157/// CurScope - Innermost of the current nested scopes for 'defvar' variables.158std::unique_ptr<TGVarScope> CurScope;159160// Record tracker161RecordKeeper &Records;162163// A "named boolean" indicating how to parse identifiers. Usually164// identifiers map to some existing object but in special cases165// (e.g. parsing def names) no such object exists yet because we are166// in the middle of creating in. For those situations, allow the167// parser to ignore missing object errors.168enum IDParseMode {169ParseValueMode, // We are parsing a value we expect to look up.170ParseNameMode, // We are parsing a name of an object that does not yet171// exist.172};173174bool NoWarnOnUnusedTemplateArgs = false;175bool TrackReferenceLocs = false;176177public:178TGParser(SourceMgr &SM, ArrayRef<std::string> Macros, RecordKeeper &records,179const bool NoWarnOnUnusedTemplateArgs = false,180const bool TrackReferenceLocs = false)181: Lex(SM, Macros), CurMultiClass(nullptr), Records(records),182NoWarnOnUnusedTemplateArgs(NoWarnOnUnusedTemplateArgs),183TrackReferenceLocs(TrackReferenceLocs) {}184185/// ParseFile - Main entrypoint for parsing a tblgen file. These parser186/// routines return true on error, or false on success.187bool ParseFile();188189bool Error(SMLoc L, const Twine &Msg) const {190PrintError(L, Msg);191return true;192}193bool TokError(const Twine &Msg) const {194return Error(Lex.getLoc(), Msg);195}196const TGLexer::DependenciesSetTy &getDependencies() const {197return Lex.getDependencies();198}199200TGVarScope *PushScope() {201CurScope = std::make_unique<TGVarScope>(std::move(CurScope));202// Returns a pointer to the new scope, so that the caller can pass it back203// to PopScope which will check by assertion that the pushes and pops204// match up properly.205return CurScope.get();206}207TGVarScope *PushScope(Record *Rec) {208CurScope = std::make_unique<TGVarScope>(std::move(CurScope), Rec);209return CurScope.get();210}211TGVarScope *PushScope(ForeachLoop *Loop) {212CurScope = std::make_unique<TGVarScope>(std::move(CurScope), Loop);213return CurScope.get();214}215TGVarScope *PushScope(MultiClass *Multiclass) {216CurScope = std::make_unique<TGVarScope>(std::move(CurScope), Multiclass);217return CurScope.get();218}219void PopScope(TGVarScope *ExpectedStackTop) {220assert(ExpectedStackTop == CurScope.get() &&221"Mismatched pushes and pops of local variable scopes");222CurScope = CurScope->extractParent();223}224225private: // Semantic analysis methods.226bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);227/// Set the value of a RecordVal within the given record. If `OverrideDefLoc`228/// is set, the provided location overrides any existing location of the229/// RecordVal.230bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,231ArrayRef<unsigned> BitList, Init *V,232bool AllowSelfAssignment = false, bool OverrideDefLoc = true);233bool AddSubClass(Record *Rec, SubClassReference &SubClass);234bool AddSubClass(RecordsEntry &Entry, SubClassReference &SubClass);235bool AddSubMultiClass(MultiClass *CurMC,236SubMultiClassReference &SubMultiClass);237238using SubstStack = SmallVector<std::pair<Init *, Init *>, 8>;239240bool addEntry(RecordsEntry E);241bool resolve(const ForeachLoop &Loop, SubstStack &Stack, bool Final,242std::vector<RecordsEntry> *Dest, SMLoc *Loc = nullptr);243bool resolve(const std::vector<RecordsEntry> &Source, SubstStack &Substs,244bool Final, std::vector<RecordsEntry> *Dest,245SMLoc *Loc = nullptr);246bool addDefOne(std::unique_ptr<Record> Rec);247248using ArgValueHandler = std::function<void(Init *, Init *)>;249bool resolveArguments(250Record *Rec, ArrayRef<ArgumentInit *> ArgValues, SMLoc Loc,251ArgValueHandler ArgValueHandler = [](Init *, Init *) {});252bool resolveArgumentsOfClass(MapResolver &R, Record *Rec,253ArrayRef<ArgumentInit *> ArgValues, SMLoc Loc);254bool resolveArgumentsOfMultiClass(SubstStack &Substs, MultiClass *MC,255ArrayRef<ArgumentInit *> ArgValues,256Init *DefmName, SMLoc Loc);257258private: // Parser methods.259bool consume(tgtok::TokKind K);260bool ParseObjectList(MultiClass *MC = nullptr);261bool ParseObject(MultiClass *MC);262bool ParseClass();263bool ParseMultiClass();264bool ParseDefm(MultiClass *CurMultiClass);265bool ParseDef(MultiClass *CurMultiClass);266bool ParseDefset();267bool ParseDeftype();268bool ParseDefvar(Record *CurRec = nullptr);269bool ParseDump(MultiClass *CurMultiClass, Record *CurRec = nullptr);270bool ParseForeach(MultiClass *CurMultiClass);271bool ParseIf(MultiClass *CurMultiClass);272bool ParseIfBody(MultiClass *CurMultiClass, StringRef Kind);273bool ParseAssert(MultiClass *CurMultiClass, Record *CurRec = nullptr);274bool ParseTopLevelLet(MultiClass *CurMultiClass);275void ParseLetList(SmallVectorImpl<LetRecord> &Result);276277bool ParseObjectBody(Record *CurRec);278bool ParseBody(Record *CurRec);279bool ParseBodyItem(Record *CurRec);280281bool ParseTemplateArgList(Record *CurRec);282Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);283VarInit *ParseForeachDeclaration(Init *&ForeachListValue);284285SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);286SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);287288Init *ParseIDValue(Record *CurRec, StringInit *Name, SMRange NameLoc,289IDParseMode Mode = ParseValueMode);290Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = nullptr,291IDParseMode Mode = ParseValueMode);292Init *ParseValue(Record *CurRec, RecTy *ItemType = nullptr,293IDParseMode Mode = ParseValueMode);294void ParseValueList(SmallVectorImpl<llvm::Init*> &Result,295Record *CurRec, RecTy *ItemType = nullptr);296bool ParseTemplateArgValueList(SmallVectorImpl<llvm::ArgumentInit *> &Result,297Record *CurRec, Record *ArgsRec);298void ParseDagArgList(299SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,300Record *CurRec);301bool ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges);302bool ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges);303TypedInit *ParseSliceElement(Record *CurRec);304TypedInit *ParseSliceElements(Record *CurRec, bool Single = false);305void ParseRangeList(SmallVectorImpl<unsigned> &Result);306bool ParseRangePiece(SmallVectorImpl<unsigned> &Ranges,307TypedInit *FirstItem = nullptr);308RecTy *ParseType();309Init *ParseOperation(Record *CurRec, RecTy *ItemType);310Init *ParseOperationSubstr(Record *CurRec, RecTy *ItemType);311Init *ParseOperationFind(Record *CurRec, RecTy *ItemType);312Init *ParseOperationForEachFilter(Record *CurRec, RecTy *ItemType);313Init *ParseOperationCond(Record *CurRec, RecTy *ItemType);314RecTy *ParseOperatorType();315Init *ParseObjectName(MultiClass *CurMultiClass);316Record *ParseClassID();317MultiClass *ParseMultiClassID();318bool ApplyLetStack(Record *CurRec);319bool ApplyLetStack(RecordsEntry &Entry);320bool CheckTemplateArgValues(SmallVectorImpl<llvm::ArgumentInit *> &Values,321SMLoc Loc, Record *ArgsRec);322};323324} // end namespace llvm325326#endif327328329