xref: /freebsd/contrib/llvm-project/clang/utils/TableGen/ClangASTNodesEmitter.cpp (revision 480093f4440d54b30b3025afeac24b48f2ba7a2e)
10b57cec5SDimitry Andric //=== ClangASTNodesEmitter.cpp - Generate Clang AST node tables -*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // These tablegen backends emit Clang AST node tables
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
13*480093f4SDimitry Andric #include "ASTTableGen.h"
14a7dea167SDimitry Andric #include "TableGenBackends.h"
15a7dea167SDimitry Andric 
16*480093f4SDimitry Andric #include "llvm/TableGen/Error.h"
170b57cec5SDimitry Andric #include "llvm/TableGen/Record.h"
180b57cec5SDimitry Andric #include "llvm/TableGen/TableGenBackend.h"
190b57cec5SDimitry Andric #include <cctype>
200b57cec5SDimitry Andric #include <map>
210b57cec5SDimitry Andric #include <set>
220b57cec5SDimitry Andric #include <string>
230b57cec5SDimitry Andric using namespace llvm;
24*480093f4SDimitry Andric using namespace clang;
25*480093f4SDimitry Andric using namespace clang::tblgen;
260b57cec5SDimitry Andric 
270b57cec5SDimitry Andric /// ClangASTNodesEmitter - The top-level class emits .inc files containing
280b57cec5SDimitry Andric ///  declarations of Clang statements.
290b57cec5SDimitry Andric ///
300b57cec5SDimitry Andric namespace {
310b57cec5SDimitry Andric class ClangASTNodesEmitter {
320b57cec5SDimitry Andric   // A map from a node to each of its derived nodes.
33*480093f4SDimitry Andric   typedef std::multimap<ASTNode, ASTNode> ChildMap;
340b57cec5SDimitry Andric   typedef ChildMap::const_iterator ChildIterator;
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric   RecordKeeper &Records;
37*480093f4SDimitry Andric   ASTNode Root;
38*480093f4SDimitry Andric   const std::string &NodeClassName;
390b57cec5SDimitry Andric   const std::string &BaseSuffix;
40*480093f4SDimitry Andric   std::string MacroHierarchyName;
41*480093f4SDimitry Andric   ChildMap Tree;
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   // Create a macro-ized version of a name
440b57cec5SDimitry Andric   static std::string macroName(std::string S) {
450b57cec5SDimitry Andric     for (unsigned i = 0; i < S.size(); ++i)
460b57cec5SDimitry Andric       S[i] = std::toupper(S[i]);
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric     return S;
490b57cec5SDimitry Andric   }
500b57cec5SDimitry Andric 
51*480093f4SDimitry Andric   const std::string &macroHierarchyName() {
52*480093f4SDimitry Andric     assert(Root && "root node not yet derived!");
53*480093f4SDimitry Andric     if (MacroHierarchyName.empty())
54*480093f4SDimitry Andric       MacroHierarchyName = macroName(Root.getName());
55*480093f4SDimitry Andric     return MacroHierarchyName;
56*480093f4SDimitry Andric   }
57*480093f4SDimitry Andric 
580b57cec5SDimitry Andric   // Return the name to be printed in the base field. Normally this is
590b57cec5SDimitry Andric   // the record's name plus the base suffix, but if it is the root node and
600b57cec5SDimitry Andric   // the suffix is non-empty, it's just the suffix.
61*480093f4SDimitry Andric   std::string baseName(ASTNode node) {
62*480093f4SDimitry Andric     if (node == Root && !BaseSuffix.empty())
630b57cec5SDimitry Andric       return BaseSuffix;
640b57cec5SDimitry Andric 
65*480093f4SDimitry Andric     return node.getName().str() + BaseSuffix;
660b57cec5SDimitry Andric   }
670b57cec5SDimitry Andric 
68*480093f4SDimitry Andric   void deriveChildTree();
69*480093f4SDimitry Andric 
70*480093f4SDimitry Andric   std::pair<ASTNode, ASTNode> EmitNode(raw_ostream& OS, ASTNode Base);
710b57cec5SDimitry Andric public:
720b57cec5SDimitry Andric   explicit ClangASTNodesEmitter(RecordKeeper &R, const std::string &N,
730b57cec5SDimitry Andric                                 const std::string &S)
74*480093f4SDimitry Andric     : Records(R), NodeClassName(N), BaseSuffix(S) {}
750b57cec5SDimitry Andric 
760b57cec5SDimitry Andric   // run - Output the .inc file contents
770b57cec5SDimitry Andric   void run(raw_ostream &OS);
780b57cec5SDimitry Andric };
790b57cec5SDimitry Andric } // end anonymous namespace
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
820b57cec5SDimitry Andric // Statement Node Tables (.inc file) generation.
830b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric // Returns the first and last non-abstract subrecords
860b57cec5SDimitry Andric // Called recursively to ensure that nodes remain contiguous
87*480093f4SDimitry Andric std::pair<ASTNode, ASTNode> ClangASTNodesEmitter::EmitNode(raw_ostream &OS,
88*480093f4SDimitry Andric                                                            ASTNode Base) {
89*480093f4SDimitry Andric   std::string BaseName = macroName(Base.getName());
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric   ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
92*480093f4SDimitry Andric   bool HasChildren = (i != e);
930b57cec5SDimitry Andric 
94*480093f4SDimitry Andric   ASTNode First, Last;
95*480093f4SDimitry Andric   if (!Base.isAbstract())
960b57cec5SDimitry Andric     First = Last = Base;
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   for (; i != e; ++i) {
99*480093f4SDimitry Andric     ASTNode Child = i->second;
100*480093f4SDimitry Andric     bool Abstract = Child.isAbstract();
101*480093f4SDimitry Andric     std::string NodeName = macroName(Child.getName());
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric     OS << "#ifndef " << NodeName << "\n";
1040b57cec5SDimitry Andric     OS << "#  define " << NodeName << "(Type, Base) "
1050b57cec5SDimitry Andric         << BaseName << "(Type, Base)\n";
1060b57cec5SDimitry Andric     OS << "#endif\n";
1070b57cec5SDimitry Andric 
108*480093f4SDimitry Andric     if (Abstract) OS << "ABSTRACT_" << macroHierarchyName() << "(";
109*480093f4SDimitry Andric     OS << NodeName << "(" << Child.getName() << ", " << baseName(Base) << ")";
110*480093f4SDimitry Andric     if (Abstract) OS << ")";
111*480093f4SDimitry Andric     OS << "\n";
1120b57cec5SDimitry Andric 
113*480093f4SDimitry Andric     auto Result = EmitNode(OS, Child);
114*480093f4SDimitry Andric     assert(Result.first && Result.second && "node didn't have children?");
115*480093f4SDimitry Andric 
116*480093f4SDimitry Andric     // Update the range of Base.
117*480093f4SDimitry Andric     if (!First) First = Result.first;
1180b57cec5SDimitry Andric     Last = Result.second;
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric     OS << "#undef " << NodeName << "\n\n";
1210b57cec5SDimitry Andric   }
1220b57cec5SDimitry Andric 
123*480093f4SDimitry Andric   // If there aren't first/last nodes, it must be because there were no
124*480093f4SDimitry Andric   // children and this node was abstract, which is not a sensible combination.
125*480093f4SDimitry Andric   if (!First) {
126*480093f4SDimitry Andric     PrintFatalError(Base.getLoc(), "abstract node has no children");
127*480093f4SDimitry Andric   }
128*480093f4SDimitry Andric   assert(Last && "set First without Last");
129*480093f4SDimitry Andric 
130*480093f4SDimitry Andric   if (HasChildren) {
131*480093f4SDimitry Andric     // Use FOO_RANGE unless this is the last of the ranges, in which case
132*480093f4SDimitry Andric     // use LAST_FOO_RANGE.
133*480093f4SDimitry Andric     if (Base == Root)
134*480093f4SDimitry Andric       OS << "LAST_" << macroHierarchyName() << "_RANGE(";
1350b57cec5SDimitry Andric     else
136*480093f4SDimitry Andric       OS << macroHierarchyName() << "_RANGE(";
137*480093f4SDimitry Andric     OS << Base.getName() << ", " << First.getName() << ", "
138*480093f4SDimitry Andric        << Last.getName() << ")\n\n";
1390b57cec5SDimitry Andric   }
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   return std::make_pair(First, Last);
1420b57cec5SDimitry Andric }
1430b57cec5SDimitry Andric 
144*480093f4SDimitry Andric void ClangASTNodesEmitter::deriveChildTree() {
145*480093f4SDimitry Andric   assert(!Root && "already computed tree");
1460b57cec5SDimitry Andric 
1470b57cec5SDimitry Andric   // Emit statements
1480b57cec5SDimitry Andric   const std::vector<Record*> Stmts
149*480093f4SDimitry Andric     = Records.getAllDerivedDefinitions(NodeClassName);
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric   for (unsigned i = 0, e = Stmts.size(); i != e; ++i) {
1520b57cec5SDimitry Andric     Record *R = Stmts[i];
1530b57cec5SDimitry Andric 
154*480093f4SDimitry Andric     if (auto B = R->getValueAsOptionalDef(BaseFieldName))
155*480093f4SDimitry Andric       Tree.insert(std::make_pair(B, R));
156*480093f4SDimitry Andric     else if (Root)
157*480093f4SDimitry Andric       PrintFatalError(R->getLoc(),
158*480093f4SDimitry Andric                       Twine("multiple root nodes in \"") + NodeClassName
159*480093f4SDimitry Andric                         + "\" hierarchy");
1600b57cec5SDimitry Andric     else
161*480093f4SDimitry Andric       Root = R;
1620b57cec5SDimitry Andric   }
1630b57cec5SDimitry Andric 
164*480093f4SDimitry Andric   if (!Root)
165*480093f4SDimitry Andric     PrintFatalError(Twine("didn't find root node in \"") + NodeClassName
166*480093f4SDimitry Andric                       + "\" hierarchy");
167*480093f4SDimitry Andric }
1680b57cec5SDimitry Andric 
169*480093f4SDimitry Andric void ClangASTNodesEmitter::run(raw_ostream &OS) {
170*480093f4SDimitry Andric   deriveChildTree();
171*480093f4SDimitry Andric 
172*480093f4SDimitry Andric   emitSourceFileHeader("List of AST nodes of a particular kind", OS);
173*480093f4SDimitry Andric 
174*480093f4SDimitry Andric   // Write the preamble
175*480093f4SDimitry Andric   OS << "#ifndef ABSTRACT_" << macroHierarchyName() << "\n";
176*480093f4SDimitry Andric   OS << "#  define ABSTRACT_" << macroHierarchyName() << "(Type) Type\n";
177*480093f4SDimitry Andric   OS << "#endif\n";
178*480093f4SDimitry Andric 
179*480093f4SDimitry Andric   OS << "#ifndef " << macroHierarchyName() << "_RANGE\n";
180*480093f4SDimitry Andric   OS << "#  define "
181*480093f4SDimitry Andric      << macroHierarchyName() << "_RANGE(Base, First, Last)\n";
182*480093f4SDimitry Andric   OS << "#endif\n\n";
183*480093f4SDimitry Andric 
184*480093f4SDimitry Andric   OS << "#ifndef LAST_" << macroHierarchyName() << "_RANGE\n";
185*480093f4SDimitry Andric   OS << "#  define LAST_"
186*480093f4SDimitry Andric      << macroHierarchyName() << "_RANGE(Base, First, Last) "
187*480093f4SDimitry Andric      << macroHierarchyName() << "_RANGE(Base, First, Last)\n";
188*480093f4SDimitry Andric   OS << "#endif\n\n";
189*480093f4SDimitry Andric 
190*480093f4SDimitry Andric   EmitNode(OS, Root);
191*480093f4SDimitry Andric 
192*480093f4SDimitry Andric   OS << "#undef " << macroHierarchyName() << "\n";
193*480093f4SDimitry Andric   OS << "#undef " << macroHierarchyName() << "_RANGE\n";
194*480093f4SDimitry Andric   OS << "#undef LAST_" << macroHierarchyName() << "_RANGE\n";
195*480093f4SDimitry Andric   OS << "#undef ABSTRACT_" << macroHierarchyName() << "\n";
1960b57cec5SDimitry Andric }
1970b57cec5SDimitry Andric 
198a7dea167SDimitry Andric void clang::EmitClangASTNodes(RecordKeeper &RK, raw_ostream &OS,
1990b57cec5SDimitry Andric                               const std::string &N, const std::string &S) {
2000b57cec5SDimitry Andric   ClangASTNodesEmitter(RK, N, S).run(OS);
2010b57cec5SDimitry Andric }
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric // Emits and addendum to a .inc file to enumerate the clang declaration
2040b57cec5SDimitry Andric // contexts.
205a7dea167SDimitry Andric void clang::EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) {
2060b57cec5SDimitry Andric   // FIXME: Find a .td file format to allow for this to be represented better.
2070b57cec5SDimitry Andric 
2080b57cec5SDimitry Andric   emitSourceFileHeader("List of AST Decl nodes", OS);
2090b57cec5SDimitry Andric 
2100b57cec5SDimitry Andric   OS << "#ifndef DECL_CONTEXT\n";
2110b57cec5SDimitry Andric   OS << "#  define DECL_CONTEXT(DECL)\n";
2120b57cec5SDimitry Andric   OS << "#endif\n";
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric   OS << "#ifndef DECL_CONTEXT_BASE\n";
2150b57cec5SDimitry Andric   OS << "#  define DECL_CONTEXT_BASE(DECL) DECL_CONTEXT(DECL)\n";
2160b57cec5SDimitry Andric   OS << "#endif\n";
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric   typedef std::set<Record*> RecordSet;
2190b57cec5SDimitry Andric   typedef std::vector<Record*> RecordVector;
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric   RecordVector DeclContextsVector
222*480093f4SDimitry Andric     = Records.getAllDerivedDefinitions(DeclContextNodeClassName);
223*480093f4SDimitry Andric   RecordVector Decls = Records.getAllDerivedDefinitions(DeclNodeClassName);
2240b57cec5SDimitry Andric   RecordSet DeclContexts (DeclContextsVector.begin(), DeclContextsVector.end());
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric   for (RecordVector::iterator i = Decls.begin(), e = Decls.end(); i != e; ++i) {
2270b57cec5SDimitry Andric     Record *R = *i;
2280b57cec5SDimitry Andric 
229*480093f4SDimitry Andric     if (Record *B = R->getValueAsOptionalDef(BaseFieldName)) {
2300b57cec5SDimitry Andric       if (DeclContexts.find(B) != DeclContexts.end()) {
2310b57cec5SDimitry Andric         OS << "DECL_CONTEXT_BASE(" << B->getName() << ")\n";
2320b57cec5SDimitry Andric         DeclContexts.erase(B);
2330b57cec5SDimitry Andric       }
2340b57cec5SDimitry Andric     }
2350b57cec5SDimitry Andric   }
2360b57cec5SDimitry Andric 
2370b57cec5SDimitry Andric   // To keep identical order, RecordVector may be used
2380b57cec5SDimitry Andric   // instead of RecordSet.
2390b57cec5SDimitry Andric   for (RecordVector::iterator
2400b57cec5SDimitry Andric          i = DeclContextsVector.begin(), e = DeclContextsVector.end();
2410b57cec5SDimitry Andric        i != e; ++i)
2420b57cec5SDimitry Andric     if (DeclContexts.find(*i) != DeclContexts.end())
2430b57cec5SDimitry Andric       OS << "DECL_CONTEXT(" << (*i)->getName() << ")\n";
2440b57cec5SDimitry Andric 
2450b57cec5SDimitry Andric   OS << "#undef DECL_CONTEXT\n";
2460b57cec5SDimitry Andric   OS << "#undef DECL_CONTEXT_BASE\n";
2470b57cec5SDimitry Andric }
248