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