blob: abdb98d8f26251be3d76821428f6460ed39280df [file] [log] [blame]
Peter Collingbournebee583f2011-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
Alexis Hunta0e54d42012-06-18 16:13:52 +000014#include "llvm/ADT/SmallString.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000015#include "llvm/ADT/StringSwitch.h"
Aaron Ballman8ee40b72013-09-09 23:33:17 +000016#include "llvm/ADT/SmallSet.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000017#include "llvm/TableGen/Record.h"
Douglas Gregor377f99b2012-05-02 17:33:51 +000018#include "llvm/TableGen/StringMatcher.h"
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000019#include "llvm/TableGen/TableGenBackend.h"
Aaron Ballman74eeeae2013-11-27 13:27:02 +000020#include "llvm/TableGen/Error.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000021#include <algorithm>
22#include <cctype>
Aaron Ballman74eeeae2013-11-27 13:27:02 +000023#include <sstream>
Peter Collingbournebee583f2011-10-06 13:03:08 +000024
25using namespace llvm;
26
27static const std::vector<StringRef>
28getValueAsListOfStrings(Record &R, StringRef FieldName) {
29 ListInit *List = R.getValueAsListInit(FieldName);
30 assert (List && "Got a null ListInit");
31
32 std::vector<StringRef> Strings;
33 Strings.reserve(List->getSize());
34
35 for (ListInit::const_iterator i = List->begin(), e = List->end();
36 i != e;
37 ++i) {
38 assert(*i && "Got a null element in a ListInit");
Sean Silva1c4aaa82012-10-10 20:25:43 +000039 if (StringInit *S = dyn_cast<StringInit>(*i))
Peter Collingbournebee583f2011-10-06 13:03:08 +000040 Strings.push_back(S->getValue());
Peter Collingbournebee583f2011-10-06 13:03:08 +000041 else
42 assert(false && "Got a non-string, non-code element in a ListInit");
43 }
44
45 return Strings;
46}
47
48static std::string ReadPCHRecord(StringRef type) {
49 return StringSwitch<std::string>(type)
50 .EndsWith("Decl *", "GetLocalDeclAs<"
51 + std::string(type, 0, type.size()-1) + ">(F, Record[Idx++])")
Richard Smithb87c4652013-10-31 21:23:20 +000052 .Case("TypeSourceInfo *", "GetTypeSourceInfo(F, Record, Idx)")
Argyrios Kyrtzidisa660ae42012-11-15 01:31:39 +000053 .Case("Expr *", "ReadExpr(F)")
Peter Collingbournebee583f2011-10-06 13:03:08 +000054 .Case("IdentifierInfo *", "GetIdentifierInfo(F, Record, Idx)")
55 .Case("SourceLocation", "ReadSourceLocation(F, Record, Idx)")
56 .Default("Record[Idx++]");
57}
58
59// Assumes that the way to get the value is SA->getname()
60static std::string WritePCHRecord(StringRef type, StringRef name) {
61 return StringSwitch<std::string>(type)
62 .EndsWith("Decl *", "AddDeclRef(" + std::string(name) +
63 ", Record);\n")
Richard Smithb87c4652013-10-31 21:23:20 +000064 .Case("TypeSourceInfo *",
65 "AddTypeSourceInfo(" + std::string(name) + ", Record);\n")
Peter Collingbournebee583f2011-10-06 13:03:08 +000066 .Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
67 .Case("IdentifierInfo *",
68 "AddIdentifierRef(" + std::string(name) + ", Record);\n")
69 .Case("SourceLocation",
70 "AddSourceLocation(" + std::string(name) + ", Record);\n")
71 .Default("Record.push_back(" + std::string(name) + ");\n");
72}
73
Michael Han4a045172012-03-07 00:12:16 +000074// Normalize attribute name by removing leading and trailing
75// underscores. For example, __foo, foo__, __foo__ would
76// become foo.
77static StringRef NormalizeAttrName(StringRef AttrName) {
78 if (AttrName.startswith("__"))
79 AttrName = AttrName.substr(2, AttrName.size());
80
81 if (AttrName.endswith("__"))
82 AttrName = AttrName.substr(0, AttrName.size() - 2);
83
84 return AttrName;
85}
86
87// Normalize attribute spelling only if the spelling has both leading
88// and trailing underscores. For example, __ms_struct__ will be
89// normalized to "ms_struct"; __cdecl will remain intact.
90static StringRef NormalizeAttrSpelling(StringRef AttrSpelling) {
91 if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) {
92 AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4);
93 }
94
95 return AttrSpelling;
96}
97
Peter Collingbournebee583f2011-10-06 13:03:08 +000098namespace {
99 class Argument {
100 std::string lowerName, upperName;
101 StringRef attrName;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000102 bool isOpt;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000103
104 public:
105 Argument(Record &Arg, StringRef Attr)
106 : lowerName(Arg.getValueAsString("Name")), upperName(lowerName),
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000107 attrName(Attr), isOpt(false) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000108 if (!lowerName.empty()) {
109 lowerName[0] = std::tolower(lowerName[0]);
110 upperName[0] = std::toupper(upperName[0]);
111 }
112 }
113 virtual ~Argument() {}
114
115 StringRef getLowerName() const { return lowerName; }
116 StringRef getUpperName() const { return upperName; }
117 StringRef getAttrName() const { return attrName; }
118
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000119 bool isOptional() const { return isOpt; }
120 void setOptional(bool set) { isOpt = set; }
121
Peter Collingbournebee583f2011-10-06 13:03:08 +0000122 // These functions print the argument contents formatted in different ways.
123 virtual void writeAccessors(raw_ostream &OS) const = 0;
124 virtual void writeAccessorDefinitions(raw_ostream &OS) const {}
125 virtual void writeCloneArgs(raw_ostream &OS) const = 0;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000126 virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0;
Daniel Dunbardc51baa2012-02-10 06:00:29 +0000127 virtual void writeTemplateInstantiation(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000128 virtual void writeCtorBody(raw_ostream &OS) const {}
129 virtual void writeCtorInitializers(raw_ostream &OS) const = 0;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000130 virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000131 virtual void writeCtorParameters(raw_ostream &OS) const = 0;
132 virtual void writeDeclarations(raw_ostream &OS) const = 0;
133 virtual void writePCHReadArgs(raw_ostream &OS) const = 0;
134 virtual void writePCHReadDecls(raw_ostream &OS) const = 0;
135 virtual void writePCHWrite(raw_ostream &OS) const = 0;
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000136 virtual void writeValue(raw_ostream &OS) const = 0;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000137 virtual void writeDump(raw_ostream &OS) const = 0;
138 virtual void writeDumpChildren(raw_ostream &OS) const {}
Richard Trieude5cc7d2013-01-31 01:44:26 +0000139 virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000140
141 virtual bool isEnumArg() const { return false; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000142 virtual bool isVariadicEnumArg() const { return false; }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000143 };
144
145 class SimpleArgument : public Argument {
146 std::string type;
147
148 public:
149 SimpleArgument(Record &Arg, StringRef Attr, std::string T)
150 : Argument(Arg, Attr), type(T)
151 {}
152
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000153 std::string getType() const { return type; }
154
Peter Collingbournebee583f2011-10-06 13:03:08 +0000155 void writeAccessors(raw_ostream &OS) const {
156 OS << " " << type << " get" << getUpperName() << "() const {\n";
157 OS << " return " << getLowerName() << ";\n";
158 OS << " }";
159 }
160 void writeCloneArgs(raw_ostream &OS) const {
161 OS << getLowerName();
162 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000163 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
164 OS << "A->get" << getUpperName() << "()";
165 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000166 void writeCtorInitializers(raw_ostream &OS) const {
167 OS << getLowerName() << "(" << getUpperName() << ")";
168 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000169 void writeCtorDefaultInitializers(raw_ostream &OS) const {
170 OS << getLowerName() << "()";
171 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000172 void writeCtorParameters(raw_ostream &OS) const {
173 OS << type << " " << getUpperName();
174 }
175 void writeDeclarations(raw_ostream &OS) const {
176 OS << type << " " << getLowerName() << ";";
177 }
178 void writePCHReadDecls(raw_ostream &OS) const {
179 std::string read = ReadPCHRecord(type);
180 OS << " " << type << " " << getLowerName() << " = " << read << ";\n";
181 }
182 void writePCHReadArgs(raw_ostream &OS) const {
183 OS << getLowerName();
184 }
185 void writePCHWrite(raw_ostream &OS) const {
186 OS << " " << WritePCHRecord(type, "SA->get" +
187 std::string(getUpperName()) + "()");
188 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000189 void writeValue(raw_ostream &OS) const {
190 if (type == "FunctionDecl *") {
Richard Smithb87c4652013-10-31 21:23:20 +0000191 OS << "\" << get" << getUpperName()
192 << "()->getNameInfo().getAsString() << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000193 } else if (type == "IdentifierInfo *") {
194 OS << "\" << get" << getUpperName() << "()->getName() << \"";
Richard Smithb87c4652013-10-31 21:23:20 +0000195 } else if (type == "TypeSourceInfo *") {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000196 OS << "\" << get" << getUpperName() << "().getAsString() << \"";
197 } else if (type == "SourceLocation") {
198 OS << "\" << get" << getUpperName() << "().getRawEncoding() << \"";
199 } else {
200 OS << "\" << get" << getUpperName() << "() << \"";
201 }
202 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000203 void writeDump(raw_ostream &OS) const {
204 if (type == "FunctionDecl *") {
205 OS << " OS << \" \";\n";
206 OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n";
207 } else if (type == "IdentifierInfo *") {
208 OS << " OS << \" \" << SA->get" << getUpperName()
209 << "()->getName();\n";
Richard Smithb87c4652013-10-31 21:23:20 +0000210 } else if (type == "TypeSourceInfo *") {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000211 OS << " OS << \" \" << SA->get" << getUpperName()
212 << "().getAsString();\n";
213 } else if (type == "SourceLocation") {
214 OS << " OS << \" \";\n";
215 OS << " SA->get" << getUpperName() << "().print(OS, *SM);\n";
216 } else if (type == "bool") {
217 OS << " if (SA->get" << getUpperName() << "()) OS << \" "
218 << getUpperName() << "\";\n";
219 } else if (type == "int" || type == "unsigned") {
220 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
221 } else {
222 llvm_unreachable("Unknown SimpleArgument type!");
223 }
224 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000225 };
226
Aaron Ballman18a78382013-11-21 00:28:23 +0000227 class DefaultSimpleArgument : public SimpleArgument {
228 int64_t Default;
229
230 public:
231 DefaultSimpleArgument(Record &Arg, StringRef Attr,
232 std::string T, int64_t Default)
233 : SimpleArgument(Arg, Attr, T), Default(Default) {}
234
235 void writeAccessors(raw_ostream &OS) const {
236 SimpleArgument::writeAccessors(OS);
237
238 OS << "\n\n static const " << getType() << " Default" << getUpperName()
239 << " = " << Default << ";";
240 }
241 };
242
Peter Collingbournebee583f2011-10-06 13:03:08 +0000243 class StringArgument : public Argument {
244 public:
245 StringArgument(Record &Arg, StringRef Attr)
246 : Argument(Arg, Attr)
247 {}
248
249 void writeAccessors(raw_ostream &OS) const {
250 OS << " llvm::StringRef get" << getUpperName() << "() const {\n";
251 OS << " return llvm::StringRef(" << getLowerName() << ", "
252 << getLowerName() << "Length);\n";
253 OS << " }\n";
254 OS << " unsigned get" << getUpperName() << "Length() const {\n";
255 OS << " return " << getLowerName() << "Length;\n";
256 OS << " }\n";
257 OS << " void set" << getUpperName()
258 << "(ASTContext &C, llvm::StringRef S) {\n";
259 OS << " " << getLowerName() << "Length = S.size();\n";
260 OS << " this->" << getLowerName() << " = new (C, 1) char ["
261 << getLowerName() << "Length];\n";
262 OS << " std::memcpy(this->" << getLowerName() << ", S.data(), "
263 << getLowerName() << "Length);\n";
264 OS << " }";
265 }
266 void writeCloneArgs(raw_ostream &OS) const {
267 OS << "get" << getUpperName() << "()";
268 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000269 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
270 OS << "A->get" << getUpperName() << "()";
271 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000272 void writeCtorBody(raw_ostream &OS) const {
273 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
274 << ".data(), " << getLowerName() << "Length);";
275 }
276 void writeCtorInitializers(raw_ostream &OS) const {
277 OS << getLowerName() << "Length(" << getUpperName() << ".size()),"
278 << getLowerName() << "(new (Ctx, 1) char[" << getLowerName()
279 << "Length])";
280 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000281 void writeCtorDefaultInitializers(raw_ostream &OS) const {
282 OS << getLowerName() << "Length(0)," << getLowerName() << "(0)";
283 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000284 void writeCtorParameters(raw_ostream &OS) const {
285 OS << "llvm::StringRef " << getUpperName();
286 }
287 void writeDeclarations(raw_ostream &OS) const {
288 OS << "unsigned " << getLowerName() << "Length;\n";
289 OS << "char *" << getLowerName() << ";";
290 }
291 void writePCHReadDecls(raw_ostream &OS) const {
292 OS << " std::string " << getLowerName()
293 << "= ReadString(Record, Idx);\n";
294 }
295 void writePCHReadArgs(raw_ostream &OS) const {
296 OS << getLowerName();
297 }
298 void writePCHWrite(raw_ostream &OS) const {
299 OS << " AddString(SA->get" << getUpperName() << "(), Record);\n";
300 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000301 void writeValue(raw_ostream &OS) const {
302 OS << "\\\"\" << get" << getUpperName() << "() << \"\\\"";
303 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000304 void writeDump(raw_ostream &OS) const {
305 OS << " OS << \" \\\"\" << SA->get" << getUpperName()
306 << "() << \"\\\"\";\n";
307 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000308 };
309
310 class AlignedArgument : public Argument {
311 public:
312 AlignedArgument(Record &Arg, StringRef Attr)
313 : Argument(Arg, Attr)
314 {}
315
316 void writeAccessors(raw_ostream &OS) const {
317 OS << " bool is" << getUpperName() << "Dependent() const;\n";
318
319 OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n";
320
321 OS << " bool is" << getUpperName() << "Expr() const {\n";
322 OS << " return is" << getLowerName() << "Expr;\n";
323 OS << " }\n";
324
325 OS << " Expr *get" << getUpperName() << "Expr() const {\n";
326 OS << " assert(is" << getLowerName() << "Expr);\n";
327 OS << " return " << getLowerName() << "Expr;\n";
328 OS << " }\n";
329
330 OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n";
331 OS << " assert(!is" << getLowerName() << "Expr);\n";
332 OS << " return " << getLowerName() << "Type;\n";
333 OS << " }";
334 }
335 void writeAccessorDefinitions(raw_ostream &OS) const {
336 OS << "bool " << getAttrName() << "Attr::is" << getUpperName()
337 << "Dependent() const {\n";
338 OS << " if (is" << getLowerName() << "Expr)\n";
339 OS << " return " << getLowerName() << "Expr && (" << getLowerName()
340 << "Expr->isValueDependent() || " << getLowerName()
341 << "Expr->isTypeDependent());\n";
342 OS << " else\n";
343 OS << " return " << getLowerName()
344 << "Type->getType()->isDependentType();\n";
345 OS << "}\n";
346
347 // FIXME: Do not do the calculation here
348 // FIXME: Handle types correctly
349 // A null pointer means maximum alignment
350 // FIXME: Load the platform-specific maximum alignment, rather than
351 // 16, the x86 max.
352 OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
353 << "(ASTContext &Ctx) const {\n";
354 OS << " assert(!is" << getUpperName() << "Dependent());\n";
355 OS << " if (is" << getLowerName() << "Expr)\n";
356 OS << " return (" << getLowerName() << "Expr ? " << getLowerName()
Richard Smithcaf33902011-10-10 18:28:20 +0000357 << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue() : 16)"
Peter Collingbournebee583f2011-10-06 13:03:08 +0000358 << "* Ctx.getCharWidth();\n";
359 OS << " else\n";
360 OS << " return 0; // FIXME\n";
361 OS << "}\n";
362 }
363 void writeCloneArgs(raw_ostream &OS) const {
364 OS << "is" << getLowerName() << "Expr, is" << getLowerName()
365 << "Expr ? static_cast<void*>(" << getLowerName()
366 << "Expr) : " << getLowerName()
367 << "Type";
368 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000369 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
370 // FIXME: move the definition in Sema::InstantiateAttrs to here.
371 // In the meantime, aligned attributes are cloned.
372 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000373 void writeCtorBody(raw_ostream &OS) const {
374 OS << " if (is" << getLowerName() << "Expr)\n";
375 OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>("
376 << getUpperName() << ");\n";
377 OS << " else\n";
378 OS << " " << getLowerName()
379 << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName()
380 << ");";
381 }
382 void writeCtorInitializers(raw_ostream &OS) const {
383 OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)";
384 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000385 void writeCtorDefaultInitializers(raw_ostream &OS) const {
386 OS << "is" << getLowerName() << "Expr(false)";
387 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000388 void writeCtorParameters(raw_ostream &OS) const {
389 OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName();
390 }
391 void writeDeclarations(raw_ostream &OS) const {
392 OS << "bool is" << getLowerName() << "Expr;\n";
393 OS << "union {\n";
394 OS << "Expr *" << getLowerName() << "Expr;\n";
395 OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
396 OS << "};";
397 }
398 void writePCHReadArgs(raw_ostream &OS) const {
399 OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr";
400 }
401 void writePCHReadDecls(raw_ostream &OS) const {
402 OS << " bool is" << getLowerName() << "Expr = Record[Idx++];\n";
403 OS << " void *" << getLowerName() << "Ptr;\n";
404 OS << " if (is" << getLowerName() << "Expr)\n";
405 OS << " " << getLowerName() << "Ptr = ReadExpr(F);\n";
406 OS << " else\n";
407 OS << " " << getLowerName()
408 << "Ptr = GetTypeSourceInfo(F, Record, Idx);\n";
409 }
410 void writePCHWrite(raw_ostream &OS) const {
411 OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n";
412 OS << " if (SA->is" << getUpperName() << "Expr())\n";
413 OS << " AddStmt(SA->get" << getUpperName() << "Expr());\n";
414 OS << " else\n";
415 OS << " AddTypeSourceInfo(SA->get" << getUpperName()
416 << "Type(), Record);\n";
417 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000418 void writeValue(raw_ostream &OS) const {
Richard Smith52f04a22012-08-16 02:43:29 +0000419 OS << "\";\n"
420 << " " << getLowerName() << "Expr->printPretty(OS, 0, Policy);\n"
421 << " OS << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000422 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000423 void writeDump(raw_ostream &OS) const {
424 }
425 void writeDumpChildren(raw_ostream &OS) const {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000426 OS << " if (SA->is" << getUpperName() << "Expr()) {\n";
427 OS << " lastChild();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000428 OS << " dumpStmt(SA->get" << getUpperName() << "Expr());\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000429 OS << " } else\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000430 OS << " dumpType(SA->get" << getUpperName()
431 << "Type()->getType());\n";
432 }
Richard Trieude5cc7d2013-01-31 01:44:26 +0000433 void writeHasChildren(raw_ostream &OS) const {
434 OS << "SA->is" << getUpperName() << "Expr()";
435 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000436 };
437
438 class VariadicArgument : public Argument {
439 std::string type;
440
441 public:
442 VariadicArgument(Record &Arg, StringRef Attr, std::string T)
443 : Argument(Arg, Attr), type(T)
444 {}
445
446 std::string getType() const { return type; }
447
448 void writeAccessors(raw_ostream &OS) const {
449 OS << " typedef " << type << "* " << getLowerName() << "_iterator;\n";
450 OS << " " << getLowerName() << "_iterator " << getLowerName()
451 << "_begin() const {\n";
452 OS << " return " << getLowerName() << ";\n";
453 OS << " }\n";
454 OS << " " << getLowerName() << "_iterator " << getLowerName()
455 << "_end() const {\n";
456 OS << " return " << getLowerName() << " + " << getLowerName()
457 << "Size;\n";
458 OS << " }\n";
459 OS << " unsigned " << getLowerName() << "_size() const {\n"
DeLesley Hutchins30398dd2012-01-20 22:50:54 +0000460 << " return " << getLowerName() << "Size;\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000461 OS << " }";
462 }
463 void writeCloneArgs(raw_ostream &OS) const {
464 OS << getLowerName() << ", " << getLowerName() << "Size";
465 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000466 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
467 // This isn't elegant, but we have to go through public methods...
468 OS << "A->" << getLowerName() << "_begin(), "
469 << "A->" << getLowerName() << "_size()";
470 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000471 void writeCtorBody(raw_ostream &OS) const {
472 // FIXME: memcpy is not safe on non-trivial types.
473 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
474 << ", " << getLowerName() << "Size * sizeof(" << getType() << "));\n";
475 }
476 void writeCtorInitializers(raw_ostream &OS) const {
477 OS << getLowerName() << "Size(" << getUpperName() << "Size), "
478 << getLowerName() << "(new (Ctx, 16) " << getType() << "["
479 << getLowerName() << "Size])";
480 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000481 void writeCtorDefaultInitializers(raw_ostream &OS) const {
482 OS << getLowerName() << "Size(0), " << getLowerName() << "(0)";
483 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000484 void writeCtorParameters(raw_ostream &OS) const {
485 OS << getType() << " *" << getUpperName() << ", unsigned "
486 << getUpperName() << "Size";
487 }
488 void writeDeclarations(raw_ostream &OS) const {
489 OS << " unsigned " << getLowerName() << "Size;\n";
490 OS << " " << getType() << " *" << getLowerName() << ";";
491 }
492 void writePCHReadDecls(raw_ostream &OS) const {
493 OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000494 OS << " SmallVector<" << type << ", 4> " << getLowerName()
Peter Collingbournebee583f2011-10-06 13:03:08 +0000495 << ";\n";
496 OS << " " << getLowerName() << ".reserve(" << getLowerName()
497 << "Size);\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000498 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000499
500 std::string read = ReadPCHRecord(type);
501 OS << " " << getLowerName() << ".push_back(" << read << ");\n";
502 }
503 void writePCHReadArgs(raw_ostream &OS) const {
504 OS << getLowerName() << ".data(), " << getLowerName() << "Size";
505 }
506 void writePCHWrite(raw_ostream &OS) const{
507 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
508 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
509 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
510 << getLowerName() << "_end(); i != e; ++i)\n";
511 OS << " " << WritePCHRecord(type, "(*i)");
512 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000513 void writeValue(raw_ostream &OS) const {
514 OS << "\";\n";
515 OS << " bool isFirst = true;\n"
516 << " for (" << getAttrName() << "Attr::" << getLowerName()
517 << "_iterator i = " << getLowerName() << "_begin(), e = "
518 << getLowerName() << "_end(); i != e; ++i) {\n"
519 << " if (isFirst) isFirst = false;\n"
520 << " else OS << \", \";\n"
521 << " OS << *i;\n"
522 << " }\n";
523 OS << " OS << \"";
524 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000525 void writeDump(raw_ostream &OS) const {
526 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
527 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
528 << getLowerName() << "_end(); I != E; ++I)\n";
529 OS << " OS << \" \" << *I;\n";
530 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000531 };
532
533 class EnumArgument : public Argument {
534 std::string type;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000535 std::vector<StringRef> values, enums, uniques;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000536 public:
537 EnumArgument(Record &Arg, StringRef Attr)
538 : Argument(Arg, Attr), type(Arg.getValueAsString("Type")),
539 values(getValueAsListOfStrings(Arg, "Values")),
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000540 enums(getValueAsListOfStrings(Arg, "Enums")),
541 uniques(enums)
542 {
543 // Calculate the various enum values
544 std::sort(uniques.begin(), uniques.end());
545 uniques.erase(std::unique(uniques.begin(), uniques.end()), uniques.end());
546 // FIXME: Emit a proper error
547 assert(!uniques.empty());
548 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000549
Aaron Ballman682ee422013-09-11 19:47:58 +0000550 bool isEnumArg() const { return true; }
551
Peter Collingbournebee583f2011-10-06 13:03:08 +0000552 void writeAccessors(raw_ostream &OS) const {
553 OS << " " << type << " get" << getUpperName() << "() const {\n";
554 OS << " return " << getLowerName() << ";\n";
555 OS << " }";
556 }
557 void writeCloneArgs(raw_ostream &OS) const {
558 OS << getLowerName();
559 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000560 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
561 OS << "A->get" << getUpperName() << "()";
562 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000563 void writeCtorInitializers(raw_ostream &OS) const {
564 OS << getLowerName() << "(" << getUpperName() << ")";
565 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000566 void writeCtorDefaultInitializers(raw_ostream &OS) const {
567 OS << getLowerName() << "(" << type << "(0))";
568 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000569 void writeCtorParameters(raw_ostream &OS) const {
570 OS << type << " " << getUpperName();
571 }
572 void writeDeclarations(raw_ostream &OS) const {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000573 std::vector<StringRef>::const_iterator i = uniques.begin(),
574 e = uniques.end();
Peter Collingbournebee583f2011-10-06 13:03:08 +0000575 // The last one needs to not have a comma.
576 --e;
577
578 OS << "public:\n";
579 OS << " enum " << type << " {\n";
580 for (; i != e; ++i)
581 OS << " " << *i << ",\n";
582 OS << " " << *e << "\n";
583 OS << " };\n";
584 OS << "private:\n";
585 OS << " " << type << " " << getLowerName() << ";";
586 }
587 void writePCHReadDecls(raw_ostream &OS) const {
588 OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName()
589 << "(static_cast<" << getAttrName() << "Attr::" << type
590 << ">(Record[Idx++]));\n";
591 }
592 void writePCHReadArgs(raw_ostream &OS) const {
593 OS << getLowerName();
594 }
595 void writePCHWrite(raw_ostream &OS) const {
596 OS << "Record.push_back(SA->get" << getUpperName() << "());\n";
597 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000598 void writeValue(raw_ostream &OS) const {
599 OS << "\" << get" << getUpperName() << "() << \"";
600 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000601 void writeDump(raw_ostream &OS) const {
602 OS << " switch(SA->get" << getUpperName() << "()) {\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000603 for (std::vector<StringRef>::const_iterator I = uniques.begin(),
604 E = uniques.end(); I != E; ++I) {
605 OS << " case " << getAttrName() << "Attr::" << *I << ":\n";
606 OS << " OS << \" " << *I << "\";\n";
607 OS << " break;\n";
608 }
609 OS << " }\n";
610 }
Aaron Ballman682ee422013-09-11 19:47:58 +0000611
612 void writeConversion(raw_ostream &OS) const {
613 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
614 OS << type << " &Out) {\n";
615 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
616 OS << type << "> >(Val)\n";
617 for (size_t I = 0; I < enums.size(); ++I) {
618 OS << " .Case(\"" << values[I] << "\", ";
619 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
620 }
621 OS << " .Default(Optional<" << type << ">());\n";
622 OS << " if (R) {\n";
623 OS << " Out = *R;\n return true;\n }\n";
624 OS << " return false;\n";
625 OS << " }\n";
626 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000627 };
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000628
629 class VariadicEnumArgument: public VariadicArgument {
630 std::string type, QualifiedTypeName;
631 std::vector<StringRef> values, enums, uniques;
632 public:
633 VariadicEnumArgument(Record &Arg, StringRef Attr)
634 : VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")),
635 type(Arg.getValueAsString("Type")),
636 values(getValueAsListOfStrings(Arg, "Values")),
637 enums(getValueAsListOfStrings(Arg, "Enums")),
638 uniques(enums)
639 {
640 // Calculate the various enum values
641 std::sort(uniques.begin(), uniques.end());
642 uniques.erase(std::unique(uniques.begin(), uniques.end()), uniques.end());
643
644 QualifiedTypeName = getAttrName().str() + "Attr::" + type;
645
646 // FIXME: Emit a proper error
647 assert(!uniques.empty());
648 }
649
650 bool isVariadicEnumArg() const { return true; }
651
652 void writeDeclarations(raw_ostream &OS) const {
653 std::vector<StringRef>::const_iterator i = uniques.begin(),
654 e = uniques.end();
655 // The last one needs to not have a comma.
656 --e;
657
658 OS << "public:\n";
659 OS << " enum " << type << " {\n";
660 for (; i != e; ++i)
661 OS << " " << *i << ",\n";
662 OS << " " << *e << "\n";
663 OS << " };\n";
664 OS << "private:\n";
665
666 VariadicArgument::writeDeclarations(OS);
667 }
668 void writeDump(raw_ostream &OS) const {
669 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
670 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
671 << getLowerName() << "_end(); I != E; ++I) {\n";
672 OS << " switch(*I) {\n";
673 for (std::vector<StringRef>::const_iterator UI = uniques.begin(),
674 UE = uniques.end(); UI != UE; ++UI) {
675 OS << " case " << getAttrName() << "Attr::" << *UI << ":\n";
676 OS << " OS << \" " << *UI << "\";\n";
677 OS << " break;\n";
678 }
679 OS << " }\n";
680 OS << " }\n";
681 }
682 void writePCHReadDecls(raw_ostream &OS) const {
683 OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
684 OS << " SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName()
685 << ";\n";
686 OS << " " << getLowerName() << ".reserve(" << getLowerName()
687 << "Size);\n";
688 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
689 OS << " " << getLowerName() << ".push_back(" << "static_cast<"
690 << QualifiedTypeName << ">(Record[Idx++]));\n";
691 }
692 void writePCHWrite(raw_ostream &OS) const{
693 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
694 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
695 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
696 << getLowerName() << "_end(); i != e; ++i)\n";
697 OS << " " << WritePCHRecord(QualifiedTypeName, "(*i)");
698 }
699 void writeConversion(raw_ostream &OS) const {
700 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
701 OS << type << " &Out) {\n";
702 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
703 OS << type << "> >(Val)\n";
704 for (size_t I = 0; I < enums.size(); ++I) {
705 OS << " .Case(\"" << values[I] << "\", ";
706 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
707 }
708 OS << " .Default(Optional<" << type << ">());\n";
709 OS << " if (R) {\n";
710 OS << " Out = *R;\n return true;\n }\n";
711 OS << " return false;\n";
712 OS << " }\n";
713 }
714 };
Peter Collingbournebee583f2011-10-06 13:03:08 +0000715
716 class VersionArgument : public Argument {
717 public:
718 VersionArgument(Record &Arg, StringRef Attr)
719 : Argument(Arg, Attr)
720 {}
721
722 void writeAccessors(raw_ostream &OS) const {
723 OS << " VersionTuple get" << getUpperName() << "() const {\n";
724 OS << " return " << getLowerName() << ";\n";
725 OS << " }\n";
726 OS << " void set" << getUpperName()
727 << "(ASTContext &C, VersionTuple V) {\n";
728 OS << " " << getLowerName() << " = V;\n";
729 OS << " }";
730 }
731 void writeCloneArgs(raw_ostream &OS) const {
732 OS << "get" << getUpperName() << "()";
733 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000734 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
735 OS << "A->get" << getUpperName() << "()";
736 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000737 void writeCtorBody(raw_ostream &OS) const {
738 }
739 void writeCtorInitializers(raw_ostream &OS) const {
740 OS << getLowerName() << "(" << getUpperName() << ")";
741 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000742 void writeCtorDefaultInitializers(raw_ostream &OS) const {
743 OS << getLowerName() << "()";
744 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000745 void writeCtorParameters(raw_ostream &OS) const {
746 OS << "VersionTuple " << getUpperName();
747 }
748 void writeDeclarations(raw_ostream &OS) const {
749 OS << "VersionTuple " << getLowerName() << ";\n";
750 }
751 void writePCHReadDecls(raw_ostream &OS) const {
752 OS << " VersionTuple " << getLowerName()
753 << "= ReadVersionTuple(Record, Idx);\n";
754 }
755 void writePCHReadArgs(raw_ostream &OS) const {
756 OS << getLowerName();
757 }
758 void writePCHWrite(raw_ostream &OS) const {
759 OS << " AddVersionTuple(SA->get" << getUpperName() << "(), Record);\n";
760 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000761 void writeValue(raw_ostream &OS) const {
762 OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";
763 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000764 void writeDump(raw_ostream &OS) const {
765 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
766 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000767 };
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000768
769 class ExprArgument : public SimpleArgument {
770 public:
771 ExprArgument(Record &Arg, StringRef Attr)
772 : SimpleArgument(Arg, Attr, "Expr *")
773 {}
774
775 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
776 OS << "tempInst" << getUpperName();
777 }
778
779 void writeTemplateInstantiation(raw_ostream &OS) const {
780 OS << " " << getType() << " tempInst" << getUpperName() << ";\n";
781 OS << " {\n";
782 OS << " EnterExpressionEvaluationContext "
783 << "Unevaluated(S, Sema::Unevaluated);\n";
784 OS << " ExprResult " << "Result = S.SubstExpr("
785 << "A->get" << getUpperName() << "(), TemplateArgs);\n";
786 OS << " tempInst" << getUpperName() << " = "
787 << "Result.takeAs<Expr>();\n";
788 OS << " }\n";
789 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000790
791 void writeDump(raw_ostream &OS) const {
792 }
793
794 void writeDumpChildren(raw_ostream &OS) const {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000795 OS << " lastChild();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000796 OS << " dumpStmt(SA->get" << getUpperName() << "());\n";
797 }
Richard Trieude5cc7d2013-01-31 01:44:26 +0000798 void writeHasChildren(raw_ostream &OS) const { OS << "true"; }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000799 };
800
801 class VariadicExprArgument : public VariadicArgument {
802 public:
803 VariadicExprArgument(Record &Arg, StringRef Attr)
804 : VariadicArgument(Arg, Attr, "Expr *")
805 {}
806
807 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
808 OS << "tempInst" << getUpperName() << ", "
809 << "A->" << getLowerName() << "_size()";
810 }
811
812 void writeTemplateInstantiation(raw_ostream &OS) const {
813 OS << " " << getType() << " *tempInst" << getUpperName()
814 << " = new (C, 16) " << getType()
815 << "[A->" << getLowerName() << "_size()];\n";
816 OS << " {\n";
817 OS << " EnterExpressionEvaluationContext "
818 << "Unevaluated(S, Sema::Unevaluated);\n";
819 OS << " " << getType() << " *TI = tempInst" << getUpperName()
820 << ";\n";
821 OS << " " << getType() << " *I = A->" << getLowerName()
822 << "_begin();\n";
823 OS << " " << getType() << " *E = A->" << getLowerName()
824 << "_end();\n";
825 OS << " for (; I != E; ++I, ++TI) {\n";
826 OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n";
827 OS << " *TI = Result.takeAs<Expr>();\n";
828 OS << " }\n";
829 OS << " }\n";
830 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000831
832 void writeDump(raw_ostream &OS) const {
833 }
834
835 void writeDumpChildren(raw_ostream &OS) const {
836 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
837 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
Richard Trieude5cc7d2013-01-31 01:44:26 +0000838 << getLowerName() << "_end(); I != E; ++I) {\n";
839 OS << " if (I + 1 == E)\n";
840 OS << " lastChild();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000841 OS << " dumpStmt(*I);\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000842 OS << " }\n";
843 }
844
845 void writeHasChildren(raw_ostream &OS) const {
846 OS << "SA->" << getLowerName() << "_begin() != "
847 << "SA->" << getLowerName() << "_end()";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000848 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000849 };
Richard Smithb87c4652013-10-31 21:23:20 +0000850
851 class TypeArgument : public SimpleArgument {
852 public:
853 TypeArgument(Record &Arg, StringRef Attr)
854 : SimpleArgument(Arg, Attr, "TypeSourceInfo *")
855 {}
856
857 void writeAccessors(raw_ostream &OS) const {
858 OS << " QualType get" << getUpperName() << "() const {\n";
859 OS << " return " << getLowerName() << "->getType();\n";
860 OS << " }";
861 OS << " " << getType() << " get" << getUpperName() << "Loc() const {\n";
862 OS << " return " << getLowerName() << ";\n";
863 OS << " }";
864 }
865 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
866 OS << "A->get" << getUpperName() << "Loc()";
867 }
868 void writePCHWrite(raw_ostream &OS) const {
869 OS << " " << WritePCHRecord(
870 getType(), "SA->get" + std::string(getUpperName()) + "Loc()");
871 }
872 };
Peter Collingbournebee583f2011-10-06 13:03:08 +0000873}
874
875static Argument *createArgument(Record &Arg, StringRef Attr,
876 Record *Search = 0) {
877 if (!Search)
878 Search = &Arg;
879
880 Argument *Ptr = 0;
881 llvm::StringRef ArgName = Search->getName();
882
883 if (ArgName == "AlignedArgument") Ptr = new AlignedArgument(Arg, Attr);
884 else if (ArgName == "EnumArgument") Ptr = new EnumArgument(Arg, Attr);
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000885 else if (ArgName == "ExprArgument") Ptr = new ExprArgument(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000886 else if (ArgName == "FunctionArgument")
887 Ptr = new SimpleArgument(Arg, Attr, "FunctionDecl *");
888 else if (ArgName == "IdentifierArgument")
889 Ptr = new SimpleArgument(Arg, Attr, "IdentifierInfo *");
890 else if (ArgName == "BoolArgument") Ptr = new SimpleArgument(Arg, Attr,
891 "bool");
Aaron Ballman18a78382013-11-21 00:28:23 +0000892 else if (ArgName == "DefaultIntArgument")
893 Ptr = new DefaultSimpleArgument(Arg, Attr, "int",
894 Arg.getValueAsInt("Default"));
Peter Collingbournebee583f2011-10-06 13:03:08 +0000895 else if (ArgName == "IntArgument") Ptr = new SimpleArgument(Arg, Attr, "int");
896 else if (ArgName == "StringArgument") Ptr = new StringArgument(Arg, Attr);
Richard Smithb87c4652013-10-31 21:23:20 +0000897 else if (ArgName == "TypeArgument") Ptr = new TypeArgument(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000898 else if (ArgName == "UnsignedArgument")
899 Ptr = new SimpleArgument(Arg, Attr, "unsigned");
900 else if (ArgName == "SourceLocArgument")
901 Ptr = new SimpleArgument(Arg, Attr, "SourceLocation");
902 else if (ArgName == "VariadicUnsignedArgument")
903 Ptr = new VariadicArgument(Arg, Attr, "unsigned");
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000904 else if (ArgName == "VariadicEnumArgument")
905 Ptr = new VariadicEnumArgument(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000906 else if (ArgName == "VariadicExprArgument")
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000907 Ptr = new VariadicExprArgument(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000908 else if (ArgName == "VersionArgument")
909 Ptr = new VersionArgument(Arg, Attr);
910
911 if (!Ptr) {
Aaron Ballman18a78382013-11-21 00:28:23 +0000912 // Search in reverse order so that the most-derived type is handled first.
Peter Collingbournebee583f2011-10-06 13:03:08 +0000913 std::vector<Record*> Bases = Search->getSuperClasses();
Aaron Ballman18a78382013-11-21 00:28:23 +0000914 for (std::vector<Record*>::reverse_iterator i = Bases.rbegin(),
915 e = Bases.rend(); i != e; ++i) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000916 Ptr = createArgument(Arg, Attr, *i);
917 if (Ptr)
918 break;
919 }
920 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000921
922 if (Ptr && Arg.getValueAsBit("Optional"))
923 Ptr->setOptional(true);
924
Peter Collingbournebee583f2011-10-06 13:03:08 +0000925 return Ptr;
926}
927
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000928static void writeAvailabilityValue(raw_ostream &OS) {
929 OS << "\" << getPlatform()->getName();\n"
930 << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n"
931 << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n"
932 << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n"
933 << " if (getUnavailable()) OS << \", unavailable\";\n"
934 << " OS << \"";
935}
936
Michael Han99315932013-01-24 16:46:58 +0000937static void writePrettyPrintFunction(Record &R, std::vector<Argument*> &Args,
938 raw_ostream &OS) {
939 std::vector<Record*> Spellings = R.getValueAsListOfDefs("Spellings");
940
941 OS << "void " << R.getName() << "Attr::printPretty("
942 << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n";
943
944 if (Spellings.size() == 0) {
945 OS << "}\n\n";
946 return;
947 }
948
949 OS <<
950 " switch (SpellingListIndex) {\n"
951 " default:\n"
952 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
953 " break;\n";
954
955 for (unsigned I = 0; I < Spellings.size(); ++ I) {
956 llvm::SmallString<16> Prefix;
957 llvm::SmallString<8> Suffix;
958 // The actual spelling of the name and namespace (if applicable)
959 // of an attribute without considering prefix and suffix.
960 llvm::SmallString<64> Spelling;
961 std::string Name = Spellings[I]->getValueAsString("Name");
962 std::string Variety = Spellings[I]->getValueAsString("Variety");
963
964 if (Variety == "GNU") {
965 Prefix = " __attribute__((";
966 Suffix = "))";
967 } else if (Variety == "CXX11") {
968 Prefix = " [[";
969 Suffix = "]]";
970 std::string Namespace = Spellings[I]->getValueAsString("Namespace");
971 if (Namespace != "") {
972 Spelling += Namespace;
973 Spelling += "::";
974 }
975 } else if (Variety == "Declspec") {
976 Prefix = " __declspec(";
977 Suffix = ")";
Richard Smith0cdcc982013-01-29 01:24:26 +0000978 } else if (Variety == "Keyword") {
979 Prefix = " ";
980 Suffix = "";
Michael Han99315932013-01-24 16:46:58 +0000981 } else {
Richard Smith0cdcc982013-01-29 01:24:26 +0000982 llvm_unreachable("Unknown attribute syntax variety!");
Michael Han99315932013-01-24 16:46:58 +0000983 }
984
985 Spelling += Name;
986
987 OS <<
988 " case " << I << " : {\n"
989 " OS << \"" + Prefix.str() + Spelling.str();
990
991 if (Args.size()) OS << "(";
992 if (Spelling == "availability") {
993 writeAvailabilityValue(OS);
994 } else {
995 for (std::vector<Argument*>::const_iterator I = Args.begin(),
996 E = Args.end(); I != E; ++ I) {
997 if (I != Args.begin()) OS << ", ";
998 (*I)->writeValue(OS);
999 }
1000 }
1001
1002 if (Args.size()) OS << ")";
1003 OS << Suffix.str() + "\";\n";
1004
1005 OS <<
1006 " break;\n"
1007 " }\n";
1008 }
1009
1010 // End of the switch statement.
1011 OS << "}\n";
1012 // End of the print function.
1013 OS << "}\n\n";
1014}
1015
Michael Hanaf02bbe2013-02-01 01:19:17 +00001016/// \brief Return the index of a spelling in a spelling list.
1017static unsigned getSpellingListIndex(const std::vector<Record*> &SpellingList,
1018 const Record &Spelling) {
1019 assert(SpellingList.size() && "Spelling list is empty!");
1020
1021 for (unsigned Index = 0; Index < SpellingList.size(); ++Index) {
1022 Record *S = SpellingList[Index];
1023 if (S->getValueAsString("Variety") != Spelling.getValueAsString("Variety"))
1024 continue;
1025 if (S->getValueAsString("Variety") == "CXX11" &&
1026 S->getValueAsString("Namespace") !=
1027 Spelling.getValueAsString("Namespace"))
1028 continue;
1029 if (S->getValueAsString("Name") != Spelling.getValueAsString("Name"))
1030 continue;
1031
1032 return Index;
1033 }
1034
1035 llvm_unreachable("Unknown spelling!");
1036}
1037
1038static void writeAttrAccessorDefinition(Record &R, raw_ostream &OS) {
1039 std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors");
1040 for (std::vector<Record*>::const_iterator I = Accessors.begin(),
1041 E = Accessors.end(); I != E; ++I) {
1042 Record *Accessor = *I;
1043 std::string Name = Accessor->getValueAsString("Name");
1044 std::vector<Record*> Spellings = Accessor->getValueAsListOfDefs(
1045 "Spellings");
1046 std::vector<Record*> SpellingList = R.getValueAsListOfDefs("Spellings");
1047 assert(SpellingList.size() &&
1048 "Attribute with empty spelling list can't have accessors!");
1049
1050 OS << " bool " << Name << "() const { return SpellingListIndex == ";
1051 for (unsigned Index = 0; Index < Spellings.size(); ++Index) {
1052 OS << getSpellingListIndex(SpellingList, *Spellings[Index]);
1053 if (Index != Spellings.size() -1)
1054 OS << " ||\n SpellingListIndex == ";
1055 else
1056 OS << "; }\n";
1057 }
1058 }
1059}
1060
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001061namespace clang {
1062
1063// Emits the class definitions for attributes.
1064void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001065 emitSourceFileHeader("Attribute classes' definitions", OS);
1066
Peter Collingbournebee583f2011-10-06 13:03:08 +00001067 OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
1068 OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
1069
1070 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1071
1072 for (std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end();
1073 i != e; ++i) {
1074 Record &R = **i;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001075
1076 if (!R.getValueAsBit("ASTNode"))
1077 continue;
1078
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001079 const std::vector<Record *> Supers = R.getSuperClasses();
1080 assert(!Supers.empty() && "Forgot to specify a superclass for the attr");
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001081 std::string SuperName;
1082 for (std::vector<Record *>::const_reverse_iterator I = Supers.rbegin(),
1083 E = Supers.rend(); I != E; ++I) {
1084 const Record &R = **I;
Aaron Ballman080cad72013-07-31 02:20:22 +00001085 if (R.getName() != "TargetSpecificAttr" && SuperName.empty())
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001086 SuperName = R.getName();
1087 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001088
1089 OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
1090
1091 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1092 std::vector<Argument*> Args;
1093 std::vector<Argument*>::iterator ai, ae;
1094 Args.reserve(ArgRecords.size());
1095
1096 for (std::vector<Record*>::iterator ri = ArgRecords.begin(),
1097 re = ArgRecords.end();
1098 ri != re; ++ri) {
1099 Record &ArgRecord = **ri;
1100 Argument *Arg = createArgument(ArgRecord, R.getName());
1101 assert(Arg);
1102 Args.push_back(Arg);
1103
1104 Arg->writeDeclarations(OS);
1105 OS << "\n\n";
1106 }
1107
1108 ae = Args.end();
1109
1110 OS << "\n public:\n";
1111 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
1112
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001113 bool HasOpt = false;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001114 for (ai = Args.begin(); ai != ae; ++ai) {
1115 OS << " , ";
1116 (*ai)->writeCtorParameters(OS);
1117 OS << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001118 if ((*ai)->isOptional())
1119 HasOpt = true;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001120 }
Michael Han99315932013-01-24 16:46:58 +00001121
1122 OS << " , ";
1123 OS << "unsigned SI = 0\n";
1124
Peter Collingbournebee583f2011-10-06 13:03:08 +00001125 OS << " )\n";
Michael Han99315932013-01-24 16:46:58 +00001126 OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI)\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001127
1128 for (ai = Args.begin(); ai != ae; ++ai) {
1129 OS << " , ";
1130 (*ai)->writeCtorInitializers(OS);
1131 OS << "\n";
1132 }
1133
1134 OS << " {\n";
1135
1136 for (ai = Args.begin(); ai != ae; ++ai) {
1137 (*ai)->writeCtorBody(OS);
1138 OS << "\n";
1139 }
1140 OS << " }\n\n";
1141
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001142 // If there are optional arguments, write out a constructor that elides the
1143 // optional arguments as well.
1144 if (HasOpt) {
1145 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
1146 for (ai = Args.begin(); ai != ae; ++ai) {
1147 if (!(*ai)->isOptional()) {
1148 OS << " , ";
1149 (*ai)->writeCtorParameters(OS);
1150 OS << "\n";
1151 }
1152 }
1153
1154 OS << " , ";
1155 OS << "unsigned SI = 0\n";
1156
1157 OS << " )\n";
1158 OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI)\n";
1159
1160 for (ai = Args.begin(); ai != ae; ++ai) {
1161 OS << " , ";
1162 (*ai)->writeCtorDefaultInitializers(OS);
1163 OS << "\n";
1164 }
1165
1166 OS << " {\n";
1167
1168 for (ai = Args.begin(); ai != ae; ++ai) {
1169 if (!(*ai)->isOptional()) {
1170 (*ai)->writeCtorBody(OS);
1171 OS << "\n";
1172 }
1173 }
1174 OS << " }\n\n";
1175 }
1176
Peter Collingbournebee583f2011-10-06 13:03:08 +00001177 OS << " virtual " << R.getName() << "Attr *clone (ASTContext &C) const;\n";
Michael Han38d96ab2013-01-27 00:06:24 +00001178 OS << " virtual void printPretty(raw_ostream &OS,\n"
Richard Smith52f04a22012-08-16 02:43:29 +00001179 << " const PrintingPolicy &Policy) const;\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001180
Michael Hanaf02bbe2013-02-01 01:19:17 +00001181 writeAttrAccessorDefinition(R, OS);
1182
Peter Collingbournebee583f2011-10-06 13:03:08 +00001183 for (ai = Args.begin(); ai != ae; ++ai) {
1184 (*ai)->writeAccessors(OS);
1185 OS << "\n\n";
Aaron Ballman682ee422013-09-11 19:47:58 +00001186
1187 if ((*ai)->isEnumArg()) {
1188 EnumArgument *EA = (EnumArgument *)*ai;
1189 EA->writeConversion(OS);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001190 } else if ((*ai)->isVariadicEnumArg()) {
1191 VariadicEnumArgument *VEA = (VariadicEnumArgument *)*ai;
1192 VEA->writeConversion(OS);
Aaron Ballman682ee422013-09-11 19:47:58 +00001193 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001194 }
1195
Jakob Stoklund Olesen6f2288b62012-01-13 04:57:47 +00001196 OS << R.getValueAsString("AdditionalMembers");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001197 OS << "\n\n";
1198
1199 OS << " static bool classof(const Attr *A) { return A->getKind() == "
1200 << "attr::" << R.getName() << "; }\n";
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001201
1202 bool LateParsed = R.getValueAsBit("LateParsed");
1203 OS << " virtual bool isLateParsed() const { return "
1204 << LateParsed << "; }\n";
1205
Peter Collingbournebee583f2011-10-06 13:03:08 +00001206 OS << "};\n\n";
1207 }
1208
1209 OS << "#endif\n";
1210}
1211
Richard Smith66e71682013-10-24 01:07:54 +00001212static bool isIdentifierArgument(Record *Arg) {
1213 return !Arg->getSuperClasses().empty() &&
1214 llvm::StringSwitch<bool>(Arg->getSuperClasses().back()->getName())
1215 .Case("IdentifierArgument", true)
1216 .Case("EnumArgument", true)
1217 .Default(false);
1218}
1219
Aaron Ballman4768b312013-11-04 12:55:56 +00001220/// \brief Emits the first-argument-is-type property for attributes.
1221void EmitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) {
1222 emitSourceFileHeader("llvm::StringSwitch code to match attributes with a "
1223 "type argument", OS);
1224
1225 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
1226
1227 for (std::vector<Record *>::iterator I = Attrs.begin(), E = Attrs.end();
1228 I != E; ++I) {
1229 Record &Attr = **I;
1230
1231 // Determine whether the first argument is a type.
1232 std::vector<Record *> Args = Attr.getValueAsListOfDefs("Args");
1233 if (Args.empty())
1234 continue;
1235
1236 if (Args[0]->getSuperClasses().back()->getName() != "TypeArgument")
1237 continue;
1238
1239 // All these spellings take a single type argument.
1240 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
1241 std::set<std::string> Emitted;
1242 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
1243 E = Spellings.end(); I != E; ++I) {
1244 if (Emitted.insert((*I)->getValueAsString("Name")).second)
1245 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", "
1246 << "true" << ")\n";
1247 }
1248 }
1249}
1250
Richard Smith66e71682013-10-24 01:07:54 +00001251// Emits the first-argument-is-identifier property for attributes.
1252void EmitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) {
Douglas Gregord2472d42013-05-02 23:25:32 +00001253 emitSourceFileHeader("llvm::StringSwitch code to match attributes with "
Richard Smith66e71682013-10-24 01:07:54 +00001254 "an identifier argument", OS);
Douglas Gregord2472d42013-05-02 23:25:32 +00001255
1256 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1257
1258 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1259 I != E; ++I) {
1260 Record &Attr = **I;
1261
Richard Smith66e71682013-10-24 01:07:54 +00001262 // Determine whether the first argument is an identifier.
Douglas Gregord2472d42013-05-02 23:25:32 +00001263 std::vector<Record *> Args = Attr.getValueAsListOfDefs("Args");
Richard Smith66e71682013-10-24 01:07:54 +00001264 if (Args.empty() || !isIdentifierArgument(Args[0]))
Douglas Gregord2472d42013-05-02 23:25:32 +00001265 continue;
1266
Richard Smith66e71682013-10-24 01:07:54 +00001267 // All these spellings take an identifier argument.
Douglas Gregord2472d42013-05-02 23:25:32 +00001268 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
Richard Smith66e71682013-10-24 01:07:54 +00001269 std::set<std::string> Emitted;
Douglas Gregord2472d42013-05-02 23:25:32 +00001270 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
1271 E = Spellings.end(); I != E; ++I) {
Richard Smith66e71682013-10-24 01:07:54 +00001272 if (Emitted.insert((*I)->getValueAsString("Name")).second)
1273 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", "
1274 << "true" << ")\n";
Douglas Gregord2472d42013-05-02 23:25:32 +00001275 }
1276 }
1277}
1278
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001279// Emits the class method definitions for attributes.
1280void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001281 emitSourceFileHeader("Attribute classes' member function definitions", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001282
1283 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1284 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ri, re;
1285 std::vector<Argument*>::iterator ai, ae;
1286
1287 for (; i != e; ++i) {
1288 Record &R = **i;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001289
1290 if (!R.getValueAsBit("ASTNode"))
1291 continue;
1292
Peter Collingbournebee583f2011-10-06 13:03:08 +00001293 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1294 std::vector<Argument*> Args;
1295 for (ri = ArgRecords.begin(), re = ArgRecords.end(); ri != re; ++ri)
1296 Args.push_back(createArgument(**ri, R.getName()));
1297
1298 for (ai = Args.begin(), ae = Args.end(); ai != ae; ++ai)
1299 (*ai)->writeAccessorDefinitions(OS);
1300
1301 OS << R.getName() << "Attr *" << R.getName()
1302 << "Attr::clone(ASTContext &C) const {\n";
1303 OS << " return new (C) " << R.getName() << "Attr(getLocation(), C";
1304 for (ai = Args.begin(); ai != ae; ++ai) {
1305 OS << ", ";
1306 (*ai)->writeCloneArgs(OS);
1307 }
Richard Smitha5aaca92013-01-29 04:21:28 +00001308 OS << ", getSpellingListIndex());\n}\n\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001309
Michael Han99315932013-01-24 16:46:58 +00001310 writePrettyPrintFunction(R, Args, OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001311 }
1312}
1313
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001314} // end namespace clang
1315
Peter Collingbournebee583f2011-10-06 13:03:08 +00001316static void EmitAttrList(raw_ostream &OS, StringRef Class,
1317 const std::vector<Record*> &AttrList) {
1318 std::vector<Record*>::const_iterator i = AttrList.begin(), e = AttrList.end();
1319
1320 if (i != e) {
1321 // Move the end iterator back to emit the last attribute.
Douglas Gregorb2daf842012-05-02 15:56:52 +00001322 for(--e; i != e; ++i) {
1323 if (!(*i)->getValueAsBit("ASTNode"))
1324 continue;
1325
Peter Collingbournebee583f2011-10-06 13:03:08 +00001326 OS << Class << "(" << (*i)->getName() << ")\n";
Douglas Gregorb2daf842012-05-02 15:56:52 +00001327 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001328
1329 OS << "LAST_" << Class << "(" << (*i)->getName() << ")\n\n";
1330 }
1331}
1332
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001333namespace clang {
1334
1335// Emits the enumeration list for attributes.
1336void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001337 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001338
1339 OS << "#ifndef LAST_ATTR\n";
1340 OS << "#define LAST_ATTR(NAME) ATTR(NAME)\n";
1341 OS << "#endif\n\n";
1342
1343 OS << "#ifndef INHERITABLE_ATTR\n";
1344 OS << "#define INHERITABLE_ATTR(NAME) ATTR(NAME)\n";
1345 OS << "#endif\n\n";
1346
1347 OS << "#ifndef LAST_INHERITABLE_ATTR\n";
1348 OS << "#define LAST_INHERITABLE_ATTR(NAME) INHERITABLE_ATTR(NAME)\n";
1349 OS << "#endif\n\n";
1350
1351 OS << "#ifndef INHERITABLE_PARAM_ATTR\n";
1352 OS << "#define INHERITABLE_PARAM_ATTR(NAME) ATTR(NAME)\n";
1353 OS << "#endif\n\n";
1354
1355 OS << "#ifndef LAST_INHERITABLE_PARAM_ATTR\n";
1356 OS << "#define LAST_INHERITABLE_PARAM_ATTR(NAME)"
1357 " INHERITABLE_PARAM_ATTR(NAME)\n";
1358 OS << "#endif\n\n";
1359
Reid Kleckner42903612013-05-14 20:55:49 +00001360 OS << "#ifndef MS_INHERITANCE_ATTR\n";
1361 OS << "#define MS_INHERITANCE_ATTR(NAME) INHERITABLE_ATTR(NAME)\n";
Reid Kleckner6a476082013-03-26 18:30:28 +00001362 OS << "#endif\n\n";
1363
Reid Kleckner42903612013-05-14 20:55:49 +00001364 OS << "#ifndef LAST_MS_INHERITANCE_ATTR\n";
1365 OS << "#define LAST_MS_INHERITANCE_ATTR(NAME)"
1366 " MS_INHERITANCE_ATTR(NAME)\n";
Reid Kleckner6a476082013-03-26 18:30:28 +00001367 OS << "#endif\n\n";
1368
Peter Collingbournebee583f2011-10-06 13:03:08 +00001369 Record *InhClass = Records.getClass("InheritableAttr");
1370 Record *InhParamClass = Records.getClass("InheritableParamAttr");
Reid Kleckner6a476082013-03-26 18:30:28 +00001371 Record *MSInheritanceClass = Records.getClass("MSInheritanceAttr");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001372 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
Reid Kleckner6a476082013-03-26 18:30:28 +00001373 NonInhAttrs, InhAttrs, InhParamAttrs, MSInhAttrs;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001374 for (std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end();
1375 i != e; ++i) {
Douglas Gregorb2daf842012-05-02 15:56:52 +00001376 if (!(*i)->getValueAsBit("ASTNode"))
1377 continue;
1378
Peter Collingbournebee583f2011-10-06 13:03:08 +00001379 if ((*i)->isSubClassOf(InhParamClass))
1380 InhParamAttrs.push_back(*i);
Reid Kleckner6a476082013-03-26 18:30:28 +00001381 else if ((*i)->isSubClassOf(MSInheritanceClass))
1382 MSInhAttrs.push_back(*i);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001383 else if ((*i)->isSubClassOf(InhClass))
1384 InhAttrs.push_back(*i);
1385 else
1386 NonInhAttrs.push_back(*i);
1387 }
1388
1389 EmitAttrList(OS, "INHERITABLE_PARAM_ATTR", InhParamAttrs);
Reid Kleckner42903612013-05-14 20:55:49 +00001390 EmitAttrList(OS, "MS_INHERITANCE_ATTR", MSInhAttrs);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001391 EmitAttrList(OS, "INHERITABLE_ATTR", InhAttrs);
1392 EmitAttrList(OS, "ATTR", NonInhAttrs);
1393
1394 OS << "#undef LAST_ATTR\n";
1395 OS << "#undef INHERITABLE_ATTR\n";
Reid Kleckner42903612013-05-14 20:55:49 +00001396 OS << "#undef MS_INHERITANCE_ATTR\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001397 OS << "#undef LAST_INHERITABLE_ATTR\n";
1398 OS << "#undef LAST_INHERITABLE_PARAM_ATTR\n";
Reid Kleckner42903612013-05-14 20:55:49 +00001399 OS << "#undef LAST_MS_INHERITANCE_ATTR\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001400 OS << "#undef ATTR\n";
1401}
1402
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001403// Emits the code to read an attribute from a precompiled header.
1404void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001405 emitSourceFileHeader("Attribute deserialization code", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001406
1407 Record *InhClass = Records.getClass("InheritableAttr");
1408 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
1409 ArgRecords;
1410 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ai, ae;
1411 std::vector<Argument*> Args;
1412 std::vector<Argument*>::iterator ri, re;
1413
1414 OS << " switch (Kind) {\n";
1415 OS << " default:\n";
1416 OS << " assert(0 && \"Unknown attribute!\");\n";
1417 OS << " break;\n";
1418 for (; i != e; ++i) {
1419 Record &R = **i;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001420 if (!R.getValueAsBit("ASTNode"))
1421 continue;
1422
Peter Collingbournebee583f2011-10-06 13:03:08 +00001423 OS << " case attr::" << R.getName() << ": {\n";
1424 if (R.isSubClassOf(InhClass))
1425 OS << " bool isInherited = Record[Idx++];\n";
1426 ArgRecords = R.getValueAsListOfDefs("Args");
1427 Args.clear();
1428 for (ai = ArgRecords.begin(), ae = ArgRecords.end(); ai != ae; ++ai) {
1429 Argument *A = createArgument(**ai, R.getName());
1430 Args.push_back(A);
1431 A->writePCHReadDecls(OS);
1432 }
1433 OS << " New = new (Context) " << R.getName() << "Attr(Range, Context";
1434 for (ri = Args.begin(), re = Args.end(); ri != re; ++ri) {
1435 OS << ", ";
1436 (*ri)->writePCHReadArgs(OS);
1437 }
1438 OS << ");\n";
1439 if (R.isSubClassOf(InhClass))
1440 OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
1441 OS << " break;\n";
1442 OS << " }\n";
1443 }
1444 OS << " }\n";
1445}
1446
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001447// Emits the code to write an attribute to a precompiled header.
1448void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001449 emitSourceFileHeader("Attribute serialization code", OS);
1450
Peter Collingbournebee583f2011-10-06 13:03:08 +00001451 Record *InhClass = Records.getClass("InheritableAttr");
1452 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
1453 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ai, ae;
1454
1455 OS << " switch (A->getKind()) {\n";
1456 OS << " default:\n";
1457 OS << " llvm_unreachable(\"Unknown attribute kind!\");\n";
1458 OS << " break;\n";
1459 for (; i != e; ++i) {
1460 Record &R = **i;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001461 if (!R.getValueAsBit("ASTNode"))
1462 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001463 OS << " case attr::" << R.getName() << ": {\n";
1464 Args = R.getValueAsListOfDefs("Args");
1465 if (R.isSubClassOf(InhClass) || !Args.empty())
1466 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
1467 << "Attr>(A);\n";
1468 if (R.isSubClassOf(InhClass))
1469 OS << " Record.push_back(SA->isInherited());\n";
1470 for (ai = Args.begin(), ae = Args.end(); ai != ae; ++ai)
1471 createArgument(**ai, R.getName())->writePCHWrite(OS);
1472 OS << " break;\n";
1473 OS << " }\n";
1474 }
1475 OS << " }\n";
1476}
1477
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001478// Emits the list of spellings for attributes.
1479void EmitClangAttrSpellingList(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001480 emitSourceFileHeader("llvm::StringSwitch code to match all known attributes",
1481 OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001482
1483 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1484
1485 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
1486 Record &Attr = **I;
1487
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001488 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001489
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001490 for (std::vector<Record*>::const_iterator I = Spellings.begin(), E = Spellings.end(); I != E; ++I) {
1491 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", true)\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001492 }
1493 }
1494
1495}
1496
Michael Han99315932013-01-24 16:46:58 +00001497void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001498 emitSourceFileHeader("Code to translate different attribute spellings "
1499 "into internal identifiers", OS);
Michael Han99315932013-01-24 16:46:58 +00001500
1501 OS <<
1502 " unsigned Index = 0;\n"
1503 " switch (AttrKind) {\n"
1504 " default:\n"
1505 " llvm_unreachable(\"Unknown attribute kind!\");\n"
1506 " break;\n";
1507
1508 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1509 for (std::vector<Record*>::const_iterator I = Attrs.begin(), E = Attrs.end();
1510 I != E; ++I) {
1511 Record &R = **I;
1512 // We only care about attributes that participate in Sema checking, so
1513 // skip those attributes that are not able to make their way to Sema.
1514 if (!R.getValueAsBit("SemaHandler"))
1515 continue;
1516
1517 std::vector<Record*> Spellings = R.getValueAsListOfDefs("Spellings");
Richard Smith852e9ce2013-11-27 01:46:48 +00001518 OS << " case AT_" << R.getName() << " : {\n";
1519 for (unsigned I = 0; I < Spellings.size(); ++ I) {
1520 SmallString<16> Namespace;
1521 if (Spellings[I]->getValueAsString("Variety") == "CXX11")
1522 Namespace = Spellings[I]->getValueAsString("Namespace");
1523 else
1524 Namespace = "";
Michael Han99315932013-01-24 16:46:58 +00001525
Richard Smith852e9ce2013-11-27 01:46:48 +00001526 OS << " if (Name == \""
1527 << Spellings[I]->getValueAsString("Name") << "\" && "
1528 << "SyntaxUsed == "
1529 << StringSwitch<unsigned>(Spellings[I]->getValueAsString("Variety"))
1530 .Case("GNU", 0)
1531 .Case("CXX11", 1)
1532 .Case("Declspec", 2)
1533 .Case("Keyword", 3)
1534 .Default(0)
1535 << " && Scope == \"" << Namespace << "\")\n"
1536 << " return " << I << ";\n";
Michael Han99315932013-01-24 16:46:58 +00001537 }
Richard Smith852e9ce2013-11-27 01:46:48 +00001538
1539 OS << " break;\n";
1540 OS << " }\n";
Michael Han99315932013-01-24 16:46:58 +00001541 }
1542
1543 OS << " }\n";
1544 OS << " return Index;\n";
1545}
1546
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001547// Emits the LateParsed property for attributes.
1548void EmitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001549 emitSourceFileHeader("llvm::StringSwitch code to match late parsed "
1550 "attributes", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001551
1552 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1553
1554 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1555 I != E; ++I) {
1556 Record &Attr = **I;
1557
1558 bool LateParsed = Attr.getValueAsBit("LateParsed");
1559
1560 if (LateParsed) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001561 std::vector<Record*> Spellings =
1562 Attr.getValueAsListOfDefs("Spellings");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001563
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001564 // FIXME: Handle non-GNU attributes
1565 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
Peter Collingbournebee583f2011-10-06 13:03:08 +00001566 E = Spellings.end(); I != E; ++I) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001567 if ((*I)->getValueAsString("Variety") != "GNU")
1568 continue;
1569 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", "
1570 << LateParsed << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001571 }
1572 }
1573 }
1574}
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001575
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001576// Emits code to instantiate dependent attributes on templates.
1577void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001578 emitSourceFileHeader("Template instantiation code for attributes", OS);
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001579
1580 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1581
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00001582 OS << "namespace clang {\n"
1583 << "namespace sema {\n\n"
1584 << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, "
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001585 << "Sema &S,\n"
1586 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"
1587 << " switch (At->getKind()) {\n"
1588 << " default:\n"
1589 << " break;\n";
1590
1591 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1592 I != E; ++I) {
1593 Record &R = **I;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001594 if (!R.getValueAsBit("ASTNode"))
1595 continue;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001596
1597 OS << " case attr::" << R.getName() << ": {\n";
Rafael Espindola7f90b7d2012-05-15 14:09:55 +00001598 bool ShouldClone = R.getValueAsBit("Clone");
1599
1600 if (!ShouldClone) {
1601 OS << " return NULL;\n";
1602 OS << " }\n";
1603 continue;
1604 }
1605
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001606 OS << " const " << R.getName() << "Attr *A = cast<"
1607 << R.getName() << "Attr>(At);\n";
1608 bool TDependent = R.getValueAsBit("TemplateDependent");
1609
1610 if (!TDependent) {
1611 OS << " return A->clone(C);\n";
1612 OS << " }\n";
1613 continue;
1614 }
1615
1616 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1617 std::vector<Argument*> Args;
1618 std::vector<Argument*>::iterator ai, ae;
1619 Args.reserve(ArgRecords.size());
1620
1621 for (std::vector<Record*>::iterator ri = ArgRecords.begin(),
1622 re = ArgRecords.end();
1623 ri != re; ++ri) {
1624 Record &ArgRecord = **ri;
1625 Argument *Arg = createArgument(ArgRecord, R.getName());
1626 assert(Arg);
1627 Args.push_back(Arg);
1628 }
1629 ae = Args.end();
1630
1631 for (ai = Args.begin(); ai != ae; ++ai) {
1632 (*ai)->writeTemplateInstantiation(OS);
1633 }
1634 OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C";
1635 for (ai = Args.begin(); ai != ae; ++ai) {
1636 OS << ", ";
1637 (*ai)->writeTemplateInstantiationArgs(OS);
1638 }
1639 OS << ");\n }\n";
1640 }
1641 OS << " } // end switch\n"
1642 << " llvm_unreachable(\"Unknown attribute!\");\n"
1643 << " return 0;\n"
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00001644 << "}\n\n"
1645 << "} // end namespace sema\n"
1646 << "} // end namespace clang\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001647}
1648
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001649typedef std::vector<std::pair<std::string, Record *> > ParsedAttrMap;
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001650
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001651static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records) {
Michael Han4a045172012-03-07 00:12:16 +00001652 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001653 ParsedAttrMap R;
Michael Han4a045172012-03-07 00:12:16 +00001654 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1655 I != E; ++I) {
1656 Record &Attr = **I;
Richard Smith852e9ce2013-11-27 01:46:48 +00001657 if (Attr.getValueAsBit("SemaHandler")) {
1658 StringRef AttrName = Attr.getName();
1659 AttrName = NormalizeAttrName(AttrName);
1660 R.push_back(std::make_pair(AttrName.str(), *I));
Michael Han4a045172012-03-07 00:12:16 +00001661 }
1662 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001663 return R;
1664}
1665
1666// Emits the list of parsed attributes.
1667void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) {
1668 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
1669
1670 OS << "#ifndef PARSED_ATTR\n";
1671 OS << "#define PARSED_ATTR(NAME) NAME\n";
1672 OS << "#endif\n\n";
1673
1674 ParsedAttrMap Names = getParsedAttrList(Records);
1675 for (ParsedAttrMap::iterator I = Names.begin(), E = Names.end(); I != E;
1676 ++I) {
1677 OS << "PARSED_ATTR(" << I->first << ")\n";
1678 }
1679}
1680
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001681static void emitArgInfo(const Record &R, std::stringstream &OS) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001682 // This function will count the number of arguments specified for the
1683 // attribute and emit the number of required arguments followed by the
1684 // number of optional arguments.
1685 std::vector<Record *> Args = R.getValueAsListOfDefs("Args");
1686 unsigned ArgCount = 0, OptCount = 0;
1687 for (std::vector<Record *>::const_iterator I = Args.begin(), E = Args.end();
1688 I != E; ++I) {
1689 const Record &Arg = **I;
1690 Arg.getValueAsBit("Optional") ? ++OptCount : ++ArgCount;
1691 }
1692 OS << ArgCount << ", " << OptCount;
1693}
1694
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001695static void GenerateDefaultAppertainsTo(raw_ostream &OS) {
1696 OS << "static bool DefaultAppertainsTo(Sema &, const AttributeList &,";
1697 OS << "const Decl *) {\n";
1698 OS << " return true;\n";
1699 OS << "}\n\n";
1700}
1701
1702static std::string CalculateDiagnostic(const Record &S) {
1703 // If the SubjectList object has a custom diagnostic associated with it,
1704 // return that directly.
1705 std::string CustomDiag = S.getValueAsString("CustomDiag");
1706 if (!CustomDiag.empty())
1707 return CustomDiag;
1708
1709 // Given the list of subjects, determine what diagnostic best fits.
1710 enum {
1711 Func = 1U << 0,
1712 Var = 1U << 1,
1713 ObjCMethod = 1U << 2,
1714 Param = 1U << 3,
1715 Class = 1U << 4,
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00001716 GenericRecord = 1U << 5,
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001717 Type = 1U << 6,
1718 ObjCIVar = 1U << 7,
1719 ObjCProp = 1U << 8,
1720 ObjCInterface = 1U << 9,
1721 Block = 1U << 10,
1722 Namespace = 1U << 11,
1723 FuncTemplate = 1U << 12,
1724 Field = 1U << 13,
1725 CXXMethod = 1U << 14
1726 };
1727 uint32_t SubMask = 0;
1728
1729 std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects");
1730 for (std::vector<Record *>::const_iterator I = Subjects.begin(),
1731 E = Subjects.end(); I != E; ++I) {
1732 uint32_t V = StringSwitch<uint32_t>((*I)->getName())
1733 .Case("Function", Func)
1734 .Case("Var", Var)
1735 .Case("ObjCMethod", ObjCMethod)
1736 .Case("ParmVar", Param)
1737 .Case("TypedefName", Type)
1738 .Case("ObjCIvar", ObjCIVar)
1739 .Case("ObjCProperty", ObjCProp)
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00001740 .Case("Record", GenericRecord)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001741 .Case("ObjCInterface", ObjCInterface)
1742 .Case("Block", Block)
1743 .Case("CXXRecord", Class)
1744 .Case("Namespace", Namespace)
1745 .Case("FunctionTemplate", FuncTemplate)
1746 .Case("Field", Field)
1747 .Case("CXXMethod", CXXMethod)
1748 .Default(0);
1749 if (!V) {
1750 // Something wasn't in our mapping, so be helpful and let the developer
1751 // know about it.
1752 PrintFatalError((*I)->getLoc(), "Unknown subject type: " +
1753 (*I)->getName());
1754 return "";
1755 }
1756
1757 SubMask |= V;
1758 }
1759
1760 switch (SubMask) {
1761 // For the simple cases where there's only a single entry in the mask, we
1762 // don't have to resort to bit fiddling.
1763 case Func: return "ExpectedFunction";
1764 case Var: return "ExpectedVariable";
1765 case Param: return "ExpectedParameter";
1766 case Class: return "ExpectedClass";
1767 case CXXMethod:
1768 // FIXME: Currently, this maps to ExpectedMethod based on existing code,
1769 // but should map to something a bit more accurate at some point.
1770 case ObjCMethod: return "ExpectedMethod";
1771 case Type: return "ExpectedType";
1772 case ObjCInterface: return "ExpectedObjectiveCInterface";
1773
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00001774 // "GenericRecord" means struct, union or class; check the language options
1775 // and if not compiling for C++, strip off the class part. Note that this
1776 // relies on the fact that the context for this declares "Sema &S".
1777 case GenericRecord:
Aaron Ballman17046b82013-11-27 19:16:55 +00001778 return "(S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass : "
1779 "ExpectedStructOrUnion)";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001780 case Func | ObjCMethod | Block: return "ExpectedFunctionMethodOrBlock";
1781 case Func | ObjCMethod | Class: return "ExpectedFunctionMethodOrClass";
1782 case Func | Param:
1783 case Func | ObjCMethod | Param: return "ExpectedFunctionMethodOrParameter";
1784 case Func | FuncTemplate:
1785 case Func | ObjCMethod: return "ExpectedFunctionOrMethod";
1786 case Func | Var: return "ExpectedVariableOrFunction";
1787 case ObjCMethod | ObjCProp: return "ExpectedMethodOrProperty";
1788 case Field | Var: return "ExpectedFieldOrGlobalVar";
1789 }
1790
1791 PrintFatalError(S.getLoc(),
1792 "Could not deduce diagnostic argument for Attr subjects");
1793
1794 return "";
1795}
1796
1797static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) {
1798 // If the attribute does not contain a Subjects definition, then use the
1799 // default appertainsTo logic.
1800 if (Attr.isValueUnset("Subjects"))
1801 return "DefaultAppertainsTo";
1802
1803 const Record *SubjectObj = Attr.getValueAsDef("Subjects");
1804 std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
1805
1806 // If the list of subjects is empty, it is assumed that the attribute
1807 // appertains to everything.
1808 if (Subjects.empty())
1809 return "DefaultAppertainsTo";
1810
1811 // If any of the subjects are a SubsetSubject derivative, bail out for now
1812 // as though it was using custom parsing.
1813 bool HasSubsetSubject = false;
1814 bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn");
1815
1816 // Otherwise, generate an appertainsTo check specific to this attribute which
1817 // checks all of the given subjects against the Decl passed in. Return the
1818 // name of that check to the caller.
1819 std::string FnName = Attr.getName() + "AppertainsTo";
1820 std::stringstream SS;
1821 SS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr, ";
1822 SS << "const Decl *D) {\n";
1823 SS << " if (";
1824 for (std::vector<Record *>::const_iterator I = Subjects.begin(),
1825 E = Subjects.end(); I != E; ++I) {
1826 if ((*I)->isSubClassOf("SubsetSubject"))
1827 HasSubsetSubject = true;
1828
1829 SS << "!isa<" << (*I)->getName() << "Decl>(D)";
1830 if (I + 1 != E)
1831 SS << " && ";
1832 }
1833 SS << ") {\n";
1834 SS << " S.Diag(Attr.getLoc(), diag::";
1835 SS << (Warn ? "warn_attribute_wrong_decl_type" :
1836 "err_attribute_wrong_decl_type");
1837 SS << ")\n";
1838 SS << " << Attr.getName() << ";
1839 SS << CalculateDiagnostic(*SubjectObj) << ";\n";
1840 SS << " return false;\n";
1841 SS << " }\n";
1842 SS << " return true;\n";
1843 SS << "}\n\n";
1844
1845 if (HasSubsetSubject)
1846 return "DefaultAppertainsTo";
1847
1848 OS << SS.str();
1849 return FnName;
1850}
1851
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001852/// Emits the parsed attribute helpers
1853void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
1854 emitSourceFileHeader("Parsed attribute helpers", OS);
1855
1856 ParsedAttrMap Attrs = getParsedAttrList(Records);
1857
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001858 // Generate the default appertainsTo diagnostic method.
1859 GenerateDefaultAppertainsTo(OS);
1860
1861 // Generate the appertainsTo diagnostic methods and write their names into
1862 // another mapping. At the same time, generate the AttrInfoMap object
1863 // contents. Due to the reliance on generated code, use separate streams so
1864 // that code will not be interleaved.
1865 std::stringstream SS;
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001866 for (ParsedAttrMap::iterator I = Attrs.begin(), E = Attrs.end(); I != E;
1867 ++I) {
1868 // We need to generate struct instances based off ParsedAttrInfo from
1869 // AttributeList.cpp.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001870 SS << " { ";
1871 emitArgInfo(*I->second, SS);
1872 SS << ", " << I->second->getValueAsBit("HasCustomParsing");
1873 SS << ", " << GenerateAppertainsTo(*I->second, OS);
1874 SS << " }";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001875
1876 if (I + 1 != E)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001877 SS << ",";
1878
1879 SS << " // AT_" << I->first << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001880 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001881
1882 OS << "static const ParsedAttrInfo AttrInfoMap[AttributeList::UnknownAttribute + 1] = {\n";
1883 OS << SS.str();
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001884 OS << "};\n\n";
Michael Han4a045172012-03-07 00:12:16 +00001885}
1886
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001887// Emits the kind list of parsed attributes
1888void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001889 emitSourceFileHeader("Attribute name matcher", OS);
1890
Michael Han4a045172012-03-07 00:12:16 +00001891 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1892
Douglas Gregor377f99b2012-05-02 17:33:51 +00001893 std::vector<StringMatcher::StringPair> Matches;
Michael Han4a045172012-03-07 00:12:16 +00001894 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1895 I != E; ++I) {
1896 Record &Attr = **I;
Richard Smith852e9ce2013-11-27 01:46:48 +00001897
Michael Han4a045172012-03-07 00:12:16 +00001898 bool SemaHandler = Attr.getValueAsBit("SemaHandler");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00001899 bool Ignored = Attr.getValueAsBit("Ignored");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00001900 if (SemaHandler || Ignored) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001901 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
Michael Han4a045172012-03-07 00:12:16 +00001902
Richard Smith852e9ce2013-11-27 01:46:48 +00001903 StringRef AttrName = NormalizeAttrName(StringRef(Attr.getName()));
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001904 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
Michael Han4a045172012-03-07 00:12:16 +00001905 E = Spellings.end(); I != E; ++I) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001906 std::string RawSpelling = (*I)->getValueAsString("Name");
Michael Han4a045172012-03-07 00:12:16 +00001907
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001908 SmallString<64> Spelling;
1909 if ((*I)->getValueAsString("Variety") == "CXX11") {
1910 Spelling += (*I)->getValueAsString("Namespace");
1911 Spelling += "::";
Alexis Hunta0e54d42012-06-18 16:13:52 +00001912 }
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001913 Spelling += NormalizeAttrSpelling(RawSpelling);
Alexis Hunta0e54d42012-06-18 16:13:52 +00001914
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00001915 if (SemaHandler)
Douglas Gregor377f99b2012-05-02 17:33:51 +00001916 Matches.push_back(
Douglas Gregor87a170c2012-05-11 23:37:49 +00001917 StringMatcher::StringPair(
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001918 StringRef(Spelling),
Alexis Hunta0e54d42012-06-18 16:13:52 +00001919 "return AttributeList::AT_" + AttrName.str() + ";"));
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00001920 else
Douglas Gregor377f99b2012-05-02 17:33:51 +00001921 Matches.push_back(
1922 StringMatcher::StringPair(
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001923 StringRef(Spelling),
Alexis Hunta0e54d42012-06-18 16:13:52 +00001924 "return AttributeList::IgnoredAttribute;"));
Michael Han4a045172012-03-07 00:12:16 +00001925 }
1926 }
1927 }
Douglas Gregor377f99b2012-05-02 17:33:51 +00001928
1929 OS << "static AttributeList::Kind getAttrKind(StringRef Name) {\n";
1930 StringMatcher("Name", Matches, OS).Emit();
1931 OS << "return AttributeList::UnknownAttribute;\n"
1932 << "}\n";
Michael Han4a045172012-03-07 00:12:16 +00001933}
1934
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001935// Emits the code to dump an attribute.
1936void EmitClangAttrDump(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001937 emitSourceFileHeader("Attribute dumper", OS);
1938
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001939 OS <<
1940 " switch (A->getKind()) {\n"
1941 " default:\n"
1942 " llvm_unreachable(\"Unknown attribute kind!\");\n"
1943 " break;\n";
1944 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
1945 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1946 I != E; ++I) {
1947 Record &R = **I;
1948 if (!R.getValueAsBit("ASTNode"))
1949 continue;
1950 OS << " case attr::" << R.getName() << ": {\n";
1951 Args = R.getValueAsListOfDefs("Args");
1952 if (!Args.empty()) {
1953 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
1954 << "Attr>(A);\n";
1955 for (std::vector<Record*>::iterator I = Args.begin(), E = Args.end();
1956 I != E; ++I)
1957 createArgument(**I, R.getName())->writeDump(OS);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001958
1959 // Code for detecting the last child.
1960 OS << " bool OldMoreChildren = hasMoreChildren();\n";
1961 OS << " bool MoreChildren = OldMoreChildren;\n";
1962
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001963 for (std::vector<Record*>::iterator I = Args.begin(), E = Args.end();
Richard Trieude5cc7d2013-01-31 01:44:26 +00001964 I != E; ++I) {
1965 // More code for detecting the last child.
1966 OS << " MoreChildren = OldMoreChildren";
1967 for (std::vector<Record*>::iterator Next = I + 1; Next != E; ++Next) {
1968 OS << " || ";
1969 createArgument(**Next, R.getName())->writeHasChildren(OS);
1970 }
1971 OS << ";\n";
1972 OS << " setMoreChildren(MoreChildren);\n";
1973
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001974 createArgument(**I, R.getName())->writeDumpChildren(OS);
Richard Trieude5cc7d2013-01-31 01:44:26 +00001975 }
1976
1977 // Reset the last child.
1978 OS << " setMoreChildren(OldMoreChildren);\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001979 }
1980 OS <<
1981 " break;\n"
1982 " }\n";
1983 }
1984 OS << " }\n";
1985}
1986
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001987} // end namespace clang