ClangASTNodesEmitter.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #include "ASTTableGen.h"
  13. #include "TableGenBackends.h"
  14. #include "llvm/TableGen/Error.h"
  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. using namespace clang;
  23. using namespace clang::tblgen;
  24. /// ClangASTNodesEmitter - The top-level class emits .inc files containing
  25. /// declarations of Clang statements.
  26. ///
  27. namespace {
  28. class ClangASTNodesEmitter {
  29. // A map from a node to each of its derived nodes.
  30. typedef std::multimap<ASTNode, ASTNode> ChildMap;
  31. typedef ChildMap::const_iterator ChildIterator;
  32. RecordKeeper &Records;
  33. ASTNode Root;
  34. const std::string &NodeClassName;
  35. const std::string &BaseSuffix;
  36. std::string MacroHierarchyName;
  37. ChildMap Tree;
  38. // Create a macro-ized version of a name
  39. static std::string macroName(std::string S) {
  40. for (unsigned i = 0; i < S.size(); ++i)
  41. S[i] = std::toupper(S[i]);
  42. return S;
  43. }
  44. const std::string &macroHierarchyName() {
  45. assert(Root && "root node not yet derived!");
  46. if (MacroHierarchyName.empty())
  47. MacroHierarchyName = macroName(std::string(Root.getName()));
  48. return MacroHierarchyName;
  49. }
  50. // Return the name to be printed in the base field. Normally this is
  51. // the record's name plus the base suffix, but if it is the root node and
  52. // the suffix is non-empty, it's just the suffix.
  53. std::string baseName(ASTNode node) {
  54. if (node == Root && !BaseSuffix.empty())
  55. return BaseSuffix;
  56. return node.getName().str() + BaseSuffix;
  57. }
  58. void deriveChildTree();
  59. std::pair<ASTNode, ASTNode> EmitNode(raw_ostream& OS, ASTNode Base);
  60. public:
  61. explicit ClangASTNodesEmitter(RecordKeeper &R, const std::string &N,
  62. const std::string &S)
  63. : Records(R), NodeClassName(N), BaseSuffix(S) {}
  64. // run - Output the .inc file contents
  65. void run(raw_ostream &OS);
  66. };
  67. } // end anonymous namespace
  68. //===----------------------------------------------------------------------===//
  69. // Statement Node Tables (.inc file) generation.
  70. //===----------------------------------------------------------------------===//
  71. // Returns the first and last non-abstract subrecords
  72. // Called recursively to ensure that nodes remain contiguous
  73. std::pair<ASTNode, ASTNode> ClangASTNodesEmitter::EmitNode(raw_ostream &OS,
  74. ASTNode Base) {
  75. std::string BaseName = macroName(std::string(Base.getName()));
  76. ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
  77. bool HasChildren = (i != e);
  78. ASTNode First, Last;
  79. if (!Base.isAbstract())
  80. First = Last = Base;
  81. for (; i != e; ++i) {
  82. ASTNode Child = i->second;
  83. bool Abstract = Child.isAbstract();
  84. std::string NodeName = macroName(std::string(Child.getName()));
  85. OS << "#ifndef " << NodeName << "\n";
  86. OS << "# define " << NodeName << "(Type, Base) "
  87. << BaseName << "(Type, Base)\n";
  88. OS << "#endif\n";
  89. if (Abstract) OS << "ABSTRACT_" << macroHierarchyName() << "(";
  90. OS << NodeName << "(" << Child.getName() << ", " << baseName(Base) << ")";
  91. if (Abstract) OS << ")";
  92. OS << "\n";
  93. auto Result = EmitNode(OS, Child);
  94. assert(Result.first && Result.second && "node didn't have children?");
  95. // Update the range of Base.
  96. if (!First) First = Result.first;
  97. Last = Result.second;
  98. OS << "#undef " << NodeName << "\n\n";
  99. }
  100. // If there aren't first/last nodes, it must be because there were no
  101. // children and this node was abstract, which is not a sensible combination.
  102. if (!First) {
  103. PrintFatalError(Base.getLoc(), "abstract node has no children");
  104. }
  105. assert(Last && "set First without Last");
  106. if (HasChildren) {
  107. // Use FOO_RANGE unless this is the last of the ranges, in which case
  108. // use LAST_FOO_RANGE.
  109. if (Base == Root)
  110. OS << "LAST_" << macroHierarchyName() << "_RANGE(";
  111. else
  112. OS << macroHierarchyName() << "_RANGE(";
  113. OS << Base.getName() << ", " << First.getName() << ", "
  114. << Last.getName() << ")\n\n";
  115. }
  116. return std::make_pair(First, Last);
  117. }
  118. void ClangASTNodesEmitter::deriveChildTree() {
  119. assert(!Root && "already computed tree");
  120. // Emit statements
  121. const std::vector<Record*> Stmts
  122. = Records.getAllDerivedDefinitions(NodeClassName);
  123. for (unsigned i = 0, e = Stmts.size(); i != e; ++i) {
  124. Record *R = Stmts[i];
  125. if (auto B = R->getValueAsOptionalDef(BaseFieldName))
  126. Tree.insert(std::make_pair(B, R));
  127. else if (Root)
  128. PrintFatalError(R->getLoc(),
  129. Twine("multiple root nodes in \"") + NodeClassName
  130. + "\" hierarchy");
  131. else
  132. Root = R;
  133. }
  134. if (!Root)
  135. PrintFatalError(Twine("didn't find root node in \"") + NodeClassName
  136. + "\" hierarchy");
  137. }
  138. void ClangASTNodesEmitter::run(raw_ostream &OS) {
  139. deriveChildTree();
  140. emitSourceFileHeader("List of AST nodes of a particular kind", OS);
  141. // Write the preamble
  142. OS << "#ifndef ABSTRACT_" << macroHierarchyName() << "\n";
  143. OS << "# define ABSTRACT_" << macroHierarchyName() << "(Type) Type\n";
  144. OS << "#endif\n";
  145. OS << "#ifndef " << macroHierarchyName() << "_RANGE\n";
  146. OS << "# define "
  147. << macroHierarchyName() << "_RANGE(Base, First, Last)\n";
  148. OS << "#endif\n\n";
  149. OS << "#ifndef LAST_" << macroHierarchyName() << "_RANGE\n";
  150. OS << "# define LAST_"
  151. << macroHierarchyName() << "_RANGE(Base, First, Last) "
  152. << macroHierarchyName() << "_RANGE(Base, First, Last)\n";
  153. OS << "#endif\n\n";
  154. EmitNode(OS, Root);
  155. OS << "#undef " << macroHierarchyName() << "\n";
  156. OS << "#undef " << macroHierarchyName() << "_RANGE\n";
  157. OS << "#undef LAST_" << macroHierarchyName() << "_RANGE\n";
  158. OS << "#undef ABSTRACT_" << macroHierarchyName() << "\n";
  159. }
  160. void clang::EmitClangASTNodes(RecordKeeper &RK, raw_ostream &OS,
  161. const std::string &N, const std::string &S) {
  162. ClangASTNodesEmitter(RK, N, S).run(OS);
  163. }
  164. // Emits and addendum to a .inc file to enumerate the clang declaration
  165. // contexts.
  166. void clang::EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) {
  167. // FIXME: Find a .td file format to allow for this to be represented better.
  168. emitSourceFileHeader("List of AST Decl nodes", OS);
  169. OS << "#ifndef DECL_CONTEXT\n";
  170. OS << "# define DECL_CONTEXT(DECL)\n";
  171. OS << "#endif\n";
  172. OS << "#ifndef DECL_CONTEXT_BASE\n";
  173. OS << "# define DECL_CONTEXT_BASE(DECL) DECL_CONTEXT(DECL)\n";
  174. OS << "#endif\n";
  175. typedef std::set<Record*> RecordSet;
  176. typedef std::vector<Record*> RecordVector;
  177. RecordVector DeclContextsVector
  178. = Records.getAllDerivedDefinitions(DeclContextNodeClassName);
  179. RecordVector Decls = Records.getAllDerivedDefinitions(DeclNodeClassName);
  180. RecordSet DeclContexts (DeclContextsVector.begin(), DeclContextsVector.end());
  181. for (RecordVector::iterator i = Decls.begin(), e = Decls.end(); i != e; ++i) {
  182. Record *R = *i;
  183. if (Record *B = R->getValueAsOptionalDef(BaseFieldName)) {
  184. if (DeclContexts.find(B) != DeclContexts.end()) {
  185. OS << "DECL_CONTEXT_BASE(" << B->getName() << ")\n";
  186. DeclContexts.erase(B);
  187. }
  188. }
  189. }
  190. // To keep identical order, RecordVector may be used
  191. // instead of RecordSet.
  192. for (RecordVector::iterator
  193. i = DeclContextsVector.begin(), e = DeclContextsVector.end();
  194. i != e; ++i)
  195. if (DeclContexts.find(*i) != DeclContexts.end())
  196. OS << "DECL_CONTEXT(" << (*i)->getName() << ")\n";
  197. OS << "#undef DECL_CONTEXT\n";
  198. OS << "#undef DECL_CONTEXT_BASE\n";
  199. }