blob: 78a2eb118bc1e636e18b79e156b3e03de5b5b2de [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
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000014#include "llvm/ADT/ArrayRef.h"
George Burgess IV1881a572016-12-01 00:13:18 +000015#include "llvm/ADT/DenseSet.h"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000016#include "llvm/ADT/iterator_range.h"
Alexis Hunta0e54d42012-06-18 16:13:52 +000017#include "llvm/ADT/SmallString.h"
Aaron Ballman28afa182014-11-17 18:17:19 +000018#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/StringExtras.h"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000020#include "llvm/ADT/StringRef.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000021#include "llvm/ADT/StringSwitch.h"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000022#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/raw_ostream.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000024#include "llvm/TableGen/Error.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000025#include "llvm/TableGen/Record.h"
Douglas Gregor377f99b2012-05-02 17:33:51 +000026#include "llvm/TableGen/StringMatcher.h"
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000027#include "llvm/TableGen/TableGenBackend.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000028#include <algorithm>
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000029#include <cassert>
Peter Collingbournebee583f2011-10-06 13:03:08 +000030#include <cctype>
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000031#include <cstddef>
32#include <cstdint>
33#include <map>
Aaron Ballman8f1439b2014-03-05 16:49:55 +000034#include <memory>
Aaron Ballman80469032013-11-29 14:57:58 +000035#include <set>
Chandler Carruth5553d0d2014-01-07 11:51:46 +000036#include <sstream>
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000037#include <string>
38#include <utility>
39#include <vector>
Peter Collingbournebee583f2011-10-06 13:03:08 +000040
41using namespace llvm;
42
Benjamin Kramerd910d162015-03-10 18:24:01 +000043namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000044
Aaron Ballmanc669cc02014-01-27 22:10:04 +000045class FlattenedSpelling {
46 std::string V, N, NS;
47 bool K;
48
49public:
50 FlattenedSpelling(const std::string &Variety, const std::string &Name,
51 const std::string &Namespace, bool KnownToGCC) :
52 V(Variety), N(Name), NS(Namespace), K(KnownToGCC) {}
53 explicit FlattenedSpelling(const Record &Spelling) :
54 V(Spelling.getValueAsString("Variety")),
55 N(Spelling.getValueAsString("Name")) {
56
57 assert(V != "GCC" && "Given a GCC spelling, which means this hasn't been"
58 "flattened!");
Tyler Nowickie8b07ed2014-06-13 17:57:25 +000059 if (V == "CXX11" || V == "Pragma")
Aaron Ballmanc669cc02014-01-27 22:10:04 +000060 NS = Spelling.getValueAsString("Namespace");
61 bool Unset;
62 K = Spelling.getValueAsBitOrUnset("KnownToGCC", Unset);
63 }
64
65 const std::string &variety() const { return V; }
66 const std::string &name() const { return N; }
67 const std::string &nameSpace() const { return NS; }
68 bool knownToGCC() const { return K; }
69};
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000070
Hans Wennborgdcfba332015-10-06 23:40:43 +000071} // end anonymous namespace
Aaron Ballmanc669cc02014-01-27 22:10:04 +000072
Benjamin Kramerd910d162015-03-10 18:24:01 +000073static std::vector<FlattenedSpelling>
74GetFlattenedSpellings(const Record &Attr) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +000075 std::vector<Record *> Spellings = Attr.getValueAsListOfDefs("Spellings");
76 std::vector<FlattenedSpelling> Ret;
77
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +000078 for (const auto &Spelling : Spellings) {
79 if (Spelling->getValueAsString("Variety") == "GCC") {
Aaron Ballmanc669cc02014-01-27 22:10:04 +000080 // Gin up two new spelling objects to add into the list.
Benjamin Kramer3204b152015-05-29 19:42:19 +000081 Ret.emplace_back("GNU", Spelling->getValueAsString("Name"), "", true);
82 Ret.emplace_back("CXX11", Spelling->getValueAsString("Name"), "gnu",
83 true);
Aaron Ballmanc669cc02014-01-27 22:10:04 +000084 } else
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +000085 Ret.push_back(FlattenedSpelling(*Spelling));
Aaron Ballmanc669cc02014-01-27 22:10:04 +000086 }
87
88 return Ret;
89}
90
Peter Collingbournebee583f2011-10-06 13:03:08 +000091static std::string ReadPCHRecord(StringRef type) {
92 return StringSwitch<std::string>(type)
93 .EndsWith("Decl *", "GetLocalDeclAs<"
94 + std::string(type, 0, type.size()-1) + ">(F, Record[Idx++])")
Richard Smithb87c4652013-10-31 21:23:20 +000095 .Case("TypeSourceInfo *", "GetTypeSourceInfo(F, Record, Idx)")
Argyrios Kyrtzidisa660ae42012-11-15 01:31:39 +000096 .Case("Expr *", "ReadExpr(F)")
Peter Collingbournebee583f2011-10-06 13:03:08 +000097 .Case("IdentifierInfo *", "GetIdentifierInfo(F, Record, Idx)")
Benjamin Kramer1b582012016-02-13 18:11:49 +000098 .Case("StringRef", "ReadString(Record, Idx)")
Peter Collingbournebee583f2011-10-06 13:03:08 +000099 .Default("Record[Idx++]");
100}
101
Richard Smith19978562016-05-18 00:16:51 +0000102// Get a type that is suitable for storing an object of the specified type.
103static StringRef getStorageType(StringRef type) {
104 return StringSwitch<StringRef>(type)
105 .Case("StringRef", "std::string")
106 .Default(type);
107}
108
Peter Collingbournebee583f2011-10-06 13:03:08 +0000109// Assumes that the way to get the value is SA->getname()
110static std::string WritePCHRecord(StringRef type, StringRef name) {
Richard Smith290d8012016-04-06 17:06:00 +0000111 return "Record." + StringSwitch<std::string>(type)
112 .EndsWith("Decl *", "AddDeclRef(" + std::string(name) + ");\n")
113 .Case("TypeSourceInfo *", "AddTypeSourceInfo(" + std::string(name) + ");\n")
Peter Collingbournebee583f2011-10-06 13:03:08 +0000114 .Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
Richard Smith290d8012016-04-06 17:06:00 +0000115 .Case("IdentifierInfo *", "AddIdentifierRef(" + std::string(name) + ");\n")
116 .Case("StringRef", "AddString(" + std::string(name) + ");\n")
117 .Default("push_back(" + std::string(name) + ");\n");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000118}
119
Michael Han4a045172012-03-07 00:12:16 +0000120// Normalize attribute name by removing leading and trailing
121// underscores. For example, __foo, foo__, __foo__ would
122// become foo.
123static StringRef NormalizeAttrName(StringRef AttrName) {
George Burgess IV1881a572016-12-01 00:13:18 +0000124 AttrName.consume_front("__");
125 AttrName.consume_back("__");
Michael Han4a045172012-03-07 00:12:16 +0000126 return AttrName;
127}
128
Aaron Ballman36a53502014-01-16 13:03:14 +0000129// Normalize the name by removing any and all leading and trailing underscores.
130// This is different from NormalizeAttrName in that it also handles names like
131// _pascal and __pascal.
132static StringRef NormalizeNameForSpellingComparison(StringRef Name) {
Benjamin Kramer5c404072015-04-10 21:37:21 +0000133 return Name.trim("_");
Aaron Ballman36a53502014-01-16 13:03:14 +0000134}
135
Michael Han4a045172012-03-07 00:12:16 +0000136// Normalize attribute spelling only if the spelling has both leading
137// and trailing underscores. For example, __ms_struct__ will be
138// normalized to "ms_struct"; __cdecl will remain intact.
139static StringRef NormalizeAttrSpelling(StringRef AttrSpelling) {
140 if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) {
141 AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4);
142 }
143
144 return AttrSpelling;
145}
146
Aaron Ballman2f22b942014-05-20 19:47:14 +0000147typedef std::vector<std::pair<std::string, const Record *>> ParsedAttrMap;
Aaron Ballman64e69862013-12-15 13:05:48 +0000148
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000149static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,
Craig Topper8ae12032014-05-07 06:21:57 +0000150 ParsedAttrMap *Dupes = nullptr) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000151 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Aaron Ballman64e69862013-12-15 13:05:48 +0000152 std::set<std::string> Seen;
153 ParsedAttrMap R;
Aaron Ballman2f22b942014-05-20 19:47:14 +0000154 for (const auto *Attr : Attrs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000155 if (Attr->getValueAsBit("SemaHandler")) {
Aaron Ballman64e69862013-12-15 13:05:48 +0000156 std::string AN;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000157 if (Attr->isSubClassOf("TargetSpecificAttr") &&
158 !Attr->isValueUnset("ParseKind")) {
159 AN = Attr->getValueAsString("ParseKind");
Aaron Ballman64e69862013-12-15 13:05:48 +0000160
161 // If this attribute has already been handled, it does not need to be
162 // handled again.
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000163 if (Seen.find(AN) != Seen.end()) {
164 if (Dupes)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000165 Dupes->push_back(std::make_pair(AN, Attr));
Aaron Ballman64e69862013-12-15 13:05:48 +0000166 continue;
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000167 }
Aaron Ballman64e69862013-12-15 13:05:48 +0000168 Seen.insert(AN);
169 } else
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000170 AN = NormalizeAttrName(Attr->getName()).str();
Aaron Ballman64e69862013-12-15 13:05:48 +0000171
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000172 R.push_back(std::make_pair(AN, Attr));
Aaron Ballman64e69862013-12-15 13:05:48 +0000173 }
174 }
175 return R;
176}
177
Peter Collingbournebee583f2011-10-06 13:03:08 +0000178namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000179
Peter Collingbournebee583f2011-10-06 13:03:08 +0000180 class Argument {
181 std::string lowerName, upperName;
182 StringRef attrName;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000183 bool isOpt;
John McCalla62c1a92015-10-28 00:17:34 +0000184 bool Fake;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000185
186 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000187 Argument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000188 : lowerName(Arg.getValueAsString("Name")), upperName(lowerName),
John McCalla62c1a92015-10-28 00:17:34 +0000189 attrName(Attr), isOpt(false), Fake(false) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000190 if (!lowerName.empty()) {
191 lowerName[0] = std::tolower(lowerName[0]);
192 upperName[0] = std::toupper(upperName[0]);
193 }
Reid Klecknerebeb0ca2016-05-31 17:42:56 +0000194 // Work around MinGW's macro definition of 'interface' to 'struct'. We
195 // have an attribute argument called 'Interface', so only the lower case
196 // name conflicts with the macro definition.
197 if (lowerName == "interface")
198 lowerName = "interface_";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000199 }
Hans Wennborgdcfba332015-10-06 23:40:43 +0000200 virtual ~Argument() = default;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000201
202 StringRef getLowerName() const { return lowerName; }
203 StringRef getUpperName() const { return upperName; }
204 StringRef getAttrName() const { return attrName; }
205
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000206 bool isOptional() const { return isOpt; }
207 void setOptional(bool set) { isOpt = set; }
208
John McCalla62c1a92015-10-28 00:17:34 +0000209 bool isFake() const { return Fake; }
210 void setFake(bool fake) { Fake = fake; }
211
Peter Collingbournebee583f2011-10-06 13:03:08 +0000212 // These functions print the argument contents formatted in different ways.
213 virtual void writeAccessors(raw_ostream &OS) const = 0;
214 virtual void writeAccessorDefinitions(raw_ostream &OS) const {}
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +0000215 virtual void writeASTVisitorTraversal(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000216 virtual void writeCloneArgs(raw_ostream &OS) const = 0;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000217 virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0;
Daniel Dunbardc51baa2012-02-10 06:00:29 +0000218 virtual void writeTemplateInstantiation(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000219 virtual void writeCtorBody(raw_ostream &OS) const {}
220 virtual void writeCtorInitializers(raw_ostream &OS) const = 0;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000221 virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000222 virtual void writeCtorParameters(raw_ostream &OS) const = 0;
223 virtual void writeDeclarations(raw_ostream &OS) const = 0;
224 virtual void writePCHReadArgs(raw_ostream &OS) const = 0;
225 virtual void writePCHReadDecls(raw_ostream &OS) const = 0;
226 virtual void writePCHWrite(raw_ostream &OS) const = 0;
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000227 virtual void writeValue(raw_ostream &OS) const = 0;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000228 virtual void writeDump(raw_ostream &OS) const = 0;
229 virtual void writeDumpChildren(raw_ostream &OS) const {}
Richard Trieude5cc7d2013-01-31 01:44:26 +0000230 virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000231
232 virtual bool isEnumArg() const { return false; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000233 virtual bool isVariadicEnumArg() const { return false; }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000234 virtual bool isVariadic() const { return false; }
Aaron Ballman36a53502014-01-16 13:03:14 +0000235
236 virtual void writeImplicitCtorArgs(raw_ostream &OS) const {
237 OS << getUpperName();
238 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000239 };
240
241 class SimpleArgument : public Argument {
242 std::string type;
243
244 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000245 SimpleArgument(const Record &Arg, StringRef Attr, std::string T)
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000246 : Argument(Arg, Attr), type(std::move(T)) {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000247
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000248 std::string getType() const { return type; }
249
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000250 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000251 OS << " " << type << " get" << getUpperName() << "() const {\n";
252 OS << " return " << getLowerName() << ";\n";
253 OS << " }";
254 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000255
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000256 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000257 OS << getLowerName();
258 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000259
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000260 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000261 OS << "A->get" << getUpperName() << "()";
262 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000263
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000264 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000265 OS << getLowerName() << "(" << getUpperName() << ")";
266 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000267
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000268 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000269 OS << getLowerName() << "()";
270 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000271
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000272 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000273 OS << type << " " << getUpperName();
274 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000275
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000276 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000277 OS << type << " " << getLowerName() << ";";
278 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000279
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000280 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000281 std::string read = ReadPCHRecord(type);
282 OS << " " << type << " " << getLowerName() << " = " << read << ";\n";
283 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000284
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000285 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000286 OS << getLowerName();
287 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000288
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000289 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000290 OS << " " << WritePCHRecord(type, "SA->get" +
291 std::string(getUpperName()) + "()");
292 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000293
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000294 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000295 if (type == "FunctionDecl *") {
Richard Smithb87c4652013-10-31 21:23:20 +0000296 OS << "\" << get" << getUpperName()
297 << "()->getNameInfo().getAsString() << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000298 } else if (type == "IdentifierInfo *") {
Akira Hatanaka3d173132016-09-10 03:29:43 +0000299 OS << "\";\n";
300 if (isOptional())
301 OS << " if (get" << getUpperName() << "()) ";
302 else
303 OS << " ";
304 OS << "OS << get" << getUpperName() << "()->getName();\n";
305 OS << " OS << \"";
Richard Smithb87c4652013-10-31 21:23:20 +0000306 } else if (type == "TypeSourceInfo *") {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000307 OS << "\" << get" << getUpperName() << "().getAsString() << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000308 } else {
309 OS << "\" << get" << getUpperName() << "() << \"";
310 }
311 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000312
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000313 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000314 if (type == "FunctionDecl *") {
315 OS << " OS << \" \";\n";
316 OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n";
317 } else if (type == "IdentifierInfo *") {
Aaron Ballman415c4142015-11-30 15:25:34 +0000318 if (isOptional())
319 OS << " if (SA->get" << getUpperName() << "())\n ";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000320 OS << " OS << \" \" << SA->get" << getUpperName()
321 << "()->getName();\n";
Richard Smithb87c4652013-10-31 21:23:20 +0000322 } else if (type == "TypeSourceInfo *") {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000323 OS << " OS << \" \" << SA->get" << getUpperName()
324 << "().getAsString();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000325 } else if (type == "bool") {
326 OS << " if (SA->get" << getUpperName() << "()) OS << \" "
327 << getUpperName() << "\";\n";
328 } else if (type == "int" || type == "unsigned") {
329 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
330 } else {
331 llvm_unreachable("Unknown SimpleArgument type!");
332 }
333 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000334 };
335
Aaron Ballman18a78382013-11-21 00:28:23 +0000336 class DefaultSimpleArgument : public SimpleArgument {
337 int64_t Default;
338
339 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000340 DefaultSimpleArgument(const Record &Arg, StringRef Attr,
Aaron Ballman18a78382013-11-21 00:28:23 +0000341 std::string T, int64_t Default)
342 : SimpleArgument(Arg, Attr, T), Default(Default) {}
343
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000344 void writeAccessors(raw_ostream &OS) const override {
Aaron Ballman18a78382013-11-21 00:28:23 +0000345 SimpleArgument::writeAccessors(OS);
346
347 OS << "\n\n static const " << getType() << " Default" << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000348 << " = ";
349 if (getType() == "bool")
350 OS << (Default != 0 ? "true" : "false");
351 else
352 OS << Default;
353 OS << ";";
Aaron Ballman18a78382013-11-21 00:28:23 +0000354 }
355 };
356
Peter Collingbournebee583f2011-10-06 13:03:08 +0000357 class StringArgument : public Argument {
358 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000359 StringArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000360 : Argument(Arg, Attr)
361 {}
362
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000363 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000364 OS << " llvm::StringRef get" << getUpperName() << "() const {\n";
365 OS << " return llvm::StringRef(" << getLowerName() << ", "
366 << getLowerName() << "Length);\n";
367 OS << " }\n";
368 OS << " unsigned get" << getUpperName() << "Length() const {\n";
369 OS << " return " << getLowerName() << "Length;\n";
370 OS << " }\n";
371 OS << " void set" << getUpperName()
372 << "(ASTContext &C, llvm::StringRef S) {\n";
373 OS << " " << getLowerName() << "Length = S.size();\n";
374 OS << " this->" << getLowerName() << " = new (C, 1) char ["
375 << getLowerName() << "Length];\n";
Chandler Carruth38a45cc2015-08-04 03:53:01 +0000376 OS << " if (!S.empty())\n";
377 OS << " std::memcpy(this->" << getLowerName() << ", S.data(), "
Peter Collingbournebee583f2011-10-06 13:03:08 +0000378 << getLowerName() << "Length);\n";
379 OS << " }";
380 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000381
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000382 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000383 OS << "get" << getUpperName() << "()";
384 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000385
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000386 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000387 OS << "A->get" << getUpperName() << "()";
388 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000389
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000390 void writeCtorBody(raw_ostream &OS) const override {
Chandler Carruth38a45cc2015-08-04 03:53:01 +0000391 OS << " if (!" << getUpperName() << ".empty())\n";
392 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000393 << ".data(), " << getLowerName() << "Length);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000394 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000395
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000396 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000397 OS << getLowerName() << "Length(" << getUpperName() << ".size()),"
398 << getLowerName() << "(new (Ctx, 1) char[" << getLowerName()
399 << "Length])";
400 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000401
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000402 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Hans Wennborg59dbe862015-09-29 20:56:43 +0000403 OS << getLowerName() << "Length(0)," << getLowerName() << "(nullptr)";
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000404 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000405
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000406 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000407 OS << "llvm::StringRef " << getUpperName();
408 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000409
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000410 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000411 OS << "unsigned " << getLowerName() << "Length;\n";
412 OS << "char *" << getLowerName() << ";";
413 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000414
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000415 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000416 OS << " std::string " << getLowerName()
417 << "= ReadString(Record, Idx);\n";
418 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000419
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000420 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000421 OS << getLowerName();
422 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000423
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000424 void writePCHWrite(raw_ostream &OS) const override {
Richard Smith290d8012016-04-06 17:06:00 +0000425 OS << " Record.AddString(SA->get" << getUpperName() << "());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000426 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000427
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000428 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000429 OS << "\\\"\" << get" << getUpperName() << "() << \"\\\"";
430 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000431
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000432 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000433 OS << " OS << \" \\\"\" << SA->get" << getUpperName()
434 << "() << \"\\\"\";\n";
435 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000436 };
437
438 class AlignedArgument : public Argument {
439 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000440 AlignedArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000441 : Argument(Arg, Attr)
442 {}
443
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000444 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000445 OS << " bool is" << getUpperName() << "Dependent() const;\n";
446
447 OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n";
448
449 OS << " bool is" << getUpperName() << "Expr() const {\n";
450 OS << " return is" << getLowerName() << "Expr;\n";
451 OS << " }\n";
452
453 OS << " Expr *get" << getUpperName() << "Expr() const {\n";
454 OS << " assert(is" << getLowerName() << "Expr);\n";
455 OS << " return " << getLowerName() << "Expr;\n";
456 OS << " }\n";
457
458 OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n";
459 OS << " assert(!is" << getLowerName() << "Expr);\n";
460 OS << " return " << getLowerName() << "Type;\n";
461 OS << " }";
462 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000463
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000464 void writeAccessorDefinitions(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000465 OS << "bool " << getAttrName() << "Attr::is" << getUpperName()
466 << "Dependent() const {\n";
467 OS << " if (is" << getLowerName() << "Expr)\n";
468 OS << " return " << getLowerName() << "Expr && (" << getLowerName()
469 << "Expr->isValueDependent() || " << getLowerName()
470 << "Expr->isTypeDependent());\n";
471 OS << " else\n";
472 OS << " return " << getLowerName()
473 << "Type->getType()->isDependentType();\n";
474 OS << "}\n";
475
476 // FIXME: Do not do the calculation here
477 // FIXME: Handle types correctly
478 // A null pointer means maximum alignment
Peter Collingbournebee583f2011-10-06 13:03:08 +0000479 OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
480 << "(ASTContext &Ctx) const {\n";
481 OS << " assert(!is" << getUpperName() << "Dependent());\n";
482 OS << " if (is" << getLowerName() << "Expr)\n";
Ulrich Weigandca3cb7f2015-04-21 17:29:35 +0000483 OS << " return " << getLowerName() << "Expr ? " << getLowerName()
484 << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue()"
485 << " * Ctx.getCharWidth() : "
486 << "Ctx.getTargetDefaultAlignForAttributeAligned();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000487 OS << " else\n";
488 OS << " return 0; // FIXME\n";
489 OS << "}\n";
490 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000491
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000492 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000493 OS << "is" << getLowerName() << "Expr, is" << getLowerName()
494 << "Expr ? static_cast<void*>(" << getLowerName()
495 << "Expr) : " << getLowerName()
496 << "Type";
497 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000498
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000499 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000500 // FIXME: move the definition in Sema::InstantiateAttrs to here.
501 // In the meantime, aligned attributes are cloned.
502 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000503
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000504 void writeCtorBody(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000505 OS << " if (is" << getLowerName() << "Expr)\n";
506 OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>("
507 << getUpperName() << ");\n";
508 OS << " else\n";
509 OS << " " << getLowerName()
510 << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000511 << ");\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000512 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000513
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000514 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000515 OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)";
516 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000517
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000518 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000519 OS << "is" << getLowerName() << "Expr(false)";
520 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000521
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000522 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000523 OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName();
524 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000525
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000526 void writeImplicitCtorArgs(raw_ostream &OS) const override {
Aaron Ballman36a53502014-01-16 13:03:14 +0000527 OS << "Is" << getUpperName() << "Expr, " << getUpperName();
528 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000529
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000530 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000531 OS << "bool is" << getLowerName() << "Expr;\n";
532 OS << "union {\n";
533 OS << "Expr *" << getLowerName() << "Expr;\n";
534 OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
535 OS << "};";
536 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000537
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000538 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000539 OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr";
540 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000541
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000542 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000543 OS << " bool is" << getLowerName() << "Expr = Record[Idx++];\n";
544 OS << " void *" << getLowerName() << "Ptr;\n";
545 OS << " if (is" << getLowerName() << "Expr)\n";
546 OS << " " << getLowerName() << "Ptr = ReadExpr(F);\n";
547 OS << " else\n";
548 OS << " " << getLowerName()
549 << "Ptr = GetTypeSourceInfo(F, Record, Idx);\n";
550 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000551
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000552 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000553 OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n";
554 OS << " if (SA->is" << getUpperName() << "Expr())\n";
Richard Smith290d8012016-04-06 17:06:00 +0000555 OS << " Record.AddStmt(SA->get" << getUpperName() << "Expr());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000556 OS << " else\n";
Richard Smith290d8012016-04-06 17:06:00 +0000557 OS << " Record.AddTypeSourceInfo(SA->get" << getUpperName()
558 << "Type());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000559 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000560
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000561 void writeValue(raw_ostream &OS) const override {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000562 OS << "\";\n";
Aaron Ballmanc960f562014-08-01 13:49:00 +0000563 // The aligned attribute argument expression is optional.
564 OS << " if (is" << getLowerName() << "Expr && "
565 << getLowerName() << "Expr)\n";
Hans Wennborg59dbe862015-09-29 20:56:43 +0000566 OS << " " << getLowerName() << "Expr->printPretty(OS, nullptr, Policy);\n";
Richard Trieuddd01ce2014-06-09 22:53:25 +0000567 OS << " OS << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000568 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000569
570 void writeDump(raw_ostream &OS) const override {}
571
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000572 void writeDumpChildren(raw_ostream &OS) const override {
Richard Smithf7514452014-10-30 21:02:37 +0000573 OS << " if (SA->is" << getUpperName() << "Expr())\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000574 OS << " dumpStmt(SA->get" << getUpperName() << "Expr());\n";
Richard Smithf7514452014-10-30 21:02:37 +0000575 OS << " else\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000576 OS << " dumpType(SA->get" << getUpperName()
577 << "Type()->getType());\n";
578 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000579
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000580 void writeHasChildren(raw_ostream &OS) const override {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000581 OS << "SA->is" << getUpperName() << "Expr()";
582 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000583 };
584
585 class VariadicArgument : public Argument {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000586 std::string Type, ArgName, ArgSizeName, RangeName;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000587
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000588 protected:
589 // Assumed to receive a parameter: raw_ostream OS.
590 virtual void writeValueImpl(raw_ostream &OS) const {
591 OS << " OS << Val;\n";
592 }
593
Peter Collingbournebee583f2011-10-06 13:03:08 +0000594 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000595 VariadicArgument(const Record &Arg, StringRef Attr, std::string T)
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000596 : Argument(Arg, Attr), Type(std::move(T)),
597 ArgName(getLowerName().str() + "_"), ArgSizeName(ArgName + "Size"),
598 RangeName(getLowerName()) {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000599
Benjamin Kramer1b582012016-02-13 18:11:49 +0000600 const std::string &getType() const { return Type; }
601 const std::string &getArgName() const { return ArgName; }
602 const std::string &getArgSizeName() const { return ArgSizeName; }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000603 bool isVariadic() const override { return true; }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000604
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000605 void writeAccessors(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000606 std::string IteratorType = getLowerName().str() + "_iterator";
607 std::string BeginFn = getLowerName().str() + "_begin()";
608 std::string EndFn = getLowerName().str() + "_end()";
609
610 OS << " typedef " << Type << "* " << IteratorType << ";\n";
611 OS << " " << IteratorType << " " << BeginFn << " const {"
612 << " return " << ArgName << "; }\n";
613 OS << " " << IteratorType << " " << EndFn << " const {"
614 << " return " << ArgName << " + " << ArgSizeName << "; }\n";
615 OS << " unsigned " << getLowerName() << "_size() const {"
616 << " return " << ArgSizeName << "; }\n";
617 OS << " llvm::iterator_range<" << IteratorType << "> " << RangeName
618 << "() const { return llvm::make_range(" << BeginFn << ", " << EndFn
619 << "); }\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000620 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000621
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000622 void writeCloneArgs(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000623 OS << ArgName << ", " << ArgSizeName;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000624 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000625
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000626 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000627 // This isn't elegant, but we have to go through public methods...
628 OS << "A->" << getLowerName() << "_begin(), "
629 << "A->" << getLowerName() << "_size()";
630 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000631
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000632 void writeCtorBody(raw_ostream &OS) const override {
Aaron Ballmand6459e52014-05-01 15:21:03 +0000633 OS << " std::copy(" << getUpperName() << ", " << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000634 << " + " << ArgSizeName << ", " << ArgName << ");\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000635 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000636
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000637 void writeCtorInitializers(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000638 OS << ArgSizeName << "(" << getUpperName() << "Size), "
639 << ArgName << "(new (Ctx, 16) " << getType() << "["
640 << ArgSizeName << "])";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000641 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000642
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000643 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000644 OS << ArgSizeName << "(0), " << ArgName << "(nullptr)";
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000645 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000646
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000647 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000648 OS << getType() << " *" << getUpperName() << ", unsigned "
649 << getUpperName() << "Size";
650 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000651
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000652 void writeImplicitCtorArgs(raw_ostream &OS) const override {
Aaron Ballman36a53502014-01-16 13:03:14 +0000653 OS << getUpperName() << ", " << getUpperName() << "Size";
654 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000655
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000656 void writeDeclarations(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000657 OS << " unsigned " << ArgSizeName << ";\n";
658 OS << " " << getType() << " *" << ArgName << ";";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000659 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000660
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000661 void writePCHReadDecls(raw_ostream &OS) const override {
Richard Smith19978562016-05-18 00:16:51 +0000662 OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
663 OS << " SmallVector<" << getType() << ", 4> "
664 << getLowerName() << ";\n";
665 OS << " " << getLowerName() << ".reserve(" << getLowerName()
Peter Collingbournebee583f2011-10-06 13:03:08 +0000666 << "Size);\n";
Richard Smith19978562016-05-18 00:16:51 +0000667
668 // If we can't store the values in the current type (if it's something
669 // like StringRef), store them in a different type and convert the
670 // container afterwards.
671 std::string StorageType = getStorageType(getType());
672 std::string StorageName = getLowerName();
673 if (StorageType != getType()) {
674 StorageName += "Storage";
675 OS << " SmallVector<" << StorageType << ", 4> "
676 << StorageName << ";\n";
677 OS << " " << StorageName << ".reserve(" << getLowerName()
678 << "Size);\n";
679 }
680
681 OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n";
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000682 std::string read = ReadPCHRecord(Type);
Richard Smith19978562016-05-18 00:16:51 +0000683 OS << " " << StorageName << ".push_back(" << read << ");\n";
684
685 if (StorageType != getType()) {
686 OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n";
687 OS << " " << getLowerName() << ".push_back("
688 << StorageName << "[i]);\n";
689 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000690 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000691
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000692 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000693 OS << getLowerName() << ".data(), " << getLowerName() << "Size";
694 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000695
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000696 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000697 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000698 OS << " for (auto &Val : SA->" << RangeName << "())\n";
699 OS << " " << WritePCHRecord(Type, "Val");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000700 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000701
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000702 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000703 OS << "\";\n";
704 OS << " bool isFirst = true;\n"
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000705 << " for (const auto &Val : " << RangeName << "()) {\n"
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000706 << " if (isFirst) isFirst = false;\n"
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000707 << " else OS << \", \";\n";
708 writeValueImpl(OS);
709 OS << " }\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000710 OS << " OS << \"";
711 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000712
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000713 void writeDump(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000714 OS << " for (const auto &Val : SA->" << RangeName << "())\n";
715 OS << " OS << \" \" << Val;\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000716 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000717 };
718
Reid Klecknerf526b9482014-02-12 18:22:18 +0000719 // Unique the enums, but maintain the original declaration ordering.
Reid Klecknerf06b2662014-02-12 19:26:24 +0000720 std::vector<std::string>
721 uniqueEnumsInOrder(const std::vector<std::string> &enums) {
Reid Klecknerf526b9482014-02-12 18:22:18 +0000722 std::vector<std::string> uniques;
George Burgess IV1881a572016-12-01 00:13:18 +0000723 SmallDenseSet<StringRef, 8> unique_set;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000724 for (const auto &i : enums) {
George Burgess IV1881a572016-12-01 00:13:18 +0000725 if (unique_set.insert(i).second)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000726 uniques.push_back(i);
Reid Klecknerf526b9482014-02-12 18:22:18 +0000727 }
728 return uniques;
729 }
730
Peter Collingbournebee583f2011-10-06 13:03:08 +0000731 class EnumArgument : public Argument {
732 std::string type;
Aaron Ballman0e468c02014-01-05 21:08:29 +0000733 std::vector<std::string> values, enums, uniques;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000734 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000735 EnumArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000736 : Argument(Arg, Attr), type(Arg.getValueAsString("Type")),
Aaron Ballman0e468c02014-01-05 21:08:29 +0000737 values(Arg.getValueAsListOfStrings("Values")),
738 enums(Arg.getValueAsListOfStrings("Enums")),
Reid Klecknerf526b9482014-02-12 18:22:18 +0000739 uniques(uniqueEnumsInOrder(enums))
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000740 {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000741 // FIXME: Emit a proper error
742 assert(!uniques.empty());
743 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000744
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000745 bool isEnumArg() const override { return true; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000746
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000747 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000748 OS << " " << type << " get" << getUpperName() << "() const {\n";
749 OS << " return " << getLowerName() << ";\n";
750 OS << " }";
751 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000752
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000753 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000754 OS << getLowerName();
755 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000756
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000757 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000758 OS << "A->get" << getUpperName() << "()";
759 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000760 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000761 OS << getLowerName() << "(" << getUpperName() << ")";
762 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000763 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000764 OS << getLowerName() << "(" << type << "(0))";
765 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000766 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000767 OS << type << " " << getUpperName();
768 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000769 void writeDeclarations(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +0000770 auto i = uniques.cbegin(), e = uniques.cend();
Peter Collingbournebee583f2011-10-06 13:03:08 +0000771 // The last one needs to not have a comma.
772 --e;
773
774 OS << "public:\n";
775 OS << " enum " << type << " {\n";
776 for (; i != e; ++i)
777 OS << " " << *i << ",\n";
778 OS << " " << *e << "\n";
779 OS << " };\n";
780 OS << "private:\n";
781 OS << " " << type << " " << getLowerName() << ";";
782 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000783
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000784 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000785 OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName()
786 << "(static_cast<" << getAttrName() << "Attr::" << type
787 << ">(Record[Idx++]));\n";
788 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000789
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000790 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000791 OS << getLowerName();
792 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000793
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000794 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000795 OS << "Record.push_back(SA->get" << getUpperName() << "());\n";
796 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000797
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000798 void writeValue(raw_ostream &OS) const override {
Aaron Ballman36d79102014-09-15 16:16:14 +0000799 // FIXME: this isn't 100% correct -- some enum arguments require printing
800 // as a string literal, while others require printing as an identifier.
801 // Tablegen currently does not distinguish between the two forms.
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000802 OS << "\\\"\" << " << getAttrName() << "Attr::Convert" << type << "ToStr(get"
803 << getUpperName() << "()) << \"\\\"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000804 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000805
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000806 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000807 OS << " switch(SA->get" << getUpperName() << "()) {\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000808 for (const auto &I : uniques) {
809 OS << " case " << getAttrName() << "Attr::" << I << ":\n";
810 OS << " OS << \" " << I << "\";\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000811 OS << " break;\n";
812 }
813 OS << " }\n";
814 }
Aaron Ballman682ee422013-09-11 19:47:58 +0000815
816 void writeConversion(raw_ostream &OS) const {
817 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
818 OS << type << " &Out) {\n";
819 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000820 OS << type << ">>(Val)\n";
Aaron Ballman682ee422013-09-11 19:47:58 +0000821 for (size_t I = 0; I < enums.size(); ++I) {
822 OS << " .Case(\"" << values[I] << "\", ";
823 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
824 }
825 OS << " .Default(Optional<" << type << ">());\n";
826 OS << " if (R) {\n";
827 OS << " Out = *R;\n return true;\n }\n";
828 OS << " return false;\n";
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000829 OS << " }\n\n";
830
831 // Mapping from enumeration values back to enumeration strings isn't
832 // trivial because some enumeration values have multiple named
833 // enumerators, such as type_visibility(internal) and
834 // type_visibility(hidden) both mapping to TypeVisibilityAttr::Hidden.
835 OS << " static const char *Convert" << type << "ToStr("
836 << type << " Val) {\n"
837 << " switch(Val) {\n";
George Burgess IV1881a572016-12-01 00:13:18 +0000838 SmallDenseSet<StringRef, 8> Uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000839 for (size_t I = 0; I < enums.size(); ++I) {
840 if (Uniques.insert(enums[I]).second)
841 OS << " case " << getAttrName() << "Attr::" << enums[I]
842 << ": return \"" << values[I] << "\";\n";
843 }
844 OS << " }\n"
845 << " llvm_unreachable(\"No enumerator with that value\");\n"
846 << " }\n";
Aaron Ballman682ee422013-09-11 19:47:58 +0000847 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000848 };
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000849
850 class VariadicEnumArgument: public VariadicArgument {
851 std::string type, QualifiedTypeName;
Aaron Ballman0e468c02014-01-05 21:08:29 +0000852 std::vector<std::string> values, enums, uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000853
854 protected:
855 void writeValueImpl(raw_ostream &OS) const override {
Aaron Ballman36d79102014-09-15 16:16:14 +0000856 // FIXME: this isn't 100% correct -- some enum arguments require printing
857 // as a string literal, while others require printing as an identifier.
858 // Tablegen currently does not distinguish between the two forms.
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000859 OS << " OS << \"\\\"\" << " << getAttrName() << "Attr::Convert" << type
860 << "ToStr(Val)" << "<< \"\\\"\";\n";
861 }
862
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000863 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000864 VariadicEnumArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000865 : VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")),
866 type(Arg.getValueAsString("Type")),
Aaron Ballman0e468c02014-01-05 21:08:29 +0000867 values(Arg.getValueAsListOfStrings("Values")),
868 enums(Arg.getValueAsListOfStrings("Enums")),
Reid Klecknerf526b9482014-02-12 18:22:18 +0000869 uniques(uniqueEnumsInOrder(enums))
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000870 {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000871 QualifiedTypeName = getAttrName().str() + "Attr::" + type;
872
873 // FIXME: Emit a proper error
874 assert(!uniques.empty());
875 }
876
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000877 bool isVariadicEnumArg() const override { return true; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000878
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000879 void writeDeclarations(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +0000880 auto i = uniques.cbegin(), e = uniques.cend();
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000881 // The last one needs to not have a comma.
882 --e;
883
884 OS << "public:\n";
885 OS << " enum " << type << " {\n";
886 for (; i != e; ++i)
887 OS << " " << *i << ",\n";
888 OS << " " << *e << "\n";
889 OS << " };\n";
890 OS << "private:\n";
891
892 VariadicArgument::writeDeclarations(OS);
893 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000894
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000895 void writeDump(raw_ostream &OS) const override {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000896 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
897 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
898 << getLowerName() << "_end(); I != E; ++I) {\n";
899 OS << " switch(*I) {\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000900 for (const auto &UI : uniques) {
901 OS << " case " << getAttrName() << "Attr::" << UI << ":\n";
902 OS << " OS << \" " << UI << "\";\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000903 OS << " break;\n";
904 }
905 OS << " }\n";
906 OS << " }\n";
907 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000908
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000909 void writePCHReadDecls(raw_ostream &OS) const override {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000910 OS << " unsigned " << getLowerName() << "Size = Record[Idx++];\n";
911 OS << " SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName()
912 << ";\n";
913 OS << " " << getLowerName() << ".reserve(" << getLowerName()
914 << "Size);\n";
915 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
916 OS << " " << getLowerName() << ".push_back(" << "static_cast<"
917 << QualifiedTypeName << ">(Record[Idx++]));\n";
918 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000919
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000920 void writePCHWrite(raw_ostream &OS) const override {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000921 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
922 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
923 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
924 << getLowerName() << "_end(); i != e; ++i)\n";
925 OS << " " << WritePCHRecord(QualifiedTypeName, "(*i)");
926 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000927
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000928 void writeConversion(raw_ostream &OS) const {
929 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
930 OS << type << " &Out) {\n";
931 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000932 OS << type << ">>(Val)\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000933 for (size_t I = 0; I < enums.size(); ++I) {
934 OS << " .Case(\"" << values[I] << "\", ";
935 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
936 }
937 OS << " .Default(Optional<" << type << ">());\n";
938 OS << " if (R) {\n";
939 OS << " Out = *R;\n return true;\n }\n";
940 OS << " return false;\n";
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000941 OS << " }\n\n";
942
943 OS << " static const char *Convert" << type << "ToStr("
944 << type << " Val) {\n"
945 << " switch(Val) {\n";
George Burgess IV1881a572016-12-01 00:13:18 +0000946 SmallDenseSet<StringRef, 8> Uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000947 for (size_t I = 0; I < enums.size(); ++I) {
948 if (Uniques.insert(enums[I]).second)
949 OS << " case " << getAttrName() << "Attr::" << enums[I]
950 << ": return \"" << values[I] << "\";\n";
951 }
952 OS << " }\n"
953 << " llvm_unreachable(\"No enumerator with that value\");\n"
954 << " }\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000955 }
956 };
Peter Collingbournebee583f2011-10-06 13:03:08 +0000957
958 class VersionArgument : public Argument {
959 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000960 VersionArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000961 : Argument(Arg, Attr)
962 {}
963
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000964 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000965 OS << " VersionTuple get" << getUpperName() << "() const {\n";
966 OS << " return " << getLowerName() << ";\n";
967 OS << " }\n";
968 OS << " void set" << getUpperName()
969 << "(ASTContext &C, VersionTuple V) {\n";
970 OS << " " << getLowerName() << " = V;\n";
971 OS << " }";
972 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000973
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000974 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000975 OS << "get" << getUpperName() << "()";
976 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000977
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000978 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000979 OS << "A->get" << getUpperName() << "()";
980 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000981
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000982 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000983 OS << getLowerName() << "(" << getUpperName() << ")";
984 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000985
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000986 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000987 OS << getLowerName() << "()";
988 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000989
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000990 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000991 OS << "VersionTuple " << getUpperName();
992 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000993
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000994 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000995 OS << "VersionTuple " << getLowerName() << ";\n";
996 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000997
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000998 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000999 OS << " VersionTuple " << getLowerName()
1000 << "= ReadVersionTuple(Record, Idx);\n";
1001 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001002
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001003 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001004 OS << getLowerName();
1005 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001006
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001007 void writePCHWrite(raw_ostream &OS) const override {
Richard Smith290d8012016-04-06 17:06:00 +00001008 OS << " Record.AddVersionTuple(SA->get" << getUpperName() << "());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001009 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001010
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001011 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001012 OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";
1013 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001014
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001015 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001016 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
1017 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001018 };
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001019
1020 class ExprArgument : public SimpleArgument {
1021 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001022 ExprArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001023 : SimpleArgument(Arg, Attr, "Expr *")
1024 {}
1025
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001026 void writeASTVisitorTraversal(raw_ostream &OS) const override {
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001027 OS << " if (!"
1028 << "getDerived().TraverseStmt(A->get" << getUpperName() << "()))\n";
1029 OS << " return false;\n";
1030 }
1031
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001032 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001033 OS << "tempInst" << getUpperName();
1034 }
1035
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001036 void writeTemplateInstantiation(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001037 OS << " " << getType() << " tempInst" << getUpperName() << ";\n";
1038 OS << " {\n";
1039 OS << " EnterExpressionEvaluationContext "
1040 << "Unevaluated(S, Sema::Unevaluated);\n";
1041 OS << " ExprResult " << "Result = S.SubstExpr("
1042 << "A->get" << getUpperName() << "(), TemplateArgs);\n";
1043 OS << " tempInst" << getUpperName() << " = "
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001044 << "Result.getAs<Expr>();\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001045 OS << " }\n";
1046 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001047
Craig Topper3164f332014-03-11 03:39:26 +00001048 void writeDump(raw_ostream &OS) const override {}
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001049
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001050 void writeDumpChildren(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001051 OS << " dumpStmt(SA->get" << getUpperName() << "());\n";
1052 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001053
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001054 void writeHasChildren(raw_ostream &OS) const override { OS << "true"; }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001055 };
1056
1057 class VariadicExprArgument : public VariadicArgument {
1058 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001059 VariadicExprArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001060 : VariadicArgument(Arg, Attr, "Expr *")
1061 {}
1062
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001063 void writeASTVisitorTraversal(raw_ostream &OS) const override {
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001064 OS << " {\n";
1065 OS << " " << getType() << " *I = A->" << getLowerName()
1066 << "_begin();\n";
1067 OS << " " << getType() << " *E = A->" << getLowerName()
1068 << "_end();\n";
1069 OS << " for (; I != E; ++I) {\n";
1070 OS << " if (!getDerived().TraverseStmt(*I))\n";
1071 OS << " return false;\n";
1072 OS << " }\n";
1073 OS << " }\n";
1074 }
1075
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001076 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001077 OS << "tempInst" << getUpperName() << ", "
1078 << "A->" << getLowerName() << "_size()";
1079 }
1080
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001081 void writeTemplateInstantiation(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +00001082 OS << " auto *tempInst" << getUpperName()
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001083 << " = new (C, 16) " << getType()
1084 << "[A->" << getLowerName() << "_size()];\n";
1085 OS << " {\n";
1086 OS << " EnterExpressionEvaluationContext "
1087 << "Unevaluated(S, Sema::Unevaluated);\n";
1088 OS << " " << getType() << " *TI = tempInst" << getUpperName()
1089 << ";\n";
1090 OS << " " << getType() << " *I = A->" << getLowerName()
1091 << "_begin();\n";
1092 OS << " " << getType() << " *E = A->" << getLowerName()
1093 << "_end();\n";
1094 OS << " for (; I != E; ++I, ++TI) {\n";
1095 OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n";
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001096 OS << " *TI = Result.getAs<Expr>();\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001097 OS << " }\n";
1098 OS << " }\n";
1099 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001100
Craig Topper3164f332014-03-11 03:39:26 +00001101 void writeDump(raw_ostream &OS) const override {}
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001102
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001103 void writeDumpChildren(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001104 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
1105 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
Richard Smithf7514452014-10-30 21:02:37 +00001106 << getLowerName() << "_end(); I != E; ++I)\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001107 OS << " dumpStmt(*I);\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001108 }
1109
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001110 void writeHasChildren(raw_ostream &OS) const override {
Richard Trieude5cc7d2013-01-31 01:44:26 +00001111 OS << "SA->" << getLowerName() << "_begin() != "
1112 << "SA->" << getLowerName() << "_end()";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001113 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001114 };
Richard Smithb87c4652013-10-31 21:23:20 +00001115
Peter Collingbourne915df992015-05-15 18:33:32 +00001116 class VariadicStringArgument : public VariadicArgument {
1117 public:
1118 VariadicStringArgument(const Record &Arg, StringRef Attr)
Benjamin Kramer1b582012016-02-13 18:11:49 +00001119 : VariadicArgument(Arg, Attr, "StringRef")
Peter Collingbourne915df992015-05-15 18:33:32 +00001120 {}
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001121
Benjamin Kramer1b582012016-02-13 18:11:49 +00001122 void writeCtorBody(raw_ostream &OS) const override {
1123 OS << " for (size_t I = 0, E = " << getArgSizeName() << "; I != E;\n"
1124 " ++I) {\n"
1125 " StringRef Ref = " << getUpperName() << "[I];\n"
1126 " if (!Ref.empty()) {\n"
1127 " char *Mem = new (Ctx, 1) char[Ref.size()];\n"
1128 " std::memcpy(Mem, Ref.data(), Ref.size());\n"
1129 " " << getArgName() << "[I] = StringRef(Mem, Ref.size());\n"
1130 " }\n"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001131 " }\n";
Benjamin Kramer1b582012016-02-13 18:11:49 +00001132 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001133
Peter Collingbourne915df992015-05-15 18:33:32 +00001134 void writeValueImpl(raw_ostream &OS) const override {
1135 OS << " OS << \"\\\"\" << Val << \"\\\"\";\n";
1136 }
1137 };
1138
Richard Smithb87c4652013-10-31 21:23:20 +00001139 class TypeArgument : public SimpleArgument {
1140 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001141 TypeArgument(const Record &Arg, StringRef Attr)
Richard Smithb87c4652013-10-31 21:23:20 +00001142 : SimpleArgument(Arg, Attr, "TypeSourceInfo *")
1143 {}
1144
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001145 void writeAccessors(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001146 OS << " QualType get" << getUpperName() << "() const {\n";
1147 OS << " return " << getLowerName() << "->getType();\n";
1148 OS << " }";
1149 OS << " " << getType() << " get" << getUpperName() << "Loc() const {\n";
1150 OS << " return " << getLowerName() << ";\n";
1151 OS << " }";
1152 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001153
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001154 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001155 OS << "A->get" << getUpperName() << "Loc()";
1156 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001157
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001158 void writePCHWrite(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001159 OS << " " << WritePCHRecord(
1160 getType(), "SA->get" + std::string(getUpperName()) + "Loc()");
1161 }
1162 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001163
Hans Wennborgdcfba332015-10-06 23:40:43 +00001164} // end anonymous namespace
Peter Collingbournebee583f2011-10-06 13:03:08 +00001165
Aaron Ballman2f22b942014-05-20 19:47:14 +00001166static std::unique_ptr<Argument>
1167createArgument(const Record &Arg, StringRef Attr,
1168 const Record *Search = nullptr) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001169 if (!Search)
1170 Search = &Arg;
1171
David Blaikie28f30ca2014-08-08 23:59:38 +00001172 std::unique_ptr<Argument> Ptr;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001173 llvm::StringRef ArgName = Search->getName();
1174
David Blaikie28f30ca2014-08-08 23:59:38 +00001175 if (ArgName == "AlignedArgument")
1176 Ptr = llvm::make_unique<AlignedArgument>(Arg, Attr);
1177 else if (ArgName == "EnumArgument")
1178 Ptr = llvm::make_unique<EnumArgument>(Arg, Attr);
1179 else if (ArgName == "ExprArgument")
1180 Ptr = llvm::make_unique<ExprArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001181 else if (ArgName == "FunctionArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001182 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "FunctionDecl *");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001183 else if (ArgName == "IdentifierArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001184 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "IdentifierInfo *");
David Majnemer4bb09802014-02-10 19:50:15 +00001185 else if (ArgName == "DefaultBoolArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001186 Ptr = llvm::make_unique<DefaultSimpleArgument>(
1187 Arg, Attr, "bool", Arg.getValueAsBit("Default"));
1188 else if (ArgName == "BoolArgument")
1189 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "bool");
Aaron Ballman18a78382013-11-21 00:28:23 +00001190 else if (ArgName == "DefaultIntArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001191 Ptr = llvm::make_unique<DefaultSimpleArgument>(
1192 Arg, Attr, "int", Arg.getValueAsInt("Default"));
1193 else if (ArgName == "IntArgument")
1194 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "int");
1195 else if (ArgName == "StringArgument")
1196 Ptr = llvm::make_unique<StringArgument>(Arg, Attr);
1197 else if (ArgName == "TypeArgument")
1198 Ptr = llvm::make_unique<TypeArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001199 else if (ArgName == "UnsignedArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001200 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "unsigned");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001201 else if (ArgName == "VariadicUnsignedArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001202 Ptr = llvm::make_unique<VariadicArgument>(Arg, Attr, "unsigned");
Peter Collingbourne915df992015-05-15 18:33:32 +00001203 else if (ArgName == "VariadicStringArgument")
1204 Ptr = llvm::make_unique<VariadicStringArgument>(Arg, Attr);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001205 else if (ArgName == "VariadicEnumArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001206 Ptr = llvm::make_unique<VariadicEnumArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001207 else if (ArgName == "VariadicExprArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001208 Ptr = llvm::make_unique<VariadicExprArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001209 else if (ArgName == "VersionArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001210 Ptr = llvm::make_unique<VersionArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001211
1212 if (!Ptr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00001213 // Search in reverse order so that the most-derived type is handled first.
Craig Topper25761242016-01-18 19:52:54 +00001214 ArrayRef<std::pair<Record*, SMRange>> Bases = Search->getSuperClasses();
David Majnemerf7e36092016-06-23 00:15:04 +00001215 for (const auto &Base : llvm::reverse(Bases)) {
Craig Topper25761242016-01-18 19:52:54 +00001216 if ((Ptr = createArgument(Arg, Attr, Base.first)))
Peter Collingbournebee583f2011-10-06 13:03:08 +00001217 break;
1218 }
1219 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001220
1221 if (Ptr && Arg.getValueAsBit("Optional"))
1222 Ptr->setOptional(true);
1223
John McCalla62c1a92015-10-28 00:17:34 +00001224 if (Ptr && Arg.getValueAsBit("Fake"))
1225 Ptr->setFake(true);
1226
David Blaikie28f30ca2014-08-08 23:59:38 +00001227 return Ptr;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001228}
1229
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001230static void writeAvailabilityValue(raw_ostream &OS) {
1231 OS << "\" << getPlatform()->getName();\n"
Manman Ren42e09eb2016-03-10 23:54:12 +00001232 << " if (getStrict()) OS << \", strict\";\n"
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001233 << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n"
1234 << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n"
1235 << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n"
1236 << " if (getUnavailable()) OS << \", unavailable\";\n"
1237 << " OS << \"";
1238}
1239
Manman Renc7890fe2016-03-16 18:50:49 +00001240static void writeDeprecatedAttrValue(raw_ostream &OS, std::string &Variety) {
1241 OS << "\\\"\" << getMessage() << \"\\\"\";\n";
1242 // Only GNU deprecated has an optional fixit argument at the second position.
1243 if (Variety == "GNU")
1244 OS << " if (!getReplacement().empty()) OS << \", \\\"\""
1245 " << getReplacement() << \"\\\"\";\n";
1246 OS << " OS << \"";
1247}
1248
Aaron Ballman3e424b52013-12-26 18:30:57 +00001249static void writeGetSpellingFunction(Record &R, raw_ostream &OS) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001250 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballman3e424b52013-12-26 18:30:57 +00001251
1252 OS << "const char *" << R.getName() << "Attr::getSpelling() const {\n";
1253 if (Spellings.empty()) {
1254 OS << " return \"(No spelling)\";\n}\n\n";
1255 return;
1256 }
1257
1258 OS << " switch (SpellingListIndex) {\n"
1259 " default:\n"
1260 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1261 " return \"(No spelling)\";\n";
1262
1263 for (unsigned I = 0; I < Spellings.size(); ++I)
1264 OS << " case " << I << ":\n"
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001265 " return \"" << Spellings[I].name() << "\";\n";
Aaron Ballman3e424b52013-12-26 18:30:57 +00001266 // End of the switch statement.
1267 OS << " }\n";
1268 // End of the getSpelling function.
1269 OS << "}\n\n";
1270}
1271
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001272static void
1273writePrettyPrintFunction(Record &R,
1274 const std::vector<std::unique_ptr<Argument>> &Args,
1275 raw_ostream &OS) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001276 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Michael Han99315932013-01-24 16:46:58 +00001277
1278 OS << "void " << R.getName() << "Attr::printPretty("
1279 << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n";
1280
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001281 if (Spellings.empty()) {
Michael Han99315932013-01-24 16:46:58 +00001282 OS << "}\n\n";
1283 return;
1284 }
1285
1286 OS <<
1287 " switch (SpellingListIndex) {\n"
1288 " default:\n"
1289 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1290 " break;\n";
1291
1292 for (unsigned I = 0; I < Spellings.size(); ++ I) {
1293 llvm::SmallString<16> Prefix;
1294 llvm::SmallString<8> Suffix;
1295 // The actual spelling of the name and namespace (if applicable)
1296 // of an attribute without considering prefix and suffix.
1297 llvm::SmallString<64> Spelling;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001298 std::string Name = Spellings[I].name();
1299 std::string Variety = Spellings[I].variety();
Michael Han99315932013-01-24 16:46:58 +00001300
1301 if (Variety == "GNU") {
1302 Prefix = " __attribute__((";
1303 Suffix = "))";
1304 } else if (Variety == "CXX11") {
1305 Prefix = " [[";
1306 Suffix = "]]";
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001307 std::string Namespace = Spellings[I].nameSpace();
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001308 if (!Namespace.empty()) {
Michael Han99315932013-01-24 16:46:58 +00001309 Spelling += Namespace;
1310 Spelling += "::";
1311 }
1312 } else if (Variety == "Declspec") {
1313 Prefix = " __declspec(";
1314 Suffix = ")";
Nico Weber20e08042016-09-03 02:55:10 +00001315 } else if (Variety == "Microsoft") {
1316 Prefix = "[";
1317 Suffix = "]";
Richard Smith0cdcc982013-01-29 01:24:26 +00001318 } else if (Variety == "Keyword") {
1319 Prefix = " ";
1320 Suffix = "";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001321 } else if (Variety == "Pragma") {
1322 Prefix = "#pragma ";
1323 Suffix = "\n";
1324 std::string Namespace = Spellings[I].nameSpace();
1325 if (!Namespace.empty()) {
1326 Spelling += Namespace;
1327 Spelling += " ";
1328 }
Michael Han99315932013-01-24 16:46:58 +00001329 } else {
Richard Smith0cdcc982013-01-29 01:24:26 +00001330 llvm_unreachable("Unknown attribute syntax variety!");
Michael Han99315932013-01-24 16:46:58 +00001331 }
1332
1333 Spelling += Name;
1334
1335 OS <<
1336 " case " << I << " : {\n"
Yaron Keren09fb7c62015-03-10 07:33:23 +00001337 " OS << \"" << Prefix << Spelling;
Michael Han99315932013-01-24 16:46:58 +00001338
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001339 if (Variety == "Pragma") {
1340 OS << " \";\n";
1341 OS << " printPrettyPragma(OS, Policy);\n";
Alexey Bataev6d455322015-10-12 06:59:48 +00001342 OS << " OS << \"\\n\";";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001343 OS << " break;\n";
1344 OS << " }\n";
1345 continue;
1346 }
1347
John McCalla62c1a92015-10-28 00:17:34 +00001348 // Fake arguments aren't part of the parsed form and should not be
1349 // pretty-printed.
George Burgess IV1881a572016-12-01 00:13:18 +00001350 bool hasNonFakeArgs = llvm::any_of(
1351 Args, [](const std::unique_ptr<Argument> &A) { return !A->isFake(); });
John McCalla62c1a92015-10-28 00:17:34 +00001352
Aaron Ballmanc960f562014-08-01 13:49:00 +00001353 // FIXME: always printing the parenthesis isn't the correct behavior for
1354 // attributes which have optional arguments that were not provided. For
1355 // instance: __attribute__((aligned)) will be pretty printed as
1356 // __attribute__((aligned())). The logic should check whether there is only
1357 // a single argument, and if it is optional, whether it has been provided.
John McCalla62c1a92015-10-28 00:17:34 +00001358 if (hasNonFakeArgs)
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001359 OS << "(";
Michael Han99315932013-01-24 16:46:58 +00001360 if (Spelling == "availability") {
1361 writeAvailabilityValue(OS);
Manman Renc7890fe2016-03-16 18:50:49 +00001362 } else if (Spelling == "deprecated" || Spelling == "gnu::deprecated") {
1363 writeDeprecatedAttrValue(OS, Variety);
Michael Han99315932013-01-24 16:46:58 +00001364 } else {
John McCalla62c1a92015-10-28 00:17:34 +00001365 unsigned index = 0;
1366 for (const auto &arg : Args) {
1367 if (arg->isFake()) continue;
1368 if (index++) OS << ", ";
1369 arg->writeValue(OS);
Michael Han99315932013-01-24 16:46:58 +00001370 }
1371 }
1372
John McCalla62c1a92015-10-28 00:17:34 +00001373 if (hasNonFakeArgs)
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001374 OS << ")";
Yaron Keren09fb7c62015-03-10 07:33:23 +00001375 OS << Suffix + "\";\n";
Michael Han99315932013-01-24 16:46:58 +00001376
1377 OS <<
1378 " break;\n"
1379 " }\n";
1380 }
1381
1382 // End of the switch statement.
1383 OS << "}\n";
1384 // End of the print function.
1385 OS << "}\n\n";
1386}
1387
Michael Hanaf02bbe2013-02-01 01:19:17 +00001388/// \brief Return the index of a spelling in a spelling list.
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001389static unsigned
1390getSpellingListIndex(const std::vector<FlattenedSpelling> &SpellingList,
1391 const FlattenedSpelling &Spelling) {
Alexander Kornienko6ee521c2015-01-23 15:36:10 +00001392 assert(!SpellingList.empty() && "Spelling list is empty!");
Michael Hanaf02bbe2013-02-01 01:19:17 +00001393
1394 for (unsigned Index = 0; Index < SpellingList.size(); ++Index) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001395 const FlattenedSpelling &S = SpellingList[Index];
1396 if (S.variety() != Spelling.variety())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001397 continue;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001398 if (S.nameSpace() != Spelling.nameSpace())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001399 continue;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001400 if (S.name() != Spelling.name())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001401 continue;
1402
1403 return Index;
1404 }
1405
1406 llvm_unreachable("Unknown spelling!");
1407}
1408
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001409static void writeAttrAccessorDefinition(const Record &R, raw_ostream &OS) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00001410 std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors");
George Burgess IV1881a572016-12-01 00:13:18 +00001411 if (Accessors.empty())
1412 return;
1413
1414 const std::vector<FlattenedSpelling> SpellingList = GetFlattenedSpellings(R);
1415 assert(!SpellingList.empty() &&
1416 "Attribute with empty spelling list can't have accessors!");
Aaron Ballman2f22b942014-05-20 19:47:14 +00001417 for (const auto *Accessor : Accessors) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00001418 std::string Name = Accessor->getValueAsString("Name");
George Burgess IV1881a572016-12-01 00:13:18 +00001419 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Accessor);
Michael Hanaf02bbe2013-02-01 01:19:17 +00001420
1421 OS << " bool " << Name << "() const { return SpellingListIndex == ";
1422 for (unsigned Index = 0; Index < Spellings.size(); ++Index) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001423 OS << getSpellingListIndex(SpellingList, Spellings[Index]);
George Burgess IV1881a572016-12-01 00:13:18 +00001424 if (Index != Spellings.size() - 1)
Michael Hanaf02bbe2013-02-01 01:19:17 +00001425 OS << " ||\n SpellingListIndex == ";
1426 else
1427 OS << "; }\n";
1428 }
1429 }
1430}
1431
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001432static bool
1433SpellingNamesAreCommon(const std::vector<FlattenedSpelling>& Spellings) {
Aaron Ballman36a53502014-01-16 13:03:14 +00001434 assert(!Spellings.empty() && "An empty list of spellings was provided");
1435 std::string FirstName = NormalizeNameForSpellingComparison(
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001436 Spellings.front().name());
Aaron Ballman2f22b942014-05-20 19:47:14 +00001437 for (const auto &Spelling :
1438 llvm::make_range(std::next(Spellings.begin()), Spellings.end())) {
1439 std::string Name = NormalizeNameForSpellingComparison(Spelling.name());
Aaron Ballman36a53502014-01-16 13:03:14 +00001440 if (Name != FirstName)
1441 return false;
1442 }
1443 return true;
1444}
1445
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001446typedef std::map<unsigned, std::string> SemanticSpellingMap;
1447static std::string
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001448CreateSemanticSpellings(const std::vector<FlattenedSpelling> &Spellings,
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001449 SemanticSpellingMap &Map) {
1450 // The enumerants are automatically generated based on the variety,
1451 // namespace (if present) and name for each attribute spelling. However,
1452 // care is taken to avoid trampling on the reserved namespace due to
1453 // underscores.
1454 std::string Ret(" enum Spelling {\n");
1455 std::set<std::string> Uniques;
1456 unsigned Idx = 0;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001457 for (auto I = Spellings.begin(), E = Spellings.end(); I != E; ++I, ++Idx) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001458 const FlattenedSpelling &S = *I;
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00001459 const std::string &Variety = S.variety();
1460 const std::string &Spelling = S.name();
1461 const std::string &Namespace = S.nameSpace();
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001462 std::string EnumName;
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001463
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001464 EnumName += (Variety + "_");
1465 if (!Namespace.empty())
1466 EnumName += (NormalizeNameForSpellingComparison(Namespace).str() +
1467 "_");
1468 EnumName += NormalizeNameForSpellingComparison(Spelling);
1469
1470 // Even if the name is not unique, this spelling index corresponds to a
1471 // particular enumerant name that we've calculated.
1472 Map[Idx] = EnumName;
1473
1474 // Since we have been stripping underscores to avoid trampling on the
1475 // reserved namespace, we may have inadvertently created duplicate
1476 // enumerant names. These duplicates are not considered part of the
1477 // semantic spelling, and can be elided.
1478 if (Uniques.find(EnumName) != Uniques.end())
1479 continue;
1480
1481 Uniques.insert(EnumName);
1482 if (I != Spellings.begin())
1483 Ret += ",\n";
Aaron Ballman9bf6b752015-03-10 17:19:18 +00001484 // Duplicate spellings are not considered part of the semantic spelling
1485 // enumeration, but the spelling index and semantic spelling values are
1486 // meant to be equivalent, so we must specify a concrete value for each
1487 // enumerator.
1488 Ret += " " + EnumName + " = " + llvm::utostr(Idx);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001489 }
1490 Ret += "\n };\n\n";
1491 return Ret;
1492}
1493
1494void WriteSemanticSpellingSwitch(const std::string &VarName,
1495 const SemanticSpellingMap &Map,
1496 raw_ostream &OS) {
1497 OS << " switch (" << VarName << ") {\n default: "
1498 << "llvm_unreachable(\"Unknown spelling list index\");\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001499 for (const auto &I : Map)
1500 OS << " case " << I.first << ": return " << I.second << ";\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001501 OS << " }\n";
1502}
1503
Aaron Ballman35db2b32014-01-29 22:13:45 +00001504// Emits the LateParsed property for attributes.
1505static void emitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) {
1506 OS << "#if defined(CLANG_ATTR_LATE_PARSED_LIST)\n";
1507 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1508
Aaron Ballman2f22b942014-05-20 19:47:14 +00001509 for (const auto *Attr : Attrs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001510 bool LateParsed = Attr->getValueAsBit("LateParsed");
Aaron Ballman35db2b32014-01-29 22:13:45 +00001511
1512 if (LateParsed) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001513 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
Aaron Ballman35db2b32014-01-29 22:13:45 +00001514
1515 // FIXME: Handle non-GNU attributes
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001516 for (const auto &I : Spellings) {
1517 if (I.variety() != "GNU")
Aaron Ballman35db2b32014-01-29 22:13:45 +00001518 continue;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001519 OS << ".Case(\"" << I.name() << "\", " << LateParsed << ")\n";
Aaron Ballman35db2b32014-01-29 22:13:45 +00001520 }
1521 }
1522 }
1523 OS << "#endif // CLANG_ATTR_LATE_PARSED_LIST\n\n";
1524}
1525
George Burgess IV1881a572016-12-01 00:13:18 +00001526template <typename Fn>
1527static void forEachUniqueSpelling(const Record &Attr, Fn &&F) {
1528 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
1529 SmallDenseSet<StringRef, 8> Seen;
1530 for (const FlattenedSpelling &S : Spellings) {
1531 if (Seen.insert(S.name()).second)
1532 F(S);
1533 }
1534}
1535
Aaron Ballman35db2b32014-01-29 22:13:45 +00001536/// \brief Emits the first-argument-is-type property for attributes.
1537static void emitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) {
1538 OS << "#if defined(CLANG_ATTR_TYPE_ARG_LIST)\n";
1539 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
1540
Aaron Ballman2f22b942014-05-20 19:47:14 +00001541 for (const auto *Attr : Attrs) {
Aaron Ballman35db2b32014-01-29 22:13:45 +00001542 // Determine whether the first argument is a type.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001543 std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
Aaron Ballman35db2b32014-01-29 22:13:45 +00001544 if (Args.empty())
1545 continue;
1546
Craig Topper25761242016-01-18 19:52:54 +00001547 if (Args[0]->getSuperClasses().back().first->getName() != "TypeArgument")
Aaron Ballman35db2b32014-01-29 22:13:45 +00001548 continue;
1549
1550 // All these spellings take a single type argument.
George Burgess IV1881a572016-12-01 00:13:18 +00001551 forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) {
1552 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
1553 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00001554 }
1555 OS << "#endif // CLANG_ATTR_TYPE_ARG_LIST\n\n";
1556}
1557
1558/// \brief Emits the parse-arguments-in-unevaluated-context property for
1559/// attributes.
1560static void emitClangAttrArgContextList(RecordKeeper &Records, raw_ostream &OS) {
1561 OS << "#if defined(CLANG_ATTR_ARG_CONTEXT_LIST)\n";
1562 ParsedAttrMap Attrs = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001563 for (const auto &I : Attrs) {
1564 const Record &Attr = *I.second;
Aaron Ballman35db2b32014-01-29 22:13:45 +00001565
1566 if (!Attr.getValueAsBit("ParseArgumentsAsUnevaluated"))
1567 continue;
1568
1569 // All these spellings take are parsed unevaluated.
George Burgess IV1881a572016-12-01 00:13:18 +00001570 forEachUniqueSpelling(Attr, [&](const FlattenedSpelling &S) {
1571 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
1572 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00001573 }
1574 OS << "#endif // CLANG_ATTR_ARG_CONTEXT_LIST\n\n";
1575}
1576
1577static bool isIdentifierArgument(Record *Arg) {
1578 return !Arg->getSuperClasses().empty() &&
Craig Topper25761242016-01-18 19:52:54 +00001579 llvm::StringSwitch<bool>(Arg->getSuperClasses().back().first->getName())
Aaron Ballman35db2b32014-01-29 22:13:45 +00001580 .Case("IdentifierArgument", true)
1581 .Case("EnumArgument", true)
Aaron Ballman55ef1512014-12-19 16:42:04 +00001582 .Case("VariadicEnumArgument", true)
Aaron Ballman35db2b32014-01-29 22:13:45 +00001583 .Default(false);
1584}
1585
1586// Emits the first-argument-is-identifier property for attributes.
1587static void emitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) {
1588 OS << "#if defined(CLANG_ATTR_IDENTIFIER_ARG_LIST)\n";
1589 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1590
Aaron Ballman2f22b942014-05-20 19:47:14 +00001591 for (const auto *Attr : Attrs) {
Aaron Ballman35db2b32014-01-29 22:13:45 +00001592 // Determine whether the first argument is an identifier.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001593 std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
Aaron Ballman35db2b32014-01-29 22:13:45 +00001594 if (Args.empty() || !isIdentifierArgument(Args[0]))
1595 continue;
1596
1597 // All these spellings take an identifier argument.
George Burgess IV1881a572016-12-01 00:13:18 +00001598 forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) {
1599 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
1600 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00001601 }
1602 OS << "#endif // CLANG_ATTR_IDENTIFIER_ARG_LIST\n\n";
1603}
1604
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001605namespace clang {
1606
1607// Emits the class definitions for attributes.
1608void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001609 emitSourceFileHeader("Attribute classes' definitions", OS);
1610
Peter Collingbournebee583f2011-10-06 13:03:08 +00001611 OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
1612 OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
1613
1614 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1615
Aaron Ballman2f22b942014-05-20 19:47:14 +00001616 for (const auto *Attr : Attrs) {
1617 const Record &R = *Attr;
Aaron Ballman06bd44b2014-02-17 18:23:02 +00001618
1619 // FIXME: Currently, documentation is generated as-needed due to the fact
1620 // that there is no way to allow a generated project "reach into" the docs
1621 // directory (for instance, it may be an out-of-tree build). However, we want
1622 // to ensure that every attribute has a Documentation field, and produce an
1623 // error if it has been neglected. Otherwise, the on-demand generation which
1624 // happens server-side will fail. This code is ensuring that functionality,
1625 // even though this Emitter doesn't technically need the documentation.
1626 // When attribute documentation can be generated as part of the build
1627 // itself, this code can be removed.
1628 (void)R.getValueAsListOfDefs("Documentation");
Douglas Gregorb2daf842012-05-02 15:56:52 +00001629
1630 if (!R.getValueAsBit("ASTNode"))
1631 continue;
1632
Craig Topper25761242016-01-18 19:52:54 +00001633 ArrayRef<std::pair<Record *, SMRange>> Supers = R.getSuperClasses();
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001634 assert(!Supers.empty() && "Forgot to specify a superclass for the attr");
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001635 std::string SuperName;
David Majnemerf7e36092016-06-23 00:15:04 +00001636 for (const auto &Super : llvm::reverse(Supers)) {
Craig Topper25761242016-01-18 19:52:54 +00001637 const Record *R = Super.first;
1638 if (R->getName() != "TargetSpecificAttr" && SuperName.empty())
1639 SuperName = R->getName();
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001640 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001641
1642 OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
1643
1644 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001645 std::vector<std::unique_ptr<Argument>> Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001646 Args.reserve(ArgRecords.size());
1647
John McCalla62c1a92015-10-28 00:17:34 +00001648 bool HasOptArg = false;
1649 bool HasFakeArg = false;
Aaron Ballman2f22b942014-05-20 19:47:14 +00001650 for (const auto *ArgRecord : ArgRecords) {
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001651 Args.emplace_back(createArgument(*ArgRecord, R.getName()));
1652 Args.back()->writeDeclarations(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001653 OS << "\n\n";
John McCalla62c1a92015-10-28 00:17:34 +00001654
1655 // For these purposes, fake takes priority over optional.
1656 if (Args.back()->isFake()) {
1657 HasFakeArg = true;
1658 } else if (Args.back()->isOptional()) {
1659 HasOptArg = true;
1660 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001661 }
1662
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001663 OS << "public:\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00001664
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001665 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballman36a53502014-01-16 13:03:14 +00001666
1667 // If there are zero or one spellings, all spelling-related functionality
1668 // can be elided. If all of the spellings share the same name, the spelling
1669 // functionality can also be elided.
1670 bool ElideSpelling = (Spellings.size() <= 1) ||
1671 SpellingNamesAreCommon(Spellings);
1672
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001673 // This maps spelling index values to semantic Spelling enumerants.
1674 SemanticSpellingMap SemanticToSyntacticMap;
Aaron Ballman36a53502014-01-16 13:03:14 +00001675
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001676 if (!ElideSpelling)
1677 OS << CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
Aaron Ballman36a53502014-01-16 13:03:14 +00001678
John McCalla62c1a92015-10-28 00:17:34 +00001679 // Emit CreateImplicit factory methods.
1680 auto emitCreateImplicit = [&](bool emitFake) {
1681 OS << " static " << R.getName() << "Attr *CreateImplicit(";
1682 OS << "ASTContext &Ctx";
1683 if (!ElideSpelling)
1684 OS << ", Spelling S";
1685 for (auto const &ai : Args) {
1686 if (ai->isFake() && !emitFake) continue;
1687 OS << ", ";
1688 ai->writeCtorParameters(OS);
1689 }
1690 OS << ", SourceRange Loc = SourceRange()";
1691 OS << ") {\n";
Eugene Zelenko5f02b772015-12-08 18:49:01 +00001692 OS << " auto *A = new (Ctx) " << R.getName();
John McCalla62c1a92015-10-28 00:17:34 +00001693 OS << "Attr(Loc, Ctx, ";
1694 for (auto const &ai : Args) {
1695 if (ai->isFake() && !emitFake) continue;
1696 ai->writeImplicitCtorArgs(OS);
1697 OS << ", ";
1698 }
1699 OS << (ElideSpelling ? "0" : "S") << ");\n";
1700 OS << " A->setImplicit(true);\n";
1701 OS << " return A;\n }\n\n";
1702 };
Aaron Ballman36a53502014-01-16 13:03:14 +00001703
John McCalla62c1a92015-10-28 00:17:34 +00001704 // Emit a CreateImplicit that takes all the arguments.
1705 emitCreateImplicit(true);
1706
1707 // Emit a CreateImplicit that takes all the non-fake arguments.
1708 if (HasFakeArg) {
1709 emitCreateImplicit(false);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001710 }
Michael Han99315932013-01-24 16:46:58 +00001711
John McCalla62c1a92015-10-28 00:17:34 +00001712 // Emit constructors.
1713 auto emitCtor = [&](bool emitOpt, bool emitFake) {
1714 auto shouldEmitArg = [=](const std::unique_ptr<Argument> &arg) {
1715 if (arg->isFake()) return emitFake;
1716 if (arg->isOptional()) return emitOpt;
1717 return true;
1718 };
Michael Han99315932013-01-24 16:46:58 +00001719
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001720 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001721 for (auto const &ai : Args) {
John McCalla62c1a92015-10-28 00:17:34 +00001722 if (!shouldEmitArg(ai)) continue;
1723 OS << " , ";
1724 ai->writeCtorParameters(OS);
1725 OS << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001726 }
1727
1728 OS << " , ";
Aaron Ballman36a53502014-01-16 13:03:14 +00001729 OS << "unsigned SI\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001730
1731 OS << " )\n";
Benjamin Kramer845e32c2015-03-19 16:06:49 +00001732 OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI, "
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001733 << ( R.getValueAsBit("LateParsed") ? "true" : "false" ) << ", "
1734 << ( R.getValueAsBit("DuplicatesAllowedWhileMerging") ? "true" : "false" ) << ")\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001735
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001736 for (auto const &ai : Args) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001737 OS << " , ";
John McCalla62c1a92015-10-28 00:17:34 +00001738 if (!shouldEmitArg(ai)) {
1739 ai->writeCtorDefaultInitializers(OS);
1740 } else {
1741 ai->writeCtorInitializers(OS);
1742 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001743 OS << "\n";
1744 }
1745
1746 OS << " {\n";
1747
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001748 for (auto const &ai : Args) {
John McCalla62c1a92015-10-28 00:17:34 +00001749 if (!shouldEmitArg(ai)) continue;
1750 ai->writeCtorBody(OS);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001751 }
1752 OS << " }\n\n";
John McCalla62c1a92015-10-28 00:17:34 +00001753 };
1754
1755 // Emit a constructor that includes all the arguments.
1756 // This is necessary for cloning.
1757 emitCtor(true, true);
1758
1759 // Emit a constructor that takes all the non-fake arguments.
1760 if (HasFakeArg) {
1761 emitCtor(true, false);
1762 }
1763
1764 // Emit a constructor that takes all the non-fake, non-optional arguments.
1765 if (HasOptArg) {
1766 emitCtor(false, false);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001767 }
1768
Benjamin Kramer845e32c2015-03-19 16:06:49 +00001769 OS << " " << R.getName() << "Attr *clone(ASTContext &C) const;\n";
Craig Toppercbce6e92014-03-11 06:22:39 +00001770 OS << " void printPretty(raw_ostream &OS,\n"
Benjamin Kramer845e32c2015-03-19 16:06:49 +00001771 << " const PrintingPolicy &Policy) const;\n";
1772 OS << " const char *getSpelling() const;\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001773
1774 if (!ElideSpelling) {
1775 assert(!SemanticToSyntacticMap.empty() && "Empty semantic mapping list");
1776 OS << " Spelling getSemanticSpelling() const {\n";
1777 WriteSemanticSpellingSwitch("SpellingListIndex", SemanticToSyntacticMap,
1778 OS);
1779 OS << " }\n";
1780 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001781
Michael Hanaf02bbe2013-02-01 01:19:17 +00001782 writeAttrAccessorDefinition(R, OS);
1783
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001784 for (auto const &ai : Args) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001785 ai->writeAccessors(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001786 OS << "\n\n";
Aaron Ballman682ee422013-09-11 19:47:58 +00001787
John McCalla62c1a92015-10-28 00:17:34 +00001788 // Don't write conversion routines for fake arguments.
1789 if (ai->isFake()) continue;
1790
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001791 if (ai->isEnumArg())
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001792 static_cast<const EnumArgument *>(ai.get())->writeConversion(OS);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001793 else if (ai->isVariadicEnumArg())
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001794 static_cast<const VariadicEnumArgument *>(ai.get())
1795 ->writeConversion(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001796 }
1797
Jakob Stoklund Olesen6f2288b62012-01-13 04:57:47 +00001798 OS << R.getValueAsString("AdditionalMembers");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001799 OS << "\n\n";
1800
1801 OS << " static bool classof(const Attr *A) { return A->getKind() == "
1802 << "attr::" << R.getName() << "; }\n";
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001803
Peter Collingbournebee583f2011-10-06 13:03:08 +00001804 OS << "};\n\n";
1805 }
1806
Eugene Zelenko5f02b772015-12-08 18:49:01 +00001807 OS << "#endif // LLVM_CLANG_ATTR_CLASSES_INC\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001808}
1809
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001810// Emits the class method definitions for attributes.
1811void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001812 emitSourceFileHeader("Attribute classes' member function definitions", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001813
1814 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001815
Aaron Ballman2f22b942014-05-20 19:47:14 +00001816 for (auto *Attr : Attrs) {
1817 Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001818
1819 if (!R.getValueAsBit("ASTNode"))
1820 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001821
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001822 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1823 std::vector<std::unique_ptr<Argument>> Args;
Aaron Ballman2f22b942014-05-20 19:47:14 +00001824 for (const auto *Arg : ArgRecords)
1825 Args.emplace_back(createArgument(*Arg, R.getName()));
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001826
1827 for (auto const &ai : Args)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001828 ai->writeAccessorDefinitions(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001829
1830 OS << R.getName() << "Attr *" << R.getName()
1831 << "Attr::clone(ASTContext &C) const {\n";
Hans Wennborg613807b2014-05-31 01:30:30 +00001832 OS << " auto *A = new (C) " << R.getName() << "Attr(getLocation(), C";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001833 for (auto const &ai : Args) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001834 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001835 ai->writeCloneArgs(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001836 }
Hans Wennborg613807b2014-05-31 01:30:30 +00001837 OS << ", getSpellingListIndex());\n";
1838 OS << " A->Inherited = Inherited;\n";
1839 OS << " A->IsPackExpansion = IsPackExpansion;\n";
1840 OS << " A->Implicit = Implicit;\n";
1841 OS << " return A;\n}\n\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001842
Michael Han99315932013-01-24 16:46:58 +00001843 writePrettyPrintFunction(R, Args, OS);
Aaron Ballman3e424b52013-12-26 18:30:57 +00001844 writeGetSpellingFunction(R, OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001845 }
Benjamin Kramer845e32c2015-03-19 16:06:49 +00001846
1847 // Instead of relying on virtual dispatch we just create a huge dispatch
1848 // switch. This is both smaller and faster than virtual functions.
1849 auto EmitFunc = [&](const char *Method) {
1850 OS << " switch (getKind()) {\n";
1851 for (const auto *Attr : Attrs) {
1852 const Record &R = *Attr;
1853 if (!R.getValueAsBit("ASTNode"))
1854 continue;
1855
1856 OS << " case attr::" << R.getName() << ":\n";
1857 OS << " return cast<" << R.getName() << "Attr>(this)->" << Method
1858 << ";\n";
1859 }
Benjamin Kramer845e32c2015-03-19 16:06:49 +00001860 OS << " }\n";
1861 OS << " llvm_unreachable(\"Unexpected attribute kind!\");\n";
1862 OS << "}\n\n";
1863 };
1864
1865 OS << "const char *Attr::getSpelling() const {\n";
1866 EmitFunc("getSpelling()");
1867
1868 OS << "Attr *Attr::clone(ASTContext &C) const {\n";
1869 EmitFunc("clone(C)");
1870
1871 OS << "void Attr::printPretty(raw_ostream &OS, "
1872 "const PrintingPolicy &Policy) const {\n";
1873 EmitFunc("printPretty(OS, Policy)");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001874}
1875
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001876} // end namespace clang
1877
John McCall2225c8b2016-03-01 00:18:05 +00001878static void emitAttrList(raw_ostream &OS, StringRef Class,
Peter Collingbournebee583f2011-10-06 13:03:08 +00001879 const std::vector<Record*> &AttrList) {
John McCall2225c8b2016-03-01 00:18:05 +00001880 for (auto Cur : AttrList) {
1881 OS << Class << "(" << Cur->getName() << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001882 }
1883}
1884
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001885// Determines if an attribute has a Pragma spelling.
1886static bool AttrHasPragmaSpelling(const Record *R) {
1887 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
George Burgess IV1881a572016-12-01 00:13:18 +00001888 return llvm::find_if(Spellings, [](const FlattenedSpelling &S) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001889 return S.variety() == "Pragma";
1890 }) != Spellings.end();
1891}
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001892
John McCall2225c8b2016-03-01 00:18:05 +00001893namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001894
John McCall2225c8b2016-03-01 00:18:05 +00001895 struct AttrClassDescriptor {
1896 const char * const MacroName;
1897 const char * const TableGenName;
1898 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001899
1900} // end anonymous namespace
John McCall2225c8b2016-03-01 00:18:05 +00001901
1902static const AttrClassDescriptor AttrClassDescriptors[] = {
1903 { "ATTR", "Attr" },
Richard Smith4f902c72016-03-08 00:32:55 +00001904 { "STMT_ATTR", "StmtAttr" },
John McCall2225c8b2016-03-01 00:18:05 +00001905 { "INHERITABLE_ATTR", "InheritableAttr" },
John McCall477f2bb2016-03-03 06:39:32 +00001906 { "INHERITABLE_PARAM_ATTR", "InheritableParamAttr" },
1907 { "PARAMETER_ABI_ATTR", "ParameterABIAttr" }
John McCall2225c8b2016-03-01 00:18:05 +00001908};
1909
1910static void emitDefaultDefine(raw_ostream &OS, StringRef name,
1911 const char *superName) {
1912 OS << "#ifndef " << name << "\n";
1913 OS << "#define " << name << "(NAME) ";
1914 if (superName) OS << superName << "(NAME)";
1915 OS << "\n#endif\n\n";
1916}
1917
1918namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001919
John McCall2225c8b2016-03-01 00:18:05 +00001920 /// A class of attributes.
1921 struct AttrClass {
1922 const AttrClassDescriptor &Descriptor;
1923 Record *TheRecord;
1924 AttrClass *SuperClass = nullptr;
1925 std::vector<AttrClass*> SubClasses;
1926 std::vector<Record*> Attrs;
1927
1928 AttrClass(const AttrClassDescriptor &Descriptor, Record *R)
1929 : Descriptor(Descriptor), TheRecord(R) {}
1930
1931 void emitDefaultDefines(raw_ostream &OS) const {
1932 // Default the macro unless this is a root class (i.e. Attr).
1933 if (SuperClass) {
1934 emitDefaultDefine(OS, Descriptor.MacroName,
1935 SuperClass->Descriptor.MacroName);
1936 }
1937 }
1938
1939 void emitUndefs(raw_ostream &OS) const {
1940 OS << "#undef " << Descriptor.MacroName << "\n";
1941 }
1942
1943 void emitAttrList(raw_ostream &OS) const {
1944 for (auto SubClass : SubClasses) {
1945 SubClass->emitAttrList(OS);
1946 }
1947
1948 ::emitAttrList(OS, Descriptor.MacroName, Attrs);
1949 }
1950
1951 void classifyAttrOnRoot(Record *Attr) {
1952 bool result = classifyAttr(Attr);
1953 assert(result && "failed to classify on root"); (void) result;
1954 }
1955
1956 void emitAttrRange(raw_ostream &OS) const {
1957 OS << "ATTR_RANGE(" << Descriptor.TableGenName
1958 << ", " << getFirstAttr()->getName()
1959 << ", " << getLastAttr()->getName() << ")\n";
1960 }
1961
1962 private:
1963 bool classifyAttr(Record *Attr) {
1964 // Check all the subclasses.
1965 for (auto SubClass : SubClasses) {
1966 if (SubClass->classifyAttr(Attr))
1967 return true;
1968 }
1969
1970 // It's not more specific than this class, but it might still belong here.
1971 if (Attr->isSubClassOf(TheRecord)) {
1972 Attrs.push_back(Attr);
1973 return true;
1974 }
1975
1976 return false;
1977 }
1978
1979 Record *getFirstAttr() const {
1980 if (!SubClasses.empty())
1981 return SubClasses.front()->getFirstAttr();
1982 return Attrs.front();
1983 }
1984
1985 Record *getLastAttr() const {
1986 if (!Attrs.empty())
1987 return Attrs.back();
1988 return SubClasses.back()->getLastAttr();
1989 }
1990 };
1991
1992 /// The entire hierarchy of attribute classes.
1993 class AttrClassHierarchy {
1994 std::vector<std::unique_ptr<AttrClass>> Classes;
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001995
John McCall2225c8b2016-03-01 00:18:05 +00001996 public:
1997 AttrClassHierarchy(RecordKeeper &Records) {
1998 // Find records for all the classes.
1999 for (auto &Descriptor : AttrClassDescriptors) {
2000 Record *ClassRecord = Records.getClass(Descriptor.TableGenName);
2001 AttrClass *Class = new AttrClass(Descriptor, ClassRecord);
2002 Classes.emplace_back(Class);
2003 }
2004
2005 // Link up the hierarchy.
2006 for (auto &Class : Classes) {
2007 if (AttrClass *SuperClass = findSuperClass(Class->TheRecord)) {
2008 Class->SuperClass = SuperClass;
2009 SuperClass->SubClasses.push_back(Class.get());
2010 }
2011 }
2012
2013#ifndef NDEBUG
2014 for (auto i = Classes.begin(), e = Classes.end(); i != e; ++i) {
2015 assert((i == Classes.begin()) == ((*i)->SuperClass == nullptr) &&
2016 "only the first class should be a root class!");
2017 }
2018#endif
2019 }
2020
2021 void emitDefaultDefines(raw_ostream &OS) const {
2022 for (auto &Class : Classes) {
2023 Class->emitDefaultDefines(OS);
2024 }
2025 }
2026
2027 void emitUndefs(raw_ostream &OS) const {
2028 for (auto &Class : Classes) {
2029 Class->emitUndefs(OS);
2030 }
2031 }
2032
2033 void emitAttrLists(raw_ostream &OS) const {
2034 // Just start from the root class.
2035 Classes[0]->emitAttrList(OS);
2036 }
2037
2038 void emitAttrRanges(raw_ostream &OS) const {
2039 for (auto &Class : Classes)
2040 Class->emitAttrRange(OS);
2041 }
2042
2043 void classifyAttr(Record *Attr) {
2044 // Add the attribute to the root class.
2045 Classes[0]->classifyAttrOnRoot(Attr);
2046 }
2047
2048 private:
2049 AttrClass *findClassByRecord(Record *R) const {
2050 for (auto &Class : Classes) {
2051 if (Class->TheRecord == R)
2052 return Class.get();
2053 }
2054 return nullptr;
2055 }
2056
2057 AttrClass *findSuperClass(Record *R) const {
2058 // TableGen flattens the superclass list, so we just need to walk it
2059 // in reverse.
2060 auto SuperClasses = R->getSuperClasses();
2061 for (signed i = 0, e = SuperClasses.size(); i != e; ++i) {
2062 auto SuperClass = findClassByRecord(SuperClasses[e - i - 1].first);
2063 if (SuperClass) return SuperClass;
2064 }
2065 return nullptr;
2066 }
2067 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002068
2069} // end anonymous namespace
John McCall2225c8b2016-03-01 00:18:05 +00002070
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002071namespace clang {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002072
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002073// Emits the enumeration list for attributes.
2074void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002075 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002076
John McCall2225c8b2016-03-01 00:18:05 +00002077 AttrClassHierarchy Hierarchy(Records);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002078
John McCall2225c8b2016-03-01 00:18:05 +00002079 // Add defaulting macro definitions.
2080 Hierarchy.emitDefaultDefines(OS);
2081 emitDefaultDefine(OS, "PRAGMA_SPELLING_ATTR", nullptr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002082
John McCall2225c8b2016-03-01 00:18:05 +00002083 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
2084 std::vector<Record *> PragmaAttrs;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002085 for (auto *Attr : Attrs) {
2086 if (!Attr->getValueAsBit("ASTNode"))
Douglas Gregorb2daf842012-05-02 15:56:52 +00002087 continue;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002088
John McCall2225c8b2016-03-01 00:18:05 +00002089 // Add the attribute to the ad-hoc groups.
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002090 if (AttrHasPragmaSpelling(Attr))
2091 PragmaAttrs.push_back(Attr);
2092
John McCall2225c8b2016-03-01 00:18:05 +00002093 // Place it in the hierarchy.
2094 Hierarchy.classifyAttr(Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002095 }
2096
John McCall2225c8b2016-03-01 00:18:05 +00002097 // Emit the main attribute list.
2098 Hierarchy.emitAttrLists(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002099
John McCall2225c8b2016-03-01 00:18:05 +00002100 // Emit the ad hoc groups.
2101 emitAttrList(OS, "PRAGMA_SPELLING_ATTR", PragmaAttrs);
2102
2103 // Emit the attribute ranges.
2104 OS << "#ifdef ATTR_RANGE\n";
2105 Hierarchy.emitAttrRanges(OS);
2106 OS << "#undef ATTR_RANGE\n";
2107 OS << "#endif\n";
2108
2109 Hierarchy.emitUndefs(OS);
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002110 OS << "#undef PRAGMA_SPELLING_ATTR\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002111}
2112
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002113// Emits the code to read an attribute from a precompiled header.
2114void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002115 emitSourceFileHeader("Attribute deserialization code", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002116
2117 Record *InhClass = Records.getClass("InheritableAttr");
2118 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
2119 ArgRecords;
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002120 std::vector<std::unique_ptr<Argument>> Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002121
2122 OS << " switch (Kind) {\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002123 for (const auto *Attr : Attrs) {
2124 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002125 if (!R.getValueAsBit("ASTNode"))
2126 continue;
2127
Peter Collingbournebee583f2011-10-06 13:03:08 +00002128 OS << " case attr::" << R.getName() << ": {\n";
2129 if (R.isSubClassOf(InhClass))
2130 OS << " bool isInherited = Record[Idx++];\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002131 OS << " bool isImplicit = Record[Idx++];\n";
2132 OS << " unsigned Spelling = Record[Idx++];\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002133 ArgRecords = R.getValueAsListOfDefs("Args");
2134 Args.clear();
Aaron Ballman2f22b942014-05-20 19:47:14 +00002135 for (const auto *Arg : ArgRecords) {
2136 Args.emplace_back(createArgument(*Arg, R.getName()));
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002137 Args.back()->writePCHReadDecls(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002138 }
2139 OS << " New = new (Context) " << R.getName() << "Attr(Range, Context";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002140 for (auto const &ri : Args) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00002141 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002142 ri->writePCHReadArgs(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002143 }
Aaron Ballman36a53502014-01-16 13:03:14 +00002144 OS << ", Spelling);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002145 if (R.isSubClassOf(InhClass))
2146 OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002147 OS << " New->setImplicit(isImplicit);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002148 OS << " break;\n";
2149 OS << " }\n";
2150 }
2151 OS << " }\n";
2152}
2153
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002154// Emits the code to write an attribute to a precompiled header.
2155void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002156 emitSourceFileHeader("Attribute serialization code", OS);
2157
Peter Collingbournebee583f2011-10-06 13:03:08 +00002158 Record *InhClass = Records.getClass("InheritableAttr");
2159 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002160
2161 OS << " switch (A->getKind()) {\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002162 for (const auto *Attr : Attrs) {
2163 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002164 if (!R.getValueAsBit("ASTNode"))
2165 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002166 OS << " case attr::" << R.getName() << ": {\n";
2167 Args = R.getValueAsListOfDefs("Args");
2168 if (R.isSubClassOf(InhClass) || !Args.empty())
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002169 OS << " const auto *SA = cast<" << R.getName()
Peter Collingbournebee583f2011-10-06 13:03:08 +00002170 << "Attr>(A);\n";
2171 if (R.isSubClassOf(InhClass))
2172 OS << " Record.push_back(SA->isInherited());\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002173 OS << " Record.push_back(A->isImplicit());\n";
2174 OS << " Record.push_back(A->getSpellingListIndex());\n";
2175
Aaron Ballman2f22b942014-05-20 19:47:14 +00002176 for (const auto *Arg : Args)
2177 createArgument(*Arg, R.getName())->writePCHWrite(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002178 OS << " break;\n";
2179 OS << " }\n";
2180 }
2181 OS << " }\n";
2182}
2183
Bob Wilson0058b822015-07-20 22:57:36 +00002184// Generate a conditional expression to check if the current target satisfies
2185// the conditions for a TargetSpecificAttr record, and append the code for
2186// those checks to the Test string. If the FnName string pointer is non-null,
2187// append a unique suffix to distinguish this set of target checks from other
2188// TargetSpecificAttr records.
2189static void GenerateTargetSpecificAttrChecks(const Record *R,
2190 std::vector<std::string> &Arches,
2191 std::string &Test,
2192 std::string *FnName) {
2193 // It is assumed that there will be an llvm::Triple object
2194 // named "T" and a TargetInfo object named "Target" within
2195 // scope that can be used to determine whether the attribute exists in
2196 // a given target.
2197 Test += "(";
2198
2199 for (auto I = Arches.begin(), E = Arches.end(); I != E; ++I) {
2200 std::string Part = *I;
2201 Test += "T.getArch() == llvm::Triple::" + Part;
2202 if (I + 1 != E)
2203 Test += " || ";
2204 if (FnName)
2205 *FnName += Part;
2206 }
2207 Test += ")";
2208
2209 // If the attribute is specific to particular OSes, check those.
2210 if (!R->isValueUnset("OSes")) {
2211 // We know that there was at least one arch test, so we need to and in the
2212 // OS tests.
2213 Test += " && (";
2214 std::vector<std::string> OSes = R->getValueAsListOfStrings("OSes");
2215 for (auto I = OSes.begin(), E = OSes.end(); I != E; ++I) {
2216 std::string Part = *I;
2217
2218 Test += "T.getOS() == llvm::Triple::" + Part;
2219 if (I + 1 != E)
2220 Test += " || ";
2221 if (FnName)
2222 *FnName += Part;
2223 }
2224 Test += ")";
2225 }
2226
2227 // If one or more CXX ABIs are specified, check those as well.
2228 if (!R->isValueUnset("CXXABIs")) {
2229 Test += " && (";
2230 std::vector<std::string> CXXABIs = R->getValueAsListOfStrings("CXXABIs");
2231 for (auto I = CXXABIs.begin(), E = CXXABIs.end(); I != E; ++I) {
2232 std::string Part = *I;
2233 Test += "Target.getCXXABI().getKind() == TargetCXXABI::" + Part;
2234 if (I + 1 != E)
2235 Test += " || ";
2236 if (FnName)
2237 *FnName += Part;
2238 }
2239 Test += ")";
2240 }
2241}
2242
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002243static void GenerateHasAttrSpellingStringSwitch(
2244 const std::vector<Record *> &Attrs, raw_ostream &OS,
2245 const std::string &Variety = "", const std::string &Scope = "") {
2246 for (const auto *Attr : Attrs) {
Aaron Ballmana0344c52014-11-14 13:44:02 +00002247 // C++11-style attributes have specific version information associated with
2248 // them. If the attribute has no scope, the version information must not
2249 // have the default value (1), as that's incorrect. Instead, the unscoped
2250 // attribute version information should be taken from the SD-6 standing
2251 // document, which can be found at:
2252 // https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations
2253 int Version = 1;
2254
2255 if (Variety == "CXX11") {
2256 std::vector<Record *> Spellings = Attr->getValueAsListOfDefs("Spellings");
2257 for (const auto &Spelling : Spellings) {
2258 if (Spelling->getValueAsString("Variety") == "CXX11") {
2259 Version = static_cast<int>(Spelling->getValueAsInt("Version"));
2260 if (Scope.empty() && Version == 1)
2261 PrintError(Spelling->getLoc(), "C++ standard attributes must "
2262 "have valid version information.");
2263 break;
2264 }
2265 }
2266 }
2267
Aaron Ballman0fa06d82014-01-09 22:57:44 +00002268 std::string Test;
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002269 if (Attr->isSubClassOf("TargetSpecificAttr")) {
2270 const Record *R = Attr->getValueAsDef("Target");
Aaron Ballman0fa06d82014-01-09 22:57:44 +00002271 std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches");
Hans Wennborgdcfba332015-10-06 23:40:43 +00002272 GenerateTargetSpecificAttrChecks(R, Arches, Test, nullptr);
Bob Wilson7c730832015-07-20 22:57:31 +00002273
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002274 // If this is the C++11 variety, also add in the LangOpts test.
2275 if (Variety == "CXX11")
2276 Test += " && LangOpts.CPlusPlus11";
2277 } else if (Variety == "CXX11")
2278 // C++11 mode should be checked against LangOpts, which is presumed to be
2279 // present in the caller.
2280 Test = "LangOpts.CPlusPlus11";
Aaron Ballman0fa06d82014-01-09 22:57:44 +00002281
Aaron Ballmana0344c52014-11-14 13:44:02 +00002282 std::string TestStr =
Aaron Ballman28afa182014-11-17 18:17:19 +00002283 !Test.empty() ? Test + " ? " + llvm::itostr(Version) + " : 0" : "1";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002284 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002285 for (const auto &S : Spellings)
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002286 if (Variety.empty() || (Variety == S.variety() &&
2287 (Scope.empty() || Scope == S.nameSpace())))
Aaron Ballmana0344c52014-11-14 13:44:02 +00002288 OS << " .Case(\"" << S.name() << "\", " << TestStr << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002289 }
Aaron Ballmana0344c52014-11-14 13:44:02 +00002290 OS << " .Default(0);\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002291}
2292
2293// Emits the list of spellings for attributes.
2294void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
2295 emitSourceFileHeader("Code to implement the __has_attribute logic", OS);
2296
2297 // Separate all of the attributes out into four group: generic, C++11, GNU,
2298 // and declspecs. Then generate a big switch statement for each of them.
2299 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Nico Weber20e08042016-09-03 02:55:10 +00002300 std::vector<Record *> Declspec, Microsoft, GNU, Pragma;
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002301 std::map<std::string, std::vector<Record *>> CXX;
2302
2303 // Walk over the list of all attributes, and split them out based on the
2304 // spelling variety.
2305 for (auto *R : Attrs) {
2306 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
2307 for (const auto &SI : Spellings) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00002308 const std::string &Variety = SI.variety();
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002309 if (Variety == "GNU")
2310 GNU.push_back(R);
2311 else if (Variety == "Declspec")
2312 Declspec.push_back(R);
Nico Weber20e08042016-09-03 02:55:10 +00002313 else if (Variety == "Microsoft")
2314 Microsoft.push_back(R);
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002315 else if (Variety == "CXX11")
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002316 CXX[SI.nameSpace()].push_back(R);
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002317 else if (Variety == "Pragma")
2318 Pragma.push_back(R);
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002319 }
2320 }
2321
Bob Wilson7c730832015-07-20 22:57:31 +00002322 OS << "const llvm::Triple &T = Target.getTriple();\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002323 OS << "switch (Syntax) {\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002324 OS << "case AttrSyntax::GNU:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002325 OS << " return llvm::StringSwitch<int>(Name)\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002326 GenerateHasAttrSpellingStringSwitch(GNU, OS, "GNU");
2327 OS << "case AttrSyntax::Declspec:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002328 OS << " return llvm::StringSwitch<int>(Name)\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002329 GenerateHasAttrSpellingStringSwitch(Declspec, OS, "Declspec");
Nico Weber20e08042016-09-03 02:55:10 +00002330 OS << "case AttrSyntax::Microsoft:\n";
2331 OS << " return llvm::StringSwitch<int>(Name)\n";
2332 GenerateHasAttrSpellingStringSwitch(Microsoft, OS, "Microsoft");
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002333 OS << "case AttrSyntax::Pragma:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002334 OS << " return llvm::StringSwitch<int>(Name)\n";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002335 GenerateHasAttrSpellingStringSwitch(Pragma, OS, "Pragma");
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002336 OS << "case AttrSyntax::CXX: {\n";
2337 // C++11-style attributes are further split out based on the Scope.
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002338 for (auto I = CXX.cbegin(), E = CXX.cend(); I != E; ++I) {
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002339 if (I != CXX.begin())
2340 OS << " else ";
2341 if (I->first.empty())
2342 OS << "if (!Scope || Scope->getName() == \"\") {\n";
2343 else
2344 OS << "if (Scope->getName() == \"" << I->first << "\") {\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002345 OS << " return llvm::StringSwitch<int>(Name)\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002346 GenerateHasAttrSpellingStringSwitch(I->second, OS, "CXX11", I->first);
2347 OS << "}";
2348 }
2349 OS << "\n}\n";
2350 OS << "}\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002351}
2352
Michael Han99315932013-01-24 16:46:58 +00002353void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002354 emitSourceFileHeader("Code to translate different attribute spellings "
2355 "into internal identifiers", OS);
Michael Han99315932013-01-24 16:46:58 +00002356
John McCall2225c8b2016-03-01 00:18:05 +00002357 OS << " switch (AttrKind) {\n";
Michael Han99315932013-01-24 16:46:58 +00002358
Aaron Ballman64e69862013-12-15 13:05:48 +00002359 ParsedAttrMap Attrs = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002360 for (const auto &I : Attrs) {
Aaron Ballman2f22b942014-05-20 19:47:14 +00002361 const Record &R = *I.second;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002362 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002363 OS << " case AT_" << I.first << ": {\n";
Richard Smith852e9ce2013-11-27 01:46:48 +00002364 for (unsigned I = 0; I < Spellings.size(); ++ I) {
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002365 OS << " if (Name == \"" << Spellings[I].name() << "\" && "
2366 << "SyntaxUsed == "
2367 << StringSwitch<unsigned>(Spellings[I].variety())
2368 .Case("GNU", 0)
2369 .Case("CXX11", 1)
2370 .Case("Declspec", 2)
Nico Weber20e08042016-09-03 02:55:10 +00002371 .Case("Microsoft", 3)
2372 .Case("Keyword", 4)
2373 .Case("Pragma", 5)
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002374 .Default(0)
2375 << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n"
2376 << " return " << I << ";\n";
Michael Han99315932013-01-24 16:46:58 +00002377 }
Richard Smith852e9ce2013-11-27 01:46:48 +00002378
2379 OS << " break;\n";
2380 OS << " }\n";
Michael Han99315932013-01-24 16:46:58 +00002381 }
2382
2383 OS << " }\n";
Aaron Ballman64e69862013-12-15 13:05:48 +00002384 OS << " return 0;\n";
Michael Han99315932013-01-24 16:46:58 +00002385}
2386
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002387// Emits code used by RecursiveASTVisitor to visit attributes
2388void EmitClangAttrASTVisitor(RecordKeeper &Records, raw_ostream &OS) {
2389 emitSourceFileHeader("Used by RecursiveASTVisitor to visit attributes.", OS);
2390
2391 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2392
2393 // Write method declarations for Traverse* methods.
2394 // We emit this here because we only generate methods for attributes that
2395 // are declared as ASTNodes.
2396 OS << "#ifdef ATTR_VISITOR_DECLS_ONLY\n\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002397 for (const auto *Attr : Attrs) {
2398 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002399 if (!R.getValueAsBit("ASTNode"))
2400 continue;
2401 OS << " bool Traverse"
2402 << R.getName() << "Attr(" << R.getName() << "Attr *A);\n";
2403 OS << " bool Visit"
2404 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
2405 << " return true; \n"
Hans Wennborg4afe5042015-07-22 20:46:26 +00002406 << " }\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002407 }
2408 OS << "\n#else // ATTR_VISITOR_DECLS_ONLY\n\n";
2409
2410 // Write individual Traverse* methods for each attribute class.
Aaron Ballman2f22b942014-05-20 19:47:14 +00002411 for (const auto *Attr : Attrs) {
2412 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002413 if (!R.getValueAsBit("ASTNode"))
2414 continue;
2415
2416 OS << "template <typename Derived>\n"
DeLesley Hutchinsbb79c332013-12-30 21:03:02 +00002417 << "bool VISITORCLASS<Derived>::Traverse"
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002418 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
2419 << " if (!getDerived().VisitAttr(A))\n"
2420 << " return false;\n"
2421 << " if (!getDerived().Visit" << R.getName() << "Attr(A))\n"
2422 << " return false;\n";
2423
2424 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman2f22b942014-05-20 19:47:14 +00002425 for (const auto *Arg : ArgRecords)
2426 createArgument(*Arg, R.getName())->writeASTVisitorTraversal(OS);
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002427
2428 OS << " return true;\n";
2429 OS << "}\n\n";
2430 }
2431
2432 // Write generic Traverse routine
2433 OS << "template <typename Derived>\n"
DeLesley Hutchinsbb79c332013-12-30 21:03:02 +00002434 << "bool VISITORCLASS<Derived>::TraverseAttr(Attr *A) {\n"
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002435 << " if (!A)\n"
2436 << " return true;\n"
2437 << "\n"
John McCall2225c8b2016-03-01 00:18:05 +00002438 << " switch (A->getKind()) {\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002439
Aaron Ballman2f22b942014-05-20 19:47:14 +00002440 for (const auto *Attr : Attrs) {
2441 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002442 if (!R.getValueAsBit("ASTNode"))
2443 continue;
2444
2445 OS << " case attr::" << R.getName() << ":\n"
2446 << " return getDerived().Traverse" << R.getName() << "Attr("
2447 << "cast<" << R.getName() << "Attr>(A));\n";
2448 }
John McCall5d7cf772016-03-01 02:09:20 +00002449 OS << " }\n"; // end switch
2450 OS << " llvm_unreachable(\"bad attribute kind\");\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002451 OS << "}\n"; // end function
2452 OS << "#endif // ATTR_VISITOR_DECLS_ONLY\n";
2453}
2454
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002455// Emits code to instantiate dependent attributes on templates.
2456void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002457 emitSourceFileHeader("Template instantiation code for attributes", OS);
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002458
2459 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2460
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00002461 OS << "namespace clang {\n"
2462 << "namespace sema {\n\n"
2463 << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, "
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002464 << "Sema &S,\n"
2465 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"
John McCall2225c8b2016-03-01 00:18:05 +00002466 << " switch (At->getKind()) {\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002467
Aaron Ballman2f22b942014-05-20 19:47:14 +00002468 for (const auto *Attr : Attrs) {
2469 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002470 if (!R.getValueAsBit("ASTNode"))
2471 continue;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002472
2473 OS << " case attr::" << R.getName() << ": {\n";
Rafael Espindola7f90b7d2012-05-15 14:09:55 +00002474 bool ShouldClone = R.getValueAsBit("Clone");
2475
2476 if (!ShouldClone) {
Hans Wennborg59dbe862015-09-29 20:56:43 +00002477 OS << " return nullptr;\n";
Rafael Espindola7f90b7d2012-05-15 14:09:55 +00002478 OS << " }\n";
2479 continue;
2480 }
2481
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002482 OS << " const auto *A = cast<"
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002483 << R.getName() << "Attr>(At);\n";
2484 bool TDependent = R.getValueAsBit("TemplateDependent");
2485
2486 if (!TDependent) {
2487 OS << " return A->clone(C);\n";
2488 OS << " }\n";
2489 continue;
2490 }
2491
2492 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002493 std::vector<std::unique_ptr<Argument>> Args;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002494 Args.reserve(ArgRecords.size());
2495
Aaron Ballman2f22b942014-05-20 19:47:14 +00002496 for (const auto *ArgRecord : ArgRecords)
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002497 Args.emplace_back(createArgument(*ArgRecord, R.getName()));
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002498
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002499 for (auto const &ai : Args)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002500 ai->writeTemplateInstantiation(OS);
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002501
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002502 OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002503 for (auto const &ai : Args) {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002504 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002505 ai->writeTemplateInstantiationArgs(OS);
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002506 }
Aaron Ballman36a53502014-01-16 13:03:14 +00002507 OS << ", A->getSpellingListIndex());\n }\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002508 }
2509 OS << " } // end switch\n"
2510 << " llvm_unreachable(\"Unknown attribute!\");\n"
Hans Wennborg59dbe862015-09-29 20:56:43 +00002511 << " return nullptr;\n"
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00002512 << "}\n\n"
2513 << "} // end namespace sema\n"
2514 << "} // end namespace clang\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002515}
2516
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002517// Emits the list of parsed attributes.
2518void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) {
2519 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
2520
2521 OS << "#ifndef PARSED_ATTR\n";
2522 OS << "#define PARSED_ATTR(NAME) NAME\n";
2523 OS << "#endif\n\n";
2524
2525 ParsedAttrMap Names = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002526 for (const auto &I : Names) {
2527 OS << "PARSED_ATTR(" << I.first << ")\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002528 }
2529}
2530
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002531static bool isArgVariadic(const Record &R, StringRef AttrName) {
2532 return createArgument(R, AttrName)->isVariadic();
2533}
2534
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002535static void emitArgInfo(const Record &R, std::stringstream &OS) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002536 // This function will count the number of arguments specified for the
2537 // attribute and emit the number of required arguments followed by the
2538 // number of optional arguments.
2539 std::vector<Record *> Args = R.getValueAsListOfDefs("Args");
2540 unsigned ArgCount = 0, OptCount = 0;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002541 bool HasVariadic = false;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002542 for (const auto *Arg : Args) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002543 Arg->getValueAsBit("Optional") ? ++OptCount : ++ArgCount;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002544 if (!HasVariadic && isArgVariadic(*Arg, R.getName()))
2545 HasVariadic = true;
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002546 }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002547
2548 // If there is a variadic argument, we will set the optional argument count
2549 // to its largest value. Since it's currently a 4-bit number, we set it to 15.
2550 OS << ArgCount << ", " << (HasVariadic ? 15 : OptCount);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002551}
2552
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002553static void GenerateDefaultAppertainsTo(raw_ostream &OS) {
Aaron Ballman93b5cc62013-12-02 19:36:42 +00002554 OS << "static bool defaultAppertainsTo(Sema &, const AttributeList &,";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002555 OS << "const Decl *) {\n";
2556 OS << " return true;\n";
2557 OS << "}\n\n";
2558}
2559
2560static std::string CalculateDiagnostic(const Record &S) {
2561 // If the SubjectList object has a custom diagnostic associated with it,
2562 // return that directly.
2563 std::string CustomDiag = S.getValueAsString("CustomDiag");
2564 if (!CustomDiag.empty())
2565 return CustomDiag;
2566
2567 // Given the list of subjects, determine what diagnostic best fits.
2568 enum {
2569 Func = 1U << 0,
2570 Var = 1U << 1,
2571 ObjCMethod = 1U << 2,
2572 Param = 1U << 3,
2573 Class = 1U << 4,
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00002574 GenericRecord = 1U << 5,
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002575 Type = 1U << 6,
2576 ObjCIVar = 1U << 7,
2577 ObjCProp = 1U << 8,
2578 ObjCInterface = 1U << 9,
2579 Block = 1U << 10,
2580 Namespace = 1U << 11,
Aaron Ballman981ba242014-05-20 14:10:53 +00002581 Field = 1U << 12,
2582 CXXMethod = 1U << 13,
Alexis Hunt724f14e2014-11-28 00:53:20 +00002583 ObjCProtocol = 1U << 14,
2584 Enum = 1U << 15
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002585 };
2586 uint32_t SubMask = 0;
2587
2588 std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects");
Aaron Ballman2f22b942014-05-20 19:47:14 +00002589 for (const auto *Subject : Subjects) {
2590 const Record &R = *Subject;
Aaron Ballman80469032013-11-29 14:57:58 +00002591 std::string Name;
2592
2593 if (R.isSubClassOf("SubsetSubject")) {
2594 PrintError(R.getLoc(), "SubsetSubjects should use a custom diagnostic");
2595 // As a fallback, look through the SubsetSubject to see what its base
2596 // type is, and use that. This needs to be updated if SubsetSubjects
2597 // are allowed within other SubsetSubjects.
2598 Name = R.getValueAsDef("Base")->getName();
2599 } else
2600 Name = R.getName();
2601
2602 uint32_t V = StringSwitch<uint32_t>(Name)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002603 .Case("Function", Func)
2604 .Case("Var", Var)
2605 .Case("ObjCMethod", ObjCMethod)
2606 .Case("ParmVar", Param)
2607 .Case("TypedefName", Type)
2608 .Case("ObjCIvar", ObjCIVar)
2609 .Case("ObjCProperty", ObjCProp)
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00002610 .Case("Record", GenericRecord)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002611 .Case("ObjCInterface", ObjCInterface)
Ted Kremenekd980da22013-12-10 19:43:42 +00002612 .Case("ObjCProtocol", ObjCProtocol)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002613 .Case("Block", Block)
2614 .Case("CXXRecord", Class)
2615 .Case("Namespace", Namespace)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002616 .Case("Field", Field)
2617 .Case("CXXMethod", CXXMethod)
Alexis Hunt724f14e2014-11-28 00:53:20 +00002618 .Case("Enum", Enum)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002619 .Default(0);
2620 if (!V) {
2621 // Something wasn't in our mapping, so be helpful and let the developer
2622 // know about it.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002623 PrintFatalError(R.getLoc(), "Unknown subject type: " + R.getName());
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002624 return "";
2625 }
2626
2627 SubMask |= V;
2628 }
2629
2630 switch (SubMask) {
2631 // For the simple cases where there's only a single entry in the mask, we
2632 // don't have to resort to bit fiddling.
2633 case Func: return "ExpectedFunction";
2634 case Var: return "ExpectedVariable";
2635 case Param: return "ExpectedParameter";
2636 case Class: return "ExpectedClass";
Alexis Hunt724f14e2014-11-28 00:53:20 +00002637 case Enum: return "ExpectedEnum";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002638 case CXXMethod:
2639 // FIXME: Currently, this maps to ExpectedMethod based on existing code,
2640 // but should map to something a bit more accurate at some point.
2641 case ObjCMethod: return "ExpectedMethod";
2642 case Type: return "ExpectedType";
2643 case ObjCInterface: return "ExpectedObjectiveCInterface";
Ted Kremenekd980da22013-12-10 19:43:42 +00002644 case ObjCProtocol: return "ExpectedObjectiveCProtocol";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002645
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00002646 // "GenericRecord" means struct, union or class; check the language options
2647 // and if not compiling for C++, strip off the class part. Note that this
2648 // relies on the fact that the context for this declares "Sema &S".
2649 case GenericRecord:
Aaron Ballman17046b82013-11-27 19:16:55 +00002650 return "(S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass : "
2651 "ExpectedStructOrUnion)";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002652 case Func | ObjCMethod | Block: return "ExpectedFunctionMethodOrBlock";
2653 case Func | ObjCMethod | Class: return "ExpectedFunctionMethodOrClass";
2654 case Func | Param:
2655 case Func | ObjCMethod | Param: return "ExpectedFunctionMethodOrParameter";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002656 case Func | ObjCMethod: return "ExpectedFunctionOrMethod";
2657 case Func | Var: return "ExpectedVariableOrFunction";
Aaron Ballman604dfec2013-12-02 17:07:07 +00002658
2659 // If not compiling for C++, the class portion does not apply.
2660 case Func | Var | Class:
2661 return "(S.getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass : "
2662 "ExpectedVariableOrFunction)";
2663
Saleem Abdulrasool511f2e52016-07-15 20:41:10 +00002664 case Func | Var | Class | ObjCInterface:
2665 return "(S.getLangOpts().CPlusPlus"
2666 " ? ((S.getLangOpts().ObjC1 || S.getLangOpts().ObjC2)"
2667 " ? ExpectedFunctionVariableClassOrObjCInterface"
2668 " : ExpectedFunctionVariableOrClass)"
2669 " : ((S.getLangOpts().ObjC1 || S.getLangOpts().ObjC2)"
2670 " ? ExpectedFunctionVariableOrObjCInterface"
2671 " : ExpectedVariableOrFunction))";
2672
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002673 case ObjCMethod | ObjCProp: return "ExpectedMethodOrProperty";
Aaron Ballman173361e2014-07-16 20:28:10 +00002674 case ObjCProtocol | ObjCInterface:
2675 return "ExpectedObjectiveCInterfaceOrProtocol";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002676 case Field | Var: return "ExpectedFieldOrGlobalVar";
2677 }
2678
2679 PrintFatalError(S.getLoc(),
2680 "Could not deduce diagnostic argument for Attr subjects");
2681
2682 return "";
2683}
2684
Aaron Ballman12b9f652014-01-16 13:55:42 +00002685static std::string GetSubjectWithSuffix(const Record *R) {
George Burgess IV1881a572016-12-01 00:13:18 +00002686 const std::string &B = R->getName();
Aaron Ballman12b9f652014-01-16 13:55:42 +00002687 if (B == "DeclBase")
2688 return "Decl";
2689 return B + "Decl";
2690}
Hans Wennborgdcfba332015-10-06 23:40:43 +00002691
Aaron Ballman80469032013-11-29 14:57:58 +00002692static std::string GenerateCustomAppertainsTo(const Record &Subject,
2693 raw_ostream &OS) {
Aaron Ballmana358c902013-12-02 14:58:17 +00002694 std::string FnName = "is" + Subject.getName();
2695
Aaron Ballman80469032013-11-29 14:57:58 +00002696 // If this code has already been generated, simply return the previous
2697 // instance of it.
2698 static std::set<std::string> CustomSubjectSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002699 auto I = CustomSubjectSet.find(FnName);
Aaron Ballman80469032013-11-29 14:57:58 +00002700 if (I != CustomSubjectSet.end())
2701 return *I;
2702
2703 Record *Base = Subject.getValueAsDef("Base");
2704
2705 // Not currently support custom subjects within custom subjects.
2706 if (Base->isSubClassOf("SubsetSubject")) {
2707 PrintFatalError(Subject.getLoc(),
2708 "SubsetSubjects within SubsetSubjects is not supported");
2709 return "";
2710 }
2711
Aaron Ballman80469032013-11-29 14:57:58 +00002712 OS << "static bool " << FnName << "(const Decl *D) {\n";
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002713 OS << " if (const auto *S = dyn_cast<";
Aaron Ballman12b9f652014-01-16 13:55:42 +00002714 OS << GetSubjectWithSuffix(Base);
Aaron Ballman47553042014-01-16 14:32:03 +00002715 OS << ">(D))\n";
2716 OS << " return " << Subject.getValueAsString("CheckCode") << ";\n";
2717 OS << " return false;\n";
Aaron Ballman80469032013-11-29 14:57:58 +00002718 OS << "}\n\n";
2719
2720 CustomSubjectSet.insert(FnName);
2721 return FnName;
2722}
2723
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002724static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) {
2725 // If the attribute does not contain a Subjects definition, then use the
2726 // default appertainsTo logic.
2727 if (Attr.isValueUnset("Subjects"))
Aaron Ballman93b5cc62013-12-02 19:36:42 +00002728 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002729
2730 const Record *SubjectObj = Attr.getValueAsDef("Subjects");
2731 std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
2732
2733 // If the list of subjects is empty, it is assumed that the attribute
2734 // appertains to everything.
2735 if (Subjects.empty())
Aaron Ballman93b5cc62013-12-02 19:36:42 +00002736 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002737
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002738 bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn");
2739
2740 // Otherwise, generate an appertainsTo check specific to this attribute which
2741 // checks all of the given subjects against the Decl passed in. Return the
2742 // name of that check to the caller.
Aaron Ballman00dcc432013-12-03 13:45:50 +00002743 std::string FnName = "check" + Attr.getName() + "AppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002744 std::stringstream SS;
2745 SS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr, ";
2746 SS << "const Decl *D) {\n";
2747 SS << " if (";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002748 for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) {
Aaron Ballman80469032013-11-29 14:57:58 +00002749 // If the subject has custom code associated with it, generate a function
2750 // for it. The function cannot be inlined into this check (yet) because it
2751 // requires the subject to be of a specific type, and were that information
2752 // inlined here, it would not support an attribute with multiple custom
2753 // subjects.
2754 if ((*I)->isSubClassOf("SubsetSubject")) {
2755 SS << "!" << GenerateCustomAppertainsTo(**I, OS) << "(D)";
2756 } else {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002757 SS << "!isa<" << GetSubjectWithSuffix(*I) << ">(D)";
Aaron Ballman80469032013-11-29 14:57:58 +00002758 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002759
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002760 if (I + 1 != E)
2761 SS << " && ";
2762 }
2763 SS << ") {\n";
2764 SS << " S.Diag(Attr.getLoc(), diag::";
2765 SS << (Warn ? "warn_attribute_wrong_decl_type" :
2766 "err_attribute_wrong_decl_type");
2767 SS << ")\n";
2768 SS << " << Attr.getName() << ";
2769 SS << CalculateDiagnostic(*SubjectObj) << ";\n";
2770 SS << " return false;\n";
2771 SS << " }\n";
2772 SS << " return true;\n";
2773 SS << "}\n\n";
2774
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002775 OS << SS.str();
2776 return FnName;
2777}
2778
Aaron Ballman3aff6332013-12-02 19:30:36 +00002779static void GenerateDefaultLangOptRequirements(raw_ostream &OS) {
2780 OS << "static bool defaultDiagnoseLangOpts(Sema &, ";
2781 OS << "const AttributeList &) {\n";
2782 OS << " return true;\n";
2783 OS << "}\n\n";
2784}
2785
2786static std::string GenerateLangOptRequirements(const Record &R,
2787 raw_ostream &OS) {
2788 // If the attribute has an empty or unset list of language requirements,
2789 // return the default handler.
2790 std::vector<Record *> LangOpts = R.getValueAsListOfDefs("LangOpts");
2791 if (LangOpts.empty())
2792 return "defaultDiagnoseLangOpts";
2793
2794 // Generate the test condition, as well as a unique function name for the
2795 // diagnostic test. The list of options should usually be short (one or two
2796 // options), and the uniqueness isn't strictly necessary (it is just for
2797 // codegen efficiency).
2798 std::string FnName = "check", Test;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002799 for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00002800 std::string Part = (*I)->getValueAsString("Name");
Eric Fiselier341e8252016-09-02 18:53:31 +00002801 if ((*I)->getValueAsBit("Negated")) {
2802 FnName += "Not";
Alexis Hunt724f14e2014-11-28 00:53:20 +00002803 Test += "!";
Eric Fiselier341e8252016-09-02 18:53:31 +00002804 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00002805 Test += "S.LangOpts." + Part;
2806 if (I + 1 != E)
2807 Test += " || ";
2808 FnName += Part;
2809 }
2810 FnName += "LangOpts";
2811
2812 // If this code has already been generated, simply return the previous
2813 // instance of it.
2814 static std::set<std::string> CustomLangOptsSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002815 auto I = CustomLangOptsSet.find(FnName);
Aaron Ballman3aff6332013-12-02 19:30:36 +00002816 if (I != CustomLangOptsSet.end())
2817 return *I;
2818
2819 OS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr) {\n";
2820 OS << " if (" << Test << ")\n";
2821 OS << " return true;\n\n";
2822 OS << " S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) ";
2823 OS << "<< Attr.getName();\n";
2824 OS << " return false;\n";
2825 OS << "}\n\n";
2826
2827 CustomLangOptsSet.insert(FnName);
2828 return FnName;
2829}
2830
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002831static void GenerateDefaultTargetRequirements(raw_ostream &OS) {
Bob Wilson7c730832015-07-20 22:57:31 +00002832 OS << "static bool defaultTargetRequirements(const TargetInfo &) {\n";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002833 OS << " return true;\n";
2834 OS << "}\n\n";
2835}
2836
2837static std::string GenerateTargetRequirements(const Record &Attr,
2838 const ParsedAttrMap &Dupes,
2839 raw_ostream &OS) {
2840 // If the attribute is not a target specific attribute, return the default
2841 // target handler.
2842 if (!Attr.isSubClassOf("TargetSpecificAttr"))
2843 return "defaultTargetRequirements";
2844
2845 // Get the list of architectures to be tested for.
2846 const Record *R = Attr.getValueAsDef("Target");
2847 std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches");
2848 if (Arches.empty()) {
2849 PrintError(Attr.getLoc(), "Empty list of target architectures for a "
2850 "target-specific attr");
2851 return "defaultTargetRequirements";
2852 }
2853
2854 // If there are other attributes which share the same parsed attribute kind,
2855 // such as target-specific attributes with a shared spelling, collapse the
2856 // duplicate architectures. This is required because a shared target-specific
2857 // attribute has only one AttributeList::Kind enumeration value, but it
2858 // applies to multiple target architectures. In order for the attribute to be
2859 // considered valid, all of its architectures need to be included.
2860 if (!Attr.isValueUnset("ParseKind")) {
2861 std::string APK = Attr.getValueAsString("ParseKind");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002862 for (const auto &I : Dupes) {
2863 if (I.first == APK) {
2864 std::vector<std::string> DA = I.second->getValueAsDef("Target")
2865 ->getValueAsListOfStrings("Arches");
George Burgess IV1881a572016-12-01 00:13:18 +00002866 std::move(DA.begin(), DA.end(), std::back_inserter(Arches));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002867 }
2868 }
2869 }
2870
Bob Wilson0058b822015-07-20 22:57:36 +00002871 std::string FnName = "isTarget";
2872 std::string Test;
2873 GenerateTargetSpecificAttrChecks(R, Arches, Test, &FnName);
Bob Wilson7c730832015-07-20 22:57:31 +00002874
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002875 // If this code has already been generated, simply return the previous
2876 // instance of it.
2877 static std::set<std::string> CustomTargetSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002878 auto I = CustomTargetSet.find(FnName);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002879 if (I != CustomTargetSet.end())
2880 return *I;
2881
Bob Wilson7c730832015-07-20 22:57:31 +00002882 OS << "static bool " << FnName << "(const TargetInfo &Target) {\n";
2883 OS << " const llvm::Triple &T = Target.getTriple();\n";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002884 OS << " return " << Test << ";\n";
2885 OS << "}\n\n";
2886
2887 CustomTargetSet.insert(FnName);
2888 return FnName;
2889}
2890
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002891static void GenerateDefaultSpellingIndexToSemanticSpelling(raw_ostream &OS) {
2892 OS << "static unsigned defaultSpellingIndexToSemanticSpelling("
2893 << "const AttributeList &Attr) {\n";
2894 OS << " return UINT_MAX;\n";
2895 OS << "}\n\n";
2896}
2897
2898static std::string GenerateSpellingIndexToSemanticSpelling(const Record &Attr,
2899 raw_ostream &OS) {
2900 // If the attribute does not have a semantic form, we can bail out early.
2901 if (!Attr.getValueAsBit("ASTNode"))
2902 return "defaultSpellingIndexToSemanticSpelling";
2903
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002904 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002905
2906 // If there are zero or one spellings, or all of the spellings share the same
2907 // name, we can also bail out early.
2908 if (Spellings.size() <= 1 || SpellingNamesAreCommon(Spellings))
2909 return "defaultSpellingIndexToSemanticSpelling";
2910
2911 // Generate the enumeration we will use for the mapping.
2912 SemanticSpellingMap SemanticToSyntacticMap;
2913 std::string Enum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
2914 std::string Name = Attr.getName() + "AttrSpellingMap";
2915
2916 OS << "static unsigned " << Name << "(const AttributeList &Attr) {\n";
2917 OS << Enum;
2918 OS << " unsigned Idx = Attr.getAttributeSpellingListIndex();\n";
2919 WriteSemanticSpellingSwitch("Idx", SemanticToSyntacticMap, OS);
2920 OS << "}\n\n";
2921
2922 return Name;
2923}
2924
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002925static bool IsKnownToGCC(const Record &Attr) {
2926 // Look at the spellings for this subject; if there are any spellings which
2927 // claim to be known to GCC, the attribute is known to GCC.
George Burgess IV1881a572016-12-01 00:13:18 +00002928 return llvm::any_of(
2929 GetFlattenedSpellings(Attr),
2930 [](const FlattenedSpelling &S) { return S.knownToGCC(); });
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00002931}
2932
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002933/// Emits the parsed attribute helpers
2934void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
2935 emitSourceFileHeader("Parsed attribute helpers", OS);
2936
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002937 // Get the list of parsed attributes, and accept the optional list of
2938 // duplicates due to the ParseKind.
2939 ParsedAttrMap Dupes;
2940 ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002941
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002942 // Generate the default appertainsTo, target and language option diagnostic,
2943 // and spelling list index mapping methods.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002944 GenerateDefaultAppertainsTo(OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00002945 GenerateDefaultLangOptRequirements(OS);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002946 GenerateDefaultTargetRequirements(OS);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002947 GenerateDefaultSpellingIndexToSemanticSpelling(OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002948
2949 // Generate the appertainsTo diagnostic methods and write their names into
2950 // another mapping. At the same time, generate the AttrInfoMap object
2951 // contents. Due to the reliance on generated code, use separate streams so
2952 // that code will not be interleaved.
2953 std::stringstream SS;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002954 for (auto I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002955 // TODO: If the attribute's kind appears in the list of duplicates, that is
2956 // because it is a target-specific attribute that appears multiple times.
2957 // It would be beneficial to test whether the duplicates are "similar
2958 // enough" to each other to not cause problems. For instance, check that
Alp Toker96cf7582014-01-18 21:49:37 +00002959 // the spellings are identical, and custom parsing rules match, etc.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002960
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002961 // We need to generate struct instances based off ParsedAttrInfo from
2962 // AttributeList.cpp.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002963 SS << " { ";
2964 emitArgInfo(*I->second, SS);
2965 SS << ", " << I->second->getValueAsBit("HasCustomParsing");
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002966 SS << ", " << I->second->isSubClassOf("TargetSpecificAttr");
2967 SS << ", " << I->second->isSubClassOf("TypeAttr");
Richard Smith4f902c72016-03-08 00:32:55 +00002968 SS << ", " << I->second->isSubClassOf("StmtAttr");
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002969 SS << ", " << IsKnownToGCC(*I->second);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002970 SS << ", " << GenerateAppertainsTo(*I->second, OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00002971 SS << ", " << GenerateLangOptRequirements(*I->second, OS);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002972 SS << ", " << GenerateTargetRequirements(*I->second, Dupes, OS);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002973 SS << ", " << GenerateSpellingIndexToSemanticSpelling(*I->second, OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002974 SS << " }";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002975
2976 if (I + 1 != E)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002977 SS << ",";
2978
2979 SS << " // AT_" << I->first << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002980 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002981
2982 OS << "static const ParsedAttrInfo AttrInfoMap[AttributeList::UnknownAttribute + 1] = {\n";
2983 OS << SS.str();
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002984 OS << "};\n\n";
Michael Han4a045172012-03-07 00:12:16 +00002985}
2986
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002987// Emits the kind list of parsed attributes
2988void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002989 emitSourceFileHeader("Attribute name matcher", OS);
2990
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002991 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Nico Weber20e08042016-09-03 02:55:10 +00002992 std::vector<StringMatcher::StringPair> GNU, Declspec, Microsoft, CXX11,
2993 Keywords, Pragma;
Aaron Ballman64e69862013-12-15 13:05:48 +00002994 std::set<std::string> Seen;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002995 for (const auto *A : Attrs) {
2996 const Record &Attr = *A;
Richard Smith852e9ce2013-11-27 01:46:48 +00002997
Michael Han4a045172012-03-07 00:12:16 +00002998 bool SemaHandler = Attr.getValueAsBit("SemaHandler");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00002999 bool Ignored = Attr.getValueAsBit("Ignored");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003000 if (SemaHandler || Ignored) {
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003001 // Attribute spellings can be shared between target-specific attributes,
3002 // and can be shared between syntaxes for the same attribute. For
3003 // instance, an attribute can be spelled GNU<"interrupt"> for an ARM-
3004 // specific attribute, or MSP430-specific attribute. Additionally, an
3005 // attribute can be spelled GNU<"dllexport"> and Declspec<"dllexport">
3006 // for the same semantic attribute. Ultimately, we need to map each of
3007 // these to a single AttributeList::Kind value, but the StringMatcher
3008 // class cannot handle duplicate match strings. So we generate a list of
3009 // string to match based on the syntax, and emit multiple string matchers
3010 // depending on the syntax used.
Aaron Ballman64e69862013-12-15 13:05:48 +00003011 std::string AttrName;
3012 if (Attr.isSubClassOf("TargetSpecificAttr") &&
3013 !Attr.isValueUnset("ParseKind")) {
3014 AttrName = Attr.getValueAsString("ParseKind");
3015 if (Seen.find(AttrName) != Seen.end())
3016 continue;
3017 Seen.insert(AttrName);
3018 } else
3019 AttrName = NormalizeAttrName(StringRef(Attr.getName())).str();
3020
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003021 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003022 for (const auto &S : Spellings) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00003023 const std::string &RawSpelling = S.name();
Craig Topper8ae12032014-05-07 06:21:57 +00003024 std::vector<StringMatcher::StringPair> *Matches = nullptr;
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00003025 std::string Spelling;
3026 const std::string &Variety = S.variety();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003027 if (Variety == "CXX11") {
3028 Matches = &CXX11;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003029 Spelling += S.nameSpace();
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003030 Spelling += "::";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003031 } else if (Variety == "GNU")
3032 Matches = &GNU;
3033 else if (Variety == "Declspec")
3034 Matches = &Declspec;
Nico Weber20e08042016-09-03 02:55:10 +00003035 else if (Variety == "Microsoft")
3036 Matches = &Microsoft;
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003037 else if (Variety == "Keyword")
3038 Matches = &Keywords;
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003039 else if (Variety == "Pragma")
3040 Matches = &Pragma;
Alexis Hunta0e54d42012-06-18 16:13:52 +00003041
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003042 assert(Matches && "Unsupported spelling variety found");
3043
3044 Spelling += NormalizeAttrSpelling(RawSpelling);
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003045 if (SemaHandler)
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003046 Matches->push_back(StringMatcher::StringPair(Spelling,
3047 "return AttributeList::AT_" + AttrName + ";"));
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003048 else
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003049 Matches->push_back(StringMatcher::StringPair(Spelling,
3050 "return AttributeList::IgnoredAttribute;"));
Michael Han4a045172012-03-07 00:12:16 +00003051 }
3052 }
3053 }
Douglas Gregor377f99b2012-05-02 17:33:51 +00003054
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003055 OS << "static AttributeList::Kind getAttrKind(StringRef Name, ";
3056 OS << "AttributeList::Syntax Syntax) {\n";
3057 OS << " if (AttributeList::AS_GNU == Syntax) {\n";
3058 StringMatcher("Name", GNU, OS).Emit();
3059 OS << " } else if (AttributeList::AS_Declspec == Syntax) {\n";
3060 StringMatcher("Name", Declspec, OS).Emit();
Nico Weber20e08042016-09-03 02:55:10 +00003061 OS << " } else if (AttributeList::AS_Microsoft == Syntax) {\n";
3062 StringMatcher("Name", Microsoft, OS).Emit();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003063 OS << " } else if (AttributeList::AS_CXX11 == Syntax) {\n";
3064 StringMatcher("Name", CXX11, OS).Emit();
Douglas Gregorbec595a2015-06-19 18:27:45 +00003065 OS << " } else if (AttributeList::AS_Keyword == Syntax || ";
3066 OS << "AttributeList::AS_ContextSensitiveKeyword == Syntax) {\n";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003067 StringMatcher("Name", Keywords, OS).Emit();
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003068 OS << " } else if (AttributeList::AS_Pragma == Syntax) {\n";
3069 StringMatcher("Name", Pragma, OS).Emit();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003070 OS << " }\n";
3071 OS << " return AttributeList::UnknownAttribute;\n"
Douglas Gregor377f99b2012-05-02 17:33:51 +00003072 << "}\n";
Michael Han4a045172012-03-07 00:12:16 +00003073}
3074
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003075// Emits the code to dump an attribute.
3076void EmitClangAttrDump(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00003077 emitSourceFileHeader("Attribute dumper", OS);
3078
John McCall2225c8b2016-03-01 00:18:05 +00003079 OS << " switch (A->getKind()) {\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003080 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003081 for (const auto *Attr : Attrs) {
3082 const Record &R = *Attr;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003083 if (!R.getValueAsBit("ASTNode"))
3084 continue;
3085 OS << " case attr::" << R.getName() << ": {\n";
Aaron Ballmanbc909612014-01-22 21:51:20 +00003086
3087 // If the attribute has a semantically-meaningful name (which is determined
3088 // by whether there is a Spelling enumeration for it), then write out the
3089 // spelling used for the attribute.
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003090 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballmanbc909612014-01-22 21:51:20 +00003091 if (Spellings.size() > 1 && !SpellingNamesAreCommon(Spellings))
3092 OS << " OS << \" \" << A->getSpelling();\n";
3093
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003094 Args = R.getValueAsListOfDefs("Args");
3095 if (!Args.empty()) {
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003096 OS << " const auto *SA = cast<" << R.getName()
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003097 << "Attr>(A);\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00003098 for (const auto *Arg : Args)
3099 createArgument(*Arg, R.getName())->writeDump(OS);
Richard Trieude5cc7d2013-01-31 01:44:26 +00003100
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003101 for (const auto *AI : Args)
3102 createArgument(*AI, R.getName())->writeDumpChildren(OS);
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003103 }
3104 OS <<
3105 " break;\n"
3106 " }\n";
3107 }
3108 OS << " }\n";
3109}
3110
Aaron Ballman35db2b32014-01-29 22:13:45 +00003111void EmitClangAttrParserStringSwitches(RecordKeeper &Records,
3112 raw_ostream &OS) {
3113 emitSourceFileHeader("Parser-related llvm::StringSwitch cases", OS);
3114 emitClangAttrArgContextList(Records, OS);
3115 emitClangAttrIdentifierArgList(Records, OS);
3116 emitClangAttrTypeArgList(Records, OS);
3117 emitClangAttrLateParsedList(Records, OS);
3118}
3119
Aaron Ballman97dba042014-02-17 15:27:10 +00003120class DocumentationData {
3121public:
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003122 const Record *Documentation;
3123 const Record *Attribute;
Aaron Ballman97dba042014-02-17 15:27:10 +00003124
Aaron Ballman4de1b582014-02-19 22:59:32 +00003125 DocumentationData(const Record &Documentation, const Record &Attribute)
3126 : Documentation(&Documentation), Attribute(&Attribute) {}
Aaron Ballman97dba042014-02-17 15:27:10 +00003127};
3128
Aaron Ballman4de1b582014-02-19 22:59:32 +00003129static void WriteCategoryHeader(const Record *DocCategory,
Aaron Ballman97dba042014-02-17 15:27:10 +00003130 raw_ostream &OS) {
Aaron Ballman4de1b582014-02-19 22:59:32 +00003131 const std::string &Name = DocCategory->getValueAsString("Name");
3132 OS << Name << "\n" << std::string(Name.length(), '=') << "\n";
3133
3134 // If there is content, print that as well.
3135 std::string ContentStr = DocCategory->getValueAsString("Content");
Benjamin Kramer5c404072015-04-10 21:37:21 +00003136 // Trim leading and trailing newlines and spaces.
3137 OS << StringRef(ContentStr).trim();
3138
Aaron Ballman4de1b582014-02-19 22:59:32 +00003139 OS << "\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003140}
3141
Aaron Ballmana66b5742014-02-17 15:36:08 +00003142enum SpellingKind {
3143 GNU = 1 << 0,
3144 CXX11 = 1 << 1,
3145 Declspec = 1 << 2,
Nico Weber20e08042016-09-03 02:55:10 +00003146 Microsoft = 1 << 3,
3147 Keyword = 1 << 4,
3148 Pragma = 1 << 5
Aaron Ballmana66b5742014-02-17 15:36:08 +00003149};
3150
Aaron Ballman97dba042014-02-17 15:27:10 +00003151static void WriteDocumentation(const DocumentationData &Doc,
3152 raw_ostream &OS) {
3153 // FIXME: there is no way to have a per-spelling category for the attribute
3154 // documentation. This may not be a limiting factor since the spellings
3155 // should generally be consistently applied across the category.
3156
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003157 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Doc.Attribute);
Aaron Ballman97dba042014-02-17 15:27:10 +00003158
3159 // Determine the heading to be used for this attribute.
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003160 std::string Heading = Doc.Documentation->getValueAsString("Heading");
Aaron Ballmanea6668c2014-02-21 14:14:04 +00003161 bool CustomHeading = !Heading.empty();
Aaron Ballman97dba042014-02-17 15:27:10 +00003162 if (Heading.empty()) {
3163 // If there's only one spelling, we can simply use that.
3164 if (Spellings.size() == 1)
3165 Heading = Spellings.begin()->name();
3166 else {
3167 std::set<std::string> Uniques;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003168 for (auto I = Spellings.begin(), E = Spellings.end();
3169 I != E && Uniques.size() <= 1; ++I) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003170 std::string Spelling = NormalizeNameForSpellingComparison(I->name());
3171 Uniques.insert(Spelling);
3172 }
3173 // If the semantic map has only one spelling, that is sufficient for our
3174 // needs.
3175 if (Uniques.size() == 1)
3176 Heading = *Uniques.begin();
3177 }
3178 }
3179
3180 // If the heading is still empty, it is an error.
3181 if (Heading.empty())
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003182 PrintFatalError(Doc.Attribute->getLoc(),
Aaron Ballman97dba042014-02-17 15:27:10 +00003183 "This attribute requires a heading to be specified");
3184
3185 // Gather a list of unique spellings; this is not the same as the semantic
3186 // spelling for the attribute. Variations in underscores and other non-
3187 // semantic characters are still acceptable.
3188 std::vector<std::string> Names;
3189
Aaron Ballman97dba042014-02-17 15:27:10 +00003190 unsigned SupportedSpellings = 0;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003191 for (const auto &I : Spellings) {
3192 SpellingKind Kind = StringSwitch<SpellingKind>(I.variety())
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003193 .Case("GNU", GNU)
3194 .Case("CXX11", CXX11)
3195 .Case("Declspec", Declspec)
Nico Weber20e08042016-09-03 02:55:10 +00003196 .Case("Microsoft", Microsoft)
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003197 .Case("Keyword", Keyword)
3198 .Case("Pragma", Pragma);
Aaron Ballman97dba042014-02-17 15:27:10 +00003199
3200 // Mask in the supported spelling.
3201 SupportedSpellings |= Kind;
3202
3203 std::string Name;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003204 if (Kind == CXX11 && !I.nameSpace().empty())
3205 Name = I.nameSpace() + "::";
3206 Name += I.name();
Aaron Ballman97dba042014-02-17 15:27:10 +00003207
3208 // If this name is the same as the heading, do not add it.
3209 if (Name != Heading)
3210 Names.push_back(Name);
3211 }
3212
3213 // Print out the heading for the attribute. If there are alternate spellings,
3214 // then display those after the heading.
Aaron Ballmanea6668c2014-02-21 14:14:04 +00003215 if (!CustomHeading && !Names.empty()) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003216 Heading += " (";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003217 for (auto I = Names.begin(), E = Names.end(); I != E; ++I) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003218 if (I != Names.begin())
3219 Heading += ", ";
3220 Heading += *I;
3221 }
3222 Heading += ")";
3223 }
3224 OS << Heading << "\n" << std::string(Heading.length(), '-') << "\n";
3225
3226 if (!SupportedSpellings)
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003227 PrintFatalError(Doc.Attribute->getLoc(),
Aaron Ballman97dba042014-02-17 15:27:10 +00003228 "Attribute has no supported spellings; cannot be "
3229 "documented");
3230
3231 // List what spelling syntaxes the attribute supports.
3232 OS << ".. csv-table:: Supported Syntaxes\n";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003233 OS << " :header: \"GNU\", \"C++11\", \"__declspec\", \"Keyword\",";
3234 OS << " \"Pragma\"\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003235 OS << " \"";
3236 if (SupportedSpellings & GNU) OS << "X";
3237 OS << "\",\"";
3238 if (SupportedSpellings & CXX11) OS << "X";
3239 OS << "\",\"";
3240 if (SupportedSpellings & Declspec) OS << "X";
3241 OS << "\",\"";
3242 if (SupportedSpellings & Keyword) OS << "X";
Aaron Ballman120c79f2014-06-25 12:48:06 +00003243 OS << "\", \"";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003244 if (SupportedSpellings & Pragma) OS << "X";
3245 OS << "\"\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003246
3247 // If the attribute is deprecated, print a message about it, and possibly
3248 // provide a replacement attribute.
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003249 if (!Doc.Documentation->isValueUnset("Deprecated")) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003250 OS << "This attribute has been deprecated, and may be removed in a future "
3251 << "version of Clang.";
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003252 const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated");
Aaron Ballman97dba042014-02-17 15:27:10 +00003253 std::string Replacement = Deprecated.getValueAsString("Replacement");
3254 if (!Replacement.empty())
3255 OS << " This attribute has been superseded by ``"
3256 << Replacement << "``.";
3257 OS << "\n\n";
3258 }
3259
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003260 std::string ContentStr = Doc.Documentation->getValueAsString("Content");
Aaron Ballman97dba042014-02-17 15:27:10 +00003261 // Trim leading and trailing newlines and spaces.
Benjamin Kramer5c404072015-04-10 21:37:21 +00003262 OS << StringRef(ContentStr).trim();
Aaron Ballman97dba042014-02-17 15:27:10 +00003263
3264 OS << "\n\n\n";
3265}
3266
3267void EmitClangAttrDocs(RecordKeeper &Records, raw_ostream &OS) {
3268 // Get the documentation introduction paragraph.
3269 const Record *Documentation = Records.getDef("GlobalDocumentation");
3270 if (!Documentation) {
3271 PrintFatalError("The Documentation top-level definition is missing, "
3272 "no documentation will be generated.");
3273 return;
3274 }
3275
Aaron Ballman4de1b582014-02-19 22:59:32 +00003276 OS << Documentation->getValueAsString("Intro") << "\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003277
Aaron Ballman97dba042014-02-17 15:27:10 +00003278 // Gather the Documentation lists from each of the attributes, based on the
3279 // category provided.
3280 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003281 std::map<const Record *, std::vector<DocumentationData>> SplitDocs;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003282 for (const auto *A : Attrs) {
3283 const Record &Attr = *A;
Aaron Ballman97dba042014-02-17 15:27:10 +00003284 std::vector<Record *> Docs = Attr.getValueAsListOfDefs("Documentation");
Aaron Ballman2f22b942014-05-20 19:47:14 +00003285 for (const auto *D : Docs) {
3286 const Record &Doc = *D;
Aaron Ballman4de1b582014-02-19 22:59:32 +00003287 const Record *Category = Doc.getValueAsDef("Category");
Aaron Ballman97dba042014-02-17 15:27:10 +00003288 // If the category is "undocumented", then there cannot be any other
3289 // documentation categories (otherwise, the attribute would become
3290 // documented).
Aaron Ballman4de1b582014-02-19 22:59:32 +00003291 std::string Cat = Category->getValueAsString("Name");
3292 bool Undocumented = Cat == "Undocumented";
Aaron Ballman97dba042014-02-17 15:27:10 +00003293 if (Undocumented && Docs.size() > 1)
3294 PrintFatalError(Doc.getLoc(),
3295 "Attribute is \"Undocumented\", but has multiple "
3296 "documentation categories");
3297
3298 if (!Undocumented)
Aaron Ballman4de1b582014-02-19 22:59:32 +00003299 SplitDocs[Category].push_back(DocumentationData(Doc, Attr));
Aaron Ballman97dba042014-02-17 15:27:10 +00003300 }
3301 }
3302
3303 // Having split the attributes out based on what documentation goes where,
3304 // we can begin to generate sections of documentation.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003305 for (const auto &I : SplitDocs) {
3306 WriteCategoryHeader(I.first, OS);
Aaron Ballman97dba042014-02-17 15:27:10 +00003307
3308 // Walk over each of the attributes in the category and write out their
3309 // documentation.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003310 for (const auto &Doc : I.second)
3311 WriteDocumentation(Doc, OS);
Aaron Ballman97dba042014-02-17 15:27:10 +00003312 }
3313}
3314
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00003315} // end namespace clang