ClangASTNodesEmitter.cpp 7.4 KB

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