[clang][TableGen] Change AST Nodes Emitter to use const RecordKeeper (#108270)

Change AST Nodes Emitter to use const RecordKeeper.

This is a part of effort to have better const correctness in TableGen
backends:


https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089
This commit is contained in:
Rahul Joshi 2024-09-12 06:51:51 -07:00 committed by GitHub
parent dbc90b55e8
commit b6ff8ed5d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 27 deletions

View File

@ -34,7 +34,7 @@ class ClangASTNodesEmitter {
typedef ChildMap::const_iterator ChildIterator;
std::set<ASTNode> PrioritizedClasses;
RecordKeeper &Records;
const RecordKeeper &Records;
ASTNode Root;
const std::string &NodeClassName;
const std::string &BaseSuffix;
@ -70,14 +70,12 @@ class ClangASTNodesEmitter {
std::pair<ASTNode, ASTNode> EmitNode(raw_ostream& OS, ASTNode Base);
public:
explicit ClangASTNodesEmitter(RecordKeeper &R, const std::string &N,
explicit ClangASTNodesEmitter(const RecordKeeper &R, const std::string &N,
const std::string &S,
std::string_view PriorizeIfSubclassOf)
: Records(R), NodeClassName(N), BaseSuffix(S) {
auto vecPrioritized =
PriorizeIfSubclassOf.empty()
? std::vector<Record *>{}
: R.getAllDerivedDefinitions(PriorizeIfSubclassOf);
ArrayRef<const Record *> vecPrioritized =
R.getAllDerivedDefinitionsIfDefined(PriorizeIfSubclassOf);
PrioritizedClasses =
std::set<ASTNode>(vecPrioritized.begin(), vecPrioritized.end());
}
@ -169,10 +167,7 @@ void ClangASTNodesEmitter::deriveChildTree() {
assert(!Root && "already computed tree");
// Emit statements
const std::vector<Record*> Stmts
= Records.getAllDerivedDefinitions(NodeClassName);
for (auto *R : Stmts) {
for (const Record *R : Records.getAllDerivedDefinitions(NodeClassName)) {
if (auto B = R->getValueAsOptionalDef(BaseFieldName))
Tree.insert(std::make_pair(B, R));
else if (Root)
@ -217,14 +212,14 @@ void ClangASTNodesEmitter::run(raw_ostream &OS) {
OS << "#undef ABSTRACT_" << macroHierarchyName() << "\n";
}
void clang::EmitClangASTNodes(RecordKeeper &RK, raw_ostream &OS,
void clang::EmitClangASTNodes(const RecordKeeper &RK, raw_ostream &OS,
const std::string &N, const std::string &S,
std::string_view PriorizeIfSubclassOf) {
ClangASTNodesEmitter(RK, N, S, PriorizeIfSubclassOf).run(OS);
}
void printDeclContext(const std::multimap<Record *, Record *> &Tree,
Record *DeclContext, raw_ostream &OS) {
void printDeclContext(const std::multimap<const Record *, const Record *> &Tree,
const Record *DeclContext, raw_ostream &OS) {
if (!DeclContext->getValueAsBit(AbstractFieldName))
OS << "DECL_CONTEXT(" << DeclContext->getName() << ")\n";
auto i = Tree.lower_bound(DeclContext);
@ -236,7 +231,7 @@ void printDeclContext(const std::multimap<Record *, Record *> &Tree,
// Emits and addendum to a .inc file to enumerate the clang declaration
// contexts.
void clang::EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) {
void clang::EmitClangDeclContext(const RecordKeeper &Records, raw_ostream &OS) {
// FIXME: Find a .td file format to allow for this to be represented better.
emitSourceFileHeader("List of AST Decl nodes", OS, Records);
@ -245,22 +240,15 @@ void clang::EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) {
OS << "# define DECL_CONTEXT(DECL)\n";
OS << "#endif\n";
std::vector<Record *> DeclContextsVector =
Records.getAllDerivedDefinitions(DeclContextNodeClassName);
std::vector<Record *> Decls =
Records.getAllDerivedDefinitions(DeclNodeClassName);
std::multimap<const Record *, const Record *> Tree;
std::multimap<Record *, Record *> Tree;
const std::vector<Record *> Stmts =
Records.getAllDerivedDefinitions(DeclNodeClassName);
for (auto *R : Stmts) {
for (const Record *R : Records.getAllDerivedDefinitions(DeclNodeClassName)) {
if (auto *B = R->getValueAsOptionalDef(BaseFieldName))
Tree.insert(std::make_pair(B, R));
}
for (auto *DeclContext : DeclContextsVector) {
for (const Record *DeclContext :
Records.getAllDerivedDefinitions(DeclContextNodeClassName)) {
printDeclContext(Tree, DeclContext, OS);
}

View File

@ -24,7 +24,7 @@ class RecordKeeper;
namespace clang {
void EmitClangDeclContext(llvm::RecordKeeper &RK, llvm::raw_ostream &OS);
void EmitClangDeclContext(const llvm::RecordKeeper &RK, llvm::raw_ostream &OS);
/**
@param PriorizeIfSubclassOf These classes should be prioritized in the output.
This is useful to force enum generation/jump tables/lookup tables to be more
@ -32,7 +32,7 @@ void EmitClangDeclContext(llvm::RecordKeeper &RK, llvm::raw_ostream &OS);
in Decl for classes that inherit from DeclContext, for functions like
castFromDeclContext.
*/
void EmitClangASTNodes(llvm::RecordKeeper &RK, llvm::raw_ostream &OS,
void EmitClangASTNodes(const llvm::RecordKeeper &RK, llvm::raw_ostream &OS,
const std::string &N, const std::string &S,
std::string_view PriorizeIfSubclassOf = "");
void EmitClangBasicReader(llvm::RecordKeeper &Records, llvm::raw_ostream &OS);