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