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 "ASTTableGen.h"
14 #include "TableGenBackends.h"
15 
16 #include "llvm/TableGen/Error.h"
17 #include "llvm/TableGen/Record.h"
18 #include "llvm/TableGen/TableGenBackend.h"
19 #include <cctype>
20 #include <map>
21 #include <set>
22 #include <string>
23 using namespace llvm;
24 using namespace clang;
25 using namespace clang::tblgen;
26 
27 /// ClangASTNodesEmitter - The top-level class emits .inc files containing
28 ///  declarations of Clang statements.
29 ///
30 namespace {
31 class ClangASTNodesEmitter {
32   // A map from a node to each of its derived nodes.
33   typedef std::multimap<ASTNode, ASTNode> ChildMap;
34   typedef ChildMap::const_iterator ChildIterator;
35 
36   std::set<ASTNode> PrioritizedClasses;
37   RecordKeeper &Records;
38   ASTNode Root;
39   const std::string &NodeClassName;
40   const std::string &BaseSuffix;
41   std::string MacroHierarchyName;
42   ChildMap Tree;
43 
44   // Create a macro-ized version of a name
45   static std::string macroName(std::string S) {
46     for (unsigned i = 0; i < S.size(); ++i)
47       S[i] = std::toupper(S[i]);
48 
49     return S;
50   }
51 
52   const std::string &macroHierarchyName() {
53     assert(Root && "root node not yet derived!");
54     if (MacroHierarchyName.empty())
55       MacroHierarchyName = macroName(std::string(Root.getName()));
56     return MacroHierarchyName;
57   }
58 
59   // Return the name to be printed in the base field. Normally this is
60   // the record's name plus the base suffix, but if it is the root node and
61   // the suffix is non-empty, it's just the suffix.
62   std::string baseName(ASTNode node) {
63     if (node == Root && !BaseSuffix.empty())
64       return BaseSuffix;
65 
66     return node.getName().str() + BaseSuffix;
67   }
68 
69   void deriveChildTree();
70 
71   std::pair<ASTNode, ASTNode> EmitNode(raw_ostream& OS, ASTNode Base);
72 public:
73   explicit ClangASTNodesEmitter(RecordKeeper &R, const std::string &N,
74                                 const std::string &S,
75                                 std::string_view PriorizeIfSubclassOf)
76       : Records(R), NodeClassName(N), BaseSuffix(S) {
77     auto vecPrioritized =
78         PriorizeIfSubclassOf.empty()
79             ? std::vector<Record *>{}
80             : R.getAllDerivedDefinitions(PriorizeIfSubclassOf);
81     PrioritizedClasses =
82         std::set<ASTNode>(vecPrioritized.begin(), vecPrioritized.end());
83   }
84 
85   // run - Output the .inc file contents
86   void run(raw_ostream &OS);
87 };
88 } // end anonymous namespace
89 
90 //===----------------------------------------------------------------------===//
91 // Statement Node Tables (.inc file) generation.
92 //===----------------------------------------------------------------------===//
93 
94 // Returns the first and last non-abstract subrecords
95 // Called recursively to ensure that nodes remain contiguous
96 std::pair<ASTNode, ASTNode> ClangASTNodesEmitter::EmitNode(raw_ostream &OS,
97                                                            ASTNode Base) {
98   std::string BaseName = macroName(std::string(Base.getName()));
99 
100   ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
101   bool HasChildren = (i != e);
102 
103   ASTNode First, Last;
104   if (!Base.isAbstract())
105     First = Last = Base;
106 
107   auto comp = [this](ASTNode LHS, ASTNode RHS) {
108     auto LHSPrioritized = PrioritizedClasses.count(LHS) > 0;
109     auto RHSPrioritized = PrioritizedClasses.count(RHS) > 0;
110     if (LHSPrioritized && !RHSPrioritized)
111       return true;
112     if (!LHSPrioritized && RHSPrioritized)
113       return false;
114 
115     return LHS.getName() > RHS.getName();
116   };
117   auto SortedChildren = std::set<ASTNode, decltype(comp)>(comp);
118 
119   for (; i != e; ++i) {
120     SortedChildren.insert(i->second);
121   }
122 
123   for (const auto &Child : SortedChildren) {
124     bool Abstract = Child.isAbstract();
125     std::string NodeName = macroName(std::string(Child.getName()));
126 
127     OS << "#ifndef " << NodeName << "\n";
128     OS << "#  define " << NodeName << "(Type, Base) "
129         << BaseName << "(Type, Base)\n";
130     OS << "#endif\n";
131 
132     if (Abstract) OS << "ABSTRACT_" << macroHierarchyName() << "(";
133     OS << NodeName << "(" << Child.getName() << ", " << baseName(Base) << ")";
134     if (Abstract) OS << ")";
135     OS << "\n";
136 
137     auto Result = EmitNode(OS, Child);
138     assert(Result.first && Result.second && "node didn't have children?");
139 
140     // Update the range of Base.
141     if (!First) First = Result.first;
142     Last = Result.second;
143 
144     OS << "#undef " << NodeName << "\n\n";
145   }
146 
147   // If there aren't first/last nodes, it must be because there were no
148   // children and this node was abstract, which is not a sensible combination.
149   if (!First) {
150     PrintFatalError(Base.getLoc(), "abstract node has no children");
151   }
152   assert(Last && "set First without Last");
153 
154   if (HasChildren) {
155     // Use FOO_RANGE unless this is the last of the ranges, in which case
156     // use LAST_FOO_RANGE.
157     if (Base == Root)
158       OS << "LAST_" << macroHierarchyName() << "_RANGE(";
159     else
160       OS << macroHierarchyName() << "_RANGE(";
161     OS << Base.getName() << ", " << First.getName() << ", "
162        << Last.getName() << ")\n\n";
163   }
164 
165   return std::make_pair(First, Last);
166 }
167 
168 void ClangASTNodesEmitter::deriveChildTree() {
169   assert(!Root && "already computed tree");
170 
171   // Emit statements
172   const std::vector<Record*> Stmts
173     = Records.getAllDerivedDefinitions(NodeClassName);
174 
175   for (auto *R : Stmts) {
176     if (auto B = R->getValueAsOptionalDef(BaseFieldName))
177       Tree.insert(std::make_pair(B, R));
178     else if (Root)
179       PrintFatalError(R->getLoc(),
180                       Twine("multiple root nodes in \"") + NodeClassName
181                         + "\" hierarchy");
182     else
183       Root = R;
184   }
185 
186   if (!Root)
187     PrintFatalError(Twine("didn't find root node in \"") + NodeClassName
188                       + "\" hierarchy");
189 }
190 
191 void ClangASTNodesEmitter::run(raw_ostream &OS) {
192   deriveChildTree();
193 
194   emitSourceFileHeader("List of AST nodes of a particular kind", OS, Records);
195 
196   // Write the preamble
197   OS << "#ifndef ABSTRACT_" << macroHierarchyName() << "\n";
198   OS << "#  define ABSTRACT_" << macroHierarchyName() << "(Type) Type\n";
199   OS << "#endif\n";
200 
201   OS << "#ifndef " << macroHierarchyName() << "_RANGE\n";
202   OS << "#  define "
203      << macroHierarchyName() << "_RANGE(Base, First, Last)\n";
204   OS << "#endif\n\n";
205 
206   OS << "#ifndef LAST_" << macroHierarchyName() << "_RANGE\n";
207   OS << "#  define LAST_" << macroHierarchyName()
208      << "_RANGE(Base, First, Last) " << macroHierarchyName()
209      << "_RANGE(Base, First, Last)\n";
210   OS << "#endif\n\n";
211 
212   EmitNode(OS, Root);
213 
214   OS << "#undef " << macroHierarchyName() << "\n";
215   OS << "#undef " << macroHierarchyName() << "_RANGE\n";
216   OS << "#undef LAST_" << macroHierarchyName() << "_RANGE\n";
217   OS << "#undef ABSTRACT_" << macroHierarchyName() << "\n";
218 }
219 
220 void clang::EmitClangASTNodes(RecordKeeper &RK, raw_ostream &OS,
221                               const std::string &N, const std::string &S,
222                               std::string_view PriorizeIfSubclassOf) {
223   ClangASTNodesEmitter(RK, N, S, PriorizeIfSubclassOf).run(OS);
224 }
225 
226 void printDeclContext(const std::multimap<Record *, Record *> &Tree,
227                       Record *DeclContext, raw_ostream &OS) {
228   if (!DeclContext->getValueAsBit(AbstractFieldName))
229     OS << "DECL_CONTEXT(" << DeclContext->getName() << ")\n";
230   auto i = Tree.lower_bound(DeclContext);
231   auto end = Tree.upper_bound(DeclContext);
232   for (; i != end; ++i) {
233     printDeclContext(Tree, i->second, OS);
234   }
235 }
236 
237 // Emits and addendum to a .inc file to enumerate the clang declaration
238 // contexts.
239 void clang::EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) {
240   // FIXME: Find a .td file format to allow for this to be represented better.
241 
242   emitSourceFileHeader("List of AST Decl nodes", OS, Records);
243 
244   OS << "#ifndef DECL_CONTEXT\n";
245   OS << "#  define DECL_CONTEXT(DECL)\n";
246   OS << "#endif\n";
247 
248   std::vector<Record *> DeclContextsVector =
249       Records.getAllDerivedDefinitions(DeclContextNodeClassName);
250   std::vector<Record *> Decls =
251       Records.getAllDerivedDefinitions(DeclNodeClassName);
252 
253   std::multimap<Record *, Record *> Tree;
254 
255   const std::vector<Record *> Stmts =
256       Records.getAllDerivedDefinitions(DeclNodeClassName);
257 
258   for (auto *R : Stmts) {
259     if (auto *B = R->getValueAsOptionalDef(BaseFieldName))
260       Tree.insert(std::make_pair(B, R));
261   }
262 
263   for (auto *DeclContext : DeclContextsVector) {
264     printDeclContext(Tree, DeclContext, OS);
265   }
266 
267   OS << "#undef DECL_CONTEXT\n";
268 }
269