1f4a2713aSLionel Sambuc //=== ClangASTNodesEmitter.cpp - Generate Clang AST node tables -*- C++ -*-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc // The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // These tablegen backends emit Clang AST node tables
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc
14f4a2713aSLionel Sambuc #include "llvm/TableGen/Record.h"
15f4a2713aSLionel Sambuc #include "llvm/TableGen/TableGenBackend.h"
16f4a2713aSLionel Sambuc #include <cctype>
17f4a2713aSLionel Sambuc #include <map>
18f4a2713aSLionel Sambuc #include <set>
19f4a2713aSLionel Sambuc #include <string>
20f4a2713aSLionel Sambuc using namespace llvm;
21f4a2713aSLionel Sambuc
22f4a2713aSLionel Sambuc /// ClangASTNodesEmitter - The top-level class emits .inc files containing
23f4a2713aSLionel Sambuc /// declarations of Clang statements.
24f4a2713aSLionel Sambuc ///
25f4a2713aSLionel Sambuc namespace {
26f4a2713aSLionel Sambuc class ClangASTNodesEmitter {
27f4a2713aSLionel Sambuc // A map from a node to each of its derived nodes.
28f4a2713aSLionel Sambuc typedef std::multimap<Record*, Record*> ChildMap;
29f4a2713aSLionel Sambuc typedef ChildMap::const_iterator ChildIterator;
30f4a2713aSLionel Sambuc
31f4a2713aSLionel Sambuc RecordKeeper &Records;
32f4a2713aSLionel Sambuc Record Root;
33f4a2713aSLionel Sambuc const std::string &BaseSuffix;
34f4a2713aSLionel Sambuc
35f4a2713aSLionel Sambuc // Create a macro-ized version of a name
macroName(std::string S)36f4a2713aSLionel Sambuc static std::string macroName(std::string S) {
37f4a2713aSLionel Sambuc for (unsigned i = 0; i < S.size(); ++i)
38f4a2713aSLionel Sambuc S[i] = std::toupper(S[i]);
39f4a2713aSLionel Sambuc
40f4a2713aSLionel Sambuc return S;
41f4a2713aSLionel Sambuc }
42f4a2713aSLionel Sambuc
43f4a2713aSLionel Sambuc // Return the name to be printed in the base field. Normally this is
44f4a2713aSLionel Sambuc // the record's name plus the base suffix, but if it is the root node and
45f4a2713aSLionel Sambuc // the suffix is non-empty, it's just the suffix.
baseName(Record & R)46f4a2713aSLionel Sambuc std::string baseName(Record &R) {
47f4a2713aSLionel Sambuc if (&R == &Root && !BaseSuffix.empty())
48f4a2713aSLionel Sambuc return BaseSuffix;
49f4a2713aSLionel Sambuc
50f4a2713aSLionel Sambuc return R.getName() + BaseSuffix;
51f4a2713aSLionel Sambuc }
52f4a2713aSLionel Sambuc
53f4a2713aSLionel Sambuc std::pair<Record *, Record *> EmitNode (const ChildMap &Tree, raw_ostream& OS,
54f4a2713aSLionel Sambuc Record *Base);
55f4a2713aSLionel Sambuc public:
ClangASTNodesEmitter(RecordKeeper & R,const std::string & N,const std::string & S)56f4a2713aSLionel Sambuc explicit ClangASTNodesEmitter(RecordKeeper &R, const std::string &N,
57f4a2713aSLionel Sambuc const std::string &S)
58f4a2713aSLionel Sambuc : Records(R), Root(N, SMLoc(), R), BaseSuffix(S)
59f4a2713aSLionel Sambuc {}
60f4a2713aSLionel Sambuc
61f4a2713aSLionel Sambuc // run - Output the .inc file contents
62f4a2713aSLionel Sambuc void run(raw_ostream &OS);
63f4a2713aSLionel Sambuc };
64f4a2713aSLionel Sambuc } // end anonymous namespace
65f4a2713aSLionel Sambuc
66f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
67f4a2713aSLionel Sambuc // Statement Node Tables (.inc file) generation.
68f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
69f4a2713aSLionel Sambuc
70f4a2713aSLionel Sambuc // Returns the first and last non-abstract subrecords
71f4a2713aSLionel Sambuc // Called recursively to ensure that nodes remain contiguous
EmitNode(const ChildMap & Tree,raw_ostream & OS,Record * Base)72f4a2713aSLionel Sambuc std::pair<Record *, Record *> ClangASTNodesEmitter::EmitNode(
73f4a2713aSLionel Sambuc const ChildMap &Tree,
74f4a2713aSLionel Sambuc raw_ostream &OS,
75f4a2713aSLionel Sambuc Record *Base) {
76f4a2713aSLionel Sambuc std::string BaseName = macroName(Base->getName());
77f4a2713aSLionel Sambuc
78f4a2713aSLionel Sambuc ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
79f4a2713aSLionel Sambuc
80*0a6a1f1dSLionel Sambuc Record *First = nullptr, *Last = nullptr;
81f4a2713aSLionel Sambuc // This might be the pseudo-node for Stmt; don't assume it has an Abstract
82f4a2713aSLionel Sambuc // bit
83f4a2713aSLionel Sambuc if (Base->getValue("Abstract") && !Base->getValueAsBit("Abstract"))
84f4a2713aSLionel Sambuc First = Last = Base;
85f4a2713aSLionel Sambuc
86f4a2713aSLionel Sambuc for (; i != e; ++i) {
87f4a2713aSLionel Sambuc Record *R = i->second;
88f4a2713aSLionel Sambuc bool Abstract = R->getValueAsBit("Abstract");
89f4a2713aSLionel Sambuc std::string NodeName = macroName(R->getName());
90f4a2713aSLionel Sambuc
91f4a2713aSLionel Sambuc OS << "#ifndef " << NodeName << "\n";
92f4a2713aSLionel Sambuc OS << "# define " << NodeName << "(Type, Base) "
93f4a2713aSLionel Sambuc << BaseName << "(Type, Base)\n";
94f4a2713aSLionel Sambuc OS << "#endif\n";
95f4a2713aSLionel Sambuc
96f4a2713aSLionel Sambuc if (Abstract)
97f4a2713aSLionel Sambuc OS << "ABSTRACT_" << macroName(Root.getName()) << "(" << NodeName << "("
98f4a2713aSLionel Sambuc << R->getName() << ", " << baseName(*Base) << "))\n";
99f4a2713aSLionel Sambuc else
100f4a2713aSLionel Sambuc OS << NodeName << "(" << R->getName() << ", "
101f4a2713aSLionel Sambuc << baseName(*Base) << ")\n";
102f4a2713aSLionel Sambuc
103f4a2713aSLionel Sambuc if (Tree.find(R) != Tree.end()) {
104f4a2713aSLionel Sambuc const std::pair<Record *, Record *> &Result
105f4a2713aSLionel Sambuc = EmitNode(Tree, OS, R);
106f4a2713aSLionel Sambuc if (!First && Result.first)
107f4a2713aSLionel Sambuc First = Result.first;
108f4a2713aSLionel Sambuc if (Result.second)
109f4a2713aSLionel Sambuc Last = Result.second;
110f4a2713aSLionel Sambuc } else {
111f4a2713aSLionel Sambuc if (!Abstract) {
112f4a2713aSLionel Sambuc Last = R;
113f4a2713aSLionel Sambuc
114f4a2713aSLionel Sambuc if (!First)
115f4a2713aSLionel Sambuc First = R;
116f4a2713aSLionel Sambuc }
117f4a2713aSLionel Sambuc }
118f4a2713aSLionel Sambuc
119f4a2713aSLionel Sambuc OS << "#undef " << NodeName << "\n\n";
120f4a2713aSLionel Sambuc }
121f4a2713aSLionel Sambuc
122f4a2713aSLionel Sambuc if (First) {
123f4a2713aSLionel Sambuc assert (Last && "Got a first node but not a last node for a range!");
124f4a2713aSLionel Sambuc if (Base == &Root)
125f4a2713aSLionel Sambuc OS << "LAST_" << macroName(Root.getName()) << "_RANGE(";
126f4a2713aSLionel Sambuc else
127f4a2713aSLionel Sambuc OS << macroName(Root.getName()) << "_RANGE(";
128f4a2713aSLionel Sambuc OS << Base->getName() << ", " << First->getName() << ", "
129f4a2713aSLionel Sambuc << Last->getName() << ")\n\n";
130f4a2713aSLionel Sambuc }
131f4a2713aSLionel Sambuc
132f4a2713aSLionel Sambuc return std::make_pair(First, Last);
133f4a2713aSLionel Sambuc }
134f4a2713aSLionel Sambuc
run(raw_ostream & OS)135f4a2713aSLionel Sambuc void ClangASTNodesEmitter::run(raw_ostream &OS) {
136f4a2713aSLionel Sambuc emitSourceFileHeader("List of AST nodes of a particular kind", OS);
137f4a2713aSLionel Sambuc
138f4a2713aSLionel Sambuc // Write the preamble
139f4a2713aSLionel Sambuc OS << "#ifndef ABSTRACT_" << macroName(Root.getName()) << "\n";
140f4a2713aSLionel Sambuc OS << "# define ABSTRACT_" << macroName(Root.getName()) << "(Type) Type\n";
141f4a2713aSLionel Sambuc OS << "#endif\n";
142f4a2713aSLionel Sambuc
143f4a2713aSLionel Sambuc OS << "#ifndef " << macroName(Root.getName()) << "_RANGE\n";
144f4a2713aSLionel Sambuc OS << "# define "
145f4a2713aSLionel Sambuc << macroName(Root.getName()) << "_RANGE(Base, First, Last)\n";
146f4a2713aSLionel Sambuc OS << "#endif\n\n";
147f4a2713aSLionel Sambuc
148f4a2713aSLionel Sambuc OS << "#ifndef LAST_" << macroName(Root.getName()) << "_RANGE\n";
149f4a2713aSLionel Sambuc OS << "# define LAST_"
150f4a2713aSLionel Sambuc << macroName(Root.getName()) << "_RANGE(Base, First, Last) "
151f4a2713aSLionel Sambuc << macroName(Root.getName()) << "_RANGE(Base, First, Last)\n";
152f4a2713aSLionel Sambuc OS << "#endif\n\n";
153f4a2713aSLionel Sambuc
154f4a2713aSLionel Sambuc // Emit statements
155f4a2713aSLionel Sambuc const std::vector<Record*> Stmts
156f4a2713aSLionel Sambuc = Records.getAllDerivedDefinitions(Root.getName());
157f4a2713aSLionel Sambuc
158f4a2713aSLionel Sambuc ChildMap Tree;
159f4a2713aSLionel Sambuc
160f4a2713aSLionel Sambuc for (unsigned i = 0, e = Stmts.size(); i != e; ++i) {
161f4a2713aSLionel Sambuc Record *R = Stmts[i];
162f4a2713aSLionel Sambuc
163f4a2713aSLionel Sambuc if (R->getValue("Base"))
164f4a2713aSLionel Sambuc Tree.insert(std::make_pair(R->getValueAsDef("Base"), R));
165f4a2713aSLionel Sambuc else
166f4a2713aSLionel Sambuc Tree.insert(std::make_pair(&Root, R));
167f4a2713aSLionel Sambuc }
168f4a2713aSLionel Sambuc
169f4a2713aSLionel Sambuc EmitNode(Tree, OS, &Root);
170f4a2713aSLionel Sambuc
171f4a2713aSLionel Sambuc OS << "#undef " << macroName(Root.getName()) << "\n";
172f4a2713aSLionel Sambuc OS << "#undef " << macroName(Root.getName()) << "_RANGE\n";
173f4a2713aSLionel Sambuc OS << "#undef LAST_" << macroName(Root.getName()) << "_RANGE\n";
174f4a2713aSLionel Sambuc OS << "#undef ABSTRACT_" << macroName(Root.getName()) << "\n";
175f4a2713aSLionel Sambuc }
176f4a2713aSLionel Sambuc
177f4a2713aSLionel Sambuc namespace clang {
EmitClangASTNodes(RecordKeeper & RK,raw_ostream & OS,const std::string & N,const std::string & S)178f4a2713aSLionel Sambuc void EmitClangASTNodes(RecordKeeper &RK, raw_ostream &OS,
179f4a2713aSLionel Sambuc const std::string &N, const std::string &S) {
180f4a2713aSLionel Sambuc ClangASTNodesEmitter(RK, N, S).run(OS);
181f4a2713aSLionel Sambuc }
182f4a2713aSLionel Sambuc
183f4a2713aSLionel Sambuc // Emits and addendum to a .inc file to enumerate the clang declaration
184f4a2713aSLionel Sambuc // contexts.
EmitClangDeclContext(RecordKeeper & Records,raw_ostream & OS)185f4a2713aSLionel Sambuc void EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) {
186f4a2713aSLionel Sambuc // FIXME: Find a .td file format to allow for this to be represented better.
187f4a2713aSLionel Sambuc
188f4a2713aSLionel Sambuc emitSourceFileHeader("List of AST Decl nodes", OS);
189f4a2713aSLionel Sambuc
190f4a2713aSLionel Sambuc OS << "#ifndef DECL_CONTEXT\n";
191f4a2713aSLionel Sambuc OS << "# define DECL_CONTEXT(DECL)\n";
192f4a2713aSLionel Sambuc OS << "#endif\n";
193f4a2713aSLionel Sambuc
194f4a2713aSLionel Sambuc OS << "#ifndef DECL_CONTEXT_BASE\n";
195f4a2713aSLionel Sambuc OS << "# define DECL_CONTEXT_BASE(DECL) DECL_CONTEXT(DECL)\n";
196f4a2713aSLionel Sambuc OS << "#endif\n";
197f4a2713aSLionel Sambuc
198f4a2713aSLionel Sambuc typedef std::set<Record*> RecordSet;
199f4a2713aSLionel Sambuc typedef std::vector<Record*> RecordVector;
200f4a2713aSLionel Sambuc
201f4a2713aSLionel Sambuc RecordVector DeclContextsVector
202f4a2713aSLionel Sambuc = Records.getAllDerivedDefinitions("DeclContext");
203f4a2713aSLionel Sambuc RecordVector Decls = Records.getAllDerivedDefinitions("Decl");
204f4a2713aSLionel Sambuc RecordSet DeclContexts (DeclContextsVector.begin(), DeclContextsVector.end());
205f4a2713aSLionel Sambuc
206f4a2713aSLionel Sambuc for (RecordVector::iterator i = Decls.begin(), e = Decls.end(); i != e; ++i) {
207f4a2713aSLionel Sambuc Record *R = *i;
208f4a2713aSLionel Sambuc
209f4a2713aSLionel Sambuc if (R->getValue("Base")) {
210f4a2713aSLionel Sambuc Record *B = R->getValueAsDef("Base");
211f4a2713aSLionel Sambuc if (DeclContexts.find(B) != DeclContexts.end()) {
212f4a2713aSLionel Sambuc OS << "DECL_CONTEXT_BASE(" << B->getName() << ")\n";
213f4a2713aSLionel Sambuc DeclContexts.erase(B);
214f4a2713aSLionel Sambuc }
215f4a2713aSLionel Sambuc }
216f4a2713aSLionel Sambuc }
217f4a2713aSLionel Sambuc
218f4a2713aSLionel Sambuc // To keep identical order, RecordVector may be used
219f4a2713aSLionel Sambuc // instead of RecordSet.
220f4a2713aSLionel Sambuc for (RecordVector::iterator
221f4a2713aSLionel Sambuc i = DeclContextsVector.begin(), e = DeclContextsVector.end();
222f4a2713aSLionel Sambuc i != e; ++i)
223f4a2713aSLionel Sambuc if (DeclContexts.find(*i) != DeclContexts.end())
224f4a2713aSLionel Sambuc OS << "DECL_CONTEXT(" << (*i)->getName() << ")\n";
225f4a2713aSLionel Sambuc
226f4a2713aSLionel Sambuc OS << "#undef DECL_CONTEXT\n";
227f4a2713aSLionel Sambuc OS << "#undef DECL_CONTEXT_BASE\n";
228f4a2713aSLionel Sambuc }
229f4a2713aSLionel Sambuc } // end namespace clang
230