1 //===- 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
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/TableGen/Record.h"
17 #include "llvm/TableGen/StringMatcher.h"
18 #include "llvm/TableGen/TableGenBackend.h"
19 #include <algorithm>
20 #include <cctype>
21
22 using namespace llvm;
23
24 static const std::vector<StringRef>
getValueAsListOfStrings(Record & R,StringRef FieldName)25 getValueAsListOfStrings(Record &R, StringRef FieldName) {
26 ListInit *List = R.getValueAsListInit(FieldName);
27 assert (List && "Got a null ListInit");
28
29 std::vector<StringRef> Strings;
30 Strings.reserve(List->getSize());
31
32 for (ListInit::const_iterator i = List->begin(), e = List->end();
33 i != e;
34 ++i) {
35 assert(*i && "Got a null element in a ListInit");
36 if (StringInit *S = dyn_cast<StringInit>(*i))
37 Strings.push_back(S->getValue());
38 else
39 assert(false && "Got a non-string, non-code element in a ListInit");
40 }
41
42 return Strings;
43 }
44
ReadPCHRecord(StringRef type)45 static std::string ReadPCHRecord(StringRef type) {
46 return StringSwitch<std::string>(type)
47 .EndsWith("Decl *", "GetLocalDeclAs<"
48 + std::string(type, 0, type.size()-1) + ">(F, Record[Idx++])")
49 .Case("QualType", "getLocalType(F, Record[Idx++])")
50 .Case("Expr *", "ReadExpr(F)")
51 .Case("IdentifierInfo *", "GetIdentifierInfo(F, Record, Idx)")
52 .Case("SourceLocation", "ReadSourceLocation(F, Record, Idx)")
53 .Default("Record[Idx++]");
54 }
55
56 // Assumes that the way to get the value is SA->getname()
WritePCHRecord(StringRef type,StringRef name)57 static std::string WritePCHRecord(StringRef type, StringRef name) {
58 return StringSwitch<std::string>(type)
59 .EndsWith("Decl *", "AddDeclRef(" + std::string(name) +
60 ", Record);\n")
61 .Case("QualType", "AddTypeRef(" + std::string(name) + ", Record);\n")
62 .Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
63 .Case("IdentifierInfo *",
64 "AddIdentifierRef(" + std::string(name) + ", Record);\n")
65 .Case("SourceLocation",
66 "AddSourceLocation(" + std::string(name) + ", Record);\n")
67 .Default("Record.push_back(" + std::string(name) + ");\n");
68 }
69
70 // Normalize attribute name by removing leading and trailing
71 // underscores. For example, __foo, foo__, __foo__ would
72 // become foo.
NormalizeAttrName(StringRef AttrName)73 static StringRef NormalizeAttrName(StringRef AttrName) {
74 if (AttrName.startswith("__"))
75 AttrName = AttrName.substr(2, AttrName.size());
76
77 if (AttrName.endswith("__"))
78 AttrName = AttrName.substr(0, AttrName.size() - 2);
79
80 return AttrName;
81 }
82
83 // Normalize attribute spelling only if the spelling has both leading
84 // and trailing underscores. For example, __ms_struct__ will be
85 // normalized to "ms_struct"; __cdecl will remain intact.
NormalizeAttrSpelling(StringRef AttrSpelling)86 static StringRef NormalizeAttrSpelling(StringRef AttrSpelling) {
87 if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) {
88 AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4);
89 }
90
91 return AttrSpelling;
92 }
93
94 namespace {
95 class Argument {
96 std::string lowerName, upperName;
97 StringRef attrName;
98
99 public:
Argument(Record & Arg,StringRef Attr)100 Argument(Record &Arg, StringRef Attr)
101 : lowerName(Arg.getValueAsString("Name")), upperName(lowerName),
102 attrName(Attr) {
103 if (!lowerName.empty()) {
104 lowerName[0] = std::tolower(lowerName[0]);
105 upperName[0] = std::toupper(upperName[0]);
106 }
107 }
~Argument()108 virtual ~Argument() {}
109
getLowerName() const110 StringRef getLowerName() const { return lowerName; }
getUpperName() const111 StringRef getUpperName() const { return upperName; }
getAttrName() const112 StringRef getAttrName() const { return attrName; }
113
114 // These functions print the argument contents formatted in different ways.
115 virtual void writeAccessors(raw_ostream &OS) const = 0;
writeAccessorDefinitions(raw_ostream & OS) const116 virtual void writeAccessorDefinitions(raw_ostream &OS) const {}
117 virtual void writeCloneArgs(raw_ostream &OS) const = 0;
118 virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0;
writeTemplateInstantiation(raw_ostream & OS) const119 virtual void writeTemplateInstantiation(raw_ostream &OS) const {}
writeCtorBody(raw_ostream & OS) const120 virtual void writeCtorBody(raw_ostream &OS) const {}
121 virtual void writeCtorInitializers(raw_ostream &OS) const = 0;
122 virtual void writeCtorParameters(raw_ostream &OS) const = 0;
123 virtual void writeDeclarations(raw_ostream &OS) const = 0;
124 virtual void writePCHReadArgs(raw_ostream &OS) const = 0;
125 virtual void writePCHReadDecls(raw_ostream &OS) const = 0;
126 virtual void writePCHWrite(raw_ostream &OS) const = 0;
127 virtual void writeValue(raw_ostream &OS) const = 0;
128 virtual void writeDump(raw_ostream &OS) const = 0;
writeDumpChildren(raw_ostream & OS) const129 virtual void writeDumpChildren(raw_ostream &OS) const {}
writeHasChildren(raw_ostream & OS) const130 virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; }
131 };
132
133 class SimpleArgument : public Argument {
134 std::string type;
135
136 public:
SimpleArgument(Record & Arg,StringRef Attr,std::string T)137 SimpleArgument(Record &Arg, StringRef Attr, std::string T)
138 : Argument(Arg, Attr), type(T)
139 {}
140
getType() const141 std::string getType() const { return type; }
142
writeAccessors(raw_ostream & OS) const143 void writeAccessors(raw_ostream &OS) const {
144 OS << " " << type << " get" << getUpperName() << "() const {\n";
145 OS << " return " << getLowerName() << ";\n";
146 OS << " }";
147 }
writeCloneArgs(raw_ostream & OS) const148 void writeCloneArgs(raw_ostream &OS) const {
149 OS << getLowerName();
150 }
writeTemplateInstantiationArgs(raw_ostream & OS) const151 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
152 OS << "A->get" << getUpperName() << "()";
153 }
writeCtorInitializers(raw_ostream & OS) const154 void writeCtorInitializers(raw_ostream &OS) const {
155 OS << getLowerName() << "(" << getUpperName() << ")";
156 }
writeCtorParameters(raw_ostream & OS) const157 void writeCtorParameters(raw_ostream &OS) const {
158 OS << type << " " << getUpperName();
159 }
writeDeclarations(raw_ostream & OS) const160 void writeDeclarations(raw_ostream &OS) const {
161 OS << type << " " << getLowerName() << ";";
162 }
writePCHReadDecls(raw_ostream & OS) const163 void writePCHReadDecls(raw_ostream &OS) const {
164 std::string read = ReadPCHRecord(type);
165 OS << " " << type << " " << getLowerName() << " = " << read << ";\n";
166 }
writePCHReadArgs(raw_ostream & OS) const167 void writePCHReadArgs(raw_ostream &OS) const {
168 OS << getLowerName();
169 }
writePCHWrite(raw_ostream & OS) const170 void writePCHWrite(raw_ostream &OS) const {
171 OS << " " << WritePCHRecord(type, "SA->get" +
172 std::string(getUpperName()) + "()");
173 }
writeValue(raw_ostream & OS) const174 void writeValue(raw_ostream &OS) const {
175 if (type == "FunctionDecl *") {
176 OS << "\" << get" << getUpperName() << "()->getNameInfo().getAsString() << \"";
177 } else if (type == "IdentifierInfo *") {
178 OS << "\" << get" << getUpperName() << "()->getName() << \"";
179 } else if (type == "QualType") {
180 OS << "\" << get" << getUpperName() << "().getAsString() << \"";
181 } else if (type == "SourceLocation") {
182 OS << "\" << get" << getUpperName() << "().getRawEncoding() << \"";
183 } else {
184 OS << "\" << get" << getUpperName() << "() << \"";
185 }
186 }
writeDump(raw_ostream & OS) const187 void writeDump(raw_ostream &OS) const {
188 if (type == "FunctionDecl *") {
189 OS << " OS << \" \";\n";
190 OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n";
191 } else if (type == "IdentifierInfo *") {
192 OS << " OS << \" \" << SA->get" << getUpperName()
193 << "()->getName();\n";
194 } else if (type == "QualType") {
195 OS << " OS << \" \" << SA->get" << getUpperName()
196 << "().getAsString();\n";
197 } else if (type == "SourceLocation") {
198 OS << " OS << \" \";\n";
199 OS << " SA->get" << getUpperName() << "().print(OS, *SM);\n";
200 } else if (type == "bool") {
201 OS << " if (SA->get" << getUpperName() << "()) OS << \" "
202 << getUpperName() << "\";\n";
203 } else if (type == "int" || type == "unsigned") {
204 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
205 } else {
206 llvm_unreachable("Unknown SimpleArgument type!");
207 }
208 }
209 };
210
211 class StringArgument : public Argument {
212 public:
StringArgument(Record & Arg,StringRef Attr)213 StringArgument(Record &Arg, StringRef Attr)
214 : Argument(Arg, Attr)
215 {}
216
writeAccessors(raw_ostream & OS) const217 void writeAccessors(raw_ostream &OS) const {
218 OS << " llvm::StringRef get" << getUpperName() << "() const {\n";
219 OS << " return llvm::StringRef(" << getLowerName() << ", "
220 << getLowerName() << "Length);\n";
221 OS << " }\n";
222 OS << " unsigned get" << getUpperName() << "Length() const {\n";
223 OS << " return " << getLowerName() << "Length;\n";
224 OS << " }\n";
225 OS << " void set" << getUpperName()
226 << "(ASTContext &C, llvm::StringRef S) {\n";
227 OS << " " << getLowerName() << "Length = S.size();\n";
228 OS << " this->" << getLowerName() << " = new (C, 1) char ["
229 << getLowerName() << "Length];\n";
230 OS << " std::memcpy(this->" << getLowerName() << ", S.data(), "
231 << getLowerName() << "Length);\n";
232 OS << " }";
233 }
writeCloneArgs(raw_ostream & OS) const234 void writeCloneArgs(raw_ostream &OS) const {
235 OS << "get" << getUpperName() << "()";
236 }
writeTemplateInstantiationArgs(raw_ostream & OS) const237 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
238 OS << "A->get" << getUpperName() << "()";
239 }
writeCtorBody(raw_ostream & OS) const240 void writeCtorBody(raw_ostream &OS) const {
241 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
242 << ".data(), " << getLowerName() << "Length);";
243 }
writeCtorInitializers(raw_ostream & OS) const244 void writeCtorInitializers(raw_ostream &OS) const {
245 OS << getLowerName() << "Length(" << getUpperName() << ".size()),"
246 << getLowerName() << "(new (Ctx, 1) char[" << getLowerName()
247 << "Length])";
248 }
writeCtorParameters(raw_ostream & OS) const249 void writeCtorParameters(raw_ostream &OS) const {
250 OS << "llvm::StringRef " << getUpperName();
251 }
writeDeclarations(raw_ostream & OS) const252 void writeDeclarations(raw_ostream &OS) const {
253 OS << "unsigned " << getLowerName() << "Length;\n";
254 OS << "char *" << getLowerName() << ";";
255 }
writePCHReadDecls(raw_ostream & OS) const256 void writePCHReadDecls(raw_ostream &OS) const {
257 OS << " std::string " << getLowerName()
258 << "= ReadString(Record, Idx);\n";
259 }
writePCHReadArgs(raw_ostream & OS) const260 void writePCHReadArgs(raw_ostream &OS) const {
261 OS << getLowerName();
262 }
writePCHWrite(raw_ostream & OS) const263 void writePCHWrite(raw_ostream &OS) const {
264 OS << " AddString(SA->get" << getUpperName() << "(), Record);\n";
265 }
writeValue(raw_ostream & OS) const266 void writeValue(raw_ostream &OS) const {
267 OS << "\\\"\" << get" << getUpperName() << "() << \"\\\"";
268 }
writeDump(raw_ostream & OS) const269 void writeDump(raw_ostream &OS) const {
270 OS << " OS << \" \\\"\" << SA->get" << getUpperName()
271 << "() << \"\\\"\";\n";
272 }
273 };
274
275 class AlignedArgument : public Argument {
276 public:
AlignedArgument(Record & Arg,StringRef Attr)277 AlignedArgument(Record &Arg, StringRef Attr)
278 : Argument(Arg, Attr)
279 {}
280
writeAccessors(raw_ostream & OS) const281 void writeAccessors(raw_ostream &OS) const {
282 OS << " bool is" << getUpperName() << "Dependent() const;\n";
283
284 OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n";
285
286 OS << " bool is" << getUpperName() << "Expr() const {\n";
287 OS << " return is" << getLowerName() << "Expr;\n";
288 OS << " }\n";
289
290 OS << " Expr *get" << getUpperName() << "Expr() const {\n";
291 OS << " assert(is" << getLowerName() << "Expr);\n";
292 OS << " return " << getLowerName() << "Expr;\n";
293 OS << " }\n";
294
295 OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n";
296 OS << " assert(!is" << getLowerName() << "Expr);\n";
297 OS << " return " << getLowerName() << "Type;\n";
298 OS << " }";
299 }
writeAccessorDefinitions(raw_ostream & OS) const300 void writeAccessorDefinitions(raw_ostream &OS) const {
301 OS << "bool " << getAttrName() << "Attr::is" << getUpperName()
302 << "Dependent() const {\n";
303 OS << " if (is" << getLowerName() << "Expr)\n";
304 OS << " return " << getLowerName() << "Expr && (" << getLowerName()
305 << "Expr->isValueDependent() || " << getLowerName()
306 << "Expr->isTypeDependent());\n";
307 OS << " else\n";
308 OS << " return " << getLowerName()
309 << "Type->getType()->isDependentType();\n";
310 OS << "}\n";
311
312 // FIXME: Do not do the calculation here
313 // FIXME: Handle types correctly
314 // A null pointer means maximum alignment
315 // FIXME: Load the platform-specific maximum alignment, rather than
316 // 16, the x86 max.
317 OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
318 << "(ASTContext &Ctx) const {\n";
319 OS << " assert(!is" << getUpperName() << "Dependent());\n";
320 OS << " if (is" << getLowerName() << "Expr)\n";
321 OS << " return (" << getLowerName() << "Expr ? " << getLowerName()
322 << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue() : 16)"
323 << "* Ctx.getCharWidth();\n";
324 OS << " else\n";
325 OS << " return 0; // FIXME\n";
326 OS << "}\n";
327 }
writeCloneArgs(raw_ostream & OS) const328 void writeCloneArgs(raw_ostream &OS) const {
329 OS << "is" << getLowerName() << "Expr, is" << getLowerName()
330 << "Expr ? static_cast<void*>(" << getLowerName()
331 << "Expr) : " << getLowerName()
332 << "Type";
333 }
writeTemplateInstantiationArgs(raw_ostream & OS) const334 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
335 // FIXME: move the definition in Sema::InstantiateAttrs to here.
336 // In the meantime, aligned attributes are cloned.
337 }
writeCtorBody(raw_ostream & OS) const338 void writeCtorBody(raw_ostream &OS) const {
339 OS << " if (is" << getLowerName() << "Expr)\n";
340 OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>("
341 << getUpperName() << ");\n";
342 OS << " else\n";
343 OS << " " << getLowerName()
344 << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName()
345 << ");";
346 }
writeCtorInitializers(raw_ostream & OS) const347 void writeCtorInitializers(raw_ostream &OS) const {
348 OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)";
349 }
writeCtorParameters(raw_ostream & OS) const350 void writeCtorParameters(raw_ostream &OS) const {
351 OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName();
352 }
writeDeclarations(raw_ostream & OS) const353 void writeDeclarations(raw_ostream &OS) const {
354 OS << "bool is" << getLowerName() << "Expr;\n";
355 OS << "union {\n";
356 OS << "Expr *" << getLowerName() << "Expr;\n";
357 OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
358 OS << "};";
359 }
writePCHReadArgs(raw_ostream & OS) const360 void writePCHReadArgs(raw_ostream &OS) const {
361 OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr";
362 }
writePCHReadDecls(raw_ostream & OS) const363 void writePCHReadDecls(raw_ostream &OS) const {
364 OS << " bool is" << getLowerName() << "Expr = Record[Idx++];\n";
365 OS << " void *" << getLowerName() << "Ptr;\n";
366 OS << " if (is" << getLowerName() << "Expr)\n";
367 OS << " " << getLowerName() << "Ptr = ReadExpr(F);\n";
368 OS << " else\n";
369 OS << " " << getLowerName()
370 << "Ptr = GetTypeSourceInfo(F, Record, Idx);\n";
371 }
writePCHWrite(raw_ostream & OS) const372 void writePCHWrite(raw_ostream &OS) const {
373 OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n";
374 OS << " if (SA->is" << getUpperName() << "Expr())\n";
375 OS << " AddStmt(SA->get" << getUpperName() << "Expr());\n";
376 OS << " else\n";
377 OS << " AddTypeSourceInfo(SA->get" << getUpperName()
378 << "Type(), Record);\n";
379 }
writeValue(raw_ostream & OS) const380 void writeValue(raw_ostream &OS) const {
381 OS << "\";\n"
382 << " " << getLowerName() << "Expr->printPretty(OS, 0, Policy);\n"
383 << " OS << \"";
384 }
writeDump(raw_ostream & OS) const385 void writeDump(raw_ostream &OS) const {
386 }
writeDumpChildren(raw_ostream & OS) const387 void writeDumpChildren(raw_ostream &OS) const {
388 OS << " if (SA->is" << getUpperName() << "Expr()) {\n";
389 OS << " lastChild();\n";
390 OS << " dumpStmt(SA->get" << getUpperName() << "Expr());\n";
391 OS << " } else\n";
392 OS << " dumpType(SA->get" << getUpperName()
393 << "Type()->getType());\n";
394 }
writeHasChildren(raw_ostream & OS) const395 void writeHasChildren(raw_ostream &OS) const {
396 OS << "SA->is" << getUpperName() << "Expr()";
397 }
398 };
399
400 class VariadicArgument : public Argument {
401 std::string type;
402
403 public:
VariadicArgument(Record & Arg,StringRef Attr,std::string T)404 VariadicArgument(Record &Arg, StringRef Attr, std::string T)
405 : Argument(Arg, Attr), type(T)
406 {}
407
getType() const408 std::string getType() const { return type; }
409
writeAccessors(raw_ostream & OS) const410 void writeAccessors(raw_ostream &OS) const {
411 OS << " typedef " << type << "* " << getLowerName() << "_iterator;\n";
412 OS << " " << getLowerName() << "_iterator " << getLowerName()
413 << "_begin() const {\n";
414 OS << " return " << getLowerName() << ";\n";
415 OS << " }\n";
416 OS << " " << getLowerName() << "_iterator " << getLowerName()
417 << "_end() const {\n";
418 OS << " return " << getLowerName() << " + " << getLowerName()
419 << "Size;\n";
420 OS << " }\n";
421 OS << " unsigned " << getLowerName() << "_size() const {\n"
422 << " return " << getLowerName() << "Size;\n";
423 OS << " }";
424 }
writeCloneArgs(raw_ostream & OS) const425 void writeCloneArgs(raw_ostream &OS) const {
426 OS << getLowerName() << ", " << getLowerName() << "Size";
427 }
writeTemplateInstantiationArgs(raw_ostream & OS) const428 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
429 // This isn't elegant, but we have to go through public methods...
430 OS << "A->" << getLowerName() << "_begin(), "
431 << "A->" << getLowerName() << "_size()";
432 }
writeCtorBody(raw_ostream & OS) const433 void writeCtorBody(raw_ostream &OS) const {
434 // FIXME: memcpy is not safe on non-trivial types.
435 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
436 << ", " << getLowerName() << "Size * sizeof(" << getType() << "));\n";
437 }
writeCtorInitializers(raw_ostream & OS) const438 void writeCtorInitializers(raw_ostream &OS) const {
439 OS << getLowerName() << "Size(" << getUpperName() << "Size), "
440 << getLowerName() << "(new (Ctx, 16) " << getType() << "["
441 << getLowerName() << "Size])";
442 }
writeCtorParameters(raw_ostream & OS) const443 void writeCtorParameters(raw_ostream &OS) const {
444 OS << getType() << " *" << getUpperName() << ", unsigned "
445 << getUpperName() << "Size";
446 }
writeDeclarations(raw_ostream & OS) const447 void writeDeclarations(raw_ostream &OS) const {
448 OS << " unsigned " << getLowerName() << "Size;\n";
449 OS << " " << getType() << " *" << getLowerName() << ";";
450 }
writePCHReadDecls(raw_ostream & OS) const451 void writePCHReadDecls(raw_ostream &OS) const {
452 OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
453 OS << " SmallVector<" << type << ", 4> " << getLowerName()
454 << ";\n";
455 OS << " " << getLowerName() << ".reserve(" << getLowerName()
456 << "Size);\n";
457 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
458
459 std::string read = ReadPCHRecord(type);
460 OS << " " << getLowerName() << ".push_back(" << read << ");\n";
461 }
writePCHReadArgs(raw_ostream & OS) const462 void writePCHReadArgs(raw_ostream &OS) const {
463 OS << getLowerName() << ".data(), " << getLowerName() << "Size";
464 }
writePCHWrite(raw_ostream & OS) const465 void writePCHWrite(raw_ostream &OS) const{
466 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
467 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
468 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
469 << getLowerName() << "_end(); i != e; ++i)\n";
470 OS << " " << WritePCHRecord(type, "(*i)");
471 }
writeValue(raw_ostream & OS) const472 void writeValue(raw_ostream &OS) const {
473 OS << "\";\n";
474 OS << " bool isFirst = true;\n"
475 << " for (" << getAttrName() << "Attr::" << getLowerName()
476 << "_iterator i = " << getLowerName() << "_begin(), e = "
477 << getLowerName() << "_end(); i != e; ++i) {\n"
478 << " if (isFirst) isFirst = false;\n"
479 << " else OS << \", \";\n"
480 << " OS << *i;\n"
481 << " }\n";
482 OS << " OS << \"";
483 }
writeDump(raw_ostream & OS) const484 void writeDump(raw_ostream &OS) const {
485 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
486 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
487 << getLowerName() << "_end(); I != E; ++I)\n";
488 OS << " OS << \" \" << *I;\n";
489 }
490 };
491
492 class EnumArgument : public Argument {
493 std::string type;
494 std::vector<StringRef> values, enums, uniques;
495 public:
EnumArgument(Record & Arg,StringRef Attr)496 EnumArgument(Record &Arg, StringRef Attr)
497 : Argument(Arg, Attr), type(Arg.getValueAsString("Type")),
498 values(getValueAsListOfStrings(Arg, "Values")),
499 enums(getValueAsListOfStrings(Arg, "Enums")),
500 uniques(enums)
501 {
502 // Calculate the various enum values
503 std::sort(uniques.begin(), uniques.end());
504 uniques.erase(std::unique(uniques.begin(), uniques.end()), uniques.end());
505 // FIXME: Emit a proper error
506 assert(!uniques.empty());
507 }
508
writeAccessors(raw_ostream & OS) const509 void writeAccessors(raw_ostream &OS) const {
510 OS << " " << type << " get" << getUpperName() << "() const {\n";
511 OS << " return " << getLowerName() << ";\n";
512 OS << " }";
513 }
writeCloneArgs(raw_ostream & OS) const514 void writeCloneArgs(raw_ostream &OS) const {
515 OS << getLowerName();
516 }
writeTemplateInstantiationArgs(raw_ostream & OS) const517 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
518 OS << "A->get" << getUpperName() << "()";
519 }
writeCtorInitializers(raw_ostream & OS) const520 void writeCtorInitializers(raw_ostream &OS) const {
521 OS << getLowerName() << "(" << getUpperName() << ")";
522 }
writeCtorParameters(raw_ostream & OS) const523 void writeCtorParameters(raw_ostream &OS) const {
524 OS << type << " " << getUpperName();
525 }
writeDeclarations(raw_ostream & OS) const526 void writeDeclarations(raw_ostream &OS) const {
527 std::vector<StringRef>::const_iterator i = uniques.begin(),
528 e = uniques.end();
529 // The last one needs to not have a comma.
530 --e;
531
532 OS << "public:\n";
533 OS << " enum " << type << " {\n";
534 for (; i != e; ++i)
535 OS << " " << *i << ",\n";
536 OS << " " << *e << "\n";
537 OS << " };\n";
538 OS << "private:\n";
539 OS << " " << type << " " << getLowerName() << ";";
540 }
writePCHReadDecls(raw_ostream & OS) const541 void writePCHReadDecls(raw_ostream &OS) const {
542 OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName()
543 << "(static_cast<" << getAttrName() << "Attr::" << type
544 << ">(Record[Idx++]));\n";
545 }
writePCHReadArgs(raw_ostream & OS) const546 void writePCHReadArgs(raw_ostream &OS) const {
547 OS << getLowerName();
548 }
writePCHWrite(raw_ostream & OS) const549 void writePCHWrite(raw_ostream &OS) const {
550 OS << "Record.push_back(SA->get" << getUpperName() << "());\n";
551 }
writeValue(raw_ostream & OS) const552 void writeValue(raw_ostream &OS) const {
553 OS << "\" << get" << getUpperName() << "() << \"";
554 }
writeDump(raw_ostream & OS) const555 void writeDump(raw_ostream &OS) const {
556 OS << " switch(SA->get" << getUpperName() << "()) {\n";
557 for (std::vector<StringRef>::const_iterator I = uniques.begin(),
558 E = uniques.end(); I != E; ++I) {
559 OS << " case " << getAttrName() << "Attr::" << *I << ":\n";
560 OS << " OS << \" " << *I << "\";\n";
561 OS << " break;\n";
562 }
563 OS << " }\n";
564 }
565 };
566
567 class VersionArgument : public Argument {
568 public:
VersionArgument(Record & Arg,StringRef Attr)569 VersionArgument(Record &Arg, StringRef Attr)
570 : Argument(Arg, Attr)
571 {}
572
writeAccessors(raw_ostream & OS) const573 void writeAccessors(raw_ostream &OS) const {
574 OS << " VersionTuple get" << getUpperName() << "() const {\n";
575 OS << " return " << getLowerName() << ";\n";
576 OS << " }\n";
577 OS << " void set" << getUpperName()
578 << "(ASTContext &C, VersionTuple V) {\n";
579 OS << " " << getLowerName() << " = V;\n";
580 OS << " }";
581 }
writeCloneArgs(raw_ostream & OS) const582 void writeCloneArgs(raw_ostream &OS) const {
583 OS << "get" << getUpperName() << "()";
584 }
writeTemplateInstantiationArgs(raw_ostream & OS) const585 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
586 OS << "A->get" << getUpperName() << "()";
587 }
writeCtorBody(raw_ostream & OS) const588 void writeCtorBody(raw_ostream &OS) const {
589 }
writeCtorInitializers(raw_ostream & OS) const590 void writeCtorInitializers(raw_ostream &OS) const {
591 OS << getLowerName() << "(" << getUpperName() << ")";
592 }
writeCtorParameters(raw_ostream & OS) const593 void writeCtorParameters(raw_ostream &OS) const {
594 OS << "VersionTuple " << getUpperName();
595 }
writeDeclarations(raw_ostream & OS) const596 void writeDeclarations(raw_ostream &OS) const {
597 OS << "VersionTuple " << getLowerName() << ";\n";
598 }
writePCHReadDecls(raw_ostream & OS) const599 void writePCHReadDecls(raw_ostream &OS) const {
600 OS << " VersionTuple " << getLowerName()
601 << "= ReadVersionTuple(Record, Idx);\n";
602 }
writePCHReadArgs(raw_ostream & OS) const603 void writePCHReadArgs(raw_ostream &OS) const {
604 OS << getLowerName();
605 }
writePCHWrite(raw_ostream & OS) const606 void writePCHWrite(raw_ostream &OS) const {
607 OS << " AddVersionTuple(SA->get" << getUpperName() << "(), Record);\n";
608 }
writeValue(raw_ostream & OS) const609 void writeValue(raw_ostream &OS) const {
610 OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";
611 }
writeDump(raw_ostream & OS) const612 void writeDump(raw_ostream &OS) const {
613 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
614 }
615 };
616
617 class ExprArgument : public SimpleArgument {
618 public:
ExprArgument(Record & Arg,StringRef Attr)619 ExprArgument(Record &Arg, StringRef Attr)
620 : SimpleArgument(Arg, Attr, "Expr *")
621 {}
622
writeTemplateInstantiationArgs(raw_ostream & OS) const623 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
624 OS << "tempInst" << getUpperName();
625 }
626
writeTemplateInstantiation(raw_ostream & OS) const627 void writeTemplateInstantiation(raw_ostream &OS) const {
628 OS << " " << getType() << " tempInst" << getUpperName() << ";\n";
629 OS << " {\n";
630 OS << " EnterExpressionEvaluationContext "
631 << "Unevaluated(S, Sema::Unevaluated);\n";
632 OS << " ExprResult " << "Result = S.SubstExpr("
633 << "A->get" << getUpperName() << "(), TemplateArgs);\n";
634 OS << " tempInst" << getUpperName() << " = "
635 << "Result.takeAs<Expr>();\n";
636 OS << " }\n";
637 }
638
writeDump(raw_ostream & OS) const639 void writeDump(raw_ostream &OS) const {
640 }
641
writeDumpChildren(raw_ostream & OS) const642 void writeDumpChildren(raw_ostream &OS) const {
643 OS << " lastChild();\n";
644 OS << " dumpStmt(SA->get" << getUpperName() << "());\n";
645 }
writeHasChildren(raw_ostream & OS) const646 void writeHasChildren(raw_ostream &OS) const { OS << "true"; }
647 };
648
649 class VariadicExprArgument : public VariadicArgument {
650 public:
VariadicExprArgument(Record & Arg,StringRef Attr)651 VariadicExprArgument(Record &Arg, StringRef Attr)
652 : VariadicArgument(Arg, Attr, "Expr *")
653 {}
654
writeTemplateInstantiationArgs(raw_ostream & OS) const655 void writeTemplateInstantiationArgs(raw_ostream &OS) const {
656 OS << "tempInst" << getUpperName() << ", "
657 << "A->" << getLowerName() << "_size()";
658 }
659
writeTemplateInstantiation(raw_ostream & OS) const660 void writeTemplateInstantiation(raw_ostream &OS) const {
661 OS << " " << getType() << " *tempInst" << getUpperName()
662 << " = new (C, 16) " << getType()
663 << "[A->" << getLowerName() << "_size()];\n";
664 OS << " {\n";
665 OS << " EnterExpressionEvaluationContext "
666 << "Unevaluated(S, Sema::Unevaluated);\n";
667 OS << " " << getType() << " *TI = tempInst" << getUpperName()
668 << ";\n";
669 OS << " " << getType() << " *I = A->" << getLowerName()
670 << "_begin();\n";
671 OS << " " << getType() << " *E = A->" << getLowerName()
672 << "_end();\n";
673 OS << " for (; I != E; ++I, ++TI) {\n";
674 OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n";
675 OS << " *TI = Result.takeAs<Expr>();\n";
676 OS << " }\n";
677 OS << " }\n";
678 }
679
writeDump(raw_ostream & OS) const680 void writeDump(raw_ostream &OS) const {
681 }
682
writeDumpChildren(raw_ostream & OS) const683 void writeDumpChildren(raw_ostream &OS) const {
684 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
685 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
686 << getLowerName() << "_end(); I != E; ++I) {\n";
687 OS << " if (I + 1 == E)\n";
688 OS << " lastChild();\n";
689 OS << " dumpStmt(*I);\n";
690 OS << " }\n";
691 }
692
writeHasChildren(raw_ostream & OS) const693 void writeHasChildren(raw_ostream &OS) const {
694 OS << "SA->" << getLowerName() << "_begin() != "
695 << "SA->" << getLowerName() << "_end()";
696 }
697 };
698 }
699
createArgument(Record & Arg,StringRef Attr,Record * Search=0)700 static Argument *createArgument(Record &Arg, StringRef Attr,
701 Record *Search = 0) {
702 if (!Search)
703 Search = &Arg;
704
705 Argument *Ptr = 0;
706 llvm::StringRef ArgName = Search->getName();
707
708 if (ArgName == "AlignedArgument") Ptr = new AlignedArgument(Arg, Attr);
709 else if (ArgName == "EnumArgument") Ptr = new EnumArgument(Arg, Attr);
710 else if (ArgName == "ExprArgument") Ptr = new ExprArgument(Arg, Attr);
711 else if (ArgName == "FunctionArgument")
712 Ptr = new SimpleArgument(Arg, Attr, "FunctionDecl *");
713 else if (ArgName == "IdentifierArgument")
714 Ptr = new SimpleArgument(Arg, Attr, "IdentifierInfo *");
715 else if (ArgName == "BoolArgument") Ptr = new SimpleArgument(Arg, Attr,
716 "bool");
717 else if (ArgName == "IntArgument") Ptr = new SimpleArgument(Arg, Attr, "int");
718 else if (ArgName == "StringArgument") Ptr = new StringArgument(Arg, Attr);
719 else if (ArgName == "TypeArgument")
720 Ptr = new SimpleArgument(Arg, Attr, "QualType");
721 else if (ArgName == "UnsignedArgument")
722 Ptr = new SimpleArgument(Arg, Attr, "unsigned");
723 else if (ArgName == "SourceLocArgument")
724 Ptr = new SimpleArgument(Arg, Attr, "SourceLocation");
725 else if (ArgName == "VariadicUnsignedArgument")
726 Ptr = new VariadicArgument(Arg, Attr, "unsigned");
727 else if (ArgName == "VariadicExprArgument")
728 Ptr = new VariadicExprArgument(Arg, Attr);
729 else if (ArgName == "VersionArgument")
730 Ptr = new VersionArgument(Arg, Attr);
731
732 if (!Ptr) {
733 std::vector<Record*> Bases = Search->getSuperClasses();
734 for (std::vector<Record*>::iterator i = Bases.begin(), e = Bases.end();
735 i != e; ++i) {
736 Ptr = createArgument(Arg, Attr, *i);
737 if (Ptr)
738 break;
739 }
740 }
741 return Ptr;
742 }
743
writeAvailabilityValue(raw_ostream & OS)744 static void writeAvailabilityValue(raw_ostream &OS) {
745 OS << "\" << getPlatform()->getName();\n"
746 << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n"
747 << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n"
748 << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n"
749 << " if (getUnavailable()) OS << \", unavailable\";\n"
750 << " OS << \"";
751 }
752
writePrettyPrintFunction(Record & R,std::vector<Argument * > & Args,raw_ostream & OS)753 static void writePrettyPrintFunction(Record &R, std::vector<Argument*> &Args,
754 raw_ostream &OS) {
755 std::vector<Record*> Spellings = R.getValueAsListOfDefs("Spellings");
756
757 OS << "void " << R.getName() << "Attr::printPretty("
758 << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n";
759
760 if (Spellings.size() == 0) {
761 OS << "}\n\n";
762 return;
763 }
764
765 OS <<
766 " switch (SpellingListIndex) {\n"
767 " default:\n"
768 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
769 " break;\n";
770
771 for (unsigned I = 0; I < Spellings.size(); ++ I) {
772 llvm::SmallString<16> Prefix;
773 llvm::SmallString<8> Suffix;
774 // The actual spelling of the name and namespace (if applicable)
775 // of an attribute without considering prefix and suffix.
776 llvm::SmallString<64> Spelling;
777 std::string Name = Spellings[I]->getValueAsString("Name");
778 std::string Variety = Spellings[I]->getValueAsString("Variety");
779
780 if (Variety == "GNU") {
781 Prefix = " __attribute__((";
782 Suffix = "))";
783 } else if (Variety == "CXX11") {
784 Prefix = " [[";
785 Suffix = "]]";
786 std::string Namespace = Spellings[I]->getValueAsString("Namespace");
787 if (Namespace != "") {
788 Spelling += Namespace;
789 Spelling += "::";
790 }
791 } else if (Variety == "Declspec") {
792 Prefix = " __declspec(";
793 Suffix = ")";
794 } else if (Variety == "Keyword") {
795 Prefix = " ";
796 Suffix = "";
797 } else {
798 llvm_unreachable("Unknown attribute syntax variety!");
799 }
800
801 Spelling += Name;
802
803 OS <<
804 " case " << I << " : {\n"
805 " OS << \"" + Prefix.str() + Spelling.str();
806
807 if (Args.size()) OS << "(";
808 if (Spelling == "availability") {
809 writeAvailabilityValue(OS);
810 } else {
811 for (std::vector<Argument*>::const_iterator I = Args.begin(),
812 E = Args.end(); I != E; ++ I) {
813 if (I != Args.begin()) OS << ", ";
814 (*I)->writeValue(OS);
815 }
816 }
817
818 if (Args.size()) OS << ")";
819 OS << Suffix.str() + "\";\n";
820
821 OS <<
822 " break;\n"
823 " }\n";
824 }
825
826 // End of the switch statement.
827 OS << "}\n";
828 // End of the print function.
829 OS << "}\n\n";
830 }
831
832 /// \brief Return the index of a spelling in a spelling list.
getSpellingListIndex(const std::vector<Record * > & SpellingList,const Record & Spelling)833 static unsigned getSpellingListIndex(const std::vector<Record*> &SpellingList,
834 const Record &Spelling) {
835 assert(SpellingList.size() && "Spelling list is empty!");
836
837 for (unsigned Index = 0; Index < SpellingList.size(); ++Index) {
838 Record *S = SpellingList[Index];
839 if (S->getValueAsString("Variety") != Spelling.getValueAsString("Variety"))
840 continue;
841 if (S->getValueAsString("Variety") == "CXX11" &&
842 S->getValueAsString("Namespace") !=
843 Spelling.getValueAsString("Namespace"))
844 continue;
845 if (S->getValueAsString("Name") != Spelling.getValueAsString("Name"))
846 continue;
847
848 return Index;
849 }
850
851 llvm_unreachable("Unknown spelling!");
852 }
853
writeAttrAccessorDefinition(Record & R,raw_ostream & OS)854 static void writeAttrAccessorDefinition(Record &R, raw_ostream &OS) {
855 std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors");
856 for (std::vector<Record*>::const_iterator I = Accessors.begin(),
857 E = Accessors.end(); I != E; ++I) {
858 Record *Accessor = *I;
859 std::string Name = Accessor->getValueAsString("Name");
860 std::vector<Record*> Spellings = Accessor->getValueAsListOfDefs(
861 "Spellings");
862 std::vector<Record*> SpellingList = R.getValueAsListOfDefs("Spellings");
863 assert(SpellingList.size() &&
864 "Attribute with empty spelling list can't have accessors!");
865
866 OS << " bool " << Name << "() const { return SpellingListIndex == ";
867 for (unsigned Index = 0; Index < Spellings.size(); ++Index) {
868 OS << getSpellingListIndex(SpellingList, *Spellings[Index]);
869 if (Index != Spellings.size() -1)
870 OS << " ||\n SpellingListIndex == ";
871 else
872 OS << "; }\n";
873 }
874 }
875 }
876
877 namespace clang {
878
879 // Emits the class definitions for attributes.
EmitClangAttrClass(RecordKeeper & Records,raw_ostream & OS)880 void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) {
881 emitSourceFileHeader("Attribute classes' definitions", OS);
882
883 OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
884 OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
885
886 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
887
888 for (std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end();
889 i != e; ++i) {
890 Record &R = **i;
891
892 if (!R.getValueAsBit("ASTNode"))
893 continue;
894
895 const std::vector<Record *> Supers = R.getSuperClasses();
896 assert(!Supers.empty() && "Forgot to specify a superclass for the attr");
897 std::string SuperName;
898 for (std::vector<Record *>::const_reverse_iterator I = Supers.rbegin(),
899 E = Supers.rend(); I != E; ++I) {
900 const Record &R = **I;
901 if (R.getName() != "TargetSpecificAttr" && SuperName.empty())
902 SuperName = R.getName();
903 }
904
905 OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
906
907 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
908 std::vector<Argument*> Args;
909 std::vector<Argument*>::iterator ai, ae;
910 Args.reserve(ArgRecords.size());
911
912 for (std::vector<Record*>::iterator ri = ArgRecords.begin(),
913 re = ArgRecords.end();
914 ri != re; ++ri) {
915 Record &ArgRecord = **ri;
916 Argument *Arg = createArgument(ArgRecord, R.getName());
917 assert(Arg);
918 Args.push_back(Arg);
919
920 Arg->writeDeclarations(OS);
921 OS << "\n\n";
922 }
923
924 ae = Args.end();
925
926 OS << "\n public:\n";
927 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
928
929 for (ai = Args.begin(); ai != ae; ++ai) {
930 OS << " , ";
931 (*ai)->writeCtorParameters(OS);
932 OS << "\n";
933 }
934
935 OS << " , ";
936 OS << "unsigned SI = 0\n";
937
938 OS << " )\n";
939 OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI)\n";
940
941 for (ai = Args.begin(); ai != ae; ++ai) {
942 OS << " , ";
943 (*ai)->writeCtorInitializers(OS);
944 OS << "\n";
945 }
946
947 OS << " {\n";
948
949 for (ai = Args.begin(); ai != ae; ++ai) {
950 (*ai)->writeCtorBody(OS);
951 OS << "\n";
952 }
953 OS << " }\n\n";
954
955 OS << " virtual " << R.getName() << "Attr *clone (ASTContext &C) const;\n";
956 OS << " virtual void printPretty(raw_ostream &OS,\n"
957 << " const PrintingPolicy &Policy) const;\n";
958
959 writeAttrAccessorDefinition(R, OS);
960
961 for (ai = Args.begin(); ai != ae; ++ai) {
962 (*ai)->writeAccessors(OS);
963 OS << "\n\n";
964 }
965
966 OS << R.getValueAsString("AdditionalMembers");
967 OS << "\n\n";
968
969 OS << " static bool classof(const Attr *A) { return A->getKind() == "
970 << "attr::" << R.getName() << "; }\n";
971
972 bool LateParsed = R.getValueAsBit("LateParsed");
973 OS << " virtual bool isLateParsed() const { return "
974 << LateParsed << "; }\n";
975
976 OS << "};\n\n";
977 }
978
979 OS << "#endif\n";
980 }
981
982 // Emits the all-arguments-are-expressions property for attributes.
EmitClangAttrExprArgsList(RecordKeeper & Records,raw_ostream & OS)983 void EmitClangAttrExprArgsList(RecordKeeper &Records, raw_ostream &OS) {
984 emitSourceFileHeader("llvm::StringSwitch code to match attributes with "
985 "expression arguments", OS);
986
987 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
988
989 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
990 I != E; ++I) {
991 Record &Attr = **I;
992
993 // Determine whether the first argument is something that is always
994 // an expression.
995 std::vector<Record *> Args = Attr.getValueAsListOfDefs("Args");
996 if (Args.empty() || Args[0]->getSuperClasses().empty())
997 continue;
998
999 // Check whether this is one of the argument kinds that implies an
1000 // expression.
1001 // FIXME: Aligned is weird.
1002 if (!llvm::StringSwitch<bool>(Args[0]->getSuperClasses().back()->getName())
1003 .Case("AlignedArgument", true)
1004 .Case("BoolArgument", true)
1005 .Case("DefaultIntArgument", true)
1006 .Case("IntArgument", true)
1007 .Case("ExprArgument", true)
1008 .Case("UnsignedArgument", true)
1009 .Case("VariadicUnsignedArgument", true)
1010 .Case("VariadicExprArgument", true)
1011 .Default(false))
1012 continue;
1013
1014 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
1015
1016 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
1017 E = Spellings.end(); I != E; ++I) {
1018 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", "
1019 << "true" << ")\n";
1020 }
1021 }
1022 }
1023
1024 // Emits the class method definitions for attributes.
EmitClangAttrImpl(RecordKeeper & Records,raw_ostream & OS)1025 void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
1026 emitSourceFileHeader("Attribute classes' member function definitions", OS);
1027
1028 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1029 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ri, re;
1030 std::vector<Argument*>::iterator ai, ae;
1031
1032 for (; i != e; ++i) {
1033 Record &R = **i;
1034
1035 if (!R.getValueAsBit("ASTNode"))
1036 continue;
1037
1038 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1039 std::vector<Argument*> Args;
1040 for (ri = ArgRecords.begin(), re = ArgRecords.end(); ri != re; ++ri)
1041 Args.push_back(createArgument(**ri, R.getName()));
1042
1043 for (ai = Args.begin(), ae = Args.end(); ai != ae; ++ai)
1044 (*ai)->writeAccessorDefinitions(OS);
1045
1046 OS << R.getName() << "Attr *" << R.getName()
1047 << "Attr::clone(ASTContext &C) const {\n";
1048 OS << " return new (C) " << R.getName() << "Attr(getLocation(), C";
1049 for (ai = Args.begin(); ai != ae; ++ai) {
1050 OS << ", ";
1051 (*ai)->writeCloneArgs(OS);
1052 }
1053 OS << ", getSpellingListIndex());\n}\n\n";
1054
1055 writePrettyPrintFunction(R, Args, OS);
1056 }
1057 }
1058
1059 } // end namespace clang
1060
EmitAttrList(raw_ostream & OS,StringRef Class,const std::vector<Record * > & AttrList)1061 static void EmitAttrList(raw_ostream &OS, StringRef Class,
1062 const std::vector<Record*> &AttrList) {
1063 std::vector<Record*>::const_iterator i = AttrList.begin(), e = AttrList.end();
1064
1065 if (i != e) {
1066 // Move the end iterator back to emit the last attribute.
1067 for(--e; i != e; ++i) {
1068 if (!(*i)->getValueAsBit("ASTNode"))
1069 continue;
1070
1071 OS << Class << "(" << (*i)->getName() << ")\n";
1072 }
1073
1074 OS << "LAST_" << Class << "(" << (*i)->getName() << ")\n\n";
1075 }
1076 }
1077
1078 namespace clang {
1079
1080 // Emits the enumeration list for attributes.
EmitClangAttrList(RecordKeeper & Records,raw_ostream & OS)1081 void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) {
1082 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
1083
1084 OS << "#ifndef LAST_ATTR\n";
1085 OS << "#define LAST_ATTR(NAME) ATTR(NAME)\n";
1086 OS << "#endif\n\n";
1087
1088 OS << "#ifndef INHERITABLE_ATTR\n";
1089 OS << "#define INHERITABLE_ATTR(NAME) ATTR(NAME)\n";
1090 OS << "#endif\n\n";
1091
1092 OS << "#ifndef LAST_INHERITABLE_ATTR\n";
1093 OS << "#define LAST_INHERITABLE_ATTR(NAME) INHERITABLE_ATTR(NAME)\n";
1094 OS << "#endif\n\n";
1095
1096 OS << "#ifndef INHERITABLE_PARAM_ATTR\n";
1097 OS << "#define INHERITABLE_PARAM_ATTR(NAME) ATTR(NAME)\n";
1098 OS << "#endif\n\n";
1099
1100 OS << "#ifndef LAST_INHERITABLE_PARAM_ATTR\n";
1101 OS << "#define LAST_INHERITABLE_PARAM_ATTR(NAME)"
1102 " INHERITABLE_PARAM_ATTR(NAME)\n";
1103 OS << "#endif\n\n";
1104
1105 OS << "#ifndef MS_INHERITANCE_ATTR\n";
1106 OS << "#define MS_INHERITANCE_ATTR(NAME) INHERITABLE_ATTR(NAME)\n";
1107 OS << "#endif\n\n";
1108
1109 OS << "#ifndef LAST_MS_INHERITANCE_ATTR\n";
1110 OS << "#define LAST_MS_INHERITANCE_ATTR(NAME)"
1111 " MS_INHERITANCE_ATTR(NAME)\n";
1112 OS << "#endif\n\n";
1113
1114 Record *InhClass = Records.getClass("InheritableAttr");
1115 Record *InhParamClass = Records.getClass("InheritableParamAttr");
1116 Record *MSInheritanceClass = Records.getClass("MSInheritanceAttr");
1117 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
1118 NonInhAttrs, InhAttrs, InhParamAttrs, MSInhAttrs;
1119 for (std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end();
1120 i != e; ++i) {
1121 if (!(*i)->getValueAsBit("ASTNode"))
1122 continue;
1123
1124 if ((*i)->isSubClassOf(InhParamClass))
1125 InhParamAttrs.push_back(*i);
1126 else if ((*i)->isSubClassOf(MSInheritanceClass))
1127 MSInhAttrs.push_back(*i);
1128 else if ((*i)->isSubClassOf(InhClass))
1129 InhAttrs.push_back(*i);
1130 else
1131 NonInhAttrs.push_back(*i);
1132 }
1133
1134 EmitAttrList(OS, "INHERITABLE_PARAM_ATTR", InhParamAttrs);
1135 EmitAttrList(OS, "MS_INHERITANCE_ATTR", MSInhAttrs);
1136 EmitAttrList(OS, "INHERITABLE_ATTR", InhAttrs);
1137 EmitAttrList(OS, "ATTR", NonInhAttrs);
1138
1139 OS << "#undef LAST_ATTR\n";
1140 OS << "#undef INHERITABLE_ATTR\n";
1141 OS << "#undef MS_INHERITANCE_ATTR\n";
1142 OS << "#undef LAST_INHERITABLE_ATTR\n";
1143 OS << "#undef LAST_INHERITABLE_PARAM_ATTR\n";
1144 OS << "#undef LAST_MS_INHERITANCE_ATTR\n";
1145 OS << "#undef ATTR\n";
1146 }
1147
1148 // Emits the code to read an attribute from a precompiled header.
EmitClangAttrPCHRead(RecordKeeper & Records,raw_ostream & OS)1149 void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) {
1150 emitSourceFileHeader("Attribute deserialization code", OS);
1151
1152 Record *InhClass = Records.getClass("InheritableAttr");
1153 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
1154 ArgRecords;
1155 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ai, ae;
1156 std::vector<Argument*> Args;
1157 std::vector<Argument*>::iterator ri, re;
1158
1159 OS << " switch (Kind) {\n";
1160 OS << " default:\n";
1161 OS << " assert(0 && \"Unknown attribute!\");\n";
1162 OS << " break;\n";
1163 for (; i != e; ++i) {
1164 Record &R = **i;
1165 if (!R.getValueAsBit("ASTNode"))
1166 continue;
1167
1168 OS << " case attr::" << R.getName() << ": {\n";
1169 if (R.isSubClassOf(InhClass))
1170 OS << " bool isInherited = Record[Idx++];\n";
1171 ArgRecords = R.getValueAsListOfDefs("Args");
1172 Args.clear();
1173 for (ai = ArgRecords.begin(), ae = ArgRecords.end(); ai != ae; ++ai) {
1174 Argument *A = createArgument(**ai, R.getName());
1175 Args.push_back(A);
1176 A->writePCHReadDecls(OS);
1177 }
1178 OS << " New = new (Context) " << R.getName() << "Attr(Range, Context";
1179 for (ri = Args.begin(), re = Args.end(); ri != re; ++ri) {
1180 OS << ", ";
1181 (*ri)->writePCHReadArgs(OS);
1182 }
1183 OS << ");\n";
1184 if (R.isSubClassOf(InhClass))
1185 OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
1186 OS << " break;\n";
1187 OS << " }\n";
1188 }
1189 OS << " }\n";
1190 }
1191
1192 // Emits the code to write an attribute to a precompiled header.
EmitClangAttrPCHWrite(RecordKeeper & Records,raw_ostream & OS)1193 void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) {
1194 emitSourceFileHeader("Attribute serialization code", OS);
1195
1196 Record *InhClass = Records.getClass("InheritableAttr");
1197 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
1198 std::vector<Record*>::iterator i = Attrs.begin(), e = Attrs.end(), ai, ae;
1199
1200 OS << " switch (A->getKind()) {\n";
1201 OS << " default:\n";
1202 OS << " llvm_unreachable(\"Unknown attribute kind!\");\n";
1203 OS << " break;\n";
1204 for (; i != e; ++i) {
1205 Record &R = **i;
1206 if (!R.getValueAsBit("ASTNode"))
1207 continue;
1208 OS << " case attr::" << R.getName() << ": {\n";
1209 Args = R.getValueAsListOfDefs("Args");
1210 if (R.isSubClassOf(InhClass) || !Args.empty())
1211 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
1212 << "Attr>(A);\n";
1213 if (R.isSubClassOf(InhClass))
1214 OS << " Record.push_back(SA->isInherited());\n";
1215 for (ai = Args.begin(), ae = Args.end(); ai != ae; ++ai)
1216 createArgument(**ai, R.getName())->writePCHWrite(OS);
1217 OS << " break;\n";
1218 OS << " }\n";
1219 }
1220 OS << " }\n";
1221 }
1222
1223 // Emits the list of spellings for attributes.
EmitClangAttrSpellingList(RecordKeeper & Records,raw_ostream & OS)1224 void EmitClangAttrSpellingList(RecordKeeper &Records, raw_ostream &OS) {
1225 emitSourceFileHeader("llvm::StringSwitch code to match all known attributes",
1226 OS);
1227
1228 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1229
1230 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
1231 Record &Attr = **I;
1232
1233 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
1234
1235 for (std::vector<Record*>::const_iterator I = Spellings.begin(), E = Spellings.end(); I != E; ++I) {
1236 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", true)\n";
1237 }
1238 }
1239
1240 }
1241
EmitClangAttrSpellingListIndex(RecordKeeper & Records,raw_ostream & OS)1242 void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) {
1243 emitSourceFileHeader("Code to translate different attribute spellings "
1244 "into internal identifiers", OS);
1245
1246 OS <<
1247 " unsigned Index = 0;\n"
1248 " switch (AttrKind) {\n"
1249 " default:\n"
1250 " llvm_unreachable(\"Unknown attribute kind!\");\n"
1251 " break;\n";
1252
1253 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1254 for (std::vector<Record*>::const_iterator I = Attrs.begin(), E = Attrs.end();
1255 I != E; ++I) {
1256 Record &R = **I;
1257 // We only care about attributes that participate in Sema checking, so
1258 // skip those attributes that are not able to make their way to Sema.
1259 if (!R.getValueAsBit("SemaHandler"))
1260 continue;
1261
1262 std::vector<Record*> Spellings = R.getValueAsListOfDefs("Spellings");
1263 // Each distinct spelling yields an attribute kind.
1264 if (R.getValueAsBit("DistinctSpellings")) {
1265 for (unsigned I = 0; I < Spellings.size(); ++ I) {
1266 OS <<
1267 " case AT_" << Spellings[I]->getValueAsString("Name") << ": \n"
1268 " Index = " << I << ";\n"
1269 " break;\n";
1270 }
1271 } else {
1272 OS << " case AT_" << R.getName() << " : {\n";
1273 for (unsigned I = 0; I < Spellings.size(); ++ I) {
1274 SmallString<16> Namespace;
1275 if (Spellings[I]->getValueAsString("Variety") == "CXX11")
1276 Namespace = Spellings[I]->getValueAsString("Namespace");
1277 else
1278 Namespace = "";
1279
1280 OS << " if (Name == \""
1281 << Spellings[I]->getValueAsString("Name") << "\" && "
1282 << "SyntaxUsed == "
1283 << StringSwitch<unsigned>(Spellings[I]->getValueAsString("Variety"))
1284 .Case("GNU", 0)
1285 .Case("CXX11", 1)
1286 .Case("Declspec", 2)
1287 .Case("Keyword", 3)
1288 .Default(0)
1289 << " && Scope == \"" << Namespace << "\")\n"
1290 << " return " << I << ";\n";
1291 }
1292
1293 OS << " break;\n";
1294 OS << " }\n";
1295 }
1296 }
1297
1298 OS << " }\n";
1299 OS << " return Index;\n";
1300 }
1301
1302 // Emits the LateParsed property for attributes.
EmitClangAttrLateParsedList(RecordKeeper & Records,raw_ostream & OS)1303 void EmitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) {
1304 emitSourceFileHeader("llvm::StringSwitch code to match late parsed "
1305 "attributes", OS);
1306
1307 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1308
1309 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1310 I != E; ++I) {
1311 Record &Attr = **I;
1312
1313 bool LateParsed = Attr.getValueAsBit("LateParsed");
1314
1315 if (LateParsed) {
1316 std::vector<Record*> Spellings =
1317 Attr.getValueAsListOfDefs("Spellings");
1318
1319 // FIXME: Handle non-GNU attributes
1320 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
1321 E = Spellings.end(); I != E; ++I) {
1322 if ((*I)->getValueAsString("Variety") != "GNU")
1323 continue;
1324 OS << ".Case(\"" << (*I)->getValueAsString("Name") << "\", "
1325 << LateParsed << ")\n";
1326 }
1327 }
1328 }
1329 }
1330
1331 // Emits code to instantiate dependent attributes on templates.
EmitClangAttrTemplateInstantiate(RecordKeeper & Records,raw_ostream & OS)1332 void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) {
1333 emitSourceFileHeader("Template instantiation code for attributes", OS);
1334
1335 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1336
1337 OS << "namespace clang {\n"
1338 << "namespace sema {\n\n"
1339 << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, "
1340 << "Sema &S,\n"
1341 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"
1342 << " switch (At->getKind()) {\n"
1343 << " default:\n"
1344 << " break;\n";
1345
1346 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1347 I != E; ++I) {
1348 Record &R = **I;
1349 if (!R.getValueAsBit("ASTNode"))
1350 continue;
1351
1352 OS << " case attr::" << R.getName() << ": {\n";
1353 bool ShouldClone = R.getValueAsBit("Clone");
1354
1355 if (!ShouldClone) {
1356 OS << " return NULL;\n";
1357 OS << " }\n";
1358 continue;
1359 }
1360
1361 OS << " const " << R.getName() << "Attr *A = cast<"
1362 << R.getName() << "Attr>(At);\n";
1363 bool TDependent = R.getValueAsBit("TemplateDependent");
1364
1365 if (!TDependent) {
1366 OS << " return A->clone(C);\n";
1367 OS << " }\n";
1368 continue;
1369 }
1370
1371 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1372 std::vector<Argument*> Args;
1373 std::vector<Argument*>::iterator ai, ae;
1374 Args.reserve(ArgRecords.size());
1375
1376 for (std::vector<Record*>::iterator ri = ArgRecords.begin(),
1377 re = ArgRecords.end();
1378 ri != re; ++ri) {
1379 Record &ArgRecord = **ri;
1380 Argument *Arg = createArgument(ArgRecord, R.getName());
1381 assert(Arg);
1382 Args.push_back(Arg);
1383 }
1384 ae = Args.end();
1385
1386 for (ai = Args.begin(); ai != ae; ++ai) {
1387 (*ai)->writeTemplateInstantiation(OS);
1388 }
1389 OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C";
1390 for (ai = Args.begin(); ai != ae; ++ai) {
1391 OS << ", ";
1392 (*ai)->writeTemplateInstantiationArgs(OS);
1393 }
1394 OS << ");\n }\n";
1395 }
1396 OS << " } // end switch\n"
1397 << " llvm_unreachable(\"Unknown attribute!\");\n"
1398 << " return 0;\n"
1399 << "}\n\n"
1400 << "} // end namespace sema\n"
1401 << "} // end namespace clang\n";
1402 }
1403
1404 // Emits the list of parsed attributes.
EmitClangAttrParsedAttrList(RecordKeeper & Records,raw_ostream & OS)1405 void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) {
1406 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
1407
1408 OS << "#ifndef PARSED_ATTR\n";
1409 OS << "#define PARSED_ATTR(NAME) NAME\n";
1410 OS << "#endif\n\n";
1411
1412 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1413
1414 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1415 I != E; ++I) {
1416 Record &Attr = **I;
1417
1418 bool SemaHandler = Attr.getValueAsBit("SemaHandler");
1419 bool DistinctSpellings = Attr.getValueAsBit("DistinctSpellings");
1420
1421 if (SemaHandler) {
1422 if (DistinctSpellings) {
1423 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
1424
1425 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
1426 E = Spellings.end(); I != E; ++I) {
1427 std::string AttrName = (*I)->getValueAsString("Name");
1428
1429 StringRef Spelling = NormalizeAttrName(AttrName);
1430
1431 OS << "PARSED_ATTR(" << Spelling << ")\n";
1432 }
1433 } else {
1434 StringRef AttrName = Attr.getName();
1435 AttrName = NormalizeAttrName(AttrName);
1436 OS << "PARSED_ATTR(" << AttrName << ")\n";
1437 }
1438 }
1439 }
1440 }
1441
1442 // Emits the kind list of parsed attributes
EmitClangAttrParsedAttrKinds(RecordKeeper & Records,raw_ostream & OS)1443 void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
1444 emitSourceFileHeader("Attribute name matcher", OS);
1445
1446 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1447
1448 std::vector<StringMatcher::StringPair> Matches;
1449 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1450 I != E; ++I) {
1451 Record &Attr = **I;
1452
1453 bool SemaHandler = Attr.getValueAsBit("SemaHandler");
1454 bool Ignored = Attr.getValueAsBit("Ignored");
1455 bool DistinctSpellings = Attr.getValueAsBit("DistinctSpellings");
1456 if (SemaHandler || Ignored) {
1457 std::vector<Record*> Spellings = Attr.getValueAsListOfDefs("Spellings");
1458
1459 for (std::vector<Record*>::const_iterator I = Spellings.begin(),
1460 E = Spellings.end(); I != E; ++I) {
1461 std::string RawSpelling = (*I)->getValueAsString("Name");
1462 StringRef AttrName = NormalizeAttrName(DistinctSpellings
1463 ? StringRef(RawSpelling)
1464 : StringRef(Attr.getName()));
1465
1466 SmallString<64> Spelling;
1467 if ((*I)->getValueAsString("Variety") == "CXX11") {
1468 Spelling += (*I)->getValueAsString("Namespace");
1469 Spelling += "::";
1470 }
1471 Spelling += NormalizeAttrSpelling(RawSpelling);
1472
1473 if (SemaHandler)
1474 Matches.push_back(
1475 StringMatcher::StringPair(
1476 StringRef(Spelling),
1477 "return AttributeList::AT_" + AttrName.str() + ";"));
1478 else
1479 Matches.push_back(
1480 StringMatcher::StringPair(
1481 StringRef(Spelling),
1482 "return AttributeList::IgnoredAttribute;"));
1483 }
1484 }
1485 }
1486
1487 OS << "static AttributeList::Kind getAttrKind(StringRef Name) {\n";
1488 StringMatcher("Name", Matches, OS).Emit();
1489 OS << "return AttributeList::UnknownAttribute;\n"
1490 << "}\n";
1491 }
1492
1493 // Emits the code to dump an attribute.
EmitClangAttrDump(RecordKeeper & Records,raw_ostream & OS)1494 void EmitClangAttrDump(RecordKeeper &Records, raw_ostream &OS) {
1495 emitSourceFileHeader("Attribute dumper", OS);
1496
1497 OS <<
1498 " switch (A->getKind()) {\n"
1499 " default:\n"
1500 " llvm_unreachable(\"Unknown attribute kind!\");\n"
1501 " break;\n";
1502 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
1503 for (std::vector<Record*>::iterator I = Attrs.begin(), E = Attrs.end();
1504 I != E; ++I) {
1505 Record &R = **I;
1506 if (!R.getValueAsBit("ASTNode"))
1507 continue;
1508 OS << " case attr::" << R.getName() << ": {\n";
1509 Args = R.getValueAsListOfDefs("Args");
1510 if (!Args.empty()) {
1511 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
1512 << "Attr>(A);\n";
1513 for (std::vector<Record*>::iterator I = Args.begin(), E = Args.end();
1514 I != E; ++I)
1515 createArgument(**I, R.getName())->writeDump(OS);
1516
1517 // Code for detecting the last child.
1518 OS << " bool OldMoreChildren = hasMoreChildren();\n";
1519 OS << " bool MoreChildren = OldMoreChildren;\n";
1520
1521 for (std::vector<Record*>::iterator I = Args.begin(), E = Args.end();
1522 I != E; ++I) {
1523 // More code for detecting the last child.
1524 OS << " MoreChildren = OldMoreChildren";
1525 for (std::vector<Record*>::iterator Next = I + 1; Next != E; ++Next) {
1526 OS << " || ";
1527 createArgument(**Next, R.getName())->writeHasChildren(OS);
1528 }
1529 OS << ";\n";
1530 OS << " setMoreChildren(MoreChildren);\n";
1531
1532 createArgument(**I, R.getName())->writeDumpChildren(OS);
1533 }
1534
1535 // Reset the last child.
1536 OS << " setMoreChildren(OldMoreChildren);\n";
1537 }
1538 OS <<
1539 " break;\n"
1540 " }\n";
1541 }
1542 OS << " }\n";
1543 }
1544
1545 } // end namespace clang
1546