xref: /freebsd/contrib/llvm-project/llvm/lib/TableGen/TGParser.h (revision 18054d0220cfc8df9c9568c437bd6fbb59d53c3c)
1 //===- TGParser.h - Parser for TableGen Files -------------------*- 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 class represents the Parser for tablegen files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_LIB_TABLEGEN_TGPARSER_H
14 #define LLVM_LIB_TABLEGEN_TGPARSER_H
15 
16 #include "TGLexer.h"
17 #include "llvm/TableGen/Error.h"
18 #include "llvm/TableGen/Record.h"
19 #include <map>
20 
21 namespace llvm {
22   class SourceMgr;
23   class Twine;
24   struct ForeachLoop;
25   struct MultiClass;
26   struct SubClassReference;
27   struct SubMultiClassReference;
28 
29   struct LetRecord {
30     StringInit *Name;
31     std::vector<unsigned> Bits;
32     Init *Value;
33     SMLoc Loc;
34     LetRecord(StringInit *N, ArrayRef<unsigned> B, Init *V, SMLoc L)
35       : Name(N), Bits(B), Value(V), Loc(L) {
36     }
37   };
38 
39   /// RecordsEntry - Holds exactly one of a Record, ForeachLoop, or
40   /// AssertionInfo.
41   struct RecordsEntry {
42     std::unique_ptr<Record> Rec;
43     std::unique_ptr<ForeachLoop> Loop;
44     std::unique_ptr<Record::AssertionInfo> Assertion;
45 
46     void dump() const;
47 
48     RecordsEntry() {}
49     RecordsEntry(std::unique_ptr<Record> Rec) : Rec(std::move(Rec)) {}
50     RecordsEntry(std::unique_ptr<ForeachLoop> Loop)
51         : Loop(std::move(Loop)) {}
52     RecordsEntry(std::unique_ptr<Record::AssertionInfo> Assertion)
53         : Assertion(std::move(Assertion)) {}
54   };
55 
56   /// 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 is
60   /// defined in the loop at all. (This happens when a ForeachLoop is
61   /// constructed by desugaring an if statement.)
62   struct ForeachLoop {
63     SMLoc Loc;
64     VarInit *IterVar;
65     Init *ListValue;
66     std::vector<RecordsEntry> Entries;
67 
68     void dump() const;
69 
70     ForeachLoop(SMLoc Loc, VarInit *IVar, Init *LValue)
71       : Loc(Loc), IterVar(IVar), ListValue(LValue) {}
72   };
73 
74   struct DefsetRecord {
75     SMLoc Loc;
76     RecTy *EltTy = nullptr;
77     SmallVector<Init *, 16> Elements;
78   };
79 
80 class TGLocalVarScope {
81   // A scope to hold local variable definitions from defvar.
82   std::map<std::string, Init *, std::less<>> vars;
83   std::unique_ptr<TGLocalVarScope> parent;
84 
85 public:
86   TGLocalVarScope() = default;
87   TGLocalVarScope(std::unique_ptr<TGLocalVarScope> parent)
88       : parent(std::move(parent)) {}
89 
90   std::unique_ptr<TGLocalVarScope> extractParent() {
91     // This is expected to be called just before we are destructed, so
92     // it doesn't much matter what state we leave 'parent' in.
93     return std::move(parent);
94   }
95 
96   Init *getVar(StringRef Name) const {
97     auto It = vars.find(Name);
98     if (It != vars.end())
99       return It->second;
100     if (parent)
101       return parent->getVar(Name);
102     return nullptr;
103   }
104 
105   bool varAlreadyDefined(StringRef Name) const {
106     // When we check whether a variable is already defined, for the purpose of
107     // reporting an error on redefinition, we don't look up to the parent
108     // scope, because it's all right to shadow an outer definition with an
109     // inner one.
110     return vars.find(Name) != vars.end();
111   }
112 
113   void addVar(StringRef Name, Init *I) {
114     bool Ins = vars.insert(std::make_pair(std::string(Name), I)).second;
115     (void)Ins;
116     assert(Ins && "Local variable already exists");
117   }
118 };
119 
120 struct MultiClass {
121   Record Rec;  // Placeholder for template args and Name.
122   std::vector<RecordsEntry> Entries;
123 
124   void dump() const;
125 
126   MultiClass(StringRef Name, SMLoc Loc, RecordKeeper &Records) :
127     Rec(Name, Loc, Records) {}
128 };
129 
130 class TGParser {
131   TGLexer Lex;
132   std::vector<SmallVector<LetRecord, 4>> LetStack;
133   std::map<std::string, std::unique_ptr<MultiClass>> MultiClasses;
134 
135   /// Loops - Keep track of any foreach loops we are within.
136   ///
137   std::vector<std::unique_ptr<ForeachLoop>> Loops;
138 
139   SmallVector<DefsetRecord *, 2> Defsets;
140 
141   /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
142   /// current value.
143   MultiClass *CurMultiClass;
144 
145   /// CurLocalScope - Innermost of the current nested scopes for 'defvar' local
146   /// variables.
147   std::unique_ptr<TGLocalVarScope> CurLocalScope;
148 
149   // Record tracker
150   RecordKeeper &Records;
151 
152   // A "named boolean" indicating how to parse identifiers.  Usually
153   // identifiers map to some existing object but in special cases
154   // (e.g. parsing def names) no such object exists yet because we are
155   // in the middle of creating in.  For those situations, allow the
156   // parser to ignore missing object errors.
157   enum IDParseMode {
158     ParseValueMode,   // We are parsing a value we expect to look up.
159     ParseNameMode,    // We are parsing a name of an object that does not yet
160                       // exist.
161   };
162 
163   bool NoWarnOnUnusedTemplateArgs = false;
164 
165 public:
166   TGParser(SourceMgr &SM, ArrayRef<std::string> Macros, RecordKeeper &records,
167            const bool NoWarnOnUnusedTemplateArgs = false)
168       : Lex(SM, Macros), CurMultiClass(nullptr), Records(records),
169         NoWarnOnUnusedTemplateArgs(NoWarnOnUnusedTemplateArgs) {}
170 
171   /// ParseFile - Main entrypoint for parsing a tblgen file.  These parser
172   /// routines return true on error, or false on success.
173   bool ParseFile();
174 
175   bool Error(SMLoc L, const Twine &Msg) const {
176     PrintError(L, Msg);
177     return true;
178   }
179   bool TokError(const Twine &Msg) const {
180     return Error(Lex.getLoc(), Msg);
181   }
182   const TGLexer::DependenciesSetTy &getDependencies() const {
183     return Lex.getDependencies();
184   }
185 
186   TGLocalVarScope *PushLocalScope() {
187     CurLocalScope = std::make_unique<TGLocalVarScope>(std::move(CurLocalScope));
188     // Returns a pointer to the new scope, so that the caller can pass it back
189     // to PopLocalScope which will check by assertion that the pushes and pops
190     // match up properly.
191     return CurLocalScope.get();
192   }
193   void PopLocalScope(TGLocalVarScope *ExpectedStackTop) {
194     assert(ExpectedStackTop == CurLocalScope.get() &&
195            "Mismatched pushes and pops of local variable scopes");
196     CurLocalScope = CurLocalScope->extractParent();
197   }
198 
199 private: // Semantic analysis methods.
200   bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
201   bool SetValue(Record *TheRec, SMLoc Loc, Init *ValName,
202                 ArrayRef<unsigned> BitList, Init *V,
203                 bool AllowSelfAssignment = false);
204   bool AddSubClass(Record *Rec, SubClassReference &SubClass);
205   bool AddSubClass(RecordsEntry &Entry, SubClassReference &SubClass);
206   bool AddSubMultiClass(MultiClass *CurMC,
207                         SubMultiClassReference &SubMultiClass);
208 
209   using SubstStack = SmallVector<std::pair<Init *, Init *>, 8>;
210 
211   bool addEntry(RecordsEntry E);
212   bool resolve(const ForeachLoop &Loop, SubstStack &Stack, bool Final,
213                std::vector<RecordsEntry> *Dest, SMLoc *Loc = nullptr);
214   bool resolve(const std::vector<RecordsEntry> &Source, SubstStack &Substs,
215                bool Final, std::vector<RecordsEntry> *Dest,
216                SMLoc *Loc = nullptr);
217   bool addDefOne(std::unique_ptr<Record> Rec);
218 
219 private:  // Parser methods.
220   bool consume(tgtok::TokKind K);
221   bool ParseObjectList(MultiClass *MC = nullptr);
222   bool ParseObject(MultiClass *MC);
223   bool ParseClass();
224   bool ParseMultiClass();
225   bool ParseDefm(MultiClass *CurMultiClass);
226   bool ParseDef(MultiClass *CurMultiClass);
227   bool ParseDefset();
228   bool ParseDefvar();
229   bool ParseForeach(MultiClass *CurMultiClass);
230   bool ParseIf(MultiClass *CurMultiClass);
231   bool ParseIfBody(MultiClass *CurMultiClass, StringRef Kind);
232   bool ParseAssert(MultiClass *CurMultiClass, Record *CurRec = nullptr);
233   bool ParseTopLevelLet(MultiClass *CurMultiClass);
234   void ParseLetList(SmallVectorImpl<LetRecord> &Result);
235 
236   bool ParseObjectBody(Record *CurRec);
237   bool ParseBody(Record *CurRec);
238   bool ParseBodyItem(Record *CurRec);
239 
240   bool ParseTemplateArgList(Record *CurRec);
241   Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
242   VarInit *ParseForeachDeclaration(Init *&ForeachListValue);
243 
244   SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
245   SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
246 
247   Init *ParseIDValue(Record *CurRec, StringInit *Name, SMLoc NameLoc,
248                      IDParseMode Mode = ParseValueMode);
249   Init *ParseSimpleValue(Record *CurRec, RecTy *ItemType = nullptr,
250                          IDParseMode Mode = ParseValueMode);
251   Init *ParseValue(Record *CurRec, RecTy *ItemType = nullptr,
252                    IDParseMode Mode = ParseValueMode);
253   void ParseValueList(SmallVectorImpl<llvm::Init*> &Result,
254                       Record *CurRec, RecTy *ItemType = nullptr);
255   bool ParseTemplateArgValueList(SmallVectorImpl<llvm::Init *> &Result,
256                                  Record *CurRec, Record *ArgsRec);
257   void ParseDagArgList(
258       SmallVectorImpl<std::pair<llvm::Init*, StringInit*>> &Result,
259       Record *CurRec);
260   bool ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges);
261   bool ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges);
262   void ParseRangeList(SmallVectorImpl<unsigned> &Result);
263   bool ParseRangePiece(SmallVectorImpl<unsigned> &Ranges,
264                        TypedInit *FirstItem = nullptr);
265   RecTy *ParseType();
266   Init *ParseOperation(Record *CurRec, RecTy *ItemType);
267   Init *ParseOperationSubstr(Record *CurRec, RecTy *ItemType);
268   Init *ParseOperationFind(Record *CurRec, RecTy *ItemType);
269   Init *ParseOperationForEachFilter(Record *CurRec, RecTy *ItemType);
270   Init *ParseOperationCond(Record *CurRec, RecTy *ItemType);
271   RecTy *ParseOperatorType();
272   Init *ParseObjectName(MultiClass *CurMultiClass);
273   Record *ParseClassID();
274   MultiClass *ParseMultiClassID();
275   bool ApplyLetStack(Record *CurRec);
276   bool ApplyLetStack(RecordsEntry &Entry);
277   bool CheckTemplateArgValues(SmallVectorImpl<llvm::Init *> &Values,
278                               SMLoc Loc, Record *ArgsRec);
279 };
280 
281 } // end namespace llvm
282 
283 #endif
284