blob: 6ef25d9a3ac4ab080e5ce132a5d622f834300009 [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 Greene05bce0b2011-07-29 22:43:06 +000024 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 Greene05bce0b2011-07-29 22:43:06 +000034 if (StringInit *S = dynamic_cast<StringInit *>(*i))
Sean Hunt726a3d22010-08-18 23:23:09 +000035 Strings.push_back(S->getValue());
David Greene05bce0b2011-07-29 22:43:06 +000036 else if (CodeInit *C = dynamic_cast<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");
Caitlin Sadowskib1aa80b2011-08-23 18:49:23 +0000492 else if (ArgName == "VariadicExprArgument")
493 Ptr = new VariadicArgument(Arg, Attr, "Expr *");
Douglas Gregor095a3f32011-03-23 01:05:46 +0000494 else if (ArgName == "VersionArgument")
495 Ptr = new VersionArgument(Arg, Attr);
Sean Hunt726a3d22010-08-18 23:23:09 +0000496
497 if (!Ptr) {
498 std::vector<Record*> Bases = Search->getSuperClasses();
499 for (std::vector<Record*>::iterator i = Bases.begin(), e = Bases.end();
500 i != e; ++i) {
501 Ptr = createArgument(Arg, Attr, *i);
502 if (Ptr)
503 break;
504 }
505 }
506 return Ptr;
507}
508
Sean Hunt16171442010-06-16 23:45:50 +0000509void ClangAttrClassEmitter::run(raw_ostream &OS) {
510 OS << "// This file is generated by TableGen. Do not edit.\n\n";
511 OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
512 OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
513
514 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
515
516 for (std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end();
517 i != e; ++i) {
518 Record &R = **i;
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000519 const std::string &SuperName = R.getSuperClasses().back()->getName();
Sean Hunt16171442010-06-16 23:45:50 +0000520
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000521 OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
Sean Hunt16171442010-06-16 23:45:50 +0000522
Sean Hunt726a3d22010-08-18 23:23:09 +0000523 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
524 std::vector<Argument*> Args;
525 std::vector<Argument*>::iterator ai, ae;
526 Args.reserve(ArgRecords.size());
Sean Hunt16171442010-06-16 23:45:50 +0000527
Sean Hunt726a3d22010-08-18 23:23:09 +0000528 for (std::vector<Record*>::iterator ri = ArgRecords.begin(),
529 re = ArgRecords.end();
530 ri != re; ++ri) {
531 Record &ArgRecord = **ri;
532 Argument *Arg = createArgument(ArgRecord, R.getName());
533 assert(Arg);
534 Args.push_back(Arg);
535
536 Arg->writeDeclarations(OS);
537 OS << "\n\n";
538 }
539
540 ae = Args.end();
Sean Hunt16171442010-06-16 23:45:50 +0000541
542 OS << "\n public:\n";
Sean Hunt726a3d22010-08-18 23:23:09 +0000543 OS << " " << R.getName() << "Attr(SourceLocation L, ASTContext &Ctx\n";
Sean Hunt16171442010-06-16 23:45:50 +0000544
Sean Hunt726a3d22010-08-18 23:23:09 +0000545 for (ai = Args.begin(); ai != ae; ++ai) {
546 OS << " , ";
547 (*ai)->writeCtorParameters(OS);
548 OS << "\n";
549 }
Sean Hunt16171442010-06-16 23:45:50 +0000550
Sean Hunt726a3d22010-08-18 23:23:09 +0000551 OS << " )\n";
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000552 OS << " : " << SuperName << "(attr::" << R.getName() << ", L)\n";
Sean Hunt16171442010-06-16 23:45:50 +0000553
Sean Hunt726a3d22010-08-18 23:23:09 +0000554 for (ai = Args.begin(); ai != ae; ++ai) {
555 OS << " , ";
556 (*ai)->writeCtorInitializers(OS);
557 OS << "\n";
558 }
Sean Hunt16171442010-06-16 23:45:50 +0000559
Sean Hunt726a3d22010-08-18 23:23:09 +0000560 OS << " {\n";
561
562 for (ai = Args.begin(); ai != ae; ++ai) {
563 (*ai)->writeCtorBody(OS);
564 OS << "\n";
565 }
566 OS << " }\n\n";
567
568 OS << " virtual " << R.getName() << "Attr *clone (ASTContext &C) const;\n";
569
570 for (ai = Args.begin(); ai != ae; ++ai) {
571 (*ai)->writeAccessors(OS);
572 OS << "\n\n";
573 }
574
575 OS << R.getValueAsCode("AdditionalMembers");
576 OS << "\n\n";
577
Sean Hunt16171442010-06-16 23:45:50 +0000578 OS << " static bool classof(const Attr *A) { return A->getKind() == "
579 << "attr::" << R.getName() << "; }\n";
580 OS << " static bool classof(const " << R.getName()
581 << "Attr *) { return true; }\n";
582 OS << "};\n\n";
583 }
584
585 OS << "#endif\n";
586}
587
Sean Hunt726a3d22010-08-18 23:23:09 +0000588void ClangAttrImplEmitter::run(raw_ostream &OS) {
589 OS << "// This file is generated by TableGen. Do not edit.\n\n";
590
591 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
592 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ri, re;
593 std::vector<Argument*>::iterator ai, ae;
594
595 for (; i != e; ++i) {
596 Record &R = **i;
597 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
598 std::vector<Argument*> Args;
599 for (ri = ArgRecords.begin(), re = ArgRecords.end(); ri != re; ++ri)
600 Args.push_back(createArgument(**ri, R.getName()));
601
602 for (ai = Args.begin(), ae = Args.end(); ai != ae; ++ai)
603 (*ai)->writeAccessorDefinitions(OS);
604
605 OS << R.getName() << "Attr *" << R.getName()
606 << "Attr::clone(ASTContext &C) const {\n";
607 OS << " return new (C) " << R.getName() << "Attr(getLocation(), C";
608 for (ai = Args.begin(); ai != ae; ++ai) {
609 OS << ", ";
610 (*ai)->writeCloneArgs(OS);
611 }
612 OS << ");\n}\n\n";
613 }
614}
615
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000616static void EmitAttrList(raw_ostream &OS, StringRef Class,
617 const std::vector<Record*> &AttrList) {
618 std::vector<Record*>::const_iterator i = AttrList.begin(), e = AttrList.end();
619
620 if (i != e) {
621 // Move the end iterator back to emit the last attribute.
622 for(--e; i != e; ++i)
623 OS << Class << "(" << (*i)->getName() << ")\n";
624
625 OS << "LAST_" << Class << "(" << (*i)->getName() << ")\n\n";
626 }
627}
628
Sean Hunt16171442010-06-16 23:45:50 +0000629void ClangAttrListEmitter::run(raw_ostream &OS) {
630 OS << "// This file is generated by TableGen. Do not edit.\n\n";
631
632 OS << "#ifndef LAST_ATTR\n";
633 OS << "#define LAST_ATTR(NAME) ATTR(NAME)\n";
634 OS << "#endif\n\n";
Sean Hunt16171442010-06-16 23:45:50 +0000635
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000636 OS << "#ifndef INHERITABLE_ATTR\n";
637 OS << "#define INHERITABLE_ATTR(NAME) ATTR(NAME)\n";
638 OS << "#endif\n\n";
639
640 OS << "#ifndef LAST_INHERITABLE_ATTR\n";
641 OS << "#define LAST_INHERITABLE_ATTR(NAME) INHERITABLE_ATTR(NAME)\n";
642 OS << "#endif\n\n";
643
John McCall9977e522011-03-02 04:00:52 +0000644 OS << "#ifndef INHERITABLE_PARAM_ATTR\n";
645 OS << "#define INHERITABLE_PARAM_ATTR(NAME) ATTR(NAME)\n";
646 OS << "#endif\n\n";
647
648 OS << "#ifndef LAST_INHERITABLE_PARAM_ATTR\n";
649 OS << "#define LAST_INHERITABLE_PARAM_ATTR(NAME)"
650 " INHERITABLE_PARAM_ATTR(NAME)\n";
651 OS << "#endif\n\n";
652
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000653 Record *InhClass = Records.getClass("InheritableAttr");
John McCall9977e522011-03-02 04:00:52 +0000654 Record *InhParamClass = Records.getClass("InheritableParamAttr");
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000655 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
John McCall9977e522011-03-02 04:00:52 +0000656 NonInhAttrs, InhAttrs, InhParamAttrs;
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000657 for (std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end();
658 i != e; ++i) {
John McCall9977e522011-03-02 04:00:52 +0000659 if ((*i)->isSubClassOf(InhParamClass))
660 InhParamAttrs.push_back(*i);
661 else if ((*i)->isSubClassOf(InhClass))
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000662 InhAttrs.push_back(*i);
663 else
664 NonInhAttrs.push_back(*i);
Sean Hunt16171442010-06-16 23:45:50 +0000665 }
666
John McCall9977e522011-03-02 04:00:52 +0000667 EmitAttrList(OS, "INHERITABLE_PARAM_ATTR", InhParamAttrs);
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000668 EmitAttrList(OS, "INHERITABLE_ATTR", InhAttrs);
669 EmitAttrList(OS, "ATTR", NonInhAttrs);
670
Sean Hunt16171442010-06-16 23:45:50 +0000671 OS << "#undef LAST_ATTR\n";
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000672 OS << "#undef INHERITABLE_ATTR\n";
673 OS << "#undef LAST_INHERITABLE_ATTR\n";
John McCall9977e522011-03-02 04:00:52 +0000674 OS << "#undef LAST_INHERITABLE_PARAM_ATTR\n";
Sean Hunt16171442010-06-16 23:45:50 +0000675 OS << "#undef ATTR\n";
676}
Sean Hunt726a3d22010-08-18 23:23:09 +0000677
678void ClangAttrPCHReadEmitter::run(raw_ostream &OS) {
Francois Pichet8ec055d2010-10-01 21:20:39 +0000679 OS << "// This file is generated by TableGen. Do not edit.\n\n";
Sean Hunt726a3d22010-08-18 23:23:09 +0000680
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000681 Record *InhClass = Records.getClass("InheritableAttr");
Sean Hunt726a3d22010-08-18 23:23:09 +0000682 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
683 ArgRecords;
684 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ai, ae;
685 std::vector<Argument*> Args;
686 std::vector<Argument*>::iterator ri, re;
687
688 OS << " switch (Kind) {\n";
689 OS << " default:\n";
690 OS << " assert(0 && \"Unknown attribute!\");\n";
691 OS << " break;\n";
692 for (; i != e; ++i) {
693 Record &R = **i;
694 OS << " case attr::" << R.getName() << ": {\n";
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000695 if (R.isSubClassOf(InhClass))
696 OS << " bool isInherited = Record[Idx++];\n";
Sean Hunt726a3d22010-08-18 23:23:09 +0000697 ArgRecords = R.getValueAsListOfDefs("Args");
698 Args.clear();
699 for (ai = ArgRecords.begin(), ae = ArgRecords.end(); ai != ae; ++ai) {
700 Argument *A = createArgument(**ai, R.getName());
701 Args.push_back(A);
702 A->writePCHReadDecls(OS);
703 }
704 OS << " New = new (*Context) " << R.getName() << "Attr(Loc, *Context";
705 for (ri = Args.begin(), re = Args.end(); ri != re; ++ri) {
706 OS << ", ";
707 (*ri)->writePCHReadArgs(OS);
708 }
709 OS << ");\n";
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000710 if (R.isSubClassOf(InhClass))
711 OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
Sean Hunt726a3d22010-08-18 23:23:09 +0000712 OS << " break;\n";
713 OS << " }\n";
714 }
715 OS << " }\n";
716}
717
718void ClangAttrPCHWriteEmitter::run(raw_ostream &OS) {
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000719 Record *InhClass = Records.getClass("InheritableAttr");
Sean Hunt726a3d22010-08-18 23:23:09 +0000720 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
721 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ai, ae;
722
723 OS << " switch (A->getKind()) {\n";
724 OS << " default:\n";
725 OS << " llvm_unreachable(\"Unknown attribute kind!\");\n";
726 OS << " break;\n";
727 for (; i != e; ++i) {
728 Record &R = **i;
729 OS << " case attr::" << R.getName() << ": {\n";
730 Args = R.getValueAsListOfDefs("Args");
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000731 if (R.isSubClassOf(InhClass) || !Args.empty())
Sean Hunt726a3d22010-08-18 23:23:09 +0000732 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
733 << "Attr>(A);\n";
Peter Collingbournebe111ef2011-01-21 02:08:26 +0000734 if (R.isSubClassOf(InhClass))
735 OS << " Record.push_back(SA->isInherited());\n";
Sean Hunt726a3d22010-08-18 23:23:09 +0000736 for (ai = Args.begin(), ae = Args.end(); ai != ae; ++ai)
737 createArgument(**ai, R.getName())->writePCHWrite(OS);
738 OS << " break;\n";
739 OS << " }\n";
740 }
741 OS << " }\n";
742}
Anders Carlsson238777e2010-10-20 01:21:53 +0000743
744void ClangAttrSpellingListEmitter::run(raw_ostream &OS) {
745 OS << "// This file is generated by TableGen. Do not edit.\n\n";
746
747 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
748
749 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
750 Record &Attr = **I;
751
752 std::vector<StringRef> Spellings = getValueAsListOfStrings(Attr, "Spellings");
753
754 for (std::vector<StringRef>::const_iterator I = Spellings.begin(), E = Spellings.end(); I != E; ++I) {
755 StringRef Spelling = *I;
756 OS << ".Case(\"" << Spelling << "\", true)\n";
757 }
758 }
759
760}