blob: 187ab4679994edea88cb26293d9018b05cca87d3 [file] [log] [blame]
Sean Hunt44ed2c32010-05-06 05:24:38 +00001//=== 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
11//
12//===----------------------------------------------------------------------===//
13
Sean Hunt84e2f952010-05-05 04:13:08 +000014#include "ClangASTNodesEmitter.h"
Sean Huntc10a62b2010-05-30 07:21:42 +000015#include <set>
Sean Hunt84e2f952010-05-05 04:13:08 +000016using namespace llvm;
17
18//===----------------------------------------------------------------------===//
19// Statement Node Tables (.inc file) generation.
20//===----------------------------------------------------------------------===//
21
Sean Hunt44ed2c32010-05-06 05:24:38 +000022// Returns the first and last non-abstract subrecords
23// Called recursively to ensure that nodes remain contiguous
Sean Huntc10a62b2010-05-30 07:21:42 +000024std::pair<Record *, Record *> ClangASTNodesEmitter::EmitNode(
25 const ChildMap &Tree,
26 raw_ostream &OS,
27 Record *Base) {
Sean Hunt44ed2c32010-05-06 05:24:38 +000028 std::string BaseName = macroName(Base->getName());
Sean Hunt84e2f952010-05-05 04:13:08 +000029
Sean Hunt44ed2c32010-05-06 05:24:38 +000030 ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
Sean Hunt84e2f952010-05-05 04:13:08 +000031
Sean Hunt44ed2c32010-05-06 05:24:38 +000032 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;
Sean Hunt84e2f952010-05-05 04:13:08 +000037
Sean Hunt44ed2c32010-05-06 05:24:38 +000038 for (; i != e; ++i) {
39 Record *R = i->second;
40 bool Abstract = R->getValueAsBit("Abstract");
41 std::string NodeName = macroName(R->getName());
Sean Hunt84e2f952010-05-05 04:13:08 +000042
Sean Hunt44ed2c32010-05-06 05:24:38 +000043 OS << "#ifndef " << NodeName << "\n";
44 OS << "# define " << NodeName << "(Type, Base) "
45 << BaseName << "(Type, Base)\n";
46 OS << "#endif\n";
Sean Hunt84e2f952010-05-05 04:13:08 +000047
Sean Hunt44ed2c32010-05-06 05:24:38 +000048 if (Abstract)
Sean Huntc10a62b2010-05-30 07:21:42 +000049 OS << "ABSTRACT_" << macroName(Root.getName()) << "(" << NodeName << "("
50 << R->getName() << ", " << baseName(*Base) << "))\n";
Sean Hunt44ed2c32010-05-06 05:24:38 +000051 else
52 OS << NodeName << "(" << R->getName() << ", "
Sean Huntc10a62b2010-05-30 07:21:42 +000053 << baseName(*Base) << ")\n";
Sean Hunt84e2f952010-05-05 04:13:08 +000054
Sean Hunt44ed2c32010-05-06 05:24:38 +000055 if (Tree.find(R) != Tree.end()) {
Sean Hunt85319752010-05-18 06:22:50 +000056 const std::pair<Record *, Record *> &Result
Sean Huntc10a62b2010-05-30 07:21:42 +000057 = EmitNode(Tree, OS, R);
Sean Hunt44ed2c32010-05-06 05:24:38 +000058 if (!First && Result.first)
59 First = Result.first;
60 if (Result.second)
61 Last = Result.second;
62 } else {
63 if (!Abstract) {
64 Last = R;
Sean Hunt84e2f952010-05-05 04:13:08 +000065
Sean Hunt44ed2c32010-05-06 05:24:38 +000066 if (!First)
67 First = R;
Sean Hunt84e2f952010-05-05 04:13:08 +000068 }
Sean Hunt84e2f952010-05-05 04:13:08 +000069 }
70
Sean Hunt44ed2c32010-05-06 05:24:38 +000071 OS << "#undef " << NodeName << "\n\n";
Sean Hunt84e2f952010-05-05 04:13:08 +000072 }
Sean Hunt44ed2c32010-05-06 05:24:38 +000073
Sean Hunt44ed2c32010-05-06 05:24:38 +000074 if (First) {
Sean Hunt85319752010-05-18 06:22:50 +000075 assert (Last && "Got a first node but not a last node for a range!");
Sean Huntc10a62b2010-05-30 07:21:42 +000076 if (Base == &Root)
77 OS << "LAST_" << macroName(Root.getName()) << "_RANGE(";
Sean Hunt85319752010-05-18 06:22:50 +000078 else
Sean Huntc10a62b2010-05-30 07:21:42 +000079 OS << macroName(Root.getName()) << "_RANGE(";
Sean Hunt85319752010-05-18 06:22:50 +000080 OS << Base->getName() << ", " << First->getName() << ", "
81 << Last->getName() << ")\n\n";
Sean Hunt44ed2c32010-05-06 05:24:38 +000082 }
83
Sean Hunt44ed2c32010-05-06 05:24:38 +000084 return std::make_pair(First, Last);
Sean Hunt84e2f952010-05-05 04:13:08 +000085}
86
Sean Huntc10a62b2010-05-30 07:21:42 +000087void ClangASTNodesEmitter::run(raw_ostream &OS) {
Sean Hunt84e2f952010-05-05 04:13:08 +000088 // Write the preamble
Sean Huntc10a62b2010-05-30 07:21:42 +000089 OS << "#ifndef ABSTRACT_" << macroName(Root.getName()) << "\n";
90 OS << "# define ABSTRACT_" << macroName(Root.getName()) << "(Type) Type\n";
Sean Hunt85319752010-05-18 06:22:50 +000091 OS << "#endif\n";
92
Sean Huntc10a62b2010-05-30 07:21:42 +000093 OS << "#ifndef " << macroName(Root.getName()) << "_RANGE\n";
94 OS << "# define "
95 << macroName(Root.getName()) << "_RANGE(Base, First, Last)\n";
Sean Hunt84e2f952010-05-05 04:13:08 +000096 OS << "#endif\n\n";
97
Sean Huntc10a62b2010-05-30 07:21:42 +000098 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";
Sean Hunt85319752010-05-18 06:22:50 +0000102 OS << "#endif\n\n";
103
Sean Hunt84e2f952010-05-05 04:13:08 +0000104 // Emit statements
Sean Huntc10a62b2010-05-30 07:21:42 +0000105 const std::vector<Record*> Stmts
106 = Records.getAllDerivedDefinitions(Root.getName());
Sean Hunt84e2f952010-05-05 04:13:08 +0000107
108 ChildMap Tree;
109
Sean Hunt84e2f952010-05-05 04:13:08 +0000110 for (unsigned i = 0, e = Stmts.size(); i != e; ++i) {
111 Record *R = Stmts[i];
112
113 if (R->getValue("Base"))
114 Tree.insert(std::make_pair(R->getValueAsDef("Base"), R));
115 else
Sean Huntc10a62b2010-05-30 07:21:42 +0000116 Tree.insert(std::make_pair(&Root, R));
Sean Hunt84e2f952010-05-05 04:13:08 +0000117 }
118
Sean Huntc10a62b2010-05-30 07:21:42 +0000119 EmitNode(Tree, OS, &Root);
Sean Hunt84e2f952010-05-05 04:13:08 +0000120
Sean Huntc10a62b2010-05-30 07:21:42 +0000121 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";
125}
126
127void ClangDeclContextEmitter::run(raw_ostream &OS) {
128 // FIXME: Find a .td file format to allow for this to be represented better.
129
130 OS << "#ifndef DECL_CONTEXT\n";
131 OS << "# define DECL_CONTEXT(DECL)\n";
132 OS << "#endif\n";
133
134 OS << "#ifndef DECL_CONTEXT_BASE\n";
135 OS << "# define DECL_CONTEXT_BASE(DECL) DECL_CONTEXT(DECL)\n";
136 OS << "#endif\n";
137
138 typedef std::set<Record*> RecordSet;
139 typedef std::vector<Record*> RecordVector;
140
141 RecordVector DeclContextsVector
142 = Records.getAllDerivedDefinitions("DeclContext");
143 RecordVector Decls = Records.getAllDerivedDefinitions("Decl");
144 RecordSet DeclContexts (DeclContextsVector.begin(), DeclContextsVector.end());
145
146 for (RecordVector::iterator i = Decls.begin(), e = Decls.end(); i != e; ++i) {
147 Record *R = *i;
148
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);
154 }
155 }
156 }
157
158 for (RecordSet::iterator i = DeclContexts.begin(), e = DeclContexts.end();
159 i != e; ++i) {
160 OS << "DECL_CONTEXT(" << (*i)->getName() << ")\n";
161 }
162
163 OS << "#undef DECL_CONTEXT\n";
164 OS << "#undef DECL_CONTEXT_BASE\n";
Shantonu Send1dd5ed2010-05-05 13:56:46 +0000165}