blob: a180e1f20d7cf3e418a867e2bb38fa664768d957 [file] [log] [blame]
Peter Collingbournebee583f2011-10-06 13:03:08 +00001//===- ClangAttrEmitter.cpp - Generate Clang attribute handling =-*- C++ -*--=//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These tablegen backends emit Clang attribute processing code
11//
12//===----------------------------------------------------------------------===//
13
Alexis Hunta0e54d42012-06-18 16:13:52 +000014#include "llvm/ADT/SmallString.h"
Chandler Carruth757fcd62014-03-04 10:05:20 +000015#include "llvm/ADT/STLExtras.h"
Aaron Ballman8ee40b72013-09-09 23:33:17 +000016#include "llvm/ADT/SmallSet.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000017#include "llvm/ADT/StringSwitch.h"
18#include "llvm/TableGen/Error.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000019#include "llvm/TableGen/Record.h"
Douglas Gregor377f99b2012-05-02 17:33:51 +000020#include "llvm/TableGen/StringMatcher.h"
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000021#include "llvm/TableGen/TableGenBackend.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000022#include <algorithm>
23#include <cctype>
Aaron Ballman8f1439b2014-03-05 16:49:55 +000024#include <memory>
Aaron Ballman80469032013-11-29 14:57:58 +000025#include <set>
Chandler Carruth5553d0d2014-01-07 11:51:46 +000026#include <sstream>
Peter Collingbournebee583f2011-10-06 13:03:08 +000027
28using namespace llvm;
29
Aaron Ballmanc669cc02014-01-27 22:10:04 +000030class FlattenedSpelling {
31 std::string V, N, NS;
32 bool K;
33
34public:
35 FlattenedSpelling(const std::string &Variety, const std::string &Name,
36 const std::string &Namespace, bool KnownToGCC) :
37 V(Variety), N(Name), NS(Namespace), K(KnownToGCC) {}
38 explicit FlattenedSpelling(const Record &Spelling) :
39 V(Spelling.getValueAsString("Variety")),
40 N(Spelling.getValueAsString("Name")) {
41
42 assert(V != "GCC" && "Given a GCC spelling, which means this hasn't been"
43 "flattened!");
Tyler Nowickie8b07ed2014-06-13 17:57:25 +000044 if (V == "CXX11" || V == "Pragma")
Aaron Ballmanc669cc02014-01-27 22:10:04 +000045 NS = Spelling.getValueAsString("Namespace");
46 bool Unset;
47 K = Spelling.getValueAsBitOrUnset("KnownToGCC", Unset);
48 }
49
50 const std::string &variety() const { return V; }
51 const std::string &name() const { return N; }
52 const std::string &nameSpace() const { return NS; }
53 bool knownToGCC() const { return K; }
54};
55
56std::vector<FlattenedSpelling> GetFlattenedSpellings(const Record &Attr) {
57 std::vector<Record *> Spellings = Attr.getValueAsListOfDefs("Spellings");
58 std::vector<FlattenedSpelling> Ret;
59
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +000060 for (const auto &Spelling : Spellings) {
61 if (Spelling->getValueAsString("Variety") == "GCC") {
Aaron Ballmanc669cc02014-01-27 22:10:04 +000062 // Gin up two new spelling objects to add into the list.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +000063 Ret.push_back(FlattenedSpelling("GNU", Spelling->getValueAsString("Name"),
Aaron Ballmanc669cc02014-01-27 22:10:04 +000064 "", true));
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +000065 Ret.push_back(FlattenedSpelling(
66 "CXX11", Spelling->getValueAsString("Name"), "gnu", true));
Aaron Ballmanc669cc02014-01-27 22:10:04 +000067 } else
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +000068 Ret.push_back(FlattenedSpelling(*Spelling));
Aaron Ballmanc669cc02014-01-27 22:10:04 +000069 }
70
71 return Ret;
72}
73
Peter Collingbournebee583f2011-10-06 13:03:08 +000074static std::string ReadPCHRecord(StringRef type) {
75 return StringSwitch<std::string>(type)
76 .EndsWith("Decl *", "GetLocalDeclAs<"
77 + std::string(type, 0, type.size()-1) + ">(F, Record[Idx++])")
Richard Smithb87c4652013-10-31 21:23:20 +000078 .Case("TypeSourceInfo *", "GetTypeSourceInfo(F, Record, Idx)")
Argyrios Kyrtzidisa660ae42012-11-15 01:31:39 +000079 .Case("Expr *", "ReadExpr(F)")
Peter Collingbournebee583f2011-10-06 13:03:08 +000080 .Case("IdentifierInfo *", "GetIdentifierInfo(F, Record, Idx)")
Peter Collingbournebee583f2011-10-06 13:03:08 +000081 .Default("Record[Idx++]");
82}
83
84// Assumes that the way to get the value is SA->getname()
85static std::string WritePCHRecord(StringRef type, StringRef name) {
86 return StringSwitch<std::string>(type)
87 .EndsWith("Decl *", "AddDeclRef(" + std::string(name) +
88 ", Record);\n")
Richard Smithb87c4652013-10-31 21:23:20 +000089 .Case("TypeSourceInfo *",
90 "AddTypeSourceInfo(" + std::string(name) + ", Record);\n")
Peter Collingbournebee583f2011-10-06 13:03:08 +000091 .Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
92 .Case("IdentifierInfo *",
93 "AddIdentifierRef(" + std::string(name) + ", Record);\n")
Peter Collingbournebee583f2011-10-06 13:03:08 +000094 .Default("Record.push_back(" + std::string(name) + ");\n");
95}
96
Michael Han4a045172012-03-07 00:12:16 +000097// Normalize attribute name by removing leading and trailing
98// underscores. For example, __foo, foo__, __foo__ would
99// become foo.
100static StringRef NormalizeAttrName(StringRef AttrName) {
101 if (AttrName.startswith("__"))
102 AttrName = AttrName.substr(2, AttrName.size());
103
104 if (AttrName.endswith("__"))
105 AttrName = AttrName.substr(0, AttrName.size() - 2);
106
107 return AttrName;
108}
109
Aaron Ballman36a53502014-01-16 13:03:14 +0000110// Normalize the name by removing any and all leading and trailing underscores.
111// This is different from NormalizeAttrName in that it also handles names like
112// _pascal and __pascal.
113static StringRef NormalizeNameForSpellingComparison(StringRef Name) {
114 while (Name.startswith("_"))
115 Name = Name.substr(1, Name.size());
116 while (Name.endswith("_"))
117 Name = Name.substr(0, Name.size() - 1);
118 return Name;
119}
120
Michael Han4a045172012-03-07 00:12:16 +0000121// Normalize attribute spelling only if the spelling has both leading
122// and trailing underscores. For example, __ms_struct__ will be
123// normalized to "ms_struct"; __cdecl will remain intact.
124static StringRef NormalizeAttrSpelling(StringRef AttrSpelling) {
125 if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) {
126 AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4);
127 }
128
129 return AttrSpelling;
130}
131
Aaron Ballman2f22b942014-05-20 19:47:14 +0000132typedef std::vector<std::pair<std::string, const Record *>> ParsedAttrMap;
Aaron Ballman64e69862013-12-15 13:05:48 +0000133
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000134static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,
Craig Topper8ae12032014-05-07 06:21:57 +0000135 ParsedAttrMap *Dupes = nullptr) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000136 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Aaron Ballman64e69862013-12-15 13:05:48 +0000137 std::set<std::string> Seen;
138 ParsedAttrMap R;
Aaron Ballman2f22b942014-05-20 19:47:14 +0000139 for (const auto *Attr : Attrs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000140 if (Attr->getValueAsBit("SemaHandler")) {
Aaron Ballman64e69862013-12-15 13:05:48 +0000141 std::string AN;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000142 if (Attr->isSubClassOf("TargetSpecificAttr") &&
143 !Attr->isValueUnset("ParseKind")) {
144 AN = Attr->getValueAsString("ParseKind");
Aaron Ballman64e69862013-12-15 13:05:48 +0000145
146 // If this attribute has already been handled, it does not need to be
147 // handled again.
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000148 if (Seen.find(AN) != Seen.end()) {
149 if (Dupes)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000150 Dupes->push_back(std::make_pair(AN, Attr));
Aaron Ballman64e69862013-12-15 13:05:48 +0000151 continue;
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000152 }
Aaron Ballman64e69862013-12-15 13:05:48 +0000153 Seen.insert(AN);
154 } else
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000155 AN = NormalizeAttrName(Attr->getName()).str();
Aaron Ballman64e69862013-12-15 13:05:48 +0000156
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000157 R.push_back(std::make_pair(AN, Attr));
Aaron Ballman64e69862013-12-15 13:05:48 +0000158 }
159 }
160 return R;
161}
162
Peter Collingbournebee583f2011-10-06 13:03:08 +0000163namespace {
164 class Argument {
165 std::string lowerName, upperName;
166 StringRef attrName;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000167 bool isOpt;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000168
169 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000170 Argument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000171 : lowerName(Arg.getValueAsString("Name")), upperName(lowerName),
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000172 attrName(Attr), isOpt(false) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000173 if (!lowerName.empty()) {
174 lowerName[0] = std::tolower(lowerName[0]);
175 upperName[0] = std::toupper(upperName[0]);
176 }
177 }
178 virtual ~Argument() {}
179
180 StringRef getLowerName() const { return lowerName; }
181 StringRef getUpperName() const { return upperName; }
182 StringRef getAttrName() const { return attrName; }
183
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000184 bool isOptional() const { return isOpt; }
185 void setOptional(bool set) { isOpt = set; }
186
Peter Collingbournebee583f2011-10-06 13:03:08 +0000187 // These functions print the argument contents formatted in different ways.
188 virtual void writeAccessors(raw_ostream &OS) const = 0;
189 virtual void writeAccessorDefinitions(raw_ostream &OS) const {}
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +0000190 virtual void writeASTVisitorTraversal(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000191 virtual void writeCloneArgs(raw_ostream &OS) const = 0;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000192 virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0;
Daniel Dunbardc51baa2012-02-10 06:00:29 +0000193 virtual void writeTemplateInstantiation(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000194 virtual void writeCtorBody(raw_ostream &OS) const {}
195 virtual void writeCtorInitializers(raw_ostream &OS) const = 0;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000196 virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000197 virtual void writeCtorParameters(raw_ostream &OS) const = 0;
198 virtual void writeDeclarations(raw_ostream &OS) const = 0;
199 virtual void writePCHReadArgs(raw_ostream &OS) const = 0;
200 virtual void writePCHReadDecls(raw_ostream &OS) const = 0;
201 virtual void writePCHWrite(raw_ostream &OS) const = 0;
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000202 virtual void writeValue(raw_ostream &OS) const = 0;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000203 virtual void writeDump(raw_ostream &OS) const = 0;
204 virtual void writeDumpChildren(raw_ostream &OS) const {}
Richard Trieude5cc7d2013-01-31 01:44:26 +0000205 virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000206
207 virtual bool isEnumArg() const { return false; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000208 virtual bool isVariadicEnumArg() const { return false; }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000209 virtual bool isVariadic() const { return false; }
Aaron Ballman36a53502014-01-16 13:03:14 +0000210
211 virtual void writeImplicitCtorArgs(raw_ostream &OS) const {
212 OS << getUpperName();
213 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000214 };
215
216 class SimpleArgument : public Argument {
217 std::string type;
218
219 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000220 SimpleArgument(const Record &Arg, StringRef Attr, std::string T)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000221 : Argument(Arg, Attr), type(T)
222 {}
223
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000224 std::string getType() const { return type; }
225
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000226 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000227 OS << " " << type << " get" << getUpperName() << "() const {\n";
228 OS << " return " << getLowerName() << ";\n";
229 OS << " }";
230 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000231 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000232 OS << getLowerName();
233 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000234 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000235 OS << "A->get" << getUpperName() << "()";
236 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000237 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000238 OS << getLowerName() << "(" << getUpperName() << ")";
239 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000240 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000241 OS << getLowerName() << "()";
242 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000243 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000244 OS << type << " " << getUpperName();
245 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000246 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000247 OS << type << " " << getLowerName() << ";";
248 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000249 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000250 std::string read = ReadPCHRecord(type);
251 OS << " " << type << " " << getLowerName() << " = " << read << ";\n";
252 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000253 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000254 OS << getLowerName();
255 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000256 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000257 OS << " " << WritePCHRecord(type, "SA->get" +
258 std::string(getUpperName()) + "()");
259 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000260 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000261 if (type == "FunctionDecl *") {
Richard Smithb87c4652013-10-31 21:23:20 +0000262 OS << "\" << get" << getUpperName()
263 << "()->getNameInfo().getAsString() << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000264 } else if (type == "IdentifierInfo *") {
265 OS << "\" << get" << getUpperName() << "()->getName() << \"";
Richard Smithb87c4652013-10-31 21:23:20 +0000266 } else if (type == "TypeSourceInfo *") {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000267 OS << "\" << get" << getUpperName() << "().getAsString() << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000268 } else {
269 OS << "\" << get" << getUpperName() << "() << \"";
270 }
271 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000272 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000273 if (type == "FunctionDecl *") {
274 OS << " OS << \" \";\n";
275 OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n";
276 } else if (type == "IdentifierInfo *") {
277 OS << " OS << \" \" << SA->get" << getUpperName()
278 << "()->getName();\n";
Richard Smithb87c4652013-10-31 21:23:20 +0000279 } else if (type == "TypeSourceInfo *") {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000280 OS << " OS << \" \" << SA->get" << getUpperName()
281 << "().getAsString();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000282 } else if (type == "bool") {
283 OS << " if (SA->get" << getUpperName() << "()) OS << \" "
284 << getUpperName() << "\";\n";
285 } else if (type == "int" || type == "unsigned") {
286 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
287 } else {
288 llvm_unreachable("Unknown SimpleArgument type!");
289 }
290 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000291 };
292
Aaron Ballman18a78382013-11-21 00:28:23 +0000293 class DefaultSimpleArgument : public SimpleArgument {
294 int64_t Default;
295
296 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000297 DefaultSimpleArgument(const Record &Arg, StringRef Attr,
Aaron Ballman18a78382013-11-21 00:28:23 +0000298 std::string T, int64_t Default)
299 : SimpleArgument(Arg, Attr, T), Default(Default) {}
300
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000301 void writeAccessors(raw_ostream &OS) const override {
Aaron Ballman18a78382013-11-21 00:28:23 +0000302 SimpleArgument::writeAccessors(OS);
303
304 OS << "\n\n static const " << getType() << " Default" << getUpperName()
305 << " = " << Default << ";";
306 }
307 };
308
Peter Collingbournebee583f2011-10-06 13:03:08 +0000309 class StringArgument : public Argument {
310 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000311 StringArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000312 : Argument(Arg, Attr)
313 {}
314
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000315 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000316 OS << " llvm::StringRef get" << getUpperName() << "() const {\n";
317 OS << " return llvm::StringRef(" << getLowerName() << ", "
318 << getLowerName() << "Length);\n";
319 OS << " }\n";
320 OS << " unsigned get" << getUpperName() << "Length() const {\n";
321 OS << " return " << getLowerName() << "Length;\n";
322 OS << " }\n";
323 OS << " void set" << getUpperName()
324 << "(ASTContext &C, llvm::StringRef S) {\n";
325 OS << " " << getLowerName() << "Length = S.size();\n";
326 OS << " this->" << getLowerName() << " = new (C, 1) char ["
327 << getLowerName() << "Length];\n";
328 OS << " std::memcpy(this->" << getLowerName() << ", S.data(), "
329 << getLowerName() << "Length);\n";
330 OS << " }";
331 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000332 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000333 OS << "get" << getUpperName() << "()";
334 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000335 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000336 OS << "A->get" << getUpperName() << "()";
337 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000338 void writeCtorBody(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000339 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
340 << ".data(), " << getLowerName() << "Length);";
341 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000342 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000343 OS << getLowerName() << "Length(" << getUpperName() << ".size()),"
344 << getLowerName() << "(new (Ctx, 1) char[" << getLowerName()
345 << "Length])";
346 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000347 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000348 OS << getLowerName() << "Length(0)," << getLowerName() << "(0)";
349 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000350 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000351 OS << "llvm::StringRef " << getUpperName();
352 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000353 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000354 OS << "unsigned " << getLowerName() << "Length;\n";
355 OS << "char *" << getLowerName() << ";";
356 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000357 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000358 OS << " std::string " << getLowerName()
359 << "= ReadString(Record, Idx);\n";
360 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000361 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000362 OS << getLowerName();
363 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000364 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000365 OS << " AddString(SA->get" << getUpperName() << "(), Record);\n";
366 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000367 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000368 OS << "\\\"\" << get" << getUpperName() << "() << \"\\\"";
369 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000370 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000371 OS << " OS << \" \\\"\" << SA->get" << getUpperName()
372 << "() << \"\\\"\";\n";
373 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000374 };
375
376 class AlignedArgument : public Argument {
377 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000378 AlignedArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000379 : Argument(Arg, Attr)
380 {}
381
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000382 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000383 OS << " bool is" << getUpperName() << "Dependent() const;\n";
384
385 OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n";
386
387 OS << " bool is" << getUpperName() << "Expr() const {\n";
388 OS << " return is" << getLowerName() << "Expr;\n";
389 OS << " }\n";
390
391 OS << " Expr *get" << getUpperName() << "Expr() const {\n";
392 OS << " assert(is" << getLowerName() << "Expr);\n";
393 OS << " return " << getLowerName() << "Expr;\n";
394 OS << " }\n";
395
396 OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n";
397 OS << " assert(!is" << getLowerName() << "Expr);\n";
398 OS << " return " << getLowerName() << "Type;\n";
399 OS << " }";
400 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000401 void writeAccessorDefinitions(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000402 OS << "bool " << getAttrName() << "Attr::is" << getUpperName()
403 << "Dependent() const {\n";
404 OS << " if (is" << getLowerName() << "Expr)\n";
405 OS << " return " << getLowerName() << "Expr && (" << getLowerName()
406 << "Expr->isValueDependent() || " << getLowerName()
407 << "Expr->isTypeDependent());\n";
408 OS << " else\n";
409 OS << " return " << getLowerName()
410 << "Type->getType()->isDependentType();\n";
411 OS << "}\n";
412
413 // FIXME: Do not do the calculation here
414 // FIXME: Handle types correctly
415 // A null pointer means maximum alignment
416 // FIXME: Load the platform-specific maximum alignment, rather than
417 // 16, the x86 max.
418 OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
419 << "(ASTContext &Ctx) const {\n";
420 OS << " assert(!is" << getUpperName() << "Dependent());\n";
421 OS << " if (is" << getLowerName() << "Expr)\n";
422 OS << " return (" << getLowerName() << "Expr ? " << getLowerName()
Richard Smithcaf33902011-10-10 18:28:20 +0000423 << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue() : 16)"
Peter Collingbournebee583f2011-10-06 13:03:08 +0000424 << "* Ctx.getCharWidth();\n";
425 OS << " else\n";
426 OS << " return 0; // FIXME\n";
427 OS << "}\n";
428 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000429 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000430 OS << "is" << getLowerName() << "Expr, is" << getLowerName()
431 << "Expr ? static_cast<void*>(" << getLowerName()
432 << "Expr) : " << getLowerName()
433 << "Type";
434 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000435 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000436 // FIXME: move the definition in Sema::InstantiateAttrs to here.
437 // In the meantime, aligned attributes are cloned.
438 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000439 void writeCtorBody(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000440 OS << " if (is" << getLowerName() << "Expr)\n";
441 OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>("
442 << getUpperName() << ");\n";
443 OS << " else\n";
444 OS << " " << getLowerName()
445 << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName()
446 << ");";
447 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000448 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000449 OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)";
450 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000451 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000452 OS << "is" << getLowerName() << "Expr(false)";
453 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000454 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000455 OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName();
456 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000457 void writeImplicitCtorArgs(raw_ostream &OS) const override {
Aaron Ballman36a53502014-01-16 13:03:14 +0000458 OS << "Is" << getUpperName() << "Expr, " << getUpperName();
459 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000460 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000461 OS << "bool is" << getLowerName() << "Expr;\n";
462 OS << "union {\n";
463 OS << "Expr *" << getLowerName() << "Expr;\n";
464 OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
465 OS << "};";
466 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000467 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000468 OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr";
469 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000470 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000471 OS << " bool is" << getLowerName() << "Expr = Record[Idx++];\n";
472 OS << " void *" << getLowerName() << "Ptr;\n";
473 OS << " if (is" << getLowerName() << "Expr)\n";
474 OS << " " << getLowerName() << "Ptr = ReadExpr(F);\n";
475 OS << " else\n";
476 OS << " " << getLowerName()
477 << "Ptr = GetTypeSourceInfo(F, Record, Idx);\n";
478 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000479 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000480 OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n";
481 OS << " if (SA->is" << getUpperName() << "Expr())\n";
482 OS << " AddStmt(SA->get" << getUpperName() << "Expr());\n";
483 OS << " else\n";
484 OS << " AddTypeSourceInfo(SA->get" << getUpperName()
485 << "Type(), Record);\n";
486 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000487 void writeValue(raw_ostream &OS) const override {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000488 OS << "\";\n";
Aaron Ballmanc960f562014-08-01 13:49:00 +0000489 // The aligned attribute argument expression is optional.
490 OS << " if (is" << getLowerName() << "Expr && "
491 << getLowerName() << "Expr)\n";
492 OS << " " << getLowerName() << "Expr->printPretty(OS, 0, Policy);\n";
Richard Trieuddd01ce2014-06-09 22:53:25 +0000493 OS << " OS << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000494 }
Craig Topper3164f332014-03-11 03:39:26 +0000495 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000496 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000497 void writeDumpChildren(raw_ostream &OS) const override {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000498 OS << " if (SA->is" << getUpperName() << "Expr()) {\n";
499 OS << " lastChild();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000500 OS << " dumpStmt(SA->get" << getUpperName() << "Expr());\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000501 OS << " } else\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000502 OS << " dumpType(SA->get" << getUpperName()
503 << "Type()->getType());\n";
504 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000505 void writeHasChildren(raw_ostream &OS) const override {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000506 OS << "SA->is" << getUpperName() << "Expr()";
507 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000508 };
509
510 class VariadicArgument : public Argument {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000511 std::string Type, ArgName, ArgSizeName, RangeName;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000512
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000513 protected:
514 // Assumed to receive a parameter: raw_ostream OS.
515 virtual void writeValueImpl(raw_ostream &OS) const {
516 OS << " OS << Val;\n";
517 }
518
Peter Collingbournebee583f2011-10-06 13:03:08 +0000519 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000520 VariadicArgument(const Record &Arg, StringRef Attr, std::string T)
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000521 : Argument(Arg, Attr), Type(T), ArgName(getLowerName().str() + "_"),
522 ArgSizeName(ArgName + "Size"), RangeName(getLowerName()) {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000523
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000524 std::string getType() const { return Type; }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000525 bool isVariadic() const override { return true; }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000526
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000527 void writeAccessors(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000528 std::string IteratorType = getLowerName().str() + "_iterator";
529 std::string BeginFn = getLowerName().str() + "_begin()";
530 std::string EndFn = getLowerName().str() + "_end()";
531
532 OS << " typedef " << Type << "* " << IteratorType << ";\n";
533 OS << " " << IteratorType << " " << BeginFn << " const {"
534 << " return " << ArgName << "; }\n";
535 OS << " " << IteratorType << " " << EndFn << " const {"
536 << " return " << ArgName << " + " << ArgSizeName << "; }\n";
537 OS << " unsigned " << getLowerName() << "_size() const {"
538 << " return " << ArgSizeName << "; }\n";
539 OS << " llvm::iterator_range<" << IteratorType << "> " << RangeName
540 << "() const { return llvm::make_range(" << BeginFn << ", " << EndFn
541 << "); }\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000542 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000543 void writeCloneArgs(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000544 OS << ArgName << ", " << ArgSizeName;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000545 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000546 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000547 // This isn't elegant, but we have to go through public methods...
548 OS << "A->" << getLowerName() << "_begin(), "
549 << "A->" << getLowerName() << "_size()";
550 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000551 void writeCtorBody(raw_ostream &OS) const override {
Aaron Ballmand6459e52014-05-01 15:21:03 +0000552 OS << " std::copy(" << getUpperName() << ", " << getUpperName()
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000553 << " + " << ArgSizeName << ", " << ArgName << ");";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000554 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000555 void writeCtorInitializers(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000556 OS << ArgSizeName << "(" << getUpperName() << "Size), "
557 << ArgName << "(new (Ctx, 16) " << getType() << "["
558 << ArgSizeName << "])";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000559 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000560 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000561 OS << ArgSizeName << "(0), " << ArgName << "(nullptr)";
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000562 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000563 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000564 OS << getType() << " *" << getUpperName() << ", unsigned "
565 << getUpperName() << "Size";
566 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000567 void writeImplicitCtorArgs(raw_ostream &OS) const override {
Aaron Ballman36a53502014-01-16 13:03:14 +0000568 OS << getUpperName() << ", " << getUpperName() << "Size";
569 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000570 void writeDeclarations(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000571 OS << " unsigned " << ArgSizeName << ";\n";
572 OS << " " << getType() << " *" << ArgName << ";";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000573 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000574 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000575 OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000576 OS << " SmallVector<" << Type << ", 4> " << getLowerName()
Peter Collingbournebee583f2011-10-06 13:03:08 +0000577 << ";\n";
578 OS << " " << getLowerName() << ".reserve(" << getLowerName()
579 << "Size);\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000580 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000581
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000582 std::string read = ReadPCHRecord(Type);
Peter Collingbournebee583f2011-10-06 13:03:08 +0000583 OS << " " << getLowerName() << ".push_back(" << read << ");\n";
584 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000585 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000586 OS << getLowerName() << ".data(), " << getLowerName() << "Size";
587 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000588 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000589 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000590 OS << " for (auto &Val : SA->" << RangeName << "())\n";
591 OS << " " << WritePCHRecord(Type, "Val");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000592 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000593 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000594 OS << "\";\n";
595 OS << " bool isFirst = true;\n"
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000596 << " for (const auto &Val : " << RangeName << "()) {\n"
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000597 << " if (isFirst) isFirst = false;\n"
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000598 << " else OS << \", \";\n";
599 writeValueImpl(OS);
600 OS << " }\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000601 OS << " OS << \"";
602 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000603 void writeDump(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000604 OS << " for (const auto &Val : SA->" << RangeName << "())\n";
605 OS << " OS << \" \" << Val;\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000606 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000607 };
608
Reid Klecknerf526b9482014-02-12 18:22:18 +0000609 // Unique the enums, but maintain the original declaration ordering.
Reid Klecknerf06b2662014-02-12 19:26:24 +0000610 std::vector<std::string>
611 uniqueEnumsInOrder(const std::vector<std::string> &enums) {
Reid Klecknerf526b9482014-02-12 18:22:18 +0000612 std::vector<std::string> uniques;
613 std::set<std::string> unique_set(enums.begin(), enums.end());
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000614 for (const auto &i : enums) {
615 std::set<std::string>::iterator set_i = unique_set.find(i);
Reid Klecknerf526b9482014-02-12 18:22:18 +0000616 if (set_i != unique_set.end()) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000617 uniques.push_back(i);
Reid Klecknerf526b9482014-02-12 18:22:18 +0000618 unique_set.erase(set_i);
619 }
620 }
621 return uniques;
622 }
623
Peter Collingbournebee583f2011-10-06 13:03:08 +0000624 class EnumArgument : public Argument {
625 std::string type;
Aaron Ballman0e468c02014-01-05 21:08:29 +0000626 std::vector<std::string> values, enums, uniques;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000627 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000628 EnumArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000629 : Argument(Arg, Attr), type(Arg.getValueAsString("Type")),
Aaron Ballman0e468c02014-01-05 21:08:29 +0000630 values(Arg.getValueAsListOfStrings("Values")),
631 enums(Arg.getValueAsListOfStrings("Enums")),
Reid Klecknerf526b9482014-02-12 18:22:18 +0000632 uniques(uniqueEnumsInOrder(enums))
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000633 {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000634 // FIXME: Emit a proper error
635 assert(!uniques.empty());
636 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000637
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000638 bool isEnumArg() const override { return true; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000639
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000640 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000641 OS << " " << type << " get" << getUpperName() << "() const {\n";
642 OS << " return " << getLowerName() << ";\n";
643 OS << " }";
644 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000645 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000646 OS << getLowerName();
647 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000648 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000649 OS << "A->get" << getUpperName() << "()";
650 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000651 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000652 OS << getLowerName() << "(" << getUpperName() << ")";
653 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000654 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000655 OS << getLowerName() << "(" << type << "(0))";
656 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000657 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000658 OS << type << " " << getUpperName();
659 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000660 void writeDeclarations(raw_ostream &OS) const override {
Aaron Ballman0e468c02014-01-05 21:08:29 +0000661 std::vector<std::string>::const_iterator i = uniques.begin(),
662 e = uniques.end();
Peter Collingbournebee583f2011-10-06 13:03:08 +0000663 // The last one needs to not have a comma.
664 --e;
665
666 OS << "public:\n";
667 OS << " enum " << type << " {\n";
668 for (; i != e; ++i)
669 OS << " " << *i << ",\n";
670 OS << " " << *e << "\n";
671 OS << " };\n";
672 OS << "private:\n";
673 OS << " " << type << " " << getLowerName() << ";";
674 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000675 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000676 OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName()
677 << "(static_cast<" << getAttrName() << "Attr::" << type
678 << ">(Record[Idx++]));\n";
679 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000680 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000681 OS << getLowerName();
682 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000683 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000684 OS << "Record.push_back(SA->get" << getUpperName() << "());\n";
685 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000686 void writeValue(raw_ostream &OS) const override {
Aaron Ballman36d79102014-09-15 16:16:14 +0000687 // FIXME: this isn't 100% correct -- some enum arguments require printing
688 // as a string literal, while others require printing as an identifier.
689 // Tablegen currently does not distinguish between the two forms.
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000690 OS << "\\\"\" << " << getAttrName() << "Attr::Convert" << type << "ToStr(get"
691 << getUpperName() << "()) << \"\\\"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000692 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000693 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000694 OS << " switch(SA->get" << getUpperName() << "()) {\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000695 for (const auto &I : uniques) {
696 OS << " case " << getAttrName() << "Attr::" << I << ":\n";
697 OS << " OS << \" " << I << "\";\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000698 OS << " break;\n";
699 }
700 OS << " }\n";
701 }
Aaron Ballman682ee422013-09-11 19:47:58 +0000702
703 void writeConversion(raw_ostream &OS) const {
704 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
705 OS << type << " &Out) {\n";
706 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000707 OS << type << ">>(Val)\n";
Aaron Ballman682ee422013-09-11 19:47:58 +0000708 for (size_t I = 0; I < enums.size(); ++I) {
709 OS << " .Case(\"" << values[I] << "\", ";
710 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
711 }
712 OS << " .Default(Optional<" << type << ">());\n";
713 OS << " if (R) {\n";
714 OS << " Out = *R;\n return true;\n }\n";
715 OS << " return false;\n";
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000716 OS << " }\n\n";
717
718 // Mapping from enumeration values back to enumeration strings isn't
719 // trivial because some enumeration values have multiple named
720 // enumerators, such as type_visibility(internal) and
721 // type_visibility(hidden) both mapping to TypeVisibilityAttr::Hidden.
722 OS << " static const char *Convert" << type << "ToStr("
723 << type << " Val) {\n"
724 << " switch(Val) {\n";
725 std::set<std::string> Uniques;
726 for (size_t I = 0; I < enums.size(); ++I) {
727 if (Uniques.insert(enums[I]).second)
728 OS << " case " << getAttrName() << "Attr::" << enums[I]
729 << ": return \"" << values[I] << "\";\n";
730 }
731 OS << " }\n"
732 << " llvm_unreachable(\"No enumerator with that value\");\n"
733 << " }\n";
Aaron Ballman682ee422013-09-11 19:47:58 +0000734 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000735 };
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000736
737 class VariadicEnumArgument: public VariadicArgument {
738 std::string type, QualifiedTypeName;
Aaron Ballman0e468c02014-01-05 21:08:29 +0000739 std::vector<std::string> values, enums, uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000740
741 protected:
742 void writeValueImpl(raw_ostream &OS) const override {
Aaron Ballman36d79102014-09-15 16:16:14 +0000743 // FIXME: this isn't 100% correct -- some enum arguments require printing
744 // as a string literal, while others require printing as an identifier.
745 // Tablegen currently does not distinguish between the two forms.
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000746 OS << " OS << \"\\\"\" << " << getAttrName() << "Attr::Convert" << type
747 << "ToStr(Val)" << "<< \"\\\"\";\n";
748 }
749
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000750 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000751 VariadicEnumArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000752 : VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")),
753 type(Arg.getValueAsString("Type")),
Aaron Ballman0e468c02014-01-05 21:08:29 +0000754 values(Arg.getValueAsListOfStrings("Values")),
755 enums(Arg.getValueAsListOfStrings("Enums")),
Reid Klecknerf526b9482014-02-12 18:22:18 +0000756 uniques(uniqueEnumsInOrder(enums))
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000757 {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000758 QualifiedTypeName = getAttrName().str() + "Attr::" + type;
759
760 // FIXME: Emit a proper error
761 assert(!uniques.empty());
762 }
763
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000764 bool isVariadicEnumArg() const override { return true; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000765
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000766 void writeDeclarations(raw_ostream &OS) const override {
Aaron Ballman0e468c02014-01-05 21:08:29 +0000767 std::vector<std::string>::const_iterator i = uniques.begin(),
768 e = uniques.end();
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000769 // The last one needs to not have a comma.
770 --e;
771
772 OS << "public:\n";
773 OS << " enum " << type << " {\n";
774 for (; i != e; ++i)
775 OS << " " << *i << ",\n";
776 OS << " " << *e << "\n";
777 OS << " };\n";
778 OS << "private:\n";
779
780 VariadicArgument::writeDeclarations(OS);
781 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000782 void writeDump(raw_ostream &OS) const override {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000783 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
784 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
785 << getLowerName() << "_end(); I != E; ++I) {\n";
786 OS << " switch(*I) {\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000787 for (const auto &UI : uniques) {
788 OS << " case " << getAttrName() << "Attr::" << UI << ":\n";
789 OS << " OS << \" " << UI << "\";\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000790 OS << " break;\n";
791 }
792 OS << " }\n";
793 OS << " }\n";
794 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000795 void writePCHReadDecls(raw_ostream &OS) const override {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000796 OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
797 OS << " SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName()
798 << ";\n";
799 OS << " " << getLowerName() << ".reserve(" << getLowerName()
800 << "Size);\n";
801 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
802 OS << " " << getLowerName() << ".push_back(" << "static_cast<"
803 << QualifiedTypeName << ">(Record[Idx++]));\n";
804 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000805 void writePCHWrite(raw_ostream &OS) const override {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000806 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
807 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
808 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
809 << getLowerName() << "_end(); i != e; ++i)\n";
810 OS << " " << WritePCHRecord(QualifiedTypeName, "(*i)");
811 }
812 void writeConversion(raw_ostream &OS) const {
813 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
814 OS << type << " &Out) {\n";
815 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000816 OS << type << ">>(Val)\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000817 for (size_t I = 0; I < enums.size(); ++I) {
818 OS << " .Case(\"" << values[I] << "\", ";
819 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
820 }
821 OS << " .Default(Optional<" << type << ">());\n";
822 OS << " if (R) {\n";
823 OS << " Out = *R;\n return true;\n }\n";
824 OS << " return false;\n";
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000825 OS << " }\n\n";
826
827 OS << " static const char *Convert" << type << "ToStr("
828 << type << " Val) {\n"
829 << " switch(Val) {\n";
830 std::set<std::string> Uniques;
831 for (size_t I = 0; I < enums.size(); ++I) {
832 if (Uniques.insert(enums[I]).second)
833 OS << " case " << getAttrName() << "Attr::" << enums[I]
834 << ": return \"" << values[I] << "\";\n";
835 }
836 OS << " }\n"
837 << " llvm_unreachable(\"No enumerator with that value\");\n"
838 << " }\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000839 }
840 };
Peter Collingbournebee583f2011-10-06 13:03:08 +0000841
842 class VersionArgument : public Argument {
843 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000844 VersionArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000845 : Argument(Arg, Attr)
846 {}
847
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000848 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000849 OS << " VersionTuple get" << getUpperName() << "() const {\n";
850 OS << " return " << getLowerName() << ";\n";
851 OS << " }\n";
852 OS << " void set" << getUpperName()
853 << "(ASTContext &C, VersionTuple V) {\n";
854 OS << " " << getLowerName() << " = V;\n";
855 OS << " }";
856 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000857 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000858 OS << "get" << getUpperName() << "()";
859 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000860 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000861 OS << "A->get" << getUpperName() << "()";
862 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000863 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000864 OS << getLowerName() << "(" << getUpperName() << ")";
865 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000866 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000867 OS << getLowerName() << "()";
868 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000869 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000870 OS << "VersionTuple " << getUpperName();
871 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000872 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000873 OS << "VersionTuple " << getLowerName() << ";\n";
874 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000875 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000876 OS << " VersionTuple " << getLowerName()
877 << "= ReadVersionTuple(Record, Idx);\n";
878 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000879 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000880 OS << getLowerName();
881 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000882 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000883 OS << " AddVersionTuple(SA->get" << getUpperName() << "(), Record);\n";
884 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000885 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000886 OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";
887 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000888 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000889 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
890 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000891 };
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000892
893 class ExprArgument : public SimpleArgument {
894 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000895 ExprArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000896 : SimpleArgument(Arg, Attr, "Expr *")
897 {}
898
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000899 void writeASTVisitorTraversal(raw_ostream &OS) const override {
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +0000900 OS << " if (!"
901 << "getDerived().TraverseStmt(A->get" << getUpperName() << "()))\n";
902 OS << " return false;\n";
903 }
904
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000905 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000906 OS << "tempInst" << getUpperName();
907 }
908
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000909 void writeTemplateInstantiation(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000910 OS << " " << getType() << " tempInst" << getUpperName() << ";\n";
911 OS << " {\n";
912 OS << " EnterExpressionEvaluationContext "
913 << "Unevaluated(S, Sema::Unevaluated);\n";
914 OS << " ExprResult " << "Result = S.SubstExpr("
915 << "A->get" << getUpperName() << "(), TemplateArgs);\n";
916 OS << " tempInst" << getUpperName() << " = "
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000917 << "Result.getAs<Expr>();\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000918 OS << " }\n";
919 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000920
Craig Topper3164f332014-03-11 03:39:26 +0000921 void writeDump(raw_ostream &OS) const override {}
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000922
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000923 void writeDumpChildren(raw_ostream &OS) const override {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000924 OS << " lastChild();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000925 OS << " dumpStmt(SA->get" << getUpperName() << "());\n";
926 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000927 void writeHasChildren(raw_ostream &OS) const override { OS << "true"; }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000928 };
929
930 class VariadicExprArgument : public VariadicArgument {
931 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000932 VariadicExprArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000933 : VariadicArgument(Arg, Attr, "Expr *")
934 {}
935
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000936 void writeASTVisitorTraversal(raw_ostream &OS) const override {
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +0000937 OS << " {\n";
938 OS << " " << getType() << " *I = A->" << getLowerName()
939 << "_begin();\n";
940 OS << " " << getType() << " *E = A->" << getLowerName()
941 << "_end();\n";
942 OS << " for (; I != E; ++I) {\n";
943 OS << " if (!getDerived().TraverseStmt(*I))\n";
944 OS << " return false;\n";
945 OS << " }\n";
946 OS << " }\n";
947 }
948
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000949 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000950 OS << "tempInst" << getUpperName() << ", "
951 << "A->" << getLowerName() << "_size()";
952 }
953
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000954 void writeTemplateInstantiation(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000955 OS << " " << getType() << " *tempInst" << getUpperName()
956 << " = new (C, 16) " << getType()
957 << "[A->" << getLowerName() << "_size()];\n";
958 OS << " {\n";
959 OS << " EnterExpressionEvaluationContext "
960 << "Unevaluated(S, Sema::Unevaluated);\n";
961 OS << " " << getType() << " *TI = tempInst" << getUpperName()
962 << ";\n";
963 OS << " " << getType() << " *I = A->" << getLowerName()
964 << "_begin();\n";
965 OS << " " << getType() << " *E = A->" << getLowerName()
966 << "_end();\n";
967 OS << " for (; I != E; ++I, ++TI) {\n";
968 OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n";
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000969 OS << " *TI = Result.getAs<Expr>();\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000970 OS << " }\n";
971 OS << " }\n";
972 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000973
Craig Topper3164f332014-03-11 03:39:26 +0000974 void writeDump(raw_ostream &OS) const override {}
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000975
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000976 void writeDumpChildren(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000977 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
978 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
Richard Trieude5cc7d2013-01-31 01:44:26 +0000979 << getLowerName() << "_end(); I != E; ++I) {\n";
980 OS << " if (I + 1 == E)\n";
981 OS << " lastChild();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000982 OS << " dumpStmt(*I);\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +0000983 OS << " }\n";
984 }
985
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000986 void writeHasChildren(raw_ostream &OS) const override {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000987 OS << "SA->" << getLowerName() << "_begin() != "
988 << "SA->" << getLowerName() << "_end()";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000989 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000990 };
Richard Smithb87c4652013-10-31 21:23:20 +0000991
992 class TypeArgument : public SimpleArgument {
993 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000994 TypeArgument(const Record &Arg, StringRef Attr)
Richard Smithb87c4652013-10-31 21:23:20 +0000995 : SimpleArgument(Arg, Attr, "TypeSourceInfo *")
996 {}
997
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000998 void writeAccessors(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +0000999 OS << " QualType get" << getUpperName() << "() const {\n";
1000 OS << " return " << getLowerName() << "->getType();\n";
1001 OS << " }";
1002 OS << " " << getType() << " get" << getUpperName() << "Loc() const {\n";
1003 OS << " return " << getLowerName() << ";\n";
1004 OS << " }";
1005 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001006 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001007 OS << "A->get" << getUpperName() << "Loc()";
1008 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001009 void writePCHWrite(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001010 OS << " " << WritePCHRecord(
1011 getType(), "SA->get" + std::string(getUpperName()) + "Loc()");
1012 }
1013 };
Peter Collingbournebee583f2011-10-06 13:03:08 +00001014}
1015
Aaron Ballman2f22b942014-05-20 19:47:14 +00001016static std::unique_ptr<Argument>
1017createArgument(const Record &Arg, StringRef Attr,
1018 const Record *Search = nullptr) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001019 if (!Search)
1020 Search = &Arg;
1021
David Blaikie28f30ca2014-08-08 23:59:38 +00001022 std::unique_ptr<Argument> Ptr;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001023 llvm::StringRef ArgName = Search->getName();
1024
David Blaikie28f30ca2014-08-08 23:59:38 +00001025 if (ArgName == "AlignedArgument")
1026 Ptr = llvm::make_unique<AlignedArgument>(Arg, Attr);
1027 else if (ArgName == "EnumArgument")
1028 Ptr = llvm::make_unique<EnumArgument>(Arg, Attr);
1029 else if (ArgName == "ExprArgument")
1030 Ptr = llvm::make_unique<ExprArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001031 else if (ArgName == "FunctionArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001032 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "FunctionDecl *");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001033 else if (ArgName == "IdentifierArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001034 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "IdentifierInfo *");
David Majnemer4bb09802014-02-10 19:50:15 +00001035 else if (ArgName == "DefaultBoolArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001036 Ptr = llvm::make_unique<DefaultSimpleArgument>(
1037 Arg, Attr, "bool", Arg.getValueAsBit("Default"));
1038 else if (ArgName == "BoolArgument")
1039 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "bool");
Aaron Ballman18a78382013-11-21 00:28:23 +00001040 else if (ArgName == "DefaultIntArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001041 Ptr = llvm::make_unique<DefaultSimpleArgument>(
1042 Arg, Attr, "int", Arg.getValueAsInt("Default"));
1043 else if (ArgName == "IntArgument")
1044 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "int");
1045 else if (ArgName == "StringArgument")
1046 Ptr = llvm::make_unique<StringArgument>(Arg, Attr);
1047 else if (ArgName == "TypeArgument")
1048 Ptr = llvm::make_unique<TypeArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001049 else if (ArgName == "UnsignedArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001050 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "unsigned");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001051 else if (ArgName == "VariadicUnsignedArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001052 Ptr = llvm::make_unique<VariadicArgument>(Arg, Attr, "unsigned");
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001053 else if (ArgName == "VariadicEnumArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001054 Ptr = llvm::make_unique<VariadicEnumArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001055 else if (ArgName == "VariadicExprArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001056 Ptr = llvm::make_unique<VariadicExprArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001057 else if (ArgName == "VersionArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001058 Ptr = llvm::make_unique<VersionArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001059
1060 if (!Ptr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00001061 // Search in reverse order so that the most-derived type is handled first.
Peter Collingbournebee583f2011-10-06 13:03:08 +00001062 std::vector<Record*> Bases = Search->getSuperClasses();
Aaron Ballman2f22b942014-05-20 19:47:14 +00001063 for (const auto *Base : llvm::make_range(Bases.rbegin(), Bases.rend())) {
David Blaikie28f30ca2014-08-08 23:59:38 +00001064 if ((Ptr = createArgument(Arg, Attr, Base)))
Peter Collingbournebee583f2011-10-06 13:03:08 +00001065 break;
1066 }
1067 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001068
1069 if (Ptr && Arg.getValueAsBit("Optional"))
1070 Ptr->setOptional(true);
1071
David Blaikie28f30ca2014-08-08 23:59:38 +00001072 return Ptr;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001073}
1074
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001075static void writeAvailabilityValue(raw_ostream &OS) {
1076 OS << "\" << getPlatform()->getName();\n"
1077 << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n"
1078 << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n"
1079 << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n"
1080 << " if (getUnavailable()) OS << \", unavailable\";\n"
1081 << " OS << \"";
1082}
1083
Aaron Ballman3e424b52013-12-26 18:30:57 +00001084static void writeGetSpellingFunction(Record &R, raw_ostream &OS) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001085 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballman3e424b52013-12-26 18:30:57 +00001086
1087 OS << "const char *" << R.getName() << "Attr::getSpelling() const {\n";
1088 if (Spellings.empty()) {
1089 OS << " return \"(No spelling)\";\n}\n\n";
1090 return;
1091 }
1092
1093 OS << " switch (SpellingListIndex) {\n"
1094 " default:\n"
1095 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1096 " return \"(No spelling)\";\n";
1097
1098 for (unsigned I = 0; I < Spellings.size(); ++I)
1099 OS << " case " << I << ":\n"
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001100 " return \"" << Spellings[I].name() << "\";\n";
Aaron Ballman3e424b52013-12-26 18:30:57 +00001101 // End of the switch statement.
1102 OS << " }\n";
1103 // End of the getSpelling function.
1104 OS << "}\n\n";
1105}
1106
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001107static void
1108writePrettyPrintFunction(Record &R,
1109 const std::vector<std::unique_ptr<Argument>> &Args,
1110 raw_ostream &OS) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001111 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Michael Han99315932013-01-24 16:46:58 +00001112
1113 OS << "void " << R.getName() << "Attr::printPretty("
1114 << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n";
1115
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001116 if (Spellings.empty()) {
Michael Han99315932013-01-24 16:46:58 +00001117 OS << "}\n\n";
1118 return;
1119 }
1120
1121 OS <<
1122 " switch (SpellingListIndex) {\n"
1123 " default:\n"
1124 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1125 " break;\n";
1126
1127 for (unsigned I = 0; I < Spellings.size(); ++ I) {
1128 llvm::SmallString<16> Prefix;
1129 llvm::SmallString<8> Suffix;
1130 // The actual spelling of the name and namespace (if applicable)
1131 // of an attribute without considering prefix and suffix.
1132 llvm::SmallString<64> Spelling;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001133 std::string Name = Spellings[I].name();
1134 std::string Variety = Spellings[I].variety();
Michael Han99315932013-01-24 16:46:58 +00001135
1136 if (Variety == "GNU") {
1137 Prefix = " __attribute__((";
1138 Suffix = "))";
1139 } else if (Variety == "CXX11") {
1140 Prefix = " [[";
1141 Suffix = "]]";
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001142 std::string Namespace = Spellings[I].nameSpace();
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001143 if (!Namespace.empty()) {
Michael Han99315932013-01-24 16:46:58 +00001144 Spelling += Namespace;
1145 Spelling += "::";
1146 }
1147 } else if (Variety == "Declspec") {
1148 Prefix = " __declspec(";
1149 Suffix = ")";
Richard Smith0cdcc982013-01-29 01:24:26 +00001150 } else if (Variety == "Keyword") {
1151 Prefix = " ";
1152 Suffix = "";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001153 } else if (Variety == "Pragma") {
1154 Prefix = "#pragma ";
1155 Suffix = "\n";
1156 std::string Namespace = Spellings[I].nameSpace();
1157 if (!Namespace.empty()) {
1158 Spelling += Namespace;
1159 Spelling += " ";
1160 }
Michael Han99315932013-01-24 16:46:58 +00001161 } else {
Richard Smith0cdcc982013-01-29 01:24:26 +00001162 llvm_unreachable("Unknown attribute syntax variety!");
Michael Han99315932013-01-24 16:46:58 +00001163 }
1164
1165 Spelling += Name;
1166
1167 OS <<
1168 " case " << I << " : {\n"
1169 " OS << \"" + Prefix.str() + Spelling.str();
1170
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001171 if (Variety == "Pragma") {
1172 OS << " \";\n";
1173 OS << " printPrettyPragma(OS, Policy);\n";
1174 OS << " break;\n";
1175 OS << " }\n";
1176 continue;
1177 }
1178
Aaron Ballmanc960f562014-08-01 13:49:00 +00001179 // FIXME: always printing the parenthesis isn't the correct behavior for
1180 // attributes which have optional arguments that were not provided. For
1181 // instance: __attribute__((aligned)) will be pretty printed as
1182 // __attribute__((aligned())). The logic should check whether there is only
1183 // a single argument, and if it is optional, whether it has been provided.
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001184 if (!Args.empty())
1185 OS << "(";
Michael Han99315932013-01-24 16:46:58 +00001186 if (Spelling == "availability") {
1187 writeAvailabilityValue(OS);
1188 } else {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001189 for (auto I = Args.begin(), E = Args.end(); I != E; ++ I) {
Michael Han99315932013-01-24 16:46:58 +00001190 if (I != Args.begin()) OS << ", ";
1191 (*I)->writeValue(OS);
1192 }
1193 }
1194
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001195 if (!Args.empty())
1196 OS << ")";
Michael Han99315932013-01-24 16:46:58 +00001197 OS << Suffix.str() + "\";\n";
1198
1199 OS <<
1200 " break;\n"
1201 " }\n";
1202 }
1203
1204 // End of the switch statement.
1205 OS << "}\n";
1206 // End of the print function.
1207 OS << "}\n\n";
1208}
1209
Michael Hanaf02bbe2013-02-01 01:19:17 +00001210/// \brief Return the index of a spelling in a spelling list.
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001211static unsigned
1212getSpellingListIndex(const std::vector<FlattenedSpelling> &SpellingList,
1213 const FlattenedSpelling &Spelling) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00001214 assert(SpellingList.size() && "Spelling list is empty!");
1215
1216 for (unsigned Index = 0; Index < SpellingList.size(); ++Index) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001217 const FlattenedSpelling &S = SpellingList[Index];
1218 if (S.variety() != Spelling.variety())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001219 continue;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001220 if (S.nameSpace() != Spelling.nameSpace())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001221 continue;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001222 if (S.name() != Spelling.name())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001223 continue;
1224
1225 return Index;
1226 }
1227
1228 llvm_unreachable("Unknown spelling!");
1229}
1230
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001231static void writeAttrAccessorDefinition(const Record &R, raw_ostream &OS) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00001232 std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors");
Aaron Ballman2f22b942014-05-20 19:47:14 +00001233 for (const auto *Accessor : Accessors) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00001234 std::string Name = Accessor->getValueAsString("Name");
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001235 std::vector<FlattenedSpelling> Spellings =
1236 GetFlattenedSpellings(*Accessor);
1237 std::vector<FlattenedSpelling> SpellingList = GetFlattenedSpellings(R);
Michael Hanaf02bbe2013-02-01 01:19:17 +00001238 assert(SpellingList.size() &&
1239 "Attribute with empty spelling list can't have accessors!");
1240
1241 OS << " bool " << Name << "() const { return SpellingListIndex == ";
1242 for (unsigned Index = 0; Index < Spellings.size(); ++Index) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001243 OS << getSpellingListIndex(SpellingList, Spellings[Index]);
Michael Hanaf02bbe2013-02-01 01:19:17 +00001244 if (Index != Spellings.size() -1)
1245 OS << " ||\n SpellingListIndex == ";
1246 else
1247 OS << "; }\n";
1248 }
1249 }
1250}
1251
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001252static bool
1253SpellingNamesAreCommon(const std::vector<FlattenedSpelling>& Spellings) {
Aaron Ballman36a53502014-01-16 13:03:14 +00001254 assert(!Spellings.empty() && "An empty list of spellings was provided");
1255 std::string FirstName = NormalizeNameForSpellingComparison(
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001256 Spellings.front().name());
Aaron Ballman2f22b942014-05-20 19:47:14 +00001257 for (const auto &Spelling :
1258 llvm::make_range(std::next(Spellings.begin()), Spellings.end())) {
1259 std::string Name = NormalizeNameForSpellingComparison(Spelling.name());
Aaron Ballman36a53502014-01-16 13:03:14 +00001260 if (Name != FirstName)
1261 return false;
1262 }
1263 return true;
1264}
1265
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001266typedef std::map<unsigned, std::string> SemanticSpellingMap;
1267static std::string
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001268CreateSemanticSpellings(const std::vector<FlattenedSpelling> &Spellings,
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001269 SemanticSpellingMap &Map) {
1270 // The enumerants are automatically generated based on the variety,
1271 // namespace (if present) and name for each attribute spelling. However,
1272 // care is taken to avoid trampling on the reserved namespace due to
1273 // underscores.
1274 std::string Ret(" enum Spelling {\n");
1275 std::set<std::string> Uniques;
1276 unsigned Idx = 0;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001277 for (auto I = Spellings.begin(), E = Spellings.end(); I != E; ++I, ++Idx) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001278 const FlattenedSpelling &S = *I;
1279 std::string Variety = S.variety();
1280 std::string Spelling = S.name();
1281 std::string Namespace = S.nameSpace();
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001282 std::string EnumName = "";
1283
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001284 EnumName += (Variety + "_");
1285 if (!Namespace.empty())
1286 EnumName += (NormalizeNameForSpellingComparison(Namespace).str() +
1287 "_");
1288 EnumName += NormalizeNameForSpellingComparison(Spelling);
1289
1290 // Even if the name is not unique, this spelling index corresponds to a
1291 // particular enumerant name that we've calculated.
1292 Map[Idx] = EnumName;
1293
1294 // Since we have been stripping underscores to avoid trampling on the
1295 // reserved namespace, we may have inadvertently created duplicate
1296 // enumerant names. These duplicates are not considered part of the
1297 // semantic spelling, and can be elided.
1298 if (Uniques.find(EnumName) != Uniques.end())
1299 continue;
1300
1301 Uniques.insert(EnumName);
1302 if (I != Spellings.begin())
1303 Ret += ",\n";
1304 Ret += " " + EnumName;
1305 }
1306 Ret += "\n };\n\n";
1307 return Ret;
1308}
1309
1310void WriteSemanticSpellingSwitch(const std::string &VarName,
1311 const SemanticSpellingMap &Map,
1312 raw_ostream &OS) {
1313 OS << " switch (" << VarName << ") {\n default: "
1314 << "llvm_unreachable(\"Unknown spelling list index\");\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001315 for (const auto &I : Map)
1316 OS << " case " << I.first << ": return " << I.second << ";\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001317 OS << " }\n";
1318}
1319
Aaron Ballman35db2b32014-01-29 22:13:45 +00001320// Emits the LateParsed property for attributes.
1321static void emitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) {
1322 OS << "#if defined(CLANG_ATTR_LATE_PARSED_LIST)\n";
1323 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1324
Aaron Ballman2f22b942014-05-20 19:47:14 +00001325 for (const auto *Attr : Attrs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001326 bool LateParsed = Attr->getValueAsBit("LateParsed");
Aaron Ballman35db2b32014-01-29 22:13:45 +00001327
1328 if (LateParsed) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001329 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
Aaron Ballman35db2b32014-01-29 22:13:45 +00001330
1331 // FIXME: Handle non-GNU attributes
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001332 for (const auto &I : Spellings) {
1333 if (I.variety() != "GNU")
Aaron Ballman35db2b32014-01-29 22:13:45 +00001334 continue;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001335 OS << ".Case(\"" << I.name() << "\", " << LateParsed << ")\n";
Aaron Ballman35db2b32014-01-29 22:13:45 +00001336 }
1337 }
1338 }
1339 OS << "#endif // CLANG_ATTR_LATE_PARSED_LIST\n\n";
1340}
1341
1342/// \brief Emits the first-argument-is-type property for attributes.
1343static void emitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) {
1344 OS << "#if defined(CLANG_ATTR_TYPE_ARG_LIST)\n";
1345 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
1346
Aaron Ballman2f22b942014-05-20 19:47:14 +00001347 for (const auto *Attr : Attrs) {
Aaron Ballman35db2b32014-01-29 22:13:45 +00001348 // Determine whether the first argument is a type.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001349 std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
Aaron Ballman35db2b32014-01-29 22:13:45 +00001350 if (Args.empty())
1351 continue;
1352
1353 if (Args[0]->getSuperClasses().back()->getName() != "TypeArgument")
1354 continue;
1355
1356 // All these spellings take a single type argument.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001357 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
Aaron Ballman35db2b32014-01-29 22:13:45 +00001358 std::set<std::string> Emitted;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001359 for (const auto &S : Spellings) {
1360 if (Emitted.insert(S.name()).second)
1361 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
Aaron Ballman35db2b32014-01-29 22:13:45 +00001362 }
1363 }
1364 OS << "#endif // CLANG_ATTR_TYPE_ARG_LIST\n\n";
1365}
1366
1367/// \brief Emits the parse-arguments-in-unevaluated-context property for
1368/// attributes.
1369static void emitClangAttrArgContextList(RecordKeeper &Records, raw_ostream &OS) {
1370 OS << "#if defined(CLANG_ATTR_ARG_CONTEXT_LIST)\n";
1371 ParsedAttrMap Attrs = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001372 for (const auto &I : Attrs) {
1373 const Record &Attr = *I.second;
Aaron Ballman35db2b32014-01-29 22:13:45 +00001374
1375 if (!Attr.getValueAsBit("ParseArgumentsAsUnevaluated"))
1376 continue;
1377
1378 // All these spellings take are parsed unevaluated.
1379 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
1380 std::set<std::string> Emitted;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001381 for (const auto &S : Spellings) {
1382 if (Emitted.insert(S.name()).second)
1383 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
Aaron Ballman35db2b32014-01-29 22:13:45 +00001384 }
1385 }
1386 OS << "#endif // CLANG_ATTR_ARG_CONTEXT_LIST\n\n";
1387}
1388
1389static bool isIdentifierArgument(Record *Arg) {
1390 return !Arg->getSuperClasses().empty() &&
1391 llvm::StringSwitch<bool>(Arg->getSuperClasses().back()->getName())
1392 .Case("IdentifierArgument", true)
1393 .Case("EnumArgument", true)
1394 .Default(false);
1395}
1396
1397// Emits the first-argument-is-identifier property for attributes.
1398static void emitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) {
1399 OS << "#if defined(CLANG_ATTR_IDENTIFIER_ARG_LIST)\n";
1400 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1401
Aaron Ballman2f22b942014-05-20 19:47:14 +00001402 for (const auto *Attr : Attrs) {
Aaron Ballman35db2b32014-01-29 22:13:45 +00001403 // Determine whether the first argument is an identifier.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001404 std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
Aaron Ballman35db2b32014-01-29 22:13:45 +00001405 if (Args.empty() || !isIdentifierArgument(Args[0]))
1406 continue;
1407
1408 // All these spellings take an identifier argument.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001409 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
Aaron Ballman35db2b32014-01-29 22:13:45 +00001410 std::set<std::string> Emitted;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001411 for (const auto &S : Spellings) {
1412 if (Emitted.insert(S.name()).second)
1413 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
Aaron Ballman35db2b32014-01-29 22:13:45 +00001414 }
1415 }
1416 OS << "#endif // CLANG_ATTR_IDENTIFIER_ARG_LIST\n\n";
1417}
1418
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001419namespace clang {
1420
1421// Emits the class definitions for attributes.
1422void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001423 emitSourceFileHeader("Attribute classes' definitions", OS);
1424
Peter Collingbournebee583f2011-10-06 13:03:08 +00001425 OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
1426 OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
1427
1428 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1429
Aaron Ballman2f22b942014-05-20 19:47:14 +00001430 for (const auto *Attr : Attrs) {
1431 const Record &R = *Attr;
Aaron Ballman06bd44b2014-02-17 18:23:02 +00001432
1433 // FIXME: Currently, documentation is generated as-needed due to the fact
1434 // that there is no way to allow a generated project "reach into" the docs
1435 // directory (for instance, it may be an out-of-tree build). However, we want
1436 // to ensure that every attribute has a Documentation field, and produce an
1437 // error if it has been neglected. Otherwise, the on-demand generation which
1438 // happens server-side will fail. This code is ensuring that functionality,
1439 // even though this Emitter doesn't technically need the documentation.
1440 // When attribute documentation can be generated as part of the build
1441 // itself, this code can be removed.
1442 (void)R.getValueAsListOfDefs("Documentation");
Douglas Gregorb2daf842012-05-02 15:56:52 +00001443
1444 if (!R.getValueAsBit("ASTNode"))
1445 continue;
1446
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001447 const std::vector<Record *> Supers = R.getSuperClasses();
1448 assert(!Supers.empty() && "Forgot to specify a superclass for the attr");
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001449 std::string SuperName;
Aaron Ballman2f22b942014-05-20 19:47:14 +00001450 for (const auto *Super : llvm::make_range(Supers.rbegin(), Supers.rend())) {
1451 const Record &R = *Super;
Aaron Ballman080cad72013-07-31 02:20:22 +00001452 if (R.getName() != "TargetSpecificAttr" && SuperName.empty())
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001453 SuperName = R.getName();
1454 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001455
1456 OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
1457
1458 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001459 std::vector<std::unique_ptr<Argument>> Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001460 Args.reserve(ArgRecords.size());
1461
Aaron Ballman2f22b942014-05-20 19:47:14 +00001462 for (const auto *ArgRecord : ArgRecords) {
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001463 Args.emplace_back(createArgument(*ArgRecord, R.getName()));
1464 Args.back()->writeDeclarations(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001465 OS << "\n\n";
1466 }
1467
Aaron Ballman36a53502014-01-16 13:03:14 +00001468 OS << "\npublic:\n";
1469
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001470 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballman36a53502014-01-16 13:03:14 +00001471
1472 // If there are zero or one spellings, all spelling-related functionality
1473 // can be elided. If all of the spellings share the same name, the spelling
1474 // functionality can also be elided.
1475 bool ElideSpelling = (Spellings.size() <= 1) ||
1476 SpellingNamesAreCommon(Spellings);
1477
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001478 // This maps spelling index values to semantic Spelling enumerants.
1479 SemanticSpellingMap SemanticToSyntacticMap;
Aaron Ballman36a53502014-01-16 13:03:14 +00001480
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001481 if (!ElideSpelling)
1482 OS << CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
Aaron Ballman36a53502014-01-16 13:03:14 +00001483
1484 OS << " static " << R.getName() << "Attr *CreateImplicit(";
1485 OS << "ASTContext &Ctx";
1486 if (!ElideSpelling)
1487 OS << ", Spelling S";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001488 for (auto const &ai : Args) {
Aaron Ballman36a53502014-01-16 13:03:14 +00001489 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001490 ai->writeCtorParameters(OS);
Aaron Ballman36a53502014-01-16 13:03:14 +00001491 }
1492 OS << ", SourceRange Loc = SourceRange()";
1493 OS << ") {\n";
1494 OS << " " << R.getName() << "Attr *A = new (Ctx) " << R.getName();
1495 OS << "Attr(Loc, Ctx, ";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001496 for (auto const &ai : Args) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001497 ai->writeImplicitCtorArgs(OS);
Aaron Ballman36a53502014-01-16 13:03:14 +00001498 OS << ", ";
1499 }
1500 OS << (ElideSpelling ? "0" : "S") << ");\n";
1501 OS << " A->setImplicit(true);\n";
1502 OS << " return A;\n }\n\n";
1503
Peter Collingbournebee583f2011-10-06 13:03:08 +00001504 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
1505
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001506 bool HasOpt = false;
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001507 for (auto const &ai : Args) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001508 OS << " , ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001509 ai->writeCtorParameters(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001510 OS << "\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001511 if (ai->isOptional())
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001512 HasOpt = true;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001513 }
Michael Han99315932013-01-24 16:46:58 +00001514
1515 OS << " , ";
Aaron Ballman36a53502014-01-16 13:03:14 +00001516 OS << "unsigned SI\n";
Michael Han99315932013-01-24 16:46:58 +00001517
Peter Collingbournebee583f2011-10-06 13:03:08 +00001518 OS << " )\n";
Michael Han99315932013-01-24 16:46:58 +00001519 OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI)\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001520
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001521 for (auto const &ai : Args) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001522 OS << " , ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001523 ai->writeCtorInitializers(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001524 OS << "\n";
1525 }
1526
1527 OS << " {\n";
1528
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001529 for (auto const &ai : Args) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001530 ai->writeCtorBody(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001531 OS << "\n";
1532 }
1533 OS << " }\n\n";
1534
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001535 // If there are optional arguments, write out a constructor that elides the
1536 // optional arguments as well.
1537 if (HasOpt) {
1538 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001539 for (auto const &ai : Args) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001540 if (!ai->isOptional()) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001541 OS << " , ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001542 ai->writeCtorParameters(OS);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001543 OS << "\n";
1544 }
1545 }
1546
1547 OS << " , ";
Aaron Ballman36a53502014-01-16 13:03:14 +00001548 OS << "unsigned SI\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001549
1550 OS << " )\n";
1551 OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI)\n";
1552
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001553 for (auto const &ai : Args) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001554 OS << " , ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001555 ai->writeCtorDefaultInitializers(OS);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001556 OS << "\n";
1557 }
1558
1559 OS << " {\n";
1560
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001561 for (auto const &ai : Args) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001562 if (!ai->isOptional()) {
1563 ai->writeCtorBody(OS);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001564 OS << "\n";
1565 }
1566 }
1567 OS << " }\n\n";
1568 }
1569
Craig Toppercbce6e92014-03-11 06:22:39 +00001570 OS << " " << R.getName() << "Attr *clone(ASTContext &C) const override;\n";
1571 OS << " void printPretty(raw_ostream &OS,\n"
1572 << " const PrintingPolicy &Policy) const override;\n";
1573 OS << " const char *getSpelling() const override;\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001574
1575 if (!ElideSpelling) {
1576 assert(!SemanticToSyntacticMap.empty() && "Empty semantic mapping list");
1577 OS << " Spelling getSemanticSpelling() const {\n";
1578 WriteSemanticSpellingSwitch("SpellingListIndex", SemanticToSyntacticMap,
1579 OS);
1580 OS << " }\n";
1581 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001582
Michael Hanaf02bbe2013-02-01 01:19:17 +00001583 writeAttrAccessorDefinition(R, OS);
1584
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001585 for (auto const &ai : Args) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001586 ai->writeAccessors(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001587 OS << "\n\n";
Aaron Ballman682ee422013-09-11 19:47:58 +00001588
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001589 if (ai->isEnumArg())
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001590 static_cast<const EnumArgument *>(ai.get())->writeConversion(OS);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001591 else if (ai->isVariadicEnumArg())
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001592 static_cast<const VariadicEnumArgument *>(ai.get())
1593 ->writeConversion(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001594 }
1595
Jakob Stoklund Olesen6f2288b62012-01-13 04:57:47 +00001596 OS << R.getValueAsString("AdditionalMembers");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001597 OS << "\n\n";
1598
1599 OS << " static bool classof(const Attr *A) { return A->getKind() == "
1600 << "attr::" << R.getName() << "; }\n";
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001601
1602 bool LateParsed = R.getValueAsBit("LateParsed");
Craig Toppercbce6e92014-03-11 06:22:39 +00001603 OS << " bool isLateParsed() const override { return "
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001604 << LateParsed << "; }\n";
1605
Aaron Ballmanb9023ed2014-01-20 18:07:09 +00001606 if (R.getValueAsBit("DuplicatesAllowedWhileMerging"))
Craig Toppercbce6e92014-03-11 06:22:39 +00001607 OS << " bool duplicatesAllowed() const override { return true; }\n\n";
Aaron Ballmanb9023ed2014-01-20 18:07:09 +00001608
Peter Collingbournebee583f2011-10-06 13:03:08 +00001609 OS << "};\n\n";
1610 }
1611
1612 OS << "#endif\n";
1613}
1614
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001615// Emits the class method definitions for attributes.
1616void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001617 emitSourceFileHeader("Attribute classes' member function definitions", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001618
1619 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001620
Aaron Ballman2f22b942014-05-20 19:47:14 +00001621 for (auto *Attr : Attrs) {
1622 Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001623
1624 if (!R.getValueAsBit("ASTNode"))
1625 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001626
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001627 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1628 std::vector<std::unique_ptr<Argument>> Args;
Aaron Ballman2f22b942014-05-20 19:47:14 +00001629 for (const auto *Arg : ArgRecords)
1630 Args.emplace_back(createArgument(*Arg, R.getName()));
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001631
1632 for (auto const &ai : Args)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001633 ai->writeAccessorDefinitions(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001634
1635 OS << R.getName() << "Attr *" << R.getName()
1636 << "Attr::clone(ASTContext &C) const {\n";
Hans Wennborg613807b2014-05-31 01:30:30 +00001637 OS << " auto *A = new (C) " << R.getName() << "Attr(getLocation(), C";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001638 for (auto const &ai : Args) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001639 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001640 ai->writeCloneArgs(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001641 }
Hans Wennborg613807b2014-05-31 01:30:30 +00001642 OS << ", getSpellingListIndex());\n";
1643 OS << " A->Inherited = Inherited;\n";
1644 OS << " A->IsPackExpansion = IsPackExpansion;\n";
1645 OS << " A->Implicit = Implicit;\n";
1646 OS << " return A;\n}\n\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001647
Michael Han99315932013-01-24 16:46:58 +00001648 writePrettyPrintFunction(R, Args, OS);
Aaron Ballman3e424b52013-12-26 18:30:57 +00001649 writeGetSpellingFunction(R, OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001650 }
1651}
1652
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001653} // end namespace clang
1654
Peter Collingbournebee583f2011-10-06 13:03:08 +00001655static void EmitAttrList(raw_ostream &OS, StringRef Class,
1656 const std::vector<Record*> &AttrList) {
1657 std::vector<Record*>::const_iterator i = AttrList.begin(), e = AttrList.end();
1658
1659 if (i != e) {
1660 // Move the end iterator back to emit the last attribute.
Douglas Gregorb2daf842012-05-02 15:56:52 +00001661 for(--e; i != e; ++i) {
1662 if (!(*i)->getValueAsBit("ASTNode"))
1663 continue;
1664
Peter Collingbournebee583f2011-10-06 13:03:08 +00001665 OS << Class << "(" << (*i)->getName() << ")\n";
Douglas Gregorb2daf842012-05-02 15:56:52 +00001666 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001667
1668 OS << "LAST_" << Class << "(" << (*i)->getName() << ")\n\n";
1669 }
1670}
1671
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001672// Determines if an attribute has a Pragma spelling.
1673static bool AttrHasPragmaSpelling(const Record *R) {
1674 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
1675 return std::find_if(Spellings.begin(), Spellings.end(),
1676 [](const FlattenedSpelling &S) {
1677 return S.variety() == "Pragma";
1678 }) != Spellings.end();
1679}
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001680
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001681namespace clang {
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001682// Emits the enumeration list for attributes.
1683void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001684 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001685
1686 OS << "#ifndef LAST_ATTR\n";
1687 OS << "#define LAST_ATTR(NAME) ATTR(NAME)\n";
1688 OS << "#endif\n\n";
1689
1690 OS << "#ifndef INHERITABLE_ATTR\n";
1691 OS << "#define INHERITABLE_ATTR(NAME) ATTR(NAME)\n";
1692 OS << "#endif\n\n";
1693
1694 OS << "#ifndef LAST_INHERITABLE_ATTR\n";
1695 OS << "#define LAST_INHERITABLE_ATTR(NAME) INHERITABLE_ATTR(NAME)\n";
1696 OS << "#endif\n\n";
1697
1698 OS << "#ifndef INHERITABLE_PARAM_ATTR\n";
1699 OS << "#define INHERITABLE_PARAM_ATTR(NAME) ATTR(NAME)\n";
1700 OS << "#endif\n\n";
1701
1702 OS << "#ifndef LAST_INHERITABLE_PARAM_ATTR\n";
1703 OS << "#define LAST_INHERITABLE_PARAM_ATTR(NAME)"
1704 " INHERITABLE_PARAM_ATTR(NAME)\n";
1705 OS << "#endif\n\n";
1706
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001707 OS << "#ifndef PRAGMA_SPELLING_ATTR\n";
1708 OS << "#define PRAGMA_SPELLING_ATTR(NAME)\n";
1709 OS << "#endif\n\n";
1710
1711 OS << "#ifndef LAST_PRAGMA_SPELLING_ATTR\n";
1712 OS << "#define LAST_PRAGMA_SPELLING_ATTR(NAME) PRAGMA_SPELLING_ATTR(NAME)\n";
1713 OS << "#endif\n\n";
1714
Peter Collingbournebee583f2011-10-06 13:03:08 +00001715 Record *InhClass = Records.getClass("InheritableAttr");
1716 Record *InhParamClass = Records.getClass("InheritableParamAttr");
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001717 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"),
1718 NonInhAttrs, InhAttrs, InhParamAttrs, PragmaAttrs;
Aaron Ballman2f22b942014-05-20 19:47:14 +00001719 for (auto *Attr : Attrs) {
1720 if (!Attr->getValueAsBit("ASTNode"))
Douglas Gregorb2daf842012-05-02 15:56:52 +00001721 continue;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001722
1723 if (AttrHasPragmaSpelling(Attr))
1724 PragmaAttrs.push_back(Attr);
1725
Aaron Ballman2f22b942014-05-20 19:47:14 +00001726 if (Attr->isSubClassOf(InhParamClass))
1727 InhParamAttrs.push_back(Attr);
1728 else if (Attr->isSubClassOf(InhClass))
1729 InhAttrs.push_back(Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001730 else
Aaron Ballman2f22b942014-05-20 19:47:14 +00001731 NonInhAttrs.push_back(Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001732 }
1733
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001734 EmitAttrList(OS, "PRAGMA_SPELLING_ATTR", PragmaAttrs);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001735 EmitAttrList(OS, "INHERITABLE_PARAM_ATTR", InhParamAttrs);
1736 EmitAttrList(OS, "INHERITABLE_ATTR", InhAttrs);
1737 EmitAttrList(OS, "ATTR", NonInhAttrs);
1738
1739 OS << "#undef LAST_ATTR\n";
1740 OS << "#undef INHERITABLE_ATTR\n";
1741 OS << "#undef LAST_INHERITABLE_ATTR\n";
1742 OS << "#undef LAST_INHERITABLE_PARAM_ATTR\n";
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001743 OS << "#undef LAST_PRAGMA_ATTR\n";
1744 OS << "#undef PRAGMA_SPELLING_ATTR\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001745 OS << "#undef ATTR\n";
1746}
1747
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001748// Emits the code to read an attribute from a precompiled header.
1749void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001750 emitSourceFileHeader("Attribute deserialization code", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001751
1752 Record *InhClass = Records.getClass("InheritableAttr");
1753 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
1754 ArgRecords;
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001755 std::vector<std::unique_ptr<Argument>> Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001756
1757 OS << " switch (Kind) {\n";
1758 OS << " default:\n";
Craig Topperd8d43192014-06-18 05:13:13 +00001759 OS << " llvm_unreachable(\"Unknown attribute!\");\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00001760 for (const auto *Attr : Attrs) {
1761 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001762 if (!R.getValueAsBit("ASTNode"))
1763 continue;
1764
Peter Collingbournebee583f2011-10-06 13:03:08 +00001765 OS << " case attr::" << R.getName() << ": {\n";
1766 if (R.isSubClassOf(InhClass))
1767 OS << " bool isInherited = Record[Idx++];\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00001768 OS << " bool isImplicit = Record[Idx++];\n";
1769 OS << " unsigned Spelling = Record[Idx++];\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001770 ArgRecords = R.getValueAsListOfDefs("Args");
1771 Args.clear();
Aaron Ballman2f22b942014-05-20 19:47:14 +00001772 for (const auto *Arg : ArgRecords) {
1773 Args.emplace_back(createArgument(*Arg, R.getName()));
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001774 Args.back()->writePCHReadDecls(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001775 }
1776 OS << " New = new (Context) " << R.getName() << "Attr(Range, Context";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001777 for (auto const &ri : Args) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001778 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001779 ri->writePCHReadArgs(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001780 }
Aaron Ballman36a53502014-01-16 13:03:14 +00001781 OS << ", Spelling);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001782 if (R.isSubClassOf(InhClass))
1783 OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00001784 OS << " New->setImplicit(isImplicit);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001785 OS << " break;\n";
1786 OS << " }\n";
1787 }
1788 OS << " }\n";
1789}
1790
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001791// Emits the code to write an attribute to a precompiled header.
1792void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001793 emitSourceFileHeader("Attribute serialization code", OS);
1794
Peter Collingbournebee583f2011-10-06 13:03:08 +00001795 Record *InhClass = Records.getClass("InheritableAttr");
1796 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001797
1798 OS << " switch (A->getKind()) {\n";
1799 OS << " default:\n";
1800 OS << " llvm_unreachable(\"Unknown attribute kind!\");\n";
1801 OS << " break;\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00001802 for (const auto *Attr : Attrs) {
1803 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001804 if (!R.getValueAsBit("ASTNode"))
1805 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001806 OS << " case attr::" << R.getName() << ": {\n";
1807 Args = R.getValueAsListOfDefs("Args");
1808 if (R.isSubClassOf(InhClass) || !Args.empty())
1809 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
1810 << "Attr>(A);\n";
1811 if (R.isSubClassOf(InhClass))
1812 OS << " Record.push_back(SA->isInherited());\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00001813 OS << " Record.push_back(A->isImplicit());\n";
1814 OS << " Record.push_back(A->getSpellingListIndex());\n";
1815
Aaron Ballman2f22b942014-05-20 19:47:14 +00001816 for (const auto *Arg : Args)
1817 createArgument(*Arg, R.getName())->writePCHWrite(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001818 OS << " break;\n";
1819 OS << " }\n";
1820 }
1821 OS << " }\n";
1822}
1823
Aaron Ballman2fbf9942014-03-31 13:14:44 +00001824static void GenerateHasAttrSpellingStringSwitch(
1825 const std::vector<Record *> &Attrs, raw_ostream &OS,
1826 const std::string &Variety = "", const std::string &Scope = "") {
1827 for (const auto *Attr : Attrs) {
Aaron Ballman0fa06d82014-01-09 22:57:44 +00001828 // It is assumed that there will be an llvm::Triple object named T within
1829 // scope that can be used to determine whether the attribute exists in
1830 // a given target.
1831 std::string Test;
Aaron Ballman2fbf9942014-03-31 13:14:44 +00001832 if (Attr->isSubClassOf("TargetSpecificAttr")) {
1833 const Record *R = Attr->getValueAsDef("Target");
Aaron Ballman0fa06d82014-01-09 22:57:44 +00001834 std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001835
Aaron Ballman0fa06d82014-01-09 22:57:44 +00001836 Test += "(";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001837 for (auto AI = Arches.begin(), AE = Arches.end(); AI != AE; ++AI) {
Aaron Ballman0fa06d82014-01-09 22:57:44 +00001838 std::string Part = *AI;
1839 Test += "T.getArch() == llvm::Triple::" + Part;
1840 if (AI + 1 != AE)
1841 Test += " || ";
1842 }
1843 Test += ")";
1844
1845 std::vector<std::string> OSes;
1846 if (!R->isValueUnset("OSes")) {
1847 Test += " && (";
1848 std::vector<std::string> OSes = R->getValueAsListOfStrings("OSes");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001849 for (auto AI = OSes.begin(), AE = OSes.end(); AI != AE; ++AI) {
Aaron Ballman0fa06d82014-01-09 22:57:44 +00001850 std::string Part = *AI;
1851
1852 Test += "T.getOS() == llvm::Triple::" + Part;
1853 if (AI + 1 != AE)
1854 Test += " || ";
1855 }
1856 Test += ")";
1857 }
Aaron Ballman2fbf9942014-03-31 13:14:44 +00001858
1859 // If this is the C++11 variety, also add in the LangOpts test.
1860 if (Variety == "CXX11")
1861 Test += " && LangOpts.CPlusPlus11";
1862 } else if (Variety == "CXX11")
1863 // C++11 mode should be checked against LangOpts, which is presumed to be
1864 // present in the caller.
1865 Test = "LangOpts.CPlusPlus11";
1866 else
Aaron Ballman0fa06d82014-01-09 22:57:44 +00001867 Test = "true";
1868
Aaron Ballman2fbf9942014-03-31 13:14:44 +00001869 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001870 for (const auto &S : Spellings)
Aaron Ballman2fbf9942014-03-31 13:14:44 +00001871 if (Variety.empty() || (Variety == S.variety() &&
1872 (Scope.empty() || Scope == S.nameSpace())))
1873 OS << " .Case(\"" << S.name() << "\", " << Test << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001874 }
Aaron Ballman2fbf9942014-03-31 13:14:44 +00001875 OS << " .Default(false);\n";
1876}
1877
1878// Emits the list of spellings for attributes.
1879void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
1880 emitSourceFileHeader("Code to implement the __has_attribute logic", OS);
1881
1882 // Separate all of the attributes out into four group: generic, C++11, GNU,
1883 // and declspecs. Then generate a big switch statement for each of them.
1884 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001885 std::vector<Record *> Declspec, GNU, Pragma;
Aaron Ballman2fbf9942014-03-31 13:14:44 +00001886 std::map<std::string, std::vector<Record *>> CXX;
1887
1888 // Walk over the list of all attributes, and split them out based on the
1889 // spelling variety.
1890 for (auto *R : Attrs) {
1891 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
1892 for (const auto &SI : Spellings) {
1893 std::string Variety = SI.variety();
1894 if (Variety == "GNU")
1895 GNU.push_back(R);
1896 else if (Variety == "Declspec")
1897 Declspec.push_back(R);
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001898 else if (Variety == "CXX11")
Aaron Ballman2fbf9942014-03-31 13:14:44 +00001899 CXX[SI.nameSpace()].push_back(R);
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001900 else if (Variety == "Pragma")
1901 Pragma.push_back(R);
Aaron Ballman2fbf9942014-03-31 13:14:44 +00001902 }
1903 }
1904
1905 OS << "switch (Syntax) {\n";
1906 OS << "case AttrSyntax::Generic:\n";
1907 OS << " return llvm::StringSwitch<bool>(Name)\n";
1908 GenerateHasAttrSpellingStringSwitch(Attrs, OS);
1909 OS << "case AttrSyntax::GNU:\n";
1910 OS << " return llvm::StringSwitch<bool>(Name)\n";
1911 GenerateHasAttrSpellingStringSwitch(GNU, OS, "GNU");
1912 OS << "case AttrSyntax::Declspec:\n";
1913 OS << " return llvm::StringSwitch<bool>(Name)\n";
1914 GenerateHasAttrSpellingStringSwitch(Declspec, OS, "Declspec");
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001915 OS << "case AttrSyntax::Pragma:\n";
1916 OS << " return llvm::StringSwitch<bool>(Name)\n";
1917 GenerateHasAttrSpellingStringSwitch(Pragma, OS, "Pragma");
Aaron Ballman2fbf9942014-03-31 13:14:44 +00001918 OS << "case AttrSyntax::CXX: {\n";
1919 // C++11-style attributes are further split out based on the Scope.
1920 for (std::map<std::string, std::vector<Record *>>::iterator I = CXX.begin(),
1921 E = CXX.end();
1922 I != E; ++I) {
1923 if (I != CXX.begin())
1924 OS << " else ";
1925 if (I->first.empty())
1926 OS << "if (!Scope || Scope->getName() == \"\") {\n";
1927 else
1928 OS << "if (Scope->getName() == \"" << I->first << "\") {\n";
1929 OS << " return llvm::StringSwitch<bool>(Name)\n";
1930 GenerateHasAttrSpellingStringSwitch(I->second, OS, "CXX11", I->first);
1931 OS << "}";
1932 }
1933 OS << "\n}\n";
1934 OS << "}\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001935}
1936
Michael Han99315932013-01-24 16:46:58 +00001937void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001938 emitSourceFileHeader("Code to translate different attribute spellings "
1939 "into internal identifiers", OS);
Michael Han99315932013-01-24 16:46:58 +00001940
1941 OS <<
Michael Han99315932013-01-24 16:46:58 +00001942 " switch (AttrKind) {\n"
1943 " default:\n"
1944 " llvm_unreachable(\"Unknown attribute kind!\");\n"
1945 " break;\n";
1946
Aaron Ballman64e69862013-12-15 13:05:48 +00001947 ParsedAttrMap Attrs = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001948 for (const auto &I : Attrs) {
Aaron Ballman2f22b942014-05-20 19:47:14 +00001949 const Record &R = *I.second;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001950 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001951 OS << " case AT_" << I.first << ": {\n";
Richard Smith852e9ce2013-11-27 01:46:48 +00001952 for (unsigned I = 0; I < Spellings.size(); ++ I) {
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001953 OS << " if (Name == \"" << Spellings[I].name() << "\" && "
1954 << "SyntaxUsed == "
1955 << StringSwitch<unsigned>(Spellings[I].variety())
1956 .Case("GNU", 0)
1957 .Case("CXX11", 1)
1958 .Case("Declspec", 2)
1959 .Case("Keyword", 3)
1960 .Case("Pragma", 4)
1961 .Default(0)
1962 << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n"
1963 << " return " << I << ";\n";
Michael Han99315932013-01-24 16:46:58 +00001964 }
Richard Smith852e9ce2013-11-27 01:46:48 +00001965
1966 OS << " break;\n";
1967 OS << " }\n";
Michael Han99315932013-01-24 16:46:58 +00001968 }
1969
1970 OS << " }\n";
Aaron Ballman64e69862013-12-15 13:05:48 +00001971 OS << " return 0;\n";
Michael Han99315932013-01-24 16:46:58 +00001972}
1973
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001974// Emits code used by RecursiveASTVisitor to visit attributes
1975void EmitClangAttrASTVisitor(RecordKeeper &Records, raw_ostream &OS) {
1976 emitSourceFileHeader("Used by RecursiveASTVisitor to visit attributes.", OS);
1977
1978 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1979
1980 // Write method declarations for Traverse* methods.
1981 // We emit this here because we only generate methods for attributes that
1982 // are declared as ASTNodes.
1983 OS << "#ifdef ATTR_VISITOR_DECLS_ONLY\n\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00001984 for (const auto *Attr : Attrs) {
1985 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001986 if (!R.getValueAsBit("ASTNode"))
1987 continue;
1988 OS << " bool Traverse"
1989 << R.getName() << "Attr(" << R.getName() << "Attr *A);\n";
1990 OS << " bool Visit"
1991 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
1992 << " return true; \n"
1993 << " };\n";
1994 }
1995 OS << "\n#else // ATTR_VISITOR_DECLS_ONLY\n\n";
1996
1997 // Write individual Traverse* methods for each attribute class.
Aaron Ballman2f22b942014-05-20 19:47:14 +00001998 for (const auto *Attr : Attrs) {
1999 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002000 if (!R.getValueAsBit("ASTNode"))
2001 continue;
2002
2003 OS << "template <typename Derived>\n"
DeLesley Hutchinsbb79c332013-12-30 21:03:02 +00002004 << "bool VISITORCLASS<Derived>::Traverse"
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002005 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
2006 << " if (!getDerived().VisitAttr(A))\n"
2007 << " return false;\n"
2008 << " if (!getDerived().Visit" << R.getName() << "Attr(A))\n"
2009 << " return false;\n";
2010
2011 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman2f22b942014-05-20 19:47:14 +00002012 for (const auto *Arg : ArgRecords)
2013 createArgument(*Arg, R.getName())->writeASTVisitorTraversal(OS);
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002014
2015 OS << " return true;\n";
2016 OS << "}\n\n";
2017 }
2018
2019 // Write generic Traverse routine
2020 OS << "template <typename Derived>\n"
DeLesley Hutchinsbb79c332013-12-30 21:03:02 +00002021 << "bool VISITORCLASS<Derived>::TraverseAttr(Attr *A) {\n"
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002022 << " if (!A)\n"
2023 << " return true;\n"
2024 << "\n"
2025 << " switch (A->getKind()) {\n"
2026 << " default:\n"
2027 << " return true;\n";
2028
Aaron Ballman2f22b942014-05-20 19:47:14 +00002029 for (const auto *Attr : Attrs) {
2030 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002031 if (!R.getValueAsBit("ASTNode"))
2032 continue;
2033
2034 OS << " case attr::" << R.getName() << ":\n"
2035 << " return getDerived().Traverse" << R.getName() << "Attr("
2036 << "cast<" << R.getName() << "Attr>(A));\n";
2037 }
2038 OS << " }\n"; // end case
2039 OS << "}\n"; // end function
2040 OS << "#endif // ATTR_VISITOR_DECLS_ONLY\n";
2041}
2042
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002043// Emits code to instantiate dependent attributes on templates.
2044void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002045 emitSourceFileHeader("Template instantiation code for attributes", OS);
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002046
2047 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2048
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00002049 OS << "namespace clang {\n"
2050 << "namespace sema {\n\n"
2051 << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, "
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002052 << "Sema &S,\n"
2053 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"
2054 << " switch (At->getKind()) {\n"
2055 << " default:\n"
2056 << " break;\n";
2057
Aaron Ballman2f22b942014-05-20 19:47:14 +00002058 for (const auto *Attr : Attrs) {
2059 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002060 if (!R.getValueAsBit("ASTNode"))
2061 continue;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002062
2063 OS << " case attr::" << R.getName() << ": {\n";
Rafael Espindola7f90b7d2012-05-15 14:09:55 +00002064 bool ShouldClone = R.getValueAsBit("Clone");
2065
2066 if (!ShouldClone) {
2067 OS << " return NULL;\n";
2068 OS << " }\n";
2069 continue;
2070 }
2071
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002072 OS << " const " << R.getName() << "Attr *A = cast<"
2073 << R.getName() << "Attr>(At);\n";
2074 bool TDependent = R.getValueAsBit("TemplateDependent");
2075
2076 if (!TDependent) {
2077 OS << " return A->clone(C);\n";
2078 OS << " }\n";
2079 continue;
2080 }
2081
2082 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002083 std::vector<std::unique_ptr<Argument>> Args;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002084 Args.reserve(ArgRecords.size());
2085
Aaron Ballman2f22b942014-05-20 19:47:14 +00002086 for (const auto *ArgRecord : ArgRecords)
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002087 Args.emplace_back(createArgument(*ArgRecord, R.getName()));
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002088
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002089 for (auto const &ai : Args)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002090 ai->writeTemplateInstantiation(OS);
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002091
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002092 OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002093 for (auto const &ai : Args) {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002094 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002095 ai->writeTemplateInstantiationArgs(OS);
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002096 }
Aaron Ballman36a53502014-01-16 13:03:14 +00002097 OS << ", A->getSpellingListIndex());\n }\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002098 }
2099 OS << " } // end switch\n"
2100 << " llvm_unreachable(\"Unknown attribute!\");\n"
2101 << " return 0;\n"
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00002102 << "}\n\n"
2103 << "} // end namespace sema\n"
2104 << "} // end namespace clang\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002105}
2106
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002107// Emits the list of parsed attributes.
2108void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) {
2109 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
2110
2111 OS << "#ifndef PARSED_ATTR\n";
2112 OS << "#define PARSED_ATTR(NAME) NAME\n";
2113 OS << "#endif\n\n";
2114
2115 ParsedAttrMap Names = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002116 for (const auto &I : Names) {
2117 OS << "PARSED_ATTR(" << I.first << ")\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002118 }
2119}
2120
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002121static bool isArgVariadic(const Record &R, StringRef AttrName) {
2122 return createArgument(R, AttrName)->isVariadic();
2123}
2124
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002125static void emitArgInfo(const Record &R, std::stringstream &OS) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002126 // This function will count the number of arguments specified for the
2127 // attribute and emit the number of required arguments followed by the
2128 // number of optional arguments.
2129 std::vector<Record *> Args = R.getValueAsListOfDefs("Args");
2130 unsigned ArgCount = 0, OptCount = 0;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002131 bool HasVariadic = false;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002132 for (const auto *Arg : Args) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002133 Arg->getValueAsBit("Optional") ? ++OptCount : ++ArgCount;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002134 if (!HasVariadic && isArgVariadic(*Arg, R.getName()))
2135 HasVariadic = true;
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002136 }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002137
2138 // If there is a variadic argument, we will set the optional argument count
2139 // to its largest value. Since it's currently a 4-bit number, we set it to 15.
2140 OS << ArgCount << ", " << (HasVariadic ? 15 : OptCount);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002141}
2142
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002143static void GenerateDefaultAppertainsTo(raw_ostream &OS) {
Aaron Ballman93b5cc62013-12-02 19:36:42 +00002144 OS << "static bool defaultAppertainsTo(Sema &, const AttributeList &,";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002145 OS << "const Decl *) {\n";
2146 OS << " return true;\n";
2147 OS << "}\n\n";
2148}
2149
2150static std::string CalculateDiagnostic(const Record &S) {
2151 // If the SubjectList object has a custom diagnostic associated with it,
2152 // return that directly.
2153 std::string CustomDiag = S.getValueAsString("CustomDiag");
2154 if (!CustomDiag.empty())
2155 return CustomDiag;
2156
2157 // Given the list of subjects, determine what diagnostic best fits.
2158 enum {
2159 Func = 1U << 0,
2160 Var = 1U << 1,
2161 ObjCMethod = 1U << 2,
2162 Param = 1U << 3,
2163 Class = 1U << 4,
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00002164 GenericRecord = 1U << 5,
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002165 Type = 1U << 6,
2166 ObjCIVar = 1U << 7,
2167 ObjCProp = 1U << 8,
2168 ObjCInterface = 1U << 9,
2169 Block = 1U << 10,
2170 Namespace = 1U << 11,
Aaron Ballman981ba242014-05-20 14:10:53 +00002171 Field = 1U << 12,
2172 CXXMethod = 1U << 13,
2173 ObjCProtocol = 1U << 14
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002174 };
2175 uint32_t SubMask = 0;
2176
2177 std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects");
Aaron Ballman2f22b942014-05-20 19:47:14 +00002178 for (const auto *Subject : Subjects) {
2179 const Record &R = *Subject;
Aaron Ballman80469032013-11-29 14:57:58 +00002180 std::string Name;
2181
2182 if (R.isSubClassOf("SubsetSubject")) {
2183 PrintError(R.getLoc(), "SubsetSubjects should use a custom diagnostic");
2184 // As a fallback, look through the SubsetSubject to see what its base
2185 // type is, and use that. This needs to be updated if SubsetSubjects
2186 // are allowed within other SubsetSubjects.
2187 Name = R.getValueAsDef("Base")->getName();
2188 } else
2189 Name = R.getName();
2190
2191 uint32_t V = StringSwitch<uint32_t>(Name)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002192 .Case("Function", Func)
2193 .Case("Var", Var)
2194 .Case("ObjCMethod", ObjCMethod)
2195 .Case("ParmVar", Param)
2196 .Case("TypedefName", Type)
2197 .Case("ObjCIvar", ObjCIVar)
2198 .Case("ObjCProperty", ObjCProp)
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00002199 .Case("Record", GenericRecord)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002200 .Case("ObjCInterface", ObjCInterface)
Ted Kremenekd980da22013-12-10 19:43:42 +00002201 .Case("ObjCProtocol", ObjCProtocol)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002202 .Case("Block", Block)
2203 .Case("CXXRecord", Class)
2204 .Case("Namespace", Namespace)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002205 .Case("Field", Field)
2206 .Case("CXXMethod", CXXMethod)
2207 .Default(0);
2208 if (!V) {
2209 // Something wasn't in our mapping, so be helpful and let the developer
2210 // know about it.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002211 PrintFatalError(R.getLoc(), "Unknown subject type: " + R.getName());
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002212 return "";
2213 }
2214
2215 SubMask |= V;
2216 }
2217
2218 switch (SubMask) {
2219 // For the simple cases where there's only a single entry in the mask, we
2220 // don't have to resort to bit fiddling.
2221 case Func: return "ExpectedFunction";
2222 case Var: return "ExpectedVariable";
2223 case Param: return "ExpectedParameter";
2224 case Class: return "ExpectedClass";
2225 case CXXMethod:
2226 // FIXME: Currently, this maps to ExpectedMethod based on existing code,
2227 // but should map to something a bit more accurate at some point.
2228 case ObjCMethod: return "ExpectedMethod";
2229 case Type: return "ExpectedType";
2230 case ObjCInterface: return "ExpectedObjectiveCInterface";
Ted Kremenekd980da22013-12-10 19:43:42 +00002231 case ObjCProtocol: return "ExpectedObjectiveCProtocol";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002232
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00002233 // "GenericRecord" means struct, union or class; check the language options
2234 // and if not compiling for C++, strip off the class part. Note that this
2235 // relies on the fact that the context for this declares "Sema &S".
2236 case GenericRecord:
Aaron Ballman17046b82013-11-27 19:16:55 +00002237 return "(S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass : "
2238 "ExpectedStructOrUnion)";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002239 case Func | ObjCMethod | Block: return "ExpectedFunctionMethodOrBlock";
2240 case Func | ObjCMethod | Class: return "ExpectedFunctionMethodOrClass";
2241 case Func | Param:
2242 case Func | ObjCMethod | Param: return "ExpectedFunctionMethodOrParameter";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002243 case Func | ObjCMethod: return "ExpectedFunctionOrMethod";
2244 case Func | Var: return "ExpectedVariableOrFunction";
Aaron Ballman604dfec2013-12-02 17:07:07 +00002245
2246 // If not compiling for C++, the class portion does not apply.
2247 case Func | Var | Class:
2248 return "(S.getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass : "
2249 "ExpectedVariableOrFunction)";
2250
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002251 case ObjCMethod | ObjCProp: return "ExpectedMethodOrProperty";
Aaron Ballman173361e2014-07-16 20:28:10 +00002252 case ObjCProtocol | ObjCInterface:
2253 return "ExpectedObjectiveCInterfaceOrProtocol";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002254 case Field | Var: return "ExpectedFieldOrGlobalVar";
2255 }
2256
2257 PrintFatalError(S.getLoc(),
2258 "Could not deduce diagnostic argument for Attr subjects");
2259
2260 return "";
2261}
2262
Aaron Ballman12b9f652014-01-16 13:55:42 +00002263static std::string GetSubjectWithSuffix(const Record *R) {
2264 std::string B = R->getName();
2265 if (B == "DeclBase")
2266 return "Decl";
2267 return B + "Decl";
2268}
Aaron Ballman80469032013-11-29 14:57:58 +00002269static std::string GenerateCustomAppertainsTo(const Record &Subject,
2270 raw_ostream &OS) {
Aaron Ballmana358c902013-12-02 14:58:17 +00002271 std::string FnName = "is" + Subject.getName();
2272
Aaron Ballman80469032013-11-29 14:57:58 +00002273 // If this code has already been generated, simply return the previous
2274 // instance of it.
2275 static std::set<std::string> CustomSubjectSet;
Aaron Ballmana358c902013-12-02 14:58:17 +00002276 std::set<std::string>::iterator I = CustomSubjectSet.find(FnName);
Aaron Ballman80469032013-11-29 14:57:58 +00002277 if (I != CustomSubjectSet.end())
2278 return *I;
2279
2280 Record *Base = Subject.getValueAsDef("Base");
2281
2282 // Not currently support custom subjects within custom subjects.
2283 if (Base->isSubClassOf("SubsetSubject")) {
2284 PrintFatalError(Subject.getLoc(),
2285 "SubsetSubjects within SubsetSubjects is not supported");
2286 return "";
2287 }
2288
Aaron Ballman80469032013-11-29 14:57:58 +00002289 OS << "static bool " << FnName << "(const Decl *D) {\n";
Aaron Ballman47553042014-01-16 14:32:03 +00002290 OS << " if (const " << GetSubjectWithSuffix(Base) << " *S = dyn_cast<";
Aaron Ballman12b9f652014-01-16 13:55:42 +00002291 OS << GetSubjectWithSuffix(Base);
Aaron Ballman47553042014-01-16 14:32:03 +00002292 OS << ">(D))\n";
2293 OS << " return " << Subject.getValueAsString("CheckCode") << ";\n";
2294 OS << " return false;\n";
Aaron Ballman80469032013-11-29 14:57:58 +00002295 OS << "}\n\n";
2296
2297 CustomSubjectSet.insert(FnName);
2298 return FnName;
2299}
2300
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002301static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) {
2302 // If the attribute does not contain a Subjects definition, then use the
2303 // default appertainsTo logic.
2304 if (Attr.isValueUnset("Subjects"))
Aaron Ballman93b5cc62013-12-02 19:36:42 +00002305 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002306
2307 const Record *SubjectObj = Attr.getValueAsDef("Subjects");
2308 std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
2309
2310 // If the list of subjects is empty, it is assumed that the attribute
2311 // appertains to everything.
2312 if (Subjects.empty())
Aaron Ballman93b5cc62013-12-02 19:36:42 +00002313 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002314
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002315 bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn");
2316
2317 // Otherwise, generate an appertainsTo check specific to this attribute which
2318 // checks all of the given subjects against the Decl passed in. Return the
2319 // name of that check to the caller.
Aaron Ballman00dcc432013-12-03 13:45:50 +00002320 std::string FnName = "check" + Attr.getName() + "AppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002321 std::stringstream SS;
2322 SS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr, ";
2323 SS << "const Decl *D) {\n";
2324 SS << " if (";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002325 for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) {
Aaron Ballman80469032013-11-29 14:57:58 +00002326 // If the subject has custom code associated with it, generate a function
2327 // for it. The function cannot be inlined into this check (yet) because it
2328 // requires the subject to be of a specific type, and were that information
2329 // inlined here, it would not support an attribute with multiple custom
2330 // subjects.
2331 if ((*I)->isSubClassOf("SubsetSubject")) {
2332 SS << "!" << GenerateCustomAppertainsTo(**I, OS) << "(D)";
2333 } else {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002334 SS << "!isa<" << GetSubjectWithSuffix(*I) << ">(D)";
Aaron Ballman80469032013-11-29 14:57:58 +00002335 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002336
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002337 if (I + 1 != E)
2338 SS << " && ";
2339 }
2340 SS << ") {\n";
2341 SS << " S.Diag(Attr.getLoc(), diag::";
2342 SS << (Warn ? "warn_attribute_wrong_decl_type" :
2343 "err_attribute_wrong_decl_type");
2344 SS << ")\n";
2345 SS << " << Attr.getName() << ";
2346 SS << CalculateDiagnostic(*SubjectObj) << ";\n";
2347 SS << " return false;\n";
2348 SS << " }\n";
2349 SS << " return true;\n";
2350 SS << "}\n\n";
2351
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002352 OS << SS.str();
2353 return FnName;
2354}
2355
Aaron Ballman3aff6332013-12-02 19:30:36 +00002356static void GenerateDefaultLangOptRequirements(raw_ostream &OS) {
2357 OS << "static bool defaultDiagnoseLangOpts(Sema &, ";
2358 OS << "const AttributeList &) {\n";
2359 OS << " return true;\n";
2360 OS << "}\n\n";
2361}
2362
2363static std::string GenerateLangOptRequirements(const Record &R,
2364 raw_ostream &OS) {
2365 // If the attribute has an empty or unset list of language requirements,
2366 // return the default handler.
2367 std::vector<Record *> LangOpts = R.getValueAsListOfDefs("LangOpts");
2368 if (LangOpts.empty())
2369 return "defaultDiagnoseLangOpts";
2370
2371 // Generate the test condition, as well as a unique function name for the
2372 // diagnostic test. The list of options should usually be short (one or two
2373 // options), and the uniqueness isn't strictly necessary (it is just for
2374 // codegen efficiency).
2375 std::string FnName = "check", Test;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002376 for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00002377 std::string Part = (*I)->getValueAsString("Name");
2378 Test += "S.LangOpts." + Part;
2379 if (I + 1 != E)
2380 Test += " || ";
2381 FnName += Part;
2382 }
2383 FnName += "LangOpts";
2384
2385 // If this code has already been generated, simply return the previous
2386 // instance of it.
2387 static std::set<std::string> CustomLangOptsSet;
2388 std::set<std::string>::iterator I = CustomLangOptsSet.find(FnName);
2389 if (I != CustomLangOptsSet.end())
2390 return *I;
2391
2392 OS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr) {\n";
2393 OS << " if (" << Test << ")\n";
2394 OS << " return true;\n\n";
2395 OS << " S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) ";
2396 OS << "<< Attr.getName();\n";
2397 OS << " return false;\n";
2398 OS << "}\n\n";
2399
2400 CustomLangOptsSet.insert(FnName);
2401 return FnName;
2402}
2403
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002404static void GenerateDefaultTargetRequirements(raw_ostream &OS) {
Benjamin Kramer9299637dc2014-03-04 19:31:42 +00002405 OS << "static bool defaultTargetRequirements(const llvm::Triple &) {\n";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002406 OS << " return true;\n";
2407 OS << "}\n\n";
2408}
2409
2410static std::string GenerateTargetRequirements(const Record &Attr,
2411 const ParsedAttrMap &Dupes,
2412 raw_ostream &OS) {
2413 // If the attribute is not a target specific attribute, return the default
2414 // target handler.
2415 if (!Attr.isSubClassOf("TargetSpecificAttr"))
2416 return "defaultTargetRequirements";
2417
2418 // Get the list of architectures to be tested for.
2419 const Record *R = Attr.getValueAsDef("Target");
2420 std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches");
2421 if (Arches.empty()) {
2422 PrintError(Attr.getLoc(), "Empty list of target architectures for a "
2423 "target-specific attr");
2424 return "defaultTargetRequirements";
2425 }
2426
2427 // If there are other attributes which share the same parsed attribute kind,
2428 // such as target-specific attributes with a shared spelling, collapse the
2429 // duplicate architectures. This is required because a shared target-specific
2430 // attribute has only one AttributeList::Kind enumeration value, but it
2431 // applies to multiple target architectures. In order for the attribute to be
2432 // considered valid, all of its architectures need to be included.
2433 if (!Attr.isValueUnset("ParseKind")) {
2434 std::string APK = Attr.getValueAsString("ParseKind");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002435 for (const auto &I : Dupes) {
2436 if (I.first == APK) {
2437 std::vector<std::string> DA = I.second->getValueAsDef("Target")
2438 ->getValueAsListOfStrings("Arches");
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002439 std::copy(DA.begin(), DA.end(), std::back_inserter(Arches));
2440 }
2441 }
2442 }
2443
2444 std::string FnName = "isTarget", Test = "(";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002445 for (auto I = Arches.begin(), E = Arches.end(); I != E; ++I) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002446 std::string Part = *I;
2447 Test += "Arch == llvm::Triple::" + Part;
2448 if (I + 1 != E)
2449 Test += " || ";
2450 FnName += Part;
2451 }
2452 Test += ")";
2453
2454 // If the target also requires OS testing, generate those tests as well.
2455 bool UsesOS = false;
2456 if (!R->isValueUnset("OSes")) {
2457 UsesOS = true;
2458
2459 // We know that there was at least one arch test, so we need to and in the
2460 // OS tests.
2461 Test += " && (";
2462 std::vector<std::string> OSes = R->getValueAsListOfStrings("OSes");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002463 for (auto I = OSes.begin(), E = OSes.end(); I != E; ++I) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002464 std::string Part = *I;
2465
2466 Test += "OS == llvm::Triple::" + Part;
2467 if (I + 1 != E)
2468 Test += " || ";
2469 FnName += Part;
2470 }
2471 Test += ")";
2472 }
2473
2474 // If this code has already been generated, simply return the previous
2475 // instance of it.
2476 static std::set<std::string> CustomTargetSet;
2477 std::set<std::string>::iterator I = CustomTargetSet.find(FnName);
2478 if (I != CustomTargetSet.end())
2479 return *I;
2480
Benjamin Kramer9299637dc2014-03-04 19:31:42 +00002481 OS << "static bool " << FnName << "(const llvm::Triple &T) {\n";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002482 OS << " llvm::Triple::ArchType Arch = T.getArch();\n";
2483 if (UsesOS)
2484 OS << " llvm::Triple::OSType OS = T.getOS();\n";
2485 OS << " return " << Test << ";\n";
2486 OS << "}\n\n";
2487
2488 CustomTargetSet.insert(FnName);
2489 return FnName;
2490}
2491
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002492static void GenerateDefaultSpellingIndexToSemanticSpelling(raw_ostream &OS) {
2493 OS << "static unsigned defaultSpellingIndexToSemanticSpelling("
2494 << "const AttributeList &Attr) {\n";
2495 OS << " return UINT_MAX;\n";
2496 OS << "}\n\n";
2497}
2498
2499static std::string GenerateSpellingIndexToSemanticSpelling(const Record &Attr,
2500 raw_ostream &OS) {
2501 // If the attribute does not have a semantic form, we can bail out early.
2502 if (!Attr.getValueAsBit("ASTNode"))
2503 return "defaultSpellingIndexToSemanticSpelling";
2504
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002505 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002506
2507 // If there are zero or one spellings, or all of the spellings share the same
2508 // name, we can also bail out early.
2509 if (Spellings.size() <= 1 || SpellingNamesAreCommon(Spellings))
2510 return "defaultSpellingIndexToSemanticSpelling";
2511
2512 // Generate the enumeration we will use for the mapping.
2513 SemanticSpellingMap SemanticToSyntacticMap;
2514 std::string Enum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
2515 std::string Name = Attr.getName() + "AttrSpellingMap";
2516
2517 OS << "static unsigned " << Name << "(const AttributeList &Attr) {\n";
2518 OS << Enum;
2519 OS << " unsigned Idx = Attr.getAttributeSpellingListIndex();\n";
2520 WriteSemanticSpellingSwitch("Idx", SemanticToSyntacticMap, OS);
2521 OS << "}\n\n";
2522
2523 return Name;
2524}
2525
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002526static bool IsKnownToGCC(const Record &Attr) {
2527 // Look at the spellings for this subject; if there are any spellings which
2528 // claim to be known to GCC, the attribute is known to GCC.
2529 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002530 for (const auto &I : Spellings) {
2531 if (I.knownToGCC())
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00002532 return true;
2533 }
2534 return false;
2535}
2536
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002537/// Emits the parsed attribute helpers
2538void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
2539 emitSourceFileHeader("Parsed attribute helpers", OS);
2540
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002541 // Get the list of parsed attributes, and accept the optional list of
2542 // duplicates due to the ParseKind.
2543 ParsedAttrMap Dupes;
2544 ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002545
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002546 // Generate the default appertainsTo, target and language option diagnostic,
2547 // and spelling list index mapping methods.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002548 GenerateDefaultAppertainsTo(OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00002549 GenerateDefaultLangOptRequirements(OS);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002550 GenerateDefaultTargetRequirements(OS);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002551 GenerateDefaultSpellingIndexToSemanticSpelling(OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002552
2553 // Generate the appertainsTo diagnostic methods and write their names into
2554 // another mapping. At the same time, generate the AttrInfoMap object
2555 // contents. Due to the reliance on generated code, use separate streams so
2556 // that code will not be interleaved.
2557 std::stringstream SS;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002558 for (auto I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002559 // TODO: If the attribute's kind appears in the list of duplicates, that is
2560 // because it is a target-specific attribute that appears multiple times.
2561 // It would be beneficial to test whether the duplicates are "similar
2562 // enough" to each other to not cause problems. For instance, check that
Alp Toker96cf7582014-01-18 21:49:37 +00002563 // the spellings are identical, and custom parsing rules match, etc.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002564
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002565 // We need to generate struct instances based off ParsedAttrInfo from
2566 // AttributeList.cpp.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002567 SS << " { ";
2568 emitArgInfo(*I->second, SS);
2569 SS << ", " << I->second->getValueAsBit("HasCustomParsing");
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002570 SS << ", " << I->second->isSubClassOf("TargetSpecificAttr");
2571 SS << ", " << I->second->isSubClassOf("TypeAttr");
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002572 SS << ", " << IsKnownToGCC(*I->second);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002573 SS << ", " << GenerateAppertainsTo(*I->second, OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00002574 SS << ", " << GenerateLangOptRequirements(*I->second, OS);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002575 SS << ", " << GenerateTargetRequirements(*I->second, Dupes, OS);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002576 SS << ", " << GenerateSpellingIndexToSemanticSpelling(*I->second, OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002577 SS << " }";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002578
2579 if (I + 1 != E)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002580 SS << ",";
2581
2582 SS << " // AT_" << I->first << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002583 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002584
2585 OS << "static const ParsedAttrInfo AttrInfoMap[AttributeList::UnknownAttribute + 1] = {\n";
2586 OS << SS.str();
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002587 OS << "};\n\n";
Michael Han4a045172012-03-07 00:12:16 +00002588}
2589
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002590// Emits the kind list of parsed attributes
2591void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002592 emitSourceFileHeader("Attribute name matcher", OS);
2593
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002594 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002595 std::vector<StringMatcher::StringPair> GNU, Declspec, CXX11, Keywords, Pragma;
Aaron Ballman64e69862013-12-15 13:05:48 +00002596 std::set<std::string> Seen;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002597 for (const auto *A : Attrs) {
2598 const Record &Attr = *A;
Richard Smith852e9ce2013-11-27 01:46:48 +00002599
Michael Han4a045172012-03-07 00:12:16 +00002600 bool SemaHandler = Attr.getValueAsBit("SemaHandler");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00002601 bool Ignored = Attr.getValueAsBit("Ignored");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00002602 if (SemaHandler || Ignored) {
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002603 // Attribute spellings can be shared between target-specific attributes,
2604 // and can be shared between syntaxes for the same attribute. For
2605 // instance, an attribute can be spelled GNU<"interrupt"> for an ARM-
2606 // specific attribute, or MSP430-specific attribute. Additionally, an
2607 // attribute can be spelled GNU<"dllexport"> and Declspec<"dllexport">
2608 // for the same semantic attribute. Ultimately, we need to map each of
2609 // these to a single AttributeList::Kind value, but the StringMatcher
2610 // class cannot handle duplicate match strings. So we generate a list of
2611 // string to match based on the syntax, and emit multiple string matchers
2612 // depending on the syntax used.
Aaron Ballman64e69862013-12-15 13:05:48 +00002613 std::string AttrName;
2614 if (Attr.isSubClassOf("TargetSpecificAttr") &&
2615 !Attr.isValueUnset("ParseKind")) {
2616 AttrName = Attr.getValueAsString("ParseKind");
2617 if (Seen.find(AttrName) != Seen.end())
2618 continue;
2619 Seen.insert(AttrName);
2620 } else
2621 AttrName = NormalizeAttrName(StringRef(Attr.getName())).str();
2622
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002623 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002624 for (const auto &S : Spellings) {
2625 std::string RawSpelling = S.name();
Craig Topper8ae12032014-05-07 06:21:57 +00002626 std::vector<StringMatcher::StringPair> *Matches = nullptr;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002627 std::string Spelling, Variety = S.variety();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002628 if (Variety == "CXX11") {
2629 Matches = &CXX11;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002630 Spelling += S.nameSpace();
Alexis Hunt3bc72c12012-06-19 23:57:03 +00002631 Spelling += "::";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002632 } else if (Variety == "GNU")
2633 Matches = &GNU;
2634 else if (Variety == "Declspec")
2635 Matches = &Declspec;
2636 else if (Variety == "Keyword")
2637 Matches = &Keywords;
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002638 else if (Variety == "Pragma")
2639 Matches = &Pragma;
Alexis Hunta0e54d42012-06-18 16:13:52 +00002640
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002641 assert(Matches && "Unsupported spelling variety found");
2642
2643 Spelling += NormalizeAttrSpelling(RawSpelling);
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00002644 if (SemaHandler)
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002645 Matches->push_back(StringMatcher::StringPair(Spelling,
2646 "return AttributeList::AT_" + AttrName + ";"));
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00002647 else
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002648 Matches->push_back(StringMatcher::StringPair(Spelling,
2649 "return AttributeList::IgnoredAttribute;"));
Michael Han4a045172012-03-07 00:12:16 +00002650 }
2651 }
2652 }
Douglas Gregor377f99b2012-05-02 17:33:51 +00002653
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002654 OS << "static AttributeList::Kind getAttrKind(StringRef Name, ";
2655 OS << "AttributeList::Syntax Syntax) {\n";
2656 OS << " if (AttributeList::AS_GNU == Syntax) {\n";
2657 StringMatcher("Name", GNU, OS).Emit();
2658 OS << " } else if (AttributeList::AS_Declspec == Syntax) {\n";
2659 StringMatcher("Name", Declspec, OS).Emit();
2660 OS << " } else if (AttributeList::AS_CXX11 == Syntax) {\n";
2661 StringMatcher("Name", CXX11, OS).Emit();
2662 OS << " } else if (AttributeList::AS_Keyword == Syntax) {\n";
2663 StringMatcher("Name", Keywords, OS).Emit();
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002664 OS << " } else if (AttributeList::AS_Pragma == Syntax) {\n";
2665 StringMatcher("Name", Pragma, OS).Emit();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002666 OS << " }\n";
2667 OS << " return AttributeList::UnknownAttribute;\n"
Douglas Gregor377f99b2012-05-02 17:33:51 +00002668 << "}\n";
Michael Han4a045172012-03-07 00:12:16 +00002669}
2670
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00002671// Emits the code to dump an attribute.
2672void EmitClangAttrDump(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002673 emitSourceFileHeader("Attribute dumper", OS);
2674
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00002675 OS <<
2676 " switch (A->getKind()) {\n"
2677 " default:\n"
2678 " llvm_unreachable(\"Unknown attribute kind!\");\n"
2679 " break;\n";
2680 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002681 for (const auto *Attr : Attrs) {
2682 const Record &R = *Attr;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00002683 if (!R.getValueAsBit("ASTNode"))
2684 continue;
2685 OS << " case attr::" << R.getName() << ": {\n";
Aaron Ballmanbc909612014-01-22 21:51:20 +00002686
2687 // If the attribute has a semantically-meaningful name (which is determined
2688 // by whether there is a Spelling enumeration for it), then write out the
2689 // spelling used for the attribute.
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002690 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballmanbc909612014-01-22 21:51:20 +00002691 if (Spellings.size() > 1 && !SpellingNamesAreCommon(Spellings))
2692 OS << " OS << \" \" << A->getSpelling();\n";
2693
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00002694 Args = R.getValueAsListOfDefs("Args");
2695 if (!Args.empty()) {
2696 OS << " const " << R.getName() << "Attr *SA = cast<" << R.getName()
2697 << "Attr>(A);\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002698 for (const auto *Arg : Args)
2699 createArgument(*Arg, R.getName())->writeDump(OS);
Richard Trieude5cc7d2013-01-31 01:44:26 +00002700
2701 // Code for detecting the last child.
2702 OS << " bool OldMoreChildren = hasMoreChildren();\n";
Arnaud A. de Grandmaisonc6b40452014-03-21 22:35:34 +00002703 OS << " bool MoreChildren;\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +00002704
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002705 for (auto AI = Args.begin(), AE = Args.end(); AI != AE; ++AI) {
Richard Trieude5cc7d2013-01-31 01:44:26 +00002706 // More code for detecting the last child.
2707 OS << " MoreChildren = OldMoreChildren";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002708 for (auto Next = AI + 1; Next != AE; ++Next) {
Richard Trieude5cc7d2013-01-31 01:44:26 +00002709 OS << " || ";
2710 createArgument(**Next, R.getName())->writeHasChildren(OS);
2711 }
2712 OS << ";\n";
2713 OS << " setMoreChildren(MoreChildren);\n";
2714
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002715 createArgument(**AI, R.getName())->writeDumpChildren(OS);
Richard Trieude5cc7d2013-01-31 01:44:26 +00002716 }
2717
2718 // Reset the last child.
2719 OS << " setMoreChildren(OldMoreChildren);\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00002720 }
2721 OS <<
2722 " break;\n"
2723 " }\n";
2724 }
2725 OS << " }\n";
2726}
2727
Aaron Ballman35db2b32014-01-29 22:13:45 +00002728void EmitClangAttrParserStringSwitches(RecordKeeper &Records,
2729 raw_ostream &OS) {
2730 emitSourceFileHeader("Parser-related llvm::StringSwitch cases", OS);
2731 emitClangAttrArgContextList(Records, OS);
2732 emitClangAttrIdentifierArgList(Records, OS);
2733 emitClangAttrTypeArgList(Records, OS);
2734 emitClangAttrLateParsedList(Records, OS);
2735}
2736
Aaron Ballman97dba042014-02-17 15:27:10 +00002737class DocumentationData {
2738public:
Aaron Ballman1a3e5852014-02-17 16:18:32 +00002739 const Record *Documentation;
2740 const Record *Attribute;
Aaron Ballman97dba042014-02-17 15:27:10 +00002741
Aaron Ballman4de1b582014-02-19 22:59:32 +00002742 DocumentationData(const Record &Documentation, const Record &Attribute)
2743 : Documentation(&Documentation), Attribute(&Attribute) {}
Aaron Ballman97dba042014-02-17 15:27:10 +00002744};
2745
Aaron Ballman4de1b582014-02-19 22:59:32 +00002746static void WriteCategoryHeader(const Record *DocCategory,
Aaron Ballman97dba042014-02-17 15:27:10 +00002747 raw_ostream &OS) {
Aaron Ballman4de1b582014-02-19 22:59:32 +00002748 const std::string &Name = DocCategory->getValueAsString("Name");
2749 OS << Name << "\n" << std::string(Name.length(), '=') << "\n";
2750
2751 // If there is content, print that as well.
2752 std::string ContentStr = DocCategory->getValueAsString("Content");
2753 if (!ContentStr.empty()) {
2754 // Trim leading and trailing newlines and spaces.
2755 StringRef Content(ContentStr);
2756 while (Content.startswith("\r") || Content.startswith("\n") ||
2757 Content.startswith(" ") || Content.startswith("\t"))
2758 Content = Content.substr(1);
2759 while (Content.endswith("\r") || Content.endswith("\n") ||
2760 Content.endswith(" ") || Content.endswith("\t"))
2761 Content = Content.substr(0, Content.size() - 1);
2762 OS << Content;
Aaron Ballman97dba042014-02-17 15:27:10 +00002763 }
Aaron Ballman4de1b582014-02-19 22:59:32 +00002764 OS << "\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00002765}
2766
Aaron Ballmana66b5742014-02-17 15:36:08 +00002767enum SpellingKind {
2768 GNU = 1 << 0,
2769 CXX11 = 1 << 1,
2770 Declspec = 1 << 2,
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002771 Keyword = 1 << 3,
2772 Pragma = 1 << 4
Aaron Ballmana66b5742014-02-17 15:36:08 +00002773};
2774
Aaron Ballman97dba042014-02-17 15:27:10 +00002775static void WriteDocumentation(const DocumentationData &Doc,
2776 raw_ostream &OS) {
2777 // FIXME: there is no way to have a per-spelling category for the attribute
2778 // documentation. This may not be a limiting factor since the spellings
2779 // should generally be consistently applied across the category.
2780
Aaron Ballman1a3e5852014-02-17 16:18:32 +00002781 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Doc.Attribute);
Aaron Ballman97dba042014-02-17 15:27:10 +00002782
2783 // Determine the heading to be used for this attribute.
Aaron Ballman1a3e5852014-02-17 16:18:32 +00002784 std::string Heading = Doc.Documentation->getValueAsString("Heading");
Aaron Ballmanea6668c2014-02-21 14:14:04 +00002785 bool CustomHeading = !Heading.empty();
Aaron Ballman97dba042014-02-17 15:27:10 +00002786 if (Heading.empty()) {
2787 // If there's only one spelling, we can simply use that.
2788 if (Spellings.size() == 1)
2789 Heading = Spellings.begin()->name();
2790 else {
2791 std::set<std::string> Uniques;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002792 for (auto I = Spellings.begin(), E = Spellings.end();
2793 I != E && Uniques.size() <= 1; ++I) {
Aaron Ballman97dba042014-02-17 15:27:10 +00002794 std::string Spelling = NormalizeNameForSpellingComparison(I->name());
2795 Uniques.insert(Spelling);
2796 }
2797 // If the semantic map has only one spelling, that is sufficient for our
2798 // needs.
2799 if (Uniques.size() == 1)
2800 Heading = *Uniques.begin();
2801 }
2802 }
2803
2804 // If the heading is still empty, it is an error.
2805 if (Heading.empty())
Aaron Ballman1a3e5852014-02-17 16:18:32 +00002806 PrintFatalError(Doc.Attribute->getLoc(),
Aaron Ballman97dba042014-02-17 15:27:10 +00002807 "This attribute requires a heading to be specified");
2808
2809 // Gather a list of unique spellings; this is not the same as the semantic
2810 // spelling for the attribute. Variations in underscores and other non-
2811 // semantic characters are still acceptable.
2812 std::vector<std::string> Names;
2813
Aaron Ballman97dba042014-02-17 15:27:10 +00002814 unsigned SupportedSpellings = 0;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002815 for (const auto &I : Spellings) {
2816 SpellingKind Kind = StringSwitch<SpellingKind>(I.variety())
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002817 .Case("GNU", GNU)
2818 .Case("CXX11", CXX11)
2819 .Case("Declspec", Declspec)
2820 .Case("Keyword", Keyword)
2821 .Case("Pragma", Pragma);
Aaron Ballman97dba042014-02-17 15:27:10 +00002822
2823 // Mask in the supported spelling.
2824 SupportedSpellings |= Kind;
2825
2826 std::string Name;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002827 if (Kind == CXX11 && !I.nameSpace().empty())
2828 Name = I.nameSpace() + "::";
2829 Name += I.name();
Aaron Ballman97dba042014-02-17 15:27:10 +00002830
2831 // If this name is the same as the heading, do not add it.
2832 if (Name != Heading)
2833 Names.push_back(Name);
2834 }
2835
2836 // Print out the heading for the attribute. If there are alternate spellings,
2837 // then display those after the heading.
Aaron Ballmanea6668c2014-02-21 14:14:04 +00002838 if (!CustomHeading && !Names.empty()) {
Aaron Ballman97dba042014-02-17 15:27:10 +00002839 Heading += " (";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002840 for (auto I = Names.begin(), E = Names.end(); I != E; ++I) {
Aaron Ballman97dba042014-02-17 15:27:10 +00002841 if (I != Names.begin())
2842 Heading += ", ";
2843 Heading += *I;
2844 }
2845 Heading += ")";
2846 }
2847 OS << Heading << "\n" << std::string(Heading.length(), '-') << "\n";
2848
2849 if (!SupportedSpellings)
Aaron Ballman1a3e5852014-02-17 16:18:32 +00002850 PrintFatalError(Doc.Attribute->getLoc(),
Aaron Ballman97dba042014-02-17 15:27:10 +00002851 "Attribute has no supported spellings; cannot be "
2852 "documented");
2853
2854 // List what spelling syntaxes the attribute supports.
2855 OS << ".. csv-table:: Supported Syntaxes\n";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002856 OS << " :header: \"GNU\", \"C++11\", \"__declspec\", \"Keyword\",";
2857 OS << " \"Pragma\"\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00002858 OS << " \"";
2859 if (SupportedSpellings & GNU) OS << "X";
2860 OS << "\",\"";
2861 if (SupportedSpellings & CXX11) OS << "X";
2862 OS << "\",\"";
2863 if (SupportedSpellings & Declspec) OS << "X";
2864 OS << "\",\"";
2865 if (SupportedSpellings & Keyword) OS << "X";
Aaron Ballman120c79f2014-06-25 12:48:06 +00002866 OS << "\", \"";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002867 if (SupportedSpellings & Pragma) OS << "X";
2868 OS << "\"\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00002869
2870 // If the attribute is deprecated, print a message about it, and possibly
2871 // provide a replacement attribute.
Aaron Ballman1a3e5852014-02-17 16:18:32 +00002872 if (!Doc.Documentation->isValueUnset("Deprecated")) {
Aaron Ballman97dba042014-02-17 15:27:10 +00002873 OS << "This attribute has been deprecated, and may be removed in a future "
2874 << "version of Clang.";
Aaron Ballman1a3e5852014-02-17 16:18:32 +00002875 const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated");
Aaron Ballman97dba042014-02-17 15:27:10 +00002876 std::string Replacement = Deprecated.getValueAsString("Replacement");
2877 if (!Replacement.empty())
2878 OS << " This attribute has been superseded by ``"
2879 << Replacement << "``.";
2880 OS << "\n\n";
2881 }
2882
Aaron Ballman1a3e5852014-02-17 16:18:32 +00002883 std::string ContentStr = Doc.Documentation->getValueAsString("Content");
Aaron Ballman97dba042014-02-17 15:27:10 +00002884 // Trim leading and trailing newlines and spaces.
2885 StringRef Content(ContentStr);
2886 while (Content.startswith("\r") || Content.startswith("\n") ||
2887 Content.startswith(" ") || Content.startswith("\t"))
2888 Content = Content.substr(1);
2889 while (Content.endswith("\r") || Content.endswith("\n") ||
2890 Content.endswith(" ") || Content.endswith("\t"))
2891 Content = Content.substr(0, Content.size() - 1);
2892 OS << Content;
2893
2894 OS << "\n\n\n";
2895}
2896
2897void EmitClangAttrDocs(RecordKeeper &Records, raw_ostream &OS) {
2898 // Get the documentation introduction paragraph.
2899 const Record *Documentation = Records.getDef("GlobalDocumentation");
2900 if (!Documentation) {
2901 PrintFatalError("The Documentation top-level definition is missing, "
2902 "no documentation will be generated.");
2903 return;
2904 }
2905
Aaron Ballman4de1b582014-02-19 22:59:32 +00002906 OS << Documentation->getValueAsString("Intro") << "\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00002907
Aaron Ballman97dba042014-02-17 15:27:10 +00002908 // Gather the Documentation lists from each of the attributes, based on the
2909 // category provided.
2910 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002911 std::map<const Record *, std::vector<DocumentationData>> SplitDocs;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002912 for (const auto *A : Attrs) {
2913 const Record &Attr = *A;
Aaron Ballman97dba042014-02-17 15:27:10 +00002914 std::vector<Record *> Docs = Attr.getValueAsListOfDefs("Documentation");
Aaron Ballman2f22b942014-05-20 19:47:14 +00002915 for (const auto *D : Docs) {
2916 const Record &Doc = *D;
Aaron Ballman4de1b582014-02-19 22:59:32 +00002917 const Record *Category = Doc.getValueAsDef("Category");
Aaron Ballman97dba042014-02-17 15:27:10 +00002918 // If the category is "undocumented", then there cannot be any other
2919 // documentation categories (otherwise, the attribute would become
2920 // documented).
Aaron Ballman4de1b582014-02-19 22:59:32 +00002921 std::string Cat = Category->getValueAsString("Name");
2922 bool Undocumented = Cat == "Undocumented";
Aaron Ballman97dba042014-02-17 15:27:10 +00002923 if (Undocumented && Docs.size() > 1)
2924 PrintFatalError(Doc.getLoc(),
2925 "Attribute is \"Undocumented\", but has multiple "
2926 "documentation categories");
2927
2928 if (!Undocumented)
Aaron Ballman4de1b582014-02-19 22:59:32 +00002929 SplitDocs[Category].push_back(DocumentationData(Doc, Attr));
Aaron Ballman97dba042014-02-17 15:27:10 +00002930 }
2931 }
2932
2933 // Having split the attributes out based on what documentation goes where,
2934 // we can begin to generate sections of documentation.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002935 for (const auto &I : SplitDocs) {
2936 WriteCategoryHeader(I.first, OS);
Aaron Ballman97dba042014-02-17 15:27:10 +00002937
2938 // Walk over each of the attributes in the category and write out their
2939 // documentation.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002940 for (const auto &Doc : I.second)
2941 WriteDocumentation(Doc, OS);
Aaron Ballman97dba042014-02-17 15:27:10 +00002942 }
2943}
2944
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002945} // end namespace clang