blob: edd3e38471bee29e4ae88ecd2551dd5915bc4d12 [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"
Alex Lorenz9e7bf162017-04-18 14:33:39 +000015#include "llvm/ADT/DenseMap.h"
George Burgess IV1881a572016-12-01 00:13:18 +000016#include "llvm/ADT/DenseSet.h"
Alex Lorenz3bfe9622017-04-18 10:46:41 +000017#include "llvm/ADT/STLExtras.h"
Alex Lorenz9e7bf162017-04-18 14:33:39 +000018#include "llvm/ADT/SmallString.h"
Aaron Ballman28afa182014-11-17 18:17:19 +000019#include "llvm/ADT/StringExtras.h"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000020#include "llvm/ADT/StringRef.h"
Alex Lorenz9e7bf162017-04-18 14:33:39 +000021#include "llvm/ADT/StringSet.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000022#include "llvm/ADT/StringSwitch.h"
Alex Lorenz9e7bf162017-04-18 14:33:39 +000023#include "llvm/ADT/iterator_range.h"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000024#include "llvm/Support/ErrorHandling.h"
25#include "llvm/Support/raw_ostream.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000026#include "llvm/TableGen/Error.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000027#include "llvm/TableGen/Record.h"
Douglas Gregor377f99b2012-05-02 17:33:51 +000028#include "llvm/TableGen/StringMatcher.h"
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000029#include "llvm/TableGen/TableGenBackend.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000030#include <algorithm>
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000031#include <cassert>
Peter Collingbournebee583f2011-10-06 13:03:08 +000032#include <cctype>
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000033#include <cstddef>
34#include <cstdint>
35#include <map>
Aaron Ballman8f1439b2014-03-05 16:49:55 +000036#include <memory>
Aaron Ballman80469032013-11-29 14:57:58 +000037#include <set>
Chandler Carruth5553d0d2014-01-07 11:51:46 +000038#include <sstream>
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000039#include <string>
40#include <utility>
41#include <vector>
Peter Collingbournebee583f2011-10-06 13:03:08 +000042
43using namespace llvm;
44
Benjamin Kramerd910d162015-03-10 18:24:01 +000045namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000046
Aaron Ballmanc669cc02014-01-27 22:10:04 +000047class FlattenedSpelling {
48 std::string V, N, NS;
49 bool K;
50
51public:
52 FlattenedSpelling(const std::string &Variety, const std::string &Name,
53 const std::string &Namespace, bool KnownToGCC) :
54 V(Variety), N(Name), NS(Namespace), K(KnownToGCC) {}
55 explicit FlattenedSpelling(const Record &Spelling) :
56 V(Spelling.getValueAsString("Variety")),
57 N(Spelling.getValueAsString("Name")) {
58
59 assert(V != "GCC" && "Given a GCC spelling, which means this hasn't been"
60 "flattened!");
Tyler Nowickie8b07ed2014-06-13 17:57:25 +000061 if (V == "CXX11" || V == "Pragma")
Aaron Ballmanc669cc02014-01-27 22:10:04 +000062 NS = Spelling.getValueAsString("Namespace");
63 bool Unset;
64 K = Spelling.getValueAsBitOrUnset("KnownToGCC", Unset);
65 }
66
67 const std::string &variety() const { return V; }
68 const std::string &name() const { return N; }
69 const std::string &nameSpace() const { return NS; }
70 bool knownToGCC() const { return K; }
71};
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000072
Hans Wennborgdcfba332015-10-06 23:40:43 +000073} // end anonymous namespace
Aaron Ballmanc669cc02014-01-27 22:10:04 +000074
Benjamin Kramerd910d162015-03-10 18:24:01 +000075static std::vector<FlattenedSpelling>
76GetFlattenedSpellings(const Record &Attr) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +000077 std::vector<Record *> Spellings = Attr.getValueAsListOfDefs("Spellings");
78 std::vector<FlattenedSpelling> Ret;
79
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +000080 for (const auto &Spelling : Spellings) {
81 if (Spelling->getValueAsString("Variety") == "GCC") {
Aaron Ballmanc669cc02014-01-27 22:10:04 +000082 // Gin up two new spelling objects to add into the list.
Benjamin Kramer3204b152015-05-29 19:42:19 +000083 Ret.emplace_back("GNU", Spelling->getValueAsString("Name"), "", true);
84 Ret.emplace_back("CXX11", Spelling->getValueAsString("Name"), "gnu",
85 true);
Aaron Ballmanc669cc02014-01-27 22:10:04 +000086 } else
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +000087 Ret.push_back(FlattenedSpelling(*Spelling));
Aaron Ballmanc669cc02014-01-27 22:10:04 +000088 }
89
90 return Ret;
91}
92
Peter Collingbournebee583f2011-10-06 13:03:08 +000093static std::string ReadPCHRecord(StringRef type) {
94 return StringSwitch<std::string>(type)
David L. Jones267b8842017-01-24 01:04:30 +000095 .EndsWith("Decl *", "Record.GetLocalDeclAs<"
96 + std::string(type, 0, type.size()-1) + ">(Record.readInt())")
97 .Case("TypeSourceInfo *", "Record.getTypeSourceInfo()")
98 .Case("Expr *", "Record.readExpr()")
99 .Case("IdentifierInfo *", "Record.getIdentifierInfo()")
100 .Case("StringRef", "Record.readString()")
101 .Default("Record.readInt()");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000102}
103
Richard Smith19978562016-05-18 00:16:51 +0000104// Get a type that is suitable for storing an object of the specified type.
105static StringRef getStorageType(StringRef type) {
106 return StringSwitch<StringRef>(type)
107 .Case("StringRef", "std::string")
108 .Default(type);
109}
110
Peter Collingbournebee583f2011-10-06 13:03:08 +0000111// Assumes that the way to get the value is SA->getname()
112static std::string WritePCHRecord(StringRef type, StringRef name) {
Richard Smith290d8012016-04-06 17:06:00 +0000113 return "Record." + StringSwitch<std::string>(type)
114 .EndsWith("Decl *", "AddDeclRef(" + std::string(name) + ");\n")
115 .Case("TypeSourceInfo *", "AddTypeSourceInfo(" + std::string(name) + ");\n")
Peter Collingbournebee583f2011-10-06 13:03:08 +0000116 .Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
Richard Smith290d8012016-04-06 17:06:00 +0000117 .Case("IdentifierInfo *", "AddIdentifierRef(" + std::string(name) + ");\n")
118 .Case("StringRef", "AddString(" + std::string(name) + ");\n")
119 .Default("push_back(" + std::string(name) + ");\n");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000120}
121
Michael Han4a045172012-03-07 00:12:16 +0000122// Normalize attribute name by removing leading and trailing
123// underscores. For example, __foo, foo__, __foo__ would
124// become foo.
125static StringRef NormalizeAttrName(StringRef AttrName) {
George Burgess IV1881a572016-12-01 00:13:18 +0000126 AttrName.consume_front("__");
127 AttrName.consume_back("__");
Michael Han4a045172012-03-07 00:12:16 +0000128 return AttrName;
129}
130
Aaron Ballman36a53502014-01-16 13:03:14 +0000131// Normalize the name by removing any and all leading and trailing underscores.
132// This is different from NormalizeAttrName in that it also handles names like
133// _pascal and __pascal.
134static StringRef NormalizeNameForSpellingComparison(StringRef Name) {
Benjamin Kramer5c404072015-04-10 21:37:21 +0000135 return Name.trim("_");
Aaron Ballman36a53502014-01-16 13:03:14 +0000136}
137
Justin Lebar4086fe52017-01-05 16:51:54 +0000138// Normalize the spelling of a GNU attribute (i.e. "x" in "__attribute__((x))"),
139// removing "__" if it appears at the beginning and end of the attribute's name.
140static StringRef NormalizeGNUAttrSpelling(StringRef AttrSpelling) {
Michael Han4a045172012-03-07 00:12:16 +0000141 if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) {
142 AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4);
143 }
144
145 return AttrSpelling;
146}
147
Aaron Ballman2f22b942014-05-20 19:47:14 +0000148typedef std::vector<std::pair<std::string, const Record *>> ParsedAttrMap;
Aaron Ballman64e69862013-12-15 13:05:48 +0000149
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000150static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,
Craig Topper8ae12032014-05-07 06:21:57 +0000151 ParsedAttrMap *Dupes = nullptr) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000152 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Aaron Ballman64e69862013-12-15 13:05:48 +0000153 std::set<std::string> Seen;
154 ParsedAttrMap R;
Aaron Ballman2f22b942014-05-20 19:47:14 +0000155 for (const auto *Attr : Attrs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000156 if (Attr->getValueAsBit("SemaHandler")) {
Aaron Ballman64e69862013-12-15 13:05:48 +0000157 std::string AN;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000158 if (Attr->isSubClassOf("TargetSpecificAttr") &&
159 !Attr->isValueUnset("ParseKind")) {
160 AN = Attr->getValueAsString("ParseKind");
Aaron Ballman64e69862013-12-15 13:05:48 +0000161
162 // If this attribute has already been handled, it does not need to be
163 // handled again.
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000164 if (Seen.find(AN) != Seen.end()) {
165 if (Dupes)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000166 Dupes->push_back(std::make_pair(AN, Attr));
Aaron Ballman64e69862013-12-15 13:05:48 +0000167 continue;
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000168 }
Aaron Ballman64e69862013-12-15 13:05:48 +0000169 Seen.insert(AN);
170 } else
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000171 AN = NormalizeAttrName(Attr->getName()).str();
Aaron Ballman64e69862013-12-15 13:05:48 +0000172
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000173 R.push_back(std::make_pair(AN, Attr));
Aaron Ballman64e69862013-12-15 13:05:48 +0000174 }
175 }
176 return R;
177}
178
Peter Collingbournebee583f2011-10-06 13:03:08 +0000179namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000180
Peter Collingbournebee583f2011-10-06 13:03:08 +0000181 class Argument {
182 std::string lowerName, upperName;
183 StringRef attrName;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000184 bool isOpt;
John McCalla62c1a92015-10-28 00:17:34 +0000185 bool Fake;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000186
187 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000188 Argument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000189 : lowerName(Arg.getValueAsString("Name")), upperName(lowerName),
John McCalla62c1a92015-10-28 00:17:34 +0000190 attrName(Attr), isOpt(false), Fake(false) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000191 if (!lowerName.empty()) {
192 lowerName[0] = std::tolower(lowerName[0]);
193 upperName[0] = std::toupper(upperName[0]);
194 }
Reid Klecknerebeb0ca2016-05-31 17:42:56 +0000195 // Work around MinGW's macro definition of 'interface' to 'struct'. We
196 // have an attribute argument called 'Interface', so only the lower case
197 // name conflicts with the macro definition.
198 if (lowerName == "interface")
199 lowerName = "interface_";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000200 }
Hans Wennborgdcfba332015-10-06 23:40:43 +0000201 virtual ~Argument() = default;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000202
203 StringRef getLowerName() const { return lowerName; }
204 StringRef getUpperName() const { return upperName; }
205 StringRef getAttrName() const { return attrName; }
206
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000207 bool isOptional() const { return isOpt; }
208 void setOptional(bool set) { isOpt = set; }
209
John McCalla62c1a92015-10-28 00:17:34 +0000210 bool isFake() const { return Fake; }
211 void setFake(bool fake) { Fake = fake; }
212
Peter Collingbournebee583f2011-10-06 13:03:08 +0000213 // These functions print the argument contents formatted in different ways.
214 virtual void writeAccessors(raw_ostream &OS) const = 0;
215 virtual void writeAccessorDefinitions(raw_ostream &OS) const {}
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +0000216 virtual void writeASTVisitorTraversal(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000217 virtual void writeCloneArgs(raw_ostream &OS) const = 0;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000218 virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0;
Daniel Dunbardc51baa2012-02-10 06:00:29 +0000219 virtual void writeTemplateInstantiation(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000220 virtual void writeCtorBody(raw_ostream &OS) const {}
221 virtual void writeCtorInitializers(raw_ostream &OS) const = 0;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000222 virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000223 virtual void writeCtorParameters(raw_ostream &OS) const = 0;
224 virtual void writeDeclarations(raw_ostream &OS) const = 0;
225 virtual void writePCHReadArgs(raw_ostream &OS) const = 0;
226 virtual void writePCHReadDecls(raw_ostream &OS) const = 0;
227 virtual void writePCHWrite(raw_ostream &OS) const = 0;
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000228 virtual void writeValue(raw_ostream &OS) const = 0;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000229 virtual void writeDump(raw_ostream &OS) const = 0;
230 virtual void writeDumpChildren(raw_ostream &OS) const {}
Richard Trieude5cc7d2013-01-31 01:44:26 +0000231 virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000232
233 virtual bool isEnumArg() const { return false; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000234 virtual bool isVariadicEnumArg() const { return false; }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000235 virtual bool isVariadic() const { return false; }
Aaron Ballman36a53502014-01-16 13:03:14 +0000236
237 virtual void writeImplicitCtorArgs(raw_ostream &OS) const {
238 OS << getUpperName();
239 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000240 };
241
242 class SimpleArgument : public Argument {
243 std::string type;
244
245 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000246 SimpleArgument(const Record &Arg, StringRef Attr, std::string T)
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000247 : Argument(Arg, Attr), type(std::move(T)) {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000248
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000249 std::string getType() const { return type; }
250
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000251 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000252 OS << " " << type << " get" << getUpperName() << "() const {\n";
253 OS << " return " << getLowerName() << ";\n";
254 OS << " }";
255 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000256
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000257 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000258 OS << getLowerName();
259 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000260
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000261 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000262 OS << "A->get" << getUpperName() << "()";
263 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000264
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000265 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000266 OS << getLowerName() << "(" << getUpperName() << ")";
267 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000268
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000269 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000270 OS << getLowerName() << "()";
271 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000272
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000273 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000274 OS << type << " " << getUpperName();
275 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000276
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000277 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000278 OS << type << " " << getLowerName() << ";";
279 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000280
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000281 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000282 std::string read = ReadPCHRecord(type);
283 OS << " " << type << " " << getLowerName() << " = " << read << ";\n";
284 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000285
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000286 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000287 OS << getLowerName();
288 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000289
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000290 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000291 OS << " " << WritePCHRecord(type, "SA->get" +
292 std::string(getUpperName()) + "()");
293 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000294
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000295 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000296 if (type == "FunctionDecl *") {
Richard Smithb87c4652013-10-31 21:23:20 +0000297 OS << "\" << get" << getUpperName()
298 << "()->getNameInfo().getAsString() << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000299 } else if (type == "IdentifierInfo *") {
Akira Hatanaka3d173132016-09-10 03:29:43 +0000300 OS << "\";\n";
301 if (isOptional())
302 OS << " if (get" << getUpperName() << "()) ";
303 else
304 OS << " ";
305 OS << "OS << get" << getUpperName() << "()->getName();\n";
306 OS << " OS << \"";
Richard Smithb87c4652013-10-31 21:23:20 +0000307 } else if (type == "TypeSourceInfo *") {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000308 OS << "\" << get" << getUpperName() << "().getAsString() << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000309 } else {
310 OS << "\" << get" << getUpperName() << "() << \"";
311 }
312 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000313
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000314 void writeDump(raw_ostream &OS) const override {
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +0000315 if (type == "FunctionDecl *" || type == "NamedDecl *") {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000316 OS << " OS << \" \";\n";
317 OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n";
318 } else if (type == "IdentifierInfo *") {
Aaron Ballman415c4142015-11-30 15:25:34 +0000319 if (isOptional())
320 OS << " if (SA->get" << getUpperName() << "())\n ";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000321 OS << " OS << \" \" << SA->get" << getUpperName()
322 << "()->getName();\n";
Richard Smithb87c4652013-10-31 21:23:20 +0000323 } else if (type == "TypeSourceInfo *") {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000324 OS << " OS << \" \" << SA->get" << getUpperName()
325 << "().getAsString();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000326 } else if (type == "bool") {
327 OS << " if (SA->get" << getUpperName() << "()) OS << \" "
328 << getUpperName() << "\";\n";
329 } else if (type == "int" || type == "unsigned") {
330 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
331 } else {
332 llvm_unreachable("Unknown SimpleArgument type!");
333 }
334 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000335 };
336
Aaron Ballman18a78382013-11-21 00:28:23 +0000337 class DefaultSimpleArgument : public SimpleArgument {
338 int64_t Default;
339
340 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000341 DefaultSimpleArgument(const Record &Arg, StringRef Attr,
Aaron Ballman18a78382013-11-21 00:28:23 +0000342 std::string T, int64_t Default)
343 : SimpleArgument(Arg, Attr, T), Default(Default) {}
344
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000345 void writeAccessors(raw_ostream &OS) const override {
Aaron Ballman18a78382013-11-21 00:28:23 +0000346 SimpleArgument::writeAccessors(OS);
347
348 OS << "\n\n static const " << getType() << " Default" << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000349 << " = ";
350 if (getType() == "bool")
351 OS << (Default != 0 ? "true" : "false");
352 else
353 OS << Default;
354 OS << ";";
Aaron Ballman18a78382013-11-21 00:28:23 +0000355 }
356 };
357
Peter Collingbournebee583f2011-10-06 13:03:08 +0000358 class StringArgument : public Argument {
359 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000360 StringArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000361 : Argument(Arg, Attr)
362 {}
363
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000364 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000365 OS << " llvm::StringRef get" << getUpperName() << "() const {\n";
366 OS << " return llvm::StringRef(" << getLowerName() << ", "
367 << getLowerName() << "Length);\n";
368 OS << " }\n";
369 OS << " unsigned get" << getUpperName() << "Length() const {\n";
370 OS << " return " << getLowerName() << "Length;\n";
371 OS << " }\n";
372 OS << " void set" << getUpperName()
373 << "(ASTContext &C, llvm::StringRef S) {\n";
374 OS << " " << getLowerName() << "Length = S.size();\n";
375 OS << " this->" << getLowerName() << " = new (C, 1) char ["
376 << getLowerName() << "Length];\n";
Chandler Carruth38a45cc2015-08-04 03:53:01 +0000377 OS << " if (!S.empty())\n";
378 OS << " std::memcpy(this->" << getLowerName() << ", S.data(), "
Peter Collingbournebee583f2011-10-06 13:03:08 +0000379 << getLowerName() << "Length);\n";
380 OS << " }";
381 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000382
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000383 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000384 OS << "get" << getUpperName() << "()";
385 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000386
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000387 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000388 OS << "A->get" << getUpperName() << "()";
389 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000390
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000391 void writeCtorBody(raw_ostream &OS) const override {
Chandler Carruth38a45cc2015-08-04 03:53:01 +0000392 OS << " if (!" << getUpperName() << ".empty())\n";
393 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000394 << ".data(), " << getLowerName() << "Length);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000395 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000396
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000397 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000398 OS << getLowerName() << "Length(" << getUpperName() << ".size()),"
399 << getLowerName() << "(new (Ctx, 1) char[" << getLowerName()
400 << "Length])";
401 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000402
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000403 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Hans Wennborg59dbe862015-09-29 20:56:43 +0000404 OS << getLowerName() << "Length(0)," << getLowerName() << "(nullptr)";
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000405 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000406
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000407 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000408 OS << "llvm::StringRef " << getUpperName();
409 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000410
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000411 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000412 OS << "unsigned " << getLowerName() << "Length;\n";
413 OS << "char *" << getLowerName() << ";";
414 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000415
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000416 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000417 OS << " std::string " << getLowerName()
David L. Jones267b8842017-01-24 01:04:30 +0000418 << "= Record.readString();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000419 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000420
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000421 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000422 OS << getLowerName();
423 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000424
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000425 void writePCHWrite(raw_ostream &OS) const override {
Richard Smith290d8012016-04-06 17:06:00 +0000426 OS << " Record.AddString(SA->get" << getUpperName() << "());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000427 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000428
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000429 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000430 OS << "\\\"\" << get" << getUpperName() << "() << \"\\\"";
431 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000432
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000433 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000434 OS << " OS << \" \\\"\" << SA->get" << getUpperName()
435 << "() << \"\\\"\";\n";
436 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000437 };
438
439 class AlignedArgument : public Argument {
440 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000441 AlignedArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000442 : Argument(Arg, Attr)
443 {}
444
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000445 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000446 OS << " bool is" << getUpperName() << "Dependent() const;\n";
447
448 OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n";
449
450 OS << " bool is" << getUpperName() << "Expr() const {\n";
451 OS << " return is" << getLowerName() << "Expr;\n";
452 OS << " }\n";
453
454 OS << " Expr *get" << getUpperName() << "Expr() const {\n";
455 OS << " assert(is" << getLowerName() << "Expr);\n";
456 OS << " return " << getLowerName() << "Expr;\n";
457 OS << " }\n";
458
459 OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n";
460 OS << " assert(!is" << getLowerName() << "Expr);\n";
461 OS << " return " << getLowerName() << "Type;\n";
462 OS << " }";
463 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000464
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000465 void writeAccessorDefinitions(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000466 OS << "bool " << getAttrName() << "Attr::is" << getUpperName()
467 << "Dependent() const {\n";
468 OS << " if (is" << getLowerName() << "Expr)\n";
469 OS << " return " << getLowerName() << "Expr && (" << getLowerName()
470 << "Expr->isValueDependent() || " << getLowerName()
471 << "Expr->isTypeDependent());\n";
472 OS << " else\n";
473 OS << " return " << getLowerName()
474 << "Type->getType()->isDependentType();\n";
475 OS << "}\n";
476
477 // FIXME: Do not do the calculation here
478 // FIXME: Handle types correctly
479 // A null pointer means maximum alignment
Peter Collingbournebee583f2011-10-06 13:03:08 +0000480 OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
481 << "(ASTContext &Ctx) const {\n";
482 OS << " assert(!is" << getUpperName() << "Dependent());\n";
483 OS << " if (is" << getLowerName() << "Expr)\n";
Ulrich Weigandca3cb7f2015-04-21 17:29:35 +0000484 OS << " return " << getLowerName() << "Expr ? " << getLowerName()
485 << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue()"
486 << " * Ctx.getCharWidth() : "
487 << "Ctx.getTargetDefaultAlignForAttributeAligned();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000488 OS << " else\n";
489 OS << " return 0; // FIXME\n";
490 OS << "}\n";
491 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000492
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000493 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000494 OS << "is" << getLowerName() << "Expr, is" << getLowerName()
495 << "Expr ? static_cast<void*>(" << getLowerName()
496 << "Expr) : " << getLowerName()
497 << "Type";
498 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000499
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000500 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000501 // FIXME: move the definition in Sema::InstantiateAttrs to here.
502 // In the meantime, aligned attributes are cloned.
503 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000504
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000505 void writeCtorBody(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000506 OS << " if (is" << getLowerName() << "Expr)\n";
507 OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>("
508 << getUpperName() << ");\n";
509 OS << " else\n";
510 OS << " " << getLowerName()
511 << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000512 << ");\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000513 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000514
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000515 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000516 OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)";
517 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000518
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000519 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000520 OS << "is" << getLowerName() << "Expr(false)";
521 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000522
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000523 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000524 OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName();
525 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000526
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000527 void writeImplicitCtorArgs(raw_ostream &OS) const override {
Aaron Ballman36a53502014-01-16 13:03:14 +0000528 OS << "Is" << getUpperName() << "Expr, " << getUpperName();
529 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000530
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000531 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000532 OS << "bool is" << getLowerName() << "Expr;\n";
533 OS << "union {\n";
534 OS << "Expr *" << getLowerName() << "Expr;\n";
535 OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
536 OS << "};";
537 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000538
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000539 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000540 OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr";
541 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000542
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000543 void writePCHReadDecls(raw_ostream &OS) const override {
David L. Jones267b8842017-01-24 01:04:30 +0000544 OS << " bool is" << getLowerName() << "Expr = Record.readInt();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000545 OS << " void *" << getLowerName() << "Ptr;\n";
546 OS << " if (is" << getLowerName() << "Expr)\n";
David L. Jones267b8842017-01-24 01:04:30 +0000547 OS << " " << getLowerName() << "Ptr = Record.readExpr();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000548 OS << " else\n";
549 OS << " " << getLowerName()
David L. Jones267b8842017-01-24 01:04:30 +0000550 << "Ptr = Record.getTypeSourceInfo();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000551 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000552
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000553 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000554 OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n";
555 OS << " if (SA->is" << getUpperName() << "Expr())\n";
Richard Smith290d8012016-04-06 17:06:00 +0000556 OS << " Record.AddStmt(SA->get" << getUpperName() << "Expr());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000557 OS << " else\n";
Richard Smith290d8012016-04-06 17:06:00 +0000558 OS << " Record.AddTypeSourceInfo(SA->get" << getUpperName()
559 << "Type());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000560 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000561
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000562 void writeValue(raw_ostream &OS) const override {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000563 OS << "\";\n";
Aaron Ballmanc960f562014-08-01 13:49:00 +0000564 // The aligned attribute argument expression is optional.
565 OS << " if (is" << getLowerName() << "Expr && "
566 << getLowerName() << "Expr)\n";
Hans Wennborg59dbe862015-09-29 20:56:43 +0000567 OS << " " << getLowerName() << "Expr->printPretty(OS, nullptr, Policy);\n";
Richard Trieuddd01ce2014-06-09 22:53:25 +0000568 OS << " OS << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000569 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000570
571 void writeDump(raw_ostream &OS) const override {}
572
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000573 void writeDumpChildren(raw_ostream &OS) const override {
Richard Smithf7514452014-10-30 21:02:37 +0000574 OS << " if (SA->is" << getUpperName() << "Expr())\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000575 OS << " dumpStmt(SA->get" << getUpperName() << "Expr());\n";
Richard Smithf7514452014-10-30 21:02:37 +0000576 OS << " else\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000577 OS << " dumpType(SA->get" << getUpperName()
578 << "Type()->getType());\n";
579 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000580
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000581 void writeHasChildren(raw_ostream &OS) const override {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000582 OS << "SA->is" << getUpperName() << "Expr()";
583 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000584 };
585
586 class VariadicArgument : public Argument {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000587 std::string Type, ArgName, ArgSizeName, RangeName;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000588
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000589 protected:
590 // Assumed to receive a parameter: raw_ostream OS.
591 virtual void writeValueImpl(raw_ostream &OS) const {
592 OS << " OS << Val;\n";
593 }
594
Peter Collingbournebee583f2011-10-06 13:03:08 +0000595 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000596 VariadicArgument(const Record &Arg, StringRef Attr, std::string T)
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000597 : Argument(Arg, Attr), Type(std::move(T)),
598 ArgName(getLowerName().str() + "_"), ArgSizeName(ArgName + "Size"),
599 RangeName(getLowerName()) {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000600
Benjamin Kramer1b582012016-02-13 18:11:49 +0000601 const std::string &getType() const { return Type; }
602 const std::string &getArgName() const { return ArgName; }
603 const std::string &getArgSizeName() const { return ArgSizeName; }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000604 bool isVariadic() const override { return true; }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000605
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000606 void writeAccessors(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000607 std::string IteratorType = getLowerName().str() + "_iterator";
608 std::string BeginFn = getLowerName().str() + "_begin()";
609 std::string EndFn = getLowerName().str() + "_end()";
610
611 OS << " typedef " << Type << "* " << IteratorType << ";\n";
612 OS << " " << IteratorType << " " << BeginFn << " const {"
613 << " return " << ArgName << "; }\n";
614 OS << " " << IteratorType << " " << EndFn << " const {"
615 << " return " << ArgName << " + " << ArgSizeName << "; }\n";
616 OS << " unsigned " << getLowerName() << "_size() const {"
617 << " return " << ArgSizeName << "; }\n";
618 OS << " llvm::iterator_range<" << IteratorType << "> " << RangeName
619 << "() const { return llvm::make_range(" << BeginFn << ", " << EndFn
620 << "); }\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000621 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000622
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000623 void writeCloneArgs(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000624 OS << ArgName << ", " << ArgSizeName;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000625 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000626
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000627 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000628 // This isn't elegant, but we have to go through public methods...
629 OS << "A->" << getLowerName() << "_begin(), "
630 << "A->" << getLowerName() << "_size()";
631 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000632
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000633 void writeCtorBody(raw_ostream &OS) const override {
Aaron Ballmand6459e52014-05-01 15:21:03 +0000634 OS << " std::copy(" << getUpperName() << ", " << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000635 << " + " << ArgSizeName << ", " << ArgName << ");\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000636 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000637
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000638 void writeCtorInitializers(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000639 OS << ArgSizeName << "(" << getUpperName() << "Size), "
640 << ArgName << "(new (Ctx, 16) " << getType() << "["
641 << ArgSizeName << "])";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000642 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000643
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000644 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000645 OS << ArgSizeName << "(0), " << ArgName << "(nullptr)";
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000646 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000647
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000648 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000649 OS << getType() << " *" << getUpperName() << ", unsigned "
650 << getUpperName() << "Size";
651 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000652
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000653 void writeImplicitCtorArgs(raw_ostream &OS) const override {
Aaron Ballman36a53502014-01-16 13:03:14 +0000654 OS << getUpperName() << ", " << getUpperName() << "Size";
655 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000656
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000657 void writeDeclarations(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000658 OS << " unsigned " << ArgSizeName << ";\n";
659 OS << " " << getType() << " *" << ArgName << ";";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000660 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000661
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000662 void writePCHReadDecls(raw_ostream &OS) const override {
David L. Jones267b8842017-01-24 01:04:30 +0000663 OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n";
Richard Smith19978562016-05-18 00:16:51 +0000664 OS << " SmallVector<" << getType() << ", 4> "
665 << getLowerName() << ";\n";
666 OS << " " << getLowerName() << ".reserve(" << getLowerName()
Peter Collingbournebee583f2011-10-06 13:03:08 +0000667 << "Size);\n";
Richard Smith19978562016-05-18 00:16:51 +0000668
669 // If we can't store the values in the current type (if it's something
670 // like StringRef), store them in a different type and convert the
671 // container afterwards.
672 std::string StorageType = getStorageType(getType());
673 std::string StorageName = getLowerName();
674 if (StorageType != getType()) {
675 StorageName += "Storage";
676 OS << " SmallVector<" << StorageType << ", 4> "
677 << StorageName << ";\n";
678 OS << " " << StorageName << ".reserve(" << getLowerName()
679 << "Size);\n";
680 }
681
682 OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n";
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000683 std::string read = ReadPCHRecord(Type);
Richard Smith19978562016-05-18 00:16:51 +0000684 OS << " " << StorageName << ".push_back(" << read << ");\n";
685
686 if (StorageType != getType()) {
687 OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n";
688 OS << " " << getLowerName() << ".push_back("
689 << StorageName << "[i]);\n";
690 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000691 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000692
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000693 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000694 OS << getLowerName() << ".data(), " << getLowerName() << "Size";
695 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000696
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000697 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000698 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000699 OS << " for (auto &Val : SA->" << RangeName << "())\n";
700 OS << " " << WritePCHRecord(Type, "Val");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000701 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000702
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000703 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000704 OS << "\";\n";
705 OS << " bool isFirst = true;\n"
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000706 << " for (const auto &Val : " << RangeName << "()) {\n"
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000707 << " if (isFirst) isFirst = false;\n"
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000708 << " else OS << \", \";\n";
709 writeValueImpl(OS);
710 OS << " }\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000711 OS << " OS << \"";
712 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000713
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000714 void writeDump(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000715 OS << " for (const auto &Val : SA->" << RangeName << "())\n";
716 OS << " OS << \" \" << Val;\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000717 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000718 };
719
Reid Klecknerf526b9482014-02-12 18:22:18 +0000720 // Unique the enums, but maintain the original declaration ordering.
Reid Klecknerf06b2662014-02-12 19:26:24 +0000721 std::vector<std::string>
722 uniqueEnumsInOrder(const std::vector<std::string> &enums) {
Reid Klecknerf526b9482014-02-12 18:22:18 +0000723 std::vector<std::string> uniques;
George Burgess IV1881a572016-12-01 00:13:18 +0000724 SmallDenseSet<StringRef, 8> unique_set;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000725 for (const auto &i : enums) {
George Burgess IV1881a572016-12-01 00:13:18 +0000726 if (unique_set.insert(i).second)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000727 uniques.push_back(i);
Reid Klecknerf526b9482014-02-12 18:22:18 +0000728 }
729 return uniques;
730 }
731
Peter Collingbournebee583f2011-10-06 13:03:08 +0000732 class EnumArgument : public Argument {
733 std::string type;
Aaron Ballman0e468c02014-01-05 21:08:29 +0000734 std::vector<std::string> values, enums, uniques;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000735 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000736 EnumArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000737 : Argument(Arg, Attr), type(Arg.getValueAsString("Type")),
Aaron Ballman0e468c02014-01-05 21:08:29 +0000738 values(Arg.getValueAsListOfStrings("Values")),
739 enums(Arg.getValueAsListOfStrings("Enums")),
Reid Klecknerf526b9482014-02-12 18:22:18 +0000740 uniques(uniqueEnumsInOrder(enums))
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000741 {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000742 // FIXME: Emit a proper error
743 assert(!uniques.empty());
744 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000745
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000746 bool isEnumArg() const override { return true; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000747
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000748 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000749 OS << " " << type << " get" << getUpperName() << "() const {\n";
750 OS << " return " << getLowerName() << ";\n";
751 OS << " }";
752 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000753
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000754 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000755 OS << getLowerName();
756 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000757
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000758 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000759 OS << "A->get" << getUpperName() << "()";
760 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000761 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000762 OS << getLowerName() << "(" << getUpperName() << ")";
763 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000764 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000765 OS << getLowerName() << "(" << type << "(0))";
766 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000767 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000768 OS << type << " " << getUpperName();
769 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000770 void writeDeclarations(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +0000771 auto i = uniques.cbegin(), e = uniques.cend();
Peter Collingbournebee583f2011-10-06 13:03:08 +0000772 // The last one needs to not have a comma.
773 --e;
774
775 OS << "public:\n";
776 OS << " enum " << type << " {\n";
777 for (; i != e; ++i)
778 OS << " " << *i << ",\n";
779 OS << " " << *e << "\n";
780 OS << " };\n";
781 OS << "private:\n";
782 OS << " " << type << " " << getLowerName() << ";";
783 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000784
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000785 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000786 OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName()
787 << "(static_cast<" << getAttrName() << "Attr::" << type
David L. Jones267b8842017-01-24 01:04:30 +0000788 << ">(Record.readInt()));\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000789 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000790
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000791 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000792 OS << getLowerName();
793 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000794
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000795 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000796 OS << "Record.push_back(SA->get" << getUpperName() << "());\n";
797 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000798
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000799 void writeValue(raw_ostream &OS) const override {
Aaron Ballman36d79102014-09-15 16:16:14 +0000800 // FIXME: this isn't 100% correct -- some enum arguments require printing
801 // as a string literal, while others require printing as an identifier.
802 // Tablegen currently does not distinguish between the two forms.
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000803 OS << "\\\"\" << " << getAttrName() << "Attr::Convert" << type << "ToStr(get"
804 << getUpperName() << "()) << \"\\\"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000805 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000806
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000807 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000808 OS << " switch(SA->get" << getUpperName() << "()) {\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000809 for (const auto &I : uniques) {
810 OS << " case " << getAttrName() << "Attr::" << I << ":\n";
811 OS << " OS << \" " << I << "\";\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000812 OS << " break;\n";
813 }
814 OS << " }\n";
815 }
Aaron Ballman682ee422013-09-11 19:47:58 +0000816
817 void writeConversion(raw_ostream &OS) const {
818 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
819 OS << type << " &Out) {\n";
820 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000821 OS << type << ">>(Val)\n";
Aaron Ballman682ee422013-09-11 19:47:58 +0000822 for (size_t I = 0; I < enums.size(); ++I) {
823 OS << " .Case(\"" << values[I] << "\", ";
824 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
825 }
826 OS << " .Default(Optional<" << type << ">());\n";
827 OS << " if (R) {\n";
828 OS << " Out = *R;\n return true;\n }\n";
829 OS << " return false;\n";
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000830 OS << " }\n\n";
831
832 // Mapping from enumeration values back to enumeration strings isn't
833 // trivial because some enumeration values have multiple named
834 // enumerators, such as type_visibility(internal) and
835 // type_visibility(hidden) both mapping to TypeVisibilityAttr::Hidden.
836 OS << " static const char *Convert" << type << "ToStr("
837 << type << " Val) {\n"
838 << " switch(Val) {\n";
George Burgess IV1881a572016-12-01 00:13:18 +0000839 SmallDenseSet<StringRef, 8> Uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000840 for (size_t I = 0; I < enums.size(); ++I) {
841 if (Uniques.insert(enums[I]).second)
842 OS << " case " << getAttrName() << "Attr::" << enums[I]
843 << ": return \"" << values[I] << "\";\n";
844 }
845 OS << " }\n"
846 << " llvm_unreachable(\"No enumerator with that value\");\n"
847 << " }\n";
Aaron Ballman682ee422013-09-11 19:47:58 +0000848 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000849 };
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000850
851 class VariadicEnumArgument: public VariadicArgument {
852 std::string type, QualifiedTypeName;
Aaron Ballman0e468c02014-01-05 21:08:29 +0000853 std::vector<std::string> values, enums, uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000854
855 protected:
856 void writeValueImpl(raw_ostream &OS) const override {
Aaron Ballman36d79102014-09-15 16:16:14 +0000857 // FIXME: this isn't 100% correct -- some enum arguments require printing
858 // as a string literal, while others require printing as an identifier.
859 // Tablegen currently does not distinguish between the two forms.
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000860 OS << " OS << \"\\\"\" << " << getAttrName() << "Attr::Convert" << type
861 << "ToStr(Val)" << "<< \"\\\"\";\n";
862 }
863
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000864 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000865 VariadicEnumArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000866 : VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")),
867 type(Arg.getValueAsString("Type")),
Aaron Ballman0e468c02014-01-05 21:08:29 +0000868 values(Arg.getValueAsListOfStrings("Values")),
869 enums(Arg.getValueAsListOfStrings("Enums")),
Reid Klecknerf526b9482014-02-12 18:22:18 +0000870 uniques(uniqueEnumsInOrder(enums))
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000871 {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000872 QualifiedTypeName = getAttrName().str() + "Attr::" + type;
873
874 // FIXME: Emit a proper error
875 assert(!uniques.empty());
876 }
877
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000878 bool isVariadicEnumArg() const override { return true; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000879
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000880 void writeDeclarations(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +0000881 auto i = uniques.cbegin(), e = uniques.cend();
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000882 // The last one needs to not have a comma.
883 --e;
884
885 OS << "public:\n";
886 OS << " enum " << type << " {\n";
887 for (; i != e; ++i)
888 OS << " " << *i << ",\n";
889 OS << " " << *e << "\n";
890 OS << " };\n";
891 OS << "private:\n";
892
893 VariadicArgument::writeDeclarations(OS);
894 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000895
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000896 void writeDump(raw_ostream &OS) const override {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000897 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
898 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
899 << getLowerName() << "_end(); I != E; ++I) {\n";
900 OS << " switch(*I) {\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000901 for (const auto &UI : uniques) {
902 OS << " case " << getAttrName() << "Attr::" << UI << ":\n";
903 OS << " OS << \" " << UI << "\";\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000904 OS << " break;\n";
905 }
906 OS << " }\n";
907 OS << " }\n";
908 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000909
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000910 void writePCHReadDecls(raw_ostream &OS) const override {
David L. Jones267b8842017-01-24 01:04:30 +0000911 OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000912 OS << " SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName()
913 << ";\n";
914 OS << " " << getLowerName() << ".reserve(" << getLowerName()
915 << "Size);\n";
916 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
917 OS << " " << getLowerName() << ".push_back(" << "static_cast<"
David L. Jones267b8842017-01-24 01:04:30 +0000918 << QualifiedTypeName << ">(Record.readInt()));\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000919 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000920
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000921 void writePCHWrite(raw_ostream &OS) const override {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000922 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
923 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
924 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
925 << getLowerName() << "_end(); i != e; ++i)\n";
926 OS << " " << WritePCHRecord(QualifiedTypeName, "(*i)");
927 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000928
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000929 void writeConversion(raw_ostream &OS) const {
930 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
931 OS << type << " &Out) {\n";
932 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000933 OS << type << ">>(Val)\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000934 for (size_t I = 0; I < enums.size(); ++I) {
935 OS << " .Case(\"" << values[I] << "\", ";
936 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
937 }
938 OS << " .Default(Optional<" << type << ">());\n";
939 OS << " if (R) {\n";
940 OS << " Out = *R;\n return true;\n }\n";
941 OS << " return false;\n";
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000942 OS << " }\n\n";
943
944 OS << " static const char *Convert" << type << "ToStr("
945 << type << " Val) {\n"
946 << " switch(Val) {\n";
George Burgess IV1881a572016-12-01 00:13:18 +0000947 SmallDenseSet<StringRef, 8> Uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000948 for (size_t I = 0; I < enums.size(); ++I) {
949 if (Uniques.insert(enums[I]).second)
950 OS << " case " << getAttrName() << "Attr::" << enums[I]
951 << ": return \"" << values[I] << "\";\n";
952 }
953 OS << " }\n"
954 << " llvm_unreachable(\"No enumerator with that value\");\n"
955 << " }\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000956 }
957 };
Peter Collingbournebee583f2011-10-06 13:03:08 +0000958
959 class VersionArgument : public Argument {
960 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000961 VersionArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000962 : Argument(Arg, Attr)
963 {}
964
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000965 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000966 OS << " VersionTuple get" << getUpperName() << "() const {\n";
967 OS << " return " << getLowerName() << ";\n";
968 OS << " }\n";
969 OS << " void set" << getUpperName()
970 << "(ASTContext &C, VersionTuple V) {\n";
971 OS << " " << getLowerName() << " = V;\n";
972 OS << " }";
973 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000974
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000975 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000976 OS << "get" << getUpperName() << "()";
977 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000978
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000979 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000980 OS << "A->get" << getUpperName() << "()";
981 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000982
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000983 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000984 OS << getLowerName() << "(" << getUpperName() << ")";
985 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000986
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000987 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000988 OS << getLowerName() << "()";
989 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000990
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000991 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000992 OS << "VersionTuple " << getUpperName();
993 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000994
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000995 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000996 OS << "VersionTuple " << getLowerName() << ";\n";
997 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000998
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000999 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001000 OS << " VersionTuple " << getLowerName()
David L. Jones267b8842017-01-24 01:04:30 +00001001 << "= Record.readVersionTuple();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001002 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001003
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001004 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001005 OS << getLowerName();
1006 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001007
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001008 void writePCHWrite(raw_ostream &OS) const override {
Richard Smith290d8012016-04-06 17:06:00 +00001009 OS << " Record.AddVersionTuple(SA->get" << getUpperName() << "());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001010 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001011
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001012 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001013 OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";
1014 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001015
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001016 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001017 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
1018 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001019 };
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001020
1021 class ExprArgument : public SimpleArgument {
1022 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001023 ExprArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001024 : SimpleArgument(Arg, Attr, "Expr *")
1025 {}
1026
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001027 void writeASTVisitorTraversal(raw_ostream &OS) const override {
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001028 OS << " if (!"
1029 << "getDerived().TraverseStmt(A->get" << getUpperName() << "()))\n";
1030 OS << " return false;\n";
1031 }
1032
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001033 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001034 OS << "tempInst" << getUpperName();
1035 }
1036
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001037 void writeTemplateInstantiation(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001038 OS << " " << getType() << " tempInst" << getUpperName() << ";\n";
1039 OS << " {\n";
1040 OS << " EnterExpressionEvaluationContext "
Faisal Valid143a0c2017-04-01 21:30:49 +00001041 << "Unevaluated(S, Sema::ExpressionEvaluationContext::Unevaluated);\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001042 OS << " ExprResult " << "Result = S.SubstExpr("
1043 << "A->get" << getUpperName() << "(), TemplateArgs);\n";
1044 OS << " tempInst" << getUpperName() << " = "
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001045 << "Result.getAs<Expr>();\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001046 OS << " }\n";
1047 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001048
Craig Topper3164f332014-03-11 03:39:26 +00001049 void writeDump(raw_ostream &OS) const override {}
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001050
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001051 void writeDumpChildren(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001052 OS << " dumpStmt(SA->get" << getUpperName() << "());\n";
1053 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001054
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001055 void writeHasChildren(raw_ostream &OS) const override { OS << "true"; }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001056 };
1057
1058 class VariadicExprArgument : public VariadicArgument {
1059 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001060 VariadicExprArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001061 : VariadicArgument(Arg, Attr, "Expr *")
1062 {}
1063
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001064 void writeASTVisitorTraversal(raw_ostream &OS) const override {
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001065 OS << " {\n";
1066 OS << " " << getType() << " *I = A->" << getLowerName()
1067 << "_begin();\n";
1068 OS << " " << getType() << " *E = A->" << getLowerName()
1069 << "_end();\n";
1070 OS << " for (; I != E; ++I) {\n";
1071 OS << " if (!getDerived().TraverseStmt(*I))\n";
1072 OS << " return false;\n";
1073 OS << " }\n";
1074 OS << " }\n";
1075 }
1076
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001077 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001078 OS << "tempInst" << getUpperName() << ", "
1079 << "A->" << getLowerName() << "_size()";
1080 }
1081
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001082 void writeTemplateInstantiation(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +00001083 OS << " auto *tempInst" << getUpperName()
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001084 << " = new (C, 16) " << getType()
1085 << "[A->" << getLowerName() << "_size()];\n";
1086 OS << " {\n";
1087 OS << " EnterExpressionEvaluationContext "
Faisal Valid143a0c2017-04-01 21:30:49 +00001088 << "Unevaluated(S, Sema::ExpressionEvaluationContext::Unevaluated);\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001089 OS << " " << getType() << " *TI = tempInst" << getUpperName()
1090 << ";\n";
1091 OS << " " << getType() << " *I = A->" << getLowerName()
1092 << "_begin();\n";
1093 OS << " " << getType() << " *E = A->" << getLowerName()
1094 << "_end();\n";
1095 OS << " for (; I != E; ++I, ++TI) {\n";
1096 OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n";
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001097 OS << " *TI = Result.getAs<Expr>();\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001098 OS << " }\n";
1099 OS << " }\n";
1100 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001101
Craig Topper3164f332014-03-11 03:39:26 +00001102 void writeDump(raw_ostream &OS) const override {}
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001103
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001104 void writeDumpChildren(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001105 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
1106 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
Richard Smithf7514452014-10-30 21:02:37 +00001107 << getLowerName() << "_end(); I != E; ++I)\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001108 OS << " dumpStmt(*I);\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001109 }
1110
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001111 void writeHasChildren(raw_ostream &OS) const override {
Richard Trieude5cc7d2013-01-31 01:44:26 +00001112 OS << "SA->" << getLowerName() << "_begin() != "
1113 << "SA->" << getLowerName() << "_end()";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001114 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001115 };
Richard Smithb87c4652013-10-31 21:23:20 +00001116
Peter Collingbourne915df992015-05-15 18:33:32 +00001117 class VariadicStringArgument : public VariadicArgument {
1118 public:
1119 VariadicStringArgument(const Record &Arg, StringRef Attr)
Benjamin Kramer1b582012016-02-13 18:11:49 +00001120 : VariadicArgument(Arg, Attr, "StringRef")
Peter Collingbourne915df992015-05-15 18:33:32 +00001121 {}
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001122
Benjamin Kramer1b582012016-02-13 18:11:49 +00001123 void writeCtorBody(raw_ostream &OS) const override {
1124 OS << " for (size_t I = 0, E = " << getArgSizeName() << "; I != E;\n"
1125 " ++I) {\n"
1126 " StringRef Ref = " << getUpperName() << "[I];\n"
1127 " if (!Ref.empty()) {\n"
1128 " char *Mem = new (Ctx, 1) char[Ref.size()];\n"
1129 " std::memcpy(Mem, Ref.data(), Ref.size());\n"
1130 " " << getArgName() << "[I] = StringRef(Mem, Ref.size());\n"
1131 " }\n"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001132 " }\n";
Benjamin Kramer1b582012016-02-13 18:11:49 +00001133 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001134
Peter Collingbourne915df992015-05-15 18:33:32 +00001135 void writeValueImpl(raw_ostream &OS) const override {
1136 OS << " OS << \"\\\"\" << Val << \"\\\"\";\n";
1137 }
1138 };
1139
Richard Smithb87c4652013-10-31 21:23:20 +00001140 class TypeArgument : public SimpleArgument {
1141 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001142 TypeArgument(const Record &Arg, StringRef Attr)
Richard Smithb87c4652013-10-31 21:23:20 +00001143 : SimpleArgument(Arg, Attr, "TypeSourceInfo *")
1144 {}
1145
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001146 void writeAccessors(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001147 OS << " QualType get" << getUpperName() << "() const {\n";
1148 OS << " return " << getLowerName() << "->getType();\n";
1149 OS << " }";
1150 OS << " " << getType() << " get" << getUpperName() << "Loc() const {\n";
1151 OS << " return " << getLowerName() << ";\n";
1152 OS << " }";
1153 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001154
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001155 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001156 OS << "A->get" << getUpperName() << "Loc()";
1157 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001158
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001159 void writePCHWrite(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001160 OS << " " << WritePCHRecord(
1161 getType(), "SA->get" + std::string(getUpperName()) + "Loc()");
1162 }
1163 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001164
Hans Wennborgdcfba332015-10-06 23:40:43 +00001165} // end anonymous namespace
Peter Collingbournebee583f2011-10-06 13:03:08 +00001166
Aaron Ballman2f22b942014-05-20 19:47:14 +00001167static std::unique_ptr<Argument>
1168createArgument(const Record &Arg, StringRef Attr,
1169 const Record *Search = nullptr) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001170 if (!Search)
1171 Search = &Arg;
1172
David Blaikie28f30ca2014-08-08 23:59:38 +00001173 std::unique_ptr<Argument> Ptr;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001174 llvm::StringRef ArgName = Search->getName();
1175
David Blaikie28f30ca2014-08-08 23:59:38 +00001176 if (ArgName == "AlignedArgument")
1177 Ptr = llvm::make_unique<AlignedArgument>(Arg, Attr);
1178 else if (ArgName == "EnumArgument")
1179 Ptr = llvm::make_unique<EnumArgument>(Arg, Attr);
1180 else if (ArgName == "ExprArgument")
1181 Ptr = llvm::make_unique<ExprArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001182 else if (ArgName == "FunctionArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001183 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "FunctionDecl *");
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001184 else if (ArgName == "NamedArgument")
1185 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "NamedDecl *");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001186 else if (ArgName == "IdentifierArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001187 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "IdentifierInfo *");
David Majnemer4bb09802014-02-10 19:50:15 +00001188 else if (ArgName == "DefaultBoolArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001189 Ptr = llvm::make_unique<DefaultSimpleArgument>(
1190 Arg, Attr, "bool", Arg.getValueAsBit("Default"));
1191 else if (ArgName == "BoolArgument")
1192 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "bool");
Aaron Ballman18a78382013-11-21 00:28:23 +00001193 else if (ArgName == "DefaultIntArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001194 Ptr = llvm::make_unique<DefaultSimpleArgument>(
1195 Arg, Attr, "int", Arg.getValueAsInt("Default"));
1196 else if (ArgName == "IntArgument")
1197 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "int");
1198 else if (ArgName == "StringArgument")
1199 Ptr = llvm::make_unique<StringArgument>(Arg, Attr);
1200 else if (ArgName == "TypeArgument")
1201 Ptr = llvm::make_unique<TypeArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001202 else if (ArgName == "UnsignedArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001203 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "unsigned");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001204 else if (ArgName == "VariadicUnsignedArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001205 Ptr = llvm::make_unique<VariadicArgument>(Arg, Attr, "unsigned");
Peter Collingbourne915df992015-05-15 18:33:32 +00001206 else if (ArgName == "VariadicStringArgument")
1207 Ptr = llvm::make_unique<VariadicStringArgument>(Arg, Attr);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001208 else if (ArgName == "VariadicEnumArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001209 Ptr = llvm::make_unique<VariadicEnumArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001210 else if (ArgName == "VariadicExprArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001211 Ptr = llvm::make_unique<VariadicExprArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001212 else if (ArgName == "VersionArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001213 Ptr = llvm::make_unique<VersionArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001214
1215 if (!Ptr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00001216 // Search in reverse order so that the most-derived type is handled first.
Craig Topper25761242016-01-18 19:52:54 +00001217 ArrayRef<std::pair<Record*, SMRange>> Bases = Search->getSuperClasses();
David Majnemerf7e36092016-06-23 00:15:04 +00001218 for (const auto &Base : llvm::reverse(Bases)) {
Craig Topper25761242016-01-18 19:52:54 +00001219 if ((Ptr = createArgument(Arg, Attr, Base.first)))
Peter Collingbournebee583f2011-10-06 13:03:08 +00001220 break;
1221 }
1222 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001223
1224 if (Ptr && Arg.getValueAsBit("Optional"))
1225 Ptr->setOptional(true);
1226
John McCalla62c1a92015-10-28 00:17:34 +00001227 if (Ptr && Arg.getValueAsBit("Fake"))
1228 Ptr->setFake(true);
1229
David Blaikie28f30ca2014-08-08 23:59:38 +00001230 return Ptr;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001231}
1232
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001233static void writeAvailabilityValue(raw_ostream &OS) {
1234 OS << "\" << getPlatform()->getName();\n"
Manman Ren42e09eb2016-03-10 23:54:12 +00001235 << " if (getStrict()) OS << \", strict\";\n"
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001236 << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n"
1237 << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n"
1238 << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n"
1239 << " if (getUnavailable()) OS << \", unavailable\";\n"
1240 << " OS << \"";
1241}
1242
Manman Renc7890fe2016-03-16 18:50:49 +00001243static void writeDeprecatedAttrValue(raw_ostream &OS, std::string &Variety) {
1244 OS << "\\\"\" << getMessage() << \"\\\"\";\n";
1245 // Only GNU deprecated has an optional fixit argument at the second position.
1246 if (Variety == "GNU")
1247 OS << " if (!getReplacement().empty()) OS << \", \\\"\""
1248 " << getReplacement() << \"\\\"\";\n";
1249 OS << " OS << \"";
1250}
1251
Aaron Ballman3e424b52013-12-26 18:30:57 +00001252static void writeGetSpellingFunction(Record &R, raw_ostream &OS) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001253 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballman3e424b52013-12-26 18:30:57 +00001254
1255 OS << "const char *" << R.getName() << "Attr::getSpelling() const {\n";
1256 if (Spellings.empty()) {
1257 OS << " return \"(No spelling)\";\n}\n\n";
1258 return;
1259 }
1260
1261 OS << " switch (SpellingListIndex) {\n"
1262 " default:\n"
1263 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1264 " return \"(No spelling)\";\n";
1265
1266 for (unsigned I = 0; I < Spellings.size(); ++I)
1267 OS << " case " << I << ":\n"
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001268 " return \"" << Spellings[I].name() << "\";\n";
Aaron Ballman3e424b52013-12-26 18:30:57 +00001269 // End of the switch statement.
1270 OS << " }\n";
1271 // End of the getSpelling function.
1272 OS << "}\n\n";
1273}
1274
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001275static void
1276writePrettyPrintFunction(Record &R,
1277 const std::vector<std::unique_ptr<Argument>> &Args,
1278 raw_ostream &OS) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001279 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Michael Han99315932013-01-24 16:46:58 +00001280
1281 OS << "void " << R.getName() << "Attr::printPretty("
1282 << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n";
1283
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001284 if (Spellings.empty()) {
Michael Han99315932013-01-24 16:46:58 +00001285 OS << "}\n\n";
1286 return;
1287 }
1288
1289 OS <<
1290 " switch (SpellingListIndex) {\n"
1291 " default:\n"
1292 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1293 " break;\n";
1294
1295 for (unsigned I = 0; I < Spellings.size(); ++ I) {
1296 llvm::SmallString<16> Prefix;
1297 llvm::SmallString<8> Suffix;
1298 // The actual spelling of the name and namespace (if applicable)
1299 // of an attribute without considering prefix and suffix.
1300 llvm::SmallString<64> Spelling;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001301 std::string Name = Spellings[I].name();
1302 std::string Variety = Spellings[I].variety();
Michael Han99315932013-01-24 16:46:58 +00001303
1304 if (Variety == "GNU") {
1305 Prefix = " __attribute__((";
1306 Suffix = "))";
1307 } else if (Variety == "CXX11") {
1308 Prefix = " [[";
1309 Suffix = "]]";
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001310 std::string Namespace = Spellings[I].nameSpace();
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001311 if (!Namespace.empty()) {
Michael Han99315932013-01-24 16:46:58 +00001312 Spelling += Namespace;
1313 Spelling += "::";
1314 }
1315 } else if (Variety == "Declspec") {
1316 Prefix = " __declspec(";
1317 Suffix = ")";
Nico Weber20e08042016-09-03 02:55:10 +00001318 } else if (Variety == "Microsoft") {
1319 Prefix = "[";
1320 Suffix = "]";
Richard Smith0cdcc982013-01-29 01:24:26 +00001321 } else if (Variety == "Keyword") {
1322 Prefix = " ";
1323 Suffix = "";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001324 } else if (Variety == "Pragma") {
1325 Prefix = "#pragma ";
1326 Suffix = "\n";
1327 std::string Namespace = Spellings[I].nameSpace();
1328 if (!Namespace.empty()) {
1329 Spelling += Namespace;
1330 Spelling += " ";
1331 }
Michael Han99315932013-01-24 16:46:58 +00001332 } else {
Richard Smith0cdcc982013-01-29 01:24:26 +00001333 llvm_unreachable("Unknown attribute syntax variety!");
Michael Han99315932013-01-24 16:46:58 +00001334 }
1335
1336 Spelling += Name;
1337
1338 OS <<
1339 " case " << I << " : {\n"
Yaron Keren09fb7c62015-03-10 07:33:23 +00001340 " OS << \"" << Prefix << Spelling;
Michael Han99315932013-01-24 16:46:58 +00001341
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001342 if (Variety == "Pragma") {
1343 OS << " \";\n";
1344 OS << " printPrettyPragma(OS, Policy);\n";
Alexey Bataev6d455322015-10-12 06:59:48 +00001345 OS << " OS << \"\\n\";";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001346 OS << " break;\n";
1347 OS << " }\n";
1348 continue;
1349 }
1350
John McCalla62c1a92015-10-28 00:17:34 +00001351 // Fake arguments aren't part of the parsed form and should not be
1352 // pretty-printed.
George Burgess IV1881a572016-12-01 00:13:18 +00001353 bool hasNonFakeArgs = llvm::any_of(
1354 Args, [](const std::unique_ptr<Argument> &A) { return !A->isFake(); });
John McCalla62c1a92015-10-28 00:17:34 +00001355
Aaron Ballmanc960f562014-08-01 13:49:00 +00001356 // FIXME: always printing the parenthesis isn't the correct behavior for
1357 // attributes which have optional arguments that were not provided. For
1358 // instance: __attribute__((aligned)) will be pretty printed as
1359 // __attribute__((aligned())). The logic should check whether there is only
1360 // a single argument, and if it is optional, whether it has been provided.
John McCalla62c1a92015-10-28 00:17:34 +00001361 if (hasNonFakeArgs)
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001362 OS << "(";
Michael Han99315932013-01-24 16:46:58 +00001363 if (Spelling == "availability") {
1364 writeAvailabilityValue(OS);
Manman Renc7890fe2016-03-16 18:50:49 +00001365 } else if (Spelling == "deprecated" || Spelling == "gnu::deprecated") {
1366 writeDeprecatedAttrValue(OS, Variety);
Michael Han99315932013-01-24 16:46:58 +00001367 } else {
John McCalla62c1a92015-10-28 00:17:34 +00001368 unsigned index = 0;
1369 for (const auto &arg : Args) {
1370 if (arg->isFake()) continue;
1371 if (index++) OS << ", ";
1372 arg->writeValue(OS);
Michael Han99315932013-01-24 16:46:58 +00001373 }
1374 }
1375
John McCalla62c1a92015-10-28 00:17:34 +00001376 if (hasNonFakeArgs)
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001377 OS << ")";
Yaron Keren09fb7c62015-03-10 07:33:23 +00001378 OS << Suffix + "\";\n";
Michael Han99315932013-01-24 16:46:58 +00001379
1380 OS <<
1381 " break;\n"
1382 " }\n";
1383 }
1384
1385 // End of the switch statement.
1386 OS << "}\n";
1387 // End of the print function.
1388 OS << "}\n\n";
1389}
1390
Michael Hanaf02bbe2013-02-01 01:19:17 +00001391/// \brief Return the index of a spelling in a spelling list.
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001392static unsigned
1393getSpellingListIndex(const std::vector<FlattenedSpelling> &SpellingList,
1394 const FlattenedSpelling &Spelling) {
Alexander Kornienko6ee521c2015-01-23 15:36:10 +00001395 assert(!SpellingList.empty() && "Spelling list is empty!");
Michael Hanaf02bbe2013-02-01 01:19:17 +00001396
1397 for (unsigned Index = 0; Index < SpellingList.size(); ++Index) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001398 const FlattenedSpelling &S = SpellingList[Index];
1399 if (S.variety() != Spelling.variety())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001400 continue;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001401 if (S.nameSpace() != Spelling.nameSpace())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001402 continue;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001403 if (S.name() != Spelling.name())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001404 continue;
1405
1406 return Index;
1407 }
1408
1409 llvm_unreachable("Unknown spelling!");
1410}
1411
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001412static void writeAttrAccessorDefinition(const Record &R, raw_ostream &OS) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00001413 std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors");
George Burgess IV1881a572016-12-01 00:13:18 +00001414 if (Accessors.empty())
1415 return;
1416
1417 const std::vector<FlattenedSpelling> SpellingList = GetFlattenedSpellings(R);
1418 assert(!SpellingList.empty() &&
1419 "Attribute with empty spelling list can't have accessors!");
Aaron Ballman2f22b942014-05-20 19:47:14 +00001420 for (const auto *Accessor : Accessors) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00001421 std::string Name = Accessor->getValueAsString("Name");
George Burgess IV1881a572016-12-01 00:13:18 +00001422 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Accessor);
Michael Hanaf02bbe2013-02-01 01:19:17 +00001423
1424 OS << " bool " << Name << "() const { return SpellingListIndex == ";
1425 for (unsigned Index = 0; Index < Spellings.size(); ++Index) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001426 OS << getSpellingListIndex(SpellingList, Spellings[Index]);
George Burgess IV1881a572016-12-01 00:13:18 +00001427 if (Index != Spellings.size() - 1)
Michael Hanaf02bbe2013-02-01 01:19:17 +00001428 OS << " ||\n SpellingListIndex == ";
1429 else
1430 OS << "; }\n";
1431 }
1432 }
1433}
1434
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001435static bool
1436SpellingNamesAreCommon(const std::vector<FlattenedSpelling>& Spellings) {
Aaron Ballman36a53502014-01-16 13:03:14 +00001437 assert(!Spellings.empty() && "An empty list of spellings was provided");
1438 std::string FirstName = NormalizeNameForSpellingComparison(
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001439 Spellings.front().name());
Aaron Ballman2f22b942014-05-20 19:47:14 +00001440 for (const auto &Spelling :
1441 llvm::make_range(std::next(Spellings.begin()), Spellings.end())) {
1442 std::string Name = NormalizeNameForSpellingComparison(Spelling.name());
Aaron Ballman36a53502014-01-16 13:03:14 +00001443 if (Name != FirstName)
1444 return false;
1445 }
1446 return true;
1447}
1448
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001449typedef std::map<unsigned, std::string> SemanticSpellingMap;
1450static std::string
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001451CreateSemanticSpellings(const std::vector<FlattenedSpelling> &Spellings,
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001452 SemanticSpellingMap &Map) {
1453 // The enumerants are automatically generated based on the variety,
1454 // namespace (if present) and name for each attribute spelling. However,
1455 // care is taken to avoid trampling on the reserved namespace due to
1456 // underscores.
1457 std::string Ret(" enum Spelling {\n");
1458 std::set<std::string> Uniques;
1459 unsigned Idx = 0;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001460 for (auto I = Spellings.begin(), E = Spellings.end(); I != E; ++I, ++Idx) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001461 const FlattenedSpelling &S = *I;
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00001462 const std::string &Variety = S.variety();
1463 const std::string &Spelling = S.name();
1464 const std::string &Namespace = S.nameSpace();
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001465 std::string EnumName;
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001466
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001467 EnumName += (Variety + "_");
1468 if (!Namespace.empty())
1469 EnumName += (NormalizeNameForSpellingComparison(Namespace).str() +
1470 "_");
1471 EnumName += NormalizeNameForSpellingComparison(Spelling);
1472
1473 // Even if the name is not unique, this spelling index corresponds to a
1474 // particular enumerant name that we've calculated.
1475 Map[Idx] = EnumName;
1476
1477 // Since we have been stripping underscores to avoid trampling on the
1478 // reserved namespace, we may have inadvertently created duplicate
1479 // enumerant names. These duplicates are not considered part of the
1480 // semantic spelling, and can be elided.
1481 if (Uniques.find(EnumName) != Uniques.end())
1482 continue;
1483
1484 Uniques.insert(EnumName);
1485 if (I != Spellings.begin())
1486 Ret += ",\n";
Aaron Ballman9bf6b752015-03-10 17:19:18 +00001487 // Duplicate spellings are not considered part of the semantic spelling
1488 // enumeration, but the spelling index and semantic spelling values are
1489 // meant to be equivalent, so we must specify a concrete value for each
1490 // enumerator.
1491 Ret += " " + EnumName + " = " + llvm::utostr(Idx);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001492 }
1493 Ret += "\n };\n\n";
1494 return Ret;
1495}
1496
1497void WriteSemanticSpellingSwitch(const std::string &VarName,
1498 const SemanticSpellingMap &Map,
1499 raw_ostream &OS) {
1500 OS << " switch (" << VarName << ") {\n default: "
1501 << "llvm_unreachable(\"Unknown spelling list index\");\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001502 for (const auto &I : Map)
1503 OS << " case " << I.first << ": return " << I.second << ";\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001504 OS << " }\n";
1505}
1506
Aaron Ballman35db2b32014-01-29 22:13:45 +00001507// Emits the LateParsed property for attributes.
1508static void emitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) {
1509 OS << "#if defined(CLANG_ATTR_LATE_PARSED_LIST)\n";
1510 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1511
Aaron Ballman2f22b942014-05-20 19:47:14 +00001512 for (const auto *Attr : Attrs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001513 bool LateParsed = Attr->getValueAsBit("LateParsed");
Aaron Ballman35db2b32014-01-29 22:13:45 +00001514
1515 if (LateParsed) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001516 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
Aaron Ballman35db2b32014-01-29 22:13:45 +00001517
1518 // FIXME: Handle non-GNU attributes
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001519 for (const auto &I : Spellings) {
1520 if (I.variety() != "GNU")
Aaron Ballman35db2b32014-01-29 22:13:45 +00001521 continue;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001522 OS << ".Case(\"" << I.name() << "\", " << LateParsed << ")\n";
Aaron Ballman35db2b32014-01-29 22:13:45 +00001523 }
1524 }
1525 }
1526 OS << "#endif // CLANG_ATTR_LATE_PARSED_LIST\n\n";
1527}
1528
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001529static bool hasGNUorCXX11Spelling(const Record &Attribute) {
1530 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attribute);
1531 for (const auto &I : Spellings) {
1532 if (I.variety() == "GNU" || I.variety() == "CXX11")
1533 return true;
1534 }
1535 return false;
1536}
1537
1538namespace {
1539
1540struct AttributeSubjectMatchRule {
1541 const Record *MetaSubject;
1542 const Record *Constraint;
1543
1544 AttributeSubjectMatchRule(const Record *MetaSubject, const Record *Constraint)
1545 : MetaSubject(MetaSubject), Constraint(Constraint) {
1546 assert(MetaSubject && "Missing subject");
1547 }
1548
1549 bool isSubRule() const { return Constraint != nullptr; }
1550
1551 std::vector<Record *> getSubjects() const {
1552 return (Constraint ? Constraint : MetaSubject)
1553 ->getValueAsListOfDefs("Subjects");
1554 }
1555
1556 std::vector<Record *> getLangOpts() const {
1557 if (Constraint) {
1558 // Lookup the options in the sub-rule first, in case the sub-rule
1559 // overrides the rules options.
1560 std::vector<Record *> Opts = Constraint->getValueAsListOfDefs("LangOpts");
1561 if (!Opts.empty())
1562 return Opts;
1563 }
1564 return MetaSubject->getValueAsListOfDefs("LangOpts");
1565 }
1566
1567 // Abstract rules are used only for sub-rules
1568 bool isAbstractRule() const { return getSubjects().empty(); }
1569
1570 std::string getName() const {
1571 return (Constraint ? Constraint : MetaSubject)->getValueAsString("Name");
1572 }
1573
1574 bool isNegatedSubRule() const {
1575 assert(isSubRule() && "Not a sub-rule");
1576 return Constraint->getValueAsBit("Negated");
1577 }
1578
1579 std::string getSpelling() const {
1580 std::string Result = MetaSubject->getValueAsString("Name");
1581 if (isSubRule()) {
1582 Result += '(';
1583 if (isNegatedSubRule())
1584 Result += "unless(";
1585 Result += getName();
1586 if (isNegatedSubRule())
1587 Result += ')';
1588 Result += ')';
1589 }
1590 return Result;
1591 }
1592
1593 std::string getEnumValueName() const {
1594 std::string Result =
1595 "SubjectMatchRule_" + MetaSubject->getValueAsString("Name");
1596 if (isSubRule()) {
1597 Result += "_";
1598 if (isNegatedSubRule())
1599 Result += "not_";
1600 Result += Constraint->getValueAsString("Name");
1601 }
1602 if (isAbstractRule())
1603 Result += "_abstract";
1604 return Result;
1605 }
1606
1607 std::string getEnumValue() const { return "attr::" + getEnumValueName(); }
1608
1609 static const char *EnumName;
1610};
1611
1612const char *AttributeSubjectMatchRule::EnumName = "attr::SubjectMatchRule";
1613
1614struct PragmaClangAttributeSupport {
1615 std::vector<AttributeSubjectMatchRule> Rules;
Alex Lorenz24952fb2017-04-19 15:52:11 +00001616
1617 class RuleOrAggregateRuleSet {
1618 std::vector<AttributeSubjectMatchRule> Rules;
1619 bool IsRule;
1620 RuleOrAggregateRuleSet(ArrayRef<AttributeSubjectMatchRule> Rules,
1621 bool IsRule)
1622 : Rules(Rules), IsRule(IsRule) {}
1623
1624 public:
1625 bool isRule() const { return IsRule; }
1626
1627 const AttributeSubjectMatchRule &getRule() const {
1628 assert(IsRule && "not a rule!");
1629 return Rules[0];
1630 }
1631
1632 ArrayRef<AttributeSubjectMatchRule> getAggregateRuleSet() const {
1633 return Rules;
1634 }
1635
1636 static RuleOrAggregateRuleSet
1637 getRule(const AttributeSubjectMatchRule &Rule) {
1638 return RuleOrAggregateRuleSet(Rule, /*IsRule=*/true);
1639 }
1640 static RuleOrAggregateRuleSet
1641 getAggregateRuleSet(ArrayRef<AttributeSubjectMatchRule> Rules) {
1642 return RuleOrAggregateRuleSet(Rules, /*IsRule=*/false);
1643 }
1644 };
1645 llvm::DenseMap<const Record *, RuleOrAggregateRuleSet> SubjectsToRules;
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001646
1647 PragmaClangAttributeSupport(RecordKeeper &Records);
1648
1649 bool isAttributedSupported(const Record &Attribute);
1650
1651 void emitMatchRuleList(raw_ostream &OS);
1652
1653 std::string generateStrictConformsTo(const Record &Attr, raw_ostream &OS);
1654
1655 void generateParsingHelpers(raw_ostream &OS);
1656};
1657
1658} // end anonymous namespace
1659
Alex Lorenz24952fb2017-04-19 15:52:11 +00001660static bool doesDeclDeriveFrom(const Record *D, const Record *Base) {
1661 const Record *CurrentBase = D->getValueAsDef("Base");
1662 if (!CurrentBase)
1663 return false;
1664 if (CurrentBase == Base)
1665 return true;
1666 return doesDeclDeriveFrom(CurrentBase, Base);
1667}
1668
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001669PragmaClangAttributeSupport::PragmaClangAttributeSupport(
1670 RecordKeeper &Records) {
1671 std::vector<Record *> MetaSubjects =
1672 Records.getAllDerivedDefinitions("AttrSubjectMatcherRule");
1673 auto MapFromSubjectsToRules = [this](const Record *SubjectContainer,
1674 const Record *MetaSubject,
Saleem Abdulrasoolbe4773c2017-05-01 00:26:59 +00001675 const Record *Constraint) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001676 Rules.emplace_back(MetaSubject, Constraint);
1677 std::vector<Record *> ApplicableSubjects =
1678 SubjectContainer->getValueAsListOfDefs("Subjects");
1679 for (const auto *Subject : ApplicableSubjects) {
1680 bool Inserted =
Alex Lorenz24952fb2017-04-19 15:52:11 +00001681 SubjectsToRules
1682 .try_emplace(Subject, RuleOrAggregateRuleSet::getRule(
1683 AttributeSubjectMatchRule(MetaSubject,
1684 Constraint)))
1685 .second;
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001686 if (!Inserted) {
1687 PrintFatalError("Attribute subject match rules should not represent"
1688 "same attribute subjects.");
1689 }
1690 }
1691 };
1692 for (const auto *MetaSubject : MetaSubjects) {
Saleem Abdulrasoolbe4773c2017-05-01 00:26:59 +00001693 MapFromSubjectsToRules(MetaSubject, MetaSubject, /*Constraints=*/nullptr);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001694 std::vector<Record *> Constraints =
1695 MetaSubject->getValueAsListOfDefs("Constraints");
1696 for (const auto *Constraint : Constraints)
1697 MapFromSubjectsToRules(Constraint, MetaSubject, Constraint);
1698 }
Alex Lorenz24952fb2017-04-19 15:52:11 +00001699
1700 std::vector<Record *> Aggregates =
1701 Records.getAllDerivedDefinitions("AttrSubjectMatcherAggregateRule");
1702 std::vector<Record *> DeclNodes = Records.getAllDerivedDefinitions("DDecl");
1703 for (const auto *Aggregate : Aggregates) {
1704 Record *SubjectDecl = Aggregate->getValueAsDef("Subject");
1705
1706 // Gather sub-classes of the aggregate subject that act as attribute
1707 // subject rules.
1708 std::vector<AttributeSubjectMatchRule> Rules;
1709 for (const auto *D : DeclNodes) {
1710 if (doesDeclDeriveFrom(D, SubjectDecl)) {
1711 auto It = SubjectsToRules.find(D);
1712 if (It == SubjectsToRules.end())
1713 continue;
1714 if (!It->second.isRule() || It->second.getRule().isSubRule())
1715 continue; // Assume that the rule will be included as well.
1716 Rules.push_back(It->second.getRule());
1717 }
1718 }
1719
1720 bool Inserted =
1721 SubjectsToRules
1722 .try_emplace(SubjectDecl,
1723 RuleOrAggregateRuleSet::getAggregateRuleSet(Rules))
1724 .second;
1725 if (!Inserted) {
1726 PrintFatalError("Attribute subject match rules should not represent"
1727 "same attribute subjects.");
1728 }
1729 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001730}
1731
1732static PragmaClangAttributeSupport &
1733getPragmaAttributeSupport(RecordKeeper &Records) {
1734 static PragmaClangAttributeSupport Instance(Records);
1735 return Instance;
1736}
1737
1738void PragmaClangAttributeSupport::emitMatchRuleList(raw_ostream &OS) {
1739 OS << "#ifndef ATTR_MATCH_SUB_RULE\n";
1740 OS << "#define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, "
1741 "IsNegated) "
1742 << "ATTR_MATCH_RULE(Value, Spelling, IsAbstract)\n";
1743 OS << "#endif\n";
1744 for (const auto &Rule : Rules) {
1745 OS << (Rule.isSubRule() ? "ATTR_MATCH_SUB_RULE" : "ATTR_MATCH_RULE") << '(';
1746 OS << Rule.getEnumValueName() << ", \"" << Rule.getSpelling() << "\", "
1747 << Rule.isAbstractRule();
1748 if (Rule.isSubRule())
1749 OS << ", "
1750 << AttributeSubjectMatchRule(Rule.MetaSubject, nullptr).getEnumValue()
1751 << ", " << Rule.isNegatedSubRule();
1752 OS << ")\n";
1753 }
1754 OS << "#undef ATTR_MATCH_SUB_RULE\n";
1755}
1756
1757bool PragmaClangAttributeSupport::isAttributedSupported(
1758 const Record &Attribute) {
1759 if (Attribute.getValueAsBit("ForcePragmaAttributeSupport"))
1760 return true;
1761 // Opt-out rules:
1762 // FIXME: The documentation check should be moved before
1763 // the ForcePragmaAttributeSupport check after annotate is documented.
1764 // No documentation present.
1765 if (Attribute.isValueUnset("Documentation"))
1766 return false;
1767 std::vector<Record *> Docs = Attribute.getValueAsListOfDefs("Documentation");
1768 if (Docs.empty())
1769 return false;
1770 if (Docs.size() == 1 && Docs[0]->getName() == "Undocumented")
1771 return false;
1772 // An attribute requires delayed parsing (LateParsed is on)
1773 if (Attribute.getValueAsBit("LateParsed"))
1774 return false;
1775 // An attribute has no GNU/CXX11 spelling
1776 if (!hasGNUorCXX11Spelling(Attribute))
1777 return false;
1778 // An attribute subject list has a subject that isn't covered by one of the
1779 // subject match rules or has no subjects at all.
1780 if (Attribute.isValueUnset("Subjects"))
1781 return false;
1782 const Record *SubjectObj = Attribute.getValueAsDef("Subjects");
1783 std::vector<Record *> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
1784 if (Subjects.empty())
1785 return false;
1786 for (const auto *Subject : Subjects) {
1787 if (SubjectsToRules.find(Subject) == SubjectsToRules.end())
1788 return false;
1789 }
1790 return true;
1791}
1792
1793std::string
1794PragmaClangAttributeSupport::generateStrictConformsTo(const Record &Attr,
1795 raw_ostream &OS) {
1796 if (!isAttributedSupported(Attr))
1797 return "nullptr";
1798 // Generate a function that constructs a set of matching rules that describe
1799 // to which declarations the attribute should apply to.
1800 std::string FnName = "matchRulesFor" + Attr.getName().str();
1801 std::stringstream SS;
1802 SS << "static void " << FnName << "(llvm::SmallVectorImpl<std::pair<"
1803 << AttributeSubjectMatchRule::EnumName
1804 << ", bool>> &MatchRules, const LangOptions &LangOpts) {\n";
1805 if (Attr.isValueUnset("Subjects")) {
1806 SS << "}\n\n";
1807 OS << SS.str();
1808 return FnName;
1809 }
1810 const Record *SubjectObj = Attr.getValueAsDef("Subjects");
1811 std::vector<Record *> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
1812 for (const auto *Subject : Subjects) {
1813 auto It = SubjectsToRules.find(Subject);
1814 assert(It != SubjectsToRules.end() &&
1815 "This attribute is unsupported by #pragma clang attribute");
Alex Lorenz24952fb2017-04-19 15:52:11 +00001816 for (const auto &Rule : It->getSecond().getAggregateRuleSet()) {
1817 // The rule might be language specific, so only subtract it from the given
1818 // rules if the specific language options are specified.
1819 std::vector<Record *> LangOpts = Rule.getLangOpts();
1820 SS << " MatchRules.push_back(std::make_pair(" << Rule.getEnumValue()
1821 << ", /*IsSupported=*/";
1822 if (!LangOpts.empty()) {
1823 for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) {
1824 std::string Part = (*I)->getValueAsString("Name");
1825 if ((*I)->getValueAsBit("Negated"))
1826 SS << "!";
1827 SS << "LangOpts." + Part;
1828 if (I + 1 != E)
1829 SS << " || ";
1830 }
1831 } else
1832 SS << "true";
1833 SS << "));\n";
1834 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001835 }
1836 SS << "}\n\n";
1837 OS << SS.str();
1838 return FnName;
1839}
1840
1841void PragmaClangAttributeSupport::generateParsingHelpers(raw_ostream &OS) {
1842 // Generate routines that check the names of sub-rules.
1843 OS << "Optional<attr::SubjectMatchRule> "
1844 "defaultIsAttributeSubjectMatchSubRuleFor(StringRef, bool) {\n";
1845 OS << " return None;\n";
1846 OS << "}\n\n";
1847
1848 std::map<const Record *, std::vector<AttributeSubjectMatchRule>>
1849 SubMatchRules;
1850 for (const auto &Rule : Rules) {
1851 if (!Rule.isSubRule())
1852 continue;
1853 SubMatchRules[Rule.MetaSubject].push_back(Rule);
1854 }
1855
1856 for (const auto &SubMatchRule : SubMatchRules) {
1857 OS << "Optional<attr::SubjectMatchRule> isAttributeSubjectMatchSubRuleFor_"
1858 << SubMatchRule.first->getValueAsString("Name")
1859 << "(StringRef Name, bool IsUnless) {\n";
1860 OS << " if (IsUnless)\n";
1861 OS << " return "
1862 "llvm::StringSwitch<Optional<attr::SubjectMatchRule>>(Name).\n";
1863 for (const auto &Rule : SubMatchRule.second) {
1864 if (Rule.isNegatedSubRule())
1865 OS << " Case(\"" << Rule.getName() << "\", " << Rule.getEnumValue()
1866 << ").\n";
1867 }
1868 OS << " Default(None);\n";
1869 OS << " return "
1870 "llvm::StringSwitch<Optional<attr::SubjectMatchRule>>(Name).\n";
1871 for (const auto &Rule : SubMatchRule.second) {
1872 if (!Rule.isNegatedSubRule())
1873 OS << " Case(\"" << Rule.getName() << "\", " << Rule.getEnumValue()
1874 << ").\n";
1875 }
1876 OS << " Default(None);\n";
1877 OS << "}\n\n";
1878 }
1879
1880 // Generate the function that checks for the top-level rules.
1881 OS << "std::pair<Optional<attr::SubjectMatchRule>, "
1882 "Optional<attr::SubjectMatchRule> (*)(StringRef, "
1883 "bool)> isAttributeSubjectMatchRule(StringRef Name) {\n";
1884 OS << " return "
1885 "llvm::StringSwitch<std::pair<Optional<attr::SubjectMatchRule>, "
1886 "Optional<attr::SubjectMatchRule> (*) (StringRef, "
1887 "bool)>>(Name).\n";
1888 for (const auto &Rule : Rules) {
1889 if (Rule.isSubRule())
1890 continue;
1891 std::string SubRuleFunction;
1892 if (SubMatchRules.count(Rule.MetaSubject))
1893 SubRuleFunction = "isAttributeSubjectMatchSubRuleFor_" + Rule.getName();
1894 else
1895 SubRuleFunction = "defaultIsAttributeSubjectMatchSubRuleFor";
1896 OS << " Case(\"" << Rule.getName() << "\", std::make_pair("
1897 << Rule.getEnumValue() << ", " << SubRuleFunction << ")).\n";
1898 }
1899 OS << " Default(std::make_pair(None, "
1900 "defaultIsAttributeSubjectMatchSubRuleFor));\n";
1901 OS << "}\n\n";
1902
1903 // Generate the function that checks for the submatch rules.
1904 OS << "const char *validAttributeSubjectMatchSubRules("
1905 << AttributeSubjectMatchRule::EnumName << " Rule) {\n";
1906 OS << " switch (Rule) {\n";
1907 for (const auto &SubMatchRule : SubMatchRules) {
1908 OS << " case "
1909 << AttributeSubjectMatchRule(SubMatchRule.first, nullptr).getEnumValue()
1910 << ":\n";
1911 OS << " return \"'";
1912 bool IsFirst = true;
1913 for (const auto &Rule : SubMatchRule.second) {
1914 if (!IsFirst)
1915 OS << ", '";
1916 IsFirst = false;
1917 if (Rule.isNegatedSubRule())
1918 OS << "unless(";
1919 OS << Rule.getName();
1920 if (Rule.isNegatedSubRule())
1921 OS << ')';
1922 OS << "'";
1923 }
1924 OS << "\";\n";
1925 }
1926 OS << " default: return nullptr;\n";
1927 OS << " }\n";
1928 OS << "}\n\n";
1929}
1930
George Burgess IV1881a572016-12-01 00:13:18 +00001931template <typename Fn>
1932static void forEachUniqueSpelling(const Record &Attr, Fn &&F) {
1933 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
1934 SmallDenseSet<StringRef, 8> Seen;
1935 for (const FlattenedSpelling &S : Spellings) {
1936 if (Seen.insert(S.name()).second)
1937 F(S);
1938 }
1939}
1940
Aaron Ballman35db2b32014-01-29 22:13:45 +00001941/// \brief Emits the first-argument-is-type property for attributes.
1942static void emitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) {
1943 OS << "#if defined(CLANG_ATTR_TYPE_ARG_LIST)\n";
1944 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
1945
Aaron Ballman2f22b942014-05-20 19:47:14 +00001946 for (const auto *Attr : Attrs) {
Aaron Ballman35db2b32014-01-29 22:13:45 +00001947 // Determine whether the first argument is a type.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001948 std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
Aaron Ballman35db2b32014-01-29 22:13:45 +00001949 if (Args.empty())
1950 continue;
1951
Craig Topper25761242016-01-18 19:52:54 +00001952 if (Args[0]->getSuperClasses().back().first->getName() != "TypeArgument")
Aaron Ballman35db2b32014-01-29 22:13:45 +00001953 continue;
1954
1955 // All these spellings take a single type argument.
George Burgess IV1881a572016-12-01 00:13:18 +00001956 forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) {
1957 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
1958 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00001959 }
1960 OS << "#endif // CLANG_ATTR_TYPE_ARG_LIST\n\n";
1961}
1962
1963/// \brief Emits the parse-arguments-in-unevaluated-context property for
1964/// attributes.
1965static void emitClangAttrArgContextList(RecordKeeper &Records, raw_ostream &OS) {
1966 OS << "#if defined(CLANG_ATTR_ARG_CONTEXT_LIST)\n";
1967 ParsedAttrMap Attrs = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001968 for (const auto &I : Attrs) {
1969 const Record &Attr = *I.second;
Aaron Ballman35db2b32014-01-29 22:13:45 +00001970
1971 if (!Attr.getValueAsBit("ParseArgumentsAsUnevaluated"))
1972 continue;
1973
1974 // All these spellings take are parsed unevaluated.
George Burgess IV1881a572016-12-01 00:13:18 +00001975 forEachUniqueSpelling(Attr, [&](const FlattenedSpelling &S) {
1976 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
1977 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00001978 }
1979 OS << "#endif // CLANG_ATTR_ARG_CONTEXT_LIST\n\n";
1980}
1981
1982static bool isIdentifierArgument(Record *Arg) {
1983 return !Arg->getSuperClasses().empty() &&
Craig Topper25761242016-01-18 19:52:54 +00001984 llvm::StringSwitch<bool>(Arg->getSuperClasses().back().first->getName())
Aaron Ballman35db2b32014-01-29 22:13:45 +00001985 .Case("IdentifierArgument", true)
1986 .Case("EnumArgument", true)
Aaron Ballman55ef1512014-12-19 16:42:04 +00001987 .Case("VariadicEnumArgument", true)
Aaron Ballman35db2b32014-01-29 22:13:45 +00001988 .Default(false);
1989}
1990
1991// Emits the first-argument-is-identifier property for attributes.
1992static void emitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) {
1993 OS << "#if defined(CLANG_ATTR_IDENTIFIER_ARG_LIST)\n";
1994 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1995
Aaron Ballman2f22b942014-05-20 19:47:14 +00001996 for (const auto *Attr : Attrs) {
Aaron Ballman35db2b32014-01-29 22:13:45 +00001997 // Determine whether the first argument is an identifier.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001998 std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
Aaron Ballman35db2b32014-01-29 22:13:45 +00001999 if (Args.empty() || !isIdentifierArgument(Args[0]))
2000 continue;
2001
2002 // All these spellings take an identifier argument.
George Burgess IV1881a572016-12-01 00:13:18 +00002003 forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) {
2004 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
2005 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00002006 }
2007 OS << "#endif // CLANG_ATTR_IDENTIFIER_ARG_LIST\n\n";
2008}
2009
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002010namespace clang {
2011
2012// Emits the class definitions for attributes.
2013void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002014 emitSourceFileHeader("Attribute classes' definitions", OS);
2015
Peter Collingbournebee583f2011-10-06 13:03:08 +00002016 OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
2017 OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
2018
2019 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2020
Aaron Ballman2f22b942014-05-20 19:47:14 +00002021 for (const auto *Attr : Attrs) {
2022 const Record &R = *Attr;
Aaron Ballman06bd44b2014-02-17 18:23:02 +00002023
2024 // FIXME: Currently, documentation is generated as-needed due to the fact
2025 // that there is no way to allow a generated project "reach into" the docs
2026 // directory (for instance, it may be an out-of-tree build). However, we want
2027 // to ensure that every attribute has a Documentation field, and produce an
2028 // error if it has been neglected. Otherwise, the on-demand generation which
2029 // happens server-side will fail. This code is ensuring that functionality,
2030 // even though this Emitter doesn't technically need the documentation.
2031 // When attribute documentation can be generated as part of the build
2032 // itself, this code can be removed.
2033 (void)R.getValueAsListOfDefs("Documentation");
Douglas Gregorb2daf842012-05-02 15:56:52 +00002034
2035 if (!R.getValueAsBit("ASTNode"))
2036 continue;
2037
Craig Topper25761242016-01-18 19:52:54 +00002038 ArrayRef<std::pair<Record *, SMRange>> Supers = R.getSuperClasses();
Aaron Ballman0979e9e2013-07-30 01:44:15 +00002039 assert(!Supers.empty() && "Forgot to specify a superclass for the attr");
Aaron Ballman0979e9e2013-07-30 01:44:15 +00002040 std::string SuperName;
David Majnemerf7e36092016-06-23 00:15:04 +00002041 for (const auto &Super : llvm::reverse(Supers)) {
Craig Topper25761242016-01-18 19:52:54 +00002042 const Record *R = Super.first;
2043 if (R->getName() != "TargetSpecificAttr" && SuperName.empty())
2044 SuperName = R->getName();
Aaron Ballman0979e9e2013-07-30 01:44:15 +00002045 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00002046
2047 OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
2048
2049 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002050 std::vector<std::unique_ptr<Argument>> Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002051 Args.reserve(ArgRecords.size());
2052
John McCalla62c1a92015-10-28 00:17:34 +00002053 bool HasOptArg = false;
2054 bool HasFakeArg = false;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002055 for (const auto *ArgRecord : ArgRecords) {
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002056 Args.emplace_back(createArgument(*ArgRecord, R.getName()));
2057 Args.back()->writeDeclarations(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002058 OS << "\n\n";
John McCalla62c1a92015-10-28 00:17:34 +00002059
2060 // For these purposes, fake takes priority over optional.
2061 if (Args.back()->isFake()) {
2062 HasFakeArg = true;
2063 } else if (Args.back()->isOptional()) {
2064 HasOptArg = true;
2065 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00002066 }
2067
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002068 OS << "public:\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002069
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002070 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballman36a53502014-01-16 13:03:14 +00002071
2072 // If there are zero or one spellings, all spelling-related functionality
2073 // can be elided. If all of the spellings share the same name, the spelling
2074 // functionality can also be elided.
2075 bool ElideSpelling = (Spellings.size() <= 1) ||
2076 SpellingNamesAreCommon(Spellings);
2077
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002078 // This maps spelling index values to semantic Spelling enumerants.
2079 SemanticSpellingMap SemanticToSyntacticMap;
Aaron Ballman36a53502014-01-16 13:03:14 +00002080
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002081 if (!ElideSpelling)
2082 OS << CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
Aaron Ballman36a53502014-01-16 13:03:14 +00002083
John McCalla62c1a92015-10-28 00:17:34 +00002084 // Emit CreateImplicit factory methods.
2085 auto emitCreateImplicit = [&](bool emitFake) {
2086 OS << " static " << R.getName() << "Attr *CreateImplicit(";
2087 OS << "ASTContext &Ctx";
2088 if (!ElideSpelling)
2089 OS << ", Spelling S";
2090 for (auto const &ai : Args) {
2091 if (ai->isFake() && !emitFake) continue;
2092 OS << ", ";
2093 ai->writeCtorParameters(OS);
2094 }
2095 OS << ", SourceRange Loc = SourceRange()";
2096 OS << ") {\n";
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002097 OS << " auto *A = new (Ctx) " << R.getName();
John McCalla62c1a92015-10-28 00:17:34 +00002098 OS << "Attr(Loc, Ctx, ";
2099 for (auto const &ai : Args) {
2100 if (ai->isFake() && !emitFake) continue;
2101 ai->writeImplicitCtorArgs(OS);
2102 OS << ", ";
2103 }
2104 OS << (ElideSpelling ? "0" : "S") << ");\n";
2105 OS << " A->setImplicit(true);\n";
2106 OS << " return A;\n }\n\n";
2107 };
Aaron Ballman36a53502014-01-16 13:03:14 +00002108
John McCalla62c1a92015-10-28 00:17:34 +00002109 // Emit a CreateImplicit that takes all the arguments.
2110 emitCreateImplicit(true);
2111
2112 // Emit a CreateImplicit that takes all the non-fake arguments.
2113 if (HasFakeArg) {
2114 emitCreateImplicit(false);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002115 }
Michael Han99315932013-01-24 16:46:58 +00002116
John McCalla62c1a92015-10-28 00:17:34 +00002117 // Emit constructors.
2118 auto emitCtor = [&](bool emitOpt, bool emitFake) {
2119 auto shouldEmitArg = [=](const std::unique_ptr<Argument> &arg) {
2120 if (arg->isFake()) return emitFake;
2121 if (arg->isOptional()) return emitOpt;
2122 return true;
2123 };
Michael Han99315932013-01-24 16:46:58 +00002124
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002125 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002126 for (auto const &ai : Args) {
John McCalla62c1a92015-10-28 00:17:34 +00002127 if (!shouldEmitArg(ai)) continue;
2128 OS << " , ";
2129 ai->writeCtorParameters(OS);
2130 OS << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002131 }
2132
2133 OS << " , ";
Aaron Ballman36a53502014-01-16 13:03:14 +00002134 OS << "unsigned SI\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002135
2136 OS << " )\n";
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002137 OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI, "
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002138 << ( R.getValueAsBit("LateParsed") ? "true" : "false" ) << ", "
2139 << ( R.getValueAsBit("DuplicatesAllowedWhileMerging") ? "true" : "false" ) << ")\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002140
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002141 for (auto const &ai : Args) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002142 OS << " , ";
John McCalla62c1a92015-10-28 00:17:34 +00002143 if (!shouldEmitArg(ai)) {
2144 ai->writeCtorDefaultInitializers(OS);
2145 } else {
2146 ai->writeCtorInitializers(OS);
2147 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002148 OS << "\n";
2149 }
2150
2151 OS << " {\n";
2152
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002153 for (auto const &ai : Args) {
John McCalla62c1a92015-10-28 00:17:34 +00002154 if (!shouldEmitArg(ai)) continue;
2155 ai->writeCtorBody(OS);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002156 }
2157 OS << " }\n\n";
John McCalla62c1a92015-10-28 00:17:34 +00002158 };
2159
2160 // Emit a constructor that includes all the arguments.
2161 // This is necessary for cloning.
2162 emitCtor(true, true);
2163
2164 // Emit a constructor that takes all the non-fake arguments.
2165 if (HasFakeArg) {
2166 emitCtor(true, false);
2167 }
2168
2169 // Emit a constructor that takes all the non-fake, non-optional arguments.
2170 if (HasOptArg) {
2171 emitCtor(false, false);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002172 }
2173
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002174 OS << " " << R.getName() << "Attr *clone(ASTContext &C) const;\n";
Craig Toppercbce6e92014-03-11 06:22:39 +00002175 OS << " void printPretty(raw_ostream &OS,\n"
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002176 << " const PrintingPolicy &Policy) const;\n";
2177 OS << " const char *getSpelling() const;\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002178
2179 if (!ElideSpelling) {
2180 assert(!SemanticToSyntacticMap.empty() && "Empty semantic mapping list");
2181 OS << " Spelling getSemanticSpelling() const {\n";
2182 WriteSemanticSpellingSwitch("SpellingListIndex", SemanticToSyntacticMap,
2183 OS);
2184 OS << " }\n";
2185 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00002186
Michael Hanaf02bbe2013-02-01 01:19:17 +00002187 writeAttrAccessorDefinition(R, OS);
2188
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002189 for (auto const &ai : Args) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002190 ai->writeAccessors(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002191 OS << "\n\n";
Aaron Ballman682ee422013-09-11 19:47:58 +00002192
John McCalla62c1a92015-10-28 00:17:34 +00002193 // Don't write conversion routines for fake arguments.
2194 if (ai->isFake()) continue;
2195
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002196 if (ai->isEnumArg())
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002197 static_cast<const EnumArgument *>(ai.get())->writeConversion(OS);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002198 else if (ai->isVariadicEnumArg())
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002199 static_cast<const VariadicEnumArgument *>(ai.get())
2200 ->writeConversion(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002201 }
2202
Jakob Stoklund Olesen6f2288b62012-01-13 04:57:47 +00002203 OS << R.getValueAsString("AdditionalMembers");
Peter Collingbournebee583f2011-10-06 13:03:08 +00002204 OS << "\n\n";
2205
2206 OS << " static bool classof(const Attr *A) { return A->getKind() == "
2207 << "attr::" << R.getName() << "; }\n";
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00002208
Peter Collingbournebee583f2011-10-06 13:03:08 +00002209 OS << "};\n\n";
2210 }
2211
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002212 OS << "#endif // LLVM_CLANG_ATTR_CLASSES_INC\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002213}
2214
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002215// Emits the class method definitions for attributes.
2216void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002217 emitSourceFileHeader("Attribute classes' member function definitions", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002218
2219 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
Peter Collingbournebee583f2011-10-06 13:03:08 +00002220
Aaron Ballman2f22b942014-05-20 19:47:14 +00002221 for (auto *Attr : Attrs) {
2222 Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002223
2224 if (!R.getValueAsBit("ASTNode"))
2225 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002226
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002227 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
2228 std::vector<std::unique_ptr<Argument>> Args;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002229 for (const auto *Arg : ArgRecords)
2230 Args.emplace_back(createArgument(*Arg, R.getName()));
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002231
2232 for (auto const &ai : Args)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002233 ai->writeAccessorDefinitions(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002234
2235 OS << R.getName() << "Attr *" << R.getName()
2236 << "Attr::clone(ASTContext &C) const {\n";
Hans Wennborg613807b2014-05-31 01:30:30 +00002237 OS << " auto *A = new (C) " << R.getName() << "Attr(getLocation(), C";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002238 for (auto const &ai : Args) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00002239 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002240 ai->writeCloneArgs(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002241 }
Hans Wennborg613807b2014-05-31 01:30:30 +00002242 OS << ", getSpellingListIndex());\n";
2243 OS << " A->Inherited = Inherited;\n";
2244 OS << " A->IsPackExpansion = IsPackExpansion;\n";
2245 OS << " A->Implicit = Implicit;\n";
2246 OS << " return A;\n}\n\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00002247
Michael Han99315932013-01-24 16:46:58 +00002248 writePrettyPrintFunction(R, Args, OS);
Aaron Ballman3e424b52013-12-26 18:30:57 +00002249 writeGetSpellingFunction(R, OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002250 }
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002251
2252 // Instead of relying on virtual dispatch we just create a huge dispatch
2253 // switch. This is both smaller and faster than virtual functions.
2254 auto EmitFunc = [&](const char *Method) {
2255 OS << " switch (getKind()) {\n";
2256 for (const auto *Attr : Attrs) {
2257 const Record &R = *Attr;
2258 if (!R.getValueAsBit("ASTNode"))
2259 continue;
2260
2261 OS << " case attr::" << R.getName() << ":\n";
2262 OS << " return cast<" << R.getName() << "Attr>(this)->" << Method
2263 << ";\n";
2264 }
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002265 OS << " }\n";
2266 OS << " llvm_unreachable(\"Unexpected attribute kind!\");\n";
2267 OS << "}\n\n";
2268 };
2269
2270 OS << "const char *Attr::getSpelling() const {\n";
2271 EmitFunc("getSpelling()");
2272
2273 OS << "Attr *Attr::clone(ASTContext &C) const {\n";
2274 EmitFunc("clone(C)");
2275
2276 OS << "void Attr::printPretty(raw_ostream &OS, "
2277 "const PrintingPolicy &Policy) const {\n";
2278 EmitFunc("printPretty(OS, Policy)");
Peter Collingbournebee583f2011-10-06 13:03:08 +00002279}
2280
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002281} // end namespace clang
2282
John McCall2225c8b2016-03-01 00:18:05 +00002283static void emitAttrList(raw_ostream &OS, StringRef Class,
Peter Collingbournebee583f2011-10-06 13:03:08 +00002284 const std::vector<Record*> &AttrList) {
John McCall2225c8b2016-03-01 00:18:05 +00002285 for (auto Cur : AttrList) {
2286 OS << Class << "(" << Cur->getName() << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002287 }
2288}
2289
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002290// Determines if an attribute has a Pragma spelling.
2291static bool AttrHasPragmaSpelling(const Record *R) {
2292 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
George Burgess IV1881a572016-12-01 00:13:18 +00002293 return llvm::find_if(Spellings, [](const FlattenedSpelling &S) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002294 return S.variety() == "Pragma";
2295 }) != Spellings.end();
2296}
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002297
John McCall2225c8b2016-03-01 00:18:05 +00002298namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002299
John McCall2225c8b2016-03-01 00:18:05 +00002300 struct AttrClassDescriptor {
2301 const char * const MacroName;
2302 const char * const TableGenName;
2303 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002304
2305} // end anonymous namespace
John McCall2225c8b2016-03-01 00:18:05 +00002306
2307static const AttrClassDescriptor AttrClassDescriptors[] = {
2308 { "ATTR", "Attr" },
Richard Smith4f902c72016-03-08 00:32:55 +00002309 { "STMT_ATTR", "StmtAttr" },
John McCall2225c8b2016-03-01 00:18:05 +00002310 { "INHERITABLE_ATTR", "InheritableAttr" },
John McCall477f2bb2016-03-03 06:39:32 +00002311 { "INHERITABLE_PARAM_ATTR", "InheritableParamAttr" },
2312 { "PARAMETER_ABI_ATTR", "ParameterABIAttr" }
John McCall2225c8b2016-03-01 00:18:05 +00002313};
2314
2315static void emitDefaultDefine(raw_ostream &OS, StringRef name,
2316 const char *superName) {
2317 OS << "#ifndef " << name << "\n";
2318 OS << "#define " << name << "(NAME) ";
2319 if (superName) OS << superName << "(NAME)";
2320 OS << "\n#endif\n\n";
2321}
2322
2323namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002324
John McCall2225c8b2016-03-01 00:18:05 +00002325 /// A class of attributes.
2326 struct AttrClass {
2327 const AttrClassDescriptor &Descriptor;
2328 Record *TheRecord;
2329 AttrClass *SuperClass = nullptr;
2330 std::vector<AttrClass*> SubClasses;
2331 std::vector<Record*> Attrs;
2332
2333 AttrClass(const AttrClassDescriptor &Descriptor, Record *R)
2334 : Descriptor(Descriptor), TheRecord(R) {}
2335
2336 void emitDefaultDefines(raw_ostream &OS) const {
2337 // Default the macro unless this is a root class (i.e. Attr).
2338 if (SuperClass) {
2339 emitDefaultDefine(OS, Descriptor.MacroName,
2340 SuperClass->Descriptor.MacroName);
2341 }
2342 }
2343
2344 void emitUndefs(raw_ostream &OS) const {
2345 OS << "#undef " << Descriptor.MacroName << "\n";
2346 }
2347
2348 void emitAttrList(raw_ostream &OS) const {
2349 for (auto SubClass : SubClasses) {
2350 SubClass->emitAttrList(OS);
2351 }
2352
2353 ::emitAttrList(OS, Descriptor.MacroName, Attrs);
2354 }
2355
2356 void classifyAttrOnRoot(Record *Attr) {
2357 bool result = classifyAttr(Attr);
2358 assert(result && "failed to classify on root"); (void) result;
2359 }
2360
2361 void emitAttrRange(raw_ostream &OS) const {
2362 OS << "ATTR_RANGE(" << Descriptor.TableGenName
2363 << ", " << getFirstAttr()->getName()
2364 << ", " << getLastAttr()->getName() << ")\n";
2365 }
2366
2367 private:
2368 bool classifyAttr(Record *Attr) {
2369 // Check all the subclasses.
2370 for (auto SubClass : SubClasses) {
2371 if (SubClass->classifyAttr(Attr))
2372 return true;
2373 }
2374
2375 // It's not more specific than this class, but it might still belong here.
2376 if (Attr->isSubClassOf(TheRecord)) {
2377 Attrs.push_back(Attr);
2378 return true;
2379 }
2380
2381 return false;
2382 }
2383
2384 Record *getFirstAttr() const {
2385 if (!SubClasses.empty())
2386 return SubClasses.front()->getFirstAttr();
2387 return Attrs.front();
2388 }
2389
2390 Record *getLastAttr() const {
2391 if (!Attrs.empty())
2392 return Attrs.back();
2393 return SubClasses.back()->getLastAttr();
2394 }
2395 };
2396
2397 /// The entire hierarchy of attribute classes.
2398 class AttrClassHierarchy {
2399 std::vector<std::unique_ptr<AttrClass>> Classes;
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002400
John McCall2225c8b2016-03-01 00:18:05 +00002401 public:
2402 AttrClassHierarchy(RecordKeeper &Records) {
2403 // Find records for all the classes.
2404 for (auto &Descriptor : AttrClassDescriptors) {
2405 Record *ClassRecord = Records.getClass(Descriptor.TableGenName);
2406 AttrClass *Class = new AttrClass(Descriptor, ClassRecord);
2407 Classes.emplace_back(Class);
2408 }
2409
2410 // Link up the hierarchy.
2411 for (auto &Class : Classes) {
2412 if (AttrClass *SuperClass = findSuperClass(Class->TheRecord)) {
2413 Class->SuperClass = SuperClass;
2414 SuperClass->SubClasses.push_back(Class.get());
2415 }
2416 }
2417
2418#ifndef NDEBUG
2419 for (auto i = Classes.begin(), e = Classes.end(); i != e; ++i) {
2420 assert((i == Classes.begin()) == ((*i)->SuperClass == nullptr) &&
2421 "only the first class should be a root class!");
2422 }
2423#endif
2424 }
2425
2426 void emitDefaultDefines(raw_ostream &OS) const {
2427 for (auto &Class : Classes) {
2428 Class->emitDefaultDefines(OS);
2429 }
2430 }
2431
2432 void emitUndefs(raw_ostream &OS) const {
2433 for (auto &Class : Classes) {
2434 Class->emitUndefs(OS);
2435 }
2436 }
2437
2438 void emitAttrLists(raw_ostream &OS) const {
2439 // Just start from the root class.
2440 Classes[0]->emitAttrList(OS);
2441 }
2442
2443 void emitAttrRanges(raw_ostream &OS) const {
2444 for (auto &Class : Classes)
2445 Class->emitAttrRange(OS);
2446 }
2447
2448 void classifyAttr(Record *Attr) {
2449 // Add the attribute to the root class.
2450 Classes[0]->classifyAttrOnRoot(Attr);
2451 }
2452
2453 private:
2454 AttrClass *findClassByRecord(Record *R) const {
2455 for (auto &Class : Classes) {
2456 if (Class->TheRecord == R)
2457 return Class.get();
2458 }
2459 return nullptr;
2460 }
2461
2462 AttrClass *findSuperClass(Record *R) const {
2463 // TableGen flattens the superclass list, so we just need to walk it
2464 // in reverse.
2465 auto SuperClasses = R->getSuperClasses();
2466 for (signed i = 0, e = SuperClasses.size(); i != e; ++i) {
2467 auto SuperClass = findClassByRecord(SuperClasses[e - i - 1].first);
2468 if (SuperClass) return SuperClass;
2469 }
2470 return nullptr;
2471 }
2472 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002473
2474} // end anonymous namespace
John McCall2225c8b2016-03-01 00:18:05 +00002475
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002476namespace clang {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002477
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002478// Emits the enumeration list for attributes.
2479void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002480 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002481
John McCall2225c8b2016-03-01 00:18:05 +00002482 AttrClassHierarchy Hierarchy(Records);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002483
John McCall2225c8b2016-03-01 00:18:05 +00002484 // Add defaulting macro definitions.
2485 Hierarchy.emitDefaultDefines(OS);
2486 emitDefaultDefine(OS, "PRAGMA_SPELLING_ATTR", nullptr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002487
John McCall2225c8b2016-03-01 00:18:05 +00002488 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
2489 std::vector<Record *> PragmaAttrs;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002490 for (auto *Attr : Attrs) {
2491 if (!Attr->getValueAsBit("ASTNode"))
Douglas Gregorb2daf842012-05-02 15:56:52 +00002492 continue;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002493
John McCall2225c8b2016-03-01 00:18:05 +00002494 // Add the attribute to the ad-hoc groups.
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002495 if (AttrHasPragmaSpelling(Attr))
2496 PragmaAttrs.push_back(Attr);
2497
John McCall2225c8b2016-03-01 00:18:05 +00002498 // Place it in the hierarchy.
2499 Hierarchy.classifyAttr(Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002500 }
2501
John McCall2225c8b2016-03-01 00:18:05 +00002502 // Emit the main attribute list.
2503 Hierarchy.emitAttrLists(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002504
John McCall2225c8b2016-03-01 00:18:05 +00002505 // Emit the ad hoc groups.
2506 emitAttrList(OS, "PRAGMA_SPELLING_ATTR", PragmaAttrs);
2507
2508 // Emit the attribute ranges.
2509 OS << "#ifdef ATTR_RANGE\n";
2510 Hierarchy.emitAttrRanges(OS);
2511 OS << "#undef ATTR_RANGE\n";
2512 OS << "#endif\n";
2513
2514 Hierarchy.emitUndefs(OS);
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002515 OS << "#undef PRAGMA_SPELLING_ATTR\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002516}
2517
Alex Lorenz9e7bf162017-04-18 14:33:39 +00002518// Emits the enumeration list for attributes.
2519void EmitClangAttrSubjectMatchRuleList(RecordKeeper &Records, raw_ostream &OS) {
2520 emitSourceFileHeader(
2521 "List of all attribute subject matching rules that Clang recognizes", OS);
2522 PragmaClangAttributeSupport &PragmaAttributeSupport =
2523 getPragmaAttributeSupport(Records);
2524 emitDefaultDefine(OS, "ATTR_MATCH_RULE", nullptr);
2525 PragmaAttributeSupport.emitMatchRuleList(OS);
2526 OS << "#undef ATTR_MATCH_RULE\n";
2527}
2528
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002529// Emits the code to read an attribute from a precompiled header.
2530void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002531 emitSourceFileHeader("Attribute deserialization code", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002532
2533 Record *InhClass = Records.getClass("InheritableAttr");
2534 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
2535 ArgRecords;
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002536 std::vector<std::unique_ptr<Argument>> Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002537
2538 OS << " switch (Kind) {\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002539 for (const auto *Attr : Attrs) {
2540 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002541 if (!R.getValueAsBit("ASTNode"))
2542 continue;
2543
Peter Collingbournebee583f2011-10-06 13:03:08 +00002544 OS << " case attr::" << R.getName() << ": {\n";
2545 if (R.isSubClassOf(InhClass))
David L. Jones267b8842017-01-24 01:04:30 +00002546 OS << " bool isInherited = Record.readInt();\n";
2547 OS << " bool isImplicit = Record.readInt();\n";
2548 OS << " unsigned Spelling = Record.readInt();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002549 ArgRecords = R.getValueAsListOfDefs("Args");
2550 Args.clear();
Aaron Ballman2f22b942014-05-20 19:47:14 +00002551 for (const auto *Arg : ArgRecords) {
2552 Args.emplace_back(createArgument(*Arg, R.getName()));
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002553 Args.back()->writePCHReadDecls(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002554 }
2555 OS << " New = new (Context) " << R.getName() << "Attr(Range, Context";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002556 for (auto const &ri : Args) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00002557 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002558 ri->writePCHReadArgs(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002559 }
Aaron Ballman36a53502014-01-16 13:03:14 +00002560 OS << ", Spelling);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002561 if (R.isSubClassOf(InhClass))
2562 OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002563 OS << " New->setImplicit(isImplicit);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002564 OS << " break;\n";
2565 OS << " }\n";
2566 }
2567 OS << " }\n";
2568}
2569
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002570// Emits the code to write an attribute to a precompiled header.
2571void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002572 emitSourceFileHeader("Attribute serialization code", OS);
2573
Peter Collingbournebee583f2011-10-06 13:03:08 +00002574 Record *InhClass = Records.getClass("InheritableAttr");
2575 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002576
2577 OS << " switch (A->getKind()) {\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002578 for (const auto *Attr : Attrs) {
2579 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002580 if (!R.getValueAsBit("ASTNode"))
2581 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002582 OS << " case attr::" << R.getName() << ": {\n";
2583 Args = R.getValueAsListOfDefs("Args");
2584 if (R.isSubClassOf(InhClass) || !Args.empty())
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002585 OS << " const auto *SA = cast<" << R.getName()
Peter Collingbournebee583f2011-10-06 13:03:08 +00002586 << "Attr>(A);\n";
2587 if (R.isSubClassOf(InhClass))
2588 OS << " Record.push_back(SA->isInherited());\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002589 OS << " Record.push_back(A->isImplicit());\n";
2590 OS << " Record.push_back(A->getSpellingListIndex());\n";
2591
Aaron Ballman2f22b942014-05-20 19:47:14 +00002592 for (const auto *Arg : Args)
2593 createArgument(*Arg, R.getName())->writePCHWrite(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002594 OS << " break;\n";
2595 OS << " }\n";
2596 }
2597 OS << " }\n";
2598}
2599
Bob Wilson0058b822015-07-20 22:57:36 +00002600// Generate a conditional expression to check if the current target satisfies
2601// the conditions for a TargetSpecificAttr record, and append the code for
2602// those checks to the Test string. If the FnName string pointer is non-null,
2603// append a unique suffix to distinguish this set of target checks from other
2604// TargetSpecificAttr records.
2605static void GenerateTargetSpecificAttrChecks(const Record *R,
2606 std::vector<std::string> &Arches,
2607 std::string &Test,
2608 std::string *FnName) {
2609 // It is assumed that there will be an llvm::Triple object
2610 // named "T" and a TargetInfo object named "Target" within
2611 // scope that can be used to determine whether the attribute exists in
2612 // a given target.
2613 Test += "(";
2614
2615 for (auto I = Arches.begin(), E = Arches.end(); I != E; ++I) {
2616 std::string Part = *I;
2617 Test += "T.getArch() == llvm::Triple::" + Part;
2618 if (I + 1 != E)
2619 Test += " || ";
2620 if (FnName)
2621 *FnName += Part;
2622 }
2623 Test += ")";
2624
2625 // If the attribute is specific to particular OSes, check those.
2626 if (!R->isValueUnset("OSes")) {
2627 // We know that there was at least one arch test, so we need to and in the
2628 // OS tests.
2629 Test += " && (";
2630 std::vector<std::string> OSes = R->getValueAsListOfStrings("OSes");
2631 for (auto I = OSes.begin(), E = OSes.end(); I != E; ++I) {
2632 std::string Part = *I;
2633
2634 Test += "T.getOS() == llvm::Triple::" + Part;
2635 if (I + 1 != E)
2636 Test += " || ";
2637 if (FnName)
2638 *FnName += Part;
2639 }
2640 Test += ")";
2641 }
2642
2643 // If one or more CXX ABIs are specified, check those as well.
2644 if (!R->isValueUnset("CXXABIs")) {
2645 Test += " && (";
2646 std::vector<std::string> CXXABIs = R->getValueAsListOfStrings("CXXABIs");
2647 for (auto I = CXXABIs.begin(), E = CXXABIs.end(); I != E; ++I) {
2648 std::string Part = *I;
2649 Test += "Target.getCXXABI().getKind() == TargetCXXABI::" + Part;
2650 if (I + 1 != E)
2651 Test += " || ";
2652 if (FnName)
2653 *FnName += Part;
2654 }
2655 Test += ")";
2656 }
2657}
2658
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002659static void GenerateHasAttrSpellingStringSwitch(
2660 const std::vector<Record *> &Attrs, raw_ostream &OS,
2661 const std::string &Variety = "", const std::string &Scope = "") {
2662 for (const auto *Attr : Attrs) {
Aaron Ballmana0344c52014-11-14 13:44:02 +00002663 // C++11-style attributes have specific version information associated with
2664 // them. If the attribute has no scope, the version information must not
2665 // have the default value (1), as that's incorrect. Instead, the unscoped
2666 // attribute version information should be taken from the SD-6 standing
2667 // document, which can be found at:
2668 // https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations
2669 int Version = 1;
2670
2671 if (Variety == "CXX11") {
2672 std::vector<Record *> Spellings = Attr->getValueAsListOfDefs("Spellings");
2673 for (const auto &Spelling : Spellings) {
2674 if (Spelling->getValueAsString("Variety") == "CXX11") {
2675 Version = static_cast<int>(Spelling->getValueAsInt("Version"));
2676 if (Scope.empty() && Version == 1)
2677 PrintError(Spelling->getLoc(), "C++ standard attributes must "
2678 "have valid version information.");
2679 break;
2680 }
2681 }
2682 }
2683
Aaron Ballman0fa06d82014-01-09 22:57:44 +00002684 std::string Test;
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002685 if (Attr->isSubClassOf("TargetSpecificAttr")) {
2686 const Record *R = Attr->getValueAsDef("Target");
Aaron Ballman0fa06d82014-01-09 22:57:44 +00002687 std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches");
Hans Wennborgdcfba332015-10-06 23:40:43 +00002688 GenerateTargetSpecificAttrChecks(R, Arches, Test, nullptr);
Bob Wilson7c730832015-07-20 22:57:31 +00002689
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002690 // If this is the C++11 variety, also add in the LangOpts test.
2691 if (Variety == "CXX11")
2692 Test += " && LangOpts.CPlusPlus11";
2693 } else if (Variety == "CXX11")
2694 // C++11 mode should be checked against LangOpts, which is presumed to be
2695 // present in the caller.
2696 Test = "LangOpts.CPlusPlus11";
Aaron Ballman0fa06d82014-01-09 22:57:44 +00002697
Aaron Ballmana0344c52014-11-14 13:44:02 +00002698 std::string TestStr =
Aaron Ballman28afa182014-11-17 18:17:19 +00002699 !Test.empty() ? Test + " ? " + llvm::itostr(Version) + " : 0" : "1";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002700 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002701 for (const auto &S : Spellings)
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002702 if (Variety.empty() || (Variety == S.variety() &&
2703 (Scope.empty() || Scope == S.nameSpace())))
Aaron Ballmana0344c52014-11-14 13:44:02 +00002704 OS << " .Case(\"" << S.name() << "\", " << TestStr << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002705 }
Aaron Ballmana0344c52014-11-14 13:44:02 +00002706 OS << " .Default(0);\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002707}
2708
2709// Emits the list of spellings for attributes.
2710void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
2711 emitSourceFileHeader("Code to implement the __has_attribute logic", OS);
2712
2713 // Separate all of the attributes out into four group: generic, C++11, GNU,
2714 // and declspecs. Then generate a big switch statement for each of them.
2715 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Nico Weber20e08042016-09-03 02:55:10 +00002716 std::vector<Record *> Declspec, Microsoft, GNU, Pragma;
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002717 std::map<std::string, std::vector<Record *>> CXX;
2718
2719 // Walk over the list of all attributes, and split them out based on the
2720 // spelling variety.
2721 for (auto *R : Attrs) {
2722 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
2723 for (const auto &SI : Spellings) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00002724 const std::string &Variety = SI.variety();
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002725 if (Variety == "GNU")
2726 GNU.push_back(R);
2727 else if (Variety == "Declspec")
2728 Declspec.push_back(R);
Nico Weber20e08042016-09-03 02:55:10 +00002729 else if (Variety == "Microsoft")
2730 Microsoft.push_back(R);
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002731 else if (Variety == "CXX11")
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002732 CXX[SI.nameSpace()].push_back(R);
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002733 else if (Variety == "Pragma")
2734 Pragma.push_back(R);
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002735 }
2736 }
2737
Bob Wilson7c730832015-07-20 22:57:31 +00002738 OS << "const llvm::Triple &T = Target.getTriple();\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002739 OS << "switch (Syntax) {\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002740 OS << "case AttrSyntax::GNU:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002741 OS << " return llvm::StringSwitch<int>(Name)\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002742 GenerateHasAttrSpellingStringSwitch(GNU, OS, "GNU");
2743 OS << "case AttrSyntax::Declspec:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002744 OS << " return llvm::StringSwitch<int>(Name)\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002745 GenerateHasAttrSpellingStringSwitch(Declspec, OS, "Declspec");
Nico Weber20e08042016-09-03 02:55:10 +00002746 OS << "case AttrSyntax::Microsoft:\n";
2747 OS << " return llvm::StringSwitch<int>(Name)\n";
2748 GenerateHasAttrSpellingStringSwitch(Microsoft, OS, "Microsoft");
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002749 OS << "case AttrSyntax::Pragma:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002750 OS << " return llvm::StringSwitch<int>(Name)\n";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002751 GenerateHasAttrSpellingStringSwitch(Pragma, OS, "Pragma");
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002752 OS << "case AttrSyntax::CXX: {\n";
2753 // C++11-style attributes are further split out based on the Scope.
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002754 for (auto I = CXX.cbegin(), E = CXX.cend(); I != E; ++I) {
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002755 if (I != CXX.begin())
2756 OS << " else ";
2757 if (I->first.empty())
2758 OS << "if (!Scope || Scope->getName() == \"\") {\n";
2759 else
2760 OS << "if (Scope->getName() == \"" << I->first << "\") {\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002761 OS << " return llvm::StringSwitch<int>(Name)\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002762 GenerateHasAttrSpellingStringSwitch(I->second, OS, "CXX11", I->first);
2763 OS << "}";
2764 }
2765 OS << "\n}\n";
2766 OS << "}\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002767}
2768
Michael Han99315932013-01-24 16:46:58 +00002769void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002770 emitSourceFileHeader("Code to translate different attribute spellings "
2771 "into internal identifiers", OS);
Michael Han99315932013-01-24 16:46:58 +00002772
John McCall2225c8b2016-03-01 00:18:05 +00002773 OS << " switch (AttrKind) {\n";
Michael Han99315932013-01-24 16:46:58 +00002774
Aaron Ballman64e69862013-12-15 13:05:48 +00002775 ParsedAttrMap Attrs = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002776 for (const auto &I : Attrs) {
Aaron Ballman2f22b942014-05-20 19:47:14 +00002777 const Record &R = *I.second;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002778 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002779 OS << " case AT_" << I.first << ": {\n";
Richard Smith852e9ce2013-11-27 01:46:48 +00002780 for (unsigned I = 0; I < Spellings.size(); ++ I) {
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002781 OS << " if (Name == \"" << Spellings[I].name() << "\" && "
2782 << "SyntaxUsed == "
2783 << StringSwitch<unsigned>(Spellings[I].variety())
2784 .Case("GNU", 0)
2785 .Case("CXX11", 1)
2786 .Case("Declspec", 2)
Nico Weber20e08042016-09-03 02:55:10 +00002787 .Case("Microsoft", 3)
2788 .Case("Keyword", 4)
2789 .Case("Pragma", 5)
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002790 .Default(0)
2791 << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n"
2792 << " return " << I << ";\n";
Michael Han99315932013-01-24 16:46:58 +00002793 }
Richard Smith852e9ce2013-11-27 01:46:48 +00002794
2795 OS << " break;\n";
2796 OS << " }\n";
Michael Han99315932013-01-24 16:46:58 +00002797 }
2798
2799 OS << " }\n";
Aaron Ballman64e69862013-12-15 13:05:48 +00002800 OS << " return 0;\n";
Michael Han99315932013-01-24 16:46:58 +00002801}
2802
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002803// Emits code used by RecursiveASTVisitor to visit attributes
2804void EmitClangAttrASTVisitor(RecordKeeper &Records, raw_ostream &OS) {
2805 emitSourceFileHeader("Used by RecursiveASTVisitor to visit attributes.", OS);
2806
2807 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2808
2809 // Write method declarations for Traverse* methods.
2810 // We emit this here because we only generate methods for attributes that
2811 // are declared as ASTNodes.
2812 OS << "#ifdef ATTR_VISITOR_DECLS_ONLY\n\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002813 for (const auto *Attr : Attrs) {
2814 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002815 if (!R.getValueAsBit("ASTNode"))
2816 continue;
2817 OS << " bool Traverse"
2818 << R.getName() << "Attr(" << R.getName() << "Attr *A);\n";
2819 OS << " bool Visit"
2820 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
2821 << " return true; \n"
Hans Wennborg4afe5042015-07-22 20:46:26 +00002822 << " }\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002823 }
2824 OS << "\n#else // ATTR_VISITOR_DECLS_ONLY\n\n";
2825
2826 // Write individual Traverse* methods for each attribute class.
Aaron Ballman2f22b942014-05-20 19:47:14 +00002827 for (const auto *Attr : Attrs) {
2828 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002829 if (!R.getValueAsBit("ASTNode"))
2830 continue;
2831
2832 OS << "template <typename Derived>\n"
DeLesley Hutchinsbb79c332013-12-30 21:03:02 +00002833 << "bool VISITORCLASS<Derived>::Traverse"
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002834 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
2835 << " if (!getDerived().VisitAttr(A))\n"
2836 << " return false;\n"
2837 << " if (!getDerived().Visit" << R.getName() << "Attr(A))\n"
2838 << " return false;\n";
2839
2840 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman2f22b942014-05-20 19:47:14 +00002841 for (const auto *Arg : ArgRecords)
2842 createArgument(*Arg, R.getName())->writeASTVisitorTraversal(OS);
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002843
2844 OS << " return true;\n";
2845 OS << "}\n\n";
2846 }
2847
2848 // Write generic Traverse routine
2849 OS << "template <typename Derived>\n"
DeLesley Hutchinsbb79c332013-12-30 21:03:02 +00002850 << "bool VISITORCLASS<Derived>::TraverseAttr(Attr *A) {\n"
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002851 << " if (!A)\n"
2852 << " return true;\n"
2853 << "\n"
John McCall2225c8b2016-03-01 00:18:05 +00002854 << " switch (A->getKind()) {\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002855
Aaron Ballman2f22b942014-05-20 19:47:14 +00002856 for (const auto *Attr : Attrs) {
2857 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002858 if (!R.getValueAsBit("ASTNode"))
2859 continue;
2860
2861 OS << " case attr::" << R.getName() << ":\n"
2862 << " return getDerived().Traverse" << R.getName() << "Attr("
2863 << "cast<" << R.getName() << "Attr>(A));\n";
2864 }
John McCall5d7cf772016-03-01 02:09:20 +00002865 OS << " }\n"; // end switch
2866 OS << " llvm_unreachable(\"bad attribute kind\");\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002867 OS << "}\n"; // end function
2868 OS << "#endif // ATTR_VISITOR_DECLS_ONLY\n";
2869}
2870
Erich Keanea32910d2017-03-23 18:51:54 +00002871void EmitClangAttrTemplateInstantiateHelper(const std::vector<Record *> &Attrs,
2872 raw_ostream &OS,
2873 bool AppliesToDecl) {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002874
Erich Keanea32910d2017-03-23 18:51:54 +00002875 OS << " switch (At->getKind()) {\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002876 for (const auto *Attr : Attrs) {
2877 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002878 if (!R.getValueAsBit("ASTNode"))
2879 continue;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002880 OS << " case attr::" << R.getName() << ": {\n";
Erich Keanea32910d2017-03-23 18:51:54 +00002881 bool ShouldClone = R.getValueAsBit("Clone") &&
2882 (!AppliesToDecl ||
2883 R.getValueAsBit("MeaningfulToClassTemplateDefinition"));
Rafael Espindola7f90b7d2012-05-15 14:09:55 +00002884
2885 if (!ShouldClone) {
Hans Wennborg59dbe862015-09-29 20:56:43 +00002886 OS << " return nullptr;\n";
Rafael Espindola7f90b7d2012-05-15 14:09:55 +00002887 OS << " }\n";
2888 continue;
2889 }
2890
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002891 OS << " const auto *A = cast<"
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002892 << R.getName() << "Attr>(At);\n";
2893 bool TDependent = R.getValueAsBit("TemplateDependent");
2894
2895 if (!TDependent) {
2896 OS << " return A->clone(C);\n";
2897 OS << " }\n";
2898 continue;
2899 }
2900
2901 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002902 std::vector<std::unique_ptr<Argument>> Args;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002903 Args.reserve(ArgRecords.size());
2904
Aaron Ballman2f22b942014-05-20 19:47:14 +00002905 for (const auto *ArgRecord : ArgRecords)
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002906 Args.emplace_back(createArgument(*ArgRecord, R.getName()));
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002907
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002908 for (auto const &ai : Args)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002909 ai->writeTemplateInstantiation(OS);
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002910
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002911 OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002912 for (auto const &ai : Args) {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002913 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002914 ai->writeTemplateInstantiationArgs(OS);
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002915 }
Aaron Ballman36a53502014-01-16 13:03:14 +00002916 OS << ", A->getSpellingListIndex());\n }\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002917 }
2918 OS << " } // end switch\n"
2919 << " llvm_unreachable(\"Unknown attribute!\");\n"
Erich Keanea32910d2017-03-23 18:51:54 +00002920 << " return nullptr;\n";
2921}
2922
2923// Emits code to instantiate dependent attributes on templates.
2924void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) {
2925 emitSourceFileHeader("Template instantiation code for attributes", OS);
2926
2927 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2928
2929 OS << "namespace clang {\n"
2930 << "namespace sema {\n\n"
2931 << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, "
2932 << "Sema &S,\n"
2933 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n";
2934 EmitClangAttrTemplateInstantiateHelper(Attrs, OS, /*AppliesToDecl*/false);
2935 OS << "}\n\n"
2936 << "Attr *instantiateTemplateAttributeForDecl(const Attr *At,\n"
2937 << " ASTContext &C, Sema &S,\n"
2938 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n";
2939 EmitClangAttrTemplateInstantiateHelper(Attrs, OS, /*AppliesToDecl*/true);
2940 OS << "}\n\n"
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00002941 << "} // end namespace sema\n"
2942 << "} // end namespace clang\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002943}
2944
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002945// Emits the list of parsed attributes.
2946void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) {
2947 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
2948
2949 OS << "#ifndef PARSED_ATTR\n";
2950 OS << "#define PARSED_ATTR(NAME) NAME\n";
2951 OS << "#endif\n\n";
2952
2953 ParsedAttrMap Names = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002954 for (const auto &I : Names) {
2955 OS << "PARSED_ATTR(" << I.first << ")\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002956 }
2957}
2958
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002959static bool isArgVariadic(const Record &R, StringRef AttrName) {
2960 return createArgument(R, AttrName)->isVariadic();
2961}
2962
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002963static void emitArgInfo(const Record &R, std::stringstream &OS) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002964 // This function will count the number of arguments specified for the
2965 // attribute and emit the number of required arguments followed by the
2966 // number of optional arguments.
2967 std::vector<Record *> Args = R.getValueAsListOfDefs("Args");
2968 unsigned ArgCount = 0, OptCount = 0;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002969 bool HasVariadic = false;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002970 for (const auto *Arg : Args) {
George Burgess IV8a36ace2016-12-01 17:52:39 +00002971 // If the arg is fake, it's the user's job to supply it: general parsing
2972 // logic shouldn't need to know anything about it.
2973 if (Arg->getValueAsBit("Fake"))
2974 continue;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002975 Arg->getValueAsBit("Optional") ? ++OptCount : ++ArgCount;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002976 if (!HasVariadic && isArgVariadic(*Arg, R.getName()))
2977 HasVariadic = true;
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002978 }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002979
2980 // If there is a variadic argument, we will set the optional argument count
2981 // to its largest value. Since it's currently a 4-bit number, we set it to 15.
2982 OS << ArgCount << ", " << (HasVariadic ? 15 : OptCount);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002983}
2984
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002985static void GenerateDefaultAppertainsTo(raw_ostream &OS) {
Aaron Ballman93b5cc62013-12-02 19:36:42 +00002986 OS << "static bool defaultAppertainsTo(Sema &, const AttributeList &,";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002987 OS << "const Decl *) {\n";
2988 OS << " return true;\n";
2989 OS << "}\n\n";
2990}
2991
2992static std::string CalculateDiagnostic(const Record &S) {
2993 // If the SubjectList object has a custom diagnostic associated with it,
2994 // return that directly.
2995 std::string CustomDiag = S.getValueAsString("CustomDiag");
2996 if (!CustomDiag.empty())
2997 return CustomDiag;
2998
2999 // Given the list of subjects, determine what diagnostic best fits.
3000 enum {
3001 Func = 1U << 0,
3002 Var = 1U << 1,
3003 ObjCMethod = 1U << 2,
3004 Param = 1U << 3,
3005 Class = 1U << 4,
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00003006 GenericRecord = 1U << 5,
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003007 Type = 1U << 6,
3008 ObjCIVar = 1U << 7,
3009 ObjCProp = 1U << 8,
3010 ObjCInterface = 1U << 9,
3011 Block = 1U << 10,
3012 Namespace = 1U << 11,
Aaron Ballman981ba242014-05-20 14:10:53 +00003013 Field = 1U << 12,
3014 CXXMethod = 1U << 13,
Alexis Hunt724f14e2014-11-28 00:53:20 +00003015 ObjCProtocol = 1U << 14,
Alex Lorenz24952fb2017-04-19 15:52:11 +00003016 Enum = 1U << 15,
3017 Named = 1U << 16,
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003018 };
3019 uint32_t SubMask = 0;
3020
3021 std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects");
Aaron Ballman2f22b942014-05-20 19:47:14 +00003022 for (const auto *Subject : Subjects) {
3023 const Record &R = *Subject;
Aaron Ballman80469032013-11-29 14:57:58 +00003024 std::string Name;
3025
3026 if (R.isSubClassOf("SubsetSubject")) {
3027 PrintError(R.getLoc(), "SubsetSubjects should use a custom diagnostic");
3028 // As a fallback, look through the SubsetSubject to see what its base
3029 // type is, and use that. This needs to be updated if SubsetSubjects
3030 // are allowed within other SubsetSubjects.
3031 Name = R.getValueAsDef("Base")->getName();
3032 } else
3033 Name = R.getName();
3034
3035 uint32_t V = StringSwitch<uint32_t>(Name)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003036 .Case("Function", Func)
3037 .Case("Var", Var)
3038 .Case("ObjCMethod", ObjCMethod)
3039 .Case("ParmVar", Param)
3040 .Case("TypedefName", Type)
3041 .Case("ObjCIvar", ObjCIVar)
3042 .Case("ObjCProperty", ObjCProp)
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00003043 .Case("Record", GenericRecord)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003044 .Case("ObjCInterface", ObjCInterface)
Ted Kremenekd980da22013-12-10 19:43:42 +00003045 .Case("ObjCProtocol", ObjCProtocol)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003046 .Case("Block", Block)
3047 .Case("CXXRecord", Class)
3048 .Case("Namespace", Namespace)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003049 .Case("Field", Field)
3050 .Case("CXXMethod", CXXMethod)
Alexis Hunt724f14e2014-11-28 00:53:20 +00003051 .Case("Enum", Enum)
Alex Lorenz24952fb2017-04-19 15:52:11 +00003052 .Case("Named", Named)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003053 .Default(0);
3054 if (!V) {
3055 // Something wasn't in our mapping, so be helpful and let the developer
3056 // know about it.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003057 PrintFatalError(R.getLoc(), "Unknown subject type: " + R.getName());
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003058 return "";
3059 }
3060
3061 SubMask |= V;
3062 }
3063
3064 switch (SubMask) {
3065 // For the simple cases where there's only a single entry in the mask, we
3066 // don't have to resort to bit fiddling.
3067 case Func: return "ExpectedFunction";
3068 case Var: return "ExpectedVariable";
3069 case Param: return "ExpectedParameter";
3070 case Class: return "ExpectedClass";
Alexis Hunt724f14e2014-11-28 00:53:20 +00003071 case Enum: return "ExpectedEnum";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003072 case CXXMethod:
3073 // FIXME: Currently, this maps to ExpectedMethod based on existing code,
3074 // but should map to something a bit more accurate at some point.
3075 case ObjCMethod: return "ExpectedMethod";
3076 case Type: return "ExpectedType";
3077 case ObjCInterface: return "ExpectedObjectiveCInterface";
Ted Kremenekd980da22013-12-10 19:43:42 +00003078 case ObjCProtocol: return "ExpectedObjectiveCProtocol";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003079
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00003080 // "GenericRecord" means struct, union or class; check the language options
3081 // and if not compiling for C++, strip off the class part. Note that this
3082 // relies on the fact that the context for this declares "Sema &S".
3083 case GenericRecord:
Aaron Ballman17046b82013-11-27 19:16:55 +00003084 return "(S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass : "
3085 "ExpectedStructOrUnion)";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003086 case Func | ObjCMethod | Block: return "ExpectedFunctionMethodOrBlock";
3087 case Func | ObjCMethod | Class: return "ExpectedFunctionMethodOrClass";
3088 case Func | Param:
3089 case Func | ObjCMethod | Param: return "ExpectedFunctionMethodOrParameter";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003090 case Func | ObjCMethod: return "ExpectedFunctionOrMethod";
3091 case Func | Var: return "ExpectedVariableOrFunction";
Aaron Ballman604dfec2013-12-02 17:07:07 +00003092
3093 // If not compiling for C++, the class portion does not apply.
3094 case Func | Var | Class:
3095 return "(S.getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass : "
3096 "ExpectedVariableOrFunction)";
3097
Saleem Abdulrasool511f2e52016-07-15 20:41:10 +00003098 case Func | Var | Class | ObjCInterface:
3099 return "(S.getLangOpts().CPlusPlus"
3100 " ? ((S.getLangOpts().ObjC1 || S.getLangOpts().ObjC2)"
3101 " ? ExpectedFunctionVariableClassOrObjCInterface"
3102 " : ExpectedFunctionVariableOrClass)"
3103 " : ((S.getLangOpts().ObjC1 || S.getLangOpts().ObjC2)"
3104 " ? ExpectedFunctionVariableOrObjCInterface"
3105 " : ExpectedVariableOrFunction))";
3106
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003107 case ObjCMethod | ObjCProp: return "ExpectedMethodOrProperty";
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00003108 case Func | ObjCMethod | ObjCProp:
3109 return "ExpectedFunctionOrMethodOrProperty";
Aaron Ballman173361e2014-07-16 20:28:10 +00003110 case ObjCProtocol | ObjCInterface:
3111 return "ExpectedObjectiveCInterfaceOrProtocol";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003112 case Field | Var: return "ExpectedFieldOrGlobalVar";
Alex Lorenz24952fb2017-04-19 15:52:11 +00003113
3114 case Named:
3115 return "ExpectedNamedDecl";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003116 }
3117
3118 PrintFatalError(S.getLoc(),
3119 "Could not deduce diagnostic argument for Attr subjects");
3120
3121 return "";
3122}
3123
Aaron Ballman12b9f652014-01-16 13:55:42 +00003124static std::string GetSubjectWithSuffix(const Record *R) {
George Burgess IV1881a572016-12-01 00:13:18 +00003125 const std::string &B = R->getName();
Aaron Ballman12b9f652014-01-16 13:55:42 +00003126 if (B == "DeclBase")
3127 return "Decl";
3128 return B + "Decl";
3129}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003130
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003131static std::string functionNameForCustomAppertainsTo(const Record &Subject) {
3132 return "is" + Subject.getName().str();
3133}
3134
Aaron Ballman80469032013-11-29 14:57:58 +00003135static std::string GenerateCustomAppertainsTo(const Record &Subject,
3136 raw_ostream &OS) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003137 std::string FnName = functionNameForCustomAppertainsTo(Subject);
Aaron Ballmana358c902013-12-02 14:58:17 +00003138
Aaron Ballman80469032013-11-29 14:57:58 +00003139 // If this code has already been generated, simply return the previous
3140 // instance of it.
3141 static std::set<std::string> CustomSubjectSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003142 auto I = CustomSubjectSet.find(FnName);
Aaron Ballman80469032013-11-29 14:57:58 +00003143 if (I != CustomSubjectSet.end())
3144 return *I;
3145
3146 Record *Base = Subject.getValueAsDef("Base");
3147
3148 // Not currently support custom subjects within custom subjects.
3149 if (Base->isSubClassOf("SubsetSubject")) {
3150 PrintFatalError(Subject.getLoc(),
3151 "SubsetSubjects within SubsetSubjects is not supported");
3152 return "";
3153 }
3154
Aaron Ballman80469032013-11-29 14:57:58 +00003155 OS << "static bool " << FnName << "(const Decl *D) {\n";
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003156 OS << " if (const auto *S = dyn_cast<";
Aaron Ballman12b9f652014-01-16 13:55:42 +00003157 OS << GetSubjectWithSuffix(Base);
Aaron Ballman47553042014-01-16 14:32:03 +00003158 OS << ">(D))\n";
3159 OS << " return " << Subject.getValueAsString("CheckCode") << ";\n";
3160 OS << " return false;\n";
Aaron Ballman80469032013-11-29 14:57:58 +00003161 OS << "}\n\n";
3162
3163 CustomSubjectSet.insert(FnName);
3164 return FnName;
3165}
3166
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003167static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) {
3168 // If the attribute does not contain a Subjects definition, then use the
3169 // default appertainsTo logic.
3170 if (Attr.isValueUnset("Subjects"))
Aaron Ballman93b5cc62013-12-02 19:36:42 +00003171 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003172
3173 const Record *SubjectObj = Attr.getValueAsDef("Subjects");
3174 std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
3175
3176 // If the list of subjects is empty, it is assumed that the attribute
3177 // appertains to everything.
3178 if (Subjects.empty())
Aaron Ballman93b5cc62013-12-02 19:36:42 +00003179 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003180
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003181 bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn");
3182
3183 // Otherwise, generate an appertainsTo check specific to this attribute which
3184 // checks all of the given subjects against the Decl passed in. Return the
3185 // name of that check to the caller.
Matthias Braunbbbf5d42016-12-04 05:55:09 +00003186 std::string FnName = "check" + Attr.getName().str() + "AppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003187 std::stringstream SS;
3188 SS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr, ";
3189 SS << "const Decl *D) {\n";
3190 SS << " if (";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003191 for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) {
Aaron Ballman80469032013-11-29 14:57:58 +00003192 // If the subject has custom code associated with it, generate a function
3193 // for it. The function cannot be inlined into this check (yet) because it
3194 // requires the subject to be of a specific type, and were that information
3195 // inlined here, it would not support an attribute with multiple custom
3196 // subjects.
3197 if ((*I)->isSubClassOf("SubsetSubject")) {
3198 SS << "!" << GenerateCustomAppertainsTo(**I, OS) << "(D)";
3199 } else {
Aaron Ballman12b9f652014-01-16 13:55:42 +00003200 SS << "!isa<" << GetSubjectWithSuffix(*I) << ">(D)";
Aaron Ballman80469032013-11-29 14:57:58 +00003201 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003202
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003203 if (I + 1 != E)
3204 SS << " && ";
3205 }
3206 SS << ") {\n";
3207 SS << " S.Diag(Attr.getLoc(), diag::";
3208 SS << (Warn ? "warn_attribute_wrong_decl_type" :
3209 "err_attribute_wrong_decl_type");
3210 SS << ")\n";
3211 SS << " << Attr.getName() << ";
3212 SS << CalculateDiagnostic(*SubjectObj) << ";\n";
3213 SS << " return false;\n";
3214 SS << " }\n";
3215 SS << " return true;\n";
3216 SS << "}\n\n";
3217
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003218 OS << SS.str();
3219 return FnName;
3220}
3221
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003222static void
3223emitAttributeMatchRules(PragmaClangAttributeSupport &PragmaAttributeSupport,
3224 raw_ostream &OS) {
3225 OS << "static bool checkAttributeMatchRuleAppliesTo(const Decl *D, "
3226 << AttributeSubjectMatchRule::EnumName << " rule) {\n";
3227 OS << " switch (rule) {\n";
3228 for (const auto &Rule : PragmaAttributeSupport.Rules) {
3229 if (Rule.isAbstractRule()) {
3230 OS << " case " << Rule.getEnumValue() << ":\n";
3231 OS << " assert(false && \"Abstract matcher rule isn't allowed\");\n";
3232 OS << " return false;\n";
3233 continue;
3234 }
3235 std::vector<Record *> Subjects = Rule.getSubjects();
3236 assert(!Subjects.empty() && "Missing subjects");
3237 OS << " case " << Rule.getEnumValue() << ":\n";
3238 OS << " return ";
3239 for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) {
3240 // If the subject has custom code associated with it, use the function
3241 // that was generated for GenerateAppertainsTo to check if the declaration
3242 // is valid.
3243 if ((*I)->isSubClassOf("SubsetSubject"))
3244 OS << functionNameForCustomAppertainsTo(**I) << "(D)";
3245 else
3246 OS << "isa<" << GetSubjectWithSuffix(*I) << ">(D)";
3247
3248 if (I + 1 != E)
3249 OS << " || ";
3250 }
3251 OS << ";\n";
3252 }
3253 OS << " }\n";
3254 OS << " llvm_unreachable(\"Invalid match rule\");\nreturn false;\n";
3255 OS << "}\n\n";
3256}
3257
Aaron Ballman3aff6332013-12-02 19:30:36 +00003258static void GenerateDefaultLangOptRequirements(raw_ostream &OS) {
3259 OS << "static bool defaultDiagnoseLangOpts(Sema &, ";
3260 OS << "const AttributeList &) {\n";
3261 OS << " return true;\n";
3262 OS << "}\n\n";
3263}
3264
3265static std::string GenerateLangOptRequirements(const Record &R,
3266 raw_ostream &OS) {
3267 // If the attribute has an empty or unset list of language requirements,
3268 // return the default handler.
3269 std::vector<Record *> LangOpts = R.getValueAsListOfDefs("LangOpts");
3270 if (LangOpts.empty())
3271 return "defaultDiagnoseLangOpts";
3272
3273 // Generate the test condition, as well as a unique function name for the
3274 // diagnostic test. The list of options should usually be short (one or two
3275 // options), and the uniqueness isn't strictly necessary (it is just for
3276 // codegen efficiency).
3277 std::string FnName = "check", Test;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003278 for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00003279 std::string Part = (*I)->getValueAsString("Name");
Eric Fiselier341e8252016-09-02 18:53:31 +00003280 if ((*I)->getValueAsBit("Negated")) {
3281 FnName += "Not";
Alexis Hunt724f14e2014-11-28 00:53:20 +00003282 Test += "!";
Eric Fiselier341e8252016-09-02 18:53:31 +00003283 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00003284 Test += "S.LangOpts." + Part;
3285 if (I + 1 != E)
3286 Test += " || ";
3287 FnName += Part;
3288 }
3289 FnName += "LangOpts";
3290
3291 // If this code has already been generated, simply return the previous
3292 // instance of it.
3293 static std::set<std::string> CustomLangOptsSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003294 auto I = CustomLangOptsSet.find(FnName);
Aaron Ballman3aff6332013-12-02 19:30:36 +00003295 if (I != CustomLangOptsSet.end())
3296 return *I;
3297
3298 OS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr) {\n";
3299 OS << " if (" << Test << ")\n";
3300 OS << " return true;\n\n";
3301 OS << " S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) ";
3302 OS << "<< Attr.getName();\n";
3303 OS << " return false;\n";
3304 OS << "}\n\n";
3305
3306 CustomLangOptsSet.insert(FnName);
3307 return FnName;
3308}
3309
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003310static void GenerateDefaultTargetRequirements(raw_ostream &OS) {
Bob Wilson7c730832015-07-20 22:57:31 +00003311 OS << "static bool defaultTargetRequirements(const TargetInfo &) {\n";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003312 OS << " return true;\n";
3313 OS << "}\n\n";
3314}
3315
3316static std::string GenerateTargetRequirements(const Record &Attr,
3317 const ParsedAttrMap &Dupes,
3318 raw_ostream &OS) {
3319 // If the attribute is not a target specific attribute, return the default
3320 // target handler.
3321 if (!Attr.isSubClassOf("TargetSpecificAttr"))
3322 return "defaultTargetRequirements";
3323
3324 // Get the list of architectures to be tested for.
3325 const Record *R = Attr.getValueAsDef("Target");
3326 std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches");
3327 if (Arches.empty()) {
3328 PrintError(Attr.getLoc(), "Empty list of target architectures for a "
3329 "target-specific attr");
3330 return "defaultTargetRequirements";
3331 }
3332
3333 // If there are other attributes which share the same parsed attribute kind,
3334 // such as target-specific attributes with a shared spelling, collapse the
3335 // duplicate architectures. This is required because a shared target-specific
3336 // attribute has only one AttributeList::Kind enumeration value, but it
3337 // applies to multiple target architectures. In order for the attribute to be
3338 // considered valid, all of its architectures need to be included.
3339 if (!Attr.isValueUnset("ParseKind")) {
3340 std::string APK = Attr.getValueAsString("ParseKind");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003341 for (const auto &I : Dupes) {
3342 if (I.first == APK) {
3343 std::vector<std::string> DA = I.second->getValueAsDef("Target")
3344 ->getValueAsListOfStrings("Arches");
George Burgess IV1881a572016-12-01 00:13:18 +00003345 std::move(DA.begin(), DA.end(), std::back_inserter(Arches));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003346 }
3347 }
3348 }
3349
Bob Wilson0058b822015-07-20 22:57:36 +00003350 std::string FnName = "isTarget";
3351 std::string Test;
3352 GenerateTargetSpecificAttrChecks(R, Arches, Test, &FnName);
Bob Wilson7c730832015-07-20 22:57:31 +00003353
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003354 // If this code has already been generated, simply return the previous
3355 // instance of it.
3356 static std::set<std::string> CustomTargetSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003357 auto I = CustomTargetSet.find(FnName);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003358 if (I != CustomTargetSet.end())
3359 return *I;
3360
Bob Wilson7c730832015-07-20 22:57:31 +00003361 OS << "static bool " << FnName << "(const TargetInfo &Target) {\n";
3362 OS << " const llvm::Triple &T = Target.getTriple();\n";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003363 OS << " return " << Test << ";\n";
3364 OS << "}\n\n";
3365
3366 CustomTargetSet.insert(FnName);
3367 return FnName;
3368}
3369
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003370static void GenerateDefaultSpellingIndexToSemanticSpelling(raw_ostream &OS) {
3371 OS << "static unsigned defaultSpellingIndexToSemanticSpelling("
3372 << "const AttributeList &Attr) {\n";
3373 OS << " return UINT_MAX;\n";
3374 OS << "}\n\n";
3375}
3376
3377static std::string GenerateSpellingIndexToSemanticSpelling(const Record &Attr,
3378 raw_ostream &OS) {
3379 // If the attribute does not have a semantic form, we can bail out early.
3380 if (!Attr.getValueAsBit("ASTNode"))
3381 return "defaultSpellingIndexToSemanticSpelling";
3382
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003383 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003384
3385 // If there are zero or one spellings, or all of the spellings share the same
3386 // name, we can also bail out early.
3387 if (Spellings.size() <= 1 || SpellingNamesAreCommon(Spellings))
3388 return "defaultSpellingIndexToSemanticSpelling";
3389
3390 // Generate the enumeration we will use for the mapping.
3391 SemanticSpellingMap SemanticToSyntacticMap;
3392 std::string Enum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
Matthias Braunbbbf5d42016-12-04 05:55:09 +00003393 std::string Name = Attr.getName().str() + "AttrSpellingMap";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003394
3395 OS << "static unsigned " << Name << "(const AttributeList &Attr) {\n";
3396 OS << Enum;
3397 OS << " unsigned Idx = Attr.getAttributeSpellingListIndex();\n";
3398 WriteSemanticSpellingSwitch("Idx", SemanticToSyntacticMap, OS);
3399 OS << "}\n\n";
3400
3401 return Name;
3402}
3403
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003404static bool IsKnownToGCC(const Record &Attr) {
3405 // Look at the spellings for this subject; if there are any spellings which
3406 // claim to be known to GCC, the attribute is known to GCC.
George Burgess IV1881a572016-12-01 00:13:18 +00003407 return llvm::any_of(
3408 GetFlattenedSpellings(Attr),
3409 [](const FlattenedSpelling &S) { return S.knownToGCC(); });
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00003410}
3411
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003412/// Emits the parsed attribute helpers
3413void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
3414 emitSourceFileHeader("Parsed attribute helpers", OS);
3415
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003416 PragmaClangAttributeSupport &PragmaAttributeSupport =
3417 getPragmaAttributeSupport(Records);
3418
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003419 // Get the list of parsed attributes, and accept the optional list of
3420 // duplicates due to the ParseKind.
3421 ParsedAttrMap Dupes;
3422 ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003423
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003424 // Generate the default appertainsTo, target and language option diagnostic,
3425 // and spelling list index mapping methods.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003426 GenerateDefaultAppertainsTo(OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00003427 GenerateDefaultLangOptRequirements(OS);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003428 GenerateDefaultTargetRequirements(OS);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003429 GenerateDefaultSpellingIndexToSemanticSpelling(OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003430
3431 // Generate the appertainsTo diagnostic methods and write their names into
3432 // another mapping. At the same time, generate the AttrInfoMap object
3433 // contents. Due to the reliance on generated code, use separate streams so
3434 // that code will not be interleaved.
3435 std::stringstream SS;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003436 for (auto I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003437 // TODO: If the attribute's kind appears in the list of duplicates, that is
3438 // because it is a target-specific attribute that appears multiple times.
3439 // It would be beneficial to test whether the duplicates are "similar
3440 // enough" to each other to not cause problems. For instance, check that
Alp Toker96cf7582014-01-18 21:49:37 +00003441 // the spellings are identical, and custom parsing rules match, etc.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003442
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003443 // We need to generate struct instances based off ParsedAttrInfo from
3444 // AttributeList.cpp.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003445 SS << " { ";
3446 emitArgInfo(*I->second, SS);
3447 SS << ", " << I->second->getValueAsBit("HasCustomParsing");
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003448 SS << ", " << I->second->isSubClassOf("TargetSpecificAttr");
3449 SS << ", " << I->second->isSubClassOf("TypeAttr");
Richard Smith4f902c72016-03-08 00:32:55 +00003450 SS << ", " << I->second->isSubClassOf("StmtAttr");
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003451 SS << ", " << IsKnownToGCC(*I->second);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003452 SS << ", " << PragmaAttributeSupport.isAttributedSupported(*I->second);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003453 SS << ", " << GenerateAppertainsTo(*I->second, OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00003454 SS << ", " << GenerateLangOptRequirements(*I->second, OS);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003455 SS << ", " << GenerateTargetRequirements(*I->second, Dupes, OS);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003456 SS << ", " << GenerateSpellingIndexToSemanticSpelling(*I->second, OS);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003457 SS << ", "
3458 << PragmaAttributeSupport.generateStrictConformsTo(*I->second, OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003459 SS << " }";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003460
3461 if (I + 1 != E)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003462 SS << ",";
3463
3464 SS << " // AT_" << I->first << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003465 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003466
3467 OS << "static const ParsedAttrInfo AttrInfoMap[AttributeList::UnknownAttribute + 1] = {\n";
3468 OS << SS.str();
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003469 OS << "};\n\n";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003470
3471 // Generate the attribute match rules.
3472 emitAttributeMatchRules(PragmaAttributeSupport, OS);
Michael Han4a045172012-03-07 00:12:16 +00003473}
3474
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00003475// Emits the kind list of parsed attributes
3476void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00003477 emitSourceFileHeader("Attribute name matcher", OS);
3478
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003479 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Nico Weber20e08042016-09-03 02:55:10 +00003480 std::vector<StringMatcher::StringPair> GNU, Declspec, Microsoft, CXX11,
3481 Keywords, Pragma;
Aaron Ballman64e69862013-12-15 13:05:48 +00003482 std::set<std::string> Seen;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003483 for (const auto *A : Attrs) {
3484 const Record &Attr = *A;
Richard Smith852e9ce2013-11-27 01:46:48 +00003485
Michael Han4a045172012-03-07 00:12:16 +00003486 bool SemaHandler = Attr.getValueAsBit("SemaHandler");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003487 bool Ignored = Attr.getValueAsBit("Ignored");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003488 if (SemaHandler || Ignored) {
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003489 // Attribute spellings can be shared between target-specific attributes,
3490 // and can be shared between syntaxes for the same attribute. For
3491 // instance, an attribute can be spelled GNU<"interrupt"> for an ARM-
3492 // specific attribute, or MSP430-specific attribute. Additionally, an
3493 // attribute can be spelled GNU<"dllexport"> and Declspec<"dllexport">
3494 // for the same semantic attribute. Ultimately, we need to map each of
3495 // these to a single AttributeList::Kind value, but the StringMatcher
3496 // class cannot handle duplicate match strings. So we generate a list of
3497 // string to match based on the syntax, and emit multiple string matchers
3498 // depending on the syntax used.
Aaron Ballman64e69862013-12-15 13:05:48 +00003499 std::string AttrName;
3500 if (Attr.isSubClassOf("TargetSpecificAttr") &&
3501 !Attr.isValueUnset("ParseKind")) {
3502 AttrName = Attr.getValueAsString("ParseKind");
3503 if (Seen.find(AttrName) != Seen.end())
3504 continue;
3505 Seen.insert(AttrName);
3506 } else
3507 AttrName = NormalizeAttrName(StringRef(Attr.getName())).str();
3508
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003509 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003510 for (const auto &S : Spellings) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00003511 const std::string &RawSpelling = S.name();
Craig Topper8ae12032014-05-07 06:21:57 +00003512 std::vector<StringMatcher::StringPair> *Matches = nullptr;
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00003513 std::string Spelling;
3514 const std::string &Variety = S.variety();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003515 if (Variety == "CXX11") {
3516 Matches = &CXX11;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003517 Spelling += S.nameSpace();
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003518 Spelling += "::";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003519 } else if (Variety == "GNU")
3520 Matches = &GNU;
3521 else if (Variety == "Declspec")
3522 Matches = &Declspec;
Nico Weber20e08042016-09-03 02:55:10 +00003523 else if (Variety == "Microsoft")
3524 Matches = &Microsoft;
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003525 else if (Variety == "Keyword")
3526 Matches = &Keywords;
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003527 else if (Variety == "Pragma")
3528 Matches = &Pragma;
Alexis Hunta0e54d42012-06-18 16:13:52 +00003529
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003530 assert(Matches && "Unsupported spelling variety found");
3531
Justin Lebar4086fe52017-01-05 16:51:54 +00003532 if (Variety == "GNU")
3533 Spelling += NormalizeGNUAttrSpelling(RawSpelling);
3534 else
3535 Spelling += RawSpelling;
3536
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003537 if (SemaHandler)
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003538 Matches->push_back(StringMatcher::StringPair(Spelling,
3539 "return AttributeList::AT_" + AttrName + ";"));
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003540 else
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003541 Matches->push_back(StringMatcher::StringPair(Spelling,
3542 "return AttributeList::IgnoredAttribute;"));
Michael Han4a045172012-03-07 00:12:16 +00003543 }
3544 }
3545 }
Douglas Gregor377f99b2012-05-02 17:33:51 +00003546
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003547 OS << "static AttributeList::Kind getAttrKind(StringRef Name, ";
3548 OS << "AttributeList::Syntax Syntax) {\n";
3549 OS << " if (AttributeList::AS_GNU == Syntax) {\n";
3550 StringMatcher("Name", GNU, OS).Emit();
3551 OS << " } else if (AttributeList::AS_Declspec == Syntax) {\n";
3552 StringMatcher("Name", Declspec, OS).Emit();
Nico Weber20e08042016-09-03 02:55:10 +00003553 OS << " } else if (AttributeList::AS_Microsoft == Syntax) {\n";
3554 StringMatcher("Name", Microsoft, OS).Emit();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003555 OS << " } else if (AttributeList::AS_CXX11 == Syntax) {\n";
3556 StringMatcher("Name", CXX11, OS).Emit();
Douglas Gregorbec595a2015-06-19 18:27:45 +00003557 OS << " } else if (AttributeList::AS_Keyword == Syntax || ";
3558 OS << "AttributeList::AS_ContextSensitiveKeyword == Syntax) {\n";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003559 StringMatcher("Name", Keywords, OS).Emit();
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003560 OS << " } else if (AttributeList::AS_Pragma == Syntax) {\n";
3561 StringMatcher("Name", Pragma, OS).Emit();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003562 OS << " }\n";
3563 OS << " return AttributeList::UnknownAttribute;\n"
Douglas Gregor377f99b2012-05-02 17:33:51 +00003564 << "}\n";
Michael Han4a045172012-03-07 00:12:16 +00003565}
3566
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003567// Emits the code to dump an attribute.
3568void EmitClangAttrDump(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00003569 emitSourceFileHeader("Attribute dumper", OS);
3570
John McCall2225c8b2016-03-01 00:18:05 +00003571 OS << " switch (A->getKind()) {\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003572 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003573 for (const auto *Attr : Attrs) {
3574 const Record &R = *Attr;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003575 if (!R.getValueAsBit("ASTNode"))
3576 continue;
3577 OS << " case attr::" << R.getName() << ": {\n";
Aaron Ballmanbc909612014-01-22 21:51:20 +00003578
3579 // If the attribute has a semantically-meaningful name (which is determined
3580 // by whether there is a Spelling enumeration for it), then write out the
3581 // spelling used for the attribute.
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003582 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballmanbc909612014-01-22 21:51:20 +00003583 if (Spellings.size() > 1 && !SpellingNamesAreCommon(Spellings))
3584 OS << " OS << \" \" << A->getSpelling();\n";
3585
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003586 Args = R.getValueAsListOfDefs("Args");
3587 if (!Args.empty()) {
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003588 OS << " const auto *SA = cast<" << R.getName()
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003589 << "Attr>(A);\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00003590 for (const auto *Arg : Args)
3591 createArgument(*Arg, R.getName())->writeDump(OS);
Richard Trieude5cc7d2013-01-31 01:44:26 +00003592
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003593 for (const auto *AI : Args)
3594 createArgument(*AI, R.getName())->writeDumpChildren(OS);
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003595 }
3596 OS <<
3597 " break;\n"
3598 " }\n";
3599 }
3600 OS << " }\n";
3601}
3602
Aaron Ballman35db2b32014-01-29 22:13:45 +00003603void EmitClangAttrParserStringSwitches(RecordKeeper &Records,
3604 raw_ostream &OS) {
3605 emitSourceFileHeader("Parser-related llvm::StringSwitch cases", OS);
3606 emitClangAttrArgContextList(Records, OS);
3607 emitClangAttrIdentifierArgList(Records, OS);
3608 emitClangAttrTypeArgList(Records, OS);
3609 emitClangAttrLateParsedList(Records, OS);
3610}
3611
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003612void EmitClangAttrSubjectMatchRulesParserStringSwitches(RecordKeeper &Records,
3613 raw_ostream &OS) {
3614 getPragmaAttributeSupport(Records).generateParsingHelpers(OS);
3615}
3616
Aaron Ballman97dba042014-02-17 15:27:10 +00003617class DocumentationData {
3618public:
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003619 const Record *Documentation;
3620 const Record *Attribute;
Aaron Ballman97dba042014-02-17 15:27:10 +00003621
Aaron Ballman4de1b582014-02-19 22:59:32 +00003622 DocumentationData(const Record &Documentation, const Record &Attribute)
3623 : Documentation(&Documentation), Attribute(&Attribute) {}
Aaron Ballman97dba042014-02-17 15:27:10 +00003624};
3625
Aaron Ballman4de1b582014-02-19 22:59:32 +00003626static void WriteCategoryHeader(const Record *DocCategory,
Aaron Ballman97dba042014-02-17 15:27:10 +00003627 raw_ostream &OS) {
Aaron Ballman4de1b582014-02-19 22:59:32 +00003628 const std::string &Name = DocCategory->getValueAsString("Name");
3629 OS << Name << "\n" << std::string(Name.length(), '=') << "\n";
3630
3631 // If there is content, print that as well.
3632 std::string ContentStr = DocCategory->getValueAsString("Content");
Benjamin Kramer5c404072015-04-10 21:37:21 +00003633 // Trim leading and trailing newlines and spaces.
3634 OS << StringRef(ContentStr).trim();
3635
Aaron Ballman4de1b582014-02-19 22:59:32 +00003636 OS << "\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003637}
3638
Aaron Ballmana66b5742014-02-17 15:36:08 +00003639enum SpellingKind {
3640 GNU = 1 << 0,
3641 CXX11 = 1 << 1,
3642 Declspec = 1 << 2,
Nico Weber20e08042016-09-03 02:55:10 +00003643 Microsoft = 1 << 3,
3644 Keyword = 1 << 4,
3645 Pragma = 1 << 5
Aaron Ballmana66b5742014-02-17 15:36:08 +00003646};
3647
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003648static void WriteDocumentation(RecordKeeper &Records,
3649 const DocumentationData &Doc, raw_ostream &OS) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003650 // FIXME: there is no way to have a per-spelling category for the attribute
3651 // documentation. This may not be a limiting factor since the spellings
3652 // should generally be consistently applied across the category.
3653
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003654 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Doc.Attribute);
Aaron Ballman97dba042014-02-17 15:27:10 +00003655
3656 // Determine the heading to be used for this attribute.
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003657 std::string Heading = Doc.Documentation->getValueAsString("Heading");
Aaron Ballmanea6668c2014-02-21 14:14:04 +00003658 bool CustomHeading = !Heading.empty();
Aaron Ballman97dba042014-02-17 15:27:10 +00003659 if (Heading.empty()) {
3660 // If there's only one spelling, we can simply use that.
3661 if (Spellings.size() == 1)
3662 Heading = Spellings.begin()->name();
3663 else {
3664 std::set<std::string> Uniques;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003665 for (auto I = Spellings.begin(), E = Spellings.end();
3666 I != E && Uniques.size() <= 1; ++I) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003667 std::string Spelling = NormalizeNameForSpellingComparison(I->name());
3668 Uniques.insert(Spelling);
3669 }
3670 // If the semantic map has only one spelling, that is sufficient for our
3671 // needs.
3672 if (Uniques.size() == 1)
3673 Heading = *Uniques.begin();
3674 }
3675 }
3676
3677 // If the heading is still empty, it is an error.
3678 if (Heading.empty())
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003679 PrintFatalError(Doc.Attribute->getLoc(),
Aaron Ballman97dba042014-02-17 15:27:10 +00003680 "This attribute requires a heading to be specified");
3681
3682 // Gather a list of unique spellings; this is not the same as the semantic
3683 // spelling for the attribute. Variations in underscores and other non-
3684 // semantic characters are still acceptable.
3685 std::vector<std::string> Names;
3686
Aaron Ballman97dba042014-02-17 15:27:10 +00003687 unsigned SupportedSpellings = 0;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003688 for (const auto &I : Spellings) {
3689 SpellingKind Kind = StringSwitch<SpellingKind>(I.variety())
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003690 .Case("GNU", GNU)
3691 .Case("CXX11", CXX11)
3692 .Case("Declspec", Declspec)
Nico Weber20e08042016-09-03 02:55:10 +00003693 .Case("Microsoft", Microsoft)
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003694 .Case("Keyword", Keyword)
3695 .Case("Pragma", Pragma);
Aaron Ballman97dba042014-02-17 15:27:10 +00003696
3697 // Mask in the supported spelling.
3698 SupportedSpellings |= Kind;
3699
3700 std::string Name;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003701 if (Kind == CXX11 && !I.nameSpace().empty())
3702 Name = I.nameSpace() + "::";
3703 Name += I.name();
Aaron Ballman97dba042014-02-17 15:27:10 +00003704
3705 // If this name is the same as the heading, do not add it.
3706 if (Name != Heading)
3707 Names.push_back(Name);
3708 }
3709
3710 // Print out the heading for the attribute. If there are alternate spellings,
3711 // then display those after the heading.
Aaron Ballmanea6668c2014-02-21 14:14:04 +00003712 if (!CustomHeading && !Names.empty()) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003713 Heading += " (";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003714 for (auto I = Names.begin(), E = Names.end(); I != E; ++I) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003715 if (I != Names.begin())
3716 Heading += ", ";
3717 Heading += *I;
3718 }
3719 Heading += ")";
3720 }
3721 OS << Heading << "\n" << std::string(Heading.length(), '-') << "\n";
3722
3723 if (!SupportedSpellings)
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003724 PrintFatalError(Doc.Attribute->getLoc(),
Aaron Ballman97dba042014-02-17 15:27:10 +00003725 "Attribute has no supported spellings; cannot be "
3726 "documented");
3727
3728 // List what spelling syntaxes the attribute supports.
3729 OS << ".. csv-table:: Supported Syntaxes\n";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003730 OS << " :header: \"GNU\", \"C++11\", \"__declspec\", \"Keyword\",";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003731 OS << " \"Pragma\", \"Pragma clang attribute\"\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003732 OS << " \"";
3733 if (SupportedSpellings & GNU) OS << "X";
3734 OS << "\",\"";
3735 if (SupportedSpellings & CXX11) OS << "X";
3736 OS << "\",\"";
3737 if (SupportedSpellings & Declspec) OS << "X";
3738 OS << "\",\"";
3739 if (SupportedSpellings & Keyword) OS << "X";
Aaron Ballman120c79f2014-06-25 12:48:06 +00003740 OS << "\", \"";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003741 if (SupportedSpellings & Pragma) OS << "X";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003742 OS << "\", \"";
3743 if (getPragmaAttributeSupport(Records).isAttributedSupported(*Doc.Attribute))
3744 OS << "X";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003745 OS << "\"\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003746
3747 // If the attribute is deprecated, print a message about it, and possibly
3748 // provide a replacement attribute.
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003749 if (!Doc.Documentation->isValueUnset("Deprecated")) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003750 OS << "This attribute has been deprecated, and may be removed in a future "
3751 << "version of Clang.";
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003752 const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated");
Aaron Ballman97dba042014-02-17 15:27:10 +00003753 std::string Replacement = Deprecated.getValueAsString("Replacement");
3754 if (!Replacement.empty())
3755 OS << " This attribute has been superseded by ``"
3756 << Replacement << "``.";
3757 OS << "\n\n";
3758 }
3759
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003760 std::string ContentStr = Doc.Documentation->getValueAsString("Content");
Aaron Ballman97dba042014-02-17 15:27:10 +00003761 // Trim leading and trailing newlines and spaces.
Benjamin Kramer5c404072015-04-10 21:37:21 +00003762 OS << StringRef(ContentStr).trim();
Aaron Ballman97dba042014-02-17 15:27:10 +00003763
3764 OS << "\n\n\n";
3765}
3766
3767void EmitClangAttrDocs(RecordKeeper &Records, raw_ostream &OS) {
3768 // Get the documentation introduction paragraph.
3769 const Record *Documentation = Records.getDef("GlobalDocumentation");
3770 if (!Documentation) {
3771 PrintFatalError("The Documentation top-level definition is missing, "
3772 "no documentation will be generated.");
3773 return;
3774 }
3775
Aaron Ballman4de1b582014-02-19 22:59:32 +00003776 OS << Documentation->getValueAsString("Intro") << "\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003777
Aaron Ballman97dba042014-02-17 15:27:10 +00003778 // Gather the Documentation lists from each of the attributes, based on the
3779 // category provided.
3780 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003781 std::map<const Record *, std::vector<DocumentationData>> SplitDocs;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003782 for (const auto *A : Attrs) {
3783 const Record &Attr = *A;
Aaron Ballman97dba042014-02-17 15:27:10 +00003784 std::vector<Record *> Docs = Attr.getValueAsListOfDefs("Documentation");
Aaron Ballman2f22b942014-05-20 19:47:14 +00003785 for (const auto *D : Docs) {
3786 const Record &Doc = *D;
Aaron Ballman4de1b582014-02-19 22:59:32 +00003787 const Record *Category = Doc.getValueAsDef("Category");
Aaron Ballman97dba042014-02-17 15:27:10 +00003788 // If the category is "undocumented", then there cannot be any other
3789 // documentation categories (otherwise, the attribute would become
3790 // documented).
Aaron Ballman4de1b582014-02-19 22:59:32 +00003791 std::string Cat = Category->getValueAsString("Name");
3792 bool Undocumented = Cat == "Undocumented";
Aaron Ballman97dba042014-02-17 15:27:10 +00003793 if (Undocumented && Docs.size() > 1)
3794 PrintFatalError(Doc.getLoc(),
3795 "Attribute is \"Undocumented\", but has multiple "
3796 "documentation categories");
3797
3798 if (!Undocumented)
Aaron Ballman4de1b582014-02-19 22:59:32 +00003799 SplitDocs[Category].push_back(DocumentationData(Doc, Attr));
Aaron Ballman97dba042014-02-17 15:27:10 +00003800 }
3801 }
3802
3803 // Having split the attributes out based on what documentation goes where,
3804 // we can begin to generate sections of documentation.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003805 for (const auto &I : SplitDocs) {
3806 WriteCategoryHeader(I.first, OS);
Aaron Ballman97dba042014-02-17 15:27:10 +00003807
3808 // Walk over each of the attributes in the category and write out their
3809 // documentation.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003810 for (const auto &Doc : I.second)
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003811 WriteDocumentation(Records, Doc, OS);
3812 }
3813}
3814
3815void EmitTestPragmaAttributeSupportedAttributes(RecordKeeper &Records,
3816 raw_ostream &OS) {
3817 PragmaClangAttributeSupport Support = getPragmaAttributeSupport(Records);
3818 ParsedAttrMap Attrs = getParsedAttrList(Records);
3819 unsigned NumAttrs = 0;
3820 for (const auto &I : Attrs) {
3821 if (Support.isAttributedSupported(*I.second))
3822 ++NumAttrs;
3823 }
3824 OS << "#pragma clang attribute supports " << NumAttrs << " attributes:\n";
3825 for (const auto &I : Attrs) {
3826 if (!Support.isAttributedSupported(*I.second))
3827 continue;
3828 OS << I.first;
3829 if (I.second->isValueUnset("Subjects")) {
3830 OS << " ()\n";
3831 continue;
3832 }
3833 const Record *SubjectObj = I.second->getValueAsDef("Subjects");
3834 std::vector<Record *> Subjects =
3835 SubjectObj->getValueAsListOfDefs("Subjects");
3836 OS << " (";
3837 for (const auto &Subject : llvm::enumerate(Subjects)) {
3838 if (Subject.index())
3839 OS << ", ";
Alex Lorenz24952fb2017-04-19 15:52:11 +00003840 PragmaClangAttributeSupport::RuleOrAggregateRuleSet &RuleSet =
3841 Support.SubjectsToRules.find(Subject.value())->getSecond();
3842 if (RuleSet.isRule()) {
3843 OS << RuleSet.getRule().getEnumValueName();
3844 continue;
3845 }
3846 OS << "(";
3847 for (const auto &Rule : llvm::enumerate(RuleSet.getAggregateRuleSet())) {
3848 if (Rule.index())
3849 OS << ", ";
3850 OS << Rule.value().getEnumValueName();
3851 }
3852 OS << ")";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003853 }
3854 OS << ")\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003855 }
3856}
3857
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00003858} // end namespace clang