Do more to modernize MergeFunctions. Refactor in response to Chris' code review.
[llvm.git] / utils / TableGen / ClangASTNodesEmitter.cpp
blob187ab4679994edea88cb26293d9018b05cca87d3
1 //=== ClangASTNodesEmitter.cpp - Generate Clang AST node tables -*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // These tablegen backends emit Clang AST node tables
12 //===----------------------------------------------------------------------===//
14 #include "ClangASTNodesEmitter.h"
15 #include <set>
16 using namespace llvm;
18 //===----------------------------------------------------------------------===//
19 // Statement Node Tables (.inc file) generation.
20 //===----------------------------------------------------------------------===//
22 // Returns the first and last non-abstract subrecords
23 // Called recursively to ensure that nodes remain contiguous
24 std::pair<Record *, Record *> ClangASTNodesEmitter::EmitNode(
25 const ChildMap &Tree,
26 raw_ostream &OS,
27 Record *Base) {
28 std::string BaseName = macroName(Base->getName());
30 ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
32 Record *First = 0, *Last = 0;
33 // This might be the pseudo-node for Stmt; don't assume it has an Abstract
34 // bit
35 if (Base->getValue("Abstract") && !Base->getValueAsBit("Abstract"))
36 First = Last = Base;
38 for (; i != e; ++i) {
39 Record *R = i->second;
40 bool Abstract = R->getValueAsBit("Abstract");
41 std::string NodeName = macroName(R->getName());
43 OS << "#ifndef " << NodeName << "\n";
44 OS << "# define " << NodeName << "(Type, Base) "
45 << BaseName << "(Type, Base)\n";
46 OS << "#endif\n";
48 if (Abstract)
49 OS << "ABSTRACT_" << macroName(Root.getName()) << "(" << NodeName << "("
50 << R->getName() << ", " << baseName(*Base) << "))\n";
51 else
52 OS << NodeName << "(" << R->getName() << ", "
53 << baseName(*Base) << ")\n";
55 if (Tree.find(R) != Tree.end()) {
56 const std::pair<Record *, Record *> &Result
57 = EmitNode(Tree, OS, R);
58 if (!First && Result.first)
59 First = Result.first;
60 if (Result.second)
61 Last = Result.second;
62 } else {
63 if (!Abstract) {
64 Last = R;
66 if (!First)
67 First = R;
71 OS << "#undef " << NodeName << "\n\n";
74 if (First) {
75 assert (Last && "Got a first node but not a last node for a range!");
76 if (Base == &Root)
77 OS << "LAST_" << macroName(Root.getName()) << "_RANGE(";
78 else
79 OS << macroName(Root.getName()) << "_RANGE(";
80 OS << Base->getName() << ", " << First->getName() << ", "
81 << Last->getName() << ")\n\n";
84 return std::make_pair(First, Last);
87 void ClangASTNodesEmitter::run(raw_ostream &OS) {
88 // Write the preamble
89 OS << "#ifndef ABSTRACT_" << macroName(Root.getName()) << "\n";
90 OS << "# define ABSTRACT_" << macroName(Root.getName()) << "(Type) Type\n";
91 OS << "#endif\n";
93 OS << "#ifndef " << macroName(Root.getName()) << "_RANGE\n";
94 OS << "# define "
95 << macroName(Root.getName()) << "_RANGE(Base, First, Last)\n";
96 OS << "#endif\n\n";
98 OS << "#ifndef LAST_" << macroName(Root.getName()) << "_RANGE\n";
99 OS << "# define LAST_"
100 << macroName(Root.getName()) << "_RANGE(Base, First, Last) "
101 << macroName(Root.getName()) << "_RANGE(Base, First, Last)\n";
102 OS << "#endif\n\n";
104 // Emit statements
105 const std::vector<Record*> Stmts
106 = Records.getAllDerivedDefinitions(Root.getName());
108 ChildMap Tree;
110 for (unsigned i = 0, e = Stmts.size(); i != e; ++i) {
111 Record *R = Stmts[i];
113 if (R->getValue("Base"))
114 Tree.insert(std::make_pair(R->getValueAsDef("Base"), R));
115 else
116 Tree.insert(std::make_pair(&Root, R));
119 EmitNode(Tree, OS, &Root);
121 OS << "#undef " << macroName(Root.getName()) << "\n";
122 OS << "#undef " << macroName(Root.getName()) << "_RANGE\n";
123 OS << "#undef LAST_" << macroName(Root.getName()) << "_RANGE\n";
124 OS << "#undef ABSTRACT_" << macroName(Root.getName()) << "\n";
127 void ClangDeclContextEmitter::run(raw_ostream &OS) {
128 // FIXME: Find a .td file format to allow for this to be represented better.
130 OS << "#ifndef DECL_CONTEXT\n";
131 OS << "# define DECL_CONTEXT(DECL)\n";
132 OS << "#endif\n";
134 OS << "#ifndef DECL_CONTEXT_BASE\n";
135 OS << "# define DECL_CONTEXT_BASE(DECL) DECL_CONTEXT(DECL)\n";
136 OS << "#endif\n";
138 typedef std::set<Record*> RecordSet;
139 typedef std::vector<Record*> RecordVector;
141 RecordVector DeclContextsVector
142 = Records.getAllDerivedDefinitions("DeclContext");
143 RecordVector Decls = Records.getAllDerivedDefinitions("Decl");
144 RecordSet DeclContexts (DeclContextsVector.begin(), DeclContextsVector.end());
146 for (RecordVector::iterator i = Decls.begin(), e = Decls.end(); i != e; ++i) {
147 Record *R = *i;
149 if (R->getValue("Base")) {
150 Record *B = R->getValueAsDef("Base");
151 if (DeclContexts.find(B) != DeclContexts.end()) {
152 OS << "DECL_CONTEXT_BASE(" << B->getName() << ")\n";
153 DeclContexts.erase(B);
158 for (RecordSet::iterator i = DeclContexts.begin(), e = DeclContexts.end();
159 i != e; ++i) {
160 OS << "DECL_CONTEXT(" << (*i)->getName() << ")\n";
163 OS << "#undef DECL_CONTEXT\n";
164 OS << "#undef DECL_CONTEXT_BASE\n";