blob: ef618cea4e8391172321478d5e767689d8351c1b [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";
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000448 OS << " SmallVector<" << type << ", 4> " << getLowerName()
Peter Collingbourne51d77772011-10-06 13:03:08 +0000449 << ";\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";
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000552 for (std::vector<StringRef>::const_iterator I = uniques.begin(),
553 E = uniques.end(); I != E; ++I) {
554 OS << " case " << getAttrName() << "Attr::" << *I << ":\n";
555 OS << " OS << \" " << *I << "\";\n";
556 OS << " break;\n";
557 }
558 OS << " }\n";
559 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000560 };
561
562 class VersionArgument : public Argument {
563 public:
564 VersionArgument(Record &Arg, StringRef Attr)
565 : Argument(Arg, Attr)
566 {}
567
568 void writeAccessors(raw_ostream &OS) const {
569 OS << " VersionTuple get" << getUpperName() << "() const {\n";
570 OS << " return " << getLowerName() << ";\n";
571 OS << " }\n";
572 OS << " void set" << getUpperName()
573 << "(ASTContext &C, VersionTuple V) {\n";
574 OS << " " << getLowerName() << " = V;\n";
575 OS << " }";
576 }
577 void writeCloneArgs(raw_ostream &OS) const {
578 OS << "get" << getUpperName() << "()";
579 }
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000580 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
581 OS << "A->get" << getUpperName() << "()";
582 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000583 void writeCtorBody(raw_ostream &OS) const {
584 }
585 void writeCtorInitializers(raw_ostream &OS) const {
586 OS << getLowerName() << "(" << getUpperName() << ")";
587 }
588 void writeCtorParameters(raw_ostream &OS) const {
589 OS << "VersionTuple " << getUpperName();
590 }
591 void writeDeclarations(raw_ostream &OS) const {
592 OS << "VersionTuple " << getLowerName() << ";\n";
593 }
594 void writePCHReadDecls(raw_ostream &OS) const {
595 OS << " VersionTuple " << getLowerName()
596 << "= ReadVersionTuple(Record, Idx);\n";
597 }
598 void writePCHReadArgs(raw_ostream &OS) const {
599 OS << getLowerName();
600 }
601 void writePCHWrite(raw_ostream &OS) const {
602 OS << " AddVersionTuple(SA->get" << getUpperName() << "(), Record);\n";
603 }
Douglas Gregor1bea8802011-11-19 19:22:57 +0000604 void writeValue(raw_ostream &OS) const {
605 OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";
606 }
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000607 void writeDump(raw_ostream &OS) const {
608 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
609 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000610 };
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000611
612 class ExprArgument : public SimpleArgument {
613 public:
614 ExprArgument(Record &Arg, StringRef Attr)
615 : SimpleArgument(Arg, Attr, "Expr *")
616 {}
617
618 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
619 OS << "tempInst" << getUpperName();
620 }
621
622 void writeTemplateInstantiation(raw_ostream &OS) const {
623 OS << " " << getType() << " tempInst" << getUpperName() << ";\n";
624 OS << " {\n";
625 OS << " EnterExpressionEvaluationContext "
626 << "Unevaluated(S, Sema::Unevaluated);\n";
627 OS << " ExprResult " << "Result = S.SubstExpr("
628 << "A->get" << getUpperName() << "(), TemplateArgs);\n";
629 OS << " tempInst" << getUpperName() << " = "
630 << "Result.takeAs<Expr>();\n";
631 OS << " }\n";
632 }
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000633
634 void writeDump(raw_ostream &OS) const {
635 }
636
637 void writeDumpChildren(raw_ostream &OS) const {
638 OS << " dumpStmt(SA->get" << getUpperName() << "());\n";
639 }
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000640 };
641
642 class VariadicExprArgument : public VariadicArgument {
643 public:
644 VariadicExprArgument(Record &Arg, StringRef Attr)
645 : VariadicArgument(Arg, Attr, "Expr *")
646 {}
647
648 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
649 OS << "tempInst" << getUpperName() << ", "
650 << "A->" << getLowerName() << "_size()";
651 }
652
653 void writeTemplateInstantiation(raw_ostream &OS) const {
654 OS << " " << getType() << " *tempInst" << getUpperName()
655 << " = new (C, 16) " << getType()
656 << "[A->" << getLowerName() << "_size()];\n";
657 OS << " {\n";
658 OS << " EnterExpressionEvaluationContext "
659 << "Unevaluated(S, Sema::Unevaluated);\n";
660 OS << " " << getType() << " *TI = tempInst" << getUpperName()
661 << ";\n";
662 OS << " " << getType() << " *I = A->" << getLowerName()
663 << "_begin();\n";
664 OS << " " << getType() << " *E = A->" << getLowerName()
665 << "_end();\n";
666 OS << " for (; I != E; ++I, ++TI) {\n";
667 OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n";
668 OS << " *TI = Result.takeAs<Expr>();\n";
669 OS << " }\n";
670 OS << " }\n";
671 }
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +0000672
673 void writeDump(raw_ostream &OS) const {
674 }
675
676 void writeDumpChildren(raw_ostream &OS) const {
677 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
678 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
679 << getLowerName() << "_end(); I != E; ++I)\n";
680 OS << " dumpStmt(*I);\n";
681 }
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000682 };
Peter Collingbourne51d77772011-10-06 13:03:08 +0000683}
684
685static Argument *createArgument(Record &Arg, StringRef Attr,
686 Record *Search = 0) {
687 if (!Search)
688 Search = &Arg;
689
690 Argument *Ptr = 0;
691 llvm::StringRef ArgName = Search->getName();
692
693 if (ArgName == "AlignedArgument") Ptr = new AlignedArgument(Arg, Attr);
694 else if (ArgName == "EnumArgument") Ptr = new EnumArgument(Arg, Attr);
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000695 else if (ArgName == "ExprArgument") Ptr = new ExprArgument(Arg, Attr);
Peter Collingbourne51d77772011-10-06 13:03:08 +0000696 else if (ArgName == "FunctionArgument")
697 Ptr = new SimpleArgument(Arg, Attr, "FunctionDecl *");
698 else if (ArgName == "IdentifierArgument")
699 Ptr = new SimpleArgument(Arg, Attr, "IdentifierInfo *");
700 else if (ArgName == "BoolArgument") Ptr = new SimpleArgument(Arg, Attr,
701 "bool");
702 else if (ArgName == "IntArgument") Ptr = new SimpleArgument(Arg, Attr, "int");
703 else if (ArgName == "StringArgument") Ptr = new StringArgument(Arg, Attr);
704 else if (ArgName == "TypeArgument")
705 Ptr = new SimpleArgument(Arg, Attr, "QualType");
706 else if (ArgName == "UnsignedArgument")
707 Ptr = new SimpleArgument(Arg, Attr, "unsigned");
708 else if (ArgName == "SourceLocArgument")
709 Ptr = new SimpleArgument(Arg, Attr, "SourceLocation");
710 else if (ArgName == "VariadicUnsignedArgument")
711 Ptr = new VariadicArgument(Arg, Attr, "unsigned");
712 else if (ArgName == "VariadicExprArgument")
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +0000713 Ptr = new VariadicExprArgument(Arg, Attr);
Peter Collingbourne51d77772011-10-06 13:03:08 +0000714 else if (ArgName == "VersionArgument")
715 Ptr = new VersionArgument(Arg, Attr);
716
717 if (!Ptr) {
718 std::vector<Record*> Bases = Search->getSuperClasses();
719 for (std::vector<Record*>::iterator i = Bases.begin(), e = Bases.end();
720 i != e; ++i) {
721 Ptr = createArgument(Arg, Attr, *i);
722 if (Ptr)
723 break;
724 }
725 }
726 return Ptr;
727}
728
Douglas Gregor1bea8802011-11-19 19:22:57 +0000729static void writeAvailabilityValue(raw_ostream &OS) {
730 OS << "\" << getPlatform()->getName();\n"
731 << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n"
732 << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n"
733 << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n"
734 << " if (getUnavailable()) OS << \", unavailable\";\n"
735 << " OS << \"";
736}
737
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000738namespace clang {
739
740// Emits the class definitions for attributes.
741void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000742 OS << "// This file is generated by TableGen. Do not edit.\n\n";
743 OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
744 OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
745
746 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
747
748 for (std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end();
749 i != e; ++i) {
750 Record &R = **i;
Douglas Gregor3e7d31a2012-05-02 15:56:52 +0000751
752 if (!R.getValueAsBit("ASTNode"))
753 continue;
754
Peter Collingbourne51d77772011-10-06 13:03:08 +0000755 const std::string &SuperName = R.getSuperClasses().back()->getName();
756
757 OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
758
759 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
760 std::vector<Argument*> Args;
761 std::vector<Argument*>::iterator ai, ae;
762 Args.reserve(ArgRecords.size());
763
764 for (std::vector<Record*>::iterator ri = ArgRecords.begin(),
765 re = ArgRecords.end();
766 ri != re; ++ri) {
767 Record &ArgRecord = **ri;
768 Argument *Arg = createArgument(ArgRecord, R.getName());
769 assert(Arg);
770 Args.push_back(Arg);
771
772 Arg->writeDeclarations(OS);
773 OS << "\n\n";
774 }
775
776 ae = Args.end();
777
778 OS << "\n public:\n";
779 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
780
781 for (ai = Args.begin(); ai != ae; ++ai) {
782 OS << " , ";
783 (*ai)->writeCtorParameters(OS);
784 OS << "\n";
785 }
786
787 OS << " )\n";
788 OS << " : " << SuperName << "(attr::" << R.getName() << ", R)\n";
789
790 for (ai = Args.begin(); ai != ae; ++ai) {
791 OS << " , ";
792 (*ai)->writeCtorInitializers(OS);
793 OS << "\n";
794 }
795
796 OS << " {\n";
797
798 for (ai = Args.begin(); ai != ae; ++ai) {
799 (*ai)->writeCtorBody(OS);
800 OS << "\n";
801 }
802 OS << " }\n\n";
803
804 OS << " virtual " << R.getName() << "Attr *clone (ASTContext &C) const;\n";
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000805 OS << " virtual void printPretty(raw_ostream &OS,"
Richard Smith0dae7292012-08-16 02:43:29 +0000806 << " const PrintingPolicy &Policy) const;\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000807
808 for (ai = Args.begin(); ai != ae; ++ai) {
809 (*ai)->writeAccessors(OS);
810 OS << "\n\n";
811 }
812
Jakob Stoklund Oleseneb666732012-01-13 04:57:47 +0000813 OS << R.getValueAsString("AdditionalMembers");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000814 OS << "\n\n";
815
816 OS << " static bool classof(const Attr *A) { return A->getKind() == "
817 << "attr::" << R.getName() << "; }\n";
DeLesley Hutchins23323e02012-01-20 22:50:54 +0000818
819 bool LateParsed = R.getValueAsBit("LateParsed");
820 OS << " virtual bool isLateParsed() const { return "
821 << LateParsed << "; }\n";
822
Peter Collingbourne51d77772011-10-06 13:03:08 +0000823 OS << "};\n\n";
824 }
825
826 OS << "#endif\n";
827}
828
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000829// Emits the class method definitions for attributes.
830void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000831 OS << "// This file is generated by TableGen. Do not edit.\n\n";
832
833 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
834 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ri, re;
835 std::vector<Argument*>::iterator ai, ae;
836
837 for (; i != e; ++i) {
838 Record &R = **i;
Douglas Gregor3e7d31a2012-05-02 15:56:52 +0000839
840 if (!R.getValueAsBit("ASTNode"))
841 continue;
842
Peter Collingbourne51d77772011-10-06 13:03:08 +0000843 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Sean Hunt8e083e72012-06-19 23:57:03 +0000844 std::vector<Record*> Spellings = R.getValueAsListOfDefs("Spellings");
Peter Collingbourne51d77772011-10-06 13:03:08 +0000845 std::vector<Argument*> Args;
846 for (ri = ArgRecords.begin(), re = ArgRecords.end(); ri != re; ++ri)
847 Args.push_back(createArgument(**ri, R.getName()));
848
849 for (ai = Args.begin(), ae = Args.end(); ai != ae; ++ai)
850 (*ai)->writeAccessorDefinitions(OS);
851
852 OS << R.getName() << "Attr *" << R.getName()
853 << "Attr::clone(ASTContext &C) const {\n";
854 OS << " return new (C) " << R.getName() << "Attr(getLocation(), C";
855 for (ai = Args.begin(); ai != ae; ++ai) {
856 OS << ", ";
857 (*ai)->writeCloneArgs(OS);
858 }
859 OS << ");\n}\n\n";
Douglas Gregor1bea8802011-11-19 19:22:57 +0000860
861 OS << "void " << R.getName() << "Attr::printPretty("
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000862 << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n";
Douglas Gregor1bea8802011-11-19 19:22:57 +0000863 if (Spellings.begin() != Spellings.end()) {
NAKAMURA Takumi41de13b2012-07-03 14:45:09 +0000864 std::string Spelling = (*Spellings.begin())->getValueAsString("Name");
Sean Hunt8e083e72012-06-19 23:57:03 +0000865 OS << " OS << \" __attribute__((" << Spelling;
Douglas Gregor1bea8802011-11-19 19:22:57 +0000866 if (Args.size()) OS << "(";
Sean Hunt8e083e72012-06-19 23:57:03 +0000867 if (Spelling == "availability") {
Douglas Gregor1bea8802011-11-19 19:22:57 +0000868 writeAvailabilityValue(OS);
869 } else {
870 for (ai = Args.begin(); ai != ae; ++ai) {
871 if (ai!=Args.begin()) OS <<", ";
872 (*ai)->writeValue(OS);
873 }
874 }
875 if (Args.size()) OS << ")";
876 OS << "))\";\n";
877 }
878 OS << "}\n\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +0000879 }
880}
881
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000882} // end namespace clang
883
Peter Collingbourne51d77772011-10-06 13:03:08 +0000884static void EmitAttrList(raw_ostream &OS, StringRef Class,
885 const std::vector<Record*> &AttrList) {
886 std::vector<Record*>::const_iterator i = AttrList.begin(), e = AttrList.end();
887
888 if (i != e) {
889 // Move the end iterator back to emit the last attribute.
Douglas Gregor3e7d31a2012-05-02 15:56:52 +0000890 for(--e; i != e; ++i) {
891 if (!(*i)->getValueAsBit("ASTNode"))
892 continue;
893
Peter Collingbourne51d77772011-10-06 13:03:08 +0000894 OS << Class << "(" << (*i)->getName() << ")\n";
Douglas Gregor3e7d31a2012-05-02 15:56:52 +0000895 }
Peter Collingbourne51d77772011-10-06 13:03:08 +0000896
897 OS << "LAST_" << Class << "(" << (*i)->getName() << ")\n\n";
898 }
899}
900
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000901namespace clang {
902
903// Emits the enumeration list for attributes.
904void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000905 OS << "// This file is generated by TableGen. Do not edit.\n\n";
906
907 OS << "#ifndef LAST_ATTR\n";
908 OS << "#define LAST_ATTR(NAME) ATTR(NAME)\n";
909 OS << "#endif\n\n";
910
911 OS << "#ifndef INHERITABLE_ATTR\n";
912 OS << "#define INHERITABLE_ATTR(NAME) ATTR(NAME)\n";
913 OS << "#endif\n\n";
914
915 OS << "#ifndef LAST_INHERITABLE_ATTR\n";
916 OS << "#define LAST_INHERITABLE_ATTR(NAME) INHERITABLE_ATTR(NAME)\n";
917 OS << "#endif\n\n";
918
919 OS << "#ifndef INHERITABLE_PARAM_ATTR\n";
920 OS << "#define INHERITABLE_PARAM_ATTR(NAME) ATTR(NAME)\n";
921 OS << "#endif\n\n";
922
923 OS << "#ifndef LAST_INHERITABLE_PARAM_ATTR\n";
924 OS << "#define LAST_INHERITABLE_PARAM_ATTR(NAME)"
925 " INHERITABLE_PARAM_ATTR(NAME)\n";
926 OS << "#endif\n\n";
927
928 Record *InhClass = Records.getClass("InheritableAttr");
929 Record *InhParamClass = Records.getClass("InheritableParamAttr");
930 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
931 NonInhAttrs, InhAttrs, InhParamAttrs;
932 for (std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end();
933 i != e; ++i) {
Douglas Gregor3e7d31a2012-05-02 15:56:52 +0000934 if (!(*i)->getValueAsBit("ASTNode"))
935 continue;
936
Peter Collingbourne51d77772011-10-06 13:03:08 +0000937 if ((*i)->isSubClassOf(InhParamClass))
938 InhParamAttrs.push_back(*i);
939 else if ((*i)->isSubClassOf(InhClass))
940 InhAttrs.push_back(*i);
941 else
942 NonInhAttrs.push_back(*i);
943 }
944
945 EmitAttrList(OS, "INHERITABLE_PARAM_ATTR", InhParamAttrs);
946 EmitAttrList(OS, "INHERITABLE_ATTR", InhAttrs);
947 EmitAttrList(OS, "ATTR", NonInhAttrs);
948
949 OS << "#undef LAST_ATTR\n";
950 OS << "#undef INHERITABLE_ATTR\n";
951 OS << "#undef LAST_INHERITABLE_ATTR\n";
952 OS << "#undef LAST_INHERITABLE_PARAM_ATTR\n";
953 OS << "#undef ATTR\n";
954}
955
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +0000956// Emits the code to read an attribute from a precompiled header.
957void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +0000958 OS << "// This file is generated by TableGen. Do not edit.\n\n";
959
960 Record *InhClass = Records.getClass("InheritableAttr");
961 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
962 ArgRecords;
963 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ai, ae;
964 std::vector<Argument*> Args;
965 std::vector<Argument*>::iterator ri, re;
966
967 OS << " switch (Kind) {\n";
968 OS << " default:\n";
969 OS << " assert(0 && \"Unknown attribute!\");\n";
970 OS << " break;\n";
971 for (; i != e; ++i) {
972 Record &R = **i;
Douglas Gregor3e7d31a2012-05-02 15:56:52 +0000973 if (!R.getValueAsBit("ASTNode"))
974 continue;
975
Peter Collingbourne51d77772011-10-06 13:03:08 +0000976 OS << " case attr::" << R.getName() << ": {\n";
977 if (R.isSubClassOf(InhClass))
978 OS << " bool isInherited = Record[Idx++];\n";
979 ArgRecords = R.getValueAsListOfDefs("Args");
980 Args.clear();
981 for (ai = ArgRecords.begin(), ae = ArgRecords.end(); ai != ae; ++ai) {
982 Argument *A = createArgument(**ai, R.getName());
983 Args.push_back(A);
984 A->writePCHReadDecls(OS);
985 }
986 OS << " New = new (Context) " << R.getName() << "Attr(Range, Context";
987 for (ri = Args.begin(), re = Args.end(); ri != re; ++ri) {
988 OS << ", ";
989 (*ri)->writePCHReadArgs(OS);
990 }
991 OS << ");\n";
992 if (R.isSubClassOf(InhClass))
993 OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
994 OS << " break;\n";
995 OS << " }\n";
996 }
997 OS << " }\n";
998}
999
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00001000// Emits the code to write an attribute to a precompiled header.
1001void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +00001002 Record *InhClass = Records.getClass("InheritableAttr");
1003 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
1004 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ai, ae;
1005
1006 OS << " switch (A->getKind()) {\n";
1007 OS << " default:\n";
1008 OS << " llvm_unreachable(\"Unknown attribute kind!\");\n";
1009 OS << " break;\n";
1010 for (; i != e; ++i) {
1011 Record &R = **i;
Douglas Gregor3e7d31a2012-05-02 15:56:52 +00001012 if (!R.getValueAsBit("ASTNode"))
1013 continue;
Peter Collingbourne51d77772011-10-06 13:03:08 +00001014 OS << " case attr::" << R.getName() << ": {\n";
1015 Args = R.getValueAsListOfDefs("Args");
1016 if (R.isSubClassOf(InhClass) || !Args.empty())
1017 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
1018 << "Attr>(A);\n";
1019 if (R.isSubClassOf(InhClass))
1020 OS << " Record.push_back(SA->isInherited());\n";
1021 for (ai = Args.begin(), ae = Args.end(); ai != ae; ++ai)
1022 createArgument(**ai, R.getName())->writePCHWrite(OS);
1023 OS << " break;\n";
1024 OS << " }\n";
1025 }
1026 OS << " }\n";
1027}
1028
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00001029// Emits the list of spellings for attributes.
1030void EmitClangAttrSpellingList(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +00001031 OS << "// This file is generated by TableGen. Do not edit.\n\n";
1032
1033 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1034
1035 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
1036 Record &Attr = **I;
1037
Sean Hunt8e083e72012-06-19 23:57:03 +00001038 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001039
Sean Hunt8e083e72012-06-19 23:57:03 +00001040 for (std::vector<Record*>::const_iterator I = Spellings.begin(), E = Spellings.end(); I != E; ++I) {
1041 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", true)\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001042 }
1043 }
1044
1045}
1046
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00001047// Emits the LateParsed property for attributes.
1048void EmitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) {
Peter Collingbourne51d77772011-10-06 13:03:08 +00001049 OS << "// This file is generated by TableGen. Do not edit.\n\n";
1050
1051 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1052
1053 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1054 I != E; ++I) {
1055 Record &Attr = **I;
1056
1057 bool LateParsed = Attr.getValueAsBit("LateParsed");
1058
1059 if (LateParsed) {
Sean Hunt8e083e72012-06-19 23:57:03 +00001060 std::vector<Record*> Spellings =
1061 Attr.getValueAsListOfDefs("Spellings");
Peter Collingbourne51d77772011-10-06 13:03:08 +00001062
Sean Hunt8e083e72012-06-19 23:57:03 +00001063 // FIXME: Handle non-GNU attributes
1064 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
Peter Collingbourne51d77772011-10-06 13:03:08 +00001065 E = Spellings.end(); I != E; ++I) {
Sean Hunt8e083e72012-06-19 23:57:03 +00001066 if ((*I)->getValueAsString("Variety") != "GNU")
1067 continue;
1068 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", "
1069 << LateParsed << ")\n";
Peter Collingbourne51d77772011-10-06 13:03:08 +00001070 }
1071 }
1072 }
1073}
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +00001074
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00001075// Emits code to instantiate dependent attributes on templates.
1076void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) {
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +00001077 OS << "// This file is generated by TableGen. Do not edit.\n\n";
1078
1079 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1080
Benjamin Kramer5bbc3852012-02-06 11:13:08 +00001081 OS << "namespace clang {\n"
1082 << "namespace sema {\n\n"
1083 << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, "
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +00001084 << "Sema &S,\n"
1085 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"
1086 << " switch (At->getKind()) {\n"
1087 << " default:\n"
1088 << " break;\n";
1089
1090 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1091 I != E; ++I) {
1092 Record &R = **I;
Douglas Gregor3e7d31a2012-05-02 15:56:52 +00001093 if (!R.getValueAsBit("ASTNode"))
1094 continue;
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +00001095
1096 OS << " case attr::" << R.getName() << ": {\n";
Rafael Espindola31c195a2012-05-15 14:09:55 +00001097 bool ShouldClone = R.getValueAsBit("Clone");
1098
1099 if (!ShouldClone) {
1100 OS << " return NULL;\n";
1101 OS << " }\n";
1102 continue;
1103 }
1104
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +00001105 OS << " const " << R.getName() << "Attr *A = cast<"
1106 << R.getName() << "Attr>(At);\n";
1107 bool TDependent = R.getValueAsBit("TemplateDependent");
1108
1109 if (!TDependent) {
1110 OS << " return A->clone(C);\n";
1111 OS << " }\n";
1112 continue;
1113 }
1114
1115 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1116 std::vector<Argument*> Args;
1117 std::vector<Argument*>::iterator ai, ae;
1118 Args.reserve(ArgRecords.size());
1119
1120 for (std::vector<Record*>::iterator ri = ArgRecords.begin(),
1121 re = ArgRecords.end();
1122 ri != re; ++ri) {
1123 Record &ArgRecord = **ri;
1124 Argument *Arg = createArgument(ArgRecord, R.getName());
1125 assert(Arg);
1126 Args.push_back(Arg);
1127 }
1128 ae = Args.end();
1129
1130 for (ai = Args.begin(); ai != ae; ++ai) {
1131 (*ai)->writeTemplateInstantiation(OS);
1132 }
1133 OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C";
1134 for (ai = Args.begin(); ai != ae; ++ai) {
1135 OS << ", ";
1136 (*ai)->writeTemplateInstantiationArgs(OS);
1137 }
1138 OS << ");\n }\n";
1139 }
1140 OS << " } // end switch\n"
1141 << " llvm_unreachable(\"Unknown attribute!\");\n"
1142 << " return 0;\n"
Benjamin Kramer5bbc3852012-02-06 11:13:08 +00001143 << "}\n\n"
1144 << "} // end namespace sema\n"
1145 << "} // end namespace clang\n";
DeLesley Hutchins7b9ff0c2012-01-20 22:37:06 +00001146}
1147
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00001148// Emits the list of parsed attributes.
1149void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) {
Michael Hane53ac8a2012-03-07 00:12:16 +00001150 OS << "// This file is generated by TableGen. Do not edit.\n\n";
1151
1152 OS << "#ifndef PARSED_ATTR\n";
1153 OS << "#define PARSED_ATTR(NAME) NAME\n";
1154 OS << "#endif\n\n";
1155
1156 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
Michael Hane53ac8a2012-03-07 00:12:16 +00001157
1158 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1159 I != E; ++I) {
1160 Record &Attr = **I;
1161
1162 bool SemaHandler = Attr.getValueAsBit("SemaHandler");
Douglas Gregor703d4122012-05-11 23:37:49 +00001163 bool DistinctSpellings = Attr.getValueAsBit("DistinctSpellings");
1164
Michael Hane53ac8a2012-03-07 00:12:16 +00001165 if (SemaHandler) {
Sean Hunt8e083e72012-06-19 23:57:03 +00001166 if (DistinctSpellings) {
1167 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +00001168
Sean Hunt8e083e72012-06-19 23:57:03 +00001169 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
1170 E = Spellings.end(); I != E; ++I) {
1171 std::string AttrName = (*I)->getValueAsString("Name");
1172
1173 StringRef Spelling = NormalizeAttrName(AttrName);
1174
1175 OS << "PARSED_ATTR(" << Spelling << ")\n";
1176 }
1177 } else {
1178 StringRef AttrName = Attr.getName();
1179 AttrName = NormalizeAttrName(AttrName);
1180 OS << "PARSED_ATTR(" << AttrName << ")\n";
Michael Hane53ac8a2012-03-07 00:12:16 +00001181 }
1182 }
1183 }
1184}
1185
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00001186// Emits the kind list of parsed attributes
1187void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
Michael Hane53ac8a2012-03-07 00:12:16 +00001188 OS << "// This file is generated by TableGen. Do not edit.\n\n";
Douglas Gregor0c19b3c2012-05-02 17:33:51 +00001189 OS << "\n";
1190
Michael Hane53ac8a2012-03-07 00:12:16 +00001191 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1192
Douglas Gregor0c19b3c2012-05-02 17:33:51 +00001193 std::vector<StringMatcher::StringPair> Matches;
Michael Hane53ac8a2012-03-07 00:12:16 +00001194 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1195 I != E; ++I) {
1196 Record &Attr = **I;
1197
1198 bool SemaHandler = Attr.getValueAsBit("SemaHandler");
Douglas Gregor331d2ec2012-05-02 16:18:45 +00001199 bool Ignored = Attr.getValueAsBit("Ignored");
Douglas Gregor703d4122012-05-11 23:37:49 +00001200 bool DistinctSpellings = Attr.getValueAsBit("DistinctSpellings");
Douglas Gregor331d2ec2012-05-02 16:18:45 +00001201 if (SemaHandler || Ignored) {
Sean Hunt8e083e72012-06-19 23:57:03 +00001202 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
Michael Hane53ac8a2012-03-07 00:12:16 +00001203
Sean Hunt8e083e72012-06-19 23:57:03 +00001204 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
Michael Hane53ac8a2012-03-07 00:12:16 +00001205 E = Spellings.end(); I != E; ++I) {
Sean Hunt8e083e72012-06-19 23:57:03 +00001206 std::string RawSpelling = (*I)->getValueAsString("Name");
Douglas Gregor703d4122012-05-11 23:37:49 +00001207 StringRef AttrName = NormalizeAttrName(DistinctSpellings
Sean Hunt8e083e72012-06-19 23:57:03 +00001208 ? StringRef(RawSpelling)
1209 : StringRef(Attr.getName()));
Michael Hane53ac8a2012-03-07 00:12:16 +00001210
Sean Hunt8e083e72012-06-19 23:57:03 +00001211 SmallString<64> Spelling;
1212 if ((*I)->getValueAsString("Variety") == "CXX11") {
1213 Spelling += (*I)->getValueAsString("Namespace");
1214 Spelling += "::";
Sean Hunt93f95f22012-06-18 16:13:52 +00001215 }
Sean Hunt8e083e72012-06-19 23:57:03 +00001216 Spelling += NormalizeAttrSpelling(RawSpelling);
Sean Hunt93f95f22012-06-18 16:13:52 +00001217
Douglas Gregor331d2ec2012-05-02 16:18:45 +00001218 if (SemaHandler)
Douglas Gregor0c19b3c2012-05-02 17:33:51 +00001219 Matches.push_back(
Douglas Gregor703d4122012-05-11 23:37:49 +00001220 StringMatcher::StringPair(
Sean Hunt8e083e72012-06-19 23:57:03 +00001221 StringRef(Spelling),
Sean Hunt93f95f22012-06-18 16:13:52 +00001222 "return AttributeList::AT_" + AttrName.str() + ";"));
Douglas Gregor331d2ec2012-05-02 16:18:45 +00001223 else
Douglas Gregor0c19b3c2012-05-02 17:33:51 +00001224 Matches.push_back(
1225 StringMatcher::StringPair(
Sean Hunt8e083e72012-06-19 23:57:03 +00001226 StringRef(Spelling),
Sean Hunt93f95f22012-06-18 16:13:52 +00001227 "return AttributeList::IgnoredAttribute;"));
Michael Hane53ac8a2012-03-07 00:12:16 +00001228 }
1229 }
1230 }
Douglas Gregor0c19b3c2012-05-02 17:33:51 +00001231
1232 OS << "static AttributeList::Kind getAttrKind(StringRef Name) {\n";
1233 StringMatcher("Name", Matches, OS).Emit();
1234 OS << "return AttributeList::UnknownAttribute;\n"
1235 << "}\n";
Michael Hane53ac8a2012-03-07 00:12:16 +00001236}
1237
Alexander Kornienkoc3cd2b02013-01-07 17:53:08 +00001238// Emits the code to dump an attribute.
1239void EmitClangAttrDump(RecordKeeper &Records, raw_ostream &OS) {
1240 OS <<
1241 " switch (A->getKind()) {\n"
1242 " default:\n"
1243 " llvm_unreachable(\"Unknown attribute kind!\");\n"
1244 " break;\n";
1245 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
1246 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1247 I != E; ++I) {
1248 Record &R = **I;
1249 if (!R.getValueAsBit("ASTNode"))
1250 continue;
1251 OS << " case attr::" << R.getName() << ": {\n";
1252 Args = R.getValueAsListOfDefs("Args");
1253 if (!Args.empty()) {
1254 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
1255 << "Attr>(A);\n";
1256 for (std::vector<Record*>::iterator I = Args.begin(), E = Args.end();
1257 I != E; ++I)
1258 createArgument(**I, R.getName())->writeDump(OS);
1259 for (std::vector<Record*>::iterator I = Args.begin(), E = Args.end();
1260 I != E; ++I)
1261 createArgument(**I, R.getName())->writeDumpChildren(OS);
1262 }
1263 OS <<
1264 " break;\n"
1265 " }\n";
1266 }
1267 OS << " }\n";
1268}
1269
Jakob Stoklund Olesen3cc509b2012-06-13 05:12:41 +00001270} // end namespace clang