blob: 4c3742c8e339193874580c6a9b11c602d732708c [file] [log] [blame]
Peter Collingbournebee583f2011-10-06 13:03:08 +00001//===- ClangAttrEmitter.cpp - Generate Clang attribute handling =-*- C++ -*--=//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Peter Collingbournebee583f2011-10-06 13:03:08 +00006//
7//===----------------------------------------------------------------------===//
8//
9// These tablegen backends emit Clang attribute processing code
10//
11//===----------------------------------------------------------------------===//
12
John McCallc45f8d42019-10-01 23:12:57 +000013#include "TableGenBackends.h"
John McCallb6f03a52019-10-25 18:38:07 -070014#include "ASTTableGen.h"
John McCallc45f8d42019-10-01 23:12:57 +000015
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000016#include "llvm/ADT/ArrayRef.h"
Alex Lorenz9e7bf162017-04-18 14:33:39 +000017#include "llvm/ADT/DenseMap.h"
George Burgess IV1881a572016-12-01 00:13:18 +000018#include "llvm/ADT/DenseSet.h"
Alex Lorenz3bfe9622017-04-18 10:46:41 +000019#include "llvm/ADT/STLExtras.h"
Alex Lorenz9e7bf162017-04-18 14:33:39 +000020#include "llvm/ADT/SmallString.h"
Aaron Ballman28afa182014-11-17 18:17:19 +000021#include "llvm/ADT/StringExtras.h"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000022#include "llvm/ADT/StringRef.h"
Alex Lorenz9e7bf162017-04-18 14:33:39 +000023#include "llvm/ADT/StringSet.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000024#include "llvm/ADT/StringSwitch.h"
Alex Lorenz9e7bf162017-04-18 14:33:39 +000025#include "llvm/ADT/iterator_range.h"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000026#include "llvm/Support/ErrorHandling.h"
27#include "llvm/Support/raw_ostream.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000028#include "llvm/TableGen/Error.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000029#include "llvm/TableGen/Record.h"
Douglas Gregor377f99b2012-05-02 17:33:51 +000030#include "llvm/TableGen/StringMatcher.h"
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000031#include "llvm/TableGen/TableGenBackend.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000032#include <algorithm>
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000033#include <cassert>
Peter Collingbournebee583f2011-10-06 13:03:08 +000034#include <cctype>
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000035#include <cstddef>
36#include <cstdint>
37#include <map>
Aaron Ballman8f1439b2014-03-05 16:49:55 +000038#include <memory>
Aaron Ballman80469032013-11-29 14:57:58 +000039#include <set>
Chandler Carruth5553d0d2014-01-07 11:51:46 +000040#include <sstream>
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000041#include <string>
42#include <utility>
43#include <vector>
Peter Collingbournebee583f2011-10-06 13:03:08 +000044
45using namespace llvm;
46
Benjamin Kramerd910d162015-03-10 18:24:01 +000047namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000048
Aaron Ballmanc669cc02014-01-27 22:10:04 +000049class FlattenedSpelling {
50 std::string V, N, NS;
51 bool K;
52
53public:
54 FlattenedSpelling(const std::string &Variety, const std::string &Name,
55 const std::string &Namespace, bool KnownToGCC) :
56 V(Variety), N(Name), NS(Namespace), K(KnownToGCC) {}
57 explicit FlattenedSpelling(const Record &Spelling) :
58 V(Spelling.getValueAsString("Variety")),
59 N(Spelling.getValueAsString("Name")) {
60
Aaron Ballmanffc43362017-10-26 12:19:02 +000061 assert(V != "GCC" && V != "Clang" &&
62 "Given a GCC spelling, which means this hasn't been flattened!");
Aaron Ballman606093a2017-10-15 15:01:42 +000063 if (V == "CXX11" || V == "C2x" || V == "Pragma")
Aaron Ballmanc669cc02014-01-27 22:10:04 +000064 NS = Spelling.getValueAsString("Namespace");
65 bool Unset;
66 K = Spelling.getValueAsBitOrUnset("KnownToGCC", Unset);
67 }
68
69 const std::string &variety() const { return V; }
70 const std::string &name() const { return N; }
71 const std::string &nameSpace() const { return NS; }
72 bool knownToGCC() const { return K; }
73};
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000074
Hans Wennborgdcfba332015-10-06 23:40:43 +000075} // end anonymous namespace
Aaron Ballmanc669cc02014-01-27 22:10:04 +000076
Benjamin Kramerd910d162015-03-10 18:24:01 +000077static std::vector<FlattenedSpelling>
78GetFlattenedSpellings(const Record &Attr) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +000079 std::vector<Record *> Spellings = Attr.getValueAsListOfDefs("Spellings");
80 std::vector<FlattenedSpelling> Ret;
81
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +000082 for (const auto &Spelling : Spellings) {
Aaron Ballmanffc43362017-10-26 12:19:02 +000083 StringRef Variety = Spelling->getValueAsString("Variety");
84 StringRef Name = Spelling->getValueAsString("Name");
85 if (Variety == "GCC") {
Aaron Ballmanc669cc02014-01-27 22:10:04 +000086 // Gin up two new spelling objects to add into the list.
Aaron Ballmanffc43362017-10-26 12:19:02 +000087 Ret.emplace_back("GNU", Name, "", true);
88 Ret.emplace_back("CXX11", Name, "gnu", true);
89 } else if (Variety == "Clang") {
90 Ret.emplace_back("GNU", Name, "", false);
91 Ret.emplace_back("CXX11", Name, "clang", false);
Aaron Ballman10007812018-01-03 22:22:48 +000092 if (Spelling->getValueAsBit("AllowInC"))
93 Ret.emplace_back("C2x", Name, "clang", false);
Aaron Ballmanc669cc02014-01-27 22:10:04 +000094 } else
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +000095 Ret.push_back(FlattenedSpelling(*Spelling));
Aaron Ballmanc669cc02014-01-27 22:10:04 +000096 }
97
98 return Ret;
99}
100
Peter Collingbournebee583f2011-10-06 13:03:08 +0000101static std::string ReadPCHRecord(StringRef type) {
102 return StringSwitch<std::string>(type)
David L. Jones267b8842017-01-24 01:04:30 +0000103 .EndsWith("Decl *", "Record.GetLocalDeclAs<"
104 + std::string(type, 0, type.size()-1) + ">(Record.readInt())")
John McCall3ce3d232019-12-13 03:37:23 -0500105 .Case("TypeSourceInfo *", "Record.readTypeSourceInfo()")
David L. Jones267b8842017-01-24 01:04:30 +0000106 .Case("Expr *", "Record.readExpr()")
John McCall3ce3d232019-12-13 03:37:23 -0500107 .Case("IdentifierInfo *", "Record.readIdentifier()")
David L. Jones267b8842017-01-24 01:04:30 +0000108 .Case("StringRef", "Record.readString()")
Joel E. Denny81508102018-03-13 14:51:22 +0000109 .Case("ParamIdx", "ParamIdx::deserialize(Record.readInt())")
David L. Jones267b8842017-01-24 01:04:30 +0000110 .Default("Record.readInt()");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000111}
112
Richard Smith19978562016-05-18 00:16:51 +0000113// Get a type that is suitable for storing an object of the specified type.
114static StringRef getStorageType(StringRef type) {
115 return StringSwitch<StringRef>(type)
116 .Case("StringRef", "std::string")
117 .Default(type);
118}
119
Peter Collingbournebee583f2011-10-06 13:03:08 +0000120// Assumes that the way to get the value is SA->getname()
121static std::string WritePCHRecord(StringRef type, StringRef name) {
Richard Smith290d8012016-04-06 17:06:00 +0000122 return "Record." + StringSwitch<std::string>(type)
123 .EndsWith("Decl *", "AddDeclRef(" + std::string(name) + ");\n")
124 .Case("TypeSourceInfo *", "AddTypeSourceInfo(" + std::string(name) + ");\n")
Peter Collingbournebee583f2011-10-06 13:03:08 +0000125 .Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
Richard Smith290d8012016-04-06 17:06:00 +0000126 .Case("IdentifierInfo *", "AddIdentifierRef(" + std::string(name) + ");\n")
127 .Case("StringRef", "AddString(" + std::string(name) + ");\n")
Joel E. Denny81508102018-03-13 14:51:22 +0000128 .Case("ParamIdx", "push_back(" + std::string(name) + ".serialize());\n")
Richard Smith290d8012016-04-06 17:06:00 +0000129 .Default("push_back(" + std::string(name) + ");\n");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000130}
131
Michael Han4a045172012-03-07 00:12:16 +0000132// Normalize attribute name by removing leading and trailing
133// underscores. For example, __foo, foo__, __foo__ would
134// become foo.
135static StringRef NormalizeAttrName(StringRef AttrName) {
George Burgess IV1881a572016-12-01 00:13:18 +0000136 AttrName.consume_front("__");
137 AttrName.consume_back("__");
Michael Han4a045172012-03-07 00:12:16 +0000138 return AttrName;
139}
140
Aaron Ballman36a53502014-01-16 13:03:14 +0000141// Normalize the name by removing any and all leading and trailing underscores.
142// This is different from NormalizeAttrName in that it also handles names like
143// _pascal and __pascal.
144static StringRef NormalizeNameForSpellingComparison(StringRef Name) {
Benjamin Kramer5c404072015-04-10 21:37:21 +0000145 return Name.trim("_");
Aaron Ballman36a53502014-01-16 13:03:14 +0000146}
147
Justin Lebar4086fe52017-01-05 16:51:54 +0000148// Normalize the spelling of a GNU attribute (i.e. "x" in "__attribute__((x))"),
149// removing "__" if it appears at the beginning and end of the attribute's name.
150static StringRef NormalizeGNUAttrSpelling(StringRef AttrSpelling) {
Michael Han4a045172012-03-07 00:12:16 +0000151 if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) {
152 AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4);
153 }
154
155 return AttrSpelling;
156}
157
Aaron Ballman2f22b942014-05-20 19:47:14 +0000158typedef std::vector<std::pair<std::string, const Record *>> ParsedAttrMap;
Aaron Ballman64e69862013-12-15 13:05:48 +0000159
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000160static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,
Craig Topper8ae12032014-05-07 06:21:57 +0000161 ParsedAttrMap *Dupes = nullptr) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000162 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Aaron Ballman64e69862013-12-15 13:05:48 +0000163 std::set<std::string> Seen;
164 ParsedAttrMap R;
Aaron Ballman2f22b942014-05-20 19:47:14 +0000165 for (const auto *Attr : Attrs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000166 if (Attr->getValueAsBit("SemaHandler")) {
Aaron Ballman64e69862013-12-15 13:05:48 +0000167 std::string AN;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000168 if (Attr->isSubClassOf("TargetSpecificAttr") &&
169 !Attr->isValueUnset("ParseKind")) {
170 AN = Attr->getValueAsString("ParseKind");
Aaron Ballman64e69862013-12-15 13:05:48 +0000171
172 // If this attribute has already been handled, it does not need to be
173 // handled again.
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000174 if (Seen.find(AN) != Seen.end()) {
175 if (Dupes)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000176 Dupes->push_back(std::make_pair(AN, Attr));
Aaron Ballman64e69862013-12-15 13:05:48 +0000177 continue;
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000178 }
Aaron Ballman64e69862013-12-15 13:05:48 +0000179 Seen.insert(AN);
180 } else
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000181 AN = NormalizeAttrName(Attr->getName()).str();
Aaron Ballman64e69862013-12-15 13:05:48 +0000182
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000183 R.push_back(std::make_pair(AN, Attr));
Aaron Ballman64e69862013-12-15 13:05:48 +0000184 }
185 }
186 return R;
187}
188
Peter Collingbournebee583f2011-10-06 13:03:08 +0000189namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000190
Peter Collingbournebee583f2011-10-06 13:03:08 +0000191 class Argument {
192 std::string lowerName, upperName;
193 StringRef attrName;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000194 bool isOpt;
John McCalla62c1a92015-10-28 00:17:34 +0000195 bool Fake;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000196
197 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000198 Argument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000199 : lowerName(Arg.getValueAsString("Name")), upperName(lowerName),
John McCalla62c1a92015-10-28 00:17:34 +0000200 attrName(Attr), isOpt(false), Fake(false) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000201 if (!lowerName.empty()) {
202 lowerName[0] = std::tolower(lowerName[0]);
203 upperName[0] = std::toupper(upperName[0]);
204 }
Reid Klecknerebeb0ca2016-05-31 17:42:56 +0000205 // Work around MinGW's macro definition of 'interface' to 'struct'. We
206 // have an attribute argument called 'Interface', so only the lower case
207 // name conflicts with the macro definition.
208 if (lowerName == "interface")
209 lowerName = "interface_";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000210 }
Hans Wennborgdcfba332015-10-06 23:40:43 +0000211 virtual ~Argument() = default;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000212
213 StringRef getLowerName() const { return lowerName; }
214 StringRef getUpperName() const { return upperName; }
215 StringRef getAttrName() const { return attrName; }
216
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000217 bool isOptional() const { return isOpt; }
218 void setOptional(bool set) { isOpt = set; }
219
John McCalla62c1a92015-10-28 00:17:34 +0000220 bool isFake() const { return Fake; }
221 void setFake(bool fake) { Fake = fake; }
222
Peter Collingbournebee583f2011-10-06 13:03:08 +0000223 // These functions print the argument contents formatted in different ways.
224 virtual void writeAccessors(raw_ostream &OS) const = 0;
225 virtual void writeAccessorDefinitions(raw_ostream &OS) const {}
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +0000226 virtual void writeASTVisitorTraversal(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000227 virtual void writeCloneArgs(raw_ostream &OS) const = 0;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000228 virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0;
Daniel Dunbardc51baa2012-02-10 06:00:29 +0000229 virtual void writeTemplateInstantiation(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000230 virtual void writeCtorBody(raw_ostream &OS) const {}
231 virtual void writeCtorInitializers(raw_ostream &OS) const = 0;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000232 virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000233 virtual void writeCtorParameters(raw_ostream &OS) const = 0;
234 virtual void writeDeclarations(raw_ostream &OS) const = 0;
235 virtual void writePCHReadArgs(raw_ostream &OS) const = 0;
236 virtual void writePCHReadDecls(raw_ostream &OS) const = 0;
237 virtual void writePCHWrite(raw_ostream &OS) const = 0;
Aaron Ballman48a533d2018-02-27 23:49:28 +0000238 virtual std::string getIsOmitted() const { return "false"; }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000239 virtual void writeValue(raw_ostream &OS) const = 0;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000240 virtual void writeDump(raw_ostream &OS) const = 0;
241 virtual void writeDumpChildren(raw_ostream &OS) const {}
Richard Trieude5cc7d2013-01-31 01:44:26 +0000242 virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000243
244 virtual bool isEnumArg() const { return false; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000245 virtual bool isVariadicEnumArg() const { return false; }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000246 virtual bool isVariadic() const { return false; }
Aaron Ballman36a53502014-01-16 13:03:14 +0000247
248 virtual void writeImplicitCtorArgs(raw_ostream &OS) const {
249 OS << getUpperName();
250 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000251 };
252
253 class SimpleArgument : public Argument {
254 std::string type;
255
256 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000257 SimpleArgument(const Record &Arg, StringRef Attr, std::string T)
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000258 : Argument(Arg, Attr), type(std::move(T)) {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000259
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000260 std::string getType() const { return type; }
261
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000262 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000263 OS << " " << type << " get" << getUpperName() << "() const {\n";
264 OS << " return " << getLowerName() << ";\n";
265 OS << " }";
266 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000267
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000268 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000269 OS << getLowerName();
270 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000271
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000272 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000273 OS << "A->get" << getUpperName() << "()";
274 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000275
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000276 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000277 OS << getLowerName() << "(" << getUpperName() << ")";
278 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000279
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000280 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000281 OS << getLowerName() << "()";
282 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000283
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000284 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000285 OS << type << " " << getUpperName();
286 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000287
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000288 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000289 OS << type << " " << getLowerName() << ";";
290 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000291
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000292 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000293 std::string read = ReadPCHRecord(type);
294 OS << " " << type << " " << getLowerName() << " = " << read << ";\n";
295 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000296
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000297 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000298 OS << getLowerName();
299 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000300
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000301 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000302 OS << " " << WritePCHRecord(type, "SA->get" +
303 std::string(getUpperName()) + "()");
304 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000305
Aaron Ballman48a533d2018-02-27 23:49:28 +0000306 std::string getIsOmitted() const override {
307 if (type == "IdentifierInfo *")
308 return "!get" + getUpperName().str() + "()";
Matthias Gehred293cbd2019-07-25 17:50:51 +0000309 if (type == "TypeSourceInfo *")
310 return "!get" + getUpperName().str() + "Loc()";
Joel E. Denny81508102018-03-13 14:51:22 +0000311 if (type == "ParamIdx")
312 return "!get" + getUpperName().str() + "().isValid()";
Aaron Ballman48a533d2018-02-27 23:49:28 +0000313 return "false";
314 }
315
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000316 void writeValue(raw_ostream &OS) const override {
Aaron Ballman48a533d2018-02-27 23:49:28 +0000317 if (type == "FunctionDecl *")
Richard Smithb87c4652013-10-31 21:23:20 +0000318 OS << "\" << get" << getUpperName()
319 << "()->getNameInfo().getAsString() << \"";
Aaron Ballman48a533d2018-02-27 23:49:28 +0000320 else if (type == "IdentifierInfo *")
321 // Some non-optional (comma required) identifier arguments can be the
322 // empty string but are then recorded as a nullptr.
323 OS << "\" << (get" << getUpperName() << "() ? get" << getUpperName()
324 << "()->getName() : \"\") << \"";
325 else if (type == "TypeSourceInfo *")
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000326 OS << "\" << get" << getUpperName() << "().getAsString() << \"";
Joel E. Denny81508102018-03-13 14:51:22 +0000327 else if (type == "ParamIdx")
328 OS << "\" << get" << getUpperName() << "().getSourceIndex() << \"";
Aaron Ballman48a533d2018-02-27 23:49:28 +0000329 else
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000330 OS << "\" << get" << getUpperName() << "() << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000331 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000332
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000333 void writeDump(raw_ostream &OS) const override {
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +0000334 if (type == "FunctionDecl *" || type == "NamedDecl *") {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000335 OS << " OS << \" \";\n";
336 OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n";
337 } else if (type == "IdentifierInfo *") {
Aaron Ballman48a533d2018-02-27 23:49:28 +0000338 // Some non-optional (comma required) identifier arguments can be the
339 // empty string but are then recorded as a nullptr.
340 OS << " if (SA->get" << getUpperName() << "())\n"
341 << " OS << \" \" << SA->get" << getUpperName()
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000342 << "()->getName();\n";
Richard Smithb87c4652013-10-31 21:23:20 +0000343 } else if (type == "TypeSourceInfo *") {
Matthias Gehred293cbd2019-07-25 17:50:51 +0000344 if (isOptional())
345 OS << " if (SA->get" << getUpperName() << "Loc())";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000346 OS << " OS << \" \" << SA->get" << getUpperName()
347 << "().getAsString();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000348 } else if (type == "bool") {
349 OS << " if (SA->get" << getUpperName() << "()) OS << \" "
350 << getUpperName() << "\";\n";
351 } else if (type == "int" || type == "unsigned") {
352 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
Joel E. Denny81508102018-03-13 14:51:22 +0000353 } else if (type == "ParamIdx") {
354 if (isOptional())
355 OS << " if (SA->get" << getUpperName() << "().isValid())\n ";
356 OS << " OS << \" \" << SA->get" << getUpperName()
357 << "().getSourceIndex();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000358 } else {
359 llvm_unreachable("Unknown SimpleArgument type!");
360 }
361 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000362 };
363
Aaron Ballman18a78382013-11-21 00:28:23 +0000364 class DefaultSimpleArgument : public SimpleArgument {
365 int64_t Default;
366
367 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000368 DefaultSimpleArgument(const Record &Arg, StringRef Attr,
Aaron Ballman18a78382013-11-21 00:28:23 +0000369 std::string T, int64_t Default)
370 : SimpleArgument(Arg, Attr, T), Default(Default) {}
371
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000372 void writeAccessors(raw_ostream &OS) const override {
Aaron Ballman18a78382013-11-21 00:28:23 +0000373 SimpleArgument::writeAccessors(OS);
374
375 OS << "\n\n static const " << getType() << " Default" << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000376 << " = ";
377 if (getType() == "bool")
378 OS << (Default != 0 ? "true" : "false");
379 else
380 OS << Default;
381 OS << ";";
Aaron Ballman18a78382013-11-21 00:28:23 +0000382 }
383 };
384
Peter Collingbournebee583f2011-10-06 13:03:08 +0000385 class StringArgument : public Argument {
386 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000387 StringArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000388 : Argument(Arg, Attr)
389 {}
390
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000391 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000392 OS << " llvm::StringRef get" << getUpperName() << "() const {\n";
393 OS << " return llvm::StringRef(" << getLowerName() << ", "
394 << getLowerName() << "Length);\n";
395 OS << " }\n";
396 OS << " unsigned get" << getUpperName() << "Length() const {\n";
397 OS << " return " << getLowerName() << "Length;\n";
398 OS << " }\n";
399 OS << " void set" << getUpperName()
400 << "(ASTContext &C, llvm::StringRef S) {\n";
401 OS << " " << getLowerName() << "Length = S.size();\n";
402 OS << " this->" << getLowerName() << " = new (C, 1) char ["
403 << getLowerName() << "Length];\n";
Chandler Carruth38a45cc2015-08-04 03:53:01 +0000404 OS << " if (!S.empty())\n";
405 OS << " std::memcpy(this->" << getLowerName() << ", S.data(), "
Peter Collingbournebee583f2011-10-06 13:03:08 +0000406 << getLowerName() << "Length);\n";
407 OS << " }";
408 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000409
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000410 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000411 OS << "get" << getUpperName() << "()";
412 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000413
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000414 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000415 OS << "A->get" << getUpperName() << "()";
416 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000417
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000418 void writeCtorBody(raw_ostream &OS) const override {
Chandler Carruth38a45cc2015-08-04 03:53:01 +0000419 OS << " if (!" << getUpperName() << ".empty())\n";
420 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000421 << ".data(), " << getLowerName() << "Length);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000422 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000423
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000424 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000425 OS << getLowerName() << "Length(" << getUpperName() << ".size()),"
426 << getLowerName() << "(new (Ctx, 1) char[" << getLowerName()
427 << "Length])";
428 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000429
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000430 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Hans Wennborg59dbe862015-09-29 20:56:43 +0000431 OS << getLowerName() << "Length(0)," << getLowerName() << "(nullptr)";
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000432 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000433
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000434 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000435 OS << "llvm::StringRef " << getUpperName();
436 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000437
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000438 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000439 OS << "unsigned " << getLowerName() << "Length;\n";
440 OS << "char *" << getLowerName() << ";";
441 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000442
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000443 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000444 OS << " std::string " << getLowerName()
David L. Jones267b8842017-01-24 01:04:30 +0000445 << "= Record.readString();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000446 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000447
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000448 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000449 OS << getLowerName();
450 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000451
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000452 void writePCHWrite(raw_ostream &OS) const override {
Richard Smith290d8012016-04-06 17:06:00 +0000453 OS << " Record.AddString(SA->get" << getUpperName() << "());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000454 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000455
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000456 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000457 OS << "\\\"\" << get" << getUpperName() << "() << \"\\\"";
458 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000459
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000460 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000461 OS << " OS << \" \\\"\" << SA->get" << getUpperName()
462 << "() << \"\\\"\";\n";
463 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000464 };
465
466 class AlignedArgument : public Argument {
467 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000468 AlignedArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000469 : Argument(Arg, Attr)
470 {}
471
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000472 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000473 OS << " bool is" << getUpperName() << "Dependent() const;\n";
474
475 OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n";
476
477 OS << " bool is" << getUpperName() << "Expr() const {\n";
478 OS << " return is" << getLowerName() << "Expr;\n";
479 OS << " }\n";
480
481 OS << " Expr *get" << getUpperName() << "Expr() const {\n";
482 OS << " assert(is" << getLowerName() << "Expr);\n";
483 OS << " return " << getLowerName() << "Expr;\n";
484 OS << " }\n";
485
486 OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n";
487 OS << " assert(!is" << getLowerName() << "Expr);\n";
488 OS << " return " << getLowerName() << "Type;\n";
489 OS << " }";
490 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000491
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000492 void writeAccessorDefinitions(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000493 OS << "bool " << getAttrName() << "Attr::is" << getUpperName()
494 << "Dependent() const {\n";
495 OS << " if (is" << getLowerName() << "Expr)\n";
496 OS << " return " << getLowerName() << "Expr && (" << getLowerName()
497 << "Expr->isValueDependent() || " << getLowerName()
498 << "Expr->isTypeDependent());\n";
499 OS << " else\n";
500 OS << " return " << getLowerName()
501 << "Type->getType()->isDependentType();\n";
502 OS << "}\n";
503
504 // FIXME: Do not do the calculation here
505 // FIXME: Handle types correctly
506 // A null pointer means maximum alignment
Peter Collingbournebee583f2011-10-06 13:03:08 +0000507 OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
508 << "(ASTContext &Ctx) const {\n";
509 OS << " assert(!is" << getUpperName() << "Dependent());\n";
510 OS << " if (is" << getLowerName() << "Expr)\n";
Ulrich Weigandca3cb7f2015-04-21 17:29:35 +0000511 OS << " return " << getLowerName() << "Expr ? " << getLowerName()
512 << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue()"
513 << " * Ctx.getCharWidth() : "
514 << "Ctx.getTargetDefaultAlignForAttributeAligned();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000515 OS << " else\n";
516 OS << " return 0; // FIXME\n";
517 OS << "}\n";
518 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000519
Richard Smithf26d5512017-08-15 22:58:45 +0000520 void writeASTVisitorTraversal(raw_ostream &OS) const override {
521 StringRef Name = getUpperName();
522 OS << " if (A->is" << Name << "Expr()) {\n"
523 << " if (!getDerived().TraverseStmt(A->get" << Name << "Expr()))\n"
524 << " return false;\n"
525 << " } else if (auto *TSI = A->get" << Name << "Type()) {\n"
526 << " if (!getDerived().TraverseTypeLoc(TSI->getTypeLoc()))\n"
527 << " return false;\n"
528 << " }\n";
529 }
530
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000531 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000532 OS << "is" << getLowerName() << "Expr, is" << getLowerName()
533 << "Expr ? static_cast<void*>(" << getLowerName()
534 << "Expr) : " << getLowerName()
535 << "Type";
536 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000537
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000538 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000539 // FIXME: move the definition in Sema::InstantiateAttrs to here.
540 // In the meantime, aligned attributes are cloned.
541 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000542
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000543 void writeCtorBody(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000544 OS << " if (is" << getLowerName() << "Expr)\n";
545 OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>("
546 << getUpperName() << ");\n";
547 OS << " else\n";
548 OS << " " << getLowerName()
549 << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000550 << ");\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000551 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000552
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000553 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000554 OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)";
555 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000556
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000557 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000558 OS << "is" << getLowerName() << "Expr(false)";
559 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000560
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000561 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000562 OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName();
563 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000564
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000565 void writeImplicitCtorArgs(raw_ostream &OS) const override {
Aaron Ballman36a53502014-01-16 13:03:14 +0000566 OS << "Is" << getUpperName() << "Expr, " << getUpperName();
567 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000568
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000569 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000570 OS << "bool is" << getLowerName() << "Expr;\n";
571 OS << "union {\n";
572 OS << "Expr *" << getLowerName() << "Expr;\n";
573 OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
574 OS << "};";
575 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000576
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000577 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000578 OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr";
579 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000580
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000581 void writePCHReadDecls(raw_ostream &OS) const override {
David L. Jones267b8842017-01-24 01:04:30 +0000582 OS << " bool is" << getLowerName() << "Expr = Record.readInt();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000583 OS << " void *" << getLowerName() << "Ptr;\n";
584 OS << " if (is" << getLowerName() << "Expr)\n";
David L. Jones267b8842017-01-24 01:04:30 +0000585 OS << " " << getLowerName() << "Ptr = Record.readExpr();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000586 OS << " else\n";
587 OS << " " << getLowerName()
John McCall3ce3d232019-12-13 03:37:23 -0500588 << "Ptr = Record.readTypeSourceInfo();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000589 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000590
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000591 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000592 OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n";
593 OS << " if (SA->is" << getUpperName() << "Expr())\n";
Richard Smith290d8012016-04-06 17:06:00 +0000594 OS << " Record.AddStmt(SA->get" << getUpperName() << "Expr());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000595 OS << " else\n";
Richard Smith290d8012016-04-06 17:06:00 +0000596 OS << " Record.AddTypeSourceInfo(SA->get" << getUpperName()
597 << "Type());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000598 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000599
Aaron Ballman48a533d2018-02-27 23:49:28 +0000600 std::string getIsOmitted() const override {
601 return "!is" + getLowerName().str() + "Expr || !" + getLowerName().str()
602 + "Expr";
603 }
604
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000605 void writeValue(raw_ostream &OS) const override {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000606 OS << "\";\n";
Aaron Ballman48a533d2018-02-27 23:49:28 +0000607 OS << " " << getLowerName()
608 << "Expr->printPretty(OS, nullptr, Policy);\n";
Richard Trieuddd01ce2014-06-09 22:53:25 +0000609 OS << " OS << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000610 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000611
Stephen Kellydb8fac12019-01-11 19:16:01 +0000612 void writeDump(raw_ostream &OS) const override {
613 OS << " if (!SA->is" << getUpperName() << "Expr())\n";
614 OS << " dumpType(SA->get" << getUpperName()
615 << "Type()->getType());\n";
616 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000617
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000618 void writeDumpChildren(raw_ostream &OS) const override {
Richard Smithf7514452014-10-30 21:02:37 +0000619 OS << " if (SA->is" << getUpperName() << "Expr())\n";
Stephen Kelly6d110d62019-01-30 19:49:49 +0000620 OS << " Visit(SA->get" << getUpperName() << "Expr());\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000621 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000622
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000623 void writeHasChildren(raw_ostream &OS) const override {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000624 OS << "SA->is" << getUpperName() << "Expr()";
625 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000626 };
627
628 class VariadicArgument : public Argument {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000629 std::string Type, ArgName, ArgSizeName, RangeName;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000630
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000631 protected:
632 // Assumed to receive a parameter: raw_ostream OS.
633 virtual void writeValueImpl(raw_ostream &OS) const {
634 OS << " OS << Val;\n";
635 }
Joel E. Denny81508102018-03-13 14:51:22 +0000636 // Assumed to receive a parameter: raw_ostream OS.
637 virtual void writeDumpImpl(raw_ostream &OS) const {
638 OS << " OS << \" \" << Val;\n";
639 }
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000640
Peter Collingbournebee583f2011-10-06 13:03:08 +0000641 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000642 VariadicArgument(const Record &Arg, StringRef Attr, std::string T)
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000643 : Argument(Arg, Attr), Type(std::move(T)),
644 ArgName(getLowerName().str() + "_"), ArgSizeName(ArgName + "Size"),
645 RangeName(getLowerName()) {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000646
Benjamin Kramer1b582012016-02-13 18:11:49 +0000647 const std::string &getType() const { return Type; }
648 const std::string &getArgName() const { return ArgName; }
649 const std::string &getArgSizeName() const { return ArgSizeName; }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000650 bool isVariadic() const override { return true; }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000651
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000652 void writeAccessors(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000653 std::string IteratorType = getLowerName().str() + "_iterator";
654 std::string BeginFn = getLowerName().str() + "_begin()";
655 std::string EndFn = getLowerName().str() + "_end()";
656
657 OS << " typedef " << Type << "* " << IteratorType << ";\n";
658 OS << " " << IteratorType << " " << BeginFn << " const {"
659 << " return " << ArgName << "; }\n";
660 OS << " " << IteratorType << " " << EndFn << " const {"
661 << " return " << ArgName << " + " << ArgSizeName << "; }\n";
662 OS << " unsigned " << getLowerName() << "_size() const {"
663 << " return " << ArgSizeName << "; }\n";
664 OS << " llvm::iterator_range<" << IteratorType << "> " << RangeName
665 << "() const { return llvm::make_range(" << BeginFn << ", " << EndFn
666 << "); }\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000667 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000668
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000669 void writeCloneArgs(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000670 OS << ArgName << ", " << ArgSizeName;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000671 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000672
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000673 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000674 // This isn't elegant, but we have to go through public methods...
675 OS << "A->" << getLowerName() << "_begin(), "
676 << "A->" << getLowerName() << "_size()";
677 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000678
Richard Smithf26d5512017-08-15 22:58:45 +0000679 void writeASTVisitorTraversal(raw_ostream &OS) const override {
680 // FIXME: Traverse the elements.
681 }
682
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000683 void writeCtorBody(raw_ostream &OS) const override {
Aaron Ballmand6459e52014-05-01 15:21:03 +0000684 OS << " std::copy(" << getUpperName() << ", " << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000685 << " + " << ArgSizeName << ", " << ArgName << ");\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000686 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000687
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000688 void writeCtorInitializers(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000689 OS << ArgSizeName << "(" << getUpperName() << "Size), "
690 << ArgName << "(new (Ctx, 16) " << getType() << "["
691 << ArgSizeName << "])";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000692 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000693
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000694 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000695 OS << ArgSizeName << "(0), " << ArgName << "(nullptr)";
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000696 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000697
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000698 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000699 OS << getType() << " *" << getUpperName() << ", unsigned "
700 << getUpperName() << "Size";
701 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000702
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000703 void writeImplicitCtorArgs(raw_ostream &OS) const override {
Aaron Ballman36a53502014-01-16 13:03:14 +0000704 OS << getUpperName() << ", " << getUpperName() << "Size";
705 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000706
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000707 void writeDeclarations(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000708 OS << " unsigned " << ArgSizeName << ";\n";
709 OS << " " << getType() << " *" << ArgName << ";";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000710 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000711
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000712 void writePCHReadDecls(raw_ostream &OS) const override {
David L. Jones267b8842017-01-24 01:04:30 +0000713 OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n";
Richard Smith19978562016-05-18 00:16:51 +0000714 OS << " SmallVector<" << getType() << ", 4> "
715 << getLowerName() << ";\n";
716 OS << " " << getLowerName() << ".reserve(" << getLowerName()
Peter Collingbournebee583f2011-10-06 13:03:08 +0000717 << "Size);\n";
Richard Smith19978562016-05-18 00:16:51 +0000718
719 // If we can't store the values in the current type (if it's something
720 // like StringRef), store them in a different type and convert the
721 // container afterwards.
722 std::string StorageType = getStorageType(getType());
723 std::string StorageName = getLowerName();
724 if (StorageType != getType()) {
725 StorageName += "Storage";
726 OS << " SmallVector<" << StorageType << ", 4> "
727 << StorageName << ";\n";
728 OS << " " << StorageName << ".reserve(" << getLowerName()
729 << "Size);\n";
730 }
731
732 OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n";
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000733 std::string read = ReadPCHRecord(Type);
Richard Smith19978562016-05-18 00:16:51 +0000734 OS << " " << StorageName << ".push_back(" << read << ");\n";
735
736 if (StorageType != getType()) {
737 OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n";
738 OS << " " << getLowerName() << ".push_back("
739 << StorageName << "[i]);\n";
740 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000741 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000742
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000743 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000744 OS << getLowerName() << ".data(), " << getLowerName() << "Size";
745 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000746
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000747 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000748 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000749 OS << " for (auto &Val : SA->" << RangeName << "())\n";
750 OS << " " << WritePCHRecord(Type, "Val");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000751 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000752
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000753 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000754 OS << "\";\n";
755 OS << " bool isFirst = true;\n"
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000756 << " for (const auto &Val : " << RangeName << "()) {\n"
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000757 << " if (isFirst) isFirst = false;\n"
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000758 << " else OS << \", \";\n";
759 writeValueImpl(OS);
760 OS << " }\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000761 OS << " OS << \"";
762 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000763
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000764 void writeDump(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000765 OS << " for (const auto &Val : SA->" << RangeName << "())\n";
Joel E. Denny81508102018-03-13 14:51:22 +0000766 writeDumpImpl(OS);
767 }
768 };
769
770 class VariadicParamIdxArgument : public VariadicArgument {
771 public:
772 VariadicParamIdxArgument(const Record &Arg, StringRef Attr)
773 : VariadicArgument(Arg, Attr, "ParamIdx") {}
774
775 public:
776 void writeValueImpl(raw_ostream &OS) const override {
777 OS << " OS << Val.getSourceIndex();\n";
778 }
779
780 void writeDumpImpl(raw_ostream &OS) const override {
781 OS << " OS << \" \" << Val.getSourceIndex();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000782 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000783 };
784
Johannes Doerfertac991bb2019-01-19 05:36:54 +0000785 struct VariadicParamOrParamIdxArgument : public VariadicArgument {
786 VariadicParamOrParamIdxArgument(const Record &Arg, StringRef Attr)
787 : VariadicArgument(Arg, Attr, "int") {}
788 };
789
Reid Klecknerf526b9482014-02-12 18:22:18 +0000790 // Unique the enums, but maintain the original declaration ordering.
Craig Topper00648582017-05-31 19:01:22 +0000791 std::vector<StringRef>
792 uniqueEnumsInOrder(const std::vector<StringRef> &enums) {
793 std::vector<StringRef> uniques;
George Burgess IV1881a572016-12-01 00:13:18 +0000794 SmallDenseSet<StringRef, 8> unique_set;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000795 for (const auto &i : enums) {
George Burgess IV1881a572016-12-01 00:13:18 +0000796 if (unique_set.insert(i).second)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000797 uniques.push_back(i);
Reid Klecknerf526b9482014-02-12 18:22:18 +0000798 }
799 return uniques;
800 }
801
Peter Collingbournebee583f2011-10-06 13:03:08 +0000802 class EnumArgument : public Argument {
803 std::string type;
Craig Topper00648582017-05-31 19:01:22 +0000804 std::vector<StringRef> values, enums, uniques;
805
Peter Collingbournebee583f2011-10-06 13:03:08 +0000806 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000807 EnumArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000808 : Argument(Arg, Attr), type(Arg.getValueAsString("Type")),
Aaron Ballman0e468c02014-01-05 21:08:29 +0000809 values(Arg.getValueAsListOfStrings("Values")),
810 enums(Arg.getValueAsListOfStrings("Enums")),
Reid Klecknerf526b9482014-02-12 18:22:18 +0000811 uniques(uniqueEnumsInOrder(enums))
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000812 {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000813 // FIXME: Emit a proper error
814 assert(!uniques.empty());
815 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000816
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000817 bool isEnumArg() const override { return true; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000818
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000819 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000820 OS << " " << type << " get" << getUpperName() << "() const {\n";
821 OS << " return " << getLowerName() << ";\n";
822 OS << " }";
823 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000824
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000825 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000826 OS << getLowerName();
827 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000828
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000829 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000830 OS << "A->get" << getUpperName() << "()";
831 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000832 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000833 OS << getLowerName() << "(" << getUpperName() << ")";
834 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000835 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000836 OS << getLowerName() << "(" << type << "(0))";
837 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000838 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000839 OS << type << " " << getUpperName();
840 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000841 void writeDeclarations(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +0000842 auto i = uniques.cbegin(), e = uniques.cend();
Peter Collingbournebee583f2011-10-06 13:03:08 +0000843 // The last one needs to not have a comma.
844 --e;
845
846 OS << "public:\n";
847 OS << " enum " << type << " {\n";
848 for (; i != e; ++i)
849 OS << " " << *i << ",\n";
850 OS << " " << *e << "\n";
851 OS << " };\n";
852 OS << "private:\n";
853 OS << " " << type << " " << getLowerName() << ";";
854 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000855
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000856 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000857 OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName()
858 << "(static_cast<" << getAttrName() << "Attr::" << type
David L. Jones267b8842017-01-24 01:04:30 +0000859 << ">(Record.readInt()));\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000860 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000861
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000862 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000863 OS << getLowerName();
864 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000865
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000866 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000867 OS << "Record.push_back(SA->get" << getUpperName() << "());\n";
868 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000869
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000870 void writeValue(raw_ostream &OS) const override {
Aaron Ballman36d79102014-09-15 16:16:14 +0000871 // FIXME: this isn't 100% correct -- some enum arguments require printing
872 // as a string literal, while others require printing as an identifier.
873 // Tablegen currently does not distinguish between the two forms.
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000874 OS << "\\\"\" << " << getAttrName() << "Attr::Convert" << type << "ToStr(get"
875 << getUpperName() << "()) << \"\\\"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000876 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000877
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000878 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000879 OS << " switch(SA->get" << getUpperName() << "()) {\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000880 for (const auto &I : uniques) {
881 OS << " case " << getAttrName() << "Attr::" << I << ":\n";
882 OS << " OS << \" " << I << "\";\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000883 OS << " break;\n";
884 }
885 OS << " }\n";
886 }
Aaron Ballman682ee422013-09-11 19:47:58 +0000887
888 void writeConversion(raw_ostream &OS) const {
889 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
890 OS << type << " &Out) {\n";
891 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000892 OS << type << ">>(Val)\n";
Aaron Ballman682ee422013-09-11 19:47:58 +0000893 for (size_t I = 0; I < enums.size(); ++I) {
894 OS << " .Case(\"" << values[I] << "\", ";
895 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
896 }
897 OS << " .Default(Optional<" << type << ">());\n";
898 OS << " if (R) {\n";
899 OS << " Out = *R;\n return true;\n }\n";
900 OS << " return false;\n";
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000901 OS << " }\n\n";
902
903 // Mapping from enumeration values back to enumeration strings isn't
904 // trivial because some enumeration values have multiple named
905 // enumerators, such as type_visibility(internal) and
906 // type_visibility(hidden) both mapping to TypeVisibilityAttr::Hidden.
907 OS << " static const char *Convert" << type << "ToStr("
908 << type << " Val) {\n"
909 << " switch(Val) {\n";
George Burgess IV1881a572016-12-01 00:13:18 +0000910 SmallDenseSet<StringRef, 8> Uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000911 for (size_t I = 0; I < enums.size(); ++I) {
912 if (Uniques.insert(enums[I]).second)
913 OS << " case " << getAttrName() << "Attr::" << enums[I]
914 << ": return \"" << values[I] << "\";\n";
915 }
916 OS << " }\n"
917 << " llvm_unreachable(\"No enumerator with that value\");\n"
918 << " }\n";
Aaron Ballman682ee422013-09-11 19:47:58 +0000919 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000920 };
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000921
922 class VariadicEnumArgument: public VariadicArgument {
923 std::string type, QualifiedTypeName;
Craig Topper00648582017-05-31 19:01:22 +0000924 std::vector<StringRef> values, enums, uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000925
926 protected:
927 void writeValueImpl(raw_ostream &OS) const override {
Aaron Ballman36d79102014-09-15 16:16:14 +0000928 // FIXME: this isn't 100% correct -- some enum arguments require printing
929 // as a string literal, while others require printing as an identifier.
930 // Tablegen currently does not distinguish between the two forms.
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000931 OS << " OS << \"\\\"\" << " << getAttrName() << "Attr::Convert" << type
932 << "ToStr(Val)" << "<< \"\\\"\";\n";
933 }
934
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000935 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000936 VariadicEnumArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000937 : VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")),
938 type(Arg.getValueAsString("Type")),
Aaron Ballman0e468c02014-01-05 21:08:29 +0000939 values(Arg.getValueAsListOfStrings("Values")),
940 enums(Arg.getValueAsListOfStrings("Enums")),
Reid Klecknerf526b9482014-02-12 18:22:18 +0000941 uniques(uniqueEnumsInOrder(enums))
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000942 {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000943 QualifiedTypeName = getAttrName().str() + "Attr::" + type;
944
945 // FIXME: Emit a proper error
946 assert(!uniques.empty());
947 }
948
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000949 bool isVariadicEnumArg() const override { return true; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000950
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000951 void writeDeclarations(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +0000952 auto i = uniques.cbegin(), e = uniques.cend();
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000953 // The last one needs to not have a comma.
954 --e;
955
956 OS << "public:\n";
957 OS << " enum " << type << " {\n";
958 for (; i != e; ++i)
959 OS << " " << *i << ",\n";
960 OS << " " << *e << "\n";
961 OS << " };\n";
962 OS << "private:\n";
963
964 VariadicArgument::writeDeclarations(OS);
965 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000966
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000967 void writeDump(raw_ostream &OS) const override {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000968 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
969 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
970 << getLowerName() << "_end(); I != E; ++I) {\n";
971 OS << " switch(*I) {\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000972 for (const auto &UI : uniques) {
973 OS << " case " << getAttrName() << "Attr::" << UI << ":\n";
974 OS << " OS << \" " << UI << "\";\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000975 OS << " break;\n";
976 }
977 OS << " }\n";
978 OS << " }\n";
979 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000980
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000981 void writePCHReadDecls(raw_ostream &OS) const override {
David L. Jones267b8842017-01-24 01:04:30 +0000982 OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000983 OS << " SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName()
984 << ";\n";
985 OS << " " << getLowerName() << ".reserve(" << getLowerName()
986 << "Size);\n";
987 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
988 OS << " " << getLowerName() << ".push_back(" << "static_cast<"
David L. Jones267b8842017-01-24 01:04:30 +0000989 << QualifiedTypeName << ">(Record.readInt()));\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000990 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000991
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000992 void writePCHWrite(raw_ostream &OS) const override {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000993 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
994 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
995 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
996 << getLowerName() << "_end(); i != e; ++i)\n";
997 OS << " " << WritePCHRecord(QualifiedTypeName, "(*i)");
998 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000999
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001000 void writeConversion(raw_ostream &OS) const {
1001 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
1002 OS << type << " &Out) {\n";
1003 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001004 OS << type << ">>(Val)\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001005 for (size_t I = 0; I < enums.size(); ++I) {
1006 OS << " .Case(\"" << values[I] << "\", ";
1007 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
1008 }
1009 OS << " .Default(Optional<" << type << ">());\n";
1010 OS << " if (R) {\n";
1011 OS << " Out = *R;\n return true;\n }\n";
1012 OS << " return false;\n";
Aaron Ballman25a2cb92014-09-15 15:14:13 +00001013 OS << " }\n\n";
1014
1015 OS << " static const char *Convert" << type << "ToStr("
1016 << type << " Val) {\n"
1017 << " switch(Val) {\n";
George Burgess IV1881a572016-12-01 00:13:18 +00001018 SmallDenseSet<StringRef, 8> Uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +00001019 for (size_t I = 0; I < enums.size(); ++I) {
1020 if (Uniques.insert(enums[I]).second)
1021 OS << " case " << getAttrName() << "Attr::" << enums[I]
1022 << ": return \"" << values[I] << "\";\n";
1023 }
1024 OS << " }\n"
1025 << " llvm_unreachable(\"No enumerator with that value\");\n"
1026 << " }\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001027 }
1028 };
Peter Collingbournebee583f2011-10-06 13:03:08 +00001029
1030 class VersionArgument : public Argument {
1031 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001032 VersionArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +00001033 : Argument(Arg, Attr)
1034 {}
1035
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001036 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001037 OS << " VersionTuple get" << getUpperName() << "() const {\n";
1038 OS << " return " << getLowerName() << ";\n";
1039 OS << " }\n";
1040 OS << " void set" << getUpperName()
1041 << "(ASTContext &C, VersionTuple V) {\n";
1042 OS << " " << getLowerName() << " = V;\n";
1043 OS << " }";
1044 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001045
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001046 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001047 OS << "get" << getUpperName() << "()";
1048 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001049
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001050 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001051 OS << "A->get" << getUpperName() << "()";
1052 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001053
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001054 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001055 OS << getLowerName() << "(" << getUpperName() << ")";
1056 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001057
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001058 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001059 OS << getLowerName() << "()";
1060 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001061
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001062 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001063 OS << "VersionTuple " << getUpperName();
1064 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001065
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001066 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001067 OS << "VersionTuple " << getLowerName() << ";\n";
1068 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001069
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001070 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001071 OS << " VersionTuple " << getLowerName()
David L. Jones267b8842017-01-24 01:04:30 +00001072 << "= Record.readVersionTuple();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001073 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001074
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001075 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001076 OS << getLowerName();
1077 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001078
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001079 void writePCHWrite(raw_ostream &OS) const override {
Richard Smith290d8012016-04-06 17:06:00 +00001080 OS << " Record.AddVersionTuple(SA->get" << getUpperName() << "());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001081 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001082
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001083 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001084 OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";
1085 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001086
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001087 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001088 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
1089 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001090 };
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001091
1092 class ExprArgument : public SimpleArgument {
1093 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001094 ExprArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001095 : SimpleArgument(Arg, Attr, "Expr *")
1096 {}
1097
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001098 void writeASTVisitorTraversal(raw_ostream &OS) const override {
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001099 OS << " if (!"
1100 << "getDerived().TraverseStmt(A->get" << getUpperName() << "()))\n";
1101 OS << " return false;\n";
1102 }
1103
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001104 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001105 OS << "tempInst" << getUpperName();
1106 }
1107
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001108 void writeTemplateInstantiation(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001109 OS << " " << getType() << " tempInst" << getUpperName() << ";\n";
1110 OS << " {\n";
1111 OS << " EnterExpressionEvaluationContext "
Faisal Valid143a0c2017-04-01 21:30:49 +00001112 << "Unevaluated(S, Sema::ExpressionEvaluationContext::Unevaluated);\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001113 OS << " ExprResult " << "Result = S.SubstExpr("
1114 << "A->get" << getUpperName() << "(), TemplateArgs);\n";
1115 OS << " tempInst" << getUpperName() << " = "
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001116 << "Result.getAs<Expr>();\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001117 OS << " }\n";
1118 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001119
Craig Topper3164f332014-03-11 03:39:26 +00001120 void writeDump(raw_ostream &OS) const override {}
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001121
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001122 void writeDumpChildren(raw_ostream &OS) const override {
Stephen Kelly6d110d62019-01-30 19:49:49 +00001123 OS << " Visit(SA->get" << getUpperName() << "());\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001124 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001125
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001126 void writeHasChildren(raw_ostream &OS) const override { OS << "true"; }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001127 };
1128
1129 class VariadicExprArgument : public VariadicArgument {
1130 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001131 VariadicExprArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001132 : VariadicArgument(Arg, Attr, "Expr *")
1133 {}
1134
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001135 void writeASTVisitorTraversal(raw_ostream &OS) const override {
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001136 OS << " {\n";
1137 OS << " " << getType() << " *I = A->" << getLowerName()
1138 << "_begin();\n";
1139 OS << " " << getType() << " *E = A->" << getLowerName()
1140 << "_end();\n";
1141 OS << " for (; I != E; ++I) {\n";
1142 OS << " if (!getDerived().TraverseStmt(*I))\n";
1143 OS << " return false;\n";
1144 OS << " }\n";
1145 OS << " }\n";
1146 }
1147
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001148 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001149 OS << "tempInst" << getUpperName() << ", "
1150 << "A->" << getLowerName() << "_size()";
1151 }
1152
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001153 void writeTemplateInstantiation(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +00001154 OS << " auto *tempInst" << getUpperName()
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001155 << " = new (C, 16) " << getType()
1156 << "[A->" << getLowerName() << "_size()];\n";
1157 OS << " {\n";
1158 OS << " EnterExpressionEvaluationContext "
Faisal Valid143a0c2017-04-01 21:30:49 +00001159 << "Unevaluated(S, Sema::ExpressionEvaluationContext::Unevaluated);\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001160 OS << " " << getType() << " *TI = tempInst" << getUpperName()
1161 << ";\n";
1162 OS << " " << getType() << " *I = A->" << getLowerName()
1163 << "_begin();\n";
1164 OS << " " << getType() << " *E = A->" << getLowerName()
1165 << "_end();\n";
1166 OS << " for (; I != E; ++I, ++TI) {\n";
1167 OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n";
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001168 OS << " *TI = Result.getAs<Expr>();\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001169 OS << " }\n";
1170 OS << " }\n";
1171 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001172
Craig Topper3164f332014-03-11 03:39:26 +00001173 void writeDump(raw_ostream &OS) const override {}
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001174
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001175 void writeDumpChildren(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001176 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
1177 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
Richard Smithf7514452014-10-30 21:02:37 +00001178 << getLowerName() << "_end(); I != E; ++I)\n";
Stephen Kelly6d110d62019-01-30 19:49:49 +00001179 OS << " Visit(*I);\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001180 }
1181
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001182 void writeHasChildren(raw_ostream &OS) const override {
Richard Trieude5cc7d2013-01-31 01:44:26 +00001183 OS << "SA->" << getLowerName() << "_begin() != "
1184 << "SA->" << getLowerName() << "_end()";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001185 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001186 };
Richard Smithb87c4652013-10-31 21:23:20 +00001187
Erich Keane3efe0022018-07-20 14:13:28 +00001188 class VariadicIdentifierArgument : public VariadicArgument {
1189 public:
1190 VariadicIdentifierArgument(const Record &Arg, StringRef Attr)
1191 : VariadicArgument(Arg, Attr, "IdentifierInfo *")
1192 {}
1193 };
1194
Peter Collingbourne915df992015-05-15 18:33:32 +00001195 class VariadicStringArgument : public VariadicArgument {
1196 public:
1197 VariadicStringArgument(const Record &Arg, StringRef Attr)
Benjamin Kramer1b582012016-02-13 18:11:49 +00001198 : VariadicArgument(Arg, Attr, "StringRef")
Peter Collingbourne915df992015-05-15 18:33:32 +00001199 {}
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001200
Benjamin Kramer1b582012016-02-13 18:11:49 +00001201 void writeCtorBody(raw_ostream &OS) const override {
1202 OS << " for (size_t I = 0, E = " << getArgSizeName() << "; I != E;\n"
1203 " ++I) {\n"
1204 " StringRef Ref = " << getUpperName() << "[I];\n"
1205 " if (!Ref.empty()) {\n"
1206 " char *Mem = new (Ctx, 1) char[Ref.size()];\n"
1207 " std::memcpy(Mem, Ref.data(), Ref.size());\n"
1208 " " << getArgName() << "[I] = StringRef(Mem, Ref.size());\n"
1209 " }\n"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001210 " }\n";
Benjamin Kramer1b582012016-02-13 18:11:49 +00001211 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001212
Peter Collingbourne915df992015-05-15 18:33:32 +00001213 void writeValueImpl(raw_ostream &OS) const override {
1214 OS << " OS << \"\\\"\" << Val << \"\\\"\";\n";
1215 }
1216 };
1217
Richard Smithb87c4652013-10-31 21:23:20 +00001218 class TypeArgument : public SimpleArgument {
1219 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001220 TypeArgument(const Record &Arg, StringRef Attr)
Richard Smithb87c4652013-10-31 21:23:20 +00001221 : SimpleArgument(Arg, Attr, "TypeSourceInfo *")
1222 {}
1223
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001224 void writeAccessors(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001225 OS << " QualType get" << getUpperName() << "() const {\n";
1226 OS << " return " << getLowerName() << "->getType();\n";
1227 OS << " }";
1228 OS << " " << getType() << " get" << getUpperName() << "Loc() const {\n";
1229 OS << " return " << getLowerName() << ";\n";
1230 OS << " }";
1231 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001232
Richard Smithf26d5512017-08-15 22:58:45 +00001233 void writeASTVisitorTraversal(raw_ostream &OS) const override {
1234 OS << " if (auto *TSI = A->get" << getUpperName() << "Loc())\n";
1235 OS << " if (!getDerived().TraverseTypeLoc(TSI->getTypeLoc()))\n";
1236 OS << " return false;\n";
1237 }
1238
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001239 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001240 OS << "A->get" << getUpperName() << "Loc()";
1241 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001242
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001243 void writePCHWrite(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001244 OS << " " << WritePCHRecord(
1245 getType(), "SA->get" + std::string(getUpperName()) + "Loc()");
1246 }
1247 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001248
Hans Wennborgdcfba332015-10-06 23:40:43 +00001249} // end anonymous namespace
Peter Collingbournebee583f2011-10-06 13:03:08 +00001250
Aaron Ballman2f22b942014-05-20 19:47:14 +00001251static std::unique_ptr<Argument>
1252createArgument(const Record &Arg, StringRef Attr,
1253 const Record *Search = nullptr) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001254 if (!Search)
1255 Search = &Arg;
1256
David Blaikie28f30ca2014-08-08 23:59:38 +00001257 std::unique_ptr<Argument> Ptr;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001258 llvm::StringRef ArgName = Search->getName();
1259
David Blaikie28f30ca2014-08-08 23:59:38 +00001260 if (ArgName == "AlignedArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001261 Ptr = std::make_unique<AlignedArgument>(Arg, Attr);
David Blaikie28f30ca2014-08-08 23:59:38 +00001262 else if (ArgName == "EnumArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001263 Ptr = std::make_unique<EnumArgument>(Arg, Attr);
David Blaikie28f30ca2014-08-08 23:59:38 +00001264 else if (ArgName == "ExprArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001265 Ptr = std::make_unique<ExprArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001266 else if (ArgName == "FunctionArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001267 Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "FunctionDecl *");
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001268 else if (ArgName == "NamedArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001269 Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "NamedDecl *");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001270 else if (ArgName == "IdentifierArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001271 Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "IdentifierInfo *");
David Majnemer4bb09802014-02-10 19:50:15 +00001272 else if (ArgName == "DefaultBoolArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001273 Ptr = std::make_unique<DefaultSimpleArgument>(
David Blaikie28f30ca2014-08-08 23:59:38 +00001274 Arg, Attr, "bool", Arg.getValueAsBit("Default"));
1275 else if (ArgName == "BoolArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001276 Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "bool");
Aaron Ballman18a78382013-11-21 00:28:23 +00001277 else if (ArgName == "DefaultIntArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001278 Ptr = std::make_unique<DefaultSimpleArgument>(
David Blaikie28f30ca2014-08-08 23:59:38 +00001279 Arg, Attr, "int", Arg.getValueAsInt("Default"));
1280 else if (ArgName == "IntArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001281 Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "int");
David Blaikie28f30ca2014-08-08 23:59:38 +00001282 else if (ArgName == "StringArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001283 Ptr = std::make_unique<StringArgument>(Arg, Attr);
David Blaikie28f30ca2014-08-08 23:59:38 +00001284 else if (ArgName == "TypeArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001285 Ptr = std::make_unique<TypeArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001286 else if (ArgName == "UnsignedArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001287 Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "unsigned");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001288 else if (ArgName == "VariadicUnsignedArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001289 Ptr = std::make_unique<VariadicArgument>(Arg, Attr, "unsigned");
Peter Collingbourne915df992015-05-15 18:33:32 +00001290 else if (ArgName == "VariadicStringArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001291 Ptr = std::make_unique<VariadicStringArgument>(Arg, Attr);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001292 else if (ArgName == "VariadicEnumArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001293 Ptr = std::make_unique<VariadicEnumArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001294 else if (ArgName == "VariadicExprArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001295 Ptr = std::make_unique<VariadicExprArgument>(Arg, Attr);
Joel E. Denny81508102018-03-13 14:51:22 +00001296 else if (ArgName == "VariadicParamIdxArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001297 Ptr = std::make_unique<VariadicParamIdxArgument>(Arg, Attr);
Johannes Doerfertac991bb2019-01-19 05:36:54 +00001298 else if (ArgName == "VariadicParamOrParamIdxArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001299 Ptr = std::make_unique<VariadicParamOrParamIdxArgument>(Arg, Attr);
Joel E. Denny81508102018-03-13 14:51:22 +00001300 else if (ArgName == "ParamIdxArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001301 Ptr = std::make_unique<SimpleArgument>(Arg, Attr, "ParamIdx");
Erich Keane3efe0022018-07-20 14:13:28 +00001302 else if (ArgName == "VariadicIdentifierArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001303 Ptr = std::make_unique<VariadicIdentifierArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001304 else if (ArgName == "VersionArgument")
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +00001305 Ptr = std::make_unique<VersionArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001306
1307 if (!Ptr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00001308 // Search in reverse order so that the most-derived type is handled first.
Craig Topper25761242016-01-18 19:52:54 +00001309 ArrayRef<std::pair<Record*, SMRange>> Bases = Search->getSuperClasses();
David Majnemerf7e36092016-06-23 00:15:04 +00001310 for (const auto &Base : llvm::reverse(Bases)) {
Craig Topper25761242016-01-18 19:52:54 +00001311 if ((Ptr = createArgument(Arg, Attr, Base.first)))
Peter Collingbournebee583f2011-10-06 13:03:08 +00001312 break;
1313 }
1314 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001315
1316 if (Ptr && Arg.getValueAsBit("Optional"))
1317 Ptr->setOptional(true);
1318
John McCalla62c1a92015-10-28 00:17:34 +00001319 if (Ptr && Arg.getValueAsBit("Fake"))
1320 Ptr->setFake(true);
1321
David Blaikie28f30ca2014-08-08 23:59:38 +00001322 return Ptr;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001323}
1324
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001325static void writeAvailabilityValue(raw_ostream &OS) {
1326 OS << "\" << getPlatform()->getName();\n"
Manman Ren42e09eb2016-03-10 23:54:12 +00001327 << " if (getStrict()) OS << \", strict\";\n"
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001328 << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n"
1329 << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n"
1330 << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n"
1331 << " if (getUnavailable()) OS << \", unavailable\";\n"
1332 << " OS << \"";
1333}
1334
Manman Renc7890fe2016-03-16 18:50:49 +00001335static void writeDeprecatedAttrValue(raw_ostream &OS, std::string &Variety) {
1336 OS << "\\\"\" << getMessage() << \"\\\"\";\n";
1337 // Only GNU deprecated has an optional fixit argument at the second position.
1338 if (Variety == "GNU")
1339 OS << " if (!getReplacement().empty()) OS << \", \\\"\""
1340 " << getReplacement() << \"\\\"\";\n";
1341 OS << " OS << \"";
1342}
1343
Aaron Ballman3e424b52013-12-26 18:30:57 +00001344static void writeGetSpellingFunction(Record &R, raw_ostream &OS) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001345 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballman3e424b52013-12-26 18:30:57 +00001346
1347 OS << "const char *" << R.getName() << "Attr::getSpelling() const {\n";
1348 if (Spellings.empty()) {
1349 OS << " return \"(No spelling)\";\n}\n\n";
1350 return;
1351 }
1352
Erich Keane6a24e802019-09-13 17:39:31 +00001353 OS << " switch (getAttributeSpellingListIndex()) {\n"
Aaron Ballman3e424b52013-12-26 18:30:57 +00001354 " default:\n"
1355 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1356 " return \"(No spelling)\";\n";
1357
1358 for (unsigned I = 0; I < Spellings.size(); ++I)
1359 OS << " case " << I << ":\n"
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001360 " return \"" << Spellings[I].name() << "\";\n";
Aaron Ballman3e424b52013-12-26 18:30:57 +00001361 // End of the switch statement.
1362 OS << " }\n";
1363 // End of the getSpelling function.
1364 OS << "}\n\n";
1365}
1366
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001367static void
1368writePrettyPrintFunction(Record &R,
1369 const std::vector<std::unique_ptr<Argument>> &Args,
1370 raw_ostream &OS) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001371 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Michael Han99315932013-01-24 16:46:58 +00001372
1373 OS << "void " << R.getName() << "Attr::printPretty("
1374 << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n";
1375
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001376 if (Spellings.empty()) {
Michael Han99315932013-01-24 16:46:58 +00001377 OS << "}\n\n";
1378 return;
1379 }
1380
Erich Keane6a24e802019-09-13 17:39:31 +00001381 OS << " switch (getAttributeSpellingListIndex()) {\n"
1382 " default:\n"
1383 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1384 " break;\n";
Michael Han99315932013-01-24 16:46:58 +00001385
1386 for (unsigned I = 0; I < Spellings.size(); ++ I) {
1387 llvm::SmallString<16> Prefix;
1388 llvm::SmallString<8> Suffix;
1389 // The actual spelling of the name and namespace (if applicable)
1390 // of an attribute without considering prefix and suffix.
1391 llvm::SmallString<64> Spelling;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001392 std::string Name = Spellings[I].name();
1393 std::string Variety = Spellings[I].variety();
Michael Han99315932013-01-24 16:46:58 +00001394
1395 if (Variety == "GNU") {
1396 Prefix = " __attribute__((";
1397 Suffix = "))";
Aaron Ballman606093a2017-10-15 15:01:42 +00001398 } else if (Variety == "CXX11" || Variety == "C2x") {
Michael Han99315932013-01-24 16:46:58 +00001399 Prefix = " [[";
1400 Suffix = "]]";
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001401 std::string Namespace = Spellings[I].nameSpace();
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001402 if (!Namespace.empty()) {
Michael Han99315932013-01-24 16:46:58 +00001403 Spelling += Namespace;
1404 Spelling += "::";
1405 }
1406 } else if (Variety == "Declspec") {
1407 Prefix = " __declspec(";
1408 Suffix = ")";
Nico Weber20e08042016-09-03 02:55:10 +00001409 } else if (Variety == "Microsoft") {
1410 Prefix = "[";
1411 Suffix = "]";
Richard Smith0cdcc982013-01-29 01:24:26 +00001412 } else if (Variety == "Keyword") {
1413 Prefix = " ";
1414 Suffix = "";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001415 } else if (Variety == "Pragma") {
1416 Prefix = "#pragma ";
1417 Suffix = "\n";
1418 std::string Namespace = Spellings[I].nameSpace();
1419 if (!Namespace.empty()) {
1420 Spelling += Namespace;
1421 Spelling += " ";
1422 }
Michael Han99315932013-01-24 16:46:58 +00001423 } else {
Richard Smith0cdcc982013-01-29 01:24:26 +00001424 llvm_unreachable("Unknown attribute syntax variety!");
Michael Han99315932013-01-24 16:46:58 +00001425 }
1426
1427 Spelling += Name;
1428
1429 OS <<
1430 " case " << I << " : {\n"
Yaron Keren09fb7c62015-03-10 07:33:23 +00001431 " OS << \"" << Prefix << Spelling;
Michael Han99315932013-01-24 16:46:58 +00001432
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001433 if (Variety == "Pragma") {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +00001434 OS << "\";\n";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001435 OS << " printPrettyPragma(OS, Policy);\n";
Alexey Bataev6d455322015-10-12 06:59:48 +00001436 OS << " OS << \"\\n\";";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001437 OS << " break;\n";
1438 OS << " }\n";
1439 continue;
1440 }
1441
Michael Han99315932013-01-24 16:46:58 +00001442 if (Spelling == "availability") {
Aaron Ballman48a533d2018-02-27 23:49:28 +00001443 OS << "(";
Michael Han99315932013-01-24 16:46:58 +00001444 writeAvailabilityValue(OS);
Aaron Ballman48a533d2018-02-27 23:49:28 +00001445 OS << ")";
Manman Renc7890fe2016-03-16 18:50:49 +00001446 } else if (Spelling == "deprecated" || Spelling == "gnu::deprecated") {
Aaron Ballman48a533d2018-02-27 23:49:28 +00001447 OS << "(";
1448 writeDeprecatedAttrValue(OS, Variety);
1449 OS << ")";
Michael Han99315932013-01-24 16:46:58 +00001450 } else {
Aaron Ballman48a533d2018-02-27 23:49:28 +00001451 // To avoid printing parentheses around an empty argument list or
1452 // printing spurious commas at the end of an argument list, we need to
1453 // determine where the last provided non-fake argument is.
1454 unsigned NonFakeArgs = 0;
1455 unsigned TrailingOptArgs = 0;
1456 bool FoundNonOptArg = false;
1457 for (const auto &arg : llvm::reverse(Args)) {
1458 if (arg->isFake())
1459 continue;
1460 ++NonFakeArgs;
1461 if (FoundNonOptArg)
1462 continue;
1463 // FIXME: arg->getIsOmitted() == "false" means we haven't implemented
1464 // any way to detect whether the argument was omitted.
1465 if (!arg->isOptional() || arg->getIsOmitted() == "false") {
1466 FoundNonOptArg = true;
1467 continue;
1468 }
1469 if (!TrailingOptArgs++)
1470 OS << "\";\n"
1471 << " unsigned TrailingOmittedArgs = 0;\n";
1472 OS << " if (" << arg->getIsOmitted() << ")\n"
1473 << " ++TrailingOmittedArgs;\n";
Michael Han99315932013-01-24 16:46:58 +00001474 }
Aaron Ballman48a533d2018-02-27 23:49:28 +00001475 if (TrailingOptArgs)
1476 OS << " OS << \"";
1477 if (TrailingOptArgs < NonFakeArgs)
1478 OS << "(";
1479 else if (TrailingOptArgs)
1480 OS << "\";\n"
1481 << " if (TrailingOmittedArgs < " << NonFakeArgs << ")\n"
1482 << " OS << \"(\";\n"
1483 << " OS << \"";
1484 unsigned ArgIndex = 0;
1485 for (const auto &arg : Args) {
1486 if (arg->isFake())
1487 continue;
1488 if (ArgIndex) {
1489 if (ArgIndex >= NonFakeArgs - TrailingOptArgs)
1490 OS << "\";\n"
1491 << " if (" << ArgIndex << " < " << NonFakeArgs
1492 << " - TrailingOmittedArgs)\n"
1493 << " OS << \", \";\n"
1494 << " OS << \"";
1495 else
1496 OS << ", ";
1497 }
1498 std::string IsOmitted = arg->getIsOmitted();
1499 if (arg->isOptional() && IsOmitted != "false")
1500 OS << "\";\n"
1501 << " if (!(" << IsOmitted << ")) {\n"
1502 << " OS << \"";
1503 arg->writeValue(OS);
1504 if (arg->isOptional() && IsOmitted != "false")
1505 OS << "\";\n"
1506 << " }\n"
1507 << " OS << \"";
1508 ++ArgIndex;
1509 }
1510 if (TrailingOptArgs < NonFakeArgs)
1511 OS << ")";
1512 else if (TrailingOptArgs)
1513 OS << "\";\n"
1514 << " if (TrailingOmittedArgs < " << NonFakeArgs << ")\n"
1515 << " OS << \")\";\n"
1516 << " OS << \"";
Michael Han99315932013-01-24 16:46:58 +00001517 }
1518
Yaron Keren09fb7c62015-03-10 07:33:23 +00001519 OS << Suffix + "\";\n";
Michael Han99315932013-01-24 16:46:58 +00001520
1521 OS <<
1522 " break;\n"
1523 " }\n";
1524 }
1525
1526 // End of the switch statement.
1527 OS << "}\n";
1528 // End of the print function.
1529 OS << "}\n\n";
1530}
1531
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001532/// Return the index of a spelling in a spelling list.
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001533static unsigned
1534getSpellingListIndex(const std::vector<FlattenedSpelling> &SpellingList,
1535 const FlattenedSpelling &Spelling) {
Alexander Kornienko6ee521c2015-01-23 15:36:10 +00001536 assert(!SpellingList.empty() && "Spelling list is empty!");
Michael Hanaf02bbe2013-02-01 01:19:17 +00001537
1538 for (unsigned Index = 0; Index < SpellingList.size(); ++Index) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001539 const FlattenedSpelling &S = SpellingList[Index];
1540 if (S.variety() != Spelling.variety())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001541 continue;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001542 if (S.nameSpace() != Spelling.nameSpace())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001543 continue;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001544 if (S.name() != Spelling.name())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001545 continue;
1546
1547 return Index;
1548 }
1549
1550 llvm_unreachable("Unknown spelling!");
1551}
1552
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001553static void writeAttrAccessorDefinition(const Record &R, raw_ostream &OS) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00001554 std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors");
George Burgess IV1881a572016-12-01 00:13:18 +00001555 if (Accessors.empty())
1556 return;
1557
1558 const std::vector<FlattenedSpelling> SpellingList = GetFlattenedSpellings(R);
1559 assert(!SpellingList.empty() &&
1560 "Attribute with empty spelling list can't have accessors!");
Aaron Ballman2f22b942014-05-20 19:47:14 +00001561 for (const auto *Accessor : Accessors) {
Erich Keane3bff4142017-10-16 23:25:24 +00001562 const StringRef Name = Accessor->getValueAsString("Name");
George Burgess IV1881a572016-12-01 00:13:18 +00001563 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Accessor);
Michael Hanaf02bbe2013-02-01 01:19:17 +00001564
Erich Keane6a24e802019-09-13 17:39:31 +00001565 OS << " bool " << Name
1566 << "() const { return getAttributeSpellingListIndex() == ";
Michael Hanaf02bbe2013-02-01 01:19:17 +00001567 for (unsigned Index = 0; Index < Spellings.size(); ++Index) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001568 OS << getSpellingListIndex(SpellingList, Spellings[Index]);
George Burgess IV1881a572016-12-01 00:13:18 +00001569 if (Index != Spellings.size() - 1)
Erich Keane6a24e802019-09-13 17:39:31 +00001570 OS << " ||\n getAttributeSpellingListIndex() == ";
Michael Hanaf02bbe2013-02-01 01:19:17 +00001571 else
1572 OS << "; }\n";
1573 }
1574 }
1575}
1576
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001577static bool
1578SpellingNamesAreCommon(const std::vector<FlattenedSpelling>& Spellings) {
Aaron Ballman36a53502014-01-16 13:03:14 +00001579 assert(!Spellings.empty() && "An empty list of spellings was provided");
1580 std::string FirstName = NormalizeNameForSpellingComparison(
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001581 Spellings.front().name());
Aaron Ballman2f22b942014-05-20 19:47:14 +00001582 for (const auto &Spelling :
1583 llvm::make_range(std::next(Spellings.begin()), Spellings.end())) {
1584 std::string Name = NormalizeNameForSpellingComparison(Spelling.name());
Aaron Ballman36a53502014-01-16 13:03:14 +00001585 if (Name != FirstName)
1586 return false;
1587 }
1588 return true;
1589}
1590
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001591typedef std::map<unsigned, std::string> SemanticSpellingMap;
1592static std::string
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001593CreateSemanticSpellings(const std::vector<FlattenedSpelling> &Spellings,
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001594 SemanticSpellingMap &Map) {
1595 // The enumerants are automatically generated based on the variety,
1596 // namespace (if present) and name for each attribute spelling. However,
1597 // care is taken to avoid trampling on the reserved namespace due to
1598 // underscores.
1599 std::string Ret(" enum Spelling {\n");
1600 std::set<std::string> Uniques;
1601 unsigned Idx = 0;
Erich Keane68b09772019-09-17 14:11:51 +00001602
1603 // If we have a need to have this many spellings we likely need to add an
1604 // extra bit to the SpellingIndex in AttributeCommonInfo, then increase the
1605 // value of SpellingNotCalculated there and here.
1606 assert(Spellings.size() < 15 &&
1607 "Too many spellings, would step on SpellingNotCalculated in "
1608 "AttributeCommonInfo");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001609 for (auto I = Spellings.begin(), E = Spellings.end(); I != E; ++I, ++Idx) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001610 const FlattenedSpelling &S = *I;
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00001611 const std::string &Variety = S.variety();
1612 const std::string &Spelling = S.name();
1613 const std::string &Namespace = S.nameSpace();
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001614 std::string EnumName;
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001615
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001616 EnumName += (Variety + "_");
1617 if (!Namespace.empty())
1618 EnumName += (NormalizeNameForSpellingComparison(Namespace).str() +
1619 "_");
1620 EnumName += NormalizeNameForSpellingComparison(Spelling);
1621
1622 // Even if the name is not unique, this spelling index corresponds to a
1623 // particular enumerant name that we've calculated.
1624 Map[Idx] = EnumName;
1625
1626 // Since we have been stripping underscores to avoid trampling on the
1627 // reserved namespace, we may have inadvertently created duplicate
1628 // enumerant names. These duplicates are not considered part of the
1629 // semantic spelling, and can be elided.
1630 if (Uniques.find(EnumName) != Uniques.end())
1631 continue;
1632
1633 Uniques.insert(EnumName);
1634 if (I != Spellings.begin())
1635 Ret += ",\n";
Aaron Ballman9bf6b752015-03-10 17:19:18 +00001636 // Duplicate spellings are not considered part of the semantic spelling
1637 // enumeration, but the spelling index and semantic spelling values are
1638 // meant to be equivalent, so we must specify a concrete value for each
1639 // enumerator.
1640 Ret += " " + EnumName + " = " + llvm::utostr(Idx);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001641 }
Erich Keane68b09772019-09-17 14:11:51 +00001642 Ret += ",\n SpellingNotCalculated = 15\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001643 Ret += "\n };\n\n";
1644 return Ret;
1645}
1646
1647void WriteSemanticSpellingSwitch(const std::string &VarName,
1648 const SemanticSpellingMap &Map,
1649 raw_ostream &OS) {
1650 OS << " switch (" << VarName << ") {\n default: "
1651 << "llvm_unreachable(\"Unknown spelling list index\");\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001652 for (const auto &I : Map)
1653 OS << " case " << I.first << ": return " << I.second << ";\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001654 OS << " }\n";
1655}
1656
Aaron Ballman35db2b32014-01-29 22:13:45 +00001657// Emits the LateParsed property for attributes.
1658static void emitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) {
1659 OS << "#if defined(CLANG_ATTR_LATE_PARSED_LIST)\n";
1660 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1661
Aaron Ballman2f22b942014-05-20 19:47:14 +00001662 for (const auto *Attr : Attrs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001663 bool LateParsed = Attr->getValueAsBit("LateParsed");
Aaron Ballman35db2b32014-01-29 22:13:45 +00001664
1665 if (LateParsed) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001666 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
Aaron Ballman35db2b32014-01-29 22:13:45 +00001667
1668 // FIXME: Handle non-GNU attributes
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001669 for (const auto &I : Spellings) {
1670 if (I.variety() != "GNU")
Aaron Ballman35db2b32014-01-29 22:13:45 +00001671 continue;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001672 OS << ".Case(\"" << I.name() << "\", " << LateParsed << ")\n";
Aaron Ballman35db2b32014-01-29 22:13:45 +00001673 }
1674 }
1675 }
1676 OS << "#endif // CLANG_ATTR_LATE_PARSED_LIST\n\n";
1677}
1678
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001679static bool hasGNUorCXX11Spelling(const Record &Attribute) {
1680 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attribute);
1681 for (const auto &I : Spellings) {
1682 if (I.variety() == "GNU" || I.variety() == "CXX11")
1683 return true;
1684 }
1685 return false;
1686}
1687
1688namespace {
1689
1690struct AttributeSubjectMatchRule {
1691 const Record *MetaSubject;
1692 const Record *Constraint;
1693
1694 AttributeSubjectMatchRule(const Record *MetaSubject, const Record *Constraint)
1695 : MetaSubject(MetaSubject), Constraint(Constraint) {
1696 assert(MetaSubject && "Missing subject");
1697 }
1698
1699 bool isSubRule() const { return Constraint != nullptr; }
1700
1701 std::vector<Record *> getSubjects() const {
1702 return (Constraint ? Constraint : MetaSubject)
1703 ->getValueAsListOfDefs("Subjects");
1704 }
1705
1706 std::vector<Record *> getLangOpts() const {
1707 if (Constraint) {
1708 // Lookup the options in the sub-rule first, in case the sub-rule
1709 // overrides the rules options.
1710 std::vector<Record *> Opts = Constraint->getValueAsListOfDefs("LangOpts");
1711 if (!Opts.empty())
1712 return Opts;
1713 }
1714 return MetaSubject->getValueAsListOfDefs("LangOpts");
1715 }
1716
1717 // Abstract rules are used only for sub-rules
1718 bool isAbstractRule() const { return getSubjects().empty(); }
1719
Erich Keane3bff4142017-10-16 23:25:24 +00001720 StringRef getName() const {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001721 return (Constraint ? Constraint : MetaSubject)->getValueAsString("Name");
1722 }
1723
1724 bool isNegatedSubRule() const {
1725 assert(isSubRule() && "Not a sub-rule");
1726 return Constraint->getValueAsBit("Negated");
1727 }
1728
1729 std::string getSpelling() const {
1730 std::string Result = MetaSubject->getValueAsString("Name");
1731 if (isSubRule()) {
1732 Result += '(';
1733 if (isNegatedSubRule())
1734 Result += "unless(";
1735 Result += getName();
1736 if (isNegatedSubRule())
1737 Result += ')';
1738 Result += ')';
1739 }
1740 return Result;
1741 }
1742
1743 std::string getEnumValueName() const {
Craig Topper00648582017-05-31 19:01:22 +00001744 SmallString<128> Result;
1745 Result += "SubjectMatchRule_";
1746 Result += MetaSubject->getValueAsString("Name");
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001747 if (isSubRule()) {
1748 Result += "_";
1749 if (isNegatedSubRule())
1750 Result += "not_";
1751 Result += Constraint->getValueAsString("Name");
1752 }
1753 if (isAbstractRule())
1754 Result += "_abstract";
Craig Topper00648582017-05-31 19:01:22 +00001755 return Result.str();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001756 }
1757
1758 std::string getEnumValue() const { return "attr::" + getEnumValueName(); }
1759
1760 static const char *EnumName;
1761};
1762
1763const char *AttributeSubjectMatchRule::EnumName = "attr::SubjectMatchRule";
1764
1765struct PragmaClangAttributeSupport {
1766 std::vector<AttributeSubjectMatchRule> Rules;
Alex Lorenz24952fb2017-04-19 15:52:11 +00001767
1768 class RuleOrAggregateRuleSet {
1769 std::vector<AttributeSubjectMatchRule> Rules;
1770 bool IsRule;
1771 RuleOrAggregateRuleSet(ArrayRef<AttributeSubjectMatchRule> Rules,
1772 bool IsRule)
1773 : Rules(Rules), IsRule(IsRule) {}
1774
1775 public:
1776 bool isRule() const { return IsRule; }
1777
1778 const AttributeSubjectMatchRule &getRule() const {
1779 assert(IsRule && "not a rule!");
1780 return Rules[0];
1781 }
1782
1783 ArrayRef<AttributeSubjectMatchRule> getAggregateRuleSet() const {
1784 return Rules;
1785 }
1786
1787 static RuleOrAggregateRuleSet
1788 getRule(const AttributeSubjectMatchRule &Rule) {
1789 return RuleOrAggregateRuleSet(Rule, /*IsRule=*/true);
1790 }
1791 static RuleOrAggregateRuleSet
1792 getAggregateRuleSet(ArrayRef<AttributeSubjectMatchRule> Rules) {
1793 return RuleOrAggregateRuleSet(Rules, /*IsRule=*/false);
1794 }
1795 };
1796 llvm::DenseMap<const Record *, RuleOrAggregateRuleSet> SubjectsToRules;
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001797
1798 PragmaClangAttributeSupport(RecordKeeper &Records);
1799
1800 bool isAttributedSupported(const Record &Attribute);
1801
1802 void emitMatchRuleList(raw_ostream &OS);
1803
1804 std::string generateStrictConformsTo(const Record &Attr, raw_ostream &OS);
1805
1806 void generateParsingHelpers(raw_ostream &OS);
1807};
1808
1809} // end anonymous namespace
1810
Alex Lorenz24952fb2017-04-19 15:52:11 +00001811static bool doesDeclDeriveFrom(const Record *D, const Record *Base) {
John McCallbaf91d02019-10-25 16:28:03 -07001812 const Record *CurrentBase = D->getValueAsOptionalDef(BaseFieldName);
Alex Lorenz24952fb2017-04-19 15:52:11 +00001813 if (!CurrentBase)
1814 return false;
1815 if (CurrentBase == Base)
1816 return true;
1817 return doesDeclDeriveFrom(CurrentBase, Base);
1818}
1819
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001820PragmaClangAttributeSupport::PragmaClangAttributeSupport(
1821 RecordKeeper &Records) {
1822 std::vector<Record *> MetaSubjects =
1823 Records.getAllDerivedDefinitions("AttrSubjectMatcherRule");
1824 auto MapFromSubjectsToRules = [this](const Record *SubjectContainer,
1825 const Record *MetaSubject,
Saleem Abdulrasoolbe4773c2017-05-01 00:26:59 +00001826 const Record *Constraint) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001827 Rules.emplace_back(MetaSubject, Constraint);
1828 std::vector<Record *> ApplicableSubjects =
1829 SubjectContainer->getValueAsListOfDefs("Subjects");
1830 for (const auto *Subject : ApplicableSubjects) {
1831 bool Inserted =
Alex Lorenz24952fb2017-04-19 15:52:11 +00001832 SubjectsToRules
1833 .try_emplace(Subject, RuleOrAggregateRuleSet::getRule(
1834 AttributeSubjectMatchRule(MetaSubject,
1835 Constraint)))
1836 .second;
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001837 if (!Inserted) {
1838 PrintFatalError("Attribute subject match rules should not represent"
1839 "same attribute subjects.");
1840 }
1841 }
1842 };
1843 for (const auto *MetaSubject : MetaSubjects) {
Saleem Abdulrasoolbe4773c2017-05-01 00:26:59 +00001844 MapFromSubjectsToRules(MetaSubject, MetaSubject, /*Constraints=*/nullptr);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001845 std::vector<Record *> Constraints =
1846 MetaSubject->getValueAsListOfDefs("Constraints");
1847 for (const auto *Constraint : Constraints)
1848 MapFromSubjectsToRules(Constraint, MetaSubject, Constraint);
1849 }
Alex Lorenz24952fb2017-04-19 15:52:11 +00001850
1851 std::vector<Record *> Aggregates =
1852 Records.getAllDerivedDefinitions("AttrSubjectMatcherAggregateRule");
John McCallbaf91d02019-10-25 16:28:03 -07001853 std::vector<Record *> DeclNodes =
1854 Records.getAllDerivedDefinitions(DeclNodeClassName);
Alex Lorenz24952fb2017-04-19 15:52:11 +00001855 for (const auto *Aggregate : Aggregates) {
1856 Record *SubjectDecl = Aggregate->getValueAsDef("Subject");
1857
1858 // Gather sub-classes of the aggregate subject that act as attribute
1859 // subject rules.
1860 std::vector<AttributeSubjectMatchRule> Rules;
1861 for (const auto *D : DeclNodes) {
1862 if (doesDeclDeriveFrom(D, SubjectDecl)) {
1863 auto It = SubjectsToRules.find(D);
1864 if (It == SubjectsToRules.end())
1865 continue;
1866 if (!It->second.isRule() || It->second.getRule().isSubRule())
1867 continue; // Assume that the rule will be included as well.
1868 Rules.push_back(It->second.getRule());
1869 }
1870 }
1871
1872 bool Inserted =
1873 SubjectsToRules
1874 .try_emplace(SubjectDecl,
1875 RuleOrAggregateRuleSet::getAggregateRuleSet(Rules))
1876 .second;
1877 if (!Inserted) {
1878 PrintFatalError("Attribute subject match rules should not represent"
1879 "same attribute subjects.");
1880 }
1881 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001882}
1883
1884static PragmaClangAttributeSupport &
1885getPragmaAttributeSupport(RecordKeeper &Records) {
1886 static PragmaClangAttributeSupport Instance(Records);
1887 return Instance;
1888}
1889
1890void PragmaClangAttributeSupport::emitMatchRuleList(raw_ostream &OS) {
1891 OS << "#ifndef ATTR_MATCH_SUB_RULE\n";
1892 OS << "#define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, "
1893 "IsNegated) "
1894 << "ATTR_MATCH_RULE(Value, Spelling, IsAbstract)\n";
1895 OS << "#endif\n";
1896 for (const auto &Rule : Rules) {
1897 OS << (Rule.isSubRule() ? "ATTR_MATCH_SUB_RULE" : "ATTR_MATCH_RULE") << '(';
1898 OS << Rule.getEnumValueName() << ", \"" << Rule.getSpelling() << "\", "
1899 << Rule.isAbstractRule();
1900 if (Rule.isSubRule())
1901 OS << ", "
1902 << AttributeSubjectMatchRule(Rule.MetaSubject, nullptr).getEnumValue()
1903 << ", " << Rule.isNegatedSubRule();
1904 OS << ")\n";
1905 }
1906 OS << "#undef ATTR_MATCH_SUB_RULE\n";
1907}
1908
1909bool PragmaClangAttributeSupport::isAttributedSupported(
1910 const Record &Attribute) {
Richard Smith1bb64532018-08-30 01:01:07 +00001911 // If the attribute explicitly specified whether to support #pragma clang
1912 // attribute, use that setting.
1913 bool Unset;
1914 bool SpecifiedResult =
1915 Attribute.getValueAsBitOrUnset("PragmaAttributeSupport", Unset);
1916 if (!Unset)
1917 return SpecifiedResult;
1918
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001919 // Opt-out rules:
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001920 // An attribute requires delayed parsing (LateParsed is on)
1921 if (Attribute.getValueAsBit("LateParsed"))
1922 return false;
1923 // An attribute has no GNU/CXX11 spelling
1924 if (!hasGNUorCXX11Spelling(Attribute))
1925 return false;
1926 // An attribute subject list has a subject that isn't covered by one of the
1927 // subject match rules or has no subjects at all.
1928 if (Attribute.isValueUnset("Subjects"))
1929 return false;
1930 const Record *SubjectObj = Attribute.getValueAsDef("Subjects");
1931 std::vector<Record *> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
1932 if (Subjects.empty())
1933 return false;
1934 for (const auto *Subject : Subjects) {
1935 if (SubjectsToRules.find(Subject) == SubjectsToRules.end())
1936 return false;
1937 }
1938 return true;
1939}
1940
John McCall2c91c3b2019-05-30 04:09:01 +00001941static std::string GenerateTestExpression(ArrayRef<Record *> LangOpts) {
1942 std::string Test;
1943
1944 for (auto *E : LangOpts) {
1945 if (!Test.empty())
1946 Test += " || ";
1947
1948 const StringRef Code = E->getValueAsString("CustomCode");
1949 if (!Code.empty()) {
1950 Test += "(";
1951 Test += Code;
1952 Test += ")";
1953 } else {
1954 Test += "LangOpts.";
1955 Test += E->getValueAsString("Name");
1956 }
1957 }
1958
1959 if (Test.empty())
1960 return "true";
1961
1962 return Test;
1963}
1964
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001965std::string
1966PragmaClangAttributeSupport::generateStrictConformsTo(const Record &Attr,
1967 raw_ostream &OS) {
1968 if (!isAttributedSupported(Attr))
1969 return "nullptr";
1970 // Generate a function that constructs a set of matching rules that describe
1971 // to which declarations the attribute should apply to.
1972 std::string FnName = "matchRulesFor" + Attr.getName().str();
Erich Keane3bff4142017-10-16 23:25:24 +00001973 OS << "static void " << FnName << "(llvm::SmallVectorImpl<std::pair<"
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001974 << AttributeSubjectMatchRule::EnumName
1975 << ", bool>> &MatchRules, const LangOptions &LangOpts) {\n";
1976 if (Attr.isValueUnset("Subjects")) {
Erich Keane3bff4142017-10-16 23:25:24 +00001977 OS << "}\n\n";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001978 return FnName;
1979 }
1980 const Record *SubjectObj = Attr.getValueAsDef("Subjects");
1981 std::vector<Record *> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
1982 for (const auto *Subject : Subjects) {
1983 auto It = SubjectsToRules.find(Subject);
1984 assert(It != SubjectsToRules.end() &&
1985 "This attribute is unsupported by #pragma clang attribute");
Alex Lorenz24952fb2017-04-19 15:52:11 +00001986 for (const auto &Rule : It->getSecond().getAggregateRuleSet()) {
1987 // The rule might be language specific, so only subtract it from the given
1988 // rules if the specific language options are specified.
1989 std::vector<Record *> LangOpts = Rule.getLangOpts();
Erich Keane3bff4142017-10-16 23:25:24 +00001990 OS << " MatchRules.push_back(std::make_pair(" << Rule.getEnumValue()
John McCall2c91c3b2019-05-30 04:09:01 +00001991 << ", /*IsSupported=*/" << GenerateTestExpression(LangOpts)
1992 << "));\n";
Alex Lorenz24952fb2017-04-19 15:52:11 +00001993 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001994 }
Erich Keane3bff4142017-10-16 23:25:24 +00001995 OS << "}\n\n";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001996 return FnName;
1997}
1998
1999void PragmaClangAttributeSupport::generateParsingHelpers(raw_ostream &OS) {
2000 // Generate routines that check the names of sub-rules.
2001 OS << "Optional<attr::SubjectMatchRule> "
2002 "defaultIsAttributeSubjectMatchSubRuleFor(StringRef, bool) {\n";
2003 OS << " return None;\n";
2004 OS << "}\n\n";
2005
2006 std::map<const Record *, std::vector<AttributeSubjectMatchRule>>
2007 SubMatchRules;
2008 for (const auto &Rule : Rules) {
2009 if (!Rule.isSubRule())
2010 continue;
2011 SubMatchRules[Rule.MetaSubject].push_back(Rule);
2012 }
2013
2014 for (const auto &SubMatchRule : SubMatchRules) {
2015 OS << "Optional<attr::SubjectMatchRule> isAttributeSubjectMatchSubRuleFor_"
2016 << SubMatchRule.first->getValueAsString("Name")
2017 << "(StringRef Name, bool IsUnless) {\n";
2018 OS << " if (IsUnless)\n";
2019 OS << " return "
2020 "llvm::StringSwitch<Optional<attr::SubjectMatchRule>>(Name).\n";
2021 for (const auto &Rule : SubMatchRule.second) {
2022 if (Rule.isNegatedSubRule())
2023 OS << " Case(\"" << Rule.getName() << "\", " << Rule.getEnumValue()
2024 << ").\n";
2025 }
2026 OS << " Default(None);\n";
2027 OS << " return "
2028 "llvm::StringSwitch<Optional<attr::SubjectMatchRule>>(Name).\n";
2029 for (const auto &Rule : SubMatchRule.second) {
2030 if (!Rule.isNegatedSubRule())
2031 OS << " Case(\"" << Rule.getName() << "\", " << Rule.getEnumValue()
2032 << ").\n";
2033 }
2034 OS << " Default(None);\n";
2035 OS << "}\n\n";
2036 }
2037
2038 // Generate the function that checks for the top-level rules.
2039 OS << "std::pair<Optional<attr::SubjectMatchRule>, "
2040 "Optional<attr::SubjectMatchRule> (*)(StringRef, "
2041 "bool)> isAttributeSubjectMatchRule(StringRef Name) {\n";
2042 OS << " return "
2043 "llvm::StringSwitch<std::pair<Optional<attr::SubjectMatchRule>, "
2044 "Optional<attr::SubjectMatchRule> (*) (StringRef, "
2045 "bool)>>(Name).\n";
2046 for (const auto &Rule : Rules) {
2047 if (Rule.isSubRule())
2048 continue;
2049 std::string SubRuleFunction;
2050 if (SubMatchRules.count(Rule.MetaSubject))
Erich Keane3bff4142017-10-16 23:25:24 +00002051 SubRuleFunction =
2052 ("isAttributeSubjectMatchSubRuleFor_" + Rule.getName()).str();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00002053 else
2054 SubRuleFunction = "defaultIsAttributeSubjectMatchSubRuleFor";
2055 OS << " Case(\"" << Rule.getName() << "\", std::make_pair("
2056 << Rule.getEnumValue() << ", " << SubRuleFunction << ")).\n";
2057 }
2058 OS << " Default(std::make_pair(None, "
2059 "defaultIsAttributeSubjectMatchSubRuleFor));\n";
2060 OS << "}\n\n";
2061
2062 // Generate the function that checks for the submatch rules.
2063 OS << "const char *validAttributeSubjectMatchSubRules("
2064 << AttributeSubjectMatchRule::EnumName << " Rule) {\n";
2065 OS << " switch (Rule) {\n";
2066 for (const auto &SubMatchRule : SubMatchRules) {
2067 OS << " case "
2068 << AttributeSubjectMatchRule(SubMatchRule.first, nullptr).getEnumValue()
2069 << ":\n";
2070 OS << " return \"'";
2071 bool IsFirst = true;
2072 for (const auto &Rule : SubMatchRule.second) {
2073 if (!IsFirst)
2074 OS << ", '";
2075 IsFirst = false;
2076 if (Rule.isNegatedSubRule())
2077 OS << "unless(";
2078 OS << Rule.getName();
2079 if (Rule.isNegatedSubRule())
2080 OS << ')';
2081 OS << "'";
2082 }
2083 OS << "\";\n";
2084 }
2085 OS << " default: return nullptr;\n";
2086 OS << " }\n";
2087 OS << "}\n\n";
2088}
2089
George Burgess IV1881a572016-12-01 00:13:18 +00002090template <typename Fn>
2091static void forEachUniqueSpelling(const Record &Attr, Fn &&F) {
2092 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
2093 SmallDenseSet<StringRef, 8> Seen;
2094 for (const FlattenedSpelling &S : Spellings) {
2095 if (Seen.insert(S.name()).second)
2096 F(S);
2097 }
2098}
2099
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002100/// Emits the first-argument-is-type property for attributes.
Aaron Ballman35db2b32014-01-29 22:13:45 +00002101static void emitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) {
2102 OS << "#if defined(CLANG_ATTR_TYPE_ARG_LIST)\n";
2103 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
2104
Aaron Ballman2f22b942014-05-20 19:47:14 +00002105 for (const auto *Attr : Attrs) {
Aaron Ballman35db2b32014-01-29 22:13:45 +00002106 // Determine whether the first argument is a type.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002107 std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
Aaron Ballman35db2b32014-01-29 22:13:45 +00002108 if (Args.empty())
2109 continue;
2110
Craig Topper25761242016-01-18 19:52:54 +00002111 if (Args[0]->getSuperClasses().back().first->getName() != "TypeArgument")
Aaron Ballman35db2b32014-01-29 22:13:45 +00002112 continue;
2113
2114 // All these spellings take a single type argument.
George Burgess IV1881a572016-12-01 00:13:18 +00002115 forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) {
2116 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
2117 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00002118 }
2119 OS << "#endif // CLANG_ATTR_TYPE_ARG_LIST\n\n";
2120}
2121
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002122/// Emits the parse-arguments-in-unevaluated-context property for
Aaron Ballman35db2b32014-01-29 22:13:45 +00002123/// attributes.
2124static void emitClangAttrArgContextList(RecordKeeper &Records, raw_ostream &OS) {
2125 OS << "#if defined(CLANG_ATTR_ARG_CONTEXT_LIST)\n";
2126 ParsedAttrMap Attrs = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002127 for (const auto &I : Attrs) {
2128 const Record &Attr = *I.second;
Aaron Ballman35db2b32014-01-29 22:13:45 +00002129
2130 if (!Attr.getValueAsBit("ParseArgumentsAsUnevaluated"))
2131 continue;
2132
2133 // All these spellings take are parsed unevaluated.
George Burgess IV1881a572016-12-01 00:13:18 +00002134 forEachUniqueSpelling(Attr, [&](const FlattenedSpelling &S) {
2135 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
2136 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00002137 }
2138 OS << "#endif // CLANG_ATTR_ARG_CONTEXT_LIST\n\n";
2139}
2140
2141static bool isIdentifierArgument(Record *Arg) {
2142 return !Arg->getSuperClasses().empty() &&
Craig Topper25761242016-01-18 19:52:54 +00002143 llvm::StringSwitch<bool>(Arg->getSuperClasses().back().first->getName())
Aaron Ballman35db2b32014-01-29 22:13:45 +00002144 .Case("IdentifierArgument", true)
2145 .Case("EnumArgument", true)
Aaron Ballman55ef1512014-12-19 16:42:04 +00002146 .Case("VariadicEnumArgument", true)
Aaron Ballman35db2b32014-01-29 22:13:45 +00002147 .Default(false);
2148}
2149
Erich Keane3efe0022018-07-20 14:13:28 +00002150static bool isVariadicIdentifierArgument(Record *Arg) {
2151 return !Arg->getSuperClasses().empty() &&
2152 llvm::StringSwitch<bool>(
2153 Arg->getSuperClasses().back().first->getName())
2154 .Case("VariadicIdentifierArgument", true)
Johannes Doerfertac991bb2019-01-19 05:36:54 +00002155 .Case("VariadicParamOrParamIdxArgument", true)
Erich Keane3efe0022018-07-20 14:13:28 +00002156 .Default(false);
2157}
2158
2159static void emitClangAttrVariadicIdentifierArgList(RecordKeeper &Records,
2160 raw_ostream &OS) {
2161 OS << "#if defined(CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST)\n";
2162 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
2163 for (const auto *A : Attrs) {
2164 // Determine whether the first argument is a variadic identifier.
2165 std::vector<Record *> Args = A->getValueAsListOfDefs("Args");
2166 if (Args.empty() || !isVariadicIdentifierArgument(Args[0]))
2167 continue;
2168
2169 // All these spellings take an identifier argument.
2170 forEachUniqueSpelling(*A, [&](const FlattenedSpelling &S) {
2171 OS << ".Case(\"" << S.name() << "\", "
2172 << "true"
2173 << ")\n";
2174 });
2175 }
2176 OS << "#endif // CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST\n\n";
2177}
2178
Aaron Ballman35db2b32014-01-29 22:13:45 +00002179// Emits the first-argument-is-identifier property for attributes.
2180static void emitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) {
2181 OS << "#if defined(CLANG_ATTR_IDENTIFIER_ARG_LIST)\n";
2182 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2183
Aaron Ballman2f22b942014-05-20 19:47:14 +00002184 for (const auto *Attr : Attrs) {
Aaron Ballman35db2b32014-01-29 22:13:45 +00002185 // Determine whether the first argument is an identifier.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002186 std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
Aaron Ballman35db2b32014-01-29 22:13:45 +00002187 if (Args.empty() || !isIdentifierArgument(Args[0]))
2188 continue;
2189
2190 // All these spellings take an identifier argument.
George Burgess IV1881a572016-12-01 00:13:18 +00002191 forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) {
2192 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
2193 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00002194 }
2195 OS << "#endif // CLANG_ATTR_IDENTIFIER_ARG_LIST\n\n";
2196}
2197
Johannes Doerfertac991bb2019-01-19 05:36:54 +00002198static bool keywordThisIsaIdentifierInArgument(const Record *Arg) {
2199 return !Arg->getSuperClasses().empty() &&
2200 llvm::StringSwitch<bool>(
2201 Arg->getSuperClasses().back().first->getName())
2202 .Case("VariadicParamOrParamIdxArgument", true)
2203 .Default(false);
2204}
2205
2206static void emitClangAttrThisIsaIdentifierArgList(RecordKeeper &Records,
2207 raw_ostream &OS) {
2208 OS << "#if defined(CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST)\n";
2209 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
2210 for (const auto *A : Attrs) {
2211 // Determine whether the first argument is a variadic identifier.
2212 std::vector<Record *> Args = A->getValueAsListOfDefs("Args");
2213 if (Args.empty() || !keywordThisIsaIdentifierInArgument(Args[0]))
2214 continue;
2215
2216 // All these spellings take an identifier argument.
2217 forEachUniqueSpelling(*A, [&](const FlattenedSpelling &S) {
2218 OS << ".Case(\"" << S.name() << "\", "
2219 << "true"
2220 << ")\n";
2221 });
2222 }
2223 OS << "#endif // CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST\n\n";
2224}
2225
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002226// Emits the class definitions for attributes.
John McCallc45f8d42019-10-01 23:12:57 +00002227void clang::EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002228 emitSourceFileHeader("Attribute classes' definitions", OS);
2229
Peter Collingbournebee583f2011-10-06 13:03:08 +00002230 OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
2231 OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
2232
2233 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
Erich Keane6a24e802019-09-13 17:39:31 +00002234 ParsedAttrMap AttrMap = getParsedAttrList(Records);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002235
Aaron Ballman2f22b942014-05-20 19:47:14 +00002236 for (const auto *Attr : Attrs) {
2237 const Record &R = *Attr;
Aaron Ballman06bd44b2014-02-17 18:23:02 +00002238
2239 // FIXME: Currently, documentation is generated as-needed due to the fact
2240 // that there is no way to allow a generated project "reach into" the docs
2241 // directory (for instance, it may be an out-of-tree build). However, we want
2242 // to ensure that every attribute has a Documentation field, and produce an
2243 // error if it has been neglected. Otherwise, the on-demand generation which
2244 // happens server-side will fail. This code is ensuring that functionality,
2245 // even though this Emitter doesn't technically need the documentation.
2246 // When attribute documentation can be generated as part of the build
2247 // itself, this code can be removed.
2248 (void)R.getValueAsListOfDefs("Documentation");
Douglas Gregorb2daf842012-05-02 15:56:52 +00002249
2250 if (!R.getValueAsBit("ASTNode"))
2251 continue;
2252
Craig Topper25761242016-01-18 19:52:54 +00002253 ArrayRef<std::pair<Record *, SMRange>> Supers = R.getSuperClasses();
Aaron Ballman0979e9e2013-07-30 01:44:15 +00002254 assert(!Supers.empty() && "Forgot to specify a superclass for the attr");
Aaron Ballman0979e9e2013-07-30 01:44:15 +00002255 std::string SuperName;
Richard Smith33bddbd2018-01-04 23:42:29 +00002256 bool Inheritable = false;
David Majnemerf7e36092016-06-23 00:15:04 +00002257 for (const auto &Super : llvm::reverse(Supers)) {
Craig Topper25761242016-01-18 19:52:54 +00002258 const Record *R = Super.first;
Aaron Ballmanb9a457a2018-05-03 15:33:50 +00002259 if (R->getName() != "TargetSpecificAttr" &&
2260 R->getName() != "DeclOrTypeAttr" && SuperName.empty())
Craig Topper25761242016-01-18 19:52:54 +00002261 SuperName = R->getName();
Richard Smith33bddbd2018-01-04 23:42:29 +00002262 if (R->getName() == "InheritableAttr")
2263 Inheritable = true;
Aaron Ballman0979e9e2013-07-30 01:44:15 +00002264 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00002265
2266 OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
2267
2268 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002269 std::vector<std::unique_ptr<Argument>> Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002270 Args.reserve(ArgRecords.size());
2271
John McCalla62c1a92015-10-28 00:17:34 +00002272 bool HasOptArg = false;
2273 bool HasFakeArg = false;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002274 for (const auto *ArgRecord : ArgRecords) {
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002275 Args.emplace_back(createArgument(*ArgRecord, R.getName()));
2276 Args.back()->writeDeclarations(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002277 OS << "\n\n";
John McCalla62c1a92015-10-28 00:17:34 +00002278
2279 // For these purposes, fake takes priority over optional.
2280 if (Args.back()->isFake()) {
2281 HasFakeArg = true;
2282 } else if (Args.back()->isOptional()) {
2283 HasOptArg = true;
2284 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00002285 }
2286
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002287 OS << "public:\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002288
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002289 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballman36a53502014-01-16 13:03:14 +00002290
2291 // If there are zero or one spellings, all spelling-related functionality
2292 // can be elided. If all of the spellings share the same name, the spelling
2293 // functionality can also be elided.
2294 bool ElideSpelling = (Spellings.size() <= 1) ||
2295 SpellingNamesAreCommon(Spellings);
2296
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002297 // This maps spelling index values to semantic Spelling enumerants.
2298 SemanticSpellingMap SemanticToSyntacticMap;
Aaron Ballman36a53502014-01-16 13:03:14 +00002299
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002300 if (!ElideSpelling)
2301 OS << CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
Aaron Ballman36a53502014-01-16 13:03:14 +00002302
Erich Keane6a24e802019-09-13 17:39:31 +00002303 const auto &ParsedAttrSpellingItr = llvm::find_if(
2304 AttrMap, [R](const std::pair<std::string, const Record *> &P) {
2305 return &R == P.second;
2306 });
2307
John McCalla62c1a92015-10-28 00:17:34 +00002308 // Emit CreateImplicit factory methods.
Erich Keane6a24e802019-09-13 17:39:31 +00002309 auto emitCreate = [&](bool Implicit, bool emitFake) {
2310 OS << " static " << R.getName() << "Attr *Create";
2311 if (Implicit)
2312 OS << "Implicit";
2313 OS << "(";
John McCalla62c1a92015-10-28 00:17:34 +00002314 OS << "ASTContext &Ctx";
John McCalla62c1a92015-10-28 00:17:34 +00002315 for (auto const &ai : Args) {
2316 if (ai->isFake() && !emitFake) continue;
2317 OS << ", ";
2318 ai->writeCtorParameters(OS);
2319 }
Erich Keane6a24e802019-09-13 17:39:31 +00002320 OS << ", const AttributeCommonInfo &CommonInfo = {SourceRange{}}) {\n";
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002321 OS << " auto *A = new (Ctx) " << R.getName();
Erich Keane6a24e802019-09-13 17:39:31 +00002322 OS << "Attr(Ctx, CommonInfo";
John McCalla62c1a92015-10-28 00:17:34 +00002323 for (auto const &ai : Args) {
2324 if (ai->isFake() && !emitFake) continue;
John McCalla62c1a92015-10-28 00:17:34 +00002325 OS << ", ";
Erich Keane6a24e802019-09-13 17:39:31 +00002326 ai->writeImplicitCtorArgs(OS);
John McCalla62c1a92015-10-28 00:17:34 +00002327 }
Erich Keane6a24e802019-09-13 17:39:31 +00002328 OS << ");\n";
2329 if (Implicit) {
2330 OS << " A->setImplicit(true);\n";
2331 }
2332 if (Implicit || ElideSpelling) {
2333 OS << " if (!A->isAttributeSpellingListCalculated() && "
2334 "!A->getAttrName())\n";
2335 OS << " A->setAttributeSpellingListIndex(0);\n";
2336 }
John McCalla62c1a92015-10-28 00:17:34 +00002337 OS << " return A;\n }\n\n";
2338 };
Aaron Ballman36a53502014-01-16 13:03:14 +00002339
Erich Keane6a24e802019-09-13 17:39:31 +00002340 auto emitCreateNoCI = [&](bool Implicit, bool emitFake) {
2341 OS <<" static " << R.getName() << "Attr *Create";
2342 if (Implicit)
2343 OS << "Implicit";
2344 OS << "(";
2345 OS << "ASTContext &Ctx";
2346 for (auto const &ai : Args) {
2347 if (ai->isFake() && !emitFake) continue;
2348 OS << ", ";
2349 ai->writeCtorParameters(OS);
2350 }
2351 OS << ", SourceRange Range, AttributeCommonInfo::Syntax Syntax";
2352 if (!ElideSpelling)
2353 OS << ", " << R.getName()
Erich Keanef9cd3812019-09-13 17:56:38 +00002354 << "Attr::Spelling S = "
Erich Keane6a24e802019-09-13 17:39:31 +00002355 "static_cast<Spelling>(SpellingNotCalculated)";
2356 OS << ") {\n";
2357 OS << " AttributeCommonInfo I(Range, ";
2358
2359 if (ParsedAttrSpellingItr != std::end(AttrMap))
2360 OS << "AT_" << ParsedAttrSpellingItr->first;
2361 else
2362 OS << "NoSemaHandlerAttribute";
2363
2364 OS << ", Syntax";
2365 if (!ElideSpelling)
Erich Keanef9cd3812019-09-13 17:56:38 +00002366 OS << ", S";
Erich Keane6a24e802019-09-13 17:39:31 +00002367 OS << ");\n";
2368 OS << " return Create";
2369 if (Implicit)
2370 OS << "Implicit";
2371 OS << "(Ctx";
2372 for (auto const &ai : Args) {
2373 if (ai->isFake() && !emitFake) continue;
2374 OS << ", ";
2375 ai->writeImplicitCtorArgs(OS);
2376 }
2377 OS << ", I);\n";
2378 OS << " }\n";
2379 };
2380
2381 auto emitCreates = [&](bool emitFake) {
2382 emitCreate(true, emitFake);
2383 emitCreate(false, emitFake);
2384 emitCreateNoCI(true, emitFake);
2385 emitCreateNoCI(false, emitFake);
2386 };
2387
John McCalla62c1a92015-10-28 00:17:34 +00002388 // Emit a CreateImplicit that takes all the arguments.
Erich Keane6a24e802019-09-13 17:39:31 +00002389 emitCreates(true);
John McCalla62c1a92015-10-28 00:17:34 +00002390
2391 // Emit a CreateImplicit that takes all the non-fake arguments.
Erich Keane6a24e802019-09-13 17:39:31 +00002392 if (HasFakeArg)
2393 emitCreates(false);
Michael Han99315932013-01-24 16:46:58 +00002394
John McCalla62c1a92015-10-28 00:17:34 +00002395 // Emit constructors.
2396 auto emitCtor = [&](bool emitOpt, bool emitFake) {
2397 auto shouldEmitArg = [=](const std::unique_ptr<Argument> &arg) {
2398 if (arg->isFake()) return emitFake;
2399 if (arg->isOptional()) return emitOpt;
2400 return true;
2401 };
Erich Keane6a24e802019-09-13 17:39:31 +00002402 OS << " " << R.getName()
2403 << "Attr(ASTContext &Ctx, const AttributeCommonInfo &CommonInfo";
2404 OS << '\n';
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002405 for (auto const &ai : Args) {
John McCalla62c1a92015-10-28 00:17:34 +00002406 if (!shouldEmitArg(ai)) continue;
2407 OS << " , ";
2408 ai->writeCtorParameters(OS);
2409 OS << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002410 }
2411
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002412 OS << " )\n";
Erich Keane6a24e802019-09-13 17:39:31 +00002413 OS << " : " << SuperName << "(Ctx, CommonInfo, ";
2414 OS << "attr::" << R.getName() << ", "
2415 << (R.getValueAsBit("LateParsed") ? "true" : "false");
Richard Smith33bddbd2018-01-04 23:42:29 +00002416 if (Inheritable) {
2417 OS << ", "
2418 << (R.getValueAsBit("InheritEvenIfAlreadyPresent") ? "true"
2419 : "false");
2420 }
2421 OS << ")\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002422
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002423 for (auto const &ai : Args) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002424 OS << " , ";
John McCalla62c1a92015-10-28 00:17:34 +00002425 if (!shouldEmitArg(ai)) {
2426 ai->writeCtorDefaultInitializers(OS);
2427 } else {
2428 ai->writeCtorInitializers(OS);
2429 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002430 OS << "\n";
2431 }
2432
2433 OS << " {\n";
2434
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002435 for (auto const &ai : Args) {
John McCalla62c1a92015-10-28 00:17:34 +00002436 if (!shouldEmitArg(ai)) continue;
2437 ai->writeCtorBody(OS);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002438 }
2439 OS << " }\n\n";
John McCalla62c1a92015-10-28 00:17:34 +00002440 };
2441
2442 // Emit a constructor that includes all the arguments.
2443 // This is necessary for cloning.
2444 emitCtor(true, true);
2445
2446 // Emit a constructor that takes all the non-fake arguments.
Erich Keane6a24e802019-09-13 17:39:31 +00002447 if (HasFakeArg)
John McCalla62c1a92015-10-28 00:17:34 +00002448 emitCtor(true, false);
John McCalla62c1a92015-10-28 00:17:34 +00002449
2450 // Emit a constructor that takes all the non-fake, non-optional arguments.
Erich Keane6a24e802019-09-13 17:39:31 +00002451 if (HasOptArg)
John McCalla62c1a92015-10-28 00:17:34 +00002452 emitCtor(false, false);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002453
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002454 OS << " " << R.getName() << "Attr *clone(ASTContext &C) const;\n";
Craig Toppercbce6e92014-03-11 06:22:39 +00002455 OS << " void printPretty(raw_ostream &OS,\n"
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002456 << " const PrintingPolicy &Policy) const;\n";
2457 OS << " const char *getSpelling() const;\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002458
2459 if (!ElideSpelling) {
2460 assert(!SemanticToSyntacticMap.empty() && "Empty semantic mapping list");
2461 OS << " Spelling getSemanticSpelling() const {\n";
Erich Keane6a24e802019-09-13 17:39:31 +00002462 WriteSemanticSpellingSwitch("getAttributeSpellingListIndex()",
2463 SemanticToSyntacticMap, OS);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002464 OS << " }\n";
2465 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00002466
Michael Hanaf02bbe2013-02-01 01:19:17 +00002467 writeAttrAccessorDefinition(R, OS);
2468
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002469 for (auto const &ai : Args) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002470 ai->writeAccessors(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002471 OS << "\n\n";
Aaron Ballman682ee422013-09-11 19:47:58 +00002472
John McCalla62c1a92015-10-28 00:17:34 +00002473 // Don't write conversion routines for fake arguments.
2474 if (ai->isFake()) continue;
2475
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002476 if (ai->isEnumArg())
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002477 static_cast<const EnumArgument *>(ai.get())->writeConversion(OS);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002478 else if (ai->isVariadicEnumArg())
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002479 static_cast<const VariadicEnumArgument *>(ai.get())
2480 ->writeConversion(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002481 }
2482
Jakob Stoklund Olesen6f2288b62012-01-13 04:57:47 +00002483 OS << R.getValueAsString("AdditionalMembers");
Peter Collingbournebee583f2011-10-06 13:03:08 +00002484 OS << "\n\n";
2485
2486 OS << " static bool classof(const Attr *A) { return A->getKind() == "
2487 << "attr::" << R.getName() << "; }\n";
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00002488
Peter Collingbournebee583f2011-10-06 13:03:08 +00002489 OS << "};\n\n";
2490 }
2491
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002492 OS << "#endif // LLVM_CLANG_ATTR_CLASSES_INC\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002493}
2494
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002495// Emits the class method definitions for attributes.
John McCallc45f8d42019-10-01 23:12:57 +00002496void clang::EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002497 emitSourceFileHeader("Attribute classes' member function definitions", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002498
2499 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
Peter Collingbournebee583f2011-10-06 13:03:08 +00002500
Aaron Ballman2f22b942014-05-20 19:47:14 +00002501 for (auto *Attr : Attrs) {
2502 Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002503
2504 if (!R.getValueAsBit("ASTNode"))
2505 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002506
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002507 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
2508 std::vector<std::unique_ptr<Argument>> Args;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002509 for (const auto *Arg : ArgRecords)
2510 Args.emplace_back(createArgument(*Arg, R.getName()));
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002511
2512 for (auto const &ai : Args)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002513 ai->writeAccessorDefinitions(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002514
2515 OS << R.getName() << "Attr *" << R.getName()
2516 << "Attr::clone(ASTContext &C) const {\n";
Erich Keane6a24e802019-09-13 17:39:31 +00002517 OS << " auto *A = new (C) " << R.getName() << "Attr(C, *this";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002518 for (auto const &ai : Args) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00002519 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002520 ai->writeCloneArgs(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002521 }
Erich Keane6a24e802019-09-13 17:39:31 +00002522 OS << ");\n";
Hans Wennborg613807b2014-05-31 01:30:30 +00002523 OS << " A->Inherited = Inherited;\n";
2524 OS << " A->IsPackExpansion = IsPackExpansion;\n";
Erich Keane6a24e802019-09-13 17:39:31 +00002525 OS << " A->setImplicit(Implicit);\n";
Hans Wennborg613807b2014-05-31 01:30:30 +00002526 OS << " return A;\n}\n\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00002527
Michael Han99315932013-01-24 16:46:58 +00002528 writePrettyPrintFunction(R, Args, OS);
Aaron Ballman3e424b52013-12-26 18:30:57 +00002529 writeGetSpellingFunction(R, OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002530 }
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002531
2532 // Instead of relying on virtual dispatch we just create a huge dispatch
2533 // switch. This is both smaller and faster than virtual functions.
2534 auto EmitFunc = [&](const char *Method) {
2535 OS << " switch (getKind()) {\n";
2536 for (const auto *Attr : Attrs) {
2537 const Record &R = *Attr;
2538 if (!R.getValueAsBit("ASTNode"))
2539 continue;
2540
2541 OS << " case attr::" << R.getName() << ":\n";
2542 OS << " return cast<" << R.getName() << "Attr>(this)->" << Method
2543 << ";\n";
2544 }
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002545 OS << " }\n";
2546 OS << " llvm_unreachable(\"Unexpected attribute kind!\");\n";
2547 OS << "}\n\n";
2548 };
2549
2550 OS << "const char *Attr::getSpelling() const {\n";
2551 EmitFunc("getSpelling()");
2552
2553 OS << "Attr *Attr::clone(ASTContext &C) const {\n";
2554 EmitFunc("clone(C)");
2555
2556 OS << "void Attr::printPretty(raw_ostream &OS, "
2557 "const PrintingPolicy &Policy) const {\n";
2558 EmitFunc("printPretty(OS, Policy)");
Peter Collingbournebee583f2011-10-06 13:03:08 +00002559}
2560
John McCall2225c8b2016-03-01 00:18:05 +00002561static void emitAttrList(raw_ostream &OS, StringRef Class,
Peter Collingbournebee583f2011-10-06 13:03:08 +00002562 const std::vector<Record*> &AttrList) {
John McCall2225c8b2016-03-01 00:18:05 +00002563 for (auto Cur : AttrList) {
2564 OS << Class << "(" << Cur->getName() << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002565 }
2566}
2567
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002568// Determines if an attribute has a Pragma spelling.
2569static bool AttrHasPragmaSpelling(const Record *R) {
2570 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
George Burgess IV1881a572016-12-01 00:13:18 +00002571 return llvm::find_if(Spellings, [](const FlattenedSpelling &S) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002572 return S.variety() == "Pragma";
2573 }) != Spellings.end();
2574}
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002575
John McCall2225c8b2016-03-01 00:18:05 +00002576namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002577
John McCall2225c8b2016-03-01 00:18:05 +00002578 struct AttrClassDescriptor {
2579 const char * const MacroName;
2580 const char * const TableGenName;
2581 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002582
2583} // end anonymous namespace
John McCall2225c8b2016-03-01 00:18:05 +00002584
2585static const AttrClassDescriptor AttrClassDescriptors[] = {
2586 { "ATTR", "Attr" },
Richard Smithe43e2b32018-08-20 21:47:29 +00002587 { "TYPE_ATTR", "TypeAttr" },
Richard Smith4f902c72016-03-08 00:32:55 +00002588 { "STMT_ATTR", "StmtAttr" },
John McCall2225c8b2016-03-01 00:18:05 +00002589 { "INHERITABLE_ATTR", "InheritableAttr" },
Richard Smithe43e2b32018-08-20 21:47:29 +00002590 { "DECL_OR_TYPE_ATTR", "DeclOrTypeAttr" },
John McCall477f2bb2016-03-03 06:39:32 +00002591 { "INHERITABLE_PARAM_ATTR", "InheritableParamAttr" },
2592 { "PARAMETER_ABI_ATTR", "ParameterABIAttr" }
John McCall2225c8b2016-03-01 00:18:05 +00002593};
2594
2595static void emitDefaultDefine(raw_ostream &OS, StringRef name,
2596 const char *superName) {
2597 OS << "#ifndef " << name << "\n";
2598 OS << "#define " << name << "(NAME) ";
2599 if (superName) OS << superName << "(NAME)";
2600 OS << "\n#endif\n\n";
2601}
2602
2603namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002604
John McCall2225c8b2016-03-01 00:18:05 +00002605 /// A class of attributes.
2606 struct AttrClass {
2607 const AttrClassDescriptor &Descriptor;
2608 Record *TheRecord;
2609 AttrClass *SuperClass = nullptr;
2610 std::vector<AttrClass*> SubClasses;
2611 std::vector<Record*> Attrs;
2612
2613 AttrClass(const AttrClassDescriptor &Descriptor, Record *R)
2614 : Descriptor(Descriptor), TheRecord(R) {}
2615
2616 void emitDefaultDefines(raw_ostream &OS) const {
2617 // Default the macro unless this is a root class (i.e. Attr).
2618 if (SuperClass) {
2619 emitDefaultDefine(OS, Descriptor.MacroName,
2620 SuperClass->Descriptor.MacroName);
2621 }
2622 }
2623
2624 void emitUndefs(raw_ostream &OS) const {
2625 OS << "#undef " << Descriptor.MacroName << "\n";
2626 }
2627
2628 void emitAttrList(raw_ostream &OS) const {
2629 for (auto SubClass : SubClasses) {
2630 SubClass->emitAttrList(OS);
2631 }
2632
2633 ::emitAttrList(OS, Descriptor.MacroName, Attrs);
2634 }
2635
2636 void classifyAttrOnRoot(Record *Attr) {
2637 bool result = classifyAttr(Attr);
2638 assert(result && "failed to classify on root"); (void) result;
2639 }
2640
2641 void emitAttrRange(raw_ostream &OS) const {
2642 OS << "ATTR_RANGE(" << Descriptor.TableGenName
2643 << ", " << getFirstAttr()->getName()
2644 << ", " << getLastAttr()->getName() << ")\n";
2645 }
2646
2647 private:
2648 bool classifyAttr(Record *Attr) {
2649 // Check all the subclasses.
2650 for (auto SubClass : SubClasses) {
2651 if (SubClass->classifyAttr(Attr))
2652 return true;
2653 }
2654
2655 // It's not more specific than this class, but it might still belong here.
2656 if (Attr->isSubClassOf(TheRecord)) {
2657 Attrs.push_back(Attr);
2658 return true;
2659 }
2660
2661 return false;
2662 }
2663
2664 Record *getFirstAttr() const {
2665 if (!SubClasses.empty())
2666 return SubClasses.front()->getFirstAttr();
2667 return Attrs.front();
2668 }
2669
2670 Record *getLastAttr() const {
2671 if (!Attrs.empty())
2672 return Attrs.back();
2673 return SubClasses.back()->getLastAttr();
2674 }
2675 };
2676
2677 /// The entire hierarchy of attribute classes.
2678 class AttrClassHierarchy {
2679 std::vector<std::unique_ptr<AttrClass>> Classes;
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002680
John McCall2225c8b2016-03-01 00:18:05 +00002681 public:
2682 AttrClassHierarchy(RecordKeeper &Records) {
2683 // Find records for all the classes.
2684 for (auto &Descriptor : AttrClassDescriptors) {
2685 Record *ClassRecord = Records.getClass(Descriptor.TableGenName);
2686 AttrClass *Class = new AttrClass(Descriptor, ClassRecord);
2687 Classes.emplace_back(Class);
2688 }
2689
2690 // Link up the hierarchy.
2691 for (auto &Class : Classes) {
2692 if (AttrClass *SuperClass = findSuperClass(Class->TheRecord)) {
2693 Class->SuperClass = SuperClass;
2694 SuperClass->SubClasses.push_back(Class.get());
2695 }
2696 }
2697
2698#ifndef NDEBUG
2699 for (auto i = Classes.begin(), e = Classes.end(); i != e; ++i) {
2700 assert((i == Classes.begin()) == ((*i)->SuperClass == nullptr) &&
2701 "only the first class should be a root class!");
2702 }
2703#endif
2704 }
2705
2706 void emitDefaultDefines(raw_ostream &OS) const {
2707 for (auto &Class : Classes) {
2708 Class->emitDefaultDefines(OS);
2709 }
2710 }
2711
2712 void emitUndefs(raw_ostream &OS) const {
2713 for (auto &Class : Classes) {
2714 Class->emitUndefs(OS);
2715 }
2716 }
2717
2718 void emitAttrLists(raw_ostream &OS) const {
2719 // Just start from the root class.
2720 Classes[0]->emitAttrList(OS);
2721 }
2722
2723 void emitAttrRanges(raw_ostream &OS) const {
2724 for (auto &Class : Classes)
2725 Class->emitAttrRange(OS);
2726 }
2727
2728 void classifyAttr(Record *Attr) {
2729 // Add the attribute to the root class.
2730 Classes[0]->classifyAttrOnRoot(Attr);
2731 }
2732
2733 private:
2734 AttrClass *findClassByRecord(Record *R) const {
2735 for (auto &Class : Classes) {
2736 if (Class->TheRecord == R)
2737 return Class.get();
2738 }
2739 return nullptr;
2740 }
2741
2742 AttrClass *findSuperClass(Record *R) const {
2743 // TableGen flattens the superclass list, so we just need to walk it
2744 // in reverse.
2745 auto SuperClasses = R->getSuperClasses();
2746 for (signed i = 0, e = SuperClasses.size(); i != e; ++i) {
2747 auto SuperClass = findClassByRecord(SuperClasses[e - i - 1].first);
2748 if (SuperClass) return SuperClass;
2749 }
2750 return nullptr;
2751 }
2752 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002753
2754} // end anonymous namespace
John McCall2225c8b2016-03-01 00:18:05 +00002755
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002756namespace clang {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002757
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002758// Emits the enumeration list for attributes.
2759void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002760 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002761
John McCall2225c8b2016-03-01 00:18:05 +00002762 AttrClassHierarchy Hierarchy(Records);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002763
John McCall2225c8b2016-03-01 00:18:05 +00002764 // Add defaulting macro definitions.
2765 Hierarchy.emitDefaultDefines(OS);
2766 emitDefaultDefine(OS, "PRAGMA_SPELLING_ATTR", nullptr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002767
John McCall2225c8b2016-03-01 00:18:05 +00002768 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
2769 std::vector<Record *> PragmaAttrs;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002770 for (auto *Attr : Attrs) {
2771 if (!Attr->getValueAsBit("ASTNode"))
Douglas Gregorb2daf842012-05-02 15:56:52 +00002772 continue;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002773
John McCall2225c8b2016-03-01 00:18:05 +00002774 // Add the attribute to the ad-hoc groups.
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002775 if (AttrHasPragmaSpelling(Attr))
2776 PragmaAttrs.push_back(Attr);
2777
John McCall2225c8b2016-03-01 00:18:05 +00002778 // Place it in the hierarchy.
2779 Hierarchy.classifyAttr(Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002780 }
2781
John McCall2225c8b2016-03-01 00:18:05 +00002782 // Emit the main attribute list.
2783 Hierarchy.emitAttrLists(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002784
John McCall2225c8b2016-03-01 00:18:05 +00002785 // Emit the ad hoc groups.
2786 emitAttrList(OS, "PRAGMA_SPELLING_ATTR", PragmaAttrs);
2787
2788 // Emit the attribute ranges.
2789 OS << "#ifdef ATTR_RANGE\n";
2790 Hierarchy.emitAttrRanges(OS);
2791 OS << "#undef ATTR_RANGE\n";
2792 OS << "#endif\n";
2793
2794 Hierarchy.emitUndefs(OS);
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002795 OS << "#undef PRAGMA_SPELLING_ATTR\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002796}
2797
Alex Lorenz9e7bf162017-04-18 14:33:39 +00002798// Emits the enumeration list for attributes.
2799void EmitClangAttrSubjectMatchRuleList(RecordKeeper &Records, raw_ostream &OS) {
2800 emitSourceFileHeader(
2801 "List of all attribute subject matching rules that Clang recognizes", OS);
2802 PragmaClangAttributeSupport &PragmaAttributeSupport =
2803 getPragmaAttributeSupport(Records);
2804 emitDefaultDefine(OS, "ATTR_MATCH_RULE", nullptr);
2805 PragmaAttributeSupport.emitMatchRuleList(OS);
2806 OS << "#undef ATTR_MATCH_RULE\n";
2807}
2808
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002809// Emits the code to read an attribute from a precompiled header.
2810void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002811 emitSourceFileHeader("Attribute deserialization code", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002812
2813 Record *InhClass = Records.getClass("InheritableAttr");
2814 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
2815 ArgRecords;
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002816 std::vector<std::unique_ptr<Argument>> Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002817
2818 OS << " switch (Kind) {\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002819 for (const auto *Attr : Attrs) {
2820 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002821 if (!R.getValueAsBit("ASTNode"))
2822 continue;
Erich Keane6a24e802019-09-13 17:39:31 +00002823
Peter Collingbournebee583f2011-10-06 13:03:08 +00002824 OS << " case attr::" << R.getName() << ": {\n";
2825 if (R.isSubClassOf(InhClass))
David L. Jones267b8842017-01-24 01:04:30 +00002826 OS << " bool isInherited = Record.readInt();\n";
2827 OS << " bool isImplicit = Record.readInt();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002828 ArgRecords = R.getValueAsListOfDefs("Args");
2829 Args.clear();
Aaron Ballman2f22b942014-05-20 19:47:14 +00002830 for (const auto *Arg : ArgRecords) {
2831 Args.emplace_back(createArgument(*Arg, R.getName()));
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002832 Args.back()->writePCHReadDecls(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002833 }
Erich Keane6a24e802019-09-13 17:39:31 +00002834 OS << " New = new (Context) " << R.getName() << "Attr(Context, Info";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002835 for (auto const &ri : Args) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00002836 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002837 ri->writePCHReadArgs(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002838 }
Erich Keane6a24e802019-09-13 17:39:31 +00002839 OS << ");\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002840 if (R.isSubClassOf(InhClass))
2841 OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002842 OS << " New->setImplicit(isImplicit);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002843 OS << " break;\n";
2844 OS << " }\n";
2845 }
2846 OS << " }\n";
2847}
2848
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002849// Emits the code to write an attribute to a precompiled header.
2850void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002851 emitSourceFileHeader("Attribute serialization code", OS);
2852
Peter Collingbournebee583f2011-10-06 13:03:08 +00002853 Record *InhClass = Records.getClass("InheritableAttr");
2854 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002855
2856 OS << " switch (A->getKind()) {\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002857 for (const auto *Attr : Attrs) {
2858 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002859 if (!R.getValueAsBit("ASTNode"))
2860 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002861 OS << " case attr::" << R.getName() << ": {\n";
2862 Args = R.getValueAsListOfDefs("Args");
2863 if (R.isSubClassOf(InhClass) || !Args.empty())
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002864 OS << " const auto *SA = cast<" << R.getName()
Peter Collingbournebee583f2011-10-06 13:03:08 +00002865 << "Attr>(A);\n";
2866 if (R.isSubClassOf(InhClass))
2867 OS << " Record.push_back(SA->isInherited());\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002868 OS << " Record.push_back(A->isImplicit());\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002869
Aaron Ballman2f22b942014-05-20 19:47:14 +00002870 for (const auto *Arg : Args)
2871 createArgument(*Arg, R.getName())->writePCHWrite(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002872 OS << " break;\n";
2873 OS << " }\n";
2874 }
2875 OS << " }\n";
2876}
2877
Erich Keane75449672017-12-20 18:51:08 +00002878// Helper function for GenerateTargetSpecificAttrChecks that alters the 'Test'
2879// parameter with only a single check type, if applicable.
Richard Smith78b239e2019-06-20 20:44:45 +00002880static bool GenerateTargetSpecificAttrCheck(const Record *R, std::string &Test,
Erich Keane75449672017-12-20 18:51:08 +00002881 std::string *FnName,
2882 StringRef ListName,
2883 StringRef CheckAgainst,
2884 StringRef Scope) {
2885 if (!R->isValueUnset(ListName)) {
2886 Test += " && (";
2887 std::vector<StringRef> Items = R->getValueAsListOfStrings(ListName);
2888 for (auto I = Items.begin(), E = Items.end(); I != E; ++I) {
2889 StringRef Part = *I;
2890 Test += CheckAgainst;
2891 Test += " == ";
2892 Test += Scope;
2893 Test += Part;
2894 if (I + 1 != E)
2895 Test += " || ";
2896 if (FnName)
2897 *FnName += Part;
2898 }
2899 Test += ")";
Richard Smith78b239e2019-06-20 20:44:45 +00002900 return true;
Erich Keane75449672017-12-20 18:51:08 +00002901 }
Richard Smith78b239e2019-06-20 20:44:45 +00002902 return false;
Erich Keane75449672017-12-20 18:51:08 +00002903}
2904
Bob Wilson0058b822015-07-20 22:57:36 +00002905// Generate a conditional expression to check if the current target satisfies
2906// the conditions for a TargetSpecificAttr record, and append the code for
2907// those checks to the Test string. If the FnName string pointer is non-null,
2908// append a unique suffix to distinguish this set of target checks from other
2909// TargetSpecificAttr records.
Richard Smith78b239e2019-06-20 20:44:45 +00002910static bool GenerateTargetSpecificAttrChecks(const Record *R,
Craig Topper00648582017-05-31 19:01:22 +00002911 std::vector<StringRef> &Arches,
Bob Wilson0058b822015-07-20 22:57:36 +00002912 std::string &Test,
2913 std::string *FnName) {
Richard Smith78b239e2019-06-20 20:44:45 +00002914 bool AnyTargetChecks = false;
2915
Bob Wilson0058b822015-07-20 22:57:36 +00002916 // It is assumed that there will be an llvm::Triple object
2917 // named "T" and a TargetInfo object named "Target" within
2918 // scope that can be used to determine whether the attribute exists in
2919 // a given target.
Erich Keane75449672017-12-20 18:51:08 +00002920 Test += "true";
2921 // If one or more architectures is specified, check those. Arches are handled
2922 // differently because GenerateTargetRequirements needs to combine the list
2923 // with ParseKind.
2924 if (!Arches.empty()) {
Richard Smith78b239e2019-06-20 20:44:45 +00002925 AnyTargetChecks = true;
Erich Keane75449672017-12-20 18:51:08 +00002926 Test += " && (";
2927 for (auto I = Arches.begin(), E = Arches.end(); I != E; ++I) {
2928 StringRef Part = *I;
2929 Test += "T.getArch() == llvm::Triple::";
2930 Test += Part;
2931 if (I + 1 != E)
2932 Test += " || ";
2933 if (FnName)
2934 *FnName += Part;
2935 }
2936 Test += ")";
Bob Wilson0058b822015-07-20 22:57:36 +00002937 }
Bob Wilson0058b822015-07-20 22:57:36 +00002938
2939 // If the attribute is specific to particular OSes, check those.
Richard Smith78b239e2019-06-20 20:44:45 +00002940 AnyTargetChecks |= GenerateTargetSpecificAttrCheck(
2941 R, Test, FnName, "OSes", "T.getOS()", "llvm::Triple::");
Bob Wilson0058b822015-07-20 22:57:36 +00002942
Erich Keane75449672017-12-20 18:51:08 +00002943 // If one or more object formats is specified, check those.
Richard Smith78b239e2019-06-20 20:44:45 +00002944 AnyTargetChecks |=
2945 GenerateTargetSpecificAttrCheck(R, Test, FnName, "ObjectFormats",
2946 "T.getObjectFormat()", "llvm::Triple::");
2947
2948 // If custom code is specified, emit it.
2949 StringRef Code = R->getValueAsString("CustomCode");
2950 if (!Code.empty()) {
2951 AnyTargetChecks = true;
2952 Test += " && (";
2953 Test += Code;
2954 Test += ")";
2955 }
2956
2957 return AnyTargetChecks;
Bob Wilson0058b822015-07-20 22:57:36 +00002958}
2959
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002960static void GenerateHasAttrSpellingStringSwitch(
2961 const std::vector<Record *> &Attrs, raw_ostream &OS,
2962 const std::string &Variety = "", const std::string &Scope = "") {
2963 for (const auto *Attr : Attrs) {
Aaron Ballmana0344c52014-11-14 13:44:02 +00002964 // C++11-style attributes have specific version information associated with
2965 // them. If the attribute has no scope, the version information must not
2966 // have the default value (1), as that's incorrect. Instead, the unscoped
2967 // attribute version information should be taken from the SD-6 standing
2968 // document, which can be found at:
2969 // https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations
2970 int Version = 1;
2971
2972 if (Variety == "CXX11") {
2973 std::vector<Record *> Spellings = Attr->getValueAsListOfDefs("Spellings");
2974 for (const auto &Spelling : Spellings) {
2975 if (Spelling->getValueAsString("Variety") == "CXX11") {
2976 Version = static_cast<int>(Spelling->getValueAsInt("Version"));
2977 if (Scope.empty() && Version == 1)
2978 PrintError(Spelling->getLoc(), "C++ standard attributes must "
2979 "have valid version information.");
2980 break;
2981 }
2982 }
2983 }
2984
Aaron Ballman0fa06d82014-01-09 22:57:44 +00002985 std::string Test;
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002986 if (Attr->isSubClassOf("TargetSpecificAttr")) {
2987 const Record *R = Attr->getValueAsDef("Target");
Craig Topper00648582017-05-31 19:01:22 +00002988 std::vector<StringRef> Arches = R->getValueAsListOfStrings("Arches");
Hans Wennborgdcfba332015-10-06 23:40:43 +00002989 GenerateTargetSpecificAttrChecks(R, Arches, Test, nullptr);
Bob Wilson7c730832015-07-20 22:57:31 +00002990
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002991 // If this is the C++11 variety, also add in the LangOpts test.
2992 if (Variety == "CXX11")
2993 Test += " && LangOpts.CPlusPlus11";
Aaron Ballman606093a2017-10-15 15:01:42 +00002994 else if (Variety == "C2x")
2995 Test += " && LangOpts.DoubleSquareBracketAttributes";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002996 } else if (Variety == "CXX11")
2997 // C++11 mode should be checked against LangOpts, which is presumed to be
2998 // present in the caller.
2999 Test = "LangOpts.CPlusPlus11";
Aaron Ballman606093a2017-10-15 15:01:42 +00003000 else if (Variety == "C2x")
3001 Test = "LangOpts.DoubleSquareBracketAttributes";
Aaron Ballman0fa06d82014-01-09 22:57:44 +00003002
Aaron Ballmana0344c52014-11-14 13:44:02 +00003003 std::string TestStr =
Aaron Ballman28afa182014-11-17 18:17:19 +00003004 !Test.empty() ? Test + " ? " + llvm::itostr(Version) + " : 0" : "1";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00003005 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003006 for (const auto &S : Spellings)
Aaron Ballman2fbf9942014-03-31 13:14:44 +00003007 if (Variety.empty() || (Variety == S.variety() &&
3008 (Scope.empty() || Scope == S.nameSpace())))
Aaron Ballmana0344c52014-11-14 13:44:02 +00003009 OS << " .Case(\"" << S.name() << "\", " << TestStr << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00003010 }
Aaron Ballmana0344c52014-11-14 13:44:02 +00003011 OS << " .Default(0);\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00003012}
3013
3014// Emits the list of spellings for attributes.
3015void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
3016 emitSourceFileHeader("Code to implement the __has_attribute logic", OS);
3017
3018 // Separate all of the attributes out into four group: generic, C++11, GNU,
3019 // and declspecs. Then generate a big switch statement for each of them.
3020 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Nico Weber20e08042016-09-03 02:55:10 +00003021 std::vector<Record *> Declspec, Microsoft, GNU, Pragma;
Aaron Ballman606093a2017-10-15 15:01:42 +00003022 std::map<std::string, std::vector<Record *>> CXX, C2x;
Aaron Ballman2fbf9942014-03-31 13:14:44 +00003023
3024 // Walk over the list of all attributes, and split them out based on the
3025 // spelling variety.
3026 for (auto *R : Attrs) {
3027 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
3028 for (const auto &SI : Spellings) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00003029 const std::string &Variety = SI.variety();
Aaron Ballman2fbf9942014-03-31 13:14:44 +00003030 if (Variety == "GNU")
3031 GNU.push_back(R);
3032 else if (Variety == "Declspec")
3033 Declspec.push_back(R);
Nico Weber20e08042016-09-03 02:55:10 +00003034 else if (Variety == "Microsoft")
3035 Microsoft.push_back(R);
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003036 else if (Variety == "CXX11")
Aaron Ballman2fbf9942014-03-31 13:14:44 +00003037 CXX[SI.nameSpace()].push_back(R);
Aaron Ballman606093a2017-10-15 15:01:42 +00003038 else if (Variety == "C2x")
3039 C2x[SI.nameSpace()].push_back(R);
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003040 else if (Variety == "Pragma")
3041 Pragma.push_back(R);
Aaron Ballman2fbf9942014-03-31 13:14:44 +00003042 }
3043 }
3044
Bob Wilson7c730832015-07-20 22:57:31 +00003045 OS << "const llvm::Triple &T = Target.getTriple();\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00003046 OS << "switch (Syntax) {\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00003047 OS << "case AttrSyntax::GNU:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00003048 OS << " return llvm::StringSwitch<int>(Name)\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00003049 GenerateHasAttrSpellingStringSwitch(GNU, OS, "GNU");
3050 OS << "case AttrSyntax::Declspec:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00003051 OS << " return llvm::StringSwitch<int>(Name)\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00003052 GenerateHasAttrSpellingStringSwitch(Declspec, OS, "Declspec");
Nico Weber20e08042016-09-03 02:55:10 +00003053 OS << "case AttrSyntax::Microsoft:\n";
3054 OS << " return llvm::StringSwitch<int>(Name)\n";
3055 GenerateHasAttrSpellingStringSwitch(Microsoft, OS, "Microsoft");
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003056 OS << "case AttrSyntax::Pragma:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00003057 OS << " return llvm::StringSwitch<int>(Name)\n";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003058 GenerateHasAttrSpellingStringSwitch(Pragma, OS, "Pragma");
Aaron Ballman606093a2017-10-15 15:01:42 +00003059 auto fn = [&OS](const char *Spelling, const char *Variety,
3060 const std::map<std::string, std::vector<Record *>> &List) {
3061 OS << "case AttrSyntax::" << Variety << ": {\n";
3062 // C++11-style attributes are further split out based on the Scope.
3063 for (auto I = List.cbegin(), E = List.cend(); I != E; ++I) {
Stephen Kellydb8fac12019-01-11 19:16:01 +00003064 if (I != List.cbegin())
3065 OS << " else ";
3066 if (I->first.empty())
3067 OS << "if (ScopeName == \"\") {\n";
3068 else
3069 OS << "if (ScopeName == \"" << I->first << "\") {\n";
3070 OS << " return llvm::StringSwitch<int>(Name)\n";
3071 GenerateHasAttrSpellingStringSwitch(I->second, OS, Spelling, I->first);
3072 OS << "}";
Aaron Ballman606093a2017-10-15 15:01:42 +00003073 }
Aaron Ballman4ff3b5ab2017-10-18 12:11:58 +00003074 OS << "\n} break;\n";
Aaron Ballman606093a2017-10-15 15:01:42 +00003075 };
3076 fn("CXX11", "CXX", CXX);
3077 fn("C2x", "C", C2x);
Aaron Ballman2fbf9942014-03-31 13:14:44 +00003078 OS << "}\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00003079}
3080
Michael Han99315932013-01-24 16:46:58 +00003081void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00003082 emitSourceFileHeader("Code to translate different attribute spellings "
3083 "into internal identifiers", OS);
Michael Han99315932013-01-24 16:46:58 +00003084
Erich Keane6a24e802019-09-13 17:39:31 +00003085 OS << " switch (getParsedKind()) {\n";
3086 OS << " case IgnoredAttribute:\n";
3087 OS << " case UnknownAttribute:\n";
3088 OS << " case NoSemaHandlerAttribute:\n";
3089 OS << " llvm_unreachable(\"Ignored/unknown shouldn't get here\");\n";
Michael Han99315932013-01-24 16:46:58 +00003090
Aaron Ballman64e69862013-12-15 13:05:48 +00003091 ParsedAttrMap Attrs = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003092 for (const auto &I : Attrs) {
Aaron Ballman2f22b942014-05-20 19:47:14 +00003093 const Record &R = *I.second;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003094 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003095 OS << " case AT_" << I.first << ": {\n";
Richard Smith852e9ce2013-11-27 01:46:48 +00003096 for (unsigned I = 0; I < Spellings.size(); ++ I) {
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003097 OS << " if (Name == \"" << Spellings[I].name() << "\" && "
Erich Keane6a24e802019-09-13 17:39:31 +00003098 << "getSyntax() == AttributeCommonInfo::AS_" << Spellings[I].variety()
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003099 << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n"
3100 << " return " << I << ";\n";
Michael Han99315932013-01-24 16:46:58 +00003101 }
Richard Smith852e9ce2013-11-27 01:46:48 +00003102
3103 OS << " break;\n";
3104 OS << " }\n";
Michael Han99315932013-01-24 16:46:58 +00003105 }
3106
3107 OS << " }\n";
Aaron Ballman64e69862013-12-15 13:05:48 +00003108 OS << " return 0;\n";
Michael Han99315932013-01-24 16:46:58 +00003109}
3110
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003111// Emits code used by RecursiveASTVisitor to visit attributes
3112void EmitClangAttrASTVisitor(RecordKeeper &Records, raw_ostream &OS) {
3113 emitSourceFileHeader("Used by RecursiveASTVisitor to visit attributes.", OS);
3114
3115 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
3116
3117 // Write method declarations for Traverse* methods.
3118 // We emit this here because we only generate methods for attributes that
3119 // are declared as ASTNodes.
3120 OS << "#ifdef ATTR_VISITOR_DECLS_ONLY\n\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00003121 for (const auto *Attr : Attrs) {
3122 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003123 if (!R.getValueAsBit("ASTNode"))
3124 continue;
3125 OS << " bool Traverse"
3126 << R.getName() << "Attr(" << R.getName() << "Attr *A);\n";
3127 OS << " bool Visit"
3128 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
3129 << " return true; \n"
Hans Wennborg4afe5042015-07-22 20:46:26 +00003130 << " }\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003131 }
3132 OS << "\n#else // ATTR_VISITOR_DECLS_ONLY\n\n";
3133
3134 // Write individual Traverse* methods for each attribute class.
Aaron Ballman2f22b942014-05-20 19:47:14 +00003135 for (const auto *Attr : Attrs) {
3136 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003137 if (!R.getValueAsBit("ASTNode"))
3138 continue;
3139
3140 OS << "template <typename Derived>\n"
DeLesley Hutchinsbb79c332013-12-30 21:03:02 +00003141 << "bool VISITORCLASS<Derived>::Traverse"
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003142 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
3143 << " if (!getDerived().VisitAttr(A))\n"
3144 << " return false;\n"
3145 << " if (!getDerived().Visit" << R.getName() << "Attr(A))\n"
3146 << " return false;\n";
3147
3148 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman2f22b942014-05-20 19:47:14 +00003149 for (const auto *Arg : ArgRecords)
3150 createArgument(*Arg, R.getName())->writeASTVisitorTraversal(OS);
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003151
3152 OS << " return true;\n";
3153 OS << "}\n\n";
3154 }
3155
3156 // Write generic Traverse routine
3157 OS << "template <typename Derived>\n"
DeLesley Hutchinsbb79c332013-12-30 21:03:02 +00003158 << "bool VISITORCLASS<Derived>::TraverseAttr(Attr *A) {\n"
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003159 << " if (!A)\n"
3160 << " return true;\n"
3161 << "\n"
John McCall2225c8b2016-03-01 00:18:05 +00003162 << " switch (A->getKind()) {\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003163
Aaron Ballman2f22b942014-05-20 19:47:14 +00003164 for (const auto *Attr : Attrs) {
3165 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003166 if (!R.getValueAsBit("ASTNode"))
3167 continue;
3168
3169 OS << " case attr::" << R.getName() << ":\n"
3170 << " return getDerived().Traverse" << R.getName() << "Attr("
3171 << "cast<" << R.getName() << "Attr>(A));\n";
3172 }
John McCall5d7cf772016-03-01 02:09:20 +00003173 OS << " }\n"; // end switch
3174 OS << " llvm_unreachable(\"bad attribute kind\");\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003175 OS << "}\n"; // end function
3176 OS << "#endif // ATTR_VISITOR_DECLS_ONLY\n";
3177}
3178
Erich Keanea32910d2017-03-23 18:51:54 +00003179void EmitClangAttrTemplateInstantiateHelper(const std::vector<Record *> &Attrs,
3180 raw_ostream &OS,
3181 bool AppliesToDecl) {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003182
Erich Keanea32910d2017-03-23 18:51:54 +00003183 OS << " switch (At->getKind()) {\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00003184 for (const auto *Attr : Attrs) {
3185 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00003186 if (!R.getValueAsBit("ASTNode"))
3187 continue;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003188 OS << " case attr::" << R.getName() << ": {\n";
Erich Keanea32910d2017-03-23 18:51:54 +00003189 bool ShouldClone = R.getValueAsBit("Clone") &&
3190 (!AppliesToDecl ||
3191 R.getValueAsBit("MeaningfulToClassTemplateDefinition"));
Rafael Espindola7f90b7d2012-05-15 14:09:55 +00003192
3193 if (!ShouldClone) {
Hans Wennborg59dbe862015-09-29 20:56:43 +00003194 OS << " return nullptr;\n";
Rafael Espindola7f90b7d2012-05-15 14:09:55 +00003195 OS << " }\n";
3196 continue;
3197 }
3198
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003199 OS << " const auto *A = cast<"
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003200 << R.getName() << "Attr>(At);\n";
3201 bool TDependent = R.getValueAsBit("TemplateDependent");
3202
3203 if (!TDependent) {
3204 OS << " return A->clone(C);\n";
3205 OS << " }\n";
3206 continue;
3207 }
3208
3209 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman8f1439b2014-03-05 16:49:55 +00003210 std::vector<std::unique_ptr<Argument>> Args;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003211 Args.reserve(ArgRecords.size());
3212
Aaron Ballman2f22b942014-05-20 19:47:14 +00003213 for (const auto *ArgRecord : ArgRecords)
Aaron Ballman8f1439b2014-03-05 16:49:55 +00003214 Args.emplace_back(createArgument(*ArgRecord, R.getName()));
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003215
Aaron Ballman8f1439b2014-03-05 16:49:55 +00003216 for (auto const &ai : Args)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003217 ai->writeTemplateInstantiation(OS);
Aaron Ballman8f1439b2014-03-05 16:49:55 +00003218
Erich Keane6a24e802019-09-13 17:39:31 +00003219 OS << " return new (C) " << R.getName() << "Attr(C, *A";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00003220 for (auto const &ai : Args) {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003221 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003222 ai->writeTemplateInstantiationArgs(OS);
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003223 }
Erich Keane6a24e802019-09-13 17:39:31 +00003224 OS << ");\n }\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003225 }
3226 OS << " } // end switch\n"
3227 << " llvm_unreachable(\"Unknown attribute!\");\n"
Erich Keanea32910d2017-03-23 18:51:54 +00003228 << " return nullptr;\n";
3229}
3230
3231// Emits code to instantiate dependent attributes on templates.
3232void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) {
3233 emitSourceFileHeader("Template instantiation code for attributes", OS);
3234
3235 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
3236
3237 OS << "namespace clang {\n"
3238 << "namespace sema {\n\n"
3239 << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, "
3240 << "Sema &S,\n"
3241 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n";
3242 EmitClangAttrTemplateInstantiateHelper(Attrs, OS, /*AppliesToDecl*/false);
3243 OS << "}\n\n"
3244 << "Attr *instantiateTemplateAttributeForDecl(const Attr *At,\n"
3245 << " ASTContext &C, Sema &S,\n"
3246 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n";
3247 EmitClangAttrTemplateInstantiateHelper(Attrs, OS, /*AppliesToDecl*/true);
3248 OS << "}\n\n"
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00003249 << "} // end namespace sema\n"
3250 << "} // end namespace clang\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003251}
3252
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003253// Emits the list of parsed attributes.
3254void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) {
3255 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
3256
3257 OS << "#ifndef PARSED_ATTR\n";
3258 OS << "#define PARSED_ATTR(NAME) NAME\n";
3259 OS << "#endif\n\n";
3260
3261 ParsedAttrMap Names = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003262 for (const auto &I : Names) {
3263 OS << "PARSED_ATTR(" << I.first << ")\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003264 }
3265}
3266
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00003267static bool isArgVariadic(const Record &R, StringRef AttrName) {
3268 return createArgument(R, AttrName)->isVariadic();
3269}
3270
Erich Keanedf9e8ae2017-10-16 22:47:26 +00003271static void emitArgInfo(const Record &R, raw_ostream &OS) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003272 // This function will count the number of arguments specified for the
3273 // attribute and emit the number of required arguments followed by the
3274 // number of optional arguments.
3275 std::vector<Record *> Args = R.getValueAsListOfDefs("Args");
3276 unsigned ArgCount = 0, OptCount = 0;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00003277 bool HasVariadic = false;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003278 for (const auto *Arg : Args) {
George Burgess IV8a36ace2016-12-01 17:52:39 +00003279 // If the arg is fake, it's the user's job to supply it: general parsing
3280 // logic shouldn't need to know anything about it.
3281 if (Arg->getValueAsBit("Fake"))
3282 continue;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003283 Arg->getValueAsBit("Optional") ? ++OptCount : ++ArgCount;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00003284 if (!HasVariadic && isArgVariadic(*Arg, R.getName()))
3285 HasVariadic = true;
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003286 }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00003287
3288 // If there is a variadic argument, we will set the optional argument count
3289 // to its largest value. Since it's currently a 4-bit number, we set it to 15.
3290 OS << ArgCount << ", " << (HasVariadic ? 15 : OptCount);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003291}
3292
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003293static void GenerateDefaultAppertainsTo(raw_ostream &OS) {
Erich Keanee891aa92018-07-13 15:07:47 +00003294 OS << "static bool defaultAppertainsTo(Sema &, const ParsedAttr &,";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003295 OS << "const Decl *) {\n";
3296 OS << " return true;\n";
3297 OS << "}\n\n";
3298}
3299
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003300static std::string GetDiagnosticSpelling(const Record &R) {
3301 std::string Ret = R.getValueAsString("DiagSpelling");
3302 if (!Ret.empty())
3303 return Ret;
3304
3305 // If we couldn't find the DiagSpelling in this object, we can check to see
3306 // if the object is one that has a base, and if it is, loop up to the Base
3307 // member recursively.
John McCallbaf91d02019-10-25 16:28:03 -07003308 if (auto Base = R.getValueAsOptionalDef(BaseFieldName))
3309 return GetDiagnosticSpelling(*Base);
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003310
3311 return "";
3312}
3313
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003314static std::string CalculateDiagnostic(const Record &S) {
3315 // If the SubjectList object has a custom diagnostic associated with it,
3316 // return that directly.
Erich Keane3bff4142017-10-16 23:25:24 +00003317 const StringRef CustomDiag = S.getValueAsString("CustomDiag");
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003318 if (!CustomDiag.empty())
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003319 return ("\"" + Twine(CustomDiag) + "\"").str();
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003320
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003321 std::vector<std::string> DiagList;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003322 std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects");
Aaron Ballman2f22b942014-05-20 19:47:14 +00003323 for (const auto *Subject : Subjects) {
3324 const Record &R = *Subject;
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003325 // Get the diagnostic text from the Decl or Stmt node given.
3326 std::string V = GetDiagnosticSpelling(R);
3327 if (V.empty()) {
3328 PrintError(R.getLoc(),
3329 "Could not determine diagnostic spelling for the node: " +
3330 R.getName() + "; please add one to DeclNodes.td");
3331 } else {
3332 // The node may contain a list of elements itself, so split the elements
3333 // by a comma, and trim any whitespace.
3334 SmallVector<StringRef, 2> Frags;
3335 llvm::SplitString(V, Frags, ",");
3336 for (auto Str : Frags) {
3337 DiagList.push_back(Str.trim());
3338 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003339 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003340 }
3341
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003342 if (DiagList.empty()) {
3343 PrintFatalError(S.getLoc(),
3344 "Could not deduce diagnostic argument for Attr subjects");
3345 return "";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003346 }
3347
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003348 // FIXME: this is not particularly good for localization purposes and ideally
3349 // should be part of the diagnostics engine itself with some sort of list
3350 // specifier.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003351
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003352 // A single member of the list can be returned directly.
3353 if (DiagList.size() == 1)
3354 return '"' + DiagList.front() + '"';
3355
3356 if (DiagList.size() == 2)
3357 return '"' + DiagList[0] + " and " + DiagList[1] + '"';
3358
3359 // If there are more than two in the list, we serialize the first N - 1
3360 // elements with a comma. This leaves the string in the state: foo, bar,
3361 // baz (but misses quux). We can then add ", and " for the last element
3362 // manually.
3363 std::string Diag = llvm::join(DiagList.begin(), DiagList.end() - 1, ", ");
3364 return '"' + Diag + ", and " + *(DiagList.end() - 1) + '"';
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003365}
3366
Aaron Ballman12b9f652014-01-16 13:55:42 +00003367static std::string GetSubjectWithSuffix(const Record *R) {
George Burgess IV1881a572016-12-01 00:13:18 +00003368 const std::string &B = R->getName();
Aaron Ballman12b9f652014-01-16 13:55:42 +00003369 if (B == "DeclBase")
3370 return "Decl";
3371 return B + "Decl";
3372}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003373
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003374static std::string functionNameForCustomAppertainsTo(const Record &Subject) {
3375 return "is" + Subject.getName().str();
3376}
3377
Aaron Ballman80469032013-11-29 14:57:58 +00003378static std::string GenerateCustomAppertainsTo(const Record &Subject,
3379 raw_ostream &OS) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003380 std::string FnName = functionNameForCustomAppertainsTo(Subject);
Aaron Ballmana358c902013-12-02 14:58:17 +00003381
Aaron Ballman80469032013-11-29 14:57:58 +00003382 // If this code has already been generated, simply return the previous
3383 // instance of it.
3384 static std::set<std::string> CustomSubjectSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003385 auto I = CustomSubjectSet.find(FnName);
Aaron Ballman80469032013-11-29 14:57:58 +00003386 if (I != CustomSubjectSet.end())
3387 return *I;
3388
John McCallbaf91d02019-10-25 16:28:03 -07003389 // This only works with non-root Decls.
3390 Record *Base = Subject.getValueAsDef(BaseFieldName);
Aaron Ballman80469032013-11-29 14:57:58 +00003391
3392 // Not currently support custom subjects within custom subjects.
3393 if (Base->isSubClassOf("SubsetSubject")) {
3394 PrintFatalError(Subject.getLoc(),
3395 "SubsetSubjects within SubsetSubjects is not supported");
3396 return "";
3397 }
3398
Aaron Ballman80469032013-11-29 14:57:58 +00003399 OS << "static bool " << FnName << "(const Decl *D) {\n";
Erich Keane873de982018-08-03 14:24:34 +00003400 OS << " if (const auto *S = dyn_cast<";
3401 OS << GetSubjectWithSuffix(Base);
3402 OS << ">(D))\n";
3403 OS << " return " << Subject.getValueAsString("CheckCode") << ";\n";
Aaron Ballman47553042014-01-16 14:32:03 +00003404 OS << " return false;\n";
Aaron Ballman80469032013-11-29 14:57:58 +00003405 OS << "}\n\n";
3406
3407 CustomSubjectSet.insert(FnName);
3408 return FnName;
3409}
3410
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003411static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) {
3412 // If the attribute does not contain a Subjects definition, then use the
3413 // default appertainsTo logic.
3414 if (Attr.isValueUnset("Subjects"))
Aaron Ballman93b5cc62013-12-02 19:36:42 +00003415 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003416
3417 const Record *SubjectObj = Attr.getValueAsDef("Subjects");
3418 std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
3419
3420 // If the list of subjects is empty, it is assumed that the attribute
3421 // appertains to everything.
3422 if (Subjects.empty())
Aaron Ballman93b5cc62013-12-02 19:36:42 +00003423 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003424
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003425 bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn");
3426
3427 // Otherwise, generate an appertainsTo check specific to this attribute which
3428 // checks all of the given subjects against the Decl passed in. Return the
3429 // name of that check to the caller.
Richard Smithf4e248c2018-08-01 00:33:25 +00003430 //
3431 // If D is null, that means the attribute was not applied to a declaration
3432 // at all (for instance because it was applied to a type), or that the caller
3433 // has determined that the check should fail (perhaps prior to the creation
3434 // of the declaration).
Matthias Braunbbbf5d42016-12-04 05:55:09 +00003435 std::string FnName = "check" + Attr.getName().str() + "AppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003436 std::stringstream SS;
Erich Keanee891aa92018-07-13 15:07:47 +00003437 SS << "static bool " << FnName << "(Sema &S, const ParsedAttr &Attr, ";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003438 SS << "const Decl *D) {\n";
Richard Smithf4e248c2018-08-01 00:33:25 +00003439 SS << " if (!D || (";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003440 for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) {
Aaron Ballman80469032013-11-29 14:57:58 +00003441 // If the subject has custom code associated with it, generate a function
3442 // for it. The function cannot be inlined into this check (yet) because it
3443 // requires the subject to be of a specific type, and were that information
3444 // inlined here, it would not support an attribute with multiple custom
3445 // subjects.
3446 if ((*I)->isSubClassOf("SubsetSubject")) {
3447 SS << "!" << GenerateCustomAppertainsTo(**I, OS) << "(D)";
3448 } else {
Aaron Ballman12b9f652014-01-16 13:55:42 +00003449 SS << "!isa<" << GetSubjectWithSuffix(*I) << ">(D)";
Aaron Ballman80469032013-11-29 14:57:58 +00003450 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003451
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003452 if (I + 1 != E)
3453 SS << " && ";
3454 }
Richard Smithf4e248c2018-08-01 00:33:25 +00003455 SS << ")) {\n";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003456 SS << " S.Diag(Attr.getLoc(), diag::";
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003457 SS << (Warn ? "warn_attribute_wrong_decl_type_str" :
3458 "err_attribute_wrong_decl_type_str");
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003459 SS << ")\n";
Erich Keane44bacdf2018-08-09 13:21:32 +00003460 SS << " << Attr << ";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003461 SS << CalculateDiagnostic(*SubjectObj) << ";\n";
3462 SS << " return false;\n";
3463 SS << " }\n";
3464 SS << " return true;\n";
3465 SS << "}\n\n";
3466
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003467 OS << SS.str();
3468 return FnName;
3469}
3470
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003471static void
3472emitAttributeMatchRules(PragmaClangAttributeSupport &PragmaAttributeSupport,
3473 raw_ostream &OS) {
3474 OS << "static bool checkAttributeMatchRuleAppliesTo(const Decl *D, "
3475 << AttributeSubjectMatchRule::EnumName << " rule) {\n";
3476 OS << " switch (rule) {\n";
3477 for (const auto &Rule : PragmaAttributeSupport.Rules) {
3478 if (Rule.isAbstractRule()) {
3479 OS << " case " << Rule.getEnumValue() << ":\n";
3480 OS << " assert(false && \"Abstract matcher rule isn't allowed\");\n";
3481 OS << " return false;\n";
3482 continue;
3483 }
3484 std::vector<Record *> Subjects = Rule.getSubjects();
3485 assert(!Subjects.empty() && "Missing subjects");
3486 OS << " case " << Rule.getEnumValue() << ":\n";
3487 OS << " return ";
3488 for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) {
3489 // If the subject has custom code associated with it, use the function
3490 // that was generated for GenerateAppertainsTo to check if the declaration
3491 // is valid.
3492 if ((*I)->isSubClassOf("SubsetSubject"))
3493 OS << functionNameForCustomAppertainsTo(**I) << "(D)";
3494 else
3495 OS << "isa<" << GetSubjectWithSuffix(*I) << ">(D)";
3496
3497 if (I + 1 != E)
3498 OS << " || ";
3499 }
3500 OS << ";\n";
3501 }
3502 OS << " }\n";
3503 OS << " llvm_unreachable(\"Invalid match rule\");\nreturn false;\n";
3504 OS << "}\n\n";
3505}
3506
Aaron Ballman3aff6332013-12-02 19:30:36 +00003507static void GenerateDefaultLangOptRequirements(raw_ostream &OS) {
3508 OS << "static bool defaultDiagnoseLangOpts(Sema &, ";
Erich Keanee891aa92018-07-13 15:07:47 +00003509 OS << "const ParsedAttr &) {\n";
Aaron Ballman3aff6332013-12-02 19:30:36 +00003510 OS << " return true;\n";
3511 OS << "}\n\n";
3512}
3513
3514static std::string GenerateLangOptRequirements(const Record &R,
3515 raw_ostream &OS) {
3516 // If the attribute has an empty or unset list of language requirements,
3517 // return the default handler.
3518 std::vector<Record *> LangOpts = R.getValueAsListOfDefs("LangOpts");
3519 if (LangOpts.empty())
3520 return "defaultDiagnoseLangOpts";
3521
John McCall2c91c3b2019-05-30 04:09:01 +00003522 // Generate a unique function name for the diagnostic test. The list of
3523 // options should usually be short (one or two options), and the
3524 // uniqueness isn't strictly necessary (it is just for codegen efficiency).
3525 std::string FnName = "check";
3526 for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I)
3527 FnName += (*I)->getValueAsString("Name");
Aaron Ballman3aff6332013-12-02 19:30:36 +00003528 FnName += "LangOpts";
3529
3530 // If this code has already been generated, simply return the previous
3531 // instance of it.
3532 static std::set<std::string> CustomLangOptsSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003533 auto I = CustomLangOptsSet.find(FnName);
Aaron Ballman3aff6332013-12-02 19:30:36 +00003534 if (I != CustomLangOptsSet.end())
3535 return *I;
3536
Erich Keanee891aa92018-07-13 15:07:47 +00003537 OS << "static bool " << FnName << "(Sema &S, const ParsedAttr &Attr) {\n";
John McCall2c91c3b2019-05-30 04:09:01 +00003538 OS << " auto &LangOpts = S.LangOpts;\n";
3539 OS << " if (" << GenerateTestExpression(LangOpts) << ")\n";
Aaron Ballman3aff6332013-12-02 19:30:36 +00003540 OS << " return true;\n\n";
3541 OS << " S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) ";
Erich Keane6a24e802019-09-13 17:39:31 +00003542 OS << "<< Attr;\n";
Aaron Ballman3aff6332013-12-02 19:30:36 +00003543 OS << " return false;\n";
3544 OS << "}\n\n";
3545
3546 CustomLangOptsSet.insert(FnName);
3547 return FnName;
3548}
3549
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003550static void GenerateDefaultTargetRequirements(raw_ostream &OS) {
Bob Wilson7c730832015-07-20 22:57:31 +00003551 OS << "static bool defaultTargetRequirements(const TargetInfo &) {\n";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003552 OS << " return true;\n";
3553 OS << "}\n\n";
3554}
3555
3556static std::string GenerateTargetRequirements(const Record &Attr,
3557 const ParsedAttrMap &Dupes,
3558 raw_ostream &OS) {
3559 // If the attribute is not a target specific attribute, return the default
3560 // target handler.
3561 if (!Attr.isSubClassOf("TargetSpecificAttr"))
3562 return "defaultTargetRequirements";
3563
3564 // Get the list of architectures to be tested for.
3565 const Record *R = Attr.getValueAsDef("Target");
Craig Topper00648582017-05-31 19:01:22 +00003566 std::vector<StringRef> Arches = R->getValueAsListOfStrings("Arches");
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003567
3568 // If there are other attributes which share the same parsed attribute kind,
3569 // such as target-specific attributes with a shared spelling, collapse the
3570 // duplicate architectures. This is required because a shared target-specific
Erich Keanee891aa92018-07-13 15:07:47 +00003571 // attribute has only one ParsedAttr::Kind enumeration value, but it
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003572 // applies to multiple target architectures. In order for the attribute to be
3573 // considered valid, all of its architectures need to be included.
3574 if (!Attr.isValueUnset("ParseKind")) {
Erich Keane3bff4142017-10-16 23:25:24 +00003575 const StringRef APK = Attr.getValueAsString("ParseKind");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003576 for (const auto &I : Dupes) {
3577 if (I.first == APK) {
Craig Topper00648582017-05-31 19:01:22 +00003578 std::vector<StringRef> DA =
3579 I.second->getValueAsDef("Target")->getValueAsListOfStrings(
3580 "Arches");
3581 Arches.insert(Arches.end(), DA.begin(), DA.end());
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003582 }
3583 }
3584 }
3585
Bob Wilson0058b822015-07-20 22:57:36 +00003586 std::string FnName = "isTarget";
3587 std::string Test;
Richard Smith78b239e2019-06-20 20:44:45 +00003588 bool UsesT = GenerateTargetSpecificAttrChecks(R, Arches, Test, &FnName);
Bob Wilson7c730832015-07-20 22:57:31 +00003589
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003590 // If this code has already been generated, simply return the previous
3591 // instance of it.
3592 static std::set<std::string> CustomTargetSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003593 auto I = CustomTargetSet.find(FnName);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003594 if (I != CustomTargetSet.end())
3595 return *I;
3596
Bob Wilson7c730832015-07-20 22:57:31 +00003597 OS << "static bool " << FnName << "(const TargetInfo &Target) {\n";
Richard Smith78b239e2019-06-20 20:44:45 +00003598 if (UsesT)
3599 OS << " const llvm::Triple &T = Target.getTriple(); (void)T;\n";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003600 OS << " return " << Test << ";\n";
3601 OS << "}\n\n";
3602
3603 CustomTargetSet.insert(FnName);
3604 return FnName;
3605}
3606
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003607static void GenerateDefaultSpellingIndexToSemanticSpelling(raw_ostream &OS) {
3608 OS << "static unsigned defaultSpellingIndexToSemanticSpelling("
Erich Keanee891aa92018-07-13 15:07:47 +00003609 << "const ParsedAttr &Attr) {\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003610 OS << " return UINT_MAX;\n";
3611 OS << "}\n\n";
3612}
3613
3614static std::string GenerateSpellingIndexToSemanticSpelling(const Record &Attr,
3615 raw_ostream &OS) {
3616 // If the attribute does not have a semantic form, we can bail out early.
3617 if (!Attr.getValueAsBit("ASTNode"))
3618 return "defaultSpellingIndexToSemanticSpelling";
3619
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003620 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003621
3622 // If there are zero or one spellings, or all of the spellings share the same
3623 // name, we can also bail out early.
3624 if (Spellings.size() <= 1 || SpellingNamesAreCommon(Spellings))
3625 return "defaultSpellingIndexToSemanticSpelling";
3626
3627 // Generate the enumeration we will use for the mapping.
3628 SemanticSpellingMap SemanticToSyntacticMap;
3629 std::string Enum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
Matthias Braunbbbf5d42016-12-04 05:55:09 +00003630 std::string Name = Attr.getName().str() + "AttrSpellingMap";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003631
Erich Keanee891aa92018-07-13 15:07:47 +00003632 OS << "static unsigned " << Name << "(const ParsedAttr &Attr) {\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003633 OS << Enum;
3634 OS << " unsigned Idx = Attr.getAttributeSpellingListIndex();\n";
3635 WriteSemanticSpellingSwitch("Idx", SemanticToSyntacticMap, OS);
3636 OS << "}\n\n";
3637
3638 return Name;
3639}
3640
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003641static bool IsKnownToGCC(const Record &Attr) {
3642 // Look at the spellings for this subject; if there are any spellings which
3643 // claim to be known to GCC, the attribute is known to GCC.
George Burgess IV1881a572016-12-01 00:13:18 +00003644 return llvm::any_of(
3645 GetFlattenedSpellings(Attr),
3646 [](const FlattenedSpelling &S) { return S.knownToGCC(); });
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00003647}
3648
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003649/// Emits the parsed attribute helpers
3650void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
3651 emitSourceFileHeader("Parsed attribute helpers", OS);
3652
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003653 PragmaClangAttributeSupport &PragmaAttributeSupport =
3654 getPragmaAttributeSupport(Records);
3655
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003656 // Get the list of parsed attributes, and accept the optional list of
3657 // duplicates due to the ParseKind.
3658 ParsedAttrMap Dupes;
3659 ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003660
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003661 // Generate the default appertainsTo, target and language option diagnostic,
3662 // and spelling list index mapping methods.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003663 GenerateDefaultAppertainsTo(OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00003664 GenerateDefaultLangOptRequirements(OS);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003665 GenerateDefaultTargetRequirements(OS);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003666 GenerateDefaultSpellingIndexToSemanticSpelling(OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003667
3668 // Generate the appertainsTo diagnostic methods and write their names into
3669 // another mapping. At the same time, generate the AttrInfoMap object
3670 // contents. Due to the reliance on generated code, use separate streams so
3671 // that code will not be interleaved.
Erich Keanedf9e8ae2017-10-16 22:47:26 +00003672 std::string Buffer;
3673 raw_string_ostream SS {Buffer};
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003674 for (auto I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003675 // TODO: If the attribute's kind appears in the list of duplicates, that is
3676 // because it is a target-specific attribute that appears multiple times.
3677 // It would be beneficial to test whether the duplicates are "similar
3678 // enough" to each other to not cause problems. For instance, check that
Alp Toker96cf7582014-01-18 21:49:37 +00003679 // the spellings are identical, and custom parsing rules match, etc.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003680
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003681 // We need to generate struct instances based off ParsedAttrInfo from
Erich Keanee891aa92018-07-13 15:07:47 +00003682 // ParsedAttr.cpp.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003683 SS << " { ";
3684 emitArgInfo(*I->second, SS);
3685 SS << ", " << I->second->getValueAsBit("HasCustomParsing");
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003686 SS << ", " << I->second->isSubClassOf("TargetSpecificAttr");
Aaron Ballmanb9a457a2018-05-03 15:33:50 +00003687 SS << ", "
3688 << (I->second->isSubClassOf("TypeAttr") ||
3689 I->second->isSubClassOf("DeclOrTypeAttr"));
Richard Smith4f902c72016-03-08 00:32:55 +00003690 SS << ", " << I->second->isSubClassOf("StmtAttr");
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003691 SS << ", " << IsKnownToGCC(*I->second);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003692 SS << ", " << PragmaAttributeSupport.isAttributedSupported(*I->second);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003693 SS << ", " << GenerateAppertainsTo(*I->second, OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00003694 SS << ", " << GenerateLangOptRequirements(*I->second, OS);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003695 SS << ", " << GenerateTargetRequirements(*I->second, Dupes, OS);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003696 SS << ", " << GenerateSpellingIndexToSemanticSpelling(*I->second, OS);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003697 SS << ", "
3698 << PragmaAttributeSupport.generateStrictConformsTo(*I->second, OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003699 SS << " }";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003700
3701 if (I + 1 != E)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003702 SS << ",";
3703
3704 SS << " // AT_" << I->first << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003705 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003706
Erich Keanee891aa92018-07-13 15:07:47 +00003707 OS << "static const ParsedAttrInfo AttrInfoMap[ParsedAttr::UnknownAttribute "
3708 "+ 1] = {\n";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003709 OS << SS.str();
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003710 OS << "};\n\n";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003711
3712 // Generate the attribute match rules.
3713 emitAttributeMatchRules(PragmaAttributeSupport, OS);
Michael Han4a045172012-03-07 00:12:16 +00003714}
3715
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00003716// Emits the kind list of parsed attributes
3717void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00003718 emitSourceFileHeader("Attribute name matcher", OS);
3719
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003720 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Nico Weber20e08042016-09-03 02:55:10 +00003721 std::vector<StringMatcher::StringPair> GNU, Declspec, Microsoft, CXX11,
Aaron Ballman606093a2017-10-15 15:01:42 +00003722 Keywords, Pragma, C2x;
Aaron Ballman64e69862013-12-15 13:05:48 +00003723 std::set<std::string> Seen;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003724 for (const auto *A : Attrs) {
3725 const Record &Attr = *A;
Richard Smith852e9ce2013-11-27 01:46:48 +00003726
Michael Han4a045172012-03-07 00:12:16 +00003727 bool SemaHandler = Attr.getValueAsBit("SemaHandler");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003728 bool Ignored = Attr.getValueAsBit("Ignored");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003729 if (SemaHandler || Ignored) {
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003730 // Attribute spellings can be shared between target-specific attributes,
3731 // and can be shared between syntaxes for the same attribute. For
3732 // instance, an attribute can be spelled GNU<"interrupt"> for an ARM-
3733 // specific attribute, or MSP430-specific attribute. Additionally, an
3734 // attribute can be spelled GNU<"dllexport"> and Declspec<"dllexport">
3735 // for the same semantic attribute. Ultimately, we need to map each of
Erich Keaneb79f3312019-09-16 13:58:59 +00003736 // these to a single AttributeCommonInfo::Kind value, but the
3737 // StringMatcher class cannot handle duplicate match strings. So we
3738 // generate a list of string to match based on the syntax, and emit
3739 // multiple string matchers depending on the syntax used.
Aaron Ballman64e69862013-12-15 13:05:48 +00003740 std::string AttrName;
3741 if (Attr.isSubClassOf("TargetSpecificAttr") &&
3742 !Attr.isValueUnset("ParseKind")) {
3743 AttrName = Attr.getValueAsString("ParseKind");
3744 if (Seen.find(AttrName) != Seen.end())
3745 continue;
3746 Seen.insert(AttrName);
3747 } else
3748 AttrName = NormalizeAttrName(StringRef(Attr.getName())).str();
3749
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003750 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003751 for (const auto &S : Spellings) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00003752 const std::string &RawSpelling = S.name();
Craig Topper8ae12032014-05-07 06:21:57 +00003753 std::vector<StringMatcher::StringPair> *Matches = nullptr;
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00003754 std::string Spelling;
3755 const std::string &Variety = S.variety();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003756 if (Variety == "CXX11") {
3757 Matches = &CXX11;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003758 Spelling += S.nameSpace();
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003759 Spelling += "::";
Aaron Ballman606093a2017-10-15 15:01:42 +00003760 } else if (Variety == "C2x") {
3761 Matches = &C2x;
3762 Spelling += S.nameSpace();
3763 Spelling += "::";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003764 } else if (Variety == "GNU")
3765 Matches = &GNU;
3766 else if (Variety == "Declspec")
3767 Matches = &Declspec;
Nico Weber20e08042016-09-03 02:55:10 +00003768 else if (Variety == "Microsoft")
3769 Matches = &Microsoft;
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003770 else if (Variety == "Keyword")
3771 Matches = &Keywords;
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003772 else if (Variety == "Pragma")
3773 Matches = &Pragma;
Alexis Hunta0e54d42012-06-18 16:13:52 +00003774
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003775 assert(Matches && "Unsupported spelling variety found");
3776
Justin Lebar4086fe52017-01-05 16:51:54 +00003777 if (Variety == "GNU")
3778 Spelling += NormalizeGNUAttrSpelling(RawSpelling);
3779 else
3780 Spelling += RawSpelling;
3781
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003782 if (SemaHandler)
Erich Keanee891aa92018-07-13 15:07:47 +00003783 Matches->push_back(StringMatcher::StringPair(
Erich Keaneb79f3312019-09-16 13:58:59 +00003784 Spelling, "return AttributeCommonInfo::AT_" + AttrName + ";"));
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003785 else
Erich Keanee891aa92018-07-13 15:07:47 +00003786 Matches->push_back(StringMatcher::StringPair(
Erich Keaneb79f3312019-09-16 13:58:59 +00003787 Spelling, "return AttributeCommonInfo::IgnoredAttribute;"));
Michael Han4a045172012-03-07 00:12:16 +00003788 }
3789 }
3790 }
Erich Keanee891aa92018-07-13 15:07:47 +00003791
Erich Keaneb79f3312019-09-16 13:58:59 +00003792 OS << "static AttributeCommonInfo::Kind getAttrKind(StringRef Name, ";
3793 OS << "AttributeCommonInfo::Syntax Syntax) {\n";
3794 OS << " if (AttributeCommonInfo::AS_GNU == Syntax) {\n";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003795 StringMatcher("Name", GNU, OS).Emit();
Erich Keaneb79f3312019-09-16 13:58:59 +00003796 OS << " } else if (AttributeCommonInfo::AS_Declspec == Syntax) {\n";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003797 StringMatcher("Name", Declspec, OS).Emit();
Erich Keaneb79f3312019-09-16 13:58:59 +00003798 OS << " } else if (AttributeCommonInfo::AS_Microsoft == Syntax) {\n";
Nico Weber20e08042016-09-03 02:55:10 +00003799 StringMatcher("Name", Microsoft, OS).Emit();
Erich Keaneb79f3312019-09-16 13:58:59 +00003800 OS << " } else if (AttributeCommonInfo::AS_CXX11 == Syntax) {\n";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003801 StringMatcher("Name", CXX11, OS).Emit();
Erich Keaneb79f3312019-09-16 13:58:59 +00003802 OS << " } else if (AttributeCommonInfo::AS_C2x == Syntax) {\n";
Aaron Ballman606093a2017-10-15 15:01:42 +00003803 StringMatcher("Name", C2x, OS).Emit();
Erich Keaneb79f3312019-09-16 13:58:59 +00003804 OS << " } else if (AttributeCommonInfo::AS_Keyword == Syntax || ";
3805 OS << "AttributeCommonInfo::AS_ContextSensitiveKeyword == Syntax) {\n";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003806 StringMatcher("Name", Keywords, OS).Emit();
Erich Keaneb79f3312019-09-16 13:58:59 +00003807 OS << " } else if (AttributeCommonInfo::AS_Pragma == Syntax) {\n";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003808 StringMatcher("Name", Pragma, OS).Emit();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003809 OS << " }\n";
Erich Keaneb79f3312019-09-16 13:58:59 +00003810 OS << " return AttributeCommonInfo::UnknownAttribute;\n"
Douglas Gregor377f99b2012-05-02 17:33:51 +00003811 << "}\n";
Michael Han4a045172012-03-07 00:12:16 +00003812}
3813
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003814// Emits the code to dump an attribute.
Stephen Kellydb8fac12019-01-11 19:16:01 +00003815void EmitClangAttrTextNodeDump(RecordKeeper &Records, raw_ostream &OS) {
3816 emitSourceFileHeader("Attribute text node dumper", OS);
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00003817
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003818 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003819 for (const auto *Attr : Attrs) {
3820 const Record &R = *Attr;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003821 if (!R.getValueAsBit("ASTNode"))
3822 continue;
Aaron Ballmanbc909612014-01-22 21:51:20 +00003823
3824 // If the attribute has a semantically-meaningful name (which is determined
3825 // by whether there is a Spelling enumeration for it), then write out the
3826 // spelling used for the attribute.
Stephen Kellydb8fac12019-01-11 19:16:01 +00003827
3828 std::string FunctionContent;
3829 llvm::raw_string_ostream SS(FunctionContent);
3830
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003831 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballmanbc909612014-01-22 21:51:20 +00003832 if (Spellings.size() > 1 && !SpellingNamesAreCommon(Spellings))
Stephen Kellydb8fac12019-01-11 19:16:01 +00003833 SS << " OS << \" \" << A->getSpelling();\n";
Aaron Ballmanbc909612014-01-22 21:51:20 +00003834
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003835 Args = R.getValueAsListOfDefs("Args");
Stephen Kellydb8fac12019-01-11 19:16:01 +00003836 for (const auto *Arg : Args)
3837 createArgument(*Arg, R.getName())->writeDump(SS);
Richard Trieude5cc7d2013-01-31 01:44:26 +00003838
Stephen Kellydb8fac12019-01-11 19:16:01 +00003839 if (SS.tell()) {
3840 OS << " void Visit" << R.getName() << "Attr(const " << R.getName()
3841 << "Attr *A) {\n";
3842 if (!Args.empty())
3843 OS << " const auto *SA = cast<" << R.getName()
3844 << "Attr>(A); (void)SA;\n";
3845 OS << SS.str();
3846 OS << " }\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003847 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003848 }
Stephen Kellydb8fac12019-01-11 19:16:01 +00003849}
3850
3851void EmitClangAttrNodeTraverse(RecordKeeper &Records, raw_ostream &OS) {
3852 emitSourceFileHeader("Attribute text node traverser", OS);
3853
3854 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
3855 for (const auto *Attr : Attrs) {
3856 const Record &R = *Attr;
3857 if (!R.getValueAsBit("ASTNode"))
3858 continue;
3859
3860 std::string FunctionContent;
3861 llvm::raw_string_ostream SS(FunctionContent);
3862
3863 Args = R.getValueAsListOfDefs("Args");
3864 for (const auto *Arg : Args)
3865 createArgument(*Arg, R.getName())->writeDumpChildren(SS);
3866 if (SS.tell()) {
3867 OS << " void Visit" << R.getName() << "Attr(const " << R.getName()
3868 << "Attr *A) {\n";
3869 if (!Args.empty())
3870 OS << " const auto *SA = cast<" << R.getName()
3871 << "Attr>(A); (void)SA;\n";
3872 OS << SS.str();
3873 OS << " }\n";
3874 }
3875 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003876}
3877
Aaron Ballman35db2b32014-01-29 22:13:45 +00003878void EmitClangAttrParserStringSwitches(RecordKeeper &Records,
3879 raw_ostream &OS) {
3880 emitSourceFileHeader("Parser-related llvm::StringSwitch cases", OS);
3881 emitClangAttrArgContextList(Records, OS);
3882 emitClangAttrIdentifierArgList(Records, OS);
Erich Keane3efe0022018-07-20 14:13:28 +00003883 emitClangAttrVariadicIdentifierArgList(Records, OS);
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003884 emitClangAttrThisIsaIdentifierArgList(Records, OS);
Aaron Ballman35db2b32014-01-29 22:13:45 +00003885 emitClangAttrTypeArgList(Records, OS);
3886 emitClangAttrLateParsedList(Records, OS);
3887}
3888
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003889void EmitClangAttrSubjectMatchRulesParserStringSwitches(RecordKeeper &Records,
3890 raw_ostream &OS) {
3891 getPragmaAttributeSupport(Records).generateParsingHelpers(OS);
3892}
3893
Richard Smith09ac4a12018-08-30 19:16:33 +00003894enum class SpellingKind {
3895 GNU,
3896 CXX11,
3897 C2x,
3898 Declspec,
3899 Microsoft,
3900 Keyword,
3901 Pragma,
3902};
3903static const size_t NumSpellingKinds = (size_t)SpellingKind::Pragma + 1;
3904
3905class SpellingList {
3906 std::vector<std::string> Spellings[NumSpellingKinds];
3907
3908public:
3909 ArrayRef<std::string> operator[](SpellingKind K) const {
3910 return Spellings[(size_t)K];
3911 }
3912
3913 void add(const Record &Attr, FlattenedSpelling Spelling) {
3914 SpellingKind Kind = StringSwitch<SpellingKind>(Spelling.variety())
3915 .Case("GNU", SpellingKind::GNU)
3916 .Case("CXX11", SpellingKind::CXX11)
3917 .Case("C2x", SpellingKind::C2x)
3918 .Case("Declspec", SpellingKind::Declspec)
3919 .Case("Microsoft", SpellingKind::Microsoft)
3920 .Case("Keyword", SpellingKind::Keyword)
3921 .Case("Pragma", SpellingKind::Pragma);
3922 std::string Name;
3923 if (!Spelling.nameSpace().empty()) {
3924 switch (Kind) {
3925 case SpellingKind::CXX11:
3926 case SpellingKind::C2x:
3927 Name = Spelling.nameSpace() + "::";
3928 break;
3929 case SpellingKind::Pragma:
3930 Name = Spelling.nameSpace() + " ";
3931 break;
3932 default:
3933 PrintFatalError(Attr.getLoc(), "Unexpected namespace in spelling");
3934 }
3935 }
3936 Name += Spelling.name();
3937
3938 Spellings[(size_t)Kind].push_back(Name);
3939 }
3940};
3941
Aaron Ballman97dba042014-02-17 15:27:10 +00003942class DocumentationData {
3943public:
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003944 const Record *Documentation;
3945 const Record *Attribute;
Erich Keanea98a2be2017-10-16 20:31:05 +00003946 std::string Heading;
Richard Smith09ac4a12018-08-30 19:16:33 +00003947 SpellingList SupportedSpellings;
Aaron Ballman97dba042014-02-17 15:27:10 +00003948
Erich Keanea98a2be2017-10-16 20:31:05 +00003949 DocumentationData(const Record &Documentation, const Record &Attribute,
Richard Smith09ac4a12018-08-30 19:16:33 +00003950 std::pair<std::string, SpellingList> HeadingAndSpellings)
Erich Keanea98a2be2017-10-16 20:31:05 +00003951 : Documentation(&Documentation), Attribute(&Attribute),
Richard Smith09ac4a12018-08-30 19:16:33 +00003952 Heading(std::move(HeadingAndSpellings.first)),
3953 SupportedSpellings(std::move(HeadingAndSpellings.second)) {}
Aaron Ballman97dba042014-02-17 15:27:10 +00003954};
3955
Aaron Ballman4de1b582014-02-19 22:59:32 +00003956static void WriteCategoryHeader(const Record *DocCategory,
Aaron Ballman97dba042014-02-17 15:27:10 +00003957 raw_ostream &OS) {
Erich Keane3bff4142017-10-16 23:25:24 +00003958 const StringRef Name = DocCategory->getValueAsString("Name");
3959 OS << Name << "\n" << std::string(Name.size(), '=') << "\n";
Aaron Ballman4de1b582014-02-19 22:59:32 +00003960
3961 // If there is content, print that as well.
Erich Keane3bff4142017-10-16 23:25:24 +00003962 const StringRef ContentStr = DocCategory->getValueAsString("Content");
Benjamin Kramer5c404072015-04-10 21:37:21 +00003963 // Trim leading and trailing newlines and spaces.
Erich Keane3bff4142017-10-16 23:25:24 +00003964 OS << ContentStr.trim();
Benjamin Kramer5c404072015-04-10 21:37:21 +00003965
Aaron Ballman4de1b582014-02-19 22:59:32 +00003966 OS << "\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003967}
3968
Richard Smith09ac4a12018-08-30 19:16:33 +00003969static std::pair<std::string, SpellingList>
3970GetAttributeHeadingAndSpellings(const Record &Documentation,
3971 const Record &Attribute) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003972 // FIXME: there is no way to have a per-spelling category for the attribute
3973 // documentation. This may not be a limiting factor since the spellings
3974 // should generally be consistently applied across the category.
3975
Erich Keanea98a2be2017-10-16 20:31:05 +00003976 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attribute);
Richard Smith09ac4a12018-08-30 19:16:33 +00003977 if (Spellings.empty())
3978 PrintFatalError(Attribute.getLoc(),
3979 "Attribute has no supported spellings; cannot be "
3980 "documented");
Aaron Ballman97dba042014-02-17 15:27:10 +00003981
3982 // Determine the heading to be used for this attribute.
Erich Keanea98a2be2017-10-16 20:31:05 +00003983 std::string Heading = Documentation.getValueAsString("Heading");
Aaron Ballman97dba042014-02-17 15:27:10 +00003984 if (Heading.empty()) {
3985 // If there's only one spelling, we can simply use that.
3986 if (Spellings.size() == 1)
3987 Heading = Spellings.begin()->name();
3988 else {
3989 std::set<std::string> Uniques;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003990 for (auto I = Spellings.begin(), E = Spellings.end();
3991 I != E && Uniques.size() <= 1; ++I) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003992 std::string Spelling = NormalizeNameForSpellingComparison(I->name());
3993 Uniques.insert(Spelling);
3994 }
3995 // If the semantic map has only one spelling, that is sufficient for our
3996 // needs.
3997 if (Uniques.size() == 1)
3998 Heading = *Uniques.begin();
3999 }
4000 }
4001
4002 // If the heading is still empty, it is an error.
4003 if (Heading.empty())
Erich Keanea98a2be2017-10-16 20:31:05 +00004004 PrintFatalError(Attribute.getLoc(),
Aaron Ballman97dba042014-02-17 15:27:10 +00004005 "This attribute requires a heading to be specified");
4006
Richard Smith09ac4a12018-08-30 19:16:33 +00004007 SpellingList SupportedSpellings;
4008 for (const auto &I : Spellings)
4009 SupportedSpellings.add(Attribute, I);
Aaron Ballman97dba042014-02-17 15:27:10 +00004010
Richard Smith09ac4a12018-08-30 19:16:33 +00004011 return std::make_pair(std::move(Heading), std::move(SupportedSpellings));
Erich Keanea98a2be2017-10-16 20:31:05 +00004012}
4013
4014static void WriteDocumentation(RecordKeeper &Records,
4015 const DocumentationData &Doc, raw_ostream &OS) {
4016 OS << Doc.Heading << "\n" << std::string(Doc.Heading.length(), '-') << "\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00004017
4018 // List what spelling syntaxes the attribute supports.
4019 OS << ".. csv-table:: Supported Syntaxes\n";
Richard Smith09ac4a12018-08-30 19:16:33 +00004020 OS << " :header: \"GNU\", \"C++11\", \"C2x\", \"``__declspec``\",";
4021 OS << " \"Keyword\", \"``#pragma``\", \"``#pragma clang attribute``\"\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00004022 OS << " \"";
Richard Smith09ac4a12018-08-30 19:16:33 +00004023 for (size_t Kind = 0; Kind != NumSpellingKinds; ++Kind) {
4024 SpellingKind K = (SpellingKind)Kind;
Richard Smith23ff7e82018-08-30 19:19:15 +00004025 // TODO: List Microsoft (IDL-style attribute) spellings once we fully
4026 // support them.
Richard Smith09ac4a12018-08-30 19:16:33 +00004027 if (K == SpellingKind::Microsoft)
4028 continue;
4029
4030 bool PrintedAny = false;
4031 for (StringRef Spelling : Doc.SupportedSpellings[K]) {
4032 if (PrintedAny)
4033 OS << " |br| ";
4034 OS << "``" << Spelling << "``";
4035 PrintedAny = true;
4036 }
4037
4038 OS << "\",\"";
4039 }
4040
4041 if (getPragmaAttributeSupport(Records).isAttributedSupported(
4042 *Doc.Attribute))
4043 OS << "Yes";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00004044 OS << "\"\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00004045
4046 // If the attribute is deprecated, print a message about it, and possibly
4047 // provide a replacement attribute.
Aaron Ballman1a3e5852014-02-17 16:18:32 +00004048 if (!Doc.Documentation->isValueUnset("Deprecated")) {
Aaron Ballman97dba042014-02-17 15:27:10 +00004049 OS << "This attribute has been deprecated, and may be removed in a future "
4050 << "version of Clang.";
Aaron Ballman1a3e5852014-02-17 16:18:32 +00004051 const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated");
Erich Keane3bff4142017-10-16 23:25:24 +00004052 const StringRef Replacement = Deprecated.getValueAsString("Replacement");
Aaron Ballman97dba042014-02-17 15:27:10 +00004053 if (!Replacement.empty())
Joel E. Denny6053ec22018-02-28 16:57:33 +00004054 OS << " This attribute has been superseded by ``" << Replacement
4055 << "``.";
Aaron Ballman97dba042014-02-17 15:27:10 +00004056 OS << "\n\n";
4057 }
4058
Erich Keane3bff4142017-10-16 23:25:24 +00004059 const StringRef ContentStr = Doc.Documentation->getValueAsString("Content");
Aaron Ballman97dba042014-02-17 15:27:10 +00004060 // Trim leading and trailing newlines and spaces.
Erich Keane3bff4142017-10-16 23:25:24 +00004061 OS << ContentStr.trim();
Aaron Ballman97dba042014-02-17 15:27:10 +00004062
4063 OS << "\n\n\n";
4064}
4065
4066void EmitClangAttrDocs(RecordKeeper &Records, raw_ostream &OS) {
4067 // Get the documentation introduction paragraph.
4068 const Record *Documentation = Records.getDef("GlobalDocumentation");
4069 if (!Documentation) {
4070 PrintFatalError("The Documentation top-level definition is missing, "
4071 "no documentation will be generated.");
4072 return;
4073 }
4074
Aaron Ballman4de1b582014-02-19 22:59:32 +00004075 OS << Documentation->getValueAsString("Intro") << "\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00004076
Aaron Ballman97dba042014-02-17 15:27:10 +00004077 // Gather the Documentation lists from each of the attributes, based on the
4078 // category provided.
4079 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00004080 std::map<const Record *, std::vector<DocumentationData>> SplitDocs;
Aaron Ballman2f22b942014-05-20 19:47:14 +00004081 for (const auto *A : Attrs) {
4082 const Record &Attr = *A;
Aaron Ballman97dba042014-02-17 15:27:10 +00004083 std::vector<Record *> Docs = Attr.getValueAsListOfDefs("Documentation");
Aaron Ballman2f22b942014-05-20 19:47:14 +00004084 for (const auto *D : Docs) {
4085 const Record &Doc = *D;
Aaron Ballman4de1b582014-02-19 22:59:32 +00004086 const Record *Category = Doc.getValueAsDef("Category");
Aaron Ballman97dba042014-02-17 15:27:10 +00004087 // If the category is "undocumented", then there cannot be any other
4088 // documentation categories (otherwise, the attribute would become
4089 // documented).
Erich Keane3bff4142017-10-16 23:25:24 +00004090 const StringRef Cat = Category->getValueAsString("Name");
Aaron Ballman4de1b582014-02-19 22:59:32 +00004091 bool Undocumented = Cat == "Undocumented";
Aaron Ballman97dba042014-02-17 15:27:10 +00004092 if (Undocumented && Docs.size() > 1)
4093 PrintFatalError(Doc.getLoc(),
4094 "Attribute is \"Undocumented\", but has multiple "
Erich Keanea98a2be2017-10-16 20:31:05 +00004095 "documentation categories");
Aaron Ballman97dba042014-02-17 15:27:10 +00004096
4097 if (!Undocumented)
Erich Keanea98a2be2017-10-16 20:31:05 +00004098 SplitDocs[Category].push_back(DocumentationData(
Richard Smith09ac4a12018-08-30 19:16:33 +00004099 Doc, Attr, GetAttributeHeadingAndSpellings(Doc, Attr)));
Aaron Ballman97dba042014-02-17 15:27:10 +00004100 }
4101 }
4102
4103 // Having split the attributes out based on what documentation goes where,
4104 // we can begin to generate sections of documentation.
Erich Keanea98a2be2017-10-16 20:31:05 +00004105 for (auto &I : SplitDocs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00004106 WriteCategoryHeader(I.first, OS);
Aaron Ballman97dba042014-02-17 15:27:10 +00004107
Fangrui Song1d38c132018-09-30 21:41:11 +00004108 llvm::sort(I.second,
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00004109 [](const DocumentationData &D1, const DocumentationData &D2) {
4110 return D1.Heading < D2.Heading;
Fangrui Song1d38c132018-09-30 21:41:11 +00004111 });
Erich Keanea98a2be2017-10-16 20:31:05 +00004112
Aaron Ballman97dba042014-02-17 15:27:10 +00004113 // Walk over each of the attributes in the category and write out their
4114 // documentation.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00004115 for (const auto &Doc : I.second)
Alex Lorenz9e7bf162017-04-18 14:33:39 +00004116 WriteDocumentation(Records, Doc, OS);
4117 }
4118}
4119
4120void EmitTestPragmaAttributeSupportedAttributes(RecordKeeper &Records,
4121 raw_ostream &OS) {
4122 PragmaClangAttributeSupport Support = getPragmaAttributeSupport(Records);
4123 ParsedAttrMap Attrs = getParsedAttrList(Records);
Erik Pilkington23c48c22018-12-04 00:31:31 +00004124 OS << "#pragma clang attribute supports the following attributes:\n";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00004125 for (const auto &I : Attrs) {
4126 if (!Support.isAttributedSupported(*I.second))
4127 continue;
4128 OS << I.first;
4129 if (I.second->isValueUnset("Subjects")) {
4130 OS << " ()\n";
4131 continue;
4132 }
4133 const Record *SubjectObj = I.second->getValueAsDef("Subjects");
4134 std::vector<Record *> Subjects =
4135 SubjectObj->getValueAsListOfDefs("Subjects");
4136 OS << " (";
4137 for (const auto &Subject : llvm::enumerate(Subjects)) {
4138 if (Subject.index())
4139 OS << ", ";
Alex Lorenz24952fb2017-04-19 15:52:11 +00004140 PragmaClangAttributeSupport::RuleOrAggregateRuleSet &RuleSet =
4141 Support.SubjectsToRules.find(Subject.value())->getSecond();
4142 if (RuleSet.isRule()) {
4143 OS << RuleSet.getRule().getEnumValueName();
4144 continue;
4145 }
4146 OS << "(";
4147 for (const auto &Rule : llvm::enumerate(RuleSet.getAggregateRuleSet())) {
4148 if (Rule.index())
4149 OS << ", ";
4150 OS << Rule.value().getEnumValueName();
4151 }
4152 OS << ")";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00004153 }
4154 OS << ")\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00004155 }
Erik Pilkington23c48c22018-12-04 00:31:31 +00004156 OS << "End of supported attributes.\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00004157}
4158
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00004159} // end namespace clang