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