ClangASTNodesEmitter.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //=== ClangASTNodesEmitter.cpp - Generate Clang AST node tables -*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // These tablegen backends emit Clang AST node tables
  11. //
  12. //===----------------------------------------------------------------------===//
  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() + 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. namespace clang {
  146. void EmitClangASTNodes(RecordKeeper &RK, raw_ostream &OS,
  147. const std::string &N, const std::string &S) {
  148. ClangASTNodesEmitter(RK, N, S).run(OS);
  149. }
  150. // Emits and addendum to a .inc file to enumerate the clang declaration
  151. // contexts.
  152. void EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) {
  153. // FIXME: Find a .td file format to allow for this to be represented better.
  154. emitSourceFileHeader("List of AST Decl nodes", OS);
  155. OS << "#ifndef DECL_CONTEXT\n";
  156. OS << "# define DECL_CONTEXT(DECL)\n";
  157. OS << "#endif\n";
  158. OS << "#ifndef DECL_CONTEXT_BASE\n";
  159. OS << "# define DECL_CONTEXT_BASE(DECL) DECL_CONTEXT(DECL)\n";
  160. OS << "#endif\n";
  161. typedef std::set<Record*> RecordSet;
  162. typedef std::vector<Record*> RecordVector;
  163. RecordVector DeclContextsVector
  164. = Records.getAllDerivedDefinitions("DeclContext");
  165. RecordVector Decls = Records.getAllDerivedDefinitions("Decl");
  166. RecordSet DeclContexts (DeclContextsVector.begin(), DeclContextsVector.end());
  167. for (RecordVector::iterator i = Decls.begin(), e = Decls.end(); i != e; ++i) {
  168. Record *R = *i;
  169. if (R->getValue("Base")) {
  170. Record *B = R->getValueAsDef("Base");
  171. if (DeclContexts.find(B) != DeclContexts.end()) {
  172. OS << "DECL_CONTEXT_BASE(" << B->getName() << ")\n";
  173. DeclContexts.erase(B);
  174. }
  175. }
  176. }
  177. // To keep identical order, RecordVector may be used
  178. // instead of RecordSet.
  179. for (RecordVector::iterator
  180. i = DeclContextsVector.begin(), e = DeclContextsVector.end();
  181. i != e; ++i)
  182. if (DeclContexts.find(*i) != DeclContexts.end())
  183. OS << "DECL_CONTEXT(" << (*i)->getName() << ")\n";
  184. OS << "#undef DECL_CONTEXT\n";
  185. OS << "#undef DECL_CONTEXT_BASE\n";
  186. }
  187. } // end namespace clang