blob: 3651636e9566224f70082d4db6a1f6d1593b01e5 [file] [log] [blame]
Peter Collingbourne51d77772011-10-06 13:03:08 +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
Sean Hunt93f95f22012-06-18 16:13:52 +000014#include "llvm/ADT/SmallString.h"
Peter Collingbourne51d77772011-10-06 13:03:08 +000015#include "llvm/ADT/StringSwitch.h"
16#include "llvm/TableGen/Record.h"
Douglas Gregor0c19b3c2012-05-02 17:33:51 +000017#include "llvm/TableGen/StringMatcher.h"
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +000018#include "llvm/TableGen/TableGenBackend.h"
Peter Collingbourne51d77772011-10-06 13:03:08 +000019#include <algorithm>
20#include <cctype>
21
22using namespace llvm;
23
24static const std::vector<StringRef>
25getValueAsListOfStrings(Record &R, StringRef FieldName) {
26 ListInit *List = R.getValueAsListInit(FieldName);
27 assert (List && "Got a null ListInit");
28
29 std::vector<StringRef> Strings;
30 Strings.reserve(List->getSize());
31
32 for (ListInit::const_iterator i = List->begin(), e = List->end();
33 i != e;
34 ++i) {
35 assert(*i && "Got a null element in a ListInit");
Sean Silva1ab46322012-10-10 20:25:43 +000036 if (StringInit *S = dyn_cast<StringInit>(*i))
Peter Collingbourne51d77772011-10-06 13:03:08 +000037 Strings.push_back(S->getValue());
Peter Collingbourne51d77772011-10-06 13:03:08 +000038 else
39 assert(false && "Got a non-string, non-code element in a ListInit");
40 }
41
42 return Strings;
43}
44
45static std::string ReadPCHRecord(StringRef type) {
46 return StringSwitch<std::string>(type)
47 .EndsWith("Decl *", "GetLocalDeclAs<"
48 + std::string(type, 0, type.size()-1) + ">(F, Record[Idx++])")
49 .Case("QualType", "getLocalType(F, Record[Idx++])")
Argyrios Kyrtzidis350aea72012-11-15 01:31:39 +000050 .Case("Expr *", "ReadExpr(F)")
Peter Collingbourne51d77772011-10-06 13:03:08 +000051 .Case("IdentifierInfo *", "GetIdentifierInfo(F, Record, Idx)")
52 .Case("SourceLocation", "ReadSourceLocation(F, Record, Idx)")
53 .Default("Record[Idx++]");
54}
55
56// Assumes that the way to get the value is SA->getname()
57static std::string WritePCHRecord(StringRef type, StringRef name) {
58 return StringSwitch<std::string>(type)
59 .EndsWith("Decl *", "AddDeclRef(" + std::string(name) +
60 ", Record);\n")
61 .Case("QualType", "AddTypeRef(" + std::string(name) + ", Record);\n")
62 .Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
63 .Case("IdentifierInfo *",
64 "AddIdentifierRef(" + std::string(name) + ", Record);\n")
65 .Case("SourceLocation",
66 "AddSourceLocation(" + std::string(name) + ", Record);\n")
67 .Default("Record.push_back(" + std::string(name) + ");\n");
68}
69
Michael Hane53ac8a2012-03-07 00:12:16 +000070// Normalize attribute name by removing leading and trailing
71// underscores. For example, __foo, foo__, __foo__ would
72// become foo.
73static StringRef NormalizeAttrName(StringRef AttrName) {
74 if (AttrName.startswith("__"))
75 AttrName = AttrName.substr(2, AttrName.size());
76
77 if (AttrName.endswith("__"))
78 AttrName = AttrName.substr(0, AttrName.size() - 2);
79
80 return AttrName;
81}
82
83// Normalize attribute spelling only if the spelling has both leading
84// and trailing underscores. For example, __ms_struct__ will be
85// normalized to "ms_struct"; __cdecl will remain intact.
86static StringRef NormalizeAttrSpelling(StringRef AttrSpelling) {
87 if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) {
88 AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4);
89 }
90
91 return AttrSpelling;
92}
93
Peter Collingbourne51d77772011-10-06 13:03:08 +000094namespace {
95 class Argument {
96 std::string lowerName, upperName;
97 StringRef attrName;
98
99 public:
100 Argument(Record &Arg, StringRef Attr)
101 : lowerName(Arg.getValueAsString("Name")), upperName(lowerName),
102 attrName(Attr) {
103 if (!lowerName.empty()) {
104 lowerName[0] = std::tolower(lowerName[0]);
105 upperName[0] = std::toupper(upperName[0]);
106 }
107 }
108 virtual ~Argument() {}
109
110 StringRef getLowerName() const { return lowerName; }
111 StringRef getUpperName() const { return upperName; }
112 StringRef getAttrName() const { return attrName; }
113
114 // These functions print the argument contents formatted in different ways.
115 virtual void writeAccessors(raw_ostream &OS) const = 0;
116 virtual void writeAccessorDefinitions(raw_ostream &OS) const {}
117 virtual void writeCloneArgs(raw_ostream &OS) const = 0;
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000118 virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0;
Daniel Dunbarb8806092012-02-10 06:00:29 +0000119 virtual void writeTemplateInstantiation(raw_ostream &OS) const {}
Peter Collingbourne51d77772011-10-06 13:03:08 +0000120 virtual void writeCtorBody(raw_ostream &OS) const {}
121 virtual void writeCtorInitializers(raw_ostream &OS) const = 0;
122 virtual void writeCtorParameters(raw_ostream &OS) const = 0;
123 virtual void writeDeclarations(raw_ostream &OS) const = 0;
124 virtual void writePCHReadArgs(raw_ostream &OS) const = 0;
125 virtual void writePCHReadDecls(raw_ostream &OS) const = 0;
126 virtual void writePCHWrite(raw_ostream &OS) const = 0;
Douglas Gregor1bea8802011-11-19 19:22:57 +0000127 virtual void writeValue(raw_ostream &OS) const = 0;
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000128 virtual void writeDump(raw_ostream &OS) const = 0;
129 virtual void writeDumpChildren(raw_ostream &OS) const {}
Peter Collingbourne51d77772011-10-06 13:03:08 +0000130 };
131
132 class SimpleArgument : public Argument {
133 std::string type;
134
135 public:
136 SimpleArgument(Record &Arg, StringRef Attr, std::string T)
137 : Argument(Arg, Attr), type(T)
138 {}
139
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000140 std::string getType() const { return type; }
141
Peter Collingbourne51d77772011-10-06 13:03:08 +0000142 void writeAccessors(raw_ostream &OS) const {
143 OS << " " << type << " get" << getUpperName() << "() const {\n";
144 OS << " return " << getLowerName() << ";\n";
145 OS << " }";
146 }
147 void writeCloneArgs(raw_ostream &OS) const {
148 OS << getLowerName();
149 }
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000150 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
151 OS << "A->get" << getUpperName() << "()";
152 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000153 void writeCtorInitializers(raw_ostream &OS) const {
154 OS << getLowerName() << "(" << getUpperName() << ")";
155 }
156 void writeCtorParameters(raw_ostream &OS) const {
157 OS << type << " " << getUpperName();
158 }
159 void writeDeclarations(raw_ostream &OS) const {
160 OS << type << " " << getLowerName() << ";";
161 }
162 void writePCHReadDecls(raw_ostream &OS) const {
163 std::string read = ReadPCHRecord(type);
164 OS << " " << type << " " << getLowerName() << " = " << read << ";\n";
165 }
166 void writePCHReadArgs(raw_ostream &OS) const {
167 OS << getLowerName();
168 }
169 void writePCHWrite(raw_ostream &OS) const {
170 OS << " " << WritePCHRecord(type, "SA->get" +
171 std::string(getUpperName()) + "()");
172 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000173 void writeValue(raw_ostream &OS) const {
174 if (type == "FunctionDecl *") {
175 OS << "\" << get" << getUpperName() << "()->getNameInfo().getAsString() << \"";
176 } else if (type == "IdentifierInfo *") {
177 OS << "\" << get" << getUpperName() << "()->getName() << \"";
178 } else if (type == "QualType") {
179 OS << "\" << get" << getUpperName() << "().getAsString() << \"";
180 } else if (type == "SourceLocation") {
181 OS << "\" << get" << getUpperName() << "().getRawEncoding() << \"";
182 } else {
183 OS << "\" << get" << getUpperName() << "() << \"";
184 }
185 }
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000186 void writeDump(raw_ostream &OS) const {
187 if (type == "FunctionDecl *") {
188 OS << " OS << \" \";\n";
189 OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n";
190 } else if (type == "IdentifierInfo *") {
191 OS << " OS << \" \" << SA->get" << getUpperName()
192 << "()->getName();\n";
193 } else if (type == "QualType") {
194 OS << " OS << \" \" << SA->get" << getUpperName()
195 << "().getAsString();\n";
196 } else if (type == "SourceLocation") {
197 OS << " OS << \" \";\n";
198 OS << " SA->get" << getUpperName() << "().print(OS, *SM);\n";
199 } else if (type == "bool") {
200 OS << " if (SA->get" << getUpperName() << "()) OS << \" "
201 << getUpperName() << "\";\n";
202 } else if (type == "int" || type == "unsigned") {
203 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
204 } else {
205 llvm_unreachable("Unknown SimpleArgument type!");
206 }
207 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000208 };
209
210 class StringArgument : public Argument {
211 public:
212 StringArgument(Record &Arg, StringRef Attr)
213 : Argument(Arg, Attr)
214 {}
215
216 void writeAccessors(raw_ostream &OS) const {
217 OS << " llvm::StringRef get" << getUpperName() << "() const {\n";
218 OS << " return llvm::StringRef(" << getLowerName() << ", "
219 << getLowerName() << "Length);\n";
220 OS << " }\n";
221 OS << " unsigned get" << getUpperName() << "Length() const {\n";
222 OS << " return " << getLowerName() << "Length;\n";
223 OS << " }\n";
224 OS << " void set" << getUpperName()
225 << "(ASTContext &C, llvm::StringRef S) {\n";
226 OS << " " << getLowerName() << "Length = S.size();\n";
227 OS << " this->" << getLowerName() << " = new (C, 1) char ["
228 << getLowerName() << "Length];\n";
229 OS << " std::memcpy(this->" << getLowerName() << ", S.data(), "
230 << getLowerName() << "Length);\n";
231 OS << " }";
232 }
233 void writeCloneArgs(raw_ostream &OS) const {
234 OS << "get" << getUpperName() << "()";
235 }
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000236 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
237 OS << "A->get" << getUpperName() << "()";
238 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000239 void writeCtorBody(raw_ostream &OS) const {
240 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
241 << ".data(), " << getLowerName() << "Length);";
242 }
243 void writeCtorInitializers(raw_ostream &OS) const {
244 OS << getLowerName() << "Length(" << getUpperName() << ".size()),"
245 << getLowerName() << "(new (Ctx, 1) char[" << getLowerName()
246 << "Length])";
247 }
248 void writeCtorParameters(raw_ostream &OS) const {
249 OS << "llvm::StringRef " << getUpperName();
250 }
251 void writeDeclarations(raw_ostream &OS) const {
252 OS << "unsigned " << getLowerName() << "Length;\n";
253 OS << "char *" << getLowerName() << ";";
254 }
255 void writePCHReadDecls(raw_ostream &OS) const {
256 OS << " std::string " << getLowerName()
257 << "= ReadString(Record, Idx);\n";
258 }
259 void writePCHReadArgs(raw_ostream &OS) const {
260 OS << getLowerName();
261 }
262 void writePCHWrite(raw_ostream &OS) const {
263 OS << " AddString(SA->get" << getUpperName() << "(), Record);\n";
264 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000265 void writeValue(raw_ostream &OS) const {
266 OS << "\\\"\" << get" << getUpperName() << "() << \"\\\"";
267 }
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000268 void writeDump(raw_ostream &OS) const {
269 OS << " OS << \" \\\"\" << SA->get" << getUpperName()
270 << "() << \"\\\"\";\n";
271 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000272 };
273
274 class AlignedArgument : public Argument {
275 public:
276 AlignedArgument(Record &Arg, StringRef Attr)
277 : Argument(Arg, Attr)
278 {}
279
280 void writeAccessors(raw_ostream &OS) const {
281 OS << " bool is" << getUpperName() << "Dependent() const;\n";
282
283 OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n";
284
285 OS << " bool is" << getUpperName() << "Expr() const {\n";
286 OS << " return is" << getLowerName() << "Expr;\n";
287 OS << " }\n";
288
289 OS << " Expr *get" << getUpperName() << "Expr() const {\n";
290 OS << " assert(is" << getLowerName() << "Expr);\n";
291 OS << " return " << getLowerName() << "Expr;\n";
292 OS << " }\n";
293
294 OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n";
295 OS << " assert(!is" << getLowerName() << "Expr);\n";
296 OS << " return " << getLowerName() << "Type;\n";
297 OS << " }";
298 }
299 void writeAccessorDefinitions(raw_ostream &OS) const {
300 OS << "bool " << getAttrName() << "Attr::is" << getUpperName()
301 << "Dependent() const {\n";
302 OS << " if (is" << getLowerName() << "Expr)\n";
303 OS << " return " << getLowerName() << "Expr && (" << getLowerName()
304 << "Expr->isValueDependent() || " << getLowerName()
305 << "Expr->isTypeDependent());\n";
306 OS << " else\n";
307 OS << " return " << getLowerName()
308 << "Type->getType()->isDependentType();\n";
309 OS << "}\n";
310
311 // FIXME: Do not do the calculation here
312 // FIXME: Handle types correctly
313 // A null pointer means maximum alignment
314 // FIXME: Load the platform-specific maximum alignment, rather than
315 // 16, the x86 max.
316 OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
317 << "(ASTContext &Ctx) const {\n";
318 OS << " assert(!is" << getUpperName() << "Dependent());\n";
319 OS << " if (is" << getLowerName() << "Expr)\n";
320 OS << " return (" << getLowerName() << "Expr ? " << getLowerName()
Richard Smitha6b8b2c2011-10-10 18:28:20 +0000321 << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue() : 16)"
Peter Collingbourne51d77772011-10-06 13:03:08 +0000322 << "* Ctx.getCharWidth();\n";
323 OS << " else\n";
324 OS << " return 0; // FIXME\n";
325 OS << "}\n";
326 }
327 void writeCloneArgs(raw_ostream &OS) const {
328 OS << "is" << getLowerName() << "Expr, is" << getLowerName()
329 << "Expr ? static_cast<void*>(" << getLowerName()
330 << "Expr) : " << getLowerName()
331 << "Type";
332 }
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000333 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
334 // FIXME: move the definition in Sema::InstantiateAttrs to here.
335 // In the meantime, aligned attributes are cloned.
336 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000337 void writeCtorBody(raw_ostream &OS) const {
338 OS << " if (is" << getLowerName() << "Expr)\n";
339 OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>("
340 << getUpperName() << ");\n";
341 OS << " else\n";
342 OS << " " << getLowerName()
343 << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName()
344 << ");";
345 }
346 void writeCtorInitializers(raw_ostream &OS) const {
347 OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)";
348 }
349 void writeCtorParameters(raw_ostream &OS) const {
350 OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName();
351 }
352 void writeDeclarations(raw_ostream &OS) const {
353 OS << "bool is" << getLowerName() << "Expr;\n";
354 OS << "union {\n";
355 OS << "Expr *" << getLowerName() << "Expr;\n";
356 OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
357 OS << "};";
358 }
359 void writePCHReadArgs(raw_ostream &OS) const {
360 OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr";
361 }
362 void writePCHReadDecls(raw_ostream &OS) const {
363 OS << " bool is" << getLowerName() << "Expr = Record[Idx++];\n";
364 OS << " void *" << getLowerName() << "Ptr;\n";
365 OS << " if (is" << getLowerName() << "Expr)\n";
366 OS << " " << getLowerName() << "Ptr = ReadExpr(F);\n";
367 OS << " else\n";
368 OS << " " << getLowerName()
369 << "Ptr = GetTypeSourceInfo(F, Record, Idx);\n";
370 }
371 void writePCHWrite(raw_ostream &OS) const {
372 OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n";
373 OS << " if (SA->is" << getUpperName() << "Expr())\n";
374 OS << " AddStmt(SA->get" << getUpperName() << "Expr());\n";
375 OS << " else\n";
376 OS << " AddTypeSourceInfo(SA->get" << getUpperName()
377 << "Type(), Record);\n";
378 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000379 void writeValue(raw_ostream &OS) const {
Richard Smith0dae7292012-08-16 02:43:29 +0000380 OS << "\";\n"
381 << " " << getLowerName() << "Expr->printPretty(OS, 0, Policy);\n"
382 << " OS << \"";
Douglas Gregor1bea8802011-11-19 19:22:57 +0000383 }
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000384 void writeDump(raw_ostream &OS) const {
385 }
386 void writeDumpChildren(raw_ostream &OS) const {
387 OS << " if (SA->is" << getUpperName() << "Expr())\n";
388 OS << " dumpStmt(SA->get" << getUpperName() << "Expr());\n";
389 OS << " else\n";
390 OS << " dumpType(SA->get" << getUpperName()
391 << "Type()->getType());\n";
392 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000393 };
394
395 class VariadicArgument : public Argument {
396 std::string type;
397
398 public:
399 VariadicArgument(Record &Arg, StringRef Attr, std::string T)
400 : Argument(Arg, Attr), type(T)
401 {}
402
403 std::string getType() const { return type; }
404
405 void writeAccessors(raw_ostream &OS) const {
406 OS << " typedef " << type << "* " << getLowerName() << "_iterator;\n";
407 OS << " " << getLowerName() << "_iterator " << getLowerName()
408 << "_begin() const {\n";
409 OS << " return " << getLowerName() << ";\n";
410 OS << " }\n";
411 OS << " " << getLowerName() << "_iterator " << getLowerName()
412 << "_end() const {\n";
413 OS << " return " << getLowerName() << " + " << getLowerName()
414 << "Size;\n";
415 OS << " }\n";
416 OS << " unsigned " << getLowerName() << "_size() const {\n"
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000417 << " return " << getLowerName() << "Size;\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000418 OS << " }";
419 }
420 void writeCloneArgs(raw_ostream &OS) const {
421 OS << getLowerName() << ", " << getLowerName() << "Size";
422 }
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000423 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
424 // This isn't elegant, but we have to go through public methods...
425 OS << "A->" << getLowerName() << "_begin(), "
426 << "A->" << getLowerName() << "_size()";
427 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000428 void writeCtorBody(raw_ostream &OS) const {
429 // FIXME: memcpy is not safe on non-trivial types.
430 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
431 << ", " << getLowerName() << "Size * sizeof(" << getType() << "));\n";
432 }
433 void writeCtorInitializers(raw_ostream &OS) const {
434 OS << getLowerName() << "Size(" << getUpperName() << "Size), "
435 << getLowerName() << "(new (Ctx, 16) " << getType() << "["
436 << getLowerName() << "Size])";
437 }
438 void writeCtorParameters(raw_ostream &OS) const {
439 OS << getType() << " *" << getUpperName() << ", unsigned "
440 << getUpperName() << "Size";
441 }
442 void writeDeclarations(raw_ostream &OS) const {
443 OS << " unsigned " << getLowerName() << "Size;\n";
444 OS << " " << getType() << " *" << getLowerName() << ";";
445 }
446 void writePCHReadDecls(raw_ostream &OS) const {
447 OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
448 OS << " llvm::SmallVector<" << type << ", 4> " << getLowerName()
449 << ";\n";
450 OS << " " << getLowerName() << ".reserve(" << getLowerName()
451 << "Size);\n";
452 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
453
454 std::string read = ReadPCHRecord(type);
455 OS << " " << getLowerName() << ".push_back(" << read << ");\n";
456 }
457 void writePCHReadArgs(raw_ostream &OS) const {
458 OS << getLowerName() << ".data(), " << getLowerName() << "Size";
459 }
460 void writePCHWrite(raw_ostream &OS) const{
461 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
462 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
463 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
464 << getLowerName() << "_end(); i != e; ++i)\n";
465 OS << " " << WritePCHRecord(type, "(*i)");
466 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000467 void writeValue(raw_ostream &OS) const {
468 OS << "\";\n";
469 OS << " bool isFirst = true;\n"
470 << " for (" << getAttrName() << "Attr::" << getLowerName()
471 << "_iterator i = " << getLowerName() << "_begin(), e = "
472 << getLowerName() << "_end(); i != e; ++i) {\n"
473 << " if (isFirst) isFirst = false;\n"
474 << " else OS << \", \";\n"
475 << " OS << *i;\n"
476 << " }\n";
477 OS << " OS << \"";
478 }
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000479 void writeDump(raw_ostream &OS) const {
480 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
481 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
482 << getLowerName() << "_end(); I != E; ++I)\n";
483 OS << " OS << \" \" << *I;\n";
484 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000485 };
486
487 class EnumArgument : public Argument {
488 std::string type;
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000489 std::vector<StringRef> values, enums, uniques;
Peter Collingbourne51d77772011-10-06 13:03:08 +0000490 public:
491 EnumArgument(Record &Arg, StringRef Attr)
492 : Argument(Arg, Attr), type(Arg.getValueAsString("Type")),
493 values(getValueAsListOfStrings(Arg, "Values")),
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000494 enums(getValueAsListOfStrings(Arg, "Enums")),
495 uniques(enums)
496 {
497 // Calculate the various enum values
498 std::sort(uniques.begin(), uniques.end());
499 uniques.erase(std::unique(uniques.begin(), uniques.end()), uniques.end());
500 // FIXME: Emit a proper error
501 assert(!uniques.empty());
502 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000503
504 void writeAccessors(raw_ostream &OS) const {
505 OS << " " << type << " get" << getUpperName() << "() const {\n";
506 OS << " return " << getLowerName() << ";\n";
507 OS << " }";
508 }
509 void writeCloneArgs(raw_ostream &OS) const {
510 OS << getLowerName();
511 }
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000512 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
513 OS << "A->get" << getUpperName() << "()";
514 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000515 void writeCtorInitializers(raw_ostream &OS) const {
516 OS << getLowerName() << "(" << getUpperName() << ")";
517 }
518 void writeCtorParameters(raw_ostream &OS) const {
519 OS << type << " " << getUpperName();
520 }
521 void writeDeclarations(raw_ostream &OS) const {
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000522 std::vector<StringRef>::const_iterator i = uniques.begin(),
523 e = uniques.end();
Peter Collingbourne51d77772011-10-06 13:03:08 +0000524 // The last one needs to not have a comma.
525 --e;
526
527 OS << "public:\n";
528 OS << " enum " << type << " {\n";
529 for (; i != e; ++i)
530 OS << " " << *i << ",\n";
531 OS << " " << *e << "\n";
532 OS << " };\n";
533 OS << "private:\n";
534 OS << " " << type << " " << getLowerName() << ";";
535 }
536 void writePCHReadDecls(raw_ostream &OS) const {
537 OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName()
538 << "(static_cast<" << getAttrName() << "Attr::" << type
539 << ">(Record[Idx++]));\n";
540 }
541 void writePCHReadArgs(raw_ostream &OS) const {
542 OS << getLowerName();
543 }
544 void writePCHWrite(raw_ostream &OS) const {
545 OS << "Record.push_back(SA->get" << getUpperName() << "());\n";
546 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000547 void writeValue(raw_ostream &OS) const {
548 OS << "\" << get" << getUpperName() << "() << \"";
549 }
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000550 void writeDump(raw_ostream &OS) const {
551 OS << " switch(SA->get" << getUpperName() << "()) {\n";
552 OS << " default:\n";
553 OS << " llvm_unreachable(\"Unknown " << getAttrName() << "Attr::"
554 << type << "!\");\n";
555 OS << " break;\n";
556
557 for (std::vector<StringRef>::const_iterator I = uniques.begin(),
558 E = uniques.end(); I != E; ++I) {
559 OS << " case " << getAttrName() << "Attr::" << *I << ":\n";
560 OS << " OS << \" " << *I << "\";\n";
561 OS << " break;\n";
562 }
563 OS << " }\n";
564 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000565 };
566
567 class VersionArgument : public Argument {
568 public:
569 VersionArgument(Record &Arg, StringRef Attr)
570 : Argument(Arg, Attr)
571 {}
572
573 void writeAccessors(raw_ostream &OS) const {
574 OS << " VersionTuple get" << getUpperName() << "() const {\n";
575 OS << " return " << getLowerName() << ";\n";
576 OS << " }\n";
577 OS << " void set" << getUpperName()
578 << "(ASTContext &C, VersionTuple V) {\n";
579 OS << " " << getLowerName() << " = V;\n";
580 OS << " }";
581 }
582 void writeCloneArgs(raw_ostream &OS) const {
583 OS << "get" << getUpperName() << "()";
584 }
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000585 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
586 OS << "A->get" << getUpperName() << "()";
587 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000588 void writeCtorBody(raw_ostream &OS) const {
589 }
590 void writeCtorInitializers(raw_ostream &OS) const {
591 OS << getLowerName() << "(" << getUpperName() << ")";
592 }
593 void writeCtorParameters(raw_ostream &OS) const {
594 OS << "VersionTuple " << getUpperName();
595 }
596 void writeDeclarations(raw_ostream &OS) const {
597 OS << "VersionTuple " << getLowerName() << ";\n";
598 }
599 void writePCHReadDecls(raw_ostream &OS) const {
600 OS << " VersionTuple " << getLowerName()
601 << "= ReadVersionTuple(Record, Idx);\n";
602 }
603 void writePCHReadArgs(raw_ostream &OS) const {
604 OS << getLowerName();
605 }
606 void writePCHWrite(raw_ostream &OS) const {
607 OS << " AddVersionTuple(SA->get" << getUpperName() << "(), Record);\n";
608 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000609 void writeValue(raw_ostream &OS) const {
610 OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";
611 }
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000612 void writeDump(raw_ostream &OS) const {
613 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
614 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000615 };
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000616
617 class ExprArgument : public SimpleArgument {
618 public:
619 ExprArgument(Record &Arg, StringRef Attr)
620 : SimpleArgument(Arg, Attr, "Expr *")
621 {}
622
623 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
624 OS << "tempInst" << getUpperName();
625 }
626
627 void writeTemplateInstantiation(raw_ostream &OS) const {
628 OS << " " << getType() << " tempInst" << getUpperName() << ";\n";
629 OS << " {\n";
630 OS << " EnterExpressionEvaluationContext "
631 << "Unevaluated(S, Sema::Unevaluated);\n";
632 OS << " ExprResult " << "Result = S.SubstExpr("
633 << "A->get" << getUpperName() << "(), TemplateArgs);\n";
634 OS << " tempInst" << getUpperName() << " = "
635 << "Result.takeAs<Expr>();\n";
636 OS << " }\n";
637 }
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000638
639 void writeDump(raw_ostream &OS) const {
640 }
641
642 void writeDumpChildren(raw_ostream &OS) const {
643 OS << " dumpStmt(SA->get" << getUpperName() << "());\n";
644 }
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000645 };
646
647 class VariadicExprArgument : public VariadicArgument {
648 public:
649 VariadicExprArgument(Record &Arg, StringRef Attr)
650 : VariadicArgument(Arg, Attr, "Expr *")
651 {}
652
653 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
654 OS << "tempInst" << getUpperName() << ", "
655 << "A->" << getLowerName() << "_size()";
656 }
657
658 void writeTemplateInstantiation(raw_ostream &OS) const {
659 OS << " " << getType() << " *tempInst" << getUpperName()
660 << " = new (C, 16) " << getType()
661 << "[A->" << getLowerName() << "_size()];\n";
662 OS << " {\n";
663 OS << " EnterExpressionEvaluationContext "
664 << "Unevaluated(S, Sema::Unevaluated);\n";
665 OS << " " << getType() << " *TI = tempInst" << getUpperName()
666 << ";\n";
667 OS << " " << getType() << " *I = A->" << getLowerName()
668 << "_begin();\n";
669 OS << " " << getType() << " *E = A->" << getLowerName()
670 << "_end();\n";
671 OS << " for (; I != E; ++I, ++TI) {\n";
672 OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n";
673 OS << " *TI = Result.takeAs<Expr>();\n";
674 OS << " }\n";
675 OS << " }\n";
676 }
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000677
678 void writeDump(raw_ostream &OS) const {
679 }
680
681 void writeDumpChildren(raw_ostream &OS) const {
682 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
683 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
684 << getLowerName() << "_end(); I != E; ++I)\n";
685 OS << " dumpStmt(*I);\n";
686 }
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000687 };
Peter Collingbourne51d77772011-10-06 13:03:08 +0000688}
689
690static Argument *createArgument(Record &Arg, StringRef Attr,
691 Record *Search = 0) {
692 if (!Search)
693 Search = &Arg;
694
695 Argument *Ptr = 0;
696 llvm::StringRef ArgName = Search->getName();
697
698 if (ArgName == "AlignedArgument") Ptr = new AlignedArgument(Arg, Attr);
699 else if (ArgName == "EnumArgument") Ptr = new EnumArgument(Arg, Attr);
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000700 else if (ArgName == "ExprArgument") Ptr = new ExprArgument(Arg, Attr);
Peter Collingbourne51d77772011-10-06 13:03:08 +0000701 else if (ArgName == "FunctionArgument")
702 Ptr = new SimpleArgument(Arg, Attr, "FunctionDecl *");
703 else if (ArgName == "IdentifierArgument")
704 Ptr = new SimpleArgument(Arg, Attr, "IdentifierInfo *");
705 else if (ArgName == "BoolArgument") Ptr = new SimpleArgument(Arg, Attr,
706 "bool");
707 else if (ArgName == "IntArgument") Ptr = new SimpleArgument(Arg, Attr, "int");
708 else if (ArgName == "StringArgument") Ptr = new StringArgument(Arg, Attr);
709 else if (ArgName == "TypeArgument")
710 Ptr = new SimpleArgument(Arg, Attr, "QualType");
711 else if (ArgName == "UnsignedArgument")
712 Ptr = new SimpleArgument(Arg, Attr, "unsigned");
713 else if (ArgName == "SourceLocArgument")
714 Ptr = new SimpleArgument(Arg, Attr, "SourceLocation");
715 else if (ArgName == "VariadicUnsignedArgument")
716 Ptr = new VariadicArgument(Arg, Attr, "unsigned");
717 else if (ArgName == "VariadicExprArgument")
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000718 Ptr = new VariadicExprArgument(Arg, Attr);
Peter Collingbourne51d77772011-10-06 13:03:08 +0000719 else if (ArgName == "VersionArgument")
720 Ptr = new VersionArgument(Arg, Attr);
721
722 if (!Ptr) {
723 std::vector<Record*> Bases = Search->getSuperClasses();
724 for (std::vector<Record*>::iterator i = Bases.begin(), e = Bases.end();
725 i != e; ++i) {
726 Ptr = createArgument(Arg, Attr, *i);
727 if (Ptr)
728 break;
729 }
730 }
731 return Ptr;
732}
733
Douglas Gregor1bea8802011-11-19 19:22:57 +0000734static void writeAvailabilityValue(raw_ostream &OS) {
735 OS << "\" << getPlatform()->getName();\n"
736 << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n"
737 << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n"
738 << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n"
739 << " if (getUnavailable()) OS << \", unavailable\";\n"
740 << " OS << \"";
741}
742
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000743namespace clang {
744
745// Emits the class definitions for attributes.
746void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000747 OS << "// This file is generated by TableGen. Do not edit.\n\n";
748 OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
749 OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
750
751 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
752
753 for (std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end();
754 i != e; ++i) {
755 Record &R = **i;
Douglas Gregor3e7d31a2012-05-02 15:56:52 +0000756
757 if (!R.getValueAsBit("ASTNode"))
758 continue;
759
Peter Collingbourne51d77772011-10-06 13:03:08 +0000760 const std::string &SuperName = R.getSuperClasses().back()->getName();
761
762 OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
763
764 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
765 std::vector<Argument*> Args;
766 std::vector<Argument*>::iterator ai, ae;
767 Args.reserve(ArgRecords.size());
768
769 for (std::vector<Record*>::iterator ri = ArgRecords.begin(),
770 re = ArgRecords.end();
771 ri != re; ++ri) {
772 Record &ArgRecord = **ri;
773 Argument *Arg = createArgument(ArgRecord, R.getName());
774 assert(Arg);
775 Args.push_back(Arg);
776
777 Arg->writeDeclarations(OS);
778 OS << "\n\n";
779 }
780
781 ae = Args.end();
782
783 OS << "\n public:\n";
784 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
785
786 for (ai = Args.begin(); ai != ae; ++ai) {
787 OS << " , ";
788 (*ai)->writeCtorParameters(OS);
789 OS << "\n";
790 }
791
792 OS << " )\n";
793 OS << " : " << SuperName << "(attr::" << R.getName() << ", R)\n";
794
795 for (ai = Args.begin(); ai != ae; ++ai) {
796 OS << " , ";
797 (*ai)->writeCtorInitializers(OS);
798 OS << "\n";
799 }
800
801 OS << " {\n";
802
803 for (ai = Args.begin(); ai != ae; ++ai) {
804 (*ai)->writeCtorBody(OS);
805 OS << "\n";
806 }
807 OS << " }\n\n";
808
809 OS << " virtual " << R.getName() << "Attr *clone (ASTContext &C) const;\n";
Richard Smith0dae7292012-08-16 02:43:29 +0000810 OS << " virtual void printPretty(llvm::raw_ostream &OS,"
811 << " const PrintingPolicy &Policy) const;\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000812
813 for (ai = Args.begin(); ai != ae; ++ai) {
814 (*ai)->writeAccessors(OS);
815 OS << "\n\n";
816 }
817
Jakob Stoklund Oleseneb666732012-01-13 04:57:47 +0000818 OS << R.getValueAsString("AdditionalMembers");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000819 OS << "\n\n";
820
821 OS << " static bool classof(const Attr *A) { return A->getKind() == "
822 << "attr::" << R.getName() << "; }\n";
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000823
824 bool LateParsed = R.getValueAsBit("LateParsed");
825 OS << " virtual bool isLateParsed() const { return "
826 << LateParsed << "; }\n";
827
Peter Collingbourne51d77772011-10-06 13:03:08 +0000828 OS << "};\n\n";
829 }
830
831 OS << "#endif\n";
832}
833
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000834// Emits the class method definitions for attributes.
835void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000836 OS << "// This file is generated by TableGen. Do not edit.\n\n";
837
838 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
839 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ri, re;
840 std::vector<Argument*>::iterator ai, ae;
841
842 for (; i != e; ++i) {
843 Record &R = **i;
Douglas Gregor3e7d31a2012-05-02 15:56:52 +0000844
845 if (!R.getValueAsBit("ASTNode"))
846 continue;
847
Peter Collingbourne51d77772011-10-06 13:03:08 +0000848 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Sean Hunt8e083e72012-06-19 23:57:03 +0000849 std::vector<Record*> Spellings = R.getValueAsListOfDefs("Spellings");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000850 std::vector<Argument*> Args;
851 for (ri = ArgRecords.begin(), re = ArgRecords.end(); ri != re; ++ri)
852 Args.push_back(createArgument(**ri, R.getName()));
853
854 for (ai = Args.begin(), ae = Args.end(); ai != ae; ++ai)
855 (*ai)->writeAccessorDefinitions(OS);
856
857 OS << R.getName() << "Attr *" << R.getName()
858 << "Attr::clone(ASTContext &C) const {\n";
859 OS << " return new (C) " << R.getName() << "Attr(getLocation(), C";
860 for (ai = Args.begin(); ai != ae; ++ai) {
861 OS << ", ";
862 (*ai)->writeCloneArgs(OS);
863 }
864 OS << ");\n}\n\n";
Douglas Gregor1bea8802011-11-19 19:22:57 +0000865
866 OS << "void " << R.getName() << "Attr::printPretty("
Richard Smith0dae7292012-08-16 02:43:29 +0000867 << "llvm::raw_ostream &OS, const PrintingPolicy &Policy) const {\n";
Douglas Gregor1bea8802011-11-19 19:22:57 +0000868 if (Spellings.begin() != Spellings.end()) {
NAKAMURA Takumi41de13b2012-07-03 14:45:09 +0000869 std::string Spelling = (*Spellings.begin())->getValueAsString("Name");
Sean Hunt8e083e72012-06-19 23:57:03 +0000870 OS << " OS << \" __attribute__((" << Spelling;
Douglas Gregor1bea8802011-11-19 19:22:57 +0000871 if (Args.size()) OS << "(";
Sean Hunt8e083e72012-06-19 23:57:03 +0000872 if (Spelling == "availability") {
Douglas Gregor1bea8802011-11-19 19:22:57 +0000873 writeAvailabilityValue(OS);
874 } else {
875 for (ai = Args.begin(); ai != ae; ++ai) {
876 if (ai!=Args.begin()) OS <<", ";
877 (*ai)->writeValue(OS);
878 }
879 }
880 if (Args.size()) OS << ")";
881 OS << "))\";\n";
882 }
883 OS << "}\n\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000884 }
885}
886
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000887} // end namespace clang
888
Peter Collingbourne51d77772011-10-06 13:03:08 +0000889static void EmitAttrList(raw_ostream &OS, StringRef Class,
890 const std::vector<Record*> &AttrList) {
891 std::vector<Record*>::const_iterator i = AttrList.begin(), e = AttrList.end();
892
893 if (i != e) {
894 // Move the end iterator back to emit the last attribute.
Douglas Gregor3e7d31a2012-05-02 15:56:52 +0000895 for(--e; i != e; ++i) {
896 if (!(*i)->getValueAsBit("ASTNode"))
897 continue;
898
Peter Collingbourne51d77772011-10-06 13:03:08 +0000899 OS << Class << "(" << (*i)->getName() << ")\n";
Douglas Gregor3e7d31a2012-05-02 15:56:52 +0000900 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000901
902 OS << "LAST_" << Class << "(" << (*i)->getName() << ")\n\n";
903 }
904}
905
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000906namespace clang {
907
908// Emits the enumeration list for attributes.
909void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000910 OS << "// This file is generated by TableGen. Do not edit.\n\n";
911
912 OS << "#ifndef LAST_ATTR\n";
913 OS << "#define LAST_ATTR(NAME) ATTR(NAME)\n";
914 OS << "#endif\n\n";
915
916 OS << "#ifndef INHERITABLE_ATTR\n";
917 OS << "#define INHERITABLE_ATTR(NAME) ATTR(NAME)\n";
918 OS << "#endif\n\n";
919
920 OS << "#ifndef LAST_INHERITABLE_ATTR\n";
921 OS << "#define LAST_INHERITABLE_ATTR(NAME) INHERITABLE_ATTR(NAME)\n";
922 OS << "#endif\n\n";
923
924 OS << "#ifndef INHERITABLE_PARAM_ATTR\n";
925 OS << "#define INHERITABLE_PARAM_ATTR(NAME) ATTR(NAME)\n";
926 OS << "#endif\n\n";
927
928 OS << "#ifndef LAST_INHERITABLE_PARAM_ATTR\n";
929 OS << "#define LAST_INHERITABLE_PARAM_ATTR(NAME)"
930 " INHERITABLE_PARAM_ATTR(NAME)\n";
931 OS << "#endif\n\n";
932
933 Record *InhClass = Records.getClass("InheritableAttr");
934 Record *InhParamClass = Records.getClass("InheritableParamAttr");
935 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
936 NonInhAttrs, InhAttrs, InhParamAttrs;
937 for (std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end();
938 i != e; ++i) {
Douglas Gregor3e7d31a2012-05-02 15:56:52 +0000939 if (!(*i)->getValueAsBit("ASTNode"))
940 continue;
941
Peter Collingbourne51d77772011-10-06 13:03:08 +0000942 if ((*i)->isSubClassOf(InhParamClass))
943 InhParamAttrs.push_back(*i);
944 else if ((*i)->isSubClassOf(InhClass))
945 InhAttrs.push_back(*i);
946 else
947 NonInhAttrs.push_back(*i);
948 }
949
950 EmitAttrList(OS, "INHERITABLE_PARAM_ATTR", InhParamAttrs);
951 EmitAttrList(OS, "INHERITABLE_ATTR", InhAttrs);
952 EmitAttrList(OS, "ATTR", NonInhAttrs);
953
954 OS << "#undef LAST_ATTR\n";
955 OS << "#undef INHERITABLE_ATTR\n";
956 OS << "#undef LAST_INHERITABLE_ATTR\n";
957 OS << "#undef LAST_INHERITABLE_PARAM_ATTR\n";
958 OS << "#undef ATTR\n";
959}
960
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000961// Emits the code to read an attribute from a precompiled header.
962void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000963 OS << "// This file is generated by TableGen. Do not edit.\n\n";
964
965 Record *InhClass = Records.getClass("InheritableAttr");
966 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
967 ArgRecords;
968 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ai, ae;
969 std::vector<Argument*> Args;
970 std::vector<Argument*>::iterator ri, re;
971
972 OS << " switch (Kind) {\n";
973 OS << " default:\n";
974 OS << " assert(0 && \"Unknown attribute!\");\n";
975 OS << " break;\n";
976 for (; i != e; ++i) {
977 Record &R = **i;
Douglas Gregor3e7d31a2012-05-02 15:56:52 +0000978 if (!R.getValueAsBit("ASTNode"))
979 continue;
980
Peter Collingbourne51d77772011-10-06 13:03:08 +0000981 OS << " case attr::" << R.getName() << ": {\n";
982 if (R.isSubClassOf(InhClass))
983 OS << " bool isInherited = Record[Idx++];\n";
984 ArgRecords = R.getValueAsListOfDefs("Args");
985 Args.clear();
986 for (ai = ArgRecords.begin(), ae = ArgRecords.end(); ai != ae; ++ai) {
987 Argument *A = createArgument(**ai, R.getName());
988 Args.push_back(A);
989 A->writePCHReadDecls(OS);
990 }
991 OS << " New = new (Context) " << R.getName() << "Attr(Range, Context";
992 for (ri = Args.begin(), re = Args.end(); ri != re; ++ri) {
993 OS << ", ";
994 (*ri)->writePCHReadArgs(OS);
995 }
996 OS << ");\n";
997 if (R.isSubClassOf(InhClass))
998 OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
999 OS << " break;\n";
1000 OS << " }\n";
1001 }
1002 OS << " }\n";
1003}
1004
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00001005// Emits the code to write an attribute to a precompiled header.
1006void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +00001007 Record *InhClass = Records.getClass("InheritableAttr");
1008 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
1009 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ai, ae;
1010
1011 OS << " switch (A->getKind()) {\n";
1012 OS << " default:\n";
1013 OS << " llvm_unreachable(\"Unknown attribute kind!\");\n";
1014 OS << " break;\n";
1015 for (; i != e; ++i) {
1016 Record &R = **i;
Douglas Gregor3e7d31a2012-05-02 15:56:52 +00001017 if (!R.getValueAsBit("ASTNode"))
1018 continue;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001019 OS << " case attr::" << R.getName() << ": {\n";
1020 Args = R.getValueAsListOfDefs("Args");
1021 if (R.isSubClassOf(InhClass) || !Args.empty())
1022 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
1023 << "Attr>(A);\n";
1024 if (R.isSubClassOf(InhClass))
1025 OS << " Record.push_back(SA->isInherited());\n";
1026 for (ai = Args.begin(), ae = Args.end(); ai != ae; ++ai)
1027 createArgument(**ai, R.getName())->writePCHWrite(OS);
1028 OS << " break;\n";
1029 OS << " }\n";
1030 }
1031 OS << " }\n";
1032}
1033
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00001034// Emits the list of spellings for attributes.
1035void EmitClangAttrSpellingList(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +00001036 OS << "// This file is generated by TableGen. Do not edit.\n\n";
1037
1038 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1039
1040 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
1041 Record &Attr = **I;
1042
Sean Hunt8e083e72012-06-19 23:57:03 +00001043 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001044
Sean Hunt8e083e72012-06-19 23:57:03 +00001045 for (std::vector<Record*>::const_iterator I = Spellings.begin(), E = Spellings.end(); I != E; ++I) {
1046 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", true)\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001047 }
1048 }
1049
1050}
1051
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00001052// Emits the LateParsed property for attributes.
1053void EmitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +00001054 OS << "// This file is generated by TableGen. Do not edit.\n\n";
1055
1056 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1057
1058 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1059 I != E; ++I) {
1060 Record &Attr = **I;
1061
1062 bool LateParsed = Attr.getValueAsBit("LateParsed");
1063
1064 if (LateParsed) {
Sean Hunt8e083e72012-06-19 23:57:03 +00001065 std::vector<Record*> Spellings =
1066 Attr.getValueAsListOfDefs("Spellings");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001067
Sean Hunt8e083e72012-06-19 23:57:03 +00001068 // FIXME: Handle non-GNU attributes
1069 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
Peter Collingbourne51d77772011-10-06 13:03:08 +00001070 E = Spellings.end(); I != E; ++I) {
Sean Hunt8e083e72012-06-19 23:57:03 +00001071 if ((*I)->getValueAsString("Variety") != "GNU")
1072 continue;
1073 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", "
1074 << LateParsed << ")\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001075 }
1076 }
1077 }
1078}
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +00001079
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00001080// Emits code to instantiate dependent attributes on templates.
1081void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) {
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +00001082 OS << "// This file is generated by TableGen. Do not edit.\n\n";
1083
1084 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1085
Benjamin Kramer5bbc3852012-02-06 11:13:08 +00001086 OS << "namespace clang {\n"
1087 << "namespace sema {\n\n"
1088 << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, "
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +00001089 << "Sema &S,\n"
1090 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"
1091 << " switch (At->getKind()) {\n"
1092 << " default:\n"
1093 << " break;\n";
1094
1095 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1096 I != E; ++I) {
1097 Record &R = **I;
Douglas Gregor3e7d31a2012-05-02 15:56:52 +00001098 if (!R.getValueAsBit("ASTNode"))
1099 continue;
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +00001100
1101 OS << " case attr::" << R.getName() << ": {\n";
Rafael Espindola31c195a2012-05-15 14:09:55 +00001102 bool ShouldClone = R.getValueAsBit("Clone");
1103
1104 if (!ShouldClone) {
1105 OS << " return NULL;\n";
1106 OS << " }\n";
1107 continue;
1108 }
1109
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +00001110 OS << " const " << R.getName() << "Attr *A = cast<"
1111 << R.getName() << "Attr>(At);\n";
1112 bool TDependent = R.getValueAsBit("TemplateDependent");
1113
1114 if (!TDependent) {
1115 OS << " return A->clone(C);\n";
1116 OS << " }\n";
1117 continue;
1118 }
1119
1120 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1121 std::vector<Argument*> Args;
1122 std::vector<Argument*>::iterator ai, ae;
1123 Args.reserve(ArgRecords.size());
1124
1125 for (std::vector<Record*>::iterator ri = ArgRecords.begin(),
1126 re = ArgRecords.end();
1127 ri != re; ++ri) {
1128 Record &ArgRecord = **ri;
1129 Argument *Arg = createArgument(ArgRecord, R.getName());
1130 assert(Arg);
1131 Args.push_back(Arg);
1132 }
1133 ae = Args.end();
1134
1135 for (ai = Args.begin(); ai != ae; ++ai) {
1136 (*ai)->writeTemplateInstantiation(OS);
1137 }
1138 OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C";
1139 for (ai = Args.begin(); ai != ae; ++ai) {
1140 OS << ", ";
1141 (*ai)->writeTemplateInstantiationArgs(OS);
1142 }
1143 OS << ");\n }\n";
1144 }
1145 OS << " } // end switch\n"
1146 << " llvm_unreachable(\"Unknown attribute!\");\n"
1147 << " return 0;\n"
Benjamin Kramer5bbc3852012-02-06 11:13:08 +00001148 << "}\n\n"
1149 << "} // end namespace sema\n"
1150 << "} // end namespace clang\n";
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +00001151}
1152
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00001153// Emits the list of parsed attributes.
1154void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) {
Michael Hane53ac8a2012-03-07 00:12:16 +00001155 OS << "// This file is generated by TableGen. Do not edit.\n\n";
1156
1157 OS << "#ifndef PARSED_ATTR\n";
1158 OS << "#define PARSED_ATTR(NAME) NAME\n";
1159 OS << "#endif\n\n";
1160
1161 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
Michael Hane53ac8a2012-03-07 00:12:16 +00001162
1163 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1164 I != E; ++I) {
1165 Record &Attr = **I;
1166
1167 bool SemaHandler = Attr.getValueAsBit("SemaHandler");
Douglas Gregor703d4122012-05-11 23:37:49 +00001168 bool DistinctSpellings = Attr.getValueAsBit("DistinctSpellings");
1169
Michael Hane53ac8a2012-03-07 00:12:16 +00001170 if (SemaHandler) {
Sean Hunt8e083e72012-06-19 23:57:03 +00001171 if (DistinctSpellings) {
1172 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +00001173
Sean Hunt8e083e72012-06-19 23:57:03 +00001174 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
1175 E = Spellings.end(); I != E; ++I) {
1176 std::string AttrName = (*I)->getValueAsString("Name");
1177
1178 StringRef Spelling = NormalizeAttrName(AttrName);
1179
1180 OS << "PARSED_ATTR(" << Spelling << ")\n";
1181 }
1182 } else {
1183 StringRef AttrName = Attr.getName();
1184 AttrName = NormalizeAttrName(AttrName);
1185 OS << "PARSED_ATTR(" << AttrName << ")\n";
Michael Hane53ac8a2012-03-07 00:12:16 +00001186 }
1187 }
1188 }
1189}
1190
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00001191// Emits the kind list of parsed attributes
1192void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
Michael Hane53ac8a2012-03-07 00:12:16 +00001193 OS << "// This file is generated by TableGen. Do not edit.\n\n";
Douglas Gregor0c19b3c2012-05-02 17:33:51 +00001194 OS << "\n";
1195
Michael Hane53ac8a2012-03-07 00:12:16 +00001196 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1197
Douglas Gregor0c19b3c2012-05-02 17:33:51 +00001198 std::vector<StringMatcher::StringPair> Matches;
Michael Hane53ac8a2012-03-07 00:12:16 +00001199 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1200 I != E; ++I) {
1201 Record &Attr = **I;
1202
1203 bool SemaHandler = Attr.getValueAsBit("SemaHandler");
Douglas Gregor331d2ec2012-05-02 16:18:45 +00001204 bool Ignored = Attr.getValueAsBit("Ignored");
Douglas Gregor703d4122012-05-11 23:37:49 +00001205 bool DistinctSpellings = Attr.getValueAsBit("DistinctSpellings");
Douglas Gregor331d2ec2012-05-02 16:18:45 +00001206 if (SemaHandler || Ignored) {
Sean Hunt8e083e72012-06-19 23:57:03 +00001207 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
Michael Hane53ac8a2012-03-07 00:12:16 +00001208
Sean Hunt8e083e72012-06-19 23:57:03 +00001209 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
Michael Hane53ac8a2012-03-07 00:12:16 +00001210 E = Spellings.end(); I != E; ++I) {
Sean Hunt8e083e72012-06-19 23:57:03 +00001211 std::string RawSpelling = (*I)->getValueAsString("Name");
Douglas Gregor703d4122012-05-11 23:37:49 +00001212 StringRef AttrName = NormalizeAttrName(DistinctSpellings
Sean Hunt8e083e72012-06-19 23:57:03 +00001213 ? StringRef(RawSpelling)
1214 : StringRef(Attr.getName()));
Michael Hane53ac8a2012-03-07 00:12:16 +00001215
Sean Hunt8e083e72012-06-19 23:57:03 +00001216 SmallString<64> Spelling;
1217 if ((*I)->getValueAsString("Variety") == "CXX11") {
1218 Spelling += (*I)->getValueAsString("Namespace");
1219 Spelling += "::";
Sean Hunt93f95f22012-06-18 16:13:52 +00001220 }
Sean Hunt8e083e72012-06-19 23:57:03 +00001221 Spelling += NormalizeAttrSpelling(RawSpelling);
Sean Hunt93f95f22012-06-18 16:13:52 +00001222
Douglas Gregor331d2ec2012-05-02 16:18:45 +00001223 if (SemaHandler)
Douglas Gregor0c19b3c2012-05-02 17:33:51 +00001224 Matches.push_back(
Douglas Gregor703d4122012-05-11 23:37:49 +00001225 StringMatcher::StringPair(
Sean Hunt8e083e72012-06-19 23:57:03 +00001226 StringRef(Spelling),
Sean Hunt93f95f22012-06-18 16:13:52 +00001227 "return AttributeList::AT_" + AttrName.str() + ";"));
Douglas Gregor331d2ec2012-05-02 16:18:45 +00001228 else
Douglas Gregor0c19b3c2012-05-02 17:33:51 +00001229 Matches.push_back(
1230 StringMatcher::StringPair(
Sean Hunt8e083e72012-06-19 23:57:03 +00001231 StringRef(Spelling),
Sean Hunt93f95f22012-06-18 16:13:52 +00001232 "return AttributeList::IgnoredAttribute;"));
Michael Hane53ac8a2012-03-07 00:12:16 +00001233 }
1234 }
1235 }
Douglas Gregor0c19b3c2012-05-02 17:33:51 +00001236
1237 OS << "static AttributeList::Kind getAttrKind(StringRef Name) {\n";
1238 StringMatcher("Name", Matches, OS).Emit();
1239 OS << "return AttributeList::UnknownAttribute;\n"
1240 << "}\n";
Michael Hane53ac8a2012-03-07 00:12:16 +00001241}
1242
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +00001243// Emits the code to dump an attribute.
1244void EmitClangAttrDump(RecordKeeper &Records, raw_ostream &OS) {
1245 OS <<
1246 " switch (A->getKind()) {\n"
1247 " default:\n"
1248 " llvm_unreachable(\"Unknown attribute kind!\");\n"
1249 " break;\n";
1250 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
1251 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1252 I != E; ++I) {
1253 Record &R = **I;
1254 if (!R.getValueAsBit("ASTNode"))
1255 continue;
1256 OS << " case attr::" << R.getName() << ": {\n";
1257 Args = R.getValueAsListOfDefs("Args");
1258 if (!Args.empty()) {
1259 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
1260 << "Attr>(A);\n";
1261 for (std::vector<Record*>::iterator I = Args.begin(), E = Args.end();
1262 I != E; ++I)
1263 createArgument(**I, R.getName())->writeDump(OS);
1264 for (std::vector<Record*>::iterator I = Args.begin(), E = Args.end();
1265 I != E; ++I)
1266 createArgument(**I, R.getName())->writeDumpChildren(OS);
1267 }
1268 OS <<
1269 " break;\n"
1270 " }\n";
1271 }
1272 OS << " }\n";
1273}
1274
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00001275} // end namespace clang