1 //=== ClangASTNodesEmitter.cpp - Generate Clang AST node tables -*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // These tablegen backends emit Clang AST node tables
11 //===----------------------------------------------------------------------===//
13 #include "llvm/TableGen/Record.h"
14 #include "llvm/TableGen/TableGenBackend.h"
21 /// ClangASTNodesEmitter - The top-level class emits .inc files containing
22 /// declarations of Clang statements.
25 class ClangASTNodesEmitter {
26 // A map from a node to each of its derived nodes.
27 typedef std::multimap<Record*, Record*> ChildMap;
28 typedef ChildMap::const_iterator ChildIterator;
30 RecordKeeper &Records;
32 const std::string &BaseSuffix;
34 // Create a macro-ized version of a name
35 static std::string macroName(std::string S) {
36 for (unsigned i = 0; i < S.size(); ++i)
37 S[i] = std::toupper(S[i]);
42 // Return the name to be printed in the base field. Normally this is
43 // the record's name plus the base suffix, but if it is the root node and
44 // the suffix is non-empty, it's just the suffix.
45 std::string baseName(Record &R) {
46 if (&R == &Root && !BaseSuffix.empty())
49 return R.getName().str() + BaseSuffix;
52 std::pair<Record *, Record *> EmitNode (const ChildMap &Tree, raw_ostream& OS,
55 explicit ClangASTNodesEmitter(RecordKeeper &R, const std::string &N,
57 : Records(R), Root(N, SMLoc(), R), BaseSuffix(S)
60 // run - Output the .inc file contents
61 void run(raw_ostream &OS);
63 } // end anonymous namespace
65 //===----------------------------------------------------------------------===//
66 // Statement Node Tables (.inc file) generation.
67 //===----------------------------------------------------------------------===//
69 // Returns the first and last non-abstract subrecords
70 // Called recursively to ensure that nodes remain contiguous
71 std::pair<Record *, Record *> ClangASTNodesEmitter::EmitNode(
75 std::string BaseName = macroName(Base->getName());
77 ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
79 Record *First = nullptr, *Last = nullptr;
80 // This might be the pseudo-node for Stmt; don't assume it has an Abstract
82 if (Base->getValue("Abstract") && !Base->getValueAsBit("Abstract"))
86 Record *R = i->second;
87 bool Abstract = R->getValueAsBit("Abstract");
88 std::string NodeName = macroName(R->getName());
90 OS << "#ifndef " << NodeName << "\n";
91 OS << "# define " << NodeName << "(Type, Base) "
92 << BaseName << "(Type, Base)\n";
96 OS << "ABSTRACT_" << macroName(Root.getName()) << "(" << NodeName << "("
97 << R->getName() << ", " << baseName(*Base) << "))\n";
99 OS << NodeName << "(" << R->getName() << ", "
100 << baseName(*Base) << ")\n";
102 if (Tree.find(R) != Tree.end()) {
103 const std::pair<Record *, Record *> &Result
104 = EmitNode(Tree, OS, R);
105 if (!First && Result.first)
106 First = Result.first;
108 Last = Result.second;
118 OS << "#undef " << NodeName << "\n\n";
122 assert (Last && "Got a first node but not a last node for a range!");
124 OS << "LAST_" << macroName(Root.getName()) << "_RANGE(";
126 OS << macroName(Root.getName()) << "_RANGE(";
127 OS << Base->getName() << ", " << First->getName() << ", "
128 << Last->getName() << ")\n\n";
131 return std::make_pair(First, Last);
134 void ClangASTNodesEmitter::run(raw_ostream &OS) {
135 emitSourceFileHeader("List of AST nodes of a particular kind", OS);
137 // Write the preamble
138 OS << "#ifndef ABSTRACT_" << macroName(Root.getName()) << "\n";
139 OS << "# define ABSTRACT_" << macroName(Root.getName()) << "(Type) Type\n";
142 OS << "#ifndef " << macroName(Root.getName()) << "_RANGE\n";
144 << macroName(Root.getName()) << "_RANGE(Base, First, Last)\n";
147 OS << "#ifndef LAST_" << macroName(Root.getName()) << "_RANGE\n";
148 OS << "# define LAST_"
149 << macroName(Root.getName()) << "_RANGE(Base, First, Last) "
150 << macroName(Root.getName()) << "_RANGE(Base, First, Last)\n";
154 const std::vector<Record*> Stmts
155 = Records.getAllDerivedDefinitions(Root.getName());
159 for (unsigned i = 0, e = Stmts.size(); i != e; ++i) {
160 Record *R = Stmts[i];
162 if (R->getValue("Base"))
163 Tree.insert(std::make_pair(R->getValueAsDef("Base"), R));
165 Tree.insert(std::make_pair(&Root, R));
168 EmitNode(Tree, OS, &Root);
170 OS << "#undef " << macroName(Root.getName()) << "\n";
171 OS << "#undef " << macroName(Root.getName()) << "_RANGE\n";
172 OS << "#undef LAST_" << macroName(Root.getName()) << "_RANGE\n";
173 OS << "#undef ABSTRACT_" << macroName(Root.getName()) << "\n";
177 void EmitClangASTNodes(RecordKeeper &RK, raw_ostream &OS,
178 const std::string &N, const std::string &S) {
179 ClangASTNodesEmitter(RK, N, S).run(OS);
182 // Emits and addendum to a .inc file to enumerate the clang declaration
184 void EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) {
185 // FIXME: Find a .td file format to allow for this to be represented better.
187 emitSourceFileHeader("List of AST Decl nodes", OS);
189 OS << "#ifndef DECL_CONTEXT\n";
190 OS << "# define DECL_CONTEXT(DECL)\n";
193 OS << "#ifndef DECL_CONTEXT_BASE\n";
194 OS << "# define DECL_CONTEXT_BASE(DECL) DECL_CONTEXT(DECL)\n";
197 typedef std::set<Record*> RecordSet;
198 typedef std::vector<Record*> RecordVector;
200 RecordVector DeclContextsVector
201 = Records.getAllDerivedDefinitions("DeclContext");
202 RecordVector Decls = Records.getAllDerivedDefinitions("Decl");
203 RecordSet DeclContexts (DeclContextsVector.begin(), DeclContextsVector.end());
205 for (RecordVector::iterator i = Decls.begin(), e = Decls.end(); i != e; ++i) {
208 if (R->getValue("Base")) {
209 Record *B = R->getValueAsDef("Base");
210 if (DeclContexts.find(B) != DeclContexts.end()) {
211 OS << "DECL_CONTEXT_BASE(" << B->getName() << ")\n";
212 DeclContexts.erase(B);
217 // To keep identical order, RecordVector may be used
218 // instead of RecordSet.
219 for (RecordVector::iterator
220 i = DeclContextsVector.begin(), e = DeclContextsVector.end();
222 if (DeclContexts.find(*i) != DeclContexts.end())
223 OS << "DECL_CONTEXT(" << (*i)->getName() << ")\n";
225 OS << "#undef DECL_CONTEXT\n";
226 OS << "#undef DECL_CONTEXT_BASE\n";
228 } // end namespace clang