Skip to content

Commit 8ae5521

Browse files
authored
[TableGen] Migrate CTags/DFA/Directive Emitters to const RecordKeeper (#107693)
Migrate CTags/DFA/Directive Emitters to const RecordKeeper.
1 parent ce3c58e commit 8ae5521

File tree

4 files changed

+47
-47
lines changed

4 files changed

+47
-47
lines changed

llvm/include/llvm/TableGen/DirectiveEmitter.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ namespace llvm {
1515
// DirectiveBase.td and provides helper methods for accessing it.
1616
class DirectiveLanguage {
1717
public:
18-
explicit DirectiveLanguage(llvm::RecordKeeper &Records) : Records(Records) {
18+
explicit DirectiveLanguage(const llvm::RecordKeeper &Records)
19+
: Records(Records) {
1920
const auto &DirectiveLanguages = getDirectiveLanguages();
2021
Def = DirectiveLanguages[0];
2122
}
@@ -50,29 +51,29 @@ class DirectiveLanguage {
5051
return Def->getValueAsBit("enableBitmaskEnumInNamespace");
5152
}
5253

53-
std::vector<Record *> getAssociations() const {
54+
ArrayRef<const Record *> getAssociations() const {
5455
return Records.getAllDerivedDefinitions("Association");
5556
}
5657

57-
std::vector<Record *> getCategories() const {
58+
ArrayRef<const Record *> getCategories() const {
5859
return Records.getAllDerivedDefinitions("Category");
5960
}
6061

61-
std::vector<Record *> getDirectives() const {
62+
ArrayRef<const Record *> getDirectives() const {
6263
return Records.getAllDerivedDefinitions("Directive");
6364
}
6465

65-
std::vector<Record *> getClauses() const {
66+
ArrayRef<const Record *> getClauses() const {
6667
return Records.getAllDerivedDefinitions("Clause");
6768
}
6869

6970
bool HasValidityErrors() const;
7071

7172
private:
7273
const llvm::Record *Def;
73-
llvm::RecordKeeper &Records;
74+
const llvm::RecordKeeper &Records;
7475

75-
std::vector<Record *> getDirectiveLanguages() const {
76+
ArrayRef<const Record *> getDirectiveLanguages() const {
7677
return Records.getAllDerivedDefinitions("DirectiveLanguage");
7778
}
7879
};

llvm/utils/TableGen/CTagsEmitter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ class Tag {
5050

5151
class CTagsEmitter {
5252
private:
53-
RecordKeeper &Records;
53+
const RecordKeeper &Records;
5454

5555
public:
56-
CTagsEmitter(RecordKeeper &R) : Records(R) {}
56+
CTagsEmitter(const RecordKeeper &R) : Records(R) {}
5757

5858
void run(raw_ostream &OS);
5959

llvm/utils/TableGen/DFAEmitter.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ class Transition {
182182
SmallVector<std::string, 4> Types;
183183

184184
public:
185-
Transition(Record *R, Automaton *Parent);
185+
Transition(const Record *R, Automaton *Parent);
186186
const ActionTuple &getActions() { return Actions; }
187187
SmallVector<std::string, 4> getTypes() { return Types; }
188188

@@ -191,16 +191,16 @@ class Transition {
191191
};
192192

193193
class Automaton {
194-
RecordKeeper &Records;
195-
Record *R;
194+
const RecordKeeper &Records;
195+
const Record *R;
196196
std::vector<Transition> Transitions;
197197
/// All possible action tuples, uniqued.
198198
UniqueVector<ActionTuple> Actions;
199199
/// The fields within each Transition object to find the action symbols.
200200
std::vector<StringRef> ActionSymbolFields;
201201

202202
public:
203-
Automaton(RecordKeeper &Records, Record *R);
203+
Automaton(const RecordKeeper &Records, const Record *R);
204204
void emit(raw_ostream &OS);
205205

206206
ArrayRef<StringRef> getActionSymbolFields() { return ActionSymbolFields; }
@@ -210,10 +210,10 @@ class Automaton {
210210
};
211211

212212
class AutomatonEmitter {
213-
RecordKeeper &Records;
213+
const RecordKeeper &Records;
214214

215215
public:
216-
AutomatonEmitter(RecordKeeper &R) : Records(R) {}
216+
AutomatonEmitter(const RecordKeeper &R) : Records(R) {}
217217
void run(raw_ostream &OS);
218218
};
219219

@@ -232,23 +232,23 @@ class CustomDfaEmitter : public DfaEmitter {
232232
} // namespace
233233

234234
void AutomatonEmitter::run(raw_ostream &OS) {
235-
for (Record *R : Records.getAllDerivedDefinitions("GenericAutomaton")) {
235+
for (const Record *R : Records.getAllDerivedDefinitions("GenericAutomaton")) {
236236
Automaton A(Records, R);
237237
OS << "#ifdef GET_" << R->getName() << "_DECL\n";
238238
A.emit(OS);
239239
OS << "#endif // GET_" << R->getName() << "_DECL\n";
240240
}
241241
}
242242

243-
Automaton::Automaton(RecordKeeper &Records, Record *R)
243+
Automaton::Automaton(const RecordKeeper &Records, const Record *R)
244244
: Records(Records), R(R) {
245245
LLVM_DEBUG(dbgs() << "Emitting automaton for " << R->getName() << "\n");
246246
ActionSymbolFields = R->getValueAsListOfStrings("SymbolFields");
247247
}
248248

249249
void Automaton::emit(raw_ostream &OS) {
250250
StringRef TransitionClass = R->getValueAsString("TransitionClass");
251-
for (Record *T : Records.getAllDerivedDefinitions(TransitionClass)) {
251+
for (const Record *T : Records.getAllDerivedDefinitions(TransitionClass)) {
252252
assert(T->isSubClassOf("Transition"));
253253
Transitions.emplace_back(T, this);
254254
Actions.insert(Transitions.back().getActions());
@@ -305,7 +305,7 @@ StringRef Automaton::getActionSymbolType(StringRef A) {
305305
return R->getValueAsString(Ty.str());
306306
}
307307

308-
Transition::Transition(Record *R, Automaton *Parent) {
308+
Transition::Transition(const Record *R, Automaton *Parent) {
309309
BitsInit *NewStateInit = R->getValueAsBitsInit("NewState");
310310
NewState = 0;
311311
assert(NewStateInit->getNumBits() <= sizeof(uint64_t) * 8 &&
@@ -318,8 +318,8 @@ Transition::Transition(Record *R, Automaton *Parent) {
318318
}
319319

320320
for (StringRef A : Parent->getActionSymbolFields()) {
321-
RecordVal *SymbolV = R->getValue(A);
322-
if (auto *Ty = dyn_cast<RecordRecTy>(SymbolV->getType())) {
321+
const RecordVal *SymbolV = R->getValue(A);
322+
if (const auto *Ty = dyn_cast<RecordRecTy>(SymbolV->getType())) {
323323
Actions.emplace_back(R->getValueAsDef(A));
324324
Types.emplace_back(Ty->getAsString());
325325
} else if (isa<IntRecTy>(SymbolV->getType())) {

llvm/utils/TableGen/DirectiveEmitter.cpp

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ class IfDefScope {
4646

4747
// Generate enum class. Entries are emitted in the order in which they appear
4848
// in the `Records` vector.
49-
static void GenerateEnumClass(const std::vector<Record *> &Records,
50-
raw_ostream &OS, StringRef Enum, StringRef Prefix,
49+
static void GenerateEnumClass(ArrayRef<const Record *> Records, raw_ostream &OS,
50+
StringRef Enum, StringRef Prefix,
5151
const DirectiveLanguage &DirLang,
5252
bool ExportEnums) {
5353
OS << "\n";
@@ -79,7 +79,7 @@ static void GenerateEnumClass(const std::vector<Record *> &Records,
7979

8080
// Generate enums for values that clauses can take.
8181
// Also generate function declarations for get<Enum>Name(StringRef Str).
82-
static void GenerateEnumClauseVal(const std::vector<Record *> &Records,
82+
static void GenerateEnumClauseVal(ArrayRef<const Record *> Records,
8383
raw_ostream &OS,
8484
const DirectiveLanguage &DirLang,
8585
std::string &EnumHelperFuncs) {
@@ -144,7 +144,7 @@ static bool HasDuplicateClauses(const std::vector<Record *> &Clauses,
144144
// three allowed list. Also, since required implies allowed, clauses cannot
145145
// appear in both the allowedClauses and requiredClauses lists.
146146
static bool
147-
HasDuplicateClausesInDirectives(const std::vector<Record *> &Directives) {
147+
HasDuplicateClausesInDirectives(ArrayRef<const Record *> Directives) {
148148
bool HasDuplicate = false;
149149
for (const auto &D : Directives) {
150150
Directive Dir{D};
@@ -184,7 +184,7 @@ bool DirectiveLanguage::HasValidityErrors() const {
184184
// Count the maximum number of leaf constituents per construct.
185185
static size_t GetMaxLeafCount(const DirectiveLanguage &DirLang) {
186186
size_t MaxCount = 0;
187-
for (Record *R : DirLang.getDirectives()) {
187+
for (const Record *R : DirLang.getDirectives()) {
188188
size_t Count = Directive{R}.getLeafConstructs().size();
189189
MaxCount = std::max(MaxCount, Count);
190190
}
@@ -193,7 +193,7 @@ static size_t GetMaxLeafCount(const DirectiveLanguage &DirLang) {
193193

194194
// Generate the declaration section for the enumeration in the directive
195195
// language
196-
static void EmitDirectivesDecl(RecordKeeper &Records, raw_ostream &OS) {
196+
static void EmitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
197197
const auto DirLang = DirectiveLanguage{Records};
198198
if (DirLang.HasValidityErrors())
199199
return;
@@ -220,7 +220,7 @@ static void EmitDirectivesDecl(RecordKeeper &Records, raw_ostream &OS) {
220220
OS << "\nLLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();\n";
221221

222222
// Emit Directive associations
223-
std::vector<Record *> associations;
223+
std::vector<const Record *> associations;
224224
llvm::copy_if(
225225
DirLang.getAssociations(), std::back_inserter(associations),
226226
// Skip the "special" value
@@ -283,9 +283,8 @@ static void EmitDirectivesDecl(RecordKeeper &Records, raw_ostream &OS) {
283283
}
284284

285285
// Generate function implementation for get<Enum>Name(StringRef Str)
286-
static void GenerateGetName(const std::vector<Record *> &Records,
287-
raw_ostream &OS, StringRef Enum,
288-
const DirectiveLanguage &DirLang,
286+
static void GenerateGetName(ArrayRef<const Record *> Records, raw_ostream &OS,
287+
StringRef Enum, const DirectiveLanguage &DirLang,
289288
StringRef Prefix) {
290289
OS << "\n";
291290
OS << "llvm::StringRef llvm::" << DirLang.getCppNamespace() << "::get"
@@ -308,13 +307,13 @@ static void GenerateGetName(const std::vector<Record *> &Records,
308307
}
309308

310309
// Generate function implementation for get<Enum>Kind(StringRef Str)
311-
static void GenerateGetKind(const std::vector<Record *> &Records,
312-
raw_ostream &OS, StringRef Enum,
313-
const DirectiveLanguage &DirLang, StringRef Prefix,
314-
bool ImplicitAsUnknown) {
310+
static void GenerateGetKind(ArrayRef<const Record *> Records, raw_ostream &OS,
311+
StringRef Enum, const DirectiveLanguage &DirLang,
312+
StringRef Prefix, bool ImplicitAsUnknown) {
315313

316-
auto DefaultIt = llvm::find_if(
317-
Records, [](Record *R) { return R->getValueAsBit("isDefault") == true; });
314+
auto DefaultIt = llvm::find_if(Records, [](const Record *R) {
315+
return R->getValueAsBit("isDefault") == true;
316+
});
318317

319318
if (DefaultIt == Records.end()) {
320319
PrintError("At least one " + Enum + " must be defined as default.");
@@ -506,8 +505,8 @@ static void EmitLeafTable(const DirectiveLanguage &DirLang, raw_ostream &OS,
506505
// row. To avoid this, an auxiliary ordering table is created, such that
507506
// row for Dir_A = table[auxiliary[Dir_A]].
508507

509-
std::vector<Record *> Directives = DirLang.getDirectives();
510-
DenseMap<Record *, int> DirId; // Record * -> llvm::omp::Directive
508+
ArrayRef<const Record *> Directives = DirLang.getDirectives();
509+
DenseMap<const Record *, int> DirId; // Record * -> llvm::omp::Directive
511510

512511
for (auto [Idx, Rec] : llvm::enumerate(Directives))
513512
DirId.insert(std::make_pair(Rec, Idx));
@@ -628,7 +627,7 @@ static void GenerateGetDirectiveAssociation(const DirectiveLanguage &DirLang,
628627
Invalid,
629628
};
630629

631-
std::vector<Record *> associations = DirLang.getAssociations();
630+
ArrayRef<const Record *> associations = DirLang.getAssociations();
632631

633632
auto getAssocValue = [](StringRef name) -> Association {
634633
return StringSwitch<Association>(name)
@@ -720,7 +719,7 @@ static void GenerateGetDirectiveAssociation(const DirectiveLanguage &DirLang,
720719
return Result;
721720
};
722721

723-
for (Record *R : DirLang.getDirectives())
722+
for (const Record *R : DirLang.getDirectives())
724723
compAssocImpl(R, compAssocImpl); // Updates AsMap.
725724

726725
OS << '\n';
@@ -739,7 +738,7 @@ static void GenerateGetDirectiveAssociation(const DirectiveLanguage &DirLang,
739738
OS << AssociationTypeName << " llvm::" << DirLang.getCppNamespace()
740739
<< "::getDirectiveAssociation(" << DirectiveTypeName << " Dir) {\n";
741740
OS << " switch (Dir) {\n";
742-
for (Record *R : DirLang.getDirectives()) {
741+
for (const Record *R : DirLang.getDirectives()) {
743742
if (auto F = AsMap.find(R); F != AsMap.end()) {
744743
Directive Dir{R};
745744
OS << " case " << getQualifiedName(Dir.getFormattedName()) << ":\n";
@@ -763,7 +762,7 @@ static void GenerateGetDirectiveCategory(const DirectiveLanguage &DirLang,
763762
<< GetDirectiveType(DirLang) << " Dir) {\n";
764763
OS << " switch (Dir) {\n";
765764

766-
for (Record *R : DirLang.getDirectives()) {
765+
for (const Record *R : DirLang.getDirectives()) {
767766
Directive D{R};
768767
OS << " case " << GetDirectiveName(DirLang, R) << ":\n";
769768
OS << " return " << CategoryNamespace
@@ -903,7 +902,7 @@ static void GenerateFlangClauseParserClassList(const DirectiveLanguage &DirLang,
903902
IfDefScope Scope("GEN_FLANG_CLAUSE_PARSER_CLASSES_LIST", OS);
904903

905904
OS << "\n";
906-
llvm::interleaveComma(DirLang.getClauses(), OS, [&](Record *C) {
905+
llvm::interleaveComma(DirLang.getClauses(), OS, [&](const Record *C) {
907906
Clause Clause{C};
908907
OS << Clause.getFormattedParserClassName() << "\n";
909908
});
@@ -1013,7 +1012,7 @@ static void GenerateFlangClauseParserKindMap(const DirectiveLanguage &DirLang,
10131012
<< " Parser clause\");\n";
10141013
}
10151014

1016-
static bool compareClauseName(Record *R1, Record *R2) {
1015+
static bool compareClauseName(const Record *R1, const Record *R2) {
10171016
Clause C1{R1};
10181017
Clause C2{R2};
10191018
return (C1.getName() > C2.getName());
@@ -1022,7 +1021,7 @@ static bool compareClauseName(Record *R1, Record *R2) {
10221021
// Generate the parser for the clauses.
10231022
static void GenerateFlangClausesParser(const DirectiveLanguage &DirLang,
10241023
raw_ostream &OS) {
1025-
std::vector<Record *> Clauses = DirLang.getClauses();
1024+
std::vector<const Record *> Clauses = DirLang.getClauses();
10261025
// Sort clauses in reverse alphabetical order so with clauses with same
10271026
// beginning, the longer option is tried before.
10281027
llvm::sort(Clauses, compareClauseName);
@@ -1231,7 +1230,7 @@ void EmitDirectivesBasicImpl(const DirectiveLanguage &DirLang,
12311230

12321231
// Generate the implemenation section for the enumeration in the directive
12331232
// language.
1234-
static void EmitDirectivesImpl(RecordKeeper &Records, raw_ostream &OS) {
1233+
static void EmitDirectivesImpl(const RecordKeeper &Records, raw_ostream &OS) {
12351234
const auto DirLang = DirectiveLanguage{Records};
12361235
if (DirLang.HasValidityErrors())
12371236
return;

0 commit comments

Comments
 (0)