blob: 395400ea854669320a7f42c7d8c3c5bff7d51b05 [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>
Aaron Ballman80469032013-11-29 14:57:58 +000024#include <set>
Peter Collingbournebee583f2011-10-06 13:03:08 +000025
26using namespace llvm;
27
28static const std::vector<StringRef>
29getValueAsListOfStrings(Record &R, StringRef FieldName) {
30 ListInit *List = R.getValueAsListInit(FieldName);
31 assert (List && "Got a null ListInit");
32
33 std::vector<StringRef> Strings;
34 Strings.reserve(List->getSize());
35
36 for (ListInit::const_iterator i = List->begin(), e = List->end();
37 i != e;
38 ++i) {
39 assert(*i && "Got a null element in a ListInit");
Sean Silva1c4aaa82012-10-10 20:25:43 +000040 if (StringInit *S = dyn_cast<StringInit>(*i))
Peter Collingbournebee583f2011-10-06 13:03:08 +000041 Strings.push_back(S->getValue());
Peter Collingbournebee583f2011-10-06 13:03:08 +000042 else
43 assert(false && "Got a non-string, non-code element in a ListInit");
44 }
45
46 return Strings;
47}
48
49static std::string ReadPCHRecord(StringRef type) {
50 return StringSwitch<std::string>(type)
51 .EndsWith("Decl *", "GetLocalDeclAs<"
52 + std::string(type, 0, type.size()-1) + ">(F, Record[Idx++])")
Richard Smithb87c4652013-10-31 21:23:20 +000053 .Case("TypeSourceInfo *", "GetTypeSourceInfo(F, Record, Idx)")
Argyrios Kyrtzidisa660ae42012-11-15 01:31:39 +000054 .Case("Expr *", "ReadExpr(F)")
Peter Collingbournebee583f2011-10-06 13:03:08 +000055 .Case("IdentifierInfo *", "GetIdentifierInfo(F, Record, Idx)")
56 .Case("SourceLocation", "ReadSourceLocation(F, Record, Idx)")
57 .Default("Record[Idx++]");
58}
59
60// Assumes that the way to get the value is SA->getname()
61static std::string WritePCHRecord(StringRef type, StringRef name) {
62 return StringSwitch<std::string>(type)
63 .EndsWith("Decl *", "AddDeclRef(" + std::string(name) +
64 ", Record);\n")
Richard Smithb87c4652013-10-31 21:23:20 +000065 .Case("TypeSourceInfo *",
66 "AddTypeSourceInfo(" + std::string(name) + ", Record);\n")
Peter Collingbournebee583f2011-10-06 13:03:08 +000067 .Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
68 .Case("IdentifierInfo *",
69 "AddIdentifierRef(" + std::string(name) + ", Record);\n")
70 .Case("SourceLocation",
71 "AddSourceLocation(" + std::string(name) + ", Record);\n")
72 .Default("Record.push_back(" + std::string(name) + ");\n");
73}
74
Michael Han4a045172012-03-07 00:12:16 +000075// Normalize attribute name by removing leading and trailing
76// underscores. For example, __foo, foo__, __foo__ would
77// become foo.
78static StringRef NormalizeAttrName(StringRef AttrName) {
79 if (AttrName.startswith("__"))
80 AttrName = AttrName.substr(2, AttrName.size());
81
82 if (AttrName.endswith("__"))
83 AttrName = AttrName.substr(0, AttrName.size() - 2);
84
85 return AttrName;
86}
87
88// Normalize attribute spelling only if the spelling has both leading
89// and trailing underscores. For example, __ms_struct__ will be
90// normalized to "ms_struct"; __cdecl will remain intact.
91static StringRef NormalizeAttrSpelling(StringRef AttrSpelling) {
92 if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) {
93 AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4);
94 }
95
96 return AttrSpelling;
97}
98
Peter Collingbournebee583f2011-10-06 13:03:08 +000099namespace {
100 class Argument {
101 std::string lowerName, upperName;
102 StringRef attrName;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000103 bool isOpt;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000104
105 public:
106 Argument(Record &Arg, StringRef Attr)
107 : lowerName(Arg.getValueAsString("Name")), upperName(lowerName),
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000108 attrName(Attr), isOpt(false) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000109 if (!lowerName.empty()) {
110 lowerName[0] = std::tolower(lowerName[0]);
111 upperName[0] = std::toupper(upperName[0]);
112 }
113 }
114 virtual ~Argument() {}
115
116 StringRef getLowerName() const { return lowerName; }
117 StringRef getUpperName() const { return upperName; }
118 StringRef getAttrName() const { return attrName; }
119
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000120 bool isOptional() const { return isOpt; }
121 void setOptional(bool set) { isOpt = set; }
122
Peter Collingbournebee583f2011-10-06 13:03:08 +0000123 // These functions print the argument contents formatted in different ways.
124 virtual void writeAccessors(raw_ostream &OS) const = 0;
125 virtual void writeAccessorDefinitions(raw_ostream &OS) const {}
126 virtual void writeCloneArgs(raw_ostream &OS) const = 0;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000127 virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0;
Daniel Dunbardc51baa2012-02-10 06:00:29 +0000128 virtual void writeTemplateInstantiation(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000129 virtual void writeCtorBody(raw_ostream &OS) const {}
130 virtual void writeCtorInitializers(raw_ostream &OS) const = 0;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000131 virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000132 virtual void writeCtorParameters(raw_ostream &OS) const = 0;
133 virtual void writeDeclarations(raw_ostream &OS) const = 0;
134 virtual void writePCHReadArgs(raw_ostream &OS) const = 0;
135 virtual void writePCHReadDecls(raw_ostream &OS) const = 0;
136 virtual void writePCHWrite(raw_ostream &OS) const = 0;
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000137 virtual void writeValue(raw_ostream &OS) const = 0;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000138 virtual void writeDump(raw_ostream &OS) const = 0;
139 virtual void writeDumpChildren(raw_ostream &OS) const {}
Richard Trieude5cc7d2013-01-31 01:44:26 +0000140 virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000141
142 virtual bool isEnumArg() const { return false; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000143 virtual bool isVariadicEnumArg() const { return false; }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000144 };
145
146 class SimpleArgument : public Argument {
147 std::string type;
148
149 public:
150 SimpleArgument(Record &Arg, StringRef Attr, std::string T)
151 : Argument(Arg, Attr), type(T)
152 {}
153
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000154 std::string getType() const { return type; }
155
Peter Collingbournebee583f2011-10-06 13:03:08 +0000156 void writeAccessors(raw_ostream &OS) const {
157 OS << " " << type << " get" << getUpperName() << "() const {\n";
158 OS << " return " << getLowerName() << ";\n";
159 OS << " }";
160 }
161 void writeCloneArgs(raw_ostream &OS) const {
162 OS << getLowerName();
163 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000164 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
165 OS << "A->get" << getUpperName() << "()";
166 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000167 void writeCtorInitializers(raw_ostream &OS) const {
168 OS << getLowerName() << "(" << getUpperName() << ")";
169 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000170 void writeCtorDefaultInitializers(raw_ostream &OS) const {
171 OS << getLowerName() << "()";
172 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000173 void writeCtorParameters(raw_ostream &OS) const {
174 OS << type << " " << getUpperName();
175 }
176 void writeDeclarations(raw_ostream &OS) const {
177 OS << type << " " << getLowerName() << ";";
178 }
179 void writePCHReadDecls(raw_ostream &OS) const {
180 std::string read = ReadPCHRecord(type);
181 OS << " " << type << " " << getLowerName() << " = " << read << ";\n";
182 }
183 void writePCHReadArgs(raw_ostream &OS) const {
184 OS << getLowerName();
185 }
186 void writePCHWrite(raw_ostream &OS) const {
187 OS << " " << WritePCHRecord(type, "SA->get" +
188 std::string(getUpperName()) + "()");
189 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000190 void writeValue(raw_ostream &OS) const {
191 if (type == "FunctionDecl *") {
Richard Smithb87c4652013-10-31 21:23:20 +0000192 OS << "\" << get" << getUpperName()
193 << "()->getNameInfo().getAsString() << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000194 } else if (type == "IdentifierInfo *") {
195 OS << "\" << get" << getUpperName() << "()->getName() << \"";
Richard Smithb87c4652013-10-31 21:23:20 +0000196 } else if (type == "TypeSourceInfo *") {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000197 OS << "\" << get" << getUpperName() << "().getAsString() << \"";
198 } else if (type == "SourceLocation") {
199 OS << "\" << get" << getUpperName() << "().getRawEncoding() << \"";
200 } else {
201 OS << "\" << get" << getUpperName() << "() << \"";
202 }
203 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000204 void writeDump(raw_ostream &OS) const {
205 if (type == "FunctionDecl *") {
206 OS << " OS << \" \";\n";
207 OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n";
208 } else if (type == "IdentifierInfo *") {
209 OS << " OS << \" \" << SA->get" << getUpperName()
210 << "()->getName();\n";
Richard Smithb87c4652013-10-31 21:23:20 +0000211 } else if (type == "TypeSourceInfo *") {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000212 OS << " OS << \" \" << SA->get" << getUpperName()
213 << "().getAsString();\n";
214 } else if (type == "SourceLocation") {
215 OS << " OS << \" \";\n";
216 OS << " SA->get" << getUpperName() << "().print(OS, *SM);\n";
217 } else if (type == "bool") {
218 OS << " if (SA->get" << getUpperName() << "()) OS << \" "
219 << getUpperName() << "\";\n";
220 } else if (type == "int" || type == "unsigned") {
221 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
222 } else {
223 llvm_unreachable("Unknown SimpleArgument type!");
224 }
225 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000226 };
227
Aaron Ballman18a78382013-11-21 00:28:23 +0000228 class DefaultSimpleArgument : public SimpleArgument {
229 int64_t Default;
230
231 public:
232 DefaultSimpleArgument(Record &Arg, StringRef Attr,
233 std::string T, int64_t Default)
234 : SimpleArgument(Arg, Attr, T), Default(Default) {}
235
236 void writeAccessors(raw_ostream &OS) const {
237 SimpleArgument::writeAccessors(OS);
238
239 OS << "\n\n static const " << getType() << " Default" << getUpperName()
240 << " = " << Default << ";";
241 }
242 };
243
Peter Collingbournebee583f2011-10-06 13:03:08 +0000244 class StringArgument : public Argument {
245 public:
246 StringArgument(Record &Arg, StringRef Attr)
247 : Argument(Arg, Attr)
248 {}
249
250 void writeAccessors(raw_ostream &OS) const {
251 OS << " llvm::StringRef get" << getUpperName() << "() const {\n";
252 OS << " return llvm::StringRef(" << getLowerName() << ", "
253 << getLowerName() << "Length);\n";
254 OS << " }\n";
255 OS << " unsigned get" << getUpperName() << "Length() const {\n";
256 OS << " return " << getLowerName() << "Length;\n";
257 OS << " }\n";
258 OS << " void set" << getUpperName()
259 << "(ASTContext &C, llvm::StringRef S) {\n";
260 OS << " " << getLowerName() << "Length = S.size();\n";
261 OS << " this->" << getLowerName() << " = new (C, 1) char ["
262 << getLowerName() << "Length];\n";
263 OS << " std::memcpy(this->" << getLowerName() << ", S.data(), "
264 << getLowerName() << "Length);\n";
265 OS << " }";
266 }
267 void writeCloneArgs(raw_ostream &OS) const {
268 OS << "get" << getUpperName() << "()";
269 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000270 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
271 OS << "A->get" << getUpperName() << "()";
272 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000273 void writeCtorBody(raw_ostream &OS) const {
274 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
275 << ".data(), " << getLowerName() << "Length);";
276 }
277 void writeCtorInitializers(raw_ostream &OS) const {
278 OS << getLowerName() << "Length(" << getUpperName() << ".size()),"
279 << getLowerName() << "(new (Ctx, 1) char[" << getLowerName()
280 << "Length])";
281 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000282 void writeCtorDefaultInitializers(raw_ostream &OS) const {
283 OS << getLowerName() << "Length(0)," << getLowerName() << "(0)";
284 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000285 void writeCtorParameters(raw_ostream &OS) const {
286 OS << "llvm::StringRef " << getUpperName();
287 }
288 void writeDeclarations(raw_ostream &OS) const {
289 OS << "unsigned " << getLowerName() << "Length;\n";
290 OS << "char *" << getLowerName() << ";";
291 }
292 void writePCHReadDecls(raw_ostream &OS) const {
293 OS << " std::string " << getLowerName()
294 << "= ReadString(Record, Idx);\n";
295 }
296 void writePCHReadArgs(raw_ostream &OS) const {
297 OS << getLowerName();
298 }
299 void writePCHWrite(raw_ostream &OS) const {
300 OS << " AddString(SA->get" << getUpperName() << "(), Record);\n";
301 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000302 void writeValue(raw_ostream &OS) const {
303 OS << "\\\"\" << get" << getUpperName() << "() << \"\\\"";
304 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000305 void writeDump(raw_ostream &OS) const {
306 OS << " OS << \" \\\"\" << SA->get" << getUpperName()
307 << "() << \"\\\"\";\n";
308 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000309 };
310
311 class AlignedArgument : public Argument {
312 public:
313 AlignedArgument(Record &Arg, StringRef Attr)
314 : Argument(Arg, Attr)
315 {}
316
317 void writeAccessors(raw_ostream &OS) const {
318 OS << " bool is" << getUpperName() << "Dependent() const;\n";
319
320 OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n";
321
322 OS << " bool is" << getUpperName() << "Expr() const {\n";
323 OS << " return is" << getLowerName() << "Expr;\n";
324 OS << " }\n";
325
326 OS << " Expr *get" << getUpperName() << "Expr() const {\n";
327 OS << " assert(is" << getLowerName() << "Expr);\n";
328 OS << " return " << getLowerName() << "Expr;\n";
329 OS << " }\n";
330
331 OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n";
332 OS << " assert(!is" << getLowerName() << "Expr);\n";
333 OS << " return " << getLowerName() << "Type;\n";
334 OS << " }";
335 }
336 void writeAccessorDefinitions(raw_ostream &OS) const {
337 OS << "bool " << getAttrName() << "Attr::is" << getUpperName()
338 << "Dependent() const {\n";
339 OS << " if (is" << getLowerName() << "Expr)\n";
340 OS << " return " << getLowerName() << "Expr && (" << getLowerName()
341 << "Expr->isValueDependent() || " << getLowerName()
342 << "Expr->isTypeDependent());\n";
343 OS << " else\n";
344 OS << " return " << getLowerName()
345 << "Type->getType()->isDependentType();\n";
346 OS << "}\n";
347
348 // FIXME: Do not do the calculation here
349 // FIXME: Handle types correctly
350 // A null pointer means maximum alignment
351 // FIXME: Load the platform-specific maximum alignment, rather than
352 // 16, the x86 max.
353 OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
354 << "(ASTContext &Ctx) const {\n";
355 OS << " assert(!is" << getUpperName() << "Dependent());\n";
356 OS << " if (is" << getLowerName() << "Expr)\n";
357 OS << " return (" << getLowerName() << "Expr ? " << getLowerName()
Richard Smithcaf33902011-10-10 18:28:20 +0000358 << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue() : 16)"
Peter Collingbournebee583f2011-10-06 13:03:08 +0000359 << "* Ctx.getCharWidth();\n";
360 OS << " else\n";
361 OS << " return 0; // FIXME\n";
362 OS << "}\n";
363 }
364 void writeCloneArgs(raw_ostream &OS) const {
365 OS << "is" << getLowerName() << "Expr, is" << getLowerName()
366 << "Expr ? static_cast<void*>(" << getLowerName()
367 << "Expr) : " << getLowerName()
368 << "Type";
369 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000370 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
371 // FIXME: move the definition in Sema::InstantiateAttrs to here.
372 // In the meantime, aligned attributes are cloned.
373 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000374 void writeCtorBody(raw_ostream &OS) const {
375 OS << " if (is" << getLowerName() << "Expr)\n";
376 OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>("
377 << getUpperName() << ");\n";
378 OS << " else\n";
379 OS << " " << getLowerName()
380 << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName()
381 << ");";
382 }
383 void writeCtorInitializers(raw_ostream &OS) const {
384 OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)";
385 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000386 void writeCtorDefaultInitializers(raw_ostream &OS) const {
387 OS << "is" << getLowerName() << "Expr(false)";
388 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000389 void writeCtorParameters(raw_ostream &OS) const {
390 OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName();
391 }
392 void writeDeclarations(raw_ostream &OS) const {
393 OS << "bool is" << getLowerName() << "Expr;\n";
394 OS << "union {\n";
395 OS << "Expr *" << getLowerName() << "Expr;\n";
396 OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
397 OS << "};";
398 }
399 void writePCHReadArgs(raw_ostream &OS) const {
400 OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr";
401 }
402 void writePCHReadDecls(raw_ostream &OS) const {
403 OS << " bool is" << getLowerName() << "Expr = Record[Idx++];\n";
404 OS << " void *" << getLowerName() << "Ptr;\n";
405 OS << " if (is" << getLowerName() << "Expr)\n";
406 OS << " " << getLowerName() << "Ptr = ReadExpr(F);\n";
407 OS << " else\n";
408 OS << " " << getLowerName()
409 << "Ptr = GetTypeSourceInfo(F, Record, Idx);\n";
410 }
411 void writePCHWrite(raw_ostream &OS) const {
412 OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n";
413 OS << " if (SA->is" << getUpperName() << "Expr())\n";
414 OS << " AddStmt(SA->get" << getUpperName() << "Expr());\n";
415 OS << " else\n";
416 OS << " AddTypeSourceInfo(SA->get" << getUpperName()
417 << "Type(), Record);\n";
418 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000419 void writeValue(raw_ostream &OS) const {
Richard Smith52f04a22012-08-16 02:43:29 +0000420 OS << "\";\n"
421 << " " << getLowerName() << "Expr->printPretty(OS, 0, Policy);\n"
422 << " OS << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000423 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000424 void writeDump(raw_ostream &OS) const {
425 }
426 void writeDumpChildren(raw_ostream &OS) const {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000427 OS << " if (SA->is" << getUpperName() << "Expr()) {\n";
428 OS << " lastChild();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000429 OS << " dumpStmt(SA->get" << getUpperName() << "Expr());\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000430 OS << " } else\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000431 OS << " dumpType(SA->get" << getUpperName()
432 << "Type()->getType());\n";
433 }
Richard Trieude5cc7d2013-01-31 01:44:26 +0000434 void writeHasChildren(raw_ostream &OS) const {
435 OS << "SA->is" << getUpperName() << "Expr()";
436 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000437 };
438
439 class VariadicArgument : public Argument {
440 std::string type;
441
442 public:
443 VariadicArgument(Record &Arg, StringRef Attr, std::string T)
444 : Argument(Arg, Attr), type(T)
445 {}
446
447 std::string getType() const { return type; }
448
449 void writeAccessors(raw_ostream &OS) const {
450 OS << " typedef " << type << "* " << getLowerName() << "_iterator;\n";
451 OS << " " << getLowerName() << "_iterator " << getLowerName()
452 << "_begin() const {\n";
453 OS << " return " << getLowerName() << ";\n";
454 OS << " }\n";
455 OS << " " << getLowerName() << "_iterator " << getLowerName()
456 << "_end() const {\n";
457 OS << " return " << getLowerName() << " + " << getLowerName()
458 << "Size;\n";
459 OS << " }\n";
460 OS << " unsigned " << getLowerName() << "_size() const {\n"
DeLesley Hutchins30398dd2012-01-20 22:50:54 +0000461 << " return " << getLowerName() << "Size;\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000462 OS << " }";
463 }
464 void writeCloneArgs(raw_ostream &OS) const {
465 OS << getLowerName() << ", " << getLowerName() << "Size";
466 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000467 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
468 // This isn't elegant, but we have to go through public methods...
469 OS << "A->" << getLowerName() << "_begin(), "
470 << "A->" << getLowerName() << "_size()";
471 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000472 void writeCtorBody(raw_ostream &OS) const {
473 // FIXME: memcpy is not safe on non-trivial types.
474 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
475 << ", " << getLowerName() << "Size * sizeof(" << getType() << "));\n";
476 }
477 void writeCtorInitializers(raw_ostream &OS) const {
478 OS << getLowerName() << "Size(" << getUpperName() << "Size), "
479 << getLowerName() << "(new (Ctx, 16) " << getType() << "["
480 << getLowerName() << "Size])";
481 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000482 void writeCtorDefaultInitializers(raw_ostream &OS) const {
483 OS << getLowerName() << "Size(0), " << getLowerName() << "(0)";
484 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000485 void writeCtorParameters(raw_ostream &OS) const {
486 OS << getType() << " *" << getUpperName() << ", unsigned "
487 << getUpperName() << "Size";
488 }
489 void writeDeclarations(raw_ostream &OS) const {
490 OS << " unsigned " << getLowerName() << "Size;\n";
491 OS << " " << getType() << " *" << getLowerName() << ";";
492 }
493 void writePCHReadDecls(raw_ostream &OS) const {
494 OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000495 OS << " SmallVector<" << type << ", 4> " << getLowerName()
Peter Collingbournebee583f2011-10-06 13:03:08 +0000496 << ";\n";
497 OS << " " << getLowerName() << ".reserve(" << getLowerName()
498 << "Size);\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000499 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000500
501 std::string read = ReadPCHRecord(type);
502 OS << " " << getLowerName() << ".push_back(" << read << ");\n";
503 }
504 void writePCHReadArgs(raw_ostream &OS) const {
505 OS << getLowerName() << ".data(), " << getLowerName() << "Size";
506 }
507 void writePCHWrite(raw_ostream &OS) const{
508 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
509 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
510 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
511 << getLowerName() << "_end(); i != e; ++i)\n";
512 OS << " " << WritePCHRecord(type, "(*i)");
513 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000514 void writeValue(raw_ostream &OS) const {
515 OS << "\";\n";
516 OS << " bool isFirst = true;\n"
517 << " for (" << getAttrName() << "Attr::" << getLowerName()
518 << "_iterator i = " << getLowerName() << "_begin(), e = "
519 << getLowerName() << "_end(); i != e; ++i) {\n"
520 << " if (isFirst) isFirst = false;\n"
521 << " else OS << \", \";\n"
522 << " OS << *i;\n"
523 << " }\n";
524 OS << " OS << \"";
525 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000526 void writeDump(raw_ostream &OS) const {
527 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
528 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
529 << getLowerName() << "_end(); I != E; ++I)\n";
530 OS << " OS << \" \" << *I;\n";
531 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000532 };
533
534 class EnumArgument : public Argument {
535 std::string type;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000536 std::vector<StringRef> values, enums, uniques;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000537 public:
538 EnumArgument(Record &Arg, StringRef Attr)
539 : Argument(Arg, Attr), type(Arg.getValueAsString("Type")),
540 values(getValueAsListOfStrings(Arg, "Values")),
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000541 enums(getValueAsListOfStrings(Arg, "Enums")),
542 uniques(enums)
543 {
544 // Calculate the various enum values
545 std::sort(uniques.begin(), uniques.end());
546 uniques.erase(std::unique(uniques.begin(), uniques.end()), uniques.end());
547 // FIXME: Emit a proper error
548 assert(!uniques.empty());
549 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000550
Aaron Ballman682ee422013-09-11 19:47:58 +0000551 bool isEnumArg() const { return true; }
552
Peter Collingbournebee583f2011-10-06 13:03:08 +0000553 void writeAccessors(raw_ostream &OS) const {
554 OS << " " << type << " get" << getUpperName() << "() const {\n";
555 OS << " return " << getLowerName() << ";\n";
556 OS << " }";
557 }
558 void writeCloneArgs(raw_ostream &OS) const {
559 OS << getLowerName();
560 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000561 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
562 OS << "A->get" << getUpperName() << "()";
563 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000564 void writeCtorInitializers(raw_ostream &OS) const {
565 OS << getLowerName() << "(" << getUpperName() << ")";
566 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000567 void writeCtorDefaultInitializers(raw_ostream &OS) const {
568 OS << getLowerName() << "(" << type << "(0))";
569 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000570 void writeCtorParameters(raw_ostream &OS) const {
571 OS << type << " " << getUpperName();
572 }
573 void writeDeclarations(raw_ostream &OS) const {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000574 std::vector<StringRef>::const_iterator i = uniques.begin(),
575 e = uniques.end();
Peter Collingbournebee583f2011-10-06 13:03:08 +0000576 // The last one needs to not have a comma.
577 --e;
578
579 OS << "public:\n";
580 OS << " enum " << type << " {\n";
581 for (; i != e; ++i)
582 OS << " " << *i << ",\n";
583 OS << " " << *e << "\n";
584 OS << " };\n";
585 OS << "private:\n";
586 OS << " " << type << " " << getLowerName() << ";";
587 }
588 void writePCHReadDecls(raw_ostream &OS) const {
589 OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName()
590 << "(static_cast<" << getAttrName() << "Attr::" << type
591 << ">(Record[Idx++]));\n";
592 }
593 void writePCHReadArgs(raw_ostream &OS) const {
594 OS << getLowerName();
595 }
596 void writePCHWrite(raw_ostream &OS) const {
597 OS << "Record.push_back(SA->get" << getUpperName() << "());\n";
598 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000599 void writeValue(raw_ostream &OS) const {
600 OS << "\" << get" << getUpperName() << "() << \"";
601 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000602 void writeDump(raw_ostream &OS) const {
603 OS << " switch(SA->get" << getUpperName() << "()) {\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000604 for (std::vector<StringRef>::const_iterator I = uniques.begin(),
605 E = uniques.end(); I != E; ++I) {
606 OS << " case " << getAttrName() << "Attr::" << *I << ":\n";
607 OS << " OS << \" " << *I << "\";\n";
608 OS << " break;\n";
609 }
610 OS << " }\n";
611 }
Aaron Ballman682ee422013-09-11 19:47:58 +0000612
613 void writeConversion(raw_ostream &OS) const {
614 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
615 OS << type << " &Out) {\n";
616 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
617 OS << type << "> >(Val)\n";
618 for (size_t I = 0; I < enums.size(); ++I) {
619 OS << " .Case(\"" << values[I] << "\", ";
620 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
621 }
622 OS << " .Default(Optional<" << type << ">());\n";
623 OS << " if (R) {\n";
624 OS << " Out = *R;\n return true;\n }\n";
625 OS << " return false;\n";
626 OS << " }\n";
627 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000628 };
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000629
630 class VariadicEnumArgument: public VariadicArgument {
631 std::string type, QualifiedTypeName;
632 std::vector<StringRef> values, enums, uniques;
633 public:
634 VariadicEnumArgument(Record &Arg, StringRef Attr)
635 : VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")),
636 type(Arg.getValueAsString("Type")),
637 values(getValueAsListOfStrings(Arg, "Values")),
638 enums(getValueAsListOfStrings(Arg, "Enums")),
639 uniques(enums)
640 {
641 // Calculate the various enum values
642 std::sort(uniques.begin(), uniques.end());
643 uniques.erase(std::unique(uniques.begin(), uniques.end()), uniques.end());
644
645 QualifiedTypeName = getAttrName().str() + "Attr::" + type;
646
647 // FIXME: Emit a proper error
648 assert(!uniques.empty());
649 }
650
651 bool isVariadicEnumArg() const { return true; }
652
653 void writeDeclarations(raw_ostream &OS) const {
654 std::vector<StringRef>::const_iterator i = uniques.begin(),
655 e = uniques.end();
656 // The last one needs to not have a comma.
657 --e;
658
659 OS << "public:\n";
660 OS << " enum " << type << " {\n";
661 for (; i != e; ++i)
662 OS << " " << *i << ",\n";
663 OS << " " << *e << "\n";
664 OS << " };\n";
665 OS << "private:\n";
666
667 VariadicArgument::writeDeclarations(OS);
668 }
669 void writeDump(raw_ostream &OS) const {
670 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
671 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
672 << getLowerName() << "_end(); I != E; ++I) {\n";
673 OS << " switch(*I) {\n";
674 for (std::vector<StringRef>::const_iterator UI = uniques.begin(),
675 UE = uniques.end(); UI != UE; ++UI) {
676 OS << " case " << getAttrName() << "Attr::" << *UI << ":\n";
677 OS << " OS << \" " << *UI << "\";\n";
678 OS << " break;\n";
679 }
680 OS << " }\n";
681 OS << " }\n";
682 }
683 void writePCHReadDecls(raw_ostream &OS) const {
684 OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
685 OS << " SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName()
686 << ";\n";
687 OS << " " << getLowerName() << ".reserve(" << getLowerName()
688 << "Size);\n";
689 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
690 OS << " " << getLowerName() << ".push_back(" << "static_cast<"
691 << QualifiedTypeName << ">(Record[Idx++]));\n";
692 }
693 void writePCHWrite(raw_ostream &OS) const{
694 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
695 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
696 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
697 << getLowerName() << "_end(); i != e; ++i)\n";
698 OS << " " << WritePCHRecord(QualifiedTypeName, "(*i)");
699 }
700 void writeConversion(raw_ostream &OS) const {
701 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
702 OS << type << " &Out) {\n";
703 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
704 OS << type << "> >(Val)\n";
705 for (size_t I = 0; I < enums.size(); ++I) {
706 OS << " .Case(\"" << values[I] << "\", ";
707 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
708 }
709 OS << " .Default(Optional<" << type << ">());\n";
710 OS << " if (R) {\n";
711 OS << " Out = *R;\n return true;\n }\n";
712 OS << " return false;\n";
713 OS << " }\n";
714 }
715 };
Peter Collingbournebee583f2011-10-06 13:03:08 +0000716
717 class VersionArgument : public Argument {
718 public:
719 VersionArgument(Record &Arg, StringRef Attr)
720 : Argument(Arg, Attr)
721 {}
722
723 void writeAccessors(raw_ostream &OS) const {
724 OS << " VersionTuple get" << getUpperName() << "() const {\n";
725 OS << " return " << getLowerName() << ";\n";
726 OS << " }\n";
727 OS << " void set" << getUpperName()
728 << "(ASTContext &C, VersionTuple V) {\n";
729 OS << " " << getLowerName() << " = V;\n";
730 OS << " }";
731 }
732 void writeCloneArgs(raw_ostream &OS) const {
733 OS << "get" << getUpperName() << "()";
734 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000735 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
736 OS << "A->get" << getUpperName() << "()";
737 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000738 void writeCtorBody(raw_ostream &OS) const {
739 }
740 void writeCtorInitializers(raw_ostream &OS) const {
741 OS << getLowerName() << "(" << getUpperName() << ")";
742 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000743 void writeCtorDefaultInitializers(raw_ostream &OS) const {
744 OS << getLowerName() << "()";
745 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000746 void writeCtorParameters(raw_ostream &OS) const {
747 OS << "VersionTuple " << getUpperName();
748 }
749 void writeDeclarations(raw_ostream &OS) const {
750 OS << "VersionTuple " << getLowerName() << ";\n";
751 }
752 void writePCHReadDecls(raw_ostream &OS) const {
753 OS << " VersionTuple " << getLowerName()
754 << "= ReadVersionTuple(Record, Idx);\n";
755 }
756 void writePCHReadArgs(raw_ostream &OS) const {
757 OS << getLowerName();
758 }
759 void writePCHWrite(raw_ostream &OS) const {
760 OS << " AddVersionTuple(SA->get" << getUpperName() << "(), Record);\n";
761 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000762 void writeValue(raw_ostream &OS) const {
763 OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";
764 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000765 void writeDump(raw_ostream &OS) const {
766 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
767 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000768 };
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000769
770 class ExprArgument : public SimpleArgument {
771 public:
772 ExprArgument(Record &Arg, StringRef Attr)
773 : SimpleArgument(Arg, Attr, "Expr *")
774 {}
775
776 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
777 OS << "tempInst" << getUpperName();
778 }
779
780 void writeTemplateInstantiation(raw_ostream &OS) const {
781 OS << " " << getType() << " tempInst" << getUpperName() << ";\n";
782 OS << " {\n";
783 OS << " EnterExpressionEvaluationContext "
784 << "Unevaluated(S, Sema::Unevaluated);\n";
785 OS << " ExprResult " << "Result = S.SubstExpr("
786 << "A->get" << getUpperName() << "(), TemplateArgs);\n";
787 OS << " tempInst" << getUpperName() << " = "
788 << "Result.takeAs<Expr>();\n";
789 OS << " }\n";
790 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000791
792 void writeDump(raw_ostream &OS) const {
793 }
794
795 void writeDumpChildren(raw_ostream &OS) const {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000796 OS << " lastChild();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000797 OS << " dumpStmt(SA->get" << getUpperName() << "());\n";
798 }
Richard Trieude5cc7d2013-01-31 01:44:26 +0000799 void writeHasChildren(raw_ostream &OS) const { OS << "true"; }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000800 };
801
802 class VariadicExprArgument : public VariadicArgument {
803 public:
804 VariadicExprArgument(Record &Arg, StringRef Attr)
805 : VariadicArgument(Arg, Attr, "Expr *")
806 {}
807
808 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
809 OS << "tempInst" << getUpperName() << ", "
810 << "A->" << getLowerName() << "_size()";
811 }
812
813 void writeTemplateInstantiation(raw_ostream &OS) const {
814 OS << " " << getType() << " *tempInst" << getUpperName()
815 << " = new (C, 16) " << getType()
816 << "[A->" << getLowerName() << "_size()];\n";
817 OS << " {\n";
818 OS << " EnterExpressionEvaluationContext "
819 << "Unevaluated(S, Sema::Unevaluated);\n";
820 OS << " " << getType() << " *TI = tempInst" << getUpperName()
821 << ";\n";
822 OS << " " << getType() << " *I = A->" << getLowerName()
823 << "_begin();\n";
824 OS << " " << getType() << " *E = A->" << getLowerName()
825 << "_end();\n";
826 OS << " for (; I != E; ++I, ++TI) {\n";
827 OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n";
828 OS << " *TI = Result.takeAs<Expr>();\n";
829 OS << " }\n";
830 OS << " }\n";
831 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000832
833 void writeDump(raw_ostream &OS) const {
834 }
835
836 void writeDumpChildren(raw_ostream &OS) const {
837 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
838 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
Richard Trieude5cc7d2013-01-31 01:44:26 +0000839 << getLowerName() << "_end(); I != E; ++I) {\n";
840 OS << " if (I + 1 == E)\n";
841 OS << " lastChild();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000842 OS << " dumpStmt(*I);\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000843 OS << " }\n";
844 }
845
846 void writeHasChildren(raw_ostream &OS) const {
847 OS << "SA->" << getLowerName() << "_begin() != "
848 << "SA->" << getLowerName() << "_end()";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000849 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000850 };
Richard Smithb87c4652013-10-31 21:23:20 +0000851
852 class TypeArgument : public SimpleArgument {
853 public:
854 TypeArgument(Record &Arg, StringRef Attr)
855 : SimpleArgument(Arg, Attr, "TypeSourceInfo *")
856 {}
857
858 void writeAccessors(raw_ostream &OS) const {
859 OS << " QualType get" << getUpperName() << "() const {\n";
860 OS << " return " << getLowerName() << "->getType();\n";
861 OS << " }";
862 OS << " " << getType() << " get" << getUpperName() << "Loc() const {\n";
863 OS << " return " << getLowerName() << ";\n";
864 OS << " }";
865 }
866 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
867 OS << "A->get" << getUpperName() << "Loc()";
868 }
869 void writePCHWrite(raw_ostream &OS) const {
870 OS << " " << WritePCHRecord(
871 getType(), "SA->get" + std::string(getUpperName()) + "Loc()");
872 }
873 };
Peter Collingbournebee583f2011-10-06 13:03:08 +0000874}
875
876static Argument *createArgument(Record &Arg, StringRef Attr,
877 Record *Search = 0) {
878 if (!Search)
879 Search = &Arg;
880
881 Argument *Ptr = 0;
882 llvm::StringRef ArgName = Search->getName();
883
884 if (ArgName == "AlignedArgument") Ptr = new AlignedArgument(Arg, Attr);
885 else if (ArgName == "EnumArgument") Ptr = new EnumArgument(Arg, Attr);
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000886 else if (ArgName == "ExprArgument") Ptr = new ExprArgument(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000887 else if (ArgName == "FunctionArgument")
888 Ptr = new SimpleArgument(Arg, Attr, "FunctionDecl *");
889 else if (ArgName == "IdentifierArgument")
890 Ptr = new SimpleArgument(Arg, Attr, "IdentifierInfo *");
891 else if (ArgName == "BoolArgument") Ptr = new SimpleArgument(Arg, Attr,
892 "bool");
Aaron Ballman18a78382013-11-21 00:28:23 +0000893 else if (ArgName == "DefaultIntArgument")
894 Ptr = new DefaultSimpleArgument(Arg, Attr, "int",
895 Arg.getValueAsInt("Default"));
Peter Collingbournebee583f2011-10-06 13:03:08 +0000896 else if (ArgName == "IntArgument") Ptr = new SimpleArgument(Arg, Attr, "int");
897 else if (ArgName == "StringArgument") Ptr = new StringArgument(Arg, Attr);
Richard Smithb87c4652013-10-31 21:23:20 +0000898 else if (ArgName == "TypeArgument") Ptr = new TypeArgument(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000899 else if (ArgName == "UnsignedArgument")
900 Ptr = new SimpleArgument(Arg, Attr, "unsigned");
901 else if (ArgName == "SourceLocArgument")
902 Ptr = new SimpleArgument(Arg, Attr, "SourceLocation");
903 else if (ArgName == "VariadicUnsignedArgument")
904 Ptr = new VariadicArgument(Arg, Attr, "unsigned");
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000905 else if (ArgName == "VariadicEnumArgument")
906 Ptr = new VariadicEnumArgument(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000907 else if (ArgName == "VariadicExprArgument")
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000908 Ptr = new VariadicExprArgument(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000909 else if (ArgName == "VersionArgument")
910 Ptr = new VersionArgument(Arg, Attr);
911
912 if (!Ptr) {
Aaron Ballman18a78382013-11-21 00:28:23 +0000913 // Search in reverse order so that the most-derived type is handled first.
Peter Collingbournebee583f2011-10-06 13:03:08 +0000914 std::vector<Record*> Bases = Search->getSuperClasses();
Aaron Ballman18a78382013-11-21 00:28:23 +0000915 for (std::vector<Record*>::reverse_iterator i = Bases.rbegin(),
916 e = Bases.rend(); i != e; ++i) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000917 Ptr = createArgument(Arg, Attr, *i);
918 if (Ptr)
919 break;
920 }
921 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000922
923 if (Ptr && Arg.getValueAsBit("Optional"))
924 Ptr->setOptional(true);
925
Peter Collingbournebee583f2011-10-06 13:03:08 +0000926 return Ptr;
927}
928
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000929static void writeAvailabilityValue(raw_ostream &OS) {
930 OS << "\" << getPlatform()->getName();\n"
931 << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n"
932 << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n"
933 << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n"
934 << " if (getUnavailable()) OS << \", unavailable\";\n"
935 << " OS << \"";
936}
937
Michael Han99315932013-01-24 16:46:58 +0000938static void writePrettyPrintFunction(Record &R, std::vector<Argument*> &Args,
939 raw_ostream &OS) {
940 std::vector<Record*> Spellings = R.getValueAsListOfDefs("Spellings");
941
942 OS << "void " << R.getName() << "Attr::printPretty("
943 << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n";
944
945 if (Spellings.size() == 0) {
946 OS << "}\n\n";
947 return;
948 }
949
950 OS <<
951 " switch (SpellingListIndex) {\n"
952 " default:\n"
953 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
954 " break;\n";
955
956 for (unsigned I = 0; I < Spellings.size(); ++ I) {
957 llvm::SmallString<16> Prefix;
958 llvm::SmallString<8> Suffix;
959 // The actual spelling of the name and namespace (if applicable)
960 // of an attribute without considering prefix and suffix.
961 llvm::SmallString<64> Spelling;
962 std::string Name = Spellings[I]->getValueAsString("Name");
963 std::string Variety = Spellings[I]->getValueAsString("Variety");
964
965 if (Variety == "GNU") {
966 Prefix = " __attribute__((";
967 Suffix = "))";
968 } else if (Variety == "CXX11") {
969 Prefix = " [[";
970 Suffix = "]]";
971 std::string Namespace = Spellings[I]->getValueAsString("Namespace");
972 if (Namespace != "") {
973 Spelling += Namespace;
974 Spelling += "::";
975 }
976 } else if (Variety == "Declspec") {
977 Prefix = " __declspec(";
978 Suffix = ")";
Richard Smith0cdcc982013-01-29 01:24:26 +0000979 } else if (Variety == "Keyword") {
980 Prefix = " ";
981 Suffix = "";
Michael Han99315932013-01-24 16:46:58 +0000982 } else {
Richard Smith0cdcc982013-01-29 01:24:26 +0000983 llvm_unreachable("Unknown attribute syntax variety!");
Michael Han99315932013-01-24 16:46:58 +0000984 }
985
986 Spelling += Name;
987
988 OS <<
989 " case " << I << " : {\n"
990 " OS << \"" + Prefix.str() + Spelling.str();
991
992 if (Args.size()) OS << "(";
993 if (Spelling == "availability") {
994 writeAvailabilityValue(OS);
995 } else {
996 for (std::vector<Argument*>::const_iterator I = Args.begin(),
997 E = Args.end(); I != E; ++ I) {
998 if (I != Args.begin()) OS << ", ";
999 (*I)->writeValue(OS);
1000 }
1001 }
1002
1003 if (Args.size()) OS << ")";
1004 OS << Suffix.str() + "\";\n";
1005
1006 OS <<
1007 " break;\n"
1008 " }\n";
1009 }
1010
1011 // End of the switch statement.
1012 OS << "}\n";
1013 // End of the print function.
1014 OS << "}\n\n";
1015}
1016
Michael Hanaf02bbe2013-02-01 01:19:17 +00001017/// \brief Return the index of a spelling in a spelling list.
1018static unsigned getSpellingListIndex(const std::vector<Record*> &SpellingList,
1019 const Record &Spelling) {
1020 assert(SpellingList.size() && "Spelling list is empty!");
1021
1022 for (unsigned Index = 0; Index < SpellingList.size(); ++Index) {
1023 Record *S = SpellingList[Index];
1024 if (S->getValueAsString("Variety") != Spelling.getValueAsString("Variety"))
1025 continue;
1026 if (S->getValueAsString("Variety") == "CXX11" &&
1027 S->getValueAsString("Namespace") !=
1028 Spelling.getValueAsString("Namespace"))
1029 continue;
1030 if (S->getValueAsString("Name") != Spelling.getValueAsString("Name"))
1031 continue;
1032
1033 return Index;
1034 }
1035
1036 llvm_unreachable("Unknown spelling!");
1037}
1038
1039static void writeAttrAccessorDefinition(Record &R, raw_ostream &OS) {
1040 std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors");
1041 for (std::vector<Record*>::const_iterator I = Accessors.begin(),
1042 E = Accessors.end(); I != E; ++I) {
1043 Record *Accessor = *I;
1044 std::string Name = Accessor->getValueAsString("Name");
1045 std::vector<Record*> Spellings = Accessor->getValueAsListOfDefs(
1046 "Spellings");
1047 std::vector<Record*> SpellingList = R.getValueAsListOfDefs("Spellings");
1048 assert(SpellingList.size() &&
1049 "Attribute with empty spelling list can't have accessors!");
1050
1051 OS << " bool " << Name << "() const { return SpellingListIndex == ";
1052 for (unsigned Index = 0; Index < Spellings.size(); ++Index) {
1053 OS << getSpellingListIndex(SpellingList, *Spellings[Index]);
1054 if (Index != Spellings.size() -1)
1055 OS << " ||\n SpellingListIndex == ";
1056 else
1057 OS << "; }\n";
1058 }
1059 }
1060}
1061
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001062namespace clang {
1063
1064// Emits the class definitions for attributes.
1065void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001066 emitSourceFileHeader("Attribute classes' definitions", OS);
1067
Peter Collingbournebee583f2011-10-06 13:03:08 +00001068 OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
1069 OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
1070
1071 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1072
1073 for (std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end();
1074 i != e; ++i) {
1075 Record &R = **i;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001076
1077 if (!R.getValueAsBit("ASTNode"))
1078 continue;
1079
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001080 const std::vector<Record *> Supers = R.getSuperClasses();
1081 assert(!Supers.empty() && "Forgot to specify a superclass for the attr");
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001082 std::string SuperName;
1083 for (std::vector<Record *>::const_reverse_iterator I = Supers.rbegin(),
1084 E = Supers.rend(); I != E; ++I) {
1085 const Record &R = **I;
Aaron Ballman080cad72013-07-31 02:20:22 +00001086 if (R.getName() != "TargetSpecificAttr" && SuperName.empty())
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001087 SuperName = R.getName();
1088 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001089
1090 OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
1091
1092 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1093 std::vector<Argument*> Args;
1094 std::vector<Argument*>::iterator ai, ae;
1095 Args.reserve(ArgRecords.size());
1096
1097 for (std::vector<Record*>::iterator ri = ArgRecords.begin(),
1098 re = ArgRecords.end();
1099 ri != re; ++ri) {
1100 Record &ArgRecord = **ri;
1101 Argument *Arg = createArgument(ArgRecord, R.getName());
1102 assert(Arg);
1103 Args.push_back(Arg);
1104
1105 Arg->writeDeclarations(OS);
1106 OS << "\n\n";
1107 }
1108
1109 ae = Args.end();
1110
1111 OS << "\n public:\n";
1112 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
1113
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001114 bool HasOpt = false;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001115 for (ai = Args.begin(); ai != ae; ++ai) {
1116 OS << " , ";
1117 (*ai)->writeCtorParameters(OS);
1118 OS << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001119 if ((*ai)->isOptional())
1120 HasOpt = true;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001121 }
Michael Han99315932013-01-24 16:46:58 +00001122
1123 OS << " , ";
1124 OS << "unsigned SI = 0\n";
1125
Peter Collingbournebee583f2011-10-06 13:03:08 +00001126 OS << " )\n";
Michael Han99315932013-01-24 16:46:58 +00001127 OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI)\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001128
1129 for (ai = Args.begin(); ai != ae; ++ai) {
1130 OS << " , ";
1131 (*ai)->writeCtorInitializers(OS);
1132 OS << "\n";
1133 }
1134
1135 OS << " {\n";
1136
1137 for (ai = Args.begin(); ai != ae; ++ai) {
1138 (*ai)->writeCtorBody(OS);
1139 OS << "\n";
1140 }
1141 OS << " }\n\n";
1142
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001143 // If there are optional arguments, write out a constructor that elides the
1144 // optional arguments as well.
1145 if (HasOpt) {
1146 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
1147 for (ai = Args.begin(); ai != ae; ++ai) {
1148 if (!(*ai)->isOptional()) {
1149 OS << " , ";
1150 (*ai)->writeCtorParameters(OS);
1151 OS << "\n";
1152 }
1153 }
1154
1155 OS << " , ";
1156 OS << "unsigned SI = 0\n";
1157
1158 OS << " )\n";
1159 OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI)\n";
1160
1161 for (ai = Args.begin(); ai != ae; ++ai) {
1162 OS << " , ";
1163 (*ai)->writeCtorDefaultInitializers(OS);
1164 OS << "\n";
1165 }
1166
1167 OS << " {\n";
1168
1169 for (ai = Args.begin(); ai != ae; ++ai) {
1170 if (!(*ai)->isOptional()) {
1171 (*ai)->writeCtorBody(OS);
1172 OS << "\n";
1173 }
1174 }
1175 OS << " }\n\n";
1176 }
1177
Peter Collingbournebee583f2011-10-06 13:03:08 +00001178 OS << " virtual " << R.getName() << "Attr *clone (ASTContext &C) const;\n";
Michael Han38d96ab2013-01-27 00:06:24 +00001179 OS << " virtual void printPretty(raw_ostream &OS,\n"
Richard Smith52f04a22012-08-16 02:43:29 +00001180 << " const PrintingPolicy &Policy) const;\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001181
Michael Hanaf02bbe2013-02-01 01:19:17 +00001182 writeAttrAccessorDefinition(R, OS);
1183
Peter Collingbournebee583f2011-10-06 13:03:08 +00001184 for (ai = Args.begin(); ai != ae; ++ai) {
1185 (*ai)->writeAccessors(OS);
1186 OS << "\n\n";
Aaron Ballman682ee422013-09-11 19:47:58 +00001187
1188 if ((*ai)->isEnumArg()) {
1189 EnumArgument *EA = (EnumArgument *)*ai;
1190 EA->writeConversion(OS);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001191 } else if ((*ai)->isVariadicEnumArg()) {
1192 VariadicEnumArgument *VEA = (VariadicEnumArgument *)*ai;
1193 VEA->writeConversion(OS);
Aaron Ballman682ee422013-09-11 19:47:58 +00001194 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001195 }
1196
Jakob Stoklund Olesen6f2288b62012-01-13 04:57:47 +00001197 OS << R.getValueAsString("AdditionalMembers");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001198 OS << "\n\n";
1199
1200 OS << " static bool classof(const Attr *A) { return A->getKind() == "
1201 << "attr::" << R.getName() << "; }\n";
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001202
1203 bool LateParsed = R.getValueAsBit("LateParsed");
1204 OS << " virtual bool isLateParsed() const { return "
1205 << LateParsed << "; }\n";
1206
Peter Collingbournebee583f2011-10-06 13:03:08 +00001207 OS << "};\n\n";
1208 }
1209
1210 OS << "#endif\n";
1211}
1212
Richard Smith66e71682013-10-24 01:07:54 +00001213static bool isIdentifierArgument(Record *Arg) {
1214 return !Arg->getSuperClasses().empty() &&
1215 llvm::StringSwitch<bool>(Arg->getSuperClasses().back()->getName())
1216 .Case("IdentifierArgument", true)
1217 .Case("EnumArgument", true)
1218 .Default(false);
1219}
1220
Aaron Ballman4768b312013-11-04 12:55:56 +00001221/// \brief Emits the first-argument-is-type property for attributes.
1222void EmitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) {
1223 emitSourceFileHeader("llvm::StringSwitch code to match attributes with a "
1224 "type argument", OS);
1225
1226 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
1227
1228 for (std::vector<Record *>::iterator I = Attrs.begin(), E = Attrs.end();
1229 I != E; ++I) {
1230 Record &Attr = **I;
1231
1232 // Determine whether the first argument is a type.
1233 std::vector<Record *> Args = Attr.getValueAsListOfDefs("Args");
1234 if (Args.empty())
1235 continue;
1236
1237 if (Args[0]->getSuperClasses().back()->getName() != "TypeArgument")
1238 continue;
1239
1240 // All these spellings take a single type argument.
1241 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
1242 std::set<std::string> Emitted;
1243 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
1244 E = Spellings.end(); I != E; ++I) {
1245 if (Emitted.insert((*I)->getValueAsString("Name")).second)
1246 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", "
1247 << "true" << ")\n";
1248 }
1249 }
1250}
1251
Richard Smith66e71682013-10-24 01:07:54 +00001252// Emits the first-argument-is-identifier property for attributes.
1253void EmitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) {
Douglas Gregord2472d42013-05-02 23:25:32 +00001254 emitSourceFileHeader("llvm::StringSwitch code to match attributes with "
Richard Smith66e71682013-10-24 01:07:54 +00001255 "an identifier argument", OS);
Douglas Gregord2472d42013-05-02 23:25:32 +00001256
1257 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1258
1259 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1260 I != E; ++I) {
1261 Record &Attr = **I;
1262
Richard Smith66e71682013-10-24 01:07:54 +00001263 // Determine whether the first argument is an identifier.
Douglas Gregord2472d42013-05-02 23:25:32 +00001264 std::vector<Record *> Args = Attr.getValueAsListOfDefs("Args");
Richard Smith66e71682013-10-24 01:07:54 +00001265 if (Args.empty() || !isIdentifierArgument(Args[0]))
Douglas Gregord2472d42013-05-02 23:25:32 +00001266 continue;
1267
Richard Smith66e71682013-10-24 01:07:54 +00001268 // All these spellings take an identifier argument.
Douglas Gregord2472d42013-05-02 23:25:32 +00001269 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
Richard Smith66e71682013-10-24 01:07:54 +00001270 std::set<std::string> Emitted;
Douglas Gregord2472d42013-05-02 23:25:32 +00001271 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
1272 E = Spellings.end(); I != E; ++I) {
Richard Smith66e71682013-10-24 01:07:54 +00001273 if (Emitted.insert((*I)->getValueAsString("Name")).second)
1274 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", "
1275 << "true" << ")\n";
Douglas Gregord2472d42013-05-02 23:25:32 +00001276 }
1277 }
1278}
1279
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001280// Emits the class method definitions for attributes.
1281void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001282 emitSourceFileHeader("Attribute classes' member function definitions", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001283
1284 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1285 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ri, re;
1286 std::vector<Argument*>::iterator ai, ae;
1287
1288 for (; i != e; ++i) {
1289 Record &R = **i;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001290
1291 if (!R.getValueAsBit("ASTNode"))
1292 continue;
1293
Peter Collingbournebee583f2011-10-06 13:03:08 +00001294 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1295 std::vector<Argument*> Args;
1296 for (ri = ArgRecords.begin(), re = ArgRecords.end(); ri != re; ++ri)
1297 Args.push_back(createArgument(**ri, R.getName()));
1298
1299 for (ai = Args.begin(), ae = Args.end(); ai != ae; ++ai)
1300 (*ai)->writeAccessorDefinitions(OS);
1301
1302 OS << R.getName() << "Attr *" << R.getName()
1303 << "Attr::clone(ASTContext &C) const {\n";
1304 OS << " return new (C) " << R.getName() << "Attr(getLocation(), C";
1305 for (ai = Args.begin(); ai != ae; ++ai) {
1306 OS << ", ";
1307 (*ai)->writeCloneArgs(OS);
1308 }
Richard Smitha5aaca92013-01-29 04:21:28 +00001309 OS << ", getSpellingListIndex());\n}\n\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001310
Michael Han99315932013-01-24 16:46:58 +00001311 writePrettyPrintFunction(R, Args, OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001312 }
1313}
1314
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001315} // end namespace clang
1316
Peter Collingbournebee583f2011-10-06 13:03:08 +00001317static void EmitAttrList(raw_ostream &OS, StringRef Class,
1318 const std::vector<Record*> &AttrList) {
1319 std::vector<Record*>::const_iterator i = AttrList.begin(), e = AttrList.end();
1320
1321 if (i != e) {
1322 // Move the end iterator back to emit the last attribute.
Douglas Gregorb2daf842012-05-02 15:56:52 +00001323 for(--e; i != e; ++i) {
1324 if (!(*i)->getValueAsBit("ASTNode"))
1325 continue;
1326
Peter Collingbournebee583f2011-10-06 13:03:08 +00001327 OS << Class << "(" << (*i)->getName() << ")\n";
Douglas Gregorb2daf842012-05-02 15:56:52 +00001328 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001329
1330 OS << "LAST_" << Class << "(" << (*i)->getName() << ")\n\n";
1331 }
1332}
1333
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001334namespace clang {
1335
1336// Emits the enumeration list for attributes.
1337void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001338 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001339
1340 OS << "#ifndef LAST_ATTR\n";
1341 OS << "#define LAST_ATTR(NAME) ATTR(NAME)\n";
1342 OS << "#endif\n\n";
1343
1344 OS << "#ifndef INHERITABLE_ATTR\n";
1345 OS << "#define INHERITABLE_ATTR(NAME) ATTR(NAME)\n";
1346 OS << "#endif\n\n";
1347
1348 OS << "#ifndef LAST_INHERITABLE_ATTR\n";
1349 OS << "#define LAST_INHERITABLE_ATTR(NAME) INHERITABLE_ATTR(NAME)\n";
1350 OS << "#endif\n\n";
1351
1352 OS << "#ifndef INHERITABLE_PARAM_ATTR\n";
1353 OS << "#define INHERITABLE_PARAM_ATTR(NAME) ATTR(NAME)\n";
1354 OS << "#endif\n\n";
1355
1356 OS << "#ifndef LAST_INHERITABLE_PARAM_ATTR\n";
1357 OS << "#define LAST_INHERITABLE_PARAM_ATTR(NAME)"
1358 " INHERITABLE_PARAM_ATTR(NAME)\n";
1359 OS << "#endif\n\n";
1360
Reid Kleckner42903612013-05-14 20:55:49 +00001361 OS << "#ifndef MS_INHERITANCE_ATTR\n";
1362 OS << "#define MS_INHERITANCE_ATTR(NAME) INHERITABLE_ATTR(NAME)\n";
Reid Kleckner6a476082013-03-26 18:30:28 +00001363 OS << "#endif\n\n";
1364
Reid Kleckner42903612013-05-14 20:55:49 +00001365 OS << "#ifndef LAST_MS_INHERITANCE_ATTR\n";
1366 OS << "#define LAST_MS_INHERITANCE_ATTR(NAME)"
1367 " MS_INHERITANCE_ATTR(NAME)\n";
Reid Kleckner6a476082013-03-26 18:30:28 +00001368 OS << "#endif\n\n";
1369
Peter Collingbournebee583f2011-10-06 13:03:08 +00001370 Record *InhClass = Records.getClass("InheritableAttr");
1371 Record *InhParamClass = Records.getClass("InheritableParamAttr");
Reid Kleckner6a476082013-03-26 18:30:28 +00001372 Record *MSInheritanceClass = Records.getClass("MSInheritanceAttr");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001373 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
Reid Kleckner6a476082013-03-26 18:30:28 +00001374 NonInhAttrs, InhAttrs, InhParamAttrs, MSInhAttrs;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001375 for (std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end();
1376 i != e; ++i) {
Douglas Gregorb2daf842012-05-02 15:56:52 +00001377 if (!(*i)->getValueAsBit("ASTNode"))
1378 continue;
1379
Peter Collingbournebee583f2011-10-06 13:03:08 +00001380 if ((*i)->isSubClassOf(InhParamClass))
1381 InhParamAttrs.push_back(*i);
Reid Kleckner6a476082013-03-26 18:30:28 +00001382 else if ((*i)->isSubClassOf(MSInheritanceClass))
1383 MSInhAttrs.push_back(*i);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001384 else if ((*i)->isSubClassOf(InhClass))
1385 InhAttrs.push_back(*i);
1386 else
1387 NonInhAttrs.push_back(*i);
1388 }
1389
1390 EmitAttrList(OS, "INHERITABLE_PARAM_ATTR", InhParamAttrs);
Reid Kleckner42903612013-05-14 20:55:49 +00001391 EmitAttrList(OS, "MS_INHERITANCE_ATTR", MSInhAttrs);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001392 EmitAttrList(OS, "INHERITABLE_ATTR", InhAttrs);
1393 EmitAttrList(OS, "ATTR", NonInhAttrs);
1394
1395 OS << "#undef LAST_ATTR\n";
1396 OS << "#undef INHERITABLE_ATTR\n";
Reid Kleckner42903612013-05-14 20:55:49 +00001397 OS << "#undef MS_INHERITANCE_ATTR\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001398 OS << "#undef LAST_INHERITABLE_ATTR\n";
1399 OS << "#undef LAST_INHERITABLE_PARAM_ATTR\n";
Reid Kleckner42903612013-05-14 20:55:49 +00001400 OS << "#undef LAST_MS_INHERITANCE_ATTR\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001401 OS << "#undef ATTR\n";
1402}
1403
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001404// Emits the code to read an attribute from a precompiled header.
1405void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001406 emitSourceFileHeader("Attribute deserialization code", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001407
1408 Record *InhClass = Records.getClass("InheritableAttr");
1409 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
1410 ArgRecords;
1411 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ai, ae;
1412 std::vector<Argument*> Args;
1413 std::vector<Argument*>::iterator ri, re;
1414
1415 OS << " switch (Kind) {\n";
1416 OS << " default:\n";
1417 OS << " assert(0 && \"Unknown attribute!\");\n";
1418 OS << " break;\n";
1419 for (; i != e; ++i) {
1420 Record &R = **i;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001421 if (!R.getValueAsBit("ASTNode"))
1422 continue;
1423
Peter Collingbournebee583f2011-10-06 13:03:08 +00001424 OS << " case attr::" << R.getName() << ": {\n";
1425 if (R.isSubClassOf(InhClass))
1426 OS << " bool isInherited = Record[Idx++];\n";
1427 ArgRecords = R.getValueAsListOfDefs("Args");
1428 Args.clear();
1429 for (ai = ArgRecords.begin(), ae = ArgRecords.end(); ai != ae; ++ai) {
1430 Argument *A = createArgument(**ai, R.getName());
1431 Args.push_back(A);
1432 A->writePCHReadDecls(OS);
1433 }
1434 OS << " New = new (Context) " << R.getName() << "Attr(Range, Context";
1435 for (ri = Args.begin(), re = Args.end(); ri != re; ++ri) {
1436 OS << ", ";
1437 (*ri)->writePCHReadArgs(OS);
1438 }
1439 OS << ");\n";
1440 if (R.isSubClassOf(InhClass))
1441 OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
1442 OS << " break;\n";
1443 OS << " }\n";
1444 }
1445 OS << " }\n";
1446}
1447
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001448// Emits the code to write an attribute to a precompiled header.
1449void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001450 emitSourceFileHeader("Attribute serialization code", OS);
1451
Peter Collingbournebee583f2011-10-06 13:03:08 +00001452 Record *InhClass = Records.getClass("InheritableAttr");
1453 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
1454 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ai, ae;
1455
1456 OS << " switch (A->getKind()) {\n";
1457 OS << " default:\n";
1458 OS << " llvm_unreachable(\"Unknown attribute kind!\");\n";
1459 OS << " break;\n";
1460 for (; i != e; ++i) {
1461 Record &R = **i;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001462 if (!R.getValueAsBit("ASTNode"))
1463 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001464 OS << " case attr::" << R.getName() << ": {\n";
1465 Args = R.getValueAsListOfDefs("Args");
1466 if (R.isSubClassOf(InhClass) || !Args.empty())
1467 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
1468 << "Attr>(A);\n";
1469 if (R.isSubClassOf(InhClass))
1470 OS << " Record.push_back(SA->isInherited());\n";
1471 for (ai = Args.begin(), ae = Args.end(); ai != ae; ++ai)
1472 createArgument(**ai, R.getName())->writePCHWrite(OS);
1473 OS << " break;\n";
1474 OS << " }\n";
1475 }
1476 OS << " }\n";
1477}
1478
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001479// Emits the list of spellings for attributes.
1480void EmitClangAttrSpellingList(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001481 emitSourceFileHeader("llvm::StringSwitch code to match all known attributes",
1482 OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001483
1484 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1485
1486 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
1487 Record &Attr = **I;
1488
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001489 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001490
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001491 for (std::vector<Record*>::const_iterator I = Spellings.begin(), E = Spellings.end(); I != E; ++I) {
1492 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", true)\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001493 }
1494 }
1495
1496}
1497
Michael Han99315932013-01-24 16:46:58 +00001498void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001499 emitSourceFileHeader("Code to translate different attribute spellings "
1500 "into internal identifiers", OS);
Michael Han99315932013-01-24 16:46:58 +00001501
1502 OS <<
1503 " unsigned Index = 0;\n"
1504 " switch (AttrKind) {\n"
1505 " default:\n"
1506 " llvm_unreachable(\"Unknown attribute kind!\");\n"
1507 " break;\n";
1508
1509 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1510 for (std::vector<Record*>::const_iterator I = Attrs.begin(), E = Attrs.end();
1511 I != E; ++I) {
1512 Record &R = **I;
1513 // We only care about attributes that participate in Sema checking, so
1514 // skip those attributes that are not able to make their way to Sema.
1515 if (!R.getValueAsBit("SemaHandler"))
1516 continue;
1517
1518 std::vector<Record*> Spellings = R.getValueAsListOfDefs("Spellings");
Richard Smith852e9ce2013-11-27 01:46:48 +00001519 OS << " case AT_" << R.getName() << " : {\n";
1520 for (unsigned I = 0; I < Spellings.size(); ++ I) {
1521 SmallString<16> Namespace;
1522 if (Spellings[I]->getValueAsString("Variety") == "CXX11")
1523 Namespace = Spellings[I]->getValueAsString("Namespace");
1524 else
1525 Namespace = "";
Michael Han99315932013-01-24 16:46:58 +00001526
Richard Smith852e9ce2013-11-27 01:46:48 +00001527 OS << " if (Name == \""
1528 << Spellings[I]->getValueAsString("Name") << "\" && "
1529 << "SyntaxUsed == "
1530 << StringSwitch<unsigned>(Spellings[I]->getValueAsString("Variety"))
1531 .Case("GNU", 0)
1532 .Case("CXX11", 1)
1533 .Case("Declspec", 2)
1534 .Case("Keyword", 3)
1535 .Default(0)
1536 << " && Scope == \"" << Namespace << "\")\n"
1537 << " return " << I << ";\n";
Michael Han99315932013-01-24 16:46:58 +00001538 }
Richard Smith852e9ce2013-11-27 01:46:48 +00001539
1540 OS << " break;\n";
1541 OS << " }\n";
Michael Han99315932013-01-24 16:46:58 +00001542 }
1543
1544 OS << " }\n";
1545 OS << " return Index;\n";
1546}
1547
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001548// Emits the LateParsed property for attributes.
1549void EmitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001550 emitSourceFileHeader("llvm::StringSwitch code to match late parsed "
1551 "attributes", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001552
1553 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1554
1555 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1556 I != E; ++I) {
1557 Record &Attr = **I;
1558
1559 bool LateParsed = Attr.getValueAsBit("LateParsed");
1560
1561 if (LateParsed) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001562 std::vector<Record*> Spellings =
1563 Attr.getValueAsListOfDefs("Spellings");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001564
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001565 // FIXME: Handle non-GNU attributes
1566 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
Peter Collingbournebee583f2011-10-06 13:03:08 +00001567 E = Spellings.end(); I != E; ++I) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001568 if ((*I)->getValueAsString("Variety") != "GNU")
1569 continue;
1570 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", "
1571 << LateParsed << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001572 }
1573 }
1574 }
1575}
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001576
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001577// Emits code to instantiate dependent attributes on templates.
1578void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001579 emitSourceFileHeader("Template instantiation code for attributes", OS);
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001580
1581 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1582
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00001583 OS << "namespace clang {\n"
1584 << "namespace sema {\n\n"
1585 << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, "
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001586 << "Sema &S,\n"
1587 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"
1588 << " switch (At->getKind()) {\n"
1589 << " default:\n"
1590 << " break;\n";
1591
1592 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1593 I != E; ++I) {
1594 Record &R = **I;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001595 if (!R.getValueAsBit("ASTNode"))
1596 continue;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001597
1598 OS << " case attr::" << R.getName() << ": {\n";
Rafael Espindola7f90b7d2012-05-15 14:09:55 +00001599 bool ShouldClone = R.getValueAsBit("Clone");
1600
1601 if (!ShouldClone) {
1602 OS << " return NULL;\n";
1603 OS << " }\n";
1604 continue;
1605 }
1606
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001607 OS << " const " << R.getName() << "Attr *A = cast<"
1608 << R.getName() << "Attr>(At);\n";
1609 bool TDependent = R.getValueAsBit("TemplateDependent");
1610
1611 if (!TDependent) {
1612 OS << " return A->clone(C);\n";
1613 OS << " }\n";
1614 continue;
1615 }
1616
1617 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1618 std::vector<Argument*> Args;
1619 std::vector<Argument*>::iterator ai, ae;
1620 Args.reserve(ArgRecords.size());
1621
1622 for (std::vector<Record*>::iterator ri = ArgRecords.begin(),
1623 re = ArgRecords.end();
1624 ri != re; ++ri) {
1625 Record &ArgRecord = **ri;
1626 Argument *Arg = createArgument(ArgRecord, R.getName());
1627 assert(Arg);
1628 Args.push_back(Arg);
1629 }
1630 ae = Args.end();
1631
1632 for (ai = Args.begin(); ai != ae; ++ai) {
1633 (*ai)->writeTemplateInstantiation(OS);
1634 }
1635 OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C";
1636 for (ai = Args.begin(); ai != ae; ++ai) {
1637 OS << ", ";
1638 (*ai)->writeTemplateInstantiationArgs(OS);
1639 }
1640 OS << ");\n }\n";
1641 }
1642 OS << " } // end switch\n"
1643 << " llvm_unreachable(\"Unknown attribute!\");\n"
1644 << " return 0;\n"
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00001645 << "}\n\n"
1646 << "} // end namespace sema\n"
1647 << "} // end namespace clang\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001648}
1649
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001650typedef std::vector<std::pair<std::string, Record *> > ParsedAttrMap;
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001651
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001652static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records) {
Michael Han4a045172012-03-07 00:12:16 +00001653 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001654 ParsedAttrMap R;
Michael Han4a045172012-03-07 00:12:16 +00001655 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1656 I != E; ++I) {
1657 Record &Attr = **I;
Richard Smith852e9ce2013-11-27 01:46:48 +00001658 if (Attr.getValueAsBit("SemaHandler")) {
1659 StringRef AttrName = Attr.getName();
1660 AttrName = NormalizeAttrName(AttrName);
1661 R.push_back(std::make_pair(AttrName.str(), *I));
Michael Han4a045172012-03-07 00:12:16 +00001662 }
1663 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001664 return R;
1665}
1666
1667// Emits the list of parsed attributes.
1668void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) {
1669 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
1670
1671 OS << "#ifndef PARSED_ATTR\n";
1672 OS << "#define PARSED_ATTR(NAME) NAME\n";
1673 OS << "#endif\n\n";
1674
1675 ParsedAttrMap Names = getParsedAttrList(Records);
1676 for (ParsedAttrMap::iterator I = Names.begin(), E = Names.end(); I != E;
1677 ++I) {
1678 OS << "PARSED_ATTR(" << I->first << ")\n";
1679 }
1680}
1681
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001682static void emitArgInfo(const Record &R, std::stringstream &OS) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001683 // This function will count the number of arguments specified for the
1684 // attribute and emit the number of required arguments followed by the
1685 // number of optional arguments.
1686 std::vector<Record *> Args = R.getValueAsListOfDefs("Args");
1687 unsigned ArgCount = 0, OptCount = 0;
1688 for (std::vector<Record *>::const_iterator I = Args.begin(), E = Args.end();
1689 I != E; ++I) {
1690 const Record &Arg = **I;
1691 Arg.getValueAsBit("Optional") ? ++OptCount : ++ArgCount;
1692 }
1693 OS << ArgCount << ", " << OptCount;
1694}
1695
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001696static void GenerateDefaultAppertainsTo(raw_ostream &OS) {
Aaron Ballman93b5cc62013-12-02 19:36:42 +00001697 OS << "static bool defaultAppertainsTo(Sema &, const AttributeList &,";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001698 OS << "const Decl *) {\n";
1699 OS << " return true;\n";
1700 OS << "}\n\n";
1701}
1702
1703static std::string CalculateDiagnostic(const Record &S) {
1704 // If the SubjectList object has a custom diagnostic associated with it,
1705 // return that directly.
1706 std::string CustomDiag = S.getValueAsString("CustomDiag");
1707 if (!CustomDiag.empty())
1708 return CustomDiag;
1709
1710 // Given the list of subjects, determine what diagnostic best fits.
1711 enum {
1712 Func = 1U << 0,
1713 Var = 1U << 1,
1714 ObjCMethod = 1U << 2,
1715 Param = 1U << 3,
1716 Class = 1U << 4,
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00001717 GenericRecord = 1U << 5,
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001718 Type = 1U << 6,
1719 ObjCIVar = 1U << 7,
1720 ObjCProp = 1U << 8,
1721 ObjCInterface = 1U << 9,
1722 Block = 1U << 10,
1723 Namespace = 1U << 11,
1724 FuncTemplate = 1U << 12,
1725 Field = 1U << 13,
1726 CXXMethod = 1U << 14
1727 };
1728 uint32_t SubMask = 0;
1729
1730 std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects");
1731 for (std::vector<Record *>::const_iterator I = Subjects.begin(),
1732 E = Subjects.end(); I != E; ++I) {
Aaron Ballman80469032013-11-29 14:57:58 +00001733 const Record &R = (**I);
1734 std::string Name;
1735
1736 if (R.isSubClassOf("SubsetSubject")) {
1737 PrintError(R.getLoc(), "SubsetSubjects should use a custom diagnostic");
1738 // As a fallback, look through the SubsetSubject to see what its base
1739 // type is, and use that. This needs to be updated if SubsetSubjects
1740 // are allowed within other SubsetSubjects.
1741 Name = R.getValueAsDef("Base")->getName();
1742 } else
1743 Name = R.getName();
1744
1745 uint32_t V = StringSwitch<uint32_t>(Name)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001746 .Case("Function", Func)
1747 .Case("Var", Var)
1748 .Case("ObjCMethod", ObjCMethod)
1749 .Case("ParmVar", Param)
1750 .Case("TypedefName", Type)
1751 .Case("ObjCIvar", ObjCIVar)
1752 .Case("ObjCProperty", ObjCProp)
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00001753 .Case("Record", GenericRecord)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001754 .Case("ObjCInterface", ObjCInterface)
1755 .Case("Block", Block)
1756 .Case("CXXRecord", Class)
1757 .Case("Namespace", Namespace)
1758 .Case("FunctionTemplate", FuncTemplate)
1759 .Case("Field", Field)
1760 .Case("CXXMethod", CXXMethod)
1761 .Default(0);
1762 if (!V) {
1763 // Something wasn't in our mapping, so be helpful and let the developer
1764 // know about it.
1765 PrintFatalError((*I)->getLoc(), "Unknown subject type: " +
1766 (*I)->getName());
1767 return "";
1768 }
1769
1770 SubMask |= V;
1771 }
1772
1773 switch (SubMask) {
1774 // For the simple cases where there's only a single entry in the mask, we
1775 // don't have to resort to bit fiddling.
1776 case Func: return "ExpectedFunction";
1777 case Var: return "ExpectedVariable";
1778 case Param: return "ExpectedParameter";
1779 case Class: return "ExpectedClass";
1780 case CXXMethod:
1781 // FIXME: Currently, this maps to ExpectedMethod based on existing code,
1782 // but should map to something a bit more accurate at some point.
1783 case ObjCMethod: return "ExpectedMethod";
1784 case Type: return "ExpectedType";
1785 case ObjCInterface: return "ExpectedObjectiveCInterface";
1786
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00001787 // "GenericRecord" means struct, union or class; check the language options
1788 // and if not compiling for C++, strip off the class part. Note that this
1789 // relies on the fact that the context for this declares "Sema &S".
1790 case GenericRecord:
Aaron Ballman17046b82013-11-27 19:16:55 +00001791 return "(S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass : "
1792 "ExpectedStructOrUnion)";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001793 case Func | ObjCMethod | Block: return "ExpectedFunctionMethodOrBlock";
1794 case Func | ObjCMethod | Class: return "ExpectedFunctionMethodOrClass";
1795 case Func | Param:
1796 case Func | ObjCMethod | Param: return "ExpectedFunctionMethodOrParameter";
1797 case Func | FuncTemplate:
1798 case Func | ObjCMethod: return "ExpectedFunctionOrMethod";
1799 case Func | Var: return "ExpectedVariableOrFunction";
Aaron Ballman604dfec2013-12-02 17:07:07 +00001800
1801 // If not compiling for C++, the class portion does not apply.
1802 case Func | Var | Class:
1803 return "(S.getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass : "
1804 "ExpectedVariableOrFunction)";
1805
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001806 case ObjCMethod | ObjCProp: return "ExpectedMethodOrProperty";
1807 case Field | Var: return "ExpectedFieldOrGlobalVar";
1808 }
1809
1810 PrintFatalError(S.getLoc(),
1811 "Could not deduce diagnostic argument for Attr subjects");
1812
1813 return "";
1814}
1815
Aaron Ballman80469032013-11-29 14:57:58 +00001816static std::string GenerateCustomAppertainsTo(const Record &Subject,
1817 raw_ostream &OS) {
Aaron Ballmana358c902013-12-02 14:58:17 +00001818 std::string FnName = "is" + Subject.getName();
1819
Aaron Ballman80469032013-11-29 14:57:58 +00001820 // If this code has already been generated, simply return the previous
1821 // instance of it.
1822 static std::set<std::string> CustomSubjectSet;
Aaron Ballmana358c902013-12-02 14:58:17 +00001823 std::set<std::string>::iterator I = CustomSubjectSet.find(FnName);
Aaron Ballman80469032013-11-29 14:57:58 +00001824 if (I != CustomSubjectSet.end())
1825 return *I;
1826
1827 Record *Base = Subject.getValueAsDef("Base");
1828
1829 // Not currently support custom subjects within custom subjects.
1830 if (Base->isSubClassOf("SubsetSubject")) {
1831 PrintFatalError(Subject.getLoc(),
1832 "SubsetSubjects within SubsetSubjects is not supported");
1833 return "";
1834 }
1835
Aaron Ballman80469032013-11-29 14:57:58 +00001836 OS << "static bool " << FnName << "(const Decl *D) {\n";
Aaron Ballman4cfafb92013-11-29 16:12:29 +00001837 OS << " const " << Base->getName() << "Decl *S = dyn_cast<";
1838 OS << Base->getName();
Aaron Ballman80469032013-11-29 14:57:58 +00001839 OS << "Decl>(D);\n";
Aaron Ballman4cfafb92013-11-29 16:12:29 +00001840 OS << " return S && " << Subject.getValueAsString("CheckCode") << ";\n";
Aaron Ballman80469032013-11-29 14:57:58 +00001841 OS << "}\n\n";
1842
1843 CustomSubjectSet.insert(FnName);
1844 return FnName;
1845}
1846
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001847static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) {
1848 // If the attribute does not contain a Subjects definition, then use the
1849 // default appertainsTo logic.
1850 if (Attr.isValueUnset("Subjects"))
Aaron Ballman93b5cc62013-12-02 19:36:42 +00001851 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001852
1853 const Record *SubjectObj = Attr.getValueAsDef("Subjects");
1854 std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
1855
1856 // If the list of subjects is empty, it is assumed that the attribute
1857 // appertains to everything.
1858 if (Subjects.empty())
Aaron Ballman93b5cc62013-12-02 19:36:42 +00001859 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001860
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001861 bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn");
1862
1863 // Otherwise, generate an appertainsTo check specific to this attribute which
1864 // checks all of the given subjects against the Decl passed in. Return the
1865 // name of that check to the caller.
Aaron Ballman00dcc432013-12-03 13:45:50 +00001866 std::string FnName = "check" + Attr.getName() + "AppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001867 std::stringstream SS;
1868 SS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr, ";
1869 SS << "const Decl *D) {\n";
1870 SS << " if (";
1871 for (std::vector<Record *>::const_iterator I = Subjects.begin(),
1872 E = Subjects.end(); I != E; ++I) {
Aaron Ballman80469032013-11-29 14:57:58 +00001873 // If the subject has custom code associated with it, generate a function
1874 // for it. The function cannot be inlined into this check (yet) because it
1875 // requires the subject to be of a specific type, and were that information
1876 // inlined here, it would not support an attribute with multiple custom
1877 // subjects.
1878 if ((*I)->isSubClassOf("SubsetSubject")) {
1879 SS << "!" << GenerateCustomAppertainsTo(**I, OS) << "(D)";
1880 } else {
1881 SS << "!isa<" << (*I)->getName() << "Decl>(D)";
1882 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001883
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001884 if (I + 1 != E)
1885 SS << " && ";
1886 }
1887 SS << ") {\n";
1888 SS << " S.Diag(Attr.getLoc(), diag::";
1889 SS << (Warn ? "warn_attribute_wrong_decl_type" :
1890 "err_attribute_wrong_decl_type");
1891 SS << ")\n";
1892 SS << " << Attr.getName() << ";
1893 SS << CalculateDiagnostic(*SubjectObj) << ";\n";
1894 SS << " return false;\n";
1895 SS << " }\n";
1896 SS << " return true;\n";
1897 SS << "}\n\n";
1898
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001899 OS << SS.str();
1900 return FnName;
1901}
1902
Aaron Ballman3aff6332013-12-02 19:30:36 +00001903static void GenerateDefaultLangOptRequirements(raw_ostream &OS) {
1904 OS << "static bool defaultDiagnoseLangOpts(Sema &, ";
1905 OS << "const AttributeList &) {\n";
1906 OS << " return true;\n";
1907 OS << "}\n\n";
1908}
1909
1910static std::string GenerateLangOptRequirements(const Record &R,
1911 raw_ostream &OS) {
1912 // If the attribute has an empty or unset list of language requirements,
1913 // return the default handler.
1914 std::vector<Record *> LangOpts = R.getValueAsListOfDefs("LangOpts");
1915 if (LangOpts.empty())
1916 return "defaultDiagnoseLangOpts";
1917
1918 // Generate the test condition, as well as a unique function name for the
1919 // diagnostic test. The list of options should usually be short (one or two
1920 // options), and the uniqueness isn't strictly necessary (it is just for
1921 // codegen efficiency).
1922 std::string FnName = "check", Test;
1923 for (std::vector<Record *>::const_iterator I = LangOpts.begin(),
1924 E = LangOpts.end(); I != E; ++I) {
1925 std::string Part = (*I)->getValueAsString("Name");
1926 Test += "S.LangOpts." + Part;
1927 if (I + 1 != E)
1928 Test += " || ";
1929 FnName += Part;
1930 }
1931 FnName += "LangOpts";
1932
1933 // If this code has already been generated, simply return the previous
1934 // instance of it.
1935 static std::set<std::string> CustomLangOptsSet;
1936 std::set<std::string>::iterator I = CustomLangOptsSet.find(FnName);
1937 if (I != CustomLangOptsSet.end())
1938 return *I;
1939
1940 OS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr) {\n";
1941 OS << " if (" << Test << ")\n";
1942 OS << " return true;\n\n";
1943 OS << " S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) ";
1944 OS << "<< Attr.getName();\n";
1945 OS << " return false;\n";
1946 OS << "}\n\n";
1947
1948 CustomLangOptsSet.insert(FnName);
1949 return FnName;
1950}
1951
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001952/// Emits the parsed attribute helpers
1953void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
1954 emitSourceFileHeader("Parsed attribute helpers", OS);
1955
1956 ParsedAttrMap Attrs = getParsedAttrList(Records);
1957
Aaron Ballman3aff6332013-12-02 19:30:36 +00001958 // Generate the default appertainsTo and language option diagnostic methods.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001959 GenerateDefaultAppertainsTo(OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00001960 GenerateDefaultLangOptRequirements(OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001961
1962 // Generate the appertainsTo diagnostic methods and write their names into
1963 // another mapping. At the same time, generate the AttrInfoMap object
1964 // contents. Due to the reliance on generated code, use separate streams so
1965 // that code will not be interleaved.
1966 std::stringstream SS;
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001967 for (ParsedAttrMap::iterator I = Attrs.begin(), E = Attrs.end(); I != E;
1968 ++I) {
1969 // We need to generate struct instances based off ParsedAttrInfo from
1970 // AttributeList.cpp.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001971 SS << " { ";
1972 emitArgInfo(*I->second, SS);
1973 SS << ", " << I->second->getValueAsBit("HasCustomParsing");
1974 SS << ", " << GenerateAppertainsTo(*I->second, OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00001975 SS << ", " << GenerateLangOptRequirements(*I->second, OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001976 SS << " }";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001977
1978 if (I + 1 != E)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001979 SS << ",";
1980
1981 SS << " // AT_" << I->first << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001982 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001983
1984 OS << "static const ParsedAttrInfo AttrInfoMap[AttributeList::UnknownAttribute + 1] = {\n";
1985 OS << SS.str();
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001986 OS << "};\n\n";
Michael Han4a045172012-03-07 00:12:16 +00001987}
1988
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001989// Emits the kind list of parsed attributes
1990void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001991 emitSourceFileHeader("Attribute name matcher", OS);
1992
Michael Han4a045172012-03-07 00:12:16 +00001993 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1994
Douglas Gregor377f99b2012-05-02 17:33:51 +00001995 std::vector<StringMatcher::StringPair> Matches;
Michael Han4a045172012-03-07 00:12:16 +00001996 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1997 I != E; ++I) {
1998 Record &Attr = **I;
Richard Smith852e9ce2013-11-27 01:46:48 +00001999
Michael Han4a045172012-03-07 00:12:16 +00002000 bool SemaHandler = Attr.getValueAsBit("SemaHandler");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00002001 bool Ignored = Attr.getValueAsBit("Ignored");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00002002 if (SemaHandler || Ignored) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002003 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
Michael Han4a045172012-03-07 00:12:16 +00002004
Richard Smith852e9ce2013-11-27 01:46:48 +00002005 StringRef AttrName = NormalizeAttrName(StringRef(Attr.getName()));
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002006 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
Michael Han4a045172012-03-07 00:12:16 +00002007 E = Spellings.end(); I != E; ++I) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002008 std::string RawSpelling = (*I)->getValueAsString("Name");
Michael Han4a045172012-03-07 00:12:16 +00002009
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002010 SmallString<64> Spelling;
2011 if ((*I)->getValueAsString("Variety") == "CXX11") {
2012 Spelling += (*I)->getValueAsString("Namespace");
2013 Spelling += "::";
Alexis Hunta0e54d42012-06-18 16:13:52 +00002014 }
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002015 Spelling += NormalizeAttrSpelling(RawSpelling);
Alexis Hunta0e54d42012-06-18 16:13:52 +00002016
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00002017 if (SemaHandler)
Douglas Gregor377f99b2012-05-02 17:33:51 +00002018 Matches.push_back(
Douglas Gregor87a170c2012-05-11 23:37:49 +00002019 StringMatcher::StringPair(
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002020 StringRef(Spelling),
Alexis Hunta0e54d42012-06-18 16:13:52 +00002021 "return AttributeList::AT_" + AttrName.str() + ";"));
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00002022 else
Douglas Gregor377f99b2012-05-02 17:33:51 +00002023 Matches.push_back(
2024 StringMatcher::StringPair(
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002025 StringRef(Spelling),
Alexis Hunta0e54d42012-06-18 16:13:52 +00002026 "return AttributeList::IgnoredAttribute;"));
Michael Han4a045172012-03-07 00:12:16 +00002027 }
2028 }
2029 }
Douglas Gregor377f99b2012-05-02 17:33:51 +00002030
2031 OS << "static AttributeList::Kind getAttrKind(StringRef Name) {\n";
2032 StringMatcher("Name", Matches, OS).Emit();
2033 OS << "return AttributeList::UnknownAttribute;\n"
2034 << "}\n";
Michael Han4a045172012-03-07 00:12:16 +00002035}
2036
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00002037// Emits the code to dump an attribute.
2038void EmitClangAttrDump(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002039 emitSourceFileHeader("Attribute dumper", OS);
2040
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00002041 OS <<
2042 " switch (A->getKind()) {\n"
2043 " default:\n"
2044 " llvm_unreachable(\"Unknown attribute kind!\");\n"
2045 " break;\n";
2046 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
2047 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
2048 I != E; ++I) {
2049 Record &R = **I;
2050 if (!R.getValueAsBit("ASTNode"))
2051 continue;
2052 OS << " case attr::" << R.getName() << ": {\n";
2053 Args = R.getValueAsListOfDefs("Args");
2054 if (!Args.empty()) {
2055 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
2056 << "Attr>(A);\n";
2057 for (std::vector<Record*>::iterator I = Args.begin(), E = Args.end();
2058 I != E; ++I)
2059 createArgument(**I, R.getName())->writeDump(OS);
Richard Trieude5cc7d2013-01-31 01:44:26 +00002060
2061 // Code for detecting the last child.
2062 OS << " bool OldMoreChildren = hasMoreChildren();\n";
2063 OS << " bool MoreChildren = OldMoreChildren;\n";
2064
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00002065 for (std::vector<Record*>::iterator I = Args.begin(), E = Args.end();
Richard Trieude5cc7d2013-01-31 01:44:26 +00002066 I != E; ++I) {
2067 // More code for detecting the last child.
2068 OS << " MoreChildren = OldMoreChildren";
2069 for (std::vector<Record*>::iterator Next = I + 1; Next != E; ++Next) {
2070 OS << " || ";
2071 createArgument(**Next, R.getName())->writeHasChildren(OS);
2072 }
2073 OS << ";\n";
2074 OS << " setMoreChildren(MoreChildren);\n";
2075
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00002076 createArgument(**I, R.getName())->writeDumpChildren(OS);
Richard Trieude5cc7d2013-01-31 01:44:26 +00002077 }
2078
2079 // Reset the last child.
2080 OS << " setMoreChildren(OldMoreChildren);\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00002081 }
2082 OS <<
2083 " break;\n"
2084 " }\n";
2085 }
2086 OS << " }\n";
2087}
2088
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002089} // end namespace clang