1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "llvm/ADT/ArrayRef.h" |
14 | #include "llvm/ADT/DenseMap.h" |
15 | #include "llvm/ADT/DenseSet.h" |
16 | #include "llvm/ADT/STLExtras.h" |
17 | #include "llvm/ADT/SmallString.h" |
18 | #include "llvm/ADT/StringExtras.h" |
19 | #include "llvm/ADT/StringRef.h" |
20 | #include "llvm/ADT/StringSet.h" |
21 | #include "llvm/ADT/StringSwitch.h" |
22 | #include "llvm/ADT/iterator_range.h" |
23 | #include "llvm/Support/ErrorHandling.h" |
24 | #include "llvm/Support/raw_ostream.h" |
25 | #include "llvm/TableGen/Error.h" |
26 | #include "llvm/TableGen/Record.h" |
27 | #include "llvm/TableGen/StringMatcher.h" |
28 | #include "llvm/TableGen/TableGenBackend.h" |
29 | #include <algorithm> |
30 | #include <cassert> |
31 | #include <cctype> |
32 | #include <cstddef> |
33 | #include <cstdint> |
34 | #include <map> |
35 | #include <memory> |
36 | #include <set> |
37 | #include <sstream> |
38 | #include <string> |
39 | #include <utility> |
40 | #include <vector> |
41 | |
42 | using namespace llvm; |
43 | |
44 | namespace { |
45 | |
46 | class FlattenedSpelling { |
47 | std::string V, N, NS; |
48 | bool K; |
49 | |
50 | public: |
51 | FlattenedSpelling(const std::string &Variety, const std::string &Name, |
52 | const std::string &Namespace, bool KnownToGCC) : |
53 | V(Variety), N(Name), NS(Namespace), K(KnownToGCC) {} |
54 | explicit FlattenedSpelling(const Record &Spelling) : |
55 | V(Spelling.getValueAsString("Variety")), |
56 | N(Spelling.getValueAsString("Name")) { |
57 | |
58 | (0) . __assert_fail ("V != \"GCC\" && V != \"Clang\" && \"Given a GCC spelling, which means this hasn't been flattened!\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 59, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(V != "GCC" && V != "Clang" && |
59 | (0) . __assert_fail ("V != \"GCC\" && V != \"Clang\" && \"Given a GCC spelling, which means this hasn't been flattened!\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 59, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Given a GCC spelling, which means this hasn't been flattened!"); |
60 | if (V == "CXX11" || V == "C2x" || V == "Pragma") |
61 | NS = Spelling.getValueAsString("Namespace"); |
62 | bool Unset; |
63 | K = Spelling.getValueAsBitOrUnset("KnownToGCC", Unset); |
64 | } |
65 | |
66 | const std::string &variety() const { return V; } |
67 | const std::string &name() const { return N; } |
68 | const std::string &nameSpace() const { return NS; } |
69 | bool knownToGCC() const { return K; } |
70 | }; |
71 | |
72 | } |
73 | |
74 | static std::vector<FlattenedSpelling> |
75 | GetFlattenedSpellings(const Record &Attr) { |
76 | std::vector<Record *> Spellings = Attr.getValueAsListOfDefs("Spellings"); |
77 | std::vector<FlattenedSpelling> Ret; |
78 | |
79 | for (const auto &Spelling : Spellings) { |
80 | StringRef Variety = Spelling->getValueAsString("Variety"); |
81 | StringRef Name = Spelling->getValueAsString("Name"); |
82 | if (Variety == "GCC") { |
83 | |
84 | Ret.emplace_back("GNU", Name, "", true); |
85 | Ret.emplace_back("CXX11", Name, "gnu", true); |
86 | } else if (Variety == "Clang") { |
87 | Ret.emplace_back("GNU", Name, "", false); |
88 | Ret.emplace_back("CXX11", Name, "clang", false); |
89 | if (Spelling->getValueAsBit("AllowInC")) |
90 | Ret.emplace_back("C2x", Name, "clang", false); |
91 | } else |
92 | Ret.push_back(FlattenedSpelling(*Spelling)); |
93 | } |
94 | |
95 | return Ret; |
96 | } |
97 | |
98 | static std::string ReadPCHRecord(StringRef type) { |
99 | return StringSwitch<std::string>(type) |
100 | .EndsWith("Decl *", "Record.GetLocalDeclAs<" |
101 | + std::string(type, 0, type.size()-1) + ">(Record.readInt())") |
102 | .Case("TypeSourceInfo *", "Record.getTypeSourceInfo()") |
103 | .Case("Expr *", "Record.readExpr()") |
104 | .Case("IdentifierInfo *", "Record.getIdentifierInfo()") |
105 | .Case("StringRef", "Record.readString()") |
106 | .Case("ParamIdx", "ParamIdx::deserialize(Record.readInt())") |
107 | .Default("Record.readInt()"); |
108 | } |
109 | |
110 | |
111 | static StringRef getStorageType(StringRef type) { |
112 | return StringSwitch<StringRef>(type) |
113 | .Case("StringRef", "std::string") |
114 | .Default(type); |
115 | } |
116 | |
117 | |
118 | static std::string WritePCHRecord(StringRef type, StringRef name) { |
119 | return "Record." + StringSwitch<std::string>(type) |
120 | .EndsWith("Decl *", "AddDeclRef(" + std::string(name) + ");\n") |
121 | .Case("TypeSourceInfo *", "AddTypeSourceInfo(" + std::string(name) + ");\n") |
122 | .Case("Expr *", "AddStmt(" + std::string(name) + ");\n") |
123 | .Case("IdentifierInfo *", "AddIdentifierRef(" + std::string(name) + ");\n") |
124 | .Case("StringRef", "AddString(" + std::string(name) + ");\n") |
125 | .Case("ParamIdx", "push_back(" + std::string(name) + ".serialize());\n") |
126 | .Default("push_back(" + std::string(name) + ");\n"); |
127 | } |
128 | |
129 | |
130 | |
131 | |
132 | static StringRef NormalizeAttrName(StringRef AttrName) { |
133 | AttrName.consume_front("__"); |
134 | AttrName.consume_back("__"); |
135 | return AttrName; |
136 | } |
137 | |
138 | |
139 | |
140 | |
141 | static StringRef NormalizeNameForSpellingComparison(StringRef Name) { |
142 | return Name.trim("_"); |
143 | } |
144 | |
145 | |
146 | |
147 | static StringRef NormalizeGNUAttrSpelling(StringRef AttrSpelling) { |
148 | if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) { |
149 | AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4); |
150 | } |
151 | |
152 | return AttrSpelling; |
153 | } |
154 | |
155 | typedef std::vector<std::pair<std::string, const Record *>> ParsedAttrMap; |
156 | |
157 | static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records, |
158 | ParsedAttrMap *Dupes = nullptr) { |
159 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); |
160 | std::set<std::string> Seen; |
161 | ParsedAttrMap R; |
162 | for (const auto *Attr : Attrs) { |
163 | if (Attr->getValueAsBit("SemaHandler")) { |
164 | std::string AN; |
165 | if (Attr->isSubClassOf("TargetSpecificAttr") && |
166 | !Attr->isValueUnset("ParseKind")) { |
167 | AN = Attr->getValueAsString("ParseKind"); |
168 | |
169 | |
170 | |
171 | if (Seen.find(AN) != Seen.end()) { |
172 | if (Dupes) |
173 | Dupes->push_back(std::make_pair(AN, Attr)); |
174 | continue; |
175 | } |
176 | Seen.insert(AN); |
177 | } else |
178 | AN = NormalizeAttrName(Attr->getName()).str(); |
179 | |
180 | R.push_back(std::make_pair(AN, Attr)); |
181 | } |
182 | } |
183 | return R; |
184 | } |
185 | |
186 | namespace { |
187 | |
188 | class Argument { |
189 | std::string lowerName, upperName; |
190 | StringRef attrName; |
191 | bool isOpt; |
192 | bool Fake; |
193 | |
194 | public: |
195 | Argument(const Record &Arg, StringRef Attr) |
196 | : lowerName(Arg.getValueAsString("Name")), upperName(lowerName), |
197 | attrName(Attr), isOpt(false), Fake(false) { |
198 | if (!lowerName.empty()) { |
199 | lowerName[0] = std::tolower(lowerName[0]); |
200 | upperName[0] = std::toupper(upperName[0]); |
201 | } |
202 | |
203 | |
204 | |
205 | if (lowerName == "interface") |
206 | lowerName = "interface_"; |
207 | } |
208 | virtual ~Argument() = default; |
209 | |
210 | StringRef getLowerName() const { return lowerName; } |
211 | StringRef getUpperName() const { return upperName; } |
212 | StringRef getAttrName() const { return attrName; } |
213 | |
214 | bool isOptional() const { return isOpt; } |
215 | void setOptional(bool set) { isOpt = set; } |
216 | |
217 | bool isFake() const { return Fake; } |
218 | void setFake(bool fake) { Fake = fake; } |
219 | |
220 | |
221 | virtual void writeAccessors(raw_ostream &OS) const = 0; |
222 | virtual void writeAccessorDefinitions(raw_ostream &OS) const {} |
223 | virtual void writeASTVisitorTraversal(raw_ostream &OS) const {} |
224 | virtual void writeCloneArgs(raw_ostream &OS) const = 0; |
225 | virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0; |
226 | virtual void writeTemplateInstantiation(raw_ostream &OS) const {} |
227 | virtual void writeCtorBody(raw_ostream &OS) const {} |
228 | virtual void writeCtorInitializers(raw_ostream &OS) const = 0; |
229 | virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0; |
230 | virtual void writeCtorParameters(raw_ostream &OS) const = 0; |
231 | virtual void writeDeclarations(raw_ostream &OS) const = 0; |
232 | virtual void writePCHReadArgs(raw_ostream &OS) const = 0; |
233 | virtual void writePCHReadDecls(raw_ostream &OS) const = 0; |
234 | virtual void writePCHWrite(raw_ostream &OS) const = 0; |
235 | virtual std::string getIsOmitted() const { return "false"; } |
236 | virtual void writeValue(raw_ostream &OS) const = 0; |
237 | virtual void writeDump(raw_ostream &OS) const = 0; |
238 | virtual void writeDumpChildren(raw_ostream &OS) const {} |
239 | virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; } |
240 | |
241 | virtual bool isEnumArg() const { return false; } |
242 | virtual bool isVariadicEnumArg() const { return false; } |
243 | virtual bool isVariadic() const { return false; } |
244 | |
245 | virtual void writeImplicitCtorArgs(raw_ostream &OS) const { |
246 | OS << getUpperName(); |
247 | } |
248 | }; |
249 | |
250 | class SimpleArgument : public Argument { |
251 | std::string type; |
252 | |
253 | public: |
254 | SimpleArgument(const Record &Arg, StringRef Attr, std::string T) |
255 | : Argument(Arg, Attr), type(std::move(T)) {} |
256 | |
257 | std::string getType() const { return type; } |
258 | |
259 | void writeAccessors(raw_ostream &OS) const override { |
260 | OS << " " << type << " get" << getUpperName() << "() const {\n"; |
261 | OS << " return " << getLowerName() << ";\n"; |
262 | OS << " }"; |
263 | } |
264 | |
265 | void writeCloneArgs(raw_ostream &OS) const override { |
266 | OS << getLowerName(); |
267 | } |
268 | |
269 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
270 | OS << "A->get" << getUpperName() << "()"; |
271 | } |
272 | |
273 | void writeCtorInitializers(raw_ostream &OS) const override { |
274 | OS << getLowerName() << "(" << getUpperName() << ")"; |
275 | } |
276 | |
277 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
278 | OS << getLowerName() << "()"; |
279 | } |
280 | |
281 | void writeCtorParameters(raw_ostream &OS) const override { |
282 | OS << type << " " << getUpperName(); |
283 | } |
284 | |
285 | void writeDeclarations(raw_ostream &OS) const override { |
286 | OS << type << " " << getLowerName() << ";"; |
287 | } |
288 | |
289 | void writePCHReadDecls(raw_ostream &OS) const override { |
290 | std::string read = ReadPCHRecord(type); |
291 | OS << " " << type << " " << getLowerName() << " = " << read << ";\n"; |
292 | } |
293 | |
294 | void writePCHReadArgs(raw_ostream &OS) const override { |
295 | OS << getLowerName(); |
296 | } |
297 | |
298 | void writePCHWrite(raw_ostream &OS) const override { |
299 | OS << " " << WritePCHRecord(type, "SA->get" + |
300 | std::string(getUpperName()) + "()"); |
301 | } |
302 | |
303 | std::string getIsOmitted() const override { |
304 | if (type == "IdentifierInfo *") |
305 | return "!get" + getUpperName().str() + "()"; |
306 | if (type == "ParamIdx") |
307 | return "!get" + getUpperName().str() + "().isValid()"; |
308 | return "false"; |
309 | } |
310 | |
311 | void writeValue(raw_ostream &OS) const override { |
312 | if (type == "FunctionDecl *") |
313 | OS << "\" << get" << getUpperName() |
314 | << "()->getNameInfo().getAsString() << \""; |
315 | else if (type == "IdentifierInfo *") |
316 | |
317 | |
318 | OS << "\" << (get" << getUpperName() << "() ? get" << getUpperName() |
319 | << "()->getName() : \"\") << \""; |
320 | else if (type == "TypeSourceInfo *") |
321 | OS << "\" << get" << getUpperName() << "().getAsString() << \""; |
322 | else if (type == "ParamIdx") |
323 | OS << "\" << get" << getUpperName() << "().getSourceIndex() << \""; |
324 | else |
325 | OS << "\" << get" << getUpperName() << "() << \""; |
326 | } |
327 | |
328 | void writeDump(raw_ostream &OS) const override { |
329 | if (type == "FunctionDecl *" || type == "NamedDecl *") { |
330 | OS << " OS << \" \";\n"; |
331 | OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n"; |
332 | } else if (type == "IdentifierInfo *") { |
333 | |
334 | |
335 | OS << " if (SA->get" << getUpperName() << "())\n" |
336 | << " OS << \" \" << SA->get" << getUpperName() |
337 | << "()->getName();\n"; |
338 | } else if (type == "TypeSourceInfo *") { |
339 | OS << " OS << \" \" << SA->get" << getUpperName() |
340 | << "().getAsString();\n"; |
341 | } else if (type == "bool") { |
342 | OS << " if (SA->get" << getUpperName() << "()) OS << \" " |
343 | << getUpperName() << "\";\n"; |
344 | } else if (type == "int" || type == "unsigned") { |
345 | OS << " OS << \" \" << SA->get" << getUpperName() << "();\n"; |
346 | } else if (type == "ParamIdx") { |
347 | if (isOptional()) |
348 | OS << " if (SA->get" << getUpperName() << "().isValid())\n "; |
349 | OS << " OS << \" \" << SA->get" << getUpperName() |
350 | << "().getSourceIndex();\n"; |
351 | } else { |
352 | llvm_unreachable("Unknown SimpleArgument type!"); |
353 | } |
354 | } |
355 | }; |
356 | |
357 | class DefaultSimpleArgument : public SimpleArgument { |
358 | int64_t Default; |
359 | |
360 | public: |
361 | DefaultSimpleArgument(const Record &Arg, StringRef Attr, |
362 | std::string T, int64_t Default) |
363 | : SimpleArgument(Arg, Attr, T), Default(Default) {} |
364 | |
365 | void writeAccessors(raw_ostream &OS) const override { |
366 | SimpleArgument::writeAccessors(OS); |
367 | |
368 | OS << "\n\n static const " << getType() << " Default" << getUpperName() |
369 | << " = "; |
370 | if (getType() == "bool") |
371 | OS << (Default != 0 ? "true" : "false"); |
372 | else |
373 | OS << Default; |
374 | OS << ";"; |
375 | } |
376 | }; |
377 | |
378 | class StringArgument : public Argument { |
379 | public: |
380 | StringArgument(const Record &Arg, StringRef Attr) |
381 | : Argument(Arg, Attr) |
382 | {} |
383 | |
384 | void writeAccessors(raw_ostream &OS) const override { |
385 | OS << " llvm::StringRef get" << getUpperName() << "() const {\n"; |
386 | OS << " return llvm::StringRef(" << getLowerName() << ", " |
387 | << getLowerName() << "Length);\n"; |
388 | OS << " }\n"; |
389 | OS << " unsigned get" << getUpperName() << "Length() const {\n"; |
390 | OS << " return " << getLowerName() << "Length;\n"; |
391 | OS << " }\n"; |
392 | OS << " void set" << getUpperName() |
393 | << "(ASTContext &C, llvm::StringRef S) {\n"; |
394 | OS << " " << getLowerName() << "Length = S.size();\n"; |
395 | OS << " this->" << getLowerName() << " = new (C, 1) char [" |
396 | << getLowerName() << "Length];\n"; |
397 | OS << " if (!S.empty())\n"; |
398 | OS << " std::memcpy(this->" << getLowerName() << ", S.data(), " |
399 | << getLowerName() << "Length);\n"; |
400 | OS << " }"; |
401 | } |
402 | |
403 | void writeCloneArgs(raw_ostream &OS) const override { |
404 | OS << "get" << getUpperName() << "()"; |
405 | } |
406 | |
407 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
408 | OS << "A->get" << getUpperName() << "()"; |
409 | } |
410 | |
411 | void writeCtorBody(raw_ostream &OS) const override { |
412 | OS << " if (!" << getUpperName() << ".empty())\n"; |
413 | OS << " std::memcpy(" << getLowerName() << ", " << getUpperName() |
414 | << ".data(), " << getLowerName() << "Length);\n"; |
415 | } |
416 | |
417 | void writeCtorInitializers(raw_ostream &OS) const override { |
418 | OS << getLowerName() << "Length(" << getUpperName() << ".size())," |
419 | << getLowerName() << "(new (Ctx, 1) char[" << getLowerName() |
420 | << "Length])"; |
421 | } |
422 | |
423 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
424 | OS << getLowerName() << "Length(0)," << getLowerName() << "(nullptr)"; |
425 | } |
426 | |
427 | void writeCtorParameters(raw_ostream &OS) const override { |
428 | OS << "llvm::StringRef " << getUpperName(); |
429 | } |
430 | |
431 | void writeDeclarations(raw_ostream &OS) const override { |
432 | OS << "unsigned " << getLowerName() << "Length;\n"; |
433 | OS << "char *" << getLowerName() << ";"; |
434 | } |
435 | |
436 | void writePCHReadDecls(raw_ostream &OS) const override { |
437 | OS << " std::string " << getLowerName() |
438 | << "= Record.readString();\n"; |
439 | } |
440 | |
441 | void writePCHReadArgs(raw_ostream &OS) const override { |
442 | OS << getLowerName(); |
443 | } |
444 | |
445 | void writePCHWrite(raw_ostream &OS) const override { |
446 | OS << " Record.AddString(SA->get" << getUpperName() << "());\n"; |
447 | } |
448 | |
449 | void writeValue(raw_ostream &OS) const override { |
450 | OS << "\\\"\" << get" << getUpperName() << "() << \"\\\""; |
451 | } |
452 | |
453 | void writeDump(raw_ostream &OS) const override { |
454 | OS << " OS << \" \\\"\" << SA->get" << getUpperName() |
455 | << "() << \"\\\"\";\n"; |
456 | } |
457 | }; |
458 | |
459 | class AlignedArgument : public Argument { |
460 | public: |
461 | AlignedArgument(const Record &Arg, StringRef Attr) |
462 | : Argument(Arg, Attr) |
463 | {} |
464 | |
465 | void writeAccessors(raw_ostream &OS) const override { |
466 | OS << " bool is" << getUpperName() << "Dependent() const;\n"; |
467 | |
468 | OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n"; |
469 | |
470 | OS << " bool is" << getUpperName() << "Expr() const {\n"; |
471 | OS << " return is" << getLowerName() << "Expr;\n"; |
472 | OS << " }\n"; |
473 | |
474 | OS << " Expr *get" << getUpperName() << "Expr() const {\n"; |
475 | OS << " assert(is" << getLowerName() << "Expr);\n"; |
476 | OS << " return " << getLowerName() << "Expr;\n"; |
477 | OS << " }\n"; |
478 | |
479 | OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n"; |
480 | OS << " assert(!is" << getLowerName() << "Expr);\n"; |
481 | OS << " return " << getLowerName() << "Type;\n"; |
482 | OS << " }"; |
483 | } |
484 | |
485 | void writeAccessorDefinitions(raw_ostream &OS) const override { |
486 | OS << "bool " << getAttrName() << "Attr::is" << getUpperName() |
487 | << "Dependent() const {\n"; |
488 | OS << " if (is" << getLowerName() << "Expr)\n"; |
489 | OS << " return " << getLowerName() << "Expr && (" << getLowerName() |
490 | << "Expr->isValueDependent() || " << getLowerName() |
491 | << "Expr->isTypeDependent());\n"; |
492 | OS << " else\n"; |
493 | OS << " return " << getLowerName() |
494 | << "Type->getType()->isDependentType();\n"; |
495 | OS << "}\n"; |
496 | |
497 | |
498 | |
499 | |
500 | OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName() |
501 | << "(ASTContext &Ctx) const {\n"; |
502 | OS << " assert(!is" << getUpperName() << "Dependent());\n"; |
503 | OS << " if (is" << getLowerName() << "Expr)\n"; |
504 | OS << " return " << getLowerName() << "Expr ? " << getLowerName() |
505 | << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue()" |
506 | << " * Ctx.getCharWidth() : " |
507 | << "Ctx.getTargetDefaultAlignForAttributeAligned();\n"; |
508 | OS << " else\n"; |
509 | OS << " return 0; // FIXME\n"; |
510 | OS << "}\n"; |
511 | } |
512 | |
513 | void writeASTVisitorTraversal(raw_ostream &OS) const override { |
514 | StringRef Name = getUpperName(); |
515 | OS << " if (A->is" << Name << "Expr()) {\n" |
516 | << " if (!getDerived().TraverseStmt(A->get" << Name << "Expr()))\n" |
517 | << " return false;\n" |
518 | << " } else if (auto *TSI = A->get" << Name << "Type()) {\n" |
519 | << " if (!getDerived().TraverseTypeLoc(TSI->getTypeLoc()))\n" |
520 | << " return false;\n" |
521 | << " }\n"; |
522 | } |
523 | |
524 | void writeCloneArgs(raw_ostream &OS) const override { |
525 | OS << "is" << getLowerName() << "Expr, is" << getLowerName() |
526 | << "Expr ? static_cast<void*>(" << getLowerName() |
527 | << "Expr) : " << getLowerName() |
528 | << "Type"; |
529 | } |
530 | |
531 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
532 | |
533 | |
534 | } |
535 | |
536 | void writeCtorBody(raw_ostream &OS) const override { |
537 | OS << " if (is" << getLowerName() << "Expr)\n"; |
538 | OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>(" |
539 | << getUpperName() << ");\n"; |
540 | OS << " else\n"; |
541 | OS << " " << getLowerName() |
542 | << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName() |
543 | << ");\n"; |
544 | } |
545 | |
546 | void writeCtorInitializers(raw_ostream &OS) const override { |
547 | OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)"; |
548 | } |
549 | |
550 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
551 | OS << "is" << getLowerName() << "Expr(false)"; |
552 | } |
553 | |
554 | void writeCtorParameters(raw_ostream &OS) const override { |
555 | OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName(); |
556 | } |
557 | |
558 | void writeImplicitCtorArgs(raw_ostream &OS) const override { |
559 | OS << "Is" << getUpperName() << "Expr, " << getUpperName(); |
560 | } |
561 | |
562 | void writeDeclarations(raw_ostream &OS) const override { |
563 | OS << "bool is" << getLowerName() << "Expr;\n"; |
564 | OS << "union {\n"; |
565 | OS << "Expr *" << getLowerName() << "Expr;\n"; |
566 | OS << "TypeSourceInfo *" << getLowerName() << "Type;\n"; |
567 | OS << "};"; |
568 | } |
569 | |
570 | void writePCHReadArgs(raw_ostream &OS) const override { |
571 | OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr"; |
572 | } |
573 | |
574 | void writePCHReadDecls(raw_ostream &OS) const override { |
575 | OS << " bool is" << getLowerName() << "Expr = Record.readInt();\n"; |
576 | OS << " void *" << getLowerName() << "Ptr;\n"; |
577 | OS << " if (is" << getLowerName() << "Expr)\n"; |
578 | OS << " " << getLowerName() << "Ptr = Record.readExpr();\n"; |
579 | OS << " else\n"; |
580 | OS << " " << getLowerName() |
581 | << "Ptr = Record.getTypeSourceInfo();\n"; |
582 | } |
583 | |
584 | void writePCHWrite(raw_ostream &OS) const override { |
585 | OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n"; |
586 | OS << " if (SA->is" << getUpperName() << "Expr())\n"; |
587 | OS << " Record.AddStmt(SA->get" << getUpperName() << "Expr());\n"; |
588 | OS << " else\n"; |
589 | OS << " Record.AddTypeSourceInfo(SA->get" << getUpperName() |
590 | << "Type());\n"; |
591 | } |
592 | |
593 | std::string getIsOmitted() const override { |
594 | return "!is" + getLowerName().str() + "Expr || !" + getLowerName().str() |
595 | + "Expr"; |
596 | } |
597 | |
598 | void writeValue(raw_ostream &OS) const override { |
599 | OS << "\";\n"; |
600 | OS << " " << getLowerName() |
601 | << "Expr->printPretty(OS, nullptr, Policy);\n"; |
602 | OS << " OS << \""; |
603 | } |
604 | |
605 | void writeDump(raw_ostream &OS) const override { |
606 | OS << " if (!SA->is" << getUpperName() << "Expr())\n"; |
607 | OS << " dumpType(SA->get" << getUpperName() |
608 | << "Type()->getType());\n"; |
609 | } |
610 | |
611 | void writeDumpChildren(raw_ostream &OS) const override { |
612 | OS << " if (SA->is" << getUpperName() << "Expr())\n"; |
613 | OS << " Visit(SA->get" << getUpperName() << "Expr());\n"; |
614 | } |
615 | |
616 | void writeHasChildren(raw_ostream &OS) const override { |
617 | OS << "SA->is" << getUpperName() << "Expr()"; |
618 | } |
619 | }; |
620 | |
621 | class VariadicArgument : public Argument { |
622 | std::string Type, ArgName, ArgSizeName, RangeName; |
623 | |
624 | protected: |
625 | |
626 | virtual void writeValueImpl(raw_ostream &OS) const { |
627 | OS << " OS << Val;\n"; |
628 | } |
629 | |
630 | virtual void writeDumpImpl(raw_ostream &OS) const { |
631 | OS << " OS << \" \" << Val;\n"; |
632 | } |
633 | |
634 | public: |
635 | VariadicArgument(const Record &Arg, StringRef Attr, std::string T) |
636 | : Argument(Arg, Attr), Type(std::move(T)), |
637 | ArgName(getLowerName().str() + "_"), ArgSizeName(ArgName + "Size"), |
638 | RangeName(getLowerName()) {} |
639 | |
640 | const std::string &getType() const { return Type; } |
641 | const std::string &getArgName() const { return ArgName; } |
642 | const std::string &getArgSizeName() const { return ArgSizeName; } |
643 | bool isVariadic() const override { return true; } |
644 | |
645 | void writeAccessors(raw_ostream &OS) const override { |
646 | std::string IteratorType = getLowerName().str() + "_iterator"; |
647 | std::string BeginFn = getLowerName().str() + "_begin()"; |
648 | std::string EndFn = getLowerName().str() + "_end()"; |
649 | |
650 | OS << " typedef " << Type << "* " << IteratorType << ";\n"; |
651 | OS << " " << IteratorType << " " << BeginFn << " const {" |
652 | << " return " << ArgName << "; }\n"; |
653 | OS << " " << IteratorType << " " << EndFn << " const {" |
654 | << " return " << ArgName << " + " << ArgSizeName << "; }\n"; |
655 | OS << " unsigned " << getLowerName() << "_size() const {" |
656 | << " return " << ArgSizeName << "; }\n"; |
657 | OS << " llvm::iterator_range<" << IteratorType << "> " << RangeName |
658 | << "() const { return llvm::make_range(" << BeginFn << ", " << EndFn |
659 | << "); }\n"; |
660 | } |
661 | |
662 | void writeCloneArgs(raw_ostream &OS) const override { |
663 | OS << ArgName << ", " << ArgSizeName; |
664 | } |
665 | |
666 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
667 | |
668 | OS << "A->" << getLowerName() << "_begin(), " |
669 | << "A->" << getLowerName() << "_size()"; |
670 | } |
671 | |
672 | void writeASTVisitorTraversal(raw_ostream &OS) const override { |
673 | |
674 | } |
675 | |
676 | void writeCtorBody(raw_ostream &OS) const override { |
677 | OS << " std::copy(" << getUpperName() << ", " << getUpperName() |
678 | << " + " << ArgSizeName << ", " << ArgName << ");\n"; |
679 | } |
680 | |
681 | void writeCtorInitializers(raw_ostream &OS) const override { |
682 | OS << ArgSizeName << "(" << getUpperName() << "Size), " |
683 | << ArgName << "(new (Ctx, 16) " << getType() << "[" |
684 | << ArgSizeName << "])"; |
685 | } |
686 | |
687 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
688 | OS << ArgSizeName << "(0), " << ArgName << "(nullptr)"; |
689 | } |
690 | |
691 | void writeCtorParameters(raw_ostream &OS) const override { |
692 | OS << getType() << " *" << getUpperName() << ", unsigned " |
693 | << getUpperName() << "Size"; |
694 | } |
695 | |
696 | void writeImplicitCtorArgs(raw_ostream &OS) const override { |
697 | OS << getUpperName() << ", " << getUpperName() << "Size"; |
698 | } |
699 | |
700 | void writeDeclarations(raw_ostream &OS) const override { |
701 | OS << " unsigned " << ArgSizeName << ";\n"; |
702 | OS << " " << getType() << " *" << ArgName << ";"; |
703 | } |
704 | |
705 | void writePCHReadDecls(raw_ostream &OS) const override { |
706 | OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n"; |
707 | OS << " SmallVector<" << getType() << ", 4> " |
708 | << getLowerName() << ";\n"; |
709 | OS << " " << getLowerName() << ".reserve(" << getLowerName() |
710 | << "Size);\n"; |
711 | |
712 | |
713 | |
714 | |
715 | std::string StorageType = getStorageType(getType()); |
716 | std::string StorageName = getLowerName(); |
717 | if (StorageType != getType()) { |
718 | StorageName += "Storage"; |
719 | OS << " SmallVector<" << StorageType << ", 4> " |
720 | << StorageName << ";\n"; |
721 | OS << " " << StorageName << ".reserve(" << getLowerName() |
722 | << "Size);\n"; |
723 | } |
724 | |
725 | OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n"; |
726 | std::string read = ReadPCHRecord(Type); |
727 | OS << " " << StorageName << ".push_back(" << read << ");\n"; |
728 | |
729 | if (StorageType != getType()) { |
730 | OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n"; |
731 | OS << " " << getLowerName() << ".push_back(" |
732 | << StorageName << "[i]);\n"; |
733 | } |
734 | } |
735 | |
736 | void writePCHReadArgs(raw_ostream &OS) const override { |
737 | OS << getLowerName() << ".data(), " << getLowerName() << "Size"; |
738 | } |
739 | |
740 | void writePCHWrite(raw_ostream &OS) const override { |
741 | OS << " Record.push_back(SA->" << getLowerName() << "_size());\n"; |
742 | OS << " for (auto &Val : SA->" << RangeName << "())\n"; |
743 | OS << " " << WritePCHRecord(Type, "Val"); |
744 | } |
745 | |
746 | void writeValue(raw_ostream &OS) const override { |
747 | OS << "\";\n"; |
748 | OS << " bool isFirst = true;\n" |
749 | << " for (const auto &Val : " << RangeName << "()) {\n" |
750 | << " if (isFirst) isFirst = false;\n" |
751 | << " else OS << \", \";\n"; |
752 | writeValueImpl(OS); |
753 | OS << " }\n"; |
754 | OS << " OS << \""; |
755 | } |
756 | |
757 | void writeDump(raw_ostream &OS) const override { |
758 | OS << " for (const auto &Val : SA->" << RangeName << "())\n"; |
759 | writeDumpImpl(OS); |
760 | } |
761 | }; |
762 | |
763 | class VariadicParamIdxArgument : public VariadicArgument { |
764 | public: |
765 | VariadicParamIdxArgument(const Record &Arg, StringRef Attr) |
766 | : VariadicArgument(Arg, Attr, "ParamIdx") {} |
767 | |
768 | public: |
769 | void writeValueImpl(raw_ostream &OS) const override { |
770 | OS << " OS << Val.getSourceIndex();\n"; |
771 | } |
772 | |
773 | void writeDumpImpl(raw_ostream &OS) const override { |
774 | OS << " OS << \" \" << Val.getSourceIndex();\n"; |
775 | } |
776 | }; |
777 | |
778 | struct VariadicParamOrParamIdxArgument : public VariadicArgument { |
779 | VariadicParamOrParamIdxArgument(const Record &Arg, StringRef Attr) |
780 | : VariadicArgument(Arg, Attr, "int") {} |
781 | }; |
782 | |
783 | |
784 | std::vector<StringRef> |
785 | uniqueEnumsInOrder(const std::vector<StringRef> &enums) { |
786 | std::vector<StringRef> uniques; |
787 | SmallDenseSet<StringRef, 8> unique_set; |
788 | for (const auto &i : enums) { |
789 | if (unique_set.insert(i).second) |
790 | uniques.push_back(i); |
791 | } |
792 | return uniques; |
793 | } |
794 | |
795 | class EnumArgument : public Argument { |
796 | std::string type; |
797 | std::vector<StringRef> values, enums, uniques; |
798 | |
799 | public: |
800 | EnumArgument(const Record &Arg, StringRef Attr) |
801 | : Argument(Arg, Attr), type(Arg.getValueAsString("Type")), |
802 | values(Arg.getValueAsListOfStrings("Values")), |
803 | enums(Arg.getValueAsListOfStrings("Enums")), |
804 | uniques(uniqueEnumsInOrder(enums)) |
805 | { |
806 | |
807 | assert(!uniques.empty()); |
808 | } |
809 | |
810 | bool isEnumArg() const override { return true; } |
811 | |
812 | void writeAccessors(raw_ostream &OS) const override { |
813 | OS << " " << type << " get" << getUpperName() << "() const {\n"; |
814 | OS << " return " << getLowerName() << ";\n"; |
815 | OS << " }"; |
816 | } |
817 | |
818 | void writeCloneArgs(raw_ostream &OS) const override { |
819 | OS << getLowerName(); |
820 | } |
821 | |
822 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
823 | OS << "A->get" << getUpperName() << "()"; |
824 | } |
825 | void writeCtorInitializers(raw_ostream &OS) const override { |
826 | OS << getLowerName() << "(" << getUpperName() << ")"; |
827 | } |
828 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
829 | OS << getLowerName() << "(" << type << "(0))"; |
830 | } |
831 | void writeCtorParameters(raw_ostream &OS) const override { |
832 | OS << type << " " << getUpperName(); |
833 | } |
834 | void writeDeclarations(raw_ostream &OS) const override { |
835 | auto i = uniques.cbegin(), e = uniques.cend(); |
836 | |
837 | --e; |
838 | |
839 | OS << "public:\n"; |
840 | OS << " enum " << type << " {\n"; |
841 | for (; i != e; ++i) |
842 | OS << " " << *i << ",\n"; |
843 | OS << " " << *e << "\n"; |
844 | OS << " };\n"; |
845 | OS << "private:\n"; |
846 | OS << " " << type << " " << getLowerName() << ";"; |
847 | } |
848 | |
849 | void writePCHReadDecls(raw_ostream &OS) const override { |
850 | OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName() |
851 | << "(static_cast<" << getAttrName() << "Attr::" << type |
852 | << ">(Record.readInt()));\n"; |
853 | } |
854 | |
855 | void writePCHReadArgs(raw_ostream &OS) const override { |
856 | OS << getLowerName(); |
857 | } |
858 | |
859 | void writePCHWrite(raw_ostream &OS) const override { |
860 | OS << "Record.push_back(SA->get" << getUpperName() << "());\n"; |
861 | } |
862 | |
863 | void writeValue(raw_ostream &OS) const override { |
864 | |
865 | |
866 | |
867 | OS << "\\\"\" << " << getAttrName() << "Attr::Convert" << type << "ToStr(get" |
868 | << getUpperName() << "()) << \"\\\""; |
869 | } |
870 | |
871 | void writeDump(raw_ostream &OS) const override { |
872 | OS << " switch(SA->get" << getUpperName() << "()) {\n"; |
873 | for (const auto &I : uniques) { |
874 | OS << " case " << getAttrName() << "Attr::" << I << ":\n"; |
875 | OS << " OS << \" " << I << "\";\n"; |
876 | OS << " break;\n"; |
877 | } |
878 | OS << " }\n"; |
879 | } |
880 | |
881 | void writeConversion(raw_ostream &OS) const { |
882 | OS << " static bool ConvertStrTo" << type << "(StringRef Val, "; |
883 | OS << type << " &Out) {\n"; |
884 | OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<"; |
885 | OS << type << ">>(Val)\n"; |
886 | for (size_t I = 0; I < enums.size(); ++I) { |
887 | OS << " .Case(\"" << values[I] << "\", "; |
888 | OS << getAttrName() << "Attr::" << enums[I] << ")\n"; |
889 | } |
890 | OS << " .Default(Optional<" << type << ">());\n"; |
891 | OS << " if (R) {\n"; |
892 | OS << " Out = *R;\n return true;\n }\n"; |
893 | OS << " return false;\n"; |
894 | OS << " }\n\n"; |
895 | |
896 | |
897 | |
898 | |
899 | |
900 | OS << " static const char *Convert" << type << "ToStr(" |
901 | << type << " Val) {\n" |
902 | << " switch(Val) {\n"; |
903 | SmallDenseSet<StringRef, 8> Uniques; |
904 | for (size_t I = 0; I < enums.size(); ++I) { |
905 | if (Uniques.insert(enums[I]).second) |
906 | OS << " case " << getAttrName() << "Attr::" << enums[I] |
907 | << ": return \"" << values[I] << "\";\n"; |
908 | } |
909 | OS << " }\n" |
910 | << " llvm_unreachable(\"No enumerator with that value\");\n" |
911 | << " }\n"; |
912 | } |
913 | }; |
914 | |
915 | class VariadicEnumArgument: public VariadicArgument { |
916 | std::string type, QualifiedTypeName; |
917 | std::vector<StringRef> values, enums, uniques; |
918 | |
919 | protected: |
920 | void writeValueImpl(raw_ostream &OS) const override { |
921 | |
922 | |
923 | |
924 | OS << " OS << \"\\\"\" << " << getAttrName() << "Attr::Convert" << type |
925 | << "ToStr(Val)" << "<< \"\\\"\";\n"; |
926 | } |
927 | |
928 | public: |
929 | VariadicEnumArgument(const Record &Arg, StringRef Attr) |
930 | : VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")), |
931 | type(Arg.getValueAsString("Type")), |
932 | values(Arg.getValueAsListOfStrings("Values")), |
933 | enums(Arg.getValueAsListOfStrings("Enums")), |
934 | uniques(uniqueEnumsInOrder(enums)) |
935 | { |
936 | QualifiedTypeName = getAttrName().str() + "Attr::" + type; |
937 | |
938 | |
939 | assert(!uniques.empty()); |
940 | } |
941 | |
942 | bool isVariadicEnumArg() const override { return true; } |
943 | |
944 | void writeDeclarations(raw_ostream &OS) const override { |
945 | auto i = uniques.cbegin(), e = uniques.cend(); |
946 | |
947 | --e; |
948 | |
949 | OS << "public:\n"; |
950 | OS << " enum " << type << " {\n"; |
951 | for (; i != e; ++i) |
952 | OS << " " << *i << ",\n"; |
953 | OS << " " << *e << "\n"; |
954 | OS << " };\n"; |
955 | OS << "private:\n"; |
956 | |
957 | VariadicArgument::writeDeclarations(OS); |
958 | } |
959 | |
960 | void writeDump(raw_ostream &OS) const override { |
961 | OS << " for (" << getAttrName() << "Attr::" << getLowerName() |
962 | << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->" |
963 | << getLowerName() << "_end(); I != E; ++I) {\n"; |
964 | OS << " switch(*I) {\n"; |
965 | for (const auto &UI : uniques) { |
966 | OS << " case " << getAttrName() << "Attr::" << UI << ":\n"; |
967 | OS << " OS << \" " << UI << "\";\n"; |
968 | OS << " break;\n"; |
969 | } |
970 | OS << " }\n"; |
971 | OS << " }\n"; |
972 | } |
973 | |
974 | void writePCHReadDecls(raw_ostream &OS) const override { |
975 | OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n"; |
976 | OS << " SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName() |
977 | << ";\n"; |
978 | OS << " " << getLowerName() << ".reserve(" << getLowerName() |
979 | << "Size);\n"; |
980 | OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n"; |
981 | OS << " " << getLowerName() << ".push_back(" << "static_cast<" |
982 | << QualifiedTypeName << ">(Record.readInt()));\n"; |
983 | } |
984 | |
985 | void writePCHWrite(raw_ostream &OS) const override { |
986 | OS << " Record.push_back(SA->" << getLowerName() << "_size());\n"; |
987 | OS << " for (" << getAttrName() << "Attr::" << getLowerName() |
988 | << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->" |
989 | << getLowerName() << "_end(); i != e; ++i)\n"; |
990 | OS << " " << WritePCHRecord(QualifiedTypeName, "(*i)"); |
991 | } |
992 | |
993 | void writeConversion(raw_ostream &OS) const { |
994 | OS << " static bool ConvertStrTo" << type << "(StringRef Val, "; |
995 | OS << type << " &Out) {\n"; |
996 | OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<"; |
997 | OS << type << ">>(Val)\n"; |
998 | for (size_t I = 0; I < enums.size(); ++I) { |
999 | OS << " .Case(\"" << values[I] << "\", "; |
1000 | OS << getAttrName() << "Attr::" << enums[I] << ")\n"; |
1001 | } |
1002 | OS << " .Default(Optional<" << type << ">());\n"; |
1003 | OS << " if (R) {\n"; |
1004 | OS << " Out = *R;\n return true;\n }\n"; |
1005 | OS << " return false;\n"; |
1006 | OS << " }\n\n"; |
1007 | |
1008 | OS << " static const char *Convert" << type << "ToStr(" |
1009 | << type << " Val) {\n" |
1010 | << " switch(Val) {\n"; |
1011 | SmallDenseSet<StringRef, 8> Uniques; |
1012 | for (size_t I = 0; I < enums.size(); ++I) { |
1013 | if (Uniques.insert(enums[I]).second) |
1014 | OS << " case " << getAttrName() << "Attr::" << enums[I] |
1015 | << ": return \"" << values[I] << "\";\n"; |
1016 | } |
1017 | OS << " }\n" |
1018 | << " llvm_unreachable(\"No enumerator with that value\");\n" |
1019 | << " }\n"; |
1020 | } |
1021 | }; |
1022 | |
1023 | class VersionArgument : public Argument { |
1024 | public: |
1025 | VersionArgument(const Record &Arg, StringRef Attr) |
1026 | : Argument(Arg, Attr) |
1027 | {} |
1028 | |
1029 | void writeAccessors(raw_ostream &OS) const override { |
1030 | OS << " VersionTuple get" << getUpperName() << "() const {\n"; |
1031 | OS << " return " << getLowerName() << ";\n"; |
1032 | OS << " }\n"; |
1033 | OS << " void set" << getUpperName() |
1034 | << "(ASTContext &C, VersionTuple V) {\n"; |
1035 | OS << " " << getLowerName() << " = V;\n"; |
1036 | OS << " }"; |
1037 | } |
1038 | |
1039 | void writeCloneArgs(raw_ostream &OS) const override { |
1040 | OS << "get" << getUpperName() << "()"; |
1041 | } |
1042 | |
1043 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
1044 | OS << "A->get" << getUpperName() << "()"; |
1045 | } |
1046 | |
1047 | void writeCtorInitializers(raw_ostream &OS) const override { |
1048 | OS << getLowerName() << "(" << getUpperName() << ")"; |
1049 | } |
1050 | |
1051 | void writeCtorDefaultInitializers(raw_ostream &OS) const override { |
1052 | OS << getLowerName() << "()"; |
1053 | } |
1054 | |
1055 | void writeCtorParameters(raw_ostream &OS) const override { |
1056 | OS << "VersionTuple " << getUpperName(); |
1057 | } |
1058 | |
1059 | void writeDeclarations(raw_ostream &OS) const override { |
1060 | OS << "VersionTuple " << getLowerName() << ";\n"; |
1061 | } |
1062 | |
1063 | void writePCHReadDecls(raw_ostream &OS) const override { |
1064 | OS << " VersionTuple " << getLowerName() |
1065 | << "= Record.readVersionTuple();\n"; |
1066 | } |
1067 | |
1068 | void writePCHReadArgs(raw_ostream &OS) const override { |
1069 | OS << getLowerName(); |
1070 | } |
1071 | |
1072 | void writePCHWrite(raw_ostream &OS) const override { |
1073 | OS << " Record.AddVersionTuple(SA->get" << getUpperName() << "());\n"; |
1074 | } |
1075 | |
1076 | void writeValue(raw_ostream &OS) const override { |
1077 | OS << getLowerName() << "=\" << get" << getUpperName() << "() << \""; |
1078 | } |
1079 | |
1080 | void writeDump(raw_ostream &OS) const override { |
1081 | OS << " OS << \" \" << SA->get" << getUpperName() << "();\n"; |
1082 | } |
1083 | }; |
1084 | |
1085 | class ExprArgument : public SimpleArgument { |
1086 | public: |
1087 | ExprArgument(const Record &Arg, StringRef Attr) |
1088 | : SimpleArgument(Arg, Attr, "Expr *") |
1089 | {} |
1090 | |
1091 | void writeASTVisitorTraversal(raw_ostream &OS) const override { |
1092 | OS << " if (!" |
1093 | << "getDerived().TraverseStmt(A->get" << getUpperName() << "()))\n"; |
1094 | OS << " return false;\n"; |
1095 | } |
1096 | |
1097 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
1098 | OS << "tempInst" << getUpperName(); |
1099 | } |
1100 | |
1101 | void writeTemplateInstantiation(raw_ostream &OS) const override { |
1102 | OS << " " << getType() << " tempInst" << getUpperName() << ";\n"; |
1103 | OS << " {\n"; |
1104 | OS << " EnterExpressionEvaluationContext " |
1105 | << "Unevaluated(S, Sema::ExpressionEvaluationContext::Unevaluated);\n"; |
1106 | OS << " ExprResult " << "Result = S.SubstExpr(" |
1107 | << "A->get" << getUpperName() << "(), TemplateArgs);\n"; |
1108 | OS << " tempInst" << getUpperName() << " = " |
1109 | << "Result.getAs<Expr>();\n"; |
1110 | OS << " }\n"; |
1111 | } |
1112 | |
1113 | void writeDump(raw_ostream &OS) const override {} |
1114 | |
1115 | void writeDumpChildren(raw_ostream &OS) const override { |
1116 | OS << " Visit(SA->get" << getUpperName() << "());\n"; |
1117 | } |
1118 | |
1119 | void writeHasChildren(raw_ostream &OS) const override { OS << "true"; } |
1120 | }; |
1121 | |
1122 | class VariadicExprArgument : public VariadicArgument { |
1123 | public: |
1124 | VariadicExprArgument(const Record &Arg, StringRef Attr) |
1125 | : VariadicArgument(Arg, Attr, "Expr *") |
1126 | {} |
1127 | |
1128 | void writeASTVisitorTraversal(raw_ostream &OS) const override { |
1129 | OS << " {\n"; |
1130 | OS << " " << getType() << " *I = A->" << getLowerName() |
1131 | << "_begin();\n"; |
1132 | OS << " " << getType() << " *E = A->" << getLowerName() |
1133 | << "_end();\n"; |
1134 | OS << " for (; I != E; ++I) {\n"; |
1135 | OS << " if (!getDerived().TraverseStmt(*I))\n"; |
1136 | OS << " return false;\n"; |
1137 | OS << " }\n"; |
1138 | OS << " }\n"; |
1139 | } |
1140 | |
1141 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
1142 | OS << "tempInst" << getUpperName() << ", " |
1143 | << "A->" << getLowerName() << "_size()"; |
1144 | } |
1145 | |
1146 | void writeTemplateInstantiation(raw_ostream &OS) const override { |
1147 | OS << " auto *tempInst" << getUpperName() |
1148 | << " = new (C, 16) " << getType() |
1149 | << "[A->" << getLowerName() << "_size()];\n"; |
1150 | OS << " {\n"; |
1151 | OS << " EnterExpressionEvaluationContext " |
1152 | << "Unevaluated(S, Sema::ExpressionEvaluationContext::Unevaluated);\n"; |
1153 | OS << " " << getType() << " *TI = tempInst" << getUpperName() |
1154 | << ";\n"; |
1155 | OS << " " << getType() << " *I = A->" << getLowerName() |
1156 | << "_begin();\n"; |
1157 | OS << " " << getType() << " *E = A->" << getLowerName() |
1158 | << "_end();\n"; |
1159 | OS << " for (; I != E; ++I, ++TI) {\n"; |
1160 | OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n"; |
1161 | OS << " *TI = Result.getAs<Expr>();\n"; |
1162 | OS << " }\n"; |
1163 | OS << " }\n"; |
1164 | } |
1165 | |
1166 | void writeDump(raw_ostream &OS) const override {} |
1167 | |
1168 | void writeDumpChildren(raw_ostream &OS) const override { |
1169 | OS << " for (" << getAttrName() << "Attr::" << getLowerName() |
1170 | << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->" |
1171 | << getLowerName() << "_end(); I != E; ++I)\n"; |
1172 | OS << " Visit(*I);\n"; |
1173 | } |
1174 | |
1175 | void writeHasChildren(raw_ostream &OS) const override { |
1176 | OS << "SA->" << getLowerName() << "_begin() != " |
1177 | << "SA->" << getLowerName() << "_end()"; |
1178 | } |
1179 | }; |
1180 | |
1181 | class VariadicIdentifierArgument : public VariadicArgument { |
1182 | public: |
1183 | VariadicIdentifierArgument(const Record &Arg, StringRef Attr) |
1184 | : VariadicArgument(Arg, Attr, "IdentifierInfo *") |
1185 | {} |
1186 | }; |
1187 | |
1188 | class VariadicStringArgument : public VariadicArgument { |
1189 | public: |
1190 | VariadicStringArgument(const Record &Arg, StringRef Attr) |
1191 | : VariadicArgument(Arg, Attr, "StringRef") |
1192 | {} |
1193 | |
1194 | void writeCtorBody(raw_ostream &OS) const override { |
1195 | OS << " for (size_t I = 0, E = " << getArgSizeName() << "; I != E;\n" |
1196 | " ++I) {\n" |
1197 | " StringRef Ref = " << getUpperName() << "[I];\n" |
1198 | " if (!Ref.empty()) {\n" |
1199 | " char *Mem = new (Ctx, 1) char[Ref.size()];\n" |
1200 | " std::memcpy(Mem, Ref.data(), Ref.size());\n" |
1201 | " " << getArgName() << "[I] = StringRef(Mem, Ref.size());\n" |
1202 | " }\n" |
1203 | " }\n"; |
1204 | } |
1205 | |
1206 | void writeValueImpl(raw_ostream &OS) const override { |
1207 | OS << " OS << \"\\\"\" << Val << \"\\\"\";\n"; |
1208 | } |
1209 | }; |
1210 | |
1211 | class TypeArgument : public SimpleArgument { |
1212 | public: |
1213 | TypeArgument(const Record &Arg, StringRef Attr) |
1214 | : SimpleArgument(Arg, Attr, "TypeSourceInfo *") |
1215 | {} |
1216 | |
1217 | void writeAccessors(raw_ostream &OS) const override { |
1218 | OS << " QualType get" << getUpperName() << "() const {\n"; |
1219 | OS << " return " << getLowerName() << "->getType();\n"; |
1220 | OS << " }"; |
1221 | OS << " " << getType() << " get" << getUpperName() << "Loc() const {\n"; |
1222 | OS << " return " << getLowerName() << ";\n"; |
1223 | OS << " }"; |
1224 | } |
1225 | |
1226 | void writeASTVisitorTraversal(raw_ostream &OS) const override { |
1227 | OS << " if (auto *TSI = A->get" << getUpperName() << "Loc())\n"; |
1228 | OS << " if (!getDerived().TraverseTypeLoc(TSI->getTypeLoc()))\n"; |
1229 | OS << " return false;\n"; |
1230 | } |
1231 | |
1232 | void writeTemplateInstantiationArgs(raw_ostream &OS) const override { |
1233 | OS << "A->get" << getUpperName() << "Loc()"; |
1234 | } |
1235 | |
1236 | void writePCHWrite(raw_ostream &OS) const override { |
1237 | OS << " " << WritePCHRecord( |
1238 | getType(), "SA->get" + std::string(getUpperName()) + "Loc()"); |
1239 | } |
1240 | }; |
1241 | |
1242 | } |
1243 | |
1244 | static std::unique_ptr<Argument> |
1245 | createArgument(const Record &Arg, StringRef Attr, |
1246 | const Record *Search = nullptr) { |
1247 | if (!Search) |
1248 | Search = &Arg; |
1249 | |
1250 | std::unique_ptr<Argument> Ptr; |
1251 | llvm::StringRef ArgName = Search->getName(); |
1252 | |
1253 | if (ArgName == "AlignedArgument") |
1254 | Ptr = llvm::make_unique<AlignedArgument>(Arg, Attr); |
1255 | else if (ArgName == "EnumArgument") |
1256 | Ptr = llvm::make_unique<EnumArgument>(Arg, Attr); |
1257 | else if (ArgName == "ExprArgument") |
1258 | Ptr = llvm::make_unique<ExprArgument>(Arg, Attr); |
1259 | else if (ArgName == "FunctionArgument") |
1260 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "FunctionDecl *"); |
1261 | else if (ArgName == "NamedArgument") |
1262 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "NamedDecl *"); |
1263 | else if (ArgName == "IdentifierArgument") |
1264 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "IdentifierInfo *"); |
1265 | else if (ArgName == "DefaultBoolArgument") |
1266 | Ptr = llvm::make_unique<DefaultSimpleArgument>( |
1267 | Arg, Attr, "bool", Arg.getValueAsBit("Default")); |
1268 | else if (ArgName == "BoolArgument") |
1269 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "bool"); |
1270 | else if (ArgName == "DefaultIntArgument") |
1271 | Ptr = llvm::make_unique<DefaultSimpleArgument>( |
1272 | Arg, Attr, "int", Arg.getValueAsInt("Default")); |
1273 | else if (ArgName == "IntArgument") |
1274 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "int"); |
1275 | else if (ArgName == "StringArgument") |
1276 | Ptr = llvm::make_unique<StringArgument>(Arg, Attr); |
1277 | else if (ArgName == "TypeArgument") |
1278 | Ptr = llvm::make_unique<TypeArgument>(Arg, Attr); |
1279 | else if (ArgName == "UnsignedArgument") |
1280 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "unsigned"); |
1281 | else if (ArgName == "VariadicUnsignedArgument") |
1282 | Ptr = llvm::make_unique<VariadicArgument>(Arg, Attr, "unsigned"); |
1283 | else if (ArgName == "VariadicStringArgument") |
1284 | Ptr = llvm::make_unique<VariadicStringArgument>(Arg, Attr); |
1285 | else if (ArgName == "VariadicEnumArgument") |
1286 | Ptr = llvm::make_unique<VariadicEnumArgument>(Arg, Attr); |
1287 | else if (ArgName == "VariadicExprArgument") |
1288 | Ptr = llvm::make_unique<VariadicExprArgument>(Arg, Attr); |
1289 | else if (ArgName == "VariadicParamIdxArgument") |
1290 | Ptr = llvm::make_unique<VariadicParamIdxArgument>(Arg, Attr); |
1291 | else if (ArgName == "VariadicParamOrParamIdxArgument") |
1292 | Ptr = llvm::make_unique<VariadicParamOrParamIdxArgument>(Arg, Attr); |
1293 | else if (ArgName == "ParamIdxArgument") |
1294 | Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "ParamIdx"); |
1295 | else if (ArgName == "VariadicIdentifierArgument") |
1296 | Ptr = llvm::make_unique<VariadicIdentifierArgument>(Arg, Attr); |
1297 | else if (ArgName == "VersionArgument") |
1298 | Ptr = llvm::make_unique<VersionArgument>(Arg, Attr); |
1299 | |
1300 | if (!Ptr) { |
1301 | |
1302 | ArrayRef<std::pair<Record*, SMRange>> Bases = Search->getSuperClasses(); |
1303 | for (const auto &Base : llvm::reverse(Bases)) { |
1304 | if ((Ptr = createArgument(Arg, Attr, Base.first))) |
1305 | break; |
1306 | } |
1307 | } |
1308 | |
1309 | if (Ptr && Arg.getValueAsBit("Optional")) |
1310 | Ptr->setOptional(true); |
1311 | |
1312 | if (Ptr && Arg.getValueAsBit("Fake")) |
1313 | Ptr->setFake(true); |
1314 | |
1315 | return Ptr; |
1316 | } |
1317 | |
1318 | static void writeAvailabilityValue(raw_ostream &OS) { |
1319 | OS << "\" << getPlatform()->getName();\n" |
1320 | << " if (getStrict()) OS << \", strict\";\n" |
1321 | << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n" |
1322 | << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n" |
1323 | << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n" |
1324 | << " if (getUnavailable()) OS << \", unavailable\";\n" |
1325 | << " OS << \""; |
1326 | } |
1327 | |
1328 | static void writeDeprecatedAttrValue(raw_ostream &OS, std::string &Variety) { |
1329 | OS << "\\\"\" << getMessage() << \"\\\"\";\n"; |
1330 | |
1331 | if (Variety == "GNU") |
1332 | OS << " if (!getReplacement().empty()) OS << \", \\\"\"" |
1333 | " << getReplacement() << \"\\\"\";\n"; |
1334 | OS << " OS << \""; |
1335 | } |
1336 | |
1337 | static void writeGetSpellingFunction(Record &R, raw_ostream &OS) { |
1338 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); |
1339 | |
1340 | OS << "const char *" << R.getName() << "Attr::getSpelling() const {\n"; |
1341 | if (Spellings.empty()) { |
1342 | OS << " return \"(No spelling)\";\n}\n\n"; |
1343 | return; |
1344 | } |
1345 | |
1346 | OS << " switch (SpellingListIndex) {\n" |
1347 | " default:\n" |
1348 | " llvm_unreachable(\"Unknown attribute spelling!\");\n" |
1349 | " return \"(No spelling)\";\n"; |
1350 | |
1351 | for (unsigned I = 0; I < Spellings.size(); ++I) |
1352 | OS << " case " << I << ":\n" |
1353 | " return \"" << Spellings[I].name() << "\";\n"; |
1354 | |
1355 | OS << " }\n"; |
1356 | |
1357 | OS << "}\n\n"; |
1358 | } |
1359 | |
1360 | static void |
1361 | writePrettyPrintFunction(Record &R, |
1362 | const std::vector<std::unique_ptr<Argument>> &Args, |
1363 | raw_ostream &OS) { |
1364 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); |
1365 | |
1366 | OS << "void " << R.getName() << "Attr::printPretty(" |
1367 | << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n"; |
1368 | |
1369 | if (Spellings.empty()) { |
1370 | OS << "}\n\n"; |
1371 | return; |
1372 | } |
1373 | |
1374 | OS << |
1375 | " switch (SpellingListIndex) {\n" |
1376 | " default:\n" |
1377 | " llvm_unreachable(\"Unknown attribute spelling!\");\n" |
1378 | " break;\n"; |
1379 | |
1380 | for (unsigned I = 0; I < Spellings.size(); ++ I) { |
1381 | llvm::SmallString<16> Prefix; |
1382 | llvm::SmallString<8> Suffix; |
1383 | |
1384 | |
1385 | llvm::SmallString<64> Spelling; |
1386 | std::string Name = Spellings[I].name(); |
1387 | std::string Variety = Spellings[I].variety(); |
1388 | |
1389 | if (Variety == "GNU") { |
1390 | Prefix = " __attribute__(("; |
1391 | Suffix = "))"; |
1392 | } else if (Variety == "CXX11" || Variety == "C2x") { |
1393 | Prefix = " [["; |
1394 | Suffix = "]]"; |
1395 | std::string Namespace = Spellings[I].nameSpace(); |
1396 | if (!Namespace.empty()) { |
1397 | Spelling += Namespace; |
1398 | Spelling += "::"; |
1399 | } |
1400 | } else if (Variety == "Declspec") { |
1401 | Prefix = " __declspec("; |
1402 | Suffix = ")"; |
1403 | } else if (Variety == "Microsoft") { |
1404 | Prefix = "["; |
1405 | Suffix = "]"; |
1406 | } else if (Variety == "Keyword") { |
1407 | Prefix = " "; |
1408 | Suffix = ""; |
1409 | } else if (Variety == "Pragma") { |
1410 | Prefix = "#pragma "; |
1411 | Suffix = "\n"; |
1412 | std::string Namespace = Spellings[I].nameSpace(); |
1413 | if (!Namespace.empty()) { |
1414 | Spelling += Namespace; |
1415 | Spelling += " "; |
1416 | } |
1417 | } else { |
1418 | llvm_unreachable("Unknown attribute syntax variety!"); |
1419 | } |
1420 | |
1421 | Spelling += Name; |
1422 | |
1423 | OS << |
1424 | " case " << I << " : {\n" |
1425 | " OS << \"" << Prefix << Spelling; |
1426 | |
1427 | if (Variety == "Pragma") { |
1428 | OS << "\";\n"; |
1429 | OS << " printPrettyPragma(OS, Policy);\n"; |
1430 | OS << " OS << \"\\n\";"; |
1431 | OS << " break;\n"; |
1432 | OS << " }\n"; |
1433 | continue; |
1434 | } |
1435 | |
1436 | if (Spelling == "availability") { |
1437 | OS << "("; |
1438 | writeAvailabilityValue(OS); |
1439 | OS << ")"; |
1440 | } else if (Spelling == "deprecated" || Spelling == "gnu::deprecated") { |
1441 | OS << "("; |
1442 | writeDeprecatedAttrValue(OS, Variety); |
1443 | OS << ")"; |
1444 | } else { |
1445 | |
1446 | |
1447 | |
1448 | unsigned NonFakeArgs = 0; |
1449 | unsigned TrailingOptArgs = 0; |
1450 | bool FoundNonOptArg = false; |
1451 | for (const auto &arg : llvm::reverse(Args)) { |
1452 | if (arg->isFake()) |
1453 | continue; |
1454 | ++NonFakeArgs; |
1455 | if (FoundNonOptArg) |
1456 | continue; |
1457 | |
1458 | |
1459 | if (!arg->isOptional() || arg->getIsOmitted() == "false") { |
1460 | FoundNonOptArg = true; |
1461 | continue; |
1462 | } |
1463 | if (!TrailingOptArgs++) |
1464 | OS << "\";\n" |
1465 | << " unsigned TrailingOmittedArgs = 0;\n"; |
1466 | OS << " if (" << arg->getIsOmitted() << ")\n" |
1467 | << " ++TrailingOmittedArgs;\n"; |
1468 | } |
1469 | if (TrailingOptArgs) |
1470 | OS << " OS << \""; |
1471 | if (TrailingOptArgs < NonFakeArgs) |
1472 | OS << "("; |
1473 | else if (TrailingOptArgs) |
1474 | OS << "\";\n" |
1475 | << " if (TrailingOmittedArgs < " << NonFakeArgs << ")\n" |
1476 | << " OS << \"(\";\n" |
1477 | << " OS << \""; |
1478 | unsigned ArgIndex = 0; |
1479 | for (const auto &arg : Args) { |
1480 | if (arg->isFake()) |
1481 | continue; |
1482 | if (ArgIndex) { |
1483 | if (ArgIndex >= NonFakeArgs - TrailingOptArgs) |
1484 | OS << "\";\n" |
1485 | << " if (" << ArgIndex << " < " << NonFakeArgs |
1486 | << " - TrailingOmittedArgs)\n" |
1487 | << " OS << \", \";\n" |
1488 | << " OS << \""; |
1489 | else |
1490 | OS << ", "; |
1491 | } |
1492 | std::string IsOmitted = arg->getIsOmitted(); |
1493 | if (arg->isOptional() && IsOmitted != "false") |
1494 | OS << "\";\n" |
1495 | << " if (!(" << IsOmitted << ")) {\n" |
1496 | << " OS << \""; |
1497 | arg->writeValue(OS); |
1498 | if (arg->isOptional() && IsOmitted != "false") |
1499 | OS << "\";\n" |
1500 | << " }\n" |
1501 | << " OS << \""; |
1502 | ++ArgIndex; |
1503 | } |
1504 | if (TrailingOptArgs < NonFakeArgs) |
1505 | OS << ")"; |
1506 | else if (TrailingOptArgs) |
1507 | OS << "\";\n" |
1508 | << " if (TrailingOmittedArgs < " << NonFakeArgs << ")\n" |
1509 | << " OS << \")\";\n" |
1510 | << " OS << \""; |
1511 | } |
1512 | |
1513 | OS << Suffix + "\";\n"; |
1514 | |
1515 | OS << |
1516 | " break;\n" |
1517 | " }\n"; |
1518 | } |
1519 | |
1520 | |
1521 | OS << "}\n"; |
1522 | |
1523 | OS << "}\n\n"; |
1524 | } |
1525 | |
1526 | |
1527 | static unsigned |
1528 | getSpellingListIndex(const std::vector<FlattenedSpelling> &SpellingList, |
1529 | const FlattenedSpelling &Spelling) { |
1530 | (0) . __assert_fail ("!SpellingList.empty() && \"Spelling list is empty!\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 1530, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!SpellingList.empty() && "Spelling list is empty!"); |
1531 | |
1532 | for (unsigned Index = 0; Index < SpellingList.size(); ++Index) { |
1533 | const FlattenedSpelling &S = SpellingList[Index]; |
1534 | if (S.variety() != Spelling.variety()) |
1535 | continue; |
1536 | if (S.nameSpace() != Spelling.nameSpace()) |
1537 | continue; |
1538 | if (S.name() != Spelling.name()) |
1539 | continue; |
1540 | |
1541 | return Index; |
1542 | } |
1543 | |
1544 | llvm_unreachable("Unknown spelling!"); |
1545 | } |
1546 | |
1547 | static void writeAttrAccessorDefinition(const Record &R, raw_ostream &OS) { |
1548 | std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors"); |
1549 | if (Accessors.empty()) |
1550 | return; |
1551 | |
1552 | const std::vector<FlattenedSpelling> SpellingList = GetFlattenedSpellings(R); |
1553 | (0) . __assert_fail ("!SpellingList.empty() && \"Attribute with empty spelling list can't have accessors!\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 1554, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!SpellingList.empty() && |
1554 | (0) . __assert_fail ("!SpellingList.empty() && \"Attribute with empty spelling list can't have accessors!\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 1554, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Attribute with empty spelling list can't have accessors!"); |
1555 | for (const auto *Accessor : Accessors) { |
1556 | const StringRef Name = Accessor->getValueAsString("Name"); |
1557 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Accessor); |
1558 | |
1559 | OS << " bool " << Name << "() const { return SpellingListIndex == "; |
1560 | for (unsigned Index = 0; Index < Spellings.size(); ++Index) { |
1561 | OS << getSpellingListIndex(SpellingList, Spellings[Index]); |
1562 | if (Index != Spellings.size() - 1) |
1563 | OS << " ||\n SpellingListIndex == "; |
1564 | else |
1565 | OS << "; }\n"; |
1566 | } |
1567 | } |
1568 | } |
1569 | |
1570 | static bool |
1571 | SpellingNamesAreCommon(const std::vector<FlattenedSpelling>& Spellings) { |
1572 | (0) . __assert_fail ("!Spellings.empty() && \"An empty list of spellings was provided\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 1572, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Spellings.empty() && "An empty list of spellings was provided"); |
1573 | std::string FirstName = NormalizeNameForSpellingComparison( |
1574 | Spellings.front().name()); |
1575 | for (const auto &Spelling : |
1576 | llvm::make_range(std::next(Spellings.begin()), Spellings.end())) { |
1577 | std::string Name = NormalizeNameForSpellingComparison(Spelling.name()); |
1578 | if (Name != FirstName) |
1579 | return false; |
1580 | } |
1581 | return true; |
1582 | } |
1583 | |
1584 | typedef std::map<unsigned, std::string> SemanticSpellingMap; |
1585 | static std::string |
1586 | CreateSemanticSpellings(const std::vector<FlattenedSpelling> &Spellings, |
1587 | SemanticSpellingMap &Map) { |
1588 | |
1589 | |
1590 | |
1591 | |
1592 | std::string Ret(" enum Spelling {\n"); |
1593 | std::set<std::string> Uniques; |
1594 | unsigned Idx = 0; |
1595 | for (auto I = Spellings.begin(), E = Spellings.end(); I != E; ++I, ++Idx) { |
1596 | const FlattenedSpelling &S = *I; |
1597 | const std::string &Variety = S.variety(); |
1598 | const std::string &Spelling = S.name(); |
1599 | const std::string &Namespace = S.nameSpace(); |
1600 | std::string EnumName; |
1601 | |
1602 | EnumName += (Variety + "_"); |
1603 | if (!Namespace.empty()) |
1604 | EnumName += (NormalizeNameForSpellingComparison(Namespace).str() + |
1605 | "_"); |
1606 | EnumName += NormalizeNameForSpellingComparison(Spelling); |
1607 | |
1608 | |
1609 | |
1610 | Map[Idx] = EnumName; |
1611 | |
1612 | |
1613 | |
1614 | |
1615 | |
1616 | if (Uniques.find(EnumName) != Uniques.end()) |
1617 | continue; |
1618 | |
1619 | Uniques.insert(EnumName); |
1620 | if (I != Spellings.begin()) |
1621 | Ret += ",\n"; |
1622 | |
1623 | |
1624 | |
1625 | |
1626 | Ret += " " + EnumName + " = " + llvm::utostr(Idx); |
1627 | } |
1628 | Ret += "\n };\n\n"; |
1629 | return Ret; |
1630 | } |
1631 | |
1632 | void WriteSemanticSpellingSwitch(const std::string &VarName, |
1633 | const SemanticSpellingMap &Map, |
1634 | raw_ostream &OS) { |
1635 | OS << " switch (" << VarName << ") {\n default: " |
1636 | << "llvm_unreachable(\"Unknown spelling list index\");\n"; |
1637 | for (const auto &I : Map) |
1638 | OS << " case " << I.first << ": return " << I.second << ";\n"; |
1639 | OS << " }\n"; |
1640 | } |
1641 | |
1642 | |
1643 | static void emitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) { |
1644 | OS << "#if defined(CLANG_ATTR_LATE_PARSED_LIST)\n"; |
1645 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); |
1646 | |
1647 | for (const auto *Attr : Attrs) { |
1648 | bool LateParsed = Attr->getValueAsBit("LateParsed"); |
1649 | |
1650 | if (LateParsed) { |
1651 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr); |
1652 | |
1653 | |
1654 | for (const auto &I : Spellings) { |
1655 | if (I.variety() != "GNU") |
1656 | continue; |
1657 | OS << ".Case(\"" << I.name() << "\", " << LateParsed << ")\n"; |
1658 | } |
1659 | } |
1660 | } |
1661 | OS << "#endif // CLANG_ATTR_LATE_PARSED_LIST\n\n"; |
1662 | } |
1663 | |
1664 | static bool hasGNUorCXX11Spelling(const Record &Attribute) { |
1665 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attribute); |
1666 | for (const auto &I : Spellings) { |
1667 | if (I.variety() == "GNU" || I.variety() == "CXX11") |
1668 | return true; |
1669 | } |
1670 | return false; |
1671 | } |
1672 | |
1673 | namespace { |
1674 | |
1675 | struct AttributeSubjectMatchRule { |
1676 | const Record *MetaSubject; |
1677 | const Record *Constraint; |
1678 | |
1679 | AttributeSubjectMatchRule(const Record *MetaSubject, const Record *Constraint) |
1680 | : MetaSubject(MetaSubject), Constraint(Constraint) { |
1681 | (0) . __assert_fail ("MetaSubject && \"Missing subject\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 1681, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(MetaSubject && "Missing subject"); |
1682 | } |
1683 | |
1684 | bool isSubRule() const { return Constraint != nullptr; } |
1685 | |
1686 | std::vector<Record *> getSubjects() const { |
1687 | return (Constraint ? Constraint : MetaSubject) |
1688 | ->getValueAsListOfDefs("Subjects"); |
1689 | } |
1690 | |
1691 | std::vector<Record *> getLangOpts() const { |
1692 | if (Constraint) { |
1693 | |
1694 | |
1695 | std::vector<Record *> Opts = Constraint->getValueAsListOfDefs("LangOpts"); |
1696 | if (!Opts.empty()) |
1697 | return Opts; |
1698 | } |
1699 | return MetaSubject->getValueAsListOfDefs("LangOpts"); |
1700 | } |
1701 | |
1702 | |
1703 | bool isAbstractRule() const { return getSubjects().empty(); } |
1704 | |
1705 | StringRef getName() const { |
1706 | return (Constraint ? Constraint : MetaSubject)->getValueAsString("Name"); |
1707 | } |
1708 | |
1709 | bool isNegatedSubRule() const { |
1710 | (0) . __assert_fail ("isSubRule() && \"Not a sub-rule\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 1710, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(isSubRule() && "Not a sub-rule"); |
1711 | return Constraint->getValueAsBit("Negated"); |
1712 | } |
1713 | |
1714 | std::string getSpelling() const { |
1715 | std::string Result = MetaSubject->getValueAsString("Name"); |
1716 | if (isSubRule()) { |
1717 | Result += '('; |
1718 | if (isNegatedSubRule()) |
1719 | Result += "unless("; |
1720 | Result += getName(); |
1721 | if (isNegatedSubRule()) |
1722 | Result += ')'; |
1723 | Result += ')'; |
1724 | } |
1725 | return Result; |
1726 | } |
1727 | |
1728 | std::string getEnumValueName() const { |
1729 | SmallString<128> Result; |
1730 | Result += "SubjectMatchRule_"; |
1731 | Result += MetaSubject->getValueAsString("Name"); |
1732 | if (isSubRule()) { |
1733 | Result += "_"; |
1734 | if (isNegatedSubRule()) |
1735 | Result += "not_"; |
1736 | Result += Constraint->getValueAsString("Name"); |
1737 | } |
1738 | if (isAbstractRule()) |
1739 | Result += "_abstract"; |
1740 | return Result.str(); |
1741 | } |
1742 | |
1743 | std::string getEnumValue() const { return "attr::" + getEnumValueName(); } |
1744 | |
1745 | static const char *EnumName; |
1746 | }; |
1747 | |
1748 | const char *AttributeSubjectMatchRule::EnumName = "attr::SubjectMatchRule"; |
1749 | |
1750 | struct PragmaClangAttributeSupport { |
1751 | std::vector<AttributeSubjectMatchRule> Rules; |
1752 | |
1753 | class RuleOrAggregateRuleSet { |
1754 | std::vector<AttributeSubjectMatchRule> Rules; |
1755 | bool IsRule; |
1756 | RuleOrAggregateRuleSet(ArrayRef<AttributeSubjectMatchRule> Rules, |
1757 | bool IsRule) |
1758 | : Rules(Rules), IsRule(IsRule) {} |
1759 | |
1760 | public: |
1761 | bool isRule() const { return IsRule; } |
1762 | |
1763 | const AttributeSubjectMatchRule &getRule() const { |
1764 | (0) . __assert_fail ("IsRule && \"not a rule!\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 1764, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(IsRule && "not a rule!"); |
1765 | return Rules[0]; |
1766 | } |
1767 | |
1768 | ArrayRef<AttributeSubjectMatchRule> getAggregateRuleSet() const { |
1769 | return Rules; |
1770 | } |
1771 | |
1772 | static RuleOrAggregateRuleSet |
1773 | getRule(const AttributeSubjectMatchRule &Rule) { |
1774 | return RuleOrAggregateRuleSet(Rule, ); |
1775 | } |
1776 | static RuleOrAggregateRuleSet |
1777 | getAggregateRuleSet(ArrayRef<AttributeSubjectMatchRule> Rules) { |
1778 | return RuleOrAggregateRuleSet(Rules, ); |
1779 | } |
1780 | }; |
1781 | llvm::DenseMap<const Record *, RuleOrAggregateRuleSet> SubjectsToRules; |
1782 | |
1783 | PragmaClangAttributeSupport(RecordKeeper &Records); |
1784 | |
1785 | bool isAttributedSupported(const Record &Attribute); |
1786 | |
1787 | void emitMatchRuleList(raw_ostream &OS); |
1788 | |
1789 | std::string generateStrictConformsTo(const Record &Attr, raw_ostream &OS); |
1790 | |
1791 | void generateParsingHelpers(raw_ostream &OS); |
1792 | }; |
1793 | |
1794 | } |
1795 | |
1796 | static bool doesDeclDeriveFrom(const Record *D, const Record *Base) { |
1797 | const Record *CurrentBase = D->getValueAsDef("Base"); |
1798 | if (!CurrentBase) |
1799 | return false; |
1800 | if (CurrentBase == Base) |
1801 | return true; |
1802 | return doesDeclDeriveFrom(CurrentBase, Base); |
1803 | } |
1804 | |
1805 | PragmaClangAttributeSupport::PragmaClangAttributeSupport( |
1806 | RecordKeeper &Records) { |
1807 | std::vector<Record *> MetaSubjects = |
1808 | Records.getAllDerivedDefinitions("AttrSubjectMatcherRule"); |
1809 | auto MapFromSubjectsToRules = [this](const Record *SubjectContainer, |
1810 | const Record *MetaSubject, |
1811 | const Record *Constraint) { |
1812 | Rules.emplace_back(MetaSubject, Constraint); |
1813 | std::vector<Record *> ApplicableSubjects = |
1814 | SubjectContainer->getValueAsListOfDefs("Subjects"); |
1815 | for (const auto *Subject : ApplicableSubjects) { |
1816 | bool Inserted = |
1817 | SubjectsToRules |
1818 | .try_emplace(Subject, RuleOrAggregateRuleSet::getRule( |
1819 | AttributeSubjectMatchRule(MetaSubject, |
1820 | Constraint))) |
1821 | .second; |
1822 | if (!Inserted) { |
1823 | PrintFatalError("Attribute subject match rules should not represent" |
1824 | "same attribute subjects."); |
1825 | } |
1826 | } |
1827 | }; |
1828 | for (const auto *MetaSubject : MetaSubjects) { |
1829 | MapFromSubjectsToRules(MetaSubject, MetaSubject, ); |
1830 | std::vector<Record *> Constraints = |
1831 | MetaSubject->getValueAsListOfDefs("Constraints"); |
1832 | for (const auto *Constraint : Constraints) |
1833 | MapFromSubjectsToRules(Constraint, MetaSubject, Constraint); |
1834 | } |
1835 | |
1836 | std::vector<Record *> Aggregates = |
1837 | Records.getAllDerivedDefinitions("AttrSubjectMatcherAggregateRule"); |
1838 | std::vector<Record *> DeclNodes = Records.getAllDerivedDefinitions("DDecl"); |
1839 | for (const auto *Aggregate : Aggregates) { |
1840 | Record *SubjectDecl = Aggregate->getValueAsDef("Subject"); |
1841 | |
1842 | |
1843 | |
1844 | std::vector<AttributeSubjectMatchRule> Rules; |
1845 | for (const auto *D : DeclNodes) { |
1846 | if (doesDeclDeriveFrom(D, SubjectDecl)) { |
1847 | auto It = SubjectsToRules.find(D); |
1848 | if (It == SubjectsToRules.end()) |
1849 | continue; |
1850 | if (!It->second.isRule() || It->second.getRule().isSubRule()) |
1851 | continue; |
1852 | Rules.push_back(It->second.getRule()); |
1853 | } |
1854 | } |
1855 | |
1856 | bool Inserted = |
1857 | SubjectsToRules |
1858 | .try_emplace(SubjectDecl, |
1859 | RuleOrAggregateRuleSet::getAggregateRuleSet(Rules)) |
1860 | .second; |
1861 | if (!Inserted) { |
1862 | PrintFatalError("Attribute subject match rules should not represent" |
1863 | "same attribute subjects."); |
1864 | } |
1865 | } |
1866 | } |
1867 | |
1868 | static PragmaClangAttributeSupport & |
1869 | getPragmaAttributeSupport(RecordKeeper &Records) { |
1870 | static PragmaClangAttributeSupport Instance(Records); |
1871 | return Instance; |
1872 | } |
1873 | |
1874 | void PragmaClangAttributeSupport::emitMatchRuleList(raw_ostream &OS) { |
1875 | OS << "#ifndef ATTR_MATCH_SUB_RULE\n"; |
1876 | OS << "#define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, " |
1877 | "IsNegated) " |
1878 | << "ATTR_MATCH_RULE(Value, Spelling, IsAbstract)\n"; |
1879 | OS << "#endif\n"; |
1880 | for (const auto &Rule : Rules) { |
1881 | OS << (Rule.isSubRule() ? "ATTR_MATCH_SUB_RULE" : "ATTR_MATCH_RULE") << '('; |
1882 | OS << Rule.getEnumValueName() << ", \"" << Rule.getSpelling() << "\", " |
1883 | << Rule.isAbstractRule(); |
1884 | if (Rule.isSubRule()) |
1885 | OS << ", " |
1886 | << AttributeSubjectMatchRule(Rule.MetaSubject, nullptr).getEnumValue() |
1887 | << ", " << Rule.isNegatedSubRule(); |
1888 | OS << ")\n"; |
1889 | } |
1890 | OS << "#undef ATTR_MATCH_SUB_RULE\n"; |
1891 | } |
1892 | |
1893 | bool PragmaClangAttributeSupport::isAttributedSupported( |
1894 | const Record &Attribute) { |
1895 | |
1896 | |
1897 | bool Unset; |
1898 | bool SpecifiedResult = |
1899 | Attribute.getValueAsBitOrUnset("PragmaAttributeSupport", Unset); |
1900 | if (!Unset) |
1901 | return SpecifiedResult; |
1902 | |
1903 | |
1904 | |
1905 | if (Attribute.getValueAsBit("LateParsed")) |
1906 | return false; |
1907 | |
1908 | if (!hasGNUorCXX11Spelling(Attribute)) |
1909 | return false; |
1910 | |
1911 | |
1912 | if (Attribute.isValueUnset("Subjects")) |
1913 | return false; |
1914 | const Record *SubjectObj = Attribute.getValueAsDef("Subjects"); |
1915 | std::vector<Record *> Subjects = SubjectObj->getValueAsListOfDefs("Subjects"); |
1916 | if (Subjects.empty()) |
1917 | return false; |
1918 | for (const auto *Subject : Subjects) { |
1919 | if (SubjectsToRules.find(Subject) == SubjectsToRules.end()) |
1920 | return false; |
1921 | } |
1922 | return true; |
1923 | } |
1924 | |
1925 | std::string |
1926 | PragmaClangAttributeSupport::generateStrictConformsTo(const Record &Attr, |
1927 | raw_ostream &OS) { |
1928 | if (!isAttributedSupported(Attr)) |
1929 | return "nullptr"; |
1930 | |
1931 | |
1932 | std::string FnName = "matchRulesFor" + Attr.getName().str(); |
1933 | OS << "static void " << FnName << "(llvm::SmallVectorImpl<std::pair<" |
1934 | << AttributeSubjectMatchRule::EnumName |
1935 | << ", bool>> &MatchRules, const LangOptions &LangOpts) {\n"; |
1936 | if (Attr.isValueUnset("Subjects")) { |
1937 | OS << "}\n\n"; |
1938 | return FnName; |
1939 | } |
1940 | const Record *SubjectObj = Attr.getValueAsDef("Subjects"); |
1941 | std::vector<Record *> Subjects = SubjectObj->getValueAsListOfDefs("Subjects"); |
1942 | for (const auto *Subject : Subjects) { |
1943 | auto It = SubjectsToRules.find(Subject); |
1944 | (0) . __assert_fail ("It != SubjectsToRules.end() && \"This attribute is unsupported by #pragma clang attribute\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 1945, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(It != SubjectsToRules.end() && |
1945 | (0) . __assert_fail ("It != SubjectsToRules.end() && \"This attribute is unsupported by #pragma clang attribute\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 1945, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "This attribute is unsupported by #pragma clang attribute"); |
1946 | for (const auto &Rule : It->getSecond().getAggregateRuleSet()) { |
1947 | |
1948 | |
1949 | std::vector<Record *> LangOpts = Rule.getLangOpts(); |
1950 | OS << " MatchRules.push_back(std::make_pair(" << Rule.getEnumValue() |
1951 | << ", /*IsSupported=*/"; |
1952 | if (!LangOpts.empty()) { |
1953 | for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) { |
1954 | const StringRef Part = (*I)->getValueAsString("Name"); |
1955 | if ((*I)->getValueAsBit("Negated")) |
1956 | OS << "!"; |
1957 | OS << "LangOpts." << Part; |
1958 | if (I + 1 != E) |
1959 | OS << " || "; |
1960 | } |
1961 | } else |
1962 | OS << "true"; |
1963 | OS << "));\n"; |
1964 | } |
1965 | } |
1966 | OS << "}\n\n"; |
1967 | return FnName; |
1968 | } |
1969 | |
1970 | void PragmaClangAttributeSupport::generateParsingHelpers(raw_ostream &OS) { |
1971 | |
1972 | OS << "Optional<attr::SubjectMatchRule> " |
1973 | "defaultIsAttributeSubjectMatchSubRuleFor(StringRef, bool) {\n"; |
1974 | OS << " return None;\n"; |
1975 | OS << "}\n\n"; |
1976 | |
1977 | std::map<const Record *, std::vector<AttributeSubjectMatchRule>> |
1978 | SubMatchRules; |
1979 | for (const auto &Rule : Rules) { |
1980 | if (!Rule.isSubRule()) |
1981 | continue; |
1982 | SubMatchRules[Rule.MetaSubject].push_back(Rule); |
1983 | } |
1984 | |
1985 | for (const auto &SubMatchRule : SubMatchRules) { |
1986 | OS << "Optional<attr::SubjectMatchRule> isAttributeSubjectMatchSubRuleFor_" |
1987 | << SubMatchRule.first->getValueAsString("Name") |
1988 | << "(StringRef Name, bool IsUnless) {\n"; |
1989 | OS << " if (IsUnless)\n"; |
1990 | OS << " return " |
1991 | "llvm::StringSwitch<Optional<attr::SubjectMatchRule>>(Name).\n"; |
1992 | for (const auto &Rule : SubMatchRule.second) { |
1993 | if (Rule.isNegatedSubRule()) |
1994 | OS << " Case(\"" << Rule.getName() << "\", " << Rule.getEnumValue() |
1995 | << ").\n"; |
1996 | } |
1997 | OS << " Default(None);\n"; |
1998 | OS << " return " |
1999 | "llvm::StringSwitch<Optional<attr::SubjectMatchRule>>(Name).\n"; |
2000 | for (const auto &Rule : SubMatchRule.second) { |
2001 | if (!Rule.isNegatedSubRule()) |
2002 | OS << " Case(\"" << Rule.getName() << "\", " << Rule.getEnumValue() |
2003 | << ").\n"; |
2004 | } |
2005 | OS << " Default(None);\n"; |
2006 | OS << "}\n\n"; |
2007 | } |
2008 | |
2009 | |
2010 | OS << "std::pair<Optional<attr::SubjectMatchRule>, " |
2011 | "Optional<attr::SubjectMatchRule> (*)(StringRef, " |
2012 | "bool)> isAttributeSubjectMatchRule(StringRef Name) {\n"; |
2013 | OS << " return " |
2014 | "llvm::StringSwitch<std::pair<Optional<attr::SubjectMatchRule>, " |
2015 | "Optional<attr::SubjectMatchRule> (*) (StringRef, " |
2016 | "bool)>>(Name).\n"; |
2017 | for (const auto &Rule : Rules) { |
2018 | if (Rule.isSubRule()) |
2019 | continue; |
2020 | std::string SubRuleFunction; |
2021 | if (SubMatchRules.count(Rule.MetaSubject)) |
2022 | SubRuleFunction = |
2023 | ("isAttributeSubjectMatchSubRuleFor_" + Rule.getName()).str(); |
2024 | else |
2025 | SubRuleFunction = "defaultIsAttributeSubjectMatchSubRuleFor"; |
2026 | OS << " Case(\"" << Rule.getName() << "\", std::make_pair(" |
2027 | << Rule.getEnumValue() << ", " << SubRuleFunction << ")).\n"; |
2028 | } |
2029 | OS << " Default(std::make_pair(None, " |
2030 | "defaultIsAttributeSubjectMatchSubRuleFor));\n"; |
2031 | OS << "}\n\n"; |
2032 | |
2033 | |
2034 | OS << "const char *validAttributeSubjectMatchSubRules(" |
2035 | << AttributeSubjectMatchRule::EnumName << " Rule) {\n"; |
2036 | OS << " switch (Rule) {\n"; |
2037 | for (const auto &SubMatchRule : SubMatchRules) { |
2038 | OS << " case " |
2039 | << AttributeSubjectMatchRule(SubMatchRule.first, nullptr).getEnumValue() |
2040 | << ":\n"; |
2041 | OS << " return \"'"; |
2042 | bool IsFirst = true; |
2043 | for (const auto &Rule : SubMatchRule.second) { |
2044 | if (!IsFirst) |
2045 | OS << ", '"; |
2046 | IsFirst = false; |
2047 | if (Rule.isNegatedSubRule()) |
2048 | OS << "unless("; |
2049 | OS << Rule.getName(); |
2050 | if (Rule.isNegatedSubRule()) |
2051 | OS << ')'; |
2052 | OS << "'"; |
2053 | } |
2054 | OS << "\";\n"; |
2055 | } |
2056 | OS << " default: return nullptr;\n"; |
2057 | OS << " }\n"; |
2058 | OS << "}\n\n"; |
2059 | } |
2060 | |
2061 | template <typename Fn> |
2062 | static void forEachUniqueSpelling(const Record &Attr, Fn &&F) { |
2063 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr); |
2064 | SmallDenseSet<StringRef, 8> Seen; |
2065 | for (const FlattenedSpelling &S : Spellings) { |
2066 | if (Seen.insert(S.name()).second) |
2067 | F(S); |
2068 | } |
2069 | } |
2070 | |
2071 | |
2072 | static void emitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) { |
2073 | OS << "#if defined(CLANG_ATTR_TYPE_ARG_LIST)\n"; |
2074 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); |
2075 | |
2076 | for (const auto *Attr : Attrs) { |
2077 | |
2078 | std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args"); |
2079 | if (Args.empty()) |
2080 | continue; |
2081 | |
2082 | if (Args[0]->getSuperClasses().back().first->getName() != "TypeArgument") |
2083 | continue; |
2084 | |
2085 | |
2086 | forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) { |
2087 | OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n"; |
2088 | }); |
2089 | } |
2090 | OS << "#endif // CLANG_ATTR_TYPE_ARG_LIST\n\n"; |
2091 | } |
2092 | |
2093 | |
2094 | |
2095 | static void emitClangAttrArgContextList(RecordKeeper &Records, raw_ostream &OS) { |
2096 | OS << "#if defined(CLANG_ATTR_ARG_CONTEXT_LIST)\n"; |
2097 | ParsedAttrMap Attrs = getParsedAttrList(Records); |
2098 | for (const auto &I : Attrs) { |
2099 | const Record &Attr = *I.second; |
2100 | |
2101 | if (!Attr.getValueAsBit("ParseArgumentsAsUnevaluated")) |
2102 | continue; |
2103 | |
2104 | |
2105 | forEachUniqueSpelling(Attr, [&](const FlattenedSpelling &S) { |
2106 | OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n"; |
2107 | }); |
2108 | } |
2109 | OS << "#endif // CLANG_ATTR_ARG_CONTEXT_LIST\n\n"; |
2110 | } |
2111 | |
2112 | static bool isIdentifierArgument(Record *Arg) { |
2113 | return !Arg->getSuperClasses().empty() && |
2114 | llvm::StringSwitch<bool>(Arg->getSuperClasses().back().first->getName()) |
2115 | .Case("IdentifierArgument", true) |
2116 | .Case("EnumArgument", true) |
2117 | .Case("VariadicEnumArgument", true) |
2118 | .Default(false); |
2119 | } |
2120 | |
2121 | static bool isVariadicIdentifierArgument(Record *Arg) { |
2122 | return !Arg->getSuperClasses().empty() && |
2123 | llvm::StringSwitch<bool>( |
2124 | Arg->getSuperClasses().back().first->getName()) |
2125 | .Case("VariadicIdentifierArgument", true) |
2126 | .Case("VariadicParamOrParamIdxArgument", true) |
2127 | .Default(false); |
2128 | } |
2129 | |
2130 | static void emitClangAttrVariadicIdentifierArgList(RecordKeeper &Records, |
2131 | raw_ostream &OS) { |
2132 | OS << "#if defined(CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST)\n"; |
2133 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); |
2134 | for (const auto *A : Attrs) { |
2135 | |
2136 | std::vector<Record *> Args = A->getValueAsListOfDefs("Args"); |
2137 | if (Args.empty() || !isVariadicIdentifierArgument(Args[0])) |
2138 | continue; |
2139 | |
2140 | |
2141 | forEachUniqueSpelling(*A, [&](const FlattenedSpelling &S) { |
2142 | OS << ".Case(\"" << S.name() << "\", " |
2143 | << "true" |
2144 | << ")\n"; |
2145 | }); |
2146 | } |
2147 | OS << "#endif // CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST\n\n"; |
2148 | } |
2149 | |
2150 | |
2151 | static void emitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) { |
2152 | OS << "#if defined(CLANG_ATTR_IDENTIFIER_ARG_LIST)\n"; |
2153 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); |
2154 | |
2155 | for (const auto *Attr : Attrs) { |
2156 | |
2157 | std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args"); |
2158 | if (Args.empty() || !isIdentifierArgument(Args[0])) |
2159 | continue; |
2160 | |
2161 | |
2162 | forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) { |
2163 | OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n"; |
2164 | }); |
2165 | } |
2166 | OS << "#endif // CLANG_ATTR_IDENTIFIER_ARG_LIST\n\n"; |
2167 | } |
2168 | |
2169 | static bool keywordThisIsaIdentifierInArgument(const Record *Arg) { |
2170 | return !Arg->getSuperClasses().empty() && |
2171 | llvm::StringSwitch<bool>( |
2172 | Arg->getSuperClasses().back().first->getName()) |
2173 | .Case("VariadicParamOrParamIdxArgument", true) |
2174 | .Default(false); |
2175 | } |
2176 | |
2177 | static void emitClangAttrThisIsaIdentifierArgList(RecordKeeper &Records, |
2178 | raw_ostream &OS) { |
2179 | OS << "#if defined(CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST)\n"; |
2180 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); |
2181 | for (const auto *A : Attrs) { |
2182 | |
2183 | std::vector<Record *> Args = A->getValueAsListOfDefs("Args"); |
2184 | if (Args.empty() || !keywordThisIsaIdentifierInArgument(Args[0])) |
2185 | continue; |
2186 | |
2187 | |
2188 | forEachUniqueSpelling(*A, [&](const FlattenedSpelling &S) { |
2189 | OS << ".Case(\"" << S.name() << "\", " |
2190 | << "true" |
2191 | << ")\n"; |
2192 | }); |
2193 | } |
2194 | OS << "#endif // CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST\n\n"; |
2195 | } |
2196 | |
2197 | namespace clang { |
2198 | |
2199 | |
2200 | void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) { |
2201 | emitSourceFileHeader("Attribute classes' definitions", OS); |
2202 | |
2203 | OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n"; |
2204 | OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n"; |
2205 | |
2206 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); |
2207 | |
2208 | for (const auto *Attr : Attrs) { |
2209 | const Record &R = *Attr; |
2210 | |
2211 | |
2212 | |
2213 | |
2214 | |
2215 | |
2216 | |
2217 | |
2218 | |
2219 | |
2220 | (void)R.getValueAsListOfDefs("Documentation"); |
2221 | |
2222 | if (!R.getValueAsBit("ASTNode")) |
2223 | continue; |
2224 | |
2225 | ArrayRef<std::pair<Record *, SMRange>> Supers = R.getSuperClasses(); |
2226 | (0) . __assert_fail ("!Supers.empty() && \"Forgot to specify a superclass for the attr\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 2226, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Supers.empty() && "Forgot to specify a superclass for the attr"); |
2227 | std::string SuperName; |
2228 | bool Inheritable = false; |
2229 | for (const auto &Super : llvm::reverse(Supers)) { |
2230 | const Record *R = Super.first; |
2231 | if (R->getName() != "TargetSpecificAttr" && |
2232 | R->getName() != "DeclOrTypeAttr" && SuperName.empty()) |
2233 | SuperName = R->getName(); |
2234 | if (R->getName() == "InheritableAttr") |
2235 | Inheritable = true; |
2236 | } |
2237 | |
2238 | OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n"; |
2239 | |
2240 | std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args"); |
2241 | std::vector<std::unique_ptr<Argument>> Args; |
2242 | Args.reserve(ArgRecords.size()); |
2243 | |
2244 | bool HasOptArg = false; |
2245 | bool HasFakeArg = false; |
2246 | for (const auto *ArgRecord : ArgRecords) { |
2247 | Args.emplace_back(createArgument(*ArgRecord, R.getName())); |
2248 | Args.back()->writeDeclarations(OS); |
2249 | OS << "\n\n"; |
2250 | |
2251 | |
2252 | if (Args.back()->isFake()) { |
2253 | HasFakeArg = true; |
2254 | } else if (Args.back()->isOptional()) { |
2255 | HasOptArg = true; |
2256 | } |
2257 | } |
2258 | |
2259 | OS << "public:\n"; |
2260 | |
2261 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); |
2262 | |
2263 | |
2264 | |
2265 | |
2266 | bool ElideSpelling = (Spellings.size() <= 1) || |
2267 | SpellingNamesAreCommon(Spellings); |
2268 | |
2269 | |
2270 | SemanticSpellingMap SemanticToSyntacticMap; |
2271 | |
2272 | if (!ElideSpelling) |
2273 | OS << CreateSemanticSpellings(Spellings, SemanticToSyntacticMap); |
2274 | |
2275 | |
2276 | auto emitCreateImplicit = [&](bool emitFake) { |
2277 | OS << " static " << R.getName() << "Attr *CreateImplicit("; |
2278 | OS << "ASTContext &Ctx"; |
2279 | if (!ElideSpelling) |
2280 | OS << ", Spelling S"; |
2281 | for (auto const &ai : Args) { |
2282 | if (ai->isFake() && !emitFake) continue; |
2283 | OS << ", "; |
2284 | ai->writeCtorParameters(OS); |
2285 | } |
2286 | OS << ", SourceRange Loc = SourceRange()"; |
2287 | OS << ") {\n"; |
2288 | OS << " auto *A = new (Ctx) " << R.getName(); |
2289 | OS << "Attr(Loc, Ctx, "; |
2290 | for (auto const &ai : Args) { |
2291 | if (ai->isFake() && !emitFake) continue; |
2292 | ai->writeImplicitCtorArgs(OS); |
2293 | OS << ", "; |
2294 | } |
2295 | OS << (ElideSpelling ? "0" : "S") << ");\n"; |
2296 | OS << " A->setImplicit(true);\n"; |
2297 | OS << " return A;\n }\n\n"; |
2298 | }; |
2299 | |
2300 | |
2301 | emitCreateImplicit(true); |
2302 | |
2303 | |
2304 | if (HasFakeArg) { |
2305 | emitCreateImplicit(false); |
2306 | } |
2307 | |
2308 | |
2309 | auto emitCtor = [&](bool emitOpt, bool emitFake) { |
2310 | auto shouldEmitArg = [=](const std::unique_ptr<Argument> &arg) { |
2311 | if (arg->isFake()) return emitFake; |
2312 | if (arg->isOptional()) return emitOpt; |
2313 | return true; |
2314 | }; |
2315 | |
2316 | OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n"; |
2317 | for (auto const &ai : Args) { |
2318 | if (!shouldEmitArg(ai)) continue; |
2319 | OS << " , "; |
2320 | ai->writeCtorParameters(OS); |
2321 | OS << "\n"; |
2322 | } |
2323 | |
2324 | OS << " , "; |
2325 | OS << "unsigned SI\n"; |
2326 | |
2327 | OS << " )\n"; |
2328 | OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI, " |
2329 | << ( R.getValueAsBit("LateParsed") ? "true" : "false" ); |
2330 | if (Inheritable) { |
2331 | OS << ", " |
2332 | << (R.getValueAsBit("InheritEvenIfAlreadyPresent") ? "true" |
2333 | : "false"); |
2334 | } |
2335 | OS << ")\n"; |
2336 | |
2337 | for (auto const &ai : Args) { |
2338 | OS << " , "; |
2339 | if (!shouldEmitArg(ai)) { |
2340 | ai->writeCtorDefaultInitializers(OS); |
2341 | } else { |
2342 | ai->writeCtorInitializers(OS); |
2343 | } |
2344 | OS << "\n"; |
2345 | } |
2346 | |
2347 | OS << " {\n"; |
2348 | |
2349 | for (auto const &ai : Args) { |
2350 | if (!shouldEmitArg(ai)) continue; |
2351 | ai->writeCtorBody(OS); |
2352 | } |
2353 | OS << " }\n\n"; |
2354 | }; |
2355 | |
2356 | |
2357 | |
2358 | emitCtor(true, true); |
2359 | |
2360 | |
2361 | if (HasFakeArg) { |
2362 | emitCtor(true, false); |
2363 | } |
2364 | |
2365 | |
2366 | if (HasOptArg) { |
2367 | emitCtor(false, false); |
2368 | } |
2369 | |
2370 | OS << " " << R.getName() << "Attr *clone(ASTContext &C) const;\n"; |
2371 | OS << " void printPretty(raw_ostream &OS,\n" |
2372 | << " const PrintingPolicy &Policy) const;\n"; |
2373 | OS << " const char *getSpelling() const;\n"; |
2374 | |
2375 | if (!ElideSpelling) { |
2376 | (0) . __assert_fail ("!SemanticToSyntacticMap.empty() && \"Empty semantic mapping list\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 2376, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!SemanticToSyntacticMap.empty() && "Empty semantic mapping list"); |
2377 | OS << " Spelling getSemanticSpelling() const {\n"; |
2378 | WriteSemanticSpellingSwitch("SpellingListIndex", SemanticToSyntacticMap, |
2379 | OS); |
2380 | OS << " }\n"; |
2381 | } |
2382 | |
2383 | writeAttrAccessorDefinition(R, OS); |
2384 | |
2385 | for (auto const &ai : Args) { |
2386 | ai->writeAccessors(OS); |
2387 | OS << "\n\n"; |
2388 | |
2389 | |
2390 | if (ai->isFake()) continue; |
2391 | |
2392 | if (ai->isEnumArg()) |
2393 | static_cast<const EnumArgument *>(ai.get())->writeConversion(OS); |
2394 | else if (ai->isVariadicEnumArg()) |
2395 | static_cast<const VariadicEnumArgument *>(ai.get()) |
2396 | ->writeConversion(OS); |
2397 | } |
2398 | |
2399 | OS << R.getValueAsString("AdditionalMembers"); |
2400 | OS << "\n\n"; |
2401 | |
2402 | OS << " static bool classof(const Attr *A) { return A->getKind() == " |
2403 | << "attr::" << R.getName() << "; }\n"; |
2404 | |
2405 | OS << "};\n\n"; |
2406 | } |
2407 | |
2408 | OS << "#endif // LLVM_CLANG_ATTR_CLASSES_INC\n"; |
2409 | } |
2410 | |
2411 | |
2412 | void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) { |
2413 | emitSourceFileHeader("Attribute classes' member function definitions", OS); |
2414 | |
2415 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); |
2416 | |
2417 | for (auto *Attr : Attrs) { |
2418 | Record &R = *Attr; |
2419 | |
2420 | if (!R.getValueAsBit("ASTNode")) |
2421 | continue; |
2422 | |
2423 | std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args"); |
2424 | std::vector<std::unique_ptr<Argument>> Args; |
2425 | for (const auto *Arg : ArgRecords) |
2426 | Args.emplace_back(createArgument(*Arg, R.getName())); |
2427 | |
2428 | for (auto const &ai : Args) |
2429 | ai->writeAccessorDefinitions(OS); |
2430 | |
2431 | OS << R.getName() << "Attr *" << R.getName() |
2432 | << "Attr::clone(ASTContext &C) const {\n"; |
2433 | OS << " auto *A = new (C) " << R.getName() << "Attr(getLocation(), C"; |
2434 | for (auto const &ai : Args) { |
2435 | OS << ", "; |
2436 | ai->writeCloneArgs(OS); |
2437 | } |
2438 | OS << ", getSpellingListIndex());\n"; |
2439 | OS << " A->Inherited = Inherited;\n"; |
2440 | OS << " A->IsPackExpansion = IsPackExpansion;\n"; |
2441 | OS << " A->Implicit = Implicit;\n"; |
2442 | OS << " return A;\n}\n\n"; |
2443 | |
2444 | writePrettyPrintFunction(R, Args, OS); |
2445 | writeGetSpellingFunction(R, OS); |
2446 | } |
2447 | |
2448 | |
2449 | |
2450 | auto EmitFunc = [&](const char *Method) { |
2451 | OS << " switch (getKind()) {\n"; |
2452 | for (const auto *Attr : Attrs) { |
2453 | const Record &R = *Attr; |
2454 | if (!R.getValueAsBit("ASTNode")) |
2455 | continue; |
2456 | |
2457 | OS << " case attr::" << R.getName() << ":\n"; |
2458 | OS << " return cast<" << R.getName() << "Attr>(this)->" << Method |
2459 | << ";\n"; |
2460 | } |
2461 | OS << " }\n"; |
2462 | OS << " llvm_unreachable(\"Unexpected attribute kind!\");\n"; |
2463 | OS << "}\n\n"; |
2464 | }; |
2465 | |
2466 | OS << "const char *Attr::getSpelling() const {\n"; |
2467 | EmitFunc("getSpelling()"); |
2468 | |
2469 | OS << "Attr *Attr::clone(ASTContext &C) const {\n"; |
2470 | EmitFunc("clone(C)"); |
2471 | |
2472 | OS << "void Attr::printPretty(raw_ostream &OS, " |
2473 | "const PrintingPolicy &Policy) const {\n"; |
2474 | EmitFunc("printPretty(OS, Policy)"); |
2475 | } |
2476 | |
2477 | } |
2478 | |
2479 | static void emitAttrList(raw_ostream &OS, StringRef Class, |
2480 | const std::vector<Record*> &AttrList) { |
2481 | for (auto Cur : AttrList) { |
2482 | OS << Class << "(" << Cur->getName() << ")\n"; |
2483 | } |
2484 | } |
2485 | |
2486 | |
2487 | static bool AttrHasPragmaSpelling(const Record *R) { |
2488 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R); |
2489 | return llvm::find_if(Spellings, [](const FlattenedSpelling &S) { |
2490 | return S.variety() == "Pragma"; |
2491 | }) != Spellings.end(); |
2492 | } |
2493 | |
2494 | namespace { |
2495 | |
2496 | struct AttrClassDescriptor { |
2497 | const char * const MacroName; |
2498 | const char * const TableGenName; |
2499 | }; |
2500 | |
2501 | } |
2502 | |
2503 | static const AttrClassDescriptor AttrClassDescriptors[] = { |
2504 | { "ATTR", "Attr" }, |
2505 | { "TYPE_ATTR", "TypeAttr" }, |
2506 | { "STMT_ATTR", "StmtAttr" }, |
2507 | { "INHERITABLE_ATTR", "InheritableAttr" }, |
2508 | { "DECL_OR_TYPE_ATTR", "DeclOrTypeAttr" }, |
2509 | { "INHERITABLE_PARAM_ATTR", "InheritableParamAttr" }, |
2510 | { "PARAMETER_ABI_ATTR", "ParameterABIAttr" } |
2511 | }; |
2512 | |
2513 | static void emitDefaultDefine(raw_ostream &OS, StringRef name, |
2514 | const char *superName) { |
2515 | OS << "#ifndef " << name << "\n"; |
2516 | OS << "#define " << name << "(NAME) "; |
2517 | if (superName) OS << superName << "(NAME)"; |
2518 | OS << "\n#endif\n\n"; |
2519 | } |
2520 | |
2521 | namespace { |
2522 | |
2523 | |
2524 | struct AttrClass { |
2525 | const AttrClassDescriptor &Descriptor; |
2526 | Record *TheRecord; |
2527 | AttrClass *SuperClass = nullptr; |
2528 | std::vector<AttrClass*> SubClasses; |
2529 | std::vector<Record*> Attrs; |
2530 | |
2531 | AttrClass(const AttrClassDescriptor &Descriptor, Record *R) |
2532 | : Descriptor(Descriptor), TheRecord(R) {} |
2533 | |
2534 | void emitDefaultDefines(raw_ostream &OS) const { |
2535 | |
2536 | if (SuperClass) { |
2537 | emitDefaultDefine(OS, Descriptor.MacroName, |
2538 | SuperClass->Descriptor.MacroName); |
2539 | } |
2540 | } |
2541 | |
2542 | void emitUndefs(raw_ostream &OS) const { |
2543 | OS << "#undef " << Descriptor.MacroName << "\n"; |
2544 | } |
2545 | |
2546 | void emitAttrList(raw_ostream &OS) const { |
2547 | for (auto SubClass : SubClasses) { |
2548 | SubClass->emitAttrList(OS); |
2549 | } |
2550 | |
2551 | ::emitAttrList(OS, Descriptor.MacroName, Attrs); |
2552 | } |
2553 | |
2554 | void classifyAttrOnRoot(Record *Attr) { |
2555 | bool result = classifyAttr(Attr); |
2556 | (0) . __assert_fail ("result && \"failed to classify on root\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 2556, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(result && "failed to classify on root"); (void) result; |
2557 | } |
2558 | |
2559 | void emitAttrRange(raw_ostream &OS) const { |
2560 | OS << "ATTR_RANGE(" << Descriptor.TableGenName |
2561 | << ", " << getFirstAttr()->getName() |
2562 | << ", " << getLastAttr()->getName() << ")\n"; |
2563 | } |
2564 | |
2565 | private: |
2566 | bool classifyAttr(Record *Attr) { |
2567 | |
2568 | for (auto SubClass : SubClasses) { |
2569 | if (SubClass->classifyAttr(Attr)) |
2570 | return true; |
2571 | } |
2572 | |
2573 | |
2574 | if (Attr->isSubClassOf(TheRecord)) { |
2575 | Attrs.push_back(Attr); |
2576 | return true; |
2577 | } |
2578 | |
2579 | return false; |
2580 | } |
2581 | |
2582 | Record *getFirstAttr() const { |
2583 | if (!SubClasses.empty()) |
2584 | return SubClasses.front()->getFirstAttr(); |
2585 | return Attrs.front(); |
2586 | } |
2587 | |
2588 | Record *getLastAttr() const { |
2589 | if (!Attrs.empty()) |
2590 | return Attrs.back(); |
2591 | return SubClasses.back()->getLastAttr(); |
2592 | } |
2593 | }; |
2594 | |
2595 | |
2596 | class AttrClassHierarchy { |
2597 | std::vector<std::unique_ptr<AttrClass>> Classes; |
2598 | |
2599 | public: |
2600 | AttrClassHierarchy(RecordKeeper &Records) { |
2601 | |
2602 | for (auto &Descriptor : AttrClassDescriptors) { |
2603 | Record *ClassRecord = Records.getClass(Descriptor.TableGenName); |
2604 | AttrClass *Class = new AttrClass(Descriptor, ClassRecord); |
2605 | Classes.emplace_back(Class); |
2606 | } |
2607 | |
2608 | |
2609 | for (auto &Class : Classes) { |
2610 | if (AttrClass *SuperClass = findSuperClass(Class->TheRecord)) { |
2611 | Class->SuperClass = SuperClass; |
2612 | SuperClass->SubClasses.push_back(Class.get()); |
2613 | } |
2614 | } |
2615 | |
2616 | #ifndef NDEBUG |
2617 | for (auto i = Classes.begin(), e = Classes.end(); i != e; ++i) { |
2618 | (0) . __assert_fail ("(i == Classes.begin()) == ((*i)->SuperClass == nullptr) && \"only the first class should be a root class!\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 2619, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((i == Classes.begin()) == ((*i)->SuperClass == nullptr) && |
2619 | (0) . __assert_fail ("(i == Classes.begin()) == ((*i)->SuperClass == nullptr) && \"only the first class should be a root class!\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 2619, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "only the first class should be a root class!"); |
2620 | } |
2621 | #endif |
2622 | } |
2623 | |
2624 | void emitDefaultDefines(raw_ostream &OS) const { |
2625 | for (auto &Class : Classes) { |
2626 | Class->emitDefaultDefines(OS); |
2627 | } |
2628 | } |
2629 | |
2630 | void emitUndefs(raw_ostream &OS) const { |
2631 | for (auto &Class : Classes) { |
2632 | Class->emitUndefs(OS); |
2633 | } |
2634 | } |
2635 | |
2636 | void emitAttrLists(raw_ostream &OS) const { |
2637 | |
2638 | Classes[0]->emitAttrList(OS); |
2639 | } |
2640 | |
2641 | void emitAttrRanges(raw_ostream &OS) const { |
2642 | for (auto &Class : Classes) |
2643 | Class->emitAttrRange(OS); |
2644 | } |
2645 | |
2646 | void classifyAttr(Record *Attr) { |
2647 | |
2648 | Classes[0]->classifyAttrOnRoot(Attr); |
2649 | } |
2650 | |
2651 | private: |
2652 | AttrClass *findClassByRecord(Record *R) const { |
2653 | for (auto &Class : Classes) { |
2654 | if (Class->TheRecord == R) |
2655 | return Class.get(); |
2656 | } |
2657 | return nullptr; |
2658 | } |
2659 | |
2660 | AttrClass *findSuperClass(Record *R) const { |
2661 | |
2662 | |
2663 | auto SuperClasses = R->getSuperClasses(); |
2664 | for (signed i = 0, e = SuperClasses.size(); i != e; ++i) { |
2665 | auto SuperClass = findClassByRecord(SuperClasses[e - i - 1].first); |
2666 | if (SuperClass) return SuperClass; |
2667 | } |
2668 | return nullptr; |
2669 | } |
2670 | }; |
2671 | |
2672 | } |
2673 | |
2674 | namespace clang { |
2675 | |
2676 | |
2677 | void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) { |
2678 | emitSourceFileHeader("List of all attributes that Clang recognizes", OS); |
2679 | |
2680 | AttrClassHierarchy Hierarchy(Records); |
2681 | |
2682 | |
2683 | Hierarchy.emitDefaultDefines(OS); |
2684 | emitDefaultDefine(OS, "PRAGMA_SPELLING_ATTR", nullptr); |
2685 | |
2686 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); |
2687 | std::vector<Record *> PragmaAttrs; |
2688 | for (auto *Attr : Attrs) { |
2689 | if (!Attr->getValueAsBit("ASTNode")) |
2690 | continue; |
2691 | |
2692 | |
2693 | if (AttrHasPragmaSpelling(Attr)) |
2694 | PragmaAttrs.push_back(Attr); |
2695 | |
2696 | |
2697 | Hierarchy.classifyAttr(Attr); |
2698 | } |
2699 | |
2700 | |
2701 | Hierarchy.emitAttrLists(OS); |
2702 | |
2703 | |
2704 | emitAttrList(OS, "PRAGMA_SPELLING_ATTR", PragmaAttrs); |
2705 | |
2706 | |
2707 | OS << "#ifdef ATTR_RANGE\n"; |
2708 | Hierarchy.emitAttrRanges(OS); |
2709 | OS << "#undef ATTR_RANGE\n"; |
2710 | OS << "#endif\n"; |
2711 | |
2712 | Hierarchy.emitUndefs(OS); |
2713 | OS << "#undef PRAGMA_SPELLING_ATTR\n"; |
2714 | } |
2715 | |
2716 | |
2717 | void EmitClangAttrSubjectMatchRuleList(RecordKeeper &Records, raw_ostream &OS) { |
2718 | emitSourceFileHeader( |
2719 | "List of all attribute subject matching rules that Clang recognizes", OS); |
2720 | PragmaClangAttributeSupport &PragmaAttributeSupport = |
2721 | getPragmaAttributeSupport(Records); |
2722 | emitDefaultDefine(OS, "ATTR_MATCH_RULE", nullptr); |
2723 | PragmaAttributeSupport.emitMatchRuleList(OS); |
2724 | OS << "#undef ATTR_MATCH_RULE\n"; |
2725 | } |
2726 | |
2727 | |
2728 | void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) { |
2729 | emitSourceFileHeader("Attribute deserialization code", OS); |
2730 | |
2731 | Record *InhClass = Records.getClass("InheritableAttr"); |
2732 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), |
2733 | ArgRecords; |
2734 | std::vector<std::unique_ptr<Argument>> Args; |
2735 | |
2736 | OS << " switch (Kind) {\n"; |
2737 | for (const auto *Attr : Attrs) { |
2738 | const Record &R = *Attr; |
2739 | if (!R.getValueAsBit("ASTNode")) |
2740 | continue; |
2741 | |
2742 | OS << " case attr::" << R.getName() << ": {\n"; |
2743 | if (R.isSubClassOf(InhClass)) |
2744 | OS << " bool isInherited = Record.readInt();\n"; |
2745 | OS << " bool isImplicit = Record.readInt();\n"; |
2746 | OS << " unsigned Spelling = Record.readInt();\n"; |
2747 | ArgRecords = R.getValueAsListOfDefs("Args"); |
2748 | Args.clear(); |
2749 | for (const auto *Arg : ArgRecords) { |
2750 | Args.emplace_back(createArgument(*Arg, R.getName())); |
2751 | Args.back()->writePCHReadDecls(OS); |
2752 | } |
2753 | OS << " New = new (Context) " << R.getName() << "Attr(Range, Context"; |
2754 | for (auto const &ri : Args) { |
2755 | OS << ", "; |
2756 | ri->writePCHReadArgs(OS); |
2757 | } |
2758 | OS << ", Spelling);\n"; |
2759 | if (R.isSubClassOf(InhClass)) |
2760 | OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n"; |
2761 | OS << " New->setImplicit(isImplicit);\n"; |
2762 | OS << " break;\n"; |
2763 | OS << " }\n"; |
2764 | } |
2765 | OS << " }\n"; |
2766 | } |
2767 | |
2768 | |
2769 | void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) { |
2770 | emitSourceFileHeader("Attribute serialization code", OS); |
2771 | |
2772 | Record *InhClass = Records.getClass("InheritableAttr"); |
2773 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args; |
2774 | |
2775 | OS << " switch (A->getKind()) {\n"; |
2776 | for (const auto *Attr : Attrs) { |
2777 | const Record &R = *Attr; |
2778 | if (!R.getValueAsBit("ASTNode")) |
2779 | continue; |
2780 | OS << " case attr::" << R.getName() << ": {\n"; |
2781 | Args = R.getValueAsListOfDefs("Args"); |
2782 | if (R.isSubClassOf(InhClass) || !Args.empty()) |
2783 | OS << " const auto *SA = cast<" << R.getName() |
2784 | << "Attr>(A);\n"; |
2785 | if (R.isSubClassOf(InhClass)) |
2786 | OS << " Record.push_back(SA->isInherited());\n"; |
2787 | OS << " Record.push_back(A->isImplicit());\n"; |
2788 | OS << " Record.push_back(A->getSpellingListIndex());\n"; |
2789 | |
2790 | for (const auto *Arg : Args) |
2791 | createArgument(*Arg, R.getName())->writePCHWrite(OS); |
2792 | OS << " break;\n"; |
2793 | OS << " }\n"; |
2794 | } |
2795 | OS << " }\n"; |
2796 | } |
2797 | |
2798 | |
2799 | |
2800 | static void GenerateTargetSpecificAttrCheck(const Record *R, std::string &Test, |
2801 | std::string *FnName, |
2802 | StringRef ListName, |
2803 | StringRef CheckAgainst, |
2804 | StringRef Scope) { |
2805 | if (!R->isValueUnset(ListName)) { |
2806 | Test += " && ("; |
2807 | std::vector<StringRef> Items = R->getValueAsListOfStrings(ListName); |
2808 | for (auto I = Items.begin(), E = Items.end(); I != E; ++I) { |
2809 | StringRef Part = *I; |
2810 | Test += CheckAgainst; |
2811 | Test += " == "; |
2812 | Test += Scope; |
2813 | Test += Part; |
2814 | if (I + 1 != E) |
2815 | Test += " || "; |
2816 | if (FnName) |
2817 | *FnName += Part; |
2818 | } |
2819 | Test += ")"; |
2820 | } |
2821 | } |
2822 | |
2823 | |
2824 | |
2825 | |
2826 | |
2827 | |
2828 | static void GenerateTargetSpecificAttrChecks(const Record *R, |
2829 | std::vector<StringRef> &Arches, |
2830 | std::string &Test, |
2831 | std::string *FnName) { |
2832 | |
2833 | |
2834 | |
2835 | |
2836 | Test += "true"; |
2837 | |
2838 | |
2839 | |
2840 | if (!Arches.empty()) { |
2841 | Test += " && ("; |
2842 | for (auto I = Arches.begin(), E = Arches.end(); I != E; ++I) { |
2843 | StringRef Part = *I; |
2844 | Test += "T.getArch() == llvm::Triple::"; |
2845 | Test += Part; |
2846 | if (I + 1 != E) |
2847 | Test += " || "; |
2848 | if (FnName) |
2849 | *FnName += Part; |
2850 | } |
2851 | Test += ")"; |
2852 | } |
2853 | |
2854 | |
2855 | GenerateTargetSpecificAttrCheck(R, Test, FnName, "OSes", "T.getOS()", |
2856 | "llvm::Triple::"); |
2857 | |
2858 | |
2859 | GenerateTargetSpecificAttrCheck(R, Test, FnName, "CXXABIs", |
2860 | "Target.getCXXABI().getKind()", |
2861 | "TargetCXXABI::"); |
2862 | |
2863 | GenerateTargetSpecificAttrCheck(R, Test, FnName, "ObjectFormats", |
2864 | "T.getObjectFormat()", "llvm::Triple::"); |
2865 | } |
2866 | |
2867 | static void GenerateHasAttrSpellingStringSwitch( |
2868 | const std::vector<Record *> &Attrs, raw_ostream &OS, |
2869 | const std::string &Variety = "", const std::string &Scope = "") { |
2870 | for (const auto *Attr : Attrs) { |
2871 | |
2872 | |
2873 | |
2874 | |
2875 | |
2876 | |
2877 | int Version = 1; |
2878 | |
2879 | if (Variety == "CXX11") { |
2880 | std::vector<Record *> Spellings = Attr->getValueAsListOfDefs("Spellings"); |
2881 | for (const auto &Spelling : Spellings) { |
2882 | if (Spelling->getValueAsString("Variety") == "CXX11") { |
2883 | Version = static_cast<int>(Spelling->getValueAsInt("Version")); |
2884 | if (Scope.empty() && Version == 1) |
2885 | PrintError(Spelling->getLoc(), "C++ standard attributes must " |
2886 | "have valid version information."); |
2887 | break; |
2888 | } |
2889 | } |
2890 | } |
2891 | |
2892 | std::string Test; |
2893 | if (Attr->isSubClassOf("TargetSpecificAttr")) { |
2894 | const Record *R = Attr->getValueAsDef("Target"); |
2895 | std::vector<StringRef> Arches = R->getValueAsListOfStrings("Arches"); |
2896 | GenerateTargetSpecificAttrChecks(R, Arches, Test, nullptr); |
2897 | |
2898 | |
2899 | if (Variety == "CXX11") |
2900 | Test += " && LangOpts.CPlusPlus11"; |
2901 | else if (Variety == "C2x") |
2902 | Test += " && LangOpts.DoubleSquareBracketAttributes"; |
2903 | } else if (Variety == "CXX11") |
2904 | |
2905 | |
2906 | Test = "LangOpts.CPlusPlus11"; |
2907 | else if (Variety == "C2x") |
2908 | Test = "LangOpts.DoubleSquareBracketAttributes"; |
2909 | |
2910 | std::string TestStr = |
2911 | !Test.empty() ? Test + " ? " + llvm::itostr(Version) + " : 0" : "1"; |
2912 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr); |
2913 | for (const auto &S : Spellings) |
2914 | if (Variety.empty() || (Variety == S.variety() && |
2915 | (Scope.empty() || Scope == S.nameSpace()))) |
2916 | OS << " .Case(\"" << S.name() << "\", " << TestStr << ")\n"; |
2917 | } |
2918 | OS << " .Default(0);\n"; |
2919 | } |
2920 | |
2921 | |
2922 | void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) { |
2923 | emitSourceFileHeader("Code to implement the __has_attribute logic", OS); |
2924 | |
2925 | |
2926 | |
2927 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); |
2928 | std::vector<Record *> Declspec, Microsoft, GNU, Pragma; |
2929 | std::map<std::string, std::vector<Record *>> CXX, C2x; |
2930 | |
2931 | |
2932 | |
2933 | for (auto *R : Attrs) { |
2934 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R); |
2935 | for (const auto &SI : Spellings) { |
2936 | const std::string &Variety = SI.variety(); |
2937 | if (Variety == "GNU") |
2938 | GNU.push_back(R); |
2939 | else if (Variety == "Declspec") |
2940 | Declspec.push_back(R); |
2941 | else if (Variety == "Microsoft") |
2942 | Microsoft.push_back(R); |
2943 | else if (Variety == "CXX11") |
2944 | CXX[SI.nameSpace()].push_back(R); |
2945 | else if (Variety == "C2x") |
2946 | C2x[SI.nameSpace()].push_back(R); |
2947 | else if (Variety == "Pragma") |
2948 | Pragma.push_back(R); |
2949 | } |
2950 | } |
2951 | |
2952 | OS << "const llvm::Triple &T = Target.getTriple();\n"; |
2953 | OS << "switch (Syntax) {\n"; |
2954 | OS << "case AttrSyntax::GNU:\n"; |
2955 | OS << " return llvm::StringSwitch<int>(Name)\n"; |
2956 | GenerateHasAttrSpellingStringSwitch(GNU, OS, "GNU"); |
2957 | OS << "case AttrSyntax::Declspec:\n"; |
2958 | OS << " return llvm::StringSwitch<int>(Name)\n"; |
2959 | GenerateHasAttrSpellingStringSwitch(Declspec, OS, "Declspec"); |
2960 | OS << "case AttrSyntax::Microsoft:\n"; |
2961 | OS << " return llvm::StringSwitch<int>(Name)\n"; |
2962 | GenerateHasAttrSpellingStringSwitch(Microsoft, OS, "Microsoft"); |
2963 | OS << "case AttrSyntax::Pragma:\n"; |
2964 | OS << " return llvm::StringSwitch<int>(Name)\n"; |
2965 | GenerateHasAttrSpellingStringSwitch(Pragma, OS, "Pragma"); |
2966 | auto fn = [&OS](const char *Spelling, const char *Variety, |
2967 | const std::map<std::string, std::vector<Record *>> &List) { |
2968 | OS << "case AttrSyntax::" << Variety << ": {\n"; |
2969 | |
2970 | for (auto I = List.cbegin(), E = List.cend(); I != E; ++I) { |
2971 | if (I != List.cbegin()) |
2972 | OS << " else "; |
2973 | if (I->first.empty()) |
2974 | OS << "if (ScopeName == \"\") {\n"; |
2975 | else |
2976 | OS << "if (ScopeName == \"" << I->first << "\") {\n"; |
2977 | OS << " return llvm::StringSwitch<int>(Name)\n"; |
2978 | GenerateHasAttrSpellingStringSwitch(I->second, OS, Spelling, I->first); |
2979 | OS << "}"; |
2980 | } |
2981 | OS << "\n} break;\n"; |
2982 | }; |
2983 | fn("CXX11", "CXX", CXX); |
2984 | fn("C2x", "C", C2x); |
2985 | OS << "}\n"; |
2986 | } |
2987 | |
2988 | void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) { |
2989 | emitSourceFileHeader("Code to translate different attribute spellings " |
2990 | "into internal identifiers", OS); |
2991 | |
2992 | OS << " switch (AttrKind) {\n"; |
2993 | |
2994 | ParsedAttrMap Attrs = getParsedAttrList(Records); |
2995 | for (const auto &I : Attrs) { |
2996 | const Record &R = *I.second; |
2997 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); |
2998 | OS << " case AT_" << I.first << ": {\n"; |
2999 | for (unsigned I = 0; I < Spellings.size(); ++ I) { |
3000 | OS << " if (Name == \"" << Spellings[I].name() << "\" && " |
3001 | << "SyntaxUsed == " |
3002 | << StringSwitch<unsigned>(Spellings[I].variety()) |
3003 | .Case("GNU", 0) |
3004 | .Case("CXX11", 1) |
3005 | .Case("C2x", 2) |
3006 | .Case("Declspec", 3) |
3007 | .Case("Microsoft", 4) |
3008 | .Case("Keyword", 5) |
3009 | .Case("Pragma", 6) |
3010 | .Default(0) |
3011 | << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n" |
3012 | << " return " << I << ";\n"; |
3013 | } |
3014 | |
3015 | OS << " break;\n"; |
3016 | OS << " }\n"; |
3017 | } |
3018 | |
3019 | OS << " }\n"; |
3020 | OS << " return 0;\n"; |
3021 | } |
3022 | |
3023 | |
3024 | void EmitClangAttrASTVisitor(RecordKeeper &Records, raw_ostream &OS) { |
3025 | emitSourceFileHeader("Used by RecursiveASTVisitor to visit attributes.", OS); |
3026 | |
3027 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); |
3028 | |
3029 | |
3030 | |
3031 | |
3032 | OS << "#ifdef ATTR_VISITOR_DECLS_ONLY\n\n"; |
3033 | for (const auto *Attr : Attrs) { |
3034 | const Record &R = *Attr; |
3035 | if (!R.getValueAsBit("ASTNode")) |
3036 | continue; |
3037 | OS << " bool Traverse" |
3038 | << R.getName() << "Attr(" << R.getName() << "Attr *A);\n"; |
3039 | OS << " bool Visit" |
3040 | << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n" |
3041 | << " return true; \n" |
3042 | << " }\n"; |
3043 | } |
3044 | OS << "\n#else // ATTR_VISITOR_DECLS_ONLY\n\n"; |
3045 | |
3046 | |
3047 | for (const auto *Attr : Attrs) { |
3048 | const Record &R = *Attr; |
3049 | if (!R.getValueAsBit("ASTNode")) |
3050 | continue; |
3051 | |
3052 | OS << "template <typename Derived>\n" |
3053 | << "bool VISITORCLASS<Derived>::Traverse" |
3054 | << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n" |
3055 | << " if (!getDerived().VisitAttr(A))\n" |
3056 | << " return false;\n" |
3057 | << " if (!getDerived().Visit" << R.getName() << "Attr(A))\n" |
3058 | << " return false;\n"; |
3059 | |
3060 | std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args"); |
3061 | for (const auto *Arg : ArgRecords) |
3062 | createArgument(*Arg, R.getName())->writeASTVisitorTraversal(OS); |
3063 | |
3064 | OS << " return true;\n"; |
3065 | OS << "}\n\n"; |
3066 | } |
3067 | |
3068 | |
3069 | OS << "template <typename Derived>\n" |
3070 | << "bool VISITORCLASS<Derived>::TraverseAttr(Attr *A) {\n" |
3071 | << " if (!A)\n" |
3072 | << " return true;\n" |
3073 | << "\n" |
3074 | << " switch (A->getKind()) {\n"; |
3075 | |
3076 | for (const auto *Attr : Attrs) { |
3077 | const Record &R = *Attr; |
3078 | if (!R.getValueAsBit("ASTNode")) |
3079 | continue; |
3080 | |
3081 | OS << " case attr::" << R.getName() << ":\n" |
3082 | << " return getDerived().Traverse" << R.getName() << "Attr(" |
3083 | << "cast<" << R.getName() << "Attr>(A));\n"; |
3084 | } |
3085 | OS << " }\n"; |
3086 | OS << " llvm_unreachable(\"bad attribute kind\");\n"; |
3087 | OS << "}\n"; |
3088 | OS << "#endif // ATTR_VISITOR_DECLS_ONLY\n"; |
3089 | } |
3090 | |
3091 | void EmitClangAttrTemplateInstantiateHelper(const std::vector<Record *> &Attrs, |
3092 | raw_ostream &OS, |
3093 | bool AppliesToDecl) { |
3094 | |
3095 | OS << " switch (At->getKind()) {\n"; |
3096 | for (const auto *Attr : Attrs) { |
3097 | const Record &R = *Attr; |
3098 | if (!R.getValueAsBit("ASTNode")) |
3099 | continue; |
3100 | OS << " case attr::" << R.getName() << ": {\n"; |
3101 | bool ShouldClone = R.getValueAsBit("Clone") && |
3102 | (!AppliesToDecl || |
3103 | R.getValueAsBit("MeaningfulToClassTemplateDefinition")); |
3104 | |
3105 | if (!ShouldClone) { |
3106 | OS << " return nullptr;\n"; |
3107 | OS << " }\n"; |
3108 | continue; |
3109 | } |
3110 | |
3111 | OS << " const auto *A = cast<" |
3112 | << R.getName() << "Attr>(At);\n"; |
3113 | bool TDependent = R.getValueAsBit("TemplateDependent"); |
3114 | |
3115 | if (!TDependent) { |
3116 | OS << " return A->clone(C);\n"; |
3117 | OS << " }\n"; |
3118 | continue; |
3119 | } |
3120 | |
3121 | std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args"); |
3122 | std::vector<std::unique_ptr<Argument>> Args; |
3123 | Args.reserve(ArgRecords.size()); |
3124 | |
3125 | for (const auto *ArgRecord : ArgRecords) |
3126 | Args.emplace_back(createArgument(*ArgRecord, R.getName())); |
3127 | |
3128 | for (auto const &ai : Args) |
3129 | ai->writeTemplateInstantiation(OS); |
3130 | |
3131 | OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C"; |
3132 | for (auto const &ai : Args) { |
3133 | OS << ", "; |
3134 | ai->writeTemplateInstantiationArgs(OS); |
3135 | } |
3136 | OS << ", A->getSpellingListIndex());\n }\n"; |
3137 | } |
3138 | OS << " } // end switch\n" |
3139 | << " llvm_unreachable(\"Unknown attribute!\");\n" |
3140 | << " return nullptr;\n"; |
3141 | } |
3142 | |
3143 | |
3144 | void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) { |
3145 | emitSourceFileHeader("Template instantiation code for attributes", OS); |
3146 | |
3147 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"); |
3148 | |
3149 | OS << "namespace clang {\n" |
3150 | << "namespace sema {\n\n" |
3151 | << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, " |
3152 | << "Sema &S,\n" |
3153 | << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"; |
3154 | EmitClangAttrTemplateInstantiateHelper(Attrs, OS, ); |
3155 | OS << "}\n\n" |
3156 | << "Attr *instantiateTemplateAttributeForDecl(const Attr *At,\n" |
3157 | << " ASTContext &C, Sema &S,\n" |
3158 | << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"; |
3159 | EmitClangAttrTemplateInstantiateHelper(Attrs, OS, ); |
3160 | OS << "}\n\n" |
3161 | << "} // end namespace sema\n" |
3162 | << "} // end namespace clang\n"; |
3163 | } |
3164 | |
3165 | |
3166 | void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) { |
3167 | emitSourceFileHeader("List of all attributes that Clang recognizes", OS); |
3168 | |
3169 | OS << "#ifndef PARSED_ATTR\n"; |
3170 | OS << "#define PARSED_ATTR(NAME) NAME\n"; |
3171 | OS << "#endif\n\n"; |
3172 | |
3173 | ParsedAttrMap Names = getParsedAttrList(Records); |
3174 | for (const auto &I : Names) { |
3175 | OS << "PARSED_ATTR(" << I.first << ")\n"; |
3176 | } |
3177 | } |
3178 | |
3179 | static bool isArgVariadic(const Record &R, StringRef AttrName) { |
3180 | return createArgument(R, AttrName)->isVariadic(); |
3181 | } |
3182 | |
3183 | static void emitArgInfo(const Record &R, raw_ostream &OS) { |
3184 | |
3185 | |
3186 | |
3187 | std::vector<Record *> Args = R.getValueAsListOfDefs("Args"); |
3188 | unsigned ArgCount = 0, OptCount = 0; |
3189 | bool HasVariadic = false; |
3190 | for (const auto *Arg : Args) { |
3191 | |
3192 | |
3193 | if (Arg->getValueAsBit("Fake")) |
3194 | continue; |
3195 | Arg->getValueAsBit("Optional") ? ++OptCount : ++ArgCount; |
3196 | if (!HasVariadic && isArgVariadic(*Arg, R.getName())) |
3197 | HasVariadic = true; |
3198 | } |
3199 | |
3200 | |
3201 | |
3202 | OS << ArgCount << ", " << (HasVariadic ? 15 : OptCount); |
3203 | } |
3204 | |
3205 | static void GenerateDefaultAppertainsTo(raw_ostream &OS) { |
3206 | OS << "static bool defaultAppertainsTo(Sema &, const ParsedAttr &,"; |
3207 | OS << "const Decl *) {\n"; |
3208 | OS << " return true;\n"; |
3209 | OS << "}\n\n"; |
3210 | } |
3211 | |
3212 | static std::string GetDiagnosticSpelling(const Record &R) { |
3213 | std::string Ret = R.getValueAsString("DiagSpelling"); |
3214 | if (!Ret.empty()) |
3215 | return Ret; |
3216 | |
3217 | |
3218 | |
3219 | |
3220 | std::string Super = R.getSuperClasses().back().first->getName(); |
3221 | if (Super == "DDecl" || Super == "DStmt") |
3222 | return GetDiagnosticSpelling(*R.getValueAsDef("Base")); |
3223 | |
3224 | return ""; |
3225 | } |
3226 | |
3227 | static std::string CalculateDiagnostic(const Record &S) { |
3228 | |
3229 | |
3230 | const StringRef CustomDiag = S.getValueAsString("CustomDiag"); |
3231 | if (!CustomDiag.empty()) |
3232 | return ("\"" + Twine(CustomDiag) + "\"").str(); |
3233 | |
3234 | std::vector<std::string> DiagList; |
3235 | std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects"); |
3236 | for (const auto *Subject : Subjects) { |
3237 | const Record &R = *Subject; |
3238 | |
3239 | std::string V = GetDiagnosticSpelling(R); |
3240 | if (V.empty()) { |
3241 | PrintError(R.getLoc(), |
3242 | "Could not determine diagnostic spelling for the node: " + |
3243 | R.getName() + "; please add one to DeclNodes.td"); |
3244 | } else { |
3245 | |
3246 | |
3247 | SmallVector<StringRef, 2> Frags; |
3248 | llvm::SplitString(V, Frags, ","); |
3249 | for (auto Str : Frags) { |
3250 | DiagList.push_back(Str.trim()); |
3251 | } |
3252 | } |
3253 | } |
3254 | |
3255 | if (DiagList.empty()) { |
3256 | PrintFatalError(S.getLoc(), |
3257 | "Could not deduce diagnostic argument for Attr subjects"); |
3258 | return ""; |
3259 | } |
3260 | |
3261 | |
3262 | |
3263 | |
3264 | |
3265 | |
3266 | if (DiagList.size() == 1) |
3267 | return '"' + DiagList.front() + '"'; |
3268 | |
3269 | if (DiagList.size() == 2) |
3270 | return '"' + DiagList[0] + " and " + DiagList[1] + '"'; |
3271 | |
3272 | |
3273 | |
3274 | |
3275 | |
3276 | std::string Diag = llvm::join(DiagList.begin(), DiagList.end() - 1, ", "); |
3277 | return '"' + Diag + ", and " + *(DiagList.end() - 1) + '"'; |
3278 | } |
3279 | |
3280 | static std::string GetSubjectWithSuffix(const Record *R) { |
3281 | const std::string &B = R->getName(); |
3282 | if (B == "DeclBase") |
3283 | return "Decl"; |
3284 | return B + "Decl"; |
3285 | } |
3286 | |
3287 | static std::string functionNameForCustomAppertainsTo(const Record &Subject) { |
3288 | return "is" + Subject.getName().str(); |
3289 | } |
3290 | |
3291 | static std::string GenerateCustomAppertainsTo(const Record &Subject, |
3292 | raw_ostream &OS) { |
3293 | std::string FnName = functionNameForCustomAppertainsTo(Subject); |
3294 | |
3295 | |
3296 | |
3297 | static std::set<std::string> CustomSubjectSet; |
3298 | auto I = CustomSubjectSet.find(FnName); |
3299 | if (I != CustomSubjectSet.end()) |
3300 | return *I; |
3301 | |
3302 | Record *Base = Subject.getValueAsDef("Base"); |
3303 | |
3304 | |
3305 | if (Base->isSubClassOf("SubsetSubject")) { |
3306 | PrintFatalError(Subject.getLoc(), |
3307 | "SubsetSubjects within SubsetSubjects is not supported"); |
3308 | return ""; |
3309 | } |
3310 | |
3311 | OS << "static bool " << FnName << "(const Decl *D) {\n"; |
3312 | OS << " if (const auto *S = dyn_cast<"; |
3313 | OS << GetSubjectWithSuffix(Base); |
3314 | OS << ">(D))\n"; |
3315 | OS << " return " << Subject.getValueAsString("CheckCode") << ";\n"; |
3316 | OS << " return false;\n"; |
3317 | OS << "}\n\n"; |
3318 | |
3319 | CustomSubjectSet.insert(FnName); |
3320 | return FnName; |
3321 | } |
3322 | |
3323 | static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) { |
3324 | |
3325 | |
3326 | if (Attr.isValueUnset("Subjects")) |
3327 | return "defaultAppertainsTo"; |
3328 | |
3329 | const Record *SubjectObj = Attr.getValueAsDef("Subjects"); |
3330 | std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects"); |
3331 | |
3332 | |
3333 | |
3334 | if (Subjects.empty()) |
3335 | return "defaultAppertainsTo"; |
3336 | |
3337 | bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn"); |
3338 | |
3339 | |
3340 | |
3341 | |
3342 | |
3343 | |
3344 | |
3345 | |
3346 | |
3347 | std::string FnName = "check" + Attr.getName().str() + "AppertainsTo"; |
3348 | std::stringstream SS; |
3349 | SS << "static bool " << FnName << "(Sema &S, const ParsedAttr &Attr, "; |
3350 | SS << "const Decl *D) {\n"; |
3351 | SS << " if (!D || ("; |
3352 | for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) { |
3353 | |
3354 | |
3355 | |
3356 | |
3357 | |
3358 | if ((*I)->isSubClassOf("SubsetSubject")) { |
3359 | SS << "!" << GenerateCustomAppertainsTo(**I, OS) << "(D)"; |
3360 | } else { |
3361 | SS << "!isa<" << GetSubjectWithSuffix(*I) << ">(D)"; |
3362 | } |
3363 | |
3364 | if (I + 1 != E) |
3365 | SS << " && "; |
3366 | } |
3367 | SS << ")) {\n"; |
3368 | SS << " S.Diag(Attr.getLoc(), diag::"; |
3369 | SS << (Warn ? "warn_attribute_wrong_decl_type_str" : |
3370 | "err_attribute_wrong_decl_type_str"); |
3371 | SS << ")\n"; |
3372 | SS << " << Attr << "; |
3373 | SS << CalculateDiagnostic(*SubjectObj) << ";\n"; |
3374 | SS << " return false;\n"; |
3375 | SS << " }\n"; |
3376 | SS << " return true;\n"; |
3377 | SS << "}\n\n"; |
3378 | |
3379 | OS << SS.str(); |
3380 | return FnName; |
3381 | } |
3382 | |
3383 | static void |
3384 | emitAttributeMatchRules(PragmaClangAttributeSupport &PragmaAttributeSupport, |
3385 | raw_ostream &OS) { |
3386 | OS << "static bool checkAttributeMatchRuleAppliesTo(const Decl *D, " |
3387 | << AttributeSubjectMatchRule::EnumName << " rule) {\n"; |
3388 | OS << " switch (rule) {\n"; |
3389 | for (const auto &Rule : PragmaAttributeSupport.Rules) { |
3390 | if (Rule.isAbstractRule()) { |
3391 | OS << " case " << Rule.getEnumValue() << ":\n"; |
3392 | OS << " assert(false && \"Abstract matcher rule isn't allowed\");\n"; |
3393 | OS << " return false;\n"; |
3394 | continue; |
3395 | } |
3396 | std::vector<Record *> Subjects = Rule.getSubjects(); |
3397 | (0) . __assert_fail ("!Subjects.empty() && \"Missing subjects\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 3397, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!Subjects.empty() && "Missing subjects"); |
3398 | OS << " case " << Rule.getEnumValue() << ":\n"; |
3399 | OS << " return "; |
3400 | for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) { |
3401 | |
3402 | |
3403 | |
3404 | if ((*I)->isSubClassOf("SubsetSubject")) |
3405 | OS << functionNameForCustomAppertainsTo(**I) << "(D)"; |
3406 | else |
3407 | OS << "isa<" << GetSubjectWithSuffix(*I) << ">(D)"; |
3408 | |
3409 | if (I + 1 != E) |
3410 | OS << " || "; |
3411 | } |
3412 | OS << ";\n"; |
3413 | } |
3414 | OS << " }\n"; |
3415 | OS << " llvm_unreachable(\"Invalid match rule\");\nreturn false;\n"; |
3416 | OS << "}\n\n"; |
3417 | } |
3418 | |
3419 | static void GenerateDefaultLangOptRequirements(raw_ostream &OS) { |
3420 | OS << "static bool defaultDiagnoseLangOpts(Sema &, "; |
3421 | OS << "const ParsedAttr &) {\n"; |
3422 | OS << " return true;\n"; |
3423 | OS << "}\n\n"; |
3424 | } |
3425 | |
3426 | static std::string GenerateLangOptRequirements(const Record &R, |
3427 | raw_ostream &OS) { |
3428 | |
3429 | |
3430 | std::vector<Record *> LangOpts = R.getValueAsListOfDefs("LangOpts"); |
3431 | if (LangOpts.empty()) |
3432 | return "defaultDiagnoseLangOpts"; |
3433 | |
3434 | |
3435 | |
3436 | |
3437 | |
3438 | std::string FnName = "check", Test; |
3439 | for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) { |
3440 | const StringRef Part = (*I)->getValueAsString("Name"); |
3441 | if ((*I)->getValueAsBit("Negated")) { |
3442 | FnName += "Not"; |
3443 | Test += "!"; |
3444 | } |
3445 | Test += "S.LangOpts."; |
3446 | Test += Part; |
3447 | if (I + 1 != E) |
3448 | Test += " || "; |
3449 | FnName += Part; |
3450 | } |
3451 | FnName += "LangOpts"; |
3452 | |
3453 | |
3454 | |
3455 | static std::set<std::string> CustomLangOptsSet; |
3456 | auto I = CustomLangOptsSet.find(FnName); |
3457 | if (I != CustomLangOptsSet.end()) |
3458 | return *I; |
3459 | |
3460 | OS << "static bool " << FnName << "(Sema &S, const ParsedAttr &Attr) {\n"; |
3461 | OS << " if (" << Test << ")\n"; |
3462 | OS << " return true;\n\n"; |
3463 | OS << " S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) "; |
3464 | OS << "<< Attr.getName();\n"; |
3465 | OS << " return false;\n"; |
3466 | OS << "}\n\n"; |
3467 | |
3468 | CustomLangOptsSet.insert(FnName); |
3469 | return FnName; |
3470 | } |
3471 | |
3472 | static void GenerateDefaultTargetRequirements(raw_ostream &OS) { |
3473 | OS << "static bool defaultTargetRequirements(const TargetInfo &) {\n"; |
3474 | OS << " return true;\n"; |
3475 | OS << "}\n\n"; |
3476 | } |
3477 | |
3478 | static std::string GenerateTargetRequirements(const Record &Attr, |
3479 | const ParsedAttrMap &Dupes, |
3480 | raw_ostream &OS) { |
3481 | |
3482 | |
3483 | if (!Attr.isSubClassOf("TargetSpecificAttr")) |
3484 | return "defaultTargetRequirements"; |
3485 | |
3486 | |
3487 | const Record *R = Attr.getValueAsDef("Target"); |
3488 | std::vector<StringRef> Arches = R->getValueAsListOfStrings("Arches"); |
3489 | |
3490 | |
3491 | |
3492 | |
3493 | |
3494 | |
3495 | |
3496 | if (!Attr.isValueUnset("ParseKind")) { |
3497 | const StringRef APK = Attr.getValueAsString("ParseKind"); |
3498 | for (const auto &I : Dupes) { |
3499 | if (I.first == APK) { |
3500 | std::vector<StringRef> DA = |
3501 | I.second->getValueAsDef("Target")->getValueAsListOfStrings( |
3502 | "Arches"); |
3503 | Arches.insert(Arches.end(), DA.begin(), DA.end()); |
3504 | } |
3505 | } |
3506 | } |
3507 | |
3508 | std::string FnName = "isTarget"; |
3509 | std::string Test; |
3510 | GenerateTargetSpecificAttrChecks(R, Arches, Test, &FnName); |
3511 | |
3512 | |
3513 | |
3514 | static std::set<std::string> CustomTargetSet; |
3515 | auto I = CustomTargetSet.find(FnName); |
3516 | if (I != CustomTargetSet.end()) |
3517 | return *I; |
3518 | |
3519 | OS << "static bool " << FnName << "(const TargetInfo &Target) {\n"; |
3520 | OS << " const llvm::Triple &T = Target.getTriple();\n"; |
3521 | OS << " return " << Test << ";\n"; |
3522 | OS << "}\n\n"; |
3523 | |
3524 | CustomTargetSet.insert(FnName); |
3525 | return FnName; |
3526 | } |
3527 | |
3528 | static void GenerateDefaultSpellingIndexToSemanticSpelling(raw_ostream &OS) { |
3529 | OS << "static unsigned defaultSpellingIndexToSemanticSpelling(" |
3530 | << "const ParsedAttr &Attr) {\n"; |
3531 | OS << " return UINT_MAX;\n"; |
3532 | OS << "}\n\n"; |
3533 | } |
3534 | |
3535 | static std::string GenerateSpellingIndexToSemanticSpelling(const Record &Attr, |
3536 | raw_ostream &OS) { |
3537 | |
3538 | if (!Attr.getValueAsBit("ASTNode")) |
3539 | return "defaultSpellingIndexToSemanticSpelling"; |
3540 | |
3541 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr); |
3542 | |
3543 | |
3544 | |
3545 | if (Spellings.size() <= 1 || SpellingNamesAreCommon(Spellings)) |
3546 | return "defaultSpellingIndexToSemanticSpelling"; |
3547 | |
3548 | |
3549 | SemanticSpellingMap SemanticToSyntacticMap; |
3550 | std::string Enum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap); |
3551 | std::string Name = Attr.getName().str() + "AttrSpellingMap"; |
3552 | |
3553 | OS << "static unsigned " << Name << "(const ParsedAttr &Attr) {\n"; |
3554 | OS << Enum; |
3555 | OS << " unsigned Idx = Attr.getAttributeSpellingListIndex();\n"; |
3556 | WriteSemanticSpellingSwitch("Idx", SemanticToSyntacticMap, OS); |
3557 | OS << "}\n\n"; |
3558 | |
3559 | return Name; |
3560 | } |
3561 | |
3562 | static bool IsKnownToGCC(const Record &Attr) { |
3563 | |
3564 | |
3565 | return llvm::any_of( |
3566 | GetFlattenedSpellings(Attr), |
3567 | [](const FlattenedSpelling &S) { return S.knownToGCC(); }); |
3568 | } |
3569 | |
3570 | |
3571 | void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) { |
3572 | emitSourceFileHeader("Parsed attribute helpers", OS); |
3573 | |
3574 | PragmaClangAttributeSupport &PragmaAttributeSupport = |
3575 | getPragmaAttributeSupport(Records); |
3576 | |
3577 | |
3578 | |
3579 | ParsedAttrMap Dupes; |
3580 | ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes); |
3581 | |
3582 | |
3583 | |
3584 | GenerateDefaultAppertainsTo(OS); |
3585 | GenerateDefaultLangOptRequirements(OS); |
3586 | GenerateDefaultTargetRequirements(OS); |
3587 | GenerateDefaultSpellingIndexToSemanticSpelling(OS); |
3588 | |
3589 | |
3590 | |
3591 | |
3592 | |
3593 | std::string Buffer; |
3594 | raw_string_ostream SS {Buffer}; |
3595 | for (auto I = Attrs.begin(), E = Attrs.end(); I != E; ++I) { |
3596 | |
3597 | |
3598 | |
3599 | |
3600 | |
3601 | |
3602 | |
3603 | |
3604 | SS << " { "; |
3605 | emitArgInfo(*I->second, SS); |
3606 | SS << ", " << I->second->getValueAsBit("HasCustomParsing"); |
3607 | SS << ", " << I->second->isSubClassOf("TargetSpecificAttr"); |
3608 | SS << ", " |
3609 | << (I->second->isSubClassOf("TypeAttr") || |
3610 | I->second->isSubClassOf("DeclOrTypeAttr")); |
3611 | SS << ", " << I->second->isSubClassOf("StmtAttr"); |
3612 | SS << ", " << IsKnownToGCC(*I->second); |
3613 | SS << ", " << PragmaAttributeSupport.isAttributedSupported(*I->second); |
3614 | SS << ", " << GenerateAppertainsTo(*I->second, OS); |
3615 | SS << ", " << GenerateLangOptRequirements(*I->second, OS); |
3616 | SS << ", " << GenerateTargetRequirements(*I->second, Dupes, OS); |
3617 | SS << ", " << GenerateSpellingIndexToSemanticSpelling(*I->second, OS); |
3618 | SS << ", " |
3619 | << PragmaAttributeSupport.generateStrictConformsTo(*I->second, OS); |
3620 | SS << " }"; |
3621 | |
3622 | if (I + 1 != E) |
3623 | SS << ","; |
3624 | |
3625 | SS << " // AT_" << I->first << "\n"; |
3626 | } |
3627 | |
3628 | OS << "static const ParsedAttrInfo AttrInfoMap[ParsedAttr::UnknownAttribute " |
3629 | "+ 1] = {\n"; |
3630 | OS << SS.str(); |
3631 | OS << "};\n\n"; |
3632 | |
3633 | |
3634 | emitAttributeMatchRules(PragmaAttributeSupport, OS); |
3635 | } |
3636 | |
3637 | |
3638 | void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) { |
3639 | emitSourceFileHeader("Attribute name matcher", OS); |
3640 | |
3641 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); |
3642 | std::vector<StringMatcher::StringPair> GNU, Declspec, Microsoft, CXX11, |
3643 | Keywords, Pragma, C2x; |
3644 | std::set<std::string> Seen; |
3645 | for (const auto *A : Attrs) { |
3646 | const Record &Attr = *A; |
3647 | |
3648 | bool SemaHandler = Attr.getValueAsBit("SemaHandler"); |
3649 | bool Ignored = Attr.getValueAsBit("Ignored"); |
3650 | if (SemaHandler || Ignored) { |
3651 | |
3652 | |
3653 | |
3654 | |
3655 | |
3656 | |
3657 | |
3658 | |
3659 | |
3660 | |
3661 | std::string AttrName; |
3662 | if (Attr.isSubClassOf("TargetSpecificAttr") && |
3663 | !Attr.isValueUnset("ParseKind")) { |
3664 | AttrName = Attr.getValueAsString("ParseKind"); |
3665 | if (Seen.find(AttrName) != Seen.end()) |
3666 | continue; |
3667 | Seen.insert(AttrName); |
3668 | } else |
3669 | AttrName = NormalizeAttrName(StringRef(Attr.getName())).str(); |
3670 | |
3671 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr); |
3672 | for (const auto &S : Spellings) { |
3673 | const std::string &RawSpelling = S.name(); |
3674 | std::vector<StringMatcher::StringPair> *Matches = nullptr; |
3675 | std::string Spelling; |
3676 | const std::string &Variety = S.variety(); |
3677 | if (Variety == "CXX11") { |
3678 | Matches = &CXX11; |
3679 | Spelling += S.nameSpace(); |
3680 | Spelling += "::"; |
3681 | } else if (Variety == "C2x") { |
3682 | Matches = &C2x; |
3683 | Spelling += S.nameSpace(); |
3684 | Spelling += "::"; |
3685 | } else if (Variety == "GNU") |
3686 | Matches = &GNU; |
3687 | else if (Variety == "Declspec") |
3688 | Matches = &Declspec; |
3689 | else if (Variety == "Microsoft") |
3690 | Matches = &Microsoft; |
3691 | else if (Variety == "Keyword") |
3692 | Matches = &Keywords; |
3693 | else if (Variety == "Pragma") |
3694 | Matches = &Pragma; |
3695 | |
3696 | (0) . __assert_fail ("Matches && \"Unsupported spelling variety found\"", "/home/seafit/code_projects/clang_source/clang/utils/TableGen/ClangAttrEmitter.cpp", 3696, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Matches && "Unsupported spelling variety found"); |
3697 | |
3698 | if (Variety == "GNU") |
3699 | Spelling += NormalizeGNUAttrSpelling(RawSpelling); |
3700 | else |
3701 | Spelling += RawSpelling; |
3702 | |
3703 | if (SemaHandler) |
3704 | Matches->push_back(StringMatcher::StringPair( |
3705 | Spelling, "return ParsedAttr::AT_" + AttrName + ";")); |
3706 | else |
3707 | Matches->push_back(StringMatcher::StringPair( |
3708 | Spelling, "return ParsedAttr::IgnoredAttribute;")); |
3709 | } |
3710 | } |
3711 | } |
3712 | |
3713 | OS << "static ParsedAttr::Kind getAttrKind(StringRef Name, "; |
3714 | OS << "ParsedAttr::Syntax Syntax) {\n"; |
3715 | OS << " if (ParsedAttr::AS_GNU == Syntax) {\n"; |
3716 | StringMatcher("Name", GNU, OS).Emit(); |
3717 | OS << " } else if (ParsedAttr::AS_Declspec == Syntax) {\n"; |
3718 | StringMatcher("Name", Declspec, OS).Emit(); |
3719 | OS << " } else if (ParsedAttr::AS_Microsoft == Syntax) {\n"; |
3720 | StringMatcher("Name", Microsoft, OS).Emit(); |
3721 | OS << " } else if (ParsedAttr::AS_CXX11 == Syntax) {\n"; |
3722 | StringMatcher("Name", CXX11, OS).Emit(); |
3723 | OS << " } else if (ParsedAttr::AS_C2x == Syntax) {\n"; |
3724 | StringMatcher("Name", C2x, OS).Emit(); |
3725 | OS << " } else if (ParsedAttr::AS_Keyword == Syntax || "; |
3726 | OS << "ParsedAttr::AS_ContextSensitiveKeyword == Syntax) {\n"; |
3727 | StringMatcher("Name", Keywords, OS).Emit(); |
3728 | OS << " } else if (ParsedAttr::AS_Pragma == Syntax) {\n"; |
3729 | StringMatcher("Name", Pragma, OS).Emit(); |
3730 | OS << " }\n"; |
3731 | OS << " return ParsedAttr::UnknownAttribute;\n" |
3732 | << "}\n"; |
3733 | } |
3734 | |
3735 | |
3736 | void EmitClangAttrTextNodeDump(RecordKeeper &Records, raw_ostream &OS) { |
3737 | emitSourceFileHeader("Attribute text node dumper", OS); |
3738 | |
3739 | std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args; |
3740 | for (const auto *Attr : Attrs) { |
3741 | const Record &R = *Attr; |
3742 | if (!R.getValueAsBit("ASTNode")) |
3743 | continue; |
3744 | |
3745 | |
3746 | |
3747 | |
3748 | |
3749 | std::string FunctionContent; |
3750 | llvm::raw_string_ostream SS(FunctionContent); |
3751 | |
3752 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R); |
3753 | if (Spellings.size() > 1 && !SpellingNamesAreCommon(Spellings)) |
3754 | SS << " OS << \" \" << A->getSpelling();\n"; |
3755 | |
3756 | Args = R.getValueAsListOfDefs("Args"); |
3757 | for (const auto *Arg : Args) |
3758 | createArgument(*Arg, R.getName())->writeDump(SS); |
3759 | |
3760 | if (SS.tell()) { |
3761 | OS << " void Visit" << R.getName() << "Attr(const " << R.getName() |
3762 | << "Attr *A) {\n"; |
3763 | if (!Args.empty()) |
3764 | OS << " const auto *SA = cast<" << R.getName() |
3765 | << "Attr>(A); (void)SA;\n"; |
3766 | OS << SS.str(); |
3767 | OS << " }\n"; |
3768 | } |
3769 | } |
3770 | } |
3771 | |
3772 | void EmitClangAttrNodeTraverse(RecordKeeper &Records, raw_ostream &OS) { |
3773 | emitSourceFileHeader("Attribute text node traverser", OS); |
3774 | |
3775 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"), Args; |
3776 | for (const auto *Attr : Attrs) { |
3777 | const Record &R = *Attr; |
3778 | if (!R.getValueAsBit("ASTNode")) |
3779 | continue; |
3780 | |
3781 | std::string FunctionContent; |
3782 | llvm::raw_string_ostream SS(FunctionContent); |
3783 | |
3784 | Args = R.getValueAsListOfDefs("Args"); |
3785 | for (const auto *Arg : Args) |
3786 | createArgument(*Arg, R.getName())->writeDumpChildren(SS); |
3787 | if (SS.tell()) { |
3788 | OS << " void Visit" << R.getName() << "Attr(const " << R.getName() |
3789 | << "Attr *A) {\n"; |
3790 | if (!Args.empty()) |
3791 | OS << " const auto *SA = cast<" << R.getName() |
3792 | << "Attr>(A); (void)SA;\n"; |
3793 | OS << SS.str(); |
3794 | OS << " }\n"; |
3795 | } |
3796 | } |
3797 | } |
3798 | |
3799 | void EmitClangAttrParserStringSwitches(RecordKeeper &Records, |
3800 | raw_ostream &OS) { |
3801 | emitSourceFileHeader("Parser-related llvm::StringSwitch cases", OS); |
3802 | emitClangAttrArgContextList(Records, OS); |
3803 | emitClangAttrIdentifierArgList(Records, OS); |
3804 | emitClangAttrVariadicIdentifierArgList(Records, OS); |
3805 | emitClangAttrThisIsaIdentifierArgList(Records, OS); |
3806 | emitClangAttrTypeArgList(Records, OS); |
3807 | emitClangAttrLateParsedList(Records, OS); |
3808 | } |
3809 | |
3810 | void EmitClangAttrSubjectMatchRulesParserStringSwitches(RecordKeeper &Records, |
3811 | raw_ostream &OS) { |
3812 | getPragmaAttributeSupport(Records).generateParsingHelpers(OS); |
3813 | } |
3814 | |
3815 | enum class SpellingKind { |
3816 | GNU, |
3817 | CXX11, |
3818 | C2x, |
3819 | Declspec, |
3820 | Microsoft, |
3821 | Keyword, |
3822 | Pragma, |
3823 | }; |
3824 | static const size_t NumSpellingKinds = (size_t)SpellingKind::Pragma + 1; |
3825 | |
3826 | class SpellingList { |
3827 | std::vector<std::string> Spellings[NumSpellingKinds]; |
3828 | |
3829 | public: |
3830 | ArrayRef<std::string> operator[](SpellingKind K) const { |
3831 | return Spellings[(size_t)K]; |
3832 | } |
3833 | |
3834 | void add(const Record &Attr, FlattenedSpelling Spelling) { |
3835 | SpellingKind Kind = StringSwitch<SpellingKind>(Spelling.variety()) |
3836 | .Case("GNU", SpellingKind::GNU) |
3837 | .Case("CXX11", SpellingKind::CXX11) |
3838 | .Case("C2x", SpellingKind::C2x) |
3839 | .Case("Declspec", SpellingKind::Declspec) |
3840 | .Case("Microsoft", SpellingKind::Microsoft) |
3841 | .Case("Keyword", SpellingKind::Keyword) |
3842 | .Case("Pragma", SpellingKind::Pragma); |
3843 | std::string Name; |
3844 | if (!Spelling.nameSpace().empty()) { |
3845 | switch (Kind) { |
3846 | case SpellingKind::CXX11: |
3847 | case SpellingKind::C2x: |
3848 | Name = Spelling.nameSpace() + "::"; |
3849 | break; |
3850 | case SpellingKind::Pragma: |
3851 | Name = Spelling.nameSpace() + " "; |
3852 | break; |
3853 | default: |
3854 | PrintFatalError(Attr.getLoc(), "Unexpected namespace in spelling"); |
3855 | } |
3856 | } |
3857 | Name += Spelling.name(); |
3858 | |
3859 | Spellings[(size_t)Kind].push_back(Name); |
3860 | } |
3861 | }; |
3862 | |
3863 | class DocumentationData { |
3864 | public: |
3865 | const Record *Documentation; |
3866 | const Record *Attribute; |
3867 | std::string Heading; |
3868 | SpellingList SupportedSpellings; |
3869 | |
3870 | DocumentationData(const Record &Documentation, const Record &Attribute, |
3871 | std::pair<std::string, SpellingList> HeadingAndSpellings) |
3872 | : Documentation(&Documentation), Attribute(&Attribute), |
3873 | Heading(std::move(HeadingAndSpellings.first)), |
3874 | SupportedSpellings(std::move(HeadingAndSpellings.second)) {} |
3875 | }; |
3876 | |
3877 | static void (const Record *DocCategory, |
3878 | raw_ostream &OS) { |
3879 | const StringRef Name = DocCategory->getValueAsString("Name"); |
3880 | OS << Name << "\n" << std::string(Name.size(), '=') << "\n"; |
3881 | |
3882 | |
3883 | const StringRef ContentStr = DocCategory->getValueAsString("Content"); |
3884 | |
3885 | OS << ContentStr.trim(); |
3886 | |
3887 | OS << "\n\n"; |
3888 | } |
3889 | |
3890 | static std::pair<std::string, SpellingList> |
3891 | GetAttributeHeadingAndSpellings(const Record &Documentation, |
3892 | const Record &Attribute) { |
3893 | |
3894 | |
3895 | |
3896 | |
3897 | std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attribute); |
3898 | if (Spellings.empty()) |
3899 | PrintFatalError(Attribute.getLoc(), |
3900 | "Attribute has no supported spellings; cannot be " |
3901 | "documented"); |
3902 | |
3903 | |
3904 | std::string Heading = Documentation.getValueAsString("Heading"); |
3905 | if (Heading.empty()) { |
3906 | |
3907 | if (Spellings.size() == 1) |
3908 | Heading = Spellings.begin()->name(); |
3909 | else { |
3910 | std::set<std::string> Uniques; |
3911 | for (auto I = Spellings.begin(), E = Spellings.end(); |
3912 | I != E && Uniques.size() <= 1; ++I) { |
3913 | std::string Spelling = NormalizeNameForSpellingComparison(I->name()); |
3914 | Uniques.insert(Spelling); |
3915 | } |
3916 | |
3917 | |
3918 | if (Uniques.size() == 1) |
3919 | Heading = *Uniques.begin(); |
3920 | } |
3921 | } |
3922 | |
3923 | |
3924 | if (Heading.empty()) |
3925 | PrintFatalError(Attribute.getLoc(), |
3926 | "This attribute requires a heading to be specified"); |
3927 | |
3928 | SpellingList SupportedSpellings; |
3929 | for (const auto &I : Spellings) |
3930 | SupportedSpellings.add(Attribute, I); |
3931 | |
3932 | return std::make_pair(std::move(Heading), std::move(SupportedSpellings)); |
3933 | } |
3934 | |
3935 | static void WriteDocumentation(RecordKeeper &Records, |
3936 | const DocumentationData &Doc, raw_ostream &OS) { |
3937 | OS << Doc.Heading << "\n" << std::string(Doc.Heading.length(), '-') << "\n"; |
3938 | |
3939 | |
3940 | OS << ".. csv-table:: Supported Syntaxes\n"; |
3941 | OS << " :header: \"GNU\", \"C++11\", \"C2x\", \"``__declspec``\","; |
3942 | OS << " \"Keyword\", \"``#pragma``\", \"``#pragma clang attribute``\"\n\n"; |
3943 | OS << " \""; |
3944 | for (size_t Kind = 0; Kind != NumSpellingKinds; ++Kind) { |
3945 | SpellingKind K = (SpellingKind)Kind; |
3946 | |
3947 | |
3948 | if (K == SpellingKind::Microsoft) |
3949 | continue; |
3950 | |
3951 | bool PrintedAny = false; |
3952 | for (StringRef Spelling : Doc.SupportedSpellings[K]) { |
3953 | if (PrintedAny) |
3954 | OS << " |br| "; |
3955 | OS << "``" << Spelling << "``"; |
3956 | PrintedAny = true; |
3957 | } |
3958 | |
3959 | OS << "\",\""; |
3960 | } |
3961 | |
3962 | if (getPragmaAttributeSupport(Records).isAttributedSupported( |
3963 | *Doc.Attribute)) |
3964 | OS << "Yes"; |
3965 | OS << "\"\n\n"; |
3966 | |
3967 | |
3968 | |
3969 | if (!Doc.Documentation->isValueUnset("Deprecated")) { |
3970 | OS << "This attribute has been deprecated, and may be removed in a future " |
3971 | << "version of Clang."; |
3972 | const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated"); |
3973 | const StringRef Replacement = Deprecated.getValueAsString("Replacement"); |
3974 | if (!Replacement.empty()) |
3975 | OS << " This attribute has been superseded by ``" << Replacement |
3976 | << "``."; |
3977 | OS << "\n\n"; |
3978 | } |
3979 | |
3980 | const StringRef ContentStr = Doc.Documentation->getValueAsString("Content"); |
3981 | |
3982 | OS << ContentStr.trim(); |
3983 | |
3984 | OS << "\n\n\n"; |
3985 | } |
3986 | |
3987 | void EmitClangAttrDocs(RecordKeeper &Records, raw_ostream &OS) { |
3988 | |
3989 | const Record *Documentation = Records.getDef("GlobalDocumentation"); |
3990 | if (!Documentation) { |
3991 | PrintFatalError("The Documentation top-level definition is missing, " |
3992 | "no documentation will be generated."); |
3993 | return; |
3994 | } |
3995 | |
3996 | OS << Documentation->getValueAsString("Intro") << "\n"; |
3997 | |
3998 | |
3999 | |
4000 | std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"); |
4001 | std::map<const Record *, std::vector<DocumentationData>> SplitDocs; |
4002 | for (const auto *A : Attrs) { |
4003 | const Record &Attr = *A; |
4004 | std::vector<Record *> Docs = Attr.getValueAsListOfDefs("Documentation"); |
4005 | for (const auto *D : Docs) { |
4006 | const Record &Doc = *D; |
4007 | const Record *Category = Doc.getValueAsDef("Category"); |
4008 | |
4009 | |
4010 | |
4011 | const StringRef Cat = Category->getValueAsString("Name"); |
4012 | bool Undocumented = Cat == "Undocumented"; |
4013 | if (Undocumented && Docs.size() > 1) |
4014 | PrintFatalError(Doc.getLoc(), |
4015 | "Attribute is \"Undocumented\", but has multiple " |
4016 | "documentation categories"); |
4017 | |
4018 | if (!Undocumented) |
4019 | SplitDocs[Category].push_back(DocumentationData( |
4020 | Doc, Attr, GetAttributeHeadingAndSpellings(Doc, Attr))); |
4021 | } |
4022 | } |
4023 | |
4024 | |
4025 | |
4026 | for (auto &I : SplitDocs) { |
4027 | WriteCategoryHeader(I.first, OS); |
4028 | |
4029 | llvm::sort(I.second, |
4030 | [](const DocumentationData &D1, const DocumentationData &D2) { |
4031 | return D1.Heading < D2.Heading; |
4032 | }); |
4033 | |
4034 | |
4035 | |
4036 | for (const auto &Doc : I.second) |
4037 | WriteDocumentation(Records, Doc, OS); |
4038 | } |
4039 | } |
4040 | |
4041 | void EmitTestPragmaAttributeSupportedAttributes(RecordKeeper &Records, |
4042 | raw_ostream &OS) { |
4043 | PragmaClangAttributeSupport Support = getPragmaAttributeSupport(Records); |
4044 | ParsedAttrMap Attrs = getParsedAttrList(Records); |
4045 | OS << "#pragma clang attribute supports the following attributes:\n"; |
4046 | for (const auto &I : Attrs) { |
4047 | if (!Support.isAttributedSupported(*I.second)) |
4048 | continue; |
4049 | OS << I.first; |
4050 | if (I.second->isValueUnset("Subjects")) { |
4051 | OS << " ()\n"; |
4052 | continue; |
4053 | } |
4054 | const Record *SubjectObj = I.second->getValueAsDef("Subjects"); |
4055 | std::vector<Record *> Subjects = |
4056 | SubjectObj->getValueAsListOfDefs("Subjects"); |
4057 | OS << " ("; |
4058 | for (const auto &Subject : llvm::enumerate(Subjects)) { |
4059 | if (Subject.index()) |
4060 | OS << ", "; |
4061 | PragmaClangAttributeSupport::RuleOrAggregateRuleSet &RuleSet = |
4062 | Support.SubjectsToRules.find(Subject.value())->getSecond(); |
4063 | if (RuleSet.isRule()) { |
4064 | OS << RuleSet.getRule().getEnumValueName(); |
4065 | continue; |
4066 | } |
4067 | OS << "("; |
4068 | for (const auto &Rule : llvm::enumerate(RuleSet.getAggregateRuleSet())) { |
4069 | if (Rule.index()) |
4070 | OS << ", "; |
4071 | OS << Rule.value().getEnumValueName(); |
4072 | } |
4073 | OS << ")"; |
4074 | } |
4075 | OS << ")\n"; |
4076 | } |
4077 | OS << "End of supported attributes.\n"; |
4078 | } |
4079 | |
4080 | } |
4081 | |