blob: 0489bb073d23245440808e93ccb29bd10809056f [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
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000013#include "llvm/ADT/ArrayRef.h"
Alex Lorenz9e7bf162017-04-18 14:33:39 +000014#include "llvm/ADT/DenseMap.h"
George Burgess IV1881a572016-12-01 00:13:18 +000015#include "llvm/ADT/DenseSet.h"
Alex Lorenz3bfe9622017-04-18 10:46:41 +000016#include "llvm/ADT/STLExtras.h"
Alex Lorenz9e7bf162017-04-18 14:33:39 +000017#include "llvm/ADT/SmallString.h"
Aaron Ballman28afa182014-11-17 18:17:19 +000018#include "llvm/ADT/StringExtras.h"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000019#include "llvm/ADT/StringRef.h"
Alex Lorenz9e7bf162017-04-18 14:33:39 +000020#include "llvm/ADT/StringSet.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000021#include "llvm/ADT/StringSwitch.h"
Alex Lorenz9e7bf162017-04-18 14:33:39 +000022#include "llvm/ADT/iterator_range.h"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000023#include "llvm/Support/ErrorHandling.h"
24#include "llvm/Support/raw_ostream.h"
Chandler Carruth5553d0d2014-01-07 11:51:46 +000025#include "llvm/TableGen/Error.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000026#include "llvm/TableGen/Record.h"
Douglas Gregor377f99b2012-05-02 17:33:51 +000027#include "llvm/TableGen/StringMatcher.h"
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +000028#include "llvm/TableGen/TableGenBackend.h"
Peter Collingbournebee583f2011-10-06 13:03:08 +000029#include <algorithm>
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000030#include <cassert>
Peter Collingbournebee583f2011-10-06 13:03:08 +000031#include <cctype>
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000032#include <cstddef>
33#include <cstdint>
34#include <map>
Aaron Ballman8f1439b2014-03-05 16:49:55 +000035#include <memory>
Aaron Ballman80469032013-11-29 14:57:58 +000036#include <set>
Chandler Carruth5553d0d2014-01-07 11:51:46 +000037#include <sstream>
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000038#include <string>
39#include <utility>
40#include <vector>
Peter Collingbournebee583f2011-10-06 13:03:08 +000041
42using namespace llvm;
43
Benjamin Kramerd910d162015-03-10 18:24:01 +000044namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000045
Aaron Ballmanc669cc02014-01-27 22:10:04 +000046class FlattenedSpelling {
47 std::string V, N, NS;
48 bool K;
49
50public:
51 FlattenedSpelling(const std::string &Variety, const std::string &Name,
52 const std::string &Namespace, bool KnownToGCC) :
53 V(Variety), N(Name), NS(Namespace), K(KnownToGCC) {}
54 explicit FlattenedSpelling(const Record &Spelling) :
55 V(Spelling.getValueAsString("Variety")),
56 N(Spelling.getValueAsString("Name")) {
57
Aaron Ballmanffc43362017-10-26 12:19:02 +000058 assert(V != "GCC" && V != "Clang" &&
59 "Given a GCC spelling, which means this hasn't been flattened!");
Aaron Ballman606093a2017-10-15 15:01:42 +000060 if (V == "CXX11" || V == "C2x" || V == "Pragma")
Aaron Ballmanc669cc02014-01-27 22:10:04 +000061 NS = Spelling.getValueAsString("Namespace");
62 bool Unset;
63 K = Spelling.getValueAsBitOrUnset("KnownToGCC", Unset);
64 }
65
66 const std::string &variety() const { return V; }
67 const std::string &name() const { return N; }
68 const std::string &nameSpace() const { return NS; }
69 bool knownToGCC() const { return K; }
70};
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +000071
Hans Wennborgdcfba332015-10-06 23:40:43 +000072} // end anonymous namespace
Aaron Ballmanc669cc02014-01-27 22:10:04 +000073
Benjamin Kramerd910d162015-03-10 18:24:01 +000074static std::vector<FlattenedSpelling>
75GetFlattenedSpellings(const Record &Attr) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +000076 std::vector<Record *> Spellings = Attr.getValueAsListOfDefs("Spellings");
77 std::vector<FlattenedSpelling> Ret;
78
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +000079 for (const auto &Spelling : Spellings) {
Aaron Ballmanffc43362017-10-26 12:19:02 +000080 StringRef Variety = Spelling->getValueAsString("Variety");
81 StringRef Name = Spelling->getValueAsString("Name");
82 if (Variety == "GCC") {
Aaron Ballmanc669cc02014-01-27 22:10:04 +000083 // Gin up two new spelling objects to add into the list.
Aaron Ballmanffc43362017-10-26 12:19:02 +000084 Ret.emplace_back("GNU", Name, "", true);
85 Ret.emplace_back("CXX11", Name, "gnu", true);
86 } else if (Variety == "Clang") {
87 Ret.emplace_back("GNU", Name, "", false);
88 Ret.emplace_back("CXX11", Name, "clang", false);
Aaron Ballman10007812018-01-03 22:22:48 +000089 if (Spelling->getValueAsBit("AllowInC"))
90 Ret.emplace_back("C2x", Name, "clang", false);
Aaron Ballmanc669cc02014-01-27 22:10:04 +000091 } else
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +000092 Ret.push_back(FlattenedSpelling(*Spelling));
Aaron Ballmanc669cc02014-01-27 22:10:04 +000093 }
94
95 return Ret;
96}
97
Peter Collingbournebee583f2011-10-06 13:03:08 +000098static std::string ReadPCHRecord(StringRef type) {
99 return StringSwitch<std::string>(type)
David L. Jones267b8842017-01-24 01:04:30 +0000100 .EndsWith("Decl *", "Record.GetLocalDeclAs<"
101 + std::string(type, 0, type.size()-1) + ">(Record.readInt())")
102 .Case("TypeSourceInfo *", "Record.getTypeSourceInfo()")
103 .Case("Expr *", "Record.readExpr()")
104 .Case("IdentifierInfo *", "Record.getIdentifierInfo()")
105 .Case("StringRef", "Record.readString()")
Joel E. Denny81508102018-03-13 14:51:22 +0000106 .Case("ParamIdx", "ParamIdx::deserialize(Record.readInt())")
David L. Jones267b8842017-01-24 01:04:30 +0000107 .Default("Record.readInt()");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000108}
109
Richard Smith19978562016-05-18 00:16:51 +0000110// Get a type that is suitable for storing an object of the specified type.
111static StringRef getStorageType(StringRef type) {
112 return StringSwitch<StringRef>(type)
113 .Case("StringRef", "std::string")
114 .Default(type);
115}
116
Peter Collingbournebee583f2011-10-06 13:03:08 +0000117// Assumes that the way to get the value is SA->getname()
118static std::string WritePCHRecord(StringRef type, StringRef name) {
Richard Smith290d8012016-04-06 17:06:00 +0000119 return "Record." + StringSwitch<std::string>(type)
120 .EndsWith("Decl *", "AddDeclRef(" + std::string(name) + ");\n")
121 .Case("TypeSourceInfo *", "AddTypeSourceInfo(" + std::string(name) + ");\n")
Peter Collingbournebee583f2011-10-06 13:03:08 +0000122 .Case("Expr *", "AddStmt(" + std::string(name) + ");\n")
Richard Smith290d8012016-04-06 17:06:00 +0000123 .Case("IdentifierInfo *", "AddIdentifierRef(" + std::string(name) + ");\n")
124 .Case("StringRef", "AddString(" + std::string(name) + ");\n")
Joel E. Denny81508102018-03-13 14:51:22 +0000125 .Case("ParamIdx", "push_back(" + std::string(name) + ".serialize());\n")
Richard Smith290d8012016-04-06 17:06:00 +0000126 .Default("push_back(" + std::string(name) + ");\n");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000127}
128
Michael Han4a045172012-03-07 00:12:16 +0000129// Normalize attribute name by removing leading and trailing
130// underscores. For example, __foo, foo__, __foo__ would
131// become foo.
132static StringRef NormalizeAttrName(StringRef AttrName) {
George Burgess IV1881a572016-12-01 00:13:18 +0000133 AttrName.consume_front("__");
134 AttrName.consume_back("__");
Michael Han4a045172012-03-07 00:12:16 +0000135 return AttrName;
136}
137
Aaron Ballman36a53502014-01-16 13:03:14 +0000138// Normalize the name by removing any and all leading and trailing underscores.
139// This is different from NormalizeAttrName in that it also handles names like
140// _pascal and __pascal.
141static StringRef NormalizeNameForSpellingComparison(StringRef Name) {
Benjamin Kramer5c404072015-04-10 21:37:21 +0000142 return Name.trim("_");
Aaron Ballman36a53502014-01-16 13:03:14 +0000143}
144
Justin Lebar4086fe52017-01-05 16:51:54 +0000145// Normalize the spelling of a GNU attribute (i.e. "x" in "__attribute__((x))"),
146// removing "__" if it appears at the beginning and end of the attribute's name.
147static StringRef NormalizeGNUAttrSpelling(StringRef AttrSpelling) {
Michael Han4a045172012-03-07 00:12:16 +0000148 if (AttrSpelling.startswith("__") && AttrSpelling.endswith("__")) {
149 AttrSpelling = AttrSpelling.substr(2, AttrSpelling.size() - 4);
150 }
151
152 return AttrSpelling;
153}
154
Aaron Ballman2f22b942014-05-20 19:47:14 +0000155typedef std::vector<std::pair<std::string, const Record *>> ParsedAttrMap;
Aaron Ballman64e69862013-12-15 13:05:48 +0000156
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000157static ParsedAttrMap getParsedAttrList(const RecordKeeper &Records,
Craig Topper8ae12032014-05-07 06:21:57 +0000158 ParsedAttrMap *Dupes = nullptr) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000159 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Aaron Ballman64e69862013-12-15 13:05:48 +0000160 std::set<std::string> Seen;
161 ParsedAttrMap R;
Aaron Ballman2f22b942014-05-20 19:47:14 +0000162 for (const auto *Attr : Attrs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000163 if (Attr->getValueAsBit("SemaHandler")) {
Aaron Ballman64e69862013-12-15 13:05:48 +0000164 std::string AN;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000165 if (Attr->isSubClassOf("TargetSpecificAttr") &&
166 !Attr->isValueUnset("ParseKind")) {
167 AN = Attr->getValueAsString("ParseKind");
Aaron Ballman64e69862013-12-15 13:05:48 +0000168
169 // If this attribute has already been handled, it does not need to be
170 // handled again.
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000171 if (Seen.find(AN) != Seen.end()) {
172 if (Dupes)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000173 Dupes->push_back(std::make_pair(AN, Attr));
Aaron Ballman64e69862013-12-15 13:05:48 +0000174 continue;
Aaron Ballmanab7691c2014-01-09 22:48:32 +0000175 }
Aaron Ballman64e69862013-12-15 13:05:48 +0000176 Seen.insert(AN);
177 } else
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000178 AN = NormalizeAttrName(Attr->getName()).str();
Aaron Ballman64e69862013-12-15 13:05:48 +0000179
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000180 R.push_back(std::make_pair(AN, Attr));
Aaron Ballman64e69862013-12-15 13:05:48 +0000181 }
182 }
183 return R;
184}
185
Peter Collingbournebee583f2011-10-06 13:03:08 +0000186namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000187
Peter Collingbournebee583f2011-10-06 13:03:08 +0000188 class Argument {
189 std::string lowerName, upperName;
190 StringRef attrName;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000191 bool isOpt;
John McCalla62c1a92015-10-28 00:17:34 +0000192 bool Fake;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000193
194 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000195 Argument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000196 : lowerName(Arg.getValueAsString("Name")), upperName(lowerName),
John McCalla62c1a92015-10-28 00:17:34 +0000197 attrName(Attr), isOpt(false), Fake(false) {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000198 if (!lowerName.empty()) {
199 lowerName[0] = std::tolower(lowerName[0]);
200 upperName[0] = std::toupper(upperName[0]);
201 }
Reid Klecknerebeb0ca2016-05-31 17:42:56 +0000202 // Work around MinGW's macro definition of 'interface' to 'struct'. We
203 // have an attribute argument called 'Interface', so only the lower case
204 // name conflicts with the macro definition.
205 if (lowerName == "interface")
206 lowerName = "interface_";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000207 }
Hans Wennborgdcfba332015-10-06 23:40:43 +0000208 virtual ~Argument() = default;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000209
210 StringRef getLowerName() const { return lowerName; }
211 StringRef getUpperName() const { return upperName; }
212 StringRef getAttrName() const { return attrName; }
213
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000214 bool isOptional() const { return isOpt; }
215 void setOptional(bool set) { isOpt = set; }
216
John McCalla62c1a92015-10-28 00:17:34 +0000217 bool isFake() const { return Fake; }
218 void setFake(bool fake) { Fake = fake; }
219
Peter Collingbournebee583f2011-10-06 13:03:08 +0000220 // These functions print the argument contents formatted in different ways.
221 virtual void writeAccessors(raw_ostream &OS) const = 0;
222 virtual void writeAccessorDefinitions(raw_ostream &OS) const {}
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +0000223 virtual void writeASTVisitorTraversal(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000224 virtual void writeCloneArgs(raw_ostream &OS) const = 0;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000225 virtual void writeTemplateInstantiationArgs(raw_ostream &OS) const = 0;
Daniel Dunbardc51baa2012-02-10 06:00:29 +0000226 virtual void writeTemplateInstantiation(raw_ostream &OS) const {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000227 virtual void writeCtorBody(raw_ostream &OS) const {}
228 virtual void writeCtorInitializers(raw_ostream &OS) const = 0;
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000229 virtual void writeCtorDefaultInitializers(raw_ostream &OS) const = 0;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000230 virtual void writeCtorParameters(raw_ostream &OS) const = 0;
231 virtual void writeDeclarations(raw_ostream &OS) const = 0;
232 virtual void writePCHReadArgs(raw_ostream &OS) const = 0;
233 virtual void writePCHReadDecls(raw_ostream &OS) const = 0;
234 virtual void writePCHWrite(raw_ostream &OS) const = 0;
Aaron Ballman48a533d2018-02-27 23:49:28 +0000235 virtual std::string getIsOmitted() const { return "false"; }
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000236 virtual void writeValue(raw_ostream &OS) const = 0;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000237 virtual void writeDump(raw_ostream &OS) const = 0;
238 virtual void writeDumpChildren(raw_ostream &OS) const {}
Richard Trieude5cc7d2013-01-31 01:44:26 +0000239 virtual void writeHasChildren(raw_ostream &OS) const { OS << "false"; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000240
241 virtual bool isEnumArg() const { return false; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000242 virtual bool isVariadicEnumArg() const { return false; }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000243 virtual bool isVariadic() const { return false; }
Aaron Ballman36a53502014-01-16 13:03:14 +0000244
245 virtual void writeImplicitCtorArgs(raw_ostream &OS) const {
246 OS << getUpperName();
247 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000248 };
249
250 class SimpleArgument : public Argument {
251 std::string type;
252
253 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000254 SimpleArgument(const Record &Arg, StringRef Attr, std::string T)
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000255 : Argument(Arg, Attr), type(std::move(T)) {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000256
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000257 std::string getType() const { return type; }
258
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000259 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000260 OS << " " << type << " get" << getUpperName() << "() const {\n";
261 OS << " return " << getLowerName() << ";\n";
262 OS << " }";
263 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000264
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000265 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000266 OS << getLowerName();
267 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000268
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000269 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000270 OS << "A->get" << getUpperName() << "()";
271 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000272
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000273 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000274 OS << getLowerName() << "(" << getUpperName() << ")";
275 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000276
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000277 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000278 OS << getLowerName() << "()";
279 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000280
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000281 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000282 OS << type << " " << getUpperName();
283 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000284
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000285 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000286 OS << type << " " << getLowerName() << ";";
287 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000288
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000289 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000290 std::string read = ReadPCHRecord(type);
291 OS << " " << type << " " << getLowerName() << " = " << read << ";\n";
292 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000293
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000294 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000295 OS << getLowerName();
296 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000297
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000298 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000299 OS << " " << WritePCHRecord(type, "SA->get" +
300 std::string(getUpperName()) + "()");
301 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000302
Aaron Ballman48a533d2018-02-27 23:49:28 +0000303 std::string getIsOmitted() const override {
304 if (type == "IdentifierInfo *")
305 return "!get" + getUpperName().str() + "()";
Matthias Gehred293cbd2019-07-25 17:50:51 +0000306 if (type == "TypeSourceInfo *")
307 return "!get" + getUpperName().str() + "Loc()";
Joel E. Denny81508102018-03-13 14:51:22 +0000308 if (type == "ParamIdx")
309 return "!get" + getUpperName().str() + "().isValid()";
Aaron Ballman48a533d2018-02-27 23:49:28 +0000310 return "false";
311 }
312
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000313 void writeValue(raw_ostream &OS) const override {
Aaron Ballman48a533d2018-02-27 23:49:28 +0000314 if (type == "FunctionDecl *")
Richard Smithb87c4652013-10-31 21:23:20 +0000315 OS << "\" << get" << getUpperName()
316 << "()->getNameInfo().getAsString() << \"";
Aaron Ballman48a533d2018-02-27 23:49:28 +0000317 else if (type == "IdentifierInfo *")
318 // Some non-optional (comma required) identifier arguments can be the
319 // empty string but are then recorded as a nullptr.
320 OS << "\" << (get" << getUpperName() << "() ? get" << getUpperName()
321 << "()->getName() : \"\") << \"";
322 else if (type == "TypeSourceInfo *")
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000323 OS << "\" << get" << getUpperName() << "().getAsString() << \"";
Joel E. Denny81508102018-03-13 14:51:22 +0000324 else if (type == "ParamIdx")
325 OS << "\" << get" << getUpperName() << "().getSourceIndex() << \"";
Aaron Ballman48a533d2018-02-27 23:49:28 +0000326 else
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000327 OS << "\" << get" << getUpperName() << "() << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000328 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000329
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000330 void writeDump(raw_ostream &OS) const override {
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +0000331 if (type == "FunctionDecl *" || type == "NamedDecl *") {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000332 OS << " OS << \" \";\n";
333 OS << " dumpBareDeclRef(SA->get" << getUpperName() << "());\n";
334 } else if (type == "IdentifierInfo *") {
Aaron Ballman48a533d2018-02-27 23:49:28 +0000335 // Some non-optional (comma required) identifier arguments can be the
336 // empty string but are then recorded as a nullptr.
337 OS << " if (SA->get" << getUpperName() << "())\n"
338 << " OS << \" \" << SA->get" << getUpperName()
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000339 << "()->getName();\n";
Richard Smithb87c4652013-10-31 21:23:20 +0000340 } else if (type == "TypeSourceInfo *") {
Matthias Gehred293cbd2019-07-25 17:50:51 +0000341 if (isOptional())
342 OS << " if (SA->get" << getUpperName() << "Loc())";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000343 OS << " OS << \" \" << SA->get" << getUpperName()
344 << "().getAsString();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000345 } else if (type == "bool") {
346 OS << " if (SA->get" << getUpperName() << "()) OS << \" "
347 << getUpperName() << "\";\n";
348 } else if (type == "int" || type == "unsigned") {
349 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
Joel E. Denny81508102018-03-13 14:51:22 +0000350 } else if (type == "ParamIdx") {
351 if (isOptional())
352 OS << " if (SA->get" << getUpperName() << "().isValid())\n ";
353 OS << " OS << \" \" << SA->get" << getUpperName()
354 << "().getSourceIndex();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000355 } else {
356 llvm_unreachable("Unknown SimpleArgument type!");
357 }
358 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000359 };
360
Aaron Ballman18a78382013-11-21 00:28:23 +0000361 class DefaultSimpleArgument : public SimpleArgument {
362 int64_t Default;
363
364 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000365 DefaultSimpleArgument(const Record &Arg, StringRef Attr,
Aaron Ballman18a78382013-11-21 00:28:23 +0000366 std::string T, int64_t Default)
367 : SimpleArgument(Arg, Attr, T), Default(Default) {}
368
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000369 void writeAccessors(raw_ostream &OS) const override {
Aaron Ballman18a78382013-11-21 00:28:23 +0000370 SimpleArgument::writeAccessors(OS);
371
372 OS << "\n\n static const " << getType() << " Default" << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000373 << " = ";
374 if (getType() == "bool")
375 OS << (Default != 0 ? "true" : "false");
376 else
377 OS << Default;
378 OS << ";";
Aaron Ballman18a78382013-11-21 00:28:23 +0000379 }
380 };
381
Peter Collingbournebee583f2011-10-06 13:03:08 +0000382 class StringArgument : public Argument {
383 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000384 StringArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000385 : Argument(Arg, Attr)
386 {}
387
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000388 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000389 OS << " llvm::StringRef get" << getUpperName() << "() const {\n";
390 OS << " return llvm::StringRef(" << getLowerName() << ", "
391 << getLowerName() << "Length);\n";
392 OS << " }\n";
393 OS << " unsigned get" << getUpperName() << "Length() const {\n";
394 OS << " return " << getLowerName() << "Length;\n";
395 OS << " }\n";
396 OS << " void set" << getUpperName()
397 << "(ASTContext &C, llvm::StringRef S) {\n";
398 OS << " " << getLowerName() << "Length = S.size();\n";
399 OS << " this->" << getLowerName() << " = new (C, 1) char ["
400 << getLowerName() << "Length];\n";
Chandler Carruth38a45cc2015-08-04 03:53:01 +0000401 OS << " if (!S.empty())\n";
402 OS << " std::memcpy(this->" << getLowerName() << ", S.data(), "
Peter Collingbournebee583f2011-10-06 13:03:08 +0000403 << getLowerName() << "Length);\n";
404 OS << " }";
405 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000406
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000407 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000408 OS << "get" << getUpperName() << "()";
409 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000410
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000411 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000412 OS << "A->get" << getUpperName() << "()";
413 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000414
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000415 void writeCtorBody(raw_ostream &OS) const override {
Chandler Carruth38a45cc2015-08-04 03:53:01 +0000416 OS << " if (!" << getUpperName() << ".empty())\n";
417 OS << " std::memcpy(" << getLowerName() << ", " << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000418 << ".data(), " << getLowerName() << "Length);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000419 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000420
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000421 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000422 OS << getLowerName() << "Length(" << getUpperName() << ".size()),"
423 << getLowerName() << "(new (Ctx, 1) char[" << getLowerName()
424 << "Length])";
425 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000426
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000427 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Hans Wennborg59dbe862015-09-29 20:56:43 +0000428 OS << getLowerName() << "Length(0)," << getLowerName() << "(nullptr)";
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000429 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000430
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000431 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000432 OS << "llvm::StringRef " << getUpperName();
433 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000434
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000435 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000436 OS << "unsigned " << getLowerName() << "Length;\n";
437 OS << "char *" << getLowerName() << ";";
438 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000439
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000440 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000441 OS << " std::string " << getLowerName()
David L. Jones267b8842017-01-24 01:04:30 +0000442 << "= Record.readString();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000443 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000444
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000445 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000446 OS << getLowerName();
447 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000448
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000449 void writePCHWrite(raw_ostream &OS) const override {
Richard Smith290d8012016-04-06 17:06:00 +0000450 OS << " Record.AddString(SA->get" << getUpperName() << "());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000451 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000452
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000453 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000454 OS << "\\\"\" << get" << getUpperName() << "() << \"\\\"";
455 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000456
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000457 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000458 OS << " OS << \" \\\"\" << SA->get" << getUpperName()
459 << "() << \"\\\"\";\n";
460 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000461 };
462
463 class AlignedArgument : public Argument {
464 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000465 AlignedArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000466 : Argument(Arg, Attr)
467 {}
468
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000469 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000470 OS << " bool is" << getUpperName() << "Dependent() const;\n";
471
472 OS << " unsigned get" << getUpperName() << "(ASTContext &Ctx) const;\n";
473
474 OS << " bool is" << getUpperName() << "Expr() const {\n";
475 OS << " return is" << getLowerName() << "Expr;\n";
476 OS << " }\n";
477
478 OS << " Expr *get" << getUpperName() << "Expr() const {\n";
479 OS << " assert(is" << getLowerName() << "Expr);\n";
480 OS << " return " << getLowerName() << "Expr;\n";
481 OS << " }\n";
482
483 OS << " TypeSourceInfo *get" << getUpperName() << "Type() const {\n";
484 OS << " assert(!is" << getLowerName() << "Expr);\n";
485 OS << " return " << getLowerName() << "Type;\n";
486 OS << " }";
487 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000488
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000489 void writeAccessorDefinitions(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000490 OS << "bool " << getAttrName() << "Attr::is" << getUpperName()
491 << "Dependent() const {\n";
492 OS << " if (is" << getLowerName() << "Expr)\n";
493 OS << " return " << getLowerName() << "Expr && (" << getLowerName()
494 << "Expr->isValueDependent() || " << getLowerName()
495 << "Expr->isTypeDependent());\n";
496 OS << " else\n";
497 OS << " return " << getLowerName()
498 << "Type->getType()->isDependentType();\n";
499 OS << "}\n";
500
501 // FIXME: Do not do the calculation here
502 // FIXME: Handle types correctly
503 // A null pointer means maximum alignment
Peter Collingbournebee583f2011-10-06 13:03:08 +0000504 OS << "unsigned " << getAttrName() << "Attr::get" << getUpperName()
505 << "(ASTContext &Ctx) const {\n";
506 OS << " assert(!is" << getUpperName() << "Dependent());\n";
507 OS << " if (is" << getLowerName() << "Expr)\n";
Ulrich Weigandca3cb7f2015-04-21 17:29:35 +0000508 OS << " return " << getLowerName() << "Expr ? " << getLowerName()
509 << "Expr->EvaluateKnownConstInt(Ctx).getZExtValue()"
510 << " * Ctx.getCharWidth() : "
511 << "Ctx.getTargetDefaultAlignForAttributeAligned();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000512 OS << " else\n";
513 OS << " return 0; // FIXME\n";
514 OS << "}\n";
515 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000516
Richard Smithf26d5512017-08-15 22:58:45 +0000517 void writeASTVisitorTraversal(raw_ostream &OS) const override {
518 StringRef Name = getUpperName();
519 OS << " if (A->is" << Name << "Expr()) {\n"
520 << " if (!getDerived().TraverseStmt(A->get" << Name << "Expr()))\n"
521 << " return false;\n"
522 << " } else if (auto *TSI = A->get" << Name << "Type()) {\n"
523 << " if (!getDerived().TraverseTypeLoc(TSI->getTypeLoc()))\n"
524 << " return false;\n"
525 << " }\n";
526 }
527
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000528 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000529 OS << "is" << getLowerName() << "Expr, is" << getLowerName()
530 << "Expr ? static_cast<void*>(" << getLowerName()
531 << "Expr) : " << getLowerName()
532 << "Type";
533 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000534
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000535 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000536 // FIXME: move the definition in Sema::InstantiateAttrs to here.
537 // In the meantime, aligned attributes are cloned.
538 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000539
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000540 void writeCtorBody(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000541 OS << " if (is" << getLowerName() << "Expr)\n";
542 OS << " " << getLowerName() << "Expr = reinterpret_cast<Expr *>("
543 << getUpperName() << ");\n";
544 OS << " else\n";
545 OS << " " << getLowerName()
546 << "Type = reinterpret_cast<TypeSourceInfo *>(" << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000547 << ");\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000548 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000549
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000550 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000551 OS << "is" << getLowerName() << "Expr(Is" << getUpperName() << "Expr)";
552 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000553
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000554 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000555 OS << "is" << getLowerName() << "Expr(false)";
556 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000557
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000558 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000559 OS << "bool Is" << getUpperName() << "Expr, void *" << getUpperName();
560 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000561
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000562 void writeImplicitCtorArgs(raw_ostream &OS) const override {
Aaron Ballman36a53502014-01-16 13:03:14 +0000563 OS << "Is" << getUpperName() << "Expr, " << getUpperName();
564 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000565
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000566 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000567 OS << "bool is" << getLowerName() << "Expr;\n";
568 OS << "union {\n";
569 OS << "Expr *" << getLowerName() << "Expr;\n";
570 OS << "TypeSourceInfo *" << getLowerName() << "Type;\n";
571 OS << "};";
572 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000573
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000574 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000575 OS << "is" << getLowerName() << "Expr, " << getLowerName() << "Ptr";
576 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000577
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000578 void writePCHReadDecls(raw_ostream &OS) const override {
David L. Jones267b8842017-01-24 01:04:30 +0000579 OS << " bool is" << getLowerName() << "Expr = Record.readInt();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000580 OS << " void *" << getLowerName() << "Ptr;\n";
581 OS << " if (is" << getLowerName() << "Expr)\n";
David L. Jones267b8842017-01-24 01:04:30 +0000582 OS << " " << getLowerName() << "Ptr = Record.readExpr();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000583 OS << " else\n";
584 OS << " " << getLowerName()
David L. Jones267b8842017-01-24 01:04:30 +0000585 << "Ptr = Record.getTypeSourceInfo();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000586 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000587
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000588 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000589 OS << " Record.push_back(SA->is" << getUpperName() << "Expr());\n";
590 OS << " if (SA->is" << getUpperName() << "Expr())\n";
Richard Smith290d8012016-04-06 17:06:00 +0000591 OS << " Record.AddStmt(SA->get" << getUpperName() << "Expr());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000592 OS << " else\n";
Richard Smith290d8012016-04-06 17:06:00 +0000593 OS << " Record.AddTypeSourceInfo(SA->get" << getUpperName()
594 << "Type());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000595 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000596
Aaron Ballman48a533d2018-02-27 23:49:28 +0000597 std::string getIsOmitted() const override {
598 return "!is" + getLowerName().str() + "Expr || !" + getLowerName().str()
599 + "Expr";
600 }
601
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000602 void writeValue(raw_ostream &OS) const override {
Richard Trieuddd01ce2014-06-09 22:53:25 +0000603 OS << "\";\n";
Aaron Ballman48a533d2018-02-27 23:49:28 +0000604 OS << " " << getLowerName()
605 << "Expr->printPretty(OS, nullptr, Policy);\n";
Richard Trieuddd01ce2014-06-09 22:53:25 +0000606 OS << " OS << \"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000607 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000608
Stephen Kellydb8fac12019-01-11 19:16:01 +0000609 void writeDump(raw_ostream &OS) const override {
610 OS << " if (!SA->is" << getUpperName() << "Expr())\n";
611 OS << " dumpType(SA->get" << getUpperName()
612 << "Type()->getType());\n";
613 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000614
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000615 void writeDumpChildren(raw_ostream &OS) const override {
Richard Smithf7514452014-10-30 21:02:37 +0000616 OS << " if (SA->is" << getUpperName() << "Expr())\n";
Stephen Kelly6d110d62019-01-30 19:49:49 +0000617 OS << " Visit(SA->get" << getUpperName() << "Expr());\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000618 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000619
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000620 void writeHasChildren(raw_ostream &OS) const override {
Richard Trieude5cc7d2013-01-31 01:44:26 +0000621 OS << "SA->is" << getUpperName() << "Expr()";
622 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000623 };
624
625 class VariadicArgument : public Argument {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000626 std::string Type, ArgName, ArgSizeName, RangeName;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000627
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000628 protected:
629 // Assumed to receive a parameter: raw_ostream OS.
630 virtual void writeValueImpl(raw_ostream &OS) const {
631 OS << " OS << Val;\n";
632 }
Joel E. Denny81508102018-03-13 14:51:22 +0000633 // Assumed to receive a parameter: raw_ostream OS.
634 virtual void writeDumpImpl(raw_ostream &OS) const {
635 OS << " OS << \" \" << Val;\n";
636 }
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000637
Peter Collingbournebee583f2011-10-06 13:03:08 +0000638 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000639 VariadicArgument(const Record &Arg, StringRef Attr, std::string T)
Benjamin Kramercfeacf52016-05-27 14:27:13 +0000640 : Argument(Arg, Attr), Type(std::move(T)),
641 ArgName(getLowerName().str() + "_"), ArgSizeName(ArgName + "Size"),
642 RangeName(getLowerName()) {}
Peter Collingbournebee583f2011-10-06 13:03:08 +0000643
Benjamin Kramer1b582012016-02-13 18:11:49 +0000644 const std::string &getType() const { return Type; }
645 const std::string &getArgName() const { return ArgName; }
646 const std::string &getArgSizeName() const { return ArgSizeName; }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +0000647 bool isVariadic() const override { return true; }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000648
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000649 void writeAccessors(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000650 std::string IteratorType = getLowerName().str() + "_iterator";
651 std::string BeginFn = getLowerName().str() + "_begin()";
652 std::string EndFn = getLowerName().str() + "_end()";
653
654 OS << " typedef " << Type << "* " << IteratorType << ";\n";
655 OS << " " << IteratorType << " " << BeginFn << " const {"
656 << " return " << ArgName << "; }\n";
657 OS << " " << IteratorType << " " << EndFn << " const {"
658 << " return " << ArgName << " + " << ArgSizeName << "; }\n";
659 OS << " unsigned " << getLowerName() << "_size() const {"
660 << " return " << ArgSizeName << "; }\n";
661 OS << " llvm::iterator_range<" << IteratorType << "> " << RangeName
662 << "() const { return llvm::make_range(" << BeginFn << ", " << EndFn
663 << "); }\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000664 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000665
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000666 void writeCloneArgs(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000667 OS << ArgName << ", " << ArgSizeName;
Peter Collingbournebee583f2011-10-06 13:03:08 +0000668 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000669
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000670 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000671 // This isn't elegant, but we have to go through public methods...
672 OS << "A->" << getLowerName() << "_begin(), "
673 << "A->" << getLowerName() << "_size()";
674 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000675
Richard Smithf26d5512017-08-15 22:58:45 +0000676 void writeASTVisitorTraversal(raw_ostream &OS) const override {
677 // FIXME: Traverse the elements.
678 }
679
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000680 void writeCtorBody(raw_ostream &OS) const override {
Aaron Ballmand6459e52014-05-01 15:21:03 +0000681 OS << " std::copy(" << getUpperName() << ", " << getUpperName()
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000682 << " + " << ArgSizeName << ", " << ArgName << ");\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000683 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000684
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000685 void writeCtorInitializers(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000686 OS << ArgSizeName << "(" << getUpperName() << "Size), "
687 << ArgName << "(new (Ctx, 16) " << getType() << "["
688 << ArgSizeName << "])";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000689 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000690
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000691 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000692 OS << ArgSizeName << "(0), " << ArgName << "(nullptr)";
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000693 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000694
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000695 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000696 OS << getType() << " *" << getUpperName() << ", unsigned "
697 << getUpperName() << "Size";
698 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000699
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000700 void writeImplicitCtorArgs(raw_ostream &OS) const override {
Aaron Ballman36a53502014-01-16 13:03:14 +0000701 OS << getUpperName() << ", " << getUpperName() << "Size";
702 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000703
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000704 void writeDeclarations(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000705 OS << " unsigned " << ArgSizeName << ";\n";
706 OS << " " << getType() << " *" << ArgName << ";";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000707 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000708
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000709 void writePCHReadDecls(raw_ostream &OS) const override {
David L. Jones267b8842017-01-24 01:04:30 +0000710 OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n";
Richard Smith19978562016-05-18 00:16:51 +0000711 OS << " SmallVector<" << getType() << ", 4> "
712 << getLowerName() << ";\n";
713 OS << " " << getLowerName() << ".reserve(" << getLowerName()
Peter Collingbournebee583f2011-10-06 13:03:08 +0000714 << "Size);\n";
Richard Smith19978562016-05-18 00:16:51 +0000715
716 // If we can't store the values in the current type (if it's something
717 // like StringRef), store them in a different type and convert the
718 // container afterwards.
719 std::string StorageType = getStorageType(getType());
720 std::string StorageName = getLowerName();
721 if (StorageType != getType()) {
722 StorageName += "Storage";
723 OS << " SmallVector<" << StorageType << ", 4> "
724 << StorageName << ";\n";
725 OS << " " << StorageName << ".reserve(" << getLowerName()
726 << "Size);\n";
727 }
728
729 OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n";
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000730 std::string read = ReadPCHRecord(Type);
Richard Smith19978562016-05-18 00:16:51 +0000731 OS << " " << StorageName << ".push_back(" << read << ");\n";
732
733 if (StorageType != getType()) {
734 OS << " for (unsigned i = 0; i != " << getLowerName() << "Size; ++i)\n";
735 OS << " " << getLowerName() << ".push_back("
736 << StorageName << "[i]);\n";
737 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000738 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000739
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000740 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000741 OS << getLowerName() << ".data(), " << getLowerName() << "Size";
742 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000743
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000744 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000745 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000746 OS << " for (auto &Val : SA->" << RangeName << "())\n";
747 OS << " " << WritePCHRecord(Type, "Val");
Peter Collingbournebee583f2011-10-06 13:03:08 +0000748 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000749
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000750 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000751 OS << "\";\n";
752 OS << " bool isFirst = true;\n"
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000753 << " for (const auto &Val : " << RangeName << "()) {\n"
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000754 << " if (isFirst) isFirst = false;\n"
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000755 << " else OS << \", \";\n";
756 writeValueImpl(OS);
757 OS << " }\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000758 OS << " OS << \"";
759 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000760
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000761 void writeDump(raw_ostream &OS) const override {
Aaron Ballmana82eaa72014-05-02 13:35:42 +0000762 OS << " for (const auto &Val : SA->" << RangeName << "())\n";
Joel E. Denny81508102018-03-13 14:51:22 +0000763 writeDumpImpl(OS);
764 }
765 };
766
767 class VariadicParamIdxArgument : public VariadicArgument {
768 public:
769 VariadicParamIdxArgument(const Record &Arg, StringRef Attr)
770 : VariadicArgument(Arg, Attr, "ParamIdx") {}
771
772 public:
773 void writeValueImpl(raw_ostream &OS) const override {
774 OS << " OS << Val.getSourceIndex();\n";
775 }
776
777 void writeDumpImpl(raw_ostream &OS) const override {
778 OS << " OS << \" \" << Val.getSourceIndex();\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000779 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000780 };
781
Johannes Doerfertac991bb2019-01-19 05:36:54 +0000782 struct VariadicParamOrParamIdxArgument : public VariadicArgument {
783 VariadicParamOrParamIdxArgument(const Record &Arg, StringRef Attr)
784 : VariadicArgument(Arg, Attr, "int") {}
785 };
786
Reid Klecknerf526b9482014-02-12 18:22:18 +0000787 // Unique the enums, but maintain the original declaration ordering.
Craig Topper00648582017-05-31 19:01:22 +0000788 std::vector<StringRef>
789 uniqueEnumsInOrder(const std::vector<StringRef> &enums) {
790 std::vector<StringRef> uniques;
George Burgess IV1881a572016-12-01 00:13:18 +0000791 SmallDenseSet<StringRef, 8> unique_set;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000792 for (const auto &i : enums) {
George Burgess IV1881a572016-12-01 00:13:18 +0000793 if (unique_set.insert(i).second)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000794 uniques.push_back(i);
Reid Klecknerf526b9482014-02-12 18:22:18 +0000795 }
796 return uniques;
797 }
798
Peter Collingbournebee583f2011-10-06 13:03:08 +0000799 class EnumArgument : public Argument {
800 std::string type;
Craig Topper00648582017-05-31 19:01:22 +0000801 std::vector<StringRef> values, enums, uniques;
802
Peter Collingbournebee583f2011-10-06 13:03:08 +0000803 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000804 EnumArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +0000805 : Argument(Arg, Attr), type(Arg.getValueAsString("Type")),
Aaron Ballman0e468c02014-01-05 21:08:29 +0000806 values(Arg.getValueAsListOfStrings("Values")),
807 enums(Arg.getValueAsListOfStrings("Enums")),
Reid Klecknerf526b9482014-02-12 18:22:18 +0000808 uniques(uniqueEnumsInOrder(enums))
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000809 {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000810 // FIXME: Emit a proper error
811 assert(!uniques.empty());
812 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000813
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000814 bool isEnumArg() const override { return true; }
Aaron Ballman682ee422013-09-11 19:47:58 +0000815
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000816 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000817 OS << " " << type << " get" << getUpperName() << "() const {\n";
818 OS << " return " << getLowerName() << ";\n";
819 OS << " }";
820 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000821
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000822 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000823 OS << getLowerName();
824 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000825
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000826 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +0000827 OS << "A->get" << getUpperName() << "()";
828 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000829 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000830 OS << getLowerName() << "(" << getUpperName() << ")";
831 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000832 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +0000833 OS << getLowerName() << "(" << type << "(0))";
834 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000835 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000836 OS << type << " " << getUpperName();
837 }
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000838 void writeDeclarations(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +0000839 auto i = uniques.cbegin(), e = uniques.cend();
Peter Collingbournebee583f2011-10-06 13:03:08 +0000840 // The last one needs to not have a comma.
841 --e;
842
843 OS << "public:\n";
844 OS << " enum " << type << " {\n";
845 for (; i != e; ++i)
846 OS << " " << *i << ",\n";
847 OS << " " << *e << "\n";
848 OS << " };\n";
849 OS << "private:\n";
850 OS << " " << type << " " << getLowerName() << ";";
851 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000852
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000853 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000854 OS << " " << getAttrName() << "Attr::" << type << " " << getLowerName()
855 << "(static_cast<" << getAttrName() << "Attr::" << type
David L. Jones267b8842017-01-24 01:04:30 +0000856 << ">(Record.readInt()));\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +0000857 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000858
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000859 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000860 OS << getLowerName();
861 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000862
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000863 void writePCHWrite(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +0000864 OS << "Record.push_back(SA->get" << getUpperName() << "());\n";
865 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000866
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000867 void writeValue(raw_ostream &OS) const override {
Aaron Ballman36d79102014-09-15 16:16:14 +0000868 // FIXME: this isn't 100% correct -- some enum arguments require printing
869 // as a string literal, while others require printing as an identifier.
870 // Tablegen currently does not distinguish between the two forms.
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000871 OS << "\\\"\" << " << getAttrName() << "Attr::Convert" << type << "ToStr(get"
872 << getUpperName() << "()) << \"\\\"";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +0000873 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000874
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000875 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000876 OS << " switch(SA->get" << getUpperName() << "()) {\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000877 for (const auto &I : uniques) {
878 OS << " case " << getAttrName() << "Attr::" << I << ":\n";
879 OS << " OS << \" " << I << "\";\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +0000880 OS << " break;\n";
881 }
882 OS << " }\n";
883 }
Aaron Ballman682ee422013-09-11 19:47:58 +0000884
885 void writeConversion(raw_ostream &OS) const {
886 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
887 OS << type << " &Out) {\n";
888 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000889 OS << type << ">>(Val)\n";
Aaron Ballman682ee422013-09-11 19:47:58 +0000890 for (size_t I = 0; I < enums.size(); ++I) {
891 OS << " .Case(\"" << values[I] << "\", ";
892 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
893 }
894 OS << " .Default(Optional<" << type << ">());\n";
895 OS << " if (R) {\n";
896 OS << " Out = *R;\n return true;\n }\n";
897 OS << " return false;\n";
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000898 OS << " }\n\n";
899
900 // Mapping from enumeration values back to enumeration strings isn't
901 // trivial because some enumeration values have multiple named
902 // enumerators, such as type_visibility(internal) and
903 // type_visibility(hidden) both mapping to TypeVisibilityAttr::Hidden.
904 OS << " static const char *Convert" << type << "ToStr("
905 << type << " Val) {\n"
906 << " switch(Val) {\n";
George Burgess IV1881a572016-12-01 00:13:18 +0000907 SmallDenseSet<StringRef, 8> Uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000908 for (size_t I = 0; I < enums.size(); ++I) {
909 if (Uniques.insert(enums[I]).second)
910 OS << " case " << getAttrName() << "Attr::" << enums[I]
911 << ": return \"" << values[I] << "\";\n";
912 }
913 OS << " }\n"
914 << " llvm_unreachable(\"No enumerator with that value\");\n"
915 << " }\n";
Aaron Ballman682ee422013-09-11 19:47:58 +0000916 }
Peter Collingbournebee583f2011-10-06 13:03:08 +0000917 };
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000918
919 class VariadicEnumArgument: public VariadicArgument {
920 std::string type, QualifiedTypeName;
Craig Topper00648582017-05-31 19:01:22 +0000921 std::vector<StringRef> values, enums, uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000922
923 protected:
924 void writeValueImpl(raw_ostream &OS) const override {
Aaron Ballman36d79102014-09-15 16:16:14 +0000925 // FIXME: this isn't 100% correct -- some enum arguments require printing
926 // as a string literal, while others require printing as an identifier.
927 // Tablegen currently does not distinguish between the two forms.
Aaron Ballman25a2cb92014-09-15 15:14:13 +0000928 OS << " OS << \"\\\"\" << " << getAttrName() << "Attr::Convert" << type
929 << "ToStr(Val)" << "<< \"\\\"\";\n";
930 }
931
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000932 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +0000933 VariadicEnumArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000934 : VariadicArgument(Arg, Attr, Arg.getValueAsString("Type")),
935 type(Arg.getValueAsString("Type")),
Aaron Ballman0e468c02014-01-05 21:08:29 +0000936 values(Arg.getValueAsListOfStrings("Values")),
937 enums(Arg.getValueAsListOfStrings("Enums")),
Reid Klecknerf526b9482014-02-12 18:22:18 +0000938 uniques(uniqueEnumsInOrder(enums))
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000939 {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000940 QualifiedTypeName = getAttrName().str() + "Attr::" + type;
941
942 // FIXME: Emit a proper error
943 assert(!uniques.empty());
944 }
945
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000946 bool isVariadicEnumArg() const override { return true; }
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000947
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000948 void writeDeclarations(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +0000949 auto i = uniques.cbegin(), e = uniques.cend();
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000950 // The last one needs to not have a comma.
951 --e;
952
953 OS << "public:\n";
954 OS << " enum " << type << " {\n";
955 for (; i != e; ++i)
956 OS << " " << *i << ",\n";
957 OS << " " << *e << "\n";
958 OS << " };\n";
959 OS << "private:\n";
960
961 VariadicArgument::writeDeclarations(OS);
962 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000963
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000964 void writeDump(raw_ostream &OS) const override {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000965 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
966 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
967 << getLowerName() << "_end(); I != E; ++I) {\n";
968 OS << " switch(*I) {\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +0000969 for (const auto &UI : uniques) {
970 OS << " case " << getAttrName() << "Attr::" << UI << ":\n";
971 OS << " OS << \" " << UI << "\";\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000972 OS << " break;\n";
973 }
974 OS << " }\n";
975 OS << " }\n";
976 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000977
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000978 void writePCHReadDecls(raw_ostream &OS) const override {
David L. Jones267b8842017-01-24 01:04:30 +0000979 OS << " unsigned " << getLowerName() << "Size = Record.readInt();\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000980 OS << " SmallVector<" << QualifiedTypeName << ", 4> " << getLowerName()
981 << ";\n";
982 OS << " " << getLowerName() << ".reserve(" << getLowerName()
983 << "Size);\n";
984 OS << " for (unsigned i = " << getLowerName() << "Size; i; --i)\n";
985 OS << " " << getLowerName() << ".push_back(" << "static_cast<"
David L. Jones267b8842017-01-24 01:04:30 +0000986 << QualifiedTypeName << ">(Record.readInt()));\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000987 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000988
Aaron Ballman8cbf6332014-03-06 15:09:50 +0000989 void writePCHWrite(raw_ostream &OS) const override {
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000990 OS << " Record.push_back(SA->" << getLowerName() << "_size());\n";
991 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
992 << "_iterator i = SA->" << getLowerName() << "_begin(), e = SA->"
993 << getLowerName() << "_end(); i != e; ++i)\n";
994 OS << " " << WritePCHRecord(QualifiedTypeName, "(*i)");
995 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +0000996
DeLesley Hutchins210791a2013-10-04 21:28:06 +0000997 void writeConversion(raw_ostream &OS) const {
998 OS << " static bool ConvertStrTo" << type << "(StringRef Val, ";
999 OS << type << " &Out) {\n";
1000 OS << " Optional<" << type << "> R = llvm::StringSwitch<Optional<";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001001 OS << type << ">>(Val)\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001002 for (size_t I = 0; I < enums.size(); ++I) {
1003 OS << " .Case(\"" << values[I] << "\", ";
1004 OS << getAttrName() << "Attr::" << enums[I] << ")\n";
1005 }
1006 OS << " .Default(Optional<" << type << ">());\n";
1007 OS << " if (R) {\n";
1008 OS << " Out = *R;\n return true;\n }\n";
1009 OS << " return false;\n";
Aaron Ballman25a2cb92014-09-15 15:14:13 +00001010 OS << " }\n\n";
1011
1012 OS << " static const char *Convert" << type << "ToStr("
1013 << type << " Val) {\n"
1014 << " switch(Val) {\n";
George Burgess IV1881a572016-12-01 00:13:18 +00001015 SmallDenseSet<StringRef, 8> Uniques;
Aaron Ballman25a2cb92014-09-15 15:14:13 +00001016 for (size_t I = 0; I < enums.size(); ++I) {
1017 if (Uniques.insert(enums[I]).second)
1018 OS << " case " << getAttrName() << "Attr::" << enums[I]
1019 << ": return \"" << values[I] << "\";\n";
1020 }
1021 OS << " }\n"
1022 << " llvm_unreachable(\"No enumerator with that value\");\n"
1023 << " }\n";
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001024 }
1025 };
Peter Collingbournebee583f2011-10-06 13:03:08 +00001026
1027 class VersionArgument : public Argument {
1028 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001029 VersionArgument(const Record &Arg, StringRef Attr)
Peter Collingbournebee583f2011-10-06 13:03:08 +00001030 : Argument(Arg, Attr)
1031 {}
1032
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001033 void writeAccessors(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001034 OS << " VersionTuple get" << getUpperName() << "() const {\n";
1035 OS << " return " << getLowerName() << ";\n";
1036 OS << " }\n";
1037 OS << " void set" << getUpperName()
1038 << "(ASTContext &C, VersionTuple V) {\n";
1039 OS << " " << getLowerName() << " = V;\n";
1040 OS << " }";
1041 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001042
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001043 void writeCloneArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001044 OS << "get" << getUpperName() << "()";
1045 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001046
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001047 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001048 OS << "A->get" << getUpperName() << "()";
1049 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001050
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001051 void writeCtorInitializers(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001052 OS << getLowerName() << "(" << getUpperName() << ")";
1053 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001054
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001055 void writeCtorDefaultInitializers(raw_ostream &OS) const override {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001056 OS << getLowerName() << "()";
1057 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001058
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001059 void writeCtorParameters(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001060 OS << "VersionTuple " << getUpperName();
1061 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001062
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001063 void writeDeclarations(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001064 OS << "VersionTuple " << getLowerName() << ";\n";
1065 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001066
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001067 void writePCHReadDecls(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001068 OS << " VersionTuple " << getLowerName()
David L. Jones267b8842017-01-24 01:04:30 +00001069 << "= Record.readVersionTuple();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001070 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001071
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001072 void writePCHReadArgs(raw_ostream &OS) const override {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001073 OS << getLowerName();
1074 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001075
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001076 void writePCHWrite(raw_ostream &OS) const override {
Richard Smith290d8012016-04-06 17:06:00 +00001077 OS << " Record.AddVersionTuple(SA->get" << getUpperName() << "());\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00001078 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001079
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001080 void writeValue(raw_ostream &OS) const override {
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001081 OS << getLowerName() << "=\" << get" << getUpperName() << "() << \"";
1082 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001083
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001084 void writeDump(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001085 OS << " OS << \" \" << SA->get" << getUpperName() << "();\n";
1086 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00001087 };
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001088
1089 class ExprArgument : public SimpleArgument {
1090 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001091 ExprArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001092 : SimpleArgument(Arg, Attr, "Expr *")
1093 {}
1094
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001095 void writeASTVisitorTraversal(raw_ostream &OS) const override {
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001096 OS << " if (!"
1097 << "getDerived().TraverseStmt(A->get" << getUpperName() << "()))\n";
1098 OS << " return false;\n";
1099 }
1100
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001101 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001102 OS << "tempInst" << getUpperName();
1103 }
1104
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001105 void writeTemplateInstantiation(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001106 OS << " " << getType() << " tempInst" << getUpperName() << ";\n";
1107 OS << " {\n";
1108 OS << " EnterExpressionEvaluationContext "
Faisal Valid143a0c2017-04-01 21:30:49 +00001109 << "Unevaluated(S, Sema::ExpressionEvaluationContext::Unevaluated);\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001110 OS << " ExprResult " << "Result = S.SubstExpr("
1111 << "A->get" << getUpperName() << "(), TemplateArgs);\n";
1112 OS << " tempInst" << getUpperName() << " = "
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001113 << "Result.getAs<Expr>();\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001114 OS << " }\n";
1115 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001116
Craig Topper3164f332014-03-11 03:39:26 +00001117 void writeDump(raw_ostream &OS) const override {}
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001118
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001119 void writeDumpChildren(raw_ostream &OS) const override {
Stephen Kelly6d110d62019-01-30 19:49:49 +00001120 OS << " Visit(SA->get" << getUpperName() << "());\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001121 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001122
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001123 void writeHasChildren(raw_ostream &OS) const override { OS << "true"; }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001124 };
1125
1126 class VariadicExprArgument : public VariadicArgument {
1127 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001128 VariadicExprArgument(const Record &Arg, StringRef Attr)
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001129 : VariadicArgument(Arg, Attr, "Expr *")
1130 {}
1131
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001132 void writeASTVisitorTraversal(raw_ostream &OS) const override {
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00001133 OS << " {\n";
1134 OS << " " << getType() << " *I = A->" << getLowerName()
1135 << "_begin();\n";
1136 OS << " " << getType() << " *E = A->" << getLowerName()
1137 << "_end();\n";
1138 OS << " for (; I != E; ++I) {\n";
1139 OS << " if (!getDerived().TraverseStmt(*I))\n";
1140 OS << " return false;\n";
1141 OS << " }\n";
1142 OS << " }\n";
1143 }
1144
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001145 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001146 OS << "tempInst" << getUpperName() << ", "
1147 << "A->" << getLowerName() << "_size()";
1148 }
1149
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001150 void writeTemplateInstantiation(raw_ostream &OS) const override {
Eugene Zelenko5f02b772015-12-08 18:49:01 +00001151 OS << " auto *tempInst" << getUpperName()
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001152 << " = new (C, 16) " << getType()
1153 << "[A->" << getLowerName() << "_size()];\n";
1154 OS << " {\n";
1155 OS << " EnterExpressionEvaluationContext "
Faisal Valid143a0c2017-04-01 21:30:49 +00001156 << "Unevaluated(S, Sema::ExpressionEvaluationContext::Unevaluated);\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001157 OS << " " << getType() << " *TI = tempInst" << getUpperName()
1158 << ";\n";
1159 OS << " " << getType() << " *I = A->" << getLowerName()
1160 << "_begin();\n";
1161 OS << " " << getType() << " *E = A->" << getLowerName()
1162 << "_end();\n";
1163 OS << " for (; I != E; ++I, ++TI) {\n";
1164 OS << " ExprResult Result = S.SubstExpr(*I, TemplateArgs);\n";
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001165 OS << " *TI = Result.getAs<Expr>();\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001166 OS << " }\n";
1167 OS << " }\n";
1168 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001169
Craig Topper3164f332014-03-11 03:39:26 +00001170 void writeDump(raw_ostream &OS) const override {}
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001171
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001172 void writeDumpChildren(raw_ostream &OS) const override {
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001173 OS << " for (" << getAttrName() << "Attr::" << getLowerName()
1174 << "_iterator I = SA->" << getLowerName() << "_begin(), E = SA->"
Richard Smithf7514452014-10-30 21:02:37 +00001175 << getLowerName() << "_end(); I != E; ++I)\n";
Stephen Kelly6d110d62019-01-30 19:49:49 +00001176 OS << " Visit(*I);\n";
Richard Trieude5cc7d2013-01-31 01:44:26 +00001177 }
1178
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001179 void writeHasChildren(raw_ostream &OS) const override {
Richard Trieude5cc7d2013-01-31 01:44:26 +00001180 OS << "SA->" << getLowerName() << "_begin() != "
1181 << "SA->" << getLowerName() << "_end()";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00001182 }
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00001183 };
Richard Smithb87c4652013-10-31 21:23:20 +00001184
Erich Keane3efe0022018-07-20 14:13:28 +00001185 class VariadicIdentifierArgument : public VariadicArgument {
1186 public:
1187 VariadicIdentifierArgument(const Record &Arg, StringRef Attr)
1188 : VariadicArgument(Arg, Attr, "IdentifierInfo *")
1189 {}
1190 };
1191
Peter Collingbourne915df992015-05-15 18:33:32 +00001192 class VariadicStringArgument : public VariadicArgument {
1193 public:
1194 VariadicStringArgument(const Record &Arg, StringRef Attr)
Benjamin Kramer1b582012016-02-13 18:11:49 +00001195 : VariadicArgument(Arg, Attr, "StringRef")
Peter Collingbourne915df992015-05-15 18:33:32 +00001196 {}
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001197
Benjamin Kramer1b582012016-02-13 18:11:49 +00001198 void writeCtorBody(raw_ostream &OS) const override {
1199 OS << " for (size_t I = 0, E = " << getArgSizeName() << "; I != E;\n"
1200 " ++I) {\n"
1201 " StringRef Ref = " << getUpperName() << "[I];\n"
1202 " if (!Ref.empty()) {\n"
1203 " char *Mem = new (Ctx, 1) char[Ref.size()];\n"
1204 " std::memcpy(Mem, Ref.data(), Ref.size());\n"
1205 " " << getArgName() << "[I] = StringRef(Mem, Ref.size());\n"
1206 " }\n"
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001207 " }\n";
Benjamin Kramer1b582012016-02-13 18:11:49 +00001208 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001209
Peter Collingbourne915df992015-05-15 18:33:32 +00001210 void writeValueImpl(raw_ostream &OS) const override {
1211 OS << " OS << \"\\\"\" << Val << \"\\\"\";\n";
1212 }
1213 };
1214
Richard Smithb87c4652013-10-31 21:23:20 +00001215 class TypeArgument : public SimpleArgument {
1216 public:
Aaron Ballman2f22b942014-05-20 19:47:14 +00001217 TypeArgument(const Record &Arg, StringRef Attr)
Richard Smithb87c4652013-10-31 21:23:20 +00001218 : SimpleArgument(Arg, Attr, "TypeSourceInfo *")
1219 {}
1220
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001221 void writeAccessors(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001222 OS << " QualType get" << getUpperName() << "() const {\n";
1223 OS << " return " << getLowerName() << "->getType();\n";
1224 OS << " }";
1225 OS << " " << getType() << " get" << getUpperName() << "Loc() const {\n";
1226 OS << " return " << getLowerName() << ";\n";
1227 OS << " }";
1228 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001229
Richard Smithf26d5512017-08-15 22:58:45 +00001230 void writeASTVisitorTraversal(raw_ostream &OS) const override {
1231 OS << " if (auto *TSI = A->get" << getUpperName() << "Loc())\n";
1232 OS << " if (!getDerived().TraverseTypeLoc(TSI->getTypeLoc()))\n";
1233 OS << " return false;\n";
1234 }
1235
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001236 void writeTemplateInstantiationArgs(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001237 OS << "A->get" << getUpperName() << "Loc()";
1238 }
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001239
Aaron Ballman8cbf6332014-03-06 15:09:50 +00001240 void writePCHWrite(raw_ostream &OS) const override {
Richard Smithb87c4652013-10-31 21:23:20 +00001241 OS << " " << WritePCHRecord(
1242 getType(), "SA->get" + std::string(getUpperName()) + "Loc()");
1243 }
1244 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001245
Hans Wennborgdcfba332015-10-06 23:40:43 +00001246} // end anonymous namespace
Peter Collingbournebee583f2011-10-06 13:03:08 +00001247
Aaron Ballman2f22b942014-05-20 19:47:14 +00001248static std::unique_ptr<Argument>
1249createArgument(const Record &Arg, StringRef Attr,
1250 const Record *Search = nullptr) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00001251 if (!Search)
1252 Search = &Arg;
1253
David Blaikie28f30ca2014-08-08 23:59:38 +00001254 std::unique_ptr<Argument> Ptr;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001255 llvm::StringRef ArgName = Search->getName();
1256
David Blaikie28f30ca2014-08-08 23:59:38 +00001257 if (ArgName == "AlignedArgument")
1258 Ptr = llvm::make_unique<AlignedArgument>(Arg, Attr);
1259 else if (ArgName == "EnumArgument")
1260 Ptr = llvm::make_unique<EnumArgument>(Arg, Attr);
1261 else if (ArgName == "ExprArgument")
1262 Ptr = llvm::make_unique<ExprArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001263 else if (ArgName == "FunctionArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001264 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "FunctionDecl *");
Argyrios Kyrtzidisa7233bd2017-05-24 00:46:27 +00001265 else if (ArgName == "NamedArgument")
1266 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "NamedDecl *");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001267 else if (ArgName == "IdentifierArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001268 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "IdentifierInfo *");
David Majnemer4bb09802014-02-10 19:50:15 +00001269 else if (ArgName == "DefaultBoolArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001270 Ptr = llvm::make_unique<DefaultSimpleArgument>(
1271 Arg, Attr, "bool", Arg.getValueAsBit("Default"));
1272 else if (ArgName == "BoolArgument")
1273 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "bool");
Aaron Ballman18a78382013-11-21 00:28:23 +00001274 else if (ArgName == "DefaultIntArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001275 Ptr = llvm::make_unique<DefaultSimpleArgument>(
1276 Arg, Attr, "int", Arg.getValueAsInt("Default"));
1277 else if (ArgName == "IntArgument")
1278 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "int");
1279 else if (ArgName == "StringArgument")
1280 Ptr = llvm::make_unique<StringArgument>(Arg, Attr);
1281 else if (ArgName == "TypeArgument")
1282 Ptr = llvm::make_unique<TypeArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001283 else if (ArgName == "UnsignedArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001284 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "unsigned");
Peter Collingbournebee583f2011-10-06 13:03:08 +00001285 else if (ArgName == "VariadicUnsignedArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001286 Ptr = llvm::make_unique<VariadicArgument>(Arg, Attr, "unsigned");
Peter Collingbourne915df992015-05-15 18:33:32 +00001287 else if (ArgName == "VariadicStringArgument")
1288 Ptr = llvm::make_unique<VariadicStringArgument>(Arg, Attr);
DeLesley Hutchins210791a2013-10-04 21:28:06 +00001289 else if (ArgName == "VariadicEnumArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001290 Ptr = llvm::make_unique<VariadicEnumArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001291 else if (ArgName == "VariadicExprArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001292 Ptr = llvm::make_unique<VariadicExprArgument>(Arg, Attr);
Joel E. Denny81508102018-03-13 14:51:22 +00001293 else if (ArgName == "VariadicParamIdxArgument")
1294 Ptr = llvm::make_unique<VariadicParamIdxArgument>(Arg, Attr);
Johannes Doerfertac991bb2019-01-19 05:36:54 +00001295 else if (ArgName == "VariadicParamOrParamIdxArgument")
1296 Ptr = llvm::make_unique<VariadicParamOrParamIdxArgument>(Arg, Attr);
Joel E. Denny81508102018-03-13 14:51:22 +00001297 else if (ArgName == "ParamIdxArgument")
1298 Ptr = llvm::make_unique<SimpleArgument>(Arg, Attr, "ParamIdx");
Erich Keane3efe0022018-07-20 14:13:28 +00001299 else if (ArgName == "VariadicIdentifierArgument")
1300 Ptr = llvm::make_unique<VariadicIdentifierArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001301 else if (ArgName == "VersionArgument")
David Blaikie28f30ca2014-08-08 23:59:38 +00001302 Ptr = llvm::make_unique<VersionArgument>(Arg, Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00001303
1304 if (!Ptr) {
Aaron Ballman18a78382013-11-21 00:28:23 +00001305 // Search in reverse order so that the most-derived type is handled first.
Craig Topper25761242016-01-18 19:52:54 +00001306 ArrayRef<std::pair<Record*, SMRange>> Bases = Search->getSuperClasses();
David Majnemerf7e36092016-06-23 00:15:04 +00001307 for (const auto &Base : llvm::reverse(Bases)) {
Craig Topper25761242016-01-18 19:52:54 +00001308 if ((Ptr = createArgument(Arg, Attr, Base.first)))
Peter Collingbournebee583f2011-10-06 13:03:08 +00001309 break;
1310 }
1311 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +00001312
1313 if (Ptr && Arg.getValueAsBit("Optional"))
1314 Ptr->setOptional(true);
1315
John McCalla62c1a92015-10-28 00:17:34 +00001316 if (Ptr && Arg.getValueAsBit("Fake"))
1317 Ptr->setFake(true);
1318
David Blaikie28f30ca2014-08-08 23:59:38 +00001319 return Ptr;
Peter Collingbournebee583f2011-10-06 13:03:08 +00001320}
1321
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001322static void writeAvailabilityValue(raw_ostream &OS) {
1323 OS << "\" << getPlatform()->getName();\n"
Manman Ren42e09eb2016-03-10 23:54:12 +00001324 << " if (getStrict()) OS << \", strict\";\n"
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00001325 << " if (!getIntroduced().empty()) OS << \", introduced=\" << getIntroduced();\n"
1326 << " if (!getDeprecated().empty()) OS << \", deprecated=\" << getDeprecated();\n"
1327 << " if (!getObsoleted().empty()) OS << \", obsoleted=\" << getObsoleted();\n"
1328 << " if (getUnavailable()) OS << \", unavailable\";\n"
1329 << " OS << \"";
1330}
1331
Manman Renc7890fe2016-03-16 18:50:49 +00001332static void writeDeprecatedAttrValue(raw_ostream &OS, std::string &Variety) {
1333 OS << "\\\"\" << getMessage() << \"\\\"\";\n";
1334 // Only GNU deprecated has an optional fixit argument at the second position.
1335 if (Variety == "GNU")
1336 OS << " if (!getReplacement().empty()) OS << \", \\\"\""
1337 " << getReplacement() << \"\\\"\";\n";
1338 OS << " OS << \"";
1339}
1340
Aaron Ballman3e424b52013-12-26 18:30:57 +00001341static void writeGetSpellingFunction(Record &R, raw_ostream &OS) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001342 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballman3e424b52013-12-26 18:30:57 +00001343
1344 OS << "const char *" << R.getName() << "Attr::getSpelling() const {\n";
1345 if (Spellings.empty()) {
1346 OS << " return \"(No spelling)\";\n}\n\n";
1347 return;
1348 }
1349
1350 OS << " switch (SpellingListIndex) {\n"
1351 " default:\n"
1352 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1353 " return \"(No spelling)\";\n";
1354
1355 for (unsigned I = 0; I < Spellings.size(); ++I)
1356 OS << " case " << I << ":\n"
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001357 " return \"" << Spellings[I].name() << "\";\n";
Aaron Ballman3e424b52013-12-26 18:30:57 +00001358 // End of the switch statement.
1359 OS << " }\n";
1360 // End of the getSpelling function.
1361 OS << "}\n\n";
1362}
1363
Aaron Ballman8f1439b2014-03-05 16:49:55 +00001364static void
1365writePrettyPrintFunction(Record &R,
1366 const std::vector<std::unique_ptr<Argument>> &Args,
1367 raw_ostream &OS) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001368 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Michael Han99315932013-01-24 16:46:58 +00001369
1370 OS << "void " << R.getName() << "Attr::printPretty("
1371 << "raw_ostream &OS, const PrintingPolicy &Policy) const {\n";
1372
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001373 if (Spellings.empty()) {
Michael Han99315932013-01-24 16:46:58 +00001374 OS << "}\n\n";
1375 return;
1376 }
1377
1378 OS <<
1379 " switch (SpellingListIndex) {\n"
1380 " default:\n"
1381 " llvm_unreachable(\"Unknown attribute spelling!\");\n"
1382 " break;\n";
1383
1384 for (unsigned I = 0; I < Spellings.size(); ++ I) {
1385 llvm::SmallString<16> Prefix;
1386 llvm::SmallString<8> Suffix;
1387 // The actual spelling of the name and namespace (if applicable)
1388 // of an attribute without considering prefix and suffix.
1389 llvm::SmallString<64> Spelling;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001390 std::string Name = Spellings[I].name();
1391 std::string Variety = Spellings[I].variety();
Michael Han99315932013-01-24 16:46:58 +00001392
1393 if (Variety == "GNU") {
1394 Prefix = " __attribute__((";
1395 Suffix = "))";
Aaron Ballman606093a2017-10-15 15:01:42 +00001396 } else if (Variety == "CXX11" || Variety == "C2x") {
Michael Han99315932013-01-24 16:46:58 +00001397 Prefix = " [[";
1398 Suffix = "]]";
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001399 std::string Namespace = Spellings[I].nameSpace();
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001400 if (!Namespace.empty()) {
Michael Han99315932013-01-24 16:46:58 +00001401 Spelling += Namespace;
1402 Spelling += "::";
1403 }
1404 } else if (Variety == "Declspec") {
1405 Prefix = " __declspec(";
1406 Suffix = ")";
Nico Weber20e08042016-09-03 02:55:10 +00001407 } else if (Variety == "Microsoft") {
1408 Prefix = "[";
1409 Suffix = "]";
Richard Smith0cdcc982013-01-29 01:24:26 +00001410 } else if (Variety == "Keyword") {
1411 Prefix = " ";
1412 Suffix = "";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001413 } else if (Variety == "Pragma") {
1414 Prefix = "#pragma ";
1415 Suffix = "\n";
1416 std::string Namespace = Spellings[I].nameSpace();
1417 if (!Namespace.empty()) {
1418 Spelling += Namespace;
1419 Spelling += " ";
1420 }
Michael Han99315932013-01-24 16:46:58 +00001421 } else {
Richard Smith0cdcc982013-01-29 01:24:26 +00001422 llvm_unreachable("Unknown attribute syntax variety!");
Michael Han99315932013-01-24 16:46:58 +00001423 }
1424
1425 Spelling += Name;
1426
1427 OS <<
1428 " case " << I << " : {\n"
Yaron Keren09fb7c62015-03-10 07:33:23 +00001429 " OS << \"" << Prefix << Spelling;
Michael Han99315932013-01-24 16:46:58 +00001430
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001431 if (Variety == "Pragma") {
Alexey Bataevcbecfdf2018-02-14 17:38:47 +00001432 OS << "\";\n";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001433 OS << " printPrettyPragma(OS, Policy);\n";
Alexey Bataev6d455322015-10-12 06:59:48 +00001434 OS << " OS << \"\\n\";";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00001435 OS << " break;\n";
1436 OS << " }\n";
1437 continue;
1438 }
1439
Michael Han99315932013-01-24 16:46:58 +00001440 if (Spelling == "availability") {
Aaron Ballman48a533d2018-02-27 23:49:28 +00001441 OS << "(";
Michael Han99315932013-01-24 16:46:58 +00001442 writeAvailabilityValue(OS);
Aaron Ballman48a533d2018-02-27 23:49:28 +00001443 OS << ")";
Manman Renc7890fe2016-03-16 18:50:49 +00001444 } else if (Spelling == "deprecated" || Spelling == "gnu::deprecated") {
Aaron Ballman48a533d2018-02-27 23:49:28 +00001445 OS << "(";
1446 writeDeprecatedAttrValue(OS, Variety);
1447 OS << ")";
Michael Han99315932013-01-24 16:46:58 +00001448 } else {
Aaron Ballman48a533d2018-02-27 23:49:28 +00001449 // To avoid printing parentheses around an empty argument list or
1450 // printing spurious commas at the end of an argument list, we need to
1451 // determine where the last provided non-fake argument is.
1452 unsigned NonFakeArgs = 0;
1453 unsigned TrailingOptArgs = 0;
1454 bool FoundNonOptArg = false;
1455 for (const auto &arg : llvm::reverse(Args)) {
1456 if (arg->isFake())
1457 continue;
1458 ++NonFakeArgs;
1459 if (FoundNonOptArg)
1460 continue;
1461 // FIXME: arg->getIsOmitted() == "false" means we haven't implemented
1462 // any way to detect whether the argument was omitted.
1463 if (!arg->isOptional() || arg->getIsOmitted() == "false") {
1464 FoundNonOptArg = true;
1465 continue;
1466 }
1467 if (!TrailingOptArgs++)
1468 OS << "\";\n"
1469 << " unsigned TrailingOmittedArgs = 0;\n";
1470 OS << " if (" << arg->getIsOmitted() << ")\n"
1471 << " ++TrailingOmittedArgs;\n";
Michael Han99315932013-01-24 16:46:58 +00001472 }
Aaron Ballman48a533d2018-02-27 23:49:28 +00001473 if (TrailingOptArgs)
1474 OS << " OS << \"";
1475 if (TrailingOptArgs < NonFakeArgs)
1476 OS << "(";
1477 else if (TrailingOptArgs)
1478 OS << "\";\n"
1479 << " if (TrailingOmittedArgs < " << NonFakeArgs << ")\n"
1480 << " OS << \"(\";\n"
1481 << " OS << \"";
1482 unsigned ArgIndex = 0;
1483 for (const auto &arg : Args) {
1484 if (arg->isFake())
1485 continue;
1486 if (ArgIndex) {
1487 if (ArgIndex >= NonFakeArgs - TrailingOptArgs)
1488 OS << "\";\n"
1489 << " if (" << ArgIndex << " < " << NonFakeArgs
1490 << " - TrailingOmittedArgs)\n"
1491 << " OS << \", \";\n"
1492 << " OS << \"";
1493 else
1494 OS << ", ";
1495 }
1496 std::string IsOmitted = arg->getIsOmitted();
1497 if (arg->isOptional() && IsOmitted != "false")
1498 OS << "\";\n"
1499 << " if (!(" << IsOmitted << ")) {\n"
1500 << " OS << \"";
1501 arg->writeValue(OS);
1502 if (arg->isOptional() && IsOmitted != "false")
1503 OS << "\";\n"
1504 << " }\n"
1505 << " OS << \"";
1506 ++ArgIndex;
1507 }
1508 if (TrailingOptArgs < NonFakeArgs)
1509 OS << ")";
1510 else if (TrailingOptArgs)
1511 OS << "\";\n"
1512 << " if (TrailingOmittedArgs < " << NonFakeArgs << ")\n"
1513 << " OS << \")\";\n"
1514 << " OS << \"";
Michael Han99315932013-01-24 16:46:58 +00001515 }
1516
Yaron Keren09fb7c62015-03-10 07:33:23 +00001517 OS << Suffix + "\";\n";
Michael Han99315932013-01-24 16:46:58 +00001518
1519 OS <<
1520 " break;\n"
1521 " }\n";
1522 }
1523
1524 // End of the switch statement.
1525 OS << "}\n";
1526 // End of the print function.
1527 OS << "}\n\n";
1528}
1529
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001530/// Return the index of a spelling in a spelling list.
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001531static unsigned
1532getSpellingListIndex(const std::vector<FlattenedSpelling> &SpellingList,
1533 const FlattenedSpelling &Spelling) {
Alexander Kornienko6ee521c2015-01-23 15:36:10 +00001534 assert(!SpellingList.empty() && "Spelling list is empty!");
Michael Hanaf02bbe2013-02-01 01:19:17 +00001535
1536 for (unsigned Index = 0; Index < SpellingList.size(); ++Index) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001537 const FlattenedSpelling &S = SpellingList[Index];
1538 if (S.variety() != Spelling.variety())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001539 continue;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001540 if (S.nameSpace() != Spelling.nameSpace())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001541 continue;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001542 if (S.name() != Spelling.name())
Michael Hanaf02bbe2013-02-01 01:19:17 +00001543 continue;
1544
1545 return Index;
1546 }
1547
1548 llvm_unreachable("Unknown spelling!");
1549}
1550
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001551static void writeAttrAccessorDefinition(const Record &R, raw_ostream &OS) {
Michael Hanaf02bbe2013-02-01 01:19:17 +00001552 std::vector<Record*> Accessors = R.getValueAsListOfDefs("Accessors");
George Burgess IV1881a572016-12-01 00:13:18 +00001553 if (Accessors.empty())
1554 return;
1555
1556 const std::vector<FlattenedSpelling> SpellingList = GetFlattenedSpellings(R);
1557 assert(!SpellingList.empty() &&
1558 "Attribute with empty spelling list can't have accessors!");
Aaron Ballman2f22b942014-05-20 19:47:14 +00001559 for (const auto *Accessor : Accessors) {
Erich Keane3bff4142017-10-16 23:25:24 +00001560 const StringRef Name = Accessor->getValueAsString("Name");
George Burgess IV1881a572016-12-01 00:13:18 +00001561 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Accessor);
Michael Hanaf02bbe2013-02-01 01:19:17 +00001562
1563 OS << " bool " << Name << "() const { return SpellingListIndex == ";
1564 for (unsigned Index = 0; Index < Spellings.size(); ++Index) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001565 OS << getSpellingListIndex(SpellingList, Spellings[Index]);
George Burgess IV1881a572016-12-01 00:13:18 +00001566 if (Index != Spellings.size() - 1)
Michael Hanaf02bbe2013-02-01 01:19:17 +00001567 OS << " ||\n SpellingListIndex == ";
1568 else
1569 OS << "; }\n";
1570 }
1571 }
1572}
1573
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001574static bool
1575SpellingNamesAreCommon(const std::vector<FlattenedSpelling>& Spellings) {
Aaron Ballman36a53502014-01-16 13:03:14 +00001576 assert(!Spellings.empty() && "An empty list of spellings was provided");
1577 std::string FirstName = NormalizeNameForSpellingComparison(
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001578 Spellings.front().name());
Aaron Ballman2f22b942014-05-20 19:47:14 +00001579 for (const auto &Spelling :
1580 llvm::make_range(std::next(Spellings.begin()), Spellings.end())) {
1581 std::string Name = NormalizeNameForSpellingComparison(Spelling.name());
Aaron Ballman36a53502014-01-16 13:03:14 +00001582 if (Name != FirstName)
1583 return false;
1584 }
1585 return true;
1586}
1587
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001588typedef std::map<unsigned, std::string> SemanticSpellingMap;
1589static std::string
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001590CreateSemanticSpellings(const std::vector<FlattenedSpelling> &Spellings,
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001591 SemanticSpellingMap &Map) {
1592 // The enumerants are automatically generated based on the variety,
1593 // namespace (if present) and name for each attribute spelling. However,
1594 // care is taken to avoid trampling on the reserved namespace due to
1595 // underscores.
1596 std::string Ret(" enum Spelling {\n");
1597 std::set<std::string> Uniques;
1598 unsigned Idx = 0;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001599 for (auto I = Spellings.begin(), E = Spellings.end(); I != E; ++I, ++Idx) {
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001600 const FlattenedSpelling &S = *I;
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00001601 const std::string &Variety = S.variety();
1602 const std::string &Spelling = S.name();
1603 const std::string &Namespace = S.nameSpace();
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00001604 std::string EnumName;
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001605
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001606 EnumName += (Variety + "_");
1607 if (!Namespace.empty())
1608 EnumName += (NormalizeNameForSpellingComparison(Namespace).str() +
1609 "_");
1610 EnumName += NormalizeNameForSpellingComparison(Spelling);
1611
1612 // Even if the name is not unique, this spelling index corresponds to a
1613 // particular enumerant name that we've calculated.
1614 Map[Idx] = EnumName;
1615
1616 // Since we have been stripping underscores to avoid trampling on the
1617 // reserved namespace, we may have inadvertently created duplicate
1618 // enumerant names. These duplicates are not considered part of the
1619 // semantic spelling, and can be elided.
1620 if (Uniques.find(EnumName) != Uniques.end())
1621 continue;
1622
1623 Uniques.insert(EnumName);
1624 if (I != Spellings.begin())
1625 Ret += ",\n";
Aaron Ballman9bf6b752015-03-10 17:19:18 +00001626 // Duplicate spellings are not considered part of the semantic spelling
1627 // enumeration, but the spelling index and semantic spelling values are
1628 // meant to be equivalent, so we must specify a concrete value for each
1629 // enumerator.
1630 Ret += " " + EnumName + " = " + llvm::utostr(Idx);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001631 }
1632 Ret += "\n };\n\n";
1633 return Ret;
1634}
1635
1636void WriteSemanticSpellingSwitch(const std::string &VarName,
1637 const SemanticSpellingMap &Map,
1638 raw_ostream &OS) {
1639 OS << " switch (" << VarName << ") {\n default: "
1640 << "llvm_unreachable(\"Unknown spelling list index\");\n";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001641 for (const auto &I : Map)
1642 OS << " case " << I.first << ": return " << I.second << ";\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00001643 OS << " }\n";
1644}
1645
Aaron Ballman35db2b32014-01-29 22:13:45 +00001646// Emits the LateParsed property for attributes.
1647static void emitClangAttrLateParsedList(RecordKeeper &Records, raw_ostream &OS) {
1648 OS << "#if defined(CLANG_ATTR_LATE_PARSED_LIST)\n";
1649 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
1650
Aaron Ballman2f22b942014-05-20 19:47:14 +00001651 for (const auto *Attr : Attrs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001652 bool LateParsed = Attr->getValueAsBit("LateParsed");
Aaron Ballman35db2b32014-01-29 22:13:45 +00001653
1654 if (LateParsed) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001655 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
Aaron Ballman35db2b32014-01-29 22:13:45 +00001656
1657 // FIXME: Handle non-GNU attributes
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001658 for (const auto &I : Spellings) {
1659 if (I.variety() != "GNU")
Aaron Ballman35db2b32014-01-29 22:13:45 +00001660 continue;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00001661 OS << ".Case(\"" << I.name() << "\", " << LateParsed << ")\n";
Aaron Ballman35db2b32014-01-29 22:13:45 +00001662 }
1663 }
1664 }
1665 OS << "#endif // CLANG_ATTR_LATE_PARSED_LIST\n\n";
1666}
1667
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001668static bool hasGNUorCXX11Spelling(const Record &Attribute) {
1669 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attribute);
1670 for (const auto &I : Spellings) {
1671 if (I.variety() == "GNU" || I.variety() == "CXX11")
1672 return true;
1673 }
1674 return false;
1675}
1676
1677namespace {
1678
1679struct AttributeSubjectMatchRule {
1680 const Record *MetaSubject;
1681 const Record *Constraint;
1682
1683 AttributeSubjectMatchRule(const Record *MetaSubject, const Record *Constraint)
1684 : MetaSubject(MetaSubject), Constraint(Constraint) {
1685 assert(MetaSubject && "Missing subject");
1686 }
1687
1688 bool isSubRule() const { return Constraint != nullptr; }
1689
1690 std::vector<Record *> getSubjects() const {
1691 return (Constraint ? Constraint : MetaSubject)
1692 ->getValueAsListOfDefs("Subjects");
1693 }
1694
1695 std::vector<Record *> getLangOpts() const {
1696 if (Constraint) {
1697 // Lookup the options in the sub-rule first, in case the sub-rule
1698 // overrides the rules options.
1699 std::vector<Record *> Opts = Constraint->getValueAsListOfDefs("LangOpts");
1700 if (!Opts.empty())
1701 return Opts;
1702 }
1703 return MetaSubject->getValueAsListOfDefs("LangOpts");
1704 }
1705
1706 // Abstract rules are used only for sub-rules
1707 bool isAbstractRule() const { return getSubjects().empty(); }
1708
Erich Keane3bff4142017-10-16 23:25:24 +00001709 StringRef getName() const {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001710 return (Constraint ? Constraint : MetaSubject)->getValueAsString("Name");
1711 }
1712
1713 bool isNegatedSubRule() const {
1714 assert(isSubRule() && "Not a sub-rule");
1715 return Constraint->getValueAsBit("Negated");
1716 }
1717
1718 std::string getSpelling() const {
1719 std::string Result = MetaSubject->getValueAsString("Name");
1720 if (isSubRule()) {
1721 Result += '(';
1722 if (isNegatedSubRule())
1723 Result += "unless(";
1724 Result += getName();
1725 if (isNegatedSubRule())
1726 Result += ')';
1727 Result += ')';
1728 }
1729 return Result;
1730 }
1731
1732 std::string getEnumValueName() const {
Craig Topper00648582017-05-31 19:01:22 +00001733 SmallString<128> Result;
1734 Result += "SubjectMatchRule_";
1735 Result += MetaSubject->getValueAsString("Name");
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001736 if (isSubRule()) {
1737 Result += "_";
1738 if (isNegatedSubRule())
1739 Result += "not_";
1740 Result += Constraint->getValueAsString("Name");
1741 }
1742 if (isAbstractRule())
1743 Result += "_abstract";
Craig Topper00648582017-05-31 19:01:22 +00001744 return Result.str();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001745 }
1746
1747 std::string getEnumValue() const { return "attr::" + getEnumValueName(); }
1748
1749 static const char *EnumName;
1750};
1751
1752const char *AttributeSubjectMatchRule::EnumName = "attr::SubjectMatchRule";
1753
1754struct PragmaClangAttributeSupport {
1755 std::vector<AttributeSubjectMatchRule> Rules;
Alex Lorenz24952fb2017-04-19 15:52:11 +00001756
1757 class RuleOrAggregateRuleSet {
1758 std::vector<AttributeSubjectMatchRule> Rules;
1759 bool IsRule;
1760 RuleOrAggregateRuleSet(ArrayRef<AttributeSubjectMatchRule> Rules,
1761 bool IsRule)
1762 : Rules(Rules), IsRule(IsRule) {}
1763
1764 public:
1765 bool isRule() const { return IsRule; }
1766
1767 const AttributeSubjectMatchRule &getRule() const {
1768 assert(IsRule && "not a rule!");
1769 return Rules[0];
1770 }
1771
1772 ArrayRef<AttributeSubjectMatchRule> getAggregateRuleSet() const {
1773 return Rules;
1774 }
1775
1776 static RuleOrAggregateRuleSet
1777 getRule(const AttributeSubjectMatchRule &Rule) {
1778 return RuleOrAggregateRuleSet(Rule, /*IsRule=*/true);
1779 }
1780 static RuleOrAggregateRuleSet
1781 getAggregateRuleSet(ArrayRef<AttributeSubjectMatchRule> Rules) {
1782 return RuleOrAggregateRuleSet(Rules, /*IsRule=*/false);
1783 }
1784 };
1785 llvm::DenseMap<const Record *, RuleOrAggregateRuleSet> SubjectsToRules;
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001786
1787 PragmaClangAttributeSupport(RecordKeeper &Records);
1788
1789 bool isAttributedSupported(const Record &Attribute);
1790
1791 void emitMatchRuleList(raw_ostream &OS);
1792
1793 std::string generateStrictConformsTo(const Record &Attr, raw_ostream &OS);
1794
1795 void generateParsingHelpers(raw_ostream &OS);
1796};
1797
1798} // end anonymous namespace
1799
Alex Lorenz24952fb2017-04-19 15:52:11 +00001800static bool doesDeclDeriveFrom(const Record *D, const Record *Base) {
1801 const Record *CurrentBase = D->getValueAsDef("Base");
1802 if (!CurrentBase)
1803 return false;
1804 if (CurrentBase == Base)
1805 return true;
1806 return doesDeclDeriveFrom(CurrentBase, Base);
1807}
1808
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001809PragmaClangAttributeSupport::PragmaClangAttributeSupport(
1810 RecordKeeper &Records) {
1811 std::vector<Record *> MetaSubjects =
1812 Records.getAllDerivedDefinitions("AttrSubjectMatcherRule");
1813 auto MapFromSubjectsToRules = [this](const Record *SubjectContainer,
1814 const Record *MetaSubject,
Saleem Abdulrasoolbe4773c2017-05-01 00:26:59 +00001815 const Record *Constraint) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001816 Rules.emplace_back(MetaSubject, Constraint);
1817 std::vector<Record *> ApplicableSubjects =
1818 SubjectContainer->getValueAsListOfDefs("Subjects");
1819 for (const auto *Subject : ApplicableSubjects) {
1820 bool Inserted =
Alex Lorenz24952fb2017-04-19 15:52:11 +00001821 SubjectsToRules
1822 .try_emplace(Subject, RuleOrAggregateRuleSet::getRule(
1823 AttributeSubjectMatchRule(MetaSubject,
1824 Constraint)))
1825 .second;
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001826 if (!Inserted) {
1827 PrintFatalError("Attribute subject match rules should not represent"
1828 "same attribute subjects.");
1829 }
1830 }
1831 };
1832 for (const auto *MetaSubject : MetaSubjects) {
Saleem Abdulrasoolbe4773c2017-05-01 00:26:59 +00001833 MapFromSubjectsToRules(MetaSubject, MetaSubject, /*Constraints=*/nullptr);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001834 std::vector<Record *> Constraints =
1835 MetaSubject->getValueAsListOfDefs("Constraints");
1836 for (const auto *Constraint : Constraints)
1837 MapFromSubjectsToRules(Constraint, MetaSubject, Constraint);
1838 }
Alex Lorenz24952fb2017-04-19 15:52:11 +00001839
1840 std::vector<Record *> Aggregates =
1841 Records.getAllDerivedDefinitions("AttrSubjectMatcherAggregateRule");
1842 std::vector<Record *> DeclNodes = Records.getAllDerivedDefinitions("DDecl");
1843 for (const auto *Aggregate : Aggregates) {
1844 Record *SubjectDecl = Aggregate->getValueAsDef("Subject");
1845
1846 // Gather sub-classes of the aggregate subject that act as attribute
1847 // subject rules.
1848 std::vector<AttributeSubjectMatchRule> Rules;
1849 for (const auto *D : DeclNodes) {
1850 if (doesDeclDeriveFrom(D, SubjectDecl)) {
1851 auto It = SubjectsToRules.find(D);
1852 if (It == SubjectsToRules.end())
1853 continue;
1854 if (!It->second.isRule() || It->second.getRule().isSubRule())
1855 continue; // Assume that the rule will be included as well.
1856 Rules.push_back(It->second.getRule());
1857 }
1858 }
1859
1860 bool Inserted =
1861 SubjectsToRules
1862 .try_emplace(SubjectDecl,
1863 RuleOrAggregateRuleSet::getAggregateRuleSet(Rules))
1864 .second;
1865 if (!Inserted) {
1866 PrintFatalError("Attribute subject match rules should not represent"
1867 "same attribute subjects.");
1868 }
1869 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001870}
1871
1872static PragmaClangAttributeSupport &
1873getPragmaAttributeSupport(RecordKeeper &Records) {
1874 static PragmaClangAttributeSupport Instance(Records);
1875 return Instance;
1876}
1877
1878void PragmaClangAttributeSupport::emitMatchRuleList(raw_ostream &OS) {
1879 OS << "#ifndef ATTR_MATCH_SUB_RULE\n";
1880 OS << "#define ATTR_MATCH_SUB_RULE(Value, Spelling, IsAbstract, Parent, "
1881 "IsNegated) "
1882 << "ATTR_MATCH_RULE(Value, Spelling, IsAbstract)\n";
1883 OS << "#endif\n";
1884 for (const auto &Rule : Rules) {
1885 OS << (Rule.isSubRule() ? "ATTR_MATCH_SUB_RULE" : "ATTR_MATCH_RULE") << '(';
1886 OS << Rule.getEnumValueName() << ", \"" << Rule.getSpelling() << "\", "
1887 << Rule.isAbstractRule();
1888 if (Rule.isSubRule())
1889 OS << ", "
1890 << AttributeSubjectMatchRule(Rule.MetaSubject, nullptr).getEnumValue()
1891 << ", " << Rule.isNegatedSubRule();
1892 OS << ")\n";
1893 }
1894 OS << "#undef ATTR_MATCH_SUB_RULE\n";
1895}
1896
1897bool PragmaClangAttributeSupport::isAttributedSupported(
1898 const Record &Attribute) {
Richard Smith1bb64532018-08-30 01:01:07 +00001899 // If the attribute explicitly specified whether to support #pragma clang
1900 // attribute, use that setting.
1901 bool Unset;
1902 bool SpecifiedResult =
1903 Attribute.getValueAsBitOrUnset("PragmaAttributeSupport", Unset);
1904 if (!Unset)
1905 return SpecifiedResult;
1906
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001907 // Opt-out rules:
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001908 // An attribute requires delayed parsing (LateParsed is on)
1909 if (Attribute.getValueAsBit("LateParsed"))
1910 return false;
1911 // An attribute has no GNU/CXX11 spelling
1912 if (!hasGNUorCXX11Spelling(Attribute))
1913 return false;
1914 // An attribute subject list has a subject that isn't covered by one of the
1915 // subject match rules or has no subjects at all.
1916 if (Attribute.isValueUnset("Subjects"))
1917 return false;
1918 const Record *SubjectObj = Attribute.getValueAsDef("Subjects");
1919 std::vector<Record *> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
1920 if (Subjects.empty())
1921 return false;
1922 for (const auto *Subject : Subjects) {
1923 if (SubjectsToRules.find(Subject) == SubjectsToRules.end())
1924 return false;
1925 }
1926 return true;
1927}
1928
John McCall2c91c3b2019-05-30 04:09:01 +00001929static std::string GenerateTestExpression(ArrayRef<Record *> LangOpts) {
1930 std::string Test;
1931
1932 for (auto *E : LangOpts) {
1933 if (!Test.empty())
1934 Test += " || ";
1935
1936 const StringRef Code = E->getValueAsString("CustomCode");
1937 if (!Code.empty()) {
1938 Test += "(";
1939 Test += Code;
1940 Test += ")";
1941 } else {
1942 Test += "LangOpts.";
1943 Test += E->getValueAsString("Name");
1944 }
1945 }
1946
1947 if (Test.empty())
1948 return "true";
1949
1950 return Test;
1951}
1952
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001953std::string
1954PragmaClangAttributeSupport::generateStrictConformsTo(const Record &Attr,
1955 raw_ostream &OS) {
1956 if (!isAttributedSupported(Attr))
1957 return "nullptr";
1958 // Generate a function that constructs a set of matching rules that describe
1959 // to which declarations the attribute should apply to.
1960 std::string FnName = "matchRulesFor" + Attr.getName().str();
Erich Keane3bff4142017-10-16 23:25:24 +00001961 OS << "static void " << FnName << "(llvm::SmallVectorImpl<std::pair<"
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001962 << AttributeSubjectMatchRule::EnumName
1963 << ", bool>> &MatchRules, const LangOptions &LangOpts) {\n";
1964 if (Attr.isValueUnset("Subjects")) {
Erich Keane3bff4142017-10-16 23:25:24 +00001965 OS << "}\n\n";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001966 return FnName;
1967 }
1968 const Record *SubjectObj = Attr.getValueAsDef("Subjects");
1969 std::vector<Record *> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
1970 for (const auto *Subject : Subjects) {
1971 auto It = SubjectsToRules.find(Subject);
1972 assert(It != SubjectsToRules.end() &&
1973 "This attribute is unsupported by #pragma clang attribute");
Alex Lorenz24952fb2017-04-19 15:52:11 +00001974 for (const auto &Rule : It->getSecond().getAggregateRuleSet()) {
1975 // The rule might be language specific, so only subtract it from the given
1976 // rules if the specific language options are specified.
1977 std::vector<Record *> LangOpts = Rule.getLangOpts();
Erich Keane3bff4142017-10-16 23:25:24 +00001978 OS << " MatchRules.push_back(std::make_pair(" << Rule.getEnumValue()
John McCall2c91c3b2019-05-30 04:09:01 +00001979 << ", /*IsSupported=*/" << GenerateTestExpression(LangOpts)
1980 << "));\n";
Alex Lorenz24952fb2017-04-19 15:52:11 +00001981 }
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001982 }
Erich Keane3bff4142017-10-16 23:25:24 +00001983 OS << "}\n\n";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00001984 return FnName;
1985}
1986
1987void PragmaClangAttributeSupport::generateParsingHelpers(raw_ostream &OS) {
1988 // Generate routines that check the names of sub-rules.
1989 OS << "Optional<attr::SubjectMatchRule> "
1990 "defaultIsAttributeSubjectMatchSubRuleFor(StringRef, bool) {\n";
1991 OS << " return None;\n";
1992 OS << "}\n\n";
1993
1994 std::map<const Record *, std::vector<AttributeSubjectMatchRule>>
1995 SubMatchRules;
1996 for (const auto &Rule : Rules) {
1997 if (!Rule.isSubRule())
1998 continue;
1999 SubMatchRules[Rule.MetaSubject].push_back(Rule);
2000 }
2001
2002 for (const auto &SubMatchRule : SubMatchRules) {
2003 OS << "Optional<attr::SubjectMatchRule> isAttributeSubjectMatchSubRuleFor_"
2004 << SubMatchRule.first->getValueAsString("Name")
2005 << "(StringRef Name, bool IsUnless) {\n";
2006 OS << " if (IsUnless)\n";
2007 OS << " return "
2008 "llvm::StringSwitch<Optional<attr::SubjectMatchRule>>(Name).\n";
2009 for (const auto &Rule : SubMatchRule.second) {
2010 if (Rule.isNegatedSubRule())
2011 OS << " Case(\"" << Rule.getName() << "\", " << Rule.getEnumValue()
2012 << ").\n";
2013 }
2014 OS << " Default(None);\n";
2015 OS << " return "
2016 "llvm::StringSwitch<Optional<attr::SubjectMatchRule>>(Name).\n";
2017 for (const auto &Rule : SubMatchRule.second) {
2018 if (!Rule.isNegatedSubRule())
2019 OS << " Case(\"" << Rule.getName() << "\", " << Rule.getEnumValue()
2020 << ").\n";
2021 }
2022 OS << " Default(None);\n";
2023 OS << "}\n\n";
2024 }
2025
2026 // Generate the function that checks for the top-level rules.
2027 OS << "std::pair<Optional<attr::SubjectMatchRule>, "
2028 "Optional<attr::SubjectMatchRule> (*)(StringRef, "
2029 "bool)> isAttributeSubjectMatchRule(StringRef Name) {\n";
2030 OS << " return "
2031 "llvm::StringSwitch<std::pair<Optional<attr::SubjectMatchRule>, "
2032 "Optional<attr::SubjectMatchRule> (*) (StringRef, "
2033 "bool)>>(Name).\n";
2034 for (const auto &Rule : Rules) {
2035 if (Rule.isSubRule())
2036 continue;
2037 std::string SubRuleFunction;
2038 if (SubMatchRules.count(Rule.MetaSubject))
Erich Keane3bff4142017-10-16 23:25:24 +00002039 SubRuleFunction =
2040 ("isAttributeSubjectMatchSubRuleFor_" + Rule.getName()).str();
Alex Lorenz9e7bf162017-04-18 14:33:39 +00002041 else
2042 SubRuleFunction = "defaultIsAttributeSubjectMatchSubRuleFor";
2043 OS << " Case(\"" << Rule.getName() << "\", std::make_pair("
2044 << Rule.getEnumValue() << ", " << SubRuleFunction << ")).\n";
2045 }
2046 OS << " Default(std::make_pair(None, "
2047 "defaultIsAttributeSubjectMatchSubRuleFor));\n";
2048 OS << "}\n\n";
2049
2050 // Generate the function that checks for the submatch rules.
2051 OS << "const char *validAttributeSubjectMatchSubRules("
2052 << AttributeSubjectMatchRule::EnumName << " Rule) {\n";
2053 OS << " switch (Rule) {\n";
2054 for (const auto &SubMatchRule : SubMatchRules) {
2055 OS << " case "
2056 << AttributeSubjectMatchRule(SubMatchRule.first, nullptr).getEnumValue()
2057 << ":\n";
2058 OS << " return \"'";
2059 bool IsFirst = true;
2060 for (const auto &Rule : SubMatchRule.second) {
2061 if (!IsFirst)
2062 OS << ", '";
2063 IsFirst = false;
2064 if (Rule.isNegatedSubRule())
2065 OS << "unless(";
2066 OS << Rule.getName();
2067 if (Rule.isNegatedSubRule())
2068 OS << ')';
2069 OS << "'";
2070 }
2071 OS << "\";\n";
2072 }
2073 OS << " default: return nullptr;\n";
2074 OS << " }\n";
2075 OS << "}\n\n";
2076}
2077
George Burgess IV1881a572016-12-01 00:13:18 +00002078template <typename Fn>
2079static void forEachUniqueSpelling(const Record &Attr, Fn &&F) {
2080 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
2081 SmallDenseSet<StringRef, 8> Seen;
2082 for (const FlattenedSpelling &S : Spellings) {
2083 if (Seen.insert(S.name()).second)
2084 F(S);
2085 }
2086}
2087
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002088/// Emits the first-argument-is-type property for attributes.
Aaron Ballman35db2b32014-01-29 22:13:45 +00002089static void emitClangAttrTypeArgList(RecordKeeper &Records, raw_ostream &OS) {
2090 OS << "#if defined(CLANG_ATTR_TYPE_ARG_LIST)\n";
2091 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
2092
Aaron Ballman2f22b942014-05-20 19:47:14 +00002093 for (const auto *Attr : Attrs) {
Aaron Ballman35db2b32014-01-29 22:13:45 +00002094 // Determine whether the first argument is a type.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002095 std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
Aaron Ballman35db2b32014-01-29 22:13:45 +00002096 if (Args.empty())
2097 continue;
2098
Craig Topper25761242016-01-18 19:52:54 +00002099 if (Args[0]->getSuperClasses().back().first->getName() != "TypeArgument")
Aaron Ballman35db2b32014-01-29 22:13:45 +00002100 continue;
2101
2102 // All these spellings take a single type argument.
George Burgess IV1881a572016-12-01 00:13:18 +00002103 forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) {
2104 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
2105 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00002106 }
2107 OS << "#endif // CLANG_ATTR_TYPE_ARG_LIST\n\n";
2108}
2109
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00002110/// Emits the parse-arguments-in-unevaluated-context property for
Aaron Ballman35db2b32014-01-29 22:13:45 +00002111/// attributes.
2112static void emitClangAttrArgContextList(RecordKeeper &Records, raw_ostream &OS) {
2113 OS << "#if defined(CLANG_ATTR_ARG_CONTEXT_LIST)\n";
2114 ParsedAttrMap Attrs = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002115 for (const auto &I : Attrs) {
2116 const Record &Attr = *I.second;
Aaron Ballman35db2b32014-01-29 22:13:45 +00002117
2118 if (!Attr.getValueAsBit("ParseArgumentsAsUnevaluated"))
2119 continue;
2120
2121 // All these spellings take are parsed unevaluated.
George Burgess IV1881a572016-12-01 00:13:18 +00002122 forEachUniqueSpelling(Attr, [&](const FlattenedSpelling &S) {
2123 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
2124 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00002125 }
2126 OS << "#endif // CLANG_ATTR_ARG_CONTEXT_LIST\n\n";
2127}
2128
2129static bool isIdentifierArgument(Record *Arg) {
2130 return !Arg->getSuperClasses().empty() &&
Craig Topper25761242016-01-18 19:52:54 +00002131 llvm::StringSwitch<bool>(Arg->getSuperClasses().back().first->getName())
Aaron Ballman35db2b32014-01-29 22:13:45 +00002132 .Case("IdentifierArgument", true)
2133 .Case("EnumArgument", true)
Aaron Ballman55ef1512014-12-19 16:42:04 +00002134 .Case("VariadicEnumArgument", true)
Aaron Ballman35db2b32014-01-29 22:13:45 +00002135 .Default(false);
2136}
2137
Erich Keane3efe0022018-07-20 14:13:28 +00002138static bool isVariadicIdentifierArgument(Record *Arg) {
2139 return !Arg->getSuperClasses().empty() &&
2140 llvm::StringSwitch<bool>(
2141 Arg->getSuperClasses().back().first->getName())
2142 .Case("VariadicIdentifierArgument", true)
Johannes Doerfertac991bb2019-01-19 05:36:54 +00002143 .Case("VariadicParamOrParamIdxArgument", true)
Erich Keane3efe0022018-07-20 14:13:28 +00002144 .Default(false);
2145}
2146
2147static void emitClangAttrVariadicIdentifierArgList(RecordKeeper &Records,
2148 raw_ostream &OS) {
2149 OS << "#if defined(CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST)\n";
2150 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
2151 for (const auto *A : Attrs) {
2152 // Determine whether the first argument is a variadic identifier.
2153 std::vector<Record *> Args = A->getValueAsListOfDefs("Args");
2154 if (Args.empty() || !isVariadicIdentifierArgument(Args[0]))
2155 continue;
2156
2157 // All these spellings take an identifier argument.
2158 forEachUniqueSpelling(*A, [&](const FlattenedSpelling &S) {
2159 OS << ".Case(\"" << S.name() << "\", "
2160 << "true"
2161 << ")\n";
2162 });
2163 }
2164 OS << "#endif // CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST\n\n";
2165}
2166
Aaron Ballman35db2b32014-01-29 22:13:45 +00002167// Emits the first-argument-is-identifier property for attributes.
2168static void emitClangAttrIdentifierArgList(RecordKeeper &Records, raw_ostream &OS) {
2169 OS << "#if defined(CLANG_ATTR_IDENTIFIER_ARG_LIST)\n";
2170 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2171
Aaron Ballman2f22b942014-05-20 19:47:14 +00002172 for (const auto *Attr : Attrs) {
Aaron Ballman35db2b32014-01-29 22:13:45 +00002173 // Determine whether the first argument is an identifier.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002174 std::vector<Record *> Args = Attr->getValueAsListOfDefs("Args");
Aaron Ballman35db2b32014-01-29 22:13:45 +00002175 if (Args.empty() || !isIdentifierArgument(Args[0]))
2176 continue;
2177
2178 // All these spellings take an identifier argument.
George Burgess IV1881a572016-12-01 00:13:18 +00002179 forEachUniqueSpelling(*Attr, [&](const FlattenedSpelling &S) {
2180 OS << ".Case(\"" << S.name() << "\", " << "true" << ")\n";
2181 });
Aaron Ballman35db2b32014-01-29 22:13:45 +00002182 }
2183 OS << "#endif // CLANG_ATTR_IDENTIFIER_ARG_LIST\n\n";
2184}
2185
Johannes Doerfertac991bb2019-01-19 05:36:54 +00002186static bool keywordThisIsaIdentifierInArgument(const Record *Arg) {
2187 return !Arg->getSuperClasses().empty() &&
2188 llvm::StringSwitch<bool>(
2189 Arg->getSuperClasses().back().first->getName())
2190 .Case("VariadicParamOrParamIdxArgument", true)
2191 .Default(false);
2192}
2193
2194static void emitClangAttrThisIsaIdentifierArgList(RecordKeeper &Records,
2195 raw_ostream &OS) {
2196 OS << "#if defined(CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST)\n";
2197 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
2198 for (const auto *A : Attrs) {
2199 // Determine whether the first argument is a variadic identifier.
2200 std::vector<Record *> Args = A->getValueAsListOfDefs("Args");
2201 if (Args.empty() || !keywordThisIsaIdentifierInArgument(Args[0]))
2202 continue;
2203
2204 // All these spellings take an identifier argument.
2205 forEachUniqueSpelling(*A, [&](const FlattenedSpelling &S) {
2206 OS << ".Case(\"" << S.name() << "\", "
2207 << "true"
2208 << ")\n";
2209 });
2210 }
2211 OS << "#endif // CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST\n\n";
2212}
2213
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002214namespace clang {
2215
2216// Emits the class definitions for attributes.
2217void EmitClangAttrClass(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002218 emitSourceFileHeader("Attribute classes' definitions", OS);
2219
Peter Collingbournebee583f2011-10-06 13:03:08 +00002220 OS << "#ifndef LLVM_CLANG_ATTR_CLASSES_INC\n";
2221 OS << "#define LLVM_CLANG_ATTR_CLASSES_INC\n\n";
2222
2223 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
2224
Aaron Ballman2f22b942014-05-20 19:47:14 +00002225 for (const auto *Attr : Attrs) {
2226 const Record &R = *Attr;
Aaron Ballman06bd44b2014-02-17 18:23:02 +00002227
2228 // FIXME: Currently, documentation is generated as-needed due to the fact
2229 // that there is no way to allow a generated project "reach into" the docs
2230 // directory (for instance, it may be an out-of-tree build). However, we want
2231 // to ensure that every attribute has a Documentation field, and produce an
2232 // error if it has been neglected. Otherwise, the on-demand generation which
2233 // happens server-side will fail. This code is ensuring that functionality,
2234 // even though this Emitter doesn't technically need the documentation.
2235 // When attribute documentation can be generated as part of the build
2236 // itself, this code can be removed.
2237 (void)R.getValueAsListOfDefs("Documentation");
Douglas Gregorb2daf842012-05-02 15:56:52 +00002238
2239 if (!R.getValueAsBit("ASTNode"))
2240 continue;
2241
Craig Topper25761242016-01-18 19:52:54 +00002242 ArrayRef<std::pair<Record *, SMRange>> Supers = R.getSuperClasses();
Aaron Ballman0979e9e2013-07-30 01:44:15 +00002243 assert(!Supers.empty() && "Forgot to specify a superclass for the attr");
Aaron Ballman0979e9e2013-07-30 01:44:15 +00002244 std::string SuperName;
Richard Smith33bddbd2018-01-04 23:42:29 +00002245 bool Inheritable = false;
David Majnemerf7e36092016-06-23 00:15:04 +00002246 for (const auto &Super : llvm::reverse(Supers)) {
Craig Topper25761242016-01-18 19:52:54 +00002247 const Record *R = Super.first;
Aaron Ballmanb9a457a2018-05-03 15:33:50 +00002248 if (R->getName() != "TargetSpecificAttr" &&
2249 R->getName() != "DeclOrTypeAttr" && SuperName.empty())
Craig Topper25761242016-01-18 19:52:54 +00002250 SuperName = R->getName();
Richard Smith33bddbd2018-01-04 23:42:29 +00002251 if (R->getName() == "InheritableAttr")
2252 Inheritable = true;
Aaron Ballman0979e9e2013-07-30 01:44:15 +00002253 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00002254
2255 OS << "class " << R.getName() << "Attr : public " << SuperName << " {\n";
2256
2257 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002258 std::vector<std::unique_ptr<Argument>> Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002259 Args.reserve(ArgRecords.size());
2260
John McCalla62c1a92015-10-28 00:17:34 +00002261 bool HasOptArg = false;
2262 bool HasFakeArg = false;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002263 for (const auto *ArgRecord : ArgRecords) {
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002264 Args.emplace_back(createArgument(*ArgRecord, R.getName()));
2265 Args.back()->writeDeclarations(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002266 OS << "\n\n";
John McCalla62c1a92015-10-28 00:17:34 +00002267
2268 // For these purposes, fake takes priority over optional.
2269 if (Args.back()->isFake()) {
2270 HasFakeArg = true;
2271 } else if (Args.back()->isOptional()) {
2272 HasOptArg = true;
2273 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00002274 }
2275
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002276 OS << "public:\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002277
Aaron Ballmanc669cc02014-01-27 22:10:04 +00002278 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballman36a53502014-01-16 13:03:14 +00002279
2280 // If there are zero or one spellings, all spelling-related functionality
2281 // can be elided. If all of the spellings share the same name, the spelling
2282 // functionality can also be elided.
2283 bool ElideSpelling = (Spellings.size() <= 1) ||
2284 SpellingNamesAreCommon(Spellings);
2285
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002286 // This maps spelling index values to semantic Spelling enumerants.
2287 SemanticSpellingMap SemanticToSyntacticMap;
Aaron Ballman36a53502014-01-16 13:03:14 +00002288
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002289 if (!ElideSpelling)
2290 OS << CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
Aaron Ballman36a53502014-01-16 13:03:14 +00002291
John McCalla62c1a92015-10-28 00:17:34 +00002292 // Emit CreateImplicit factory methods.
2293 auto emitCreateImplicit = [&](bool emitFake) {
2294 OS << " static " << R.getName() << "Attr *CreateImplicit(";
2295 OS << "ASTContext &Ctx";
2296 if (!ElideSpelling)
2297 OS << ", Spelling S";
2298 for (auto const &ai : Args) {
2299 if (ai->isFake() && !emitFake) continue;
2300 OS << ", ";
2301 ai->writeCtorParameters(OS);
2302 }
2303 OS << ", SourceRange Loc = SourceRange()";
2304 OS << ") {\n";
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002305 OS << " auto *A = new (Ctx) " << R.getName();
John McCalla62c1a92015-10-28 00:17:34 +00002306 OS << "Attr(Loc, Ctx, ";
2307 for (auto const &ai : Args) {
2308 if (ai->isFake() && !emitFake) continue;
2309 ai->writeImplicitCtorArgs(OS);
2310 OS << ", ";
2311 }
2312 OS << (ElideSpelling ? "0" : "S") << ");\n";
2313 OS << " A->setImplicit(true);\n";
2314 OS << " return A;\n }\n\n";
2315 };
Aaron Ballman36a53502014-01-16 13:03:14 +00002316
John McCalla62c1a92015-10-28 00:17:34 +00002317 // Emit a CreateImplicit that takes all the arguments.
2318 emitCreateImplicit(true);
2319
2320 // Emit a CreateImplicit that takes all the non-fake arguments.
2321 if (HasFakeArg) {
2322 emitCreateImplicit(false);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002323 }
Michael Han99315932013-01-24 16:46:58 +00002324
John McCalla62c1a92015-10-28 00:17:34 +00002325 // Emit constructors.
2326 auto emitCtor = [&](bool emitOpt, bool emitFake) {
2327 auto shouldEmitArg = [=](const std::unique_ptr<Argument> &arg) {
2328 if (arg->isFake()) return emitFake;
2329 if (arg->isOptional()) return emitOpt;
2330 return true;
2331 };
Michael Han99315932013-01-24 16:46:58 +00002332
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002333 OS << " " << R.getName() << "Attr(SourceRange R, ASTContext &Ctx\n";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002334 for (auto const &ai : Args) {
John McCalla62c1a92015-10-28 00:17:34 +00002335 if (!shouldEmitArg(ai)) continue;
2336 OS << " , ";
2337 ai->writeCtorParameters(OS);
2338 OS << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002339 }
2340
2341 OS << " , ";
Aaron Ballman36a53502014-01-16 13:03:14 +00002342 OS << "unsigned SI\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002343
2344 OS << " )\n";
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002345 OS << " : " << SuperName << "(attr::" << R.getName() << ", R, SI, "
Richard Smith33bddbd2018-01-04 23:42:29 +00002346 << ( R.getValueAsBit("LateParsed") ? "true" : "false" );
2347 if (Inheritable) {
2348 OS << ", "
2349 << (R.getValueAsBit("InheritEvenIfAlreadyPresent") ? "true"
2350 : "false");
2351 }
2352 OS << ")\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002353
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002354 for (auto const &ai : Args) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002355 OS << " , ";
John McCalla62c1a92015-10-28 00:17:34 +00002356 if (!shouldEmitArg(ai)) {
2357 ai->writeCtorDefaultInitializers(OS);
2358 } else {
2359 ai->writeCtorInitializers(OS);
2360 }
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002361 OS << "\n";
2362 }
2363
2364 OS << " {\n";
2365
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002366 for (auto const &ai : Args) {
John McCalla62c1a92015-10-28 00:17:34 +00002367 if (!shouldEmitArg(ai)) continue;
2368 ai->writeCtorBody(OS);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002369 }
2370 OS << " }\n\n";
John McCalla62c1a92015-10-28 00:17:34 +00002371 };
2372
2373 // Emit a constructor that includes all the arguments.
2374 // This is necessary for cloning.
2375 emitCtor(true, true);
2376
2377 // Emit a constructor that takes all the non-fake arguments.
2378 if (HasFakeArg) {
2379 emitCtor(true, false);
2380 }
2381
2382 // Emit a constructor that takes all the non-fake, non-optional arguments.
2383 if (HasOptArg) {
2384 emitCtor(false, false);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00002385 }
2386
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002387 OS << " " << R.getName() << "Attr *clone(ASTContext &C) const;\n";
Craig Toppercbce6e92014-03-11 06:22:39 +00002388 OS << " void printPretty(raw_ostream &OS,\n"
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002389 << " const PrintingPolicy &Policy) const;\n";
2390 OS << " const char *getSpelling() const;\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00002391
2392 if (!ElideSpelling) {
2393 assert(!SemanticToSyntacticMap.empty() && "Empty semantic mapping list");
2394 OS << " Spelling getSemanticSpelling() const {\n";
2395 WriteSemanticSpellingSwitch("SpellingListIndex", SemanticToSyntacticMap,
2396 OS);
2397 OS << " }\n";
2398 }
Peter Collingbournebee583f2011-10-06 13:03:08 +00002399
Michael Hanaf02bbe2013-02-01 01:19:17 +00002400 writeAttrAccessorDefinition(R, OS);
2401
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002402 for (auto const &ai : Args) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002403 ai->writeAccessors(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002404 OS << "\n\n";
Aaron Ballman682ee422013-09-11 19:47:58 +00002405
John McCalla62c1a92015-10-28 00:17:34 +00002406 // Don't write conversion routines for fake arguments.
2407 if (ai->isFake()) continue;
2408
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002409 if (ai->isEnumArg())
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002410 static_cast<const EnumArgument *>(ai.get())->writeConversion(OS);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002411 else if (ai->isVariadicEnumArg())
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002412 static_cast<const VariadicEnumArgument *>(ai.get())
2413 ->writeConversion(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002414 }
2415
Jakob Stoklund Olesen6f2288b62012-01-13 04:57:47 +00002416 OS << R.getValueAsString("AdditionalMembers");
Peter Collingbournebee583f2011-10-06 13:03:08 +00002417 OS << "\n\n";
2418
2419 OS << " static bool classof(const Attr *A) { return A->getKind() == "
2420 << "attr::" << R.getName() << "; }\n";
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00002421
Peter Collingbournebee583f2011-10-06 13:03:08 +00002422 OS << "};\n\n";
2423 }
2424
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002425 OS << "#endif // LLVM_CLANG_ATTR_CLASSES_INC\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002426}
2427
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002428// Emits the class method definitions for attributes.
2429void EmitClangAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002430 emitSourceFileHeader("Attribute classes' member function definitions", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002431
2432 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
Peter Collingbournebee583f2011-10-06 13:03:08 +00002433
Aaron Ballman2f22b942014-05-20 19:47:14 +00002434 for (auto *Attr : Attrs) {
2435 Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002436
2437 if (!R.getValueAsBit("ASTNode"))
2438 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002439
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002440 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
2441 std::vector<std::unique_ptr<Argument>> Args;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002442 for (const auto *Arg : ArgRecords)
2443 Args.emplace_back(createArgument(*Arg, R.getName()));
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002444
2445 for (auto const &ai : Args)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002446 ai->writeAccessorDefinitions(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002447
2448 OS << R.getName() << "Attr *" << R.getName()
2449 << "Attr::clone(ASTContext &C) const {\n";
Hans Wennborg613807b2014-05-31 01:30:30 +00002450 OS << " auto *A = new (C) " << R.getName() << "Attr(getLocation(), C";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002451 for (auto const &ai : Args) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00002452 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002453 ai->writeCloneArgs(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002454 }
Hans Wennborg613807b2014-05-31 01:30:30 +00002455 OS << ", getSpellingListIndex());\n";
2456 OS << " A->Inherited = Inherited;\n";
2457 OS << " A->IsPackExpansion = IsPackExpansion;\n";
2458 OS << " A->Implicit = Implicit;\n";
2459 OS << " return A;\n}\n\n";
Douglas Gregor49ccfaa2011-11-19 19:22:57 +00002460
Michael Han99315932013-01-24 16:46:58 +00002461 writePrettyPrintFunction(R, Args, OS);
Aaron Ballman3e424b52013-12-26 18:30:57 +00002462 writeGetSpellingFunction(R, OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002463 }
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002464
2465 // Instead of relying on virtual dispatch we just create a huge dispatch
2466 // switch. This is both smaller and faster than virtual functions.
2467 auto EmitFunc = [&](const char *Method) {
2468 OS << " switch (getKind()) {\n";
2469 for (const auto *Attr : Attrs) {
2470 const Record &R = *Attr;
2471 if (!R.getValueAsBit("ASTNode"))
2472 continue;
2473
2474 OS << " case attr::" << R.getName() << ":\n";
2475 OS << " return cast<" << R.getName() << "Attr>(this)->" << Method
2476 << ";\n";
2477 }
Benjamin Kramer845e32c2015-03-19 16:06:49 +00002478 OS << " }\n";
2479 OS << " llvm_unreachable(\"Unexpected attribute kind!\");\n";
2480 OS << "}\n\n";
2481 };
2482
2483 OS << "const char *Attr::getSpelling() const {\n";
2484 EmitFunc("getSpelling()");
2485
2486 OS << "Attr *Attr::clone(ASTContext &C) const {\n";
2487 EmitFunc("clone(C)");
2488
2489 OS << "void Attr::printPretty(raw_ostream &OS, "
2490 "const PrintingPolicy &Policy) const {\n";
2491 EmitFunc("printPretty(OS, Policy)");
Peter Collingbournebee583f2011-10-06 13:03:08 +00002492}
2493
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002494} // end namespace clang
2495
John McCall2225c8b2016-03-01 00:18:05 +00002496static void emitAttrList(raw_ostream &OS, StringRef Class,
Peter Collingbournebee583f2011-10-06 13:03:08 +00002497 const std::vector<Record*> &AttrList) {
John McCall2225c8b2016-03-01 00:18:05 +00002498 for (auto Cur : AttrList) {
2499 OS << Class << "(" << Cur->getName() << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002500 }
2501}
2502
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002503// Determines if an attribute has a Pragma spelling.
2504static bool AttrHasPragmaSpelling(const Record *R) {
2505 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
George Burgess IV1881a572016-12-01 00:13:18 +00002506 return llvm::find_if(Spellings, [](const FlattenedSpelling &S) {
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002507 return S.variety() == "Pragma";
2508 }) != Spellings.end();
2509}
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002510
John McCall2225c8b2016-03-01 00:18:05 +00002511namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002512
John McCall2225c8b2016-03-01 00:18:05 +00002513 struct AttrClassDescriptor {
2514 const char * const MacroName;
2515 const char * const TableGenName;
2516 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002517
2518} // end anonymous namespace
John McCall2225c8b2016-03-01 00:18:05 +00002519
2520static const AttrClassDescriptor AttrClassDescriptors[] = {
2521 { "ATTR", "Attr" },
Richard Smithe43e2b32018-08-20 21:47:29 +00002522 { "TYPE_ATTR", "TypeAttr" },
Richard Smith4f902c72016-03-08 00:32:55 +00002523 { "STMT_ATTR", "StmtAttr" },
John McCall2225c8b2016-03-01 00:18:05 +00002524 { "INHERITABLE_ATTR", "InheritableAttr" },
Richard Smithe43e2b32018-08-20 21:47:29 +00002525 { "DECL_OR_TYPE_ATTR", "DeclOrTypeAttr" },
John McCall477f2bb2016-03-03 06:39:32 +00002526 { "INHERITABLE_PARAM_ATTR", "InheritableParamAttr" },
2527 { "PARAMETER_ABI_ATTR", "ParameterABIAttr" }
John McCall2225c8b2016-03-01 00:18:05 +00002528};
2529
2530static void emitDefaultDefine(raw_ostream &OS, StringRef name,
2531 const char *superName) {
2532 OS << "#ifndef " << name << "\n";
2533 OS << "#define " << name << "(NAME) ";
2534 if (superName) OS << superName << "(NAME)";
2535 OS << "\n#endif\n\n";
2536}
2537
2538namespace {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002539
John McCall2225c8b2016-03-01 00:18:05 +00002540 /// A class of attributes.
2541 struct AttrClass {
2542 const AttrClassDescriptor &Descriptor;
2543 Record *TheRecord;
2544 AttrClass *SuperClass = nullptr;
2545 std::vector<AttrClass*> SubClasses;
2546 std::vector<Record*> Attrs;
2547
2548 AttrClass(const AttrClassDescriptor &Descriptor, Record *R)
2549 : Descriptor(Descriptor), TheRecord(R) {}
2550
2551 void emitDefaultDefines(raw_ostream &OS) const {
2552 // Default the macro unless this is a root class (i.e. Attr).
2553 if (SuperClass) {
2554 emitDefaultDefine(OS, Descriptor.MacroName,
2555 SuperClass->Descriptor.MacroName);
2556 }
2557 }
2558
2559 void emitUndefs(raw_ostream &OS) const {
2560 OS << "#undef " << Descriptor.MacroName << "\n";
2561 }
2562
2563 void emitAttrList(raw_ostream &OS) const {
2564 for (auto SubClass : SubClasses) {
2565 SubClass->emitAttrList(OS);
2566 }
2567
2568 ::emitAttrList(OS, Descriptor.MacroName, Attrs);
2569 }
2570
2571 void classifyAttrOnRoot(Record *Attr) {
2572 bool result = classifyAttr(Attr);
2573 assert(result && "failed to classify on root"); (void) result;
2574 }
2575
2576 void emitAttrRange(raw_ostream &OS) const {
2577 OS << "ATTR_RANGE(" << Descriptor.TableGenName
2578 << ", " << getFirstAttr()->getName()
2579 << ", " << getLastAttr()->getName() << ")\n";
2580 }
2581
2582 private:
2583 bool classifyAttr(Record *Attr) {
2584 // Check all the subclasses.
2585 for (auto SubClass : SubClasses) {
2586 if (SubClass->classifyAttr(Attr))
2587 return true;
2588 }
2589
2590 // It's not more specific than this class, but it might still belong here.
2591 if (Attr->isSubClassOf(TheRecord)) {
2592 Attrs.push_back(Attr);
2593 return true;
2594 }
2595
2596 return false;
2597 }
2598
2599 Record *getFirstAttr() const {
2600 if (!SubClasses.empty())
2601 return SubClasses.front()->getFirstAttr();
2602 return Attrs.front();
2603 }
2604
2605 Record *getLastAttr() const {
2606 if (!Attrs.empty())
2607 return Attrs.back();
2608 return SubClasses.back()->getLastAttr();
2609 }
2610 };
2611
2612 /// The entire hierarchy of attribute classes.
2613 class AttrClassHierarchy {
2614 std::vector<std::unique_ptr<AttrClass>> Classes;
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002615
John McCall2225c8b2016-03-01 00:18:05 +00002616 public:
2617 AttrClassHierarchy(RecordKeeper &Records) {
2618 // Find records for all the classes.
2619 for (auto &Descriptor : AttrClassDescriptors) {
2620 Record *ClassRecord = Records.getClass(Descriptor.TableGenName);
2621 AttrClass *Class = new AttrClass(Descriptor, ClassRecord);
2622 Classes.emplace_back(Class);
2623 }
2624
2625 // Link up the hierarchy.
2626 for (auto &Class : Classes) {
2627 if (AttrClass *SuperClass = findSuperClass(Class->TheRecord)) {
2628 Class->SuperClass = SuperClass;
2629 SuperClass->SubClasses.push_back(Class.get());
2630 }
2631 }
2632
2633#ifndef NDEBUG
2634 for (auto i = Classes.begin(), e = Classes.end(); i != e; ++i) {
2635 assert((i == Classes.begin()) == ((*i)->SuperClass == nullptr) &&
2636 "only the first class should be a root class!");
2637 }
2638#endif
2639 }
2640
2641 void emitDefaultDefines(raw_ostream &OS) const {
2642 for (auto &Class : Classes) {
2643 Class->emitDefaultDefines(OS);
2644 }
2645 }
2646
2647 void emitUndefs(raw_ostream &OS) const {
2648 for (auto &Class : Classes) {
2649 Class->emitUndefs(OS);
2650 }
2651 }
2652
2653 void emitAttrLists(raw_ostream &OS) const {
2654 // Just start from the root class.
2655 Classes[0]->emitAttrList(OS);
2656 }
2657
2658 void emitAttrRanges(raw_ostream &OS) const {
2659 for (auto &Class : Classes)
2660 Class->emitAttrRange(OS);
2661 }
2662
2663 void classifyAttr(Record *Attr) {
2664 // Add the attribute to the root class.
2665 Classes[0]->classifyAttrOnRoot(Attr);
2666 }
2667
2668 private:
2669 AttrClass *findClassByRecord(Record *R) const {
2670 for (auto &Class : Classes) {
2671 if (Class->TheRecord == R)
2672 return Class.get();
2673 }
2674 return nullptr;
2675 }
2676
2677 AttrClass *findSuperClass(Record *R) const {
2678 // TableGen flattens the superclass list, so we just need to walk it
2679 // in reverse.
2680 auto SuperClasses = R->getSuperClasses();
2681 for (signed i = 0, e = SuperClasses.size(); i != e; ++i) {
2682 auto SuperClass = findClassByRecord(SuperClasses[e - i - 1].first);
2683 if (SuperClass) return SuperClass;
2684 }
2685 return nullptr;
2686 }
2687 };
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002688
2689} // end anonymous namespace
John McCall2225c8b2016-03-01 00:18:05 +00002690
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002691namespace clang {
Eugene Zelenkoa9f3e902016-05-12 22:27:08 +00002692
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002693// Emits the enumeration list for attributes.
2694void EmitClangAttrList(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002695 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002696
John McCall2225c8b2016-03-01 00:18:05 +00002697 AttrClassHierarchy Hierarchy(Records);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002698
John McCall2225c8b2016-03-01 00:18:05 +00002699 // Add defaulting macro definitions.
2700 Hierarchy.emitDefaultDefines(OS);
2701 emitDefaultDefine(OS, "PRAGMA_SPELLING_ATTR", nullptr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002702
John McCall2225c8b2016-03-01 00:18:05 +00002703 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
2704 std::vector<Record *> PragmaAttrs;
Aaron Ballman2f22b942014-05-20 19:47:14 +00002705 for (auto *Attr : Attrs) {
2706 if (!Attr->getValueAsBit("ASTNode"))
Douglas Gregorb2daf842012-05-02 15:56:52 +00002707 continue;
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002708
John McCall2225c8b2016-03-01 00:18:05 +00002709 // Add the attribute to the ad-hoc groups.
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002710 if (AttrHasPragmaSpelling(Attr))
2711 PragmaAttrs.push_back(Attr);
2712
John McCall2225c8b2016-03-01 00:18:05 +00002713 // Place it in the hierarchy.
2714 Hierarchy.classifyAttr(Attr);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002715 }
2716
John McCall2225c8b2016-03-01 00:18:05 +00002717 // Emit the main attribute list.
2718 Hierarchy.emitAttrLists(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002719
John McCall2225c8b2016-03-01 00:18:05 +00002720 // Emit the ad hoc groups.
2721 emitAttrList(OS, "PRAGMA_SPELLING_ATTR", PragmaAttrs);
2722
2723 // Emit the attribute ranges.
2724 OS << "#ifdef ATTR_RANGE\n";
2725 Hierarchy.emitAttrRanges(OS);
2726 OS << "#undef ATTR_RANGE\n";
2727 OS << "#endif\n";
2728
2729 Hierarchy.emitUndefs(OS);
Tyler Nowickic724a83e2014-10-12 20:46:07 +00002730 OS << "#undef PRAGMA_SPELLING_ATTR\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002731}
2732
Alex Lorenz9e7bf162017-04-18 14:33:39 +00002733// Emits the enumeration list for attributes.
2734void EmitClangAttrSubjectMatchRuleList(RecordKeeper &Records, raw_ostream &OS) {
2735 emitSourceFileHeader(
2736 "List of all attribute subject matching rules that Clang recognizes", OS);
2737 PragmaClangAttributeSupport &PragmaAttributeSupport =
2738 getPragmaAttributeSupport(Records);
2739 emitDefaultDefine(OS, "ATTR_MATCH_RULE", nullptr);
2740 PragmaAttributeSupport.emitMatchRuleList(OS);
2741 OS << "#undef ATTR_MATCH_RULE\n";
2742}
2743
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002744// Emits the code to read an attribute from a precompiled header.
2745void EmitClangAttrPCHRead(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002746 emitSourceFileHeader("Attribute deserialization code", OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002747
2748 Record *InhClass = Records.getClass("InheritableAttr");
2749 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"),
2750 ArgRecords;
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002751 std::vector<std::unique_ptr<Argument>> Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002752
2753 OS << " switch (Kind) {\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002754 for (const auto *Attr : Attrs) {
2755 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002756 if (!R.getValueAsBit("ASTNode"))
2757 continue;
2758
Peter Collingbournebee583f2011-10-06 13:03:08 +00002759 OS << " case attr::" << R.getName() << ": {\n";
2760 if (R.isSubClassOf(InhClass))
David L. Jones267b8842017-01-24 01:04:30 +00002761 OS << " bool isInherited = Record.readInt();\n";
2762 OS << " bool isImplicit = Record.readInt();\n";
2763 OS << " unsigned Spelling = Record.readInt();\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002764 ArgRecords = R.getValueAsListOfDefs("Args");
2765 Args.clear();
Aaron Ballman2f22b942014-05-20 19:47:14 +00002766 for (const auto *Arg : ArgRecords) {
2767 Args.emplace_back(createArgument(*Arg, R.getName()));
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002768 Args.back()->writePCHReadDecls(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002769 }
2770 OS << " New = new (Context) " << R.getName() << "Attr(Range, Context";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00002771 for (auto const &ri : Args) {
Peter Collingbournebee583f2011-10-06 13:03:08 +00002772 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002773 ri->writePCHReadArgs(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002774 }
Aaron Ballman36a53502014-01-16 13:03:14 +00002775 OS << ", Spelling);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002776 if (R.isSubClassOf(InhClass))
2777 OS << " cast<InheritableAttr>(New)->setInherited(isInherited);\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002778 OS << " New->setImplicit(isImplicit);\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002779 OS << " break;\n";
2780 OS << " }\n";
2781 }
2782 OS << " }\n";
2783}
2784
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00002785// Emits the code to write an attribute to a precompiled header.
2786void EmitClangAttrPCHWrite(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00002787 emitSourceFileHeader("Attribute serialization code", OS);
2788
Peter Collingbournebee583f2011-10-06 13:03:08 +00002789 Record *InhClass = Records.getClass("InheritableAttr");
2790 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002791
2792 OS << " switch (A->getKind()) {\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00002793 for (const auto *Attr : Attrs) {
2794 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00002795 if (!R.getValueAsBit("ASTNode"))
2796 continue;
Peter Collingbournebee583f2011-10-06 13:03:08 +00002797 OS << " case attr::" << R.getName() << ": {\n";
2798 Args = R.getValueAsListOfDefs("Args");
2799 if (R.isSubClassOf(InhClass) || !Args.empty())
Eugene Zelenko5f02b772015-12-08 18:49:01 +00002800 OS << " const auto *SA = cast<" << R.getName()
Peter Collingbournebee583f2011-10-06 13:03:08 +00002801 << "Attr>(A);\n";
2802 if (R.isSubClassOf(InhClass))
2803 OS << " Record.push_back(SA->isInherited());\n";
Aaron Ballman36a53502014-01-16 13:03:14 +00002804 OS << " Record.push_back(A->isImplicit());\n";
2805 OS << " Record.push_back(A->getSpellingListIndex());\n";
2806
Aaron Ballman2f22b942014-05-20 19:47:14 +00002807 for (const auto *Arg : Args)
2808 createArgument(*Arg, R.getName())->writePCHWrite(OS);
Peter Collingbournebee583f2011-10-06 13:03:08 +00002809 OS << " break;\n";
2810 OS << " }\n";
2811 }
2812 OS << " }\n";
2813}
2814
Erich Keane75449672017-12-20 18:51:08 +00002815// Helper function for GenerateTargetSpecificAttrChecks that alters the 'Test'
2816// parameter with only a single check type, if applicable.
Richard Smith78b239e2019-06-20 20:44:45 +00002817static bool GenerateTargetSpecificAttrCheck(const Record *R, std::string &Test,
Erich Keane75449672017-12-20 18:51:08 +00002818 std::string *FnName,
2819 StringRef ListName,
2820 StringRef CheckAgainst,
2821 StringRef Scope) {
2822 if (!R->isValueUnset(ListName)) {
2823 Test += " && (";
2824 std::vector<StringRef> Items = R->getValueAsListOfStrings(ListName);
2825 for (auto I = Items.begin(), E = Items.end(); I != E; ++I) {
2826 StringRef Part = *I;
2827 Test += CheckAgainst;
2828 Test += " == ";
2829 Test += Scope;
2830 Test += Part;
2831 if (I + 1 != E)
2832 Test += " || ";
2833 if (FnName)
2834 *FnName += Part;
2835 }
2836 Test += ")";
Richard Smith78b239e2019-06-20 20:44:45 +00002837 return true;
Erich Keane75449672017-12-20 18:51:08 +00002838 }
Richard Smith78b239e2019-06-20 20:44:45 +00002839 return false;
Erich Keane75449672017-12-20 18:51:08 +00002840}
2841
Bob Wilson0058b822015-07-20 22:57:36 +00002842// Generate a conditional expression to check if the current target satisfies
2843// the conditions for a TargetSpecificAttr record, and append the code for
2844// those checks to the Test string. If the FnName string pointer is non-null,
2845// append a unique suffix to distinguish this set of target checks from other
2846// TargetSpecificAttr records.
Richard Smith78b239e2019-06-20 20:44:45 +00002847static bool GenerateTargetSpecificAttrChecks(const Record *R,
Craig Topper00648582017-05-31 19:01:22 +00002848 std::vector<StringRef> &Arches,
Bob Wilson0058b822015-07-20 22:57:36 +00002849 std::string &Test,
2850 std::string *FnName) {
Richard Smith78b239e2019-06-20 20:44:45 +00002851 bool AnyTargetChecks = false;
2852
Bob Wilson0058b822015-07-20 22:57:36 +00002853 // It is assumed that there will be an llvm::Triple object
2854 // named "T" and a TargetInfo object named "Target" within
2855 // scope that can be used to determine whether the attribute exists in
2856 // a given target.
Erich Keane75449672017-12-20 18:51:08 +00002857 Test += "true";
2858 // If one or more architectures is specified, check those. Arches are handled
2859 // differently because GenerateTargetRequirements needs to combine the list
2860 // with ParseKind.
2861 if (!Arches.empty()) {
Richard Smith78b239e2019-06-20 20:44:45 +00002862 AnyTargetChecks = true;
Erich Keane75449672017-12-20 18:51:08 +00002863 Test += " && (";
2864 for (auto I = Arches.begin(), E = Arches.end(); I != E; ++I) {
2865 StringRef Part = *I;
2866 Test += "T.getArch() == llvm::Triple::";
2867 Test += Part;
2868 if (I + 1 != E)
2869 Test += " || ";
2870 if (FnName)
2871 *FnName += Part;
2872 }
2873 Test += ")";
Bob Wilson0058b822015-07-20 22:57:36 +00002874 }
Bob Wilson0058b822015-07-20 22:57:36 +00002875
2876 // If the attribute is specific to particular OSes, check those.
Richard Smith78b239e2019-06-20 20:44:45 +00002877 AnyTargetChecks |= GenerateTargetSpecificAttrCheck(
2878 R, Test, FnName, "OSes", "T.getOS()", "llvm::Triple::");
Bob Wilson0058b822015-07-20 22:57:36 +00002879
Erich Keane75449672017-12-20 18:51:08 +00002880 // If one or more object formats is specified, check those.
Richard Smith78b239e2019-06-20 20:44:45 +00002881 AnyTargetChecks |=
2882 GenerateTargetSpecificAttrCheck(R, Test, FnName, "ObjectFormats",
2883 "T.getObjectFormat()", "llvm::Triple::");
2884
2885 // If custom code is specified, emit it.
2886 StringRef Code = R->getValueAsString("CustomCode");
2887 if (!Code.empty()) {
2888 AnyTargetChecks = true;
2889 Test += " && (";
2890 Test += Code;
2891 Test += ")";
2892 }
2893
2894 return AnyTargetChecks;
Bob Wilson0058b822015-07-20 22:57:36 +00002895}
2896
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002897static void GenerateHasAttrSpellingStringSwitch(
2898 const std::vector<Record *> &Attrs, raw_ostream &OS,
2899 const std::string &Variety = "", const std::string &Scope = "") {
2900 for (const auto *Attr : Attrs) {
Aaron Ballmana0344c52014-11-14 13:44:02 +00002901 // C++11-style attributes have specific version information associated with
2902 // them. If the attribute has no scope, the version information must not
2903 // have the default value (1), as that's incorrect. Instead, the unscoped
2904 // attribute version information should be taken from the SD-6 standing
2905 // document, which can be found at:
2906 // https://isocpp.org/std/standing-documents/sd-6-sg10-feature-test-recommendations
2907 int Version = 1;
2908
2909 if (Variety == "CXX11") {
2910 std::vector<Record *> Spellings = Attr->getValueAsListOfDefs("Spellings");
2911 for (const auto &Spelling : Spellings) {
2912 if (Spelling->getValueAsString("Variety") == "CXX11") {
2913 Version = static_cast<int>(Spelling->getValueAsInt("Version"));
2914 if (Scope.empty() && Version == 1)
2915 PrintError(Spelling->getLoc(), "C++ standard attributes must "
2916 "have valid version information.");
2917 break;
2918 }
2919 }
2920 }
2921
Aaron Ballman0fa06d82014-01-09 22:57:44 +00002922 std::string Test;
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002923 if (Attr->isSubClassOf("TargetSpecificAttr")) {
2924 const Record *R = Attr->getValueAsDef("Target");
Craig Topper00648582017-05-31 19:01:22 +00002925 std::vector<StringRef> Arches = R->getValueAsListOfStrings("Arches");
Hans Wennborgdcfba332015-10-06 23:40:43 +00002926 GenerateTargetSpecificAttrChecks(R, Arches, Test, nullptr);
Bob Wilson7c730832015-07-20 22:57:31 +00002927
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002928 // If this is the C++11 variety, also add in the LangOpts test.
2929 if (Variety == "CXX11")
2930 Test += " && LangOpts.CPlusPlus11";
Aaron Ballman606093a2017-10-15 15:01:42 +00002931 else if (Variety == "C2x")
2932 Test += " && LangOpts.DoubleSquareBracketAttributes";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002933 } else if (Variety == "CXX11")
2934 // C++11 mode should be checked against LangOpts, which is presumed to be
2935 // present in the caller.
2936 Test = "LangOpts.CPlusPlus11";
Aaron Ballman606093a2017-10-15 15:01:42 +00002937 else if (Variety == "C2x")
2938 Test = "LangOpts.DoubleSquareBracketAttributes";
Aaron Ballman0fa06d82014-01-09 22:57:44 +00002939
Aaron Ballmana0344c52014-11-14 13:44:02 +00002940 std::string TestStr =
Aaron Ballman28afa182014-11-17 18:17:19 +00002941 !Test.empty() ? Test + " ? " + llvm::itostr(Version) + " : 0" : "1";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002942 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Attr);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00002943 for (const auto &S : Spellings)
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002944 if (Variety.empty() || (Variety == S.variety() &&
2945 (Scope.empty() || Scope == S.nameSpace())))
Aaron Ballmana0344c52014-11-14 13:44:02 +00002946 OS << " .Case(\"" << S.name() << "\", " << TestStr << ")\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00002947 }
Aaron Ballmana0344c52014-11-14 13:44:02 +00002948 OS << " .Default(0);\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002949}
2950
2951// Emits the list of spellings for attributes.
2952void EmitClangAttrHasAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
2953 emitSourceFileHeader("Code to implement the __has_attribute logic", OS);
2954
2955 // Separate all of the attributes out into four group: generic, C++11, GNU,
2956 // and declspecs. Then generate a big switch statement for each of them.
2957 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Nico Weber20e08042016-09-03 02:55:10 +00002958 std::vector<Record *> Declspec, Microsoft, GNU, Pragma;
Aaron Ballman606093a2017-10-15 15:01:42 +00002959 std::map<std::string, std::vector<Record *>> CXX, C2x;
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002960
2961 // Walk over the list of all attributes, and split them out based on the
2962 // spelling variety.
2963 for (auto *R : Attrs) {
2964 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*R);
2965 for (const auto &SI : Spellings) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00002966 const std::string &Variety = SI.variety();
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002967 if (Variety == "GNU")
2968 GNU.push_back(R);
2969 else if (Variety == "Declspec")
2970 Declspec.push_back(R);
Nico Weber20e08042016-09-03 02:55:10 +00002971 else if (Variety == "Microsoft")
2972 Microsoft.push_back(R);
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002973 else if (Variety == "CXX11")
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002974 CXX[SI.nameSpace()].push_back(R);
Aaron Ballman606093a2017-10-15 15:01:42 +00002975 else if (Variety == "C2x")
2976 C2x[SI.nameSpace()].push_back(R);
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002977 else if (Variety == "Pragma")
2978 Pragma.push_back(R);
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002979 }
2980 }
2981
Bob Wilson7c730832015-07-20 22:57:31 +00002982 OS << "const llvm::Triple &T = Target.getTriple();\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002983 OS << "switch (Syntax) {\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002984 OS << "case AttrSyntax::GNU:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002985 OS << " return llvm::StringSwitch<int>(Name)\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002986 GenerateHasAttrSpellingStringSwitch(GNU, OS, "GNU");
2987 OS << "case AttrSyntax::Declspec:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002988 OS << " return llvm::StringSwitch<int>(Name)\n";
Aaron Ballman2fbf9942014-03-31 13:14:44 +00002989 GenerateHasAttrSpellingStringSwitch(Declspec, OS, "Declspec");
Nico Weber20e08042016-09-03 02:55:10 +00002990 OS << "case AttrSyntax::Microsoft:\n";
2991 OS << " return llvm::StringSwitch<int>(Name)\n";
2992 GenerateHasAttrSpellingStringSwitch(Microsoft, OS, "Microsoft");
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002993 OS << "case AttrSyntax::Pragma:\n";
Aaron Ballmana0344c52014-11-14 13:44:02 +00002994 OS << " return llvm::StringSwitch<int>(Name)\n";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00002995 GenerateHasAttrSpellingStringSwitch(Pragma, OS, "Pragma");
Aaron Ballman606093a2017-10-15 15:01:42 +00002996 auto fn = [&OS](const char *Spelling, const char *Variety,
2997 const std::map<std::string, std::vector<Record *>> &List) {
2998 OS << "case AttrSyntax::" << Variety << ": {\n";
2999 // C++11-style attributes are further split out based on the Scope.
3000 for (auto I = List.cbegin(), E = List.cend(); I != E; ++I) {
Stephen Kellydb8fac12019-01-11 19:16:01 +00003001 if (I != List.cbegin())
3002 OS << " else ";
3003 if (I->first.empty())
3004 OS << "if (ScopeName == \"\") {\n";
3005 else
3006 OS << "if (ScopeName == \"" << I->first << "\") {\n";
3007 OS << " return llvm::StringSwitch<int>(Name)\n";
3008 GenerateHasAttrSpellingStringSwitch(I->second, OS, Spelling, I->first);
3009 OS << "}";
Aaron Ballman606093a2017-10-15 15:01:42 +00003010 }
Aaron Ballman4ff3b5ab2017-10-18 12:11:58 +00003011 OS << "\n} break;\n";
Aaron Ballman606093a2017-10-15 15:01:42 +00003012 };
3013 fn("CXX11", "CXX", CXX);
3014 fn("C2x", "C", C2x);
Aaron Ballman2fbf9942014-03-31 13:14:44 +00003015 OS << "}\n";
Peter Collingbournebee583f2011-10-06 13:03:08 +00003016}
3017
Michael Han99315932013-01-24 16:46:58 +00003018void EmitClangAttrSpellingListIndex(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00003019 emitSourceFileHeader("Code to translate different attribute spellings "
3020 "into internal identifiers", OS);
Michael Han99315932013-01-24 16:46:58 +00003021
John McCall2225c8b2016-03-01 00:18:05 +00003022 OS << " switch (AttrKind) {\n";
Michael Han99315932013-01-24 16:46:58 +00003023
Aaron Ballman64e69862013-12-15 13:05:48 +00003024 ParsedAttrMap Attrs = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003025 for (const auto &I : Attrs) {
Aaron Ballman2f22b942014-05-20 19:47:14 +00003026 const Record &R = *I.second;
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003027 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003028 OS << " case AT_" << I.first << ": {\n";
Richard Smith852e9ce2013-11-27 01:46:48 +00003029 for (unsigned I = 0; I < Spellings.size(); ++ I) {
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003030 OS << " if (Name == \"" << Spellings[I].name() << "\" && "
3031 << "SyntaxUsed == "
3032 << StringSwitch<unsigned>(Spellings[I].variety())
3033 .Case("GNU", 0)
3034 .Case("CXX11", 1)
Aaron Ballman606093a2017-10-15 15:01:42 +00003035 .Case("C2x", 2)
3036 .Case("Declspec", 3)
3037 .Case("Microsoft", 4)
3038 .Case("Keyword", 5)
3039 .Case("Pragma", 6)
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003040 .Default(0)
3041 << " && Scope == \"" << Spellings[I].nameSpace() << "\")\n"
3042 << " return " << I << ";\n";
Michael Han99315932013-01-24 16:46:58 +00003043 }
Richard Smith852e9ce2013-11-27 01:46:48 +00003044
3045 OS << " break;\n";
3046 OS << " }\n";
Michael Han99315932013-01-24 16:46:58 +00003047 }
3048
3049 OS << " }\n";
Aaron Ballman64e69862013-12-15 13:05:48 +00003050 OS << " return 0;\n";
Michael Han99315932013-01-24 16:46:58 +00003051}
3052
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003053// Emits code used by RecursiveASTVisitor to visit attributes
3054void EmitClangAttrASTVisitor(RecordKeeper &Records, raw_ostream &OS) {
3055 emitSourceFileHeader("Used by RecursiveASTVisitor to visit attributes.", OS);
3056
3057 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
3058
3059 // Write method declarations for Traverse* methods.
3060 // We emit this here because we only generate methods for attributes that
3061 // are declared as ASTNodes.
3062 OS << "#ifdef ATTR_VISITOR_DECLS_ONLY\n\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00003063 for (const auto *Attr : Attrs) {
3064 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003065 if (!R.getValueAsBit("ASTNode"))
3066 continue;
3067 OS << " bool Traverse"
3068 << R.getName() << "Attr(" << R.getName() << "Attr *A);\n";
3069 OS << " bool Visit"
3070 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
3071 << " return true; \n"
Hans Wennborg4afe5042015-07-22 20:46:26 +00003072 << " }\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003073 }
3074 OS << "\n#else // ATTR_VISITOR_DECLS_ONLY\n\n";
3075
3076 // Write individual Traverse* methods for each attribute class.
Aaron Ballman2f22b942014-05-20 19:47:14 +00003077 for (const auto *Attr : Attrs) {
3078 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003079 if (!R.getValueAsBit("ASTNode"))
3080 continue;
3081
3082 OS << "template <typename Derived>\n"
DeLesley Hutchinsbb79c332013-12-30 21:03:02 +00003083 << "bool VISITORCLASS<Derived>::Traverse"
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003084 << R.getName() << "Attr(" << R.getName() << "Attr *A) {\n"
3085 << " if (!getDerived().VisitAttr(A))\n"
3086 << " return false;\n"
3087 << " if (!getDerived().Visit" << R.getName() << "Attr(A))\n"
3088 << " return false;\n";
3089
3090 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman2f22b942014-05-20 19:47:14 +00003091 for (const auto *Arg : ArgRecords)
3092 createArgument(*Arg, R.getName())->writeASTVisitorTraversal(OS);
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003093
3094 OS << " return true;\n";
3095 OS << "}\n\n";
3096 }
3097
3098 // Write generic Traverse routine
3099 OS << "template <typename Derived>\n"
DeLesley Hutchinsbb79c332013-12-30 21:03:02 +00003100 << "bool VISITORCLASS<Derived>::TraverseAttr(Attr *A) {\n"
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003101 << " if (!A)\n"
3102 << " return true;\n"
3103 << "\n"
John McCall2225c8b2016-03-01 00:18:05 +00003104 << " switch (A->getKind()) {\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003105
Aaron Ballman2f22b942014-05-20 19:47:14 +00003106 for (const auto *Attr : Attrs) {
3107 const Record &R = *Attr;
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003108 if (!R.getValueAsBit("ASTNode"))
3109 continue;
3110
3111 OS << " case attr::" << R.getName() << ":\n"
3112 << " return getDerived().Traverse" << R.getName() << "Attr("
3113 << "cast<" << R.getName() << "Attr>(A));\n";
3114 }
John McCall5d7cf772016-03-01 02:09:20 +00003115 OS << " }\n"; // end switch
3116 OS << " llvm_unreachable(\"bad attribute kind\");\n";
DeLesley Hutchinsc4a82432013-12-30 17:24:36 +00003117 OS << "}\n"; // end function
3118 OS << "#endif // ATTR_VISITOR_DECLS_ONLY\n";
3119}
3120
Erich Keanea32910d2017-03-23 18:51:54 +00003121void EmitClangAttrTemplateInstantiateHelper(const std::vector<Record *> &Attrs,
3122 raw_ostream &OS,
3123 bool AppliesToDecl) {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003124
Erich Keanea32910d2017-03-23 18:51:54 +00003125 OS << " switch (At->getKind()) {\n";
Aaron Ballman2f22b942014-05-20 19:47:14 +00003126 for (const auto *Attr : Attrs) {
3127 const Record &R = *Attr;
Douglas Gregorb2daf842012-05-02 15:56:52 +00003128 if (!R.getValueAsBit("ASTNode"))
3129 continue;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003130 OS << " case attr::" << R.getName() << ": {\n";
Erich Keanea32910d2017-03-23 18:51:54 +00003131 bool ShouldClone = R.getValueAsBit("Clone") &&
3132 (!AppliesToDecl ||
3133 R.getValueAsBit("MeaningfulToClassTemplateDefinition"));
Rafael Espindola7f90b7d2012-05-15 14:09:55 +00003134
3135 if (!ShouldClone) {
Hans Wennborg59dbe862015-09-29 20:56:43 +00003136 OS << " return nullptr;\n";
Rafael Espindola7f90b7d2012-05-15 14:09:55 +00003137 OS << " }\n";
3138 continue;
3139 }
3140
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003141 OS << " const auto *A = cast<"
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003142 << R.getName() << "Attr>(At);\n";
3143 bool TDependent = R.getValueAsBit("TemplateDependent");
3144
3145 if (!TDependent) {
3146 OS << " return A->clone(C);\n";
3147 OS << " }\n";
3148 continue;
3149 }
3150
3151 std::vector<Record*> ArgRecords = R.getValueAsListOfDefs("Args");
Aaron Ballman8f1439b2014-03-05 16:49:55 +00003152 std::vector<std::unique_ptr<Argument>> Args;
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003153 Args.reserve(ArgRecords.size());
3154
Aaron Ballman2f22b942014-05-20 19:47:14 +00003155 for (const auto *ArgRecord : ArgRecords)
Aaron Ballman8f1439b2014-03-05 16:49:55 +00003156 Args.emplace_back(createArgument(*ArgRecord, R.getName()));
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003157
Aaron Ballman8f1439b2014-03-05 16:49:55 +00003158 for (auto const &ai : Args)
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003159 ai->writeTemplateInstantiation(OS);
Aaron Ballman8f1439b2014-03-05 16:49:55 +00003160
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003161 OS << " return new (C) " << R.getName() << "Attr(A->getLocation(), C";
Aaron Ballman8f1439b2014-03-05 16:49:55 +00003162 for (auto const &ai : Args) {
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003163 OS << ", ";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003164 ai->writeTemplateInstantiationArgs(OS);
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003165 }
Aaron Ballman36a53502014-01-16 13:03:14 +00003166 OS << ", A->getSpellingListIndex());\n }\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003167 }
3168 OS << " } // end switch\n"
3169 << " llvm_unreachable(\"Unknown attribute!\");\n"
Erich Keanea32910d2017-03-23 18:51:54 +00003170 << " return nullptr;\n";
3171}
3172
3173// Emits code to instantiate dependent attributes on templates.
3174void EmitClangAttrTemplateInstantiate(RecordKeeper &Records, raw_ostream &OS) {
3175 emitSourceFileHeader("Template instantiation code for attributes", OS);
3176
3177 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr");
3178
3179 OS << "namespace clang {\n"
3180 << "namespace sema {\n\n"
3181 << "Attr *instantiateTemplateAttribute(const Attr *At, ASTContext &C, "
3182 << "Sema &S,\n"
3183 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n";
3184 EmitClangAttrTemplateInstantiateHelper(Attrs, OS, /*AppliesToDecl*/false);
3185 OS << "}\n\n"
3186 << "Attr *instantiateTemplateAttributeForDecl(const Attr *At,\n"
3187 << " ASTContext &C, Sema &S,\n"
3188 << " const MultiLevelTemplateArgumentList &TemplateArgs) {\n";
3189 EmitClangAttrTemplateInstantiateHelper(Attrs, OS, /*AppliesToDecl*/true);
3190 OS << "}\n\n"
Benjamin Kramerbf8da9d2012-02-06 11:13:08 +00003191 << "} // end namespace sema\n"
3192 << "} // end namespace clang\n";
DeLesley Hutchinsceec3062012-01-20 22:37:06 +00003193}
3194
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003195// Emits the list of parsed attributes.
3196void EmitClangAttrParsedAttrList(RecordKeeper &Records, raw_ostream &OS) {
3197 emitSourceFileHeader("List of all attributes that Clang recognizes", OS);
3198
3199 OS << "#ifndef PARSED_ATTR\n";
3200 OS << "#define PARSED_ATTR(NAME) NAME\n";
3201 OS << "#endif\n\n";
3202
3203 ParsedAttrMap Names = getParsedAttrList(Records);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003204 for (const auto &I : Names) {
3205 OS << "PARSED_ATTR(" << I.first << ")\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003206 }
3207}
3208
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00003209static bool isArgVariadic(const Record &R, StringRef AttrName) {
3210 return createArgument(R, AttrName)->isVariadic();
3211}
3212
Erich Keanedf9e8ae2017-10-16 22:47:26 +00003213static void emitArgInfo(const Record &R, raw_ostream &OS) {
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003214 // This function will count the number of arguments specified for the
3215 // attribute and emit the number of required arguments followed by the
3216 // number of optional arguments.
3217 std::vector<Record *> Args = R.getValueAsListOfDefs("Args");
3218 unsigned ArgCount = 0, OptCount = 0;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00003219 bool HasVariadic = false;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003220 for (const auto *Arg : Args) {
George Burgess IV8a36ace2016-12-01 17:52:39 +00003221 // If the arg is fake, it's the user's job to supply it: general parsing
3222 // logic shouldn't need to know anything about it.
3223 if (Arg->getValueAsBit("Fake"))
3224 continue;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003225 Arg->getValueAsBit("Optional") ? ++OptCount : ++ArgCount;
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00003226 if (!HasVariadic && isArgVariadic(*Arg, R.getName()))
3227 HasVariadic = true;
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003228 }
Aaron Ballman8ed8dbd2014-07-31 16:37:04 +00003229
3230 // If there is a variadic argument, we will set the optional argument count
3231 // to its largest value. Since it's currently a 4-bit number, we set it to 15.
3232 OS << ArgCount << ", " << (HasVariadic ? 15 : OptCount);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003233}
3234
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003235static void GenerateDefaultAppertainsTo(raw_ostream &OS) {
Erich Keanee891aa92018-07-13 15:07:47 +00003236 OS << "static bool defaultAppertainsTo(Sema &, const ParsedAttr &,";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003237 OS << "const Decl *) {\n";
3238 OS << " return true;\n";
3239 OS << "}\n\n";
3240}
3241
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003242static std::string GetDiagnosticSpelling(const Record &R) {
3243 std::string Ret = R.getValueAsString("DiagSpelling");
3244 if (!Ret.empty())
3245 return Ret;
3246
3247 // If we couldn't find the DiagSpelling in this object, we can check to see
3248 // if the object is one that has a base, and if it is, loop up to the Base
3249 // member recursively.
3250 std::string Super = R.getSuperClasses().back().first->getName();
3251 if (Super == "DDecl" || Super == "DStmt")
3252 return GetDiagnosticSpelling(*R.getValueAsDef("Base"));
3253
3254 return "";
3255}
3256
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003257static std::string CalculateDiagnostic(const Record &S) {
3258 // If the SubjectList object has a custom diagnostic associated with it,
3259 // return that directly.
Erich Keane3bff4142017-10-16 23:25:24 +00003260 const StringRef CustomDiag = S.getValueAsString("CustomDiag");
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003261 if (!CustomDiag.empty())
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003262 return ("\"" + Twine(CustomDiag) + "\"").str();
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003263
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003264 std::vector<std::string> DiagList;
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003265 std::vector<Record *> Subjects = S.getValueAsListOfDefs("Subjects");
Aaron Ballman2f22b942014-05-20 19:47:14 +00003266 for (const auto *Subject : Subjects) {
3267 const Record &R = *Subject;
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003268 // Get the diagnostic text from the Decl or Stmt node given.
3269 std::string V = GetDiagnosticSpelling(R);
3270 if (V.empty()) {
3271 PrintError(R.getLoc(),
3272 "Could not determine diagnostic spelling for the node: " +
3273 R.getName() + "; please add one to DeclNodes.td");
3274 } else {
3275 // The node may contain a list of elements itself, so split the elements
3276 // by a comma, and trim any whitespace.
3277 SmallVector<StringRef, 2> Frags;
3278 llvm::SplitString(V, Frags, ",");
3279 for (auto Str : Frags) {
3280 DiagList.push_back(Str.trim());
3281 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003282 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003283 }
3284
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003285 if (DiagList.empty()) {
3286 PrintFatalError(S.getLoc(),
3287 "Could not deduce diagnostic argument for Attr subjects");
3288 return "";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003289 }
3290
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003291 // FIXME: this is not particularly good for localization purposes and ideally
3292 // should be part of the diagnostics engine itself with some sort of list
3293 // specifier.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003294
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003295 // A single member of the list can be returned directly.
3296 if (DiagList.size() == 1)
3297 return '"' + DiagList.front() + '"';
3298
3299 if (DiagList.size() == 2)
3300 return '"' + DiagList[0] + " and " + DiagList[1] + '"';
3301
3302 // If there are more than two in the list, we serialize the first N - 1
3303 // elements with a comma. This leaves the string in the state: foo, bar,
3304 // baz (but misses quux). We can then add ", and " for the last element
3305 // manually.
3306 std::string Diag = llvm::join(DiagList.begin(), DiagList.end() - 1, ", ");
3307 return '"' + Diag + ", and " + *(DiagList.end() - 1) + '"';
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003308}
3309
Aaron Ballman12b9f652014-01-16 13:55:42 +00003310static std::string GetSubjectWithSuffix(const Record *R) {
George Burgess IV1881a572016-12-01 00:13:18 +00003311 const std::string &B = R->getName();
Aaron Ballman12b9f652014-01-16 13:55:42 +00003312 if (B == "DeclBase")
3313 return "Decl";
3314 return B + "Decl";
3315}
Hans Wennborgdcfba332015-10-06 23:40:43 +00003316
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003317static std::string functionNameForCustomAppertainsTo(const Record &Subject) {
3318 return "is" + Subject.getName().str();
3319}
3320
Aaron Ballman80469032013-11-29 14:57:58 +00003321static std::string GenerateCustomAppertainsTo(const Record &Subject,
3322 raw_ostream &OS) {
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003323 std::string FnName = functionNameForCustomAppertainsTo(Subject);
Aaron Ballmana358c902013-12-02 14:58:17 +00003324
Aaron Ballman80469032013-11-29 14:57:58 +00003325 // If this code has already been generated, simply return the previous
3326 // instance of it.
3327 static std::set<std::string> CustomSubjectSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003328 auto I = CustomSubjectSet.find(FnName);
Aaron Ballman80469032013-11-29 14:57:58 +00003329 if (I != CustomSubjectSet.end())
3330 return *I;
3331
3332 Record *Base = Subject.getValueAsDef("Base");
3333
3334 // Not currently support custom subjects within custom subjects.
3335 if (Base->isSubClassOf("SubsetSubject")) {
3336 PrintFatalError(Subject.getLoc(),
3337 "SubsetSubjects within SubsetSubjects is not supported");
3338 return "";
3339 }
3340
Aaron Ballman80469032013-11-29 14:57:58 +00003341 OS << "static bool " << FnName << "(const Decl *D) {\n";
Erich Keane873de982018-08-03 14:24:34 +00003342 OS << " if (const auto *S = dyn_cast<";
3343 OS << GetSubjectWithSuffix(Base);
3344 OS << ">(D))\n";
3345 OS << " return " << Subject.getValueAsString("CheckCode") << ";\n";
Aaron Ballman47553042014-01-16 14:32:03 +00003346 OS << " return false;\n";
Aaron Ballman80469032013-11-29 14:57:58 +00003347 OS << "}\n\n";
3348
3349 CustomSubjectSet.insert(FnName);
3350 return FnName;
3351}
3352
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003353static std::string GenerateAppertainsTo(const Record &Attr, raw_ostream &OS) {
3354 // If the attribute does not contain a Subjects definition, then use the
3355 // default appertainsTo logic.
3356 if (Attr.isValueUnset("Subjects"))
Aaron Ballman93b5cc62013-12-02 19:36:42 +00003357 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003358
3359 const Record *SubjectObj = Attr.getValueAsDef("Subjects");
3360 std::vector<Record*> Subjects = SubjectObj->getValueAsListOfDefs("Subjects");
3361
3362 // If the list of subjects is empty, it is assumed that the attribute
3363 // appertains to everything.
3364 if (Subjects.empty())
Aaron Ballman93b5cc62013-12-02 19:36:42 +00003365 return "defaultAppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003366
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003367 bool Warn = SubjectObj->getValueAsDef("Diag")->getValueAsBit("Warn");
3368
3369 // Otherwise, generate an appertainsTo check specific to this attribute which
3370 // checks all of the given subjects against the Decl passed in. Return the
3371 // name of that check to the caller.
Richard Smithf4e248c2018-08-01 00:33:25 +00003372 //
3373 // If D is null, that means the attribute was not applied to a declaration
3374 // at all (for instance because it was applied to a type), or that the caller
3375 // has determined that the check should fail (perhaps prior to the creation
3376 // of the declaration).
Matthias Braunbbbf5d42016-12-04 05:55:09 +00003377 std::string FnName = "check" + Attr.getName().str() + "AppertainsTo";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003378 std::stringstream SS;
Erich Keanee891aa92018-07-13 15:07:47 +00003379 SS << "static bool " << FnName << "(Sema &S, const ParsedAttr &Attr, ";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003380 SS << "const Decl *D) {\n";
Richard Smithf4e248c2018-08-01 00:33:25 +00003381 SS << " if (!D || (";
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003382 for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) {
Aaron Ballman80469032013-11-29 14:57:58 +00003383 // If the subject has custom code associated with it, generate a function
3384 // for it. The function cannot be inlined into this check (yet) because it
3385 // requires the subject to be of a specific type, and were that information
3386 // inlined here, it would not support an attribute with multiple custom
3387 // subjects.
3388 if ((*I)->isSubClassOf("SubsetSubject")) {
3389 SS << "!" << GenerateCustomAppertainsTo(**I, OS) << "(D)";
3390 } else {
Aaron Ballman12b9f652014-01-16 13:55:42 +00003391 SS << "!isa<" << GetSubjectWithSuffix(*I) << ">(D)";
Aaron Ballman80469032013-11-29 14:57:58 +00003392 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003393
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003394 if (I + 1 != E)
3395 SS << " && ";
3396 }
Richard Smithf4e248c2018-08-01 00:33:25 +00003397 SS << ")) {\n";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003398 SS << " S.Diag(Attr.getLoc(), diag::";
Aaron Ballmanadf66b62017-11-26 20:01:12 +00003399 SS << (Warn ? "warn_attribute_wrong_decl_type_str" :
3400 "err_attribute_wrong_decl_type_str");
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003401 SS << ")\n";
Erich Keane44bacdf2018-08-09 13:21:32 +00003402 SS << " << Attr << ";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003403 SS << CalculateDiagnostic(*SubjectObj) << ";\n";
3404 SS << " return false;\n";
3405 SS << " }\n";
3406 SS << " return true;\n";
3407 SS << "}\n\n";
3408
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003409 OS << SS.str();
3410 return FnName;
3411}
3412
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003413static void
3414emitAttributeMatchRules(PragmaClangAttributeSupport &PragmaAttributeSupport,
3415 raw_ostream &OS) {
3416 OS << "static bool checkAttributeMatchRuleAppliesTo(const Decl *D, "
3417 << AttributeSubjectMatchRule::EnumName << " rule) {\n";
3418 OS << " switch (rule) {\n";
3419 for (const auto &Rule : PragmaAttributeSupport.Rules) {
3420 if (Rule.isAbstractRule()) {
3421 OS << " case " << Rule.getEnumValue() << ":\n";
3422 OS << " assert(false && \"Abstract matcher rule isn't allowed\");\n";
3423 OS << " return false;\n";
3424 continue;
3425 }
3426 std::vector<Record *> Subjects = Rule.getSubjects();
3427 assert(!Subjects.empty() && "Missing subjects");
3428 OS << " case " << Rule.getEnumValue() << ":\n";
3429 OS << " return ";
3430 for (auto I = Subjects.begin(), E = Subjects.end(); I != E; ++I) {
3431 // If the subject has custom code associated with it, use the function
3432 // that was generated for GenerateAppertainsTo to check if the declaration
3433 // is valid.
3434 if ((*I)->isSubClassOf("SubsetSubject"))
3435 OS << functionNameForCustomAppertainsTo(**I) << "(D)";
3436 else
3437 OS << "isa<" << GetSubjectWithSuffix(*I) << ">(D)";
3438
3439 if (I + 1 != E)
3440 OS << " || ";
3441 }
3442 OS << ";\n";
3443 }
3444 OS << " }\n";
3445 OS << " llvm_unreachable(\"Invalid match rule\");\nreturn false;\n";
3446 OS << "}\n\n";
3447}
3448
Aaron Ballman3aff6332013-12-02 19:30:36 +00003449static void GenerateDefaultLangOptRequirements(raw_ostream &OS) {
3450 OS << "static bool defaultDiagnoseLangOpts(Sema &, ";
Erich Keanee891aa92018-07-13 15:07:47 +00003451 OS << "const ParsedAttr &) {\n";
Aaron Ballman3aff6332013-12-02 19:30:36 +00003452 OS << " return true;\n";
3453 OS << "}\n\n";
3454}
3455
3456static std::string GenerateLangOptRequirements(const Record &R,
3457 raw_ostream &OS) {
3458 // If the attribute has an empty or unset list of language requirements,
3459 // return the default handler.
3460 std::vector<Record *> LangOpts = R.getValueAsListOfDefs("LangOpts");
3461 if (LangOpts.empty())
3462 return "defaultDiagnoseLangOpts";
3463
John McCall2c91c3b2019-05-30 04:09:01 +00003464 // Generate a unique function name for the diagnostic test. The list of
3465 // options should usually be short (one or two options), and the
3466 // uniqueness isn't strictly necessary (it is just for codegen efficiency).
3467 std::string FnName = "check";
3468 for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I)
3469 FnName += (*I)->getValueAsString("Name");
Aaron Ballman3aff6332013-12-02 19:30:36 +00003470 FnName += "LangOpts";
3471
3472 // If this code has already been generated, simply return the previous
3473 // instance of it.
3474 static std::set<std::string> CustomLangOptsSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003475 auto I = CustomLangOptsSet.find(FnName);
Aaron Ballman3aff6332013-12-02 19:30:36 +00003476 if (I != CustomLangOptsSet.end())
3477 return *I;
3478
Erich Keanee891aa92018-07-13 15:07:47 +00003479 OS << "static bool " << FnName << "(Sema &S, const ParsedAttr &Attr) {\n";
John McCall2c91c3b2019-05-30 04:09:01 +00003480 OS << " auto &LangOpts = S.LangOpts;\n";
3481 OS << " if (" << GenerateTestExpression(LangOpts) << ")\n";
Aaron Ballman3aff6332013-12-02 19:30:36 +00003482 OS << " return true;\n\n";
3483 OS << " S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) ";
3484 OS << "<< Attr.getName();\n";
3485 OS << " return false;\n";
3486 OS << "}\n\n";
3487
3488 CustomLangOptsSet.insert(FnName);
3489 return FnName;
3490}
3491
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003492static void GenerateDefaultTargetRequirements(raw_ostream &OS) {
Bob Wilson7c730832015-07-20 22:57:31 +00003493 OS << "static bool defaultTargetRequirements(const TargetInfo &) {\n";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003494 OS << " return true;\n";
3495 OS << "}\n\n";
3496}
3497
3498static std::string GenerateTargetRequirements(const Record &Attr,
3499 const ParsedAttrMap &Dupes,
3500 raw_ostream &OS) {
3501 // If the attribute is not a target specific attribute, return the default
3502 // target handler.
3503 if (!Attr.isSubClassOf("TargetSpecificAttr"))
3504 return "defaultTargetRequirements";
3505
3506 // Get the list of architectures to be tested for.
3507 const Record *R = Attr.getValueAsDef("Target");
Craig Topper00648582017-05-31 19:01:22 +00003508 std::vector<StringRef> Arches = R->getValueAsListOfStrings("Arches");
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003509
3510 // If there are other attributes which share the same parsed attribute kind,
3511 // such as target-specific attributes with a shared spelling, collapse the
3512 // duplicate architectures. This is required because a shared target-specific
Erich Keanee891aa92018-07-13 15:07:47 +00003513 // attribute has only one ParsedAttr::Kind enumeration value, but it
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003514 // applies to multiple target architectures. In order for the attribute to be
3515 // considered valid, all of its architectures need to be included.
3516 if (!Attr.isValueUnset("ParseKind")) {
Erich Keane3bff4142017-10-16 23:25:24 +00003517 const StringRef APK = Attr.getValueAsString("ParseKind");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003518 for (const auto &I : Dupes) {
3519 if (I.first == APK) {
Craig Topper00648582017-05-31 19:01:22 +00003520 std::vector<StringRef> DA =
3521 I.second->getValueAsDef("Target")->getValueAsListOfStrings(
3522 "Arches");
3523 Arches.insert(Arches.end(), DA.begin(), DA.end());
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003524 }
3525 }
3526 }
3527
Bob Wilson0058b822015-07-20 22:57:36 +00003528 std::string FnName = "isTarget";
3529 std::string Test;
Richard Smith78b239e2019-06-20 20:44:45 +00003530 bool UsesT = GenerateTargetSpecificAttrChecks(R, Arches, Test, &FnName);
Bob Wilson7c730832015-07-20 22:57:31 +00003531
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003532 // If this code has already been generated, simply return the previous
3533 // instance of it.
3534 static std::set<std::string> CustomTargetSet;
Eugene Zelenko5f02b772015-12-08 18:49:01 +00003535 auto I = CustomTargetSet.find(FnName);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003536 if (I != CustomTargetSet.end())
3537 return *I;
3538
Bob Wilson7c730832015-07-20 22:57:31 +00003539 OS << "static bool " << FnName << "(const TargetInfo &Target) {\n";
Richard Smith78b239e2019-06-20 20:44:45 +00003540 if (UsesT)
3541 OS << " const llvm::Triple &T = Target.getTriple(); (void)T;\n";
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003542 OS << " return " << Test << ";\n";
3543 OS << "}\n\n";
3544
3545 CustomTargetSet.insert(FnName);
3546 return FnName;
3547}
3548
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003549static void GenerateDefaultSpellingIndexToSemanticSpelling(raw_ostream &OS) {
3550 OS << "static unsigned defaultSpellingIndexToSemanticSpelling("
Erich Keanee891aa92018-07-13 15:07:47 +00003551 << "const ParsedAttr &Attr) {\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003552 OS << " return UINT_MAX;\n";
3553 OS << "}\n\n";
3554}
3555
3556static std::string GenerateSpellingIndexToSemanticSpelling(const Record &Attr,
3557 raw_ostream &OS) {
3558 // If the attribute does not have a semantic form, we can bail out early.
3559 if (!Attr.getValueAsBit("ASTNode"))
3560 return "defaultSpellingIndexToSemanticSpelling";
3561
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003562 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003563
3564 // If there are zero or one spellings, or all of the spellings share the same
3565 // name, we can also bail out early.
3566 if (Spellings.size() <= 1 || SpellingNamesAreCommon(Spellings))
3567 return "defaultSpellingIndexToSemanticSpelling";
3568
3569 // Generate the enumeration we will use for the mapping.
3570 SemanticSpellingMap SemanticToSyntacticMap;
3571 std::string Enum = CreateSemanticSpellings(Spellings, SemanticToSyntacticMap);
Matthias Braunbbbf5d42016-12-04 05:55:09 +00003572 std::string Name = Attr.getName().str() + "AttrSpellingMap";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003573
Erich Keanee891aa92018-07-13 15:07:47 +00003574 OS << "static unsigned " << Name << "(const ParsedAttr &Attr) {\n";
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003575 OS << Enum;
3576 OS << " unsigned Idx = Attr.getAttributeSpellingListIndex();\n";
3577 WriteSemanticSpellingSwitch("Idx", SemanticToSyntacticMap, OS);
3578 OS << "}\n\n";
3579
3580 return Name;
3581}
3582
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003583static bool IsKnownToGCC(const Record &Attr) {
3584 // Look at the spellings for this subject; if there are any spellings which
3585 // claim to be known to GCC, the attribute is known to GCC.
George Burgess IV1881a572016-12-01 00:13:18 +00003586 return llvm::any_of(
3587 GetFlattenedSpellings(Attr),
3588 [](const FlattenedSpelling &S) { return S.knownToGCC(); });
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00003589}
3590
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003591/// Emits the parsed attribute helpers
3592void EmitClangAttrParsedAttrImpl(RecordKeeper &Records, raw_ostream &OS) {
3593 emitSourceFileHeader("Parsed attribute helpers", OS);
3594
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003595 PragmaClangAttributeSupport &PragmaAttributeSupport =
3596 getPragmaAttributeSupport(Records);
3597
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003598 // Get the list of parsed attributes, and accept the optional list of
3599 // duplicates due to the ParseKind.
3600 ParsedAttrMap Dupes;
3601 ParsedAttrMap Attrs = getParsedAttrList(Records, &Dupes);
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003602
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003603 // Generate the default appertainsTo, target and language option diagnostic,
3604 // and spelling list index mapping methods.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003605 GenerateDefaultAppertainsTo(OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00003606 GenerateDefaultLangOptRequirements(OS);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003607 GenerateDefaultTargetRequirements(OS);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003608 GenerateDefaultSpellingIndexToSemanticSpelling(OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003609
3610 // Generate the appertainsTo diagnostic methods and write their names into
3611 // another mapping. At the same time, generate the AttrInfoMap object
3612 // contents. Due to the reliance on generated code, use separate streams so
3613 // that code will not be interleaved.
Erich Keanedf9e8ae2017-10-16 22:47:26 +00003614 std::string Buffer;
3615 raw_string_ostream SS {Buffer};
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003616 for (auto I = Attrs.begin(), E = Attrs.end(); I != E; ++I) {
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003617 // TODO: If the attribute's kind appears in the list of duplicates, that is
3618 // because it is a target-specific attribute that appears multiple times.
3619 // It would be beneficial to test whether the duplicates are "similar
3620 // enough" to each other to not cause problems. For instance, check that
Alp Toker96cf7582014-01-18 21:49:37 +00003621 // the spellings are identical, and custom parsing rules match, etc.
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003622
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003623 // We need to generate struct instances based off ParsedAttrInfo from
Erich Keanee891aa92018-07-13 15:07:47 +00003624 // ParsedAttr.cpp.
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003625 SS << " { ";
3626 emitArgInfo(*I->second, SS);
3627 SS << ", " << I->second->getValueAsBit("HasCustomParsing");
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003628 SS << ", " << I->second->isSubClassOf("TargetSpecificAttr");
Aaron Ballmanb9a457a2018-05-03 15:33:50 +00003629 SS << ", "
3630 << (I->second->isSubClassOf("TypeAttr") ||
3631 I->second->isSubClassOf("DeclOrTypeAttr"));
Richard Smith4f902c72016-03-08 00:32:55 +00003632 SS << ", " << I->second->isSubClassOf("StmtAttr");
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003633 SS << ", " << IsKnownToGCC(*I->second);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003634 SS << ", " << PragmaAttributeSupport.isAttributedSupported(*I->second);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003635 SS << ", " << GenerateAppertainsTo(*I->second, OS);
Aaron Ballman3aff6332013-12-02 19:30:36 +00003636 SS << ", " << GenerateLangOptRequirements(*I->second, OS);
Aaron Ballmanab7691c2014-01-09 22:48:32 +00003637 SS << ", " << GenerateTargetRequirements(*I->second, Dupes, OS);
Aaron Ballman81cb8cb2014-01-24 21:32:49 +00003638 SS << ", " << GenerateSpellingIndexToSemanticSpelling(*I->second, OS);
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003639 SS << ", "
3640 << PragmaAttributeSupport.generateStrictConformsTo(*I->second, OS);
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003641 SS << " }";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003642
3643 if (I + 1 != E)
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003644 SS << ",";
3645
3646 SS << " // AT_" << I->first << "\n";
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003647 }
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003648
Erich Keanee891aa92018-07-13 15:07:47 +00003649 OS << "static const ParsedAttrInfo AttrInfoMap[ParsedAttr::UnknownAttribute "
3650 "+ 1] = {\n";
Aaron Ballman74eeeae2013-11-27 13:27:02 +00003651 OS << SS.str();
Aaron Ballman8ee40b72013-09-09 23:33:17 +00003652 OS << "};\n\n";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003653
3654 // Generate the attribute match rules.
3655 emitAttributeMatchRules(PragmaAttributeSupport, OS);
Michael Han4a045172012-03-07 00:12:16 +00003656}
3657
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00003658// Emits the kind list of parsed attributes
3659void EmitClangAttrParsedAttrKinds(RecordKeeper &Records, raw_ostream &OS) {
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00003660 emitSourceFileHeader("Attribute name matcher", OS);
3661
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003662 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Nico Weber20e08042016-09-03 02:55:10 +00003663 std::vector<StringMatcher::StringPair> GNU, Declspec, Microsoft, CXX11,
Aaron Ballman606093a2017-10-15 15:01:42 +00003664 Keywords, Pragma, C2x;
Aaron Ballman64e69862013-12-15 13:05:48 +00003665 std::set<std::string> Seen;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003666 for (const auto *A : Attrs) {
3667 const Record &Attr = *A;
Richard Smith852e9ce2013-11-27 01:46:48 +00003668
Michael Han4a045172012-03-07 00:12:16 +00003669 bool SemaHandler = Attr.getValueAsBit("SemaHandler");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003670 bool Ignored = Attr.getValueAsBit("Ignored");
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003671 if (SemaHandler || Ignored) {
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003672 // Attribute spellings can be shared between target-specific attributes,
3673 // and can be shared between syntaxes for the same attribute. For
3674 // instance, an attribute can be spelled GNU<"interrupt"> for an ARM-
3675 // specific attribute, or MSP430-specific attribute. Additionally, an
3676 // attribute can be spelled GNU<"dllexport"> and Declspec<"dllexport">
3677 // for the same semantic attribute. Ultimately, we need to map each of
Erich Keanee891aa92018-07-13 15:07:47 +00003678 // these to a single ParsedAttr::Kind value, but the StringMatcher
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003679 // class cannot handle duplicate match strings. So we generate a list of
3680 // string to match based on the syntax, and emit multiple string matchers
3681 // depending on the syntax used.
Aaron Ballman64e69862013-12-15 13:05:48 +00003682 std::string AttrName;
3683 if (Attr.isSubClassOf("TargetSpecificAttr") &&
3684 !Attr.isValueUnset("ParseKind")) {
3685 AttrName = Attr.getValueAsString("ParseKind");
3686 if (Seen.find(AttrName) != Seen.end())
3687 continue;
3688 Seen.insert(AttrName);
3689 } else
3690 AttrName = NormalizeAttrName(StringRef(Attr.getName())).str();
3691
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003692 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attr);
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003693 for (const auto &S : Spellings) {
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00003694 const std::string &RawSpelling = S.name();
Craig Topper8ae12032014-05-07 06:21:57 +00003695 std::vector<StringMatcher::StringPair> *Matches = nullptr;
Benjamin Kramer2e018ef2016-05-27 13:36:58 +00003696 std::string Spelling;
3697 const std::string &Variety = S.variety();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003698 if (Variety == "CXX11") {
3699 Matches = &CXX11;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003700 Spelling += S.nameSpace();
Alexis Hunt3bc72c12012-06-19 23:57:03 +00003701 Spelling += "::";
Aaron Ballman606093a2017-10-15 15:01:42 +00003702 } else if (Variety == "C2x") {
3703 Matches = &C2x;
3704 Spelling += S.nameSpace();
3705 Spelling += "::";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003706 } else if (Variety == "GNU")
3707 Matches = &GNU;
3708 else if (Variety == "Declspec")
3709 Matches = &Declspec;
Nico Weber20e08042016-09-03 02:55:10 +00003710 else if (Variety == "Microsoft")
3711 Matches = &Microsoft;
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003712 else if (Variety == "Keyword")
3713 Matches = &Keywords;
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003714 else if (Variety == "Pragma")
3715 Matches = &Pragma;
Alexis Hunta0e54d42012-06-18 16:13:52 +00003716
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003717 assert(Matches && "Unsupported spelling variety found");
3718
Justin Lebar4086fe52017-01-05 16:51:54 +00003719 if (Variety == "GNU")
3720 Spelling += NormalizeGNUAttrSpelling(RawSpelling);
3721 else
3722 Spelling += RawSpelling;
3723
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003724 if (SemaHandler)
Erich Keanee891aa92018-07-13 15:07:47 +00003725 Matches->push_back(StringMatcher::StringPair(
3726 Spelling, "return ParsedAttr::AT_" + AttrName + ";"));
Douglas Gregor19fbb8f2012-05-02 16:18:45 +00003727 else
Erich Keanee891aa92018-07-13 15:07:47 +00003728 Matches->push_back(StringMatcher::StringPair(
3729 Spelling, "return ParsedAttr::IgnoredAttribute;"));
Michael Han4a045172012-03-07 00:12:16 +00003730 }
3731 }
3732 }
Erich Keanee891aa92018-07-13 15:07:47 +00003733
3734 OS << "static ParsedAttr::Kind getAttrKind(StringRef Name, ";
3735 OS << "ParsedAttr::Syntax Syntax) {\n";
3736 OS << " if (ParsedAttr::AS_GNU == Syntax) {\n";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003737 StringMatcher("Name", GNU, OS).Emit();
Erich Keanee891aa92018-07-13 15:07:47 +00003738 OS << " } else if (ParsedAttr::AS_Declspec == Syntax) {\n";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003739 StringMatcher("Name", Declspec, OS).Emit();
Erich Keanee891aa92018-07-13 15:07:47 +00003740 OS << " } else if (ParsedAttr::AS_Microsoft == Syntax) {\n";
Nico Weber20e08042016-09-03 02:55:10 +00003741 StringMatcher("Name", Microsoft, OS).Emit();
Erich Keanee891aa92018-07-13 15:07:47 +00003742 OS << " } else if (ParsedAttr::AS_CXX11 == Syntax) {\n";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003743 StringMatcher("Name", CXX11, OS).Emit();
Erich Keanee891aa92018-07-13 15:07:47 +00003744 OS << " } else if (ParsedAttr::AS_C2x == Syntax) {\n";
Aaron Ballman606093a2017-10-15 15:01:42 +00003745 StringMatcher("Name", C2x, OS).Emit();
Erich Keanee891aa92018-07-13 15:07:47 +00003746 OS << " } else if (ParsedAttr::AS_Keyword == Syntax || ";
3747 OS << "ParsedAttr::AS_ContextSensitiveKeyword == Syntax) {\n";
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003748 StringMatcher("Name", Keywords, OS).Emit();
Erich Keanee891aa92018-07-13 15:07:47 +00003749 OS << " } else if (ParsedAttr::AS_Pragma == Syntax) {\n";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003750 StringMatcher("Name", Pragma, OS).Emit();
Aaron Ballman09e98ff2014-01-13 21:42:39 +00003751 OS << " }\n";
Erich Keanee891aa92018-07-13 15:07:47 +00003752 OS << " return ParsedAttr::UnknownAttribute;\n"
Douglas Gregor377f99b2012-05-02 17:33:51 +00003753 << "}\n";
Michael Han4a045172012-03-07 00:12:16 +00003754}
3755
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003756// Emits the code to dump an attribute.
Stephen Kellydb8fac12019-01-11 19:16:01 +00003757void EmitClangAttrTextNodeDump(RecordKeeper &Records, raw_ostream &OS) {
3758 emitSourceFileHeader("Attribute text node dumper", OS);
Dmitri Gribenko6b11fca2013-01-30 21:54:20 +00003759
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003760 std::vector<Record*> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
Aaron Ballman2f22b942014-05-20 19:47:14 +00003761 for (const auto *Attr : Attrs) {
3762 const Record &R = *Attr;
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003763 if (!R.getValueAsBit("ASTNode"))
3764 continue;
Aaron Ballmanbc909612014-01-22 21:51:20 +00003765
3766 // If the attribute has a semantically-meaningful name (which is determined
3767 // by whether there is a Spelling enumeration for it), then write out the
3768 // spelling used for the attribute.
Stephen Kellydb8fac12019-01-11 19:16:01 +00003769
3770 std::string FunctionContent;
3771 llvm::raw_string_ostream SS(FunctionContent);
3772
Aaron Ballmanc669cc02014-01-27 22:10:04 +00003773 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(R);
Aaron Ballmanbc909612014-01-22 21:51:20 +00003774 if (Spellings.size() > 1 && !SpellingNamesAreCommon(Spellings))
Stephen Kellydb8fac12019-01-11 19:16:01 +00003775 SS << " OS << \" \" << A->getSpelling();\n";
Aaron Ballmanbc909612014-01-22 21:51:20 +00003776
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003777 Args = R.getValueAsListOfDefs("Args");
Stephen Kellydb8fac12019-01-11 19:16:01 +00003778 for (const auto *Arg : Args)
3779 createArgument(*Arg, R.getName())->writeDump(SS);
Richard Trieude5cc7d2013-01-31 01:44:26 +00003780
Stephen Kellydb8fac12019-01-11 19:16:01 +00003781 if (SS.tell()) {
3782 OS << " void Visit" << R.getName() << "Attr(const " << R.getName()
3783 << "Attr *A) {\n";
3784 if (!Args.empty())
3785 OS << " const auto *SA = cast<" << R.getName()
3786 << "Attr>(A); (void)SA;\n";
3787 OS << SS.str();
3788 OS << " }\n";
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003789 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003790 }
Stephen Kellydb8fac12019-01-11 19:16:01 +00003791}
3792
3793void EmitClangAttrNodeTraverse(RecordKeeper &Records, raw_ostream &OS) {
3794 emitSourceFileHeader("Attribute text node traverser", OS);
3795
3796 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr"), Args;
3797 for (const auto *Attr : Attrs) {
3798 const Record &R = *Attr;
3799 if (!R.getValueAsBit("ASTNode"))
3800 continue;
3801
3802 std::string FunctionContent;
3803 llvm::raw_string_ostream SS(FunctionContent);
3804
3805 Args = R.getValueAsListOfDefs("Args");
3806 for (const auto *Arg : Args)
3807 createArgument(*Arg, R.getName())->writeDumpChildren(SS);
3808 if (SS.tell()) {
3809 OS << " void Visit" << R.getName() << "Attr(const " << R.getName()
3810 << "Attr *A) {\n";
3811 if (!Args.empty())
3812 OS << " const auto *SA = cast<" << R.getName()
3813 << "Attr>(A); (void)SA;\n";
3814 OS << SS.str();
3815 OS << " }\n";
3816 }
3817 }
Alexander Kornienko5bc364e2013-01-07 17:53:08 +00003818}
3819
Aaron Ballman35db2b32014-01-29 22:13:45 +00003820void EmitClangAttrParserStringSwitches(RecordKeeper &Records,
3821 raw_ostream &OS) {
3822 emitSourceFileHeader("Parser-related llvm::StringSwitch cases", OS);
3823 emitClangAttrArgContextList(Records, OS);
3824 emitClangAttrIdentifierArgList(Records, OS);
Erich Keane3efe0022018-07-20 14:13:28 +00003825 emitClangAttrVariadicIdentifierArgList(Records, OS);
Johannes Doerfertac991bb2019-01-19 05:36:54 +00003826 emitClangAttrThisIsaIdentifierArgList(Records, OS);
Aaron Ballman35db2b32014-01-29 22:13:45 +00003827 emitClangAttrTypeArgList(Records, OS);
3828 emitClangAttrLateParsedList(Records, OS);
3829}
3830
Alex Lorenz9e7bf162017-04-18 14:33:39 +00003831void EmitClangAttrSubjectMatchRulesParserStringSwitches(RecordKeeper &Records,
3832 raw_ostream &OS) {
3833 getPragmaAttributeSupport(Records).generateParsingHelpers(OS);
3834}
3835
Richard Smith09ac4a12018-08-30 19:16:33 +00003836enum class SpellingKind {
3837 GNU,
3838 CXX11,
3839 C2x,
3840 Declspec,
3841 Microsoft,
3842 Keyword,
3843 Pragma,
3844};
3845static const size_t NumSpellingKinds = (size_t)SpellingKind::Pragma + 1;
3846
3847class SpellingList {
3848 std::vector<std::string> Spellings[NumSpellingKinds];
3849
3850public:
3851 ArrayRef<std::string> operator[](SpellingKind K) const {
3852 return Spellings[(size_t)K];
3853 }
3854
3855 void add(const Record &Attr, FlattenedSpelling Spelling) {
3856 SpellingKind Kind = StringSwitch<SpellingKind>(Spelling.variety())
3857 .Case("GNU", SpellingKind::GNU)
3858 .Case("CXX11", SpellingKind::CXX11)
3859 .Case("C2x", SpellingKind::C2x)
3860 .Case("Declspec", SpellingKind::Declspec)
3861 .Case("Microsoft", SpellingKind::Microsoft)
3862 .Case("Keyword", SpellingKind::Keyword)
3863 .Case("Pragma", SpellingKind::Pragma);
3864 std::string Name;
3865 if (!Spelling.nameSpace().empty()) {
3866 switch (Kind) {
3867 case SpellingKind::CXX11:
3868 case SpellingKind::C2x:
3869 Name = Spelling.nameSpace() + "::";
3870 break;
3871 case SpellingKind::Pragma:
3872 Name = Spelling.nameSpace() + " ";
3873 break;
3874 default:
3875 PrintFatalError(Attr.getLoc(), "Unexpected namespace in spelling");
3876 }
3877 }
3878 Name += Spelling.name();
3879
3880 Spellings[(size_t)Kind].push_back(Name);
3881 }
3882};
3883
Aaron Ballman97dba042014-02-17 15:27:10 +00003884class DocumentationData {
3885public:
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003886 const Record *Documentation;
3887 const Record *Attribute;
Erich Keanea98a2be2017-10-16 20:31:05 +00003888 std::string Heading;
Richard Smith09ac4a12018-08-30 19:16:33 +00003889 SpellingList SupportedSpellings;
Aaron Ballman97dba042014-02-17 15:27:10 +00003890
Erich Keanea98a2be2017-10-16 20:31:05 +00003891 DocumentationData(const Record &Documentation, const Record &Attribute,
Richard Smith09ac4a12018-08-30 19:16:33 +00003892 std::pair<std::string, SpellingList> HeadingAndSpellings)
Erich Keanea98a2be2017-10-16 20:31:05 +00003893 : Documentation(&Documentation), Attribute(&Attribute),
Richard Smith09ac4a12018-08-30 19:16:33 +00003894 Heading(std::move(HeadingAndSpellings.first)),
3895 SupportedSpellings(std::move(HeadingAndSpellings.second)) {}
Aaron Ballman97dba042014-02-17 15:27:10 +00003896};
3897
Aaron Ballman4de1b582014-02-19 22:59:32 +00003898static void WriteCategoryHeader(const Record *DocCategory,
Aaron Ballman97dba042014-02-17 15:27:10 +00003899 raw_ostream &OS) {
Erich Keane3bff4142017-10-16 23:25:24 +00003900 const StringRef Name = DocCategory->getValueAsString("Name");
3901 OS << Name << "\n" << std::string(Name.size(), '=') << "\n";
Aaron Ballman4de1b582014-02-19 22:59:32 +00003902
3903 // If there is content, print that as well.
Erich Keane3bff4142017-10-16 23:25:24 +00003904 const StringRef ContentStr = DocCategory->getValueAsString("Content");
Benjamin Kramer5c404072015-04-10 21:37:21 +00003905 // Trim leading and trailing newlines and spaces.
Erich Keane3bff4142017-10-16 23:25:24 +00003906 OS << ContentStr.trim();
Benjamin Kramer5c404072015-04-10 21:37:21 +00003907
Aaron Ballman4de1b582014-02-19 22:59:32 +00003908 OS << "\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003909}
3910
Richard Smith09ac4a12018-08-30 19:16:33 +00003911static std::pair<std::string, SpellingList>
3912GetAttributeHeadingAndSpellings(const Record &Documentation,
3913 const Record &Attribute) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003914 // FIXME: there is no way to have a per-spelling category for the attribute
3915 // documentation. This may not be a limiting factor since the spellings
3916 // should generally be consistently applied across the category.
3917
Erich Keanea98a2be2017-10-16 20:31:05 +00003918 std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(Attribute);
Richard Smith09ac4a12018-08-30 19:16:33 +00003919 if (Spellings.empty())
3920 PrintFatalError(Attribute.getLoc(),
3921 "Attribute has no supported spellings; cannot be "
3922 "documented");
Aaron Ballman97dba042014-02-17 15:27:10 +00003923
3924 // Determine the heading to be used for this attribute.
Erich Keanea98a2be2017-10-16 20:31:05 +00003925 std::string Heading = Documentation.getValueAsString("Heading");
Aaron Ballman97dba042014-02-17 15:27:10 +00003926 if (Heading.empty()) {
3927 // If there's only one spelling, we can simply use that.
3928 if (Spellings.size() == 1)
3929 Heading = Spellings.begin()->name();
3930 else {
3931 std::set<std::string> Uniques;
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00003932 for (auto I = Spellings.begin(), E = Spellings.end();
3933 I != E && Uniques.size() <= 1; ++I) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003934 std::string Spelling = NormalizeNameForSpellingComparison(I->name());
3935 Uniques.insert(Spelling);
3936 }
3937 // If the semantic map has only one spelling, that is sufficient for our
3938 // needs.
3939 if (Uniques.size() == 1)
3940 Heading = *Uniques.begin();
3941 }
3942 }
3943
3944 // If the heading is still empty, it is an error.
3945 if (Heading.empty())
Erich Keanea98a2be2017-10-16 20:31:05 +00003946 PrintFatalError(Attribute.getLoc(),
Aaron Ballman97dba042014-02-17 15:27:10 +00003947 "This attribute requires a heading to be specified");
3948
Richard Smith09ac4a12018-08-30 19:16:33 +00003949 SpellingList SupportedSpellings;
3950 for (const auto &I : Spellings)
3951 SupportedSpellings.add(Attribute, I);
Aaron Ballman97dba042014-02-17 15:27:10 +00003952
Richard Smith09ac4a12018-08-30 19:16:33 +00003953 return std::make_pair(std::move(Heading), std::move(SupportedSpellings));
Erich Keanea98a2be2017-10-16 20:31:05 +00003954}
3955
3956static void WriteDocumentation(RecordKeeper &Records,
3957 const DocumentationData &Doc, raw_ostream &OS) {
3958 OS << Doc.Heading << "\n" << std::string(Doc.Heading.length(), '-') << "\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003959
3960 // List what spelling syntaxes the attribute supports.
3961 OS << ".. csv-table:: Supported Syntaxes\n";
Richard Smith09ac4a12018-08-30 19:16:33 +00003962 OS << " :header: \"GNU\", \"C++11\", \"C2x\", \"``__declspec``\",";
3963 OS << " \"Keyword\", \"``#pragma``\", \"``#pragma clang attribute``\"\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003964 OS << " \"";
Richard Smith09ac4a12018-08-30 19:16:33 +00003965 for (size_t Kind = 0; Kind != NumSpellingKinds; ++Kind) {
3966 SpellingKind K = (SpellingKind)Kind;
Richard Smith23ff7e82018-08-30 19:19:15 +00003967 // TODO: List Microsoft (IDL-style attribute) spellings once we fully
3968 // support them.
Richard Smith09ac4a12018-08-30 19:16:33 +00003969 if (K == SpellingKind::Microsoft)
3970 continue;
3971
3972 bool PrintedAny = false;
3973 for (StringRef Spelling : Doc.SupportedSpellings[K]) {
3974 if (PrintedAny)
3975 OS << " |br| ";
3976 OS << "``" << Spelling << "``";
3977 PrintedAny = true;
3978 }
3979
3980 OS << "\",\"";
3981 }
3982
3983 if (getPragmaAttributeSupport(Records).isAttributedSupported(
3984 *Doc.Attribute))
3985 OS << "Yes";
Tyler Nowickie8b07ed2014-06-13 17:57:25 +00003986 OS << "\"\n\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00003987
3988 // If the attribute is deprecated, print a message about it, and possibly
3989 // provide a replacement attribute.
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003990 if (!Doc.Documentation->isValueUnset("Deprecated")) {
Aaron Ballman97dba042014-02-17 15:27:10 +00003991 OS << "This attribute has been deprecated, and may be removed in a future "
3992 << "version of Clang.";
Aaron Ballman1a3e5852014-02-17 16:18:32 +00003993 const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated");
Erich Keane3bff4142017-10-16 23:25:24 +00003994 const StringRef Replacement = Deprecated.getValueAsString("Replacement");
Aaron Ballman97dba042014-02-17 15:27:10 +00003995 if (!Replacement.empty())
Joel E. Denny6053ec22018-02-28 16:57:33 +00003996 OS << " This attribute has been superseded by ``" << Replacement
3997 << "``.";
Aaron Ballman97dba042014-02-17 15:27:10 +00003998 OS << "\n\n";
3999 }
4000
Erich Keane3bff4142017-10-16 23:25:24 +00004001 const StringRef ContentStr = Doc.Documentation->getValueAsString("Content");
Aaron Ballman97dba042014-02-17 15:27:10 +00004002 // Trim leading and trailing newlines and spaces.
Erich Keane3bff4142017-10-16 23:25:24 +00004003 OS << ContentStr.trim();
Aaron Ballman97dba042014-02-17 15:27:10 +00004004
4005 OS << "\n\n\n";
4006}
4007
4008void EmitClangAttrDocs(RecordKeeper &Records, raw_ostream &OS) {
4009 // Get the documentation introduction paragraph.
4010 const Record *Documentation = Records.getDef("GlobalDocumentation");
4011 if (!Documentation) {
4012 PrintFatalError("The Documentation top-level definition is missing, "
4013 "no documentation will be generated.");
4014 return;
4015 }
4016
Aaron Ballman4de1b582014-02-19 22:59:32 +00004017 OS << Documentation->getValueAsString("Intro") << "\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00004018
Aaron Ballman97dba042014-02-17 15:27:10 +00004019 // Gather the Documentation lists from each of the attributes, based on the
4020 // category provided.
4021 std::vector<Record *> Attrs = Records.getAllDerivedDefinitions("Attr");
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00004022 std::map<const Record *, std::vector<DocumentationData>> SplitDocs;
Aaron Ballman2f22b942014-05-20 19:47:14 +00004023 for (const auto *A : Attrs) {
4024 const Record &Attr = *A;
Aaron Ballman97dba042014-02-17 15:27:10 +00004025 std::vector<Record *> Docs = Attr.getValueAsListOfDefs("Documentation");
Aaron Ballman2f22b942014-05-20 19:47:14 +00004026 for (const auto *D : Docs) {
4027 const Record &Doc = *D;
Aaron Ballman4de1b582014-02-19 22:59:32 +00004028 const Record *Category = Doc.getValueAsDef("Category");
Aaron Ballman97dba042014-02-17 15:27:10 +00004029 // If the category is "undocumented", then there cannot be any other
4030 // documentation categories (otherwise, the attribute would become
4031 // documented).
Erich Keane3bff4142017-10-16 23:25:24 +00004032 const StringRef Cat = Category->getValueAsString("Name");
Aaron Ballman4de1b582014-02-19 22:59:32 +00004033 bool Undocumented = Cat == "Undocumented";
Aaron Ballman97dba042014-02-17 15:27:10 +00004034 if (Undocumented && Docs.size() > 1)
4035 PrintFatalError(Doc.getLoc(),
4036 "Attribute is \"Undocumented\", but has multiple "
Erich Keanea98a2be2017-10-16 20:31:05 +00004037 "documentation categories");
Aaron Ballman97dba042014-02-17 15:27:10 +00004038
4039 if (!Undocumented)
Erich Keanea98a2be2017-10-16 20:31:05 +00004040 SplitDocs[Category].push_back(DocumentationData(
Richard Smith09ac4a12018-08-30 19:16:33 +00004041 Doc, Attr, GetAttributeHeadingAndSpellings(Doc, Attr)));
Aaron Ballman97dba042014-02-17 15:27:10 +00004042 }
4043 }
4044
4045 // Having split the attributes out based on what documentation goes where,
4046 // we can begin to generate sections of documentation.
Erich Keanea98a2be2017-10-16 20:31:05 +00004047 for (auto &I : SplitDocs) {
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00004048 WriteCategoryHeader(I.first, OS);
Aaron Ballman97dba042014-02-17 15:27:10 +00004049
Fangrui Song1d38c132018-09-30 21:41:11 +00004050 llvm::sort(I.second,
Mandeep Singh Grangc205d8c2018-03-27 16:50:00 +00004051 [](const DocumentationData &D1, const DocumentationData &D2) {
4052 return D1.Heading < D2.Heading;
Fangrui Song1d38c132018-09-30 21:41:11 +00004053 });
Erich Keanea98a2be2017-10-16 20:31:05 +00004054
Aaron Ballman97dba042014-02-17 15:27:10 +00004055 // Walk over each of the attributes in the category and write out their
4056 // documentation.
Aaron Ballmanb097f7fe2014-03-02 17:38:37 +00004057 for (const auto &Doc : I.second)
Alex Lorenz9e7bf162017-04-18 14:33:39 +00004058 WriteDocumentation(Records, Doc, OS);
4059 }
4060}
4061
4062void EmitTestPragmaAttributeSupportedAttributes(RecordKeeper &Records,
4063 raw_ostream &OS) {
4064 PragmaClangAttributeSupport Support = getPragmaAttributeSupport(Records);
4065 ParsedAttrMap Attrs = getParsedAttrList(Records);
Erik Pilkington23c48c22018-12-04 00:31:31 +00004066 OS << "#pragma clang attribute supports the following attributes:\n";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00004067 for (const auto &I : Attrs) {
4068 if (!Support.isAttributedSupported(*I.second))
4069 continue;
4070 OS << I.first;
4071 if (I.second->isValueUnset("Subjects")) {
4072 OS << " ()\n";
4073 continue;
4074 }
4075 const Record *SubjectObj = I.second->getValueAsDef("Subjects");
4076 std::vector<Record *> Subjects =
4077 SubjectObj->getValueAsListOfDefs("Subjects");
4078 OS << " (";
4079 for (const auto &Subject : llvm::enumerate(Subjects)) {
4080 if (Subject.index())
4081 OS << ", ";
Alex Lorenz24952fb2017-04-19 15:52:11 +00004082 PragmaClangAttributeSupport::RuleOrAggregateRuleSet &RuleSet =
4083 Support.SubjectsToRules.find(Subject.value())->getSecond();
4084 if (RuleSet.isRule()) {
4085 OS << RuleSet.getRule().getEnumValueName();
4086 continue;
4087 }
4088 OS << "(";
4089 for (const auto &Rule : llvm::enumerate(RuleSet.getAggregateRuleSet())) {
4090 if (Rule.index())
4091 OS << ", ";
4092 OS << Rule.value().getEnumValueName();
4093 }
4094 OS << ")";
Alex Lorenz9e7bf162017-04-18 14:33:39 +00004095 }
4096 OS << ")\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00004097 }
Erik Pilkington23c48c22018-12-04 00:31:31 +00004098 OS << "End of supported attributes.\n";
Aaron Ballman97dba042014-02-17 15:27:10 +00004099}
4100
Jakob Stoklund Olesen995e0e12012-06-13 05:12:41 +00004101} // end namespace clang