10b57cec5SDimitry Andric //=== ClangASTNodesEmitter.cpp - Generate Clang AST node tables -*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // These tablegen backends emit Clang AST node tables 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 13480093f4SDimitry Andric #include "ASTTableGen.h" 14a7dea167SDimitry Andric #include "TableGenBackends.h" 15a7dea167SDimitry Andric 16480093f4SDimitry Andric #include "llvm/TableGen/Error.h" 170b57cec5SDimitry Andric #include "llvm/TableGen/Record.h" 180b57cec5SDimitry Andric #include "llvm/TableGen/TableGenBackend.h" 190b57cec5SDimitry Andric #include <cctype> 200b57cec5SDimitry Andric #include <map> 210b57cec5SDimitry Andric #include <set> 220b57cec5SDimitry Andric #include <string> 230b57cec5SDimitry Andric using namespace llvm; 24480093f4SDimitry Andric using namespace clang; 25480093f4SDimitry Andric using namespace clang::tblgen; 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric /// ClangASTNodesEmitter - The top-level class emits .inc files containing 280b57cec5SDimitry Andric /// declarations of Clang statements. 290b57cec5SDimitry Andric /// 300b57cec5SDimitry Andric namespace { 310b57cec5SDimitry Andric class ClangASTNodesEmitter { 320b57cec5SDimitry Andric // A map from a node to each of its derived nodes. 33480093f4SDimitry Andric typedef std::multimap<ASTNode, ASTNode> ChildMap; 340b57cec5SDimitry Andric typedef ChildMap::const_iterator ChildIterator; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric RecordKeeper &Records; 37480093f4SDimitry Andric ASTNode Root; 38480093f4SDimitry Andric const std::string &NodeClassName; 390b57cec5SDimitry Andric const std::string &BaseSuffix; 40480093f4SDimitry Andric std::string MacroHierarchyName; 41480093f4SDimitry Andric ChildMap Tree; 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric // Create a macro-ized version of a name 440b57cec5SDimitry Andric static std::string macroName(std::string S) { 450b57cec5SDimitry Andric for (unsigned i = 0; i < S.size(); ++i) 460b57cec5SDimitry Andric S[i] = std::toupper(S[i]); 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric return S; 490b57cec5SDimitry Andric } 500b57cec5SDimitry Andric 51480093f4SDimitry Andric const std::string ¯oHierarchyName() { 52480093f4SDimitry Andric assert(Root && "root node not yet derived!"); 53480093f4SDimitry Andric if (MacroHierarchyName.empty()) 545ffd83dbSDimitry Andric MacroHierarchyName = macroName(std::string(Root.getName())); 55480093f4SDimitry Andric return MacroHierarchyName; 56480093f4SDimitry Andric } 57480093f4SDimitry Andric 580b57cec5SDimitry Andric // Return the name to be printed in the base field. Normally this is 590b57cec5SDimitry Andric // the record's name plus the base suffix, but if it is the root node and 600b57cec5SDimitry Andric // the suffix is non-empty, it's just the suffix. 61480093f4SDimitry Andric std::string baseName(ASTNode node) { 62480093f4SDimitry Andric if (node == Root && !BaseSuffix.empty()) 630b57cec5SDimitry Andric return BaseSuffix; 640b57cec5SDimitry Andric 65480093f4SDimitry Andric return node.getName().str() + BaseSuffix; 660b57cec5SDimitry Andric } 670b57cec5SDimitry Andric 68480093f4SDimitry Andric void deriveChildTree(); 69480093f4SDimitry Andric 70480093f4SDimitry Andric std::pair<ASTNode, ASTNode> EmitNode(raw_ostream& OS, ASTNode Base); 710b57cec5SDimitry Andric public: 720b57cec5SDimitry Andric explicit ClangASTNodesEmitter(RecordKeeper &R, const std::string &N, 730b57cec5SDimitry Andric const std::string &S) 74480093f4SDimitry Andric : Records(R), NodeClassName(N), BaseSuffix(S) {} 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric // run - Output the .inc file contents 770b57cec5SDimitry Andric void run(raw_ostream &OS); 780b57cec5SDimitry Andric }; 790b57cec5SDimitry Andric } // end anonymous namespace 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 820b57cec5SDimitry Andric // Statement Node Tables (.inc file) generation. 830b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 840b57cec5SDimitry Andric 850b57cec5SDimitry Andric // Returns the first and last non-abstract subrecords 860b57cec5SDimitry Andric // Called recursively to ensure that nodes remain contiguous 87480093f4SDimitry Andric std::pair<ASTNode, ASTNode> ClangASTNodesEmitter::EmitNode(raw_ostream &OS, 88480093f4SDimitry Andric ASTNode Base) { 895ffd83dbSDimitry Andric std::string BaseName = macroName(std::string(Base.getName())); 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base); 92480093f4SDimitry Andric bool HasChildren = (i != e); 930b57cec5SDimitry Andric 94480093f4SDimitry Andric ASTNode First, Last; 95480093f4SDimitry Andric if (!Base.isAbstract()) 960b57cec5SDimitry Andric First = Last = Base; 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric for (; i != e; ++i) { 99480093f4SDimitry Andric ASTNode Child = i->second; 100480093f4SDimitry Andric bool Abstract = Child.isAbstract(); 1015ffd83dbSDimitry Andric std::string NodeName = macroName(std::string(Child.getName())); 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric OS << "#ifndef " << NodeName << "\n"; 1040b57cec5SDimitry Andric OS << "# define " << NodeName << "(Type, Base) " 1050b57cec5SDimitry Andric << BaseName << "(Type, Base)\n"; 1060b57cec5SDimitry Andric OS << "#endif\n"; 1070b57cec5SDimitry Andric 108480093f4SDimitry Andric if (Abstract) OS << "ABSTRACT_" << macroHierarchyName() << "("; 109480093f4SDimitry Andric OS << NodeName << "(" << Child.getName() << ", " << baseName(Base) << ")"; 110480093f4SDimitry Andric if (Abstract) OS << ")"; 111480093f4SDimitry Andric OS << "\n"; 1120b57cec5SDimitry Andric 113480093f4SDimitry Andric auto Result = EmitNode(OS, Child); 114480093f4SDimitry Andric assert(Result.first && Result.second && "node didn't have children?"); 115480093f4SDimitry Andric 116480093f4SDimitry Andric // Update the range of Base. 117480093f4SDimitry Andric if (!First) First = Result.first; 1180b57cec5SDimitry Andric Last = Result.second; 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric OS << "#undef " << NodeName << "\n\n"; 1210b57cec5SDimitry Andric } 1220b57cec5SDimitry Andric 123480093f4SDimitry Andric // If there aren't first/last nodes, it must be because there were no 124480093f4SDimitry Andric // children and this node was abstract, which is not a sensible combination. 125480093f4SDimitry Andric if (!First) { 126480093f4SDimitry Andric PrintFatalError(Base.getLoc(), "abstract node has no children"); 127480093f4SDimitry Andric } 128480093f4SDimitry Andric assert(Last && "set First without Last"); 129480093f4SDimitry Andric 130480093f4SDimitry Andric if (HasChildren) { 131480093f4SDimitry Andric // Use FOO_RANGE unless this is the last of the ranges, in which case 132480093f4SDimitry Andric // use LAST_FOO_RANGE. 133480093f4SDimitry Andric if (Base == Root) 134480093f4SDimitry Andric OS << "LAST_" << macroHierarchyName() << "_RANGE("; 1350b57cec5SDimitry Andric else 136480093f4SDimitry Andric OS << macroHierarchyName() << "_RANGE("; 137480093f4SDimitry Andric OS << Base.getName() << ", " << First.getName() << ", " 138480093f4SDimitry Andric << Last.getName() << ")\n\n"; 1390b57cec5SDimitry Andric } 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric return std::make_pair(First, Last); 1420b57cec5SDimitry Andric } 1430b57cec5SDimitry Andric 144480093f4SDimitry Andric void ClangASTNodesEmitter::deriveChildTree() { 145480093f4SDimitry Andric assert(!Root && "already computed tree"); 1460b57cec5SDimitry Andric 1470b57cec5SDimitry Andric // Emit statements 1480b57cec5SDimitry Andric const std::vector<Record*> Stmts 149480093f4SDimitry Andric = Records.getAllDerivedDefinitions(NodeClassName); 1500b57cec5SDimitry Andric 1510b57cec5SDimitry Andric for (unsigned i = 0, e = Stmts.size(); i != e; ++i) { 1520b57cec5SDimitry Andric Record *R = Stmts[i]; 1530b57cec5SDimitry Andric 154480093f4SDimitry Andric if (auto B = R->getValueAsOptionalDef(BaseFieldName)) 155480093f4SDimitry Andric Tree.insert(std::make_pair(B, R)); 156480093f4SDimitry Andric else if (Root) 157480093f4SDimitry Andric PrintFatalError(R->getLoc(), 158480093f4SDimitry Andric Twine("multiple root nodes in \"") + NodeClassName 159480093f4SDimitry Andric + "\" hierarchy"); 1600b57cec5SDimitry Andric else 161480093f4SDimitry Andric Root = R; 1620b57cec5SDimitry Andric } 1630b57cec5SDimitry Andric 164480093f4SDimitry Andric if (!Root) 165480093f4SDimitry Andric PrintFatalError(Twine("didn't find root node in \"") + NodeClassName 166480093f4SDimitry Andric + "\" hierarchy"); 167480093f4SDimitry Andric } 1680b57cec5SDimitry Andric 169480093f4SDimitry Andric void ClangASTNodesEmitter::run(raw_ostream &OS) { 170480093f4SDimitry Andric deriveChildTree(); 171480093f4SDimitry Andric 172*5f757f3fSDimitry Andric emitSourceFileHeader("List of AST nodes of a particular kind", OS, Records); 173480093f4SDimitry Andric 174480093f4SDimitry Andric // Write the preamble 175480093f4SDimitry Andric OS << "#ifndef ABSTRACT_" << macroHierarchyName() << "\n"; 176480093f4SDimitry Andric OS << "# define ABSTRACT_" << macroHierarchyName() << "(Type) Type\n"; 177480093f4SDimitry Andric OS << "#endif\n"; 178480093f4SDimitry Andric 179480093f4SDimitry Andric OS << "#ifndef " << macroHierarchyName() << "_RANGE\n"; 180480093f4SDimitry Andric OS << "# define " 181480093f4SDimitry Andric << macroHierarchyName() << "_RANGE(Base, First, Last)\n"; 182480093f4SDimitry Andric OS << "#endif\n\n"; 183480093f4SDimitry Andric 184480093f4SDimitry Andric OS << "#ifndef LAST_" << macroHierarchyName() << "_RANGE\n"; 185480093f4SDimitry Andric OS << "# define LAST_" 186480093f4SDimitry Andric << macroHierarchyName() << "_RANGE(Base, First, Last) " 187480093f4SDimitry Andric << macroHierarchyName() << "_RANGE(Base, First, Last)\n"; 188480093f4SDimitry Andric OS << "#endif\n\n"; 189480093f4SDimitry Andric 190480093f4SDimitry Andric EmitNode(OS, Root); 191480093f4SDimitry Andric 192480093f4SDimitry Andric OS << "#undef " << macroHierarchyName() << "\n"; 193480093f4SDimitry Andric OS << "#undef " << macroHierarchyName() << "_RANGE\n"; 194480093f4SDimitry Andric OS << "#undef LAST_" << macroHierarchyName() << "_RANGE\n"; 195480093f4SDimitry Andric OS << "#undef ABSTRACT_" << macroHierarchyName() << "\n"; 1960b57cec5SDimitry Andric } 1970b57cec5SDimitry Andric 198a7dea167SDimitry Andric void clang::EmitClangASTNodes(RecordKeeper &RK, raw_ostream &OS, 1990b57cec5SDimitry Andric const std::string &N, const std::string &S) { 2000b57cec5SDimitry Andric ClangASTNodesEmitter(RK, N, S).run(OS); 2010b57cec5SDimitry Andric } 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric // Emits and addendum to a .inc file to enumerate the clang declaration 2040b57cec5SDimitry Andric // contexts. 205a7dea167SDimitry Andric void clang::EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) { 2060b57cec5SDimitry Andric // FIXME: Find a .td file format to allow for this to be represented better. 2070b57cec5SDimitry Andric 208*5f757f3fSDimitry Andric emitSourceFileHeader("List of AST Decl nodes", OS, Records); 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric OS << "#ifndef DECL_CONTEXT\n"; 2110b57cec5SDimitry Andric OS << "# define DECL_CONTEXT(DECL)\n"; 2120b57cec5SDimitry Andric OS << "#endif\n"; 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric OS << "#ifndef DECL_CONTEXT_BASE\n"; 2150b57cec5SDimitry Andric OS << "# define DECL_CONTEXT_BASE(DECL) DECL_CONTEXT(DECL)\n"; 2160b57cec5SDimitry Andric OS << "#endif\n"; 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric typedef std::set<Record*> RecordSet; 2190b57cec5SDimitry Andric typedef std::vector<Record*> RecordVector; 2200b57cec5SDimitry Andric 2210b57cec5SDimitry Andric RecordVector DeclContextsVector 222480093f4SDimitry Andric = Records.getAllDerivedDefinitions(DeclContextNodeClassName); 223480093f4SDimitry Andric RecordVector Decls = Records.getAllDerivedDefinitions(DeclNodeClassName); 2240b57cec5SDimitry Andric RecordSet DeclContexts (DeclContextsVector.begin(), DeclContextsVector.end()); 2250b57cec5SDimitry Andric 2260b57cec5SDimitry Andric for (RecordVector::iterator i = Decls.begin(), e = Decls.end(); i != e; ++i) { 2270b57cec5SDimitry Andric Record *R = *i; 2280b57cec5SDimitry Andric 229480093f4SDimitry Andric if (Record *B = R->getValueAsOptionalDef(BaseFieldName)) { 2300b57cec5SDimitry Andric if (DeclContexts.find(B) != DeclContexts.end()) { 2310b57cec5SDimitry Andric OS << "DECL_CONTEXT_BASE(" << B->getName() << ")\n"; 2320b57cec5SDimitry Andric DeclContexts.erase(B); 2330b57cec5SDimitry Andric } 2340b57cec5SDimitry Andric } 2350b57cec5SDimitry Andric } 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric // To keep identical order, RecordVector may be used 2380b57cec5SDimitry Andric // instead of RecordSet. 2390b57cec5SDimitry Andric for (RecordVector::iterator 2400b57cec5SDimitry Andric i = DeclContextsVector.begin(), e = DeclContextsVector.end(); 2410b57cec5SDimitry Andric i != e; ++i) 2420b57cec5SDimitry Andric if (DeclContexts.find(*i) != DeclContexts.end()) 2430b57cec5SDimitry Andric OS << "DECL_CONTEXT(" << (*i)->getName() << ")\n"; 2440b57cec5SDimitry Andric 2450b57cec5SDimitry Andric OS << "#undef DECL_CONTEXT\n"; 2460b57cec5SDimitry Andric OS << "#undef DECL_CONTEXT_BASE\n"; 2470b57cec5SDimitry Andric } 248