blob: f06a1cb7464886fc897bbe7a435961323455db47 [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"
Aaron Ballman8ee40b72013-09-09 23:33:17 +000015#include "llvm/ADT/SmallSet.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000016#include "llvm/ADT/StringSwitch.h"
Aaron Ballman36a53502014-01-16 13:03:14 +000017#include "llvm/ADT/STLExtras.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000018#include "llvm/TableGen/Error.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000019#include "llvm/TableGen/Record.h"
Douglas Gregor377f99b2012-05-02 17:33:51 +000020#include "llvm/TableGen/StringMatcher.h"
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000021#include "llvm/TableGen/TableGenBackend.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000022#include <algorithm>
23#include <cctype>
Aaron Ballman80469032013-11-29 14:57:58 +000024#include <set>
Chandler Carruth5553d0d2014-01-07 11:51:46 +000025#include <sstream>
Peter Collingbournebee583f2011-10-06 13:03:08 +000026
27using namespace llvm;
28
Peter Collingbournebee583f2011-10-06 13:03:08 +000029static std::string ReadPCHRecord(StringRef type) {
30 return StringSwitch<std::string>(type)
31 .EndsWith("Decl *", "GetLocalDeclAs<"
32 + std::string(type, 0, type.size()-1) + ">(F, Record[Idx++])")
Richard Smithb87c4652013-10-31 21:23:20 +000033 .Case("TypeSourceInfo *", "GetTypeSourceInfo(F, Record, Idx)")
Argyrios Kyrtzidisa660ae42012-11-15 01:31:39 +000034 .Case("Expr *", "ReadExpr(F)")
Peter Collingbournebee583f2011-10-06 13:03:08 +000035 .Case("IdentifierInfo *", "GetIdentifierInfo(F, Record, Idx)")
Peter Collingbournebee583f2011-10-06 13:03:08 +000036 .Default("Record[Idx++]");
37}
38
39// Assumes that the way to get the value is SA->getname()
40static std::string WritePCHRecord(StringRef type, StringRef name) {
41 return StringSwitch<std::string>(type)
42 .EndsWith("Decl *", "AddDeclRef(" + std::string(name) +
43 ", Record);\n")
Richard Smithb87c4652013-10-31 21:23:20 +000044 .Case("TypeSourceInfo *",
45 "AddTypeSourceInfo(" + std::string(name) + ", Record);\n")
Peter Collingbournebee583f2011-10-06 13:03:08 +000046 .Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
47 .Case("IdentifierInfo *",
48 "AddIdentifierRef(" + std::string(name) + ", Record);\n")
Peter Collingbournebee583f2011-10-06 13:03:08 +000049 .Default("Record.push_back(" + std::string(name) + ");\n");
50}
51
Michael Han4a045172012-03-07 00:12:16 +000052// Normalize attribute name by removing leading and trailing
53// underscores. For example, __foo, foo__, __foo__ would
54// become foo.
55static StringRef NormalizeAttrName(StringRef AttrName) {
56 if (AttrName.startswith("__"))
57 AttrName = AttrName.substr(2, AttrName.size());
58
59 if (AttrName.endswith("__"))
60 AttrName = AttrName.substr(0, AttrName.size() - 2);
61
62 return AttrName;
63}
64
Aaron Ballman36a53502014-01-16 13:03:14 +000065// Normalize the name by removing any and all leading and trailing underscores.
66// This is different from NormalizeAttrName in that it also handles names like
67// _pascal and __pascal.
68static StringRef NormalizeNameForSpellingComparison(StringRef Name) {
69 while (Name.startswith("_"))
70 Name = Name.substr(1, Name.size());
71 while (Name.endswith("_"))
72 Name = Name.substr(0, Name.size() - 1);
73 return Name;
74}
75
Michael Han4a045172012-03-07 00:12:16 +000076// Normalize attribute spelling only if the spelling has both leading
77// and trailing underscores. For example, __ms_struct__ will be
78// normalized to "ms_struct"; __cdecl will remain intact.
79static StringRef NormalizeAttrSpelling(StringRef AttrSpelling) {
80 if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) {
81 AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4);
82 }
83
84 return AttrSpelling;
85}
86
Aaron Ballman64e69862013-12-15 13:05:48 +000087typedef std::vector<std::pair<std::string, Record *> > ParsedAttrMap;
88
Aaron Ballmanab7691c2014-01-09 22:48:32 +000089static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,
90 ParsedAttrMap *Dupes = 0) {
Aaron Ballman64e69862013-12-15 13:05:48 +000091 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
92 std::set<std::string> Seen;
93 ParsedAttrMap R;
94 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
95 I != E; ++I) {
96 Record &Attr = **I;
97 if (Attr.getValueAsBit("SemaHandler")) {
98 std::string AN;
99 if (Attr.isSubClassOf("TargetSpecificAttr") &&
100 !Attr.isValueUnset("ParseKind")) {
101 AN = Attr.getValueAsString("ParseKind");
102
103 // If this attribute has already been handled, it does not need to be
104 // handled again.
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000105 if (Seen.find(AN) != Seen.end()) {
106 if (Dupes)
107 Dupes->push_back(std::make_pair(AN, *I));
Aaron Ballman64e69862013-12-15 13:05:48 +0000108 continue;
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000109 }
Aaron Ballman64e69862013-12-15 13:05:48 +0000110 Seen.insert(AN);
111 } else
112 AN = NormalizeAttrName(Attr.getName()).str();
113
114 R.push_back(std::make_pair(AN, *I));
115 }
116 }
117 return R;
118}
119
Peter Collingbournebee583f2011-10-06 13:03:08 +0000120namespace {
121 class Argument {
122 std::string lowerName, upperName;
123 StringRef attrName;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000124 bool isOpt;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000125
126 public:
127 Argument(Record &Arg, StringRef Attr)
128 : lowerName(Arg.getValueAsString("Name")), upperName(lowerName),
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000129 attrName(Attr), isOpt(false) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000130 if (!lowerName.empty()) {
131 lowerName[0] = std::tolower(lowerName[0]);
132 upperName[0] = std::toupper(upperName[0]);
133 }
134 }
135 virtual ~Argument() {}
136
137 StringRef getLowerName() const { return lowerName; }
138 StringRef getUpperName() const { return upperName; }
139 StringRef getAttrName() const { return attrName; }
140
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000141 bool isOptional() const { return isOpt; }
142 void setOptional(bool set) { isOpt = set; }
143
Peter Collingbournebee583f2011-10-06 13:03:08 +0000144 // These functions print the argument contents formatted in different ways.
145 virtual void writeAccessors(raw_ostream &OS) const = 0;
146 virtual void writeAccessorDefinitions(raw_ostream &OS) const {}
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +0000147 virtual void writeASTVisitorTraversal(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000148 virtual void writeCloneArgs(raw_ostream &OS) const = 0;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000149 virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0;
Daniel Dunbardc51baa2012-02-10 06:00:29 +0000150 virtual void writeTemplateInstantiation(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000151 virtual void writeCtorBody(raw_ostream &OS) const {}
152 virtual void writeCtorInitializers(raw_ostream &OS) const = 0;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000153 virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000154 virtual void writeCtorParameters(raw_ostream &OS) const = 0;
155 virtual void writeDeclarations(raw_ostream &OS) const = 0;
156 virtual void writePCHReadArgs(raw_ostream &OS) const = 0;
157 virtual void writePCHReadDecls(raw_ostream &OS) const = 0;
158 virtual void writePCHWrite(raw_ostream &OS) const = 0;
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000159 virtual void writeValue(raw_ostream &OS) const = 0;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000160 virtual void writeDump(raw_ostream &OS) const = 0;
161 virtual void writeDumpChildren(raw_ostream &OS) const {}
Richard Trieude5cc7d2013-01-31 01:44:26 +0000162 virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000163
164 virtual bool isEnumArg() const { return false; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000165 virtual bool isVariadicEnumArg() const { return false; }
Aaron Ballman36a53502014-01-16 13:03:14 +0000166
167 virtual void writeImplicitCtorArgs(raw_ostream &OS) const {
168 OS << getUpperName();
169 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000170 };
171
172 class SimpleArgument : public Argument {
173 std::string type;
174
175 public:
176 SimpleArgument(Record &Arg, StringRef Attr, std::string T)
177 : Argument(Arg, Attr), type(T)
178 {}
179
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000180 std::string getType() const { return type; }
181
Peter Collingbournebee583f2011-10-06 13:03:08 +0000182 void writeAccessors(raw_ostream &OS) const {
183 OS << " " << type << " get" << getUpperName() << "() const {\n";
184 OS << " return " << getLowerName() << ";\n";
185 OS << " }";
186 }
187 void writeCloneArgs(raw_ostream &OS) const {
188 OS << getLowerName();
189 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000190 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
191 OS << "A->get" << getUpperName() << "()";
192 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000193 void writeCtorInitializers(raw_ostream &OS) const {
194 OS << getLowerName() << "(" << getUpperName() << ")";
195 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000196 void writeCtorDefaultInitializers(raw_ostream &OS) const {
197 OS << getLowerName() << "()";
198 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000199 void writeCtorParameters(raw_ostream &OS) const {
200 OS << type << " " << getUpperName();
201 }
202 void writeDeclarations(raw_ostream &OS) const {
203 OS << type << " " << getLowerName() << ";";
204 }
205 void writePCHReadDecls(raw_ostream &OS) const {
206 std::string read = ReadPCHRecord(type);
207 OS << " " << type << " " << getLowerName() << " = " << read << ";\n";
208 }
209 void writePCHReadArgs(raw_ostream &OS) const {
210 OS << getLowerName();
211 }
212 void writePCHWrite(raw_ostream &OS) const {
213 OS << " " << WritePCHRecord(type, "SA->get" +
214 std::string(getUpperName()) + "()");
215 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000216 void writeValue(raw_ostream &OS) const {
217 if (type == "FunctionDecl *") {
Richard Smithb87c4652013-10-31 21:23:20 +0000218 OS << "\" << get" << getUpperName()
219 << "()->getNameInfo().getAsString() << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000220 } else if (type == "IdentifierInfo *") {
221 OS << "\" << get" << getUpperName() << "()->getName() << \"";
Richard Smithb87c4652013-10-31 21:23:20 +0000222 } else if (type == "TypeSourceInfo *") {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000223 OS << "\" << get" << getUpperName() << "().getAsString() << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000224 } else {
225 OS << "\" << get" << getUpperName() << "() << \"";
226 }
227 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000228 void writeDump(raw_ostream &OS) const {
229 if (type == "FunctionDecl *") {
230 OS << " OS << \" \";\n";
231 OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n";
232 } else if (type == "IdentifierInfo *") {
233 OS << " OS << \" \" << SA->get" << getUpperName()
234 << "()->getName();\n";
Richard Smithb87c4652013-10-31 21:23:20 +0000235 } else if (type == "TypeSourceInfo *") {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000236 OS << " OS << \" \" << SA->get" << getUpperName()
237 << "().getAsString();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000238 } else if (type == "bool") {
239 OS << " if (SA->get" << getUpperName() << "()) OS << \" "
240 << getUpperName() << "\";\n";
241 } else if (type == "int" || type == "unsigned") {
242 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
243 } else {
244 llvm_unreachable("Unknown SimpleArgument type!");
245 }
246 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000247 };
248
Aaron Ballman18a78382013-11-21 00:28:23 +0000249 class DefaultSimpleArgument : public SimpleArgument {
250 int64_t Default;
251
252 public:
253 DefaultSimpleArgument(Record &Arg, StringRef Attr,
254 std::string T, int64_t Default)
255 : SimpleArgument(Arg, Attr, T), Default(Default) {}
256
257 void writeAccessors(raw_ostream &OS) const {
258 SimpleArgument::writeAccessors(OS);
259
260 OS << "\n\n static const " << getType() << " Default" << getUpperName()
261 << " = " << Default << ";";
262 }
263 };
264
Peter Collingbournebee583f2011-10-06 13:03:08 +0000265 class StringArgument : public Argument {
266 public:
267 StringArgument(Record &Arg, StringRef Attr)
268 : Argument(Arg, Attr)
269 {}
270
271 void writeAccessors(raw_ostream &OS) const {
272 OS << " llvm::StringRef get" << getUpperName() << "() const {\n";
273 OS << " return llvm::StringRef(" << getLowerName() << ", "
274 << getLowerName() << "Length);\n";
275 OS << " }\n";
276 OS << " unsigned get" << getUpperName() << "Length() const {\n";
277 OS << " return " << getLowerName() << "Length;\n";
278 OS << " }\n";
279 OS << " void set" << getUpperName()
280 << "(ASTContext &C, llvm::StringRef S) {\n";
281 OS << " " << getLowerName() << "Length = S.size();\n";
282 OS << " this->" << getLowerName() << " = new (C, 1) char ["
283 << getLowerName() << "Length];\n";
284 OS << " std::memcpy(this->" << getLowerName() << ", S.data(), "
285 << getLowerName() << "Length);\n";
286 OS << " }";
287 }
288 void writeCloneArgs(raw_ostream &OS) const {
289 OS << "get" << getUpperName() << "()";
290 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000291 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
292 OS << "A->get" << getUpperName() << "()";
293 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000294 void writeCtorBody(raw_ostream &OS) const {
295 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
296 << ".data(), " << getLowerName() << "Length);";
297 }
298 void writeCtorInitializers(raw_ostream &OS) const {
299 OS << getLowerName() << "Length(" << getUpperName() << ".size()),"
300 << getLowerName() << "(new (Ctx, 1) char[" << getLowerName()
301 << "Length])";
302 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000303 void writeCtorDefaultInitializers(raw_ostream &OS) const {
304 OS << getLowerName() << "Length(0)," << getLowerName() << "(0)";
305 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000306 void writeCtorParameters(raw_ostream &OS) const {
307 OS << "llvm::StringRef " << getUpperName();
308 }
309 void writeDeclarations(raw_ostream &OS) const {
310 OS << "unsigned " << getLowerName() << "Length;\n";
311 OS << "char *" << getLowerName() << ";";
312 }
313 void writePCHReadDecls(raw_ostream &OS) const {
314 OS << " std::string " << getLowerName()
315 << "= ReadString(Record, Idx);\n";
316 }
317 void writePCHReadArgs(raw_ostream &OS) const {
318 OS << getLowerName();
319 }
320 void writePCHWrite(raw_ostream &OS) const {
321 OS << " AddString(SA->get" << getUpperName() << "(), Record);\n";
322 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000323 void writeValue(raw_ostream &OS) const {
324 OS << "\\\"\" << get" << getUpperName() << "() << \"\\\"";
325 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000326 void writeDump(raw_ostream &OS) const {
327 OS << " OS << \" \\\"\" << SA->get" << getUpperName()
328 << "() << \"\\\"\";\n";
329 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000330 };
331
332 class AlignedArgument : public Argument {
333 public:
334 AlignedArgument(Record &Arg, StringRef Attr)
335 : Argument(Arg, Attr)
336 {}
337
338 void writeAccessors(raw_ostream &OS) const {
339 OS << " bool is" << getUpperName() << "Dependent() const;\n";
340
341 OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n";
342
343 OS << " bool is" << getUpperName() << "Expr() const {\n";
344 OS << " return is" << getLowerName() << "Expr;\n";
345 OS << " }\n";
346
347 OS << " Expr *get" << getUpperName() << "Expr() const {\n";
348 OS << " assert(is" << getLowerName() << "Expr);\n";
349 OS << " return " << getLowerName() << "Expr;\n";
350 OS << " }\n";
351
352 OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n";
353 OS << " assert(!is" << getLowerName() << "Expr);\n";
354 OS << " return " << getLowerName() << "Type;\n";
355 OS << " }";
356 }
357 void writeAccessorDefinitions(raw_ostream &OS) const {
358 OS << "bool " << getAttrName() << "Attr::is" << getUpperName()
359 << "Dependent() const {\n";
360 OS << " if (is" << getLowerName() << "Expr)\n";
361 OS << " return " << getLowerName() << "Expr && (" << getLowerName()
362 << "Expr->isValueDependent() || " << getLowerName()
363 << "Expr->isTypeDependent());\n";
364 OS << " else\n";
365 OS << " return " << getLowerName()
366 << "Type->getType()->isDependentType();\n";
367 OS << "}\n";
368
369 // FIXME: Do not do the calculation here
370 // FIXME: Handle types correctly
371 // A null pointer means maximum alignment
372 // FIXME: Load the platform-specific maximum alignment, rather than
373 // 16, the x86 max.
374 OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
375 << "(ASTContext &Ctx) const {\n";
376 OS << " assert(!is" << getUpperName() << "Dependent());\n";
377 OS << " if (is" << getLowerName() << "Expr)\n";
378 OS << " return (" << getLowerName() << "Expr ? " << getLowerName()
Richard Smithcaf33902011-10-10 18:28:20 +0000379 << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue() : 16)"
Peter Collingbournebee583f2011-10-06 13:03:08 +0000380 << "* Ctx.getCharWidth();\n";
381 OS << " else\n";
382 OS << " return 0; // FIXME\n";
383 OS << "}\n";
384 }
385 void writeCloneArgs(raw_ostream &OS) const {
386 OS << "is" << getLowerName() << "Expr, is" << getLowerName()
387 << "Expr ? static_cast<void*>(" << getLowerName()
388 << "Expr) : " << getLowerName()
389 << "Type";
390 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000391 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
392 // FIXME: move the definition in Sema::InstantiateAttrs to here.
393 // In the meantime, aligned attributes are cloned.
394 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000395 void writeCtorBody(raw_ostream &OS) const {
396 OS << " if (is" << getLowerName() << "Expr)\n";
397 OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>("
398 << getUpperName() << ");\n";
399 OS << " else\n";
400 OS << " " << getLowerName()
401 << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName()
402 << ");";
403 }
404 void writeCtorInitializers(raw_ostream &OS) const {
405 OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)";
406 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000407 void writeCtorDefaultInitializers(raw_ostream &OS) const {
408 OS << "is" << getLowerName() << "Expr(false)";
409 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000410 void writeCtorParameters(raw_ostream &OS) const {
411 OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName();
412 }
Aaron Ballman36a53502014-01-16 13:03:14 +0000413 void writeImplicitCtorArgs(raw_ostream &OS) const {
414 OS << "Is" << getUpperName() << "Expr, " << getUpperName();
415 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000416 void writeDeclarations(raw_ostream &OS) const {
417 OS << "bool is" << getLowerName() << "Expr;\n";
418 OS << "union {\n";
419 OS << "Expr *" << getLowerName() << "Expr;\n";
420 OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
421 OS << "};";
422 }
423 void writePCHReadArgs(raw_ostream &OS) const {
424 OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr";
425 }
426 void writePCHReadDecls(raw_ostream &OS) const {
427 OS << " bool is" << getLowerName() << "Expr = Record[Idx++];\n";
428 OS << " void *" << getLowerName() << "Ptr;\n";
429 OS << " if (is" << getLowerName() << "Expr)\n";
430 OS << " " << getLowerName() << "Ptr = ReadExpr(F);\n";
431 OS << " else\n";
432 OS << " " << getLowerName()
433 << "Ptr = GetTypeSourceInfo(F, Record, Idx);\n";
434 }
435 void writePCHWrite(raw_ostream &OS) const {
436 OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n";
437 OS << " if (SA->is" << getUpperName() << "Expr())\n";
438 OS << " AddStmt(SA->get" << getUpperName() << "Expr());\n";
439 OS << " else\n";
440 OS << " AddTypeSourceInfo(SA->get" << getUpperName()
441 << "Type(), Record);\n";
442 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000443 void writeValue(raw_ostream &OS) const {
Richard Smith52f04a22012-08-16 02:43:29 +0000444 OS << "\";\n"
445 << " " << getLowerName() << "Expr->printPretty(OS, 0, Policy);\n"
446 << " OS << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000447 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000448 void writeDump(raw_ostream &OS) const {
449 }
450 void writeDumpChildren(raw_ostream &OS) const {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000451 OS << " if (SA->is" << getUpperName() << "Expr()) {\n";
452 OS << " lastChild();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000453 OS << " dumpStmt(SA->get" << getUpperName() << "Expr());\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000454 OS << " } else\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000455 OS << " dumpType(SA->get" << getUpperName()
456 << "Type()->getType());\n";
457 }
Richard Trieude5cc7d2013-01-31 01:44:26 +0000458 void writeHasChildren(raw_ostream &OS) const {
459 OS << "SA->is" << getUpperName() << "Expr()";
460 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000461 };
462
463 class VariadicArgument : public Argument {
464 std::string type;
465
466 public:
467 VariadicArgument(Record &Arg, StringRef Attr, std::string T)
468 : Argument(Arg, Attr), type(T)
469 {}
470
471 std::string getType() const { return type; }
472
473 void writeAccessors(raw_ostream &OS) const {
474 OS << " typedef " << type << "* " << getLowerName() << "_iterator;\n";
475 OS << " " << getLowerName() << "_iterator " << getLowerName()
476 << "_begin() const {\n";
477 OS << " return " << getLowerName() << ";\n";
478 OS << " }\n";
479 OS << " " << getLowerName() << "_iterator " << getLowerName()
480 << "_end() const {\n";
481 OS << " return " << getLowerName() << " + " << getLowerName()
482 << "Size;\n";
483 OS << " }\n";
484 OS << " unsigned " << getLowerName() << "_size() const {\n"
DeLesley Hutchins30398dd2012-01-20 22:50:54 +0000485 << " return " << getLowerName() << "Size;\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000486 OS << " }";
487 }
488 void writeCloneArgs(raw_ostream &OS) const {
489 OS << getLowerName() << ", " << getLowerName() << "Size";
490 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000491 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
492 // This isn't elegant, but we have to go through public methods...
493 OS << "A->" << getLowerName() << "_begin(), "
494 << "A->" << getLowerName() << "_size()";
495 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000496 void writeCtorBody(raw_ostream &OS) const {
497 // FIXME: memcpy is not safe on non-trivial types.
498 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
499 << ", " << getLowerName() << "Size * sizeof(" << getType() << "));\n";
500 }
501 void writeCtorInitializers(raw_ostream &OS) const {
502 OS << getLowerName() << "Size(" << getUpperName() << "Size), "
503 << getLowerName() << "(new (Ctx, 16) " << getType() << "["
504 << getLowerName() << "Size])";
505 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000506 void writeCtorDefaultInitializers(raw_ostream &OS) const {
507 OS << getLowerName() << "Size(0), " << getLowerName() << "(0)";
508 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000509 void writeCtorParameters(raw_ostream &OS) const {
510 OS << getType() << " *" << getUpperName() << ", unsigned "
511 << getUpperName() << "Size";
512 }
Aaron Ballman36a53502014-01-16 13:03:14 +0000513 void writeImplicitCtorArgs(raw_ostream &OS) const {
514 OS << getUpperName() << ", " << getUpperName() << "Size";
515 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000516 void writeDeclarations(raw_ostream &OS) const {
517 OS << " unsigned " << getLowerName() << "Size;\n";
518 OS << " " << getType() << " *" << getLowerName() << ";";
519 }
520 void writePCHReadDecls(raw_ostream &OS) const {
521 OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000522 OS << " SmallVector<" << type << ", 4> " << getLowerName()
Peter Collingbournebee583f2011-10-06 13:03:08 +0000523 << ";\n";
524 OS << " " << getLowerName() << ".reserve(" << getLowerName()
525 << "Size);\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000526 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000527
528 std::string read = ReadPCHRecord(type);
529 OS << " " << getLowerName() << ".push_back(" << read << ");\n";
530 }
531 void writePCHReadArgs(raw_ostream &OS) const {
532 OS << getLowerName() << ".data(), " << getLowerName() << "Size";
533 }
534 void writePCHWrite(raw_ostream &OS) const{
535 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
536 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
537 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
538 << getLowerName() << "_end(); i != e; ++i)\n";
539 OS << " " << WritePCHRecord(type, "(*i)");
540 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000541 void writeValue(raw_ostream &OS) const {
542 OS << "\";\n";
543 OS << " bool isFirst = true;\n"
544 << " for (" << getAttrName() << "Attr::" << getLowerName()
545 << "_iterator i = " << getLowerName() << "_begin(), e = "
546 << getLowerName() << "_end(); i != e; ++i) {\n"
547 << " if (isFirst) isFirst = false;\n"
548 << " else OS << \", \";\n"
549 << " OS << *i;\n"
550 << " }\n";
551 OS << " OS << \"";
552 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000553 void writeDump(raw_ostream &OS) const {
554 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
555 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
556 << getLowerName() << "_end(); I != E; ++I)\n";
557 OS << " OS << \" \" << *I;\n";
558 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000559 };
560
561 class EnumArgument : public Argument {
562 std::string type;
Aaron Ballman0e468c02014-01-05 21:08:29 +0000563 std::vector<std::string> values, enums, uniques;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000564 public:
565 EnumArgument(Record &Arg, StringRef Attr)
566 : Argument(Arg, Attr), type(Arg.getValueAsString("Type")),
Aaron Ballman0e468c02014-01-05 21:08:29 +0000567 values(Arg.getValueAsListOfStrings("Values")),
568 enums(Arg.getValueAsListOfStrings("Enums")),
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000569 uniques(enums)
570 {
571 // Calculate the various enum values
572 std::sort(uniques.begin(), uniques.end());
573 uniques.erase(std::unique(uniques.begin(), uniques.end()), uniques.end());
574 // FIXME: Emit a proper error
575 assert(!uniques.empty());
576 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000577
Aaron Ballman682ee422013-09-11 19:47:58 +0000578 bool isEnumArg() const { return true; }
579
Peter Collingbournebee583f2011-10-06 13:03:08 +0000580 void writeAccessors(raw_ostream &OS) const {
581 OS << " " << type << " get" << getUpperName() << "() const {\n";
582 OS << " return " << getLowerName() << ";\n";
583 OS << " }";
584 }
585 void writeCloneArgs(raw_ostream &OS) const {
586 OS << getLowerName();
587 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000588 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
589 OS << "A->get" << getUpperName() << "()";
590 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000591 void writeCtorInitializers(raw_ostream &OS) const {
592 OS << getLowerName() << "(" << getUpperName() << ")";
593 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000594 void writeCtorDefaultInitializers(raw_ostream &OS) const {
595 OS << getLowerName() << "(" << type << "(0))";
596 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000597 void writeCtorParameters(raw_ostream &OS) const {
598 OS << type << " " << getUpperName();
599 }
600 void writeDeclarations(raw_ostream &OS) const {
Aaron Ballman0e468c02014-01-05 21:08:29 +0000601 std::vector<std::string>::const_iterator i = uniques.begin(),
602 e = uniques.end();
Peter Collingbournebee583f2011-10-06 13:03:08 +0000603 // The last one needs to not have a comma.
604 --e;
605
606 OS << "public:\n";
607 OS << " enum " << type << " {\n";
608 for (; i != e; ++i)
609 OS << " " << *i << ",\n";
610 OS << " " << *e << "\n";
611 OS << " };\n";
612 OS << "private:\n";
613 OS << " " << type << " " << getLowerName() << ";";
614 }
615 void writePCHReadDecls(raw_ostream &OS) const {
616 OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName()
617 << "(static_cast<" << getAttrName() << "Attr::" << type
618 << ">(Record[Idx++]));\n";
619 }
620 void writePCHReadArgs(raw_ostream &OS) const {
621 OS << getLowerName();
622 }
623 void writePCHWrite(raw_ostream &OS) const {
624 OS << "Record.push_back(SA->get" << getUpperName() << "());\n";
625 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000626 void writeValue(raw_ostream &OS) const {
627 OS << "\" << get" << getUpperName() << "() << \"";
628 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000629 void writeDump(raw_ostream &OS) const {
630 OS << " switch(SA->get" << getUpperName() << "()) {\n";
Aaron Ballman0e468c02014-01-05 21:08:29 +0000631 for (std::vector<std::string>::const_iterator I = uniques.begin(),
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000632 E = uniques.end(); I != E; ++I) {
633 OS << " case " << getAttrName() << "Attr::" << *I << ":\n";
634 OS << " OS << \" " << *I << "\";\n";
635 OS << " break;\n";
636 }
637 OS << " }\n";
638 }
Aaron Ballman682ee422013-09-11 19:47:58 +0000639
640 void writeConversion(raw_ostream &OS) const {
641 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
642 OS << type << " &Out) {\n";
643 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
644 OS << type << "> >(Val)\n";
645 for (size_t I = 0; I < enums.size(); ++I) {
646 OS << " .Case(\"" << values[I] << "\", ";
647 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
648 }
649 OS << " .Default(Optional<" << type << ">());\n";
650 OS << " if (R) {\n";
651 OS << " Out = *R;\n return true;\n }\n";
652 OS << " return false;\n";
653 OS << " }\n";
654 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000655 };
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000656
657 class VariadicEnumArgument: public VariadicArgument {
658 std::string type, QualifiedTypeName;
Aaron Ballman0e468c02014-01-05 21:08:29 +0000659 std::vector<std::string> values, enums, uniques;
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000660 public:
661 VariadicEnumArgument(Record &Arg, StringRef Attr)
662 : VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")),
663 type(Arg.getValueAsString("Type")),
Aaron Ballman0e468c02014-01-05 21:08:29 +0000664 values(Arg.getValueAsListOfStrings("Values")),
665 enums(Arg.getValueAsListOfStrings("Enums")),
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000666 uniques(enums)
667 {
668 // Calculate the various enum values
669 std::sort(uniques.begin(), uniques.end());
670 uniques.erase(std::unique(uniques.begin(), uniques.end()), uniques.end());
671
672 QualifiedTypeName = getAttrName().str() + "Attr::" + type;
673
674 // FIXME: Emit a proper error
675 assert(!uniques.empty());
676 }
677
678 bool isVariadicEnumArg() const { return true; }
679
680 void writeDeclarations(raw_ostream &OS) const {
Aaron Ballman0e468c02014-01-05 21:08:29 +0000681 std::vector<std::string>::const_iterator i = uniques.begin(),
682 e = uniques.end();
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000683 // The last one needs to not have a comma.
684 --e;
685
686 OS << "public:\n";
687 OS << " enum " << type << " {\n";
688 for (; i != e; ++i)
689 OS << " " << *i << ",\n";
690 OS << " " << *e << "\n";
691 OS << " };\n";
692 OS << "private:\n";
693
694 VariadicArgument::writeDeclarations(OS);
695 }
696 void writeDump(raw_ostream &OS) const {
697 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
698 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
699 << getLowerName() << "_end(); I != E; ++I) {\n";
700 OS << " switch(*I) {\n";
Aaron Ballman0e468c02014-01-05 21:08:29 +0000701 for (std::vector<std::string>::const_iterator UI = uniques.begin(),
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000702 UE = uniques.end(); UI != UE; ++UI) {
703 OS << " case " << getAttrName() << "Attr::" << *UI << ":\n";
704 OS << " OS << \" " << *UI << "\";\n";
705 OS << " break;\n";
706 }
707 OS << " }\n";
708 OS << " }\n";
709 }
710 void writePCHReadDecls(raw_ostream &OS) const {
711 OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
712 OS << " SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName()
713 << ";\n";
714 OS << " " << getLowerName() << ".reserve(" << getLowerName()
715 << "Size);\n";
716 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
717 OS << " " << getLowerName() << ".push_back(" << "static_cast<"
718 << QualifiedTypeName << ">(Record[Idx++]));\n";
719 }
720 void writePCHWrite(raw_ostream &OS) const{
721 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
722 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
723 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
724 << getLowerName() << "_end(); i != e; ++i)\n";
725 OS << " " << WritePCHRecord(QualifiedTypeName, "(*i)");
726 }
727 void writeConversion(raw_ostream &OS) const {
728 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
729 OS << type << " &Out) {\n";
730 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
731 OS << type << "> >(Val)\n";
732 for (size_t I = 0; I < enums.size(); ++I) {
733 OS << " .Case(\"" << values[I] << "\", ";
734 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
735 }
736 OS << " .Default(Optional<" << type << ">());\n";
737 OS << " if (R) {\n";
738 OS << " Out = *R;\n return true;\n }\n";
739 OS << " return false;\n";
740 OS << " }\n";
741 }
742 };
Peter Collingbournebee583f2011-10-06 13:03:08 +0000743
744 class VersionArgument : public Argument {
745 public:
746 VersionArgument(Record &Arg, StringRef Attr)
747 : Argument(Arg, Attr)
748 {}
749
750 void writeAccessors(raw_ostream &OS) const {
751 OS << " VersionTuple get" << getUpperName() << "() const {\n";
752 OS << " return " << getLowerName() << ";\n";
753 OS << " }\n";
754 OS << " void set" << getUpperName()
755 << "(ASTContext &C, VersionTuple V) {\n";
756 OS << " " << getLowerName() << " = V;\n";
757 OS << " }";
758 }
759 void writeCloneArgs(raw_ostream &OS) const {
760 OS << "get" << getUpperName() << "()";
761 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000762 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
763 OS << "A->get" << getUpperName() << "()";
764 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000765 void writeCtorBody(raw_ostream &OS) const {
766 }
767 void writeCtorInitializers(raw_ostream &OS) const {
768 OS << getLowerName() << "(" << getUpperName() << ")";
769 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000770 void writeCtorDefaultInitializers(raw_ostream &OS) const {
771 OS << getLowerName() << "()";
772 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000773 void writeCtorParameters(raw_ostream &OS) const {
774 OS << "VersionTuple " << getUpperName();
775 }
776 void writeDeclarations(raw_ostream &OS) const {
777 OS << "VersionTuple " << getLowerName() << ";\n";
778 }
779 void writePCHReadDecls(raw_ostream &OS) const {
780 OS << " VersionTuple " << getLowerName()
781 << "= ReadVersionTuple(Record, Idx);\n";
782 }
783 void writePCHReadArgs(raw_ostream &OS) const {
784 OS << getLowerName();
785 }
786 void writePCHWrite(raw_ostream &OS) const {
787 OS << " AddVersionTuple(SA->get" << getUpperName() << "(), Record);\n";
788 }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000789 void writeValue(raw_ostream &OS) const {
790 OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";
791 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000792 void writeDump(raw_ostream &OS) const {
793 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
794 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000795 };
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000796
797 class ExprArgument : public SimpleArgument {
798 public:
799 ExprArgument(Record &Arg, StringRef Attr)
800 : SimpleArgument(Arg, Attr, "Expr *")
801 {}
802
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +0000803 virtual void writeASTVisitorTraversal(raw_ostream &OS) const {
804 OS << " if (!"
805 << "getDerived().TraverseStmt(A->get" << getUpperName() << "()))\n";
806 OS << " return false;\n";
807 }
808
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000809 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
810 OS << "tempInst" << getUpperName();
811 }
812
813 void writeTemplateInstantiation(raw_ostream &OS) const {
814 OS << " " << getType() << " tempInst" << getUpperName() << ";\n";
815 OS << " {\n";
816 OS << " EnterExpressionEvaluationContext "
817 << "Unevaluated(S, Sema::Unevaluated);\n";
818 OS << " ExprResult " << "Result = S.SubstExpr("
819 << "A->get" << getUpperName() << "(), TemplateArgs);\n";
820 OS << " tempInst" << getUpperName() << " = "
821 << "Result.takeAs<Expr>();\n";
822 OS << " }\n";
823 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000824
825 void writeDump(raw_ostream &OS) const {
826 }
827
828 void writeDumpChildren(raw_ostream &OS) const {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000829 OS << " lastChild();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000830 OS << " dumpStmt(SA->get" << getUpperName() << "());\n";
831 }
Richard Trieude5cc7d2013-01-31 01:44:26 +0000832 void writeHasChildren(raw_ostream &OS) const { OS << "true"; }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000833 };
834
835 class VariadicExprArgument : public VariadicArgument {
836 public:
837 VariadicExprArgument(Record &Arg, StringRef Attr)
838 : VariadicArgument(Arg, Attr, "Expr *")
839 {}
840
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +0000841 virtual void writeASTVisitorTraversal(raw_ostream &OS) const {
842 OS << " {\n";
843 OS << " " << getType() << " *I = A->" << getLowerName()
844 << "_begin();\n";
845 OS << " " << getType() << " *E = A->" << getLowerName()
846 << "_end();\n";
847 OS << " for (; I != E; ++I) {\n";
848 OS << " if (!getDerived().TraverseStmt(*I))\n";
849 OS << " return false;\n";
850 OS << " }\n";
851 OS << " }\n";
852 }
853
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000854 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
855 OS << "tempInst" << getUpperName() << ", "
856 << "A->" << getLowerName() << "_size()";
857 }
858
859 void writeTemplateInstantiation(raw_ostream &OS) const {
860 OS << " " << getType() << " *tempInst" << getUpperName()
861 << " = new (C, 16) " << getType()
862 << "[A->" << getLowerName() << "_size()];\n";
863 OS << " {\n";
864 OS << " EnterExpressionEvaluationContext "
865 << "Unevaluated(S, Sema::Unevaluated);\n";
866 OS << " " << getType() << " *TI = tempInst" << getUpperName()
867 << ";\n";
868 OS << " " << getType() << " *I = A->" << getLowerName()
869 << "_begin();\n";
870 OS << " " << getType() << " *E = A->" << getLowerName()
871 << "_end();\n";
872 OS << " for (; I != E; ++I, ++TI) {\n";
873 OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n";
874 OS << " *TI = Result.takeAs<Expr>();\n";
875 OS << " }\n";
876 OS << " }\n";
877 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000878
879 void writeDump(raw_ostream &OS) const {
880 }
881
882 void writeDumpChildren(raw_ostream &OS) const {
883 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
884 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
Richard Trieude5cc7d2013-01-31 01:44:26 +0000885 << getLowerName() << "_end(); I != E; ++I) {\n";
886 OS << " if (I + 1 == E)\n";
887 OS << " lastChild();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000888 OS << " dumpStmt(*I);\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000889 OS << " }\n";
890 }
891
892 void writeHasChildren(raw_ostream &OS) const {
893 OS << "SA->" << getLowerName() << "_begin() != "
894 << "SA->" << getLowerName() << "_end()";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000895 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000896 };
Richard Smithb87c4652013-10-31 21:23:20 +0000897
898 class TypeArgument : public SimpleArgument {
899 public:
900 TypeArgument(Record &Arg, StringRef Attr)
901 : SimpleArgument(Arg, Attr, "TypeSourceInfo *")
902 {}
903
904 void writeAccessors(raw_ostream &OS) const {
905 OS << " QualType get" << getUpperName() << "() const {\n";
906 OS << " return " << getLowerName() << "->getType();\n";
907 OS << " }";
908 OS << " " << getType() << " get" << getUpperName() << "Loc() const {\n";
909 OS << " return " << getLowerName() << ";\n";
910 OS << " }";
911 }
912 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
913 OS << "A->get" << getUpperName() << "Loc()";
914 }
915 void writePCHWrite(raw_ostream &OS) const {
916 OS << " " << WritePCHRecord(
917 getType(), "SA->get" + std::string(getUpperName()) + "Loc()");
918 }
919 };
Peter Collingbournebee583f2011-10-06 13:03:08 +0000920}
921
Aaron Ballmanaa47d242013-12-19 19:39:25 +0000922static Argument *createArgument(Record &Arg, StringRef Attr,
923 Record *Search = 0) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000924 if (!Search)
925 Search = &Arg;
926
Aaron Ballmanaa47d242013-12-19 19:39:25 +0000927 Argument *Ptr = 0;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000928 llvm::StringRef ArgName = Search->getName();
929
Aaron Ballmanaa47d242013-12-19 19:39:25 +0000930 if (ArgName == "AlignedArgument") Ptr = new AlignedArgument(Arg, Attr);
931 else if (ArgName == "EnumArgument") Ptr = new EnumArgument(Arg, Attr);
932 else if (ArgName == "ExprArgument") Ptr = new ExprArgument(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000933 else if (ArgName == "FunctionArgument")
Aaron Ballmanaa47d242013-12-19 19:39:25 +0000934 Ptr = new SimpleArgument(Arg, Attr, "FunctionDecl *");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000935 else if (ArgName == "IdentifierArgument")
Aaron Ballmanaa47d242013-12-19 19:39:25 +0000936 Ptr = new SimpleArgument(Arg, Attr, "IdentifierInfo *");
937 else if (ArgName == "BoolArgument") Ptr = new SimpleArgument(Arg, Attr,
938 "bool");
Aaron Ballman18a78382013-11-21 00:28:23 +0000939 else if (ArgName == "DefaultIntArgument")
Aaron Ballmanaa47d242013-12-19 19:39:25 +0000940 Ptr = new DefaultSimpleArgument(Arg, Attr, "int",
941 Arg.getValueAsInt("Default"));
942 else if (ArgName == "IntArgument") Ptr = new SimpleArgument(Arg, Attr, "int");
943 else if (ArgName == "StringArgument") Ptr = new StringArgument(Arg, Attr);
944 else if (ArgName == "TypeArgument") Ptr = new TypeArgument(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000945 else if (ArgName == "UnsignedArgument")
Aaron Ballmanaa47d242013-12-19 19:39:25 +0000946 Ptr = new SimpleArgument(Arg, Attr, "unsigned");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000947 else if (ArgName == "VariadicUnsignedArgument")
Aaron Ballmanaa47d242013-12-19 19:39:25 +0000948 Ptr = new VariadicArgument(Arg, Attr, "unsigned");
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000949 else if (ArgName == "VariadicEnumArgument")
Aaron Ballmanaa47d242013-12-19 19:39:25 +0000950 Ptr = new VariadicEnumArgument(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000951 else if (ArgName == "VariadicExprArgument")
Aaron Ballmanaa47d242013-12-19 19:39:25 +0000952 Ptr = new VariadicExprArgument(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000953 else if (ArgName == "VersionArgument")
Aaron Ballmanaa47d242013-12-19 19:39:25 +0000954 Ptr = new VersionArgument(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000955
956 if (!Ptr) {
Aaron Ballman18a78382013-11-21 00:28:23 +0000957 // Search in reverse order so that the most-derived type is handled first.
Peter Collingbournebee583f2011-10-06 13:03:08 +0000958 std::vector<Record*> Bases = Search->getSuperClasses();
Aaron Ballman18a78382013-11-21 00:28:23 +0000959 for (std::vector<Record*>::reverse_iterator i = Bases.rbegin(),
960 e = Bases.rend(); i != e; ++i) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000961 Ptr = createArgument(Arg, Attr, *i);
962 if (Ptr)
963 break;
964 }
965 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000966
967 if (Ptr && Arg.getValueAsBit("Optional"))
968 Ptr->setOptional(true);
969
Peter Collingbournebee583f2011-10-06 13:03:08 +0000970 return Ptr;
971}
972
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000973static void writeAvailabilityValue(raw_ostream &OS) {
974 OS << "\" << getPlatform()->getName();\n"
975 << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n"
976 << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n"
977 << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n"
978 << " if (getUnavailable()) OS << \", unavailable\";\n"
979 << " OS << \"";
980}
981
Aaron Ballman3e424b52013-12-26 18:30:57 +0000982static void writeGetSpellingFunction(Record &R, raw_ostream &OS) {
983 std::vector<Record *> Spellings = R.getValueAsListOfDefs("Spellings");
984
985 OS << "const char *" << R.getName() << "Attr::getSpelling() const {\n";
986 if (Spellings.empty()) {
987 OS << " return \"(No spelling)\";\n}\n\n";
988 return;
989 }
990
991 OS << " switch (SpellingListIndex) {\n"
992 " default:\n"
993 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
994 " return \"(No spelling)\";\n";
995
996 for (unsigned I = 0; I < Spellings.size(); ++I)
997 OS << " case " << I << ":\n"
998 " return \"" << Spellings[I]->getValueAsString("Name") << "\";\n";
999 // End of the switch statement.
1000 OS << " }\n";
1001 // End of the getSpelling function.
1002 OS << "}\n\n";
1003}
1004
Michael Han99315932013-01-24 16:46:58 +00001005static void writePrettyPrintFunction(Record &R, std::vector<Argument*> &Args,
1006 raw_ostream &OS) {
1007 std::vector<Record*> Spellings = R.getValueAsListOfDefs("Spellings");
1008
1009 OS << "void " << R.getName() << "Attr::printPretty("
1010 << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n";
1011
1012 if (Spellings.size() == 0) {
1013 OS << "}\n\n";
1014 return;
1015 }
1016
1017 OS <<
1018 " switch (SpellingListIndex) {\n"
1019 " default:\n"
1020 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1021 " break;\n";
1022
1023 for (unsigned I = 0; I < Spellings.size(); ++ I) {
1024 llvm::SmallString<16> Prefix;
1025 llvm::SmallString<8> Suffix;
1026 // The actual spelling of the name and namespace (if applicable)
1027 // of an attribute without considering prefix and suffix.
1028 llvm::SmallString<64> Spelling;
1029 std::string Name = Spellings[I]->getValueAsString("Name");
1030 std::string Variety = Spellings[I]->getValueAsString("Variety");
1031
1032 if (Variety == "GNU") {
1033 Prefix = " __attribute__((";
1034 Suffix = "))";
1035 } else if (Variety == "CXX11") {
1036 Prefix = " [[";
1037 Suffix = "]]";
1038 std::string Namespace = Spellings[I]->getValueAsString("Namespace");
1039 if (Namespace != "") {
1040 Spelling += Namespace;
1041 Spelling += "::";
1042 }
1043 } else if (Variety == "Declspec") {
1044 Prefix = " __declspec(";
1045 Suffix = ")";
Richard Smith0cdcc982013-01-29 01:24:26 +00001046 } else if (Variety == "Keyword") {
1047 Prefix = " ";
1048 Suffix = "";
Michael Han99315932013-01-24 16:46:58 +00001049 } else {
Richard Smith0cdcc982013-01-29 01:24:26 +00001050 llvm_unreachable("Unknown attribute syntax variety!");
Michael Han99315932013-01-24 16:46:58 +00001051 }
1052
1053 Spelling += Name;
1054
1055 OS <<
1056 " case " << I << " : {\n"
1057 " OS << \"" + Prefix.str() + Spelling.str();
1058
1059 if (Args.size()) OS << "(";
1060 if (Spelling == "availability") {
1061 writeAvailabilityValue(OS);
1062 } else {
1063 for (std::vector<Argument*>::const_iterator I = Args.begin(),
1064 E = Args.end(); I != E; ++ I) {
1065 if (I != Args.begin()) OS << ", ";
1066 (*I)->writeValue(OS);
1067 }
1068 }
1069
1070 if (Args.size()) OS << ")";
1071 OS << Suffix.str() + "\";\n";
1072
1073 OS <<
1074 " break;\n"
1075 " }\n";
1076 }
1077
1078 // End of the switch statement.
1079 OS << "}\n";
1080 // End of the print function.
1081 OS << "}\n\n";
1082}
1083
Michael Hanaf02bbe2013-02-01 01:19:17 +00001084/// \brief Return the index of a spelling in a spelling list.
1085static unsigned getSpellingListIndex(const std::vector<Record*> &SpellingList,
1086 const Record &Spelling) {
1087 assert(SpellingList.size() && "Spelling list is empty!");
1088
1089 for (unsigned Index = 0; Index < SpellingList.size(); ++Index) {
1090 Record *S = SpellingList[Index];
1091 if (S->getValueAsString("Variety") != Spelling.getValueAsString("Variety"))
1092 continue;
1093 if (S->getValueAsString("Variety") == "CXX11" &&
1094 S->getValueAsString("Namespace") !=
1095 Spelling.getValueAsString("Namespace"))
1096 continue;
1097 if (S->getValueAsString("Name") != Spelling.getValueAsString("Name"))
1098 continue;
1099
1100 return Index;
1101 }
1102
1103 llvm_unreachable("Unknown spelling!");
1104}
1105
1106static void writeAttrAccessorDefinition(Record &R, raw_ostream &OS) {
1107 std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors");
1108 for (std::vector<Record*>::const_iterator I = Accessors.begin(),
1109 E = Accessors.end(); I != E; ++I) {
1110 Record *Accessor = *I;
1111 std::string Name = Accessor->getValueAsString("Name");
1112 std::vector<Record*> Spellings = Accessor->getValueAsListOfDefs(
1113 "Spellings");
1114 std::vector<Record*> SpellingList = R.getValueAsListOfDefs("Spellings");
1115 assert(SpellingList.size() &&
1116 "Attribute with empty spelling list can't have accessors!");
1117
1118 OS << " bool " << Name << "() const { return SpellingListIndex == ";
1119 for (unsigned Index = 0; Index < Spellings.size(); ++Index) {
1120 OS << getSpellingListIndex(SpellingList, *Spellings[Index]);
1121 if (Index != Spellings.size() -1)
1122 OS << " ||\n SpellingListIndex == ";
1123 else
1124 OS << "; }\n";
1125 }
1126 }
1127}
1128
Aaron Ballman36a53502014-01-16 13:03:14 +00001129static bool SpellingNamesAreCommon(const std::vector<Record *>& Spellings) {
1130 assert(!Spellings.empty() && "An empty list of spellings was provided");
1131 std::string FirstName = NormalizeNameForSpellingComparison(
1132 Spellings.front()->getValueAsString("Name"));
1133 for (std::vector<Record *>::const_iterator I = llvm::next(Spellings.begin()),
1134 E = Spellings.end(); I != E; ++I) {
1135 std::string Name = NormalizeNameForSpellingComparison(
1136 (*I)->getValueAsString("Name"));
1137 if (Name != FirstName)
1138 return false;
1139 }
1140 return true;
1141}
1142
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001143namespace clang {
1144
1145// Emits the class definitions for attributes.
1146void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001147 emitSourceFileHeader("Attribute classes' definitions", OS);
1148
Peter Collingbournebee583f2011-10-06 13:03:08 +00001149 OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
1150 OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
1151
1152 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1153
1154 for (std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end();
1155 i != e; ++i) {
1156 Record &R = **i;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001157
1158 if (!R.getValueAsBit("ASTNode"))
1159 continue;
1160
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001161 const std::vector<Record *> Supers = R.getSuperClasses();
1162 assert(!Supers.empty() && "Forgot to specify a superclass for the attr");
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001163 std::string SuperName;
1164 for (std::vector<Record *>::const_reverse_iterator I = Supers.rbegin(),
1165 E = Supers.rend(); I != E; ++I) {
1166 const Record &R = **I;
Aaron Ballman080cad72013-07-31 02:20:22 +00001167 if (R.getName() != "TargetSpecificAttr" && SuperName.empty())
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001168 SuperName = R.getName();
1169 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001170
1171 OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
1172
1173 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1174 std::vector<Argument*> Args;
1175 std::vector<Argument*>::iterator ai, ae;
1176 Args.reserve(ArgRecords.size());
1177
1178 for (std::vector<Record*>::iterator ri = ArgRecords.begin(),
1179 re = ArgRecords.end();
1180 ri != re; ++ri) {
1181 Record &ArgRecord = **ri;
Aaron Ballmanaa47d242013-12-19 19:39:25 +00001182 Argument *Arg = createArgument(ArgRecord, R.getName());
Peter Collingbournebee583f2011-10-06 13:03:08 +00001183 assert(Arg);
1184 Args.push_back(Arg);
1185
1186 Arg->writeDeclarations(OS);
1187 OS << "\n\n";
1188 }
1189
1190 ae = Args.end();
1191
Aaron Ballman36a53502014-01-16 13:03:14 +00001192 OS << "\npublic:\n";
1193
1194 std::vector<Record *> Spellings = R.getValueAsListOfDefs("Spellings");
1195
1196 // If there are zero or one spellings, all spelling-related functionality
1197 // can be elided. If all of the spellings share the same name, the spelling
1198 // functionality can also be elided.
1199 bool ElideSpelling = (Spellings.size() <= 1) ||
1200 SpellingNamesAreCommon(Spellings);
1201
1202 if (!ElideSpelling) {
Aaron Ballman07c3d532014-01-16 19:44:01 +00001203 // The enumerants are automatically generated based on the variety,
1204 // namespace (if present) and name for each attribute spelling. However,
1205 // care is taken to avoid trampling on the reserved namespace due to
1206 // underscores.
Aaron Ballman36a53502014-01-16 13:03:14 +00001207 OS << " enum Spelling {\n";
Aaron Ballman07c3d532014-01-16 19:44:01 +00001208 std::set<std::string> Uniques;
Aaron Ballman36a53502014-01-16 13:03:14 +00001209 for (std::vector<Record *>::const_iterator I = Spellings.begin(),
1210 E = Spellings.end(); I != E; ++I) {
1211 if (I != Spellings.begin())
1212 OS << ",\n";
1213 const Record &S = **I;
1214 std::string Variety = S.getValueAsString("Variety");
1215 std::string Spelling = S.getValueAsString("Name");
1216 std::string Namespace = "";
Aaron Ballman07c3d532014-01-16 19:44:01 +00001217 std::string EnumName = "";
Aaron Ballman36a53502014-01-16 13:03:14 +00001218
1219 if (Variety == "CXX11")
1220 Namespace = S.getValueAsString("Namespace");
1221
Aaron Ballman07c3d532014-01-16 19:44:01 +00001222 EnumName += (Variety + "_");
Aaron Ballman36a53502014-01-16 13:03:14 +00001223 if (!Namespace.empty())
Aaron Ballman07c3d532014-01-16 19:44:01 +00001224 EnumName += (NormalizeNameForSpellingComparison(Namespace).str() +
1225 "_");
1226 EnumName += NormalizeNameForSpellingComparison(Spelling);
1227
1228 // Since we have been stripping underscores to avoid trampling on the
Alp Toker96cf7582014-01-18 21:49:37 +00001229 // reserved namespace, we may have inadvertently created duplicate
Aaron Ballman07c3d532014-01-16 19:44:01 +00001230 // enumerant names. Unique the name if required.
1231 while (Uniques.find(EnumName) != Uniques.end())
1232 EnumName += "_alternate";
1233 Uniques.insert(EnumName);
1234
1235 OS << " " << EnumName;
Aaron Ballman36a53502014-01-16 13:03:14 +00001236 }
1237 OS << "\n };\n\n";
1238 }
1239
1240 OS << " static " << R.getName() << "Attr *CreateImplicit(";
1241 OS << "ASTContext &Ctx";
1242 if (!ElideSpelling)
1243 OS << ", Spelling S";
1244 for (ai = Args.begin(); ai != ae; ++ai) {
1245 OS << ", ";
1246 (*ai)->writeCtorParameters(OS);
1247 }
1248 OS << ", SourceRange Loc = SourceRange()";
1249 OS << ") {\n";
1250 OS << " " << R.getName() << "Attr *A = new (Ctx) " << R.getName();
1251 OS << "Attr(Loc, Ctx, ";
1252 for (ai = Args.begin(); ai != ae; ++ai) {
1253 (*ai)->writeImplicitCtorArgs(OS);
1254 OS << ", ";
1255 }
1256 OS << (ElideSpelling ? "0" : "S") << ");\n";
1257 OS << " A->setImplicit(true);\n";
1258 OS << " return A;\n }\n\n";
1259
Peter Collingbournebee583f2011-10-06 13:03:08 +00001260 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
1261
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001262 bool HasOpt = false;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001263 for (ai = Args.begin(); ai != ae; ++ai) {
1264 OS << " , ";
1265 (*ai)->writeCtorParameters(OS);
1266 OS << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001267 if ((*ai)->isOptional())
1268 HasOpt = true;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001269 }
Michael Han99315932013-01-24 16:46:58 +00001270
1271 OS << " , ";
Aaron Ballman36a53502014-01-16 13:03:14 +00001272 OS << "unsigned SI\n";
Michael Han99315932013-01-24 16:46:58 +00001273
Peter Collingbournebee583f2011-10-06 13:03:08 +00001274 OS << " )\n";
Michael Han99315932013-01-24 16:46:58 +00001275 OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI)\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001276
1277 for (ai = Args.begin(); ai != ae; ++ai) {
1278 OS << " , ";
1279 (*ai)->writeCtorInitializers(OS);
1280 OS << "\n";
1281 }
1282
1283 OS << " {\n";
1284
1285 for (ai = Args.begin(); ai != ae; ++ai) {
1286 (*ai)->writeCtorBody(OS);
1287 OS << "\n";
1288 }
1289 OS << " }\n\n";
1290
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001291 // If there are optional arguments, write out a constructor that elides the
1292 // optional arguments as well.
1293 if (HasOpt) {
1294 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
1295 for (ai = Args.begin(); ai != ae; ++ai) {
1296 if (!(*ai)->isOptional()) {
1297 OS << " , ";
1298 (*ai)->writeCtorParameters(OS);
1299 OS << "\n";
1300 }
1301 }
1302
1303 OS << " , ";
Aaron Ballman36a53502014-01-16 13:03:14 +00001304 OS << "unsigned SI\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001305
1306 OS << " )\n";
1307 OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI)\n";
1308
1309 for (ai = Args.begin(); ai != ae; ++ai) {
1310 OS << " , ";
1311 (*ai)->writeCtorDefaultInitializers(OS);
1312 OS << "\n";
1313 }
1314
1315 OS << " {\n";
1316
1317 for (ai = Args.begin(); ai != ae; ++ai) {
1318 if (!(*ai)->isOptional()) {
1319 (*ai)->writeCtorBody(OS);
1320 OS << "\n";
1321 }
1322 }
1323 OS << " }\n\n";
1324 }
1325
Peter Collingbournebee583f2011-10-06 13:03:08 +00001326 OS << " virtual " << R.getName() << "Attr *clone (ASTContext &C) const;\n";
Michael Han38d96ab2013-01-27 00:06:24 +00001327 OS << " virtual void printPretty(raw_ostream &OS,\n"
Richard Smith52f04a22012-08-16 02:43:29 +00001328 << " const PrintingPolicy &Policy) const;\n";
Aaron Ballman3e424b52013-12-26 18:30:57 +00001329 OS << " virtual const char *getSpelling() const;\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001330
Michael Hanaf02bbe2013-02-01 01:19:17 +00001331 writeAttrAccessorDefinition(R, OS);
1332
Peter Collingbournebee583f2011-10-06 13:03:08 +00001333 for (ai = Args.begin(); ai != ae; ++ai) {
1334 (*ai)->writeAccessors(OS);
1335 OS << "\n\n";
Aaron Ballman682ee422013-09-11 19:47:58 +00001336
1337 if ((*ai)->isEnumArg()) {
1338 EnumArgument *EA = (EnumArgument *)*ai;
1339 EA->writeConversion(OS);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001340 } else if ((*ai)->isVariadicEnumArg()) {
1341 VariadicEnumArgument *VEA = (VariadicEnumArgument *)*ai;
1342 VEA->writeConversion(OS);
Aaron Ballman682ee422013-09-11 19:47:58 +00001343 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001344 }
1345
Jakob Stoklund Olesen6f2288b62012-01-13 04:57:47 +00001346 OS << R.getValueAsString("AdditionalMembers");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001347 OS << "\n\n";
1348
1349 OS << " static bool classof(const Attr *A) { return A->getKind() == "
1350 << "attr::" << R.getName() << "; }\n";
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001351
1352 bool LateParsed = R.getValueAsBit("LateParsed");
1353 OS << " virtual bool isLateParsed() const { return "
1354 << LateParsed << "; }\n";
1355
Aaron Ballmanb9023ed2014-01-20 18:07:09 +00001356 if (R.getValueAsBit("DuplicatesAllowedWhileMerging"))
1357 OS << " virtual bool duplicatesAllowed() const { return true; }\n\n";
1358
Peter Collingbournebee583f2011-10-06 13:03:08 +00001359 OS << "};\n\n";
1360 }
1361
1362 OS << "#endif\n";
1363}
1364
Richard Smith66e71682013-10-24 01:07:54 +00001365static bool isIdentifierArgument(Record *Arg) {
1366 return !Arg->getSuperClasses().empty() &&
1367 llvm::StringSwitch<bool>(Arg->getSuperClasses().back()->getName())
1368 .Case("IdentifierArgument", true)
1369 .Case("EnumArgument", true)
1370 .Default(false);
1371}
1372
Aaron Ballman4768b312013-11-04 12:55:56 +00001373/// \brief Emits the first-argument-is-type property for attributes.
1374void EmitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) {
1375 emitSourceFileHeader("llvm::StringSwitch code to match attributes with a "
1376 "type argument", OS);
1377
1378 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
1379
1380 for (std::vector<Record *>::iterator I = Attrs.begin(), E = Attrs.end();
1381 I != E; ++I) {
1382 Record &Attr = **I;
1383
1384 // Determine whether the first argument is a type.
1385 std::vector<Record *> Args = Attr.getValueAsListOfDefs("Args");
1386 if (Args.empty())
1387 continue;
1388
1389 if (Args[0]->getSuperClasses().back()->getName() != "TypeArgument")
1390 continue;
1391
1392 // All these spellings take a single type argument.
1393 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
1394 std::set<std::string> Emitted;
1395 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
1396 E = Spellings.end(); I != E; ++I) {
1397 if (Emitted.insert((*I)->getValueAsString("Name")).second)
1398 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", "
1399 << "true" << ")\n";
1400 }
1401 }
1402}
1403
Aaron Ballman15b27b92014-01-09 19:39:35 +00001404/// \brief Emits the parse-arguments-in-unevaluated-context property for
1405/// attributes.
1406void EmitClangAttrArgContextList(RecordKeeper &Records, raw_ostream &OS) {
1407 emitSourceFileHeader("StringSwitch code to match attributes which require "
1408 "an unevaluated context", OS);
1409
1410 ParsedAttrMap Attrs = getParsedAttrList(Records);
1411 for (ParsedAttrMap::const_iterator I = Attrs.begin(), E = Attrs.end();
1412 I != E; ++I) {
1413 const Record &Attr = *I->second;
1414
1415 if (!Attr.getValueAsBit("ParseArgumentsAsUnevaluated"))
1416 continue;
1417
1418 // All these spellings take are parsed unevaluated.
1419 std::vector<Record *> Spellings = Attr.getValueAsListOfDefs("Spellings");
1420 std::set<std::string> Emitted;
1421 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
1422 E = Spellings.end(); I != E; ++I) {
1423 if (Emitted.insert((*I)->getValueAsString("Name")).second)
1424 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", "
1425 << "true" << ")\n";
1426 }
1427
1428 }
1429}
1430
Richard Smith66e71682013-10-24 01:07:54 +00001431// Emits the first-argument-is-identifier property for attributes.
1432void EmitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) {
Douglas Gregord2472d42013-05-02 23:25:32 +00001433 emitSourceFileHeader("llvm::StringSwitch code to match attributes with "
Richard Smith66e71682013-10-24 01:07:54 +00001434 "an identifier argument", OS);
Douglas Gregord2472d42013-05-02 23:25:32 +00001435
1436 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1437
1438 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1439 I != E; ++I) {
1440 Record &Attr = **I;
1441
Richard Smith66e71682013-10-24 01:07:54 +00001442 // Determine whether the first argument is an identifier.
Douglas Gregord2472d42013-05-02 23:25:32 +00001443 std::vector<Record *> Args = Attr.getValueAsListOfDefs("Args");
Richard Smith66e71682013-10-24 01:07:54 +00001444 if (Args.empty() || !isIdentifierArgument(Args[0]))
Douglas Gregord2472d42013-05-02 23:25:32 +00001445 continue;
1446
Richard Smith66e71682013-10-24 01:07:54 +00001447 // All these spellings take an identifier argument.
Douglas Gregord2472d42013-05-02 23:25:32 +00001448 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
Richard Smith66e71682013-10-24 01:07:54 +00001449 std::set<std::string> Emitted;
Douglas Gregord2472d42013-05-02 23:25:32 +00001450 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
1451 E = Spellings.end(); I != E; ++I) {
Richard Smith66e71682013-10-24 01:07:54 +00001452 if (Emitted.insert((*I)->getValueAsString("Name")).second)
1453 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", "
1454 << "true" << ")\n";
Douglas Gregord2472d42013-05-02 23:25:32 +00001455 }
1456 }
1457}
1458
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001459// Emits the class method definitions for attributes.
1460void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001461 emitSourceFileHeader("Attribute classes' member function definitions", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001462
1463 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1464 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ri, re;
1465 std::vector<Argument*>::iterator ai, ae;
1466
1467 for (; i != e; ++i) {
1468 Record &R = **i;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001469
1470 if (!R.getValueAsBit("ASTNode"))
1471 continue;
1472
Peter Collingbournebee583f2011-10-06 13:03:08 +00001473 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1474 std::vector<Argument*> Args;
1475 for (ri = ArgRecords.begin(), re = ArgRecords.end(); ri != re; ++ri)
Aaron Ballmanaa47d242013-12-19 19:39:25 +00001476 Args.push_back(createArgument(**ri, R.getName()));
Peter Collingbournebee583f2011-10-06 13:03:08 +00001477
1478 for (ai = Args.begin(), ae = Args.end(); ai != ae; ++ai)
1479 (*ai)->writeAccessorDefinitions(OS);
1480
1481 OS << R.getName() << "Attr *" << R.getName()
1482 << "Attr::clone(ASTContext &C) const {\n";
1483 OS << " return new (C) " << R.getName() << "Attr(getLocation(), C";
1484 for (ai = Args.begin(); ai != ae; ++ai) {
1485 OS << ", ";
1486 (*ai)->writeCloneArgs(OS);
1487 }
Richard Smitha5aaca92013-01-29 04:21:28 +00001488 OS << ", getSpellingListIndex());\n}\n\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001489
Michael Han99315932013-01-24 16:46:58 +00001490 writePrettyPrintFunction(R, Args, OS);
Aaron Ballman3e424b52013-12-26 18:30:57 +00001491 writeGetSpellingFunction(R, OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001492 }
1493}
1494
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001495} // end namespace clang
1496
Peter Collingbournebee583f2011-10-06 13:03:08 +00001497static void EmitAttrList(raw_ostream &OS, StringRef Class,
1498 const std::vector<Record*> &AttrList) {
1499 std::vector<Record*>::const_iterator i = AttrList.begin(), e = AttrList.end();
1500
1501 if (i != e) {
1502 // Move the end iterator back to emit the last attribute.
Douglas Gregorb2daf842012-05-02 15:56:52 +00001503 for(--e; i != e; ++i) {
1504 if (!(*i)->getValueAsBit("ASTNode"))
1505 continue;
1506
Peter Collingbournebee583f2011-10-06 13:03:08 +00001507 OS << Class << "(" << (*i)->getName() << ")\n";
Douglas Gregorb2daf842012-05-02 15:56:52 +00001508 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001509
1510 OS << "LAST_" << Class << "(" << (*i)->getName() << ")\n\n";
1511 }
1512}
1513
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001514namespace clang {
1515
1516// Emits the enumeration list for attributes.
1517void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001518 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001519
1520 OS << "#ifndef LAST_ATTR\n";
1521 OS << "#define LAST_ATTR(NAME) ATTR(NAME)\n";
1522 OS << "#endif\n\n";
1523
1524 OS << "#ifndef INHERITABLE_ATTR\n";
1525 OS << "#define INHERITABLE_ATTR(NAME) ATTR(NAME)\n";
1526 OS << "#endif\n\n";
1527
1528 OS << "#ifndef LAST_INHERITABLE_ATTR\n";
1529 OS << "#define LAST_INHERITABLE_ATTR(NAME) INHERITABLE_ATTR(NAME)\n";
1530 OS << "#endif\n\n";
1531
1532 OS << "#ifndef INHERITABLE_PARAM_ATTR\n";
1533 OS << "#define INHERITABLE_PARAM_ATTR(NAME) ATTR(NAME)\n";
1534 OS << "#endif\n\n";
1535
1536 OS << "#ifndef LAST_INHERITABLE_PARAM_ATTR\n";
1537 OS << "#define LAST_INHERITABLE_PARAM_ATTR(NAME)"
1538 " INHERITABLE_PARAM_ATTR(NAME)\n";
1539 OS << "#endif\n\n";
1540
1541 Record *InhClass = Records.getClass("InheritableAttr");
1542 Record *InhParamClass = Records.getClass("InheritableParamAttr");
1543 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
Aaron Ballman8edb5c22013-12-18 23:44:18 +00001544 NonInhAttrs, InhAttrs, InhParamAttrs;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001545 for (std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end();
1546 i != e; ++i) {
Douglas Gregorb2daf842012-05-02 15:56:52 +00001547 if (!(*i)->getValueAsBit("ASTNode"))
1548 continue;
1549
Peter Collingbournebee583f2011-10-06 13:03:08 +00001550 if ((*i)->isSubClassOf(InhParamClass))
1551 InhParamAttrs.push_back(*i);
1552 else if ((*i)->isSubClassOf(InhClass))
1553 InhAttrs.push_back(*i);
1554 else
1555 NonInhAttrs.push_back(*i);
1556 }
1557
1558 EmitAttrList(OS, "INHERITABLE_PARAM_ATTR", InhParamAttrs);
1559 EmitAttrList(OS, "INHERITABLE_ATTR", InhAttrs);
1560 EmitAttrList(OS, "ATTR", NonInhAttrs);
1561
1562 OS << "#undef LAST_ATTR\n";
1563 OS << "#undef INHERITABLE_ATTR\n";
1564 OS << "#undef LAST_INHERITABLE_ATTR\n";
1565 OS << "#undef LAST_INHERITABLE_PARAM_ATTR\n";
1566 OS << "#undef ATTR\n";
1567}
1568
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001569// Emits the code to read an attribute from a precompiled header.
1570void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001571 emitSourceFileHeader("Attribute deserialization code", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001572
1573 Record *InhClass = Records.getClass("InheritableAttr");
1574 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
1575 ArgRecords;
1576 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ai, ae;
1577 std::vector<Argument*> Args;
1578 std::vector<Argument*>::iterator ri, re;
1579
1580 OS << " switch (Kind) {\n";
1581 OS << " default:\n";
1582 OS << " assert(0 && \"Unknown attribute!\");\n";
1583 OS << " break;\n";
1584 for (; i != e; ++i) {
1585 Record &R = **i;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001586 if (!R.getValueAsBit("ASTNode"))
1587 continue;
1588
Peter Collingbournebee583f2011-10-06 13:03:08 +00001589 OS << " case attr::" << R.getName() << ": {\n";
1590 if (R.isSubClassOf(InhClass))
1591 OS << " bool isInherited = Record[Idx++];\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00001592 OS << " bool isImplicit = Record[Idx++];\n";
1593 OS << " unsigned Spelling = Record[Idx++];\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001594 ArgRecords = R.getValueAsListOfDefs("Args");
1595 Args.clear();
1596 for (ai = ArgRecords.begin(), ae = ArgRecords.end(); ai != ae; ++ai) {
Aaron Ballmanaa47d242013-12-19 19:39:25 +00001597 Argument *A = createArgument(**ai, R.getName());
Peter Collingbournebee583f2011-10-06 13:03:08 +00001598 Args.push_back(A);
1599 A->writePCHReadDecls(OS);
1600 }
1601 OS << " New = new (Context) " << R.getName() << "Attr(Range, Context";
1602 for (ri = Args.begin(), re = Args.end(); ri != re; ++ri) {
1603 OS << ", ";
1604 (*ri)->writePCHReadArgs(OS);
1605 }
Aaron Ballman36a53502014-01-16 13:03:14 +00001606 OS << ", Spelling);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001607 if (R.isSubClassOf(InhClass))
1608 OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00001609 OS << " New->setImplicit(isImplicit);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001610 OS << " break;\n";
1611 OS << " }\n";
1612 }
1613 OS << " }\n";
1614}
1615
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001616// Emits the code to write an attribute to a precompiled header.
1617void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001618 emitSourceFileHeader("Attribute serialization code", OS);
1619
Peter Collingbournebee583f2011-10-06 13:03:08 +00001620 Record *InhClass = Records.getClass("InheritableAttr");
1621 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
1622 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ai, ae;
1623
1624 OS << " switch (A->getKind()) {\n";
1625 OS << " default:\n";
1626 OS << " llvm_unreachable(\"Unknown attribute kind!\");\n";
1627 OS << " break;\n";
1628 for (; i != e; ++i) {
1629 Record &R = **i;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001630 if (!R.getValueAsBit("ASTNode"))
1631 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001632 OS << " case attr::" << R.getName() << ": {\n";
1633 Args = R.getValueAsListOfDefs("Args");
1634 if (R.isSubClassOf(InhClass) || !Args.empty())
1635 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
1636 << "Attr>(A);\n";
1637 if (R.isSubClassOf(InhClass))
1638 OS << " Record.push_back(SA->isInherited());\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00001639 OS << " Record.push_back(A->isImplicit());\n";
1640 OS << " Record.push_back(A->getSpellingListIndex());\n";
1641
Peter Collingbournebee583f2011-10-06 13:03:08 +00001642 for (ai = Args.begin(), ae = Args.end(); ai != ae; ++ai)
1643 createArgument(**ai, R.getName())->writePCHWrite(OS);
1644 OS << " break;\n";
1645 OS << " }\n";
1646 }
1647 OS << " }\n";
1648}
1649
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001650// Emits the list of spellings for attributes.
1651void EmitClangAttrSpellingList(RecordKeeper &Records, raw_ostream &OS) {
Aaron Ballman0fa06d82014-01-09 22:57:44 +00001652 emitSourceFileHeader("llvm::StringSwitch code to match attributes based on "
1653 "the target triple, T", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001654
1655 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1656
Aaron Ballman0fa06d82014-01-09 22:57:44 +00001657 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1658 I != E; ++I) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001659 Record &Attr = **I;
1660
Aaron Ballman0fa06d82014-01-09 22:57:44 +00001661 // It is assumed that there will be an llvm::Triple object named T within
1662 // scope that can be used to determine whether the attribute exists in
1663 // a given target.
1664 std::string Test;
1665 if (Attr.isSubClassOf("TargetSpecificAttr")) {
1666 const Record *R = Attr.getValueAsDef("Target");
1667 std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001668
Aaron Ballman0fa06d82014-01-09 22:57:44 +00001669 Test += "(";
1670 for (std::vector<std::string>::const_iterator AI = Arches.begin(),
1671 AE = Arches.end(); AI != AE; ++AI) {
1672 std::string Part = *AI;
1673 Test += "T.getArch() == llvm::Triple::" + Part;
1674 if (AI + 1 != AE)
1675 Test += " || ";
1676 }
1677 Test += ")";
1678
1679 std::vector<std::string> OSes;
1680 if (!R->isValueUnset("OSes")) {
1681 Test += " && (";
1682 std::vector<std::string> OSes = R->getValueAsListOfStrings("OSes");
1683 for (std::vector<std::string>::const_iterator AI = OSes.begin(),
1684 AE = OSes.end(); AI != AE; ++AI) {
1685 std::string Part = *AI;
1686
1687 Test += "T.getOS() == llvm::Triple::" + Part;
1688 if (AI + 1 != AE)
1689 Test += " || ";
1690 }
1691 Test += ")";
1692 }
1693 } else
1694 Test = "true";
1695
1696 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
1697 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
1698 E = Spellings.end(); I != E; ++I) {
1699 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", " << Test;
1700 OS << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001701 }
1702 }
1703
1704}
1705
Michael Han99315932013-01-24 16:46:58 +00001706void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001707 emitSourceFileHeader("Code to translate different attribute spellings "
1708 "into internal identifiers", OS);
Michael Han99315932013-01-24 16:46:58 +00001709
1710 OS <<
Michael Han99315932013-01-24 16:46:58 +00001711 " switch (AttrKind) {\n"
1712 " default:\n"
1713 " llvm_unreachable(\"Unknown attribute kind!\");\n"
1714 " break;\n";
1715
Aaron Ballman64e69862013-12-15 13:05:48 +00001716 ParsedAttrMap Attrs = getParsedAttrList(Records);
1717 for (ParsedAttrMap::const_iterator I = Attrs.begin(), E = Attrs.end();
Michael Han99315932013-01-24 16:46:58 +00001718 I != E; ++I) {
Aaron Ballman64e69862013-12-15 13:05:48 +00001719 Record &R = *I->second;
Michael Han99315932013-01-24 16:46:58 +00001720 std::vector<Record*> Spellings = R.getValueAsListOfDefs("Spellings");
Aaron Ballman64e69862013-12-15 13:05:48 +00001721 OS << " case AT_" << I->first << ": {\n";
Richard Smith852e9ce2013-11-27 01:46:48 +00001722 for (unsigned I = 0; I < Spellings.size(); ++ I) {
1723 SmallString<16> Namespace;
1724 if (Spellings[I]->getValueAsString("Variety") == "CXX11")
1725 Namespace = Spellings[I]->getValueAsString("Namespace");
1726 else
1727 Namespace = "";
Michael Han99315932013-01-24 16:46:58 +00001728
Richard Smith852e9ce2013-11-27 01:46:48 +00001729 OS << " if (Name == \""
1730 << Spellings[I]->getValueAsString("Name") << "\" && "
1731 << "SyntaxUsed == "
1732 << StringSwitch<unsigned>(Spellings[I]->getValueAsString("Variety"))
1733 .Case("GNU", 0)
1734 .Case("CXX11", 1)
1735 .Case("Declspec", 2)
1736 .Case("Keyword", 3)
1737 .Default(0)
1738 << " && Scope == \"" << Namespace << "\")\n"
1739 << " return " << I << ";\n";
Michael Han99315932013-01-24 16:46:58 +00001740 }
Richard Smith852e9ce2013-11-27 01:46:48 +00001741
1742 OS << " break;\n";
1743 OS << " }\n";
Michael Han99315932013-01-24 16:46:58 +00001744 }
1745
1746 OS << " }\n";
Aaron Ballman64e69862013-12-15 13:05:48 +00001747 OS << " return 0;\n";
Michael Han99315932013-01-24 16:46:58 +00001748}
1749
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001750// Emits code used by RecursiveASTVisitor to visit attributes
1751void EmitClangAttrASTVisitor(RecordKeeper &Records, raw_ostream &OS) {
1752 emitSourceFileHeader("Used by RecursiveASTVisitor to visit attributes.", OS);
1753
1754 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1755
1756 // Write method declarations for Traverse* methods.
1757 // We emit this here because we only generate methods for attributes that
1758 // are declared as ASTNodes.
1759 OS << "#ifdef ATTR_VISITOR_DECLS_ONLY\n\n";
1760 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1761 I != E; ++I) {
1762 Record &R = **I;
1763 if (!R.getValueAsBit("ASTNode"))
1764 continue;
1765 OS << " bool Traverse"
1766 << R.getName() << "Attr(" << R.getName() << "Attr *A);\n";
1767 OS << " bool Visit"
1768 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
1769 << " return true; \n"
1770 << " };\n";
1771 }
1772 OS << "\n#else // ATTR_VISITOR_DECLS_ONLY\n\n";
1773
1774 // Write individual Traverse* methods for each attribute class.
1775 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1776 I != E; ++I) {
1777 Record &R = **I;
1778 if (!R.getValueAsBit("ASTNode"))
1779 continue;
1780
1781 OS << "template <typename Derived>\n"
DeLesley Hutchinsbb79c332013-12-30 21:03:02 +00001782 << "bool VISITORCLASS<Derived>::Traverse"
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001783 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
1784 << " if (!getDerived().VisitAttr(A))\n"
1785 << " return false;\n"
1786 << " if (!getDerived().Visit" << R.getName() << "Attr(A))\n"
1787 << " return false;\n";
1788
1789 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1790 for (std::vector<Record*>::iterator ri = ArgRecords.begin(),
1791 re = ArgRecords.end();
1792 ri != re; ++ri) {
1793 Record &ArgRecord = **ri;
1794 Argument *Arg = createArgument(ArgRecord, R.getName());
1795 assert(Arg);
1796 Arg->writeASTVisitorTraversal(OS);
1797 }
1798
1799 OS << " return true;\n";
1800 OS << "}\n\n";
1801 }
1802
1803 // Write generic Traverse routine
1804 OS << "template <typename Derived>\n"
DeLesley Hutchinsbb79c332013-12-30 21:03:02 +00001805 << "bool VISITORCLASS<Derived>::TraverseAttr(Attr *A) {\n"
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001806 << " if (!A)\n"
1807 << " return true;\n"
1808 << "\n"
1809 << " switch (A->getKind()) {\n"
1810 << " default:\n"
1811 << " return true;\n";
1812
1813 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1814 I != E; ++I) {
1815 Record &R = **I;
1816 if (!R.getValueAsBit("ASTNode"))
1817 continue;
1818
1819 OS << " case attr::" << R.getName() << ":\n"
1820 << " return getDerived().Traverse" << R.getName() << "Attr("
1821 << "cast<" << R.getName() << "Attr>(A));\n";
1822 }
1823 OS << " }\n"; // end case
1824 OS << "}\n"; // end function
1825 OS << "#endif // ATTR_VISITOR_DECLS_ONLY\n";
1826}
1827
1828
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001829// Emits the LateParsed property for attributes.
1830void EmitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001831 emitSourceFileHeader("llvm::StringSwitch code to match late parsed "
1832 "attributes", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001833
1834 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1835
1836 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1837 I != E; ++I) {
1838 Record &Attr = **I;
1839
1840 bool LateParsed = Attr.getValueAsBit("LateParsed");
1841
1842 if (LateParsed) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001843 std::vector<Record*> Spellings =
1844 Attr.getValueAsListOfDefs("Spellings");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001845
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001846 // FIXME: Handle non-GNU attributes
1847 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
Peter Collingbournebee583f2011-10-06 13:03:08 +00001848 E = Spellings.end(); I != E; ++I) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00001849 if ((*I)->getValueAsString("Variety") != "GNU")
1850 continue;
1851 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", "
1852 << LateParsed << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001853 }
1854 }
1855 }
1856}
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001857
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001858// Emits code to instantiate dependent attributes on templates.
1859void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001860 emitSourceFileHeader("Template instantiation code for attributes", OS);
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001861
1862 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1863
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00001864 OS << "namespace clang {\n"
1865 << "namespace sema {\n\n"
1866 << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, "
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001867 << "Sema &S,\n"
1868 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"
1869 << " switch (At->getKind()) {\n"
1870 << " default:\n"
1871 << " break;\n";
1872
1873 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1874 I != E; ++I) {
1875 Record &R = **I;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001876 if (!R.getValueAsBit("ASTNode"))
1877 continue;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001878
1879 OS << " case attr::" << R.getName() << ": {\n";
Rafael Espindola7f90b7d2012-05-15 14:09:55 +00001880 bool ShouldClone = R.getValueAsBit("Clone");
1881
1882 if (!ShouldClone) {
1883 OS << " return NULL;\n";
1884 OS << " }\n";
1885 continue;
1886 }
1887
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001888 OS << " const " << R.getName() << "Attr *A = cast<"
1889 << R.getName() << "Attr>(At);\n";
1890 bool TDependent = R.getValueAsBit("TemplateDependent");
1891
1892 if (!TDependent) {
1893 OS << " return A->clone(C);\n";
1894 OS << " }\n";
1895 continue;
1896 }
1897
1898 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1899 std::vector<Argument*> Args;
1900 std::vector<Argument*>::iterator ai, ae;
1901 Args.reserve(ArgRecords.size());
1902
1903 for (std::vector<Record*>::iterator ri = ArgRecords.begin(),
1904 re = ArgRecords.end();
1905 ri != re; ++ri) {
1906 Record &ArgRecord = **ri;
Aaron Ballmanaa47d242013-12-19 19:39:25 +00001907 Argument *Arg = createArgument(ArgRecord, R.getName());
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001908 assert(Arg);
1909 Args.push_back(Arg);
1910 }
1911 ae = Args.end();
1912
1913 for (ai = Args.begin(); ai != ae; ++ai) {
1914 (*ai)->writeTemplateInstantiation(OS);
1915 }
1916 OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C";
1917 for (ai = Args.begin(); ai != ae; ++ai) {
1918 OS << ", ";
1919 (*ai)->writeTemplateInstantiationArgs(OS);
1920 }
Aaron Ballman36a53502014-01-16 13:03:14 +00001921 OS << ", A->getSpellingListIndex());\n }\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001922 }
1923 OS << " } // end switch\n"
1924 << " llvm_unreachable(\"Unknown attribute!\");\n"
1925 << " return 0;\n"
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00001926 << "}\n\n"
1927 << "} // end namespace sema\n"
1928 << "} // end namespace clang\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001929}
1930
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001931// Emits the list of parsed attributes.
1932void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) {
1933 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
1934
1935 OS << "#ifndef PARSED_ATTR\n";
1936 OS << "#define PARSED_ATTR(NAME) NAME\n";
1937 OS << "#endif\n\n";
1938
1939 ParsedAttrMap Names = getParsedAttrList(Records);
1940 for (ParsedAttrMap::iterator I = Names.begin(), E = Names.end(); I != E;
1941 ++I) {
1942 OS << "PARSED_ATTR(" << I->first << ")\n";
1943 }
1944}
1945
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001946static void emitArgInfo(const Record &R, std::stringstream &OS) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001947 // This function will count the number of arguments specified for the
1948 // attribute and emit the number of required arguments followed by the
1949 // number of optional arguments.
1950 std::vector<Record *> Args = R.getValueAsListOfDefs("Args");
1951 unsigned ArgCount = 0, OptCount = 0;
1952 for (std::vector<Record *>::const_iterator I = Args.begin(), E = Args.end();
1953 I != E; ++I) {
1954 const Record &Arg = **I;
1955 Arg.getValueAsBit("Optional") ? ++OptCount : ++ArgCount;
1956 }
1957 OS << ArgCount << ", " << OptCount;
1958}
1959
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001960static void GenerateDefaultAppertainsTo(raw_ostream &OS) {
Aaron Ballman93b5cc62013-12-02 19:36:42 +00001961 OS << "static bool defaultAppertainsTo(Sema &, const AttributeList &,";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001962 OS << "const Decl *) {\n";
1963 OS << " return true;\n";
1964 OS << "}\n\n";
1965}
1966
1967static std::string CalculateDiagnostic(const Record &S) {
1968 // If the SubjectList object has a custom diagnostic associated with it,
1969 // return that directly.
1970 std::string CustomDiag = S.getValueAsString("CustomDiag");
1971 if (!CustomDiag.empty())
1972 return CustomDiag;
1973
1974 // Given the list of subjects, determine what diagnostic best fits.
1975 enum {
1976 Func = 1U << 0,
1977 Var = 1U << 1,
1978 ObjCMethod = 1U << 2,
1979 Param = 1U << 3,
1980 Class = 1U << 4,
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00001981 GenericRecord = 1U << 5,
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001982 Type = 1U << 6,
1983 ObjCIVar = 1U << 7,
1984 ObjCProp = 1U << 8,
1985 ObjCInterface = 1U << 9,
1986 Block = 1U << 10,
1987 Namespace = 1U << 11,
1988 FuncTemplate = 1U << 12,
1989 Field = 1U << 13,
Ted Kremenekd980da22013-12-10 19:43:42 +00001990 CXXMethod = 1U << 14,
1991 ObjCProtocol = 1U << 15
Aaron Ballman74eeeae2013-11-27 13:27:02 +00001992 };
1993 uint32_t SubMask = 0;
1994
1995 std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects");
1996 for (std::vector<Record *>::const_iterator I = Subjects.begin(),
1997 E = Subjects.end(); I != E; ++I) {
Aaron Ballman80469032013-11-29 14:57:58 +00001998 const Record &R = (**I);
1999 std::string Name;
2000
2001 if (R.isSubClassOf("SubsetSubject")) {
2002 PrintError(R.getLoc(), "SubsetSubjects should use a custom diagnostic");
2003 // As a fallback, look through the SubsetSubject to see what its base
2004 // type is, and use that. This needs to be updated if SubsetSubjects
2005 // are allowed within other SubsetSubjects.
2006 Name = R.getValueAsDef("Base")->getName();
2007 } else
2008 Name = R.getName();
2009
2010 uint32_t V = StringSwitch<uint32_t>(Name)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002011 .Case("Function", Func)
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00002012 .Case("FunctionDefinition", Func)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002013 .Case("Var", Var)
2014 .Case("ObjCMethod", ObjCMethod)
2015 .Case("ParmVar", Param)
2016 .Case("TypedefName", Type)
2017 .Case("ObjCIvar", ObjCIVar)
2018 .Case("ObjCProperty", ObjCProp)
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00002019 .Case("Record", GenericRecord)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002020 .Case("ObjCInterface", ObjCInterface)
Ted Kremenekd980da22013-12-10 19:43:42 +00002021 .Case("ObjCProtocol", ObjCProtocol)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002022 .Case("Block", Block)
2023 .Case("CXXRecord", Class)
2024 .Case("Namespace", Namespace)
2025 .Case("FunctionTemplate", FuncTemplate)
2026 .Case("Field", Field)
2027 .Case("CXXMethod", CXXMethod)
2028 .Default(0);
2029 if (!V) {
2030 // Something wasn't in our mapping, so be helpful and let the developer
2031 // know about it.
2032 PrintFatalError((*I)->getLoc(), "Unknown subject type: " +
2033 (*I)->getName());
2034 return "";
2035 }
2036
2037 SubMask |= V;
2038 }
2039
2040 switch (SubMask) {
2041 // For the simple cases where there's only a single entry in the mask, we
2042 // don't have to resort to bit fiddling.
2043 case Func: return "ExpectedFunction";
2044 case Var: return "ExpectedVariable";
2045 case Param: return "ExpectedParameter";
2046 case Class: return "ExpectedClass";
2047 case CXXMethod:
2048 // FIXME: Currently, this maps to ExpectedMethod based on existing code,
2049 // but should map to something a bit more accurate at some point.
2050 case ObjCMethod: return "ExpectedMethod";
2051 case Type: return "ExpectedType";
2052 case ObjCInterface: return "ExpectedObjectiveCInterface";
Ted Kremenekd980da22013-12-10 19:43:42 +00002053 case ObjCProtocol: return "ExpectedObjectiveCProtocol";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002054
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00002055 // "GenericRecord" means struct, union or class; check the language options
2056 // and if not compiling for C++, strip off the class part. Note that this
2057 // relies on the fact that the context for this declares "Sema &S".
2058 case GenericRecord:
Aaron Ballman17046b82013-11-27 19:16:55 +00002059 return "(S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass : "
2060 "ExpectedStructOrUnion)";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002061 case Func | ObjCMethod | Block: return "ExpectedFunctionMethodOrBlock";
2062 case Func | ObjCMethod | Class: return "ExpectedFunctionMethodOrClass";
2063 case Func | Param:
2064 case Func | ObjCMethod | Param: return "ExpectedFunctionMethodOrParameter";
2065 case Func | FuncTemplate:
2066 case Func | ObjCMethod: return "ExpectedFunctionOrMethod";
2067 case Func | Var: return "ExpectedVariableOrFunction";
Aaron Ballman604dfec2013-12-02 17:07:07 +00002068
2069 // If not compiling for C++, the class portion does not apply.
2070 case Func | Var | Class:
2071 return "(S.getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass : "
2072 "ExpectedVariableOrFunction)";
2073
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002074 case ObjCMethod | ObjCProp: return "ExpectedMethodOrProperty";
2075 case Field | Var: return "ExpectedFieldOrGlobalVar";
2076 }
2077
2078 PrintFatalError(S.getLoc(),
2079 "Could not deduce diagnostic argument for Attr subjects");
2080
2081 return "";
2082}
2083
Aaron Ballman12b9f652014-01-16 13:55:42 +00002084static std::string GetSubjectWithSuffix(const Record *R) {
2085 std::string B = R->getName();
2086 if (B == "DeclBase")
2087 return "Decl";
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00002088 else if (B == "FunctionDefinition")
2089 return "FunctionDecl";
Aaron Ballman12b9f652014-01-16 13:55:42 +00002090 return B + "Decl";
2091}
Aaron Ballman80469032013-11-29 14:57:58 +00002092static std::string GenerateCustomAppertainsTo(const Record &Subject,
2093 raw_ostream &OS) {
Aaron Ballmana358c902013-12-02 14:58:17 +00002094 std::string FnName = "is" + Subject.getName();
2095
Aaron Ballman80469032013-11-29 14:57:58 +00002096 // If this code has already been generated, simply return the previous
2097 // instance of it.
2098 static std::set<std::string> CustomSubjectSet;
Aaron Ballmana358c902013-12-02 14:58:17 +00002099 std::set<std::string>::iterator I = CustomSubjectSet.find(FnName);
Aaron Ballman80469032013-11-29 14:57:58 +00002100 if (I != CustomSubjectSet.end())
2101 return *I;
2102
2103 Record *Base = Subject.getValueAsDef("Base");
2104
2105 // Not currently support custom subjects within custom subjects.
2106 if (Base->isSubClassOf("SubsetSubject")) {
2107 PrintFatalError(Subject.getLoc(),
2108 "SubsetSubjects within SubsetSubjects is not supported");
2109 return "";
2110 }
2111
Aaron Ballman80469032013-11-29 14:57:58 +00002112 OS << "static bool " << FnName << "(const Decl *D) {\n";
Aaron Ballman47553042014-01-16 14:32:03 +00002113 OS << " if (const " << GetSubjectWithSuffix(Base) << " *S = dyn_cast<";
Aaron Ballman12b9f652014-01-16 13:55:42 +00002114 OS << GetSubjectWithSuffix(Base);
Aaron Ballman47553042014-01-16 14:32:03 +00002115 OS << ">(D))\n";
2116 OS << " return " << Subject.getValueAsString("CheckCode") << ";\n";
2117 OS << " return false;\n";
Aaron Ballman80469032013-11-29 14:57:58 +00002118 OS << "}\n\n";
2119
2120 CustomSubjectSet.insert(FnName);
2121 return FnName;
2122}
2123
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002124static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) {
2125 // If the attribute does not contain a Subjects definition, then use the
2126 // default appertainsTo logic.
2127 if (Attr.isValueUnset("Subjects"))
Aaron Ballman93b5cc62013-12-02 19:36:42 +00002128 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002129
2130 const Record *SubjectObj = Attr.getValueAsDef("Subjects");
2131 std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
2132
2133 // If the list of subjects is empty, it is assumed that the attribute
2134 // appertains to everything.
2135 if (Subjects.empty())
Aaron Ballman93b5cc62013-12-02 19:36:42 +00002136 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002137
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002138 bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn");
2139
2140 // Otherwise, generate an appertainsTo check specific to this attribute which
2141 // checks all of the given subjects against the Decl passed in. Return the
2142 // name of that check to the caller.
Aaron Ballman00dcc432013-12-03 13:45:50 +00002143 std::string FnName = "check" + Attr.getName() + "AppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002144 std::stringstream SS;
2145 SS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr, ";
2146 SS << "const Decl *D) {\n";
2147 SS << " if (";
2148 for (std::vector<Record *>::const_iterator I = Subjects.begin(),
2149 E = Subjects.end(); I != E; ++I) {
Aaron Ballman80469032013-11-29 14:57:58 +00002150 // If the subject has custom code associated with it, generate a function
2151 // for it. The function cannot be inlined into this check (yet) because it
2152 // requires the subject to be of a specific type, and were that information
2153 // inlined here, it would not support an attribute with multiple custom
2154 // subjects.
2155 if ((*I)->isSubClassOf("SubsetSubject")) {
2156 SS << "!" << GenerateCustomAppertainsTo(**I, OS) << "(D)";
2157 } else {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002158 SS << "!isa<" << GetSubjectWithSuffix(*I) << ">(D)";
Aaron Ballman80469032013-11-29 14:57:58 +00002159 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002160
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002161 if (I + 1 != E)
2162 SS << " && ";
2163 }
2164 SS << ") {\n";
2165 SS << " S.Diag(Attr.getLoc(), diag::";
2166 SS << (Warn ? "warn_attribute_wrong_decl_type" :
2167 "err_attribute_wrong_decl_type");
2168 SS << ")\n";
2169 SS << " << Attr.getName() << ";
2170 SS << CalculateDiagnostic(*SubjectObj) << ";\n";
2171 SS << " return false;\n";
2172 SS << " }\n";
2173 SS << " return true;\n";
2174 SS << "}\n\n";
2175
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002176 OS << SS.str();
2177 return FnName;
2178}
2179
Aaron Ballman3aff6332013-12-02 19:30:36 +00002180static void GenerateDefaultLangOptRequirements(raw_ostream &OS) {
2181 OS << "static bool defaultDiagnoseLangOpts(Sema &, ";
2182 OS << "const AttributeList &) {\n";
2183 OS << " return true;\n";
2184 OS << "}\n\n";
2185}
2186
2187static std::string GenerateLangOptRequirements(const Record &R,
2188 raw_ostream &OS) {
2189 // If the attribute has an empty or unset list of language requirements,
2190 // return the default handler.
2191 std::vector<Record *> LangOpts = R.getValueAsListOfDefs("LangOpts");
2192 if (LangOpts.empty())
2193 return "defaultDiagnoseLangOpts";
2194
2195 // Generate the test condition, as well as a unique function name for the
2196 // diagnostic test. The list of options should usually be short (one or two
2197 // options), and the uniqueness isn't strictly necessary (it is just for
2198 // codegen efficiency).
2199 std::string FnName = "check", Test;
2200 for (std::vector<Record *>::const_iterator I = LangOpts.begin(),
2201 E = LangOpts.end(); I != E; ++I) {
2202 std::string Part = (*I)->getValueAsString("Name");
2203 Test += "S.LangOpts." + Part;
2204 if (I + 1 != E)
2205 Test += " || ";
2206 FnName += Part;
2207 }
2208 FnName += "LangOpts";
2209
2210 // If this code has already been generated, simply return the previous
2211 // instance of it.
2212 static std::set<std::string> CustomLangOptsSet;
2213 std::set<std::string>::iterator I = CustomLangOptsSet.find(FnName);
2214 if (I != CustomLangOptsSet.end())
2215 return *I;
2216
2217 OS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr) {\n";
2218 OS << " if (" << Test << ")\n";
2219 OS << " return true;\n\n";
2220 OS << " S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) ";
2221 OS << "<< Attr.getName();\n";
2222 OS << " return false;\n";
2223 OS << "}\n\n";
2224
2225 CustomLangOptsSet.insert(FnName);
2226 return FnName;
2227}
2228
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002229static void GenerateDefaultTargetRequirements(raw_ostream &OS) {
2230 OS << "static bool defaultTargetRequirements(llvm::Triple) {\n";
2231 OS << " return true;\n";
2232 OS << "}\n\n";
2233}
2234
2235static std::string GenerateTargetRequirements(const Record &Attr,
2236 const ParsedAttrMap &Dupes,
2237 raw_ostream &OS) {
2238 // If the attribute is not a target specific attribute, return the default
2239 // target handler.
2240 if (!Attr.isSubClassOf("TargetSpecificAttr"))
2241 return "defaultTargetRequirements";
2242
2243 // Get the list of architectures to be tested for.
2244 const Record *R = Attr.getValueAsDef("Target");
2245 std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches");
2246 if (Arches.empty()) {
2247 PrintError(Attr.getLoc(), "Empty list of target architectures for a "
2248 "target-specific attr");
2249 return "defaultTargetRequirements";
2250 }
2251
2252 // If there are other attributes which share the same parsed attribute kind,
2253 // such as target-specific attributes with a shared spelling, collapse the
2254 // duplicate architectures. This is required because a shared target-specific
2255 // attribute has only one AttributeList::Kind enumeration value, but it
2256 // applies to multiple target architectures. In order for the attribute to be
2257 // considered valid, all of its architectures need to be included.
2258 if (!Attr.isValueUnset("ParseKind")) {
2259 std::string APK = Attr.getValueAsString("ParseKind");
2260 for (ParsedAttrMap::const_iterator I = Dupes.begin(), E = Dupes.end();
2261 I != E; ++I) {
2262 if (I->first == APK) {
2263 std::vector<std::string> DA = I->second->getValueAsDef("Target")->
2264 getValueAsListOfStrings("Arches");
2265 std::copy(DA.begin(), DA.end(), std::back_inserter(Arches));
2266 }
2267 }
2268 }
2269
2270 std::string FnName = "isTarget", Test = "(";
2271 for (std::vector<std::string>::const_iterator I = Arches.begin(),
2272 E = Arches.end(); I != E; ++I) {
2273 std::string Part = *I;
2274 Test += "Arch == llvm::Triple::" + Part;
2275 if (I + 1 != E)
2276 Test += " || ";
2277 FnName += Part;
2278 }
2279 Test += ")";
2280
2281 // If the target also requires OS testing, generate those tests as well.
2282 bool UsesOS = false;
2283 if (!R->isValueUnset("OSes")) {
2284 UsesOS = true;
2285
2286 // We know that there was at least one arch test, so we need to and in the
2287 // OS tests.
2288 Test += " && (";
2289 std::vector<std::string> OSes = R->getValueAsListOfStrings("OSes");
2290 for (std::vector<std::string>::const_iterator I = OSes.begin(),
2291 E = OSes.end(); I != E; ++I) {
2292 std::string Part = *I;
2293
2294 Test += "OS == llvm::Triple::" + Part;
2295 if (I + 1 != E)
2296 Test += " || ";
2297 FnName += Part;
2298 }
2299 Test += ")";
2300 }
2301
2302 // If this code has already been generated, simply return the previous
2303 // instance of it.
2304 static std::set<std::string> CustomTargetSet;
2305 std::set<std::string>::iterator I = CustomTargetSet.find(FnName);
2306 if (I != CustomTargetSet.end())
2307 return *I;
2308
2309 OS << "static bool " << FnName << "(llvm::Triple T) {\n";
2310 OS << " llvm::Triple::ArchType Arch = T.getArch();\n";
2311 if (UsesOS)
2312 OS << " llvm::Triple::OSType OS = T.getOS();\n";
2313 OS << " return " << Test << ";\n";
2314 OS << "}\n\n";
2315
2316 CustomTargetSet.insert(FnName);
2317 return FnName;
2318}
2319
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00002320static bool CanAppearOnFuncDef(const Record &Attr) {
2321 // Look at the subjects this function appertains to; if a FunctionDefinition
2322 // appears in the list, then this attribute can appear on a function
2323 // definition.
2324 if (Attr.isValueUnset("Subjects"))
2325 return false;
2326
2327 std::vector<Record *> Subjects = Attr.getValueAsDef("Subjects")->
2328 getValueAsListOfDefs("Subjects");
2329 for (std::vector<Record *>::const_iterator I = Subjects.begin(),
2330 E = Subjects.end(); I != E; ++I) {
2331 const Record &Subject = **I;
2332 if (Subject.getName() == "FunctionDefinition")
2333 return true;
2334 }
2335 return false;
2336}
2337
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002338/// Emits the parsed attribute helpers
2339void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
2340 emitSourceFileHeader("Parsed attribute helpers", OS);
2341
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002342 // Get the list of parsed attributes, and accept the optional list of
2343 // duplicates due to the ParseKind.
2344 ParsedAttrMap Dupes;
2345 ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002346
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002347 // Generate the default appertainsTo, target and language option diagnostic
2348 // methods.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002349 GenerateDefaultAppertainsTo(OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00002350 GenerateDefaultLangOptRequirements(OS);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002351 GenerateDefaultTargetRequirements(OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002352
2353 // Generate the appertainsTo diagnostic methods and write their names into
2354 // another mapping. At the same time, generate the AttrInfoMap object
2355 // contents. Due to the reliance on generated code, use separate streams so
2356 // that code will not be interleaved.
2357 std::stringstream SS;
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002358 for (ParsedAttrMap::iterator I = Attrs.begin(), E = Attrs.end(); I != E;
2359 ++I) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002360 // TODO: If the attribute's kind appears in the list of duplicates, that is
2361 // because it is a target-specific attribute that appears multiple times.
2362 // It would be beneficial to test whether the duplicates are "similar
2363 // enough" to each other to not cause problems. For instance, check that
Alp Toker96cf7582014-01-18 21:49:37 +00002364 // the spellings are identical, and custom parsing rules match, etc.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002365
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002366 // We need to generate struct instances based off ParsedAttrInfo from
2367 // AttributeList.cpp.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002368 SS << " { ";
2369 emitArgInfo(*I->second, SS);
2370 SS << ", " << I->second->getValueAsBit("HasCustomParsing");
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002371 SS << ", " << I->second->isSubClassOf("TargetSpecificAttr");
2372 SS << ", " << I->second->isSubClassOf("TypeAttr");
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00002373 SS << ", " << CanAppearOnFuncDef(*I->second);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002374 SS << ", " << GenerateAppertainsTo(*I->second, OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00002375 SS << ", " << GenerateLangOptRequirements(*I->second, OS);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002376 SS << ", " << GenerateTargetRequirements(*I->second, Dupes, OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002377 SS << " }";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002378
2379 if (I + 1 != E)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002380 SS << ",";
2381
2382 SS << " // AT_" << I->first << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002383 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002384
2385 OS << "static const ParsedAttrInfo AttrInfoMap[AttributeList::UnknownAttribute + 1] = {\n";
2386 OS << SS.str();
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002387 OS << "};\n\n";
Michael Han4a045172012-03-07 00:12:16 +00002388}
2389
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002390// Emits the kind list of parsed attributes
2391void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002392 emitSourceFileHeader("Attribute name matcher", OS);
2393
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002394 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
2395 std::vector<StringMatcher::StringPair> GNU, Declspec, CXX11, Keywords;
Aaron Ballman64e69862013-12-15 13:05:48 +00002396 std::set<std::string> Seen;
Michael Han4a045172012-03-07 00:12:16 +00002397 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
2398 I != E; ++I) {
2399 Record &Attr = **I;
Richard Smith852e9ce2013-11-27 01:46:48 +00002400
Michael Han4a045172012-03-07 00:12:16 +00002401 bool SemaHandler = Attr.getValueAsBit("SemaHandler");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00002402 bool Ignored = Attr.getValueAsBit("Ignored");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00002403 if (SemaHandler || Ignored) {
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002404 // Attribute spellings can be shared between target-specific attributes,
2405 // and can be shared between syntaxes for the same attribute. For
2406 // instance, an attribute can be spelled GNU<"interrupt"> for an ARM-
2407 // specific attribute, or MSP430-specific attribute. Additionally, an
2408 // attribute can be spelled GNU<"dllexport"> and Declspec<"dllexport">
2409 // for the same semantic attribute. Ultimately, we need to map each of
2410 // these to a single AttributeList::Kind value, but the StringMatcher
2411 // class cannot handle duplicate match strings. So we generate a list of
2412 // string to match based on the syntax, and emit multiple string matchers
2413 // depending on the syntax used.
Aaron Ballman64e69862013-12-15 13:05:48 +00002414 std::string AttrName;
2415 if (Attr.isSubClassOf("TargetSpecificAttr") &&
2416 !Attr.isValueUnset("ParseKind")) {
2417 AttrName = Attr.getValueAsString("ParseKind");
2418 if (Seen.find(AttrName) != Seen.end())
2419 continue;
2420 Seen.insert(AttrName);
2421 } else
2422 AttrName = NormalizeAttrName(StringRef(Attr.getName())).str();
2423
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002424 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002425 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
Michael Han4a045172012-03-07 00:12:16 +00002426 E = Spellings.end(); I != E; ++I) {
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002427 std::string RawSpelling = (*I)->getValueAsString("Name");
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002428 std::vector<StringMatcher::StringPair> *Matches = 0;
2429 std::string Spelling, Variety = (*I)->getValueAsString("Variety");
2430 if (Variety == "CXX11") {
2431 Matches = &CXX11;
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002432 Spelling += (*I)->getValueAsString("Namespace");
2433 Spelling += "::";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002434 } else if (Variety == "GNU")
2435 Matches = &GNU;
2436 else if (Variety == "Declspec")
2437 Matches = &Declspec;
2438 else if (Variety == "Keyword")
2439 Matches = &Keywords;
Alexis Hunta0e54d42012-06-18 16:13:52 +00002440
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002441 assert(Matches && "Unsupported spelling variety found");
2442
2443 Spelling += NormalizeAttrSpelling(RawSpelling);
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00002444 if (SemaHandler)
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002445 Matches->push_back(StringMatcher::StringPair(Spelling,
2446 "return AttributeList::AT_" + AttrName + ";"));
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00002447 else
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002448 Matches->push_back(StringMatcher::StringPair(Spelling,
2449 "return AttributeList::IgnoredAttribute;"));
Michael Han4a045172012-03-07 00:12:16 +00002450 }
2451 }
2452 }
Douglas Gregor377f99b2012-05-02 17:33:51 +00002453
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002454 OS << "static AttributeList::Kind getAttrKind(StringRef Name, ";
2455 OS << "AttributeList::Syntax Syntax) {\n";
2456 OS << " if (AttributeList::AS_GNU == Syntax) {\n";
2457 StringMatcher("Name", GNU, OS).Emit();
2458 OS << " } else if (AttributeList::AS_Declspec == Syntax) {\n";
2459 StringMatcher("Name", Declspec, OS).Emit();
2460 OS << " } else if (AttributeList::AS_CXX11 == Syntax) {\n";
2461 StringMatcher("Name", CXX11, OS).Emit();
2462 OS << " } else if (AttributeList::AS_Keyword == Syntax) {\n";
2463 StringMatcher("Name", Keywords, OS).Emit();
2464 OS << " }\n";
2465 OS << " return AttributeList::UnknownAttribute;\n"
Douglas Gregor377f99b2012-05-02 17:33:51 +00002466 << "}\n";
Michael Han4a045172012-03-07 00:12:16 +00002467}
2468
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00002469// Emits the code to dump an attribute.
2470void EmitClangAttrDump(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002471 emitSourceFileHeader("Attribute dumper", OS);
2472
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00002473 OS <<
2474 " switch (A->getKind()) {\n"
2475 " default:\n"
2476 " llvm_unreachable(\"Unknown attribute kind!\");\n"
2477 " break;\n";
2478 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
2479 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
2480 I != E; ++I) {
2481 Record &R = **I;
2482 if (!R.getValueAsBit("ASTNode"))
2483 continue;
2484 OS << " case attr::" << R.getName() << ": {\n";
Aaron Ballmanbc909612014-01-22 21:51:20 +00002485
2486 // If the attribute has a semantically-meaningful name (which is determined
2487 // by whether there is a Spelling enumeration for it), then write out the
2488 // spelling used for the attribute.
2489 std::vector<Record *> Spellings = R.getValueAsListOfDefs("Spellings");
2490 if (Spellings.size() > 1 && !SpellingNamesAreCommon(Spellings))
2491 OS << " OS << \" \" << A->getSpelling();\n";
2492
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00002493 Args = R.getValueAsListOfDefs("Args");
2494 if (!Args.empty()) {
2495 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
2496 << "Attr>(A);\n";
2497 for (std::vector<Record*>::iterator I = Args.begin(), E = Args.end();
2498 I != E; ++I)
2499 createArgument(**I, R.getName())->writeDump(OS);
Richard Trieude5cc7d2013-01-31 01:44:26 +00002500
2501 // Code for detecting the last child.
2502 OS << " bool OldMoreChildren = hasMoreChildren();\n";
2503 OS << " bool MoreChildren = OldMoreChildren;\n";
2504
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00002505 for (std::vector<Record*>::iterator I = Args.begin(), E = Args.end();
Richard Trieude5cc7d2013-01-31 01:44:26 +00002506 I != E; ++I) {
2507 // More code for detecting the last child.
2508 OS << " MoreChildren = OldMoreChildren";
2509 for (std::vector<Record*>::iterator Next = I + 1; Next != E; ++Next) {
2510 OS << " || ";
2511 createArgument(**Next, R.getName())->writeHasChildren(OS);
2512 }
2513 OS << ";\n";
2514 OS << " setMoreChildren(MoreChildren);\n";
2515
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00002516 createArgument(**I, R.getName())->writeDumpChildren(OS);
Richard Trieude5cc7d2013-01-31 01:44:26 +00002517 }
2518
2519 // Reset the last child.
2520 OS << " setMoreChildren(OldMoreChildren);\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00002521 }
2522 OS <<
2523 " break;\n"
2524 " }\n";
2525 }
2526 OS << " }\n";
2527}
2528
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002529} // end namespace clang