blob: ab74598e6dba790c4a7149f7683a13db6e7aea41 [file] [log] [blame]
Sean Hunt16171442010-06-16 23:45:50 +00001//===- ClangAttrEmitter.cpp - Generate Clang attribute handling =-*- 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 attribute processing code
11//
12//===----------------------------------------------------------------------===//
13
14#include "ClangAttrEmitter.h"
15#include "Record.h"
Sean Hunt726a3d22010-08-18 23:23:09 +000016#include "llvm/ADT/StringSwitch.h"
Sean Hunt16171442010-06-16 23:45:50 +000017#include <algorithm>
Sean Hunt5c5f4662010-08-19 00:19:03 +000018#include <cctype>
Sean Hunt16171442010-06-16 23:45:50 +000019
20using namespace llvm;
21
Jim Grosbachbb168242010-10-08 18:13:57 +000022static const std::vector<StringRef>
23getValueAsListOfStrings(Record &R, StringRef FieldName) {
David Greenef37dd022011-07-29 19:07:05 +000024 const ListInit *List = R.getValueAsListInit(FieldName);
Sean Hunt726a3d22010-08-18 23:23:09 +000025 assert (List && "Got a null ListInit");
26
27 std::vector<StringRef> Strings;
28 Strings.reserve(List->getSize());
29
David Greene60c04af2011-07-29 19:07:02 +000030 for (ListInit::const_iterator i = List->begin(), e = List->end();
31 i != e;
32 ++i) {
Sean Hunt726a3d22010-08-18 23:23:09 +000033 assert(*i && "Got a null element in a ListInit");
David Greenef37dd022011-07-29 19:07:05 +000034 if (const StringInit *S = dynamic_cast<const StringInit *>(*i))
Sean Hunt726a3d22010-08-18 23:23:09 +000035 Strings.push_back(S->getValue());
David Greenef37dd022011-07-29 19:07:05 +000036 else if (const CodeInit *C = dynamic_cast<const CodeInit *>(*i))
Sean Hunt726a3d22010-08-18 23:23:09 +000037 Strings.push_back(C->getValue());
38 else
39 assert(false && "Got a non-string, non-code element in a ListInit");
40 }
41
42 return Strings;
43}
44
45std::string ReadPCHRecord(StringRef type) {
46 return StringSwitch<std::string>(type)
Douglas Gregor6bd48422011-07-28 20:55:16 +000047 .EndsWith("Decl *", "GetLocalDeclAs<"
48 + std::string(type, 0, type.size()-1) + ">(F, Record[Idx++])")
49 .Case("QualType", "getLocalType(F, Record[Idx++])")
Sean Huntc85094f2011-02-17 03:30:09 +000050 .Case("Expr *", "ReadSubExpr()")
Douglas Gregor6bd48422011-07-28 20:55:16 +000051 .Case("IdentifierInfo *", "GetIdentifierInfo(F, Record, Idx)")
Sean Hunt726a3d22010-08-18 23:23:09 +000052 .Default("Record[Idx++]");
53}
54
55// Assumes that the way to get the value is SA->getname()
56std::string WritePCHRecord(StringRef type, StringRef name) {
57 return StringSwitch<std::string>(type)
58 .EndsWith("Decl *", "AddDeclRef(" + std::string(name) +
59 ", Record);\n")
60 .Case("QualType", "AddTypeRef(" + std::string(name) + ", Record);\n")
Sean Huntc85094f2011-02-17 03:30:09 +000061 .Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
Douglas Gregor095a3f32011-03-23 01:05:46 +000062 .Case("IdentifierInfo *",
63 "AddIdentifierRef(" + std::string(name) + ", Record);\n")
Sean Hunt726a3d22010-08-18 23:23:09 +000064 .Default("Record.push_back(" + std::string(name) + ");\n");
65}
66
67namespace {
68 class Argument {
69 std::string lowerName, upperName;
70 StringRef attrName;
71
72 public:
73 Argument(Record &Arg, StringRef Attr)
74 : lowerName(Arg.getValueAsString("Name")), upperName(lowerName),
75 attrName(Attr) {
76 if (!lowerName.empty()) {
77 lowerName[0] = std::tolower(lowerName[0]);
78 upperName[0] = std::toupper(upperName[0]);
79 }
80 }
Chandler Carruth54f61632010-08-23 08:25:07 +000081 virtual ~Argument() {}
Sean Hunt726a3d22010-08-18 23:23:09 +000082
Sean Hunt5b385002010-08-19 00:03:05 +000083 StringRef getLowerName() const { return lowerName; }
84 StringRef getUpperName() const { return upperName; }
85 StringRef getAttrName() const { return attrName; }
Sean Hunt726a3d22010-08-18 23:23:09 +000086
87 // These functions print the argument contents formatted in different ways.
88 virtual void writeAccessors(raw_ostream &OS) const = 0;
89 virtual void writeAccessorDefinitions(raw_ostream &OS) const {}
90 virtual void writeCloneArgs(raw_ostream &OS) const = 0;
91 virtual void writeCtorBody(raw_ostream &OS) const {}
92 virtual void writeCtorInitializers(raw_ostream &OS) const = 0;
93 virtual void writeCtorParameters(raw_ostream &OS) const = 0;
94 virtual void writeDeclarations(raw_ostream &OS) const = 0;
95 virtual void writePCHReadArgs(raw_ostream &OS) const = 0;
96 virtual void writePCHReadDecls(raw_ostream &OS) const = 0;
97 virtual void writePCHWrite(raw_ostream &OS) const = 0;
98 };
99
100 class SimpleArgument : public Argument {
101 std::string type;
102
103 public:
104 SimpleArgument(Record &Arg, StringRef Attr, std::string T)
105 : Argument(Arg, Attr), type(T)
106 {}
107
Sean Hunt726a3d22010-08-18 23:23:09 +0000108 void writeAccessors(raw_ostream &OS) const {
109 OS << " " << type << " get" << getUpperName() << "() const {\n";
110 OS << " return " << getLowerName() << ";\n";
111 OS << " }";
112 }
113 void writeCloneArgs(raw_ostream &OS) const {
114 OS << getLowerName();
115 }
116 void writeCtorInitializers(raw_ostream &OS) const {
117 OS << getLowerName() << "(" << getUpperName() << ")";
118 }
119 void writeCtorParameters(raw_ostream &OS) const {
120 OS << type << " " << getUpperName();
121 }
122 void writeDeclarations(raw_ostream &OS) const {
123 OS << type << " " << getLowerName() << ";";
124 }
125 void writePCHReadDecls(raw_ostream &OS) const {
126 std::string read = ReadPCHRecord(type);
127 OS << " " << type << " " << getLowerName() << " = " << read << ";\n";
128 }
129 void writePCHReadArgs(raw_ostream &OS) const {
130 OS << getLowerName();
131 }
132 void writePCHWrite(raw_ostream &OS) const {
133 OS << " " << WritePCHRecord(type, "SA->get" +
134 std::string(getUpperName()) + "()");
135 }
136 };
137
138 class StringArgument : public Argument {
139 public:
140 StringArgument(Record &Arg, StringRef Attr)
141 : Argument(Arg, Attr)
142 {}
143
144 void writeAccessors(raw_ostream &OS) const {
145 OS << " llvm::StringRef get" << getUpperName() << "() const {\n";
146 OS << " return llvm::StringRef(" << getLowerName() << ", "
147 << getLowerName() << "Length);\n";
148 OS << " }\n";
149 OS << " unsigned get" << getUpperName() << "Length() const {\n";
150 OS << " return " << getLowerName() << "Length;\n";
151 OS << " }\n";
152 OS << " void set" << getUpperName()
153 << "(ASTContext &C, llvm::StringRef S) {\n";
154 OS << " " << getLowerName() << "Length = S.size();\n";
155 OS << " this->" << getLowerName() << " = new (C, 1) char ["
156 << getLowerName() << "Length];\n";
157 OS << " std::memcpy(this->" << getLowerName() << ", S.data(), "
158 << getLowerName() << "Length);\n";
159 OS << " }";
160 }
161 void writeCloneArgs(raw_ostream &OS) const {
162 OS << "get" << getUpperName() << "()";
163 }
164 void writeCtorBody(raw_ostream &OS) const {
165 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
166 << ".data(), " << getLowerName() << "Length);";
167 }
168 void writeCtorInitializers(raw_ostream &OS) const {
169 OS << getLowerName() << "Length(" << getUpperName() << ".size()),"
170 << getLowerName() << "(new (Ctx, 1) char[" << getLowerName()
171 << "Length])";
172 }
173 void writeCtorParameters(raw_ostream &OS) const {
174 OS << "llvm::StringRef " << getUpperName();
175 }
176 void writeDeclarations(raw_ostream &OS) const {
177 OS << "unsigned " << getLowerName() << "Length;\n";
178 OS << "char *" << getLowerName() << ";";
179 }
180 void writePCHReadDecls(raw_ostream &OS) const {
Jim Grosbachbb168242010-10-08 18:13:57 +0000181 OS << " std::string " << getLowerName()
182 << "= ReadString(Record, Idx);\n";
Sean Hunt726a3d22010-08-18 23:23:09 +0000183 }
184 void writePCHReadArgs(raw_ostream &OS) const {
185 OS << getLowerName();
186 }
187 void writePCHWrite(raw_ostream &OS) const {
188 OS << " AddString(SA->get" << getUpperName() << "(), Record);\n";
189 }
190 };
191
192 class AlignedArgument : public Argument {
193 public:
194 AlignedArgument(Record &Arg, StringRef Attr)
195 : Argument(Arg, Attr)
196 {}
197
198 void writeAccessors(raw_ostream &OS) const {
199 OS << " bool is" << getUpperName() << "Dependent() const;\n";
200
201 OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n";
202
203 OS << " bool is" << getUpperName() << "Expr() const {\n";
204 OS << " return is" << getLowerName() << "Expr;\n";
205 OS << " }\n";
206
207 OS << " Expr *get" << getUpperName() << "Expr() const {\n";
208 OS << " assert(is" << getLowerName() << "Expr);\n";
209 OS << " return " << getLowerName() << "Expr;\n";
210 OS << " }\n";
211
212 OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n";
213 OS << " assert(!is" << getLowerName() << "Expr);\n";
214 OS << " return " << getLowerName() << "Type;\n";
215 OS << " }";
216 }
217 void writeAccessorDefinitions(raw_ostream &OS) const {
218 OS << "bool " << getAttrName() << "Attr::is" << getUpperName()
219 << "Dependent() const {\n";
220 OS << " if (is" << getLowerName() << "Expr)\n";
221 OS << " return " << getLowerName() << "Expr && (" << getLowerName()
222 << "Expr->isValueDependent() || " << getLowerName()
223 << "Expr->isTypeDependent());\n";
224 OS << " else\n";
225 OS << " return " << getLowerName()
226 << "Type->getType()->isDependentType();\n";
227 OS << "}\n";
228
229 // FIXME: Do not do the calculation here
230 // FIXME: Handle types correctly
231 // A null pointer means maximum alignment
232 // FIXME: Load the platform-specific maximum alignment, rather than
233 // 16, the x86 max.
234 OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
235 << "(ASTContext &Ctx) const {\n";
236 OS << " assert(!is" << getUpperName() << "Dependent());\n";
237 OS << " if (is" << getLowerName() << "Expr)\n";
238 OS << " return (" << getLowerName() << "Expr ? " << getLowerName()
239 << "Expr->EvaluateAsInt(Ctx).getZExtValue() : 16)"
240 << "* Ctx.getCharWidth();\n";
241 OS << " else\n";
242 OS << " return 0; // FIXME\n";
243 OS << "}\n";
244 }
245 void writeCloneArgs(raw_ostream &OS) const {
246 OS << "is" << getLowerName() << "Expr, is" << getLowerName()
247 << "Expr ? static_cast<void*>(" << getLowerName()
248 << "Expr) : " << getLowerName()
249 << "Type";
250 }
251 void writeCtorBody(raw_ostream &OS) const {
252 OS << " if (is" << getLowerName() << "Expr)\n";
253 OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>("
254 << getUpperName() << ");\n";
255 OS << " else\n";
256 OS << " " << getLowerName()
257 << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName()
258 << ");";
259 }
260 void writeCtorInitializers(raw_ostream &OS) const {
261 OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)";
262 }
263 void writeCtorParameters(raw_ostream &OS) const {
264 OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName();
265 }
266 void writeDeclarations(raw_ostream &OS) const {
267 OS << "bool is" << getLowerName() << "Expr;\n";
268 OS << "union {\n";
269 OS << "Expr *" << getLowerName() << "Expr;\n";
270 OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
271 OS << "};";
272 }
273 void writePCHReadArgs(raw_ostream &OS) const {
274 OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr";
275 }
276 void writePCHReadDecls(raw_ostream &OS) const {
277 OS << " bool is" << getLowerName() << "Expr = Record[Idx++];\n";
278 OS << " void *" << getLowerName() << "Ptr;\n";
279 OS << " if (is" << getLowerName() << "Expr)\n";
Sebastian Redl2dc91642010-10-05 15:59:36 +0000280 OS << " " << getLowerName() << "Ptr = ReadExpr(F);\n";
Sean Hunt726a3d22010-08-18 23:23:09 +0000281 OS << " else\n";
282 OS << " " << getLowerName()
Sebastian Redl2dc91642010-10-05 15:59:36 +0000283 << "Ptr = GetTypeSourceInfo(F, Record, Idx);\n";
Sean Hunt726a3d22010-08-18 23:23:09 +0000284 }
285 void writePCHWrite(raw_ostream &OS) const {
286 OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n";
287 OS << " if (SA->is" << getUpperName() << "Expr())\n";
288 OS << " AddStmt(SA->get" << getUpperName() << "Expr());\n";
289 OS << " else\n";
290 OS << " AddTypeSourceInfo(SA->get" << getUpperName()
291 << "Type(), Record);\n";
292 }
293 };
294
295 class VariadicArgument : public Argument {
296 std::string type;
297
298 public:
299 VariadicArgument(Record &Arg, StringRef Attr, std::string T)
300 : Argument(Arg, Attr), type(T)
301 {}
302
303 std::string getType() const { return type; }
304
305 void writeAccessors(raw_ostream &OS) const {
306 OS << " typedef " << type << "* " << getLowerName() << "_iterator;\n";
307 OS << " " << getLowerName() << "_iterator " << getLowerName()
308 << "_begin() const {\n";
309 OS << " return " << getLowerName() << ";\n";
310 OS << " }\n";
311 OS << " " << getLowerName() << "_iterator " << getLowerName()
312 << "_end() const {\n";
313 OS << " return " << getLowerName() << " + " << getLowerName()
314 << "Size;\n";
315 OS << " }\n";
316 OS << " unsigned " << getLowerName() << "_size() const {\n"
317 << " return " << getLowerName() << "Size;\n;";
318 OS << " }";
319 }
320 void writeCloneArgs(raw_ostream &OS) const {
321 OS << getLowerName() << ", " << getLowerName() << "Size";
322 }
323 void writeCtorBody(raw_ostream &OS) const {
324 // FIXME: memcpy is not safe on non-trivial types.
325 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
326 << ", " << getLowerName() << "Size * sizeof(" << getType() << "));\n";
327 }
328 void writeCtorInitializers(raw_ostream &OS) const {
329 OS << getLowerName() << "Size(" << getUpperName() << "Size), "
330 << getLowerName() << "(new (Ctx, 16) " << getType() << "["
331 << getLowerName() << "Size])";
332 }
333 void writeCtorParameters(raw_ostream &OS) const {
334 OS << getType() << " *" << getUpperName() << ", unsigned "
335 << getUpperName() << "Size";
336 }
337 void writeDeclarations(raw_ostream &OS) const {
338 OS << " unsigned " << getLowerName() << "Size;\n";
339 OS << " " << getType() << " *" << getLowerName() << ";";
340 }
341 void writePCHReadDecls(raw_ostream &OS) const {
342 OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
343 OS << " llvm::SmallVector<" << type << ", 4> " << getLowerName()
344 << ";\n";
345 OS << " " << getLowerName() << ".reserve(" << getLowerName()
346 << "Size);\n";
347 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
348
349 std::string read = ReadPCHRecord(type);
350 OS << " " << getLowerName() << ".push_back(" << read << ");\n";
351 }
352 void writePCHReadArgs(raw_ostream &OS) const {
353 OS << getLowerName() << ".data(), " << getLowerName() << "Size";
354 }
355 void writePCHWrite(raw_ostream &OS) const{
356 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
357 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
358 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
359 << getLowerName() << "_end(); i != e; ++i)\n";
360 OS << " " << WritePCHRecord(type, "(*i)");
361 }
362 };
363
364 class EnumArgument : public Argument {
Eli Friedmana8fa3922010-08-19 06:11:05 +0000365 std::string type;
Sean Hunt726a3d22010-08-18 23:23:09 +0000366 std::vector<StringRef> values, enums;
367 public:
368 EnumArgument(Record &Arg, StringRef Attr)
369 : Argument(Arg, Attr), type(Arg.getValueAsString("Type")),
370 values(getValueAsListOfStrings(Arg, "Values")),
371 enums(getValueAsListOfStrings(Arg, "Enums"))
372 {}
373
374 void writeAccessors(raw_ostream &OS) const {
375 OS << " " << type << " get" << getUpperName() << "() const {\n";
376 OS << " return " << getLowerName() << ";\n";
377 OS << " }";
378 }
379 void writeCloneArgs(raw_ostream &OS) const {
380 OS << getLowerName();
381 }
382 void writeCtorInitializers(raw_ostream &OS) const {
383 OS << getLowerName() << "(" << getUpperName() << ")";
384 }
385 void writeCtorParameters(raw_ostream &OS) const {
386 OS << type << " " << getUpperName();
387 }
388 void writeDeclarations(raw_ostream &OS) const {
389 // Calculate the various enum values
390 std::vector<StringRef> uniques(enums);
391 std::sort(uniques.begin(), uniques.end());
392 uniques.erase(std::unique(uniques.begin(), uniques.end()),
393 uniques.end());
394 // FIXME: Emit a proper error
395 assert(!uniques.empty());
396
397 std::vector<StringRef>::iterator i = uniques.begin(),
398 e = uniques.end();
399 // The last one needs to not have a comma.
400 --e;
401
402 OS << "public:\n";
403 OS << " enum " << type << " {\n";
404 for (; i != e; ++i)
405 OS << " " << *i << ",\n";
406 OS << " " << *e << "\n";
407 OS << " };\n";
408 OS << "private:\n";
409 OS << " " << type << " " << getLowerName() << ";";
410 }
411 void writePCHReadDecls(raw_ostream &OS) const {
412 OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName()
413 << "(static_cast<" << getAttrName() << "Attr::" << type
414 << ">(Record[Idx++]));\n";
415 }
416 void writePCHReadArgs(raw_ostream &OS) const {
417 OS << getLowerName();
418 }
419 void writePCHWrite(raw_ostream &OS) const {
420 OS << "Record.push_back(SA->get" << getUpperName() << "());\n";
421 }
422 };
Douglas Gregor095a3f32011-03-23 01:05:46 +0000423
424 class VersionArgument : public Argument {
425 public:
426 VersionArgument(Record &Arg, StringRef Attr)
427 : Argument(Arg, Attr)
428 {}
429
430 void writeAccessors(raw_ostream &OS) const {
431 OS << " VersionTuple get" << getUpperName() << "() const {\n";
432 OS << " return " << getLowerName() << ";\n";
433 OS << " }\n";
434 OS << " void set" << getUpperName()
435 << "(ASTContext &C, VersionTuple V) {\n";
436 OS << " " << getLowerName() << " = V;\n";
437 OS << " }";
438 }
439 void writeCloneArgs(raw_ostream &OS) const {
440 OS << "get" << getUpperName() << "()";
441 }
442 void writeCtorBody(raw_ostream &OS) const {
443 }
444 void writeCtorInitializers(raw_ostream &OS) const {
445 OS << getLowerName() << "(" << getUpperName() << ")";
446 }
447 void writeCtorParameters(raw_ostream &OS) const {
448 OS << "VersionTuple " << getUpperName();
449 }
450 void writeDeclarations(raw_ostream &OS) const {
451 OS << "VersionTuple " << getLowerName() << ";\n";
452 }
453 void writePCHReadDecls(raw_ostream &OS) const {
454 OS << " VersionTuple " << getLowerName()
455 << "= ReadVersionTuple(Record, Idx);\n";
456 }
457 void writePCHReadArgs(raw_ostream &OS) const {
458 OS << getLowerName();
459 }
460 void writePCHWrite(raw_ostream &OS) const {
461 OS << " AddVersionTuple(SA->get" << getUpperName() << "(), Record);\n";
462 }
463 };
Sean Hunt726a3d22010-08-18 23:23:09 +0000464}
465
466static Argument *createArgument(Record &Arg, StringRef Attr,
467 Record *Search = 0) {
468 if (!Search)
469 Search = &Arg;
470
471 Argument *Ptr = 0;
472 llvm::StringRef ArgName = Search->getName();
473
474 if (ArgName == "AlignedArgument") Ptr = new AlignedArgument(Arg, Attr);
475 else if (ArgName == "EnumArgument") Ptr = new EnumArgument(Arg, Attr);
476 else if (ArgName == "ExprArgument") Ptr = new SimpleArgument(Arg, Attr,
477 "Expr *");
478 else if (ArgName == "FunctionArgument")
479 Ptr = new SimpleArgument(Arg, Attr, "FunctionDecl *");
480 else if (ArgName == "IdentifierArgument")
481 Ptr = new SimpleArgument(Arg, Attr, "IdentifierInfo *");
Douglas Gregor32ce3f92011-03-26 03:40:01 +0000482 else if (ArgName == "BoolArgument") Ptr = new SimpleArgument(Arg, Attr,
483 "bool");
Sean Hunt726a3d22010-08-18 23:23:09 +0000484 else if (ArgName == "IntArgument") Ptr = new SimpleArgument(Arg, Attr, "int");
485 else if (ArgName == "StringArgument") Ptr = new StringArgument(Arg, Attr);
486 else if (ArgName == "TypeArgument")
487 Ptr = new SimpleArgument(Arg, Attr, "QualType");
488 else if (ArgName == "UnsignedArgument")
489 Ptr = new SimpleArgument(Arg, Attr, "unsigned");
490 else if (ArgName == "VariadicUnsignedArgument")
491 Ptr = new VariadicArgument(Arg, Attr, "unsigned");
Douglas Gregor095a3f32011-03-23 01:05:46 +0000492 else if (ArgName == "VersionArgument")
493 Ptr = new VersionArgument(Arg, Attr);
Sean Hunt726a3d22010-08-18 23:23:09 +0000494
495 if (!Ptr) {
496 std::vector<Record*> Bases = Search->getSuperClasses();
497 for (std::vector<Record*>::iterator i = Bases.begin(), e = Bases.end();
498 i != e; ++i) {
499 Ptr = createArgument(Arg, Attr, *i);
500 if (Ptr)
501 break;
502 }
503 }
504 return Ptr;
505}
506
Sean Hunt16171442010-06-16 23:45:50 +0000507void ClangAttrClassEmitter::run(raw_ostream &OS) {
508 OS << "// This file is generated by TableGen. Do not edit.\n\n";
509 OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
510 OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
511
512 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
513
514 for (std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end();
515 i != e; ++i) {
516 Record &R = **i;
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000517 const std::string &SuperName = R.getSuperClasses().back()->getName();
Sean Hunt16171442010-06-16 23:45:50 +0000518
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000519 OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
Sean Hunt16171442010-06-16 23:45:50 +0000520
Sean Hunt726a3d22010-08-18 23:23:09 +0000521 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
522 std::vector<Argument*> Args;
523 std::vector<Argument*>::iterator ai, ae;
524 Args.reserve(ArgRecords.size());
Sean Hunt16171442010-06-16 23:45:50 +0000525
Sean Hunt726a3d22010-08-18 23:23:09 +0000526 for (std::vector<Record*>::iterator ri = ArgRecords.begin(),
527 re = ArgRecords.end();
528 ri != re; ++ri) {
529 Record &ArgRecord = **ri;
530 Argument *Arg = createArgument(ArgRecord, R.getName());
531 assert(Arg);
532 Args.push_back(Arg);
533
534 Arg->writeDeclarations(OS);
535 OS << "\n\n";
536 }
537
538 ae = Args.end();
Sean Hunt16171442010-06-16 23:45:50 +0000539
540 OS << "\n public:\n";
Sean Hunt726a3d22010-08-18 23:23:09 +0000541 OS << " " << R.getName() << "Attr(SourceLocation L, ASTContext &Ctx\n";
Sean Hunt16171442010-06-16 23:45:50 +0000542
Sean Hunt726a3d22010-08-18 23:23:09 +0000543 for (ai = Args.begin(); ai != ae; ++ai) {
544 OS << " , ";
545 (*ai)->writeCtorParameters(OS);
546 OS << "\n";
547 }
Sean Hunt16171442010-06-16 23:45:50 +0000548
Sean Hunt726a3d22010-08-18 23:23:09 +0000549 OS << " )\n";
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000550 OS << " : " << SuperName << "(attr::" << R.getName() << ", L)\n";
Sean Hunt16171442010-06-16 23:45:50 +0000551
Sean Hunt726a3d22010-08-18 23:23:09 +0000552 for (ai = Args.begin(); ai != ae; ++ai) {
553 OS << " , ";
554 (*ai)->writeCtorInitializers(OS);
555 OS << "\n";
556 }
Sean Hunt16171442010-06-16 23:45:50 +0000557
Sean Hunt726a3d22010-08-18 23:23:09 +0000558 OS << " {\n";
559
560 for (ai = Args.begin(); ai != ae; ++ai) {
561 (*ai)->writeCtorBody(OS);
562 OS << "\n";
563 }
564 OS << " }\n\n";
565
566 OS << " virtual " << R.getName() << "Attr *clone (ASTContext &C) const;\n";
567
568 for (ai = Args.begin(); ai != ae; ++ai) {
569 (*ai)->writeAccessors(OS);
570 OS << "\n\n";
571 }
572
573 OS << R.getValueAsCode("AdditionalMembers");
574 OS << "\n\n";
575
Sean Hunt16171442010-06-16 23:45:50 +0000576 OS << " static bool classof(const Attr *A) { return A->getKind() == "
577 << "attr::" << R.getName() << "; }\n";
578 OS << " static bool classof(const " << R.getName()
579 << "Attr *) { return true; }\n";
580 OS << "};\n\n";
581 }
582
583 OS << "#endif\n";
584}
585
Sean Hunt726a3d22010-08-18 23:23:09 +0000586void ClangAttrImplEmitter::run(raw_ostream &OS) {
587 OS << "// This file is generated by TableGen. Do not edit.\n\n";
588
589 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
590 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ri, re;
591 std::vector<Argument*>::iterator ai, ae;
592
593 for (; i != e; ++i) {
594 Record &R = **i;
595 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
596 std::vector<Argument*> Args;
597 for (ri = ArgRecords.begin(), re = ArgRecords.end(); ri != re; ++ri)
598 Args.push_back(createArgument(**ri, R.getName()));
599
600 for (ai = Args.begin(), ae = Args.end(); ai != ae; ++ai)
601 (*ai)->writeAccessorDefinitions(OS);
602
603 OS << R.getName() << "Attr *" << R.getName()
604 << "Attr::clone(ASTContext &C) const {\n";
605 OS << " return new (C) " << R.getName() << "Attr(getLocation(), C";
606 for (ai = Args.begin(); ai != ae; ++ai) {
607 OS << ", ";
608 (*ai)->writeCloneArgs(OS);
609 }
610 OS << ");\n}\n\n";
611 }
612}
613
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000614static void EmitAttrList(raw_ostream &OS, StringRef Class,
615 const std::vector<Record*> &AttrList) {
616 std::vector<Record*>::const_iterator i = AttrList.begin(), e = AttrList.end();
617
618 if (i != e) {
619 // Move the end iterator back to emit the last attribute.
620 for(--e; i != e; ++i)
621 OS << Class << "(" << (*i)->getName() << ")\n";
622
623 OS << "LAST_" << Class << "(" << (*i)->getName() << ")\n\n";
624 }
625}
626
Sean Hunt16171442010-06-16 23:45:50 +0000627void ClangAttrListEmitter::run(raw_ostream &OS) {
628 OS << "// This file is generated by TableGen. Do not edit.\n\n";
629
630 OS << "#ifndef LAST_ATTR\n";
631 OS << "#define LAST_ATTR(NAME) ATTR(NAME)\n";
632 OS << "#endif\n\n";
Sean Hunt16171442010-06-16 23:45:50 +0000633
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000634 OS << "#ifndef INHERITABLE_ATTR\n";
635 OS << "#define INHERITABLE_ATTR(NAME) ATTR(NAME)\n";
636 OS << "#endif\n\n";
637
638 OS << "#ifndef LAST_INHERITABLE_ATTR\n";
639 OS << "#define LAST_INHERITABLE_ATTR(NAME) INHERITABLE_ATTR(NAME)\n";
640 OS << "#endif\n\n";
641
John McCall9977e522011-03-02 04:00:52 +0000642 OS << "#ifndef INHERITABLE_PARAM_ATTR\n";
643 OS << "#define INHERITABLE_PARAM_ATTR(NAME) ATTR(NAME)\n";
644 OS << "#endif\n\n";
645
646 OS << "#ifndef LAST_INHERITABLE_PARAM_ATTR\n";
647 OS << "#define LAST_INHERITABLE_PARAM_ATTR(NAME)"
648 " INHERITABLE_PARAM_ATTR(NAME)\n";
649 OS << "#endif\n\n";
650
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000651 Record *InhClass = Records.getClass("InheritableAttr");
John McCall9977e522011-03-02 04:00:52 +0000652 Record *InhParamClass = Records.getClass("InheritableParamAttr");
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000653 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
John McCall9977e522011-03-02 04:00:52 +0000654 NonInhAttrs, InhAttrs, InhParamAttrs;
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000655 for (std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end();
656 i != e; ++i) {
John McCall9977e522011-03-02 04:00:52 +0000657 if ((*i)->isSubClassOf(InhParamClass))
658 InhParamAttrs.push_back(*i);
659 else if ((*i)->isSubClassOf(InhClass))
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000660 InhAttrs.push_back(*i);
661 else
662 NonInhAttrs.push_back(*i);
Sean Hunt16171442010-06-16 23:45:50 +0000663 }
664
John McCall9977e522011-03-02 04:00:52 +0000665 EmitAttrList(OS, "INHERITABLE_PARAM_ATTR", InhParamAttrs);
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000666 EmitAttrList(OS, "INHERITABLE_ATTR", InhAttrs);
667 EmitAttrList(OS, "ATTR", NonInhAttrs);
668
Sean Hunt16171442010-06-16 23:45:50 +0000669 OS << "#undef LAST_ATTR\n";
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000670 OS << "#undef INHERITABLE_ATTR\n";
671 OS << "#undef LAST_INHERITABLE_ATTR\n";
John McCall9977e522011-03-02 04:00:52 +0000672 OS << "#undef LAST_INHERITABLE_PARAM_ATTR\n";
Sean Hunt16171442010-06-16 23:45:50 +0000673 OS << "#undef ATTR\n";
674}
Sean Hunt726a3d22010-08-18 23:23:09 +0000675
676void ClangAttrPCHReadEmitter::run(raw_ostream &OS) {
Francois Pichet8ec055d2010-10-01 21:20:39 +0000677 OS << "// This file is generated by TableGen. Do not edit.\n\n";
Sean Hunt726a3d22010-08-18 23:23:09 +0000678
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000679 Record *InhClass = Records.getClass("InheritableAttr");
Sean Hunt726a3d22010-08-18 23:23:09 +0000680 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
681 ArgRecords;
682 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ai, ae;
683 std::vector<Argument*> Args;
684 std::vector<Argument*>::iterator ri, re;
685
686 OS << " switch (Kind) {\n";
687 OS << " default:\n";
688 OS << " assert(0 && \"Unknown attribute!\");\n";
689 OS << " break;\n";
690 for (; i != e; ++i) {
691 Record &R = **i;
692 OS << " case attr::" << R.getName() << ": {\n";
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000693 if (R.isSubClassOf(InhClass))
694 OS << " bool isInherited = Record[Idx++];\n";
Sean Hunt726a3d22010-08-18 23:23:09 +0000695 ArgRecords = R.getValueAsListOfDefs("Args");
696 Args.clear();
697 for (ai = ArgRecords.begin(), ae = ArgRecords.end(); ai != ae; ++ai) {
698 Argument *A = createArgument(**ai, R.getName());
699 Args.push_back(A);
700 A->writePCHReadDecls(OS);
701 }
702 OS << " New = new (*Context) " << R.getName() << "Attr(Loc, *Context";
703 for (ri = Args.begin(), re = Args.end(); ri != re; ++ri) {
704 OS << ", ";
705 (*ri)->writePCHReadArgs(OS);
706 }
707 OS << ");\n";
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000708 if (R.isSubClassOf(InhClass))
709 OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
Sean Hunt726a3d22010-08-18 23:23:09 +0000710 OS << " break;\n";
711 OS << " }\n";
712 }
713 OS << " }\n";
714}
715
716void ClangAttrPCHWriteEmitter::run(raw_ostream &OS) {
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000717 Record *InhClass = Records.getClass("InheritableAttr");
Sean Hunt726a3d22010-08-18 23:23:09 +0000718 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
719 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ai, ae;
720
721 OS << " switch (A->getKind()) {\n";
722 OS << " default:\n";
723 OS << " llvm_unreachable(\"Unknown attribute kind!\");\n";
724 OS << " break;\n";
725 for (; i != e; ++i) {
726 Record &R = **i;
727 OS << " case attr::" << R.getName() << ": {\n";
728 Args = R.getValueAsListOfDefs("Args");
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000729 if (R.isSubClassOf(InhClass) || !Args.empty())
Sean Hunt726a3d22010-08-18 23:23:09 +0000730 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
731 << "Attr>(A);\n";
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000732 if (R.isSubClassOf(InhClass))
733 OS << " Record.push_back(SA->isInherited());\n";
Sean Hunt726a3d22010-08-18 23:23:09 +0000734 for (ai = Args.begin(), ae = Args.end(); ai != ae; ++ai)
735 createArgument(**ai, R.getName())->writePCHWrite(OS);
736 OS << " break;\n";
737 OS << " }\n";
738 }
739 OS << " }\n";
740}
Anders Carlsson238777e2010-10-20 01:21:53 +0000741
742void ClangAttrSpellingListEmitter::run(raw_ostream &OS) {
743 OS << "// This file is generated by TableGen. Do not edit.\n\n";
744
745 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
746
747 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
748 Record &Attr = **I;
749
750 std::vector<StringRef> Spellings = getValueAsListOfStrings(Attr, "Spellings");
751
752 for (std::vector<StringRef>::const_iterator I = Spellings.begin(), E = Spellings.end(); I != E; ++I) {
753 StringRef Spelling = *I;
754 OS << ".Case(\"" << Spelling << "\", true)\n";
755 }
756 }
757
758}