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