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