1 //=== ClangASTNodesEmitter.cpp - Generate Clang AST node tables -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // These tablegen backends emit Clang AST node tables
12 //===----------------------------------------------------------------------===//
14 #include "ClangASTNodesEmitter.h"
20 //===----------------------------------------------------------------------===//
21 // Statement Node Tables (.inc file) generation.
22 //===----------------------------------------------------------------------===//
24 // Create a macro-ized version of a name
25 static std::string macroName(std::string S) {
26 for (unsigned i = 0; i < S.size(); ++i)
27 S[i] = std::toupper(S[i]);
32 // A map from a node to each of its derived nodes.
33 typedef std::multimap<Record*, Record*> ChildMap;
34 typedef ChildMap::const_iterator ChildIterator;
36 // Returns the first and last non-abstract subrecords
37 // Called recursively to ensure that nodes remain contiguous
38 static std::pair<Record *, Record *> EmitStmtNode(const ChildMap &Tree,
41 std::string BaseName = macroName(Base->getName());
43 ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
45 Record *First = 0, *Last = 0;
46 // This might be the pseudo-node for Stmt; don't assume it has an Abstract
48 if (Base->getValue("Abstract") && !Base->getValueAsBit("Abstract"))
52 Record *R = i->second;
53 bool Abstract = R->getValueAsBit("Abstract");
54 std::string NodeName = macroName(R->getName());
56 OS << "#ifndef " << NodeName << "\n";
57 OS << "# define " << NodeName << "(Type, Base) "
58 << BaseName << "(Type, Base)\n";
62 OS << "ABSTRACT(" << NodeName << "(" << R->getName() << ", "
63 << Base->getName() << "))\n";
65 OS << NodeName << "(" << R->getName() << ", "
66 << Base->getName() << ")\n";
68 if (Tree.find(R) != Tree.end()) {
69 const std::pair<Record *, Record *> &Result = EmitStmtNode(Tree, OS, R);
70 if (!First && Result.first)
83 OS << "#undef " << NodeName << "\n\n";
86 assert(!First == !Last && "Got a first or last node, but not the other");
89 OS << "#ifndef FIRST_" << BaseName << "\n";
90 OS << "# define FIRST_" << BaseName << "(CLASS)\n";
92 OS << "#ifndef LAST_" << BaseName << "\n";
93 OS << "# define LAST_" << BaseName << "(CLASS)\n";
96 OS << "FIRST_" << BaseName << "(" << First->getName() << ")\n";
97 OS << "LAST_" << BaseName << "(" << Last->getName() << ")\n\n";
100 OS << "#undef FIRST_" << BaseName << "\n";
101 OS << "#undef LAST_" << BaseName << "\n\n";
103 return std::make_pair(First, Last);
106 void ClangStmtNodesEmitter::run(raw_ostream &OS) {
107 // Write the preamble
108 OS << "#ifndef ABSTRACT\n";
109 OS << "# define ABSTRACT(Stmt) Stmt\n";
113 const std::vector<Record*> Stmts = Records.getAllDerivedDefinitions("Stmt");
117 // Create a pseudo-record to serve as the Stmt node, which isn't actually
119 Record Stmt ("Stmt", SMLoc());
121 for (unsigned i = 0, e = Stmts.size(); i != e; ++i) {
122 Record *R = Stmts[i];
124 if (R->getValue("Base"))
125 Tree.insert(std::make_pair(R->getValueAsDef("Base"), R));
127 Tree.insert(std::make_pair(&Stmt, R));
130 EmitStmtNode(Tree, OS, &Stmt);
132 OS << "#undef STMT\n";
133 OS << "#undef ABSTRACT\n";