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,
42 std::string BaseName = macroName(Base->getName());
44 ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
46 Record *First = 0, *Last = 0;
47 // This might be the pseudo-node for Stmt; don't assume it has an Abstract
49 if (Base->getValue("Abstract") && !Base->getValueAsBit("Abstract"))
53 Record *R = i->second;
54 bool Abstract = R->getValueAsBit("Abstract");
55 std::string NodeName = macroName(R->getName());
57 OS << "#ifndef " << NodeName << "\n";
58 OS << "# define " << NodeName << "(Type, Base) "
59 << BaseName << "(Type, Base)\n";
63 OS << "ABSTRACT_STMT(" << NodeName << "(" << R->getName() << ", "
64 << Base->getName() << "))\n";
66 OS << NodeName << "(" << R->getName() << ", "
67 << Base->getName() << ")\n";
69 if (Tree.find(R) != Tree.end()) {
70 const std::pair<Record *, Record *> &Result
71 = EmitStmtNode(Tree, OS, R, false);
72 if (!First && Result.first)
85 OS << "#undef " << NodeName << "\n\n";
89 assert (Last && "Got a first node but not a last node for a range!");
91 OS << "LAST_STMT_RANGE(";
95 OS << Base->getName() << ", " << First->getName() << ", "
96 << Last->getName() << ")\n\n";
99 return std::make_pair(First, Last);
102 void ClangStmtNodesEmitter::run(raw_ostream &OS) {
103 // Write the preamble
104 OS << "#ifndef ABSTRACT_STMT\n";
105 OS << "# define ABSTRACT_STMT(Stmt) Stmt\n";
108 OS << "#ifndef STMT_RANGE\n";
109 OS << "# define STMT_RANGE(Base, First, Last)\n";
112 OS << "#ifndef LAST_STMT_RANGE\n";
113 OS << "# define LAST_STMT_RANGE(Base, First, Last) "
114 "STMT_RANGE(Base, First, Last)\n";
118 const std::vector<Record*> Stmts = Records.getAllDerivedDefinitions("Stmt");
122 // Create a pseudo-record to serve as the Stmt node, which isn't actually
124 Record Stmt ("Stmt", SMLoc());
126 for (unsigned i = 0, e = Stmts.size(); i != e; ++i) {
127 Record *R = Stmts[i];
129 if (R->getValue("Base"))
130 Tree.insert(std::make_pair(R->getValueAsDef("Base"), R));
132 Tree.insert(std::make_pair(&Stmt, R));
135 EmitStmtNode(Tree, OS, &Stmt);
137 OS << "#undef STMT\n";
138 OS << "#undef STMT_RANGE\n";
139 OS << "#undef LAST_STMT_RANGE\n";
140 OS << "#undef ABSTRACT_STMT\n";