1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "llvm/TableGen/Record.h" |
14 | #include "llvm/TableGen/TableGenBackend.h" |
15 | #include <cctype> |
16 | #include <map> |
17 | #include <set> |
18 | #include <string> |
19 | using namespace llvm; |
20 | |
21 | |
22 | |
23 | |
24 | namespace { |
25 | class ClangASTNodesEmitter { |
26 | |
27 | typedef std::multimap<Record*, Record*> ChildMap; |
28 | typedef ChildMap::const_iterator ChildIterator; |
29 | |
30 | RecordKeeper &Records; |
31 | Record Root; |
32 | const std::string &BaseSuffix; |
33 | |
34 | |
35 | static std::string macroName(std::string S) { |
36 | for (unsigned i = 0; i < S.size(); ++i) |
37 | S[i] = std::toupper(S[i]); |
38 | |
39 | return S; |
40 | } |
41 | |
42 | |
43 | |
44 | |
45 | std::string baseName(Record &R) { |
46 | if (&R == &Root && !BaseSuffix.empty()) |
47 | return BaseSuffix; |
48 | |
49 | return R.getName().str() + BaseSuffix; |
50 | } |
51 | |
52 | std::pair<Record *, Record *> EmitNode (const ChildMap &Tree, raw_ostream& OS, |
53 | Record *Base); |
54 | public: |
55 | explicit ClangASTNodesEmitter(RecordKeeper &R, const std::string &N, |
56 | const std::string &S) |
57 | : Records(R), Root(N, SMLoc(), R), BaseSuffix(S) |
58 | {} |
59 | |
60 | |
61 | void run(raw_ostream &OS); |
62 | }; |
63 | } |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | |
70 | |
71 | std::pair<Record *, Record *> ClangASTNodesEmitter::EmitNode( |
72 | const ChildMap &Tree, |
73 | raw_ostream &OS, |
74 | Record *Base) { |
75 | std::string BaseName = macroName(Base->getName()); |
76 | |
77 | ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base); |
78 | |
79 | Record *First = nullptr, *Last = nullptr; |
80 | |
81 | |
82 | if (Base->getValue("Abstract") && !Base->getValueAsBit("Abstract")) |
83 | First = Last = Base; |
84 | |
85 | for (; i != e; ++i) { |
86 | Record *R = i->second; |
87 | bool Abstract = R->getValueAsBit("Abstract"); |
88 | std::string NodeName = macroName(R->getName()); |
89 | |
90 | OS << "#ifndef " << NodeName << "\n"; |
91 | OS << "# define " << NodeName << "(Type, Base) " |
92 | << BaseName << "(Type, Base)\n"; |
93 | OS << "#endif\n"; |
94 | |
95 | if (Abstract) |
96 | OS << "ABSTRACT_" << macroName(Root.getName()) << "(" << NodeName << "(" |
97 | << R->getName() << ", " << baseName(*Base) << "))\n"; |
98 | else |
99 | OS << NodeName << "(" << R->getName() << ", " |
100 | << baseName(*Base) << ")\n"; |
101 | |
102 | if (Tree.find(R) != Tree.end()) { |
103 | const std::pair<Record *, Record *> &Result |
104 | = EmitNode(Tree, OS, R); |
105 | if (!First && Result.first) |
106 | First = Result.first; |
107 | if (Result.second) |
108 | Last = Result.second; |
109 | } else { |
110 | if (!Abstract) { |
111 | Last = R; |
112 | |
113 | if (!First) |
114 | First = R; |
115 | } |
116 | } |
117 | |
118 | OS << "#undef " << NodeName << "\n\n"; |
119 | } |
120 | |
121 | if (First) { |
122 | assert (Last && "Got a first node but not a last node for a range!"); |
123 | if (Base == &Root) |
124 | OS << "LAST_" << macroName(Root.getName()) << "_RANGE("; |
125 | else |
126 | OS << macroName(Root.getName()) << "_RANGE("; |
127 | OS << Base->getName() << ", " << First->getName() << ", " |
128 | << Last->getName() << ")\n\n"; |
129 | } |
130 | |
131 | return std::make_pair(First, Last); |
132 | } |
133 | |
134 | void ClangASTNodesEmitter::run(raw_ostream &OS) { |
135 | emitSourceFileHeader("List of AST nodes of a particular kind", OS); |
136 | |
137 | |
138 | OS << "#ifndef ABSTRACT_" << macroName(Root.getName()) << "\n"; |
139 | OS << "# define ABSTRACT_" << macroName(Root.getName()) << "(Type) Type\n"; |
140 | OS << "#endif\n"; |
141 | |
142 | OS << "#ifndef " << macroName(Root.getName()) << "_RANGE\n"; |
143 | OS << "# define " |
144 | << macroName(Root.getName()) << "_RANGE(Base, First, Last)\n"; |
145 | OS << "#endif\n\n"; |
146 | |
147 | OS << "#ifndef LAST_" << macroName(Root.getName()) << "_RANGE\n"; |
148 | OS << "# define LAST_" |
149 | << macroName(Root.getName()) << "_RANGE(Base, First, Last) " |
150 | << macroName(Root.getName()) << "_RANGE(Base, First, Last)\n"; |
151 | OS << "#endif\n\n"; |
152 | |
153 | |
154 | const std::vector<Record*> Stmts |
155 | = Records.getAllDerivedDefinitions(Root.getName()); |
156 | |
157 | ChildMap Tree; |
158 | |
159 | for (unsigned i = 0, e = Stmts.size(); i != e; ++i) { |
160 | Record *R = Stmts[i]; |
161 | |
162 | if (R->getValue("Base")) |
163 | Tree.insert(std::make_pair(R->getValueAsDef("Base"), R)); |
164 | else |
165 | Tree.insert(std::make_pair(&Root, R)); |
166 | } |
167 | |
168 | EmitNode(Tree, OS, &Root); |
169 | |
170 | OS << "#undef " << macroName(Root.getName()) << "\n"; |
171 | OS << "#undef " << macroName(Root.getName()) << "_RANGE\n"; |
172 | OS << "#undef LAST_" << macroName(Root.getName()) << "_RANGE\n"; |
173 | OS << "#undef ABSTRACT_" << macroName(Root.getName()) << "\n"; |
174 | } |
175 | |
176 | namespace clang { |
177 | void EmitClangASTNodes(RecordKeeper &RK, raw_ostream &OS, |
178 | const std::string &N, const std::string &S) { |
179 | ClangASTNodesEmitter(RK, N, S).run(OS); |
180 | } |
181 | |
182 | |
183 | |
184 | void EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) { |
185 | |
186 | |
187 | emitSourceFileHeader("List of AST Decl nodes", OS); |
188 | |
189 | OS << "#ifndef DECL_CONTEXT\n"; |
190 | OS << "# define DECL_CONTEXT(DECL)\n"; |
191 | OS << "#endif\n"; |
192 | |
193 | OS << "#ifndef DECL_CONTEXT_BASE\n"; |
194 | OS << "# define DECL_CONTEXT_BASE(DECL) DECL_CONTEXT(DECL)\n"; |
195 | OS << "#endif\n"; |
196 | |
197 | typedef std::set<Record*> RecordSet; |
198 | typedef std::vector<Record*> RecordVector; |
199 | |
200 | RecordVector DeclContextsVector |
201 | = Records.getAllDerivedDefinitions("DeclContext"); |
202 | RecordVector Decls = Records.getAllDerivedDefinitions("Decl"); |
203 | RecordSet DeclContexts (DeclContextsVector.begin(), DeclContextsVector.end()); |
204 | |
205 | for (RecordVector::iterator i = Decls.begin(), e = Decls.end(); i != e; ++i) { |
206 | Record *R = *i; |
207 | |
208 | if (R->getValue("Base")) { |
209 | Record *B = R->getValueAsDef("Base"); |
210 | if (DeclContexts.find(B) != DeclContexts.end()) { |
211 | OS << "DECL_CONTEXT_BASE(" << B->getName() << ")\n"; |
212 | DeclContexts.erase(B); |
213 | } |
214 | } |
215 | } |
216 | |
217 | |
218 | |
219 | for (RecordVector::iterator |
220 | i = DeclContextsVector.begin(), e = DeclContextsVector.end(); |
221 | i != e; ++i) |
222 | if (DeclContexts.find(*i) != DeclContexts.end()) |
223 | OS << "DECL_CONTEXT(" << (*i)->getName() << ")\n"; |
224 | |
225 | OS << "#undef DECL_CONTEXT\n"; |
226 | OS << "#undef DECL_CONTEXT_BASE\n"; |
227 | } |
228 | } |
229 | |