1 //=== ClangASTNodesEmitter.cpp - Generate Clang AST node tables -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These tablegen backends emit Clang AST node tables
12 //===----------------------------------------------------------------------===//
14 #include "llvm/TableGen/Record.h"
15 #include "llvm/TableGen/TableGenBackend.h"
22 /// ClangASTNodesEmitter - The top-level class emits .inc files containing
23 /// declarations of Clang statements.
26 class ClangASTNodesEmitter {
27 // A map from a node to each of its derived nodes.
28 typedef std::multimap<Record*, Record*> ChildMap;
29 typedef ChildMap::const_iterator ChildIterator;
31 RecordKeeper &Records;
33 const std::string &BaseSuffix;
35 // Create a macro-ized version of a name
36 static std::string macroName(std::string S) {
37 for (unsigned i = 0; i < S.size(); ++i)
38 S[i] = std::toupper(S[i]);
43 // Return the name to be printed in the base field. Normally this is
44 // the record's name plus the base suffix, but if it is the root node and
45 // the suffix is non-empty, it's just the suffix.
46 std::string baseName(Record &R) {
47 if (&R == &Root && !BaseSuffix.empty())
50 return R.getName() + BaseSuffix;
53 std::pair<Record *, Record *> EmitNode (const ChildMap &Tree, raw_ostream& OS,
56 explicit ClangASTNodesEmitter(RecordKeeper &R, const std::string &N,
58 : Records(R), Root(N, SMLoc(), R), BaseSuffix(S)
61 // run - Output the .inc file contents
62 void run(raw_ostream &OS);
64 } // end anonymous namespace
66 //===----------------------------------------------------------------------===//
67 // Statement Node Tables (.inc file) generation.
68 //===----------------------------------------------------------------------===//
70 // Returns the first and last non-abstract subrecords
71 // Called recursively to ensure that nodes remain contiguous
72 std::pair<Record *, Record *> ClangASTNodesEmitter::EmitNode(
76 std::string BaseName = macroName(Base->getName());
78 ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
80 Record *First = 0, *Last = 0;
81 // This might be the pseudo-node for Stmt; don't assume it has an Abstract
83 if (Base->getValue("Abstract") && !Base->getValueAsBit("Abstract"))
87 Record *R = i->second;
88 bool Abstract = R->getValueAsBit("Abstract");
89 std::string NodeName = macroName(R->getName());
91 OS << "#ifndef " << NodeName << "\n";
92 OS << "# define " << NodeName << "(Type, Base) "
93 << BaseName << "(Type, Base)\n";
97 OS << "ABSTRACT_" << macroName(Root.getName()) << "(" << NodeName << "("
98 << R->getName() << ", " << baseName(*Base) << "))\n";
100 OS << NodeName << "(" << R->getName() << ", "
101 << baseName(*Base) << ")\n";
103 if (Tree.find(R) != Tree.end()) {
104 const std::pair<Record *, Record *> &Result
105 = EmitNode(Tree, OS, R);
106 if (!First && Result.first)
107 First = Result.first;
109 Last = Result.second;
119 OS << "#undef " << NodeName << "\n\n";
123 assert (Last && "Got a first node but not a last node for a range!");
125 OS << "LAST_" << macroName(Root.getName()) << "_RANGE(";
127 OS << macroName(Root.getName()) << "_RANGE(";
128 OS << Base->getName() << ", " << First->getName() << ", "
129 << Last->getName() << ")\n\n";
132 return std::make_pair(First, Last);
135 void ClangASTNodesEmitter::run(raw_ostream &OS) {
136 emitSourceFileHeader("List of AST nodes of a particular kind", OS);
138 // Write the preamble
139 OS << "#ifndef ABSTRACT_" << macroName(Root.getName()) << "\n";
140 OS << "# define ABSTRACT_" << macroName(Root.getName()) << "(Type) Type\n";
143 OS << "#ifndef " << macroName(Root.getName()) << "_RANGE\n";
145 << macroName(Root.getName()) << "_RANGE(Base, First, Last)\n";
148 OS << "#ifndef LAST_" << macroName(Root.getName()) << "_RANGE\n";
149 OS << "# define LAST_"
150 << macroName(Root.getName()) << "_RANGE(Base, First, Last) "
151 << macroName(Root.getName()) << "_RANGE(Base, First, Last)\n";
155 const std::vector<Record*> Stmts
156 = Records.getAllDerivedDefinitions(Root.getName());
160 for (unsigned i = 0, e = Stmts.size(); i != e; ++i) {
161 Record *R = Stmts[i];
163 if (R->getValue("Base"))
164 Tree.insert(std::make_pair(R->getValueAsDef("Base"), R));
166 Tree.insert(std::make_pair(&Root, R));
169 EmitNode(Tree, OS, &Root);
171 OS << "#undef " << macroName(Root.getName()) << "\n";
172 OS << "#undef " << macroName(Root.getName()) << "_RANGE\n";
173 OS << "#undef LAST_" << macroName(Root.getName()) << "_RANGE\n";
174 OS << "#undef ABSTRACT_" << macroName(Root.getName()) << "\n";
178 void EmitClangASTNodes(RecordKeeper &RK, raw_ostream &OS,
179 const std::string &N, const std::string &S) {
180 ClangASTNodesEmitter(RK, N, S).run(OS);
183 // Emits and addendum to a .inc file to enumerate the clang declaration
185 void EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) {
186 // FIXME: Find a .td file format to allow for this to be represented better.
188 emitSourceFileHeader("List of AST Decl nodes", OS);
190 OS << "#ifndef DECL_CONTEXT\n";
191 OS << "# define DECL_CONTEXT(DECL)\n";
194 OS << "#ifndef DECL_CONTEXT_BASE\n";
195 OS << "# define DECL_CONTEXT_BASE(DECL) DECL_CONTEXT(DECL)\n";
198 typedef std::set<Record*> RecordSet;
199 typedef std::vector<Record*> RecordVector;
201 RecordVector DeclContextsVector
202 = Records.getAllDerivedDefinitions("DeclContext");
203 RecordVector Decls = Records.getAllDerivedDefinitions("Decl");
204 RecordSet DeclContexts (DeclContextsVector.begin(), DeclContextsVector.end());
206 for (RecordVector::iterator i = Decls.begin(), e = Decls.end(); i != e; ++i) {
209 if (R->getValue("Base")) {
210 Record *B = R->getValueAsDef("Base");
211 if (DeclContexts.find(B) != DeclContexts.end()) {
212 OS << "DECL_CONTEXT_BASE(" << B->getName() << ")\n";
213 DeclContexts.erase(B);
218 // To keep identical order, RecordVector may be used
219 // instead of RecordSet.
220 for (RecordVector::iterator
221 i = DeclContextsVector.begin(), e = DeclContextsVector.end();
223 if (DeclContexts.find(*i) != DeclContexts.end())
224 OS << "DECL_CONTEXT(" << (*i)->getName() << ")\n";
226 OS << "#undef DECL_CONTEXT\n";
227 OS << "#undef DECL_CONTEXT_BASE\n";
229 } // end namespace clang