blob: 9284fe40814c509b275422c06cce869536202442 [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
Aaron Ballmanffc43362017-10-26 12:19:02 +000059 assert(V != "GCC" && V != "Clang" &&
60 "Given a GCC spelling, which means this hasn't been flattened!");
Aaron Ballman606093a2017-10-15 15:01:42 +000061 if (V == "CXX11" || V == "C2x" || 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) {
Aaron Ballmanffc43362017-10-26 12:19:02 +000081 StringRef Variety = Spelling->getValueAsString("Variety");
82 StringRef Name = Spelling->getValueAsString("Name");
83 if (Variety == "GCC") {
Aaron Ballmanc669cc02014-01-27 22:10:04 +000084 // Gin up two new spelling objects to add into the list.
Aaron Ballmanffc43362017-10-26 12:19:02 +000085 Ret.emplace_back("GNU", Name, "", true);
86 Ret.emplace_back("CXX11", Name, "gnu", true);
87 } else if (Variety == "Clang") {
88 Ret.emplace_back("GNU", Name, "", false);
89 Ret.emplace_back("CXX11", Name, "clang", false);
Aaron Ballmanc669cc02014-01-27 22:10:04 +000090 } else
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +000091 Ret.push_back(FlattenedSpelling(*Spelling));
Aaron Ballmanc669cc02014-01-27 22:10:04 +000092 }
93
94 return Ret;
95}
96
Peter Collingbournebee583f2011-10-06 13:03:08 +000097static std::string ReadPCHRecord(StringRef type) {
98 return StringSwitch<std::string>(type)
David L. Jones267b8842017-01-24 01:04:30 +000099 .EndsWith("Decl *", "Record.GetLocalDeclAs<"
100 + std::string(type, 0, type.size()-1) + ">(Record.readInt())")
101 .Case("TypeSourceInfo *", "Record.getTypeSourceInfo()")
102 .Case("Expr *", "Record.readExpr()")
103 .Case("IdentifierInfo *", "Record.getIdentifierInfo()")
104 .Case("StringRef", "Record.readString()")
105 .Default("Record.readInt()");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000106}
107
Richard Smith19978562016-05-18 00:16:51 +0000108// Get a type that is suitable for storing an object of the specified type.
109static StringRef getStorageType(StringRef type) {
110 return StringSwitch<StringRef>(type)
111 .Case("StringRef", "std::string")
112 .Default(type);
113}
114
Peter Collingbournebee583f2011-10-06 13:03:08 +0000115// Assumes that the way to get the value is SA->getname()
116static std::string WritePCHRecord(StringRef type, StringRef name) {
Richard Smith290d8012016-04-06 17:06:00 +0000117 return "Record." + StringSwitch<std::string>(type)
118 .EndsWith("Decl *", "AddDeclRef(" + std::string(name) + ");\n")
119 .Case("TypeSourceInfo *", "AddTypeSourceInfo(" + std::string(name) + ");\n")
Peter Collingbournebee583f2011-10-06 13:03:08 +0000120 .Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
Richard Smith290d8012016-04-06 17:06:00 +0000121 .Case("IdentifierInfo *", "AddIdentifierRef(" + std::string(name) + ");\n")
122 .Case("StringRef", "AddString(" + std::string(name) + ");\n")
123 .Default("push_back(" + std::string(name) + ");\n");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000124}
125
Michael Han4a045172012-03-07 00:12:16 +0000126// Normalize attribute name by removing leading and trailing
127// underscores. For example, __foo, foo__, __foo__ would
128// become foo.
129static StringRef NormalizeAttrName(StringRef AttrName) {
George Burgess IV1881a572016-12-01 00:13:18 +0000130 AttrName.consume_front("__");
131 AttrName.consume_back("__");
Michael Han4a045172012-03-07 00:12:16 +0000132 return AttrName;
133}
134
Aaron Ballman36a53502014-01-16 13:03:14 +0000135// Normalize the name by removing any and all leading and trailing underscores.
136// This is different from NormalizeAttrName in that it also handles names like
137// _pascal and __pascal.
138static StringRef NormalizeNameForSpellingComparison(StringRef Name) {
Benjamin Kramer5c404072015-04-10 21:37:21 +0000139 return Name.trim("_");
Aaron Ballman36a53502014-01-16 13:03:14 +0000140}
141
Justin Lebar4086fe52017-01-05 16:51:54 +0000142// Normalize the spelling of a GNU attribute (i.e. "x" in "__attribute__((x))"),
143// removing "__" if it appears at the beginning and end of the attribute's name.
144static StringRef NormalizeGNUAttrSpelling(StringRef AttrSpelling) {
Michael Han4a045172012-03-07 00:12:16 +0000145 if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) {
146 AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4);
147 }
148
149 return AttrSpelling;
150}
151
Aaron Ballman2f22b942014-05-20 19:47:14 +0000152typedef std::vector<std::pair<std::string, const Record *>> ParsedAttrMap;
Aaron Ballman64e69862013-12-15 13:05:48 +0000153
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000154static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,
Craig Topper8ae12032014-05-07 06:21:57 +0000155 ParsedAttrMap *Dupes = nullptr) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000156 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Aaron Ballman64e69862013-12-15 13:05:48 +0000157 std::set<std::string> Seen;
158 ParsedAttrMap R;
Aaron Ballman2f22b942014-05-20 19:47:14 +0000159 for (const auto *Attr : Attrs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000160 if (Attr->getValueAsBit("SemaHandler")) {
Aaron Ballman64e69862013-12-15 13:05:48 +0000161 std::string AN;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000162 if (Attr->isSubClassOf("TargetSpecificAttr") &&
163 !Attr->isValueUnset("ParseKind")) {
164 AN = Attr->getValueAsString("ParseKind");
Aaron Ballman64e69862013-12-15 13:05:48 +0000165
166 // If this attribute has already been handled, it does not need to be
167 // handled again.
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000168 if (Seen.find(AN) != Seen.end()) {
169 if (Dupes)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000170 Dupes->push_back(std::make_pair(AN, Attr));
Aaron Ballman64e69862013-12-15 13:05:48 +0000171 continue;
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000172 }
Aaron Ballman64e69862013-12-15 13:05:48 +0000173 Seen.insert(AN);
174 } else
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000175 AN = NormalizeAttrName(Attr->getName()).str();
Aaron Ballman64e69862013-12-15 13:05:48 +0000176
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000177 R.push_back(std::make_pair(AN, Attr));
Aaron Ballman64e69862013-12-15 13:05:48 +0000178 }
179 }
180 return R;
181}
182
Peter Collingbournebee583f2011-10-06 13:03:08 +0000183namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000184
Peter Collingbournebee583f2011-10-06 13:03:08 +0000185 class Argument {
186 std::string lowerName, upperName;
187 StringRef attrName;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000188 bool isOpt;
John McCalla62c1a92015-10-28 00:17:34 +0000189 bool Fake;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000190
191 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000192 Argument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000193 : lowerName(Arg.getValueAsString("Name")), upperName(lowerName),
John McCalla62c1a92015-10-28 00:17:34 +0000194 attrName(Attr), isOpt(false), Fake(false) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000195 if (!lowerName.empty()) {
196 lowerName[0] = std::tolower(lowerName[0]);
197 upperName[0] = std::toupper(upperName[0]);
198 }
Reid Klecknerebeb0ca2016-05-31 17:42:56 +0000199 // Work around MinGW's macro definition of 'interface' to 'struct'. We
200 // have an attribute argument called 'Interface', so only the lower case
201 // name conflicts with the macro definition.
202 if (lowerName == "interface")
203 lowerName = "interface_";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000204 }
Hans Wennborgdcfba332015-10-06 23:40:43 +0000205 virtual ~Argument() = default;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000206
207 StringRef getLowerName() const { return lowerName; }
208 StringRef getUpperName() const { return upperName; }
209 StringRef getAttrName() const { return attrName; }
210
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000211 bool isOptional() const { return isOpt; }
212 void setOptional(bool set) { isOpt = set; }
213
John McCalla62c1a92015-10-28 00:17:34 +0000214 bool isFake() const { return Fake; }
215 void setFake(bool fake) { Fake = fake; }
216
Peter Collingbournebee583f2011-10-06 13:03:08 +0000217 // These functions print the argument contents formatted in different ways.
218 virtual void writeAccessors(raw_ostream &OS) const = 0;
219 virtual void writeAccessorDefinitions(raw_ostream &OS) const {}
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +0000220 virtual void writeASTVisitorTraversal(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000221 virtual void writeCloneArgs(raw_ostream &OS) const = 0;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000222 virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0;
Daniel Dunbardc51baa2012-02-10 06:00:29 +0000223 virtual void writeTemplateInstantiation(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000224 virtual void writeCtorBody(raw_ostream &OS) const {}
225 virtual void writeCtorInitializers(raw_ostream &OS) const = 0;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000226 virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000227 virtual void writeCtorParameters(raw_ostream &OS) const = 0;
228 virtual void writeDeclarations(raw_ostream &OS) const = 0;
229 virtual void writePCHReadArgs(raw_ostream &OS) const = 0;
230 virtual void writePCHReadDecls(raw_ostream &OS) const = 0;
231 virtual void writePCHWrite(raw_ostream &OS) const = 0;
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000232 virtual void writeValue(raw_ostream &OS) const = 0;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000233 virtual void writeDump(raw_ostream &OS) const = 0;
234 virtual void writeDumpChildren(raw_ostream &OS) const {}
Richard Trieude5cc7d2013-01-31 01:44:26 +0000235 virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000236
237 virtual bool isEnumArg() const { return false; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000238 virtual bool isVariadicEnumArg() const { return false; }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000239 virtual bool isVariadic() const { return false; }
Aaron Ballman36a53502014-01-16 13:03:14 +0000240
241 virtual void writeImplicitCtorArgs(raw_ostream &OS) const {
242 OS << getUpperName();
243 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000244 };
245
246 class SimpleArgument : public Argument {
247 std::string type;
248
249 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000250 SimpleArgument(const Record &Arg, StringRef Attr, std::string T)
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000251 : Argument(Arg, Attr), type(std::move(T)) {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000252
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000253 std::string getType() const { return type; }
254
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000255 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000256 OS << " " << type << " get" << getUpperName() << "() const {\n";
257 OS << " return " << getLowerName() << ";\n";
258 OS << " }";
259 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000260
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000261 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000262 OS << getLowerName();
263 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000264
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000265 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000266 OS << "A->get" << getUpperName() << "()";
267 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000268
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000269 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000270 OS << getLowerName() << "(" << getUpperName() << ")";
271 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000272
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000273 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000274 OS << getLowerName() << "()";
275 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000276
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000277 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000278 OS << type << " " << getUpperName();
279 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000280
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000281 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000282 OS << type << " " << getLowerName() << ";";
283 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000284
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000285 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000286 std::string read = ReadPCHRecord(type);
287 OS << " " << type << " " << getLowerName() << " = " << read << ";\n";
288 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000289
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000290 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000291 OS << getLowerName();
292 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000293
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000294 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000295 OS << " " << WritePCHRecord(type, "SA->get" +
296 std::string(getUpperName()) + "()");
297 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000298
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000299 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000300 if (type == "FunctionDecl *") {
Richard Smithb87c4652013-10-31 21:23:20 +0000301 OS << "\" << get" << getUpperName()
302 << "()->getNameInfo().getAsString() << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000303 } else if (type == "IdentifierInfo *") {
Akira Hatanaka3d173132016-09-10 03:29:43 +0000304 OS << "\";\n";
305 if (isOptional())
306 OS << " if (get" << getUpperName() << "()) ";
307 else
308 OS << " ";
309 OS << "OS << get" << getUpperName() << "()->getName();\n";
310 OS << " OS << \"";
Richard Smithb87c4652013-10-31 21:23:20 +0000311 } else if (type == "TypeSourceInfo *") {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000312 OS << "\" << get" << getUpperName() << "().getAsString() << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000313 } else {
314 OS << "\" << get" << getUpperName() << "() << \"";
315 }
316 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000317
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000318 void writeDump(raw_ostream &OS) const override {
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +0000319 if (type == "FunctionDecl *" || type == "NamedDecl *") {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000320 OS << " OS << \" \";\n";
321 OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n";
322 } else if (type == "IdentifierInfo *") {
Aaron Ballman415c4142015-11-30 15:25:34 +0000323 if (isOptional())
324 OS << " if (SA->get" << getUpperName() << "())\n ";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000325 OS << " OS << \" \" << SA->get" << getUpperName()
326 << "()->getName();\n";
Richard Smithb87c4652013-10-31 21:23:20 +0000327 } else if (type == "TypeSourceInfo *") {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000328 OS << " OS << \" \" << SA->get" << getUpperName()
329 << "().getAsString();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000330 } else if (type == "bool") {
331 OS << " if (SA->get" << getUpperName() << "()) OS << \" "
332 << getUpperName() << "\";\n";
333 } else if (type == "int" || type == "unsigned") {
334 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
335 } else {
336 llvm_unreachable("Unknown SimpleArgument type!");
337 }
338 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000339 };
340
Aaron Ballman18a78382013-11-21 00:28:23 +0000341 class DefaultSimpleArgument : public SimpleArgument {
342 int64_t Default;
343
344 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000345 DefaultSimpleArgument(const Record &Arg, StringRef Attr,
Aaron Ballman18a78382013-11-21 00:28:23 +0000346 std::string T, int64_t Default)
347 : SimpleArgument(Arg, Attr, T), Default(Default) {}
348
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000349 void writeAccessors(raw_ostream &OS) const override {
Aaron Ballman18a78382013-11-21 00:28:23 +0000350 SimpleArgument::writeAccessors(OS);
351
352 OS << "\n\n static const " << getType() << " Default" << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000353 << " = ";
354 if (getType() == "bool")
355 OS << (Default != 0 ? "true" : "false");
356 else
357 OS << Default;
358 OS << ";";
Aaron Ballman18a78382013-11-21 00:28:23 +0000359 }
360 };
361
Peter Collingbournebee583f2011-10-06 13:03:08 +0000362 class StringArgument : public Argument {
363 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000364 StringArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000365 : Argument(Arg, Attr)
366 {}
367
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000368 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000369 OS << " llvm::StringRef get" << getUpperName() << "() const {\n";
370 OS << " return llvm::StringRef(" << getLowerName() << ", "
371 << getLowerName() << "Length);\n";
372 OS << " }\n";
373 OS << " unsigned get" << getUpperName() << "Length() const {\n";
374 OS << " return " << getLowerName() << "Length;\n";
375 OS << " }\n";
376 OS << " void set" << getUpperName()
377 << "(ASTContext &C, llvm::StringRef S) {\n";
378 OS << " " << getLowerName() << "Length = S.size();\n";
379 OS << " this->" << getLowerName() << " = new (C, 1) char ["
380 << getLowerName() << "Length];\n";
Chandler Carruth38a45cc2015-08-04 03:53:01 +0000381 OS << " if (!S.empty())\n";
382 OS << " std::memcpy(this->" << getLowerName() << ", S.data(), "
Peter Collingbournebee583f2011-10-06 13:03:08 +0000383 << getLowerName() << "Length);\n";
384 OS << " }";
385 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000386
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000387 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000388 OS << "get" << getUpperName() << "()";
389 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000390
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000391 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000392 OS << "A->get" << getUpperName() << "()";
393 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000394
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000395 void writeCtorBody(raw_ostream &OS) const override {
Chandler Carruth38a45cc2015-08-04 03:53:01 +0000396 OS << " if (!" << getUpperName() << ".empty())\n";
397 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000398 << ".data(), " << getLowerName() << "Length);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000399 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000400
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000401 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000402 OS << getLowerName() << "Length(" << getUpperName() << ".size()),"
403 << getLowerName() << "(new (Ctx, 1) char[" << getLowerName()
404 << "Length])";
405 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000406
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000407 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Hans Wennborg59dbe862015-09-29 20:56:43 +0000408 OS << getLowerName() << "Length(0)," << getLowerName() << "(nullptr)";
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000409 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000410
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000411 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000412 OS << "llvm::StringRef " << getUpperName();
413 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000414
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000415 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000416 OS << "unsigned " << getLowerName() << "Length;\n";
417 OS << "char *" << getLowerName() << ";";
418 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000419
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000420 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000421 OS << " std::string " << getLowerName()
David L. Jones267b8842017-01-24 01:04:30 +0000422 << "= Record.readString();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000423 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000424
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000425 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000426 OS << getLowerName();
427 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000428
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000429 void writePCHWrite(raw_ostream &OS) const override {
Richard Smith290d8012016-04-06 17:06:00 +0000430 OS << " Record.AddString(SA->get" << getUpperName() << "());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000431 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000432
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000433 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000434 OS << "\\\"\" << get" << getUpperName() << "() << \"\\\"";
435 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000436
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000437 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000438 OS << " OS << \" \\\"\" << SA->get" << getUpperName()
439 << "() << \"\\\"\";\n";
440 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000441 };
442
443 class AlignedArgument : public Argument {
444 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000445 AlignedArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000446 : Argument(Arg, Attr)
447 {}
448
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000449 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000450 OS << " bool is" << getUpperName() << "Dependent() const;\n";
451
452 OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n";
453
454 OS << " bool is" << getUpperName() << "Expr() const {\n";
455 OS << " return is" << getLowerName() << "Expr;\n";
456 OS << " }\n";
457
458 OS << " Expr *get" << getUpperName() << "Expr() const {\n";
459 OS << " assert(is" << getLowerName() << "Expr);\n";
460 OS << " return " << getLowerName() << "Expr;\n";
461 OS << " }\n";
462
463 OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n";
464 OS << " assert(!is" << getLowerName() << "Expr);\n";
465 OS << " return " << getLowerName() << "Type;\n";
466 OS << " }";
467 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000468
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000469 void writeAccessorDefinitions(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000470 OS << "bool " << getAttrName() << "Attr::is" << getUpperName()
471 << "Dependent() const {\n";
472 OS << " if (is" << getLowerName() << "Expr)\n";
473 OS << " return " << getLowerName() << "Expr && (" << getLowerName()
474 << "Expr->isValueDependent() || " << getLowerName()
475 << "Expr->isTypeDependent());\n";
476 OS << " else\n";
477 OS << " return " << getLowerName()
478 << "Type->getType()->isDependentType();\n";
479 OS << "}\n";
480
481 // FIXME: Do not do the calculation here
482 // FIXME: Handle types correctly
483 // A null pointer means maximum alignment
Peter Collingbournebee583f2011-10-06 13:03:08 +0000484 OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
485 << "(ASTContext &Ctx) const {\n";
486 OS << " assert(!is" << getUpperName() << "Dependent());\n";
487 OS << " if (is" << getLowerName() << "Expr)\n";
Ulrich Weigandca3cb7f2015-04-21 17:29:35 +0000488 OS << " return " << getLowerName() << "Expr ? " << getLowerName()
489 << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue()"
490 << " * Ctx.getCharWidth() : "
491 << "Ctx.getTargetDefaultAlignForAttributeAligned();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000492 OS << " else\n";
493 OS << " return 0; // FIXME\n";
494 OS << "}\n";
495 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000496
Richard Smithf26d5512017-08-15 22:58:45 +0000497 void writeASTVisitorTraversal(raw_ostream &OS) const override {
498 StringRef Name = getUpperName();
499 OS << " if (A->is" << Name << "Expr()) {\n"
500 << " if (!getDerived().TraverseStmt(A->get" << Name << "Expr()))\n"
501 << " return false;\n"
502 << " } else if (auto *TSI = A->get" << Name << "Type()) {\n"
503 << " if (!getDerived().TraverseTypeLoc(TSI->getTypeLoc()))\n"
504 << " return false;\n"
505 << " }\n";
506 }
507
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000508 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000509 OS << "is" << getLowerName() << "Expr, is" << getLowerName()
510 << "Expr ? static_cast<void*>(" << getLowerName()
511 << "Expr) : " << getLowerName()
512 << "Type";
513 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000514
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000515 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000516 // FIXME: move the definition in Sema::InstantiateAttrs to here.
517 // In the meantime, aligned attributes are cloned.
518 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000519
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000520 void writeCtorBody(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000521 OS << " if (is" << getLowerName() << "Expr)\n";
522 OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>("
523 << getUpperName() << ");\n";
524 OS << " else\n";
525 OS << " " << getLowerName()
526 << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000527 << ");\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000528 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000529
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000530 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000531 OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)";
532 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000533
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000534 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000535 OS << "is" << getLowerName() << "Expr(false)";
536 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000537
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000538 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000539 OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName();
540 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000541
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000542 void writeImplicitCtorArgs(raw_ostream &OS) const override {
Aaron Ballman36a53502014-01-16 13:03:14 +0000543 OS << "Is" << getUpperName() << "Expr, " << getUpperName();
544 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000545
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000546 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000547 OS << "bool is" << getLowerName() << "Expr;\n";
548 OS << "union {\n";
549 OS << "Expr *" << getLowerName() << "Expr;\n";
550 OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
551 OS << "};";
552 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000553
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000554 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000555 OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr";
556 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000557
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000558 void writePCHReadDecls(raw_ostream &OS) const override {
David L. Jones267b8842017-01-24 01:04:30 +0000559 OS << " bool is" << getLowerName() << "Expr = Record.readInt();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000560 OS << " void *" << getLowerName() << "Ptr;\n";
561 OS << " if (is" << getLowerName() << "Expr)\n";
David L. Jones267b8842017-01-24 01:04:30 +0000562 OS << " " << getLowerName() << "Ptr = Record.readExpr();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000563 OS << " else\n";
564 OS << " " << getLowerName()
David L. Jones267b8842017-01-24 01:04:30 +0000565 << "Ptr = Record.getTypeSourceInfo();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000566 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000567
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000568 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000569 OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n";
570 OS << " if (SA->is" << getUpperName() << "Expr())\n";
Richard Smith290d8012016-04-06 17:06:00 +0000571 OS << " Record.AddStmt(SA->get" << getUpperName() << "Expr());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000572 OS << " else\n";
Richard Smith290d8012016-04-06 17:06:00 +0000573 OS << " Record.AddTypeSourceInfo(SA->get" << getUpperName()
574 << "Type());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000575 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000576
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000577 void writeValue(raw_ostream &OS) const override {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000578 OS << "\";\n";
Aaron Ballmanc960f562014-08-01 13:49:00 +0000579 // The aligned attribute argument expression is optional.
580 OS << " if (is" << getLowerName() << "Expr && "
581 << getLowerName() << "Expr)\n";
Hans Wennborg59dbe862015-09-29 20:56:43 +0000582 OS << " " << getLowerName() << "Expr->printPretty(OS, nullptr, Policy);\n";
Richard Trieuddd01ce2014-06-09 22:53:25 +0000583 OS << " OS << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000584 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000585
586 void writeDump(raw_ostream &OS) const override {}
587
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000588 void writeDumpChildren(raw_ostream &OS) const override {
Richard Smithf7514452014-10-30 21:02:37 +0000589 OS << " if (SA->is" << getUpperName() << "Expr())\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000590 OS << " dumpStmt(SA->get" << getUpperName() << "Expr());\n";
Richard Smithf7514452014-10-30 21:02:37 +0000591 OS << " else\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000592 OS << " dumpType(SA->get" << getUpperName()
593 << "Type()->getType());\n";
594 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000595
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000596 void writeHasChildren(raw_ostream &OS) const override {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000597 OS << "SA->is" << getUpperName() << "Expr()";
598 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000599 };
600
601 class VariadicArgument : public Argument {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000602 std::string Type, ArgName, ArgSizeName, RangeName;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000603
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000604 protected:
605 // Assumed to receive a parameter: raw_ostream OS.
606 virtual void writeValueImpl(raw_ostream &OS) const {
607 OS << " OS << Val;\n";
608 }
609
Peter Collingbournebee583f2011-10-06 13:03:08 +0000610 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000611 VariadicArgument(const Record &Arg, StringRef Attr, std::string T)
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000612 : Argument(Arg, Attr), Type(std::move(T)),
613 ArgName(getLowerName().str() + "_"), ArgSizeName(ArgName + "Size"),
614 RangeName(getLowerName()) {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000615
Benjamin Kramer1b582012016-02-13 18:11:49 +0000616 const std::string &getType() const { return Type; }
617 const std::string &getArgName() const { return ArgName; }
618 const std::string &getArgSizeName() const { return ArgSizeName; }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000619 bool isVariadic() const override { return true; }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000620
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000621 void writeAccessors(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000622 std::string IteratorType = getLowerName().str() + "_iterator";
623 std::string BeginFn = getLowerName().str() + "_begin()";
624 std::string EndFn = getLowerName().str() + "_end()";
625
626 OS << " typedef " << Type << "* " << IteratorType << ";\n";
627 OS << " " << IteratorType << " " << BeginFn << " const {"
628 << " return " << ArgName << "; }\n";
629 OS << " " << IteratorType << " " << EndFn << " const {"
630 << " return " << ArgName << " + " << ArgSizeName << "; }\n";
631 OS << " unsigned " << getLowerName() << "_size() const {"
632 << " return " << ArgSizeName << "; }\n";
633 OS << " llvm::iterator_range<" << IteratorType << "> " << RangeName
634 << "() const { return llvm::make_range(" << BeginFn << ", " << EndFn
635 << "); }\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 writeCloneArgs(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000639 OS << ArgName << ", " << ArgSizeName;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000640 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000641
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000642 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000643 // This isn't elegant, but we have to go through public methods...
644 OS << "A->" << getLowerName() << "_begin(), "
645 << "A->" << getLowerName() << "_size()";
646 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000647
Richard Smithf26d5512017-08-15 22:58:45 +0000648 void writeASTVisitorTraversal(raw_ostream &OS) const override {
649 // FIXME: Traverse the elements.
650 }
651
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000652 void writeCtorBody(raw_ostream &OS) const override {
Aaron Ballmand6459e52014-05-01 15:21:03 +0000653 OS << " std::copy(" << getUpperName() << ", " << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000654 << " + " << ArgSizeName << ", " << ArgName << ");\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000655 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000656
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000657 void writeCtorInitializers(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000658 OS << ArgSizeName << "(" << getUpperName() << "Size), "
659 << ArgName << "(new (Ctx, 16) " << getType() << "["
660 << ArgSizeName << "])";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000661 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000662
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000663 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000664 OS << ArgSizeName << "(0), " << ArgName << "(nullptr)";
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000665 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000666
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000667 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000668 OS << getType() << " *" << getUpperName() << ", unsigned "
669 << getUpperName() << "Size";
670 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000671
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000672 void writeImplicitCtorArgs(raw_ostream &OS) const override {
Aaron Ballman36a53502014-01-16 13:03:14 +0000673 OS << getUpperName() << ", " << getUpperName() << "Size";
674 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000675
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000676 void writeDeclarations(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000677 OS << " unsigned " << ArgSizeName << ";\n";
678 OS << " " << getType() << " *" << ArgName << ";";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000679 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000680
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000681 void writePCHReadDecls(raw_ostream &OS) const override {
David L. Jones267b8842017-01-24 01:04:30 +0000682 OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n";
Richard Smith19978562016-05-18 00:16:51 +0000683 OS << " SmallVector<" << getType() << ", 4> "
684 << getLowerName() << ";\n";
685 OS << " " << getLowerName() << ".reserve(" << getLowerName()
Peter Collingbournebee583f2011-10-06 13:03:08 +0000686 << "Size);\n";
Richard Smith19978562016-05-18 00:16:51 +0000687
688 // If we can't store the values in the current type (if it's something
689 // like StringRef), store them in a different type and convert the
690 // container afterwards.
691 std::string StorageType = getStorageType(getType());
692 std::string StorageName = getLowerName();
693 if (StorageType != getType()) {
694 StorageName += "Storage";
695 OS << " SmallVector<" << StorageType << ", 4> "
696 << StorageName << ";\n";
697 OS << " " << StorageName << ".reserve(" << getLowerName()
698 << "Size);\n";
699 }
700
701 OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n";
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000702 std::string read = ReadPCHRecord(Type);
Richard Smith19978562016-05-18 00:16:51 +0000703 OS << " " << StorageName << ".push_back(" << read << ");\n";
704
705 if (StorageType != getType()) {
706 OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n";
707 OS << " " << getLowerName() << ".push_back("
708 << StorageName << "[i]);\n";
709 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000710 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000711
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000712 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000713 OS << getLowerName() << ".data(), " << getLowerName() << "Size";
714 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000715
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000716 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000717 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000718 OS << " for (auto &Val : SA->" << RangeName << "())\n";
719 OS << " " << WritePCHRecord(Type, "Val");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000720 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000721
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000722 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000723 OS << "\";\n";
724 OS << " bool isFirst = true;\n"
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000725 << " for (const auto &Val : " << RangeName << "()) {\n"
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000726 << " if (isFirst) isFirst = false;\n"
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000727 << " else OS << \", \";\n";
728 writeValueImpl(OS);
729 OS << " }\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000730 OS << " OS << \"";
731 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000732
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000733 void writeDump(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000734 OS << " for (const auto &Val : SA->" << RangeName << "())\n";
735 OS << " OS << \" \" << Val;\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000736 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000737 };
738
Reid Klecknerf526b9482014-02-12 18:22:18 +0000739 // Unique the enums, but maintain the original declaration ordering.
Craig Topper00648582017-05-31 19:01:22 +0000740 std::vector<StringRef>
741 uniqueEnumsInOrder(const std::vector<StringRef> &enums) {
742 std::vector<StringRef> uniques;
George Burgess IV1881a572016-12-01 00:13:18 +0000743 SmallDenseSet<StringRef, 8> unique_set;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000744 for (const auto &i : enums) {
George Burgess IV1881a572016-12-01 00:13:18 +0000745 if (unique_set.insert(i).second)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000746 uniques.push_back(i);
Reid Klecknerf526b9482014-02-12 18:22:18 +0000747 }
748 return uniques;
749 }
750
Peter Collingbournebee583f2011-10-06 13:03:08 +0000751 class EnumArgument : public Argument {
752 std::string type;
Craig Topper00648582017-05-31 19:01:22 +0000753 std::vector<StringRef> values, enums, uniques;
754
Peter Collingbournebee583f2011-10-06 13:03:08 +0000755 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000756 EnumArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000757 : Argument(Arg, Attr), type(Arg.getValueAsString("Type")),
Aaron Ballman0e468c02014-01-05 21:08:29 +0000758 values(Arg.getValueAsListOfStrings("Values")),
759 enums(Arg.getValueAsListOfStrings("Enums")),
Reid Klecknerf526b9482014-02-12 18:22:18 +0000760 uniques(uniqueEnumsInOrder(enums))
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000761 {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000762 // FIXME: Emit a proper error
763 assert(!uniques.empty());
764 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000765
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000766 bool isEnumArg() const override { return true; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000767
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000768 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000769 OS << " " << type << " get" << getUpperName() << "() const {\n";
770 OS << " return " << getLowerName() << ";\n";
771 OS << " }";
772 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000773
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000774 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000775 OS << getLowerName();
776 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000777
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000778 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000779 OS << "A->get" << getUpperName() << "()";
780 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000781 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000782 OS << getLowerName() << "(" << getUpperName() << ")";
783 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000784 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000785 OS << getLowerName() << "(" << type << "(0))";
786 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000787 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000788 OS << type << " " << getUpperName();
789 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000790 void writeDeclarations(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +0000791 auto i = uniques.cbegin(), e = uniques.cend();
Peter Collingbournebee583f2011-10-06 13:03:08 +0000792 // The last one needs to not have a comma.
793 --e;
794
795 OS << "public:\n";
796 OS << " enum " << type << " {\n";
797 for (; i != e; ++i)
798 OS << " " << *i << ",\n";
799 OS << " " << *e << "\n";
800 OS << " };\n";
801 OS << "private:\n";
802 OS << " " << type << " " << getLowerName() << ";";
803 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000804
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000805 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000806 OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName()
807 << "(static_cast<" << getAttrName() << "Attr::" << type
David L. Jones267b8842017-01-24 01:04:30 +0000808 << ">(Record.readInt()));\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000809 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000810
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000811 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000812 OS << getLowerName();
813 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000814
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000815 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000816 OS << "Record.push_back(SA->get" << getUpperName() << "());\n";
817 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000818
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000819 void writeValue(raw_ostream &OS) const override {
Aaron Ballman36d79102014-09-15 16:16:14 +0000820 // FIXME: this isn't 100% correct -- some enum arguments require printing
821 // as a string literal, while others require printing as an identifier.
822 // Tablegen currently does not distinguish between the two forms.
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000823 OS << "\\\"\" << " << getAttrName() << "Attr::Convert" << type << "ToStr(get"
824 << getUpperName() << "()) << \"\\\"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000825 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000826
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000827 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000828 OS << " switch(SA->get" << getUpperName() << "()) {\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000829 for (const auto &I : uniques) {
830 OS << " case " << getAttrName() << "Attr::" << I << ":\n";
831 OS << " OS << \" " << I << "\";\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000832 OS << " break;\n";
833 }
834 OS << " }\n";
835 }
Aaron Ballman682ee422013-09-11 19:47:58 +0000836
837 void writeConversion(raw_ostream &OS) const {
838 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
839 OS << type << " &Out) {\n";
840 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000841 OS << type << ">>(Val)\n";
Aaron Ballman682ee422013-09-11 19:47:58 +0000842 for (size_t I = 0; I < enums.size(); ++I) {
843 OS << " .Case(\"" << values[I] << "\", ";
844 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
845 }
846 OS << " .Default(Optional<" << type << ">());\n";
847 OS << " if (R) {\n";
848 OS << " Out = *R;\n return true;\n }\n";
849 OS << " return false;\n";
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000850 OS << " }\n\n";
851
852 // Mapping from enumeration values back to enumeration strings isn't
853 // trivial because some enumeration values have multiple named
854 // enumerators, such as type_visibility(internal) and
855 // type_visibility(hidden) both mapping to TypeVisibilityAttr::Hidden.
856 OS << " static const char *Convert" << type << "ToStr("
857 << type << " Val) {\n"
858 << " switch(Val) {\n";
George Burgess IV1881a572016-12-01 00:13:18 +0000859 SmallDenseSet<StringRef, 8> Uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000860 for (size_t I = 0; I < enums.size(); ++I) {
861 if (Uniques.insert(enums[I]).second)
862 OS << " case " << getAttrName() << "Attr::" << enums[I]
863 << ": return \"" << values[I] << "\";\n";
864 }
865 OS << " }\n"
866 << " llvm_unreachable(\"No enumerator with that value\");\n"
867 << " }\n";
Aaron Ballman682ee422013-09-11 19:47:58 +0000868 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000869 };
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000870
871 class VariadicEnumArgument: public VariadicArgument {
872 std::string type, QualifiedTypeName;
Craig Topper00648582017-05-31 19:01:22 +0000873 std::vector<StringRef> values, enums, uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000874
875 protected:
876 void writeValueImpl(raw_ostream &OS) const override {
Aaron Ballman36d79102014-09-15 16:16:14 +0000877 // FIXME: this isn't 100% correct -- some enum arguments require printing
878 // as a string literal, while others require printing as an identifier.
879 // Tablegen currently does not distinguish between the two forms.
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000880 OS << " OS << \"\\\"\" << " << getAttrName() << "Attr::Convert" << type
881 << "ToStr(Val)" << "<< \"\\\"\";\n";
882 }
883
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000884 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000885 VariadicEnumArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000886 : VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")),
887 type(Arg.getValueAsString("Type")),
Aaron Ballman0e468c02014-01-05 21:08:29 +0000888 values(Arg.getValueAsListOfStrings("Values")),
889 enums(Arg.getValueAsListOfStrings("Enums")),
Reid Klecknerf526b9482014-02-12 18:22:18 +0000890 uniques(uniqueEnumsInOrder(enums))
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000891 {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000892 QualifiedTypeName = getAttrName().str() + "Attr::" + type;
893
894 // FIXME: Emit a proper error
895 assert(!uniques.empty());
896 }
897
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000898 bool isVariadicEnumArg() const override { return true; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000899
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000900 void writeDeclarations(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +0000901 auto i = uniques.cbegin(), e = uniques.cend();
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000902 // The last one needs to not have a comma.
903 --e;
904
905 OS << "public:\n";
906 OS << " enum " << type << " {\n";
907 for (; i != e; ++i)
908 OS << " " << *i << ",\n";
909 OS << " " << *e << "\n";
910 OS << " };\n";
911 OS << "private:\n";
912
913 VariadicArgument::writeDeclarations(OS);
914 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000915
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000916 void writeDump(raw_ostream &OS) const override {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000917 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
918 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
919 << getLowerName() << "_end(); I != E; ++I) {\n";
920 OS << " switch(*I) {\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000921 for (const auto &UI : uniques) {
922 OS << " case " << getAttrName() << "Attr::" << UI << ":\n";
923 OS << " OS << \" " << UI << "\";\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000924 OS << " break;\n";
925 }
926 OS << " }\n";
927 OS << " }\n";
928 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000929
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000930 void writePCHReadDecls(raw_ostream &OS) const override {
David L. Jones267b8842017-01-24 01:04:30 +0000931 OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000932 OS << " SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName()
933 << ";\n";
934 OS << " " << getLowerName() << ".reserve(" << getLowerName()
935 << "Size);\n";
936 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
937 OS << " " << getLowerName() << ".push_back(" << "static_cast<"
David L. Jones267b8842017-01-24 01:04:30 +0000938 << QualifiedTypeName << ">(Record.readInt()));\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000939 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000940
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000941 void writePCHWrite(raw_ostream &OS) const override {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000942 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
943 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
944 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
945 << getLowerName() << "_end(); i != e; ++i)\n";
946 OS << " " << WritePCHRecord(QualifiedTypeName, "(*i)");
947 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000948
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000949 void writeConversion(raw_ostream &OS) const {
950 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
951 OS << type << " &Out) {\n";
952 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000953 OS << type << ">>(Val)\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000954 for (size_t I = 0; I < enums.size(); ++I) {
955 OS << " .Case(\"" << values[I] << "\", ";
956 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
957 }
958 OS << " .Default(Optional<" << type << ">());\n";
959 OS << " if (R) {\n";
960 OS << " Out = *R;\n return true;\n }\n";
961 OS << " return false;\n";
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000962 OS << " }\n\n";
963
964 OS << " static const char *Convert" << type << "ToStr("
965 << type << " Val) {\n"
966 << " switch(Val) {\n";
George Burgess IV1881a572016-12-01 00:13:18 +0000967 SmallDenseSet<StringRef, 8> Uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000968 for (size_t I = 0; I < enums.size(); ++I) {
969 if (Uniques.insert(enums[I]).second)
970 OS << " case " << getAttrName() << "Attr::" << enums[I]
971 << ": return \"" << values[I] << "\";\n";
972 }
973 OS << " }\n"
974 << " llvm_unreachable(\"No enumerator with that value\");\n"
975 << " }\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000976 }
977 };
Peter Collingbournebee583f2011-10-06 13:03:08 +0000978
979 class VersionArgument : public Argument {
980 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000981 VersionArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000982 : Argument(Arg, Attr)
983 {}
984
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000985 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000986 OS << " VersionTuple get" << getUpperName() << "() const {\n";
987 OS << " return " << getLowerName() << ";\n";
988 OS << " }\n";
989 OS << " void set" << getUpperName()
990 << "(ASTContext &C, VersionTuple V) {\n";
991 OS << " " << getLowerName() << " = V;\n";
992 OS << " }";
993 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000994
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000995 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000996 OS << "get" << getUpperName() << "()";
997 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000998
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000999 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001000 OS << "A->get" << getUpperName() << "()";
1001 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001002
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001003 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001004 OS << getLowerName() << "(" << getUpperName() << ")";
1005 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001006
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001007 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001008 OS << getLowerName() << "()";
1009 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001010
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001011 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001012 OS << "VersionTuple " << getUpperName();
1013 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001014
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001015 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001016 OS << "VersionTuple " << getLowerName() << ";\n";
1017 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001018
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001019 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001020 OS << " VersionTuple " << getLowerName()
David L. Jones267b8842017-01-24 01:04:30 +00001021 << "= Record.readVersionTuple();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001022 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001023
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001024 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001025 OS << getLowerName();
1026 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001027
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001028 void writePCHWrite(raw_ostream &OS) const override {
Richard Smith290d8012016-04-06 17:06:00 +00001029 OS << " Record.AddVersionTuple(SA->get" << getUpperName() << "());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001030 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001031
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001032 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001033 OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";
1034 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001035
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001036 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001037 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
1038 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001039 };
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001040
1041 class ExprArgument : public SimpleArgument {
1042 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001043 ExprArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001044 : SimpleArgument(Arg, Attr, "Expr *")
1045 {}
1046
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001047 void writeASTVisitorTraversal(raw_ostream &OS) const override {
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001048 OS << " if (!"
1049 << "getDerived().TraverseStmt(A->get" << getUpperName() << "()))\n";
1050 OS << " return false;\n";
1051 }
1052
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001053 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001054 OS << "tempInst" << getUpperName();
1055 }
1056
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001057 void writeTemplateInstantiation(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001058 OS << " " << getType() << " tempInst" << getUpperName() << ";\n";
1059 OS << " {\n";
1060 OS << " EnterExpressionEvaluationContext "
Faisal Valid143a0c2017-04-01 21:30:49 +00001061 << "Unevaluated(S, Sema::ExpressionEvaluationContext::Unevaluated);\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001062 OS << " ExprResult " << "Result = S.SubstExpr("
1063 << "A->get" << getUpperName() << "(), TemplateArgs);\n";
1064 OS << " tempInst" << getUpperName() << " = "
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001065 << "Result.getAs<Expr>();\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001066 OS << " }\n";
1067 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001068
Craig Topper3164f332014-03-11 03:39:26 +00001069 void writeDump(raw_ostream &OS) const override {}
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001070
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001071 void writeDumpChildren(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001072 OS << " dumpStmt(SA->get" << getUpperName() << "());\n";
1073 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001074
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001075 void writeHasChildren(raw_ostream &OS) const override { OS << "true"; }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001076 };
1077
1078 class VariadicExprArgument : public VariadicArgument {
1079 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001080 VariadicExprArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001081 : VariadicArgument(Arg, Attr, "Expr *")
1082 {}
1083
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001084 void writeASTVisitorTraversal(raw_ostream &OS) const override {
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001085 OS << " {\n";
1086 OS << " " << getType() << " *I = A->" << getLowerName()
1087 << "_begin();\n";
1088 OS << " " << getType() << " *E = A->" << getLowerName()
1089 << "_end();\n";
1090 OS << " for (; I != E; ++I) {\n";
1091 OS << " if (!getDerived().TraverseStmt(*I))\n";
1092 OS << " return false;\n";
1093 OS << " }\n";
1094 OS << " }\n";
1095 }
1096
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001097 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001098 OS << "tempInst" << getUpperName() << ", "
1099 << "A->" << getLowerName() << "_size()";
1100 }
1101
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001102 void writeTemplateInstantiation(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +00001103 OS << " auto *tempInst" << getUpperName()
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001104 << " = new (C, 16) " << getType()
1105 << "[A->" << getLowerName() << "_size()];\n";
1106 OS << " {\n";
1107 OS << " EnterExpressionEvaluationContext "
Faisal Valid143a0c2017-04-01 21:30:49 +00001108 << "Unevaluated(S, Sema::ExpressionEvaluationContext::Unevaluated);\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001109 OS << " " << getType() << " *TI = tempInst" << getUpperName()
1110 << ";\n";
1111 OS << " " << getType() << " *I = A->" << getLowerName()
1112 << "_begin();\n";
1113 OS << " " << getType() << " *E = A->" << getLowerName()
1114 << "_end();\n";
1115 OS << " for (; I != E; ++I, ++TI) {\n";
1116 OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n";
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001117 OS << " *TI = Result.getAs<Expr>();\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001118 OS << " }\n";
1119 OS << " }\n";
1120 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001121
Craig Topper3164f332014-03-11 03:39:26 +00001122 void writeDump(raw_ostream &OS) const override {}
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001123
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001124 void writeDumpChildren(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001125 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
1126 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
Richard Smithf7514452014-10-30 21:02:37 +00001127 << getLowerName() << "_end(); I != E; ++I)\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001128 OS << " dumpStmt(*I);\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001129 }
1130
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001131 void writeHasChildren(raw_ostream &OS) const override {
Richard Trieude5cc7d2013-01-31 01:44:26 +00001132 OS << "SA->" << getLowerName() << "_begin() != "
1133 << "SA->" << getLowerName() << "_end()";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001134 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001135 };
Richard Smithb87c4652013-10-31 21:23:20 +00001136
Peter Collingbourne915df992015-05-15 18:33:32 +00001137 class VariadicStringArgument : public VariadicArgument {
1138 public:
1139 VariadicStringArgument(const Record &Arg, StringRef Attr)
Benjamin Kramer1b582012016-02-13 18:11:49 +00001140 : VariadicArgument(Arg, Attr, "StringRef")
Peter Collingbourne915df992015-05-15 18:33:32 +00001141 {}
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001142
Benjamin Kramer1b582012016-02-13 18:11:49 +00001143 void writeCtorBody(raw_ostream &OS) const override {
1144 OS << " for (size_t I = 0, E = " << getArgSizeName() << "; I != E;\n"
1145 " ++I) {\n"
1146 " StringRef Ref = " << getUpperName() << "[I];\n"
1147 " if (!Ref.empty()) {\n"
1148 " char *Mem = new (Ctx, 1) char[Ref.size()];\n"
1149 " std::memcpy(Mem, Ref.data(), Ref.size());\n"
1150 " " << getArgName() << "[I] = StringRef(Mem, Ref.size());\n"
1151 " }\n"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001152 " }\n";
Benjamin Kramer1b582012016-02-13 18:11:49 +00001153 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001154
Peter Collingbourne915df992015-05-15 18:33:32 +00001155 void writeValueImpl(raw_ostream &OS) const override {
1156 OS << " OS << \"\\\"\" << Val << \"\\\"\";\n";
1157 }
1158 };
1159
Richard Smithb87c4652013-10-31 21:23:20 +00001160 class TypeArgument : public SimpleArgument {
1161 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001162 TypeArgument(const Record &Arg, StringRef Attr)
Richard Smithb87c4652013-10-31 21:23:20 +00001163 : SimpleArgument(Arg, Attr, "TypeSourceInfo *")
1164 {}
1165
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001166 void writeAccessors(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001167 OS << " QualType get" << getUpperName() << "() const {\n";
1168 OS << " return " << getLowerName() << "->getType();\n";
1169 OS << " }";
1170 OS << " " << getType() << " get" << getUpperName() << "Loc() const {\n";
1171 OS << " return " << getLowerName() << ";\n";
1172 OS << " }";
1173 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001174
Richard Smithf26d5512017-08-15 22:58:45 +00001175 void writeASTVisitorTraversal(raw_ostream &OS) const override {
1176 OS << " if (auto *TSI = A->get" << getUpperName() << "Loc())\n";
1177 OS << " if (!getDerived().TraverseTypeLoc(TSI->getTypeLoc()))\n";
1178 OS << " return false;\n";
1179 }
1180
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001181 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001182 OS << "A->get" << getUpperName() << "Loc()";
1183 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001184
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001185 void writePCHWrite(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001186 OS << " " << WritePCHRecord(
1187 getType(), "SA->get" + std::string(getUpperName()) + "Loc()");
1188 }
1189 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001190
Hans Wennborgdcfba332015-10-06 23:40:43 +00001191} // end anonymous namespace
Peter Collingbournebee583f2011-10-06 13:03:08 +00001192
Aaron Ballman2f22b942014-05-20 19:47:14 +00001193static std::unique_ptr<Argument>
1194createArgument(const Record &Arg, StringRef Attr,
1195 const Record *Search = nullptr) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001196 if (!Search)
1197 Search = &Arg;
1198
David Blaikie28f30ca2014-08-08 23:59:38 +00001199 std::unique_ptr<Argument> Ptr;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001200 llvm::StringRef ArgName = Search->getName();
1201
David Blaikie28f30ca2014-08-08 23:59:38 +00001202 if (ArgName == "AlignedArgument")
1203 Ptr = llvm::make_unique<AlignedArgument>(Arg, Attr);
1204 else if (ArgName == "EnumArgument")
1205 Ptr = llvm::make_unique<EnumArgument>(Arg, Attr);
1206 else if (ArgName == "ExprArgument")
1207 Ptr = llvm::make_unique<ExprArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001208 else if (ArgName == "FunctionArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001209 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "FunctionDecl *");
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001210 else if (ArgName == "NamedArgument")
1211 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "NamedDecl *");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001212 else if (ArgName == "IdentifierArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001213 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "IdentifierInfo *");
David Majnemer4bb09802014-02-10 19:50:15 +00001214 else if (ArgName == "DefaultBoolArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001215 Ptr = llvm::make_unique<DefaultSimpleArgument>(
1216 Arg, Attr, "bool", Arg.getValueAsBit("Default"));
1217 else if (ArgName == "BoolArgument")
1218 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "bool");
Aaron Ballman18a78382013-11-21 00:28:23 +00001219 else if (ArgName == "DefaultIntArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001220 Ptr = llvm::make_unique<DefaultSimpleArgument>(
1221 Arg, Attr, "int", Arg.getValueAsInt("Default"));
1222 else if (ArgName == "IntArgument")
1223 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "int");
1224 else if (ArgName == "StringArgument")
1225 Ptr = llvm::make_unique<StringArgument>(Arg, Attr);
1226 else if (ArgName == "TypeArgument")
1227 Ptr = llvm::make_unique<TypeArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001228 else if (ArgName == "UnsignedArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001229 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "unsigned");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001230 else if (ArgName == "VariadicUnsignedArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001231 Ptr = llvm::make_unique<VariadicArgument>(Arg, Attr, "unsigned");
Peter Collingbourne915df992015-05-15 18:33:32 +00001232 else if (ArgName == "VariadicStringArgument")
1233 Ptr = llvm::make_unique<VariadicStringArgument>(Arg, Attr);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001234 else if (ArgName == "VariadicEnumArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001235 Ptr = llvm::make_unique<VariadicEnumArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001236 else if (ArgName == "VariadicExprArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001237 Ptr = llvm::make_unique<VariadicExprArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001238 else if (ArgName == "VersionArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001239 Ptr = llvm::make_unique<VersionArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001240
1241 if (!Ptr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00001242 // Search in reverse order so that the most-derived type is handled first.
Craig Topper25761242016-01-18 19:52:54 +00001243 ArrayRef<std::pair<Record*, SMRange>> Bases = Search->getSuperClasses();
David Majnemerf7e36092016-06-23 00:15:04 +00001244 for (const auto &Base : llvm::reverse(Bases)) {
Craig Topper25761242016-01-18 19:52:54 +00001245 if ((Ptr = createArgument(Arg, Attr, Base.first)))
Peter Collingbournebee583f2011-10-06 13:03:08 +00001246 break;
1247 }
1248 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001249
1250 if (Ptr && Arg.getValueAsBit("Optional"))
1251 Ptr->setOptional(true);
1252
John McCalla62c1a92015-10-28 00:17:34 +00001253 if (Ptr && Arg.getValueAsBit("Fake"))
1254 Ptr->setFake(true);
1255
David Blaikie28f30ca2014-08-08 23:59:38 +00001256 return Ptr;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001257}
1258
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001259static void writeAvailabilityValue(raw_ostream &OS) {
1260 OS << "\" << getPlatform()->getName();\n"
Manman Ren42e09eb2016-03-10 23:54:12 +00001261 << " if (getStrict()) OS << \", strict\";\n"
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001262 << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n"
1263 << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n"
1264 << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n"
1265 << " if (getUnavailable()) OS << \", unavailable\";\n"
1266 << " OS << \"";
1267}
1268
Manman Renc7890fe2016-03-16 18:50:49 +00001269static void writeDeprecatedAttrValue(raw_ostream &OS, std::string &Variety) {
1270 OS << "\\\"\" << getMessage() << \"\\\"\";\n";
1271 // Only GNU deprecated has an optional fixit argument at the second position.
1272 if (Variety == "GNU")
1273 OS << " if (!getReplacement().empty()) OS << \", \\\"\""
1274 " << getReplacement() << \"\\\"\";\n";
1275 OS << " OS << \"";
1276}
1277
Aaron Ballman3e424b52013-12-26 18:30:57 +00001278static void writeGetSpellingFunction(Record &R, raw_ostream &OS) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001279 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballman3e424b52013-12-26 18:30:57 +00001280
1281 OS << "const char *" << R.getName() << "Attr::getSpelling() const {\n";
1282 if (Spellings.empty()) {
1283 OS << " return \"(No spelling)\";\n}\n\n";
1284 return;
1285 }
1286
1287 OS << " switch (SpellingListIndex) {\n"
1288 " default:\n"
1289 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1290 " return \"(No spelling)\";\n";
1291
1292 for (unsigned I = 0; I < Spellings.size(); ++I)
1293 OS << " case " << I << ":\n"
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001294 " return \"" << Spellings[I].name() << "\";\n";
Aaron Ballman3e424b52013-12-26 18:30:57 +00001295 // End of the switch statement.
1296 OS << " }\n";
1297 // End of the getSpelling function.
1298 OS << "}\n\n";
1299}
1300
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001301static void
1302writePrettyPrintFunction(Record &R,
1303 const std::vector<std::unique_ptr<Argument>> &Args,
1304 raw_ostream &OS) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001305 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Michael Han99315932013-01-24 16:46:58 +00001306
1307 OS << "void " << R.getName() << "Attr::printPretty("
1308 << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n";
1309
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001310 if (Spellings.empty()) {
Michael Han99315932013-01-24 16:46:58 +00001311 OS << "}\n\n";
1312 return;
1313 }
1314
1315 OS <<
1316 " switch (SpellingListIndex) {\n"
1317 " default:\n"
1318 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1319 " break;\n";
1320
1321 for (unsigned I = 0; I < Spellings.size(); ++ I) {
1322 llvm::SmallString<16> Prefix;
1323 llvm::SmallString<8> Suffix;
1324 // The actual spelling of the name and namespace (if applicable)
1325 // of an attribute without considering prefix and suffix.
1326 llvm::SmallString<64> Spelling;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001327 std::string Name = Spellings[I].name();
1328 std::string Variety = Spellings[I].variety();
Michael Han99315932013-01-24 16:46:58 +00001329
1330 if (Variety == "GNU") {
1331 Prefix = " __attribute__((";
1332 Suffix = "))";
Aaron Ballman606093a2017-10-15 15:01:42 +00001333 } else if (Variety == "CXX11" || Variety == "C2x") {
Michael Han99315932013-01-24 16:46:58 +00001334 Prefix = " [[";
1335 Suffix = "]]";
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001336 std::string Namespace = Spellings[I].nameSpace();
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001337 if (!Namespace.empty()) {
Michael Han99315932013-01-24 16:46:58 +00001338 Spelling += Namespace;
1339 Spelling += "::";
1340 }
1341 } else if (Variety == "Declspec") {
1342 Prefix = " __declspec(";
1343 Suffix = ")";
Nico Weber20e08042016-09-03 02:55:10 +00001344 } else if (Variety == "Microsoft") {
1345 Prefix = "[";
1346 Suffix = "]";
Richard Smith0cdcc982013-01-29 01:24:26 +00001347 } else if (Variety == "Keyword") {
1348 Prefix = " ";
1349 Suffix = "";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001350 } else if (Variety == "Pragma") {
1351 Prefix = "#pragma ";
1352 Suffix = "\n";
1353 std::string Namespace = Spellings[I].nameSpace();
1354 if (!Namespace.empty()) {
1355 Spelling += Namespace;
1356 Spelling += " ";
1357 }
Michael Han99315932013-01-24 16:46:58 +00001358 } else {
Richard Smith0cdcc982013-01-29 01:24:26 +00001359 llvm_unreachable("Unknown attribute syntax variety!");
Michael Han99315932013-01-24 16:46:58 +00001360 }
1361
1362 Spelling += Name;
1363
1364 OS <<
1365 " case " << I << " : {\n"
Yaron Keren09fb7c62015-03-10 07:33:23 +00001366 " OS << \"" << Prefix << Spelling;
Michael Han99315932013-01-24 16:46:58 +00001367
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001368 if (Variety == "Pragma") {
1369 OS << " \";\n";
1370 OS << " printPrettyPragma(OS, Policy);\n";
Alexey Bataev6d455322015-10-12 06:59:48 +00001371 OS << " OS << \"\\n\";";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001372 OS << " break;\n";
1373 OS << " }\n";
1374 continue;
1375 }
1376
John McCalla62c1a92015-10-28 00:17:34 +00001377 // Fake arguments aren't part of the parsed form and should not be
1378 // pretty-printed.
George Burgess IV1881a572016-12-01 00:13:18 +00001379 bool hasNonFakeArgs = llvm::any_of(
1380 Args, [](const std::unique_ptr<Argument> &A) { return !A->isFake(); });
John McCalla62c1a92015-10-28 00:17:34 +00001381
Aaron Ballmanc960f562014-08-01 13:49:00 +00001382 // FIXME: always printing the parenthesis isn't the correct behavior for
1383 // attributes which have optional arguments that were not provided. For
1384 // instance: __attribute__((aligned)) will be pretty printed as
1385 // __attribute__((aligned())). The logic should check whether there is only
1386 // a single argument, and if it is optional, whether it has been provided.
John McCalla62c1a92015-10-28 00:17:34 +00001387 if (hasNonFakeArgs)
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001388 OS << "(";
Michael Han99315932013-01-24 16:46:58 +00001389 if (Spelling == "availability") {
1390 writeAvailabilityValue(OS);
Manman Renc7890fe2016-03-16 18:50:49 +00001391 } else if (Spelling == "deprecated" || Spelling == "gnu::deprecated") {
1392 writeDeprecatedAttrValue(OS, Variety);
Michael Han99315932013-01-24 16:46:58 +00001393 } else {
John McCalla62c1a92015-10-28 00:17:34 +00001394 unsigned index = 0;
1395 for (const auto &arg : Args) {
1396 if (arg->isFake()) continue;
1397 if (index++) OS << ", ";
1398 arg->writeValue(OS);
Michael Han99315932013-01-24 16:46:58 +00001399 }
1400 }
1401
John McCalla62c1a92015-10-28 00:17:34 +00001402 if (hasNonFakeArgs)
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001403 OS << ")";
Yaron Keren09fb7c62015-03-10 07:33:23 +00001404 OS << Suffix + "\";\n";
Michael Han99315932013-01-24 16:46:58 +00001405
1406 OS <<
1407 " break;\n"
1408 " }\n";
1409 }
1410
1411 // End of the switch statement.
1412 OS << "}\n";
1413 // End of the print function.
1414 OS << "}\n\n";
1415}
1416
Michael Hanaf02bbe2013-02-01 01:19:17 +00001417/// \brief Return the index of a spelling in a spelling list.
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001418static unsigned
1419getSpellingListIndex(const std::vector<FlattenedSpelling> &SpellingList,
1420 const FlattenedSpelling &Spelling) {
Alexander Kornienko6ee521c2015-01-23 15:36:10 +00001421 assert(!SpellingList.empty() && "Spelling list is empty!");
Michael Hanaf02bbe2013-02-01 01:19:17 +00001422
1423 for (unsigned Index = 0; Index < SpellingList.size(); ++Index) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001424 const FlattenedSpelling &S = SpellingList[Index];
1425 if (S.variety() != Spelling.variety())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001426 continue;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001427 if (S.nameSpace() != Spelling.nameSpace())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001428 continue;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001429 if (S.name() != Spelling.name())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001430 continue;
1431
1432 return Index;
1433 }
1434
1435 llvm_unreachable("Unknown spelling!");
1436}
1437
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001438static void writeAttrAccessorDefinition(const Record &R, raw_ostream &OS) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00001439 std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors");
George Burgess IV1881a572016-12-01 00:13:18 +00001440 if (Accessors.empty())
1441 return;
1442
1443 const std::vector<FlattenedSpelling> SpellingList = GetFlattenedSpellings(R);
1444 assert(!SpellingList.empty() &&
1445 "Attribute with empty spelling list can't have accessors!");
Aaron Ballman2f22b942014-05-20 19:47:14 +00001446 for (const auto *Accessor : Accessors) {
Erich Keane3bff4142017-10-16 23:25:24 +00001447 const StringRef Name = Accessor->getValueAsString("Name");
George Burgess IV1881a572016-12-01 00:13:18 +00001448 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Accessor);
Michael Hanaf02bbe2013-02-01 01:19:17 +00001449
1450 OS << " bool " << Name << "() const { return SpellingListIndex == ";
1451 for (unsigned Index = 0; Index < Spellings.size(); ++Index) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001452 OS << getSpellingListIndex(SpellingList, Spellings[Index]);
George Burgess IV1881a572016-12-01 00:13:18 +00001453 if (Index != Spellings.size() - 1)
Michael Hanaf02bbe2013-02-01 01:19:17 +00001454 OS << " ||\n SpellingListIndex == ";
1455 else
1456 OS << "; }\n";
1457 }
1458 }
1459}
1460
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001461static bool
1462SpellingNamesAreCommon(const std::vector<FlattenedSpelling>& Spellings) {
Aaron Ballman36a53502014-01-16 13:03:14 +00001463 assert(!Spellings.empty() && "An empty list of spellings was provided");
1464 std::string FirstName = NormalizeNameForSpellingComparison(
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001465 Spellings.front().name());
Aaron Ballman2f22b942014-05-20 19:47:14 +00001466 for (const auto &Spelling :
1467 llvm::make_range(std::next(Spellings.begin()), Spellings.end())) {
1468 std::string Name = NormalizeNameForSpellingComparison(Spelling.name());
Aaron Ballman36a53502014-01-16 13:03:14 +00001469 if (Name != FirstName)
1470 return false;
1471 }
1472 return true;
1473}
1474
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001475typedef std::map<unsigned, std::string> SemanticSpellingMap;
1476static std::string
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001477CreateSemanticSpellings(const std::vector<FlattenedSpelling> &Spellings,
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001478 SemanticSpellingMap &Map) {
1479 // The enumerants are automatically generated based on the variety,
1480 // namespace (if present) and name for each attribute spelling. However,
1481 // care is taken to avoid trampling on the reserved namespace due to
1482 // underscores.
1483 std::string Ret(" enum Spelling {\n");
1484 std::set<std::string> Uniques;
1485 unsigned Idx = 0;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001486 for (auto I = Spellings.begin(), E = Spellings.end(); I != E; ++I, ++Idx) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001487 const FlattenedSpelling &S = *I;
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00001488 const std::string &Variety = S.variety();
1489 const std::string &Spelling = S.name();
1490 const std::string &Namespace = S.nameSpace();
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001491 std::string EnumName;
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001492
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001493 EnumName += (Variety + "_");
1494 if (!Namespace.empty())
1495 EnumName += (NormalizeNameForSpellingComparison(Namespace).str() +
1496 "_");
1497 EnumName += NormalizeNameForSpellingComparison(Spelling);
1498
1499 // Even if the name is not unique, this spelling index corresponds to a
1500 // particular enumerant name that we've calculated.
1501 Map[Idx] = EnumName;
1502
1503 // Since we have been stripping underscores to avoid trampling on the
1504 // reserved namespace, we may have inadvertently created duplicate
1505 // enumerant names. These duplicates are not considered part of the
1506 // semantic spelling, and can be elided.
1507 if (Uniques.find(EnumName) != Uniques.end())
1508 continue;
1509
1510 Uniques.insert(EnumName);
1511 if (I != Spellings.begin())
1512 Ret += ",\n";
Aaron Ballman9bf6b752015-03-10 17:19:18 +00001513 // Duplicate spellings are not considered part of the semantic spelling
1514 // enumeration, but the spelling index and semantic spelling values are
1515 // meant to be equivalent, so we must specify a concrete value for each
1516 // enumerator.
1517 Ret += " " + EnumName + " = " + llvm::utostr(Idx);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001518 }
1519 Ret += "\n };\n\n";
1520 return Ret;
1521}
1522
1523void WriteSemanticSpellingSwitch(const std::string &VarName,
1524 const SemanticSpellingMap &Map,
1525 raw_ostream &OS) {
1526 OS << " switch (" << VarName << ") {\n default: "
1527 << "llvm_unreachable(\"Unknown spelling list index\");\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001528 for (const auto &I : Map)
1529 OS << " case " << I.first << ": return " << I.second << ";\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001530 OS << " }\n";
1531}
1532
Aaron Ballman35db2b32014-01-29 22:13:45 +00001533// Emits the LateParsed property for attributes.
1534static void emitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) {
1535 OS << "#if defined(CLANG_ATTR_LATE_PARSED_LIST)\n";
1536 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1537
Aaron Ballman2f22b942014-05-20 19:47:14 +00001538 for (const auto *Attr : Attrs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001539 bool LateParsed = Attr->getValueAsBit("LateParsed");
Aaron Ballman35db2b32014-01-29 22:13:45 +00001540
1541 if (LateParsed) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001542 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
Aaron Ballman35db2b32014-01-29 22:13:45 +00001543
1544 // FIXME: Handle non-GNU attributes
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001545 for (const auto &I : Spellings) {
1546 if (I.variety() != "GNU")
Aaron Ballman35db2b32014-01-29 22:13:45 +00001547 continue;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001548 OS << ".Case(\"" << I.name() << "\", " << LateParsed << ")\n";
Aaron Ballman35db2b32014-01-29 22:13:45 +00001549 }
1550 }
1551 }
1552 OS << "#endif // CLANG_ATTR_LATE_PARSED_LIST\n\n";
1553}
1554
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001555static bool hasGNUorCXX11Spelling(const Record &Attribute) {
1556 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attribute);
1557 for (const auto &I : Spellings) {
1558 if (I.variety() == "GNU" || I.variety() == "CXX11")
1559 return true;
1560 }
1561 return false;
1562}
1563
1564namespace {
1565
1566struct AttributeSubjectMatchRule {
1567 const Record *MetaSubject;
1568 const Record *Constraint;
1569
1570 AttributeSubjectMatchRule(const Record *MetaSubject, const Record *Constraint)
1571 : MetaSubject(MetaSubject), Constraint(Constraint) {
1572 assert(MetaSubject && "Missing subject");
1573 }
1574
1575 bool isSubRule() const { return Constraint != nullptr; }
1576
1577 std::vector<Record *> getSubjects() const {
1578 return (Constraint ? Constraint : MetaSubject)
1579 ->getValueAsListOfDefs("Subjects");
1580 }
1581
1582 std::vector<Record *> getLangOpts() const {
1583 if (Constraint) {
1584 // Lookup the options in the sub-rule first, in case the sub-rule
1585 // overrides the rules options.
1586 std::vector<Record *> Opts = Constraint->getValueAsListOfDefs("LangOpts");
1587 if (!Opts.empty())
1588 return Opts;
1589 }
1590 return MetaSubject->getValueAsListOfDefs("LangOpts");
1591 }
1592
1593 // Abstract rules are used only for sub-rules
1594 bool isAbstractRule() const { return getSubjects().empty(); }
1595
Erich Keane3bff4142017-10-16 23:25:24 +00001596 StringRef getName() const {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001597 return (Constraint ? Constraint : MetaSubject)->getValueAsString("Name");
1598 }
1599
1600 bool isNegatedSubRule() const {
1601 assert(isSubRule() && "Not a sub-rule");
1602 return Constraint->getValueAsBit("Negated");
1603 }
1604
1605 std::string getSpelling() const {
1606 std::string Result = MetaSubject->getValueAsString("Name");
1607 if (isSubRule()) {
1608 Result += '(';
1609 if (isNegatedSubRule())
1610 Result += "unless(";
1611 Result += getName();
1612 if (isNegatedSubRule())
1613 Result += ')';
1614 Result += ')';
1615 }
1616 return Result;
1617 }
1618
1619 std::string getEnumValueName() const {
Craig Topper00648582017-05-31 19:01:22 +00001620 SmallString<128> Result;
1621 Result += "SubjectMatchRule_";
1622 Result += MetaSubject->getValueAsString("Name");
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001623 if (isSubRule()) {
1624 Result += "_";
1625 if (isNegatedSubRule())
1626 Result += "not_";
1627 Result += Constraint->getValueAsString("Name");
1628 }
1629 if (isAbstractRule())
1630 Result += "_abstract";
Craig Topper00648582017-05-31 19:01:22 +00001631 return Result.str();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001632 }
1633
1634 std::string getEnumValue() const { return "attr::" + getEnumValueName(); }
1635
1636 static const char *EnumName;
1637};
1638
1639const char *AttributeSubjectMatchRule::EnumName = "attr::SubjectMatchRule";
1640
1641struct PragmaClangAttributeSupport {
1642 std::vector<AttributeSubjectMatchRule> Rules;
Alex Lorenz24952fb2017-04-19 15:52:11 +00001643
1644 class RuleOrAggregateRuleSet {
1645 std::vector<AttributeSubjectMatchRule> Rules;
1646 bool IsRule;
1647 RuleOrAggregateRuleSet(ArrayRef<AttributeSubjectMatchRule> Rules,
1648 bool IsRule)
1649 : Rules(Rules), IsRule(IsRule) {}
1650
1651 public:
1652 bool isRule() const { return IsRule; }
1653
1654 const AttributeSubjectMatchRule &getRule() const {
1655 assert(IsRule && "not a rule!");
1656 return Rules[0];
1657 }
1658
1659 ArrayRef<AttributeSubjectMatchRule> getAggregateRuleSet() const {
1660 return Rules;
1661 }
1662
1663 static RuleOrAggregateRuleSet
1664 getRule(const AttributeSubjectMatchRule &Rule) {
1665 return RuleOrAggregateRuleSet(Rule, /*IsRule=*/true);
1666 }
1667 static RuleOrAggregateRuleSet
1668 getAggregateRuleSet(ArrayRef<AttributeSubjectMatchRule> Rules) {
1669 return RuleOrAggregateRuleSet(Rules, /*IsRule=*/false);
1670 }
1671 };
1672 llvm::DenseMap<const Record *, RuleOrAggregateRuleSet> SubjectsToRules;
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001673
1674 PragmaClangAttributeSupport(RecordKeeper &Records);
1675
1676 bool isAttributedSupported(const Record &Attribute);
1677
1678 void emitMatchRuleList(raw_ostream &OS);
1679
1680 std::string generateStrictConformsTo(const Record &Attr, raw_ostream &OS);
1681
1682 void generateParsingHelpers(raw_ostream &OS);
1683};
1684
1685} // end anonymous namespace
1686
Alex Lorenz24952fb2017-04-19 15:52:11 +00001687static bool doesDeclDeriveFrom(const Record *D, const Record *Base) {
1688 const Record *CurrentBase = D->getValueAsDef("Base");
1689 if (!CurrentBase)
1690 return false;
1691 if (CurrentBase == Base)
1692 return true;
1693 return doesDeclDeriveFrom(CurrentBase, Base);
1694}
1695
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001696PragmaClangAttributeSupport::PragmaClangAttributeSupport(
1697 RecordKeeper &Records) {
1698 std::vector<Record *> MetaSubjects =
1699 Records.getAllDerivedDefinitions("AttrSubjectMatcherRule");
1700 auto MapFromSubjectsToRules = [this](const Record *SubjectContainer,
1701 const Record *MetaSubject,
Saleem Abdulrasoolbe4773c2017-05-01 00:26:59 +00001702 const Record *Constraint) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001703 Rules.emplace_back(MetaSubject, Constraint);
1704 std::vector<Record *> ApplicableSubjects =
1705 SubjectContainer->getValueAsListOfDefs("Subjects");
1706 for (const auto *Subject : ApplicableSubjects) {
1707 bool Inserted =
Alex Lorenz24952fb2017-04-19 15:52:11 +00001708 SubjectsToRules
1709 .try_emplace(Subject, RuleOrAggregateRuleSet::getRule(
1710 AttributeSubjectMatchRule(MetaSubject,
1711 Constraint)))
1712 .second;
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001713 if (!Inserted) {
1714 PrintFatalError("Attribute subject match rules should not represent"
1715 "same attribute subjects.");
1716 }
1717 }
1718 };
1719 for (const auto *MetaSubject : MetaSubjects) {
Saleem Abdulrasoolbe4773c2017-05-01 00:26:59 +00001720 MapFromSubjectsToRules(MetaSubject, MetaSubject, /*Constraints=*/nullptr);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001721 std::vector<Record *> Constraints =
1722 MetaSubject->getValueAsListOfDefs("Constraints");
1723 for (const auto *Constraint : Constraints)
1724 MapFromSubjectsToRules(Constraint, MetaSubject, Constraint);
1725 }
Alex Lorenz24952fb2017-04-19 15:52:11 +00001726
1727 std::vector<Record *> Aggregates =
1728 Records.getAllDerivedDefinitions("AttrSubjectMatcherAggregateRule");
1729 std::vector<Record *> DeclNodes = Records.getAllDerivedDefinitions("DDecl");
1730 for (const auto *Aggregate : Aggregates) {
1731 Record *SubjectDecl = Aggregate->getValueAsDef("Subject");
1732
1733 // Gather sub-classes of the aggregate subject that act as attribute
1734 // subject rules.
1735 std::vector<AttributeSubjectMatchRule> Rules;
1736 for (const auto *D : DeclNodes) {
1737 if (doesDeclDeriveFrom(D, SubjectDecl)) {
1738 auto It = SubjectsToRules.find(D);
1739 if (It == SubjectsToRules.end())
1740 continue;
1741 if (!It->second.isRule() || It->second.getRule().isSubRule())
1742 continue; // Assume that the rule will be included as well.
1743 Rules.push_back(It->second.getRule());
1744 }
1745 }
1746
1747 bool Inserted =
1748 SubjectsToRules
1749 .try_emplace(SubjectDecl,
1750 RuleOrAggregateRuleSet::getAggregateRuleSet(Rules))
1751 .second;
1752 if (!Inserted) {
1753 PrintFatalError("Attribute subject match rules should not represent"
1754 "same attribute subjects.");
1755 }
1756 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001757}
1758
1759static PragmaClangAttributeSupport &
1760getPragmaAttributeSupport(RecordKeeper &Records) {
1761 static PragmaClangAttributeSupport Instance(Records);
1762 return Instance;
1763}
1764
1765void PragmaClangAttributeSupport::emitMatchRuleList(raw_ostream &OS) {
1766 OS << "#ifndef ATTR_MATCH_SUB_RULE\n";
1767 OS << "#define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, "
1768 "IsNegated) "
1769 << "ATTR_MATCH_RULE(Value, Spelling, IsAbstract)\n";
1770 OS << "#endif\n";
1771 for (const auto &Rule : Rules) {
1772 OS << (Rule.isSubRule() ? "ATTR_MATCH_SUB_RULE" : "ATTR_MATCH_RULE") << '(';
1773 OS << Rule.getEnumValueName() << ", \"" << Rule.getSpelling() << "\", "
1774 << Rule.isAbstractRule();
1775 if (Rule.isSubRule())
1776 OS << ", "
1777 << AttributeSubjectMatchRule(Rule.MetaSubject, nullptr).getEnumValue()
1778 << ", " << Rule.isNegatedSubRule();
1779 OS << ")\n";
1780 }
1781 OS << "#undef ATTR_MATCH_SUB_RULE\n";
1782}
1783
1784bool PragmaClangAttributeSupport::isAttributedSupported(
1785 const Record &Attribute) {
1786 if (Attribute.getValueAsBit("ForcePragmaAttributeSupport"))
1787 return true;
1788 // Opt-out rules:
1789 // FIXME: The documentation check should be moved before
1790 // the ForcePragmaAttributeSupport check after annotate is documented.
1791 // No documentation present.
1792 if (Attribute.isValueUnset("Documentation"))
1793 return false;
1794 std::vector<Record *> Docs = Attribute.getValueAsListOfDefs("Documentation");
1795 if (Docs.empty())
1796 return false;
1797 if (Docs.size() == 1 && Docs[0]->getName() == "Undocumented")
1798 return false;
1799 // An attribute requires delayed parsing (LateParsed is on)
1800 if (Attribute.getValueAsBit("LateParsed"))
1801 return false;
1802 // An attribute has no GNU/CXX11 spelling
1803 if (!hasGNUorCXX11Spelling(Attribute))
1804 return false;
1805 // An attribute subject list has a subject that isn't covered by one of the
1806 // subject match rules or has no subjects at all.
1807 if (Attribute.isValueUnset("Subjects"))
1808 return false;
1809 const Record *SubjectObj = Attribute.getValueAsDef("Subjects");
1810 std::vector<Record *> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
1811 if (Subjects.empty())
1812 return false;
1813 for (const auto *Subject : Subjects) {
1814 if (SubjectsToRules.find(Subject) == SubjectsToRules.end())
1815 return false;
1816 }
1817 return true;
1818}
1819
1820std::string
1821PragmaClangAttributeSupport::generateStrictConformsTo(const Record &Attr,
1822 raw_ostream &OS) {
1823 if (!isAttributedSupported(Attr))
1824 return "nullptr";
1825 // Generate a function that constructs a set of matching rules that describe
1826 // to which declarations the attribute should apply to.
1827 std::string FnName = "matchRulesFor" + Attr.getName().str();
Erich Keane3bff4142017-10-16 23:25:24 +00001828 OS << "static void " << FnName << "(llvm::SmallVectorImpl<std::pair<"
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001829 << AttributeSubjectMatchRule::EnumName
1830 << ", bool>> &MatchRules, const LangOptions &LangOpts) {\n";
1831 if (Attr.isValueUnset("Subjects")) {
Erich Keane3bff4142017-10-16 23:25:24 +00001832 OS << "}\n\n";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001833 return FnName;
1834 }
1835 const Record *SubjectObj = Attr.getValueAsDef("Subjects");
1836 std::vector<Record *> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
1837 for (const auto *Subject : Subjects) {
1838 auto It = SubjectsToRules.find(Subject);
1839 assert(It != SubjectsToRules.end() &&
1840 "This attribute is unsupported by #pragma clang attribute");
Alex Lorenz24952fb2017-04-19 15:52:11 +00001841 for (const auto &Rule : It->getSecond().getAggregateRuleSet()) {
1842 // The rule might be language specific, so only subtract it from the given
1843 // rules if the specific language options are specified.
1844 std::vector<Record *> LangOpts = Rule.getLangOpts();
Erich Keane3bff4142017-10-16 23:25:24 +00001845 OS << " MatchRules.push_back(std::make_pair(" << Rule.getEnumValue()
Alex Lorenz24952fb2017-04-19 15:52:11 +00001846 << ", /*IsSupported=*/";
1847 if (!LangOpts.empty()) {
1848 for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) {
Erich Keane3bff4142017-10-16 23:25:24 +00001849 const StringRef Part = (*I)->getValueAsString("Name");
Alex Lorenz24952fb2017-04-19 15:52:11 +00001850 if ((*I)->getValueAsBit("Negated"))
Erich Keane3bff4142017-10-16 23:25:24 +00001851 OS << "!";
1852 OS << "LangOpts." << Part;
Alex Lorenz24952fb2017-04-19 15:52:11 +00001853 if (I + 1 != E)
Erich Keane3bff4142017-10-16 23:25:24 +00001854 OS << " || ";
Alex Lorenz24952fb2017-04-19 15:52:11 +00001855 }
1856 } else
Erich Keane3bff4142017-10-16 23:25:24 +00001857 OS << "true";
1858 OS << "));\n";
Alex Lorenz24952fb2017-04-19 15:52:11 +00001859 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001860 }
Erich Keane3bff4142017-10-16 23:25:24 +00001861 OS << "}\n\n";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001862 return FnName;
1863}
1864
1865void PragmaClangAttributeSupport::generateParsingHelpers(raw_ostream &OS) {
1866 // Generate routines that check the names of sub-rules.
1867 OS << "Optional<attr::SubjectMatchRule> "
1868 "defaultIsAttributeSubjectMatchSubRuleFor(StringRef, bool) {\n";
1869 OS << " return None;\n";
1870 OS << "}\n\n";
1871
1872 std::map<const Record *, std::vector<AttributeSubjectMatchRule>>
1873 SubMatchRules;
1874 for (const auto &Rule : Rules) {
1875 if (!Rule.isSubRule())
1876 continue;
1877 SubMatchRules[Rule.MetaSubject].push_back(Rule);
1878 }
1879
1880 for (const auto &SubMatchRule : SubMatchRules) {
1881 OS << "Optional<attr::SubjectMatchRule> isAttributeSubjectMatchSubRuleFor_"
1882 << SubMatchRule.first->getValueAsString("Name")
1883 << "(StringRef Name, bool IsUnless) {\n";
1884 OS << " if (IsUnless)\n";
1885 OS << " return "
1886 "llvm::StringSwitch<Optional<attr::SubjectMatchRule>>(Name).\n";
1887 for (const auto &Rule : SubMatchRule.second) {
1888 if (Rule.isNegatedSubRule())
1889 OS << " Case(\"" << Rule.getName() << "\", " << Rule.getEnumValue()
1890 << ").\n";
1891 }
1892 OS << " Default(None);\n";
1893 OS << " return "
1894 "llvm::StringSwitch<Optional<attr::SubjectMatchRule>>(Name).\n";
1895 for (const auto &Rule : SubMatchRule.second) {
1896 if (!Rule.isNegatedSubRule())
1897 OS << " Case(\"" << Rule.getName() << "\", " << Rule.getEnumValue()
1898 << ").\n";
1899 }
1900 OS << " Default(None);\n";
1901 OS << "}\n\n";
1902 }
1903
1904 // Generate the function that checks for the top-level rules.
1905 OS << "std::pair<Optional<attr::SubjectMatchRule>, "
1906 "Optional<attr::SubjectMatchRule> (*)(StringRef, "
1907 "bool)> isAttributeSubjectMatchRule(StringRef Name) {\n";
1908 OS << " return "
1909 "llvm::StringSwitch<std::pair<Optional<attr::SubjectMatchRule>, "
1910 "Optional<attr::SubjectMatchRule> (*) (StringRef, "
1911 "bool)>>(Name).\n";
1912 for (const auto &Rule : Rules) {
1913 if (Rule.isSubRule())
1914 continue;
1915 std::string SubRuleFunction;
1916 if (SubMatchRules.count(Rule.MetaSubject))
Erich Keane3bff4142017-10-16 23:25:24 +00001917 SubRuleFunction =
1918 ("isAttributeSubjectMatchSubRuleFor_" + Rule.getName()).str();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001919 else
1920 SubRuleFunction = "defaultIsAttributeSubjectMatchSubRuleFor";
1921 OS << " Case(\"" << Rule.getName() << "\", std::make_pair("
1922 << Rule.getEnumValue() << ", " << SubRuleFunction << ")).\n";
1923 }
1924 OS << " Default(std::make_pair(None, "
1925 "defaultIsAttributeSubjectMatchSubRuleFor));\n";
1926 OS << "}\n\n";
1927
1928 // Generate the function that checks for the submatch rules.
1929 OS << "const char *validAttributeSubjectMatchSubRules("
1930 << AttributeSubjectMatchRule::EnumName << " Rule) {\n";
1931 OS << " switch (Rule) {\n";
1932 for (const auto &SubMatchRule : SubMatchRules) {
1933 OS << " case "
1934 << AttributeSubjectMatchRule(SubMatchRule.first, nullptr).getEnumValue()
1935 << ":\n";
1936 OS << " return \"'";
1937 bool IsFirst = true;
1938 for (const auto &Rule : SubMatchRule.second) {
1939 if (!IsFirst)
1940 OS << ", '";
1941 IsFirst = false;
1942 if (Rule.isNegatedSubRule())
1943 OS << "unless(";
1944 OS << Rule.getName();
1945 if (Rule.isNegatedSubRule())
1946 OS << ')';
1947 OS << "'";
1948 }
1949 OS << "\";\n";
1950 }
1951 OS << " default: return nullptr;\n";
1952 OS << " }\n";
1953 OS << "}\n\n";
1954}
1955
George Burgess IV1881a572016-12-01 00:13:18 +00001956template <typename Fn>
1957static void forEachUniqueSpelling(const Record &Attr, Fn &&F) {
1958 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
1959 SmallDenseSet<StringRef, 8> Seen;
1960 for (const FlattenedSpelling &S : Spellings) {
1961 if (Seen.insert(S.name()).second)
1962 F(S);
1963 }
1964}
1965
Aaron Ballman35db2b32014-01-29 22:13:45 +00001966/// \brief Emits the first-argument-is-type property for attributes.
1967static void emitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) {
1968 OS << "#if defined(CLANG_ATTR_TYPE_ARG_LIST)\n";
1969 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
1970
Aaron Ballman2f22b942014-05-20 19:47:14 +00001971 for (const auto *Attr : Attrs) {
Aaron Ballman35db2b32014-01-29 22:13:45 +00001972 // Determine whether the first argument is a type.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001973 std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
Aaron Ballman35db2b32014-01-29 22:13:45 +00001974 if (Args.empty())
1975 continue;
1976
Craig Topper25761242016-01-18 19:52:54 +00001977 if (Args[0]->getSuperClasses().back().first->getName() != "TypeArgument")
Aaron Ballman35db2b32014-01-29 22:13:45 +00001978 continue;
1979
1980 // All these spellings take a single type argument.
George Burgess IV1881a572016-12-01 00:13:18 +00001981 forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) {
1982 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
1983 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00001984 }
1985 OS << "#endif // CLANG_ATTR_TYPE_ARG_LIST\n\n";
1986}
1987
1988/// \brief Emits the parse-arguments-in-unevaluated-context property for
1989/// attributes.
1990static void emitClangAttrArgContextList(RecordKeeper &Records, raw_ostream &OS) {
1991 OS << "#if defined(CLANG_ATTR_ARG_CONTEXT_LIST)\n";
1992 ParsedAttrMap Attrs = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001993 for (const auto &I : Attrs) {
1994 const Record &Attr = *I.second;
Aaron Ballman35db2b32014-01-29 22:13:45 +00001995
1996 if (!Attr.getValueAsBit("ParseArgumentsAsUnevaluated"))
1997 continue;
1998
1999 // All these spellings take are parsed unevaluated.
George Burgess IV1881a572016-12-01 00:13:18 +00002000 forEachUniqueSpelling(Attr, [&](const FlattenedSpelling &S) {
2001 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
2002 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00002003 }
2004 OS << "#endif // CLANG_ATTR_ARG_CONTEXT_LIST\n\n";
2005}
2006
2007static bool isIdentifierArgument(Record *Arg) {
2008 return !Arg->getSuperClasses().empty() &&
Craig Topper25761242016-01-18 19:52:54 +00002009 llvm::StringSwitch<bool>(Arg->getSuperClasses().back().first->getName())
Aaron Ballman35db2b32014-01-29 22:13:45 +00002010 .Case("IdentifierArgument", true)
2011 .Case("EnumArgument", true)
Aaron Ballman55ef1512014-12-19 16:42:04 +00002012 .Case("VariadicEnumArgument", true)
Aaron Ballman35db2b32014-01-29 22:13:45 +00002013 .Default(false);
2014}
2015
2016// Emits the first-argument-is-identifier property for attributes.
2017static void emitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) {
2018 OS << "#if defined(CLANG_ATTR_IDENTIFIER_ARG_LIST)\n";
2019 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2020
Aaron Ballman2f22b942014-05-20 19:47:14 +00002021 for (const auto *Attr : Attrs) {
Aaron Ballman35db2b32014-01-29 22:13:45 +00002022 // Determine whether the first argument is an identifier.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002023 std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
Aaron Ballman35db2b32014-01-29 22:13:45 +00002024 if (Args.empty() || !isIdentifierArgument(Args[0]))
2025 continue;
2026
2027 // All these spellings take an identifier argument.
George Burgess IV1881a572016-12-01 00:13:18 +00002028 forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) {
2029 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
2030 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00002031 }
2032 OS << "#endif // CLANG_ATTR_IDENTIFIER_ARG_LIST\n\n";
2033}
2034
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002035namespace clang {
2036
2037// Emits the class definitions for attributes.
2038void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002039 emitSourceFileHeader("Attribute classes' definitions", OS);
2040
Peter Collingbournebee583f2011-10-06 13:03:08 +00002041 OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
2042 OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
2043
2044 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2045
Aaron Ballman2f22b942014-05-20 19:47:14 +00002046 for (const auto *Attr : Attrs) {
2047 const Record &R = *Attr;
Aaron Ballman06bd44b2014-02-17 18:23:02 +00002048
2049 // FIXME: Currently, documentation is generated as-needed due to the fact
2050 // that there is no way to allow a generated project "reach into" the docs
2051 // directory (for instance, it may be an out-of-tree build). However, we want
2052 // to ensure that every attribute has a Documentation field, and produce an
2053 // error if it has been neglected. Otherwise, the on-demand generation which
2054 // happens server-side will fail. This code is ensuring that functionality,
2055 // even though this Emitter doesn't technically need the documentation.
2056 // When attribute documentation can be generated as part of the build
2057 // itself, this code can be removed.
2058 (void)R.getValueAsListOfDefs("Documentation");
Douglas Gregorb2daf842012-05-02 15:56:52 +00002059
2060 if (!R.getValueAsBit("ASTNode"))
2061 continue;
2062
Craig Topper25761242016-01-18 19:52:54 +00002063 ArrayRef<std::pair<Record *, SMRange>> Supers = R.getSuperClasses();
Aaron Ballman0979e9e2013-07-30 01:44:15 +00002064 assert(!Supers.empty() && "Forgot to specify a superclass for the attr");
Aaron Ballman0979e9e2013-07-30 01:44:15 +00002065 std::string SuperName;
David Majnemerf7e36092016-06-23 00:15:04 +00002066 for (const auto &Super : llvm::reverse(Supers)) {
Craig Topper25761242016-01-18 19:52:54 +00002067 const Record *R = Super.first;
2068 if (R->getName() != "TargetSpecificAttr" && SuperName.empty())
2069 SuperName = R->getName();
Aaron Ballman0979e9e2013-07-30 01:44:15 +00002070 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00002071
2072 OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
2073
2074 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002075 std::vector<std::unique_ptr<Argument>> Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002076 Args.reserve(ArgRecords.size());
2077
John McCalla62c1a92015-10-28 00:17:34 +00002078 bool HasOptArg = false;
2079 bool HasFakeArg = false;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002080 for (const auto *ArgRecord : ArgRecords) {
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002081 Args.emplace_back(createArgument(*ArgRecord, R.getName()));
2082 Args.back()->writeDeclarations(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002083 OS << "\n\n";
John McCalla62c1a92015-10-28 00:17:34 +00002084
2085 // For these purposes, fake takes priority over optional.
2086 if (Args.back()->isFake()) {
2087 HasFakeArg = true;
2088 } else if (Args.back()->isOptional()) {
2089 HasOptArg = true;
2090 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00002091 }
2092
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002093 OS << "public:\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002094
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002095 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballman36a53502014-01-16 13:03:14 +00002096
2097 // If there are zero or one spellings, all spelling-related functionality
2098 // can be elided. If all of the spellings share the same name, the spelling
2099 // functionality can also be elided.
2100 bool ElideSpelling = (Spellings.size() <= 1) ||
2101 SpellingNamesAreCommon(Spellings);
2102
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002103 // This maps spelling index values to semantic Spelling enumerants.
2104 SemanticSpellingMap SemanticToSyntacticMap;
Aaron Ballman36a53502014-01-16 13:03:14 +00002105
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002106 if (!ElideSpelling)
2107 OS << CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
Aaron Ballman36a53502014-01-16 13:03:14 +00002108
John McCalla62c1a92015-10-28 00:17:34 +00002109 // Emit CreateImplicit factory methods.
2110 auto emitCreateImplicit = [&](bool emitFake) {
2111 OS << " static " << R.getName() << "Attr *CreateImplicit(";
2112 OS << "ASTContext &Ctx";
2113 if (!ElideSpelling)
2114 OS << ", Spelling S";
2115 for (auto const &ai : Args) {
2116 if (ai->isFake() && !emitFake) continue;
2117 OS << ", ";
2118 ai->writeCtorParameters(OS);
2119 }
2120 OS << ", SourceRange Loc = SourceRange()";
2121 OS << ") {\n";
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002122 OS << " auto *A = new (Ctx) " << R.getName();
John McCalla62c1a92015-10-28 00:17:34 +00002123 OS << "Attr(Loc, Ctx, ";
2124 for (auto const &ai : Args) {
2125 if (ai->isFake() && !emitFake) continue;
2126 ai->writeImplicitCtorArgs(OS);
2127 OS << ", ";
2128 }
2129 OS << (ElideSpelling ? "0" : "S") << ");\n";
2130 OS << " A->setImplicit(true);\n";
2131 OS << " return A;\n }\n\n";
2132 };
Aaron Ballman36a53502014-01-16 13:03:14 +00002133
John McCalla62c1a92015-10-28 00:17:34 +00002134 // Emit a CreateImplicit that takes all the arguments.
2135 emitCreateImplicit(true);
2136
2137 // Emit a CreateImplicit that takes all the non-fake arguments.
2138 if (HasFakeArg) {
2139 emitCreateImplicit(false);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002140 }
Michael Han99315932013-01-24 16:46:58 +00002141
John McCalla62c1a92015-10-28 00:17:34 +00002142 // Emit constructors.
2143 auto emitCtor = [&](bool emitOpt, bool emitFake) {
2144 auto shouldEmitArg = [=](const std::unique_ptr<Argument> &arg) {
2145 if (arg->isFake()) return emitFake;
2146 if (arg->isOptional()) return emitOpt;
2147 return true;
2148 };
Michael Han99315932013-01-24 16:46:58 +00002149
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002150 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002151 for (auto const &ai : Args) {
John McCalla62c1a92015-10-28 00:17:34 +00002152 if (!shouldEmitArg(ai)) continue;
2153 OS << " , ";
2154 ai->writeCtorParameters(OS);
2155 OS << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002156 }
2157
2158 OS << " , ";
Aaron Ballman36a53502014-01-16 13:03:14 +00002159 OS << "unsigned SI\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002160
2161 OS << " )\n";
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002162 OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI, "
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002163 << ( R.getValueAsBit("LateParsed") ? "true" : "false" ) << ", "
2164 << ( R.getValueAsBit("DuplicatesAllowedWhileMerging") ? "true" : "false" ) << ")\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002165
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002166 for (auto const &ai : Args) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002167 OS << " , ";
John McCalla62c1a92015-10-28 00:17:34 +00002168 if (!shouldEmitArg(ai)) {
2169 ai->writeCtorDefaultInitializers(OS);
2170 } else {
2171 ai->writeCtorInitializers(OS);
2172 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002173 OS << "\n";
2174 }
2175
2176 OS << " {\n";
2177
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002178 for (auto const &ai : Args) {
John McCalla62c1a92015-10-28 00:17:34 +00002179 if (!shouldEmitArg(ai)) continue;
2180 ai->writeCtorBody(OS);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002181 }
2182 OS << " }\n\n";
John McCalla62c1a92015-10-28 00:17:34 +00002183 };
2184
2185 // Emit a constructor that includes all the arguments.
2186 // This is necessary for cloning.
2187 emitCtor(true, true);
2188
2189 // Emit a constructor that takes all the non-fake arguments.
2190 if (HasFakeArg) {
2191 emitCtor(true, false);
2192 }
2193
2194 // Emit a constructor that takes all the non-fake, non-optional arguments.
2195 if (HasOptArg) {
2196 emitCtor(false, false);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002197 }
2198
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002199 OS << " " << R.getName() << "Attr *clone(ASTContext &C) const;\n";
Craig Toppercbce6e92014-03-11 06:22:39 +00002200 OS << " void printPretty(raw_ostream &OS,\n"
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002201 << " const PrintingPolicy &Policy) const;\n";
2202 OS << " const char *getSpelling() const;\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002203
2204 if (!ElideSpelling) {
2205 assert(!SemanticToSyntacticMap.empty() && "Empty semantic mapping list");
2206 OS << " Spelling getSemanticSpelling() const {\n";
2207 WriteSemanticSpellingSwitch("SpellingListIndex", SemanticToSyntacticMap,
2208 OS);
2209 OS << " }\n";
2210 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00002211
Michael Hanaf02bbe2013-02-01 01:19:17 +00002212 writeAttrAccessorDefinition(R, OS);
2213
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002214 for (auto const &ai : Args) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002215 ai->writeAccessors(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002216 OS << "\n\n";
Aaron Ballman682ee422013-09-11 19:47:58 +00002217
John McCalla62c1a92015-10-28 00:17:34 +00002218 // Don't write conversion routines for fake arguments.
2219 if (ai->isFake()) continue;
2220
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002221 if (ai->isEnumArg())
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002222 static_cast<const EnumArgument *>(ai.get())->writeConversion(OS);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002223 else if (ai->isVariadicEnumArg())
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002224 static_cast<const VariadicEnumArgument *>(ai.get())
2225 ->writeConversion(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002226 }
2227
Jakob Stoklund Olesen6f2288b62012-01-13 04:57:47 +00002228 OS << R.getValueAsString("AdditionalMembers");
Peter Collingbournebee583f2011-10-06 13:03:08 +00002229 OS << "\n\n";
2230
2231 OS << " static bool classof(const Attr *A) { return A->getKind() == "
2232 << "attr::" << R.getName() << "; }\n";
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00002233
Peter Collingbournebee583f2011-10-06 13:03:08 +00002234 OS << "};\n\n";
2235 }
2236
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002237 OS << "#endif // LLVM_CLANG_ATTR_CLASSES_INC\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002238}
2239
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002240// Emits the class method definitions for attributes.
2241void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002242 emitSourceFileHeader("Attribute classes' member function definitions", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002243
2244 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
Peter Collingbournebee583f2011-10-06 13:03:08 +00002245
Aaron Ballman2f22b942014-05-20 19:47:14 +00002246 for (auto *Attr : Attrs) {
2247 Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002248
2249 if (!R.getValueAsBit("ASTNode"))
2250 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002251
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002252 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
2253 std::vector<std::unique_ptr<Argument>> Args;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002254 for (const auto *Arg : ArgRecords)
2255 Args.emplace_back(createArgument(*Arg, R.getName()));
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002256
2257 for (auto const &ai : Args)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002258 ai->writeAccessorDefinitions(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002259
2260 OS << R.getName() << "Attr *" << R.getName()
2261 << "Attr::clone(ASTContext &C) const {\n";
Hans Wennborg613807b2014-05-31 01:30:30 +00002262 OS << " auto *A = new (C) " << R.getName() << "Attr(getLocation(), C";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002263 for (auto const &ai : Args) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00002264 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002265 ai->writeCloneArgs(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002266 }
Hans Wennborg613807b2014-05-31 01:30:30 +00002267 OS << ", getSpellingListIndex());\n";
2268 OS << " A->Inherited = Inherited;\n";
2269 OS << " A->IsPackExpansion = IsPackExpansion;\n";
2270 OS << " A->Implicit = Implicit;\n";
2271 OS << " return A;\n}\n\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00002272
Michael Han99315932013-01-24 16:46:58 +00002273 writePrettyPrintFunction(R, Args, OS);
Aaron Ballman3e424b52013-12-26 18:30:57 +00002274 writeGetSpellingFunction(R, OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002275 }
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002276
2277 // Instead of relying on virtual dispatch we just create a huge dispatch
2278 // switch. This is both smaller and faster than virtual functions.
2279 auto EmitFunc = [&](const char *Method) {
2280 OS << " switch (getKind()) {\n";
2281 for (const auto *Attr : Attrs) {
2282 const Record &R = *Attr;
2283 if (!R.getValueAsBit("ASTNode"))
2284 continue;
2285
2286 OS << " case attr::" << R.getName() << ":\n";
2287 OS << " return cast<" << R.getName() << "Attr>(this)->" << Method
2288 << ";\n";
2289 }
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002290 OS << " }\n";
2291 OS << " llvm_unreachable(\"Unexpected attribute kind!\");\n";
2292 OS << "}\n\n";
2293 };
2294
2295 OS << "const char *Attr::getSpelling() const {\n";
2296 EmitFunc("getSpelling()");
2297
2298 OS << "Attr *Attr::clone(ASTContext &C) const {\n";
2299 EmitFunc("clone(C)");
2300
2301 OS << "void Attr::printPretty(raw_ostream &OS, "
2302 "const PrintingPolicy &Policy) const {\n";
2303 EmitFunc("printPretty(OS, Policy)");
Peter Collingbournebee583f2011-10-06 13:03:08 +00002304}
2305
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002306} // end namespace clang
2307
John McCall2225c8b2016-03-01 00:18:05 +00002308static void emitAttrList(raw_ostream &OS, StringRef Class,
Peter Collingbournebee583f2011-10-06 13:03:08 +00002309 const std::vector<Record*> &AttrList) {
John McCall2225c8b2016-03-01 00:18:05 +00002310 for (auto Cur : AttrList) {
2311 OS << Class << "(" << Cur->getName() << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002312 }
2313}
2314
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002315// Determines if an attribute has a Pragma spelling.
2316static bool AttrHasPragmaSpelling(const Record *R) {
2317 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
George Burgess IV1881a572016-12-01 00:13:18 +00002318 return llvm::find_if(Spellings, [](const FlattenedSpelling &S) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002319 return S.variety() == "Pragma";
2320 }) != Spellings.end();
2321}
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002322
John McCall2225c8b2016-03-01 00:18:05 +00002323namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002324
John McCall2225c8b2016-03-01 00:18:05 +00002325 struct AttrClassDescriptor {
2326 const char * const MacroName;
2327 const char * const TableGenName;
2328 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002329
2330} // end anonymous namespace
John McCall2225c8b2016-03-01 00:18:05 +00002331
2332static const AttrClassDescriptor AttrClassDescriptors[] = {
2333 { "ATTR", "Attr" },
Richard Smith4f902c72016-03-08 00:32:55 +00002334 { "STMT_ATTR", "StmtAttr" },
John McCall2225c8b2016-03-01 00:18:05 +00002335 { "INHERITABLE_ATTR", "InheritableAttr" },
John McCall477f2bb2016-03-03 06:39:32 +00002336 { "INHERITABLE_PARAM_ATTR", "InheritableParamAttr" },
2337 { "PARAMETER_ABI_ATTR", "ParameterABIAttr" }
John McCall2225c8b2016-03-01 00:18:05 +00002338};
2339
2340static void emitDefaultDefine(raw_ostream &OS, StringRef name,
2341 const char *superName) {
2342 OS << "#ifndef " << name << "\n";
2343 OS << "#define " << name << "(NAME) ";
2344 if (superName) OS << superName << "(NAME)";
2345 OS << "\n#endif\n\n";
2346}
2347
2348namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002349
John McCall2225c8b2016-03-01 00:18:05 +00002350 /// A class of attributes.
2351 struct AttrClass {
2352 const AttrClassDescriptor &Descriptor;
2353 Record *TheRecord;
2354 AttrClass *SuperClass = nullptr;
2355 std::vector<AttrClass*> SubClasses;
2356 std::vector<Record*> Attrs;
2357
2358 AttrClass(const AttrClassDescriptor &Descriptor, Record *R)
2359 : Descriptor(Descriptor), TheRecord(R) {}
2360
2361 void emitDefaultDefines(raw_ostream &OS) const {
2362 // Default the macro unless this is a root class (i.e. Attr).
2363 if (SuperClass) {
2364 emitDefaultDefine(OS, Descriptor.MacroName,
2365 SuperClass->Descriptor.MacroName);
2366 }
2367 }
2368
2369 void emitUndefs(raw_ostream &OS) const {
2370 OS << "#undef " << Descriptor.MacroName << "\n";
2371 }
2372
2373 void emitAttrList(raw_ostream &OS) const {
2374 for (auto SubClass : SubClasses) {
2375 SubClass->emitAttrList(OS);
2376 }
2377
2378 ::emitAttrList(OS, Descriptor.MacroName, Attrs);
2379 }
2380
2381 void classifyAttrOnRoot(Record *Attr) {
2382 bool result = classifyAttr(Attr);
2383 assert(result && "failed to classify on root"); (void) result;
2384 }
2385
2386 void emitAttrRange(raw_ostream &OS) const {
2387 OS << "ATTR_RANGE(" << Descriptor.TableGenName
2388 << ", " << getFirstAttr()->getName()
2389 << ", " << getLastAttr()->getName() << ")\n";
2390 }
2391
2392 private:
2393 bool classifyAttr(Record *Attr) {
2394 // Check all the subclasses.
2395 for (auto SubClass : SubClasses) {
2396 if (SubClass->classifyAttr(Attr))
2397 return true;
2398 }
2399
2400 // It's not more specific than this class, but it might still belong here.
2401 if (Attr->isSubClassOf(TheRecord)) {
2402 Attrs.push_back(Attr);
2403 return true;
2404 }
2405
2406 return false;
2407 }
2408
2409 Record *getFirstAttr() const {
2410 if (!SubClasses.empty())
2411 return SubClasses.front()->getFirstAttr();
2412 return Attrs.front();
2413 }
2414
2415 Record *getLastAttr() const {
2416 if (!Attrs.empty())
2417 return Attrs.back();
2418 return SubClasses.back()->getLastAttr();
2419 }
2420 };
2421
2422 /// The entire hierarchy of attribute classes.
2423 class AttrClassHierarchy {
2424 std::vector<std::unique_ptr<AttrClass>> Classes;
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002425
John McCall2225c8b2016-03-01 00:18:05 +00002426 public:
2427 AttrClassHierarchy(RecordKeeper &Records) {
2428 // Find records for all the classes.
2429 for (auto &Descriptor : AttrClassDescriptors) {
2430 Record *ClassRecord = Records.getClass(Descriptor.TableGenName);
2431 AttrClass *Class = new AttrClass(Descriptor, ClassRecord);
2432 Classes.emplace_back(Class);
2433 }
2434
2435 // Link up the hierarchy.
2436 for (auto &Class : Classes) {
2437 if (AttrClass *SuperClass = findSuperClass(Class->TheRecord)) {
2438 Class->SuperClass = SuperClass;
2439 SuperClass->SubClasses.push_back(Class.get());
2440 }
2441 }
2442
2443#ifndef NDEBUG
2444 for (auto i = Classes.begin(), e = Classes.end(); i != e; ++i) {
2445 assert((i == Classes.begin()) == ((*i)->SuperClass == nullptr) &&
2446 "only the first class should be a root class!");
2447 }
2448#endif
2449 }
2450
2451 void emitDefaultDefines(raw_ostream &OS) const {
2452 for (auto &Class : Classes) {
2453 Class->emitDefaultDefines(OS);
2454 }
2455 }
2456
2457 void emitUndefs(raw_ostream &OS) const {
2458 for (auto &Class : Classes) {
2459 Class->emitUndefs(OS);
2460 }
2461 }
2462
2463 void emitAttrLists(raw_ostream &OS) const {
2464 // Just start from the root class.
2465 Classes[0]->emitAttrList(OS);
2466 }
2467
2468 void emitAttrRanges(raw_ostream &OS) const {
2469 for (auto &Class : Classes)
2470 Class->emitAttrRange(OS);
2471 }
2472
2473 void classifyAttr(Record *Attr) {
2474 // Add the attribute to the root class.
2475 Classes[0]->classifyAttrOnRoot(Attr);
2476 }
2477
2478 private:
2479 AttrClass *findClassByRecord(Record *R) const {
2480 for (auto &Class : Classes) {
2481 if (Class->TheRecord == R)
2482 return Class.get();
2483 }
2484 return nullptr;
2485 }
2486
2487 AttrClass *findSuperClass(Record *R) const {
2488 // TableGen flattens the superclass list, so we just need to walk it
2489 // in reverse.
2490 auto SuperClasses = R->getSuperClasses();
2491 for (signed i = 0, e = SuperClasses.size(); i != e; ++i) {
2492 auto SuperClass = findClassByRecord(SuperClasses[e - i - 1].first);
2493 if (SuperClass) return SuperClass;
2494 }
2495 return nullptr;
2496 }
2497 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002498
2499} // end anonymous namespace
John McCall2225c8b2016-03-01 00:18:05 +00002500
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002501namespace clang {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002502
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002503// Emits the enumeration list for attributes.
2504void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002505 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002506
John McCall2225c8b2016-03-01 00:18:05 +00002507 AttrClassHierarchy Hierarchy(Records);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002508
John McCall2225c8b2016-03-01 00:18:05 +00002509 // Add defaulting macro definitions.
2510 Hierarchy.emitDefaultDefines(OS);
2511 emitDefaultDefine(OS, "PRAGMA_SPELLING_ATTR", nullptr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002512
John McCall2225c8b2016-03-01 00:18:05 +00002513 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
2514 std::vector<Record *> PragmaAttrs;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002515 for (auto *Attr : Attrs) {
2516 if (!Attr->getValueAsBit("ASTNode"))
Douglas Gregorb2daf842012-05-02 15:56:52 +00002517 continue;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002518
John McCall2225c8b2016-03-01 00:18:05 +00002519 // Add the attribute to the ad-hoc groups.
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002520 if (AttrHasPragmaSpelling(Attr))
2521 PragmaAttrs.push_back(Attr);
2522
John McCall2225c8b2016-03-01 00:18:05 +00002523 // Place it in the hierarchy.
2524 Hierarchy.classifyAttr(Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002525 }
2526
John McCall2225c8b2016-03-01 00:18:05 +00002527 // Emit the main attribute list.
2528 Hierarchy.emitAttrLists(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002529
John McCall2225c8b2016-03-01 00:18:05 +00002530 // Emit the ad hoc groups.
2531 emitAttrList(OS, "PRAGMA_SPELLING_ATTR", PragmaAttrs);
2532
2533 // Emit the attribute ranges.
2534 OS << "#ifdef ATTR_RANGE\n";
2535 Hierarchy.emitAttrRanges(OS);
2536 OS << "#undef ATTR_RANGE\n";
2537 OS << "#endif\n";
2538
2539 Hierarchy.emitUndefs(OS);
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002540 OS << "#undef PRAGMA_SPELLING_ATTR\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002541}
2542
Alex Lorenz9e7bf162017-04-18 14:33:39 +00002543// Emits the enumeration list for attributes.
2544void EmitClangAttrSubjectMatchRuleList(RecordKeeper &Records, raw_ostream &OS) {
2545 emitSourceFileHeader(
2546 "List of all attribute subject matching rules that Clang recognizes", OS);
2547 PragmaClangAttributeSupport &PragmaAttributeSupport =
2548 getPragmaAttributeSupport(Records);
2549 emitDefaultDefine(OS, "ATTR_MATCH_RULE", nullptr);
2550 PragmaAttributeSupport.emitMatchRuleList(OS);
2551 OS << "#undef ATTR_MATCH_RULE\n";
2552}
2553
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002554// Emits the code to read an attribute from a precompiled header.
2555void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002556 emitSourceFileHeader("Attribute deserialization code", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002557
2558 Record *InhClass = Records.getClass("InheritableAttr");
2559 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
2560 ArgRecords;
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002561 std::vector<std::unique_ptr<Argument>> Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002562
2563 OS << " switch (Kind) {\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002564 for (const auto *Attr : Attrs) {
2565 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002566 if (!R.getValueAsBit("ASTNode"))
2567 continue;
2568
Peter Collingbournebee583f2011-10-06 13:03:08 +00002569 OS << " case attr::" << R.getName() << ": {\n";
2570 if (R.isSubClassOf(InhClass))
David L. Jones267b8842017-01-24 01:04:30 +00002571 OS << " bool isInherited = Record.readInt();\n";
2572 OS << " bool isImplicit = Record.readInt();\n";
2573 OS << " unsigned Spelling = Record.readInt();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002574 ArgRecords = R.getValueAsListOfDefs("Args");
2575 Args.clear();
Aaron Ballman2f22b942014-05-20 19:47:14 +00002576 for (const auto *Arg : ArgRecords) {
2577 Args.emplace_back(createArgument(*Arg, R.getName()));
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002578 Args.back()->writePCHReadDecls(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002579 }
2580 OS << " New = new (Context) " << R.getName() << "Attr(Range, Context";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002581 for (auto const &ri : Args) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00002582 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002583 ri->writePCHReadArgs(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002584 }
Aaron Ballman36a53502014-01-16 13:03:14 +00002585 OS << ", Spelling);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002586 if (R.isSubClassOf(InhClass))
2587 OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002588 OS << " New->setImplicit(isImplicit);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002589 OS << " break;\n";
2590 OS << " }\n";
2591 }
2592 OS << " }\n";
2593}
2594
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002595// Emits the code to write an attribute to a precompiled header.
2596void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002597 emitSourceFileHeader("Attribute serialization code", OS);
2598
Peter Collingbournebee583f2011-10-06 13:03:08 +00002599 Record *InhClass = Records.getClass("InheritableAttr");
2600 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002601
2602 OS << " switch (A->getKind()) {\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002603 for (const auto *Attr : Attrs) {
2604 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002605 if (!R.getValueAsBit("ASTNode"))
2606 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002607 OS << " case attr::" << R.getName() << ": {\n";
2608 Args = R.getValueAsListOfDefs("Args");
2609 if (R.isSubClassOf(InhClass) || !Args.empty())
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002610 OS << " const auto *SA = cast<" << R.getName()
Peter Collingbournebee583f2011-10-06 13:03:08 +00002611 << "Attr>(A);\n";
2612 if (R.isSubClassOf(InhClass))
2613 OS << " Record.push_back(SA->isInherited());\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002614 OS << " Record.push_back(A->isImplicit());\n";
2615 OS << " Record.push_back(A->getSpellingListIndex());\n";
2616
Aaron Ballman2f22b942014-05-20 19:47:14 +00002617 for (const auto *Arg : Args)
2618 createArgument(*Arg, R.getName())->writePCHWrite(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002619 OS << " break;\n";
2620 OS << " }\n";
2621 }
2622 OS << " }\n";
2623}
2624
Bob Wilson0058b822015-07-20 22:57:36 +00002625// Generate a conditional expression to check if the current target satisfies
2626// the conditions for a TargetSpecificAttr record, and append the code for
2627// those checks to the Test string. If the FnName string pointer is non-null,
2628// append a unique suffix to distinguish this set of target checks from other
2629// TargetSpecificAttr records.
2630static void GenerateTargetSpecificAttrChecks(const Record *R,
Craig Topper00648582017-05-31 19:01:22 +00002631 std::vector<StringRef> &Arches,
Bob Wilson0058b822015-07-20 22:57:36 +00002632 std::string &Test,
2633 std::string *FnName) {
2634 // It is assumed that there will be an llvm::Triple object
2635 // named "T" and a TargetInfo object named "Target" within
2636 // scope that can be used to determine whether the attribute exists in
2637 // a given target.
2638 Test += "(";
2639
2640 for (auto I = Arches.begin(), E = Arches.end(); I != E; ++I) {
Craig Topper00648582017-05-31 19:01:22 +00002641 StringRef Part = *I;
2642 Test += "T.getArch() == llvm::Triple::";
2643 Test += Part;
Bob Wilson0058b822015-07-20 22:57:36 +00002644 if (I + 1 != E)
2645 Test += " || ";
2646 if (FnName)
2647 *FnName += Part;
2648 }
2649 Test += ")";
2650
2651 // If the attribute is specific to particular OSes, check those.
2652 if (!R->isValueUnset("OSes")) {
2653 // We know that there was at least one arch test, so we need to and in the
2654 // OS tests.
2655 Test += " && (";
Craig Topper00648582017-05-31 19:01:22 +00002656 std::vector<StringRef> OSes = R->getValueAsListOfStrings("OSes");
Bob Wilson0058b822015-07-20 22:57:36 +00002657 for (auto I = OSes.begin(), E = OSes.end(); I != E; ++I) {
Craig Topper00648582017-05-31 19:01:22 +00002658 StringRef Part = *I;
Bob Wilson0058b822015-07-20 22:57:36 +00002659
Craig Topper00648582017-05-31 19:01:22 +00002660 Test += "T.getOS() == llvm::Triple::";
2661 Test += Part;
Bob Wilson0058b822015-07-20 22:57:36 +00002662 if (I + 1 != E)
2663 Test += " || ";
2664 if (FnName)
2665 *FnName += Part;
2666 }
2667 Test += ")";
2668 }
2669
2670 // If one or more CXX ABIs are specified, check those as well.
2671 if (!R->isValueUnset("CXXABIs")) {
2672 Test += " && (";
Craig Topper00648582017-05-31 19:01:22 +00002673 std::vector<StringRef> CXXABIs = R->getValueAsListOfStrings("CXXABIs");
Bob Wilson0058b822015-07-20 22:57:36 +00002674 for (auto I = CXXABIs.begin(), E = CXXABIs.end(); I != E; ++I) {
Craig Topper00648582017-05-31 19:01:22 +00002675 StringRef Part = *I;
2676 Test += "Target.getCXXABI().getKind() == TargetCXXABI::";
2677 Test += Part;
Bob Wilson0058b822015-07-20 22:57:36 +00002678 if (I + 1 != E)
2679 Test += " || ";
2680 if (FnName)
2681 *FnName += Part;
2682 }
2683 Test += ")";
2684 }
2685}
2686
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002687static void GenerateHasAttrSpellingStringSwitch(
2688 const std::vector<Record *> &Attrs, raw_ostream &OS,
2689 const std::string &Variety = "", const std::string &Scope = "") {
2690 for (const auto *Attr : Attrs) {
Aaron Ballmana0344c52014-11-14 13:44:02 +00002691 // C++11-style attributes have specific version information associated with
2692 // them. If the attribute has no scope, the version information must not
2693 // have the default value (1), as that's incorrect. Instead, the unscoped
2694 // attribute version information should be taken from the SD-6 standing
2695 // document, which can be found at:
2696 // https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations
2697 int Version = 1;
2698
2699 if (Variety == "CXX11") {
2700 std::vector<Record *> Spellings = Attr->getValueAsListOfDefs("Spellings");
2701 for (const auto &Spelling : Spellings) {
2702 if (Spelling->getValueAsString("Variety") == "CXX11") {
2703 Version = static_cast<int>(Spelling->getValueAsInt("Version"));
2704 if (Scope.empty() && Version == 1)
2705 PrintError(Spelling->getLoc(), "C++ standard attributes must "
2706 "have valid version information.");
2707 break;
2708 }
2709 }
2710 }
2711
Aaron Ballman0fa06d82014-01-09 22:57:44 +00002712 std::string Test;
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002713 if (Attr->isSubClassOf("TargetSpecificAttr")) {
2714 const Record *R = Attr->getValueAsDef("Target");
Craig Topper00648582017-05-31 19:01:22 +00002715 std::vector<StringRef> Arches = R->getValueAsListOfStrings("Arches");
Hans Wennborgdcfba332015-10-06 23:40:43 +00002716 GenerateTargetSpecificAttrChecks(R, Arches, Test, nullptr);
Bob Wilson7c730832015-07-20 22:57:31 +00002717
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002718 // If this is the C++11 variety, also add in the LangOpts test.
2719 if (Variety == "CXX11")
2720 Test += " && LangOpts.CPlusPlus11";
Aaron Ballman606093a2017-10-15 15:01:42 +00002721 else if (Variety == "C2x")
2722 Test += " && LangOpts.DoubleSquareBracketAttributes";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002723 } else if (Variety == "CXX11")
2724 // C++11 mode should be checked against LangOpts, which is presumed to be
2725 // present in the caller.
2726 Test = "LangOpts.CPlusPlus11";
Aaron Ballman606093a2017-10-15 15:01:42 +00002727 else if (Variety == "C2x")
2728 Test = "LangOpts.DoubleSquareBracketAttributes";
Aaron Ballman0fa06d82014-01-09 22:57:44 +00002729
Aaron Ballmana0344c52014-11-14 13:44:02 +00002730 std::string TestStr =
Aaron Ballman28afa182014-11-17 18:17:19 +00002731 !Test.empty() ? Test + " ? " + llvm::itostr(Version) + " : 0" : "1";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002732 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002733 for (const auto &S : Spellings)
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002734 if (Variety.empty() || (Variety == S.variety() &&
2735 (Scope.empty() || Scope == S.nameSpace())))
Aaron Ballmana0344c52014-11-14 13:44:02 +00002736 OS << " .Case(\"" << S.name() << "\", " << TestStr << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002737 }
Aaron Ballmana0344c52014-11-14 13:44:02 +00002738 OS << " .Default(0);\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002739}
2740
2741// Emits the list of spellings for attributes.
2742void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
2743 emitSourceFileHeader("Code to implement the __has_attribute logic", OS);
2744
2745 // Separate all of the attributes out into four group: generic, C++11, GNU,
2746 // and declspecs. Then generate a big switch statement for each of them.
2747 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Nico Weber20e08042016-09-03 02:55:10 +00002748 std::vector<Record *> Declspec, Microsoft, GNU, Pragma;
Aaron Ballman606093a2017-10-15 15:01:42 +00002749 std::map<std::string, std::vector<Record *>> CXX, C2x;
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002750
2751 // Walk over the list of all attributes, and split them out based on the
2752 // spelling variety.
2753 for (auto *R : Attrs) {
2754 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
2755 for (const auto &SI : Spellings) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00002756 const std::string &Variety = SI.variety();
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002757 if (Variety == "GNU")
2758 GNU.push_back(R);
2759 else if (Variety == "Declspec")
2760 Declspec.push_back(R);
Nico Weber20e08042016-09-03 02:55:10 +00002761 else if (Variety == "Microsoft")
2762 Microsoft.push_back(R);
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002763 else if (Variety == "CXX11")
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002764 CXX[SI.nameSpace()].push_back(R);
Aaron Ballman606093a2017-10-15 15:01:42 +00002765 else if (Variety == "C2x")
2766 C2x[SI.nameSpace()].push_back(R);
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002767 else if (Variety == "Pragma")
2768 Pragma.push_back(R);
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002769 }
2770 }
2771
Bob Wilson7c730832015-07-20 22:57:31 +00002772 OS << "const llvm::Triple &T = Target.getTriple();\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002773 OS << "switch (Syntax) {\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002774 OS << "case AttrSyntax::GNU:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002775 OS << " return llvm::StringSwitch<int>(Name)\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002776 GenerateHasAttrSpellingStringSwitch(GNU, OS, "GNU");
2777 OS << "case AttrSyntax::Declspec:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002778 OS << " return llvm::StringSwitch<int>(Name)\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002779 GenerateHasAttrSpellingStringSwitch(Declspec, OS, "Declspec");
Nico Weber20e08042016-09-03 02:55:10 +00002780 OS << "case AttrSyntax::Microsoft:\n";
2781 OS << " return llvm::StringSwitch<int>(Name)\n";
2782 GenerateHasAttrSpellingStringSwitch(Microsoft, OS, "Microsoft");
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002783 OS << "case AttrSyntax::Pragma:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002784 OS << " return llvm::StringSwitch<int>(Name)\n";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002785 GenerateHasAttrSpellingStringSwitch(Pragma, OS, "Pragma");
Aaron Ballman606093a2017-10-15 15:01:42 +00002786 auto fn = [&OS](const char *Spelling, const char *Variety,
2787 const std::map<std::string, std::vector<Record *>> &List) {
2788 OS << "case AttrSyntax::" << Variety << ": {\n";
2789 // C++11-style attributes are further split out based on the Scope.
2790 for (auto I = List.cbegin(), E = List.cend(); I != E; ++I) {
2791 if (I != List.cbegin())
2792 OS << " else ";
2793 if (I->first.empty())
2794 OS << "if (!Scope || Scope->getName() == \"\") {\n";
2795 else
2796 OS << "if (Scope->getName() == \"" << I->first << "\") {\n";
2797 OS << " return llvm::StringSwitch<int>(Name)\n";
2798 GenerateHasAttrSpellingStringSwitch(I->second, OS, Spelling, I->first);
2799 OS << "}";
2800 }
Aaron Ballman4ff3b5ab2017-10-18 12:11:58 +00002801 OS << "\n} break;\n";
Aaron Ballman606093a2017-10-15 15:01:42 +00002802 };
2803 fn("CXX11", "CXX", CXX);
2804 fn("C2x", "C", C2x);
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002805 OS << "}\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002806}
2807
Michael Han99315932013-01-24 16:46:58 +00002808void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002809 emitSourceFileHeader("Code to translate different attribute spellings "
2810 "into internal identifiers", OS);
Michael Han99315932013-01-24 16:46:58 +00002811
John McCall2225c8b2016-03-01 00:18:05 +00002812 OS << " switch (AttrKind) {\n";
Michael Han99315932013-01-24 16:46:58 +00002813
Aaron Ballman64e69862013-12-15 13:05:48 +00002814 ParsedAttrMap Attrs = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002815 for (const auto &I : Attrs) {
Aaron Ballman2f22b942014-05-20 19:47:14 +00002816 const Record &R = *I.second;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002817 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002818 OS << " case AT_" << I.first << ": {\n";
Richard Smith852e9ce2013-11-27 01:46:48 +00002819 for (unsigned I = 0; I < Spellings.size(); ++ I) {
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002820 OS << " if (Name == \"" << Spellings[I].name() << "\" && "
2821 << "SyntaxUsed == "
2822 << StringSwitch<unsigned>(Spellings[I].variety())
2823 .Case("GNU", 0)
2824 .Case("CXX11", 1)
Aaron Ballman606093a2017-10-15 15:01:42 +00002825 .Case("C2x", 2)
2826 .Case("Declspec", 3)
2827 .Case("Microsoft", 4)
2828 .Case("Keyword", 5)
2829 .Case("Pragma", 6)
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002830 .Default(0)
2831 << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n"
2832 << " return " << I << ";\n";
Michael Han99315932013-01-24 16:46:58 +00002833 }
Richard Smith852e9ce2013-11-27 01:46:48 +00002834
2835 OS << " break;\n";
2836 OS << " }\n";
Michael Han99315932013-01-24 16:46:58 +00002837 }
2838
2839 OS << " }\n";
Aaron Ballman64e69862013-12-15 13:05:48 +00002840 OS << " return 0;\n";
Michael Han99315932013-01-24 16:46:58 +00002841}
2842
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002843// Emits code used by RecursiveASTVisitor to visit attributes
2844void EmitClangAttrASTVisitor(RecordKeeper &Records, raw_ostream &OS) {
2845 emitSourceFileHeader("Used by RecursiveASTVisitor to visit attributes.", OS);
2846
2847 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2848
2849 // Write method declarations for Traverse* methods.
2850 // We emit this here because we only generate methods for attributes that
2851 // are declared as ASTNodes.
2852 OS << "#ifdef ATTR_VISITOR_DECLS_ONLY\n\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002853 for (const auto *Attr : Attrs) {
2854 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002855 if (!R.getValueAsBit("ASTNode"))
2856 continue;
2857 OS << " bool Traverse"
2858 << R.getName() << "Attr(" << R.getName() << "Attr *A);\n";
2859 OS << " bool Visit"
2860 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
2861 << " return true; \n"
Hans Wennborg4afe5042015-07-22 20:46:26 +00002862 << " }\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002863 }
2864 OS << "\n#else // ATTR_VISITOR_DECLS_ONLY\n\n";
2865
2866 // Write individual Traverse* methods for each attribute class.
Aaron Ballman2f22b942014-05-20 19:47:14 +00002867 for (const auto *Attr : Attrs) {
2868 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002869 if (!R.getValueAsBit("ASTNode"))
2870 continue;
2871
2872 OS << "template <typename Derived>\n"
DeLesley Hutchinsbb79c332013-12-30 21:03:02 +00002873 << "bool VISITORCLASS<Derived>::Traverse"
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002874 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
2875 << " if (!getDerived().VisitAttr(A))\n"
2876 << " return false;\n"
2877 << " if (!getDerived().Visit" << R.getName() << "Attr(A))\n"
2878 << " return false;\n";
2879
2880 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman2f22b942014-05-20 19:47:14 +00002881 for (const auto *Arg : ArgRecords)
2882 createArgument(*Arg, R.getName())->writeASTVisitorTraversal(OS);
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002883
2884 OS << " return true;\n";
2885 OS << "}\n\n";
2886 }
2887
2888 // Write generic Traverse routine
2889 OS << "template <typename Derived>\n"
DeLesley Hutchinsbb79c332013-12-30 21:03:02 +00002890 << "bool VISITORCLASS<Derived>::TraverseAttr(Attr *A) {\n"
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002891 << " if (!A)\n"
2892 << " return true;\n"
2893 << "\n"
John McCall2225c8b2016-03-01 00:18:05 +00002894 << " switch (A->getKind()) {\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002895
Aaron Ballman2f22b942014-05-20 19:47:14 +00002896 for (const auto *Attr : Attrs) {
2897 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002898 if (!R.getValueAsBit("ASTNode"))
2899 continue;
2900
2901 OS << " case attr::" << R.getName() << ":\n"
2902 << " return getDerived().Traverse" << R.getName() << "Attr("
2903 << "cast<" << R.getName() << "Attr>(A));\n";
2904 }
John McCall5d7cf772016-03-01 02:09:20 +00002905 OS << " }\n"; // end switch
2906 OS << " llvm_unreachable(\"bad attribute kind\");\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002907 OS << "}\n"; // end function
2908 OS << "#endif // ATTR_VISITOR_DECLS_ONLY\n";
2909}
2910
Erich Keanea32910d2017-03-23 18:51:54 +00002911void EmitClangAttrTemplateInstantiateHelper(const std::vector<Record *> &Attrs,
2912 raw_ostream &OS,
2913 bool AppliesToDecl) {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002914
Erich Keanea32910d2017-03-23 18:51:54 +00002915 OS << " switch (At->getKind()) {\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002916 for (const auto *Attr : Attrs) {
2917 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002918 if (!R.getValueAsBit("ASTNode"))
2919 continue;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002920 OS << " case attr::" << R.getName() << ": {\n";
Erich Keanea32910d2017-03-23 18:51:54 +00002921 bool ShouldClone = R.getValueAsBit("Clone") &&
2922 (!AppliesToDecl ||
2923 R.getValueAsBit("MeaningfulToClassTemplateDefinition"));
Rafael Espindola7f90b7d2012-05-15 14:09:55 +00002924
2925 if (!ShouldClone) {
Hans Wennborg59dbe862015-09-29 20:56:43 +00002926 OS << " return nullptr;\n";
Rafael Espindola7f90b7d2012-05-15 14:09:55 +00002927 OS << " }\n";
2928 continue;
2929 }
2930
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002931 OS << " const auto *A = cast<"
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002932 << R.getName() << "Attr>(At);\n";
2933 bool TDependent = R.getValueAsBit("TemplateDependent");
2934
2935 if (!TDependent) {
2936 OS << " return A->clone(C);\n";
2937 OS << " }\n";
2938 continue;
2939 }
2940
2941 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002942 std::vector<std::unique_ptr<Argument>> Args;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002943 Args.reserve(ArgRecords.size());
2944
Aaron Ballman2f22b942014-05-20 19:47:14 +00002945 for (const auto *ArgRecord : ArgRecords)
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002946 Args.emplace_back(createArgument(*ArgRecord, R.getName()));
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002947
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002948 for (auto const &ai : Args)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002949 ai->writeTemplateInstantiation(OS);
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002950
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002951 OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002952 for (auto const &ai : Args) {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002953 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002954 ai->writeTemplateInstantiationArgs(OS);
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002955 }
Aaron Ballman36a53502014-01-16 13:03:14 +00002956 OS << ", A->getSpellingListIndex());\n }\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002957 }
2958 OS << " } // end switch\n"
2959 << " llvm_unreachable(\"Unknown attribute!\");\n"
Erich Keanea32910d2017-03-23 18:51:54 +00002960 << " return nullptr;\n";
2961}
2962
2963// Emits code to instantiate dependent attributes on templates.
2964void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) {
2965 emitSourceFileHeader("Template instantiation code for attributes", OS);
2966
2967 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2968
2969 OS << "namespace clang {\n"
2970 << "namespace sema {\n\n"
2971 << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, "
2972 << "Sema &S,\n"
2973 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n";
2974 EmitClangAttrTemplateInstantiateHelper(Attrs, OS, /*AppliesToDecl*/false);
2975 OS << "}\n\n"
2976 << "Attr *instantiateTemplateAttributeForDecl(const Attr *At,\n"
2977 << " ASTContext &C, Sema &S,\n"
2978 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n";
2979 EmitClangAttrTemplateInstantiateHelper(Attrs, OS, /*AppliesToDecl*/true);
2980 OS << "}\n\n"
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00002981 << "} // end namespace sema\n"
2982 << "} // end namespace clang\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002983}
2984
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002985// Emits the list of parsed attributes.
2986void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) {
2987 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
2988
2989 OS << "#ifndef PARSED_ATTR\n";
2990 OS << "#define PARSED_ATTR(NAME) NAME\n";
2991 OS << "#endif\n\n";
2992
2993 ParsedAttrMap Names = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002994 for (const auto &I : Names) {
2995 OS << "PARSED_ATTR(" << I.first << ")\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002996 }
2997}
2998
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002999static bool isArgVariadic(const Record &R, StringRef AttrName) {
3000 return createArgument(R, AttrName)->isVariadic();
3001}
3002
Erich Keanedf9e8ae2017-10-16 22:47:26 +00003003static void emitArgInfo(const Record &R, raw_ostream &OS) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003004 // This function will count the number of arguments specified for the
3005 // attribute and emit the number of required arguments followed by the
3006 // number of optional arguments.
3007 std::vector<Record *> Args = R.getValueAsListOfDefs("Args");
3008 unsigned ArgCount = 0, OptCount = 0;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00003009 bool HasVariadic = false;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003010 for (const auto *Arg : Args) {
George Burgess IV8a36ace2016-12-01 17:52:39 +00003011 // If the arg is fake, it's the user's job to supply it: general parsing
3012 // logic shouldn't need to know anything about it.
3013 if (Arg->getValueAsBit("Fake"))
3014 continue;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003015 Arg->getValueAsBit("Optional") ? ++OptCount : ++ArgCount;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00003016 if (!HasVariadic && isArgVariadic(*Arg, R.getName()))
3017 HasVariadic = true;
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003018 }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00003019
3020 // If there is a variadic argument, we will set the optional argument count
3021 // to its largest value. Since it's currently a 4-bit number, we set it to 15.
3022 OS << ArgCount << ", " << (HasVariadic ? 15 : OptCount);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003023}
3024
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003025static void GenerateDefaultAppertainsTo(raw_ostream &OS) {
Aaron Ballman93b5cc62013-12-02 19:36:42 +00003026 OS << "static bool defaultAppertainsTo(Sema &, const AttributeList &,";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003027 OS << "const Decl *) {\n";
3028 OS << " return true;\n";
3029 OS << "}\n\n";
3030}
3031
3032static std::string CalculateDiagnostic(const Record &S) {
3033 // If the SubjectList object has a custom diagnostic associated with it,
3034 // return that directly.
Erich Keane3bff4142017-10-16 23:25:24 +00003035 const StringRef CustomDiag = S.getValueAsString("CustomDiag");
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003036 if (!CustomDiag.empty())
3037 return CustomDiag;
3038
3039 // Given the list of subjects, determine what diagnostic best fits.
3040 enum {
3041 Func = 1U << 0,
3042 Var = 1U << 1,
3043 ObjCMethod = 1U << 2,
3044 Param = 1U << 3,
3045 Class = 1U << 4,
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00003046 GenericRecord = 1U << 5,
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003047 Type = 1U << 6,
3048 ObjCIVar = 1U << 7,
3049 ObjCProp = 1U << 8,
3050 ObjCInterface = 1U << 9,
3051 Block = 1U << 10,
3052 Namespace = 1U << 11,
Aaron Ballman981ba242014-05-20 14:10:53 +00003053 Field = 1U << 12,
3054 CXXMethod = 1U << 13,
Alexis Hunt724f14e2014-11-28 00:53:20 +00003055 ObjCProtocol = 1U << 14,
Alex Lorenz24952fb2017-04-19 15:52:11 +00003056 Enum = 1U << 15,
3057 Named = 1U << 16,
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003058 };
3059 uint32_t SubMask = 0;
3060
3061 std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects");
Aaron Ballman2f22b942014-05-20 19:47:14 +00003062 for (const auto *Subject : Subjects) {
3063 const Record &R = *Subject;
Aaron Ballman80469032013-11-29 14:57:58 +00003064 std::string Name;
3065
3066 if (R.isSubClassOf("SubsetSubject")) {
3067 PrintError(R.getLoc(), "SubsetSubjects should use a custom diagnostic");
3068 // As a fallback, look through the SubsetSubject to see what its base
3069 // type is, and use that. This needs to be updated if SubsetSubjects
3070 // are allowed within other SubsetSubjects.
3071 Name = R.getValueAsDef("Base")->getName();
3072 } else
3073 Name = R.getName();
3074
3075 uint32_t V = StringSwitch<uint32_t>(Name)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003076 .Case("Function", Func)
3077 .Case("Var", Var)
3078 .Case("ObjCMethod", ObjCMethod)
3079 .Case("ParmVar", Param)
3080 .Case("TypedefName", Type)
3081 .Case("ObjCIvar", ObjCIVar)
3082 .Case("ObjCProperty", ObjCProp)
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00003083 .Case("Record", GenericRecord)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003084 .Case("ObjCInterface", ObjCInterface)
Ted Kremenekd980da22013-12-10 19:43:42 +00003085 .Case("ObjCProtocol", ObjCProtocol)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003086 .Case("Block", Block)
3087 .Case("CXXRecord", Class)
3088 .Case("Namespace", Namespace)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003089 .Case("Field", Field)
3090 .Case("CXXMethod", CXXMethod)
Alexis Hunt724f14e2014-11-28 00:53:20 +00003091 .Case("Enum", Enum)
Alex Lorenz24952fb2017-04-19 15:52:11 +00003092 .Case("Named", Named)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003093 .Default(0);
3094 if (!V) {
3095 // Something wasn't in our mapping, so be helpful and let the developer
3096 // know about it.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003097 PrintFatalError(R.getLoc(), "Unknown subject type: " + R.getName());
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003098 return "";
3099 }
3100
3101 SubMask |= V;
3102 }
3103
3104 switch (SubMask) {
3105 // For the simple cases where there's only a single entry in the mask, we
3106 // don't have to resort to bit fiddling.
3107 case Func: return "ExpectedFunction";
3108 case Var: return "ExpectedVariable";
3109 case Param: return "ExpectedParameter";
3110 case Class: return "ExpectedClass";
Alexis Hunt724f14e2014-11-28 00:53:20 +00003111 case Enum: return "ExpectedEnum";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003112 case CXXMethod:
3113 // FIXME: Currently, this maps to ExpectedMethod based on existing code,
3114 // but should map to something a bit more accurate at some point.
3115 case ObjCMethod: return "ExpectedMethod";
3116 case Type: return "ExpectedType";
3117 case ObjCInterface: return "ExpectedObjectiveCInterface";
Ted Kremenekd980da22013-12-10 19:43:42 +00003118 case ObjCProtocol: return "ExpectedObjectiveCProtocol";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003119
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00003120 // "GenericRecord" means struct, union or class; check the language options
3121 // and if not compiling for C++, strip off the class part. Note that this
3122 // relies on the fact that the context for this declares "Sema &S".
3123 case GenericRecord:
Aaron Ballman17046b82013-11-27 19:16:55 +00003124 return "(S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass : "
3125 "ExpectedStructOrUnion)";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003126 case Func | ObjCMethod | Block: return "ExpectedFunctionMethodOrBlock";
3127 case Func | ObjCMethod | Class: return "ExpectedFunctionMethodOrClass";
3128 case Func | Param:
3129 case Func | ObjCMethod | Param: return "ExpectedFunctionMethodOrParameter";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003130 case Func | ObjCMethod: return "ExpectedFunctionOrMethod";
3131 case Func | Var: return "ExpectedVariableOrFunction";
Aaron Ballman604dfec2013-12-02 17:07:07 +00003132
3133 // If not compiling for C++, the class portion does not apply.
3134 case Func | Var | Class:
3135 return "(S.getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass : "
3136 "ExpectedVariableOrFunction)";
3137
Saleem Abdulrasool511f2e52016-07-15 20:41:10 +00003138 case Func | Var | Class | ObjCInterface:
3139 return "(S.getLangOpts().CPlusPlus"
3140 " ? ((S.getLangOpts().ObjC1 || S.getLangOpts().ObjC2)"
3141 " ? ExpectedFunctionVariableClassOrObjCInterface"
3142 " : ExpectedFunctionVariableOrClass)"
3143 " : ((S.getLangOpts().ObjC1 || S.getLangOpts().ObjC2)"
3144 " ? ExpectedFunctionVariableOrObjCInterface"
3145 " : ExpectedVariableOrFunction))";
3146
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003147 case ObjCMethod | ObjCProp: return "ExpectedMethodOrProperty";
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00003148 case Func | ObjCMethod | ObjCProp:
3149 return "ExpectedFunctionOrMethodOrProperty";
Aaron Ballman173361e2014-07-16 20:28:10 +00003150 case ObjCProtocol | ObjCInterface:
3151 return "ExpectedObjectiveCInterfaceOrProtocol";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003152 case Field | Var: return "ExpectedFieldOrGlobalVar";
Alex Lorenz24952fb2017-04-19 15:52:11 +00003153
3154 case Named:
3155 return "ExpectedNamedDecl";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003156 }
3157
3158 PrintFatalError(S.getLoc(),
3159 "Could not deduce diagnostic argument for Attr subjects");
3160
3161 return "";
3162}
3163
Aaron Ballman12b9f652014-01-16 13:55:42 +00003164static std::string GetSubjectWithSuffix(const Record *R) {
George Burgess IV1881a572016-12-01 00:13:18 +00003165 const std::string &B = R->getName();
Aaron Ballman12b9f652014-01-16 13:55:42 +00003166 if (B == "DeclBase")
3167 return "Decl";
3168 return B + "Decl";
3169}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003170
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003171static std::string functionNameForCustomAppertainsTo(const Record &Subject) {
3172 return "is" + Subject.getName().str();
3173}
3174
Aaron Ballman80469032013-11-29 14:57:58 +00003175static std::string GenerateCustomAppertainsTo(const Record &Subject,
3176 raw_ostream &OS) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003177 std::string FnName = functionNameForCustomAppertainsTo(Subject);
Aaron Ballmana358c902013-12-02 14:58:17 +00003178
Aaron Ballman80469032013-11-29 14:57:58 +00003179 // If this code has already been generated, simply return the previous
3180 // instance of it.
3181 static std::set<std::string> CustomSubjectSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003182 auto I = CustomSubjectSet.find(FnName);
Aaron Ballman80469032013-11-29 14:57:58 +00003183 if (I != CustomSubjectSet.end())
3184 return *I;
3185
3186 Record *Base = Subject.getValueAsDef("Base");
3187
3188 // Not currently support custom subjects within custom subjects.
3189 if (Base->isSubClassOf("SubsetSubject")) {
3190 PrintFatalError(Subject.getLoc(),
3191 "SubsetSubjects within SubsetSubjects is not supported");
3192 return "";
3193 }
3194
Aaron Ballman80469032013-11-29 14:57:58 +00003195 OS << "static bool " << FnName << "(const Decl *D) {\n";
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003196 OS << " if (const auto *S = dyn_cast<";
Aaron Ballman12b9f652014-01-16 13:55:42 +00003197 OS << GetSubjectWithSuffix(Base);
Aaron Ballman47553042014-01-16 14:32:03 +00003198 OS << ">(D))\n";
3199 OS << " return " << Subject.getValueAsString("CheckCode") << ";\n";
3200 OS << " return false;\n";
Aaron Ballman80469032013-11-29 14:57:58 +00003201 OS << "}\n\n";
3202
3203 CustomSubjectSet.insert(FnName);
3204 return FnName;
3205}
3206
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003207static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) {
3208 // If the attribute does not contain a Subjects definition, then use the
3209 // default appertainsTo logic.
3210 if (Attr.isValueUnset("Subjects"))
Aaron Ballman93b5cc62013-12-02 19:36:42 +00003211 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003212
3213 const Record *SubjectObj = Attr.getValueAsDef("Subjects");
3214 std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
3215
3216 // If the list of subjects is empty, it is assumed that the attribute
3217 // appertains to everything.
3218 if (Subjects.empty())
Aaron Ballman93b5cc62013-12-02 19:36:42 +00003219 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003220
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003221 bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn");
3222
3223 // Otherwise, generate an appertainsTo check specific to this attribute which
3224 // checks all of the given subjects against the Decl passed in. Return the
3225 // name of that check to the caller.
Matthias Braunbbbf5d42016-12-04 05:55:09 +00003226 std::string FnName = "check" + Attr.getName().str() + "AppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003227 std::stringstream SS;
3228 SS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr, ";
3229 SS << "const Decl *D) {\n";
3230 SS << " if (";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003231 for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) {
Aaron Ballman80469032013-11-29 14:57:58 +00003232 // If the subject has custom code associated with it, generate a function
3233 // for it. The function cannot be inlined into this check (yet) because it
3234 // requires the subject to be of a specific type, and were that information
3235 // inlined here, it would not support an attribute with multiple custom
3236 // subjects.
3237 if ((*I)->isSubClassOf("SubsetSubject")) {
3238 SS << "!" << GenerateCustomAppertainsTo(**I, OS) << "(D)";
3239 } else {
Aaron Ballman12b9f652014-01-16 13:55:42 +00003240 SS << "!isa<" << GetSubjectWithSuffix(*I) << ">(D)";
Aaron Ballman80469032013-11-29 14:57:58 +00003241 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003242
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003243 if (I + 1 != E)
3244 SS << " && ";
3245 }
3246 SS << ") {\n";
3247 SS << " S.Diag(Attr.getLoc(), diag::";
3248 SS << (Warn ? "warn_attribute_wrong_decl_type" :
3249 "err_attribute_wrong_decl_type");
3250 SS << ")\n";
3251 SS << " << Attr.getName() << ";
3252 SS << CalculateDiagnostic(*SubjectObj) << ";\n";
3253 SS << " return false;\n";
3254 SS << " }\n";
3255 SS << " return true;\n";
3256 SS << "}\n\n";
3257
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003258 OS << SS.str();
3259 return FnName;
3260}
3261
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003262static void
3263emitAttributeMatchRules(PragmaClangAttributeSupport &PragmaAttributeSupport,
3264 raw_ostream &OS) {
3265 OS << "static bool checkAttributeMatchRuleAppliesTo(const Decl *D, "
3266 << AttributeSubjectMatchRule::EnumName << " rule) {\n";
3267 OS << " switch (rule) {\n";
3268 for (const auto &Rule : PragmaAttributeSupport.Rules) {
3269 if (Rule.isAbstractRule()) {
3270 OS << " case " << Rule.getEnumValue() << ":\n";
3271 OS << " assert(false && \"Abstract matcher rule isn't allowed\");\n";
3272 OS << " return false;\n";
3273 continue;
3274 }
3275 std::vector<Record *> Subjects = Rule.getSubjects();
3276 assert(!Subjects.empty() && "Missing subjects");
3277 OS << " case " << Rule.getEnumValue() << ":\n";
3278 OS << " return ";
3279 for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) {
3280 // If the subject has custom code associated with it, use the function
3281 // that was generated for GenerateAppertainsTo to check if the declaration
3282 // is valid.
3283 if ((*I)->isSubClassOf("SubsetSubject"))
3284 OS << functionNameForCustomAppertainsTo(**I) << "(D)";
3285 else
3286 OS << "isa<" << GetSubjectWithSuffix(*I) << ">(D)";
3287
3288 if (I + 1 != E)
3289 OS << " || ";
3290 }
3291 OS << ";\n";
3292 }
3293 OS << " }\n";
3294 OS << " llvm_unreachable(\"Invalid match rule\");\nreturn false;\n";
3295 OS << "}\n\n";
3296}
3297
Aaron Ballman3aff6332013-12-02 19:30:36 +00003298static void GenerateDefaultLangOptRequirements(raw_ostream &OS) {
3299 OS << "static bool defaultDiagnoseLangOpts(Sema &, ";
3300 OS << "const AttributeList &) {\n";
3301 OS << " return true;\n";
3302 OS << "}\n\n";
3303}
3304
3305static std::string GenerateLangOptRequirements(const Record &R,
3306 raw_ostream &OS) {
3307 // If the attribute has an empty or unset list of language requirements,
3308 // return the default handler.
3309 std::vector<Record *> LangOpts = R.getValueAsListOfDefs("LangOpts");
3310 if (LangOpts.empty())
3311 return "defaultDiagnoseLangOpts";
3312
3313 // Generate the test condition, as well as a unique function name for the
3314 // diagnostic test. The list of options should usually be short (one or two
3315 // options), and the uniqueness isn't strictly necessary (it is just for
3316 // codegen efficiency).
3317 std::string FnName = "check", Test;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003318 for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) {
Erich Keane3bff4142017-10-16 23:25:24 +00003319 const StringRef Part = (*I)->getValueAsString("Name");
Eric Fiselier341e8252016-09-02 18:53:31 +00003320 if ((*I)->getValueAsBit("Negated")) {
3321 FnName += "Not";
Alexis Hunt724f14e2014-11-28 00:53:20 +00003322 Test += "!";
Eric Fiselier341e8252016-09-02 18:53:31 +00003323 }
Erich Keane3bff4142017-10-16 23:25:24 +00003324 Test += "S.LangOpts.";
3325 Test += Part;
Aaron Ballman3aff6332013-12-02 19:30:36 +00003326 if (I + 1 != E)
3327 Test += " || ";
3328 FnName += Part;
3329 }
3330 FnName += "LangOpts";
3331
3332 // If this code has already been generated, simply return the previous
3333 // instance of it.
3334 static std::set<std::string> CustomLangOptsSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003335 auto I = CustomLangOptsSet.find(FnName);
Aaron Ballman3aff6332013-12-02 19:30:36 +00003336 if (I != CustomLangOptsSet.end())
3337 return *I;
3338
3339 OS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr) {\n";
3340 OS << " if (" << Test << ")\n";
3341 OS << " return true;\n\n";
3342 OS << " S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) ";
3343 OS << "<< Attr.getName();\n";
3344 OS << " return false;\n";
3345 OS << "}\n\n";
3346
3347 CustomLangOptsSet.insert(FnName);
3348 return FnName;
3349}
3350
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003351static void GenerateDefaultTargetRequirements(raw_ostream &OS) {
Bob Wilson7c730832015-07-20 22:57:31 +00003352 OS << "static bool defaultTargetRequirements(const TargetInfo &) {\n";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003353 OS << " return true;\n";
3354 OS << "}\n\n";
3355}
3356
3357static std::string GenerateTargetRequirements(const Record &Attr,
3358 const ParsedAttrMap &Dupes,
3359 raw_ostream &OS) {
3360 // If the attribute is not a target specific attribute, return the default
3361 // target handler.
3362 if (!Attr.isSubClassOf("TargetSpecificAttr"))
3363 return "defaultTargetRequirements";
3364
3365 // Get the list of architectures to be tested for.
3366 const Record *R = Attr.getValueAsDef("Target");
Craig Topper00648582017-05-31 19:01:22 +00003367 std::vector<StringRef> Arches = R->getValueAsListOfStrings("Arches");
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003368 if (Arches.empty()) {
3369 PrintError(Attr.getLoc(), "Empty list of target architectures for a "
3370 "target-specific attr");
3371 return "defaultTargetRequirements";
3372 }
3373
3374 // If there are other attributes which share the same parsed attribute kind,
3375 // such as target-specific attributes with a shared spelling, collapse the
3376 // duplicate architectures. This is required because a shared target-specific
3377 // attribute has only one AttributeList::Kind enumeration value, but it
3378 // applies to multiple target architectures. In order for the attribute to be
3379 // considered valid, all of its architectures need to be included.
3380 if (!Attr.isValueUnset("ParseKind")) {
Erich Keane3bff4142017-10-16 23:25:24 +00003381 const StringRef APK = Attr.getValueAsString("ParseKind");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003382 for (const auto &I : Dupes) {
3383 if (I.first == APK) {
Craig Topper00648582017-05-31 19:01:22 +00003384 std::vector<StringRef> DA =
3385 I.second->getValueAsDef("Target")->getValueAsListOfStrings(
3386 "Arches");
3387 Arches.insert(Arches.end(), DA.begin(), DA.end());
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003388 }
3389 }
3390 }
3391
Bob Wilson0058b822015-07-20 22:57:36 +00003392 std::string FnName = "isTarget";
3393 std::string Test;
3394 GenerateTargetSpecificAttrChecks(R, Arches, Test, &FnName);
Bob Wilson7c730832015-07-20 22:57:31 +00003395
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003396 // If this code has already been generated, simply return the previous
3397 // instance of it.
3398 static std::set<std::string> CustomTargetSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003399 auto I = CustomTargetSet.find(FnName);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003400 if (I != CustomTargetSet.end())
3401 return *I;
3402
Bob Wilson7c730832015-07-20 22:57:31 +00003403 OS << "static bool " << FnName << "(const TargetInfo &Target) {\n";
3404 OS << " const llvm::Triple &T = Target.getTriple();\n";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003405 OS << " return " << Test << ";\n";
3406 OS << "}\n\n";
3407
3408 CustomTargetSet.insert(FnName);
3409 return FnName;
3410}
3411
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003412static void GenerateDefaultSpellingIndexToSemanticSpelling(raw_ostream &OS) {
3413 OS << "static unsigned defaultSpellingIndexToSemanticSpelling("
3414 << "const AttributeList &Attr) {\n";
3415 OS << " return UINT_MAX;\n";
3416 OS << "}\n\n";
3417}
3418
3419static std::string GenerateSpellingIndexToSemanticSpelling(const Record &Attr,
3420 raw_ostream &OS) {
3421 // If the attribute does not have a semantic form, we can bail out early.
3422 if (!Attr.getValueAsBit("ASTNode"))
3423 return "defaultSpellingIndexToSemanticSpelling";
3424
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003425 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003426
3427 // If there are zero or one spellings, or all of the spellings share the same
3428 // name, we can also bail out early.
3429 if (Spellings.size() <= 1 || SpellingNamesAreCommon(Spellings))
3430 return "defaultSpellingIndexToSemanticSpelling";
3431
3432 // Generate the enumeration we will use for the mapping.
3433 SemanticSpellingMap SemanticToSyntacticMap;
3434 std::string Enum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
Matthias Braunbbbf5d42016-12-04 05:55:09 +00003435 std::string Name = Attr.getName().str() + "AttrSpellingMap";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003436
3437 OS << "static unsigned " << Name << "(const AttributeList &Attr) {\n";
3438 OS << Enum;
3439 OS << " unsigned Idx = Attr.getAttributeSpellingListIndex();\n";
3440 WriteSemanticSpellingSwitch("Idx", SemanticToSyntacticMap, OS);
3441 OS << "}\n\n";
3442
3443 return Name;
3444}
3445
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003446static bool IsKnownToGCC(const Record &Attr) {
3447 // Look at the spellings for this subject; if there are any spellings which
3448 // claim to be known to GCC, the attribute is known to GCC.
George Burgess IV1881a572016-12-01 00:13:18 +00003449 return llvm::any_of(
3450 GetFlattenedSpellings(Attr),
3451 [](const FlattenedSpelling &S) { return S.knownToGCC(); });
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00003452}
3453
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003454/// Emits the parsed attribute helpers
3455void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
3456 emitSourceFileHeader("Parsed attribute helpers", OS);
3457
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003458 PragmaClangAttributeSupport &PragmaAttributeSupport =
3459 getPragmaAttributeSupport(Records);
3460
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003461 // Get the list of parsed attributes, and accept the optional list of
3462 // duplicates due to the ParseKind.
3463 ParsedAttrMap Dupes;
3464 ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003465
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003466 // Generate the default appertainsTo, target and language option diagnostic,
3467 // and spelling list index mapping methods.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003468 GenerateDefaultAppertainsTo(OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00003469 GenerateDefaultLangOptRequirements(OS);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003470 GenerateDefaultTargetRequirements(OS);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003471 GenerateDefaultSpellingIndexToSemanticSpelling(OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003472
3473 // Generate the appertainsTo diagnostic methods and write their names into
3474 // another mapping. At the same time, generate the AttrInfoMap object
3475 // contents. Due to the reliance on generated code, use separate streams so
3476 // that code will not be interleaved.
Erich Keanedf9e8ae2017-10-16 22:47:26 +00003477 std::string Buffer;
3478 raw_string_ostream SS {Buffer};
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003479 for (auto I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003480 // TODO: If the attribute's kind appears in the list of duplicates, that is
3481 // because it is a target-specific attribute that appears multiple times.
3482 // It would be beneficial to test whether the duplicates are "similar
3483 // enough" to each other to not cause problems. For instance, check that
Alp Toker96cf7582014-01-18 21:49:37 +00003484 // the spellings are identical, and custom parsing rules match, etc.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003485
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003486 // We need to generate struct instances based off ParsedAttrInfo from
3487 // AttributeList.cpp.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003488 SS << " { ";
3489 emitArgInfo(*I->second, SS);
3490 SS << ", " << I->second->getValueAsBit("HasCustomParsing");
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003491 SS << ", " << I->second->isSubClassOf("TargetSpecificAttr");
3492 SS << ", " << I->second->isSubClassOf("TypeAttr");
Richard Smith4f902c72016-03-08 00:32:55 +00003493 SS << ", " << I->second->isSubClassOf("StmtAttr");
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003494 SS << ", " << IsKnownToGCC(*I->second);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003495 SS << ", " << PragmaAttributeSupport.isAttributedSupported(*I->second);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003496 SS << ", " << GenerateAppertainsTo(*I->second, OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00003497 SS << ", " << GenerateLangOptRequirements(*I->second, OS);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003498 SS << ", " << GenerateTargetRequirements(*I->second, Dupes, OS);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003499 SS << ", " << GenerateSpellingIndexToSemanticSpelling(*I->second, OS);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003500 SS << ", "
3501 << PragmaAttributeSupport.generateStrictConformsTo(*I->second, OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003502 SS << " }";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003503
3504 if (I + 1 != E)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003505 SS << ",";
3506
3507 SS << " // AT_" << I->first << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003508 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003509
3510 OS << "static const ParsedAttrInfo AttrInfoMap[AttributeList::UnknownAttribute + 1] = {\n";
3511 OS << SS.str();
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003512 OS << "};\n\n";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003513
3514 // Generate the attribute match rules.
3515 emitAttributeMatchRules(PragmaAttributeSupport, OS);
Michael Han4a045172012-03-07 00:12:16 +00003516}
3517
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00003518// Emits the kind list of parsed attributes
3519void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00003520 emitSourceFileHeader("Attribute name matcher", OS);
3521
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003522 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Nico Weber20e08042016-09-03 02:55:10 +00003523 std::vector<StringMatcher::StringPair> GNU, Declspec, Microsoft, CXX11,
Aaron Ballman606093a2017-10-15 15:01:42 +00003524 Keywords, Pragma, C2x;
Aaron Ballman64e69862013-12-15 13:05:48 +00003525 std::set<std::string> Seen;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003526 for (const auto *A : Attrs) {
3527 const Record &Attr = *A;
Richard Smith852e9ce2013-11-27 01:46:48 +00003528
Michael Han4a045172012-03-07 00:12:16 +00003529 bool SemaHandler = Attr.getValueAsBit("SemaHandler");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003530 bool Ignored = Attr.getValueAsBit("Ignored");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003531 if (SemaHandler || Ignored) {
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003532 // Attribute spellings can be shared between target-specific attributes,
3533 // and can be shared between syntaxes for the same attribute. For
3534 // instance, an attribute can be spelled GNU<"interrupt"> for an ARM-
3535 // specific attribute, or MSP430-specific attribute. Additionally, an
3536 // attribute can be spelled GNU<"dllexport"> and Declspec<"dllexport">
3537 // for the same semantic attribute. Ultimately, we need to map each of
3538 // these to a single AttributeList::Kind value, but the StringMatcher
3539 // class cannot handle duplicate match strings. So we generate a list of
3540 // string to match based on the syntax, and emit multiple string matchers
3541 // depending on the syntax used.
Aaron Ballman64e69862013-12-15 13:05:48 +00003542 std::string AttrName;
3543 if (Attr.isSubClassOf("TargetSpecificAttr") &&
3544 !Attr.isValueUnset("ParseKind")) {
3545 AttrName = Attr.getValueAsString("ParseKind");
3546 if (Seen.find(AttrName) != Seen.end())
3547 continue;
3548 Seen.insert(AttrName);
3549 } else
3550 AttrName = NormalizeAttrName(StringRef(Attr.getName())).str();
3551
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003552 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003553 for (const auto &S : Spellings) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00003554 const std::string &RawSpelling = S.name();
Craig Topper8ae12032014-05-07 06:21:57 +00003555 std::vector<StringMatcher::StringPair> *Matches = nullptr;
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00003556 std::string Spelling;
3557 const std::string &Variety = S.variety();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003558 if (Variety == "CXX11") {
3559 Matches = &CXX11;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003560 Spelling += S.nameSpace();
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003561 Spelling += "::";
Aaron Ballman606093a2017-10-15 15:01:42 +00003562 } else if (Variety == "C2x") {
3563 Matches = &C2x;
3564 Spelling += S.nameSpace();
3565 Spelling += "::";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003566 } else if (Variety == "GNU")
3567 Matches = &GNU;
3568 else if (Variety == "Declspec")
3569 Matches = &Declspec;
Nico Weber20e08042016-09-03 02:55:10 +00003570 else if (Variety == "Microsoft")
3571 Matches = &Microsoft;
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003572 else if (Variety == "Keyword")
3573 Matches = &Keywords;
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003574 else if (Variety == "Pragma")
3575 Matches = &Pragma;
Alexis Hunta0e54d42012-06-18 16:13:52 +00003576
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003577 assert(Matches && "Unsupported spelling variety found");
3578
Justin Lebar4086fe52017-01-05 16:51:54 +00003579 if (Variety == "GNU")
3580 Spelling += NormalizeGNUAttrSpelling(RawSpelling);
3581 else
3582 Spelling += RawSpelling;
3583
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003584 if (SemaHandler)
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003585 Matches->push_back(StringMatcher::StringPair(Spelling,
3586 "return AttributeList::AT_" + AttrName + ";"));
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003587 else
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003588 Matches->push_back(StringMatcher::StringPair(Spelling,
3589 "return AttributeList::IgnoredAttribute;"));
Michael Han4a045172012-03-07 00:12:16 +00003590 }
3591 }
3592 }
Douglas Gregor377f99b2012-05-02 17:33:51 +00003593
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003594 OS << "static AttributeList::Kind getAttrKind(StringRef Name, ";
3595 OS << "AttributeList::Syntax Syntax) {\n";
3596 OS << " if (AttributeList::AS_GNU == Syntax) {\n";
3597 StringMatcher("Name", GNU, OS).Emit();
3598 OS << " } else if (AttributeList::AS_Declspec == Syntax) {\n";
3599 StringMatcher("Name", Declspec, OS).Emit();
Nico Weber20e08042016-09-03 02:55:10 +00003600 OS << " } else if (AttributeList::AS_Microsoft == Syntax) {\n";
3601 StringMatcher("Name", Microsoft, OS).Emit();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003602 OS << " } else if (AttributeList::AS_CXX11 == Syntax) {\n";
3603 StringMatcher("Name", CXX11, OS).Emit();
Aaron Ballman606093a2017-10-15 15:01:42 +00003604 OS << " } else if (AttributeList::AS_C2x == Syntax) {\n";
3605 StringMatcher("Name", C2x, OS).Emit();
Douglas Gregorbec595a2015-06-19 18:27:45 +00003606 OS << " } else if (AttributeList::AS_Keyword == Syntax || ";
3607 OS << "AttributeList::AS_ContextSensitiveKeyword == Syntax) {\n";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003608 StringMatcher("Name", Keywords, OS).Emit();
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003609 OS << " } else if (AttributeList::AS_Pragma == Syntax) {\n";
3610 StringMatcher("Name", Pragma, OS).Emit();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003611 OS << " }\n";
3612 OS << " return AttributeList::UnknownAttribute;\n"
Douglas Gregor377f99b2012-05-02 17:33:51 +00003613 << "}\n";
Michael Han4a045172012-03-07 00:12:16 +00003614}
3615
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003616// Emits the code to dump an attribute.
3617void EmitClangAttrDump(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00003618 emitSourceFileHeader("Attribute dumper", OS);
3619
John McCall2225c8b2016-03-01 00:18:05 +00003620 OS << " switch (A->getKind()) {\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003621 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003622 for (const auto *Attr : Attrs) {
3623 const Record &R = *Attr;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003624 if (!R.getValueAsBit("ASTNode"))
3625 continue;
3626 OS << " case attr::" << R.getName() << ": {\n";
Aaron Ballmanbc909612014-01-22 21:51:20 +00003627
3628 // If the attribute has a semantically-meaningful name (which is determined
3629 // by whether there is a Spelling enumeration for it), then write out the
3630 // spelling used for the attribute.
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003631 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballmanbc909612014-01-22 21:51:20 +00003632 if (Spellings.size() > 1 && !SpellingNamesAreCommon(Spellings))
3633 OS << " OS << \" \" << A->getSpelling();\n";
3634
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003635 Args = R.getValueAsListOfDefs("Args");
3636 if (!Args.empty()) {
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003637 OS << " const auto *SA = cast<" << R.getName()
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003638 << "Attr>(A);\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00003639 for (const auto *Arg : Args)
3640 createArgument(*Arg, R.getName())->writeDump(OS);
Richard Trieude5cc7d2013-01-31 01:44:26 +00003641
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003642 for (const auto *AI : Args)
3643 createArgument(*AI, R.getName())->writeDumpChildren(OS);
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003644 }
3645 OS <<
3646 " break;\n"
3647 " }\n";
3648 }
3649 OS << " }\n";
3650}
3651
Aaron Ballman35db2b32014-01-29 22:13:45 +00003652void EmitClangAttrParserStringSwitches(RecordKeeper &Records,
3653 raw_ostream &OS) {
3654 emitSourceFileHeader("Parser-related llvm::StringSwitch cases", OS);
3655 emitClangAttrArgContextList(Records, OS);
3656 emitClangAttrIdentifierArgList(Records, OS);
3657 emitClangAttrTypeArgList(Records, OS);
3658 emitClangAttrLateParsedList(Records, OS);
3659}
3660
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003661void EmitClangAttrSubjectMatchRulesParserStringSwitches(RecordKeeper &Records,
3662 raw_ostream &OS) {
3663 getPragmaAttributeSupport(Records).generateParsingHelpers(OS);
3664}
3665
Aaron Ballman97dba042014-02-17 15:27:10 +00003666class DocumentationData {
3667public:
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003668 const Record *Documentation;
3669 const Record *Attribute;
Erich Keanea98a2be2017-10-16 20:31:05 +00003670 std::string Heading;
3671 unsigned SupportedSpellings;
Aaron Ballman97dba042014-02-17 15:27:10 +00003672
Erich Keanea98a2be2017-10-16 20:31:05 +00003673 DocumentationData(const Record &Documentation, const Record &Attribute,
3674 const std::pair<std::string, unsigned> HeadingAndKinds)
3675 : Documentation(&Documentation), Attribute(&Attribute),
3676 Heading(std::move(HeadingAndKinds.first)),
3677 SupportedSpellings(HeadingAndKinds.second) {}
Aaron Ballman97dba042014-02-17 15:27:10 +00003678};
3679
Aaron Ballman4de1b582014-02-19 22:59:32 +00003680static void WriteCategoryHeader(const Record *DocCategory,
Aaron Ballman97dba042014-02-17 15:27:10 +00003681 raw_ostream &OS) {
Erich Keane3bff4142017-10-16 23:25:24 +00003682 const StringRef Name = DocCategory->getValueAsString("Name");
3683 OS << Name << "\n" << std::string(Name.size(), '=') << "\n";
Aaron Ballman4de1b582014-02-19 22:59:32 +00003684
3685 // If there is content, print that as well.
Erich Keane3bff4142017-10-16 23:25:24 +00003686 const StringRef ContentStr = DocCategory->getValueAsString("Content");
Benjamin Kramer5c404072015-04-10 21:37:21 +00003687 // Trim leading and trailing newlines and spaces.
Erich Keane3bff4142017-10-16 23:25:24 +00003688 OS << ContentStr.trim();
Benjamin Kramer5c404072015-04-10 21:37:21 +00003689
Aaron Ballman4de1b582014-02-19 22:59:32 +00003690 OS << "\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003691}
3692
Aaron Ballmana66b5742014-02-17 15:36:08 +00003693enum SpellingKind {
3694 GNU = 1 << 0,
3695 CXX11 = 1 << 1,
Aaron Ballman606093a2017-10-15 15:01:42 +00003696 C2x = 1 << 2,
3697 Declspec = 1 << 3,
3698 Microsoft = 1 << 4,
3699 Keyword = 1 << 5,
3700 Pragma = 1 << 6
Aaron Ballmana66b5742014-02-17 15:36:08 +00003701};
3702
Erich Keanea98a2be2017-10-16 20:31:05 +00003703static std::pair<std::string, unsigned>
3704GetAttributeHeadingAndSpellingKinds(const Record &Documentation,
3705 const Record &Attribute) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003706 // FIXME: there is no way to have a per-spelling category for the attribute
3707 // documentation. This may not be a limiting factor since the spellings
3708 // should generally be consistently applied across the category.
3709
Erich Keanea98a2be2017-10-16 20:31:05 +00003710 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attribute);
Aaron Ballman97dba042014-02-17 15:27:10 +00003711
3712 // Determine the heading to be used for this attribute.
Erich Keanea98a2be2017-10-16 20:31:05 +00003713 std::string Heading = Documentation.getValueAsString("Heading");
Aaron Ballmanea6668c2014-02-21 14:14:04 +00003714 bool CustomHeading = !Heading.empty();
Aaron Ballman97dba042014-02-17 15:27:10 +00003715 if (Heading.empty()) {
3716 // If there's only one spelling, we can simply use that.
3717 if (Spellings.size() == 1)
3718 Heading = Spellings.begin()->name();
3719 else {
3720 std::set<std::string> Uniques;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003721 for (auto I = Spellings.begin(), E = Spellings.end();
3722 I != E && Uniques.size() <= 1; ++I) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003723 std::string Spelling = NormalizeNameForSpellingComparison(I->name());
3724 Uniques.insert(Spelling);
3725 }
3726 // If the semantic map has only one spelling, that is sufficient for our
3727 // needs.
3728 if (Uniques.size() == 1)
3729 Heading = *Uniques.begin();
3730 }
3731 }
3732
3733 // If the heading is still empty, it is an error.
3734 if (Heading.empty())
Erich Keanea98a2be2017-10-16 20:31:05 +00003735 PrintFatalError(Attribute.getLoc(),
Aaron Ballman97dba042014-02-17 15:27:10 +00003736 "This attribute requires a heading to be specified");
3737
3738 // Gather a list of unique spellings; this is not the same as the semantic
3739 // spelling for the attribute. Variations in underscores and other non-
3740 // semantic characters are still acceptable.
3741 std::vector<std::string> Names;
3742
Aaron Ballman97dba042014-02-17 15:27:10 +00003743 unsigned SupportedSpellings = 0;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003744 for (const auto &I : Spellings) {
3745 SpellingKind Kind = StringSwitch<SpellingKind>(I.variety())
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003746 .Case("GNU", GNU)
3747 .Case("CXX11", CXX11)
Aaron Ballman606093a2017-10-15 15:01:42 +00003748 .Case("C2x", C2x)
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003749 .Case("Declspec", Declspec)
Nico Weber20e08042016-09-03 02:55:10 +00003750 .Case("Microsoft", Microsoft)
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003751 .Case("Keyword", Keyword)
3752 .Case("Pragma", Pragma);
Aaron Ballman97dba042014-02-17 15:27:10 +00003753
3754 // Mask in the supported spelling.
3755 SupportedSpellings |= Kind;
3756
3757 std::string Name;
Aaron Ballman606093a2017-10-15 15:01:42 +00003758 if ((Kind == CXX11 || Kind == C2x) && !I.nameSpace().empty())
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003759 Name = I.nameSpace() + "::";
3760 Name += I.name();
Aaron Ballman97dba042014-02-17 15:27:10 +00003761
3762 // If this name is the same as the heading, do not add it.
3763 if (Name != Heading)
3764 Names.push_back(Name);
3765 }
3766
3767 // Print out the heading for the attribute. If there are alternate spellings,
3768 // then display those after the heading.
Aaron Ballmanea6668c2014-02-21 14:14:04 +00003769 if (!CustomHeading && !Names.empty()) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003770 Heading += " (";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003771 for (auto I = Names.begin(), E = Names.end(); I != E; ++I) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003772 if (I != Names.begin())
3773 Heading += ", ";
3774 Heading += *I;
3775 }
3776 Heading += ")";
3777 }
Aaron Ballman97dba042014-02-17 15:27:10 +00003778 if (!SupportedSpellings)
Erich Keanea98a2be2017-10-16 20:31:05 +00003779 PrintFatalError(Attribute.getLoc(),
Aaron Ballman97dba042014-02-17 15:27:10 +00003780 "Attribute has no supported spellings; cannot be "
3781 "documented");
Erich Keanea98a2be2017-10-16 20:31:05 +00003782 return std::make_pair(std::move(Heading), SupportedSpellings);
3783}
3784
3785static void WriteDocumentation(RecordKeeper &Records,
3786 const DocumentationData &Doc, raw_ostream &OS) {
3787 OS << Doc.Heading << "\n" << std::string(Doc.Heading.length(), '-') << "\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003788
3789 // List what spelling syntaxes the attribute supports.
3790 OS << ".. csv-table:: Supported Syntaxes\n";
Aaron Ballman606093a2017-10-15 15:01:42 +00003791 OS << " :header: \"GNU\", \"C++11\", \"C2x\", \"__declspec\", \"Keyword\",";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003792 OS << " \"Pragma\", \"Pragma clang attribute\"\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003793 OS << " \"";
Erich Keanea98a2be2017-10-16 20:31:05 +00003794 if (Doc.SupportedSpellings & GNU) OS << "X";
Aaron Ballman97dba042014-02-17 15:27:10 +00003795 OS << "\",\"";
Erich Keanea98a2be2017-10-16 20:31:05 +00003796 if (Doc.SupportedSpellings & CXX11) OS << "X";
Aaron Ballman97dba042014-02-17 15:27:10 +00003797 OS << "\",\"";
Erich Keanea98a2be2017-10-16 20:31:05 +00003798 if (Doc.SupportedSpellings & C2x) OS << "X";
Aaron Ballman606093a2017-10-15 15:01:42 +00003799 OS << "\",\"";
Erich Keanea98a2be2017-10-16 20:31:05 +00003800 if (Doc.SupportedSpellings & Declspec) OS << "X";
Aaron Ballman97dba042014-02-17 15:27:10 +00003801 OS << "\",\"";
Erich Keanea98a2be2017-10-16 20:31:05 +00003802 if (Doc.SupportedSpellings & Keyword) OS << "X";
Aaron Ballman120c79f2014-06-25 12:48:06 +00003803 OS << "\", \"";
Erich Keanea98a2be2017-10-16 20:31:05 +00003804 if (Doc.SupportedSpellings & Pragma) OS << "X";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003805 OS << "\", \"";
3806 if (getPragmaAttributeSupport(Records).isAttributedSupported(*Doc.Attribute))
3807 OS << "X";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003808 OS << "\"\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003809
3810 // If the attribute is deprecated, print a message about it, and possibly
3811 // provide a replacement attribute.
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003812 if (!Doc.Documentation->isValueUnset("Deprecated")) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003813 OS << "This attribute has been deprecated, and may be removed in a future "
3814 << "version of Clang.";
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003815 const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated");
Erich Keane3bff4142017-10-16 23:25:24 +00003816 const StringRef Replacement = Deprecated.getValueAsString("Replacement");
Aaron Ballman97dba042014-02-17 15:27:10 +00003817 if (!Replacement.empty())
3818 OS << " This attribute has been superseded by ``"
3819 << Replacement << "``.";
3820 OS << "\n\n";
3821 }
3822
Erich Keane3bff4142017-10-16 23:25:24 +00003823 const StringRef ContentStr = Doc.Documentation->getValueAsString("Content");
Aaron Ballman97dba042014-02-17 15:27:10 +00003824 // Trim leading and trailing newlines and spaces.
Erich Keane3bff4142017-10-16 23:25:24 +00003825 OS << ContentStr.trim();
Aaron Ballman97dba042014-02-17 15:27:10 +00003826
3827 OS << "\n\n\n";
3828}
3829
3830void EmitClangAttrDocs(RecordKeeper &Records, raw_ostream &OS) {
3831 // Get the documentation introduction paragraph.
3832 const Record *Documentation = Records.getDef("GlobalDocumentation");
3833 if (!Documentation) {
3834 PrintFatalError("The Documentation top-level definition is missing, "
3835 "no documentation will be generated.");
3836 return;
3837 }
3838
Aaron Ballman4de1b582014-02-19 22:59:32 +00003839 OS << Documentation->getValueAsString("Intro") << "\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003840
Aaron Ballman97dba042014-02-17 15:27:10 +00003841 // Gather the Documentation lists from each of the attributes, based on the
3842 // category provided.
3843 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003844 std::map<const Record *, std::vector<DocumentationData>> SplitDocs;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003845 for (const auto *A : Attrs) {
3846 const Record &Attr = *A;
Aaron Ballman97dba042014-02-17 15:27:10 +00003847 std::vector<Record *> Docs = Attr.getValueAsListOfDefs("Documentation");
Aaron Ballman2f22b942014-05-20 19:47:14 +00003848 for (const auto *D : Docs) {
3849 const Record &Doc = *D;
Aaron Ballman4de1b582014-02-19 22:59:32 +00003850 const Record *Category = Doc.getValueAsDef("Category");
Aaron Ballman97dba042014-02-17 15:27:10 +00003851 // If the category is "undocumented", then there cannot be any other
3852 // documentation categories (otherwise, the attribute would become
3853 // documented).
Erich Keane3bff4142017-10-16 23:25:24 +00003854 const StringRef Cat = Category->getValueAsString("Name");
Aaron Ballman4de1b582014-02-19 22:59:32 +00003855 bool Undocumented = Cat == "Undocumented";
Aaron Ballman97dba042014-02-17 15:27:10 +00003856 if (Undocumented && Docs.size() > 1)
3857 PrintFatalError(Doc.getLoc(),
3858 "Attribute is \"Undocumented\", but has multiple "
Erich Keanea98a2be2017-10-16 20:31:05 +00003859 "documentation categories");
Aaron Ballman97dba042014-02-17 15:27:10 +00003860
3861 if (!Undocumented)
Erich Keanea98a2be2017-10-16 20:31:05 +00003862 SplitDocs[Category].push_back(DocumentationData(
3863 Doc, Attr, GetAttributeHeadingAndSpellingKinds(Doc, Attr)));
Aaron Ballman97dba042014-02-17 15:27:10 +00003864 }
3865 }
3866
3867 // Having split the attributes out based on what documentation goes where,
3868 // we can begin to generate sections of documentation.
Erich Keanea98a2be2017-10-16 20:31:05 +00003869 for (auto &I : SplitDocs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003870 WriteCategoryHeader(I.first, OS);
Aaron Ballman97dba042014-02-17 15:27:10 +00003871
Erich Keanea98a2be2017-10-16 20:31:05 +00003872 std::sort(I.second.begin(), I.second.end(),
3873 [](const DocumentationData &D1, const DocumentationData &D2) {
3874 return D1.Heading < D2.Heading;
3875 });
3876
Aaron Ballman97dba042014-02-17 15:27:10 +00003877 // Walk over each of the attributes in the category and write out their
3878 // documentation.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003879 for (const auto &Doc : I.second)
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003880 WriteDocumentation(Records, Doc, OS);
3881 }
3882}
3883
3884void EmitTestPragmaAttributeSupportedAttributes(RecordKeeper &Records,
3885 raw_ostream &OS) {
3886 PragmaClangAttributeSupport Support = getPragmaAttributeSupport(Records);
3887 ParsedAttrMap Attrs = getParsedAttrList(Records);
3888 unsigned NumAttrs = 0;
3889 for (const auto &I : Attrs) {
3890 if (Support.isAttributedSupported(*I.second))
3891 ++NumAttrs;
3892 }
3893 OS << "#pragma clang attribute supports " << NumAttrs << " attributes:\n";
3894 for (const auto &I : Attrs) {
3895 if (!Support.isAttributedSupported(*I.second))
3896 continue;
3897 OS << I.first;
3898 if (I.second->isValueUnset("Subjects")) {
3899 OS << " ()\n";
3900 continue;
3901 }
3902 const Record *SubjectObj = I.second->getValueAsDef("Subjects");
3903 std::vector<Record *> Subjects =
3904 SubjectObj->getValueAsListOfDefs("Subjects");
3905 OS << " (";
3906 for (const auto &Subject : llvm::enumerate(Subjects)) {
3907 if (Subject.index())
3908 OS << ", ";
Alex Lorenz24952fb2017-04-19 15:52:11 +00003909 PragmaClangAttributeSupport::RuleOrAggregateRuleSet &RuleSet =
3910 Support.SubjectsToRules.find(Subject.value())->getSecond();
3911 if (RuleSet.isRule()) {
3912 OS << RuleSet.getRule().getEnumValueName();
3913 continue;
3914 }
3915 OS << "(";
3916 for (const auto &Rule : llvm::enumerate(RuleSet.getAggregateRuleSet())) {
3917 if (Rule.index())
3918 OS << ", ";
3919 OS << Rule.value().getEnumValueName();
3920 }
3921 OS << ")";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003922 }
3923 OS << ")\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003924 }
3925}
3926
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00003927} // end namespace clang