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