1 #include "ClangASTNodesEmitter.h"
11 //===----------------------------------------------------------------------===//
12 // Statement Node Tables (.inc file) generation.
13 //===----------------------------------------------------------------------===//
15 // Sort to create a tree-like structure based on the 'Base' field.
17 // Create a macro-ized version of a name
18 std::string macroName(std::string S) {
19 for (unsigned i = 0; i < S.size(); ++i)
20 S[i] = std::toupper(S[i]);
25 // A map from a node to each of its derived nodes.
26 typedef std::multimap<Record*, Record*> ChildMap;
27 typedef ChildMap::const_iterator ChildIterator;
29 // Returns the first and last non-abstract subrecords
30 std::pair<Record *, Record *> EmitStmtNode(const ChildMap &Tree,
31 raw_ostream &OS, Record *Base) {
32 std::string BaseName = macroName(Base->getName());
34 ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
36 Record *First = 0, *Last = 0;
37 // This might be the pseudo-node for Stmt; don't assume it has an Abstract
39 if (Base->getValue("Abstract") && !Base->getValueAsBit("Abstract"))
43 Record *R = i->second;
44 bool Abstract = R->getValueAsBit("Abstract");
45 std::string NodeName = macroName(R->getName());
47 OS << "#ifndef " << NodeName << "\n";
48 OS << "# define " << NodeName << "(Type, Base) "
49 << BaseName << "(Type, Base)\n";
53 OS << "ABSTRACT(" << NodeName << "(" << R->getName() << ", "
54 << Base->getName() << "))\n";
56 OS << NodeName << "(" << R->getName() << ", "
57 << Base->getName() << ")\n";
59 if (Tree.find(R) != Tree.end()) {
60 const std::pair<Record *, Record *> &Result = EmitStmtNode(Tree, OS, R);
61 if (!First && Result.first)
74 OS << "#undef " << NodeName << "\n\n";
77 assert(!First == !Last && "Got a first or last node, but not the other");
80 OS << "#ifndef FIRST_" << BaseName << "\n";
81 OS << "# define FIRST_" << BaseName << "(CLASS)\n";
83 OS << "#ifndef LAST_" << BaseName << "\n";
84 OS << "# define LAST_" << BaseName << "(CLASS)\n";
87 OS << "FIRST_" << BaseName << "(" << First->getName() << ")\n";
88 OS << "LAST_" << BaseName << "(" << Last->getName() << ")\n\n";
91 OS << "#undef FIRST_" << BaseName << "\n";
92 OS << "#undef LAST_" << BaseName << "\n\n";
94 return std::make_pair(First, Last);
98 void ClangStmtNodesEmitter::run(raw_ostream &OS) {
100 OS << "#ifndef ABSTRACT\n";
101 OS << "# define ABSTRACT(Stmt) Stmt\n";
105 const std::vector<Record*> Stmts = Records.getAllDerivedDefinitions("Stmt");
109 // Create a pseudo-record to serve as the Stmt node, which isn't actually
111 Record Stmt ("Stmt", SMLoc());
113 for (unsigned i = 0, e = Stmts.size(); i != e; ++i) {
114 Record *R = Stmts[i];
116 if (R->getValue("Base"))
117 Tree.insert(std::make_pair(R->getValueAsDef("Base"), R));
119 Tree.insert(std::make_pair(&Stmt, R));
122 EmitStmtNode(Tree, OS, &Stmt);
124 OS << "#undef STMT\n";
125 OS << "#undef ABSTRACT\n";