[ControlHeightReduction] Add assert to avoid underflow (#116339)
[llvm-project.git] / llvm / lib / TableGen / TGParser.h
blobcac1ba827f113892c5e5d84d465de0fd79146e32
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.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_LIB_TABLEGEN_TGPARSER_H
14 #define LLVM_LIB_TABLEGEN_TGPARSER_H
16 #include "TGLexer.h"
17 #include "llvm/TableGen/Error.h"
18 #include "llvm/TableGen/Record.h"
19 #include <map>
21 namespace llvm {
22 class SourceMgr;
23 class Twine;
24 struct ForeachLoop;
25 struct MultiClass;
26 struct SubClassReference;
27 struct SubMultiClassReference;
29 struct LetRecord {
30 const StringInit *Name;
31 std::vector<unsigned> Bits;
32 const Init *Value;
33 SMLoc Loc;
34 LetRecord(const StringInit *N, ArrayRef<unsigned> B, const Init *V, SMLoc L)
35 : Name(N), Bits(B), Value(V), Loc(L) {}
38 /// RecordsEntry - Holds exactly one of a Record, ForeachLoop, or
39 /// AssertionInfo.
40 struct RecordsEntry {
41 std::unique_ptr<Record> Rec;
42 std::unique_ptr<ForeachLoop> Loop;
43 std::unique_ptr<Record::AssertionInfo> Assertion;
44 std::unique_ptr<Record::DumpInfo> Dump;
46 void dump() const;
48 RecordsEntry() = default;
49 RecordsEntry(std::unique_ptr<Record> Rec) : Rec(std::move(Rec)) {}
50 RecordsEntry(std::unique_ptr<ForeachLoop> Loop) : Loop(std::move(Loop)) {}
51 RecordsEntry(std::unique_ptr<Record::AssertionInfo> Assertion)
52 : Assertion(std::move(Assertion)) {}
53 RecordsEntry(std::unique_ptr<Record::DumpInfo> Dump)
54 : Dump(std::move(Dump)) {}
57 /// ForeachLoop - Record the iteration state associated with a for loop.
58 /// This is used to instantiate items in the loop body.
59 ///
60 /// IterVar is allowed to be null, in which case no iteration variable is
61 /// defined in the loop at all. (This happens when a ForeachLoop is
62 /// constructed by desugaring an if statement.)
63 struct ForeachLoop {
64 SMLoc Loc;
65 const VarInit *IterVar;
66 const Init *ListValue;
67 std::vector<RecordsEntry> Entries;
69 void dump() const;
71 ForeachLoop(SMLoc Loc, const VarInit *IVar, const Init *LValue)
72 : Loc(Loc), IterVar(IVar), ListValue(LValue) {}
75 struct DefsetRecord {
76 SMLoc Loc;
77 const RecTy *EltTy = nullptr;
78 SmallVector<Init *, 16> Elements;
81 struct MultiClass {
82 Record Rec; // Placeholder for template args and Name.
83 std::vector<RecordsEntry> Entries;
85 void dump() const;
87 MultiClass(StringRef Name, SMLoc Loc, RecordKeeper &Records)
88 : Rec(Name, Loc, Records, Record::RK_MultiClass) {}
91 class TGVarScope {
92 public:
93 enum ScopeKind { SK_Local, SK_Record, SK_ForeachLoop, SK_MultiClass };
95 private:
96 ScopeKind Kind;
97 std::unique_ptr<TGVarScope> Parent;
98 // A scope to hold variable definitions from defvar.
99 std::map<std::string, const Init *, std::less<>> Vars;
100 Record *CurRec = nullptr;
101 ForeachLoop *CurLoop = nullptr;
102 MultiClass *CurMultiClass = nullptr;
104 public:
105 TGVarScope(std::unique_ptr<TGVarScope> Parent)
106 : Kind(SK_Local), Parent(std::move(Parent)) {}
107 TGVarScope(std::unique_ptr<TGVarScope> Parent, Record *Rec)
108 : Kind(SK_Record), Parent(std::move(Parent)), CurRec(Rec) {}
109 TGVarScope(std::unique_ptr<TGVarScope> Parent, ForeachLoop *Loop)
110 : Kind(SK_ForeachLoop), Parent(std::move(Parent)), CurLoop(Loop) {}
111 TGVarScope(std::unique_ptr<TGVarScope> Parent, MultiClass *Multiclass)
112 : Kind(SK_MultiClass), Parent(std::move(Parent)),
113 CurMultiClass(Multiclass) {}
115 std::unique_ptr<TGVarScope> extractParent() {
116 // This is expected to be called just before we are destructed, so
117 // it doesn't much matter what state we leave 'parent' in.
118 return std::move(Parent);
121 const Init *getVar(RecordKeeper &Records, MultiClass *ParsingMultiClass,
122 const StringInit *Name, SMRange NameLoc,
123 bool TrackReferenceLocs) const;
125 bool varAlreadyDefined(StringRef Name) const {
126 // When we check whether a variable is already defined, for the purpose of
127 // reporting an error on redefinition, we don't look up to the parent
128 // scope, because it's all right to shadow an outer definition with an
129 // inner one.
130 return Vars.find(Name) != Vars.end();
133 void addVar(StringRef Name, const Init *I) {
134 bool Ins = Vars.insert(std::make_pair(std::string(Name), I)).second;
135 (void)Ins;
136 assert(Ins && "Local variable already exists");
139 bool isOutermost() const { return Parent == nullptr; }
142 class TGParser {
143 TGLexer Lex;
144 std::vector<SmallVector<LetRecord, 4>> LetStack;
145 std::map<std::string, std::unique_ptr<MultiClass>> MultiClasses;
146 std::map<std::string, const RecTy *> TypeAliases;
148 /// Loops - Keep track of any foreach loops we are within.
150 std::vector<std::unique_ptr<ForeachLoop>> Loops;
152 SmallVector<DefsetRecord *, 2> Defsets;
154 /// CurMultiClass - If we are parsing a 'multiclass' definition, this is the
155 /// current value.
156 MultiClass *CurMultiClass;
158 /// CurScope - Innermost of the current nested scopes for 'defvar' variables.
159 std::unique_ptr<TGVarScope> CurScope;
161 // Record tracker
162 RecordKeeper &Records;
164 // A "named boolean" indicating how to parse identifiers. Usually
165 // identifiers map to some existing object but in special cases
166 // (e.g. parsing def names) no such object exists yet because we are
167 // in the middle of creating in. For those situations, allow the
168 // parser to ignore missing object errors.
169 enum IDParseMode {
170 ParseValueMode, // We are parsing a value we expect to look up.
171 ParseNameMode, // We are parsing a name of an object that does not yet
172 // exist.
175 bool NoWarnOnUnusedTemplateArgs = false;
176 bool TrackReferenceLocs = false;
178 public:
179 TGParser(SourceMgr &SM, ArrayRef<std::string> Macros, RecordKeeper &records,
180 const bool NoWarnOnUnusedTemplateArgs = false,
181 const bool TrackReferenceLocs = false)
182 : Lex(SM, Macros), CurMultiClass(nullptr), Records(records),
183 NoWarnOnUnusedTemplateArgs(NoWarnOnUnusedTemplateArgs),
184 TrackReferenceLocs(TrackReferenceLocs) {}
186 /// ParseFile - Main entrypoint for parsing a tblgen file. These parser
187 /// routines return true on error, or false on success.
188 bool ParseFile();
190 bool Error(SMLoc L, const Twine &Msg) const {
191 PrintError(L, Msg);
192 return true;
194 bool TokError(const Twine &Msg) const {
195 return Error(Lex.getLoc(), Msg);
197 const TGLexer::DependenciesSetTy &getDependencies() const {
198 return Lex.getDependencies();
201 TGVarScope *PushScope() {
202 CurScope = std::make_unique<TGVarScope>(std::move(CurScope));
203 // Returns a pointer to the new scope, so that the caller can pass it back
204 // to PopScope which will check by assertion that the pushes and pops
205 // match up properly.
206 return CurScope.get();
208 TGVarScope *PushScope(Record *Rec) {
209 CurScope = std::make_unique<TGVarScope>(std::move(CurScope), Rec);
210 return CurScope.get();
212 TGVarScope *PushScope(ForeachLoop *Loop) {
213 CurScope = std::make_unique<TGVarScope>(std::move(CurScope), Loop);
214 return CurScope.get();
216 TGVarScope *PushScope(MultiClass *Multiclass) {
217 CurScope = std::make_unique<TGVarScope>(std::move(CurScope), Multiclass);
218 return CurScope.get();
220 void PopScope(TGVarScope *ExpectedStackTop) {
221 assert(ExpectedStackTop == CurScope.get() &&
222 "Mismatched pushes and pops of local variable scopes");
223 CurScope = CurScope->extractParent();
226 private: // Semantic analysis methods.
227 bool AddValue(Record *TheRec, SMLoc Loc, const RecordVal &RV);
228 /// Set the value of a RecordVal within the given record. If `OverrideDefLoc`
229 /// is set, the provided location overrides any existing location of the
230 /// RecordVal.
231 bool SetValue(Record *TheRec, SMLoc Loc, const Init *ValName,
232 ArrayRef<unsigned> BitList, const Init *V,
233 bool AllowSelfAssignment = false, bool OverrideDefLoc = true);
234 bool AddSubClass(Record *Rec, SubClassReference &SubClass);
235 bool AddSubClass(RecordsEntry &Entry, SubClassReference &SubClass);
236 bool AddSubMultiClass(MultiClass *CurMC,
237 SubMultiClassReference &SubMultiClass);
239 using SubstStack = SmallVector<std::pair<const Init *, const Init *>, 8>;
241 bool addEntry(RecordsEntry E);
242 bool resolve(const ForeachLoop &Loop, SubstStack &Stack, bool Final,
243 std::vector<RecordsEntry> *Dest, SMLoc *Loc = nullptr);
244 bool resolve(const std::vector<RecordsEntry> &Source, SubstStack &Substs,
245 bool Final, std::vector<RecordsEntry> *Dest,
246 SMLoc *Loc = nullptr);
247 bool addDefOne(std::unique_ptr<Record> Rec);
249 using ArgValueHandler = std::function<void(const Init *, const Init *)>;
250 bool resolveArguments(
251 const Record *Rec, ArrayRef<const ArgumentInit *> ArgValues, SMLoc Loc,
252 ArgValueHandler ArgValueHandler = [](const Init *, const Init *) {});
253 bool resolveArgumentsOfClass(MapResolver &R, const Record *Rec,
254 ArrayRef<const ArgumentInit *> ArgValues,
255 SMLoc Loc);
256 bool resolveArgumentsOfMultiClass(SubstStack &Substs, MultiClass *MC,
257 ArrayRef<const ArgumentInit *> ArgValues,
258 const Init *DefmName, SMLoc Loc);
260 private: // Parser methods.
261 bool consume(tgtok::TokKind K);
262 bool ParseObjectList(MultiClass *MC = nullptr);
263 bool ParseObject(MultiClass *MC);
264 bool ParseClass();
265 bool ParseMultiClass();
266 bool ParseDefm(MultiClass *CurMultiClass);
267 bool ParseDef(MultiClass *CurMultiClass);
268 bool ParseDefset();
269 bool ParseDeftype();
270 bool ParseDefvar(Record *CurRec = nullptr);
271 bool ParseDump(MultiClass *CurMultiClass, Record *CurRec = nullptr);
272 bool ParseForeach(MultiClass *CurMultiClass);
273 bool ParseIf(MultiClass *CurMultiClass);
274 bool ParseIfBody(MultiClass *CurMultiClass, StringRef Kind);
275 bool ParseAssert(MultiClass *CurMultiClass, Record *CurRec = nullptr);
276 bool ParseTopLevelLet(MultiClass *CurMultiClass);
277 void ParseLetList(SmallVectorImpl<LetRecord> &Result);
279 bool ParseObjectBody(Record *CurRec);
280 bool ParseBody(Record *CurRec);
281 bool ParseBodyItem(Record *CurRec);
283 bool ParseTemplateArgList(Record *CurRec);
284 const Init *ParseDeclaration(Record *CurRec, bool ParsingTemplateArgs);
285 const VarInit *ParseForeachDeclaration(const Init *&ForeachListValue);
287 SubClassReference ParseSubClassReference(Record *CurRec, bool isDefm);
288 SubMultiClassReference ParseSubMultiClassReference(MultiClass *CurMC);
290 const Init *ParseIDValue(Record *CurRec, const StringInit *Name,
291 SMRange NameLoc, IDParseMode Mode = ParseValueMode);
292 const Init *ParseSimpleValue(Record *CurRec, const RecTy *ItemType = nullptr,
293 IDParseMode Mode = ParseValueMode);
294 const Init *ParseValue(Record *CurRec, const RecTy *ItemType = nullptr,
295 IDParseMode Mode = ParseValueMode);
296 void ParseValueList(SmallVectorImpl<const Init *> &Result, Record *CurRec,
297 const RecTy *ItemType = nullptr);
298 bool ParseTemplateArgValueList(SmallVectorImpl<const ArgumentInit *> &Result,
299 Record *CurRec, const Record *ArgsRec);
300 void ParseDagArgList(
301 SmallVectorImpl<std::pair<const Init *, const StringInit *>> &Result,
302 Record *CurRec);
303 bool ParseOptionalRangeList(SmallVectorImpl<unsigned> &Ranges);
304 bool ParseOptionalBitList(SmallVectorImpl<unsigned> &Ranges);
305 const TypedInit *ParseSliceElement(Record *CurRec);
306 const TypedInit *ParseSliceElements(Record *CurRec, bool Single = false);
307 void ParseRangeList(SmallVectorImpl<unsigned> &Result);
308 bool ParseRangePiece(SmallVectorImpl<unsigned> &Ranges,
309 const TypedInit *FirstItem = nullptr);
310 const RecTy *ParseType();
311 const Init *ParseOperation(Record *CurRec, const RecTy *ItemType);
312 const Init *ParseOperationSubstr(Record *CurRec, const RecTy *ItemType);
313 const Init *ParseOperationFind(Record *CurRec, const RecTy *ItemType);
314 const Init *ParseOperationForEachFilter(Record *CurRec,
315 const RecTy *ItemType);
316 const Init *ParseOperationCond(Record *CurRec, const RecTy *ItemType);
317 const RecTy *ParseOperatorType();
318 const Init *ParseObjectName(MultiClass *CurMultiClass);
319 const Record *ParseClassID();
320 MultiClass *ParseMultiClassID();
321 bool ApplyLetStack(Record *CurRec);
322 bool ApplyLetStack(RecordsEntry &Entry);
323 bool CheckTemplateArgValues(SmallVectorImpl<const ArgumentInit *> &Values,
324 SMLoc Loc, const Record *ArgsRec);
327 } // end namespace llvm
329 #endif