blob: 990b860deaef8a75f2da82fd7714df538db56a98 [file] [log] [blame]
Peter Collingbournebee583f2011-10-06 13:03:08 +00001//===- ClangAttrEmitter.cpp - Generate Clang attribute handling =-*- C++ -*--=//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// These tablegen backends emit Clang attribute processing code
11//
12//===----------------------------------------------------------------------===//
13
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000014#include "llvm/ADT/ArrayRef.h"
George Burgess IV1881a572016-12-01 00:13:18 +000015#include "llvm/ADT/DenseSet.h"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000016#include "llvm/ADT/iterator_range.h"
Alexis Hunta0e54d42012-06-18 16:13:52 +000017#include "llvm/ADT/SmallString.h"
Aaron Ballman28afa182014-11-17 18:17:19 +000018#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/StringExtras.h"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000020#include "llvm/ADT/StringRef.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000021#include "llvm/ADT/StringSwitch.h"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000022#include "llvm/Support/ErrorHandling.h"
23#include "llvm/Support/raw_ostream.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000024#include "llvm/TableGen/Error.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000025#include "llvm/TableGen/Record.h"
Douglas Gregor377f99b2012-05-02 17:33:51 +000026#include "llvm/TableGen/StringMatcher.h"
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000027#include "llvm/TableGen/TableGenBackend.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000028#include <algorithm>
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000029#include <cassert>
Peter Collingbournebee583f2011-10-06 13:03:08 +000030#include <cctype>
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000031#include <cstddef>
32#include <cstdint>
33#include <map>
Aaron Ballman8f1439b2014-03-05 16:49:55 +000034#include <memory>
Aaron Ballman80469032013-11-29 14:57:58 +000035#include <set>
Chandler Carruth5553d0d2014-01-07 11:51:46 +000036#include <sstream>
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000037#include <string>
38#include <utility>
39#include <vector>
Peter Collingbournebee583f2011-10-06 13:03:08 +000040
41using namespace llvm;
42
Benjamin Kramerd910d162015-03-10 18:24:01 +000043namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000044
Aaron Ballmanc669cc02014-01-27 22:10:04 +000045class FlattenedSpelling {
46 std::string V, N, NS;
47 bool K;
48
49public:
50 FlattenedSpelling(const std::string &Variety, const std::string &Name,
51 const std::string &Namespace, bool KnownToGCC) :
52 V(Variety), N(Name), NS(Namespace), K(KnownToGCC) {}
53 explicit FlattenedSpelling(const Record &Spelling) :
54 V(Spelling.getValueAsString("Variety")),
55 N(Spelling.getValueAsString("Name")) {
56
57 assert(V != "GCC" && "Given a GCC spelling, which means this hasn't been"
58 "flattened!");
Tyler Nowickie8b07ed2014-06-13 17:57:25 +000059 if (V == "CXX11" || V == "Pragma")
Aaron Ballmanc669cc02014-01-27 22:10:04 +000060 NS = Spelling.getValueAsString("Namespace");
61 bool Unset;
62 K = Spelling.getValueAsBitOrUnset("KnownToGCC", Unset);
63 }
64
65 const std::string &variety() const { return V; }
66 const std::string &name() const { return N; }
67 const std::string &nameSpace() const { return NS; }
68 bool knownToGCC() const { return K; }
69};
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000070
Hans Wennborgdcfba332015-10-06 23:40:43 +000071} // end anonymous namespace
Aaron Ballmanc669cc02014-01-27 22:10:04 +000072
Benjamin Kramerd910d162015-03-10 18:24:01 +000073static std::vector<FlattenedSpelling>
74GetFlattenedSpellings(const Record &Attr) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +000075 std::vector<Record *> Spellings = Attr.getValueAsListOfDefs("Spellings");
76 std::vector<FlattenedSpelling> Ret;
77
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +000078 for (const auto &Spelling : Spellings) {
79 if (Spelling->getValueAsString("Variety") == "GCC") {
Aaron Ballmanc669cc02014-01-27 22:10:04 +000080 // Gin up two new spelling objects to add into the list.
Benjamin Kramer3204b152015-05-29 19:42:19 +000081 Ret.emplace_back("GNU", Spelling->getValueAsString("Name"), "", true);
82 Ret.emplace_back("CXX11", Spelling->getValueAsString("Name"), "gnu",
83 true);
Aaron Ballmanc669cc02014-01-27 22:10:04 +000084 } else
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +000085 Ret.push_back(FlattenedSpelling(*Spelling));
Aaron Ballmanc669cc02014-01-27 22:10:04 +000086 }
87
88 return Ret;
89}
90
Peter Collingbournebee583f2011-10-06 13:03:08 +000091static std::string ReadPCHRecord(StringRef type) {
92 return StringSwitch<std::string>(type)
David L. Jones267b8842017-01-24 01:04:30 +000093 .EndsWith("Decl *", "Record.GetLocalDeclAs<"
94 + std::string(type, 0, type.size()-1) + ">(Record.readInt())")
95 .Case("TypeSourceInfo *", "Record.getTypeSourceInfo()")
96 .Case("Expr *", "Record.readExpr()")
97 .Case("IdentifierInfo *", "Record.getIdentifierInfo()")
98 .Case("StringRef", "Record.readString()")
99 .Default("Record.readInt()");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000100}
101
Richard Smith19978562016-05-18 00:16:51 +0000102// Get a type that is suitable for storing an object of the specified type.
103static StringRef getStorageType(StringRef type) {
104 return StringSwitch<StringRef>(type)
105 .Case("StringRef", "std::string")
106 .Default(type);
107}
108
Peter Collingbournebee583f2011-10-06 13:03:08 +0000109// Assumes that the way to get the value is SA->getname()
110static std::string WritePCHRecord(StringRef type, StringRef name) {
Richard Smith290d8012016-04-06 17:06:00 +0000111 return "Record." + StringSwitch<std::string>(type)
112 .EndsWith("Decl *", "AddDeclRef(" + std::string(name) + ");\n")
113 .Case("TypeSourceInfo *", "AddTypeSourceInfo(" + std::string(name) + ");\n")
Peter Collingbournebee583f2011-10-06 13:03:08 +0000114 .Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
Richard Smith290d8012016-04-06 17:06:00 +0000115 .Case("IdentifierInfo *", "AddIdentifierRef(" + std::string(name) + ");\n")
116 .Case("StringRef", "AddString(" + std::string(name) + ");\n")
117 .Default("push_back(" + std::string(name) + ");\n");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000118}
119
Michael Han4a045172012-03-07 00:12:16 +0000120// Normalize attribute name by removing leading and trailing
121// underscores. For example, __foo, foo__, __foo__ would
122// become foo.
123static StringRef NormalizeAttrName(StringRef AttrName) {
George Burgess IV1881a572016-12-01 00:13:18 +0000124 AttrName.consume_front("__");
125 AttrName.consume_back("__");
Michael Han4a045172012-03-07 00:12:16 +0000126 return AttrName;
127}
128
Aaron Ballman36a53502014-01-16 13:03:14 +0000129// Normalize the name by removing any and all leading and trailing underscores.
130// This is different from NormalizeAttrName in that it also handles names like
131// _pascal and __pascal.
132static StringRef NormalizeNameForSpellingComparison(StringRef Name) {
Benjamin Kramer5c404072015-04-10 21:37:21 +0000133 return Name.trim("_");
Aaron Ballman36a53502014-01-16 13:03:14 +0000134}
135
Justin Lebar4086fe52017-01-05 16:51:54 +0000136// Normalize the spelling of a GNU attribute (i.e. "x" in "__attribute__((x))"),
137// removing "__" if it appears at the beginning and end of the attribute's name.
138static StringRef NormalizeGNUAttrSpelling(StringRef AttrSpelling) {
Michael Han4a045172012-03-07 00:12:16 +0000139 if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) {
140 AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4);
141 }
142
143 return AttrSpelling;
144}
145
Aaron Ballman2f22b942014-05-20 19:47:14 +0000146typedef std::vector<std::pair<std::string, const Record *>> ParsedAttrMap;
Aaron Ballman64e69862013-12-15 13:05:48 +0000147
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000148static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,
Craig Topper8ae12032014-05-07 06:21:57 +0000149 ParsedAttrMap *Dupes = nullptr) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000150 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Aaron Ballman64e69862013-12-15 13:05:48 +0000151 std::set<std::string> Seen;
152 ParsedAttrMap R;
Aaron Ballman2f22b942014-05-20 19:47:14 +0000153 for (const auto *Attr : Attrs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000154 if (Attr->getValueAsBit("SemaHandler")) {
Aaron Ballman64e69862013-12-15 13:05:48 +0000155 std::string AN;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000156 if (Attr->isSubClassOf("TargetSpecificAttr") &&
157 !Attr->isValueUnset("ParseKind")) {
158 AN = Attr->getValueAsString("ParseKind");
Aaron Ballman64e69862013-12-15 13:05:48 +0000159
160 // If this attribute has already been handled, it does not need to be
161 // handled again.
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000162 if (Seen.find(AN) != Seen.end()) {
163 if (Dupes)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000164 Dupes->push_back(std::make_pair(AN, Attr));
Aaron Ballman64e69862013-12-15 13:05:48 +0000165 continue;
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000166 }
Aaron Ballman64e69862013-12-15 13:05:48 +0000167 Seen.insert(AN);
168 } else
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000169 AN = NormalizeAttrName(Attr->getName()).str();
Aaron Ballman64e69862013-12-15 13:05:48 +0000170
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000171 R.push_back(std::make_pair(AN, Attr));
Aaron Ballman64e69862013-12-15 13:05:48 +0000172 }
173 }
174 return R;
175}
176
Peter Collingbournebee583f2011-10-06 13:03:08 +0000177namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000178
Peter Collingbournebee583f2011-10-06 13:03:08 +0000179 class Argument {
180 std::string lowerName, upperName;
181 StringRef attrName;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000182 bool isOpt;
John McCalla62c1a92015-10-28 00:17:34 +0000183 bool Fake;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000184
185 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000186 Argument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000187 : lowerName(Arg.getValueAsString("Name")), upperName(lowerName),
John McCalla62c1a92015-10-28 00:17:34 +0000188 attrName(Attr), isOpt(false), Fake(false) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000189 if (!lowerName.empty()) {
190 lowerName[0] = std::tolower(lowerName[0]);
191 upperName[0] = std::toupper(upperName[0]);
192 }
Reid Klecknerebeb0ca2016-05-31 17:42:56 +0000193 // Work around MinGW's macro definition of 'interface' to 'struct'. We
194 // have an attribute argument called 'Interface', so only the lower case
195 // name conflicts with the macro definition.
196 if (lowerName == "interface")
197 lowerName = "interface_";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000198 }
Hans Wennborgdcfba332015-10-06 23:40:43 +0000199 virtual ~Argument() = default;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000200
201 StringRef getLowerName() const { return lowerName; }
202 StringRef getUpperName() const { return upperName; }
203 StringRef getAttrName() const { return attrName; }
204
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000205 bool isOptional() const { return isOpt; }
206 void setOptional(bool set) { isOpt = set; }
207
John McCalla62c1a92015-10-28 00:17:34 +0000208 bool isFake() const { return Fake; }
209 void setFake(bool fake) { Fake = fake; }
210
Peter Collingbournebee583f2011-10-06 13:03:08 +0000211 // These functions print the argument contents formatted in different ways.
212 virtual void writeAccessors(raw_ostream &OS) const = 0;
213 virtual void writeAccessorDefinitions(raw_ostream &OS) const {}
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +0000214 virtual void writeASTVisitorTraversal(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000215 virtual void writeCloneArgs(raw_ostream &OS) const = 0;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000216 virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0;
Daniel Dunbardc51baa2012-02-10 06:00:29 +0000217 virtual void writeTemplateInstantiation(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000218 virtual void writeCtorBody(raw_ostream &OS) const {}
219 virtual void writeCtorInitializers(raw_ostream &OS) const = 0;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000220 virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000221 virtual void writeCtorParameters(raw_ostream &OS) const = 0;
222 virtual void writeDeclarations(raw_ostream &OS) const = 0;
223 virtual void writePCHReadArgs(raw_ostream &OS) const = 0;
224 virtual void writePCHReadDecls(raw_ostream &OS) const = 0;
225 virtual void writePCHWrite(raw_ostream &OS) const = 0;
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000226 virtual void writeValue(raw_ostream &OS) const = 0;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000227 virtual void writeDump(raw_ostream &OS) const = 0;
228 virtual void writeDumpChildren(raw_ostream &OS) const {}
Richard Trieude5cc7d2013-01-31 01:44:26 +0000229 virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000230
231 virtual bool isEnumArg() const { return false; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000232 virtual bool isVariadicEnumArg() const { return false; }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000233 virtual bool isVariadic() const { return false; }
Aaron Ballman36a53502014-01-16 13:03:14 +0000234
235 virtual void writeImplicitCtorArgs(raw_ostream &OS) const {
236 OS << getUpperName();
237 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000238 };
239
240 class SimpleArgument : public Argument {
241 std::string type;
242
243 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000244 SimpleArgument(const Record &Arg, StringRef Attr, std::string T)
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000245 : Argument(Arg, Attr), type(std::move(T)) {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000246
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000247 std::string getType() const { return type; }
248
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000249 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000250 OS << " " << type << " get" << getUpperName() << "() const {\n";
251 OS << " return " << getLowerName() << ";\n";
252 OS << " }";
253 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000254
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000255 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000256 OS << getLowerName();
257 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000258
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000259 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000260 OS << "A->get" << getUpperName() << "()";
261 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000262
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000263 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000264 OS << getLowerName() << "(" << getUpperName() << ")";
265 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000266
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000267 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000268 OS << getLowerName() << "()";
269 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000270
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000271 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000272 OS << type << " " << getUpperName();
273 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000274
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000275 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000276 OS << type << " " << getLowerName() << ";";
277 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000278
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000279 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000280 std::string read = ReadPCHRecord(type);
281 OS << " " << type << " " << getLowerName() << " = " << read << ";\n";
282 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000283
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000284 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000285 OS << getLowerName();
286 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000287
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000288 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000289 OS << " " << WritePCHRecord(type, "SA->get" +
290 std::string(getUpperName()) + "()");
291 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000292
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000293 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000294 if (type == "FunctionDecl *") {
Richard Smithb87c4652013-10-31 21:23:20 +0000295 OS << "\" << get" << getUpperName()
296 << "()->getNameInfo().getAsString() << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000297 } else if (type == "IdentifierInfo *") {
Akira Hatanaka3d173132016-09-10 03:29:43 +0000298 OS << "\";\n";
299 if (isOptional())
300 OS << " if (get" << getUpperName() << "()) ";
301 else
302 OS << " ";
303 OS << "OS << get" << getUpperName() << "()->getName();\n";
304 OS << " OS << \"";
Richard Smithb87c4652013-10-31 21:23:20 +0000305 } else if (type == "TypeSourceInfo *") {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000306 OS << "\" << get" << getUpperName() << "().getAsString() << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000307 } else {
308 OS << "\" << get" << getUpperName() << "() << \"";
309 }
310 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000311
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000312 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000313 if (type == "FunctionDecl *") {
314 OS << " OS << \" \";\n";
315 OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n";
316 } else if (type == "IdentifierInfo *") {
Aaron Ballman415c4142015-11-30 15:25:34 +0000317 if (isOptional())
318 OS << " if (SA->get" << getUpperName() << "())\n ";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000319 OS << " OS << \" \" << SA->get" << getUpperName()
320 << "()->getName();\n";
Richard Smithb87c4652013-10-31 21:23:20 +0000321 } else if (type == "TypeSourceInfo *") {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000322 OS << " OS << \" \" << SA->get" << getUpperName()
323 << "().getAsString();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000324 } else if (type == "bool") {
325 OS << " if (SA->get" << getUpperName() << "()) OS << \" "
326 << getUpperName() << "\";\n";
327 } else if (type == "int" || type == "unsigned") {
328 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
329 } else {
330 llvm_unreachable("Unknown SimpleArgument type!");
331 }
332 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000333 };
334
Aaron Ballman18a78382013-11-21 00:28:23 +0000335 class DefaultSimpleArgument : public SimpleArgument {
336 int64_t Default;
337
338 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000339 DefaultSimpleArgument(const Record &Arg, StringRef Attr,
Aaron Ballman18a78382013-11-21 00:28:23 +0000340 std::string T, int64_t Default)
341 : SimpleArgument(Arg, Attr, T), Default(Default) {}
342
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000343 void writeAccessors(raw_ostream &OS) const override {
Aaron Ballman18a78382013-11-21 00:28:23 +0000344 SimpleArgument::writeAccessors(OS);
345
346 OS << "\n\n static const " << getType() << " Default" << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000347 << " = ";
348 if (getType() == "bool")
349 OS << (Default != 0 ? "true" : "false");
350 else
351 OS << Default;
352 OS << ";";
Aaron Ballman18a78382013-11-21 00:28:23 +0000353 }
354 };
355
Peter Collingbournebee583f2011-10-06 13:03:08 +0000356 class StringArgument : public Argument {
357 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000358 StringArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000359 : Argument(Arg, Attr)
360 {}
361
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000362 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000363 OS << " llvm::StringRef get" << getUpperName() << "() const {\n";
364 OS << " return llvm::StringRef(" << getLowerName() << ", "
365 << getLowerName() << "Length);\n";
366 OS << " }\n";
367 OS << " unsigned get" << getUpperName() << "Length() const {\n";
368 OS << " return " << getLowerName() << "Length;\n";
369 OS << " }\n";
370 OS << " void set" << getUpperName()
371 << "(ASTContext &C, llvm::StringRef S) {\n";
372 OS << " " << getLowerName() << "Length = S.size();\n";
373 OS << " this->" << getLowerName() << " = new (C, 1) char ["
374 << getLowerName() << "Length];\n";
Chandler Carruth38a45cc2015-08-04 03:53:01 +0000375 OS << " if (!S.empty())\n";
376 OS << " std::memcpy(this->" << getLowerName() << ", S.data(), "
Peter Collingbournebee583f2011-10-06 13:03:08 +0000377 << getLowerName() << "Length);\n";
378 OS << " }";
379 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000380
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000381 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000382 OS << "get" << getUpperName() << "()";
383 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000384
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000385 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000386 OS << "A->get" << getUpperName() << "()";
387 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000388
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000389 void writeCtorBody(raw_ostream &OS) const override {
Chandler Carruth38a45cc2015-08-04 03:53:01 +0000390 OS << " if (!" << getUpperName() << ".empty())\n";
391 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000392 << ".data(), " << getLowerName() << "Length);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000393 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000394
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000395 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000396 OS << getLowerName() << "Length(" << getUpperName() << ".size()),"
397 << getLowerName() << "(new (Ctx, 1) char[" << getLowerName()
398 << "Length])";
399 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000400
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000401 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Hans Wennborg59dbe862015-09-29 20:56:43 +0000402 OS << getLowerName() << "Length(0)," << getLowerName() << "(nullptr)";
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000403 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000404
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000405 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000406 OS << "llvm::StringRef " << getUpperName();
407 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000408
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000409 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000410 OS << "unsigned " << getLowerName() << "Length;\n";
411 OS << "char *" << getLowerName() << ";";
412 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000413
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000414 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000415 OS << " std::string " << getLowerName()
David L. Jones267b8842017-01-24 01:04:30 +0000416 << "= Record.readString();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000417 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000418
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000419 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000420 OS << getLowerName();
421 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000422
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000423 void writePCHWrite(raw_ostream &OS) const override {
Richard Smith290d8012016-04-06 17:06:00 +0000424 OS << " Record.AddString(SA->get" << getUpperName() << "());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000425 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000426
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000427 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000428 OS << "\\\"\" << get" << getUpperName() << "() << \"\\\"";
429 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000430
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000431 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000432 OS << " OS << \" \\\"\" << SA->get" << getUpperName()
433 << "() << \"\\\"\";\n";
434 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000435 };
436
437 class AlignedArgument : public Argument {
438 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000439 AlignedArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000440 : Argument(Arg, Attr)
441 {}
442
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000443 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000444 OS << " bool is" << getUpperName() << "Dependent() const;\n";
445
446 OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n";
447
448 OS << " bool is" << getUpperName() << "Expr() const {\n";
449 OS << " return is" << getLowerName() << "Expr;\n";
450 OS << " }\n";
451
452 OS << " Expr *get" << getUpperName() << "Expr() const {\n";
453 OS << " assert(is" << getLowerName() << "Expr);\n";
454 OS << " return " << getLowerName() << "Expr;\n";
455 OS << " }\n";
456
457 OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n";
458 OS << " assert(!is" << getLowerName() << "Expr);\n";
459 OS << " return " << getLowerName() << "Type;\n";
460 OS << " }";
461 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000462
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000463 void writeAccessorDefinitions(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000464 OS << "bool " << getAttrName() << "Attr::is" << getUpperName()
465 << "Dependent() const {\n";
466 OS << " if (is" << getLowerName() << "Expr)\n";
467 OS << " return " << getLowerName() << "Expr && (" << getLowerName()
468 << "Expr->isValueDependent() || " << getLowerName()
469 << "Expr->isTypeDependent());\n";
470 OS << " else\n";
471 OS << " return " << getLowerName()
472 << "Type->getType()->isDependentType();\n";
473 OS << "}\n";
474
475 // FIXME: Do not do the calculation here
476 // FIXME: Handle types correctly
477 // A null pointer means maximum alignment
Peter Collingbournebee583f2011-10-06 13:03:08 +0000478 OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
479 << "(ASTContext &Ctx) const {\n";
480 OS << " assert(!is" << getUpperName() << "Dependent());\n";
481 OS << " if (is" << getLowerName() << "Expr)\n";
Ulrich Weigandca3cb7f2015-04-21 17:29:35 +0000482 OS << " return " << getLowerName() << "Expr ? " << getLowerName()
483 << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue()"
484 << " * Ctx.getCharWidth() : "
485 << "Ctx.getTargetDefaultAlignForAttributeAligned();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000486 OS << " else\n";
487 OS << " return 0; // FIXME\n";
488 OS << "}\n";
489 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000490
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000491 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000492 OS << "is" << getLowerName() << "Expr, is" << getLowerName()
493 << "Expr ? static_cast<void*>(" << getLowerName()
494 << "Expr) : " << getLowerName()
495 << "Type";
496 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000497
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000498 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000499 // FIXME: move the definition in Sema::InstantiateAttrs to here.
500 // In the meantime, aligned attributes are cloned.
501 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000502
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000503 void writeCtorBody(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000504 OS << " if (is" << getLowerName() << "Expr)\n";
505 OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>("
506 << getUpperName() << ");\n";
507 OS << " else\n";
508 OS << " " << getLowerName()
509 << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000510 << ");\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000511 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000512
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000513 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000514 OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)";
515 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000516
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000517 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000518 OS << "is" << getLowerName() << "Expr(false)";
519 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000520
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000521 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000522 OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName();
523 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000524
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000525 void writeImplicitCtorArgs(raw_ostream &OS) const override {
Aaron Ballman36a53502014-01-16 13:03:14 +0000526 OS << "Is" << getUpperName() << "Expr, " << getUpperName();
527 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000528
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000529 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000530 OS << "bool is" << getLowerName() << "Expr;\n";
531 OS << "union {\n";
532 OS << "Expr *" << getLowerName() << "Expr;\n";
533 OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
534 OS << "};";
535 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000536
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000537 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000538 OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr";
539 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000540
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000541 void writePCHReadDecls(raw_ostream &OS) const override {
David L. Jones267b8842017-01-24 01:04:30 +0000542 OS << " bool is" << getLowerName() << "Expr = Record.readInt();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000543 OS << " void *" << getLowerName() << "Ptr;\n";
544 OS << " if (is" << getLowerName() << "Expr)\n";
David L. Jones267b8842017-01-24 01:04:30 +0000545 OS << " " << getLowerName() << "Ptr = Record.readExpr();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000546 OS << " else\n";
547 OS << " " << getLowerName()
David L. Jones267b8842017-01-24 01:04:30 +0000548 << "Ptr = Record.getTypeSourceInfo();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000549 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000550
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000551 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000552 OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n";
553 OS << " if (SA->is" << getUpperName() << "Expr())\n";
Richard Smith290d8012016-04-06 17:06:00 +0000554 OS << " Record.AddStmt(SA->get" << getUpperName() << "Expr());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000555 OS << " else\n";
Richard Smith290d8012016-04-06 17:06:00 +0000556 OS << " Record.AddTypeSourceInfo(SA->get" << getUpperName()
557 << "Type());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000558 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000559
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000560 void writeValue(raw_ostream &OS) const override {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000561 OS << "\";\n";
Aaron Ballmanc960f562014-08-01 13:49:00 +0000562 // The aligned attribute argument expression is optional.
563 OS << " if (is" << getLowerName() << "Expr && "
564 << getLowerName() << "Expr)\n";
Hans Wennborg59dbe862015-09-29 20:56:43 +0000565 OS << " " << getLowerName() << "Expr->printPretty(OS, nullptr, Policy);\n";
Richard Trieuddd01ce2014-06-09 22:53:25 +0000566 OS << " OS << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000567 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000568
569 void writeDump(raw_ostream &OS) const override {}
570
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000571 void writeDumpChildren(raw_ostream &OS) const override {
Richard Smithf7514452014-10-30 21:02:37 +0000572 OS << " if (SA->is" << getUpperName() << "Expr())\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000573 OS << " dumpStmt(SA->get" << getUpperName() << "Expr());\n";
Richard Smithf7514452014-10-30 21:02:37 +0000574 OS << " else\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000575 OS << " dumpType(SA->get" << getUpperName()
576 << "Type()->getType());\n";
577 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000578
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000579 void writeHasChildren(raw_ostream &OS) const override {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000580 OS << "SA->is" << getUpperName() << "Expr()";
581 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000582 };
583
584 class VariadicArgument : public Argument {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000585 std::string Type, ArgName, ArgSizeName, RangeName;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000586
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000587 protected:
588 // Assumed to receive a parameter: raw_ostream OS.
589 virtual void writeValueImpl(raw_ostream &OS) const {
590 OS << " OS << Val;\n";
591 }
592
Peter Collingbournebee583f2011-10-06 13:03:08 +0000593 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000594 VariadicArgument(const Record &Arg, StringRef Attr, std::string T)
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000595 : Argument(Arg, Attr), Type(std::move(T)),
596 ArgName(getLowerName().str() + "_"), ArgSizeName(ArgName + "Size"),
597 RangeName(getLowerName()) {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000598
Benjamin Kramer1b582012016-02-13 18:11:49 +0000599 const std::string &getType() const { return Type; }
600 const std::string &getArgName() const { return ArgName; }
601 const std::string &getArgSizeName() const { return ArgSizeName; }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000602 bool isVariadic() const override { return true; }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000603
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000604 void writeAccessors(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000605 std::string IteratorType = getLowerName().str() + "_iterator";
606 std::string BeginFn = getLowerName().str() + "_begin()";
607 std::string EndFn = getLowerName().str() + "_end()";
608
609 OS << " typedef " << Type << "* " << IteratorType << ";\n";
610 OS << " " << IteratorType << " " << BeginFn << " const {"
611 << " return " << ArgName << "; }\n";
612 OS << " " << IteratorType << " " << EndFn << " const {"
613 << " return " << ArgName << " + " << ArgSizeName << "; }\n";
614 OS << " unsigned " << getLowerName() << "_size() const {"
615 << " return " << ArgSizeName << "; }\n";
616 OS << " llvm::iterator_range<" << IteratorType << "> " << RangeName
617 << "() const { return llvm::make_range(" << BeginFn << ", " << EndFn
618 << "); }\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000619 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000620
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000621 void writeCloneArgs(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000622 OS << ArgName << ", " << ArgSizeName;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000623 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000624
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000625 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000626 // This isn't elegant, but we have to go through public methods...
627 OS << "A->" << getLowerName() << "_begin(), "
628 << "A->" << getLowerName() << "_size()";
629 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000630
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000631 void writeCtorBody(raw_ostream &OS) const override {
Aaron Ballmand6459e52014-05-01 15:21:03 +0000632 OS << " std::copy(" << getUpperName() << ", " << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000633 << " + " << ArgSizeName << ", " << ArgName << ");\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000634 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000635
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000636 void writeCtorInitializers(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000637 OS << ArgSizeName << "(" << getUpperName() << "Size), "
638 << ArgName << "(new (Ctx, 16) " << getType() << "["
639 << 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 writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000643 OS << ArgSizeName << "(0), " << ArgName << "(nullptr)";
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000644 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000645
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000646 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000647 OS << getType() << " *" << getUpperName() << ", unsigned "
648 << getUpperName() << "Size";
649 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000650
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000651 void writeImplicitCtorArgs(raw_ostream &OS) const override {
Aaron Ballman36a53502014-01-16 13:03:14 +0000652 OS << getUpperName() << ", " << getUpperName() << "Size";
653 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000654
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000655 void writeDeclarations(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000656 OS << " unsigned " << ArgSizeName << ";\n";
657 OS << " " << getType() << " *" << ArgName << ";";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000658 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000659
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000660 void writePCHReadDecls(raw_ostream &OS) const override {
David L. Jones267b8842017-01-24 01:04:30 +0000661 OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n";
Richard Smith19978562016-05-18 00:16:51 +0000662 OS << " SmallVector<" << getType() << ", 4> "
663 << getLowerName() << ";\n";
664 OS << " " << getLowerName() << ".reserve(" << getLowerName()
Peter Collingbournebee583f2011-10-06 13:03:08 +0000665 << "Size);\n";
Richard Smith19978562016-05-18 00:16:51 +0000666
667 // If we can't store the values in the current type (if it's something
668 // like StringRef), store them in a different type and convert the
669 // container afterwards.
670 std::string StorageType = getStorageType(getType());
671 std::string StorageName = getLowerName();
672 if (StorageType != getType()) {
673 StorageName += "Storage";
674 OS << " SmallVector<" << StorageType << ", 4> "
675 << StorageName << ";\n";
676 OS << " " << StorageName << ".reserve(" << getLowerName()
677 << "Size);\n";
678 }
679
680 OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n";
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000681 std::string read = ReadPCHRecord(Type);
Richard Smith19978562016-05-18 00:16:51 +0000682 OS << " " << StorageName << ".push_back(" << read << ");\n";
683
684 if (StorageType != getType()) {
685 OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n";
686 OS << " " << getLowerName() << ".push_back("
687 << StorageName << "[i]);\n";
688 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000689 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000690
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000691 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000692 OS << getLowerName() << ".data(), " << getLowerName() << "Size";
693 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000694
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000695 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000696 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000697 OS << " for (auto &Val : SA->" << RangeName << "())\n";
698 OS << " " << WritePCHRecord(Type, "Val");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000699 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000700
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000701 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000702 OS << "\";\n";
703 OS << " bool isFirst = true;\n"
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000704 << " for (const auto &Val : " << RangeName << "()) {\n"
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000705 << " if (isFirst) isFirst = false;\n"
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000706 << " else OS << \", \";\n";
707 writeValueImpl(OS);
708 OS << " }\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000709 OS << " OS << \"";
710 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000711
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000712 void writeDump(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000713 OS << " for (const auto &Val : SA->" << RangeName << "())\n";
714 OS << " OS << \" \" << Val;\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000715 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000716 };
717
Reid Klecknerf526b9482014-02-12 18:22:18 +0000718 // Unique the enums, but maintain the original declaration ordering.
Reid Klecknerf06b2662014-02-12 19:26:24 +0000719 std::vector<std::string>
720 uniqueEnumsInOrder(const std::vector<std::string> &enums) {
Reid Klecknerf526b9482014-02-12 18:22:18 +0000721 std::vector<std::string> uniques;
George Burgess IV1881a572016-12-01 00:13:18 +0000722 SmallDenseSet<StringRef, 8> unique_set;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000723 for (const auto &i : enums) {
George Burgess IV1881a572016-12-01 00:13:18 +0000724 if (unique_set.insert(i).second)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000725 uniques.push_back(i);
Reid Klecknerf526b9482014-02-12 18:22:18 +0000726 }
727 return uniques;
728 }
729
Peter Collingbournebee583f2011-10-06 13:03:08 +0000730 class EnumArgument : public Argument {
731 std::string type;
Aaron Ballman0e468c02014-01-05 21:08:29 +0000732 std::vector<std::string> values, enums, uniques;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000733 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000734 EnumArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000735 : Argument(Arg, Attr), type(Arg.getValueAsString("Type")),
Aaron Ballman0e468c02014-01-05 21:08:29 +0000736 values(Arg.getValueAsListOfStrings("Values")),
737 enums(Arg.getValueAsListOfStrings("Enums")),
Reid Klecknerf526b9482014-02-12 18:22:18 +0000738 uniques(uniqueEnumsInOrder(enums))
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000739 {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000740 // FIXME: Emit a proper error
741 assert(!uniques.empty());
742 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000743
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000744 bool isEnumArg() const override { return true; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000745
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000746 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000747 OS << " " << type << " get" << getUpperName() << "() const {\n";
748 OS << " return " << getLowerName() << ";\n";
749 OS << " }";
750 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000751
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000752 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000753 OS << getLowerName();
754 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000755
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000756 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000757 OS << "A->get" << getUpperName() << "()";
758 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000759 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000760 OS << getLowerName() << "(" << getUpperName() << ")";
761 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000762 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000763 OS << getLowerName() << "(" << type << "(0))";
764 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000765 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000766 OS << type << " " << getUpperName();
767 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000768 void writeDeclarations(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +0000769 auto i = uniques.cbegin(), e = uniques.cend();
Peter Collingbournebee583f2011-10-06 13:03:08 +0000770 // The last one needs to not have a comma.
771 --e;
772
773 OS << "public:\n";
774 OS << " enum " << type << " {\n";
775 for (; i != e; ++i)
776 OS << " " << *i << ",\n";
777 OS << " " << *e << "\n";
778 OS << " };\n";
779 OS << "private:\n";
780 OS << " " << type << " " << getLowerName() << ";";
781 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000782
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000783 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000784 OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName()
785 << "(static_cast<" << getAttrName() << "Attr::" << type
David L. Jones267b8842017-01-24 01:04:30 +0000786 << ">(Record.readInt()));\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000787 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000788
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000789 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000790 OS << getLowerName();
791 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000792
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000793 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000794 OS << "Record.push_back(SA->get" << getUpperName() << "());\n";
795 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000796
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000797 void writeValue(raw_ostream &OS) const override {
Aaron Ballman36d79102014-09-15 16:16:14 +0000798 // FIXME: this isn't 100% correct -- some enum arguments require printing
799 // as a string literal, while others require printing as an identifier.
800 // Tablegen currently does not distinguish between the two forms.
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000801 OS << "\\\"\" << " << getAttrName() << "Attr::Convert" << type << "ToStr(get"
802 << getUpperName() << "()) << \"\\\"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000803 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000804
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000805 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000806 OS << " switch(SA->get" << getUpperName() << "()) {\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000807 for (const auto &I : uniques) {
808 OS << " case " << getAttrName() << "Attr::" << I << ":\n";
809 OS << " OS << \" " << I << "\";\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000810 OS << " break;\n";
811 }
812 OS << " }\n";
813 }
Aaron Ballman682ee422013-09-11 19:47:58 +0000814
815 void writeConversion(raw_ostream &OS) const {
816 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
817 OS << type << " &Out) {\n";
818 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000819 OS << type << ">>(Val)\n";
Aaron Ballman682ee422013-09-11 19:47:58 +0000820 for (size_t I = 0; I < enums.size(); ++I) {
821 OS << " .Case(\"" << values[I] << "\", ";
822 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
823 }
824 OS << " .Default(Optional<" << type << ">());\n";
825 OS << " if (R) {\n";
826 OS << " Out = *R;\n return true;\n }\n";
827 OS << " return false;\n";
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000828 OS << " }\n\n";
829
830 // Mapping from enumeration values back to enumeration strings isn't
831 // trivial because some enumeration values have multiple named
832 // enumerators, such as type_visibility(internal) and
833 // type_visibility(hidden) both mapping to TypeVisibilityAttr::Hidden.
834 OS << " static const char *Convert" << type << "ToStr("
835 << type << " Val) {\n"
836 << " switch(Val) {\n";
George Burgess IV1881a572016-12-01 00:13:18 +0000837 SmallDenseSet<StringRef, 8> Uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000838 for (size_t I = 0; I < enums.size(); ++I) {
839 if (Uniques.insert(enums[I]).second)
840 OS << " case " << getAttrName() << "Attr::" << enums[I]
841 << ": return \"" << values[I] << "\";\n";
842 }
843 OS << " }\n"
844 << " llvm_unreachable(\"No enumerator with that value\");\n"
845 << " }\n";
Aaron Ballman682ee422013-09-11 19:47:58 +0000846 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000847 };
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000848
849 class VariadicEnumArgument: public VariadicArgument {
850 std::string type, QualifiedTypeName;
Aaron Ballman0e468c02014-01-05 21:08:29 +0000851 std::vector<std::string> values, enums, uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000852
853 protected:
854 void writeValueImpl(raw_ostream &OS) const override {
Aaron Ballman36d79102014-09-15 16:16:14 +0000855 // FIXME: this isn't 100% correct -- some enum arguments require printing
856 // as a string literal, while others require printing as an identifier.
857 // Tablegen currently does not distinguish between the two forms.
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000858 OS << " OS << \"\\\"\" << " << getAttrName() << "Attr::Convert" << type
859 << "ToStr(Val)" << "<< \"\\\"\";\n";
860 }
861
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000862 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000863 VariadicEnumArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000864 : VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")),
865 type(Arg.getValueAsString("Type")),
Aaron Ballman0e468c02014-01-05 21:08:29 +0000866 values(Arg.getValueAsListOfStrings("Values")),
867 enums(Arg.getValueAsListOfStrings("Enums")),
Reid Klecknerf526b9482014-02-12 18:22:18 +0000868 uniques(uniqueEnumsInOrder(enums))
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000869 {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000870 QualifiedTypeName = getAttrName().str() + "Attr::" + type;
871
872 // FIXME: Emit a proper error
873 assert(!uniques.empty());
874 }
875
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000876 bool isVariadicEnumArg() const override { return true; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000877
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000878 void writeDeclarations(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +0000879 auto i = uniques.cbegin(), e = uniques.cend();
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000880 // The last one needs to not have a comma.
881 --e;
882
883 OS << "public:\n";
884 OS << " enum " << type << " {\n";
885 for (; i != e; ++i)
886 OS << " " << *i << ",\n";
887 OS << " " << *e << "\n";
888 OS << " };\n";
889 OS << "private:\n";
890
891 VariadicArgument::writeDeclarations(OS);
892 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000893
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000894 void writeDump(raw_ostream &OS) const override {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000895 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
896 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
897 << getLowerName() << "_end(); I != E; ++I) {\n";
898 OS << " switch(*I) {\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000899 for (const auto &UI : uniques) {
900 OS << " case " << getAttrName() << "Attr::" << UI << ":\n";
901 OS << " OS << \" " << UI << "\";\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000902 OS << " break;\n";
903 }
904 OS << " }\n";
905 OS << " }\n";
906 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000907
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000908 void writePCHReadDecls(raw_ostream &OS) const override {
David L. Jones267b8842017-01-24 01:04:30 +0000909 OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000910 OS << " SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName()
911 << ";\n";
912 OS << " " << getLowerName() << ".reserve(" << getLowerName()
913 << "Size);\n";
914 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
915 OS << " " << getLowerName() << ".push_back(" << "static_cast<"
David L. Jones267b8842017-01-24 01:04:30 +0000916 << QualifiedTypeName << ">(Record.readInt()));\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000917 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000918
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000919 void writePCHWrite(raw_ostream &OS) const override {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000920 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
921 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
922 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
923 << getLowerName() << "_end(); i != e; ++i)\n";
924 OS << " " << WritePCHRecord(QualifiedTypeName, "(*i)");
925 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000926
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000927 void writeConversion(raw_ostream &OS) const {
928 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
929 OS << type << " &Out) {\n";
930 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000931 OS << type << ">>(Val)\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000932 for (size_t I = 0; I < enums.size(); ++I) {
933 OS << " .Case(\"" << values[I] << "\", ";
934 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
935 }
936 OS << " .Default(Optional<" << type << ">());\n";
937 OS << " if (R) {\n";
938 OS << " Out = *R;\n return true;\n }\n";
939 OS << " return false;\n";
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000940 OS << " }\n\n";
941
942 OS << " static const char *Convert" << type << "ToStr("
943 << type << " Val) {\n"
944 << " switch(Val) {\n";
George Burgess IV1881a572016-12-01 00:13:18 +0000945 SmallDenseSet<StringRef, 8> Uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000946 for (size_t I = 0; I < enums.size(); ++I) {
947 if (Uniques.insert(enums[I]).second)
948 OS << " case " << getAttrName() << "Attr::" << enums[I]
949 << ": return \"" << values[I] << "\";\n";
950 }
951 OS << " }\n"
952 << " llvm_unreachable(\"No enumerator with that value\");\n"
953 << " }\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000954 }
955 };
Peter Collingbournebee583f2011-10-06 13:03:08 +0000956
957 class VersionArgument : public Argument {
958 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000959 VersionArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000960 : Argument(Arg, Attr)
961 {}
962
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000963 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000964 OS << " VersionTuple get" << getUpperName() << "() const {\n";
965 OS << " return " << getLowerName() << ";\n";
966 OS << " }\n";
967 OS << " void set" << getUpperName()
968 << "(ASTContext &C, VersionTuple V) {\n";
969 OS << " " << getLowerName() << " = V;\n";
970 OS << " }";
971 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000972
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000973 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000974 OS << "get" << getUpperName() << "()";
975 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000976
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000977 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000978 OS << "A->get" << getUpperName() << "()";
979 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000980
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000981 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000982 OS << getLowerName() << "(" << getUpperName() << ")";
983 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000984
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000985 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000986 OS << getLowerName() << "()";
987 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000988
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000989 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000990 OS << "VersionTuple " << getUpperName();
991 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000992
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000993 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000994 OS << "VersionTuple " << getLowerName() << ";\n";
995 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000996
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000997 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000998 OS << " VersionTuple " << getLowerName()
David L. Jones267b8842017-01-24 01:04:30 +0000999 << "= Record.readVersionTuple();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001000 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001001
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001002 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001003 OS << getLowerName();
1004 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001005
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001006 void writePCHWrite(raw_ostream &OS) const override {
Richard Smith290d8012016-04-06 17:06:00 +00001007 OS << " Record.AddVersionTuple(SA->get" << getUpperName() << "());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001008 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001009
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001010 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001011 OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";
1012 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001013
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001014 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001015 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
1016 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001017 };
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001018
1019 class ExprArgument : public SimpleArgument {
1020 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001021 ExprArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001022 : SimpleArgument(Arg, Attr, "Expr *")
1023 {}
1024
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001025 void writeASTVisitorTraversal(raw_ostream &OS) const override {
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001026 OS << " if (!"
1027 << "getDerived().TraverseStmt(A->get" << getUpperName() << "()))\n";
1028 OS << " return false;\n";
1029 }
1030
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001031 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001032 OS << "tempInst" << getUpperName();
1033 }
1034
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001035 void writeTemplateInstantiation(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001036 OS << " " << getType() << " tempInst" << getUpperName() << ";\n";
1037 OS << " {\n";
1038 OS << " EnterExpressionEvaluationContext "
1039 << "Unevaluated(S, Sema::Unevaluated);\n";
1040 OS << " ExprResult " << "Result = S.SubstExpr("
1041 << "A->get" << getUpperName() << "(), TemplateArgs);\n";
1042 OS << " tempInst" << getUpperName() << " = "
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001043 << "Result.getAs<Expr>();\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001044 OS << " }\n";
1045 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001046
Craig Topper3164f332014-03-11 03:39:26 +00001047 void writeDump(raw_ostream &OS) const override {}
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001048
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001049 void writeDumpChildren(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001050 OS << " dumpStmt(SA->get" << getUpperName() << "());\n";
1051 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001052
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001053 void writeHasChildren(raw_ostream &OS) const override { OS << "true"; }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001054 };
1055
1056 class VariadicExprArgument : public VariadicArgument {
1057 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001058 VariadicExprArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001059 : VariadicArgument(Arg, Attr, "Expr *")
1060 {}
1061
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001062 void writeASTVisitorTraversal(raw_ostream &OS) const override {
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001063 OS << " {\n";
1064 OS << " " << getType() << " *I = A->" << getLowerName()
1065 << "_begin();\n";
1066 OS << " " << getType() << " *E = A->" << getLowerName()
1067 << "_end();\n";
1068 OS << " for (; I != E; ++I) {\n";
1069 OS << " if (!getDerived().TraverseStmt(*I))\n";
1070 OS << " return false;\n";
1071 OS << " }\n";
1072 OS << " }\n";
1073 }
1074
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001075 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001076 OS << "tempInst" << getUpperName() << ", "
1077 << "A->" << getLowerName() << "_size()";
1078 }
1079
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001080 void writeTemplateInstantiation(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +00001081 OS << " auto *tempInst" << getUpperName()
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001082 << " = new (C, 16) " << getType()
1083 << "[A->" << getLowerName() << "_size()];\n";
1084 OS << " {\n";
1085 OS << " EnterExpressionEvaluationContext "
1086 << "Unevaluated(S, Sema::Unevaluated);\n";
1087 OS << " " << getType() << " *TI = tempInst" << getUpperName()
1088 << ";\n";
1089 OS << " " << getType() << " *I = A->" << getLowerName()
1090 << "_begin();\n";
1091 OS << " " << getType() << " *E = A->" << getLowerName()
1092 << "_end();\n";
1093 OS << " for (; I != E; ++I, ++TI) {\n";
1094 OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n";
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001095 OS << " *TI = Result.getAs<Expr>();\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001096 OS << " }\n";
1097 OS << " }\n";
1098 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001099
Craig Topper3164f332014-03-11 03:39:26 +00001100 void writeDump(raw_ostream &OS) const override {}
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001101
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001102 void writeDumpChildren(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001103 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
1104 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
Richard Smithf7514452014-10-30 21:02:37 +00001105 << getLowerName() << "_end(); I != E; ++I)\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001106 OS << " dumpStmt(*I);\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001107 }
1108
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001109 void writeHasChildren(raw_ostream &OS) const override {
Richard Trieude5cc7d2013-01-31 01:44:26 +00001110 OS << "SA->" << getLowerName() << "_begin() != "
1111 << "SA->" << getLowerName() << "_end()";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001112 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001113 };
Richard Smithb87c4652013-10-31 21:23:20 +00001114
Peter Collingbourne915df992015-05-15 18:33:32 +00001115 class VariadicStringArgument : public VariadicArgument {
1116 public:
1117 VariadicStringArgument(const Record &Arg, StringRef Attr)
Benjamin Kramer1b582012016-02-13 18:11:49 +00001118 : VariadicArgument(Arg, Attr, "StringRef")
Peter Collingbourne915df992015-05-15 18:33:32 +00001119 {}
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001120
Benjamin Kramer1b582012016-02-13 18:11:49 +00001121 void writeCtorBody(raw_ostream &OS) const override {
1122 OS << " for (size_t I = 0, E = " << getArgSizeName() << "; I != E;\n"
1123 " ++I) {\n"
1124 " StringRef Ref = " << getUpperName() << "[I];\n"
1125 " if (!Ref.empty()) {\n"
1126 " char *Mem = new (Ctx, 1) char[Ref.size()];\n"
1127 " std::memcpy(Mem, Ref.data(), Ref.size());\n"
1128 " " << getArgName() << "[I] = StringRef(Mem, Ref.size());\n"
1129 " }\n"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001130 " }\n";
Benjamin Kramer1b582012016-02-13 18:11:49 +00001131 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001132
Peter Collingbourne915df992015-05-15 18:33:32 +00001133 void writeValueImpl(raw_ostream &OS) const override {
1134 OS << " OS << \"\\\"\" << Val << \"\\\"\";\n";
1135 }
1136 };
1137
Richard Smithb87c4652013-10-31 21:23:20 +00001138 class TypeArgument : public SimpleArgument {
1139 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001140 TypeArgument(const Record &Arg, StringRef Attr)
Richard Smithb87c4652013-10-31 21:23:20 +00001141 : SimpleArgument(Arg, Attr, "TypeSourceInfo *")
1142 {}
1143
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001144 void writeAccessors(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001145 OS << " QualType get" << getUpperName() << "() const {\n";
1146 OS << " return " << getLowerName() << "->getType();\n";
1147 OS << " }";
1148 OS << " " << getType() << " get" << getUpperName() << "Loc() const {\n";
1149 OS << " return " << getLowerName() << ";\n";
1150 OS << " }";
1151 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001152
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001153 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001154 OS << "A->get" << getUpperName() << "Loc()";
1155 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001156
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001157 void writePCHWrite(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001158 OS << " " << WritePCHRecord(
1159 getType(), "SA->get" + std::string(getUpperName()) + "Loc()");
1160 }
1161 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001162
Hans Wennborgdcfba332015-10-06 23:40:43 +00001163} // end anonymous namespace
Peter Collingbournebee583f2011-10-06 13:03:08 +00001164
Aaron Ballman2f22b942014-05-20 19:47:14 +00001165static std::unique_ptr<Argument>
1166createArgument(const Record &Arg, StringRef Attr,
1167 const Record *Search = nullptr) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001168 if (!Search)
1169 Search = &Arg;
1170
David Blaikie28f30ca2014-08-08 23:59:38 +00001171 std::unique_ptr<Argument> Ptr;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001172 llvm::StringRef ArgName = Search->getName();
1173
David Blaikie28f30ca2014-08-08 23:59:38 +00001174 if (ArgName == "AlignedArgument")
1175 Ptr = llvm::make_unique<AlignedArgument>(Arg, Attr);
1176 else if (ArgName == "EnumArgument")
1177 Ptr = llvm::make_unique<EnumArgument>(Arg, Attr);
1178 else if (ArgName == "ExprArgument")
1179 Ptr = llvm::make_unique<ExprArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001180 else if (ArgName == "FunctionArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001181 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "FunctionDecl *");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001182 else if (ArgName == "IdentifierArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001183 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "IdentifierInfo *");
David Majnemer4bb09802014-02-10 19:50:15 +00001184 else if (ArgName == "DefaultBoolArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001185 Ptr = llvm::make_unique<DefaultSimpleArgument>(
1186 Arg, Attr, "bool", Arg.getValueAsBit("Default"));
1187 else if (ArgName == "BoolArgument")
1188 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "bool");
Aaron Ballman18a78382013-11-21 00:28:23 +00001189 else if (ArgName == "DefaultIntArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001190 Ptr = llvm::make_unique<DefaultSimpleArgument>(
1191 Arg, Attr, "int", Arg.getValueAsInt("Default"));
1192 else if (ArgName == "IntArgument")
1193 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "int");
1194 else if (ArgName == "StringArgument")
1195 Ptr = llvm::make_unique<StringArgument>(Arg, Attr);
1196 else if (ArgName == "TypeArgument")
1197 Ptr = llvm::make_unique<TypeArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001198 else if (ArgName == "UnsignedArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001199 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "unsigned");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001200 else if (ArgName == "VariadicUnsignedArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001201 Ptr = llvm::make_unique<VariadicArgument>(Arg, Attr, "unsigned");
Peter Collingbourne915df992015-05-15 18:33:32 +00001202 else if (ArgName == "VariadicStringArgument")
1203 Ptr = llvm::make_unique<VariadicStringArgument>(Arg, Attr);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001204 else if (ArgName == "VariadicEnumArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001205 Ptr = llvm::make_unique<VariadicEnumArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001206 else if (ArgName == "VariadicExprArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001207 Ptr = llvm::make_unique<VariadicExprArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001208 else if (ArgName == "VersionArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001209 Ptr = llvm::make_unique<VersionArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001210
1211 if (!Ptr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00001212 // Search in reverse order so that the most-derived type is handled first.
Craig Topper25761242016-01-18 19:52:54 +00001213 ArrayRef<std::pair<Record*, SMRange>> Bases = Search->getSuperClasses();
David Majnemerf7e36092016-06-23 00:15:04 +00001214 for (const auto &Base : llvm::reverse(Bases)) {
Craig Topper25761242016-01-18 19:52:54 +00001215 if ((Ptr = createArgument(Arg, Attr, Base.first)))
Peter Collingbournebee583f2011-10-06 13:03:08 +00001216 break;
1217 }
1218 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001219
1220 if (Ptr && Arg.getValueAsBit("Optional"))
1221 Ptr->setOptional(true);
1222
John McCalla62c1a92015-10-28 00:17:34 +00001223 if (Ptr && Arg.getValueAsBit("Fake"))
1224 Ptr->setFake(true);
1225
David Blaikie28f30ca2014-08-08 23:59:38 +00001226 return Ptr;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001227}
1228
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001229static void writeAvailabilityValue(raw_ostream &OS) {
1230 OS << "\" << getPlatform()->getName();\n"
Manman Ren42e09eb2016-03-10 23:54:12 +00001231 << " if (getStrict()) OS << \", strict\";\n"
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001232 << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n"
1233 << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n"
1234 << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n"
1235 << " if (getUnavailable()) OS << \", unavailable\";\n"
1236 << " OS << \"";
1237}
1238
Manman Renc7890fe2016-03-16 18:50:49 +00001239static void writeDeprecatedAttrValue(raw_ostream &OS, std::string &Variety) {
1240 OS << "\\\"\" << getMessage() << \"\\\"\";\n";
1241 // Only GNU deprecated has an optional fixit argument at the second position.
1242 if (Variety == "GNU")
1243 OS << " if (!getReplacement().empty()) OS << \", \\\"\""
1244 " << getReplacement() << \"\\\"\";\n";
1245 OS << " OS << \"";
1246}
1247
Aaron Ballman3e424b52013-12-26 18:30:57 +00001248static void writeGetSpellingFunction(Record &R, raw_ostream &OS) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001249 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballman3e424b52013-12-26 18:30:57 +00001250
1251 OS << "const char *" << R.getName() << "Attr::getSpelling() const {\n";
1252 if (Spellings.empty()) {
1253 OS << " return \"(No spelling)\";\n}\n\n";
1254 return;
1255 }
1256
1257 OS << " switch (SpellingListIndex) {\n"
1258 " default:\n"
1259 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1260 " return \"(No spelling)\";\n";
1261
1262 for (unsigned I = 0; I < Spellings.size(); ++I)
1263 OS << " case " << I << ":\n"
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001264 " return \"" << Spellings[I].name() << "\";\n";
Aaron Ballman3e424b52013-12-26 18:30:57 +00001265 // End of the switch statement.
1266 OS << " }\n";
1267 // End of the getSpelling function.
1268 OS << "}\n\n";
1269}
1270
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001271static void
1272writePrettyPrintFunction(Record &R,
1273 const std::vector<std::unique_ptr<Argument>> &Args,
1274 raw_ostream &OS) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001275 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Michael Han99315932013-01-24 16:46:58 +00001276
1277 OS << "void " << R.getName() << "Attr::printPretty("
1278 << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n";
1279
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001280 if (Spellings.empty()) {
Michael Han99315932013-01-24 16:46:58 +00001281 OS << "}\n\n";
1282 return;
1283 }
1284
1285 OS <<
1286 " switch (SpellingListIndex) {\n"
1287 " default:\n"
1288 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1289 " break;\n";
1290
1291 for (unsigned I = 0; I < Spellings.size(); ++ I) {
1292 llvm::SmallString<16> Prefix;
1293 llvm::SmallString<8> Suffix;
1294 // The actual spelling of the name and namespace (if applicable)
1295 // of an attribute without considering prefix and suffix.
1296 llvm::SmallString<64> Spelling;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001297 std::string Name = Spellings[I].name();
1298 std::string Variety = Spellings[I].variety();
Michael Han99315932013-01-24 16:46:58 +00001299
1300 if (Variety == "GNU") {
1301 Prefix = " __attribute__((";
1302 Suffix = "))";
1303 } else if (Variety == "CXX11") {
1304 Prefix = " [[";
1305 Suffix = "]]";
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001306 std::string Namespace = Spellings[I].nameSpace();
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001307 if (!Namespace.empty()) {
Michael Han99315932013-01-24 16:46:58 +00001308 Spelling += Namespace;
1309 Spelling += "::";
1310 }
1311 } else if (Variety == "Declspec") {
1312 Prefix = " __declspec(";
1313 Suffix = ")";
Nico Weber20e08042016-09-03 02:55:10 +00001314 } else if (Variety == "Microsoft") {
1315 Prefix = "[";
1316 Suffix = "]";
Richard Smith0cdcc982013-01-29 01:24:26 +00001317 } else if (Variety == "Keyword") {
1318 Prefix = " ";
1319 Suffix = "";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001320 } else if (Variety == "Pragma") {
1321 Prefix = "#pragma ";
1322 Suffix = "\n";
1323 std::string Namespace = Spellings[I].nameSpace();
1324 if (!Namespace.empty()) {
1325 Spelling += Namespace;
1326 Spelling += " ";
1327 }
Michael Han99315932013-01-24 16:46:58 +00001328 } else {
Richard Smith0cdcc982013-01-29 01:24:26 +00001329 llvm_unreachable("Unknown attribute syntax variety!");
Michael Han99315932013-01-24 16:46:58 +00001330 }
1331
1332 Spelling += Name;
1333
1334 OS <<
1335 " case " << I << " : {\n"
Yaron Keren09fb7c62015-03-10 07:33:23 +00001336 " OS << \"" << Prefix << Spelling;
Michael Han99315932013-01-24 16:46:58 +00001337
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001338 if (Variety == "Pragma") {
1339 OS << " \";\n";
1340 OS << " printPrettyPragma(OS, Policy);\n";
Alexey Bataev6d455322015-10-12 06:59:48 +00001341 OS << " OS << \"\\n\";";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001342 OS << " break;\n";
1343 OS << " }\n";
1344 continue;
1345 }
1346
John McCalla62c1a92015-10-28 00:17:34 +00001347 // Fake arguments aren't part of the parsed form and should not be
1348 // pretty-printed.
George Burgess IV1881a572016-12-01 00:13:18 +00001349 bool hasNonFakeArgs = llvm::any_of(
1350 Args, [](const std::unique_ptr<Argument> &A) { return !A->isFake(); });
John McCalla62c1a92015-10-28 00:17:34 +00001351
Aaron Ballmanc960f562014-08-01 13:49:00 +00001352 // FIXME: always printing the parenthesis isn't the correct behavior for
1353 // attributes which have optional arguments that were not provided. For
1354 // instance: __attribute__((aligned)) will be pretty printed as
1355 // __attribute__((aligned())). The logic should check whether there is only
1356 // a single argument, and if it is optional, whether it has been provided.
John McCalla62c1a92015-10-28 00:17:34 +00001357 if (hasNonFakeArgs)
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001358 OS << "(";
Michael Han99315932013-01-24 16:46:58 +00001359 if (Spelling == "availability") {
1360 writeAvailabilityValue(OS);
Manman Renc7890fe2016-03-16 18:50:49 +00001361 } else if (Spelling == "deprecated" || Spelling == "gnu::deprecated") {
1362 writeDeprecatedAttrValue(OS, Variety);
Michael Han99315932013-01-24 16:46:58 +00001363 } else {
John McCalla62c1a92015-10-28 00:17:34 +00001364 unsigned index = 0;
1365 for (const auto &arg : Args) {
1366 if (arg->isFake()) continue;
1367 if (index++) OS << ", ";
1368 arg->writeValue(OS);
Michael Han99315932013-01-24 16:46:58 +00001369 }
1370 }
1371
John McCalla62c1a92015-10-28 00:17:34 +00001372 if (hasNonFakeArgs)
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001373 OS << ")";
Yaron Keren09fb7c62015-03-10 07:33:23 +00001374 OS << Suffix + "\";\n";
Michael Han99315932013-01-24 16:46:58 +00001375
1376 OS <<
1377 " break;\n"
1378 " }\n";
1379 }
1380
1381 // End of the switch statement.
1382 OS << "}\n";
1383 // End of the print function.
1384 OS << "}\n\n";
1385}
1386
Michael Hanaf02bbe2013-02-01 01:19:17 +00001387/// \brief Return the index of a spelling in a spelling list.
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001388static unsigned
1389getSpellingListIndex(const std::vector<FlattenedSpelling> &SpellingList,
1390 const FlattenedSpelling &Spelling) {
Alexander Kornienko6ee521c2015-01-23 15:36:10 +00001391 assert(!SpellingList.empty() && "Spelling list is empty!");
Michael Hanaf02bbe2013-02-01 01:19:17 +00001392
1393 for (unsigned Index = 0; Index < SpellingList.size(); ++Index) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001394 const FlattenedSpelling &S = SpellingList[Index];
1395 if (S.variety() != Spelling.variety())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001396 continue;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001397 if (S.nameSpace() != Spelling.nameSpace())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001398 continue;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001399 if (S.name() != Spelling.name())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001400 continue;
1401
1402 return Index;
1403 }
1404
1405 llvm_unreachable("Unknown spelling!");
1406}
1407
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001408static void writeAttrAccessorDefinition(const Record &R, raw_ostream &OS) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00001409 std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors");
George Burgess IV1881a572016-12-01 00:13:18 +00001410 if (Accessors.empty())
1411 return;
1412
1413 const std::vector<FlattenedSpelling> SpellingList = GetFlattenedSpellings(R);
1414 assert(!SpellingList.empty() &&
1415 "Attribute with empty spelling list can't have accessors!");
Aaron Ballman2f22b942014-05-20 19:47:14 +00001416 for (const auto *Accessor : Accessors) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00001417 std::string Name = Accessor->getValueAsString("Name");
George Burgess IV1881a572016-12-01 00:13:18 +00001418 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Accessor);
Michael Hanaf02bbe2013-02-01 01:19:17 +00001419
1420 OS << " bool " << Name << "() const { return SpellingListIndex == ";
1421 for (unsigned Index = 0; Index < Spellings.size(); ++Index) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001422 OS << getSpellingListIndex(SpellingList, Spellings[Index]);
George Burgess IV1881a572016-12-01 00:13:18 +00001423 if (Index != Spellings.size() - 1)
Michael Hanaf02bbe2013-02-01 01:19:17 +00001424 OS << " ||\n SpellingListIndex == ";
1425 else
1426 OS << "; }\n";
1427 }
1428 }
1429}
1430
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001431static bool
1432SpellingNamesAreCommon(const std::vector<FlattenedSpelling>& Spellings) {
Aaron Ballman36a53502014-01-16 13:03:14 +00001433 assert(!Spellings.empty() && "An empty list of spellings was provided");
1434 std::string FirstName = NormalizeNameForSpellingComparison(
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001435 Spellings.front().name());
Aaron Ballman2f22b942014-05-20 19:47:14 +00001436 for (const auto &Spelling :
1437 llvm::make_range(std::next(Spellings.begin()), Spellings.end())) {
1438 std::string Name = NormalizeNameForSpellingComparison(Spelling.name());
Aaron Ballman36a53502014-01-16 13:03:14 +00001439 if (Name != FirstName)
1440 return false;
1441 }
1442 return true;
1443}
1444
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001445typedef std::map<unsigned, std::string> SemanticSpellingMap;
1446static std::string
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001447CreateSemanticSpellings(const std::vector<FlattenedSpelling> &Spellings,
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001448 SemanticSpellingMap &Map) {
1449 // The enumerants are automatically generated based on the variety,
1450 // namespace (if present) and name for each attribute spelling. However,
1451 // care is taken to avoid trampling on the reserved namespace due to
1452 // underscores.
1453 std::string Ret(" enum Spelling {\n");
1454 std::set<std::string> Uniques;
1455 unsigned Idx = 0;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001456 for (auto I = Spellings.begin(), E = Spellings.end(); I != E; ++I, ++Idx) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001457 const FlattenedSpelling &S = *I;
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00001458 const std::string &Variety = S.variety();
1459 const std::string &Spelling = S.name();
1460 const std::string &Namespace = S.nameSpace();
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001461 std::string EnumName;
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001462
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001463 EnumName += (Variety + "_");
1464 if (!Namespace.empty())
1465 EnumName += (NormalizeNameForSpellingComparison(Namespace).str() +
1466 "_");
1467 EnumName += NormalizeNameForSpellingComparison(Spelling);
1468
1469 // Even if the name is not unique, this spelling index corresponds to a
1470 // particular enumerant name that we've calculated.
1471 Map[Idx] = EnumName;
1472
1473 // Since we have been stripping underscores to avoid trampling on the
1474 // reserved namespace, we may have inadvertently created duplicate
1475 // enumerant names. These duplicates are not considered part of the
1476 // semantic spelling, and can be elided.
1477 if (Uniques.find(EnumName) != Uniques.end())
1478 continue;
1479
1480 Uniques.insert(EnumName);
1481 if (I != Spellings.begin())
1482 Ret += ",\n";
Aaron Ballman9bf6b752015-03-10 17:19:18 +00001483 // Duplicate spellings are not considered part of the semantic spelling
1484 // enumeration, but the spelling index and semantic spelling values are
1485 // meant to be equivalent, so we must specify a concrete value for each
1486 // enumerator.
1487 Ret += " " + EnumName + " = " + llvm::utostr(Idx);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001488 }
1489 Ret += "\n };\n\n";
1490 return Ret;
1491}
1492
1493void WriteSemanticSpellingSwitch(const std::string &VarName,
1494 const SemanticSpellingMap &Map,
1495 raw_ostream &OS) {
1496 OS << " switch (" << VarName << ") {\n default: "
1497 << "llvm_unreachable(\"Unknown spelling list index\");\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001498 for (const auto &I : Map)
1499 OS << " case " << I.first << ": return " << I.second << ";\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001500 OS << " }\n";
1501}
1502
Aaron Ballman35db2b32014-01-29 22:13:45 +00001503// Emits the LateParsed property for attributes.
1504static void emitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) {
1505 OS << "#if defined(CLANG_ATTR_LATE_PARSED_LIST)\n";
1506 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1507
Aaron Ballman2f22b942014-05-20 19:47:14 +00001508 for (const auto *Attr : Attrs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001509 bool LateParsed = Attr->getValueAsBit("LateParsed");
Aaron Ballman35db2b32014-01-29 22:13:45 +00001510
1511 if (LateParsed) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001512 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
Aaron Ballman35db2b32014-01-29 22:13:45 +00001513
1514 // FIXME: Handle non-GNU attributes
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001515 for (const auto &I : Spellings) {
1516 if (I.variety() != "GNU")
Aaron Ballman35db2b32014-01-29 22:13:45 +00001517 continue;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001518 OS << ".Case(\"" << I.name() << "\", " << LateParsed << ")\n";
Aaron Ballman35db2b32014-01-29 22:13:45 +00001519 }
1520 }
1521 }
1522 OS << "#endif // CLANG_ATTR_LATE_PARSED_LIST\n\n";
1523}
1524
George Burgess IV1881a572016-12-01 00:13:18 +00001525template <typename Fn>
1526static void forEachUniqueSpelling(const Record &Attr, Fn &&F) {
1527 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
1528 SmallDenseSet<StringRef, 8> Seen;
1529 for (const FlattenedSpelling &S : Spellings) {
1530 if (Seen.insert(S.name()).second)
1531 F(S);
1532 }
1533}
1534
Aaron Ballman35db2b32014-01-29 22:13:45 +00001535/// \brief Emits the first-argument-is-type property for attributes.
1536static void emitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) {
1537 OS << "#if defined(CLANG_ATTR_TYPE_ARG_LIST)\n";
1538 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
1539
Aaron Ballman2f22b942014-05-20 19:47:14 +00001540 for (const auto *Attr : Attrs) {
Aaron Ballman35db2b32014-01-29 22:13:45 +00001541 // Determine whether the first argument is a type.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001542 std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
Aaron Ballman35db2b32014-01-29 22:13:45 +00001543 if (Args.empty())
1544 continue;
1545
Craig Topper25761242016-01-18 19:52:54 +00001546 if (Args[0]->getSuperClasses().back().first->getName() != "TypeArgument")
Aaron Ballman35db2b32014-01-29 22:13:45 +00001547 continue;
1548
1549 // All these spellings take a single type argument.
George Burgess IV1881a572016-12-01 00:13:18 +00001550 forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) {
1551 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
1552 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00001553 }
1554 OS << "#endif // CLANG_ATTR_TYPE_ARG_LIST\n\n";
1555}
1556
1557/// \brief Emits the parse-arguments-in-unevaluated-context property for
1558/// attributes.
1559static void emitClangAttrArgContextList(RecordKeeper &Records, raw_ostream &OS) {
1560 OS << "#if defined(CLANG_ATTR_ARG_CONTEXT_LIST)\n";
1561 ParsedAttrMap Attrs = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001562 for (const auto &I : Attrs) {
1563 const Record &Attr = *I.second;
Aaron Ballman35db2b32014-01-29 22:13:45 +00001564
1565 if (!Attr.getValueAsBit("ParseArgumentsAsUnevaluated"))
1566 continue;
1567
1568 // All these spellings take are parsed unevaluated.
George Burgess IV1881a572016-12-01 00:13:18 +00001569 forEachUniqueSpelling(Attr, [&](const FlattenedSpelling &S) {
1570 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
1571 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00001572 }
1573 OS << "#endif // CLANG_ATTR_ARG_CONTEXT_LIST\n\n";
1574}
1575
1576static bool isIdentifierArgument(Record *Arg) {
1577 return !Arg->getSuperClasses().empty() &&
Craig Topper25761242016-01-18 19:52:54 +00001578 llvm::StringSwitch<bool>(Arg->getSuperClasses().back().first->getName())
Aaron Ballman35db2b32014-01-29 22:13:45 +00001579 .Case("IdentifierArgument", true)
1580 .Case("EnumArgument", true)
Aaron Ballman55ef1512014-12-19 16:42:04 +00001581 .Case("VariadicEnumArgument", true)
Aaron Ballman35db2b32014-01-29 22:13:45 +00001582 .Default(false);
1583}
1584
1585// Emits the first-argument-is-identifier property for attributes.
1586static void emitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) {
1587 OS << "#if defined(CLANG_ATTR_IDENTIFIER_ARG_LIST)\n";
1588 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1589
Aaron Ballman2f22b942014-05-20 19:47:14 +00001590 for (const auto *Attr : Attrs) {
Aaron Ballman35db2b32014-01-29 22:13:45 +00001591 // Determine whether the first argument is an identifier.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001592 std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
Aaron Ballman35db2b32014-01-29 22:13:45 +00001593 if (Args.empty() || !isIdentifierArgument(Args[0]))
1594 continue;
1595
1596 // All these spellings take an identifier argument.
George Burgess IV1881a572016-12-01 00:13:18 +00001597 forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) {
1598 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
1599 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00001600 }
1601 OS << "#endif // CLANG_ATTR_IDENTIFIER_ARG_LIST\n\n";
1602}
1603
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001604namespace clang {
1605
1606// Emits the class definitions for attributes.
1607void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001608 emitSourceFileHeader("Attribute classes' definitions", OS);
1609
Peter Collingbournebee583f2011-10-06 13:03:08 +00001610 OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
1611 OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
1612
1613 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1614
Aaron Ballman2f22b942014-05-20 19:47:14 +00001615 for (const auto *Attr : Attrs) {
1616 const Record &R = *Attr;
Aaron Ballman06bd44b2014-02-17 18:23:02 +00001617
1618 // FIXME: Currently, documentation is generated as-needed due to the fact
1619 // that there is no way to allow a generated project "reach into" the docs
1620 // directory (for instance, it may be an out-of-tree build). However, we want
1621 // to ensure that every attribute has a Documentation field, and produce an
1622 // error if it has been neglected. Otherwise, the on-demand generation which
1623 // happens server-side will fail. This code is ensuring that functionality,
1624 // even though this Emitter doesn't technically need the documentation.
1625 // When attribute documentation can be generated as part of the build
1626 // itself, this code can be removed.
1627 (void)R.getValueAsListOfDefs("Documentation");
Douglas Gregorb2daf842012-05-02 15:56:52 +00001628
1629 if (!R.getValueAsBit("ASTNode"))
1630 continue;
1631
Craig Topper25761242016-01-18 19:52:54 +00001632 ArrayRef<std::pair<Record *, SMRange>> Supers = R.getSuperClasses();
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001633 assert(!Supers.empty() && "Forgot to specify a superclass for the attr");
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001634 std::string SuperName;
David Majnemerf7e36092016-06-23 00:15:04 +00001635 for (const auto &Super : llvm::reverse(Supers)) {
Craig Topper25761242016-01-18 19:52:54 +00001636 const Record *R = Super.first;
1637 if (R->getName() != "TargetSpecificAttr" && SuperName.empty())
1638 SuperName = R->getName();
Aaron Ballman0979e9e2013-07-30 01:44:15 +00001639 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001640
1641 OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
1642
1643 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001644 std::vector<std::unique_ptr<Argument>> Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001645 Args.reserve(ArgRecords.size());
1646
John McCalla62c1a92015-10-28 00:17:34 +00001647 bool HasOptArg = false;
1648 bool HasFakeArg = false;
Aaron Ballman2f22b942014-05-20 19:47:14 +00001649 for (const auto *ArgRecord : ArgRecords) {
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001650 Args.emplace_back(createArgument(*ArgRecord, R.getName()));
1651 Args.back()->writeDeclarations(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001652 OS << "\n\n";
John McCalla62c1a92015-10-28 00:17:34 +00001653
1654 // For these purposes, fake takes priority over optional.
1655 if (Args.back()->isFake()) {
1656 HasFakeArg = true;
1657 } else if (Args.back()->isOptional()) {
1658 HasOptArg = true;
1659 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001660 }
1661
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001662 OS << "public:\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00001663
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001664 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballman36a53502014-01-16 13:03:14 +00001665
1666 // If there are zero or one spellings, all spelling-related functionality
1667 // can be elided. If all of the spellings share the same name, the spelling
1668 // functionality can also be elided.
1669 bool ElideSpelling = (Spellings.size() <= 1) ||
1670 SpellingNamesAreCommon(Spellings);
1671
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001672 // This maps spelling index values to semantic Spelling enumerants.
1673 SemanticSpellingMap SemanticToSyntacticMap;
Aaron Ballman36a53502014-01-16 13:03:14 +00001674
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001675 if (!ElideSpelling)
1676 OS << CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
Aaron Ballman36a53502014-01-16 13:03:14 +00001677
John McCalla62c1a92015-10-28 00:17:34 +00001678 // Emit CreateImplicit factory methods.
1679 auto emitCreateImplicit = [&](bool emitFake) {
1680 OS << " static " << R.getName() << "Attr *CreateImplicit(";
1681 OS << "ASTContext &Ctx";
1682 if (!ElideSpelling)
1683 OS << ", Spelling S";
1684 for (auto const &ai : Args) {
1685 if (ai->isFake() && !emitFake) continue;
1686 OS << ", ";
1687 ai->writeCtorParameters(OS);
1688 }
1689 OS << ", SourceRange Loc = SourceRange()";
1690 OS << ") {\n";
Eugene Zelenko5f02b772015-12-08 18:49:01 +00001691 OS << " auto *A = new (Ctx) " << R.getName();
John McCalla62c1a92015-10-28 00:17:34 +00001692 OS << "Attr(Loc, Ctx, ";
1693 for (auto const &ai : Args) {
1694 if (ai->isFake() && !emitFake) continue;
1695 ai->writeImplicitCtorArgs(OS);
1696 OS << ", ";
1697 }
1698 OS << (ElideSpelling ? "0" : "S") << ");\n";
1699 OS << " A->setImplicit(true);\n";
1700 OS << " return A;\n }\n\n";
1701 };
Aaron Ballman36a53502014-01-16 13:03:14 +00001702
John McCalla62c1a92015-10-28 00:17:34 +00001703 // Emit a CreateImplicit that takes all the arguments.
1704 emitCreateImplicit(true);
1705
1706 // Emit a CreateImplicit that takes all the non-fake arguments.
1707 if (HasFakeArg) {
1708 emitCreateImplicit(false);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001709 }
Michael Han99315932013-01-24 16:46:58 +00001710
John McCalla62c1a92015-10-28 00:17:34 +00001711 // Emit constructors.
1712 auto emitCtor = [&](bool emitOpt, bool emitFake) {
1713 auto shouldEmitArg = [=](const std::unique_ptr<Argument> &arg) {
1714 if (arg->isFake()) return emitFake;
1715 if (arg->isOptional()) return emitOpt;
1716 return true;
1717 };
Michael Han99315932013-01-24 16:46:58 +00001718
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001719 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001720 for (auto const &ai : Args) {
John McCalla62c1a92015-10-28 00:17:34 +00001721 if (!shouldEmitArg(ai)) continue;
1722 OS << " , ";
1723 ai->writeCtorParameters(OS);
1724 OS << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001725 }
1726
1727 OS << " , ";
Aaron Ballman36a53502014-01-16 13:03:14 +00001728 OS << "unsigned SI\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001729
1730 OS << " )\n";
Benjamin Kramer845e32c2015-03-19 16:06:49 +00001731 OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI, "
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001732 << ( R.getValueAsBit("LateParsed") ? "true" : "false" ) << ", "
1733 << ( R.getValueAsBit("DuplicatesAllowedWhileMerging") ? "true" : "false" ) << ")\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001734
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001735 for (auto const &ai : Args) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001736 OS << " , ";
John McCalla62c1a92015-10-28 00:17:34 +00001737 if (!shouldEmitArg(ai)) {
1738 ai->writeCtorDefaultInitializers(OS);
1739 } else {
1740 ai->writeCtorInitializers(OS);
1741 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001742 OS << "\n";
1743 }
1744
1745 OS << " {\n";
1746
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001747 for (auto const &ai : Args) {
John McCalla62c1a92015-10-28 00:17:34 +00001748 if (!shouldEmitArg(ai)) continue;
1749 ai->writeCtorBody(OS);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001750 }
1751 OS << " }\n\n";
John McCalla62c1a92015-10-28 00:17:34 +00001752 };
1753
1754 // Emit a constructor that includes all the arguments.
1755 // This is necessary for cloning.
1756 emitCtor(true, true);
1757
1758 // Emit a constructor that takes all the non-fake arguments.
1759 if (HasFakeArg) {
1760 emitCtor(true, false);
1761 }
1762
1763 // Emit a constructor that takes all the non-fake, non-optional arguments.
1764 if (HasOptArg) {
1765 emitCtor(false, false);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001766 }
1767
Benjamin Kramer845e32c2015-03-19 16:06:49 +00001768 OS << " " << R.getName() << "Attr *clone(ASTContext &C) const;\n";
Craig Toppercbce6e92014-03-11 06:22:39 +00001769 OS << " void printPretty(raw_ostream &OS,\n"
Benjamin Kramer845e32c2015-03-19 16:06:49 +00001770 << " const PrintingPolicy &Policy) const;\n";
1771 OS << " const char *getSpelling() const;\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001772
1773 if (!ElideSpelling) {
1774 assert(!SemanticToSyntacticMap.empty() && "Empty semantic mapping list");
1775 OS << " Spelling getSemanticSpelling() const {\n";
1776 WriteSemanticSpellingSwitch("SpellingListIndex", SemanticToSyntacticMap,
1777 OS);
1778 OS << " }\n";
1779 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001780
Michael Hanaf02bbe2013-02-01 01:19:17 +00001781 writeAttrAccessorDefinition(R, OS);
1782
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001783 for (auto const &ai : Args) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001784 ai->writeAccessors(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001785 OS << "\n\n";
Aaron Ballman682ee422013-09-11 19:47:58 +00001786
John McCalla62c1a92015-10-28 00:17:34 +00001787 // Don't write conversion routines for fake arguments.
1788 if (ai->isFake()) continue;
1789
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001790 if (ai->isEnumArg())
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001791 static_cast<const EnumArgument *>(ai.get())->writeConversion(OS);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001792 else if (ai->isVariadicEnumArg())
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001793 static_cast<const VariadicEnumArgument *>(ai.get())
1794 ->writeConversion(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001795 }
1796
Jakob Stoklund Olesen6f2288b62012-01-13 04:57:47 +00001797 OS << R.getValueAsString("AdditionalMembers");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001798 OS << "\n\n";
1799
1800 OS << " static bool classof(const Attr *A) { return A->getKind() == "
1801 << "attr::" << R.getName() << "; }\n";
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001802
Peter Collingbournebee583f2011-10-06 13:03:08 +00001803 OS << "};\n\n";
1804 }
1805
Eugene Zelenko5f02b772015-12-08 18:49:01 +00001806 OS << "#endif // LLVM_CLANG_ATTR_CLASSES_INC\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001807}
1808
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001809// Emits the class method definitions for attributes.
1810void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00001811 emitSourceFileHeader("Attribute classes' member function definitions", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001812
1813 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001814
Aaron Ballman2f22b942014-05-20 19:47:14 +00001815 for (auto *Attr : Attrs) {
1816 Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00001817
1818 if (!R.getValueAsBit("ASTNode"))
1819 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001820
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001821 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
1822 std::vector<std::unique_ptr<Argument>> Args;
Aaron Ballman2f22b942014-05-20 19:47:14 +00001823 for (const auto *Arg : ArgRecords)
1824 Args.emplace_back(createArgument(*Arg, R.getName()));
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001825
1826 for (auto const &ai : Args)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001827 ai->writeAccessorDefinitions(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001828
1829 OS << R.getName() << "Attr *" << R.getName()
1830 << "Attr::clone(ASTContext &C) const {\n";
Hans Wennborg613807b2014-05-31 01:30:30 +00001831 OS << " auto *A = new (C) " << R.getName() << "Attr(getLocation(), C";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001832 for (auto const &ai : Args) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001833 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001834 ai->writeCloneArgs(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001835 }
Hans Wennborg613807b2014-05-31 01:30:30 +00001836 OS << ", getSpellingListIndex());\n";
1837 OS << " A->Inherited = Inherited;\n";
1838 OS << " A->IsPackExpansion = IsPackExpansion;\n";
1839 OS << " A->Implicit = Implicit;\n";
1840 OS << " return A;\n}\n\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001841
Michael Han99315932013-01-24 16:46:58 +00001842 writePrettyPrintFunction(R, Args, OS);
Aaron Ballman3e424b52013-12-26 18:30:57 +00001843 writeGetSpellingFunction(R, OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001844 }
Benjamin Kramer845e32c2015-03-19 16:06:49 +00001845
1846 // Instead of relying on virtual dispatch we just create a huge dispatch
1847 // switch. This is both smaller and faster than virtual functions.
1848 auto EmitFunc = [&](const char *Method) {
1849 OS << " switch (getKind()) {\n";
1850 for (const auto *Attr : Attrs) {
1851 const Record &R = *Attr;
1852 if (!R.getValueAsBit("ASTNode"))
1853 continue;
1854
1855 OS << " case attr::" << R.getName() << ":\n";
1856 OS << " return cast<" << R.getName() << "Attr>(this)->" << Method
1857 << ";\n";
1858 }
Benjamin Kramer845e32c2015-03-19 16:06:49 +00001859 OS << " }\n";
1860 OS << " llvm_unreachable(\"Unexpected attribute kind!\");\n";
1861 OS << "}\n\n";
1862 };
1863
1864 OS << "const char *Attr::getSpelling() const {\n";
1865 EmitFunc("getSpelling()");
1866
1867 OS << "Attr *Attr::clone(ASTContext &C) const {\n";
1868 EmitFunc("clone(C)");
1869
1870 OS << "void Attr::printPretty(raw_ostream &OS, "
1871 "const PrintingPolicy &Policy) const {\n";
1872 EmitFunc("printPretty(OS, Policy)");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001873}
1874
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001875} // end namespace clang
1876
John McCall2225c8b2016-03-01 00:18:05 +00001877static void emitAttrList(raw_ostream &OS, StringRef Class,
Peter Collingbournebee583f2011-10-06 13:03:08 +00001878 const std::vector<Record*> &AttrList) {
John McCall2225c8b2016-03-01 00:18:05 +00001879 for (auto Cur : AttrList) {
1880 OS << Class << "(" << Cur->getName() << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001881 }
1882}
1883
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001884// Determines if an attribute has a Pragma spelling.
1885static bool AttrHasPragmaSpelling(const Record *R) {
1886 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
George Burgess IV1881a572016-12-01 00:13:18 +00001887 return llvm::find_if(Spellings, [](const FlattenedSpelling &S) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00001888 return S.variety() == "Pragma";
1889 }) != Spellings.end();
1890}
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00001891
John McCall2225c8b2016-03-01 00:18:05 +00001892namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001893
John McCall2225c8b2016-03-01 00:18:05 +00001894 struct AttrClassDescriptor {
1895 const char * const MacroName;
1896 const char * const TableGenName;
1897 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001898
1899} // end anonymous namespace
John McCall2225c8b2016-03-01 00:18:05 +00001900
1901static const AttrClassDescriptor AttrClassDescriptors[] = {
1902 { "ATTR", "Attr" },
Richard Smith4f902c72016-03-08 00:32:55 +00001903 { "STMT_ATTR", "StmtAttr" },
John McCall2225c8b2016-03-01 00:18:05 +00001904 { "INHERITABLE_ATTR", "InheritableAttr" },
John McCall477f2bb2016-03-03 06:39:32 +00001905 { "INHERITABLE_PARAM_ATTR", "InheritableParamAttr" },
1906 { "PARAMETER_ABI_ATTR", "ParameterABIAttr" }
John McCall2225c8b2016-03-01 00:18:05 +00001907};
1908
1909static void emitDefaultDefine(raw_ostream &OS, StringRef name,
1910 const char *superName) {
1911 OS << "#ifndef " << name << "\n";
1912 OS << "#define " << name << "(NAME) ";
1913 if (superName) OS << superName << "(NAME)";
1914 OS << "\n#endif\n\n";
1915}
1916
1917namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001918
John McCall2225c8b2016-03-01 00:18:05 +00001919 /// A class of attributes.
1920 struct AttrClass {
1921 const AttrClassDescriptor &Descriptor;
1922 Record *TheRecord;
1923 AttrClass *SuperClass = nullptr;
1924 std::vector<AttrClass*> SubClasses;
1925 std::vector<Record*> Attrs;
1926
1927 AttrClass(const AttrClassDescriptor &Descriptor, Record *R)
1928 : Descriptor(Descriptor), TheRecord(R) {}
1929
1930 void emitDefaultDefines(raw_ostream &OS) const {
1931 // Default the macro unless this is a root class (i.e. Attr).
1932 if (SuperClass) {
1933 emitDefaultDefine(OS, Descriptor.MacroName,
1934 SuperClass->Descriptor.MacroName);
1935 }
1936 }
1937
1938 void emitUndefs(raw_ostream &OS) const {
1939 OS << "#undef " << Descriptor.MacroName << "\n";
1940 }
1941
1942 void emitAttrList(raw_ostream &OS) const {
1943 for (auto SubClass : SubClasses) {
1944 SubClass->emitAttrList(OS);
1945 }
1946
1947 ::emitAttrList(OS, Descriptor.MacroName, Attrs);
1948 }
1949
1950 void classifyAttrOnRoot(Record *Attr) {
1951 bool result = classifyAttr(Attr);
1952 assert(result && "failed to classify on root"); (void) result;
1953 }
1954
1955 void emitAttrRange(raw_ostream &OS) const {
1956 OS << "ATTR_RANGE(" << Descriptor.TableGenName
1957 << ", " << getFirstAttr()->getName()
1958 << ", " << getLastAttr()->getName() << ")\n";
1959 }
1960
1961 private:
1962 bool classifyAttr(Record *Attr) {
1963 // Check all the subclasses.
1964 for (auto SubClass : SubClasses) {
1965 if (SubClass->classifyAttr(Attr))
1966 return true;
1967 }
1968
1969 // It's not more specific than this class, but it might still belong here.
1970 if (Attr->isSubClassOf(TheRecord)) {
1971 Attrs.push_back(Attr);
1972 return true;
1973 }
1974
1975 return false;
1976 }
1977
1978 Record *getFirstAttr() const {
1979 if (!SubClasses.empty())
1980 return SubClasses.front()->getFirstAttr();
1981 return Attrs.front();
1982 }
1983
1984 Record *getLastAttr() const {
1985 if (!Attrs.empty())
1986 return Attrs.back();
1987 return SubClasses.back()->getLastAttr();
1988 }
1989 };
1990
1991 /// The entire hierarchy of attribute classes.
1992 class AttrClassHierarchy {
1993 std::vector<std::unique_ptr<AttrClass>> Classes;
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001994
John McCall2225c8b2016-03-01 00:18:05 +00001995 public:
1996 AttrClassHierarchy(RecordKeeper &Records) {
1997 // Find records for all the classes.
1998 for (auto &Descriptor : AttrClassDescriptors) {
1999 Record *ClassRecord = Records.getClass(Descriptor.TableGenName);
2000 AttrClass *Class = new AttrClass(Descriptor, ClassRecord);
2001 Classes.emplace_back(Class);
2002 }
2003
2004 // Link up the hierarchy.
2005 for (auto &Class : Classes) {
2006 if (AttrClass *SuperClass = findSuperClass(Class->TheRecord)) {
2007 Class->SuperClass = SuperClass;
2008 SuperClass->SubClasses.push_back(Class.get());
2009 }
2010 }
2011
2012#ifndef NDEBUG
2013 for (auto i = Classes.begin(), e = Classes.end(); i != e; ++i) {
2014 assert((i == Classes.begin()) == ((*i)->SuperClass == nullptr) &&
2015 "only the first class should be a root class!");
2016 }
2017#endif
2018 }
2019
2020 void emitDefaultDefines(raw_ostream &OS) const {
2021 for (auto &Class : Classes) {
2022 Class->emitDefaultDefines(OS);
2023 }
2024 }
2025
2026 void emitUndefs(raw_ostream &OS) const {
2027 for (auto &Class : Classes) {
2028 Class->emitUndefs(OS);
2029 }
2030 }
2031
2032 void emitAttrLists(raw_ostream &OS) const {
2033 // Just start from the root class.
2034 Classes[0]->emitAttrList(OS);
2035 }
2036
2037 void emitAttrRanges(raw_ostream &OS) const {
2038 for (auto &Class : Classes)
2039 Class->emitAttrRange(OS);
2040 }
2041
2042 void classifyAttr(Record *Attr) {
2043 // Add the attribute to the root class.
2044 Classes[0]->classifyAttrOnRoot(Attr);
2045 }
2046
2047 private:
2048 AttrClass *findClassByRecord(Record *R) const {
2049 for (auto &Class : Classes) {
2050 if (Class->TheRecord == R)
2051 return Class.get();
2052 }
2053 return nullptr;
2054 }
2055
2056 AttrClass *findSuperClass(Record *R) const {
2057 // TableGen flattens the superclass list, so we just need to walk it
2058 // in reverse.
2059 auto SuperClasses = R->getSuperClasses();
2060 for (signed i = 0, e = SuperClasses.size(); i != e; ++i) {
2061 auto SuperClass = findClassByRecord(SuperClasses[e - i - 1].first);
2062 if (SuperClass) return SuperClass;
2063 }
2064 return nullptr;
2065 }
2066 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002067
2068} // end anonymous namespace
John McCall2225c8b2016-03-01 00:18:05 +00002069
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002070namespace clang {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002071
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002072// Emits the enumeration list for attributes.
2073void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002074 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002075
John McCall2225c8b2016-03-01 00:18:05 +00002076 AttrClassHierarchy Hierarchy(Records);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002077
John McCall2225c8b2016-03-01 00:18:05 +00002078 // Add defaulting macro definitions.
2079 Hierarchy.emitDefaultDefines(OS);
2080 emitDefaultDefine(OS, "PRAGMA_SPELLING_ATTR", nullptr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002081
John McCall2225c8b2016-03-01 00:18:05 +00002082 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
2083 std::vector<Record *> PragmaAttrs;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002084 for (auto *Attr : Attrs) {
2085 if (!Attr->getValueAsBit("ASTNode"))
Douglas Gregorb2daf842012-05-02 15:56:52 +00002086 continue;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002087
John McCall2225c8b2016-03-01 00:18:05 +00002088 // Add the attribute to the ad-hoc groups.
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002089 if (AttrHasPragmaSpelling(Attr))
2090 PragmaAttrs.push_back(Attr);
2091
John McCall2225c8b2016-03-01 00:18:05 +00002092 // Place it in the hierarchy.
2093 Hierarchy.classifyAttr(Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002094 }
2095
John McCall2225c8b2016-03-01 00:18:05 +00002096 // Emit the main attribute list.
2097 Hierarchy.emitAttrLists(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002098
John McCall2225c8b2016-03-01 00:18:05 +00002099 // Emit the ad hoc groups.
2100 emitAttrList(OS, "PRAGMA_SPELLING_ATTR", PragmaAttrs);
2101
2102 // Emit the attribute ranges.
2103 OS << "#ifdef ATTR_RANGE\n";
2104 Hierarchy.emitAttrRanges(OS);
2105 OS << "#undef ATTR_RANGE\n";
2106 OS << "#endif\n";
2107
2108 Hierarchy.emitUndefs(OS);
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002109 OS << "#undef PRAGMA_SPELLING_ATTR\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002110}
2111
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002112// Emits the code to read an attribute from a precompiled header.
2113void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002114 emitSourceFileHeader("Attribute deserialization code", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002115
2116 Record *InhClass = Records.getClass("InheritableAttr");
2117 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
2118 ArgRecords;
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002119 std::vector<std::unique_ptr<Argument>> Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002120
2121 OS << " switch (Kind) {\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002122 for (const auto *Attr : Attrs) {
2123 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002124 if (!R.getValueAsBit("ASTNode"))
2125 continue;
2126
Peter Collingbournebee583f2011-10-06 13:03:08 +00002127 OS << " case attr::" << R.getName() << ": {\n";
2128 if (R.isSubClassOf(InhClass))
David L. Jones267b8842017-01-24 01:04:30 +00002129 OS << " bool isInherited = Record.readInt();\n";
2130 OS << " bool isImplicit = Record.readInt();\n";
2131 OS << " unsigned Spelling = Record.readInt();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002132 ArgRecords = R.getValueAsListOfDefs("Args");
2133 Args.clear();
Aaron Ballman2f22b942014-05-20 19:47:14 +00002134 for (const auto *Arg : ArgRecords) {
2135 Args.emplace_back(createArgument(*Arg, R.getName()));
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002136 Args.back()->writePCHReadDecls(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002137 }
2138 OS << " New = new (Context) " << R.getName() << "Attr(Range, Context";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002139 for (auto const &ri : Args) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00002140 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002141 ri->writePCHReadArgs(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002142 }
Aaron Ballman36a53502014-01-16 13:03:14 +00002143 OS << ", Spelling);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002144 if (R.isSubClassOf(InhClass))
2145 OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002146 OS << " New->setImplicit(isImplicit);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002147 OS << " break;\n";
2148 OS << " }\n";
2149 }
2150 OS << " }\n";
2151}
2152
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002153// Emits the code to write an attribute to a precompiled header.
2154void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002155 emitSourceFileHeader("Attribute serialization code", OS);
2156
Peter Collingbournebee583f2011-10-06 13:03:08 +00002157 Record *InhClass = Records.getClass("InheritableAttr");
2158 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002159
2160 OS << " switch (A->getKind()) {\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002161 for (const auto *Attr : Attrs) {
2162 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002163 if (!R.getValueAsBit("ASTNode"))
2164 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002165 OS << " case attr::" << R.getName() << ": {\n";
2166 Args = R.getValueAsListOfDefs("Args");
2167 if (R.isSubClassOf(InhClass) || !Args.empty())
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002168 OS << " const auto *SA = cast<" << R.getName()
Peter Collingbournebee583f2011-10-06 13:03:08 +00002169 << "Attr>(A);\n";
2170 if (R.isSubClassOf(InhClass))
2171 OS << " Record.push_back(SA->isInherited());\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002172 OS << " Record.push_back(A->isImplicit());\n";
2173 OS << " Record.push_back(A->getSpellingListIndex());\n";
2174
Aaron Ballman2f22b942014-05-20 19:47:14 +00002175 for (const auto *Arg : Args)
2176 createArgument(*Arg, R.getName())->writePCHWrite(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002177 OS << " break;\n";
2178 OS << " }\n";
2179 }
2180 OS << " }\n";
2181}
2182
Bob Wilson0058b822015-07-20 22:57:36 +00002183// Generate a conditional expression to check if the current target satisfies
2184// the conditions for a TargetSpecificAttr record, and append the code for
2185// those checks to the Test string. If the FnName string pointer is non-null,
2186// append a unique suffix to distinguish this set of target checks from other
2187// TargetSpecificAttr records.
2188static void GenerateTargetSpecificAttrChecks(const Record *R,
2189 std::vector<std::string> &Arches,
2190 std::string &Test,
2191 std::string *FnName) {
2192 // It is assumed that there will be an llvm::Triple object
2193 // named "T" and a TargetInfo object named "Target" within
2194 // scope that can be used to determine whether the attribute exists in
2195 // a given target.
2196 Test += "(";
2197
2198 for (auto I = Arches.begin(), E = Arches.end(); I != E; ++I) {
2199 std::string Part = *I;
2200 Test += "T.getArch() == llvm::Triple::" + Part;
2201 if (I + 1 != E)
2202 Test += " || ";
2203 if (FnName)
2204 *FnName += Part;
2205 }
2206 Test += ")";
2207
2208 // If the attribute is specific to particular OSes, check those.
2209 if (!R->isValueUnset("OSes")) {
2210 // We know that there was at least one arch test, so we need to and in the
2211 // OS tests.
2212 Test += " && (";
2213 std::vector<std::string> OSes = R->getValueAsListOfStrings("OSes");
2214 for (auto I = OSes.begin(), E = OSes.end(); I != E; ++I) {
2215 std::string Part = *I;
2216
2217 Test += "T.getOS() == llvm::Triple::" + Part;
2218 if (I + 1 != E)
2219 Test += " || ";
2220 if (FnName)
2221 *FnName += Part;
2222 }
2223 Test += ")";
2224 }
2225
2226 // If one or more CXX ABIs are specified, check those as well.
2227 if (!R->isValueUnset("CXXABIs")) {
2228 Test += " && (";
2229 std::vector<std::string> CXXABIs = R->getValueAsListOfStrings("CXXABIs");
2230 for (auto I = CXXABIs.begin(), E = CXXABIs.end(); I != E; ++I) {
2231 std::string Part = *I;
2232 Test += "Target.getCXXABI().getKind() == TargetCXXABI::" + Part;
2233 if (I + 1 != E)
2234 Test += " || ";
2235 if (FnName)
2236 *FnName += Part;
2237 }
2238 Test += ")";
2239 }
2240}
2241
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002242static void GenerateHasAttrSpellingStringSwitch(
2243 const std::vector<Record *> &Attrs, raw_ostream &OS,
2244 const std::string &Variety = "", const std::string &Scope = "") {
2245 for (const auto *Attr : Attrs) {
Aaron Ballmana0344c52014-11-14 13:44:02 +00002246 // C++11-style attributes have specific version information associated with
2247 // them. If the attribute has no scope, the version information must not
2248 // have the default value (1), as that's incorrect. Instead, the unscoped
2249 // attribute version information should be taken from the SD-6 standing
2250 // document, which can be found at:
2251 // https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations
2252 int Version = 1;
2253
2254 if (Variety == "CXX11") {
2255 std::vector<Record *> Spellings = Attr->getValueAsListOfDefs("Spellings");
2256 for (const auto &Spelling : Spellings) {
2257 if (Spelling->getValueAsString("Variety") == "CXX11") {
2258 Version = static_cast<int>(Spelling->getValueAsInt("Version"));
2259 if (Scope.empty() && Version == 1)
2260 PrintError(Spelling->getLoc(), "C++ standard attributes must "
2261 "have valid version information.");
2262 break;
2263 }
2264 }
2265 }
2266
Aaron Ballman0fa06d82014-01-09 22:57:44 +00002267 std::string Test;
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002268 if (Attr->isSubClassOf("TargetSpecificAttr")) {
2269 const Record *R = Attr->getValueAsDef("Target");
Aaron Ballman0fa06d82014-01-09 22:57:44 +00002270 std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches");
Hans Wennborgdcfba332015-10-06 23:40:43 +00002271 GenerateTargetSpecificAttrChecks(R, Arches, Test, nullptr);
Bob Wilson7c730832015-07-20 22:57:31 +00002272
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002273 // If this is the C++11 variety, also add in the LangOpts test.
2274 if (Variety == "CXX11")
2275 Test += " && LangOpts.CPlusPlus11";
2276 } else if (Variety == "CXX11")
2277 // C++11 mode should be checked against LangOpts, which is presumed to be
2278 // present in the caller.
2279 Test = "LangOpts.CPlusPlus11";
Aaron Ballman0fa06d82014-01-09 22:57:44 +00002280
Aaron Ballmana0344c52014-11-14 13:44:02 +00002281 std::string TestStr =
Aaron Ballman28afa182014-11-17 18:17:19 +00002282 !Test.empty() ? Test + " ? " + llvm::itostr(Version) + " : 0" : "1";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002283 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002284 for (const auto &S : Spellings)
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002285 if (Variety.empty() || (Variety == S.variety() &&
2286 (Scope.empty() || Scope == S.nameSpace())))
Aaron Ballmana0344c52014-11-14 13:44:02 +00002287 OS << " .Case(\"" << S.name() << "\", " << TestStr << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002288 }
Aaron Ballmana0344c52014-11-14 13:44:02 +00002289 OS << " .Default(0);\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002290}
2291
2292// Emits the list of spellings for attributes.
2293void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
2294 emitSourceFileHeader("Code to implement the __has_attribute logic", OS);
2295
2296 // Separate all of the attributes out into four group: generic, C++11, GNU,
2297 // and declspecs. Then generate a big switch statement for each of them.
2298 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Nico Weber20e08042016-09-03 02:55:10 +00002299 std::vector<Record *> Declspec, Microsoft, GNU, Pragma;
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002300 std::map<std::string, std::vector<Record *>> CXX;
2301
2302 // Walk over the list of all attributes, and split them out based on the
2303 // spelling variety.
2304 for (auto *R : Attrs) {
2305 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
2306 for (const auto &SI : Spellings) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00002307 const std::string &Variety = SI.variety();
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002308 if (Variety == "GNU")
2309 GNU.push_back(R);
2310 else if (Variety == "Declspec")
2311 Declspec.push_back(R);
Nico Weber20e08042016-09-03 02:55:10 +00002312 else if (Variety == "Microsoft")
2313 Microsoft.push_back(R);
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002314 else if (Variety == "CXX11")
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002315 CXX[SI.nameSpace()].push_back(R);
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002316 else if (Variety == "Pragma")
2317 Pragma.push_back(R);
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002318 }
2319 }
2320
Bob Wilson7c730832015-07-20 22:57:31 +00002321 OS << "const llvm::Triple &T = Target.getTriple();\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002322 OS << "switch (Syntax) {\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002323 OS << "case AttrSyntax::GNU:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002324 OS << " return llvm::StringSwitch<int>(Name)\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002325 GenerateHasAttrSpellingStringSwitch(GNU, OS, "GNU");
2326 OS << "case AttrSyntax::Declspec:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002327 OS << " return llvm::StringSwitch<int>(Name)\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002328 GenerateHasAttrSpellingStringSwitch(Declspec, OS, "Declspec");
Nico Weber20e08042016-09-03 02:55:10 +00002329 OS << "case AttrSyntax::Microsoft:\n";
2330 OS << " return llvm::StringSwitch<int>(Name)\n";
2331 GenerateHasAttrSpellingStringSwitch(Microsoft, OS, "Microsoft");
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002332 OS << "case AttrSyntax::Pragma:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002333 OS << " return llvm::StringSwitch<int>(Name)\n";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002334 GenerateHasAttrSpellingStringSwitch(Pragma, OS, "Pragma");
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002335 OS << "case AttrSyntax::CXX: {\n";
2336 // C++11-style attributes are further split out based on the Scope.
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002337 for (auto I = CXX.cbegin(), E = CXX.cend(); I != E; ++I) {
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002338 if (I != CXX.begin())
2339 OS << " else ";
2340 if (I->first.empty())
2341 OS << "if (!Scope || Scope->getName() == \"\") {\n";
2342 else
2343 OS << "if (Scope->getName() == \"" << I->first << "\") {\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002344 OS << " return llvm::StringSwitch<int>(Name)\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002345 GenerateHasAttrSpellingStringSwitch(I->second, OS, "CXX11", I->first);
2346 OS << "}";
2347 }
2348 OS << "\n}\n";
2349 OS << "}\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002350}
2351
Michael Han99315932013-01-24 16:46:58 +00002352void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002353 emitSourceFileHeader("Code to translate different attribute spellings "
2354 "into internal identifiers", OS);
Michael Han99315932013-01-24 16:46:58 +00002355
John McCall2225c8b2016-03-01 00:18:05 +00002356 OS << " switch (AttrKind) {\n";
Michael Han99315932013-01-24 16:46:58 +00002357
Aaron Ballman64e69862013-12-15 13:05:48 +00002358 ParsedAttrMap Attrs = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002359 for (const auto &I : Attrs) {
Aaron Ballman2f22b942014-05-20 19:47:14 +00002360 const Record &R = *I.second;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002361 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002362 OS << " case AT_" << I.first << ": {\n";
Richard Smith852e9ce2013-11-27 01:46:48 +00002363 for (unsigned I = 0; I < Spellings.size(); ++ I) {
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002364 OS << " if (Name == \"" << Spellings[I].name() << "\" && "
2365 << "SyntaxUsed == "
2366 << StringSwitch<unsigned>(Spellings[I].variety())
2367 .Case("GNU", 0)
2368 .Case("CXX11", 1)
2369 .Case("Declspec", 2)
Nico Weber20e08042016-09-03 02:55:10 +00002370 .Case("Microsoft", 3)
2371 .Case("Keyword", 4)
2372 .Case("Pragma", 5)
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002373 .Default(0)
2374 << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n"
2375 << " return " << I << ";\n";
Michael Han99315932013-01-24 16:46:58 +00002376 }
Richard Smith852e9ce2013-11-27 01:46:48 +00002377
2378 OS << " break;\n";
2379 OS << " }\n";
Michael Han99315932013-01-24 16:46:58 +00002380 }
2381
2382 OS << " }\n";
Aaron Ballman64e69862013-12-15 13:05:48 +00002383 OS << " return 0;\n";
Michael Han99315932013-01-24 16:46:58 +00002384}
2385
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002386// Emits code used by RecursiveASTVisitor to visit attributes
2387void EmitClangAttrASTVisitor(RecordKeeper &Records, raw_ostream &OS) {
2388 emitSourceFileHeader("Used by RecursiveASTVisitor to visit attributes.", OS);
2389
2390 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2391
2392 // Write method declarations for Traverse* methods.
2393 // We emit this here because we only generate methods for attributes that
2394 // are declared as ASTNodes.
2395 OS << "#ifdef ATTR_VISITOR_DECLS_ONLY\n\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002396 for (const auto *Attr : Attrs) {
2397 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002398 if (!R.getValueAsBit("ASTNode"))
2399 continue;
2400 OS << " bool Traverse"
2401 << R.getName() << "Attr(" << R.getName() << "Attr *A);\n";
2402 OS << " bool Visit"
2403 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
2404 << " return true; \n"
Hans Wennborg4afe5042015-07-22 20:46:26 +00002405 << " }\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002406 }
2407 OS << "\n#else // ATTR_VISITOR_DECLS_ONLY\n\n";
2408
2409 // Write individual Traverse* methods for each attribute class.
Aaron Ballman2f22b942014-05-20 19:47:14 +00002410 for (const auto *Attr : Attrs) {
2411 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002412 if (!R.getValueAsBit("ASTNode"))
2413 continue;
2414
2415 OS << "template <typename Derived>\n"
DeLesley Hutchinsbb79c332013-12-30 21:03:02 +00002416 << "bool VISITORCLASS<Derived>::Traverse"
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002417 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
2418 << " if (!getDerived().VisitAttr(A))\n"
2419 << " return false;\n"
2420 << " if (!getDerived().Visit" << R.getName() << "Attr(A))\n"
2421 << " return false;\n";
2422
2423 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman2f22b942014-05-20 19:47:14 +00002424 for (const auto *Arg : ArgRecords)
2425 createArgument(*Arg, R.getName())->writeASTVisitorTraversal(OS);
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002426
2427 OS << " return true;\n";
2428 OS << "}\n\n";
2429 }
2430
2431 // Write generic Traverse routine
2432 OS << "template <typename Derived>\n"
DeLesley Hutchinsbb79c332013-12-30 21:03:02 +00002433 << "bool VISITORCLASS<Derived>::TraverseAttr(Attr *A) {\n"
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002434 << " if (!A)\n"
2435 << " return true;\n"
2436 << "\n"
John McCall2225c8b2016-03-01 00:18:05 +00002437 << " switch (A->getKind()) {\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002438
Aaron Ballman2f22b942014-05-20 19:47:14 +00002439 for (const auto *Attr : Attrs) {
2440 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002441 if (!R.getValueAsBit("ASTNode"))
2442 continue;
2443
2444 OS << " case attr::" << R.getName() << ":\n"
2445 << " return getDerived().Traverse" << R.getName() << "Attr("
2446 << "cast<" << R.getName() << "Attr>(A));\n";
2447 }
John McCall5d7cf772016-03-01 02:09:20 +00002448 OS << " }\n"; // end switch
2449 OS << " llvm_unreachable(\"bad attribute kind\");\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00002450 OS << "}\n"; // end function
2451 OS << "#endif // ATTR_VISITOR_DECLS_ONLY\n";
2452}
2453
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002454// Emits code to instantiate dependent attributes on templates.
2455void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002456 emitSourceFileHeader("Template instantiation code for attributes", OS);
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002457
2458 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2459
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00002460 OS << "namespace clang {\n"
2461 << "namespace sema {\n\n"
2462 << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, "
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002463 << "Sema &S,\n"
2464 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n"
John McCall2225c8b2016-03-01 00:18:05 +00002465 << " switch (At->getKind()) {\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002466
Aaron Ballman2f22b942014-05-20 19:47:14 +00002467 for (const auto *Attr : Attrs) {
2468 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002469 if (!R.getValueAsBit("ASTNode"))
2470 continue;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002471
2472 OS << " case attr::" << R.getName() << ": {\n";
Rafael Espindola7f90b7d2012-05-15 14:09:55 +00002473 bool ShouldClone = R.getValueAsBit("Clone");
2474
2475 if (!ShouldClone) {
Hans Wennborg59dbe862015-09-29 20:56:43 +00002476 OS << " return nullptr;\n";
Rafael Espindola7f90b7d2012-05-15 14:09:55 +00002477 OS << " }\n";
2478 continue;
2479 }
2480
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002481 OS << " const auto *A = cast<"
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002482 << R.getName() << "Attr>(At);\n";
2483 bool TDependent = R.getValueAsBit("TemplateDependent");
2484
2485 if (!TDependent) {
2486 OS << " return A->clone(C);\n";
2487 OS << " }\n";
2488 continue;
2489 }
2490
2491 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002492 std::vector<std::unique_ptr<Argument>> Args;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002493 Args.reserve(ArgRecords.size());
2494
Aaron Ballman2f22b942014-05-20 19:47:14 +00002495 for (const auto *ArgRecord : ArgRecords)
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002496 Args.emplace_back(createArgument(*ArgRecord, R.getName()));
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002497
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002498 for (auto const &ai : Args)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002499 ai->writeTemplateInstantiation(OS);
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002500
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002501 OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002502 for (auto const &ai : Args) {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002503 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002504 ai->writeTemplateInstantiationArgs(OS);
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002505 }
Aaron Ballman36a53502014-01-16 13:03:14 +00002506 OS << ", A->getSpellingListIndex());\n }\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002507 }
2508 OS << " } // end switch\n"
2509 << " llvm_unreachable(\"Unknown attribute!\");\n"
Hans Wennborg59dbe862015-09-29 20:56:43 +00002510 << " return nullptr;\n"
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00002511 << "}\n\n"
2512 << "} // end namespace sema\n"
2513 << "} // end namespace clang\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00002514}
2515
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002516// Emits the list of parsed attributes.
2517void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) {
2518 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
2519
2520 OS << "#ifndef PARSED_ATTR\n";
2521 OS << "#define PARSED_ATTR(NAME) NAME\n";
2522 OS << "#endif\n\n";
2523
2524 ParsedAttrMap Names = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002525 for (const auto &I : Names) {
2526 OS << "PARSED_ATTR(" << I.first << ")\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002527 }
2528}
2529
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002530static bool isArgVariadic(const Record &R, StringRef AttrName) {
2531 return createArgument(R, AttrName)->isVariadic();
2532}
2533
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002534static void emitArgInfo(const Record &R, std::stringstream &OS) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002535 // This function will count the number of arguments specified for the
2536 // attribute and emit the number of required arguments followed by the
2537 // number of optional arguments.
2538 std::vector<Record *> Args = R.getValueAsListOfDefs("Args");
2539 unsigned ArgCount = 0, OptCount = 0;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002540 bool HasVariadic = false;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002541 for (const auto *Arg : Args) {
George Burgess IV8a36ace2016-12-01 17:52:39 +00002542 // If the arg is fake, it's the user's job to supply it: general parsing
2543 // logic shouldn't need to know anything about it.
2544 if (Arg->getValueAsBit("Fake"))
2545 continue;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002546 Arg->getValueAsBit("Optional") ? ++OptCount : ++ArgCount;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002547 if (!HasVariadic && isArgVariadic(*Arg, R.getName()))
2548 HasVariadic = true;
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002549 }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00002550
2551 // If there is a variadic argument, we will set the optional argument count
2552 // to its largest value. Since it's currently a 4-bit number, we set it to 15.
2553 OS << ArgCount << ", " << (HasVariadic ? 15 : OptCount);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002554}
2555
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002556static void GenerateDefaultAppertainsTo(raw_ostream &OS) {
Aaron Ballman93b5cc62013-12-02 19:36:42 +00002557 OS << "static bool defaultAppertainsTo(Sema &, const AttributeList &,";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002558 OS << "const Decl *) {\n";
2559 OS << " return true;\n";
2560 OS << "}\n\n";
2561}
2562
2563static std::string CalculateDiagnostic(const Record &S) {
2564 // If the SubjectList object has a custom diagnostic associated with it,
2565 // return that directly.
2566 std::string CustomDiag = S.getValueAsString("CustomDiag");
2567 if (!CustomDiag.empty())
2568 return CustomDiag;
2569
2570 // Given the list of subjects, determine what diagnostic best fits.
2571 enum {
2572 Func = 1U << 0,
2573 Var = 1U << 1,
2574 ObjCMethod = 1U << 2,
2575 Param = 1U << 3,
2576 Class = 1U << 4,
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00002577 GenericRecord = 1U << 5,
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002578 Type = 1U << 6,
2579 ObjCIVar = 1U << 7,
2580 ObjCProp = 1U << 8,
2581 ObjCInterface = 1U << 9,
2582 Block = 1U << 10,
2583 Namespace = 1U << 11,
Aaron Ballman981ba242014-05-20 14:10:53 +00002584 Field = 1U << 12,
2585 CXXMethod = 1U << 13,
Alexis Hunt724f14e2014-11-28 00:53:20 +00002586 ObjCProtocol = 1U << 14,
2587 Enum = 1U << 15
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002588 };
2589 uint32_t SubMask = 0;
2590
2591 std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects");
Aaron Ballman2f22b942014-05-20 19:47:14 +00002592 for (const auto *Subject : Subjects) {
2593 const Record &R = *Subject;
Aaron Ballman80469032013-11-29 14:57:58 +00002594 std::string Name;
2595
2596 if (R.isSubClassOf("SubsetSubject")) {
2597 PrintError(R.getLoc(), "SubsetSubjects should use a custom diagnostic");
2598 // As a fallback, look through the SubsetSubject to see what its base
2599 // type is, and use that. This needs to be updated if SubsetSubjects
2600 // are allowed within other SubsetSubjects.
2601 Name = R.getValueAsDef("Base")->getName();
2602 } else
2603 Name = R.getName();
2604
2605 uint32_t V = StringSwitch<uint32_t>(Name)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002606 .Case("Function", Func)
2607 .Case("Var", Var)
2608 .Case("ObjCMethod", ObjCMethod)
2609 .Case("ParmVar", Param)
2610 .Case("TypedefName", Type)
2611 .Case("ObjCIvar", ObjCIVar)
2612 .Case("ObjCProperty", ObjCProp)
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00002613 .Case("Record", GenericRecord)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002614 .Case("ObjCInterface", ObjCInterface)
Ted Kremenekd980da22013-12-10 19:43:42 +00002615 .Case("ObjCProtocol", ObjCProtocol)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002616 .Case("Block", Block)
2617 .Case("CXXRecord", Class)
2618 .Case("Namespace", Namespace)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002619 .Case("Field", Field)
2620 .Case("CXXMethod", CXXMethod)
Alexis Hunt724f14e2014-11-28 00:53:20 +00002621 .Case("Enum", Enum)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002622 .Default(0);
2623 if (!V) {
2624 // Something wasn't in our mapping, so be helpful and let the developer
2625 // know about it.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002626 PrintFatalError(R.getLoc(), "Unknown subject type: " + R.getName());
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002627 return "";
2628 }
2629
2630 SubMask |= V;
2631 }
2632
2633 switch (SubMask) {
2634 // For the simple cases where there's only a single entry in the mask, we
2635 // don't have to resort to bit fiddling.
2636 case Func: return "ExpectedFunction";
2637 case Var: return "ExpectedVariable";
2638 case Param: return "ExpectedParameter";
2639 case Class: return "ExpectedClass";
Alexis Hunt724f14e2014-11-28 00:53:20 +00002640 case Enum: return "ExpectedEnum";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002641 case CXXMethod:
2642 // FIXME: Currently, this maps to ExpectedMethod based on existing code,
2643 // but should map to something a bit more accurate at some point.
2644 case ObjCMethod: return "ExpectedMethod";
2645 case Type: return "ExpectedType";
2646 case ObjCInterface: return "ExpectedObjectiveCInterface";
Ted Kremenekd980da22013-12-10 19:43:42 +00002647 case ObjCProtocol: return "ExpectedObjectiveCProtocol";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002648
Aaron Ballmanc1494bd2013-11-27 20:14:30 +00002649 // "GenericRecord" means struct, union or class; check the language options
2650 // and if not compiling for C++, strip off the class part. Note that this
2651 // relies on the fact that the context for this declares "Sema &S".
2652 case GenericRecord:
Aaron Ballman17046b82013-11-27 19:16:55 +00002653 return "(S.getLangOpts().CPlusPlus ? ExpectedStructOrUnionOrClass : "
2654 "ExpectedStructOrUnion)";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002655 case Func | ObjCMethod | Block: return "ExpectedFunctionMethodOrBlock";
2656 case Func | ObjCMethod | Class: return "ExpectedFunctionMethodOrClass";
2657 case Func | Param:
2658 case Func | ObjCMethod | Param: return "ExpectedFunctionMethodOrParameter";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002659 case Func | ObjCMethod: return "ExpectedFunctionOrMethod";
2660 case Func | Var: return "ExpectedVariableOrFunction";
Aaron Ballman604dfec2013-12-02 17:07:07 +00002661
2662 // If not compiling for C++, the class portion does not apply.
2663 case Func | Var | Class:
2664 return "(S.getLangOpts().CPlusPlus ? ExpectedFunctionVariableOrClass : "
2665 "ExpectedVariableOrFunction)";
2666
Saleem Abdulrasool511f2e52016-07-15 20:41:10 +00002667 case Func | Var | Class | ObjCInterface:
2668 return "(S.getLangOpts().CPlusPlus"
2669 " ? ((S.getLangOpts().ObjC1 || S.getLangOpts().ObjC2)"
2670 " ? ExpectedFunctionVariableClassOrObjCInterface"
2671 " : ExpectedFunctionVariableOrClass)"
2672 " : ((S.getLangOpts().ObjC1 || S.getLangOpts().ObjC2)"
2673 " ? ExpectedFunctionVariableOrObjCInterface"
2674 " : ExpectedVariableOrFunction))";
2675
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002676 case ObjCMethod | ObjCProp: return "ExpectedMethodOrProperty";
Aaron Ballman173361e2014-07-16 20:28:10 +00002677 case ObjCProtocol | ObjCInterface:
2678 return "ExpectedObjectiveCInterfaceOrProtocol";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002679 case Field | Var: return "ExpectedFieldOrGlobalVar";
2680 }
2681
2682 PrintFatalError(S.getLoc(),
2683 "Could not deduce diagnostic argument for Attr subjects");
2684
2685 return "";
2686}
2687
Aaron Ballman12b9f652014-01-16 13:55:42 +00002688static std::string GetSubjectWithSuffix(const Record *R) {
George Burgess IV1881a572016-12-01 00:13:18 +00002689 const std::string &B = R->getName();
Aaron Ballman12b9f652014-01-16 13:55:42 +00002690 if (B == "DeclBase")
2691 return "Decl";
2692 return B + "Decl";
2693}
Hans Wennborgdcfba332015-10-06 23:40:43 +00002694
Aaron Ballman80469032013-11-29 14:57:58 +00002695static std::string GenerateCustomAppertainsTo(const Record &Subject,
2696 raw_ostream &OS) {
Matthias Braunbbbf5d42016-12-04 05:55:09 +00002697 std::string FnName = "is" + Subject.getName().str();
Aaron Ballmana358c902013-12-02 14:58:17 +00002698
Aaron Ballman80469032013-11-29 14:57:58 +00002699 // If this code has already been generated, simply return the previous
2700 // instance of it.
2701 static std::set<std::string> CustomSubjectSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002702 auto I = CustomSubjectSet.find(FnName);
Aaron Ballman80469032013-11-29 14:57:58 +00002703 if (I != CustomSubjectSet.end())
2704 return *I;
2705
2706 Record *Base = Subject.getValueAsDef("Base");
2707
2708 // Not currently support custom subjects within custom subjects.
2709 if (Base->isSubClassOf("SubsetSubject")) {
2710 PrintFatalError(Subject.getLoc(),
2711 "SubsetSubjects within SubsetSubjects is not supported");
2712 return "";
2713 }
2714
Aaron Ballman80469032013-11-29 14:57:58 +00002715 OS << "static bool " << FnName << "(const Decl *D) {\n";
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002716 OS << " if (const auto *S = dyn_cast<";
Aaron Ballman12b9f652014-01-16 13:55:42 +00002717 OS << GetSubjectWithSuffix(Base);
Aaron Ballman47553042014-01-16 14:32:03 +00002718 OS << ">(D))\n";
2719 OS << " return " << Subject.getValueAsString("CheckCode") << ";\n";
2720 OS << " return false;\n";
Aaron Ballman80469032013-11-29 14:57:58 +00002721 OS << "}\n\n";
2722
2723 CustomSubjectSet.insert(FnName);
2724 return FnName;
2725}
2726
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002727static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) {
2728 // If the attribute does not contain a Subjects definition, then use the
2729 // default appertainsTo logic.
2730 if (Attr.isValueUnset("Subjects"))
Aaron Ballman93b5cc62013-12-02 19:36:42 +00002731 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002732
2733 const Record *SubjectObj = Attr.getValueAsDef("Subjects");
2734 std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
2735
2736 // If the list of subjects is empty, it is assumed that the attribute
2737 // appertains to everything.
2738 if (Subjects.empty())
Aaron Ballman93b5cc62013-12-02 19:36:42 +00002739 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002740
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002741 bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn");
2742
2743 // Otherwise, generate an appertainsTo check specific to this attribute which
2744 // checks all of the given subjects against the Decl passed in. Return the
2745 // name of that check to the caller.
Matthias Braunbbbf5d42016-12-04 05:55:09 +00002746 std::string FnName = "check" + Attr.getName().str() + "AppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002747 std::stringstream SS;
2748 SS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr, ";
2749 SS << "const Decl *D) {\n";
2750 SS << " if (";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002751 for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) {
Aaron Ballman80469032013-11-29 14:57:58 +00002752 // If the subject has custom code associated with it, generate a function
2753 // for it. The function cannot be inlined into this check (yet) because it
2754 // requires the subject to be of a specific type, and were that information
2755 // inlined here, it would not support an attribute with multiple custom
2756 // subjects.
2757 if ((*I)->isSubClassOf("SubsetSubject")) {
2758 SS << "!" << GenerateCustomAppertainsTo(**I, OS) << "(D)";
2759 } else {
Aaron Ballman12b9f652014-01-16 13:55:42 +00002760 SS << "!isa<" << GetSubjectWithSuffix(*I) << ">(D)";
Aaron Ballman80469032013-11-29 14:57:58 +00002761 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002762
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002763 if (I + 1 != E)
2764 SS << " && ";
2765 }
2766 SS << ") {\n";
2767 SS << " S.Diag(Attr.getLoc(), diag::";
2768 SS << (Warn ? "warn_attribute_wrong_decl_type" :
2769 "err_attribute_wrong_decl_type");
2770 SS << ")\n";
2771 SS << " << Attr.getName() << ";
2772 SS << CalculateDiagnostic(*SubjectObj) << ";\n";
2773 SS << " return false;\n";
2774 SS << " }\n";
2775 SS << " return true;\n";
2776 SS << "}\n\n";
2777
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002778 OS << SS.str();
2779 return FnName;
2780}
2781
Aaron Ballman3aff6332013-12-02 19:30:36 +00002782static void GenerateDefaultLangOptRequirements(raw_ostream &OS) {
2783 OS << "static bool defaultDiagnoseLangOpts(Sema &, ";
2784 OS << "const AttributeList &) {\n";
2785 OS << " return true;\n";
2786 OS << "}\n\n";
2787}
2788
2789static std::string GenerateLangOptRequirements(const Record &R,
2790 raw_ostream &OS) {
2791 // If the attribute has an empty or unset list of language requirements,
2792 // return the default handler.
2793 std::vector<Record *> LangOpts = R.getValueAsListOfDefs("LangOpts");
2794 if (LangOpts.empty())
2795 return "defaultDiagnoseLangOpts";
2796
2797 // Generate the test condition, as well as a unique function name for the
2798 // diagnostic test. The list of options should usually be short (one or two
2799 // options), and the uniqueness isn't strictly necessary (it is just for
2800 // codegen efficiency).
2801 std::string FnName = "check", Test;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002802 for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) {
Aaron Ballman3aff6332013-12-02 19:30:36 +00002803 std::string Part = (*I)->getValueAsString("Name");
Eric Fiselier341e8252016-09-02 18:53:31 +00002804 if ((*I)->getValueAsBit("Negated")) {
2805 FnName += "Not";
Alexis Hunt724f14e2014-11-28 00:53:20 +00002806 Test += "!";
Eric Fiselier341e8252016-09-02 18:53:31 +00002807 }
Aaron Ballman3aff6332013-12-02 19:30:36 +00002808 Test += "S.LangOpts." + Part;
2809 if (I + 1 != E)
2810 Test += " || ";
2811 FnName += Part;
2812 }
2813 FnName += "LangOpts";
2814
2815 // If this code has already been generated, simply return the previous
2816 // instance of it.
2817 static std::set<std::string> CustomLangOptsSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002818 auto I = CustomLangOptsSet.find(FnName);
Aaron Ballman3aff6332013-12-02 19:30:36 +00002819 if (I != CustomLangOptsSet.end())
2820 return *I;
2821
2822 OS << "static bool " << FnName << "(Sema &S, const AttributeList &Attr) {\n";
2823 OS << " if (" << Test << ")\n";
2824 OS << " return true;\n\n";
2825 OS << " S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) ";
2826 OS << "<< Attr.getName();\n";
2827 OS << " return false;\n";
2828 OS << "}\n\n";
2829
2830 CustomLangOptsSet.insert(FnName);
2831 return FnName;
2832}
2833
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002834static void GenerateDefaultTargetRequirements(raw_ostream &OS) {
Bob Wilson7c730832015-07-20 22:57:31 +00002835 OS << "static bool defaultTargetRequirements(const TargetInfo &) {\n";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002836 OS << " return true;\n";
2837 OS << "}\n\n";
2838}
2839
2840static std::string GenerateTargetRequirements(const Record &Attr,
2841 const ParsedAttrMap &Dupes,
2842 raw_ostream &OS) {
2843 // If the attribute is not a target specific attribute, return the default
2844 // target handler.
2845 if (!Attr.isSubClassOf("TargetSpecificAttr"))
2846 return "defaultTargetRequirements";
2847
2848 // Get the list of architectures to be tested for.
2849 const Record *R = Attr.getValueAsDef("Target");
2850 std::vector<std::string> Arches = R->getValueAsListOfStrings("Arches");
2851 if (Arches.empty()) {
2852 PrintError(Attr.getLoc(), "Empty list of target architectures for a "
2853 "target-specific attr");
2854 return "defaultTargetRequirements";
2855 }
2856
2857 // If there are other attributes which share the same parsed attribute kind,
2858 // such as target-specific attributes with a shared spelling, collapse the
2859 // duplicate architectures. This is required because a shared target-specific
2860 // attribute has only one AttributeList::Kind enumeration value, but it
2861 // applies to multiple target architectures. In order for the attribute to be
2862 // considered valid, all of its architectures need to be included.
2863 if (!Attr.isValueUnset("ParseKind")) {
2864 std::string APK = Attr.getValueAsString("ParseKind");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002865 for (const auto &I : Dupes) {
2866 if (I.first == APK) {
2867 std::vector<std::string> DA = I.second->getValueAsDef("Target")
2868 ->getValueAsListOfStrings("Arches");
George Burgess IV1881a572016-12-01 00:13:18 +00002869 std::move(DA.begin(), DA.end(), std::back_inserter(Arches));
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002870 }
2871 }
2872 }
2873
Bob Wilson0058b822015-07-20 22:57:36 +00002874 std::string FnName = "isTarget";
2875 std::string Test;
2876 GenerateTargetSpecificAttrChecks(R, Arches, Test, &FnName);
Bob Wilson7c730832015-07-20 22:57:31 +00002877
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002878 // If this code has already been generated, simply return the previous
2879 // instance of it.
2880 static std::set<std::string> CustomTargetSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002881 auto I = CustomTargetSet.find(FnName);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002882 if (I != CustomTargetSet.end())
2883 return *I;
2884
Bob Wilson7c730832015-07-20 22:57:31 +00002885 OS << "static bool " << FnName << "(const TargetInfo &Target) {\n";
2886 OS << " const llvm::Triple &T = Target.getTriple();\n";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002887 OS << " return " << Test << ";\n";
2888 OS << "}\n\n";
2889
2890 CustomTargetSet.insert(FnName);
2891 return FnName;
2892}
2893
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002894static void GenerateDefaultSpellingIndexToSemanticSpelling(raw_ostream &OS) {
2895 OS << "static unsigned defaultSpellingIndexToSemanticSpelling("
2896 << "const AttributeList &Attr) {\n";
2897 OS << " return UINT_MAX;\n";
2898 OS << "}\n\n";
2899}
2900
2901static std::string GenerateSpellingIndexToSemanticSpelling(const Record &Attr,
2902 raw_ostream &OS) {
2903 // If the attribute does not have a semantic form, we can bail out early.
2904 if (!Attr.getValueAsBit("ASTNode"))
2905 return "defaultSpellingIndexToSemanticSpelling";
2906
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002907 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002908
2909 // If there are zero or one spellings, or all of the spellings share the same
2910 // name, we can also bail out early.
2911 if (Spellings.size() <= 1 || SpellingNamesAreCommon(Spellings))
2912 return "defaultSpellingIndexToSemanticSpelling";
2913
2914 // Generate the enumeration we will use for the mapping.
2915 SemanticSpellingMap SemanticToSyntacticMap;
2916 std::string Enum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
Matthias Braunbbbf5d42016-12-04 05:55:09 +00002917 std::string Name = Attr.getName().str() + "AttrSpellingMap";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002918
2919 OS << "static unsigned " << Name << "(const AttributeList &Attr) {\n";
2920 OS << Enum;
2921 OS << " unsigned Idx = Attr.getAttributeSpellingListIndex();\n";
2922 WriteSemanticSpellingSwitch("Idx", SemanticToSyntacticMap, OS);
2923 OS << "}\n\n";
2924
2925 return Name;
2926}
2927
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002928static bool IsKnownToGCC(const Record &Attr) {
2929 // Look at the spellings for this subject; if there are any spellings which
2930 // claim to be known to GCC, the attribute is known to GCC.
George Burgess IV1881a572016-12-01 00:13:18 +00002931 return llvm::any_of(
2932 GetFlattenedSpellings(Attr),
2933 [](const FlattenedSpelling &S) { return S.knownToGCC(); });
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00002934}
2935
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002936/// Emits the parsed attribute helpers
2937void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
2938 emitSourceFileHeader("Parsed attribute helpers", OS);
2939
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002940 // Get the list of parsed attributes, and accept the optional list of
2941 // duplicates due to the ParseKind.
2942 ParsedAttrMap Dupes;
2943 ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002944
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002945 // Generate the default appertainsTo, target and language option diagnostic,
2946 // and spelling list index mapping methods.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002947 GenerateDefaultAppertainsTo(OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00002948 GenerateDefaultLangOptRequirements(OS);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002949 GenerateDefaultTargetRequirements(OS);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002950 GenerateDefaultSpellingIndexToSemanticSpelling(OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002951
2952 // Generate the appertainsTo diagnostic methods and write their names into
2953 // another mapping. At the same time, generate the AttrInfoMap object
2954 // contents. Due to the reliance on generated code, use separate streams so
2955 // that code will not be interleaved.
2956 std::stringstream SS;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002957 for (auto I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002958 // TODO: If the attribute's kind appears in the list of duplicates, that is
2959 // because it is a target-specific attribute that appears multiple times.
2960 // It would be beneficial to test whether the duplicates are "similar
2961 // enough" to each other to not cause problems. For instance, check that
Alp Toker96cf7582014-01-18 21:49:37 +00002962 // the spellings are identical, and custom parsing rules match, etc.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002963
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002964 // We need to generate struct instances based off ParsedAttrInfo from
2965 // AttributeList.cpp.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002966 SS << " { ";
2967 emitArgInfo(*I->second, SS);
2968 SS << ", " << I->second->getValueAsBit("HasCustomParsing");
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002969 SS << ", " << I->second->isSubClassOf("TargetSpecificAttr");
2970 SS << ", " << I->second->isSubClassOf("TypeAttr");
Richard Smith4f902c72016-03-08 00:32:55 +00002971 SS << ", " << I->second->isSubClassOf("StmtAttr");
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002972 SS << ", " << IsKnownToGCC(*I->second);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002973 SS << ", " << GenerateAppertainsTo(*I->second, OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00002974 SS << ", " << GenerateLangOptRequirements(*I->second, OS);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00002975 SS << ", " << GenerateTargetRequirements(*I->second, Dupes, OS);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002976 SS << ", " << GenerateSpellingIndexToSemanticSpelling(*I->second, OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002977 SS << " }";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002978
2979 if (I + 1 != E)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002980 SS << ",";
2981
2982 SS << " // AT_" << I->first << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002983 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00002984
2985 OS << "static const ParsedAttrInfo AttrInfoMap[AttributeList::UnknownAttribute + 1] = {\n";
2986 OS << SS.str();
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002987 OS << "};\n\n";
Michael Han4a045172012-03-07 00:12:16 +00002988}
2989
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002990// Emits the kind list of parsed attributes
2991void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002992 emitSourceFileHeader("Attribute name matcher", OS);
2993
Aaron Ballman09e98ff2014-01-13 21:42:39 +00002994 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Nico Weber20e08042016-09-03 02:55:10 +00002995 std::vector<StringMatcher::StringPair> GNU, Declspec, Microsoft, CXX11,
2996 Keywords, Pragma;
Aaron Ballman64e69862013-12-15 13:05:48 +00002997 std::set<std::string> Seen;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002998 for (const auto *A : Attrs) {
2999 const Record &Attr = *A;
Richard Smith852e9ce2013-11-27 01:46:48 +00003000
Michael Han4a045172012-03-07 00:12:16 +00003001 bool SemaHandler = Attr.getValueAsBit("SemaHandler");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003002 bool Ignored = Attr.getValueAsBit("Ignored");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003003 if (SemaHandler || Ignored) {
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003004 // Attribute spellings can be shared between target-specific attributes,
3005 // and can be shared between syntaxes for the same attribute. For
3006 // instance, an attribute can be spelled GNU<"interrupt"> for an ARM-
3007 // specific attribute, or MSP430-specific attribute. Additionally, an
3008 // attribute can be spelled GNU<"dllexport"> and Declspec<"dllexport">
3009 // for the same semantic attribute. Ultimately, we need to map each of
3010 // these to a single AttributeList::Kind value, but the StringMatcher
3011 // class cannot handle duplicate match strings. So we generate a list of
3012 // string to match based on the syntax, and emit multiple string matchers
3013 // depending on the syntax used.
Aaron Ballman64e69862013-12-15 13:05:48 +00003014 std::string AttrName;
3015 if (Attr.isSubClassOf("TargetSpecificAttr") &&
3016 !Attr.isValueUnset("ParseKind")) {
3017 AttrName = Attr.getValueAsString("ParseKind");
3018 if (Seen.find(AttrName) != Seen.end())
3019 continue;
3020 Seen.insert(AttrName);
3021 } else
3022 AttrName = NormalizeAttrName(StringRef(Attr.getName())).str();
3023
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003024 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003025 for (const auto &S : Spellings) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00003026 const std::string &RawSpelling = S.name();
Craig Topper8ae12032014-05-07 06:21:57 +00003027 std::vector<StringMatcher::StringPair> *Matches = nullptr;
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00003028 std::string Spelling;
3029 const std::string &Variety = S.variety();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003030 if (Variety == "CXX11") {
3031 Matches = &CXX11;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003032 Spelling += S.nameSpace();
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003033 Spelling += "::";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003034 } else if (Variety == "GNU")
3035 Matches = &GNU;
3036 else if (Variety == "Declspec")
3037 Matches = &Declspec;
Nico Weber20e08042016-09-03 02:55:10 +00003038 else if (Variety == "Microsoft")
3039 Matches = &Microsoft;
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003040 else if (Variety == "Keyword")
3041 Matches = &Keywords;
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003042 else if (Variety == "Pragma")
3043 Matches = &Pragma;
Alexis Hunta0e54d42012-06-18 16:13:52 +00003044
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003045 assert(Matches && "Unsupported spelling variety found");
3046
Justin Lebar4086fe52017-01-05 16:51:54 +00003047 if (Variety == "GNU")
3048 Spelling += NormalizeGNUAttrSpelling(RawSpelling);
3049 else
3050 Spelling += RawSpelling;
3051
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003052 if (SemaHandler)
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003053 Matches->push_back(StringMatcher::StringPair(Spelling,
3054 "return AttributeList::AT_" + AttrName + ";"));
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003055 else
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003056 Matches->push_back(StringMatcher::StringPair(Spelling,
3057 "return AttributeList::IgnoredAttribute;"));
Michael Han4a045172012-03-07 00:12:16 +00003058 }
3059 }
3060 }
Douglas Gregor377f99b2012-05-02 17:33:51 +00003061
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003062 OS << "static AttributeList::Kind getAttrKind(StringRef Name, ";
3063 OS << "AttributeList::Syntax Syntax) {\n";
3064 OS << " if (AttributeList::AS_GNU == Syntax) {\n";
3065 StringMatcher("Name", GNU, OS).Emit();
3066 OS << " } else if (AttributeList::AS_Declspec == Syntax) {\n";
3067 StringMatcher("Name", Declspec, OS).Emit();
Nico Weber20e08042016-09-03 02:55:10 +00003068 OS << " } else if (AttributeList::AS_Microsoft == Syntax) {\n";
3069 StringMatcher("Name", Microsoft, OS).Emit();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003070 OS << " } else if (AttributeList::AS_CXX11 == Syntax) {\n";
3071 StringMatcher("Name", CXX11, OS).Emit();
Douglas Gregorbec595a2015-06-19 18:27:45 +00003072 OS << " } else if (AttributeList::AS_Keyword == Syntax || ";
3073 OS << "AttributeList::AS_ContextSensitiveKeyword == Syntax) {\n";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003074 StringMatcher("Name", Keywords, OS).Emit();
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003075 OS << " } else if (AttributeList::AS_Pragma == Syntax) {\n";
3076 StringMatcher("Name", Pragma, OS).Emit();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003077 OS << " }\n";
3078 OS << " return AttributeList::UnknownAttribute;\n"
Douglas Gregor377f99b2012-05-02 17:33:51 +00003079 << "}\n";
Michael Han4a045172012-03-07 00:12:16 +00003080}
3081
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003082// Emits the code to dump an attribute.
3083void EmitClangAttrDump(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00003084 emitSourceFileHeader("Attribute dumper", OS);
3085
John McCall2225c8b2016-03-01 00:18:05 +00003086 OS << " switch (A->getKind()) {\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003087 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003088 for (const auto *Attr : Attrs) {
3089 const Record &R = *Attr;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003090 if (!R.getValueAsBit("ASTNode"))
3091 continue;
3092 OS << " case attr::" << R.getName() << ": {\n";
Aaron Ballmanbc909612014-01-22 21:51:20 +00003093
3094 // If the attribute has a semantically-meaningful name (which is determined
3095 // by whether there is a Spelling enumeration for it), then write out the
3096 // spelling used for the attribute.
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003097 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballmanbc909612014-01-22 21:51:20 +00003098 if (Spellings.size() > 1 && !SpellingNamesAreCommon(Spellings))
3099 OS << " OS << \" \" << A->getSpelling();\n";
3100
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003101 Args = R.getValueAsListOfDefs("Args");
3102 if (!Args.empty()) {
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003103 OS << " const auto *SA = cast<" << R.getName()
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003104 << "Attr>(A);\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00003105 for (const auto *Arg : Args)
3106 createArgument(*Arg, R.getName())->writeDump(OS);
Richard Trieude5cc7d2013-01-31 01:44:26 +00003107
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003108 for (const auto *AI : Args)
3109 createArgument(*AI, R.getName())->writeDumpChildren(OS);
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003110 }
3111 OS <<
3112 " break;\n"
3113 " }\n";
3114 }
3115 OS << " }\n";
3116}
3117
Aaron Ballman35db2b32014-01-29 22:13:45 +00003118void EmitClangAttrParserStringSwitches(RecordKeeper &Records,
3119 raw_ostream &OS) {
3120 emitSourceFileHeader("Parser-related llvm::StringSwitch cases", OS);
3121 emitClangAttrArgContextList(Records, OS);
3122 emitClangAttrIdentifierArgList(Records, OS);
3123 emitClangAttrTypeArgList(Records, OS);
3124 emitClangAttrLateParsedList(Records, OS);
3125}
3126
Aaron Ballman97dba042014-02-17 15:27:10 +00003127class DocumentationData {
3128public:
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003129 const Record *Documentation;
3130 const Record *Attribute;
Aaron Ballman97dba042014-02-17 15:27:10 +00003131
Aaron Ballman4de1b582014-02-19 22:59:32 +00003132 DocumentationData(const Record &Documentation, const Record &Attribute)
3133 : Documentation(&Documentation), Attribute(&Attribute) {}
Aaron Ballman97dba042014-02-17 15:27:10 +00003134};
3135
Aaron Ballman4de1b582014-02-19 22:59:32 +00003136static void WriteCategoryHeader(const Record *DocCategory,
Aaron Ballman97dba042014-02-17 15:27:10 +00003137 raw_ostream &OS) {
Aaron Ballman4de1b582014-02-19 22:59:32 +00003138 const std::string &Name = DocCategory->getValueAsString("Name");
3139 OS << Name << "\n" << std::string(Name.length(), '=') << "\n";
3140
3141 // If there is content, print that as well.
3142 std::string ContentStr = DocCategory->getValueAsString("Content");
Benjamin Kramer5c404072015-04-10 21:37:21 +00003143 // Trim leading and trailing newlines and spaces.
3144 OS << StringRef(ContentStr).trim();
3145
Aaron Ballman4de1b582014-02-19 22:59:32 +00003146 OS << "\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003147}
3148
Aaron Ballmana66b5742014-02-17 15:36:08 +00003149enum SpellingKind {
3150 GNU = 1 << 0,
3151 CXX11 = 1 << 1,
3152 Declspec = 1 << 2,
Nico Weber20e08042016-09-03 02:55:10 +00003153 Microsoft = 1 << 3,
3154 Keyword = 1 << 4,
3155 Pragma = 1 << 5
Aaron Ballmana66b5742014-02-17 15:36:08 +00003156};
3157
Aaron Ballman97dba042014-02-17 15:27:10 +00003158static void WriteDocumentation(const DocumentationData &Doc,
3159 raw_ostream &OS) {
3160 // FIXME: there is no way to have a per-spelling category for the attribute
3161 // documentation. This may not be a limiting factor since the spellings
3162 // should generally be consistently applied across the category.
3163
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003164 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Doc.Attribute);
Aaron Ballman97dba042014-02-17 15:27:10 +00003165
3166 // Determine the heading to be used for this attribute.
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003167 std::string Heading = Doc.Documentation->getValueAsString("Heading");
Aaron Ballmanea6668c2014-02-21 14:14:04 +00003168 bool CustomHeading = !Heading.empty();
Aaron Ballman97dba042014-02-17 15:27:10 +00003169 if (Heading.empty()) {
3170 // If there's only one spelling, we can simply use that.
3171 if (Spellings.size() == 1)
3172 Heading = Spellings.begin()->name();
3173 else {
3174 std::set<std::string> Uniques;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003175 for (auto I = Spellings.begin(), E = Spellings.end();
3176 I != E && Uniques.size() <= 1; ++I) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003177 std::string Spelling = NormalizeNameForSpellingComparison(I->name());
3178 Uniques.insert(Spelling);
3179 }
3180 // If the semantic map has only one spelling, that is sufficient for our
3181 // needs.
3182 if (Uniques.size() == 1)
3183 Heading = *Uniques.begin();
3184 }
3185 }
3186
3187 // If the heading is still empty, it is an error.
3188 if (Heading.empty())
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003189 PrintFatalError(Doc.Attribute->getLoc(),
Aaron Ballman97dba042014-02-17 15:27:10 +00003190 "This attribute requires a heading to be specified");
3191
3192 // Gather a list of unique spellings; this is not the same as the semantic
3193 // spelling for the attribute. Variations in underscores and other non-
3194 // semantic characters are still acceptable.
3195 std::vector<std::string> Names;
3196
Aaron Ballman97dba042014-02-17 15:27:10 +00003197 unsigned SupportedSpellings = 0;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003198 for (const auto &I : Spellings) {
3199 SpellingKind Kind = StringSwitch<SpellingKind>(I.variety())
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003200 .Case("GNU", GNU)
3201 .Case("CXX11", CXX11)
3202 .Case("Declspec", Declspec)
Nico Weber20e08042016-09-03 02:55:10 +00003203 .Case("Microsoft", Microsoft)
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003204 .Case("Keyword", Keyword)
3205 .Case("Pragma", Pragma);
Aaron Ballman97dba042014-02-17 15:27:10 +00003206
3207 // Mask in the supported spelling.
3208 SupportedSpellings |= Kind;
3209
3210 std::string Name;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003211 if (Kind == CXX11 && !I.nameSpace().empty())
3212 Name = I.nameSpace() + "::";
3213 Name += I.name();
Aaron Ballman97dba042014-02-17 15:27:10 +00003214
3215 // If this name is the same as the heading, do not add it.
3216 if (Name != Heading)
3217 Names.push_back(Name);
3218 }
3219
3220 // Print out the heading for the attribute. If there are alternate spellings,
3221 // then display those after the heading.
Aaron Ballmanea6668c2014-02-21 14:14:04 +00003222 if (!CustomHeading && !Names.empty()) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003223 Heading += " (";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003224 for (auto I = Names.begin(), E = Names.end(); I != E; ++I) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003225 if (I != Names.begin())
3226 Heading += ", ";
3227 Heading += *I;
3228 }
3229 Heading += ")";
3230 }
3231 OS << Heading << "\n" << std::string(Heading.length(), '-') << "\n";
3232
3233 if (!SupportedSpellings)
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003234 PrintFatalError(Doc.Attribute->getLoc(),
Aaron Ballman97dba042014-02-17 15:27:10 +00003235 "Attribute has no supported spellings; cannot be "
3236 "documented");
3237
3238 // List what spelling syntaxes the attribute supports.
3239 OS << ".. csv-table:: Supported Syntaxes\n";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003240 OS << " :header: \"GNU\", \"C++11\", \"__declspec\", \"Keyword\",";
3241 OS << " \"Pragma\"\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003242 OS << " \"";
3243 if (SupportedSpellings & GNU) OS << "X";
3244 OS << "\",\"";
3245 if (SupportedSpellings & CXX11) OS << "X";
3246 OS << "\",\"";
3247 if (SupportedSpellings & Declspec) OS << "X";
3248 OS << "\",\"";
3249 if (SupportedSpellings & Keyword) OS << "X";
Aaron Ballman120c79f2014-06-25 12:48:06 +00003250 OS << "\", \"";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003251 if (SupportedSpellings & Pragma) OS << "X";
3252 OS << "\"\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003253
3254 // If the attribute is deprecated, print a message about it, and possibly
3255 // provide a replacement attribute.
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003256 if (!Doc.Documentation->isValueUnset("Deprecated")) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003257 OS << "This attribute has been deprecated, and may be removed in a future "
3258 << "version of Clang.";
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003259 const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated");
Aaron Ballman97dba042014-02-17 15:27:10 +00003260 std::string Replacement = Deprecated.getValueAsString("Replacement");
3261 if (!Replacement.empty())
3262 OS << " This attribute has been superseded by ``"
3263 << Replacement << "``.";
3264 OS << "\n\n";
3265 }
3266
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003267 std::string ContentStr = Doc.Documentation->getValueAsString("Content");
Aaron Ballman97dba042014-02-17 15:27:10 +00003268 // Trim leading and trailing newlines and spaces.
Benjamin Kramer5c404072015-04-10 21:37:21 +00003269 OS << StringRef(ContentStr).trim();
Aaron Ballman97dba042014-02-17 15:27:10 +00003270
3271 OS << "\n\n\n";
3272}
3273
3274void EmitClangAttrDocs(RecordKeeper &Records, raw_ostream &OS) {
3275 // Get the documentation introduction paragraph.
3276 const Record *Documentation = Records.getDef("GlobalDocumentation");
3277 if (!Documentation) {
3278 PrintFatalError("The Documentation top-level definition is missing, "
3279 "no documentation will be generated.");
3280 return;
3281 }
3282
Aaron Ballman4de1b582014-02-19 22:59:32 +00003283 OS << Documentation->getValueAsString("Intro") << "\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003284
Aaron Ballman97dba042014-02-17 15:27:10 +00003285 // Gather the Documentation lists from each of the attributes, based on the
3286 // category provided.
3287 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003288 std::map<const Record *, std::vector<DocumentationData>> SplitDocs;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003289 for (const auto *A : Attrs) {
3290 const Record &Attr = *A;
Aaron Ballman97dba042014-02-17 15:27:10 +00003291 std::vector<Record *> Docs = Attr.getValueAsListOfDefs("Documentation");
Aaron Ballman2f22b942014-05-20 19:47:14 +00003292 for (const auto *D : Docs) {
3293 const Record &Doc = *D;
Aaron Ballman4de1b582014-02-19 22:59:32 +00003294 const Record *Category = Doc.getValueAsDef("Category");
Aaron Ballman97dba042014-02-17 15:27:10 +00003295 // If the category is "undocumented", then there cannot be any other
3296 // documentation categories (otherwise, the attribute would become
3297 // documented).
Aaron Ballman4de1b582014-02-19 22:59:32 +00003298 std::string Cat = Category->getValueAsString("Name");
3299 bool Undocumented = Cat == "Undocumented";
Aaron Ballman97dba042014-02-17 15:27:10 +00003300 if (Undocumented && Docs.size() > 1)
3301 PrintFatalError(Doc.getLoc(),
3302 "Attribute is \"Undocumented\", but has multiple "
3303 "documentation categories");
3304
3305 if (!Undocumented)
Aaron Ballman4de1b582014-02-19 22:59:32 +00003306 SplitDocs[Category].push_back(DocumentationData(Doc, Attr));
Aaron Ballman97dba042014-02-17 15:27:10 +00003307 }
3308 }
3309
3310 // Having split the attributes out based on what documentation goes where,
3311 // we can begin to generate sections of documentation.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003312 for (const auto &I : SplitDocs) {
3313 WriteCategoryHeader(I.first, OS);
Aaron Ballman97dba042014-02-17 15:27:10 +00003314
3315 // Walk over each of the attributes in the category and write out their
3316 // documentation.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003317 for (const auto &Doc : I.second)
3318 WriteDocumentation(Doc, OS);
Aaron Ballman97dba042014-02-17 15:27:10 +00003319 }
3320}
3321
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00003322} // end namespace clang